- Fix handling of structures stored in memory
[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                 struct occurance *spot;
961                 spot = triple->occurance;
962                 while(spot->parent) {
963                         spot = spot->parent;
964                 }
965                 fprintf(fp, "%s:%d.%d: ", 
966                         spot->filename, spot->line, spot->col);
967                 return;
968         }
969         if (!state->file) {
970                 return;
971         }
972         col = get_col(state->file);
973         fprintf(fp, "%s:%d.%d: ", 
974                 state->file->report_name, state->file->report_line, col);
975 }
976
977 static void __internal_error(struct compile_state *state, struct triple *ptr, 
978         char *fmt, ...)
979 {
980         va_list args;
981         va_start(args, fmt);
982         loc(stderr, state, ptr);
983         if (ptr) {
984                 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
985         }
986         fprintf(stderr, "Internal compiler error: ");
987         vfprintf(stderr, fmt, args);
988         fprintf(stderr, "\n");
989         va_end(args);
990         do_cleanup(state);
991         abort();
992 }
993
994
995 static void __internal_warning(struct compile_state *state, struct triple *ptr, 
996         char *fmt, ...)
997 {
998         va_list args;
999         va_start(args, fmt);
1000         loc(stderr, state, ptr);
1001         fprintf(stderr, "Internal compiler warning: ");
1002         vfprintf(stderr, fmt, args);
1003         fprintf(stderr, "\n");
1004         va_end(args);
1005 }
1006
1007
1008
1009 static void __error(struct compile_state *state, struct triple *ptr, 
1010         char *fmt, ...)
1011 {
1012         va_list args;
1013         va_start(args, fmt);
1014         loc(stderr, state, ptr);
1015         vfprintf(stderr, fmt, args);
1016         va_end(args);
1017         fprintf(stderr, "\n");
1018         do_cleanup(state);
1019         if (state->debug & DEBUG_ABORT_ON_ERROR) {
1020                 abort();
1021         }
1022         exit(1);
1023 }
1024
1025 static void __warning(struct compile_state *state, struct triple *ptr, 
1026         char *fmt, ...)
1027 {
1028         va_list args;
1029         va_start(args, fmt);
1030         loc(stderr, state, ptr);
1031         fprintf(stderr, "warning: "); 
1032         vfprintf(stderr, fmt, args);
1033         fprintf(stderr, "\n");
1034         va_end(args);
1035 }
1036
1037 #if DEBUG_ERROR_MESSAGES 
1038 #  define internal_error fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1039 #  define internal_warning fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1040 #  define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1041 #  define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1042 #else
1043 #  define internal_error __internal_error
1044 #  define internal_warning __internal_warning
1045 #  define error __error
1046 #  define warning __warning
1047 #endif
1048 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1049
1050 static void valid_op(struct compile_state *state, int op)
1051 {
1052         char *fmt = "invalid op: %d";
1053         if (op >= OP_MAX) {
1054                 internal_error(state, 0, fmt, op);
1055         }
1056         if (op < 0) {
1057                 internal_error(state, 0, fmt, op);
1058         }
1059 }
1060
1061 static void valid_ins(struct compile_state *state, struct triple *ptr)
1062 {
1063         valid_op(state, ptr->op);
1064 }
1065
1066 static void process_trigraphs(struct compile_state *state)
1067 {
1068         char *src, *dest, *end;
1069         struct file_state *file;
1070         file = state->file;
1071         src = dest = file->buf;
1072         end = file->buf + file->size;
1073         while((end - src) >= 3) {
1074                 if ((src[0] == '?') && (src[1] == '?')) {
1075                         int c = -1;
1076                         switch(src[2]) {
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                         case '<': c = '{'; break;
1084                         case '>': c = '}'; break;
1085                         case '-': c = '~'; break;
1086                         }
1087                         if (c != -1) {
1088                                 *dest++ = c;
1089                                 src += 3;
1090                         }
1091                         else {
1092                                 *dest++ = *src++;
1093                         }
1094                 }
1095                 else {
1096                         *dest++ = *src++;
1097                 }
1098         }
1099         while(src != end) {
1100                 *dest++ = *src++;
1101         }
1102         file->size = dest - file->buf;
1103 }
1104
1105 static void splice_lines(struct compile_state *state)
1106 {
1107         char *src, *dest, *end;
1108         struct file_state *file;
1109         file = state->file;
1110         src = dest = file->buf;
1111         end = file->buf + file->size;
1112         while((end - src) >= 2) {
1113                 if ((src[0] == '\\') && (src[1] == '\n')) {
1114                         src += 2;
1115                 }
1116                 else {
1117                         *dest++ = *src++;
1118                 }
1119         }
1120         while(src != end) {
1121                 *dest++ = *src++;
1122         }
1123         file->size = dest - file->buf;
1124 }
1125
1126 static struct type void_type;
1127 static void use_triple(struct triple *used, struct triple *user)
1128 {
1129         struct triple_set **ptr, *new;
1130         if (!used)
1131                 return;
1132         if (!user)
1133                 return;
1134         ptr = &used->use;
1135         while(*ptr) {
1136                 if ((*ptr)->member == user) {
1137                         return;
1138                 }
1139                 ptr = &(*ptr)->next;
1140         }
1141         /* Append new to the head of the list, 
1142          * copy_func and rename_block_variables
1143          * depends on this.
1144          */
1145         new = xcmalloc(sizeof(*new), "triple_set");
1146         new->member = user;
1147         new->next   = used->use;
1148         used->use   = new;
1149 }
1150
1151 static void unuse_triple(struct triple *used, struct triple *unuser)
1152 {
1153         struct triple_set *use, **ptr;
1154         if (!used) {
1155                 return;
1156         }
1157         ptr = &used->use;
1158         while(*ptr) {
1159                 use = *ptr;
1160                 if (use->member == unuser) {
1161                         *ptr = use->next;
1162                         xfree(use);
1163                 }
1164                 else {
1165                         ptr = &use->next;
1166                 }
1167         }
1168 }
1169
1170 static void push_triple(struct triple *used, struct triple *user)
1171 {
1172         struct triple_set *new;
1173         if (!used)
1174                 return;
1175         if (!user)
1176                 return;
1177         /* Append new to the head of the list,
1178          * it's the only sensible behavoir for a stack.
1179          */
1180         new = xcmalloc(sizeof(*new), "triple_set");
1181         new->member = user;
1182         new->next   = used->use;
1183         used->use   = new;
1184 }
1185
1186 static void pop_triple(struct triple *used, struct triple *unuser)
1187 {
1188         struct triple_set *use, **ptr;
1189         ptr = &used->use;
1190         while(*ptr) {
1191                 use = *ptr;
1192                 if (use->member == unuser) {
1193                         *ptr = use->next;
1194                         xfree(use);
1195                         /* Only free one occurance from the stack */
1196                         return;
1197                 }
1198                 else {
1199                         ptr = &use->next;
1200                 }
1201         }
1202 }
1203
1204 static void put_occurance(struct occurance *occurance)
1205 {
1206         occurance->count -= 1;
1207         if (occurance->count <= 0) {
1208                 if (occurance->parent) {
1209                         put_occurance(occurance->parent);
1210                 }
1211                 xfree(occurance);
1212         }
1213 }
1214
1215 static void get_occurance(struct occurance *occurance)
1216 {
1217         occurance->count += 1;
1218 }
1219
1220
1221 static struct occurance *new_occurance(struct compile_state *state)
1222 {
1223         struct occurance *result, *last;
1224         const char *filename;
1225         const char *function;
1226         int line, col;
1227
1228         function = "";
1229         filename = 0;
1230         line = 0;
1231         col  = 0;
1232         if (state->file) {
1233                 filename = state->file->report_name;
1234                 line     = state->file->report_line;
1235                 col      = get_col(state->file);
1236         }
1237         if (state->function) {
1238                 function = state->function;
1239         }
1240         last = state->last_occurance;
1241         if (last &&
1242                 (last->col == col) &&
1243                 (last->line == line) &&
1244                 (last->function == function) &&
1245                 (strcmp(last->filename, filename) == 0)) {
1246                 get_occurance(last);
1247                 return last;
1248         }
1249         if (last) {
1250                 state->last_occurance = 0;
1251                 put_occurance(last);
1252         }
1253         result = xmalloc(sizeof(*result), "occurance");
1254         result->count    = 2;
1255         result->filename = filename;
1256         result->function = function;
1257         result->line     = line;
1258         result->col      = col;
1259         result->parent   = 0;
1260         state->last_occurance = result;
1261         return result;
1262 }
1263
1264 static struct occurance *inline_occurance(struct compile_state *state,
1265         struct occurance *new, struct occurance *orig)
1266 {
1267         struct occurance *result, *last;
1268         last = state->last_occurance;
1269         if (last &&
1270                 (last->parent   == orig) &&
1271                 (last->col      == new->col) &&
1272                 (last->line     == new->line) &&
1273                 (last->function == new->function) &&
1274                 (last->filename == new->filename)) {
1275                 get_occurance(last);
1276                 return last;
1277         }
1278         if (last) {
1279                 state->last_occurance = 0;
1280                 put_occurance(last);
1281         }
1282         get_occurance(orig);
1283         result = xmalloc(sizeof(*result), "occurance");
1284         result->count    = 2;
1285         result->filename = new->filename;
1286         result->function = new->function;
1287         result->line     = new->line;
1288         result->col      = new->col;
1289         result->parent   = orig;
1290         state->last_occurance = result;
1291         return result;
1292 }
1293         
1294
1295 static struct occurance dummy_occurance = {
1296         .count    = 2,
1297         .filename = __FILE__,
1298         .function = "",
1299         .line     = __LINE__,
1300         .col      = 0,
1301         .parent   = 0,
1302 };
1303
1304 /* The zero triple is used as a place holder when we are removing pointers
1305  * from a triple.  Having allows certain sanity checks to pass even
1306  * when the original triple that was pointed to is gone.
1307  */
1308 static struct triple zero_triple = {
1309         .next      = &zero_triple,
1310         .prev      = &zero_triple,
1311         .use       = 0,
1312         .op        = OP_INTCONST,
1313         .sizes     = TRIPLE_SIZES(0, 0, 0, 0),
1314         .id        = -1, /* An invalid id */
1315         .u = { .cval   = 0, },
1316         .occurance = &dummy_occurance,
1317         .param { [0] = 0, [1] = 0, },
1318 };
1319
1320
1321 static unsigned short triple_sizes(struct compile_state *state,
1322         int op, struct type *type, int lhs_wanted, int rhs_wanted)
1323 {
1324         int lhs, rhs, misc, targ;
1325         valid_op(state, op);
1326         lhs = table_ops[op].lhs;
1327         rhs = table_ops[op].rhs;
1328         misc = table_ops[op].misc;
1329         targ = table_ops[op].targ;
1330         
1331         
1332         if (op == OP_CALL) {
1333                 struct type *param;
1334                 rhs = 0;
1335                 param = type->right;
1336                 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1337                         rhs++;
1338                         param = param->right;
1339                 }
1340                 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1341                         rhs++;
1342                 }
1343                 lhs = 0;
1344                 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1345                         lhs = type->left->elements;
1346                 }
1347         }
1348         else if (op == OP_VAL_VEC) {
1349                 rhs = type->elements;
1350         }
1351         else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1352                 rhs = rhs_wanted;
1353         }
1354         else if (op == OP_ASM) {
1355                 rhs = rhs_wanted;
1356                 lhs = lhs_wanted;
1357         }
1358         if ((rhs < 0) || (rhs > MAX_RHS)) {
1359                 internal_error(state, 0, "bad rhs");
1360         }
1361         if ((lhs < 0) || (lhs > MAX_LHS)) {
1362                 internal_error(state, 0, "bad lhs");
1363         }
1364         if ((misc < 0) || (misc > MAX_MISC)) {
1365                 internal_error(state, 0, "bad misc");
1366         }
1367         if ((targ < 0) || (targ > MAX_TARG)) {
1368                 internal_error(state, 0, "bad targs");
1369         }
1370         return TRIPLE_SIZES(lhs, rhs, misc, targ);
1371 }
1372
1373 static struct triple *alloc_triple(struct compile_state *state, 
1374         int op, struct type *type, int lhs, int rhs,
1375         struct occurance *occurance)
1376 {
1377         size_t size, sizes, extra_count, min_count;
1378         struct triple *ret;
1379         sizes = triple_sizes(state, op, type, lhs, rhs);
1380
1381         min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1382         extra_count = TRIPLE_SIZE(sizes);
1383         extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1384
1385         size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1386         ret = xcmalloc(size, "tripple");
1387         ret->op        = op;
1388         ret->sizes     = sizes;
1389         ret->type      = type;
1390         ret->next      = ret;
1391         ret->prev      = ret;
1392         ret->occurance = occurance;
1393         return ret;
1394 }
1395
1396 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1397 {
1398         struct triple *dup;
1399         int src_lhs, src_rhs, src_size;
1400         src_lhs = TRIPLE_LHS(src->sizes);
1401         src_rhs = TRIPLE_RHS(src->sizes);
1402         src_size = TRIPLE_SIZE(src->sizes);
1403         get_occurance(src->occurance);
1404         dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1405                 src->occurance);
1406         memcpy(dup, src, sizeof(*src));
1407         memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1408         return dup;
1409 }
1410
1411 static struct triple *new_triple(struct compile_state *state, 
1412         int op, struct type *type, int lhs, int rhs)
1413 {
1414         struct triple *ret;
1415         struct occurance *occurance;
1416         occurance = new_occurance(state);
1417         ret = alloc_triple(state, op, type, lhs, rhs, occurance);
1418         return ret;
1419 }
1420
1421 static struct triple *build_triple(struct compile_state *state, 
1422         int op, struct type *type, struct triple *left, struct triple *right,
1423         struct occurance *occurance)
1424 {
1425         struct triple *ret;
1426         size_t count;
1427         ret = alloc_triple(state, op, type, -1, -1, occurance);
1428         count = TRIPLE_SIZE(ret->sizes);
1429         if (count > 0) {
1430                 ret->param[0] = left;
1431         }
1432         if (count > 1) {
1433                 ret->param[1] = right;
1434         }
1435         return ret;
1436 }
1437
1438 static struct triple *triple(struct compile_state *state, 
1439         int op, struct type *type, struct triple *left, struct triple *right)
1440 {
1441         struct triple *ret;
1442         size_t count;
1443         ret = new_triple(state, op, type, -1, -1);
1444         count = TRIPLE_SIZE(ret->sizes);
1445         if (count >= 1) {
1446                 ret->param[0] = left;
1447         }
1448         if (count >= 2) {
1449                 ret->param[1] = right;
1450         }
1451         return ret;
1452 }
1453
1454 static struct triple *branch(struct compile_state *state, 
1455         struct triple *targ, struct triple *test)
1456 {
1457         struct triple *ret;
1458         ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1459         if (test) {
1460                 RHS(ret, 0) = test;
1461         }
1462         TARG(ret, 0) = targ;
1463         /* record the branch target was used */
1464         if (!targ || (targ->op != OP_LABEL)) {
1465                 internal_error(state, 0, "branch not to label");
1466                 use_triple(targ, ret);
1467         }
1468         return ret;
1469 }
1470
1471
1472 static void insert_triple(struct compile_state *state,
1473         struct triple *first, struct triple *ptr)
1474 {
1475         if (ptr) {
1476                 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1477                         internal_error(state, ptr, "expression already used");
1478                 }
1479                 ptr->next       = first;
1480                 ptr->prev       = first->prev;
1481                 ptr->prev->next = ptr;
1482                 ptr->next->prev = ptr;
1483                 if ((ptr->prev->op == OP_BRANCH) && 
1484                         TRIPLE_RHS(ptr->prev->sizes)) {
1485                         unuse_triple(first, ptr->prev);
1486                         use_triple(ptr, ptr->prev);
1487                 }
1488         }
1489 }
1490
1491 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1492 {
1493         /* This function is used to determine if u.block 
1494          * is utilized to store the current block number.
1495          */
1496         int stores_block;
1497         valid_ins(state, ins);
1498         stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1499         return stores_block;
1500 }
1501
1502 static struct block *block_of_triple(struct compile_state *state, 
1503         struct triple *ins)
1504 {
1505         struct triple *first;
1506         first = RHS(state->main_function, 0);
1507         while(ins != first && !triple_stores_block(state, ins)) {
1508                 if (ins == ins->prev) {
1509                         internal_error(state, 0, "ins == ins->prev?");
1510                 }
1511                 ins = ins->prev;
1512         }
1513         if (!triple_stores_block(state, ins)) {
1514                 internal_error(state, ins, "Cannot find block");
1515         }
1516         return ins->u.block;
1517 }
1518
1519 static struct triple *pre_triple(struct compile_state *state,
1520         struct triple *base,
1521         int op, struct type *type, struct triple *left, struct triple *right)
1522 {
1523         struct block *block;
1524         struct triple *ret;
1525         /* If I am an OP_PIECE jump to the real instruction */
1526         if (base->op == OP_PIECE) {
1527                 base = MISC(base, 0);
1528         }
1529         block = block_of_triple(state, base);
1530         get_occurance(base->occurance);
1531         ret = build_triple(state, op, type, left, right, base->occurance);
1532         if (triple_stores_block(state, ret)) {
1533                 ret->u.block = block;
1534         }
1535         insert_triple(state, base, ret);
1536         if (block->first == base) {
1537                 block->first = ret;
1538         }
1539         return ret;
1540 }
1541
1542 static struct triple *post_triple(struct compile_state *state,
1543         struct triple *base,
1544         int op, struct type *type, struct triple *left, struct triple *right)
1545 {
1546         struct block *block;
1547         struct triple *ret;
1548         int zlhs;
1549         /* If I am an OP_PIECE jump to the real instruction */
1550         if (base->op == OP_PIECE) {
1551                 base = MISC(base, 0);
1552         }
1553         /* If I have a left hand side skip over it */
1554         zlhs = TRIPLE_LHS(base->sizes);
1555         if (zlhs && (base->op != OP_WRITE) && (base->op != OP_STORE)) {
1556                 base = LHS(base, zlhs - 1);
1557         }
1558
1559         block = block_of_triple(state, base);
1560         get_occurance(base->occurance);
1561         ret = build_triple(state, op, type, left, right, base->occurance);
1562         if (triple_stores_block(state, ret)) {
1563                 ret->u.block = block;
1564         }
1565         insert_triple(state, base->next, ret);
1566         if (block->last == base) {
1567                 block->last = ret;
1568         }
1569         return ret;
1570 }
1571
1572 static struct triple *label(struct compile_state *state)
1573 {
1574         /* Labels don't get a type */
1575         struct triple *result;
1576         result = triple(state, OP_LABEL, &void_type, 0, 0);
1577         return result;
1578 }
1579
1580 static void display_triple(FILE *fp, struct triple *ins)
1581 {
1582         struct occurance *ptr;
1583         const char *reg;
1584         char pre, post;
1585         pre = post = ' ';
1586         if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1587                 pre = '^';
1588         }
1589         if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1590                 post = 'v';
1591         }
1592         reg = arch_reg_str(ID_REG(ins->id));
1593         if (ins->op == OP_INTCONST) {
1594                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx>         ",
1595                         ins, pre, post, reg, ins->template_id, tops(ins->op), 
1596                         ins->u.cval);
1597         }
1598         else if (ins->op == OP_ADDRCONST) {
1599                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1600                         ins, pre, post, reg, ins->template_id, tops(ins->op), 
1601                         MISC(ins, 0), ins->u.cval);
1602         }
1603         else {
1604                 int i, count;
1605                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s", 
1606                         ins, pre, post, reg, ins->template_id, tops(ins->op));
1607                 count = TRIPLE_SIZE(ins->sizes);
1608                 for(i = 0; i < count; i++) {
1609                         fprintf(fp, " %-10p", ins->param[i]);
1610                 }
1611                 for(; i < 2; i++) {
1612                         fprintf(fp, "           ");
1613                 }
1614         }
1615         fprintf(fp, " @");
1616         for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1617                 fprintf(fp, " %s,%s:%d.%d",
1618                         ptr->function, 
1619                         ptr->filename,
1620                         ptr->line, 
1621                         ptr->col);
1622         }
1623         fprintf(fp, "\n");
1624         fflush(fp);
1625 }
1626
1627 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1628 {
1629         /* Does the triple have no side effects.
1630          * I.e. Rexecuting the triple with the same arguments 
1631          * gives the same value.
1632          */
1633         unsigned pure;
1634         valid_ins(state, ins);
1635         pure = PURE_BITS(table_ops[ins->op].flags);
1636         if ((pure != PURE) && (pure != IMPURE)) {
1637                 internal_error(state, 0, "Purity of %s not known\n",
1638                         tops(ins->op));
1639         }
1640         return pure == PURE;
1641 }
1642
1643 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1644 {
1645         /* This function is used to determine which triples need
1646          * a register.
1647          */
1648         int is_branch;
1649         valid_ins(state, ins);
1650         is_branch = (table_ops[ins->op].targ != 0);
1651         return is_branch;
1652 }
1653
1654 static int triple_is_def(struct compile_state *state, struct triple *ins)
1655 {
1656         /* This function is used to determine which triples need
1657          * a register.
1658          */
1659         int is_def;
1660         valid_ins(state, ins);
1661         is_def = (table_ops[ins->op].flags & DEF) == DEF;
1662         return is_def;
1663 }
1664
1665 static struct triple **triple_iter(struct compile_state *state,
1666         size_t count, struct triple **vector,
1667         struct triple *ins, struct triple **last)
1668 {
1669         struct triple **ret;
1670         ret = 0;
1671         if (count) {
1672                 if (!last) {
1673                         ret = vector;
1674                 }
1675                 else if ((last >= vector) && (last < (vector + count - 1))) {
1676                         ret = last + 1;
1677                 }
1678         }
1679         return ret;
1680         
1681 }
1682
1683 static struct triple **triple_lhs(struct compile_state *state,
1684         struct triple *ins, struct triple **last)
1685 {
1686         return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0), 
1687                 ins, last);
1688 }
1689
1690 static struct triple **triple_rhs(struct compile_state *state,
1691         struct triple *ins, struct triple **last)
1692 {
1693         return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0), 
1694                 ins, last);
1695 }
1696
1697 static struct triple **triple_misc(struct compile_state *state,
1698         struct triple *ins, struct triple **last)
1699 {
1700         return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0), 
1701                 ins, last);
1702 }
1703
1704 static struct triple **triple_targ(struct compile_state *state,
1705         struct triple *ins, struct triple **last)
1706 {
1707         size_t count;
1708         struct triple **ret, **vector;
1709         ret = 0;
1710         count = TRIPLE_TARG(ins->sizes);
1711         vector = &TARG(ins, 0);
1712         if (count) {
1713                 if (!last) {
1714                         ret = vector;
1715                 }
1716                 else if ((last >= vector) && (last < (vector + count - 1))) {
1717                         ret = last + 1;
1718                 }
1719                 else if ((last == (vector + count - 1)) && 
1720                         TRIPLE_RHS(ins->sizes)) {
1721                         ret = &ins->next;
1722                 }
1723         }
1724         return ret;
1725 }
1726
1727
1728 static void verify_use(struct compile_state *state,
1729         struct triple *user, struct triple *used)
1730 {
1731         int size, i;
1732         size = TRIPLE_SIZE(user->sizes);
1733         for(i = 0; i < size; i++) {
1734                 if (user->param[i] == used) {
1735                         break;
1736                 }
1737         }
1738         if (triple_is_branch(state, user)) {
1739                 if (user->next == used) {
1740                         i = -1;
1741                 }
1742         }
1743         if (i == size) {
1744                 internal_error(state, user, "%s(%p) does not use %s(%p)",
1745                         tops(user->op), user, tops(used->op), used);
1746         }
1747 }
1748
1749 static int find_rhs_use(struct compile_state *state, 
1750         struct triple *user, struct triple *used)
1751 {
1752         struct triple **param;
1753         int size, i;
1754         verify_use(state, user, used);
1755         size = TRIPLE_RHS(user->sizes);
1756         param = &RHS(user, 0);
1757         for(i = 0; i < size; i++) {
1758                 if (param[i] == used) {
1759                         return i;
1760                 }
1761         }
1762         return -1;
1763 }
1764
1765 static void free_triple(struct compile_state *state, struct triple *ptr)
1766 {
1767         size_t size;
1768         size = sizeof(*ptr) - sizeof(ptr->param) +
1769                 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1770         ptr->prev->next = ptr->next;
1771         ptr->next->prev = ptr->prev;
1772         if (ptr->use) {
1773                 internal_error(state, ptr, "ptr->use != 0");
1774         }
1775         put_occurance(ptr->occurance);
1776         memset(ptr, -1, size);
1777         xfree(ptr);
1778 }
1779
1780 static void release_triple(struct compile_state *state, struct triple *ptr)
1781 {
1782         struct triple_set *set, *next;
1783         struct triple **expr;
1784         /* Remove ptr from use chains where it is the user */
1785         expr = triple_rhs(state, ptr, 0);
1786         for(; expr; expr = triple_rhs(state, ptr, expr)) {
1787                 if (*expr) {
1788                         unuse_triple(*expr, ptr);
1789                 }
1790         }
1791         expr = triple_lhs(state, ptr, 0);
1792         for(; expr; expr = triple_lhs(state, ptr, expr)) {
1793                 if (*expr) {
1794                         unuse_triple(*expr, ptr);
1795                 }
1796         }
1797         expr = triple_misc(state, ptr, 0);
1798         for(; expr; expr = triple_misc(state, ptr, expr)) {
1799                 if (*expr) {
1800                         unuse_triple(*expr, ptr);
1801                 }
1802         }
1803         expr = triple_targ(state, ptr, 0);
1804         for(; expr; expr = triple_targ(state, ptr, expr)) {
1805                 if (*expr) {
1806                         unuse_triple(*expr, ptr);
1807                 }
1808         }
1809         /* Reomve ptr from use chains where it is used */
1810         for(set = ptr->use; set; set = next) {
1811                 next = set->next;
1812                 expr = triple_rhs(state, set->member, 0);
1813                 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1814                         if (*expr == ptr) {
1815                                 *expr = &zero_triple;
1816                         }
1817                 }
1818                 expr = triple_lhs(state, set->member, 0);
1819                 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1820                         if (*expr == ptr) {
1821                                 *expr = &zero_triple;
1822                         }
1823                 }
1824                 expr = triple_misc(state, set->member, 0);
1825                 for(; expr; expr = triple_misc(state, set->member, expr)) {
1826                         if (*expr == ptr) {
1827                                 *expr = &zero_triple;
1828                         }
1829                 }
1830                 expr = triple_targ(state, set->member, 0);
1831                 for(; expr; expr = triple_targ(state, set->member, expr)) {
1832                         if (*expr == ptr) {
1833                                 *expr = &zero_triple;
1834                         }
1835                 }
1836                 unuse_triple(ptr, set->member);
1837         }
1838         free_triple(state, ptr);
1839 }
1840
1841 static void print_triple(struct compile_state *state, struct triple *ptr);
1842
1843 #define TOK_UNKNOWN     0
1844 #define TOK_SPACE       1
1845 #define TOK_SEMI        2
1846 #define TOK_LBRACE      3
1847 #define TOK_RBRACE      4
1848 #define TOK_COMMA       5
1849 #define TOK_EQ          6
1850 #define TOK_COLON       7
1851 #define TOK_LBRACKET    8
1852 #define TOK_RBRACKET    9
1853 #define TOK_LPAREN      10
1854 #define TOK_RPAREN      11
1855 #define TOK_STAR        12
1856 #define TOK_DOTS        13
1857 #define TOK_MORE        14
1858 #define TOK_LESS        15
1859 #define TOK_TIMESEQ     16
1860 #define TOK_DIVEQ       17
1861 #define TOK_MODEQ       18
1862 #define TOK_PLUSEQ      19
1863 #define TOK_MINUSEQ     20
1864 #define TOK_SLEQ        21
1865 #define TOK_SREQ        22
1866 #define TOK_ANDEQ       23
1867 #define TOK_XOREQ       24
1868 #define TOK_OREQ        25
1869 #define TOK_EQEQ        26
1870 #define TOK_NOTEQ       27
1871 #define TOK_QUEST       28
1872 #define TOK_LOGOR       29
1873 #define TOK_LOGAND      30
1874 #define TOK_OR          31
1875 #define TOK_AND         32
1876 #define TOK_XOR         33
1877 #define TOK_LESSEQ      34
1878 #define TOK_MOREEQ      35
1879 #define TOK_SL          36
1880 #define TOK_SR          37
1881 #define TOK_PLUS        38
1882 #define TOK_MINUS       39
1883 #define TOK_DIV         40
1884 #define TOK_MOD         41
1885 #define TOK_PLUSPLUS    42
1886 #define TOK_MINUSMINUS  43
1887 #define TOK_BANG        44
1888 #define TOK_ARROW       45
1889 #define TOK_DOT         46
1890 #define TOK_TILDE       47
1891 #define TOK_LIT_STRING  48
1892 #define TOK_LIT_CHAR    49
1893 #define TOK_LIT_INT     50
1894 #define TOK_LIT_FLOAT   51
1895 #define TOK_MACRO       52
1896 #define TOK_CONCATENATE 53
1897
1898 #define TOK_IDENT       54
1899 #define TOK_STRUCT_NAME 55
1900 #define TOK_ENUM_CONST  56
1901 #define TOK_TYPE_NAME   57
1902
1903 #define TOK_AUTO        58
1904 #define TOK_BREAK       59
1905 #define TOK_CASE        60
1906 #define TOK_CHAR        61
1907 #define TOK_CONST       62
1908 #define TOK_CONTINUE    63
1909 #define TOK_DEFAULT     64
1910 #define TOK_DO          65
1911 #define TOK_DOUBLE      66
1912 #define TOK_ELSE        67
1913 #define TOK_ENUM        68
1914 #define TOK_EXTERN      69
1915 #define TOK_FLOAT       70
1916 #define TOK_FOR         71
1917 #define TOK_GOTO        72
1918 #define TOK_IF          73
1919 #define TOK_INLINE      74
1920 #define TOK_INT         75
1921 #define TOK_LONG        76
1922 #define TOK_REGISTER    77
1923 #define TOK_RESTRICT    78
1924 #define TOK_RETURN      79
1925 #define TOK_SHORT       80
1926 #define TOK_SIGNED      81
1927 #define TOK_SIZEOF      82
1928 #define TOK_STATIC      83
1929 #define TOK_STRUCT      84
1930 #define TOK_SWITCH      85
1931 #define TOK_TYPEDEF     86
1932 #define TOK_UNION       87
1933 #define TOK_UNSIGNED    88
1934 #define TOK_VOID        89
1935 #define TOK_VOLATILE    90
1936 #define TOK_WHILE       91
1937 #define TOK_ASM         92
1938 #define TOK_ATTRIBUTE   93
1939 #define TOK_ALIGNOF     94
1940 #define TOK_FIRST_KEYWORD TOK_AUTO
1941 #define TOK_LAST_KEYWORD  TOK_ALIGNOF
1942
1943 #define TOK_DEFINE      100
1944 #define TOK_UNDEF       101
1945 #define TOK_INCLUDE     102
1946 #define TOK_LINE        103
1947 #define TOK_ERROR       104
1948 #define TOK_WARNING     105
1949 #define TOK_PRAGMA      106
1950 #define TOK_IFDEF       107
1951 #define TOK_IFNDEF      108
1952 #define TOK_ELIF        109
1953 #define TOK_ENDIF       110
1954
1955 #define TOK_FIRST_MACRO TOK_DEFINE
1956 #define TOK_LAST_MACRO  TOK_ENDIF
1957          
1958 #define TOK_EOF         111
1959
1960 static const char *tokens[] = {
1961 [TOK_UNKNOWN     ] = "unknown",
1962 [TOK_SPACE       ] = ":space:",
1963 [TOK_SEMI        ] = ";",
1964 [TOK_LBRACE      ] = "{",
1965 [TOK_RBRACE      ] = "}",
1966 [TOK_COMMA       ] = ",",
1967 [TOK_EQ          ] = "=",
1968 [TOK_COLON       ] = ":",
1969 [TOK_LBRACKET    ] = "[",
1970 [TOK_RBRACKET    ] = "]",
1971 [TOK_LPAREN      ] = "(",
1972 [TOK_RPAREN      ] = ")",
1973 [TOK_STAR        ] = "*",
1974 [TOK_DOTS        ] = "...",
1975 [TOK_MORE        ] = ">",
1976 [TOK_LESS        ] = "<",
1977 [TOK_TIMESEQ     ] = "*=",
1978 [TOK_DIVEQ       ] = "/=",
1979 [TOK_MODEQ       ] = "%=",
1980 [TOK_PLUSEQ      ] = "+=",
1981 [TOK_MINUSEQ     ] = "-=",
1982 [TOK_SLEQ        ] = "<<=",
1983 [TOK_SREQ        ] = ">>=",
1984 [TOK_ANDEQ       ] = "&=",
1985 [TOK_XOREQ       ] = "^=",
1986 [TOK_OREQ        ] = "|=",
1987 [TOK_EQEQ        ] = "==",
1988 [TOK_NOTEQ       ] = "!=",
1989 [TOK_QUEST       ] = "?",
1990 [TOK_LOGOR       ] = "||",
1991 [TOK_LOGAND      ] = "&&",
1992 [TOK_OR          ] = "|",
1993 [TOK_AND         ] = "&",
1994 [TOK_XOR         ] = "^",
1995 [TOK_LESSEQ      ] = "<=",
1996 [TOK_MOREEQ      ] = ">=",
1997 [TOK_SL          ] = "<<",
1998 [TOK_SR          ] = ">>",
1999 [TOK_PLUS        ] = "+",
2000 [TOK_MINUS       ] = "-",
2001 [TOK_DIV         ] = "/",
2002 [TOK_MOD         ] = "%",
2003 [TOK_PLUSPLUS    ] = "++",
2004 [TOK_MINUSMINUS  ] = "--",
2005 [TOK_BANG        ] = "!",
2006 [TOK_ARROW       ] = "->",
2007 [TOK_DOT         ] = ".",
2008 [TOK_TILDE       ] = "~",
2009 [TOK_LIT_STRING  ] = ":string:",
2010 [TOK_IDENT       ] = ":ident:",
2011 [TOK_TYPE_NAME   ] = ":typename:",
2012 [TOK_LIT_CHAR    ] = ":char:",
2013 [TOK_LIT_INT     ] = ":integer:",
2014 [TOK_LIT_FLOAT   ] = ":float:",
2015 [TOK_MACRO       ] = "#",
2016 [TOK_CONCATENATE ] = "##",
2017
2018 [TOK_AUTO        ] = "auto",
2019 [TOK_BREAK       ] = "break",
2020 [TOK_CASE        ] = "case",
2021 [TOK_CHAR        ] = "char",
2022 [TOK_CONST       ] = "const",
2023 [TOK_CONTINUE    ] = "continue",
2024 [TOK_DEFAULT     ] = "default",
2025 [TOK_DO          ] = "do",
2026 [TOK_DOUBLE      ] = "double",
2027 [TOK_ELSE        ] = "else",
2028 [TOK_ENUM        ] = "enum",
2029 [TOK_EXTERN      ] = "extern",
2030 [TOK_FLOAT       ] = "float",
2031 [TOK_FOR         ] = "for",
2032 [TOK_GOTO        ] = "goto",
2033 [TOK_IF          ] = "if",
2034 [TOK_INLINE      ] = "inline",
2035 [TOK_INT         ] = "int",
2036 [TOK_LONG        ] = "long",
2037 [TOK_REGISTER    ] = "register",
2038 [TOK_RESTRICT    ] = "restrict",
2039 [TOK_RETURN      ] = "return",
2040 [TOK_SHORT       ] = "short",
2041 [TOK_SIGNED      ] = "signed",
2042 [TOK_SIZEOF      ] = "sizeof",
2043 [TOK_STATIC      ] = "static",
2044 [TOK_STRUCT      ] = "struct",
2045 [TOK_SWITCH      ] = "switch",
2046 [TOK_TYPEDEF     ] = "typedef",
2047 [TOK_UNION       ] = "union",
2048 [TOK_UNSIGNED    ] = "unsigned",
2049 [TOK_VOID        ] = "void",
2050 [TOK_VOLATILE    ] = "volatile",
2051 [TOK_WHILE       ] = "while",
2052 [TOK_ASM         ] = "asm",
2053 [TOK_ATTRIBUTE   ] = "__attribute__",
2054 [TOK_ALIGNOF     ] = "__alignof__",
2055
2056 [TOK_DEFINE      ] = "define",
2057 [TOK_UNDEF       ] = "undef",
2058 [TOK_INCLUDE     ] = "include",
2059 [TOK_LINE        ] = "line",
2060 [TOK_ERROR       ] = "error",
2061 [TOK_WARNING     ] = "warning",
2062 [TOK_PRAGMA      ] = "pragma",
2063 [TOK_IFDEF       ] = "ifdef",
2064 [TOK_IFNDEF      ] = "ifndef",
2065 [TOK_ELIF        ] = "elif",
2066 [TOK_ENDIF       ] = "endif",
2067
2068 [TOK_EOF         ] = "EOF",
2069 };
2070
2071 static unsigned int hash(const char *str, int str_len)
2072 {
2073         unsigned int hash;
2074         const char *end;
2075         end = str + str_len;
2076         hash = 0;
2077         for(; str < end; str++) {
2078                 hash = (hash *263) + *str;
2079         }
2080         hash = hash & (HASH_TABLE_SIZE -1);
2081         return hash;
2082 }
2083
2084 static struct hash_entry *lookup(
2085         struct compile_state *state, const char *name, int name_len)
2086 {
2087         struct hash_entry *entry;
2088         unsigned int index;
2089         index = hash(name, name_len);
2090         entry = state->hash_table[index];
2091         while(entry && 
2092                 ((entry->name_len != name_len) ||
2093                         (memcmp(entry->name, name, name_len) != 0))) {
2094                 entry = entry->next;
2095         }
2096         if (!entry) {
2097                 char *new_name;
2098                 /* Get a private copy of the name */
2099                 new_name = xmalloc(name_len + 1, "hash_name");
2100                 memcpy(new_name, name, name_len);
2101                 new_name[name_len] = '\0';
2102
2103                 /* Create a new hash entry */
2104                 entry = xcmalloc(sizeof(*entry), "hash_entry");
2105                 entry->next = state->hash_table[index];
2106                 entry->name = new_name;
2107                 entry->name_len = name_len;
2108
2109                 /* Place the new entry in the hash table */
2110                 state->hash_table[index] = entry;
2111         }
2112         return entry;
2113 }
2114
2115 static void ident_to_keyword(struct compile_state *state, struct token *tk)
2116 {
2117         struct hash_entry *entry;
2118         entry = tk->ident;
2119         if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2120                 (entry->tok == TOK_ENUM_CONST) ||
2121                 ((entry->tok >= TOK_FIRST_KEYWORD) && 
2122                         (entry->tok <= TOK_LAST_KEYWORD)))) {
2123                 tk->tok = entry->tok;
2124         }
2125 }
2126
2127 static void ident_to_macro(struct compile_state *state, struct token *tk)
2128 {
2129         struct hash_entry *entry;
2130         entry = tk->ident;
2131         if (entry && 
2132                 (entry->tok >= TOK_FIRST_MACRO) &&
2133                 (entry->tok <= TOK_LAST_MACRO)) {
2134                 tk->tok = entry->tok;
2135         }
2136 }
2137
2138 static void hash_keyword(
2139         struct compile_state *state, const char *keyword, int tok)
2140 {
2141         struct hash_entry *entry;
2142         entry = lookup(state, keyword, strlen(keyword));
2143         if (entry && entry->tok != TOK_UNKNOWN) {
2144                 die("keyword %s already hashed", keyword);
2145         }
2146         entry->tok  = tok;
2147 }
2148
2149 static void symbol(
2150         struct compile_state *state, struct hash_entry *ident,
2151         struct symbol **chain, struct triple *def, struct type *type)
2152 {
2153         struct symbol *sym;
2154         if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2155                 error(state, 0, "%s already defined", ident->name);
2156         }
2157         sym = xcmalloc(sizeof(*sym), "symbol");
2158         sym->ident = ident;
2159         sym->def   = def;
2160         sym->type  = type;
2161         sym->scope_depth = state->scope_depth;
2162         sym->next = *chain;
2163         *chain    = sym;
2164 }
2165
2166 static void label_symbol(struct compile_state *state, 
2167         struct hash_entry *ident, struct triple *label)
2168 {
2169         struct symbol *sym;
2170         if (ident->sym_label) {
2171                 error(state, 0, "label %s already defined", ident->name);
2172         }
2173         sym = xcmalloc(sizeof(*sym), "label");
2174         sym->ident = ident;
2175         sym->def   = label;
2176         sym->type  = &void_type;
2177         sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2178         sym->next  = 0;
2179         ident->sym_label = sym;
2180 }
2181
2182 static void start_scope(struct compile_state *state)
2183 {
2184         state->scope_depth++;
2185 }
2186
2187 static void end_scope_syms(struct symbol **chain, int depth)
2188 {
2189         struct symbol *sym, *next;
2190         sym = *chain;
2191         while(sym && (sym->scope_depth == depth)) {
2192                 next = sym->next;
2193                 xfree(sym);
2194                 sym = next;
2195         }
2196         *chain = sym;
2197 }
2198
2199 static void end_scope(struct compile_state *state)
2200 {
2201         int i;
2202         int depth;
2203         /* Walk through the hash table and remove all symbols
2204          * in the current scope. 
2205          */
2206         depth = state->scope_depth;
2207         for(i = 0; i < HASH_TABLE_SIZE; i++) {
2208                 struct hash_entry *entry;
2209                 entry = state->hash_table[i];
2210                 while(entry) {
2211                         end_scope_syms(&entry->sym_label,  depth);
2212                         end_scope_syms(&entry->sym_struct, depth);
2213                         end_scope_syms(&entry->sym_ident,  depth);
2214                         entry = entry->next;
2215                 }
2216         }
2217         state->scope_depth = depth - 1;
2218 }
2219
2220 static void register_keywords(struct compile_state *state)
2221 {
2222         hash_keyword(state, "auto",          TOK_AUTO);
2223         hash_keyword(state, "break",         TOK_BREAK);
2224         hash_keyword(state, "case",          TOK_CASE);
2225         hash_keyword(state, "char",          TOK_CHAR);
2226         hash_keyword(state, "const",         TOK_CONST);
2227         hash_keyword(state, "continue",      TOK_CONTINUE);
2228         hash_keyword(state, "default",       TOK_DEFAULT);
2229         hash_keyword(state, "do",            TOK_DO);
2230         hash_keyword(state, "double",        TOK_DOUBLE);
2231         hash_keyword(state, "else",          TOK_ELSE);
2232         hash_keyword(state, "enum",          TOK_ENUM);
2233         hash_keyword(state, "extern",        TOK_EXTERN);
2234         hash_keyword(state, "float",         TOK_FLOAT);
2235         hash_keyword(state, "for",           TOK_FOR);
2236         hash_keyword(state, "goto",          TOK_GOTO);
2237         hash_keyword(state, "if",            TOK_IF);
2238         hash_keyword(state, "inline",        TOK_INLINE);
2239         hash_keyword(state, "int",           TOK_INT);
2240         hash_keyword(state, "long",          TOK_LONG);
2241         hash_keyword(state, "register",      TOK_REGISTER);
2242         hash_keyword(state, "restrict",      TOK_RESTRICT);
2243         hash_keyword(state, "return",        TOK_RETURN);
2244         hash_keyword(state, "short",         TOK_SHORT);
2245         hash_keyword(state, "signed",        TOK_SIGNED);
2246         hash_keyword(state, "sizeof",        TOK_SIZEOF);
2247         hash_keyword(state, "static",        TOK_STATIC);
2248         hash_keyword(state, "struct",        TOK_STRUCT);
2249         hash_keyword(state, "switch",        TOK_SWITCH);
2250         hash_keyword(state, "typedef",       TOK_TYPEDEF);
2251         hash_keyword(state, "union",         TOK_UNION);
2252         hash_keyword(state, "unsigned",      TOK_UNSIGNED);
2253         hash_keyword(state, "void",          TOK_VOID);
2254         hash_keyword(state, "volatile",      TOK_VOLATILE);
2255         hash_keyword(state, "__volatile__",  TOK_VOLATILE);
2256         hash_keyword(state, "while",         TOK_WHILE);
2257         hash_keyword(state, "asm",           TOK_ASM);
2258         hash_keyword(state, "__asm__",       TOK_ASM);
2259         hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2260         hash_keyword(state, "__alignof__",   TOK_ALIGNOF);
2261 }
2262
2263 static void register_macro_keywords(struct compile_state *state)
2264 {
2265         hash_keyword(state, "define",        TOK_DEFINE);
2266         hash_keyword(state, "undef",         TOK_UNDEF);
2267         hash_keyword(state, "include",       TOK_INCLUDE);
2268         hash_keyword(state, "line",          TOK_LINE);
2269         hash_keyword(state, "error",         TOK_ERROR);
2270         hash_keyword(state, "warning",       TOK_WARNING);
2271         hash_keyword(state, "pragma",        TOK_PRAGMA);
2272         hash_keyword(state, "ifdef",         TOK_IFDEF);
2273         hash_keyword(state, "ifndef",        TOK_IFNDEF);
2274         hash_keyword(state, "elif",          TOK_ELIF);
2275         hash_keyword(state, "endif",         TOK_ENDIF);
2276 }
2277
2278 static int spacep(int c)
2279 {
2280         int ret = 0;
2281         switch(c) {
2282         case ' ':
2283         case '\t':
2284         case '\f':
2285         case '\v':
2286         case '\r':
2287         case '\n':
2288                 ret = 1;
2289                 break;
2290         }
2291         return ret;
2292 }
2293
2294 static int digitp(int c)
2295 {
2296         int ret = 0;
2297         switch(c) {
2298         case '0': case '1': case '2': case '3': case '4': 
2299         case '5': case '6': case '7': case '8': case '9':
2300                 ret = 1;
2301                 break;
2302         }
2303         return ret;
2304 }
2305 static int digval(int c)
2306 {
2307         int val = -1;
2308         if ((c >= '0') && (c <= '9')) {
2309                 val = c - '0';
2310         }
2311         return val;
2312 }
2313
2314 static int hexdigitp(int c)
2315 {
2316         int ret = 0;
2317         switch(c) {
2318         case '0': case '1': case '2': case '3': case '4': 
2319         case '5': case '6': case '7': case '8': case '9':
2320         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2321         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2322                 ret = 1;
2323                 break;
2324         }
2325         return ret;
2326 }
2327 static int hexdigval(int c) 
2328 {
2329         int val = -1;
2330         if ((c >= '0') && (c <= '9')) {
2331                 val = c - '0';
2332         }
2333         else if ((c >= 'A') && (c <= 'F')) {
2334                 val = 10 + (c - 'A');
2335         }
2336         else if ((c >= 'a') && (c <= 'f')) {
2337                 val = 10 + (c - 'a');
2338         }
2339         return val;
2340 }
2341
2342 static int octdigitp(int c)
2343 {
2344         int ret = 0;
2345         switch(c) {
2346         case '0': case '1': case '2': case '3': 
2347         case '4': case '5': case '6': case '7':
2348                 ret = 1;
2349                 break;
2350         }
2351         return ret;
2352 }
2353 static int octdigval(int c)
2354 {
2355         int val = -1;
2356         if ((c >= '0') && (c <= '7')) {
2357                 val = c - '0';
2358         }
2359         return val;
2360 }
2361
2362 static int letterp(int c)
2363 {
2364         int ret = 0;
2365         switch(c) {
2366         case 'a': case 'b': case 'c': case 'd': case 'e':
2367         case 'f': case 'g': case 'h': case 'i': case 'j':
2368         case 'k': case 'l': case 'm': case 'n': case 'o':
2369         case 'p': case 'q': case 'r': case 's': case 't':
2370         case 'u': case 'v': case 'w': case 'x': case 'y':
2371         case 'z':
2372         case 'A': case 'B': case 'C': case 'D': case 'E':
2373         case 'F': case 'G': case 'H': case 'I': case 'J':
2374         case 'K': case 'L': case 'M': case 'N': case 'O':
2375         case 'P': case 'Q': case 'R': case 'S': case 'T':
2376         case 'U': case 'V': case 'W': case 'X': case 'Y':
2377         case 'Z':
2378         case '_':
2379                 ret = 1;
2380                 break;
2381         }
2382         return ret;
2383 }
2384
2385 static int char_value(struct compile_state *state,
2386         const signed char **strp, const signed char *end)
2387 {
2388         const signed char *str;
2389         int c;
2390         str = *strp;
2391         c = *str++;
2392         if ((c == '\\') && (str < end)) {
2393                 switch(*str) {
2394                 case 'n':  c = '\n'; str++; break;
2395                 case 't':  c = '\t'; str++; break;
2396                 case 'v':  c = '\v'; str++; break;
2397                 case 'b':  c = '\b'; str++; break;
2398                 case 'r':  c = '\r'; str++; break;
2399                 case 'f':  c = '\f'; str++; break;
2400                 case 'a':  c = '\a'; str++; break;
2401                 case '\\': c = '\\'; str++; break;
2402                 case '?':  c = '?';  str++; break;
2403                 case '\'': c = '\''; str++; break;
2404                 case '"':  c = '"';  break;
2405                 case 'x': 
2406                         c = 0;
2407                         str++;
2408                         while((str < end) && hexdigitp(*str)) {
2409                                 c <<= 4;
2410                                 c += hexdigval(*str);
2411                                 str++;
2412                         }
2413                         break;
2414                 case '0': case '1': case '2': case '3': 
2415                 case '4': case '5': case '6': case '7':
2416                         c = 0;
2417                         while((str < end) && octdigitp(*str)) {
2418                                 c <<= 3;
2419                                 c += octdigval(*str);
2420                                 str++;
2421                         }
2422                         break;
2423                 default:
2424                         error(state, 0, "Invalid character constant");
2425                         break;
2426                 }
2427         }
2428         *strp = str;
2429         return c;
2430 }
2431
2432 static char *after_digits(char *ptr, char *end)
2433 {
2434         while((ptr < end) && digitp(*ptr)) {
2435                 ptr++;
2436         }
2437         return ptr;
2438 }
2439
2440 static char *after_octdigits(char *ptr, char *end)
2441 {
2442         while((ptr < end) && octdigitp(*ptr)) {
2443                 ptr++;
2444         }
2445         return ptr;
2446 }
2447
2448 static char *after_hexdigits(char *ptr, char *end)
2449 {
2450         while((ptr < end) && hexdigitp(*ptr)) {
2451                 ptr++;
2452         }
2453         return ptr;
2454 }
2455
2456 static void save_string(struct compile_state *state, 
2457         struct token *tk, char *start, char *end, const char *id)
2458 {
2459         char *str;
2460         int str_len;
2461         /* Create a private copy of the string */
2462         str_len = end - start + 1;
2463         str = xmalloc(str_len + 1, id);
2464         memcpy(str, start, str_len);
2465         str[str_len] = '\0';
2466
2467         /* Store the copy in the token */
2468         tk->val.str = str;
2469         tk->str_len = str_len;
2470 }
2471 static void next_token(struct compile_state *state, int index)
2472 {
2473         struct file_state *file;
2474         struct token *tk;
2475         char *token;
2476         int c, c1, c2, c3;
2477         char *tokp, *end;
2478         int tok;
2479 next_token:
2480         file = state->file;
2481         tk = &state->token[index];
2482         tk->str_len = 0;
2483         tk->ident = 0;
2484         token = tokp = file->pos;
2485         end = file->buf + file->size;
2486         tok = TOK_UNKNOWN;
2487         c = -1;
2488         if (tokp < end) {
2489                 c = *tokp;
2490         }
2491         c1 = -1;
2492         if ((tokp + 1) < end) {
2493                 c1 = tokp[1];
2494         }
2495         c2 = -1;
2496         if ((tokp + 2) < end) {
2497                 c2 = tokp[2];
2498         }
2499         c3 = -1;
2500         if ((tokp + 3) < end) {
2501                 c3 = tokp[3];
2502         }
2503         if (tokp >= end) {
2504                 tok = TOK_EOF;
2505                 tokp = end;
2506         }
2507         /* Whitespace */
2508         else if (spacep(c)) {
2509                 tok = TOK_SPACE;
2510                 while ((tokp < end) && spacep(c)) {
2511                         if (c == '\n') {
2512                                 file->line++;
2513                                 file->report_line++;
2514                                 file->line_start = tokp + 1;
2515                         }
2516                         c = *(++tokp);
2517                 }
2518                 if (!spacep(c)) {
2519                         tokp--;
2520                 }
2521         }
2522         /* EOL Comments */
2523         else if ((c == '/') && (c1 == '/')) {
2524                 tok = TOK_SPACE;
2525                 for(tokp += 2; tokp < end; tokp++) {
2526                         c = *tokp;
2527                         if (c == '\n') {
2528                                 file->line++;
2529                                 file->report_line++;
2530                                 file->line_start = tokp +1;
2531                                 break;
2532                         }
2533                 }
2534         }
2535         /* Comments */
2536         else if ((c == '/') && (c1 == '*')) {
2537                 int line;
2538                 char *line_start;
2539                 line = file->line;
2540                 line_start = file->line_start;
2541                 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2542                         c = *tokp;
2543                         if (c == '\n') {
2544                                 line++;
2545                                 line_start = tokp +1;
2546                         }
2547                         else if ((c == '*') && (tokp[1] == '/')) {
2548                                 tok = TOK_SPACE;
2549                                 tokp += 1;
2550                                 break;
2551                         }
2552                 }
2553                 if (tok == TOK_UNKNOWN) {
2554                         error(state, 0, "unterminated comment");
2555                 }
2556                 file->report_line += line - file->line;
2557                 file->line = line;
2558                 file->line_start = line_start;
2559         }
2560         /* string constants */
2561         else if ((c == '"') ||
2562                 ((c == 'L') && (c1 == '"'))) {
2563                 int line;
2564                 char *line_start;
2565                 int wchar;
2566                 line = file->line;
2567                 line_start = file->line_start;
2568                 wchar = 0;
2569                 if (c == 'L') {
2570                         wchar = 1;
2571                         tokp++;
2572                 }
2573                 for(tokp += 1; tokp < end; tokp++) {
2574                         c = *tokp;
2575                         if (c == '\n') {
2576                                 line++;
2577                                 line_start = tokp + 1;
2578                         }
2579                         else if ((c == '\\') && (tokp +1 < end)) {
2580                                 tokp++;
2581                         }
2582                         else if (c == '"') {
2583                                 tok = TOK_LIT_STRING;
2584                                 break;
2585                         }
2586                 }
2587                 if (tok == TOK_UNKNOWN) {
2588                         error(state, 0, "unterminated string constant");
2589                 }
2590                 if (line != file->line) {
2591                         warning(state, 0, "multiline string constant");
2592                 }
2593                 file->report_line += line - file->line;
2594                 file->line = line;
2595                 file->line_start = line_start;
2596
2597                 /* Save the string value */
2598                 save_string(state, tk, token, tokp, "literal string");
2599         }
2600         /* character constants */
2601         else if ((c == '\'') ||
2602                 ((c == 'L') && (c1 == '\''))) {
2603                 int line;
2604                 char *line_start;
2605                 int wchar;
2606                 line = file->line;
2607                 line_start = file->line_start;
2608                 wchar = 0;
2609                 if (c == 'L') {
2610                         wchar = 1;
2611                         tokp++;
2612                 }
2613                 for(tokp += 1; tokp < end; tokp++) {
2614                         c = *tokp;
2615                         if (c == '\n') {
2616                                 line++;
2617                                 line_start = tokp + 1;
2618                         }
2619                         else if ((c == '\\') && (tokp +1 < end)) {
2620                                 tokp++;
2621                         }
2622                         else if (c == '\'') {
2623                                 tok = TOK_LIT_CHAR;
2624                                 break;
2625                         }
2626                 }
2627                 if (tok == TOK_UNKNOWN) {
2628                         error(state, 0, "unterminated character constant");
2629                 }
2630                 if (line != file->line) {
2631                         warning(state, 0, "multiline character constant");
2632                 }
2633                 file->report_line += line - file->line;
2634                 file->line = line;
2635                 file->line_start = line_start;
2636
2637                 /* Save the character value */
2638                 save_string(state, tk, token, tokp, "literal character");
2639         }
2640         /* integer and floating constants 
2641          * Integer Constants
2642          * {digits}
2643          * 0[Xx]{hexdigits}
2644          * 0{octdigit}+
2645          * 
2646          * Floating constants
2647          * {digits}.{digits}[Ee][+-]?{digits}
2648          * {digits}.{digits}
2649          * {digits}[Ee][+-]?{digits}
2650          * .{digits}[Ee][+-]?{digits}
2651          * .{digits}
2652          */
2653         
2654         else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2655                 char *next, *new;
2656                 int is_float;
2657                 is_float = 0;
2658                 if (c != '.') {
2659                         next = after_digits(tokp, end);
2660                 }
2661                 else {
2662                         next = tokp;
2663                 }
2664                 if (next[0] == '.') {
2665                         new = after_digits(next, end);
2666                         is_float = (new != next);
2667                         next = new;
2668                 }
2669                 if ((next[0] == 'e') || (next[0] == 'E')) {
2670                         if (((next + 1) < end) && 
2671                                 ((next[1] == '+') || (next[1] == '-'))) {
2672                                 next++;
2673                         }
2674                         new = after_digits(next, end);
2675                         is_float = (new != next);
2676                         next = new;
2677                 }
2678                 if (is_float) {
2679                         tok = TOK_LIT_FLOAT;
2680                         if ((next < end) && (
2681                                 (next[0] == 'f') ||
2682                                 (next[0] == 'F') ||
2683                                 (next[0] == 'l') ||
2684                                 (next[0] == 'L'))
2685                                 ) {
2686                                 next++;
2687                         }
2688                 }
2689                 if (!is_float && digitp(c)) {
2690                         tok = TOK_LIT_INT;
2691                         if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2692                                 next = after_hexdigits(tokp + 2, end);
2693                         }
2694                         else if (c == '0') {
2695                                 next = after_octdigits(tokp, end);
2696                         }
2697                         else {
2698                                 next = after_digits(tokp, end);
2699                         }
2700                         /* crazy integer suffixes */
2701                         if ((next < end) && 
2702                                 ((next[0] == 'u') || (next[0] == 'U'))) { 
2703                                 next++;
2704                                 if ((next < end) &&
2705                                         ((next[0] == 'l') || (next[0] == 'L'))) {
2706                                         next++;
2707                                 }
2708                         }
2709                         else if ((next < end) &&
2710                                 ((next[0] == 'l') || (next[0] == 'L'))) {
2711                                 next++;
2712                                 if ((next < end) && 
2713                                         ((next[0] == 'u') || (next[0] == 'U'))) { 
2714                                         next++;
2715                                 }
2716                         }
2717                 }
2718                 tokp = next - 1;
2719
2720                 /* Save the integer/floating point value */
2721                 save_string(state, tk, token, tokp, "literal number");
2722         }
2723         /* identifiers */
2724         else if (letterp(c)) {
2725                 tok = TOK_IDENT;
2726                 for(tokp += 1; tokp < end; tokp++) {
2727                         c = *tokp;
2728                         if (!letterp(c) && !digitp(c)) {
2729                                 break;
2730                         }
2731                 }
2732                 tokp -= 1;
2733                 tk->ident = lookup(state, token, tokp +1 - token);
2734         }
2735         /* C99 alternate macro characters */
2736         else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { 
2737                 tokp += 3; 
2738                 tok = TOK_CONCATENATE; 
2739         }
2740         else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2741         else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2742         else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2743         else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2744         else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2745         else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2746         else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2747         else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2748         else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2749         else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2750         else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2751         else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2752         else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2753         else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2754         else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2755         else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2756         else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2757         else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2758         else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2759         else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2760         else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2761         else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2762         else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2763         else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2764         else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2765         else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2766         else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2767         else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2768         else if (c == ';') { tok = TOK_SEMI; }
2769         else if (c == '{') { tok = TOK_LBRACE; }
2770         else if (c == '}') { tok = TOK_RBRACE; }
2771         else if (c == ',') { tok = TOK_COMMA; }
2772         else if (c == '=') { tok = TOK_EQ; }
2773         else if (c == ':') { tok = TOK_COLON; }
2774         else if (c == '[') { tok = TOK_LBRACKET; }
2775         else if (c == ']') { tok = TOK_RBRACKET; }
2776         else if (c == '(') { tok = TOK_LPAREN; }
2777         else if (c == ')') { tok = TOK_RPAREN; }
2778         else if (c == '*') { tok = TOK_STAR; }
2779         else if (c == '>') { tok = TOK_MORE; }
2780         else if (c == '<') { tok = TOK_LESS; }
2781         else if (c == '?') { tok = TOK_QUEST; }
2782         else if (c == '|') { tok = TOK_OR; }
2783         else if (c == '&') { tok = TOK_AND; }
2784         else if (c == '^') { tok = TOK_XOR; }
2785         else if (c == '+') { tok = TOK_PLUS; }
2786         else if (c == '-') { tok = TOK_MINUS; }
2787         else if (c == '/') { tok = TOK_DIV; }
2788         else if (c == '%') { tok = TOK_MOD; }
2789         else if (c == '!') { tok = TOK_BANG; }
2790         else if (c == '.') { tok = TOK_DOT; }
2791         else if (c == '~') { tok = TOK_TILDE; }
2792         else if (c == '#') { tok = TOK_MACRO; }
2793         if (tok == TOK_MACRO) {
2794                 /* Only match preprocessor directives at the start of a line */
2795                 char *ptr;
2796                 for(ptr = file->line_start; spacep(*ptr); ptr++)
2797                         ;
2798                 if (ptr != tokp) {
2799                         tok = TOK_UNKNOWN;
2800                 }
2801         }
2802         if (tok == TOK_UNKNOWN) {
2803                 error(state, 0, "unknown token");
2804         }
2805
2806         file->pos = tokp + 1;
2807         tk->tok = tok;
2808         if (tok == TOK_IDENT) {
2809                 ident_to_keyword(state, tk);
2810         }
2811         /* Don't return space tokens. */
2812         if (tok == TOK_SPACE) {
2813                 goto next_token;
2814         }
2815 }
2816
2817 static void compile_macro(struct compile_state *state, struct token *tk)
2818 {
2819         struct file_state *file;
2820         struct hash_entry *ident;
2821         ident = tk->ident;
2822         file = xmalloc(sizeof(*file), "file_state");
2823         file->basename = xstrdup(tk->ident->name);
2824         file->dirname = xstrdup("");
2825         file->size = ident->sym_define->buf_len;
2826         file->buf = xmalloc(file->size +2,  file->basename);
2827         memcpy(file->buf, ident->sym_define->buf, file->size);
2828         file->buf[file->size] = '\n';
2829         file->buf[file->size + 1] = '\0';
2830         file->pos = file->buf;
2831         file->line_start = file->pos;
2832         file->line = 1;
2833         file->report_line = 1;
2834         file->report_name = file->basename;
2835         file->report_dir  = file->dirname;
2836         file->prev = state->file;
2837         state->file = file;
2838 }
2839
2840
2841 static int mpeek(struct compile_state *state, int index)
2842 {
2843         struct token *tk;
2844         int rescan;
2845         tk = &state->token[index + 1];
2846         if (tk->tok == -1) {
2847                 next_token(state, index + 1);
2848         }
2849         do {
2850                 rescan = 0;
2851                 if ((tk->tok == TOK_EOF) && 
2852                         (state->file != state->macro_file) &&
2853                         (state->file->prev)) {
2854                         struct file_state *file = state->file;
2855                         state->file = file->prev;
2856                         /* file->basename is used keep it */
2857                         if (file->report_dir != file->dirname) {
2858                                 xfree(file->report_dir);
2859                         }
2860                         xfree(file->dirname);
2861                         xfree(file->buf);
2862                         xfree(file);
2863                         next_token(state, index + 1);
2864                         rescan = 1;
2865                 }
2866                 else if (tk->ident && tk->ident->sym_define) {
2867                         compile_macro(state, tk);
2868                         next_token(state, index + 1);
2869                         rescan = 1;
2870                 }
2871         } while(rescan);
2872         /* Don't show the token on the next line */
2873         if (state->macro_line < state->macro_file->line) {
2874                 return TOK_EOF;
2875         }
2876         return state->token[index +1].tok;
2877 }
2878
2879 static void meat(struct compile_state *state, int index, int tok)
2880 {
2881         int next_tok;
2882         int i;
2883         next_tok = mpeek(state, index);
2884         if (next_tok != tok) {
2885                 const char *name1, *name2;
2886                 name1 = tokens[next_tok];
2887                 name2 = "";
2888                 if (next_tok == TOK_IDENT) {
2889                         name2 = state->token[index + 1].ident->name;
2890                 }
2891                 error(state, 0, "found %s %s expected %s", 
2892                         name1, name2, tokens[tok]);
2893         }
2894         /* Free the old token value */
2895         if (state->token[index].str_len) {
2896                 memset((void *)(state->token[index].val.str), -1, 
2897                         state->token[index].str_len);
2898                 xfree(state->token[index].val.str);
2899         }
2900         for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2901                 state->token[i] = state->token[i + 1];
2902         }
2903         memset(&state->token[i], 0, sizeof(state->token[i]));
2904         state->token[i].tok = -1;
2905 }
2906
2907 static long_t mcexpr(struct compile_state *state, int index);
2908
2909 static long_t mprimary_expr(struct compile_state *state, int index)
2910 {
2911         long_t val;
2912         int tok;
2913         tok = mpeek(state, index);
2914         while(state->token[index + 1].ident && 
2915                 state->token[index + 1].ident->sym_define) {
2916                 meat(state, index, tok);
2917                 compile_macro(state, &state->token[index]);
2918                 tok = mpeek(state, index);
2919         }
2920         switch(tok) {
2921         case TOK_LPAREN:
2922                 meat(state, index, TOK_LPAREN);
2923                 val = mcexpr(state, index);
2924                 meat(state, index, TOK_RPAREN);
2925                 break;
2926         case TOK_LIT_INT:
2927         {
2928                 char *end;
2929                 meat(state, index, TOK_LIT_INT);
2930                 errno = 0;
2931                 val = strtol(state->token[index].val.str, &end, 0);
2932                 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2933                         (errno == ERANGE)) {
2934                         error(state, 0, "Integer constant to large");
2935                 }
2936                 break;
2937         }
2938         default:
2939                 meat(state, index, TOK_LIT_INT);
2940                 val = 0;
2941         }
2942         return val;
2943 }
2944 static long_t munary_expr(struct compile_state *state, int index)
2945 {
2946         long_t val;
2947         switch(mpeek(state, index)) {
2948         case TOK_PLUS:
2949                 meat(state, index, TOK_PLUS);
2950                 val = munary_expr(state, index);
2951                 val = + val;
2952                 break;
2953         case TOK_MINUS:
2954                 meat(state, index, TOK_MINUS);
2955                 val = munary_expr(state, index);
2956                 val = - val;
2957                 break;
2958         case TOK_TILDE:
2959                 meat(state, index, TOK_BANG);
2960                 val = munary_expr(state, index);
2961                 val = ~ val;
2962                 break;
2963         case TOK_BANG:
2964                 meat(state, index, TOK_BANG);
2965                 val = munary_expr(state, index);
2966                 val = ! val;
2967                 break;
2968         default:
2969                 val = mprimary_expr(state, index);
2970                 break;
2971         }
2972         return val;
2973         
2974 }
2975 static long_t mmul_expr(struct compile_state *state, int index)
2976 {
2977         long_t val;
2978         int done;
2979         val = munary_expr(state, index);
2980         do {
2981                 long_t right;
2982                 done = 0;
2983                 switch(mpeek(state, index)) {
2984                 case TOK_STAR:
2985                         meat(state, index, TOK_STAR);
2986                         right = munary_expr(state, index);
2987                         val = val * right;
2988                         break;
2989                 case TOK_DIV:
2990                         meat(state, index, TOK_DIV);
2991                         right = munary_expr(state, index);
2992                         val = val / right;
2993                         break;
2994                 case TOK_MOD:
2995                         meat(state, index, TOK_MOD);
2996                         right = munary_expr(state, index);
2997                         val = val % right;
2998                         break;
2999                 default:
3000                         done = 1;
3001                         break;
3002                 }
3003         } while(!done);
3004
3005         return val;
3006 }
3007
3008 static long_t madd_expr(struct compile_state *state, int index)
3009 {
3010         long_t val;
3011         int done;
3012         val = mmul_expr(state, index);
3013         do {
3014                 long_t right;
3015                 done = 0;
3016                 switch(mpeek(state, index)) {
3017                 case TOK_PLUS:
3018                         meat(state, index, TOK_PLUS);
3019                         right = mmul_expr(state, index);
3020                         val = val + right;
3021                         break;
3022                 case TOK_MINUS:
3023                         meat(state, index, TOK_MINUS);
3024                         right = mmul_expr(state, index);
3025                         val = val - right;
3026                         break;
3027                 default:
3028                         done = 1;
3029                         break;
3030                 }
3031         } while(!done);
3032
3033         return val;
3034 }
3035
3036 static long_t mshift_expr(struct compile_state *state, int index)
3037 {
3038         long_t val;
3039         int done;
3040         val = madd_expr(state, index);
3041         do {
3042                 long_t right;
3043                 done = 0;
3044                 switch(mpeek(state, index)) {
3045                 case TOK_SL:
3046                         meat(state, index, TOK_SL);
3047                         right = madd_expr(state, index);
3048                         val = val << right;
3049                         break;
3050                 case TOK_SR:
3051                         meat(state, index, TOK_SR);
3052                         right = madd_expr(state, index);
3053                         val = val >> right;
3054                         break;
3055                 default:
3056                         done = 1;
3057                         break;
3058                 }
3059         } while(!done);
3060
3061         return val;
3062 }
3063
3064 static long_t mrel_expr(struct compile_state *state, int index)
3065 {
3066         long_t val;
3067         int done;
3068         val = mshift_expr(state, index);
3069         do {
3070                 long_t right;
3071                 done = 0;
3072                 switch(mpeek(state, index)) {
3073                 case TOK_LESS:
3074                         meat(state, index, TOK_LESS);
3075                         right = mshift_expr(state, index);
3076                         val = val < right;
3077                         break;
3078                 case TOK_MORE:
3079                         meat(state, index, TOK_MORE);
3080                         right = mshift_expr(state, index);
3081                         val = val > right;
3082                         break;
3083                 case TOK_LESSEQ:
3084                         meat(state, index, TOK_LESSEQ);
3085                         right = mshift_expr(state, index);
3086                         val = val <= right;
3087                         break;
3088                 case TOK_MOREEQ:
3089                         meat(state, index, TOK_MOREEQ);
3090                         right = mshift_expr(state, index);
3091                         val = val >= right;
3092                         break;
3093                 default:
3094                         done = 1;
3095                         break;
3096                 }
3097         } while(!done);
3098         return val;
3099 }
3100
3101 static long_t meq_expr(struct compile_state *state, int index)
3102 {
3103         long_t val;
3104         int done;
3105         val = mrel_expr(state, index);
3106         do {
3107                 long_t right;
3108                 done = 0;
3109                 switch(mpeek(state, index)) {
3110                 case TOK_EQEQ:
3111                         meat(state, index, TOK_EQEQ);
3112                         right = mrel_expr(state, index);
3113                         val = val == right;
3114                         break;
3115                 case TOK_NOTEQ:
3116                         meat(state, index, TOK_NOTEQ);
3117                         right = mrel_expr(state, index);
3118                         val = val != right;
3119                         break;
3120                 default:
3121                         done = 1;
3122                         break;
3123                 }
3124         } while(!done);
3125         return val;
3126 }
3127
3128 static long_t mand_expr(struct compile_state *state, int index)
3129 {
3130         long_t val;
3131         val = meq_expr(state, index);
3132         if (mpeek(state, index) == TOK_AND) {
3133                 long_t right;
3134                 meat(state, index, TOK_AND);
3135                 right = meq_expr(state, index);
3136                 val = val & right;
3137         }
3138         return val;
3139 }
3140
3141 static long_t mxor_expr(struct compile_state *state, int index)
3142 {
3143         long_t val;
3144         val = mand_expr(state, index);
3145         if (mpeek(state, index) == TOK_XOR) {
3146                 long_t right;
3147                 meat(state, index, TOK_XOR);
3148                 right = mand_expr(state, index);
3149                 val = val ^ right;
3150         }
3151         return val;
3152 }
3153
3154 static long_t mor_expr(struct compile_state *state, int index)
3155 {
3156         long_t val;
3157         val = mxor_expr(state, index);
3158         if (mpeek(state, index) == TOK_OR) {
3159                 long_t right;
3160                 meat(state, index, TOK_OR);
3161                 right = mxor_expr(state, index);
3162                 val = val | right;
3163         }
3164         return val;
3165 }
3166
3167 static long_t mland_expr(struct compile_state *state, int index)
3168 {
3169         long_t val;
3170         val = mor_expr(state, index);
3171         if (mpeek(state, index) == TOK_LOGAND) {
3172                 long_t right;
3173                 meat(state, index, TOK_LOGAND);
3174                 right = mor_expr(state, index);
3175                 val = val && right;
3176         }
3177         return val;
3178 }
3179 static long_t mlor_expr(struct compile_state *state, int index)
3180 {
3181         long_t val;
3182         val = mland_expr(state, index);
3183         if (mpeek(state, index) == TOK_LOGOR) {
3184                 long_t right;
3185                 meat(state, index, TOK_LOGOR);
3186                 right = mland_expr(state, index);
3187                 val = val || right;
3188         }
3189         return val;
3190 }
3191
3192 static long_t mcexpr(struct compile_state *state, int index)
3193 {
3194         return mlor_expr(state, index);
3195 }
3196 static void preprocess(struct compile_state *state, int index)
3197 {
3198         /* Doing much more with the preprocessor would require
3199          * a parser and a major restructuring.
3200          * Postpone that for later.
3201          */
3202         struct file_state *file;
3203         struct token *tk;
3204         int line;
3205         int tok;
3206         
3207         file = state->file;
3208         tk = &state->token[index];
3209         state->macro_line = line = file->line;
3210         state->macro_file = file;
3211
3212         next_token(state, index);
3213         ident_to_macro(state, tk);
3214         if (tk->tok == TOK_IDENT) {
3215                 error(state, 0, "undefined preprocessing directive `%s'",
3216                         tk->ident->name);
3217         }
3218         switch(tk->tok) {
3219         case TOK_LIT_INT:
3220         {
3221                 int override_line;
3222                 override_line = strtoul(tk->val.str, 0, 10);
3223                 next_token(state, index);
3224                 /* I have a cpp line marker parse it */
3225                 if (tk->tok == TOK_LIT_STRING) {
3226                         const char *token, *base;
3227                         char *name, *dir;
3228                         int name_len, dir_len;
3229                         name = xmalloc(tk->str_len, "report_name");
3230                         token = tk->val.str + 1;
3231                         base = strrchr(token, '/');
3232                         name_len = tk->str_len -2;
3233                         if (base != 0) {
3234                                 dir_len = base - token;
3235                                 base++;
3236                                 name_len -= base - token;
3237                         } else {
3238                                 dir_len = 0;
3239                                 base = token;
3240                         }
3241                         memcpy(name, base, name_len);
3242                         name[name_len] = '\0';
3243                         dir = xmalloc(dir_len + 1, "report_dir");
3244                         memcpy(dir, token, dir_len);
3245                         dir[dir_len] = '\0';
3246                         file->report_line = override_line - 1;
3247                         file->report_name = name;
3248                         file->report_dir = dir;
3249                 }
3250         }
3251                 break;
3252         case TOK_LINE:
3253                 meat(state, index, TOK_LINE);
3254                 meat(state, index, TOK_LIT_INT);
3255                 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3256                 if (mpeek(state, index) == TOK_LIT_STRING) {
3257                         const char *token, *base;
3258                         char *name, *dir;
3259                         int name_len, dir_len;
3260                         meat(state, index, TOK_LIT_STRING);
3261                         name = xmalloc(tk->str_len, "report_name");
3262                         token = tk->val.str + 1;
3263                         name_len = tk->str_len - 2;
3264                         if (base != 0) {
3265                                 dir_len = base - token;
3266                                 base++;
3267                                 name_len -= base - token;
3268                         } else {
3269                                 dir_len = 0;
3270                                 base = token;
3271                         }
3272                         memcpy(name, base, name_len);
3273                         name[name_len] = '\0';
3274                         dir = xmalloc(dir_len + 1, "report_dir");
3275                         memcpy(dir, token, dir_len);
3276                         dir[dir_len] = '\0';
3277                         file->report_name = name;
3278                         file->report_dir = dir;
3279                 }
3280                 break;
3281         case TOK_UNDEF:
3282         case TOK_PRAGMA:
3283                 if (state->if_value < 0) {
3284                         break;
3285                 }
3286                 warning(state, 0, "Ignoring preprocessor directive: %s", 
3287                         tk->ident->name);
3288                 break;
3289         case TOK_ELIF:
3290                 error(state, 0, "#elif not supported");
3291 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3292                 if (state->if_depth == 0) {
3293                         error(state, 0, "#elif without #if");
3294                 }
3295                 /* If the #if was taken the #elif just disables the following code */
3296                 if (state->if_value >= 0) {
3297                         state->if_value = - state->if_value;
3298                 }
3299                 /* If the previous #if was not taken see if the #elif enables the 
3300                  * trailing code.
3301                  */
3302                 else if ((state->if_value < 0) && 
3303                         (state->if_depth == - state->if_value))
3304                 {
3305                         if (mcexpr(state, index) != 0) {
3306                                 state->if_value = state->if_depth;
3307                         }
3308                         else {
3309                                 state->if_value = - state->if_depth;
3310                         }
3311                 }
3312                 break;
3313         case TOK_IF:
3314                 state->if_depth++;
3315                 if (state->if_value < 0) {
3316                         break;
3317                 }
3318                 if (mcexpr(state, index) != 0) {
3319                         state->if_value = state->if_depth;
3320                 }
3321                 else {
3322                         state->if_value = - state->if_depth;
3323                 }
3324                 break;
3325         case TOK_IFNDEF:
3326                 state->if_depth++;
3327                 if (state->if_value < 0) {
3328                         break;
3329                 }
3330                 next_token(state, index);
3331                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3332                         error(state, 0, "Invalid macro name");
3333                 }
3334                 if (tk->ident->sym_define == 0) {
3335                         state->if_value = state->if_depth;
3336                 } 
3337                 else {
3338                         state->if_value = - state->if_depth;
3339                 }
3340                 break;
3341         case TOK_IFDEF:
3342                 state->if_depth++;
3343                 if (state->if_value < 0) {
3344                         break;
3345                 }
3346                 next_token(state, index);
3347                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3348                         error(state, 0, "Invalid macro name");
3349                 }
3350                 if (tk->ident->sym_define != 0) {
3351                         state->if_value = state->if_depth;
3352                 }
3353                 else {
3354                         state->if_value = - state->if_depth;
3355                 }
3356                 break;
3357         case TOK_ELSE:
3358                 if (state->if_depth == 0) {
3359                         error(state, 0, "#else without #if");
3360                 }
3361                 if ((state->if_value >= 0) ||
3362                         ((state->if_value < 0) && 
3363                                 (state->if_depth == -state->if_value)))
3364                 {
3365                         state->if_value = - state->if_value;
3366                 }
3367                 break;
3368         case TOK_ENDIF:
3369                 if (state->if_depth == 0) {
3370                         error(state, 0, "#endif without #if");
3371                 }
3372                 if ((state->if_value >= 0) ||
3373                         ((state->if_value < 0) &&
3374                                 (state->if_depth == -state->if_value))) 
3375                 {
3376                         state->if_value = state->if_depth - 1;
3377                 }
3378                 state->if_depth--;
3379                 break;
3380         case TOK_DEFINE:
3381         {
3382                 struct hash_entry *ident;
3383                 struct macro *macro;
3384                 char *ptr;
3385                 
3386                 if (state->if_value < 0) /* quit early when #if'd out */
3387                         break;
3388
3389                 meat(state, index, TOK_IDENT);
3390                 ident = tk->ident;
3391                 
3392
3393                 if (*file->pos == '(') {
3394 #warning "FIXME macros with arguments not supported"
3395                         error(state, 0, "Macros with arguments not supported");
3396                 }
3397
3398                 /* Find the end of the line to get an estimate of
3399                  * the macro's length.
3400                  */
3401                 for(ptr = file->pos; *ptr != '\n'; ptr++)  
3402                         ;
3403
3404                 if (ident->sym_define != 0) {
3405                         error(state, 0, "macro %s already defined\n", ident->name);
3406                 }
3407                 macro = xmalloc(sizeof(*macro), "macro");
3408                 macro->ident = ident;
3409                 macro->buf_len = ptr - file->pos +1;
3410                 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3411
3412                 memcpy(macro->buf, file->pos, macro->buf_len);
3413                 macro->buf[macro->buf_len] = '\n';
3414                 macro->buf[macro->buf_len +1] = '\0';
3415
3416                 ident->sym_define = macro;
3417                 break;
3418         }
3419         case TOK_ERROR:
3420         {
3421                 char *end;
3422                 int len;
3423                 /* Find the end of the line */
3424                 for(end = file->pos; *end != '\n'; end++)
3425                         ;
3426                 len = (end - file->pos);
3427                 if (state->if_value >= 0) {
3428                         error(state, 0, "%*.*s", len, len, file->pos);
3429                 }
3430                 file->pos = end;
3431                 break;
3432         }
3433         case TOK_WARNING:
3434         {
3435                 char *end;
3436                 int len;
3437                 /* Find the end of the line */
3438                 for(end = file->pos; *end != '\n'; end++)
3439                         ;
3440                 len = (end - file->pos);
3441                 if (state->if_value >= 0) {
3442                         warning(state, 0, "%*.*s", len, len, file->pos);
3443                 }
3444                 file->pos = end;
3445                 break;
3446         }
3447         case TOK_INCLUDE:
3448         {
3449                 char *name;
3450                 char *ptr;
3451                 int local;
3452                 local = 0;
3453                 name = 0;
3454                 next_token(state, index);
3455                 if (tk->tok == TOK_LIT_STRING) {
3456                         const char *token;
3457                         int name_len;
3458                         name = xmalloc(tk->str_len, "include");
3459                         token = tk->val.str +1;
3460                         name_len = tk->str_len -2;
3461                         if (*token == '"') {
3462                                 token++;
3463                                 name_len--;
3464                         }
3465                         memcpy(name, token, name_len);
3466                         name[name_len] = '\0';
3467                         local = 1;
3468                 }
3469                 else if (tk->tok == TOK_LESS) {
3470                         char *start, *end;
3471                         start = file->pos;
3472                         for(end = start; *end != '\n'; end++) {
3473                                 if (*end == '>') {
3474                                         break;
3475                                 }
3476                         }
3477                         if (*end == '\n') {
3478                                 error(state, 0, "Unterminated included directive");
3479                         }
3480                         name = xmalloc(end - start + 1, "include");
3481                         memcpy(name, start, end - start);
3482                         name[end - start] = '\0';
3483                         file->pos = end +1;
3484                         local = 0;
3485                 }
3486                 else {
3487                         error(state, 0, "Invalid include directive");
3488                 }
3489                 /* Error if there are any characters after the include */
3490                 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3491                         switch(*ptr) {
3492                         case ' ':
3493                         case '\t':
3494                         case '\v':
3495                                 break;
3496                         default:
3497                                 error(state, 0, "garbage after include directive");
3498                         }
3499                 }
3500                 if (state->if_value >= 0) {
3501                         compile_file(state, name, local);
3502                 }
3503                 xfree(name);
3504                 next_token(state, index);
3505                 return;
3506         }
3507         default:
3508                 /* Ignore # without a following ident */
3509                 if (tk->tok == TOK_IDENT) {
3510                         error(state, 0, "Invalid preprocessor directive: %s", 
3511                                 tk->ident->name);
3512                 }
3513                 break;
3514         }
3515         /* Consume the rest of the macro line */
3516         do {
3517                 tok = mpeek(state, index);
3518                 meat(state, index, tok);
3519         } while(tok != TOK_EOF);
3520         return;
3521 }
3522
3523 static void token(struct compile_state *state, int index)
3524 {
3525         struct file_state *file;
3526         struct token *tk;
3527         int rescan;
3528
3529         tk = &state->token[index];
3530         next_token(state, index);
3531         do {
3532                 rescan = 0;
3533                 file = state->file;
3534                 if (tk->tok == TOK_EOF && file->prev) {
3535                         state->file = file->prev;
3536                         /* file->basename is used keep it */
3537                         xfree(file->dirname);
3538                         xfree(file->buf);
3539                         xfree(file);
3540                         next_token(state, index);
3541                         rescan = 1;
3542                 }
3543                 else if (tk->tok == TOK_MACRO) {
3544                         preprocess(state, index);
3545                         rescan = 1;
3546                 }
3547                 else if (tk->ident && tk->ident->sym_define) {
3548                         compile_macro(state, tk);
3549                         next_token(state, index);
3550                         rescan = 1;
3551                 }
3552                 else if (state->if_value < 0) {
3553                         next_token(state, index);
3554                         rescan = 1;
3555                 }
3556         } while(rescan);
3557 }
3558
3559 static int peek(struct compile_state *state)
3560 {
3561         if (state->token[1].tok == -1) {
3562                 token(state, 1);
3563         }
3564         return state->token[1].tok;
3565 }
3566
3567 static int peek2(struct compile_state *state)
3568 {
3569         if (state->token[1].tok == -1) {
3570                 token(state, 1);
3571         }
3572         if (state->token[2].tok == -1) {
3573                 token(state, 2);
3574         }
3575         return state->token[2].tok;
3576 }
3577
3578 static void eat(struct compile_state *state, int tok)
3579 {
3580         int next_tok;
3581         int i;
3582         next_tok = peek(state);
3583         if (next_tok != tok) {
3584                 const char *name1, *name2;
3585                 name1 = tokens[next_tok];
3586                 name2 = "";
3587                 if (next_tok == TOK_IDENT) {
3588                         name2 = state->token[1].ident->name;
3589                 }
3590                 error(state, 0, "\tfound %s %s expected %s",
3591                         name1, name2 ,tokens[tok]);
3592         }
3593         /* Free the old token value */
3594         if (state->token[0].str_len) {
3595                 xfree((void *)(state->token[0].val.str));
3596         }
3597         for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3598                 state->token[i] = state->token[i + 1];
3599         }
3600         memset(&state->token[i], 0, sizeof(state->token[i]));
3601         state->token[i].tok = -1;
3602 }
3603
3604 #warning "FIXME do not hardcode the include paths"
3605 static char *include_paths[] = {
3606         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3607         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3608         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3609         0
3610 };
3611
3612 static void compile_file(struct compile_state *state, const char *filename, int local)
3613 {
3614         char cwd[4096];
3615         const char *subdir, *base;
3616         int subdir_len;
3617         struct file_state *file;
3618         char *basename;
3619         file = xmalloc(sizeof(*file), "file_state");
3620
3621         base = strrchr(filename, '/');
3622         subdir = filename;
3623         if (base != 0) {
3624                 subdir_len = base - filename;
3625                 base++;
3626         }
3627         else {
3628                 base = filename;
3629                 subdir_len = 0;
3630         }
3631         basename = xmalloc(strlen(base) +1, "basename");
3632         strcpy(basename, base);
3633         file->basename = basename;
3634
3635         if (getcwd(cwd, sizeof(cwd)) == 0) {
3636                 die("cwd buffer to small");
3637         }
3638         
3639         if (subdir[0] == '/') {
3640                 file->dirname = xmalloc(subdir_len + 1, "dirname");
3641                 memcpy(file->dirname, subdir, subdir_len);
3642                 file->dirname[subdir_len] = '\0';
3643         }
3644         else {
3645                 char *dir;
3646                 int dirlen;
3647                 char **path;
3648                 /* Find the appropriate directory... */
3649                 dir = 0;
3650                 if (!state->file && exists(cwd, filename)) {
3651                         dir = cwd;
3652                 }
3653                 if (local && state->file && exists(state->file->dirname, filename)) {
3654                         dir = state->file->dirname;
3655                 }
3656                 for(path = include_paths; !dir && *path; path++) {
3657                         if (exists(*path, filename)) {
3658                                 dir = *path;
3659                         }
3660                 }
3661                 if (!dir) {
3662                         error(state, 0, "Cannot find `%s'\n", filename);
3663                 }
3664                 dirlen = strlen(dir);
3665                 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3666                 memcpy(file->dirname, dir, dirlen);
3667                 file->dirname[dirlen] = '/';
3668                 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3669                 file->dirname[dirlen + 1 + subdir_len] = '\0';
3670         }
3671         file->buf = slurp_file(file->dirname, file->basename, &file->size);
3672         xchdir(cwd);
3673
3674         file->pos = file->buf;
3675         file->line_start = file->pos;
3676         file->line = 1;
3677
3678         file->report_line = 1;
3679         file->report_name = file->basename;
3680         file->report_dir  = file->dirname;
3681
3682         file->prev = state->file;
3683         state->file = file;
3684         
3685         process_trigraphs(state);
3686         splice_lines(state);
3687 }
3688
3689 /* Type helper functions */
3690
3691 static struct type *new_type(
3692         unsigned int type, struct type *left, struct type *right)
3693 {
3694         struct type *result;
3695         result = xmalloc(sizeof(*result), "type");
3696         result->type = type;
3697         result->left = left;
3698         result->right = right;
3699         result->field_ident = 0;
3700         result->type_ident = 0;
3701         return result;
3702 }
3703
3704 static struct type *clone_type(unsigned int specifiers, struct type *old)
3705 {
3706         struct type *result;
3707         result = xmalloc(sizeof(*result), "type");
3708         memcpy(result, old, sizeof(*result));
3709         result->type &= TYPE_MASK;
3710         result->type |= specifiers;
3711         return result;
3712 }
3713
3714 #define SIZEOF_SHORT 2
3715 #define SIZEOF_INT   4
3716 #define SIZEOF_LONG  (sizeof(long_t))
3717
3718 #define ALIGNOF_SHORT 2
3719 #define ALIGNOF_INT   4
3720 #define ALIGNOF_LONG  (sizeof(long_t))
3721
3722 #define MASK_UCHAR(X)    ((X) & ((ulong_t)0xff))
3723 #define MASK_USHORT(X)   ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3724 static inline ulong_t mask_uint(ulong_t x)
3725 {
3726         if (SIZEOF_INT < SIZEOF_LONG) {
3727                 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3728                 x &= mask;
3729         }
3730         return x;
3731 }
3732 #define MASK_UINT(X)      (mask_uint(X))
3733 #define MASK_ULONG(X)    (X)
3734
3735 static struct type void_type   = { .type  = TYPE_VOID };
3736 static struct type char_type   = { .type  = TYPE_CHAR };
3737 static struct type uchar_type  = { .type  = TYPE_UCHAR };
3738 static struct type short_type  = { .type  = TYPE_SHORT };
3739 static struct type ushort_type = { .type  = TYPE_USHORT };
3740 static struct type int_type    = { .type  = TYPE_INT };
3741 static struct type uint_type   = { .type  = TYPE_UINT };
3742 static struct type long_type   = { .type  = TYPE_LONG };
3743 static struct type ulong_type  = { .type  = TYPE_ULONG };
3744
3745 static struct triple *variable(struct compile_state *state, struct type *type)
3746 {
3747         struct triple *result;
3748         if ((type->type & STOR_MASK) != STOR_PERM) {
3749                 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3750                         result = triple(state, OP_ADECL, type, 0, 0);
3751                 } else {
3752                         struct type *field;
3753                         struct triple **vector;
3754                         ulong_t index;
3755                         result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3756                         vector = &result->param[0];
3757
3758                         field = type->left;
3759                         index = 0;
3760                         while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3761                                 vector[index] = variable(state, field->left);
3762                                 field = field->right;
3763                                 index++;
3764                         }
3765                         vector[index] = variable(state, field);
3766                 }
3767         }
3768         else {
3769                 result = triple(state, OP_SDECL, type, 0, 0);
3770         }
3771         return result;
3772 }
3773
3774 static void stor_of(FILE *fp, struct type *type)
3775 {
3776         switch(type->type & STOR_MASK) {
3777         case STOR_AUTO:
3778                 fprintf(fp, "auto ");
3779                 break;
3780         case STOR_STATIC:
3781                 fprintf(fp, "static ");
3782                 break;
3783         case STOR_EXTERN:
3784                 fprintf(fp, "extern ");
3785                 break;
3786         case STOR_REGISTER:
3787                 fprintf(fp, "register ");
3788                 break;
3789         case STOR_TYPEDEF:
3790                 fprintf(fp, "typedef ");
3791                 break;
3792         case STOR_INLINE:
3793                 fprintf(fp, "inline ");
3794                 break;
3795         }
3796 }
3797 static void qual_of(FILE *fp, struct type *type)
3798 {
3799         if (type->type & QUAL_CONST) {
3800                 fprintf(fp, " const");
3801         }
3802         if (type->type & QUAL_VOLATILE) {
3803                 fprintf(fp, " volatile");
3804         }
3805         if (type->type & QUAL_RESTRICT) {
3806                 fprintf(fp, " restrict");
3807         }
3808 }
3809
3810 static void name_of(FILE *fp, struct type *type)
3811 {
3812         stor_of(fp, type);
3813         switch(type->type & TYPE_MASK) {
3814         case TYPE_VOID:
3815                 fprintf(fp, "void");
3816                 qual_of(fp, type);
3817                 break;
3818         case TYPE_CHAR:
3819                 fprintf(fp, "signed char");
3820                 qual_of(fp, type);
3821                 break;
3822         case TYPE_UCHAR:
3823                 fprintf(fp, "unsigned char");
3824                 qual_of(fp, type);
3825                 break;
3826         case TYPE_SHORT:
3827                 fprintf(fp, "signed short");
3828                 qual_of(fp, type);
3829                 break;
3830         case TYPE_USHORT:
3831                 fprintf(fp, "unsigned short");
3832                 qual_of(fp, type);
3833                 break;
3834         case TYPE_INT:
3835                 fprintf(fp, "signed int");
3836                 qual_of(fp, type);
3837                 break;
3838         case TYPE_UINT:
3839                 fprintf(fp, "unsigned int");
3840                 qual_of(fp, type);
3841                 break;
3842         case TYPE_LONG:
3843                 fprintf(fp, "signed long");
3844                 qual_of(fp, type);
3845                 break;
3846         case TYPE_ULONG:
3847                 fprintf(fp, "unsigned long");
3848                 qual_of(fp, type);
3849                 break;
3850         case TYPE_POINTER:
3851                 name_of(fp, type->left);
3852                 fprintf(fp, " * ");
3853                 qual_of(fp, type);
3854                 break;
3855         case TYPE_PRODUCT:
3856         case TYPE_OVERLAP:
3857                 name_of(fp, type->left);
3858                 fprintf(fp, ", ");
3859                 name_of(fp, type->right);
3860                 break;
3861         case TYPE_ENUM:
3862                 fprintf(fp, "enum %s", type->type_ident->name);
3863                 qual_of(fp, type);
3864                 break;
3865         case TYPE_STRUCT:
3866                 fprintf(fp, "struct %s", type->type_ident->name);
3867                 qual_of(fp, type);
3868                 break;
3869         case TYPE_FUNCTION:
3870         {
3871                 name_of(fp, type->left);
3872                 fprintf(fp, " (*)(");
3873                 name_of(fp, type->right);
3874                 fprintf(fp, ")");
3875                 break;
3876         }
3877         case TYPE_ARRAY:
3878                 name_of(fp, type->left);
3879                 fprintf(fp, " [%ld]", type->elements);
3880                 break;
3881         default:
3882                 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3883                 break;
3884         }
3885 }
3886
3887 static size_t align_of(struct compile_state *state, struct type *type)
3888 {
3889         size_t align;
3890         align = 0;
3891         switch(type->type & TYPE_MASK) {
3892         case TYPE_VOID:
3893                 align = 1;
3894                 break;
3895         case TYPE_CHAR:
3896         case TYPE_UCHAR:
3897                 align = 1;
3898                 break;
3899         case TYPE_SHORT:
3900         case TYPE_USHORT:
3901                 align = ALIGNOF_SHORT;
3902                 break;
3903         case TYPE_INT:
3904         case TYPE_UINT:
3905         case TYPE_ENUM:
3906                 align = ALIGNOF_INT;
3907                 break;
3908         case TYPE_LONG:
3909         case TYPE_ULONG:
3910         case TYPE_POINTER:
3911                 align = ALIGNOF_LONG;
3912                 break;
3913         case TYPE_PRODUCT:
3914         case TYPE_OVERLAP:
3915         {
3916                 size_t left_align, right_align;
3917                 left_align  = align_of(state, type->left);
3918                 right_align = align_of(state, type->right);
3919                 align = (left_align >= right_align) ? left_align : right_align;
3920                 break;
3921         }
3922         case TYPE_ARRAY:
3923                 align = align_of(state, type->left);
3924                 break;
3925         case TYPE_STRUCT:
3926                 align = align_of(state, type->left);
3927                 break;
3928         default:
3929                 error(state, 0, "alignof not yet defined for type\n");
3930                 break;
3931         }
3932         return align;
3933 }
3934
3935 static size_t size_of(struct compile_state *state, struct type *type)
3936 {
3937         size_t size;
3938         size = 0;
3939         switch(type->type & TYPE_MASK) {
3940         case TYPE_VOID:
3941                 size = 0;
3942                 break;
3943         case TYPE_CHAR:
3944         case TYPE_UCHAR:
3945                 size = 1;
3946                 break;
3947         case TYPE_SHORT:
3948         case TYPE_USHORT:
3949                 size = SIZEOF_SHORT;
3950                 break;
3951         case TYPE_INT:
3952         case TYPE_UINT:
3953         case TYPE_ENUM:
3954                 size = SIZEOF_INT;
3955                 break;
3956         case TYPE_LONG:
3957         case TYPE_ULONG:
3958         case TYPE_POINTER:
3959                 size = SIZEOF_LONG;
3960                 break;
3961         case TYPE_PRODUCT:
3962         {
3963                 size_t align, pad;
3964                 size = size_of(state, type->left);
3965                 while((type->right->type & TYPE_MASK) == TYPE_PRODUCT) {
3966                         type = type->right;
3967                         align = align_of(state, type->left);
3968                         pad = align - (size % align);
3969                         size = size + pad + size_of(state, type->left);
3970                 }
3971                 align = align_of(state, type->right);
3972                 pad = align - (size % align);
3973                 size = size + pad + sizeof(type->right);
3974                 break;
3975         }
3976         case TYPE_OVERLAP:
3977         {
3978                 size_t size_left, size_right;
3979                 size_left = size_of(state, type->left);
3980                 size_right = size_of(state, type->right);
3981                 size = (size_left >= size_right)? size_left : size_right;
3982                 break;
3983         }
3984         case TYPE_ARRAY:
3985                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
3986                         internal_error(state, 0, "Invalid array type");
3987                 } else {
3988                         size = size_of(state, type->left) * type->elements;
3989                 }
3990                 break;
3991         case TYPE_STRUCT:
3992                 size = size_of(state, type->left);
3993                 break;
3994         default:
3995                 error(state, 0, "sizeof not yet defined for type\n");
3996                 break;
3997         }
3998         return size;
3999 }
4000
4001 static size_t field_offset(struct compile_state *state, 
4002         struct type *type, struct hash_entry *field)
4003 {
4004         size_t size, align, pad;
4005         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4006                 internal_error(state, 0, "field_offset only works on structures");
4007         }
4008         size = 0;
4009         type = type->left;
4010         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4011                 if (type->left->field_ident == field) {
4012                         type = type->left;
4013                         break;
4014                 }
4015                 size += size_of(state, type->left);
4016                 type = type->right;
4017                 align = align_of(state, type);
4018                 pad = align - (size % align);
4019                 size += pad;
4020         }
4021         if (type->field_ident != field) {
4022                 internal_error(state, 0, "field_offset: member %s not present",
4023                         field->name);
4024         }
4025         return size;
4026 }
4027
4028 static struct type *field_type(struct compile_state *state, 
4029         struct type *type, struct hash_entry *field)
4030 {
4031         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4032                 internal_error(state, 0, "field_type only works on structures");
4033         }
4034         type = type->left;
4035         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4036                 if (type->left->field_ident == field) {
4037                         type = type->left;
4038                         break;
4039                 }
4040                 type = type->right;
4041         }
4042         if (type->field_ident != field) {
4043                 internal_error(state, 0, "field_type: member %s not present", 
4044                         field->name);
4045         }
4046         return type;
4047 }
4048
4049 static struct triple *struct_field(struct compile_state *state,
4050         struct triple *decl, struct hash_entry *field)
4051 {
4052         struct triple **vector;
4053         struct type *type;
4054         ulong_t index;
4055         type = decl->type;
4056         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4057                 return decl;
4058         }
4059         if (decl->op != OP_VAL_VEC) {
4060                 internal_error(state, 0, "Invalid struct variable");
4061         }
4062         if (!field) {
4063                 internal_error(state, 0, "Missing structure field");
4064         }
4065         type = type->left;
4066         vector = &RHS(decl, 0);
4067         index = 0;
4068         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4069                 if (type->left->field_ident == field) {
4070                         type = type->left;
4071                         break;
4072                 }
4073                 index += 1;
4074                 type = type->right;
4075         }
4076         if (type->field_ident != field) {
4077                 internal_error(state, 0, "field %s not found?", field->name);
4078         }
4079         return vector[index];
4080 }
4081
4082 static void arrays_complete(struct compile_state *state, struct type *type)
4083 {
4084         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4085                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4086                         error(state, 0, "array size not specified");
4087                 }
4088                 arrays_complete(state, type->left);
4089         }
4090 }
4091
4092 static unsigned int do_integral_promotion(unsigned int type)
4093 {
4094         type &= TYPE_MASK;
4095         if (TYPE_INTEGER(type) && 
4096                 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4097                 type = TYPE_INT;
4098         }
4099         return type;
4100 }
4101
4102 static unsigned int do_arithmetic_conversion(
4103         unsigned int left, unsigned int right)
4104 {
4105         left &= TYPE_MASK;
4106         right &= TYPE_MASK;
4107         if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4108                 return TYPE_LDOUBLE;
4109         }
4110         else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4111                 return TYPE_DOUBLE;
4112         }
4113         else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4114                 return TYPE_FLOAT;
4115         }
4116         left = do_integral_promotion(left);
4117         right = do_integral_promotion(right);
4118         /* If both operands have the same size done */
4119         if (left == right) {
4120                 return left;
4121         }
4122         /* If both operands have the same signedness pick the larger */
4123         else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4124                 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4125         }
4126         /* If the signed type can hold everything use it */
4127         else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4128                 return left;
4129         }
4130         else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4131                 return right;
4132         }
4133         /* Convert to the unsigned type with the same rank as the signed type */
4134         else if (TYPE_SIGNED(left)) {
4135                 return TYPE_MKUNSIGNED(left);
4136         }
4137         else {
4138                 return TYPE_MKUNSIGNED(right);
4139         }
4140 }
4141
4142 /* see if two types are the same except for qualifiers */
4143 static int equiv_types(struct type *left, struct type *right)
4144 {
4145         unsigned int type;
4146         /* Error if the basic types do not match */
4147         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4148                 return 0;
4149         }
4150         type = left->type & TYPE_MASK;
4151         /* if the basic types match and it is an arithmetic type we are done */
4152         if (TYPE_ARITHMETIC(type)) {
4153                 return 1;
4154         }
4155         /* If it is a pointer type recurse and keep testing */
4156         if (type == TYPE_POINTER) {
4157                 return equiv_types(left->left, right->left);
4158         }
4159         else if (type == TYPE_ARRAY) {
4160                 return (left->elements == right->elements) &&
4161                         equiv_types(left->left, right->left);
4162         }
4163         /* test for struct/union equality */
4164         else if (type == TYPE_STRUCT) {
4165                 return left->type_ident == right->type_ident;
4166         }
4167         /* Test for equivalent functions */
4168         else if (type == TYPE_FUNCTION) {
4169                 return equiv_types(left->left, right->left) &&
4170                         equiv_types(left->right, right->right);
4171         }
4172         /* We only see TYPE_PRODUCT as part of function equivalence matching */
4173         else if (type == TYPE_PRODUCT) {
4174                 return equiv_types(left->left, right->left) &&
4175                         equiv_types(left->right, right->right);
4176         }
4177         /* We should see TYPE_OVERLAP */
4178         else {
4179                 return 0;
4180         }
4181 }
4182
4183 static int equiv_ptrs(struct type *left, struct type *right)
4184 {
4185         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4186                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4187                 return 0;
4188         }
4189         return equiv_types(left->left, right->left);
4190 }
4191
4192 static struct type *compatible_types(struct type *left, struct type *right)
4193 {
4194         struct type *result;
4195         unsigned int type, qual_type;
4196         /* Error if the basic types do not match */
4197         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4198                 return 0;
4199         }
4200         type = left->type & TYPE_MASK;
4201         qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4202         result = 0;
4203         /* if the basic types match and it is an arithmetic type we are done */
4204         if (TYPE_ARITHMETIC(type)) {
4205                 result = new_type(qual_type, 0, 0);
4206         }
4207         /* If it is a pointer type recurse and keep testing */
4208         else if (type == TYPE_POINTER) {
4209                 result = compatible_types(left->left, right->left);
4210                 if (result) {
4211                         result = new_type(qual_type, result, 0);
4212                 }
4213         }
4214         /* test for struct/union equality */
4215         else if (type == TYPE_STRUCT) {
4216                 if (left->type_ident == right->type_ident) {
4217                         result = left;
4218                 }
4219         }
4220         /* Test for equivalent functions */
4221         else if (type == TYPE_FUNCTION) {
4222                 struct type *lf, *rf;
4223                 lf = compatible_types(left->left, right->left);
4224                 rf = compatible_types(left->right, right->right);
4225                 if (lf && rf) {
4226                         result = new_type(qual_type, lf, rf);
4227                 }
4228         }
4229         /* We only see TYPE_PRODUCT as part of function equivalence matching */
4230         else if (type == TYPE_PRODUCT) {
4231                 struct type *lf, *rf;
4232                 lf = compatible_types(left->left, right->left);
4233                 rf = compatible_types(left->right, right->right);
4234                 if (lf && rf) {
4235                         result = new_type(qual_type, lf, rf);
4236                 }
4237         }
4238         else {
4239                 /* Nothing else is compatible */
4240         }
4241         return result;
4242 }
4243
4244 static struct type *compatible_ptrs(struct type *left, struct type *right)
4245 {
4246         struct type *result;
4247         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4248                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4249                 return 0;
4250         }
4251         result = compatible_types(left->left, right->left);
4252         if (result) {
4253                 unsigned int qual_type;
4254                 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4255                 result = new_type(qual_type, result, 0);
4256         }
4257         return result;
4258         
4259 }
4260 static struct triple *integral_promotion(
4261         struct compile_state *state, struct triple *def)
4262 {
4263         struct type *type;
4264         type = def->type;
4265         /* As all operations are carried out in registers
4266          * the values are converted on load I just convert
4267          * logical type of the operand.
4268          */
4269         if (TYPE_INTEGER(type->type)) {
4270                 unsigned int int_type;
4271                 int_type = type->type & ~TYPE_MASK;
4272                 int_type |= do_integral_promotion(type->type);
4273                 if (int_type != type->type) {
4274                         def->type = new_type(int_type, 0, 0);
4275                 }
4276         }
4277         return def;
4278 }
4279
4280
4281 static void arithmetic(struct compile_state *state, struct triple *def)
4282 {
4283         if (!TYPE_ARITHMETIC(def->type->type)) {
4284                 error(state, 0, "arithmetic type expexted");
4285         }
4286 }
4287
4288 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4289 {
4290         if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4291                 error(state, def, "pointer or arithmetic type expected");
4292         }
4293 }
4294
4295 static int is_integral(struct triple *ins)
4296 {
4297         return TYPE_INTEGER(ins->type->type);
4298 }
4299
4300 static void integral(struct compile_state *state, struct triple *def)
4301 {
4302         if (!is_integral(def)) {
4303                 error(state, 0, "integral type expected");
4304         }
4305 }
4306
4307
4308 static void bool(struct compile_state *state, struct triple *def)
4309 {
4310         if (!TYPE_ARITHMETIC(def->type->type) &&
4311                 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4312                 error(state, 0, "arithmetic or pointer type expected");
4313         }
4314 }
4315
4316 static int is_signed(struct type *type)
4317 {
4318         return !!TYPE_SIGNED(type->type);
4319 }
4320
4321 /* Is this value located in a register otherwise it must be in memory */
4322 static int is_in_reg(struct compile_state *state, struct triple *def)
4323 {
4324         int in_reg;
4325         if (def->op == OP_ADECL) {
4326                 in_reg = 1;
4327         }
4328         else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4329                 in_reg = 0;
4330         }
4331         else if (def->op == OP_VAL_VEC) {
4332                 in_reg = is_in_reg(state, RHS(def, 0));
4333         }
4334         else if (def->op == OP_DOT) {
4335                 in_reg = is_in_reg(state, RHS(def, 0));
4336         }
4337         else {
4338                 internal_error(state, 0, "unknown expr storage location");
4339                 in_reg = -1;
4340         }
4341         return in_reg;
4342 }
4343
4344 /* Is this a stable variable location otherwise it must be a temporary */
4345 static int is_stable(struct compile_state *state, struct triple *def)
4346 {
4347         int ret;
4348         ret = 0;
4349         if (!def) {
4350                 return 0;
4351         }
4352         if ((def->op == OP_ADECL) || 
4353                 (def->op == OP_SDECL) || 
4354                 (def->op == OP_DEREF) ||
4355                 (def->op == OP_BLOBCONST)) {
4356                 ret = 1;
4357         }
4358         else if (def->op == OP_DOT) {
4359                 ret = is_stable(state, RHS(def, 0));
4360         }
4361         else if (def->op == OP_VAL_VEC) {
4362                 struct triple **vector;
4363                 ulong_t i;
4364                 ret = 1;
4365                 vector = &RHS(def, 0);
4366                 for(i = 0; i < def->type->elements; i++) {
4367                         if (!is_stable(state, vector[i])) {
4368                                 ret = 0;
4369                                 break;
4370                         }
4371                 }
4372         }
4373         return ret;
4374 }
4375
4376 static int is_lvalue(struct compile_state *state, struct triple *def)
4377 {
4378         int ret;
4379         ret = 1;
4380         if (!def) {
4381                 return 0;
4382         }
4383         if (!is_stable(state, def)) {
4384                 return 0;
4385         }
4386         if (def->op == OP_DOT) {
4387                 ret = is_lvalue(state, RHS(def, 0));
4388         }
4389         return ret;
4390 }
4391
4392 static void clvalue(struct compile_state *state, struct triple *def)
4393 {
4394         if (!def) {
4395                 internal_error(state, def, "nothing where lvalue expected?");
4396         }
4397         if (!is_lvalue(state, def)) { 
4398                 error(state, def, "lvalue expected");
4399         }
4400 }
4401 static void lvalue(struct compile_state *state, struct triple *def)
4402 {
4403         clvalue(state, def);
4404         if (def->type->type & QUAL_CONST) {
4405                 error(state, def, "modifable lvalue expected");
4406         }
4407 }
4408
4409 static int is_pointer(struct triple *def)
4410 {
4411         return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4412 }
4413
4414 static void pointer(struct compile_state *state, struct triple *def)
4415 {
4416         if (!is_pointer(def)) {
4417                 error(state, def, "pointer expected");
4418         }
4419 }
4420
4421 static struct triple *int_const(
4422         struct compile_state *state, struct type *type, ulong_t value)
4423 {
4424         struct triple *result;
4425         switch(type->type & TYPE_MASK) {
4426         case TYPE_CHAR:
4427         case TYPE_INT:   case TYPE_UINT:
4428         case TYPE_LONG:  case TYPE_ULONG:
4429                 break;
4430         default:
4431                 internal_error(state, 0, "constant for unkown type");
4432         }
4433         result = triple(state, OP_INTCONST, type, 0, 0);
4434         result->u.cval = value;
4435         return result;
4436 }
4437
4438
4439 static struct triple *do_mk_addr_expr(struct compile_state *state, 
4440         struct triple *expr, struct type *type, ulong_t offset)
4441 {
4442         struct triple *result;
4443         clvalue(state, expr);
4444
4445         result = 0;
4446         if (expr->op == OP_ADECL) {
4447                 error(state, expr, "address of auto variables not supported");
4448         }
4449         else if (expr->op == OP_SDECL) {
4450                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4451                 MISC(result, 0) = expr;
4452                 result->u.cval = offset;
4453         }
4454         else if (expr->op == OP_DEREF) {
4455                 result = triple(state, OP_ADD, type,
4456                         RHS(expr, 0),
4457                         int_const(state, &ulong_type, offset));
4458         }
4459         return result;
4460 }
4461
4462 static struct triple *mk_addr_expr(
4463         struct compile_state *state, struct triple *expr, ulong_t offset)
4464 {
4465         struct type *type;
4466         
4467         type = new_type(
4468                 TYPE_POINTER | (expr->type->type & QUAL_MASK),
4469                 expr->type, 0);
4470
4471         return do_mk_addr_expr(state, expr, type, offset);
4472 }
4473
4474 static struct triple *mk_deref_expr(
4475         struct compile_state *state, struct triple *expr)
4476 {
4477         struct type *base_type;
4478         pointer(state, expr);
4479         base_type = expr->type->left;
4480         return triple(state, OP_DEREF, base_type, expr, 0);
4481 }
4482
4483 static struct triple *deref_field(
4484         struct compile_state *state, struct triple *expr, struct hash_entry *field)
4485 {
4486         struct triple *result;
4487         struct type *type, *member;
4488         if (!field) {
4489                 internal_error(state, 0, "No field passed to deref_field");
4490         }
4491         result = 0;
4492         type = expr->type;
4493         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4494                 error(state, 0, "request for member %s in something not a struct or union",
4495                         field->name);
4496         }
4497         member = type->left;
4498         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4499                 if (member->left->field_ident == field) {
4500                         member = member->left;
4501                         break;
4502                 }
4503                 member = member->right;
4504         }
4505         if (member->field_ident != field) {
4506                 error(state, 0, "%s is not a member", field->name);
4507         }
4508         if ((type->type & STOR_MASK) == STOR_PERM) {
4509                 /* Do the pointer arithmetic to get a deref the field */
4510                 ulong_t offset;
4511                 offset = field_offset(state, type, field);
4512                 result = do_mk_addr_expr(state, expr, member, offset);
4513                 result = mk_deref_expr(state, result);
4514         }
4515         else {
4516                 /* Find the variable for the field I want. */
4517                 result = triple(state, OP_DOT, 
4518                         field_type(state, type, field), expr, 0);
4519                 result->u.field = field;
4520         }
4521         return result;
4522 }
4523
4524 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4525 {
4526         int op;
4527         if  (!def) {
4528                 return 0;
4529         }
4530         if (!is_stable(state, def)) {
4531                 return def;
4532         }
4533         /* Tranform an array to a pointer to the first element */
4534 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4535         if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4536                 struct type *type;
4537                 struct triple *result;
4538                 type = new_type(
4539                         TYPE_POINTER | (def->type->type & QUAL_MASK),
4540                         def->type->left, 0);
4541                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4542                 MISC(result, 0) = def;
4543                 return result;
4544         }
4545         if (is_in_reg(state, def)) {
4546                 op = OP_READ;
4547         } else {
4548                 op = OP_LOAD;
4549         }
4550         return triple(state, op, def->type, def, 0);
4551 }
4552
4553 static void write_compatible(struct compile_state *state,
4554         struct type *dest, struct type *rval)
4555 {
4556         int compatible = 0;
4557         /* Both operands have arithmetic type */
4558         if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4559                 compatible = 1;
4560         }
4561         /* One operand is a pointer and the other is a pointer to void */
4562         else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4563                 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4564                 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4565                         ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4566                 compatible = 1;
4567         }
4568         /* If both types are the same without qualifiers we are good */
4569         else if (equiv_ptrs(dest, rval)) {
4570                 compatible = 1;
4571         }
4572         /* test for struct/union equality  */
4573         else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4574                 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4575                 (dest->type_ident == rval->type_ident)) {
4576                 compatible = 1;
4577         }
4578         if (!compatible) {
4579                 error(state, 0, "Incompatible types in assignment");
4580         }
4581 }
4582
4583 static struct triple *write_expr(
4584         struct compile_state *state, struct triple *dest, struct triple *rval)
4585 {
4586         struct triple *def;
4587         int op;
4588
4589         def = 0;
4590         if (!rval) {
4591                 internal_error(state, 0, "missing rval");
4592         }
4593
4594         if (rval->op == OP_LIST) {
4595                 internal_error(state, 0, "expression of type OP_LIST?");
4596         }
4597         if (!is_lvalue(state, dest)) {
4598                 internal_error(state, 0, "writing to a non lvalue?");
4599         }
4600         if (dest->type->type & QUAL_CONST) {
4601                 internal_error(state, 0, "modifable lvalue expexted");
4602         }
4603
4604         write_compatible(state, dest->type, rval->type);
4605
4606         /* Now figure out which assignment operator to use */
4607         op = -1;
4608         if (is_in_reg(state, dest)) {
4609                 op = OP_WRITE;
4610         } else {
4611                 op = OP_STORE;
4612         }
4613         def = triple(state, op, dest->type, dest, rval);
4614         return def;
4615 }
4616
4617 static struct triple *init_expr(
4618         struct compile_state *state, struct triple *dest, struct triple *rval)
4619 {
4620         struct triple *def;
4621
4622         def = 0;
4623         if (!rval) {
4624                 internal_error(state, 0, "missing rval");
4625         }
4626         if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4627                 rval = read_expr(state, rval);
4628                 def = write_expr(state, dest, rval);
4629         }
4630         else {
4631                 /* Fill in the array size if necessary */
4632                 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4633                         ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4634                         if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4635                                 dest->type->elements = rval->type->elements;
4636                         }
4637                 }
4638                 if (!equiv_types(dest->type, rval->type)) {
4639                         error(state, 0, "Incompatible types in inializer");
4640                 }
4641                 MISC(dest, 0) = rval;
4642                 insert_triple(state, dest, rval);
4643                 rval->id |= TRIPLE_FLAG_FLATTENED;
4644                 use_triple(MISC(dest, 0), dest);
4645         }
4646         return def;
4647 }
4648
4649 struct type *arithmetic_result(
4650         struct compile_state *state, struct triple *left, struct triple *right)
4651 {
4652         struct type *type;
4653         /* Sanity checks to ensure I am working with arithmetic types */
4654         arithmetic(state, left);
4655         arithmetic(state, right);
4656         type = new_type(
4657                 do_arithmetic_conversion(
4658                         left->type->type, 
4659                         right->type->type), 0, 0);
4660         return type;
4661 }
4662
4663 struct type *ptr_arithmetic_result(
4664         struct compile_state *state, struct triple *left, struct triple *right)
4665 {
4666         struct type *type;
4667         /* Sanity checks to ensure I am working with the proper types */
4668         ptr_arithmetic(state, left);
4669         arithmetic(state, right);
4670         if (TYPE_ARITHMETIC(left->type->type) && 
4671                 TYPE_ARITHMETIC(right->type->type)) {
4672                 type = arithmetic_result(state, left, right);
4673         }
4674         else if (TYPE_PTR(left->type->type)) {
4675                 type = left->type;
4676         }
4677         else {
4678                 internal_error(state, 0, "huh?");
4679                 type = 0;
4680         }
4681         return type;
4682 }
4683
4684
4685 /* boolean helper function */
4686
4687 static struct triple *ltrue_expr(struct compile_state *state, 
4688         struct triple *expr)
4689 {
4690         switch(expr->op) {
4691         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
4692         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
4693         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4694                 /* If the expression is already boolean do nothing */
4695                 break;
4696         default:
4697                 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4698                 break;
4699         }
4700         return expr;
4701 }
4702
4703 static struct triple *lfalse_expr(struct compile_state *state, 
4704         struct triple *expr)
4705 {
4706         return triple(state, OP_LFALSE, &int_type, expr, 0);
4707 }
4708
4709 static struct triple *cond_expr(
4710         struct compile_state *state, 
4711         struct triple *test, struct triple *left, struct triple *right)
4712 {
4713         struct triple *def;
4714         struct type *result_type;
4715         unsigned int left_type, right_type;
4716         bool(state, test);
4717         left_type = left->type->type;
4718         right_type = right->type->type;
4719         result_type = 0;
4720         /* Both operands have arithmetic type */
4721         if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4722                 result_type = arithmetic_result(state, left, right);
4723         }
4724         /* Both operands have void type */
4725         else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4726                 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4727                 result_type = &void_type;
4728         }
4729         /* pointers to the same type... */
4730         else if ((result_type = compatible_ptrs(left->type, right->type))) {
4731                 ;
4732         }
4733         /* Both operands are pointers and left is a pointer to void */
4734         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4735                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4736                 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4737                 result_type = right->type;
4738         }
4739         /* Both operands are pointers and right is a pointer to void */
4740         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4741                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4742                 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4743                 result_type = left->type;
4744         }
4745         if (!result_type) {
4746                 error(state, 0, "Incompatible types in conditional expression");
4747         }
4748         /* Cleanup and invert the test */
4749         test = lfalse_expr(state, read_expr(state, test));
4750         def = new_triple(state, OP_COND, result_type, 0, 3);
4751         def->param[0] = test;
4752         def->param[1] = left;
4753         def->param[2] = right;
4754         return def;
4755 }
4756
4757
4758 static int expr_depth(struct compile_state *state, struct triple *ins)
4759 {
4760         int count;
4761         count = 0;
4762         if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4763                 count = 0;
4764         }
4765         else if (ins->op == OP_DEREF) {
4766                 count = expr_depth(state, RHS(ins, 0)) - 1;
4767         }
4768         else if (ins->op == OP_VAL) {
4769                 count = expr_depth(state, RHS(ins, 0)) - 1;
4770         }
4771         else if (ins->op == OP_COMMA) {
4772                 int ldepth, rdepth;
4773                 ldepth = expr_depth(state, RHS(ins, 0));
4774                 rdepth = expr_depth(state, RHS(ins, 1));
4775                 count = (ldepth >= rdepth)? ldepth : rdepth;
4776         }
4777         else if (ins->op == OP_CALL) {
4778                 /* Don't figure the depth of a call just guess it is huge */
4779                 count = 1000;
4780         }
4781         else {
4782                 struct triple **expr;
4783                 expr = triple_rhs(state, ins, 0);
4784                 for(;expr; expr = triple_rhs(state, ins, expr)) {
4785                         if (*expr) {
4786                                 int depth;
4787                                 depth = expr_depth(state, *expr);
4788                                 if (depth > count) {
4789                                         count = depth;
4790                                 }
4791                         }
4792                 }
4793         }
4794         return count + 1;
4795 }
4796
4797 static struct triple *flatten(
4798         struct compile_state *state, struct triple *first, struct triple *ptr);
4799
4800 static struct triple *mk_add_expr(
4801         struct compile_state *state, struct triple *left, struct triple *right);
4802
4803 static struct triple *flatten_generic(
4804         struct compile_state *state, struct triple *first, struct triple *ptr)
4805 {
4806         struct rhs_vector {
4807                 int depth;
4808                 struct triple **ins;
4809         } vector[MAX_RHS];
4810         int i, rhs, lhs;
4811         /* Only operations with just a rhs should come here */
4812         rhs = TRIPLE_RHS(ptr->sizes);
4813         lhs = TRIPLE_LHS(ptr->sizes);
4814         if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4815                 internal_error(state, ptr, "unexpected args for: %d %s",
4816                         ptr->op, tops(ptr->op));
4817         }
4818         /* Find the depth of the rhs elements */
4819         for(i = 0; i < rhs; i++) {
4820                 vector[i].ins = &RHS(ptr, i);
4821                 vector[i].depth = expr_depth(state, *vector[i].ins);
4822         }
4823         /* Selection sort the rhs */
4824         for(i = 0; i < rhs; i++) {
4825                 int j, max = i;
4826                 for(j = i + 1; j < rhs; j++ ) {
4827                         if (vector[j].depth > vector[max].depth) {
4828                                 max = j;
4829                         }
4830                 }
4831                 if (max != i) {
4832                         struct rhs_vector tmp;
4833                         tmp = vector[i];
4834                         vector[i] = vector[max];
4835                         vector[max] = tmp;
4836                 }
4837         }
4838         /* Now flatten the rhs elements */
4839         for(i = 0; i < rhs; i++) {
4840                 *vector[i].ins = flatten(state, first, *vector[i].ins);
4841                 use_triple(*vector[i].ins, ptr);
4842         }
4843         
4844         /* Now flatten the lhs elements */
4845         for(i = 0; i < lhs; i++) {
4846                 struct triple **ins = &LHS(ptr, i);
4847                 *ins = flatten(state, first, *ins);
4848                 use_triple(*ins, ptr);
4849         }
4850         return ptr;
4851 }
4852
4853 static struct triple *flatten_land(
4854         struct compile_state *state, struct triple *first, struct triple *ptr)
4855 {
4856         struct triple *left, *right;
4857         struct triple *val, *test, *jmp, *label1, *end;
4858
4859         /* Find the triples */
4860         left = RHS(ptr, 0);
4861         right = RHS(ptr, 1);
4862
4863         /* Generate the needed triples */
4864         end = label(state);
4865
4866         /* Thread the triples together */
4867         val          = flatten(state, first, variable(state, ptr->type));
4868         left         = flatten(state, first, write_expr(state, val, left));
4869         test         = flatten(state, first, 
4870                 lfalse_expr(state, read_expr(state, val)));
4871         jmp          = flatten(state, first, branch(state, end, test));
4872         label1       = flatten(state, first, label(state));
4873         right        = flatten(state, first, write_expr(state, val, right));
4874         TARG(jmp, 0) = flatten(state, first, end); 
4875         
4876         /* Now give the caller something to chew on */
4877         return read_expr(state, val);
4878 }
4879
4880 static struct triple *flatten_lor(
4881         struct compile_state *state, struct triple *first, struct triple *ptr)
4882 {
4883         struct triple *left, *right;
4884         struct triple *val, *jmp, *label1, *end;
4885
4886         /* Find the triples */
4887         left = RHS(ptr, 0);
4888         right = RHS(ptr, 1);
4889
4890         /* Generate the needed triples */
4891         end = label(state);
4892
4893         /* Thread the triples together */
4894         val          = flatten(state, first, variable(state, ptr->type));
4895         left         = flatten(state, first, write_expr(state, val, left));
4896         jmp          = flatten(state, first, branch(state, end, left));
4897         label1       = flatten(state, first, label(state));
4898         right        = flatten(state, first, write_expr(state, val, right));
4899         TARG(jmp, 0) = flatten(state, first, end);
4900        
4901         
4902         /* Now give the caller something to chew on */
4903         return read_expr(state, val);
4904 }
4905
4906 static struct triple *flatten_cond(
4907         struct compile_state *state, struct triple *first, struct triple *ptr)
4908 {
4909         struct triple *test, *left, *right;
4910         struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
4911
4912         /* Find the triples */
4913         test = RHS(ptr, 0);
4914         left = RHS(ptr, 1);
4915         right = RHS(ptr, 2);
4916
4917         /* Generate the needed triples */
4918         end = label(state);
4919         middle = label(state);
4920
4921         /* Thread the triples together */
4922         val           = flatten(state, first, variable(state, ptr->type));
4923         test          = flatten(state, first, test);
4924         jmp1          = flatten(state, first, branch(state, middle, test));
4925         label1        = flatten(state, first, label(state));
4926         left          = flatten(state, first, left);
4927         mv1           = flatten(state, first, write_expr(state, val, left));
4928         jmp2          = flatten(state, first, branch(state, end, 0));
4929         TARG(jmp1, 0) = flatten(state, first, middle);
4930         right         = flatten(state, first, right);
4931         mv2           = flatten(state, first, write_expr(state, val, right));
4932         TARG(jmp2, 0) = flatten(state, first, end);
4933         
4934         /* Now give the caller something to chew on */
4935         return read_expr(state, val);
4936 }
4937
4938 struct triple *copy_func(struct compile_state *state, struct triple *ofunc, 
4939         struct occurance *base_occurance)
4940 {
4941         struct triple *nfunc;
4942         struct triple *nfirst, *ofirst;
4943         struct triple *new, *old;
4944
4945 #if 0
4946         fprintf(stdout, "\n");
4947         loc(stdout, state, 0);
4948         fprintf(stdout, "\n__________ copy_func _________\n");
4949         print_triple(state, ofunc);
4950         fprintf(stdout, "__________ copy_func _________ done\n\n");
4951 #endif
4952
4953         /* Make a new copy of the old function */
4954         nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
4955         nfirst = 0;
4956         ofirst = old = RHS(ofunc, 0);
4957         do {
4958                 struct triple *new;
4959                 struct occurance *occurance;
4960                 int old_lhs, old_rhs;
4961                 old_lhs = TRIPLE_LHS(old->sizes);
4962                 old_rhs = TRIPLE_RHS(old->sizes);
4963                 occurance = inline_occurance(state, base_occurance, old->occurance);
4964                 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
4965                         occurance);
4966                 if (!triple_stores_block(state, new)) {
4967                         memcpy(&new->u, &old->u, sizeof(new->u));
4968                 }
4969                 if (!nfirst) {
4970                         RHS(nfunc, 0) = nfirst = new;
4971                 }
4972                 else {
4973                         insert_triple(state, nfirst, new);
4974                 }
4975                 new->id |= TRIPLE_FLAG_FLATTENED;
4976                 
4977                 /* During the copy remember new as user of old */
4978                 use_triple(old, new);
4979
4980                 /* Populate the return type if present */
4981                 if (old == MISC(ofunc, 0)) {
4982                         MISC(nfunc, 0) = new;
4983                 }
4984                 old = old->next;
4985         } while(old != ofirst);
4986
4987         /* Make a second pass to fix up any unresolved references */
4988         old = ofirst;
4989         new = nfirst;
4990         do {
4991                 struct triple **oexpr, **nexpr;
4992                 int count, i;
4993                 /* Lookup where the copy is, to join pointers */
4994                 count = TRIPLE_SIZE(old->sizes);
4995                 for(i = 0; i < count; i++) {
4996                         oexpr = &old->param[i];
4997                         nexpr = &new->param[i];
4998                         if (!*nexpr && *oexpr && (*oexpr)->use) {
4999                                 *nexpr = (*oexpr)->use->member;
5000                                 if (*nexpr == old) {
5001                                         internal_error(state, 0, "new == old?");
5002                                 }
5003                                 use_triple(*nexpr, new);
5004                         }
5005                         if (!*nexpr && *oexpr) {
5006                                 internal_error(state, 0, "Could not copy %d\n", i);
5007                         }
5008                 }
5009                 old = old->next;
5010                 new = new->next;
5011         } while((old != ofirst) && (new != nfirst));
5012         
5013         /* Make a third pass to cleanup the extra useses */
5014         old = ofirst;
5015         new = nfirst;
5016         do {
5017                 unuse_triple(old, new);
5018                 old = old->next;
5019                 new = new->next;
5020         } while ((old != ofirst) && (new != nfirst));
5021         return nfunc;
5022 }
5023
5024 static struct triple *flatten_call(
5025         struct compile_state *state, struct triple *first, struct triple *ptr)
5026 {
5027         /* Inline the function call */
5028         struct type *ptype;
5029         struct triple *ofunc, *nfunc, *nfirst, *param, *result;
5030         struct triple *end, *nend;
5031         int pvals, i;
5032
5033         /* Find the triples */
5034         ofunc = MISC(ptr, 0);
5035         if (ofunc->op != OP_LIST) {
5036                 internal_error(state, 0, "improper function");
5037         }
5038         nfunc = copy_func(state, ofunc, ptr->occurance);
5039         nfirst = RHS(nfunc, 0)->next;
5040         /* Prepend the parameter reading into the new function list */
5041         ptype = nfunc->type->right;
5042         param = RHS(nfunc, 0)->next;
5043         pvals = TRIPLE_RHS(ptr->sizes);
5044         for(i = 0; i < pvals; i++) {
5045                 struct type *atype;
5046                 struct triple *arg;
5047                 atype = ptype;
5048                 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5049                         atype = ptype->left;
5050                 }
5051                 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5052                         param = param->next;
5053                 }
5054                 arg = RHS(ptr, i);
5055                 flatten(state, nfirst, write_expr(state, param, arg));
5056                 ptype = ptype->right;
5057                 param = param->next;
5058         }
5059         result = 0;
5060         if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
5061                 result = read_expr(state, MISC(nfunc,0));
5062         }
5063 #if 0
5064         fprintf(stdout, "\n");
5065         loc(stdout, state, 0);
5066         fprintf(stdout, "\n__________ flatten_call _________\n");
5067         print_triple(state, nfunc);
5068         fprintf(stdout, "__________ flatten_call _________ done\n\n");
5069 #endif
5070
5071         /* Get rid of the extra triples */
5072         nfirst = RHS(nfunc, 0)->next;
5073         free_triple(state, RHS(nfunc, 0));
5074         RHS(nfunc, 0) = 0;
5075         free_triple(state, nfunc);
5076
5077         /* Append the new function list onto the return list */
5078         end = first->prev;
5079         nend = nfirst->prev;
5080         end->next    = nfirst;
5081         nfirst->prev = end;
5082         nend->next   = first;
5083         first->prev  = nend;
5084
5085         return result;
5086 }
5087
5088 static struct triple *flatten(
5089         struct compile_state *state, struct triple *first, struct triple *ptr)
5090 {
5091         struct triple *orig_ptr;
5092         if (!ptr)
5093                 return 0;
5094         do {
5095                 orig_ptr = ptr;
5096                 /* Only flatten triples once */
5097                 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5098                         return ptr;
5099                 }
5100                 switch(ptr->op) {
5101                 case OP_WRITE:
5102                 case OP_STORE:
5103                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5104                         LHS(ptr, 0) = flatten(state, first, LHS(ptr, 0));
5105                         use_triple(LHS(ptr, 0), ptr);
5106                         use_triple(RHS(ptr, 0), ptr);
5107                         break;
5108                 case OP_COMMA:
5109                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5110                         ptr = RHS(ptr, 1);
5111                         break;
5112                 case OP_VAL:
5113                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5114                         return MISC(ptr, 0);
5115                         break;
5116                 case OP_LAND:
5117                         ptr = flatten_land(state, first, ptr);
5118                         break;
5119                 case OP_LOR:
5120                         ptr = flatten_lor(state, first, ptr);
5121                         break;
5122                 case OP_COND:
5123                         ptr = flatten_cond(state, first, ptr);
5124                         break;
5125                 case OP_CALL:
5126                         ptr = flatten_call(state, first, ptr);
5127                         break;
5128                 case OP_READ:
5129                 case OP_LOAD:
5130                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5131                         use_triple(RHS(ptr, 0), ptr);
5132                         break;
5133                 case OP_BRANCH:
5134                         use_triple(TARG(ptr, 0), ptr);
5135                         if (TRIPLE_RHS(ptr->sizes)) {
5136                                 use_triple(RHS(ptr, 0), ptr);
5137                                 if (ptr->next != ptr) {
5138                                         use_triple(ptr->next, ptr);
5139                                 }
5140                         }
5141                         break;
5142                 case OP_BLOBCONST:
5143                         insert_triple(state, first, ptr);
5144                         ptr->id |= TRIPLE_FLAG_FLATTENED;
5145                         ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
5146                         use_triple(MISC(ptr, 0), ptr);
5147                         break;
5148                 case OP_DEREF:
5149                         /* Since OP_DEREF is just a marker delete it when I flatten it */
5150                         ptr = RHS(ptr, 0);
5151                         RHS(orig_ptr, 0) = 0;
5152                         free_triple(state, orig_ptr);
5153                         break;
5154                 case OP_DOT:
5155                 {
5156                         struct triple *base;
5157                         base = RHS(ptr, 0);
5158                         if (base->op == OP_DEREF) {
5159                                 ulong_t offset;
5160                                 offset = field_offset(state, base->type, ptr->u.field);
5161                                 ptr = mk_add_expr(state, RHS(base, 0), 
5162                                         int_const(state, &ulong_type, offset));
5163                                 free_triple(state, base);
5164                         }
5165                         else if (base->op == OP_VAL_VEC) {
5166                                 base = flatten(state, first, base);
5167                                 ptr = struct_field(state, base, ptr->u.field);
5168                         }
5169                         break;
5170                 }
5171                 case OP_PIECE:
5172                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5173                         use_triple(MISC(ptr, 0), ptr);
5174                         use_triple(ptr, MISC(ptr, 0));
5175                         break;
5176                 case OP_ADDRCONST:
5177                 case OP_SDECL:
5178                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5179                         use_triple(MISC(ptr, 0), ptr);
5180                         break;
5181                 case OP_ADECL:
5182                         break;
5183                 default:
5184                         /* Flatten the easy cases we don't override */
5185                         ptr = flatten_generic(state, first, ptr);
5186                         break;
5187                 }
5188         } while(ptr && (ptr != orig_ptr));
5189         if (ptr) {
5190                 insert_triple(state, first, ptr);
5191                 ptr->id |= TRIPLE_FLAG_FLATTENED;
5192         }
5193         return ptr;
5194 }
5195
5196 static void release_expr(struct compile_state *state, struct triple *expr)
5197 {
5198         struct triple *head;
5199         head = label(state);
5200         flatten(state, head, expr);
5201         while(head->next != head) {
5202                 release_triple(state, head->next);
5203         }
5204         free_triple(state, head);
5205 }
5206
5207 static int replace_rhs_use(struct compile_state *state,
5208         struct triple *orig, struct triple *new, struct triple *use)
5209 {
5210         struct triple **expr;
5211         int found;
5212         found = 0;
5213         expr = triple_rhs(state, use, 0);
5214         for(;expr; expr = triple_rhs(state, use, expr)) {
5215                 if (*expr == orig) {
5216                         *expr = new;
5217                         found = 1;
5218                 }
5219         }
5220         if (found) {
5221                 unuse_triple(orig, use);
5222                 use_triple(new, use);
5223         }
5224         return found;
5225 }
5226
5227 static int replace_lhs_use(struct compile_state *state,
5228         struct triple *orig, struct triple *new, struct triple *use)
5229 {
5230         struct triple **expr;
5231         int found;
5232         found = 0;
5233         expr = triple_lhs(state, use, 0);
5234         for(;expr; expr = triple_lhs(state, use, expr)) {
5235                 if (*expr == orig) {
5236                         *expr = new;
5237                         found = 1;
5238                 }
5239         }
5240         if (found) {
5241                 unuse_triple(orig, use);
5242                 use_triple(new, use);
5243         }
5244         return found;
5245 }
5246
5247 static void propogate_use(struct compile_state *state,
5248         struct triple *orig, struct triple *new)
5249 {
5250         struct triple_set *user, *next;
5251         for(user = orig->use; user; user = next) {
5252                 struct triple *use;
5253                 int found;
5254                 next = user->next;
5255                 use = user->member;
5256                 found = 0;
5257                 found |= replace_rhs_use(state, orig, new, use);
5258                 found |= replace_lhs_use(state, orig, new, use);
5259                 if (!found) {
5260                         internal_error(state, use, "use without use");
5261                 }
5262         }
5263         if (orig->use) {
5264                 internal_error(state, orig, "used after propogate_use");
5265         }
5266 }
5267
5268 /*
5269  * Code generators
5270  * ===========================
5271  */
5272
5273 static struct triple *mk_add_expr(
5274         struct compile_state *state, struct triple *left, struct triple *right)
5275 {
5276         struct type *result_type;
5277         /* Put pointer operands on the left */
5278         if (is_pointer(right)) {
5279                 struct triple *tmp;
5280                 tmp = left;
5281                 left = right;
5282                 right = tmp;
5283         }
5284         left  = read_expr(state, left);
5285         right = read_expr(state, right);
5286         result_type = ptr_arithmetic_result(state, left, right);
5287         if (is_pointer(left)) {
5288                 right = triple(state, 
5289                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5290                         &ulong_type, 
5291                         right, 
5292                         int_const(state, &ulong_type, 
5293                                 size_of(state, left->type->left)));
5294         }
5295         return triple(state, OP_ADD, result_type, left, right);
5296 }
5297
5298 static struct triple *mk_sub_expr(
5299         struct compile_state *state, struct triple *left, struct triple *right)
5300 {
5301         struct type *result_type;
5302         result_type = ptr_arithmetic_result(state, left, right);
5303         left  = read_expr(state, left);
5304         right = read_expr(state, right);
5305         if (is_pointer(left)) {
5306                 right = triple(state, 
5307                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5308                         &ulong_type, 
5309                         right, 
5310                         int_const(state, &ulong_type, 
5311                                 size_of(state, left->type->left)));
5312         }
5313         return triple(state, OP_SUB, result_type, left, right);
5314 }
5315
5316 static struct triple *mk_pre_inc_expr(
5317         struct compile_state *state, struct triple *def)
5318 {
5319         struct triple *val;
5320         lvalue(state, def);
5321         val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5322         return triple(state, OP_VAL, def->type,
5323                 write_expr(state, def, val),
5324                 val);
5325 }
5326
5327 static struct triple *mk_pre_dec_expr(
5328         struct compile_state *state, struct triple *def)
5329 {
5330         struct triple *val;
5331         lvalue(state, def);
5332         val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5333         return triple(state, OP_VAL, def->type,
5334                 write_expr(state, def, val),
5335                 val);
5336 }
5337
5338 static struct triple *mk_post_inc_expr(
5339         struct compile_state *state, struct triple *def)
5340 {
5341         struct triple *val;
5342         lvalue(state, def);
5343         val = read_expr(state, def);
5344         return triple(state, OP_VAL, def->type,
5345                 write_expr(state, def,
5346                         mk_add_expr(state, val, int_const(state, &int_type, 1)))
5347                 , val);
5348 }
5349
5350 static struct triple *mk_post_dec_expr(
5351         struct compile_state *state, struct triple *def)
5352 {
5353         struct triple *val;
5354         lvalue(state, def);
5355         val = read_expr(state, def);
5356         return triple(state, OP_VAL, def->type, 
5357                 write_expr(state, def,
5358                         mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5359                 , val);
5360 }
5361
5362 static struct triple *mk_subscript_expr(
5363         struct compile_state *state, struct triple *left, struct triple *right)
5364 {
5365         left  = read_expr(state, left);
5366         right = read_expr(state, right);
5367         if (!is_pointer(left) && !is_pointer(right)) {
5368                 error(state, left, "subscripted value is not a pointer");
5369         }
5370         return mk_deref_expr(state, mk_add_expr(state, left, right));
5371 }
5372
5373 /*
5374  * Compile time evaluation
5375  * ===========================
5376  */
5377 static int is_const(struct triple *ins)
5378 {
5379         return IS_CONST_OP(ins->op);
5380 }
5381
5382 static int constants_equal(struct compile_state *state, 
5383         struct triple *left, struct triple *right)
5384 {
5385         int equal;
5386         if (!is_const(left) || !is_const(right)) {
5387                 equal = 0;
5388         }
5389         else if (left->op != right->op) {
5390                 equal = 0;
5391         }
5392         else if (!equiv_types(left->type, right->type)) {
5393                 equal = 0;
5394         }
5395         else {
5396                 equal = 0;
5397                 switch(left->op) {
5398                 case OP_INTCONST:
5399                         if (left->u.cval == right->u.cval) {
5400                                 equal = 1;
5401                         }
5402                         break;
5403                 case OP_BLOBCONST:
5404                 {
5405                         size_t lsize, rsize;
5406                         lsize = size_of(state, left->type);
5407                         rsize = size_of(state, right->type);
5408                         if (lsize != rsize) {
5409                                 break;
5410                         }
5411                         if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5412                                 equal = 1;
5413                         }
5414                         break;
5415                 }
5416                 case OP_ADDRCONST:
5417                         if ((MISC(left, 0) == MISC(right, 0)) &&
5418                                 (left->u.cval == right->u.cval)) {
5419                                 equal = 1;
5420                         }
5421                         break;
5422                 default:
5423                         internal_error(state, left, "uknown constant type");
5424                         break;
5425                 }
5426         }
5427         return equal;
5428 }
5429
5430 static int is_zero(struct triple *ins)
5431 {
5432         return is_const(ins) && (ins->u.cval == 0);
5433 }
5434
5435 static int is_one(struct triple *ins)
5436 {
5437         return is_const(ins) && (ins->u.cval == 1);
5438 }
5439
5440 static long_t bsr(ulong_t value)
5441 {
5442         int i;
5443         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5444                 ulong_t mask;
5445                 mask = 1;
5446                 mask <<= i;
5447                 if (value & mask) {
5448                         return i;
5449                 }
5450         }
5451         return -1;
5452 }
5453
5454 static long_t bsf(ulong_t value)
5455 {
5456         int i;
5457         for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5458                 ulong_t mask;
5459                 mask = 1;
5460                 mask <<= 1;
5461                 if (value & mask) {
5462                         return i;
5463                 }
5464         }
5465         return -1;
5466 }
5467
5468 static long_t log2(ulong_t value)
5469 {
5470         return bsr(value);
5471 }
5472
5473 static long_t tlog2(struct triple *ins)
5474 {
5475         return log2(ins->u.cval);
5476 }
5477
5478 static int is_pow2(struct triple *ins)
5479 {
5480         ulong_t value, mask;
5481         long_t log;
5482         if (!is_const(ins)) {
5483                 return 0;
5484         }
5485         value = ins->u.cval;
5486         log = log2(value);
5487         if (log == -1) {
5488                 return 0;
5489         }
5490         mask = 1;
5491         mask <<= log;
5492         return  ((value & mask) == value);
5493 }
5494
5495 static ulong_t read_const(struct compile_state *state,
5496         struct triple *ins, struct triple **expr)
5497 {
5498         struct triple *rhs;
5499         rhs = *expr;
5500         switch(rhs->type->type &TYPE_MASK) {
5501         case TYPE_CHAR:   
5502         case TYPE_SHORT:
5503         case TYPE_INT:
5504         case TYPE_LONG:
5505         case TYPE_UCHAR:   
5506         case TYPE_USHORT:  
5507         case TYPE_UINT:
5508         case TYPE_ULONG:
5509         case TYPE_POINTER:
5510                 break;
5511         default:
5512                 internal_error(state, rhs, "bad type to read_const\n");
5513                 break;
5514         }
5515         return rhs->u.cval;
5516 }
5517
5518 static long_t read_sconst(struct triple *ins, struct triple **expr)
5519 {
5520         struct triple *rhs;
5521         rhs = *expr;
5522         return (long_t)(rhs->u.cval);
5523 }
5524
5525 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5526 {
5527         struct triple **expr;
5528         expr = triple_rhs(state, ins, 0);
5529         for(;expr;expr = triple_rhs(state, ins, expr)) {
5530                 if (*expr) {
5531                         unuse_triple(*expr, ins);
5532                         *expr = 0;
5533                 }
5534         }
5535 }
5536
5537 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5538 {
5539         struct triple **expr;
5540         expr = triple_lhs(state, ins, 0);
5541         for(;expr;expr = triple_lhs(state, ins, expr)) {
5542                 unuse_triple(*expr, ins);
5543                 *expr = 0;
5544         }
5545 }
5546
5547 static void check_lhs(struct compile_state *state, struct triple *ins)
5548 {
5549         struct triple **expr;
5550         expr = triple_lhs(state, ins, 0);
5551         for(;expr;expr = triple_lhs(state, ins, expr)) {
5552                 internal_error(state, ins, "unexpected lhs");
5553         }
5554         
5555 }
5556 static void check_targ(struct compile_state *state, struct triple *ins)
5557 {
5558         struct triple **expr;
5559         expr = triple_targ(state, ins, 0);
5560         for(;expr;expr = triple_targ(state, ins, expr)) {
5561                 internal_error(state, ins, "unexpected targ");
5562         }
5563 }
5564
5565 static void wipe_ins(struct compile_state *state, struct triple *ins)
5566 {
5567         /* Becareful which instructions you replace the wiped
5568          * instruction with, as there are not enough slots
5569          * in all instructions to hold all others.
5570          */
5571         check_targ(state, ins);
5572         unuse_rhs(state, ins);
5573         unuse_lhs(state, ins);
5574 }
5575
5576 static void mkcopy(struct compile_state *state, 
5577         struct triple *ins, struct triple *rhs)
5578 {
5579         wipe_ins(state, ins);
5580         ins->op = OP_COPY;
5581         ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5582         RHS(ins, 0) = rhs;
5583         use_triple(RHS(ins, 0), ins);
5584 }
5585
5586 static void mkconst(struct compile_state *state, 
5587         struct triple *ins, ulong_t value)
5588 {
5589         if (!is_integral(ins) && !is_pointer(ins)) {
5590                 internal_error(state, ins, "unknown type to make constant\n");
5591         }
5592         wipe_ins(state, ins);
5593         ins->op = OP_INTCONST;
5594         ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5595         ins->u.cval = value;
5596 }
5597
5598 static void mkaddr_const(struct compile_state *state,
5599         struct triple *ins, struct triple *sdecl, ulong_t value)
5600 {
5601         wipe_ins(state, ins);
5602         ins->op = OP_ADDRCONST;
5603         ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5604         MISC(ins, 0) = sdecl;
5605         ins->u.cval = value;
5606         use_triple(sdecl, ins);
5607 }
5608
5609 /* Transform multicomponent variables into simple register variables */
5610 static void flatten_structures(struct compile_state *state)
5611 {
5612         struct triple *ins, *first;
5613         first = RHS(state->main_function, 0);
5614         ins = first;
5615         /* Pass one expand structure values into valvecs.
5616          */
5617         ins = first;
5618         do {
5619                 struct triple *next;
5620                 next = ins->next;
5621                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5622                         if (ins->op == OP_VAL_VEC) {
5623                                 /* Do nothing */
5624                         }
5625                         else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5626                                 struct triple *def, **vector;
5627                                 struct type *tptr;
5628                                 int op;
5629                                 ulong_t i;
5630
5631                                 op = ins->op;
5632                                 def = RHS(ins, 0);
5633                                 get_occurance(ins->occurance);
5634                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5635                                         ins->occurance);
5636
5637                                 vector = &RHS(next, 0);
5638                                 tptr = next->type->left;
5639                                 for(i = 0; i < next->type->elements; i++) {
5640                                         struct triple *sfield;
5641                                         struct type *mtype;
5642                                         mtype = tptr;
5643                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5644                                                 mtype = mtype->left;
5645                                         }
5646                                         sfield = deref_field(state, def, mtype->field_ident);
5647                                         
5648                                         vector[i] = triple(
5649                                                 state, op, mtype, sfield, 0);
5650                                         put_occurance(vector[i]->occurance);
5651                                         get_occurance(next->occurance);
5652                                         vector[i]->occurance = next->occurance;
5653                                         tptr = tptr->right;
5654                                 }
5655                                 propogate_use(state, ins, next);
5656                                 flatten(state, ins, next);
5657                                 free_triple(state, ins);
5658                         }
5659                         else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5660                                 struct triple *src, *dst, **vector;
5661                                 struct type *tptr;
5662                                 int op;
5663                                 ulong_t i;
5664
5665                                 op = ins->op;
5666                                 src = RHS(ins, 0);
5667                                 dst = LHS(ins, 0);
5668                                 get_occurance(ins->occurance);
5669                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5670                                         ins->occurance);
5671                                 
5672                                 vector = &RHS(next, 0);
5673                                 tptr = next->type->left;
5674                                 for(i = 0; i < ins->type->elements; i++) {
5675                                         struct triple *dfield, *sfield;
5676                                         struct type *mtype;
5677                                         mtype = tptr;
5678                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5679                                                 mtype = mtype->left;
5680                                         }
5681                                         sfield = deref_field(state, src, mtype->field_ident);
5682                                         dfield = deref_field(state, dst, mtype->field_ident);
5683                                         vector[i] = triple(
5684                                                 state, op, mtype, dfield, sfield);
5685                                         put_occurance(vector[i]->occurance);
5686                                         get_occurance(next->occurance);
5687                                         vector[i]->occurance = next->occurance;
5688                                         tptr = tptr->right;
5689                                 }
5690                                 propogate_use(state, ins, next);
5691                                 flatten(state, ins, next);
5692                                 free_triple(state, ins);
5693                         }
5694                 }
5695                 ins = next;
5696         } while(ins != first);
5697         /* Pass two flatten the valvecs.
5698          */
5699         ins = first;
5700         do {
5701                 struct triple *next;
5702                 next = ins->next;
5703                 if (ins->op == OP_VAL_VEC) {
5704                         release_triple(state, ins);
5705                 } 
5706                 ins = next;
5707         } while(ins != first);
5708         /* Pass three verify the state and set ->id to 0.
5709          */
5710         ins = first;
5711         do {
5712                 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5713                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5714                         internal_error(state, ins, "STRUCT_TYPE remains?");
5715                 }
5716                 if (ins->op == OP_DOT) {
5717                         internal_error(state, ins, "OP_DOT remains?");
5718                 }
5719                 if (ins->op == OP_VAL_VEC) {
5720                         internal_error(state, ins, "OP_VAL_VEC remains?");
5721                 }
5722                 ins = ins->next;
5723         } while(ins != first);
5724 }
5725
5726 /* For those operations that cannot be simplified */
5727 static void simplify_noop(struct compile_state *state, struct triple *ins)
5728 {
5729         return;
5730 }
5731
5732 static void simplify_smul(struct compile_state *state, struct triple *ins)
5733 {
5734         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5735                 struct triple *tmp;
5736                 tmp = RHS(ins, 0);
5737                 RHS(ins, 0) = RHS(ins, 1);
5738                 RHS(ins, 1) = tmp;
5739         }
5740         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5741                 long_t left, right;
5742                 left  = read_sconst(ins, &RHS(ins, 0));
5743                 right = read_sconst(ins, &RHS(ins, 1));
5744                 mkconst(state, ins, left * right);
5745         }
5746         else if (is_zero(RHS(ins, 1))) {
5747                 mkconst(state, ins, 0);
5748         }
5749         else if (is_one(RHS(ins, 1))) {
5750                 mkcopy(state, ins, RHS(ins, 0));
5751         }
5752         else if (is_pow2(RHS(ins, 1))) {
5753                 struct triple *val;
5754                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5755                 ins->op = OP_SL;
5756                 insert_triple(state, ins, val);
5757                 unuse_triple(RHS(ins, 1), ins);
5758                 use_triple(val, ins);
5759                 RHS(ins, 1) = val;
5760         }
5761 }
5762
5763 static void simplify_umul(struct compile_state *state, struct triple *ins)
5764 {
5765         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5766                 struct triple *tmp;
5767                 tmp = RHS(ins, 0);
5768                 RHS(ins, 0) = RHS(ins, 1);
5769                 RHS(ins, 1) = tmp;
5770         }
5771         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5772                 ulong_t left, right;
5773                 left  = read_const(state, ins, &RHS(ins, 0));
5774                 right = read_const(state, ins, &RHS(ins, 1));
5775                 mkconst(state, ins, left * right);
5776         }
5777         else if (is_zero(RHS(ins, 1))) {
5778                 mkconst(state, ins, 0);
5779         }
5780         else if (is_one(RHS(ins, 1))) {
5781                 mkcopy(state, ins, RHS(ins, 0));
5782         }
5783         else if (is_pow2(RHS(ins, 1))) {
5784                 struct triple *val;
5785                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5786                 ins->op = OP_SL;
5787                 insert_triple(state, ins, val);
5788                 unuse_triple(RHS(ins, 1), ins);
5789                 use_triple(val, ins);
5790                 RHS(ins, 1) = val;
5791         }
5792 }
5793
5794 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5795 {
5796         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5797                 long_t left, right;
5798                 left  = read_sconst(ins, &RHS(ins, 0));
5799                 right = read_sconst(ins, &RHS(ins, 1));
5800                 mkconst(state, ins, left / right);
5801         }
5802         else if (is_zero(RHS(ins, 0))) {
5803                 mkconst(state, ins, 0);
5804         }
5805         else if (is_zero(RHS(ins, 1))) {
5806                 error(state, ins, "division by zero");
5807         }
5808         else if (is_one(RHS(ins, 1))) {
5809                 mkcopy(state, ins, RHS(ins, 0));
5810         }
5811         else if (is_pow2(RHS(ins, 1))) {
5812                 struct triple *val;
5813                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5814                 ins->op = OP_SSR;
5815                 insert_triple(state, ins, val);
5816                 unuse_triple(RHS(ins, 1), ins);
5817                 use_triple(val, ins);
5818                 RHS(ins, 1) = val;
5819         }
5820 }
5821
5822 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5823 {
5824         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5825                 ulong_t left, right;
5826                 left  = read_const(state, ins, &RHS(ins, 0));
5827                 right = read_const(state, ins, &RHS(ins, 1));
5828                 mkconst(state, ins, left / right);
5829         }
5830         else if (is_zero(RHS(ins, 0))) {
5831                 mkconst(state, ins, 0);
5832         }
5833         else if (is_zero(RHS(ins, 1))) {
5834                 error(state, ins, "division by zero");
5835         }
5836         else if (is_one(RHS(ins, 1))) {
5837                 mkcopy(state, ins, RHS(ins, 0));
5838         }
5839         else if (is_pow2(RHS(ins, 1))) {
5840                 struct triple *val;
5841                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5842                 ins->op = OP_USR;
5843                 insert_triple(state, ins, val);
5844                 unuse_triple(RHS(ins, 1), ins);
5845                 use_triple(val, ins);
5846                 RHS(ins, 1) = val;
5847         }
5848 }
5849
5850 static void simplify_smod(struct compile_state *state, struct triple *ins)
5851 {
5852         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5853                 long_t left, right;
5854                 left  = read_const(state, ins, &RHS(ins, 0));
5855                 right = read_const(state, ins, &RHS(ins, 1));
5856                 mkconst(state, ins, left % right);
5857         }
5858         else if (is_zero(RHS(ins, 0))) {
5859                 mkconst(state, ins, 0);
5860         }
5861         else if (is_zero(RHS(ins, 1))) {
5862                 error(state, ins, "division by zero");
5863         }
5864         else if (is_one(RHS(ins, 1))) {
5865                 mkconst(state, ins, 0);
5866         }
5867         else if (is_pow2(RHS(ins, 1))) {
5868                 struct triple *val;
5869                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5870                 ins->op = OP_AND;
5871                 insert_triple(state, ins, val);
5872                 unuse_triple(RHS(ins, 1), ins);
5873                 use_triple(val, ins);
5874                 RHS(ins, 1) = val;
5875         }
5876 }
5877 static void simplify_umod(struct compile_state *state, struct triple *ins)
5878 {
5879         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5880                 ulong_t left, right;
5881                 left  = read_const(state, ins, &RHS(ins, 0));
5882                 right = read_const(state, ins, &RHS(ins, 1));
5883                 mkconst(state, ins, left % right);
5884         }
5885         else if (is_zero(RHS(ins, 0))) {
5886                 mkconst(state, ins, 0);
5887         }
5888         else if (is_zero(RHS(ins, 1))) {
5889                 error(state, ins, "division by zero");
5890         }
5891         else if (is_one(RHS(ins, 1))) {
5892                 mkconst(state, ins, 0);
5893         }
5894         else if (is_pow2(RHS(ins, 1))) {
5895                 struct triple *val;
5896                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
5897                 ins->op = OP_AND;
5898                 insert_triple(state, ins, val);
5899                 unuse_triple(RHS(ins, 1), ins);
5900                 use_triple(val, ins);
5901                 RHS(ins, 1) = val;
5902         }
5903 }
5904
5905 static void simplify_add(struct compile_state *state, struct triple *ins)
5906 {
5907         /* start with the pointer on the left */
5908         if (is_pointer(RHS(ins, 1))) {
5909                 struct triple *tmp;
5910                 tmp = RHS(ins, 0);
5911                 RHS(ins, 0) = RHS(ins, 1);
5912                 RHS(ins, 1) = tmp;
5913         }
5914         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5915                 if (!is_pointer(RHS(ins, 0))) {
5916                         ulong_t left, right;
5917                         left  = read_const(state, ins, &RHS(ins, 0));
5918                         right = read_const(state, ins, &RHS(ins, 1));
5919                         mkconst(state, ins, left + right);
5920                 }
5921                 else /* op == OP_ADDRCONST */ {
5922                         struct triple *sdecl;
5923                         ulong_t left, right;
5924                         sdecl = MISC(RHS(ins, 0), 0);
5925                         left  = RHS(ins, 0)->u.cval;
5926                         right = RHS(ins, 1)->u.cval;
5927                         mkaddr_const(state, ins, sdecl, left + right);
5928                 }
5929         }
5930         else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5931                 struct triple *tmp;
5932                 tmp = RHS(ins, 1);
5933                 RHS(ins, 1) = RHS(ins, 0);
5934                 RHS(ins, 0) = tmp;
5935         }
5936 }
5937
5938 static void simplify_sub(struct compile_state *state, struct triple *ins)
5939 {
5940         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5941                 if (!is_pointer(RHS(ins, 0))) {
5942                         ulong_t left, right;
5943                         left  = read_const(state, ins, &RHS(ins, 0));
5944                         right = read_const(state, ins, &RHS(ins, 1));
5945                         mkconst(state, ins, left - right);
5946                 }
5947                 else /* op == OP_ADDRCONST */ {
5948                         struct triple *sdecl;
5949                         ulong_t left, right;
5950                         sdecl = MISC(RHS(ins, 0), 0);
5951                         left  = RHS(ins, 0)->u.cval;
5952                         right = RHS(ins, 1)->u.cval;
5953                         mkaddr_const(state, ins, sdecl, left - right);
5954                 }
5955         }
5956 }
5957
5958 static void simplify_sl(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, "left 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_usr(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                 ulong_t left, right;
5986                 left  = read_const(state, ins, &RHS(ins, 0));
5987                 right = read_const(state, ins, &RHS(ins, 1));
5988                 mkconst(state, ins, left >> right);
5989         }
5990 }
5991
5992 static void simplify_ssr(struct compile_state *state, struct triple *ins)
5993 {
5994         if (is_const(RHS(ins, 1))) {
5995                 ulong_t right;
5996                 right = read_const(state, ins, &RHS(ins, 1));
5997                 if (right >= (size_of(state, ins->type)*8)) {
5998                         warning(state, ins, "right shift count >= width of type");
5999                 }
6000         }
6001         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6002                 long_t left, right;
6003                 left  = read_sconst(ins, &RHS(ins, 0));
6004                 right = read_sconst(ins, &RHS(ins, 1));
6005                 mkconst(state, ins, left >> right);
6006         }
6007 }
6008
6009 static void simplify_and(struct compile_state *state, struct triple *ins)
6010 {
6011         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6012                 ulong_t left, right;
6013                 left  = read_const(state, ins, &RHS(ins, 0));
6014                 right = read_const(state, ins, &RHS(ins, 1));
6015                 mkconst(state, ins, left & right);
6016         }
6017 }
6018
6019 static void simplify_or(struct compile_state *state, struct triple *ins)
6020 {
6021         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6022                 ulong_t left, right;
6023                 left  = read_const(state, ins, &RHS(ins, 0));
6024                 right = read_const(state, ins, &RHS(ins, 1));
6025                 mkconst(state, ins, left | right);
6026         }
6027 }
6028
6029 static void simplify_xor(struct compile_state *state, struct triple *ins)
6030 {
6031         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6032                 ulong_t left, right;
6033                 left  = read_const(state, ins, &RHS(ins, 0));
6034                 right = read_const(state, ins, &RHS(ins, 1));
6035                 mkconst(state, ins, left ^ right);
6036         }
6037 }
6038
6039 static void simplify_pos(struct compile_state *state, struct triple *ins)
6040 {
6041         if (is_const(RHS(ins, 0))) {
6042                 mkconst(state, ins, RHS(ins, 0)->u.cval);
6043         }
6044         else {
6045                 mkcopy(state, ins, RHS(ins, 0));
6046         }
6047 }
6048
6049 static void simplify_neg(struct compile_state *state, struct triple *ins)
6050 {
6051         if (is_const(RHS(ins, 0))) {
6052                 ulong_t left;
6053                 left = read_const(state, ins, &RHS(ins, 0));
6054                 mkconst(state, ins, -left);
6055         }
6056         else if (RHS(ins, 0)->op == OP_NEG) {
6057                 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
6058         }
6059 }
6060
6061 static void simplify_invert(struct compile_state *state, struct triple *ins)
6062 {
6063         if (is_const(RHS(ins, 0))) {
6064                 ulong_t left;
6065                 left = read_const(state, ins, &RHS(ins, 0));
6066                 mkconst(state, ins, ~left);
6067         }
6068 }
6069
6070 static void simplify_eq(struct compile_state *state, struct triple *ins)
6071 {
6072         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6073                 ulong_t left, right;
6074                 left  = read_const(state, ins, &RHS(ins, 0));
6075                 right = read_const(state, ins, &RHS(ins, 1));
6076                 mkconst(state, ins, left == right);
6077         }
6078         else if (RHS(ins, 0) == RHS(ins, 1)) {
6079                 mkconst(state, ins, 1);
6080         }
6081 }
6082
6083 static void simplify_noteq(struct compile_state *state, struct triple *ins)
6084 {
6085         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6086                 ulong_t left, right;
6087                 left  = read_const(state, ins, &RHS(ins, 0));
6088                 right = read_const(state, ins, &RHS(ins, 1));
6089                 mkconst(state, ins, left != right);
6090         }
6091         else if (RHS(ins, 0) == RHS(ins, 1)) {
6092                 mkconst(state, ins, 0);
6093         }
6094 }
6095
6096 static void simplify_sless(struct compile_state *state, struct triple *ins)
6097 {
6098         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6099                 long_t left, right;
6100                 left  = read_sconst(ins, &RHS(ins, 0));
6101                 right = read_sconst(ins, &RHS(ins, 1));
6102                 mkconst(state, ins, left < right);
6103         }
6104         else if (RHS(ins, 0) == RHS(ins, 1)) {
6105                 mkconst(state, ins, 0);
6106         }
6107 }
6108
6109 static void simplify_uless(struct compile_state *state, struct triple *ins)
6110 {
6111         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6112                 ulong_t left, right;
6113                 left  = read_const(state, ins, &RHS(ins, 0));
6114                 right = read_const(state, ins, &RHS(ins, 1));
6115                 mkconst(state, ins, left < right);
6116         }
6117         else if (is_zero(RHS(ins, 0))) {
6118                 mkconst(state, ins, 1);
6119         }
6120         else if (RHS(ins, 0) == RHS(ins, 1)) {
6121                 mkconst(state, ins, 0);
6122         }
6123 }
6124
6125 static void simplify_smore(struct compile_state *state, struct triple *ins)
6126 {
6127         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6128                 long_t left, right;
6129                 left  = read_sconst(ins, &RHS(ins, 0));
6130                 right = read_sconst(ins, &RHS(ins, 1));
6131                 mkconst(state, ins, left > right);
6132         }
6133         else if (RHS(ins, 0) == RHS(ins, 1)) {
6134                 mkconst(state, ins, 0);
6135         }
6136 }
6137
6138 static void simplify_umore(struct compile_state *state, struct triple *ins)
6139 {
6140         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6141                 ulong_t left, right;
6142                 left  = read_const(state, ins, &RHS(ins, 0));
6143                 right = read_const(state, ins, &RHS(ins, 1));
6144                 mkconst(state, ins, left > right);
6145         }
6146         else if (is_zero(RHS(ins, 1))) {
6147                 mkconst(state, ins, 1);
6148         }
6149         else if (RHS(ins, 0) == RHS(ins, 1)) {
6150                 mkconst(state, ins, 0);
6151         }
6152 }
6153
6154
6155 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6156 {
6157         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6158                 long_t left, right;
6159                 left  = read_sconst(ins, &RHS(ins, 0));
6160                 right = read_sconst(ins, &RHS(ins, 1));
6161                 mkconst(state, ins, left <= right);
6162         }
6163         else if (RHS(ins, 0) == RHS(ins, 1)) {
6164                 mkconst(state, ins, 1);
6165         }
6166 }
6167
6168 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6169 {
6170         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6171                 ulong_t left, right;
6172                 left  = read_const(state, ins, &RHS(ins, 0));
6173                 right = read_const(state, ins, &RHS(ins, 1));
6174                 mkconst(state, ins, left <= right);
6175         }
6176         else if (is_zero(RHS(ins, 0))) {
6177                 mkconst(state, ins, 1);
6178         }
6179         else if (RHS(ins, 0) == RHS(ins, 1)) {
6180                 mkconst(state, ins, 1);
6181         }
6182 }
6183
6184 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6185 {
6186         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
6187                 long_t left, right;
6188                 left  = read_sconst(ins, &RHS(ins, 0));
6189                 right = read_sconst(ins, &RHS(ins, 1));
6190                 mkconst(state, ins, left >= right);
6191         }
6192         else if (RHS(ins, 0) == RHS(ins, 1)) {
6193                 mkconst(state, ins, 1);
6194         }
6195 }
6196
6197 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6198 {
6199         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6200                 ulong_t left, right;
6201                 left  = read_const(state, ins, &RHS(ins, 0));
6202                 right = read_const(state, ins, &RHS(ins, 1));
6203                 mkconst(state, ins, left >= right);
6204         }
6205         else if (is_zero(RHS(ins, 1))) {
6206                 mkconst(state, ins, 1);
6207         }
6208         else if (RHS(ins, 0) == RHS(ins, 1)) {
6209                 mkconst(state, ins, 1);
6210         }
6211 }
6212
6213 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6214 {
6215         if (is_const(RHS(ins, 0))) {
6216                 ulong_t left;
6217                 left = read_const(state, ins, &RHS(ins, 0));
6218                 mkconst(state, ins, left == 0);
6219         }
6220         /* Otherwise if I am the only user... */
6221         else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
6222                 int need_copy = 1;
6223                 /* Invert a boolean operation */
6224                 switch(RHS(ins, 0)->op) {
6225                 case OP_LTRUE:   RHS(ins, 0)->op = OP_LFALSE;  break;
6226                 case OP_LFALSE:  RHS(ins, 0)->op = OP_LTRUE;   break;
6227                 case OP_EQ:      RHS(ins, 0)->op = OP_NOTEQ;   break;
6228                 case OP_NOTEQ:   RHS(ins, 0)->op = OP_EQ;      break;
6229                 case OP_SLESS:   RHS(ins, 0)->op = OP_SMOREEQ; break;
6230                 case OP_ULESS:   RHS(ins, 0)->op = OP_UMOREEQ; break;
6231                 case OP_SMORE:   RHS(ins, 0)->op = OP_SLESSEQ; break;
6232                 case OP_UMORE:   RHS(ins, 0)->op = OP_ULESSEQ; break;
6233                 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE;   break;
6234                 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE;   break;
6235                 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS;   break;
6236                 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS;   break;
6237                 default:
6238                         need_copy = 0;
6239                         break;
6240                 }
6241                 if (need_copy) {
6242                         mkcopy(state, ins, RHS(ins, 0));
6243                 }
6244         }
6245 }
6246
6247 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6248 {
6249         if (is_const(RHS(ins, 0))) {
6250                 ulong_t left;
6251                 left = read_const(state, ins, &RHS(ins, 0));
6252                 mkconst(state, ins, left != 0);
6253         }
6254         else switch(RHS(ins, 0)->op) {
6255         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
6256         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
6257         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
6258                 mkcopy(state, ins, RHS(ins, 0));
6259         }
6260
6261 }
6262
6263 static void simplify_copy(struct compile_state *state, struct triple *ins)
6264 {
6265         if (is_const(RHS(ins, 0))) {
6266                 switch(RHS(ins, 0)->op) {
6267                 case OP_INTCONST:
6268                 {
6269                         ulong_t left;
6270                         left = read_const(state, ins, &RHS(ins, 0));
6271                         mkconst(state, ins, left);
6272                         break;
6273                 }
6274                 case OP_ADDRCONST:
6275                 {
6276                         struct triple *sdecl;
6277                         ulong_t offset;
6278                         sdecl  = MISC(RHS(ins, 0), 0);
6279                         offset = RHS(ins, 0)->u.cval;
6280                         mkaddr_const(state, ins, sdecl, offset);
6281                         break;
6282                 }
6283                 default:
6284                         internal_error(state, ins, "uknown constant");
6285                         break;
6286                 }
6287         }
6288 }
6289
6290 static void simplify_branch(struct compile_state *state, struct triple *ins)
6291 {
6292         struct block *block;
6293         if (ins->op != OP_BRANCH) {
6294                 internal_error(state, ins, "not branch");
6295         }
6296         if (ins->use != 0) {
6297                 internal_error(state, ins, "branch use");
6298         }
6299 #warning "FIXME implement simplify branch."
6300         /* The challenge here with simplify branch is that I need to 
6301          * make modifications to the control flow graph as well
6302          * as to the branch instruction itself.
6303          */
6304         block = ins->u.block;
6305         
6306         if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6307                 struct triple *targ;
6308                 ulong_t value;
6309                 value = read_const(state, ins, &RHS(ins, 0));
6310                 unuse_triple(RHS(ins, 0), ins);
6311                 targ = TARG(ins, 0);
6312                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6313                 if (value) {
6314                         unuse_triple(ins->next, ins);
6315                         TARG(ins, 0) = targ;
6316                 }
6317                 else {
6318                         unuse_triple(targ, ins);
6319                         TARG(ins, 0) = ins->next;
6320                 }
6321 #warning "FIXME handle the case of making a branch unconditional"
6322         }
6323         if (TARG(ins, 0) == ins->next) {
6324                 unuse_triple(ins->next, ins);
6325                 if (TRIPLE_RHS(ins->sizes)) {
6326                         unuse_triple(RHS(ins, 0), ins);
6327                         unuse_triple(ins->next, ins);
6328                 }
6329                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6330                 ins->op = OP_NOOP;
6331                 if (ins->use) {
6332                         internal_error(state, ins, "noop use != 0");
6333                 }
6334 #warning "FIXME handle the case of killing a branch"
6335         }
6336 }
6337
6338 static void simplify_phi(struct compile_state *state, struct triple *ins)
6339 {
6340         struct triple **expr;
6341         ulong_t value;
6342         expr = triple_rhs(state, ins, 0);
6343         if (!*expr || !is_const(*expr)) {
6344                 return;
6345         }
6346         value = read_const(state, ins, expr);
6347         for(;expr;expr = triple_rhs(state, ins, expr)) {
6348                 if (!*expr || !is_const(*expr)) {
6349                         return;
6350                 }
6351                 if (value != read_const(state, ins, expr)) {
6352                         return;
6353                 }
6354         }
6355         mkconst(state, ins, value);
6356 }
6357
6358
6359 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6360 {
6361         if (is_const(RHS(ins, 0))) {
6362                 ulong_t left;
6363                 left = read_const(state, ins, &RHS(ins, 0));
6364                 mkconst(state, ins, bsf(left));
6365         }
6366 }
6367
6368 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6369 {
6370         if (is_const(RHS(ins, 0))) {
6371                 ulong_t left;
6372                 left = read_const(state, ins, &RHS(ins, 0));
6373                 mkconst(state, ins, bsr(left));
6374         }
6375 }
6376
6377
6378 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6379 static const simplify_t table_simplify[] = {
6380 #if 0
6381 #define simplify_smul     simplify_noop
6382 #define simplify_umul     simplify_noop
6383 #define simplify_sdiv     simplify_noop
6384 #define simplify_udiv     simplify_noop
6385 #define simplify_smod     simplify_noop
6386 #define simplify_umod     simplify_noop
6387 #endif
6388 #if 0
6389 #define simplify_add      simplify_noop
6390 #define simplify_sub      simplify_noop
6391 #endif
6392 #if 0
6393 #define simplify_sl       simplify_noop
6394 #define simplify_usr      simplify_noop
6395 #define simplify_ssr      simplify_noop
6396 #endif
6397 #if 0
6398 #define simplify_and      simplify_noop
6399 #define simplify_xor      simplify_noop
6400 #define simplify_or       simplify_noop
6401 #endif
6402 #if 0
6403 #define simplify_pos      simplify_noop
6404 #define simplify_neg      simplify_noop
6405 #define simplify_invert   simplify_noop
6406 #endif
6407
6408 #if 0
6409 #define simplify_eq       simplify_noop
6410 #define simplify_noteq    simplify_noop
6411 #endif
6412 #if 0
6413 #define simplify_sless    simplify_noop
6414 #define simplify_uless    simplify_noop
6415 #define simplify_smore    simplify_noop
6416 #define simplify_umore    simplify_noop
6417 #endif
6418 #if 0
6419 #define simplify_slesseq  simplify_noop
6420 #define simplify_ulesseq  simplify_noop
6421 #define simplify_smoreeq  simplify_noop
6422 #define simplify_umoreeq  simplify_noop
6423 #endif
6424 #if 0
6425 #define simplify_lfalse   simplify_noop
6426 #endif
6427 #if 0
6428 #define simplify_ltrue    simplify_noop
6429 #endif
6430
6431 #if 0
6432 #define simplify_copy     simplify_noop
6433 #endif
6434
6435 #if 0
6436 #define simplify_branch   simplify_noop
6437 #endif
6438
6439 #if 0
6440 #define simplify_phi      simplify_noop
6441 #endif
6442
6443 #if 0
6444 #define simplify_bsf      simplify_noop
6445 #define simplify_bsr      simplify_noop
6446 #endif
6447
6448 [OP_SMUL       ] = simplify_smul,
6449 [OP_UMUL       ] = simplify_umul,
6450 [OP_SDIV       ] = simplify_sdiv,
6451 [OP_UDIV       ] = simplify_udiv,
6452 [OP_SMOD       ] = simplify_smod,
6453 [OP_UMOD       ] = simplify_umod,
6454 [OP_ADD        ] = simplify_add,
6455 [OP_SUB        ] = simplify_sub,
6456 [OP_SL         ] = simplify_sl,
6457 [OP_USR        ] = simplify_usr,
6458 [OP_SSR        ] = simplify_ssr,
6459 [OP_AND        ] = simplify_and,
6460 [OP_XOR        ] = simplify_xor,
6461 [OP_OR         ] = simplify_or,
6462 [OP_POS        ] = simplify_pos,
6463 [OP_NEG        ] = simplify_neg,
6464 [OP_INVERT     ] = simplify_invert,
6465
6466 [OP_EQ         ] = simplify_eq,
6467 [OP_NOTEQ      ] = simplify_noteq,
6468 [OP_SLESS      ] = simplify_sless,
6469 [OP_ULESS      ] = simplify_uless,
6470 [OP_SMORE      ] = simplify_smore,
6471 [OP_UMORE      ] = simplify_umore,
6472 [OP_SLESSEQ    ] = simplify_slesseq,
6473 [OP_ULESSEQ    ] = simplify_ulesseq,
6474 [OP_SMOREEQ    ] = simplify_smoreeq,
6475 [OP_UMOREEQ    ] = simplify_umoreeq,
6476 [OP_LFALSE     ] = simplify_lfalse,
6477 [OP_LTRUE      ] = simplify_ltrue,
6478
6479 [OP_LOAD       ] = simplify_noop,
6480 [OP_STORE      ] = simplify_noop,
6481
6482 [OP_NOOP       ] = simplify_noop,
6483
6484 [OP_INTCONST   ] = simplify_noop,
6485 [OP_BLOBCONST  ] = simplify_noop,
6486 [OP_ADDRCONST  ] = simplify_noop,
6487
6488 [OP_WRITE      ] = simplify_noop,
6489 [OP_READ       ] = simplify_noop,
6490 [OP_COPY       ] = simplify_copy,
6491 [OP_PIECE      ] = simplify_noop,
6492 [OP_ASM        ] = simplify_noop,
6493
6494 [OP_DOT        ] = simplify_noop,
6495 [OP_VAL_VEC    ] = simplify_noop,
6496
6497 [OP_LIST       ] = simplify_noop,
6498 [OP_BRANCH     ] = simplify_branch,
6499 [OP_LABEL      ] = simplify_noop,
6500 [OP_ADECL      ] = simplify_noop,
6501 [OP_SDECL      ] = simplify_noop,
6502 [OP_PHI        ] = simplify_phi,
6503
6504 [OP_INB        ] = simplify_noop,
6505 [OP_INW        ] = simplify_noop,
6506 [OP_INL        ] = simplify_noop,
6507 [OP_OUTB       ] = simplify_noop,
6508 [OP_OUTW       ] = simplify_noop,
6509 [OP_OUTL       ] = simplify_noop,
6510 [OP_BSF        ] = simplify_bsf,
6511 [OP_BSR        ] = simplify_bsr,
6512 [OP_RDMSR      ] = simplify_noop,
6513 [OP_WRMSR      ] = simplify_noop,                    
6514 [OP_HLT        ] = simplify_noop,
6515 };
6516
6517 static void simplify(struct compile_state *state, struct triple *ins)
6518 {
6519         int op;
6520         simplify_t do_simplify;
6521         do {
6522                 op = ins->op;
6523                 do_simplify = 0;
6524                 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6525                         do_simplify = 0;
6526                 }
6527                 else {
6528                         do_simplify = table_simplify[op];
6529                 }
6530                 if (!do_simplify) {
6531                         internal_error(state, ins, "cannot simplify op: %d %s\n",
6532                                 op, tops(op));
6533                         return;
6534                 }
6535                 do_simplify(state, ins);
6536         } while(ins->op != op);
6537 }
6538
6539 static void simplify_all(struct compile_state *state)
6540 {
6541         struct triple *ins, *first;
6542         first = RHS(state->main_function, 0);
6543         ins = first;
6544         do {
6545                 simplify(state, ins);
6546                 ins = ins->next;
6547         } while(ins != first);
6548 }
6549
6550 /*
6551  * Builtins....
6552  * ============================
6553  */
6554
6555 static void register_builtin_function(struct compile_state *state,
6556         const char *name, int op, struct type *rtype, ...)
6557 {
6558         struct type *ftype, *atype, *param, **next;
6559         struct triple *def, *arg, *result, *work, *last, *first;
6560         struct hash_entry *ident;
6561         struct file_state file;
6562         int parameters;
6563         int name_len;
6564         va_list args;
6565         int i;
6566
6567         /* Dummy file state to get debug handling right */
6568         memset(&file, 0, sizeof(file));
6569         file.basename = "<built-in>";
6570         file.line = 1;
6571         file.report_line = 1;
6572         file.report_name = file.basename;
6573         file.prev = state->file;
6574         state->file = &file;
6575         state->function = name;
6576
6577         /* Find the Parameter count */
6578         valid_op(state, op);
6579         parameters = table_ops[op].rhs;
6580         if (parameters < 0 ) {
6581                 internal_error(state, 0, "Invalid builtin parameter count");
6582         }
6583
6584         /* Find the function type */
6585         ftype = new_type(TYPE_FUNCTION, rtype, 0);
6586         next = &ftype->right;
6587         va_start(args, rtype);
6588         for(i = 0; i < parameters; i++) {
6589                 atype = va_arg(args, struct type *);
6590                 if (!*next) {
6591                         *next = atype;
6592                 } else {
6593                         *next = new_type(TYPE_PRODUCT, *next, atype);
6594                         next = &((*next)->right);
6595                 }
6596         }
6597         if (!*next) {
6598                 *next = &void_type;
6599         }
6600         va_end(args);
6601
6602         /* Generate the needed triples */
6603         def = triple(state, OP_LIST, ftype, 0, 0);
6604         first = label(state);
6605         RHS(def, 0) = first;
6606
6607         /* Now string them together */
6608         param = ftype->right;
6609         for(i = 0; i < parameters; i++) {
6610                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6611                         atype = param->left;
6612                 } else {
6613                         atype = param;
6614                 }
6615                 arg = flatten(state, first, variable(state, atype));
6616                 param = param->right;
6617         }
6618         result = 0;
6619         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6620                 result = flatten(state, first, variable(state, rtype));
6621         }
6622         MISC(def, 0) = result;
6623         work = new_triple(state, op, rtype, -1, parameters);
6624         for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6625                 RHS(work, i) = read_expr(state, arg);
6626         }
6627         if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6628                 struct triple *val;
6629                 /* Populate the LHS with the target registers */
6630                 work = flatten(state, first, work);
6631                 work->type = &void_type;
6632                 param = rtype->left;
6633                 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6634                         internal_error(state, 0, "Invalid result type");
6635                 }
6636                 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6637                 for(i = 0; i < rtype->elements; i++) {
6638                         struct triple *piece;
6639                         atype = param;
6640                         if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6641                                 atype = param->left;
6642                         }
6643                         if (!TYPE_ARITHMETIC(atype->type) &&
6644                                 !TYPE_PTR(atype->type)) {
6645                                 internal_error(state, 0, "Invalid lhs type");
6646                         }
6647                         piece = triple(state, OP_PIECE, atype, work, 0);
6648                         piece->u.cval = i;
6649                         LHS(work, i) = piece;
6650                         RHS(val, i) = piece;
6651                 }
6652                 work = val;
6653         }
6654         if (result) {
6655                 work = write_expr(state, result, work);
6656         }
6657         work = flatten(state, first, work);
6658         last = flatten(state, first, label(state));
6659         name_len = strlen(name);
6660         ident = lookup(state, name, name_len);
6661         symbol(state, ident, &ident->sym_ident, def, ftype);
6662         
6663         state->file = file.prev;
6664         state->function = 0;
6665 #if 0
6666         fprintf(stdout, "\n");
6667         loc(stdout, state, 0);
6668         fprintf(stdout, "\n__________ builtin_function _________\n");
6669         print_triple(state, def);
6670         fprintf(stdout, "__________ builtin_function _________ done\n\n");
6671 #endif
6672 }
6673
6674 static struct type *partial_struct(struct compile_state *state,
6675         const char *field_name, struct type *type, struct type *rest)
6676 {
6677         struct hash_entry *field_ident;
6678         struct type *result;
6679         int field_name_len;
6680
6681         field_name_len = strlen(field_name);
6682         field_ident = lookup(state, field_name, field_name_len);
6683
6684         result = clone_type(0, type);
6685         result->field_ident = field_ident;
6686
6687         if (rest) {
6688                 result = new_type(TYPE_PRODUCT, result, rest);
6689         }
6690         return result;
6691 }
6692
6693 static struct type *register_builtin_type(struct compile_state *state,
6694         const char *name, struct type *type)
6695 {
6696         struct hash_entry *ident;
6697         int name_len;
6698
6699         name_len = strlen(name);
6700         ident = lookup(state, name, name_len);
6701         
6702         if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6703                 ulong_t elements = 0;
6704                 struct type *field;
6705                 type = new_type(TYPE_STRUCT, type, 0);
6706                 field = type->left;
6707                 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6708                         elements++;
6709                         field = field->right;
6710                 }
6711                 elements++;
6712                 symbol(state, ident, &ident->sym_struct, 0, type);
6713                 type->type_ident = ident;
6714                 type->elements = elements;
6715         }
6716         symbol(state, ident, &ident->sym_ident, 0, type);
6717         ident->tok = TOK_TYPE_NAME;
6718         return type;
6719 }
6720
6721
6722 static void register_builtins(struct compile_state *state)
6723 {
6724         struct type *msr_type;
6725
6726         register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type, 
6727                 &ushort_type);
6728         register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6729                 &ushort_type);
6730         register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,   
6731                 &ushort_type);
6732
6733         register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, 
6734                 &uchar_type, &ushort_type);
6735         register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, 
6736                 &ushort_type, &ushort_type);
6737         register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, 
6738                 &uint_type, &ushort_type);
6739         
6740         register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, 
6741                 &int_type);
6742         register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, 
6743                 &int_type);
6744
6745         msr_type = register_builtin_type(state, "__builtin_msr_t",
6746                 partial_struct(state, "lo", &ulong_type,
6747                 partial_struct(state, "hi", &ulong_type, 0)));
6748
6749         register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6750                 &ulong_type);
6751         register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6752                 &ulong_type, &ulong_type, &ulong_type);
6753         
6754         register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, 
6755                 &void_type);
6756 }
6757
6758 static struct type *declarator(
6759         struct compile_state *state, struct type *type, 
6760         struct hash_entry **ident, int need_ident);
6761 static void decl(struct compile_state *state, struct triple *first);
6762 static struct type *specifier_qualifier_list(struct compile_state *state);
6763 static int isdecl_specifier(int tok);
6764 static struct type *decl_specifiers(struct compile_state *state);
6765 static int istype(int tok);
6766 static struct triple *expr(struct compile_state *state);
6767 static struct triple *assignment_expr(struct compile_state *state);
6768 static struct type *type_name(struct compile_state *state);
6769 static void statement(struct compile_state *state, struct triple *fist);
6770
6771 static struct triple *call_expr(
6772         struct compile_state *state, struct triple *func)
6773 {
6774         struct triple *def;
6775         struct type *param, *type;
6776         ulong_t pvals, index;
6777
6778         if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
6779                 error(state, 0, "Called object is not a function");
6780         }
6781         if (func->op != OP_LIST) {
6782                 internal_error(state, 0, "improper function");
6783         }
6784         eat(state, TOK_LPAREN);
6785         /* Find the return type without any specifiers */
6786         type = clone_type(0, func->type->left);
6787         def = new_triple(state, OP_CALL, func->type, -1, -1);
6788         def->type = type;
6789
6790         pvals = TRIPLE_RHS(def->sizes);
6791         MISC(def, 0) = func;
6792
6793         param = func->type->right;
6794         for(index = 0; index < pvals; index++) {
6795                 struct triple *val;
6796                 struct type *arg_type;
6797                 val = read_expr(state, assignment_expr(state));
6798                 arg_type = param;
6799                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6800                         arg_type = param->left;
6801                 }
6802                 write_compatible(state, arg_type, val->type);
6803                 RHS(def, index) = val;
6804                 if (index != (pvals - 1)) {
6805                         eat(state, TOK_COMMA);
6806                         param = param->right;
6807                 }
6808         }
6809         eat(state, TOK_RPAREN);
6810         return def;
6811 }
6812
6813
6814 static struct triple *character_constant(struct compile_state *state)
6815 {
6816         struct triple *def;
6817         struct token *tk;
6818         const signed char *str, *end;
6819         int c;
6820         int str_len;
6821         eat(state, TOK_LIT_CHAR);
6822         tk = &state->token[0];
6823         str = tk->val.str + 1;
6824         str_len = tk->str_len - 2;
6825         if (str_len <= 0) {
6826                 error(state, 0, "empty character constant");
6827         }
6828         end = str + str_len;
6829         c = char_value(state, &str, end);
6830         if (str != end) {
6831                 error(state, 0, "multibyte character constant not supported");
6832         }
6833         def = int_const(state, &char_type, (ulong_t)((long_t)c));
6834         return def;
6835 }
6836
6837 static struct triple *string_constant(struct compile_state *state)
6838 {
6839         struct triple *def;
6840         struct token *tk;
6841         struct type *type;
6842         const signed char *str, *end;
6843         signed char *buf, *ptr;
6844         int str_len;
6845
6846         buf = 0;
6847         type = new_type(TYPE_ARRAY, &char_type, 0);
6848         type->elements = 0;
6849         /* The while loop handles string concatenation */
6850         do {
6851                 eat(state, TOK_LIT_STRING);
6852                 tk = &state->token[0];
6853                 str = tk->val.str + 1;
6854                 str_len = tk->str_len - 2;
6855                 if (str_len < 0) {
6856                         error(state, 0, "negative string constant length");
6857                 }
6858                 end = str + str_len;
6859                 ptr = buf;
6860                 buf = xmalloc(type->elements + str_len + 1, "string_constant");
6861                 memcpy(buf, ptr, type->elements);
6862                 ptr = buf + type->elements;
6863                 do {
6864                         *ptr++ = char_value(state, &str, end);
6865                 } while(str < end);
6866                 type->elements = ptr - buf;
6867         } while(peek(state) == TOK_LIT_STRING);
6868         *ptr = '\0';
6869         type->elements += 1;
6870         def = triple(state, OP_BLOBCONST, type, 0, 0);
6871         def->u.blob = buf;
6872         return def;
6873 }
6874
6875
6876 static struct triple *integer_constant(struct compile_state *state)
6877 {
6878         struct triple *def;
6879         unsigned long val;
6880         struct token *tk;
6881         char *end;
6882         int u, l, decimal;
6883         struct type *type;
6884
6885         eat(state, TOK_LIT_INT);
6886         tk = &state->token[0];
6887         errno = 0;
6888         decimal = (tk->val.str[0] != '0');
6889         val = strtoul(tk->val.str, &end, 0);
6890         if ((val == ULONG_MAX) && (errno == ERANGE)) {
6891                 error(state, 0, "Integer constant to large");
6892         }
6893         u = l = 0;
6894         if ((*end == 'u') || (*end == 'U')) {
6895                 u = 1;
6896                         end++;
6897         }
6898         if ((*end == 'l') || (*end == 'L')) {
6899                 l = 1;
6900                 end++;
6901         }
6902         if ((*end == 'u') || (*end == 'U')) {
6903                 u = 1;
6904                 end++;
6905         }
6906         if (*end) {
6907                 error(state, 0, "Junk at end of integer constant");
6908         }
6909         if (u && l)  {
6910                 type = &ulong_type;
6911         }
6912         else if (l) {
6913                 type = &long_type;
6914                 if (!decimal && (val > LONG_MAX)) {
6915                         type = &ulong_type;
6916                 }
6917         }
6918         else if (u) {
6919                 type = &uint_type;
6920                 if (val > UINT_MAX) {
6921                         type = &ulong_type;
6922                 }
6923         }
6924         else {
6925                 type = &int_type;
6926                 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
6927                         type = &uint_type;
6928                 }
6929                 else if (!decimal && (val > LONG_MAX)) {
6930                         type = &ulong_type;
6931                 }
6932                 else if (val > INT_MAX) {
6933                         type = &long_type;
6934                 }
6935         }
6936         def = int_const(state, type, val);
6937         return def;
6938 }
6939
6940 static struct triple *primary_expr(struct compile_state *state)
6941 {
6942         struct triple *def;
6943         int tok;
6944         tok = peek(state);
6945         switch(tok) {
6946         case TOK_IDENT:
6947         {
6948                 struct hash_entry *ident;
6949                 /* Here ident is either:
6950                  * a varable name
6951                  * a function name
6952                  * an enumeration constant.
6953                  */
6954                 eat(state, TOK_IDENT);
6955                 ident = state->token[0].ident;
6956                 if (!ident->sym_ident) {
6957                         error(state, 0, "%s undeclared", ident->name);
6958                 }
6959                 def = ident->sym_ident->def;
6960                 break;
6961         }
6962         case TOK_ENUM_CONST:
6963                 /* Here ident is an enumeration constant */
6964                 eat(state, TOK_ENUM_CONST);
6965                 def = 0;
6966                 FINISHME();
6967                 break;
6968         case TOK_LPAREN:
6969                 eat(state, TOK_LPAREN);
6970                 def = expr(state);
6971                 eat(state, TOK_RPAREN);
6972                 break;
6973         case TOK_LIT_INT:
6974                 def = integer_constant(state);
6975                 break;
6976         case TOK_LIT_FLOAT:
6977                 eat(state, TOK_LIT_FLOAT);
6978                 error(state, 0, "Floating point constants not supported");
6979                 def = 0;
6980                 FINISHME();
6981                 break;
6982         case TOK_LIT_CHAR:
6983                 def = character_constant(state);
6984                 break;
6985         case TOK_LIT_STRING:
6986                 def = string_constant(state);
6987                 break;
6988         default:
6989                 def = 0;
6990                 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
6991         }
6992         return def;
6993 }
6994
6995 static struct triple *postfix_expr(struct compile_state *state)
6996 {
6997         struct triple *def;
6998         int postfix;
6999         def = primary_expr(state);
7000         do {
7001                 struct triple *left;
7002                 int tok;
7003                 postfix = 1;
7004                 left = def;
7005                 switch((tok = peek(state))) {
7006                 case TOK_LBRACKET:
7007                         eat(state, TOK_LBRACKET);
7008                         def = mk_subscript_expr(state, left, expr(state));
7009                         eat(state, TOK_RBRACKET);
7010                         break;
7011                 case TOK_LPAREN:
7012                         def = call_expr(state, def);
7013                         break;
7014                 case TOK_DOT:
7015                 {
7016                         struct hash_entry *field;
7017                         eat(state, TOK_DOT);
7018                         eat(state, TOK_IDENT);
7019                         field = state->token[0].ident;
7020                         def = deref_field(state, def, field);
7021                         break;
7022                 }
7023                 case TOK_ARROW:
7024                 {
7025                         struct hash_entry *field;
7026                         eat(state, TOK_ARROW);
7027                         eat(state, TOK_IDENT);
7028                         field = state->token[0].ident;
7029                         def = mk_deref_expr(state, read_expr(state, def));
7030                         def = deref_field(state, def, field);
7031                         break;
7032                 }
7033                 case TOK_PLUSPLUS:
7034                         eat(state, TOK_PLUSPLUS);
7035                         def = mk_post_inc_expr(state, left);
7036                         break;
7037                 case TOK_MINUSMINUS:
7038                         eat(state, TOK_MINUSMINUS);
7039                         def = mk_post_dec_expr(state, left);
7040                         break;
7041                 default:
7042                         postfix = 0;
7043                         break;
7044                 }
7045         } while(postfix);
7046         return def;
7047 }
7048
7049 static struct triple *cast_expr(struct compile_state *state);
7050
7051 static struct triple *unary_expr(struct compile_state *state)
7052 {
7053         struct triple *def, *right;
7054         int tok;
7055         switch((tok = peek(state))) {
7056         case TOK_PLUSPLUS:
7057                 eat(state, TOK_PLUSPLUS);
7058                 def = mk_pre_inc_expr(state, unary_expr(state));
7059                 break;
7060         case TOK_MINUSMINUS:
7061                 eat(state, TOK_MINUSMINUS);
7062                 def = mk_pre_dec_expr(state, unary_expr(state));
7063                 break;
7064         case TOK_AND:
7065                 eat(state, TOK_AND);
7066                 def = mk_addr_expr(state, cast_expr(state), 0);
7067                 break;
7068         case TOK_STAR:
7069                 eat(state, TOK_STAR);
7070                 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7071                 break;
7072         case TOK_PLUS:
7073                 eat(state, TOK_PLUS);
7074                 right = read_expr(state, cast_expr(state));
7075                 arithmetic(state, right);
7076                 def = integral_promotion(state, right);
7077                 break;
7078         case TOK_MINUS:
7079                 eat(state, TOK_MINUS);
7080                 right = read_expr(state, cast_expr(state));
7081                 arithmetic(state, right);
7082                 def = integral_promotion(state, right);
7083                 def = triple(state, OP_NEG, def->type, def, 0);
7084                 break;
7085         case TOK_TILDE:
7086                 eat(state, TOK_TILDE);
7087                 right = read_expr(state, cast_expr(state));
7088                 integral(state, right);
7089                 def = integral_promotion(state, right);
7090                 def = triple(state, OP_INVERT, def->type, def, 0);
7091                 break;
7092         case TOK_BANG:
7093                 eat(state, TOK_BANG);
7094                 right = read_expr(state, cast_expr(state));
7095                 bool(state, right);
7096                 def = lfalse_expr(state, right);
7097                 break;
7098         case TOK_SIZEOF:
7099         {
7100                 struct type *type;
7101                 int tok1, tok2;
7102                 eat(state, TOK_SIZEOF);
7103                 tok1 = peek(state);
7104                 tok2 = peek2(state);
7105                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7106                         eat(state, TOK_LPAREN);
7107                         type = type_name(state);
7108                         eat(state, TOK_RPAREN);
7109                 }
7110                 else {
7111                         struct triple *expr;
7112                         expr = unary_expr(state);
7113                         type = expr->type;
7114                         release_expr(state, expr);
7115                 }
7116                 def = int_const(state, &ulong_type, size_of(state, type));
7117                 break;
7118         }
7119         case TOK_ALIGNOF:
7120         {
7121                 struct type *type;
7122                 int tok1, tok2;
7123                 eat(state, TOK_ALIGNOF);
7124                 tok1 = peek(state);
7125                 tok2 = peek2(state);
7126                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7127                         eat(state, TOK_LPAREN);
7128                         type = type_name(state);
7129                         eat(state, TOK_RPAREN);
7130                 }
7131                 else {
7132                         struct triple *expr;
7133                         expr = unary_expr(state);
7134                         type = expr->type;
7135                         release_expr(state, expr);
7136                 }
7137                 def = int_const(state, &ulong_type, align_of(state, type));
7138                 break;
7139         }
7140         default:
7141                 def = postfix_expr(state);
7142                 break;
7143         }
7144         return def;
7145 }
7146
7147 static struct triple *cast_expr(struct compile_state *state)
7148 {
7149         struct triple *def;
7150         int tok1, tok2;
7151         tok1 = peek(state);
7152         tok2 = peek2(state);
7153         if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7154                 struct type *type;
7155                 eat(state, TOK_LPAREN);
7156                 type = type_name(state);
7157                 eat(state, TOK_RPAREN);
7158                 def = read_expr(state, cast_expr(state));
7159                 def = triple(state, OP_COPY, type, def, 0);
7160         }
7161         else {
7162                 def = unary_expr(state);
7163         }
7164         return def;
7165 }
7166
7167 static struct triple *mult_expr(struct compile_state *state)
7168 {
7169         struct triple *def;
7170         int done;
7171         def = cast_expr(state);
7172         do {
7173                 struct triple *left, *right;
7174                 struct type *result_type;
7175                 int tok, op, sign;
7176                 done = 0;
7177                 switch(tok = (peek(state))) {
7178                 case TOK_STAR:
7179                 case TOK_DIV:
7180                 case TOK_MOD:
7181                         left = read_expr(state, def);
7182                         arithmetic(state, left);
7183
7184                         eat(state, tok);
7185
7186                         right = read_expr(state, cast_expr(state));
7187                         arithmetic(state, right);
7188
7189                         result_type = arithmetic_result(state, left, right);
7190                         sign = is_signed(result_type);
7191                         op = -1;
7192                         switch(tok) {
7193                         case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7194                         case TOK_DIV:  op = sign? OP_SDIV : OP_UDIV; break;
7195                         case TOK_MOD:  op = sign? OP_SMOD : OP_UMOD; break;
7196                         }
7197                         def = triple(state, op, result_type, left, right);
7198                         break;
7199                 default:
7200                         done = 1;
7201                         break;
7202                 }
7203         } while(!done);
7204         return def;
7205 }
7206
7207 static struct triple *add_expr(struct compile_state *state)
7208 {
7209         struct triple *def;
7210         int done;
7211         def = mult_expr(state);
7212         do {
7213                 done = 0;
7214                 switch( peek(state)) {
7215                 case TOK_PLUS:
7216                         eat(state, TOK_PLUS);
7217                         def = mk_add_expr(state, def, mult_expr(state));
7218                         break;
7219                 case TOK_MINUS:
7220                         eat(state, TOK_MINUS);
7221                         def = mk_sub_expr(state, def, mult_expr(state));
7222                         break;
7223                 default:
7224                         done = 1;
7225                         break;
7226                 }
7227         } while(!done);
7228         return def;
7229 }
7230
7231 static struct triple *shift_expr(struct compile_state *state)
7232 {
7233         struct triple *def;
7234         int done;
7235         def = add_expr(state);
7236         do {
7237                 struct triple *left, *right;
7238                 int tok, op;
7239                 done = 0;
7240                 switch((tok = peek(state))) {
7241                 case TOK_SL:
7242                 case TOK_SR:
7243                         left = read_expr(state, def);
7244                         integral(state, left);
7245                         left = integral_promotion(state, left);
7246
7247                         eat(state, tok);
7248
7249                         right = read_expr(state, add_expr(state));
7250                         integral(state, right);
7251                         right = integral_promotion(state, right);
7252                         
7253                         op = (tok == TOK_SL)? OP_SL : 
7254                                 is_signed(left->type)? OP_SSR: OP_USR;
7255
7256                         def = triple(state, op, left->type, left, right);
7257                         break;
7258                 default:
7259                         done = 1;
7260                         break;
7261                 }
7262         } while(!done);
7263         return def;
7264 }
7265
7266 static struct triple *relational_expr(struct compile_state *state)
7267 {
7268 #warning "Extend relational exprs to work on more than arithmetic types"
7269         struct triple *def;
7270         int done;
7271         def = shift_expr(state);
7272         do {
7273                 struct triple *left, *right;
7274                 struct type *arg_type;
7275                 int tok, op, sign;
7276                 done = 0;
7277                 switch((tok = peek(state))) {
7278                 case TOK_LESS:
7279                 case TOK_MORE:
7280                 case TOK_LESSEQ:
7281                 case TOK_MOREEQ:
7282                         left = read_expr(state, def);
7283                         arithmetic(state, left);
7284
7285                         eat(state, tok);
7286
7287                         right = read_expr(state, shift_expr(state));
7288                         arithmetic(state, right);
7289
7290                         arg_type = arithmetic_result(state, left, right);
7291                         sign = is_signed(arg_type);
7292                         op = -1;
7293                         switch(tok) {
7294                         case TOK_LESS:   op = sign? OP_SLESS : OP_ULESS; break;
7295                         case TOK_MORE:   op = sign? OP_SMORE : OP_UMORE; break;
7296                         case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7297                         case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7298                         }
7299                         def = triple(state, op, &int_type, left, right);
7300                         break;
7301                 default:
7302                         done = 1;
7303                         break;
7304                 }
7305         } while(!done);
7306         return def;
7307 }
7308
7309 static struct triple *equality_expr(struct compile_state *state)
7310 {
7311 #warning "Extend equality exprs to work on more than arithmetic types"
7312         struct triple *def;
7313         int done;
7314         def = relational_expr(state);
7315         do {
7316                 struct triple *left, *right;
7317                 int tok, op;
7318                 done = 0;
7319                 switch((tok = peek(state))) {
7320                 case TOK_EQEQ:
7321                 case TOK_NOTEQ:
7322                         left = read_expr(state, def);
7323                         arithmetic(state, left);
7324                         eat(state, tok);
7325                         right = read_expr(state, relational_expr(state));
7326                         arithmetic(state, right);
7327                         op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7328                         def = triple(state, op, &int_type, left, right);
7329                         break;
7330                 default:
7331                         done = 1;
7332                         break;
7333                 }
7334         } while(!done);
7335         return def;
7336 }
7337
7338 static struct triple *and_expr(struct compile_state *state)
7339 {
7340         struct triple *def;
7341         def = equality_expr(state);
7342         while(peek(state) == TOK_AND) {
7343                 struct triple *left, *right;
7344                 struct type *result_type;
7345                 left = read_expr(state, def);
7346                 integral(state, left);
7347                 eat(state, TOK_AND);
7348                 right = read_expr(state, equality_expr(state));
7349                 integral(state, right);
7350                 result_type = arithmetic_result(state, left, right);
7351                 def = triple(state, OP_AND, result_type, left, right);
7352         }
7353         return def;
7354 }
7355
7356 static struct triple *xor_expr(struct compile_state *state)
7357 {
7358         struct triple *def;
7359         def = and_expr(state);
7360         while(peek(state) == TOK_XOR) {
7361                 struct triple *left, *right;
7362                 struct type *result_type;
7363                 left = read_expr(state, def);
7364                 integral(state, left);
7365                 eat(state, TOK_XOR);
7366                 right = read_expr(state, and_expr(state));
7367                 integral(state, right);
7368                 result_type = arithmetic_result(state, left, right);
7369                 def = triple(state, OP_XOR, result_type, left, right);
7370         }
7371         return def;
7372 }
7373
7374 static struct triple *or_expr(struct compile_state *state)
7375 {
7376         struct triple *def;
7377         def = xor_expr(state);
7378         while(peek(state) == TOK_OR) {
7379                 struct triple *left, *right;
7380                 struct type *result_type;
7381                 left = read_expr(state, def);
7382                 integral(state, left);
7383                 eat(state, TOK_OR);
7384                 right = read_expr(state, xor_expr(state));
7385                 integral(state, right);
7386                 result_type = arithmetic_result(state, left, right);
7387                 def = triple(state, OP_OR, result_type, left, right);
7388         }
7389         return def;
7390 }
7391
7392 static struct triple *land_expr(struct compile_state *state)
7393 {
7394         struct triple *def;
7395         def = or_expr(state);
7396         while(peek(state) == TOK_LOGAND) {
7397                 struct triple *left, *right;
7398                 left = read_expr(state, def);
7399                 bool(state, left);
7400                 eat(state, TOK_LOGAND);
7401                 right = read_expr(state, or_expr(state));
7402                 bool(state, right);
7403
7404                 def = triple(state, OP_LAND, &int_type,
7405                         ltrue_expr(state, left),
7406                         ltrue_expr(state, right));
7407         }
7408         return def;
7409 }
7410
7411 static struct triple *lor_expr(struct compile_state *state)
7412 {
7413         struct triple *def;
7414         def = land_expr(state);
7415         while(peek(state) == TOK_LOGOR) {
7416                 struct triple *left, *right;
7417                 left = read_expr(state, def);
7418                 bool(state, left);
7419                 eat(state, TOK_LOGOR);
7420                 right = read_expr(state, land_expr(state));
7421                 bool(state, right);
7422                 
7423                 def = triple(state, OP_LOR, &int_type,
7424                         ltrue_expr(state, left),
7425                         ltrue_expr(state, right));
7426         }
7427         return def;
7428 }
7429
7430 static struct triple *conditional_expr(struct compile_state *state)
7431 {
7432         struct triple *def;
7433         def = lor_expr(state);
7434         if (peek(state) == TOK_QUEST) {
7435                 struct triple *test, *left, *right;
7436                 bool(state, def);
7437                 test = ltrue_expr(state, read_expr(state, def));
7438                 eat(state, TOK_QUEST);
7439                 left = read_expr(state, expr(state));
7440                 eat(state, TOK_COLON);
7441                 right = read_expr(state, conditional_expr(state));
7442
7443                 def = cond_expr(state, test, left, right);
7444         }
7445         return def;
7446 }
7447
7448 static struct triple *eval_const_expr(
7449         struct compile_state *state, struct triple *expr)
7450 {
7451         struct triple *def;
7452         if (is_const(expr)) {
7453                 def = expr;
7454         } 
7455         else {
7456                 /* If we don't start out as a constant simplify into one */
7457                 struct triple *head, *ptr;
7458                 head = label(state); /* dummy initial triple */
7459                 flatten(state, head, expr);
7460                 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7461                         simplify(state, ptr);
7462                 }
7463                 /* Remove the constant value the tail of the list */
7464                 def = head->prev;
7465                 def->prev->next = def->next;
7466                 def->next->prev = def->prev;
7467                 def->next = def->prev = def;
7468                 if (!is_const(def)) {
7469                         error(state, 0, "Not a constant expression");
7470                 }
7471                 /* Free the intermediate expressions */
7472                 while(head->next != head) {
7473                         release_triple(state, head->next);
7474                 }
7475                 free_triple(state, head);
7476         }
7477         return def;
7478 }
7479
7480 static struct triple *constant_expr(struct compile_state *state)
7481 {
7482         return eval_const_expr(state, conditional_expr(state));
7483 }
7484
7485 static struct triple *assignment_expr(struct compile_state *state)
7486 {
7487         struct triple *def, *left, *right;
7488         int tok, op, sign;
7489         /* The C grammer in K&R shows assignment expressions
7490          * only taking unary expressions as input on their
7491          * left hand side.  But specifies the precedence of
7492          * assignemnt as the lowest operator except for comma.
7493          *
7494          * Allowing conditional expressions on the left hand side
7495          * of an assignement results in a grammar that accepts
7496          * a larger set of statements than standard C.   As long
7497          * as the subset of the grammar that is standard C behaves
7498          * correctly this should cause no problems.
7499          * 
7500          * For the extra token strings accepted by the grammar
7501          * none of them should produce a valid lvalue, so they
7502          * should not produce functioning programs.
7503          *
7504          * GCC has this bug as well, so surprises should be minimal.
7505          */
7506         def = conditional_expr(state);
7507         left = def;
7508         switch((tok = peek(state))) {
7509         case TOK_EQ:
7510                 lvalue(state, left);
7511                 eat(state, TOK_EQ);
7512                 def = write_expr(state, left, 
7513                         read_expr(state, assignment_expr(state)));
7514                 break;
7515         case TOK_TIMESEQ:
7516         case TOK_DIVEQ:
7517         case TOK_MODEQ:
7518                 lvalue(state, left);
7519                 arithmetic(state, left);
7520                 eat(state, tok);
7521                 right = read_expr(state, assignment_expr(state));
7522                 arithmetic(state, right);
7523
7524                 sign = is_signed(left->type);
7525                 op = -1;
7526                 switch(tok) {
7527                 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7528                 case TOK_DIVEQ:   op = sign? OP_SDIV : OP_UDIV; break;
7529                 case TOK_MODEQ:   op = sign? OP_SMOD : OP_UMOD; break;
7530                 }
7531                 def = write_expr(state, left,
7532                         triple(state, op, left->type, 
7533                                 read_expr(state, left), right));
7534                 break;
7535         case TOK_PLUSEQ:
7536                 lvalue(state, left);
7537                 eat(state, TOK_PLUSEQ);
7538                 def = write_expr(state, left,
7539                         mk_add_expr(state, left, assignment_expr(state)));
7540                 break;
7541         case TOK_MINUSEQ:
7542                 lvalue(state, left);
7543                 eat(state, TOK_MINUSEQ);
7544                 def = write_expr(state, left,
7545                         mk_sub_expr(state, left, assignment_expr(state)));
7546                 break;
7547         case TOK_SLEQ:
7548         case TOK_SREQ:
7549         case TOK_ANDEQ:
7550         case TOK_XOREQ:
7551         case TOK_OREQ:
7552                 lvalue(state, left);
7553                 integral(state, left);
7554                 eat(state, tok);
7555                 right = read_expr(state, assignment_expr(state));
7556                 integral(state, right);
7557                 right = integral_promotion(state, right);
7558                 sign = is_signed(left->type);
7559                 op = -1;
7560                 switch(tok) {
7561                 case TOK_SLEQ:  op = OP_SL; break;
7562                 case TOK_SREQ:  op = sign? OP_SSR: OP_USR; break;
7563                 case TOK_ANDEQ: op = OP_AND; break;
7564                 case TOK_XOREQ: op = OP_XOR; break;
7565                 case TOK_OREQ:  op = OP_OR; break;
7566                 }
7567                 def = write_expr(state, left,
7568                         triple(state, op, left->type, 
7569                                 read_expr(state, left), right));
7570                 break;
7571         }
7572         return def;
7573 }
7574
7575 static struct triple *expr(struct compile_state *state)
7576 {
7577         struct triple *def;
7578         def = assignment_expr(state);
7579         while(peek(state) == TOK_COMMA) {
7580                 struct triple *left, *right;
7581                 left = def;
7582                 eat(state, TOK_COMMA);
7583                 right = assignment_expr(state);
7584                 def = triple(state, OP_COMMA, right->type, left, right);
7585         }
7586         return def;
7587 }
7588
7589 static void expr_statement(struct compile_state *state, struct triple *first)
7590 {
7591         if (peek(state) != TOK_SEMI) {
7592                 flatten(state, first, expr(state));
7593         }
7594         eat(state, TOK_SEMI);
7595 }
7596
7597 static void if_statement(struct compile_state *state, struct triple *first)
7598 {
7599         struct triple *test, *jmp1, *jmp2, *middle, *end;
7600
7601         jmp1 = jmp2 = middle = 0;
7602         eat(state, TOK_IF);
7603         eat(state, TOK_LPAREN);
7604         test = expr(state);
7605         bool(state, test);
7606         /* Cleanup and invert the test */
7607         test = lfalse_expr(state, read_expr(state, test));
7608         eat(state, TOK_RPAREN);
7609         /* Generate the needed pieces */
7610         middle = label(state);
7611         jmp1 = branch(state, middle, test);
7612         /* Thread the pieces together */
7613         flatten(state, first, test);
7614         flatten(state, first, jmp1);
7615         flatten(state, first, label(state));
7616         statement(state, first);
7617         if (peek(state) == TOK_ELSE) {
7618                 eat(state, TOK_ELSE);
7619                 /* Generate the rest of the pieces */
7620                 end = label(state);
7621                 jmp2 = branch(state, end, 0);
7622                 /* Thread them together */
7623                 flatten(state, first, jmp2);
7624                 flatten(state, first, middle);
7625                 statement(state, first);
7626                 flatten(state, first, end);
7627         }
7628         else {
7629                 flatten(state, first, middle);
7630         }
7631 }
7632
7633 static void for_statement(struct compile_state *state, struct triple *first)
7634 {
7635         struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7636         struct triple *label1, *label2, *label3;
7637         struct hash_entry *ident;
7638
7639         eat(state, TOK_FOR);
7640         eat(state, TOK_LPAREN);
7641         head = test = tail = jmp1 = jmp2 = 0;
7642         if (peek(state) != TOK_SEMI) {
7643                 head = expr(state);
7644         } 
7645         eat(state, TOK_SEMI);
7646         if (peek(state) != TOK_SEMI) {
7647                 test = expr(state);
7648                 bool(state, test);
7649                 test = ltrue_expr(state, read_expr(state, test));
7650         }
7651         eat(state, TOK_SEMI);
7652         if (peek(state) != TOK_RPAREN) {
7653                 tail = expr(state);
7654         }
7655         eat(state, TOK_RPAREN);
7656         /* Generate the needed pieces */
7657         label1 = label(state);
7658         label2 = label(state);
7659         label3 = label(state);
7660         if (test) {
7661                 jmp1 = branch(state, label3, 0);
7662                 jmp2 = branch(state, label1, test);
7663         }
7664         else {
7665                 jmp2 = branch(state, label1, 0);
7666         }
7667         end = label(state);
7668         /* Remember where break and continue go */
7669         start_scope(state);
7670         ident = state->i_break;
7671         symbol(state, ident, &ident->sym_ident, end, end->type);
7672         ident = state->i_continue;
7673         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7674         /* Now include the body */
7675         flatten(state, first, head);
7676         flatten(state, first, jmp1);
7677         flatten(state, first, label1);
7678         statement(state, first);
7679         flatten(state, first, label2);
7680         flatten(state, first, tail);
7681         flatten(state, first, label3);
7682         flatten(state, first, test);
7683         flatten(state, first, jmp2);
7684         flatten(state, first, end);
7685         /* Cleanup the break/continue scope */
7686         end_scope(state);
7687 }
7688
7689 static void while_statement(struct compile_state *state, struct triple *first)
7690 {
7691         struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7692         struct hash_entry *ident;
7693         eat(state, TOK_WHILE);
7694         eat(state, TOK_LPAREN);
7695         test = expr(state);
7696         bool(state, test);
7697         test = ltrue_expr(state, read_expr(state, test));
7698         eat(state, TOK_RPAREN);
7699         /* Generate the needed pieces */
7700         label1 = label(state);
7701         label2 = label(state);
7702         jmp1 = branch(state, label2, 0);
7703         jmp2 = branch(state, label1, test);
7704         end = label(state);
7705         /* Remember where break and continue go */
7706         start_scope(state);
7707         ident = state->i_break;
7708         symbol(state, ident, &ident->sym_ident, end, end->type);
7709         ident = state->i_continue;
7710         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7711         /* Thread them together */
7712         flatten(state, first, jmp1);
7713         flatten(state, first, label1);
7714         statement(state, first);
7715         flatten(state, first, label2);
7716         flatten(state, first, test);
7717         flatten(state, first, jmp2);
7718         flatten(state, first, end);
7719         /* Cleanup the break/continue scope */
7720         end_scope(state);
7721 }
7722
7723 static void do_statement(struct compile_state *state, struct triple *first)
7724 {
7725         struct triple *label1, *label2, *test, *end;
7726         struct hash_entry *ident;
7727         eat(state, TOK_DO);
7728         /* Generate the needed pieces */
7729         label1 = label(state);
7730         label2 = label(state);
7731         end = label(state);
7732         /* Remember where break and continue go */
7733         start_scope(state);
7734         ident = state->i_break;
7735         symbol(state, ident, &ident->sym_ident, end, end->type);
7736         ident = state->i_continue;
7737         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7738         /* Now include the body */
7739         flatten(state, first, label1);
7740         statement(state, first);
7741         /* Cleanup the break/continue scope */
7742         end_scope(state);
7743         /* Eat the rest of the loop */
7744         eat(state, TOK_WHILE);
7745         eat(state, TOK_LPAREN);
7746         test = read_expr(state, expr(state));
7747         bool(state, test);
7748         eat(state, TOK_RPAREN);
7749         eat(state, TOK_SEMI);
7750         /* Thread the pieces together */
7751         test = ltrue_expr(state, test);
7752         flatten(state, first, label2);
7753         flatten(state, first, test);
7754         flatten(state, first, branch(state, label1, test));
7755         flatten(state, first, end);
7756 }
7757
7758
7759 static void return_statement(struct compile_state *state, struct triple *first)
7760 {
7761         struct triple *jmp, *mv, *dest, *var, *val;
7762         int last;
7763         eat(state, TOK_RETURN);
7764
7765 #warning "FIXME implement a more general excess branch elimination"
7766         val = 0;
7767         /* If we have a return value do some more work */
7768         if (peek(state) != TOK_SEMI) {
7769                 val = read_expr(state, expr(state));
7770         }
7771         eat(state, TOK_SEMI);
7772
7773         /* See if this last statement in a function */
7774         last = ((peek(state) == TOK_RBRACE) && 
7775                 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
7776
7777         /* Find the return variable */
7778         var = MISC(state->main_function, 0);
7779         /* Find the return destination */
7780         dest = RHS(state->main_function, 0)->prev;
7781         mv = jmp = 0;
7782         /* If needed generate a jump instruction */
7783         if (!last) {
7784                 jmp = branch(state, dest, 0);
7785         }
7786         /* If needed generate an assignment instruction */
7787         if (val) {
7788                 mv = write_expr(state, var, val);
7789         }
7790         /* Now put the code together */
7791         if (mv) {
7792                 flatten(state, first, mv);
7793                 flatten(state, first, jmp);
7794         }
7795         else if (jmp) {
7796                 flatten(state, first, jmp);
7797         }
7798 }
7799
7800 static void break_statement(struct compile_state *state, struct triple *first)
7801 {
7802         struct triple *dest;
7803         eat(state, TOK_BREAK);
7804         eat(state, TOK_SEMI);
7805         if (!state->i_break->sym_ident) {
7806                 error(state, 0, "break statement not within loop or switch");
7807         }
7808         dest = state->i_break->sym_ident->def;
7809         flatten(state, first, branch(state, dest, 0));
7810 }
7811
7812 static void continue_statement(struct compile_state *state, struct triple *first)
7813 {
7814         struct triple *dest;
7815         eat(state, TOK_CONTINUE);
7816         eat(state, TOK_SEMI);
7817         if (!state->i_continue->sym_ident) {
7818                 error(state, 0, "continue statement outside of a loop");
7819         }
7820         dest = state->i_continue->sym_ident->def;
7821         flatten(state, first, branch(state, dest, 0));
7822 }
7823
7824 static void goto_statement(struct compile_state *state, struct triple *first)
7825 {
7826         struct hash_entry *ident;
7827         eat(state, TOK_GOTO);
7828         eat(state, TOK_IDENT);
7829         ident = state->token[0].ident;
7830         if (!ident->sym_label) {
7831                 /* If this is a forward branch allocate the label now,
7832                  * it will be flattend in the appropriate location later.
7833                  */
7834                 struct triple *ins;
7835                 ins = label(state);
7836                 label_symbol(state, ident, ins);
7837         }
7838         eat(state, TOK_SEMI);
7839
7840         flatten(state, first, branch(state, ident->sym_label->def, 0));
7841 }
7842
7843 static void labeled_statement(struct compile_state *state, struct triple *first)
7844 {
7845         struct triple *ins;
7846         struct hash_entry *ident;
7847         eat(state, TOK_IDENT);
7848
7849         ident = state->token[0].ident;
7850         if (ident->sym_label && ident->sym_label->def) {
7851                 ins = ident->sym_label->def;
7852                 put_occurance(ins->occurance);
7853                 ins->occurance = new_occurance(state);
7854         }
7855         else {
7856                 ins = label(state);
7857                 label_symbol(state, ident, ins);
7858         }
7859         if (ins->id & TRIPLE_FLAG_FLATTENED) {
7860                 error(state, 0, "label %s already defined", ident->name);
7861         }
7862         flatten(state, first, ins);
7863
7864         eat(state, TOK_COLON);
7865         statement(state, first);
7866 }
7867
7868 static void switch_statement(struct compile_state *state, struct triple *first)
7869 {
7870         FINISHME();
7871         eat(state, TOK_SWITCH);
7872         eat(state, TOK_LPAREN);
7873         expr(state);
7874         eat(state, TOK_RPAREN);
7875         statement(state, first);
7876         error(state, 0, "switch statements are not implemented");
7877         FINISHME();
7878 }
7879
7880 static void case_statement(struct compile_state *state, struct triple *first)
7881 {
7882         FINISHME();
7883         eat(state, TOK_CASE);
7884         constant_expr(state);
7885         eat(state, TOK_COLON);
7886         statement(state, first);
7887         error(state, 0, "case statements are not implemented");
7888         FINISHME();
7889 }
7890
7891 static void default_statement(struct compile_state *state, struct triple *first)
7892 {
7893         FINISHME();
7894         eat(state, TOK_DEFAULT);
7895         eat(state, TOK_COLON);
7896         statement(state, first);
7897         error(state, 0, "default statements are not implemented");
7898         FINISHME();
7899 }
7900
7901 static void asm_statement(struct compile_state *state, struct triple *first)
7902 {
7903         struct asm_info *info;
7904         struct {
7905                 struct triple *constraint;
7906                 struct triple *expr;
7907         } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
7908         struct triple *def, *asm_str;
7909         int out, in, clobbers, more, colons, i;
7910
7911         eat(state, TOK_ASM);
7912         /* For now ignore the qualifiers */
7913         switch(peek(state)) {
7914         case TOK_CONST:
7915                 eat(state, TOK_CONST);
7916                 break;
7917         case TOK_VOLATILE:
7918                 eat(state, TOK_VOLATILE);
7919                 break;
7920         }
7921         eat(state, TOK_LPAREN);
7922         asm_str = string_constant(state);
7923
7924         colons = 0;
7925         out = in = clobbers = 0;
7926         /* Outputs */
7927         if ((colons == 0) && (peek(state) == TOK_COLON)) {
7928                 eat(state, TOK_COLON);
7929                 colons++;
7930                 more = (peek(state) == TOK_LIT_STRING);
7931                 while(more) {
7932                         struct triple *var;
7933                         struct triple *constraint;
7934                         char *str;
7935                         more = 0;
7936                         if (out > MAX_LHS) {
7937                                 error(state, 0, "Maximum output count exceeded.");
7938                         }
7939                         constraint = string_constant(state);
7940                         str = constraint->u.blob;
7941                         if (str[0] != '=') {
7942                                 error(state, 0, "Output constraint does not start with =");
7943                         }
7944                         constraint->u.blob = str + 1;
7945                         eat(state, TOK_LPAREN);
7946                         var = conditional_expr(state);
7947                         eat(state, TOK_RPAREN);
7948
7949                         lvalue(state, var);
7950                         out_param[out].constraint = constraint;
7951                         out_param[out].expr       = var;
7952                         if (peek(state) == TOK_COMMA) {
7953                                 eat(state, TOK_COMMA);
7954                                 more = 1;
7955                         }
7956                         out++;
7957                 }
7958         }
7959         /* Inputs */
7960         if ((colons == 1) && (peek(state) == TOK_COLON)) {
7961                 eat(state, TOK_COLON);
7962                 colons++;
7963                 more = (peek(state) == TOK_LIT_STRING);
7964                 while(more) {
7965                         struct triple *val;
7966                         struct triple *constraint;
7967                         char *str;
7968                         more = 0;
7969                         if (in > MAX_RHS) {
7970                                 error(state, 0, "Maximum input count exceeded.");
7971                         }
7972                         constraint = string_constant(state);
7973                         str = constraint->u.blob;
7974                         if (digitp(str[0] && str[1] == '\0')) {
7975                                 int val;
7976                                 val = digval(str[0]);
7977                                 if ((val < 0) || (val >= out)) {
7978                                         error(state, 0, "Invalid input constraint %d", val);
7979                                 }
7980                         }
7981                         eat(state, TOK_LPAREN);
7982                         val = conditional_expr(state);
7983                         eat(state, TOK_RPAREN);
7984
7985                         in_param[in].constraint = constraint;
7986                         in_param[in].expr       = val;
7987                         if (peek(state) == TOK_COMMA) {
7988                                 eat(state, TOK_COMMA);
7989                                 more = 1;
7990                         }
7991                         in++;
7992                 }
7993         }
7994
7995         /* Clobber */
7996         if ((colons == 2) && (peek(state) == TOK_COLON)) {
7997                 eat(state, TOK_COLON);
7998                 colons++;
7999                 more = (peek(state) == TOK_LIT_STRING);
8000                 while(more) {
8001                         struct triple *clobber;
8002                         more = 0;
8003                         if ((clobbers + out) > MAX_LHS) {
8004                                 error(state, 0, "Maximum clobber limit exceeded.");
8005                         }
8006                         clobber = string_constant(state);
8007                         eat(state, TOK_RPAREN);
8008
8009                         clob_param[clobbers].constraint = clobber;
8010                         if (peek(state) == TOK_COMMA) {
8011                                 eat(state, TOK_COMMA);
8012                                 more = 1;
8013                         }
8014                         clobbers++;
8015                 }
8016         }
8017         eat(state, TOK_RPAREN);
8018         eat(state, TOK_SEMI);
8019
8020
8021         info = xcmalloc(sizeof(*info), "asm_info");
8022         info->str = asm_str->u.blob;
8023         free_triple(state, asm_str);
8024
8025         def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8026         def->u.ainfo = info;
8027
8028         /* Find the register constraints */
8029         for(i = 0; i < out; i++) {
8030                 struct triple *constraint;
8031                 constraint = out_param[i].constraint;
8032                 info->tmpl.lhs[i] = arch_reg_constraint(state, 
8033                         out_param[i].expr->type, constraint->u.blob);
8034                 free_triple(state, constraint);
8035         }
8036         for(; i - out < clobbers; i++) {
8037                 struct triple *constraint;
8038                 constraint = clob_param[i - out].constraint;
8039                 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8040                 free_triple(state, constraint);
8041         }
8042         for(i = 0; i < in; i++) {
8043                 struct triple *constraint;
8044                 const char *str;
8045                 constraint = in_param[i].constraint;
8046                 str = constraint->u.blob;
8047                 if (digitp(str[0]) && str[1] == '\0') {
8048                         struct reg_info cinfo;
8049                         int val;
8050                         val = digval(str[0]);
8051                         cinfo.reg = info->tmpl.lhs[val].reg;
8052                         cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8053                         cinfo.regcm &= info->tmpl.lhs[val].regcm;
8054                         if (cinfo.reg == REG_UNSET) {
8055                                 cinfo.reg = REG_VIRT0 + val;
8056                         }
8057                         if (cinfo.regcm == 0) {
8058                                 error(state, 0, "No registers for %d", val);
8059                         }
8060                         info->tmpl.lhs[val] = cinfo;
8061                         info->tmpl.rhs[i]   = cinfo;
8062                                 
8063                 } else {
8064                         info->tmpl.rhs[i] = arch_reg_constraint(state, 
8065                                 in_param[i].expr->type, str);
8066                 }
8067                 free_triple(state, constraint);
8068         }
8069
8070         /* Now build the helper expressions */
8071         for(i = 0; i < in; i++) {
8072                 RHS(def, i) = read_expr(state,in_param[i].expr);
8073         }
8074         flatten(state, first, def);
8075         for(i = 0; i < out; i++) {
8076                 struct triple *piece;
8077                 piece = triple(state, OP_PIECE, out_param[i].expr->type, def, 0);
8078                 piece->u.cval = i;
8079                 LHS(def, i) = piece;
8080                 flatten(state, first,
8081                         write_expr(state, out_param[i].expr, piece));
8082         }
8083         for(; i - out < clobbers; i++) {
8084                 struct triple *piece;
8085                 piece = triple(state, OP_PIECE, &void_type, def, 0);
8086                 piece->u.cval = i;
8087                 LHS(def, i) = piece;
8088                 flatten(state, first, piece);
8089         }
8090 }
8091
8092
8093 static int isdecl(int tok)
8094 {
8095         switch(tok) {
8096         case TOK_AUTO:
8097         case TOK_REGISTER:
8098         case TOK_STATIC:
8099         case TOK_EXTERN:
8100         case TOK_TYPEDEF:
8101         case TOK_CONST:
8102         case TOK_RESTRICT:
8103         case TOK_VOLATILE:
8104         case TOK_VOID:
8105         case TOK_CHAR:
8106         case TOK_SHORT:
8107         case TOK_INT:
8108         case TOK_LONG:
8109         case TOK_FLOAT:
8110         case TOK_DOUBLE:
8111         case TOK_SIGNED:
8112         case TOK_UNSIGNED:
8113         case TOK_STRUCT:
8114         case TOK_UNION:
8115         case TOK_ENUM:
8116         case TOK_TYPE_NAME: /* typedef name */
8117                 return 1;
8118         default:
8119                 return 0;
8120         }
8121 }
8122
8123 static void compound_statement(struct compile_state *state, struct triple *first)
8124 {
8125         eat(state, TOK_LBRACE);
8126         start_scope(state);
8127
8128         /* statement-list opt */
8129         while (peek(state) != TOK_RBRACE) {
8130                 statement(state, first);
8131         }
8132         end_scope(state);
8133         eat(state, TOK_RBRACE);
8134 }
8135
8136 static void statement(struct compile_state *state, struct triple *first)
8137 {
8138         int tok;
8139         tok = peek(state);
8140         if (tok == TOK_LBRACE) {
8141                 compound_statement(state, first);
8142         }
8143         else if (tok == TOK_IF) {
8144                 if_statement(state, first); 
8145         }
8146         else if (tok == TOK_FOR) {
8147                 for_statement(state, first);
8148         }
8149         else if (tok == TOK_WHILE) {
8150                 while_statement(state, first);
8151         }
8152         else if (tok == TOK_DO) {
8153                 do_statement(state, first);
8154         }
8155         else if (tok == TOK_RETURN) {
8156                 return_statement(state, first);
8157         }
8158         else if (tok == TOK_BREAK) {
8159                 break_statement(state, first);
8160         }
8161         else if (tok == TOK_CONTINUE) {
8162                 continue_statement(state, first);
8163         }
8164         else if (tok == TOK_GOTO) {
8165                 goto_statement(state, first);
8166         }
8167         else if (tok == TOK_SWITCH) {
8168                 switch_statement(state, first);
8169         }
8170         else if (tok == TOK_ASM) {
8171                 asm_statement(state, first);
8172         }
8173         else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8174                 labeled_statement(state, first); 
8175         }
8176         else if (tok == TOK_CASE) {
8177                 case_statement(state, first);
8178         }
8179         else if (tok == TOK_DEFAULT) {
8180                 default_statement(state, first);
8181         }
8182         else if (isdecl(tok)) {
8183                 /* This handles C99 intermixing of statements and decls */
8184                 decl(state, first);
8185         }
8186         else {
8187                 expr_statement(state, first);
8188         }
8189 }
8190
8191 static struct type *param_decl(struct compile_state *state)
8192 {
8193         struct type *type;
8194         struct hash_entry *ident;
8195         /* Cheat so the declarator will know we are not global */
8196         start_scope(state); 
8197         ident = 0;
8198         type = decl_specifiers(state);
8199         type = declarator(state, type, &ident, 0);
8200         type->field_ident = ident;
8201         end_scope(state);
8202         return type;
8203 }
8204
8205 static struct type *param_type_list(struct compile_state *state, struct type *type)
8206 {
8207         struct type *ftype, **next;
8208         ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8209         next = &ftype->right;
8210         while(peek(state) == TOK_COMMA) {
8211                 eat(state, TOK_COMMA);
8212                 if (peek(state) == TOK_DOTS) {
8213                         eat(state, TOK_DOTS);
8214                         error(state, 0, "variadic functions not supported");
8215                 }
8216                 else {
8217                         *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8218                         next = &((*next)->right);
8219                 }
8220         }
8221         return ftype;
8222 }
8223
8224
8225 static struct type *type_name(struct compile_state *state)
8226 {
8227         struct type *type;
8228         type = specifier_qualifier_list(state);
8229         /* abstract-declarator (may consume no tokens) */
8230         type = declarator(state, type, 0, 0);
8231         return type;
8232 }
8233
8234 static struct type *direct_declarator(
8235         struct compile_state *state, struct type *type, 
8236         struct hash_entry **ident, int need_ident)
8237 {
8238         struct type *outer;
8239         int op;
8240         outer = 0;
8241         arrays_complete(state, type);
8242         switch(peek(state)) {
8243         case TOK_IDENT:
8244                 eat(state, TOK_IDENT);
8245                 if (!ident) {
8246                         error(state, 0, "Unexpected identifier found");
8247                 }
8248                 /* The name of what we are declaring */
8249                 *ident = state->token[0].ident;
8250                 break;
8251         case TOK_LPAREN:
8252                 eat(state, TOK_LPAREN);
8253                 outer = declarator(state, type, ident, need_ident);
8254                 eat(state, TOK_RPAREN);
8255                 break;
8256         default:
8257                 if (need_ident) {
8258                         error(state, 0, "Identifier expected");
8259                 }
8260                 break;
8261         }
8262         do {
8263                 op = 1;
8264                 arrays_complete(state, type);
8265                 switch(peek(state)) {
8266                 case TOK_LPAREN:
8267                         eat(state, TOK_LPAREN);
8268                         type = param_type_list(state, type);
8269                         eat(state, TOK_RPAREN);
8270                         break;
8271                 case TOK_LBRACKET:
8272                 {
8273                         unsigned int qualifiers;
8274                         struct triple *value;
8275                         value = 0;
8276                         eat(state, TOK_LBRACKET);
8277                         if (peek(state) != TOK_RBRACKET) {
8278                                 value = constant_expr(state);
8279                                 integral(state, value);
8280                         }
8281                         eat(state, TOK_RBRACKET);
8282
8283                         qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8284                         type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8285                         if (value) {
8286                                 type->elements = value->u.cval;
8287                                 free_triple(state, value);
8288                         } else {
8289                                 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8290                                 op = 0;
8291                         }
8292                 }
8293                         break;
8294                 default:
8295                         op = 0;
8296                         break;
8297                 }
8298         } while(op);
8299         if (outer) {
8300                 struct type *inner;
8301                 arrays_complete(state, type);
8302                 FINISHME();
8303                 for(inner = outer; inner->left; inner = inner->left)
8304                         ;
8305                 inner->left = type;
8306                 type = outer;
8307         }
8308         return type;
8309 }
8310
8311 static struct type *declarator(
8312         struct compile_state *state, struct type *type, 
8313         struct hash_entry **ident, int need_ident)
8314 {
8315         while(peek(state) == TOK_STAR) {
8316                 eat(state, TOK_STAR);
8317                 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8318         }
8319         type = direct_declarator(state, type, ident, need_ident);
8320         return type;
8321 }
8322
8323
8324 static struct type *typedef_name(
8325         struct compile_state *state, unsigned int specifiers)
8326 {
8327         struct hash_entry *ident;
8328         struct type *type;
8329         eat(state, TOK_TYPE_NAME);
8330         ident = state->token[0].ident;
8331         type = ident->sym_ident->type;
8332         specifiers |= type->type & QUAL_MASK;
8333         if ((specifiers & (STOR_MASK | QUAL_MASK)) != 
8334                 (type->type & (STOR_MASK | QUAL_MASK))) {
8335                 type = clone_type(specifiers, type);
8336         }
8337         return type;
8338 }
8339
8340 static struct type *enum_specifier(
8341         struct compile_state *state, unsigned int specifiers)
8342 {
8343         int tok;
8344         struct type *type;
8345         type = 0;
8346         FINISHME();
8347         eat(state, TOK_ENUM);
8348         tok = peek(state);
8349         if (tok == TOK_IDENT) {
8350                 eat(state, TOK_IDENT);
8351         }
8352         if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8353                 eat(state, TOK_LBRACE);
8354                 do {
8355                         eat(state, TOK_IDENT);
8356                         if (peek(state) == TOK_EQ) {
8357                                 eat(state, TOK_EQ);
8358                                 constant_expr(state);
8359                         }
8360                         if (peek(state) == TOK_COMMA) {
8361                                 eat(state, TOK_COMMA);
8362                         }
8363                 } while(peek(state) != TOK_RBRACE);
8364                 eat(state, TOK_RBRACE);
8365         }
8366         FINISHME();
8367         return type;
8368 }
8369
8370 #if 0
8371 static struct type *struct_declarator(
8372         struct compile_state *state, struct type *type, struct hash_entry **ident)
8373 {
8374         int tok;
8375 #warning "struct_declarator is complicated because of bitfields, kill them?"
8376         tok = peek(state);
8377         if (tok != TOK_COLON) {
8378                 type = declarator(state, type, ident, 1);
8379         }
8380         if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8381                 eat(state, TOK_COLON);
8382                 constant_expr(state);
8383         }
8384         FINISHME();
8385         return type;
8386 }
8387 #endif
8388
8389 static struct type *struct_or_union_specifier(
8390         struct compile_state *state, unsigned int spec)
8391 {
8392         struct type *struct_type;
8393         struct hash_entry *ident;
8394         unsigned int type_join;
8395         int tok;
8396         struct_type = 0;
8397         ident = 0;
8398         switch(peek(state)) {
8399         case TOK_STRUCT:
8400                 eat(state, TOK_STRUCT);
8401                 type_join = TYPE_PRODUCT;
8402                 break;
8403         case TOK_UNION:
8404                 eat(state, TOK_UNION);
8405                 type_join = TYPE_OVERLAP;
8406                 error(state, 0, "unions not yet supported\n");
8407                 break;
8408         default:
8409                 eat(state, TOK_STRUCT);
8410                 type_join = TYPE_PRODUCT;
8411                 break;
8412         }
8413         tok = peek(state);
8414         if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8415                 eat(state, tok);
8416                 ident = state->token[0].ident;
8417         }
8418         if (!ident || (peek(state) == TOK_LBRACE)) {
8419                 ulong_t elements;
8420                 elements = 0;
8421                 eat(state, TOK_LBRACE);
8422                 do {
8423                         struct type *base_type;
8424                         struct type **next;
8425                         int done;
8426                         base_type = specifier_qualifier_list(state);
8427                         next = &struct_type;
8428                         do {
8429                                 struct type *type;
8430                                 struct hash_entry *fident;
8431                                 done = 1;
8432                                 type = declarator(state, base_type, &fident, 1);
8433                                 elements++;
8434                                 if (peek(state) == TOK_COMMA) {
8435                                         done = 0;
8436                                         eat(state, TOK_COMMA);
8437                                 }
8438                                 type = clone_type(0, type);
8439                                 type->field_ident = fident;
8440                                 if (*next) {
8441                                         *next = new_type(type_join, *next, type);
8442                                         next = &((*next)->right);
8443                                 } else {
8444                                         *next = type;
8445                                 }
8446                         } while(!done);
8447                         eat(state, TOK_SEMI);
8448                 } while(peek(state) != TOK_RBRACE);
8449                 eat(state, TOK_RBRACE);
8450                 struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
8451                 struct_type->type_ident = ident;
8452                 struct_type->elements = elements;
8453                 symbol(state, ident, &ident->sym_struct, 0, struct_type);
8454         }
8455         if (ident && ident->sym_struct) {
8456                 struct_type = clone_type(spec,  ident->sym_struct->type);
8457         }
8458         else if (ident && !ident->sym_struct) {
8459                 error(state, 0, "struct %s undeclared", ident->name);
8460         }
8461         return struct_type;
8462 }
8463
8464 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8465 {
8466         unsigned int specifiers;
8467         switch(peek(state)) {
8468         case TOK_AUTO:
8469                 eat(state, TOK_AUTO);
8470                 specifiers = STOR_AUTO;
8471                 break;
8472         case TOK_REGISTER:
8473                 eat(state, TOK_REGISTER);
8474                 specifiers = STOR_REGISTER;
8475                 break;
8476         case TOK_STATIC:
8477                 eat(state, TOK_STATIC);
8478                 specifiers = STOR_STATIC;
8479                 break;
8480         case TOK_EXTERN:
8481                 eat(state, TOK_EXTERN);
8482                 specifiers = STOR_EXTERN;
8483                 break;
8484         case TOK_TYPEDEF:
8485                 eat(state, TOK_TYPEDEF);
8486                 specifiers = STOR_TYPEDEF;
8487                 break;
8488         default:
8489                 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8490                         specifiers = STOR_STATIC;
8491                 }
8492                 else {
8493                         specifiers = STOR_AUTO;
8494                 }
8495         }
8496         return specifiers;
8497 }
8498
8499 static unsigned int function_specifier_opt(struct compile_state *state)
8500 {
8501         /* Ignore the inline keyword */
8502         unsigned int specifiers;
8503         specifiers = 0;
8504         switch(peek(state)) {
8505         case TOK_INLINE:
8506                 eat(state, TOK_INLINE);
8507                 specifiers = STOR_INLINE;
8508         }
8509         return specifiers;
8510 }
8511
8512 static unsigned int type_qualifiers(struct compile_state *state)
8513 {
8514         unsigned int specifiers;
8515         int done;
8516         done = 0;
8517         specifiers = QUAL_NONE;
8518         do {
8519                 switch(peek(state)) {
8520                 case TOK_CONST:
8521                         eat(state, TOK_CONST);
8522                         specifiers = QUAL_CONST;
8523                         break;
8524                 case TOK_VOLATILE:
8525                         eat(state, TOK_VOLATILE);
8526                         specifiers = QUAL_VOLATILE;
8527                         break;
8528                 case TOK_RESTRICT:
8529                         eat(state, TOK_RESTRICT);
8530                         specifiers = QUAL_RESTRICT;
8531                         break;
8532                 default:
8533                         done = 1;
8534                         break;
8535                 }
8536         } while(!done);
8537         return specifiers;
8538 }
8539
8540 static struct type *type_specifier(
8541         struct compile_state *state, unsigned int spec)
8542 {
8543         struct type *type;
8544         type = 0;
8545         switch(peek(state)) {
8546         case TOK_VOID:
8547                 eat(state, TOK_VOID);
8548                 type = new_type(TYPE_VOID | spec, 0, 0);
8549                 break;
8550         case TOK_CHAR:
8551                 eat(state, TOK_CHAR);
8552                 type = new_type(TYPE_CHAR | spec, 0, 0);
8553                 break;
8554         case TOK_SHORT:
8555                 eat(state, TOK_SHORT);
8556                 if (peek(state) == TOK_INT) {
8557                         eat(state, TOK_INT);
8558                 }
8559                 type = new_type(TYPE_SHORT | spec, 0, 0);
8560                 break;
8561         case TOK_INT:
8562                 eat(state, TOK_INT);
8563                 type = new_type(TYPE_INT | spec, 0, 0);
8564                 break;
8565         case TOK_LONG:
8566                 eat(state, TOK_LONG);
8567                 switch(peek(state)) {
8568                 case TOK_LONG:
8569                         eat(state, TOK_LONG);
8570                         error(state, 0, "long long not supported");
8571                         break;
8572                 case TOK_DOUBLE:
8573                         eat(state, TOK_DOUBLE);
8574                         error(state, 0, "long double not supported");
8575                         break;
8576                 case TOK_INT:
8577                         eat(state, TOK_INT);
8578                         type = new_type(TYPE_LONG | spec, 0, 0);
8579                         break;
8580                 default:
8581                         type = new_type(TYPE_LONG | spec, 0, 0);
8582                         break;
8583                 }
8584                 break;
8585         case TOK_FLOAT:
8586                 eat(state, TOK_FLOAT);
8587                 error(state, 0, "type float not supported");
8588                 break;
8589         case TOK_DOUBLE:
8590                 eat(state, TOK_DOUBLE);
8591                 error(state, 0, "type double not supported");
8592                 break;
8593         case TOK_SIGNED:
8594                 eat(state, TOK_SIGNED);
8595                 switch(peek(state)) {
8596                 case TOK_LONG:
8597                         eat(state, TOK_LONG);
8598                         switch(peek(state)) {
8599                         case TOK_LONG:
8600                                 eat(state, TOK_LONG);
8601                                 error(state, 0, "type long long not supported");
8602                                 break;
8603                         case TOK_INT:
8604                                 eat(state, TOK_INT);
8605                                 type = new_type(TYPE_LONG | spec, 0, 0);
8606                                 break;
8607                         default:
8608                                 type = new_type(TYPE_LONG | spec, 0, 0);
8609                                 break;
8610                         }
8611                         break;
8612                 case TOK_INT:
8613                         eat(state, TOK_INT);
8614                         type = new_type(TYPE_INT | spec, 0, 0);
8615                         break;
8616                 case TOK_SHORT:
8617                         eat(state, TOK_SHORT);
8618                         type = new_type(TYPE_SHORT | spec, 0, 0);
8619                         break;
8620                 case TOK_CHAR:
8621                         eat(state, TOK_CHAR);
8622                         type = new_type(TYPE_CHAR | spec, 0, 0);
8623                         break;
8624                 default:
8625                         type = new_type(TYPE_INT | spec, 0, 0);
8626                         break;
8627                 }
8628                 break;
8629         case TOK_UNSIGNED:
8630                 eat(state, TOK_UNSIGNED);
8631                 switch(peek(state)) {
8632                 case TOK_LONG:
8633                         eat(state, TOK_LONG);
8634                         switch(peek(state)) {
8635                         case TOK_LONG:
8636                                 eat(state, TOK_LONG);
8637                                 error(state, 0, "unsigned long long not supported");
8638                                 break;
8639                         case TOK_INT:
8640                                 eat(state, TOK_INT);
8641                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8642                                 break;
8643                         default:
8644                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8645                                 break;
8646                         }
8647                         break;
8648                 case TOK_INT:
8649                         eat(state, TOK_INT);
8650                         type = new_type(TYPE_UINT | spec, 0, 0);
8651                         break;
8652                 case TOK_SHORT:
8653                         eat(state, TOK_SHORT);
8654                         type = new_type(TYPE_USHORT | spec, 0, 0);
8655                         break;
8656                 case TOK_CHAR:
8657                         eat(state, TOK_CHAR);
8658                         type = new_type(TYPE_UCHAR | spec, 0, 0);
8659                         break;
8660                 default:
8661                         type = new_type(TYPE_UINT | spec, 0, 0);
8662                         break;
8663                 }
8664                 break;
8665                 /* struct or union specifier */
8666         case TOK_STRUCT:
8667         case TOK_UNION:
8668                 type = struct_or_union_specifier(state, spec);
8669                 break;
8670                 /* enum-spefifier */
8671         case TOK_ENUM:
8672                 type = enum_specifier(state, spec);
8673                 break;
8674                 /* typedef name */
8675         case TOK_TYPE_NAME:
8676                 type = typedef_name(state, spec);
8677                 break;
8678         default:
8679                 error(state, 0, "bad type specifier %s", 
8680                         tokens[peek(state)]);
8681                 break;
8682         }
8683         return type;
8684 }
8685
8686 static int istype(int tok)
8687 {
8688         switch(tok) {
8689         case TOK_CONST:
8690         case TOK_RESTRICT:
8691         case TOK_VOLATILE:
8692         case TOK_VOID:
8693         case TOK_CHAR:
8694         case TOK_SHORT:
8695         case TOK_INT:
8696         case TOK_LONG:
8697         case TOK_FLOAT:
8698         case TOK_DOUBLE:
8699         case TOK_SIGNED:
8700         case TOK_UNSIGNED:
8701         case TOK_STRUCT:
8702         case TOK_UNION:
8703         case TOK_ENUM:
8704         case TOK_TYPE_NAME:
8705                 return 1;
8706         default:
8707                 return 0;
8708         }
8709 }
8710
8711
8712 static struct type *specifier_qualifier_list(struct compile_state *state)
8713 {
8714         struct type *type;
8715         unsigned int specifiers = 0;
8716
8717         /* type qualifiers */
8718         specifiers |= type_qualifiers(state);
8719
8720         /* type specifier */
8721         type = type_specifier(state, specifiers);
8722
8723         return type;
8724 }
8725
8726 static int isdecl_specifier(int tok)
8727 {
8728         switch(tok) {
8729                 /* storage class specifier */
8730         case TOK_AUTO:
8731         case TOK_REGISTER:
8732         case TOK_STATIC:
8733         case TOK_EXTERN:
8734         case TOK_TYPEDEF:
8735                 /* type qualifier */
8736         case TOK_CONST:
8737         case TOK_RESTRICT:
8738         case TOK_VOLATILE:
8739                 /* type specifiers */
8740         case TOK_VOID:
8741         case TOK_CHAR:
8742         case TOK_SHORT:
8743         case TOK_INT:
8744         case TOK_LONG:
8745         case TOK_FLOAT:
8746         case TOK_DOUBLE:
8747         case TOK_SIGNED:
8748         case TOK_UNSIGNED:
8749                 /* struct or union specifier */
8750         case TOK_STRUCT:
8751         case TOK_UNION:
8752                 /* enum-spefifier */
8753         case TOK_ENUM:
8754                 /* typedef name */
8755         case TOK_TYPE_NAME:
8756                 /* function specifiers */
8757         case TOK_INLINE:
8758                 return 1;
8759         default:
8760                 return 0;
8761         }
8762 }
8763
8764 static struct type *decl_specifiers(struct compile_state *state)
8765 {
8766         struct type *type;
8767         unsigned int specifiers;
8768         /* I am overly restrictive in the arragement of specifiers supported.
8769          * C is overly flexible in this department it makes interpreting
8770          * the parse tree difficult.
8771          */
8772         specifiers = 0;
8773
8774         /* storage class specifier */
8775         specifiers |= storage_class_specifier_opt(state);
8776
8777         /* function-specifier */
8778         specifiers |= function_specifier_opt(state);
8779
8780         /* type qualifier */
8781         specifiers |= type_qualifiers(state);
8782
8783         /* type specifier */
8784         type = type_specifier(state, specifiers);
8785         return type;
8786 }
8787
8788 struct field_info {
8789         struct type *type;
8790         size_t offset;
8791 };
8792
8793 static struct field_info designator(struct compile_state *state, struct type *type)
8794 {
8795         int tok;
8796         struct field_info info;
8797         info.offset = ~0U;
8798         info.type = 0;
8799         do {
8800                 switch(peek(state)) {
8801                 case TOK_LBRACKET:
8802                 {
8803                         struct triple *value;
8804                         if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
8805                                 error(state, 0, "Array designator not in array initializer");
8806                         }
8807                         eat(state, TOK_LBRACKET);
8808                         value = constant_expr(state);
8809                         eat(state, TOK_RBRACKET);
8810
8811                         info.type = type->left;
8812                         info.offset = value->u.cval * size_of(state, info.type);
8813                         break;
8814                 }
8815                 case TOK_DOT:
8816                 {
8817                         struct hash_entry *field;
8818                         struct type *member;
8819                         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
8820                                 error(state, 0, "Struct designator not in struct initializer");
8821                         }
8822                         eat(state, TOK_DOT);
8823                         eat(state, TOK_IDENT);
8824                         field = state->token[0].ident;
8825                         info.offset = 0;
8826                         member = type->left;
8827                         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
8828                                 if (member->left->field_ident == field) {
8829                                         member = member->left;
8830                                         break;
8831                                 }
8832                                 info.offset += size_of(state, member->left);
8833                                 member = member->right;
8834                         }
8835                         if (member->field_ident != field) {
8836                                 error(state, 0, "%s is not a member", 
8837                                         field->name);
8838                         }
8839                         info.type = member;
8840                         break;
8841                 }
8842                 default:
8843                         error(state, 0, "Invalid designator");
8844                 }
8845                 tok = peek(state);
8846         } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
8847         eat(state, TOK_EQ);
8848         return info;
8849 }
8850
8851 static struct triple *initializer(
8852         struct compile_state *state, struct type *type)
8853 {
8854         struct triple *result;
8855         if (peek(state) != TOK_LBRACE) {
8856                 result = assignment_expr(state);
8857         }
8858         else {
8859                 int comma;
8860                 size_t max_offset;
8861                 struct field_info info;
8862                 void *buf;
8863                 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
8864                         ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
8865                         internal_error(state, 0, "unknown initializer type");
8866                 }
8867                 info.offset = 0;
8868                 info.type = type->left;
8869                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
8870                         max_offset = 0;
8871                 } else {
8872                         max_offset = size_of(state, type);
8873                 }
8874                 buf = xcmalloc(max_offset, "initializer");
8875                 eat(state, TOK_LBRACE);
8876                 do {
8877                         struct triple *value;
8878                         struct type *value_type;
8879                         size_t value_size;
8880                         void *dest;
8881                         int tok;
8882                         comma = 0;
8883                         tok = peek(state);
8884                         if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
8885                                 info = designator(state, type);
8886                         }
8887                         if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
8888                                 (info.offset >= max_offset)) {
8889                                 error(state, 0, "element beyond bounds");
8890                         }
8891                         value_type = info.type;
8892                         if ((value_type->type & TYPE_MASK) == TYPE_PRODUCT) {
8893                                 value_type = type->left;
8894                         }
8895                         value = eval_const_expr(state, initializer(state, value_type));
8896                         value_size = size_of(state, value_type);
8897                         if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
8898                                 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
8899                                 (max_offset <= info.offset)) {
8900                                 void *old_buf;
8901                                 size_t old_size;
8902                                 old_buf = buf;
8903                                 old_size = max_offset;
8904                                 max_offset = info.offset + value_size;
8905                                 buf = xmalloc(max_offset, "initializer");
8906                                 memcpy(buf, old_buf, old_size);
8907                                 xfree(old_buf);
8908                         }
8909                         dest = ((char *)buf) + info.offset;
8910                         if (value->op == OP_BLOBCONST) {
8911                                 memcpy(dest, value->u.blob, value_size);
8912                         }
8913                         else if ((value->op == OP_INTCONST) && (value_size == 1)) {
8914                                 *((uint8_t *)dest) = value->u.cval & 0xff;
8915                         }
8916                         else if ((value->op == OP_INTCONST) && (value_size == 2)) {
8917                                 *((uint16_t *)dest) = value->u.cval & 0xffff;
8918                         }
8919                         else if ((value->op == OP_INTCONST) && (value_size == 4)) {
8920                                 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
8921                         }
8922                         else {
8923                                 fprintf(stderr, "%d %d\n",
8924                                         value->op, value_size);
8925                                 internal_error(state, 0, "unhandled constant initializer");
8926                         }
8927                         free_triple(state, value);
8928                         if (peek(state) == TOK_COMMA) {
8929                                 eat(state, TOK_COMMA);
8930                                 comma = 1;
8931                         }
8932                         info.offset += value_size;
8933                         if ((info.type->type & TYPE_MASK) == TYPE_PRODUCT) {
8934                                 info.type = info.type->right;
8935                         }
8936                 } while(comma && (peek(state) != TOK_RBRACE));
8937                 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
8938                         ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
8939                         type->elements = max_offset / size_of(state, type->left);
8940                 }
8941                 eat(state, TOK_RBRACE);
8942                 result = triple(state, OP_BLOBCONST, type, 0, 0);
8943                 result->u.blob = buf;
8944         }
8945         return result;
8946 }
8947
8948 static void resolve_branches(struct compile_state *state)
8949 {
8950         /* Make a second pass and finish anything outstanding
8951          * with respect to branches.  The only outstanding item
8952          * is to see if there are goto to labels that have not
8953          * been defined and to error about them.
8954          */
8955         int i;
8956         for(i = 0; i < HASH_TABLE_SIZE; i++) {
8957                 struct hash_entry *entry;
8958                 for(entry = state->hash_table[i]; entry; entry = entry->next) {
8959                         struct triple *ins;
8960                         if (!entry->sym_label) {
8961                                 continue;
8962                         }
8963                         ins = entry->sym_label->def;
8964                         if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
8965                                 error(state, ins, "label `%s' used but not defined",
8966                                         entry->name);
8967                         }
8968                 }
8969         }
8970 }
8971
8972 static struct triple *function_definition(
8973         struct compile_state *state, struct type *type)
8974 {
8975         struct triple *def, *tmp, *first, *end;
8976         struct hash_entry *ident;
8977         struct type *param;
8978         int i;
8979         if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
8980                 error(state, 0, "Invalid function header");
8981         }
8982
8983         /* Verify the function type */
8984         if (((type->right->type & TYPE_MASK) != TYPE_VOID)  &&
8985                 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
8986                 (type->right->field_ident == 0)) {
8987                 error(state, 0, "Invalid function parameters");
8988         }
8989         param = type->right;
8990         i = 0;
8991         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
8992                 i++;
8993                 if (!param->left->field_ident) {
8994                         error(state, 0, "No identifier for parameter %d\n", i);
8995                 }
8996                 param = param->right;
8997         }
8998         i++;
8999         if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
9000                 error(state, 0, "No identifier for paramter %d\n", i);
9001         }
9002         
9003         /* Get a list of statements for this function. */
9004         def = triple(state, OP_LIST, type, 0, 0);
9005
9006         /* Start a new scope for the passed parameters */
9007         start_scope(state);
9008
9009         /* Put a label at the very start of a function */
9010         first = label(state);
9011         RHS(def, 0) = first;
9012
9013         /* Put a label at the very end of a function */
9014         end = label(state);
9015         flatten(state, first, end);
9016
9017         /* Walk through the parameters and create symbol table entries
9018          * for them.
9019          */
9020         param = type->right;
9021         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9022                 ident = param->left->field_ident;
9023                 tmp = variable(state, param->left);
9024                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9025                 flatten(state, end, tmp);
9026                 param = param->right;
9027         }
9028         if ((param->type & TYPE_MASK) != TYPE_VOID) {
9029                 /* And don't forget the last parameter */
9030                 ident = param->field_ident;
9031                 tmp = variable(state, param);
9032                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9033                 flatten(state, end, tmp);
9034         }
9035         /* Add a variable for the return value */
9036         MISC(def, 0) = 0;
9037         if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
9038                 /* Remove all type qualifiers from the return type */
9039                 tmp = variable(state, clone_type(0, type->left));
9040                 flatten(state, end, tmp);
9041                 /* Remember where the return value is */
9042                 MISC(def, 0) = tmp;
9043         }
9044
9045         /* Remember which function I am compiling.
9046          * Also assume the last defined function is the main function.
9047          */
9048         state->main_function = def;
9049
9050         /* Now get the actual function definition */
9051         compound_statement(state, end);
9052
9053         /* Finish anything unfinished with branches */
9054         resolve_branches(state);
9055
9056         /* Remove the parameter scope */
9057         end_scope(state);
9058
9059 #if 0
9060         fprintf(stdout, "\n");
9061         loc(stdout, state, 0);
9062         fprintf(stdout, "\n__________ function_definition _________\n");
9063         print_triple(state, def);
9064         fprintf(stdout, "__________ function_definition _________ done\n\n");
9065 #endif
9066
9067         return def;
9068 }
9069
9070 static struct triple *do_decl(struct compile_state *state, 
9071         struct type *type, struct hash_entry *ident)
9072 {
9073         struct triple *def;
9074         def = 0;
9075         /* Clean up the storage types used */
9076         switch (type->type & STOR_MASK) {
9077         case STOR_AUTO:
9078         case STOR_STATIC:
9079                 /* These are the good types I am aiming for */
9080                 break;
9081         case STOR_REGISTER:
9082                 type->type &= ~STOR_MASK;
9083                 type->type |= STOR_AUTO;
9084                 break;
9085         case STOR_EXTERN:
9086                 type->type &= ~STOR_MASK;
9087                 type->type |= STOR_STATIC;
9088                 break;
9089         case STOR_TYPEDEF:
9090                 if (!ident) {
9091                         error(state, 0, "typedef without name");
9092                 }
9093                 symbol(state, ident, &ident->sym_ident, 0, type);
9094                 ident->tok = TOK_TYPE_NAME;
9095                 return 0;
9096                 break;
9097         default:
9098                 internal_error(state, 0, "Undefined storage class");
9099         }
9100         if (ident && 
9101                 ((type->type & STOR_MASK) == STOR_STATIC) &&
9102                 ((type->type & QUAL_CONST) == 0)) {
9103                 error(state, 0, "non const static variables not supported");
9104         }
9105         if (ident) {
9106                 def = variable(state, type);
9107                 symbol(state, ident, &ident->sym_ident, def, type);
9108         }
9109         return def;
9110 }
9111
9112 static void decl(struct compile_state *state, struct triple *first)
9113 {
9114         struct type *base_type, *type;
9115         struct hash_entry *ident;
9116         struct triple *def;
9117         int global;
9118         global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9119         base_type = decl_specifiers(state);
9120         ident = 0;
9121         type = declarator(state, base_type, &ident, 0);
9122         if (global && ident && (peek(state) == TOK_LBRACE)) {
9123                 /* function */
9124                 state->function = ident->name;
9125                 def = function_definition(state, type);
9126                 symbol(state, ident, &ident->sym_ident, def, type);
9127                 state->function = 0;
9128         }
9129         else {
9130                 int done;
9131                 flatten(state, first, do_decl(state, type, ident));
9132                 /* type or variable definition */
9133                 do {
9134                         done = 1;
9135                         if (peek(state) == TOK_EQ) {
9136                                 if (!ident) {
9137                                         error(state, 0, "cannot assign to a type");
9138                                 }
9139                                 eat(state, TOK_EQ);
9140                                 flatten(state, first,
9141                                         init_expr(state, 
9142                                                 ident->sym_ident->def, 
9143                                                 initializer(state, type)));
9144                         }
9145                         arrays_complete(state, type);
9146                         if (peek(state) == TOK_COMMA) {
9147                                 eat(state, TOK_COMMA);
9148                                 ident = 0;
9149                                 type = declarator(state, base_type, &ident, 0);
9150                                 flatten(state, first, do_decl(state, type, ident));
9151                                 done = 0;
9152                         }
9153                 } while(!done);
9154                 eat(state, TOK_SEMI);
9155         }
9156 }
9157
9158 static void decls(struct compile_state *state)
9159 {
9160         struct triple *list;
9161         int tok;
9162         list = label(state);
9163         while(1) {
9164                 tok = peek(state);
9165                 if (tok == TOK_EOF) {
9166                         return;
9167                 }
9168                 if (tok == TOK_SPACE) {
9169                         eat(state, TOK_SPACE);
9170                 }
9171                 decl(state, list);
9172                 if (list->next != list) {
9173                         error(state, 0, "global variables not supported");
9174                 }
9175         }
9176 }
9177
9178 /*
9179  * Data structurs for optimation.
9180  */
9181
9182 static void do_use_block(
9183         struct block *used, struct block_set **head, struct block *user, 
9184         int front)
9185 {
9186         struct block_set **ptr, *new;
9187         if (!used)
9188                 return;
9189         if (!user)
9190                 return;
9191         ptr = head;
9192         while(*ptr) {
9193                 if ((*ptr)->member == user) {
9194                         return;
9195                 }
9196                 ptr = &(*ptr)->next;
9197         }
9198         new = xcmalloc(sizeof(*new), "block_set");
9199         new->member = user;
9200         if (front) {
9201                 new->next = *head;
9202                 *head = new;
9203         }
9204         else {
9205                 new->next = 0;
9206                 *ptr = new;
9207         }
9208 }
9209 static void do_unuse_block(
9210         struct block *used, struct block_set **head, struct block *unuser)
9211 {
9212         struct block_set *use, **ptr;
9213         ptr = head;
9214         while(*ptr) {
9215                 use = *ptr;
9216                 if (use->member == unuser) {
9217                         *ptr = use->next;
9218                         memset(use, -1, sizeof(*use));
9219                         xfree(use);
9220                 }
9221                 else {
9222                         ptr = &use->next;
9223                 }
9224         }
9225 }
9226
9227 static void use_block(struct block *used, struct block *user)
9228 {
9229         /* Append new to the head of the list, print_block
9230          * depends on this.
9231          */
9232         do_use_block(used, &used->use, user, 1); 
9233         used->users++;
9234 }
9235 static void unuse_block(struct block *used, struct block *unuser)
9236 {
9237         do_unuse_block(used, &used->use, unuser); 
9238         used->users--;
9239 }
9240
9241 static void idom_block(struct block *idom, struct block *user)
9242 {
9243         do_use_block(idom, &idom->idominates, user, 0);
9244 }
9245
9246 static void unidom_block(struct block *idom, struct block *unuser)
9247 {
9248         do_unuse_block(idom, &idom->idominates, unuser);
9249 }
9250
9251 static void domf_block(struct block *block, struct block *domf)
9252 {
9253         do_use_block(block, &block->domfrontier, domf, 0);
9254 }
9255
9256 static void undomf_block(struct block *block, struct block *undomf)
9257 {
9258         do_unuse_block(block, &block->domfrontier, undomf);
9259 }
9260
9261 static void ipdom_block(struct block *ipdom, struct block *user)
9262 {
9263         do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9264 }
9265
9266 static void unipdom_block(struct block *ipdom, struct block *unuser)
9267 {
9268         do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9269 }
9270
9271 static void ipdomf_block(struct block *block, struct block *ipdomf)
9272 {
9273         do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9274 }
9275
9276 static void unipdomf_block(struct block *block, struct block *unipdomf)
9277 {
9278         do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9279 }
9280
9281
9282
9283 static int do_walk_triple(struct compile_state *state,
9284         struct triple *ptr, int depth,
9285         int (*cb)(struct compile_state *state, struct triple *ptr, int depth)) 
9286 {
9287         int result;
9288         result = cb(state, ptr, depth);
9289         if ((result == 0) && (ptr->op == OP_LIST)) {
9290                 struct triple *list;
9291                 list = ptr;
9292                 ptr = RHS(list, 0);
9293                 do {
9294                         result = do_walk_triple(state, ptr, depth + 1, cb);
9295                         if (ptr->next->prev != ptr) {
9296                                 internal_error(state, ptr->next, "bad prev");
9297                         }
9298                         ptr = ptr->next;
9299                         
9300                 } while((result == 0) && (ptr != RHS(list, 0)));
9301         }
9302         return result;
9303 }
9304
9305 static int walk_triple(
9306         struct compile_state *state, 
9307         struct triple *ptr, 
9308         int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9309 {
9310         return do_walk_triple(state, ptr, 0, cb);
9311 }
9312
9313 static void do_print_prefix(int depth)
9314 {
9315         int i;
9316         for(i = 0; i < depth; i++) {
9317                 printf("  ");
9318         }
9319 }
9320
9321 #define PRINT_LIST 1
9322 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9323 {
9324         int op;
9325         op = ins->op;
9326         if (op == OP_LIST) {
9327 #if !PRINT_LIST
9328                 return 0;
9329 #endif
9330         }
9331         if ((op == OP_LABEL) && (ins->use)) {
9332                 printf("\n%p:\n", ins);
9333         }
9334         do_print_prefix(depth);
9335         display_triple(stdout, ins);
9336
9337         if ((ins->op == OP_BRANCH) && ins->use) {
9338                 internal_error(state, ins, "branch used?");
9339         }
9340 #if 0
9341         {
9342                 struct triple_set *user;
9343                 for(user = ins->use; user; user = user->next) {
9344                         printf("use: %p\n", user->member);
9345                 }
9346         }
9347 #endif
9348         if (triple_is_branch(state, ins)) {
9349                 printf("\n");
9350         }
9351         return 0;
9352 }
9353
9354 static void print_triple(struct compile_state *state, struct triple *ins)
9355 {
9356         walk_triple(state, ins, do_print_triple);
9357 }
9358
9359 static void print_triples(struct compile_state *state)
9360 {
9361         print_triple(state, state->main_function);
9362 }
9363
9364 struct cf_block {
9365         struct block *block;
9366 };
9367 static void find_cf_blocks(struct cf_block *cf, struct block *block)
9368 {
9369         if (!block || (cf[block->vertex].block == block)) {
9370                 return;
9371         }
9372         cf[block->vertex].block = block;
9373         find_cf_blocks(cf, block->left);
9374         find_cf_blocks(cf, block->right);
9375 }
9376
9377 static void print_control_flow(struct compile_state *state)
9378 {
9379         struct cf_block *cf;
9380         int i;
9381         printf("\ncontrol flow\n");
9382         cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9383         find_cf_blocks(cf, state->first_block);
9384
9385         for(i = 1; i <= state->last_vertex; i++) {
9386                 struct block *block;
9387                 block = cf[i].block;
9388                 if (!block)
9389                         continue;
9390                 printf("(%p) %d:", block, block->vertex);
9391                 if (block->left) {
9392                         printf(" %d", block->left->vertex);
9393                 }
9394                 if (block->right && (block->right != block->left)) {
9395                         printf(" %d", block->right->vertex);
9396                 }
9397                 printf("\n");
9398         }
9399
9400         xfree(cf);
9401 }
9402
9403
9404 static struct block *basic_block(struct compile_state *state,
9405         struct triple *first)
9406 {
9407         struct block *block;
9408         struct triple *ptr;
9409         int op;
9410         if (first->op != OP_LABEL) {
9411                 internal_error(state, 0, "block does not start with a label");
9412         }
9413         /* See if this basic block has already been setup */
9414         if (first->u.block != 0) {
9415                 return first->u.block;
9416         }
9417         /* Allocate another basic block structure */
9418         state->last_vertex += 1;
9419         block = xcmalloc(sizeof(*block), "block");
9420         block->first = block->last = first;
9421         block->vertex = state->last_vertex;
9422         ptr = first;
9423         do {
9424                 if ((ptr != first) && (ptr->op == OP_LABEL) && ptr->use) {
9425                         break;
9426                 }
9427                 block->last = ptr;
9428                 /* If ptr->u is not used remember where the baic block is */
9429                 if (triple_stores_block(state, ptr)) {
9430                         ptr->u.block = block;
9431                 }
9432                 if (ptr->op == OP_BRANCH) {
9433                         break;
9434                 }
9435                 ptr = ptr->next;
9436         } while (ptr != RHS(state->main_function, 0));
9437         if (ptr == RHS(state->main_function, 0))
9438                 return block;
9439         op = ptr->op;
9440         if (op == OP_LABEL) {
9441                 block->left = basic_block(state, ptr);
9442                 block->right = 0;
9443                 use_block(block->left, block);
9444         }
9445         else if (op == OP_BRANCH) {
9446                 block->left = 0;
9447                 /* Trace the branch target */
9448                 block->right = basic_block(state, TARG(ptr, 0));
9449                 use_block(block->right, block);
9450                 /* If there is a test trace the branch as well */
9451                 if (TRIPLE_RHS(ptr->sizes)) {
9452                         block->left = basic_block(state, ptr->next);
9453                         use_block(block->left, block);
9454                 }
9455         }
9456         else {
9457                 internal_error(state, 0, "Bad basic block split");
9458         }
9459         return block;
9460 }
9461
9462
9463 static void walk_blocks(struct compile_state *state,
9464         void (*cb)(struct compile_state *state, struct block *block, void *arg),
9465         void *arg)
9466 {
9467         struct triple *ptr, *first;
9468         struct block *last_block;
9469         last_block = 0;
9470         first = RHS(state->main_function, 0);
9471         ptr = first;
9472         do {
9473                 struct block *block;
9474                 if (ptr->op == OP_LABEL) {
9475                         block = ptr->u.block;
9476                         if (block && (block != last_block)) {
9477                                 cb(state, block, arg);
9478                         }
9479                         last_block = block;
9480                 }
9481                 ptr = ptr->next;
9482         } while(ptr != first);
9483 }
9484
9485 static void print_block(
9486         struct compile_state *state, struct block *block, void *arg)
9487 {
9488         struct triple *ptr;
9489         FILE *fp = arg;
9490
9491         fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n", 
9492                 block, 
9493                 block->vertex,
9494                 block->left, 
9495                 block->left && block->left->use?block->left->use->member : 0,
9496                 block->right, 
9497                 block->right && block->right->use?block->right->use->member : 0);
9498         if (block->first->op == OP_LABEL) {
9499                 fprintf(fp, "%p:\n", block->first);
9500         }
9501         for(ptr = block->first; ; ptr = ptr->next) {
9502                 struct triple_set *user;
9503                 int op = ptr->op;
9504                 
9505                 if (triple_stores_block(state, ptr)) {
9506                         if (ptr->u.block != block) {
9507                                 internal_error(state, ptr, 
9508                                         "Wrong block pointer: %p\n",
9509                                         ptr->u.block);
9510                         }
9511                 }
9512                 if (op == OP_ADECL) {
9513                         for(user = ptr->use; user; user = user->next) {
9514                                 if (!user->member->u.block) {
9515                                         internal_error(state, user->member, 
9516                                                 "Use %p not in a block?\n",
9517                                                 user->member);
9518                                 }
9519                         }
9520                 }
9521                 display_triple(fp, ptr);
9522
9523 #if 0
9524                 for(user = ptr->use; user; user = user->next) {
9525                         fprintf(fp, "use: %p\n", user->member);
9526                 }
9527 #endif
9528
9529                 /* Sanity checks... */
9530                 valid_ins(state, ptr);
9531                 for(user = ptr->use; user; user = user->next) {
9532                         struct triple *use;
9533                         use = user->member;
9534                         valid_ins(state, use);
9535                         if (triple_stores_block(state, user->member) &&
9536                                 !user->member->u.block) {
9537                                 internal_error(state, user->member,
9538                                         "Use %p not in a block?",
9539                                         user->member);
9540                         }
9541                 }
9542
9543                 if (ptr == block->last)
9544                         break;
9545         }
9546         fprintf(fp,"\n");
9547 }
9548
9549
9550 static void print_blocks(struct compile_state *state, FILE *fp)
9551 {
9552         fprintf(fp, "--------------- blocks ---------------\n");
9553         walk_blocks(state, print_block, fp);
9554 }
9555
9556 static void prune_nonblock_triples(struct compile_state *state)
9557 {
9558         struct block *block;
9559         struct triple *first, *ins, *next;
9560         /* Delete the triples not in a basic block */
9561         first = RHS(state->main_function, 0);
9562         block = 0;
9563         ins = first;
9564         do {
9565                 next = ins->next;
9566                 if (ins->op == OP_LABEL) {
9567                         block = ins->u.block;
9568                 }
9569                 if (!block) {
9570                         release_triple(state, ins);
9571                 }
9572                 ins = next;
9573         } while(ins != first);
9574 }
9575
9576 static void setup_basic_blocks(struct compile_state *state)
9577 {
9578         if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9579                 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9580                 internal_error(state, 0, "ins will not store block?");
9581         }
9582         /* Find the basic blocks */
9583         state->last_vertex = 0;
9584         state->first_block = basic_block(state, RHS(state->main_function,0));
9585         /* Delete the triples not in a basic block */
9586         prune_nonblock_triples(state);
9587         /* Find the last basic block */
9588         state->last_block = RHS(state->main_function, 0)->prev->u.block;
9589         if (!state->last_block) {
9590                 internal_error(state, 0, "end not used?");
9591         }
9592         /* Insert an extra unused edge from start to the end 
9593          * This helps with reverse control flow calculations.
9594          */
9595         use_block(state->first_block, state->last_block);
9596         /* If we are debugging print what I have just done */
9597         if (state->debug & DEBUG_BASIC_BLOCKS) {
9598                 print_blocks(state, stdout);
9599                 print_control_flow(state);
9600         }
9601 }
9602
9603 static void free_basic_block(struct compile_state *state, struct block *block)
9604 {
9605         struct block_set *entry, *next;
9606         struct block *child;
9607         if (!block) {
9608                 return;
9609         }
9610         if (block->vertex == -1) {
9611                 return;
9612         }
9613         block->vertex = -1;
9614         if (block->left) {
9615                 unuse_block(block->left, block);
9616         }
9617         if (block->right) {
9618                 unuse_block(block->right, block);
9619         }
9620         if (block->idom) {
9621                 unidom_block(block->idom, block);
9622         }
9623         block->idom = 0;
9624         if (block->ipdom) {
9625                 unipdom_block(block->ipdom, block);
9626         }
9627         block->ipdom = 0;
9628         for(entry = block->use; entry; entry = next) {
9629                 next = entry->next;
9630                 child = entry->member;
9631                 unuse_block(block, child);
9632                 if (child->left == block) {
9633                         child->left = 0;
9634                 }
9635                 if (child->right == block) {
9636                         child->right = 0;
9637                 }
9638         }
9639         for(entry = block->idominates; entry; entry = next) {
9640                 next = entry->next;
9641                 child = entry->member;
9642                 unidom_block(block, child);
9643                 child->idom = 0;
9644         }
9645         for(entry = block->domfrontier; entry; entry = next) {
9646                 next = entry->next;
9647                 child = entry->member;
9648                 undomf_block(block, child);
9649         }
9650         for(entry = block->ipdominates; entry; entry = next) {
9651                 next = entry->next;
9652                 child = entry->member;
9653                 unipdom_block(block, child);
9654                 child->ipdom = 0;
9655         }
9656         for(entry = block->ipdomfrontier; entry; entry = next) {
9657                 next = entry->next;
9658                 child = entry->member;
9659                 unipdomf_block(block, child);
9660         }
9661         if (block->users != 0) {
9662                 internal_error(state, 0, "block still has users");
9663         }
9664         free_basic_block(state, block->left);
9665         block->left = 0;
9666         free_basic_block(state, block->right);
9667         block->right = 0;
9668         memset(block, -1, sizeof(*block));
9669         xfree(block);
9670 }
9671
9672 static void free_basic_blocks(struct compile_state *state)
9673 {
9674         struct triple *first, *ins;
9675         free_basic_block(state, state->first_block);
9676         state->last_vertex = 0;
9677         state->first_block = state->last_block = 0;
9678         first = RHS(state->main_function, 0);
9679         ins = first;
9680         do {
9681                 if (triple_stores_block(state, ins)) {
9682                         ins->u.block = 0;
9683                 }
9684                 ins = ins->next;
9685         } while(ins != first);
9686         
9687 }
9688
9689 struct sdom_block {
9690         struct block *block;
9691         struct sdom_block *sdominates;
9692         struct sdom_block *sdom_next;
9693         struct sdom_block *sdom;
9694         struct sdom_block *label;
9695         struct sdom_block *parent;
9696         struct sdom_block *ancestor;
9697         int vertex;
9698 };
9699
9700
9701 static void unsdom_block(struct sdom_block *block)
9702 {
9703         struct sdom_block **ptr;
9704         if (!block->sdom_next) {
9705                 return;
9706         }
9707         ptr = &block->sdom->sdominates;
9708         while(*ptr) {
9709                 if ((*ptr) == block) {
9710                         *ptr = block->sdom_next;
9711                         return;
9712                 }
9713                 ptr = &(*ptr)->sdom_next;
9714         }
9715 }
9716
9717 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9718 {
9719         unsdom_block(block);
9720         block->sdom = sdom;
9721         block->sdom_next = sdom->sdominates;
9722         sdom->sdominates = block;
9723 }
9724
9725
9726
9727 static int initialize_sdblock(struct sdom_block *sd,
9728         struct block *parent, struct block *block, int vertex)
9729 {
9730         if (!block || (sd[block->vertex].block == block)) {
9731                 return vertex;
9732         }
9733         vertex += 1;
9734         /* Renumber the blocks in a convinient fashion */
9735         block->vertex = vertex;
9736         sd[vertex].block    = block;
9737         sd[vertex].sdom     = &sd[vertex];
9738         sd[vertex].label    = &sd[vertex];
9739         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9740         sd[vertex].ancestor = 0;
9741         sd[vertex].vertex   = vertex;
9742         vertex = initialize_sdblock(sd, block, block->left, vertex);
9743         vertex = initialize_sdblock(sd, block, block->right, vertex);
9744         return vertex;
9745 }
9746
9747 static int initialize_sdpblock(struct sdom_block *sd,
9748         struct block *parent, struct block *block, int vertex)
9749 {
9750         struct block_set *user;
9751         if (!block || (sd[block->vertex].block == block)) {
9752                 return vertex;
9753         }
9754         vertex += 1;
9755         /* Renumber the blocks in a convinient fashion */
9756         block->vertex = vertex;
9757         sd[vertex].block    = block;
9758         sd[vertex].sdom     = &sd[vertex];
9759         sd[vertex].label    = &sd[vertex];
9760         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9761         sd[vertex].ancestor = 0;
9762         sd[vertex].vertex   = vertex;
9763         for(user = block->use; user; user = user->next) {
9764                 vertex = initialize_sdpblock(sd, block, user->member, vertex);
9765         }
9766         return vertex;
9767 }
9768
9769 static void compress_ancestors(struct sdom_block *v)
9770 {
9771         /* This procedure assumes ancestor(v) != 0 */
9772         /* if (ancestor(ancestor(v)) != 0) {
9773          *      compress(ancestor(ancestor(v)));
9774          *      if (semi(label(ancestor(v))) < semi(label(v))) {
9775          *              label(v) = label(ancestor(v));
9776          *      }
9777          *      ancestor(v) = ancestor(ancestor(v));
9778          * }
9779          */
9780         if (!v->ancestor) {
9781                 return;
9782         }
9783         if (v->ancestor->ancestor) {
9784                 compress_ancestors(v->ancestor->ancestor);
9785                 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
9786                         v->label = v->ancestor->label;
9787                 }
9788                 v->ancestor = v->ancestor->ancestor;
9789         }
9790 }
9791
9792 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
9793 {
9794         int i;
9795         /* // step 2 
9796          *  for each v <= pred(w) {
9797          *      u = EVAL(v);
9798          *      if (semi[u] < semi[w] { 
9799          *              semi[w] = semi[u]; 
9800          *      } 
9801          * }
9802          * add w to bucket(vertex(semi[w]));
9803          * LINK(parent(w), w);
9804          *
9805          * // step 3
9806          * for each v <= bucket(parent(w)) {
9807          *      delete v from bucket(parent(w));
9808          *      u = EVAL(v);
9809          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9810          * }
9811          */
9812         for(i = state->last_vertex; i >= 2; i--) {
9813                 struct sdom_block *v, *parent, *next;
9814                 struct block_set *user;
9815                 struct block *block;
9816                 block = sd[i].block;
9817                 parent = sd[i].parent;
9818                 /* Step 2 */
9819                 for(user = block->use; user; user = user->next) {
9820                         struct sdom_block *v, *u;
9821                         v = &sd[user->member->vertex];
9822                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9823                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9824                                 sd[i].sdom = u->sdom;
9825                         }
9826                 }
9827                 sdom_block(sd[i].sdom, &sd[i]);
9828                 sd[i].ancestor = parent;
9829                 /* Step 3 */
9830                 for(v = parent->sdominates; v; v = next) {
9831                         struct sdom_block *u;
9832                         next = v->sdom_next;
9833                         unsdom_block(v);
9834                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9835                         v->block->idom = (u->sdom->vertex < v->sdom->vertex)? 
9836                                 u->block : parent->block;
9837                 }
9838         }
9839 }
9840
9841 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
9842 {
9843         int i;
9844         /* // step 2 
9845          *  for each v <= pred(w) {
9846          *      u = EVAL(v);
9847          *      if (semi[u] < semi[w] { 
9848          *              semi[w] = semi[u]; 
9849          *      } 
9850          * }
9851          * add w to bucket(vertex(semi[w]));
9852          * LINK(parent(w), w);
9853          *
9854          * // step 3
9855          * for each v <= bucket(parent(w)) {
9856          *      delete v from bucket(parent(w));
9857          *      u = EVAL(v);
9858          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
9859          * }
9860          */
9861         for(i = state->last_vertex; i >= 2; i--) {
9862                 struct sdom_block *u, *v, *parent, *next;
9863                 struct block *block;
9864                 block = sd[i].block;
9865                 parent = sd[i].parent;
9866                 /* Step 2 */
9867                 if (block->left) {
9868                         v = &sd[block->left->vertex];
9869                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9870                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9871                                 sd[i].sdom = u->sdom;
9872                         }
9873                 }
9874                 if (block->right && (block->right != block->left)) {
9875                         v = &sd[block->right->vertex];
9876                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
9877                         if (u->sdom->vertex < sd[i].sdom->vertex) {
9878                                 sd[i].sdom = u->sdom;
9879                         }
9880                 }
9881                 sdom_block(sd[i].sdom, &sd[i]);
9882                 sd[i].ancestor = parent;
9883                 /* Step 3 */
9884                 for(v = parent->sdominates; v; v = next) {
9885                         struct sdom_block *u;
9886                         next = v->sdom_next;
9887                         unsdom_block(v);
9888                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
9889                         v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? 
9890                                 u->block : parent->block;
9891                 }
9892         }
9893 }
9894
9895 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
9896 {
9897         int i;
9898         for(i = 2; i <= state->last_vertex; i++) {
9899                 struct block *block;
9900                 block = sd[i].block;
9901                 if (block->idom->vertex != sd[i].sdom->vertex) {
9902                         block->idom = block->idom->idom;
9903                 }
9904                 idom_block(block->idom, block);
9905         }
9906         sd[1].block->idom = 0;
9907 }
9908
9909 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
9910 {
9911         int i;
9912         for(i = 2; i <= state->last_vertex; i++) {
9913                 struct block *block;
9914                 block = sd[i].block;
9915                 if (block->ipdom->vertex != sd[i].sdom->vertex) {
9916                         block->ipdom = block->ipdom->ipdom;
9917                 }
9918                 ipdom_block(block->ipdom, block);
9919         }
9920         sd[1].block->ipdom = 0;
9921 }
9922
9923         /* Theorem 1:
9924          *   Every vertex of a flowgraph G = (V, E, r) except r has
9925          *   a unique immediate dominator.  
9926          *   The edges {(idom(w), w) |w <= V - {r}} form a directed tree
9927          *   rooted at r, called the dominator tree of G, such that 
9928          *   v dominates w if and only if v is a proper ancestor of w in
9929          *   the dominator tree.
9930          */
9931         /* Lemma 1:  
9932          *   If v and w are vertices of G such that v <= w,
9933          *   than any path from v to w must contain a common ancestor
9934          *   of v and w in T.
9935          */
9936         /* Lemma 2:  For any vertex w != r, idom(w) -> w */
9937         /* Lemma 3:  For any vertex w != r, sdom(w) -> w */
9938         /* Lemma 4:  For any vertex w != r, idom(w) -> sdom(w) */
9939         /* Theorem 2:
9940          *   Let w != r.  Suppose every u for which sdom(w) -> u -> w satisfies
9941          *   sdom(u) >= sdom(w).  Then idom(w) = sdom(w).
9942          */
9943         /* Theorem 3:
9944          *   Let w != r and let u be a vertex for which sdom(u) is 
9945          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
9946          *   Then sdom(u) <= sdom(w) and idom(u) = idom(w).
9947          */
9948         /* Lemma 5:  Let vertices v,w satisfy v -> w.
9949          *           Then v -> idom(w) or idom(w) -> idom(v)
9950          */
9951
9952 static void find_immediate_dominators(struct compile_state *state)
9953 {
9954         struct sdom_block *sd;
9955         /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
9956          *           vi > w for (1 <= i <= k - 1}
9957          */
9958         /* Theorem 4:
9959          *   For any vertex w != r.
9960          *   sdom(w) = min(
9961          *                 {v|(v,w) <= E  and v < w } U 
9962          *                 {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
9963          */
9964         /* Corollary 1:
9965          *   Let w != r and let u be a vertex for which sdom(u) is 
9966          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
9967          *   Then:
9968          *                   { sdom(w) if sdom(w) = sdom(u),
9969          *        idom(w) = {
9970          *                   { idom(u) otherwise
9971          */
9972         /* The algorithm consists of the following 4 steps.
9973          * Step 1.  Carry out a depth-first search of the problem graph.  
9974          *    Number the vertices from 1 to N as they are reached during
9975          *    the search.  Initialize the variables used in succeeding steps.
9976          * Step 2.  Compute the semidominators of all vertices by applying
9977          *    theorem 4.   Carry out the computation vertex by vertex in
9978          *    decreasing order by number.
9979          * Step 3.  Implicitly define the immediate dominator of each vertex
9980          *    by applying Corollary 1.
9981          * Step 4.  Explicitly define the immediate dominator of each vertex,
9982          *    carrying out the computation vertex by vertex in increasing order
9983          *    by number.
9984          */
9985         /* Step 1 initialize the basic block information */
9986         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
9987         initialize_sdblock(sd, 0, state->first_block, 0);
9988 #if 0
9989         sd[1].size  = 0;
9990         sd[1].label = 0;
9991         sd[1].sdom  = 0;
9992 #endif
9993         /* Step 2 compute the semidominators */
9994         /* Step 3 implicitly define the immediate dominator of each vertex */
9995         compute_sdom(state, sd);
9996         /* Step 4 explicitly define the immediate dominator of each vertex */
9997         compute_idom(state, sd);
9998         xfree(sd);
9999 }
10000
10001 static void find_post_dominators(struct compile_state *state)
10002 {
10003         struct sdom_block *sd;
10004         /* Step 1 initialize the basic block information */
10005         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10006
10007         initialize_sdpblock(sd, 0, state->last_block, 0);
10008
10009         /* Step 2 compute the semidominators */
10010         /* Step 3 implicitly define the immediate dominator of each vertex */
10011         compute_spdom(state, sd);
10012         /* Step 4 explicitly define the immediate dominator of each vertex */
10013         compute_ipdom(state, sd);
10014         xfree(sd);
10015 }
10016
10017
10018
10019 static void find_block_domf(struct compile_state *state, struct block *block)
10020 {
10021         struct block *child;
10022         struct block_set *user;
10023         if (block->domfrontier != 0) {
10024                 internal_error(state, block->first, "domfrontier present?");
10025         }
10026         for(user = block->idominates; user; user = user->next) {
10027                 child = user->member;
10028                 if (child->idom != block) {
10029                         internal_error(state, block->first, "bad idom");
10030                 }
10031                 find_block_domf(state, child);
10032         }
10033         if (block->left && block->left->idom != block) {
10034                 domf_block(block, block->left);
10035         }
10036         if (block->right && block->right->idom != block) {
10037                 domf_block(block, block->right);
10038         }
10039         for(user = block->idominates; user; user = user->next) {
10040                 struct block_set *frontier;
10041                 child = user->member;
10042                 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
10043                         if (frontier->member->idom != block) {
10044                                 domf_block(block, frontier->member);
10045                         }
10046                 }
10047         }
10048 }
10049
10050 static void find_block_ipdomf(struct compile_state *state, struct block *block)
10051 {
10052         struct block *child;
10053         struct block_set *user;
10054         if (block->ipdomfrontier != 0) {
10055                 internal_error(state, block->first, "ipdomfrontier present?");
10056         }
10057         for(user = block->ipdominates; user; user = user->next) {
10058                 child = user->member;
10059                 if (child->ipdom != block) {
10060                         internal_error(state, block->first, "bad ipdom");
10061                 }
10062                 find_block_ipdomf(state, child);
10063         }
10064         if (block->left && block->left->ipdom != block) {
10065                 ipdomf_block(block, block->left);
10066         }
10067         if (block->right && block->right->ipdom != block) {
10068                 ipdomf_block(block, block->right);
10069         }
10070         for(user = block->idominates; user; user = user->next) {
10071                 struct block_set *frontier;
10072                 child = user->member;
10073                 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10074                         if (frontier->member->ipdom != block) {
10075                                 ipdomf_block(block, frontier->member);
10076                         }
10077                 }
10078         }
10079 }
10080
10081 static void print_dominated(
10082         struct compile_state *state, struct block *block, void *arg)
10083 {
10084         struct block_set *user;
10085         FILE *fp = arg;
10086
10087         fprintf(fp, "%d:", block->vertex);
10088         for(user = block->idominates; user; user = user->next) {
10089                 fprintf(fp, " %d", user->member->vertex);
10090                 if (user->member->idom != block) {
10091                         internal_error(state, user->member->first, "bad idom");
10092                 }
10093         }
10094         fprintf(fp,"\n");
10095 }
10096
10097 static void print_dominators(struct compile_state *state, FILE *fp)
10098 {
10099         fprintf(fp, "\ndominates\n");
10100         walk_blocks(state, print_dominated, fp);
10101 }
10102
10103
10104 static int print_frontiers(
10105         struct compile_state *state, struct block *block, int vertex)
10106 {
10107         struct block_set *user;
10108
10109         if (!block || (block->vertex != vertex + 1)) {
10110                 return vertex;
10111         }
10112         vertex += 1;
10113
10114         printf("%d:", block->vertex);
10115         for(user = block->domfrontier; user; user = user->next) {
10116                 printf(" %d", user->member->vertex);
10117         }
10118         printf("\n");
10119
10120         vertex = print_frontiers(state, block->left, vertex);
10121         vertex = print_frontiers(state, block->right, vertex);
10122         return vertex;
10123 }
10124 static void print_dominance_frontiers(struct compile_state *state)
10125 {
10126         printf("\ndominance frontiers\n");
10127         print_frontiers(state, state->first_block, 0);
10128         
10129 }
10130
10131 static void analyze_idominators(struct compile_state *state)
10132 {
10133         /* Find the immediate dominators */
10134         find_immediate_dominators(state);
10135         /* Find the dominance frontiers */
10136         find_block_domf(state, state->first_block);
10137         /* If debuging print the print what I have just found */
10138         if (state->debug & DEBUG_FDOMINATORS) {
10139                 print_dominators(state, stdout);
10140                 print_dominance_frontiers(state);
10141                 print_control_flow(state);
10142         }
10143 }
10144
10145
10146
10147 static void print_ipdominated(
10148         struct compile_state *state, struct block *block, void *arg)
10149 {
10150         struct block_set *user;
10151         FILE *fp = arg;
10152
10153         fprintf(fp, "%d:", block->vertex);
10154         for(user = block->ipdominates; user; user = user->next) {
10155                 fprintf(fp, " %d", user->member->vertex);
10156                 if (user->member->ipdom != block) {
10157                         internal_error(state, user->member->first, "bad ipdom");
10158                 }
10159         }
10160         fprintf(fp, "\n");
10161 }
10162
10163 static void print_ipdominators(struct compile_state *state, FILE *fp)
10164 {
10165         fprintf(fp, "\nipdominates\n");
10166         walk_blocks(state, print_ipdominated, fp);
10167 }
10168
10169 static int print_pfrontiers(
10170         struct compile_state *state, struct block *block, int vertex)
10171 {
10172         struct block_set *user;
10173
10174         if (!block || (block->vertex != vertex + 1)) {
10175                 return vertex;
10176         }
10177         vertex += 1;
10178
10179         printf("%d:", block->vertex);
10180         for(user = block->ipdomfrontier; user; user = user->next) {
10181                 printf(" %d", user->member->vertex);
10182         }
10183         printf("\n");
10184         for(user = block->use; user; user = user->next) {
10185                 vertex = print_pfrontiers(state, user->member, vertex);
10186         }
10187         return vertex;
10188 }
10189 static void print_ipdominance_frontiers(struct compile_state *state)
10190 {
10191         printf("\nipdominance frontiers\n");
10192         print_pfrontiers(state, state->last_block, 0);
10193         
10194 }
10195
10196 static void analyze_ipdominators(struct compile_state *state)
10197 {
10198         /* Find the post dominators */
10199         find_post_dominators(state);
10200         /* Find the control dependencies (post dominance frontiers) */
10201         find_block_ipdomf(state, state->last_block);
10202         /* If debuging print the print what I have just found */
10203         if (state->debug & DEBUG_RDOMINATORS) {
10204                 print_ipdominators(state, stdout);
10205                 print_ipdominance_frontiers(state);
10206                 print_control_flow(state);
10207         }
10208 }
10209
10210 static int bdominates(struct compile_state *state,
10211         struct block *dom, struct block *sub)
10212 {
10213         while(sub && (sub != dom)) {
10214                 sub = sub->idom;
10215         }
10216         return sub == dom;
10217 }
10218
10219 static int tdominates(struct compile_state *state,
10220         struct triple *dom, struct triple *sub)
10221 {
10222         struct block *bdom, *bsub;
10223         int result;
10224         bdom = block_of_triple(state, dom);
10225         bsub = block_of_triple(state, sub);
10226         if (bdom != bsub) {
10227                 result = bdominates(state, bdom, bsub);
10228         } 
10229         else {
10230                 struct triple *ins;
10231                 ins = sub;
10232                 while((ins != bsub->first) && (ins != dom)) {
10233                         ins = ins->prev;
10234                 }
10235                 result = (ins == dom);
10236         }
10237         return result;
10238 }
10239
10240 static void insert_phi_operations(struct compile_state *state)
10241 {
10242         size_t size;
10243         struct triple *first;
10244         int *has_already, *work;
10245         struct block *work_list, **work_list_tail;
10246         int iter;
10247         struct triple *var;
10248
10249         size = sizeof(int) * (state->last_vertex + 1);
10250         has_already = xcmalloc(size, "has_already");
10251         work =        xcmalloc(size, "work");
10252         iter = 0;
10253
10254         first = RHS(state->main_function, 0);
10255         for(var = first->next; var != first ; var = var->next) {
10256                 struct block *block;
10257                 struct triple_set *user;
10258                 if ((var->op != OP_ADECL) || !var->use) {
10259                         continue;
10260                 }
10261                 iter += 1;
10262                 work_list = 0;
10263                 work_list_tail = &work_list;
10264                 for(user = var->use; user; user = user->next) {
10265                         if (user->member->op == OP_READ) {
10266                                 continue;
10267                         }
10268                         if (user->member->op != OP_WRITE) {
10269                                 internal_error(state, user->member, 
10270                                         "bad variable access");
10271                         }
10272                         block = user->member->u.block;
10273                         if (!block) {
10274                                 warning(state, user->member, "dead code");
10275                         }
10276                         if (work[block->vertex] >= iter) {
10277                                 continue;
10278                         }
10279                         work[block->vertex] = iter;
10280                         *work_list_tail = block;
10281                         block->work_next = 0;
10282                         work_list_tail = &block->work_next;
10283                 }
10284                 for(block = work_list; block; block = block->work_next) {
10285                         struct block_set *df;
10286                         for(df = block->domfrontier; df; df = df->next) {
10287                                 struct triple *phi;
10288                                 struct block *front;
10289                                 int in_edges;
10290                                 front = df->member;
10291
10292                                 if (has_already[front->vertex] >= iter) {
10293                                         continue;
10294                                 }
10295                                 /* Count how many edges flow into this block */
10296                                 in_edges = front->users;
10297                                 /* Insert a phi function for this variable */
10298                                 get_occurance(front->first->occurance);
10299                                 phi = alloc_triple(
10300                                         state, OP_PHI, var->type, -1, in_edges, 
10301                                         front->first->occurance);
10302                                 phi->u.block = front;
10303                                 MISC(phi, 0) = var;
10304                                 use_triple(var, phi);
10305                                 /* Insert the phi functions immediately after the label */
10306                                 insert_triple(state, front->first->next, phi);
10307                                 if (front->first == front->last) {
10308                                         front->last = front->first->next;
10309                                 }
10310                                 has_already[front->vertex] = iter;
10311
10312                                 /* If necessary plan to visit the basic block */
10313                                 if (work[front->vertex] >= iter) {
10314                                         continue;
10315                                 }
10316                                 work[front->vertex] = iter;
10317                                 *work_list_tail = front;
10318                                 front->work_next = 0;
10319                                 work_list_tail = &front->work_next;
10320                         }
10321                 }
10322         }
10323         xfree(has_already);
10324         xfree(work);
10325 }
10326
10327 /*
10328  * C(V)
10329  * S(V)
10330  */
10331 static void fixup_block_phi_variables(
10332         struct compile_state *state, struct block *parent, struct block *block)
10333 {
10334         struct block_set *set;
10335         struct triple *ptr;
10336         int edge;
10337         if (!parent || !block)
10338                 return;
10339         /* Find the edge I am coming in on */
10340         edge = 0;
10341         for(set = block->use; set; set = set->next, edge++) {
10342                 if (set->member == parent) {
10343                         break;
10344                 }
10345         }
10346         if (!set) {
10347                 internal_error(state, 0, "phi input is not on a control predecessor");
10348         }
10349         for(ptr = block->first; ; ptr = ptr->next) {
10350                 if (ptr->op == OP_PHI) {
10351                         struct triple *var, *val, **slot;
10352                         var = MISC(ptr, 0);
10353                         if (!var) {
10354                                 internal_error(state, ptr, "no var???");
10355                         }
10356                         /* Find the current value of the variable */
10357                         val = var->use->member;
10358                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10359                                 internal_error(state, val, "bad value in phi");
10360                         }
10361                         if (edge >= TRIPLE_RHS(ptr->sizes)) {
10362                                 internal_error(state, ptr, "edges > phi rhs");
10363                         }
10364                         slot = &RHS(ptr, edge);
10365                         if ((*slot != 0) && (*slot != val)) {
10366                                 internal_error(state, ptr, "phi already bound on this edge");
10367                         }
10368                         *slot = val;
10369                         use_triple(val, ptr);
10370                 }
10371                 if (ptr == block->last) {
10372                         break;
10373                 }
10374         }
10375 }
10376
10377
10378 static void rename_block_variables(
10379         struct compile_state *state, struct block *block)
10380 {
10381         struct block_set *user;
10382         struct triple *ptr, *next, *last;
10383         int done;
10384         if (!block)
10385                 return;
10386         last = block->first;
10387         done = 0;
10388         for(ptr = block->first; !done; ptr = next) {
10389                 next = ptr->next;
10390                 if (ptr == block->last) {
10391                         done = 1;
10392                 }
10393                 /* RHS(A) */
10394                 if (ptr->op == OP_READ) {
10395                         struct triple *var, *val;
10396                         var = RHS(ptr, 0);
10397                         unuse_triple(var, ptr);
10398                         if (!var->use) {
10399                                 error(state, ptr, "variable used without being set");
10400                         }
10401                         /* Find the current value of the variable */
10402                         val = var->use->member;
10403                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10404                                 internal_error(state, val, "bad value in read");
10405                         }
10406                         propogate_use(state, ptr, val);
10407                         release_triple(state, ptr);
10408                         continue;
10409                 }
10410                 /* LHS(A) */
10411                 if (ptr->op == OP_WRITE) {
10412                         struct triple *var, *val;
10413                         var = LHS(ptr, 0);
10414                         val = RHS(ptr, 0);
10415                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10416                                 internal_error(state, val, "bad value in write");
10417                         }
10418                         propogate_use(state, ptr, val);
10419                         unuse_triple(var, ptr);
10420                         /* Push OP_WRITE ptr->right onto a stack of variable uses */
10421                         push_triple(var, val);
10422                 }
10423                 if (ptr->op == OP_PHI) {
10424                         struct triple *var;
10425                         var = MISC(ptr, 0);
10426                         /* Push OP_PHI onto a stack of variable uses */
10427                         push_triple(var, ptr);
10428                 }
10429                 last = ptr;
10430         }
10431         block->last = last;
10432
10433         /* Fixup PHI functions in the cf successors */
10434         fixup_block_phi_variables(state, block, block->left);
10435         fixup_block_phi_variables(state, block, block->right);
10436         /* rename variables in the dominated nodes */
10437         for(user = block->idominates; user; user = user->next) {
10438                 rename_block_variables(state, user->member);
10439         }
10440         /* pop the renamed variable stack */
10441         last = block->first;
10442         done = 0;
10443         for(ptr = block->first; !done ; ptr = next) {
10444                 next = ptr->next;
10445                 if (ptr == block->last) {
10446                         done = 1;
10447                 }
10448                 if (ptr->op == OP_WRITE) {
10449                         struct triple *var;
10450                         var = LHS(ptr, 0);
10451                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10452                         pop_triple(var, RHS(ptr, 0));
10453                         release_triple(state, ptr);
10454                         continue;
10455                 }
10456                 if (ptr->op == OP_PHI) {
10457                         struct triple *var;
10458                         var = MISC(ptr, 0);
10459                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10460                         pop_triple(var, ptr);
10461                 }
10462                 last = ptr;
10463         }
10464         block->last = last;
10465 }
10466
10467 static void prune_block_variables(struct compile_state *state,
10468         struct block *block)
10469 {
10470         struct block_set *user;
10471         struct triple *next, *last, *ptr;
10472         int done;
10473         last = block->first;
10474         done = 0;
10475         for(ptr = block->first; !done; ptr = next) {
10476                 next = ptr->next;
10477                 if (ptr == block->last) {
10478                         done = 1;
10479                 }
10480                 if (ptr->op == OP_ADECL) {
10481                         struct triple_set *user, *next;
10482                         for(user = ptr->use; user; user = next) {
10483                                 struct triple *use;
10484                                 next = user->next;
10485                                 use = user->member;
10486                                 if (use->op != OP_PHI) {
10487                                         internal_error(state, use, "decl still used");
10488                                 }
10489                                 if (MISC(use, 0) != ptr) {
10490                                         internal_error(state, use, "bad phi use of decl");
10491                                 }
10492                                 unuse_triple(ptr, use);
10493                                 MISC(use, 0) = 0;
10494                         }
10495                         release_triple(state, ptr);
10496                         continue;
10497                 }
10498                 last = ptr;
10499         }
10500         block->last = last;
10501         for(user = block->idominates; user; user = user->next) {
10502                 prune_block_variables(state, user->member);
10503         }
10504 }
10505
10506 static void transform_to_ssa_form(struct compile_state *state)
10507 {
10508         insert_phi_operations(state);
10509 #if 0
10510         printf("@%s:%d\n", __FILE__, __LINE__);
10511         print_blocks(state, stdout);
10512 #endif
10513         rename_block_variables(state, state->first_block);
10514         prune_block_variables(state, state->first_block);
10515 }
10516
10517
10518 static void clear_vertex(
10519         struct compile_state *state, struct block *block, void *arg)
10520 {
10521         block->vertex = 0;
10522 }
10523
10524 static void mark_live_block(
10525         struct compile_state *state, struct block *block, int *next_vertex)
10526 {
10527         /* See if this is a block that has not been marked */
10528         if (block->vertex != 0) {
10529                 return;
10530         }
10531         block->vertex = *next_vertex;
10532         *next_vertex += 1;
10533         if (triple_is_branch(state, block->last)) {
10534                 struct triple **targ;
10535                 targ = triple_targ(state, block->last, 0);
10536                 for(; targ; targ = triple_targ(state, block->last, targ)) {
10537                         if (!*targ) {
10538                                 continue;
10539                         }
10540                         if (!triple_stores_block(state, *targ)) {
10541                                 internal_error(state, 0, "bad targ");
10542                         }
10543                         mark_live_block(state, (*targ)->u.block, next_vertex);
10544                 }
10545         }
10546         else if (block->last->next != RHS(state->main_function, 0)) {
10547                 struct triple *ins;
10548                 ins = block->last->next;
10549                 if (!triple_stores_block(state, ins)) {
10550                         internal_error(state, 0, "bad block start");
10551                 }
10552                 mark_live_block(state, ins->u.block, next_vertex);
10553         }
10554 }
10555
10556 static void transform_from_ssa_form(struct compile_state *state)
10557 {
10558         /* To get out of ssa form we insert moves on the incoming
10559          * edges to blocks containting phi functions.
10560          */
10561         struct triple *first;
10562         struct triple *phi, *next;
10563         int next_vertex;
10564
10565         /* Walk the control flow to see which blocks remain alive */
10566         walk_blocks(state, clear_vertex, 0);
10567         next_vertex = 1;
10568         mark_live_block(state, state->first_block, &next_vertex);
10569
10570         /* Walk all of the operations to find the phi functions */
10571         first = RHS(state->main_function, 0);
10572         for(phi = first->next; phi != first ; phi = next) {
10573                 struct block_set *set;
10574                 struct block *block;
10575                 struct triple **slot;
10576                 struct triple *var, *read;
10577                 struct triple_set *use, *use_next;
10578                 int edge, used;
10579                 next = phi->next;
10580                 if (phi->op != OP_PHI) {
10581                         continue;
10582                 }
10583                 block = phi->u.block;
10584                 slot  = &RHS(phi, 0);
10585
10586                 /* Forget uses from code in dead blocks */
10587                 for(use = phi->use; use; use = use_next) {
10588                         struct block *ublock;
10589                         struct triple **expr;
10590                         use_next = use->next;
10591                         ublock = block_of_triple(state, use->member);
10592                         if ((use->member == phi) || (ublock->vertex != 0)) {
10593                                 continue;
10594                         }
10595                         expr = triple_rhs(state, use->member, 0);
10596                         for(; expr; expr = triple_rhs(state, use->member, expr)) {
10597                                 if (*expr == phi) {
10598                                         *expr = 0;
10599                                 }
10600                         }
10601                         unuse_triple(phi, use->member);
10602                 }
10603
10604                 /* A variable to replace the phi function */
10605                 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
10606                 /* A read of the single value that is set into the variable */
10607                 read = post_triple(state, var, OP_READ, phi->type, var, 0);
10608                 use_triple(var, read);
10609
10610                 /* Replaces uses of the phi with variable reads */
10611                 propogate_use(state, phi, read);
10612
10613                 /* Walk all of the incoming edges/blocks and insert moves.
10614                  */
10615                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10616                         struct block *eblock;
10617                         struct triple *move;
10618                         struct triple *val;
10619                         eblock = set->member;
10620                         val = slot[edge];
10621                         slot[edge] = 0;
10622                         unuse_triple(val, phi);
10623
10624                         if (!val || (val == &zero_triple) ||
10625                                 (block->vertex == 0) || (eblock->vertex == 0) ||
10626                                 (val == phi) || (val == read)) {
10627                                 continue;
10628                         }
10629                         
10630                         move = post_triple(state, 
10631                                 val, OP_WRITE, phi->type, var, val);
10632                         use_triple(val, move);
10633                         use_triple(var, move);
10634                 }               
10635                 /* See if there are any writers of var */
10636                 used = 0;
10637                 for(use = var->use; use; use = use->next) {
10638                         struct triple **expr;
10639                         expr = triple_lhs(state, use->member, 0);
10640                         for(; expr; expr = triple_lhs(state, use->member, expr)) {
10641                                 if (*expr == var) {
10642                                         used = 1;
10643                                 }
10644                         }
10645                 }
10646                 /* If var is not used free it */
10647                 if (!used) {
10648                         unuse_triple(var, read);
10649                         free_triple(state, read);
10650                         free_triple(state, var);
10651                 }
10652
10653                 /* Release the phi function */
10654                 release_triple(state, phi);
10655         }
10656         
10657 }
10658
10659
10660 /* 
10661  * Register conflict resolution
10662  * =========================================================
10663  */
10664
10665 static struct reg_info find_def_color(
10666         struct compile_state *state, struct triple *def)
10667 {
10668         struct triple_set *set;
10669         struct reg_info info;
10670         info.reg = REG_UNSET;
10671         info.regcm = 0;
10672         if (!triple_is_def(state, def)) {
10673                 return info;
10674         }
10675         info = arch_reg_lhs(state, def, 0);
10676         if (info.reg >= MAX_REGISTERS) {
10677                 info.reg = REG_UNSET;
10678         }
10679         for(set = def->use; set; set = set->next) {
10680                 struct reg_info tinfo;
10681                 int i;
10682                 i = find_rhs_use(state, set->member, def);
10683                 if (i < 0) {
10684                         continue;
10685                 }
10686                 tinfo = arch_reg_rhs(state, set->member, i);
10687                 if (tinfo.reg >= MAX_REGISTERS) {
10688                         tinfo.reg = REG_UNSET;
10689                 }
10690                 if ((tinfo.reg != REG_UNSET) && 
10691                         (info.reg != REG_UNSET) &&
10692                         (tinfo.reg != info.reg)) {
10693                         internal_error(state, def, "register conflict");
10694                 }
10695                 if ((info.regcm & tinfo.regcm) == 0) {
10696                         internal_error(state, def, "regcm conflict %x & %x == 0",
10697                                 info.regcm, tinfo.regcm);
10698                 }
10699                 if (info.reg == REG_UNSET) {
10700                         info.reg = tinfo.reg;
10701                 }
10702                 info.regcm &= tinfo.regcm;
10703         }
10704         if (info.reg >= MAX_REGISTERS) {
10705                 internal_error(state, def, "register out of range");
10706         }
10707         return info;
10708 }
10709
10710 static struct reg_info find_lhs_pre_color(
10711         struct compile_state *state, struct triple *ins, int index)
10712 {
10713         struct reg_info info;
10714         int zlhs, zrhs, i;
10715         zrhs = TRIPLE_RHS(ins->sizes);
10716         zlhs = TRIPLE_LHS(ins->sizes);
10717         if (!zlhs && triple_is_def(state, ins)) {
10718                 zlhs = 1;
10719         }
10720         if (index >= zlhs) {
10721                 internal_error(state, ins, "Bad lhs %d", index);
10722         }
10723         info = arch_reg_lhs(state, ins, index);
10724         for(i = 0; i < zrhs; i++) {
10725                 struct reg_info rinfo;
10726                 rinfo = arch_reg_rhs(state, ins, i);
10727                 if ((info.reg == rinfo.reg) &&
10728                         (rinfo.reg >= MAX_REGISTERS)) {
10729                         struct reg_info tinfo;
10730                         tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
10731                         info.reg = tinfo.reg;
10732                         info.regcm &= tinfo.regcm;
10733                         break;
10734                 }
10735         }
10736         if (info.reg >= MAX_REGISTERS) {
10737                 info.reg = REG_UNSET;
10738         }
10739         return info;
10740 }
10741
10742 static struct reg_info find_rhs_post_color(
10743         struct compile_state *state, struct triple *ins, int index);
10744
10745 static struct reg_info find_lhs_post_color(
10746         struct compile_state *state, struct triple *ins, int index)
10747 {
10748         struct triple_set *set;
10749         struct reg_info info;
10750         struct triple *lhs;
10751 #if 0
10752         fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
10753                 ins, index);
10754 #endif
10755         if ((index == 0) && triple_is_def(state, ins)) {
10756                 lhs = ins;
10757         }
10758         else if (index < TRIPLE_LHS(ins->sizes)) {
10759                 lhs = LHS(ins, index);
10760         }
10761         else {
10762                 internal_error(state, ins, "Bad lhs %d", index);
10763                 lhs = 0;
10764         }
10765         info = arch_reg_lhs(state, ins, index);
10766         if (info.reg >= MAX_REGISTERS) {
10767                 info.reg = REG_UNSET;
10768         }
10769         for(set = lhs->use; set; set = set->next) {
10770                 struct reg_info rinfo;
10771                 struct triple *user;
10772                 int zrhs, i;
10773                 user = set->member;
10774                 zrhs = TRIPLE_RHS(user->sizes);
10775                 for(i = 0; i < zrhs; i++) {
10776                         if (RHS(user, i) != lhs) {
10777                                 continue;
10778                         }
10779                         rinfo = find_rhs_post_color(state, user, i);
10780                         if ((info.reg != REG_UNSET) &&
10781                                 (rinfo.reg != REG_UNSET) &&
10782                                 (info.reg != rinfo.reg)) {
10783                                 internal_error(state, ins, "register conflict");
10784                         }
10785                         if ((info.regcm & rinfo.regcm) == 0) {
10786                                 internal_error(state, ins, "regcm conflict %x & %x == 0",
10787                                         info.regcm, rinfo.regcm);
10788                         }
10789                         if (info.reg == REG_UNSET) {
10790                                 info.reg = rinfo.reg;
10791                         }
10792                         info.regcm &= rinfo.regcm;
10793                 }
10794         }
10795 #if 0
10796         fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
10797                 ins, index, info.reg, info.regcm);
10798 #endif
10799         return info;
10800 }
10801
10802 static struct reg_info find_rhs_post_color(
10803         struct compile_state *state, struct triple *ins, int index)
10804 {
10805         struct reg_info info, rinfo;
10806         int zlhs, i;
10807 #if 0
10808         fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
10809                 ins, index);
10810 #endif
10811         rinfo = arch_reg_rhs(state, ins, index);
10812         zlhs = TRIPLE_LHS(ins->sizes);
10813         if (!zlhs && triple_is_def(state, ins)) {
10814                 zlhs = 1;
10815         }
10816         info = rinfo;
10817         if (info.reg >= MAX_REGISTERS) {
10818                 info.reg = REG_UNSET;
10819         }
10820         for(i = 0; i < zlhs; i++) {
10821                 struct reg_info linfo;
10822                 linfo = arch_reg_lhs(state, ins, i);
10823                 if ((linfo.reg == rinfo.reg) &&
10824                         (linfo.reg >= MAX_REGISTERS)) {
10825                         struct reg_info tinfo;
10826                         tinfo = find_lhs_post_color(state, ins, i);
10827                         if (tinfo.reg >= MAX_REGISTERS) {
10828                                 tinfo.reg = REG_UNSET;
10829                         }
10830                         info.regcm &= linfo.reg;
10831                         info.regcm &= tinfo.regcm;
10832                         if (info.reg != REG_UNSET) {
10833                                 internal_error(state, ins, "register conflict");
10834                         }
10835                         if (info.regcm == 0) {
10836                                 internal_error(state, ins, "regcm conflict");
10837                         }
10838                         info.reg = tinfo.reg;
10839                 }
10840         }
10841 #if 0
10842         fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
10843                 ins, index, info.reg, info.regcm);
10844 #endif
10845         return info;
10846 }
10847
10848 static struct reg_info find_lhs_color(
10849         struct compile_state *state, struct triple *ins, int index)
10850 {
10851         struct reg_info pre, post, info;
10852 #if 0
10853         fprintf(stderr, "find_lhs_color(%p, %d)\n",
10854                 ins, index);
10855 #endif
10856         pre = find_lhs_pre_color(state, ins, index);
10857         post = find_lhs_post_color(state, ins, index);
10858         if ((pre.reg != post.reg) &&
10859                 (pre.reg != REG_UNSET) &&
10860                 (post.reg != REG_UNSET)) {
10861                 internal_error(state, ins, "register conflict");
10862         }
10863         info.regcm = pre.regcm & post.regcm;
10864         info.reg = pre.reg;
10865         if (info.reg == REG_UNSET) {
10866                 info.reg = post.reg;
10867         }
10868 #if 0
10869         fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x)\n",
10870                 ins, index, info.reg, info.regcm);
10871 #endif
10872         return info;
10873 }
10874
10875 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
10876 {
10877         struct triple_set *entry, *next;
10878         struct triple *out;
10879         struct reg_info info, rinfo;
10880
10881         info = arch_reg_lhs(state, ins, 0);
10882         out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
10883         use_triple(RHS(out, 0), out);
10884         /* Get the users of ins to use out instead */
10885         for(entry = ins->use; entry; entry = next) {
10886                 int i;
10887                 next = entry->next;
10888                 if (entry->member == out) {
10889                         continue;
10890                 }
10891                 i = find_rhs_use(state, entry->member, ins);
10892                 if (i < 0) {
10893                         continue;
10894                 }
10895                 rinfo = arch_reg_rhs(state, entry->member, i);
10896                 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
10897                         continue;
10898                 }
10899                 replace_rhs_use(state, ins, out, entry->member);
10900         }
10901         transform_to_arch_instruction(state, out);
10902         return out;
10903 }
10904
10905 static struct triple *pre_copy(
10906         struct compile_state *state, struct triple *ins, int index)
10907 {
10908         /* Carefully insert enough operations so that I can
10909          * enter any operation with a GPR32.
10910          */
10911         struct triple *in;
10912         struct triple **expr;
10913         if (ins->op == OP_PHI) {
10914                 internal_error(state, ins, "pre_copy on a phi?");
10915         }
10916         expr = &RHS(ins, index);
10917         in = pre_triple(state, ins, OP_COPY, (*expr)->type, *expr, 0);
10918         unuse_triple(*expr, ins);
10919         *expr = in;
10920         use_triple(RHS(in, 0), in);
10921         use_triple(in, ins);
10922         transform_to_arch_instruction(state, in);
10923         return in;
10924 }
10925
10926
10927 static void insert_copies_to_phi(struct compile_state *state)
10928 {
10929         /* To get out of ssa form we insert moves on the incoming
10930          * edges to blocks containting phi functions.
10931          */
10932         struct triple *first;
10933         struct triple *phi;
10934
10935         /* Walk all of the operations to find the phi functions */
10936         first = RHS(state->main_function, 0);
10937         for(phi = first->next; phi != first ; phi = phi->next) {
10938                 struct block_set *set;
10939                 struct block *block;
10940                 struct triple **slot;
10941                 int edge;
10942                 if (phi->op != OP_PHI) {
10943                         continue;
10944                 }
10945                 phi->id |= TRIPLE_FLAG_POST_SPLIT;
10946                 block = phi->u.block;
10947                 slot  = &RHS(phi, 0);
10948                 /* Walk all of the incoming edges/blocks and insert moves.
10949                  */
10950                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
10951                         struct block *eblock;
10952                         struct triple *move;
10953                         struct triple *val;
10954                         struct triple *ptr;
10955                         eblock = set->member;
10956                         val = slot[edge];
10957
10958                         if (val == phi) {
10959                                 continue;
10960                         }
10961
10962                         get_occurance(val->occurance);
10963                         move = build_triple(state, OP_COPY, phi->type, val, 0,
10964                                 val->occurance);
10965                         move->u.block = eblock;
10966                         move->id |= TRIPLE_FLAG_PRE_SPLIT;
10967                         use_triple(val, move);
10968                         
10969                         slot[edge] = move;
10970                         unuse_triple(val, phi);
10971                         use_triple(move, phi);
10972
10973                         /* Walk through the block backwards to find
10974                          * an appropriate location for the OP_COPY.
10975                          */
10976                         for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
10977                                 struct triple **expr;
10978                                 if ((ptr == phi) || (ptr == val)) {
10979                                         goto out;
10980                                 }
10981                                 expr = triple_rhs(state, ptr, 0);
10982                                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
10983                                         if ((*expr) == phi) {
10984                                                 goto out;
10985                                         }
10986                                 }
10987                         }
10988                 out:
10989                         if (triple_is_branch(state, ptr)) {
10990                                 internal_error(state, ptr,
10991                                         "Could not insert write to phi");
10992                         }
10993                         insert_triple(state, ptr->next, move);
10994                         if (eblock->last == ptr) {
10995                                 eblock->last = move;
10996                         }
10997                         transform_to_arch_instruction(state, move);
10998                 }
10999         }
11000 }
11001
11002 struct triple_reg_set {
11003         struct triple_reg_set *next;
11004         struct triple *member;
11005         struct triple *new;
11006 };
11007
11008 struct reg_block {
11009         struct block *block;
11010         struct triple_reg_set *in;
11011         struct triple_reg_set *out;
11012         int vertex;
11013 };
11014
11015 static int do_triple_set(struct triple_reg_set **head, 
11016         struct triple *member, struct triple *new_member)
11017 {
11018         struct triple_reg_set **ptr, *new;
11019         if (!member)
11020                 return 0;
11021         ptr = head;
11022         while(*ptr) {
11023                 if ((*ptr)->member == member) {
11024                         return 0;
11025                 }
11026                 ptr = &(*ptr)->next;
11027         }
11028         new = xcmalloc(sizeof(*new), "triple_set");
11029         new->member = member;
11030         new->new    = new_member;
11031         new->next   = *head;
11032         *head       = new;
11033         return 1;
11034 }
11035
11036 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
11037 {
11038         struct triple_reg_set *entry, **ptr;
11039         ptr = head;
11040         while(*ptr) {
11041                 entry = *ptr;
11042                 if (entry->member == member) {
11043                         *ptr = entry->next;
11044                         xfree(entry);
11045                         return;
11046                 }
11047                 else {
11048                         ptr = &entry->next;
11049                 }
11050         }
11051 }
11052
11053 static int in_triple(struct reg_block *rb, struct triple *in)
11054 {
11055         return do_triple_set(&rb->in, in, 0);
11056 }
11057 static void unin_triple(struct reg_block *rb, struct triple *unin)
11058 {
11059         do_triple_unset(&rb->in, unin);
11060 }
11061
11062 static int out_triple(struct reg_block *rb, struct triple *out)
11063 {
11064         return do_triple_set(&rb->out, out, 0);
11065 }
11066 static void unout_triple(struct reg_block *rb, struct triple *unout)
11067 {
11068         do_triple_unset(&rb->out, unout);
11069 }
11070
11071 static int initialize_regblock(struct reg_block *blocks,
11072         struct block *block, int vertex)
11073 {
11074         struct block_set *user;
11075         if (!block || (blocks[block->vertex].block == block)) {
11076                 return vertex;
11077         }
11078         vertex += 1;
11079         /* Renumber the blocks in a convinient fashion */
11080         block->vertex = vertex;
11081         blocks[vertex].block    = block;
11082         blocks[vertex].vertex   = vertex;
11083         for(user = block->use; user; user = user->next) {
11084                 vertex = initialize_regblock(blocks, user->member, vertex);
11085         }
11086         return vertex;
11087 }
11088
11089 static int phi_in(struct compile_state *state, struct reg_block *blocks,
11090         struct reg_block *rb, struct block *suc)
11091 {
11092         /* Read the conditional input set of a successor block
11093          * (i.e. the input to the phi nodes) and place it in the
11094          * current blocks output set.
11095          */
11096         struct block_set *set;
11097         struct triple *ptr;
11098         int edge;
11099         int done, change;
11100         change = 0;
11101         /* Find the edge I am coming in on */
11102         for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11103                 if (set->member == rb->block) {
11104                         break;
11105                 }
11106         }
11107         if (!set) {
11108                 internal_error(state, 0, "Not coming on a control edge?");
11109         }
11110         for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11111                 struct triple **slot, *expr, *ptr2;
11112                 int out_change, done2;
11113                 done = (ptr == suc->last);
11114                 if (ptr->op != OP_PHI) {
11115                         continue;
11116                 }
11117                 slot = &RHS(ptr, 0);
11118                 expr = slot[edge];
11119                 out_change = out_triple(rb, expr);
11120                 if (!out_change) {
11121                         continue;
11122                 }
11123                 /* If we don't define the variable also plast it
11124                  * in the current blocks input set.
11125                  */
11126                 ptr2 = rb->block->first;
11127                 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11128                         if (ptr2 == expr) {
11129                                 break;
11130                         }
11131                         done2 = (ptr2 == rb->block->last);
11132                 }
11133                 if (!done2) {
11134                         continue;
11135                 }
11136                 change |= in_triple(rb, expr);
11137         }
11138         return change;
11139 }
11140
11141 static int reg_in(struct compile_state *state, struct reg_block *blocks,
11142         struct reg_block *rb, struct block *suc)
11143 {
11144         struct triple_reg_set *in_set;
11145         int change;
11146         change = 0;
11147         /* Read the input set of a successor block
11148          * and place it in the current blocks output set.
11149          */
11150         in_set = blocks[suc->vertex].in;
11151         for(; in_set; in_set = in_set->next) {
11152                 int out_change, done;
11153                 struct triple *first, *last, *ptr;
11154                 out_change = out_triple(rb, in_set->member);
11155                 if (!out_change) {
11156                         continue;
11157                 }
11158                 /* If we don't define the variable also place it
11159                  * in the current blocks input set.
11160                  */
11161                 first = rb->block->first;
11162                 last = rb->block->last;
11163                 done = 0;
11164                 for(ptr = first; !done; ptr = ptr->next) {
11165                         if (ptr == in_set->member) {
11166                                 break;
11167                         }
11168                         done = (ptr == last);
11169                 }
11170                 if (!done) {
11171                         continue;
11172                 }
11173                 change |= in_triple(rb, in_set->member);
11174         }
11175         change |= phi_in(state, blocks, rb, suc);
11176         return change;
11177 }
11178
11179
11180 static int use_in(struct compile_state *state, struct reg_block *rb)
11181 {
11182         /* Find the variables we use but don't define and add
11183          * it to the current blocks input set.
11184          */
11185 #warning "FIXME is this O(N^2) algorithm bad?"
11186         struct block *block;
11187         struct triple *ptr;
11188         int done;
11189         int change;
11190         block = rb->block;
11191         change = 0;
11192         for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11193                 struct triple **expr;
11194                 done = (ptr == block->first);
11195                 /* The variable a phi function uses depends on the
11196                  * control flow, and is handled in phi_in, not
11197                  * here.
11198                  */
11199                 if (ptr->op == OP_PHI) {
11200                         continue;
11201                 }
11202                 expr = triple_rhs(state, ptr, 0);
11203                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11204                         struct triple *rhs, *test;
11205                         int tdone;
11206                         rhs = *expr;
11207                         if (!rhs) {
11208                                 continue;
11209                         }
11210                         /* See if rhs is defined in this block */
11211                         for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11212                                 tdone = (test == block->first);
11213                                 if (test == rhs) {
11214                                         rhs = 0;
11215                                         break;
11216                                 }
11217                         }
11218                         /* If I still have a valid rhs add it to in */
11219                         change |= in_triple(rb, rhs);
11220                 }
11221         }
11222         return change;
11223 }
11224
11225 static struct reg_block *compute_variable_lifetimes(
11226         struct compile_state *state)
11227 {
11228         struct reg_block *blocks;
11229         int change;
11230         blocks = xcmalloc(
11231                 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11232         initialize_regblock(blocks, state->last_block, 0);
11233         do {
11234                 int i;
11235                 change = 0;
11236                 for(i = 1; i <= state->last_vertex; i++) {
11237                         struct reg_block *rb;
11238                         rb = &blocks[i];
11239                         /* Add the left successor's input set to in */
11240                         if (rb->block->left) {
11241                                 change |= reg_in(state, blocks, rb, rb->block->left);
11242                         }
11243                         /* Add the right successor's input set to in */
11244                         if ((rb->block->right) && 
11245                                 (rb->block->right != rb->block->left)) {
11246                                 change |= reg_in(state, blocks, rb, rb->block->right);
11247                         }
11248                         /* Add use to in... */
11249                         change |= use_in(state, rb);
11250                 }
11251         } while(change);
11252         return blocks;
11253 }
11254
11255 static void free_variable_lifetimes(
11256         struct compile_state *state, struct reg_block *blocks)
11257 {
11258         int i;
11259         /* free in_set && out_set on each block */
11260         for(i = 1; i <= state->last_vertex; i++) {
11261                 struct triple_reg_set *entry, *next;
11262                 struct reg_block *rb;
11263                 rb = &blocks[i];
11264                 for(entry = rb->in; entry ; entry = next) {
11265                         next = entry->next;
11266                         do_triple_unset(&rb->in, entry->member);
11267                 }
11268                 for(entry = rb->out; entry; entry = next) {
11269                         next = entry->next;
11270                         do_triple_unset(&rb->out, entry->member);
11271                 }
11272         }
11273         xfree(blocks);
11274
11275 }
11276
11277 typedef void (*wvl_cb_t)(
11278         struct compile_state *state, 
11279         struct reg_block *blocks, struct triple_reg_set *live, 
11280         struct reg_block *rb, struct triple *ins, void *arg);
11281
11282 static void walk_variable_lifetimes(struct compile_state *state,
11283         struct reg_block *blocks, wvl_cb_t cb, void *arg)
11284 {
11285         int i;
11286         
11287         for(i = 1; i <= state->last_vertex; i++) {
11288                 struct triple_reg_set *live;
11289                 struct triple_reg_set *entry, *next;
11290                 struct triple *ptr, *prev;
11291                 struct reg_block *rb;
11292                 struct block *block;
11293                 int done;
11294
11295                 /* Get the blocks */
11296                 rb = &blocks[i];
11297                 block = rb->block;
11298
11299                 /* Copy out into live */
11300                 live = 0;
11301                 for(entry = rb->out; entry; entry = next) {
11302                         next = entry->next;
11303                         do_triple_set(&live, entry->member, entry->new);
11304                 }
11305                 /* Walk through the basic block calculating live */
11306                 for(done = 0, ptr = block->last; !done; ptr = prev) {
11307                         struct triple **expr;
11308
11309                         prev = ptr->prev;
11310                         done = (ptr == block->first);
11311
11312                         /* Ensure the current definition is in live */
11313                         if (triple_is_def(state, ptr)) {
11314                                 do_triple_set(&live, ptr, 0);
11315                         }
11316
11317                         /* Inform the callback function of what is
11318                          * going on.
11319                          */
11320                          cb(state, blocks, live, rb, ptr, arg);
11321                         
11322                         /* Remove the current definition from live */
11323                         do_triple_unset(&live, ptr);
11324
11325                         /* Add the current uses to live.
11326                          *
11327                          * It is safe to skip phi functions because they do
11328                          * not have any block local uses, and the block
11329                          * output sets already properly account for what
11330                          * control flow depedent uses phi functions do have.
11331                          */
11332                         if (ptr->op == OP_PHI) {
11333                                 continue;
11334                         }
11335                         expr = triple_rhs(state, ptr, 0);
11336                         for(;expr; expr = triple_rhs(state, ptr, expr)) {
11337                                 /* If the triple is not a definition skip it. */
11338                                 if (!*expr || !triple_is_def(state, *expr)) {
11339                                         continue;
11340                                 }
11341                                 do_triple_set(&live, *expr, 0);
11342                         }
11343                 }
11344                 /* Free live */
11345                 for(entry = live; entry; entry = next) {
11346                         next = entry->next;
11347                         do_triple_unset(&live, entry->member);
11348                 }
11349         }
11350 }
11351
11352 static int count_triples(struct compile_state *state)
11353 {
11354         struct triple *first, *ins;
11355         int triples = 0;
11356         first = RHS(state->main_function, 0);
11357         ins = first;
11358         do {
11359                 triples++;
11360                 ins = ins->next;
11361         } while (ins != first);
11362         return triples;
11363 }
11364 struct dead_triple {
11365         struct triple *triple;
11366         struct dead_triple *work_next;
11367         struct block *block;
11368         int color;
11369         int flags;
11370 #define TRIPLE_FLAG_ALIVE 1
11371 };
11372
11373
11374 static void awaken(
11375         struct compile_state *state,
11376         struct dead_triple *dtriple, struct triple **expr,
11377         struct dead_triple ***work_list_tail)
11378 {
11379         struct triple *triple;
11380         struct dead_triple *dt;
11381         if (!expr) {
11382                 return;
11383         }
11384         triple = *expr;
11385         if (!triple) {
11386                 return;
11387         }
11388         if (triple->id <= 0)  {
11389                 internal_error(state, triple, "bad triple id: %d",
11390                         triple->id);
11391         }
11392         if (triple->op == OP_NOOP) {
11393                 internal_warning(state, triple, "awakening noop?");
11394                 return;
11395         }
11396         dt = &dtriple[triple->id];
11397         if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11398                 dt->flags |= TRIPLE_FLAG_ALIVE;
11399                 if (!dt->work_next) {
11400                         **work_list_tail = dt;
11401                         *work_list_tail = &dt->work_next;
11402                 }
11403         }
11404 }
11405
11406 static void eliminate_inefectual_code(struct compile_state *state)
11407 {
11408         struct block *block;
11409         struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11410         int triples, i;
11411         struct triple *first, *ins;
11412
11413         /* Setup the work list */
11414         work_list = 0;
11415         work_list_tail = &work_list;
11416
11417         first = RHS(state->main_function, 0);
11418
11419         /* Count how many triples I have */
11420         triples = count_triples(state);
11421
11422         /* Now put then in an array and mark all of the triples dead */
11423         dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11424         
11425         ins = first;
11426         i = 1;
11427         block = 0;
11428         do {
11429                 if (ins->op == OP_LABEL) {
11430                         block = ins->u.block;
11431                 }
11432                 dtriple[i].triple = ins;
11433                 dtriple[i].block  = block;
11434                 dtriple[i].flags  = 0;
11435                 dtriple[i].color  = ins->id;
11436                 ins->id = i;
11437                 /* See if it is an operation we always keep */
11438 #warning "FIXME handle the case of killing a branch instruction"
11439                 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11440                         awaken(state, dtriple, &ins, &work_list_tail);
11441                 }
11442                 i++;
11443                 ins = ins->next;
11444         } while(ins != first);
11445         while(work_list) {
11446                 struct dead_triple *dt;
11447                 struct block_set *user;
11448                 struct triple **expr;
11449                 dt = work_list;
11450                 work_list = dt->work_next;
11451                 if (!work_list) {
11452                         work_list_tail = &work_list;
11453                 }
11454                 /* Wake up the data depencencies of this triple */
11455                 expr = 0;
11456                 do {
11457                         expr = triple_rhs(state, dt->triple, expr);
11458                         awaken(state, dtriple, expr, &work_list_tail);
11459                 } while(expr);
11460                 do {
11461                         expr = triple_lhs(state, dt->triple, expr);
11462                         awaken(state, dtriple, expr, &work_list_tail);
11463                 } while(expr);
11464                 do {
11465                         expr = triple_misc(state, dt->triple, expr);
11466                         awaken(state, dtriple, expr, &work_list_tail);
11467                 } while(expr);
11468                 /* Wake up the forward control dependencies */
11469                 do {
11470                         expr = triple_targ(state, dt->triple, expr);
11471                         awaken(state, dtriple, expr, &work_list_tail);
11472                 } while(expr);
11473                 /* Wake up the reverse control dependencies of this triple */
11474                 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11475                         awaken(state, dtriple, &user->member->last, &work_list_tail);
11476                 }
11477         }
11478         for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11479                 if ((dt->triple->op == OP_NOOP) && 
11480                         (dt->flags & TRIPLE_FLAG_ALIVE)) {
11481                         internal_error(state, dt->triple, "noop effective?");
11482                 }
11483                 dt->triple->id = dt->color;     /* Restore the color */
11484                 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11485 #warning "FIXME handle the case of killing a basic block"
11486                         if (dt->block->first == dt->triple) {
11487                                 continue;
11488                         }
11489                         if (dt->block->last == dt->triple) {
11490                                 dt->block->last = dt->triple->prev;
11491                         }
11492                         release_triple(state, dt->triple);
11493                 }
11494         }
11495         xfree(dtriple);
11496 }
11497
11498
11499 static void insert_mandatory_copies(struct compile_state *state)
11500 {
11501         struct triple *ins, *first;
11502
11503         /* The object is with a minimum of inserted copies,
11504          * to resolve in fundamental register conflicts between
11505          * register value producers and consumers.
11506          * Theoretically we may be greater than minimal when we
11507          * are inserting copies before instructions but that
11508          * case should be rare.
11509          */
11510         first = RHS(state->main_function, 0);
11511         ins = first;
11512         do {
11513                 struct triple_set *entry, *next;
11514                 struct triple *tmp;
11515                 struct reg_info info;
11516                 unsigned reg, regcm;
11517                 int do_post_copy, do_pre_copy;
11518                 tmp = 0;
11519                 if (!triple_is_def(state, ins)) {
11520                         goto next;
11521                 }
11522                 /* Find the architecture specific color information */
11523                 info = arch_reg_lhs(state, ins, 0);
11524                 if (info.reg >= MAX_REGISTERS) {
11525                         info.reg = REG_UNSET;
11526                 }
11527                 
11528                 reg = REG_UNSET;
11529                 regcm = arch_type_to_regcm(state, ins->type);
11530                 do_post_copy = do_pre_copy = 0;
11531
11532                 /* Walk through the uses of ins and check for conflicts */
11533                 for(entry = ins->use; entry; entry = next) {
11534                         struct reg_info rinfo;
11535                         int i;
11536                         next = entry->next;
11537                         i = find_rhs_use(state, entry->member, ins);
11538                         if (i < 0) {
11539                                 continue;
11540                         }
11541                         
11542                         /* Find the users color requirements */
11543                         rinfo = arch_reg_rhs(state, entry->member, i);
11544                         if (rinfo.reg >= MAX_REGISTERS) {
11545                                 rinfo.reg = REG_UNSET;
11546                         }
11547                         
11548                         /* See if I need a pre_copy */
11549                         if (rinfo.reg != REG_UNSET) {
11550                                 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
11551                                         do_pre_copy = 1;
11552                                 }
11553                                 reg = rinfo.reg;
11554                         }
11555                         regcm &= rinfo.regcm;
11556                         regcm = arch_regcm_normalize(state, regcm);
11557                         if (regcm == 0) {
11558                                 do_pre_copy = 1;
11559                         }
11560                 }
11561                 do_post_copy =
11562                         !do_pre_copy &&
11563                         (((info.reg != REG_UNSET) && 
11564                                 (reg != REG_UNSET) &&
11565                                 (info.reg != reg)) ||
11566                         ((info.regcm & regcm) == 0));
11567
11568                 reg = info.reg;
11569                 regcm = info.regcm;
11570                 /* Walk through the uses of insert and do a pre_copy or see if a post_copy is warranted */
11571                 for(entry = ins->use; entry; entry = next) {
11572                         struct reg_info rinfo;
11573                         int i;
11574                         next = entry->next;
11575                         i = find_rhs_use(state, entry->member, ins);
11576                         if (i < 0) {
11577                                 continue;
11578                         }
11579                         
11580                         /* Find the users color requirements */
11581                         rinfo = arch_reg_rhs(state, entry->member, i);
11582                         if (rinfo.reg >= MAX_REGISTERS) {
11583                                 rinfo.reg = REG_UNSET;
11584                         }
11585
11586                         /* Now see if it is time to do the pre_copy */
11587                         if (rinfo.reg != REG_UNSET) {
11588                                 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
11589                                         ((regcm & rinfo.regcm) == 0) ||
11590                                         /* Don't let a mandatory coalesce sneak
11591                                          * into a operation that is marked to prevent
11592                                          * coalescing.
11593                                          */
11594                                         ((reg != REG_UNNEEDED) &&
11595                                         ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
11596                                         (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
11597                                         ) {
11598                                         if (do_pre_copy) {
11599                                                 struct triple *user;
11600                                                 user = entry->member;
11601                                                 if (RHS(user, i) != ins) {
11602                                                         internal_error(state, user, "bad rhs");
11603                                                 }
11604                                                 tmp = pre_copy(state, user, i);
11605                                                 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11606                                                 continue;
11607                                         } else {
11608                                                 do_post_copy = 1;
11609                                         }
11610                                 }
11611                                 reg = rinfo.reg;
11612                         }
11613                         if ((regcm & rinfo.regcm) == 0) {
11614                                 if (do_pre_copy) {
11615                                         struct triple *user;
11616                                         user = entry->member;
11617                                         if (RHS(user, i) != ins) {
11618                                                 internal_error(state, user, "bad rhs");
11619                                         }
11620                                         tmp = pre_copy(state, user, i);
11621                                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11622                                         continue;
11623                                 } else {
11624                                         do_post_copy = 1;
11625                                 }
11626                         }
11627                         regcm &= rinfo.regcm;
11628                         
11629                 }
11630                 if (do_post_copy) {
11631                         struct reg_info pre, post;
11632                         tmp = post_copy(state, ins);
11633                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
11634                         pre = arch_reg_lhs(state, ins, 0);
11635                         post = arch_reg_lhs(state, tmp, 0);
11636                         if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
11637                                 internal_error(state, tmp, "useless copy");
11638                         }
11639                 }
11640         next:
11641                 ins = ins->next;
11642         } while(ins != first);
11643 }
11644
11645
11646 struct live_range_edge;
11647 struct live_range_def;
11648 struct live_range {
11649         struct live_range_edge *edges;
11650         struct live_range_def *defs;
11651 /* Note. The list pointed to by defs is kept in order.
11652  * That is baring splits in the flow control
11653  * defs dominates defs->next wich dominates defs->next->next
11654  * etc.
11655  */
11656         unsigned color;
11657         unsigned classes;
11658         unsigned degree;
11659         unsigned length;
11660         struct live_range *group_next, **group_prev;
11661 };
11662
11663 struct live_range_edge {
11664         struct live_range_edge *next;
11665         struct live_range *node;
11666 };
11667
11668 struct live_range_def {
11669         struct live_range_def *next;
11670         struct live_range_def *prev;
11671         struct live_range *lr;
11672         struct triple *def;
11673         unsigned orig_id;
11674 };
11675
11676 #define LRE_HASH_SIZE 2048
11677 struct lre_hash {
11678         struct lre_hash *next;
11679         struct live_range *left;
11680         struct live_range *right;
11681 };
11682
11683
11684 struct reg_state {
11685         struct lre_hash *hash[LRE_HASH_SIZE];
11686         struct reg_block *blocks;
11687         struct live_range_def *lrd;
11688         struct live_range *lr;
11689         struct live_range *low, **low_tail;
11690         struct live_range *high, **high_tail;
11691         unsigned defs;
11692         unsigned ranges;
11693         int passes, max_passes;
11694 #define MAX_ALLOCATION_PASSES 100
11695 };
11696
11697
11698 static unsigned regc_max_size(struct compile_state *state, int classes)
11699 {
11700         unsigned max_size;
11701         int i;
11702         max_size = 0;
11703         for(i = 0; i < MAX_REGC; i++) {
11704                 if (classes & (1 << i)) {
11705                         unsigned size;
11706                         size = arch_regc_size(state, i);
11707                         if (size > max_size) {
11708                                 max_size = size;
11709                         }
11710                 }
11711         }
11712         return max_size;
11713 }
11714
11715 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
11716 {
11717         unsigned equivs[MAX_REG_EQUIVS];
11718         int i;
11719         if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
11720                 internal_error(state, 0, "invalid register");
11721         }
11722         if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
11723                 internal_error(state, 0, "invalid register");
11724         }
11725         arch_reg_equivs(state, equivs, reg1);
11726         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11727                 if (equivs[i] == reg2) {
11728                         return 1;
11729                 }
11730         }
11731         return 0;
11732 }
11733
11734 static void reg_fill_used(struct compile_state *state, char *used, int reg)
11735 {
11736         unsigned equivs[MAX_REG_EQUIVS];
11737         int i;
11738         if (reg == REG_UNNEEDED) {
11739                 return;
11740         }
11741         arch_reg_equivs(state, equivs, reg);
11742         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11743                 used[equivs[i]] = 1;
11744         }
11745         return;
11746 }
11747
11748 static void reg_inc_used(struct compile_state *state, char *used, int reg)
11749 {
11750         unsigned equivs[MAX_REG_EQUIVS];
11751         int i;
11752         if (reg == REG_UNNEEDED) {
11753                 return;
11754         }
11755         arch_reg_equivs(state, equivs, reg);
11756         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
11757                 used[equivs[i]] += 1;
11758         }
11759         return;
11760 }
11761
11762 static unsigned int hash_live_edge(
11763         struct live_range *left, struct live_range *right)
11764 {
11765         unsigned int hash, val;
11766         unsigned long lval, rval;
11767         lval = ((unsigned long)left)/sizeof(struct live_range);
11768         rval = ((unsigned long)right)/sizeof(struct live_range);
11769         hash = 0;
11770         while(lval) {
11771                 val = lval & 0xff;
11772                 lval >>= 8;
11773                 hash = (hash *263) + val;
11774         }
11775         while(rval) {
11776                 val = rval & 0xff;
11777                 rval >>= 8;
11778                 hash = (hash *263) + val;
11779         }
11780         hash = hash & (LRE_HASH_SIZE - 1);
11781         return hash;
11782 }
11783
11784 static struct lre_hash **lre_probe(struct reg_state *rstate,
11785         struct live_range *left, struct live_range *right)
11786 {
11787         struct lre_hash **ptr;
11788         unsigned int index;
11789         /* Ensure left <= right */
11790         if (left > right) {
11791                 struct live_range *tmp;
11792                 tmp = left;
11793                 left = right;
11794                 right = tmp;
11795         }
11796         index = hash_live_edge(left, right);
11797         
11798         ptr = &rstate->hash[index];
11799         while(*ptr) {
11800                 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
11801                         break;
11802                 }
11803                 ptr = &(*ptr)->next;
11804         }
11805         return ptr;
11806 }
11807
11808 static int interfere(struct reg_state *rstate,
11809         struct live_range *left, struct live_range *right)
11810 {
11811         struct lre_hash **ptr;
11812         ptr = lre_probe(rstate, left, right);
11813         return ptr && *ptr;
11814 }
11815
11816 static void add_live_edge(struct reg_state *rstate, 
11817         struct live_range *left, struct live_range *right)
11818 {
11819         /* FIXME the memory allocation overhead is noticeable here... */
11820         struct lre_hash **ptr, *new_hash;
11821         struct live_range_edge *edge;
11822
11823         if (left == right) {
11824                 return;
11825         }
11826         if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
11827                 return;
11828         }
11829         /* Ensure left <= right */
11830         if (left > right) {
11831                 struct live_range *tmp;
11832                 tmp = left;
11833                 left = right;
11834                 right = tmp;
11835         }
11836         ptr = lre_probe(rstate, left, right);
11837         if (*ptr) {
11838                 return;
11839         }
11840 #if 0
11841         fprintf(stderr, "new_live_edge(%p, %p)\n",
11842                 left, right);
11843 #endif
11844         new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
11845         new_hash->next  = *ptr;
11846         new_hash->left  = left;
11847         new_hash->right = right;
11848         *ptr = new_hash;
11849
11850         edge = xmalloc(sizeof(*edge), "live_range_edge");
11851         edge->next   = left->edges;
11852         edge->node   = right;
11853         left->edges  = edge;
11854         left->degree += 1;
11855         
11856         edge = xmalloc(sizeof(*edge), "live_range_edge");
11857         edge->next    = right->edges;
11858         edge->node    = left;
11859         right->edges  = edge;
11860         right->degree += 1;
11861 }
11862
11863 static void remove_live_edge(struct reg_state *rstate,
11864         struct live_range *left, struct live_range *right)
11865 {
11866         struct live_range_edge *edge, **ptr;
11867         struct lre_hash **hptr, *entry;
11868         hptr = lre_probe(rstate, left, right);
11869         if (!hptr || !*hptr) {
11870                 return;
11871         }
11872         entry = *hptr;
11873         *hptr = entry->next;
11874         xfree(entry);
11875
11876         for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
11877                 edge = *ptr;
11878                 if (edge->node == right) {
11879                         *ptr = edge->next;
11880                         memset(edge, 0, sizeof(*edge));
11881                         xfree(edge);
11882                         right->degree--;
11883                         break;
11884                 }
11885         }
11886         for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
11887                 edge = *ptr;
11888                 if (edge->node == left) {
11889                         *ptr = edge->next;
11890                         memset(edge, 0, sizeof(*edge));
11891                         xfree(edge);
11892                         left->degree--;
11893                         break;
11894                 }
11895         }
11896 }
11897
11898 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
11899 {
11900         struct live_range_edge *edge, *next;
11901         for(edge = range->edges; edge; edge = next) {
11902                 next = edge->next;
11903                 remove_live_edge(rstate, range, edge->node);
11904         }
11905 }
11906
11907 static void transfer_live_edges(struct reg_state *rstate, 
11908         struct live_range *dest, struct live_range *src)
11909 {
11910         struct live_range_edge *edge, *next;
11911         for(edge = src->edges; edge; edge = next) {
11912                 struct live_range *other;
11913                 next = edge->next;
11914                 other = edge->node;
11915                 remove_live_edge(rstate, src, other);
11916                 add_live_edge(rstate, dest, other);
11917         }
11918 }
11919
11920
11921 /* Interference graph...
11922  * 
11923  * new(n) --- Return a graph with n nodes but no edges.
11924  * add(g,x,y) --- Return a graph including g with an between x and y
11925  * interfere(g, x, y) --- Return true if there exists an edge between the nodes
11926  *                x and y in the graph g
11927  * degree(g, x) --- Return the degree of the node x in the graph g
11928  * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
11929  *
11930  * Implement with a hash table && a set of adjcency vectors.
11931  * The hash table supports constant time implementations of add and interfere.
11932  * The adjacency vectors support an efficient implementation of neighbors.
11933  */
11934
11935 /* 
11936  *     +---------------------------------------------------+
11937  *     |         +--------------+                          |
11938  *     v         v              |                          |
11939  * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select 
11940  *
11941  * -- In simplify implment optimistic coloring... (No backtracking)
11942  * -- Implement Rematerialization it is the only form of spilling we can perform
11943  *    Essentially this means dropping a constant from a register because
11944  *    we can regenerate it later.
11945  *
11946  * --- Very conservative colalescing (don't colalesce just mark the opportunities)
11947  *     coalesce at phi points...
11948  * --- Bias coloring if at all possible do the coalesing a compile time.
11949  *
11950  *
11951  */
11952
11953 static void different_colored(
11954         struct compile_state *state, struct reg_state *rstate, 
11955         struct triple *parent, struct triple *ins)
11956 {
11957         struct live_range *lr;
11958         struct triple **expr;
11959         lr = rstate->lrd[ins->id].lr;
11960         expr = triple_rhs(state, ins, 0);
11961         for(;expr; expr = triple_rhs(state, ins, expr)) {
11962                 struct live_range *lr2;
11963                 if (!*expr || (*expr == parent) || (*expr == ins)) {
11964                         continue;
11965                 }
11966                 lr2 = rstate->lrd[(*expr)->id].lr;
11967                 if (lr->color == lr2->color) {
11968                         internal_error(state, ins, "live range too big");
11969                 }
11970         }
11971 }
11972
11973
11974 static struct live_range *coalesce_ranges(
11975         struct compile_state *state, struct reg_state *rstate,
11976         struct live_range *lr1, struct live_range *lr2)
11977 {
11978         struct live_range_def *head, *mid1, *mid2, *end, *lrd;
11979         unsigned color;
11980         unsigned classes;
11981         if (lr1 == lr2) {
11982                 return lr1;
11983         }
11984         if (!lr1->defs || !lr2->defs) {
11985                 internal_error(state, 0,
11986                         "cannot coalese dead live ranges");
11987         }
11988         if ((lr1->color == REG_UNNEEDED) ||
11989                 (lr2->color == REG_UNNEEDED)) {
11990                 internal_error(state, 0, 
11991                         "cannot coalesce live ranges without a possible color");
11992         }
11993         if ((lr1->color != lr2->color) &&
11994                 (lr1->color != REG_UNSET) &&
11995                 (lr2->color != REG_UNSET)) {
11996                 internal_error(state, lr1->defs->def, 
11997                         "cannot coalesce live ranges of different colors");
11998         }
11999         color = lr1->color;
12000         if (color == REG_UNSET) {
12001                 color = lr2->color;
12002         }
12003         classes = lr1->classes & lr2->classes;
12004         if (!classes) {
12005                 internal_error(state, lr1->defs->def,
12006                         "cannot coalesce live ranges with dissimilar register classes");
12007         }
12008         /* If there is a clear dominate live range put it in lr1,
12009          * For purposes of this test phi functions are
12010          * considered dominated by the definitions that feed into
12011          * them. 
12012          */
12013         if ((lr1->defs->prev->def->op == OP_PHI) ||
12014                 ((lr2->defs->prev->def->op != OP_PHI) &&
12015                 tdominates(state, lr2->defs->def, lr1->defs->def))) {
12016                 struct live_range *tmp;
12017                 tmp = lr1;
12018                 lr1 = lr2;
12019                 lr2 = tmp;
12020         }
12021 #if 0
12022         if (lr1->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
12023                 fprintf(stderr, "lr1 post\n");
12024         }
12025         if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12026                 fprintf(stderr, "lr1 pre\n");
12027         }
12028         if (lr2->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
12029                 fprintf(stderr, "lr2 post\n");
12030         }
12031         if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12032                 fprintf(stderr, "lr2 pre\n");
12033         }
12034 #endif
12035 #if 0
12036         fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
12037                 lr1->defs->def,
12038                 lr1->color,
12039                 lr2->defs->def,
12040                 lr2->color);
12041 #endif
12042         
12043         lr1->classes = classes;
12044         /* Append lr2 onto lr1 */
12045 #warning "FIXME should this be a merge instead of a splice?"
12046         /* This FIXME item applies to the correctness of live_range_end 
12047          * and to the necessity of making multiple passes of coalesce_live_ranges.
12048          * A failure to find some coalesce opportunities in coaleace_live_ranges
12049          * does not impact the correct of the compiler just the efficiency with
12050          * which registers are allocated.
12051          */
12052         head = lr1->defs;
12053         mid1 = lr1->defs->prev;
12054         mid2 = lr2->defs;
12055         end  = lr2->defs->prev;
12056         
12057         head->prev = end;
12058         end->next  = head;
12059
12060         mid1->next = mid2;
12061         mid2->prev = mid1;
12062
12063         /* Fixup the live range in the added live range defs */
12064         lrd = head;
12065         do {
12066                 lrd->lr = lr1;
12067                 lrd = lrd->next;
12068         } while(lrd != head);
12069
12070         /* Mark lr2 as free. */
12071         lr2->defs = 0;
12072         lr2->color = REG_UNNEEDED;
12073         lr2->classes = 0;
12074
12075         if (!lr1->defs) {
12076                 internal_error(state, 0, "lr1->defs == 0 ?");
12077         }
12078
12079         lr1->color   = color;
12080         lr1->classes = classes;
12081
12082         /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12083         transfer_live_edges(rstate, lr1, lr2);
12084
12085         return lr1;
12086 }
12087
12088 static struct live_range_def *live_range_head(
12089         struct compile_state *state, struct live_range *lr,
12090         struct live_range_def *last)
12091 {
12092         struct live_range_def *result;
12093         result = 0;
12094         if (last == 0) {
12095                 result = lr->defs;
12096         }
12097         else if (!tdominates(state, lr->defs->def, last->next->def)) {
12098                 result = last->next;
12099         }
12100         return result;
12101 }
12102
12103 static struct live_range_def *live_range_end(
12104         struct compile_state *state, struct live_range *lr,
12105         struct live_range_def *last)
12106 {
12107         struct live_range_def *result;
12108         result = 0;
12109         if (last == 0) {
12110                 result = lr->defs->prev;
12111         }
12112         else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12113                 result = last->prev;
12114         }
12115         return result;
12116 }
12117
12118
12119 static void initialize_live_ranges(
12120         struct compile_state *state, struct reg_state *rstate)
12121 {
12122         struct triple *ins, *first;
12123         size_t count, size;
12124         int i, j;
12125
12126         first = RHS(state->main_function, 0);
12127         /* First count how many instructions I have.
12128          */
12129         count = count_triples(state);
12130         /* Potentially I need one live range definitions for each
12131          * instruction, plus an extra for the split routines.
12132          */
12133         rstate->defs = count + 1;
12134         /* Potentially I need one live range for each instruction
12135          * plus an extra for the dummy live range.
12136          */
12137         rstate->ranges = count + 1;
12138         size = sizeof(rstate->lrd[0]) * rstate->defs;
12139         rstate->lrd = xcmalloc(size, "live_range_def");
12140         size = sizeof(rstate->lr[0]) * rstate->ranges;
12141         rstate->lr  = xcmalloc(size, "live_range");
12142
12143         /* Setup the dummy live range */
12144         rstate->lr[0].classes = 0;
12145         rstate->lr[0].color = REG_UNSET;
12146         rstate->lr[0].defs = 0;
12147         i = j = 0;
12148         ins = first;
12149         do {
12150                 /* If the triple is a variable give it a live range */
12151                 if (triple_is_def(state, ins)) {
12152                         struct reg_info info;
12153                         /* Find the architecture specific color information */
12154                         info = find_def_color(state, ins);
12155
12156                         i++;
12157                         rstate->lr[i].defs    = &rstate->lrd[j];
12158                         rstate->lr[i].color   = info.reg;
12159                         rstate->lr[i].classes = info.regcm;
12160                         rstate->lr[i].degree  = 0;
12161                         rstate->lrd[j].lr = &rstate->lr[i];
12162                 } 
12163                 /* Otherwise give the triple the dummy live range. */
12164                 else {
12165                         rstate->lrd[j].lr = &rstate->lr[0];
12166                 }
12167
12168                 /* Initalize the live_range_def */
12169                 rstate->lrd[j].next    = &rstate->lrd[j];
12170                 rstate->lrd[j].prev    = &rstate->lrd[j];
12171                 rstate->lrd[j].def     = ins;
12172                 rstate->lrd[j].orig_id = ins->id;
12173                 ins->id = j;
12174
12175                 j++;
12176                 ins = ins->next;
12177         } while(ins != first);
12178         rstate->ranges = i;
12179         rstate->defs -= 1;
12180
12181         /* Make a second pass to handle achitecture specific register
12182          * constraints.
12183          */
12184         ins = first;
12185         do {
12186                 int zlhs, zrhs, i, j;
12187                 if (ins->id > rstate->defs) {
12188                         internal_error(state, ins, "bad id");
12189                 }
12190                 
12191                 /* Walk through the template of ins and coalesce live ranges */
12192                 zlhs = TRIPLE_LHS(ins->sizes);
12193                 if ((zlhs == 0) && triple_is_def(state, ins)) {
12194                         zlhs = 1;
12195                 }
12196                 zrhs = TRIPLE_RHS(ins->sizes);
12197                 
12198                 for(i = 0; i < zlhs; i++) {
12199                         struct reg_info linfo;
12200                         struct live_range_def *lhs;
12201                         linfo = arch_reg_lhs(state, ins, i);
12202                         if (linfo.reg < MAX_REGISTERS) {
12203                                 continue;
12204                         }
12205                         if (triple_is_def(state, ins)) {
12206                                 lhs = &rstate->lrd[ins->id];
12207                         } else {
12208                                 lhs = &rstate->lrd[LHS(ins, i)->id];
12209                         }
12210                         for(j = 0; j < zrhs; j++) {
12211                                 struct reg_info rinfo;
12212                                 struct live_range_def *rhs;
12213                                 rinfo = arch_reg_rhs(state, ins, j);
12214                                 if (rinfo.reg < MAX_REGISTERS) {
12215                                         continue;
12216                                 }
12217                                 rhs = &rstate->lrd[RHS(ins, i)->id];
12218                                 if (rinfo.reg == linfo.reg) {
12219                                         coalesce_ranges(state, rstate, 
12220                                                 lhs->lr, rhs->lr);
12221                                 }
12222                         }
12223                 }
12224                 ins = ins->next;
12225         } while(ins != first);
12226 }
12227
12228 static void graph_ins(
12229         struct compile_state *state, 
12230         struct reg_block *blocks, struct triple_reg_set *live, 
12231         struct reg_block *rb, struct triple *ins, void *arg)
12232 {
12233         struct reg_state *rstate = arg;
12234         struct live_range *def;
12235         struct triple_reg_set *entry;
12236
12237         /* If the triple is not a definition
12238          * we do not have a definition to add to
12239          * the interference graph.
12240          */
12241         if (!triple_is_def(state, ins)) {
12242                 return;
12243         }
12244         def = rstate->lrd[ins->id].lr;
12245         
12246         /* Create an edge between ins and everything that is
12247          * alive, unless the live_range cannot share
12248          * a physical register with ins.
12249          */
12250         for(entry = live; entry; entry = entry->next) {
12251                 struct live_range *lr;
12252                 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12253                         internal_error(state, 0, "bad entry?");
12254                 }
12255                 lr = rstate->lrd[entry->member->id].lr;
12256                 if (def == lr) {
12257                         continue;
12258                 }
12259                 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12260                         continue;
12261                 }
12262                 add_live_edge(rstate, def, lr);
12263         }
12264         return;
12265 }
12266
12267 static struct live_range *get_verify_live_range(
12268         struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12269 {
12270         struct live_range *lr;
12271         struct live_range_def *lrd;
12272         int ins_found;
12273         if ((ins->id < 0) || (ins->id > rstate->defs)) {
12274                 internal_error(state, ins, "bad ins?");
12275         }
12276         lr = rstate->lrd[ins->id].lr;
12277         ins_found = 0;
12278         lrd = lr->defs;
12279         do {
12280                 if (lrd->def == ins) {
12281                         ins_found = 1;
12282                 }
12283                 lrd = lrd->next;
12284         } while(lrd != lr->defs);
12285         if (!ins_found) {
12286                 internal_error(state, ins, "ins not in live range");
12287         }
12288         return lr;
12289 }
12290
12291 static void verify_graph_ins(
12292         struct compile_state *state, 
12293         struct reg_block *blocks, struct triple_reg_set *live, 
12294         struct reg_block *rb, struct triple *ins, void *arg)
12295 {
12296         struct reg_state *rstate = arg;
12297         struct triple_reg_set *entry1, *entry2;
12298
12299
12300         /* Compare live against edges and make certain the code is working */
12301         for(entry1 = live; entry1; entry1 = entry1->next) {
12302                 struct live_range *lr1;
12303                 lr1 = get_verify_live_range(state, rstate, entry1->member);
12304                 for(entry2 = live; entry2; entry2 = entry2->next) {
12305                         struct live_range *lr2;
12306                         struct live_range_edge *edge2;
12307                         int lr1_found;
12308                         int lr2_degree;
12309                         if (entry2 == entry1) {
12310                                 continue;
12311                         }
12312                         lr2 = get_verify_live_range(state, rstate, entry2->member);
12313                         if (lr1 == lr2) {
12314                                 internal_error(state, entry2->member, 
12315                                         "live range with 2 values simultaneously alive");
12316                         }
12317                         if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12318                                 continue;
12319                         }
12320                         if (!interfere(rstate, lr1, lr2)) {
12321                                 internal_error(state, entry2->member, 
12322                                         "edges don't interfere?");
12323                         }
12324                                 
12325                         lr1_found = 0;
12326                         lr2_degree = 0;
12327                         for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12328                                 lr2_degree++;
12329                                 if (edge2->node == lr1) {
12330                                         lr1_found = 1;
12331                                 }
12332                         }
12333                         if (lr2_degree != lr2->degree) {
12334                                 internal_error(state, entry2->member,
12335                                         "computed degree: %d does not match reported degree: %d\n",
12336                                         lr2_degree, lr2->degree);
12337                         }
12338                         if (!lr1_found) {
12339                                 internal_error(state, entry2->member, "missing edge");
12340                         }
12341                 }
12342         }
12343         return;
12344 }
12345
12346
12347 static void print_interference_ins(
12348         struct compile_state *state, 
12349         struct reg_block *blocks, struct triple_reg_set *live, 
12350         struct reg_block *rb, struct triple *ins, void *arg)
12351 {
12352         struct reg_state *rstate = arg;
12353         struct live_range *lr;
12354         unsigned id;
12355
12356         lr = rstate->lrd[ins->id].lr;
12357         id = ins->id;
12358         ins->id = rstate->lrd[id].orig_id;
12359         SET_REG(ins->id, lr->color);
12360         display_triple(stdout, ins);
12361         ins->id = id;
12362
12363         if (lr->defs) {
12364                 struct live_range_def *lrd;
12365                 printf("       range:");
12366                 lrd = lr->defs;
12367                 do {
12368                         printf(" %-10p", lrd->def);
12369                         lrd = lrd->next;
12370                 } while(lrd != lr->defs);
12371                 printf("\n");
12372         }
12373         if (live) {
12374                 struct triple_reg_set *entry;
12375                 printf("        live:");
12376                 for(entry = live; entry; entry = entry->next) {
12377                         printf(" %-10p", entry->member);
12378                 }
12379                 printf("\n");
12380         }
12381         if (lr->edges) {
12382                 struct live_range_edge *entry;
12383                 printf("       edges:");
12384                 for(entry = lr->edges; entry; entry = entry->next) {
12385                         struct live_range_def *lrd;
12386                         lrd = entry->node->defs;
12387                         do {
12388                                 printf(" %-10p", lrd->def);
12389                                 lrd = lrd->next;
12390                         } while(lrd != entry->node->defs);
12391                         printf("|");
12392                 }
12393                 printf("\n");
12394         }
12395         if (triple_is_branch(state, ins)) {
12396                 printf("\n");
12397         }
12398         return;
12399 }
12400
12401 static int coalesce_live_ranges(
12402         struct compile_state *state, struct reg_state *rstate)
12403 {
12404         /* At the point where a value is moved from one
12405          * register to another that value requires two
12406          * registers, thus increasing register pressure.
12407          * Live range coaleescing reduces the register
12408          * pressure by keeping a value in one register
12409          * longer.
12410          *
12411          * In the case of a phi function all paths leading
12412          * into it must be allocated to the same register
12413          * otherwise the phi function may not be removed.
12414          *
12415          * Forcing a value to stay in a single register
12416          * for an extended period of time does have
12417          * limitations when applied to non homogenous
12418          * register pool.  
12419          *
12420          * The two cases I have identified are:
12421          * 1) Two forced register assignments may
12422          *    collide.
12423          * 2) Registers may go unused because they
12424          *    are only good for storing the value
12425          *    and not manipulating it.
12426          *
12427          * Because of this I need to split live ranges,
12428          * even outside of the context of coalesced live
12429          * ranges.  The need to split live ranges does
12430          * impose some constraints on live range coalescing.
12431          *
12432          * - Live ranges may not be coalesced across phi
12433          *   functions.  This creates a 2 headed live
12434          *   range that cannot be sanely split.
12435          *
12436          * - phi functions (coalesced in initialize_live_ranges) 
12437          *   are handled as pre split live ranges so we will
12438          *   never attempt to split them.
12439          */
12440         int coalesced;
12441         int i;
12442
12443         coalesced = 0;
12444         for(i = 0; i <= rstate->ranges; i++) {
12445                 struct live_range *lr1;
12446                 struct live_range_def *lrd1;
12447                 lr1 = &rstate->lr[i];
12448                 if (!lr1->defs) {
12449                         continue;
12450                 }
12451                 lrd1 = live_range_end(state, lr1, 0);
12452                 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
12453                         struct triple_set *set;
12454                         if (lrd1->def->op != OP_COPY) {
12455                                 continue;
12456                         }
12457                         /* Skip copies that are the result of a live range split. */
12458                         if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
12459                                 continue;
12460                         }
12461                         for(set = lrd1->def->use; set; set = set->next) {
12462                                 struct live_range_def *lrd2;
12463                                 struct live_range *lr2, *res;
12464
12465                                 lrd2 = &rstate->lrd[set->member->id];
12466
12467                                 /* Don't coalesce with instructions
12468                                  * that are the result of a live range
12469                                  * split.
12470                                  */
12471                                 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12472                                         continue;
12473                                 }
12474                                 lr2 = rstate->lrd[set->member->id].lr;
12475                                 if (lr1 == lr2) {
12476                                         continue;
12477                                 }
12478                                 if ((lr1->color != lr2->color) &&
12479                                         (lr1->color != REG_UNSET) &&
12480                                         (lr2->color != REG_UNSET)) {
12481                                         continue;
12482                                 }
12483                                 if ((lr1->classes & lr2->classes) == 0) {
12484                                         continue;
12485                                 }
12486                                 
12487                                 if (interfere(rstate, lr1, lr2)) {
12488                                         continue;
12489                                 }
12490
12491                                 res = coalesce_ranges(state, rstate, lr1, lr2);
12492                                 coalesced += 1;
12493                                 if (res != lr1) {
12494                                         goto next;
12495                                 }
12496                         }
12497                 }
12498         next:
12499                 ;
12500         }
12501         return coalesced;
12502 }
12503
12504
12505 static void fix_coalesce_conflicts(struct compile_state *state,
12506         struct reg_block *blocks, struct triple_reg_set *live,
12507         struct reg_block *rb, struct triple *ins, void *arg)
12508 {
12509         int zlhs, zrhs, i, j;
12510
12511         /* See if we have a mandatory coalesce operation between
12512          * a lhs and a rhs value.  If so and the rhs value is also
12513          * alive then this triple needs to be pre copied.  Otherwise
12514          * we would have two definitions in the same live range simultaneously
12515          * alive.
12516          */
12517         zlhs = TRIPLE_LHS(ins->sizes);
12518         if ((zlhs == 0) && triple_is_def(state, ins)) {
12519                 zlhs = 1;
12520         }
12521         zrhs = TRIPLE_RHS(ins->sizes);
12522         for(i = 0; i < zlhs; i++) {
12523                 struct reg_info linfo;
12524                 linfo = arch_reg_lhs(state, ins, i);
12525                 if (linfo.reg < MAX_REGISTERS) {
12526                         continue;
12527                 }
12528                 for(j = 0; j < zrhs; j++) {
12529                         struct reg_info rinfo;
12530                         struct triple *rhs;
12531                         struct triple_reg_set *set;
12532                         int found;
12533                         found = 0;
12534                         rinfo = arch_reg_rhs(state, ins, j);
12535                         if (rinfo.reg != linfo.reg) {
12536                                 continue;
12537                         }
12538                         rhs = RHS(ins, j);
12539                         for(set = live; set && !found; set = set->next) {
12540                                 if (set->member == rhs) {
12541                                         found = 1;
12542                                 }
12543                         }
12544                         if (found) {
12545                                 struct triple *copy;
12546                                 copy = pre_copy(state, ins, j);
12547                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12548                         }
12549                 }
12550         }
12551         return;
12552 }
12553
12554 static void replace_set_use(struct compile_state *state,
12555         struct triple_reg_set *head, struct triple *orig, struct triple *new)
12556 {
12557         struct triple_reg_set *set;
12558         for(set = head; set; set = set->next) {
12559                 if (set->member == orig) {
12560                         set->member = new;
12561                 }
12562         }
12563 }
12564
12565 static void replace_block_use(struct compile_state *state, 
12566         struct reg_block *blocks, struct triple *orig, struct triple *new)
12567 {
12568         int i;
12569 #warning "WISHLIST visit just those blocks that need it *"
12570         for(i = 1; i <= state->last_vertex; i++) {
12571                 struct reg_block *rb;
12572                 rb = &blocks[i];
12573                 replace_set_use(state, rb->in, orig, new);
12574                 replace_set_use(state, rb->out, orig, new);
12575         }
12576 }
12577
12578 static void color_instructions(struct compile_state *state)
12579 {
12580         struct triple *ins, *first;
12581         first = RHS(state->main_function, 0);
12582         ins = first;
12583         do {
12584                 if (triple_is_def(state, ins)) {
12585                         struct reg_info info;
12586                         info = find_lhs_color(state, ins, 0);
12587                         if (info.reg >= MAX_REGISTERS) {
12588                                 info.reg = REG_UNSET;
12589                         }
12590                         SET_INFO(ins->id, info);
12591                 }
12592                 ins = ins->next;
12593         } while(ins != first);
12594 }
12595
12596 static struct reg_info read_lhs_color(
12597         struct compile_state *state, struct triple *ins, int index)
12598 {
12599         struct reg_info info;
12600         if ((index == 0) && triple_is_def(state, ins)) {
12601                 info.reg   = ID_REG(ins->id);
12602                 info.regcm = ID_REGCM(ins->id);
12603         }
12604         else if (index < TRIPLE_LHS(ins->sizes)) {
12605                 info = read_lhs_color(state, LHS(ins, index), 0);
12606         }
12607         else {
12608                 internal_error(state, ins, "Bad lhs %d", index);
12609                 info.reg = REG_UNSET;
12610                 info.regcm = 0;
12611         }
12612         return info;
12613 }
12614
12615 static struct triple *resolve_tangle(
12616         struct compile_state *state, struct triple *tangle)
12617 {
12618         struct reg_info info, uinfo;
12619         struct triple_set *set, *next;
12620         struct triple *copy;
12621
12622 #warning "WISHLIST recalculate all affected instructions colors"
12623         info = find_lhs_color(state, tangle, 0);
12624         for(set = tangle->use; set; set = next) {
12625                 struct triple *user;
12626                 int i, zrhs;
12627                 next = set->next;
12628                 user = set->member;
12629                 zrhs = TRIPLE_RHS(user->sizes);
12630                 for(i = 0; i < zrhs; i++) {
12631                         if (RHS(user, i) != tangle) {
12632                                 continue;
12633                         }
12634                         uinfo = find_rhs_post_color(state, user, i);
12635                         if (uinfo.reg == info.reg) {
12636                                 copy = pre_copy(state, user, i);
12637                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12638                                 SET_INFO(copy->id, uinfo);
12639                         }
12640                 }
12641         }
12642         copy = 0;
12643         uinfo = find_lhs_pre_color(state, tangle, 0);
12644         if (uinfo.reg == info.reg) {
12645                 struct reg_info linfo;
12646                 copy = post_copy(state, tangle);
12647                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
12648                 linfo = find_lhs_color(state, copy, 0);
12649                 SET_INFO(copy->id, linfo);
12650         }
12651         info = find_lhs_color(state, tangle, 0);
12652         SET_INFO(tangle->id, info);
12653         
12654         return copy;
12655 }
12656
12657
12658 static void fix_tangles(struct compile_state *state,
12659         struct reg_block *blocks, struct triple_reg_set *live,
12660         struct reg_block *rb, struct triple *ins, void *arg)
12661 {
12662         int *tangles = arg;
12663         struct triple *tangle;
12664         do {
12665                 char used[MAX_REGISTERS];
12666                 struct triple_reg_set *set;
12667                 tangle = 0;
12668
12669                 /* Find out which registers have multiple uses at this point */
12670                 memset(used, 0, sizeof(used));
12671                 for(set = live; set; set = set->next) {
12672                         struct reg_info info;
12673                         info = read_lhs_color(state, set->member, 0);
12674                         if (info.reg == REG_UNSET) {
12675                                 continue;
12676                         }
12677                         reg_inc_used(state, used, info.reg);
12678                 }
12679                 
12680                 /* Now find the least dominated definition of a register in
12681                  * conflict I have seen so far.
12682                  */
12683                 for(set = live; set; set = set->next) {
12684                         struct reg_info info;
12685                         info = read_lhs_color(state, set->member, 0);
12686                         if (used[info.reg] < 2) {
12687                                 continue;
12688                         }
12689                         /* Changing copies that feed into phi functions
12690                          * is incorrect.
12691                          */
12692                         if (set->member->use && 
12693                                 (set->member->use->member->op == OP_PHI)) {
12694                                 continue;
12695                         }
12696                         if (!tangle || tdominates(state, set->member, tangle)) {
12697                                 tangle = set->member;
12698                         }
12699                 }
12700                 /* If I have found a tangle resolve it */
12701                 if (tangle) {
12702                         struct triple *post_copy;
12703                         (*tangles)++;
12704                         post_copy = resolve_tangle(state, tangle);
12705                         if (post_copy) {
12706                                 replace_block_use(state, blocks, tangle, post_copy);
12707                         }
12708                         if (post_copy && (tangle != ins)) {
12709                                 replace_set_use(state, live, tangle, post_copy);
12710                         }
12711                 }
12712         } while(tangle);
12713         return;
12714 }
12715
12716 static int correct_tangles(
12717         struct compile_state *state, struct reg_block *blocks)
12718 {
12719         int tangles;
12720         tangles = 0;
12721         color_instructions(state);
12722         walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
12723         return tangles;
12724 }
12725
12726 struct least_conflict {
12727         struct reg_state *rstate;
12728         struct live_range *ref_range;
12729         struct triple *ins;
12730         struct triple_reg_set *live;
12731         size_t count;
12732         int constraints;
12733 };
12734 static void least_conflict(struct compile_state *state,
12735         struct reg_block *blocks, struct triple_reg_set *live,
12736         struct reg_block *rb, struct triple *ins, void *arg)
12737 {
12738         struct least_conflict *conflict = arg;
12739         struct live_range_edge *edge;
12740         struct triple_reg_set *set;
12741         size_t count;
12742         int constraints;
12743
12744 #warning "FIXME handle instructions with left hand sides..."
12745         /* Only instructions that introduce a new definition
12746          * can be the conflict instruction.
12747          */
12748         if (!triple_is_def(state, ins)) {
12749                 return;
12750         }
12751
12752         /* See if live ranges at this instruction are a
12753          * strict subset of the live ranges that are in conflict.
12754          */
12755         count = 0;
12756         for(set = live; set; set = set->next) {
12757                 struct live_range *lr;
12758                 lr = conflict->rstate->lrd[set->member->id].lr;
12759                 /* Ignore it if there cannot be an edge between these two nodes */
12760                 if (!arch_regcm_intersect(conflict->ref_range->classes, lr->classes)) {
12761                         continue;
12762                 }
12763                 for(edge = conflict->ref_range->edges; edge; edge = edge->next) {
12764                         if (edge->node == lr) {
12765                                 break;
12766                         }
12767                 }
12768                 if (!edge && (lr != conflict->ref_range)) {
12769                         return;
12770                 }
12771                 count++;
12772         }
12773         if (count <= 1) {
12774                 return;
12775         }
12776
12777 #if 0
12778         /* See if there is an uncolored member in this subset. 
12779          */
12780          for(set = live; set; set = set->next) {
12781                 struct live_range *lr;
12782                 lr = conflict->rstate->lrd[set->member->id].lr;
12783                 if (lr->color == REG_UNSET) {
12784                         break;
12785                 }
12786         }
12787         if (!set && (conflict->ref_range != REG_UNSET)) {
12788                 return;
12789         }
12790 #endif
12791
12792         /* See if any of the live registers are constrained,
12793          * if not it won't be productive to pick this as
12794          * a conflict instruction.
12795          */
12796         constraints = 0;
12797         for(set = live; set; set = set->next) {
12798                 struct triple_set *uset;
12799                 struct reg_info info;
12800                 unsigned classes;
12801                 unsigned cur_size, size;
12802                 /* Skip this instruction */
12803                 if (set->member == ins) {
12804                         continue;
12805                 }
12806                 /* Find how many registers this value can potentially 
12807                  * be assigned to.
12808                  */
12809                 classes = arch_type_to_regcm(state, set->member->type);
12810                 size = regc_max_size(state, classes);
12811                 
12812                 /* Find how many registers we allow this value to
12813                  * be assigned to.
12814                  */
12815                 info = arch_reg_lhs(state, set->member, 0);
12816                 
12817                 /* If the value does not live in a register it
12818                  * isn't constrained.
12819                  */
12820                 if (info.reg == REG_UNNEEDED) {
12821                         continue;
12822                 }
12823                 
12824                 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12825                         cur_size = regc_max_size(state, info.regcm);
12826                 } else {
12827                         cur_size = 1;
12828                 }
12829
12830                 /* If there is no difference between potential and
12831                  * actual register count there is not a constraint
12832                  */
12833                 if (cur_size >= size) {
12834                         continue;
12835                 }
12836                 
12837                 /* If this live_range feeds into conflict->inds
12838                  * it isn't a constraint we can relieve.
12839                  */
12840                 for(uset = set->member->use; uset; uset = uset->next) {
12841                         if (uset->member == ins) {
12842                                 break;
12843                         }
12844                 }
12845                 if (uset) {
12846                         continue;
12847                 }
12848                 constraints = 1;
12849                 break;
12850         }
12851         /* Don't drop canidates with constraints */
12852         if (conflict->constraints && !constraints) {
12853                 return;
12854         }
12855
12856
12857 #if 0
12858         fprintf(stderr, "conflict ins? %p %s count: %d constraints: %d\n",
12859                 ins, tops(ins->op), count, constraints);
12860 #endif
12861         /* Find the instruction with the largest possible subset of
12862          * conflict ranges and that dominates any other instruction
12863          * with an equal sized set of conflicting ranges.
12864          */
12865         if ((count > conflict->count) ||
12866                 ((count == conflict->count) &&
12867                         tdominates(state, ins, conflict->ins))) {
12868                 struct triple_reg_set *next;
12869                 /* Remember the canidate instruction */
12870                 conflict->ins = ins;
12871                 conflict->count = count;
12872                 conflict->constraints = constraints;
12873                 /* Free the old collection of live registers */
12874                 for(set = conflict->live; set; set = next) {
12875                         next = set->next;
12876                         do_triple_unset(&conflict->live, set->member);
12877                 }
12878                 conflict->live = 0;
12879                 /* Rember the registers that are alive but do not feed
12880                  * into or out of conflict->ins.
12881                  */
12882                 for(set = live; set; set = set->next) {
12883                         struct triple **expr;
12884                         if (set->member == ins) {
12885                                 goto next;
12886                         }
12887                         expr = triple_rhs(state, ins, 0);
12888                         for(;expr; expr = triple_rhs(state, ins, expr)) {
12889                                 if (*expr == set->member) {
12890                                         goto next;
12891                                 }
12892                         }
12893                         expr = triple_lhs(state, ins, 0);
12894                         for(; expr; expr = triple_lhs(state, ins, expr)) {
12895                                 if (*expr == set->member) {
12896                                         goto next;
12897                                 }
12898                         }
12899                         do_triple_set(&conflict->live, set->member, set->new);
12900                 next:
12901                         ;
12902                 }
12903         }
12904         return;
12905 }
12906
12907 static void find_range_conflict(struct compile_state *state,
12908         struct reg_state *rstate, char *used, struct live_range *ref_range,
12909         struct least_conflict *conflict)
12910 {
12911
12912         /* there are 3 kinds ways conflicts can occure.
12913          * 1) the life time of 2 values simply overlap.
12914          * 2) the 2 values feed into the same instruction.
12915          * 3) the 2 values feed into a phi function.
12916          */
12917
12918         /* find the instruction where the problematic conflict comes
12919          * into existance.  that the instruction where all of
12920          * the values are alive, and among such instructions it is
12921          * the least dominated one.
12922          *
12923          * a value is alive an an instruction if either;
12924          * 1) the value defintion dominates the instruction and there
12925          *    is a use at or after that instrction
12926          * 2) the value definition feeds into a phi function in the
12927          *    same block as the instruction.  and the phi function
12928          *    is at or after the instruction.
12929          */
12930         memset(conflict, 0, sizeof(*conflict));
12931         conflict->rstate      = rstate;
12932         conflict->ref_range   = ref_range;
12933         conflict->ins         = 0;
12934         conflict->live        = 0;
12935         conflict->count       = 0;
12936         conflict->constraints = 0;
12937         walk_variable_lifetimes(state, rstate->blocks, least_conflict, conflict);
12938
12939         if (!conflict->ins) {
12940                 internal_error(state, ref_range->defs->def, "No conflict ins?");
12941         }
12942         if (!conflict->live) {
12943                 internal_error(state, ref_range->defs->def, "No conflict live?");
12944         }
12945 #if 0
12946         fprintf(stderr, "conflict ins: %p %s count: %d constraints: %d\n", 
12947                 conflict->ins, tops(conflict->ins->op),
12948                 conflict->count, conflict->constraints);
12949 #endif
12950         return;
12951 }
12952
12953 static struct triple *split_constrained_range(struct compile_state *state, 
12954         struct reg_state *rstate, char *used, struct least_conflict *conflict)
12955 {
12956         unsigned constrained_size;
12957         struct triple *new, *constrained;
12958         struct triple_reg_set *cset;
12959         /* Find a range that is having problems because it is
12960          * artificially constrained.
12961          */
12962         constrained_size = ~0;
12963         constrained = 0;
12964         new = 0;
12965         for(cset = conflict->live; cset; cset = cset->next) {
12966                 struct triple_set *set;
12967                 struct reg_info info;
12968                 unsigned classes;
12969                 unsigned cur_size, size;
12970                 /* Skip the live range that starts with conflict->ins */
12971                 if (cset->member == conflict->ins) {
12972                         continue;
12973                 }
12974                 /* Find how many registers this value can potentially
12975                  * be assigned to.
12976                  */
12977                 classes = arch_type_to_regcm(state, cset->member->type);
12978                 size = regc_max_size(state, classes);
12979
12980                 /* Find how many registers we allow this value to
12981                  * be assigned to.
12982                  */
12983                 info = arch_reg_lhs(state, cset->member, 0);
12984
12985                 /* If the register doesn't need a register 
12986                  * splitting it can't help.
12987                  */
12988                 if (info.reg == REG_UNNEEDED) {
12989                         continue;
12990                 }
12991 #warning "FIXME do I need a call to arch_reg_rhs around here somewhere?"
12992                 if ((info.reg == REG_UNSET) || (info.reg >= MAX_REGISTERS)) {
12993                         cur_size = regc_max_size(state, info.regcm);
12994                 } else {
12995                         cur_size = 1;
12996                 }
12997                 /* If this live_range feeds into conflict->ins
12998                  * splitting it is unlikely to help.
12999                  */
13000                 for(set = cset->member->use; set; set = set->next) {
13001                         if (set->member == conflict->ins) {
13002                                 goto next;
13003                         }
13004                 }
13005
13006                 /* If there is no difference between potential and
13007                  * actual register count there is nothing to do.
13008                  */
13009                 if (cur_size >= size) {
13010                         continue;
13011                 }
13012                 /* Of the constrained registers deal with the
13013                  * most constrained one first.
13014                  */
13015                 if (!constrained ||
13016                         (size < constrained_size)) {
13017                         constrained = cset->member;
13018                         constrained_size = size;
13019                 }
13020         next:
13021                 ;
13022         }
13023         if (constrained) {
13024                 new = post_copy(state, constrained);
13025                 new->id |= TRIPLE_FLAG_POST_SPLIT;
13026         }
13027         return new;
13028 }
13029
13030 static int split_ranges(
13031         struct compile_state *state, struct reg_state *rstate, 
13032         char *used, struct live_range *range)
13033 {
13034         struct triple *new;
13035
13036 #if 0
13037         fprintf(stderr, "split_ranges %d %s %p\n", 
13038                 rstate->passes, tops(range->defs->def->op), range->defs->def);
13039 #endif
13040         if ((range->color == REG_UNNEEDED) ||
13041                 (rstate->passes >= rstate->max_passes)) {
13042                 return 0;
13043         }
13044         new = 0;
13045         /* If I can't allocate a register something needs to be split */
13046         if (arch_select_free_register(state, used, range->classes) == REG_UNSET) {
13047                 struct least_conflict conflict;
13048
13049 #if 0
13050         fprintf(stderr, "find_range_conflict\n");
13051 #endif
13052                 /* Find where in the set of registers the conflict
13053                  * actually occurs.
13054                  */
13055                 find_range_conflict(state, rstate, used, range, &conflict);
13056
13057                 /* If a range has been artifically constrained split it */
13058                 new = split_constrained_range(state, rstate, used, &conflict);
13059                 
13060                 if (!new) {
13061                 /* Ideally I would split the live range that will not be used
13062                  * for the longest period of time in hopes that this will 
13063                  * (a) allow me to spill a register or
13064                  * (b) allow me to place a value in another register.
13065                  *
13066                  * So far I don't have a test case for this, the resolving
13067                  * of mandatory constraints has solved all of my
13068                  * know issues.  So I have choosen not to write any
13069                  * code until I cat get a better feel for cases where
13070                  * it would be useful to have.
13071                  *
13072                  */
13073 #warning "WISHLIST implement live range splitting..."
13074 #if 0
13075                         print_blocks(state, stderr);
13076                         print_dominators(state, stderr);
13077
13078 #endif
13079                         return 0;
13080                 }
13081         }
13082         if (new) {
13083                 rstate->lrd[rstate->defs].orig_id = new->id;
13084                 new->id = rstate->defs;
13085                 rstate->defs++;
13086 #if 0
13087                 fprintf(stderr, "new: %p old: %s %p\n", 
13088                         new, tops(RHS(new, 0)->op), RHS(new, 0));
13089 #endif
13090 #if 0
13091                 print_blocks(state, stderr);
13092                 print_dominators(state, stderr);
13093
13094 #endif
13095                 return 1;
13096         }
13097         return 0;
13098 }
13099
13100 #if DEBUG_COLOR_GRAPH > 1
13101 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13102 #define cgdebug_flush() fflush(stdout)
13103 #elif DEBUG_COLOR_GRAPH == 1
13104 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13105 #define cgdebug_flush() fflush(stderr)
13106 #else
13107 #define cgdebug_printf(...)
13108 #define cgdebug_flush()
13109 #endif
13110
13111         
13112 static int select_free_color(struct compile_state *state, 
13113         struct reg_state *rstate, struct live_range *range)
13114 {
13115         struct triple_set *entry;
13116         struct live_range_def *lrd;
13117         struct live_range_def *phi;
13118         struct live_range_edge *edge;
13119         char used[MAX_REGISTERS];
13120         struct triple **expr;
13121
13122         /* Instead of doing just the trivial color select here I try
13123          * a few extra things because a good color selection will help reduce
13124          * copies.
13125          */
13126
13127         /* Find the registers currently in use */
13128         memset(used, 0, sizeof(used));
13129         for(edge = range->edges; edge; edge = edge->next) {
13130                 if (edge->node->color == REG_UNSET) {
13131                         continue;
13132                 }
13133                 reg_fill_used(state, used, edge->node->color);
13134         }
13135 #if DEBUG_COLOR_GRAPH > 1
13136         {
13137                 int i;
13138                 i = 0;
13139                 for(edge = range->edges; edge; edge = edge->next) {
13140                         i++;
13141                 }
13142                 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n", 
13143                         tops(range->def->op), i, 
13144                         range->def->filename, range->def->line, range->def->col);
13145                 for(i = 0; i < MAX_REGISTERS; i++) {
13146                         if (used[i]) {
13147                                 cgdebug_printf("used: %s\n",
13148                                         arch_reg_str(i));
13149                         }
13150                 }
13151         }       
13152 #endif
13153
13154 #warning "FIXME detect conflicts caused by the source and destination being the same register"
13155
13156         /* If a color is already assigned see if it will work */
13157         if (range->color != REG_UNSET) {
13158                 struct live_range_def *lrd;
13159                 if (!used[range->color]) {
13160                         return 1;
13161                 }
13162                 for(edge = range->edges; edge; edge = edge->next) {
13163                         if (edge->node->color != range->color) {
13164                                 continue;
13165                         }
13166                         warning(state, edge->node->defs->def, "edge: ");
13167                         lrd = edge->node->defs;
13168                         do {
13169                                 warning(state, lrd->def, " %p %s",
13170                                         lrd->def, tops(lrd->def->op));
13171                                 lrd = lrd->next;
13172                         } while(lrd != edge->node->defs);
13173                 }
13174                 lrd = range->defs;
13175                 warning(state, range->defs->def, "def: ");
13176                 do {
13177                         warning(state, lrd->def, " %p %s",
13178                                 lrd->def, tops(lrd->def->op));
13179                         lrd = lrd->next;
13180                 } while(lrd != range->defs);
13181                 internal_error(state, range->defs->def,
13182                         "live range with already used color %s",
13183                         arch_reg_str(range->color));
13184         }
13185
13186         /* If I feed into an expression reuse it's color.
13187          * This should help remove copies in the case of 2 register instructions
13188          * and phi functions.
13189          */
13190         phi = 0;
13191         lrd = live_range_end(state, range, 0);
13192         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13193                 entry = lrd->def->use;
13194                 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13195                         struct live_range_def *insd;
13196                         insd = &rstate->lrd[entry->member->id];
13197                         if (insd->lr->defs == 0) {
13198                                 continue;
13199                         }
13200                         if (!phi && (insd->def->op == OP_PHI) &&
13201                                 !interfere(rstate, range, insd->lr)) {
13202                                 phi = insd;
13203                         }
13204                         if ((insd->lr->color == REG_UNSET) ||
13205                                 ((insd->lr->classes & range->classes) == 0) ||
13206                                 (used[insd->lr->color])) {
13207                                 continue;
13208                         }
13209                         if (interfere(rstate, range, insd->lr)) {
13210                                 continue;
13211                         }
13212                         range->color = insd->lr->color;
13213                 }
13214         }
13215         /* If I feed into a phi function reuse it's color or the color
13216          * of something else that feeds into the phi function.
13217          */
13218         if (phi) {
13219                 if (phi->lr->color != REG_UNSET) {
13220                         if (used[phi->lr->color]) {
13221                                 range->color = phi->lr->color;
13222                         }
13223                 }
13224                 else {
13225                         expr = triple_rhs(state, phi->def, 0);
13226                         for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13227                                 struct live_range *lr;
13228                                 if (!*expr) {
13229                                         continue;
13230                                 }
13231                                 lr = rstate->lrd[(*expr)->id].lr;
13232                                 if ((lr->color == REG_UNSET) || 
13233                                         ((lr->classes & range->classes) == 0) ||
13234                                         (used[lr->color])) {
13235                                         continue;
13236                                 }
13237                                 if (interfere(rstate, range, lr)) {
13238                                         continue;
13239                                 }
13240                                 range->color = lr->color;
13241                         }
13242                 }
13243         }
13244         /* If I don't interfere with a rhs node reuse it's color */
13245         lrd = live_range_head(state, range, 0);
13246         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13247                 expr = triple_rhs(state, lrd->def, 0);
13248                 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
13249                         struct live_range *lr;
13250                         if (!*expr) {
13251                                 continue;
13252                         }
13253                         lr = rstate->lrd[(*expr)->id].lr;
13254                         if ((lr->color == -1) || 
13255                                 ((lr->classes & range->classes) == 0) ||
13256                                 (used[lr->color])) {
13257                                 continue;
13258                         }
13259                         if (interfere(rstate, range, lr)) {
13260                                 continue;
13261                         }
13262                         range->color = lr->color;
13263                         break;
13264                 }
13265         }
13266         /* If I have not opportunitically picked a useful color
13267          * pick the first color that is free.
13268          */
13269         if (range->color == REG_UNSET) {
13270                 range->color = 
13271                         arch_select_free_register(state, used, range->classes);
13272         }
13273         if (range->color == REG_UNSET) {
13274                 struct live_range_def *lrd;
13275                 int i;
13276                 if (split_ranges(state, rstate, used, range)) {
13277                         return 0;
13278                 }
13279                 for(edge = range->edges; edge; edge = edge->next) {
13280                         warning(state, edge->node->defs->def, "edge reg %s",
13281                                 arch_reg_str(edge->node->color));
13282                         lrd = edge->node->defs;
13283                         do {
13284                                 warning(state, lrd->def, " %s",
13285                                         tops(lrd->def->op));
13286                                 lrd = lrd->next;
13287                         } while(lrd != edge->node->defs);
13288                 }
13289                 warning(state, range->defs->def, "range: ");
13290                 lrd = range->defs;
13291                 do {
13292                         warning(state, lrd->def, " %s",
13293                                 tops(lrd->def->op));
13294                         lrd = lrd->next;
13295                 } while(lrd != range->defs);
13296                         
13297                 warning(state, range->defs->def, "classes: %x",
13298                         range->classes);
13299                 for(i = 0; i < MAX_REGISTERS; i++) {
13300                         if (used[i]) {
13301                                 warning(state, range->defs->def, "used: %s",
13302                                         arch_reg_str(i));
13303                         }
13304                 }
13305 #if DEBUG_COLOR_GRAPH < 2
13306                 error(state, range->defs->def, "too few registers");
13307 #else
13308                 internal_error(state, range->defs->def, "too few registers");
13309 #endif
13310         }
13311         range->classes = arch_reg_regcm(state, range->color);
13312         if (range->color == -1) {
13313                 internal_error(state, range->defs->def, "select_free_color did not?");
13314         }
13315         return 1;
13316 }
13317
13318 static int color_graph(struct compile_state *state, struct reg_state *rstate)
13319 {
13320         int colored;
13321         struct live_range_edge *edge;
13322         struct live_range *range;
13323         if (rstate->low) {
13324                 cgdebug_printf("Lo: ");
13325                 range = rstate->low;
13326                 if (*range->group_prev != range) {
13327                         internal_error(state, 0, "lo: *prev != range?");
13328                 }
13329                 *range->group_prev = range->group_next;
13330                 if (range->group_next) {
13331                         range->group_next->group_prev = range->group_prev;
13332                 }
13333                 if (&range->group_next == rstate->low_tail) {
13334                         rstate->low_tail = range->group_prev;
13335                 }
13336                 if (rstate->low == range) {
13337                         internal_error(state, 0, "low: next != prev?");
13338                 }
13339         }
13340         else if (rstate->high) {
13341                 cgdebug_printf("Hi: ");
13342                 range = rstate->high;
13343                 if (*range->group_prev != range) {
13344                         internal_error(state, 0, "hi: *prev != range?");
13345                 }
13346                 *range->group_prev = range->group_next;
13347                 if (range->group_next) {
13348                         range->group_next->group_prev = range->group_prev;
13349                 }
13350                 if (&range->group_next == rstate->high_tail) {
13351                         rstate->high_tail = range->group_prev;
13352                 }
13353                 if (rstate->high == range) {
13354                         internal_error(state, 0, "high: next != prev?");
13355                 }
13356         }
13357         else {
13358                 return 1;
13359         }
13360         cgdebug_printf(" %d\n", range - rstate->lr);
13361         range->group_prev = 0;
13362         for(edge = range->edges; edge; edge = edge->next) {
13363                 struct live_range *node;
13364                 node = edge->node;
13365                 /* Move nodes from the high to the low list */
13366                 if (node->group_prev && (node->color == REG_UNSET) &&
13367                         (node->degree == regc_max_size(state, node->classes))) {
13368                         if (*node->group_prev != node) {
13369                                 internal_error(state, 0, "move: *prev != node?");
13370                         }
13371                         *node->group_prev = node->group_next;
13372                         if (node->group_next) {
13373                                 node->group_next->group_prev = node->group_prev;
13374                         }
13375                         if (&node->group_next == rstate->high_tail) {
13376                                 rstate->high_tail = node->group_prev;
13377                         }
13378                         cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13379                         node->group_prev  = rstate->low_tail;
13380                         node->group_next  = 0;
13381                         *rstate->low_tail = node;
13382                         rstate->low_tail  = &node->group_next;
13383                         if (*node->group_prev != node) {
13384                                 internal_error(state, 0, "move2: *prev != node?");
13385                         }
13386                 }
13387                 node->degree -= 1;
13388         }
13389         colored = color_graph(state, rstate);
13390         if (colored) {
13391                 cgdebug_printf("Coloring %d @%s:%d.%d:", 
13392                         range - rstate->lr,
13393                         range->def->filename, range->def->line, range->def->col);
13394                 cgdebug_flush();
13395                 colored = select_free_color(state, rstate, range);
13396                 cgdebug_printf(" %s\n", arch_reg_str(range->color));
13397         }
13398         return colored;
13399 }
13400
13401 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13402 {
13403         struct live_range *lr;
13404         struct live_range_edge *edge;
13405         struct triple *ins, *first;
13406         char used[MAX_REGISTERS];
13407         first = RHS(state->main_function, 0);
13408         ins = first;
13409         do {
13410                 if (triple_is_def(state, ins)) {
13411                         if ((ins->id < 0) || (ins->id > rstate->defs)) {
13412                                 internal_error(state, ins, 
13413                                         "triple without a live range def");
13414                         }
13415                         lr = rstate->lrd[ins->id].lr;
13416                         if (lr->color == REG_UNSET) {
13417                                 internal_error(state, ins,
13418                                         "triple without a color");
13419                         }
13420                         /* Find the registers used by the edges */
13421                         memset(used, 0, sizeof(used));
13422                         for(edge = lr->edges; edge; edge = edge->next) {
13423                                 if (edge->node->color == REG_UNSET) {
13424                                         internal_error(state, 0,
13425                                                 "live range without a color");
13426                         }
13427                                 reg_fill_used(state, used, edge->node->color);
13428                         }
13429                         if (used[lr->color]) {
13430                                 internal_error(state, ins,
13431                                         "triple with already used color");
13432                         }
13433                 }
13434                 ins = ins->next;
13435         } while(ins != first);
13436 }
13437
13438 static void color_triples(struct compile_state *state, struct reg_state *rstate)
13439 {
13440         struct live_range *lr;
13441         struct triple *first, *ins;
13442         first = RHS(state->main_function, 0);
13443         ins = first;
13444         do {
13445                 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13446                         internal_error(state, ins, 
13447                                 "triple without a live range");
13448                 }
13449                 lr = rstate->lrd[ins->id].lr;
13450                 SET_REG(ins->id, lr->color);
13451                 ins = ins->next;
13452         } while (ins != first);
13453 }
13454
13455 static void print_interference_block(
13456         struct compile_state *state, struct block *block, void *arg)
13457
13458 {
13459         struct reg_state *rstate = arg;
13460         struct reg_block *rb;
13461         struct triple *ptr;
13462         int phi_present;
13463         int done;
13464         rb = &rstate->blocks[block->vertex];
13465
13466         printf("\nblock: %p (%d), %p<-%p %p<-%p\n", 
13467                 block, 
13468                 block->vertex,
13469                 block->left, 
13470                 block->left && block->left->use?block->left->use->member : 0,
13471                 block->right, 
13472                 block->right && block->right->use?block->right->use->member : 0);
13473         if (rb->in) {
13474                 struct triple_reg_set *in_set;
13475                 printf("        in:");
13476                 for(in_set = rb->in; in_set; in_set = in_set->next) {
13477                         printf(" %-10p", in_set->member);
13478                 }
13479                 printf("\n");
13480         }
13481         phi_present = 0;
13482         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13483                 done = (ptr == block->last);
13484                 if (ptr->op == OP_PHI) {
13485                         phi_present = 1;
13486                         break;
13487                 }
13488         }
13489         if (phi_present) {
13490                 int edge;
13491                 for(edge = 0; edge < block->users; edge++) {
13492                         printf("     in(%d):", edge);
13493                         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13494                                 struct triple **slot;
13495                                 done = (ptr == block->last);
13496                                 if (ptr->op != OP_PHI) {
13497                                         continue;
13498                                 }
13499                                 slot = &RHS(ptr, 0);
13500                                 printf(" %-10p", slot[edge]);
13501                         }
13502                         printf("\n");
13503                 }
13504         }
13505         if (block->first->op == OP_LABEL) {
13506                 printf("%p:\n", block->first);
13507         }
13508         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
13509                 struct triple_set *user;
13510                 struct live_range *lr;
13511                 unsigned id;
13512                 int op;
13513                 op = ptr->op;
13514                 done = (ptr == block->last);
13515                 lr = rstate->lrd[ptr->id].lr;
13516                 
13517                 if (triple_stores_block(state, ptr)) {
13518                         if (ptr->u.block != block) {
13519                                 internal_error(state, ptr, 
13520                                         "Wrong block pointer: %p",
13521                                         ptr->u.block);
13522                         }
13523                 }
13524                 if (op == OP_ADECL) {
13525                         for(user = ptr->use; user; user = user->next) {
13526                                 if (!user->member->u.block) {
13527                                         internal_error(state, user->member, 
13528                                                 "Use %p not in a block?",
13529                                                 user->member);
13530                                 }
13531                                 
13532                         }
13533                 }
13534                 id = ptr->id;
13535                 ptr->id = rstate->lrd[id].orig_id;
13536                 SET_REG(ptr->id, lr->color);
13537                 display_triple(stdout, ptr);
13538                 ptr->id = id;
13539
13540                 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
13541                         internal_error(state, ptr, "lr has no defs!");
13542                 }
13543
13544                 if (lr->defs) {
13545                         struct live_range_def *lrd;
13546                         printf("       range:");
13547                         lrd = lr->defs;
13548                         do {
13549                                 printf(" %-10p", lrd->def);
13550                                 lrd = lrd->next;
13551                         } while(lrd != lr->defs);
13552                         printf("\n");
13553                 }
13554                 if (lr->edges > 0) {
13555                         struct live_range_edge *edge;
13556                         printf("       edges:");
13557                         for(edge = lr->edges; edge; edge = edge->next) {
13558                                 struct live_range_def *lrd;
13559                                 lrd = edge->node->defs;
13560                                 do {
13561                                         printf(" %-10p", lrd->def);
13562                                         lrd = lrd->next;
13563                                 } while(lrd != edge->node->defs);
13564                                 printf("|");
13565                         }
13566                         printf("\n");
13567                 }
13568                 /* Do a bunch of sanity checks */
13569                 valid_ins(state, ptr);
13570                 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
13571                         internal_error(state, ptr, "Invalid triple id: %d",
13572                                 ptr->id);
13573                 }
13574                 for(user = ptr->use; user; user = user->next) {
13575                         struct triple *use;
13576                         struct live_range *ulr;
13577                         use = user->member;
13578                         valid_ins(state, use);
13579                         if ((use->id < 0) || (use->id > rstate->defs)) {
13580                                 internal_error(state, use, "Invalid triple id: %d",
13581                                         use->id);
13582                         }
13583                         ulr = rstate->lrd[user->member->id].lr;
13584                         if (triple_stores_block(state, user->member) &&
13585                                 !user->member->u.block) {
13586                                 internal_error(state, user->member,
13587                                         "Use %p not in a block?",
13588                                         user->member);
13589                         }
13590                 }
13591         }
13592         if (rb->out) {
13593                 struct triple_reg_set *out_set;
13594                 printf("       out:");
13595                 for(out_set = rb->out; out_set; out_set = out_set->next) {
13596                         printf(" %-10p", out_set->member);
13597                 }
13598                 printf("\n");
13599         }
13600         printf("\n");
13601 }
13602
13603 static struct live_range *merge_sort_lr(
13604         struct live_range *first, struct live_range *last)
13605 {
13606         struct live_range *mid, *join, **join_tail, *pick;
13607         size_t size;
13608         size = (last - first) + 1;
13609         if (size >= 2) {
13610                 mid = first + size/2;
13611                 first = merge_sort_lr(first, mid -1);
13612                 mid   = merge_sort_lr(mid, last);
13613                 
13614                 join = 0;
13615                 join_tail = &join;
13616                 /* merge the two lists */
13617                 while(first && mid) {
13618                         if ((first->degree < mid->degree) ||
13619                                 ((first->degree == mid->degree) &&
13620                                         (first->length < mid->length))) {
13621                                 pick = first;
13622                                 first = first->group_next;
13623                                 if (first) {
13624                                         first->group_prev = 0;
13625                                 }
13626                         }
13627                         else {
13628                                 pick = mid;
13629                                 mid = mid->group_next;
13630                                 if (mid) {
13631                                         mid->group_prev = 0;
13632                                 }
13633                         }
13634                         pick->group_next = 0;
13635                         pick->group_prev = join_tail;
13636                         *join_tail = pick;
13637                         join_tail = &pick->group_next;
13638                 }
13639                 /* Splice the remaining list */
13640                 pick = (first)? first : mid;
13641                 *join_tail = pick;
13642                 if (pick) { 
13643                         pick->group_prev = join_tail;
13644                 }
13645         }
13646         else {
13647                 if (!first->defs) {
13648                         first = 0;
13649                 }
13650                 join = first;
13651         }
13652         return join;
13653 }
13654
13655 static void ids_from_rstate(struct compile_state *state, 
13656         struct reg_state *rstate)
13657 {
13658         struct triple *ins, *first;
13659         if (!rstate->defs) {
13660                 return;
13661         }
13662         /* Display the graph if desired */
13663         if (state->debug & DEBUG_INTERFERENCE) {
13664                 print_blocks(state, stdout);
13665                 print_control_flow(state);
13666         }
13667         first = RHS(state->main_function, 0);
13668         ins = first;
13669         do {
13670                 if (ins->id) {
13671                         struct live_range_def *lrd;
13672                         lrd = &rstate->lrd[ins->id];
13673                         ins->id = lrd->orig_id;
13674                 }
13675                 ins = ins->next;
13676         } while(ins != first);
13677 }
13678
13679 static void cleanup_live_edges(struct reg_state *rstate)
13680 {
13681         int i;
13682         /* Free the edges on each node */
13683         for(i = 1; i <= rstate->ranges; i++) {
13684                 remove_live_edges(rstate, &rstate->lr[i]);
13685         }
13686 }
13687
13688 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13689 {
13690         cleanup_live_edges(rstate);
13691         xfree(rstate->lrd);
13692         xfree(rstate->lr);
13693
13694         /* Free the variable lifetime information */
13695         if (rstate->blocks) {
13696                 free_variable_lifetimes(state, rstate->blocks);
13697         }
13698         rstate->defs = 0;
13699         rstate->ranges = 0;
13700         rstate->lrd = 0;
13701         rstate->lr = 0;
13702         rstate->blocks = 0;
13703 }
13704
13705 static void verify_consistency(struct compile_state *state);
13706 static void allocate_registers(struct compile_state *state)
13707 {
13708         struct reg_state rstate;
13709         int colored;
13710
13711         /* Clear out the reg_state */
13712         memset(&rstate, 0, sizeof(rstate));
13713         rstate.max_passes = MAX_ALLOCATION_PASSES;
13714
13715         do {
13716                 struct live_range **point, **next;
13717                 int tangles;
13718                 int coalesced;
13719
13720 #if 0
13721                 fprintf(stderr, "pass: %d\n", rstate.passes);
13722 #endif
13723
13724                 /* Restore ids */
13725                 ids_from_rstate(state, &rstate);
13726
13727                 /* Cleanup the temporary data structures */
13728                 cleanup_rstate(state, &rstate);
13729
13730                 /* Compute the variable lifetimes */
13731                 rstate.blocks = compute_variable_lifetimes(state);
13732
13733                 /* Fix invalid mandatory live range coalesce conflicts */
13734                 walk_variable_lifetimes(
13735                         state, rstate.blocks, fix_coalesce_conflicts, 0);
13736
13737                 /* Fix two simultaneous uses of the same register.
13738                  * In a few pathlogical cases a partial untangle moves
13739                  * the tangle to a part of the graph we won't revisit.
13740                  * So we keep looping until we have no more tangle fixes
13741                  * to apply.
13742                  */
13743                 do {
13744                         tangles = correct_tangles(state, rstate.blocks);
13745                 } while(tangles);
13746
13747                 if (state->debug & DEBUG_INSERTED_COPIES) {
13748                         printf("After resolve_tangles\n");
13749                         print_blocks(state, stdout);
13750                         print_control_flow(state);
13751                 }
13752                 verify_consistency(state);
13753                 
13754                 /* Allocate and initialize the live ranges */
13755                 initialize_live_ranges(state, &rstate);
13756
13757                 /* Note current doing coalescing in a loop appears to 
13758                  * buys me nothing.  The code is left this way in case
13759                  * there is some value in it.  Or if a future bugfix
13760                  *  yields some benefit.
13761                  */
13762                 do {
13763 #if 0
13764                         fprintf(stderr, "coalescing\n");
13765 #endif                  
13766                         /* Remove any previous live edge calculations */
13767                         cleanup_live_edges(&rstate);
13768
13769                         /* Compute the interference graph */
13770                         walk_variable_lifetimes(
13771                                 state, rstate.blocks, graph_ins, &rstate);
13772                         
13773                         /* Display the interference graph if desired */
13774                         if (state->debug & DEBUG_INTERFERENCE) {
13775                                 printf("\nlive variables by block\n");
13776                                 walk_blocks(state, print_interference_block, &rstate);
13777                                 printf("\nlive variables by instruction\n");
13778                                 walk_variable_lifetimes(
13779                                         state, rstate.blocks, 
13780                                         print_interference_ins, &rstate);
13781                         }
13782                         
13783                         coalesced = coalesce_live_ranges(state, &rstate);
13784
13785 #if 0
13786                         fprintf(stderr, "coalesced: %d\n", coalesced);
13787 #endif
13788                 } while(coalesced);
13789
13790 #if DEBUG_CONSISTENCY > 1
13791 # if 0
13792                 fprintf(stderr, "verify_graph_ins...\n");
13793 # endif
13794                 /* Verify the interference graph */
13795                 walk_variable_lifetimes(
13796                         state, rstate.blocks, verify_graph_ins, &rstate);
13797 # if 0
13798                 fprintf(stderr, "verify_graph_ins done\n");
13799 #endif
13800 #endif
13801                         
13802                 /* Build the groups low and high.  But with the nodes
13803                  * first sorted by degree order.
13804                  */
13805                 rstate.low_tail  = &rstate.low;
13806                 rstate.high_tail = &rstate.high;
13807                 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
13808                 if (rstate.high) {
13809                         rstate.high->group_prev = &rstate.high;
13810                 }
13811                 for(point = &rstate.high; *point; point = &(*point)->group_next)
13812                         ;
13813                 rstate.high_tail = point;
13814                 /* Walk through the high list and move everything that needs
13815                  * to be onto low.
13816                  */
13817                 for(point = &rstate.high; *point; point = next) {
13818                         struct live_range *range;
13819                         next = &(*point)->group_next;
13820                         range = *point;
13821                         
13822                         /* If it has a low degree or it already has a color
13823                          * place the node in low.
13824                          */
13825                         if ((range->degree < regc_max_size(state, range->classes)) ||
13826                                 (range->color != REG_UNSET)) {
13827                                 cgdebug_printf("Lo: %5d degree %5d%s\n", 
13828                                         range - rstate.lr, range->degree,
13829                                         (range->color != REG_UNSET) ? " (colored)": "");
13830                                 *range->group_prev = range->group_next;
13831                                 if (range->group_next) {
13832                                         range->group_next->group_prev = range->group_prev;
13833                                 }
13834                                 if (&range->group_next == rstate.high_tail) {
13835                                         rstate.high_tail = range->group_prev;
13836                                 }
13837                                 range->group_prev  = rstate.low_tail;
13838                                 range->group_next  = 0;
13839                                 *rstate.low_tail   = range;
13840                                 rstate.low_tail    = &range->group_next;
13841                                 next = point;
13842                         }
13843                         else {
13844                                 cgdebug_printf("hi: %5d degree %5d%s\n", 
13845                                         range - rstate.lr, range->degree,
13846                                         (range->color != REG_UNSET) ? " (colored)": "");
13847                         }
13848                 }
13849                 /* Color the live_ranges */
13850                 colored = color_graph(state, &rstate);
13851                 rstate.passes++;
13852         } while (!colored);
13853
13854         /* Verify the graph was properly colored */
13855         verify_colors(state, &rstate);
13856
13857         /* Move the colors from the graph to the triples */
13858         color_triples(state, &rstate);
13859
13860         /* Cleanup the temporary data structures */
13861         cleanup_rstate(state, &rstate);
13862 }
13863
13864 /* Sparce Conditional Constant Propogation
13865  * =========================================
13866  */
13867 struct ssa_edge;
13868 struct flow_block;
13869 struct lattice_node {
13870         unsigned old_id;
13871         struct triple *def;
13872         struct ssa_edge *out;
13873         struct flow_block *fblock;
13874         struct triple *val;
13875         /* lattice high   val && !is_const(val) 
13876          * lattice const  is_const(val)
13877          * lattice low    val == 0
13878          */
13879 };
13880 struct ssa_edge {
13881         struct lattice_node *src;
13882         struct lattice_node *dst;
13883         struct ssa_edge *work_next;
13884         struct ssa_edge *work_prev;
13885         struct ssa_edge *out_next;
13886 };
13887 struct flow_edge {
13888         struct flow_block *src;
13889         struct flow_block *dst;
13890         struct flow_edge *work_next;
13891         struct flow_edge *work_prev;
13892         struct flow_edge *in_next;
13893         struct flow_edge *out_next;
13894         int executable;
13895 };
13896 struct flow_block {
13897         struct block *block;
13898         struct flow_edge *in;
13899         struct flow_edge *out;
13900         struct flow_edge left, right;
13901 };
13902
13903 struct scc_state {
13904         int ins_count;
13905         struct lattice_node *lattice;
13906         struct ssa_edge     *ssa_edges;
13907         struct flow_block   *flow_blocks;
13908         struct flow_edge    *flow_work_list;
13909         struct ssa_edge     *ssa_work_list;
13910 };
13911
13912
13913 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, 
13914         struct flow_edge *fedge)
13915 {
13916         if (!scc->flow_work_list) {
13917                 scc->flow_work_list = fedge;
13918                 fedge->work_next = fedge->work_prev = fedge;
13919         }
13920         else {
13921                 struct flow_edge *ftail;
13922                 ftail = scc->flow_work_list->work_prev;
13923                 fedge->work_next = ftail->work_next;
13924                 fedge->work_prev = ftail;
13925                 fedge->work_next->work_prev = fedge;
13926                 fedge->work_prev->work_next = fedge;
13927         }
13928 }
13929
13930 static struct flow_edge *scc_next_fedge(
13931         struct compile_state *state, struct scc_state *scc)
13932 {
13933         struct flow_edge *fedge;
13934         fedge = scc->flow_work_list;
13935         if (fedge) {
13936                 fedge->work_next->work_prev = fedge->work_prev;
13937                 fedge->work_prev->work_next = fedge->work_next;
13938                 if (fedge->work_next != fedge) {
13939                         scc->flow_work_list = fedge->work_next;
13940                 } else {
13941                         scc->flow_work_list = 0;
13942                 }
13943         }
13944         return fedge;
13945 }
13946
13947 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
13948         struct ssa_edge *sedge)
13949 {
13950         if (!scc->ssa_work_list) {
13951                 scc->ssa_work_list = sedge;
13952                 sedge->work_next = sedge->work_prev = sedge;
13953         }
13954         else {
13955                 struct ssa_edge *stail;
13956                 stail = scc->ssa_work_list->work_prev;
13957                 sedge->work_next = stail->work_next;
13958                 sedge->work_prev = stail;
13959                 sedge->work_next->work_prev = sedge;
13960                 sedge->work_prev->work_next = sedge;
13961         }
13962 }
13963
13964 static struct ssa_edge *scc_next_sedge(
13965         struct compile_state *state, struct scc_state *scc)
13966 {
13967         struct ssa_edge *sedge;
13968         sedge = scc->ssa_work_list;
13969         if (sedge) {
13970                 sedge->work_next->work_prev = sedge->work_prev;
13971                 sedge->work_prev->work_next = sedge->work_next;
13972                 if (sedge->work_next != sedge) {
13973                         scc->ssa_work_list = sedge->work_next;
13974                 } else {
13975                         scc->ssa_work_list = 0;
13976                 }
13977         }
13978         return sedge;
13979 }
13980
13981 static void initialize_scc_state(
13982         struct compile_state *state, struct scc_state *scc)
13983 {
13984         int ins_count, ssa_edge_count;
13985         int ins_index, ssa_edge_index, fblock_index;
13986         struct triple *first, *ins;
13987         struct block *block;
13988         struct flow_block *fblock;
13989
13990         memset(scc, 0, sizeof(*scc));
13991
13992         /* Inialize pass zero find out how much memory we need */
13993         first = RHS(state->main_function, 0);
13994         ins = first;
13995         ins_count = ssa_edge_count = 0;
13996         do {
13997                 struct triple_set *edge;
13998                 ins_count += 1;
13999                 for(edge = ins->use; edge; edge = edge->next) {
14000                         ssa_edge_count++;
14001                 }
14002                 ins = ins->next;
14003         } while(ins != first);
14004 #if DEBUG_SCC
14005         fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
14006                 ins_count, ssa_edge_count, state->last_vertex);
14007 #endif
14008         scc->ins_count   = ins_count;
14009         scc->lattice     = 
14010                 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
14011         scc->ssa_edges   = 
14012                 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
14013         scc->flow_blocks = 
14014                 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1), 
14015                         "flow_blocks");
14016
14017         /* Initialize pass one collect up the nodes */
14018         fblock = 0;
14019         block = 0;
14020         ins_index = ssa_edge_index = fblock_index = 0;
14021         ins = first;
14022         do {
14023                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14024                         block = ins->u.block;
14025                         if (!block) {
14026                                 internal_error(state, ins, "label without block");
14027                         }
14028                         fblock_index += 1;
14029                         block->vertex = fblock_index;
14030                         fblock = &scc->flow_blocks[fblock_index];
14031                         fblock->block = block;
14032                 }
14033                 {
14034                         struct lattice_node *lnode;
14035                         ins_index += 1;
14036                         lnode = &scc->lattice[ins_index];
14037                         lnode->def = ins;
14038                         lnode->out = 0;
14039                         lnode->fblock = fblock;
14040                         lnode->val = ins; /* LATTICE HIGH */
14041                         lnode->old_id = ins->id;
14042                         ins->id = ins_index;
14043                 }
14044                 ins = ins->next;
14045         } while(ins != first);
14046         /* Initialize pass two collect up the edges */
14047         block = 0;
14048         fblock = 0;
14049         ins = first;
14050         do {
14051                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14052                         struct flow_edge *fedge, **ftail;
14053                         struct block_set *bedge;
14054                         block = ins->u.block;
14055                         fblock = &scc->flow_blocks[block->vertex];
14056                         fblock->in = 0;
14057                         fblock->out = 0;
14058                         ftail = &fblock->out;
14059                         if (block->left) {
14060                                 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
14061                                 if (fblock->left.dst->block != block->left) {
14062                                         internal_error(state, 0, "block mismatch");
14063                                 }
14064                                 fblock->left.out_next = 0;
14065                                 *ftail = &fblock->left;
14066                                 ftail = &fblock->left.out_next;
14067                         }
14068                         if (block->right) {
14069                                 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14070                                 if (fblock->right.dst->block != block->right) {
14071                                         internal_error(state, 0, "block mismatch");
14072                                 }
14073                                 fblock->right.out_next = 0;
14074                                 *ftail = &fblock->right;
14075                                 ftail = &fblock->right.out_next;
14076                         }
14077                         for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14078                                 fedge->src = fblock;
14079                                 fedge->work_next = fedge->work_prev = fedge;
14080                                 fedge->executable = 0;
14081                         }
14082                         ftail = &fblock->in;
14083                         for(bedge = block->use; bedge; bedge = bedge->next) {
14084                                 struct block *src_block;
14085                                 struct flow_block *sfblock;
14086                                 struct flow_edge *sfedge;
14087                                 src_block = bedge->member;
14088                                 sfblock = &scc->flow_blocks[src_block->vertex];
14089                                 sfedge = 0;
14090                                 if (src_block->left == block) {
14091                                         sfedge = &sfblock->left;
14092                                 } else {
14093                                         sfedge = &sfblock->right;
14094                                 }
14095                                 *ftail = sfedge;
14096                                 ftail = &sfedge->in_next;
14097                                 sfedge->in_next = 0;
14098                         }
14099                 }
14100                 {
14101                         struct triple_set *edge;
14102                         struct ssa_edge **stail;
14103                         struct lattice_node *lnode;
14104                         lnode = &scc->lattice[ins->id];
14105                         lnode->out = 0;
14106                         stail = &lnode->out;
14107                         for(edge = ins->use; edge; edge = edge->next) {
14108                                 struct ssa_edge *sedge;
14109                                 ssa_edge_index += 1;
14110                                 sedge = &scc->ssa_edges[ssa_edge_index];
14111                                 *stail = sedge;
14112                                 stail = &sedge->out_next;
14113                                 sedge->src = lnode;
14114                                 sedge->dst = &scc->lattice[edge->member->id];
14115                                 sedge->work_next = sedge->work_prev = sedge;
14116                                 sedge->out_next = 0;
14117                         }
14118                 }
14119                 ins = ins->next;
14120         } while(ins != first);
14121         /* Setup a dummy block 0 as a node above the start node */
14122         {
14123                 struct flow_block *fblock, *dst;
14124                 struct flow_edge *fedge;
14125                 fblock = &scc->flow_blocks[0];
14126                 fblock->block = 0;
14127                 fblock->in = 0;
14128                 fblock->out = &fblock->left;
14129                 dst = &scc->flow_blocks[state->first_block->vertex];
14130                 fedge = &fblock->left;
14131                 fedge->src        = fblock;
14132                 fedge->dst        = dst;
14133                 fedge->work_next  = fedge;
14134                 fedge->work_prev  = fedge;
14135                 fedge->in_next    = fedge->dst->in;
14136                 fedge->out_next   = 0;
14137                 fedge->executable = 0;
14138                 fedge->dst->in = fedge;
14139                 
14140                 /* Initialize the work lists */
14141                 scc->flow_work_list = 0;
14142                 scc->ssa_work_list  = 0;
14143                 scc_add_fedge(state, scc, fedge);
14144         }
14145 #if DEBUG_SCC
14146         fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14147                 ins_index, ssa_edge_index, fblock_index);
14148 #endif
14149 }
14150
14151         
14152 static void free_scc_state(
14153         struct compile_state *state, struct scc_state *scc)
14154 {
14155         xfree(scc->flow_blocks);
14156         xfree(scc->ssa_edges);
14157         xfree(scc->lattice);
14158         
14159 }
14160
14161 static struct lattice_node *triple_to_lattice(
14162         struct compile_state *state, struct scc_state *scc, struct triple *ins)
14163 {
14164         if (ins->id <= 0) {
14165                 internal_error(state, ins, "bad id");
14166         }
14167         return &scc->lattice[ins->id];
14168 }
14169
14170 static struct triple *preserve_lval(
14171         struct compile_state *state, struct lattice_node *lnode)
14172 {
14173         struct triple *old;
14174         /* Preserve the original value */
14175         if (lnode->val) {
14176                 old = dup_triple(state, lnode->val);
14177                 if (lnode->val != lnode->def) {
14178                         xfree(lnode->val);
14179                 }
14180                 lnode->val = 0;
14181         } else {
14182                 old = 0;
14183         }
14184         return old;
14185 }
14186
14187 static int lval_changed(struct compile_state *state, 
14188         struct triple *old, struct lattice_node *lnode)
14189 {
14190         int changed;
14191         /* See if the lattice value has changed */
14192         changed = 1;
14193         if (!old && !lnode->val) {
14194                 changed = 0;
14195         }
14196         if (changed && lnode->val && !is_const(lnode->val)) {
14197                 changed = 0;
14198         }
14199         if (changed &&
14200                 lnode->val && old &&
14201                 (memcmp(lnode->val->param, old->param,
14202                         TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14203                 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14204                 changed = 0;
14205         }
14206         if (old) {
14207                 xfree(old);
14208         }
14209         return changed;
14210
14211 }
14212
14213 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 
14214         struct lattice_node *lnode)
14215 {
14216         struct lattice_node *tmp;
14217         struct triple **slot, *old;
14218         struct flow_edge *fedge;
14219         int index;
14220         if (lnode->def->op != OP_PHI) {
14221                 internal_error(state, lnode->def, "not phi");
14222         }
14223         /* Store the original value */
14224         old = preserve_lval(state, lnode);
14225
14226         /* default to lattice high */
14227         lnode->val = lnode->def;
14228         slot = &RHS(lnode->def, 0);
14229         index = 0;
14230         for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14231                 if (!fedge->executable) {
14232                         continue;
14233                 }
14234                 if (!slot[index]) {
14235                         internal_error(state, lnode->def, "no phi value");
14236                 }
14237                 tmp = triple_to_lattice(state, scc, slot[index]);
14238                 /* meet(X, lattice low) = lattice low */
14239                 if (!tmp->val) {
14240                         lnode->val = 0;
14241                 }
14242                 /* meet(X, lattice high) = X */
14243                 else if (!tmp->val) {
14244                         lnode->val = lnode->val;
14245                 }
14246                 /* meet(lattice high, X) = X */
14247                 else if (!is_const(lnode->val)) {
14248                         lnode->val = dup_triple(state, tmp->val);
14249                         lnode->val->type = lnode->def->type;
14250                 }
14251                 /* meet(const, const) = const or lattice low */
14252                 else if (!constants_equal(state, lnode->val, tmp->val)) {
14253                         lnode->val = 0;
14254                 }
14255                 if (!lnode->val) {
14256                         break;
14257                 }
14258         }
14259 #if DEBUG_SCC
14260         fprintf(stderr, "phi: %d -> %s\n",
14261                 lnode->def->id,
14262                 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14263 #endif
14264         /* If the lattice value has changed update the work lists. */
14265         if (lval_changed(state, old, lnode)) {
14266                 struct ssa_edge *sedge;
14267                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14268                         scc_add_sedge(state, scc, sedge);
14269                 }
14270         }
14271 }
14272
14273 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14274         struct lattice_node *lnode)
14275 {
14276         int changed;
14277         struct triple *old, *scratch;
14278         struct triple **dexpr, **vexpr;
14279         int count, i;
14280         
14281         /* Store the original value */
14282         old = preserve_lval(state, lnode);
14283
14284         /* Reinitialize the value */
14285         lnode->val = scratch = dup_triple(state, lnode->def);
14286         scratch->id = lnode->old_id;
14287         scratch->next     = scratch;
14288         scratch->prev     = scratch;
14289         scratch->use      = 0;
14290
14291         count = TRIPLE_SIZE(scratch->sizes);
14292         for(i = 0; i < count; i++) {
14293                 dexpr = &lnode->def->param[i];
14294                 vexpr = &scratch->param[i];
14295                 *vexpr = *dexpr;
14296                 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14297                         (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14298                         *dexpr) {
14299                         struct lattice_node *tmp;
14300                         tmp = triple_to_lattice(state, scc, *dexpr);
14301                         *vexpr = (tmp->val)? tmp->val : tmp->def;
14302                 }
14303         }
14304         if (scratch->op == OP_BRANCH) {
14305                 scratch->next = lnode->def->next;
14306         }
14307         /* Recompute the value */
14308 #warning "FIXME see if simplify does anything bad"
14309         /* So far it looks like only the strength reduction
14310          * optimization are things I need to worry about.
14311          */
14312         simplify(state, scratch);
14313         /* Cleanup my value */
14314         if (scratch->use) {
14315                 internal_error(state, lnode->def, "scratch used?");
14316         }
14317         if ((scratch->prev != scratch) ||
14318                 ((scratch->next != scratch) &&
14319                         ((lnode->def->op != OP_BRANCH) ||
14320                                 (scratch->next != lnode->def->next)))) {
14321                 internal_error(state, lnode->def, "scratch in list?");
14322         }
14323         /* undo any uses... */
14324         count = TRIPLE_SIZE(scratch->sizes);
14325         for(i = 0; i < count; i++) {
14326                 vexpr = &scratch->param[i];
14327                 if (*vexpr) {
14328                         unuse_triple(*vexpr, scratch);
14329                 }
14330         }
14331         if (!is_const(scratch)) {
14332                 for(i = 0; i < count; i++) {
14333                         dexpr = &lnode->def->param[i];
14334                         if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14335                                 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14336                                 *dexpr) {
14337                                 struct lattice_node *tmp;
14338                                 tmp = triple_to_lattice(state, scc, *dexpr);
14339                                 if (!tmp->val) {
14340                                         lnode->val = 0;
14341                                 }
14342                         }
14343                 }
14344         }
14345         if (lnode->val && 
14346                 (lnode->val->op == lnode->def->op) &&
14347                 (memcmp(lnode->val->param, lnode->def->param, 
14348                         count * sizeof(lnode->val->param[0])) == 0) &&
14349                 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
14350                 lnode->val = lnode->def;
14351         }
14352         /* Find the cases that are always lattice lo */
14353         if (lnode->val && 
14354                 triple_is_def(state, lnode->val) &&
14355                 !triple_is_pure(state, lnode->val)) {
14356                 lnode->val = 0;
14357         }
14358         if (lnode->val && 
14359                 (lnode->val->op == OP_SDECL) && 
14360                 (lnode->val != lnode->def)) {
14361                 internal_error(state, lnode->def, "bad sdecl");
14362         }
14363         /* See if the lattice value has changed */
14364         changed = lval_changed(state, old, lnode);
14365         if (lnode->val != scratch) {
14366                 xfree(scratch);
14367         }
14368         return changed;
14369 }
14370
14371 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14372         struct lattice_node *lnode)
14373 {
14374         struct lattice_node *cond;
14375 #if DEBUG_SCC
14376         {
14377                 struct flow_edge *fedge;
14378                 fprintf(stderr, "branch: %d (",
14379                         lnode->def->id);
14380                 
14381                 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14382                         fprintf(stderr, " %d", fedge->dst->block->vertex);
14383                 }
14384                 fprintf(stderr, " )");
14385                 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
14386                         fprintf(stderr, " <- %d",
14387                                 RHS(lnode->def, 0)->id);
14388                 }
14389                 fprintf(stderr, "\n");
14390         }
14391 #endif
14392         if (lnode->def->op != OP_BRANCH) {
14393                 internal_error(state, lnode->def, "not branch");
14394         }
14395         /* This only applies to conditional branches */
14396         if (TRIPLE_RHS(lnode->def->sizes) == 0) {
14397                 return;
14398         }
14399         cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
14400         if (cond->val && !is_const(cond->val)) {
14401 #warning "FIXME do I need to do something here?"
14402                 warning(state, cond->def, "condition not constant?");
14403                 return;
14404         }
14405         if (cond->val == 0) {
14406                 scc_add_fedge(state, scc, cond->fblock->out);
14407                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14408         }
14409         else if (cond->val->u.cval) {
14410                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14411                 
14412         } else {
14413                 scc_add_fedge(state, scc, cond->fblock->out);
14414         }
14415
14416 }
14417
14418 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14419         struct lattice_node *lnode)
14420 {
14421         int changed;
14422
14423         changed = compute_lnode_val(state, scc, lnode);
14424 #if DEBUG_SCC
14425         {
14426                 struct triple **expr;
14427                 fprintf(stderr, "expr: %3d %10s (",
14428                         lnode->def->id, tops(lnode->def->op));
14429                 expr = triple_rhs(state, lnode->def, 0);
14430                 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
14431                         if (*expr) {
14432                                 fprintf(stderr, " %d", (*expr)->id);
14433                         }
14434                 }
14435                 fprintf(stderr, " ) -> %s\n",
14436                         (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14437         }
14438 #endif
14439         if (lnode->def->op == OP_BRANCH) {
14440                 scc_visit_branch(state, scc, lnode);
14441
14442         }
14443         else if (changed) {
14444                 struct ssa_edge *sedge;
14445                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14446                         scc_add_sedge(state, scc, sedge);
14447                 }
14448         }
14449 }
14450
14451 static void scc_writeback_values(
14452         struct compile_state *state, struct scc_state *scc)
14453 {
14454         struct triple *first, *ins;
14455         first = RHS(state->main_function, 0);
14456         ins = first;
14457         do {
14458                 struct lattice_node *lnode;
14459                 lnode = triple_to_lattice(state, scc, ins);
14460                 /* Restore id */
14461                 ins->id = lnode->old_id;
14462 #if DEBUG_SCC
14463                 if (lnode->val && !is_const(lnode->val)) {
14464                         warning(state, lnode->def, 
14465                                 "lattice node still high?");
14466                 }
14467 #endif
14468                 if (lnode->val && (lnode->val != ins)) {
14469                         /* See if it something I know how to write back */
14470                         switch(lnode->val->op) {
14471                         case OP_INTCONST:
14472                                 mkconst(state, ins, lnode->val->u.cval);
14473                                 break;
14474                         case OP_ADDRCONST:
14475                                 mkaddr_const(state, ins, 
14476                                         MISC(lnode->val, 0), lnode->val->u.cval);
14477                                 break;
14478                         default:
14479                                 /* By default don't copy the changes,
14480                                  * recompute them in place instead.
14481                                  */
14482                                 simplify(state, ins);
14483                                 break;
14484                         }
14485                         if (is_const(lnode->val) &&
14486                                 !constants_equal(state, lnode->val, ins)) {
14487                                 internal_error(state, 0, "constants not equal");
14488                         }
14489                         /* Free the lattice nodes */
14490                         xfree(lnode->val);
14491                         lnode->val = 0;
14492                 }
14493                 ins = ins->next;
14494         } while(ins != first);
14495 }
14496
14497 static void scc_transform(struct compile_state *state)
14498 {
14499         struct scc_state scc;
14500
14501         initialize_scc_state(state, &scc);
14502
14503         while(scc.flow_work_list || scc.ssa_work_list) {
14504                 struct flow_edge *fedge;
14505                 struct ssa_edge *sedge;
14506                 struct flow_edge *fptr;
14507                 while((fedge = scc_next_fedge(state, &scc))) {
14508                         struct block *block;
14509                         struct triple *ptr;
14510                         struct flow_block *fblock;
14511                         int time;
14512                         int done;
14513                         if (fedge->executable) {
14514                                 continue;
14515                         }
14516                         if (!fedge->dst) {
14517                                 internal_error(state, 0, "fedge without dst");
14518                         }
14519                         if (!fedge->src) {
14520                                 internal_error(state, 0, "fedge without src");
14521                         }
14522                         fedge->executable = 1;
14523                         fblock = fedge->dst;
14524                         block = fblock->block;
14525                         time = 0;
14526                         for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14527                                 if (fptr->executable) {
14528                                         time++;
14529                                 }
14530                         }
14531 #if DEBUG_SCC
14532                         fprintf(stderr, "vertex: %d time: %d\n", 
14533                                 block->vertex, time);
14534                         
14535 #endif
14536                         done = 0;
14537                         for(ptr = block->first; !done; ptr = ptr->next) {
14538                                 struct lattice_node *lnode;
14539                                 done = (ptr == block->last);
14540                                 lnode = &scc.lattice[ptr->id];
14541                                 if (ptr->op == OP_PHI) {
14542                                         scc_visit_phi(state, &scc, lnode);
14543                                 }
14544                                 else if (time == 1) {
14545                                         scc_visit_expr(state, &scc, lnode);
14546                                 }
14547                         }
14548                         if (fblock->out && !fblock->out->out_next) {
14549                                 scc_add_fedge(state, &scc, fblock->out);
14550                         }
14551                 }
14552                 while((sedge = scc_next_sedge(state, &scc))) {
14553                         struct lattice_node *lnode;
14554                         struct flow_block *fblock;
14555                         lnode = sedge->dst;
14556                         fblock = lnode->fblock;
14557 #if DEBUG_SCC
14558                         fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14559                                 sedge - scc.ssa_edges,
14560                                 sedge->src->def->id,
14561                                 sedge->dst->def->id);
14562 #endif
14563                         if (lnode->def->op == OP_PHI) {
14564                                 scc_visit_phi(state, &scc, lnode);
14565                         }
14566                         else {
14567                                 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14568                                         if (fptr->executable) {
14569                                                 break;
14570                                         }
14571                                 }
14572                                 if (fptr) {
14573                                         scc_visit_expr(state, &scc, lnode);
14574                                 }
14575                         }
14576                 }
14577         }
14578         
14579         scc_writeback_values(state, &scc);
14580         free_scc_state(state, &scc);
14581 }
14582
14583
14584 static void transform_to_arch_instructions(struct compile_state *state)
14585 {
14586         struct triple *ins, *first;
14587         first = RHS(state->main_function, 0);
14588         ins = first;
14589         do {
14590                 ins = transform_to_arch_instruction(state, ins);
14591         } while(ins != first);
14592 }
14593
14594 #if DEBUG_CONSISTENCY
14595 static void verify_uses(struct compile_state *state)
14596 {
14597         struct triple *first, *ins;
14598         struct triple_set *set;
14599         first = RHS(state->main_function, 0);
14600         ins = first;
14601         do {
14602                 struct triple **expr;
14603                 expr = triple_rhs(state, ins, 0);
14604                 for(; expr; expr = triple_rhs(state, ins, expr)) {
14605                         struct triple *rhs;
14606                         rhs = *expr;
14607                         for(set = rhs?rhs->use:0; set; set = set->next) {
14608                                 if (set->member == ins) {
14609                                         break;
14610                                 }
14611                         }
14612                         if (!set) {
14613                                 internal_error(state, ins, "rhs not used");
14614                         }
14615                 }
14616                 expr = triple_lhs(state, ins, 0);
14617                 for(; expr; expr = triple_lhs(state, ins, expr)) {
14618                         struct triple *lhs;
14619                         lhs = *expr;
14620                         for(set =  lhs?lhs->use:0; set; set = set->next) {
14621                                 if (set->member == ins) {
14622                                         break;
14623                                 }
14624                         }
14625                         if (!set) {
14626                                 internal_error(state, ins, "lhs not used");
14627                         }
14628                 }
14629                 ins = ins->next;
14630         } while(ins != first);
14631         
14632 }
14633 static void verify_blocks(struct compile_state *state)
14634 {
14635         struct triple *ins;
14636         struct block *block;
14637         block = state->first_block;
14638         if (!block) {
14639                 return;
14640         }
14641         do {
14642                 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14643                         if (!triple_stores_block(state, ins)) {
14644                                 continue;
14645                         }
14646                         if (ins->u.block != block) {
14647                                 internal_error(state, ins, "inconsitent block specified");
14648                         }
14649                 }
14650                 if (!triple_stores_block(state, block->last->next)) {
14651                         internal_error(state, block->last->next, 
14652                                 "cannot find next block");
14653                 }
14654                 block = block->last->next->u.block;
14655                 if (!block) {
14656                         internal_error(state, block->last->next,
14657                                 "bad next block");
14658                 }
14659         } while(block != state->first_block);
14660 }
14661
14662 static void verify_domination(struct compile_state *state)
14663 {
14664         struct triple *first, *ins;
14665         struct triple_set *set;
14666         if (!state->first_block) {
14667                 return;
14668         }
14669         
14670         first = RHS(state->main_function, 0);
14671         ins = first;
14672         do {
14673                 for(set = ins->use; set; set = set->next) {
14674                         struct triple **expr;
14675                         if (set->member->op == OP_PHI) {
14676                                 continue;
14677                         }
14678                         /* See if the use is on the righ hand side */
14679                         expr = triple_rhs(state, set->member, 0);
14680                         for(; expr ; expr = triple_rhs(state, set->member, expr)) {
14681                                 if (*expr == ins) {
14682                                         break;
14683                                 }
14684                         }
14685                         if (expr &&
14686                                 !tdominates(state, ins, set->member)) {
14687                                 internal_error(state, set->member, 
14688                                         "non dominated rhs use?");
14689                         }
14690                 }
14691                 ins = ins->next;
14692         } while(ins != first);
14693 }
14694
14695 static void verify_piece(struct compile_state *state)
14696 {
14697         struct triple *first, *ins;
14698         first = RHS(state->main_function, 0);
14699         ins = first;
14700         do {
14701                 struct triple *ptr;
14702                 int lhs, i;
14703                 lhs = TRIPLE_LHS(ins->sizes);
14704                 if ((ins->op == OP_WRITE) || (ins->op == OP_STORE)) {
14705                         lhs = 0;
14706                 }
14707                 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
14708                         if (ptr != LHS(ins, i)) {
14709                                 internal_error(state, ins, "malformed lhs on %s",
14710                                         tops(ins->op));
14711                         }
14712                         if (ptr->op != OP_PIECE) {
14713                                 internal_error(state, ins, "bad lhs op %s at %d on %s",
14714                                         tops(ptr->op), i, tops(ins->op));
14715                         }
14716                         if (ptr->u.cval != i) {
14717                                 internal_error(state, ins, "bad u.cval of %d %d expected",
14718                                         ptr->u.cval, i);
14719                         }
14720                 }
14721                 ins = ins->next;
14722         } while(ins != first);
14723 }
14724 static void verify_ins_colors(struct compile_state *state)
14725 {
14726         struct triple *first, *ins;
14727         
14728         first = RHS(state->main_function, 0);
14729         ins = first;
14730         do {
14731                 ins = ins->next;
14732         } while(ins != first);
14733 }
14734 static void verify_consistency(struct compile_state *state)
14735 {
14736         verify_uses(state);
14737         verify_blocks(state);
14738         verify_domination(state);
14739         verify_piece(state);
14740         verify_ins_colors(state);
14741 }
14742 #else 
14743 static void verify_consistency(struct compile_state *state) {}
14744 #endif /* DEBUG_USES */
14745
14746 static void optimize(struct compile_state *state)
14747 {
14748         if (state->debug & DEBUG_TRIPLES) {
14749                 print_triples(state);
14750         }
14751         /* Replace structures with simpler data types */
14752         flatten_structures(state);
14753         if (state->debug & DEBUG_TRIPLES) {
14754                 print_triples(state);
14755         }
14756         verify_consistency(state);
14757         /* Analize the intermediate code */
14758         setup_basic_blocks(state);
14759         analyze_idominators(state);
14760         analyze_ipdominators(state);
14761         /* Transform the code to ssa form */
14762         transform_to_ssa_form(state);
14763         verify_consistency(state);
14764         if (state->debug & DEBUG_CODE_ELIMINATION) {
14765                 fprintf(stdout, "After transform_to_ssa_form\n");
14766                 print_blocks(state, stdout);
14767         }
14768         /* Do strength reduction and simple constant optimizations */
14769         if (state->optimize >= 1) {
14770                 simplify_all(state);
14771         }
14772         verify_consistency(state);
14773         /* Propogate constants throughout the code */
14774         if (state->optimize >= 2) {
14775 #warning "FIXME fix scc_transform"
14776                 scc_transform(state);
14777                 transform_from_ssa_form(state);
14778                 free_basic_blocks(state);
14779                 setup_basic_blocks(state);
14780                 analyze_idominators(state);
14781                 analyze_ipdominators(state);
14782                 transform_to_ssa_form(state);
14783         }
14784         verify_consistency(state);
14785 #warning "WISHLIST implement single use constants (least possible register pressure)"
14786 #warning "WISHLIST implement induction variable elimination"
14787         /* Select architecture instructions and an initial partial
14788          * coloring based on architecture constraints.
14789          */
14790         transform_to_arch_instructions(state);
14791         verify_consistency(state);
14792         if (state->debug & DEBUG_ARCH_CODE) {
14793                 printf("After transform_to_arch_instructions\n");
14794                 print_blocks(state, stdout);
14795                 print_control_flow(state);
14796         }
14797         eliminate_inefectual_code(state);
14798         verify_consistency(state);
14799         if (state->debug & DEBUG_CODE_ELIMINATION) {
14800                 printf("After eliminate_inefectual_code\n");
14801                 print_blocks(state, stdout);
14802                 print_control_flow(state);
14803         }
14804         verify_consistency(state);
14805         /* Color all of the variables to see if they will fit in registers */
14806         insert_copies_to_phi(state);
14807         if (state->debug & DEBUG_INSERTED_COPIES) {
14808                 printf("After insert_copies_to_phi\n");
14809                 print_blocks(state, stdout);
14810                 print_control_flow(state);
14811         }
14812         verify_consistency(state);
14813         insert_mandatory_copies(state);
14814         if (state->debug & DEBUG_INSERTED_COPIES) {
14815                 printf("After insert_mandatory_copies\n");
14816                 print_blocks(state, stdout);
14817                 print_control_flow(state);
14818         }
14819         verify_consistency(state);
14820         allocate_registers(state);
14821         verify_consistency(state);
14822         if (state->debug & DEBUG_INTERMEDIATE_CODE) {
14823                 print_blocks(state, stdout);
14824         }
14825         if (state->debug & DEBUG_CONTROL_FLOW) {
14826                 print_control_flow(state);
14827         }
14828         /* Remove the optimization information.
14829          * This is more to check for memory consistency than to free memory.
14830          */
14831         free_basic_blocks(state);
14832 }
14833
14834 static void print_op_asm(struct compile_state *state,
14835         struct triple *ins, FILE *fp)
14836 {
14837         struct asm_info *info;
14838         const char *ptr;
14839         unsigned lhs, rhs, i;
14840         info = ins->u.ainfo;
14841         lhs = TRIPLE_LHS(ins->sizes);
14842         rhs = TRIPLE_RHS(ins->sizes);
14843         /* Don't count the clobbers in lhs */
14844         for(i = 0; i < lhs; i++) {
14845                 if (LHS(ins, i)->type == &void_type) {
14846                         break;
14847                 }
14848         }
14849         lhs = i;
14850         fprintf(fp, "#ASM\n");
14851         fputc('\t', fp);
14852         for(ptr = info->str; *ptr; ptr++) {
14853                 char *next;
14854                 unsigned long param;
14855                 struct triple *piece;
14856                 if (*ptr != '%') {
14857                         fputc(*ptr, fp);
14858                         continue;
14859                 }
14860                 ptr++;
14861                 if (*ptr == '%') {
14862                         fputc('%', fp);
14863                         continue;
14864                 }
14865                 param = strtoul(ptr, &next, 10);
14866                 if (ptr == next) {
14867                         error(state, ins, "Invalid asm template");
14868                 }
14869                 if (param >= (lhs + rhs)) {
14870                         error(state, ins, "Invalid param %%%u in asm template",
14871                                 param);
14872                 }
14873                 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
14874                 fprintf(fp, "%s", 
14875                         arch_reg_str(ID_REG(piece->id)));
14876                 ptr = next -1;
14877         }
14878         fprintf(fp, "\n#NOT ASM\n");
14879 }
14880
14881
14882 /* Only use the low x86 byte registers.  This allows me
14883  * allocate the entire register when a byte register is used.
14884  */
14885 #define X86_4_8BIT_GPRS 1
14886
14887 /* Recognized x86 cpu variants */
14888 #define BAD_CPU      0
14889 #define CPU_I386     1
14890 #define CPU_P3       2
14891 #define CPU_P4       3
14892 #define CPU_K7       4
14893 #define CPU_K8       5
14894
14895 #define CPU_DEFAULT  CPU_I386
14896
14897 /* The x86 register classes */
14898 #define REGC_FLAGS    0
14899 #define REGC_GPR8     1
14900 #define REGC_GPR16    2
14901 #define REGC_GPR32    3
14902 #define REGC_GPR64    4
14903 #define REGC_MMX      5
14904 #define REGC_XMM      6
14905 #define REGC_GPR32_8  7
14906 #define REGC_GPR16_8  8
14907 #define REGC_IMM32    9
14908 #define REGC_IMM16   10
14909 #define REGC_IMM8    11
14910 #define LAST_REGC  REGC_IMM8
14911 #if LAST_REGC >= MAX_REGC
14912 #error "MAX_REGC is to low"
14913 #endif
14914
14915 /* Register class masks */
14916 #define REGCM_FLAGS   (1 << REGC_FLAGS)
14917 #define REGCM_GPR8    (1 << REGC_GPR8)
14918 #define REGCM_GPR16   (1 << REGC_GPR16)
14919 #define REGCM_GPR32   (1 << REGC_GPR32)
14920 #define REGCM_GPR64   (1 << REGC_GPR64)
14921 #define REGCM_MMX     (1 << REGC_MMX)
14922 #define REGCM_XMM     (1 << REGC_XMM)
14923 #define REGCM_GPR32_8 (1 << REGC_GPR32_8)
14924 #define REGCM_GPR16_8 (1 << REGC_GPR16_8)
14925 #define REGCM_IMM32   (1 << REGC_IMM32)
14926 #define REGCM_IMM16   (1 << REGC_IMM16)
14927 #define REGCM_IMM8    (1 << REGC_IMM8)
14928 #define REGCM_ALL     ((1 << (LAST_REGC + 1)) - 1)
14929
14930 /* The x86 registers */
14931 #define REG_EFLAGS  2
14932 #define REGC_FLAGS_FIRST REG_EFLAGS
14933 #define REGC_FLAGS_LAST  REG_EFLAGS
14934 #define REG_AL      3
14935 #define REG_BL      4
14936 #define REG_CL      5
14937 #define REG_DL      6
14938 #define REG_AH      7
14939 #define REG_BH      8
14940 #define REG_CH      9
14941 #define REG_DH      10
14942 #define REGC_GPR8_FIRST  REG_AL
14943 #if X86_4_8BIT_GPRS
14944 #define REGC_GPR8_LAST   REG_DL
14945 #else 
14946 #define REGC_GPR8_LAST   REG_DH
14947 #endif
14948 #define REG_AX     11
14949 #define REG_BX     12
14950 #define REG_CX     13
14951 #define REG_DX     14
14952 #define REG_SI     15
14953 #define REG_DI     16
14954 #define REG_BP     17
14955 #define REG_SP     18
14956 #define REGC_GPR16_FIRST REG_AX
14957 #define REGC_GPR16_LAST  REG_SP
14958 #define REG_EAX    19
14959 #define REG_EBX    20
14960 #define REG_ECX    21
14961 #define REG_EDX    22
14962 #define REG_ESI    23
14963 #define REG_EDI    24
14964 #define REG_EBP    25
14965 #define REG_ESP    26
14966 #define REGC_GPR32_FIRST REG_EAX
14967 #define REGC_GPR32_LAST  REG_ESP
14968 #define REG_EDXEAX 27
14969 #define REGC_GPR64_FIRST REG_EDXEAX
14970 #define REGC_GPR64_LAST  REG_EDXEAX
14971 #define REG_MMX0   28
14972 #define REG_MMX1   29
14973 #define REG_MMX2   30
14974 #define REG_MMX3   31
14975 #define REG_MMX4   32
14976 #define REG_MMX5   33
14977 #define REG_MMX6   34
14978 #define REG_MMX7   35
14979 #define REGC_MMX_FIRST REG_MMX0
14980 #define REGC_MMX_LAST  REG_MMX7
14981 #define REG_XMM0   36
14982 #define REG_XMM1   37
14983 #define REG_XMM2   38
14984 #define REG_XMM3   39
14985 #define REG_XMM4   40
14986 #define REG_XMM5   41
14987 #define REG_XMM6   42
14988 #define REG_XMM7   43
14989 #define REGC_XMM_FIRST REG_XMM0
14990 #define REGC_XMM_LAST  REG_XMM7
14991 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
14992 #define LAST_REG   REG_XMM7
14993
14994 #define REGC_GPR32_8_FIRST REG_EAX
14995 #define REGC_GPR32_8_LAST  REG_EDX
14996 #define REGC_GPR16_8_FIRST REG_AX
14997 #define REGC_GPR16_8_LAST  REG_DX
14998
14999 #define REGC_IMM8_FIRST    -1
15000 #define REGC_IMM8_LAST     -1
15001 #define REGC_IMM16_FIRST   -2
15002 #define REGC_IMM16_LAST    -1
15003 #define REGC_IMM32_FIRST   -4
15004 #define REGC_IMM32_LAST    -1
15005
15006 #if LAST_REG >= MAX_REGISTERS
15007 #error "MAX_REGISTERS to low"
15008 #endif
15009
15010
15011 static unsigned regc_size[LAST_REGC +1] = {
15012         [REGC_FLAGS]   = REGC_FLAGS_LAST   - REGC_FLAGS_FIRST + 1,
15013         [REGC_GPR8]    = REGC_GPR8_LAST    - REGC_GPR8_FIRST + 1,
15014         [REGC_GPR16]   = REGC_GPR16_LAST   - REGC_GPR16_FIRST + 1,
15015         [REGC_GPR32]   = REGC_GPR32_LAST   - REGC_GPR32_FIRST + 1,
15016         [REGC_GPR64]   = REGC_GPR64_LAST   - REGC_GPR64_FIRST + 1,
15017         [REGC_MMX]     = REGC_MMX_LAST     - REGC_MMX_FIRST + 1,
15018         [REGC_XMM]     = REGC_XMM_LAST     - REGC_XMM_FIRST + 1,
15019         [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
15020         [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
15021         [REGC_IMM32]   = 0,
15022         [REGC_IMM16]   = 0,
15023         [REGC_IMM8]    = 0,
15024 };
15025
15026 static const struct {
15027         int first, last;
15028 } regcm_bound[LAST_REGC + 1] = {
15029         [REGC_FLAGS]   = { REGC_FLAGS_FIRST,   REGC_FLAGS_LAST },
15030         [REGC_GPR8]    = { REGC_GPR8_FIRST,    REGC_GPR8_LAST },
15031         [REGC_GPR16]   = { REGC_GPR16_FIRST,   REGC_GPR16_LAST },
15032         [REGC_GPR32]   = { REGC_GPR32_FIRST,   REGC_GPR32_LAST },
15033         [REGC_GPR64]   = { REGC_GPR64_FIRST,   REGC_GPR64_LAST },
15034         [REGC_MMX]     = { REGC_MMX_FIRST,     REGC_MMX_LAST },
15035         [REGC_XMM]     = { REGC_XMM_FIRST,     REGC_XMM_LAST },
15036         [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
15037         [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
15038         [REGC_IMM32]   = { REGC_IMM32_FIRST,   REGC_IMM32_LAST },
15039         [REGC_IMM16]   = { REGC_IMM16_FIRST,   REGC_IMM16_LAST },
15040         [REGC_IMM8]    = { REGC_IMM8_FIRST,    REGC_IMM8_LAST },
15041 };
15042
15043 static int arch_encode_cpu(const char *cpu)
15044 {
15045         struct cpu {
15046                 const char *name;
15047                 int cpu;
15048         } cpus[] = {
15049                 { "i386", CPU_I386 },
15050                 { "p3",   CPU_P3 },
15051                 { "p4",   CPU_P4 },
15052                 { "k7",   CPU_K7 },
15053                 { "k8",   CPU_K8 },
15054                 {  0,     BAD_CPU }
15055         };
15056         struct cpu *ptr;
15057         for(ptr = cpus; ptr->name; ptr++) {
15058                 if (strcmp(ptr->name, cpu) == 0) {
15059                         break;
15060                 }
15061         }
15062         return ptr->cpu;
15063 }
15064
15065 static unsigned arch_regc_size(struct compile_state *state, int class)
15066 {
15067         if ((class < 0) || (class > LAST_REGC)) {
15068                 return 0;
15069         }
15070         return regc_size[class];
15071 }
15072 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15073 {
15074         /* See if two register classes may have overlapping registers */
15075         unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 |
15076                 REGCM_GPR32_8 | REGCM_GPR32 | REGCM_GPR64;
15077
15078         /* Special case for the immediates */
15079         if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15080                 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15081                 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15082                 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { 
15083                 return 0;
15084         }
15085         return (regcm1 & regcm2) ||
15086                 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15087 }
15088
15089 static void arch_reg_equivs(
15090         struct compile_state *state, unsigned *equiv, int reg)
15091 {
15092         if ((reg < 0) || (reg > LAST_REG)) {
15093                 internal_error(state, 0, "invalid register");
15094         }
15095         *equiv++ = reg;
15096         switch(reg) {
15097         case REG_AL:
15098 #if X86_4_8BIT_GPRS
15099                 *equiv++ = REG_AH;
15100 #endif
15101                 *equiv++ = REG_AX;
15102                 *equiv++ = REG_EAX;
15103                 *equiv++ = REG_EDXEAX;
15104                 break;
15105         case REG_AH:
15106 #if X86_4_8BIT_GPRS
15107                 *equiv++ = REG_AL;
15108 #endif
15109                 *equiv++ = REG_AX;
15110                 *equiv++ = REG_EAX;
15111                 *equiv++ = REG_EDXEAX;
15112                 break;
15113         case REG_BL:  
15114 #if X86_4_8BIT_GPRS
15115                 *equiv++ = REG_BH;
15116 #endif
15117                 *equiv++ = REG_BX;
15118                 *equiv++ = REG_EBX;
15119                 break;
15120
15121         case REG_BH:
15122 #if X86_4_8BIT_GPRS
15123                 *equiv++ = REG_BL;
15124 #endif
15125                 *equiv++ = REG_BX;
15126                 *equiv++ = REG_EBX;
15127                 break;
15128         case REG_CL:
15129 #if X86_4_8BIT_GPRS
15130                 *equiv++ = REG_CH;
15131 #endif
15132                 *equiv++ = REG_CX;
15133                 *equiv++ = REG_ECX;
15134                 break;
15135
15136         case REG_CH:
15137 #if X86_4_8BIT_GPRS
15138                 *equiv++ = REG_CL;
15139 #endif
15140                 *equiv++ = REG_CX;
15141                 *equiv++ = REG_ECX;
15142                 break;
15143         case REG_DL:
15144 #if X86_4_8BIT_GPRS
15145                 *equiv++ = REG_DH;
15146 #endif
15147                 *equiv++ = REG_DX;
15148                 *equiv++ = REG_EDX;
15149                 *equiv++ = REG_EDXEAX;
15150                 break;
15151         case REG_DH:
15152 #if X86_4_8BIT_GPRS
15153                 *equiv++ = REG_DL;
15154 #endif
15155                 *equiv++ = REG_DX;
15156                 *equiv++ = REG_EDX;
15157                 *equiv++ = REG_EDXEAX;
15158                 break;
15159         case REG_AX:
15160                 *equiv++ = REG_AL;
15161                 *equiv++ = REG_AH;
15162                 *equiv++ = REG_EAX;
15163                 *equiv++ = REG_EDXEAX;
15164                 break;
15165         case REG_BX:
15166                 *equiv++ = REG_BL;
15167                 *equiv++ = REG_BH;
15168                 *equiv++ = REG_EBX;
15169                 break;
15170         case REG_CX:  
15171                 *equiv++ = REG_CL;
15172                 *equiv++ = REG_CH;
15173                 *equiv++ = REG_ECX;
15174                 break;
15175         case REG_DX:  
15176                 *equiv++ = REG_DL;
15177                 *equiv++ = REG_DH;
15178                 *equiv++ = REG_EDX;
15179                 *equiv++ = REG_EDXEAX;
15180                 break;
15181         case REG_SI:  
15182                 *equiv++ = REG_ESI;
15183                 break;
15184         case REG_DI:
15185                 *equiv++ = REG_EDI;
15186                 break;
15187         case REG_BP:
15188                 *equiv++ = REG_EBP;
15189                 break;
15190         case REG_SP:
15191                 *equiv++ = REG_ESP;
15192                 break;
15193         case REG_EAX:
15194                 *equiv++ = REG_AL;
15195                 *equiv++ = REG_AH;
15196                 *equiv++ = REG_AX;
15197                 *equiv++ = REG_EDXEAX;
15198                 break;
15199         case REG_EBX:
15200                 *equiv++ = REG_BL;
15201                 *equiv++ = REG_BH;
15202                 *equiv++ = REG_BX;
15203                 break;
15204         case REG_ECX:
15205                 *equiv++ = REG_CL;
15206                 *equiv++ = REG_CH;
15207                 *equiv++ = REG_CX;
15208                 break;
15209         case REG_EDX:
15210                 *equiv++ = REG_DL;
15211                 *equiv++ = REG_DH;
15212                 *equiv++ = REG_DX;
15213                 *equiv++ = REG_EDXEAX;
15214                 break;
15215         case REG_ESI: 
15216                 *equiv++ = REG_SI;
15217                 break;
15218         case REG_EDI: 
15219                 *equiv++ = REG_DI;
15220                 break;
15221         case REG_EBP: 
15222                 *equiv++ = REG_BP;
15223                 break;
15224         case REG_ESP: 
15225                 *equiv++ = REG_SP;
15226                 break;
15227         case REG_EDXEAX: 
15228                 *equiv++ = REG_AL;
15229                 *equiv++ = REG_AH;
15230                 *equiv++ = REG_DL;
15231                 *equiv++ = REG_DH;
15232                 *equiv++ = REG_AX;
15233                 *equiv++ = REG_DX;
15234                 *equiv++ = REG_EAX;
15235                 *equiv++ = REG_EDX;
15236                 break;
15237         }
15238         *equiv++ = REG_UNSET; 
15239 }
15240
15241 static unsigned arch_avail_mask(struct compile_state *state)
15242 {
15243         unsigned avail_mask;
15244         avail_mask = REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16 | 
15245                 REGCM_GPR32 | REGCM_GPR32_8 | REGCM_GPR64 |
15246                 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15247         switch(state->cpu) {
15248         case CPU_P3:
15249         case CPU_K7:
15250                 avail_mask |= REGCM_MMX;
15251                 break;
15252         case CPU_P4:
15253         case CPU_K8:
15254                 avail_mask |= REGCM_MMX | REGCM_XMM;
15255                 break;
15256         }
15257 #if 0
15258         /* Don't enable 8 bit values until I can force both operands
15259          * to be 8bits simultaneously.
15260          */
15261         avail_mask &= ~(REGCM_GPR8 | REGCM_GPR16_8 | REGCM_GPR16);
15262 #endif
15263         return avail_mask;
15264 }
15265
15266 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15267 {
15268         unsigned mask, result;
15269         int class, class2;
15270         result = regcm;
15271         result &= arch_avail_mask(state);
15272
15273         for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15274                 if ((result & mask) == 0) {
15275                         continue;
15276                 }
15277                 if (class > LAST_REGC) {
15278                         result &= ~mask;
15279                 }
15280                 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15281                         if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15282                                 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15283                                 result |= (1 << class2);
15284                         }
15285                 }
15286         }
15287         return result;
15288 }
15289
15290 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15291 {
15292         unsigned mask;
15293         int class;
15294         mask = 0;
15295         for(class = 0; class <= LAST_REGC; class++) {
15296                 if ((reg >= regcm_bound[class].first) &&
15297                         (reg <= regcm_bound[class].last)) {
15298                         mask |= (1 << class);
15299                 }
15300         }
15301         if (!mask) {
15302                 internal_error(state, 0, "reg %d not in any class", reg);
15303         }
15304         return mask;
15305 }
15306
15307 static struct reg_info arch_reg_constraint(
15308         struct compile_state *state, struct type *type, const char *constraint)
15309 {
15310         static const struct {
15311                 char class;
15312                 unsigned int mask;
15313                 unsigned int reg;
15314         } constraints[] = {
15315                 { 'r', REGCM_GPR32, REG_UNSET },
15316                 { 'g', REGCM_GPR32, REG_UNSET },
15317                 { 'p', REGCM_GPR32, REG_UNSET },
15318                 { 'q', REGCM_GPR8,  REG_UNSET },
15319                 { 'Q', REGCM_GPR32_8, REG_UNSET },
15320                 { 'x', REGCM_XMM,   REG_UNSET },
15321                 { 'y', REGCM_MMX,   REG_UNSET },
15322                 { 'a', REGCM_GPR32, REG_EAX },
15323                 { 'b', REGCM_GPR32, REG_EBX },
15324                 { 'c', REGCM_GPR32, REG_ECX },
15325                 { 'd', REGCM_GPR32, REG_EDX },
15326                 { 'D', REGCM_GPR32, REG_EDI },
15327                 { 'S', REGCM_GPR32, REG_ESI },
15328                 { '\0', 0, REG_UNSET },
15329         };
15330         unsigned int regcm;
15331         unsigned int mask, reg;
15332         struct reg_info result;
15333         const char *ptr;
15334         regcm = arch_type_to_regcm(state, type);
15335         reg = REG_UNSET;
15336         mask = 0;
15337         for(ptr = constraint; *ptr; ptr++) {
15338                 int i;
15339                 if (*ptr ==  ' ') {
15340                         continue;
15341                 }
15342                 for(i = 0; constraints[i].class != '\0'; i++) {
15343                         if (constraints[i].class == *ptr) {
15344                                 break;
15345                         }
15346                 }
15347                 if (constraints[i].class == '\0') {
15348                         error(state, 0, "invalid register constraint ``%c''", *ptr);
15349                         break;
15350                 }
15351                 if ((constraints[i].mask & regcm) == 0) {
15352                         error(state, 0, "invalid register class %c specified",
15353                                 *ptr);
15354                 }
15355                 mask |= constraints[i].mask;
15356                 if (constraints[i].reg != REG_UNSET) {
15357                         if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15358                                 error(state, 0, "Only one register may be specified");
15359                         }
15360                         reg = constraints[i].reg;
15361                 }
15362         }
15363         result.reg = reg;
15364         result.regcm = mask;
15365         return result;
15366 }
15367
15368 static struct reg_info arch_reg_clobber(
15369         struct compile_state *state, const char *clobber)
15370 {
15371         struct reg_info result;
15372         if (strcmp(clobber, "memory") == 0) {
15373                 result.reg = REG_UNSET;
15374                 result.regcm = 0;
15375         }
15376         else if (strcmp(clobber, "%eax") == 0) {
15377                 result.reg = REG_EAX;
15378                 result.regcm = REGCM_GPR32;
15379         }
15380         else if (strcmp(clobber, "%ebx") == 0) {
15381                 result.reg = REG_EBX;
15382                 result.regcm = REGCM_GPR32;
15383         }
15384         else if (strcmp(clobber, "%ecx") == 0) {
15385                 result.reg = REG_ECX;
15386                 result.regcm = REGCM_GPR32;
15387         }
15388         else if (strcmp(clobber, "%edx") == 0) {
15389                 result.reg = REG_EDX;
15390                 result.regcm = REGCM_GPR32;
15391         }
15392         else if (strcmp(clobber, "%esi") == 0) {
15393                 result.reg = REG_ESI;
15394                 result.regcm = REGCM_GPR32;
15395         }
15396         else if (strcmp(clobber, "%edi") == 0) {
15397                 result.reg = REG_EDI;
15398                 result.regcm = REGCM_GPR32;
15399         }
15400         else if (strcmp(clobber, "%ebp") == 0) {
15401                 result.reg = REG_EBP;
15402                 result.regcm = REGCM_GPR32;
15403         }
15404         else if (strcmp(clobber, "%esp") == 0) {
15405                 result.reg = REG_ESP;
15406                 result.regcm = REGCM_GPR32;
15407         }
15408         else if (strcmp(clobber, "cc") == 0) {
15409                 result.reg = REG_EFLAGS;
15410                 result.regcm = REGCM_FLAGS;
15411         }
15412         else if ((strncmp(clobber, "xmm", 3) == 0)  &&
15413                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15414                 result.reg = REG_XMM0 + octdigval(clobber[3]);
15415                 result.regcm = REGCM_XMM;
15416         }
15417         else if ((strncmp(clobber, "mmx", 3) == 0) &&
15418                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15419                 result.reg = REG_MMX0 + octdigval(clobber[3]);
15420                 result.regcm = REGCM_MMX;
15421         }
15422         else {
15423                 error(state, 0, "Invalid register clobber");
15424                 result.reg = REG_UNSET;
15425                 result.regcm = 0;
15426         }
15427         return result;
15428 }
15429
15430 static int do_select_reg(struct compile_state *state, 
15431         char *used, int reg, unsigned classes)
15432 {
15433         unsigned mask;
15434         if (used[reg]) {
15435                 return REG_UNSET;
15436         }
15437         mask = arch_reg_regcm(state, reg);
15438         return (classes & mask) ? reg : REG_UNSET;
15439 }
15440
15441 static int arch_select_free_register(
15442         struct compile_state *state, char *used, int classes)
15443 {
15444         /* Preference: flags, 8bit gprs, 32bit gprs, other 32bit reg
15445          * other types of registers.
15446          */
15447         int i, reg;
15448         reg = REG_UNSET;
15449         for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15450                 reg = do_select_reg(state, used, i, classes);
15451         }
15452         for(i = REGC_GPR32_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR32_LAST); i++) {
15453                 reg = do_select_reg(state, used, i, classes);
15454         }
15455         for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15456                 reg = do_select_reg(state, used, i, classes);
15457         }
15458         for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15459                 reg = do_select_reg(state, used, i, classes);
15460         }
15461         for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15462                 reg = do_select_reg(state, used, i, classes);
15463         }
15464         for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15465                 reg = do_select_reg(state, used, i, classes);
15466         }
15467         for(i = REGC_GPR64_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR64_LAST); i++) {
15468                 reg = do_select_reg(state, used, i, classes);
15469         }
15470         return reg;
15471 }
15472
15473
15474 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) 
15475 {
15476 #warning "FIXME force types smaller (if legal) before I get here"
15477         unsigned avail_mask;
15478         unsigned mask;
15479         mask = 0;
15480         avail_mask = arch_avail_mask(state);
15481         switch(type->type & TYPE_MASK) {
15482         case TYPE_ARRAY:
15483         case TYPE_VOID: 
15484                 mask = 0; 
15485                 break;
15486         case TYPE_CHAR:
15487         case TYPE_UCHAR:
15488                 mask = REGCM_GPR8 | 
15489                         REGCM_GPR16 | REGCM_GPR16_8 | 
15490                         REGCM_GPR32 | REGCM_GPR32_8 |
15491                         REGCM_GPR64 |
15492                         REGCM_MMX | REGCM_XMM |
15493                         REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
15494                 break;
15495         case TYPE_SHORT:
15496         case TYPE_USHORT:
15497                 mask =  REGCM_GPR16 | REGCM_GPR16_8 |
15498                         REGCM_GPR32 | REGCM_GPR32_8 |
15499                         REGCM_GPR64 |
15500                         REGCM_MMX | REGCM_XMM |
15501                         REGCM_IMM32 | REGCM_IMM16;
15502                 break;
15503         case TYPE_INT:
15504         case TYPE_UINT:
15505         case TYPE_LONG:
15506         case TYPE_ULONG:
15507         case TYPE_POINTER:
15508                 mask =  REGCM_GPR32 | REGCM_GPR32_8 |
15509                         REGCM_GPR64 | REGCM_MMX | REGCM_XMM |
15510                         REGCM_IMM32;
15511                 break;
15512         default:
15513                 internal_error(state, 0, "no register class for type");
15514                 break;
15515         }
15516         mask &= avail_mask;
15517         return mask;
15518 }
15519
15520 static int is_imm32(struct triple *imm)
15521 {
15522         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15523                 (imm->op == OP_ADDRCONST);
15524         
15525 }
15526 static int is_imm16(struct triple *imm)
15527 {
15528         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15529 }
15530 static int is_imm8(struct triple *imm)
15531 {
15532         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15533 }
15534
15535 static int get_imm32(struct triple *ins, struct triple **expr)
15536 {
15537         struct triple *imm;
15538         imm = *expr;
15539         while(imm->op == OP_COPY) {
15540                 imm = RHS(imm, 0);
15541         }
15542         if (!is_imm32(imm)) {
15543                 return 0;
15544         }
15545         unuse_triple(*expr, ins);
15546         use_triple(imm, ins);
15547         *expr = imm;
15548         return 1;
15549 }
15550
15551 static int get_imm8(struct triple *ins, struct triple **expr)
15552 {
15553         struct triple *imm;
15554         imm = *expr;
15555         while(imm->op == OP_COPY) {
15556                 imm = RHS(imm, 0);
15557         }
15558         if (!is_imm8(imm)) {
15559                 return 0;
15560         }
15561         unuse_triple(*expr, ins);
15562         use_triple(imm, ins);
15563         *expr = imm;
15564         return 1;
15565 }
15566
15567 #define TEMPLATE_NOP         0
15568 #define TEMPLATE_INTCONST8   1
15569 #define TEMPLATE_INTCONST32  2
15570 #define TEMPLATE_COPY_REG    3
15571 #define TEMPLATE_COPY_IMM32  4
15572 #define TEMPLATE_COPY_IMM16  5
15573 #define TEMPLATE_COPY_IMM8   6
15574 #define TEMPLATE_PHI         7
15575 #define TEMPLATE_STORE8      8
15576 #define TEMPLATE_STORE16     9
15577 #define TEMPLATE_STORE32    10
15578 #define TEMPLATE_LOAD8      11
15579 #define TEMPLATE_LOAD16     12
15580 #define TEMPLATE_LOAD32     13
15581 #define TEMPLATE_BINARY_REG 14
15582 #define TEMPLATE_BINARY_IMM 15
15583 #define TEMPLATE_SL_CL      16
15584 #define TEMPLATE_SL_IMM     17
15585 #define TEMPLATE_UNARY      18
15586 #define TEMPLATE_CMP_REG    19
15587 #define TEMPLATE_CMP_IMM    20
15588 #define TEMPLATE_TEST       21
15589 #define TEMPLATE_SET        22
15590 #define TEMPLATE_JMP        23
15591 #define TEMPLATE_INB_DX     24
15592 #define TEMPLATE_INB_IMM    25
15593 #define TEMPLATE_INW_DX     26
15594 #define TEMPLATE_INW_IMM    27
15595 #define TEMPLATE_INL_DX     28
15596 #define TEMPLATE_INL_IMM    29
15597 #define TEMPLATE_OUTB_DX    30
15598 #define TEMPLATE_OUTB_IMM   31
15599 #define TEMPLATE_OUTW_DX    32
15600 #define TEMPLATE_OUTW_IMM   33
15601 #define TEMPLATE_OUTL_DX    34
15602 #define TEMPLATE_OUTL_IMM   35
15603 #define TEMPLATE_BSF        36
15604 #define TEMPLATE_RDMSR      37
15605 #define TEMPLATE_WRMSR      38
15606 #define LAST_TEMPLATE       TEMPLATE_WRMSR
15607 #if LAST_TEMPLATE >= MAX_TEMPLATES
15608 #error "MAX_TEMPLATES to low"
15609 #endif
15610
15611 #define COPY_REGCM (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8 | REGCM_MMX | REGCM_XMM)
15612 #define COPY32_REGCM (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
15613
15614 static struct ins_template templates[] = {
15615         [TEMPLATE_NOP]      = {},
15616         [TEMPLATE_INTCONST8] = { 
15617                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15618         },
15619         [TEMPLATE_INTCONST32] = { 
15620                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15621         },
15622         [TEMPLATE_COPY_REG] = {
15623                 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15624                 .rhs = { [0] = { REG_UNSET, COPY_REGCM }  },
15625         },
15626         [TEMPLATE_COPY_IMM32] = {
15627                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
15628                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
15629         },
15630         [TEMPLATE_COPY_IMM16] = {
15631                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM | REGCM_GPR16 } },
15632                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 } },
15633         },
15634         [TEMPLATE_COPY_IMM8] = {
15635                 .lhs = { [0] = { REG_UNSET, COPY_REGCM } },
15636                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15637         },
15638         [TEMPLATE_PHI] = { 
15639                 .lhs = { [0] = { REG_VIRT0, COPY_REGCM } },
15640                 .rhs = { 
15641                         [ 0] = { REG_VIRT0, COPY_REGCM },
15642                         [ 1] = { REG_VIRT0, COPY_REGCM },
15643                         [ 2] = { REG_VIRT0, COPY_REGCM },
15644                         [ 3] = { REG_VIRT0, COPY_REGCM },
15645                         [ 4] = { REG_VIRT0, COPY_REGCM },
15646                         [ 5] = { REG_VIRT0, COPY_REGCM },
15647                         [ 6] = { REG_VIRT0, COPY_REGCM },
15648                         [ 7] = { REG_VIRT0, COPY_REGCM },
15649                         [ 8] = { REG_VIRT0, COPY_REGCM },
15650                         [ 9] = { REG_VIRT0, COPY_REGCM },
15651                         [10] = { REG_VIRT0, COPY_REGCM },
15652                         [11] = { REG_VIRT0, COPY_REGCM },
15653                         [12] = { REG_VIRT0, COPY_REGCM },
15654                         [13] = { REG_VIRT0, COPY_REGCM },
15655                         [14] = { REG_VIRT0, COPY_REGCM },
15656                         [15] = { REG_VIRT0, COPY_REGCM },
15657                 }, },
15658         [TEMPLATE_STORE8] = {
15659                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15660                 .rhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15661         },
15662         [TEMPLATE_STORE16] = {
15663                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15664                 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15665         },
15666         [TEMPLATE_STORE32] = {
15667                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15668                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15669         },
15670         [TEMPLATE_LOAD8] = {
15671                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15672                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15673         },
15674         [TEMPLATE_LOAD16] = {
15675                 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
15676                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15677         },
15678         [TEMPLATE_LOAD32] = {
15679                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15680                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15681         },
15682         [TEMPLATE_BINARY_REG] = {
15683                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15684                 .rhs = { 
15685                         [0] = { REG_VIRT0, REGCM_GPR32 },
15686                         [1] = { REG_UNSET, REGCM_GPR32 },
15687                 },
15688         },
15689         [TEMPLATE_BINARY_IMM] = {
15690                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15691                 .rhs = { 
15692                         [0] = { REG_VIRT0,    REGCM_GPR32 },
15693                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
15694                 },
15695         },
15696         [TEMPLATE_SL_CL] = {
15697                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15698                 .rhs = { 
15699                         [0] = { REG_VIRT0, REGCM_GPR32 },
15700                         [1] = { REG_CL, REGCM_GPR8 },
15701                 },
15702         },
15703         [TEMPLATE_SL_IMM] = {
15704                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15705                 .rhs = { 
15706                         [0] = { REG_VIRT0,    REGCM_GPR32 },
15707                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15708                 },
15709         },
15710         [TEMPLATE_UNARY] = {
15711                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15712                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
15713         },
15714         [TEMPLATE_CMP_REG] = {
15715                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15716                 .rhs = {
15717                         [0] = { REG_UNSET, REGCM_GPR32 },
15718                         [1] = { REG_UNSET, REGCM_GPR32 },
15719                 },
15720         },
15721         [TEMPLATE_CMP_IMM] = {
15722                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15723                 .rhs = {
15724                         [0] = { REG_UNSET, REGCM_GPR32 },
15725                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
15726                 },
15727         },
15728         [TEMPLATE_TEST] = {
15729                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15730                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15731         },
15732         [TEMPLATE_SET] = {
15733                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8 } },
15734                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15735         },
15736         [TEMPLATE_JMP] = {
15737                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
15738         },
15739         [TEMPLATE_INB_DX] = {
15740                 .lhs = { [0] = { REG_AL,  REGCM_GPR8 } },  
15741                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15742         },
15743         [TEMPLATE_INB_IMM] = {
15744                 .lhs = { [0] = { REG_AL,  REGCM_GPR8 } },  
15745                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15746         },
15747         [TEMPLATE_INW_DX]  = { 
15748                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
15749                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15750         },
15751         [TEMPLATE_INW_IMM] = { 
15752                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
15753                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15754         },
15755         [TEMPLATE_INL_DX]  = {
15756                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15757                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
15758         },
15759         [TEMPLATE_INL_IMM] = {
15760                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
15761                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
15762         },
15763         [TEMPLATE_OUTB_DX] = { 
15764                 .rhs = {
15765                         [0] = { REG_AL,  REGCM_GPR8 },
15766                         [1] = { REG_DX, REGCM_GPR16 },
15767                 },
15768         },
15769         [TEMPLATE_OUTB_IMM] = { 
15770                 .rhs = {
15771                         [0] = { REG_AL,  REGCM_GPR8 },  
15772                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15773                 },
15774         },
15775         [TEMPLATE_OUTW_DX] = { 
15776                 .rhs = {
15777                         [0] = { REG_AX,  REGCM_GPR16 },
15778                         [1] = { REG_DX, REGCM_GPR16 },
15779                 },
15780         },
15781         [TEMPLATE_OUTW_IMM] = {
15782                 .rhs = {
15783                         [0] = { REG_AX,  REGCM_GPR16 }, 
15784                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15785                 },
15786         },
15787         [TEMPLATE_OUTL_DX] = { 
15788                 .rhs = {
15789                         [0] = { REG_EAX, REGCM_GPR32 },
15790                         [1] = { REG_DX, REGCM_GPR16 },
15791                 },
15792         },
15793         [TEMPLATE_OUTL_IMM] = { 
15794                 .rhs = {
15795                         [0] = { REG_EAX, REGCM_GPR32 }, 
15796                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
15797                 },
15798         },
15799         [TEMPLATE_BSF] = {
15800                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15801                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
15802         },
15803         [TEMPLATE_RDMSR] = {
15804                 .lhs = { 
15805                         [0] = { REG_EAX, REGCM_GPR32 },
15806                         [1] = { REG_EDX, REGCM_GPR32 },
15807                 },
15808                 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
15809         },
15810         [TEMPLATE_WRMSR] = {
15811                 .rhs = {
15812                         [0] = { REG_ECX, REGCM_GPR32 },
15813                         [1] = { REG_EAX, REGCM_GPR32 },
15814                         [2] = { REG_EDX, REGCM_GPR32 },
15815                 },
15816         },
15817 };
15818
15819 static void fixup_branches(struct compile_state *state,
15820         struct triple *cmp, struct triple *use, int jmp_op)
15821 {
15822         struct triple_set *entry, *next;
15823         for(entry = use->use; entry; entry = next) {
15824                 next = entry->next;
15825                 if (entry->member->op == OP_COPY) {
15826                         fixup_branches(state, cmp, entry->member, jmp_op);
15827                 }
15828                 else if (entry->member->op == OP_BRANCH) {
15829                         struct triple *branch, *test;
15830                         struct triple *left, *right;
15831                         left = right = 0;
15832                         left = RHS(cmp, 0);
15833                         if (TRIPLE_RHS(cmp->sizes) > 1) {
15834                                 right = RHS(cmp, 1);
15835                         }
15836                         branch = entry->member;
15837                         test = pre_triple(state, branch,
15838                                 cmp->op, cmp->type, left, right);
15839                         test->template_id = TEMPLATE_TEST; 
15840                         if (cmp->op == OP_CMP) {
15841                                 test->template_id = TEMPLATE_CMP_REG;
15842                                 if (get_imm32(test, &RHS(test, 1))) {
15843                                         test->template_id = TEMPLATE_CMP_IMM;
15844                                 }
15845                         }
15846                         use_triple(RHS(test, 0), test);
15847                         use_triple(RHS(test, 1), test);
15848                         unuse_triple(RHS(branch, 0), branch);
15849                         RHS(branch, 0) = test;
15850                         branch->op = jmp_op;
15851                         branch->template_id = TEMPLATE_JMP;
15852                         use_triple(RHS(branch, 0), branch);
15853                 }
15854         }
15855 }
15856
15857 static void bool_cmp(struct compile_state *state, 
15858         struct triple *ins, int cmp_op, int jmp_op, int set_op)
15859 {
15860         struct triple_set *entry, *next;
15861         struct triple *set;
15862
15863         /* Put a barrier up before the cmp which preceeds the
15864          * copy instruction.  If a set actually occurs this gives
15865          * us a chance to move variables in registers out of the way.
15866          */
15867
15868         /* Modify the comparison operator */
15869         ins->op = cmp_op;
15870         ins->template_id = TEMPLATE_TEST;
15871         if (cmp_op == OP_CMP) {
15872                 ins->template_id = TEMPLATE_CMP_REG;
15873                 if (get_imm32(ins, &RHS(ins, 1))) {
15874                         ins->template_id =  TEMPLATE_CMP_IMM;
15875                 }
15876         }
15877         /* Generate the instruction sequence that will transform the
15878          * result of the comparison into a logical value.
15879          */
15880         set = post_triple(state, ins, set_op, ins->type, ins, 0);
15881         use_triple(ins, set);
15882         set->template_id = TEMPLATE_SET;
15883
15884         for(entry = ins->use; entry; entry = next) {
15885                 next = entry->next;
15886                 if (entry->member == set) {
15887                         continue;
15888                 }
15889                 replace_rhs_use(state, ins, set, entry->member);
15890         }
15891         fixup_branches(state, ins, set, jmp_op);
15892 }
15893
15894 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
15895 {
15896         struct triple *next;
15897         int lhs, i;
15898         lhs = TRIPLE_LHS(ins->sizes);
15899         for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
15900                 if (next != LHS(ins, i)) {
15901                         internal_error(state, ins, "malformed lhs on %s",
15902                                 tops(ins->op));
15903                 }
15904                 if (next->op != OP_PIECE) {
15905                         internal_error(state, ins, "bad lhs op %s at %d on %s",
15906                                 tops(next->op), i, tops(ins->op));
15907                 }
15908                 if (next->u.cval != i) {
15909                         internal_error(state, ins, "bad u.cval of %d %d expected",
15910                                 next->u.cval, i);
15911                 }
15912         }
15913         return next;
15914 }
15915
15916 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
15917 {
15918         struct ins_template *template;
15919         struct reg_info result;
15920         int zlhs;
15921         if (ins->op == OP_PIECE) {
15922                 index = ins->u.cval;
15923                 ins = MISC(ins, 0);
15924         }
15925         zlhs = TRIPLE_LHS(ins->sizes);
15926         if (triple_is_def(state, ins)) {
15927                 zlhs = 1;
15928         }
15929         if (index >= zlhs) {
15930                 internal_error(state, ins, "index %d out of range for %s\n",
15931                         index, tops(ins->op));
15932         }
15933         switch(ins->op) {
15934         case OP_ASM:
15935                 template = &ins->u.ainfo->tmpl;
15936                 break;
15937         default:
15938                 if (ins->template_id > LAST_TEMPLATE) {
15939                         internal_error(state, ins, "bad template number %d", 
15940                                 ins->template_id);
15941                 }
15942                 template = &templates[ins->template_id];
15943                 break;
15944         }
15945         result = template->lhs[index];
15946         result.regcm = arch_regcm_normalize(state, result.regcm);
15947         if (result.reg != REG_UNNEEDED) {
15948                 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15949         }
15950         if (result.regcm == 0) {
15951                 internal_error(state, ins, "lhs %d regcm == 0", index);
15952         }
15953         return result;
15954 }
15955
15956 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
15957 {
15958         struct reg_info result;
15959         struct ins_template *template;
15960         if ((index > TRIPLE_RHS(ins->sizes)) ||
15961                 (ins->op == OP_PIECE)) {
15962                 internal_error(state, ins, "index %d out of range for %s\n",
15963                         index, tops(ins->op));
15964         }
15965         switch(ins->op) {
15966         case OP_ASM:
15967                 template = &ins->u.ainfo->tmpl;
15968                 break;
15969         default:
15970                 if (ins->template_id > LAST_TEMPLATE) {
15971                         internal_error(state, ins, "bad template number %d", 
15972                                 ins->template_id);
15973                 }
15974                 template = &templates[ins->template_id];
15975                 break;
15976         }
15977         result = template->rhs[index];
15978         result.regcm = arch_regcm_normalize(state, result.regcm);
15979         if (result.regcm == 0) {
15980                 internal_error(state, ins, "rhs %d regcm == 0", index);
15981         }
15982         return result;
15983 }
15984
15985 static struct triple *transform_to_arch_instruction(
15986         struct compile_state *state, struct triple *ins)
15987 {
15988         /* Transform from generic 3 address instructions
15989          * to archtecture specific instructions.
15990          * And apply architecture specific constrains to instructions.
15991          * Copies are inserted to preserve the register flexibility
15992          * of 3 address instructions.
15993          */
15994         struct triple *next;
15995         next = ins->next;
15996         switch(ins->op) {
15997         case OP_INTCONST:
15998                 ins->template_id = TEMPLATE_INTCONST32;
15999                 if (ins->u.cval < 256) {
16000                         ins->template_id = TEMPLATE_INTCONST8;
16001                 }
16002                 break;
16003         case OP_ADDRCONST:
16004                 ins->template_id = TEMPLATE_INTCONST32;
16005                 break;
16006         case OP_NOOP:
16007         case OP_SDECL:
16008         case OP_BLOBCONST:
16009         case OP_LABEL:
16010                 ins->template_id = TEMPLATE_NOP;
16011                 break;
16012         case OP_COPY:
16013                 ins->template_id = TEMPLATE_COPY_REG;
16014                 if (is_imm8(RHS(ins, 0))) {
16015                         ins->template_id = TEMPLATE_COPY_IMM8;
16016                 }
16017                 else if (is_imm16(RHS(ins, 0))) {
16018                         ins->template_id = TEMPLATE_COPY_IMM16;
16019                 }
16020                 else if (is_imm32(RHS(ins, 0))) {
16021                         ins->template_id = TEMPLATE_COPY_IMM32;
16022                 }
16023                 else if (is_const(RHS(ins, 0))) {
16024                         internal_error(state, ins, "bad constant passed to copy");
16025                 }
16026                 break;
16027         case OP_PHI:
16028                 ins->template_id = TEMPLATE_PHI;
16029                 break;
16030         case OP_STORE:
16031                 switch(ins->type->type & TYPE_MASK) {
16032                 case TYPE_CHAR:    case TYPE_UCHAR:
16033                         ins->template_id = TEMPLATE_STORE8;
16034                         break;
16035                 case TYPE_SHORT:   case TYPE_USHORT:
16036                         ins->template_id = TEMPLATE_STORE16;
16037                         break;
16038                 case TYPE_INT:     case TYPE_UINT:
16039                 case TYPE_LONG:    case TYPE_ULONG:
16040                 case TYPE_POINTER:
16041                         ins->template_id = TEMPLATE_STORE32;
16042                         break;
16043                 default:
16044                         internal_error(state, ins, "unknown type in store");
16045                         break;
16046                 }
16047                 break;
16048         case OP_LOAD:
16049                 switch(ins->type->type & TYPE_MASK) {
16050                 case TYPE_CHAR:   case TYPE_UCHAR:
16051                         ins->template_id = TEMPLATE_LOAD8;
16052                         break;
16053                 case TYPE_SHORT:
16054                 case TYPE_USHORT:
16055                         ins->template_id = TEMPLATE_LOAD16;
16056                         break;
16057                 case TYPE_INT:
16058                 case TYPE_UINT:
16059                 case TYPE_LONG:
16060                 case TYPE_ULONG:
16061                 case TYPE_POINTER:
16062                         ins->template_id = TEMPLATE_LOAD32;
16063                         break;
16064                 default:
16065                         internal_error(state, ins, "unknown type in load");
16066                         break;
16067                 }
16068                 break;
16069         case OP_ADD:
16070         case OP_SUB:
16071         case OP_AND:
16072         case OP_XOR:
16073         case OP_OR:
16074         case OP_SMUL:
16075                 ins->template_id = TEMPLATE_BINARY_REG;
16076                 if (get_imm32(ins, &RHS(ins, 1))) {
16077                         ins->template_id = TEMPLATE_BINARY_IMM;
16078                 }
16079                 break;
16080         case OP_SL:
16081         case OP_SSR:
16082         case OP_USR:
16083                 ins->template_id = TEMPLATE_SL_CL;
16084                 if (get_imm8(ins, &RHS(ins, 1))) {
16085                         ins->template_id = TEMPLATE_SL_IMM;
16086                 }
16087                 break;
16088         case OP_INVERT:
16089         case OP_NEG:
16090                 ins->template_id = TEMPLATE_UNARY;
16091                 break;
16092         case OP_EQ: 
16093                 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
16094                 break;
16095         case OP_NOTEQ:
16096                 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16097                 break;
16098         case OP_SLESS:
16099                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16100                 break;
16101         case OP_ULESS:
16102                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16103                 break;
16104         case OP_SMORE:
16105                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16106                 break;
16107         case OP_UMORE:
16108                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16109                 break;
16110         case OP_SLESSEQ:
16111                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16112                 break;
16113         case OP_ULESSEQ:
16114                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16115                 break;
16116         case OP_SMOREEQ:
16117                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16118                 break;
16119         case OP_UMOREEQ:
16120                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16121                 break;
16122         case OP_LTRUE:
16123                 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16124                 break;
16125         case OP_LFALSE:
16126                 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16127                 break;
16128         case OP_BRANCH:
16129                 if (TRIPLE_RHS(ins->sizes) > 0) {
16130                         internal_error(state, ins, "bad branch test");
16131                 }
16132                 ins->op = OP_JMP;
16133                 ins->template_id = TEMPLATE_NOP;
16134                 break;
16135         case OP_INB:
16136         case OP_INW:
16137         case OP_INL:
16138                 switch(ins->op) {
16139                 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16140                 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16141                 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16142                 }
16143                 if (get_imm8(ins, &RHS(ins, 0))) {
16144                         ins->template_id += 1;
16145                 }
16146                 break;
16147         case OP_OUTB:
16148         case OP_OUTW:
16149         case OP_OUTL:
16150                 switch(ins->op) {
16151                 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16152                 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16153                 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16154                 }
16155                 if (get_imm8(ins, &RHS(ins, 1))) {
16156                         ins->template_id += 1;
16157                 }
16158                 break;
16159         case OP_BSF:
16160         case OP_BSR:
16161                 ins->template_id = TEMPLATE_BSF;
16162                 break;
16163         case OP_RDMSR:
16164                 ins->template_id = TEMPLATE_RDMSR;
16165                 next = after_lhs(state, ins);
16166                 break;
16167         case OP_WRMSR:
16168                 ins->template_id = TEMPLATE_WRMSR;
16169                 break;
16170         case OP_HLT:
16171                 ins->template_id = TEMPLATE_NOP;
16172                 break;
16173         case OP_ASM:
16174                 ins->template_id = TEMPLATE_NOP;
16175                 next = after_lhs(state, ins);
16176                 break;
16177                 /* Already transformed instructions */
16178         case OP_TEST:
16179                 ins->template_id = TEMPLATE_TEST;
16180                 break;
16181         case OP_CMP:
16182                 ins->template_id = TEMPLATE_CMP_REG;
16183                 if (get_imm32(ins, &RHS(ins, 1))) {
16184                         ins->template_id = TEMPLATE_CMP_IMM;
16185                 }
16186                 break;
16187         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
16188         case OP_JMP_SLESS:   case OP_JMP_ULESS:
16189         case OP_JMP_SMORE:   case OP_JMP_UMORE:
16190         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16191         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16192                 ins->template_id = TEMPLATE_JMP;
16193                 break;
16194         case OP_SET_EQ:      case OP_SET_NOTEQ:
16195         case OP_SET_SLESS:   case OP_SET_ULESS:
16196         case OP_SET_SMORE:   case OP_SET_UMORE:
16197         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16198         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16199                 ins->template_id = TEMPLATE_SET;
16200                 break;
16201                 /* Unhandled instructions */
16202         case OP_PIECE:
16203         default:
16204                 internal_error(state, ins, "unhandled ins: %d %s\n",
16205                         ins->op, tops(ins->op));
16206                 break;
16207         }
16208         return next;
16209 }
16210
16211 static void generate_local_labels(struct compile_state *state)
16212 {
16213         struct triple *first, *label;
16214         int label_counter;
16215         label_counter = 0;
16216         first = RHS(state->main_function, 0);
16217         label = first;
16218         do {
16219                 if ((label->op == OP_LABEL) || 
16220                         (label->op == OP_SDECL)) {
16221                         if (label->use) {
16222                                 label->u.cval = ++label_counter;
16223                         } else {
16224                                 label->u.cval = 0;
16225                         }
16226                         
16227                 }
16228                 label = label->next;
16229         } while(label != first);
16230 }
16231
16232 static int check_reg(struct compile_state *state, 
16233         struct triple *triple, int classes)
16234 {
16235         unsigned mask;
16236         int reg;
16237         reg = ID_REG(triple->id);
16238         if (reg == REG_UNSET) {
16239                 internal_error(state, triple, "register not set");
16240         }
16241         mask = arch_reg_regcm(state, reg);
16242         if (!(classes & mask)) {
16243                 internal_error(state, triple, "reg %d in wrong class",
16244                         reg);
16245         }
16246         return reg;
16247 }
16248
16249 static const char *arch_reg_str(int reg)
16250 {
16251         static const char *regs[] = {
16252                 "%unset",
16253                 "%unneeded",
16254                 "%eflags",
16255                 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
16256                 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
16257                 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
16258                 "%edx:%eax",
16259                 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
16260                 "%xmm0", "%xmm1", "%xmm2", "%xmm3", 
16261                 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
16262         };
16263         if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
16264                 reg = 0;
16265         }
16266         return regs[reg];
16267 }
16268
16269
16270 static const char *reg(struct compile_state *state, struct triple *triple,
16271         int classes)
16272 {
16273         int reg;
16274         reg = check_reg(state, triple, classes);
16275         return arch_reg_str(reg);
16276 }
16277
16278 const char *type_suffix(struct compile_state *state, struct type *type)
16279 {
16280         const char *suffix;
16281         switch(size_of(state, type)) {
16282         case 1: suffix = "b"; break;
16283         case 2: suffix = "w"; break;
16284         case 4: suffix = "l"; break;
16285         default:
16286                 internal_error(state, 0, "unknown suffix");
16287                 suffix = 0;
16288                 break;
16289         }
16290         return suffix;
16291 }
16292
16293 static void print_const_val(
16294         struct compile_state *state, struct triple *ins, FILE *fp)
16295 {
16296         switch(ins->op) {
16297         case OP_INTCONST:
16298                 fprintf(fp, " $%ld ", 
16299                         (long_t)(ins->u.cval));
16300                 break;
16301         case OP_ADDRCONST:
16302                 fprintf(fp, " $L%s%lu+%lu ",
16303                         state->label_prefix, 
16304                         MISC(ins, 0)->u.cval,
16305                         ins->u.cval);
16306                 break;
16307         default:
16308                 internal_error(state, ins, "unknown constant type");
16309                 break;
16310         }
16311 }
16312
16313 static void print_binary_op(struct compile_state *state,
16314         const char *op, struct triple *ins, FILE *fp) 
16315 {
16316         unsigned mask;
16317         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16318         if (RHS(ins, 0)->id != ins->id) {
16319                 internal_error(state, ins, "invalid register assignment");
16320         }
16321         if (is_const(RHS(ins, 1))) {
16322                 fprintf(fp, "\t%s ", op);
16323                 print_const_val(state, RHS(ins, 1), fp);
16324                 fprintf(fp, ", %s\n",
16325                         reg(state, RHS(ins, 0), mask));
16326         }
16327         else {
16328                 unsigned lmask, rmask;
16329                 int lreg, rreg;
16330                 lreg = check_reg(state, RHS(ins, 0), mask);
16331                 rreg = check_reg(state, RHS(ins, 1), mask);
16332                 lmask = arch_reg_regcm(state, lreg);
16333                 rmask = arch_reg_regcm(state, rreg);
16334                 mask = lmask & rmask;
16335                 fprintf(fp, "\t%s %s, %s\n",
16336                         op,
16337                         reg(state, RHS(ins, 1), mask),
16338                         reg(state, RHS(ins, 0), mask));
16339         }
16340 }
16341 static void print_unary_op(struct compile_state *state, 
16342         const char *op, struct triple *ins, FILE *fp)
16343 {
16344         unsigned mask;
16345         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16346         fprintf(fp, "\t%s %s\n",
16347                 op,
16348                 reg(state, RHS(ins, 0), mask));
16349 }
16350
16351 static void print_op_shift(struct compile_state *state,
16352         const char *op, struct triple *ins, FILE *fp)
16353 {
16354         unsigned mask;
16355         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16356         if (RHS(ins, 0)->id != ins->id) {
16357                 internal_error(state, ins, "invalid register assignment");
16358         }
16359         if (is_const(RHS(ins, 1))) {
16360                 fprintf(fp, "\t%s ", op);
16361                 print_const_val(state, RHS(ins, 1), fp);
16362                 fprintf(fp, ", %s\n",
16363                         reg(state, RHS(ins, 0), mask));
16364         }
16365         else {
16366                 fprintf(fp, "\t%s %s, %s\n",
16367                         op,
16368                         reg(state, RHS(ins, 1), REGCM_GPR8),
16369                         reg(state, RHS(ins, 0), mask));
16370         }
16371 }
16372
16373 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
16374 {
16375         const char *op;
16376         int mask;
16377         int dreg;
16378         mask = 0;
16379         switch(ins->op) {
16380         case OP_INB: op = "inb", mask = REGCM_GPR8; break;
16381         case OP_INW: op = "inw", mask = REGCM_GPR16; break;
16382         case OP_INL: op = "inl", mask = REGCM_GPR32; break;
16383         default:
16384                 internal_error(state, ins, "not an in operation");
16385                 op = 0;
16386                 break;
16387         }
16388         dreg = check_reg(state, ins, mask);
16389         if (!reg_is_reg(state, dreg, REG_EAX)) {
16390                 internal_error(state, ins, "dst != %%eax");
16391         }
16392         if (is_const(RHS(ins, 0))) {
16393                 fprintf(fp, "\t%s ", op);
16394                 print_const_val(state, RHS(ins, 0), fp);
16395                 fprintf(fp, ", %s\n",
16396                         reg(state, ins, mask));
16397         }
16398         else {
16399                 int addr_reg;
16400                 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
16401                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16402                         internal_error(state, ins, "src != %%dx");
16403                 }
16404                 fprintf(fp, "\t%s %s, %s\n",
16405                         op, 
16406                         reg(state, RHS(ins, 0), REGCM_GPR16),
16407                         reg(state, ins, mask));
16408         }
16409 }
16410
16411 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
16412 {
16413         const char *op;
16414         int mask;
16415         int lreg;
16416         mask = 0;
16417         switch(ins->op) {
16418         case OP_OUTB: op = "outb", mask = REGCM_GPR8; break;
16419         case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
16420         case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
16421         default:
16422                 internal_error(state, ins, "not an out operation");
16423                 op = 0;
16424                 break;
16425         }
16426         lreg = check_reg(state, RHS(ins, 0), mask);
16427         if (!reg_is_reg(state, lreg, REG_EAX)) {
16428                 internal_error(state, ins, "src != %%eax");
16429         }
16430         if (is_const(RHS(ins, 1))) {
16431                 fprintf(fp, "\t%s %s,", 
16432                         op, reg(state, RHS(ins, 0), mask));
16433                 print_const_val(state, RHS(ins, 1), fp);
16434                 fprintf(fp, "\n");
16435         }
16436         else {
16437                 int addr_reg;
16438                 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
16439                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
16440                         internal_error(state, ins, "dst != %%dx");
16441                 }
16442                 fprintf(fp, "\t%s %s, %s\n",
16443                         op, 
16444                         reg(state, RHS(ins, 0), mask),
16445                         reg(state, RHS(ins, 1), REGCM_GPR16));
16446         }
16447 }
16448
16449 static void print_op_move(struct compile_state *state,
16450         struct triple *ins, FILE *fp)
16451 {
16452         /* op_move is complex because there are many types
16453          * of registers we can move between.
16454          * Because OP_COPY will be introduced in arbitrary locations
16455          * OP_COPY must not affect flags.
16456          */
16457         int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
16458         struct triple *dst, *src;
16459         if (ins->op == OP_COPY) {
16460                 src = RHS(ins, 0);
16461                 dst = ins;
16462         }
16463         else if (ins->op == OP_WRITE) {
16464                 dst = LHS(ins, 0);
16465                 src = RHS(ins, 0);
16466         }
16467         else {
16468                 internal_error(state, ins, "unknown move operation");
16469                 src = dst = 0;
16470         }
16471         if (!is_const(src)) {
16472                 int src_reg, dst_reg;
16473                 int src_regcm, dst_regcm;
16474                 src_reg = ID_REG(src->id);
16475                 dst_reg   = ID_REG(dst->id);
16476                 src_regcm = arch_reg_regcm(state, src_reg);
16477                 dst_regcm   = arch_reg_regcm(state, dst_reg);
16478                 /* If the class is the same just move the register */
16479                 if (src_regcm & dst_regcm & 
16480                         (REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32)) {
16481                         if ((src_reg != dst_reg) || !omit_copy) {
16482                                 fprintf(fp, "\tmov %s, %s\n",
16483                                         reg(state, src, src_regcm),
16484                                         reg(state, dst, dst_regcm));
16485                         }
16486                 }
16487                 /* Move 32bit to 16bit */
16488                 else if ((src_regcm & REGCM_GPR32) &&
16489                         (dst_regcm & REGCM_GPR16)) {
16490                         src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
16491                         if ((src_reg != dst_reg) || !omit_copy) {
16492                                 fprintf(fp, "\tmovw %s, %s\n",
16493                                         arch_reg_str(src_reg), 
16494                                         arch_reg_str(dst_reg));
16495                         }
16496                 }
16497                 /* Move 32bit to 8bit */
16498                 else if ((src_regcm & REGCM_GPR32_8) &&
16499                         (dst_regcm & REGCM_GPR8))
16500                 {
16501                         src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
16502                         if ((src_reg != dst_reg) || !omit_copy) {
16503                                 fprintf(fp, "\tmovb %s, %s\n",
16504                                         arch_reg_str(src_reg),
16505                                         arch_reg_str(dst_reg));
16506                         }
16507                 }
16508                 /* Move 16bit to 8bit */
16509                 else if ((src_regcm & REGCM_GPR16_8) &&
16510                         (dst_regcm & REGCM_GPR8))
16511                 {
16512                         src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
16513                         if ((src_reg != dst_reg) || !omit_copy) {
16514                                 fprintf(fp, "\tmovb %s, %s\n",
16515                                         arch_reg_str(src_reg),
16516                                         arch_reg_str(dst_reg));
16517                         }
16518                 }
16519                 /* Move 8/16bit to 16/32bit */
16520                 else if ((src_regcm & (REGCM_GPR8 | REGCM_GPR16)) && 
16521                         (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
16522                         const char *op;
16523                         op = is_signed(src->type)? "movsx": "movzx";
16524                         fprintf(fp, "\t%s %s, %s\n",
16525                                 op,
16526                                 reg(state, src, src_regcm),
16527                                 reg(state, dst, dst_regcm));
16528                 }
16529                 /* Move between sse registers */
16530                 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
16531                         if ((src_reg != dst_reg) || !omit_copy) {
16532                                 fprintf(fp, "\tmovdqa %s, %s\n",
16533                                         reg(state, src, src_regcm),
16534                                         reg(state, dst, dst_regcm));
16535                         }
16536                 }
16537                 /* Move between mmx registers or mmx & sse  registers */
16538                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16539                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16540                         if ((src_reg != dst_reg) || !omit_copy) {
16541                                 fprintf(fp, "\tmovq %s, %s\n",
16542                                         reg(state, src, src_regcm),
16543                                         reg(state, dst, dst_regcm));
16544                         }
16545                 }
16546                 /* Move between 32bit gprs & mmx/sse registers */
16547                 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
16548                         (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
16549                         fprintf(fp, "\tmovd %s, %s\n",
16550                                 reg(state, src, src_regcm),
16551                                 reg(state, dst, dst_regcm));
16552                 }
16553 #if X86_4_8BIT_GPRS
16554                 /* Move from 8bit gprs to  mmx/sse registers */
16555                 else if ((src_regcm & REGCM_GPR8) && (src_reg <= REG_DL) &&
16556                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
16557                         const char *op;
16558                         int mid_reg;
16559                         op = is_signed(src->type)? "movsx":"movzx";
16560                         mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16561                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
16562                                 op,
16563                                 reg(state, src, src_regcm),
16564                                 arch_reg_str(mid_reg),
16565                                 arch_reg_str(mid_reg),
16566                                 reg(state, dst, dst_regcm));
16567                 }
16568                 /* Move from mmx/sse registers and 8bit gprs */
16569                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
16570                         (dst_regcm & REGCM_GPR8) && (dst_reg <= REG_DL)) {
16571                         int mid_reg;
16572                         mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16573                         fprintf(fp, "\tmovd %s, %s\n",
16574                                 reg(state, src, src_regcm),
16575                                 arch_reg_str(mid_reg));
16576                 }
16577                 /* Move from 32bit gprs to 16bit gprs */
16578                 else if ((src_regcm & REGCM_GPR32) &&
16579                         (dst_regcm & REGCM_GPR16)) {
16580                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
16581                         if ((src_reg != dst_reg) || !omit_copy) {
16582                                 fprintf(fp, "\tmov %s, %s\n",
16583                                         arch_reg_str(src_reg),
16584                                         arch_reg_str(dst_reg));
16585                         }
16586                 }
16587                 /* Move from 32bit gprs to 8bit gprs */
16588                 else if ((src_regcm & REGCM_GPR32) &&
16589                         (dst_regcm & REGCM_GPR8)) {
16590                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
16591                         if ((src_reg != dst_reg) || !omit_copy) {
16592                                 fprintf(fp, "\tmov %s, %s\n",
16593                                         arch_reg_str(src_reg),
16594                                         arch_reg_str(dst_reg));
16595                         }
16596                 }
16597                 /* Move from 16bit gprs to 8bit gprs */
16598                 else if ((src_regcm & REGCM_GPR16) &&
16599                         (dst_regcm & REGCM_GPR8)) {
16600                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
16601                         if ((src_reg != dst_reg) || !omit_copy) {
16602                                 fprintf(fp, "\tmov %s, %s\n",
16603                                         arch_reg_str(src_reg),
16604                                         arch_reg_str(dst_reg));
16605                         }
16606                 }
16607 #endif /* X86_4_8BIT_GPRS */
16608                 else {
16609                         internal_error(state, ins, "unknown copy type");
16610                 }
16611         }
16612         else {
16613                 fprintf(fp, "\tmov ");
16614                 print_const_val(state, src, fp);
16615                 fprintf(fp, ", %s\n",
16616                         reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8));
16617         }
16618 }
16619
16620 static void print_op_load(struct compile_state *state,
16621         struct triple *ins, FILE *fp)
16622 {
16623         struct triple *dst, *src;
16624         dst = ins;
16625         src = RHS(ins, 0);
16626         if (is_const(src) || is_const(dst)) {
16627                 internal_error(state, ins, "unknown load operation");
16628         }
16629         fprintf(fp, "\tmov (%s), %s\n",
16630                 reg(state, src, REGCM_GPR32),
16631                 reg(state, dst, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32));
16632 }
16633
16634
16635 static void print_op_store(struct compile_state *state,
16636         struct triple *ins, FILE *fp)
16637 {
16638         struct triple *dst, *src;
16639         dst = LHS(ins, 0);
16640         src = RHS(ins, 0);
16641         if (is_const(src) && (src->op == OP_INTCONST)) {
16642                 long_t value;
16643                 value = (long_t)(src->u.cval);
16644                 fprintf(fp, "\tmov%s $%ld, (%s)\n",
16645                         type_suffix(state, src->type),
16646                         value,
16647                         reg(state, dst, REGCM_GPR32));
16648         }
16649         else if (is_const(dst) && (dst->op == OP_INTCONST)) {
16650                 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
16651                         type_suffix(state, src->type),
16652                         reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16653                         dst->u.cval);
16654         }
16655         else {
16656                 if (is_const(src) || is_const(dst)) {
16657                         internal_error(state, ins, "unknown store operation");
16658                 }
16659                 fprintf(fp, "\tmov%s %s, (%s)\n",
16660                         type_suffix(state, src->type),
16661                         reg(state, src, REGCM_GPR8 | REGCM_GPR16 | REGCM_GPR32),
16662                         reg(state, dst, REGCM_GPR32));
16663         }
16664         
16665         
16666 }
16667
16668 static void print_op_smul(struct compile_state *state,
16669         struct triple *ins, FILE *fp)
16670 {
16671         if (!is_const(RHS(ins, 1))) {
16672                 fprintf(fp, "\timul %s, %s\n",
16673                         reg(state, RHS(ins, 1), REGCM_GPR32),
16674                         reg(state, RHS(ins, 0), REGCM_GPR32));
16675         }
16676         else {
16677                 fprintf(fp, "\timul ");
16678                 print_const_val(state, RHS(ins, 1), fp);
16679                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
16680         }
16681 }
16682
16683 static void print_op_cmp(struct compile_state *state,
16684         struct triple *ins, FILE *fp)
16685 {
16686         unsigned mask;
16687         int dreg;
16688         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16689         dreg = check_reg(state, ins, REGCM_FLAGS);
16690         if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
16691                 internal_error(state, ins, "bad dest register for cmp");
16692         }
16693         if (is_const(RHS(ins, 1))) {
16694                 fprintf(fp, "\tcmp ");
16695                 print_const_val(state, RHS(ins, 1), fp);
16696                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
16697         }
16698         else {
16699                 unsigned lmask, rmask;
16700                 int lreg, rreg;
16701                 lreg = check_reg(state, RHS(ins, 0), mask);
16702                 rreg = check_reg(state, RHS(ins, 1), mask);
16703                 lmask = arch_reg_regcm(state, lreg);
16704                 rmask = arch_reg_regcm(state, rreg);
16705                 mask = lmask & rmask;
16706                 fprintf(fp, "\tcmp %s, %s\n",
16707                         reg(state, RHS(ins, 1), mask),
16708                         reg(state, RHS(ins, 0), mask));
16709         }
16710 }
16711
16712 static void print_op_test(struct compile_state *state,
16713         struct triple *ins, FILE *fp)
16714 {
16715         unsigned mask;
16716         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8;
16717         fprintf(fp, "\ttest %s, %s\n",
16718                 reg(state, RHS(ins, 0), mask),
16719                 reg(state, RHS(ins, 0), mask));
16720 }
16721
16722 static void print_op_branch(struct compile_state *state,
16723         struct triple *branch, FILE *fp)
16724 {
16725         const char *bop = "j";
16726         if (branch->op == OP_JMP) {
16727                 if (TRIPLE_RHS(branch->sizes) != 0) {
16728                         internal_error(state, branch, "jmp with condition?");
16729                 }
16730                 bop = "jmp";
16731         }
16732         else {
16733                 struct triple *ptr;
16734                 if (TRIPLE_RHS(branch->sizes) != 1) {
16735                         internal_error(state, branch, "jmpcc without condition?");
16736                 }
16737                 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
16738                 if ((RHS(branch, 0)->op != OP_CMP) &&
16739                         (RHS(branch, 0)->op != OP_TEST)) {
16740                         internal_error(state, branch, "bad branch test");
16741                 }
16742 #warning "FIXME I have observed instructions between the test and branch instructions"
16743                 ptr = RHS(branch, 0);
16744                 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
16745                         if (ptr->op != OP_COPY) {
16746                                 internal_error(state, branch, "branch does not follow test");
16747                         }
16748                 }
16749                 switch(branch->op) {
16750                 case OP_JMP_EQ:       bop = "jz";  break;
16751                 case OP_JMP_NOTEQ:    bop = "jnz"; break;
16752                 case OP_JMP_SLESS:    bop = "jl";  break;
16753                 case OP_JMP_ULESS:    bop = "jb";  break;
16754                 case OP_JMP_SMORE:    bop = "jg";  break;
16755                 case OP_JMP_UMORE:    bop = "ja";  break;
16756                 case OP_JMP_SLESSEQ:  bop = "jle"; break;
16757                 case OP_JMP_ULESSEQ:  bop = "jbe"; break;
16758                 case OP_JMP_SMOREEQ:  bop = "jge"; break;
16759                 case OP_JMP_UMOREEQ:  bop = "jae"; break;
16760                 default:
16761                         internal_error(state, branch, "Invalid branch op");
16762                         break;
16763                 }
16764                 
16765         }
16766         fprintf(fp, "\t%s L%s%lu\n",
16767                 bop, 
16768                 state->label_prefix,
16769                 TARG(branch, 0)->u.cval);
16770 }
16771
16772 static void print_op_set(struct compile_state *state,
16773         struct triple *set, FILE *fp)
16774 {
16775         const char *sop = "set";
16776         if (TRIPLE_RHS(set->sizes) != 1) {
16777                 internal_error(state, set, "setcc without condition?");
16778         }
16779         check_reg(state, RHS(set, 0), REGCM_FLAGS);
16780         if ((RHS(set, 0)->op != OP_CMP) &&
16781                 (RHS(set, 0)->op != OP_TEST)) {
16782                 internal_error(state, set, "bad set test");
16783         }
16784         if (RHS(set, 0)->next != set) {
16785                 internal_error(state, set, "set does not follow test");
16786         }
16787         switch(set->op) {
16788         case OP_SET_EQ:       sop = "setz";  break;
16789         case OP_SET_NOTEQ:    sop = "setnz"; break;
16790         case OP_SET_SLESS:    sop = "setl";  break;
16791         case OP_SET_ULESS:    sop = "setb";  break;
16792         case OP_SET_SMORE:    sop = "setg";  break;
16793         case OP_SET_UMORE:    sop = "seta";  break;
16794         case OP_SET_SLESSEQ:  sop = "setle"; break;
16795         case OP_SET_ULESSEQ:  sop = "setbe"; break;
16796         case OP_SET_SMOREEQ:  sop = "setge"; break;
16797         case OP_SET_UMOREEQ:  sop = "setae"; break;
16798         default:
16799                 internal_error(state, set, "Invalid set op");
16800                 break;
16801         }
16802         fprintf(fp, "\t%s %s\n",
16803                 sop, reg(state, set, REGCM_GPR8));
16804 }
16805
16806 static void print_op_bit_scan(struct compile_state *state, 
16807         struct triple *ins, FILE *fp) 
16808 {
16809         const char *op;
16810         switch(ins->op) {
16811         case OP_BSF: op = "bsf"; break;
16812         case OP_BSR: op = "bsr"; break;
16813         default: 
16814                 internal_error(state, ins, "unknown bit scan");
16815                 op = 0;
16816                 break;
16817         }
16818         fprintf(fp, 
16819                 "\t%s %s, %s\n"
16820                 "\tjnz 1f\n"
16821                 "\tmovl $-1, %s\n"
16822                 "1:\n",
16823                 op,
16824                 reg(state, RHS(ins, 0), REGCM_GPR32),
16825                 reg(state, ins, REGCM_GPR32),
16826                 reg(state, ins, REGCM_GPR32));
16827 }
16828
16829 static void print_const(struct compile_state *state,
16830         struct triple *ins, FILE *fp)
16831 {
16832         switch(ins->op) {
16833         case OP_INTCONST:
16834                 switch(ins->type->type & TYPE_MASK) {
16835                 case TYPE_CHAR:
16836                 case TYPE_UCHAR:
16837                         fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
16838                         break;
16839                 case TYPE_SHORT:
16840                 case TYPE_USHORT:
16841                         fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
16842                         break;
16843                 case TYPE_INT:
16844                 case TYPE_UINT:
16845                 case TYPE_LONG:
16846                 case TYPE_ULONG:
16847                         fprintf(fp, ".int %lu\n", ins->u.cval);
16848                         break;
16849                 default:
16850                         internal_error(state, ins, "Unknown constant type");
16851                 }
16852                 break;
16853         case OP_BLOBCONST:
16854         {
16855                 unsigned char *blob;
16856                 size_t size, i;
16857                 size = size_of(state, ins->type);
16858                 blob = ins->u.blob;
16859                 for(i = 0; i < size; i++) {
16860                         fprintf(fp, ".byte 0x%02x\n",
16861                                 blob[i]);
16862                 }
16863                 break;
16864         }
16865         default:
16866                 internal_error(state, ins, "Unknown constant type");
16867                 break;
16868         }
16869 }
16870
16871 #define TEXT_SECTION ".rom.text"
16872 #define DATA_SECTION ".rom.data"
16873
16874 static void print_sdecl(struct compile_state *state,
16875         struct triple *ins, FILE *fp)
16876 {
16877         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
16878         fprintf(fp, ".balign %d\n", align_of(state, ins->type));
16879         fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16880         print_const(state, MISC(ins, 0), fp);
16881         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
16882                 
16883 }
16884
16885 static void print_instruction(struct compile_state *state,
16886         struct triple *ins, FILE *fp)
16887 {
16888         /* Assumption: after I have exted the register allocator
16889          * everything is in a valid register. 
16890          */
16891         switch(ins->op) {
16892         case OP_ASM:
16893                 print_op_asm(state, ins, fp);
16894                 break;
16895         case OP_ADD:    print_binary_op(state, "add", ins, fp); break;
16896         case OP_SUB:    print_binary_op(state, "sub", ins, fp); break;
16897         case OP_AND:    print_binary_op(state, "and", ins, fp); break;
16898         case OP_XOR:    print_binary_op(state, "xor", ins, fp); break;
16899         case OP_OR:     print_binary_op(state, "or",  ins, fp); break;
16900         case OP_SL:     print_op_shift(state, "shl", ins, fp); break;
16901         case OP_USR:    print_op_shift(state, "shr", ins, fp); break;
16902         case OP_SSR:    print_op_shift(state, "sar", ins, fp); break;
16903         case OP_POS:    break;
16904         case OP_NEG:    print_unary_op(state, "neg", ins, fp); break;
16905         case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
16906         case OP_INTCONST:
16907         case OP_ADDRCONST:
16908         case OP_BLOBCONST:
16909                 /* Don't generate anything here for constants */
16910         case OP_PHI:
16911                 /* Don't generate anything for variable declarations. */
16912                 break;
16913         case OP_SDECL:
16914                 print_sdecl(state, ins, fp);
16915                 break;
16916         case OP_WRITE: 
16917         case OP_COPY:   
16918                 print_op_move(state, ins, fp);
16919                 break;
16920         case OP_LOAD:
16921                 print_op_load(state, ins, fp);
16922                 break;
16923         case OP_STORE:
16924                 print_op_store(state, ins, fp);
16925                 break;
16926         case OP_SMUL:
16927                 print_op_smul(state, ins, fp);
16928                 break;
16929         case OP_CMP:    print_op_cmp(state, ins, fp); break;
16930         case OP_TEST:   print_op_test(state, ins, fp); break;
16931         case OP_JMP:
16932         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
16933         case OP_JMP_SLESS:   case OP_JMP_ULESS:
16934         case OP_JMP_SMORE:   case OP_JMP_UMORE:
16935         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16936         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16937                 print_op_branch(state, ins, fp);
16938                 break;
16939         case OP_SET_EQ:      case OP_SET_NOTEQ:
16940         case OP_SET_SLESS:   case OP_SET_ULESS:
16941         case OP_SET_SMORE:   case OP_SET_UMORE:
16942         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16943         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16944                 print_op_set(state, ins, fp);
16945                 break;
16946         case OP_INB:  case OP_INW:  case OP_INL:
16947                 print_op_in(state, ins, fp); 
16948                 break;
16949         case OP_OUTB: case OP_OUTW: case OP_OUTL:
16950                 print_op_out(state, ins, fp); 
16951                 break;
16952         case OP_BSF:
16953         case OP_BSR:
16954                 print_op_bit_scan(state, ins, fp);
16955                 break;
16956         case OP_RDMSR:
16957                 after_lhs(state, ins);
16958                 fprintf(fp, "\trdmsr\n");
16959                 break;
16960         case OP_WRMSR:
16961                 fprintf(fp, "\twrmsr\n");
16962                 break;
16963         case OP_HLT:
16964                 fprintf(fp, "\thlt\n");
16965                 break;
16966         case OP_LABEL:
16967                 if (!ins->use) {
16968                         return;
16969                 }
16970                 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
16971                 break;
16972                 /* Ignore OP_PIECE */
16973         case OP_PIECE:
16974                 break;
16975                 /* Operations I am not yet certain how to handle */
16976         case OP_UMUL:
16977         case OP_SDIV: case OP_UDIV:
16978         case OP_SMOD: case OP_UMOD:
16979                 /* Operations that should never get here */
16980         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
16981         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
16982         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
16983         default:
16984                 internal_error(state, ins, "unknown op: %d %s",
16985                         ins->op, tops(ins->op));
16986                 break;
16987         }
16988 }
16989
16990 static void print_instructions(struct compile_state *state)
16991 {
16992         struct triple *first, *ins;
16993         int print_location;
16994         struct occurance *last_occurance;
16995         FILE *fp;
16996         print_location = 1;
16997         last_occurance = 0;
16998         fp = state->output;
16999         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17000         first = RHS(state->main_function, 0);
17001         ins = first;
17002         do {
17003                 if (print_location && 
17004                         last_occurance != ins->occurance) {
17005                         if (!ins->occurance->parent) {
17006                                 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
17007                                         ins->occurance->function,
17008                                         ins->occurance->filename,
17009                                         ins->occurance->line,
17010                                         ins->occurance->col);
17011                         }
17012                         else {
17013                                 struct occurance *ptr;
17014                                 fprintf(fp, "\t/*\n");
17015                                 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
17016                                         fprintf(fp, "\t * %s,%s:%d.%d\n",
17017                                                 ptr->function,
17018                                                 ptr->filename,
17019                                                 ptr->line,
17020                                                 ptr->col);
17021                                 }
17022                                 fprintf(fp, "\t */\n");
17023                                 
17024                         }
17025                         if (last_occurance) {
17026                                 put_occurance(last_occurance);
17027                         }
17028                         get_occurance(ins->occurance);
17029                         last_occurance = ins->occurance;
17030                 }
17031
17032                 print_instruction(state, ins, fp);
17033                 ins = ins->next;
17034         } while(ins != first);
17035         
17036 }
17037 static void generate_code(struct compile_state *state)
17038 {
17039         generate_local_labels(state);
17040         print_instructions(state);
17041         
17042 }
17043
17044 static void print_tokens(struct compile_state *state)
17045 {
17046         struct token *tk;
17047         tk = &state->token[0];
17048         do {
17049 #if 1
17050                 token(state, 0);
17051 #else
17052                 next_token(state, 0);
17053 #endif
17054                 loc(stdout, state, 0);
17055                 printf("%s <- `%s'\n",
17056                         tokens[tk->tok],
17057                         tk->ident ? tk->ident->name :
17058                         tk->str_len ? tk->val.str : "");
17059                 
17060         } while(tk->tok != TOK_EOF);
17061 }
17062
17063 static void compile(const char *filename, const char *ofilename, 
17064         int cpu, int debug, int opt, const char *label_prefix)
17065 {
17066         int i;
17067         struct compile_state state;
17068         memset(&state, 0, sizeof(state));
17069         state.file = 0;
17070         for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17071                 memset(&state.token[i], 0, sizeof(state.token[i]));
17072                 state.token[i].tok = -1;
17073         }
17074         /* Remember the debug settings */
17075         state.cpu      = cpu;
17076         state.debug    = debug;
17077         state.optimize = opt;
17078         /* Remember the output filename */
17079         state.ofilename = ofilename;
17080         state.output    = fopen(state.ofilename, "w");
17081         if (!state.output) {
17082                 error(&state, 0, "Cannot open output file %s\n",
17083                         ofilename);
17084         }
17085         /* Remember the label prefix */
17086         state.label_prefix = label_prefix;
17087         /* Prep the preprocessor */
17088         state.if_depth = 0;
17089         state.if_value = 0;
17090         /* register the C keywords */
17091         register_keywords(&state);
17092         /* register the keywords the macro preprocessor knows */
17093         register_macro_keywords(&state);
17094         /* Memorize where some special keywords are. */
17095         state.i_continue = lookup(&state, "continue", 8);
17096         state.i_break    = lookup(&state, "break", 5);
17097         /* Enter the globl definition scope */
17098         start_scope(&state);
17099         register_builtins(&state);
17100         compile_file(&state, filename, 1);
17101 #if 0
17102         print_tokens(&state);
17103 #endif  
17104         decls(&state);
17105         /* Exit the global definition scope */
17106         end_scope(&state);
17107
17108         /* Now that basic compilation has happened 
17109          * optimize the intermediate code 
17110          */
17111         optimize(&state);
17112
17113         generate_code(&state);
17114         if (state.debug) {
17115                 fprintf(stderr, "done\n");
17116         }
17117 }
17118
17119 static void version(void)
17120 {
17121         printf("romcc " VERSION " released " RELEASE_DATE "\n");
17122 }
17123
17124 static void usage(void)
17125 {
17126         version();
17127         printf(
17128                 "Usage: romcc <source>.c\n"
17129                 "Compile a C source file without using ram\n"
17130         );
17131 }
17132
17133 static void arg_error(char *fmt, ...)
17134 {
17135         va_list args;
17136         va_start(args, fmt);
17137         vfprintf(stderr, fmt, args);
17138         va_end(args);
17139         usage();
17140         exit(1);
17141 }
17142
17143 int main(int argc, char **argv)
17144 {
17145         const char *filename;
17146         const char *ofilename;
17147         const char *label_prefix;
17148         int cpu;
17149         int last_argc;
17150         int debug;
17151         int optimize;
17152         cpu = CPU_DEFAULT;
17153         label_prefix = "";
17154         ofilename = "auto.inc";
17155         optimize = 0;
17156         debug = 0;
17157         last_argc = -1;
17158         while((argc > 1) && (argc != last_argc)) {
17159                 last_argc = argc;
17160                 if (strncmp(argv[1], "--debug=", 8) == 0) {
17161                         debug = atoi(argv[1] + 8);
17162                         argv++;
17163                         argc--;
17164                 }
17165                 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
17166                         label_prefix= argv[1] + 15;
17167                         argv++;
17168                         argc--;
17169                 }
17170                 else if ((strcmp(argv[1],"-O") == 0) ||
17171                         (strcmp(argv[1], "-O1") == 0)) {
17172                         optimize = 1;
17173                         argv++;
17174                         argc--;
17175                 }
17176                 else if (strcmp(argv[1],"-O2") == 0) {
17177                         optimize = 2;
17178                         argv++;
17179                         argc--;
17180                 }
17181                 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
17182                         ofilename = argv[2];
17183                         argv += 2;
17184                         argc -= 2;
17185                 }
17186                 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
17187                         cpu = arch_encode_cpu(argv[1] + 6);
17188                         if (cpu == BAD_CPU) {
17189                                 arg_error("Invalid cpu specified: %s\n",
17190                                         argv[1] + 6);
17191                         }
17192                         argv++;
17193                         argc--;
17194                 }
17195         }
17196         if (argc != 2) {
17197                 arg_error("Wrong argument count %d\n", argc);
17198         }
17199         filename = argv[1];
17200         compile(filename, ofilename, cpu, debug, optimize, label_prefix);
17201
17202         return 0;
17203 }