fix romcc compiling 32bit code on amd64
[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 1
18 #define DEBUG_RANGE_CONFLICTS 0
19 #define DEBUG_COALESCING 0
20 #define DEBUG_SDP_BLOCKS 0
21 #define DEBUG_TRIPLE_COLOR 0
22
23 #warning "FIXME boundary cases with small types in larger registers"
24 #warning "FIXME give clear error messages about unused variables"
25 #warning "FIXME properly handle multi dimensional arrays"
26
27 /*  Control flow graph of a loop without goto.
28  * 
29  *        AAA
30  *   +---/
31  *  /
32  * / +--->CCC
33  * | |    / \
34  * | |  DDD EEE    break;
35  * | |    \    \
36  * | |    FFF   \
37  *  \|    / \    \
38  *   |\ GGG HHH   |   continue;
39  *   | \  \   |   |
40  *   |  \ III |  /
41  *   |   \ | /  / 
42  *   |    vvv  /  
43  *   +----BBB /   
44  *         | /
45  *         vv
46  *        JJJ
47  *
48  * 
49  *             AAA
50  *     +-----+  |  +----+
51  *     |      \ | /     |
52  *     |       BBB  +-+ |
53  *     |       / \ /  | |
54  *     |     CCC JJJ / /
55  *     |     / \    / / 
56  *     |   DDD EEE / /  
57  *     |    |   +-/ /
58  *     |   FFF     /    
59  *     |   / \    /     
60  *     | GGG HHH /      
61  *     |  |   +-/
62  *     | III
63  *     +--+ 
64  *
65  * 
66  * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
67  * DFup(Z)    = { Y <- DF(Z) | idom(Y) != X }
68  *
69  *
70  * [] == DFlocal(X) U DF(X)
71  * () == DFup(X)
72  *
73  * Dominator graph of the same nodes.
74  *
75  *           AAA     AAA: [ ] ()
76  *          /   \
77  *        BBB    JJJ BBB: [ JJJ ] ( JJJ )  JJJ: [ ] ()
78  *         |
79  *        CCC        CCC: [ ] ( BBB, JJJ )
80  *        / \
81  *     DDD   EEE     DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
82  *      |
83  *     FFF           FFF: [ ] ( BBB )
84  *     / \         
85  *  GGG   HHH        GGG: [ ] ( BBB ) HHH: [ BBB ] ()
86  *   |
87  *  III              III: [ BBB ] ()
88  *
89  *
90  * BBB and JJJ are definitely the dominance frontier.
91  * Where do I place phi functions and how do I make that decision.
92  *   
93  */
94 static void die(char *fmt, ...)
95 {
96         va_list args;
97
98         va_start(args, fmt);
99         vfprintf(stderr, fmt, args);
100         va_end(args);
101         fflush(stdout);
102         fflush(stderr);
103         exit(1);
104 }
105
106 #define MALLOC_STRONG_DEBUG
107 static void *xmalloc(size_t size, const char *name)
108 {
109         void *buf;
110         buf = malloc(size);
111         if (!buf) {
112                 die("Cannot malloc %ld bytes to hold %s: %s\n",
113                         size + 0UL, name, strerror(errno));
114         }
115         return buf;
116 }
117
118 static void *xcmalloc(size_t size, const char *name)
119 {
120         void *buf;
121         buf = xmalloc(size, name);
122         memset(buf, 0, size);
123         return buf;
124 }
125
126 static void xfree(const void *ptr)
127 {
128         free((void *)ptr);
129 }
130
131 static char *xstrdup(const char *str)
132 {
133         char *new;
134         int len;
135         len = strlen(str);
136         new = xmalloc(len + 1, "xstrdup string");
137         memcpy(new, str, len);
138         new[len] = '\0';
139         return new;
140 }
141
142 static void xchdir(const char *path)
143 {
144         if (chdir(path) != 0) {
145                 die("chdir to %s failed: %s\n",
146                         path, strerror(errno));
147         }
148 }
149
150 static int exists(const char *dirname, const char *filename)
151 {
152         int does_exist = 1;
153         xchdir(dirname);
154         if (access(filename, O_RDONLY) < 0) {
155                 if ((errno != EACCES) && (errno != EROFS)) {
156                         does_exist = 0;
157                 }
158         }
159         return does_exist;
160 }
161
162
163 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
164 {
165         int fd;
166         char *buf;
167         off_t size, progress;
168         ssize_t result;
169         struct stat stats;
170         
171         if (!filename) {
172                 *r_size = 0;
173                 return 0;
174         }
175         xchdir(dirname);
176         fd = open(filename, O_RDONLY);
177         if (fd < 0) {
178                 die("Cannot open '%s' : %s\n",
179                         filename, strerror(errno));
180         }
181         result = fstat(fd, &stats);
182         if (result < 0) {
183                 die("Cannot stat: %s: %s\n",
184                         filename, strerror(errno));
185         }
186         size = stats.st_size;
187         *r_size = size +1;
188         buf = xmalloc(size +2, filename);
189         buf[size] = '\n'; /* Make certain the file is newline terminated */
190         buf[size+1] = '\0'; /* Null terminate the file for good measure */
191         progress = 0;
192         while(progress < size) {
193                 result = read(fd, buf + progress, size - progress);
194                 if (result < 0) {
195                         if ((errno == EINTR) || (errno == EAGAIN))
196                                 continue;
197                         die("read on %s of %ld bytes failed: %s\n",
198                                 filename, (size - progress)+ 0UL, strerror(errno));
199                 }
200                 progress += result;
201         }
202         result = close(fd);
203         if (result < 0) {
204                 die("Close of %s failed: %s\n",
205                         filename, strerror(errno));
206         }
207         return buf;
208 }
209
210 /* Long on the destination platform */
211 #ifdef __x86_64__
212 typedef unsigned int ulong_t;
213 typedef int long_t;
214 #else
215 typedef unsigned long ulong_t;
216 typedef long long_t;
217 #endif
218
219 struct file_state {
220         struct file_state *prev;
221         const char *basename;
222         char *dirname;
223         char *buf;
224         off_t size;
225         char *pos;
226         int line;
227         char *line_start;
228         int report_line;
229         const char *report_name;
230         const char *report_dir;
231 };
232 struct hash_entry;
233 struct token {
234         int tok;
235         struct hash_entry *ident;
236         int str_len;
237         union {
238                 ulong_t integer;
239                 const char *str;
240         } val;
241 };
242
243 /* I have two classes of types:
244  * Operational types.
245  * Logical types.  (The type the C standard says the operation is of)
246  *
247  * The operational types are:
248  * chars
249  * shorts
250  * ints
251  * longs
252  *
253  * floats
254  * doubles
255  * long doubles
256  *
257  * pointer
258  */
259
260
261 /* Machine model.
262  * No memory is useable by the compiler.
263  * There is no floating point support.
264  * All operations take place in general purpose registers.
265  * There is one type of general purpose register.
266  * Unsigned longs are stored in that general purpose register.
267  */
268
269 /* Operations on general purpose registers.
270  */
271
272 #define OP_SDIVT      0
273 #define OP_UDIVT      1
274 #define OP_SMUL       2
275 #define OP_UMUL       3
276 #define OP_SDIV       4
277 #define OP_UDIV       5
278 #define OP_SMOD       6
279 #define OP_UMOD       7
280 #define OP_ADD        8
281 #define OP_SUB        9
282 #define OP_SL        10
283 #define OP_USR       11
284 #define OP_SSR       12 
285 #define OP_AND       13 
286 #define OP_XOR       14
287 #define OP_OR        15
288 #define OP_POS       16 /* Dummy positive operator don't use it */
289 #define OP_NEG       17
290 #define OP_INVERT    18
291                      
292 #define OP_EQ        20
293 #define OP_NOTEQ     21
294 #define OP_SLESS     22
295 #define OP_ULESS     23
296 #define OP_SMORE     24
297 #define OP_UMORE     25
298 #define OP_SLESSEQ   26
299 #define OP_ULESSEQ   27
300 #define OP_SMOREEQ   28
301 #define OP_UMOREEQ   29
302                      
303 #define OP_LFALSE    30  /* Test if the expression is logically false */
304 #define OP_LTRUE     31  /* Test if the expression is logcially true */
305
306 #define OP_LOAD      32
307 #define OP_STORE     33
308 /* For OP_STORE ->type holds the type
309  * RHS(0) holds the destination address
310  * RHS(1) holds the value to store.
311  */
312
313 #define OP_NOOP      34
314
315 #define OP_MIN_CONST 50
316 #define OP_MAX_CONST 59
317 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
318 #define OP_INTCONST  50
319 /* For OP_INTCONST ->type holds the type.
320  * ->u.cval holds the constant value.
321  */
322 #define OP_BLOBCONST 51
323 /* For OP_BLOBCONST ->type holds the layout and size
324  * information.  u.blob holds a pointer to the raw binary
325  * data for the constant initializer.
326  */
327 #define OP_ADDRCONST 52
328 /* For OP_ADDRCONST ->type holds the type.
329  * MISC(0) holds the reference to the static variable.
330  * ->u.cval holds an offset from that value.
331  */
332
333 #define OP_WRITE     60 
334 /* OP_WRITE moves one pseudo register to another.
335  * RHS(0) holds the destination pseudo register, which must be an OP_DECL.
336  * RHS(1) holds the psuedo to move.
337  */
338
339 #define OP_READ      61
340 /* OP_READ reads the value of a variable and makes
341  * it available for the pseudo operation.
342  * Useful for things like def-use chains.
343  * RHS(0) holds points to the triple to read from.
344  */
345 #define OP_COPY      62
346 /* OP_COPY makes a copy of the psedo register or constant in RHS(0).
347  */
348 #define OP_PIECE     63
349 /* OP_PIECE returns one piece of a instruction that returns a structure.
350  * MISC(0) is the instruction
351  * u.cval is the LHS piece of the instruction to return.
352  */
353 #define OP_ASM       64
354 /* OP_ASM holds a sequence of assembly instructions, the result
355  * of a C asm directive.
356  * RHS(x) holds input value x to the assembly sequence.
357  * LHS(x) holds the output value x from the assembly sequence.
358  * u.blob holds the string of assembly instructions.
359  */
360
361 #define OP_DEREF     65
362 /* OP_DEREF generates an lvalue from a pointer.
363  * RHS(0) holds the pointer value.
364  * OP_DEREF serves as a place holder to indicate all necessary
365  * checks have been done to indicate a value is an lvalue.
366  */
367 #define OP_DOT       66
368 /* OP_DOT references a submember of a structure lvalue.
369  * RHS(0) holds the lvalue.
370  * ->u.field holds the name of the field we want.
371  *
372  * Not seen outside of expressions.
373  */
374 #define OP_VAL       67
375 /* OP_VAL returns the value of a subexpression of the current expression.
376  * Useful for operators that have side effects.
377  * RHS(0) holds the expression.
378  * MISC(0) holds the subexpression of RHS(0) that is the
379  * value of the expression.
380  *
381  * Not seen outside of expressions.
382  */
383 #define OP_LAND      68
384 /* OP_LAND performs a C logical and between RHS(0) and RHS(1).
385  * Not seen outside of expressions.
386  */
387 #define OP_LOR       69
388 /* OP_LOR performs a C logical or between RHS(0) and RHS(1).
389  * Not seen outside of expressions.
390  */
391 #define OP_COND      70
392 /* OP_CODE performas a C ? : operation. 
393  * RHS(0) holds the test.
394  * RHS(1) holds the expression to evaluate if the test returns true.
395  * RHS(2) holds the expression to evaluate if the test returns false.
396  * Not seen outside of expressions.
397  */
398 #define OP_COMMA     71
399 /* OP_COMMA performacs a C comma operation.
400  * That is RHS(0) is evaluated, then RHS(1)
401  * and the value of RHS(1) is returned.
402  * Not seen outside of expressions.
403  */
404
405 #define OP_CALL      72
406 /* OP_CALL performs a procedure call. 
407  * MISC(0) holds a pointer to the OP_LIST of a function
408  * RHS(x) holds argument x of a function
409  * 
410  * Currently not seen outside of expressions.
411  */
412 #define OP_VAL_VEC   74
413 /* OP_VAL_VEC is an array of triples that are either variable
414  * or values for a structure or an array.
415  * RHS(x) holds element x of the vector.
416  * triple->type->elements holds the size of the vector.
417  */
418
419 /* statements */
420 #define OP_LIST      80
421 /* OP_LIST Holds a list of statements, and a result value.
422  * RHS(0) holds the list of statements.
423  * MISC(0) holds the value of the statements.
424  */
425
426 #define OP_BRANCH    81 /* branch */
427 /* For branch instructions
428  * TARG(0) holds the branch target.
429  * RHS(0) if present holds the branch condition.
430  * ->next holds where to branch to if the branch is not taken.
431  * The branch target can only be a decl...
432  */
433
434 #define OP_LABEL     83
435 /* OP_LABEL is a triple that establishes an target for branches.
436  * ->use is the list of all branches that use this label.
437  */
438
439 #define OP_ADECL     84 
440 /* OP_DECL is a triple that establishes an lvalue for assignments.
441  * ->use is a list of statements that use the variable.
442  */
443
444 #define OP_SDECL     85
445 /* OP_SDECL is a triple that establishes a variable of static
446  * storage duration.
447  * ->use is a list of statements that use the variable.
448  * MISC(0) holds the initializer expression.
449  */
450
451
452 #define OP_PHI       86
453 /* OP_PHI is a triple used in SSA form code.  
454  * It is used when multiple code paths merge and a variable needs
455  * a single assignment from any of those code paths.
456  * The operation is a cross between OP_DECL and OP_WRITE, which
457  * is what OP_PHI is geneared from.
458  * 
459  * RHS(x) points to the value from code path x
460  * The number of RHS entries is the number of control paths into the block
461  * in which OP_PHI resides.  The elements of the array point to point
462  * to the variables OP_PHI is derived from.
463  *
464  * MISC(0) holds a pointer to the orginal OP_DECL node.
465  */
466
467 /* Architecture specific instructions */
468 #define OP_CMP         100
469 #define OP_TEST        101
470 #define OP_SET_EQ      102
471 #define OP_SET_NOTEQ   103
472 #define OP_SET_SLESS   104
473 #define OP_SET_ULESS   105
474 #define OP_SET_SMORE   106
475 #define OP_SET_UMORE   107
476 #define OP_SET_SLESSEQ 108
477 #define OP_SET_ULESSEQ 109
478 #define OP_SET_SMOREEQ 110
479 #define OP_SET_UMOREEQ 111
480
481 #define OP_JMP         112
482 #define OP_JMP_EQ      113
483 #define OP_JMP_NOTEQ   114
484 #define OP_JMP_SLESS   115
485 #define OP_JMP_ULESS   116
486 #define OP_JMP_SMORE   117
487 #define OP_JMP_UMORE   118
488 #define OP_JMP_SLESSEQ 119
489 #define OP_JMP_ULESSEQ 120
490 #define OP_JMP_SMOREEQ 121
491 #define OP_JMP_UMOREEQ 122
492
493 /* Builtin operators that it is just simpler to use the compiler for */
494 #define OP_INB         130
495 #define OP_INW         131
496 #define OP_INL         132
497 #define OP_OUTB        133
498 #define OP_OUTW        134
499 #define OP_OUTL        135
500 #define OP_BSF         136
501 #define OP_BSR         137
502 #define OP_RDMSR       138
503 #define OP_WRMSR       139
504 #define OP_HLT         140
505
506 struct op_info {
507         const char *name;
508         unsigned flags;
509 #define PURE   1
510 #define IMPURE 2
511 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
512 #define DEF    4
513 #define BLOCK  8 /* Triple stores the current block */
514         unsigned char lhs, rhs, misc, targ;
515 };
516
517 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
518         .name = (NAME), \
519         .flags = (FLAGS), \
520         .lhs = (LHS), \
521         .rhs = (RHS), \
522         .misc = (MISC), \
523         .targ = (TARG), \
524          }
525 static const struct op_info table_ops[] = {
526 [OP_SDIVT      ] = OP( 2,  2, 0, 0, PURE | BLOCK , "sdivt"),
527 [OP_UDIVT      ] = OP( 2,  2, 0, 0, PURE | BLOCK , "udivt"),
528 [OP_SMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smul"),
529 [OP_UMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umul"),
530 [OP_SDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
531 [OP_UDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "udiv"),
532 [OP_SMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smod"),
533 [OP_UMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umod"),
534 [OP_ADD        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "add"),
535 [OP_SUB        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sub"),
536 [OP_SL         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sl"),
537 [OP_USR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "usr"),
538 [OP_SSR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ssr"),
539 [OP_AND        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "and"),
540 [OP_XOR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "xor"),
541 [OP_OR         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "or"),
542 [OP_POS        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "pos"),
543 [OP_NEG        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "neg"),
544 [OP_INVERT     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "invert"),
545
546 [OP_EQ         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "eq"),
547 [OP_NOTEQ      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "noteq"),
548 [OP_SLESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sless"),
549 [OP_ULESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "uless"),
550 [OP_SMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smore"),
551 [OP_UMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umore"),
552 [OP_SLESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
553 [OP_ULESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
554 [OP_SMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
555 [OP_UMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
556 [OP_LFALSE     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
557 [OP_LTRUE      ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
558
559 [OP_LOAD       ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "load"),
560 [OP_STORE      ] = OP( 0,  2, 0, 0, IMPURE | BLOCK , "store"),
561
562 [OP_NOOP       ] = OP( 0,  0, 0, 0, PURE | BLOCK, "noop"),
563
564 [OP_INTCONST   ] = OP( 0,  0, 0, 0, PURE | DEF, "intconst"),
565 [OP_BLOBCONST  ] = OP( 0,  0, 0, 0, PURE, "blobconst"),
566 [OP_ADDRCONST  ] = OP( 0,  0, 1, 0, PURE | DEF, "addrconst"),
567
568 [OP_WRITE      ] = OP( 0,  2, 0, 0, PURE | BLOCK, "write"),
569 [OP_READ       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "read"),
570 [OP_COPY       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "copy"),
571 [OP_PIECE      ] = OP( 0,  0, 1, 0, PURE | DEF, "piece"),
572 [OP_ASM        ] = OP(-1, -1, 0, 0, IMPURE, "asm"),
573 [OP_DEREF      ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "deref"), 
574 [OP_DOT        ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "dot"),
575
576 [OP_VAL        ] = OP( 0,  1, 1, 0, 0 | DEF | BLOCK, "val"),
577 [OP_LAND       ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "land"),
578 [OP_LOR        ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "lor"),
579 [OP_COND       ] = OP( 0,  3, 0, 0, 0 | DEF | BLOCK, "cond"),
580 [OP_COMMA      ] = OP( 0,  2, 0, 0, 0 | DEF | BLOCK, "comma"),
581 /* Call is special most it can stand in for anything so it depends on context */
582 [OP_CALL       ] = OP(-1, -1, 1, 0, 0 | BLOCK, "call"),
583 /* The sizes of OP_CALL and OP_VAL_VEC depend upon context */
584 [OP_VAL_VEC    ] = OP( 0, -1, 0, 0, 0 | BLOCK, "valvec"),
585
586 [OP_LIST       ] = OP( 0,  1, 1, 0, 0 | DEF, "list"),
587 /* The number of targets for OP_BRANCH depends on context */
588 [OP_BRANCH     ] = OP( 0, -1, 0, 1, PURE | BLOCK, "branch"),
589 [OP_LABEL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "label"),
590 [OP_ADECL      ] = OP( 0,  0, 0, 0, PURE | BLOCK, "adecl"),
591 [OP_SDECL      ] = OP( 0,  0, 1, 0, PURE | BLOCK, "sdecl"),
592 /* The number of RHS elements of OP_PHI depend upon context */
593 [OP_PHI        ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
594
595 [OP_CMP        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK, "cmp"),
596 [OP_TEST       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "test"),
597 [OP_SET_EQ     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
598 [OP_SET_NOTEQ  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
599 [OP_SET_SLESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
600 [OP_SET_ULESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
601 [OP_SET_SMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
602 [OP_SET_UMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
603 [OP_SET_SLESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
604 [OP_SET_ULESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
605 [OP_SET_SMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
606 [OP_SET_UMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
607 [OP_JMP        ] = OP( 0,  0, 0, 1, PURE | BLOCK, "jmp"),
608 [OP_JMP_EQ     ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_eq"),
609 [OP_JMP_NOTEQ  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_noteq"),
610 [OP_JMP_SLESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_sless"),
611 [OP_JMP_ULESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_uless"),
612 [OP_JMP_SMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smore"),
613 [OP_JMP_UMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umore"),
614 [OP_JMP_SLESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_slesseq"),
615 [OP_JMP_ULESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_ulesseq"),
616 [OP_JMP_SMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_smoreq"),
617 [OP_JMP_UMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK, "jmp_umoreq"),
618
619 [OP_INB        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
620 [OP_INW        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
621 [OP_INL        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
622 [OP_OUTB       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outb"),
623 [OP_OUTW       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outw"),
624 [OP_OUTL       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outl"),
625 [OP_BSF        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
626 [OP_BSR        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
627 [OP_RDMSR      ] = OP( 2,  1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
628 [OP_WRMSR      ] = OP( 0,  3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
629 [OP_HLT        ] = OP( 0,  0, 0, 0, IMPURE | BLOCK, "__hlt"),
630 };
631 #undef OP
632 #define OP_MAX      (sizeof(table_ops)/sizeof(table_ops[0]))
633
634 static const char *tops(int index) 
635 {
636         static const char unknown[] = "unknown op";
637         if (index < 0) {
638                 return unknown;
639         }
640         if (index > OP_MAX) {
641                 return unknown;
642         }
643         return table_ops[index].name;
644 }
645
646 struct asm_info;
647 struct triple;
648 struct block;
649 struct triple_set {
650         struct triple_set *next;
651         struct triple *member;
652 };
653
654 #define MAX_LHS  15
655 #define MAX_RHS  250
656 #define MAX_MISC 3
657 #define MAX_TARG 3
658
659 struct occurance {
660         int count;
661         const char *filename;
662         const char *function;
663         int line;
664         int col;
665         struct occurance *parent;
666 };
667 struct triple {
668         struct triple *next, *prev;
669         struct triple_set *use;
670         struct type *type;
671         unsigned char op;
672         unsigned char template_id;
673         unsigned short sizes;
674 #define TRIPLE_LHS(SIZES)  (((SIZES) >>  0) & 0x0f)
675 #define TRIPLE_RHS(SIZES)  (((SIZES) >>  4) & 0xff)
676 #define TRIPLE_MISC(SIZES) (((SIZES) >> 12) & 0x03)
677 #define TRIPLE_TARG(SIZES) (((SIZES) >> 14) & 0x03)
678 #define TRIPLE_SIZE(SIZES) \
679         (TRIPLE_LHS(SIZES)  + \
680          TRIPLE_RHS(SIZES)  + \
681          TRIPLE_MISC(SIZES) + \
682          TRIPLE_TARG(SIZES))
683 #define TRIPLE_SIZES(LHS, RHS, MISC, TARG) \
684         ((((LHS) & 0x0f) <<  0) | \
685         (((RHS)  & 0xff) <<  4) | \
686         (((MISC) & 0x03) << 12) | \
687         (((TARG) & 0x03) << 14))
688 #define TRIPLE_LHS_OFF(SIZES)  (0)
689 #define TRIPLE_RHS_OFF(SIZES)  (TRIPLE_LHS_OFF(SIZES) + TRIPLE_LHS(SIZES))
690 #define TRIPLE_MISC_OFF(SIZES) (TRIPLE_RHS_OFF(SIZES) + TRIPLE_RHS(SIZES))
691 #define TRIPLE_TARG_OFF(SIZES) (TRIPLE_MISC_OFF(SIZES) + TRIPLE_MISC(SIZES))
692 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF((PTR)->sizes) + (INDEX)])
693 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF((PTR)->sizes) + (INDEX)])
694 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF((PTR)->sizes) + (INDEX)])
695 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF((PTR)->sizes) + (INDEX)])
696         unsigned id; /* A scratch value and finally the register */
697 #define TRIPLE_FLAG_FLATTENED   (1 << 31)
698 #define TRIPLE_FLAG_PRE_SPLIT   (1 << 30)
699 #define TRIPLE_FLAG_POST_SPLIT  (1 << 29)
700         struct occurance *occurance;
701         union {
702                 ulong_t cval;
703                 struct block  *block;
704                 void *blob;
705                 struct hash_entry *field;
706                 struct asm_info *ainfo;
707         } u;
708         struct triple *param[2];
709 };
710
711 struct reg_info {
712         unsigned reg;
713         unsigned regcm;
714 };
715 struct ins_template {
716         struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
717 };
718
719 struct asm_info {
720         struct ins_template tmpl;
721         char *str;
722 };
723
724 struct block_set {
725         struct block_set *next;
726         struct block *member;
727 };
728 struct block {
729         struct block *work_next;
730         struct block *left, *right;
731         struct triple *first, *last;
732         int users;
733         struct block_set *use;
734         struct block_set *idominates;
735         struct block_set *domfrontier;
736         struct block *idom;
737         struct block_set *ipdominates;
738         struct block_set *ipdomfrontier;
739         struct block *ipdom;
740         int vertex;
741         
742 };
743
744 struct symbol {
745         struct symbol *next;
746         struct hash_entry *ident;
747         struct triple *def;
748         struct type *type;
749         int scope_depth;
750 };
751
752 struct macro {
753         struct hash_entry *ident;
754         char *buf;
755         int buf_len;
756 };
757
758 struct hash_entry {
759         struct hash_entry *next;
760         const char *name;
761         int name_len;
762         int tok;
763         struct macro *sym_define;
764         struct symbol *sym_label;
765         struct symbol *sym_struct;
766         struct symbol *sym_ident;
767 };
768
769 #define HASH_TABLE_SIZE 2048
770
771 struct compile_state {
772         const char *label_prefix;
773         const char *ofilename;
774         FILE *output;
775         struct file_state *file;
776         struct occurance *last_occurance;
777         const char *function;
778         struct token token[4];
779         struct hash_entry *hash_table[HASH_TABLE_SIZE];
780         struct hash_entry *i_continue;
781         struct hash_entry *i_break;
782         int scope_depth;
783         int if_depth, if_value;
784         int macro_line;
785         struct file_state *macro_file;
786         struct triple *main_function;
787         struct block *first_block, *last_block;
788         int last_vertex;
789         int cpu;
790         int debug;
791         int optimize;
792 };
793
794 /* visibility global/local */
795 /* static/auto duration */
796 /* typedef, register, inline */
797 #define STOR_SHIFT         0
798 #define STOR_MASK     0x000f
799 /* Visibility */
800 #define STOR_GLOBAL   0x0001
801 /* Duration */
802 #define STOR_PERM     0x0002
803 /* Storage specifiers */
804 #define STOR_AUTO     0x0000
805 #define STOR_STATIC   0x0002
806 #define STOR_EXTERN   0x0003
807 #define STOR_REGISTER 0x0004
808 #define STOR_TYPEDEF  0x0008
809 #define STOR_INLINE   0x000c
810
811 #define QUAL_SHIFT         4
812 #define QUAL_MASK     0x0070
813 #define QUAL_NONE     0x0000
814 #define QUAL_CONST    0x0010
815 #define QUAL_VOLATILE 0x0020
816 #define QUAL_RESTRICT 0x0040
817
818 #define TYPE_SHIFT         8
819 #define TYPE_MASK     0x1f00
820 #define TYPE_INTEGER(TYPE)    (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG))
821 #define TYPE_ARITHMETIC(TYPE) (((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE))
822 #define TYPE_UNSIGNED(TYPE)   ((TYPE) & 0x0100)
823 #define TYPE_SIGNED(TYPE)     (!TYPE_UNSIGNED(TYPE))
824 #define TYPE_MKUNSIGNED(TYPE) ((TYPE) | 0x0100)
825 #define TYPE_RANK(TYPE)       ((TYPE) & ~0x0100)
826 #define TYPE_PTR(TYPE)        (((TYPE) & TYPE_MASK) == TYPE_POINTER)
827 #define TYPE_DEFAULT  0x0000
828 #define TYPE_VOID     0x0100
829 #define TYPE_CHAR     0x0200
830 #define TYPE_UCHAR    0x0300
831 #define TYPE_SHORT    0x0400
832 #define TYPE_USHORT   0x0500
833 #define TYPE_INT      0x0600
834 #define TYPE_UINT     0x0700
835 #define TYPE_LONG     0x0800
836 #define TYPE_ULONG    0x0900
837 #define TYPE_LLONG    0x0a00 /* long long */
838 #define TYPE_ULLONG   0x0b00
839 #define TYPE_FLOAT    0x0c00
840 #define TYPE_DOUBLE   0x0d00
841 #define TYPE_LDOUBLE  0x0e00 /* long double */
842 #define TYPE_STRUCT   0x1000
843 #define TYPE_ENUM     0x1100
844 #define TYPE_POINTER  0x1200 
845 /* For TYPE_POINTER:
846  * type->left holds the type pointed to.
847  */
848 #define TYPE_FUNCTION 0x1300 
849 /* For TYPE_FUNCTION:
850  * type->left holds the return type.
851  * type->right holds the...
852  */
853 #define TYPE_PRODUCT  0x1400
854 /* TYPE_PRODUCT is a basic building block when defining structures
855  * type->left holds the type that appears first in memory.
856  * type->right holds the type that appears next in memory.
857  */
858 #define TYPE_OVERLAP  0x1500
859 /* TYPE_OVERLAP is a basic building block when defining unions
860  * type->left and type->right holds to types that overlap
861  * each other in memory.
862  */
863 #define TYPE_ARRAY    0x1600
864 /* TYPE_ARRAY is a basic building block when definitng arrays.
865  * type->left holds the type we are an array of.
866  * type-> holds the number of elements.
867  */
868
869 #ifdef __x86_64__
870 #define ELEMENT_COUNT_UNSPECIFIED (~0U)
871 #else
872 #define ELEMENT_COUNT_UNSPECIFIED (~0UL)
873 #endif
874
875 struct type {
876         unsigned int type;
877         struct type *left, *right;
878         ulong_t elements;
879         struct hash_entry *field_ident;
880         struct hash_entry *type_ident;
881 };
882
883 #define MAX_REGISTERS      75
884 #define MAX_REG_EQUIVS     16
885 #define REGISTER_BITS      16
886 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
887 #define TEMPLATE_BITS      7
888 #define MAX_TEMPLATES      (1<<TEMPLATE_BITS)
889 #define MAX_REGC           14
890 #define REG_UNSET          0
891 #define REG_UNNEEDED       1
892 #define REG_VIRT0          (MAX_REGISTERS + 0)
893 #define REG_VIRT1          (MAX_REGISTERS + 1)
894 #define REG_VIRT2          (MAX_REGISTERS + 2)
895 #define REG_VIRT3          (MAX_REGISTERS + 3)
896 #define REG_VIRT4          (MAX_REGISTERS + 4)
897 #define REG_VIRT5          (MAX_REGISTERS + 5)
898 #define REG_VIRT6          (MAX_REGISTERS + 5)
899 #define REG_VIRT7          (MAX_REGISTERS + 5)
900 #define REG_VIRT8          (MAX_REGISTERS + 5)
901 #define REG_VIRT9          (MAX_REGISTERS + 5)
902
903 /* Provision for 8 register classes */
904 #define REG_SHIFT  0
905 #define REGC_SHIFT REGISTER_BITS
906 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
907 #define REG_MASK (MAX_VIRT_REGISTERS -1)
908 #define ID_REG(ID)              ((ID) & REG_MASK)
909 #define SET_REG(ID, REG)        ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
910 #define ID_REGCM(ID)            (((ID) & REGC_MASK) >> REGC_SHIFT)
911 #define SET_REGCM(ID, REGCM)    ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
912 #define SET_INFO(ID, INFO)      ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
913                 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
914
915 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
916 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
917 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
918 static void arch_reg_equivs(
919         struct compile_state *state, unsigned *equiv, int reg);
920 static int arch_select_free_register(
921         struct compile_state *state, char *used, int classes);
922 static unsigned arch_regc_size(struct compile_state *state, int class);
923 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
924 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
925 static const char *arch_reg_str(int reg);
926 static struct reg_info arch_reg_constraint(
927         struct compile_state *state, struct type *type, const char *constraint);
928 static struct reg_info arch_reg_clobber(
929         struct compile_state *state, const char *clobber);
930 static struct reg_info arch_reg_lhs(struct compile_state *state, 
931         struct triple *ins, int index);
932 static struct reg_info arch_reg_rhs(struct compile_state *state, 
933         struct triple *ins, int index);
934 static struct triple *transform_to_arch_instruction(
935         struct compile_state *state, struct triple *ins);
936
937
938
939 #define DEBUG_ABORT_ON_ERROR    0x0001
940 #define DEBUG_INTERMEDIATE_CODE 0x0002
941 #define DEBUG_CONTROL_FLOW      0x0004
942 #define DEBUG_BASIC_BLOCKS      0x0008
943 #define DEBUG_FDOMINATORS       0x0010
944 #define DEBUG_RDOMINATORS       0x0020
945 #define DEBUG_TRIPLES           0x0040
946 #define DEBUG_INTERFERENCE      0x0080
947 #define DEBUG_ARCH_CODE         0x0100
948 #define DEBUG_CODE_ELIMINATION  0x0200
949 #define DEBUG_INSERTED_COPIES   0x0400
950
951 #define GLOBAL_SCOPE_DEPTH   1
952 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
953
954 static void compile_file(struct compile_state *old_state, const char *filename, int local);
955
956 static void do_cleanup(struct compile_state *state)
957 {
958         if (state->output) {
959                 fclose(state->output);
960                 unlink(state->ofilename);
961         }
962 }
963
964 static int get_col(struct file_state *file)
965 {
966         int col;
967         char *ptr, *end;
968         ptr = file->line_start;
969         end = file->pos;
970         for(col = 0; ptr < end; ptr++) {
971                 if (*ptr != '\t') {
972                         col++;
973                 } 
974                 else {
975                         col = (col & ~7) + 8;
976                 }
977         }
978         return col;
979 }
980
981 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
982 {
983         int col;
984         if (triple && triple->occurance) {
985                 struct occurance *spot;
986                 spot = triple->occurance;
987                 while(spot->parent) {
988                         spot = spot->parent;
989                 }
990                 fprintf(fp, "%s:%d.%d: ", 
991                         spot->filename, spot->line, spot->col);
992                 return;
993         }
994         if (!state->file) {
995                 return;
996         }
997         col = get_col(state->file);
998         fprintf(fp, "%s:%d.%d: ", 
999                 state->file->report_name, state->file->report_line, col);
1000 }
1001
1002 static void __internal_error(struct compile_state *state, struct triple *ptr, 
1003         char *fmt, ...)
1004 {
1005         va_list args;
1006         va_start(args, fmt);
1007         loc(stderr, state, ptr);
1008         if (ptr) {
1009                 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1010         }
1011         fprintf(stderr, "Internal compiler error: ");
1012         vfprintf(stderr, fmt, args);
1013         fprintf(stderr, "\n");
1014         va_end(args);
1015         do_cleanup(state);
1016         abort();
1017 }
1018
1019
1020 static void __internal_warning(struct compile_state *state, struct triple *ptr, 
1021         char *fmt, ...)
1022 {
1023         va_list args;
1024         va_start(args, fmt);
1025         loc(stderr, state, ptr);
1026         if (ptr) {
1027                 fprintf(stderr, "%p %s ", ptr, tops(ptr->op));
1028         }
1029         fprintf(stderr, "Internal compiler warning: ");
1030         vfprintf(stderr, fmt, args);
1031         fprintf(stderr, "\n");
1032         va_end(args);
1033 }
1034
1035
1036
1037 static void __error(struct compile_state *state, struct triple *ptr, 
1038         char *fmt, ...)
1039 {
1040         va_list args;
1041         va_start(args, fmt);
1042         loc(stderr, state, ptr);
1043         vfprintf(stderr, fmt, args);
1044         va_end(args);
1045         fprintf(stderr, "\n");
1046         do_cleanup(state);
1047         if (state->debug & DEBUG_ABORT_ON_ERROR) {
1048                 abort();
1049         }
1050         exit(1);
1051 }
1052
1053 static void __warning(struct compile_state *state, struct triple *ptr, 
1054         char *fmt, ...)
1055 {
1056         va_list args;
1057         va_start(args, fmt);
1058         loc(stderr, state, ptr);
1059         fprintf(stderr, "warning: "); 
1060         vfprintf(stderr, fmt, args);
1061         fprintf(stderr, "\n");
1062         va_end(args);
1063 }
1064
1065 #if DEBUG_ERROR_MESSAGES 
1066 #  define internal_error fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_error
1067 #  define internal_warning fprintf(stderr,  "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__internal_warning
1068 #  define error fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__error
1069 #  define warning fprintf(stderr, "@ %s.%s:%d \t", __FILE__, __func__, __LINE__),__warning
1070 #else
1071 #  define internal_error __internal_error
1072 #  define internal_warning __internal_warning
1073 #  define error __error
1074 #  define warning __warning
1075 #endif
1076 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1077
1078 static void valid_op(struct compile_state *state, int op)
1079 {
1080         char *fmt = "invalid op: %d";
1081         if (op >= OP_MAX) {
1082                 internal_error(state, 0, fmt, op);
1083         }
1084         if (op < 0) {
1085                 internal_error(state, 0, fmt, op);
1086         }
1087 }
1088
1089 static void valid_ins(struct compile_state *state, struct triple *ptr)
1090 {
1091         valid_op(state, ptr->op);
1092 }
1093
1094 static void process_trigraphs(struct compile_state *state)
1095 {
1096         char *src, *dest, *end;
1097         struct file_state *file;
1098         file = state->file;
1099         src = dest = file->buf;
1100         end = file->buf + file->size;
1101         while((end - src) >= 3) {
1102                 if ((src[0] == '?') && (src[1] == '?')) {
1103                         int c = -1;
1104                         switch(src[2]) {
1105                         case '=': c = '#'; break;
1106                         case '/': c = '\\'; break;
1107                         case '\'': c = '^'; break;
1108                         case '(': c = '['; break;
1109                         case ')': c = ']'; break;
1110                         case '!': c = '!'; break;
1111                         case '<': c = '{'; break;
1112                         case '>': c = '}'; break;
1113                         case '-': c = '~'; break;
1114                         }
1115                         if (c != -1) {
1116                                 *dest++ = c;
1117                                 src += 3;
1118                         }
1119                         else {
1120                                 *dest++ = *src++;
1121                         }
1122                 }
1123                 else {
1124                         *dest++ = *src++;
1125                 }
1126         }
1127         while(src != end) {
1128                 *dest++ = *src++;
1129         }
1130         file->size = dest - file->buf;
1131 }
1132
1133 static void splice_lines(struct compile_state *state)
1134 {
1135         char *src, *dest, *end;
1136         struct file_state *file;
1137         file = state->file;
1138         src = dest = file->buf;
1139         end = file->buf + file->size;
1140         while((end - src) >= 2) {
1141                 if ((src[0] == '\\') && (src[1] == '\n')) {
1142                         src += 2;
1143                 }
1144                 else {
1145                         *dest++ = *src++;
1146                 }
1147         }
1148         while(src != end) {
1149                 *dest++ = *src++;
1150         }
1151         file->size = dest - file->buf;
1152 }
1153
1154 static struct type void_type;
1155 static void use_triple(struct triple *used, struct triple *user)
1156 {
1157         struct triple_set **ptr, *new;
1158         if (!used)
1159                 return;
1160         if (!user)
1161                 return;
1162         ptr = &used->use;
1163         while(*ptr) {
1164                 if ((*ptr)->member == user) {
1165                         return;
1166                 }
1167                 ptr = &(*ptr)->next;
1168         }
1169         /* Append new to the head of the list, 
1170          * copy_func and rename_block_variables
1171          * depends on this.
1172          */
1173         new = xcmalloc(sizeof(*new), "triple_set");
1174         new->member = user;
1175         new->next   = used->use;
1176         used->use   = new;
1177 }
1178
1179 static void unuse_triple(struct triple *used, struct triple *unuser)
1180 {
1181         struct triple_set *use, **ptr;
1182         if (!used) {
1183                 return;
1184         }
1185         ptr = &used->use;
1186         while(*ptr) {
1187                 use = *ptr;
1188                 if (use->member == unuser) {
1189                         *ptr = use->next;
1190                         xfree(use);
1191                 }
1192                 else {
1193                         ptr = &use->next;
1194                 }
1195         }
1196 }
1197
1198 static void put_occurance(struct occurance *occurance)
1199 {
1200         occurance->count -= 1;
1201         if (occurance->count <= 0) {
1202                 if (occurance->parent) {
1203                         put_occurance(occurance->parent);
1204                 }
1205                 xfree(occurance);
1206         }
1207 }
1208
1209 static void get_occurance(struct occurance *occurance)
1210 {
1211         occurance->count += 1;
1212 }
1213
1214
1215 static struct occurance *new_occurance(struct compile_state *state)
1216 {
1217         struct occurance *result, *last;
1218         const char *filename;
1219         const char *function;
1220         int line, col;
1221
1222         function = "";
1223         filename = 0;
1224         line = 0;
1225         col  = 0;
1226         if (state->file) {
1227                 filename = state->file->report_name;
1228                 line     = state->file->report_line;
1229                 col      = get_col(state->file);
1230         }
1231         if (state->function) {
1232                 function = state->function;
1233         }
1234         last = state->last_occurance;
1235         if (last &&
1236                 (last->col == col) &&
1237                 (last->line == line) &&
1238                 (last->function == function) &&
1239                 (strcmp(last->filename, filename) == 0)) {
1240                 get_occurance(last);
1241                 return last;
1242         }
1243         if (last) {
1244                 state->last_occurance = 0;
1245                 put_occurance(last);
1246         }
1247         result = xmalloc(sizeof(*result), "occurance");
1248         result->count    = 2;
1249         result->filename = filename;
1250         result->function = function;
1251         result->line     = line;
1252         result->col      = col;
1253         result->parent   = 0;
1254         state->last_occurance = result;
1255         return result;
1256 }
1257
1258 static struct occurance *inline_occurance(struct compile_state *state,
1259         struct occurance *new, struct occurance *orig)
1260 {
1261         struct occurance *result, *last;
1262         last = state->last_occurance;
1263         if (last &&
1264                 (last->parent   == orig) &&
1265                 (last->col      == new->col) &&
1266                 (last->line     == new->line) &&
1267                 (last->function == new->function) &&
1268                 (last->filename == new->filename)) {
1269                 get_occurance(last);
1270                 return last;
1271         }
1272         if (last) {
1273                 state->last_occurance = 0;
1274                 put_occurance(last);
1275         }
1276         get_occurance(orig);
1277         result = xmalloc(sizeof(*result), "occurance");
1278         result->count    = 2;
1279         result->filename = new->filename;
1280         result->function = new->function;
1281         result->line     = new->line;
1282         result->col      = new->col;
1283         result->parent   = orig;
1284         state->last_occurance = result;
1285         return result;
1286 }
1287         
1288
1289 static struct occurance dummy_occurance = {
1290         .count    = 2,
1291         .filename = __FILE__,
1292         .function = "",
1293         .line     = __LINE__,
1294         .col      = 0,
1295         .parent   = 0,
1296 };
1297
1298 /* The zero triple is used as a place holder when we are removing pointers
1299  * from a triple.  Having allows certain sanity checks to pass even
1300  * when the original triple that was pointed to is gone.
1301  */
1302 static struct triple zero_triple = {
1303         .next      = &zero_triple,
1304         .prev      = &zero_triple,
1305         .use       = 0,
1306         .op        = OP_INTCONST,
1307         .sizes     = TRIPLE_SIZES(0, 0, 0, 0),
1308         .id        = -1, /* An invalid id */
1309         .u = { .cval = 0, },
1310         .occurance = &dummy_occurance,
1311         .param = { [0] = 0, [1] = 0, },
1312 };
1313
1314
1315 static unsigned short triple_sizes(struct compile_state *state,
1316         int op, struct type *type, int lhs_wanted, int rhs_wanted,
1317         struct occurance *occurance)
1318 {
1319         int lhs, rhs, misc, targ;
1320         struct triple dummy;
1321         dummy.op = op;
1322         dummy.occurance = occurance;
1323         valid_op(state, op);
1324         lhs = table_ops[op].lhs;
1325         rhs = table_ops[op].rhs;
1326         misc = table_ops[op].misc;
1327         targ = table_ops[op].targ;
1328         
1329         
1330         if (op == OP_CALL) {
1331                 struct type *param;
1332                 rhs = 0;
1333                 param = type->right;
1334                 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
1335                         rhs++;
1336                         param = param->right;
1337                 }
1338                 if ((param->type & TYPE_MASK) != TYPE_VOID) {
1339                         rhs++;
1340                 }
1341                 lhs = 0;
1342                 if ((type->left->type & TYPE_MASK) == TYPE_STRUCT) {
1343                         lhs = type->left->elements;
1344                 }
1345         }
1346         else if (op == OP_VAL_VEC) {
1347                 rhs = type->elements;
1348         }
1349         else if ((op == OP_BRANCH) || (op == OP_PHI)) {
1350                 rhs = rhs_wanted;
1351         }
1352         else if (op == OP_ASM) {
1353                 rhs = rhs_wanted;
1354                 lhs = lhs_wanted;
1355         }
1356         if ((rhs < 0) || (rhs > MAX_RHS)) {
1357                 internal_error(state, &dummy, "bad rhs %d", rhs);
1358         }
1359         if ((lhs < 0) || (lhs > MAX_LHS)) {
1360                 internal_error(state, &dummy, "bad lhs");
1361         }
1362         if ((misc < 0) || (misc > MAX_MISC)) {
1363                 internal_error(state, &dummy, "bad misc");
1364         }
1365         if ((targ < 0) || (targ > MAX_TARG)) {
1366                 internal_error(state, &dummy, "bad targs");
1367         }
1368         return TRIPLE_SIZES(lhs, rhs, misc, targ);
1369 }
1370
1371 static struct triple *alloc_triple(struct compile_state *state, 
1372         int op, struct type *type, int lhs, int rhs,
1373         struct occurance *occurance)
1374 {
1375         size_t size, sizes, extra_count, min_count;
1376         struct triple *ret;
1377         sizes = triple_sizes(state, op, type, lhs, rhs, occurance);
1378
1379         min_count = sizeof(ret->param)/sizeof(ret->param[0]);
1380         extra_count = TRIPLE_SIZE(sizes);
1381         extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
1382
1383         size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
1384         ret = xcmalloc(size, "tripple");
1385         ret->op        = op;
1386         ret->sizes     = sizes;
1387         ret->type      = type;
1388         ret->next      = ret;
1389         ret->prev      = ret;
1390         ret->occurance = occurance;
1391         return ret;
1392 }
1393
1394 struct triple *dup_triple(struct compile_state *state, struct triple *src)
1395 {
1396         struct triple *dup;
1397         int src_lhs, src_rhs, src_size;
1398         src_lhs = TRIPLE_LHS(src->sizes);
1399         src_rhs = TRIPLE_RHS(src->sizes);
1400         src_size = TRIPLE_SIZE(src->sizes);
1401         get_occurance(src->occurance);
1402         dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
1403                 src->occurance);
1404         memcpy(dup, src, sizeof(*src));
1405         memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
1406         return dup;
1407 }
1408
1409 static struct triple *new_triple(struct compile_state *state, 
1410         int op, struct type *type, int lhs, int rhs)
1411 {
1412         struct triple *ret;
1413         struct occurance *occurance;
1414         occurance = new_occurance(state);
1415         ret = alloc_triple(state, op, type, lhs, rhs, occurance);
1416         return ret;
1417 }
1418
1419 static struct triple *build_triple(struct compile_state *state, 
1420         int op, struct type *type, struct triple *left, struct triple *right,
1421         struct occurance *occurance)
1422 {
1423         struct triple *ret;
1424         size_t count;
1425         ret = alloc_triple(state, op, type, -1, -1, occurance);
1426         count = TRIPLE_SIZE(ret->sizes);
1427         if (count > 0) {
1428                 ret->param[0] = left;
1429         }
1430         if (count > 1) {
1431                 ret->param[1] = right;
1432         }
1433         return ret;
1434 }
1435
1436 static struct triple *triple(struct compile_state *state, 
1437         int op, struct type *type, struct triple *left, struct triple *right)
1438 {
1439         struct triple *ret;
1440         size_t count;
1441         ret = new_triple(state, op, type, -1, -1);
1442         count = TRIPLE_SIZE(ret->sizes);
1443         if (count >= 1) {
1444                 ret->param[0] = left;
1445         }
1446         if (count >= 2) {
1447                 ret->param[1] = right;
1448         }
1449         return ret;
1450 }
1451
1452 static struct triple *branch(struct compile_state *state, 
1453         struct triple *targ, struct triple *test)
1454 {
1455         struct triple *ret;
1456         ret = new_triple(state, OP_BRANCH, &void_type, -1, test?1:0);
1457         if (test) {
1458                 RHS(ret, 0) = test;
1459         }
1460         TARG(ret, 0) = targ;
1461         /* record the branch target was used */
1462         if (!targ || (targ->op != OP_LABEL)) {
1463                 internal_error(state, 0, "branch not to label");
1464                 use_triple(targ, ret);
1465         }
1466         return ret;
1467 }
1468
1469
1470 static void insert_triple(struct compile_state *state,
1471         struct triple *first, struct triple *ptr)
1472 {
1473         if (ptr) {
1474                 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
1475                         internal_error(state, ptr, "expression already used");
1476                 }
1477                 ptr->next       = first;
1478                 ptr->prev       = first->prev;
1479                 ptr->prev->next = ptr;
1480                 ptr->next->prev = ptr;
1481                 if ((ptr->prev->op == OP_BRANCH) && 
1482                         TRIPLE_RHS(ptr->prev->sizes)) {
1483                         unuse_triple(first, ptr->prev);
1484                         use_triple(ptr, ptr->prev);
1485                 }
1486         }
1487 }
1488
1489 static int triple_stores_block(struct compile_state *state, struct triple *ins)
1490 {
1491         /* This function is used to determine if u.block 
1492          * is utilized to store the current block number.
1493          */
1494         int stores_block;
1495         valid_ins(state, ins);
1496         stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
1497         return stores_block;
1498 }
1499
1500 static struct block *block_of_triple(struct compile_state *state, 
1501         struct triple *ins)
1502 {
1503         struct triple *first;
1504         first = RHS(state->main_function, 0);
1505         while(ins != first && !triple_stores_block(state, ins)) {
1506                 if (ins == ins->prev) {
1507                         internal_error(state, 0, "ins == ins->prev?");
1508                 }
1509                 ins = ins->prev;
1510         }
1511         if (!triple_stores_block(state, ins)) {
1512                 internal_error(state, ins, "Cannot find block");
1513         }
1514         return ins->u.block;
1515 }
1516
1517 static struct triple *pre_triple(struct compile_state *state,
1518         struct triple *base,
1519         int op, struct type *type, struct triple *left, struct triple *right)
1520 {
1521         struct block *block;
1522         struct triple *ret;
1523         /* If I am an OP_PIECE jump to the real instruction */
1524         if (base->op == OP_PIECE) {
1525                 base = MISC(base, 0);
1526         }
1527         block = block_of_triple(state, base);
1528         get_occurance(base->occurance);
1529         ret = build_triple(state, op, type, left, right, base->occurance);
1530         if (triple_stores_block(state, ret)) {
1531                 ret->u.block = block;
1532         }
1533         insert_triple(state, base, ret);
1534         if (block->first == base) {
1535                 block->first = ret;
1536         }
1537         return ret;
1538 }
1539
1540 static struct triple *post_triple(struct compile_state *state,
1541         struct triple *base,
1542         int op, struct type *type, struct triple *left, struct triple *right)
1543 {
1544         struct block *block;
1545         struct triple *ret;
1546         int zlhs;
1547         /* If I am an OP_PIECE jump to the real instruction */
1548         if (base->op == OP_PIECE) {
1549                 base = MISC(base, 0);
1550         }
1551         /* If I have a left hand side skip over it */
1552         zlhs = TRIPLE_LHS(base->sizes);
1553         if (zlhs) {
1554                 base = LHS(base, zlhs - 1);
1555         }
1556
1557         block = block_of_triple(state, base);
1558         get_occurance(base->occurance);
1559         ret = build_triple(state, op, type, left, right, base->occurance);
1560         if (triple_stores_block(state, ret)) {
1561                 ret->u.block = block;
1562         }
1563         insert_triple(state, base->next, ret);
1564         if (block->last == base) {
1565                 block->last = ret;
1566         }
1567         return ret;
1568 }
1569
1570 static struct triple *label(struct compile_state *state)
1571 {
1572         /* Labels don't get a type */
1573         struct triple *result;
1574         result = triple(state, OP_LABEL, &void_type, 0, 0);
1575         return result;
1576 }
1577
1578 static void display_triple(FILE *fp, struct triple *ins)
1579 {
1580         struct occurance *ptr;
1581         const char *reg;
1582         char pre, post;
1583         pre = post = ' ';
1584         if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
1585                 pre = '^';
1586         }
1587         if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
1588                 post = 'v';
1589         }
1590         reg = arch_reg_str(ID_REG(ins->id));
1591         if (ins->op == OP_INTCONST) {
1592                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s <0x%08lx>         ",
1593                         ins, pre, post, reg, ins->template_id, tops(ins->op), 
1594                         ins->u.cval);
1595         }
1596         else if (ins->op == OP_ADDRCONST) {
1597                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s %-10p <0x%08lx>",
1598                         ins, pre, post, reg, ins->template_id, tops(ins->op), 
1599                         MISC(ins, 0), ins->u.cval);
1600         }
1601         else {
1602                 int i, count;
1603                 fprintf(fp, "(%p) %c%c %-7s %-2d %-10s", 
1604                         ins, pre, post, reg, ins->template_id, tops(ins->op));
1605                 count = TRIPLE_SIZE(ins->sizes);
1606                 for(i = 0; i < count; i++) {
1607                         fprintf(fp, " %-10p", ins->param[i]);
1608                 }
1609                 for(; i < 2; i++) {
1610                         fprintf(fp, "           ");
1611                 }
1612         }
1613         fprintf(fp, " @");
1614         for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
1615                 fprintf(fp, " %s,%s:%d.%d",
1616                         ptr->function, 
1617                         ptr->filename,
1618                         ptr->line, 
1619                         ptr->col);
1620         }
1621         fprintf(fp, "\n");
1622 #if 0
1623         {
1624                 struct triple_set *user;
1625                 for(user = ptr->use; user; user = user->next) {
1626                         fprintf(fp, "use: %p\n", user->member);
1627                 }
1628         }
1629 #endif
1630         fflush(fp);
1631 }
1632
1633 static int triple_is_pure(struct compile_state *state, struct triple *ins)
1634 {
1635         /* Does the triple have no side effects.
1636          * I.e. Rexecuting the triple with the same arguments 
1637          * gives the same value.
1638          */
1639         unsigned pure;
1640         valid_ins(state, ins);
1641         pure = PURE_BITS(table_ops[ins->op].flags);
1642         if ((pure != PURE) && (pure != IMPURE)) {
1643                 internal_error(state, 0, "Purity of %s not known\n",
1644                         tops(ins->op));
1645         }
1646         return pure == PURE;
1647 }
1648
1649 static int triple_is_branch(struct compile_state *state, struct triple *ins)
1650 {
1651         /* This function is used to determine which triples need
1652          * a register.
1653          */
1654         int is_branch;
1655         valid_ins(state, ins);
1656         is_branch = (table_ops[ins->op].targ != 0);
1657         return is_branch;
1658 }
1659
1660 static int triple_is_cond_branch(struct compile_state *state, struct triple *ins)
1661 {
1662         /* A conditional branch has the condition argument as a single
1663          * RHS parameter.
1664          */
1665         return triple_is_branch(state, ins) &&
1666                 (TRIPLE_RHS(ins->sizes) == 1);
1667 }
1668
1669 static int triple_is_uncond_branch(struct compile_state *state, struct triple *ins)
1670 {
1671         /* A unconditional branch has no RHS parameters.
1672          */
1673         return triple_is_branch(state, ins) &&
1674                 (TRIPLE_RHS(ins->sizes) == 0);
1675 }
1676
1677 static int triple_is_def(struct compile_state *state, struct triple *ins)
1678 {
1679         /* This function is used to determine which triples need
1680          * a register.
1681          */
1682         int is_def;
1683         valid_ins(state, ins);
1684         is_def = (table_ops[ins->op].flags & DEF) == DEF;
1685         return is_def;
1686 }
1687
1688 static struct triple **triple_iter(struct compile_state *state,
1689         size_t count, struct triple **vector,
1690         struct triple *ins, struct triple **last)
1691 {
1692         struct triple **ret;
1693         ret = 0;
1694         if (count) {
1695                 if (!last) {
1696                         ret = vector;
1697                 }
1698                 else if ((last >= vector) && (last < (vector + count - 1))) {
1699                         ret = last + 1;
1700                 }
1701         }
1702         return ret;
1703         
1704 }
1705
1706 static struct triple **triple_lhs(struct compile_state *state,
1707         struct triple *ins, struct triple **last)
1708 {
1709         return triple_iter(state, TRIPLE_LHS(ins->sizes), &LHS(ins,0), 
1710                 ins, last);
1711 }
1712
1713 static struct triple **triple_rhs(struct compile_state *state,
1714         struct triple *ins, struct triple **last)
1715 {
1716         return triple_iter(state, TRIPLE_RHS(ins->sizes), &RHS(ins,0), 
1717                 ins, last);
1718 }
1719
1720 static struct triple **triple_misc(struct compile_state *state,
1721         struct triple *ins, struct triple **last)
1722 {
1723         return triple_iter(state, TRIPLE_MISC(ins->sizes), &MISC(ins,0), 
1724                 ins, last);
1725 }
1726
1727 static struct triple **triple_targ(struct compile_state *state,
1728         struct triple *ins, struct triple **last)
1729 {
1730         size_t count;
1731         struct triple **ret, **vector;
1732         ret = 0;
1733         count = TRIPLE_TARG(ins->sizes);
1734         vector = &TARG(ins, 0);
1735         if (count) {
1736                 if (!last) {
1737                         ret = vector;
1738                 }
1739                 else if ((last >= vector) && (last < (vector + count - 1))) {
1740                         ret = last + 1;
1741                 }
1742                 else if ((last == (vector + count - 1)) && 
1743                         TRIPLE_RHS(ins->sizes)) {
1744                         ret = &ins->next;
1745                 }
1746         }
1747         return ret;
1748 }
1749
1750
1751 static void verify_use(struct compile_state *state,
1752         struct triple *user, struct triple *used)
1753 {
1754         int size, i;
1755         size = TRIPLE_SIZE(user->sizes);
1756         for(i = 0; i < size; i++) {
1757                 if (user->param[i] == used) {
1758                         break;
1759                 }
1760         }
1761         if (triple_is_branch(state, user)) {
1762                 if (user->next == used) {
1763                         i = -1;
1764                 }
1765         }
1766         if (i == size) {
1767                 internal_error(state, user, "%s(%p) does not use %s(%p)",
1768                         tops(user->op), user, tops(used->op), used);
1769         }
1770 }
1771
1772 static int find_rhs_use(struct compile_state *state, 
1773         struct triple *user, struct triple *used)
1774 {
1775         struct triple **param;
1776         int size, i;
1777         verify_use(state, user, used);
1778         size = TRIPLE_RHS(user->sizes);
1779         param = &RHS(user, 0);
1780         for(i = 0; i < size; i++) {
1781                 if (param[i] == used) {
1782                         return i;
1783                 }
1784         }
1785         return -1;
1786 }
1787
1788 static void free_triple(struct compile_state *state, struct triple *ptr)
1789 {
1790         size_t size;
1791         size = sizeof(*ptr) - sizeof(ptr->param) +
1792                 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr->sizes));
1793         ptr->prev->next = ptr->next;
1794         ptr->next->prev = ptr->prev;
1795         if (ptr->use) {
1796                 internal_error(state, ptr, "ptr->use != 0");
1797         }
1798         put_occurance(ptr->occurance);
1799         memset(ptr, -1, size);
1800         xfree(ptr);
1801 }
1802
1803 static void release_triple(struct compile_state *state, struct triple *ptr)
1804 {
1805         struct triple_set *set, *next;
1806         struct triple **expr;
1807         struct block *block;
1808         /* Make certain the we are not the first or last element of a block */
1809         block = block_of_triple(state, ptr);
1810         if (block && (block->last == ptr)) {
1811                 block->last = ptr->prev;
1812         }
1813         if (block && (block->first == ptr)) {
1814                 block->first = ptr->next;
1815         }
1816         /* Remove ptr from use chains where it is the user */
1817         expr = triple_rhs(state, ptr, 0);
1818         for(; expr; expr = triple_rhs(state, ptr, expr)) {
1819                 if (*expr) {
1820                         unuse_triple(*expr, ptr);
1821                 }
1822         }
1823         expr = triple_lhs(state, ptr, 0);
1824         for(; expr; expr = triple_lhs(state, ptr, expr)) {
1825                 if (*expr) {
1826                         unuse_triple(*expr, ptr);
1827                 }
1828         }
1829         expr = triple_misc(state, ptr, 0);
1830         for(; expr; expr = triple_misc(state, ptr, expr)) {
1831                 if (*expr) {
1832                         unuse_triple(*expr, ptr);
1833                 }
1834         }
1835         expr = triple_targ(state, ptr, 0);
1836         for(; expr; expr = triple_targ(state, ptr, expr)) {
1837                 if (*expr) {
1838                         unuse_triple(*expr, ptr);
1839                 }
1840         }
1841         /* Reomve ptr from use chains where it is used */
1842         for(set = ptr->use; set; set = next) {
1843                 next = set->next;
1844                 expr = triple_rhs(state, set->member, 0);
1845                 for(; expr; expr = triple_rhs(state, set->member, expr)) {
1846                         if (*expr == ptr) {
1847                                 *expr = &zero_triple;
1848                         }
1849                 }
1850                 expr = triple_lhs(state, set->member, 0);
1851                 for(; expr; expr = triple_lhs(state, set->member, expr)) {
1852                         if (*expr == ptr) {
1853                                 *expr = &zero_triple;
1854                         }
1855                 }
1856                 expr = triple_misc(state, set->member, 0);
1857                 for(; expr; expr = triple_misc(state, set->member, expr)) {
1858                         if (*expr == ptr) {
1859                                 *expr = &zero_triple;
1860                         }
1861                 }
1862                 expr = triple_targ(state, set->member, 0);
1863                 for(; expr; expr = triple_targ(state, set->member, expr)) {
1864                         if (*expr == ptr) {
1865                                 *expr = &zero_triple;
1866                         }
1867                 }
1868                 unuse_triple(ptr, set->member);
1869         }
1870         free_triple(state, ptr);
1871 }
1872
1873 static void print_triple(struct compile_state *state, struct triple *ptr);
1874
1875 #define TOK_UNKNOWN     0
1876 #define TOK_SPACE       1
1877 #define TOK_SEMI        2
1878 #define TOK_LBRACE      3
1879 #define TOK_RBRACE      4
1880 #define TOK_COMMA       5
1881 #define TOK_EQ          6
1882 #define TOK_COLON       7
1883 #define TOK_LBRACKET    8
1884 #define TOK_RBRACKET    9
1885 #define TOK_LPAREN      10
1886 #define TOK_RPAREN      11
1887 #define TOK_STAR        12
1888 #define TOK_DOTS        13
1889 #define TOK_MORE        14
1890 #define TOK_LESS        15
1891 #define TOK_TIMESEQ     16
1892 #define TOK_DIVEQ       17
1893 #define TOK_MODEQ       18
1894 #define TOK_PLUSEQ      19
1895 #define TOK_MINUSEQ     20
1896 #define TOK_SLEQ        21
1897 #define TOK_SREQ        22
1898 #define TOK_ANDEQ       23
1899 #define TOK_XOREQ       24
1900 #define TOK_OREQ        25
1901 #define TOK_EQEQ        26
1902 #define TOK_NOTEQ       27
1903 #define TOK_QUEST       28
1904 #define TOK_LOGOR       29
1905 #define TOK_LOGAND      30
1906 #define TOK_OR          31
1907 #define TOK_AND         32
1908 #define TOK_XOR         33
1909 #define TOK_LESSEQ      34
1910 #define TOK_MOREEQ      35
1911 #define TOK_SL          36
1912 #define TOK_SR          37
1913 #define TOK_PLUS        38
1914 #define TOK_MINUS       39
1915 #define TOK_DIV         40
1916 #define TOK_MOD         41
1917 #define TOK_PLUSPLUS    42
1918 #define TOK_MINUSMINUS  43
1919 #define TOK_BANG        44
1920 #define TOK_ARROW       45
1921 #define TOK_DOT         46
1922 #define TOK_TILDE       47
1923 #define TOK_LIT_STRING  48
1924 #define TOK_LIT_CHAR    49
1925 #define TOK_LIT_INT     50
1926 #define TOK_LIT_FLOAT   51
1927 #define TOK_MACRO       52
1928 #define TOK_CONCATENATE 53
1929
1930 #define TOK_IDENT       54
1931 #define TOK_STRUCT_NAME 55
1932 #define TOK_ENUM_CONST  56
1933 #define TOK_TYPE_NAME   57
1934
1935 #define TOK_AUTO        58
1936 #define TOK_BREAK       59
1937 #define TOK_CASE        60
1938 #define TOK_CHAR        61
1939 #define TOK_CONST       62
1940 #define TOK_CONTINUE    63
1941 #define TOK_DEFAULT     64
1942 #define TOK_DO          65
1943 #define TOK_DOUBLE      66
1944 #define TOK_ELSE        67
1945 #define TOK_ENUM        68
1946 #define TOK_EXTERN      69
1947 #define TOK_FLOAT       70
1948 #define TOK_FOR         71
1949 #define TOK_GOTO        72
1950 #define TOK_IF          73
1951 #define TOK_INLINE      74
1952 #define TOK_INT         75
1953 #define TOK_LONG        76
1954 #define TOK_REGISTER    77
1955 #define TOK_RESTRICT    78
1956 #define TOK_RETURN      79
1957 #define TOK_SHORT       80
1958 #define TOK_SIGNED      81
1959 #define TOK_SIZEOF      82
1960 #define TOK_STATIC      83
1961 #define TOK_STRUCT      84
1962 #define TOK_SWITCH      85
1963 #define TOK_TYPEDEF     86
1964 #define TOK_UNION       87
1965 #define TOK_UNSIGNED    88
1966 #define TOK_VOID        89
1967 #define TOK_VOLATILE    90
1968 #define TOK_WHILE       91
1969 #define TOK_ASM         92
1970 #define TOK_ATTRIBUTE   93
1971 #define TOK_ALIGNOF     94
1972 #define TOK_FIRST_KEYWORD TOK_AUTO
1973 #define TOK_LAST_KEYWORD  TOK_ALIGNOF
1974
1975 #define TOK_DEFINE      100
1976 #define TOK_UNDEF       101
1977 #define TOK_INCLUDE     102
1978 #define TOK_LINE        103
1979 #define TOK_ERROR       104
1980 #define TOK_WARNING     105
1981 #define TOK_PRAGMA      106
1982 #define TOK_IFDEF       107
1983 #define TOK_IFNDEF      108
1984 #define TOK_ELIF        109
1985 #define TOK_ENDIF       110
1986
1987 #define TOK_FIRST_MACRO TOK_DEFINE
1988 #define TOK_LAST_MACRO  TOK_ENDIF
1989          
1990 #define TOK_EOF         111
1991
1992 static const char *tokens[] = {
1993 [TOK_UNKNOWN     ] = "unknown",
1994 [TOK_SPACE       ] = ":space:",
1995 [TOK_SEMI        ] = ";",
1996 [TOK_LBRACE      ] = "{",
1997 [TOK_RBRACE      ] = "}",
1998 [TOK_COMMA       ] = ",",
1999 [TOK_EQ          ] = "=",
2000 [TOK_COLON       ] = ":",
2001 [TOK_LBRACKET    ] = "[",
2002 [TOK_RBRACKET    ] = "]",
2003 [TOK_LPAREN      ] = "(",
2004 [TOK_RPAREN      ] = ")",
2005 [TOK_STAR        ] = "*",
2006 [TOK_DOTS        ] = "...",
2007 [TOK_MORE        ] = ">",
2008 [TOK_LESS        ] = "<",
2009 [TOK_TIMESEQ     ] = "*=",
2010 [TOK_DIVEQ       ] = "/=",
2011 [TOK_MODEQ       ] = "%=",
2012 [TOK_PLUSEQ      ] = "+=",
2013 [TOK_MINUSEQ     ] = "-=",
2014 [TOK_SLEQ        ] = "<<=",
2015 [TOK_SREQ        ] = ">>=",
2016 [TOK_ANDEQ       ] = "&=",
2017 [TOK_XOREQ       ] = "^=",
2018 [TOK_OREQ        ] = "|=",
2019 [TOK_EQEQ        ] = "==",
2020 [TOK_NOTEQ       ] = "!=",
2021 [TOK_QUEST       ] = "?",
2022 [TOK_LOGOR       ] = "||",
2023 [TOK_LOGAND      ] = "&&",
2024 [TOK_OR          ] = "|",
2025 [TOK_AND         ] = "&",
2026 [TOK_XOR         ] = "^",
2027 [TOK_LESSEQ      ] = "<=",
2028 [TOK_MOREEQ      ] = ">=",
2029 [TOK_SL          ] = "<<",
2030 [TOK_SR          ] = ">>",
2031 [TOK_PLUS        ] = "+",
2032 [TOK_MINUS       ] = "-",
2033 [TOK_DIV         ] = "/",
2034 [TOK_MOD         ] = "%",
2035 [TOK_PLUSPLUS    ] = "++",
2036 [TOK_MINUSMINUS  ] = "--",
2037 [TOK_BANG        ] = "!",
2038 [TOK_ARROW       ] = "->",
2039 [TOK_DOT         ] = ".",
2040 [TOK_TILDE       ] = "~",
2041 [TOK_LIT_STRING  ] = ":string:",
2042 [TOK_IDENT       ] = ":ident:",
2043 [TOK_TYPE_NAME   ] = ":typename:",
2044 [TOK_LIT_CHAR    ] = ":char:",
2045 [TOK_LIT_INT     ] = ":integer:",
2046 [TOK_LIT_FLOAT   ] = ":float:",
2047 [TOK_MACRO       ] = "#",
2048 [TOK_CONCATENATE ] = "##",
2049
2050 [TOK_AUTO        ] = "auto",
2051 [TOK_BREAK       ] = "break",
2052 [TOK_CASE        ] = "case",
2053 [TOK_CHAR        ] = "char",
2054 [TOK_CONST       ] = "const",
2055 [TOK_CONTINUE    ] = "continue",
2056 [TOK_DEFAULT     ] = "default",
2057 [TOK_DO          ] = "do",
2058 [TOK_DOUBLE      ] = "double",
2059 [TOK_ELSE        ] = "else",
2060 [TOK_ENUM        ] = "enum",
2061 [TOK_EXTERN      ] = "extern",
2062 [TOK_FLOAT       ] = "float",
2063 [TOK_FOR         ] = "for",
2064 [TOK_GOTO        ] = "goto",
2065 [TOK_IF          ] = "if",
2066 [TOK_INLINE      ] = "inline",
2067 [TOK_INT         ] = "int",
2068 [TOK_LONG        ] = "long",
2069 [TOK_REGISTER    ] = "register",
2070 [TOK_RESTRICT    ] = "restrict",
2071 [TOK_RETURN      ] = "return",
2072 [TOK_SHORT       ] = "short",
2073 [TOK_SIGNED      ] = "signed",
2074 [TOK_SIZEOF      ] = "sizeof",
2075 [TOK_STATIC      ] = "static",
2076 [TOK_STRUCT      ] = "struct",
2077 [TOK_SWITCH      ] = "switch",
2078 [TOK_TYPEDEF     ] = "typedef",
2079 [TOK_UNION       ] = "union",
2080 [TOK_UNSIGNED    ] = "unsigned",
2081 [TOK_VOID        ] = "void",
2082 [TOK_VOLATILE    ] = "volatile",
2083 [TOK_WHILE       ] = "while",
2084 [TOK_ASM         ] = "asm",
2085 [TOK_ATTRIBUTE   ] = "__attribute__",
2086 [TOK_ALIGNOF     ] = "__alignof__",
2087
2088 [TOK_DEFINE      ] = "define",
2089 [TOK_UNDEF       ] = "undef",
2090 [TOK_INCLUDE     ] = "include",
2091 [TOK_LINE        ] = "line",
2092 [TOK_ERROR       ] = "error",
2093 [TOK_WARNING     ] = "warning",
2094 [TOK_PRAGMA      ] = "pragma",
2095 [TOK_IFDEF       ] = "ifdef",
2096 [TOK_IFNDEF      ] = "ifndef",
2097 [TOK_ELIF        ] = "elif",
2098 [TOK_ENDIF       ] = "endif",
2099
2100 [TOK_EOF         ] = "EOF",
2101 };
2102
2103 static unsigned int hash(const char *str, int str_len)
2104 {
2105         unsigned int hash;
2106         const char *end;
2107         end = str + str_len;
2108         hash = 0;
2109         for(; str < end; str++) {
2110                 hash = (hash *263) + *str;
2111         }
2112         hash = hash & (HASH_TABLE_SIZE -1);
2113         return hash;
2114 }
2115
2116 static struct hash_entry *lookup(
2117         struct compile_state *state, const char *name, int name_len)
2118 {
2119         struct hash_entry *entry;
2120         unsigned int index;
2121         index = hash(name, name_len);
2122         entry = state->hash_table[index];
2123         while(entry && 
2124                 ((entry->name_len != name_len) ||
2125                         (memcmp(entry->name, name, name_len) != 0))) {
2126                 entry = entry->next;
2127         }
2128         if (!entry) {
2129                 char *new_name;
2130                 /* Get a private copy of the name */
2131                 new_name = xmalloc(name_len + 1, "hash_name");
2132                 memcpy(new_name, name, name_len);
2133                 new_name[name_len] = '\0';
2134
2135                 /* Create a new hash entry */
2136                 entry = xcmalloc(sizeof(*entry), "hash_entry");
2137                 entry->next = state->hash_table[index];
2138                 entry->name = new_name;
2139                 entry->name_len = name_len;
2140
2141                 /* Place the new entry in the hash table */
2142                 state->hash_table[index] = entry;
2143         }
2144         return entry;
2145 }
2146
2147 static void ident_to_keyword(struct compile_state *state, struct token *tk)
2148 {
2149         struct hash_entry *entry;
2150         entry = tk->ident;
2151         if (entry && ((entry->tok == TOK_TYPE_NAME) ||
2152                 (entry->tok == TOK_ENUM_CONST) ||
2153                 ((entry->tok >= TOK_FIRST_KEYWORD) && 
2154                         (entry->tok <= TOK_LAST_KEYWORD)))) {
2155                 tk->tok = entry->tok;
2156         }
2157 }
2158
2159 static void ident_to_macro(struct compile_state *state, struct token *tk)
2160 {
2161         struct hash_entry *entry;
2162         entry = tk->ident;
2163         if (entry && 
2164                 (entry->tok >= TOK_FIRST_MACRO) &&
2165                 (entry->tok <= TOK_LAST_MACRO)) {
2166                 tk->tok = entry->tok;
2167         }
2168 }
2169
2170 static void hash_keyword(
2171         struct compile_state *state, const char *keyword, int tok)
2172 {
2173         struct hash_entry *entry;
2174         entry = lookup(state, keyword, strlen(keyword));
2175         if (entry && entry->tok != TOK_UNKNOWN) {
2176                 die("keyword %s already hashed", keyword);
2177         }
2178         entry->tok  = tok;
2179 }
2180
2181 static void symbol(
2182         struct compile_state *state, struct hash_entry *ident,
2183         struct symbol **chain, struct triple *def, struct type *type)
2184 {
2185         struct symbol *sym;
2186         if (*chain && ((*chain)->scope_depth == state->scope_depth)) {
2187                 error(state, 0, "%s already defined", ident->name);
2188         }
2189         sym = xcmalloc(sizeof(*sym), "symbol");
2190         sym->ident = ident;
2191         sym->def   = def;
2192         sym->type  = type;
2193         sym->scope_depth = state->scope_depth;
2194         sym->next = *chain;
2195         *chain    = sym;
2196 }
2197
2198 static void label_symbol(struct compile_state *state, 
2199         struct hash_entry *ident, struct triple *label)
2200 {
2201         struct symbol *sym;
2202         if (ident->sym_label) {
2203                 error(state, 0, "label %s already defined", ident->name);
2204         }
2205         sym = xcmalloc(sizeof(*sym), "label");
2206         sym->ident = ident;
2207         sym->def   = label;
2208         sym->type  = &void_type;
2209         sym->scope_depth = FUNCTION_SCOPE_DEPTH;
2210         sym->next  = 0;
2211         ident->sym_label = sym;
2212 }
2213
2214 static void start_scope(struct compile_state *state)
2215 {
2216         state->scope_depth++;
2217 }
2218
2219 static void end_scope_syms(struct symbol **chain, int depth)
2220 {
2221         struct symbol *sym, *next;
2222         sym = *chain;
2223         while(sym && (sym->scope_depth == depth)) {
2224                 next = sym->next;
2225                 xfree(sym);
2226                 sym = next;
2227         }
2228         *chain = sym;
2229 }
2230
2231 static void end_scope(struct compile_state *state)
2232 {
2233         int i;
2234         int depth;
2235         /* Walk through the hash table and remove all symbols
2236          * in the current scope. 
2237          */
2238         depth = state->scope_depth;
2239         for(i = 0; i < HASH_TABLE_SIZE; i++) {
2240                 struct hash_entry *entry;
2241                 entry = state->hash_table[i];
2242                 while(entry) {
2243                         end_scope_syms(&entry->sym_label,  depth);
2244                         end_scope_syms(&entry->sym_struct, depth);
2245                         end_scope_syms(&entry->sym_ident,  depth);
2246                         entry = entry->next;
2247                 }
2248         }
2249         state->scope_depth = depth - 1;
2250 }
2251
2252 static void register_keywords(struct compile_state *state)
2253 {
2254         hash_keyword(state, "auto",          TOK_AUTO);
2255         hash_keyword(state, "break",         TOK_BREAK);
2256         hash_keyword(state, "case",          TOK_CASE);
2257         hash_keyword(state, "char",          TOK_CHAR);
2258         hash_keyword(state, "const",         TOK_CONST);
2259         hash_keyword(state, "continue",      TOK_CONTINUE);
2260         hash_keyword(state, "default",       TOK_DEFAULT);
2261         hash_keyword(state, "do",            TOK_DO);
2262         hash_keyword(state, "double",        TOK_DOUBLE);
2263         hash_keyword(state, "else",          TOK_ELSE);
2264         hash_keyword(state, "enum",          TOK_ENUM);
2265         hash_keyword(state, "extern",        TOK_EXTERN);
2266         hash_keyword(state, "float",         TOK_FLOAT);
2267         hash_keyword(state, "for",           TOK_FOR);
2268         hash_keyword(state, "goto",          TOK_GOTO);
2269         hash_keyword(state, "if",            TOK_IF);
2270         hash_keyword(state, "inline",        TOK_INLINE);
2271         hash_keyword(state, "int",           TOK_INT);
2272         hash_keyword(state, "long",          TOK_LONG);
2273         hash_keyword(state, "register",      TOK_REGISTER);
2274         hash_keyword(state, "restrict",      TOK_RESTRICT);
2275         hash_keyword(state, "return",        TOK_RETURN);
2276         hash_keyword(state, "short",         TOK_SHORT);
2277         hash_keyword(state, "signed",        TOK_SIGNED);
2278         hash_keyword(state, "sizeof",        TOK_SIZEOF);
2279         hash_keyword(state, "static",        TOK_STATIC);
2280         hash_keyword(state, "struct",        TOK_STRUCT);
2281         hash_keyword(state, "switch",        TOK_SWITCH);
2282         hash_keyword(state, "typedef",       TOK_TYPEDEF);
2283         hash_keyword(state, "union",         TOK_UNION);
2284         hash_keyword(state, "unsigned",      TOK_UNSIGNED);
2285         hash_keyword(state, "void",          TOK_VOID);
2286         hash_keyword(state, "volatile",      TOK_VOLATILE);
2287         hash_keyword(state, "__volatile__",  TOK_VOLATILE);
2288         hash_keyword(state, "while",         TOK_WHILE);
2289         hash_keyword(state, "asm",           TOK_ASM);
2290         hash_keyword(state, "__asm__",       TOK_ASM);
2291         hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
2292         hash_keyword(state, "__alignof__",   TOK_ALIGNOF);
2293 }
2294
2295 static void register_macro_keywords(struct compile_state *state)
2296 {
2297         hash_keyword(state, "define",        TOK_DEFINE);
2298         hash_keyword(state, "undef",         TOK_UNDEF);
2299         hash_keyword(state, "include",       TOK_INCLUDE);
2300         hash_keyword(state, "line",          TOK_LINE);
2301         hash_keyword(state, "error",         TOK_ERROR);
2302         hash_keyword(state, "warning",       TOK_WARNING);
2303         hash_keyword(state, "pragma",        TOK_PRAGMA);
2304         hash_keyword(state, "ifdef",         TOK_IFDEF);
2305         hash_keyword(state, "ifndef",        TOK_IFNDEF);
2306         hash_keyword(state, "elif",          TOK_ELIF);
2307         hash_keyword(state, "endif",         TOK_ENDIF);
2308 }
2309
2310 static int spacep(int c)
2311 {
2312         int ret = 0;
2313         switch(c) {
2314         case ' ':
2315         case '\t':
2316         case '\f':
2317         case '\v':
2318         case '\r':
2319         case '\n':
2320                 ret = 1;
2321                 break;
2322         }
2323         return ret;
2324 }
2325
2326 static int digitp(int c)
2327 {
2328         int ret = 0;
2329         switch(c) {
2330         case '0': case '1': case '2': case '3': case '4': 
2331         case '5': case '6': case '7': case '8': case '9':
2332                 ret = 1;
2333                 break;
2334         }
2335         return ret;
2336 }
2337 static int digval(int c)
2338 {
2339         int val = -1;
2340         if ((c >= '0') && (c <= '9')) {
2341                 val = c - '0';
2342         }
2343         return val;
2344 }
2345
2346 static int hexdigitp(int c)
2347 {
2348         int ret = 0;
2349         switch(c) {
2350         case '0': case '1': case '2': case '3': case '4': 
2351         case '5': case '6': case '7': case '8': case '9':
2352         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2353         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2354                 ret = 1;
2355                 break;
2356         }
2357         return ret;
2358 }
2359 static int hexdigval(int c) 
2360 {
2361         int val = -1;
2362         if ((c >= '0') && (c <= '9')) {
2363                 val = c - '0';
2364         }
2365         else if ((c >= 'A') && (c <= 'F')) {
2366                 val = 10 + (c - 'A');
2367         }
2368         else if ((c >= 'a') && (c <= 'f')) {
2369                 val = 10 + (c - 'a');
2370         }
2371         return val;
2372 }
2373
2374 static int octdigitp(int c)
2375 {
2376         int ret = 0;
2377         switch(c) {
2378         case '0': case '1': case '2': case '3': 
2379         case '4': case '5': case '6': case '7':
2380                 ret = 1;
2381                 break;
2382         }
2383         return ret;
2384 }
2385 static int octdigval(int c)
2386 {
2387         int val = -1;
2388         if ((c >= '0') && (c <= '7')) {
2389                 val = c - '0';
2390         }
2391         return val;
2392 }
2393
2394 static int letterp(int c)
2395 {
2396         int ret = 0;
2397         switch(c) {
2398         case 'a': case 'b': case 'c': case 'd': case 'e':
2399         case 'f': case 'g': case 'h': case 'i': case 'j':
2400         case 'k': case 'l': case 'm': case 'n': case 'o':
2401         case 'p': case 'q': case 'r': case 's': case 't':
2402         case 'u': case 'v': case 'w': case 'x': case 'y':
2403         case 'z':
2404         case 'A': case 'B': case 'C': case 'D': case 'E':
2405         case 'F': case 'G': case 'H': case 'I': case 'J':
2406         case 'K': case 'L': case 'M': case 'N': case 'O':
2407         case 'P': case 'Q': case 'R': case 'S': case 'T':
2408         case 'U': case 'V': case 'W': case 'X': case 'Y':
2409         case 'Z':
2410         case '_':
2411                 ret = 1;
2412                 break;
2413         }
2414         return ret;
2415 }
2416
2417 static int char_value(struct compile_state *state,
2418         const signed char **strp, const signed char *end)
2419 {
2420         const signed char *str;
2421         int c;
2422         str = *strp;
2423         c = *str++;
2424         if ((c == '\\') && (str < end)) {
2425                 switch(*str) {
2426                 case 'n':  c = '\n'; str++; break;
2427                 case 't':  c = '\t'; str++; break;
2428                 case 'v':  c = '\v'; str++; break;
2429                 case 'b':  c = '\b'; str++; break;
2430                 case 'r':  c = '\r'; str++; break;
2431                 case 'f':  c = '\f'; str++; break;
2432                 case 'a':  c = '\a'; str++; break;
2433                 case '\\': c = '\\'; str++; break;
2434                 case '?':  c = '?';  str++; break;
2435                 case '\'': c = '\''; str++; break;
2436                 case '"':  c = '"';  break;
2437                 case 'x': 
2438                         c = 0;
2439                         str++;
2440                         while((str < end) && hexdigitp(*str)) {
2441                                 c <<= 4;
2442                                 c += hexdigval(*str);
2443                                 str++;
2444                         }
2445                         break;
2446                 case '0': case '1': case '2': case '3': 
2447                 case '4': case '5': case '6': case '7':
2448                         c = 0;
2449                         while((str < end) && octdigitp(*str)) {
2450                                 c <<= 3;
2451                                 c += octdigval(*str);
2452                                 str++;
2453                         }
2454                         break;
2455                 default:
2456                         error(state, 0, "Invalid character constant");
2457                         break;
2458                 }
2459         }
2460         *strp = str;
2461         return c;
2462 }
2463
2464 static char *after_digits(char *ptr, char *end)
2465 {
2466         while((ptr < end) && digitp(*ptr)) {
2467                 ptr++;
2468         }
2469         return ptr;
2470 }
2471
2472 static char *after_octdigits(char *ptr, char *end)
2473 {
2474         while((ptr < end) && octdigitp(*ptr)) {
2475                 ptr++;
2476         }
2477         return ptr;
2478 }
2479
2480 static char *after_hexdigits(char *ptr, char *end)
2481 {
2482         while((ptr < end) && hexdigitp(*ptr)) {
2483                 ptr++;
2484         }
2485         return ptr;
2486 }
2487
2488 static void save_string(struct compile_state *state, 
2489         struct token *tk, char *start, char *end, const char *id)
2490 {
2491         char *str;
2492         int str_len;
2493         /* Create a private copy of the string */
2494         str_len = end - start + 1;
2495         str = xmalloc(str_len + 1, id);
2496         memcpy(str, start, str_len);
2497         str[str_len] = '\0';
2498
2499         /* Store the copy in the token */
2500         tk->val.str = str;
2501         tk->str_len = str_len;
2502 }
2503 static void next_token(struct compile_state *state, int index)
2504 {
2505         struct file_state *file;
2506         struct token *tk;
2507         char *token;
2508         int c, c1, c2, c3;
2509         char *tokp, *end;
2510         int tok;
2511 next_token:
2512         file = state->file;
2513         tk = &state->token[index];
2514         tk->str_len = 0;
2515         tk->ident = 0;
2516         token = tokp = file->pos;
2517         end = file->buf + file->size;
2518         tok = TOK_UNKNOWN;
2519         c = -1;
2520         if (tokp < end) {
2521                 c = *tokp;
2522         }
2523         c1 = -1;
2524         if ((tokp + 1) < end) {
2525                 c1 = tokp[1];
2526         }
2527         c2 = -1;
2528         if ((tokp + 2) < end) {
2529                 c2 = tokp[2];
2530         }
2531         c3 = -1;
2532         if ((tokp + 3) < end) {
2533                 c3 = tokp[3];
2534         }
2535         if (tokp >= end) {
2536                 tok = TOK_EOF;
2537                 tokp = end;
2538         }
2539         /* Whitespace */
2540         else if (spacep(c)) {
2541                 tok = TOK_SPACE;
2542                 while ((tokp < end) && spacep(c)) {
2543                         if (c == '\n') {
2544                                 file->line++;
2545                                 file->report_line++;
2546                                 file->line_start = tokp + 1;
2547                         }
2548                         c = *(++tokp);
2549                 }
2550                 if (!spacep(c)) {
2551                         tokp--;
2552                 }
2553         }
2554         /* EOL Comments */
2555         else if ((c == '/') && (c1 == '/')) {
2556                 tok = TOK_SPACE;
2557                 for(tokp += 2; tokp < end; tokp++) {
2558                         c = *tokp;
2559                         if (c == '\n') {
2560                                 file->line++;
2561                                 file->report_line++;
2562                                 file->line_start = tokp +1;
2563                                 break;
2564                         }
2565                 }
2566         }
2567         /* Comments */
2568         else if ((c == '/') && (c1 == '*')) {
2569                 int line;
2570                 char *line_start;
2571                 line = file->line;
2572                 line_start = file->line_start;
2573                 for(tokp += 2; (end - tokp) >= 2; tokp++) {
2574                         c = *tokp;
2575                         if (c == '\n') {
2576                                 line++;
2577                                 line_start = tokp +1;
2578                         }
2579                         else if ((c == '*') && (tokp[1] == '/')) {
2580                                 tok = TOK_SPACE;
2581                                 tokp += 1;
2582                                 break;
2583                         }
2584                 }
2585                 if (tok == TOK_UNKNOWN) {
2586                         error(state, 0, "unterminated comment");
2587                 }
2588                 file->report_line += line - file->line;
2589                 file->line = line;
2590                 file->line_start = line_start;
2591         }
2592         /* string constants */
2593         else if ((c == '"') ||
2594                 ((c == 'L') && (c1 == '"'))) {
2595                 int line;
2596                 char *line_start;
2597                 int wchar;
2598                 line = file->line;
2599                 line_start = file->line_start;
2600                 wchar = 0;
2601                 if (c == 'L') {
2602                         wchar = 1;
2603                         tokp++;
2604                 }
2605                 for(tokp += 1; tokp < end; tokp++) {
2606                         c = *tokp;
2607                         if (c == '\n') {
2608                                 line++;
2609                                 line_start = tokp + 1;
2610                         }
2611                         else if ((c == '\\') && (tokp +1 < end)) {
2612                                 tokp++;
2613                         }
2614                         else if (c == '"') {
2615                                 tok = TOK_LIT_STRING;
2616                                 break;
2617                         }
2618                 }
2619                 if (tok == TOK_UNKNOWN) {
2620                         error(state, 0, "unterminated string constant");
2621                 }
2622                 if (line != file->line) {
2623                         warning(state, 0, "multiline string constant");
2624                 }
2625                 file->report_line += line - file->line;
2626                 file->line = line;
2627                 file->line_start = line_start;
2628
2629                 /* Save the string value */
2630                 save_string(state, tk, token, tokp, "literal string");
2631         }
2632         /* character constants */
2633         else if ((c == '\'') ||
2634                 ((c == 'L') && (c1 == '\''))) {
2635                 int line;
2636                 char *line_start;
2637                 int wchar;
2638                 line = file->line;
2639                 line_start = file->line_start;
2640                 wchar = 0;
2641                 if (c == 'L') {
2642                         wchar = 1;
2643                         tokp++;
2644                 }
2645                 for(tokp += 1; tokp < end; tokp++) {
2646                         c = *tokp;
2647                         if (c == '\n') {
2648                                 line++;
2649                                 line_start = tokp + 1;
2650                         }
2651                         else if ((c == '\\') && (tokp +1 < end)) {
2652                                 tokp++;
2653                         }
2654                         else if (c == '\'') {
2655                                 tok = TOK_LIT_CHAR;
2656                                 break;
2657                         }
2658                 }
2659                 if (tok == TOK_UNKNOWN) {
2660                         error(state, 0, "unterminated character constant");
2661                 }
2662                 if (line != file->line) {
2663                         warning(state, 0, "multiline character constant");
2664                 }
2665                 file->report_line += line - file->line;
2666                 file->line = line;
2667                 file->line_start = line_start;
2668
2669                 /* Save the character value */
2670                 save_string(state, tk, token, tokp, "literal character");
2671         }
2672         /* integer and floating constants 
2673          * Integer Constants
2674          * {digits}
2675          * 0[Xx]{hexdigits}
2676          * 0{octdigit}+
2677          * 
2678          * Floating constants
2679          * {digits}.{digits}[Ee][+-]?{digits}
2680          * {digits}.{digits}
2681          * {digits}[Ee][+-]?{digits}
2682          * .{digits}[Ee][+-]?{digits}
2683          * .{digits}
2684          */
2685         
2686         else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
2687                 char *next, *new;
2688                 int is_float;
2689                 is_float = 0;
2690                 if (c != '.') {
2691                         next = after_digits(tokp, end);
2692                 }
2693                 else {
2694                         next = tokp;
2695                 }
2696                 if (next[0] == '.') {
2697                         new = after_digits(next, end);
2698                         is_float = (new != next);
2699                         next = new;
2700                 }
2701                 if ((next[0] == 'e') || (next[0] == 'E')) {
2702                         if (((next + 1) < end) && 
2703                                 ((next[1] == '+') || (next[1] == '-'))) {
2704                                 next++;
2705                         }
2706                         new = after_digits(next, end);
2707                         is_float = (new != next);
2708                         next = new;
2709                 }
2710                 if (is_float) {
2711                         tok = TOK_LIT_FLOAT;
2712                         if ((next < end) && (
2713                                 (next[0] == 'f') ||
2714                                 (next[0] == 'F') ||
2715                                 (next[0] == 'l') ||
2716                                 (next[0] == 'L'))
2717                                 ) {
2718                                 next++;
2719                         }
2720                 }
2721                 if (!is_float && digitp(c)) {
2722                         tok = TOK_LIT_INT;
2723                         if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
2724                                 next = after_hexdigits(tokp + 2, end);
2725                         }
2726                         else if (c == '0') {
2727                                 next = after_octdigits(tokp, end);
2728                         }
2729                         else {
2730                                 next = after_digits(tokp, end);
2731                         }
2732                         /* crazy integer suffixes */
2733                         if ((next < end) && 
2734                                 ((next[0] == 'u') || (next[0] == 'U'))) { 
2735                                 next++;
2736                                 if ((next < end) &&
2737                                         ((next[0] == 'l') || (next[0] == 'L'))) {
2738                                         next++;
2739                                 }
2740                         }
2741                         else if ((next < end) &&
2742                                 ((next[0] == 'l') || (next[0] == 'L'))) {
2743                                 next++;
2744                                 if ((next < end) && 
2745                                         ((next[0] == 'u') || (next[0] == 'U'))) { 
2746                                         next++;
2747                                 }
2748                         }
2749                 }
2750                 tokp = next - 1;
2751
2752                 /* Save the integer/floating point value */
2753                 save_string(state, tk, token, tokp, "literal number");
2754         }
2755         /* identifiers */
2756         else if (letterp(c)) {
2757                 tok = TOK_IDENT;
2758                 for(tokp += 1; tokp < end; tokp++) {
2759                         c = *tokp;
2760                         if (!letterp(c) && !digitp(c)) {
2761                                 break;
2762                         }
2763                 }
2764                 tokp -= 1;
2765                 tk->ident = lookup(state, token, tokp +1 - token);
2766         }
2767         /* C99 alternate macro characters */
2768         else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { 
2769                 tokp += 3; 
2770                 tok = TOK_CONCATENATE; 
2771         }
2772         else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
2773         else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
2774         else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
2775         else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
2776         else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
2777         else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
2778         else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
2779         else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
2780         else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
2781         else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
2782         else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
2783         else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
2784         else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
2785         else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
2786         else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
2787         else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
2788         else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
2789         else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
2790         else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
2791         else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
2792         else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
2793         else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
2794         else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
2795         else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
2796         else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
2797         else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
2798         else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
2799         else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
2800         else if (c == ';') { tok = TOK_SEMI; }
2801         else if (c == '{') { tok = TOK_LBRACE; }
2802         else if (c == '}') { tok = TOK_RBRACE; }
2803         else if (c == ',') { tok = TOK_COMMA; }
2804         else if (c == '=') { tok = TOK_EQ; }
2805         else if (c == ':') { tok = TOK_COLON; }
2806         else if (c == '[') { tok = TOK_LBRACKET; }
2807         else if (c == ']') { tok = TOK_RBRACKET; }
2808         else if (c == '(') { tok = TOK_LPAREN; }
2809         else if (c == ')') { tok = TOK_RPAREN; }
2810         else if (c == '*') { tok = TOK_STAR; }
2811         else if (c == '>') { tok = TOK_MORE; }
2812         else if (c == '<') { tok = TOK_LESS; }
2813         else if (c == '?') { tok = TOK_QUEST; }
2814         else if (c == '|') { tok = TOK_OR; }
2815         else if (c == '&') { tok = TOK_AND; }
2816         else if (c == '^') { tok = TOK_XOR; }
2817         else if (c == '+') { tok = TOK_PLUS; }
2818         else if (c == '-') { tok = TOK_MINUS; }
2819         else if (c == '/') { tok = TOK_DIV; }
2820         else if (c == '%') { tok = TOK_MOD; }
2821         else if (c == '!') { tok = TOK_BANG; }
2822         else if (c == '.') { tok = TOK_DOT; }
2823         else if (c == '~') { tok = TOK_TILDE; }
2824         else if (c == '#') { tok = TOK_MACRO; }
2825         if (tok == TOK_MACRO) {
2826                 /* Only match preprocessor directives at the start of a line */
2827                 char *ptr;
2828                 for(ptr = file->line_start; spacep(*ptr); ptr++)
2829                         ;
2830                 if (ptr != tokp) {
2831                         tok = TOK_UNKNOWN;
2832                 }
2833         }
2834         if (tok == TOK_UNKNOWN) {
2835                 error(state, 0, "unknown token");
2836         }
2837
2838         file->pos = tokp + 1;
2839         tk->tok = tok;
2840         if (tok == TOK_IDENT) {
2841                 ident_to_keyword(state, tk);
2842         }
2843         /* Don't return space tokens. */
2844         if (tok == TOK_SPACE) {
2845                 goto next_token;
2846         }
2847 }
2848
2849 static void compile_macro(struct compile_state *state, struct token *tk)
2850 {
2851         struct file_state *file;
2852         struct hash_entry *ident;
2853         ident = tk->ident;
2854         file = xmalloc(sizeof(*file), "file_state");
2855         file->basename = xstrdup(tk->ident->name);
2856         file->dirname = xstrdup("");
2857         file->size = ident->sym_define->buf_len;
2858         file->buf = xmalloc(file->size +2,  file->basename);
2859         memcpy(file->buf, ident->sym_define->buf, file->size);
2860         file->buf[file->size] = '\n';
2861         file->buf[file->size + 1] = '\0';
2862         file->pos = file->buf;
2863         file->line_start = file->pos;
2864         file->line = 1;
2865         file->report_line = 1;
2866         file->report_name = file->basename;
2867         file->report_dir  = file->dirname;
2868         file->prev = state->file;
2869         state->file = file;
2870 }
2871
2872
2873 static int mpeek(struct compile_state *state, int index)
2874 {
2875         struct token *tk;
2876         int rescan;
2877         tk = &state->token[index + 1];
2878         if (tk->tok == -1) {
2879                 next_token(state, index + 1);
2880         }
2881         do {
2882                 rescan = 0;
2883                 if ((tk->tok == TOK_EOF) && 
2884                         (state->file != state->macro_file) &&
2885                         (state->file->prev)) {
2886                         struct file_state *file = state->file;
2887                         state->file = file->prev;
2888                         /* file->basename is used keep it */
2889                         if (file->report_dir != file->dirname) {
2890                                 xfree(file->report_dir);
2891                         }
2892                         xfree(file->dirname);
2893                         xfree(file->buf);
2894                         xfree(file);
2895                         next_token(state, index + 1);
2896                         rescan = 1;
2897                 }
2898                 else if (tk->ident && tk->ident->sym_define) {
2899                         compile_macro(state, tk);
2900                         next_token(state, index + 1);
2901                         rescan = 1;
2902                 }
2903         } while(rescan);
2904         /* Don't show the token on the next line */
2905         if (state->macro_line < state->macro_file->line) {
2906                 return TOK_EOF;
2907         }
2908         return state->token[index +1].tok;
2909 }
2910
2911 static void meat(struct compile_state *state, int index, int tok)
2912 {
2913         int next_tok;
2914         int i;
2915         next_tok = mpeek(state, index);
2916         if (next_tok != tok) {
2917                 const char *name1, *name2;
2918                 name1 = tokens[next_tok];
2919                 name2 = "";
2920                 if (next_tok == TOK_IDENT) {
2921                         name2 = state->token[index + 1].ident->name;
2922                 }
2923                 error(state, 0, "found %s %s expected %s", 
2924                         name1, name2, tokens[tok]);
2925         }
2926         /* Free the old token value */
2927         if (state->token[index].str_len) {
2928                 memset((void *)(state->token[index].val.str), -1, 
2929                         state->token[index].str_len);
2930                 xfree(state->token[index].val.str);
2931         }
2932         for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
2933                 state->token[i] = state->token[i + 1];
2934         }
2935         memset(&state->token[i], 0, sizeof(state->token[i]));
2936         state->token[i].tok = -1;
2937 }
2938
2939 static long_t mcexpr(struct compile_state *state, int index);
2940
2941 static long_t mprimary_expr(struct compile_state *state, int index)
2942 {
2943         long_t val;
2944         int tok;
2945         tok = mpeek(state, index);
2946         while(state->token[index + 1].ident && 
2947                 state->token[index + 1].ident->sym_define) {
2948                 meat(state, index, tok);
2949                 compile_macro(state, &state->token[index]);
2950                 tok = mpeek(state, index);
2951         }
2952         switch(tok) {
2953         case TOK_LPAREN:
2954                 meat(state, index, TOK_LPAREN);
2955                 val = mcexpr(state, index);
2956                 meat(state, index, TOK_RPAREN);
2957                 break;
2958         case TOK_LIT_INT:
2959         {
2960                 char *end;
2961                 meat(state, index, TOK_LIT_INT);
2962                 errno = 0;
2963                 val = strtol(state->token[index].val.str, &end, 0);
2964 #ifdef __x86_64__
2965                 if (((val == INT_MIN) || (val == INT_MAX)) &&
2966                         (errno == ERANGE)) {
2967 #else
2968                 if (((val == LONG_MIN) || (val == LONG_MAX)) &&
2969                         (errno == ERANGE)) {
2970 #endif
2971                         error(state, 0, "Integer constant to large");
2972                 }
2973                 break;
2974         }
2975         default:
2976                 meat(state, index, TOK_LIT_INT);
2977                 val = 0;
2978         }
2979         return val;
2980 }
2981 static long_t munary_expr(struct compile_state *state, int index)
2982 {
2983         long_t val;
2984         switch(mpeek(state, index)) {
2985         case TOK_PLUS:
2986                 meat(state, index, TOK_PLUS);
2987                 val = munary_expr(state, index);
2988                 val = + val;
2989                 break;
2990         case TOK_MINUS:
2991                 meat(state, index, TOK_MINUS);
2992                 val = munary_expr(state, index);
2993                 val = - val;
2994                 break;
2995         case TOK_TILDE:
2996                 meat(state, index, TOK_BANG);
2997                 val = munary_expr(state, index);
2998                 val = ~ val;
2999                 break;
3000         case TOK_BANG:
3001                 meat(state, index, TOK_BANG);
3002                 val = munary_expr(state, index);
3003                 val = ! val;
3004                 break;
3005         default:
3006                 val = mprimary_expr(state, index);
3007                 break;
3008         }
3009         return val;
3010         
3011 }
3012 static long_t mmul_expr(struct compile_state *state, int index)
3013 {
3014         long_t val;
3015         int done;
3016         val = munary_expr(state, index);
3017         do {
3018                 long_t right;
3019                 done = 0;
3020                 switch(mpeek(state, index)) {
3021                 case TOK_STAR:
3022                         meat(state, index, TOK_STAR);
3023                         right = munary_expr(state, index);
3024                         val = val * right;
3025                         break;
3026                 case TOK_DIV:
3027                         meat(state, index, TOK_DIV);
3028                         right = munary_expr(state, index);
3029                         val = val / right;
3030                         break;
3031                 case TOK_MOD:
3032                         meat(state, index, TOK_MOD);
3033                         right = munary_expr(state, index);
3034                         val = val % right;
3035                         break;
3036                 default:
3037                         done = 1;
3038                         break;
3039                 }
3040         } while(!done);
3041
3042         return val;
3043 }
3044
3045 static long_t madd_expr(struct compile_state *state, int index)
3046 {
3047         long_t val;
3048         int done;
3049         val = mmul_expr(state, index);
3050         do {
3051                 long_t right;
3052                 done = 0;
3053                 switch(mpeek(state, index)) {
3054                 case TOK_PLUS:
3055                         meat(state, index, TOK_PLUS);
3056                         right = mmul_expr(state, index);
3057                         val = val + right;
3058                         break;
3059                 case TOK_MINUS:
3060                         meat(state, index, TOK_MINUS);
3061                         right = mmul_expr(state, index);
3062                         val = val - right;
3063                         break;
3064                 default:
3065                         done = 1;
3066                         break;
3067                 }
3068         } while(!done);
3069
3070         return val;
3071 }
3072
3073 static long_t mshift_expr(struct compile_state *state, int index)
3074 {
3075         long_t val;
3076         int done;
3077         val = madd_expr(state, index);
3078         do {
3079                 long_t right;
3080                 done = 0;
3081                 switch(mpeek(state, index)) {
3082                 case TOK_SL:
3083                         meat(state, index, TOK_SL);
3084                         right = madd_expr(state, index);
3085                         val = val << right;
3086                         break;
3087                 case TOK_SR:
3088                         meat(state, index, TOK_SR);
3089                         right = madd_expr(state, index);
3090                         val = val >> right;
3091                         break;
3092                 default:
3093                         done = 1;
3094                         break;
3095                 }
3096         } while(!done);
3097
3098         return val;
3099 }
3100
3101 static long_t mrel_expr(struct compile_state *state, int index)
3102 {
3103         long_t val;
3104         int done;
3105         val = mshift_expr(state, index);
3106         do {
3107                 long_t right;
3108                 done = 0;
3109                 switch(mpeek(state, index)) {
3110                 case TOK_LESS:
3111                         meat(state, index, TOK_LESS);
3112                         right = mshift_expr(state, index);
3113                         val = val < right;
3114                         break;
3115                 case TOK_MORE:
3116                         meat(state, index, TOK_MORE);
3117                         right = mshift_expr(state, index);
3118                         val = val > right;
3119                         break;
3120                 case TOK_LESSEQ:
3121                         meat(state, index, TOK_LESSEQ);
3122                         right = mshift_expr(state, index);
3123                         val = val <= right;
3124                         break;
3125                 case TOK_MOREEQ:
3126                         meat(state, index, TOK_MOREEQ);
3127                         right = mshift_expr(state, index);
3128                         val = val >= right;
3129                         break;
3130                 default:
3131                         done = 1;
3132                         break;
3133                 }
3134         } while(!done);
3135         return val;
3136 }
3137
3138 static long_t meq_expr(struct compile_state *state, int index)
3139 {
3140         long_t val;
3141         int done;
3142         val = mrel_expr(state, index);
3143         do {
3144                 long_t right;
3145                 done = 0;
3146                 switch(mpeek(state, index)) {
3147                 case TOK_EQEQ:
3148                         meat(state, index, TOK_EQEQ);
3149                         right = mrel_expr(state, index);
3150                         val = val == right;
3151                         break;
3152                 case TOK_NOTEQ:
3153                         meat(state, index, TOK_NOTEQ);
3154                         right = mrel_expr(state, index);
3155                         val = val != right;
3156                         break;
3157                 default:
3158                         done = 1;
3159                         break;
3160                 }
3161         } while(!done);
3162         return val;
3163 }
3164
3165 static long_t mand_expr(struct compile_state *state, int index)
3166 {
3167         long_t val;
3168         val = meq_expr(state, index);
3169         if (mpeek(state, index) == TOK_AND) {
3170                 long_t right;
3171                 meat(state, index, TOK_AND);
3172                 right = meq_expr(state, index);
3173                 val = val & right;
3174         }
3175         return val;
3176 }
3177
3178 static long_t mxor_expr(struct compile_state *state, int index)
3179 {
3180         long_t val;
3181         val = mand_expr(state, index);
3182         if (mpeek(state, index) == TOK_XOR) {
3183                 long_t right;
3184                 meat(state, index, TOK_XOR);
3185                 right = mand_expr(state, index);
3186                 val = val ^ right;
3187         }
3188         return val;
3189 }
3190
3191 static long_t mor_expr(struct compile_state *state, int index)
3192 {
3193         long_t val;
3194         val = mxor_expr(state, index);
3195         if (mpeek(state, index) == TOK_OR) {
3196                 long_t right;
3197                 meat(state, index, TOK_OR);
3198                 right = mxor_expr(state, index);
3199                 val = val | right;
3200         }
3201         return val;
3202 }
3203
3204 static long_t mland_expr(struct compile_state *state, int index)
3205 {
3206         long_t val;
3207         val = mor_expr(state, index);
3208         if (mpeek(state, index) == TOK_LOGAND) {
3209                 long_t right;
3210                 meat(state, index, TOK_LOGAND);
3211                 right = mor_expr(state, index);
3212                 val = val && right;
3213         }
3214         return val;
3215 }
3216 static long_t mlor_expr(struct compile_state *state, int index)
3217 {
3218         long_t val;
3219         val = mland_expr(state, index);
3220         if (mpeek(state, index) == TOK_LOGOR) {
3221                 long_t right;
3222                 meat(state, index, TOK_LOGOR);
3223                 right = mland_expr(state, index);
3224                 val = val || right;
3225         }
3226         return val;
3227 }
3228
3229 static long_t mcexpr(struct compile_state *state, int index)
3230 {
3231         return mlor_expr(state, index);
3232 }
3233 static void preprocess(struct compile_state *state, int index)
3234 {
3235         /* Doing much more with the preprocessor would require
3236          * a parser and a major restructuring.
3237          * Postpone that for later.
3238          */
3239         struct file_state *file;
3240         struct token *tk;
3241         int line;
3242         int tok;
3243         
3244         file = state->file;
3245         tk = &state->token[index];
3246         state->macro_line = line = file->line;
3247         state->macro_file = file;
3248
3249         next_token(state, index);
3250         ident_to_macro(state, tk);
3251         if (tk->tok == TOK_IDENT) {
3252                 error(state, 0, "undefined preprocessing directive `%s'",
3253                         tk->ident->name);
3254         }
3255         switch(tk->tok) {
3256         case TOK_LIT_INT:
3257         {
3258                 int override_line;
3259                 override_line = strtoul(tk->val.str, 0, 10);
3260                 next_token(state, index);
3261                 /* I have a cpp line marker parse it */
3262                 if (tk->tok == TOK_LIT_STRING) {
3263                         const char *token, *base;
3264                         char *name, *dir;
3265                         int name_len, dir_len;
3266                         name = xmalloc(tk->str_len, "report_name");
3267                         token = tk->val.str + 1;
3268                         base = strrchr(token, '/');
3269                         name_len = tk->str_len -2;
3270                         if (base != 0) {
3271                                 dir_len = base - token;
3272                                 base++;
3273                                 name_len -= base - token;
3274                         } else {
3275                                 dir_len = 0;
3276                                 base = token;
3277                         }
3278                         memcpy(name, base, name_len);
3279                         name[name_len] = '\0';
3280                         dir = xmalloc(dir_len + 1, "report_dir");
3281                         memcpy(dir, token, dir_len);
3282                         dir[dir_len] = '\0';
3283                         file->report_line = override_line - 1;
3284                         file->report_name = name;
3285                         file->report_dir = dir;
3286                 }
3287         }
3288                 break;
3289         case TOK_LINE:
3290                 meat(state, index, TOK_LINE);
3291                 meat(state, index, TOK_LIT_INT);
3292                 file->report_line = strtoul(tk->val.str, 0, 10) -1;
3293                 if (mpeek(state, index) == TOK_LIT_STRING) {
3294                         const char *token, *base;
3295                         char *name, *dir;
3296                         int name_len, dir_len;
3297                         meat(state, index, TOK_LIT_STRING);
3298                         name = xmalloc(tk->str_len, "report_name");
3299                         token = tk->val.str + 1;
3300                         name_len = tk->str_len - 2;
3301                         if (base != 0) {
3302                                 dir_len = base - token;
3303                                 base++;
3304                                 name_len -= base - token;
3305                         } else {
3306                                 dir_len = 0;
3307                                 base = token;
3308                         }
3309                         memcpy(name, base, name_len);
3310                         name[name_len] = '\0';
3311                         dir = xmalloc(dir_len + 1, "report_dir");
3312                         memcpy(dir, token, dir_len);
3313                         dir[dir_len] = '\0';
3314                         file->report_name = name;
3315                         file->report_dir = dir;
3316                 }
3317                 break;
3318         case TOK_UNDEF:
3319         case TOK_PRAGMA:
3320                 if (state->if_value < 0) {
3321                         break;
3322                 }
3323                 warning(state, 0, "Ignoring preprocessor directive: %s", 
3324                         tk->ident->name);
3325                 break;
3326         case TOK_ELIF:
3327                 error(state, 0, "#elif not supported");
3328 #warning "FIXME multiple #elif and #else in an #if do not work properly"
3329                 if (state->if_depth == 0) {
3330                         error(state, 0, "#elif without #if");
3331                 }
3332                 /* If the #if was taken the #elif just disables the following code */
3333                 if (state->if_value >= 0) {
3334                         state->if_value = - state->if_value;
3335                 }
3336                 /* If the previous #if was not taken see if the #elif enables the 
3337                  * trailing code.
3338                  */
3339                 else if ((state->if_value < 0) && 
3340                         (state->if_depth == - state->if_value))
3341                 {
3342                         if (mcexpr(state, index) != 0) {
3343                                 state->if_value = state->if_depth;
3344                         }
3345                         else {
3346                                 state->if_value = - state->if_depth;
3347                         }
3348                 }
3349                 break;
3350         case TOK_IF:
3351                 state->if_depth++;
3352                 if (state->if_value < 0) {
3353                         break;
3354                 }
3355                 if (mcexpr(state, index) != 0) {
3356                         state->if_value = state->if_depth;
3357                 }
3358                 else {
3359                         state->if_value = - state->if_depth;
3360                 }
3361                 break;
3362         case TOK_IFNDEF:
3363                 state->if_depth++;
3364                 if (state->if_value < 0) {
3365                         break;
3366                 }
3367                 next_token(state, index);
3368                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3369                         error(state, 0, "Invalid macro name");
3370                 }
3371                 if (tk->ident->sym_define == 0) {
3372                         state->if_value = state->if_depth;
3373                 } 
3374                 else {
3375                         state->if_value = - state->if_depth;
3376                 }
3377                 break;
3378         case TOK_IFDEF:
3379                 state->if_depth++;
3380                 if (state->if_value < 0) {
3381                         break;
3382                 }
3383                 next_token(state, index);
3384                 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
3385                         error(state, 0, "Invalid macro name");
3386                 }
3387                 if (tk->ident->sym_define != 0) {
3388                         state->if_value = state->if_depth;
3389                 }
3390                 else {
3391                         state->if_value = - state->if_depth;
3392                 }
3393                 break;
3394         case TOK_ELSE:
3395                 if (state->if_depth == 0) {
3396                         error(state, 0, "#else without #if");
3397                 }
3398                 if ((state->if_value >= 0) ||
3399                         ((state->if_value < 0) && 
3400                                 (state->if_depth == -state->if_value)))
3401                 {
3402                         state->if_value = - state->if_value;
3403                 }
3404                 break;
3405         case TOK_ENDIF:
3406                 if (state->if_depth == 0) {
3407                         error(state, 0, "#endif without #if");
3408                 }
3409                 if ((state->if_value >= 0) ||
3410                         ((state->if_value < 0) &&
3411                                 (state->if_depth == -state->if_value))) 
3412                 {
3413                         state->if_value = state->if_depth - 1;
3414                 }
3415                 state->if_depth--;
3416                 break;
3417         case TOK_DEFINE:
3418         {
3419                 struct hash_entry *ident;
3420                 struct macro *macro;
3421                 char *ptr;
3422                 
3423                 if (state->if_value < 0) /* quit early when #if'd out */
3424                         break;
3425
3426                 meat(state, index, TOK_IDENT);
3427                 ident = tk->ident;
3428                 
3429
3430                 if (*file->pos == '(') {
3431 #warning "FIXME macros with arguments not supported"
3432                         error(state, 0, "Macros with arguments not supported");
3433                 }
3434
3435                 /* Find the end of the line to get an estimate of
3436                  * the macro's length.
3437                  */
3438                 for(ptr = file->pos; *ptr != '\n'; ptr++)  
3439                         ;
3440
3441                 if (ident->sym_define != 0) {
3442                         error(state, 0, "macro %s already defined\n", ident->name);
3443                 }
3444                 macro = xmalloc(sizeof(*macro), "macro");
3445                 macro->ident = ident;
3446                 macro->buf_len = ptr - file->pos +1;
3447                 macro->buf = xmalloc(macro->buf_len +2, "macro buf");
3448
3449                 memcpy(macro->buf, file->pos, macro->buf_len);
3450                 macro->buf[macro->buf_len] = '\n';
3451                 macro->buf[macro->buf_len +1] = '\0';
3452
3453                 ident->sym_define = macro;
3454                 break;
3455         }
3456         case TOK_ERROR:
3457         {
3458                 char *end;
3459                 int len;
3460                 /* Find the end of the line */
3461                 for(end = file->pos; *end != '\n'; end++)
3462                         ;
3463                 len = (end - file->pos);
3464                 if (state->if_value >= 0) {
3465                         error(state, 0, "%*.*s", len, len, file->pos);
3466                 }
3467                 file->pos = end;
3468                 break;
3469         }
3470         case TOK_WARNING:
3471         {
3472                 char *end;
3473                 int len;
3474                 /* Find the end of the line */
3475                 for(end = file->pos; *end != '\n'; end++)
3476                         ;
3477                 len = (end - file->pos);
3478                 if (state->if_value >= 0) {
3479                         warning(state, 0, "%*.*s", len, len, file->pos);
3480                 }
3481                 file->pos = end;
3482                 break;
3483         }
3484         case TOK_INCLUDE:
3485         {
3486                 char *name;
3487                 char *ptr;
3488                 int local;
3489                 local = 0;
3490                 name = 0;
3491                 next_token(state, index);
3492                 if (tk->tok == TOK_LIT_STRING) {
3493                         const char *token;
3494                         int name_len;
3495                         name = xmalloc(tk->str_len, "include");
3496                         token = tk->val.str +1;
3497                         name_len = tk->str_len -2;
3498                         if (*token == '"') {
3499                                 token++;
3500                                 name_len--;
3501                         }
3502                         memcpy(name, token, name_len);
3503                         name[name_len] = '\0';
3504                         local = 1;
3505                 }
3506                 else if (tk->tok == TOK_LESS) {
3507                         char *start, *end;
3508                         start = file->pos;
3509                         for(end = start; *end != '\n'; end++) {
3510                                 if (*end == '>') {
3511                                         break;
3512                                 }
3513                         }
3514                         if (*end == '\n') {
3515                                 error(state, 0, "Unterminated included directive");
3516                         }
3517                         name = xmalloc(end - start + 1, "include");
3518                         memcpy(name, start, end - start);
3519                         name[end - start] = '\0';
3520                         file->pos = end +1;
3521                         local = 0;
3522                 }
3523                 else {
3524                         error(state, 0, "Invalid include directive");
3525                 }
3526                 /* Error if there are any characters after the include */
3527                 for(ptr = file->pos; *ptr != '\n'; ptr++) {
3528                         switch(*ptr) {
3529                         case ' ':
3530                         case '\t':
3531                         case '\v':
3532                                 break;
3533                         default:
3534                                 error(state, 0, "garbage after include directive");
3535                         }
3536                 }
3537                 if (state->if_value >= 0) {
3538                         compile_file(state, name, local);
3539                 }
3540                 xfree(name);
3541                 next_token(state, index);
3542                 return;
3543         }
3544         default:
3545                 /* Ignore # without a following ident */
3546                 if (tk->tok == TOK_IDENT) {
3547                         error(state, 0, "Invalid preprocessor directive: %s", 
3548                                 tk->ident->name);
3549                 }
3550                 break;
3551         }
3552         /* Consume the rest of the macro line */
3553         do {
3554                 tok = mpeek(state, index);
3555                 meat(state, index, tok);
3556         } while(tok != TOK_EOF);
3557         return;
3558 }
3559
3560 static void token(struct compile_state *state, int index)
3561 {
3562         struct file_state *file;
3563         struct token *tk;
3564         int rescan;
3565
3566         tk = &state->token[index];
3567         next_token(state, index);
3568         do {
3569                 rescan = 0;
3570                 file = state->file;
3571                 if (tk->tok == TOK_EOF && file->prev) {
3572                         state->file = file->prev;
3573                         /* file->basename is used keep it */
3574                         xfree(file->dirname);
3575                         xfree(file->buf);
3576                         xfree(file);
3577                         next_token(state, index);
3578                         rescan = 1;
3579                 }
3580                 else if (tk->tok == TOK_MACRO) {
3581                         preprocess(state, index);
3582                         rescan = 1;
3583                 }
3584                 else if (tk->ident && tk->ident->sym_define) {
3585                         compile_macro(state, tk);
3586                         next_token(state, index);
3587                         rescan = 1;
3588                 }
3589                 else if (state->if_value < 0) {
3590                         next_token(state, index);
3591                         rescan = 1;
3592                 }
3593         } while(rescan);
3594 }
3595
3596 static int peek(struct compile_state *state)
3597 {
3598         if (state->token[1].tok == -1) {
3599                 token(state, 1);
3600         }
3601         return state->token[1].tok;
3602 }
3603
3604 static int peek2(struct compile_state *state)
3605 {
3606         if (state->token[1].tok == -1) {
3607                 token(state, 1);
3608         }
3609         if (state->token[2].tok == -1) {
3610                 token(state, 2);
3611         }
3612         return state->token[2].tok;
3613 }
3614
3615 static void eat(struct compile_state *state, int tok)
3616 {
3617         int next_tok;
3618         int i;
3619         next_tok = peek(state);
3620         if (next_tok != tok) {
3621                 const char *name1, *name2;
3622                 name1 = tokens[next_tok];
3623                 name2 = "";
3624                 if (next_tok == TOK_IDENT) {
3625                         name2 = state->token[1].ident->name;
3626                 }
3627                 error(state, 0, "\tfound %s %s expected %s",
3628                         name1, name2 ,tokens[tok]);
3629         }
3630         /* Free the old token value */
3631         if (state->token[0].str_len) {
3632                 xfree((void *)(state->token[0].val.str));
3633         }
3634         for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
3635                 state->token[i] = state->token[i + 1];
3636         }
3637         memset(&state->token[i], 0, sizeof(state->token[i]));
3638         state->token[i].tok = -1;
3639 }
3640
3641 #warning "FIXME do not hardcode the include paths"
3642 static char *include_paths[] = {
3643         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/include",
3644         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src/arch/i386/include",
3645         "/home/eric/projects/linuxbios/checkin/solo/freebios2/src",
3646         0
3647 };
3648
3649 static void compile_file(struct compile_state *state, const char *filename, int local)
3650 {
3651         char cwd[4096];
3652         const char *subdir, *base;
3653         int subdir_len;
3654         struct file_state *file;
3655         char *basename;
3656         file = xmalloc(sizeof(*file), "file_state");
3657
3658         base = strrchr(filename, '/');
3659         subdir = filename;
3660         if (base != 0) {
3661                 subdir_len = base - filename;
3662                 base++;
3663         }
3664         else {
3665                 base = filename;
3666                 subdir_len = 0;
3667         }
3668         basename = xmalloc(strlen(base) +1, "basename");
3669         strcpy(basename, base);
3670         file->basename = basename;
3671
3672         if (getcwd(cwd, sizeof(cwd)) == 0) {
3673                 die("cwd buffer to small");
3674         }
3675         
3676         if (subdir[0] == '/') {
3677                 file->dirname = xmalloc(subdir_len + 1, "dirname");
3678                 memcpy(file->dirname, subdir, subdir_len);
3679                 file->dirname[subdir_len] = '\0';
3680         }
3681         else {
3682                 char *dir;
3683                 int dirlen;
3684                 char **path;
3685                 /* Find the appropriate directory... */
3686                 dir = 0;
3687                 if (!state->file && exists(cwd, filename)) {
3688                         dir = cwd;
3689                 }
3690                 if (local && state->file && exists(state->file->dirname, filename)) {
3691                         dir = state->file->dirname;
3692                 }
3693                 for(path = include_paths; !dir && *path; path++) {
3694                         if (exists(*path, filename)) {
3695                                 dir = *path;
3696                         }
3697                 }
3698                 if (!dir) {
3699                         error(state, 0, "Cannot find `%s'\n", filename);
3700                 }
3701                 dirlen = strlen(dir);
3702                 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
3703                 memcpy(file->dirname, dir, dirlen);
3704                 file->dirname[dirlen] = '/';
3705                 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
3706                 file->dirname[dirlen + 1 + subdir_len] = '\0';
3707         }
3708         file->buf = slurp_file(file->dirname, file->basename, &file->size);
3709         xchdir(cwd);
3710
3711         file->pos = file->buf;
3712         file->line_start = file->pos;
3713         file->line = 1;
3714
3715         file->report_line = 1;
3716         file->report_name = file->basename;
3717         file->report_dir  = file->dirname;
3718
3719         file->prev = state->file;
3720         state->file = file;
3721         
3722         process_trigraphs(state);
3723         splice_lines(state);
3724 }
3725
3726 /* Type helper functions */
3727
3728 static struct type *new_type(
3729         unsigned int type, struct type *left, struct type *right)
3730 {
3731         struct type *result;
3732         result = xmalloc(sizeof(*result), "type");
3733         result->type = type;
3734         result->left = left;
3735         result->right = right;
3736         result->field_ident = 0;
3737         result->type_ident = 0;
3738         return result;
3739 }
3740
3741 static struct type *clone_type(unsigned int specifiers, struct type *old)
3742 {
3743         struct type *result;
3744         result = xmalloc(sizeof(*result), "type");
3745         memcpy(result, old, sizeof(*result));
3746         result->type &= TYPE_MASK;
3747         result->type |= specifiers;
3748         return result;
3749 }
3750
3751 #define SIZEOF_SHORT 2
3752 #define SIZEOF_INT   4
3753 #define SIZEOF_LONG  (sizeof(long_t))
3754
3755 #define ALIGNOF_SHORT 2
3756 #define ALIGNOF_INT   4
3757 #define ALIGNOF_LONG  (sizeof(long_t))
3758
3759 #define MASK_UCHAR(X)    ((X) & ((ulong_t)0xff))
3760 #define MASK_USHORT(X)   ((X) & (((ulong_t)1 << (SIZEOF_SHORT*8)) - 1))
3761 static inline ulong_t mask_uint(ulong_t x)
3762 {
3763         if (SIZEOF_INT < SIZEOF_LONG) {
3764                 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT*8))) -1;
3765                 x &= mask;
3766         }
3767         return x;
3768 }
3769 #define MASK_UINT(X)      (mask_uint(X))
3770 #define MASK_ULONG(X)    (X)
3771
3772 static struct type void_type   = { .type  = TYPE_VOID };
3773 static struct type char_type   = { .type  = TYPE_CHAR };
3774 static struct type uchar_type  = { .type  = TYPE_UCHAR };
3775 static struct type short_type  = { .type  = TYPE_SHORT };
3776 static struct type ushort_type = { .type  = TYPE_USHORT };
3777 static struct type int_type    = { .type  = TYPE_INT };
3778 static struct type uint_type   = { .type  = TYPE_UINT };
3779 static struct type long_type   = { .type  = TYPE_LONG };
3780 static struct type ulong_type  = { .type  = TYPE_ULONG };
3781
3782 static struct triple *variable(struct compile_state *state, struct type *type)
3783 {
3784         struct triple *result;
3785         if ((type->type & STOR_MASK) != STOR_PERM) {
3786                 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
3787                         result = triple(state, OP_ADECL, type, 0, 0);
3788                 } else {
3789                         struct type *field;
3790                         struct triple **vector;
3791                         ulong_t index;
3792                         result = new_triple(state, OP_VAL_VEC, type, -1, -1);
3793                         vector = &result->param[0];
3794
3795                         field = type->left;
3796                         index = 0;
3797                         while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
3798                                 vector[index] = variable(state, field->left);
3799                                 field = field->right;
3800                                 index++;
3801                         }
3802                         vector[index] = variable(state, field);
3803                 }
3804         }
3805         else {
3806                 result = triple(state, OP_SDECL, type, 0, 0);
3807         }
3808         return result;
3809 }
3810
3811 static void stor_of(FILE *fp, struct type *type)
3812 {
3813         switch(type->type & STOR_MASK) {
3814         case STOR_AUTO:
3815                 fprintf(fp, "auto ");
3816                 break;
3817         case STOR_STATIC:
3818                 fprintf(fp, "static ");
3819                 break;
3820         case STOR_EXTERN:
3821                 fprintf(fp, "extern ");
3822                 break;
3823         case STOR_REGISTER:
3824                 fprintf(fp, "register ");
3825                 break;
3826         case STOR_TYPEDEF:
3827                 fprintf(fp, "typedef ");
3828                 break;
3829         case STOR_INLINE:
3830                 fprintf(fp, "inline ");
3831                 break;
3832         }
3833 }
3834 static void qual_of(FILE *fp, struct type *type)
3835 {
3836         if (type->type & QUAL_CONST) {
3837                 fprintf(fp, " const");
3838         }
3839         if (type->type & QUAL_VOLATILE) {
3840                 fprintf(fp, " volatile");
3841         }
3842         if (type->type & QUAL_RESTRICT) {
3843                 fprintf(fp, " restrict");
3844         }
3845 }
3846
3847 static void name_of(FILE *fp, struct type *type)
3848 {
3849         stor_of(fp, type);
3850         switch(type->type & TYPE_MASK) {
3851         case TYPE_VOID:
3852                 fprintf(fp, "void");
3853                 qual_of(fp, type);
3854                 break;
3855         case TYPE_CHAR:
3856                 fprintf(fp, "signed char");
3857                 qual_of(fp, type);
3858                 break;
3859         case TYPE_UCHAR:
3860                 fprintf(fp, "unsigned char");
3861                 qual_of(fp, type);
3862                 break;
3863         case TYPE_SHORT:
3864                 fprintf(fp, "signed short");
3865                 qual_of(fp, type);
3866                 break;
3867         case TYPE_USHORT:
3868                 fprintf(fp, "unsigned short");
3869                 qual_of(fp, type);
3870                 break;
3871         case TYPE_INT:
3872                 fprintf(fp, "signed int");
3873                 qual_of(fp, type);
3874                 break;
3875         case TYPE_UINT:
3876                 fprintf(fp, "unsigned int");
3877                 qual_of(fp, type);
3878                 break;
3879         case TYPE_LONG:
3880                 fprintf(fp, "signed long");
3881                 qual_of(fp, type);
3882                 break;
3883         case TYPE_ULONG:
3884                 fprintf(fp, "unsigned long");
3885                 qual_of(fp, type);
3886                 break;
3887         case TYPE_POINTER:
3888                 name_of(fp, type->left);
3889                 fprintf(fp, " * ");
3890                 qual_of(fp, type);
3891                 break;
3892         case TYPE_PRODUCT:
3893         case TYPE_OVERLAP:
3894                 name_of(fp, type->left);
3895                 fprintf(fp, ", ");
3896                 name_of(fp, type->right);
3897                 break;
3898         case TYPE_ENUM:
3899                 fprintf(fp, "enum %s", type->type_ident->name);
3900                 qual_of(fp, type);
3901                 break;
3902         case TYPE_STRUCT:
3903                 fprintf(fp, "struct %s", type->type_ident->name);
3904                 qual_of(fp, type);
3905                 break;
3906         case TYPE_FUNCTION:
3907         {
3908                 name_of(fp, type->left);
3909                 fprintf(fp, " (*)(");
3910                 name_of(fp, type->right);
3911                 fprintf(fp, ")");
3912                 break;
3913         }
3914         case TYPE_ARRAY:
3915                 name_of(fp, type->left);
3916                 fprintf(fp, " [%ld]", type->elements);
3917                 break;
3918         default:
3919                 fprintf(fp, "????: %x", type->type & TYPE_MASK);
3920                 break;
3921         }
3922 }
3923
3924 static size_t align_of(struct compile_state *state, struct type *type)
3925 {
3926         size_t align;
3927         align = 0;
3928         switch(type->type & TYPE_MASK) {
3929         case TYPE_VOID:
3930                 align = 1;
3931                 break;
3932         case TYPE_CHAR:
3933         case TYPE_UCHAR:
3934                 align = 1;
3935                 break;
3936         case TYPE_SHORT:
3937         case TYPE_USHORT:
3938                 align = ALIGNOF_SHORT;
3939                 break;
3940         case TYPE_INT:
3941         case TYPE_UINT:
3942         case TYPE_ENUM:
3943                 align = ALIGNOF_INT;
3944                 break;
3945         case TYPE_LONG:
3946         case TYPE_ULONG:
3947         case TYPE_POINTER:
3948                 align = ALIGNOF_LONG;
3949                 break;
3950         case TYPE_PRODUCT:
3951         case TYPE_OVERLAP:
3952         {
3953                 size_t left_align, right_align;
3954                 left_align  = align_of(state, type->left);
3955                 right_align = align_of(state, type->right);
3956                 align = (left_align >= right_align) ? left_align : right_align;
3957                 break;
3958         }
3959         case TYPE_ARRAY:
3960                 align = align_of(state, type->left);
3961                 break;
3962         case TYPE_STRUCT:
3963                 align = align_of(state, type->left);
3964                 break;
3965         default:
3966                 error(state, 0, "alignof not yet defined for type\n");
3967                 break;
3968         }
3969         return align;
3970 }
3971
3972 static size_t needed_padding(size_t offset, size_t align)
3973 {
3974         size_t padding;
3975         padding = 0;
3976         if (offset % align) {
3977                 padding = align - (offset % align);
3978         }
3979         return padding;
3980 }
3981 static size_t size_of(struct compile_state *state, struct type *type)
3982 {
3983         size_t size;
3984         size = 0;
3985         switch(type->type & TYPE_MASK) {
3986         case TYPE_VOID:
3987                 size = 0;
3988                 break;
3989         case TYPE_CHAR:
3990         case TYPE_UCHAR:
3991                 size = 1;
3992                 break;
3993         case TYPE_SHORT:
3994         case TYPE_USHORT:
3995                 size = SIZEOF_SHORT;
3996                 break;
3997         case TYPE_INT:
3998         case TYPE_UINT:
3999         case TYPE_ENUM:
4000                 size = SIZEOF_INT;
4001                 break;
4002         case TYPE_LONG:
4003         case TYPE_ULONG:
4004         case TYPE_POINTER:
4005                 size = SIZEOF_LONG;
4006                 break;
4007         case TYPE_PRODUCT:
4008         {
4009                 size_t align, pad;
4010                 size = 0;
4011                 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4012                         align = align_of(state, type->left);
4013                         pad = needed_padding(size, align);
4014                         size = size + pad + size_of(state, type->left);
4015                         type = type->right;
4016                 }
4017                 align = align_of(state, type);
4018                 pad = needed_padding(size, align);
4019                 size = size + pad + size_of(state, type);
4020                 break;
4021         }
4022         case TYPE_OVERLAP:
4023         {
4024                 size_t size_left, size_right;
4025                 size_left = size_of(state, type->left);
4026                 size_right = size_of(state, type->right);
4027                 size = (size_left >= size_right)? size_left : size_right;
4028                 break;
4029         }
4030         case TYPE_ARRAY:
4031                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4032                         internal_error(state, 0, "Invalid array type");
4033                 } else {
4034                         size = size_of(state, type->left) * type->elements;
4035                 }
4036                 break;
4037         case TYPE_STRUCT:
4038         {
4039                 size_t align, pad;
4040                 size = size_of(state, type->left);
4041                 /* Pad structures so their size is a multiples of their alignment */
4042                 align = align_of(state, type);
4043                 pad = needed_padding(size, align);
4044                 size = size + pad;
4045                 break;
4046         }
4047         default:
4048                 internal_error(state, 0, "sizeof not yet defined for type\n");
4049                 break;
4050         }
4051         return size;
4052 }
4053
4054 static size_t field_offset(struct compile_state *state, 
4055         struct type *type, struct hash_entry *field)
4056 {
4057         struct type *member;
4058         size_t size, align;
4059         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4060                 internal_error(state, 0, "field_offset only works on structures");
4061         }
4062         size = 0;
4063         member = type->left;
4064         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4065                 align = align_of(state, member->left);
4066                 size += needed_padding(size, align);
4067                 if (member->left->field_ident == field) {
4068                         member = member->left;
4069                         break;
4070                 }
4071                 size += size_of(state, member->left);
4072                 member = member->right;
4073         }
4074         align = align_of(state, member);
4075         size += needed_padding(size, align);
4076         if (member->field_ident != field) {
4077                 error(state, 0, "member %s not present", field->name);
4078         }
4079         return size;
4080 }
4081
4082 static struct type *field_type(struct compile_state *state, 
4083         struct type *type, struct hash_entry *field)
4084 {
4085         struct type *member;
4086         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4087                 internal_error(state, 0, "field_type only works on structures");
4088         }
4089         member = type->left;
4090         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4091                 if (member->left->field_ident == field) {
4092                         member = member->left;
4093                         break;
4094                 }
4095                 member = member->right;
4096         }
4097         if (member->field_ident != field) {
4098                 error(state, 0, "member %s not present", field->name);
4099         }
4100         return member;
4101 }
4102
4103 static struct type *next_field(struct compile_state *state,
4104         struct type *type, struct type *prev_member) 
4105 {
4106         struct type *member;
4107         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4108                 internal_error(state, 0, "next_field only works on structures");
4109         }
4110         member = type->left;
4111         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
4112                 if (!prev_member) {
4113                         member = member->left;
4114                         break;
4115                 }
4116                 if (member->left == prev_member) {
4117                         prev_member = 0;
4118                 }
4119                 member = member->right;
4120         }
4121         if (member == prev_member) {
4122                 prev_member = 0;
4123         }
4124         if (prev_member) {
4125                 internal_error(state, 0, "prev_member %s not present", 
4126                         prev_member->field_ident->name);
4127         }
4128         return member;
4129 }
4130
4131 static struct triple *struct_field(struct compile_state *state,
4132         struct triple *decl, struct hash_entry *field)
4133 {
4134         struct triple **vector;
4135         struct type *type;
4136         ulong_t index;
4137         type = decl->type;
4138         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4139                 return decl;
4140         }
4141         if (decl->op != OP_VAL_VEC) {
4142                 internal_error(state, 0, "Invalid struct variable");
4143         }
4144         if (!field) {
4145                 internal_error(state, 0, "Missing structure field");
4146         }
4147         type = type->left;
4148         vector = &RHS(decl, 0);
4149         index = 0;
4150         while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
4151                 if (type->left->field_ident == field) {
4152                         type = type->left;
4153                         break;
4154                 }
4155                 index += 1;
4156                 type = type->right;
4157         }
4158         if (type->field_ident != field) {
4159                 internal_error(state, 0, "field %s not found?", field->name);
4160         }
4161         return vector[index];
4162 }
4163
4164 static void arrays_complete(struct compile_state *state, struct type *type)
4165 {
4166         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
4167                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4168                         error(state, 0, "array size not specified");
4169                 }
4170                 arrays_complete(state, type->left);
4171         }
4172 }
4173
4174 static unsigned int do_integral_promotion(unsigned int type)
4175 {
4176         type &= TYPE_MASK;
4177         if (TYPE_INTEGER(type) && 
4178                 TYPE_RANK(type) < TYPE_RANK(TYPE_INT)) {
4179                 type = TYPE_INT;
4180         }
4181         return type;
4182 }
4183
4184 static unsigned int do_arithmetic_conversion(
4185         unsigned int left, unsigned int right)
4186 {
4187         left &= TYPE_MASK;
4188         right &= TYPE_MASK;
4189         if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
4190                 return TYPE_LDOUBLE;
4191         }
4192         else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
4193                 return TYPE_DOUBLE;
4194         }
4195         else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
4196                 return TYPE_FLOAT;
4197         }
4198         left = do_integral_promotion(left);
4199         right = do_integral_promotion(right);
4200         /* If both operands have the same size done */
4201         if (left == right) {
4202                 return left;
4203         }
4204         /* If both operands have the same signedness pick the larger */
4205         else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
4206                 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
4207         }
4208         /* If the signed type can hold everything use it */
4209         else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
4210                 return left;
4211         }
4212         else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
4213                 return right;
4214         }
4215         /* Convert to the unsigned type with the same rank as the signed type */
4216         else if (TYPE_SIGNED(left)) {
4217                 return TYPE_MKUNSIGNED(left);
4218         }
4219         else {
4220                 return TYPE_MKUNSIGNED(right);
4221         }
4222 }
4223
4224 /* see if two types are the same except for qualifiers */
4225 static int equiv_types(struct type *left, struct type *right)
4226 {
4227         unsigned int type;
4228         /* Error if the basic types do not match */
4229         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4230                 return 0;
4231         }
4232         type = left->type & TYPE_MASK;
4233         /* If the basic types match and it is a void type we are done */
4234         if (type == TYPE_VOID) {
4235                 return 1;
4236         }
4237         /* if the basic types match and it is an arithmetic type we are done */
4238         if (TYPE_ARITHMETIC(type)) {
4239                 return 1;
4240         }
4241         /* If it is a pointer type recurse and keep testing */
4242         if (type == TYPE_POINTER) {
4243                 return equiv_types(left->left, right->left);
4244         }
4245         else if (type == TYPE_ARRAY) {
4246                 return (left->elements == right->elements) &&
4247                         equiv_types(left->left, right->left);
4248         }
4249         /* test for struct/union equality */
4250         else if (type == TYPE_STRUCT) {
4251                 return left->type_ident == right->type_ident;
4252         }
4253         /* Test for equivalent functions */
4254         else if (type == TYPE_FUNCTION) {
4255                 return equiv_types(left->left, right->left) &&
4256                         equiv_types(left->right, right->right);
4257         }
4258         /* We only see TYPE_PRODUCT as part of function equivalence matching */
4259         else if (type == TYPE_PRODUCT) {
4260                 return equiv_types(left->left, right->left) &&
4261                         equiv_types(left->right, right->right);
4262         }
4263         /* We should see TYPE_OVERLAP */
4264         else {
4265                 return 0;
4266         }
4267 }
4268
4269 static int equiv_ptrs(struct type *left, struct type *right)
4270 {
4271         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4272                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4273                 return 0;
4274         }
4275         return equiv_types(left->left, right->left);
4276 }
4277
4278 static struct type *compatible_types(struct type *left, struct type *right)
4279 {
4280         struct type *result;
4281         unsigned int type, qual_type;
4282         /* Error if the basic types do not match */
4283         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
4284                 return 0;
4285         }
4286         type = left->type & TYPE_MASK;
4287         qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4288         result = 0;
4289         /* if the basic types match and it is an arithmetic type we are done */
4290         if (TYPE_ARITHMETIC(type)) {
4291                 result = new_type(qual_type, 0, 0);
4292         }
4293         /* If it is a pointer type recurse and keep testing */
4294         else if (type == TYPE_POINTER) {
4295                 result = compatible_types(left->left, right->left);
4296                 if (result) {
4297                         result = new_type(qual_type, result, 0);
4298                 }
4299         }
4300         /* test for struct/union equality */
4301         else if (type == TYPE_STRUCT) {
4302                 if (left->type_ident == right->type_ident) {
4303                         result = left;
4304                 }
4305         }
4306         /* Test for equivalent functions */
4307         else if (type == TYPE_FUNCTION) {
4308                 struct type *lf, *rf;
4309                 lf = compatible_types(left->left, right->left);
4310                 rf = compatible_types(left->right, right->right);
4311                 if (lf && rf) {
4312                         result = new_type(qual_type, lf, rf);
4313                 }
4314         }
4315         /* We only see TYPE_PRODUCT as part of function equivalence matching */
4316         else if (type == TYPE_PRODUCT) {
4317                 struct type *lf, *rf;
4318                 lf = compatible_types(left->left, right->left);
4319                 rf = compatible_types(left->right, right->right);
4320                 if (lf && rf) {
4321                         result = new_type(qual_type, lf, rf);
4322                 }
4323         }
4324         else {
4325                 /* Nothing else is compatible */
4326         }
4327         return result;
4328 }
4329
4330 static struct type *compatible_ptrs(struct type *left, struct type *right)
4331 {
4332         struct type *result;
4333         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
4334                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
4335                 return 0;
4336         }
4337         result = compatible_types(left->left, right->left);
4338         if (result) {
4339                 unsigned int qual_type;
4340                 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
4341                 result = new_type(qual_type, result, 0);
4342         }
4343         return result;
4344         
4345 }
4346 static struct triple *integral_promotion(
4347         struct compile_state *state, struct triple *def)
4348 {
4349         struct type *type;
4350         type = def->type;
4351         /* As all operations are carried out in registers
4352          * the values are converted on load I just convert
4353          * logical type of the operand.
4354          */
4355         if (TYPE_INTEGER(type->type)) {
4356                 unsigned int int_type;
4357                 int_type = type->type & ~TYPE_MASK;
4358                 int_type |= do_integral_promotion(type->type);
4359                 if (int_type != type->type) {
4360                         def->type = new_type(int_type, 0, 0);
4361                 }
4362         }
4363         return def;
4364 }
4365
4366
4367 static void arithmetic(struct compile_state *state, struct triple *def)
4368 {
4369         if (!TYPE_ARITHMETIC(def->type->type)) {
4370                 error(state, 0, "arithmetic type expexted");
4371         }
4372 }
4373
4374 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
4375 {
4376         if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
4377                 error(state, def, "pointer or arithmetic type expected");
4378         }
4379 }
4380
4381 static int is_integral(struct triple *ins)
4382 {
4383         return TYPE_INTEGER(ins->type->type);
4384 }
4385
4386 static void integral(struct compile_state *state, struct triple *def)
4387 {
4388         if (!is_integral(def)) {
4389                 error(state, 0, "integral type expected");
4390         }
4391 }
4392
4393
4394 static void bool(struct compile_state *state, struct triple *def)
4395 {
4396         if (!TYPE_ARITHMETIC(def->type->type) &&
4397                 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
4398                 error(state, 0, "arithmetic or pointer type expected");
4399         }
4400 }
4401
4402 static int is_signed(struct type *type)
4403 {
4404         return !!TYPE_SIGNED(type->type);
4405 }
4406
4407 /* Is this value located in a register otherwise it must be in memory */
4408 static int is_in_reg(struct compile_state *state, struct triple *def)
4409 {
4410         int in_reg;
4411         if (def->op == OP_ADECL) {
4412                 in_reg = 1;
4413         }
4414         else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
4415                 in_reg = 0;
4416         }
4417         else if (def->op == OP_VAL_VEC) {
4418                 in_reg = is_in_reg(state, RHS(def, 0));
4419         }
4420         else if (def->op == OP_DOT) {
4421                 in_reg = is_in_reg(state, RHS(def, 0));
4422         }
4423         else {
4424                 internal_error(state, 0, "unknown expr storage location");
4425                 in_reg = -1;
4426         }
4427         return in_reg;
4428 }
4429
4430 /* Is this a stable variable location otherwise it must be a temporary */
4431 static int is_stable(struct compile_state *state, struct triple *def)
4432 {
4433         int ret;
4434         ret = 0;
4435         if (!def) {
4436                 return 0;
4437         }
4438         if ((def->op == OP_ADECL) || 
4439                 (def->op == OP_SDECL) || 
4440                 (def->op == OP_DEREF) ||
4441                 (def->op == OP_BLOBCONST)) {
4442                 ret = 1;
4443         }
4444         else if (def->op == OP_DOT) {
4445                 ret = is_stable(state, RHS(def, 0));
4446         }
4447         else if (def->op == OP_VAL_VEC) {
4448                 struct triple **vector;
4449                 ulong_t i;
4450                 ret = 1;
4451                 vector = &RHS(def, 0);
4452                 for(i = 0; i < def->type->elements; i++) {
4453                         if (!is_stable(state, vector[i])) {
4454                                 ret = 0;
4455                                 break;
4456                         }
4457                 }
4458         }
4459         return ret;
4460 }
4461
4462 static int is_lvalue(struct compile_state *state, struct triple *def)
4463 {
4464         int ret;
4465         ret = 1;
4466         if (!def) {
4467                 return 0;
4468         }
4469         if (!is_stable(state, def)) {
4470                 return 0;
4471         }
4472         if (def->op == OP_DOT) {
4473                 ret = is_lvalue(state, RHS(def, 0));
4474         }
4475         return ret;
4476 }
4477
4478 static void clvalue(struct compile_state *state, struct triple *def)
4479 {
4480         if (!def) {
4481                 internal_error(state, def, "nothing where lvalue expected?");
4482         }
4483         if (!is_lvalue(state, def)) { 
4484                 error(state, def, "lvalue expected");
4485         }
4486 }
4487 static void lvalue(struct compile_state *state, struct triple *def)
4488 {
4489         clvalue(state, def);
4490         if (def->type->type & QUAL_CONST) {
4491                 error(state, def, "modifable lvalue expected");
4492         }
4493 }
4494
4495 static int is_pointer(struct triple *def)
4496 {
4497         return (def->type->type & TYPE_MASK) == TYPE_POINTER;
4498 }
4499
4500 static void pointer(struct compile_state *state, struct triple *def)
4501 {
4502         if (!is_pointer(def)) {
4503                 error(state, def, "pointer expected");
4504         }
4505 }
4506
4507 static struct triple *int_const(
4508         struct compile_state *state, struct type *type, ulong_t value)
4509 {
4510         struct triple *result;
4511         switch(type->type & TYPE_MASK) {
4512         case TYPE_CHAR:
4513         case TYPE_INT:   case TYPE_UINT:
4514         case TYPE_LONG:  case TYPE_ULONG:
4515                 break;
4516         default:
4517                 internal_error(state, 0, "constant for unkown type");
4518         }
4519         result = triple(state, OP_INTCONST, type, 0, 0);
4520         result->u.cval = value;
4521         return result;
4522 }
4523
4524
4525 static struct triple *do_mk_addr_expr(struct compile_state *state, 
4526         struct triple *expr, struct type *type, ulong_t offset)
4527 {
4528         struct triple *result;
4529         clvalue(state, expr);
4530
4531         type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
4532
4533         result = 0;
4534         if (expr->op == OP_ADECL) {
4535                 error(state, expr, "address of auto variables not supported");
4536         }
4537         else if (expr->op == OP_SDECL) {
4538                 result = triple(state, OP_ADDRCONST, type, 0, 0);
4539                 MISC(result, 0) = expr;
4540                 result->u.cval = offset;
4541         }
4542         else if (expr->op == OP_DEREF) {
4543                 result = triple(state, OP_ADD, type,
4544                         RHS(expr, 0),
4545                         int_const(state, &ulong_type, offset));
4546         }
4547         return result;
4548 }
4549
4550 static struct triple *mk_addr_expr(
4551         struct compile_state *state, struct triple *expr, ulong_t offset)
4552 {
4553         return do_mk_addr_expr(state, expr, expr->type, offset);
4554 }
4555
4556 static struct triple *mk_deref_expr(
4557         struct compile_state *state, struct triple *expr)
4558 {
4559         struct type *base_type;
4560         pointer(state, expr);
4561         base_type = expr->type->left;
4562         return triple(state, OP_DEREF, base_type, expr, 0);
4563 }
4564
4565 static struct triple *array_to_pointer(struct compile_state *state, struct triple *def)
4566 {
4567         if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4568                 struct type *type;
4569                 type = new_type(
4570                         TYPE_POINTER | (def->type->type & QUAL_MASK),
4571                         def->type->left, 0);
4572                 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
4573                         struct triple *addrconst;
4574                         if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
4575                                 internal_error(state, def, "bad array constant");
4576                         }
4577                         addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
4578                         MISC(addrconst, 0) = def;
4579                         def = addrconst;
4580                 }
4581                 else {
4582                         def = triple(state, OP_COPY, type, def, 0);
4583                 }
4584         }
4585         return def;
4586 }
4587
4588 static struct triple *deref_field(
4589         struct compile_state *state, struct triple *expr, struct hash_entry *field)
4590 {
4591         struct triple *result;
4592         struct type *type, *member;
4593         if (!field) {
4594                 internal_error(state, 0, "No field passed to deref_field");
4595         }
4596         result = 0;
4597         type = expr->type;
4598         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
4599                 error(state, 0, "request for member %s in something not a struct or union",
4600                         field->name);
4601         }
4602         member = field_type(state, type, field);
4603         if ((type->type & STOR_MASK) == STOR_PERM) {
4604                 /* Do the pointer arithmetic to get a deref the field */
4605                 ulong_t offset;
4606                 offset = field_offset(state, type, field);
4607                 result = do_mk_addr_expr(state, expr, member, offset);
4608                 result = mk_deref_expr(state, result);
4609         }
4610         else {
4611                 /* Find the variable for the field I want. */
4612                 result = triple(state, OP_DOT, member, expr, 0);
4613                 result->u.field = field;
4614         }
4615         return result;
4616 }
4617
4618 static struct triple *read_expr(struct compile_state *state, struct triple *def)
4619 {
4620         int op;
4621         if  (!def) {
4622                 return 0;
4623         }
4624         if (!is_stable(state, def)) {
4625                 return def;
4626         }
4627         /* Tranform an array to a pointer to the first element */
4628         
4629 #warning "CHECK_ME is this the right place to transform arrays to pointers?"
4630         if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
4631                 return array_to_pointer(state, def);
4632         }
4633         if (is_in_reg(state, def)) {
4634                 op = OP_READ;
4635         } else {
4636                 op = OP_LOAD;
4637         }
4638         return triple(state, op, def->type, def, 0);
4639 }
4640
4641 int is_write_compatible(struct compile_state *state, 
4642         struct type *dest, struct type *rval)
4643 {
4644         int compatible = 0;
4645         /* Both operands have arithmetic type */
4646         if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
4647                 compatible = 1;
4648         }
4649         /* One operand is a pointer and the other is a pointer to void */
4650         else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
4651                 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
4652                 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
4653                         ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
4654                 compatible = 1;
4655         }
4656         /* If both types are the same without qualifiers we are good */
4657         else if (equiv_ptrs(dest, rval)) {
4658                 compatible = 1;
4659         }
4660         /* test for struct/union equality  */
4661         else if (((dest->type & TYPE_MASK) == TYPE_STRUCT) &&
4662                 ((rval->type & TYPE_MASK) == TYPE_STRUCT) &&
4663                 (dest->type_ident == rval->type_ident)) {
4664                 compatible = 1;
4665         }
4666         return compatible;
4667 }
4668
4669
4670 static void write_compatible(struct compile_state *state,
4671         struct type *dest, struct type *rval)
4672 {
4673         if (!is_write_compatible(state, dest, rval)) {
4674                 error(state, 0, "Incompatible types in assignment");
4675         }
4676 }
4677
4678 static int is_init_compatible(struct compile_state *state,
4679         struct type *dest, struct type *rval)
4680 {
4681         int compatible = 0;
4682         if (is_write_compatible(state, dest, rval)) {
4683                 compatible = 1;
4684         }
4685         else if (equiv_types(dest, rval)) {
4686                 compatible = 1;
4687         }
4688         return compatible;
4689 }
4690
4691 static struct triple *write_expr(
4692         struct compile_state *state, struct triple *dest, struct triple *rval)
4693 {
4694         struct triple *def;
4695         int op;
4696
4697         def = 0;
4698         if (!rval) {
4699                 internal_error(state, 0, "missing rval");
4700         }
4701
4702         if (rval->op == OP_LIST) {
4703                 internal_error(state, 0, "expression of type OP_LIST?");
4704         }
4705         if (!is_lvalue(state, dest)) {
4706                 internal_error(state, 0, "writing to a non lvalue?");
4707         }
4708         if (dest->type->type & QUAL_CONST) {
4709                 internal_error(state, 0, "modifable lvalue expexted");
4710         }
4711
4712         write_compatible(state, dest->type, rval->type);
4713
4714         /* Now figure out which assignment operator to use */
4715         op = -1;
4716         if (is_in_reg(state, dest)) {
4717                 op = OP_WRITE;
4718         } else {
4719                 op = OP_STORE;
4720         }
4721         def = triple(state, op, dest->type, dest, rval);
4722         return def;
4723 }
4724
4725 static struct triple *init_expr(
4726         struct compile_state *state, struct triple *dest, struct triple *rval)
4727 {
4728         struct triple *def;
4729
4730         def = 0;
4731         if (!rval) {
4732                 internal_error(state, 0, "missing rval");
4733         }
4734         if ((dest->type->type & STOR_MASK) != STOR_PERM) {
4735                 rval = read_expr(state, rval);
4736                 def = write_expr(state, dest, rval);
4737         }
4738         else {
4739                 /* Fill in the array size if necessary */
4740                 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
4741                         ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
4742                         if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
4743                                 dest->type->elements = rval->type->elements;
4744                         }
4745                 }
4746                 if (!equiv_types(dest->type, rval->type)) {
4747                         error(state, 0, "Incompatible types in inializer");
4748                 }
4749                 MISC(dest, 0) = rval;
4750                 insert_triple(state, dest, rval);
4751                 rval->id |= TRIPLE_FLAG_FLATTENED;
4752                 use_triple(MISC(dest, 0), dest);
4753         }
4754         return def;
4755 }
4756
4757 struct type *arithmetic_result(
4758         struct compile_state *state, struct triple *left, struct triple *right)
4759 {
4760         struct type *type;
4761         /* Sanity checks to ensure I am working with arithmetic types */
4762         arithmetic(state, left);
4763         arithmetic(state, right);
4764         type = new_type(
4765                 do_arithmetic_conversion(
4766                         left->type->type, 
4767                         right->type->type), 0, 0);
4768         return type;
4769 }
4770
4771 struct type *ptr_arithmetic_result(
4772         struct compile_state *state, struct triple *left, struct triple *right)
4773 {
4774         struct type *type;
4775         /* Sanity checks to ensure I am working with the proper types */
4776         ptr_arithmetic(state, left);
4777         arithmetic(state, right);
4778         if (TYPE_ARITHMETIC(left->type->type) && 
4779                 TYPE_ARITHMETIC(right->type->type)) {
4780                 type = arithmetic_result(state, left, right);
4781         }
4782         else if (TYPE_PTR(left->type->type)) {
4783                 type = left->type;
4784         }
4785         else {
4786                 internal_error(state, 0, "huh?");
4787                 type = 0;
4788         }
4789         return type;
4790 }
4791
4792
4793 /* boolean helper function */
4794
4795 static struct triple *ltrue_expr(struct compile_state *state, 
4796         struct triple *expr)
4797 {
4798         switch(expr->op) {
4799         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
4800         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
4801         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
4802                 /* If the expression is already boolean do nothing */
4803                 break;
4804         default:
4805                 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
4806                 break;
4807         }
4808         return expr;
4809 }
4810
4811 static struct triple *lfalse_expr(struct compile_state *state, 
4812         struct triple *expr)
4813 {
4814         return triple(state, OP_LFALSE, &int_type, expr, 0);
4815 }
4816
4817 static struct triple *cond_expr(
4818         struct compile_state *state, 
4819         struct triple *test, struct triple *left, struct triple *right)
4820 {
4821         struct triple *def;
4822         struct type *result_type;
4823         unsigned int left_type, right_type;
4824         bool(state, test);
4825         left_type = left->type->type;
4826         right_type = right->type->type;
4827         result_type = 0;
4828         /* Both operands have arithmetic type */
4829         if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
4830                 result_type = arithmetic_result(state, left, right);
4831         }
4832         /* Both operands have void type */
4833         else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
4834                 ((right_type & TYPE_MASK) == TYPE_VOID)) {
4835                 result_type = &void_type;
4836         }
4837         /* pointers to the same type... */
4838         else if ((result_type = compatible_ptrs(left->type, right->type))) {
4839                 ;
4840         }
4841         /* Both operands are pointers and left is a pointer to void */
4842         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4843                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4844                 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4845                 result_type = right->type;
4846         }
4847         /* Both operands are pointers and right is a pointer to void */
4848         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
4849                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
4850                 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
4851                 result_type = left->type;
4852         }
4853         if (!result_type) {
4854                 error(state, 0, "Incompatible types in conditional expression");
4855         }
4856         /* Cleanup and invert the test */
4857         test = lfalse_expr(state, read_expr(state, test));
4858         def = new_triple(state, OP_COND, result_type, 0, 3);
4859         def->param[0] = test;
4860         def->param[1] = left;
4861         def->param[2] = right;
4862         return def;
4863 }
4864
4865
4866 static int expr_depth(struct compile_state *state, struct triple *ins)
4867 {
4868         int count;
4869         count = 0;
4870         if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
4871                 count = 0;
4872         }
4873         else if (ins->op == OP_DEREF) {
4874                 count = expr_depth(state, RHS(ins, 0)) - 1;
4875         }
4876         else if (ins->op == OP_VAL) {
4877                 count = expr_depth(state, RHS(ins, 0)) - 1;
4878         }
4879         else if (ins->op == OP_COMMA) {
4880                 int ldepth, rdepth;
4881                 ldepth = expr_depth(state, RHS(ins, 0));
4882                 rdepth = expr_depth(state, RHS(ins, 1));
4883                 count = (ldepth >= rdepth)? ldepth : rdepth;
4884         }
4885         else if (ins->op == OP_CALL) {
4886                 /* Don't figure the depth of a call just guess it is huge */
4887                 count = 1000;
4888         }
4889         else {
4890                 struct triple **expr;
4891                 expr = triple_rhs(state, ins, 0);
4892                 for(;expr; expr = triple_rhs(state, ins, expr)) {
4893                         if (*expr) {
4894                                 int depth;
4895                                 depth = expr_depth(state, *expr);
4896                                 if (depth > count) {
4897                                         count = depth;
4898                                 }
4899                         }
4900                 }
4901         }
4902         return count + 1;
4903 }
4904
4905 static struct triple *flatten(
4906         struct compile_state *state, struct triple *first, struct triple *ptr);
4907
4908 static struct triple *flatten_generic(
4909         struct compile_state *state, struct triple *first, struct triple *ptr)
4910 {
4911         struct rhs_vector {
4912                 int depth;
4913                 struct triple **ins;
4914         } vector[MAX_RHS];
4915         int i, rhs, lhs;
4916         /* Only operations with just a rhs should come here */
4917         rhs = TRIPLE_RHS(ptr->sizes);
4918         lhs = TRIPLE_LHS(ptr->sizes);
4919         if (TRIPLE_SIZE(ptr->sizes) != lhs + rhs) {
4920                 internal_error(state, ptr, "unexpected args for: %d %s",
4921                         ptr->op, tops(ptr->op));
4922         }
4923         /* Find the depth of the rhs elements */
4924         for(i = 0; i < rhs; i++) {
4925                 vector[i].ins = &RHS(ptr, i);
4926                 vector[i].depth = expr_depth(state, *vector[i].ins);
4927         }
4928         /* Selection sort the rhs */
4929         for(i = 0; i < rhs; i++) {
4930                 int j, max = i;
4931                 for(j = i + 1; j < rhs; j++ ) {
4932                         if (vector[j].depth > vector[max].depth) {
4933                                 max = j;
4934                         }
4935                 }
4936                 if (max != i) {
4937                         struct rhs_vector tmp;
4938                         tmp = vector[i];
4939                         vector[i] = vector[max];
4940                         vector[max] = tmp;
4941                 }
4942         }
4943         /* Now flatten the rhs elements */
4944         for(i = 0; i < rhs; i++) {
4945                 *vector[i].ins = flatten(state, first, *vector[i].ins);
4946                 use_triple(*vector[i].ins, ptr);
4947         }
4948         
4949         /* Now flatten the lhs elements */
4950         for(i = 0; i < lhs; i++) {
4951                 struct triple **ins = &LHS(ptr, i);
4952                 *ins = flatten(state, first, *ins);
4953                 use_triple(*ins, ptr);
4954         }
4955         return ptr;
4956 }
4957
4958 static struct triple *flatten_land(
4959         struct compile_state *state, struct triple *first, struct triple *ptr)
4960 {
4961         struct triple *left, *right;
4962         struct triple *val, *test, *jmp, *label1, *end;
4963
4964         /* Find the triples */
4965         left = RHS(ptr, 0);
4966         right = RHS(ptr, 1);
4967
4968         /* Generate the needed triples */
4969         end = label(state);
4970
4971         /* Thread the triples together */
4972         val          = flatten(state, first, variable(state, ptr->type));
4973         left         = flatten(state, first, write_expr(state, val, left));
4974         test         = flatten(state, first, 
4975                 lfalse_expr(state, read_expr(state, val)));
4976         jmp          = flatten(state, first, branch(state, end, test));
4977         label1       = flatten(state, first, label(state));
4978         right        = flatten(state, first, write_expr(state, val, right));
4979         TARG(jmp, 0) = flatten(state, first, end); 
4980         
4981         /* Now give the caller something to chew on */
4982         return read_expr(state, val);
4983 }
4984
4985 static struct triple *flatten_lor(
4986         struct compile_state *state, struct triple *first, struct triple *ptr)
4987 {
4988         struct triple *left, *right;
4989         struct triple *val, *jmp, *label1, *end;
4990
4991         /* Find the triples */
4992         left = RHS(ptr, 0);
4993         right = RHS(ptr, 1);
4994
4995         /* Generate the needed triples */
4996         end = label(state);
4997
4998         /* Thread the triples together */
4999         val          = flatten(state, first, variable(state, ptr->type));
5000         left         = flatten(state, first, write_expr(state, val, left));
5001         jmp          = flatten(state, first, branch(state, end, left));
5002         label1       = flatten(state, first, label(state));
5003         right        = flatten(state, first, write_expr(state, val, right));
5004         TARG(jmp, 0) = flatten(state, first, end);
5005        
5006         
5007         /* Now give the caller something to chew on */
5008         return read_expr(state, val);
5009 }
5010
5011 static struct triple *flatten_cond(
5012         struct compile_state *state, struct triple *first, struct triple *ptr)
5013 {
5014         struct triple *test, *left, *right;
5015         struct triple *val, *mv1, *jmp1, *label1, *mv2, *middle, *jmp2, *end;
5016
5017         /* Find the triples */
5018         test = RHS(ptr, 0);
5019         left = RHS(ptr, 1);
5020         right = RHS(ptr, 2);
5021
5022         /* Generate the needed triples */
5023         end = label(state);
5024         middle = label(state);
5025
5026         /* Thread the triples together */
5027         val           = flatten(state, first, variable(state, ptr->type));
5028         test          = flatten(state, first, test);
5029         jmp1          = flatten(state, first, branch(state, middle, test));
5030         label1        = flatten(state, first, label(state));
5031         left          = flatten(state, first, left);
5032         mv1           = flatten(state, first, write_expr(state, val, left));
5033         jmp2          = flatten(state, first, branch(state, end, 0));
5034         TARG(jmp1, 0) = flatten(state, first, middle);
5035         right         = flatten(state, first, right);
5036         mv2           = flatten(state, first, write_expr(state, val, right));
5037         TARG(jmp2, 0) = flatten(state, first, end);
5038         
5039         /* Now give the caller something to chew on */
5040         return read_expr(state, val);
5041 }
5042
5043 struct triple *copy_func(struct compile_state *state, struct triple *ofunc, 
5044         struct occurance *base_occurance)
5045 {
5046         struct triple *nfunc;
5047         struct triple *nfirst, *ofirst;
5048         struct triple *new, *old;
5049
5050 #if 0
5051         fprintf(stdout, "\n");
5052         loc(stdout, state, 0);
5053         fprintf(stdout, "\n__________ copy_func _________\n");
5054         print_triple(state, ofunc);
5055         fprintf(stdout, "__________ copy_func _________ done\n\n");
5056 #endif
5057
5058         /* Make a new copy of the old function */
5059         nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
5060         nfirst = 0;
5061         ofirst = old = RHS(ofunc, 0);
5062         do {
5063                 struct triple *new;
5064                 struct occurance *occurance;
5065                 int old_lhs, old_rhs;
5066                 old_lhs = TRIPLE_LHS(old->sizes);
5067                 old_rhs = TRIPLE_RHS(old->sizes);
5068                 occurance = inline_occurance(state, base_occurance, old->occurance);
5069                 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
5070                         occurance);
5071                 if (!triple_stores_block(state, new)) {
5072                         memcpy(&new->u, &old->u, sizeof(new->u));
5073                 }
5074                 if (!nfirst) {
5075                         RHS(nfunc, 0) = nfirst = new;
5076                 }
5077                 else {
5078                         insert_triple(state, nfirst, new);
5079                 }
5080                 new->id |= TRIPLE_FLAG_FLATTENED;
5081                 
5082                 /* During the copy remember new as user of old */
5083                 use_triple(old, new);
5084
5085                 /* Populate the return type if present */
5086                 if (old == MISC(ofunc, 0)) {
5087                         MISC(nfunc, 0) = new;
5088                 }
5089                 old = old->next;
5090         } while(old != ofirst);
5091
5092         /* Make a second pass to fix up any unresolved references */
5093         old = ofirst;
5094         new = nfirst;
5095         do {
5096                 struct triple **oexpr, **nexpr;
5097                 int count, i;
5098                 /* Lookup where the copy is, to join pointers */
5099                 count = TRIPLE_SIZE(old->sizes);
5100                 for(i = 0; i < count; i++) {
5101                         oexpr = &old->param[i];
5102                         nexpr = &new->param[i];
5103                         if (!*nexpr && *oexpr && (*oexpr)->use) {
5104                                 *nexpr = (*oexpr)->use->member;
5105                                 if (*nexpr == old) {
5106                                         internal_error(state, 0, "new == old?");
5107                                 }
5108                                 use_triple(*nexpr, new);
5109                         }
5110                         if (!*nexpr && *oexpr) {
5111                                 internal_error(state, 0, "Could not copy %d\n", i);
5112                         }
5113                 }
5114                 old = old->next;
5115                 new = new->next;
5116         } while((old != ofirst) && (new != nfirst));
5117         
5118         /* Make a third pass to cleanup the extra useses */
5119         old = ofirst;
5120         new = nfirst;
5121         do {
5122                 unuse_triple(old, new);
5123                 old = old->next;
5124                 new = new->next;
5125         } while ((old != ofirst) && (new != nfirst));
5126         return nfunc;
5127 }
5128
5129 static struct triple *flatten_call(
5130         struct compile_state *state, struct triple *first, struct triple *ptr)
5131 {
5132         /* Inline the function call */
5133         struct type *ptype;
5134         struct triple *ofunc, *nfunc, *nfirst, *param, *result;
5135         struct triple *end, *nend;
5136         int pvals, i;
5137
5138         /* Find the triples */
5139         ofunc = MISC(ptr, 0);
5140         if (ofunc->op != OP_LIST) {
5141                 internal_error(state, 0, "improper function");
5142         }
5143         nfunc = copy_func(state, ofunc, ptr->occurance);
5144         nfirst = RHS(nfunc, 0)->next;
5145         /* Prepend the parameter reading into the new function list */
5146         ptype = nfunc->type->right;
5147         param = RHS(nfunc, 0)->next;
5148         pvals = TRIPLE_RHS(ptr->sizes);
5149         for(i = 0; i < pvals; i++) {
5150                 struct type *atype;
5151                 struct triple *arg;
5152                 atype = ptype;
5153                 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
5154                         atype = ptype->left;
5155                 }
5156                 while((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
5157                         param = param->next;
5158                 }
5159                 arg = RHS(ptr, i);
5160                 flatten(state, nfirst, write_expr(state, param, arg));
5161                 ptype = ptype->right;
5162                 param = param->next;
5163         }
5164         result = 0;
5165         if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
5166                 result = read_expr(state, MISC(nfunc,0));
5167         }
5168 #if 0
5169         fprintf(stdout, "\n");
5170         loc(stdout, state, 0);
5171         fprintf(stdout, "\n__________ flatten_call _________\n");
5172         print_triple(state, nfunc);
5173         fprintf(stdout, "__________ flatten_call _________ done\n\n");
5174 #endif
5175
5176         /* Get rid of the extra triples */
5177         nfirst = RHS(nfunc, 0)->next;
5178         free_triple(state, RHS(nfunc, 0));
5179         RHS(nfunc, 0) = 0;
5180         free_triple(state, nfunc);
5181
5182         /* Append the new function list onto the return list */
5183         end = first->prev;
5184         nend = nfirst->prev;
5185         end->next    = nfirst;
5186         nfirst->prev = end;
5187         nend->next   = first;
5188         first->prev  = nend;
5189
5190         return result;
5191 }
5192
5193 static struct triple *flatten(
5194         struct compile_state *state, struct triple *first, struct triple *ptr)
5195 {
5196         struct triple *orig_ptr;
5197         if (!ptr)
5198                 return 0;
5199         do {
5200                 orig_ptr = ptr;
5201                 /* Only flatten triples once */
5202                 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
5203                         return ptr;
5204                 }
5205                 switch(ptr->op) {
5206                 case OP_COMMA:
5207                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5208                         ptr = RHS(ptr, 1);
5209                         break;
5210                 case OP_VAL:
5211                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5212                         return MISC(ptr, 0);
5213                         break;
5214                 case OP_LAND:
5215                         ptr = flatten_land(state, first, ptr);
5216                         break;
5217                 case OP_LOR:
5218                         ptr = flatten_lor(state, first, ptr);
5219                         break;
5220                 case OP_COND:
5221                         ptr = flatten_cond(state, first, ptr);
5222                         break;
5223                 case OP_CALL:
5224                         ptr = flatten_call(state, first, ptr);
5225                         break;
5226                 case OP_READ:
5227                 case OP_LOAD:
5228                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
5229                         use_triple(RHS(ptr, 0), ptr);
5230                         break;
5231                 case OP_BRANCH:
5232                         use_triple(TARG(ptr, 0), ptr);
5233                         if (TRIPLE_RHS(ptr->sizes)) {
5234                                 use_triple(RHS(ptr, 0), ptr);
5235                                 if (ptr->next != ptr) {
5236                                         use_triple(ptr->next, ptr);
5237                                 }
5238                         }
5239                         break;
5240                 case OP_BLOBCONST:
5241                         insert_triple(state, first, ptr);
5242                         ptr->id |= TRIPLE_FLAG_FLATTENED;
5243                         ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
5244                         use_triple(MISC(ptr, 0), ptr);
5245                         break;
5246                 case OP_DEREF:
5247                         /* Since OP_DEREF is just a marker delete it when I flatten it */
5248                         ptr = RHS(ptr, 0);
5249                         RHS(orig_ptr, 0) = 0;
5250                         free_triple(state, orig_ptr);
5251                         break;
5252                 case OP_DOT:
5253                 {
5254                         struct triple *base;
5255                         base = RHS(ptr, 0);
5256                         if (base->op == OP_DEREF) {
5257                                 struct triple *left;
5258                                 ulong_t offset;
5259                                 offset = field_offset(state, base->type, ptr->u.field);
5260                                 left = RHS(base, 0);
5261                                 ptr = triple(state, OP_ADD, left->type, 
5262                                         read_expr(state, left),
5263                                         int_const(state, &ulong_type, offset));
5264                                 free_triple(state, base);
5265                         }
5266                         else if (base->op == OP_VAL_VEC) {
5267                                 base = flatten(state, first, base);
5268                                 ptr = struct_field(state, base, ptr->u.field);
5269                         }
5270                         break;
5271                 }
5272                 case OP_PIECE:
5273                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5274                         use_triple(MISC(ptr, 0), ptr);
5275                         use_triple(ptr, MISC(ptr, 0));
5276                         break;
5277                 case OP_ADDRCONST:
5278                 case OP_SDECL:
5279                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
5280                         use_triple(MISC(ptr, 0), ptr);
5281                         break;
5282                 case OP_ADECL:
5283                         break;
5284                 default:
5285                         /* Flatten the easy cases we don't override */
5286                         ptr = flatten_generic(state, first, ptr);
5287                         break;
5288                 }
5289         } while(ptr && (ptr != orig_ptr));
5290         if (ptr) {
5291                 insert_triple(state, first, ptr);
5292                 ptr->id |= TRIPLE_FLAG_FLATTENED;
5293         }
5294         return ptr;
5295 }
5296
5297 static void release_expr(struct compile_state *state, struct triple *expr)
5298 {
5299         struct triple *head;
5300         head = label(state);
5301         flatten(state, head, expr);
5302         while(head->next != head) {
5303                 release_triple(state, head->next);
5304         }
5305         free_triple(state, head);
5306 }
5307
5308 static int replace_rhs_use(struct compile_state *state,
5309         struct triple *orig, struct triple *new, struct triple *use)
5310 {
5311         struct triple **expr;
5312         int found;
5313         found = 0;
5314         expr = triple_rhs(state, use, 0);
5315         for(;expr; expr = triple_rhs(state, use, expr)) {
5316                 if (*expr == orig) {
5317                         *expr = new;
5318                         found = 1;
5319                 }
5320         }
5321         if (found) {
5322                 unuse_triple(orig, use);
5323                 use_triple(new, use);
5324         }
5325         return found;
5326 }
5327
5328 static int replace_lhs_use(struct compile_state *state,
5329         struct triple *orig, struct triple *new, struct triple *use)
5330 {
5331         struct triple **expr;
5332         int found;
5333         found = 0;
5334         expr = triple_lhs(state, use, 0);
5335         for(;expr; expr = triple_lhs(state, use, expr)) {
5336                 if (*expr == orig) {
5337                         *expr = new;
5338                         found = 1;
5339                 }
5340         }
5341         if (found) {
5342                 unuse_triple(orig, use);
5343                 use_triple(new, use);
5344         }
5345         return found;
5346 }
5347
5348 static void propogate_use(struct compile_state *state,
5349         struct triple *orig, struct triple *new)
5350 {
5351         struct triple_set *user, *next;
5352         for(user = orig->use; user; user = next) {
5353                 struct triple *use;
5354                 int found;
5355                 next = user->next;
5356                 use = user->member;
5357                 found = 0;
5358                 found |= replace_rhs_use(state, orig, new, use);
5359                 found |= replace_lhs_use(state, orig, new, use);
5360                 if (!found) {
5361                         internal_error(state, use, "use without use");
5362                 }
5363         }
5364         if (orig->use) {
5365                 internal_error(state, orig, "used after propogate_use");
5366         }
5367 }
5368
5369 /*
5370  * Code generators
5371  * ===========================
5372  */
5373
5374 static struct triple *mk_add_expr(
5375         struct compile_state *state, struct triple *left, struct triple *right)
5376 {
5377         struct type *result_type;
5378         /* Put pointer operands on the left */
5379         if (is_pointer(right)) {
5380                 struct triple *tmp;
5381                 tmp = left;
5382                 left = right;
5383                 right = tmp;
5384         }
5385         left  = read_expr(state, left);
5386         right = read_expr(state, right);
5387         result_type = ptr_arithmetic_result(state, left, right);
5388         if (is_pointer(left)) {
5389                 right = triple(state, 
5390                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5391                         &ulong_type, 
5392                         right, 
5393                         int_const(state, &ulong_type, 
5394                                 size_of(state, left->type->left)));
5395         }
5396         return triple(state, OP_ADD, result_type, left, right);
5397 }
5398
5399 static struct triple *mk_sub_expr(
5400         struct compile_state *state, struct triple *left, struct triple *right)
5401 {
5402         struct type *result_type;
5403         result_type = ptr_arithmetic_result(state, left, right);
5404         left  = read_expr(state, left);
5405         right = read_expr(state, right);
5406         if (is_pointer(left)) {
5407                 right = triple(state, 
5408                         is_signed(right->type)? OP_SMUL : OP_UMUL, 
5409                         &ulong_type, 
5410                         right, 
5411                         int_const(state, &ulong_type, 
5412                                 size_of(state, left->type->left)));
5413         }
5414         return triple(state, OP_SUB, result_type, left, right);
5415 }
5416
5417 static struct triple *mk_pre_inc_expr(
5418         struct compile_state *state, struct triple *def)
5419 {
5420         struct triple *val;
5421         lvalue(state, def);
5422         val = mk_add_expr(state, def, int_const(state, &int_type, 1));
5423         return triple(state, OP_VAL, def->type,
5424                 write_expr(state, def, val),
5425                 val);
5426 }
5427
5428 static struct triple *mk_pre_dec_expr(
5429         struct compile_state *state, struct triple *def)
5430 {
5431         struct triple *val;
5432         lvalue(state, def);
5433         val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
5434         return triple(state, OP_VAL, def->type,
5435                 write_expr(state, def, val),
5436                 val);
5437 }
5438
5439 static struct triple *mk_post_inc_expr(
5440         struct compile_state *state, struct triple *def)
5441 {
5442         struct triple *val;
5443         lvalue(state, def);
5444         val = read_expr(state, def);
5445         return triple(state, OP_VAL, def->type,
5446                 write_expr(state, def,
5447                         mk_add_expr(state, val, int_const(state, &int_type, 1)))
5448                 , val);
5449 }
5450
5451 static struct triple *mk_post_dec_expr(
5452         struct compile_state *state, struct triple *def)
5453 {
5454         struct triple *val;
5455         lvalue(state, def);
5456         val = read_expr(state, def);
5457         return triple(state, OP_VAL, def->type, 
5458                 write_expr(state, def,
5459                         mk_sub_expr(state, val, int_const(state, &int_type, 1)))
5460                 , val);
5461 }
5462
5463 static struct triple *mk_subscript_expr(
5464         struct compile_state *state, struct triple *left, struct triple *right)
5465 {
5466         left  = read_expr(state, left);
5467         right = read_expr(state, right);
5468         if (!is_pointer(left) && !is_pointer(right)) {
5469                 error(state, left, "subscripted value is not a pointer");
5470         }
5471         return mk_deref_expr(state, mk_add_expr(state, left, right));
5472 }
5473
5474 static struct triple *mk_cast_expr(
5475         struct compile_state *state, struct type *type, struct triple *expr)
5476 {
5477         struct triple *def;
5478         def = read_expr(state, expr);
5479         def = triple(state, OP_COPY, type, def, 0);
5480         return def;
5481 }
5482
5483 /*
5484  * Compile time evaluation
5485  * ===========================
5486  */
5487 static int is_const(struct triple *ins)
5488 {
5489         return IS_CONST_OP(ins->op);
5490 }
5491
5492 static int constants_equal(struct compile_state *state, 
5493         struct triple *left, struct triple *right)
5494 {
5495         int equal;
5496         if (!is_const(left) || !is_const(right)) {
5497                 equal = 0;
5498         }
5499         else if (left->op != right->op) {
5500                 equal = 0;
5501         }
5502         else if (!equiv_types(left->type, right->type)) {
5503                 equal = 0;
5504         }
5505         else {
5506                 equal = 0;
5507                 switch(left->op) {
5508                 case OP_INTCONST:
5509                         if (left->u.cval == right->u.cval) {
5510                                 equal = 1;
5511                         }
5512                         break;
5513                 case OP_BLOBCONST:
5514                 {
5515                         size_t lsize, rsize;
5516                         lsize = size_of(state, left->type);
5517                         rsize = size_of(state, right->type);
5518                         if (lsize != rsize) {
5519                                 break;
5520                         }
5521                         if (memcmp(left->u.blob, right->u.blob, lsize) == 0) {
5522                                 equal = 1;
5523                         }
5524                         break;
5525                 }
5526                 case OP_ADDRCONST:
5527                         if ((MISC(left, 0) == MISC(right, 0)) &&
5528                                 (left->u.cval == right->u.cval)) {
5529                                 equal = 1;
5530                         }
5531                         break;
5532                 default:
5533                         internal_error(state, left, "uknown constant type");
5534                         break;
5535                 }
5536         }
5537         return equal;
5538 }
5539
5540 static int is_zero(struct triple *ins)
5541 {
5542         return is_const(ins) && (ins->u.cval == 0);
5543 }
5544
5545 static int is_one(struct triple *ins)
5546 {
5547         return is_const(ins) && (ins->u.cval == 1);
5548 }
5549
5550 static long_t bit_count(ulong_t value)
5551 {
5552         int count;
5553         int i;
5554         count = 0;
5555         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5556                 ulong_t mask;
5557                 mask = 1;
5558                 mask <<= i;
5559                 if (value & mask) {
5560                         count++;
5561                 }
5562         }
5563         return count;
5564         
5565 }
5566 static long_t bsr(ulong_t value)
5567 {
5568         int i;
5569         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
5570                 ulong_t mask;
5571                 mask = 1;
5572                 mask <<= i;
5573                 if (value & mask) {
5574                         return i;
5575                 }
5576         }
5577         return -1;
5578 }
5579
5580 static long_t bsf(ulong_t value)
5581 {
5582         int i;
5583         for(i = 0; i < (sizeof(ulong_t)*8); i++) {
5584                 ulong_t mask;
5585                 mask = 1;
5586                 mask <<= 1;
5587                 if (value & mask) {
5588                         return i;
5589                 }
5590         }
5591         return -1;
5592 }
5593
5594 static long_t log2(ulong_t value)
5595 {
5596         return bsr(value);
5597 }
5598
5599 static long_t tlog2(struct triple *ins)
5600 {
5601         return log2(ins->u.cval);
5602 }
5603
5604 static int is_pow2(struct triple *ins)
5605 {
5606         ulong_t value, mask;
5607         long_t log;
5608         if (!is_const(ins)) {
5609                 return 0;
5610         }
5611         value = ins->u.cval;
5612         log = log2(value);
5613         if (log == -1) {
5614                 return 0;
5615         }
5616         mask = 1;
5617         mask <<= log;
5618         return  ((value & mask) == value);
5619 }
5620
5621 static ulong_t read_const(struct compile_state *state,
5622         struct triple *ins, struct triple **expr)
5623 {
5624         struct triple *rhs;
5625         rhs = *expr;
5626         switch(rhs->type->type &TYPE_MASK) {
5627         case TYPE_CHAR:   
5628         case TYPE_SHORT:
5629         case TYPE_INT:
5630         case TYPE_LONG:
5631         case TYPE_UCHAR:   
5632         case TYPE_USHORT:  
5633         case TYPE_UINT:
5634         case TYPE_ULONG:
5635         case TYPE_POINTER:
5636                 break;
5637         default:
5638                 internal_error(state, rhs, "bad type to read_const\n");
5639                 break;
5640         }
5641         return rhs->u.cval;
5642 }
5643
5644 static long_t read_sconst(struct triple *ins, struct triple **expr)
5645 {
5646         struct triple *rhs;
5647         rhs = *expr;
5648         return (long_t)(rhs->u.cval);
5649 }
5650
5651 static void unuse_rhs(struct compile_state *state, struct triple *ins)
5652 {
5653         struct triple **expr;
5654         expr = triple_rhs(state, ins, 0);
5655         for(;expr;expr = triple_rhs(state, ins, expr)) {
5656                 if (*expr) {
5657                         unuse_triple(*expr, ins);
5658                         *expr = 0;
5659                 }
5660         }
5661 }
5662
5663 static void unuse_lhs(struct compile_state *state, struct triple *ins)
5664 {
5665         struct triple **expr;
5666         expr = triple_lhs(state, ins, 0);
5667         for(;expr;expr = triple_lhs(state, ins, expr)) {
5668                 unuse_triple(*expr, ins);
5669                 *expr = 0;
5670         }
5671 }
5672
5673 static void check_lhs(struct compile_state *state, struct triple *ins)
5674 {
5675         struct triple **expr;
5676         expr = triple_lhs(state, ins, 0);
5677         for(;expr;expr = triple_lhs(state, ins, expr)) {
5678                 internal_error(state, ins, "unexpected lhs");
5679         }
5680         
5681 }
5682 static void check_targ(struct compile_state *state, struct triple *ins)
5683 {
5684         struct triple **expr;
5685         expr = triple_targ(state, ins, 0);
5686         for(;expr;expr = triple_targ(state, ins, expr)) {
5687                 internal_error(state, ins, "unexpected targ");
5688         }
5689 }
5690
5691 static void wipe_ins(struct compile_state *state, struct triple *ins)
5692 {
5693         /* Becareful which instructions you replace the wiped
5694          * instruction with, as there are not enough slots
5695          * in all instructions to hold all others.
5696          */
5697         check_targ(state, ins);
5698         unuse_rhs(state, ins);
5699         unuse_lhs(state, ins);
5700 }
5701
5702 static void mkcopy(struct compile_state *state, 
5703         struct triple *ins, struct triple *rhs)
5704 {
5705         wipe_ins(state, ins);
5706         ins->op = OP_COPY;
5707         ins->sizes = TRIPLE_SIZES(0, 1, 0, 0);
5708         RHS(ins, 0) = rhs;
5709         use_triple(RHS(ins, 0), ins);
5710 }
5711
5712 static void mkconst(struct compile_state *state, 
5713         struct triple *ins, ulong_t value)
5714 {
5715         if (!is_integral(ins) && !is_pointer(ins)) {
5716                 internal_error(state, ins, "unknown type to make constant\n");
5717         }
5718         wipe_ins(state, ins);
5719         ins->op = OP_INTCONST;
5720         ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
5721         ins->u.cval = value;
5722 }
5723
5724 static void mkaddr_const(struct compile_state *state,
5725         struct triple *ins, struct triple *sdecl, ulong_t value)
5726 {
5727         if (sdecl->op != OP_SDECL) {
5728                 internal_error(state, ins, "bad base for addrconst");
5729         }
5730         wipe_ins(state, ins);
5731         ins->op = OP_ADDRCONST;
5732         ins->sizes = TRIPLE_SIZES(0, 0, 1, 0);
5733         MISC(ins, 0) = sdecl;
5734         ins->u.cval = value;
5735         use_triple(sdecl, ins);
5736 }
5737
5738 /* Transform multicomponent variables into simple register variables */
5739 static void flatten_structures(struct compile_state *state)
5740 {
5741         struct triple *ins, *first;
5742         first = RHS(state->main_function, 0);
5743         ins = first;
5744         /* Pass one expand structure values into valvecs.
5745          */
5746         ins = first;
5747         do {
5748                 struct triple *next;
5749                 next = ins->next;
5750                 if ((ins->type->type & TYPE_MASK) == TYPE_STRUCT) {
5751                         if (ins->op == OP_VAL_VEC) {
5752                                 /* Do nothing */
5753                         }
5754                         else if ((ins->op == OP_LOAD) || (ins->op == OP_READ)) {
5755                                 struct triple *def, **vector;
5756                                 struct type *tptr;
5757                                 int op;
5758                                 ulong_t i;
5759
5760                                 op = ins->op;
5761                                 def = RHS(ins, 0);
5762                                 get_occurance(ins->occurance);
5763                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5764                                         ins->occurance);
5765
5766                                 vector = &RHS(next, 0);
5767                                 tptr = next->type->left;
5768                                 for(i = 0; i < next->type->elements; i++) {
5769                                         struct triple *sfield;
5770                                         struct type *mtype;
5771                                         mtype = tptr;
5772                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5773                                                 mtype = mtype->left;
5774                                         }
5775                                         sfield = deref_field(state, def, mtype->field_ident);
5776                                         
5777                                         vector[i] = triple(
5778                                                 state, op, mtype, sfield, 0);
5779                                         put_occurance(vector[i]->occurance);
5780                                         get_occurance(next->occurance);
5781                                         vector[i]->occurance = next->occurance;
5782                                         tptr = tptr->right;
5783                                 }
5784                                 propogate_use(state, ins, next);
5785                                 flatten(state, ins, next);
5786                                 free_triple(state, ins);
5787                         }
5788                         else if ((ins->op == OP_STORE) || (ins->op == OP_WRITE)) {
5789                                 struct triple *src, *dst, **vector;
5790                                 struct type *tptr;
5791                                 int op;
5792                                 ulong_t i;
5793
5794                                 op = ins->op;
5795                                 src = RHS(ins, 1);
5796                                 dst = RHS(ins, 0);
5797                                 get_occurance(ins->occurance);
5798                                 next = alloc_triple(state, OP_VAL_VEC, ins->type, -1, -1,
5799                                         ins->occurance);
5800                                 
5801                                 vector = &RHS(next, 0);
5802                                 tptr = next->type->left;
5803                                 for(i = 0; i < ins->type->elements; i++) {
5804                                         struct triple *dfield, *sfield;
5805                                         struct type *mtype;
5806                                         mtype = tptr;
5807                                         if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
5808                                                 mtype = mtype->left;
5809                                         }
5810                                         sfield = deref_field(state, src, mtype->field_ident);
5811                                         dfield = deref_field(state, dst, mtype->field_ident);
5812                                         vector[i] = triple(
5813                                                 state, op, mtype, dfield, sfield);
5814                                         put_occurance(vector[i]->occurance);
5815                                         get_occurance(next->occurance);
5816                                         vector[i]->occurance = next->occurance;
5817                                         tptr = tptr->right;
5818                                 }
5819                                 propogate_use(state, ins, next);
5820                                 flatten(state, ins, next);
5821                                 free_triple(state, ins);
5822                         }
5823                 }
5824                 ins = next;
5825         } while(ins != first);
5826         /* Pass two flatten the valvecs.
5827          */
5828         ins = first;
5829         do {
5830                 struct triple *next;
5831                 next = ins->next;
5832                 if (ins->op == OP_VAL_VEC) {
5833                         release_triple(state, ins);
5834                 } 
5835                 ins = next;
5836         } while(ins != first);
5837         /* Pass three verify the state and set ->id to 0.
5838          */
5839         ins = first;
5840         do {
5841                 ins->id &= ~TRIPLE_FLAG_FLATTENED;
5842                 if ((ins->op != OP_BLOBCONST) && (ins->op != OP_SDECL) &&
5843                         ((ins->type->type & TYPE_MASK) == TYPE_STRUCT)) {
5844                         internal_error(state, ins, "STRUCT_TYPE remains?");
5845                 }
5846                 if (ins->op == OP_DOT) {
5847                         internal_error(state, ins, "OP_DOT remains?");
5848                 }
5849                 if (ins->op == OP_VAL_VEC) {
5850                         internal_error(state, ins, "OP_VAL_VEC remains?");
5851                 }
5852                 ins = ins->next;
5853         } while(ins != first);
5854 }
5855
5856 /* For those operations that cannot be simplified */
5857 static void simplify_noop(struct compile_state *state, struct triple *ins)
5858 {
5859         return;
5860 }
5861
5862 static void simplify_smul(struct compile_state *state, struct triple *ins)
5863 {
5864         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5865                 struct triple *tmp;
5866                 tmp = RHS(ins, 0);
5867                 RHS(ins, 0) = RHS(ins, 1);
5868                 RHS(ins, 1) = tmp;
5869         }
5870         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5871                 long_t left, right;
5872                 left  = read_sconst(ins, &RHS(ins, 0));
5873                 right = read_sconst(ins, &RHS(ins, 1));
5874                 mkconst(state, ins, left * right);
5875         }
5876         else if (is_zero(RHS(ins, 1))) {
5877                 mkconst(state, ins, 0);
5878         }
5879         else if (is_one(RHS(ins, 1))) {
5880                 mkcopy(state, ins, RHS(ins, 0));
5881         }
5882         else if (is_pow2(RHS(ins, 1))) {
5883                 struct triple *val;
5884                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5885                 ins->op = OP_SL;
5886                 insert_triple(state, ins, val);
5887                 unuse_triple(RHS(ins, 1), ins);
5888                 use_triple(val, ins);
5889                 RHS(ins, 1) = val;
5890         }
5891 }
5892
5893 static void simplify_umul(struct compile_state *state, struct triple *ins)
5894 {
5895         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
5896                 struct triple *tmp;
5897                 tmp = RHS(ins, 0);
5898                 RHS(ins, 0) = RHS(ins, 1);
5899                 RHS(ins, 1) = tmp;
5900         }
5901         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5902                 ulong_t left, right;
5903                 left  = read_const(state, ins, &RHS(ins, 0));
5904                 right = read_const(state, ins, &RHS(ins, 1));
5905                 mkconst(state, ins, left * right);
5906         }
5907         else if (is_zero(RHS(ins, 1))) {
5908                 mkconst(state, ins, 0);
5909         }
5910         else if (is_one(RHS(ins, 1))) {
5911                 mkcopy(state, ins, RHS(ins, 0));
5912         }
5913         else if (is_pow2(RHS(ins, 1))) {
5914                 struct triple *val;
5915                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5916                 ins->op = OP_SL;
5917                 insert_triple(state, ins, val);
5918                 unuse_triple(RHS(ins, 1), ins);
5919                 use_triple(val, ins);
5920                 RHS(ins, 1) = val;
5921         }
5922 }
5923
5924 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
5925 {
5926         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5927                 long_t left, right;
5928                 left  = read_sconst(ins, &RHS(ins, 0));
5929                 right = read_sconst(ins, &RHS(ins, 1));
5930                 mkconst(state, ins, left / right);
5931         }
5932         else if (is_zero(RHS(ins, 0))) {
5933                 mkconst(state, ins, 0);
5934         }
5935         else if (is_zero(RHS(ins, 1))) {
5936                 error(state, ins, "division by zero");
5937         }
5938         else if (is_one(RHS(ins, 1))) {
5939                 mkcopy(state, ins, RHS(ins, 0));
5940         }
5941         else if (is_pow2(RHS(ins, 1))) {
5942                 struct triple *val;
5943                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5944                 ins->op = OP_SSR;
5945                 insert_triple(state, ins, val);
5946                 unuse_triple(RHS(ins, 1), ins);
5947                 use_triple(val, ins);
5948                 RHS(ins, 1) = val;
5949         }
5950 }
5951
5952 static void simplify_udiv(struct compile_state *state, struct triple *ins)
5953 {
5954         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5955                 ulong_t left, right;
5956                 left  = read_const(state, ins, &RHS(ins, 0));
5957                 right = read_const(state, ins, &RHS(ins, 1));
5958                 mkconst(state, ins, left / right);
5959         }
5960         else if (is_zero(RHS(ins, 0))) {
5961                 mkconst(state, ins, 0);
5962         }
5963         else if (is_zero(RHS(ins, 1))) {
5964                 error(state, ins, "division by zero");
5965         }
5966         else if (is_one(RHS(ins, 1))) {
5967                 mkcopy(state, ins, RHS(ins, 0));
5968         }
5969         else if (is_pow2(RHS(ins, 1))) {
5970                 struct triple *val;
5971                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
5972                 ins->op = OP_USR;
5973                 insert_triple(state, ins, val);
5974                 unuse_triple(RHS(ins, 1), ins);
5975                 use_triple(val, ins);
5976                 RHS(ins, 1) = val;
5977         }
5978 }
5979
5980 static void simplify_smod(struct compile_state *state, struct triple *ins)
5981 {
5982         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
5983                 long_t left, right;
5984                 left  = read_const(state, ins, &RHS(ins, 0));
5985                 right = read_const(state, ins, &RHS(ins, 1));
5986                 mkconst(state, ins, left % right);
5987         }
5988         else if (is_zero(RHS(ins, 0))) {
5989                 mkconst(state, ins, 0);
5990         }
5991         else if (is_zero(RHS(ins, 1))) {
5992                 error(state, ins, "division by zero");
5993         }
5994         else if (is_one(RHS(ins, 1))) {
5995                 mkconst(state, ins, 0);
5996         }
5997         else if (is_pow2(RHS(ins, 1))) {
5998                 struct triple *val;
5999                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
6000                 ins->op = OP_AND;
6001                 insert_triple(state, ins, val);
6002                 unuse_triple(RHS(ins, 1), ins);
6003                 use_triple(val, ins);
6004                 RHS(ins, 1) = val;
6005         }
6006 }
6007 static void simplify_umod(struct compile_state *state, struct triple *ins)
6008 {
6009         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6010                 ulong_t left, right;
6011                 left  = read_const(state, ins, &RHS(ins, 0));
6012                 right = read_const(state, ins, &RHS(ins, 1));
6013                 mkconst(state, ins, left % right);
6014         }
6015         else if (is_zero(RHS(ins, 0))) {
6016                 mkconst(state, ins, 0);
6017         }
6018         else if (is_zero(RHS(ins, 1))) {
6019                 error(state, ins, "division by zero");
6020         }
6021         else if (is_one(RHS(ins, 1))) {
6022                 mkconst(state, ins, 0);
6023         }
6024         else if (is_pow2(RHS(ins, 1))) {
6025                 struct triple *val;
6026                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
6027                 ins->op = OP_AND;
6028                 insert_triple(state, ins, val);
6029                 unuse_triple(RHS(ins, 1), ins);
6030                 use_triple(val, ins);
6031                 RHS(ins, 1) = val;
6032         }
6033 }
6034
6035 static void simplify_add(struct compile_state *state, struct triple *ins)
6036 {
6037         /* start with the pointer on the left */
6038         if (is_pointer(RHS(ins, 1))) {
6039                 struct triple *tmp;
6040                 tmp = RHS(ins, 0);
6041                 RHS(ins, 0) = RHS(ins, 1);
6042                 RHS(ins, 1) = tmp;
6043         }
6044         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6045                 if (RHS(ins, 0)->op == OP_INTCONST) {
6046                         ulong_t left, right;
6047                         left  = read_const(state, ins, &RHS(ins, 0));
6048                         right = read_const(state, ins, &RHS(ins, 1));
6049                         mkconst(state, ins, left + right);
6050                 }
6051                 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
6052                         struct triple *sdecl;
6053                         ulong_t left, right;
6054                         sdecl = MISC(RHS(ins, 0), 0);
6055                         left  = RHS(ins, 0)->u.cval;
6056                         right = RHS(ins, 1)->u.cval;
6057                         mkaddr_const(state, ins, sdecl, left + right);
6058                 }
6059                 else {
6060                         internal_warning(state, ins, "Optimize me!");
6061                 }
6062         }
6063         else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
6064                 struct triple *tmp;
6065                 tmp = RHS(ins, 1);
6066                 RHS(ins, 1) = RHS(ins, 0);
6067                 RHS(ins, 0) = tmp;
6068         }
6069 }
6070
6071 static void simplify_sub(struct compile_state *state, struct triple *ins)
6072 {
6073         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6074                 if (RHS(ins, 0)->op == OP_INTCONST) {
6075                         ulong_t left, right;
6076                         left  = read_const(state, ins, &RHS(ins, 0));
6077                         right = read_const(state, ins, &RHS(ins, 1));
6078                         mkconst(state, ins, left - right);
6079                 }
6080                 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
6081                         struct triple *sdecl;
6082                         ulong_t left, right;
6083                         sdecl = MISC(RHS(ins, 0), 0);
6084                         left  = RHS(ins, 0)->u.cval;
6085                         right = RHS(ins, 1)->u.cval;
6086                         mkaddr_const(state, ins, sdecl, left - right);
6087                 }
6088                 else {
6089                         internal_warning(state, ins, "Optimize me!");
6090                 }
6091         }
6092 }
6093
6094 static void simplify_sl(struct compile_state *state, struct triple *ins)
6095 {
6096         if (is_const(RHS(ins, 1))) {
6097                 ulong_t right;
6098                 right = read_const(state, ins, &RHS(ins, 1));
6099                 if (right >= (size_of(state, ins->type)*8)) {
6100                         warning(state, ins, "left shift count >= width of type");
6101                 }
6102         }
6103         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6104                 ulong_t left, right;
6105                 left  = read_const(state, ins, &RHS(ins, 0));
6106                 right = read_const(state, ins, &RHS(ins, 1));
6107                 mkconst(state, ins,  left << right);
6108         }
6109 }
6110
6111 static void simplify_usr(struct compile_state *state, struct triple *ins)
6112 {
6113         if (is_const(RHS(ins, 1))) {
6114                 ulong_t right;
6115                 right = read_const(state, ins, &RHS(ins, 1));
6116                 if (right >= (size_of(state, ins->type)*8)) {
6117                         warning(state, ins, "right shift count >= width of type");
6118                 }
6119         }
6120         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6121                 ulong_t left, right;
6122                 left  = read_const(state, ins, &RHS(ins, 0));
6123                 right = read_const(state, ins, &RHS(ins, 1));
6124                 mkconst(state, ins, left >> right);
6125         }
6126 }
6127
6128 static void simplify_ssr(struct compile_state *state, struct triple *ins)
6129 {
6130         if (is_const(RHS(ins, 1))) {
6131                 ulong_t right;
6132                 right = read_const(state, ins, &RHS(ins, 1));
6133                 if (right >= (size_of(state, ins->type)*8)) {
6134                         warning(state, ins, "right shift count >= width of type");
6135                 }
6136         }
6137         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6138                 long_t left, right;
6139                 left  = read_sconst(ins, &RHS(ins, 0));
6140                 right = read_sconst(ins, &RHS(ins, 1));
6141                 mkconst(state, ins, left >> right);
6142         }
6143 }
6144
6145 static void simplify_and(struct compile_state *state, struct triple *ins)
6146 {
6147         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6148                 ulong_t left, right;
6149                 left  = read_const(state, ins, &RHS(ins, 0));
6150                 right = read_const(state, ins, &RHS(ins, 1));
6151                 mkconst(state, ins, left & right);
6152         }
6153 }
6154
6155 static void simplify_or(struct compile_state *state, struct triple *ins)
6156 {
6157         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6158                 ulong_t left, right;
6159                 left  = read_const(state, ins, &RHS(ins, 0));
6160                 right = read_const(state, ins, &RHS(ins, 1));
6161                 mkconst(state, ins, left | right);
6162         }
6163 }
6164
6165 static void simplify_xor(struct compile_state *state, struct triple *ins)
6166 {
6167         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6168                 ulong_t left, right;
6169                 left  = read_const(state, ins, &RHS(ins, 0));
6170                 right = read_const(state, ins, &RHS(ins, 1));
6171                 mkconst(state, ins, left ^ right);
6172         }
6173 }
6174
6175 static void simplify_pos(struct compile_state *state, struct triple *ins)
6176 {
6177         if (is_const(RHS(ins, 0))) {
6178                 mkconst(state, ins, RHS(ins, 0)->u.cval);
6179         }
6180         else {
6181                 mkcopy(state, ins, RHS(ins, 0));
6182         }
6183 }
6184
6185 static void simplify_neg(struct compile_state *state, struct triple *ins)
6186 {
6187         if (is_const(RHS(ins, 0))) {
6188                 ulong_t left;
6189                 left = read_const(state, ins, &RHS(ins, 0));
6190                 mkconst(state, ins, -left);
6191         }
6192         else if (RHS(ins, 0)->op == OP_NEG) {
6193                 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
6194         }
6195 }
6196
6197 static void simplify_invert(struct compile_state *state, struct triple *ins)
6198 {
6199         if (is_const(RHS(ins, 0))) {
6200                 ulong_t left;
6201                 left = read_const(state, ins, &RHS(ins, 0));
6202                 mkconst(state, ins, ~left);
6203         }
6204 }
6205
6206 static void simplify_eq(struct compile_state *state, struct triple *ins)
6207 {
6208         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6209                 ulong_t left, right;
6210                 left  = read_const(state, ins, &RHS(ins, 0));
6211                 right = read_const(state, ins, &RHS(ins, 1));
6212                 mkconst(state, ins, left == right);
6213         }
6214         else if (RHS(ins, 0) == RHS(ins, 1)) {
6215                 mkconst(state, ins, 1);
6216         }
6217 }
6218
6219 static void simplify_noteq(struct compile_state *state, struct triple *ins)
6220 {
6221         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6222                 ulong_t left, right;
6223                 left  = read_const(state, ins, &RHS(ins, 0));
6224                 right = read_const(state, ins, &RHS(ins, 1));
6225                 mkconst(state, ins, left != right);
6226         }
6227         else if (RHS(ins, 0) == RHS(ins, 1)) {
6228                 mkconst(state, ins, 0);
6229         }
6230 }
6231
6232 static void simplify_sless(struct compile_state *state, struct triple *ins)
6233 {
6234         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6235                 long_t left, right;
6236                 left  = read_sconst(ins, &RHS(ins, 0));
6237                 right = read_sconst(ins, &RHS(ins, 1));
6238                 mkconst(state, ins, left < right);
6239         }
6240         else if (RHS(ins, 0) == RHS(ins, 1)) {
6241                 mkconst(state, ins, 0);
6242         }
6243 }
6244
6245 static void simplify_uless(struct compile_state *state, struct triple *ins)
6246 {
6247         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6248                 ulong_t left, right;
6249                 left  = read_const(state, ins, &RHS(ins, 0));
6250                 right = read_const(state, ins, &RHS(ins, 1));
6251                 mkconst(state, ins, left < right);
6252         }
6253         else if (is_zero(RHS(ins, 0))) {
6254                 mkconst(state, ins, 1);
6255         }
6256         else if (RHS(ins, 0) == RHS(ins, 1)) {
6257                 mkconst(state, ins, 0);
6258         }
6259 }
6260
6261 static void simplify_smore(struct compile_state *state, struct triple *ins)
6262 {
6263         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6264                 long_t left, right;
6265                 left  = read_sconst(ins, &RHS(ins, 0));
6266                 right = read_sconst(ins, &RHS(ins, 1));
6267                 mkconst(state, ins, left > right);
6268         }
6269         else if (RHS(ins, 0) == RHS(ins, 1)) {
6270                 mkconst(state, ins, 0);
6271         }
6272 }
6273
6274 static void simplify_umore(struct compile_state *state, struct triple *ins)
6275 {
6276         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6277                 ulong_t left, right;
6278                 left  = read_const(state, ins, &RHS(ins, 0));
6279                 right = read_const(state, ins, &RHS(ins, 1));
6280                 mkconst(state, ins, left > right);
6281         }
6282         else if (is_zero(RHS(ins, 1))) {
6283                 mkconst(state, ins, 1);
6284         }
6285         else if (RHS(ins, 0) == RHS(ins, 1)) {
6286                 mkconst(state, ins, 0);
6287         }
6288 }
6289
6290
6291 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
6292 {
6293         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6294                 long_t left, right;
6295                 left  = read_sconst(ins, &RHS(ins, 0));
6296                 right = read_sconst(ins, &RHS(ins, 1));
6297                 mkconst(state, ins, left <= right);
6298         }
6299         else if (RHS(ins, 0) == RHS(ins, 1)) {
6300                 mkconst(state, ins, 1);
6301         }
6302 }
6303
6304 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
6305 {
6306         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6307                 ulong_t left, right;
6308                 left  = read_const(state, ins, &RHS(ins, 0));
6309                 right = read_const(state, ins, &RHS(ins, 1));
6310                 mkconst(state, ins, left <= right);
6311         }
6312         else if (is_zero(RHS(ins, 0))) {
6313                 mkconst(state, ins, 1);
6314         }
6315         else if (RHS(ins, 0) == RHS(ins, 1)) {
6316                 mkconst(state, ins, 1);
6317         }
6318 }
6319
6320 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
6321 {
6322         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 0))) {
6323                 long_t left, right;
6324                 left  = read_sconst(ins, &RHS(ins, 0));
6325                 right = read_sconst(ins, &RHS(ins, 1));
6326                 mkconst(state, ins, left >= right);
6327         }
6328         else if (RHS(ins, 0) == RHS(ins, 1)) {
6329                 mkconst(state, ins, 1);
6330         }
6331 }
6332
6333 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
6334 {
6335         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
6336                 ulong_t left, right;
6337                 left  = read_const(state, ins, &RHS(ins, 0));
6338                 right = read_const(state, ins, &RHS(ins, 1));
6339                 mkconst(state, ins, left >= right);
6340         }
6341         else if (is_zero(RHS(ins, 1))) {
6342                 mkconst(state, ins, 1);
6343         }
6344         else if (RHS(ins, 0) == RHS(ins, 1)) {
6345                 mkconst(state, ins, 1);
6346         }
6347 }
6348
6349 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
6350 {
6351         if (is_const(RHS(ins, 0))) {
6352                 ulong_t left;
6353                 left = read_const(state, ins, &RHS(ins, 0));
6354                 mkconst(state, ins, left == 0);
6355         }
6356         /* Otherwise if I am the only user... */
6357         else if ((RHS(ins, 0)->use->member == ins) && (RHS(ins, 0)->use->next == 0)) {
6358                 int need_copy = 1;
6359                 /* Invert a boolean operation */
6360                 switch(RHS(ins, 0)->op) {
6361                 case OP_LTRUE:   RHS(ins, 0)->op = OP_LFALSE;  break;
6362                 case OP_LFALSE:  RHS(ins, 0)->op = OP_LTRUE;   break;
6363                 case OP_EQ:      RHS(ins, 0)->op = OP_NOTEQ;   break;
6364                 case OP_NOTEQ:   RHS(ins, 0)->op = OP_EQ;      break;
6365                 case OP_SLESS:   RHS(ins, 0)->op = OP_SMOREEQ; break;
6366                 case OP_ULESS:   RHS(ins, 0)->op = OP_UMOREEQ; break;
6367                 case OP_SMORE:   RHS(ins, 0)->op = OP_SLESSEQ; break;
6368                 case OP_UMORE:   RHS(ins, 0)->op = OP_ULESSEQ; break;
6369                 case OP_SLESSEQ: RHS(ins, 0)->op = OP_SMORE;   break;
6370                 case OP_ULESSEQ: RHS(ins, 0)->op = OP_UMORE;   break;
6371                 case OP_SMOREEQ: RHS(ins, 0)->op = OP_SLESS;   break;
6372                 case OP_UMOREEQ: RHS(ins, 0)->op = OP_ULESS;   break;
6373                 default:
6374                         need_copy = 0;
6375                         break;
6376                 }
6377                 if (need_copy) {
6378                         mkcopy(state, ins, RHS(ins, 0));
6379                 }
6380         }
6381 }
6382
6383 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
6384 {
6385         if (is_const(RHS(ins, 0))) {
6386                 ulong_t left;
6387                 left = read_const(state, ins, &RHS(ins, 0));
6388                 mkconst(state, ins, left != 0);
6389         }
6390         else switch(RHS(ins, 0)->op) {
6391         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
6392         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
6393         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
6394                 mkcopy(state, ins, RHS(ins, 0));
6395         }
6396
6397 }
6398
6399 static void simplify_copy(struct compile_state *state, struct triple *ins)
6400 {
6401         if (is_const(RHS(ins, 0))) {
6402                 switch(RHS(ins, 0)->op) {
6403                 case OP_INTCONST:
6404                 {
6405                         ulong_t left;
6406                         left = read_const(state, ins, &RHS(ins, 0));
6407                         mkconst(state, ins, left);
6408                         break;
6409                 }
6410                 case OP_ADDRCONST:
6411                 {
6412                         struct triple *sdecl;
6413                         ulong_t offset;
6414                         sdecl  = MISC(RHS(ins, 0), 0);
6415                         offset = RHS(ins, 0)->u.cval;
6416                         mkaddr_const(state, ins, sdecl, offset);
6417                         break;
6418                 }
6419                 default:
6420                         internal_error(state, ins, "uknown constant");
6421                         break;
6422                 }
6423         }
6424 }
6425
6426 static void simplify_branch(struct compile_state *state, struct triple *ins)
6427 {
6428         struct block *block;
6429         if (ins->op != OP_BRANCH) {
6430                 internal_error(state, ins, "not branch");
6431         }
6432         if (ins->use != 0) {
6433                 internal_error(state, ins, "branch use");
6434         }
6435 #warning "FIXME implement simplify branch."
6436         /* The challenge here with simplify branch is that I need to 
6437          * make modifications to the control flow graph as well
6438          * as to the branch instruction itself.
6439          */
6440         block = ins->u.block;
6441         
6442         if (TRIPLE_RHS(ins->sizes) && is_const(RHS(ins, 0))) {
6443                 struct triple *targ;
6444                 ulong_t value;
6445                 value = read_const(state, ins, &RHS(ins, 0));
6446                 unuse_triple(RHS(ins, 0), ins);
6447                 targ = TARG(ins, 0);
6448                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 1);
6449                 if (value) {
6450                         unuse_triple(ins->next, ins);
6451                         TARG(ins, 0) = targ;
6452                 }
6453                 else {
6454                         unuse_triple(targ, ins);
6455                         TARG(ins, 0) = ins->next;
6456                 }
6457 #warning "FIXME handle the case of making a branch unconditional"
6458         }
6459         if (TARG(ins, 0) == ins->next) {
6460                 unuse_triple(ins->next, ins);
6461                 if (TRIPLE_RHS(ins->sizes)) {
6462                         unuse_triple(RHS(ins, 0), ins);
6463                         unuse_triple(ins->next, ins);
6464                 }
6465                 ins->sizes = TRIPLE_SIZES(0, 0, 0, 0);
6466                 ins->op = OP_NOOP;
6467                 if (ins->use) {
6468                         internal_error(state, ins, "noop use != 0");
6469                 }
6470 #warning "FIXME handle the case of killing a branch"
6471         }
6472 }
6473
6474 int phi_present(struct block *block)
6475 {
6476         struct triple *ptr;
6477         if (!block) {
6478                 return 0;
6479         }
6480         ptr = block->first;
6481         do {
6482                 if (ptr->op == OP_PHI) {
6483                         return 1;
6484                 }
6485                 ptr = ptr->next;
6486         } while(ptr != block->last);
6487         return 0;
6488 }
6489
6490 static void simplify_label(struct compile_state *state, struct triple *ins)
6491 {
6492 #warning "FIXME enable simplify_label"
6493         struct triple *first, *last;
6494         first = RHS(state->main_function, 0);
6495         last = first->prev;
6496         /* Ignore the first and last instructions */
6497         if ((ins == first) || (ins == last)) {
6498                 return;
6499         }
6500         if (ins->use == 0) {
6501                 ins->op = OP_NOOP;
6502         }
6503         else if (ins->prev->op == OP_LABEL) {
6504                 struct block *block;
6505                 block = ins->prev->u.block;
6506                 /* In general it is not safe to merge one label that
6507                  * imediately follows another.  The problem is that the empty
6508                  * looking block may have phi functions that depend on it.
6509                  */
6510                 if (!block || 
6511                         (!phi_present(block->left) && 
6512                         !phi_present(block->right))) 
6513                 {
6514                         struct triple_set *user, *next;
6515                         ins->op = OP_NOOP;
6516                         for(user = ins->use; user; user = next) {
6517                                 struct triple *use;
6518                                 next = user->next;
6519                                 use = user->member;
6520                                 if (TARG(use, 0) == ins) {
6521                                         TARG(use, 0) = ins->prev;
6522                                         unuse_triple(ins, use);
6523                                         use_triple(ins->prev, use);
6524                                 }
6525                         }
6526                         if (ins->use) {
6527                                 internal_error(state, ins, "noop use != 0");
6528                         }
6529                 }
6530         }
6531 }
6532
6533 static void simplify_phi(struct compile_state *state, struct triple *ins)
6534 {
6535         struct triple **expr;
6536         ulong_t value;
6537         expr = triple_rhs(state, ins, 0);
6538         if (!*expr || !is_const(*expr)) {
6539                 return;
6540         }
6541         value = read_const(state, ins, expr);
6542         for(;expr;expr = triple_rhs(state, ins, expr)) {
6543                 if (!*expr || !is_const(*expr)) {
6544                         return;
6545                 }
6546                 if (value != read_const(state, ins, expr)) {
6547                         return;
6548                 }
6549         }
6550         mkconst(state, ins, value);
6551 }
6552
6553
6554 static void simplify_bsf(struct compile_state *state, struct triple *ins)
6555 {
6556         if (is_const(RHS(ins, 0))) {
6557                 ulong_t left;
6558                 left = read_const(state, ins, &RHS(ins, 0));
6559                 mkconst(state, ins, bsf(left));
6560         }
6561 }
6562
6563 static void simplify_bsr(struct compile_state *state, struct triple *ins)
6564 {
6565         if (is_const(RHS(ins, 0))) {
6566                 ulong_t left;
6567                 left = read_const(state, ins, &RHS(ins, 0));
6568                 mkconst(state, ins, bsr(left));
6569         }
6570 }
6571
6572
6573 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
6574 static const simplify_t table_simplify[] = {
6575 #if 1
6576 #define simplify_sdivt    simplify_noop
6577 #define simplify_udivt    simplify_noop
6578 #endif
6579 #if 0
6580 #define simplify_smul     simplify_noop
6581 #define simplify_umul     simplify_noop
6582 #define simplify_sdiv     simplify_noop
6583 #define simplify_udiv     simplify_noop
6584 #define simplify_smod     simplify_noop
6585 #define simplify_umod     simplify_noop
6586 #endif
6587 #if 0
6588 #define simplify_add      simplify_noop
6589 #define simplify_sub      simplify_noop
6590 #endif
6591 #if 0
6592 #define simplify_sl       simplify_noop
6593 #define simplify_usr      simplify_noop
6594 #define simplify_ssr      simplify_noop
6595 #endif
6596 #if 0
6597 #define simplify_and      simplify_noop
6598 #define simplify_xor      simplify_noop
6599 #define simplify_or       simplify_noop
6600 #endif
6601 #if 0
6602 #define simplify_pos      simplify_noop
6603 #define simplify_neg      simplify_noop
6604 #define simplify_invert   simplify_noop
6605 #endif
6606
6607 #if 0
6608 #define simplify_eq       simplify_noop
6609 #define simplify_noteq    simplify_noop
6610 #endif
6611 #if 0
6612 #define simplify_sless    simplify_noop
6613 #define simplify_uless    simplify_noop
6614 #define simplify_smore    simplify_noop
6615 #define simplify_umore    simplify_noop
6616 #endif
6617 #if 0
6618 #define simplify_slesseq  simplify_noop
6619 #define simplify_ulesseq  simplify_noop
6620 #define simplify_smoreeq  simplify_noop
6621 #define simplify_umoreeq  simplify_noop
6622 #endif
6623 #if 0
6624 #define simplify_lfalse   simplify_noop
6625 #endif
6626 #if 0
6627 #define simplify_ltrue    simplify_noop
6628 #endif
6629
6630 #if 0
6631 #define simplify_copy     simplify_noop
6632 #endif
6633
6634 #if 0
6635 #define simplify_branch   simplify_noop
6636 #endif
6637 #if 1
6638 #define simplify_label    simplify_noop
6639 #endif
6640
6641 #if 0
6642 #define simplify_phi      simplify_noop
6643 #endif
6644
6645 #if 0
6646 #define simplify_bsf      simplify_noop
6647 #define simplify_bsr      simplify_noop
6648 #endif
6649
6650 [OP_SDIVT      ] = simplify_sdivt,
6651 [OP_UDIVT      ] = simplify_udivt,
6652 [OP_SMUL       ] = simplify_smul,
6653 [OP_UMUL       ] = simplify_umul,
6654 [OP_SDIV       ] = simplify_sdiv,
6655 [OP_UDIV       ] = simplify_udiv,
6656 [OP_SMOD       ] = simplify_smod,
6657 [OP_UMOD       ] = simplify_umod,
6658 [OP_ADD        ] = simplify_add,
6659 [OP_SUB        ] = simplify_sub,
6660 [OP_SL         ] = simplify_sl,
6661 [OP_USR        ] = simplify_usr,
6662 [OP_SSR        ] = simplify_ssr,
6663 [OP_AND        ] = simplify_and,
6664 [OP_XOR        ] = simplify_xor,
6665 [OP_OR         ] = simplify_or,
6666 [OP_POS        ] = simplify_pos,
6667 [OP_NEG        ] = simplify_neg,
6668 [OP_INVERT     ] = simplify_invert,
6669
6670 [OP_EQ         ] = simplify_eq,
6671 [OP_NOTEQ      ] = simplify_noteq,
6672 [OP_SLESS      ] = simplify_sless,
6673 [OP_ULESS      ] = simplify_uless,
6674 [OP_SMORE      ] = simplify_smore,
6675 [OP_UMORE      ] = simplify_umore,
6676 [OP_SLESSEQ    ] = simplify_slesseq,
6677 [OP_ULESSEQ    ] = simplify_ulesseq,
6678 [OP_SMOREEQ    ] = simplify_smoreeq,
6679 [OP_UMOREEQ    ] = simplify_umoreeq,
6680 [OP_LFALSE     ] = simplify_lfalse,
6681 [OP_LTRUE      ] = simplify_ltrue,
6682
6683 [OP_LOAD       ] = simplify_noop,
6684 [OP_STORE      ] = simplify_noop,
6685
6686 [OP_NOOP       ] = simplify_noop,
6687
6688 [OP_INTCONST   ] = simplify_noop,
6689 [OP_BLOBCONST  ] = simplify_noop,
6690 [OP_ADDRCONST  ] = simplify_noop,
6691
6692 [OP_WRITE      ] = simplify_noop,
6693 [OP_READ       ] = simplify_noop,
6694 [OP_COPY       ] = simplify_copy,
6695 [OP_PIECE      ] = simplify_noop,
6696 [OP_ASM        ] = simplify_noop,
6697
6698 [OP_DOT        ] = simplify_noop,
6699 [OP_VAL_VEC    ] = simplify_noop,
6700
6701 [OP_LIST       ] = simplify_noop,
6702 [OP_BRANCH     ] = simplify_branch,
6703 [OP_LABEL      ] = simplify_label,
6704 [OP_ADECL      ] = simplify_noop,
6705 [OP_SDECL      ] = simplify_noop,
6706 [OP_PHI        ] = simplify_phi,
6707
6708 [OP_INB        ] = simplify_noop,
6709 [OP_INW        ] = simplify_noop,
6710 [OP_INL        ] = simplify_noop,
6711 [OP_OUTB       ] = simplify_noop,
6712 [OP_OUTW       ] = simplify_noop,
6713 [OP_OUTL       ] = simplify_noop,
6714 [OP_BSF        ] = simplify_bsf,
6715 [OP_BSR        ] = simplify_bsr,
6716 [OP_RDMSR      ] = simplify_noop,
6717 [OP_WRMSR      ] = simplify_noop,                    
6718 [OP_HLT        ] = simplify_noop,
6719 };
6720
6721 static void simplify(struct compile_state *state, struct triple *ins)
6722 {
6723         int op;
6724         simplify_t do_simplify;
6725         do {
6726                 op = ins->op;
6727                 do_simplify = 0;
6728                 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
6729                         do_simplify = 0;
6730                 }
6731                 else {
6732                         do_simplify = table_simplify[op];
6733                 }
6734                 if (!do_simplify) {
6735                         internal_error(state, ins, "cannot simplify op: %d %s\n",
6736                                 op, tops(op));
6737                         return;
6738                 }
6739                 do_simplify(state, ins);
6740         } while(ins->op != op);
6741 }
6742
6743 static void simplify_all(struct compile_state *state)
6744 {
6745         struct triple *ins, *first;
6746         first = RHS(state->main_function, 0);
6747         ins = first;
6748         do {
6749                 simplify(state, ins);
6750                 ins = ins->next;
6751         }while(ins != first);
6752 }
6753
6754 /*
6755  * Builtins....
6756  * ============================
6757  */
6758
6759 static void register_builtin_function(struct compile_state *state,
6760         const char *name, int op, struct type *rtype, ...)
6761 {
6762         struct type *ftype, *atype, *param, **next;
6763         struct triple *def, *arg, *result, *work, *last, *first;
6764         struct hash_entry *ident;
6765         struct file_state file;
6766         int parameters;
6767         int name_len;
6768         va_list args;
6769         int i;
6770
6771         /* Dummy file state to get debug handling right */
6772         memset(&file, 0, sizeof(file));
6773         file.basename = "<built-in>";
6774         file.line = 1;
6775         file.report_line = 1;
6776         file.report_name = file.basename;
6777         file.prev = state->file;
6778         state->file = &file;
6779         state->function = name;
6780
6781         /* Find the Parameter count */
6782         valid_op(state, op);
6783         parameters = table_ops[op].rhs;
6784         if (parameters < 0 ) {
6785                 internal_error(state, 0, "Invalid builtin parameter count");
6786         }
6787
6788         /* Find the function type */
6789         ftype = new_type(TYPE_FUNCTION, rtype, 0);
6790         next = &ftype->right;
6791         va_start(args, rtype);
6792         for(i = 0; i < parameters; i++) {
6793                 atype = va_arg(args, struct type *);
6794                 if (!*next) {
6795                         *next = atype;
6796                 } else {
6797                         *next = new_type(TYPE_PRODUCT, *next, atype);
6798                         next = &((*next)->right);
6799                 }
6800         }
6801         if (!*next) {
6802                 *next = &void_type;
6803         }
6804         va_end(args);
6805
6806         /* Generate the needed triples */
6807         def = triple(state, OP_LIST, ftype, 0, 0);
6808         first = label(state);
6809         RHS(def, 0) = first;
6810
6811         /* Now string them together */
6812         param = ftype->right;
6813         for(i = 0; i < parameters; i++) {
6814                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6815                         atype = param->left;
6816                 } else {
6817                         atype = param;
6818                 }
6819                 arg = flatten(state, first, variable(state, atype));
6820                 param = param->right;
6821         }
6822         result = 0;
6823         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
6824                 result = flatten(state, first, variable(state, rtype));
6825         }
6826         MISC(def, 0) = result;
6827         work = new_triple(state, op, rtype, -1, parameters);
6828         for(i = 0, arg = first->next; i < parameters; i++, arg = arg->next) {
6829                 RHS(work, i) = read_expr(state, arg);
6830         }
6831         if (result && ((rtype->type & TYPE_MASK) == TYPE_STRUCT)) {
6832                 struct triple *val;
6833                 /* Populate the LHS with the target registers */
6834                 work = flatten(state, first, work);
6835                 work->type = &void_type;
6836                 param = rtype->left;
6837                 if (rtype->elements != TRIPLE_LHS(work->sizes)) {
6838                         internal_error(state, 0, "Invalid result type");
6839                 }
6840                 val = new_triple(state, OP_VAL_VEC, rtype, -1, -1);
6841                 for(i = 0; i < rtype->elements; i++) {
6842                         struct triple *piece;
6843                         atype = param;
6844                         if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
6845                                 atype = param->left;
6846                         }
6847                         if (!TYPE_ARITHMETIC(atype->type) &&
6848                                 !TYPE_PTR(atype->type)) {
6849                                 internal_error(state, 0, "Invalid lhs type");
6850                         }
6851                         piece = triple(state, OP_PIECE, atype, work, 0);
6852                         piece->u.cval = i;
6853                         LHS(work, i) = piece;
6854                         RHS(val, i) = piece;
6855                 }
6856                 work = val;
6857         }
6858         if (result) {
6859                 work = write_expr(state, result, work);
6860         }
6861         work = flatten(state, first, work);
6862         last = flatten(state, first, label(state));
6863         name_len = strlen(name);
6864         ident = lookup(state, name, name_len);
6865         symbol(state, ident, &ident->sym_ident, def, ftype);
6866         
6867         state->file = file.prev;
6868         state->function = 0;
6869 #if 0
6870         fprintf(stdout, "\n");
6871         loc(stdout, state, 0);
6872         fprintf(stdout, "\n__________ builtin_function _________\n");
6873         print_triple(state, def);
6874         fprintf(stdout, "__________ builtin_function _________ done\n\n");
6875 #endif
6876 }
6877
6878 static struct type *partial_struct(struct compile_state *state,
6879         const char *field_name, struct type *type, struct type *rest)
6880 {
6881         struct hash_entry *field_ident;
6882         struct type *result;
6883         int field_name_len;
6884
6885         field_name_len = strlen(field_name);
6886         field_ident = lookup(state, field_name, field_name_len);
6887
6888         result = clone_type(0, type);
6889         result->field_ident = field_ident;
6890
6891         if (rest) {
6892                 result = new_type(TYPE_PRODUCT, result, rest);
6893         }
6894         return result;
6895 }
6896
6897 static struct type *register_builtin_type(struct compile_state *state,
6898         const char *name, struct type *type)
6899 {
6900         struct hash_entry *ident;
6901         int name_len;
6902
6903         name_len = strlen(name);
6904         ident = lookup(state, name, name_len);
6905         
6906         if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6907                 ulong_t elements = 0;
6908                 struct type *field;
6909                 type = new_type(TYPE_STRUCT, type, 0);
6910                 field = type->left;
6911                 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
6912                         elements++;
6913                         field = field->right;
6914                 }
6915                 elements++;
6916                 symbol(state, ident, &ident->sym_struct, 0, type);
6917                 type->type_ident = ident;
6918                 type->elements = elements;
6919         }
6920         symbol(state, ident, &ident->sym_ident, 0, type);
6921         ident->tok = TOK_TYPE_NAME;
6922         return type;
6923 }
6924
6925
6926 static void register_builtins(struct compile_state *state)
6927 {
6928         struct type *div_type, *ldiv_type;
6929         struct type *udiv_type, *uldiv_type;
6930         struct type *msr_type;
6931
6932         div_type = register_builtin_type(state, "__builtin_div_t",
6933                 partial_struct(state, "quot", &int_type,
6934                 partial_struct(state, "rem",  &int_type, 0)));
6935         ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
6936                 partial_struct(state, "quot", &long_type,
6937                 partial_struct(state, "rem",  &long_type, 0)));
6938         udiv_type = register_builtin_type(state, "__builtin_udiv_t",
6939                 partial_struct(state, "quot", &uint_type,
6940                 partial_struct(state, "rem",  &uint_type, 0)));
6941         uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
6942                 partial_struct(state, "quot", &ulong_type,
6943                 partial_struct(state, "rem",  &ulong_type, 0)));
6944
6945         register_builtin_function(state, "__builtin_div",   OP_SDIVT, div_type,
6946                 &int_type, &int_type);
6947         register_builtin_function(state, "__builtin_ldiv",  OP_SDIVT, ldiv_type,
6948                 &long_type, &long_type);
6949         register_builtin_function(state, "__builtin_udiv",  OP_UDIVT, udiv_type,
6950                 &uint_type, &uint_type);
6951         register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
6952                 &ulong_type, &ulong_type);
6953
6954         register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type, 
6955                 &ushort_type);
6956         register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
6957                 &ushort_type);
6958         register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,   
6959                 &ushort_type);
6960
6961         register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, 
6962                 &uchar_type, &ushort_type);
6963         register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, 
6964                 &ushort_type, &ushort_type);
6965         register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, 
6966                 &uint_type, &ushort_type);
6967         
6968         register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, 
6969                 &int_type);
6970         register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, 
6971                 &int_type);
6972
6973         msr_type = register_builtin_type(state, "__builtin_msr_t",
6974                 partial_struct(state, "lo", &ulong_type,
6975                 partial_struct(state, "hi", &ulong_type, 0)));
6976
6977         register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
6978                 &ulong_type);
6979         register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
6980                 &ulong_type, &ulong_type, &ulong_type);
6981         
6982         register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, 
6983                 &void_type);
6984 }
6985
6986 static struct type *declarator(
6987         struct compile_state *state, struct type *type, 
6988         struct hash_entry **ident, int need_ident);
6989 static void decl(struct compile_state *state, struct triple *first);
6990 static struct type *specifier_qualifier_list(struct compile_state *state);
6991 static int isdecl_specifier(int tok);
6992 static struct type *decl_specifiers(struct compile_state *state);
6993 static int istype(int tok);
6994 static struct triple *expr(struct compile_state *state);
6995 static struct triple *assignment_expr(struct compile_state *state);
6996 static struct type *type_name(struct compile_state *state);
6997 static void statement(struct compile_state *state, struct triple *fist);
6998
6999 static struct triple *call_expr(
7000         struct compile_state *state, struct triple *func)
7001 {
7002         struct triple *def;
7003         struct type *param, *type;
7004         ulong_t pvals, index;
7005
7006         if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
7007                 error(state, 0, "Called object is not a function");
7008         }
7009         if (func->op != OP_LIST) {
7010                 internal_error(state, 0, "improper function");
7011         }
7012         eat(state, TOK_LPAREN);
7013         /* Find the return type without any specifiers */
7014         type = clone_type(0, func->type->left);
7015         def = new_triple(state, OP_CALL, func->type, -1, -1);
7016         def->type = type;
7017
7018         pvals = TRIPLE_RHS(def->sizes);
7019         MISC(def, 0) = func;
7020
7021         param = func->type->right;
7022         for(index = 0; index < pvals; index++) {
7023                 struct triple *val;
7024                 struct type *arg_type;
7025                 val = read_expr(state, assignment_expr(state));
7026                 arg_type = param;
7027                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
7028                         arg_type = param->left;
7029                 }
7030                 write_compatible(state, arg_type, val->type);
7031                 RHS(def, index) = val;
7032                 if (index != (pvals - 1)) {
7033                         eat(state, TOK_COMMA);
7034                         param = param->right;
7035                 }
7036         }
7037         eat(state, TOK_RPAREN);
7038         return def;
7039 }
7040
7041
7042 static struct triple *character_constant(struct compile_state *state)
7043 {
7044         struct triple *def;
7045         struct token *tk;
7046         const signed char *str, *end;
7047         int c;
7048         int str_len;
7049         eat(state, TOK_LIT_CHAR);
7050         tk = &state->token[0];
7051         str = tk->val.str + 1;
7052         str_len = tk->str_len - 2;
7053         if (str_len <= 0) {
7054                 error(state, 0, "empty character constant");
7055         }
7056         end = str + str_len;
7057         c = char_value(state, &str, end);
7058         if (str != end) {
7059                 error(state, 0, "multibyte character constant not supported");
7060         }
7061         def = int_const(state, &char_type, (ulong_t)((long_t)c));
7062         return def;
7063 }
7064
7065 static struct triple *string_constant(struct compile_state *state)
7066 {
7067         struct triple *def;
7068         struct token *tk;
7069         struct type *type;
7070         const signed char *str, *end;
7071         signed char *buf, *ptr;
7072         int str_len;
7073
7074         buf = 0;
7075         type = new_type(TYPE_ARRAY, &char_type, 0);
7076         type->elements = 0;
7077         /* The while loop handles string concatenation */
7078         do {
7079                 eat(state, TOK_LIT_STRING);
7080                 tk = &state->token[0];
7081                 str = tk->val.str + 1;
7082                 str_len = tk->str_len - 2;
7083                 if (str_len < 0) {
7084                         error(state, 0, "negative string constant length");
7085                 }
7086                 end = str + str_len;
7087                 ptr = buf;
7088                 buf = xmalloc(type->elements + str_len + 1, "string_constant");
7089                 memcpy(buf, ptr, type->elements);
7090                 ptr = buf + type->elements;
7091                 do {
7092                         *ptr++ = char_value(state, &str, end);
7093                 } while(str < end);
7094                 type->elements = ptr - buf;
7095         } while(peek(state) == TOK_LIT_STRING);
7096         *ptr = '\0';
7097         type->elements += 1;
7098         def = triple(state, OP_BLOBCONST, type, 0, 0);
7099         def->u.blob = buf;
7100         return def;
7101 }
7102
7103
7104 static struct triple *integer_constant(struct compile_state *state)
7105 {
7106         struct triple *def;
7107         unsigned long val;
7108         struct token *tk;
7109         char *end;
7110         int u, l, decimal;
7111         struct type *type;
7112
7113         eat(state, TOK_LIT_INT);
7114         tk = &state->token[0];
7115         errno = 0;
7116         decimal = (tk->val.str[0] != '0');
7117         val = strtoul(tk->val.str, &end, 0);
7118 #ifdef __x86_64__
7119         if ((val == UINT_MAX) && (errno == ERANGE)) {
7120 #else
7121         if ((val == ULONG_MAX) && (errno == ERANGE)) {
7122 #endif
7123                 error(state, 0, "Integer constant to large");
7124         }
7125         u = l = 0;
7126         if ((*end == 'u') || (*end == 'U')) {
7127                 u = 1;
7128                         end++;
7129         }
7130         if ((*end == 'l') || (*end == 'L')) {
7131                 l = 1;
7132                 end++;
7133         }
7134         if ((*end == 'u') || (*end == 'U')) {
7135                 u = 1;
7136                 end++;
7137         }
7138         if (*end) {
7139                 error(state, 0, "Junk at end of integer constant");
7140         }
7141         if (u && l)  {
7142                 type = &ulong_type;
7143         }
7144         else if (l) {
7145                 type = &long_type;
7146 #ifdef __x86_64__
7147                 if (!decimal && (val > INT_MAX)) {
7148 #else
7149                 if (!decimal && (val > LONG_MAX)) {
7150 #endif
7151                         type = &ulong_type;
7152                 }
7153         }
7154         else if (u) {
7155                 type = &uint_type;
7156                 if (val > UINT_MAX) {
7157                         type = &ulong_type;
7158                 }
7159         }
7160         else {
7161                 type = &int_type;
7162                 if (!decimal && (val > INT_MAX) && (val <= UINT_MAX)) {
7163                         type = &uint_type;
7164                 }
7165 #ifdef __x86_64__
7166                 else if (!decimal && (val > INT_MAX)) {
7167 #else
7168                 else if (!decimal && (val > LONG_MAX)) {
7169 #endif
7170                         type = &ulong_type;
7171                 }
7172                 else if (val > INT_MAX) {
7173                         type = &long_type;
7174                 }
7175         }
7176         def = int_const(state, type, val);
7177         return def;
7178 }
7179
7180 static struct triple *primary_expr(struct compile_state *state)
7181 {
7182         struct triple *def;
7183         int tok;
7184         tok = peek(state);
7185         switch(tok) {
7186         case TOK_IDENT:
7187         {
7188                 struct hash_entry *ident;
7189                 /* Here ident is either:
7190                  * a varable name
7191                  * a function name
7192                  * an enumeration constant.
7193                  */
7194                 eat(state, TOK_IDENT);
7195                 ident = state->token[0].ident;
7196                 if (!ident->sym_ident) {
7197                         error(state, 0, "%s undeclared", ident->name);
7198                 }
7199                 def = ident->sym_ident->def;
7200                 break;
7201         }
7202         case TOK_ENUM_CONST:
7203                 /* Here ident is an enumeration constant */
7204                 eat(state, TOK_ENUM_CONST);
7205                 def = 0;
7206                 FINISHME();
7207                 break;
7208         case TOK_LPAREN:
7209                 eat(state, TOK_LPAREN);
7210                 def = expr(state);
7211                 eat(state, TOK_RPAREN);
7212                 break;
7213         case TOK_LIT_INT:
7214                 def = integer_constant(state);
7215                 break;
7216         case TOK_LIT_FLOAT:
7217                 eat(state, TOK_LIT_FLOAT);
7218                 error(state, 0, "Floating point constants not supported");
7219                 def = 0;
7220                 FINISHME();
7221                 break;
7222         case TOK_LIT_CHAR:
7223                 def = character_constant(state);
7224                 break;
7225         case TOK_LIT_STRING:
7226                 def = string_constant(state);
7227                 break;
7228         default:
7229                 def = 0;
7230                 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
7231         }
7232         return def;
7233 }
7234
7235 static struct triple *postfix_expr(struct compile_state *state)
7236 {
7237         struct triple *def;
7238         int postfix;
7239         def = primary_expr(state);
7240         do {
7241                 struct triple *left;
7242                 int tok;
7243                 postfix = 1;
7244                 left = def;
7245                 switch((tok = peek(state))) {
7246                 case TOK_LBRACKET:
7247                         eat(state, TOK_LBRACKET);
7248                         def = mk_subscript_expr(state, left, expr(state));
7249                         eat(state, TOK_RBRACKET);
7250                         break;
7251                 case TOK_LPAREN:
7252                         def = call_expr(state, def);
7253                         break;
7254                 case TOK_DOT:
7255                 {
7256                         struct hash_entry *field;
7257                         eat(state, TOK_DOT);
7258                         eat(state, TOK_IDENT);
7259                         field = state->token[0].ident;
7260                         def = deref_field(state, def, field);
7261                         break;
7262                 }
7263                 case TOK_ARROW:
7264                 {
7265                         struct hash_entry *field;
7266                         eat(state, TOK_ARROW);
7267                         eat(state, TOK_IDENT);
7268                         field = state->token[0].ident;
7269                         def = mk_deref_expr(state, read_expr(state, def));
7270                         def = deref_field(state, def, field);
7271                         break;
7272                 }
7273                 case TOK_PLUSPLUS:
7274                         eat(state, TOK_PLUSPLUS);
7275                         def = mk_post_inc_expr(state, left);
7276                         break;
7277                 case TOK_MINUSMINUS:
7278                         eat(state, TOK_MINUSMINUS);
7279                         def = mk_post_dec_expr(state, left);
7280                         break;
7281                 default:
7282                         postfix = 0;
7283                         break;
7284                 }
7285         } while(postfix);
7286         return def;
7287 }
7288
7289 static struct triple *cast_expr(struct compile_state *state);
7290
7291 static struct triple *unary_expr(struct compile_state *state)
7292 {
7293         struct triple *def, *right;
7294         int tok;
7295         switch((tok = peek(state))) {
7296         case TOK_PLUSPLUS:
7297                 eat(state, TOK_PLUSPLUS);
7298                 def = mk_pre_inc_expr(state, unary_expr(state));
7299                 break;
7300         case TOK_MINUSMINUS:
7301                 eat(state, TOK_MINUSMINUS);
7302                 def = mk_pre_dec_expr(state, unary_expr(state));
7303                 break;
7304         case TOK_AND:
7305                 eat(state, TOK_AND);
7306                 def = mk_addr_expr(state, cast_expr(state), 0);
7307                 break;
7308         case TOK_STAR:
7309                 eat(state, TOK_STAR);
7310                 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
7311                 break;
7312         case TOK_PLUS:
7313                 eat(state, TOK_PLUS);
7314                 right = read_expr(state, cast_expr(state));
7315                 arithmetic(state, right);
7316                 def = integral_promotion(state, right);
7317                 break;
7318         case TOK_MINUS:
7319                 eat(state, TOK_MINUS);
7320                 right = read_expr(state, cast_expr(state));
7321                 arithmetic(state, right);
7322                 def = integral_promotion(state, right);
7323                 def = triple(state, OP_NEG, def->type, def, 0);
7324                 break;
7325         case TOK_TILDE:
7326                 eat(state, TOK_TILDE);
7327                 right = read_expr(state, cast_expr(state));
7328                 integral(state, right);
7329                 def = integral_promotion(state, right);
7330                 def = triple(state, OP_INVERT, def->type, def, 0);
7331                 break;
7332         case TOK_BANG:
7333                 eat(state, TOK_BANG);
7334                 right = read_expr(state, cast_expr(state));
7335                 bool(state, right);
7336                 def = lfalse_expr(state, right);
7337                 break;
7338         case TOK_SIZEOF:
7339         {
7340                 struct type *type;
7341                 int tok1, tok2;
7342                 eat(state, TOK_SIZEOF);
7343                 tok1 = peek(state);
7344                 tok2 = peek2(state);
7345                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7346                         eat(state, TOK_LPAREN);
7347                         type = type_name(state);
7348                         eat(state, TOK_RPAREN);
7349                 }
7350                 else {
7351                         struct triple *expr;
7352                         expr = unary_expr(state);
7353                         type = expr->type;
7354                         release_expr(state, expr);
7355                 }
7356                 def = int_const(state, &ulong_type, size_of(state, type));
7357                 break;
7358         }
7359         case TOK_ALIGNOF:
7360         {
7361                 struct type *type;
7362                 int tok1, tok2;
7363                 eat(state, TOK_ALIGNOF);
7364                 tok1 = peek(state);
7365                 tok2 = peek2(state);
7366                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7367                         eat(state, TOK_LPAREN);
7368                         type = type_name(state);
7369                         eat(state, TOK_RPAREN);
7370                 }
7371                 else {
7372                         struct triple *expr;
7373                         expr = unary_expr(state);
7374                         type = expr->type;
7375                         release_expr(state, expr);
7376                 }
7377                 def = int_const(state, &ulong_type, align_of(state, type));
7378                 break;
7379         }
7380         default:
7381                 def = postfix_expr(state);
7382                 break;
7383         }
7384         return def;
7385 }
7386
7387 static struct triple *cast_expr(struct compile_state *state)
7388 {
7389         struct triple *def;
7390         int tok1, tok2;
7391         tok1 = peek(state);
7392         tok2 = peek2(state);
7393         if ((tok1 == TOK_LPAREN) && istype(tok2)) {
7394                 struct type *type;
7395                 eat(state, TOK_LPAREN);
7396                 type = type_name(state);
7397                 eat(state, TOK_RPAREN);
7398                 def = mk_cast_expr(state, type, cast_expr(state));
7399         }
7400         else {
7401                 def = unary_expr(state);
7402         }
7403         return def;
7404 }
7405
7406 static struct triple *mult_expr(struct compile_state *state)
7407 {
7408         struct triple *def;
7409         int done;
7410         def = cast_expr(state);
7411         do {
7412                 struct triple *left, *right;
7413                 struct type *result_type;
7414                 int tok, op, sign;
7415                 done = 0;
7416                 switch(tok = (peek(state))) {
7417                 case TOK_STAR:
7418                 case TOK_DIV:
7419                 case TOK_MOD:
7420                         left = read_expr(state, def);
7421                         arithmetic(state, left);
7422
7423                         eat(state, tok);
7424
7425                         right = read_expr(state, cast_expr(state));
7426                         arithmetic(state, right);
7427
7428                         result_type = arithmetic_result(state, left, right);
7429                         sign = is_signed(result_type);
7430                         op = -1;
7431                         switch(tok) {
7432                         case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
7433                         case TOK_DIV:  op = sign? OP_SDIV : OP_UDIV; break;
7434                         case TOK_MOD:  op = sign? OP_SMOD : OP_UMOD; break;
7435                         }
7436                         def = triple(state, op, result_type, left, right);
7437                         break;
7438                 default:
7439                         done = 1;
7440                         break;
7441                 }
7442         } while(!done);
7443         return def;
7444 }
7445
7446 static struct triple *add_expr(struct compile_state *state)
7447 {
7448         struct triple *def;
7449         int done;
7450         def = mult_expr(state);
7451         do {
7452                 done = 0;
7453                 switch( peek(state)) {
7454                 case TOK_PLUS:
7455                         eat(state, TOK_PLUS);
7456                         def = mk_add_expr(state, def, mult_expr(state));
7457                         break;
7458                 case TOK_MINUS:
7459                         eat(state, TOK_MINUS);
7460                         def = mk_sub_expr(state, def, mult_expr(state));
7461                         break;
7462                 default:
7463                         done = 1;
7464                         break;
7465                 }
7466         } while(!done);
7467         return def;
7468 }
7469
7470 static struct triple *shift_expr(struct compile_state *state)
7471 {
7472         struct triple *def;
7473         int done;
7474         def = add_expr(state);
7475         do {
7476                 struct triple *left, *right;
7477                 int tok, op;
7478                 done = 0;
7479                 switch((tok = peek(state))) {
7480                 case TOK_SL:
7481                 case TOK_SR:
7482                         left = read_expr(state, def);
7483                         integral(state, left);
7484                         left = integral_promotion(state, left);
7485
7486                         eat(state, tok);
7487
7488                         right = read_expr(state, add_expr(state));
7489                         integral(state, right);
7490                         right = integral_promotion(state, right);
7491                         
7492                         op = (tok == TOK_SL)? OP_SL : 
7493                                 is_signed(left->type)? OP_SSR: OP_USR;
7494
7495                         def = triple(state, op, left->type, left, right);
7496                         break;
7497                 default:
7498                         done = 1;
7499                         break;
7500                 }
7501         } while(!done);
7502         return def;
7503 }
7504
7505 static struct triple *relational_expr(struct compile_state *state)
7506 {
7507 #warning "Extend relational exprs to work on more than arithmetic types"
7508         struct triple *def;
7509         int done;
7510         def = shift_expr(state);
7511         do {
7512                 struct triple *left, *right;
7513                 struct type *arg_type;
7514                 int tok, op, sign;
7515                 done = 0;
7516                 switch((tok = peek(state))) {
7517                 case TOK_LESS:
7518                 case TOK_MORE:
7519                 case TOK_LESSEQ:
7520                 case TOK_MOREEQ:
7521                         left = read_expr(state, def);
7522                         arithmetic(state, left);
7523
7524                         eat(state, tok);
7525
7526                         right = read_expr(state, shift_expr(state));
7527                         arithmetic(state, right);
7528
7529                         arg_type = arithmetic_result(state, left, right);
7530                         sign = is_signed(arg_type);
7531                         op = -1;
7532                         switch(tok) {
7533                         case TOK_LESS:   op = sign? OP_SLESS : OP_ULESS; break;
7534                         case TOK_MORE:   op = sign? OP_SMORE : OP_UMORE; break;
7535                         case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
7536                         case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
7537                         }
7538                         def = triple(state, op, &int_type, left, right);
7539                         break;
7540                 default:
7541                         done = 1;
7542                         break;
7543                 }
7544         } while(!done);
7545         return def;
7546 }
7547
7548 static struct triple *equality_expr(struct compile_state *state)
7549 {
7550 #warning "Extend equality exprs to work on more than arithmetic types"
7551         struct triple *def;
7552         int done;
7553         def = relational_expr(state);
7554         do {
7555                 struct triple *left, *right;
7556                 int tok, op;
7557                 done = 0;
7558                 switch((tok = peek(state))) {
7559                 case TOK_EQEQ:
7560                 case TOK_NOTEQ:
7561                         left = read_expr(state, def);
7562                         arithmetic(state, left);
7563                         eat(state, tok);
7564                         right = read_expr(state, relational_expr(state));
7565                         arithmetic(state, right);
7566                         op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
7567                         def = triple(state, op, &int_type, left, right);
7568                         break;
7569                 default:
7570                         done = 1;
7571                         break;
7572                 }
7573         } while(!done);
7574         return def;
7575 }
7576
7577 static struct triple *and_expr(struct compile_state *state)
7578 {
7579         struct triple *def;
7580         def = equality_expr(state);
7581         while(peek(state) == TOK_AND) {
7582                 struct triple *left, *right;
7583                 struct type *result_type;
7584                 left = read_expr(state, def);
7585                 integral(state, left);
7586                 eat(state, TOK_AND);
7587                 right = read_expr(state, equality_expr(state));
7588                 integral(state, right);
7589                 result_type = arithmetic_result(state, left, right);
7590                 def = triple(state, OP_AND, result_type, left, right);
7591         }
7592         return def;
7593 }
7594
7595 static struct triple *xor_expr(struct compile_state *state)
7596 {
7597         struct triple *def;
7598         def = and_expr(state);
7599         while(peek(state) == TOK_XOR) {
7600                 struct triple *left, *right;
7601                 struct type *result_type;
7602                 left = read_expr(state, def);
7603                 integral(state, left);
7604                 eat(state, TOK_XOR);
7605                 right = read_expr(state, and_expr(state));
7606                 integral(state, right);
7607                 result_type = arithmetic_result(state, left, right);
7608                 def = triple(state, OP_XOR, result_type, left, right);
7609         }
7610         return def;
7611 }
7612
7613 static struct triple *or_expr(struct compile_state *state)
7614 {
7615         struct triple *def;
7616         def = xor_expr(state);
7617         while(peek(state) == TOK_OR) {
7618                 struct triple *left, *right;
7619                 struct type *result_type;
7620                 left = read_expr(state, def);
7621                 integral(state, left);
7622                 eat(state, TOK_OR);
7623                 right = read_expr(state, xor_expr(state));
7624                 integral(state, right);
7625                 result_type = arithmetic_result(state, left, right);
7626                 def = triple(state, OP_OR, result_type, left, right);
7627         }
7628         return def;
7629 }
7630
7631 static struct triple *land_expr(struct compile_state *state)
7632 {
7633         struct triple *def;
7634         def = or_expr(state);
7635         while(peek(state) == TOK_LOGAND) {
7636                 struct triple *left, *right;
7637                 left = read_expr(state, def);
7638                 bool(state, left);
7639                 eat(state, TOK_LOGAND);
7640                 right = read_expr(state, or_expr(state));
7641                 bool(state, right);
7642
7643                 def = triple(state, OP_LAND, &int_type,
7644                         ltrue_expr(state, left),
7645                         ltrue_expr(state, right));
7646         }
7647         return def;
7648 }
7649
7650 static struct triple *lor_expr(struct compile_state *state)
7651 {
7652         struct triple *def;
7653         def = land_expr(state);
7654         while(peek(state) == TOK_LOGOR) {
7655                 struct triple *left, *right;
7656                 left = read_expr(state, def);
7657                 bool(state, left);
7658                 eat(state, TOK_LOGOR);
7659                 right = read_expr(state, land_expr(state));
7660                 bool(state, right);
7661                 
7662                 def = triple(state, OP_LOR, &int_type,
7663                         ltrue_expr(state, left),
7664                         ltrue_expr(state, right));
7665         }
7666         return def;
7667 }
7668
7669 static struct triple *conditional_expr(struct compile_state *state)
7670 {
7671         struct triple *def;
7672         def = lor_expr(state);
7673         if (peek(state) == TOK_QUEST) {
7674                 struct triple *test, *left, *right;
7675                 bool(state, def);
7676                 test = ltrue_expr(state, read_expr(state, def));
7677                 eat(state, TOK_QUEST);
7678                 left = read_expr(state, expr(state));
7679                 eat(state, TOK_COLON);
7680                 right = read_expr(state, conditional_expr(state));
7681
7682                 def = cond_expr(state, test, left, right);
7683         }
7684         return def;
7685 }
7686
7687 static struct triple *eval_const_expr(
7688         struct compile_state *state, struct triple *expr)
7689 {
7690         struct triple *def;
7691         if (is_const(expr)) {
7692                 def = expr;
7693         } 
7694         else {
7695                 /* If we don't start out as a constant simplify into one */
7696                 struct triple *head, *ptr;
7697                 head = label(state); /* dummy initial triple */
7698                 flatten(state, head, expr);
7699                 for(ptr = head->next; ptr != head; ptr = ptr->next) {
7700                         simplify(state, ptr);
7701                 }
7702                 /* Remove the constant value the tail of the list */
7703                 def = head->prev;
7704                 def->prev->next = def->next;
7705                 def->next->prev = def->prev;
7706                 def->next = def->prev = def;
7707                 if (!is_const(def)) {
7708                         error(state, 0, "Not a constant expression");
7709                 }
7710                 /* Free the intermediate expressions */
7711                 while(head->next != head) {
7712                         release_triple(state, head->next);
7713                 }
7714                 free_triple(state, head);
7715         }
7716         return def;
7717 }
7718
7719 static struct triple *constant_expr(struct compile_state *state)
7720 {
7721         return eval_const_expr(state, conditional_expr(state));
7722 }
7723
7724 static struct triple *assignment_expr(struct compile_state *state)
7725 {
7726         struct triple *def, *left, *right;
7727         int tok, op, sign;
7728         /* The C grammer in K&R shows assignment expressions
7729          * only taking unary expressions as input on their
7730          * left hand side.  But specifies the precedence of
7731          * assignemnt as the lowest operator except for comma.
7732          *
7733          * Allowing conditional expressions on the left hand side
7734          * of an assignement results in a grammar that accepts
7735          * a larger set of statements than standard C.   As long
7736          * as the subset of the grammar that is standard C behaves
7737          * correctly this should cause no problems.
7738          * 
7739          * For the extra token strings accepted by the grammar
7740          * none of them should produce a valid lvalue, so they
7741          * should not produce functioning programs.
7742          *
7743          * GCC has this bug as well, so surprises should be minimal.
7744          */
7745         def = conditional_expr(state);
7746         left = def;
7747         switch((tok = peek(state))) {
7748         case TOK_EQ:
7749                 lvalue(state, left);
7750                 eat(state, TOK_EQ);
7751                 def = write_expr(state, left, 
7752                         read_expr(state, assignment_expr(state)));
7753                 break;
7754         case TOK_TIMESEQ:
7755         case TOK_DIVEQ:
7756         case TOK_MODEQ:
7757                 lvalue(state, left);
7758                 arithmetic(state, left);
7759                 eat(state, tok);
7760                 right = read_expr(state, assignment_expr(state));
7761                 arithmetic(state, right);
7762
7763                 sign = is_signed(left->type);
7764                 op = -1;
7765                 switch(tok) {
7766                 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
7767                 case TOK_DIVEQ:   op = sign? OP_SDIV : OP_UDIV; break;
7768                 case TOK_MODEQ:   op = sign? OP_SMOD : OP_UMOD; break;
7769                 }
7770                 def = write_expr(state, left,
7771                         triple(state, op, left->type, 
7772                                 read_expr(state, left), right));
7773                 break;
7774         case TOK_PLUSEQ:
7775                 lvalue(state, left);
7776                 eat(state, TOK_PLUSEQ);
7777                 def = write_expr(state, left,
7778                         mk_add_expr(state, left, assignment_expr(state)));
7779                 break;
7780         case TOK_MINUSEQ:
7781                 lvalue(state, left);
7782                 eat(state, TOK_MINUSEQ);
7783                 def = write_expr(state, left,
7784                         mk_sub_expr(state, left, assignment_expr(state)));
7785                 break;
7786         case TOK_SLEQ:
7787         case TOK_SREQ:
7788         case TOK_ANDEQ:
7789         case TOK_XOREQ:
7790         case TOK_OREQ:
7791                 lvalue(state, left);
7792                 integral(state, left);
7793                 eat(state, tok);
7794                 right = read_expr(state, assignment_expr(state));
7795                 integral(state, right);
7796                 right = integral_promotion(state, right);
7797                 sign = is_signed(left->type);
7798                 op = -1;
7799                 switch(tok) {
7800                 case TOK_SLEQ:  op = OP_SL; break;
7801                 case TOK_SREQ:  op = sign? OP_SSR: OP_USR; break;
7802                 case TOK_ANDEQ: op = OP_AND; break;
7803                 case TOK_XOREQ: op = OP_XOR; break;
7804                 case TOK_OREQ:  op = OP_OR; break;
7805                 }
7806                 def = write_expr(state, left,
7807                         triple(state, op, left->type, 
7808                                 read_expr(state, left), right));
7809                 break;
7810         }
7811         return def;
7812 }
7813
7814 static struct triple *expr(struct compile_state *state)
7815 {
7816         struct triple *def;
7817         def = assignment_expr(state);
7818         while(peek(state) == TOK_COMMA) {
7819                 struct triple *left, *right;
7820                 left = def;
7821                 eat(state, TOK_COMMA);
7822                 right = assignment_expr(state);
7823                 def = triple(state, OP_COMMA, right->type, left, right);
7824         }
7825         return def;
7826 }
7827
7828 static void expr_statement(struct compile_state *state, struct triple *first)
7829 {
7830         if (peek(state) != TOK_SEMI) {
7831                 flatten(state, first, expr(state));
7832         }
7833         eat(state, TOK_SEMI);
7834 }
7835
7836 static void if_statement(struct compile_state *state, struct triple *first)
7837 {
7838         struct triple *test, *jmp1, *jmp2, *middle, *end;
7839
7840         jmp1 = jmp2 = middle = 0;
7841         eat(state, TOK_IF);
7842         eat(state, TOK_LPAREN);
7843         test = expr(state);
7844         bool(state, test);
7845         /* Cleanup and invert the test */
7846         test = lfalse_expr(state, read_expr(state, test));
7847         eat(state, TOK_RPAREN);
7848         /* Generate the needed pieces */
7849         middle = label(state);
7850         jmp1 = branch(state, middle, test);
7851         /* Thread the pieces together */
7852         flatten(state, first, test);
7853         flatten(state, first, jmp1);
7854         flatten(state, first, label(state));
7855         statement(state, first);
7856         if (peek(state) == TOK_ELSE) {
7857                 eat(state, TOK_ELSE);
7858                 /* Generate the rest of the pieces */
7859                 end = label(state);
7860                 jmp2 = branch(state, end, 0);
7861                 /* Thread them together */
7862                 flatten(state, first, jmp2);
7863                 flatten(state, first, middle);
7864                 statement(state, first);
7865                 flatten(state, first, end);
7866         }
7867         else {
7868                 flatten(state, first, middle);
7869         }
7870 }
7871
7872 static void for_statement(struct compile_state *state, struct triple *first)
7873 {
7874         struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
7875         struct triple *label1, *label2, *label3;
7876         struct hash_entry *ident;
7877
7878         eat(state, TOK_FOR);
7879         eat(state, TOK_LPAREN);
7880         head = test = tail = jmp1 = jmp2 = 0;
7881         if (peek(state) != TOK_SEMI) {
7882                 head = expr(state);
7883         } 
7884         eat(state, TOK_SEMI);
7885         if (peek(state) != TOK_SEMI) {
7886                 test = expr(state);
7887                 bool(state, test);
7888                 test = ltrue_expr(state, read_expr(state, test));
7889         }
7890         eat(state, TOK_SEMI);
7891         if (peek(state) != TOK_RPAREN) {
7892                 tail = expr(state);
7893         }
7894         eat(state, TOK_RPAREN);
7895         /* Generate the needed pieces */
7896         label1 = label(state);
7897         label2 = label(state);
7898         label3 = label(state);
7899         if (test) {
7900                 jmp1 = branch(state, label3, 0);
7901                 jmp2 = branch(state, label1, test);
7902         }
7903         else {
7904                 jmp2 = branch(state, label1, 0);
7905         }
7906         end = label(state);
7907         /* Remember where break and continue go */
7908         start_scope(state);
7909         ident = state->i_break;
7910         symbol(state, ident, &ident->sym_ident, end, end->type);
7911         ident = state->i_continue;
7912         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7913         /* Now include the body */
7914         flatten(state, first, head);
7915         flatten(state, first, jmp1);
7916         flatten(state, first, label1);
7917         statement(state, first);
7918         flatten(state, first, label2);
7919         flatten(state, first, tail);
7920         flatten(state, first, label3);
7921         flatten(state, first, test);
7922         flatten(state, first, jmp2);
7923         flatten(state, first, end);
7924         /* Cleanup the break/continue scope */
7925         end_scope(state);
7926 }
7927
7928 static void while_statement(struct compile_state *state, struct triple *first)
7929 {
7930         struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
7931         struct hash_entry *ident;
7932         eat(state, TOK_WHILE);
7933         eat(state, TOK_LPAREN);
7934         test = expr(state);
7935         bool(state, test);
7936         test = ltrue_expr(state, read_expr(state, test));
7937         eat(state, TOK_RPAREN);
7938         /* Generate the needed pieces */
7939         label1 = label(state);
7940         label2 = label(state);
7941         jmp1 = branch(state, label2, 0);
7942         jmp2 = branch(state, label1, test);
7943         end = label(state);
7944         /* Remember where break and continue go */
7945         start_scope(state);
7946         ident = state->i_break;
7947         symbol(state, ident, &ident->sym_ident, end, end->type);
7948         ident = state->i_continue;
7949         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7950         /* Thread them together */
7951         flatten(state, first, jmp1);
7952         flatten(state, first, label1);
7953         statement(state, first);
7954         flatten(state, first, label2);
7955         flatten(state, first, test);
7956         flatten(state, first, jmp2);
7957         flatten(state, first, end);
7958         /* Cleanup the break/continue scope */
7959         end_scope(state);
7960 }
7961
7962 static void do_statement(struct compile_state *state, struct triple *first)
7963 {
7964         struct triple *label1, *label2, *test, *end;
7965         struct hash_entry *ident;
7966         eat(state, TOK_DO);
7967         /* Generate the needed pieces */
7968         label1 = label(state);
7969         label2 = label(state);
7970         end = label(state);
7971         /* Remember where break and continue go */
7972         start_scope(state);
7973         ident = state->i_break;
7974         symbol(state, ident, &ident->sym_ident, end, end->type);
7975         ident = state->i_continue;
7976         symbol(state, ident, &ident->sym_ident, label2, label2->type);
7977         /* Now include the body */
7978         flatten(state, first, label1);
7979         statement(state, first);
7980         /* Cleanup the break/continue scope */
7981         end_scope(state);
7982         /* Eat the rest of the loop */
7983         eat(state, TOK_WHILE);
7984         eat(state, TOK_LPAREN);
7985         test = read_expr(state, expr(state));
7986         bool(state, test);
7987         eat(state, TOK_RPAREN);
7988         eat(state, TOK_SEMI);
7989         /* Thread the pieces together */
7990         test = ltrue_expr(state, test);
7991         flatten(state, first, label2);
7992         flatten(state, first, test);
7993         flatten(state, first, branch(state, label1, test));
7994         flatten(state, first, end);
7995 }
7996
7997
7998 static void return_statement(struct compile_state *state, struct triple *first)
7999 {
8000         struct triple *jmp, *mv, *dest, *var, *val;
8001         int last;
8002         eat(state, TOK_RETURN);
8003
8004 #warning "FIXME implement a more general excess branch elimination"
8005         val = 0;
8006         /* If we have a return value do some more work */
8007         if (peek(state) != TOK_SEMI) {
8008                 val = read_expr(state, expr(state));
8009         }
8010         eat(state, TOK_SEMI);
8011
8012         /* See if this last statement in a function */
8013         last = ((peek(state) == TOK_RBRACE) && 
8014                 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
8015
8016         /* Find the return variable */
8017         var = MISC(state->main_function, 0);
8018         /* Find the return destination */
8019         dest = RHS(state->main_function, 0)->prev;
8020         mv = jmp = 0;
8021         /* If needed generate a jump instruction */
8022         if (!last) {
8023                 jmp = branch(state, dest, 0);
8024         }
8025         /* If needed generate an assignment instruction */
8026         if (val) {
8027                 mv = write_expr(state, var, val);
8028         }
8029         /* Now put the code together */
8030         if (mv) {
8031                 flatten(state, first, mv);
8032                 flatten(state, first, jmp);
8033         }
8034         else if (jmp) {
8035                 flatten(state, first, jmp);
8036         }
8037 }
8038
8039 static void break_statement(struct compile_state *state, struct triple *first)
8040 {
8041         struct triple *dest;
8042         eat(state, TOK_BREAK);
8043         eat(state, TOK_SEMI);
8044         if (!state->i_break->sym_ident) {
8045                 error(state, 0, "break statement not within loop or switch");
8046         }
8047         dest = state->i_break->sym_ident->def;
8048         flatten(state, first, branch(state, dest, 0));
8049 }
8050
8051 static void continue_statement(struct compile_state *state, struct triple *first)
8052 {
8053         struct triple *dest;
8054         eat(state, TOK_CONTINUE);
8055         eat(state, TOK_SEMI);
8056         if (!state->i_continue->sym_ident) {
8057                 error(state, 0, "continue statement outside of a loop");
8058         }
8059         dest = state->i_continue->sym_ident->def;
8060         flatten(state, first, branch(state, dest, 0));
8061 }
8062
8063 static void goto_statement(struct compile_state *state, struct triple *first)
8064 {
8065         struct hash_entry *ident;
8066         eat(state, TOK_GOTO);
8067         eat(state, TOK_IDENT);
8068         ident = state->token[0].ident;
8069         if (!ident->sym_label) {
8070                 /* If this is a forward branch allocate the label now,
8071                  * it will be flattend in the appropriate location later.
8072                  */
8073                 struct triple *ins;
8074                 ins = label(state);
8075                 label_symbol(state, ident, ins);
8076         }
8077         eat(state, TOK_SEMI);
8078
8079         flatten(state, first, branch(state, ident->sym_label->def, 0));
8080 }
8081
8082 static void labeled_statement(struct compile_state *state, struct triple *first)
8083 {
8084         struct triple *ins;
8085         struct hash_entry *ident;
8086         eat(state, TOK_IDENT);
8087
8088         ident = state->token[0].ident;
8089         if (ident->sym_label && ident->sym_label->def) {
8090                 ins = ident->sym_label->def;
8091                 put_occurance(ins->occurance);
8092                 ins->occurance = new_occurance(state);
8093         }
8094         else {
8095                 ins = label(state);
8096                 label_symbol(state, ident, ins);
8097         }
8098         if (ins->id & TRIPLE_FLAG_FLATTENED) {
8099                 error(state, 0, "label %s already defined", ident->name);
8100         }
8101         flatten(state, first, ins);
8102
8103         eat(state, TOK_COLON);
8104         statement(state, first);
8105 }
8106
8107 static void switch_statement(struct compile_state *state, struct triple *first)
8108 {
8109         FINISHME();
8110         eat(state, TOK_SWITCH);
8111         eat(state, TOK_LPAREN);
8112         expr(state);
8113         eat(state, TOK_RPAREN);
8114         statement(state, first);
8115         error(state, 0, "switch statements are not implemented");
8116         FINISHME();
8117 }
8118
8119 static void case_statement(struct compile_state *state, struct triple *first)
8120 {
8121         FINISHME();
8122         eat(state, TOK_CASE);
8123         constant_expr(state);
8124         eat(state, TOK_COLON);
8125         statement(state, first);
8126         error(state, 0, "case statements are not implemented");
8127         FINISHME();
8128 }
8129
8130 static void default_statement(struct compile_state *state, struct triple *first)
8131 {
8132         FINISHME();
8133         eat(state, TOK_DEFAULT);
8134         eat(state, TOK_COLON);
8135         statement(state, first);
8136         error(state, 0, "default statements are not implemented");
8137         FINISHME();
8138 }
8139
8140 static void asm_statement(struct compile_state *state, struct triple *first)
8141 {
8142         struct asm_info *info;
8143         struct {
8144                 struct triple *constraint;
8145                 struct triple *expr;
8146         } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
8147         struct triple *def, *asm_str;
8148         int out, in, clobbers, more, colons, i;
8149
8150         eat(state, TOK_ASM);
8151         /* For now ignore the qualifiers */
8152         switch(peek(state)) {
8153         case TOK_CONST:
8154                 eat(state, TOK_CONST);
8155                 break;
8156         case TOK_VOLATILE:
8157                 eat(state, TOK_VOLATILE);
8158                 break;
8159         }
8160         eat(state, TOK_LPAREN);
8161         asm_str = string_constant(state);
8162
8163         colons = 0;
8164         out = in = clobbers = 0;
8165         /* Outputs */
8166         if ((colons == 0) && (peek(state) == TOK_COLON)) {
8167                 eat(state, TOK_COLON);
8168                 colons++;
8169                 more = (peek(state) == TOK_LIT_STRING);
8170                 while(more) {
8171                         struct triple *var;
8172                         struct triple *constraint;
8173                         char *str;
8174                         more = 0;
8175                         if (out > MAX_LHS) {
8176                                 error(state, 0, "Maximum output count exceeded.");
8177                         }
8178                         constraint = string_constant(state);
8179                         str = constraint->u.blob;
8180                         if (str[0] != '=') {
8181                                 error(state, 0, "Output constraint does not start with =");
8182                         }
8183                         constraint->u.blob = str + 1;
8184                         eat(state, TOK_LPAREN);
8185                         var = conditional_expr(state);
8186                         eat(state, TOK_RPAREN);
8187
8188                         lvalue(state, var);
8189                         out_param[out].constraint = constraint;
8190                         out_param[out].expr       = var;
8191                         if (peek(state) == TOK_COMMA) {
8192                                 eat(state, TOK_COMMA);
8193                                 more = 1;
8194                         }
8195                         out++;
8196                 }
8197         }
8198         /* Inputs */
8199         if ((colons == 1) && (peek(state) == TOK_COLON)) {
8200                 eat(state, TOK_COLON);
8201                 colons++;
8202                 more = (peek(state) == TOK_LIT_STRING);
8203                 while(more) {
8204                         struct triple *val;
8205                         struct triple *constraint;
8206                         char *str;
8207                         more = 0;
8208                         if (in > MAX_RHS) {
8209                                 error(state, 0, "Maximum input count exceeded.");
8210                         }
8211                         constraint = string_constant(state);
8212                         str = constraint->u.blob;
8213                         if (digitp(str[0] && str[1] == '\0')) {
8214                                 int val;
8215                                 val = digval(str[0]);
8216                                 if ((val < 0) || (val >= out)) {
8217                                         error(state, 0, "Invalid input constraint %d", val);
8218                                 }
8219                         }
8220                         eat(state, TOK_LPAREN);
8221                         val = conditional_expr(state);
8222                         eat(state, TOK_RPAREN);
8223
8224                         in_param[in].constraint = constraint;
8225                         in_param[in].expr       = val;
8226                         if (peek(state) == TOK_COMMA) {
8227                                 eat(state, TOK_COMMA);
8228                                 more = 1;
8229                         }
8230                         in++;
8231                 }
8232         }
8233
8234         /* Clobber */
8235         if ((colons == 2) && (peek(state) == TOK_COLON)) {
8236                 eat(state, TOK_COLON);
8237                 colons++;
8238                 more = (peek(state) == TOK_LIT_STRING);
8239                 while(more) {
8240                         struct triple *clobber;
8241                         more = 0;
8242                         if ((clobbers + out) > MAX_LHS) {
8243                                 error(state, 0, "Maximum clobber limit exceeded.");
8244                         }
8245                         clobber = string_constant(state);
8246
8247                         clob_param[clobbers].constraint = clobber;
8248                         if (peek(state) == TOK_COMMA) {
8249                                 eat(state, TOK_COMMA);
8250                                 more = 1;
8251                         }
8252                         clobbers++;
8253                 }
8254         }
8255         eat(state, TOK_RPAREN);
8256         eat(state, TOK_SEMI);
8257
8258
8259         info = xcmalloc(sizeof(*info), "asm_info");
8260         info->str = asm_str->u.blob;
8261         free_triple(state, asm_str);
8262
8263         def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
8264         def->u.ainfo = info;
8265
8266         /* Find the register constraints */
8267         for(i = 0; i < out; i++) {
8268                 struct triple *constraint;
8269                 constraint = out_param[i].constraint;
8270                 info->tmpl.lhs[i] = arch_reg_constraint(state, 
8271                         out_param[i].expr->type, constraint->u.blob);
8272                 free_triple(state, constraint);
8273         }
8274         for(; i - out < clobbers; i++) {
8275                 struct triple *constraint;
8276                 constraint = clob_param[i - out].constraint;
8277                 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
8278                 free_triple(state, constraint);
8279         }
8280         for(i = 0; i < in; i++) {
8281                 struct triple *constraint;
8282                 const char *str;
8283                 constraint = in_param[i].constraint;
8284                 str = constraint->u.blob;
8285                 if (digitp(str[0]) && str[1] == '\0') {
8286                         struct reg_info cinfo;
8287                         int val;
8288                         val = digval(str[0]);
8289                         cinfo.reg = info->tmpl.lhs[val].reg;
8290                         cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
8291                         cinfo.regcm &= info->tmpl.lhs[val].regcm;
8292                         if (cinfo.reg == REG_UNSET) {
8293                                 cinfo.reg = REG_VIRT0 + val;
8294                         }
8295                         if (cinfo.regcm == 0) {
8296                                 error(state, 0, "No registers for %d", val);
8297                         }
8298                         info->tmpl.lhs[val] = cinfo;
8299                         info->tmpl.rhs[i]   = cinfo;
8300                                 
8301                 } else {
8302                         info->tmpl.rhs[i] = arch_reg_constraint(state, 
8303                                 in_param[i].expr->type, str);
8304                 }
8305                 free_triple(state, constraint);
8306         }
8307
8308         /* Now build the helper expressions */
8309         for(i = 0; i < in; i++) {
8310                 RHS(def, i) = read_expr(state,in_param[i].expr);
8311         }
8312         flatten(state, first, def);
8313         for(i = 0; i < (out + clobbers); i++) {
8314                 struct type *type;
8315                 struct triple *piece;
8316                 type = (i < out)? out_param[i].expr->type : &void_type;
8317                 piece = triple(state, OP_PIECE, type, def, 0);
8318                 piece->u.cval = i;
8319                 LHS(def, i) = piece;
8320                 flatten(state, first, piece);
8321         }
8322         /* And write the helpers to their destinations */
8323         for(i = 0; i < out; i++) {
8324                 struct triple *piece;
8325                 piece = LHS(def, i);
8326                 flatten(state, first,
8327                         write_expr(state, out_param[i].expr, piece));
8328         }
8329 }
8330
8331
8332 static int isdecl(int tok)
8333 {
8334         switch(tok) {
8335         case TOK_AUTO:
8336         case TOK_REGISTER:
8337         case TOK_STATIC:
8338         case TOK_EXTERN:
8339         case TOK_TYPEDEF:
8340         case TOK_CONST:
8341         case TOK_RESTRICT:
8342         case TOK_VOLATILE:
8343         case TOK_VOID:
8344         case TOK_CHAR:
8345         case TOK_SHORT:
8346         case TOK_INT:
8347         case TOK_LONG:
8348         case TOK_FLOAT:
8349         case TOK_DOUBLE:
8350         case TOK_SIGNED:
8351         case TOK_UNSIGNED:
8352         case TOK_STRUCT:
8353         case TOK_UNION:
8354         case TOK_ENUM:
8355         case TOK_TYPE_NAME: /* typedef name */
8356                 return 1;
8357         default:
8358                 return 0;
8359         }
8360 }
8361
8362 static void compound_statement(struct compile_state *state, struct triple *first)
8363 {
8364         eat(state, TOK_LBRACE);
8365         start_scope(state);
8366
8367         /* statement-list opt */
8368         while (peek(state) != TOK_RBRACE) {
8369                 statement(state, first);
8370         }
8371         end_scope(state);
8372         eat(state, TOK_RBRACE);
8373 }
8374
8375 static void statement(struct compile_state *state, struct triple *first)
8376 {
8377         int tok;
8378         tok = peek(state);
8379         if (tok == TOK_LBRACE) {
8380                 compound_statement(state, first);
8381         }
8382         else if (tok == TOK_IF) {
8383                 if_statement(state, first); 
8384         }
8385         else if (tok == TOK_FOR) {
8386                 for_statement(state, first);
8387         }
8388         else if (tok == TOK_WHILE) {
8389                 while_statement(state, first);
8390         }
8391         else if (tok == TOK_DO) {
8392                 do_statement(state, first);
8393         }
8394         else if (tok == TOK_RETURN) {
8395                 return_statement(state, first);
8396         }
8397         else if (tok == TOK_BREAK) {
8398                 break_statement(state, first);
8399         }
8400         else if (tok == TOK_CONTINUE) {
8401                 continue_statement(state, first);
8402         }
8403         else if (tok == TOK_GOTO) {
8404                 goto_statement(state, first);
8405         }
8406         else if (tok == TOK_SWITCH) {
8407                 switch_statement(state, first);
8408         }
8409         else if (tok == TOK_ASM) {
8410                 asm_statement(state, first);
8411         }
8412         else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
8413                 labeled_statement(state, first); 
8414         }
8415         else if (tok == TOK_CASE) {
8416                 case_statement(state, first);
8417         }
8418         else if (tok == TOK_DEFAULT) {
8419                 default_statement(state, first);
8420         }
8421         else if (isdecl(tok)) {
8422                 /* This handles C99 intermixing of statements and decls */
8423                 decl(state, first);
8424         }
8425         else {
8426                 expr_statement(state, first);
8427         }
8428 }
8429
8430 static struct type *param_decl(struct compile_state *state)
8431 {
8432         struct type *type;
8433         struct hash_entry *ident;
8434         /* Cheat so the declarator will know we are not global */
8435         start_scope(state); 
8436         ident = 0;
8437         type = decl_specifiers(state);
8438         type = declarator(state, type, &ident, 0);
8439         type->field_ident = ident;
8440         end_scope(state);
8441         return type;
8442 }
8443
8444 static struct type *param_type_list(struct compile_state *state, struct type *type)
8445 {
8446         struct type *ftype, **next;
8447         ftype = new_type(TYPE_FUNCTION, type, param_decl(state));
8448         next = &ftype->right;
8449         while(peek(state) == TOK_COMMA) {
8450                 eat(state, TOK_COMMA);
8451                 if (peek(state) == TOK_DOTS) {
8452                         eat(state, TOK_DOTS);
8453                         error(state, 0, "variadic functions not supported");
8454                 }
8455                 else {
8456                         *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
8457                         next = &((*next)->right);
8458                 }
8459         }
8460         return ftype;
8461 }
8462
8463
8464 static struct type *type_name(struct compile_state *state)
8465 {
8466         struct type *type;
8467         type = specifier_qualifier_list(state);
8468         /* abstract-declarator (may consume no tokens) */
8469         type = declarator(state, type, 0, 0);
8470         return type;
8471 }
8472
8473 static struct type *direct_declarator(
8474         struct compile_state *state, struct type *type, 
8475         struct hash_entry **ident, int need_ident)
8476 {
8477         struct type *outer;
8478         int op;
8479         outer = 0;
8480         arrays_complete(state, type);
8481         switch(peek(state)) {
8482         case TOK_IDENT:
8483                 eat(state, TOK_IDENT);
8484                 if (!ident) {
8485                         error(state, 0, "Unexpected identifier found");
8486                 }
8487                 /* The name of what we are declaring */
8488                 *ident = state->token[0].ident;
8489                 break;
8490         case TOK_LPAREN:
8491                 eat(state, TOK_LPAREN);
8492                 outer = declarator(state, type, ident, need_ident);
8493                 eat(state, TOK_RPAREN);
8494                 break;
8495         default:
8496                 if (need_ident) {
8497                         error(state, 0, "Identifier expected");
8498                 }
8499                 break;
8500         }
8501         do {
8502                 op = 1;
8503                 arrays_complete(state, type);
8504                 switch(peek(state)) {
8505                 case TOK_LPAREN:
8506                         eat(state, TOK_LPAREN);
8507                         type = param_type_list(state, type);
8508                         eat(state, TOK_RPAREN);
8509                         break;
8510                 case TOK_LBRACKET:
8511                 {
8512                         unsigned int qualifiers;
8513                         struct triple *value;
8514                         value = 0;
8515                         eat(state, TOK_LBRACKET);
8516                         if (peek(state) != TOK_RBRACKET) {
8517                                 value = constant_expr(state);
8518                                 integral(state, value);
8519                         }
8520                         eat(state, TOK_RBRACKET);
8521
8522                         qualifiers = type->type & (QUAL_MASK | STOR_MASK);
8523                         type = new_type(TYPE_ARRAY | qualifiers, type, 0);
8524                         if (value) {
8525                                 type->elements = value->u.cval;
8526                                 free_triple(state, value);
8527                         } else {
8528                                 type->elements = ELEMENT_COUNT_UNSPECIFIED;
8529                                 op = 0;
8530                         }
8531                 }
8532                         break;
8533                 default:
8534                         op = 0;
8535                         break;
8536                 }
8537         } while(op);
8538         if (outer) {
8539                 struct type *inner;
8540                 arrays_complete(state, type);
8541                 FINISHME();
8542                 for(inner = outer; inner->left; inner = inner->left)
8543                         ;
8544                 inner->left = type;
8545                 type = outer;
8546         }
8547         return type;
8548 }
8549
8550 static struct type *declarator(
8551         struct compile_state *state, struct type *type, 
8552         struct hash_entry **ident, int need_ident)
8553 {
8554         while(peek(state) == TOK_STAR) {
8555                 eat(state, TOK_STAR);
8556                 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
8557         }
8558         type = direct_declarator(state, type, ident, need_ident);
8559         return type;
8560 }
8561
8562
8563 static struct type *typedef_name(
8564         struct compile_state *state, unsigned int specifiers)
8565 {
8566         struct hash_entry *ident;
8567         struct type *type;
8568         eat(state, TOK_TYPE_NAME);
8569         ident = state->token[0].ident;
8570         type = ident->sym_ident->type;
8571         specifiers |= type->type & QUAL_MASK;
8572         if ((specifiers & (STOR_MASK | QUAL_MASK)) != 
8573                 (type->type & (STOR_MASK | QUAL_MASK))) {
8574                 type = clone_type(specifiers, type);
8575         }
8576         return type;
8577 }
8578
8579 static struct type *enum_specifier(
8580         struct compile_state *state, unsigned int specifiers)
8581 {
8582         int tok;
8583         struct type *type;
8584         type = 0;
8585         FINISHME();
8586         eat(state, TOK_ENUM);
8587         tok = peek(state);
8588         if (tok == TOK_IDENT) {
8589                 eat(state, TOK_IDENT);
8590         }
8591         if ((tok != TOK_IDENT) || (peek(state) == TOK_LBRACE)) {
8592                 eat(state, TOK_LBRACE);
8593                 do {
8594                         eat(state, TOK_IDENT);
8595                         if (peek(state) == TOK_EQ) {
8596                                 eat(state, TOK_EQ);
8597                                 constant_expr(state);
8598                         }
8599                         if (peek(state) == TOK_COMMA) {
8600                                 eat(state, TOK_COMMA);
8601                         }
8602                 } while(peek(state) != TOK_RBRACE);
8603                 eat(state, TOK_RBRACE);
8604         }
8605         FINISHME();
8606         return type;
8607 }
8608
8609 static struct type *struct_declarator(
8610         struct compile_state *state, struct type *type, struct hash_entry **ident)
8611 {
8612         int tok;
8613         tok = peek(state);
8614         if (tok != TOK_COLON) {
8615                 type = declarator(state, type, ident, 1);
8616         }
8617         if ((tok == TOK_COLON) || (peek(state) == TOK_COLON)) {
8618                 struct triple *value;
8619                 eat(state, TOK_COLON);
8620                 value = constant_expr(state);
8621 #warning "FIXME implement bitfields to reduce register usage"
8622                 error(state, 0, "bitfields not yet implemented");
8623         }
8624         return type;
8625 }
8626
8627 static struct type *struct_or_union_specifier(
8628         struct compile_state *state, unsigned int spec)
8629 {
8630         struct type *struct_type;
8631         struct hash_entry *ident;
8632         unsigned int type_join;
8633         int tok;
8634         struct_type = 0;
8635         ident = 0;
8636         switch(peek(state)) {
8637         case TOK_STRUCT:
8638                 eat(state, TOK_STRUCT);
8639                 type_join = TYPE_PRODUCT;
8640                 break;
8641         case TOK_UNION:
8642                 eat(state, TOK_UNION);
8643                 type_join = TYPE_OVERLAP;
8644                 error(state, 0, "unions not yet supported\n");
8645                 break;
8646         default:
8647                 eat(state, TOK_STRUCT);
8648                 type_join = TYPE_PRODUCT;
8649                 break;
8650         }
8651         tok = peek(state);
8652         if ((tok == TOK_IDENT) || (tok == TOK_TYPE_NAME)) {
8653                 eat(state, tok);
8654                 ident = state->token[0].ident;
8655         }
8656         if (!ident || (peek(state) == TOK_LBRACE)) {
8657                 ulong_t elements;
8658                 struct type **next;
8659                 elements = 0;
8660                 eat(state, TOK_LBRACE);
8661                 next = &struct_type;
8662                 do {
8663                         struct type *base_type;
8664                         int done;
8665                         base_type = specifier_qualifier_list(state);
8666                         do {
8667                                 struct type *type;
8668                                 struct hash_entry *fident;
8669                                 done = 1;
8670                                 type = struct_declarator(state, base_type, &fident);
8671                                 elements++;
8672                                 if (peek(state) == TOK_COMMA) {
8673                                         done = 0;
8674                                         eat(state, TOK_COMMA);
8675                                 }
8676                                 type = clone_type(0, type);
8677                                 type->field_ident = fident;
8678                                 if (*next) {
8679                                         *next = new_type(type_join, *next, type);
8680                                         next = &((*next)->right);
8681                                 } else {
8682                                         *next = type;
8683                                 }
8684                         } while(!done);
8685                         eat(state, TOK_SEMI);
8686                 } while(peek(state) != TOK_RBRACE);
8687                 eat(state, TOK_RBRACE);
8688                 struct_type = new_type(TYPE_STRUCT | spec, struct_type, 0);
8689                 struct_type->type_ident = ident;
8690                 struct_type->elements = elements;
8691                 if (ident) {
8692                         symbol(state, ident, &ident->sym_struct, 0, struct_type);
8693                 }
8694         }
8695         if (ident && ident->sym_struct) {
8696                 struct_type = clone_type(spec,  ident->sym_struct->type);
8697         }
8698         else if (ident && !ident->sym_struct) {
8699                 error(state, 0, "struct %s undeclared", ident->name);
8700         }
8701         return struct_type;
8702 }
8703
8704 static unsigned int storage_class_specifier_opt(struct compile_state *state)
8705 {
8706         unsigned int specifiers;
8707         switch(peek(state)) {
8708         case TOK_AUTO:
8709                 eat(state, TOK_AUTO);
8710                 specifiers = STOR_AUTO;
8711                 break;
8712         case TOK_REGISTER:
8713                 eat(state, TOK_REGISTER);
8714                 specifiers = STOR_REGISTER;
8715                 break;
8716         case TOK_STATIC:
8717                 eat(state, TOK_STATIC);
8718                 specifiers = STOR_STATIC;
8719                 break;
8720         case TOK_EXTERN:
8721                 eat(state, TOK_EXTERN);
8722                 specifiers = STOR_EXTERN;
8723                 break;
8724         case TOK_TYPEDEF:
8725                 eat(state, TOK_TYPEDEF);
8726                 specifiers = STOR_TYPEDEF;
8727                 break;
8728         default:
8729                 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
8730                         specifiers = STOR_STATIC;
8731                 }
8732                 else {
8733                         specifiers = STOR_AUTO;
8734                 }
8735         }
8736         return specifiers;
8737 }
8738
8739 static unsigned int function_specifier_opt(struct compile_state *state)
8740 {
8741         /* Ignore the inline keyword */
8742         unsigned int specifiers;
8743         specifiers = 0;
8744         switch(peek(state)) {
8745         case TOK_INLINE:
8746                 eat(state, TOK_INLINE);
8747                 specifiers = STOR_INLINE;
8748         }
8749         return specifiers;
8750 }
8751
8752 static unsigned int type_qualifiers(struct compile_state *state)
8753 {
8754         unsigned int specifiers;
8755         int done;
8756         done = 0;
8757         specifiers = QUAL_NONE;
8758         do {
8759                 switch(peek(state)) {
8760                 case TOK_CONST:
8761                         eat(state, TOK_CONST);
8762                         specifiers = QUAL_CONST;
8763                         break;
8764                 case TOK_VOLATILE:
8765                         eat(state, TOK_VOLATILE);
8766                         specifiers = QUAL_VOLATILE;
8767                         break;
8768                 case TOK_RESTRICT:
8769                         eat(state, TOK_RESTRICT);
8770                         specifiers = QUAL_RESTRICT;
8771                         break;
8772                 default:
8773                         done = 1;
8774                         break;
8775                 }
8776         } while(!done);
8777         return specifiers;
8778 }
8779
8780 static struct type *type_specifier(
8781         struct compile_state *state, unsigned int spec)
8782 {
8783         struct type *type;
8784         type = 0;
8785         switch(peek(state)) {
8786         case TOK_VOID:
8787                 eat(state, TOK_VOID);
8788                 type = new_type(TYPE_VOID | spec, 0, 0);
8789                 break;
8790         case TOK_CHAR:
8791                 eat(state, TOK_CHAR);
8792                 type = new_type(TYPE_CHAR | spec, 0, 0);
8793                 break;
8794         case TOK_SHORT:
8795                 eat(state, TOK_SHORT);
8796                 if (peek(state) == TOK_INT) {
8797                         eat(state, TOK_INT);
8798                 }
8799                 type = new_type(TYPE_SHORT | spec, 0, 0);
8800                 break;
8801         case TOK_INT:
8802                 eat(state, TOK_INT);
8803                 type = new_type(TYPE_INT | spec, 0, 0);
8804                 break;
8805         case TOK_LONG:
8806                 eat(state, TOK_LONG);
8807                 switch(peek(state)) {
8808                 case TOK_LONG:
8809                         eat(state, TOK_LONG);
8810                         error(state, 0, "long long not supported");
8811                         break;
8812                 case TOK_DOUBLE:
8813                         eat(state, TOK_DOUBLE);
8814                         error(state, 0, "long double not supported");
8815                         break;
8816                 case TOK_INT:
8817                         eat(state, TOK_INT);
8818                         type = new_type(TYPE_LONG | spec, 0, 0);
8819                         break;
8820                 default:
8821                         type = new_type(TYPE_LONG | spec, 0, 0);
8822                         break;
8823                 }
8824                 break;
8825         case TOK_FLOAT:
8826                 eat(state, TOK_FLOAT);
8827                 error(state, 0, "type float not supported");
8828                 break;
8829         case TOK_DOUBLE:
8830                 eat(state, TOK_DOUBLE);
8831                 error(state, 0, "type double not supported");
8832                 break;
8833         case TOK_SIGNED:
8834                 eat(state, TOK_SIGNED);
8835                 switch(peek(state)) {
8836                 case TOK_LONG:
8837                         eat(state, TOK_LONG);
8838                         switch(peek(state)) {
8839                         case TOK_LONG:
8840                                 eat(state, TOK_LONG);
8841                                 error(state, 0, "type long long not supported");
8842                                 break;
8843                         case TOK_INT:
8844                                 eat(state, TOK_INT);
8845                                 type = new_type(TYPE_LONG | spec, 0, 0);
8846                                 break;
8847                         default:
8848                                 type = new_type(TYPE_LONG | spec, 0, 0);
8849                                 break;
8850                         }
8851                         break;
8852                 case TOK_INT:
8853                         eat(state, TOK_INT);
8854                         type = new_type(TYPE_INT | spec, 0, 0);
8855                         break;
8856                 case TOK_SHORT:
8857                         eat(state, TOK_SHORT);
8858                         type = new_type(TYPE_SHORT | spec, 0, 0);
8859                         break;
8860                 case TOK_CHAR:
8861                         eat(state, TOK_CHAR);
8862                         type = new_type(TYPE_CHAR | spec, 0, 0);
8863                         break;
8864                 default:
8865                         type = new_type(TYPE_INT | spec, 0, 0);
8866                         break;
8867                 }
8868                 break;
8869         case TOK_UNSIGNED:
8870                 eat(state, TOK_UNSIGNED);
8871                 switch(peek(state)) {
8872                 case TOK_LONG:
8873                         eat(state, TOK_LONG);
8874                         switch(peek(state)) {
8875                         case TOK_LONG:
8876                                 eat(state, TOK_LONG);
8877                                 error(state, 0, "unsigned long long not supported");
8878                                 break;
8879                         case TOK_INT:
8880                                 eat(state, TOK_INT);
8881                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8882                                 break;
8883                         default:
8884                                 type = new_type(TYPE_ULONG | spec, 0, 0);
8885                                 break;
8886                         }
8887                         break;
8888                 case TOK_INT:
8889                         eat(state, TOK_INT);
8890                         type = new_type(TYPE_UINT | spec, 0, 0);
8891                         break;
8892                 case TOK_SHORT:
8893                         eat(state, TOK_SHORT);
8894                         type = new_type(TYPE_USHORT | spec, 0, 0);
8895                         break;
8896                 case TOK_CHAR:
8897                         eat(state, TOK_CHAR);
8898                         type = new_type(TYPE_UCHAR | spec, 0, 0);
8899                         break;
8900                 default:
8901                         type = new_type(TYPE_UINT | spec, 0, 0);
8902                         break;
8903                 }
8904                 break;
8905                 /* struct or union specifier */
8906         case TOK_STRUCT:
8907         case TOK_UNION:
8908                 type = struct_or_union_specifier(state, spec);
8909                 break;
8910                 /* enum-spefifier */
8911         case TOK_ENUM:
8912                 type = enum_specifier(state, spec);
8913                 break;
8914                 /* typedef name */
8915         case TOK_TYPE_NAME:
8916                 type = typedef_name(state, spec);
8917                 break;
8918         default:
8919                 error(state, 0, "bad type specifier %s", 
8920                         tokens[peek(state)]);
8921                 break;
8922         }
8923         return type;
8924 }
8925
8926 static int istype(int tok)
8927 {
8928         switch(tok) {
8929         case TOK_CONST:
8930         case TOK_RESTRICT:
8931         case TOK_VOLATILE:
8932         case TOK_VOID:
8933         case TOK_CHAR:
8934         case TOK_SHORT:
8935         case TOK_INT:
8936         case TOK_LONG:
8937         case TOK_FLOAT:
8938         case TOK_DOUBLE:
8939         case TOK_SIGNED:
8940         case TOK_UNSIGNED:
8941         case TOK_STRUCT:
8942         case TOK_UNION:
8943         case TOK_ENUM:
8944         case TOK_TYPE_NAME:
8945                 return 1;
8946         default:
8947                 return 0;
8948         }
8949 }
8950
8951
8952 static struct type *specifier_qualifier_list(struct compile_state *state)
8953 {
8954         struct type *type;
8955         unsigned int specifiers = 0;
8956
8957         /* type qualifiers */
8958         specifiers |= type_qualifiers(state);
8959
8960         /* type specifier */
8961         type = type_specifier(state, specifiers);
8962
8963         return type;
8964 }
8965
8966 static int isdecl_specifier(int tok)
8967 {
8968         switch(tok) {
8969                 /* storage class specifier */
8970         case TOK_AUTO:
8971         case TOK_REGISTER:
8972         case TOK_STATIC:
8973         case TOK_EXTERN:
8974         case TOK_TYPEDEF:
8975                 /* type qualifier */
8976         case TOK_CONST:
8977         case TOK_RESTRICT:
8978         case TOK_VOLATILE:
8979                 /* type specifiers */
8980         case TOK_VOID:
8981         case TOK_CHAR:
8982         case TOK_SHORT:
8983         case TOK_INT:
8984         case TOK_LONG:
8985         case TOK_FLOAT:
8986         case TOK_DOUBLE:
8987         case TOK_SIGNED:
8988         case TOK_UNSIGNED:
8989                 /* struct or union specifier */
8990         case TOK_STRUCT:
8991         case TOK_UNION:
8992                 /* enum-spefifier */
8993         case TOK_ENUM:
8994                 /* typedef name */
8995         case TOK_TYPE_NAME:
8996                 /* function specifiers */
8997         case TOK_INLINE:
8998                 return 1;
8999         default:
9000                 return 0;
9001         }
9002 }
9003
9004 static struct type *decl_specifiers(struct compile_state *state)
9005 {
9006         struct type *type;
9007         unsigned int specifiers;
9008         /* I am overly restrictive in the arragement of specifiers supported.
9009          * C is overly flexible in this department it makes interpreting
9010          * the parse tree difficult.
9011          */
9012         specifiers = 0;
9013
9014         /* storage class specifier */
9015         specifiers |= storage_class_specifier_opt(state);
9016
9017         /* function-specifier */
9018         specifiers |= function_specifier_opt(state);
9019
9020         /* type qualifier */
9021         specifiers |= type_qualifiers(state);
9022
9023         /* type specifier */
9024         type = type_specifier(state, specifiers);
9025         return type;
9026 }
9027
9028 struct field_info {
9029         struct type *type;
9030         size_t offset;
9031 };
9032
9033 static struct field_info designator(struct compile_state *state, struct type *type)
9034 {
9035         int tok;
9036         struct field_info info;
9037         info.offset = ~0U;
9038         info.type = 0;
9039         do {
9040                 switch(peek(state)) {
9041                 case TOK_LBRACKET:
9042                 {
9043                         struct triple *value;
9044                         if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
9045                                 error(state, 0, "Array designator not in array initializer");
9046                         }
9047                         eat(state, TOK_LBRACKET);
9048                         value = constant_expr(state);
9049                         eat(state, TOK_RBRACKET);
9050
9051                         info.type = type->left;
9052                         info.offset = value->u.cval * size_of(state, info.type);
9053                         break;
9054                 }
9055                 case TOK_DOT:
9056                 {
9057                         struct hash_entry *field;
9058                         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
9059                                 error(state, 0, "Struct designator not in struct initializer");
9060                         }
9061                         eat(state, TOK_DOT);
9062                         eat(state, TOK_IDENT);
9063                         field = state->token[0].ident;
9064                         info.offset = field_offset(state, type, field);
9065                         info.type   = field_type(state, type, field);
9066                         break;
9067                 }
9068                 default:
9069                         error(state, 0, "Invalid designator");
9070                 }
9071                 tok = peek(state);
9072         } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
9073         eat(state, TOK_EQ);
9074         return info;
9075 }
9076
9077 static struct triple *initializer(
9078         struct compile_state *state, struct type *type)
9079 {
9080         struct triple *result;
9081 #warning "FIXME handle string pointer initializers "
9082 #warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
9083         if (peek(state) != TOK_LBRACE) {
9084                 result = assignment_expr(state);
9085                 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
9086                         (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9087                         ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
9088                         (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9089                         (equiv_types(type->left, result->type->left))) {
9090                         type->elements = result->type->elements;
9091                 }
9092                 if (!is_init_compatible(state, type, result->type)) {
9093                         error(state, 0, "Incompatible types in initializer");
9094                 }
9095                 if (!equiv_types(type, result->type)) {
9096                         result = mk_cast_expr(state, type, result);
9097                 }
9098         }
9099         else {
9100                 int comma;
9101                 size_t max_offset;
9102                 struct field_info info;
9103                 void *buf;
9104                 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
9105                         ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
9106                         internal_error(state, 0, "unknown initializer type");
9107                 }
9108                 info.offset = 0;
9109                 info.type = type->left;
9110                 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9111                         info.type = next_field(state, type, 0);
9112                 }
9113                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
9114                         max_offset = 0;
9115                 } else {
9116                         max_offset = size_of(state, type);
9117                 }
9118                 buf = xcmalloc(max_offset, "initializer");
9119                 eat(state, TOK_LBRACE);
9120                 do {
9121                         struct triple *value;
9122                         struct type *value_type;
9123                         size_t value_size;
9124                         void *dest;
9125                         int tok;
9126                         comma = 0;
9127                         tok = peek(state);
9128                         if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
9129                                 info = designator(state, type);
9130                         }
9131                         if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
9132                                 (info.offset >= max_offset)) {
9133                                 error(state, 0, "element beyond bounds");
9134                         }
9135                         value_type = info.type;
9136                         value = eval_const_expr(state, initializer(state, value_type));
9137                         value_size = size_of(state, value_type);
9138                         if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
9139                                 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9140                                 (max_offset <= info.offset)) {
9141                                 void *old_buf;
9142                                 size_t old_size;
9143                                 old_buf = buf;
9144                                 old_size = max_offset;
9145                                 max_offset = info.offset + value_size;
9146                                 buf = xmalloc(max_offset, "initializer");
9147                                 memcpy(buf, old_buf, old_size);
9148                                 xfree(old_buf);
9149                         }
9150                         dest = ((char *)buf) + info.offset;
9151                         if (value->op == OP_BLOBCONST) {
9152                                 memcpy(dest, value->u.blob, value_size);
9153                         }
9154                         else if ((value->op == OP_INTCONST) && (value_size == 1)) {
9155                                 *((uint8_t *)dest) = value->u.cval & 0xff;
9156                         }
9157                         else if ((value->op == OP_INTCONST) && (value_size == 2)) {
9158                                 *((uint16_t *)dest) = value->u.cval & 0xffff;
9159                         }
9160                         else if ((value->op == OP_INTCONST) && (value_size == 4)) {
9161                                 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
9162                         }
9163                         else {
9164                                 internal_error(state, 0, "unhandled constant initializer");
9165                         }
9166                         free_triple(state, value);
9167                         if (peek(state) == TOK_COMMA) {
9168                                 eat(state, TOK_COMMA);
9169                                 comma = 1;
9170                         }
9171                         info.offset += value_size;
9172                         if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
9173                                 info.type = next_field(state, type, info.type);
9174                                 info.offset = field_offset(state, type, 
9175                                         info.type->field_ident);
9176                         }
9177                 } while(comma && (peek(state) != TOK_RBRACE));
9178                 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
9179                         ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
9180                         type->elements = max_offset / size_of(state, type->left);
9181                 }
9182                 eat(state, TOK_RBRACE);
9183                 result = triple(state, OP_BLOBCONST, type, 0, 0);
9184                 result->u.blob = buf;
9185         }
9186         return result;
9187 }
9188
9189 static void resolve_branches(struct compile_state *state)
9190 {
9191         /* Make a second pass and finish anything outstanding
9192          * with respect to branches.  The only outstanding item
9193          * is to see if there are goto to labels that have not
9194          * been defined and to error about them.
9195          */
9196         int i;
9197         for(i = 0; i < HASH_TABLE_SIZE; i++) {
9198                 struct hash_entry *entry;
9199                 for(entry = state->hash_table[i]; entry; entry = entry->next) {
9200                         struct triple *ins;
9201                         if (!entry->sym_label) {
9202                                 continue;
9203                         }
9204                         ins = entry->sym_label->def;
9205                         if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
9206                                 error(state, ins, "label `%s' used but not defined",
9207                                         entry->name);
9208                         }
9209                 }
9210         }
9211 }
9212
9213 static struct triple *function_definition(
9214         struct compile_state *state, struct type *type)
9215 {
9216         struct triple *def, *tmp, *first, *end;
9217         struct hash_entry *ident;
9218         struct type *param;
9219         int i;
9220         if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
9221                 error(state, 0, "Invalid function header");
9222         }
9223
9224         /* Verify the function type */
9225         if (((type->right->type & TYPE_MASK) != TYPE_VOID)  &&
9226                 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
9227                 (type->right->field_ident == 0)) {
9228                 error(state, 0, "Invalid function parameters");
9229         }
9230         param = type->right;
9231         i = 0;
9232         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9233                 i++;
9234                 if (!param->left->field_ident) {
9235                         error(state, 0, "No identifier for parameter %d\n", i);
9236                 }
9237                 param = param->right;
9238         }
9239         i++;
9240         if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
9241                 error(state, 0, "No identifier for paramter %d\n", i);
9242         }
9243         
9244         /* Get a list of statements for this function. */
9245         def = triple(state, OP_LIST, type, 0, 0);
9246
9247         /* Start a new scope for the passed parameters */
9248         start_scope(state);
9249
9250         /* Put a label at the very start of a function */
9251         first = label(state);
9252         RHS(def, 0) = first;
9253
9254         /* Put a label at the very end of a function */
9255         end = label(state);
9256         flatten(state, first, end);
9257
9258         /* Walk through the parameters and create symbol table entries
9259          * for them.
9260          */
9261         param = type->right;
9262         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
9263                 ident = param->left->field_ident;
9264                 tmp = variable(state, param->left);
9265                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9266                 flatten(state, end, tmp);
9267                 param = param->right;
9268         }
9269         if ((param->type & TYPE_MASK) != TYPE_VOID) {
9270                 /* And don't forget the last parameter */
9271                 ident = param->field_ident;
9272                 tmp = variable(state, param);
9273                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
9274                 flatten(state, end, tmp);
9275         }
9276         /* Add a variable for the return value */
9277         MISC(def, 0) = 0;
9278         if ((type->left->type & TYPE_MASK) != TYPE_VOID) {
9279                 /* Remove all type qualifiers from the return type */
9280                 tmp = variable(state, clone_type(0, type->left));
9281                 flatten(state, end, tmp);
9282                 /* Remember where the return value is */
9283                 MISC(def, 0) = tmp;
9284         }
9285
9286         /* Remember which function I am compiling.
9287          * Also assume the last defined function is the main function.
9288          */
9289         state->main_function = def;
9290
9291         /* Now get the actual function definition */
9292         compound_statement(state, end);
9293
9294         /* Finish anything unfinished with branches */
9295         resolve_branches(state);
9296
9297         /* Remove the parameter scope */
9298         end_scope(state);
9299
9300 #if 0
9301         fprintf(stdout, "\n");
9302         loc(stdout, state, 0);
9303         fprintf(stdout, "\n__________ function_definition _________\n");
9304         print_triple(state, def);
9305         fprintf(stdout, "__________ function_definition _________ done\n\n");
9306 #endif
9307
9308         return def;
9309 }
9310
9311 static struct triple *do_decl(struct compile_state *state, 
9312         struct type *type, struct hash_entry *ident)
9313 {
9314         struct triple *def;
9315         def = 0;
9316         /* Clean up the storage types used */
9317         switch (type->type & STOR_MASK) {
9318         case STOR_AUTO:
9319         case STOR_STATIC:
9320                 /* These are the good types I am aiming for */
9321                 break;
9322         case STOR_REGISTER:
9323                 type->type &= ~STOR_MASK;
9324                 type->type |= STOR_AUTO;
9325                 break;
9326         case STOR_EXTERN:
9327                 type->type &= ~STOR_MASK;
9328                 type->type |= STOR_STATIC;
9329                 break;
9330         case STOR_TYPEDEF:
9331                 if (!ident) {
9332                         error(state, 0, "typedef without name");
9333                 }
9334                 symbol(state, ident, &ident->sym_ident, 0, type);
9335                 ident->tok = TOK_TYPE_NAME;
9336                 return 0;
9337                 break;
9338         default:
9339                 internal_error(state, 0, "Undefined storage class");
9340         }
9341         if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
9342                 error(state, 0, "Function prototypes not supported");
9343         }
9344         if (ident && 
9345                 ((type->type & STOR_MASK) == STOR_STATIC) &&
9346                 ((type->type & QUAL_CONST) == 0)) {
9347                 error(state, 0, "non const static variables not supported");
9348         }
9349         if (ident) {
9350                 def = variable(state, type);
9351                 symbol(state, ident, &ident->sym_ident, def, type);
9352         }
9353         return def;
9354 }
9355
9356 static void decl(struct compile_state *state, struct triple *first)
9357 {
9358         struct type *base_type, *type;
9359         struct hash_entry *ident;
9360         struct triple *def;
9361         int global;
9362         global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
9363         base_type = decl_specifiers(state);
9364         ident = 0;
9365         type = declarator(state, base_type, &ident, 0);
9366         if (global && ident && (peek(state) == TOK_LBRACE)) {
9367                 /* function */
9368                 state->function = ident->name;
9369                 def = function_definition(state, type);
9370                 symbol(state, ident, &ident->sym_ident, def, type);
9371                 state->function = 0;
9372         }
9373         else {
9374                 int done;
9375                 flatten(state, first, do_decl(state, type, ident));
9376                 /* type or variable definition */
9377                 do {
9378                         done = 1;
9379                         if (peek(state) == TOK_EQ) {
9380                                 if (!ident) {
9381                                         error(state, 0, "cannot assign to a type");
9382                                 }
9383                                 eat(state, TOK_EQ);
9384                                 flatten(state, first,
9385                                         init_expr(state, 
9386                                                 ident->sym_ident->def, 
9387                                                 initializer(state, type)));
9388                         }
9389                         arrays_complete(state, type);
9390                         if (peek(state) == TOK_COMMA) {
9391                                 eat(state, TOK_COMMA);
9392                                 ident = 0;
9393                                 type = declarator(state, base_type, &ident, 0);
9394                                 flatten(state, first, do_decl(state, type, ident));
9395                                 done = 0;
9396                         }
9397                 } while(!done);
9398                 eat(state, TOK_SEMI);
9399         }
9400 }
9401
9402 static void decls(struct compile_state *state)
9403 {
9404         struct triple *list;
9405         int tok;
9406         list = label(state);
9407         while(1) {
9408                 tok = peek(state);
9409                 if (tok == TOK_EOF) {
9410                         return;
9411                 }
9412                 if (tok == TOK_SPACE) {
9413                         eat(state, TOK_SPACE);
9414                 }
9415                 decl(state, list);
9416                 if (list->next != list) {
9417                         error(state, 0, "global variables not supported");
9418                 }
9419         }
9420 }
9421
9422 /*
9423  * Data structurs for optimation.
9424  */
9425
9426 static void do_use_block(
9427         struct block *used, struct block_set **head, struct block *user, 
9428         int front)
9429 {
9430         struct block_set **ptr, *new;
9431         if (!used)
9432                 return;
9433         if (!user)
9434                 return;
9435         ptr = head;
9436         while(*ptr) {
9437                 if ((*ptr)->member == user) {
9438                         return;
9439                 }
9440                 ptr = &(*ptr)->next;
9441         }
9442         new = xcmalloc(sizeof(*new), "block_set");
9443         new->member = user;
9444         if (front) {
9445                 new->next = *head;
9446                 *head = new;
9447         }
9448         else {
9449                 new->next = 0;
9450                 *ptr = new;
9451         }
9452 }
9453 static void do_unuse_block(
9454         struct block *used, struct block_set **head, struct block *unuser)
9455 {
9456         struct block_set *use, **ptr;
9457         ptr = head;
9458         while(*ptr) {
9459                 use = *ptr;
9460                 if (use->member == unuser) {
9461                         *ptr = use->next;
9462                         memset(use, -1, sizeof(*use));
9463                         xfree(use);
9464                 }
9465                 else {
9466                         ptr = &use->next;
9467                 }
9468         }
9469 }
9470
9471 static void use_block(struct block *used, struct block *user)
9472 {
9473         /* Append new to the head of the list, print_block
9474          * depends on this.
9475          */
9476         do_use_block(used, &used->use, user, 1); 
9477         used->users++;
9478 }
9479 static void unuse_block(struct block *used, struct block *unuser)
9480 {
9481         do_unuse_block(used, &used->use, unuser); 
9482         used->users--;
9483 }
9484
9485 static void idom_block(struct block *idom, struct block *user)
9486 {
9487         do_use_block(idom, &idom->idominates, user, 0);
9488 }
9489
9490 static void unidom_block(struct block *idom, struct block *unuser)
9491 {
9492         do_unuse_block(idom, &idom->idominates, unuser);
9493 }
9494
9495 static void domf_block(struct block *block, struct block *domf)
9496 {
9497         do_use_block(block, &block->domfrontier, domf, 0);
9498 }
9499
9500 static void undomf_block(struct block *block, struct block *undomf)
9501 {
9502         do_unuse_block(block, &block->domfrontier, undomf);
9503 }
9504
9505 static void ipdom_block(struct block *ipdom, struct block *user)
9506 {
9507         do_use_block(ipdom, &ipdom->ipdominates, user, 0);
9508 }
9509
9510 static void unipdom_block(struct block *ipdom, struct block *unuser)
9511 {
9512         do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
9513 }
9514
9515 static void ipdomf_block(struct block *block, struct block *ipdomf)
9516 {
9517         do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
9518 }
9519
9520 static void unipdomf_block(struct block *block, struct block *unipdomf)
9521 {
9522         do_unuse_block(block, &block->ipdomfrontier, unipdomf);
9523 }
9524
9525
9526
9527 static int do_walk_triple(struct compile_state *state,
9528         struct triple *ptr, int depth,
9529         int (*cb)(struct compile_state *state, struct triple *ptr, int depth)) 
9530 {
9531         int result;
9532         result = cb(state, ptr, depth);
9533         if ((result == 0) && (ptr->op == OP_LIST)) {
9534                 struct triple *list;
9535                 list = ptr;
9536                 ptr = RHS(list, 0);
9537                 do {
9538                         result = do_walk_triple(state, ptr, depth + 1, cb);
9539                         if (ptr->next->prev != ptr) {
9540                                 internal_error(state, ptr->next, "bad prev");
9541                         }
9542                         ptr = ptr->next;
9543                         
9544                 } while((result == 0) && (ptr != RHS(list, 0)));
9545         }
9546         return result;
9547 }
9548
9549 static int walk_triple(
9550         struct compile_state *state, 
9551         struct triple *ptr, 
9552         int (*cb)(struct compile_state *state, struct triple *ptr, int depth))
9553 {
9554         return do_walk_triple(state, ptr, 0, cb);
9555 }
9556
9557 static void do_print_prefix(int depth)
9558 {
9559         int i;
9560         for(i = 0; i < depth; i++) {
9561                 printf("  ");
9562         }
9563 }
9564
9565 #define PRINT_LIST 1
9566 static int do_print_triple(struct compile_state *state, struct triple *ins, int depth)
9567 {
9568         int op;
9569         op = ins->op;
9570         if (op == OP_LIST) {
9571 #if !PRINT_LIST
9572                 return 0;
9573 #endif
9574         }
9575         if ((op == OP_LABEL) && (ins->use)) {
9576                 printf("\n%p:\n", ins);
9577         }
9578         do_print_prefix(depth);
9579         display_triple(stdout, ins);
9580
9581         if ((ins->op == OP_BRANCH) && ins->use) {
9582                 internal_error(state, ins, "branch used?");
9583         }
9584         if (triple_is_branch(state, ins)) {
9585                 printf("\n");
9586         }
9587         return 0;
9588 }
9589
9590 static void print_triple(struct compile_state *state, struct triple *ins)
9591 {
9592         walk_triple(state, ins, do_print_triple);
9593 }
9594
9595 static void print_triples(struct compile_state *state)
9596 {
9597         print_triple(state, state->main_function);
9598 }
9599
9600 struct cf_block {
9601         struct block *block;
9602 };
9603 static void find_cf_blocks(struct cf_block *cf, struct block *block)
9604 {
9605         if (!block || (cf[block->vertex].block == block)) {
9606                 return;
9607         }
9608         cf[block->vertex].block = block;
9609         find_cf_blocks(cf, block->left);
9610         find_cf_blocks(cf, block->right);
9611 }
9612
9613 static void print_control_flow(struct compile_state *state)
9614 {
9615         struct cf_block *cf;
9616         int i;
9617         printf("\ncontrol flow\n");
9618         cf = xcmalloc(sizeof(*cf) * (state->last_vertex + 1), "cf_block");
9619         find_cf_blocks(cf, state->first_block);
9620
9621         for(i = 1; i <= state->last_vertex; i++) {
9622                 struct block *block;
9623                 block = cf[i].block;
9624                 if (!block)
9625                         continue;
9626                 printf("(%p) %d:", block, block->vertex);
9627                 if (block->left) {
9628                         printf(" %d", block->left->vertex);
9629                 }
9630                 if (block->right && (block->right != block->left)) {
9631                         printf(" %d", block->right->vertex);
9632                 }
9633                 printf("\n");
9634         }
9635
9636         xfree(cf);
9637 }
9638
9639
9640 static struct block *basic_block(struct compile_state *state,
9641         struct triple *first)
9642 {
9643         struct block *block;
9644         struct triple *ptr, *final;
9645         int op;
9646         if (first->op != OP_LABEL) {
9647                 internal_error(state, 0, "block does not start with a label");
9648         }
9649         /* See if this basic block has already been setup */
9650         if (first->u.block != 0) {
9651                 return first->u.block;
9652         }
9653         /* Lookup the final instruction.
9654          * It is important that the final instruction has it's own
9655          * basic block.
9656          */
9657         final = RHS(state->main_function, 0)->prev;
9658         /* Allocate another basic block structure */
9659         state->last_vertex += 1;
9660         block = xcmalloc(sizeof(*block), "block");
9661         block->first = block->last = first;
9662         block->vertex = state->last_vertex;
9663         ptr = first;
9664         do {
9665                 if ((ptr != first) && (ptr->op == OP_LABEL) && 
9666                         ((ptr->use) || ptr == final)) {
9667                         break;
9668                 }
9669                 block->last = ptr;
9670                 /* If ptr->u is not used remember where the baic block is */
9671                 if (triple_stores_block(state, ptr)) {
9672                         ptr->u.block = block;
9673                 }
9674                 if (ptr->op == OP_BRANCH) {
9675                         break;
9676                 }
9677                 ptr = ptr->next;
9678         } while (ptr != RHS(state->main_function, 0));
9679         if (ptr == RHS(state->main_function, 0))
9680                 return block;
9681         op = ptr->op;
9682         if (op == OP_LABEL) {
9683                 block->left = basic_block(state, ptr);
9684                 block->right = 0;
9685                 use_block(block->left, block);
9686         }
9687         else if (op == OP_BRANCH) {
9688                 block->left = 0;
9689                 /* Trace the branch target */
9690                 block->right = basic_block(state, TARG(ptr, 0));
9691                 use_block(block->right, block);
9692                 /* If there is a test trace the branch as well */
9693                 if (TRIPLE_RHS(ptr->sizes)) {
9694                         block->left = basic_block(state, ptr->next);
9695                         use_block(block->left, block);
9696                 }
9697         }
9698         else {
9699                 internal_error(state, 0, "Bad basic block split");
9700         }
9701         return block;
9702 }
9703
9704
9705 static void walk_blocks(struct compile_state *state,
9706         void (*cb)(struct compile_state *state, struct block *block, void *arg),
9707         void *arg)
9708 {
9709         struct triple *ptr, *first;
9710         struct block *last_block;
9711         last_block = 0;
9712         first = RHS(state->main_function, 0);
9713         ptr = first;
9714         do {
9715                 struct block *block;
9716                 if (triple_stores_block(state, ptr)) {
9717                         block = ptr->u.block;
9718                         if (block && (block != last_block)) {
9719                                 cb(state, block, arg);
9720                         }
9721                         last_block = block;
9722                 }
9723                 if (block && (block->last == ptr)) {
9724                         block = 0;
9725                 }
9726                 ptr = ptr->next;
9727         } while(ptr != first);
9728 }
9729
9730 static void print_block(
9731         struct compile_state *state, struct block *block, void *arg)
9732 {
9733         struct block_set *user;
9734         struct triple *ptr;
9735         FILE *fp = arg;
9736
9737         fprintf(fp, "\nblock: %p (%d)  %p<-%p %p<-%p\n", 
9738                 block, 
9739                 block->vertex,
9740                 block->left, 
9741                 block->left && block->left->use?block->left->use->member : 0,
9742                 block->right, 
9743                 block->right && block->right->use?block->right->use->member : 0);
9744         if (block->first->op == OP_LABEL) {
9745                 fprintf(fp, "%p:\n", block->first);
9746         }
9747         for(ptr = block->first; ; ptr = ptr->next) {
9748                 display_triple(fp, ptr);
9749                 if (ptr == block->last)
9750                         break;
9751         }
9752         fprintf(fp, "users %d: ", block->users);
9753         for(user = block->use; user; user = user->next) {
9754                 fprintf(fp, "%p (%d) ", 
9755                         user->member,
9756                         user->member->vertex);
9757         }
9758         fprintf(fp,"\n\n");
9759 }
9760
9761
9762 static void print_blocks(struct compile_state *state, FILE *fp)
9763 {
9764         fprintf(fp, "--------------- blocks ---------------\n");
9765         walk_blocks(state, print_block, fp);
9766 }
9767
9768 static void prune_nonblock_triples(struct compile_state *state)
9769 {
9770         struct block *block;
9771         struct triple *first, *ins, *next;
9772         /* Delete the triples not in a basic block */
9773         first = RHS(state->main_function, 0);
9774         block = 0;
9775         ins = first;
9776         do {
9777                 next = ins->next;
9778                 if (ins->op == OP_LABEL) {
9779                         block = ins->u.block;
9780                 }
9781                 if (!block) {
9782                         release_triple(state, ins);
9783                 }
9784                 if (block && block->last == ins) {
9785                         block = 0;
9786                 }
9787                 ins = next;
9788         } while(ins != first);
9789 }
9790
9791 static void setup_basic_blocks(struct compile_state *state)
9792 {
9793         if (!triple_stores_block(state, RHS(state->main_function, 0)) ||
9794                 !triple_stores_block(state, RHS(state->main_function,0)->prev)) {
9795                 internal_error(state, 0, "ins will not store block?");
9796         }
9797         /* Find the basic blocks */
9798         state->last_vertex = 0;
9799         state->first_block = basic_block(state, RHS(state->main_function,0));
9800         /* Delete the triples not in a basic block */
9801         prune_nonblock_triples(state);
9802         /* Find the last basic block */
9803         state->last_block = RHS(state->main_function, 0)->prev->u.block;
9804         if (!state->last_block) {
9805                 internal_error(state, 0, "end not used?");
9806         }
9807         /* If we are debugging print what I have just done */
9808         if (state->debug & DEBUG_BASIC_BLOCKS) {
9809                 print_blocks(state, stdout);
9810                 print_control_flow(state);
9811         }
9812 }
9813
9814 static void free_basic_block(struct compile_state *state, struct block *block)
9815 {
9816         struct block_set *entry, *next;
9817         struct block *child;
9818         if (!block) {
9819                 return;
9820         }
9821         if (block->vertex == -1) {
9822                 return;
9823         }
9824         block->vertex = -1;
9825         if (block->left) {
9826                 unuse_block(block->left, block);
9827         }
9828         if (block->right) {
9829                 unuse_block(block->right, block);
9830         }
9831         if (block->idom) {
9832                 unidom_block(block->idom, block);
9833         }
9834         block->idom = 0;
9835         if (block->ipdom) {
9836                 unipdom_block(block->ipdom, block);
9837         }
9838         block->ipdom = 0;
9839         for(entry = block->use; entry; entry = next) {
9840                 next = entry->next;
9841                 child = entry->member;
9842                 unuse_block(block, child);
9843                 if (child->left == block) {
9844                         child->left = 0;
9845                 }
9846                 if (child->right == block) {
9847                         child->right = 0;
9848                 }
9849         }
9850         for(entry = block->idominates; entry; entry = next) {
9851                 next = entry->next;
9852                 child = entry->member;
9853                 unidom_block(block, child);
9854                 child->idom = 0;
9855         }
9856         for(entry = block->domfrontier; entry; entry = next) {
9857                 next = entry->next;
9858                 child = entry->member;
9859                 undomf_block(block, child);
9860         }
9861         for(entry = block->ipdominates; entry; entry = next) {
9862                 next = entry->next;
9863                 child = entry->member;
9864                 unipdom_block(block, child);
9865                 child->ipdom = 0;
9866         }
9867         for(entry = block->ipdomfrontier; entry; entry = next) {
9868                 next = entry->next;
9869                 child = entry->member;
9870                 unipdomf_block(block, child);
9871         }
9872         if (block->users != 0) {
9873                 internal_error(state, 0, "block still has users");
9874         }
9875         free_basic_block(state, block->left);
9876         block->left = 0;
9877         free_basic_block(state, block->right);
9878         block->right = 0;
9879         memset(block, -1, sizeof(*block));
9880         xfree(block);
9881 }
9882
9883 static void free_basic_blocks(struct compile_state *state)
9884 {
9885         struct triple *first, *ins;
9886         free_basic_block(state, state->first_block);
9887         state->last_vertex = 0;
9888         state->first_block = state->last_block = 0;
9889         first = RHS(state->main_function, 0);
9890         ins = first;
9891         do {
9892                 if (triple_stores_block(state, ins)) {
9893                         ins->u.block = 0;
9894                 }
9895                 ins = ins->next;
9896         } while(ins != first);
9897         
9898 }
9899
9900 struct sdom_block {
9901         struct block *block;
9902         struct sdom_block *sdominates;
9903         struct sdom_block *sdom_next;
9904         struct sdom_block *sdom;
9905         struct sdom_block *label;
9906         struct sdom_block *parent;
9907         struct sdom_block *ancestor;
9908         int vertex;
9909 };
9910
9911
9912 static void unsdom_block(struct sdom_block *block)
9913 {
9914         struct sdom_block **ptr;
9915         if (!block->sdom_next) {
9916                 return;
9917         }
9918         ptr = &block->sdom->sdominates;
9919         while(*ptr) {
9920                 if ((*ptr) == block) {
9921                         *ptr = block->sdom_next;
9922                         return;
9923                 }
9924                 ptr = &(*ptr)->sdom_next;
9925         }
9926 }
9927
9928 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
9929 {
9930         unsdom_block(block);
9931         block->sdom = sdom;
9932         block->sdom_next = sdom->sdominates;
9933         sdom->sdominates = block;
9934 }
9935
9936
9937
9938 static int initialize_sdblock(struct sdom_block *sd,
9939         struct block *parent, struct block *block, int vertex)
9940 {
9941         if (!block || (sd[block->vertex].block == block)) {
9942                 return vertex;
9943         }
9944         vertex += 1;
9945         /* Renumber the blocks in a convinient fashion */
9946         block->vertex = vertex;
9947         sd[vertex].block    = block;
9948         sd[vertex].sdom     = &sd[vertex];
9949         sd[vertex].label    = &sd[vertex];
9950         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9951         sd[vertex].ancestor = 0;
9952         sd[vertex].vertex   = vertex;
9953         vertex = initialize_sdblock(sd, block, block->left, vertex);
9954         vertex = initialize_sdblock(sd, block, block->right, vertex);
9955         return vertex;
9956 }
9957
9958 static int initialize_sdpblock(
9959         struct compile_state *state, struct sdom_block *sd,
9960         struct block *parent, struct block *block, int vertex)
9961 {
9962         struct block_set *user;
9963         if (!block || (sd[block->vertex].block == block)) {
9964                 return vertex;
9965         }
9966         vertex += 1;
9967         /* Renumber the blocks in a convinient fashion */
9968         block->vertex = vertex;
9969         sd[vertex].block    = block;
9970         sd[vertex].sdom     = &sd[vertex];
9971         sd[vertex].label    = &sd[vertex];
9972         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
9973         sd[vertex].ancestor = 0;
9974         sd[vertex].vertex   = vertex;
9975         for(user = block->use; user; user = user->next) {
9976                 vertex = initialize_sdpblock(state, sd, block, user->member, vertex);
9977         }
9978         return vertex;
9979 }
9980
9981 static int setup_sdpblocks(struct compile_state *state, struct sdom_block *sd)
9982 {
9983         struct block *block;
9984         int vertex;
9985         /* Setup as many sdpblocks as possible without using fake edges */
9986         vertex = initialize_sdpblock(state, sd, 0, state->last_block, 0);
9987
9988         /* Walk through the graph and find unconnected blocks.  If 
9989          * we can, add a fake edge from the unconnected blocks to the
9990          * end of the graph.
9991          */
9992         block = state->first_block->last->next->u.block;
9993         for(; block && block != state->first_block; block =  block->last->next->u.block) {
9994                 if (sd[block->vertex].block == block) {
9995                         continue;
9996                 }
9997                 if (block->left != 0) {
9998                         continue;
9999                 }
10000
10001 #if DEBUG_SDP_BLOCKS
10002                 fprintf(stderr, "Adding %d\n", vertex +1);
10003 #endif
10004
10005                 block->left = state->last_block;
10006                 use_block(block->left, block);
10007                 vertex = initialize_sdpblock(state, sd, state->last_block, block, vertex);
10008         }
10009         return vertex;
10010 }
10011
10012 static void compress_ancestors(struct sdom_block *v)
10013 {
10014         /* This procedure assumes ancestor(v) != 0 */
10015         /* if (ancestor(ancestor(v)) != 0) {
10016          *      compress(ancestor(ancestor(v)));
10017          *      if (semi(label(ancestor(v))) < semi(label(v))) {
10018          *              label(v) = label(ancestor(v));
10019          *      }
10020          *      ancestor(v) = ancestor(ancestor(v));
10021          * }
10022          */
10023         if (!v->ancestor) {
10024                 return;
10025         }
10026         if (v->ancestor->ancestor) {
10027                 compress_ancestors(v->ancestor->ancestor);
10028                 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
10029                         v->label = v->ancestor->label;
10030                 }
10031                 v->ancestor = v->ancestor->ancestor;
10032         }
10033 }
10034
10035 static void compute_sdom(struct compile_state *state, struct sdom_block *sd)
10036 {
10037         int i;
10038         /* // step 2 
10039          *  for each v <= pred(w) {
10040          *      u = EVAL(v);
10041          *      if (semi[u] < semi[w] { 
10042          *              semi[w] = semi[u]; 
10043          *      } 
10044          * }
10045          * add w to bucket(vertex(semi[w]));
10046          * LINK(parent(w), w);
10047          *
10048          * // step 3
10049          * for each v <= bucket(parent(w)) {
10050          *      delete v from bucket(parent(w));
10051          *      u = EVAL(v);
10052          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10053          * }
10054          */
10055         for(i = state->last_vertex; i >= 2; i--) {
10056                 struct sdom_block *v, *parent, *next;
10057                 struct block_set *user;
10058                 struct block *block;
10059                 block = sd[i].block;
10060                 parent = sd[i].parent;
10061                 /* Step 2 */
10062                 for(user = block->use; user; user = user->next) {
10063                         struct sdom_block *v, *u;
10064                         v = &sd[user->member->vertex];
10065                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10066                         if (u->sdom->vertex < sd[i].sdom->vertex) {
10067                                 sd[i].sdom = u->sdom;
10068                         }
10069                 }
10070                 sdom_block(sd[i].sdom, &sd[i]);
10071                 sd[i].ancestor = parent;
10072                 /* Step 3 */
10073                 for(v = parent->sdominates; v; v = next) {
10074                         struct sdom_block *u;
10075                         next = v->sdom_next;
10076                         unsdom_block(v);
10077                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10078                         v->block->idom = (u->sdom->vertex < v->sdom->vertex)? 
10079                                 u->block : parent->block;
10080                 }
10081         }
10082 }
10083
10084 static void compute_spdom(struct compile_state *state, struct sdom_block *sd)
10085 {
10086         int i;
10087         /* // step 2 
10088          *  for each v <= pred(w) {
10089          *      u = EVAL(v);
10090          *      if (semi[u] < semi[w] { 
10091          *              semi[w] = semi[u]; 
10092          *      } 
10093          * }
10094          * add w to bucket(vertex(semi[w]));
10095          * LINK(parent(w), w);
10096          *
10097          * // step 3
10098          * for each v <= bucket(parent(w)) {
10099          *      delete v from bucket(parent(w));
10100          *      u = EVAL(v);
10101          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
10102          * }
10103          */
10104         for(i = state->last_vertex; i >= 2; i--) {
10105                 struct sdom_block *u, *v, *parent, *next;
10106                 struct block *block;
10107                 block = sd[i].block;
10108                 parent = sd[i].parent;
10109                 /* Step 2 */
10110                 if (block->left) {
10111                         v = &sd[block->left->vertex];
10112                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10113                         if (u->sdom->vertex < sd[i].sdom->vertex) {
10114                                 sd[i].sdom = u->sdom;
10115                         }
10116                 }
10117                 if (block->right && (block->right != block->left)) {
10118                         v = &sd[block->right->vertex];
10119                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
10120                         if (u->sdom->vertex < sd[i].sdom->vertex) {
10121                                 sd[i].sdom = u->sdom;
10122                         }
10123                 }
10124                 sdom_block(sd[i].sdom, &sd[i]);
10125                 sd[i].ancestor = parent;
10126                 /* Step 3 */
10127                 for(v = parent->sdominates; v; v = next) {
10128                         struct sdom_block *u;
10129                         next = v->sdom_next;
10130                         unsdom_block(v);
10131                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
10132                         v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? 
10133                                 u->block : parent->block;
10134                 }
10135         }
10136 }
10137
10138 static void compute_idom(struct compile_state *state, struct sdom_block *sd)
10139 {
10140         int i;
10141         for(i = 2; i <= state->last_vertex; i++) {
10142                 struct block *block;
10143                 block = sd[i].block;
10144                 if (block->idom->vertex != sd[i].sdom->vertex) {
10145                         block->idom = block->idom->idom;
10146                 }
10147                 idom_block(block->idom, block);
10148         }
10149         sd[1].block->idom = 0;
10150 }
10151
10152 static void compute_ipdom(struct compile_state *state, struct sdom_block *sd)
10153 {
10154         int i;
10155         for(i = 2; i <= state->last_vertex; i++) {
10156                 struct block *block;
10157                 block = sd[i].block;
10158                 if (block->ipdom->vertex != sd[i].sdom->vertex) {
10159                         block->ipdom = block->ipdom->ipdom;
10160                 }
10161                 ipdom_block(block->ipdom, block);
10162         }
10163         sd[1].block->ipdom = 0;
10164 }
10165
10166         /* Theorem 1:
10167          *   Every vertex of a flowgraph G = (V, E, r) except r has
10168          *   a unique immediate dominator.  
10169          *   The edges {(idom(w), w) |w <= V - {r}} form a directed tree
10170          *   rooted at r, called the dominator tree of G, such that 
10171          *   v dominates w if and only if v is a proper ancestor of w in
10172          *   the dominator tree.
10173          */
10174         /* Lemma 1:  
10175          *   If v and w are vertices of G such that v <= w,
10176          *   than any path from v to w must contain a common ancestor
10177          *   of v and w in T.
10178          */
10179         /* Lemma 2:  For any vertex w != r, idom(w) -> w */
10180         /* Lemma 3:  For any vertex w != r, sdom(w) -> w */
10181         /* Lemma 4:  For any vertex w != r, idom(w) -> sdom(w) */
10182         /* Theorem 2:
10183          *   Let w != r.  Suppose every u for which sdom(w) -> u -> w satisfies
10184          *   sdom(u) >= sdom(w).  Then idom(w) = sdom(w).
10185          */
10186         /* Theorem 3:
10187          *   Let w != r and let u be a vertex for which sdom(u) is 
10188          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
10189          *   Then sdom(u) <= sdom(w) and idom(u) = idom(w).
10190          */
10191         /* Lemma 5:  Let vertices v,w satisfy v -> w.
10192          *           Then v -> idom(w) or idom(w) -> idom(v)
10193          */
10194
10195 static void find_immediate_dominators(struct compile_state *state)
10196 {
10197         struct sdom_block *sd;
10198         /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
10199          *           vi > w for (1 <= i <= k - 1}
10200          */
10201         /* Theorem 4:
10202          *   For any vertex w != r.
10203          *   sdom(w) = min(
10204          *                 {v|(v,w) <= E  and v < w } U 
10205          *                 {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
10206          */
10207         /* Corollary 1:
10208          *   Let w != r and let u be a vertex for which sdom(u) is 
10209          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
10210          *   Then:
10211          *                   { sdom(w) if sdom(w) = sdom(u),
10212          *        idom(w) = {
10213          *                   { idom(u) otherwise
10214          */
10215         /* The algorithm consists of the following 4 steps.
10216          * Step 1.  Carry out a depth-first search of the problem graph.  
10217          *    Number the vertices from 1 to N as they are reached during
10218          *    the search.  Initialize the variables used in succeeding steps.
10219          * Step 2.  Compute the semidominators of all vertices by applying
10220          *    theorem 4.   Carry out the computation vertex by vertex in
10221          *    decreasing order by number.
10222          * Step 3.  Implicitly define the immediate dominator of each vertex
10223          *    by applying Corollary 1.
10224          * Step 4.  Explicitly define the immediate dominator of each vertex,
10225          *    carrying out the computation vertex by vertex in increasing order
10226          *    by number.
10227          */
10228         /* Step 1 initialize the basic block information */
10229         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10230         initialize_sdblock(sd, 0, state->first_block, 0);
10231 #if 0
10232         sd[1].size  = 0;
10233         sd[1].label = 0;
10234         sd[1].sdom  = 0;
10235 #endif
10236         /* Step 2 compute the semidominators */
10237         /* Step 3 implicitly define the immediate dominator of each vertex */
10238         compute_sdom(state, sd);
10239         /* Step 4 explicitly define the immediate dominator of each vertex */
10240         compute_idom(state, sd);
10241         xfree(sd);
10242 }
10243
10244 static void find_post_dominators(struct compile_state *state)
10245 {
10246         struct sdom_block *sd;
10247         int vertex;
10248         /* Step 1 initialize the basic block information */
10249         sd = xcmalloc(sizeof(*sd) * (state->last_vertex + 1), "sdom_state");
10250
10251         vertex = setup_sdpblocks(state, sd);
10252         if (vertex != state->last_vertex) {
10253                 internal_error(state, 0, "missing %d blocks\n",
10254                         state->last_vertex - vertex);
10255         }
10256
10257         /* Step 2 compute the semidominators */
10258         /* Step 3 implicitly define the immediate dominator of each vertex */
10259         compute_spdom(state, sd);
10260         /* Step 4 explicitly define the immediate dominator of each vertex */
10261         compute_ipdom(state, sd);
10262         xfree(sd);
10263 }
10264
10265
10266
10267 static void find_block_domf(struct compile_state *state, struct block *block)
10268 {
10269         struct block *child;
10270         struct block_set *user;
10271         if (block->domfrontier != 0) {
10272                 internal_error(state, block->first, "domfrontier present?");
10273         }
10274         for(user = block->idominates; user; user = user->next) {
10275                 child = user->member;
10276                 if (child->idom != block) {
10277                         internal_error(state, block->first, "bad idom");
10278                 }
10279                 find_block_domf(state, child);
10280         }
10281         if (block->left && block->left->idom != block) {
10282                 domf_block(block, block->left);
10283         }
10284         if (block->right && block->right->idom != block) {
10285                 domf_block(block, block->right);
10286         }
10287         for(user = block->idominates; user; user = user->next) {
10288                 struct block_set *frontier;
10289                 child = user->member;
10290                 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
10291                         if (frontier->member->idom != block) {
10292                                 domf_block(block, frontier->member);
10293                         }
10294                 }
10295         }
10296 }
10297
10298 static void find_block_ipdomf(struct compile_state *state, struct block *block)
10299 {
10300         struct block *child;
10301         struct block_set *user;
10302         if (block->ipdomfrontier != 0) {
10303                 internal_error(state, block->first, "ipdomfrontier present?");
10304         }
10305         for(user = block->ipdominates; user; user = user->next) {
10306                 child = user->member;
10307                 if (child->ipdom != block) {
10308                         internal_error(state, block->first, "bad ipdom");
10309                 }
10310                 find_block_ipdomf(state, child);
10311         }
10312         if (block->left && block->left->ipdom != block) {
10313                 ipdomf_block(block, block->left);
10314         }
10315         if (block->right && block->right->ipdom != block) {
10316                 ipdomf_block(block, block->right);
10317         }
10318         for(user = block->idominates; user; user = user->next) {
10319                 struct block_set *frontier;
10320                 child = user->member;
10321                 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
10322                         if (frontier->member->ipdom != block) {
10323                                 ipdomf_block(block, frontier->member);
10324                         }
10325                 }
10326         }
10327 }
10328
10329 static void print_dominated(
10330         struct compile_state *state, struct block *block, void *arg)
10331 {
10332         struct block_set *user;
10333         FILE *fp = arg;
10334
10335         fprintf(fp, "%d:", block->vertex);
10336         for(user = block->idominates; user; user = user->next) {
10337                 fprintf(fp, " %d", user->member->vertex);
10338                 if (user->member->idom != block) {
10339                         internal_error(state, user->member->first, "bad idom");
10340                 }
10341         }
10342         fprintf(fp,"\n");
10343 }
10344
10345 static void print_dominators(struct compile_state *state, FILE *fp)
10346 {
10347         fprintf(fp, "\ndominates\n");
10348         walk_blocks(state, print_dominated, fp);
10349 }
10350
10351
10352 static int print_frontiers(
10353         struct compile_state *state, struct block *block, int vertex)
10354 {
10355         struct block_set *user;
10356
10357         if (!block || (block->vertex != vertex + 1)) {
10358                 return vertex;
10359         }
10360         vertex += 1;
10361
10362         printf("%d:", block->vertex);
10363         for(user = block->domfrontier; user; user = user->next) {
10364                 printf(" %d", user->member->vertex);
10365         }
10366         printf("\n");
10367
10368         vertex = print_frontiers(state, block->left, vertex);
10369         vertex = print_frontiers(state, block->right, vertex);
10370         return vertex;
10371 }
10372 static void print_dominance_frontiers(struct compile_state *state)
10373 {
10374         printf("\ndominance frontiers\n");
10375         print_frontiers(state, state->first_block, 0);
10376         
10377 }
10378
10379 static void analyze_idominators(struct compile_state *state)
10380 {
10381         /* Find the immediate dominators */
10382         find_immediate_dominators(state);
10383         /* Find the dominance frontiers */
10384         find_block_domf(state, state->first_block);
10385         /* If debuging print the print what I have just found */
10386         if (state->debug & DEBUG_FDOMINATORS) {
10387                 print_dominators(state, stdout);
10388                 print_dominance_frontiers(state);
10389                 print_control_flow(state);
10390         }
10391 }
10392
10393
10394
10395 static void print_ipdominated(
10396         struct compile_state *state, struct block *block, void *arg)
10397 {
10398         struct block_set *user;
10399         FILE *fp = arg;
10400
10401         fprintf(fp, "%d:", block->vertex);
10402         for(user = block->ipdominates; user; user = user->next) {
10403                 fprintf(fp, " %d", user->member->vertex);
10404                 if (user->member->ipdom != block) {
10405                         internal_error(state, user->member->first, "bad ipdom");
10406                 }
10407         }
10408         fprintf(fp, "\n");
10409 }
10410
10411 static void print_ipdominators(struct compile_state *state, FILE *fp)
10412 {
10413         fprintf(fp, "\nipdominates\n");
10414         walk_blocks(state, print_ipdominated, fp);
10415 }
10416
10417 static int print_pfrontiers(
10418         struct compile_state *state, struct block *block, int vertex)
10419 {
10420         struct block_set *user;
10421
10422         if (!block || (block->vertex != vertex + 1)) {
10423                 return vertex;
10424         }
10425         vertex += 1;
10426
10427         printf("%d:", block->vertex);
10428         for(user = block->ipdomfrontier; user; user = user->next) {
10429                 printf(" %d", user->member->vertex);
10430         }
10431         printf("\n");
10432         for(user = block->use; user; user = user->next) {
10433                 vertex = print_pfrontiers(state, user->member, vertex);
10434         }
10435         return vertex;
10436 }
10437 static void print_ipdominance_frontiers(struct compile_state *state)
10438 {
10439         printf("\nipdominance frontiers\n");
10440         print_pfrontiers(state, state->last_block, 0);
10441         
10442 }
10443
10444 static void analyze_ipdominators(struct compile_state *state)
10445 {
10446         /* Find the post dominators */
10447         find_post_dominators(state);
10448         /* Find the control dependencies (post dominance frontiers) */
10449         find_block_ipdomf(state, state->last_block);
10450         /* If debuging print the print what I have just found */
10451         if (state->debug & DEBUG_RDOMINATORS) {
10452                 print_ipdominators(state, stdout);
10453                 print_ipdominance_frontiers(state);
10454                 print_control_flow(state);
10455         }
10456 }
10457
10458 static int bdominates(struct compile_state *state,
10459         struct block *dom, struct block *sub)
10460 {
10461         while(sub && (sub != dom)) {
10462                 sub = sub->idom;
10463         }
10464         return sub == dom;
10465 }
10466
10467 static int tdominates(struct compile_state *state,
10468         struct triple *dom, struct triple *sub)
10469 {
10470         struct block *bdom, *bsub;
10471         int result;
10472         bdom = block_of_triple(state, dom);
10473         bsub = block_of_triple(state, sub);
10474         if (bdom != bsub) {
10475                 result = bdominates(state, bdom, bsub);
10476         } 
10477         else {
10478                 struct triple *ins;
10479                 ins = sub;
10480                 while((ins != bsub->first) && (ins != dom)) {
10481                         ins = ins->prev;
10482                 }
10483                 result = (ins == dom);
10484         }
10485         return result;
10486 }
10487
10488 static void insert_phi_operations(struct compile_state *state)
10489 {
10490         size_t size;
10491         struct triple *first;
10492         int *has_already, *work;
10493         struct block *work_list, **work_list_tail;
10494         int iter;
10495         struct triple *var, *vnext;
10496
10497         size = sizeof(int) * (state->last_vertex + 1);
10498         has_already = xcmalloc(size, "has_already");
10499         work =        xcmalloc(size, "work");
10500         iter = 0;
10501
10502         first = RHS(state->main_function, 0);
10503         for(var = first->next; var != first ; var = vnext) {
10504                 struct block *block;
10505                 struct triple_set *user, *unext;
10506                 vnext = var->next;
10507                 if ((var->op != OP_ADECL) || !var->use) {
10508                         continue;
10509                 }
10510                 iter += 1;
10511                 work_list = 0;
10512                 work_list_tail = &work_list;
10513                 for(user = var->use; user; user = unext) {
10514                         unext = user->next;
10515                         if (user->member->op == OP_READ) {
10516                                 continue;
10517                         }
10518                         if (user->member->op != OP_WRITE) {
10519                                 internal_error(state, user->member, 
10520                                         "bad variable access");
10521                         }
10522                         block = user->member->u.block;
10523                         if (!block) {
10524                                 warning(state, user->member, "dead code");
10525                                 release_triple(state, user->member);
10526                                 continue;
10527                         }
10528                         if (work[block->vertex] >= iter) {
10529                                 continue;
10530                         }
10531                         work[block->vertex] = iter;
10532                         *work_list_tail = block;
10533                         block->work_next = 0;
10534                         work_list_tail = &block->work_next;
10535                 }
10536                 for(block = work_list; block; block = block->work_next) {
10537                         struct block_set *df;
10538                         for(df = block->domfrontier; df; df = df->next) {
10539                                 struct triple *phi;
10540                                 struct block *front;
10541                                 int in_edges;
10542                                 front = df->member;
10543
10544                                 if (has_already[front->vertex] >= iter) {
10545                                         continue;
10546                                 }
10547                                 /* Count how many edges flow into this block */
10548                                 in_edges = front->users;
10549                                 /* Insert a phi function for this variable */
10550                                 get_occurance(var->occurance);
10551                                 phi = alloc_triple(
10552                                         state, OP_PHI, var->type, -1, in_edges, 
10553                                         var->occurance);
10554                                 phi->u.block = front;
10555                                 MISC(phi, 0) = var;
10556                                 use_triple(var, phi);
10557                                 /* Insert the phi functions immediately after the label */
10558                                 insert_triple(state, front->first->next, phi);
10559                                 if (front->first == front->last) {
10560                                         front->last = front->first->next;
10561                                 }
10562                                 has_already[front->vertex] = iter;
10563
10564                                 /* If necessary plan to visit the basic block */
10565                                 if (work[front->vertex] >= iter) {
10566                                         continue;
10567                                 }
10568                                 work[front->vertex] = iter;
10569                                 *work_list_tail = front;
10570                                 front->work_next = 0;
10571                                 work_list_tail = &front->work_next;
10572                         }
10573                 }
10574         }
10575         xfree(has_already);
10576         xfree(work);
10577 }
10578
10579
10580 static int count_and_number_adecls(struct compile_state *state)
10581 {
10582         struct triple *first, *ins;
10583         int adecls = 0;
10584         first = RHS(state->main_function, 0);
10585         ins = first;
10586         do {
10587                 if (ins->op == OP_ADECL) {
10588                         adecls += 1;
10589                         ins->id = adecls;
10590                 }
10591                 ins = ins->next;
10592         } while(ins != first);
10593         return adecls;
10594 }
10595
10596 static struct triple *peek_triple(struct triple_set **stacks, struct triple *var)
10597 {
10598         struct triple_set *head;
10599         struct triple *top_val;
10600         top_val = 0;
10601         head = stacks[var->id];
10602         if (head) {
10603                 top_val = head->member;
10604         }
10605         return top_val;
10606 }
10607
10608 static void push_triple(struct triple_set **stacks, struct triple *var, struct triple *val)
10609 {
10610         struct triple_set *new;
10611         /* Append new to the head of the list,
10612          * it's the only sensible behavoir for a stack.
10613          */
10614         new = xcmalloc(sizeof(*new), "triple_set");
10615         new->member = val;
10616         new->next   = stacks[var->id];
10617         stacks[var->id] = new;
10618 }
10619
10620 static void pop_triple(struct triple_set **stacks, struct triple *var, struct triple *oldval)
10621 {
10622         struct triple_set *set, **ptr;
10623         ptr = &stacks[var->id];
10624         while(*ptr) {
10625                 set = *ptr;
10626                 if (set->member == oldval) {
10627                         *ptr = set->next;
10628                         xfree(set);
10629                         /* Only free one occurance from the stack */
10630                         return;
10631                 }
10632                 else {
10633                         ptr = &set->next;
10634                 }
10635         }
10636 }
10637
10638 /*
10639  * C(V)
10640  * S(V)
10641  */
10642 static void fixup_block_phi_variables(
10643         struct compile_state *state, struct triple_set **stacks, struct block *parent, struct block *block)
10644 {
10645         struct block_set *set;
10646         struct triple *ptr;
10647         int edge;
10648         if (!parent || !block)
10649                 return;
10650         /* Find the edge I am coming in on */
10651         edge = 0;
10652         for(set = block->use; set; set = set->next, edge++) {
10653                 if (set->member == parent) {
10654                         break;
10655                 }
10656         }
10657         if (!set) {
10658                 internal_error(state, 0, "phi input is not on a control predecessor");
10659         }
10660         for(ptr = block->first; ; ptr = ptr->next) {
10661                 if (ptr->op == OP_PHI) {
10662                         struct triple *var, *val, **slot;
10663                         var = MISC(ptr, 0);
10664                         if (!var) {
10665                                 internal_error(state, ptr, "no var???");
10666                         }
10667                         /* Find the current value of the variable */
10668                         val = peek_triple(stacks, var);
10669                         if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
10670                                 internal_error(state, val, "bad value in phi");
10671                         }
10672                         if (edge >= TRIPLE_RHS(ptr->sizes)) {
10673                                 internal_error(state, ptr, "edges > phi rhs");
10674                         }
10675                         slot = &RHS(ptr, edge);
10676                         if ((*slot != 0) && (*slot != val)) {
10677                                 internal_error(state, ptr, "phi already bound on this edge");
10678                         }
10679                         *slot = val;
10680                         use_triple(val, ptr);
10681                 }
10682                 if (ptr == block->last) {
10683                         break;
10684                 }
10685         }
10686 }
10687
10688
10689 static void rename_block_variables(
10690         struct compile_state *state, struct triple_set **stacks, struct block *block)
10691 {
10692         struct block_set *user;
10693         struct triple *ptr, *next, *last;
10694         int done;
10695         if (!block)
10696                 return;
10697         last = block->first;
10698         done = 0;
10699         for(ptr = block->first; !done; ptr = next) {
10700                 next = ptr->next;
10701                 if (ptr == block->last) {
10702                         done = 1;
10703                 }
10704                 /* RHS(A) */
10705                 if (ptr->op == OP_READ) {
10706                         struct triple *var, *val;
10707                         var = RHS(ptr, 0);
10708                         unuse_triple(var, ptr);
10709                         /* Find the current value of the variable */
10710                         val = peek_triple(stacks, var);
10711                         if (!val) {
10712                                 error(state, ptr, "variable used without being set");
10713                         }
10714                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10715                                 internal_error(state, val, "bad value in read");
10716                         }
10717                         propogate_use(state, ptr, val);
10718                         release_triple(state, ptr);
10719                         continue;
10720                 }
10721                 /* LHS(A) */
10722                 if (ptr->op == OP_WRITE) {
10723                         struct triple *var, *val, *tval;
10724                         var = RHS(ptr, 0);
10725                         tval = val = RHS(ptr, 1);
10726                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
10727                                 internal_error(state, ptr, "bad value in write");
10728                         }
10729                         /* Insert a copy if the types differ */
10730                         if (!equiv_types(ptr->type, val->type)) {
10731                                 if (val->op == OP_INTCONST) {
10732                                         tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
10733                                         tval->u.cval = val->u.cval;
10734                                 }
10735                                 else {
10736                                         tval = pre_triple(state, ptr, OP_COPY, ptr->type, val, 0);
10737                                         use_triple(val, tval);
10738                                 }
10739                                 unuse_triple(val, ptr);
10740                                 RHS(ptr, 1) = tval;
10741                                 use_triple(tval, ptr);
10742                         }
10743                         propogate_use(state, ptr, tval);
10744                         unuse_triple(var, ptr);
10745                         /* Push OP_WRITE ptr->right onto a stack of variable uses */
10746                         push_triple(stacks, var, tval);
10747                 }
10748                 if (ptr->op == OP_PHI) {
10749                         struct triple *var;
10750                         var = MISC(ptr, 0);
10751                         /* Push OP_PHI onto a stack of variable uses */
10752                         push_triple(stacks, var, ptr);
10753                 }
10754                 last = ptr;
10755         }
10756         block->last = last;
10757
10758         /* Fixup PHI functions in the cf successors */
10759         fixup_block_phi_variables(state, stacks, block, block->left);
10760         fixup_block_phi_variables(state, stacks, block, block->right);
10761         /* rename variables in the dominated nodes */
10762         for(user = block->idominates; user; user = user->next) {
10763                 rename_block_variables(state, stacks, user->member);
10764         }
10765         /* pop the renamed variable stack */
10766         last = block->first;
10767         done = 0;
10768         for(ptr = block->first; !done ; ptr = next) {
10769                 next = ptr->next;
10770                 if (ptr == block->last) {
10771                         done = 1;
10772                 }
10773                 if (ptr->op == OP_WRITE) {
10774                         struct triple *var;
10775                         var = RHS(ptr, 0);
10776                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10777                         pop_triple(stacks, var, RHS(ptr, 1));
10778                         release_triple(state, ptr);
10779                         continue;
10780                 }
10781                 if (ptr->op == OP_PHI) {
10782                         struct triple *var;
10783                         var = MISC(ptr, 0);
10784                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
10785                         pop_triple(stacks, var, ptr);
10786                 }
10787                 last = ptr;
10788         }
10789         block->last = last;
10790 }
10791
10792 static void prune_block_variables(struct compile_state *state,
10793         struct block *block)
10794 {
10795         struct block_set *user;
10796         struct triple *next, *last, *ptr;
10797         int done;
10798         last = block->first;
10799         done = 0;
10800         for(ptr = block->first; !done; ptr = next) {
10801                 next = ptr->next;
10802                 if (ptr == block->last) {
10803                         done = 1;
10804                 }
10805                 if (ptr->op == OP_ADECL) {
10806                         struct triple_set *user, *next;
10807                         for(user = ptr->use; user; user = next) {
10808                                 struct triple *use;
10809                                 next = user->next;
10810                                 use = user->member;
10811                                 if (use->op != OP_PHI) {
10812                                         internal_error(state, use, "decl still used");
10813                                 }
10814                                 if (MISC(use, 0) != ptr) {
10815                                         internal_error(state, use, "bad phi use of decl");
10816                                 }
10817                                 unuse_triple(ptr, use);
10818                                 MISC(use, 0) = 0;
10819                         }
10820                         release_triple(state, ptr);
10821                         continue;
10822                 }
10823                 last = ptr;
10824         }
10825         block->last = last;
10826         for(user = block->idominates; user; user = user->next) {
10827                 prune_block_variables(state, user->member);
10828         }
10829 }
10830
10831 struct phi_triple {
10832         struct triple *phi;
10833         unsigned orig_id;
10834         int alive;
10835 };
10836
10837 static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
10838 {
10839         struct triple **slot;
10840         int zrhs, i;
10841         if (live[phi->id].alive) {
10842                 return;
10843         }
10844         live[phi->id].alive = 1;
10845         zrhs = TRIPLE_RHS(phi->sizes);
10846         slot = &RHS(phi, 0);
10847         for(i = 0; i < zrhs; i++) {
10848                 struct triple *used;
10849                 used = slot[i];
10850                 if (used && (used->op == OP_PHI)) {
10851                         keep_phi(state, live, used);
10852                 }
10853         }
10854 }
10855
10856 static void prune_unused_phis(struct compile_state *state)
10857 {
10858         struct triple *first, *phi;
10859         struct phi_triple *live;
10860         int phis, i;
10861         
10862
10863         /* Find the first instruction */
10864         first = RHS(state->main_function, 0);
10865
10866         /* Count how many phi functions I need to process */
10867         phis = 0;
10868         for(phi = first->next; phi != first; phi = phi->next) {
10869                 if (phi->op == OP_PHI) {
10870                         phis += 1;
10871                 }
10872         }
10873         
10874         /* Mark them all dead */
10875         live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
10876         phis = 0;
10877         for(phi = first->next; phi != first; phi = phi->next) {
10878                 if (phi->op != OP_PHI) {
10879                         continue;
10880                 }
10881                 live[phis].alive   = 0;
10882                 live[phis].orig_id = phi->id;
10883                 live[phis].phi     = phi;
10884                 phi->id = phis;
10885                 phis += 1;
10886         }
10887         
10888         /* Mark phis alive that are used by non phis */
10889         for(i = 0; i < phis; i++) {
10890                 struct triple_set *set;
10891                 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
10892                         if (set->member->op != OP_PHI) {
10893                                 keep_phi(state, live, live[i].phi);
10894                                 break;
10895                         }
10896                 }
10897         }
10898
10899         /* Delete the extraneous phis */
10900         for(i = 0; i < phis; i++) {
10901                 struct triple **slot;
10902                 int zrhs, j;
10903                 if (!live[i].alive) {
10904                         release_triple(state, live[i].phi);
10905                         continue;
10906                 }
10907                 phi = live[i].phi;
10908                 slot = &RHS(phi, 0);
10909                 zrhs = TRIPLE_RHS(phi->sizes);
10910                 for(j = 0; j < zrhs; j++) {
10911                         if(!slot[j]) {
10912                                 error(state, phi, "variable not set on all paths to use");
10913                         }
10914                 }
10915         }
10916         xfree(live);
10917 }
10918
10919
10920 static void transform_to_ssa_form(struct compile_state *state)
10921 {
10922         struct triple_set **stacks;
10923         int adecls;
10924         insert_phi_operations(state);
10925 #if 0
10926         printf("@%s:%d\n", __FILE__, __LINE__);
10927         print_blocks(state, stdout);
10928 #endif
10929
10930         /* Allocate stacks for the Variables */
10931         adecls = count_and_number_adecls(state);
10932         stacks = xcmalloc(sizeof(stacks[0])*(adecls + 1), "adecl stacks");
10933         rename_block_variables(state, stacks, state->first_block);
10934         xfree(stacks);
10935
10936         prune_block_variables(state, state->first_block);
10937
10938 #if 1
10939         prune_unused_phis(state);
10940 #endif
10941
10942 }
10943
10944
10945 static void clear_vertex(
10946         struct compile_state *state, struct block *block, void *arg)
10947 {
10948         block->vertex = 0;
10949 }
10950
10951 static void mark_live_block(
10952         struct compile_state *state, struct block *block, int *next_vertex)
10953 {
10954         /* See if this is a block that has not been marked */
10955         if (block->vertex != 0) {
10956                 return;
10957         }
10958         block->vertex = *next_vertex;
10959         *next_vertex += 1;
10960         if (triple_is_branch(state, block->last)) {
10961                 struct triple **targ;
10962                 targ = triple_targ(state, block->last, 0);
10963                 for(; targ; targ = triple_targ(state, block->last, targ)) {
10964                         if (!*targ) {
10965                                 continue;
10966                         }
10967                         if (!triple_stores_block(state, *targ)) {
10968                                 internal_error(state, 0, "bad targ");
10969                         }
10970                         mark_live_block(state, (*targ)->u.block, next_vertex);
10971                 }
10972         }
10973         else if (block->last->next != RHS(state->main_function, 0)) {
10974                 struct triple *ins;
10975                 ins = block->last->next;
10976                 if (!triple_stores_block(state, ins)) {
10977                         internal_error(state, 0, "bad block start");
10978                 }
10979                 mark_live_block(state, ins->u.block, next_vertex);
10980         }
10981 }
10982
10983 static void transform_from_ssa_form(struct compile_state *state)
10984 {
10985         /* To get out of ssa form we insert moves on the incoming
10986          * edges to blocks containting phi functions.
10987          */
10988         struct triple *first;
10989         struct triple *phi, *next;
10990         int next_vertex;
10991
10992         /* Walk the control flow to see which blocks remain alive */
10993         walk_blocks(state, clear_vertex, 0);
10994         next_vertex = 1;
10995         mark_live_block(state, state->first_block, &next_vertex);
10996
10997         /* Walk all of the operations to find the phi functions */
10998         first = RHS(state->main_function, 0);
10999         for(phi = first->next; phi != first ; phi = next) {
11000                 struct block_set *set;
11001                 struct block *block;
11002                 struct triple **slot;
11003                 struct triple *var, *read;
11004                 struct triple_set *use, *use_next;
11005                 int edge, used;
11006                 next = phi->next;
11007                 if (phi->op != OP_PHI) {
11008                         continue;
11009                 }
11010                 block = phi->u.block;
11011                 slot  = &RHS(phi, 0);
11012
11013                 /* Forget uses from code in dead blocks */
11014                 for(use = phi->use; use; use = use_next) {
11015                         struct block *ublock;
11016                         struct triple **expr;
11017                         use_next = use->next;
11018                         ublock = block_of_triple(state, use->member);
11019                         if ((use->member == phi) || (ublock->vertex != 0)) {
11020                                 continue;
11021                         }
11022                         expr = triple_rhs(state, use->member, 0);
11023                         for(; expr; expr = triple_rhs(state, use->member, expr)) {
11024                                 if (*expr == phi) {
11025                                         *expr = 0;
11026                                 }
11027                         }
11028                         unuse_triple(phi, use->member);
11029                 }
11030
11031 #warning "CHECK_ME does the OP_ADECL need to be placed somewhere that dominates all of the incoming phi edges?"
11032                 /* A variable to replace the phi function */
11033                 var = post_triple(state, phi, OP_ADECL, phi->type, 0,0);
11034                 /* A read of the single value that is set into the variable */
11035                 read = post_triple(state, var, OP_READ, phi->type, var, 0);
11036                 use_triple(var, read);
11037
11038                 /* Replaces uses of the phi with variable reads */
11039                 propogate_use(state, phi, read);
11040
11041                 /* Walk all of the incoming edges/blocks and insert moves.
11042                  */
11043                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
11044                         struct block *eblock;
11045                         struct triple *move;
11046                         struct triple *val, *base;
11047                         eblock = set->member;
11048                         val = slot[edge];
11049                         slot[edge] = 0;
11050                         unuse_triple(val, phi);
11051
11052                         if (!val || (val == &zero_triple) ||
11053                                 (block->vertex == 0) || (eblock->vertex == 0) ||
11054                                 (val == phi) || (val == read)) {
11055                                 continue;
11056                         }
11057                         
11058                         /* Make certain the write is placed in the edge block... */
11059                         base = eblock->first;
11060                         if (block_of_triple(state, val) == eblock) {
11061                                 base = val;
11062                         }
11063                         move = post_triple(state, base, OP_WRITE, phi->type, var, val);
11064                         use_triple(val, move);
11065                         use_triple(var, move);
11066                 }               
11067                 /* See if there are any writers of var */
11068                 used = 0;
11069                 for(use = var->use; use; use = use->next) {
11070                         if ((use->member->op == OP_WRITE) &&
11071                                 (RHS(use->member, 0) == var)) {
11072                                 used = 1;
11073                         }
11074                 }
11075                 /* If var is not used free it */
11076                 if (!used) {
11077                         unuse_triple(var, read);
11078                         free_triple(state, read);
11079                         free_triple(state, var);
11080                 }
11081
11082                 /* Release the phi function */
11083                 release_triple(state, phi);
11084         }
11085         
11086 }
11087
11088
11089 /* 
11090  * Register conflict resolution
11091  * =========================================================
11092  */
11093
11094 static struct reg_info find_def_color(
11095         struct compile_state *state, struct triple *def)
11096 {
11097         struct triple_set *set;
11098         struct reg_info info;
11099         info.reg = REG_UNSET;
11100         info.regcm = 0;
11101         if (!triple_is_def(state, def)) {
11102                 return info;
11103         }
11104         info = arch_reg_lhs(state, def, 0);
11105         if (info.reg >= MAX_REGISTERS) {
11106                 info.reg = REG_UNSET;
11107         }
11108         for(set = def->use; set; set = set->next) {
11109                 struct reg_info tinfo;
11110                 int i;
11111                 i = find_rhs_use(state, set->member, def);
11112                 if (i < 0) {
11113                         continue;
11114                 }
11115                 tinfo = arch_reg_rhs(state, set->member, i);
11116                 if (tinfo.reg >= MAX_REGISTERS) {
11117                         tinfo.reg = REG_UNSET;
11118                 }
11119                 if ((tinfo.reg != REG_UNSET) && 
11120                         (info.reg != REG_UNSET) &&
11121                         (tinfo.reg != info.reg)) {
11122                         internal_error(state, def, "register conflict");
11123                 }
11124                 if ((info.regcm & tinfo.regcm) == 0) {
11125                         internal_error(state, def, "regcm conflict %x & %x == 0",
11126                                 info.regcm, tinfo.regcm);
11127                 }
11128                 if (info.reg == REG_UNSET) {
11129                         info.reg = tinfo.reg;
11130                 }
11131                 info.regcm &= tinfo.regcm;
11132         }
11133         if (info.reg >= MAX_REGISTERS) {
11134                 internal_error(state, def, "register out of range");
11135         }
11136         return info;
11137 }
11138
11139 static struct reg_info find_lhs_pre_color(
11140         struct compile_state *state, struct triple *ins, int index)
11141 {
11142         struct reg_info info;
11143         int zlhs, zrhs, i;
11144         zrhs = TRIPLE_RHS(ins->sizes);
11145         zlhs = TRIPLE_LHS(ins->sizes);
11146         if (!zlhs && triple_is_def(state, ins)) {
11147                 zlhs = 1;
11148         }
11149         if (index >= zlhs) {
11150                 internal_error(state, ins, "Bad lhs %d", index);
11151         }
11152         info = arch_reg_lhs(state, ins, index);
11153         for(i = 0; i < zrhs; i++) {
11154                 struct reg_info rinfo;
11155                 rinfo = arch_reg_rhs(state, ins, i);
11156                 if ((info.reg == rinfo.reg) &&
11157                         (rinfo.reg >= MAX_REGISTERS)) {
11158                         struct reg_info tinfo;
11159                         tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
11160                         info.reg = tinfo.reg;
11161                         info.regcm &= tinfo.regcm;
11162                         break;
11163                 }
11164         }
11165         if (info.reg >= MAX_REGISTERS) {
11166                 info.reg = REG_UNSET;
11167         }
11168         return info;
11169 }
11170
11171 static struct reg_info find_rhs_post_color(
11172         struct compile_state *state, struct triple *ins, int index);
11173
11174 static struct reg_info find_lhs_post_color(
11175         struct compile_state *state, struct triple *ins, int index)
11176 {
11177         struct triple_set *set;
11178         struct reg_info info;
11179         struct triple *lhs;
11180 #if DEBUG_TRIPLE_COLOR
11181         fprintf(stderr, "find_lhs_post_color(%p, %d)\n",
11182                 ins, index);
11183 #endif
11184         if ((index == 0) && triple_is_def(state, ins)) {
11185                 lhs = ins;
11186         }
11187         else if (index < TRIPLE_LHS(ins->sizes)) {
11188                 lhs = LHS(ins, index);
11189         }
11190         else {
11191                 internal_error(state, ins, "Bad lhs %d", index);
11192                 lhs = 0;
11193         }
11194         info = arch_reg_lhs(state, ins, index);
11195         if (info.reg >= MAX_REGISTERS) {
11196                 info.reg = REG_UNSET;
11197         }
11198         for(set = lhs->use; set; set = set->next) {
11199                 struct reg_info rinfo;
11200                 struct triple *user;
11201                 int zrhs, i;
11202                 user = set->member;
11203                 zrhs = TRIPLE_RHS(user->sizes);
11204                 for(i = 0; i < zrhs; i++) {
11205                         if (RHS(user, i) != lhs) {
11206                                 continue;
11207                         }
11208                         rinfo = find_rhs_post_color(state, user, i);
11209                         if ((info.reg != REG_UNSET) &&
11210                                 (rinfo.reg != REG_UNSET) &&
11211                                 (info.reg != rinfo.reg)) {
11212                                 internal_error(state, ins, "register conflict");
11213                         }
11214                         if ((info.regcm & rinfo.regcm) == 0) {
11215                                 internal_error(state, ins, "regcm conflict %x & %x == 0",
11216                                         info.regcm, rinfo.regcm);
11217                         }
11218                         if (info.reg == REG_UNSET) {
11219                                 info.reg = rinfo.reg;
11220                         }
11221                         info.regcm &= rinfo.regcm;
11222                 }
11223         }
11224 #if DEBUG_TRIPLE_COLOR
11225         fprintf(stderr, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
11226                 ins, index, info.reg, info.regcm);
11227 #endif
11228         return info;
11229 }
11230
11231 static struct reg_info find_rhs_post_color(
11232         struct compile_state *state, struct triple *ins, int index)
11233 {
11234         struct reg_info info, rinfo;
11235         int zlhs, i;
11236 #if DEBUG_TRIPLE_COLOR
11237         fprintf(stderr, "find_rhs_post_color(%p, %d)\n",
11238                 ins, index);
11239 #endif
11240         rinfo = arch_reg_rhs(state, ins, index);
11241         zlhs = TRIPLE_LHS(ins->sizes);
11242         if (!zlhs && triple_is_def(state, ins)) {
11243                 zlhs = 1;
11244         }
11245         info = rinfo;
11246         if (info.reg >= MAX_REGISTERS) {
11247                 info.reg = REG_UNSET;
11248         }
11249         for(i = 0; i < zlhs; i++) {
11250                 struct reg_info linfo;
11251                 linfo = arch_reg_lhs(state, ins, i);
11252                 if ((linfo.reg == rinfo.reg) &&
11253                         (linfo.reg >= MAX_REGISTERS)) {
11254                         struct reg_info tinfo;
11255                         tinfo = find_lhs_post_color(state, ins, i);
11256                         if (tinfo.reg >= MAX_REGISTERS) {
11257                                 tinfo.reg = REG_UNSET;
11258                         }
11259                         info.regcm &= linfo.regcm;
11260                         info.regcm &= tinfo.regcm;
11261                         if (info.reg != REG_UNSET) {
11262                                 internal_error(state, ins, "register conflict");
11263                         }
11264                         if (info.regcm == 0) {
11265                                 internal_error(state, ins, "regcm conflict");
11266                         }
11267                         info.reg = tinfo.reg;
11268                 }
11269         }
11270 #if DEBUG_TRIPLE_COLOR
11271         fprintf(stderr, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
11272                 ins, index, info.reg, info.regcm);
11273 #endif
11274         return info;
11275 }
11276
11277 static struct reg_info find_lhs_color(
11278         struct compile_state *state, struct triple *ins, int index)
11279 {
11280         struct reg_info pre, post, info;
11281 #if DEBUG_TRIPLE_COLOR
11282         fprintf(stderr, "find_lhs_color(%p, %d)\n",
11283                 ins, index);
11284 #endif
11285         pre = find_lhs_pre_color(state, ins, index);
11286         post = find_lhs_post_color(state, ins, index);
11287         if ((pre.reg != post.reg) &&
11288                 (pre.reg != REG_UNSET) &&
11289                 (post.reg != REG_UNSET)) {
11290                 internal_error(state, ins, "register conflict");
11291         }
11292         info.regcm = pre.regcm & post.regcm;
11293         info.reg = pre.reg;
11294         if (info.reg == REG_UNSET) {
11295                 info.reg = post.reg;
11296         }
11297 #if DEBUG_TRIPLE_COLOR
11298         fprintf(stderr, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
11299                 ins, index, info.reg, info.regcm,
11300                 pre.reg, pre.regcm, post.reg, post.regcm);
11301 #endif
11302         return info;
11303 }
11304
11305 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
11306 {
11307         struct triple_set *entry, *next;
11308         struct triple *out;
11309         struct reg_info info, rinfo;
11310
11311         info = arch_reg_lhs(state, ins, 0);
11312         out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
11313         use_triple(RHS(out, 0), out);
11314         /* Get the users of ins to use out instead */
11315         for(entry = ins->use; entry; entry = next) {
11316                 int i;
11317                 next = entry->next;
11318                 if (entry->member == out) {
11319                         continue;
11320                 }
11321                 i = find_rhs_use(state, entry->member, ins);
11322                 if (i < 0) {
11323                         continue;
11324                 }
11325                 rinfo = arch_reg_rhs(state, entry->member, i);
11326                 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
11327                         continue;
11328                 }
11329                 replace_rhs_use(state, ins, out, entry->member);
11330         }
11331         transform_to_arch_instruction(state, out);
11332         return out;
11333 }
11334
11335 static struct triple *typed_pre_copy(
11336         struct compile_state *state, struct type *type, struct triple *ins, int index)
11337 {
11338         /* Carefully insert enough operations so that I can
11339          * enter any operation with a GPR32.
11340          */
11341         struct triple *in;
11342         struct triple **expr;
11343         unsigned classes;
11344         struct reg_info info;
11345         if (ins->op == OP_PHI) {
11346                 internal_error(state, ins, "pre_copy on a phi?");
11347         }
11348         classes = arch_type_to_regcm(state, type);
11349         info = arch_reg_rhs(state, ins, index);
11350         expr = &RHS(ins, index);
11351         if ((info.regcm & classes) == 0) {
11352                 internal_error(state, ins, "pre_copy with no register classes");
11353         }
11354         in = pre_triple(state, ins, OP_COPY, type, *expr, 0);
11355         unuse_triple(*expr, ins);
11356         *expr = in;
11357         use_triple(RHS(in, 0), in);
11358         use_triple(in, ins);
11359         transform_to_arch_instruction(state, in);
11360         return in;
11361         
11362 }
11363 static struct triple *pre_copy(
11364         struct compile_state *state, struct triple *ins, int index)
11365 {
11366         return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
11367 }
11368
11369
11370 static void insert_copies_to_phi(struct compile_state *state)
11371 {
11372         /* To get out of ssa form we insert moves on the incoming
11373          * edges to blocks containting phi functions.
11374          */
11375         struct triple *first;
11376         struct triple *phi;
11377
11378         /* Walk all of the operations to find the phi functions */
11379         first = RHS(state->main_function, 0);
11380         for(phi = first->next; phi != first ; phi = phi->next) {
11381                 struct block_set *set;
11382                 struct block *block;
11383                 struct triple **slot, *copy;
11384                 int edge;
11385                 if (phi->op != OP_PHI) {
11386                         continue;
11387                 }
11388                 phi->id |= TRIPLE_FLAG_POST_SPLIT;
11389                 block = phi->u.block;
11390                 slot  = &RHS(phi, 0);
11391                 /* Phi's that feed into mandatory live range joins
11392                  * cause nasty complications.  Insert a copy of
11393                  * the phi value so I never have to deal with
11394                  * that in the rest of the code.
11395                  */
11396                 copy = post_copy(state, phi);
11397                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
11398                 /* Walk all of the incoming edges/blocks and insert moves.
11399                  */
11400                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
11401                         struct block *eblock;
11402                         struct triple *move;
11403                         struct triple *val;
11404                         struct triple *ptr;
11405                         eblock = set->member;
11406                         val = slot[edge];
11407
11408                         if (val == phi) {
11409                                 continue;
11410                         }
11411
11412                         get_occurance(val->occurance);
11413                         move = build_triple(state, OP_COPY, phi->type, val, 0,
11414                                 val->occurance);
11415                         move->u.block = eblock;
11416                         move->id |= TRIPLE_FLAG_PRE_SPLIT;
11417                         use_triple(val, move);
11418                         
11419                         slot[edge] = move;
11420                         unuse_triple(val, phi);
11421                         use_triple(move, phi);
11422
11423                         /* Walk up the dominator tree until I have found the appropriate block */
11424                         while(eblock && !tdominates(state, val, eblock->last)) {
11425                                 eblock = eblock->idom;
11426                         }
11427                         if (!eblock) {
11428                                 internal_error(state, phi, "Cannot find block dominated by %p",
11429                                         val);
11430                         }
11431
11432                         /* Walk through the block backwards to find
11433                          * an appropriate location for the OP_COPY.
11434                          */
11435                         for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
11436                                 struct triple **expr;
11437                                 if ((ptr == phi) || (ptr == val)) {
11438                                         goto out;
11439                                 }
11440                                 expr = triple_rhs(state, ptr, 0);
11441                                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11442                                         if ((*expr) == phi) {
11443                                                 goto out;
11444                                         }
11445                                 }
11446                         }
11447                 out:
11448                         if (triple_is_branch(state, ptr)) {
11449                                 internal_error(state, ptr,
11450                                         "Could not insert write to phi");
11451                         }
11452                         insert_triple(state, ptr->next, move);
11453                         if (eblock->last == ptr) {
11454                                 eblock->last = move;
11455                         }
11456                         transform_to_arch_instruction(state, move);
11457                 }
11458         }
11459 }
11460
11461 struct triple_reg_set {
11462         struct triple_reg_set *next;
11463         struct triple *member;
11464         struct triple *new;
11465 };
11466
11467 struct reg_block {
11468         struct block *block;
11469         struct triple_reg_set *in;
11470         struct triple_reg_set *out;
11471         int vertex;
11472 };
11473
11474 static int do_triple_set(struct triple_reg_set **head, 
11475         struct triple *member, struct triple *new_member)
11476 {
11477         struct triple_reg_set **ptr, *new;
11478         if (!member)
11479                 return 0;
11480         ptr = head;
11481         while(*ptr) {
11482                 if ((*ptr)->member == member) {
11483                         return 0;
11484                 }
11485                 ptr = &(*ptr)->next;
11486         }
11487         new = xcmalloc(sizeof(*new), "triple_set");
11488         new->member = member;
11489         new->new    = new_member;
11490         new->next   = *head;
11491         *head       = new;
11492         return 1;
11493 }
11494
11495 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
11496 {
11497         struct triple_reg_set *entry, **ptr;
11498         ptr = head;
11499         while(*ptr) {
11500                 entry = *ptr;
11501                 if (entry->member == member) {
11502                         *ptr = entry->next;
11503                         xfree(entry);
11504                         return;
11505                 }
11506                 else {
11507                         ptr = &entry->next;
11508                 }
11509         }
11510 }
11511
11512 static int in_triple(struct reg_block *rb, struct triple *in)
11513 {
11514         return do_triple_set(&rb->in, in, 0);
11515 }
11516 static void unin_triple(struct reg_block *rb, struct triple *unin)
11517 {
11518         do_triple_unset(&rb->in, unin);
11519 }
11520
11521 static int out_triple(struct reg_block *rb, struct triple *out)
11522 {
11523         return do_triple_set(&rb->out, out, 0);
11524 }
11525 static void unout_triple(struct reg_block *rb, struct triple *unout)
11526 {
11527         do_triple_unset(&rb->out, unout);
11528 }
11529
11530 static int initialize_regblock(struct reg_block *blocks,
11531         struct block *block, int vertex)
11532 {
11533         struct block_set *user;
11534         if (!block || (blocks[block->vertex].block == block)) {
11535                 return vertex;
11536         }
11537         vertex += 1;
11538         /* Renumber the blocks in a convinient fashion */
11539         block->vertex = vertex;
11540         blocks[vertex].block    = block;
11541         blocks[vertex].vertex   = vertex;
11542         for(user = block->use; user; user = user->next) {
11543                 vertex = initialize_regblock(blocks, user->member, vertex);
11544         }
11545         return vertex;
11546 }
11547
11548 static int phi_in(struct compile_state *state, struct reg_block *blocks,
11549         struct reg_block *rb, struct block *suc)
11550 {
11551         /* Read the conditional input set of a successor block
11552          * (i.e. the input to the phi nodes) and place it in the
11553          * current blocks output set.
11554          */
11555         struct block_set *set;
11556         struct triple *ptr;
11557         int edge;
11558         int done, change;
11559         change = 0;
11560         /* Find the edge I am coming in on */
11561         for(edge = 0, set = suc->use; set; set = set->next, edge++) {
11562                 if (set->member == rb->block) {
11563                         break;
11564                 }
11565         }
11566         if (!set) {
11567                 internal_error(state, 0, "Not coming on a control edge?");
11568         }
11569         for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
11570                 struct triple **slot, *expr, *ptr2;
11571                 int out_change, done2;
11572                 done = (ptr == suc->last);
11573                 if (ptr->op != OP_PHI) {
11574                         continue;
11575                 }
11576                 slot = &RHS(ptr, 0);
11577                 expr = slot[edge];
11578                 out_change = out_triple(rb, expr);
11579                 if (!out_change) {
11580                         continue;
11581                 }
11582                 /* If we don't define the variable also plast it
11583                  * in the current blocks input set.
11584                  */
11585                 ptr2 = rb->block->first;
11586                 for(done2 = 0; !done2; ptr2 = ptr2->next) {
11587                         if (ptr2 == expr) {
11588                                 break;
11589                         }
11590                         done2 = (ptr2 == rb->block->last);
11591                 }
11592                 if (!done2) {
11593                         continue;
11594                 }
11595                 change |= in_triple(rb, expr);
11596         }
11597         return change;
11598 }
11599
11600 static int reg_in(struct compile_state *state, struct reg_block *blocks,
11601         struct reg_block *rb, struct block *suc)
11602 {
11603         struct triple_reg_set *in_set;
11604         int change;
11605         change = 0;
11606         /* Read the input set of a successor block
11607          * and place it in the current blocks output set.
11608          */
11609         in_set = blocks[suc->vertex].in;
11610         for(; in_set; in_set = in_set->next) {
11611                 int out_change, done;
11612                 struct triple *first, *last, *ptr;
11613                 out_change = out_triple(rb, in_set->member);
11614                 if (!out_change) {
11615                         continue;
11616                 }
11617                 /* If we don't define the variable also place it
11618                  * in the current blocks input set.
11619                  */
11620                 first = rb->block->first;
11621                 last = rb->block->last;
11622                 done = 0;
11623                 for(ptr = first; !done; ptr = ptr->next) {
11624                         if (ptr == in_set->member) {
11625                                 break;
11626                         }
11627                         done = (ptr == last);
11628                 }
11629                 if (!done) {
11630                         continue;
11631                 }
11632                 change |= in_triple(rb, in_set->member);
11633         }
11634         change |= phi_in(state, blocks, rb, suc);
11635         return change;
11636 }
11637
11638
11639 static int use_in(struct compile_state *state, struct reg_block *rb)
11640 {
11641         /* Find the variables we use but don't define and add
11642          * it to the current blocks input set.
11643          */
11644 #warning "FIXME is this O(N^2) algorithm bad?"
11645         struct block *block;
11646         struct triple *ptr;
11647         int done;
11648         int change;
11649         block = rb->block;
11650         change = 0;
11651         for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
11652                 struct triple **expr;
11653                 done = (ptr == block->first);
11654                 /* The variable a phi function uses depends on the
11655                  * control flow, and is handled in phi_in, not
11656                  * here.
11657                  */
11658                 if (ptr->op == OP_PHI) {
11659                         continue;
11660                 }
11661                 expr = triple_rhs(state, ptr, 0);
11662                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
11663                         struct triple *rhs, *test;
11664                         int tdone;
11665                         rhs = *expr;
11666                         if (!rhs) {
11667                                 continue;
11668                         }
11669                         /* See if rhs is defined in this block */
11670                         for(tdone = 0, test = ptr; !tdone; test = test->prev) {
11671                                 tdone = (test == block->first);
11672                                 if (test == rhs) {
11673                                         rhs = 0;
11674                                         break;
11675                                 }
11676                         }
11677                         /* If I still have a valid rhs add it to in */
11678                         change |= in_triple(rb, rhs);
11679                 }
11680         }
11681         return change;
11682 }
11683
11684 static struct reg_block *compute_variable_lifetimes(
11685         struct compile_state *state)
11686 {
11687         struct reg_block *blocks;
11688         int change;
11689         blocks = xcmalloc(
11690                 sizeof(*blocks)*(state->last_vertex + 1), "reg_block");
11691         initialize_regblock(blocks, state->last_block, 0);
11692         do {
11693                 int i;
11694                 change = 0;
11695                 for(i = 1; i <= state->last_vertex; i++) {
11696                         struct reg_block *rb;
11697                         rb = &blocks[i];
11698                         /* Add the left successor's input set to in */
11699                         if (rb->block->left) {
11700                                 change |= reg_in(state, blocks, rb, rb->block->left);
11701                         }
11702                         /* Add the right successor's input set to in */
11703                         if ((rb->block->right) && 
11704                                 (rb->block->right != rb->block->left)) {
11705                                 change |= reg_in(state, blocks, rb, rb->block->right);
11706                         }
11707                         /* Add use to in... */
11708                         change |= use_in(state, rb);
11709                 }
11710         } while(change);
11711         return blocks;
11712 }
11713
11714 static void free_variable_lifetimes(
11715         struct compile_state *state, struct reg_block *blocks)
11716 {
11717         int i;
11718         /* free in_set && out_set on each block */
11719         for(i = 1; i <= state->last_vertex; i++) {
11720                 struct triple_reg_set *entry, *next;
11721                 struct reg_block *rb;
11722                 rb = &blocks[i];
11723                 for(entry = rb->in; entry ; entry = next) {
11724                         next = entry->next;
11725                         do_triple_unset(&rb->in, entry->member);
11726                 }
11727                 for(entry = rb->out; entry; entry = next) {
11728                         next = entry->next;
11729                         do_triple_unset(&rb->out, entry->member);
11730                 }
11731         }
11732         xfree(blocks);
11733
11734 }
11735
11736 typedef void (*wvl_cb_t)(
11737         struct compile_state *state, 
11738         struct reg_block *blocks, struct triple_reg_set *live, 
11739         struct reg_block *rb, struct triple *ins, void *arg);
11740
11741 static void walk_variable_lifetimes(struct compile_state *state,
11742         struct reg_block *blocks, wvl_cb_t cb, void *arg)
11743 {
11744         int i;
11745         
11746         for(i = 1; i <= state->last_vertex; i++) {
11747                 struct triple_reg_set *live;
11748                 struct triple_reg_set *entry, *next;
11749                 struct triple *ptr, *prev;
11750                 struct reg_block *rb;
11751                 struct block *block;
11752                 int done;
11753
11754                 /* Get the blocks */
11755                 rb = &blocks[i];
11756                 block = rb->block;
11757
11758                 /* Copy out into live */
11759                 live = 0;
11760                 for(entry = rb->out; entry; entry = next) {
11761                         next = entry->next;
11762                         do_triple_set(&live, entry->member, entry->new);
11763                 }
11764                 /* Walk through the basic block calculating live */
11765                 for(done = 0, ptr = block->last; !done; ptr = prev) {
11766                         struct triple **expr;
11767
11768                         prev = ptr->prev;
11769                         done = (ptr == block->first);
11770
11771                         /* Ensure the current definition is in live */
11772                         if (triple_is_def(state, ptr)) {
11773                                 do_triple_set(&live, ptr, 0);
11774                         }
11775
11776                         /* Inform the callback function of what is
11777                          * going on.
11778                          */
11779                          cb(state, blocks, live, rb, ptr, arg);
11780                         
11781                         /* Remove the current definition from live */
11782                         do_triple_unset(&live, ptr);
11783
11784                         /* Add the current uses to live.
11785                          *
11786                          * It is safe to skip phi functions because they do
11787                          * not have any block local uses, and the block
11788                          * output sets already properly account for what
11789                          * control flow depedent uses phi functions do have.
11790                          */
11791                         if (ptr->op == OP_PHI) {
11792                                 continue;
11793                         }
11794                         expr = triple_rhs(state, ptr, 0);
11795                         for(;expr; expr = triple_rhs(state, ptr, expr)) {
11796                                 /* If the triple is not a definition skip it. */
11797                                 if (!*expr || !triple_is_def(state, *expr)) {
11798                                         continue;
11799                                 }
11800                                 do_triple_set(&live, *expr, 0);
11801                         }
11802                 }
11803                 /* Free live */
11804                 for(entry = live; entry; entry = next) {
11805                         next = entry->next;
11806                         do_triple_unset(&live, entry->member);
11807                 }
11808         }
11809 }
11810
11811 static int count_triples(struct compile_state *state)
11812 {
11813         struct triple *first, *ins;
11814         int triples = 0;
11815         first = RHS(state->main_function, 0);
11816         ins = first;
11817         do {
11818                 triples++;
11819                 ins = ins->next;
11820         } while (ins != first);
11821         return triples;
11822 }
11823
11824
11825 struct dead_triple {
11826         struct triple *triple;
11827         struct dead_triple *work_next;
11828         struct block *block;
11829         int color;
11830         int flags;
11831 #define TRIPLE_FLAG_ALIVE 1
11832 };
11833
11834
11835 static void awaken(
11836         struct compile_state *state,
11837         struct dead_triple *dtriple, struct triple **expr,
11838         struct dead_triple ***work_list_tail)
11839 {
11840         struct triple *triple;
11841         struct dead_triple *dt;
11842         if (!expr) {
11843                 return;
11844         }
11845         triple = *expr;
11846         if (!triple) {
11847                 return;
11848         }
11849         if (triple->id <= 0)  {
11850                 internal_error(state, triple, "bad triple id: %d",
11851                         triple->id);
11852         }
11853         if (triple->op == OP_NOOP) {
11854                 internal_warning(state, triple, "awakening noop?");
11855                 return;
11856         }
11857         dt = &dtriple[triple->id];
11858         if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11859                 dt->flags |= TRIPLE_FLAG_ALIVE;
11860                 if (!dt->work_next) {
11861                         **work_list_tail = dt;
11862                         *work_list_tail = &dt->work_next;
11863                 }
11864         }
11865 }
11866
11867 static void eliminate_inefectual_code(struct compile_state *state)
11868 {
11869         struct block *block;
11870         struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
11871         int triples, i;
11872         struct triple *first, *ins;
11873
11874         /* Setup the work list */
11875         work_list = 0;
11876         work_list_tail = &work_list;
11877
11878         first = RHS(state->main_function, 0);
11879
11880         /* Count how many triples I have */
11881         triples = count_triples(state);
11882
11883         /* Now put then in an array and mark all of the triples dead */
11884         dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
11885         
11886         ins = first;
11887         i = 1;
11888         block = 0;
11889         do {
11890                 if (ins->op == OP_LABEL) {
11891                         block = ins->u.block;
11892                 }
11893                 dtriple[i].triple = ins;
11894                 dtriple[i].block  = block;
11895                 dtriple[i].flags  = 0;
11896                 dtriple[i].color  = ins->id;
11897                 ins->id = i;
11898                 /* See if it is an operation we always keep */
11899 #warning "FIXME handle the case of killing a branch instruction"
11900                 if (!triple_is_pure(state, ins) || triple_is_branch(state, ins)) {
11901                         awaken(state, dtriple, &ins, &work_list_tail);
11902                 }
11903 #if 1
11904                 /* Unconditionally keep the very last instruction */
11905                 else if (ins->next == first) {
11906                         awaken(state, dtriple, &ins, &work_list_tail);
11907                 }
11908 #endif
11909                 i++;
11910                 ins = ins->next;
11911         } while(ins != first);
11912         while(work_list) {
11913                 struct dead_triple *dt;
11914                 struct block_set *user;
11915                 struct triple **expr;
11916                 dt = work_list;
11917                 work_list = dt->work_next;
11918                 if (!work_list) {
11919                         work_list_tail = &work_list;
11920                 }
11921                 /* Wake up the data depencencies of this triple */
11922                 expr = 0;
11923                 do {
11924                         expr = triple_rhs(state, dt->triple, expr);
11925                         awaken(state, dtriple, expr, &work_list_tail);
11926                 } while(expr);
11927                 do {
11928                         expr = triple_lhs(state, dt->triple, expr);
11929                         awaken(state, dtriple, expr, &work_list_tail);
11930                 } while(expr);
11931                 do {
11932                         expr = triple_misc(state, dt->triple, expr);
11933                         awaken(state, dtriple, expr, &work_list_tail);
11934                 } while(expr);
11935                 /* Wake up the forward control dependencies */
11936                 do {
11937                         expr = triple_targ(state, dt->triple, expr);
11938                         awaken(state, dtriple, expr, &work_list_tail);
11939                 } while(expr);
11940                 /* Wake up the reverse control dependencies of this triple */
11941                 for(user = dt->block->ipdomfrontier; user; user = user->next) {
11942                         awaken(state, dtriple, &user->member->last, &work_list_tail);
11943                 }
11944         }
11945         for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
11946                 if ((dt->triple->op == OP_NOOP) && 
11947                         (dt->flags & TRIPLE_FLAG_ALIVE)) {
11948                         internal_error(state, dt->triple, "noop effective?");
11949                 }
11950                 dt->triple->id = dt->color;     /* Restore the color */
11951                 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
11952 #warning "FIXME handle the case of killing a basic block"
11953                         if (dt->block->first == dt->triple) {
11954                                 continue;
11955                         }
11956                         if (dt->block->last == dt->triple) {
11957                                 dt->block->last = dt->triple->prev;
11958                         }
11959                         release_triple(state, dt->triple);
11960                 }
11961         }
11962         xfree(dtriple);
11963 }
11964
11965
11966 static void insert_mandatory_copies(struct compile_state *state)
11967 {
11968         struct triple *ins, *first;
11969
11970         /* The object is with a minimum of inserted copies,
11971          * to resolve in fundamental register conflicts between
11972          * register value producers and consumers.
11973          * Theoretically we may be greater than minimal when we
11974          * are inserting copies before instructions but that
11975          * case should be rare.
11976          */
11977         first = RHS(state->main_function, 0);
11978         ins = first;
11979         do {
11980                 struct triple_set *entry, *next;
11981                 struct triple *tmp;
11982                 struct reg_info info;
11983                 unsigned reg, regcm;
11984                 int do_post_copy, do_pre_copy;
11985                 tmp = 0;
11986                 if (!triple_is_def(state, ins)) {
11987                         goto next;
11988                 }
11989                 /* Find the architecture specific color information */
11990                 info = arch_reg_lhs(state, ins, 0);
11991                 if (info.reg >= MAX_REGISTERS) {
11992                         info.reg = REG_UNSET;
11993                 }
11994                 
11995                 reg = REG_UNSET;
11996                 regcm = arch_type_to_regcm(state, ins->type);
11997                 do_post_copy = do_pre_copy = 0;
11998
11999                 /* Walk through the uses of ins and check for conflicts */
12000                 for(entry = ins->use; entry; entry = next) {
12001                         struct reg_info rinfo;
12002                         int i;
12003                         next = entry->next;
12004                         i = find_rhs_use(state, entry->member, ins);
12005                         if (i < 0) {
12006                                 continue;
12007                         }
12008                         
12009                         /* Find the users color requirements */
12010                         rinfo = arch_reg_rhs(state, entry->member, i);
12011                         if (rinfo.reg >= MAX_REGISTERS) {
12012                                 rinfo.reg = REG_UNSET;
12013                         }
12014                         
12015                         /* See if I need a pre_copy */
12016                         if (rinfo.reg != REG_UNSET) {
12017                                 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
12018                                         do_pre_copy = 1;
12019                                 }
12020                                 reg = rinfo.reg;
12021                         }
12022                         regcm &= rinfo.regcm;
12023                         regcm = arch_regcm_normalize(state, regcm);
12024                         if (regcm == 0) {
12025                                 do_pre_copy = 1;
12026                         }
12027                         /* Always use pre_copies for constants.
12028                          * They do not take up any registers until a
12029                          * copy places them in one.
12030                          */
12031                         if ((info.reg == REG_UNNEEDED) && 
12032                                 (rinfo.reg != REG_UNNEEDED)) {
12033                                 do_pre_copy = 1;
12034                         }
12035                 }
12036                 do_post_copy =
12037                         !do_pre_copy &&
12038                         (((info.reg != REG_UNSET) && 
12039                                 (reg != REG_UNSET) &&
12040                                 (info.reg != reg)) ||
12041                         ((info.regcm & regcm) == 0));
12042
12043                 reg = info.reg;
12044                 regcm = info.regcm;
12045                 /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
12046                 for(entry = ins->use; entry; entry = next) {
12047                         struct reg_info rinfo;
12048                         int i;
12049                         next = entry->next;
12050                         i = find_rhs_use(state, entry->member, ins);
12051                         if (i < 0) {
12052                                 continue;
12053                         }
12054                         
12055                         /* Find the users color requirements */
12056                         rinfo = arch_reg_rhs(state, entry->member, i);
12057                         if (rinfo.reg >= MAX_REGISTERS) {
12058                                 rinfo.reg = REG_UNSET;
12059                         }
12060
12061                         /* Now see if it is time to do the pre_copy */
12062                         if (rinfo.reg != REG_UNSET) {
12063                                 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
12064                                         ((regcm & rinfo.regcm) == 0) ||
12065                                         /* Don't let a mandatory coalesce sneak
12066                                          * into a operation that is marked to prevent
12067                                          * coalescing.
12068                                          */
12069                                         ((reg != REG_UNNEEDED) &&
12070                                         ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
12071                                         (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
12072                                         ) {
12073                                         if (do_pre_copy) {
12074                                                 struct triple *user;
12075                                                 user = entry->member;
12076                                                 if (RHS(user, i) != ins) {
12077                                                         internal_error(state, user, "bad rhs");
12078                                                 }
12079                                                 tmp = pre_copy(state, user, i);
12080                                                 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
12081                                                 continue;
12082                                         } else {
12083                                                 do_post_copy = 1;
12084                                         }
12085                                 }
12086                                 reg = rinfo.reg;
12087                         }
12088                         if ((regcm & rinfo.regcm) == 0) {
12089                                 if (do_pre_copy) {
12090                                         struct triple *user;
12091                                         user = entry->member;
12092                                         if (RHS(user, i) != ins) {
12093                                                 internal_error(state, user, "bad rhs");
12094                                         }
12095                                         tmp = pre_copy(state, user, i);
12096                                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
12097                                         continue;
12098                                 } else {
12099                                         do_post_copy = 1;
12100                                 }
12101                         }
12102                         regcm &= rinfo.regcm;
12103                         
12104                 }
12105                 if (do_post_copy) {
12106                         struct reg_info pre, post;
12107                         tmp = post_copy(state, ins);
12108                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
12109                         pre = arch_reg_lhs(state, ins, 0);
12110                         post = arch_reg_lhs(state, tmp, 0);
12111                         if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
12112                                 internal_error(state, tmp, "useless copy");
12113                         }
12114                 }
12115         next:
12116                 ins = ins->next;
12117         } while(ins != first);
12118 }
12119
12120
12121 struct live_range_edge;
12122 struct live_range_def;
12123 struct live_range {
12124         struct live_range_edge *edges;
12125         struct live_range_def *defs;
12126 /* Note. The list pointed to by defs is kept in order.
12127  * That is baring splits in the flow control
12128  * defs dominates defs->next wich dominates defs->next->next
12129  * etc.
12130  */
12131         unsigned color;
12132         unsigned classes;
12133         unsigned degree;
12134         unsigned length;
12135         struct live_range *group_next, **group_prev;
12136 };
12137
12138 struct live_range_edge {
12139         struct live_range_edge *next;
12140         struct live_range *node;
12141 };
12142
12143 struct live_range_def {
12144         struct live_range_def *next;
12145         struct live_range_def *prev;
12146         struct live_range *lr;
12147         struct triple *def;
12148         unsigned orig_id;
12149 };
12150
12151 #define LRE_HASH_SIZE 2048
12152 struct lre_hash {
12153         struct lre_hash *next;
12154         struct live_range *left;
12155         struct live_range *right;
12156 };
12157
12158
12159 struct reg_state {
12160         struct lre_hash *hash[LRE_HASH_SIZE];
12161         struct reg_block *blocks;
12162         struct live_range_def *lrd;
12163         struct live_range *lr;
12164         struct live_range *low, **low_tail;
12165         struct live_range *high, **high_tail;
12166         unsigned defs;
12167         unsigned ranges;
12168         int passes, max_passes;
12169 #define MAX_ALLOCATION_PASSES 100
12170 };
12171
12172
12173
12174 struct print_interference_block_info {
12175         struct reg_state *rstate;
12176         FILE *fp;
12177         int need_edges;
12178 };
12179 static void print_interference_block(
12180         struct compile_state *state, struct block *block, void *arg)
12181
12182 {
12183         struct print_interference_block_info *info = arg;
12184         struct reg_state *rstate = info->rstate;
12185         FILE *fp = info->fp;
12186         struct reg_block *rb;
12187         struct triple *ptr;
12188         int phi_present;
12189         int done;
12190         rb = &rstate->blocks[block->vertex];
12191
12192         fprintf(fp, "\nblock: %p (%d), %p<-%p %p<-%p\n", 
12193                 block, 
12194                 block->vertex,
12195                 block->left, 
12196                 block->left && block->left->use?block->left->use->member : 0,
12197                 block->right, 
12198                 block->right && block->right->use?block->right->use->member : 0);
12199         if (rb->in) {
12200                 struct triple_reg_set *in_set;
12201                 fprintf(fp, "        in:");
12202                 for(in_set = rb->in; in_set; in_set = in_set->next) {
12203                         fprintf(fp, " %-10p", in_set->member);
12204                 }
12205                 fprintf(fp, "\n");
12206         }
12207         phi_present = 0;
12208         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12209                 done = (ptr == block->last);
12210                 if (ptr->op == OP_PHI) {
12211                         phi_present = 1;
12212                         break;
12213                 }
12214         }
12215         if (phi_present) {
12216                 int edge;
12217                 for(edge = 0; edge < block->users; edge++) {
12218                         fprintf(fp, "     in(%d):", edge);
12219                         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12220                                 struct triple **slot;
12221                                 done = (ptr == block->last);
12222                                 if (ptr->op != OP_PHI) {
12223                                         continue;
12224                                 }
12225                                 slot = &RHS(ptr, 0);
12226                                 fprintf(fp, " %-10p", slot[edge]);
12227                         }
12228                         fprintf(fp, "\n");
12229                 }
12230         }
12231         if (block->first->op == OP_LABEL) {
12232                 fprintf(fp, "%p:\n", block->first);
12233         }
12234         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
12235                 struct live_range *lr;
12236                 unsigned id;
12237                 int op;
12238                 op = ptr->op;
12239                 done = (ptr == block->last);
12240                 lr = rstate->lrd[ptr->id].lr;
12241                 
12242                 id = ptr->id;
12243                 ptr->id = rstate->lrd[id].orig_id;
12244                 SET_REG(ptr->id, lr->color);
12245                 display_triple(fp, ptr);
12246                 ptr->id = id;
12247
12248                 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
12249                         internal_error(state, ptr, "lr has no defs!");
12250                 }
12251                 if (info->need_edges) {
12252                         if (lr->defs) {
12253                                 struct live_range_def *lrd;
12254                                 fprintf(fp, "       range:");
12255                                 lrd = lr->defs;
12256                                 do {
12257                                         fprintf(fp, " %-10p", lrd->def);
12258                                         lrd = lrd->next;
12259                                 } while(lrd != lr->defs);
12260                                 fprintf(fp, "\n");
12261                         }
12262                         if (lr->edges > 0) {
12263                                 struct live_range_edge *edge;
12264                                 fprintf(fp, "       edges:");
12265                                 for(edge = lr->edges; edge; edge = edge->next) {
12266                                         struct live_range_def *lrd;
12267                                         lrd = edge->node->defs;
12268                                         do {
12269                                                 fprintf(fp, " %-10p", lrd->def);
12270                                                 lrd = lrd->next;
12271                                         } while(lrd != edge->node->defs);
12272                                         fprintf(fp, "|");
12273                                 }
12274                                 fprintf(fp, "\n");
12275                         }
12276                 }
12277                 /* Do a bunch of sanity checks */
12278                 valid_ins(state, ptr);
12279                 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
12280                         internal_error(state, ptr, "Invalid triple id: %d",
12281                                 ptr->id);
12282                 }
12283         }
12284         if (rb->out) {
12285                 struct triple_reg_set *out_set;
12286                 fprintf(fp, "       out:");
12287                 for(out_set = rb->out; out_set; out_set = out_set->next) {
12288                         fprintf(fp, " %-10p", out_set->member);
12289                 }
12290                 fprintf(fp, "\n");
12291         }
12292         fprintf(fp, "\n");
12293 }
12294
12295 static void print_interference_blocks(
12296         struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
12297 {
12298         struct print_interference_block_info info;
12299         info.rstate = rstate;
12300         info.fp = fp;
12301         info.need_edges = need_edges;
12302         fprintf(fp, "\nlive variables by block\n");
12303         walk_blocks(state, print_interference_block, &info);
12304
12305 }
12306
12307 static unsigned regc_max_size(struct compile_state *state, int classes)
12308 {
12309         unsigned max_size;
12310         int i;
12311         max_size = 0;
12312         for(i = 0; i < MAX_REGC; i++) {
12313                 if (classes & (1 << i)) {
12314                         unsigned size;
12315                         size = arch_regc_size(state, i);
12316                         if (size > max_size) {
12317                                 max_size = size;
12318                         }
12319                 }
12320         }
12321         return max_size;
12322 }
12323
12324 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
12325 {
12326         unsigned equivs[MAX_REG_EQUIVS];
12327         int i;
12328         if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
12329                 internal_error(state, 0, "invalid register");
12330         }
12331         if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
12332                 internal_error(state, 0, "invalid register");
12333         }
12334         arch_reg_equivs(state, equivs, reg1);
12335         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12336                 if (equivs[i] == reg2) {
12337                         return 1;
12338                 }
12339         }
12340         return 0;
12341 }
12342
12343 static void reg_fill_used(struct compile_state *state, char *used, int reg)
12344 {
12345         unsigned equivs[MAX_REG_EQUIVS];
12346         int i;
12347         if (reg == REG_UNNEEDED) {
12348                 return;
12349         }
12350         arch_reg_equivs(state, equivs, reg);
12351         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12352                 used[equivs[i]] = 1;
12353         }
12354         return;
12355 }
12356
12357 static void reg_inc_used(struct compile_state *state, char *used, int reg)
12358 {
12359         unsigned equivs[MAX_REG_EQUIVS];
12360         int i;
12361         if (reg == REG_UNNEEDED) {
12362                 return;
12363         }
12364         arch_reg_equivs(state, equivs, reg);
12365         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
12366                 used[equivs[i]] += 1;
12367         }
12368         return;
12369 }
12370
12371 static unsigned int hash_live_edge(
12372         struct live_range *left, struct live_range *right)
12373 {
12374         unsigned int hash, val;
12375         unsigned long lval, rval;
12376         lval = ((unsigned long)left)/sizeof(struct live_range);
12377         rval = ((unsigned long)right)/sizeof(struct live_range);
12378         hash = 0;
12379         while(lval) {
12380                 val = lval & 0xff;
12381                 lval >>= 8;
12382                 hash = (hash *263) + val;
12383         }
12384         while(rval) {
12385                 val = rval & 0xff;
12386                 rval >>= 8;
12387                 hash = (hash *263) + val;
12388         }
12389         hash = hash & (LRE_HASH_SIZE - 1);
12390         return hash;
12391 }
12392
12393 static struct lre_hash **lre_probe(struct reg_state *rstate,
12394         struct live_range *left, struct live_range *right)
12395 {
12396         struct lre_hash **ptr;
12397         unsigned int index;
12398         /* Ensure left <= right */
12399         if (left > right) {
12400                 struct live_range *tmp;
12401                 tmp = left;
12402                 left = right;
12403                 right = tmp;
12404         }
12405         index = hash_live_edge(left, right);
12406         
12407         ptr = &rstate->hash[index];
12408         while(*ptr) {
12409                 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
12410                         break;
12411                 }
12412                 ptr = &(*ptr)->next;
12413         }
12414         return ptr;
12415 }
12416
12417 static int interfere(struct reg_state *rstate,
12418         struct live_range *left, struct live_range *right)
12419 {
12420         struct lre_hash **ptr;
12421         ptr = lre_probe(rstate, left, right);
12422         return ptr && *ptr;
12423 }
12424
12425 static void add_live_edge(struct reg_state *rstate, 
12426         struct live_range *left, struct live_range *right)
12427 {
12428         /* FIXME the memory allocation overhead is noticeable here... */
12429         struct lre_hash **ptr, *new_hash;
12430         struct live_range_edge *edge;
12431
12432         if (left == right) {
12433                 return;
12434         }
12435         if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
12436                 return;
12437         }
12438         /* Ensure left <= right */
12439         if (left > right) {
12440                 struct live_range *tmp;
12441                 tmp = left;
12442                 left = right;
12443                 right = tmp;
12444         }
12445         ptr = lre_probe(rstate, left, right);
12446         if (*ptr) {
12447                 return;
12448         }
12449 #if 0
12450         fprintf(stderr, "new_live_edge(%p, %p)\n",
12451                 left, right);
12452 #endif
12453         new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
12454         new_hash->next  = *ptr;
12455         new_hash->left  = left;
12456         new_hash->right = right;
12457         *ptr = new_hash;
12458
12459         edge = xmalloc(sizeof(*edge), "live_range_edge");
12460         edge->next   = left->edges;
12461         edge->node   = right;
12462         left->edges  = edge;
12463         left->degree += 1;
12464         
12465         edge = xmalloc(sizeof(*edge), "live_range_edge");
12466         edge->next    = right->edges;
12467         edge->node    = left;
12468         right->edges  = edge;
12469         right->degree += 1;
12470 }
12471
12472 static void remove_live_edge(struct reg_state *rstate,
12473         struct live_range *left, struct live_range *right)
12474 {
12475         struct live_range_edge *edge, **ptr;
12476         struct lre_hash **hptr, *entry;
12477         hptr = lre_probe(rstate, left, right);
12478         if (!hptr || !*hptr) {
12479                 return;
12480         }
12481         entry = *hptr;
12482         *hptr = entry->next;
12483         xfree(entry);
12484
12485         for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
12486                 edge = *ptr;
12487                 if (edge->node == right) {
12488                         *ptr = edge->next;
12489                         memset(edge, 0, sizeof(*edge));
12490                         xfree(edge);
12491                         right->degree--;
12492                         break;
12493                 }
12494         }
12495         for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
12496                 edge = *ptr;
12497                 if (edge->node == left) {
12498                         *ptr = edge->next;
12499                         memset(edge, 0, sizeof(*edge));
12500                         xfree(edge);
12501                         left->degree--;
12502                         break;
12503                 }
12504         }
12505 }
12506
12507 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
12508 {
12509         struct live_range_edge *edge, *next;
12510         for(edge = range->edges; edge; edge = next) {
12511                 next = edge->next;
12512                 remove_live_edge(rstate, range, edge->node);
12513         }
12514 }
12515
12516 static void transfer_live_edges(struct reg_state *rstate, 
12517         struct live_range *dest, struct live_range *src)
12518 {
12519         struct live_range_edge *edge, *next;
12520         for(edge = src->edges; edge; edge = next) {
12521                 struct live_range *other;
12522                 next = edge->next;
12523                 other = edge->node;
12524                 remove_live_edge(rstate, src, other);
12525                 add_live_edge(rstate, dest, other);
12526         }
12527 }
12528
12529
12530 /* Interference graph...
12531  * 
12532  * new(n) --- Return a graph with n nodes but no edges.
12533  * add(g,x,y) --- Return a graph including g with an between x and y
12534  * interfere(g, x, y) --- Return true if there exists an edge between the nodes
12535  *                x and y in the graph g
12536  * degree(g, x) --- Return the degree of the node x in the graph g
12537  * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
12538  *
12539  * Implement with a hash table && a set of adjcency vectors.
12540  * The hash table supports constant time implementations of add and interfere.
12541  * The adjacency vectors support an efficient implementation of neighbors.
12542  */
12543
12544 /* 
12545  *     +---------------------------------------------------+
12546  *     |         +--------------+                          |
12547  *     v         v              |                          |
12548  * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select 
12549  *
12550  * -- In simplify implment optimistic coloring... (No backtracking)
12551  * -- Implement Rematerialization it is the only form of spilling we can perform
12552  *    Essentially this means dropping a constant from a register because
12553  *    we can regenerate it later.
12554  *
12555  * --- Very conservative colalescing (don't colalesce just mark the opportunities)
12556  *     coalesce at phi points...
12557  * --- Bias coloring if at all possible do the coalesing a compile time.
12558  *
12559  *
12560  */
12561
12562 static void different_colored(
12563         struct compile_state *state, struct reg_state *rstate, 
12564         struct triple *parent, struct triple *ins)
12565 {
12566         struct live_range *lr;
12567         struct triple **expr;
12568         lr = rstate->lrd[ins->id].lr;
12569         expr = triple_rhs(state, ins, 0);
12570         for(;expr; expr = triple_rhs(state, ins, expr)) {
12571                 struct live_range *lr2;
12572                 if (!*expr || (*expr == parent) || (*expr == ins)) {
12573                         continue;
12574                 }
12575                 lr2 = rstate->lrd[(*expr)->id].lr;
12576                 if (lr->color == lr2->color) {
12577                         internal_error(state, ins, "live range too big");
12578                 }
12579         }
12580 }
12581
12582
12583 static struct live_range *coalesce_ranges(
12584         struct compile_state *state, struct reg_state *rstate,
12585         struct live_range *lr1, struct live_range *lr2)
12586 {
12587         struct live_range_def *head, *mid1, *mid2, *end, *lrd;
12588         unsigned color;
12589         unsigned classes;
12590         if (lr1 == lr2) {
12591                 return lr1;
12592         }
12593         if (!lr1->defs || !lr2->defs) {
12594                 internal_error(state, 0,
12595                         "cannot coalese dead live ranges");
12596         }
12597         if ((lr1->color == REG_UNNEEDED) ||
12598                 (lr2->color == REG_UNNEEDED)) {
12599                 internal_error(state, 0, 
12600                         "cannot coalesce live ranges without a possible color");
12601         }
12602         if ((lr1->color != lr2->color) &&
12603                 (lr1->color != REG_UNSET) &&
12604                 (lr2->color != REG_UNSET)) {
12605                 internal_error(state, lr1->defs->def, 
12606                         "cannot coalesce live ranges of different colors");
12607         }
12608         color = lr1->color;
12609         if (color == REG_UNSET) {
12610                 color = lr2->color;
12611         }
12612         classes = lr1->classes & lr2->classes;
12613         if (!classes) {
12614                 internal_error(state, lr1->defs->def,
12615                         "cannot coalesce live ranges with dissimilar register classes");
12616         }
12617 #if DEBUG_COALESCING
12618         fprintf(stderr, "coalescing:");
12619         lrd = lr1->defs;
12620         do {
12621                 fprintf(stderr, " %p", lrd->def);
12622                 lrd = lrd->next;
12623         } while(lrd != lr1->defs);
12624         fprintf(stderr, " |");
12625         lrd = lr2->defs;
12626         do {
12627                 fprintf(stderr, " %p", lrd->def);
12628                 lrd = lrd->next;
12629         } while(lrd != lr2->defs);
12630         fprintf(stderr, "\n");
12631 #endif
12632         /* If there is a clear dominate live range put it in lr1,
12633          * For purposes of this test phi functions are
12634          * considered dominated by the definitions that feed into
12635          * them. 
12636          */
12637         if ((lr1->defs->prev->def->op == OP_PHI) ||
12638                 ((lr2->defs->prev->def->op != OP_PHI) &&
12639                 tdominates(state, lr2->defs->def, lr1->defs->def))) {
12640                 struct live_range *tmp;
12641                 tmp = lr1;
12642                 lr1 = lr2;
12643                 lr2 = tmp;
12644         }
12645 #if 0
12646         if (lr1->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
12647                 fprintf(stderr, "lr1 post\n");
12648         }
12649         if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12650                 fprintf(stderr, "lr1 pre\n");
12651         }
12652         if (lr2->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
12653                 fprintf(stderr, "lr2 post\n");
12654         }
12655         if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
12656                 fprintf(stderr, "lr2 pre\n");
12657         }
12658 #endif
12659 #if 0
12660         fprintf(stderr, "coalesce color1(%p): %3d color2(%p) %3d\n",
12661                 lr1->defs->def,
12662                 lr1->color,
12663                 lr2->defs->def,
12664                 lr2->color);
12665 #endif
12666         
12667         /* Append lr2 onto lr1 */
12668 #warning "FIXME should this be a merge instead of a splice?"
12669         /* This FIXME item applies to the correctness of live_range_end 
12670          * and to the necessity of making multiple passes of coalesce_live_ranges.
12671          * A failure to find some coalesce opportunities in coaleace_live_ranges
12672          * does not impact the correct of the compiler just the efficiency with
12673          * which registers are allocated.
12674          */
12675         head = lr1->defs;
12676         mid1 = lr1->defs->prev;
12677         mid2 = lr2->defs;
12678         end  = lr2->defs->prev;
12679         
12680         head->prev = end;
12681         end->next  = head;
12682
12683         mid1->next = mid2;
12684         mid2->prev = mid1;
12685
12686         /* Fixup the live range in the added live range defs */
12687         lrd = head;
12688         do {
12689                 lrd->lr = lr1;
12690                 lrd = lrd->next;
12691         } while(lrd != head);
12692
12693         /* Mark lr2 as free. */
12694         lr2->defs = 0;
12695         lr2->color = REG_UNNEEDED;
12696         lr2->classes = 0;
12697
12698         if (!lr1->defs) {
12699                 internal_error(state, 0, "lr1->defs == 0 ?");
12700         }
12701
12702         lr1->color   = color;
12703         lr1->classes = classes;
12704
12705         /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
12706         transfer_live_edges(rstate, lr1, lr2);
12707
12708         return lr1;
12709 }
12710
12711 static struct live_range_def *live_range_head(
12712         struct compile_state *state, struct live_range *lr,
12713         struct live_range_def *last)
12714 {
12715         struct live_range_def *result;
12716         result = 0;
12717         if (last == 0) {
12718                 result = lr->defs;
12719         }
12720         else if (!tdominates(state, lr->defs->def, last->next->def)) {
12721                 result = last->next;
12722         }
12723         return result;
12724 }
12725
12726 static struct live_range_def *live_range_end(
12727         struct compile_state *state, struct live_range *lr,
12728         struct live_range_def *last)
12729 {
12730         struct live_range_def *result;
12731         result = 0;
12732         if (last == 0) {
12733                 result = lr->defs->prev;
12734         }
12735         else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
12736                 result = last->prev;
12737         }
12738         return result;
12739 }
12740
12741
12742 static void initialize_live_ranges(
12743         struct compile_state *state, struct reg_state *rstate)
12744 {
12745         struct triple *ins, *first;
12746         size_t count, size;
12747         int i, j;
12748
12749         first = RHS(state->main_function, 0);
12750         /* First count how many instructions I have.
12751          */
12752         count = count_triples(state);
12753         /* Potentially I need one live range definitions for each
12754          * instruction.
12755          */
12756         rstate->defs = count;
12757         /* Potentially I need one live range for each instruction
12758          * plus an extra for the dummy live range.
12759          */
12760         rstate->ranges = count + 1;
12761         size = sizeof(rstate->lrd[0]) * rstate->defs;
12762         rstate->lrd = xcmalloc(size, "live_range_def");
12763         size = sizeof(rstate->lr[0]) * rstate->ranges;
12764         rstate->lr  = xcmalloc(size, "live_range");
12765
12766         /* Setup the dummy live range */
12767         rstate->lr[0].classes = 0;
12768         rstate->lr[0].color = REG_UNSET;
12769         rstate->lr[0].defs = 0;
12770         i = j = 0;
12771         ins = first;
12772         do {
12773                 /* If the triple is a variable give it a live range */
12774                 if (triple_is_def(state, ins)) {
12775                         struct reg_info info;
12776                         /* Find the architecture specific color information */
12777                         info = find_def_color(state, ins);
12778                         i++;
12779                         rstate->lr[i].defs    = &rstate->lrd[j];
12780                         rstate->lr[i].color   = info.reg;
12781                         rstate->lr[i].classes = info.regcm;
12782                         rstate->lr[i].degree  = 0;
12783                         rstate->lrd[j].lr = &rstate->lr[i];
12784                 } 
12785                 /* Otherwise give the triple the dummy live range. */
12786                 else {
12787                         rstate->lrd[j].lr = &rstate->lr[0];
12788                 }
12789
12790                 /* Initalize the live_range_def */
12791                 rstate->lrd[j].next    = &rstate->lrd[j];
12792                 rstate->lrd[j].prev    = &rstate->lrd[j];
12793                 rstate->lrd[j].def     = ins;
12794                 rstate->lrd[j].orig_id = ins->id;
12795                 ins->id = j;
12796
12797                 j++;
12798                 ins = ins->next;
12799         } while(ins != first);
12800         rstate->ranges = i;
12801
12802         /* Make a second pass to handle achitecture specific register
12803          * constraints.
12804          */
12805         ins = first;
12806         do {
12807                 int zlhs, zrhs, i, j;
12808                 if (ins->id > rstate->defs) {
12809                         internal_error(state, ins, "bad id");
12810                 }
12811                 
12812                 /* Walk through the template of ins and coalesce live ranges */
12813                 zlhs = TRIPLE_LHS(ins->sizes);
12814                 if ((zlhs == 0) && triple_is_def(state, ins)) {
12815                         zlhs = 1;
12816                 }
12817                 zrhs = TRIPLE_RHS(ins->sizes);
12818
12819 #if DEBUG_COALESCING > 1
12820                 fprintf(stderr, "mandatory coalesce: %p %d %d\n",
12821                         ins, zlhs, zrhs);
12822 #endif          
12823                 for(i = 0; i < zlhs; i++) {
12824                         struct reg_info linfo;
12825                         struct live_range_def *lhs;
12826                         linfo = arch_reg_lhs(state, ins, i);
12827                         if (linfo.reg < MAX_REGISTERS) {
12828                                 continue;
12829                         }
12830                         if (triple_is_def(state, ins)) {
12831                                 lhs = &rstate->lrd[ins->id];
12832                         } else {
12833                                 lhs = &rstate->lrd[LHS(ins, i)->id];
12834                         }
12835 #if DEBUG_COALESCING > 1
12836                         fprintf(stderr, "coalesce lhs(%d): %p %d\n",
12837                                 i, lhs, linfo.reg);
12838                 
12839 #endif          
12840                         for(j = 0; j < zrhs; j++) {
12841                                 struct reg_info rinfo;
12842                                 struct live_range_def *rhs;
12843                                 rinfo = arch_reg_rhs(state, ins, j);
12844                                 if (rinfo.reg < MAX_REGISTERS) {
12845                                         continue;
12846                                 }
12847                                 rhs = &rstate->lrd[RHS(ins, j)->id];
12848 #if DEBUG_COALESCING > 1
12849                                 fprintf(stderr, "coalesce rhs(%d): %p %d\n",
12850                                         j, rhs, rinfo.reg);
12851                 
12852 #endif          
12853                                 if (rinfo.reg == linfo.reg) {
12854                                         coalesce_ranges(state, rstate, 
12855                                                 lhs->lr, rhs->lr);
12856                                 }
12857                         }
12858                 }
12859                 ins = ins->next;
12860         } while(ins != first);
12861 }
12862
12863 static void graph_ins(
12864         struct compile_state *state, 
12865         struct reg_block *blocks, struct triple_reg_set *live, 
12866         struct reg_block *rb, struct triple *ins, void *arg)
12867 {
12868         struct reg_state *rstate = arg;
12869         struct live_range *def;
12870         struct triple_reg_set *entry;
12871
12872         /* If the triple is not a definition
12873          * we do not have a definition to add to
12874          * the interference graph.
12875          */
12876         if (!triple_is_def(state, ins)) {
12877                 return;
12878         }
12879         def = rstate->lrd[ins->id].lr;
12880         
12881         /* Create an edge between ins and everything that is
12882          * alive, unless the live_range cannot share
12883          * a physical register with ins.
12884          */
12885         for(entry = live; entry; entry = entry->next) {
12886                 struct live_range *lr;
12887                 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
12888                         internal_error(state, 0, "bad entry?");
12889                 }
12890                 lr = rstate->lrd[entry->member->id].lr;
12891                 if (def == lr) {
12892                         continue;
12893                 }
12894                 if (!arch_regcm_intersect(def->classes, lr->classes)) {
12895                         continue;
12896                 }
12897                 add_live_edge(rstate, def, lr);
12898         }
12899         return;
12900 }
12901
12902 static struct live_range *get_verify_live_range(
12903         struct compile_state *state, struct reg_state *rstate, struct triple *ins)
12904 {
12905         struct live_range *lr;
12906         struct live_range_def *lrd;
12907         int ins_found;
12908         if ((ins->id < 0) || (ins->id > rstate->defs)) {
12909                 internal_error(state, ins, "bad ins?");
12910         }
12911         lr = rstate->lrd[ins->id].lr;
12912         ins_found = 0;
12913         lrd = lr->defs;
12914         do {
12915                 if (lrd->def == ins) {
12916                         ins_found = 1;
12917                 }
12918                 lrd = lrd->next;
12919         } while(lrd != lr->defs);
12920         if (!ins_found) {
12921                 internal_error(state, ins, "ins not in live range");
12922         }
12923         return lr;
12924 }
12925
12926 static void verify_graph_ins(
12927         struct compile_state *state, 
12928         struct reg_block *blocks, struct triple_reg_set *live, 
12929         struct reg_block *rb, struct triple *ins, void *arg)
12930 {
12931         struct reg_state *rstate = arg;
12932         struct triple_reg_set *entry1, *entry2;
12933
12934
12935         /* Compare live against edges and make certain the code is working */
12936         for(entry1 = live; entry1; entry1 = entry1->next) {
12937                 struct live_range *lr1;
12938                 lr1 = get_verify_live_range(state, rstate, entry1->member);
12939                 for(entry2 = live; entry2; entry2 = entry2->next) {
12940                         struct live_range *lr2;
12941                         struct live_range_edge *edge2;
12942                         int lr1_found;
12943                         int lr2_degree;
12944                         if (entry2 == entry1) {
12945                                 continue;
12946                         }
12947                         lr2 = get_verify_live_range(state, rstate, entry2->member);
12948                         if (lr1 == lr2) {
12949                                 internal_error(state, entry2->member, 
12950                                         "live range with 2 values simultaneously alive");
12951                         }
12952                         if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
12953                                 continue;
12954                         }
12955                         if (!interfere(rstate, lr1, lr2)) {
12956                                 internal_error(state, entry2->member, 
12957                                         "edges don't interfere?");
12958                         }
12959                                 
12960                         lr1_found = 0;
12961                         lr2_degree = 0;
12962                         for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
12963                                 lr2_degree++;
12964                                 if (edge2->node == lr1) {
12965                                         lr1_found = 1;
12966                                 }
12967                         }
12968                         if (lr2_degree != lr2->degree) {
12969                                 internal_error(state, entry2->member,
12970                                         "computed degree: %d does not match reported degree: %d\n",
12971                                         lr2_degree, lr2->degree);
12972                         }
12973                         if (!lr1_found) {
12974                                 internal_error(state, entry2->member, "missing edge");
12975                         }
12976                 }
12977         }
12978         return;
12979 }
12980
12981 #if DEBUG_CONSISTENCY > 1
12982 static void verify_interference_graph(
12983         struct compile_state *state, struct reg_state *rstate)
12984 {
12985 #if 0
12986         fprintf(stderr, "verify_interference_graph...\n");
12987 #endif
12988
12989         walk_variable_lifetimes(state, rstate->blocks, verify_graph_ins, rstate);
12990 #if 0
12991         fprintf(stderr, "verify_interference_graph done\n");
12992 #endif
12993 }
12994 #else
12995 static inline void verify_interference_graph(
12996         struct compile_state *state, struct reg_state *rstate) {}
12997 #endif
12998
12999 static void print_interference_ins(
13000         struct compile_state *state, 
13001         struct reg_block *blocks, struct triple_reg_set *live, 
13002         struct reg_block *rb, struct triple *ins, void *arg)
13003 {
13004         struct reg_state *rstate = arg;
13005         struct live_range *lr;
13006         unsigned id;
13007
13008         lr = rstate->lrd[ins->id].lr;
13009         id = ins->id;
13010         ins->id = rstate->lrd[id].orig_id;
13011         SET_REG(ins->id, lr->color);
13012         display_triple(stdout, ins);
13013         ins->id = id;
13014
13015         if (lr->defs) {
13016                 struct live_range_def *lrd;
13017                 printf("       range:");
13018                 lrd = lr->defs;
13019                 do {
13020                         printf(" %-10p", lrd->def);
13021                         lrd = lrd->next;
13022                 } while(lrd != lr->defs);
13023                 printf("\n");
13024         }
13025         if (live) {
13026                 struct triple_reg_set *entry;
13027                 printf("        live:");
13028                 for(entry = live; entry; entry = entry->next) {
13029                         printf(" %-10p", entry->member);
13030                 }
13031                 printf("\n");
13032         }
13033         if (lr->edges) {
13034                 struct live_range_edge *entry;
13035                 printf("       edges:");
13036                 for(entry = lr->edges; entry; entry = entry->next) {
13037                         struct live_range_def *lrd;
13038                         lrd = entry->node->defs;
13039                         do {
13040                                 printf(" %-10p", lrd->def);
13041                                 lrd = lrd->next;
13042                         } while(lrd != entry->node->defs);
13043                         printf("|");
13044                 }
13045                 printf("\n");
13046         }
13047         if (triple_is_branch(state, ins)) {
13048                 printf("\n");
13049         }
13050         return;
13051 }
13052
13053 static int coalesce_live_ranges(
13054         struct compile_state *state, struct reg_state *rstate)
13055 {
13056         /* At the point where a value is moved from one
13057          * register to another that value requires two
13058          * registers, thus increasing register pressure.
13059          * Live range coaleescing reduces the register
13060          * pressure by keeping a value in one register
13061          * longer.
13062          *
13063          * In the case of a phi function all paths leading
13064          * into it must be allocated to the same register
13065          * otherwise the phi function may not be removed.
13066          *
13067          * Forcing a value to stay in a single register
13068          * for an extended period of time does have
13069          * limitations when applied to non homogenous
13070          * register pool.  
13071          *
13072          * The two cases I have identified are:
13073          * 1) Two forced register assignments may
13074          *    collide.
13075          * 2) Registers may go unused because they
13076          *    are only good for storing the value
13077          *    and not manipulating it.
13078          *
13079          * Because of this I need to split live ranges,
13080          * even outside of the context of coalesced live
13081          * ranges.  The need to split live ranges does
13082          * impose some constraints on live range coalescing.
13083          *
13084          * - Live ranges may not be coalesced across phi
13085          *   functions.  This creates a 2 headed live
13086          *   range that cannot be sanely split.
13087          *
13088          * - phi functions (coalesced in initialize_live_ranges) 
13089          *   are handled as pre split live ranges so we will
13090          *   never attempt to split them.
13091          */
13092         int coalesced;
13093         int i;
13094
13095         coalesced = 0;
13096         for(i = 0; i <= rstate->ranges; i++) {
13097                 struct live_range *lr1;
13098                 struct live_range_def *lrd1;
13099                 lr1 = &rstate->lr[i];
13100                 if (!lr1->defs) {
13101                         continue;
13102                 }
13103                 lrd1 = live_range_end(state, lr1, 0);
13104                 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
13105                         struct triple_set *set;
13106                         if (lrd1->def->op != OP_COPY) {
13107                                 continue;
13108                         }
13109                         /* Skip copies that are the result of a live range split. */
13110                         if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
13111                                 continue;
13112                         }
13113                         for(set = lrd1->def->use; set; set = set->next) {
13114                                 struct live_range_def *lrd2;
13115                                 struct live_range *lr2, *res;
13116
13117                                 lrd2 = &rstate->lrd[set->member->id];
13118
13119                                 /* Don't coalesce with instructions
13120                                  * that are the result of a live range
13121                                  * split.
13122                                  */
13123                                 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
13124                                         continue;
13125                                 }
13126                                 lr2 = rstate->lrd[set->member->id].lr;
13127                                 if (lr1 == lr2) {
13128                                         continue;
13129                                 }
13130                                 if ((lr1->color != lr2->color) &&
13131                                         (lr1->color != REG_UNSET) &&
13132                                         (lr2->color != REG_UNSET)) {
13133                                         continue;
13134                                 }
13135                                 if ((lr1->classes & lr2->classes) == 0) {
13136                                         continue;
13137                                 }
13138                                 
13139                                 if (interfere(rstate, lr1, lr2)) {
13140                                         continue;
13141                                 }
13142
13143                                 res = coalesce_ranges(state, rstate, lr1, lr2);
13144                                 coalesced += 1;
13145                                 if (res != lr1) {
13146                                         goto next;
13147                                 }
13148                         }
13149                 }
13150         next:
13151                 ;
13152         }
13153         return coalesced;
13154 }
13155
13156
13157 static void fix_coalesce_conflicts(struct compile_state *state,
13158         struct reg_block *blocks, struct triple_reg_set *live,
13159         struct reg_block *rb, struct triple *ins, void *arg)
13160 {
13161         int *conflicts = arg;
13162         int zlhs, zrhs, i, j;
13163
13164         /* See if we have a mandatory coalesce operation between
13165          * a lhs and a rhs value.  If so and the rhs value is also
13166          * alive then this triple needs to be pre copied.  Otherwise
13167          * we would have two definitions in the same live range simultaneously
13168          * alive.
13169          */
13170         zlhs = TRIPLE_LHS(ins->sizes);
13171         if ((zlhs == 0) && triple_is_def(state, ins)) {
13172                 zlhs = 1;
13173         }
13174         zrhs = TRIPLE_RHS(ins->sizes);
13175         for(i = 0; i < zlhs; i++) {
13176                 struct reg_info linfo;
13177                 linfo = arch_reg_lhs(state, ins, i);
13178                 if (linfo.reg < MAX_REGISTERS) {
13179                         continue;
13180                 }
13181                 for(j = 0; j < zrhs; j++) {
13182                         struct reg_info rinfo;
13183                         struct triple *rhs;
13184                         struct triple_reg_set *set;
13185                         int found;
13186                         found = 0;
13187                         rinfo = arch_reg_rhs(state, ins, j);
13188                         if (rinfo.reg != linfo.reg) {
13189                                 continue;
13190                         }
13191                         rhs = RHS(ins, j);
13192                         for(set = live; set && !found; set = set->next) {
13193                                 if (set->member == rhs) {
13194                                         found = 1;
13195                                 }
13196                         }
13197                         if (found) {
13198                                 struct triple *copy;
13199                                 copy = pre_copy(state, ins, j);
13200                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
13201                                 (*conflicts)++;
13202                         }
13203                 }
13204         }
13205         return;
13206 }
13207
13208 static int correct_coalesce_conflicts(
13209         struct compile_state *state, struct reg_block *blocks)
13210 {
13211         int conflicts;
13212         conflicts = 0;
13213         walk_variable_lifetimes(state, blocks, fix_coalesce_conflicts, &conflicts);
13214         return conflicts;
13215 }
13216
13217 static void replace_set_use(struct compile_state *state,
13218         struct triple_reg_set *head, struct triple *orig, struct triple *new)
13219 {
13220         struct triple_reg_set *set;
13221         for(set = head; set; set = set->next) {
13222                 if (set->member == orig) {
13223                         set->member = new;
13224                 }
13225         }
13226 }
13227
13228 static void replace_block_use(struct compile_state *state, 
13229         struct reg_block *blocks, struct triple *orig, struct triple *new)
13230 {
13231         int i;
13232 #warning "WISHLIST visit just those blocks that need it *"
13233         for(i = 1; i <= state->last_vertex; i++) {
13234                 struct reg_block *rb;
13235                 rb = &blocks[i];
13236                 replace_set_use(state, rb->in, orig, new);
13237                 replace_set_use(state, rb->out, orig, new);
13238         }
13239 }
13240
13241 static void color_instructions(struct compile_state *state)
13242 {
13243         struct triple *ins, *first;
13244         first = RHS(state->main_function, 0);
13245         ins = first;
13246         do {
13247                 if (triple_is_def(state, ins)) {
13248                         struct reg_info info;
13249                         info = find_lhs_color(state, ins, 0);
13250                         if (info.reg >= MAX_REGISTERS) {
13251                                 info.reg = REG_UNSET;
13252                         }
13253                         SET_INFO(ins->id, info);
13254                 }
13255                 ins = ins->next;
13256         } while(ins != first);
13257 }
13258
13259 static struct reg_info read_lhs_color(
13260         struct compile_state *state, struct triple *ins, int index)
13261 {
13262         struct reg_info info;
13263         if ((index == 0) && triple_is_def(state, ins)) {
13264                 info.reg   = ID_REG(ins->id);
13265                 info.regcm = ID_REGCM(ins->id);
13266         }
13267         else if (index < TRIPLE_LHS(ins->sizes)) {
13268                 info = read_lhs_color(state, LHS(ins, index), 0);
13269         }
13270         else {
13271                 internal_error(state, ins, "Bad lhs %d", index);
13272                 info.reg = REG_UNSET;
13273                 info.regcm = 0;
13274         }
13275         return info;
13276 }
13277
13278 static struct triple *resolve_tangle(
13279         struct compile_state *state, struct triple *tangle)
13280 {
13281         struct reg_info info, uinfo;
13282         struct triple_set *set, *next;
13283         struct triple *copy;
13284
13285 #warning "WISHLIST recalculate all affected instructions colors"
13286         info = find_lhs_color(state, tangle, 0);
13287         for(set = tangle->use; set; set = next) {
13288                 struct triple *user;
13289                 int i, zrhs;
13290                 next = set->next;
13291                 user = set->member;
13292                 zrhs = TRIPLE_RHS(user->sizes);
13293                 for(i = 0; i < zrhs; i++) {
13294                         if (RHS(user, i) != tangle) {
13295                                 continue;
13296                         }
13297                         uinfo = find_rhs_post_color(state, user, i);
13298                         if (uinfo.reg == info.reg) {
13299                                 copy = pre_copy(state, user, i);
13300                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
13301                                 SET_INFO(copy->id, uinfo);
13302                         }
13303                 }
13304         }
13305         copy = 0;
13306         uinfo = find_lhs_pre_color(state, tangle, 0);
13307         if (uinfo.reg == info.reg) {
13308                 struct reg_info linfo;
13309                 copy = post_copy(state, tangle);
13310                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
13311                 linfo = find_lhs_color(state, copy, 0);
13312                 SET_INFO(copy->id, linfo);
13313         }
13314         info = find_lhs_color(state, tangle, 0);
13315         SET_INFO(tangle->id, info);
13316         
13317         return copy;
13318 }
13319
13320
13321 static void fix_tangles(struct compile_state *state,
13322         struct reg_block *blocks, struct triple_reg_set *live,
13323         struct reg_block *rb, struct triple *ins, void *arg)
13324 {
13325         int *tangles = arg;
13326         struct triple *tangle;
13327         do {
13328                 char used[MAX_REGISTERS];
13329                 struct triple_reg_set *set;
13330                 tangle = 0;
13331
13332                 /* Find out which registers have multiple uses at this point */
13333                 memset(used, 0, sizeof(used));
13334                 for(set = live; set; set = set->next) {
13335                         struct reg_info info;
13336                         info = read_lhs_color(state, set->member, 0);
13337                         if (info.reg == REG_UNSET) {
13338                                 continue;
13339                         }
13340                         reg_inc_used(state, used, info.reg);
13341                 }
13342                 
13343                 /* Now find the least dominated definition of a register in
13344                  * conflict I have seen so far.
13345                  */
13346                 for(set = live; set; set = set->next) {
13347                         struct reg_info info;
13348                         info = read_lhs_color(state, set->member, 0);
13349                         if (used[info.reg] < 2) {
13350                                 continue;
13351                         }
13352                         /* Changing copies that feed into phi functions
13353                          * is incorrect.
13354                          */
13355                         if (set->member->use && 
13356                                 (set->member->use->member->op == OP_PHI)) {
13357                                 continue;
13358                         }
13359                         if (!tangle || tdominates(state, set->member, tangle)) {
13360                                 tangle = set->member;
13361                         }
13362                 }
13363                 /* If I have found a tangle resolve it */
13364                 if (tangle) {
13365                         struct triple *post_copy;
13366                         (*tangles)++;
13367                         post_copy = resolve_tangle(state, tangle);
13368                         if (post_copy) {
13369                                 replace_block_use(state, blocks, tangle, post_copy);
13370                         }
13371                         if (post_copy && (tangle != ins)) {
13372                                 replace_set_use(state, live, tangle, post_copy);
13373                         }
13374                 }
13375         } while(tangle);
13376         return;
13377 }
13378
13379 static int correct_tangles(
13380         struct compile_state *state, struct reg_block *blocks)
13381 {
13382         int tangles;
13383         tangles = 0;
13384         color_instructions(state);
13385         walk_variable_lifetimes(state, blocks, fix_tangles, &tangles);
13386         return tangles;
13387 }
13388
13389
13390 static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
13391 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
13392
13393 struct triple *find_constrained_def(
13394         struct compile_state *state, struct live_range *range, struct triple *constrained)
13395 {
13396         struct live_range_def *lrd;
13397         lrd = range->defs;
13398         do {
13399                 struct reg_info info;
13400                 unsigned regcm;
13401                 int is_constrained;
13402                 regcm = arch_type_to_regcm(state, lrd->def->type);
13403                 info = find_lhs_color(state, lrd->def, 0);
13404                 regcm      = arch_regcm_reg_normalize(state, regcm);
13405                 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
13406                 /* If the 2 register class masks are not equal the
13407                  * the current register class is constrained.
13408                  */
13409                 is_constrained = regcm != info.regcm;
13410                 
13411                 /* Of the constrained live ranges deal with the
13412                  * least dominated one first.
13413                  */
13414                 if (is_constrained) {
13415 #if DEBUG_RANGE_CONFLICTS
13416                         fprintf(stderr, "canidate: %p %-8s regcm: %x %x\n",
13417                                 lrd->def, tops(lrd->def->op), regcm, info.regcm);
13418 #endif
13419                         if (!constrained || 
13420                                 tdominates(state, lrd->def, constrained))
13421                         {
13422                                 constrained = lrd->def;
13423                         }
13424                 }
13425                 lrd = lrd->next;
13426         } while(lrd != range->defs);
13427         return constrained;
13428 }
13429
13430 static int split_constrained_ranges(
13431         struct compile_state *state, struct reg_state *rstate, 
13432         struct live_range *range)
13433 {
13434         /* Walk through the edges in conflict and our current live
13435          * range, and find definitions that are more severly constrained
13436          * than they type of data they contain require.
13437          * 
13438          * Then pick one of those ranges and relax the constraints.
13439          */
13440         struct live_range_edge *edge;
13441         struct triple *constrained;
13442
13443         constrained = 0;
13444         for(edge = range->edges; edge; edge = edge->next) {
13445                 constrained = find_constrained_def(state, edge->node, constrained);
13446         }
13447         if (!constrained) {
13448                 constrained = find_constrained_def(state, range, constrained);
13449         }
13450 #if DEBUG_RANGE_CONFLICTS
13451         fprintf(stderr, "constrained: %p %-8s\n",
13452                 constrained, tops(constrained->op));
13453 #endif
13454         if (constrained) {
13455                 ids_from_rstate(state, rstate);
13456                 cleanup_rstate(state, rstate);
13457                 resolve_tangle(state, constrained);
13458         }
13459         return !!constrained;
13460 }
13461         
13462 static int split_ranges(
13463         struct compile_state *state, struct reg_state *rstate,
13464         char *used, struct live_range *range)
13465 {
13466         int split;
13467 #if DEBUG_RANGE_CONFLICTS
13468         fprintf(stderr, "split_ranges %d %s %p\n", 
13469                 rstate->passes, tops(range->defs->def->op), range->defs->def);
13470 #endif
13471         if ((range->color == REG_UNNEEDED) ||
13472                 (rstate->passes >= rstate->max_passes)) {
13473                 return 0;
13474         }
13475         split = split_constrained_ranges(state, rstate, range);
13476
13477         /* Ideally I would split the live range that will not be used
13478          * for the longest period of time in hopes that this will 
13479          * (a) allow me to spill a register or
13480          * (b) allow me to place a value in another register.
13481          *
13482          * So far I don't have a test case for this, the resolving
13483          * of mandatory constraints has solved all of my
13484          * know issues.  So I have choosen not to write any
13485          * code until I cat get a better feel for cases where
13486          * it would be useful to have.
13487          *
13488          */
13489 #warning "WISHLIST implement live range splitting..."
13490         if ((DEBUG_RANGE_CONFLICTS > 1) && 
13491                 (!split || (DEBUG_RANGE_CONFLICTS > 2))) {
13492                 print_interference_blocks(state, rstate, stderr, 0);
13493                 print_dominators(state, stderr);
13494         }
13495         return split;
13496 }
13497
13498 #if DEBUG_COLOR_GRAPH > 1
13499 #define cgdebug_printf(...) fprintf(stdout, __VA_ARGS__)
13500 #define cgdebug_flush() fflush(stdout)
13501 #define cgdebug_loc(STATE, TRIPLE) loc(stdout, STATE, TRIPLE)
13502 #elif DEBUG_COLOR_GRAPH == 1
13503 #define cgdebug_printf(...) fprintf(stderr, __VA_ARGS__)
13504 #define cgdebug_flush() fflush(stderr)
13505 #define cgdebug_loc(STATE, TRIPLE) loc(stderr, STATE, TRIPLE)
13506 #else
13507 #define cgdebug_printf(...)
13508 #define cgdebug_flush()
13509 #define cgdebug_loc(STATE, TRIPLE)
13510 #endif
13511
13512         
13513 static int select_free_color(struct compile_state *state, 
13514         struct reg_state *rstate, struct live_range *range)
13515 {
13516         struct triple_set *entry;
13517         struct live_range_def *lrd;
13518         struct live_range_def *phi;
13519         struct live_range_edge *edge;
13520         char used[MAX_REGISTERS];
13521         struct triple **expr;
13522
13523         /* Instead of doing just the trivial color select here I try
13524          * a few extra things because a good color selection will help reduce
13525          * copies.
13526          */
13527
13528         /* Find the registers currently in use */
13529         memset(used, 0, sizeof(used));
13530         for(edge = range->edges; edge; edge = edge->next) {
13531                 if (edge->node->color == REG_UNSET) {
13532                         continue;
13533                 }
13534                 reg_fill_used(state, used, edge->node->color);
13535         }
13536 #if DEBUG_COLOR_GRAPH > 1
13537         {
13538                 int i;
13539                 i = 0;
13540                 for(edge = range->edges; edge; edge = edge->next) {
13541                         i++;
13542                 }
13543                 cgdebug_printf("\n%s edges: %d @%s:%d.%d\n", 
13544                         tops(range->def->op), i, 
13545                         range->def->filename, range->def->line, range->def->col);
13546                 for(i = 0; i < MAX_REGISTERS; i++) {
13547                         if (used[i]) {
13548                                 cgdebug_printf("used: %s\n",
13549                                         arch_reg_str(i));
13550                         }
13551                 }
13552         }       
13553 #endif
13554
13555         /* If a color is already assigned see if it will work */
13556         if (range->color != REG_UNSET) {
13557                 struct live_range_def *lrd;
13558                 if (!used[range->color]) {
13559                         return 1;
13560                 }
13561                 for(edge = range->edges; edge; edge = edge->next) {
13562                         if (edge->node->color != range->color) {
13563                                 continue;
13564                         }
13565                         warning(state, edge->node->defs->def, "edge: ");
13566                         lrd = edge->node->defs;
13567                         do {
13568                                 warning(state, lrd->def, " %p %s",
13569                                         lrd->def, tops(lrd->def->op));
13570                                 lrd = lrd->next;
13571                         } while(lrd != edge->node->defs);
13572                 }
13573                 lrd = range->defs;
13574                 warning(state, range->defs->def, "def: ");
13575                 do {
13576                         warning(state, lrd->def, " %p %s",
13577                                 lrd->def, tops(lrd->def->op));
13578                         lrd = lrd->next;
13579                 } while(lrd != range->defs);
13580                 internal_error(state, range->defs->def,
13581                         "live range with already used color %s",
13582                         arch_reg_str(range->color));
13583         }
13584
13585         /* If I feed into an expression reuse it's color.
13586          * This should help remove copies in the case of 2 register instructions
13587          * and phi functions.
13588          */
13589         phi = 0;
13590         lrd = live_range_end(state, range, 0);
13591         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
13592                 entry = lrd->def->use;
13593                 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
13594                         struct live_range_def *insd;
13595                         unsigned regcm;
13596                         insd = &rstate->lrd[entry->member->id];
13597                         if (insd->lr->defs == 0) {
13598                                 continue;
13599                         }
13600                         if (!phi && (insd->def->op == OP_PHI) &&
13601                                 !interfere(rstate, range, insd->lr)) {
13602                                 phi = insd;
13603                         }
13604                         if (insd->lr->color == REG_UNSET) {
13605                                 continue;
13606                         }
13607                         regcm = insd->lr->classes;
13608                         if (((regcm & range->classes) == 0) ||
13609                                 (used[insd->lr->color])) {
13610                                 continue;
13611                         }
13612                         if (interfere(rstate, range, insd->lr)) {
13613                                 continue;
13614                         }
13615                         range->color = insd->lr->color;
13616                 }
13617         }
13618         /* If I feed into a phi function reuse it's color or the color
13619          * of something else that feeds into the phi function.
13620          */
13621         if (phi) {
13622                 if (phi->lr->color != REG_UNSET) {
13623                         if (used[phi->lr->color]) {
13624                                 range->color = phi->lr->color;
13625                         }
13626                 }
13627                 else {
13628                         expr = triple_rhs(state, phi->def, 0);
13629                         for(; expr; expr = triple_rhs(state, phi->def, expr)) {
13630                                 struct live_range *lr;
13631                                 unsigned regcm;
13632                                 if (!*expr) {
13633                                         continue;
13634                                 }
13635                                 lr = rstate->lrd[(*expr)->id].lr;
13636                                 if (lr->color == REG_UNSET) {
13637                                         continue;
13638                                 }
13639                                 regcm = lr->classes;
13640                                 if (((regcm & range->classes) == 0) ||
13641                                         (used[lr->color])) {
13642                                         continue;
13643                                 }
13644                                 if (interfere(rstate, range, lr)) {
13645                                         continue;
13646                                 }
13647                                 range->color = lr->color;
13648                         }
13649                 }
13650         }
13651         /* If I don't interfere with a rhs node reuse it's color */
13652         lrd = live_range_head(state, range, 0);
13653         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
13654                 expr = triple_rhs(state, lrd->def, 0);
13655                 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
13656                         struct live_range *lr;
13657                         unsigned regcm;
13658                         if (!*expr) {
13659                                 continue;
13660                         }
13661                         lr = rstate->lrd[(*expr)->id].lr;
13662                         if (lr->color == REG_UNSET) {
13663                                 continue;
13664                         }
13665                         regcm = lr->classes;
13666                         if (((regcm & range->classes) == 0) ||
13667                                 (used[lr->color])) {
13668                                 continue;
13669                         }
13670                         if (interfere(rstate, range, lr)) {
13671                                 continue;
13672                         }
13673                         range->color = lr->color;
13674                         break;
13675                 }
13676         }
13677         /* If I have not opportunitically picked a useful color
13678          * pick the first color that is free.
13679          */
13680         if (range->color == REG_UNSET) {
13681                 range->color = 
13682                         arch_select_free_register(state, used, range->classes);
13683         }
13684         if (range->color == REG_UNSET) {
13685                 struct live_range_def *lrd;
13686                 int i;
13687                 if (split_ranges(state, rstate, used, range)) {
13688                         return 0;
13689                 }
13690                 for(edge = range->edges; edge; edge = edge->next) {
13691                         warning(state, edge->node->defs->def, "edge reg %s",
13692                                 arch_reg_str(edge->node->color));
13693                         lrd = edge->node->defs;
13694                         do {
13695                                 warning(state, lrd->def, " %s %p",
13696                                         tops(lrd->def->op), lrd->def);
13697                                 lrd = lrd->next;
13698                         } while(lrd != edge->node->defs);
13699                 }
13700                 warning(state, range->defs->def, "range: ");
13701                 lrd = range->defs;
13702                 do {
13703                         warning(state, lrd->def, " %s %p",
13704                                 tops(lrd->def->op), lrd->def);
13705                         lrd = lrd->next;
13706                 } while(lrd != range->defs);
13707                         
13708                 warning(state, range->defs->def, "classes: %x",
13709                         range->classes);
13710                 for(i = 0; i < MAX_REGISTERS; i++) {
13711                         if (used[i]) {
13712                                 warning(state, range->defs->def, "used: %s",
13713                                         arch_reg_str(i));
13714                         }
13715                 }
13716 #if DEBUG_COLOR_GRAPH < 2
13717                 error(state, range->defs->def, "too few registers");
13718 #else
13719                 internal_error(state, range->defs->def, "too few registers");
13720 #endif
13721         }
13722         range->classes &= arch_reg_regcm(state, range->color);
13723         if ((range->color == REG_UNSET) || (range->classes == 0)) {
13724                 internal_error(state, range->defs->def, "select_free_color did not?");
13725         }
13726         return 1;
13727 }
13728
13729 static int color_graph(struct compile_state *state, struct reg_state *rstate)
13730 {
13731         int colored;
13732         struct live_range_edge *edge;
13733         struct live_range *range;
13734         if (rstate->low) {
13735                 cgdebug_printf("Lo: ");
13736                 range = rstate->low;
13737                 if (*range->group_prev != range) {
13738                         internal_error(state, 0, "lo: *prev != range?");
13739                 }
13740                 *range->group_prev = range->group_next;
13741                 if (range->group_next) {
13742                         range->group_next->group_prev = range->group_prev;
13743                 }
13744                 if (&range->group_next == rstate->low_tail) {
13745                         rstate->low_tail = range->group_prev;
13746                 }
13747                 if (rstate->low == range) {
13748                         internal_error(state, 0, "low: next != prev?");
13749                 }
13750         }
13751         else if (rstate->high) {
13752                 cgdebug_printf("Hi: ");
13753                 range = rstate->high;
13754                 if (*range->group_prev != range) {
13755                         internal_error(state, 0, "hi: *prev != range?");
13756                 }
13757                 *range->group_prev = range->group_next;
13758                 if (range->group_next) {
13759                         range->group_next->group_prev = range->group_prev;
13760                 }
13761                 if (&range->group_next == rstate->high_tail) {
13762                         rstate->high_tail = range->group_prev;
13763                 }
13764                 if (rstate->high == range) {
13765                         internal_error(state, 0, "high: next != prev?");
13766                 }
13767         }
13768         else {
13769                 return 1;
13770         }
13771         cgdebug_printf(" %d\n", range - rstate->lr);
13772         range->group_prev = 0;
13773         for(edge = range->edges; edge; edge = edge->next) {
13774                 struct live_range *node;
13775                 node = edge->node;
13776                 /* Move nodes from the high to the low list */
13777                 if (node->group_prev && (node->color == REG_UNSET) &&
13778                         (node->degree == regc_max_size(state, node->classes))) {
13779                         if (*node->group_prev != node) {
13780                                 internal_error(state, 0, "move: *prev != node?");
13781                         }
13782                         *node->group_prev = node->group_next;
13783                         if (node->group_next) {
13784                                 node->group_next->group_prev = node->group_prev;
13785                         }
13786                         if (&node->group_next == rstate->high_tail) {
13787                                 rstate->high_tail = node->group_prev;
13788                         }
13789                         cgdebug_printf("Moving...%d to low\n", node - rstate->lr);
13790                         node->group_prev  = rstate->low_tail;
13791                         node->group_next  = 0;
13792                         *rstate->low_tail = node;
13793                         rstate->low_tail  = &node->group_next;
13794                         if (*node->group_prev != node) {
13795                                 internal_error(state, 0, "move2: *prev != node?");
13796                         }
13797                 }
13798                 node->degree -= 1;
13799         }
13800         colored = color_graph(state, rstate);
13801         if (colored) {
13802                 cgdebug_printf("Coloring %d @", range - rstate->lr);
13803                 cgdebug_loc(state, range->defs->def);
13804                 cgdebug_flush();
13805                 colored = select_free_color(state, rstate, range);
13806                 cgdebug_printf(" %s\n", arch_reg_str(range->color));
13807         }
13808         return colored;
13809 }
13810
13811 #if DEBUG_CONSISTENCY
13812 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
13813 {
13814         struct live_range *lr;
13815         struct live_range_edge *edge;
13816         struct triple *ins, *first;
13817         char used[MAX_REGISTERS];
13818         first = RHS(state->main_function, 0);
13819         ins = first;
13820         do {
13821                 if (triple_is_def(state, ins)) {
13822                         if ((ins->id < 0) || (ins->id > rstate->defs)) {
13823                                 internal_error(state, ins, 
13824                                         "triple without a live range def");
13825                         }
13826                         lr = rstate->lrd[ins->id].lr;
13827                         if (lr->color == REG_UNSET) {
13828                                 internal_error(state, ins,
13829                                         "triple without a color");
13830                         }
13831                         /* Find the registers used by the edges */
13832                         memset(used, 0, sizeof(used));
13833                         for(edge = lr->edges; edge; edge = edge->next) {
13834                                 if (edge->node->color == REG_UNSET) {
13835                                         internal_error(state, 0,
13836                                                 "live range without a color");
13837                         }
13838                                 reg_fill_used(state, used, edge->node->color);
13839                         }
13840                         if (used[lr->color]) {
13841                                 internal_error(state, ins,
13842                                         "triple with already used color");
13843                         }
13844                 }
13845                 ins = ins->next;
13846         } while(ins != first);
13847 }
13848 #else
13849 static inline void verify_colors(struct compile_state *state, struct reg_state *rstate) {}
13850 #endif
13851
13852 static void color_triples(struct compile_state *state, struct reg_state *rstate)
13853 {
13854         struct live_range *lr;
13855         struct triple *first, *ins;
13856         first = RHS(state->main_function, 0);
13857         ins = first;
13858         do {
13859                 if ((ins->id < 0) || (ins->id > rstate->defs)) {
13860                         internal_error(state, ins, 
13861                                 "triple without a live range");
13862                 }
13863                 lr = rstate->lrd[ins->id].lr;
13864                 SET_REG(ins->id, lr->color);
13865                 ins = ins->next;
13866         } while (ins != first);
13867 }
13868
13869 static struct live_range *merge_sort_lr(
13870         struct live_range *first, struct live_range *last)
13871 {
13872         struct live_range *mid, *join, **join_tail, *pick;
13873         size_t size;
13874         size = (last - first) + 1;
13875         if (size >= 2) {
13876                 mid = first + size/2;
13877                 first = merge_sort_lr(first, mid -1);
13878                 mid   = merge_sort_lr(mid, last);
13879                 
13880                 join = 0;
13881                 join_tail = &join;
13882                 /* merge the two lists */
13883                 while(first && mid) {
13884                         if ((first->degree < mid->degree) ||
13885                                 ((first->degree == mid->degree) &&
13886                                         (first->length < mid->length))) {
13887                                 pick = first;
13888                                 first = first->group_next;
13889                                 if (first) {
13890                                         first->group_prev = 0;
13891                                 }
13892                         }
13893                         else {
13894                                 pick = mid;
13895                                 mid = mid->group_next;
13896                                 if (mid) {
13897                                         mid->group_prev = 0;
13898                                 }
13899                         }
13900                         pick->group_next = 0;
13901                         pick->group_prev = join_tail;
13902                         *join_tail = pick;
13903                         join_tail = &pick->group_next;
13904                 }
13905                 /* Splice the remaining list */
13906                 pick = (first)? first : mid;
13907                 *join_tail = pick;
13908                 if (pick) { 
13909                         pick->group_prev = join_tail;
13910                 }
13911         }
13912         else {
13913                 if (!first->defs) {
13914                         first = 0;
13915                 }
13916                 join = first;
13917         }
13918         return join;
13919 }
13920
13921 static void ids_from_rstate(struct compile_state *state, 
13922         struct reg_state *rstate)
13923 {
13924         struct triple *ins, *first;
13925         if (!rstate->defs) {
13926                 return;
13927         }
13928         /* Display the graph if desired */
13929         if (state->debug & DEBUG_INTERFERENCE) {
13930                 print_blocks(state, stdout);
13931                 print_control_flow(state);
13932         }
13933         first = RHS(state->main_function, 0);
13934         ins = first;
13935         do {
13936                 if (ins->id) {
13937                         struct live_range_def *lrd;
13938                         lrd = &rstate->lrd[ins->id];
13939                         ins->id = lrd->orig_id;
13940                 }
13941                 ins = ins->next;
13942         } while(ins != first);
13943 }
13944
13945 static void cleanup_live_edges(struct reg_state *rstate)
13946 {
13947         int i;
13948         /* Free the edges on each node */
13949         for(i = 1; i <= rstate->ranges; i++) {
13950                 remove_live_edges(rstate, &rstate->lr[i]);
13951         }
13952 }
13953
13954 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
13955 {
13956         cleanup_live_edges(rstate);
13957         xfree(rstate->lrd);
13958         xfree(rstate->lr);
13959
13960         /* Free the variable lifetime information */
13961         if (rstate->blocks) {
13962                 free_variable_lifetimes(state, rstate->blocks);
13963         }
13964         rstate->defs = 0;
13965         rstate->ranges = 0;
13966         rstate->lrd = 0;
13967         rstate->lr = 0;
13968         rstate->blocks = 0;
13969 }
13970
13971 static void verify_consistency(struct compile_state *state);
13972 static void allocate_registers(struct compile_state *state)
13973 {
13974         struct reg_state rstate;
13975         int colored;
13976
13977         /* Clear out the reg_state */
13978         memset(&rstate, 0, sizeof(rstate));
13979         rstate.max_passes = MAX_ALLOCATION_PASSES;
13980
13981         do {
13982                 struct live_range **point, **next;
13983                 int conflicts;
13984                 int tangles;
13985                 int coalesced;
13986
13987 #if DEBUG_RANGE_CONFLICTS
13988                 fprintf(stderr, "pass: %d\n", rstate.passes);
13989 #endif
13990
13991                 /* Restore ids */
13992                 ids_from_rstate(state, &rstate);
13993
13994                 /* Cleanup the temporary data structures */
13995                 cleanup_rstate(state, &rstate);
13996
13997                 /* Compute the variable lifetimes */
13998                 rstate.blocks = compute_variable_lifetimes(state);
13999
14000                 /* Fix invalid mandatory live range coalesce conflicts */
14001                 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
14002
14003                 /* Fix two simultaneous uses of the same register.
14004                  * In a few pathlogical cases a partial untangle moves
14005                  * the tangle to a part of the graph we won't revisit.
14006                  * So we keep looping until we have no more tangle fixes
14007                  * to apply.
14008                  */
14009                 do {
14010                         tangles = correct_tangles(state, rstate.blocks);
14011                 } while(tangles);
14012
14013                 if (state->debug & DEBUG_INSERTED_COPIES) {
14014                         printf("After resolve_tangles\n");
14015                         print_blocks(state, stdout);
14016                         print_control_flow(state);
14017                 }
14018                 verify_consistency(state);
14019                 
14020                 /* Allocate and initialize the live ranges */
14021                 initialize_live_ranges(state, &rstate);
14022
14023                 /* Note current doing coalescing in a loop appears to 
14024                  * buys me nothing.  The code is left this way in case
14025                  * there is some value in it.  Or if a future bugfix
14026                  *  yields some benefit.
14027                  */
14028                 do {
14029 #if DEBUG_COALESCING
14030                         fprintf(stderr, "coalescing\n");
14031 #endif                  
14032                         /* Remove any previous live edge calculations */
14033                         cleanup_live_edges(&rstate);
14034
14035                         /* Compute the interference graph */
14036                         walk_variable_lifetimes(
14037                                 state, rstate.blocks, graph_ins, &rstate);
14038                         
14039                         /* Display the interference graph if desired */
14040                         if (state->debug & DEBUG_INTERFERENCE) {
14041                                 print_interference_blocks(state, &rstate, stdout, 1);
14042                                 printf("\nlive variables by instruction\n");
14043                                 walk_variable_lifetimes(
14044                                         state, rstate.blocks, 
14045                                         print_interference_ins, &rstate);
14046                         }
14047                         
14048                         coalesced = coalesce_live_ranges(state, &rstate);
14049
14050 #if DEBUG_COALESCING
14051                         fprintf(stderr, "coalesced: %d\n", coalesced);
14052 #endif
14053                 } while(coalesced);
14054
14055                 /* Verify the interference graph */
14056                 verify_interference_graph(state, &rstate);
14057                         
14058                 /* Build the groups low and high.  But with the nodes
14059                  * first sorted by degree order.
14060                  */
14061                 rstate.low_tail  = &rstate.low;
14062                 rstate.high_tail = &rstate.high;
14063                 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
14064                 if (rstate.high) {
14065                         rstate.high->group_prev = &rstate.high;
14066                 }
14067                 for(point = &rstate.high; *point; point = &(*point)->group_next)
14068                         ;
14069                 rstate.high_tail = point;
14070                 /* Walk through the high list and move everything that needs
14071                  * to be onto low.
14072                  */
14073                 for(point = &rstate.high; *point; point = next) {
14074                         struct live_range *range;
14075                         next = &(*point)->group_next;
14076                         range = *point;
14077                         
14078                         /* If it has a low degree or it already has a color
14079                          * place the node in low.
14080                          */
14081                         if ((range->degree < regc_max_size(state, range->classes)) ||
14082                                 (range->color != REG_UNSET)) {
14083                                 cgdebug_printf("Lo: %5d degree %5d%s\n", 
14084                                         range - rstate.lr, range->degree,
14085                                         (range->color != REG_UNSET) ? " (colored)": "");
14086                                 *range->group_prev = range->group_next;
14087                                 if (range->group_next) {
14088                                         range->group_next->group_prev = range->group_prev;
14089                                 }
14090                                 if (&range->group_next == rstate.high_tail) {
14091                                         rstate.high_tail = range->group_prev;
14092                                 }
14093                                 range->group_prev  = rstate.low_tail;
14094                                 range->group_next  = 0;
14095                                 *rstate.low_tail   = range;
14096                                 rstate.low_tail    = &range->group_next;
14097                                 next = point;
14098                         }
14099                         else {
14100                                 cgdebug_printf("hi: %5d degree %5d%s\n", 
14101                                         range - rstate.lr, range->degree,
14102                                         (range->color != REG_UNSET) ? " (colored)": "");
14103                         }
14104                 }
14105                 /* Color the live_ranges */
14106                 colored = color_graph(state, &rstate);
14107                 rstate.passes++;
14108         } while (!colored);
14109
14110         /* Verify the graph was properly colored */
14111         verify_colors(state, &rstate);
14112
14113         /* Move the colors from the graph to the triples */
14114         color_triples(state, &rstate);
14115
14116         /* Cleanup the temporary data structures */
14117         cleanup_rstate(state, &rstate);
14118 }
14119
14120 /* Sparce Conditional Constant Propogation
14121  * =========================================
14122  */
14123 struct ssa_edge;
14124 struct flow_block;
14125 struct lattice_node {
14126         unsigned old_id;
14127         struct triple *def;
14128         struct ssa_edge *out;
14129         struct flow_block *fblock;
14130         struct triple *val;
14131         /* lattice high   val && !is_const(val) 
14132          * lattice const  is_const(val)
14133          * lattice low    val == 0
14134          */
14135 };
14136 struct ssa_edge {
14137         struct lattice_node *src;
14138         struct lattice_node *dst;
14139         struct ssa_edge *work_next;
14140         struct ssa_edge *work_prev;
14141         struct ssa_edge *out_next;
14142 };
14143 struct flow_edge {
14144         struct flow_block *src;
14145         struct flow_block *dst;
14146         struct flow_edge *work_next;
14147         struct flow_edge *work_prev;
14148         struct flow_edge *in_next;
14149         struct flow_edge *out_next;
14150         int executable;
14151 };
14152 struct flow_block {
14153         struct block *block;
14154         struct flow_edge *in;
14155         struct flow_edge *out;
14156         struct flow_edge left, right;
14157 };
14158
14159 struct scc_state {
14160         int ins_count;
14161         struct lattice_node *lattice;
14162         struct ssa_edge     *ssa_edges;
14163         struct flow_block   *flow_blocks;
14164         struct flow_edge    *flow_work_list;
14165         struct ssa_edge     *ssa_work_list;
14166 };
14167
14168
14169 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, 
14170         struct flow_edge *fedge)
14171 {
14172         if (!scc->flow_work_list) {
14173                 scc->flow_work_list = fedge;
14174                 fedge->work_next = fedge->work_prev = fedge;
14175         }
14176         else {
14177                 struct flow_edge *ftail;
14178                 ftail = scc->flow_work_list->work_prev;
14179                 fedge->work_next = ftail->work_next;
14180                 fedge->work_prev = ftail;
14181                 fedge->work_next->work_prev = fedge;
14182                 fedge->work_prev->work_next = fedge;
14183         }
14184 }
14185
14186 static struct flow_edge *scc_next_fedge(
14187         struct compile_state *state, struct scc_state *scc)
14188 {
14189         struct flow_edge *fedge;
14190         fedge = scc->flow_work_list;
14191         if (fedge) {
14192                 fedge->work_next->work_prev = fedge->work_prev;
14193                 fedge->work_prev->work_next = fedge->work_next;
14194                 if (fedge->work_next != fedge) {
14195                         scc->flow_work_list = fedge->work_next;
14196                 } else {
14197                         scc->flow_work_list = 0;
14198                 }
14199         }
14200         return fedge;
14201 }
14202
14203 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
14204         struct ssa_edge *sedge)
14205 {
14206         if (!scc->ssa_work_list) {
14207                 scc->ssa_work_list = sedge;
14208                 sedge->work_next = sedge->work_prev = sedge;
14209         }
14210         else {
14211                 struct ssa_edge *stail;
14212                 stail = scc->ssa_work_list->work_prev;
14213                 sedge->work_next = stail->work_next;
14214                 sedge->work_prev = stail;
14215                 sedge->work_next->work_prev = sedge;
14216                 sedge->work_prev->work_next = sedge;
14217         }
14218 }
14219
14220 static struct ssa_edge *scc_next_sedge(
14221         struct compile_state *state, struct scc_state *scc)
14222 {
14223         struct ssa_edge *sedge;
14224         sedge = scc->ssa_work_list;
14225         if (sedge) {
14226                 sedge->work_next->work_prev = sedge->work_prev;
14227                 sedge->work_prev->work_next = sedge->work_next;
14228                 if (sedge->work_next != sedge) {
14229                         scc->ssa_work_list = sedge->work_next;
14230                 } else {
14231                         scc->ssa_work_list = 0;
14232                 }
14233         }
14234         return sedge;
14235 }
14236
14237 static void initialize_scc_state(
14238         struct compile_state *state, struct scc_state *scc)
14239 {
14240         int ins_count, ssa_edge_count;
14241         int ins_index, ssa_edge_index, fblock_index;
14242         struct triple *first, *ins;
14243         struct block *block;
14244         struct flow_block *fblock;
14245
14246         memset(scc, 0, sizeof(*scc));
14247
14248         /* Inialize pass zero find out how much memory we need */
14249         first = RHS(state->main_function, 0);
14250         ins = first;
14251         ins_count = ssa_edge_count = 0;
14252         do {
14253                 struct triple_set *edge;
14254                 ins_count += 1;
14255                 for(edge = ins->use; edge; edge = edge->next) {
14256                         ssa_edge_count++;
14257                 }
14258                 ins = ins->next;
14259         } while(ins != first);
14260 #if DEBUG_SCC
14261         fprintf(stderr, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
14262                 ins_count, ssa_edge_count, state->last_vertex);
14263 #endif
14264         scc->ins_count   = ins_count;
14265         scc->lattice     = 
14266                 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
14267         scc->ssa_edges   = 
14268                 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
14269         scc->flow_blocks = 
14270                 xcmalloc(sizeof(*scc->flow_blocks)*(state->last_vertex + 1), 
14271                         "flow_blocks");
14272
14273         /* Initialize pass one collect up the nodes */
14274         fblock = 0;
14275         block = 0;
14276         ins_index = ssa_edge_index = fblock_index = 0;
14277         ins = first;
14278         do {
14279                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14280                         block = ins->u.block;
14281                         if (!block) {
14282                                 internal_error(state, ins, "label without block");
14283                         }
14284                         fblock_index += 1;
14285                         block->vertex = fblock_index;
14286                         fblock = &scc->flow_blocks[fblock_index];
14287                         fblock->block = block;
14288                 }
14289                 {
14290                         struct lattice_node *lnode;
14291                         ins_index += 1;
14292                         lnode = &scc->lattice[ins_index];
14293                         lnode->def = ins;
14294                         lnode->out = 0;
14295                         lnode->fblock = fblock;
14296                         lnode->val = ins; /* LATTICE HIGH */
14297                         lnode->old_id = ins->id;
14298                         ins->id = ins_index;
14299                 }
14300                 ins = ins->next;
14301         } while(ins != first);
14302         /* Initialize pass two collect up the edges */
14303         block = 0;
14304         fblock = 0;
14305         ins = first;
14306         do {
14307                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
14308                         struct flow_edge *fedge, **ftail;
14309                         struct block_set *bedge;
14310                         block = ins->u.block;
14311                         fblock = &scc->flow_blocks[block->vertex];
14312                         fblock->in = 0;
14313                         fblock->out = 0;
14314                         ftail = &fblock->out;
14315                         if (block->left) {
14316                                 fblock->left.dst = &scc->flow_blocks[block->left->vertex];
14317                                 if (fblock->left.dst->block != block->left) {
14318                                         internal_error(state, 0, "block mismatch");
14319                                 }
14320                                 fblock->left.out_next = 0;
14321                                 *ftail = &fblock->left;
14322                                 ftail = &fblock->left.out_next;
14323                         }
14324                         if (block->right) {
14325                                 fblock->right.dst = &scc->flow_blocks[block->right->vertex];
14326                                 if (fblock->right.dst->block != block->right) {
14327                                         internal_error(state, 0, "block mismatch");
14328                                 }
14329                                 fblock->right.out_next = 0;
14330                                 *ftail = &fblock->right;
14331                                 ftail = &fblock->right.out_next;
14332                         }
14333                         for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
14334                                 fedge->src = fblock;
14335                                 fedge->work_next = fedge->work_prev = fedge;
14336                                 fedge->executable = 0;
14337                         }
14338                         ftail = &fblock->in;
14339                         for(bedge = block->use; bedge; bedge = bedge->next) {
14340                                 struct block *src_block;
14341                                 struct flow_block *sfblock;
14342                                 struct flow_edge *sfedge;
14343                                 src_block = bedge->member;
14344                                 sfblock = &scc->flow_blocks[src_block->vertex];
14345                                 sfedge = 0;
14346                                 if (src_block->left == block) {
14347                                         sfedge = &sfblock->left;
14348                                 } else {
14349                                         sfedge = &sfblock->right;
14350                                 }
14351                                 *ftail = sfedge;
14352                                 ftail = &sfedge->in_next;
14353                                 sfedge->in_next = 0;
14354                         }
14355                 }
14356                 {
14357                         struct triple_set *edge;
14358                         struct ssa_edge **stail;
14359                         struct lattice_node *lnode;
14360                         lnode = &scc->lattice[ins->id];
14361                         lnode->out = 0;
14362                         stail = &lnode->out;
14363                         for(edge = ins->use; edge; edge = edge->next) {
14364                                 struct ssa_edge *sedge;
14365                                 ssa_edge_index += 1;
14366                                 sedge = &scc->ssa_edges[ssa_edge_index];
14367                                 *stail = sedge;
14368                                 stail = &sedge->out_next;
14369                                 sedge->src = lnode;
14370                                 sedge->dst = &scc->lattice[edge->member->id];
14371                                 sedge->work_next = sedge->work_prev = sedge;
14372                                 sedge->out_next = 0;
14373                         }
14374                 }
14375                 ins = ins->next;
14376         } while(ins != first);
14377         /* Setup a dummy block 0 as a node above the start node */
14378         {
14379                 struct flow_block *fblock, *dst;
14380                 struct flow_edge *fedge;
14381                 fblock = &scc->flow_blocks[0];
14382                 fblock->block = 0;
14383                 fblock->in = 0;
14384                 fblock->out = &fblock->left;
14385                 dst = &scc->flow_blocks[state->first_block->vertex];
14386                 fedge = &fblock->left;
14387                 fedge->src        = fblock;
14388                 fedge->dst        = dst;
14389                 fedge->work_next  = fedge;
14390                 fedge->work_prev  = fedge;
14391                 fedge->in_next    = fedge->dst->in;
14392                 fedge->out_next   = 0;
14393                 fedge->executable = 0;
14394                 fedge->dst->in = fedge;
14395                 
14396                 /* Initialize the work lists */
14397                 scc->flow_work_list = 0;
14398                 scc->ssa_work_list  = 0;
14399                 scc_add_fedge(state, scc, fedge);
14400         }
14401 #if DEBUG_SCC
14402         fprintf(stderr, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
14403                 ins_index, ssa_edge_index, fblock_index);
14404 #endif
14405 }
14406
14407         
14408 static void free_scc_state(
14409         struct compile_state *state, struct scc_state *scc)
14410 {
14411         xfree(scc->flow_blocks);
14412         xfree(scc->ssa_edges);
14413         xfree(scc->lattice);
14414         
14415 }
14416
14417 static struct lattice_node *triple_to_lattice(
14418         struct compile_state *state, struct scc_state *scc, struct triple *ins)
14419 {
14420         if (ins->id <= 0) {
14421                 internal_error(state, ins, "bad id");
14422         }
14423         return &scc->lattice[ins->id];
14424 }
14425
14426 static struct triple *preserve_lval(
14427         struct compile_state *state, struct lattice_node *lnode)
14428 {
14429         struct triple *old;
14430         /* Preserve the original value */
14431         if (lnode->val) {
14432                 old = dup_triple(state, lnode->val);
14433                 if (lnode->val != lnode->def) {
14434                         xfree(lnode->val);
14435                 }
14436                 lnode->val = 0;
14437         } else {
14438                 old = 0;
14439         }
14440         return old;
14441 }
14442
14443 static int lval_changed(struct compile_state *state, 
14444         struct triple *old, struct lattice_node *lnode)
14445 {
14446         int changed;
14447         /* See if the lattice value has changed */
14448         changed = 1;
14449         if (!old && !lnode->val) {
14450                 changed = 0;
14451         }
14452         if (changed && lnode->val && !is_const(lnode->val)) {
14453                 changed = 0;
14454         }
14455         if (changed &&
14456                 lnode->val && old &&
14457                 (memcmp(lnode->val->param, old->param,
14458                         TRIPLE_SIZE(lnode->val->sizes) * sizeof(lnode->val->param[0])) == 0) &&
14459                 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
14460                 changed = 0;
14461         }
14462         if (old) {
14463                 xfree(old);
14464         }
14465         return changed;
14466
14467 }
14468
14469 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 
14470         struct lattice_node *lnode)
14471 {
14472         struct lattice_node *tmp;
14473         struct triple **slot, *old;
14474         struct flow_edge *fedge;
14475         int index;
14476         if (lnode->def->op != OP_PHI) {
14477                 internal_error(state, lnode->def, "not phi");
14478         }
14479         /* Store the original value */
14480         old = preserve_lval(state, lnode);
14481
14482         /* default to lattice high */
14483         lnode->val = lnode->def;
14484         slot = &RHS(lnode->def, 0);
14485         index = 0;
14486         for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
14487                 if (!fedge->executable) {
14488                         continue;
14489                 }
14490                 if (!slot[index]) {
14491                         internal_error(state, lnode->def, "no phi value");
14492                 }
14493                 tmp = triple_to_lattice(state, scc, slot[index]);
14494                 /* meet(X, lattice low) = lattice low */
14495                 if (!tmp->val) {
14496                         lnode->val = 0;
14497                 }
14498                 /* meet(X, lattice high) = X */
14499                 else if (!tmp->val) {
14500                         lnode->val = lnode->val;
14501                 }
14502                 /* meet(lattice high, X) = X */
14503                 else if (!is_const(lnode->val)) {
14504                         lnode->val = dup_triple(state, tmp->val);
14505                         lnode->val->type = lnode->def->type;
14506                 }
14507                 /* meet(const, const) = const or lattice low */
14508                 else if (!constants_equal(state, lnode->val, tmp->val)) {
14509                         lnode->val = 0;
14510                 }
14511                 if (!lnode->val) {
14512                         break;
14513                 }
14514         }
14515 #if DEBUG_SCC
14516         fprintf(stderr, "phi: %d -> %s\n",
14517                 lnode->def->id,
14518                 (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14519 #endif
14520         /* If the lattice value has changed update the work lists. */
14521         if (lval_changed(state, old, lnode)) {
14522                 struct ssa_edge *sedge;
14523                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14524                         scc_add_sedge(state, scc, sedge);
14525                 }
14526         }
14527 }
14528
14529 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
14530         struct lattice_node *lnode)
14531 {
14532         int changed;
14533         struct triple *old, *scratch;
14534         struct triple **dexpr, **vexpr;
14535         int count, i;
14536         
14537         /* Store the original value */
14538         old = preserve_lval(state, lnode);
14539
14540         /* Reinitialize the value */
14541         lnode->val = scratch = dup_triple(state, lnode->def);
14542         scratch->id = lnode->old_id;
14543         scratch->next     = scratch;
14544         scratch->prev     = scratch;
14545         scratch->use      = 0;
14546
14547         count = TRIPLE_SIZE(scratch->sizes);
14548         for(i = 0; i < count; i++) {
14549                 dexpr = &lnode->def->param[i];
14550                 vexpr = &scratch->param[i];
14551                 *vexpr = *dexpr;
14552                 if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14553                         (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14554                         *dexpr) {
14555                         struct lattice_node *tmp;
14556                         tmp = triple_to_lattice(state, scc, *dexpr);
14557                         *vexpr = (tmp->val)? tmp->val : tmp->def;
14558                 }
14559         }
14560         if (scratch->op == OP_BRANCH) {
14561                 scratch->next = lnode->def->next;
14562         }
14563         /* Recompute the value */
14564 #warning "FIXME see if simplify does anything bad"
14565         /* So far it looks like only the strength reduction
14566          * optimization are things I need to worry about.
14567          */
14568         simplify(state, scratch);
14569         /* Cleanup my value */
14570         if (scratch->use) {
14571                 internal_error(state, lnode->def, "scratch used?");
14572         }
14573         if ((scratch->prev != scratch) ||
14574                 ((scratch->next != scratch) &&
14575                         ((lnode->def->op != OP_BRANCH) ||
14576                                 (scratch->next != lnode->def->next)))) {
14577                 internal_error(state, lnode->def, "scratch in list?");
14578         }
14579         /* undo any uses... */
14580         count = TRIPLE_SIZE(scratch->sizes);
14581         for(i = 0; i < count; i++) {
14582                 vexpr = &scratch->param[i];
14583                 if (*vexpr) {
14584                         unuse_triple(*vexpr, scratch);
14585                 }
14586         }
14587         if (!is_const(scratch)) {
14588                 for(i = 0; i < count; i++) {
14589                         dexpr = &lnode->def->param[i];
14590                         if (((i < TRIPLE_MISC_OFF(scratch->sizes)) ||
14591                                 (i >= TRIPLE_TARG_OFF(scratch->sizes))) &&
14592                                 *dexpr) {
14593                                 struct lattice_node *tmp;
14594                                 tmp = triple_to_lattice(state, scc, *dexpr);
14595                                 if (!tmp->val) {
14596                                         lnode->val = 0;
14597                                 }
14598                         }
14599                 }
14600         }
14601         if (lnode->val && 
14602                 (lnode->val->op == lnode->def->op) &&
14603                 (memcmp(lnode->val->param, lnode->def->param, 
14604                         count * sizeof(lnode->val->param[0])) == 0) &&
14605                 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
14606                 lnode->val = lnode->def;
14607         }
14608         /* Find the cases that are always lattice lo */
14609         if (lnode->val && 
14610                 triple_is_def(state, lnode->val) &&
14611                 !triple_is_pure(state, lnode->val)) {
14612                 lnode->val = 0;
14613         }
14614         if (lnode->val && 
14615                 (lnode->val->op == OP_SDECL) && 
14616                 (lnode->val != lnode->def)) {
14617                 internal_error(state, lnode->def, "bad sdecl");
14618         }
14619         /* See if the lattice value has changed */
14620         changed = lval_changed(state, old, lnode);
14621         if (lnode->val != scratch) {
14622                 xfree(scratch);
14623         }
14624         return changed;
14625 }
14626
14627 static void scc_visit_branch(struct compile_state *state, struct scc_state *scc,
14628         struct lattice_node *lnode)
14629 {
14630         struct lattice_node *cond;
14631 #if DEBUG_SCC
14632         {
14633                 struct flow_edge *fedge;
14634                 fprintf(stderr, "branch: %d (",
14635                         lnode->def->id);
14636                 
14637                 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
14638                         fprintf(stderr, " %d", fedge->dst->block->vertex);
14639                 }
14640                 fprintf(stderr, " )");
14641                 if (TRIPLE_RHS(lnode->def->sizes) > 0) {
14642                         fprintf(stderr, " <- %d",
14643                                 RHS(lnode->def, 0)->id);
14644                 }
14645                 fprintf(stderr, "\n");
14646         }
14647 #endif
14648         if (lnode->def->op != OP_BRANCH) {
14649                 internal_error(state, lnode->def, "not branch");
14650         }
14651         /* This only applies to conditional branches */
14652         if (TRIPLE_RHS(lnode->def->sizes) == 0) {
14653                 return;
14654         }
14655         cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
14656         if (cond->val && !is_const(cond->val)) {
14657 #warning "FIXME do I need to do something here?"
14658                 warning(state, cond->def, "condition not constant?");
14659                 return;
14660         }
14661         if (cond->val == 0) {
14662                 scc_add_fedge(state, scc, cond->fblock->out);
14663                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14664         }
14665         else if (cond->val->u.cval) {
14666                 scc_add_fedge(state, scc, cond->fblock->out->out_next);
14667                 
14668         } else {
14669                 scc_add_fedge(state, scc, cond->fblock->out);
14670         }
14671
14672 }
14673
14674 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
14675         struct lattice_node *lnode)
14676 {
14677         int changed;
14678
14679         changed = compute_lnode_val(state, scc, lnode);
14680 #if DEBUG_SCC
14681         {
14682                 struct triple **expr;
14683                 fprintf(stderr, "expr: %3d %10s (",
14684                         lnode->def->id, tops(lnode->def->op));
14685                 expr = triple_rhs(state, lnode->def, 0);
14686                 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
14687                         if (*expr) {
14688                                 fprintf(stderr, " %d", (*expr)->id);
14689                         }
14690                 }
14691                 fprintf(stderr, " ) -> %s\n",
14692                         (!lnode->val)? "lo": is_const(lnode->val)? "const": "hi");
14693         }
14694 #endif
14695         if (lnode->def->op == OP_BRANCH) {
14696                 scc_visit_branch(state, scc, lnode);
14697
14698         }
14699         else if (changed) {
14700                 struct ssa_edge *sedge;
14701                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
14702                         scc_add_sedge(state, scc, sedge);
14703                 }
14704         }
14705 }
14706
14707 static void scc_writeback_values(
14708         struct compile_state *state, struct scc_state *scc)
14709 {
14710         struct triple *first, *ins;
14711         first = RHS(state->main_function, 0);
14712         ins = first;
14713         do {
14714                 struct lattice_node *lnode;
14715                 lnode = triple_to_lattice(state, scc, ins);
14716                 /* Restore id */
14717                 ins->id = lnode->old_id;
14718 #if DEBUG_SCC
14719                 if (lnode->val && !is_const(lnode->val)) {
14720                         warning(state, lnode->def, 
14721                                 "lattice node still high?");
14722                 }
14723 #endif
14724                 if (lnode->val && (lnode->val != ins)) {
14725                         /* See if it something I know how to write back */
14726                         switch(lnode->val->op) {
14727                         case OP_INTCONST:
14728                                 mkconst(state, ins, lnode->val->u.cval);
14729                                 break;
14730                         case OP_ADDRCONST:
14731                                 mkaddr_const(state, ins, 
14732                                         MISC(lnode->val, 0), lnode->val->u.cval);
14733                                 break;
14734                         default:
14735                                 /* By default don't copy the changes,
14736                                  * recompute them in place instead.
14737                                  */
14738                                 simplify(state, ins);
14739                                 break;
14740                         }
14741                         if (is_const(lnode->val) &&
14742                                 !constants_equal(state, lnode->val, ins)) {
14743                                 internal_error(state, 0, "constants not equal");
14744                         }
14745                         /* Free the lattice nodes */
14746                         xfree(lnode->val);
14747                         lnode->val = 0;
14748                 }
14749                 ins = ins->next;
14750         } while(ins != first);
14751 }
14752
14753 static void scc_transform(struct compile_state *state)
14754 {
14755         struct scc_state scc;
14756
14757         initialize_scc_state(state, &scc);
14758
14759         while(scc.flow_work_list || scc.ssa_work_list) {
14760                 struct flow_edge *fedge;
14761                 struct ssa_edge *sedge;
14762                 struct flow_edge *fptr;
14763                 while((fedge = scc_next_fedge(state, &scc))) {
14764                         struct block *block;
14765                         struct triple *ptr;
14766                         struct flow_block *fblock;
14767                         int time;
14768                         int done;
14769                         if (fedge->executable) {
14770                                 continue;
14771                         }
14772                         if (!fedge->dst) {
14773                                 internal_error(state, 0, "fedge without dst");
14774                         }
14775                         if (!fedge->src) {
14776                                 internal_error(state, 0, "fedge without src");
14777                         }
14778                         fedge->executable = 1;
14779                         fblock = fedge->dst;
14780                         block = fblock->block;
14781                         time = 0;
14782                         for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14783                                 if (fptr->executable) {
14784                                         time++;
14785                                 }
14786                         }
14787 #if DEBUG_SCC
14788                         fprintf(stderr, "vertex: %d time: %d\n", 
14789                                 block->vertex, time);
14790                         
14791 #endif
14792                         done = 0;
14793                         for(ptr = block->first; !done; ptr = ptr->next) {
14794                                 struct lattice_node *lnode;
14795                                 done = (ptr == block->last);
14796                                 lnode = &scc.lattice[ptr->id];
14797                                 if (ptr->op == OP_PHI) {
14798                                         scc_visit_phi(state, &scc, lnode);
14799                                 }
14800                                 else if (time == 1) {
14801                                         scc_visit_expr(state, &scc, lnode);
14802                                 }
14803                         }
14804                         if (fblock->out && !fblock->out->out_next) {
14805                                 scc_add_fedge(state, &scc, fblock->out);
14806                         }
14807                 }
14808                 while((sedge = scc_next_sedge(state, &scc))) {
14809                         struct lattice_node *lnode;
14810                         struct flow_block *fblock;
14811                         lnode = sedge->dst;
14812                         fblock = lnode->fblock;
14813 #if DEBUG_SCC
14814                         fprintf(stderr, "sedge: %5d (%5d -> %5d)\n",
14815                                 sedge - scc.ssa_edges,
14816                                 sedge->src->def->id,
14817                                 sedge->dst->def->id);
14818 #endif
14819                         if (lnode->def->op == OP_PHI) {
14820                                 scc_visit_phi(state, &scc, lnode);
14821                         }
14822                         else {
14823                                 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
14824                                         if (fptr->executable) {
14825                                                 break;
14826                                         }
14827                                 }
14828                                 if (fptr) {
14829                                         scc_visit_expr(state, &scc, lnode);
14830                                 }
14831                         }
14832                 }
14833         }
14834         
14835         scc_writeback_values(state, &scc);
14836         free_scc_state(state, &scc);
14837 }
14838
14839
14840 static void transform_to_arch_instructions(struct compile_state *state)
14841 {
14842         struct triple *ins, *first;
14843         first = RHS(state->main_function, 0);
14844         ins = first;
14845         do {
14846                 ins = transform_to_arch_instruction(state, ins);
14847         } while(ins != first);
14848 }
14849
14850 #if DEBUG_CONSISTENCY
14851 static void verify_uses(struct compile_state *state)
14852 {
14853         struct triple *first, *ins;
14854         struct triple_set *set;
14855         first = RHS(state->main_function, 0);
14856         ins = first;
14857         do {
14858                 struct triple **expr;
14859                 expr = triple_rhs(state, ins, 0);
14860                 for(; expr; expr = triple_rhs(state, ins, expr)) {
14861                         struct triple *rhs;
14862                         rhs = *expr;
14863                         for(set = rhs?rhs->use:0; set; set = set->next) {
14864                                 if (set->member == ins) {
14865                                         break;
14866                                 }
14867                         }
14868                         if (!set) {
14869                                 internal_error(state, ins, "rhs not used");
14870                         }
14871                 }
14872                 expr = triple_lhs(state, ins, 0);
14873                 for(; expr; expr = triple_lhs(state, ins, expr)) {
14874                         struct triple *lhs;
14875                         lhs = *expr;
14876                         for(set =  lhs?lhs->use:0; set; set = set->next) {
14877                                 if (set->member == ins) {
14878                                         break;
14879                                 }
14880                         }
14881                         if (!set) {
14882                                 internal_error(state, ins, "lhs not used");
14883                         }
14884                 }
14885                 ins = ins->next;
14886         } while(ins != first);
14887         
14888 }
14889 static void verify_blocks_present(struct compile_state *state)
14890 {
14891         struct triple *first, *ins;
14892         if (!state->first_block) {
14893                 return;
14894         }
14895         first = RHS(state->main_function, 0);
14896         ins = first;
14897         do {
14898                 valid_ins(state, ins);
14899                 if (triple_stores_block(state, ins)) {
14900                         if (!ins->u.block) {
14901                                 internal_error(state, ins, 
14902                                         "%p not in a block?\n", ins);
14903                         }
14904                 }
14905                 ins = ins->next;
14906         } while(ins != first);
14907         
14908         
14909 }
14910 static void verify_blocks(struct compile_state *state)
14911 {
14912         struct triple *ins;
14913         struct block *block;
14914         int blocks;
14915         block = state->first_block;
14916         if (!block) {
14917                 return;
14918         }
14919         blocks = 0;
14920         do {
14921                 int users;
14922                 struct block_set *user;
14923                 blocks++;
14924                 for(ins = block->first; ins != block->last->next; ins = ins->next) {
14925                         if (triple_stores_block(state, ins) && (ins->u.block != block)) {
14926                                 internal_error(state, ins, "inconsitent block specified");
14927                         }
14928                         valid_ins(state, ins);
14929                 }
14930                 users = 0;
14931                 for(user = block->use; user; user = user->next) {
14932                         users++;
14933                         if ((block == state->last_block) &&
14934                                 (user->member == state->first_block)) {
14935                                 continue;
14936                         }
14937                         if ((user->member->left != block) &&
14938                                 (user->member->right != block)) {
14939                                 internal_error(state, user->member->first,
14940                                         "user does not use block");
14941                         }
14942                 }
14943                 if (triple_is_branch(state, block->last) &&
14944                         (block->right != block_of_triple(state, TARG(block->last, 0))))
14945                 {
14946                         internal_error(state, block->last, "block->right != TARG(0)");
14947                 }
14948                 if (!triple_is_uncond_branch(state, block->last) &&
14949                         (block != state->last_block) &&
14950                         (block->left != block_of_triple(state, block->last->next)))
14951                 {
14952                         internal_error(state, block->last, "block->left != block->last->next");
14953                 }
14954                 if (block->left) {
14955                         for(user = block->left->use; user; user = user->next) {
14956                                 if (user->member == block) {
14957                                         break;
14958                                 }
14959                         }
14960                         if (!user || user->member != block) {
14961                                 internal_error(state, block->first,
14962                                         "block does not use left");
14963                         }
14964                 }
14965                 if (block->right) {
14966                         for(user = block->right->use; user; user = user->next) {
14967                                 if (user->member == block) {
14968                                         break;
14969                                 }
14970                         }
14971                         if (!user || user->member != block) {
14972                                 internal_error(state, block->first,
14973                                         "block does not use right");
14974                         }
14975                 }
14976                 if (block->users != users) {
14977                         internal_error(state, block->first, 
14978                                 "computed users %d != stored users %d\n",
14979                                 users, block->users);
14980                 }
14981                 if (!triple_stores_block(state, block->last->next)) {
14982                         internal_error(state, block->last->next, 
14983                                 "cannot find next block");
14984                 }
14985                 block = block->last->next->u.block;
14986                 if (!block) {
14987                         internal_error(state, block->last->next,
14988                                 "bad next block");
14989                 }
14990         } while(block != state->first_block);
14991         if (blocks != state->last_vertex) {
14992                 internal_error(state, 0, "computed blocks != stored blocks %d\n",
14993                         blocks, state->last_vertex);
14994         }
14995 }
14996
14997 static void verify_domination(struct compile_state *state)
14998 {
14999         struct triple *first, *ins;
15000         struct triple_set *set;
15001         if (!state->first_block) {
15002                 return;
15003         }
15004         
15005         first = RHS(state->main_function, 0);
15006         ins = first;
15007         do {
15008                 for(set = ins->use; set; set = set->next) {
15009                         struct triple **slot;
15010                         struct triple *use_point;
15011                         int i, zrhs;
15012                         use_point = 0;
15013                         zrhs = TRIPLE_RHS(ins->sizes);
15014                         slot = &RHS(set->member, 0);
15015                         /* See if the use is on the right hand side */
15016                         for(i = 0; i < zrhs; i++) {
15017                                 if (slot[i] == ins) {
15018                                         break;
15019                                 }
15020                         }
15021                         if (i < zrhs) {
15022                                 use_point = set->member;
15023                                 if (set->member->op == OP_PHI) {
15024                                         struct block_set *bset;
15025                                         int edge;
15026                                         bset = set->member->u.block->use;
15027                                         for(edge = 0; bset && (edge < i); edge++) {
15028                                                 bset = bset->next;
15029                                         }
15030                                         if (!bset) {
15031                                                 internal_error(state, set->member, 
15032                                                         "no edge for phi rhs %d\n", i);
15033                                         }
15034                                         use_point = bset->member->last;
15035                                 }
15036                         }
15037                         if (use_point &&
15038                                 !tdominates(state, ins, use_point)) {
15039                                 internal_warning(state, ins, 
15040                                         "ins does not dominate rhs use");
15041                                 internal_error(state, use_point, 
15042                                         "non dominated rhs use point?");
15043                         }
15044                 }
15045                 ins = ins->next;
15046         } while(ins != first);
15047 }
15048
15049 static void verify_piece(struct compile_state *state)
15050 {
15051         struct triple *first, *ins;
15052         first = RHS(state->main_function, 0);
15053         ins = first;
15054         do {
15055                 struct triple *ptr;
15056                 int lhs, i;
15057                 lhs = TRIPLE_LHS(ins->sizes);
15058                 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
15059                         if (ptr != LHS(ins, i)) {
15060                                 internal_error(state, ins, "malformed lhs on %s",
15061                                         tops(ins->op));
15062                         }
15063                         if (ptr->op != OP_PIECE) {
15064                                 internal_error(state, ins, "bad lhs op %s at %d on %s",
15065                                         tops(ptr->op), i, tops(ins->op));
15066                         }
15067                         if (ptr->u.cval != i) {
15068                                 internal_error(state, ins, "bad u.cval of %d %d expected",
15069                                         ptr->u.cval, i);
15070                         }
15071                 }
15072                 ins = ins->next;
15073         } while(ins != first);
15074 }
15075 static void verify_ins_colors(struct compile_state *state)
15076 {
15077         struct triple *first, *ins;
15078         
15079         first = RHS(state->main_function, 0);
15080         ins = first;
15081         do {
15082                 ins = ins->next;
15083         } while(ins != first);
15084 }
15085 static void verify_consistency(struct compile_state *state)
15086 {
15087         verify_uses(state);
15088         verify_blocks_present(state);
15089         verify_blocks(state);
15090         verify_domination(state);
15091         verify_piece(state);
15092         verify_ins_colors(state);
15093 }
15094 #else 
15095 static void verify_consistency(struct compile_state *state) {}
15096 #endif /* DEBUG_USES */
15097
15098 static void optimize(struct compile_state *state)
15099 {
15100         if (state->debug & DEBUG_TRIPLES) {
15101                 print_triples(state);
15102         }
15103         /* Replace structures with simpler data types */
15104         flatten_structures(state);
15105         if (state->debug & DEBUG_TRIPLES) {
15106                 print_triples(state);
15107         }
15108         verify_consistency(state);
15109         /* Analize the intermediate code */
15110         setup_basic_blocks(state);
15111         analyze_idominators(state);
15112         analyze_ipdominators(state);
15113
15114         /* Transform the code to ssa form. */
15115         /*
15116          * The transformation to ssa form puts a phi function
15117          * on each of edge of a dominance frontier where that
15118          * phi function might be needed.  At -O2 if we don't
15119          * eleminate the excess phi functions we can get an
15120          * exponential code size growth.  So I kill the extra
15121          * phi functions early and I kill them often.
15122          */
15123         transform_to_ssa_form(state);
15124         eliminate_inefectual_code(state);
15125
15126         verify_consistency(state);
15127         if (state->debug & DEBUG_CODE_ELIMINATION) {
15128                 fprintf(stdout, "After transform_to_ssa_form\n");
15129                 print_blocks(state, stdout);
15130         }
15131         /* Do strength reduction and simple constant optimizations */
15132         if (state->optimize >= 1) {
15133                 simplify_all(state);
15134                 transform_from_ssa_form(state);
15135                 free_basic_blocks(state);
15136                 setup_basic_blocks(state);
15137                 analyze_idominators(state);
15138                 analyze_ipdominators(state);
15139                 transform_to_ssa_form(state);
15140                 eliminate_inefectual_code(state);
15141         }
15142         if (state->debug & DEBUG_CODE_ELIMINATION) {
15143                 fprintf(stdout, "After simplify_all\n");
15144                 print_blocks(state, stdout);
15145         }
15146         verify_consistency(state);
15147         /* Propogate constants throughout the code */
15148         if (state->optimize >= 2) {
15149                 scc_transform(state);
15150                 transform_from_ssa_form(state);
15151                 free_basic_blocks(state);
15152                 setup_basic_blocks(state);
15153                 analyze_idominators(state);
15154                 analyze_ipdominators(state);
15155                 transform_to_ssa_form(state);
15156                 eliminate_inefectual_code(state);
15157         }
15158         verify_consistency(state);
15159 #warning "WISHLIST implement single use constants (least possible register pressure)"
15160 #warning "WISHLIST implement induction variable elimination"
15161         /* Select architecture instructions and an initial partial
15162          * coloring based on architecture constraints.
15163          */
15164         transform_to_arch_instructions(state);
15165         verify_consistency(state);
15166         if (state->debug & DEBUG_ARCH_CODE) {
15167                 printf("After transform_to_arch_instructions\n");
15168                 print_blocks(state, stdout);
15169                 print_control_flow(state);
15170         }
15171         eliminate_inefectual_code(state);
15172         verify_consistency(state);
15173         if (state->debug & DEBUG_CODE_ELIMINATION) {
15174                 printf("After eliminate_inefectual_code\n");
15175                 print_blocks(state, stdout);
15176                 print_control_flow(state);
15177         }
15178         verify_consistency(state);
15179         /* Color all of the variables to see if they will fit in registers */
15180         insert_copies_to_phi(state);
15181         if (state->debug & DEBUG_INSERTED_COPIES) {
15182                 printf("After insert_copies_to_phi\n");
15183                 print_blocks(state, stdout);
15184                 print_control_flow(state);
15185         }
15186         verify_consistency(state);
15187         insert_mandatory_copies(state);
15188         if (state->debug & DEBUG_INSERTED_COPIES) {
15189                 printf("After insert_mandatory_copies\n");
15190                 print_blocks(state, stdout);
15191                 print_control_flow(state);
15192         }
15193         verify_consistency(state);
15194         allocate_registers(state);
15195         verify_consistency(state);
15196         if (state->debug & DEBUG_INTERMEDIATE_CODE) {
15197                 print_blocks(state, stdout);
15198         }
15199         if (state->debug & DEBUG_CONTROL_FLOW) {
15200                 print_control_flow(state);
15201         }
15202         /* Remove the optimization information.
15203          * This is more to check for memory consistency than to free memory.
15204          */
15205         free_basic_blocks(state);
15206 }
15207
15208 static void print_op_asm(struct compile_state *state,
15209         struct triple *ins, FILE *fp)
15210 {
15211         struct asm_info *info;
15212         const char *ptr;
15213         unsigned lhs, rhs, i;
15214         info = ins->u.ainfo;
15215         lhs = TRIPLE_LHS(ins->sizes);
15216         rhs = TRIPLE_RHS(ins->sizes);
15217         /* Don't count the clobbers in lhs */
15218         for(i = 0; i < lhs; i++) {
15219                 if (LHS(ins, i)->type == &void_type) {
15220                         break;
15221                 }
15222         }
15223         lhs = i;
15224         fprintf(fp, "#ASM\n");
15225         fputc('\t', fp);
15226         for(ptr = info->str; *ptr; ptr++) {
15227                 char *next;
15228                 unsigned long param;
15229                 struct triple *piece;
15230                 if (*ptr != '%') {
15231                         fputc(*ptr, fp);
15232                         continue;
15233                 }
15234                 ptr++;
15235                 if (*ptr == '%') {
15236                         fputc('%', fp);
15237                         continue;
15238                 }
15239                 param = strtoul(ptr, &next, 10);
15240                 if (ptr == next) {
15241                         error(state, ins, "Invalid asm template");
15242                 }
15243                 if (param >= (lhs + rhs)) {
15244                         error(state, ins, "Invalid param %%%u in asm template",
15245                                 param);
15246                 }
15247                 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
15248                 fprintf(fp, "%s", 
15249                         arch_reg_str(ID_REG(piece->id)));
15250                 ptr = next -1;
15251         }
15252         fprintf(fp, "\n#NOT ASM\n");
15253 }
15254
15255
15256 /* Only use the low x86 byte registers.  This allows me
15257  * allocate the entire register when a byte register is used.
15258  */
15259 #define X86_4_8BIT_GPRS 1
15260
15261 /* Recognized x86 cpu variants */
15262 #define BAD_CPU      0
15263 #define CPU_I386     1
15264 #define CPU_P3       2
15265 #define CPU_P4       3
15266 #define CPU_K7       4
15267 #define CPU_K8       5
15268
15269 #define CPU_DEFAULT  CPU_I386
15270
15271 /* The x86 register classes */
15272 #define REGC_FLAGS       0
15273 #define REGC_GPR8        1
15274 #define REGC_GPR16       2
15275 #define REGC_GPR32       3
15276 #define REGC_DIVIDEND64  4
15277 #define REGC_DIVIDEND32  5
15278 #define REGC_MMX         6
15279 #define REGC_XMM         7
15280 #define REGC_GPR32_8     8
15281 #define REGC_GPR16_8     9
15282 #define REGC_GPR8_LO    10
15283 #define REGC_IMM32      11
15284 #define REGC_IMM16      12
15285 #define REGC_IMM8       13
15286 #define LAST_REGC  REGC_IMM8
15287 #if LAST_REGC >= MAX_REGC
15288 #error "MAX_REGC is to low"
15289 #endif
15290
15291 /* Register class masks */
15292 #define REGCM_FLAGS      (1 << REGC_FLAGS)
15293 #define REGCM_GPR8       (1 << REGC_GPR8)
15294 #define REGCM_GPR16      (1 << REGC_GPR16)
15295 #define REGCM_GPR32      (1 << REGC_GPR32)
15296 #define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
15297 #define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
15298 #define REGCM_MMX        (1 << REGC_MMX)
15299 #define REGCM_XMM        (1 << REGC_XMM)
15300 #define REGCM_GPR32_8    (1 << REGC_GPR32_8)
15301 #define REGCM_GPR16_8    (1 << REGC_GPR16_8)
15302 #define REGCM_GPR8_LO    (1 << REGC_GPR8_LO)
15303 #define REGCM_IMM32      (1 << REGC_IMM32)
15304 #define REGCM_IMM16      (1 << REGC_IMM16)
15305 #define REGCM_IMM8       (1 << REGC_IMM8)
15306 #define REGCM_ALL        ((1 << (LAST_REGC + 1)) - 1)
15307
15308 /* The x86 registers */
15309 #define REG_EFLAGS  2
15310 #define REGC_FLAGS_FIRST REG_EFLAGS
15311 #define REGC_FLAGS_LAST  REG_EFLAGS
15312 #define REG_AL      3
15313 #define REG_BL      4
15314 #define REG_CL      5
15315 #define REG_DL      6
15316 #define REG_AH      7
15317 #define REG_BH      8
15318 #define REG_CH      9
15319 #define REG_DH      10
15320 #define REGC_GPR8_LO_FIRST REG_AL
15321 #define REGC_GPR8_LO_LAST  REG_DL
15322 #define REGC_GPR8_FIRST  REG_AL
15323 #define REGC_GPR8_LAST   REG_DH
15324 #define REG_AX     11
15325 #define REG_BX     12
15326 #define REG_CX     13
15327 #define REG_DX     14
15328 #define REG_SI     15
15329 #define REG_DI     16
15330 #define REG_BP     17
15331 #define REG_SP     18
15332 #define REGC_GPR16_FIRST REG_AX
15333 #define REGC_GPR16_LAST  REG_SP
15334 #define REG_EAX    19
15335 #define REG_EBX    20
15336 #define REG_ECX    21
15337 #define REG_EDX    22
15338 #define REG_ESI    23
15339 #define REG_EDI    24
15340 #define REG_EBP    25
15341 #define REG_ESP    26
15342 #define REGC_GPR32_FIRST REG_EAX
15343 #define REGC_GPR32_LAST  REG_ESP
15344 #define REG_EDXEAX 27
15345 #define REGC_DIVIDEND64_FIRST REG_EDXEAX
15346 #define REGC_DIVIDEND64_LAST  REG_EDXEAX
15347 #define REG_DXAX   28
15348 #define REGC_DIVIDEND32_FIRST REG_DXAX
15349 #define REGC_DIVIDEND32_LAST  REG_DXAX
15350 #define REG_MMX0   29
15351 #define REG_MMX1   30
15352 #define REG_MMX2   31
15353 #define REG_MMX3   32
15354 #define REG_MMX4   33
15355 #define REG_MMX5   34
15356 #define REG_MMX6   35
15357 #define REG_MMX7   36
15358 #define REGC_MMX_FIRST REG_MMX0
15359 #define REGC_MMX_LAST  REG_MMX7
15360 #define REG_XMM0   37
15361 #define REG_XMM1   38
15362 #define REG_XMM2   39
15363 #define REG_XMM3   40
15364 #define REG_XMM4   41
15365 #define REG_XMM5   42
15366 #define REG_XMM6   43
15367 #define REG_XMM7   44
15368 #define REGC_XMM_FIRST REG_XMM0
15369 #define REGC_XMM_LAST  REG_XMM7
15370 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
15371 #define LAST_REG   REG_XMM7
15372
15373 #define REGC_GPR32_8_FIRST REG_EAX
15374 #define REGC_GPR32_8_LAST  REG_EDX
15375 #define REGC_GPR16_8_FIRST REG_AX
15376 #define REGC_GPR16_8_LAST  REG_DX
15377
15378 #define REGC_IMM8_FIRST    -1
15379 #define REGC_IMM8_LAST     -1
15380 #define REGC_IMM16_FIRST   -2
15381 #define REGC_IMM16_LAST    -1
15382 #define REGC_IMM32_FIRST   -4
15383 #define REGC_IMM32_LAST    -1
15384
15385 #if LAST_REG >= MAX_REGISTERS
15386 #error "MAX_REGISTERS to low"
15387 #endif
15388
15389
15390 static unsigned regc_size[LAST_REGC +1] = {
15391         [REGC_FLAGS]      = REGC_FLAGS_LAST      - REGC_FLAGS_FIRST + 1,
15392         [REGC_GPR8]       = REGC_GPR8_LAST       - REGC_GPR8_FIRST + 1,
15393         [REGC_GPR16]      = REGC_GPR16_LAST      - REGC_GPR16_FIRST + 1,
15394         [REGC_GPR32]      = REGC_GPR32_LAST      - REGC_GPR32_FIRST + 1,
15395         [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
15396         [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
15397         [REGC_MMX]        = REGC_MMX_LAST        - REGC_MMX_FIRST + 1,
15398         [REGC_XMM]        = REGC_XMM_LAST        - REGC_XMM_FIRST + 1,
15399         [REGC_GPR32_8]    = REGC_GPR32_8_LAST    - REGC_GPR32_8_FIRST + 1,
15400         [REGC_GPR16_8]    = REGC_GPR16_8_LAST    - REGC_GPR16_8_FIRST + 1,
15401         [REGC_GPR8_LO]    = REGC_GPR8_LO_LAST    - REGC_GPR8_LO_FIRST + 1,
15402         [REGC_IMM32]      = 0,
15403         [REGC_IMM16]      = 0,
15404         [REGC_IMM8]       = 0,
15405 };
15406
15407 static const struct {
15408         int first, last;
15409 } regcm_bound[LAST_REGC + 1] = {
15410         [REGC_FLAGS]      = { REGC_FLAGS_FIRST,      REGC_FLAGS_LAST },
15411         [REGC_GPR8]       = { REGC_GPR8_FIRST,       REGC_GPR8_LAST },
15412         [REGC_GPR16]      = { REGC_GPR16_FIRST,      REGC_GPR16_LAST },
15413         [REGC_GPR32]      = { REGC_GPR32_FIRST,      REGC_GPR32_LAST },
15414         [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
15415         [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
15416         [REGC_MMX]        = { REGC_MMX_FIRST,        REGC_MMX_LAST },
15417         [REGC_XMM]        = { REGC_XMM_FIRST,        REGC_XMM_LAST },
15418         [REGC_GPR32_8]    = { REGC_GPR32_8_FIRST,    REGC_GPR32_8_LAST },
15419         [REGC_GPR16_8]    = { REGC_GPR16_8_FIRST,    REGC_GPR16_8_LAST },
15420         [REGC_GPR8_LO]    = { REGC_GPR8_LO_FIRST,    REGC_GPR8_LO_LAST },
15421         [REGC_IMM32]      = { REGC_IMM32_FIRST,      REGC_IMM32_LAST },
15422         [REGC_IMM16]      = { REGC_IMM16_FIRST,      REGC_IMM16_LAST },
15423         [REGC_IMM8]       = { REGC_IMM8_FIRST,       REGC_IMM8_LAST },
15424 };
15425
15426 static int arch_encode_cpu(const char *cpu)
15427 {
15428         struct cpu {
15429                 const char *name;
15430                 int cpu;
15431         } cpus[] = {
15432                 { "i386", CPU_I386 },
15433                 { "p3",   CPU_P3 },
15434                 { "p4",   CPU_P4 },
15435                 { "k7",   CPU_K7 },
15436                 { "k8",   CPU_K8 },
15437                 {  0,     BAD_CPU }
15438         };
15439         struct cpu *ptr;
15440         for(ptr = cpus; ptr->name; ptr++) {
15441                 if (strcmp(ptr->name, cpu) == 0) {
15442                         break;
15443                 }
15444         }
15445         return ptr->cpu;
15446 }
15447
15448 static unsigned arch_regc_size(struct compile_state *state, int class)
15449 {
15450         if ((class < 0) || (class > LAST_REGC)) {
15451                 return 0;
15452         }
15453         return regc_size[class];
15454 }
15455
15456 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
15457 {
15458         /* See if two register classes may have overlapping registers */
15459         unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
15460                 REGCM_GPR32_8 | REGCM_GPR32 | 
15461                 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
15462
15463         /* Special case for the immediates */
15464         if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15465                 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
15466                 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
15467                 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { 
15468                 return 0;
15469         }
15470         return (regcm1 & regcm2) ||
15471                 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
15472 }
15473
15474 static void arch_reg_equivs(
15475         struct compile_state *state, unsigned *equiv, int reg)
15476 {
15477         if ((reg < 0) || (reg > LAST_REG)) {
15478                 internal_error(state, 0, "invalid register");
15479         }
15480         *equiv++ = reg;
15481         switch(reg) {
15482         case REG_AL:
15483 #if X86_4_8BIT_GPRS
15484                 *equiv++ = REG_AH;
15485 #endif
15486                 *equiv++ = REG_AX;
15487                 *equiv++ = REG_EAX;
15488                 *equiv++ = REG_DXAX;
15489                 *equiv++ = REG_EDXEAX;
15490                 break;
15491         case REG_AH:
15492 #if X86_4_8BIT_GPRS
15493                 *equiv++ = REG_AL;
15494 #endif
15495                 *equiv++ = REG_AX;
15496                 *equiv++ = REG_EAX;
15497                 *equiv++ = REG_DXAX;
15498                 *equiv++ = REG_EDXEAX;
15499                 break;
15500         case REG_BL:  
15501 #if X86_4_8BIT_GPRS
15502                 *equiv++ = REG_BH;
15503 #endif
15504                 *equiv++ = REG_BX;
15505                 *equiv++ = REG_EBX;
15506                 break;
15507
15508         case REG_BH:
15509 #if X86_4_8BIT_GPRS
15510                 *equiv++ = REG_BL;
15511 #endif
15512                 *equiv++ = REG_BX;
15513                 *equiv++ = REG_EBX;
15514                 break;
15515         case REG_CL:
15516 #if X86_4_8BIT_GPRS
15517                 *equiv++ = REG_CH;
15518 #endif
15519                 *equiv++ = REG_CX;
15520                 *equiv++ = REG_ECX;
15521                 break;
15522
15523         case REG_CH:
15524 #if X86_4_8BIT_GPRS
15525                 *equiv++ = REG_CL;
15526 #endif
15527                 *equiv++ = REG_CX;
15528                 *equiv++ = REG_ECX;
15529                 break;
15530         case REG_DL:
15531 #if X86_4_8BIT_GPRS
15532                 *equiv++ = REG_DH;
15533 #endif
15534                 *equiv++ = REG_DX;
15535                 *equiv++ = REG_EDX;
15536                 *equiv++ = REG_DXAX;
15537                 *equiv++ = REG_EDXEAX;
15538                 break;
15539         case REG_DH:
15540 #if X86_4_8BIT_GPRS
15541                 *equiv++ = REG_DL;
15542 #endif
15543                 *equiv++ = REG_DX;
15544                 *equiv++ = REG_EDX;
15545                 *equiv++ = REG_DXAX;
15546                 *equiv++ = REG_EDXEAX;
15547                 break;
15548         case REG_AX:
15549                 *equiv++ = REG_AL;
15550                 *equiv++ = REG_AH;
15551                 *equiv++ = REG_EAX;
15552                 *equiv++ = REG_DXAX;
15553                 *equiv++ = REG_EDXEAX;
15554                 break;
15555         case REG_BX:
15556                 *equiv++ = REG_BL;
15557                 *equiv++ = REG_BH;
15558                 *equiv++ = REG_EBX;
15559                 break;
15560         case REG_CX:  
15561                 *equiv++ = REG_CL;
15562                 *equiv++ = REG_CH;
15563                 *equiv++ = REG_ECX;
15564                 break;
15565         case REG_DX:  
15566                 *equiv++ = REG_DL;
15567                 *equiv++ = REG_DH;
15568                 *equiv++ = REG_EDX;
15569                 *equiv++ = REG_DXAX;
15570                 *equiv++ = REG_EDXEAX;
15571                 break;
15572         case REG_SI:  
15573                 *equiv++ = REG_ESI;
15574                 break;
15575         case REG_DI:
15576                 *equiv++ = REG_EDI;
15577                 break;
15578         case REG_BP:
15579                 *equiv++ = REG_EBP;
15580                 break;
15581         case REG_SP:
15582                 *equiv++ = REG_ESP;
15583                 break;
15584         case REG_EAX:
15585                 *equiv++ = REG_AL;
15586                 *equiv++ = REG_AH;
15587                 *equiv++ = REG_AX;
15588                 *equiv++ = REG_DXAX;
15589                 *equiv++ = REG_EDXEAX;
15590                 break;
15591         case REG_EBX:
15592                 *equiv++ = REG_BL;
15593                 *equiv++ = REG_BH;
15594                 *equiv++ = REG_BX;
15595                 break;
15596         case REG_ECX:
15597                 *equiv++ = REG_CL;
15598                 *equiv++ = REG_CH;
15599                 *equiv++ = REG_CX;
15600                 break;
15601         case REG_EDX:
15602                 *equiv++ = REG_DL;
15603                 *equiv++ = REG_DH;
15604                 *equiv++ = REG_DX;
15605                 *equiv++ = REG_DXAX;
15606                 *equiv++ = REG_EDXEAX;
15607                 break;
15608         case REG_ESI: 
15609                 *equiv++ = REG_SI;
15610                 break;
15611         case REG_EDI: 
15612                 *equiv++ = REG_DI;
15613                 break;
15614         case REG_EBP: 
15615                 *equiv++ = REG_BP;
15616                 break;
15617         case REG_ESP: 
15618                 *equiv++ = REG_SP;
15619                 break;
15620         case REG_DXAX: 
15621                 *equiv++ = REG_AL;
15622                 *equiv++ = REG_AH;
15623                 *equiv++ = REG_DL;
15624                 *equiv++ = REG_DH;
15625                 *equiv++ = REG_AX;
15626                 *equiv++ = REG_DX;
15627                 *equiv++ = REG_EAX;
15628                 *equiv++ = REG_EDX;
15629                 *equiv++ = REG_EDXEAX;
15630                 break;
15631         case REG_EDXEAX: 
15632                 *equiv++ = REG_AL;
15633                 *equiv++ = REG_AH;
15634                 *equiv++ = REG_DL;
15635                 *equiv++ = REG_DH;
15636                 *equiv++ = REG_AX;
15637                 *equiv++ = REG_DX;
15638                 *equiv++ = REG_EAX;
15639                 *equiv++ = REG_EDX;
15640                 *equiv++ = REG_DXAX;
15641                 break;
15642         }
15643         *equiv++ = REG_UNSET; 
15644 }
15645
15646 static unsigned arch_avail_mask(struct compile_state *state)
15647 {
15648         unsigned avail_mask;
15649         /* REGCM_GPR8 is not available */
15650         avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 | 
15651                 REGCM_GPR32 | REGCM_GPR32_8 | 
15652                 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15653                 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
15654         switch(state->cpu) {
15655         case CPU_P3:
15656         case CPU_K7:
15657                 avail_mask |= REGCM_MMX;
15658                 break;
15659         case CPU_P4:
15660         case CPU_K8:
15661                 avail_mask |= REGCM_MMX | REGCM_XMM;
15662                 break;
15663         }
15664         return avail_mask;
15665 }
15666
15667 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
15668 {
15669         unsigned mask, result;
15670         int class, class2;
15671         result = regcm;
15672
15673         for(class = 0, mask = 1; mask; mask <<= 1, class++) {
15674                 if ((result & mask) == 0) {
15675                         continue;
15676                 }
15677                 if (class > LAST_REGC) {
15678                         result &= ~mask;
15679                 }
15680                 for(class2 = 0; class2 <= LAST_REGC; class2++) {
15681                         if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
15682                                 (regcm_bound[class2].last <= regcm_bound[class].last)) {
15683                                 result |= (1 << class2);
15684                         }
15685                 }
15686         }
15687         result &= arch_avail_mask(state);
15688         return result;
15689 }
15690
15691 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
15692 {
15693         /* Like arch_regcm_normalize except immediate register classes are excluded */
15694         regcm = arch_regcm_normalize(state, regcm);
15695         /* Remove the immediate register classes */
15696         regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
15697         return regcm;
15698         
15699 }
15700
15701 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
15702 {
15703         unsigned mask;
15704         int class;
15705         mask = 0;
15706         for(class = 0; class <= LAST_REGC; class++) {
15707                 if ((reg >= regcm_bound[class].first) &&
15708                         (reg <= regcm_bound[class].last)) {
15709                         mask |= (1 << class);
15710                 }
15711         }
15712         if (!mask) {
15713                 internal_error(state, 0, "reg %d not in any class", reg);
15714         }
15715         return mask;
15716 }
15717
15718 static struct reg_info arch_reg_constraint(
15719         struct compile_state *state, struct type *type, const char *constraint)
15720 {
15721         static const struct {
15722                 char class;
15723                 unsigned int mask;
15724                 unsigned int reg;
15725         } constraints[] = {
15726                 { 'r', REGCM_GPR32,   REG_UNSET },
15727                 { 'g', REGCM_GPR32,   REG_UNSET },
15728                 { 'p', REGCM_GPR32,   REG_UNSET },
15729                 { 'q', REGCM_GPR8_LO, REG_UNSET },
15730                 { 'Q', REGCM_GPR32_8, REG_UNSET },
15731                 { 'x', REGCM_XMM,     REG_UNSET },
15732                 { 'y', REGCM_MMX,     REG_UNSET },
15733                 { 'a', REGCM_GPR32,   REG_EAX },
15734                 { 'b', REGCM_GPR32,   REG_EBX },
15735                 { 'c', REGCM_GPR32,   REG_ECX },
15736                 { 'd', REGCM_GPR32,   REG_EDX },
15737                 { 'D', REGCM_GPR32,   REG_EDI },
15738                 { 'S', REGCM_GPR32,   REG_ESI },
15739                 { '\0', 0, REG_UNSET },
15740         };
15741         unsigned int regcm;
15742         unsigned int mask, reg;
15743         struct reg_info result;
15744         const char *ptr;
15745         regcm = arch_type_to_regcm(state, type);
15746         reg = REG_UNSET;
15747         mask = 0;
15748         for(ptr = constraint; *ptr; ptr++) {
15749                 int i;
15750                 if (*ptr ==  ' ') {
15751                         continue;
15752                 }
15753                 for(i = 0; constraints[i].class != '\0'; i++) {
15754                         if (constraints[i].class == *ptr) {
15755                                 break;
15756                         }
15757                 }
15758                 if (constraints[i].class == '\0') {
15759                         error(state, 0, "invalid register constraint ``%c''", *ptr);
15760                         break;
15761                 }
15762                 if ((constraints[i].mask & regcm) == 0) {
15763                         error(state, 0, "invalid register class %c specified",
15764                                 *ptr);
15765                 }
15766                 mask |= constraints[i].mask;
15767                 if (constraints[i].reg != REG_UNSET) {
15768                         if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
15769                                 error(state, 0, "Only one register may be specified");
15770                         }
15771                         reg = constraints[i].reg;
15772                 }
15773         }
15774         result.reg = reg;
15775         result.regcm = mask;
15776         return result;
15777 }
15778
15779 static struct reg_info arch_reg_clobber(
15780         struct compile_state *state, const char *clobber)
15781 {
15782         struct reg_info result;
15783         if (strcmp(clobber, "memory") == 0) {
15784                 result.reg = REG_UNSET;
15785                 result.regcm = 0;
15786         }
15787         else if (strcmp(clobber, "%eax") == 0) {
15788                 result.reg = REG_EAX;
15789                 result.regcm = REGCM_GPR32;
15790         }
15791         else if (strcmp(clobber, "%ebx") == 0) {
15792                 result.reg = REG_EBX;
15793                 result.regcm = REGCM_GPR32;
15794         }
15795         else if (strcmp(clobber, "%ecx") == 0) {
15796                 result.reg = REG_ECX;
15797                 result.regcm = REGCM_GPR32;
15798         }
15799         else if (strcmp(clobber, "%edx") == 0) {
15800                 result.reg = REG_EDX;
15801                 result.regcm = REGCM_GPR32;
15802         }
15803         else if (strcmp(clobber, "%esi") == 0) {
15804                 result.reg = REG_ESI;
15805                 result.regcm = REGCM_GPR32;
15806         }
15807         else if (strcmp(clobber, "%edi") == 0) {
15808                 result.reg = REG_EDI;
15809                 result.regcm = REGCM_GPR32;
15810         }
15811         else if (strcmp(clobber, "%ebp") == 0) {
15812                 result.reg = REG_EBP;
15813                 result.regcm = REGCM_GPR32;
15814         }
15815         else if (strcmp(clobber, "%esp") == 0) {
15816                 result.reg = REG_ESP;
15817                 result.regcm = REGCM_GPR32;
15818         }
15819         else if (strcmp(clobber, "cc") == 0) {
15820                 result.reg = REG_EFLAGS;
15821                 result.regcm = REGCM_FLAGS;
15822         }
15823         else if ((strncmp(clobber, "xmm", 3) == 0)  &&
15824                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15825                 result.reg = REG_XMM0 + octdigval(clobber[3]);
15826                 result.regcm = REGCM_XMM;
15827         }
15828         else if ((strncmp(clobber, "mmx", 3) == 0) &&
15829                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
15830                 result.reg = REG_MMX0 + octdigval(clobber[3]);
15831                 result.regcm = REGCM_MMX;
15832         }
15833         else {
15834                 error(state, 0, "Invalid register clobber");
15835                 result.reg = REG_UNSET;
15836                 result.regcm = 0;
15837         }
15838         return result;
15839 }
15840
15841 static int do_select_reg(struct compile_state *state, 
15842         char *used, int reg, unsigned classes)
15843 {
15844         unsigned mask;
15845         if (used[reg]) {
15846                 return REG_UNSET;
15847         }
15848         mask = arch_reg_regcm(state, reg);
15849         return (classes & mask) ? reg : REG_UNSET;
15850 }
15851
15852 static int arch_select_free_register(
15853         struct compile_state *state, char *used, int classes)
15854 {
15855         /* Live ranges with the most neighbors are colored first.
15856          *
15857          * Generally it does not matter which colors are given
15858          * as the register allocator attempts to color live ranges
15859          * in an order where you are guaranteed not to run out of colors.
15860          *
15861          * Occasionally the register allocator cannot find an order
15862          * of register selection that will find a free color.  To
15863          * increase the odds the register allocator will work when
15864          * it guesses first give out registers from register classes
15865          * least likely to run out of registers.
15866          * 
15867          */
15868         int i, reg;
15869         reg = REG_UNSET;
15870         for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
15871                 reg = do_select_reg(state, used, i, classes);
15872         }
15873         for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
15874                 reg = do_select_reg(state, used, i, classes);
15875         }
15876         for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
15877                 reg = do_select_reg(state, used, i, classes);
15878         }
15879         for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
15880                 reg = do_select_reg(state, used, i, classes);
15881         }
15882         for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
15883                 reg = do_select_reg(state, used, i, classes);
15884         }
15885         for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
15886                 reg = do_select_reg(state, used, i, classes);
15887         }
15888         for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
15889                 reg = do_select_reg(state, used, i, classes);
15890         }
15891         for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
15892                 reg = do_select_reg(state, used, i, classes);
15893         }
15894         for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
15895                 reg = do_select_reg(state, used, i, classes);
15896         }
15897         return reg;
15898 }
15899
15900
15901 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) 
15902 {
15903 #warning "FIXME force types smaller (if legal) before I get here"
15904         unsigned mask;
15905         mask = 0;
15906         switch(type->type & TYPE_MASK) {
15907         case TYPE_ARRAY:
15908         case TYPE_VOID: 
15909                 mask = 0; 
15910                 break;
15911         case TYPE_CHAR:
15912         case TYPE_UCHAR:
15913                 mask = REGCM_GPR8 | REGCM_GPR8_LO |
15914                         REGCM_GPR16 | REGCM_GPR16_8 | 
15915                         REGCM_GPR32 | REGCM_GPR32_8 |
15916                         REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15917                         REGCM_MMX | REGCM_XMM |
15918                         REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
15919                 break;
15920         case TYPE_SHORT:
15921         case TYPE_USHORT:
15922                 mask =  REGCM_GPR16 | REGCM_GPR16_8 |
15923                         REGCM_GPR32 | REGCM_GPR32_8 |
15924                         REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15925                         REGCM_MMX | REGCM_XMM |
15926                         REGCM_IMM32 | REGCM_IMM16;
15927                 break;
15928         case TYPE_INT:
15929         case TYPE_UINT:
15930         case TYPE_LONG:
15931         case TYPE_ULONG:
15932         case TYPE_POINTER:
15933                 mask =  REGCM_GPR32 | REGCM_GPR32_8 |
15934                         REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
15935                         REGCM_MMX | REGCM_XMM |
15936                         REGCM_IMM32;
15937                 break;
15938         default:
15939                 internal_error(state, 0, "no register class for type");
15940                 break;
15941         }
15942         mask = arch_regcm_normalize(state, mask);
15943         return mask;
15944 }
15945
15946 static int is_imm32(struct triple *imm)
15947 {
15948         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
15949                 (imm->op == OP_ADDRCONST);
15950         
15951 }
15952 static int is_imm16(struct triple *imm)
15953 {
15954         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
15955 }
15956 static int is_imm8(struct triple *imm)
15957 {
15958         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
15959 }
15960
15961 static int get_imm32(struct triple *ins, struct triple **expr)
15962 {
15963         struct triple *imm;
15964         imm = *expr;
15965         while(imm->op == OP_COPY) {
15966                 imm = RHS(imm, 0);
15967         }
15968         if (!is_imm32(imm)) {
15969                 return 0;
15970         }
15971         unuse_triple(*expr, ins);
15972         use_triple(imm, ins);
15973         *expr = imm;
15974         return 1;
15975 }
15976
15977 static int get_imm8(struct triple *ins, struct triple **expr)
15978 {
15979         struct triple *imm;
15980         imm = *expr;
15981         while(imm->op == OP_COPY) {
15982                 imm = RHS(imm, 0);
15983         }
15984         if (!is_imm8(imm)) {
15985                 return 0;
15986         }
15987         unuse_triple(*expr, ins);
15988         use_triple(imm, ins);
15989         *expr = imm;
15990         return 1;
15991 }
15992
15993 #define TEMPLATE_NOP           0
15994 #define TEMPLATE_INTCONST8     1
15995 #define TEMPLATE_INTCONST32    2
15996 #define TEMPLATE_COPY8_REG     3
15997 #define TEMPLATE_COPY16_REG    4
15998 #define TEMPLATE_COPY32_REG    5
15999 #define TEMPLATE_COPY_IMM8     6
16000 #define TEMPLATE_COPY_IMM16    7
16001 #define TEMPLATE_COPY_IMM32    8
16002 #define TEMPLATE_PHI8          9
16003 #define TEMPLATE_PHI16        10
16004 #define TEMPLATE_PHI32        11
16005 #define TEMPLATE_STORE8       12
16006 #define TEMPLATE_STORE16      13
16007 #define TEMPLATE_STORE32      14
16008 #define TEMPLATE_LOAD8        15
16009 #define TEMPLATE_LOAD16       16
16010 #define TEMPLATE_LOAD32       17
16011 #define TEMPLATE_BINARY8_REG  18
16012 #define TEMPLATE_BINARY16_REG 19
16013 #define TEMPLATE_BINARY32_REG 20
16014 #define TEMPLATE_BINARY8_IMM  21
16015 #define TEMPLATE_BINARY16_IMM 22
16016 #define TEMPLATE_BINARY32_IMM 23
16017 #define TEMPLATE_SL8_CL       24
16018 #define TEMPLATE_SL16_CL      25
16019 #define TEMPLATE_SL32_CL      26
16020 #define TEMPLATE_SL8_IMM      27
16021 #define TEMPLATE_SL16_IMM     28
16022 #define TEMPLATE_SL32_IMM     29
16023 #define TEMPLATE_UNARY8       30
16024 #define TEMPLATE_UNARY16      31
16025 #define TEMPLATE_UNARY32      32
16026 #define TEMPLATE_CMP8_REG     33
16027 #define TEMPLATE_CMP16_REG    34
16028 #define TEMPLATE_CMP32_REG    35
16029 #define TEMPLATE_CMP8_IMM     36
16030 #define TEMPLATE_CMP16_IMM    37
16031 #define TEMPLATE_CMP32_IMM    38
16032 #define TEMPLATE_TEST8        39
16033 #define TEMPLATE_TEST16       40
16034 #define TEMPLATE_TEST32       41
16035 #define TEMPLATE_SET          42
16036 #define TEMPLATE_JMP          43
16037 #define TEMPLATE_INB_DX       44
16038 #define TEMPLATE_INB_IMM      45
16039 #define TEMPLATE_INW_DX       46
16040 #define TEMPLATE_INW_IMM      47
16041 #define TEMPLATE_INL_DX       48
16042 #define TEMPLATE_INL_IMM      49
16043 #define TEMPLATE_OUTB_DX      50
16044 #define TEMPLATE_OUTB_IMM     51
16045 #define TEMPLATE_OUTW_DX      52
16046 #define TEMPLATE_OUTW_IMM     53
16047 #define TEMPLATE_OUTL_DX      54
16048 #define TEMPLATE_OUTL_IMM     55
16049 #define TEMPLATE_BSF          56
16050 #define TEMPLATE_RDMSR        57
16051 #define TEMPLATE_WRMSR        58
16052 #define TEMPLATE_UMUL8        59
16053 #define TEMPLATE_UMUL16       60
16054 #define TEMPLATE_UMUL32       61
16055 #define TEMPLATE_DIV8         62
16056 #define TEMPLATE_DIV16        63
16057 #define TEMPLATE_DIV32        64
16058 #define LAST_TEMPLATE       TEMPLATE_DIV32
16059 #if LAST_TEMPLATE >= MAX_TEMPLATES
16060 #error "MAX_TEMPLATES to low"
16061 #endif
16062
16063 #define COPY8_REGCM     (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
16064 #define COPY16_REGCM    (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)  
16065 #define COPY32_REGCM    (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
16066
16067
16068 static struct ins_template templates[] = {
16069         [TEMPLATE_NOP]      = {},
16070         [TEMPLATE_INTCONST8] = { 
16071                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16072         },
16073         [TEMPLATE_INTCONST32] = { 
16074                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
16075         },
16076         [TEMPLATE_COPY8_REG] = {
16077                 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
16078                 .rhs = { [0] = { REG_UNSET, COPY8_REGCM }  },
16079         },
16080         [TEMPLATE_COPY16_REG] = {
16081                 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16082                 .rhs = { [0] = { REG_UNSET, COPY16_REGCM }  },
16083         },
16084         [TEMPLATE_COPY32_REG] = {
16085                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
16086                 .rhs = { [0] = { REG_UNSET, COPY32_REGCM }  },
16087         },
16088         [TEMPLATE_COPY_IMM8] = {
16089                 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
16090                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16091         },
16092         [TEMPLATE_COPY_IMM16] = {
16093                 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
16094                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
16095         },
16096         [TEMPLATE_COPY_IMM32] = {
16097                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
16098                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
16099         },
16100         [TEMPLATE_PHI8] = { 
16101                 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
16102                 .rhs = { 
16103                         [ 0] = { REG_VIRT0, COPY8_REGCM },
16104                         [ 1] = { REG_VIRT0, COPY8_REGCM },
16105                         [ 2] = { REG_VIRT0, COPY8_REGCM },
16106                         [ 3] = { REG_VIRT0, COPY8_REGCM },
16107                         [ 4] = { REG_VIRT0, COPY8_REGCM },
16108                         [ 5] = { REG_VIRT0, COPY8_REGCM },
16109                         [ 6] = { REG_VIRT0, COPY8_REGCM },
16110                         [ 7] = { REG_VIRT0, COPY8_REGCM },
16111                         [ 8] = { REG_VIRT0, COPY8_REGCM },
16112                         [ 9] = { REG_VIRT0, COPY8_REGCM },
16113                         [10] = { REG_VIRT0, COPY8_REGCM },
16114                         [11] = { REG_VIRT0, COPY8_REGCM },
16115                         [12] = { REG_VIRT0, COPY8_REGCM },
16116                         [13] = { REG_VIRT0, COPY8_REGCM },
16117                         [14] = { REG_VIRT0, COPY8_REGCM },
16118                         [15] = { REG_VIRT0, COPY8_REGCM },
16119                 }, },
16120         [TEMPLATE_PHI16] = { 
16121                 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
16122                 .rhs = { 
16123                         [ 0] = { REG_VIRT0, COPY16_REGCM },
16124                         [ 1] = { REG_VIRT0, COPY16_REGCM },
16125                         [ 2] = { REG_VIRT0, COPY16_REGCM },
16126                         [ 3] = { REG_VIRT0, COPY16_REGCM },
16127                         [ 4] = { REG_VIRT0, COPY16_REGCM },
16128                         [ 5] = { REG_VIRT0, COPY16_REGCM },
16129                         [ 6] = { REG_VIRT0, COPY16_REGCM },
16130                         [ 7] = { REG_VIRT0, COPY16_REGCM },
16131                         [ 8] = { REG_VIRT0, COPY16_REGCM },
16132                         [ 9] = { REG_VIRT0, COPY16_REGCM },
16133                         [10] = { REG_VIRT0, COPY16_REGCM },
16134                         [11] = { REG_VIRT0, COPY16_REGCM },
16135                         [12] = { REG_VIRT0, COPY16_REGCM },
16136                         [13] = { REG_VIRT0, COPY16_REGCM },
16137                         [14] = { REG_VIRT0, COPY16_REGCM },
16138                         [15] = { REG_VIRT0, COPY16_REGCM },
16139                 }, },
16140         [TEMPLATE_PHI32] = { 
16141                 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
16142                 .rhs = { 
16143                         [ 0] = { REG_VIRT0, COPY32_REGCM },
16144                         [ 1] = { REG_VIRT0, COPY32_REGCM },
16145                         [ 2] = { REG_VIRT0, COPY32_REGCM },
16146                         [ 3] = { REG_VIRT0, COPY32_REGCM },
16147                         [ 4] = { REG_VIRT0, COPY32_REGCM },
16148                         [ 5] = { REG_VIRT0, COPY32_REGCM },
16149                         [ 6] = { REG_VIRT0, COPY32_REGCM },
16150                         [ 7] = { REG_VIRT0, COPY32_REGCM },
16151                         [ 8] = { REG_VIRT0, COPY32_REGCM },
16152                         [ 9] = { REG_VIRT0, COPY32_REGCM },
16153                         [10] = { REG_VIRT0, COPY32_REGCM },
16154                         [11] = { REG_VIRT0, COPY32_REGCM },
16155                         [12] = { REG_VIRT0, COPY32_REGCM },
16156                         [13] = { REG_VIRT0, COPY32_REGCM },
16157                         [14] = { REG_VIRT0, COPY32_REGCM },
16158                         [15] = { REG_VIRT0, COPY32_REGCM },
16159                 }, },
16160         [TEMPLATE_STORE8] = {
16161                 .rhs = { 
16162                         [0] = { REG_UNSET, REGCM_GPR32 },
16163                         [1] = { REG_UNSET, REGCM_GPR8_LO },
16164                 },
16165         },
16166         [TEMPLATE_STORE16] = {
16167                 .rhs = { 
16168                         [0] = { REG_UNSET, REGCM_GPR32 },
16169                         [1] = { REG_UNSET, REGCM_GPR16 },
16170                 },
16171         },
16172         [TEMPLATE_STORE32] = {
16173                 .rhs = { 
16174                         [0] = { REG_UNSET, REGCM_GPR32 },
16175                         [1] = { REG_UNSET, REGCM_GPR32 },
16176                 },
16177         },
16178         [TEMPLATE_LOAD8] = {
16179                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16180                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16181         },
16182         [TEMPLATE_LOAD16] = {
16183                 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16184                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16185         },
16186         [TEMPLATE_LOAD32] = {
16187                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16188                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16189         },
16190         [TEMPLATE_BINARY8_REG] = {
16191                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16192                 .rhs = { 
16193                         [0] = { REG_VIRT0, REGCM_GPR8_LO },
16194                         [1] = { REG_UNSET, REGCM_GPR8_LO },
16195                 },
16196         },
16197         [TEMPLATE_BINARY16_REG] = {
16198                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16199                 .rhs = { 
16200                         [0] = { REG_VIRT0, REGCM_GPR16 },
16201                         [1] = { REG_UNSET, REGCM_GPR16 },
16202                 },
16203         },
16204         [TEMPLATE_BINARY32_REG] = {
16205                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16206                 .rhs = { 
16207                         [0] = { REG_VIRT0, REGCM_GPR32 },
16208                         [1] = { REG_UNSET, REGCM_GPR32 },
16209                 },
16210         },
16211         [TEMPLATE_BINARY8_IMM] = {
16212                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16213                 .rhs = { 
16214                         [0] = { REG_VIRT0,    REGCM_GPR8_LO },
16215                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16216                 },
16217         },
16218         [TEMPLATE_BINARY16_IMM] = {
16219                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16220                 .rhs = { 
16221                         [0] = { REG_VIRT0,    REGCM_GPR16 },
16222                         [1] = { REG_UNNEEDED, REGCM_IMM16 },
16223                 },
16224         },
16225         [TEMPLATE_BINARY32_IMM] = {
16226                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16227                 .rhs = { 
16228                         [0] = { REG_VIRT0,    REGCM_GPR32 },
16229                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
16230                 },
16231         },
16232         [TEMPLATE_SL8_CL] = {
16233                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16234                 .rhs = { 
16235                         [0] = { REG_VIRT0, REGCM_GPR8_LO },
16236                         [1] = { REG_CL, REGCM_GPR8_LO },
16237                 },
16238         },
16239         [TEMPLATE_SL16_CL] = {
16240                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16241                 .rhs = { 
16242                         [0] = { REG_VIRT0, REGCM_GPR16 },
16243                         [1] = { REG_CL, REGCM_GPR8_LO },
16244                 },
16245         },
16246         [TEMPLATE_SL32_CL] = {
16247                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16248                 .rhs = { 
16249                         [0] = { REG_VIRT0, REGCM_GPR32 },
16250                         [1] = { REG_CL, REGCM_GPR8_LO },
16251                 },
16252         },
16253         [TEMPLATE_SL8_IMM] = {
16254                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16255                 .rhs = { 
16256                         [0] = { REG_VIRT0,    REGCM_GPR8_LO },
16257                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16258                 },
16259         },
16260         [TEMPLATE_SL16_IMM] = {
16261                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16262                 .rhs = { 
16263                         [0] = { REG_VIRT0,    REGCM_GPR16 },
16264                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16265                 },
16266         },
16267         [TEMPLATE_SL32_IMM] = {
16268                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16269                 .rhs = { 
16270                         [0] = { REG_VIRT0,    REGCM_GPR32 },
16271                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16272                 },
16273         },
16274         [TEMPLATE_UNARY8] = {
16275                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16276                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
16277         },
16278         [TEMPLATE_UNARY16] = {
16279                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16280                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
16281         },
16282         [TEMPLATE_UNARY32] = {
16283                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16284                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
16285         },
16286         [TEMPLATE_CMP8_REG] = {
16287                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16288                 .rhs = {
16289                         [0] = { REG_UNSET, REGCM_GPR8_LO },
16290                         [1] = { REG_UNSET, REGCM_GPR8_LO },
16291                 },
16292         },
16293         [TEMPLATE_CMP16_REG] = {
16294                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16295                 .rhs = {
16296                         [0] = { REG_UNSET, REGCM_GPR16 },
16297                         [1] = { REG_UNSET, REGCM_GPR16 },
16298                 },
16299         },
16300         [TEMPLATE_CMP32_REG] = {
16301                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16302                 .rhs = {
16303                         [0] = { REG_UNSET, REGCM_GPR32 },
16304                         [1] = { REG_UNSET, REGCM_GPR32 },
16305                 },
16306         },
16307         [TEMPLATE_CMP8_IMM] = {
16308                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16309                 .rhs = {
16310                         [0] = { REG_UNSET, REGCM_GPR8_LO },
16311                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16312                 },
16313         },
16314         [TEMPLATE_CMP16_IMM] = {
16315                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16316                 .rhs = {
16317                         [0] = { REG_UNSET, REGCM_GPR16 },
16318                         [1] = { REG_UNNEEDED, REGCM_IMM16 },
16319                 },
16320         },
16321         [TEMPLATE_CMP32_IMM] = {
16322                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16323                 .rhs = {
16324                         [0] = { REG_UNSET, REGCM_GPR32 },
16325                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
16326                 },
16327         },
16328         [TEMPLATE_TEST8] = {
16329                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16330                 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16331         },
16332         [TEMPLATE_TEST16] = {
16333                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16334                 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
16335         },
16336         [TEMPLATE_TEST32] = {
16337                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16338                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16339         },
16340         [TEMPLATE_SET] = {
16341                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
16342                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16343         },
16344         [TEMPLATE_JMP] = {
16345                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
16346         },
16347         [TEMPLATE_INB_DX] = {
16348                 .lhs = { [0] = { REG_AL,  REGCM_GPR8_LO } },  
16349                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16350         },
16351         [TEMPLATE_INB_IMM] = {
16352                 .lhs = { [0] = { REG_AL,  REGCM_GPR8_LO } },  
16353                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16354         },
16355         [TEMPLATE_INW_DX]  = { 
16356                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
16357                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16358         },
16359         [TEMPLATE_INW_IMM] = { 
16360                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
16361                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16362         },
16363         [TEMPLATE_INL_DX]  = {
16364                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16365                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
16366         },
16367         [TEMPLATE_INL_IMM] = {
16368                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
16369                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
16370         },
16371         [TEMPLATE_OUTB_DX] = { 
16372                 .rhs = {
16373                         [0] = { REG_AL,  REGCM_GPR8_LO },
16374                         [1] = { REG_DX, REGCM_GPR16 },
16375                 },
16376         },
16377         [TEMPLATE_OUTB_IMM] = { 
16378                 .rhs = {
16379                         [0] = { REG_AL,  REGCM_GPR8_LO },  
16380                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16381                 },
16382         },
16383         [TEMPLATE_OUTW_DX] = { 
16384                 .rhs = {
16385                         [0] = { REG_AX,  REGCM_GPR16 },
16386                         [1] = { REG_DX, REGCM_GPR16 },
16387                 },
16388         },
16389         [TEMPLATE_OUTW_IMM] = {
16390                 .rhs = {
16391                         [0] = { REG_AX,  REGCM_GPR16 }, 
16392                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16393                 },
16394         },
16395         [TEMPLATE_OUTL_DX] = { 
16396                 .rhs = {
16397                         [0] = { REG_EAX, REGCM_GPR32 },
16398                         [1] = { REG_DX, REGCM_GPR16 },
16399                 },
16400         },
16401         [TEMPLATE_OUTL_IMM] = { 
16402                 .rhs = {
16403                         [0] = { REG_EAX, REGCM_GPR32 }, 
16404                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
16405                 },
16406         },
16407         [TEMPLATE_BSF] = {
16408                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16409                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
16410         },
16411         [TEMPLATE_RDMSR] = {
16412                 .lhs = { 
16413                         [0] = { REG_EAX, REGCM_GPR32 },
16414                         [1] = { REG_EDX, REGCM_GPR32 },
16415                 },
16416                 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
16417         },
16418         [TEMPLATE_WRMSR] = {
16419                 .rhs = {
16420                         [0] = { REG_ECX, REGCM_GPR32 },
16421                         [1] = { REG_EAX, REGCM_GPR32 },
16422                         [2] = { REG_EDX, REGCM_GPR32 },
16423                 },
16424         },
16425         [TEMPLATE_UMUL8] = {
16426                 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
16427                 .rhs = { 
16428                         [0] = { REG_AL, REGCM_GPR8_LO },
16429                         [1] = { REG_UNSET, REGCM_GPR8_LO },
16430                 },
16431         },
16432         [TEMPLATE_UMUL16] = {
16433                 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
16434                 .rhs = { 
16435                         [0] = { REG_AX, REGCM_GPR16 },
16436                         [1] = { REG_UNSET, REGCM_GPR16 },
16437                 },
16438         },
16439         [TEMPLATE_UMUL32] = {
16440                 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
16441                 .rhs = { 
16442                         [0] = { REG_EAX, REGCM_GPR32 },
16443                         [1] = { REG_UNSET, REGCM_GPR32 },
16444                 },
16445         },
16446         [TEMPLATE_DIV8] = {
16447                 .lhs = { 
16448                         [0] = { REG_AL, REGCM_GPR8_LO },
16449                         [1] = { REG_AH, REGCM_GPR8 },
16450                 },
16451                 .rhs = {
16452                         [0] = { REG_AX, REGCM_GPR16 },
16453                         [1] = { REG_UNSET, REGCM_GPR8_LO },
16454                 },
16455         },
16456         [TEMPLATE_DIV16] = {
16457                 .lhs = { 
16458                         [0] = { REG_AX, REGCM_GPR16 },
16459                         [1] = { REG_DX, REGCM_GPR16 },
16460                 },
16461                 .rhs = {
16462                         [0] = { REG_DXAX, REGCM_DIVIDEND32 },
16463                         [1] = { REG_UNSET, REGCM_GPR16 },
16464                 },
16465         },
16466         [TEMPLATE_DIV32] = {
16467                 .lhs = { 
16468                         [0] = { REG_EAX, REGCM_GPR32 },
16469                         [1] = { REG_EDX, REGCM_GPR32 },
16470                 },
16471                 .rhs = {
16472                         [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
16473                         [1] = { REG_UNSET, REGCM_GPR32 },
16474                 },
16475         },
16476 };
16477
16478 static void fixup_branches(struct compile_state *state,
16479         struct triple *cmp, struct triple *use, int jmp_op)
16480 {
16481         struct triple_set *entry, *next;
16482         for(entry = use->use; entry; entry = next) {
16483                 next = entry->next;
16484                 if (entry->member->op == OP_COPY) {
16485                         fixup_branches(state, cmp, entry->member, jmp_op);
16486                 }
16487                 else if (entry->member->op == OP_BRANCH) {
16488                         struct triple *branch, *test;
16489                         struct triple *left, *right;
16490                         left = right = 0;
16491                         left = RHS(cmp, 0);
16492                         if (TRIPLE_RHS(cmp->sizes) > 1) {
16493                                 right = RHS(cmp, 1);
16494                         }
16495                         branch = entry->member;
16496                         test = pre_triple(state, branch,
16497                                 cmp->op, cmp->type, left, right);
16498                         test->template_id = TEMPLATE_TEST32; 
16499                         if (cmp->op == OP_CMP) {
16500                                 test->template_id = TEMPLATE_CMP32_REG;
16501                                 if (get_imm32(test, &RHS(test, 1))) {
16502                                         test->template_id = TEMPLATE_CMP32_IMM;
16503                                 }
16504                         }
16505                         use_triple(RHS(test, 0), test);
16506                         use_triple(RHS(test, 1), test);
16507                         unuse_triple(RHS(branch, 0), branch);
16508                         RHS(branch, 0) = test;
16509                         branch->op = jmp_op;
16510                         branch->template_id = TEMPLATE_JMP;
16511                         use_triple(RHS(branch, 0), branch);
16512                 }
16513         }
16514 }
16515
16516 static void bool_cmp(struct compile_state *state, 
16517         struct triple *ins, int cmp_op, int jmp_op, int set_op)
16518 {
16519         struct triple_set *entry, *next;
16520         struct triple *set;
16521
16522         /* Put a barrier up before the cmp which preceeds the
16523          * copy instruction.  If a set actually occurs this gives
16524          * us a chance to move variables in registers out of the way.
16525          */
16526
16527         /* Modify the comparison operator */
16528         ins->op = cmp_op;
16529         ins->template_id = TEMPLATE_TEST32;
16530         if (cmp_op == OP_CMP) {
16531                 ins->template_id = TEMPLATE_CMP32_REG;
16532                 if (get_imm32(ins, &RHS(ins, 1))) {
16533                         ins->template_id =  TEMPLATE_CMP32_IMM;
16534                 }
16535         }
16536         /* Generate the instruction sequence that will transform the
16537          * result of the comparison into a logical value.
16538          */
16539         set = post_triple(state, ins, set_op, &char_type, ins, 0);
16540         use_triple(ins, set);
16541         set->template_id = TEMPLATE_SET;
16542
16543         for(entry = ins->use; entry; entry = next) {
16544                 next = entry->next;
16545                 if (entry->member == set) {
16546                         continue;
16547                 }
16548                 replace_rhs_use(state, ins, set, entry->member);
16549         }
16550         fixup_branches(state, ins, set, jmp_op);
16551 }
16552
16553 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
16554 {
16555         struct triple *next;
16556         int lhs, i;
16557         lhs = TRIPLE_LHS(ins->sizes);
16558         for(next = ins->next, i = 0; i < lhs; i++, next = next->next) {
16559                 if (next != LHS(ins, i)) {
16560                         internal_error(state, ins, "malformed lhs on %s",
16561                                 tops(ins->op));
16562                 }
16563                 if (next->op != OP_PIECE) {
16564                         internal_error(state, ins, "bad lhs op %s at %d on %s",
16565                                 tops(next->op), i, tops(ins->op));
16566                 }
16567                 if (next->u.cval != i) {
16568                         internal_error(state, ins, "bad u.cval of %d %d expected",
16569                                 next->u.cval, i);
16570                 }
16571         }
16572         return next;
16573 }
16574
16575 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
16576 {
16577         struct ins_template *template;
16578         struct reg_info result;
16579         int zlhs;
16580         if (ins->op == OP_PIECE) {
16581                 index = ins->u.cval;
16582                 ins = MISC(ins, 0);
16583         }
16584         zlhs = TRIPLE_LHS(ins->sizes);
16585         if (triple_is_def(state, ins)) {
16586                 zlhs = 1;
16587         }
16588         if (index >= zlhs) {
16589                 internal_error(state, ins, "index %d out of range for %s\n",
16590                         index, tops(ins->op));
16591         }
16592         switch(ins->op) {
16593         case OP_ASM:
16594                 template = &ins->u.ainfo->tmpl;
16595                 break;
16596         default:
16597                 if (ins->template_id > LAST_TEMPLATE) {
16598                         internal_error(state, ins, "bad template number %d", 
16599                                 ins->template_id);
16600                 }
16601                 template = &templates[ins->template_id];
16602                 break;
16603         }
16604         result = template->lhs[index];
16605         result.regcm = arch_regcm_normalize(state, result.regcm);
16606         if (result.reg != REG_UNNEEDED) {
16607                 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
16608         }
16609         if (result.regcm == 0) {
16610                 internal_error(state, ins, "lhs %d regcm == 0", index);
16611         }
16612         return result;
16613 }
16614
16615 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
16616 {
16617         struct reg_info result;
16618         struct ins_template *template;
16619         if ((index > TRIPLE_RHS(ins->sizes)) ||
16620                 (ins->op == OP_PIECE)) {
16621                 internal_error(state, ins, "index %d out of range for %s\n",
16622                         index, tops(ins->op));
16623         }
16624         switch(ins->op) {
16625         case OP_ASM:
16626                 template = &ins->u.ainfo->tmpl;
16627                 break;
16628         default:
16629                 if (ins->template_id > LAST_TEMPLATE) {
16630                         internal_error(state, ins, "bad template number %d", 
16631                                 ins->template_id);
16632                 }
16633                 template = &templates[ins->template_id];
16634                 break;
16635         }
16636         result = template->rhs[index];
16637         result.regcm = arch_regcm_normalize(state, result.regcm);
16638         if (result.regcm == 0) {
16639                 internal_error(state, ins, "rhs %d regcm == 0", index);
16640         }
16641         return result;
16642 }
16643
16644 static struct triple *mod_div(struct compile_state *state,
16645         struct triple *ins, int div_op, int index)
16646 {
16647         struct triple *div, *piece0, *piece1;
16648         
16649         /* Generate a piece to hold the remainder */
16650         piece1 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
16651         piece1->u.cval = 1;
16652
16653         /* Generate a piece to hold the quotient */
16654         piece0 = post_triple(state, ins, OP_PIECE, ins->type, 0, 0);
16655         piece0->u.cval = 0;
16656
16657         /* Generate the appropriate division instruction */
16658         div = post_triple(state, ins, div_op, ins->type, 0, 0);
16659         RHS(div, 0) = RHS(ins, 0);
16660         RHS(div, 1) = RHS(ins, 1);
16661         LHS(div, 0) = piece0;
16662         LHS(div, 1) = piece1;
16663         div->template_id  = TEMPLATE_DIV32;
16664         use_triple(RHS(div, 0), div);
16665         use_triple(RHS(div, 1), div);
16666         use_triple(LHS(div, 0), div);
16667         use_triple(LHS(div, 1), div);
16668
16669         /* Hook on piece0 */
16670         MISC(piece0, 0) = div;
16671         use_triple(div, piece0);
16672
16673         /* Hook on piece1 */
16674         MISC(piece1, 0) = div;
16675         use_triple(div, piece1);
16676         
16677         /* Replate uses of ins with the appropriate piece of the div */
16678         propogate_use(state, ins, LHS(div, index));
16679         release_triple(state, ins);
16680
16681         /* Return the address of the next instruction */
16682         return piece1->next;
16683 }
16684
16685 static struct triple *transform_to_arch_instruction(
16686         struct compile_state *state, struct triple *ins)
16687 {
16688         /* Transform from generic 3 address instructions
16689          * to archtecture specific instructions.
16690          * And apply architecture specific constraints to instructions.
16691          * Copies are inserted to preserve the register flexibility
16692          * of 3 address instructions.
16693          */
16694         struct triple *next;
16695         size_t size;
16696         next = ins->next;
16697         switch(ins->op) {
16698         case OP_INTCONST:
16699                 ins->template_id = TEMPLATE_INTCONST32;
16700                 if (ins->u.cval < 256) {
16701                         ins->template_id = TEMPLATE_INTCONST8;
16702                 }
16703                 break;
16704         case OP_ADDRCONST:
16705                 ins->template_id = TEMPLATE_INTCONST32;
16706                 break;
16707         case OP_NOOP:
16708         case OP_SDECL:
16709         case OP_BLOBCONST:
16710         case OP_LABEL:
16711                 ins->template_id = TEMPLATE_NOP;
16712                 break;
16713         case OP_COPY:
16714                 size = size_of(state, ins->type);
16715                 if (is_imm8(RHS(ins, 0)) && (size <= 1)) {
16716                         ins->template_id = TEMPLATE_COPY_IMM8;
16717                 }
16718                 else if (is_imm16(RHS(ins, 0)) && (size <= 2)) {
16719                         ins->template_id = TEMPLATE_COPY_IMM16;
16720                 }
16721                 else if (is_imm32(RHS(ins, 0)) && (size <= 4)) {
16722                         ins->template_id = TEMPLATE_COPY_IMM32;
16723                 }
16724                 else if (is_const(RHS(ins, 0))) {
16725                         internal_error(state, ins, "bad constant passed to copy");
16726                 }
16727                 else if (size <= 1) {
16728                         ins->template_id = TEMPLATE_COPY8_REG;
16729                 }
16730                 else if (size <= 2) {
16731                         ins->template_id = TEMPLATE_COPY16_REG;
16732                 }
16733                 else if (size <= 4) {
16734                         ins->template_id = TEMPLATE_COPY32_REG;
16735                 }
16736                 else {
16737                         internal_error(state, ins, "bad type passed to copy");
16738                 }
16739                 break;
16740         case OP_PHI:
16741                 size = size_of(state, ins->type);
16742                 if (size <= 1) {
16743                         ins->template_id = TEMPLATE_PHI8;
16744                 }
16745                 else if (size <= 2) {
16746                         ins->template_id = TEMPLATE_PHI16;
16747                 }
16748                 else if (size <= 4) {
16749                         ins->template_id = TEMPLATE_PHI32;
16750                 }
16751                 else {
16752                         internal_error(state, ins, "bad type passed to phi");
16753                 }
16754                 break;
16755         case OP_STORE:
16756                 switch(ins->type->type & TYPE_MASK) {
16757                 case TYPE_CHAR:    case TYPE_UCHAR:
16758                         ins->template_id = TEMPLATE_STORE8;
16759                         break;
16760                 case TYPE_SHORT:   case TYPE_USHORT:
16761                         ins->template_id = TEMPLATE_STORE16;
16762                         break;
16763                 case TYPE_INT:     case TYPE_UINT:
16764                 case TYPE_LONG:    case TYPE_ULONG:
16765                 case TYPE_POINTER:
16766                         ins->template_id = TEMPLATE_STORE32;
16767                         break;
16768                 default:
16769                         internal_error(state, ins, "unknown type in store");
16770                         break;
16771                 }
16772                 break;
16773         case OP_LOAD:
16774                 switch(ins->type->type & TYPE_MASK) {
16775                 case TYPE_CHAR:   case TYPE_UCHAR:
16776                         ins->template_id = TEMPLATE_LOAD8;
16777                         break;
16778                 case TYPE_SHORT:
16779                 case TYPE_USHORT:
16780                         ins->template_id = TEMPLATE_LOAD16;
16781                         break;
16782                 case TYPE_INT:
16783                 case TYPE_UINT:
16784                 case TYPE_LONG:
16785                 case TYPE_ULONG:
16786                 case TYPE_POINTER:
16787                         ins->template_id = TEMPLATE_LOAD32;
16788                         break;
16789                 default:
16790                         internal_error(state, ins, "unknown type in load");
16791                         break;
16792                 }
16793                 break;
16794         case OP_ADD:
16795         case OP_SUB:
16796         case OP_AND:
16797         case OP_XOR:
16798         case OP_OR:
16799         case OP_SMUL:
16800                 ins->template_id = TEMPLATE_BINARY32_REG;
16801                 if (get_imm32(ins, &RHS(ins, 1))) {
16802                         ins->template_id = TEMPLATE_BINARY32_IMM;
16803                 }
16804                 break;
16805         case OP_SDIVT:
16806         case OP_UDIVT:
16807                 ins->template_id = TEMPLATE_DIV32;
16808                 next = after_lhs(state, ins);
16809                 break;
16810                 /* FIXME UMUL does not work yet.. */
16811         case OP_UMUL:
16812                 ins->template_id = TEMPLATE_UMUL32;
16813                 break;
16814         case OP_UDIV:
16815                 next = mod_div(state, ins, OP_UDIVT, 0);
16816                 break;
16817         case OP_SDIV:
16818                 next = mod_div(state, ins, OP_SDIVT, 0);
16819                 break;
16820         case OP_UMOD:
16821                 next = mod_div(state, ins, OP_UDIVT, 1);
16822                 break;
16823         case OP_SMOD:
16824                 next = mod_div(state, ins, OP_SDIVT, 1);
16825                 break;
16826         case OP_SL:
16827         case OP_SSR:
16828         case OP_USR:
16829                 ins->template_id = TEMPLATE_SL32_CL;
16830                 if (get_imm8(ins, &RHS(ins, 1))) {
16831                         ins->template_id = TEMPLATE_SL32_IMM;
16832                 } else if (size_of(state, RHS(ins, 1)->type) > 1) {
16833                         typed_pre_copy(state, &char_type, ins, 1);
16834                 }
16835                 break;
16836         case OP_INVERT:
16837         case OP_NEG:
16838                 ins->template_id = TEMPLATE_UNARY32;
16839                 break;
16840         case OP_EQ: 
16841                 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
16842                 break;
16843         case OP_NOTEQ:
16844                 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16845                 break;
16846         case OP_SLESS:
16847                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
16848                 break;
16849         case OP_ULESS:
16850                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
16851                 break;
16852         case OP_SMORE:
16853                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
16854                 break;
16855         case OP_UMORE:
16856                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
16857                 break;
16858         case OP_SLESSEQ:
16859                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
16860                 break;
16861         case OP_ULESSEQ:
16862                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
16863                 break;
16864         case OP_SMOREEQ:
16865                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
16866                 break;
16867         case OP_UMOREEQ:
16868                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
16869                 break;
16870         case OP_LTRUE:
16871                 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
16872                 break;
16873         case OP_LFALSE:
16874                 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
16875                 break;
16876         case OP_BRANCH:
16877                 if (TRIPLE_RHS(ins->sizes) > 0) {
16878                         internal_error(state, ins, "bad branch test");
16879                 }
16880                 ins->op = OP_JMP;
16881                 ins->template_id = TEMPLATE_NOP;
16882                 break;
16883         case OP_INB:
16884         case OP_INW:
16885         case OP_INL:
16886                 switch(ins->op) {
16887                 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
16888                 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
16889                 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
16890                 }
16891                 if (get_imm8(ins, &RHS(ins, 0))) {
16892                         ins->template_id += 1;
16893                 }
16894                 break;
16895         case OP_OUTB:
16896         case OP_OUTW:
16897         case OP_OUTL:
16898                 switch(ins->op) {
16899                 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
16900                 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
16901                 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
16902                 }
16903                 if (get_imm8(ins, &RHS(ins, 1))) {
16904                         ins->template_id += 1;
16905                 }
16906                 break;
16907         case OP_BSF:
16908         case OP_BSR:
16909                 ins->template_id = TEMPLATE_BSF;
16910                 break;
16911         case OP_RDMSR:
16912                 ins->template_id = TEMPLATE_RDMSR;
16913                 next = after_lhs(state, ins);
16914                 break;
16915         case OP_WRMSR:
16916                 ins->template_id = TEMPLATE_WRMSR;
16917                 break;
16918         case OP_HLT:
16919                 ins->template_id = TEMPLATE_NOP;
16920                 break;
16921         case OP_ASM:
16922                 ins->template_id = TEMPLATE_NOP;
16923                 next = after_lhs(state, ins);
16924                 break;
16925                 /* Already transformed instructions */
16926         case OP_TEST:
16927                 ins->template_id = TEMPLATE_TEST32;
16928                 break;
16929         case OP_CMP:
16930                 ins->template_id = TEMPLATE_CMP32_REG;
16931                 if (get_imm32(ins, &RHS(ins, 1))) {
16932                         ins->template_id = TEMPLATE_CMP32_IMM;
16933                 }
16934                 break;
16935         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
16936         case OP_JMP_SLESS:   case OP_JMP_ULESS:
16937         case OP_JMP_SMORE:   case OP_JMP_UMORE:
16938         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
16939         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
16940                 ins->template_id = TEMPLATE_JMP;
16941                 break;
16942         case OP_SET_EQ:      case OP_SET_NOTEQ:
16943         case OP_SET_SLESS:   case OP_SET_ULESS:
16944         case OP_SET_SMORE:   case OP_SET_UMORE:
16945         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
16946         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
16947                 ins->template_id = TEMPLATE_SET;
16948                 break;
16949                 /* Unhandled instructions */
16950         case OP_PIECE:
16951         default:
16952                 internal_error(state, ins, "unhandled ins: %d %s\n",
16953                         ins->op, tops(ins->op));
16954                 break;
16955         }
16956         return next;
16957 }
16958
16959 static long next_label(struct compile_state *state)
16960 {
16961         static long label_counter = 0;
16962         return ++label_counter;
16963 }
16964 static void generate_local_labels(struct compile_state *state)
16965 {
16966         struct triple *first, *label;
16967         first = RHS(state->main_function, 0);
16968         label = first;
16969         do {
16970                 if ((label->op == OP_LABEL) || 
16971                         (label->op == OP_SDECL)) {
16972                         if (label->use) {
16973                                 label->u.cval = next_label(state);
16974                         } else {
16975                                 label->u.cval = 0;
16976                         }
16977                         
16978                 }
16979                 label = label->next;
16980         } while(label != first);
16981 }
16982
16983 static int check_reg(struct compile_state *state, 
16984         struct triple *triple, int classes)
16985 {
16986         unsigned mask;
16987         int reg;
16988         reg = ID_REG(triple->id);
16989         if (reg == REG_UNSET) {
16990                 internal_error(state, triple, "register not set");
16991         }
16992         mask = arch_reg_regcm(state, reg);
16993         if (!(classes & mask)) {
16994                 internal_error(state, triple, "reg %d in wrong class",
16995                         reg);
16996         }
16997         return reg;
16998 }
16999
17000 static const char *arch_reg_str(int reg)
17001 {
17002 #if REG_XMM7 != 44
17003 #error "Registers have renumberd fix arch_reg_str"
17004 #endif
17005         static const char *regs[] = {
17006                 "%unset",
17007                 "%unneeded",
17008                 "%eflags",
17009                 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
17010                 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
17011                 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
17012                 "%edx:%eax",
17013                 "%dx:%ax",
17014                 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
17015                 "%xmm0", "%xmm1", "%xmm2", "%xmm3", 
17016                 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
17017         };
17018         if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
17019                 reg = 0;
17020         }
17021         return regs[reg];
17022 }
17023
17024
17025 static const char *reg(struct compile_state *state, struct triple *triple,
17026         int classes)
17027 {
17028         int reg;
17029         reg = check_reg(state, triple, classes);
17030         return arch_reg_str(reg);
17031 }
17032
17033 const char *type_suffix(struct compile_state *state, struct type *type)
17034 {
17035         const char *suffix;
17036         switch(size_of(state, type)) {
17037         case 1: suffix = "b"; break;
17038         case 2: suffix = "w"; break;
17039         case 4: suffix = "l"; break;
17040         default:
17041                 internal_error(state, 0, "unknown suffix");
17042                 suffix = 0;
17043                 break;
17044         }
17045         return suffix;
17046 }
17047
17048 static void print_const_val(
17049         struct compile_state *state, struct triple *ins, FILE *fp)
17050 {
17051         switch(ins->op) {
17052         case OP_INTCONST:
17053                 fprintf(fp, " $%ld ", 
17054                         (long_t)(ins->u.cval));
17055                 break;
17056         case OP_ADDRCONST:
17057                 if (MISC(ins, 0)->op != OP_SDECL) {
17058                         internal_error(state, ins, "bad base for addrconst");
17059                 }
17060                 if (MISC(ins, 0)->u.cval <= 0) {
17061                         internal_error(state, ins, "unlabeled constant");
17062                 }
17063                 fprintf(fp, " $L%s%lu+%lu ",
17064                         state->label_prefix, 
17065                         MISC(ins, 0)->u.cval,
17066                         ins->u.cval);
17067                 break;
17068         default:
17069                 internal_error(state, ins, "unknown constant type");
17070                 break;
17071         }
17072 }
17073
17074 static void print_const(struct compile_state *state,
17075         struct triple *ins, FILE *fp)
17076 {
17077         switch(ins->op) {
17078         case OP_INTCONST:
17079                 switch(ins->type->type & TYPE_MASK) {
17080                 case TYPE_CHAR:
17081                 case TYPE_UCHAR:
17082                         fprintf(fp, ".byte 0x%02lx\n", ins->u.cval);
17083                         break;
17084                 case TYPE_SHORT:
17085                 case TYPE_USHORT:
17086                         fprintf(fp, ".short 0x%04lx\n", ins->u.cval);
17087                         break;
17088                 case TYPE_INT:
17089                 case TYPE_UINT:
17090                 case TYPE_LONG:
17091                 case TYPE_ULONG:
17092                         fprintf(fp, ".int %lu\n", ins->u.cval);
17093                         break;
17094                 default:
17095                         internal_error(state, ins, "Unknown constant type");
17096                 }
17097                 break;
17098         case OP_ADDRCONST:
17099                 if (MISC(ins, 0)->op != OP_SDECL) {
17100                         internal_error(state, ins, "bad base for addrconst");
17101                 }
17102                 if (MISC(ins, 0)->u.cval <= 0) {
17103                         internal_error(state, ins, "unlabeled constant");
17104                 }
17105                 fprintf(fp, ".int L%s%lu+%lu\n",
17106                         state->label_prefix,
17107                         MISC(ins, 0)->u.cval,
17108                         ins->u.cval);
17109                 break;
17110         case OP_BLOBCONST:
17111         {
17112                 unsigned char *blob;
17113                 size_t size, i;
17114                 size = size_of(state, ins->type);
17115                 blob = ins->u.blob;
17116                 for(i = 0; i < size; i++) {
17117                         fprintf(fp, ".byte 0x%02x\n",
17118                                 blob[i]);
17119                 }
17120                 break;
17121         }
17122         default:
17123                 internal_error(state, ins, "Unknown constant type");
17124                 break;
17125         }
17126 }
17127
17128 #define TEXT_SECTION ".rom.text"
17129 #define DATA_SECTION ".rom.data"
17130
17131 static long get_const_pool_ref(
17132         struct compile_state *state, struct triple *ins, FILE *fp)
17133 {
17134         long ref;
17135         ref = next_label(state);
17136         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
17137         fprintf(fp, ".balign %d\n", align_of(state, ins->type));
17138         fprintf(fp, "L%s%lu:\n", state->label_prefix, ref);
17139         print_const(state, ins, fp);
17140         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17141         return ref;
17142 }
17143
17144 static void print_binary_op(struct compile_state *state,
17145         const char *op, struct triple *ins, FILE *fp) 
17146 {
17147         unsigned mask;
17148         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17149         if (RHS(ins, 0)->id != ins->id) {
17150                 internal_error(state, ins, "invalid register assignment");
17151         }
17152         if (is_const(RHS(ins, 1))) {
17153                 fprintf(fp, "\t%s ", op);
17154                 print_const_val(state, RHS(ins, 1), fp);
17155                 fprintf(fp, ", %s\n",
17156                         reg(state, RHS(ins, 0), mask));
17157         }
17158         else {
17159                 unsigned lmask, rmask;
17160                 int lreg, rreg;
17161                 lreg = check_reg(state, RHS(ins, 0), mask);
17162                 rreg = check_reg(state, RHS(ins, 1), mask);
17163                 lmask = arch_reg_regcm(state, lreg);
17164                 rmask = arch_reg_regcm(state, rreg);
17165                 mask = lmask & rmask;
17166                 fprintf(fp, "\t%s %s, %s\n",
17167                         op,
17168                         reg(state, RHS(ins, 1), mask),
17169                         reg(state, RHS(ins, 0), mask));
17170         }
17171 }
17172 static void print_unary_op(struct compile_state *state, 
17173         const char *op, struct triple *ins, FILE *fp)
17174 {
17175         unsigned mask;
17176         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17177         fprintf(fp, "\t%s %s\n",
17178                 op,
17179                 reg(state, RHS(ins, 0), mask));
17180 }
17181
17182 static void print_op_shift(struct compile_state *state,
17183         const char *op, struct triple *ins, FILE *fp)
17184 {
17185         unsigned mask;
17186         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17187         if (RHS(ins, 0)->id != ins->id) {
17188                 internal_error(state, ins, "invalid register assignment");
17189         }
17190         if (is_const(RHS(ins, 1))) {
17191                 fprintf(fp, "\t%s ", op);
17192                 print_const_val(state, RHS(ins, 1), fp);
17193                 fprintf(fp, ", %s\n",
17194                         reg(state, RHS(ins, 0), mask));
17195         }
17196         else {
17197                 fprintf(fp, "\t%s %s, %s\n",
17198                         op,
17199                         reg(state, RHS(ins, 1), REGCM_GPR8_LO),
17200                         reg(state, RHS(ins, 0), mask));
17201         }
17202 }
17203
17204 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
17205 {
17206         const char *op;
17207         int mask;
17208         int dreg;
17209         mask = 0;
17210         switch(ins->op) {
17211         case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
17212         case OP_INW: op = "inw", mask = REGCM_GPR16; break;
17213         case OP_INL: op = "inl", mask = REGCM_GPR32; break;
17214         default:
17215                 internal_error(state, ins, "not an in operation");
17216                 op = 0;
17217                 break;
17218         }
17219         dreg = check_reg(state, ins, mask);
17220         if (!reg_is_reg(state, dreg, REG_EAX)) {
17221                 internal_error(state, ins, "dst != %%eax");
17222         }
17223         if (is_const(RHS(ins, 0))) {
17224                 fprintf(fp, "\t%s ", op);
17225                 print_const_val(state, RHS(ins, 0), fp);
17226                 fprintf(fp, ", %s\n",
17227                         reg(state, ins, mask));
17228         }
17229         else {
17230                 int addr_reg;
17231                 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
17232                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
17233                         internal_error(state, ins, "src != %%dx");
17234                 }
17235                 fprintf(fp, "\t%s %s, %s\n",
17236                         op, 
17237                         reg(state, RHS(ins, 0), REGCM_GPR16),
17238                         reg(state, ins, mask));
17239         }
17240 }
17241
17242 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
17243 {
17244         const char *op;
17245         int mask;
17246         int lreg;
17247         mask = 0;
17248         switch(ins->op) {
17249         case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
17250         case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
17251         case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
17252         default:
17253                 internal_error(state, ins, "not an out operation");
17254                 op = 0;
17255                 break;
17256         }
17257         lreg = check_reg(state, RHS(ins, 0), mask);
17258         if (!reg_is_reg(state, lreg, REG_EAX)) {
17259                 internal_error(state, ins, "src != %%eax");
17260         }
17261         if (is_const(RHS(ins, 1))) {
17262                 fprintf(fp, "\t%s %s,", 
17263                         op, reg(state, RHS(ins, 0), mask));
17264                 print_const_val(state, RHS(ins, 1), fp);
17265                 fprintf(fp, "\n");
17266         }
17267         else {
17268                 int addr_reg;
17269                 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
17270                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
17271                         internal_error(state, ins, "dst != %%dx");
17272                 }
17273                 fprintf(fp, "\t%s %s, %s\n",
17274                         op, 
17275                         reg(state, RHS(ins, 0), mask),
17276                         reg(state, RHS(ins, 1), REGCM_GPR16));
17277         }
17278 }
17279
17280 static void print_op_move(struct compile_state *state,
17281         struct triple *ins, FILE *fp)
17282 {
17283         /* op_move is complex because there are many types
17284          * of registers we can move between.
17285          * Because OP_COPY will be introduced in arbitrary locations
17286          * OP_COPY must not affect flags.
17287          */
17288         int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
17289         struct triple *dst, *src;
17290         if (ins->op == OP_COPY) {
17291                 src = RHS(ins, 0);
17292                 dst = ins;
17293         }
17294         else {
17295                 internal_error(state, ins, "unknown move operation");
17296                 src = dst = 0;
17297         }
17298         if (!is_const(src)) {
17299                 int src_reg, dst_reg;
17300                 int src_regcm, dst_regcm;
17301                 src_reg   = ID_REG(src->id);
17302                 dst_reg   = ID_REG(dst->id);
17303                 src_regcm = arch_reg_regcm(state, src_reg);
17304                 dst_regcm = arch_reg_regcm(state, dst_reg);
17305                 /* If the class is the same just move the register */
17306                 if (src_regcm & dst_regcm & 
17307                         (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
17308                         if ((src_reg != dst_reg) || !omit_copy) {
17309                                 fprintf(fp, "\tmov %s, %s\n",
17310                                         reg(state, src, src_regcm),
17311                                         reg(state, dst, dst_regcm));
17312                         }
17313                 }
17314                 /* Move 32bit to 16bit */
17315                 else if ((src_regcm & REGCM_GPR32) &&
17316                         (dst_regcm & REGCM_GPR16)) {
17317                         src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
17318                         if ((src_reg != dst_reg) || !omit_copy) {
17319                                 fprintf(fp, "\tmovw %s, %s\n",
17320                                         arch_reg_str(src_reg), 
17321                                         arch_reg_str(dst_reg));
17322                         }
17323                 }
17324                 /* Move from 32bit gprs to 16bit gprs */
17325                 else if ((src_regcm & REGCM_GPR32) &&
17326                         (dst_regcm & REGCM_GPR16)) {
17327                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17328                         if ((src_reg != dst_reg) || !omit_copy) {
17329                                 fprintf(fp, "\tmov %s, %s\n",
17330                                         arch_reg_str(src_reg),
17331                                         arch_reg_str(dst_reg));
17332                         }
17333                 }
17334                 /* Move 32bit to 8bit */
17335                 else if ((src_regcm & REGCM_GPR32_8) &&
17336                         (dst_regcm & REGCM_GPR8_LO))
17337                 {
17338                         src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
17339                         if ((src_reg != dst_reg) || !omit_copy) {
17340                                 fprintf(fp, "\tmovb %s, %s\n",
17341                                         arch_reg_str(src_reg),
17342                                         arch_reg_str(dst_reg));
17343                         }
17344                 }
17345                 /* Move 16bit to 8bit */
17346                 else if ((src_regcm & REGCM_GPR16_8) &&
17347                         (dst_regcm & REGCM_GPR8_LO))
17348                 {
17349                         src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
17350                         if ((src_reg != dst_reg) || !omit_copy) {
17351                                 fprintf(fp, "\tmovb %s, %s\n",
17352                                         arch_reg_str(src_reg),
17353                                         arch_reg_str(dst_reg));
17354                         }
17355                 }
17356                 /* Move 8/16bit to 16/32bit */
17357                 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) && 
17358                         (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
17359                         const char *op;
17360                         op = is_signed(src->type)? "movsx": "movzx";
17361                         fprintf(fp, "\t%s %s, %s\n",
17362                                 op,
17363                                 reg(state, src, src_regcm),
17364                                 reg(state, dst, dst_regcm));
17365                 }
17366                 /* Move between sse registers */
17367                 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
17368                         if ((src_reg != dst_reg) || !omit_copy) {
17369                                 fprintf(fp, "\tmovdqa %s, %s\n",
17370                                         reg(state, src, src_regcm),
17371                                         reg(state, dst, dst_regcm));
17372                         }
17373                 }
17374                 /* Move between mmx registers */
17375                 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
17376                         if ((src_reg != dst_reg) || !omit_copy) {
17377                                 fprintf(fp, "\tmovq %s, %s\n",
17378                                         reg(state, src, src_regcm),
17379                                         reg(state, dst, dst_regcm));
17380                         }
17381                 }
17382                 /* Move from sse to mmx registers */
17383                 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
17384                         fprintf(fp, "\tmovdq2q %s, %s\n",
17385                                 reg(state, src, src_regcm),
17386                                 reg(state, dst, dst_regcm));
17387                 }
17388                 /* Move from mmx to sse registers */
17389                 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
17390                         fprintf(fp, "\tmovq2dq %s, %s\n",
17391                                 reg(state, src, src_regcm),
17392                                 reg(state, dst, dst_regcm));
17393                 }
17394                 /* Move between 32bit gprs & mmx/sse registers */
17395                 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
17396                         (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
17397                         fprintf(fp, "\tmovd %s, %s\n",
17398                                 reg(state, src, src_regcm),
17399                                 reg(state, dst, dst_regcm));
17400                 }
17401                 /* Move from 16bit gprs &  mmx/sse registers */
17402                 else if ((src_regcm & REGCM_GPR16) &&
17403                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
17404                         const char *op;
17405                         int mid_reg;
17406                         op = is_signed(src->type)? "movsx":"movzx";
17407                         mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17408                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
17409                                 op,
17410                                 arch_reg_str(src_reg),
17411                                 arch_reg_str(mid_reg),
17412                                 arch_reg_str(mid_reg),
17413                                 arch_reg_str(dst_reg));
17414                 }
17415                 /* Move from mmx/sse registers to 16bit gprs */
17416                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17417                         (dst_regcm & REGCM_GPR16)) {
17418                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
17419                         fprintf(fp, "\tmovd %s, %s\n",
17420                                 arch_reg_str(src_reg),
17421                                 arch_reg_str(dst_reg));
17422                 }
17423                 /* Move from gpr to 64bit dividend */
17424                 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))  &&
17425                         (dst_regcm & REGCM_DIVIDEND64)) {
17426                         const char *extend;
17427                         extend = is_signed(src->type)? "cltd":"movl $0, %edx";
17428                         fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
17429                                 arch_reg_str(src_reg), 
17430                                 extend);
17431                 }
17432                 /* Move from 64bit gpr to gpr */
17433                 else if ((src_regcm & REGCM_DIVIDEND64) &&
17434                         (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
17435                         if (dst_regcm & REGCM_GPR32) {
17436                                 src_reg = REG_EAX;
17437                         } 
17438                         else if (dst_regcm & REGCM_GPR16) {
17439                                 src_reg = REG_AX;
17440                         }
17441                         else if (dst_regcm & REGCM_GPR8_LO) {
17442                                 src_reg = REG_AL;
17443                         }
17444                         fprintf(fp, "\tmov %s, %s\n",
17445                                 arch_reg_str(src_reg),
17446                                 arch_reg_str(dst_reg));
17447                 }
17448                 /* Move from mmx/sse registers to 64bit gpr */
17449                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17450                         (dst_regcm & REGCM_DIVIDEND64)) {
17451                         const char *extend;
17452                         extend = is_signed(src->type)? "cltd": "movl $0, %edx";
17453                         fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
17454                                 arch_reg_str(src_reg),
17455                                 extend);
17456                 }
17457                 /* Move from 64bit gpr to mmx/sse register */
17458                 else if ((src_regcm & REGCM_DIVIDEND64) &&
17459                         (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
17460                         fprintf(fp, "\tmovd %%eax, %s\n",
17461                                 arch_reg_str(dst_reg));
17462                 }
17463 #if X86_4_8BIT_GPRS
17464                 /* Move from 8bit gprs to  mmx/sse registers */
17465                 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
17466                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
17467                         const char *op;
17468                         int mid_reg;
17469                         op = is_signed(src->type)? "movsx":"movzx";
17470                         mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17471                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
17472                                 op,
17473                                 reg(state, src, src_regcm),
17474                                 arch_reg_str(mid_reg),
17475                                 arch_reg_str(mid_reg),
17476                                 reg(state, dst, dst_regcm));
17477                 }
17478                 /* Move from mmx/sse registers and 8bit gprs */
17479                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
17480                         (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
17481                         int mid_reg;
17482                         mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17483                         fprintf(fp, "\tmovd %s, %s\n",
17484                                 reg(state, src, src_regcm),
17485                                 arch_reg_str(mid_reg));
17486                 }
17487                 /* Move from 32bit gprs to 8bit gprs */
17488                 else if ((src_regcm & REGCM_GPR32) &&
17489                         (dst_regcm & REGCM_GPR8_LO)) {
17490                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
17491                         if ((src_reg != dst_reg) || !omit_copy) {
17492                                 fprintf(fp, "\tmov %s, %s\n",
17493                                         arch_reg_str(src_reg),
17494                                         arch_reg_str(dst_reg));
17495                         }
17496                 }
17497                 /* Move from 16bit gprs to 8bit gprs */
17498                 else if ((src_regcm & REGCM_GPR16) &&
17499                         (dst_regcm & REGCM_GPR8_LO)) {
17500                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
17501                         if ((src_reg != dst_reg) || !omit_copy) {
17502                                 fprintf(fp, "\tmov %s, %s\n",
17503                                         arch_reg_str(src_reg),
17504                                         arch_reg_str(dst_reg));
17505                         }
17506                 }
17507 #endif /* X86_4_8BIT_GPRS */
17508                 else {
17509                         internal_error(state, ins, "unknown copy type");
17510                 }
17511         }
17512         else {
17513                 int dst_reg;
17514                 int dst_regcm;
17515                 dst_reg = ID_REG(dst->id);
17516                 dst_regcm = arch_reg_regcm(state, dst_reg);
17517                 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
17518                         fprintf(fp, "\tmov ");
17519                         print_const_val(state, src, fp);
17520                         fprintf(fp, ", %s\n",
17521                                 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
17522                 }
17523                 else if (dst_regcm & REGCM_DIVIDEND64) {
17524                         if (size_of(state, dst->type) > 4) {
17525                                 internal_error(state, ins, "64bit constant...");
17526                         }
17527                         fprintf(fp, "\tmov $0, %%edx\n");
17528                         fprintf(fp, "\tmov ");
17529                         print_const_val(state, src, fp);
17530                         fprintf(fp, ", %%eax\n");
17531                 }
17532                 else if (dst_regcm & REGCM_DIVIDEND32) {
17533                         if (size_of(state, dst->type) > 2) {
17534                                 internal_error(state, ins, "32bit constant...");
17535                         }
17536                         fprintf(fp, "\tmov $0, %%dx\n");
17537                         fprintf(fp, "\tmov ");
17538                         print_const_val(state, src, fp);
17539                         fprintf(fp, ", %%ax");
17540                 }
17541                 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
17542                         long ref;
17543                         ref = get_const_pool_ref(state, src, fp);
17544                         fprintf(fp, "\tmovq L%s%lu, %s\n",
17545                                 state->label_prefix, ref,
17546                                 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
17547                 }
17548                 else {
17549                         internal_error(state, ins, "unknown copy immediate type");
17550                 }
17551         }
17552 }
17553
17554 static void print_op_load(struct compile_state *state,
17555         struct triple *ins, FILE *fp)
17556 {
17557         struct triple *dst, *src;
17558         dst = ins;
17559         src = RHS(ins, 0);
17560         if (is_const(src) || is_const(dst)) {
17561                 internal_error(state, ins, "unknown load operation");
17562         }
17563         fprintf(fp, "\tmov (%s), %s\n",
17564                 reg(state, src, REGCM_GPR32),
17565                 reg(state, dst, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32));
17566 }
17567
17568
17569 static void print_op_store(struct compile_state *state,
17570         struct triple *ins, FILE *fp)
17571 {
17572         struct triple *dst, *src;
17573         dst = RHS(ins, 0);
17574         src = RHS(ins, 1);
17575         if (is_const(src) && (src->op == OP_INTCONST)) {
17576                 long_t value;
17577                 value = (long_t)(src->u.cval);
17578                 fprintf(fp, "\tmov%s $%ld, (%s)\n",
17579                         type_suffix(state, src->type),
17580                         value,
17581                         reg(state, dst, REGCM_GPR32));
17582         }
17583         else if (is_const(dst) && (dst->op == OP_INTCONST)) {
17584                 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
17585                         type_suffix(state, src->type),
17586                         reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
17587                         dst->u.cval);
17588         }
17589         else {
17590                 if (is_const(src) || is_const(dst)) {
17591                         internal_error(state, ins, "unknown store operation");
17592                 }
17593                 fprintf(fp, "\tmov%s %s, (%s)\n",
17594                         type_suffix(state, src->type),
17595                         reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
17596                         reg(state, dst, REGCM_GPR32));
17597         }
17598         
17599         
17600 }
17601
17602 static void print_op_smul(struct compile_state *state,
17603         struct triple *ins, FILE *fp)
17604 {
17605         if (!is_const(RHS(ins, 1))) {
17606                 fprintf(fp, "\timul %s, %s\n",
17607                         reg(state, RHS(ins, 1), REGCM_GPR32),
17608                         reg(state, RHS(ins, 0), REGCM_GPR32));
17609         }
17610         else {
17611                 fprintf(fp, "\timul ");
17612                 print_const_val(state, RHS(ins, 1), fp);
17613                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
17614         }
17615 }
17616
17617 static void print_op_cmp(struct compile_state *state,
17618         struct triple *ins, FILE *fp)
17619 {
17620         unsigned mask;
17621         int dreg;
17622         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17623         dreg = check_reg(state, ins, REGCM_FLAGS);
17624         if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
17625                 internal_error(state, ins, "bad dest register for cmp");
17626         }
17627         if (is_const(RHS(ins, 1))) {
17628                 fprintf(fp, "\tcmp ");
17629                 print_const_val(state, RHS(ins, 1), fp);
17630                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
17631         }
17632         else {
17633                 unsigned lmask, rmask;
17634                 int lreg, rreg;
17635                 lreg = check_reg(state, RHS(ins, 0), mask);
17636                 rreg = check_reg(state, RHS(ins, 1), mask);
17637                 lmask = arch_reg_regcm(state, lreg);
17638                 rmask = arch_reg_regcm(state, rreg);
17639                 mask = lmask & rmask;
17640                 fprintf(fp, "\tcmp %s, %s\n",
17641                         reg(state, RHS(ins, 1), mask),
17642                         reg(state, RHS(ins, 0), mask));
17643         }
17644 }
17645
17646 static void print_op_test(struct compile_state *state,
17647         struct triple *ins, FILE *fp)
17648 {
17649         unsigned mask;
17650         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
17651         fprintf(fp, "\ttest %s, %s\n",
17652                 reg(state, RHS(ins, 0), mask),
17653                 reg(state, RHS(ins, 0), mask));
17654 }
17655
17656 static void print_op_branch(struct compile_state *state,
17657         struct triple *branch, FILE *fp)
17658 {
17659         const char *bop = "j";
17660         if (branch->op == OP_JMP) {
17661                 if (TRIPLE_RHS(branch->sizes) != 0) {
17662                         internal_error(state, branch, "jmp with condition?");
17663                 }
17664                 bop = "jmp";
17665         }
17666         else {
17667                 struct triple *ptr;
17668                 if (TRIPLE_RHS(branch->sizes) != 1) {
17669                         internal_error(state, branch, "jmpcc without condition?");
17670                 }
17671                 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
17672                 if ((RHS(branch, 0)->op != OP_CMP) &&
17673                         (RHS(branch, 0)->op != OP_TEST)) {
17674                         internal_error(state, branch, "bad branch test");
17675                 }
17676 #warning "FIXME I have observed instructions between the test and branch instructions"
17677                 ptr = RHS(branch, 0);
17678                 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
17679                         if (ptr->op != OP_COPY) {
17680                                 internal_error(state, branch, "branch does not follow test");
17681                         }
17682                 }
17683                 switch(branch->op) {
17684                 case OP_JMP_EQ:       bop = "jz";  break;
17685                 case OP_JMP_NOTEQ:    bop = "jnz"; break;
17686                 case OP_JMP_SLESS:    bop = "jl";  break;
17687                 case OP_JMP_ULESS:    bop = "jb";  break;
17688                 case OP_JMP_SMORE:    bop = "jg";  break;
17689                 case OP_JMP_UMORE:    bop = "ja";  break;
17690                 case OP_JMP_SLESSEQ:  bop = "jle"; break;
17691                 case OP_JMP_ULESSEQ:  bop = "jbe"; break;
17692                 case OP_JMP_SMOREEQ:  bop = "jge"; break;
17693                 case OP_JMP_UMOREEQ:  bop = "jae"; break;
17694                 default:
17695                         internal_error(state, branch, "Invalid branch op");
17696                         break;
17697                 }
17698                 
17699         }
17700         fprintf(fp, "\t%s L%s%lu\n",
17701                 bop, 
17702                 state->label_prefix,
17703                 TARG(branch, 0)->u.cval);
17704 }
17705
17706 static void print_op_set(struct compile_state *state,
17707         struct triple *set, FILE *fp)
17708 {
17709         const char *sop = "set";
17710         if (TRIPLE_RHS(set->sizes) != 1) {
17711                 internal_error(state, set, "setcc without condition?");
17712         }
17713         check_reg(state, RHS(set, 0), REGCM_FLAGS);
17714         if ((RHS(set, 0)->op != OP_CMP) &&
17715                 (RHS(set, 0)->op != OP_TEST)) {
17716                 internal_error(state, set, "bad set test");
17717         }
17718         if (RHS(set, 0)->next != set) {
17719                 internal_error(state, set, "set does not follow test");
17720         }
17721         switch(set->op) {
17722         case OP_SET_EQ:       sop = "setz";  break;
17723         case OP_SET_NOTEQ:    sop = "setnz"; break;
17724         case OP_SET_SLESS:    sop = "setl";  break;
17725         case OP_SET_ULESS:    sop = "setb";  break;
17726         case OP_SET_SMORE:    sop = "setg";  break;
17727         case OP_SET_UMORE:    sop = "seta";  break;
17728         case OP_SET_SLESSEQ:  sop = "setle"; break;
17729         case OP_SET_ULESSEQ:  sop = "setbe"; break;
17730         case OP_SET_SMOREEQ:  sop = "setge"; break;
17731         case OP_SET_UMOREEQ:  sop = "setae"; break;
17732         default:
17733                 internal_error(state, set, "Invalid set op");
17734                 break;
17735         }
17736         fprintf(fp, "\t%s %s\n",
17737                 sop, reg(state, set, REGCM_GPR8_LO));
17738 }
17739
17740 static void print_op_bit_scan(struct compile_state *state, 
17741         struct triple *ins, FILE *fp) 
17742 {
17743         const char *op;
17744         switch(ins->op) {
17745         case OP_BSF: op = "bsf"; break;
17746         case OP_BSR: op = "bsr"; break;
17747         default: 
17748                 internal_error(state, ins, "unknown bit scan");
17749                 op = 0;
17750                 break;
17751         }
17752         fprintf(fp, 
17753                 "\t%s %s, %s\n"
17754                 "\tjnz 1f\n"
17755                 "\tmovl $-1, %s\n"
17756                 "1:\n",
17757                 op,
17758                 reg(state, RHS(ins, 0), REGCM_GPR32),
17759                 reg(state, ins, REGCM_GPR32),
17760                 reg(state, ins, REGCM_GPR32));
17761 }
17762
17763
17764 static void print_sdecl(struct compile_state *state,
17765         struct triple *ins, FILE *fp)
17766 {
17767         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
17768         fprintf(fp, ".balign %d\n", align_of(state, ins->type));
17769         fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
17770         print_const(state, MISC(ins, 0), fp);
17771         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17772                 
17773 }
17774
17775 static void print_instruction(struct compile_state *state,
17776         struct triple *ins, FILE *fp)
17777 {
17778         /* Assumption: after I have exted the register allocator
17779          * everything is in a valid register. 
17780          */
17781         switch(ins->op) {
17782         case OP_ASM:
17783                 print_op_asm(state, ins, fp);
17784                 break;
17785         case OP_ADD:    print_binary_op(state, "add", ins, fp); break;
17786         case OP_SUB:    print_binary_op(state, "sub", ins, fp); break;
17787         case OP_AND:    print_binary_op(state, "and", ins, fp); break;
17788         case OP_XOR:    print_binary_op(state, "xor", ins, fp); break;
17789         case OP_OR:     print_binary_op(state, "or",  ins, fp); break;
17790         case OP_SL:     print_op_shift(state, "shl", ins, fp); break;
17791         case OP_USR:    print_op_shift(state, "shr", ins, fp); break;
17792         case OP_SSR:    print_op_shift(state, "sar", ins, fp); break;
17793         case OP_POS:    break;
17794         case OP_NEG:    print_unary_op(state, "neg", ins, fp); break;
17795         case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
17796         case OP_INTCONST:
17797         case OP_ADDRCONST:
17798         case OP_BLOBCONST:
17799                 /* Don't generate anything here for constants */
17800         case OP_PHI:
17801                 /* Don't generate anything for variable declarations. */
17802                 break;
17803         case OP_SDECL:
17804                 print_sdecl(state, ins, fp);
17805                 break;
17806         case OP_COPY:   
17807                 print_op_move(state, ins, fp);
17808                 break;
17809         case OP_LOAD:
17810                 print_op_load(state, ins, fp);
17811                 break;
17812         case OP_STORE:
17813                 print_op_store(state, ins, fp);
17814                 break;
17815         case OP_SMUL:
17816                 print_op_smul(state, ins, fp);
17817                 break;
17818         case OP_CMP:    print_op_cmp(state, ins, fp); break;
17819         case OP_TEST:   print_op_test(state, ins, fp); break;
17820         case OP_JMP:
17821         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
17822         case OP_JMP_SLESS:   case OP_JMP_ULESS:
17823         case OP_JMP_SMORE:   case OP_JMP_UMORE:
17824         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
17825         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
17826                 print_op_branch(state, ins, fp);
17827                 break;
17828         case OP_SET_EQ:      case OP_SET_NOTEQ:
17829         case OP_SET_SLESS:   case OP_SET_ULESS:
17830         case OP_SET_SMORE:   case OP_SET_UMORE:
17831         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
17832         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
17833                 print_op_set(state, ins, fp);
17834                 break;
17835         case OP_INB:  case OP_INW:  case OP_INL:
17836                 print_op_in(state, ins, fp); 
17837                 break;
17838         case OP_OUTB: case OP_OUTW: case OP_OUTL:
17839                 print_op_out(state, ins, fp); 
17840                 break;
17841         case OP_BSF:
17842         case OP_BSR:
17843                 print_op_bit_scan(state, ins, fp);
17844                 break;
17845         case OP_RDMSR:
17846                 after_lhs(state, ins);
17847                 fprintf(fp, "\trdmsr\n");
17848                 break;
17849         case OP_WRMSR:
17850                 fprintf(fp, "\twrmsr\n");
17851                 break;
17852         case OP_HLT:
17853                 fprintf(fp, "\thlt\n");
17854                 break;
17855         case OP_SDIVT:
17856                 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17857                 break;
17858         case OP_UDIVT:
17859                 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17860                 break;
17861         case OP_UMUL:
17862                 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
17863                 break;
17864         case OP_LABEL:
17865                 if (!ins->use) {
17866                         return;
17867                 }
17868                 fprintf(fp, "L%s%lu:\n", state->label_prefix, ins->u.cval);
17869                 break;
17870                 /* Ignore OP_PIECE */
17871         case OP_PIECE:
17872                 break;
17873                 /* Operations that should never get here */
17874         case OP_SDIV: case OP_UDIV:
17875         case OP_SMOD: case OP_UMOD:
17876         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
17877         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
17878         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
17879         default:
17880                 internal_error(state, ins, "unknown op: %d %s",
17881                         ins->op, tops(ins->op));
17882                 break;
17883         }
17884 }
17885
17886 static void print_instructions(struct compile_state *state)
17887 {
17888         struct triple *first, *ins;
17889         int print_location;
17890         struct occurance *last_occurance;
17891         FILE *fp;
17892         int max_inline_depth;
17893         max_inline_depth = 0;
17894         print_location = 1;
17895         last_occurance = 0;
17896         fp = state->output;
17897         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
17898         first = RHS(state->main_function, 0);
17899         ins = first;
17900         do {
17901                 if (print_location && 
17902                         last_occurance != ins->occurance) {
17903                         if (!ins->occurance->parent) {
17904                                 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
17905                                         ins->occurance->function,
17906                                         ins->occurance->filename,
17907                                         ins->occurance->line,
17908                                         ins->occurance->col);
17909                         }
17910                         else {
17911                                 struct occurance *ptr;
17912                                 int inline_depth;
17913                                 fprintf(fp, "\t/*\n");
17914                                 inline_depth = 0;
17915                                 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
17916                                         inline_depth++;
17917                                         fprintf(fp, "\t * %s,%s:%d.%d\n",
17918                                                 ptr->function,
17919                                                 ptr->filename,
17920                                                 ptr->line,
17921                                                 ptr->col);
17922                                 }
17923                                 fprintf(fp, "\t */\n");
17924                                 if (inline_depth > max_inline_depth) {
17925                                         max_inline_depth = inline_depth;
17926                                 }
17927                         }
17928                         if (last_occurance) {
17929                                 put_occurance(last_occurance);
17930                         }
17931                         get_occurance(ins->occurance);
17932                         last_occurance = ins->occurance;
17933                 }
17934
17935                 print_instruction(state, ins, fp);
17936                 ins = ins->next;
17937         } while(ins != first);
17938         if (print_location) {
17939                 fprintf(fp, "/* max inline depth %d */\n",
17940                         max_inline_depth);
17941         }
17942 }
17943
17944 static void generate_code(struct compile_state *state)
17945 {
17946         generate_local_labels(state);
17947         print_instructions(state);
17948         
17949 }
17950
17951 static void print_tokens(struct compile_state *state)
17952 {
17953         struct token *tk;
17954         tk = &state->token[0];
17955         do {
17956 #if 1
17957                 token(state, 0);
17958 #else
17959                 next_token(state, 0);
17960 #endif
17961                 loc(stdout, state, 0);
17962                 printf("%s <- `%s'\n",
17963                         tokens[tk->tok],
17964                         tk->ident ? tk->ident->name :
17965                         tk->str_len ? tk->val.str : "");
17966                 
17967         } while(tk->tok != TOK_EOF);
17968 }
17969
17970 static void compile(const char *filename, const char *ofilename, 
17971         int cpu, int debug, int opt, const char *label_prefix)
17972 {
17973         int i;
17974         struct compile_state state;
17975         memset(&state, 0, sizeof(state));
17976         state.file = 0;
17977         for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
17978                 memset(&state.token[i], 0, sizeof(state.token[i]));
17979                 state.token[i].tok = -1;
17980         }
17981         /* Remember the debug settings */
17982         state.cpu      = cpu;
17983         state.debug    = debug;
17984         state.optimize = opt;
17985         /* Remember the output filename */
17986         state.ofilename = ofilename;
17987         state.output    = fopen(state.ofilename, "w");
17988         if (!state.output) {
17989                 error(&state, 0, "Cannot open output file %s\n",
17990                         ofilename);
17991         }
17992         /* Remember the label prefix */
17993         state.label_prefix = label_prefix;
17994         /* Prep the preprocessor */
17995         state.if_depth = 0;
17996         state.if_value = 0;
17997         /* register the C keywords */
17998         register_keywords(&state);
17999         /* register the keywords the macro preprocessor knows */
18000         register_macro_keywords(&state);
18001         /* Memorize where some special keywords are. */
18002         state.i_continue = lookup(&state, "continue", 8);
18003         state.i_break    = lookup(&state, "break", 5);
18004         /* Enter the globl definition scope */
18005         start_scope(&state);
18006         register_builtins(&state);
18007         compile_file(&state, filename, 1);
18008 #if 0
18009         print_tokens(&state);
18010 #endif  
18011         decls(&state);
18012         /* Exit the global definition scope */
18013         end_scope(&state);
18014
18015         /* Now that basic compilation has happened 
18016          * optimize the intermediate code 
18017          */
18018         optimize(&state);
18019
18020         generate_code(&state);
18021         if (state.debug) {
18022                 fprintf(stderr, "done\n");
18023         }
18024 }
18025
18026 static void version(void)
18027 {
18028         printf("romcc " VERSION " released " RELEASE_DATE "\n");
18029 }
18030
18031 static void usage(void)
18032 {
18033         version();
18034         printf(
18035                 "Usage: romcc <source>.c\n"
18036                 "Compile a C source file without using ram\n"
18037         );
18038 }
18039
18040 static void arg_error(char *fmt, ...)
18041 {
18042         va_list args;
18043         va_start(args, fmt);
18044         vfprintf(stderr, fmt, args);
18045         va_end(args);
18046         usage();
18047         exit(1);
18048 }
18049
18050 int main(int argc, char **argv)
18051 {
18052         const char *filename;
18053         const char *ofilename;
18054         const char *label_prefix;
18055         int cpu;
18056         int last_argc;
18057         int debug;
18058         int optimize;
18059         cpu = CPU_DEFAULT;
18060         label_prefix = "";
18061         ofilename = "auto.inc";
18062         optimize = 0;
18063         debug = 0;
18064         last_argc = -1;
18065         while((argc > 1) && (argc != last_argc)) {
18066                 last_argc = argc;
18067                 if (strncmp(argv[1], "--debug=", 8) == 0) {
18068                         debug = atoi(argv[1] + 8);
18069                         argv++;
18070                         argc--;
18071                 }
18072                 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
18073                         label_prefix= argv[1] + 15;
18074                         argv++;
18075                         argc--;
18076                 }
18077                 else if ((strcmp(argv[1],"-O") == 0) ||
18078                         (strcmp(argv[1], "-O1") == 0)) {
18079                         optimize = 1;
18080                         argv++;
18081                         argc--;
18082                 }
18083                 else if (strcmp(argv[1],"-O2") == 0) {
18084                         optimize = 2;
18085                         argv++;
18086                         argc--;
18087                 }
18088                 else if ((strcmp(argv[1], "-o") == 0) && (argc > 2)) {
18089                         ofilename = argv[2];
18090                         argv += 2;
18091                         argc -= 2;
18092                 }
18093                 else if (strncmp(argv[1], "-mcpu=", 6) == 0) {
18094                         cpu = arch_encode_cpu(argv[1] + 6);
18095                         if (cpu == BAD_CPU) {
18096                                 arg_error("Invalid cpu specified: %s\n",
18097                                         argv[1] + 6);
18098                         }
18099                         argv++;
18100                         argc--;
18101                 }
18102         }
18103         if (argc != 2) {
18104                 arg_error("Wrong argument count %d\n", argc);
18105         }
18106         filename = argv[1];
18107         compile(filename, ofilename, cpu, debug, optimize, label_prefix);
18108
18109         return 0;
18110 }