- Romcc preprocessor bug fixes, (The code size went down about 350 lines.. :)
[coreboot.git] / util / romcc / romcc.c
1 #undef VERSION_MAJOR
2 #undef VERSION_MINOR
3 #undef RELEASE_DATE
4 #undef VERSION
5 #define VERSION_MAJOR "0"
6 #define VERSION_MINOR "65"
7 #define RELEASE_DATE "8 November 2004"
8 #define VERSION VERSION_MAJOR "." VERSION_MINOR
9
10 #include <stdarg.h>
11 #include <errno.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <locale.h>
23 #include <time.h>
24
25 #define MAX_CWD_SIZE 4096
26 #define MAX_ALLOCATION_PASSES 100
27
28 #define DEBUG_CONSISTENCY 1
29 #define DEBUG_SDP_BLOCKS 0
30 #define DEBUG_TRIPLE_COLOR 0
31
32 #define DEBUG_DISPLAY_USES 1
33 #define DEBUG_DISPLAY_TYPES 1
34 #define DEBUG_REPLACE_CLOSURE_TYPE_HIRES 0
35 #define DEBUG_DECOMPOSE_PRINT_TUPLES 0
36 #define DEBUG_DECOMPOSE_HIRES  0
37 #define DEBUG_INITIALIZER 0
38 #define DEBUG_UPDATE_CLOSURE_TYPE 0
39 #define DEBUG_LOCAL_TRIPLE 0
40 #define DEBUG_BASIC_BLOCKS_VERBOSE 0
41 #define DEBUG_CPS_RENAME_VARIABLES_HIRES 0
42 #define DEBUG_SIMPLIFY_HIRES 0
43 #define DEBUG_SHRINKING 0
44 #define DEBUG_COALESCE_HITCHES 0
45 #define DEBUG_CODE_ELIMINATION 0
46
47 #define DEBUG_EXPLICIT_CLOSURES 0
48
49 #warning "FIXME give clear error messages about unused variables"
50 #warning "FIXME properly handle multi dimensional arrays"
51 #warning "FIXME handle multiple register sizes"
52
53 /*  Control flow graph of a loop without goto.
54  * 
55  *        AAA
56  *   +---/
57  *  /
58  * / +--->CCC
59  * | |    / \
60  * | |  DDD EEE    break;
61  * | |    \    \
62  * | |    FFF   \
63  *  \|    / \    \
64  *   |\ GGG HHH   |   continue;
65  *   | \  \   |   |
66  *   |  \ III |  /
67  *   |   \ | /  / 
68  *   |    vvv  /  
69  *   +----BBB /   
70  *         | /
71  *         vv
72  *        JJJ
73  *
74  * 
75  *             AAA
76  *     +-----+  |  +----+
77  *     |      \ | /     |
78  *     |       BBB  +-+ |
79  *     |       / \ /  | |
80  *     |     CCC JJJ / /
81  *     |     / \    / / 
82  *     |   DDD EEE / /  
83  *     |    |   +-/ /
84  *     |   FFF     /    
85  *     |   / \    /     
86  *     | GGG HHH /      
87  *     |  |   +-/
88  *     | III
89  *     +--+ 
90  *
91  * 
92  * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
93  * DFup(Z)    = { Y <- DF(Z) | idom(Y) != X }
94  *
95  *
96  * [] == DFlocal(X) U DF(X)
97  * () == DFup(X)
98  *
99  * Dominator graph of the same nodes.
100  *
101  *           AAA     AAA: [ ] ()
102  *          /   \
103  *        BBB    JJJ BBB: [ JJJ ] ( JJJ )  JJJ: [ ] ()
104  *         |
105  *        CCC        CCC: [ ] ( BBB, JJJ )
106  *        / \
107  *     DDD   EEE     DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
108  *      |
109  *     FFF           FFF: [ ] ( BBB )
110  *     / \         
111  *  GGG   HHH        GGG: [ ] ( BBB ) HHH: [ BBB ] ()
112  *   |
113  *  III              III: [ BBB ] ()
114  *
115  *
116  * BBB and JJJ are definitely the dominance frontier.
117  * Where do I place phi functions and how do I make that decision.
118  *   
119  */
120 static void die(char *fmt, ...)
121 {
122         va_list args;
123
124         va_start(args, fmt);
125         vfprintf(stderr, fmt, args);
126         va_end(args);
127         fflush(stdout);
128         fflush(stderr);
129         exit(1);
130 }
131
132 static void *xmalloc(size_t size, const char *name)
133 {
134         void *buf;
135         buf = malloc(size);
136         if (!buf) {
137                 die("Cannot malloc %ld bytes to hold %s: %s\n",
138                         size + 0UL, name, strerror(errno));
139         }
140         return buf;
141 }
142
143 static void *xcmalloc(size_t size, const char *name)
144 {
145         void *buf;
146         buf = xmalloc(size, name);
147         memset(buf, 0, size);
148         return buf;
149 }
150
151 static void *xrealloc(void *ptr, size_t size, const char *name)
152 {
153         void *buf;
154         buf = realloc(ptr, size);
155         if (!buf) {
156                 die("Cannot realloc %ld bytes to hold %s: %s\n",
157                         size + 0UL, name, strerror(errno));
158         }
159         return buf;
160 }
161
162 static void xfree(const void *ptr)
163 {
164         free((void *)ptr);
165 }
166
167 static char *xstrdup(const char *str)
168 {
169         char *new;
170         int len;
171         len = strlen(str);
172         new = xmalloc(len + 1, "xstrdup string");
173         memcpy(new, str, len);
174         new[len] = '\0';
175         return new;
176 }
177
178 static void xchdir(const char *path)
179 {
180         if (chdir(path) != 0) {
181                 die("chdir to `%s' failed: %s\n",
182                         path, strerror(errno));
183         }
184 }
185
186 static int exists(const char *dirname, const char *filename)
187 {
188         char cwd[MAX_CWD_SIZE];
189         int does_exist;
190
191         if (getcwd(cwd, sizeof(cwd)) == 0) {
192                 die("cwd buffer to small");
193         }
194
195         does_exist = 1;
196         if (chdir(dirname) != 0) {
197                 does_exist = 0;
198         }
199         if (does_exist && (access(filename, O_RDONLY) < 0)) {
200                 if ((errno != EACCES) && (errno != EROFS)) {
201                         does_exist = 0;
202                 }
203         }
204         xchdir(cwd);
205         return does_exist;
206 }
207
208
209 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
210 {
211         char cwd[MAX_CWD_SIZE];
212         int fd;
213         char *buf;
214         off_t size, progress;
215         ssize_t result;
216         struct stat stats;
217         
218         if (!filename) {
219                 *r_size = 0;
220                 return 0;
221         }
222         if (getcwd(cwd, sizeof(cwd)) == 0) {
223                 die("cwd buffer to small");
224         }
225         xchdir(dirname);
226         fd = open(filename, O_RDONLY);
227         xchdir(cwd);
228         if (fd < 0) {
229                 die("Cannot open '%s' : %s\n",
230                         filename, strerror(errno));
231         }
232         result = fstat(fd, &stats);
233         if (result < 0) {
234                 die("Cannot stat: %s: %s\n",
235                         filename, strerror(errno));
236         }
237         size = stats.st_size;
238         *r_size = size +1;
239         buf = xmalloc(size +2, filename);
240         buf[size] = '\n'; /* Make certain the file is newline terminated */
241         buf[size+1] = '\0'; /* Null terminate the file for good measure */
242         progress = 0;
243         while(progress < size) {
244                 result = read(fd, buf + progress, size - progress);
245                 if (result < 0) {
246                         if ((errno == EINTR) || (errno == EAGAIN))
247                                 continue;
248                         die("read on %s of %ld bytes failed: %s\n",
249                                 filename, (size - progress)+ 0UL, strerror(errno));
250                 }
251                 progress += result;
252         }
253         result = close(fd);
254         if (result < 0) {
255                 die("Close of %s failed: %s\n",
256                         filename, strerror(errno));
257         }
258         return buf;
259 }
260
261 /* Types on the destination platform */
262 #warning "FIXME this assumes 32bit x86 is the destination"
263 typedef int8_t   schar_t;
264 typedef uint8_t  uchar_t;
265 typedef int8_t   char_t;
266 typedef int16_t  short_t;
267 typedef uint16_t ushort_t;
268 typedef int32_t  int_t;
269 typedef uint32_t uint_t;
270 typedef int32_t  long_t;
271 typedef uint32_t ulong_t;
272
273 #define SCHAR_T_MIN (-128)
274 #define SCHAR_T_MAX 127
275 #define UCHAR_T_MAX 255
276 #define CHAR_T_MIN  SCHAR_T_MIN
277 #define CHAR_T_MAX  SCHAR_T_MAX
278 #define SHRT_T_MIN  (-32768)
279 #define SHRT_T_MAX  32767
280 #define USHRT_T_MAX 65535
281 #define INT_T_MIN   (-LONG_T_MAX - 1)
282 #define INT_T_MAX   2147483647
283 #define UINT_T_MAX  4294967295U
284 #define LONG_T_MIN  (-LONG_T_MAX - 1)
285 #define LONG_T_MAX  2147483647
286 #define ULONG_T_MAX 4294967295U
287
288 #define SIZEOF_I8    8
289 #define SIZEOF_I16   16
290 #define SIZEOF_I32   32
291 #define SIZEOF_I64   64
292
293 #define SIZEOF_CHAR    8
294 #define SIZEOF_SHORT   16
295 #define SIZEOF_INT     32
296 #define SIZEOF_LONG    (sizeof(long_t)*SIZEOF_CHAR)
297
298
299 #define ALIGNOF_CHAR    8
300 #define ALIGNOF_SHORT   16
301 #define ALIGNOF_INT     32
302 #define ALIGNOF_LONG    (sizeof(long_t)*SIZEOF_CHAR)
303
304 #define REG_SIZEOF_REG     32
305 #define REG_SIZEOF_CHAR    REG_SIZEOF_REG
306 #define REG_SIZEOF_SHORT   REG_SIZEOF_REG
307 #define REG_SIZEOF_INT     REG_SIZEOF_REG
308 #define REG_SIZEOF_LONG    REG_SIZEOF_REG
309
310 #define REG_ALIGNOF_REG     REG_SIZEOF_REG
311 #define REG_ALIGNOF_CHAR    REG_SIZEOF_REG
312 #define REG_ALIGNOF_SHORT   REG_SIZEOF_REG
313 #define REG_ALIGNOF_INT     REG_SIZEOF_REG
314 #define REG_ALIGNOF_LONG    REG_SIZEOF_REG
315
316 /* Additional definitions for clarity.
317  * I currently assume a long is the largest native
318  * machine word and that a pointer fits into it.
319  */
320 #define SIZEOF_WORD     SIZEOF_LONG
321 #define SIZEOF_POINTER  SIZEOF_LONG
322 #define ALIGNOF_WORD    ALIGNOF_LONG
323 #define ALIGNOF_POINTER ALIGNOF_LONG
324 #define REG_SIZEOF_POINTER  REG_SIZEOF_LONG
325 #define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG
326
327 struct file_state {
328         struct file_state *prev;
329         const char *basename;
330         char *dirname;
331         char *buf;
332         off_t size;
333         const char *pos;
334         int line;
335         const char *line_start;
336         int report_line;
337         const char *report_name;
338         const char *report_dir;
339 };
340 struct hash_entry;
341 struct token {
342         int tok;
343         struct hash_entry *ident;
344         int str_len;
345         union {
346                 ulong_t integer;
347                 const char *str;
348                 int notmacro;
349         } val;
350 };
351
352 /* I have two classes of types:
353  * Operational types.
354  * Logical types.  (The type the C standard says the operation is of)
355  *
356  * The operational types are:
357  * chars
358  * shorts
359  * ints
360  * longs
361  *
362  * floats
363  * doubles
364  * long doubles
365  *
366  * pointer
367  */
368
369
370 /* Machine model.
371  * No memory is useable by the compiler.
372  * There is no floating point support.
373  * All operations take place in general purpose registers.
374  * There is one type of general purpose register.
375  * Unsigned longs are stored in that general purpose register.
376  */
377
378 /* Operations on general purpose registers.
379  */
380
381 #define OP_SDIVT      0
382 #define OP_UDIVT      1
383 #define OP_SMUL       2
384 #define OP_UMUL       3
385 #define OP_SDIV       4
386 #define OP_UDIV       5
387 #define OP_SMOD       6
388 #define OP_UMOD       7
389 #define OP_ADD        8
390 #define OP_SUB        9
391 #define OP_SL        10
392 #define OP_USR       11
393 #define OP_SSR       12 
394 #define OP_AND       13 
395 #define OP_XOR       14
396 #define OP_OR        15
397 #define OP_POS       16 /* Dummy positive operator don't use it */
398 #define OP_NEG       17
399 #define OP_INVERT    18
400                      
401 #define OP_EQ        20
402 #define OP_NOTEQ     21
403 #define OP_SLESS     22
404 #define OP_ULESS     23
405 #define OP_SMORE     24
406 #define OP_UMORE     25
407 #define OP_SLESSEQ   26
408 #define OP_ULESSEQ   27
409 #define OP_SMOREEQ   28
410 #define OP_UMOREEQ   29
411                      
412 #define OP_LFALSE    30  /* Test if the expression is logically false */
413 #define OP_LTRUE     31  /* Test if the expression is logcially true */
414
415 #define OP_LOAD      32
416 #define OP_STORE     33
417 /* For OP_STORE ->type holds the type
418  * RHS(0) holds the destination address
419  * RHS(1) holds the value to store.
420  */
421
422 #define OP_UEXTRACT  34
423 /* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register
424  * RHS(0) holds the psuedo register to extract from
425  * ->type holds the size of the bitfield.
426  * ->u.bitfield.size holds the size of the bitfield.
427  * ->u.bitfield.offset holds the offset to extract from
428  */
429 #define OP_SEXTRACT  35
430 /* OP_SEXTRACT extracts a signed bitfield from a pseudo register
431  * RHS(0) holds the psuedo register to extract from
432  * ->type holds the size of the bitfield.
433  * ->u.bitfield.size holds the size of the bitfield.
434  * ->u.bitfield.offset holds the offset to extract from
435  */
436 #define OP_DEPOSIT   36
437 /* OP_DEPOSIT replaces a bitfield with a new value.
438  * RHS(0) holds the value to replace a bitifield in.
439  * RHS(1) holds the replacement value
440  * ->u.bitfield.size holds the size of the bitfield.
441  * ->u.bitfield.offset holds the deposit into
442  */
443
444 #define OP_NOOP      37
445
446 #define OP_MIN_CONST 50
447 #define OP_MAX_CONST 58
448 #define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
449 #define OP_INTCONST  50
450 /* For OP_INTCONST ->type holds the type.
451  * ->u.cval holds the constant value.
452  */
453 #define OP_BLOBCONST 51
454 /* For OP_BLOBCONST ->type holds the layout and size
455  * information.  u.blob holds a pointer to the raw binary
456  * data for the constant initializer.
457  */
458 #define OP_ADDRCONST 52
459 /* For OP_ADDRCONST ->type holds the type.
460  * MISC(0) holds the reference to the static variable.
461  * ->u.cval holds an offset from that value.
462  */
463 #define OP_UNKNOWNVAL 59
464 /* For OP_UNKNOWNAL ->type holds the type.
465  * For some reason we don't know what value this type has.
466  * This allows for variables that have don't have values
467  * assigned yet, or variables whose value we simply do not know.
468  */
469
470 #define OP_WRITE     60 
471 /* OP_WRITE moves one pseudo register to another.
472  * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
473  * RHS(0) holds the psuedo to move.
474  */
475
476 #define OP_READ      61
477 /* OP_READ reads the value of a variable and makes
478  * it available for the pseudo operation.
479  * Useful for things like def-use chains.
480  * RHS(0) holds points to the triple to read from.
481  */
482 #define OP_COPY      62
483 /* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
484  */
485 #define OP_CONVERT   63
486 /* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0).
487  * And then the type is converted appropriately.
488  */
489 #define OP_PIECE     64
490 /* OP_PIECE returns one piece of a instruction that returns a structure.
491  * MISC(0) is the instruction
492  * u.cval is the LHS piece of the instruction to return.
493  */
494 #define OP_ASM       65
495 /* OP_ASM holds a sequence of assembly instructions, the result
496  * of a C asm directive.
497  * RHS(x) holds input value x to the assembly sequence.
498  * LHS(x) holds the output value x from the assembly sequence.
499  * u.blob holds the string of assembly instructions.
500  */
501
502 #define OP_DEREF     66
503 /* OP_DEREF generates an lvalue from a pointer.
504  * RHS(0) holds the pointer value.
505  * OP_DEREF serves as a place holder to indicate all necessary
506  * checks have been done to indicate a value is an lvalue.
507  */
508 #define OP_DOT       67
509 /* OP_DOT references a submember of a structure lvalue.
510  * MISC(0) holds the lvalue.
511  * ->u.field holds the name of the field we want.
512  *
513  * Not seen after structures are flattened.
514  */
515 #define OP_INDEX     68
516 /* OP_INDEX references a submember of a tuple or array lvalue.
517  * MISC(0) holds the lvalue.
518  * ->u.cval holds the index into the lvalue.
519  *
520  * Not seen after structures are flattened.
521  */
522 #define OP_VAL       69
523 /* OP_VAL returns the value of a subexpression of the current expression.
524  * Useful for operators that have side effects.
525  * RHS(0) holds the expression.
526  * MISC(0) holds the subexpression of RHS(0) that is the
527  * value of the expression.
528  *
529  * Not seen outside of expressions.
530  */
531
532 #define OP_TUPLE     70
533 /* OP_TUPLE is an array of triples that are either variable
534  * or values for a structure or an array.  It is used as
535  * a place holder when flattening compound types.
536  * The value represented by an OP_TUPLE is held in N registers.
537  * LHS(0..N-1) refer to those registers.
538  * ->use is a list of statements that use the value.
539  * 
540  * Although OP_TUPLE always has register sized pieces they are not
541  * used until structures are flattened/decomposed into their register
542  * components. 
543  * ???? registers ????
544  */
545
546 #define OP_BITREF    71
547 /* OP_BITREF describes a bitfield as an lvalue.
548  * RHS(0) holds the register value.
549  * ->type holds the type of the bitfield.
550  * ->u.bitfield.size holds the size of the bitfield.
551  * ->u.bitfield.offset holds the offset of the bitfield in the register
552  */
553
554
555 #define OP_FCALL     72
556 /* OP_FCALL performs a procedure call. 
557  * MISC(0) holds a pointer to the OP_LIST of a function
558  * RHS(x) holds argument x of a function
559  * 
560  * Currently not seen outside of expressions.
561  */
562 #define OP_PROG      73
563 /* OP_PROG is an expression that holds a list of statements, or
564  * expressions.  The final expression is the value of the expression.
565  * RHS(0) holds the start of the list.
566  */
567
568 /* statements */
569 #define OP_LIST      80
570 /* OP_LIST Holds a list of statements that compose a function, and a result value.
571  * RHS(0) holds the list of statements.
572  * A list of all functions is maintained.
573  */
574
575 #define OP_BRANCH    81 /* an unconditional branch */
576 /* For branch instructions
577  * TARG(0) holds the branch target.
578  * ->next holds where to branch to if the branch is not taken.
579  * The branch target can only be a label
580  */
581
582 #define OP_CBRANCH   82 /* a conditional branch */
583 /* For conditional branch instructions
584  * RHS(0) holds the branch condition.
585  * TARG(0) holds the branch target.
586  * ->next holds where to branch to if the branch is not taken.
587  * The branch target can only be a label
588  */
589
590 #define OP_CALL      83 /* an uncontional branch that will return */
591 /* For call instructions
592  * MISC(0) holds the OP_RET that returns from the branch
593  * TARG(0) holds the branch target.
594  * ->next holds where to branch to if the branch is not taken.
595  * The branch target can only be a label
596  */
597
598 #define OP_RET       84 /* an uncontinonal branch through a variable back to an OP_CALL */
599 /* For call instructions
600  * RHS(0) holds the variable with the return address
601  * The branch target can only be a label
602  */
603
604 #define OP_LABEL     86
605 /* OP_LABEL is a triple that establishes an target for branches.
606  * ->use is the list of all branches that use this label.
607  */
608
609 #define OP_ADECL     87 
610 /* OP_ADECL is a triple that establishes an lvalue for assignments.
611  * A variable takes N registers to contain.
612  * LHS(0..N-1) refer to an OP_PIECE triple that represents
613  * the Xth register that the variable is stored in.
614  * ->use is a list of statements that use the variable.
615  * 
616  * Although OP_ADECL always has register sized pieces they are not
617  * used until structures are flattened/decomposed into their register
618  * components. 
619  */
620
621 #define OP_SDECL     88
622 /* OP_SDECL is a triple that establishes a variable of static
623  * storage duration.
624  * ->use is a list of statements that use the variable.
625  * MISC(0) holds the initializer expression.
626  */
627
628
629 #define OP_PHI       89
630 /* OP_PHI is a triple used in SSA form code.  
631  * It is used when multiple code paths merge and a variable needs
632  * a single assignment from any of those code paths.
633  * The operation is a cross between OP_DECL and OP_WRITE, which
634  * is what OP_PHI is generated from.
635  * 
636  * RHS(x) points to the value from code path x
637  * The number of RHS entries is the number of control paths into the block
638  * in which OP_PHI resides.  The elements of the array point to point
639  * to the variables OP_PHI is derived from.
640  *
641  * MISC(0) holds a pointer to the orginal OP_DECL node.
642  */
643
644 #if 0
645 /* continuation helpers
646  */
647 #define OP_CPS_BRANCH    90 /* an unconditional branch */
648 /* OP_CPS_BRANCH calls a continuation 
649  * RHS(x) holds argument x of the function
650  * TARG(0) holds OP_CPS_START target
651  */
652 #define OP_CPS_CBRANCH   91  /* a conditional branch */
653 /* OP_CPS_CBRANCH conditionally calls one of two continuations 
654  * RHS(0) holds the branch condition
655  * RHS(x + 1) holds argument x of the function
656  * TARG(0) holds the OP_CPS_START to jump to when true
657  * ->next holds the OP_CPS_START to jump to when false
658  */
659 #define OP_CPS_CALL      92  /* an uncontional branch that will return */
660 /* For OP_CPS_CALL instructions
661  * RHS(x) holds argument x of the function
662  * MISC(0) holds the OP_CPS_RET that returns from the branch
663  * TARG(0) holds the branch target.
664  * ->next holds where the OP_CPS_RET will return to.
665  */
666 #define OP_CPS_RET       93
667 /* OP_CPS_RET conditionally calls one of two continuations 
668  * RHS(0) holds the variable with the return function address
669  * RHS(x + 1) holds argument x of the function
670  * The branch target may be any OP_CPS_START
671  */
672 #define OP_CPS_END       94
673 /* OP_CPS_END is the triple at the end of the program.
674  * For most practical purposes it is a branch.
675  */
676 #define OP_CPS_START     95
677 /* OP_CPS_START is a triple at the start of a continuation
678  * The arguments variables takes N registers to contain.
679  * LHS(0..N-1) refer to an OP_PIECE triple that represents
680  * the Xth register that the arguments are stored in.
681  */
682 #endif
683
684 /* Architecture specific instructions */
685 #define OP_CMP         100
686 #define OP_TEST        101
687 #define OP_SET_EQ      102
688 #define OP_SET_NOTEQ   103
689 #define OP_SET_SLESS   104
690 #define OP_SET_ULESS   105
691 #define OP_SET_SMORE   106
692 #define OP_SET_UMORE   107
693 #define OP_SET_SLESSEQ 108
694 #define OP_SET_ULESSEQ 109
695 #define OP_SET_SMOREEQ 110
696 #define OP_SET_UMOREEQ 111
697
698 #define OP_JMP         112
699 #define OP_JMP_EQ      113
700 #define OP_JMP_NOTEQ   114
701 #define OP_JMP_SLESS   115
702 #define OP_JMP_ULESS   116
703 #define OP_JMP_SMORE   117
704 #define OP_JMP_UMORE   118
705 #define OP_JMP_SLESSEQ 119
706 #define OP_JMP_ULESSEQ 120
707 #define OP_JMP_SMOREEQ 121
708 #define OP_JMP_UMOREEQ 122
709
710 /* Builtin operators that it is just simpler to use the compiler for */
711 #define OP_INB         130
712 #define OP_INW         131
713 #define OP_INL         132
714 #define OP_OUTB        133
715 #define OP_OUTW        134
716 #define OP_OUTL        135
717 #define OP_BSF         136
718 #define OP_BSR         137
719 #define OP_RDMSR       138
720 #define OP_WRMSR       139
721 #define OP_HLT         140
722
723 struct op_info {
724         const char *name;
725         unsigned flags;
726 #define PURE       0x001 /* Triple has no side effects */
727 #define IMPURE     0x002 /* Triple has side effects */
728 #define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
729 #define DEF        0x004 /* Triple is a variable definition */
730 #define BLOCK      0x008 /* Triple stores the current block */
731 #define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */
732 #define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 )
733 #define UBRANCH    0x020 /* Triple is an unconditional branch instruction */
734 #define CBRANCH    0x040 /* Triple is a conditional branch instruction */
735 #define RETBRANCH  0x060 /* Triple is a return instruction */
736 #define CALLBRANCH 0x080 /* Triple is a call instruction */
737 #define ENDBRANCH  0x0a0 /* Triple is an end instruction */
738 #define PART       0x100 /* Triple is really part of another triple */
739 #define BITFIELD   0x200 /* Triple manipulates a bitfield */
740         signed char lhs, rhs, misc, targ;
741 };
742
743 #define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
744         .name = (NAME), \
745         .flags = (FLAGS), \
746         .lhs = (LHS), \
747         .rhs = (RHS), \
748         .misc = (MISC), \
749         .targ = (TARG), \
750          }
751 static const struct op_info table_ops[] = {
752 [OP_SDIVT      ] = OP( 2,  2, 0, 0, PURE | BLOCK , "sdivt"),
753 [OP_UDIVT      ] = OP( 2,  2, 0, 0, PURE | BLOCK , "udivt"),
754 [OP_SMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smul"),
755 [OP_UMUL       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umul"),
756 [OP_SDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
757 [OP_UDIV       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "udiv"),
758 [OP_SMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smod"),
759 [OP_UMOD       ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umod"),
760 [OP_ADD        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "add"),
761 [OP_SUB        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sub"),
762 [OP_SL         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sl"),
763 [OP_USR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "usr"),
764 [OP_SSR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ssr"),
765 [OP_AND        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "and"),
766 [OP_XOR        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "xor"),
767 [OP_OR         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "or"),
768 [OP_POS        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "pos"),
769 [OP_NEG        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "neg"),
770 [OP_INVERT     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "invert"),
771
772 [OP_EQ         ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "eq"),
773 [OP_NOTEQ      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "noteq"),
774 [OP_SLESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "sless"),
775 [OP_ULESS      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "uless"),
776 [OP_SMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smore"),
777 [OP_UMORE      ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umore"),
778 [OP_SLESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
779 [OP_ULESSEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
780 [OP_SMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
781 [OP_UMOREEQ    ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
782 [OP_LFALSE     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
783 [OP_LTRUE      ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
784
785 [OP_LOAD       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "load"),
786 [OP_STORE      ] = OP( 0,  2, 0, 0, PURE | BLOCK , "store"),
787
788 [OP_UEXTRACT   ] = OP( 0,  1, 0, 0, PURE | DEF | BITFIELD, "uextract"),
789 [OP_SEXTRACT   ] = OP( 0,  1, 0, 0, PURE | DEF | BITFIELD, "sextract"),
790 [OP_DEPOSIT    ] = OP( 0,  2, 0, 0, PURE | DEF | BITFIELD, "deposit"),
791
792 [OP_NOOP       ] = OP( 0,  0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
793
794 [OP_INTCONST   ] = OP( 0,  0, 0, 0, PURE | DEF, "intconst"),
795 [OP_BLOBCONST  ] = OP( 0,  0, 0, 0, PURE , "blobconst"),
796 [OP_ADDRCONST  ] = OP( 0,  0, 1, 0, PURE | DEF, "addrconst"),
797 [OP_UNKNOWNVAL ] = OP( 0,  0, 0, 0, PURE | DEF, "unknown"),
798
799 #warning "FIXME is it correct for OP_WRITE to be a def?  I currently use it as one..."
800 [OP_WRITE      ] = OP( 0,  1, 1, 0, PURE | DEF | BLOCK, "write"),
801 [OP_READ       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "read"),
802 [OP_COPY       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "copy"),
803 [OP_CONVERT    ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "convert"),
804 [OP_PIECE      ] = OP( 0,  0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"),
805 [OP_ASM        ] = OP(-1, -1, 0, 0, PURE, "asm"),
806 [OP_DEREF      ] = OP( 0,  1, 0, 0, 0 | DEF | BLOCK, "deref"), 
807 [OP_DOT        ] = OP( 0,  0, 1, 0, PURE | DEF | PART, "dot"),
808 [OP_INDEX      ] = OP( 0,  0, 1, 0, PURE | DEF | PART, "index"),
809
810 [OP_VAL        ] = OP( 0,  1, 1, 0, 0 | DEF | BLOCK, "val"),
811 [OP_TUPLE      ] = OP(-1,  0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"),
812 [OP_BITREF     ] = OP( 0,  1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"),
813 /* Call is special most it can stand in for anything so it depends on context */
814 [OP_FCALL      ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"),
815 [OP_PROG       ] = OP( 0,  1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"),
816 /* The sizes of OP_FCALL depends upon context */
817
818 [OP_LIST       ] = OP( 0,  1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
819 [OP_BRANCH     ] = OP( 0,  0, 0, 1, PURE | BLOCK | UBRANCH, "branch"),
820 [OP_CBRANCH    ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"),
821 [OP_CALL       ] = OP( 0,  0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"),
822 [OP_RET        ] = OP( 0,  1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"),
823 [OP_LABEL      ] = OP( 0,  0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
824 [OP_ADECL      ] = OP( 0,  0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
825 [OP_SDECL      ] = OP( 0,  0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
826 /* The number of RHS elements of OP_PHI depend upon context */
827 [OP_PHI        ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
828
829 #if 0
830 [OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH,     "cps_branch"),
831 [OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH,     "cps_cbranch"),
832 [OP_CPS_CALL   ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH,  "cps_call"),
833 [OP_CPS_RET    ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH,   "cps_ret"),
834 [OP_CPS_END    ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"),
835 [OP_CPS_START  ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL,  "cps_start"),
836 #endif
837
838 [OP_CMP        ] = OP( 0,  2, 0, 0, PURE | DEF | BLOCK, "cmp"),
839 [OP_TEST       ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "test"),
840 [OP_SET_EQ     ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
841 [OP_SET_NOTEQ  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
842 [OP_SET_SLESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
843 [OP_SET_ULESS  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
844 [OP_SET_SMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
845 [OP_SET_UMORE  ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
846 [OP_SET_SLESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
847 [OP_SET_ULESSEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
848 [OP_SET_SMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
849 [OP_SET_UMOREEQ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
850 [OP_JMP        ] = OP( 0,  0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"),
851 [OP_JMP_EQ     ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"),
852 [OP_JMP_NOTEQ  ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"),
853 [OP_JMP_SLESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"),
854 [OP_JMP_ULESS  ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"),
855 [OP_JMP_SMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"),
856 [OP_JMP_UMORE  ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"),
857 [OP_JMP_SLESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"),
858 [OP_JMP_ULESSEQ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"),
859 [OP_JMP_SMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"),
860 [OP_JMP_UMOREEQ] = OP( 0,  1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"),
861
862 [OP_INB        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
863 [OP_INW        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
864 [OP_INL        ] = OP( 0,  1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
865 [OP_OUTB       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outb"),
866 [OP_OUTW       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outw"),
867 [OP_OUTL       ] = OP( 0,  2, 0, 0, IMPURE| BLOCK, "__outl"),
868 [OP_BSF        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
869 [OP_BSR        ] = OP( 0,  1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
870 [OP_RDMSR      ] = OP( 2,  1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
871 [OP_WRMSR      ] = OP( 0,  3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
872 [OP_HLT        ] = OP( 0,  0, 0, 0, IMPURE | BLOCK, "__hlt"),
873 };
874 #undef OP
875 #define OP_MAX      (sizeof(table_ops)/sizeof(table_ops[0]))
876
877 static const char *tops(int index) 
878 {
879         static const char unknown[] = "unknown op";
880         if (index < 0) {
881                 return unknown;
882         }
883         if (index > OP_MAX) {
884                 return unknown;
885         }
886         return table_ops[index].name;
887 }
888
889 struct asm_info;
890 struct triple;
891 struct block;
892 struct triple_set {
893         struct triple_set *next;
894         struct triple *member;
895 };
896
897 #define MAX_LHS  63
898 #define MAX_RHS  127
899 #define MAX_MISC 3
900 #define MAX_TARG 1
901
902 struct occurance {
903         int count;
904         const char *filename;
905         const char *function;
906         int line;
907         int col;
908         struct occurance *parent;
909 };
910 struct bitfield {
911         ulong_t size : 8;
912         ulong_t offset : 24;
913 };
914 struct triple {
915         struct triple *next, *prev;
916         struct triple_set *use;
917         struct type *type;
918         unsigned int op : 8;
919         unsigned int template_id : 7;
920         unsigned int lhs  : 6;
921         unsigned int rhs  : 7;
922         unsigned int misc : 2;
923         unsigned int targ : 1;
924 #define TRIPLE_SIZE(TRIPLE) \
925         ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ)
926 #define TRIPLE_LHS_OFF(PTR)  (0)
927 #define TRIPLE_RHS_OFF(PTR)  (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs)
928 #define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs)
929 #define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc)
930 #define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)])
931 #define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)])
932 #define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)])
933 #define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)])
934         unsigned id; /* A scratch value and finally the register */
935 #define TRIPLE_FLAG_FLATTENED   (1 << 31)
936 #define TRIPLE_FLAG_PRE_SPLIT   (1 << 30)
937 #define TRIPLE_FLAG_POST_SPLIT  (1 << 29)
938 #define TRIPLE_FLAG_VOLATILE    (1 << 28)
939 #define TRIPLE_FLAG_INLINE      (1 << 27) /* ???? */
940 #define TRIPLE_FLAG_LOCAL       (1 << 26)
941
942 #define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
943         struct occurance *occurance;
944         union {
945                 ulong_t cval;
946                 struct bitfield bitfield;
947                 struct block  *block;
948                 void *blob;
949                 struct hash_entry *field;
950                 struct asm_info *ainfo;
951                 struct triple *func;
952                 struct symbol *symbol;
953         } u;
954         struct triple *param[2];
955 };
956
957 struct reg_info {
958         unsigned reg;
959         unsigned regcm;
960 };
961 struct ins_template {
962         struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
963 };
964
965 struct asm_info {
966         struct ins_template tmpl;
967         char *str;
968 };
969
970 struct block_set {
971         struct block_set *next;
972         struct block *member;
973 };
974 struct block {
975         struct block *work_next;
976         struct triple *first, *last;
977         int edge_count;
978         struct block_set *edges;
979         int users;
980         struct block_set *use;
981         struct block_set *idominates;
982         struct block_set *domfrontier;
983         struct block *idom;
984         struct block_set *ipdominates;
985         struct block_set *ipdomfrontier;
986         struct block *ipdom;
987         int vertex;
988         
989 };
990
991 struct symbol {
992         struct symbol *next;
993         struct hash_entry *ident;
994         struct triple *def;
995         struct type *type;
996         int scope_depth;
997 };
998
999 struct macro_arg {
1000         struct macro_arg *next;
1001         struct hash_entry *ident;
1002 };
1003 struct macro {
1004         struct hash_entry *ident;
1005         char *buf;
1006         int buf_len;
1007         int buf_off;
1008         struct macro_arg *args;
1009         int argc;
1010 };
1011
1012 struct hash_entry {
1013         struct hash_entry *next;
1014         const char *name;
1015         int name_len;
1016         int tok;
1017         struct macro *sym_define;
1018         struct symbol *sym_label;
1019         struct symbol *sym_tag;
1020         struct symbol *sym_ident;
1021 };
1022
1023 #define HASH_TABLE_SIZE 2048
1024
1025 struct compiler_state {
1026         const char *label_prefix;
1027         const char *ofilename;
1028         unsigned long flags;
1029         unsigned long debug;
1030         unsigned long max_allocation_passes;
1031
1032         size_t include_path_count;
1033         const char **include_paths;
1034
1035         size_t define_count;
1036         const char **defines;
1037
1038         size_t undef_count;
1039         const char **undefs;
1040 };
1041 struct arch_state {
1042         unsigned long features;
1043 };
1044 struct basic_blocks {
1045         struct triple *func;
1046         struct triple *first;
1047         struct block *first_block, *last_block;
1048         int last_vertex;
1049 };
1050 #define MAX_CPP_IF_DEPTH 63
1051 struct compile_state {
1052         struct compiler_state *compiler;
1053         struct arch_state *arch;
1054         FILE *output;
1055         FILE *errout;
1056         FILE *dbgout;
1057         struct file_state *file;
1058         struct occurance *last_occurance;
1059         const char *function;
1060         int    token_base;
1061         struct token token[6];
1062         struct hash_entry *hash_table[HASH_TABLE_SIZE];
1063         struct hash_entry *i_switch;
1064         struct hash_entry *i_case;
1065         struct hash_entry *i_continue;
1066         struct hash_entry *i_break;
1067         struct hash_entry *i_default;
1068         struct hash_entry *i_return;
1069         /* Additional hash entries for predefined macros */
1070         struct hash_entry *i_defined;
1071         struct hash_entry *i___VA_ARGS__;
1072         struct hash_entry *i___FILE__;
1073         struct hash_entry *i___LINE__;
1074         /* Additional hash entries for predefined identifiers */
1075         struct hash_entry *i___func__;
1076         /* Additional hash entries for attributes */
1077         struct hash_entry *i_noinline;
1078         struct hash_entry *i_always_inline;
1079         int scope_depth;
1080         unsigned char if_bytes[(MAX_CPP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
1081         int if_depth;
1082         int eat_depth, eat_targ;
1083         int macro_line;
1084         struct file_state *macro_file;
1085         struct triple *functions;
1086         struct triple *main_function;
1087         struct triple *first;
1088         struct triple *global_pool;
1089         struct basic_blocks bb;
1090         int functions_joined;
1091 };
1092
1093 /* visibility global/local */
1094 /* static/auto duration */
1095 /* typedef, register, inline */
1096 #define STOR_SHIFT         0
1097 #define STOR_MASK     0x001f
1098 /* Visibility */
1099 #define STOR_GLOBAL   0x0001
1100 /* Duration */
1101 #define STOR_PERM     0x0002
1102 /* Definition locality */
1103 #define STOR_NONLOCAL 0x0004  /* The definition is not in this translation unit */
1104 /* Storage specifiers */
1105 #define STOR_AUTO     0x0000
1106 #define STOR_STATIC   0x0002
1107 #define STOR_LOCAL    0x0003
1108 #define STOR_EXTERN   0x0007
1109 #define STOR_INLINE   0x0008
1110 #define STOR_REGISTER 0x0010
1111 #define STOR_TYPEDEF  0x0018
1112
1113 #define QUAL_SHIFT         5
1114 #define QUAL_MASK     0x00e0
1115 #define QUAL_NONE     0x0000
1116 #define QUAL_CONST    0x0020
1117 #define QUAL_VOLATILE 0x0040
1118 #define QUAL_RESTRICT 0x0080
1119
1120 #define TYPE_SHIFT         8
1121 #define TYPE_MASK     0x1f00
1122 #define TYPE_INTEGER(TYPE)    ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1123 #define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1124 #define TYPE_UNSIGNED(TYPE)   ((TYPE) & 0x0100)
1125 #define TYPE_SIGNED(TYPE)     (!TYPE_UNSIGNED(TYPE))
1126 #define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1127 #define TYPE_RANK(TYPE)       ((TYPE) & ~0xF1FF)
1128 #define TYPE_PTR(TYPE)        (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1129 #define TYPE_DEFAULT  0x0000
1130 #define TYPE_VOID     0x0100
1131 #define TYPE_CHAR     0x0200
1132 #define TYPE_UCHAR    0x0300
1133 #define TYPE_SHORT    0x0400
1134 #define TYPE_USHORT   0x0500
1135 #define TYPE_INT      0x0600
1136 #define TYPE_UINT     0x0700
1137 #define TYPE_LONG     0x0800
1138 #define TYPE_ULONG    0x0900
1139 #define TYPE_LLONG    0x0a00 /* long long */
1140 #define TYPE_ULLONG   0x0b00
1141 #define TYPE_FLOAT    0x0c00
1142 #define TYPE_DOUBLE   0x0d00
1143 #define TYPE_LDOUBLE  0x0e00 /* long double */
1144
1145 /* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1146 #define TYPE_ENUM     0x1600
1147 #define TYPE_LIST     0x1700
1148 /* TYPE_LIST is a basic building block when defining enumerations
1149  * type->field_ident holds the name of this enumeration entry.
1150  * type->right holds the entry in the list.
1151  */
1152
1153 #define TYPE_STRUCT   0x1000
1154 /* For TYPE_STRUCT
1155  * type->left holds the link list of TYPE_PRODUCT entries that
1156  * make up the structure.
1157  * type->elements hold the length of the linked list
1158  */
1159 #define TYPE_UNION    0x1100
1160 /* For TYPE_UNION
1161  * type->left holds the link list of TYPE_OVERLAP entries that
1162  * make up the union.
1163  * type->elements hold the length of the linked list
1164  */
1165 #define TYPE_POINTER  0x1200 
1166 /* For TYPE_POINTER:
1167  * type->left holds the type pointed to.
1168  */
1169 #define TYPE_FUNCTION 0x1300 
1170 /* For TYPE_FUNCTION:
1171  * type->left holds the return type.
1172  * type->right holds the type of the arguments
1173  * type->elements holds the count of the arguments
1174  */
1175 #define TYPE_PRODUCT  0x1400
1176 /* TYPE_PRODUCT is a basic building block when defining structures
1177  * type->left holds the type that appears first in memory.
1178  * type->right holds the type that appears next in memory.
1179  */
1180 #define TYPE_OVERLAP  0x1500
1181 /* TYPE_OVERLAP is a basic building block when defining unions
1182  * type->left and type->right holds to types that overlap
1183  * each other in memory.
1184  */
1185 #define TYPE_ARRAY    0x1800
1186 /* TYPE_ARRAY is a basic building block when definitng arrays.
1187  * type->left holds the type we are an array of.
1188  * type->elements holds the number of elements.
1189  */
1190 #define TYPE_TUPLE    0x1900
1191 /* TYPE_TUPLE is a basic building block when defining 
1192  * positionally reference type conglomerations. (i.e. closures)
1193  * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1194  * except it has no field names.
1195  * type->left holds the liked list of TYPE_PRODUCT entries that
1196  * make up the closure type.
1197  * type->elements hold the number of elements in the closure.
1198  */
1199 #define TYPE_JOIN     0x1a00
1200 /* TYPE_JOIN is a basic building block when defining 
1201  * positionally reference type conglomerations. (i.e. closures)
1202  * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1203  * except it has no field names.
1204  * type->left holds the liked list of TYPE_OVERLAP entries that
1205  * make up the closure type.
1206  * type->elements hold the number of elements in the closure.
1207  */
1208 #define TYPE_BITFIELD 0x1b00
1209 /* TYPE_BITFIED is the type of a bitfield.
1210  * type->left holds the type basic type TYPE_BITFIELD is derived from.
1211  * type->elements holds the number of bits in the bitfield.
1212  */
1213 #define TYPE_UNKNOWN  0x1c00
1214 /* TYPE_UNKNOWN is the type of an unknown value.
1215  * Used on unknown consts and other places where I don't know the type.
1216  */
1217
1218 #define ATTRIB_SHIFT                 16
1219 #define ATTRIB_MASK          0xffff0000
1220 #define ATTRIB_NOINLINE      0x00010000
1221 #define ATTRIB_ALWAYS_INLINE 0x00020000
1222
1223 #define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
1224
1225 struct type {
1226         unsigned int type;
1227         struct type *left, *right;
1228         ulong_t elements;
1229         struct hash_entry *field_ident;
1230         struct hash_entry *type_ident;
1231 };
1232
1233 #define TEMPLATE_BITS      7
1234 #define MAX_TEMPLATES      (1<<TEMPLATE_BITS)
1235 #define MAX_REG_EQUIVS     16
1236 #define MAX_REGC           14
1237 #define MAX_REGISTERS      75
1238 #define REGISTER_BITS      7
1239 #define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
1240 #define REG_ERROR          0
1241 #define REG_UNSET          1
1242 #define REG_UNNEEDED       2
1243 #define REG_VIRT0          (MAX_REGISTERS + 0)
1244 #define REG_VIRT1          (MAX_REGISTERS + 1)
1245 #define REG_VIRT2          (MAX_REGISTERS + 2)
1246 #define REG_VIRT3          (MAX_REGISTERS + 3)
1247 #define REG_VIRT4          (MAX_REGISTERS + 4)
1248 #define REG_VIRT5          (MAX_REGISTERS + 5)
1249 #define REG_VIRT6          (MAX_REGISTERS + 6)
1250 #define REG_VIRT7          (MAX_REGISTERS + 7)
1251 #define REG_VIRT8          (MAX_REGISTERS + 8)
1252 #define REG_VIRT9          (MAX_REGISTERS + 9)
1253
1254 #if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1255 #error "MAX_VIRT_REGISTERS to small"
1256 #endif
1257 #if (MAX_REGC + REGISTER_BITS) >= 26
1258 #error "Too many id bits used"
1259 #endif
1260
1261 /* Provision for 8 register classes */
1262 #define REG_SHIFT  0
1263 #define REGC_SHIFT REGISTER_BITS
1264 #define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
1265 #define REG_MASK (MAX_VIRT_REGISTERS -1)
1266 #define ID_REG(ID)              ((ID) & REG_MASK)
1267 #define SET_REG(ID, REG)        ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
1268 #define ID_REGCM(ID)            (((ID) & REGC_MASK) >> REGC_SHIFT)
1269 #define SET_REGCM(ID, REGCM)    ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1270 #define SET_INFO(ID, INFO)      ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1271                 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
1272
1273 #define ARCH_INPUT_REGS 4
1274 #define ARCH_OUTPUT_REGS 4
1275
1276 static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1277 static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
1278 static unsigned arch_reg_regcm(struct compile_state *state, int reg);
1279 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
1280 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
1281 static void arch_reg_equivs(
1282         struct compile_state *state, unsigned *equiv, int reg);
1283 static int arch_select_free_register(
1284         struct compile_state *state, char *used, int classes);
1285 static unsigned arch_regc_size(struct compile_state *state, int class);
1286 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1287 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1288 static const char *arch_reg_str(int reg);
1289 static struct reg_info arch_reg_constraint(
1290         struct compile_state *state, struct type *type, const char *constraint);
1291 static struct reg_info arch_reg_clobber(
1292         struct compile_state *state, const char *clobber);
1293 static struct reg_info arch_reg_lhs(struct compile_state *state, 
1294         struct triple *ins, int index);
1295 static struct reg_info arch_reg_rhs(struct compile_state *state, 
1296         struct triple *ins, int index);
1297 static int arch_reg_size(int reg);
1298 static struct triple *transform_to_arch_instruction(
1299         struct compile_state *state, struct triple *ins);
1300 static struct triple *flatten(
1301         struct compile_state *state, struct triple *first, struct triple *ptr);
1302
1303
1304
1305
1306 #define DEBUG_ABORT_ON_ERROR    0x00000001
1307 #define DEBUG_BASIC_BLOCKS      0x00000002
1308 #define DEBUG_FDOMINATORS       0x00000004
1309 #define DEBUG_RDOMINATORS       0x00000008
1310 #define DEBUG_TRIPLES           0x00000010
1311 #define DEBUG_INTERFERENCE      0x00000020
1312 #define DEBUG_SCC_TRANSFORM     0x00000040
1313 #define DEBUG_SCC_TRANSFORM2    0x00000080
1314 #define DEBUG_REBUILD_SSA_FORM  0x00000100
1315 #define DEBUG_INLINE            0x00000200
1316 #define DEBUG_RANGE_CONFLICTS   0x00000400
1317 #define DEBUG_RANGE_CONFLICTS2  0x00000800
1318 #define DEBUG_COLOR_GRAPH       0x00001000
1319 #define DEBUG_COLOR_GRAPH2      0x00002000
1320 #define DEBUG_COALESCING        0x00004000
1321 #define DEBUG_COALESCING2       0x00008000
1322 #define DEBUG_VERIFICATION      0x00010000
1323 #define DEBUG_CALLS             0x00020000
1324 #define DEBUG_CALLS2            0x00040000
1325 #define DEBUG_TOKENS            0x80000000
1326
1327 #define DEBUG_DEFAULT ( \
1328         DEBUG_ABORT_ON_ERROR | \
1329         DEBUG_BASIC_BLOCKS | \
1330         DEBUG_FDOMINATORS | \
1331         DEBUG_RDOMINATORS | \
1332         DEBUG_TRIPLES | \
1333         0 )
1334
1335 #define DEBUG_ALL ( \
1336         DEBUG_ABORT_ON_ERROR   | \
1337         DEBUG_BASIC_BLOCKS     | \
1338         DEBUG_FDOMINATORS      | \
1339         DEBUG_RDOMINATORS      | \
1340         DEBUG_TRIPLES          | \
1341         DEBUG_INTERFERENCE     | \
1342         DEBUG_SCC_TRANSFORM    | \
1343         DEBUG_SCC_TRANSFORM2   | \
1344         DEBUG_REBUILD_SSA_FORM | \
1345         DEBUG_INLINE           | \
1346         DEBUG_RANGE_CONFLICTS  | \
1347         DEBUG_RANGE_CONFLICTS2 | \
1348         DEBUG_COLOR_GRAPH      | \
1349         DEBUG_COLOR_GRAPH2     | \
1350         DEBUG_COALESCING       | \
1351         DEBUG_COALESCING2      | \
1352         DEBUG_VERIFICATION     | \
1353         DEBUG_CALLS            | \
1354         DEBUG_CALLS2           | \
1355         DEBUG_TOKENS           | \
1356         0 )
1357
1358 #define COMPILER_INLINE_MASK               0x00000007
1359 #define COMPILER_INLINE_ALWAYS             0x00000000
1360 #define COMPILER_INLINE_NEVER              0x00000001
1361 #define COMPILER_INLINE_DEFAULTON          0x00000002
1362 #define COMPILER_INLINE_DEFAULTOFF         0x00000003
1363 #define COMPILER_INLINE_NOPENALTY          0x00000004
1364 #define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1365 #define COMPILER_SIMPLIFY                  0x00000010
1366 #define COMPILER_SCC_TRANSFORM             0x00000020
1367 #define COMPILER_SIMPLIFY_OP               0x00000040
1368 #define COMPILER_SIMPLIFY_PHI              0x00000080
1369 #define COMPILER_SIMPLIFY_LABEL            0x00000100
1370 #define COMPILER_SIMPLIFY_BRANCH           0x00000200
1371 #define COMPILER_SIMPLIFY_COPY             0x00000400
1372 #define COMPILER_SIMPLIFY_ARITH            0x00000800
1373 #define COMPILER_SIMPLIFY_SHIFT            0x00001000
1374 #define COMPILER_SIMPLIFY_BITWISE          0x00002000
1375 #define COMPILER_SIMPLIFY_LOGICAL          0x00004000
1376 #define COMPILER_SIMPLIFY_BITFIELD         0x00008000
1377
1378 #define COMPILER_CPP_ONLY                  0x80000000
1379
1380 #define COMPILER_DEFAULT_FLAGS ( \
1381         COMPILER_ELIMINATE_INEFECTUAL_CODE | \
1382         COMPILER_INLINE_DEFAULTON | \
1383         COMPILER_SIMPLIFY_OP | \
1384         COMPILER_SIMPLIFY_PHI | \
1385         COMPILER_SIMPLIFY_LABEL | \
1386         COMPILER_SIMPLIFY_BRANCH | \
1387         COMPILER_SIMPLIFY_COPY | \
1388         COMPILER_SIMPLIFY_ARITH | \
1389         COMPILER_SIMPLIFY_SHIFT | \
1390         COMPILER_SIMPLIFY_BITWISE | \
1391         COMPILER_SIMPLIFY_LOGICAL | \
1392         COMPILER_SIMPLIFY_BITFIELD | \
1393         0 )
1394
1395 #define GLOBAL_SCOPE_DEPTH   1
1396 #define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
1397
1398 static void compile_file(struct compile_state *old_state, const char *filename, int local);
1399
1400
1401
1402 static void init_compiler_state(struct compiler_state *compiler)
1403 {
1404         memset(compiler, 0, sizeof(*compiler));
1405         compiler->label_prefix = "";
1406         compiler->ofilename = "auto.inc";
1407         compiler->flags = COMPILER_DEFAULT_FLAGS;
1408         compiler->debug = 0;
1409         compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
1410         compiler->include_path_count = 1;
1411         compiler->include_paths      = xcmalloc(sizeof(char *), "include_paths");
1412         compiler->define_count       = 1;
1413         compiler->defines            = xcmalloc(sizeof(char *), "defines");
1414         compiler->undef_count        = 1;
1415         compiler->undefs             = xcmalloc(sizeof(char *), "undefs");
1416 }
1417
1418 struct compiler_flag {
1419         const char *name;
1420         unsigned long flag;
1421 };
1422
1423 struct compiler_arg {
1424         const char *name;
1425         unsigned long mask;
1426         struct compiler_flag flags[16];
1427 };
1428
1429 static int set_flag(
1430         const struct compiler_flag *ptr, unsigned long *flags,
1431         int act, const char *flag)
1432 {
1433         int result = -1;
1434         for(; ptr->name; ptr++) {
1435                 if (strcmp(ptr->name, flag) == 0) {
1436                         break;
1437                 }
1438         }
1439         if (ptr->name) {
1440                 result = 0;
1441                 *flags &= ~(ptr->flag);
1442                 if (act) {
1443                         *flags |= ptr->flag;
1444                 }
1445         }
1446         return result;
1447 }
1448
1449 static int set_arg(
1450         const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1451 {
1452         const char *val;
1453         int result = -1;
1454         int len;
1455         val = strchr(arg, '=');
1456         if (val) {
1457                 len = val - arg;
1458                 val++;
1459                 for(; ptr->name; ptr++) {
1460                         if (strncmp(ptr->name, arg, len) == 0) {
1461                                 break;
1462                         }
1463                 }
1464                 if (ptr->name) {
1465                         *flags &= ~ptr->mask;
1466                         result = set_flag(&ptr->flags[0], flags, 1, val);
1467                 }
1468         }
1469         return result;
1470 }
1471         
1472
1473 static void flag_usage(FILE *fp, const struct compiler_flag *ptr, 
1474         const char *prefix, const char *invert_prefix)
1475 {
1476         for(;ptr->name; ptr++) {
1477                 fprintf(fp, "%s%s\n", prefix, ptr->name);
1478                 if (invert_prefix) {
1479                         fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1480                 }
1481         }
1482 }
1483
1484 static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1485         const char *prefix)
1486 {
1487         for(;ptr->name; ptr++) {
1488                 const struct compiler_flag *flag;
1489                 for(flag = &ptr->flags[0]; flag->name; flag++) {
1490                         fprintf(fp, "%s%s=%s\n", 
1491                                 prefix, ptr->name, flag->name);
1492                 }
1493         }
1494 }
1495
1496 static int append_string(size_t *max, const char ***vec, const char *str,
1497         const char *name)
1498 {
1499         size_t count;
1500         count = ++(*max);
1501         *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1502         (*vec)[count -1] = 0;
1503         (*vec)[count -2] = str; 
1504         return 0;
1505 }
1506
1507 static void arg_error(char *fmt, ...);
1508 static const char *identifier(const char *str, const char *end);
1509
1510 static int append_include_path(struct compiler_state *compiler, const char *str)
1511 {
1512         int result;
1513         if (!exists(str, ".")) {
1514                 arg_error("Nonexistent include path: `%s'\n",
1515                         str);
1516         }
1517         result = append_string(&compiler->include_path_count,
1518                 &compiler->include_paths, str, "include_paths");
1519         return result;
1520 }
1521
1522 static int append_define(struct compiler_state *compiler, const char *str)
1523 {
1524         const char *end, *rest;
1525         int result;
1526
1527         end = strchr(str, '=');
1528         if (!end) {
1529                 end = str + strlen(str);
1530         }
1531         rest = identifier(str, end);
1532         if (rest != end) {
1533                 int len = end - str - 1;
1534                 arg_error("Invalid name cannot define macro: `%*.*s'\n", 
1535                         len, len, str);
1536         }
1537         result = append_string(&compiler->define_count,
1538                 &compiler->defines, str, "defines");
1539         return result;
1540 }
1541
1542 static int append_undef(struct compiler_state *compiler, const char *str)
1543 {
1544         const char *end, *rest;
1545         int result;
1546
1547         end = str + strlen(str);
1548         rest = identifier(str, end);
1549         if (rest != end) {
1550                 int len = end - str - 1;
1551                 arg_error("Invalid name cannot undefine macro: `%*.*s'\n", 
1552                         len, len, str);
1553         }
1554         result = append_string(&compiler->undef_count,
1555                 &compiler->undefs, str, "undefs");
1556         return result;
1557 }
1558
1559 static const struct compiler_flag romcc_flags[] = {
1560         { "cpp-only",                  COMPILER_CPP_ONLY },
1561         { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1562         { "simplify",                  COMPILER_SIMPLIFY },
1563         { "scc-transform",             COMPILER_SCC_TRANSFORM },
1564         { "simplify-op",               COMPILER_SIMPLIFY_OP },
1565         { "simplify-phi",              COMPILER_SIMPLIFY_PHI },
1566         { "simplify-label",            COMPILER_SIMPLIFY_LABEL },
1567         { "simplify-branch",           COMPILER_SIMPLIFY_BRANCH },
1568         { "simplify-copy",             COMPILER_SIMPLIFY_COPY },
1569         { "simplify-arith",            COMPILER_SIMPLIFY_ARITH },
1570         { "simplify-shift",            COMPILER_SIMPLIFY_SHIFT },
1571         { "simplify-bitwise",          COMPILER_SIMPLIFY_BITWISE },
1572         { "simplify-logical",          COMPILER_SIMPLIFY_LOGICAL },
1573         { "simplify-bitfield",         COMPILER_SIMPLIFY_BITFIELD },
1574         { 0, 0 },
1575 };
1576 static const struct compiler_arg romcc_args[] = {
1577         { "inline-policy",             COMPILER_INLINE_MASK,
1578                 {
1579                         { "always",      COMPILER_INLINE_ALWAYS, },
1580                         { "never",       COMPILER_INLINE_NEVER, },
1581                         { "defaulton",   COMPILER_INLINE_DEFAULTON, },
1582                         { "defaultoff",  COMPILER_INLINE_DEFAULTOFF, },
1583                         { "nopenalty",   COMPILER_INLINE_NOPENALTY, },
1584                         { 0, 0 },
1585                 },
1586         },
1587         { 0, 0 },
1588 };
1589 static const struct compiler_flag romcc_opt_flags[] = {
1590         { "-O",  COMPILER_SIMPLIFY },
1591         { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
1592         { "-E",  COMPILER_CPP_ONLY },
1593         { 0, 0, },
1594 };
1595 static const struct compiler_flag romcc_debug_flags[] = {
1596         { "all",                   DEBUG_ALL },
1597         { "abort-on-error",        DEBUG_ABORT_ON_ERROR },
1598         { "basic-blocks",          DEBUG_BASIC_BLOCKS },
1599         { "fdominators",           DEBUG_FDOMINATORS },
1600         { "rdominators",           DEBUG_RDOMINATORS },
1601         { "triples",               DEBUG_TRIPLES },
1602         { "interference",          DEBUG_INTERFERENCE },
1603         { "scc-transform",         DEBUG_SCC_TRANSFORM },
1604         { "scc-transform2",        DEBUG_SCC_TRANSFORM2 },
1605         { "rebuild-ssa-form",      DEBUG_REBUILD_SSA_FORM },
1606         { "inline",                DEBUG_INLINE },
1607         { "live-range-conflicts",  DEBUG_RANGE_CONFLICTS },
1608         { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1609         { "color-graph",           DEBUG_COLOR_GRAPH },
1610         { "color-graph2",          DEBUG_COLOR_GRAPH2 },
1611         { "coalescing",            DEBUG_COALESCING },
1612         { "coalescing2",           DEBUG_COALESCING2 },
1613         { "verification",          DEBUG_VERIFICATION },
1614         { "calls",                 DEBUG_CALLS },
1615         { "calls2",                DEBUG_CALLS2 },
1616         { "tokens",                DEBUG_TOKENS },
1617         { 0, 0 },
1618 };
1619
1620 static int compiler_encode_flag(
1621         struct compiler_state *compiler, const char *flag)
1622 {
1623         int act;
1624         int result;
1625
1626         act = 1;
1627         result = -1;
1628         if (strncmp(flag, "no-", 3) == 0) {
1629                 flag += 3;
1630                 act = 0;
1631         }
1632         if (strncmp(flag, "-O", 2) == 0) {
1633                 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1634         }
1635         else if (strncmp(flag, "-E", 2) == 0) {
1636                 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1637         }
1638         else if (strncmp(flag, "-I", 2) == 0) {
1639                 result = append_include_path(compiler, flag + 2);
1640         }
1641         else if (strncmp(flag, "-D", 2) == 0) {
1642                 result = append_define(compiler, flag + 2);
1643         }
1644         else if (strncmp(flag, "-U", 2) == 0) {
1645                 result = append_undef(compiler, flag + 2);
1646         }
1647         else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1648                 result = 0;
1649                 compiler->label_prefix = flag + 13;
1650         }
1651         else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1652                 unsigned long max_passes;
1653                 char *end;
1654                 max_passes = strtoul(flag + 22, &end, 10);
1655                 if (end[0] == '\0') {
1656                         result = 0;
1657                         compiler->max_allocation_passes = max_passes;
1658                 }
1659         }
1660         else if (act && strcmp(flag, "debug") == 0) {
1661                 result = 0;
1662                 compiler->debug |= DEBUG_DEFAULT;
1663         }
1664         else if (strncmp(flag, "debug-", 6) == 0) {
1665                 flag += 6;
1666                 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
1667         }
1668         else {
1669                 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1670                 if (result < 0) {
1671                         result = set_arg(romcc_args, &compiler->flags, flag);
1672                 }
1673         }
1674         return result;
1675 }
1676
1677 static void compiler_usage(FILE *fp)
1678 {
1679         flag_usage(fp, romcc_opt_flags, "", 0);
1680         flag_usage(fp, romcc_flags, "-f", "-fno-");
1681         arg_usage(fp,  romcc_args, "-f");
1682         flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1683         fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1684         fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1685         fprintf(fp, "-I<include path>\n");
1686         fprintf(fp, "-D<macro>[=defn]\n");
1687         fprintf(fp, "-U<macro>\n");
1688 }
1689
1690 static void do_cleanup(struct compile_state *state)
1691 {
1692         if (state->output) {
1693                 fclose(state->output);
1694                 unlink(state->compiler->ofilename);
1695                 state->output = 0;
1696         }
1697         if (state->dbgout) {
1698                 fflush(state->dbgout);
1699         }
1700         if (state->errout) {
1701                 fflush(state->errout);
1702         }
1703 }
1704
1705 static struct compile_state *exit_state;
1706 static void exit_cleanup(void)
1707 {
1708         if (exit_state) {
1709                 do_cleanup(exit_state);
1710         }
1711 }
1712
1713 static int get_col(struct file_state *file)
1714 {
1715         int col;
1716         const char *ptr, *end;
1717         ptr = file->line_start;
1718         end = file->pos;
1719         for(col = 0; ptr < end; ptr++) {
1720                 if (*ptr != '\t') {
1721                         col++;
1722                 } 
1723                 else {
1724                         col = (col & ~7) + 8;
1725                 }
1726         }
1727         return col;
1728 }
1729
1730 static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1731 {
1732         int col;
1733         if (triple && triple->occurance) {
1734                 struct occurance *spot;
1735                 for(spot = triple->occurance; spot; spot = spot->parent) {
1736                         fprintf(fp, "%s:%d.%d: ", 
1737                                 spot->filename, spot->line, spot->col);
1738                 }
1739                 return;
1740         }
1741         if (!state->file) {
1742                 return;
1743         }
1744         col = get_col(state->file);
1745         fprintf(fp, "%s:%d.%d: ", 
1746                 state->file->report_name, state->file->report_line, col);
1747 }
1748
1749 static void internal_error(struct compile_state *state, struct triple *ptr, 
1750         const char *fmt, ...)
1751 {
1752         FILE *fp = state->errout;
1753         va_list args;
1754         va_start(args, fmt);
1755         loc(fp, state, ptr);
1756         fputc('\n', fp);
1757         if (ptr) {
1758                 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1759         }
1760         fprintf(fp, "Internal compiler error: ");
1761         vfprintf(fp, fmt, args);
1762         fprintf(fp, "\n");
1763         va_end(args);
1764         do_cleanup(state);
1765         abort();
1766 }
1767
1768
1769 static void internal_warning(struct compile_state *state, struct triple *ptr, 
1770         const char *fmt, ...)
1771 {
1772         FILE *fp = state->errout;
1773         va_list args;
1774         va_start(args, fmt);
1775         loc(fp, state, ptr);
1776         if (ptr) {
1777                 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1778         }
1779         fprintf(fp, "Internal compiler warning: ");
1780         vfprintf(fp, fmt, args);
1781         fprintf(fp, "\n");
1782         va_end(args);
1783 }
1784
1785
1786
1787 static void error(struct compile_state *state, struct triple *ptr, 
1788         const char *fmt, ...)
1789 {
1790         FILE *fp = state->errout;
1791         va_list args;
1792         va_start(args, fmt);
1793         loc(fp, state, ptr);
1794         fputc('\n', fp);
1795         if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1796                 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1797         }
1798         vfprintf(fp, fmt, args);
1799         va_end(args);
1800         fprintf(fp, "\n");
1801         do_cleanup(state);
1802         if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
1803                 abort();
1804         }
1805         exit(1);
1806 }
1807
1808 static void warning(struct compile_state *state, struct triple *ptr, 
1809         const char *fmt, ...)
1810 {
1811         FILE *fp = state->errout;
1812         va_list args;
1813         va_start(args, fmt);
1814         loc(fp, state, ptr);
1815         fprintf(fp, "warning: "); 
1816         if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1817                 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1818         }
1819         vfprintf(fp, fmt, args);
1820         fprintf(fp, "\n");
1821         va_end(args);
1822 }
1823
1824 #define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1825
1826 static void valid_op(struct compile_state *state, int op)
1827 {
1828         char *fmt = "invalid op: %d";
1829         if (op >= OP_MAX) {
1830                 internal_error(state, 0, fmt, op);
1831         }
1832         if (op < 0) {
1833                 internal_error(state, 0, fmt, op);
1834         }
1835 }
1836
1837 static void valid_ins(struct compile_state *state, struct triple *ptr)
1838 {
1839         valid_op(state, ptr->op);
1840 }
1841
1842 static void valid_param_count(struct compile_state *state, struct triple *ins)
1843 {
1844         int lhs, rhs, misc, targ;
1845         valid_ins(state, ins);
1846         lhs  = table_ops[ins->op].lhs;
1847         rhs  = table_ops[ins->op].rhs;
1848         misc = table_ops[ins->op].misc;
1849         targ = table_ops[ins->op].targ;
1850
1851         if ((lhs >= 0) && (ins->lhs != lhs)) {
1852                 internal_error(state, ins, "Bad lhs count");
1853         }
1854         if ((rhs >= 0) && (ins->rhs != rhs)) {
1855                 internal_error(state, ins, "Bad rhs count");
1856         }
1857         if ((misc >= 0) && (ins->misc != misc)) {
1858                 internal_error(state, ins, "Bad misc count");
1859         }
1860         if ((targ >= 0) && (ins->targ != targ)) {
1861                 internal_error(state, ins, "Bad targ count");
1862         }
1863 }
1864
1865 static void process_trigraphs(struct compile_state *state)
1866 {
1867         char *src, *dest, *end;
1868         struct file_state *file;
1869         file = state->file;
1870         src = dest = file->buf;
1871         end = file->buf + file->size;
1872         while((end - src) >= 3) {
1873                 if ((src[0] == '?') && (src[1] == '?')) {
1874                         int c = -1;
1875                         switch(src[2]) {
1876                         case '=': c = '#'; break;
1877                         case '/': c = '\\'; break;
1878                         case '\'': c = '^'; break;
1879                         case '(': c = '['; break;
1880                         case ')': c = ']'; break;
1881                         case '!': c = '!'; break;
1882                         case '<': c = '{'; break;
1883                         case '>': c = '}'; break;
1884                         case '-': c = '~'; break;
1885                         }
1886                         if (c != -1) {
1887                                 *dest++ = c;
1888                                 src += 3;
1889                         }
1890                         else {
1891                                 *dest++ = *src++;
1892                         }
1893                 }
1894                 else {
1895                         *dest++ = *src++;
1896                 }
1897         }
1898         while(src != end) {
1899                 *dest++ = *src++;
1900         }
1901         file->size = dest - file->buf;
1902 }
1903
1904 static void splice_lines(struct compile_state *state)
1905 {
1906         char *src, *dest, *end;
1907         struct file_state *file;
1908         file = state->file;
1909         src = dest = file->buf;
1910         end = file->buf + file->size;
1911         while((end - src) >= 2) {
1912                 if ((src[0] == '\\') && (src[1] == '\n')) {
1913                         src += 2;
1914                 }
1915                 else {
1916                         *dest++ = *src++;
1917                 }
1918         }
1919         while(src != end) {
1920                 *dest++ = *src++;
1921         }
1922         file->size = dest - file->buf;
1923 }
1924
1925 static struct type void_type;
1926 static struct type unknown_type;
1927 static void use_triple(struct triple *used, struct triple *user)
1928 {
1929         struct triple_set **ptr, *new;
1930         if (!used)
1931                 return;
1932         if (!user)
1933                 return;
1934         ptr = &used->use;
1935         while(*ptr) {
1936                 if ((*ptr)->member == user) {
1937                         return;
1938                 }
1939                 ptr = &(*ptr)->next;
1940         }
1941         /* Append new to the head of the list, 
1942          * copy_func and rename_block_variables
1943          * depends on this.
1944          */
1945         new = xcmalloc(sizeof(*new), "triple_set");
1946         new->member = user;
1947         new->next   = used->use;
1948         used->use   = new;
1949 }
1950
1951 static void unuse_triple(struct triple *used, struct triple *unuser)
1952 {
1953         struct triple_set *use, **ptr;
1954         if (!used) {
1955                 return;
1956         }
1957         ptr = &used->use;
1958         while(*ptr) {
1959                 use = *ptr;
1960                 if (use->member == unuser) {
1961                         *ptr = use->next;
1962                         xfree(use);
1963                 }
1964                 else {
1965                         ptr = &use->next;
1966                 }
1967         }
1968 }
1969
1970 static void put_occurance(struct occurance *occurance)
1971 {
1972         if (occurance) {
1973                 occurance->count -= 1;
1974                 if (occurance->count <= 0) {
1975                         if (occurance->parent) {
1976                                 put_occurance(occurance->parent);
1977                         }
1978                         xfree(occurance);
1979                 }
1980         }
1981 }
1982
1983 static void get_occurance(struct occurance *occurance)
1984 {
1985         if (occurance) {
1986                 occurance->count += 1;
1987         }
1988 }
1989
1990
1991 static struct occurance *new_occurance(struct compile_state *state)
1992 {
1993         struct occurance *result, *last;
1994         const char *filename;
1995         const char *function;
1996         int line, col;
1997
1998         function = "";
1999         filename = 0;
2000         line = 0;
2001         col  = 0;
2002         if (state->file) {
2003                 filename = state->file->report_name;
2004                 line     = state->file->report_line;
2005                 col      = get_col(state->file);
2006         }
2007         if (state->function) {
2008                 function = state->function;
2009         }
2010         last = state->last_occurance;
2011         if (last &&
2012                 (last->col == col) &&
2013                 (last->line == line) &&
2014                 (last->function == function) &&
2015                 ((last->filename == filename) ||
2016                         (strcmp(last->filename, filename) == 0))) 
2017         {
2018                 get_occurance(last);
2019                 return last;
2020         }
2021         if (last) {
2022                 state->last_occurance = 0;
2023                 put_occurance(last);
2024         }
2025         result = xmalloc(sizeof(*result), "occurance");
2026         result->count    = 2;
2027         result->filename = filename;
2028         result->function = function;
2029         result->line     = line;
2030         result->col      = col;
2031         result->parent   = 0;
2032         state->last_occurance = result;
2033         return result;
2034 }
2035
2036 static struct occurance *inline_occurance(struct compile_state *state,
2037         struct occurance *base, struct occurance *top)
2038 {
2039         struct occurance *result, *last;
2040         if (top->parent) {
2041                 internal_error(state, 0, "inlining an already inlined function?");
2042         }
2043         /* If I have a null base treat it that way */
2044         if ((base->parent == 0) &&
2045                 (base->col == 0) &&
2046                 (base->line == 0) &&
2047                 (base->function[0] == '\0') &&
2048                 (base->filename[0] == '\0')) {
2049                 base = 0;
2050         }
2051         /* See if I can reuse the last occurance I had */
2052         last = state->last_occurance;
2053         if (last &&
2054                 (last->parent   == base) &&
2055                 (last->col      == top->col) &&
2056                 (last->line     == top->line) &&
2057                 (last->function == top->function) &&
2058                 (last->filename == top->filename)) {
2059                 get_occurance(last);
2060                 return last;
2061         }
2062         /* I can't reuse the last occurance so free it */
2063         if (last) {
2064                 state->last_occurance = 0;
2065                 put_occurance(last);
2066         }
2067         /* Generate a new occurance structure */
2068         get_occurance(base);
2069         result = xmalloc(sizeof(*result), "occurance");
2070         result->count    = 2;
2071         result->filename = top->filename;
2072         result->function = top->function;
2073         result->line     = top->line;
2074         result->col      = top->col;
2075         result->parent   = base;
2076         state->last_occurance = result;
2077         return result;
2078 }
2079
2080 static struct occurance dummy_occurance = {
2081         .count    = 2,
2082         .filename = __FILE__,
2083         .function = "",
2084         .line     = __LINE__,
2085         .col      = 0,
2086         .parent   = 0,
2087 };
2088
2089 /* The undef triple is used as a place holder when we are removing pointers
2090  * from a triple.  Having allows certain sanity checks to pass even
2091  * when the original triple that was pointed to is gone.
2092  */
2093 static struct triple unknown_triple = {
2094         .next      = &unknown_triple,
2095         .prev      = &unknown_triple,
2096         .use       = 0,
2097         .op        = OP_UNKNOWNVAL,
2098         .lhs       = 0,
2099         .rhs       = 0,
2100         .misc      = 0,
2101         .targ      = 0,
2102         .type      = &unknown_type,
2103         .id        = -1, /* An invalid id */
2104         .u = { .cval = 0, },
2105         .occurance = &dummy_occurance,
2106         .param = { [0] = 0, [1] = 0, },
2107 };
2108
2109
2110 static size_t registers_of(struct compile_state *state, struct type *type);
2111
2112 static struct triple *alloc_triple(struct compile_state *state, 
2113         int op, struct type *type, int lhs_wanted, int rhs_wanted,
2114         struct occurance *occurance)
2115 {
2116         size_t size, extra_count, min_count;
2117         int lhs, rhs, misc, targ;
2118         struct triple *ret, dummy;
2119         dummy.op = op;
2120         dummy.occurance = occurance;
2121         valid_op(state, op);
2122         lhs = table_ops[op].lhs;
2123         rhs = table_ops[op].rhs;
2124         misc = table_ops[op].misc;
2125         targ = table_ops[op].targ;
2126
2127         switch(op) {
2128         case OP_FCALL:
2129                 rhs = rhs_wanted;
2130                 break;
2131         case OP_PHI:
2132                 rhs = rhs_wanted;
2133                 break;
2134         case OP_ADECL:
2135                 lhs = registers_of(state, type);
2136                 break;
2137         case OP_TUPLE:
2138                 lhs = registers_of(state, type);
2139                 break;
2140         case OP_ASM:
2141                 rhs = rhs_wanted;
2142                 lhs = lhs_wanted;
2143                 break;
2144         }
2145         if ((rhs < 0) || (rhs > MAX_RHS)) {
2146                 internal_error(state, &dummy, "bad rhs count %d", rhs);
2147         }
2148         if ((lhs < 0) || (lhs > MAX_LHS)) {
2149                 internal_error(state, &dummy, "bad lhs count %d", lhs);
2150         }
2151         if ((misc < 0) || (misc > MAX_MISC)) {
2152                 internal_error(state, &dummy, "bad misc count %d", misc);
2153         }
2154         if ((targ < 0) || (targ > MAX_TARG)) {
2155                 internal_error(state, &dummy, "bad targs count %d", targ);
2156         }
2157
2158         min_count = sizeof(ret->param)/sizeof(ret->param[0]);
2159         extra_count = lhs + rhs + misc + targ;
2160         extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2161
2162         size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2163         ret = xcmalloc(size, "tripple");
2164         ret->op        = op;
2165         ret->lhs       = lhs;
2166         ret->rhs       = rhs;
2167         ret->misc      = misc;
2168         ret->targ      = targ;
2169         ret->type      = type;
2170         ret->next      = ret;
2171         ret->prev      = ret;
2172         ret->occurance = occurance;
2173         /* A simple sanity check */
2174         if ((ret->op != op) ||
2175                 (ret->lhs != lhs) ||
2176                 (ret->rhs != rhs) ||
2177                 (ret->misc != misc) ||
2178                 (ret->targ != targ) ||
2179                 (ret->type != type) ||
2180                 (ret->next != ret) ||
2181                 (ret->prev != ret) ||
2182                 (ret->occurance != occurance)) {
2183                 internal_error(state, ret, "huh?");
2184         }
2185         return ret;
2186 }
2187
2188 struct triple *dup_triple(struct compile_state *state, struct triple *src)
2189 {
2190         struct triple *dup;
2191         int src_lhs, src_rhs, src_size;
2192         src_lhs = src->lhs;
2193         src_rhs = src->rhs;
2194         src_size = TRIPLE_SIZE(src);
2195         get_occurance(src->occurance);
2196         dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
2197                 src->occurance);
2198         memcpy(dup, src, sizeof(*src));
2199         memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
2200         return dup;
2201 }
2202
2203 static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2204 {
2205         struct triple *copy;
2206         copy = dup_triple(state, src);
2207         copy->use = 0;
2208         copy->next = copy->prev = copy;
2209         return copy;
2210 }
2211
2212 static struct triple *new_triple(struct compile_state *state, 
2213         int op, struct type *type, int lhs, int rhs)
2214 {
2215         struct triple *ret;
2216         struct occurance *occurance;
2217         occurance = new_occurance(state);
2218         ret = alloc_triple(state, op, type, lhs, rhs, occurance);
2219         return ret;
2220 }
2221
2222 static struct triple *build_triple(struct compile_state *state, 
2223         int op, struct type *type, struct triple *left, struct triple *right,
2224         struct occurance *occurance)
2225 {
2226         struct triple *ret;
2227         size_t count;
2228         ret = alloc_triple(state, op, type, -1, -1, occurance);
2229         count = TRIPLE_SIZE(ret);
2230         if (count > 0) {
2231                 ret->param[0] = left;
2232         }
2233         if (count > 1) {
2234                 ret->param[1] = right;
2235         }
2236         return ret;
2237 }
2238
2239 static struct triple *triple(struct compile_state *state, 
2240         int op, struct type *type, struct triple *left, struct triple *right)
2241 {
2242         struct triple *ret;
2243         size_t count;
2244         ret = new_triple(state, op, type, -1, -1);
2245         count = TRIPLE_SIZE(ret);
2246         if (count >= 1) {
2247                 ret->param[0] = left;
2248         }
2249         if (count >= 2) {
2250                 ret->param[1] = right;
2251         }
2252         return ret;
2253 }
2254
2255 static struct triple *branch(struct compile_state *state, 
2256         struct triple *targ, struct triple *test)
2257 {
2258         struct triple *ret;
2259         if (test) {
2260                 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
2261                 RHS(ret, 0) = test;
2262         } else {
2263                 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
2264         }
2265         TARG(ret, 0) = targ;
2266         /* record the branch target was used */
2267         if (!targ || (targ->op != OP_LABEL)) {
2268                 internal_error(state, 0, "branch not to label");
2269         }
2270         return ret;
2271 }
2272
2273 static int triple_is_label(struct compile_state *state, struct triple *ins);
2274 static int triple_is_call(struct compile_state *state, struct triple *ins);
2275 static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
2276 static void insert_triple(struct compile_state *state,
2277         struct triple *first, struct triple *ptr)
2278 {
2279         if (ptr) {
2280                 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
2281                         internal_error(state, ptr, "expression already used");
2282                 }
2283                 ptr->next       = first;
2284                 ptr->prev       = first->prev;
2285                 ptr->prev->next = ptr;
2286                 ptr->next->prev = ptr;
2287
2288                 if (triple_is_cbranch(state, ptr->prev) ||
2289                         triple_is_call(state, ptr->prev)) {
2290                         unuse_triple(first, ptr->prev);
2291                         use_triple(ptr, ptr->prev);
2292                 }
2293         }
2294 }
2295
2296 static int triple_stores_block(struct compile_state *state, struct triple *ins)
2297 {
2298         /* This function is used to determine if u.block 
2299          * is utilized to store the current block number.
2300          */
2301         int stores_block;
2302         valid_ins(state, ins);
2303         stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2304         return stores_block;
2305 }
2306
2307 static int triple_is_branch(struct compile_state *state, struct triple *ins);
2308 static struct block *block_of_triple(struct compile_state *state, 
2309         struct triple *ins)
2310 {
2311         struct triple *first;
2312         if (!ins || ins == &unknown_triple) {
2313                 return 0;
2314         }
2315         first = state->first;
2316         while(ins != first && !triple_is_branch(state, ins->prev) &&
2317                 !triple_stores_block(state, ins)) 
2318         { 
2319                 if (ins == ins->prev) {
2320                         internal_error(state, ins, "ins == ins->prev?");
2321                 }
2322                 ins = ins->prev;
2323         }
2324         return triple_stores_block(state, ins)? ins->u.block: 0;
2325 }
2326
2327 static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
2328 static struct triple *pre_triple(struct compile_state *state,
2329         struct triple *base,
2330         int op, struct type *type, struct triple *left, struct triple *right)
2331 {
2332         struct block *block;
2333         struct triple *ret;
2334         int i;
2335         /* If I am an OP_PIECE jump to the real instruction */
2336         if (base->op == OP_PIECE) {
2337                 base = MISC(base, 0);
2338         }
2339         block = block_of_triple(state, base);
2340         get_occurance(base->occurance);
2341         ret = build_triple(state, op, type, left, right, base->occurance);
2342         generate_lhs_pieces(state, ret);
2343         if (triple_stores_block(state, ret)) {
2344                 ret->u.block = block;
2345         }
2346         insert_triple(state, base, ret);
2347         for(i = 0; i < ret->lhs; i++) {
2348                 struct triple *piece;
2349                 piece = LHS(ret, i);
2350                 insert_triple(state, base, piece);
2351                 use_triple(ret, piece);
2352                 use_triple(piece, ret);
2353         }
2354         if (block && (block->first == base)) {
2355                 block->first = ret;
2356         }
2357         return ret;
2358 }
2359
2360 static struct triple *post_triple(struct compile_state *state,
2361         struct triple *base,
2362         int op, struct type *type, struct triple *left, struct triple *right)
2363 {
2364         struct block *block;
2365         struct triple *ret, *next;
2366         int zlhs, i;
2367         /* If I am an OP_PIECE jump to the real instruction */
2368         if (base->op == OP_PIECE) {
2369                 base = MISC(base, 0);
2370         }
2371         /* If I have a left hand side skip over it */
2372         zlhs = base->lhs;
2373         if (zlhs) {
2374                 base = LHS(base, zlhs - 1);
2375         }
2376
2377         block = block_of_triple(state, base);
2378         get_occurance(base->occurance);
2379         ret = build_triple(state, op, type, left, right, base->occurance);
2380         generate_lhs_pieces(state, ret);
2381         if (triple_stores_block(state, ret)) {
2382                 ret->u.block = block;
2383         }
2384         next = base->next;
2385         insert_triple(state, next, ret);
2386         zlhs = ret->lhs;
2387         for(i = 0; i < zlhs; i++) {
2388                 struct triple *piece;
2389                 piece = LHS(ret, i);
2390                 insert_triple(state, next, piece);
2391                 use_triple(ret, piece);
2392                 use_triple(piece, ret);
2393         }
2394         if (block && (block->last == base)) {
2395                 block->last = ret;
2396                 if (zlhs) {
2397                         block->last = LHS(ret, zlhs - 1);
2398                 }
2399         }
2400         return ret;
2401 }
2402
2403 static struct type *reg_type(
2404         struct compile_state *state, struct type *type, int reg);
2405
2406 static void generate_lhs_piece(
2407         struct compile_state *state, struct triple *ins, int index)
2408 {
2409         struct type *piece_type;
2410         struct triple *piece;
2411         get_occurance(ins->occurance);
2412         piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2413
2414         if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2415                 piece_type = piece_type->left;
2416         }
2417 #if 0
2418 {
2419         static void name_of(FILE *fp, struct type *type);
2420         FILE * fp = state->errout;
2421         fprintf(fp, "piece_type(%d): ", index);
2422         name_of(fp, piece_type);
2423         fprintf(fp, "\n");
2424 }
2425 #endif
2426         piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2427         piece->u.cval  = index;
2428         LHS(ins, piece->u.cval) = piece;
2429         MISC(piece, 0) = ins;
2430 }
2431
2432 static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2433 {
2434         int i, zlhs;
2435         zlhs = ins->lhs;
2436         for(i = 0; i < zlhs; i++) {
2437                 generate_lhs_piece(state, ins, i);
2438         }
2439 }
2440
2441 static struct triple *label(struct compile_state *state)
2442 {
2443         /* Labels don't get a type */
2444         struct triple *result;
2445         result = triple(state, OP_LABEL, &void_type, 0, 0);
2446         return result;
2447 }
2448
2449 static struct triple *mkprog(struct compile_state *state, ...)
2450 {
2451         struct triple *prog, *head, *arg;
2452         va_list args;
2453         int i;
2454
2455         head = label(state);
2456         prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2457         RHS(prog, 0) = head;
2458         va_start(args, state);
2459         i = 0;
2460         while((arg = va_arg(args, struct triple *)) != 0) {
2461                 if (++i >= 100) {
2462                         internal_error(state, 0, "too many arguments to mkprog");
2463                 }
2464                 flatten(state, head, arg);
2465         }
2466         va_end(args);
2467         prog->type = head->prev->type;
2468         return prog;
2469 }
2470 static void name_of(FILE *fp, struct type *type);
2471 static void display_triple(FILE *fp, struct triple *ins)
2472 {
2473         struct occurance *ptr;
2474         const char *reg;
2475         char pre, post, vol;
2476         pre = post = vol = ' ';
2477         if (ins) {
2478                 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2479                         pre = '^';
2480                 }
2481                 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2482                         post = ',';
2483                 }
2484                 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2485                         vol = 'v';
2486                 }
2487                 reg = arch_reg_str(ID_REG(ins->id));
2488         }
2489         if (ins == 0) {
2490                 fprintf(fp, "(%p) <nothing> ", ins);
2491         }
2492         else if (ins->op == OP_INTCONST) {
2493                 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx>         ",
2494                         ins, pre, post, vol, reg, ins->template_id, tops(ins->op), 
2495                         (unsigned long)(ins->u.cval));
2496         }
2497         else if (ins->op == OP_ADDRCONST) {
2498                 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2499                         ins, pre, post, vol, reg, ins->template_id, tops(ins->op), 
2500                         MISC(ins, 0), (unsigned long)(ins->u.cval));
2501         }
2502         else if (ins->op == OP_INDEX) {
2503                 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2504                         ins, pre, post, vol, reg, ins->template_id, tops(ins->op), 
2505                         RHS(ins, 0), (unsigned long)(ins->u.cval));
2506         }
2507         else if (ins->op == OP_PIECE) {
2508                 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2509                         ins, pre, post, vol, reg, ins->template_id, tops(ins->op), 
2510                         MISC(ins, 0), (unsigned long)(ins->u.cval));
2511         }
2512         else {
2513                 int i, count;
2514                 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s", 
2515                         ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2516                 if (table_ops[ins->op].flags & BITFIELD) {
2517                         fprintf(fp, " <%2d-%2d:%2d>", 
2518                                 ins->u.bitfield.offset,
2519                                 ins->u.bitfield.offset + ins->u.bitfield.size,
2520                                 ins->u.bitfield.size);
2521                 }
2522                 count = TRIPLE_SIZE(ins);
2523                 for(i = 0; i < count; i++) {
2524                         fprintf(fp, " %-10p", ins->param[i]);
2525                 }
2526                 for(; i < 2; i++) {
2527                         fprintf(fp, "           ");
2528                 }
2529         }
2530         if (ins) {
2531                 struct triple_set *user;
2532 #if DEBUG_DISPLAY_TYPES
2533                 fprintf(fp, " <");
2534                 name_of(fp, ins->type);
2535                 fprintf(fp, "> ");
2536 #endif
2537 #if DEBUG_DISPLAY_USES
2538                 fprintf(fp, " [");
2539                 for(user = ins->use; user; user = user->next) {
2540                         fprintf(fp, " %-10p", user->member);
2541                 }
2542                 fprintf(fp, " ]");
2543 #endif
2544                 fprintf(fp, " @");
2545                 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2546                         fprintf(fp, " %s,%s:%d.%d",
2547                                 ptr->function, 
2548                                 ptr->filename,
2549                                 ptr->line, 
2550                                 ptr->col);
2551                 }
2552                 if (ins->op == OP_ASM) {
2553                         fprintf(fp, "\n\t%s", ins->u.ainfo->str);
2554                 }
2555         }
2556         fprintf(fp, "\n");
2557         fflush(fp);
2558 }
2559
2560 static int equiv_types(struct type *left, struct type *right);
2561 static void display_triple_changes(
2562         FILE *fp, const struct triple *new, const struct triple *orig)
2563 {
2564
2565         int new_count, orig_count;
2566         new_count = TRIPLE_SIZE(new);
2567         orig_count = TRIPLE_SIZE(orig);
2568         if ((new->op != orig->op) ||
2569                 (new_count != orig_count) ||
2570                 (memcmp(orig->param, new->param,        
2571                         orig_count * sizeof(orig->param[0])) != 0) ||
2572                 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0)) 
2573         {
2574                 struct occurance *ptr;
2575                 int i, min_count, indent;
2576                 fprintf(fp, "(%p %p)", new, orig);
2577                 if (orig->op == new->op) {
2578                         fprintf(fp, " %-11s", tops(orig->op));
2579                 } else {
2580                         fprintf(fp, " [%-10s %-10s]", 
2581                                 tops(new->op), tops(orig->op));
2582                 }
2583                 min_count = new_count;
2584                 if (min_count > orig_count) {
2585                         min_count = orig_count;
2586                 }
2587                 for(indent = i = 0; i < min_count; i++) {
2588                         if (orig->param[i] == new->param[i]) {
2589                                 fprintf(fp, " %-11p", 
2590                                         orig->param[i]);
2591                                 indent += 12;
2592                         } else {
2593                                 fprintf(fp, " [%-10p %-10p]",
2594                                         new->param[i], 
2595                                         orig->param[i]);
2596                                 indent += 24;
2597                         }
2598                 }
2599                 for(; i < orig_count; i++) {
2600                         fprintf(fp, " [%-9p]", orig->param[i]);
2601                         indent += 12;
2602                 }
2603                 for(; i < new_count; i++) {
2604                         fprintf(fp, " [%-9p]", new->param[i]);
2605                         indent += 12;
2606                 }
2607                 if ((new->op == OP_INTCONST)||
2608                         (new->op == OP_ADDRCONST)) {
2609                         fprintf(fp, " <0x%08lx>", 
2610                                 (unsigned long)(new->u.cval));
2611                         indent += 13;
2612                 }
2613                 for(;indent < 36; indent++) {
2614                         putc(' ', fp);
2615                 }
2616
2617 #if DEBUG_DISPLAY_TYPES
2618                 fprintf(fp, " <");
2619                 name_of(fp, new->type);
2620                 if (!equiv_types(new->type, orig->type)) {
2621                         fprintf(fp, " -- ");
2622                         name_of(fp, orig->type);
2623                 }
2624                 fprintf(fp, "> ");
2625 #endif
2626
2627                 fprintf(fp, " @");
2628                 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2629                         fprintf(fp, " %s,%s:%d.%d",
2630                                 ptr->function, 
2631                                 ptr->filename,
2632                                 ptr->line, 
2633                                 ptr->col);
2634                         
2635                 }
2636                 fprintf(fp, "\n");
2637                 fflush(fp);
2638         }
2639 }
2640
2641 static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
2642 {
2643         /* Does the triple have no side effects.
2644          * I.e. Rexecuting the triple with the same arguments 
2645          * gives the same value.
2646          */
2647         unsigned pure;
2648         valid_ins(state, ins);
2649         pure = PURE_BITS(table_ops[ins->op].flags);
2650         if ((pure != PURE) && (pure != IMPURE)) {
2651                 internal_error(state, 0, "Purity of %s not known",
2652                         tops(ins->op));
2653         }
2654         return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
2655 }
2656
2657 static int triple_is_branch_type(struct compile_state *state, 
2658         struct triple *ins, unsigned type)
2659 {
2660         /* Is this one of the passed branch types? */
2661         valid_ins(state, ins);
2662         return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2663 }
2664
2665 static int triple_is_branch(struct compile_state *state, struct triple *ins)
2666 {
2667         /* Is this triple a branch instruction? */
2668         valid_ins(state, ins);
2669         return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
2670 }
2671
2672 static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
2673 {
2674         /* Is this triple a conditional branch instruction? */
2675         return triple_is_branch_type(state, ins, CBRANCH);
2676 }
2677
2678 static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
2679 {
2680         /* Is this triple a unconditional branch instruction? */
2681         unsigned type;
2682         valid_ins(state, ins);
2683         type = BRANCH_BITS(table_ops[ins->op].flags);
2684         return (type != 0) && (type != CBRANCH);
2685 }
2686
2687 static int triple_is_call(struct compile_state *state, struct triple *ins)
2688 {
2689         /* Is this triple a call instruction? */
2690         return triple_is_branch_type(state, ins, CALLBRANCH);
2691 }
2692
2693 static int triple_is_ret(struct compile_state *state, struct triple *ins)
2694 {
2695         /* Is this triple a return instruction? */
2696         return triple_is_branch_type(state, ins, RETBRANCH);
2697 }
2698
2699 static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2700 {
2701         /* Is this triple an unconditional branch and not a call or a
2702          * return? */
2703         return triple_is_branch_type(state, ins, UBRANCH);
2704 }
2705
2706 static int triple_is_end(struct compile_state *state, struct triple *ins)
2707 {
2708         return triple_is_branch_type(state, ins, ENDBRANCH);
2709 }
2710
2711 static int triple_is_label(struct compile_state *state, struct triple *ins)
2712 {
2713         valid_ins(state, ins);
2714         return (ins->op == OP_LABEL);
2715 }
2716
2717 static struct triple *triple_to_block_start(
2718         struct compile_state *state, struct triple *start)
2719 {
2720         while(!triple_is_branch(state, start->prev) &&
2721                 (!triple_is_label(state, start) || !start->use)) {
2722                 start = start->prev;
2723         }
2724         return start;
2725 }
2726
2727 static int triple_is_def(struct compile_state *state, struct triple *ins)
2728 {
2729         /* This function is used to determine which triples need
2730          * a register.
2731          */
2732         int is_def;
2733         valid_ins(state, ins);
2734         is_def = (table_ops[ins->op].flags & DEF) == DEF;
2735         if (ins->lhs >= 1) {
2736                 is_def = 0;
2737         }
2738         return is_def;
2739 }
2740
2741 static int triple_is_structural(struct compile_state *state, struct triple *ins)
2742 {
2743         int is_structural;
2744         valid_ins(state, ins);
2745         is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2746         return is_structural;
2747 }
2748
2749 static int triple_is_part(struct compile_state *state, struct triple *ins)
2750 {
2751         int is_part;
2752         valid_ins(state, ins);
2753         is_part = (table_ops[ins->op].flags & PART) == PART;
2754         return is_part;
2755 }
2756
2757 static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2758 {
2759         return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2760 }
2761
2762 static struct triple **triple_iter(struct compile_state *state,
2763         size_t count, struct triple **vector,
2764         struct triple *ins, struct triple **last)
2765 {
2766         struct triple **ret;
2767         ret = 0;
2768         if (count) {
2769                 if (!last) {
2770                         ret = vector;
2771                 }
2772                 else if ((last >= vector) && (last < (vector + count - 1))) {
2773                         ret = last + 1;
2774                 }
2775         }
2776         return ret;
2777         
2778 }
2779
2780 static struct triple **triple_lhs(struct compile_state *state,
2781         struct triple *ins, struct triple **last)
2782 {
2783         return triple_iter(state, ins->lhs, &LHS(ins,0), 
2784                 ins, last);
2785 }
2786
2787 static struct triple **triple_rhs(struct compile_state *state,
2788         struct triple *ins, struct triple **last)
2789 {
2790         return triple_iter(state, ins->rhs, &RHS(ins,0), 
2791                 ins, last);
2792 }
2793
2794 static struct triple **triple_misc(struct compile_state *state,
2795         struct triple *ins, struct triple **last)
2796 {
2797         return triple_iter(state, ins->misc, &MISC(ins,0), 
2798                 ins, last);
2799 }
2800
2801 static struct triple **do_triple_targ(struct compile_state *state,
2802         struct triple *ins, struct triple **last, int call_edges, int next_edges)
2803 {
2804         size_t count;
2805         struct triple **ret, **vector;
2806         int next_is_targ;
2807         ret = 0;
2808         count = ins->targ;
2809         next_is_targ = 0;
2810         if (triple_is_cbranch(state, ins)) {
2811                 next_is_targ = 1;
2812         }
2813         if (!call_edges && triple_is_call(state, ins)) {
2814                 count = 0;
2815         }
2816         if (next_edges && triple_is_call(state, ins)) {
2817                 next_is_targ = 1;
2818         }
2819         vector = &TARG(ins, 0);
2820         if (!ret && next_is_targ) {
2821                 if (!last) {
2822                         ret = &ins->next;
2823                 } else if (last == &ins->next) {
2824                         last = 0;
2825                 }
2826         }
2827         if (!ret && count) {
2828                 if (!last) {
2829                         ret = vector;
2830                 }
2831                 else if ((last >= vector) && (last < (vector + count - 1))) {
2832                         ret = last + 1;
2833                 }
2834                 else if (last == vector + count - 1) {
2835                         last = 0;
2836                 }
2837         }
2838         if (!ret && triple_is_ret(state, ins) && call_edges) {
2839                 struct triple_set *use;
2840                 for(use = ins->use; use; use = use->next) {
2841                         if (!triple_is_call(state, use->member)) {
2842                                 continue;
2843                         }
2844                         if (!last) {
2845                                 ret = &use->member->next;
2846                                 break;
2847                         }
2848                         else if (last == &use->member->next) {
2849                                 last = 0;
2850                         }
2851                 }
2852         }
2853         return ret;
2854 }
2855
2856 static struct triple **triple_targ(struct compile_state *state,
2857         struct triple *ins, struct triple **last)
2858 {
2859         return do_triple_targ(state, ins, last, 1, 1);
2860 }
2861
2862 static struct triple **triple_edge_targ(struct compile_state *state,
2863         struct triple *ins, struct triple **last)
2864 {
2865         return do_triple_targ(state, ins, last, 
2866                 state->functions_joined, !state->functions_joined);
2867 }
2868
2869 static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2870 {
2871         struct triple *next;
2872         int lhs, i;
2873         lhs = ins->lhs;
2874         next = ins->next;
2875         for(i = 0; i < lhs; i++) {
2876                 struct triple *piece;
2877                 piece = LHS(ins, i);
2878                 if (next != piece) {
2879                         internal_error(state, ins, "malformed lhs on %s",
2880                                 tops(ins->op));
2881                 }
2882                 if (next->op != OP_PIECE) {
2883                         internal_error(state, ins, "bad lhs op %s at %d on %s",
2884                                 tops(next->op), i, tops(ins->op));
2885                 }
2886                 if (next->u.cval != i) {
2887                         internal_error(state, ins, "bad u.cval of %d %d expected",
2888                                 next->u.cval, i);
2889                 }
2890                 next = next->next;
2891         }
2892         return next;
2893 }
2894
2895 /* Function piece accessor functions */
2896 static struct triple *do_farg(struct compile_state *state, 
2897         struct triple *func, unsigned index)
2898 {
2899         struct type *ftype;
2900         struct triple *first, *arg;
2901         unsigned i;
2902
2903         ftype = func->type;
2904         if((index < 0) || (index >= (ftype->elements + 2))) {
2905                 internal_error(state, func, "bad argument index: %d", index);
2906         }
2907         first = RHS(func, 0);
2908         arg = first->next;
2909         for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2910                 /* do nothing */
2911         }
2912         if (arg->op != OP_ADECL) {
2913                 internal_error(state, 0, "arg not adecl?");
2914         }
2915         return arg;
2916 }
2917 static struct triple *fresult(struct compile_state *state, struct triple *func)
2918 {
2919         return do_farg(state, func, 0);
2920 }
2921 static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2922 {
2923         return do_farg(state, func, 1);
2924 }
2925 static struct triple *farg(struct compile_state *state, 
2926         struct triple *func, unsigned index)
2927 {
2928         return do_farg(state, func, index + 2);
2929 }
2930
2931
2932 static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2933 {
2934         struct triple *first, *ins;
2935         fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2936         first = ins = RHS(func, 0);
2937         do {
2938                 if (triple_is_label(state, ins) && ins->use) {
2939                         fprintf(fp, "%p:\n", ins);
2940                 }
2941                 display_triple(fp, ins);
2942
2943                 if (triple_is_branch(state, ins)) {
2944                         fprintf(fp, "\n");
2945                 }
2946                 if (ins->next->prev != ins) {
2947                         internal_error(state, ins->next, "bad prev");
2948                 }
2949                 ins = ins->next;
2950         } while(ins != first);
2951 }
2952
2953 static void verify_use(struct compile_state *state,
2954         struct triple *user, struct triple *used)
2955 {
2956         int size, i;
2957         size = TRIPLE_SIZE(user);
2958         for(i = 0; i < size; i++) {
2959                 if (user->param[i] == used) {
2960                         break;
2961                 }
2962         }
2963         if (triple_is_branch(state, user)) {
2964                 if (user->next == used) {
2965                         i = -1;
2966                 }
2967         }
2968         if (i == size) {
2969                 internal_error(state, user, "%s(%p) does not use %s(%p)",
2970                         tops(user->op), user, tops(used->op), used);
2971         }
2972 }
2973
2974 static int find_rhs_use(struct compile_state *state, 
2975         struct triple *user, struct triple *used)
2976 {
2977         struct triple **param;
2978         int size, i;
2979         verify_use(state, user, used);
2980 #warning "AUDIT ME ->rhs"
2981         size = user->rhs;
2982         param = &RHS(user, 0);
2983         for(i = 0; i < size; i++) {
2984                 if (param[i] == used) {
2985                         return i;
2986                 }
2987         }
2988         return -1;
2989 }
2990
2991 static void free_triple(struct compile_state *state, struct triple *ptr)
2992 {
2993         size_t size;
2994         size = sizeof(*ptr) - sizeof(ptr->param) +
2995                 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
2996         ptr->prev->next = ptr->next;
2997         ptr->next->prev = ptr->prev;
2998         if (ptr->use) {
2999                 internal_error(state, ptr, "ptr->use != 0");
3000         }
3001         put_occurance(ptr->occurance);
3002         memset(ptr, -1, size);
3003         xfree(ptr);
3004 }
3005
3006 static void release_triple(struct compile_state *state, struct triple *ptr)
3007 {
3008         struct triple_set *set, *next;
3009         struct triple **expr;
3010         struct block *block;
3011         if (ptr == &unknown_triple) {
3012                 return;
3013         }
3014         valid_ins(state, ptr);
3015         /* Make certain the we are not the first or last element of a block */
3016         block = block_of_triple(state, ptr);
3017         if (block) {
3018                 if ((block->last == ptr) && (block->first == ptr)) {
3019                         block->last = block->first = 0;
3020                 }
3021                 else if (block->last == ptr) {
3022                         block->last = ptr->prev;
3023                 }
3024                 else if (block->first == ptr) {
3025                         block->first = ptr->next;
3026                 }
3027         }
3028         /* Remove ptr from use chains where it is the user */
3029         expr = triple_rhs(state, ptr, 0);
3030         for(; expr; expr = triple_rhs(state, ptr, expr)) {
3031                 if (*expr) {
3032                         unuse_triple(*expr, ptr);
3033                 }
3034         }
3035         expr = triple_lhs(state, ptr, 0);
3036         for(; expr; expr = triple_lhs(state, ptr, expr)) {
3037                 if (*expr) {
3038                         unuse_triple(*expr, ptr);
3039                 }
3040         }
3041         expr = triple_misc(state, ptr, 0);
3042         for(; expr; expr = triple_misc(state, ptr, expr)) {
3043                 if (*expr) {
3044                         unuse_triple(*expr, ptr);
3045                 }
3046         }
3047         expr = triple_targ(state, ptr, 0);
3048         for(; expr; expr = triple_targ(state, ptr, expr)) {
3049                 if (*expr){
3050                         unuse_triple(*expr, ptr);
3051                 }
3052         }
3053         /* Reomve ptr from use chains where it is used */
3054         for(set = ptr->use; set; set = next) {
3055                 next = set->next;
3056                 valid_ins(state, set->member);
3057                 expr = triple_rhs(state, set->member, 0);
3058                 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3059                         if (*expr == ptr) {
3060                                 *expr = &unknown_triple;
3061                         }
3062                 }
3063                 expr = triple_lhs(state, set->member, 0);
3064                 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3065                         if (*expr == ptr) {
3066                                 *expr = &unknown_triple;
3067                         }
3068                 }
3069                 expr = triple_misc(state, set->member, 0);
3070                 for(; expr; expr = triple_misc(state, set->member, expr)) {
3071                         if (*expr == ptr) {
3072                                 *expr = &unknown_triple;
3073                         }
3074                 }
3075                 expr = triple_targ(state, set->member, 0);
3076                 for(; expr; expr = triple_targ(state, set->member, expr)) {
3077                         if (*expr == ptr) {
3078                                 *expr = &unknown_triple;
3079                         }
3080                 }
3081                 unuse_triple(ptr, set->member);
3082         }
3083         free_triple(state, ptr);
3084 }
3085
3086 static void print_triples(struct compile_state *state);
3087 static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
3088
3089 #define TOK_UNKNOWN     0
3090 #define TOK_SPACE       1
3091 #define TOK_SEMI        2
3092 #define TOK_LBRACE      3
3093 #define TOK_RBRACE      4
3094 #define TOK_COMMA       5
3095 #define TOK_EQ          6
3096 #define TOK_COLON       7
3097 #define TOK_LBRACKET    8
3098 #define TOK_RBRACKET    9
3099 #define TOK_LPAREN      10
3100 #define TOK_RPAREN      11
3101 #define TOK_STAR        12
3102 #define TOK_DOTS        13
3103 #define TOK_MORE        14
3104 #define TOK_LESS        15
3105 #define TOK_TIMESEQ     16
3106 #define TOK_DIVEQ       17
3107 #define TOK_MODEQ       18
3108 #define TOK_PLUSEQ      19
3109 #define TOK_MINUSEQ     20
3110 #define TOK_SLEQ        21
3111 #define TOK_SREQ        22
3112 #define TOK_ANDEQ       23
3113 #define TOK_XOREQ       24
3114 #define TOK_OREQ        25
3115 #define TOK_EQEQ        26
3116 #define TOK_NOTEQ       27
3117 #define TOK_QUEST       28
3118 #define TOK_LOGOR       29
3119 #define TOK_LOGAND      30
3120 #define TOK_OR          31
3121 #define TOK_AND         32
3122 #define TOK_XOR         33
3123 #define TOK_LESSEQ      34
3124 #define TOK_MOREEQ      35
3125 #define TOK_SL          36
3126 #define TOK_SR          37
3127 #define TOK_PLUS        38
3128 #define TOK_MINUS       39
3129 #define TOK_DIV         40
3130 #define TOK_MOD         41
3131 #define TOK_PLUSPLUS    42
3132 #define TOK_MINUSMINUS  43
3133 #define TOK_BANG        44
3134 #define TOK_ARROW       45
3135 #define TOK_DOT         46
3136 #define TOK_TILDE       47
3137 #define TOK_LIT_STRING  48
3138 #define TOK_LIT_CHAR    49
3139 #define TOK_LIT_INT     50
3140 #define TOK_LIT_FLOAT   51
3141 #define TOK_MACRO       52
3142 #define TOK_CONCATENATE 53
3143
3144 #define TOK_IDENT       54
3145 #define TOK_STRUCT_NAME 55
3146 #define TOK_ENUM_CONST  56
3147 #define TOK_TYPE_NAME   57
3148
3149 #define TOK_AUTO        58
3150 #define TOK_BREAK       59
3151 #define TOK_CASE        60
3152 #define TOK_CHAR        61
3153 #define TOK_CONST       62
3154 #define TOK_CONTINUE    63
3155 #define TOK_DEFAULT     64
3156 #define TOK_DO          65
3157 #define TOK_DOUBLE      66
3158 #define TOK_ELSE        67
3159 #define TOK_ENUM        68
3160 #define TOK_EXTERN      69
3161 #define TOK_FLOAT       70
3162 #define TOK_FOR         71
3163 #define TOK_GOTO        72
3164 #define TOK_IF          73
3165 #define TOK_INLINE      74
3166 #define TOK_INT         75
3167 #define TOK_LONG        76
3168 #define TOK_REGISTER    77
3169 #define TOK_RESTRICT    78
3170 #define TOK_RETURN      79
3171 #define TOK_SHORT       80
3172 #define TOK_SIGNED      81
3173 #define TOK_SIZEOF      82
3174 #define TOK_STATIC      83
3175 #define TOK_STRUCT      84
3176 #define TOK_SWITCH      85
3177 #define TOK_TYPEDEF     86
3178 #define TOK_UNION       87
3179 #define TOK_UNSIGNED    88
3180 #define TOK_VOID        89
3181 #define TOK_VOLATILE    90
3182 #define TOK_WHILE       91
3183 #define TOK_ASM         92
3184 #define TOK_ATTRIBUTE   93
3185 #define TOK_ALIGNOF     94
3186 #define TOK_FIRST_KEYWORD TOK_AUTO
3187 #define TOK_LAST_KEYWORD  TOK_ALIGNOF
3188
3189 #define TOK_MDEFINE     100
3190 #define TOK_MDEFINED    101
3191 #define TOK_MUNDEF      102
3192 #define TOK_MINCLUDE    103
3193 #define TOK_MLINE       104
3194 #define TOK_MERROR      105
3195 #define TOK_MWARNING    106
3196 #define TOK_MPRAGMA     107
3197 #define TOK_MIFDEF      108
3198 #define TOK_MIFNDEF     109
3199 #define TOK_MELIF       110
3200 #define TOK_MENDIF      111
3201
3202 #define TOK_FIRST_MACRO TOK_MDEFINE
3203 #define TOK_LAST_MACRO  TOK_MENDIF
3204          
3205 #define TOK_MIF         112
3206 #define TOK_MELSE       113
3207 #define TOK_MIDENT      114
3208
3209 #define TOK_EOL         115
3210 #define TOK_EOF         116
3211
3212 static const char *tokens[] = {
3213 [TOK_UNKNOWN     ] = ":unknown:",
3214 [TOK_SPACE       ] = ":space:",
3215 [TOK_SEMI        ] = ";",
3216 [TOK_LBRACE      ] = "{",
3217 [TOK_RBRACE      ] = "}",
3218 [TOK_COMMA       ] = ",",
3219 [TOK_EQ          ] = "=",
3220 [TOK_COLON       ] = ":",
3221 [TOK_LBRACKET    ] = "[",
3222 [TOK_RBRACKET    ] = "]",
3223 [TOK_LPAREN      ] = "(",
3224 [TOK_RPAREN      ] = ")",
3225 [TOK_STAR        ] = "*",
3226 [TOK_DOTS        ] = "...",
3227 [TOK_MORE        ] = ">",
3228 [TOK_LESS        ] = "<",
3229 [TOK_TIMESEQ     ] = "*=",
3230 [TOK_DIVEQ       ] = "/=",
3231 [TOK_MODEQ       ] = "%=",
3232 [TOK_PLUSEQ      ] = "+=",
3233 [TOK_MINUSEQ     ] = "-=",
3234 [TOK_SLEQ        ] = "<<=",
3235 [TOK_SREQ        ] = ">>=",
3236 [TOK_ANDEQ       ] = "&=",
3237 [TOK_XOREQ       ] = "^=",
3238 [TOK_OREQ        ] = "|=",
3239 [TOK_EQEQ        ] = "==",
3240 [TOK_NOTEQ       ] = "!=",
3241 [TOK_QUEST       ] = "?",
3242 [TOK_LOGOR       ] = "||",
3243 [TOK_LOGAND      ] = "&&",
3244 [TOK_OR          ] = "|",
3245 [TOK_AND         ] = "&",
3246 [TOK_XOR         ] = "^",
3247 [TOK_LESSEQ      ] = "<=",
3248 [TOK_MOREEQ      ] = ">=",
3249 [TOK_SL          ] = "<<",
3250 [TOK_SR          ] = ">>",
3251 [TOK_PLUS        ] = "+",
3252 [TOK_MINUS       ] = "-",
3253 [TOK_DIV         ] = "/",
3254 [TOK_MOD         ] = "%",
3255 [TOK_PLUSPLUS    ] = "++",
3256 [TOK_MINUSMINUS  ] = "--",
3257 [TOK_BANG        ] = "!",
3258 [TOK_ARROW       ] = "->",
3259 [TOK_DOT         ] = ".",
3260 [TOK_TILDE       ] = "~",
3261 [TOK_LIT_STRING  ] = ":string:",
3262 [TOK_IDENT       ] = ":ident:",
3263 [TOK_TYPE_NAME   ] = ":typename:",
3264 [TOK_LIT_CHAR    ] = ":char:",
3265 [TOK_LIT_INT     ] = ":integer:",
3266 [TOK_LIT_FLOAT   ] = ":float:",
3267 [TOK_MACRO       ] = "#",
3268 [TOK_CONCATENATE ] = "##",
3269
3270 [TOK_AUTO        ] = "auto",
3271 [TOK_BREAK       ] = "break",
3272 [TOK_CASE        ] = "case",
3273 [TOK_CHAR        ] = "char",
3274 [TOK_CONST       ] = "const",
3275 [TOK_CONTINUE    ] = "continue",
3276 [TOK_DEFAULT     ] = "default",
3277 [TOK_DO          ] = "do",
3278 [TOK_DOUBLE      ] = "double",
3279 [TOK_ELSE        ] = "else",
3280 [TOK_ENUM        ] = "enum",
3281 [TOK_EXTERN      ] = "extern",
3282 [TOK_FLOAT       ] = "float",
3283 [TOK_FOR         ] = "for",
3284 [TOK_GOTO        ] = "goto",
3285 [TOK_IF          ] = "if",
3286 [TOK_INLINE      ] = "inline",
3287 [TOK_INT         ] = "int",
3288 [TOK_LONG        ] = "long",
3289 [TOK_REGISTER    ] = "register",
3290 [TOK_RESTRICT    ] = "restrict",
3291 [TOK_RETURN      ] = "return",
3292 [TOK_SHORT       ] = "short",
3293 [TOK_SIGNED      ] = "signed",
3294 [TOK_SIZEOF      ] = "sizeof",
3295 [TOK_STATIC      ] = "static",
3296 [TOK_STRUCT      ] = "struct",
3297 [TOK_SWITCH      ] = "switch",
3298 [TOK_TYPEDEF     ] = "typedef",
3299 [TOK_UNION       ] = "union",
3300 [TOK_UNSIGNED    ] = "unsigned",
3301 [TOK_VOID        ] = "void",
3302 [TOK_VOLATILE    ] = "volatile",
3303 [TOK_WHILE       ] = "while",
3304 [TOK_ASM         ] = "asm",
3305 [TOK_ATTRIBUTE   ] = "__attribute__",
3306 [TOK_ALIGNOF     ] = "__alignof__",
3307
3308 [TOK_MDEFINE     ] = "#define",
3309 [TOK_MDEFINED    ] = "#defined",
3310 [TOK_MUNDEF      ] = "#undef",
3311 [TOK_MINCLUDE    ] = "#include",
3312 [TOK_MLINE       ] = "#line",
3313 [TOK_MERROR      ] = "#error",
3314 [TOK_MWARNING    ] = "#warning",
3315 [TOK_MPRAGMA     ] = "#pragma",
3316 [TOK_MIFDEF      ] = "#ifdef",
3317 [TOK_MIFNDEF     ] = "#ifndef",
3318 [TOK_MELIF       ] = "#elif",
3319 [TOK_MENDIF      ] = "#endif",
3320
3321 [TOK_MIF         ] = "#if",
3322 [TOK_MELSE       ] = "#else",
3323 [TOK_MIDENT      ] = "#:ident:",
3324 [TOK_EOL         ] = "EOL", 
3325 [TOK_EOF         ] = "EOF",
3326 };
3327
3328 static unsigned int hash(const char *str, int str_len)
3329 {
3330         unsigned int hash;
3331         const char *end;
3332         end = str + str_len;
3333         hash = 0;
3334         for(; str < end; str++) {
3335                 hash = (hash *263) + *str;
3336         }
3337         hash = hash & (HASH_TABLE_SIZE -1);
3338         return hash;
3339 }
3340
3341 static struct hash_entry *lookup(
3342         struct compile_state *state, const char *name, int name_len)
3343 {
3344         struct hash_entry *entry;
3345         unsigned int index;
3346         index = hash(name, name_len);
3347         entry = state->hash_table[index];
3348         while(entry && 
3349                 ((entry->name_len != name_len) ||
3350                         (memcmp(entry->name, name, name_len) != 0))) {
3351                 entry = entry->next;
3352         }
3353         if (!entry) {
3354                 char *new_name;
3355                 /* Get a private copy of the name */
3356                 new_name = xmalloc(name_len + 1, "hash_name");
3357                 memcpy(new_name, name, name_len);
3358                 new_name[name_len] = '\0';
3359
3360                 /* Create a new hash entry */
3361                 entry = xcmalloc(sizeof(*entry), "hash_entry");
3362                 entry->next = state->hash_table[index];
3363                 entry->name = new_name;
3364                 entry->name_len = name_len;
3365
3366                 /* Place the new entry in the hash table */
3367                 state->hash_table[index] = entry;
3368         }
3369         return entry;
3370 }
3371
3372 static void ident_to_keyword(struct compile_state *state, struct token *tk)
3373 {
3374         struct hash_entry *entry;
3375         entry = tk->ident;
3376         if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3377                 (entry->tok == TOK_ENUM_CONST) ||
3378                 ((entry->tok >= TOK_FIRST_KEYWORD) && 
3379                         (entry->tok <= TOK_LAST_KEYWORD)))) {
3380                 tk->tok = entry->tok;
3381         }
3382 }
3383
3384 static void ident_to_macro(struct compile_state *state, struct token *tk)
3385 {
3386         struct hash_entry *entry;
3387         entry = tk->ident;
3388         if (!entry)
3389                 return;
3390         if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
3391                 tk->tok = entry->tok;
3392         }
3393         else if (entry->tok == TOK_IF) {
3394                 tk->tok = TOK_MIF;
3395         }
3396         else if (entry->tok == TOK_ELSE) {
3397                 tk->tok = TOK_MELSE;
3398         }
3399         else {
3400                 tk->tok = TOK_MIDENT;
3401         }
3402 }
3403
3404 static void hash_keyword(
3405         struct compile_state *state, const char *keyword, int tok)
3406 {
3407         struct hash_entry *entry;
3408         entry = lookup(state, keyword, strlen(keyword));
3409         if (entry && entry->tok != TOK_UNKNOWN) {
3410                 die("keyword %s already hashed", keyword);
3411         }
3412         entry->tok  = tok;
3413 }
3414
3415 static void romcc_symbol(
3416         struct compile_state *state, struct hash_entry *ident,
3417         struct symbol **chain, struct triple *def, struct type *type, int depth)
3418 {
3419         struct symbol *sym;
3420         if (*chain && ((*chain)->scope_depth >= depth)) {
3421                 error(state, 0, "%s already defined", ident->name);
3422         }
3423         sym = xcmalloc(sizeof(*sym), "symbol");
3424         sym->ident = ident;
3425         sym->def   = def;
3426         sym->type  = type;
3427         sym->scope_depth = depth;
3428         sym->next = *chain;
3429         *chain    = sym;
3430 }
3431
3432 static void symbol(
3433         struct compile_state *state, struct hash_entry *ident,
3434         struct symbol **chain, struct triple *def, struct type *type)
3435 {
3436         romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3437 }
3438
3439 static void var_symbol(struct compile_state *state, 
3440         struct hash_entry *ident, struct triple *def)
3441 {
3442         if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3443                 internal_error(state, 0, "bad var type");
3444         }
3445         symbol(state, ident, &ident->sym_ident, def, def->type);
3446 }
3447
3448 static void label_symbol(struct compile_state *state, 
3449         struct hash_entry *ident, struct triple *label, int depth)
3450 {
3451         romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
3452 }
3453
3454 static void start_scope(struct compile_state *state)
3455 {
3456         state->scope_depth++;
3457 }
3458
3459 static void end_scope_syms(struct compile_state *state,
3460         struct symbol **chain, int depth)
3461 {
3462         struct symbol *sym, *next;
3463         sym = *chain;
3464         while(sym && (sym->scope_depth == depth)) {
3465                 next = sym->next;
3466                 xfree(sym);
3467                 sym = next;
3468         }
3469         *chain = sym;
3470 }
3471
3472 static void end_scope(struct compile_state *state)
3473 {
3474         int i;
3475         int depth;
3476         /* Walk through the hash table and remove all symbols
3477          * in the current scope. 
3478          */
3479         depth = state->scope_depth;
3480         for(i = 0; i < HASH_TABLE_SIZE; i++) {
3481                 struct hash_entry *entry;
3482                 entry = state->hash_table[i];
3483                 while(entry) {
3484                         end_scope_syms(state, &entry->sym_label, depth);
3485                         end_scope_syms(state, &entry->sym_tag,   depth);
3486                         end_scope_syms(state, &entry->sym_ident, depth);
3487                         entry = entry->next;
3488                 }
3489         }
3490         state->scope_depth = depth - 1;
3491 }
3492
3493 static void register_keywords(struct compile_state *state)
3494 {
3495         hash_keyword(state, "auto",          TOK_AUTO);
3496         hash_keyword(state, "break",         TOK_BREAK);
3497         hash_keyword(state, "case",          TOK_CASE);
3498         hash_keyword(state, "char",          TOK_CHAR);
3499         hash_keyword(state, "const",         TOK_CONST);
3500         hash_keyword(state, "continue",      TOK_CONTINUE);
3501         hash_keyword(state, "default",       TOK_DEFAULT);
3502         hash_keyword(state, "do",            TOK_DO);
3503         hash_keyword(state, "double",        TOK_DOUBLE);
3504         hash_keyword(state, "else",          TOK_ELSE);
3505         hash_keyword(state, "enum",          TOK_ENUM);
3506         hash_keyword(state, "extern",        TOK_EXTERN);
3507         hash_keyword(state, "float",         TOK_FLOAT);
3508         hash_keyword(state, "for",           TOK_FOR);
3509         hash_keyword(state, "goto",          TOK_GOTO);
3510         hash_keyword(state, "if",            TOK_IF);
3511         hash_keyword(state, "inline",        TOK_INLINE);
3512         hash_keyword(state, "int",           TOK_INT);
3513         hash_keyword(state, "long",          TOK_LONG);
3514         hash_keyword(state, "register",      TOK_REGISTER);
3515         hash_keyword(state, "restrict",      TOK_RESTRICT);
3516         hash_keyword(state, "return",        TOK_RETURN);
3517         hash_keyword(state, "short",         TOK_SHORT);
3518         hash_keyword(state, "signed",        TOK_SIGNED);
3519         hash_keyword(state, "sizeof",        TOK_SIZEOF);
3520         hash_keyword(state, "static",        TOK_STATIC);
3521         hash_keyword(state, "struct",        TOK_STRUCT);
3522         hash_keyword(state, "switch",        TOK_SWITCH);
3523         hash_keyword(state, "typedef",       TOK_TYPEDEF);
3524         hash_keyword(state, "union",         TOK_UNION);
3525         hash_keyword(state, "unsigned",      TOK_UNSIGNED);
3526         hash_keyword(state, "void",          TOK_VOID);
3527         hash_keyword(state, "volatile",      TOK_VOLATILE);
3528         hash_keyword(state, "__volatile__",  TOK_VOLATILE);
3529         hash_keyword(state, "while",         TOK_WHILE);
3530         hash_keyword(state, "asm",           TOK_ASM);
3531         hash_keyword(state, "__asm__",       TOK_ASM);
3532         hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3533         hash_keyword(state, "__alignof__",   TOK_ALIGNOF);
3534 }
3535
3536 static void register_macro_keywords(struct compile_state *state)
3537 {
3538         hash_keyword(state, "define",        TOK_MDEFINE);
3539         hash_keyword(state, "defined",       TOK_MDEFINED);
3540         hash_keyword(state, "undef",         TOK_MUNDEF);
3541         hash_keyword(state, "include",       TOK_MINCLUDE);
3542         hash_keyword(state, "line",          TOK_MLINE);
3543         hash_keyword(state, "error",         TOK_MERROR);
3544         hash_keyword(state, "warning",       TOK_MWARNING);
3545         hash_keyword(state, "pragma",        TOK_MPRAGMA);
3546         hash_keyword(state, "ifdef",         TOK_MIFDEF);
3547         hash_keyword(state, "ifndef",        TOK_MIFNDEF);
3548         hash_keyword(state, "elif",          TOK_MELIF);
3549         hash_keyword(state, "endif",         TOK_MENDIF);
3550 }
3551
3552
3553 static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3554 {
3555         if (ident->sym_define != 0) {
3556                 struct macro *macro;
3557                 struct macro_arg *arg, *anext;
3558                 macro = ident->sym_define;
3559                 ident->sym_define = 0;
3560                 
3561                 /* Free the macro arguments... */
3562                 anext = macro->args;
3563                 while(anext) {
3564                         arg = anext;
3565                         anext = arg->next;
3566                         xfree(arg);
3567                 }
3568
3569                 /* Free the macro buffer */
3570                 xfree(macro->buf);
3571
3572                 /* Now free the macro itself */
3573                 xfree(macro);
3574         }
3575 }
3576
3577 static void define_macro(
3578         struct compile_state *state,
3579         struct hash_entry *ident, 
3580         const char *value, int value_len, int value_off, 
3581         struct macro_arg *args)
3582 {
3583         struct macro *macro;
3584         struct macro_arg *arg;
3585         macro = ident->sym_define;
3586         if (macro != 0) {
3587                 /* Explicitly allow identical redefinitions of the same macro */
3588                 if ((macro->buf_len == value_len) &&
3589                         (memcmp(macro->buf, value, value_len) == 0)) {
3590                         return;
3591                 }
3592                 error(state, 0, "macro %s already defined\n", ident->name);
3593         }
3594 #if 0
3595         fprintf(state->errout, "%s: `%*.*s'\n",
3596                 ident->name,
3597                 value_len - value_off,
3598                 value_len - value_off,
3599                 value + value_off);
3600 #endif
3601         macro = xmalloc(sizeof(*macro), "macro");
3602         macro->ident = ident;
3603         macro->buf_len = value_len;
3604         macro->buf_off = value_off;
3605         macro->args    = args;
3606         macro->buf = xmalloc(macro->buf_len + 1, "macro buf");
3607
3608         macro->argc = 0;
3609         for(arg = args; arg; arg = arg->next) {
3610                 macro->argc += 1;
3611         }      
3612
3613         memcpy(macro->buf, value, macro->buf_len);
3614         macro->buf[macro->buf_len] = '\0';
3615
3616         ident->sym_define = macro;
3617 }
3618
3619 static void register_builtin_macro(struct compile_state *state,
3620         const char *name, const char *value)
3621 {
3622         struct hash_entry *ident;
3623
3624         if (value[0] == '(') {
3625                 internal_error(state, 0, "Builtin macros with arguments not supported");
3626         }
3627         ident = lookup(state, name, strlen(name));
3628         define_macro(state, ident, value, strlen(value), 0, 0);
3629 }
3630
3631 static void register_builtin_macros(struct compile_state *state)
3632 {
3633         char buf[30];
3634         char scratch[30];
3635         time_t now;
3636         struct tm *tm;
3637         now = time(NULL);
3638         tm = localtime(&now);
3639
3640         register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3641         register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3642         register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3643         register_builtin_macro(state, "__LINE__", "54321");
3644
3645         strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3646         sprintf(buf, "\"%s\"", scratch);
3647         register_builtin_macro(state, "__DATE__", buf);
3648
3649         strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3650         sprintf(buf, "\"%s\"", scratch);
3651         register_builtin_macro(state, "__TIME__", buf);
3652
3653         /* I can't be a conforming implementation of C :( */
3654         register_builtin_macro(state, "__STDC__", "0");
3655         /* In particular I don't conform to C99 */
3656         register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3657         
3658 }
3659
3660 static void process_cmdline_macros(struct compile_state *state)
3661 {
3662         const char **macro, *name;
3663         struct hash_entry *ident;
3664         for(macro = state->compiler->defines; (name = *macro); macro++) {
3665                 const char *body;
3666                 size_t name_len;
3667
3668                 name_len = strlen(name);
3669                 body = strchr(name, '=');
3670                 if (!body) {
3671                         body = "\0";
3672                 } else {
3673                         name_len = body - name;
3674                         body++;
3675                 }
3676                 ident = lookup(state, name, name_len);
3677                 define_macro(state, ident, body, strlen(body), 0, 0);
3678         }
3679         for(macro = state->compiler->undefs; (name = *macro); macro++) {
3680                 ident = lookup(state, name, strlen(name));
3681                 undef_macro(state, ident);
3682         }
3683 }
3684
3685 static int spacep(int c)
3686 {
3687         int ret = 0;
3688         switch(c) {
3689         case ' ':
3690         case '\t':
3691         case '\f':
3692         case '\v':
3693         case '\r':
3694                 ret = 1;
3695                 break;
3696         }
3697         return ret;
3698 }
3699
3700 static int eolp(int c)
3701 {
3702         int ret = 0;
3703         switch(c) {
3704         case '\n':
3705                 ret = 1;
3706                 break;
3707         }
3708         return ret;
3709 }
3710
3711 static int digitp(int c)
3712 {
3713         int ret = 0;
3714         switch(c) {
3715         case '0': case '1': case '2': case '3': case '4': 
3716         case '5': case '6': case '7': case '8': case '9':
3717                 ret = 1;
3718                 break;
3719         }
3720         return ret;
3721 }
3722 static int digval(int c)
3723 {
3724         int val = -1;
3725         if ((c >= '0') && (c <= '9')) {
3726                 val = c - '0';
3727         }
3728         return val;
3729 }
3730
3731 static int hexdigitp(int c)
3732 {
3733         int ret = 0;
3734         switch(c) {
3735         case '0': case '1': case '2': case '3': case '4': 
3736         case '5': case '6': case '7': case '8': case '9':
3737         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3738         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3739                 ret = 1;
3740                 break;
3741         }
3742         return ret;
3743 }
3744 static int hexdigval(int c) 
3745 {
3746         int val = -1;
3747         if ((c >= '0') && (c <= '9')) {
3748                 val = c - '0';
3749         }
3750         else if ((c >= 'A') && (c <= 'F')) {
3751                 val = 10 + (c - 'A');
3752         }
3753         else if ((c >= 'a') && (c <= 'f')) {
3754                 val = 10 + (c - 'a');
3755         }
3756         return val;
3757 }
3758
3759 static int octdigitp(int c)
3760 {
3761         int ret = 0;
3762         switch(c) {
3763         case '0': case '1': case '2': case '3': 
3764         case '4': case '5': case '6': case '7':
3765                 ret = 1;
3766                 break;
3767         }
3768         return ret;
3769 }
3770 static int octdigval(int c)
3771 {
3772         int val = -1;
3773         if ((c >= '0') && (c <= '7')) {
3774                 val = c - '0';
3775         }
3776         return val;
3777 }
3778
3779 static int letterp(int c)
3780 {
3781         int ret = 0;
3782         switch(c) {
3783         case 'a': case 'b': case 'c': case 'd': case 'e':
3784         case 'f': case 'g': case 'h': case 'i': case 'j':
3785         case 'k': case 'l': case 'm': case 'n': case 'o':
3786         case 'p': case 'q': case 'r': case 's': case 't':
3787         case 'u': case 'v': case 'w': case 'x': case 'y':
3788         case 'z':
3789         case 'A': case 'B': case 'C': case 'D': case 'E':
3790         case 'F': case 'G': case 'H': case 'I': case 'J':
3791         case 'K': case 'L': case 'M': case 'N': case 'O':
3792         case 'P': case 'Q': case 'R': case 'S': case 'T':
3793         case 'U': case 'V': case 'W': case 'X': case 'Y':
3794         case 'Z':
3795         case '_':
3796                 ret = 1;
3797                 break;
3798         }
3799         return ret;
3800 }
3801
3802 static const char *identifier(const char *str, const char *end)
3803 {
3804         if (letterp(*str)) {
3805                 for(; str < end; str++) {
3806                         int c;
3807                         c = *str;
3808                         if (!letterp(c) && !digitp(c)) {
3809                                 break;
3810                         }
3811                 }
3812         }
3813         return str;
3814 }
3815
3816 static int char_value(struct compile_state *state,
3817         const signed char **strp, const signed char *end)
3818 {
3819         const signed char *str;
3820         int c;
3821         str = *strp;
3822         c = *str++;
3823         if ((c == '\\') && (str < end)) {
3824                 switch(*str) {
3825                 case 'n':  c = '\n'; str++; break;
3826                 case 't':  c = '\t'; str++; break;
3827                 case 'v':  c = '\v'; str++; break;
3828                 case 'b':  c = '\b'; str++; break;
3829                 case 'r':  c = '\r'; str++; break;
3830                 case 'f':  c = '\f'; str++; break;
3831                 case 'a':  c = '\a'; str++; break;
3832                 case '\\': c = '\\'; str++; break;
3833                 case '?':  c = '?';  str++; break;
3834                 case '\'': c = '\''; str++; break;
3835                 case '"':  c = '"';  str++; break;
3836                 case 'x': 
3837                         c = 0;
3838                         str++;
3839                         while((str < end) && hexdigitp(*str)) {
3840                                 c <<= 4;
3841                                 c += hexdigval(*str);
3842                                 str++;
3843                         }
3844                         break;
3845                 case '0': case '1': case '2': case '3': 
3846                 case '4': case '5': case '6': case '7':
3847                         c = 0;
3848                         while((str < end) && octdigitp(*str)) {
3849                                 c <<= 3;
3850                                 c += octdigval(*str);
3851                                 str++;
3852                         }
3853                         break;
3854                 default:
3855                         error(state, 0, "Invalid character constant");
3856                         break;
3857                 }
3858         }
3859         *strp = str;
3860         return c;
3861 }
3862
3863 static const char *after_digits(const char *ptr, const char *end)
3864 {
3865         while((ptr < end) && digitp(*ptr)) {
3866                 ptr++;
3867         }
3868         return ptr;
3869 }
3870
3871 static const char *after_octdigits(const char *ptr, const char *end)
3872 {
3873         while((ptr < end) && octdigitp(*ptr)) {
3874                 ptr++;
3875         }
3876         return ptr;
3877 }
3878
3879 static const char *after_hexdigits(const char *ptr, const char *end)
3880 {
3881         while((ptr < end) && hexdigitp(*ptr)) {
3882                 ptr++;
3883         }
3884         return ptr;
3885 }
3886
3887 static void save_string(struct compile_state *state, 
3888         struct token *tk, const char *start, const char *end, const char *id)
3889 {
3890         char *str;
3891         int str_len;
3892         /* Create a private copy of the string */
3893         str_len = end - start + 1;
3894         str = xmalloc(str_len + 1, id);
3895         memcpy(str, start, str_len);
3896         str[str_len] = '\0';
3897
3898         /* Store the copy in the token */
3899         tk->val.str = str;
3900         tk->str_len = str_len;
3901 }
3902
3903 static int lparen_peek(struct compile_state *state, struct file_state *file)
3904 {
3905         const char *tokp, *end;
3906         /* Is the next token going to be an lparen? 
3907          * Whitespace tokens are significant for seeing if a macro
3908          * should be expanded.
3909          */
3910         tokp = file->pos;
3911         end = file->buf + file->size;
3912         return (tokp < end) && (*tokp == '(');
3913 }
3914
3915 static void raw_next_token(struct compile_state *state, 
3916         struct file_state *file, struct token *tk)
3917 {
3918         const char *token;
3919         int c, c1, c2, c3;
3920         const char *tokp, *end;
3921         int tok;
3922
3923         tk->str_len = 0;
3924         tk->ident = 0;
3925         token = tokp = file->pos;
3926         end = file->buf + file->size;
3927         tok = TOK_UNKNOWN;
3928         c = -1;
3929         if (tokp < end) {
3930                 c = *tokp;
3931         }
3932         c1 = -1;
3933         if ((tokp + 1) < end) {
3934                 c1 = tokp[1];
3935         }
3936         c2 = -1;
3937         if ((tokp + 2) < end) {
3938                 c2 = tokp[2];
3939         }
3940         c3 = -1;
3941         if ((tokp + 3) < end) {
3942                 c3 = tokp[3];
3943         }
3944         if (tokp >= end) {
3945                 tok = TOK_EOF;
3946                 tokp = end;
3947         }
3948         /* End of Line */
3949         else if (eolp(c)) {
3950                 tok = TOK_EOL;
3951                 file->line++;
3952                 file->report_line++;
3953                 file->line_start = tokp + 1;
3954         }
3955         /* Whitespace */
3956         else if (spacep(c)) {
3957                 tok = TOK_SPACE;
3958                 while ((tokp < end) && spacep(c)) {
3959                         c = *(++tokp);
3960                 }
3961                 if (!spacep(c)) {
3962                         tokp--;
3963                 }
3964         }
3965         /* EOL Comments */
3966         else if ((c == '/') && (c1 == '/')) {
3967                 tok = TOK_SPACE;
3968                 for(tokp += 2; tokp < end; tokp++) {
3969                         c = *tokp;
3970                         if (c == '\n') {
3971                                 tokp--;
3972                                 break;
3973                         }
3974                 }
3975         }
3976         /* Comments */
3977         else if ((c == '/') && (c1 == '*')) {
3978                 int line;
3979                 const char *line_start;
3980                 line = file->line;
3981                 line_start = file->line_start;
3982                 for(tokp += 2; (end - tokp) >= 2; tokp++) {
3983                         c = *tokp;
3984                         if (c == '\n') {
3985                                 line++;
3986                                 line_start = tokp +1;
3987                         }
3988                         else if ((c == '*') && (tokp[1] == '/')) {
3989                                 tok = TOK_SPACE;
3990                                 tokp += 1;
3991                                 break;
3992                         }
3993                 }
3994                 if (tok == TOK_UNKNOWN) {
3995                         error(state, 0, "unterminated comment");
3996                 }
3997                 if (state->token_base && (line != file->line)) {
3998                         error(state, 0, 
3999                                 "multiline comment in preprocessor directive");
4000                 }
4001                 file->report_line += line - file->line;
4002                 file->line = line;
4003                 file->line_start = line_start;
4004         }
4005         /* string constants */
4006         else if ((c == '"') ||
4007                 ((c == 'L') && (c1 == '"'))) {
4008                 int line;
4009                 const char *line_start;
4010                 int wchar;
4011                 line = file->line;
4012                 line_start = file->line_start;
4013                 wchar = 0;
4014                 if (c == 'L') {
4015                         wchar = 1;
4016                         tokp++;
4017                 }
4018                 for(tokp += 1; tokp < end; tokp++) {
4019                         c = *tokp;
4020                         if (c == '\n') {
4021                                 line++;
4022                                 line_start = tokp + 1;
4023                         }
4024                         else if ((c == '\\') && (tokp +1 < end)) {
4025                                 tokp++;
4026                         }
4027                         else if (c == '"') {
4028                                 tok = TOK_LIT_STRING;
4029                                 break;
4030                         }
4031                 }
4032                 if (tok == TOK_UNKNOWN) {
4033                         error(state, 0, "unterminated string constant");
4034                 }
4035                 if (line != file->line) {
4036                         if (state->token_base) {
4037                                 /* Preprocessor directives cannot span lines */
4038                                 error(state, 0, "multiline string constant");
4039                         } else {
4040                                 warning(state, 0, "multiline string constant");
4041                         }
4042                 }
4043                 file->report_line += line - file->line;
4044                 file->line = line;
4045                 file->line_start = line_start;
4046
4047                 /* Save the string value */
4048                 save_string(state, tk, token, tokp, "literal string");
4049         }
4050         /* character constants */
4051         else if ((c == '\'') ||
4052                 ((c == 'L') && (c1 == '\''))) {
4053                 int line;
4054                 const char *line_start;
4055                 int wchar;
4056                 line = file->line;
4057                 line_start = file->line_start;
4058                 wchar = 0;
4059                 if (c == 'L') {
4060                         wchar = 1;
4061                         tokp++;
4062                 }
4063                 for(tokp += 1; tokp < end; tokp++) {
4064                         c = *tokp;
4065                         if (c == '\n') {
4066                                 line++;
4067                                 line_start = tokp + 1;
4068                         }
4069                         else if ((c == '\\') && (tokp +1 < end)) {
4070                                 tokp++;
4071                         }
4072                         else if (c == '\'') {
4073                                 tok = TOK_LIT_CHAR;
4074                                 break;
4075                         }
4076                 }
4077                 if (tok == TOK_UNKNOWN) {
4078                         error(state, 0, "unterminated character constant");
4079                 }
4080                 if (line != file->line) {
4081                         if (state->token_base) {
4082                                 /* Preprocessor directives cannot span lines */
4083                                 error(state, 0, "multiline character constant");
4084                         } else {
4085                                 warning(state, 0, "multiline character constant");
4086                         }
4087                 }
4088                 file->report_line += line - file->line;
4089                 file->line = line;
4090                 file->line_start = line_start;
4091
4092                 /* Save the character value */
4093                 save_string(state, tk, token, tokp, "literal character");
4094         }
4095         /* integer and floating constants 
4096          * Integer Constants
4097          * {digits}
4098          * 0[Xx]{hexdigits}
4099          * 0{octdigit}+
4100          * 
4101          * Floating constants
4102          * {digits}.{digits}[Ee][+-]?{digits}
4103          * {digits}.{digits}
4104          * {digits}[Ee][+-]?{digits}
4105          * .{digits}[Ee][+-]?{digits}
4106          * .{digits}
4107          */
4108         
4109         else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
4110                 const char *next, *new;
4111                 int is_float;
4112                 is_float = 0;
4113                 if (c != '.') {
4114                         next = after_digits(tokp, end);
4115                 }
4116                 else {
4117                         next = tokp;
4118                 }
4119                 if (next[0] == '.') {
4120                         new = after_digits(next, end);
4121                         is_float = (new != next);
4122                         next = new;
4123                 }
4124                 if ((next[0] == 'e') || (next[0] == 'E')) {
4125                         if (((next + 1) < end) && 
4126                                 ((next[1] == '+') || (next[1] == '-'))) {
4127                                 next++;
4128                         }
4129                         new = after_digits(next, end);
4130                         is_float = (new != next);
4131                         next = new;
4132                 }
4133                 if (is_float) {
4134                         tok = TOK_LIT_FLOAT;
4135                         if ((next < end) && (
4136                                 (next[0] == 'f') ||
4137                                 (next[0] == 'F') ||
4138                                 (next[0] == 'l') ||
4139                                 (next[0] == 'L'))
4140                                 ) {
4141                                 next++;
4142                         }
4143                 }
4144                 if (!is_float && digitp(c)) {
4145                         tok = TOK_LIT_INT;
4146                         if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
4147                                 next = after_hexdigits(tokp + 2, end);
4148                         }
4149                         else if (c == '0') {
4150                                 next = after_octdigits(tokp, end);
4151                         }
4152                         else {
4153                                 next = after_digits(tokp, end);
4154                         }
4155                         /* crazy integer suffixes */
4156                         if ((next < end) && 
4157                                 ((next[0] == 'u') || (next[0] == 'U'))) { 
4158                                 next++;
4159                                 if ((next < end) &&
4160                                         ((next[0] == 'l') || (next[0] == 'L'))) {
4161                                         next++;
4162                                 }
4163                         }
4164                         else if ((next < end) &&
4165                                 ((next[0] == 'l') || (next[0] == 'L'))) {
4166                                 next++;
4167                                 if ((next < end) && 
4168                                         ((next[0] == 'u') || (next[0] == 'U'))) { 
4169                                         next++;
4170                                 }
4171                         }
4172                 }
4173                 tokp = next - 1;
4174
4175                 /* Save the integer/floating point value */
4176                 save_string(state, tk, token, tokp, "literal number");
4177         }
4178         /* identifiers */
4179         else if (letterp(c)) {
4180                 tok = TOK_IDENT;
4181                 tokp = identifier(tokp, end);
4182                 tokp -= 1;
4183                 tk->ident = lookup(state, token, tokp +1 - token);
4184                 /* See if this identifier can be macro expanded */
4185                 tk->val.notmacro = 0;
4186                 if ((tokp < end) && (tokp[1] == '$')) {
4187                         tokp++;
4188                         tk->val.notmacro = 1;
4189                 }
4190         }
4191         /* C99 alternate macro characters */
4192         else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) { 
4193                 tokp += 3; 
4194                 tok = TOK_CONCATENATE; 
4195         }
4196         else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
4197         else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
4198         else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
4199         else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
4200         else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
4201         else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
4202         else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
4203         else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
4204         else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
4205         else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
4206         else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
4207         else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
4208         else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
4209         else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
4210         else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
4211         else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
4212         else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
4213         else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
4214         else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
4215         else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
4216         else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
4217         else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
4218         else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
4219         else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
4220         else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
4221         else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
4222         else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
4223         else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
4224         else if (c == ';') { tok = TOK_SEMI; }
4225         else if (c == '{') { tok = TOK_LBRACE; }
4226         else if (c == '}') { tok = TOK_RBRACE; }
4227         else if (c == ',') { tok = TOK_COMMA; }
4228         else if (c == '=') { tok = TOK_EQ; }
4229         else if (c == ':') { tok = TOK_COLON; }
4230         else if (c == '[') { tok = TOK_LBRACKET; }
4231         else if (c == ']') { tok = TOK_RBRACKET; }
4232         else if (c == '(') { tok = TOK_LPAREN; }
4233         else if (c == ')') { tok = TOK_RPAREN; }
4234         else if (c == '*') { tok = TOK_STAR; }
4235         else if (c == '>') { tok = TOK_MORE; }
4236         else if (c == '<') { tok = TOK_LESS; }
4237         else if (c == '?') { tok = TOK_QUEST; }
4238         else if (c == '|') { tok = TOK_OR; }
4239         else if (c == '&') { tok = TOK_AND; }
4240         else if (c == '^') { tok = TOK_XOR; }
4241         else if (c == '+') { tok = TOK_PLUS; }
4242         else if (c == '-') { tok = TOK_MINUS; }
4243         else if (c == '/') { tok = TOK_DIV; }
4244         else if (c == '%') { tok = TOK_MOD; }
4245         else if (c == '!') { tok = TOK_BANG; }
4246         else if (c == '.') { tok = TOK_DOT; }
4247         else if (c == '~') { tok = TOK_TILDE; }
4248         else if (c == '#') { tok = TOK_MACRO; }
4249
4250         file->pos = tokp + 1;
4251         tk->tok = tok;
4252         if (tok == TOK_IDENT) {
4253                 if (state->token_base == 0) {
4254                         ident_to_keyword(state, tk);
4255                 } else {
4256                         ident_to_macro(state, tk);
4257                 }
4258         }
4259 }
4260
4261 static void next_token(struct compile_state *state, struct token *tk)
4262 {
4263         struct file_state *file;
4264         file = state->file;
4265         /* Don't return space tokens. */
4266         do {
4267                 raw_next_token(state, file, tk);
4268                 if (tk->tok == TOK_MACRO) {
4269                         /* Only match preprocessor directives at the start of a line */
4270                         const char *ptr;
4271                         for(ptr = file->line_start; spacep(*ptr); ptr++)
4272                                 ;
4273                         if (ptr != file->pos - 1) {
4274                                 tk->tok = TOK_UNKNOWN;
4275                         }
4276                 }
4277                 if (tk->tok == TOK_UNKNOWN) {
4278                         error(state, 0, "unknown token");
4279                 }
4280         } while(tk->tok == TOK_SPACE);
4281 }
4282
4283 static void check_tok(struct compile_state *state, struct token *tk, int tok)
4284 {
4285         if (tk->tok != tok) {
4286                 const char *name1, *name2;
4287                 name1 = tokens[tk->tok];
4288                 name2 = "";
4289                 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
4290                         name2 = tk->ident->name;
4291                 }
4292                 error(state, 0, "\tfound %s %s expected %s",
4293                         name1, name2, tokens[tok]);
4294         }
4295 }
4296
4297 struct macro_arg_value {
4298         struct hash_entry *ident;
4299         unsigned char *value;
4300         size_t len;
4301 };
4302 static struct macro_arg_value *read_macro_args(
4303         struct compile_state *state, struct macro *macro, 
4304         struct file_state *file, struct token *tk)
4305 {
4306         struct macro_arg_value *argv;
4307         struct macro_arg *arg;
4308         int paren_depth;
4309         int i;
4310
4311         if (macro->argc == 0) {
4312                 do {
4313                         raw_next_token(state, file, tk);
4314                 } while(tk->tok == TOK_SPACE);
4315                 return 0;
4316         }
4317         argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4318         for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4319                 argv[i].value = 0;
4320                 argv[i].len   = 0;
4321                 argv[i].ident = arg->ident;
4322         }
4323         paren_depth = 0;
4324         i = 0;
4325         
4326         for(;;) {
4327                 const char *start;
4328                 size_t len;
4329                 start = file->pos;
4330                 raw_next_token(state, file, tk);
4331                 
4332                 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4333                         (argv[i].ident != state->i___VA_ARGS__)) 
4334                 {
4335                         i++;
4336                         if (i >= macro->argc) {
4337                                 error(state, 0, "too many args to %s\n",
4338                                         macro->ident->name);
4339                         }
4340                         continue;
4341                 }
4342                 
4343                 if (tk->tok == TOK_LPAREN) {
4344                         paren_depth++;
4345                 }
4346                 
4347                 if (tk->tok == TOK_RPAREN) {
4348                         if (paren_depth == 0) {
4349                                 break;
4350                         }
4351                         paren_depth--;
4352                 }
4353                 if (tk->tok == TOK_EOF) {
4354                         error(state, 0, "End of file encountered while parsing macro arguments");
4355                 }
4356                 
4357                 len = file->pos - start;
4358                 argv[i].value = xrealloc(
4359                         argv[i].value, argv[i].len + len, "macro args");
4360                 memcpy(argv[i].value + argv[i].len, start, len);
4361                 argv[i].len += len;
4362         }
4363         if (i != macro->argc -1) {
4364                 error(state, 0, "missing %s arg %d\n", 
4365                         macro->ident->name, i +2);
4366         }
4367         return argv;
4368 }
4369
4370
4371 static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4372 {
4373         int i;
4374         for(i = 0; i < macro->argc; i++) {
4375                 xfree(argv[i].value);
4376         }
4377         xfree(argv);
4378 }
4379
4380 struct macro_buf {
4381         char *str;
4382         size_t len, pos;
4383 };
4384
4385 static void append_macro_text(struct compile_state *state,
4386         struct macro *macro, struct macro_buf *buf, 
4387         const char *fstart, size_t flen)
4388 {
4389 #if 0
4390         fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4391                 buf->pos, buf->pos, buf->str,
4392                 flen, flen, fstart);
4393 #endif
4394         if ((buf->pos + flen) < buf->len) {
4395                 memcpy(buf->str + buf->pos, fstart, flen);
4396         } else {
4397                 buf->str = xrealloc(buf->str, buf->len + flen, macro->ident->name);
4398                 memcpy(buf->str + buf->pos, fstart, flen);
4399                 buf->len += flen;
4400         }
4401         buf->pos += flen;
4402 }
4403
4404 static int compile_macro(struct compile_state *state, 
4405         struct file_state **filep, struct token *tk);
4406
4407 static void macro_expand_args(struct compile_state *state, 
4408         struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4409 {
4410         size_t i;
4411         
4412         for(i = 0; i < macro->argc; i++) {
4413                 struct file_state fmacro, *file;
4414                 struct macro_buf buf;
4415                 const char *fstart;
4416                 size_t flen;
4417
4418                 fmacro.basename    = argv[i].ident->name;
4419                 fmacro.dirname     = "";
4420                 fmacro.size        = argv[i].len;
4421                 fmacro.buf         = argv[i].value;
4422                 fmacro.pos         = fmacro.buf;
4423                 fmacro.line_start  = fmacro.buf;
4424                 fmacro.line        = 1;
4425                 fmacro.report_line = 1;
4426                 fmacro.report_name = fmacro.basename;
4427                 fmacro.report_dir  = fmacro.dirname;
4428                 fmacro.prev        = 0;
4429
4430                 buf.len = argv[i].len;
4431                 buf.str = xmalloc(buf.len, argv[i].ident->name);
4432                 buf.pos = 0;
4433
4434                 file = &fmacro;
4435                 for(;;) {
4436                         fstart = file->pos;
4437                         raw_next_token(state, file, tk);
4438                         flen = file->pos - fstart;
4439                         
4440                         if (tk->tok == TOK_EOF) {
4441                                 struct file_state *old;
4442                                 old = file;
4443                                 file = file->prev;
4444                                 if (!file) {
4445                                         break;
4446                                 }
4447                                 /* old->basename is used keep it */
4448                                 xfree(old->dirname);
4449                                 xfree(old->buf);
4450                                 xfree(old);
4451                                 continue;
4452                         }
4453                         else if (tk->ident && tk->ident->sym_define) {
4454                                 if (compile_macro(state, &file, tk)) {
4455                                         continue;
4456                                 }
4457                         }
4458
4459                         append_macro_text(state, macro, &buf,
4460                                 fstart, flen);
4461                 }
4462                         
4463                 xfree(argv[i].value);
4464                 argv[i].value = buf.str;
4465                 argv[i].len   = buf.pos;
4466         }
4467         return;
4468 }
4469
4470 static void expand_macro(struct compile_state *state,
4471         struct macro *macro, struct macro_buf *buf,
4472         struct macro_arg_value *argv, struct token *tk)
4473 {
4474         struct file_state fmacro;
4475         const char space[] = " ";
4476         const char *fstart;
4477         size_t flen;
4478         size_t i, j;
4479         fmacro.basename = macro->ident->name;
4480         fmacro.dirname  = "";
4481         fmacro.size = macro->buf_len - macro->buf_off;;
4482         fmacro.buf  = macro->buf + macro->buf_off;
4483         fmacro.pos  = fmacro.buf;
4484         fmacro.line_start = fmacro.buf;
4485         fmacro.line = 1;
4486         fmacro.report_line = 1;
4487         fmacro.report_name = fmacro.basename;
4488         fmacro.report_dir  = fmacro.dirname;
4489         fmacro.prev = 0;
4490         
4491         buf->len = macro->buf_len + 3;
4492         buf->str = xmalloc(buf->len, macro->ident->name);
4493         buf->pos = 0;
4494         
4495         fstart = fmacro.pos;
4496         raw_next_token(state, &fmacro, tk);
4497         while(tk->tok != TOK_EOF) {
4498                 flen = fmacro.pos - fstart;
4499                 switch(tk->tok) {
4500                 case TOK_IDENT:
4501                         for(i = 0; i < macro->argc; i++) {
4502                                 if (argv[i].ident == tk->ident) {
4503                                         break;
4504                                 }
4505                         }
4506                         if (i >= macro->argc) {
4507                                 break;
4508                         }
4509                         /* Substitute macro parameter */
4510                         fstart = argv[i].value;
4511                         flen   = argv[i].len;
4512                         break;
4513                 case TOK_MACRO:
4514                         if (!macro->buf_off) {
4515                                 break;
4516                         }
4517                         do {
4518                                 raw_next_token(state, &fmacro, tk);
4519                         } while(tk->tok == TOK_SPACE);
4520                         check_tok(state, tk, TOK_IDENT);
4521                         for(i = 0; i < macro->argc; i++) {
4522                                 if (argv[i].ident == tk->ident) {
4523                                         break;
4524                                 }
4525                         }
4526                         if (i >= macro->argc) {
4527                                 error(state, 0, "parameter `%s' not found",
4528                                         tk->ident->name);
4529                         }
4530                         /* Stringize token */
4531                         append_macro_text(state, macro, buf, "\"", 1);
4532                         for(j = 0; j < argv[i].len; j++) {
4533                                 char *str = argv[i].value + j;
4534                                 size_t len = 1;
4535                                 if (*str == '\\') {
4536                                         str = "\\";
4537                                         len = 2;
4538                                 } 
4539                                 else if (*str == '"') {
4540                                         str = "\\\"";
4541                                         len = 2;
4542                                 }
4543                                 append_macro_text(state, macro, buf, str, len);
4544                         }
4545                         append_macro_text(state, macro, buf, "\"", 1);
4546                         fstart = 0;
4547                         flen   = 0;
4548                         break;
4549                 case TOK_CONCATENATE:
4550                         /* Concatenate tokens */
4551                         /* Delete the previous whitespace token */
4552                         if (buf->str[buf->pos - 1] == ' ') {
4553                                 buf->pos -= 1;
4554                         }
4555                         /* Skip the next sequence of whitspace tokens */
4556                         do {
4557                                 fstart = fmacro.pos;
4558                                 raw_next_token(state, &fmacro, tk);
4559                         } while(tk->tok == TOK_SPACE);
4560                         /* Restart at the top of the loop.
4561                          * I need to process the non white space token.
4562                          */
4563                         continue;
4564                         break;
4565                 case TOK_SPACE:
4566                         /* Collapse multiple spaces into one */
4567                         if (buf->str[buf->pos - 1] != ' ') {
4568                                 fstart = space;
4569                                 flen   = 1;
4570                         } else {
4571                                 fstart = 0;
4572                                 flen   = 0;
4573                         }
4574                         break;
4575                 default:
4576                         break;
4577                 }
4578
4579                 append_macro_text(state, macro, buf, fstart, flen);
4580                 
4581                 fstart = fmacro.pos;
4582                 raw_next_token(state, &fmacro, tk);
4583         }
4584 }
4585
4586 static void tag_macro_name(struct compile_state *state,
4587         struct macro *macro, struct macro_buf *buf,
4588         struct token *tk)
4589 {
4590         /* Guard all instances of the macro name in the replacement
4591          * text from further macro expansion.
4592          */
4593         struct file_state fmacro;
4594         const char *fstart;
4595         size_t flen;
4596         fmacro.basename = macro->ident->name;
4597         fmacro.dirname  = "";
4598         fmacro.size = buf->pos;
4599         fmacro.buf  = buf->str;
4600         fmacro.pos  = fmacro.buf;
4601         fmacro.line_start = fmacro.buf;
4602         fmacro.line = 1;
4603         fmacro.report_line = 1;
4604         fmacro.report_name = fmacro.basename;
4605         fmacro.report_dir  = fmacro.dirname;
4606         fmacro.prev = 0;
4607         
4608         buf->len = macro->buf_len + 3;
4609         buf->str = xmalloc(buf->len, macro->ident->name);
4610         buf->pos = 0;
4611         
4612         fstart = fmacro.pos;
4613         raw_next_token(state, &fmacro, tk);
4614         while(tk->tok != TOK_EOF) {
4615                 flen = fmacro.pos - fstart;
4616                 if ((tk->tok == TOK_IDENT) &&
4617                         (tk->ident == macro->ident) &&
4618                         (tk->val.notmacro == 0)) {
4619                         append_macro_text(state, macro, buf, fstart, flen);
4620                         fstart = "$";
4621                         flen   = 1;
4622                 }
4623
4624                 append_macro_text(state, macro, buf, fstart, flen);
4625                 
4626                 fstart = fmacro.pos;
4627                 raw_next_token(state, &fmacro, tk);
4628         }
4629         xfree(fmacro.buf);
4630 }
4631         
4632 static int compile_macro(struct compile_state *state, 
4633         struct file_state **filep, struct token *tk)
4634 {
4635         struct file_state *file;
4636         struct hash_entry *ident;
4637         struct macro *macro;
4638         struct macro_arg_value *argv;
4639         struct macro_buf buf;
4640
4641 #if 0
4642         fprintf(state->errout, "macro: %s\n", tk->ident->name);
4643 #endif
4644         ident = tk->ident;
4645         macro = ident->sym_define;
4646
4647         /* If this token comes from a macro expansion ignore it */
4648         if (tk->val.notmacro) {
4649                 return 0;
4650         }
4651         /* If I am a function like macro and the identifier is not followed
4652          * by a left parenthesis, do nothing.
4653          */
4654         if ((macro->buf_off != 0) && !lparen_peek(state, *filep)) {
4655                 return 0;
4656         }
4657
4658         /* Read in the macro arguments */
4659         argv = 0;
4660         if (macro->buf_off) {
4661                 raw_next_token(state, *filep, tk);
4662                 check_tok(state, tk, TOK_LPAREN);
4663
4664                 argv = read_macro_args(state, macro, *filep, tk);
4665
4666                 check_tok(state, tk, TOK_RPAREN);
4667         }
4668         /* Macro expand the macro arguments */
4669         macro_expand_args(state, macro, argv, tk);
4670
4671         buf.str = 0;
4672         buf.len = 0;
4673         buf.pos = 0;
4674         if (ident == state->i___FILE__) {
4675                 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4676                 buf.str = xmalloc(buf.len, ident->name);
4677                 sprintf(buf.str, "\"%s\"", state->file->basename);
4678                 buf.pos = strlen(buf.str);
4679         }
4680         else if (ident == state->i___LINE__) {
4681                 buf.len = 30;
4682                 buf.str = xmalloc(buf.len, ident->name);
4683                 sprintf(buf.str, "%d", state->file->line);
4684                 buf.pos = strlen(buf.str);
4685         }
4686         else {
4687                 expand_macro(state, macro, &buf, argv, tk);
4688         }
4689         /* Tag the macro name with a $ so it will no longer
4690          * be regonized as a canidate for macro expansion.
4691          */
4692         tag_macro_name(state, macro, &buf, tk);
4693         append_macro_text(state, macro, &buf, "\n\0", 2);
4694
4695 #if 0
4696         fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4697                 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4698 #endif
4699
4700         free_macro_args(macro, argv);
4701
4702         file = xmalloc(sizeof(*file), "file_state");
4703         file->basename = xstrdup(ident->name);
4704         file->dirname = xstrdup("");
4705         file->buf = buf.str;
4706         file->size = buf.pos - 2;
4707         file->pos = file->buf;
4708         file->line_start = file->pos;
4709         file->line = 1;
4710         file->report_line = 1;
4711         file->report_name = file->basename;
4712         file->report_dir  = file->dirname;
4713         file->prev = *filep;
4714         *filep = file;
4715         return 1;
4716 }
4717
4718 static void eat_tokens(struct compile_state *state, int targ_tok)
4719 {
4720         if (state->eat_depth > 0) {
4721                 internal_error(state, 0, "Already eating...");
4722         }
4723         state->eat_depth = state->if_depth;
4724         state->eat_targ = targ_tok;
4725 }
4726 static int if_eat(struct compile_state *state)
4727 {
4728         return state->eat_depth > 0;
4729 }
4730 static int if_value(struct compile_state *state)
4731 {
4732         int index, offset;
4733         index = state->if_depth / CHAR_BIT;
4734         offset = state->if_depth % CHAR_BIT;
4735         return !!(state->if_bytes[index] & (1 << (offset)));
4736 }
4737 static void set_if_value(struct compile_state *state, int value) 
4738 {
4739         int index, offset;
4740         index = state->if_depth / CHAR_BIT;
4741         offset = state->if_depth % CHAR_BIT;
4742
4743         state->if_bytes[index] &= ~(1 << offset);
4744         if (value) {
4745                 state->if_bytes[index] |= (1 << offset);
4746         }
4747 }
4748 static void in_if(struct compile_state *state, const char *name)
4749 {
4750         if (state->if_depth <= 0) {
4751                 error(state, 0, "%s without #if", name);
4752         }
4753 }
4754 static void enter_if(struct compile_state *state)
4755 {
4756         state->if_depth += 1;
4757         if (state->if_depth > MAX_CPP_IF_DEPTH) {
4758                 error(state, 0, "#if depth too great");
4759         }
4760 }
4761 static void reenter_if(struct compile_state *state, const char *name)
4762 {
4763         in_if(state, name);
4764         if ((state->eat_depth == state->if_depth) &&
4765                 (state->eat_targ == TOK_MELSE)) {
4766                 state->eat_depth = 0;
4767                 state->eat_targ = 0;
4768         }
4769 }
4770 static void enter_else(struct compile_state *state, const char *name)
4771 {
4772         in_if(state, name);
4773         if ((state->eat_depth == state->if_depth) &&
4774                 (state->eat_targ == TOK_MELSE)) {
4775                 state->eat_depth = 0;
4776                 state->eat_targ = 0;
4777         }
4778 }
4779 static void exit_if(struct compile_state *state, const char *name)
4780 {
4781         in_if(state, name);
4782         if (state->eat_depth == state->if_depth) {
4783                 state->eat_depth = 0;
4784                 state->eat_targ = 0;
4785         }
4786         state->if_depth -= 1;
4787 }
4788
4789 static void cpp_token(struct compile_state *state, struct token *tk)
4790 {
4791         struct file_state *file;
4792         int rescan;
4793
4794         next_token(state, tk);
4795         do {
4796                 rescan = 0;
4797                 file = state->file;
4798                 /* Exit out of an include directive or macro call */
4799                 if ((tk->tok == TOK_EOF) && 
4800                         (state->file && state->macro_file) &&
4801                         file->prev) 
4802                 {
4803                         state->file = file->prev;
4804                         /* file->basename is used keep it */
4805                         xfree(file->dirname);
4806                         xfree(file->buf);
4807                         xfree(file);
4808                         next_token(state, tk);
4809                         rescan = 1;
4810                 }
4811         } while(rescan);
4812 }
4813
4814 static void preprocess(struct compile_state *state, struct token *tk);
4815
4816 static void token(struct compile_state *state, struct token *tk)
4817 {
4818         int rescan;
4819         cpp_token(state, tk);
4820         do {
4821                 rescan = 0;
4822                 /* Process a macro directive */
4823                 if (tk->tok == TOK_MACRO) {
4824                         preprocess(state, tk);
4825                         rescan = 1;
4826                 }
4827                 /* Expand a macro call */
4828                 else if (tk->ident && tk->ident->sym_define) {
4829                         rescan = compile_macro(state, &state->file, tk);
4830                         if (rescan) {
4831                                 cpp_token(state, tk);
4832                         }
4833                 }
4834                 /* Eat tokens disabled by the preprocessor */
4835                 else if (if_eat(state)) {
4836                         cpp_token(state, tk);
4837                         rescan = 1;
4838                 }
4839                 /* When not in macro context hide EOL */
4840                 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
4841                         next_token(state, tk);
4842                         rescan = 1;
4843                 }
4844         } while(rescan);
4845 }
4846
4847
4848 static inline struct token *get_token(struct compile_state *state, int offset)
4849 {
4850         int index;
4851         index = state->token_base + offset;
4852         if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4853                 internal_error(state, 0, "token array to small");
4854         }
4855         return &state->token[index];
4856 }
4857
4858 static struct token *do_eat_token(struct compile_state *state, int tok)
4859 {
4860         struct token *tk;
4861         int i;
4862         check_tok(state, get_token(state, 1), tok);
4863         
4864         /* Free the old token value */
4865         tk = get_token(state, 0);
4866         if (tk->str_len) {
4867                 memset((void *)tk->val.str, -1, tk->str_len);
4868                 xfree(tk->val.str);
4869         }
4870         /* Overwrite the old token with newer tokens */
4871         for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4872                 state->token[i] = state->token[i + 1];
4873         }
4874         /* Clear the last token */
4875         memset(&state->token[i], 0, sizeof(state->token[i]));
4876         state->token[i].tok = -1;
4877
4878         /* Return the token */
4879         return tk;
4880 }
4881
4882 static int cpp_peek(struct compile_state *state)
4883 {
4884         struct token *tk1;
4885         tk1 = get_token(state, 1);
4886         if (tk1->tok == -1) {
4887                 cpp_token(state, tk1);
4888         }
4889         return tk1->tok;
4890 }
4891
4892 static struct token *cpp_eat(struct compile_state *state, int tok)
4893 {
4894         cpp_peek(state);
4895         return do_eat_token(state, tok);
4896 }
4897
4898 static int peek(struct compile_state *state)
4899 {
4900         struct token *tk1;
4901         tk1 = get_token(state, 1);
4902         if (tk1->tok == -1) {
4903                 token(state, tk1);
4904         }
4905         return tk1->tok;
4906 }
4907
4908 static int peek2(struct compile_state *state)
4909 {
4910         struct token *tk1, *tk2;
4911         tk1 = get_token(state, 1);
4912         tk2 = get_token(state, 2);
4913         if (tk1->tok == -1) {
4914                 token(state, tk1);
4915         }
4916         if (tk2->tok == -1) {
4917                 token(state, tk2);
4918         }
4919         return tk2->tok;
4920 }
4921
4922 static struct token *eat(struct compile_state *state, int tok)
4923 {
4924         peek(state);
4925         return do_eat_token(state, tok);
4926 }
4927
4928 static void compile_file(struct compile_state *state, const char *filename, int local)
4929 {
4930         char cwd[MAX_CWD_SIZE];
4931         const char *subdir, *base;
4932         int subdir_len;
4933         struct file_state *file;
4934         char *basename;
4935         file = xmalloc(sizeof(*file), "file_state");
4936
4937         base = strrchr(filename, '/');
4938         subdir = filename;
4939         if (base != 0) {
4940                 subdir_len = base - filename;
4941                 base++;
4942         }
4943         else {
4944                 base = filename;
4945                 subdir_len = 0;
4946         }
4947         basename = xmalloc(strlen(base) +1, "basename");
4948         strcpy(basename, base);
4949         file->basename = basename;
4950
4951         if (getcwd(cwd, sizeof(cwd)) == 0) {
4952                 die("cwd buffer to small");
4953         }
4954         if (subdir[0] == '/') {
4955                 file->dirname = xmalloc(subdir_len + 1, "dirname");
4956                 memcpy(file->dirname, subdir, subdir_len);
4957                 file->dirname[subdir_len] = '\0';
4958         }
4959         else {
4960                 const char *dir;
4961                 int dirlen;
4962                 const char **path;
4963                 /* Find the appropriate directory... */
4964                 dir = 0;
4965                 if (!state->file && exists(cwd, filename)) {
4966                         dir = cwd;
4967                 }
4968                 if (local && state->file && exists(state->file->dirname, filename)) {
4969                         dir = state->file->dirname;
4970                 }
4971                 for(path = state->compiler->include_paths; !dir && *path; path++) {
4972                         if (exists(*path, filename)) {
4973                                 dir = *path;
4974                         }
4975                 }
4976                 if (!dir) {
4977                         error(state, 0, "Cannot find `%s'\n", filename);
4978                 }
4979                 dirlen = strlen(dir);
4980                 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
4981                 memcpy(file->dirname, dir, dirlen);
4982                 file->dirname[dirlen] = '/';
4983                 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
4984                 file->dirname[dirlen + 1 + subdir_len] = '\0';
4985         }
4986         file->buf = slurp_file(file->dirname, file->basename, &file->size);
4987
4988         file->pos = file->buf;
4989         file->line_start = file->pos;
4990         file->line = 1;
4991
4992         file->report_line = 1;
4993         file->report_name = file->basename;
4994         file->report_dir  = file->dirname;
4995
4996         file->prev = state->file;
4997         state->file = file;
4998         
4999         process_trigraphs(state);
5000         splice_lines(state);
5001 }
5002
5003 static struct triple *constant_expr(struct compile_state *state);
5004 static void integral(struct compile_state *state, struct triple *def);
5005
5006 static int mcexpr(struct compile_state *state)
5007 {
5008         struct triple *cvalue;
5009         cvalue = constant_expr(state);
5010         integral(state, cvalue);
5011         if (cvalue->op != OP_INTCONST) {
5012                 error(state, 0, "integer constant expected");
5013         }
5014         return cvalue->u.cval != 0;
5015 }
5016
5017 static void preprocess(struct compile_state *state, struct token *current_token)
5018 {
5019         /* Doing much more with the preprocessor would require
5020          * a parser and a major restructuring.
5021          * Postpone that for later.
5022          */
5023         struct file_state *file;
5024         int old_token_base;
5025         int line;
5026         int tok;
5027         
5028         file = state->file;
5029         state->macro_line = line = file->line;
5030         state->macro_file = file;
5031
5032         old_token_base = state->token_base;
5033         state->token_base = current_token - state->token;
5034
5035         tok = cpp_peek(state);
5036         switch(tok) {
5037         case TOK_LIT_INT:
5038         {
5039                 struct token *tk;
5040                 int override_line;
5041                 tk = cpp_eat(state, TOK_LIT_INT);
5042                 override_line = strtoul(tk->val.str, 0, 10);
5043                 /* I have a cpp line marker parse it */
5044                 if (cpp_peek(state) == TOK_LIT_STRING) {
5045                         const char *token, *base;
5046                         char *name, *dir;
5047                         int name_len, dir_len;
5048                         tk = cpp_eat(state, TOK_LIT_STRING);
5049                         name = xmalloc(tk->str_len, "report_name");
5050                         token = tk->val.str + 1;
5051                         base = strrchr(token, '/');
5052                         name_len = tk->str_len -2;
5053                         if (base != 0) {
5054                                 dir_len = base - token;
5055                                 base++;
5056                                 name_len -= base - token;
5057                         } else {
5058                                 dir_len = 0;
5059                                 base = token;
5060                         }
5061                         memcpy(name, base, name_len);
5062                         name[name_len] = '\0';
5063                         dir = xmalloc(dir_len + 1, "report_dir");
5064                         memcpy(dir, token, dir_len);
5065                         dir[dir_len] = '\0';
5066                         file->report_line = override_line - 1;
5067                         file->report_name = name;
5068                         file->report_dir = dir;
5069                 }
5070                 break;
5071         }
5072         case TOK_MLINE:
5073         {
5074                 struct token *tk;
5075                 cpp_eat(state, TOK_MLINE);
5076                 tk = eat(state, TOK_LIT_INT);
5077                 file->report_line = strtoul(tk->val.str, 0, 10) -1;
5078                 if (cpp_peek(state) == TOK_LIT_STRING) {
5079                         const char *token, *base;
5080                         char *name, *dir;
5081                         int name_len, dir_len;
5082                         tk = cpp_eat(state, TOK_LIT_STRING);
5083                         name = xmalloc(tk->str_len, "report_name");
5084                         token = tk->val.str + 1;
5085                         base = strrchr(token, '/');
5086                         name_len = tk->str_len - 2;
5087                         if (base != 0) {
5088                                 dir_len = base - token;
5089                                 base++;
5090                                 name_len -= base - token;
5091                         } else {
5092                                 dir_len = 0;
5093                                 base = token;
5094                         }
5095                         memcpy(name, base, name_len);
5096                         name[name_len] = '\0';
5097                         dir = xmalloc(dir_len + 1, "report_dir");
5098                         memcpy(dir, token, dir_len);
5099                         dir[dir_len] = '\0';
5100                         file->report_name = name;
5101                         file->report_dir = dir;
5102                 }
5103                 break;
5104         }
5105         case TOK_MUNDEF:
5106         {
5107                 struct hash_entry *ident;
5108                 cpp_eat(state, TOK_MUNDEF);
5109                 if (if_eat(state))  /* quit early when #if'd out */
5110                         break;
5111                 
5112                 ident = cpp_eat(state, TOK_MIDENT)->ident;
5113
5114                 undef_macro(state, ident);
5115                 break;
5116         }
5117         case TOK_MPRAGMA:
5118                 cpp_eat(state, TOK_MPRAGMA);
5119                 if (if_eat(state))  /* quit early when #if'd out */
5120                         break;
5121                 warning(state, 0, "Ignoring pragma"); 
5122                 break;
5123         case TOK_MELIF:
5124                 cpp_eat(state, TOK_MELIF);
5125                 reenter_if(state, "#elif");
5126                 if (if_eat(state))   /* quit early when #if'd out */
5127                         break;
5128                 /* If the #if was taken the #elif just disables the following code */
5129                 if (if_value(state)) {
5130                         eat_tokens(state, TOK_MENDIF);
5131                 }
5132                 /* If the previous #if was not taken see if the #elif enables the 
5133                  * trailing code.
5134                  */
5135                 else {
5136                         set_if_value(state, mcexpr(state));
5137                         if (!if_value(state)) {
5138                                 eat_tokens(state, TOK_MELSE);
5139                         }
5140                 }
5141                 break;
5142         case TOK_MIF:
5143                 cpp_eat(state, TOK_MIF);
5144                 enter_if(state);
5145                 if (if_eat(state))  /* quit early when #if'd out */
5146                         break;
5147                 set_if_value(state, mcexpr(state));
5148                 if (!if_value(state)) {
5149                         eat_tokens(state, TOK_MELSE);
5150                 }
5151                 break;
5152         case TOK_MIFNDEF:
5153         {
5154                 struct hash_entry *ident;
5155
5156                 cpp_eat(state, TOK_MIFNDEF);
5157                 enter_if(state);
5158                 if (if_eat(state))  /* quit early when #if'd out */
5159                         break;
5160                 ident = cpp_eat(state, TOK_MIDENT)->ident;
5161                 set_if_value(state, ident->sym_define == 0);
5162                 if (!if_value(state)) {
5163                         eat_tokens(state, TOK_MELSE);
5164                 }
5165                 break;
5166         }
5167         case TOK_MIFDEF:
5168         {
5169                 struct hash_entry *ident;
5170                 cpp_eat(state, TOK_MIFDEF);
5171                 enter_if(state);
5172                 if (if_eat(state))  /* quit early when #if'd out */
5173                         break;
5174                 ident = cpp_eat(state, TOK_MIDENT)->ident;
5175                 set_if_value(state, ident->sym_define != 0);
5176                 if (!if_value(state)) {
5177                         eat_tokens(state, TOK_MELSE);
5178                 }
5179                 break;
5180         }
5181         case TOK_MELSE:
5182                 cpp_eat(state, TOK_MELSE);
5183                 enter_else(state, "#else");
5184                 if (!if_eat(state) && if_value(state)) {
5185                         eat_tokens(state, TOK_MENDIF);
5186                 }
5187                 break;
5188         case TOK_MENDIF:
5189                 cpp_eat(state, TOK_MENDIF);
5190                 exit_if(state, "#endif");
5191                 break;
5192         case TOK_MDEFINE:
5193         {
5194                 struct hash_entry *ident;
5195                 struct macro_arg *args, **larg;
5196                 const char *start, *mstart, *ptr;
5197
5198                 cpp_eat(state, TOK_MDEFINE);
5199                 if (if_eat(state))  /* quit early when #if'd out */
5200                         break;
5201
5202                 ident = cpp_eat(state, TOK_MIDENT)->ident;
5203                 args = 0;
5204                 larg = &args;
5205
5206                 /* Remember the start of the macro */
5207                 start = file->pos;
5208
5209                 /* Find the end of the line. */
5210                 for(ptr = start; *ptr != '\n'; ptr++)  
5211                         ;
5212
5213                 /* remove the trailing whitespace */
5214                 ptr-=1;
5215                 while(spacep(*ptr)) {
5216                         ptr--;
5217                 }
5218
5219                 /* Remove leading whitespace */
5220                 while(spacep(*start) && (start < ptr)) {
5221                         start++;
5222                 }
5223                 /* Remember where the macro starts */
5224                 mstart = start;
5225
5226                 /* Parse macro parameters */
5227                 if (lparen_peek(state, state->file)) {
5228                         cpp_eat(state, TOK_LPAREN);
5229                         
5230                         for(;;) {
5231                                 struct macro_arg *narg, *arg;
5232                                 struct hash_entry *aident;
5233                                 int tok;
5234
5235                                 tok = cpp_peek(state);
5236                                 if (!args && (tok == TOK_RPAREN)) {
5237                                         break;
5238                                 }
5239                                 else if (tok == TOK_DOTS) {
5240                                         cpp_eat(state, TOK_DOTS);
5241                                         aident = state->i___VA_ARGS__;
5242                                 } 
5243                                 else {
5244                                         aident = cpp_eat(state, TOK_MIDENT)->ident;
5245                                 }
5246                                 
5247                                 narg = xcmalloc(sizeof(*arg), "macro arg");
5248                                 narg->ident = aident;
5249
5250                                 /* Verify I don't have a duplicate identifier */
5251                                 for(arg = args; arg; arg = arg->next) {
5252                                         if (arg->ident == narg->ident) {
5253                                                 error(state, 0, "Duplicate macro arg `%s'",
5254                                                         narg->ident->name);
5255                                         }
5256                                 }
5257                                 /* Add the new argument to the end of the list */
5258                                 *larg = narg;
5259                                 larg = &narg->next;
5260
5261                                 if ((aident == state->i___VA_ARGS__) ||
5262                                         (cpp_peek(state) != TOK_COMMA)) {
5263                                         break;
5264                                 }
5265                                 cpp_eat(state, TOK_COMMA);
5266                         }
5267                         cpp_eat(state, TOK_RPAREN);
5268
5269                         /* Get the start of the macro body */
5270                         mstart = file->pos;
5271
5272                         /* Remove leading whitespace */
5273                         while(spacep(*mstart) && (mstart < ptr)) {
5274                                 mstart++;
5275                         }
5276                 }
5277                 define_macro(state, ident, start, ptr - start + 1, 
5278                         mstart - start, args);
5279                 break;
5280         }
5281         case TOK_MERROR:
5282         {
5283                 const char *end;
5284                 int len;
5285                 
5286                 cpp_eat(state, TOK_MERROR);
5287                 /* Find the end of the line */
5288                 for(end = file->pos; *end != '\n'; end++)
5289                         ;
5290                 len = (end - file->pos);
5291                 if (!if_eat(state)) {
5292                         error(state, 0, "%*.*s", len, len, file->pos);
5293                 }
5294                 file->pos = end;
5295                 break;
5296         }
5297         case TOK_MWARNING:
5298         {
5299                 const char *end;
5300                 int len;
5301                 
5302                 cpp_eat(state, TOK_MWARNING);
5303                 /* Find the end of the line */
5304                 for(end = file->pos; *end != '\n'; end++)
5305                         ;
5306                 len = (end - file->pos);
5307                 if (!if_eat(state)) {
5308                         warning(state, 0, "%*.*s", len, len, file->pos);
5309                 }
5310                 file->pos = end;
5311                 break;
5312         }
5313         case TOK_MINCLUDE:
5314         {
5315                 char *name;
5316                 int local;
5317                 local = 0;
5318                 name = 0;
5319
5320                 cpp_eat(state, TOK_MINCLUDE);
5321                 tok = peek(state);
5322                 if (tok == TOK_LIT_STRING) {
5323                         struct token *tk;
5324                         const char *token;
5325                         int name_len;
5326                         tk = eat(state, TOK_LIT_STRING);
5327                         name = xmalloc(tk->str_len, "include");
5328                         token = tk->val.str +1;
5329                         name_len = tk->str_len -2;
5330                         if (*token == '"') {
5331                                 token++;
5332                                 name_len--;
5333                         }
5334                         memcpy(name, token, name_len);
5335                         name[name_len] = '\0';
5336                         local = 1;
5337                 }
5338                 else if (tok == TOK_LESS) {
5339                         const char *start, *end;
5340                         eat(state, TOK_LESS);
5341                         start = file->pos;
5342                         for(end = start; *end != '\n'; end++) {
5343                                 if (*end == '>') {
5344                                         break;
5345                                 }
5346                         }
5347                         if (*end == '\n') {
5348                                 error(state, 0, "Unterminated include directive");
5349                         }
5350                         name = xmalloc(end - start + 1, "include");
5351                         memcpy(name, start, end - start);
5352                         name[end - start] = '\0';
5353                         file->pos = end;
5354                         local = 0;
5355                         eat(state, TOK_MORE);
5356                 }
5357                 else {
5358                         error(state, 0, "Invalid include directive");
5359                 }
5360                 /* Error if there are any tokens after the include */
5361                 if (cpp_peek(state) != TOK_EOL) {
5362                         error(state, 0, "garbage after include directive");
5363                 }
5364                 if (!if_eat(state)) {
5365                         compile_file(state, name, local);
5366                 }
5367                 xfree(name);
5368                 break;
5369         }
5370         case TOK_EOL:
5371                 /* Ignore # without a follwing ident */
5372                 break;
5373         default:
5374         {
5375                 const char *name1, *name2;
5376                 name1 = tokens[tok];
5377                 name2 = "";
5378                 if (tok == TOK_MIDENT) {
5379                         name2 = get_token(state, 1)->ident->name;
5380                 }
5381                 error(state, 0, "Invalid preprocessor directive: %s %s", 
5382                         name1, name2);
5383                 break;
5384         }
5385         }
5386         /* Consume the rest of the macro line */
5387         do {
5388                 tok = cpp_peek(state);
5389                 cpp_eat(state, tok);
5390         } while((tok != TOK_EOF) && (tok != TOK_EOL));
5391         state->token_base = old_token_base;
5392         return;
5393 }
5394
5395 /* Type helper functions */
5396
5397 static struct type *new_type(
5398         unsigned int type, struct type *left, struct type *right)
5399 {
5400         struct type *result;
5401         result = xmalloc(sizeof(*result), "type");
5402         result->type = type;
5403         result->left = left;
5404         result->right = right;
5405         result->field_ident = 0;
5406         result->type_ident = 0;
5407         result->elements = 0;
5408         return result;
5409 }
5410
5411 static struct type *clone_type(unsigned int specifiers, struct type *old)
5412 {
5413         struct type *result;
5414         result = xmalloc(sizeof(*result), "type");
5415         memcpy(result, old, sizeof(*result));
5416         result->type &= TYPE_MASK;
5417         result->type |= specifiers;
5418         return result;
5419 }
5420
5421 static struct type *dup_type(struct compile_state *state, struct type *orig)
5422 {
5423         struct type *new;
5424         new = xcmalloc(sizeof(*new), "type");
5425         new->type = orig->type;
5426         new->field_ident = orig->field_ident;
5427         new->type_ident  = orig->type_ident;
5428         new->elements    = orig->elements;
5429         if (orig->left) {
5430                 new->left = dup_type(state, orig->left);
5431         }
5432         if (orig->right) {
5433                 new->right = dup_type(state, orig->right);
5434         }
5435         return new;
5436 }
5437
5438
5439 static struct type *invalid_type(struct compile_state *state, struct type *type)
5440 {
5441         struct type *invalid, *member;
5442         invalid = 0;
5443         if (!type) {
5444                 internal_error(state, 0, "type missing?");
5445         }
5446         switch(type->type & TYPE_MASK) {
5447         case TYPE_VOID:
5448         case TYPE_CHAR:         case TYPE_UCHAR:
5449         case TYPE_SHORT:        case TYPE_USHORT:
5450         case TYPE_INT:          case TYPE_UINT:
5451         case TYPE_LONG:         case TYPE_ULONG:
5452         case TYPE_LLONG:        case TYPE_ULLONG:
5453         case TYPE_POINTER:
5454         case TYPE_ENUM:
5455                 break;
5456         case TYPE_BITFIELD:
5457                 invalid = invalid_type(state, type->left);
5458                 break;
5459         case TYPE_ARRAY:
5460                 invalid = invalid_type(state, type->left);
5461                 break;
5462         case TYPE_STRUCT:
5463         case TYPE_TUPLE:
5464                 member = type->left;
5465                 while(member && (invalid == 0) && 
5466                         ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5467                         invalid = invalid_type(state, member->left);
5468                         member = member->right;
5469                 }
5470                 if (!invalid) {
5471                         invalid = invalid_type(state, member);
5472                 }
5473                 break;
5474         case TYPE_UNION:
5475         case TYPE_JOIN:
5476                 member = type->left;
5477                 while(member && (invalid == 0) &&
5478                         ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5479                         invalid = invalid_type(state, member->left);
5480                         member = member->right;
5481                 }
5482                 if (!invalid) {
5483                         invalid = invalid_type(state, member);
5484                 }
5485                 break;
5486         default:
5487                 invalid = type;
5488                 break;
5489         }
5490         return invalid;
5491         
5492 }
5493
5494 #define MASK_UCHAR(X)    ((X) & ((ulong_t)0xff))
5495 #define MASK_USHORT(X)   ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
5496 static inline ulong_t mask_uint(ulong_t x)
5497 {
5498         if (SIZEOF_INT < SIZEOF_LONG) {
5499                 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT))) -1;
5500                 x &= mask;
5501         }
5502         return x;
5503 }
5504 #define MASK_UINT(X)      (mask_uint(X))
5505 #define MASK_ULONG(X)    (X)
5506
5507 static struct type void_type    = { .type  = TYPE_VOID };
5508 static struct type char_type    = { .type  = TYPE_CHAR };
5509 static struct type uchar_type   = { .type  = TYPE_UCHAR };
5510 static struct type short_type   = { .type  = TYPE_SHORT };
5511 static struct type ushort_type  = { .type  = TYPE_USHORT };
5512 static struct type int_type     = { .type  = TYPE_INT };
5513 static struct type uint_type    = { .type  = TYPE_UINT };
5514 static struct type long_type    = { .type  = TYPE_LONG };
5515 static struct type ulong_type   = { .type  = TYPE_ULONG };
5516 static struct type unknown_type = { .type  = TYPE_UNKNOWN };
5517
5518 static struct type void_ptr_type  = {
5519         .type = TYPE_POINTER,
5520         .left = &void_type,
5521 };
5522
5523 static struct type void_func_type = { 
5524         .type  = TYPE_FUNCTION,
5525         .left  = &void_type,
5526         .right = &void_type,
5527 };
5528
5529 static size_t bits_to_bytes(size_t size)
5530 {
5531         return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5532 }
5533
5534 static struct triple *variable(struct compile_state *state, struct type *type)
5535 {
5536         struct triple *result;
5537         if ((type->type & STOR_MASK) != STOR_PERM) {
5538                 result = triple(state, OP_ADECL, type, 0, 0);
5539                 generate_lhs_pieces(state, result);
5540         }
5541         else {
5542                 result = triple(state, OP_SDECL, type, 0, 0);
5543         }
5544         return result;
5545 }
5546
5547 static void stor_of(FILE *fp, struct type *type)
5548 {
5549         switch(type->type & STOR_MASK) {
5550         case STOR_AUTO:
5551                 fprintf(fp, "auto ");
5552                 break;
5553         case STOR_STATIC:
5554                 fprintf(fp, "static ");
5555                 break;
5556         case STOR_LOCAL:
5557                 fprintf(fp, "local ");
5558                 break;
5559         case STOR_EXTERN:
5560                 fprintf(fp, "extern ");
5561                 break;
5562         case STOR_REGISTER:
5563                 fprintf(fp, "register ");
5564                 break;
5565         case STOR_TYPEDEF:
5566                 fprintf(fp, "typedef ");
5567                 break;
5568         case STOR_INLINE | STOR_LOCAL:
5569                 fprintf(fp, "inline ");
5570                 break;
5571         case STOR_INLINE | STOR_STATIC:
5572                 fprintf(fp, "static inline");
5573                 break;
5574         case STOR_INLINE | STOR_EXTERN:
5575                 fprintf(fp, "extern inline");
5576                 break;
5577         default:
5578                 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5579                 break;
5580         }
5581 }
5582 static void qual_of(FILE *fp, struct type *type)
5583 {
5584         if (type->type & QUAL_CONST) {
5585                 fprintf(fp, " const");
5586         }
5587         if (type->type & QUAL_VOLATILE) {
5588                 fprintf(fp, " volatile");
5589         }
5590         if (type->type & QUAL_RESTRICT) {
5591                 fprintf(fp, " restrict");
5592         }
5593 }
5594
5595 static void name_of(FILE *fp, struct type *type)
5596 {
5597         unsigned int base_type;
5598         base_type = type->type & TYPE_MASK;
5599         if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5600                 stor_of(fp, type);
5601         }
5602         switch(base_type) {
5603         case TYPE_VOID:
5604                 fprintf(fp, "void");
5605                 qual_of(fp, type);
5606                 break;
5607         case TYPE_CHAR:
5608                 fprintf(fp, "signed char");
5609                 qual_of(fp, type);
5610                 break;
5611         case TYPE_UCHAR:
5612                 fprintf(fp, "unsigned char");
5613                 qual_of(fp, type);
5614                 break;
5615         case TYPE_SHORT:
5616                 fprintf(fp, "signed short");
5617                 qual_of(fp, type);
5618                 break;
5619         case TYPE_USHORT:
5620                 fprintf(fp, "unsigned short");
5621                 qual_of(fp, type);
5622                 break;
5623         case TYPE_INT:
5624                 fprintf(fp, "signed int");
5625                 qual_of(fp, type);
5626                 break;
5627         case TYPE_UINT:
5628                 fprintf(fp, "unsigned int");
5629                 qual_of(fp, type);
5630                 break;
5631         case TYPE_LONG:
5632                 fprintf(fp, "signed long");
5633                 qual_of(fp, type);
5634                 break;
5635         case TYPE_ULONG:
5636                 fprintf(fp, "unsigned long");
5637                 qual_of(fp, type);
5638                 break;
5639         case TYPE_POINTER:
5640                 name_of(fp, type->left);
5641                 fprintf(fp, " * ");
5642                 qual_of(fp, type);
5643                 break;
5644         case TYPE_PRODUCT:
5645                 name_of(fp, type->left);
5646                 fprintf(fp, ", ");
5647                 name_of(fp, type->right);
5648                 break;
5649         case TYPE_OVERLAP:
5650                 name_of(fp, type->left);
5651                 fprintf(fp, ",| ");
5652                 name_of(fp, type->right);
5653                 break;
5654         case TYPE_ENUM:
5655                 fprintf(fp, "enum %s", 
5656                         (type->type_ident)? type->type_ident->name : "");
5657                 qual_of(fp, type);
5658                 break;
5659         case TYPE_STRUCT:
5660                 fprintf(fp, "struct %s { ", 
5661                         (type->type_ident)? type->type_ident->name : "");
5662                 name_of(fp, type->left);
5663                 fprintf(fp, " } ");
5664                 qual_of(fp, type);
5665                 break;
5666         case TYPE_UNION:
5667                 fprintf(fp, "union %s { ", 
5668                         (type->type_ident)? type->type_ident->name : "");
5669                 name_of(fp, type->left);
5670                 fprintf(fp, " } ");
5671                 qual_of(fp, type);
5672                 break;
5673         case TYPE_FUNCTION:
5674                 name_of(fp, type->left);
5675                 fprintf(fp, " (*)(");
5676                 name_of(fp, type->right);
5677                 fprintf(fp, ")");
5678                 break;
5679         case TYPE_ARRAY:
5680                 name_of(fp, type->left);
5681                 fprintf(fp, " [%ld]", (long)(type->elements));
5682                 break;
5683         case TYPE_TUPLE:
5684                 fprintf(fp, "tuple { "); 
5685                 name_of(fp, type->left);
5686                 fprintf(fp, " } ");
5687                 qual_of(fp, type);
5688                 break;
5689         case TYPE_JOIN:
5690                 fprintf(fp, "join { ");
5691                 name_of(fp, type->left);
5692                 fprintf(fp, " } ");
5693                 qual_of(fp, type);
5694                 break;
5695         case TYPE_BITFIELD:
5696                 name_of(fp, type->left);
5697                 fprintf(fp, " : %d ", type->elements);
5698                 qual_of(fp, type);
5699                 break;
5700         case TYPE_UNKNOWN:
5701                 fprintf(fp, "unknown_t");
5702                 break;
5703         default:
5704                 fprintf(fp, "????: %x", base_type);
5705                 break;
5706         }
5707         if (type->field_ident && type->field_ident->name) {
5708                 fprintf(fp, " .%s", type->field_ident->name);
5709         }
5710 }
5711
5712 static size_t align_of(struct compile_state *state, struct type *type)
5713 {
5714         size_t align;
5715         align = 0;
5716         switch(type->type & TYPE_MASK) {
5717         case TYPE_VOID:
5718                 align = 1;
5719                 break;
5720         case TYPE_BITFIELD:
5721                 align = 1;
5722                 break;
5723         case TYPE_CHAR:
5724         case TYPE_UCHAR:
5725                 align = ALIGNOF_CHAR;
5726                 break;
5727         case TYPE_SHORT:
5728         case TYPE_USHORT:
5729                 align = ALIGNOF_SHORT;
5730                 break;
5731         case TYPE_INT:
5732         case TYPE_UINT:
5733         case TYPE_ENUM:
5734                 align = ALIGNOF_INT;
5735                 break;
5736         case TYPE_LONG:
5737         case TYPE_ULONG:
5738                 align = ALIGNOF_LONG;
5739                 break;
5740         case TYPE_POINTER:
5741                 align = ALIGNOF_POINTER;
5742                 break;
5743         case TYPE_PRODUCT:
5744         case TYPE_OVERLAP:
5745         {
5746                 size_t left_align, right_align;
5747                 left_align  = align_of(state, type->left);
5748                 right_align = align_of(state, type->right);
5749                 align = (left_align >= right_align) ? left_align : right_align;
5750                 break;
5751         }
5752         case TYPE_ARRAY:
5753                 align = align_of(state, type->left);
5754                 break;
5755         case TYPE_STRUCT:
5756         case TYPE_TUPLE:
5757         case TYPE_UNION:
5758         case TYPE_JOIN:
5759                 align = align_of(state, type->left);
5760                 break;
5761         default:
5762                 error(state, 0, "alignof not yet defined for type\n");
5763                 break;
5764         }
5765         return align;
5766 }
5767
5768 static size_t reg_align_of(struct compile_state *state, struct type *type)
5769 {
5770         size_t align;
5771         align = 0;
5772         switch(type->type & TYPE_MASK) {
5773         case TYPE_VOID:
5774                 align = 1;
5775                 break;
5776         case TYPE_BITFIELD:
5777                 align = 1;
5778                 break;
5779         case TYPE_CHAR:
5780         case TYPE_UCHAR:
5781                 align = REG_ALIGNOF_CHAR;
5782                 break;
5783         case TYPE_SHORT:
5784         case TYPE_USHORT:
5785                 align = REG_ALIGNOF_SHORT;
5786                 break;
5787         case TYPE_INT:
5788         case TYPE_UINT:
5789         case TYPE_ENUM:
5790                 align = REG_ALIGNOF_INT;
5791                 break;
5792         case TYPE_LONG:
5793         case TYPE_ULONG:
5794                 align = REG_ALIGNOF_LONG;
5795                 break;
5796         case TYPE_POINTER:
5797                 align = REG_ALIGNOF_POINTER;
5798                 break;
5799         case TYPE_PRODUCT:
5800         case TYPE_OVERLAP:
5801         {
5802                 size_t left_align, right_align;
5803                 left_align  = reg_align_of(state, type->left);
5804                 right_align = reg_align_of(state, type->right);
5805                 align = (left_align >= right_align) ? left_align : right_align;
5806                 break;
5807         }
5808         case TYPE_ARRAY:
5809                 align = reg_align_of(state, type->left);
5810                 break;
5811         case TYPE_STRUCT:
5812         case TYPE_UNION:
5813         case TYPE_TUPLE:
5814         case TYPE_JOIN:
5815                 align = reg_align_of(state, type->left);
5816                 break;
5817         default:
5818                 error(state, 0, "alignof not yet defined for type\n");
5819                 break;
5820         }
5821         return align;
5822 }
5823
5824 static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5825 {
5826         return bits_to_bytes(align_of(state, type));
5827 }
5828 static size_t size_of(struct compile_state *state, struct type *type);
5829 static size_t reg_size_of(struct compile_state *state, struct type *type);
5830
5831 static size_t needed_padding(struct compile_state *state, 
5832         struct type *type, size_t offset)
5833 {
5834         size_t padding, align;
5835         align = align_of(state, type);
5836         /* Align to the next machine word if the bitfield does completely
5837          * fit into the current word.
5838          */
5839         if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5840                 size_t size;
5841                 size = size_of(state, type);
5842                 if ((offset + type->elements)/size != offset/size) {
5843                         align = size;
5844                 }
5845         }
5846         padding = 0;
5847         if (offset % align) {
5848                 padding = align - (offset % align);
5849         }
5850         return padding;
5851 }
5852
5853 static size_t reg_needed_padding(struct compile_state *state, 
5854         struct type *type, size_t offset)
5855 {
5856         size_t padding, align;
5857         align = reg_align_of(state, type);
5858         /* Align to the next register word if the bitfield does completely
5859          * fit into the current register.
5860          */
5861         if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
5862                 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG))) 
5863         {
5864                 align = REG_SIZEOF_REG;
5865         }
5866         padding = 0;
5867         if (offset % align) {
5868                 padding = align - (offset % align);
5869         }
5870         return padding;
5871 }
5872
5873 static size_t size_of(struct compile_state *state, struct type *type)
5874 {
5875         size_t size;
5876         size = 0;
5877         switch(type->type & TYPE_MASK) {
5878         case TYPE_VOID:
5879                 size = 0;
5880                 break;
5881         case TYPE_BITFIELD:
5882                 size = type->elements;
5883                 break;
5884         case TYPE_CHAR:
5885         case TYPE_UCHAR:
5886                 size = SIZEOF_CHAR;
5887                 break;
5888         case TYPE_SHORT:
5889         case TYPE_USHORT:
5890                 size = SIZEOF_SHORT;
5891                 break;
5892         case TYPE_INT:
5893         case TYPE_UINT:
5894         case TYPE_ENUM:
5895                 size = SIZEOF_INT;
5896                 break;
5897         case TYPE_LONG:
5898         case TYPE_ULONG:
5899                 size = SIZEOF_LONG;
5900                 break;
5901         case TYPE_POINTER:
5902                 size = SIZEOF_POINTER;
5903                 break;
5904         case TYPE_PRODUCT:
5905         {
5906                 size_t pad;
5907                 size = 0;
5908                 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
5909                         pad = needed_padding(state, type->left, size);
5910                         size = size + pad + size_of(state, type->left);
5911                         type = type->right;
5912                 }
5913                 pad = needed_padding(state, type, size);
5914                 size = size + pad + size_of(state, type);
5915                 break;
5916         }
5917         case TYPE_OVERLAP:
5918         {
5919                 size_t size_left, size_right;
5920                 size_left = size_of(state, type->left);
5921                 size_right = size_of(state, type->right);
5922                 size = (size_left >= size_right)? size_left : size_right;
5923                 break;
5924         }
5925         case TYPE_ARRAY:
5926                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
5927                         internal_error(state, 0, "Invalid array type");
5928                 } else {
5929                         size = size_of(state, type->left) * type->elements;
5930                 }
5931                 break;
5932         case TYPE_STRUCT:
5933         case TYPE_TUPLE:
5934         {
5935                 size_t pad;
5936                 size = size_of(state, type->left);
5937                 /* Pad structures so their size is a multiples of their alignment */
5938                 pad = needed_padding(state, type, size);
5939                 size = size + pad;
5940                 break;
5941         }
5942         case TYPE_UNION:
5943         case TYPE_JOIN:
5944         {
5945                 size_t pad;
5946                 size = size_of(state, type->left);
5947                 /* Pad unions so their size is a multiple of their alignment */
5948                 pad = needed_padding(state, type, size);
5949                 size = size + pad;
5950                 break;
5951         }
5952         default:
5953                 internal_error(state, 0, "sizeof not yet defined for type");
5954                 break;
5955         }
5956         return size;
5957 }
5958
5959 static size_t reg_size_of(struct compile_state *state, struct type *type)
5960 {
5961         size_t size;
5962         size = 0;
5963         switch(type->type & TYPE_MASK) {
5964         case TYPE_VOID:
5965                 size = 0;
5966                 break;
5967         case TYPE_BITFIELD:
5968                 size = type->elements;
5969                 break;
5970         case TYPE_CHAR:
5971         case TYPE_UCHAR:
5972                 size = REG_SIZEOF_CHAR;
5973                 break;
5974         case TYPE_SHORT:
5975         case TYPE_USHORT:
5976                 size = REG_SIZEOF_SHORT;
5977                 break;
5978         case TYPE_INT:
5979         case TYPE_UINT:
5980         case TYPE_ENUM:
5981                 size = REG_SIZEOF_INT;
5982                 break;
5983         case TYPE_LONG:
5984         case TYPE_ULONG:
5985                 size = REG_SIZEOF_LONG;
5986                 break;
5987         case TYPE_POINTER:
5988                 size = REG_SIZEOF_POINTER;
5989                 break;
5990         case TYPE_PRODUCT:
5991         {
5992                 size_t pad;
5993                 size = 0;
5994                 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
5995                         pad = reg_needed_padding(state, type->left, size);
5996                         size = size + pad + reg_size_of(state, type->left);
5997                         type = type->right;
5998                 }
5999                 pad = reg_needed_padding(state, type, size);
6000                 size = size + pad + reg_size_of(state, type);
6001                 break;
6002         }
6003         case TYPE_OVERLAP:
6004         {
6005                 size_t size_left, size_right;
6006                 size_left  = reg_size_of(state, type->left);
6007                 size_right = reg_size_of(state, type->right);
6008                 size = (size_left >= size_right)? size_left : size_right;
6009                 break;
6010         }
6011         case TYPE_ARRAY:
6012                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6013                         internal_error(state, 0, "Invalid array type");
6014                 } else {
6015                         size = reg_size_of(state, type->left) * type->elements;
6016                 }
6017                 break;
6018         case TYPE_STRUCT:
6019         case TYPE_TUPLE:
6020         {
6021                 size_t pad;
6022                 size = reg_size_of(state, type->left);
6023                 /* Pad structures so their size is a multiples of their alignment */
6024                 pad = reg_needed_padding(state, type, size);
6025                 size = size + pad;
6026                 break;
6027         }
6028         case TYPE_UNION:
6029         case TYPE_JOIN:
6030         {
6031                 size_t pad;
6032                 size = reg_size_of(state, type->left);
6033                 /* Pad unions so their size is a multiple of their alignment */
6034                 pad = reg_needed_padding(state, type, size);
6035                 size = size + pad;
6036                 break;
6037         }
6038         default:
6039                 internal_error(state, 0, "sizeof not yet defined for type");
6040                 break;
6041         }
6042         return size;
6043 }
6044
6045 static size_t registers_of(struct compile_state *state, struct type *type)
6046 {
6047         size_t registers;
6048         registers = reg_size_of(state, type);
6049         registers += REG_SIZEOF_REG - 1;
6050         registers /= REG_SIZEOF_REG;
6051         return registers;
6052 }
6053
6054 static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6055 {
6056         return bits_to_bytes(size_of(state, type));
6057 }
6058
6059 static size_t field_offset(struct compile_state *state, 
6060         struct type *type, struct hash_entry *field)
6061 {
6062         struct type *member;
6063         size_t size;
6064
6065         size = 0;
6066         member = 0;
6067         if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6068                 member = type->left;
6069                 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6070                         size += needed_padding(state, member->left, size);
6071                         if (member->left->field_ident == field) {
6072                                 member = member->left;
6073                                 break;
6074                         }
6075                         size += size_of(state, member->left);
6076                         member = member->right;
6077                 }
6078                 size += needed_padding(state, member, size);
6079         }
6080         else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6081                 member = type->left;
6082                 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6083                         if (member->left->field_ident == field) {
6084                                 member = member->left;
6085                                 break;
6086                         }
6087                         member = member->right;
6088                 }
6089         }
6090         else {
6091                 internal_error(state, 0, "field_offset only works on structures and unions");
6092         }
6093
6094         if (!member || (member->field_ident != field)) {
6095                 error(state, 0, "member %s not present", field->name);
6096         }
6097         return size;
6098 }
6099
6100 static size_t field_reg_offset(struct compile_state *state, 
6101         struct type *type, struct hash_entry *field)
6102 {
6103         struct type *member;
6104         size_t size;
6105
6106         size = 0;
6107         member = 0;
6108         if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6109                 member = type->left;
6110                 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6111                         size += reg_needed_padding(state, member->left, size);
6112                         if (member->left->field_ident == field) {
6113                                 member = member->left;
6114                                 break;
6115                         }
6116                         size += reg_size_of(state, member->left);
6117                         member = member->right;
6118                 }
6119         }
6120         else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6121                 member = type->left;
6122                 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6123                         if (member->left->field_ident == field) {
6124                                 member = member->left;
6125                                 break;
6126                         }
6127                         member = member->right;
6128                 }
6129         }
6130         else {
6131                 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6132         }
6133
6134         size += reg_needed_padding(state, member, size);
6135         if (!member || (member->field_ident != field)) {
6136                 error(state, 0, "member %s not present", field->name);
6137         }
6138         return size;
6139 }
6140
6141 static struct type *field_type(struct compile_state *state, 
6142         struct type *type, struct hash_entry *field)
6143 {
6144         struct type *member;
6145
6146         member = 0;
6147         if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6148                 member = type->left;
6149                 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6150                         if (member->left->field_ident == field) {
6151                                 member = member->left;
6152                                 break;
6153                         }
6154                         member = member->right;
6155                 }
6156         }
6157         else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6158                 member = type->left;
6159                 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6160                         if (member->left->field_ident == field) {
6161                                 member = member->left;
6162                                 break;
6163                         }
6164                         member = member->right;
6165                 }
6166         }
6167         else {
6168                 internal_error(state, 0, "field_type only works on structures and unions");
6169         }
6170         
6171         if (!member || (member->field_ident != field)) {
6172                 error(state, 0, "member %s not present", field->name);
6173         }
6174         return member;
6175 }
6176
6177 static size_t index_offset(struct compile_state *state, 
6178         struct type *type, ulong_t index)
6179 {
6180         struct type *member;
6181         size_t size;
6182         size = 0;
6183         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6184                 size = size_of(state, type->left) * index;
6185         }
6186         else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6187                 ulong_t i;
6188                 member = type->left;
6189                 i = 0;
6190                 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6191                         size += needed_padding(state, member->left, size);
6192                         if (i == index) {
6193                                 member = member->left;
6194                                 break;
6195                         }
6196                         size += size_of(state, member->left);
6197                         i++;
6198                         member = member->right;
6199                 }
6200                 size += needed_padding(state, member, size);
6201                 if (i != index) {
6202                         internal_error(state, 0, "Missing member index: %u", index);
6203                 }
6204         }
6205         else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6206                 ulong_t i;
6207                 size = 0;
6208                 member = type->left;
6209                 i = 0;
6210                 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6211                         if (i == index) {
6212                                 member = member->left;
6213                                 break;
6214                         }
6215                         i++;
6216                         member = member->right;
6217                 }
6218                 if (i != index) {
6219                         internal_error(state, 0, "Missing member index: %u", index);
6220                 }
6221         }
6222         else {
6223                 internal_error(state, 0, 
6224                         "request for index %u in something not an array, tuple or join",
6225                         index);
6226         }
6227         return size;
6228 }
6229
6230 static size_t index_reg_offset(struct compile_state *state, 
6231         struct type *type, ulong_t index)
6232 {
6233         struct type *member;
6234         size_t size;
6235         size = 0;
6236         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6237                 size = reg_size_of(state, type->left) * index;
6238         }
6239         else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6240                 ulong_t i;
6241                 member = type->left;
6242                 i = 0;
6243                 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6244                         size += reg_needed_padding(state, member->left, size);
6245                         if (i == index) {
6246                                 member = member->left;
6247                                 break;
6248                         }
6249                         size += reg_size_of(state, member->left);
6250                         i++;
6251                         member = member->right;
6252                 }
6253                 size += reg_needed_padding(state, member, size);
6254                 if (i != index) {
6255                         internal_error(state, 0, "Missing member index: %u", index);
6256                 }
6257                 
6258         }
6259         else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6260                 ulong_t i;
6261                 size = 0;
6262                 member = type->left;
6263                 i = 0;
6264                 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6265                         if (i == index) {
6266                                 member = member->left;
6267                                 break;
6268                         }
6269                         i++;
6270                         member = member->right;
6271                 }
6272                 if (i != index) {
6273                         internal_error(state, 0, "Missing member index: %u", index);
6274                 }
6275         }
6276         else {
6277                 internal_error(state, 0, 
6278                         "request for index %u in something not an array, tuple or join",
6279                         index);
6280         }
6281         return size;
6282 }
6283
6284 static struct type *index_type(struct compile_state *state,
6285         struct type *type, ulong_t index)
6286 {
6287         struct type *member;
6288         if (index >= type->elements) {
6289                 internal_error(state, 0, "Invalid element %u requested", index);
6290         }
6291         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6292                 member = type->left;
6293         }
6294         else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6295                 ulong_t i;
6296                 member = type->left;
6297                 i = 0;
6298                 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6299                         if (i == index) {
6300                                 member = member->left;
6301                                 break;
6302                         }
6303                         i++;
6304                         member = member->right;
6305                 }
6306                 if (i != index) {
6307                         internal_error(state, 0, "Missing member index: %u", index);
6308                 }
6309         }
6310         else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6311                 ulong_t i;
6312                 member = type->left;
6313                 i = 0;
6314                 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6315                         if (i == index) {
6316                                 member = member->left;
6317                                 break;
6318                         }
6319                         i++;
6320                         member = member->right;
6321                 }
6322                 if (i != index) {
6323                         internal_error(state, 0, "Missing member index: %u", index);
6324                 }
6325         }
6326         else {
6327                 member = 0;
6328                 internal_error(state, 0, 
6329                         "request for index %u in something not an array, tuple or join",
6330                         index);
6331         }
6332         return member;
6333 }
6334
6335 static struct type *unpack_type(struct compile_state *state, struct type *type)
6336 {
6337         /* If I have a single register compound type not a bit-field
6338          * find the real type.
6339          */
6340         struct type *start_type;
6341         size_t size;
6342         /* Get out early if I need multiple registers for this type */
6343         size = reg_size_of(state, type);
6344         if (size > REG_SIZEOF_REG) {
6345                 return type;
6346         }
6347         /* Get out early if I don't need any registers for this type */
6348         if (size == 0) {
6349                 return &void_type;
6350         }
6351         /* Loop until I have no more layers I can remove */
6352         do {
6353                 start_type = type;
6354                 switch(type->type & TYPE_MASK) {
6355                 case TYPE_ARRAY:
6356                         /* If I have a single element the unpacked type
6357                          * is that element.
6358                          */
6359                         if (type->elements == 1) {
6360                                 type = type->left;
6361                         }
6362                         break;
6363                 case TYPE_STRUCT:
6364                 case TYPE_TUPLE:
6365                         /* If I have a single element the unpacked type
6366                          * is that element.
6367                          */
6368                         if (type->elements == 1) {
6369                                 type = type->left;
6370                         }
6371                         /* If I have multiple elements the unpacked
6372                          * type is the non-void element.
6373                          */
6374                         else {
6375                                 struct type *next, *member;
6376                                 struct type *sub_type;
6377                                 sub_type = 0;
6378                                 next = type->left;
6379                                 while(next) {
6380                                         member = next;
6381                                         next = 0;
6382                                         if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6383                                                 next = member->right;
6384                                                 member = member->left;
6385                                         }
6386                                         if (reg_size_of(state, member) > 0) {
6387                                                 if (sub_type) {
6388                                                         internal_error(state, 0, "true compound type in a register");
6389                                                 }
6390                                                 sub_type = member;
6391                                         }
6392                                 }
6393                                 if (sub_type) {
6394                                         type = sub_type;
6395                                 }
6396                         }
6397                         break;
6398
6399                 case TYPE_UNION:
6400                 case TYPE_JOIN:
6401                         /* If I have a single element the unpacked type
6402                          * is that element.
6403                          */
6404                         if (type->elements == 1) {
6405                                 type = type->left;
6406                         }
6407                         /* I can't in general unpack union types */
6408                         break;
6409                 default:
6410                         /* If I'm not a compound type I can't unpack it */
6411                         break;
6412                 }
6413         } while(start_type != type);
6414         switch(type->type & TYPE_MASK) {
6415         case TYPE_STRUCT:
6416         case TYPE_ARRAY:
6417         case TYPE_TUPLE:
6418                 internal_error(state, 0, "irredicible type?");
6419                 break;
6420         }
6421         return type;
6422 }
6423
6424 static int equiv_types(struct type *left, struct type *right);
6425 static int is_compound_type(struct type *type);
6426
6427 static struct type *reg_type(
6428         struct compile_state *state, struct type *type, int reg_offset)
6429 {
6430         struct type *member;
6431         size_t size;
6432 #if 1
6433         struct type *invalid;
6434         invalid = invalid_type(state, type);
6435         if (invalid) {
6436                 fprintf(state->errout, "type: ");
6437                 name_of(state->errout, type);
6438                 fprintf(state->errout, "\n");
6439                 fprintf(state->errout, "invalid: ");
6440                 name_of(state->errout, invalid);
6441                 fprintf(state->errout, "\n");
6442                 internal_error(state, 0, "bad input type?");
6443         }
6444 #endif
6445
6446         size = reg_size_of(state, type);
6447         if (reg_offset > size) {
6448                 member = 0;
6449                 fprintf(state->errout, "type: ");
6450                 name_of(state->errout, type);
6451                 fprintf(state->errout, "\n");
6452                 internal_error(state, 0, "offset outside of type");
6453         }
6454         else {
6455                 switch(type->type & TYPE_MASK) {
6456                         /* Don't do anything with the basic types */
6457                 case TYPE_VOID:
6458                 case TYPE_CHAR:         case TYPE_UCHAR:
6459                 case TYPE_SHORT:        case TYPE_USHORT:
6460                 case TYPE_INT:          case TYPE_UINT:
6461                 case TYPE_LONG:         case TYPE_ULONG:
6462                 case TYPE_LLONG:        case TYPE_ULLONG:
6463                 case TYPE_FLOAT:        case TYPE_DOUBLE:
6464                 case TYPE_LDOUBLE:
6465                 case TYPE_POINTER:
6466                 case TYPE_ENUM:
6467                 case TYPE_BITFIELD:
6468                         member = type;
6469                         break;
6470                 case TYPE_ARRAY:
6471                         member = type->left;
6472                         size = reg_size_of(state, member);
6473                         if (size > REG_SIZEOF_REG) {
6474                                 member = reg_type(state, member, reg_offset % size);
6475                         }
6476                         break;
6477                 case TYPE_STRUCT:
6478                 case TYPE_TUPLE:
6479                 {
6480                         size_t offset;
6481                         offset = 0;
6482                         member = type->left;
6483                         while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6484                                 size = reg_size_of(state, member->left);
6485                                 offset += reg_needed_padding(state, member->left, offset);
6486                                 if ((offset + size) > reg_offset) {
6487                                         member = member->left;
6488                                         break;
6489                                 }
6490                                 offset += size;
6491                                 member = member->right;
6492                         }
6493                         offset += reg_needed_padding(state, member, offset);
6494                         member = reg_type(state, member, reg_offset - offset);
6495                         break;
6496                 }
6497                 case TYPE_UNION:
6498                 case TYPE_JOIN:
6499                 {
6500                         struct type *join, **jnext, *mnext;
6501                         join = new_type(TYPE_JOIN, 0, 0);
6502                         jnext = &join->left;
6503                         mnext = type->left;
6504                         while(mnext) {
6505                                 size_t size;
6506                                 member = mnext;
6507                                 mnext = 0;
6508                                 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6509                                         mnext = member->right;
6510                                         member = member->left;
6511                                 }
6512                                 size = reg_size_of(state, member);
6513                                 if (size > reg_offset) {
6514                                         struct type *part, *hunt;
6515                                         part = reg_type(state, member, reg_offset);
6516                                         /* See if this type is already in the union */
6517                                         hunt = join->left;
6518                                         while(hunt) {
6519                                                 struct type *test = hunt;
6520                                                 hunt = 0;
6521                                                 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6522                                                         hunt = test->right;
6523                                                         test = test->left;
6524                                                 }
6525                                                 if (equiv_types(part, test)) {
6526                                                         goto next;
6527                                                 }
6528                                         }
6529                                         /* Nope add it */
6530                                         if (!*jnext) {
6531                                                 *jnext = part;
6532                                         } else {
6533                                                 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6534                                                 jnext = &(*jnext)->right;
6535                                         }
6536                                         join->elements++;
6537                                 }
6538                         next:
6539                                 ;
6540                         }
6541                         if (join->elements == 0) {
6542                                 internal_error(state, 0, "No elements?");
6543                         }
6544                         member = join;
6545                         break;
6546                 }
6547                 default:
6548                         member = 0;
6549                         fprintf(state->errout, "type: ");
6550                         name_of(state->errout, type);
6551                         fprintf(state->errout, "\n");
6552                         internal_error(state, 0, "reg_type not yet defined for type");
6553                         
6554                 }
6555         }
6556         /* If I have a single register compound type not a bit-field
6557          * find the real type.
6558          */
6559         member = unpack_type(state, member);
6560                 ;
6561         size  = reg_size_of(state, member);
6562         if (size > REG_SIZEOF_REG) {
6563                 internal_error(state, 0, "Cannot find type of single register");
6564         }
6565 #if 1
6566         invalid = invalid_type(state, member);
6567         if (invalid) {
6568                 fprintf(state->errout, "type: ");
6569                 name_of(state->errout, member);
6570                 fprintf(state->errout, "\n");
6571                 fprintf(state->errout, "invalid: ");
6572                 name_of(state->errout, invalid);
6573                 fprintf(state->errout, "\n");
6574                 internal_error(state, 0, "returning bad type?");
6575         }
6576 #endif
6577         return member;
6578 }
6579
6580 static struct type *next_field(struct compile_state *state,
6581         struct type *type, struct type *prev_member) 
6582 {
6583         struct type *member;
6584         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6585                 internal_error(state, 0, "next_field only works on structures");
6586         }
6587         member = type->left;
6588         while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6589                 if (!prev_member) {
6590                         member = member->left;
6591                         break;
6592                 }
6593                 if (member->left == prev_member) {
6594                         prev_member = 0;
6595                 }
6596                 member = member->right;
6597         }
6598         if (member == prev_member) {
6599                 prev_member = 0;
6600         }
6601         if (prev_member) {
6602                 internal_error(state, 0, "prev_member %s not present", 
6603                         prev_member->field_ident->name);
6604         }
6605         return member;
6606 }
6607
6608 typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type, 
6609         size_t ret_offset, size_t mem_offset, void *arg);
6610
6611 static void walk_type_fields(struct compile_state *state,
6612         struct type *type, size_t reg_offset, size_t mem_offset,
6613         walk_type_fields_cb_t cb, void *arg);
6614
6615 static void walk_struct_fields(struct compile_state *state,
6616         struct type *type, size_t reg_offset, size_t mem_offset,
6617         walk_type_fields_cb_t cb, void *arg)
6618 {
6619         struct type *tptr;
6620         ulong_t i;
6621         if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6622                 internal_error(state, 0, "walk_struct_fields only works on structures");
6623         }
6624         tptr = type->left;
6625         for(i = 0; i < type->elements; i++) {
6626                 struct type *mtype;
6627                 mtype = tptr;
6628                 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6629                         mtype = mtype->left;
6630                 }
6631                 walk_type_fields(state, mtype, 
6632                         reg_offset + 
6633                         field_reg_offset(state, type, mtype->field_ident),
6634                         mem_offset + 
6635                         field_offset(state, type, mtype->field_ident),
6636                         cb, arg);
6637                 tptr = tptr->right;
6638         }
6639         
6640 }
6641
6642 static void walk_type_fields(struct compile_state *state,
6643         struct type *type, size_t reg_offset, size_t mem_offset,
6644         walk_type_fields_cb_t cb, void *arg)
6645 {
6646         switch(type->type & TYPE_MASK) {
6647         case TYPE_STRUCT:
6648                 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6649                 break;
6650         case TYPE_CHAR:
6651         case TYPE_UCHAR:
6652         case TYPE_SHORT:
6653         case TYPE_USHORT:
6654         case TYPE_INT:
6655         case TYPE_UINT:
6656         case TYPE_LONG:
6657         case TYPE_ULONG:
6658                 cb(state, type, reg_offset, mem_offset, arg);
6659                 break;
6660         case TYPE_VOID:
6661                 break;
6662         default:
6663                 internal_error(state, 0, "walk_type_fields not yet implemented for type");
6664         }
6665 }
6666
6667 static void arrays_complete(struct compile_state *state, struct type *type)
6668 {
6669         if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6670                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6671                         error(state, 0, "array size not specified");
6672                 }
6673                 arrays_complete(state, type->left);
6674         }
6675 }
6676
6677 static unsigned int get_basic_type(struct type *type)
6678 {
6679         unsigned int basic;
6680         basic = type->type & TYPE_MASK;
6681         /* Convert enums to ints */
6682         if (basic == TYPE_ENUM) {
6683                 basic = TYPE_INT;
6684         }
6685         /* Convert bitfields to standard types */
6686         else if (basic == TYPE_BITFIELD) {
6687                 if (type->elements <= SIZEOF_CHAR) {
6688                         basic = TYPE_CHAR;
6689                 }
6690                 else if (type->elements <= SIZEOF_SHORT) {
6691                         basic = TYPE_SHORT;
6692                 }
6693                 else if (type->elements <= SIZEOF_INT) {
6694                         basic = TYPE_INT;
6695                 }
6696                 else if (type->elements <= SIZEOF_LONG) {
6697                         basic = TYPE_LONG;
6698                 }
6699                 if (!TYPE_SIGNED(type->left->type)) {
6700                         basic += 1;
6701                 }
6702         }
6703         return basic;
6704 }
6705
6706 static unsigned int do_integral_promotion(unsigned int type)
6707 {
6708         if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
6709                 type = TYPE_INT;
6710         }
6711         return type;
6712 }
6713
6714 static unsigned int do_arithmetic_conversion(
6715         unsigned int left, unsigned int right)
6716 {
6717         if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6718                 return TYPE_LDOUBLE;
6719         }
6720         else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6721                 return TYPE_DOUBLE;
6722         }
6723         else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6724                 return TYPE_FLOAT;
6725         }
6726         left = do_integral_promotion(left);
6727         right = do_integral_promotion(right);
6728         /* If both operands have the same size done */
6729         if (left == right) {
6730                 return left;
6731         }
6732         /* If both operands have the same signedness pick the larger */
6733         else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6734                 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6735         }
6736         /* If the signed type can hold everything use it */
6737         else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6738                 return left;
6739         }
6740         else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6741                 return right;
6742         }
6743         /* Convert to the unsigned type with the same rank as the signed type */
6744         else if (TYPE_SIGNED(left)) {
6745                 return TYPE_MKUNSIGNED(left);
6746         }
6747         else {
6748                 return TYPE_MKUNSIGNED(right);
6749         }
6750 }
6751
6752 /* see if two types are the same except for qualifiers */
6753 static int equiv_types(struct type *left, struct type *right)
6754 {
6755         unsigned int type;
6756         /* Error if the basic types do not match */
6757         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6758                 return 0;
6759         }
6760         type = left->type & TYPE_MASK;
6761         /* If the basic types match and it is a void type we are done */
6762         if (type == TYPE_VOID) {
6763                 return 1;
6764         }
6765         /* For bitfields we need to compare the sizes */
6766         else if (type == TYPE_BITFIELD) {
6767                 return (left->elements == right->elements) &&
6768                         (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6769         }
6770         /* if the basic types match and it is an arithmetic type we are done */
6771         else if (TYPE_ARITHMETIC(type)) {
6772                 return 1;
6773         }
6774         /* If it is a pointer type recurse and keep testing */
6775         else if (type == TYPE_POINTER) {
6776                 return equiv_types(left->left, right->left);
6777         }
6778         else if (type == TYPE_ARRAY) {
6779                 return (left->elements == right->elements) &&
6780                         equiv_types(left->left, right->left);
6781         }
6782         /* test for struct equality */
6783         else if (type == TYPE_STRUCT) {
6784                 return left->type_ident == right->type_ident;
6785         }
6786         /* test for union equality */
6787         else if (type == TYPE_UNION) {
6788                 return left->type_ident == right->type_ident;
6789         }
6790         /* Test for equivalent functions */
6791         else if (type == TYPE_FUNCTION) {
6792                 return equiv_types(left->left, right->left) &&
6793                         equiv_types(left->right, right->right);
6794         }
6795         /* We only see TYPE_PRODUCT as part of function equivalence matching */
6796         /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
6797         else if (type == TYPE_PRODUCT) {
6798                 return equiv_types(left->left, right->left) &&
6799                         equiv_types(left->right, right->right);
6800         }
6801         /* We should see TYPE_OVERLAP when comparing joins */
6802         else if (type == TYPE_OVERLAP) {
6803                 return equiv_types(left->left, right->left) &&
6804                         equiv_types(left->right, right->right);
6805         }
6806         /* Test for equivalence of tuples */
6807         else if (type == TYPE_TUPLE) {
6808                 return (left->elements == right->elements) &&
6809                         equiv_types(left->left, right->left);
6810         }
6811         /* Test for equivalence of joins */
6812         else if (type == TYPE_JOIN) {
6813                 return (left->elements == right->elements) &&
6814                         equiv_types(left->left, right->left);
6815         }
6816         else {
6817                 return 0;
6818         }
6819 }
6820
6821 static int equiv_ptrs(struct type *left, struct type *right)
6822 {
6823         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6824                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6825                 return 0;
6826         }
6827         return equiv_types(left->left, right->left);
6828 }
6829
6830 static struct type *compatible_types(struct type *left, struct type *right)
6831 {
6832         struct type *result;
6833         unsigned int type, qual_type;
6834         /* Error if the basic types do not match */
6835         if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6836                 return 0;
6837         }
6838         type = left->type & TYPE_MASK;
6839         qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6840         result = 0;
6841         /* if the basic types match and it is an arithmetic type we are done */
6842         if (TYPE_ARITHMETIC(type)) {
6843                 result = new_type(qual_type, 0, 0);
6844         }
6845         /* If it is a pointer type recurse and keep testing */
6846         else if (type == TYPE_POINTER) {
6847                 result = compatible_types(left->left, right->left);
6848                 if (result) {
6849                         result = new_type(qual_type, result, 0);
6850                 }
6851         }
6852         /* test for struct equality */
6853         else if (type == TYPE_STRUCT) {
6854                 if (left->type_ident == right->type_ident) {
6855                         result = left;
6856                 }
6857         }
6858         /* test for union equality */
6859         else if (type == TYPE_UNION) {
6860                 if (left->type_ident == right->type_ident) {
6861                         result = left;
6862                 }
6863         }
6864         /* Test for equivalent functions */
6865         else if (type == TYPE_FUNCTION) {
6866                 struct type *lf, *rf;
6867                 lf = compatible_types(left->left, right->left);
6868                 rf = compatible_types(left->right, right->right);
6869                 if (lf && rf) {
6870                         result = new_type(qual_type, lf, rf);
6871                 }
6872         }
6873         /* We only see TYPE_PRODUCT as part of function equivalence matching */
6874         else if (type == TYPE_PRODUCT) {
6875                 struct type *lf, *rf;
6876                 lf = compatible_types(left->left, right->left);
6877                 rf = compatible_types(left->right, right->right);
6878                 if (lf && rf) {
6879                         result = new_type(qual_type, lf, rf);
6880                 }
6881         }
6882         else {
6883                 /* Nothing else is compatible */
6884         }
6885         return result;
6886 }
6887
6888 /* See if left is a equivalent to right or right is a union member of left */
6889 static int is_subset_type(struct type *left, struct type *right)
6890 {
6891         if (equiv_types(left, right)) {
6892                 return 1;
6893         }
6894         if ((left->type & TYPE_MASK) == TYPE_JOIN) {
6895                 struct type *member, *mnext;
6896                 mnext = left->left;
6897                 while(mnext) {
6898                         member = mnext;
6899                         mnext = 0;
6900                         if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6901                                 mnext = member->right;
6902                                 member = member->left;
6903                         }
6904                         if (is_subset_type( member, right)) {
6905                                 return 1;
6906                         }
6907                 }
6908         }
6909         return 0;
6910 }
6911
6912 static struct type *compatible_ptrs(struct type *left, struct type *right)
6913 {
6914         struct type *result;
6915         if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6916                 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6917                 return 0;
6918         }
6919         result = compatible_types(left->left, right->left);
6920         if (result) {
6921                 unsigned int qual_type;
6922                 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6923                 result = new_type(qual_type, result, 0);
6924         }
6925         return result;
6926         
6927 }
6928 static struct triple *integral_promotion(
6929         struct compile_state *state, struct triple *def)
6930 {
6931         struct type *type;
6932         type = def->type;
6933         /* As all operations are carried out in registers
6934          * the values are converted on load I just convert
6935          * logical type of the operand.
6936          */
6937         if (TYPE_INTEGER(type->type)) {
6938                 unsigned int int_type;
6939                 int_type = type->type & ~TYPE_MASK;
6940                 int_type |= do_integral_promotion(get_basic_type(type));
6941                 if (int_type != type->type) {
6942                         if (def->op != OP_LOAD) {
6943                                 def->type = new_type(int_type, 0, 0);
6944                         }
6945                         else {
6946                                 def = triple(state, OP_CONVERT, 
6947                                         new_type(int_type, 0, 0), def, 0);
6948                         }
6949                 }
6950         }
6951         return def;
6952 }
6953
6954
6955 static void arithmetic(struct compile_state *state, struct triple *def)
6956 {
6957         if (!TYPE_ARITHMETIC(def->type->type)) {
6958                 error(state, 0, "arithmetic type expexted");
6959         }
6960 }
6961
6962 static void ptr_arithmetic(struct compile_state *state, struct triple *def)
6963 {
6964         if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
6965                 error(state, def, "pointer or arithmetic type expected");
6966         }
6967 }
6968
6969 static int is_integral(struct triple *ins)
6970 {
6971         return TYPE_INTEGER(ins->type->type);
6972 }
6973
6974 static void integral(struct compile_state *state, struct triple *def)
6975 {
6976         if (!is_integral(def)) {
6977                 error(state, 0, "integral type expected");
6978         }
6979 }
6980
6981
6982 static void bool(struct compile_state *state, struct triple *def)
6983 {
6984         if (!TYPE_ARITHMETIC(def->type->type) &&
6985                 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
6986                 error(state, 0, "arithmetic or pointer type expected");
6987         }
6988 }
6989
6990 static int is_signed(struct type *type)
6991 {
6992         if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6993                 type = type->left;
6994         }
6995         return !!TYPE_SIGNED(type->type);
6996 }
6997 static int is_compound_type(struct type *type)
6998 {
6999         int is_compound;
7000         switch((type->type & TYPE_MASK)) {
7001         case TYPE_ARRAY:
7002         case TYPE_STRUCT:
7003         case TYPE_TUPLE:
7004         case TYPE_UNION:
7005         case TYPE_JOIN: 
7006                 is_compound = 1;
7007                 break;
7008         default:
7009                 is_compound = 0;
7010                 break;
7011         }
7012         return is_compound;
7013 }
7014
7015 /* Is this value located in a register otherwise it must be in memory */
7016 static int is_in_reg(struct compile_state *state, struct triple *def)
7017 {
7018         int in_reg;
7019         if (def->op == OP_ADECL) {
7020                 in_reg = 1;
7021         }
7022         else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7023                 in_reg = 0;
7024         }
7025         else if (triple_is_part(state, def)) {
7026                 in_reg = is_in_reg(state, MISC(def, 0));
7027         }
7028         else {
7029                 internal_error(state, def, "unknown expr storage location");
7030                 in_reg = -1;
7031         }
7032         return in_reg;
7033 }
7034
7035 /* Is this an auto or static variable location? Something that can
7036  * be assigned to.  Otherwise it must must be a pure value, a temporary.
7037  */
7038 static int is_lvalue(struct compile_state *state, struct triple *def)
7039 {
7040         int ret;
7041         ret = 0;
7042         if (!def) {
7043                 return 0;
7044         }
7045         if ((def->op == OP_ADECL) || 
7046                 (def->op == OP_SDECL) || 
7047                 (def->op == OP_DEREF) ||
7048                 (def->op == OP_BLOBCONST) ||
7049                 (def->op == OP_LIST)) {
7050                 ret = 1;
7051         }
7052         else if (triple_is_part(state, def)) {
7053                 ret = is_lvalue(state, MISC(def, 0));
7054         }
7055         return ret;
7056 }
7057
7058 static void clvalue(struct compile_state *state, struct triple *def)
7059 {
7060         if (!def) {
7061                 internal_error(state, def, "nothing where lvalue expected?");
7062         }
7063         if (!is_lvalue(state, def)) { 
7064                 error(state, def, "lvalue expected");
7065         }
7066 }
7067 static void lvalue(struct compile_state *state, struct triple *def)
7068 {
7069         clvalue(state, def);
7070         if (def->type->type & QUAL_CONST) {
7071                 error(state, def, "modifable lvalue expected");
7072         }
7073 }
7074
7075 static int is_pointer(struct triple *def)
7076 {
7077         return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7078 }
7079
7080 static void pointer(struct compile_state *state, struct triple *def)
7081 {
7082         if (!is_pointer(def)) {
7083                 error(state, def, "pointer expected");
7084         }
7085 }
7086
7087 static struct triple *int_const(
7088         struct compile_state *state, struct type *type, ulong_t value)
7089 {
7090         struct triple *result;
7091         switch(type->type & TYPE_MASK) {
7092         case TYPE_CHAR:
7093         case TYPE_INT:   case TYPE_UINT:
7094         case TYPE_LONG:  case TYPE_ULONG:
7095                 break;
7096         default:
7097                 internal_error(state, 0, "constant for unknown type");
7098         }
7099         result = triple(state, OP_INTCONST, type, 0, 0);
7100         result->u.cval = value;
7101         return result;
7102 }
7103
7104
7105 static struct triple *read_expr(struct compile_state *state, struct triple *def);
7106
7107 static struct triple *do_mk_addr_expr(struct compile_state *state, 
7108         struct triple *expr, struct type *type, ulong_t offset)
7109 {
7110         struct triple *result;
7111         struct type *ptr_type;
7112         clvalue(state, expr);
7113
7114         ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
7115
7116         
7117         result = 0;
7118         if (expr->op == OP_ADECL) {
7119                 error(state, expr, "address of auto variables not supported");
7120         }
7121         else if (expr->op == OP_SDECL) {
7122                 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
7123                 MISC(result, 0) = expr;
7124                 result->u.cval = offset;
7125         }
7126         else if (expr->op == OP_DEREF) {
7127                 result = triple(state, OP_ADD, ptr_type,
7128                         RHS(expr, 0),
7129                         int_const(state, &ulong_type, offset));
7130         }
7131         else if (expr->op == OP_BLOBCONST) {
7132                 FINISHME();
7133                 internal_error(state, expr, "not yet implemented");
7134         }
7135         else if (expr->op == OP_LIST) {
7136                 error(state, 0, "Function addresses not supported");
7137         }
7138         else if (triple_is_part(state, expr)) {
7139                 struct triple *part;
7140                 part = expr;
7141                 expr = MISC(expr, 0);
7142                 if (part->op == OP_DOT) {
7143                         offset += bits_to_bytes(
7144                                 field_offset(state, expr->type, part->u.field));
7145                 }
7146                 else if (part->op == OP_INDEX) {
7147                         offset += bits_to_bytes(
7148                                 index_offset(state, expr->type, part->u.cval));
7149                 }
7150                 else {
7151                         internal_error(state, part, "unhandled part type");
7152                 }
7153                 result = do_mk_addr_expr(state, expr, type, offset);
7154         }
7155         if (!result) {
7156                 internal_error(state, expr, "cannot take address of expression");
7157         }
7158         return result;
7159 }
7160
7161 static struct triple *mk_addr_expr(
7162         struct compile_state *state, struct triple *expr, ulong_t offset)
7163 {
7164         return do_mk_addr_expr(state, expr, expr->type, offset);
7165 }
7166
7167 static struct triple *mk_deref_expr(
7168         struct compile_state *state, struct triple *expr)
7169 {
7170         struct type *base_type;
7171         pointer(state, expr);
7172         base_type = expr->type->left;
7173         return triple(state, OP_DEREF, base_type, expr, 0);
7174 }
7175
7176 /* lvalue conversions always apply except when certain operators
7177  * are applied.  So I apply apply it when I know no more
7178  * operators will be applied.
7179  */
7180 static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
7181 {
7182         /* Tranform an array to a pointer to the first element */
7183         if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7184                 struct type *type;
7185                 type = new_type(
7186                         TYPE_POINTER | (def->type->type & QUAL_MASK),
7187                         def->type->left, 0);
7188                 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
7189                         struct triple *addrconst;
7190                         if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7191                                 internal_error(state, def, "bad array constant");
7192                         }
7193                         addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7194                         MISC(addrconst, 0) = def;
7195                         def = addrconst;
7196                 }
7197                 else {
7198                         def = triple(state, OP_CONVERT, type, def, 0);
7199                 }
7200         }
7201         /* Transform a function to a pointer to it */
7202         else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7203                 def = mk_addr_expr(state, def, 0);
7204         }
7205         return def;
7206 }
7207
7208 static struct triple *deref_field(
7209         struct compile_state *state, struct triple *expr, struct hash_entry *field)
7210 {
7211         struct triple *result;
7212         struct type *type, *member;
7213         ulong_t offset;
7214         if (!field) {
7215                 internal_error(state, 0, "No field passed to deref_field");
7216         }
7217         result = 0;
7218         type = expr->type;
7219         if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7220                 ((type->type & TYPE_MASK) != TYPE_UNION)) {
7221                 error(state, 0, "request for member %s in something not a struct or union",
7222                         field->name);
7223         }
7224         member = field_type(state, type, field);
7225         if ((type->type & STOR_MASK) == STOR_PERM) {
7226                 /* Do the pointer arithmetic to get a deref the field */
7227                 offset = bits_to_bytes(field_offset(state, type, field));
7228                 result = do_mk_addr_expr(state, expr, member, offset);
7229                 result = mk_deref_expr(state, result);
7230         }
7231         else {
7232                 /* Find the variable for the field I want. */
7233                 result = triple(state, OP_DOT, member, expr, 0);
7234                 result->u.field = field;
7235         }
7236         return result;
7237 }
7238
7239 static struct triple *deref_index(
7240         struct compile_state *state, struct triple *expr, size_t index)
7241 {
7242         struct triple *result;
7243         struct type *type, *member;
7244         ulong_t offset;
7245
7246         result = 0;
7247         type = expr->type;
7248         member = index_type(state, type, index);
7249
7250         if ((type->type & STOR_MASK) == STOR_PERM) {
7251                 offset = bits_to_bytes(index_offset(state, type, index));
7252                 result = do_mk_addr_expr(state, expr, member, offset);
7253                 result = mk_deref_expr(state, result);
7254         }
7255         else {
7256                 result = triple(state, OP_INDEX, member, expr, 0);
7257                 result->u.cval = index;
7258         }
7259         return result;
7260 }
7261
7262 static struct triple *read_expr(struct compile_state *state, struct triple *def)
7263 {
7264         int op;
7265         if  (!def) {
7266                 return 0;
7267         }
7268 #warning "CHECK_ME is this the only place I need to do lvalue conversions?"
7269         /* Transform lvalues into something we can read */
7270         def = lvalue_conversion(state, def);
7271         if (!is_lvalue(state, def)) {
7272                 return def;
7273         }
7274         if (is_in_reg(state, def)) {
7275                 op = OP_READ;
7276         } else {
7277                 if (def->op == OP_SDECL) {
7278                         def = mk_addr_expr(state, def, 0);
7279                         def = mk_deref_expr(state, def);
7280                 }
7281                 op = OP_LOAD;
7282         }
7283         def = triple(state, op, def->type, def, 0);
7284         if (def->type->type & QUAL_VOLATILE) {
7285                 def->id |= TRIPLE_FLAG_VOLATILE;
7286         }
7287         return def;
7288 }
7289
7290 int is_write_compatible(struct compile_state *state, 
7291         struct type *dest, struct type *rval)
7292 {
7293         int compatible = 0;
7294         /* Both operands have arithmetic type */
7295         if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7296                 compatible = 1;
7297         }
7298         /* One operand is a pointer and the other is a pointer to void */
7299         else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7300                 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7301                 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7302                         ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7303                 compatible = 1;
7304         }
7305         /* If both types are the same without qualifiers we are good */
7306         else if (equiv_ptrs(dest, rval)) {
7307                 compatible = 1;
7308         }
7309         /* test for struct/union equality  */
7310         else if (equiv_types(dest, rval)) {
7311                 compatible = 1;
7312         }
7313         return compatible;
7314 }
7315
7316 static void write_compatible(struct compile_state *state,
7317         struct type *dest, struct type *rval)
7318 {
7319         if (!is_write_compatible(state, dest, rval)) {
7320                 FILE *fp = state->errout;
7321                 fprintf(fp, "dest: ");
7322                 name_of(fp, dest);
7323                 fprintf(fp,"\nrval: ");
7324                 name_of(fp, rval);
7325                 fprintf(fp, "\n");
7326                 error(state, 0, "Incompatible types in assignment");
7327         }
7328 }
7329
7330 static int is_init_compatible(struct compile_state *state,
7331         struct type *dest, struct type *rval)
7332 {
7333         int compatible = 0;
7334         if (is_write_compatible(state, dest, rval)) {
7335                 compatible = 1;
7336         }
7337         else if (equiv_types(dest, rval)) {
7338                 compatible = 1;
7339         }
7340         return compatible;
7341 }
7342
7343 static struct triple *write_expr(
7344         struct compile_state *state, struct triple *dest, struct triple *rval)
7345 {
7346         struct triple *def;
7347         int op;
7348
7349         def = 0;
7350         if (!rval) {
7351                 internal_error(state, 0, "missing rval");
7352         }
7353
7354         if (rval->op == OP_LIST) {
7355                 internal_error(state, 0, "expression of type OP_LIST?");
7356         }
7357         if (!is_lvalue(state, dest)) {
7358                 internal_error(state, 0, "writing to a non lvalue?");
7359         }
7360         if (dest->type->type & QUAL_CONST) {
7361                 internal_error(state, 0, "modifable lvalue expexted");
7362         }
7363
7364         write_compatible(state, dest->type, rval->type);
7365         if (!equiv_types(dest->type, rval->type)) {
7366                 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7367         }
7368
7369         /* Now figure out which assignment operator to use */
7370         op = -1;
7371         if (is_in_reg(state, dest)) {
7372                 def = triple(state, OP_WRITE, dest->type, rval, dest);
7373                 if (MISC(def, 0) != dest) {
7374                         internal_error(state, def, "huh?");
7375                 }
7376                 if (RHS(def, 0) != rval) {
7377                         internal_error(state, def, "huh?");
7378                 }
7379         } else {
7380                 def = triple(state, OP_STORE, dest->type, dest, rval);
7381         }
7382         if (def->type->type & QUAL_VOLATILE) {
7383                 def->id |= TRIPLE_FLAG_VOLATILE;
7384         }
7385         return def;
7386 }
7387
7388 static struct triple *init_expr(
7389         struct compile_state *state, struct triple *dest, struct triple *rval)
7390 {
7391         struct triple *def;
7392
7393         def = 0;
7394         if (!rval) {
7395                 internal_error(state, 0, "missing rval");
7396         }
7397         if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7398                 rval = read_expr(state, rval);
7399                 def = write_expr(state, dest, rval);
7400         }
7401         else {
7402                 /* Fill in the array size if necessary */
7403                 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7404                         ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7405                         if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7406                                 dest->type->elements = rval->type->elements;
7407                         }
7408                 }
7409                 if (!equiv_types(dest->type, rval->type)) {
7410                         error(state, 0, "Incompatible types in inializer");
7411                 }
7412                 MISC(dest, 0) = rval;
7413                 insert_triple(state, dest, rval);
7414                 rval->id |= TRIPLE_FLAG_FLATTENED;
7415                 use_triple(MISC(dest, 0), dest);
7416         }
7417         return def;
7418 }
7419
7420 struct type *arithmetic_result(
7421         struct compile_state *state, struct triple *left, struct triple *right)
7422 {
7423         struct type *type;
7424         /* Sanity checks to ensure I am working with arithmetic types */
7425         arithmetic(state, left);
7426         arithmetic(state, right);
7427         type = new_type(
7428                 do_arithmetic_conversion(
7429                         get_basic_type(left->type),
7430                         get_basic_type(right->type)),
7431                 0, 0);
7432         return type;
7433 }
7434
7435 struct type *ptr_arithmetic_result(
7436         struct compile_state *state, struct triple *left, struct triple *right)
7437 {
7438         struct type *type;
7439         /* Sanity checks to ensure I am working with the proper types */
7440         ptr_arithmetic(state, left);
7441         arithmetic(state, right);
7442         if (TYPE_ARITHMETIC(left->type->type) && 
7443                 TYPE_ARITHMETIC(right->type->type)) {
7444                 type = arithmetic_result(state, left, right);
7445         }
7446         else if (TYPE_PTR(left->type->type)) {
7447                 type = left->type;
7448         }
7449         else {
7450                 internal_error(state, 0, "huh?");
7451                 type = 0;
7452         }
7453         return type;
7454 }
7455
7456 /* boolean helper function */
7457
7458 static struct triple *ltrue_expr(struct compile_state *state, 
7459         struct triple *expr)
7460 {
7461         switch(expr->op) {
7462         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
7463         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
7464         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7465                 /* If the expression is already boolean do nothing */
7466                 break;
7467         default:
7468                 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7469                 break;
7470         }
7471         return expr;
7472 }
7473
7474 static struct triple *lfalse_expr(struct compile_state *state, 
7475         struct triple *expr)
7476 {
7477         return triple(state, OP_LFALSE, &int_type, expr, 0);
7478 }
7479
7480 static struct triple *mkland_expr(
7481         struct compile_state *state,
7482         struct triple *left, struct triple *right)
7483 {
7484         struct triple *def, *val, *var, *jmp, *mid, *end;
7485         struct triple *lstore, *rstore;
7486
7487         /* Generate some intermediate triples */
7488         end = label(state);
7489         var = variable(state, &int_type);
7490         
7491         /* Store the left hand side value */
7492         lstore = write_expr(state, var, left);
7493
7494         /* Jump if the value is false */
7495         jmp =  branch(state, end, 
7496                 lfalse_expr(state, read_expr(state, var)));
7497         mid = label(state);
7498         
7499         /* Store the right hand side value */
7500         rstore = write_expr(state, var, right);
7501
7502         /* An expression for the computed value */
7503         val = read_expr(state, var);
7504
7505         /* Generate the prog for a logical and */
7506         def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0);
7507         
7508         return def;
7509 }
7510
7511 static struct triple *mklor_expr(
7512         struct compile_state *state,
7513         struct triple *left, struct triple *right)
7514 {
7515         struct triple *def, *val, *var, *jmp, *mid, *end;
7516
7517         /* Generate some intermediate triples */
7518         end = label(state);
7519         var = variable(state, &int_type);
7520         
7521         /* Store the left hand side value */
7522         left = write_expr(state, var, left);
7523         
7524         /* Jump if the value is true */
7525         jmp = branch(state, end, read_expr(state, var));
7526         mid = label(state);
7527         
7528         /* Store the right hand side value */
7529         right = write_expr(state, var, right);
7530                 
7531         /* An expression for the computed value*/
7532         val = read_expr(state, var);
7533
7534         /* Generate the prog for a logical or */
7535         def = mkprog(state, var, left, jmp, mid, right, end, val, 0);
7536
7537         return def;
7538 }
7539
7540 static struct triple *mkcond_expr(
7541         struct compile_state *state, 
7542         struct triple *test, struct triple *left, struct triple *right)
7543 {
7544         struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
7545         struct type *result_type;
7546         unsigned int left_type, right_type;
7547         bool(state, test);
7548         left_type = left->type->type;
7549         right_type = right->type->type;
7550         result_type = 0;
7551         /* Both operands have arithmetic type */
7552         if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7553                 result_type = arithmetic_result(state, left, right);
7554         }
7555         /* Both operands have void type */
7556         else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7557                 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7558                 result_type = &void_type;
7559         }
7560         /* pointers to the same type... */
7561         else if ((result_type = compatible_ptrs(left->type, right->type))) {
7562                 ;
7563         }
7564         /* Both operands are pointers and left is a pointer to void */
7565         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7566                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7567                 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7568                 result_type = right->type;
7569         }
7570         /* Both operands are pointers and right is a pointer to void */
7571         else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7572                 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7573                 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7574                 result_type = left->type;
7575         }
7576         if (!result_type) {
7577                 error(state, 0, "Incompatible types in conditional expression");
7578         }
7579         /* Generate some intermediate triples */
7580         mid = label(state);
7581         end = label(state);
7582         var = variable(state, result_type);
7583
7584         /* Branch if the test is false */
7585         jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7586         top = label(state);
7587
7588         /* Store the left hand side value */
7589         left = write_expr(state, var, left);
7590
7591         /* Branch to the end */
7592         jmp2 = branch(state, end, 0);
7593
7594         /* Store the right hand side value */
7595         right = write_expr(state, var, right);
7596         
7597         /* An expression for the computed value */
7598         val = read_expr(state, var);
7599
7600         /* Generate the prog for a conditional expression */
7601         def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0);
7602
7603         return def;
7604 }
7605
7606
7607 static int expr_depth(struct compile_state *state, struct triple *ins)
7608 {
7609 #warning "FIXME move optimal ordering of subexpressions into the optimizer"
7610         int count;
7611         count = 0;
7612         if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7613                 count = 0;
7614         }
7615         else if (ins->op == OP_DEREF) {
7616                 count = expr_depth(state, RHS(ins, 0)) - 1;
7617         }
7618         else if (ins->op == OP_VAL) {
7619                 count = expr_depth(state, RHS(ins, 0)) - 1;
7620         }
7621         else if (ins->op == OP_FCALL) {
7622                 /* Don't figure the depth of a call just guess it is huge */
7623                 count = 1000;
7624         }
7625         else {
7626                 struct triple **expr;
7627                 expr = triple_rhs(state, ins, 0);
7628                 for(;expr; expr = triple_rhs(state, ins, expr)) {
7629                         if (*expr) {
7630                                 int depth;
7631                                 depth = expr_depth(state, *expr);
7632                                 if (depth > count) {
7633                                         count = depth;
7634                                 }
7635                         }
7636                 }
7637         }
7638         return count + 1;
7639 }
7640
7641 static struct triple *flatten_generic(
7642         struct compile_state *state, struct triple *first, struct triple *ptr,
7643         int ignored)
7644 {
7645         struct rhs_vector {
7646                 int depth;
7647                 struct triple **ins;
7648         } vector[MAX_RHS];
7649         int i, rhs, lhs;
7650         /* Only operations with just a rhs and a lhs should come here */
7651         rhs = ptr->rhs;
7652         lhs = ptr->lhs;
7653         if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
7654                 internal_error(state, ptr, "unexpected args for: %d %s",
7655                         ptr->op, tops(ptr->op));
7656         }
7657         /* Find the depth of the rhs elements */
7658         for(i = 0; i < rhs; i++) {
7659                 vector[i].ins = &RHS(ptr, i);
7660                 vector[i].depth = expr_depth(state, *vector[i].ins);
7661         }
7662         /* Selection sort the rhs */
7663         for(i = 0; i < rhs; i++) {
7664                 int j, max = i;
7665                 for(j = i + 1; j < rhs; j++ ) {
7666                         if (vector[j].depth > vector[max].depth) {
7667                                 max = j;
7668                         }
7669                 }
7670                 if (max != i) {
7671                         struct rhs_vector tmp;
7672                         tmp = vector[i];
7673                         vector[i] = vector[max];
7674                         vector[max] = tmp;
7675                 }
7676         }
7677         /* Now flatten the rhs elements */
7678         for(i = 0; i < rhs; i++) {
7679                 *vector[i].ins = flatten(state, first, *vector[i].ins);
7680                 use_triple(*vector[i].ins, ptr);
7681         }
7682         if (lhs) {
7683                 insert_triple(state, first, ptr);
7684                 ptr->id |= TRIPLE_FLAG_FLATTENED;
7685                 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7686                 
7687                 /* Now flatten the lhs elements */
7688                 for(i = 0; i < lhs; i++) {
7689                         struct triple **ins = &LHS(ptr, i);
7690                         *ins = flatten(state, first, *ins);
7691                         use_triple(*ins, ptr);
7692                 }
7693         }
7694         return ptr;
7695 }
7696
7697 static struct triple *flatten_prog(
7698         struct compile_state *state, struct triple *first, struct triple *ptr)
7699 {
7700         struct triple *head, *body, *val;
7701         head = RHS(ptr, 0);
7702         RHS(ptr, 0) = 0;
7703         val  = head->prev;
7704         body = head->next;
7705         release_triple(state, head);
7706         release_triple(state, ptr);
7707         val->next        = first;
7708         body->prev       = first->prev;
7709         body->prev->next = body;
7710         val->next->prev  = val;
7711
7712         if (triple_is_cbranch(state, body->prev) ||
7713                 triple_is_call(state, body->prev)) {
7714                 unuse_triple(first, body->prev);
7715                 use_triple(body, body->prev);
7716         }
7717         
7718         if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7719                 internal_error(state, val, "val not flattened?");
7720         }
7721
7722         return val;
7723 }
7724
7725
7726 static struct triple *flatten_part(
7727         struct compile_state *state, struct triple *first, struct triple *ptr)
7728 {
7729         if (!triple_is_part(state, ptr)) {
7730                 internal_error(state, ptr,  "not a part");
7731         }
7732         if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7733                 internal_error(state, ptr, "unexpected args for: %d %s",
7734                         ptr->op, tops(ptr->op));
7735         }
7736         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7737         use_triple(MISC(ptr, 0), ptr);
7738         return flatten_generic(state, first, ptr, 1);
7739 }
7740
7741 static struct triple *flatten(
7742         struct compile_state *state, struct triple *first, struct triple *ptr)
7743 {
7744         struct triple *orig_ptr;
7745         if (!ptr)
7746                 return 0;
7747         do {
7748                 orig_ptr = ptr;
7749                 /* Only flatten triples once */
7750                 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7751                         return ptr;
7752                 }
7753                 switch(ptr->op) {
7754                 case OP_VAL:
7755                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7756                         return MISC(ptr, 0);
7757                         break;
7758                 case OP_PROG:
7759                         ptr = flatten_prog(state, first, ptr);
7760                         break;
7761                 case OP_FCALL:
7762                         ptr = flatten_generic(state, first, ptr, 1);
7763                         insert_triple(state, first, ptr);
7764                         ptr->id |= TRIPLE_FLAG_FLATTENED;
7765                         ptr->id &= ~TRIPLE_FLAG_LOCAL;
7766                         if (ptr->next != ptr) {
7767                                 use_triple(ptr->next, ptr);
7768                         }
7769                         break;
7770                 case OP_READ:
7771                 case OP_LOAD:
7772                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7773                         use_triple(RHS(ptr, 0), ptr);
7774                         break;
7775                 case OP_WRITE:
7776                         ptr = flatten_generic(state, first, ptr, 1);
7777                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7778                         use_triple(MISC(ptr, 0), ptr);
7779                         break;
7780                 case OP_BRANCH:
7781                         use_triple(TARG(ptr, 0), ptr);
7782                         break;
7783                 case OP_CBRANCH:
7784                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7785                         use_triple(RHS(ptr, 0), ptr);
7786                         use_triple(TARG(ptr, 0), ptr);
7787                         insert_triple(state, first, ptr);
7788                         ptr->id |= TRIPLE_FLAG_FLATTENED;
7789                         ptr->id &= ~TRIPLE_FLAG_LOCAL;
7790                         if (ptr->next != ptr) {
7791                                 use_triple(ptr->next, ptr);
7792                         }
7793                         break;
7794                 case OP_CALL:
7795                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7796                         use_triple(MISC(ptr, 0), ptr);
7797                         use_triple(TARG(ptr, 0), ptr);
7798                         insert_triple(state, first, ptr);
7799                         ptr->id |= TRIPLE_FLAG_FLATTENED;
7800                         ptr->id &= ~TRIPLE_FLAG_LOCAL;
7801                         if (ptr->next != ptr) {
7802                                 use_triple(ptr->next, ptr);
7803                         }
7804                         break;
7805                 case OP_RET:
7806                         RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7807                         use_triple(RHS(ptr, 0), ptr);
7808                         break;
7809                 case OP_BLOBCONST:
7810                         insert_triple(state, state->global_pool, ptr);
7811                         ptr->id |= TRIPLE_FLAG_FLATTENED;
7812                         ptr->id &= ~TRIPLE_FLAG_LOCAL;
7813                         ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
7814                         use_triple(MISC(ptr, 0), ptr);
7815                         break;
7816                 case OP_DEREF:
7817                         /* Since OP_DEREF is just a marker delete it when I flatten it */
7818                         ptr = RHS(ptr, 0);
7819                         RHS(orig_ptr, 0) = 0;
7820                         free_triple(state, orig_ptr);
7821                         break;
7822                 case OP_DOT:
7823                         if (RHS(ptr, 0)->op == OP_DEREF) {
7824                                 struct triple *base, *left;
7825                                 ulong_t offset;
7826                                 base = MISC(ptr, 0);
7827                                 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
7828                                 left = RHS(base, 0);
7829                                 ptr = triple(state, OP_ADD, left->type, 
7830                                         read_expr(state, left),
7831                                         int_const(state, &ulong_type, offset));
7832                                 free_triple(state, base);
7833                         }
7834                         else {
7835                                 ptr = flatten_part(state, first, ptr);
7836                         }
7837                         break;
7838                 case OP_INDEX:
7839                         if (RHS(ptr, 0)->op == OP_DEREF) {
7840                                 struct triple *base, *left;
7841                                 ulong_t offset;
7842                                 base = MISC(ptr, 0);
7843                                 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
7844                                 left = RHS(base, 0);
7845                                 ptr = triple(state, OP_ADD, left->type,
7846                                         read_expr(state, left),
7847                                         int_const(state, &long_type, offset));
7848                                 free_triple(state, base);
7849                         }
7850                         else {
7851                                 ptr = flatten_part(state, first, ptr);
7852                         }
7853                         break;
7854                 case OP_PIECE:
7855                         ptr = flatten_part(state, first, ptr);
7856                         use_triple(ptr, MISC(ptr, 0));
7857                         break;
7858                 case OP_ADDRCONST:
7859                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7860                         use_triple(MISC(ptr, 0), ptr);
7861                         break;
7862                 case OP_SDECL:
7863                         first = state->global_pool;
7864                         MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7865                         use_triple(MISC(ptr, 0), ptr);
7866                         insert_triple(state, first, ptr);
7867                         ptr->id |= TRIPLE_FLAG_FLATTENED;
7868                         ptr->id &= ~TRIPLE_FLAG_LOCAL;
7869                         return ptr;
7870                 case OP_ADECL:
7871                         ptr = flatten_generic(state, first, ptr, 0);
7872                         break;
7873                 default:
7874                         /* Flatten the easy cases we don't override */
7875                         ptr = flatten_generic(state, first, ptr, 0);
7876                         break;
7877                 }
7878         } while(ptr && (ptr != orig_ptr));
7879         if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
7880                 insert_triple(state, first, ptr);
7881                 ptr->id |= TRIPLE_FLAG_FLATTENED;
7882                 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7883         }
7884         return ptr;
7885 }
7886
7887 static void release_expr(struct compile_state *state, struct triple *expr)
7888 {
7889         struct triple *head;
7890         head = label(state);
7891         flatten(state, head, expr);
7892         while(head->next != head) {
7893                 release_triple(state, head->next);
7894         }
7895         free_triple(state, head);
7896 }
7897
7898 static int replace_rhs_use(struct compile_state *state,
7899         struct triple *orig, struct triple *new, struct triple *use)
7900 {
7901         struct triple **expr;
7902         int found;
7903         found = 0;
7904         expr = triple_rhs(state, use, 0);
7905         for(;expr; expr = triple_rhs(state, use, expr)) {
7906                 if (*expr == orig) {
7907                         *expr = new;
7908                         found = 1;
7909                 }
7910         }
7911         if (found) {
7912                 unuse_triple(orig, use);
7913                 use_triple(new, use);
7914         }
7915         return found;
7916 }
7917
7918 static int replace_lhs_use(struct compile_state *state,
7919         struct triple *orig, struct triple *new, struct triple *use)
7920 {
7921         struct triple **expr;
7922         int found;
7923         found = 0;
7924         expr = triple_lhs(state, use, 0);
7925         for(;expr; expr = triple_lhs(state, use, expr)) {
7926                 if (*expr == orig) {
7927                         *expr = new;
7928                         found = 1;
7929                 }
7930         }
7931         if (found) {
7932                 unuse_triple(orig, use);
7933                 use_triple(new, use);
7934         }
7935         return found;
7936 }
7937
7938 static int replace_misc_use(struct compile_state *state,
7939         struct triple *orig, struct triple *new, struct triple *use)
7940 {
7941         struct triple **expr;
7942         int found;
7943         found = 0;
7944         expr = triple_misc(state, use, 0);
7945         for(;expr; expr = triple_misc(state, use, expr)) {
7946                 if (*expr == orig) {
7947                         *expr = new;
7948                         found = 1;
7949                 }
7950         }
7951         if (found) {
7952                 unuse_triple(orig, use);
7953                 use_triple(new, use);
7954         }
7955         return found;
7956 }
7957
7958 static int replace_targ_use(struct compile_state *state,
7959         struct triple *orig, struct triple *new, struct triple *use)
7960 {
7961         struct triple **expr;
7962         int found;
7963         found = 0;
7964         expr = triple_targ(state, use, 0);
7965         for(;expr; expr = triple_targ(state, use, expr)) {
7966                 if (*expr == orig) {
7967                         *expr = new;
7968                         found = 1;
7969                 }
7970         }
7971         if (found) {
7972                 unuse_triple(orig, use);
7973                 use_triple(new, use);
7974         }
7975         return found;
7976 }
7977
7978 static void replace_use(struct compile_state *state,
7979         struct triple *orig, struct triple *new, struct triple *use)
7980 {
7981         int found;
7982         found = 0;
7983         found |= replace_rhs_use(state, orig, new, use);
7984         found |= replace_lhs_use(state, orig, new, use);
7985         found |= replace_misc_use(state, orig, new, use);
7986         found |= replace_targ_use(state, orig, new, use);
7987         if (!found) {
7988                 internal_error(state, use, "use without use");
7989         }
7990 }
7991
7992 static void propogate_use(struct compile_state *state,
7993         struct triple *orig, struct triple *new)
7994 {
7995         struct triple_set *user, *next;
7996         for(user = orig->use; user; user = next) {
7997                 /* Careful replace_use modifies the use chain and
7998                  * removes use.  So we must get a copy of the next
7999                  * entry early.
8000                  */
8001                 next = user->next;
8002                 replace_use(state, orig, new, user->member);
8003         }
8004         if (orig->use) {
8005                 internal_error(state, orig, "used after propogate_use");
8006         }
8007 }
8008
8009 /*
8010  * Code generators
8011  * ===========================
8012  */
8013
8014 static struct triple *mk_cast_expr(
8015         struct compile_state *state, struct type *type, struct triple *expr)
8016 {
8017         struct triple *def;
8018         def = read_expr(state, expr);
8019         def = triple(state, OP_CONVERT, type, def, 0);
8020         return def;
8021 }
8022
8023 static struct triple *mk_add_expr(
8024         struct compile_state *state, struct triple *left, struct triple *right)
8025 {
8026         struct type *result_type;
8027         /* Put pointer operands on the left */
8028         if (is_pointer(right)) {
8029                 struct triple *tmp;
8030                 tmp = left;
8031                 left = right;
8032                 right = tmp;
8033         }
8034         left  = read_expr(state, left);
8035         right = read_expr(state, right);
8036         result_type = ptr_arithmetic_result(state, left, right);
8037         if (is_pointer(left)) {
8038                 struct type *ptr_math;
8039                 int op;
8040                 if (is_signed(right->type)) {
8041                         ptr_math = &long_type;
8042                         op = OP_SMUL;
8043                 } else {
8044                         ptr_math = &ulong_type;
8045                         op = OP_UMUL;
8046                 }
8047                 if (!equiv_types(right->type, ptr_math)) {
8048                         right = mk_cast_expr(state, ptr_math, right);
8049                 }
8050                 right = triple(state, op, ptr_math, right, 
8051                         int_const(state, ptr_math, 
8052                                 size_of_in_bytes(state, left->type->left)));
8053         }
8054         return triple(state, OP_ADD, result_type, left, right);
8055 }
8056
8057 static struct triple *mk_sub_expr(
8058         struct compile_state *state, struct triple *left, struct triple *right)
8059 {
8060         struct type *result_type;
8061         result_type = ptr_arithmetic_result(state, left, right);
8062         left  = read_expr(state, left);
8063         right = read_expr(state, right);
8064         if (is_pointer(left)) {
8065                 struct type *ptr_math;
8066                 int op;
8067                 if (is_signed(right->type)) {
8068                         ptr_math = &long_type;
8069                         op = OP_SMUL;
8070                 } else {
8071                         ptr_math = &ulong_type;
8072                         op = OP_UMUL;
8073                 }
8074                 if (!equiv_types(right->type, ptr_math)) {
8075                         right = mk_cast_expr(state, ptr_math, right);
8076                 }
8077                 right = triple(state, op, ptr_math, right, 
8078                         int_const(state, ptr_math, 
8079                                 size_of_in_bytes(state, left->type->left)));
8080         }
8081         return triple(state, OP_SUB, result_type, left, right);
8082 }
8083
8084 static struct triple *mk_pre_inc_expr(
8085         struct compile_state *state, struct triple *def)
8086 {
8087         struct triple *val;
8088         lvalue(state, def);
8089         val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8090         return triple(state, OP_VAL, def->type,
8091                 write_expr(state, def, val),
8092                 val);
8093 }
8094
8095 static struct triple *mk_pre_dec_expr(
8096         struct compile_state *state, struct triple *def)
8097 {
8098         struct triple *val;
8099         lvalue(state, def);
8100         val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8101         return triple(state, OP_VAL, def->type,
8102                 write_expr(state, def, val),
8103                 val);
8104 }
8105
8106 static struct triple *mk_post_inc_expr(
8107         struct compile_state *state, struct triple *def)
8108 {
8109         struct triple *val;
8110         lvalue(state, def);
8111         val = read_expr(state, def);
8112         return triple(state, OP_VAL, def->type,
8113                 write_expr(state, def,
8114                         mk_add_expr(state, val, int_const(state, &int_type, 1)))
8115                 , val);
8116 }
8117
8118 static struct triple *mk_post_dec_expr(
8119         struct compile_state *state, struct triple *def)
8120 {
8121         struct triple *val;
8122         lvalue(state, def);
8123         val = read_expr(state, def);
8124         return triple(state, OP_VAL, def->type, 
8125                 write_expr(state, def,
8126                         mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8127                 , val);
8128 }
8129
8130 static struct triple *mk_subscript_expr(
8131         struct compile_state *state, struct triple *left, struct triple *right)
8132 {
8133         left  = read_expr(state, left);
8134         right = read_expr(state, right);
8135         if (!is_pointer(left) && !is_pointer(right)) {
8136                 error(state, left, "subscripted value is not a pointer");
8137         }
8138         return mk_deref_expr(state, mk_add_expr(state, left, right));
8139 }
8140
8141
8142 /*
8143  * Compile time evaluation
8144  * ===========================
8145  */
8146 static int is_const(struct triple *ins)
8147 {
8148         return IS_CONST_OP(ins->op);
8149 }
8150
8151 static int is_simple_const(struct triple *ins)
8152 {
8153         /* Is this a constant that u.cval has the value.
8154          * Or equivalently is this a constant that read_const
8155          * works on.
8156          * So far only OP_INTCONST qualifies.  
8157          */
8158         return (ins->op == OP_INTCONST);
8159 }
8160
8161 static int constants_equal(struct compile_state *state, 
8162         struct triple *left, struct triple *right)
8163 {
8164         int equal;
8165         if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8166                 equal = 0;
8167         }
8168         else if (!is_const(left) || !is_const(right)) {
8169                 equal = 0;
8170         }
8171         else if (left->op != right->op) {
8172                 equal = 0;
8173         }
8174         else if (!equiv_types(left->type, right->type)) {
8175                 equal = 0;
8176         }
8177         else {
8178                 equal = 0;
8179                 switch(left->op) {
8180                 case OP_INTCONST:
8181                         if (left->u.cval == right->u.cval) {
8182                                 equal = 1;
8183                         }
8184                         break;
8185                 case OP_BLOBCONST:
8186                 {
8187                         size_t lsize, rsize, bytes;
8188                         lsize = size_of(state, left->type);
8189                         rsize = size_of(state, right->type);
8190                         if (lsize != rsize) {
8191                                 break;
8192                         }
8193                         bytes = bits_to_bytes(lsize);
8194                         if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
8195                                 equal = 1;
8196                         }
8197                         break;
8198                 }
8199                 case OP_ADDRCONST:
8200                         if ((MISC(left, 0) == MISC(right, 0)) &&
8201                                 (left->u.cval == right->u.cval)) {
8202                                 equal = 1;
8203                         }
8204                         break;
8205                 default:
8206                         internal_error(state, left, "uknown constant type");
8207                         break;
8208                 }
8209         }
8210         return equal;
8211 }
8212
8213 static int is_zero(struct triple *ins)
8214 {
8215         return is_simple_const(ins) && (ins->u.cval == 0);
8216 }
8217
8218 static int is_one(struct triple *ins)
8219 {
8220         return is_simple_const(ins) && (ins->u.cval == 1);
8221 }
8222
8223 static long_t bit_count(ulong_t value)
8224 {
8225         int count;
8226         int i;
8227         count = 0;
8228         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8229                 ulong_t mask;
8230                 mask = 1;
8231                 mask <<= i;
8232                 if (value & mask) {
8233                         count++;
8234                 }
8235         }
8236         return count;
8237         
8238 }
8239 static long_t bsr(ulong_t value)
8240 {
8241         int i;
8242         for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8243                 ulong_t mask;
8244                 mask = 1;
8245                 mask <<= i;
8246                 if (value & mask) {
8247                         return i;
8248                 }
8249         }
8250         return -1;
8251 }
8252
8253 static long_t bsf(ulong_t value)
8254 {
8255         int i;
8256         for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8257                 ulong_t mask;
8258                 mask = 1;
8259                 mask <<= 1;
8260                 if (value & mask) {
8261                         return i;
8262                 }
8263         }
8264         return -1;
8265 }
8266
8267 static long_t log2(ulong_t value)
8268 {
8269         return bsr(value);
8270 }
8271
8272 static long_t tlog2(struct triple *ins)
8273 {
8274         return log2(ins->u.cval);
8275 }
8276
8277 static int is_pow2(struct triple *ins)
8278 {
8279         ulong_t value, mask;
8280         long_t log;
8281         if (!is_const(ins)) {
8282                 return 0;
8283         }
8284         value = ins->u.cval;
8285         log = log2(value);
8286         if (log == -1) {
8287                 return 0;
8288         }
8289         mask = 1;
8290         mask <<= log;
8291         return  ((value & mask) == value);
8292 }
8293
8294 static ulong_t read_const(struct compile_state *state,
8295         struct triple *ins, struct triple *rhs)
8296 {
8297         switch(rhs->type->type &TYPE_MASK) {
8298         case TYPE_CHAR:   
8299         case TYPE_SHORT:
8300         case TYPE_INT:
8301         case TYPE_LONG:
8302         case TYPE_UCHAR:   
8303         case TYPE_USHORT:  
8304         case TYPE_UINT:
8305         case TYPE_ULONG:
8306         case TYPE_POINTER:
8307         case TYPE_BITFIELD:
8308                 break;
8309         default:
8310                 fprintf(state->errout, "type: ");
8311                 name_of(state->errout, rhs->type);
8312                 fprintf(state->errout, "\n");
8313                 internal_warning(state, rhs, "bad type to read_const");
8314                 break;
8315         }
8316         if (!is_simple_const(rhs)) {
8317                 internal_error(state, rhs, "bad op to read_const");
8318         }
8319         return rhs->u.cval;
8320 }
8321
8322 static long_t read_sconst(struct compile_state *state,
8323         struct triple *ins, struct triple *rhs)
8324 {
8325         return (long_t)(rhs->u.cval);
8326 }
8327
8328 int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8329 {
8330         if (!is_const(rhs)) {
8331                 internal_error(state, 0, "non const passed to const_true");
8332         }
8333         return !is_zero(rhs);
8334 }
8335
8336 int const_eq(struct compile_state *state, struct triple *ins,
8337         struct triple *left, struct triple *right)
8338 {
8339         int result;
8340         if (!is_const(left) || !is_const(right)) {
8341                 internal_warning(state, ins, "non const passed to const_eq");
8342                 result = -1;
8343         }
8344         else if (left == right) {
8345                 result = 1;
8346         }
8347         else if (is_simple_const(left) && is_simple_const(right)) {
8348                 ulong_t lval, rval;
8349                 lval = read_const(state, ins, left);
8350                 rval = read_const(state, ins, right);
8351                 result = (lval == rval);
8352         }
8353         else if ((left->op == OP_ADDRCONST) && 
8354                 (right->op == OP_ADDRCONST)) {
8355                 result = (MISC(left, 0) == MISC(right, 0)) &&
8356                         (left->u.cval == right->u.cval);
8357         }
8358         else {
8359                 internal_warning(state, ins, "incomparable constants passed to const_eq");
8360                 result = -1;
8361         }
8362         return result;
8363         
8364 }
8365
8366 int const_ucmp(struct compile_state *state, struct triple *ins,
8367         struct triple *left, struct triple *right)
8368 {
8369         int result;
8370         if (!is_const(left) || !is_const(right)) {
8371                 internal_warning(state, ins, "non const past to const_ucmp");
8372                 result = -2;
8373         }
8374         else if (left == right) {
8375                 result = 0;
8376         }
8377         else if (is_simple_const(left) && is_simple_const(right)) {
8378                 ulong_t lval, rval;
8379                 lval = read_const(state, ins, left);
8380                 rval = read_const(state, ins, right);
8381                 result = 0;
8382                 if (lval > rval) {
8383                         result = 1;
8384                 } else if (rval > lval) {
8385                         result = -1;
8386                 }
8387         }
8388         else if ((left->op == OP_ADDRCONST) && 
8389                 (right->op == OP_ADDRCONST) &&
8390                 (MISC(left, 0) == MISC(right, 0))) {
8391                 result = 0;
8392                 if (left->u.cval > right->u.cval) {
8393                         result = 1;
8394                 } else if (left->u.cval < right->u.cval) {
8395                         result = -1;
8396                 }
8397         }
8398         else {
8399                 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
8400                 result = -2;
8401         }
8402         return result;
8403 }
8404
8405 int const_scmp(struct compile_state *state, struct triple *ins,
8406         struct triple *left, struct triple *right)
8407 {
8408         int result;
8409         if (!is_const(left) || !is_const(right)) {
8410                 internal_warning(state, ins, "non const past to ucmp_const");
8411                 result = -2;
8412         }
8413         else if (left == right) {
8414                 result = 0;
8415         }
8416         else if (is_simple_const(left) && is_simple_const(right)) {
8417                 long_t lval, rval;
8418                 lval = read_sconst(state, ins, left);
8419                 rval = read_sconst(state, ins, right);
8420                 result = 0;
8421                 if (lval > rval) {
8422                         result = 1;
8423                 } else if (rval > lval) {
8424                         result = -1;
8425                 }
8426         }
8427         else {
8428                 internal_warning(state, ins, "incomparable constants passed to const_scmp");
8429                 result = -2;
8430         }
8431         return result;
8432 }
8433
8434 static void unuse_rhs(struct compile_state *state, struct triple *ins)
8435 {
8436         struct triple **expr;
8437         expr = triple_rhs(state, ins, 0);
8438         for(;expr;expr = triple_rhs(state, ins, expr)) {
8439                 if (*expr) {
8440                         unuse_triple(*expr, ins);
8441                         *expr = 0;
8442                 }
8443         }
8444 }
8445
8446 static void unuse_lhs(struct compile_state *state, struct triple *ins)
8447 {
8448         struct triple **expr;
8449         expr = triple_lhs(state, ins, 0);
8450         for(;expr;expr = triple_lhs(state, ins, expr)) {
8451                 unuse_triple(*expr, ins);
8452                 *expr = 0;
8453         }
8454 }
8455
8456 static void unuse_misc(struct compile_state *state, struct triple *ins)
8457 {
8458         struct triple **expr;
8459         expr = triple_misc(state, ins, 0);
8460         for(;expr;expr = triple_misc(state, ins, expr)) {
8461                 unuse_triple(*expr, ins);
8462                 *expr = 0;
8463         }
8464 }
8465
8466 static void unuse_targ(struct compile_state *state, struct triple *ins)
8467 {
8468         int i;
8469         struct triple **slot;
8470         slot = &TARG(ins, 0);
8471         for(i = 0; i < ins->targ; i++) {
8472                 unuse_triple(slot[i], ins);
8473                 slot[i] = 0;
8474         }
8475 }
8476
8477 static void check_lhs(struct compile_state *state, struct triple *ins)
8478 {
8479         struct triple **expr;
8480         expr = triple_lhs(state, ins, 0);
8481         for(;expr;expr = triple_lhs(state, ins, expr)) {
8482                 internal_error(state, ins, "unexpected lhs");
8483         }
8484         
8485 }
8486
8487 static void check_misc(struct compile_state *state, struct triple *ins)
8488 {
8489         struct triple **expr;
8490         expr = triple_misc(state, ins, 0);
8491         for(;expr;expr = triple_misc(state, ins, expr)) {
8492                 if (*expr) {
8493                         internal_error(state, ins, "unexpected misc");
8494                 }
8495         }
8496 }
8497
8498 static void check_targ(struct compile_state *state, struct triple *ins)
8499 {
8500         struct triple **expr;
8501         expr = triple_targ(state, ins, 0);
8502         for(;expr;expr = triple_targ(state, ins, expr)) {
8503                 internal_error(state, ins, "unexpected targ");
8504         }
8505 }
8506
8507 static void wipe_ins(struct compile_state *state, struct triple *ins)
8508 {
8509         /* Becareful which instructions you replace the wiped
8510          * instruction with, as there are not enough slots
8511          * in all instructions to hold all others.
8512          */
8513         check_targ(state, ins);
8514         check_misc(state, ins);
8515         unuse_rhs(state, ins);
8516         unuse_lhs(state, ins);
8517         ins->lhs  = 0;
8518         ins->rhs  = 0;
8519         ins->misc = 0;
8520         ins->targ = 0;
8521 }
8522
8523 static void wipe_branch(struct compile_state *state, struct triple *ins)
8524 {
8525         /* Becareful which instructions you replace the wiped
8526          * instruction with, as there are not enough slots
8527          * in all instructions to hold all others.
8528          */
8529         unuse_rhs(state, ins);
8530         unuse_lhs(state, ins);
8531         unuse_misc(state, ins);
8532         unuse_targ(state, ins);
8533         ins->lhs  = 0;
8534         ins->rhs  = 0;
8535         ins->misc = 0;
8536         ins->targ = 0;
8537 }
8538
8539 static void mkcopy(struct compile_state *state, 
8540         struct triple *ins, struct triple *rhs)
8541 {
8542         struct block *block;
8543         if (!equiv_types(ins->type, rhs->type)) {
8544                 FILE *fp = state->errout;
8545                 fprintf(fp, "src type: ");
8546                 name_of(fp, rhs->type);
8547                 fprintf(fp, "\ndst type: ");
8548                 name_of(fp, ins->type);
8549                 fprintf(fp, "\n");
8550                 internal_error(state, ins, "mkcopy type mismatch");
8551         }
8552         block = block_of_triple(state, ins);
8553         wipe_ins(state, ins);
8554         ins->op = OP_COPY;
8555         ins->rhs  = 1;
8556         ins->u.block = block;
8557         RHS(ins, 0) = rhs;
8558         use_triple(RHS(ins, 0), ins);
8559 }
8560
8561 static void mkconst(struct compile_state *state, 
8562         struct triple *ins, ulong_t value)
8563 {
8564         if (!is_integral(ins) && !is_pointer(ins)) {
8565                 fprintf(state->errout, "type: ");
8566                 name_of(state->errout, ins->type);
8567                 fprintf(state->errout, "\n");
8568                 internal_error(state, ins, "unknown type to make constant value: %ld",
8569                         value);
8570         }
8571         wipe_ins(state, ins);
8572         ins->op = OP_INTCONST;
8573         ins->u.cval = value;
8574 }
8575
8576 static void mkaddr_const(struct compile_state *state,
8577         struct triple *ins, struct triple *sdecl, ulong_t value)
8578 {
8579         if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
8580                 internal_error(state, ins, "bad base for addrconst");
8581         }
8582         wipe_ins(state, ins);
8583         ins->op = OP_ADDRCONST;
8584         ins->misc = 1;
8585         MISC(ins, 0) = sdecl;
8586         ins->u.cval = value;
8587         use_triple(sdecl, ins);
8588 }
8589
8590 #if DEBUG_DECOMPOSE_PRINT_TUPLES
8591 static void print_tuple(struct compile_state *state, 
8592         struct triple *ins, struct triple *tuple)
8593 {
8594         FILE *fp = state->dbgout;
8595         fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8596         name_of(fp, tuple->type);
8597         if (tuple->lhs > 0) {
8598                 fprintf(fp, " lhs: ");
8599                 name_of(fp, LHS(tuple, 0)->type);
8600         }
8601         fprintf(fp, "\n");
8602         
8603 }
8604 #endif
8605
8606 static struct triple *decompose_with_tuple(struct compile_state *state, 
8607         struct triple *ins, struct triple *tuple)
8608 {
8609         struct triple *next;
8610         next = ins->next;
8611         flatten(state, next, tuple);
8612 #if DEBUG_DECOMPOSE_PRINT_TUPLES
8613         print_tuple(state, ins, tuple);
8614 #endif
8615
8616         if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8617                 struct triple *tmp;
8618                 if (tuple->lhs != 1) {
8619                         internal_error(state, tuple, "plain type in multiple registers?");
8620                 }
8621                 tmp = LHS(tuple, 0);
8622                 release_triple(state, tuple);
8623                 tuple = tmp;
8624         }
8625
8626         propogate_use(state, ins, tuple);
8627         release_triple(state, ins);
8628         
8629         return next;
8630 }
8631
8632 static struct triple *decompose_unknownval(struct compile_state *state,
8633         struct triple *ins)
8634 {
8635         struct triple *tuple;
8636         ulong_t i;
8637
8638 #if DEBUG_DECOMPOSE_HIRES
8639         FILE *fp = state->dbgout;
8640         fprintf(fp, "unknown type: ");
8641         name_of(fp, ins->type);
8642         fprintf(fp, "\n");
8643 #endif
8644
8645         get_occurance(ins->occurance);
8646         tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1, 
8647                 ins->occurance);
8648
8649         for(i = 0; i < tuple->lhs; i++) {
8650                 struct type *piece_type;
8651                 struct triple *unknown;
8652
8653                 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8654                 get_occurance(tuple->occurance);
8655                 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8656                         tuple->occurance);
8657                 LHS(tuple, i) = unknown;
8658         }
8659         return decompose_with_tuple(state, ins, tuple);
8660 }
8661
8662
8663 static struct triple *decompose_read(struct compile_state *state, 
8664         struct triple *ins)
8665 {
8666         struct triple *tuple, *lval;
8667         ulong_t i;
8668
8669         lval = RHS(ins, 0);
8670
8671         if (lval->op == OP_PIECE) {
8672                 return ins->next;
8673         }
8674         get_occurance(ins->occurance);
8675         tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8676                 ins->occurance);
8677
8678         if ((tuple->lhs != lval->lhs) &&
8679                 (!triple_is_def(state, lval) || (tuple->lhs != 1))) 
8680         {
8681                 internal_error(state, ins, "lhs size inconsistency?");
8682         }
8683         for(i = 0; i < tuple->lhs; i++) {
8684                 struct triple *piece, *read, *bitref;
8685                 if ((i != 0) || !triple_is_def(state, lval)) {
8686                         piece = LHS(lval, i);
8687                 } else {
8688                         piece = lval;
8689                 }
8690
8691                 /* See if the piece is really a bitref */
8692                 bitref = 0;
8693                 if (piece->op == OP_BITREF) {
8694                         bitref = piece;
8695                         piece = RHS(bitref, 0);
8696                 }
8697
8698                 get_occurance(tuple->occurance);
8699                 read = alloc_triple(state, OP_READ, piece->type, -1, -1, 
8700                         tuple->occurance);
8701                 RHS(read, 0) = piece;
8702
8703                 if (bitref) {
8704                         struct triple *extract;
8705                         int op;
8706                         if (is_signed(bitref->type->left)) {
8707                                 op = OP_SEXTRACT;
8708                         } else {
8709                                 op = OP_UEXTRACT;
8710                         }
8711                         get_occurance(tuple->occurance);
8712                         extract = alloc_triple(state, op, bitref->type, -1, -1,
8713                                 tuple->occurance);
8714                         RHS(extract, 0) = read;
8715                         extract->u.bitfield.size   = bitref->u.bitfield.size;
8716                         extract->u.bitfield.offset = bitref->u.bitfield.offset;
8717
8718                         read = extract;
8719                 }
8720
8721                 LHS(tuple, i) = read;
8722         }
8723         return decompose_with_tuple(state, ins, tuple);
8724 }
8725
8726 static struct triple *decompose_write(struct compile_state *state, 
8727         struct triple *ins)
8728 {
8729         struct triple *tuple, *lval, *val;
8730         ulong_t i;
8731         
8732         lval = MISC(ins, 0);
8733         val = RHS(ins, 0);
8734         get_occurance(ins->occurance);
8735         tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8736                 ins->occurance);
8737
8738         if ((tuple->lhs != lval->lhs) &&
8739                 (!triple_is_def(state, lval) || tuple->lhs != 1)) 
8740         {
8741                 internal_error(state, ins, "lhs size inconsistency?");
8742         }
8743         for(i = 0; i < tuple->lhs; i++) {
8744                 struct triple *piece, *write, *pval, *bitref;
8745                 if ((i != 0) || !triple_is_def(state, lval)) {
8746                         piece = LHS(lval, i);
8747                 } else {
8748                         piece = lval;
8749                 }
8750                 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8751                         pval = val;
8752                 }
8753                 else {
8754                         if (i > val->lhs) {
8755                                 internal_error(state, ins, "lhs size inconsistency?");
8756                         }
8757                         pval = LHS(val, i);
8758                 }
8759                 
8760                 /* See if the piece is really a bitref */
8761                 bitref = 0;
8762                 if (piece->op == OP_BITREF) {
8763                         struct triple *read, *deposit;
8764                         bitref = piece;
8765                         piece = RHS(bitref, 0);
8766
8767                         /* Read the destination register */
8768                         get_occurance(tuple->occurance);
8769                         read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8770                                 tuple->occurance);
8771                         RHS(read, 0) = piece;
8772
8773                         /* Deposit the new bitfield value */
8774                         get_occurance(tuple->occurance);
8775                         deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8776                                 tuple->occurance);
8777                         RHS(deposit, 0) = read;
8778                         RHS(deposit, 1) = pval;
8779                         deposit->u.bitfield.size   = bitref->u.bitfield.size;
8780                         deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8781
8782                         /* Now write the newly generated value */
8783                         pval = deposit;
8784                 }
8785
8786                 get_occurance(tuple->occurance);
8787                 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1, 
8788                         tuple->occurance);
8789                 MISC(write, 0) = piece;
8790                 RHS(write, 0) = pval;
8791                 LHS(tuple, i) = write;
8792         }
8793         return decompose_with_tuple(state, ins, tuple);
8794 }
8795
8796 struct decompose_load_info {
8797         struct occurance *occurance;
8798         struct triple *lval;
8799         struct triple *tuple;
8800 };
8801 static void decompose_load_cb(struct compile_state *state,
8802         struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8803 {
8804         struct decompose_load_info *info = arg;
8805         struct triple *load;
8806         
8807         if (reg_offset > info->tuple->lhs) {
8808                 internal_error(state, info->tuple, "lhs to small?");
8809         }
8810         get_occurance(info->occurance);
8811         load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8812         RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8813         LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8814 }
8815
8816 static struct triple *decompose_load(struct compile_state *state, 
8817         struct triple *ins)
8818 {
8819         struct triple *tuple;
8820         struct decompose_load_info info;
8821
8822         if (!is_compound_type(ins->type)) {
8823                 return ins->next;
8824         }
8825         get_occurance(ins->occurance);
8826         tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8827                 ins->occurance);
8828
8829         info.occurance = ins->occurance;
8830         info.lval      = RHS(ins, 0);
8831         info.tuple     = tuple;
8832         walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8833
8834         return decompose_with_tuple(state, ins, tuple);
8835 }
8836
8837
8838 struct decompose_store_info {
8839         struct occurance *occurance;
8840         struct triple *lval;
8841         struct triple *val;
8842         struct triple *tuple;
8843 };
8844 static void decompose_store_cb(struct compile_state *state,
8845         struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8846 {
8847         struct decompose_store_info *info = arg;
8848         struct triple *store;
8849         
8850         if (reg_offset > info->tuple->lhs) {
8851                 internal_error(state, info->tuple, "lhs to small?");
8852         }
8853         get_occurance(info->occurance);
8854         store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
8855         RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
8856         RHS(store, 1) = LHS(info->val, reg_offset);
8857         LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
8858 }
8859
8860 static struct triple *decompose_store(struct compile_state *state, 
8861         struct triple *ins)
8862 {
8863         struct triple *tuple;
8864         struct decompose_store_info info;
8865
8866         if (!is_compound_type(ins->type)) {
8867                 return ins->next;
8868         }
8869         get_occurance(ins->occurance);
8870         tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8871                 ins->occurance);
8872
8873         info.occurance = ins->occurance;
8874         info.lval      = RHS(ins, 0);
8875         info.val       = RHS(ins, 1);
8876         info.tuple     = tuple;
8877         walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
8878
8879         return decompose_with_tuple(state, ins, tuple);
8880 }
8881
8882 static struct triple *decompose_dot(struct compile_state *state, 
8883         struct triple *ins)
8884 {
8885         struct triple *tuple, *lval;
8886         struct type *type;
8887         size_t reg_offset;
8888         int i, idx;
8889
8890         lval = MISC(ins, 0);
8891         reg_offset = field_reg_offset(state, lval->type, ins->u.field);
8892         idx  = reg_offset/REG_SIZEOF_REG;
8893         type = field_type(state, lval->type, ins->u.field);
8894 #if DEBUG_DECOMPOSE_HIRES
8895         {
8896                 FILE *fp = state->dbgout;
8897                 fprintf(fp, "field type: ");
8898                 name_of(fp, type);
8899                 fprintf(fp, "\n");
8900         }
8901 #endif
8902
8903         get_occurance(ins->occurance);
8904         tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, 
8905                 ins->occurance);
8906
8907         if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
8908                 (tuple->lhs != 1))
8909         {
8910                 internal_error(state, ins, "multi register bitfield?");
8911         }
8912
8913         for(i = 0; i < tuple->lhs; i++, idx++) {
8914                 struct triple *piece;
8915                 if (!triple_is_def(state, lval)) {
8916                         if (idx > lval->lhs) {
8917                                 internal_error(state, ins, "inconsistent lhs count");
8918                         }
8919                         piece = LHS(lval, idx);
8920                 } else {
8921                         if (idx != 0) {
8922                                 internal_error(state, ins, "bad reg_offset into def");
8923                         }
8924                         if (i != 0) {
8925                                 internal_error(state, ins, "bad reg count from def");
8926                         }
8927                         piece = lval;
8928                 }
8929
8930                 /* Remember the offset of the bitfield */
8931                 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
8932                         get_occurance(ins->occurance);
8933                         piece = build_triple(state, OP_BITREF, type, piece, 0,
8934                                 ins->occurance);
8935                         piece->u.bitfield.size   = size_of(state, type);
8936                         piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
8937                 }
8938                 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
8939                         internal_error(state, ins, 
8940                                 "request for a nonbitfield sub register?");
8941                 }
8942
8943                 LHS(tuple, i) = piece;
8944         }
8945
8946         return decompose_with_tuple(state, ins, tuple);
8947 }
8948
8949 static struct triple *decompose_index(struct compile_state *state, 
8950         struct triple *ins)
8951 {
8952         struct triple *tuple, *lval;
8953         struct type *type;
8954         int i, idx;
8955
8956         lval = MISC(ins, 0);
8957         idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
8958         type = index_type(state, lval->type, ins->u.cval);
8959 #if DEBUG_DECOMPOSE_HIRES
8960 {
8961         FILE *fp = state->dbgout;
8962         fprintf(fp, "index type: ");
8963         name_of(fp, type);
8964         fprintf(fp, "\n");
8965 }
8966 #endif
8967
8968         get_occurance(ins->occurance);
8969         tuple = alloc_triple(state, OP_TUPLE, type, -1, -1, 
8970                 ins->occurance);
8971
8972         for(i = 0; i < tuple->lhs; i++, idx++) {
8973                 struct triple *piece;
8974                 if (!triple_is_def(state, lval)) {
8975                         if (idx > lval->lhs) {
8976                                 internal_error(state, ins, "inconsistent lhs count");
8977                         }
8978                         piece = LHS(lval, idx);
8979                 } else {
8980                         if (idx != 0) {
8981                                 internal_error(state, ins, "bad reg_offset into def");
8982                         }
8983                         if (i != 0) {
8984                                 internal_error(state, ins, "bad reg count from def");
8985                         }
8986                         piece = lval;
8987                 }
8988                 LHS(tuple, i) = piece;
8989         }
8990
8991         return decompose_with_tuple(state, ins, tuple);
8992 }
8993
8994 static void decompose_compound_types(struct compile_state *state)
8995 {
8996         struct triple *ins, *next, *first;
8997         FILE *fp;
8998         fp = state->dbgout;
8999         first = state->first;
9000         ins = first;
9001
9002         /* Pass one expand compound values into pseudo registers.
9003          */
9004         next = first;
9005         do {
9006                 ins = next;
9007                 next = ins->next;
9008                 switch(ins->op) {
9009                 case OP_UNKNOWNVAL:
9010                         next = decompose_unknownval(state, ins);
9011                         break;
9012
9013                 case OP_READ:
9014                         next = decompose_read(state, ins);
9015                         break;
9016
9017                 case OP_WRITE:
9018                         next = decompose_write(state, ins);
9019                         break;
9020
9021
9022                 /* Be very careful with the load/store logic. These
9023                  * operations must convert from the in register layout
9024                  * to the in memory layout, which is nontrivial.
9025                  */
9026                 case OP_LOAD:
9027                         next = decompose_load(state, ins);
9028                         break;
9029                 case OP_STORE:
9030                         next = decompose_store(state, ins);
9031                         break;
9032
9033                 case OP_DOT:
9034                         next = decompose_dot(state, ins);
9035                         break;
9036                 case OP_INDEX:
9037                         next = decompose_index(state, ins);
9038                         break;
9039                         
9040                 }
9041 #if DEBUG_DECOMPOSE_HIRES
9042                 fprintf(fp, "decompose next: %p \n", next);
9043                 fflush(fp);
9044                 fprintf(fp, "next->op: %d %s\n",
9045                         next->op, tops(next->op));
9046                 /* High resolution debugging mode */
9047                 print_triples(state);
9048 #endif
9049         } while (next != first);
9050
9051         /* Pass two remove the tuples.
9052          */
9053         ins = first;
9054         do {
9055                 next = ins->next;
9056                 if (ins->op == OP_TUPLE) {
9057                         if (ins->use) {
9058                                 internal_error(state, ins, "tuple used");
9059                         }
9060                         else {
9061                                 release_triple(state, ins);
9062                         }
9063                 } 
9064                 ins = next;
9065         } while(ins != first);
9066         ins = first;
9067         do {
9068                 next = ins->next;
9069                 if (ins->op == OP_BITREF) {
9070                         if (ins->use) {
9071                                 internal_error(state, ins, "bitref used");
9072                         } 
9073                         else {
9074                                 release_triple(state, ins);
9075                         }
9076                 }
9077                 ins = next;
9078         } while(ins != first);
9079
9080         /* Pass three verify the state and set ->id to 0.
9081          */
9082         next = first;
9083         do {
9084                 ins = next;
9085                 next = ins->next;
9086                 ins->id &= ~TRIPLE_FLAG_FLATTENED;
9087                 if (triple_stores_block(state, ins)) {
9088                         ins->u.block = 0;
9089                 }
9090                 if (triple_is_def(state, ins)) {
9091                         if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9092                                 internal_error(state, ins, "multi register value remains?");
9093                         }
9094                 }
9095                 if (ins->op == OP_DOT) {
9096                         internal_error(state, ins, "OP_DOT remains?");
9097                 }
9098                 if (ins->op == OP_INDEX) {
9099                         internal_error(state, ins, "OP_INDEX remains?");
9100                 }
9101                 if (ins->op == OP_BITREF) {
9102                         internal_error(state, ins, "OP_BITREF remains?");
9103                 }
9104                 if (ins->op == OP_TUPLE) {
9105                         internal_error(state, ins, "OP_TUPLE remains?");
9106                 }
9107         } while(next != first);
9108 }
9109
9110 /* For those operations that cannot be simplified */
9111 static void simplify_noop(struct compile_state *state, struct triple *ins)
9112 {
9113         return;
9114 }
9115
9116 static void simplify_smul(struct compile_state *state, struct triple *ins)
9117 {
9118         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
9119                 struct triple *tmp;
9120                 tmp = RHS(ins, 0);
9121                 RHS(ins, 0) = RHS(ins, 1);
9122                 RHS(ins, 1) = tmp;
9123         }
9124         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
9125                 long_t left, right;
9126                 left  = read_sconst(state, ins, RHS(ins, 0));
9127                 right = read_sconst(state, ins, RHS(ins, 1));
9128                 mkconst(state, ins, left * right);
9129         }
9130         else if (is_zero(RHS(ins, 1))) {
9131                 mkconst(state, ins, 0);
9132         }
9133         else if (is_one(RHS(ins, 1))) {
9134                 mkcopy(state, ins, RHS(ins, 0));
9135         }
9136         else if (is_pow2(RHS(ins, 1))) {
9137                 struct triple *val;
9138                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9139                 ins->op = OP_SL;
9140                 insert_triple(state, state->global_pool, val);
9141                 unuse_triple(RHS(ins, 1), ins);
9142                 use_triple(val, ins);
9143                 RHS(ins, 1) = val;
9144         }
9145 }
9146
9147 static void simplify_umul(struct compile_state *state, struct triple *ins)
9148 {
9149         if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
9150                 struct triple *tmp;
9151                 tmp = RHS(ins, 0);
9152                 RHS(ins, 0) = RHS(ins, 1);
9153                 RHS(ins, 1) = tmp;
9154         }
9155         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9156                 ulong_t left, right;
9157                 left  = read_const(state, ins, RHS(ins, 0));
9158                 right = read_const(state, ins, RHS(ins, 1));
9159                 mkconst(state, ins, left * right);
9160         }
9161         else if (is_zero(RHS(ins, 1))) {
9162                 mkconst(state, ins, 0);
9163         }
9164         else if (is_one(RHS(ins, 1))) {
9165                 mkcopy(state, ins, RHS(ins, 0));
9166         }
9167         else if (is_pow2(RHS(ins, 1))) {
9168                 struct triple *val;
9169                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9170                 ins->op = OP_SL;
9171                 insert_triple(state, state->global_pool, val);
9172                 unuse_triple(RHS(ins, 1), ins);
9173                 use_triple(val, ins);
9174                 RHS(ins, 1) = val;
9175         }
9176 }
9177
9178 static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9179 {
9180         if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
9181                 long_t left, right;
9182                 left  = read_sconst(state, ins, RHS(ins, 0));
9183                 right = read_sconst(state, ins, RHS(ins, 1));
9184                 mkconst(state, ins, left / right);
9185         }
9186         else if (is_zero(RHS(ins, 0))) {
9187                 mkconst(state, ins, 0);
9188         }
9189         else if (is_zero(RHS(ins, 1))) {
9190                 error(state, ins, "division by zero");
9191         }
9192         else if (is_one(RHS(ins, 1))) {
9193                 mkcopy(state, ins, RHS(ins, 0));
9194         }
9195         else if (is_pow2(RHS(ins, 1))) {
9196                 struct triple *val;
9197                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9198                 ins->op = OP_SSR;
9199                 insert_triple(state, state->global_pool, val);
9200                 unuse_triple(RHS(ins, 1), ins);
9201                 use_triple(val, ins);
9202                 RHS(ins, 1) = val;
9203         }
9204 }
9205
9206 static void simplify_udiv(struct compile_state *state, struct triple *ins)
9207 {
9208         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9209                 ulong_t left, right;
9210                 left  = read_const(state, ins, RHS(ins, 0));
9211                 right = read_const(state, ins, RHS(ins, 1));
9212                 mkconst(state, ins, left / right);
9213         }
9214         else if (is_zero(RHS(ins, 0))) {
9215                 mkconst(state, ins, 0);
9216         }
9217         else if (is_zero(RHS(ins, 1))) {
9218                 error(state, ins, "division by zero");
9219         }
9220         else if (is_one(RHS(ins, 1))) {
9221                 mkcopy(state, ins, RHS(ins, 0));
9222         }
9223         else if (is_pow2(RHS(ins, 1))) {
9224                 struct triple *val;
9225                 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
9226                 ins->op = OP_USR;
9227                 insert_triple(state, state->global_pool, val);
9228                 unuse_triple(RHS(ins, 1), ins);
9229                 use_triple(val, ins);
9230                 RHS(ins, 1) = val;
9231         }
9232 }
9233
9234 static void simplify_smod(struct compile_state *state, struct triple *ins)
9235 {
9236         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9237                 long_t left, right;
9238                 left  = read_const(state, ins, RHS(ins, 0));
9239                 right = read_const(state, ins, RHS(ins, 1));
9240                 mkconst(state, ins, left % right);
9241         }
9242         else if (is_zero(RHS(ins, 0))) {
9243                 mkconst(state, ins, 0);
9244         }
9245         else if (is_zero(RHS(ins, 1))) {
9246                 error(state, ins, "division by zero");
9247         }
9248         else if (is_one(RHS(ins, 1))) {
9249                 mkconst(state, ins, 0);
9250         }
9251         else if (is_pow2(RHS(ins, 1))) {
9252                 struct triple *val;
9253                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
9254                 ins->op = OP_AND;
9255                 insert_triple(state, state->global_pool, val);
9256                 unuse_triple(RHS(ins, 1), ins);
9257                 use_triple(val, ins);
9258                 RHS(ins, 1) = val;
9259         }
9260 }
9261
9262 static void simplify_umod(struct compile_state *state, struct triple *ins)
9263 {
9264         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9265                 ulong_t left, right;
9266                 left  = read_const(state, ins, RHS(ins, 0));
9267                 right = read_const(state, ins, RHS(ins, 1));
9268                 mkconst(state, ins, left % right);
9269         }
9270         else if (is_zero(RHS(ins, 0))) {
9271                 mkconst(state, ins, 0);
9272         }
9273         else if (is_zero(RHS(ins, 1))) {
9274                 error(state, ins, "division by zero");
9275         }
9276         else if (is_one(RHS(ins, 1))) {
9277                 mkconst(state, ins, 0);
9278         }
9279         else if (is_pow2(RHS(ins, 1))) {
9280                 struct triple *val;
9281                 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
9282                 ins->op = OP_AND;
9283                 insert_triple(state, state->global_pool, val);
9284                 unuse_triple(RHS(ins, 1), ins);
9285                 use_triple(val, ins);
9286                 RHS(ins, 1) = val;
9287         }
9288 }
9289
9290 static void simplify_add(struct compile_state *state, struct triple *ins)
9291 {
9292         /* start with the pointer on the left */
9293         if (is_pointer(RHS(ins, 1))) {
9294                 struct triple *tmp;
9295                 tmp = RHS(ins, 0);
9296                 RHS(ins, 0) = RHS(ins, 1);
9297                 RHS(ins, 1) = tmp;
9298         }
9299         if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9300                 if (RHS(ins, 0)->op == OP_INTCONST) {
9301                         ulong_t left, right;
9302                         left  = read_const(state, ins, RHS(ins, 0));
9303                         right = read_const(state, ins, RHS(ins, 1));
9304                         mkconst(state, ins, left + right);
9305                 }
9306                 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
9307                         struct triple *sdecl;
9308                         ulong_t left, right;
9309                         sdecl = MISC(RHS(ins, 0), 0);
9310                         left  = RHS(ins, 0)->u.cval;
9311                         right = RHS(ins, 1)->u.cval;
9312                         mkaddr_const(state, ins, sdecl, left + right);
9313                 }
9314                 else {
9315                         internal_warning(state, ins, "Optimize me!");
9316                 }
9317         }
9318         else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
9319                 struct triple *tmp;
9320                 tmp = RHS(ins, 1);
9321                 RHS(ins, 1) = RHS(ins, 0);
9322                 RHS(ins, 0) = tmp;
9323         }
9324 }
9325
9326 static void simplify_sub(struct compile_state *state, struct triple *ins)
9327 {
9328         if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9329                 if (RHS(ins, 0)->op == OP_INTCONST) {
9330                         ulong_t left, right;
9331                         left  = read_const(state, ins, RHS(ins, 0));
9332                         right = read_const(state, ins, RHS(ins, 1));
9333                         mkconst(state, ins, left - right);
9334                 }
9335                 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
9336                         struct triple *sdecl;
9337                         ulong_t left, right;
9338                         sdecl = MISC(RHS(ins, 0), 0);
9339                         left  = RHS(ins, 0)->u.cval;
9340                         right = RHS(ins, 1)->u.cval;
9341                         mkaddr_const(state, ins, sdecl, left - right);
9342                 }
9343                 else {
9344                         internal_warning(state, ins, "Optimize me!");
9345                 }
9346         }
9347 }
9348
9349 static void simplify_sl(struct compile_state *state, struct triple *ins)
9350 {
9351         if (is_simple_const(RHS(ins, 1))) {
9352                 ulong_t right;
9353                 right = read_const(state, ins, RHS(ins, 1));
9354                 if (right >= (size_of(state, ins->type))) {
9355                         warning(state, ins, "left shift count >= width of type");
9356                 }
9357         }
9358         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9359                 ulong_t left, right;
9360                 left  = read_const(state, ins, RHS(ins, 0));
9361                 right = read_const(state, ins, RHS(ins, 1));
9362                 mkconst(state, ins,  left << right);
9363         }
9364 }
9365
9366 static void simplify_usr(struct compile_state *state, struct triple *ins)
9367 {
9368         if (is_simple_const(RHS(ins, 1))) {
9369                 ulong_t right;
9370                 right = read_const(state, ins, RHS(ins, 1));
9371                 if (right >= (size_of(state, ins->type))) {
9372                         warning(state, ins, "right shift count >= width of type");
9373                 }
9374         }
9375         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9376                 ulong_t left, right;
9377                 left  = read_const(state, ins, RHS(ins, 0));
9378                 right = read_const(state, ins, RHS(ins, 1));
9379                 mkconst(state, ins, left >> right);
9380         }
9381 }
9382
9383 static void simplify_ssr(struct compile_state *state, struct triple *ins)
9384 {
9385         if (is_simple_const(RHS(ins, 1))) {
9386                 ulong_t right;
9387                 right = read_const(state, ins, RHS(ins, 1));
9388                 if (right >= (size_of(state, ins->type))) {
9389                         warning(state, ins, "right shift count >= width of type");
9390                 }
9391         }
9392         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9393                 long_t left, right;
9394                 left  = read_sconst(state, ins, RHS(ins, 0));
9395                 right = read_sconst(state, ins, RHS(ins, 1));
9396                 mkconst(state, ins, left >> right);
9397         }
9398 }
9399
9400 static void simplify_and(struct compile_state *state, struct triple *ins)
9401 {
9402         struct triple *left, *right;
9403         left = RHS(ins, 0);
9404         right = RHS(ins, 1);
9405
9406         if (is_simple_const(left) && is_simple_const(right)) {
9407                 ulong_t lval, rval;
9408                 lval = read_const(state, ins, left);
9409                 rval = read_const(state, ins, right);
9410                 mkconst(state, ins, lval & rval);
9411         }
9412         else if (is_zero(right) || is_zero(left)) {
9413                 mkconst(state, ins, 0);
9414         }
9415 }
9416
9417 static void simplify_or(struct compile_state *state, struct triple *ins)
9418 {
9419         struct triple *left, *right;
9420         left = RHS(ins, 0);
9421         right = RHS(ins, 1);
9422
9423         if (is_simple_const(left) && is_simple_const(right)) {
9424                 ulong_t lval, rval;
9425                 lval = read_const(state, ins, left);
9426                 rval = read_const(state, ins, right);
9427                 mkconst(state, ins, lval | rval);
9428         }
9429 #if 0 /* I need to handle type mismatches here... */
9430         else if (is_zero(right)) {
9431                 mkcopy(state, ins, left);
9432         }
9433         else if (is_zero(left)) {
9434                 mkcopy(state, ins, right);
9435         }
9436 #endif
9437 }
9438
9439 static void simplify_xor(struct compile_state *state, struct triple *ins)
9440 {
9441         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9442                 ulong_t left, right;
9443                 left  = read_const(state, ins, RHS(ins, 0));
9444                 right = read_const(state, ins, RHS(ins, 1));
9445                 mkconst(state, ins, left ^ right);
9446         }
9447 }
9448
9449 static void simplify_pos(struct compile_state *state, struct triple *ins)
9450 {
9451         if (is_const(RHS(ins, 0))) {
9452                 mkconst(state, ins, RHS(ins, 0)->u.cval);
9453         }
9454         else {
9455                 mkcopy(state, ins, RHS(ins, 0));
9456         }
9457 }
9458
9459 static void simplify_neg(struct compile_state *state, struct triple *ins)
9460 {
9461         if (is_simple_const(RHS(ins, 0))) {
9462                 ulong_t left;
9463                 left = read_const(state, ins, RHS(ins, 0));
9464                 mkconst(state, ins, -left);
9465         }
9466         else if (RHS(ins, 0)->op == OP_NEG) {
9467                 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
9468         }
9469 }
9470
9471 static void simplify_invert(struct compile_state *state, struct triple *ins)
9472 {
9473         if (is_simple_const(RHS(ins, 0))) {
9474                 ulong_t left;
9475                 left = read_const(state, ins, RHS(ins, 0));
9476                 mkconst(state, ins, ~left);
9477         }
9478 }
9479
9480 static void simplify_eq(struct compile_state *state, struct triple *ins)
9481 {
9482         struct triple *left, *right;
9483         left = RHS(ins, 0);
9484         right = RHS(ins, 1);
9485
9486         if (is_const(left) && is_const(right)) {
9487                 int val;
9488                 val = const_eq(state, ins, left, right);
9489                 if (val >= 0) {
9490                         mkconst(state, ins, val == 1);
9491                 }
9492         }
9493         else if (left == right) {
9494                 mkconst(state, ins, 1);
9495         }
9496 }
9497
9498 static void simplify_noteq(struct compile_state *state, struct triple *ins)
9499 {
9500         struct triple *left, *right;
9501         left = RHS(ins, 0);
9502         right = RHS(ins, 1);
9503
9504         if (is_const(left) && is_const(right)) {
9505                 int val;
9506                 val = const_eq(state, ins, left, right);
9507                 if (val >= 0) {
9508                         mkconst(state, ins, val != 1);
9509                 }
9510         }
9511         if (left == right) {
9512                 mkconst(state, ins, 0);
9513         }
9514 }
9515
9516 static void simplify_sless(struct compile_state *state, struct triple *ins)
9517 {
9518         struct triple *left, *right;
9519         left = RHS(ins, 0);
9520         right = RHS(ins, 1);
9521
9522         if (is_const(left) && is_const(right)) {
9523                 int val;
9524                 val = const_scmp(state, ins, left, right);
9525                 if ((val >= -1) && (val <= 1)) {
9526                         mkconst(state, ins, val < 0);
9527                 }
9528         }
9529         else if (left == right) {
9530                 mkconst(state, ins, 0);
9531         }
9532 }
9533
9534 static void simplify_uless(struct compile_state *state, struct triple *ins)
9535 {
9536         struct triple *left, *right;
9537         left = RHS(ins, 0);
9538         right = RHS(ins, 1);
9539
9540         if (is_const(left) && is_const(right)) {
9541                 int val;
9542                 val = const_ucmp(state, ins, left, right);
9543                 if ((val >= -1) && (val <= 1)) {
9544                         mkconst(state, ins, val < 0);
9545                 }
9546         }
9547         else if (is_zero(right)) {
9548                 mkconst(state, ins, 0);
9549         }
9550         else if (left == right) {
9551                 mkconst(state, ins, 0);
9552         }
9553 }
9554
9555 static void simplify_smore(struct compile_state *state, struct triple *ins)
9556 {
9557         struct triple *left, *right;
9558         left = RHS(ins, 0);
9559         right = RHS(ins, 1);
9560
9561         if (is_const(left) && is_const(right)) {
9562                 int val;
9563                 val = const_scmp(state, ins, left, right);
9564                 if ((val >= -1) && (val <= 1)) {
9565                         mkconst(state, ins, val > 0);
9566                 }
9567         }
9568         else if (left == right) {
9569                 mkconst(state, ins, 0);
9570         }
9571 }
9572
9573 static void simplify_umore(struct compile_state *state, struct triple *ins)
9574 {
9575         struct triple *left, *right;
9576         left = RHS(ins, 0);
9577         right = RHS(ins, 1);
9578
9579         if (is_const(left) && is_const(right)) {
9580                 int val;
9581                 val = const_ucmp(state, ins, left, right);
9582                 if ((val >= -1) && (val <= 1)) {
9583                         mkconst(state, ins, val > 0);
9584                 }
9585         }
9586         else if (is_zero(left)) {
9587                 mkconst(state, ins, 0);
9588         }
9589         else if (left == right) {
9590                 mkconst(state, ins, 0);
9591         }
9592 }
9593
9594
9595 static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9596 {
9597         struct triple *left, *right;
9598         left = RHS(ins, 0);
9599         right = RHS(ins, 1);
9600
9601         if (is_const(left) && is_const(right)) {
9602                 int val;
9603                 val = const_scmp(state, ins, left, right);
9604                 if ((val >= -1) && (val <= 1)) {
9605                         mkconst(state, ins, val <= 0);
9606                 }
9607         }
9608         else if (left == right) {
9609                 mkconst(state, ins, 1);
9610         }
9611 }
9612
9613 static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9614 {
9615         struct triple *left, *right;
9616         left = RHS(ins, 0);
9617         right = RHS(ins, 1);
9618
9619         if (is_const(left) && is_const(right)) {
9620                 int val;
9621                 val = const_ucmp(state, ins, left, right);
9622                 if ((val >= -1) && (val <= 1)) {
9623                         mkconst(state, ins, val <= 0);
9624                 }
9625         }
9626         else if (is_zero(left)) {
9627                 mkconst(state, ins, 1);
9628         }
9629         else if (left == right) {
9630                 mkconst(state, ins, 1);
9631         }
9632 }
9633
9634 static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9635 {
9636         struct triple *left, *right;
9637         left = RHS(ins, 0);
9638         right = RHS(ins, 1);
9639
9640         if (is_const(left) && is_const(right)) {
9641                 int val;
9642                 val = const_scmp(state, ins, left, right);
9643                 if ((val >= -1) && (val <= 1)) {
9644                         mkconst(state, ins, val >= 0);
9645                 }
9646         }
9647         else if (left == right) {
9648                 mkconst(state, ins, 1);
9649         }
9650 }
9651
9652 static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9653 {
9654         struct triple *left, *right;
9655         left = RHS(ins, 0);
9656         right = RHS(ins, 1);
9657
9658         if (is_const(left) && is_const(right)) {
9659                 int val;
9660                 val = const_ucmp(state, ins, left, right);
9661                 if ((val >= -1) && (val <= 1)) {
9662                         mkconst(state, ins, val >= 0);
9663                 }
9664         }
9665         else if (is_zero(right)) {
9666                 mkconst(state, ins, 1);
9667         }
9668         else if (left == right) {
9669                 mkconst(state, ins, 1);
9670         }
9671 }
9672
9673 static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9674 {
9675         struct triple *rhs;
9676         rhs = RHS(ins, 0);
9677
9678         if (is_const(rhs)) {
9679                 mkconst(state, ins, !const_ltrue(state, ins, rhs));
9680         }
9681         /* Otherwise if I am the only user... */
9682         else if ((rhs->use) &&
9683                 (rhs->use->member == ins) && (rhs->use->next == 0)) {
9684                 int need_copy = 1;
9685                 /* Invert a boolean operation */
9686                 switch(rhs->op) {
9687                 case OP_LTRUE:   rhs->op = OP_LFALSE;  break;
9688                 case OP_LFALSE:  rhs->op = OP_LTRUE;   break;
9689                 case OP_EQ:      rhs->op = OP_NOTEQ;   break;
9690                 case OP_NOTEQ:   rhs->op = OP_EQ;      break;
9691                 case OP_SLESS:   rhs->op = OP_SMOREEQ; break;
9692                 case OP_ULESS:   rhs->op = OP_UMOREEQ; break;
9693                 case OP_SMORE:   rhs->op = OP_SLESSEQ; break;
9694                 case OP_UMORE:   rhs->op = OP_ULESSEQ; break;
9695                 case OP_SLESSEQ: rhs->op = OP_SMORE;   break;
9696                 case OP_ULESSEQ: rhs->op = OP_UMORE;   break;
9697                 case OP_SMOREEQ: rhs->op = OP_SLESS;   break;
9698                 case OP_UMOREEQ: rhs->op = OP_ULESS;   break;
9699                 default:
9700                         need_copy = 0;
9701                         break;
9702                 }
9703                 if (need_copy) {
9704                         mkcopy(state, ins, rhs);
9705                 }
9706         }
9707 }
9708
9709 static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9710 {
9711         struct triple *rhs;
9712         rhs = RHS(ins, 0);
9713
9714         if (is_const(rhs)) {
9715                 mkconst(state, ins, const_ltrue(state, ins, rhs));
9716         }
9717         else switch(rhs->op) {
9718         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
9719         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
9720         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
9721                 mkcopy(state, ins, rhs);
9722         }
9723
9724 }
9725
9726 static void simplify_load(struct compile_state *state, struct triple *ins)
9727 {
9728         struct triple *addr, *sdecl, *blob;
9729
9730         /* If I am doing a load with a constant pointer from a constant
9731          * table get the value.
9732          */
9733         addr = RHS(ins, 0);
9734         if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9735                 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9736                 (blob->op == OP_BLOBCONST)) {
9737                 unsigned char buffer[SIZEOF_WORD];
9738                 size_t reg_size, mem_size;
9739                 const char *src, *end;
9740                 ulong_t val;
9741                 reg_size = reg_size_of(state, ins->type);
9742                 if (reg_size > REG_SIZEOF_REG) {
9743                         internal_error(state, ins, "load size greater than register");
9744                 }
9745                 mem_size = size_of(state, ins->type);
9746                 end = blob->u.blob;
9747                 end += bits_to_bytes(size_of(state, sdecl->type));
9748                 src = blob->u.blob;
9749                 src += addr->u.cval;
9750
9751                 if (src > end) {
9752                         error(state, ins, "Load address out of bounds");
9753                 }
9754
9755                 memset(buffer, 0, sizeof(buffer));
9756                 memcpy(buffer, src, bits_to_bytes(mem_size));
9757
9758                 switch(mem_size) {
9759                 case SIZEOF_I8:  val = *((uint8_t *) buffer); break;
9760                 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9761                 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9762                 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9763                 default:
9764                         internal_error(state, ins, "mem_size: %d not handled",
9765                                 mem_size);
9766                         val = 0;
9767                         break;
9768                 }
9769                 mkconst(state, ins, val);
9770         }
9771 }
9772
9773 static void simplify_uextract(struct compile_state *state, struct triple *ins)
9774 {
9775         if (is_simple_const(RHS(ins, 0))) {
9776                 ulong_t val;
9777                 ulong_t mask;
9778                 val = read_const(state, ins, RHS(ins, 0));
9779                 mask = 1;
9780                 mask <<= ins->u.bitfield.size;
9781                 mask -= 1;
9782                 val >>= ins->u.bitfield.offset;
9783                 val &= mask;
9784                 mkconst(state, ins, val);
9785         }
9786 }
9787
9788 static void simplify_sextract(struct compile_state *state, struct triple *ins)
9789 {
9790         if (is_simple_const(RHS(ins, 0))) {
9791                 ulong_t val;
9792                 ulong_t mask;
9793                 long_t sval;
9794                 val = read_const(state, ins, RHS(ins, 0));
9795                 mask = 1;
9796                 mask <<= ins->u.bitfield.size;
9797                 mask -= 1;
9798                 val >>= ins->u.bitfield.offset;
9799                 val &= mask;
9800                 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9801                 sval = val;
9802                 sval >>= (SIZEOF_LONG - ins->u.bitfield.size); 
9803                 mkconst(state, ins, sval);
9804         }
9805 }
9806
9807 static void simplify_deposit(struct compile_state *state, struct triple *ins)
9808 {
9809         if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9810                 ulong_t targ, val;
9811                 ulong_t mask;
9812                 targ = read_const(state, ins, RHS(ins, 0));
9813                 val  = read_const(state, ins, RHS(ins, 1));
9814                 mask = 1;
9815                 mask <<= ins->u.bitfield.size;
9816                 mask -= 1;
9817                 mask <<= ins->u.bitfield.offset;
9818                 targ &= ~mask;
9819                 val <<= ins->u.bitfield.offset;
9820                 val &= mask;
9821                 targ |= val;
9822                 mkconst(state, ins, targ);
9823         }
9824 }
9825
9826 static void simplify_copy(struct compile_state *state, struct triple *ins)
9827 {
9828         struct triple *right;
9829         right = RHS(ins, 0);
9830         if (is_subset_type(ins->type, right->type)) {
9831                 ins->type = right->type;
9832         }
9833         if (equiv_types(ins->type, right->type)) {
9834                 ins->op = OP_COPY;/* I don't need to convert if the types match */
9835         } else {
9836                 if (ins->op == OP_COPY) {
9837                         internal_error(state, ins, "type mismatch on copy");
9838                 }
9839         }
9840         if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
9841                 struct triple *sdecl;
9842                 ulong_t offset;
9843                 sdecl  = MISC(right, 0);
9844                 offset = right->u.cval;
9845                 mkaddr_const(state, ins, sdecl, offset);
9846         }
9847         else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
9848                 switch(right->op) {
9849                 case OP_INTCONST:
9850                 {
9851                         ulong_t left;
9852                         left = read_const(state, ins, right);
9853                         /* Ensure I have not overflowed the destination. */
9854                         if (size_of(state, right->type) > size_of(state, ins->type)) {
9855                                 ulong_t mask;
9856                                 mask = 1;
9857                                 mask <<= size_of(state, ins->type);
9858                                 mask -= 1;
9859                                 left &= mask;
9860                         }
9861                         /* Ensure I am properly sign extended */
9862                         if (size_of(state, right->type) < size_of(state, ins->type) &&
9863                                 is_signed(right->type)) {
9864                                 long_t val;
9865                                 int shift;
9866                                 shift = SIZEOF_LONG - size_of(state, right->type);
9867                                 val = left;
9868                                 val <<= shift;
9869                                 val >>= shift;
9870                                 left = val;
9871                         }
9872                         mkconst(state, ins, left);
9873                         break;
9874                 }
9875                 default:
9876                         internal_error(state, ins, "uknown constant");
9877                         break;
9878                 }
9879         }
9880 }
9881
9882 static int phi_present(struct block *block)
9883 {
9884         struct triple *ptr;
9885         if (!block) {
9886                 return 0;
9887         }
9888         ptr = block->first;
9889         do {
9890                 if (ptr->op == OP_PHI) {
9891                         return 1;
9892                 }
9893                 ptr = ptr->next;
9894         } while(ptr != block->last);
9895         return 0;
9896 }
9897
9898 static int phi_dependency(struct block *block)
9899 {
9900         /* A block has a phi dependency if a phi function
9901          * depends on that block to exist, and makes a block
9902          * that is otherwise useless unsafe to remove.
9903          */
9904         if (block) {
9905                 struct block_set *edge;
9906                 for(edge = block->edges; edge; edge = edge->next) {
9907                         if (phi_present(edge->member)) {
9908                                 return 1;
9909                         }
9910                 }
9911         }
9912         return 0;
9913 }
9914
9915 static struct triple *branch_target(struct compile_state *state, struct triple *ins)
9916 {
9917         struct triple *targ;
9918         targ = TARG(ins, 0);
9919         /* During scc_transform temporary triples are allocated that
9920          * loop back onto themselves. If I see one don't advance the
9921          * target.
9922          */
9923         while(triple_is_structural(state, targ) && 
9924                 (targ->next != targ) && (targ->next != state->first)) {
9925                 targ = targ->next;
9926         }
9927         return targ;
9928 }
9929
9930
9931 static void simplify_branch(struct compile_state *state, struct triple *ins)
9932 {
9933         int simplified, loops;
9934         if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
9935                 internal_error(state, ins, "not branch");
9936         }
9937         if (ins->use != 0) {
9938                 internal_error(state, ins, "branch use");
9939         }
9940         /* The challenge here with simplify branch is that I need to 
9941          * make modifications to the control flow graph as well
9942          * as to the branch instruction itself.  That is handled
9943          * by rebuilding the basic blocks after simplify all is called.
9944          */
9945
9946         /* If we have a branch to an unconditional branch update
9947          * our target.  But watch out for dependencies from phi
9948          * functions.
9949          * Also only do this a limited number of times so
9950          * we don't get into an infinite loop.
9951          */
9952         loops = 0;
9953         do {
9954                 struct triple *targ;
9955                 simplified = 0;
9956                 targ = branch_target(state, ins);
9957                 if ((targ != ins) && (targ->op == OP_BRANCH) && 
9958                         !phi_dependency(targ->u.block))
9959                 {
9960                         unuse_triple(TARG(ins, 0), ins);
9961                         TARG(ins, 0) = TARG(targ, 0);
9962                         use_triple(TARG(ins, 0), ins);
9963                         simplified = 1;
9964                 }
9965         } while(simplified && (++loops < 20));
9966
9967         /* If we have a conditional branch with a constant condition
9968          * make it an unconditional branch.
9969          */
9970         if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
9971                 struct triple *targ;
9972                 ulong_t value;
9973                 value = read_const(state, ins, RHS(ins, 0));
9974                 unuse_triple(RHS(ins, 0), ins);
9975                 targ = TARG(ins, 0);
9976                 ins->rhs  = 0;
9977                 ins->targ = 1;
9978                 ins->op = OP_BRANCH;
9979                 if (value) {
9980                         unuse_triple(ins->next, ins);
9981                         TARG(ins, 0) = targ;
9982                 }
9983                 else {
9984                         unuse_triple(targ, ins);
9985                         TARG(ins, 0) = ins->next;
9986                 }
9987         }
9988
9989         /* If we have a branch to the next instruction,
9990          * make it a noop.
9991          */
9992         if (TARG(ins, 0) == ins->next) {
9993                 unuse_triple(TARG(ins, 0), ins);
9994                 if (ins->op == OP_CBRANCH) {
9995                         unuse_triple(RHS(ins, 0), ins);
9996                         unuse_triple(ins->next, ins);
9997                 }
9998                 ins->lhs = 0;
9999                 ins->rhs = 0;
10000                 ins->misc = 0;
10001                 ins->targ = 0;
10002                 ins->op = OP_NOOP;
10003                 if (ins->use) {
10004                         internal_error(state, ins, "noop use != 0");
10005                 }
10006         }
10007 }
10008
10009 static void simplify_label(struct compile_state *state, struct triple *ins)
10010 {
10011         /* Ignore volatile labels */
10012         if (!triple_is_pure(state, ins, ins->id)) {
10013                 return;
10014         }
10015         if (ins->use == 0) {
10016                 ins->op = OP_NOOP;
10017         }
10018         else if (ins->prev->op == OP_LABEL) {
10019                 /* In general it is not safe to merge one label that
10020                  * imediately follows another.  The problem is that the empty
10021                  * looking block may have phi functions that depend on it.
10022                  */
10023                 if (!phi_dependency(ins->prev->u.block)) {
10024                         struct triple_set *user, *next;
10025                         ins->op = OP_NOOP;
10026                         for(user = ins->use; user; user = next) {
10027                                 struct triple *use, **expr;
10028                                 next = user->next;
10029                                 use = user->member;
10030                                 expr = triple_targ(state, use, 0);
10031                                 for(;expr; expr = triple_targ(state, use, expr)) {
10032                                         if (*expr == ins) {
10033                                                 *expr = ins->prev;
10034                                                 unuse_triple(ins, use);
10035                                                 use_triple(ins->prev, use);
10036                                         }
10037                                         
10038                                 }
10039                         }
10040                         if (ins->use) {
10041                                 internal_error(state, ins, "noop use != 0");
10042                         }
10043                 }
10044         }
10045 }
10046
10047 static void simplify_phi(struct compile_state *state, struct triple *ins)
10048 {
10049         struct triple **slot;
10050         struct triple *value;
10051         int zrhs, i;
10052         ulong_t cvalue;
10053         slot = &RHS(ins, 0);
10054         zrhs = ins->rhs;
10055         if (zrhs == 0) {
10056                 return;
10057         }
10058         /* See if all of the rhs members of a phi have the same value */
10059         if (slot[0] && is_simple_const(slot[0])) {
10060                 cvalue = read_const(state, ins, slot[0]);
10061                 for(i = 1; i < zrhs; i++) {
10062                         if (    !slot[i] ||
10063                                 !is_simple_const(slot[i]) ||
10064                                 !equiv_types(slot[0]->type, slot[i]->type) ||
10065                                 (cvalue != read_const(state, ins, slot[i]))) {
10066                                 break;
10067                         }
10068                 }
10069                 if (i == zrhs) {
10070                         mkconst(state, ins, cvalue);
10071                         return;
10072                 }
10073         }
10074         
10075         /* See if all of rhs members of a phi are the same */
10076         value = slot[0];
10077         for(i = 1; i < zrhs; i++) {
10078                 if (slot[i] != value) {
10079                         break;
10080                 }
10081         }
10082         if (i == zrhs) {
10083                 /* If the phi has a single value just copy it */
10084                 if (!is_subset_type(ins->type, value->type)) {
10085                         internal_error(state, ins, "bad input type to phi");
10086                 }
10087                 /* Make the types match */
10088                 if (!equiv_types(ins->type, value->type)) {
10089                         ins->type = value->type;
10090                 }
10091                 /* Now make the actual copy */
10092                 mkcopy(state, ins, value);
10093                 return;
10094         }
10095 }
10096
10097
10098 static void simplify_bsf(struct compile_state *state, struct triple *ins)
10099 {
10100         if (is_simple_const(RHS(ins, 0))) {
10101                 ulong_t left;
10102                 left = read_const(state, ins, RHS(ins, 0));
10103                 mkconst(state, ins, bsf(left));
10104         }
10105 }
10106
10107 static void simplify_bsr(struct compile_state *state, struct triple *ins)
10108 {
10109         if (is_simple_const(RHS(ins, 0))) {
10110                 ulong_t left;
10111                 left = read_const(state, ins, RHS(ins, 0));
10112                 mkconst(state, ins, bsr(left));
10113         }
10114 }
10115
10116
10117 typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
10118 static const struct simplify_table {
10119         simplify_t func;
10120         unsigned long flag;
10121 } table_simplify[] = {
10122 #define simplify_sdivt    simplify_noop
10123 #define simplify_udivt    simplify_noop
10124 #define simplify_piece    simplify_noop
10125
10126 [OP_SDIVT      ] = { simplify_sdivt,    COMPILER_SIMPLIFY_ARITH },
10127 [OP_UDIVT      ] = { simplify_udivt,    COMPILER_SIMPLIFY_ARITH },
10128 [OP_SMUL       ] = { simplify_smul,     COMPILER_SIMPLIFY_ARITH },
10129 [OP_UMUL       ] = { simplify_umul,     COMPILER_SIMPLIFY_ARITH },
10130 [OP_SDIV       ] = { simplify_sdiv,     COMPILER_SIMPLIFY_ARITH },
10131 [OP_UDIV       ] = { simplify_udiv,     COMPILER_SIMPLIFY_ARITH },
10132 [OP_SMOD       ] = { simplify_smod,     COMPILER_SIMPLIFY_ARITH },
10133 [OP_UMOD       ] = { simplify_umod,     COMPILER_SIMPLIFY_ARITH },
10134 [OP_ADD        ] = { simplify_add,      COMPILER_SIMPLIFY_ARITH },
10135 [OP_SUB        ] = { simplify_sub,      COMPILER_SIMPLIFY_ARITH },
10136 [OP_SL         ] = { simplify_sl,       COMPILER_SIMPLIFY_SHIFT },
10137 [OP_USR        ] = { simplify_usr,      COMPILER_SIMPLIFY_SHIFT },
10138 [OP_SSR        ] = { simplify_ssr,      COMPILER_SIMPLIFY_SHIFT },
10139 [OP_AND        ] = { simplify_and,      COMPILER_SIMPLIFY_BITWISE },
10140 [OP_XOR        ] = { simplify_xor,      COMPILER_SIMPLIFY_BITWISE },
10141 [OP_OR         ] = { simplify_or,       COMPILER_SIMPLIFY_BITWISE },
10142 [OP_POS        ] = { simplify_pos,      COMPILER_SIMPLIFY_ARITH },
10143 [OP_NEG        ] = { simplify_neg,      COMPILER_SIMPLIFY_ARITH },
10144 [OP_INVERT     ] = { simplify_invert,   COMPILER_SIMPLIFY_BITWISE },
10145
10146 [OP_EQ         ] = { simplify_eq,       COMPILER_SIMPLIFY_LOGICAL },
10147 [OP_NOTEQ      ] = { simplify_noteq,    COMPILER_SIMPLIFY_LOGICAL },
10148 [OP_SLESS      ] = { simplify_sless,    COMPILER_SIMPLIFY_LOGICAL },
10149 [OP_ULESS      ] = { simplify_uless,    COMPILER_SIMPLIFY_LOGICAL },
10150 [OP_SMORE      ] = { simplify_smore,    COMPILER_SIMPLIFY_LOGICAL },
10151 [OP_UMORE      ] = { simplify_umore,    COMPILER_SIMPLIFY_LOGICAL },
10152 [OP_SLESSEQ    ] = { simplify_slesseq,  COMPILER_SIMPLIFY_LOGICAL },
10153 [OP_ULESSEQ    ] = { simplify_ulesseq,  COMPILER_SIMPLIFY_LOGICAL },
10154 [OP_SMOREEQ    ] = { simplify_smoreeq,  COMPILER_SIMPLIFY_LOGICAL },
10155 [OP_UMOREEQ    ] = { simplify_umoreeq,  COMPILER_SIMPLIFY_LOGICAL },
10156 [OP_LFALSE     ] = { simplify_lfalse,   COMPILER_SIMPLIFY_LOGICAL },
10157 [OP_LTRUE      ] = { simplify_ltrue,    COMPILER_SIMPLIFY_LOGICAL },
10158
10159 [OP_LOAD       ] = { simplify_load,     COMPILER_SIMPLIFY_OP },
10160 [OP_STORE      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10161
10162 [OP_UEXTRACT   ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10163 [OP_SEXTRACT   ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10164 [OP_DEPOSIT    ] = { simplify_deposit,  COMPILER_SIMPLIFY_BITFIELD },
10165
10166 [OP_NOOP       ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10167
10168 [OP_INTCONST   ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10169 [OP_BLOBCONST  ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10170 [OP_ADDRCONST  ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10171 [OP_UNKNOWNVAL ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10172
10173 [OP_WRITE      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10174 [OP_READ       ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10175 [OP_COPY       ] = { simplify_copy,     COMPILER_SIMPLIFY_COPY },
10176 [OP_CONVERT    ] = { simplify_copy,     COMPILER_SIMPLIFY_COPY },
10177 [OP_PIECE      ] = { simplify_piece,    COMPILER_SIMPLIFY_OP },
10178 [OP_ASM        ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10179
10180 [OP_DOT        ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10181 [OP_INDEX      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10182
10183 [OP_LIST       ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10184 [OP_BRANCH     ] = { simplify_branch,   COMPILER_SIMPLIFY_BRANCH },
10185 [OP_CBRANCH    ] = { simplify_branch,   COMPILER_SIMPLIFY_BRANCH },
10186 [OP_CALL       ] = { simplify_noop,     COMPILER_SIMPLIFY_BRANCH },
10187 [OP_RET        ] = { simplify_noop,     COMPILER_SIMPLIFY_BRANCH },
10188 [OP_LABEL      ] = { simplify_label,    COMPILER_SIMPLIFY_LABEL },
10189 [OP_ADECL      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10190 [OP_SDECL      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10191 [OP_PHI        ] = { simplify_phi,      COMPILER_SIMPLIFY_PHI },
10192
10193 [OP_INB        ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10194 [OP_INW        ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10195 [OP_INL        ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10196 [OP_OUTB       ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10197 [OP_OUTW       ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10198 [OP_OUTL       ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10199 [OP_BSF        ] = { simplify_bsf,      COMPILER_SIMPLIFY_OP },
10200 [OP_BSR        ] = { simplify_bsr,      COMPILER_SIMPLIFY_OP },
10201 [OP_RDMSR      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10202 [OP_WRMSR      ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },               
10203 [OP_HLT        ] = { simplify_noop,     COMPILER_SIMPLIFY_OP },
10204 };
10205
10206 static inline void debug_simplify(struct compile_state *state, 
10207         simplify_t do_simplify, struct triple *ins)
10208 {
10209 #if DEBUG_SIMPLIFY_HIRES
10210                 if (state->functions_joined && (do_simplify != simplify_noop)) {
10211                         /* High resolution debugging mode */
10212                         fprintf(state->dbgout, "simplifing: ");
10213                         display_triple(state->dbgout, ins);
10214                 }
10215 #endif
10216                 do_simplify(state, ins);
10217 #if DEBUG_SIMPLIFY_HIRES
10218                 if (state->functions_joined && (do_simplify != simplify_noop)) {
10219                         /* High resolution debugging mode */
10220                         fprintf(state->dbgout, "simplified: ");
10221                         display_triple(state->dbgout, ins);
10222                 }
10223 #endif
10224 }
10225 static void simplify(struct compile_state *state, struct triple *ins)
10226 {
10227         int op;
10228         simplify_t do_simplify;
10229         if (ins == &unknown_triple) {
10230                 internal_error(state, ins, "simplifying the unknown triple?");
10231         }
10232         do {
10233                 op = ins->op;
10234                 do_simplify = 0;
10235                 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10236                         do_simplify = 0;
10237                 }
10238                 else {
10239                         do_simplify = table_simplify[op].func;
10240                 }
10241                 if (do_simplify && 
10242                         !(state->compiler->flags & table_simplify[op].flag)) {
10243                         do_simplify = simplify_noop;
10244                 }
10245                 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
10246                         do_simplify = simplify_noop;
10247                 }
10248         
10249                 if (!do_simplify) {
10250                         internal_error(state, ins, "cannot simplify op: %d %s",
10251                                 op, tops(op));
10252                         return;
10253                 }
10254                 debug_simplify(state, do_simplify, ins);
10255         } while(ins->op != op);
10256 }
10257
10258 static void rebuild_ssa_form(struct compile_state *state);
10259
10260 static void simplify_all(struct compile_state *state)
10261 {
10262         struct triple *ins, *first;
10263         if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10264                 return;
10265         }
10266         first = state->first;
10267         ins = first->prev;
10268         do {
10269                 simplify(state, ins);
10270                 ins = ins->prev;
10271         } while(ins != first->prev);
10272         ins = first;
10273         do {
10274                 simplify(state, ins);
10275                 ins = ins->next;
10276         }while(ins != first);
10277         rebuild_ssa_form(state);
10278
10279         print_blocks(state, __func__, state->dbgout);
10280 }
10281
10282 /*
10283  * Builtins....
10284  * ============================
10285  */
10286
10287 static void register_builtin_function(struct compile_state *state,
10288         const char *name, int op, struct type *rtype, ...)
10289 {
10290         struct type *ftype, *atype, *ctype, *crtype, *param, **next;
10291         struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
10292         struct hash_entry *ident;
10293         struct file_state file;
10294         int parameters;
10295         int name_len;
10296         va_list args;
10297         int i;
10298
10299         /* Dummy file state to get debug handling right */
10300         memset(&file, 0, sizeof(file));
10301         file.basename = "<built-in>";
10302         file.line = 1;
10303         file.report_line = 1;
10304         file.report_name = file.basename;
10305         file.prev = state->file;
10306         state->file = &file;
10307         state->function = name;
10308
10309         /* Find the Parameter count */
10310         valid_op(state, op);
10311         parameters = table_ops[op].rhs;
10312         if (parameters < 0 ) {
10313                 internal_error(state, 0, "Invalid builtin parameter count");
10314         }
10315
10316         /* Find the function type */
10317         ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
10318         ftype->elements = parameters;
10319         next = &ftype->right;
10320         va_start(args, rtype);
10321         for(i = 0; i < parameters; i++) {
10322                 atype = va_arg(args, struct type *);
10323                 if (!*next) {
10324                         *next = atype;
10325                 } else {
10326                         *next = new_type(TYPE_PRODUCT, *next, atype);
10327                         next = &((*next)->right);
10328                 }
10329         }
10330         if (!*next) {
10331                 *next = &void_type;
10332         }
10333         va_end(args);
10334
10335         /* Get the initial closure type */
10336         ctype = new_type(TYPE_JOIN, &void_type, 0);
10337         ctype->elements = 1;
10338
10339         /* Get the return type */
10340         crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10341         crtype->elements = 2;
10342
10343         /* Generate the needed triples */
10344         def = triple(state, OP_LIST, ftype, 0, 0);
10345         first = label(state);
10346         RHS(def, 0) = first;
10347         result = flatten(state, first, variable(state, crtype));
10348         retvar = flatten(state, first, variable(state, &void_ptr_type));
10349         ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
10350
10351         /* Now string them together */
10352         param = ftype->right;
10353         for(i = 0; i < parameters; i++) {
10354                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10355                         atype = param->left;
10356                 } else {
10357                         atype = param;
10358                 }
10359                 arg = flatten(state, first, variable(state, atype));
10360                 param = param->right;
10361         }
10362         work = new_triple(state, op, rtype, -1, parameters);
10363         generate_lhs_pieces(state, work);
10364         for(i = 0; i < parameters; i++) {
10365                 RHS(work, i) = read_expr(state, farg(state, def, i));
10366         }
10367         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10368                 work = write_expr(state, deref_index(state, result, 1), work);
10369         }
10370         work = flatten(state, first, work);
10371         last = flatten(state, first, label(state));
10372         ret  = flatten(state, first, ret);
10373         name_len = strlen(name);
10374         ident = lookup(state, name, name_len);
10375         ftype->type_ident = ident;
10376         symbol(state, ident, &ident->sym_ident, def, ftype);
10377         
10378         state->file = file.prev;
10379         state->function = 0;
10380         state->main_function = 0;
10381
10382         if (!state->functions) {
10383                 state->functions = def;
10384         } else {
10385                 insert_triple(state, state->functions, def);
10386         }
10387         if (state->compiler->debug & DEBUG_INLINE) {
10388                 FILE *fp = state->dbgout;
10389                 fprintf(fp, "\n");
10390                 loc(fp, state, 0);
10391                 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10392                 display_func(state, fp, def);
10393                 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
10394         }
10395 }
10396
10397 static struct type *partial_struct(struct compile_state *state,
10398         const char *field_name, struct type *type, struct type *rest)
10399 {
10400         struct hash_entry *field_ident;
10401         struct type *result;
10402         int field_name_len;
10403
10404         field_name_len = strlen(field_name);
10405         field_ident = lookup(state, field_name, field_name_len);
10406
10407         result = clone_type(0, type);
10408         result->field_ident = field_ident;
10409
10410         if (rest) {
10411                 result = new_type(TYPE_PRODUCT, result, rest);
10412         }
10413         return result;
10414 }
10415
10416 static struct type *register_builtin_type(struct compile_state *state,
10417         const char *name, struct type *type)
10418 {
10419         struct hash_entry *ident;
10420         int name_len;
10421
10422         name_len = strlen(name);
10423         ident = lookup(state, name, name_len);
10424         
10425         if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10426                 ulong_t elements = 0;
10427                 struct type *field;
10428                 type = new_type(TYPE_STRUCT, type, 0);
10429                 field = type->left;
10430                 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10431                         elements++;
10432                         field = field->right;
10433                 }
10434                 elements++;
10435                 symbol(state, ident, &ident->sym_tag, 0, type);
10436                 type->type_ident = ident;
10437                 type->elements = elements;
10438         }
10439         symbol(state, ident, &ident->sym_ident, 0, type);
10440         ident->tok = TOK_TYPE_NAME;
10441         return type;
10442 }
10443
10444
10445 static void register_builtins(struct compile_state *state)
10446 {
10447         struct type *div_type, *ldiv_type;
10448         struct type *udiv_type, *uldiv_type;
10449         struct type *msr_type;
10450
10451         div_type = register_builtin_type(state, "__builtin_div_t",
10452                 partial_struct(state, "quot", &int_type,
10453                 partial_struct(state, "rem",  &int_type, 0)));
10454         ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10455                 partial_struct(state, "quot", &long_type,
10456                 partial_struct(state, "rem",  &long_type, 0)));
10457         udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10458                 partial_struct(state, "quot", &uint_type,
10459                 partial_struct(state, "rem",  &uint_type, 0)));
10460         uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10461                 partial_struct(state, "quot", &ulong_type,
10462                 partial_struct(state, "rem",  &ulong_type, 0)));
10463
10464         register_builtin_function(state, "__builtin_div",   OP_SDIVT, div_type,
10465                 &int_type, &int_type);
10466         register_builtin_function(state, "__builtin_ldiv",  OP_SDIVT, ldiv_type,
10467                 &long_type, &long_type);
10468         register_builtin_function(state, "__builtin_udiv",  OP_UDIVT, udiv_type,
10469                 &uint_type, &uint_type);
10470         register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10471                 &ulong_type, &ulong_type);
10472
10473         register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type, 
10474                 &ushort_type);
10475         register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10476                 &ushort_type);
10477         register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,   
10478                 &ushort_type);
10479
10480         register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type, 
10481                 &uchar_type, &ushort_type);
10482         register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type, 
10483                 &ushort_type, &ushort_type);
10484         register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type, 
10485                 &uint_type, &ushort_type);
10486         
10487         register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type, 
10488                 &int_type);
10489         register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type, 
10490                 &int_type);
10491
10492         msr_type = register_builtin_type(state, "__builtin_msr_t",
10493                 partial_struct(state, "lo", &ulong_type,
10494                 partial_struct(state, "hi", &ulong_type, 0)));
10495
10496         register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10497                 &ulong_type);
10498         register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10499                 &ulong_type, &ulong_type, &ulong_type);
10500         
10501         register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type, 
10502                 &void_type);
10503 }
10504
10505 static struct type *declarator(
10506         struct compile_state *state, struct type *type, 
10507         struct hash_entry **ident, int need_ident);
10508 static void decl(struct compile_state *state, struct triple *first);
10509 static struct type *specifier_qualifier_list(struct compile_state *state);
10510 static int isdecl_specifier(int tok);
10511 static struct type *decl_specifiers(struct compile_state *state);
10512 static int istype(int tok);
10513 static struct triple *expr(struct compile_state *state);
10514 static struct triple *assignment_expr(struct compile_state *state);
10515 static struct type *type_name(struct compile_state *state);
10516 static void statement(struct compile_state *state, struct triple *first);
10517
10518 static struct triple *call_expr(
10519         struct compile_state *state, struct triple *func)
10520 {
10521         struct triple *def;
10522         struct type *param, *type;
10523         ulong_t pvals, index;
10524
10525         if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10526                 error(state, 0, "Called object is not a function");
10527         }
10528         if (func->op != OP_LIST) {
10529                 internal_error(state, 0, "improper function");
10530         }
10531         eat(state, TOK_LPAREN);
10532         /* Find the return type without any specifiers */
10533         type = clone_type(0, func->type->left);
10534         /* Count the number of rhs entries for OP_FCALL */
10535         param = func->type->right;
10536         pvals = 0;
10537         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10538                 pvals++;
10539                 param = param->right;
10540         }
10541         if ((param->type & TYPE_MASK) != TYPE_VOID) {
10542                 pvals++;
10543         }
10544         def = new_triple(state, OP_FCALL, type, -1, pvals);
10545         MISC(def, 0) = func;
10546
10547         param = func->type->right;
10548         for(index = 0; index < pvals; index++) {
10549                 struct triple *val;
10550                 struct type *arg_type;
10551                 val = read_expr(state, assignment_expr(state));
10552                 arg_type = param;
10553                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10554                         arg_type = param->left;
10555                 }
10556                 write_compatible(state, arg_type, val->type);
10557                 RHS(def, index) = val;
10558                 if (index != (pvals - 1)) {
10559                         eat(state, TOK_COMMA);
10560                         param = param->right;
10561                 }
10562         }
10563         eat(state, TOK_RPAREN);
10564         return def;
10565 }
10566
10567
10568 static struct triple *character_constant(struct compile_state *state)
10569 {
10570         struct triple *def;
10571         struct token *tk;
10572         const signed char *str, *end;
10573         int c;
10574         int str_len;
10575         tk = eat(state, TOK_LIT_CHAR);
10576         str = tk->val.str + 1;
10577         str_len = tk->str_len - 2;
10578         if (str_len <= 0) {
10579                 error(state, 0, "empty character constant");
10580         }
10581         end = str + str_len;
10582         c = char_value(state, &str, end);
10583         if (str != end) {
10584                 error(state, 0, "multibyte character constant not supported");
10585         }
10586         def = int_const(state, &char_type, (ulong_t)((long_t)c));
10587         return def;
10588 }
10589
10590 static struct triple *string_constant(struct compile_state *state)
10591 {
10592         struct triple *def;
10593         struct token *tk;
10594         struct type *type;
10595         const signed char *str, *end;
10596         signed char *buf, *ptr;
10597         int str_len;
10598
10599         buf = 0;
10600         type = new_type(TYPE_ARRAY, &char_type, 0);
10601         type->elements = 0;
10602         /* The while loop handles string concatenation */
10603         do {
10604                 tk = eat(state, TOK_LIT_STRING);
10605                 str = tk->val.str + 1;
10606                 str_len = tk->str_len - 2;
10607                 if (str_len < 0) {
10608                         error(state, 0, "negative string constant length");
10609                 }
10610                 end = str + str_len;
10611                 ptr = buf;
10612                 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10613                 memcpy(buf, ptr, type->elements);
10614                 ptr = buf + type->elements;
10615                 do {
10616                         *ptr++ = char_value(state, &str, end);
10617                 } while(str < end);
10618                 type->elements = ptr - buf;
10619         } while(peek(state) == TOK_LIT_STRING);
10620         *ptr = '\0';
10621         type->elements += 1;
10622         def = triple(state, OP_BLOBCONST, type, 0, 0);
10623         def->u.blob = buf;
10624
10625         return def;
10626 }
10627
10628
10629 static struct triple *integer_constant(struct compile_state *state)
10630 {
10631         struct triple *def;
10632         unsigned long val;
10633         struct token *tk;
10634         char *end;
10635         int u, l, decimal;
10636         struct type *type;
10637
10638         tk = eat(state, TOK_LIT_INT);
10639         errno = 0;
10640         decimal = (tk->val.str[0] != '0');
10641         val = strtoul(tk->val.str, &end, 0);
10642         if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
10643                 error(state, 0, "Integer constant to large");
10644         }
10645         u = l = 0;
10646         if ((*end == 'u') || (*end == 'U')) {
10647                 u = 1;
10648                         end++;
10649         }
10650         if ((*end == 'l') || (*end == 'L')) {
10651                 l = 1;
10652                 end++;
10653         }
10654         if ((*end == 'u') || (*end == 'U')) {
10655                 u = 1;
10656                 end++;
10657         }
10658         if (*end) {
10659                 error(state, 0, "Junk at end of integer constant");
10660         }
10661         if (u && l)  {
10662                 type = &ulong_type;
10663         }
10664         else if (l) {
10665                 type = &long_type;
10666                 if (!decimal && (val > LONG_T_MAX)) {
10667                         type = &ulong_type;
10668                 }
10669         }
10670         else if (u) {
10671                 type = &uint_type;
10672                 if (val > UINT_T_MAX) {
10673                         type = &ulong_type;
10674                 }
10675         }
10676         else {
10677                 type = &int_type;
10678                 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
10679                         type = &uint_type;
10680                 }
10681                 else if (!decimal && (val > LONG_T_MAX)) {
10682                         type = &ulong_type;
10683                 }
10684                 else if (val > INT_T_MAX) {
10685                         type = &long_type;
10686                 }
10687         }
10688         def = int_const(state, type, val);
10689         return def;
10690 }
10691
10692 static struct triple *primary_expr(struct compile_state *state)
10693 {
10694         struct triple *def;
10695         int tok;
10696         tok = peek(state);
10697         switch(tok) {
10698         case TOK_IDENT:
10699         {
10700                 struct hash_entry *ident;
10701                 /* Here ident is either:
10702                  * a varable name
10703                  * a function name
10704                  */
10705                 ident = eat(state, TOK_IDENT)->ident;
10706                 if (!ident->sym_ident) {
10707                         error(state, 0, "%s undeclared", ident->name);
10708                 }
10709                 def = ident->sym_ident->def;
10710                 break;
10711         }
10712         case TOK_ENUM_CONST:
10713         {
10714                 struct hash_entry *ident;
10715                 /* Here ident is an enumeration constant */
10716                 ident = eat(state, TOK_ENUM_CONST)->ident;
10717                 if (!ident->sym_ident) {
10718                         error(state, 0, "%s undeclared", ident->name);
10719                 }
10720                 def = ident->sym_ident->def;
10721                 break;
10722         }
10723         case TOK_MIDENT:
10724         {
10725                 struct hash_entry *ident;
10726                 ident = eat(state, TOK_MIDENT)->ident;
10727                 warning(state, 0, "Replacing undefined macro: %s with 0",
10728                         ident->name);
10729                 def = int_const(state, &int_type, 0);
10730                 break;
10731         }
10732         case TOK_LPAREN:
10733                 eat(state, TOK_LPAREN);
10734                 def = expr(state);
10735                 eat(state, TOK_RPAREN);
10736                 break;
10737         case TOK_LIT_INT:
10738                 def = integer_constant(state);
10739                 break;
10740         case TOK_LIT_FLOAT:
10741                 eat(state, TOK_LIT_FLOAT);
10742                 error(state, 0, "Floating point constants not supported");
10743                 def = 0;
10744                 FINISHME();
10745                 break;
10746         case TOK_LIT_CHAR:
10747                 def = character_constant(state);
10748                 break;
10749         case TOK_LIT_STRING:
10750                 def = string_constant(state);
10751                 break;
10752         default:
10753                 def = 0;
10754                 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10755         }
10756         return def;
10757 }
10758
10759 static struct triple *postfix_expr(struct compile_state *state)
10760 {
10761         struct triple *def;
10762         int postfix;
10763         def = primary_expr(state);
10764         do {
10765                 struct triple *left;
10766                 int tok;
10767                 postfix = 1;
10768                 left = def;
10769                 switch((tok = peek(state))) {
10770                 case TOK_LBRACKET:
10771                         eat(state, TOK_LBRACKET);
10772                         def = mk_subscript_expr(state, left, expr(state));
10773                         eat(state, TOK_RBRACKET);
10774                         break;
10775                 case TOK_LPAREN:
10776                         def = call_expr(state, def);
10777                         break;
10778                 case TOK_DOT:
10779                 {
10780                         struct hash_entry *field;
10781                         eat(state, TOK_DOT);
10782                         field = eat(state, TOK_IDENT)->ident;
10783                         def = deref_field(state, def, field);
10784                         break;
10785                 }
10786                 case TOK_ARROW:
10787                 {
10788                         struct hash_entry *field;
10789                         eat(state, TOK_ARROW);
10790                         field = eat(state, TOK_IDENT)->ident;
10791                         def = mk_deref_expr(state, read_expr(state, def));
10792                         def = deref_field(state, def, field);
10793                         break;
10794                 }
10795                 case TOK_PLUSPLUS:
10796                         eat(state, TOK_PLUSPLUS);
10797                         def = mk_post_inc_expr(state, left);
10798                         break;
10799                 case TOK_MINUSMINUS:
10800                         eat(state, TOK_MINUSMINUS);
10801                         def = mk_post_dec_expr(state, left);
10802                         break;
10803                 default:
10804                         postfix = 0;
10805                         break;
10806                 }
10807         } while(postfix);
10808         return def;
10809 }
10810
10811 static struct triple *cast_expr(struct compile_state *state);
10812
10813 static struct triple *unary_expr(struct compile_state *state)
10814 {
10815         struct triple *def, *right;
10816         int tok;
10817         switch((tok = peek(state))) {
10818         case TOK_PLUSPLUS:
10819                 eat(state, TOK_PLUSPLUS);
10820                 def = mk_pre_inc_expr(state, unary_expr(state));
10821                 break;
10822         case TOK_MINUSMINUS:
10823                 eat(state, TOK_MINUSMINUS);
10824                 def = mk_pre_dec_expr(state, unary_expr(state));
10825                 break;
10826         case TOK_AND:
10827                 eat(state, TOK_AND);
10828                 def = mk_addr_expr(state, cast_expr(state), 0);
10829                 break;
10830         case TOK_STAR:
10831                 eat(state, TOK_STAR);
10832                 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
10833                 break;
10834         case TOK_PLUS:
10835                 eat(state, TOK_PLUS);
10836                 right = read_expr(state, cast_expr(state));
10837                 arithmetic(state, right);
10838                 def = integral_promotion(state, right);
10839                 break;
10840         case TOK_MINUS:
10841                 eat(state, TOK_MINUS);
10842                 right = read_expr(state, cast_expr(state));
10843                 arithmetic(state, right);
10844                 def = integral_promotion(state, right);
10845                 def = triple(state, OP_NEG, def->type, def, 0);
10846                 break;
10847         case TOK_TILDE:
10848                 eat(state, TOK_TILDE);
10849                 right = read_expr(state, cast_expr(state));
10850                 integral(state, right);
10851                 def = integral_promotion(state, right);
10852                 def = triple(state, OP_INVERT, def->type, def, 0);
10853                 break;
10854         case TOK_BANG:
10855                 eat(state, TOK_BANG);
10856                 right = read_expr(state, cast_expr(state));
10857                 bool(state, right);
10858                 def = lfalse_expr(state, right);
10859                 break;
10860         case TOK_SIZEOF:
10861         {
10862                 struct type *type;
10863                 int tok1, tok2;
10864                 eat(state, TOK_SIZEOF);
10865                 tok1 = peek(state);
10866                 tok2 = peek2(state);
10867                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
10868                         eat(state, TOK_LPAREN);
10869                         type = type_name(state);
10870                         eat(state, TOK_RPAREN);
10871                 }
10872                 else {
10873                         struct triple *expr;
10874                         expr = unary_expr(state);
10875                         type = expr->type;
10876                         release_expr(state, expr);
10877                 }
10878                 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
10879                 break;
10880         }
10881         case TOK_ALIGNOF:
10882         {
10883                 struct type *type;
10884                 int tok1, tok2;
10885                 eat(state, TOK_ALIGNOF);
10886                 tok1 = peek(state);
10887                 tok2 = peek2(state);
10888                 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
10889                         eat(state, TOK_LPAREN);
10890                         type = type_name(state);
10891                         eat(state, TOK_RPAREN);
10892                 }
10893                 else {
10894                         struct triple *expr;
10895                         expr = unary_expr(state);
10896                         type = expr->type;
10897                         release_expr(state, expr);
10898                 }
10899                 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
10900                 break;
10901         }
10902         case TOK_MDEFINED:
10903         {
10904                 /* We only come here if we are called from the preprocessor */
10905                 struct hash_entry *ident;
10906                 int parens;
10907                 eat(state, TOK_MDEFINED);
10908                 parens = 0;
10909                 if (cpp_peek(state) == TOK_LPAREN) {
10910                         cpp_eat(state, TOK_LPAREN);
10911                         parens = 1;
10912                 }
10913                 ident = cpp_eat(state, TOK_MIDENT)->ident;
10914                 if (parens) {
10915                         eat(state, TOK_RPAREN);
10916                 }
10917                 def = int_const(state, &int_type, ident->sym_define != 0);
10918                 break;
10919         }
10920         default:
10921                 def = postfix_expr(state);
10922                 break;
10923         }
10924         return def;
10925 }
10926
10927 static struct triple *cast_expr(struct compile_state *state)
10928 {
10929         struct triple *def;
10930         int tok1, tok2;
10931         tok1 = peek(state);
10932         tok2 = peek2(state);
10933         if ((tok1 == TOK_LPAREN) && istype(tok2)) {
10934                 struct type *type;
10935                 eat(state, TOK_LPAREN);
10936                 type = type_name(state);
10937                 eat(state, TOK_RPAREN);
10938                 def = mk_cast_expr(state, type, cast_expr(state));
10939         }
10940         else {
10941                 def = unary_expr(state);
10942         }
10943         return def;
10944 }
10945
10946 static struct triple *mult_expr(struct compile_state *state)
10947 {
10948         struct triple *def;
10949         int done;
10950         def = cast_expr(state);
10951         do {
10952                 struct triple *left, *right;
10953                 struct type *result_type;
10954                 int tok, op, sign;
10955                 done = 0;
10956                 switch(tok = (peek(state))) {
10957                 case TOK_STAR:
10958                 case TOK_DIV:
10959                 case TOK_MOD:
10960                         left = read_expr(state, def);
10961                         arithmetic(state, left);
10962
10963                         eat(state, tok);
10964
10965                         right = read_expr(state, cast_expr(state));
10966                         arithmetic(state, right);
10967
10968                         result_type = arithmetic_result(state, left, right);
10969                         sign = is_signed(result_type);
10970                         op = -1;
10971                         switch(tok) {
10972                         case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
10973                         case TOK_DIV:  op = sign? OP_SDIV : OP_UDIV; break;
10974                         case TOK_MOD:  op = sign? OP_SMOD : OP_UMOD; break;
10975                         }
10976                         def = triple(state, op, result_type, left, right);
10977                         break;
10978                 default:
10979                         done = 1;
10980                         break;
10981                 }
10982         } while(!done);
10983         return def;
10984 }
10985
10986 static struct triple *add_expr(struct compile_state *state)
10987 {
10988         struct triple *def;
10989         int done;
10990         def = mult_expr(state);
10991         do {
10992                 done = 0;
10993                 switch( peek(state)) {
10994                 case TOK_PLUS:
10995                         eat(state, TOK_PLUS);
10996                         def = mk_add_expr(state, def, mult_expr(state));
10997                         break;
10998                 case TOK_MINUS:
10999                         eat(state, TOK_MINUS);
11000                         def = mk_sub_expr(state, def, mult_expr(state));
11001                         break;
11002                 default:
11003                         done = 1;
11004                         break;
11005                 }
11006         } while(!done);
11007         return def;
11008 }
11009
11010 static struct triple *shift_expr(struct compile_state *state)
11011 {
11012         struct triple *def;
11013         int done;
11014         def = add_expr(state);
11015         do {
11016                 struct triple *left, *right;
11017                 int tok, op;
11018                 done = 0;
11019                 switch((tok = peek(state))) {
11020                 case TOK_SL:
11021                 case TOK_SR:
11022                         left = read_expr(state, def);
11023                         integral(state, left);
11024                         left = integral_promotion(state, left);
11025
11026                         eat(state, tok);
11027
11028                         right = read_expr(state, add_expr(state));
11029                         integral(state, right);
11030                         right = integral_promotion(state, right);
11031                         
11032                         op = (tok == TOK_SL)? OP_SL : 
11033                                 is_signed(left->type)? OP_SSR: OP_USR;
11034
11035                         def = triple(state, op, left->type, left, right);
11036                         break;
11037                 default:
11038                         done = 1;
11039                         break;
11040                 }
11041         } while(!done);
11042         return def;
11043 }
11044
11045 static struct triple *relational_expr(struct compile_state *state)
11046 {
11047 #warning "Extend relational exprs to work on more than arithmetic types"
11048         struct triple *def;
11049         int done;
11050         def = shift_expr(state);
11051         do {
11052                 struct triple *left, *right;
11053                 struct type *arg_type;
11054                 int tok, op, sign;
11055                 done = 0;
11056                 switch((tok = peek(state))) {
11057                 case TOK_LESS:
11058                 case TOK_MORE:
11059                 case TOK_LESSEQ:
11060                 case TOK_MOREEQ:
11061                         left = read_expr(state, def);
11062                         arithmetic(state, left);
11063
11064                         eat(state, tok);
11065
11066                         right = read_expr(state, shift_expr(state));
11067                         arithmetic(state, right);
11068
11069                         arg_type = arithmetic_result(state, left, right);
11070                         sign = is_signed(arg_type);
11071                         op = -1;
11072                         switch(tok) {
11073                         case TOK_LESS:   op = sign? OP_SLESS : OP_ULESS; break;
11074                         case TOK_MORE:   op = sign? OP_SMORE : OP_UMORE; break;
11075                         case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11076                         case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11077                         }
11078                         def = triple(state, op, &int_type, left, right);
11079                         break;
11080                 default:
11081                         done = 1;
11082                         break;
11083                 }
11084         } while(!done);
11085         return def;
11086 }
11087
11088 static struct triple *equality_expr(struct compile_state *state)
11089 {
11090 #warning "Extend equality exprs to work on more than arithmetic types"
11091         struct triple *def;
11092         int done;
11093         def = relational_expr(state);
11094         do {
11095                 struct triple *left, *right;
11096                 int tok, op;
11097                 done = 0;
11098                 switch((tok = peek(state))) {
11099                 case TOK_EQEQ:
11100                 case TOK_NOTEQ:
11101                         left = read_expr(state, def);
11102                         arithmetic(state, left);
11103                         eat(state, tok);
11104                         right = read_expr(state, relational_expr(state));
11105                         arithmetic(state, right);
11106                         op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11107                         def = triple(state, op, &int_type, left, right);
11108                         break;
11109                 default:
11110                         done = 1;
11111                         break;
11112                 }
11113         } while(!done);
11114         return def;
11115 }
11116
11117 static struct triple *and_expr(struct compile_state *state)
11118 {
11119         struct triple *def;
11120         def = equality_expr(state);
11121         while(peek(state) == TOK_AND) {
11122                 struct triple *left, *right;
11123                 struct type *result_type;
11124                 left = read_expr(state, def);
11125                 integral(state, left);
11126                 eat(state, TOK_AND);
11127                 right = read_expr(state, equality_expr(state));
11128                 integral(state, right);
11129                 result_type = arithmetic_result(state, left, right);
11130                 def = triple(state, OP_AND, result_type, left, right);
11131         }
11132         return def;
11133 }
11134
11135 static struct triple *xor_expr(struct compile_state *state)
11136 {
11137         struct triple *def;
11138         def = and_expr(state);
11139         while(peek(state) == TOK_XOR) {
11140                 struct triple *left, *right;
11141                 struct type *result_type;
11142                 left = read_expr(state, def);
11143                 integral(state, left);
11144                 eat(state, TOK_XOR);
11145                 right = read_expr(state, and_expr(state));
11146                 integral(state, right);
11147                 result_type = arithmetic_result(state, left, right);
11148                 def = triple(state, OP_XOR, result_type, left, right);
11149         }
11150         return def;
11151 }
11152
11153 static struct triple *or_expr(struct compile_state *state)
11154 {
11155         struct triple *def;
11156         def = xor_expr(state);
11157         while(peek(state) == TOK_OR) {
11158                 struct triple *left, *right;
11159                 struct type *result_type;
11160                 left = read_expr(state, def);
11161                 integral(state, left);
11162                 eat(state, TOK_OR);
11163                 right = read_expr(state, xor_expr(state));
11164                 integral(state, right);
11165                 result_type = arithmetic_result(state, left, right);
11166                 def = triple(state, OP_OR, result_type, left, right);
11167         }
11168         return def;
11169 }
11170
11171 static struct triple *land_expr(struct compile_state *state)
11172 {
11173         struct triple *def;
11174         def = or_expr(state);
11175         while(peek(state) == TOK_LOGAND) {
11176                 struct triple *left, *right;
11177                 left = read_expr(state, def);
11178                 bool(state, left);
11179                 eat(state, TOK_LOGAND);
11180                 right = read_expr(state, or_expr(state));
11181                 bool(state, right);
11182
11183                 def = mkland_expr(state,
11184                         ltrue_expr(state, left),
11185                         ltrue_expr(state, right));
11186         }
11187         return def;
11188 }
11189
11190 static struct triple *lor_expr(struct compile_state *state)
11191 {
11192         struct triple *def;
11193         def = land_expr(state);
11194         while(peek(state) == TOK_LOGOR) {
11195                 struct triple *left, *right;
11196                 left = read_expr(state, def);
11197                 bool(state, left);
11198                 eat(state, TOK_LOGOR);
11199                 right = read_expr(state, land_expr(state));
11200                 bool(state, right);
11201
11202                 def = mklor_expr(state, 
11203                         ltrue_expr(state, left),
11204                         ltrue_expr(state, right));
11205         }
11206         return def;
11207 }
11208
11209 static struct triple *conditional_expr(struct compile_state *state)
11210 {
11211         struct triple *def;
11212         def = lor_expr(state);
11213         if (peek(state) == TOK_QUEST) {
11214                 struct triple *test, *left, *right;
11215                 bool(state, def);
11216                 test = ltrue_expr(state, read_expr(state, def));
11217                 eat(state, TOK_QUEST);
11218                 left = read_expr(state, expr(state));
11219                 eat(state, TOK_COLON);
11220                 right = read_expr(state, conditional_expr(state));
11221
11222                 def = mkcond_expr(state, test, left, right);
11223         }
11224         return def;
11225 }
11226
11227 struct cv_triple {
11228         struct triple *val;
11229         int id;
11230 };
11231
11232 static void set_cv(struct compile_state *state, struct cv_triple *cv,
11233         struct triple *dest, struct triple *val)
11234 {
11235         if (cv[dest->id].val) {
11236                 free_triple(state, cv[dest->id].val);
11237         }
11238         cv[dest->id].val = val;
11239 }
11240 static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11241         struct triple *src)
11242 {
11243         return cv[src->id].val;
11244 }
11245
11246 static struct triple *eval_const_expr(
11247         struct compile_state *state, struct triple *expr)
11248 {
11249         struct triple *def;
11250         if (is_const(expr)) {
11251                 def = expr;
11252         }
11253         else {
11254                 /* If we don't start out as a constant simplify into one */
11255                 struct triple *head, *ptr;
11256                 struct cv_triple *cv;
11257                 int i, count;
11258                 head = label(state); /* dummy initial triple */
11259                 flatten(state, head, expr);
11260                 count = 1;
11261                 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11262                         count++;
11263                 }
11264                 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11265                 i = 1;
11266                 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11267                         cv[i].val = 0;
11268                         cv[i].id  = ptr->id;
11269                         ptr->id   = i;
11270                         i++;
11271                 }
11272                 ptr = head->next;
11273                 do {
11274                         valid_ins(state, ptr);
11275                         if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
11276                                 internal_error(state, ptr, 
11277                                         "unexpected %s in constant expression",
11278                                         tops(ptr->op));
11279                         }
11280                         else if (ptr->op == OP_LIST) {
11281                         }
11282                         else if (triple_is_structural(state, ptr)) {
11283                                 ptr = ptr->next;
11284                         }
11285                         else if (triple_is_ubranch(state, ptr)) {
11286                                 ptr = TARG(ptr, 0);
11287                         }
11288                         else if (triple_is_cbranch(state, ptr)) {
11289                                 struct triple *cond_val;
11290                                 cond_val = get_cv(state, cv, RHS(ptr, 0));
11291                                 if (!cond_val || !is_const(cond_val) || 
11292                                         (cond_val->op != OP_INTCONST)) 
11293                                 {
11294                                         internal_error(state, ptr, "bad branch condition");
11295                                 }
11296                                 if (cond_val->u.cval == 0) {
11297                                         ptr = ptr->next;
11298                                 } else {
11299                                         ptr = TARG(ptr, 0);
11300                                 }
11301                         }
11302                         else if (triple_is_branch(state, ptr)) {
11303                                 error(state, ptr, "bad branch type in constant expression");
11304                         }
11305                         else if (ptr->op == OP_WRITE) {
11306                                 struct triple *val;
11307                                 val = get_cv(state, cv, RHS(ptr, 0));
11308                                 
11309                                 set_cv(state, cv, MISC(ptr, 0), 
11310                                         copy_triple(state, val));
11311                                 set_cv(state, cv, ptr, 
11312                                         copy_triple(state, val));
11313                                 ptr = ptr->next;
11314                         }
11315                         else if (ptr->op == OP_READ) {
11316                                 set_cv(state, cv, ptr, 
11317                                         copy_triple(state, 
11318                                                 get_cv(state, cv, RHS(ptr, 0))));
11319                                 ptr = ptr->next;
11320                         }
11321                         else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11322                                 struct triple *val, **rhs;
11323                                 val = copy_triple(state, ptr);
11324                                 rhs = triple_rhs(state, val, 0);
11325                                 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11326                                         if (!*rhs) {
11327                                                 internal_error(state, ptr, "Missing rhs");
11328                                         }
11329                                         *rhs = get_cv(state, cv, *rhs);
11330                                 }
11331                                 simplify(state, val);
11332                                 set_cv(state, cv, ptr, val);
11333                                 ptr = ptr->next;
11334                         }
11335                         else {
11336                                 error(state, ptr, "impure operation in constant expression");
11337                         }
11338                         
11339                 } while(ptr != head);
11340
11341                 /* Get the result value */
11342                 def = get_cv(state, cv, head->prev);
11343                 cv[head->prev->id].val = 0;
11344
11345                 /* Free the temporary values */
11346                 for(i = 0; i < count; i++) {
11347                         if (cv[i].val) {
11348                                 free_triple(state, cv[i].val);
11349                                 cv[i].val = 0;
11350                         }
11351                 }
11352                 xfree(cv);
11353                 /* Free the intermediate expressions */
11354                 while(head->next != head) {
11355                         release_triple(state, head->next);
11356                 }
11357                 free_triple(state, head);
11358         }
11359         if (!is_const(def)) {
11360                 error(state, expr, "Not a constant expression");
11361         }
11362         return def;
11363 }
11364
11365 static struct triple *constant_expr(struct compile_state *state)
11366 {
11367         return eval_const_expr(state, conditional_expr(state));
11368 }
11369
11370 static struct triple *assignment_expr(struct compile_state *state)
11371 {
11372         struct triple *def, *left, *right;
11373         int tok, op, sign;
11374         /* The C grammer in K&R shows assignment expressions
11375          * only taking unary expressions as input on their
11376          * left hand side.  But specifies the precedence of
11377          * assignemnt as the lowest operator except for comma.
11378          *
11379          * Allowing conditional expressions on the left hand side
11380          * of an assignement results in a grammar that accepts
11381          * a larger set of statements than standard C.   As long
11382          * as the subset of the grammar that is standard C behaves
11383          * correctly this should cause no problems.
11384          * 
11385          * For the extra token strings accepted by the grammar
11386          * none of them should produce a valid lvalue, so they
11387          * should not produce functioning programs.
11388          *
11389          * GCC has this bug as well, so surprises should be minimal.
11390          */
11391         def = conditional_expr(state);
11392         left = def;
11393         switch((tok = peek(state))) {
11394         case TOK_EQ:
11395                 lvalue(state, left);
11396                 eat(state, TOK_EQ);
11397                 def = write_expr(state, left, 
11398                         read_expr(state, assignment_expr(state)));
11399                 break;
11400         case TOK_TIMESEQ:
11401         case TOK_DIVEQ:
11402         case TOK_MODEQ:
11403                 lvalue(state, left);
11404                 arithmetic(state, left);
11405                 eat(state, tok);
11406                 right = read_expr(state, assignment_expr(state));
11407                 arithmetic(state, right);
11408
11409                 sign = is_signed(left->type);
11410                 op = -1;
11411                 switch(tok) {
11412                 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11413                 case TOK_DIVEQ:   op = sign? OP_SDIV : OP_UDIV; break;
11414                 case TOK_MODEQ:   op = sign? OP_SMOD : OP_UMOD; break;
11415                 }
11416                 def = write_expr(state, left,
11417                         triple(state, op, left->type, 
11418                                 read_expr(state, left), right));
11419                 break;
11420         case TOK_PLUSEQ:
11421                 lvalue(state, left);
11422                 eat(state, TOK_PLUSEQ);
11423                 def = write_expr(state, left,
11424                         mk_add_expr(state, left, assignment_expr(state)));
11425                 break;
11426         case TOK_MINUSEQ:
11427                 lvalue(state, left);
11428                 eat(state, TOK_MINUSEQ);
11429                 def = write_expr(state, left,
11430                         mk_sub_expr(state, left, assignment_expr(state)));
11431                 break;
11432         case TOK_SLEQ:
11433         case TOK_SREQ:
11434         case TOK_ANDEQ:
11435         case TOK_XOREQ:
11436         case TOK_OREQ:
11437                 lvalue(state, left);
11438                 integral(state, left);
11439                 eat(state, tok);
11440                 right = read_expr(state, assignment_expr(state));
11441                 integral(state, right);
11442                 right = integral_promotion(state, right);
11443                 sign = is_signed(left->type);
11444                 op = -1;
11445                 switch(tok) {
11446                 case TOK_SLEQ:  op = OP_SL; break;
11447                 case TOK_SREQ:  op = sign? OP_SSR: OP_USR; break;
11448                 case TOK_ANDEQ: op = OP_AND; break;
11449                 case TOK_XOREQ: op = OP_XOR; break;
11450                 case TOK_OREQ:  op = OP_OR; break;
11451                 }
11452                 def = write_expr(state, left,
11453                         triple(state, op, left->type, 
11454                                 read_expr(state, left), right));
11455                 break;
11456         }
11457         return def;
11458 }
11459
11460 static struct triple *expr(struct compile_state *state)
11461 {
11462         struct triple *def;
11463         def = assignment_expr(state);
11464         while(peek(state) == TOK_COMMA) {
11465                 eat(state, TOK_COMMA);
11466                 def = mkprog(state, def, assignment_expr(state), 0);
11467         }
11468         return def;
11469 }
11470
11471 static void expr_statement(struct compile_state *state, struct triple *first)
11472 {
11473         if (peek(state) != TOK_SEMI) {
11474                 /* lvalue conversions always apply except when certian operators
11475                  * are applied.  I apply the lvalue conversions here
11476                  * as I know no more operators will be applied.
11477                  */
11478                 flatten(state, first, lvalue_conversion(state, expr(state)));
11479         }
11480         eat(state, TOK_SEMI);
11481 }
11482
11483 static void if_statement(struct compile_state *state, struct triple *first)
11484 {
11485         struct triple *test, *jmp1, *jmp2, *middle, *end;
11486
11487         jmp1 = jmp2 = middle = 0;
11488         eat(state, TOK_IF);
11489         eat(state, TOK_LPAREN);
11490         test = expr(state);
11491         bool(state, test);
11492         /* Cleanup and invert the test */
11493         test = lfalse_expr(state, read_expr(state, test));
11494         eat(state, TOK_RPAREN);
11495         /* Generate the needed pieces */
11496         middle = label(state);
11497         jmp1 = branch(state, middle, test);
11498         /* Thread the pieces together */
11499         flatten(state, first, test);
11500         flatten(state, first, jmp1);
11501         flatten(state, first, label(state));
11502         statement(state, first);
11503         if (peek(state) == TOK_ELSE) {
11504                 eat(state, TOK_ELSE);
11505                 /* Generate the rest of the pieces */
11506                 end = label(state);
11507                 jmp2 = branch(state, end, 0);
11508                 /* Thread them together */
11509                 flatten(state, first, jmp2);
11510                 flatten(state, first, middle);
11511                 statement(state, first);
11512                 flatten(state, first, end);
11513         }
11514         else {
11515                 flatten(state, first, middle);
11516         }
11517 }
11518
11519 static void for_statement(struct compile_state *state, struct triple *first)
11520 {
11521         struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11522         struct triple *label1, *label2, *label3;
11523         struct hash_entry *ident;
11524
11525         eat(state, TOK_FOR);
11526         eat(state, TOK_LPAREN);
11527         head = test = tail = jmp1 = jmp2 = 0;
11528         if (peek(state) != TOK_SEMI) {
11529                 head = expr(state);
11530         } 
11531         eat(state, TOK_SEMI);
11532         if (peek(state) != TOK_SEMI) {
11533                 test = expr(state);
11534                 bool(state, test);
11535                 test = ltrue_expr(state, read_expr(state, test));
11536         }
11537         eat(state, TOK_SEMI);
11538         if (peek(state) != TOK_RPAREN) {
11539                 tail = expr(state);
11540         }
11541         eat(state, TOK_RPAREN);
11542         /* Generate the needed pieces */
11543         label1 = label(state);
11544         label2 = label(state);
11545         label3 = label(state);
11546         if (test) {
11547                 jmp1 = branch(state, label3, 0);
11548                 jmp2 = branch(state, label1, test);
11549         }
11550         else {
11551                 jmp2 = branch(state, label1, 0);
11552         }
11553         end = label(state);
11554         /* Remember where break and continue go */
11555         start_scope(state);
11556         ident = state->i_break;
11557         symbol(state, ident, &ident->sym_ident, end, end->type);
11558         ident = state->i_continue;
11559         symbol(state, ident, &ident->sym_ident, label2, label2->type);
11560         /* Now include the body */
11561         flatten(state, first, head);
11562         flatten(state, first, jmp1);
11563         flatten(state, first, label1);
11564         statement(state, first);
11565         flatten(state, first, label2);
11566         flatten(state, first, tail);
11567         flatten(state, first, label3);
11568         flatten(state, first, test);
11569         flatten(state, first, jmp2);
11570         flatten(state, first, end);
11571         /* Cleanup the break/continue scope */
11572         end_scope(state);
11573 }
11574
11575 static void while_statement(struct compile_state *state, struct triple *first)
11576 {
11577         struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11578         struct hash_entry *ident;
11579         eat(state, TOK_WHILE);
11580         eat(state, TOK_LPAREN);
11581         test = expr(state);
11582         bool(state, test);
11583         test = ltrue_expr(state, read_expr(state, test));
11584         eat(state, TOK_RPAREN);
11585         /* Generate the needed pieces */
11586         label1 = label(state);
11587         label2 = label(state);
11588         jmp1 = branch(state, label2, 0);
11589         jmp2 = branch(state, label1, test);
11590         end = label(state);
11591         /* Remember where break and continue go */
11592         start_scope(state);
11593         ident = state->i_break;
11594         symbol(state, ident, &ident->sym_ident, end, end->type);
11595         ident = state->i_continue;
11596         symbol(state, ident, &ident->sym_ident, label2, label2->type);
11597         /* Thread them together */
11598         flatten(state, first, jmp1);
11599         flatten(state, first, label1);
11600         statement(state, first);
11601         flatten(state, first, label2);
11602         flatten(state, first, test);
11603         flatten(state, first, jmp2);
11604         flatten(state, first, end);
11605         /* Cleanup the break/continue scope */
11606         end_scope(state);
11607 }
11608
11609 static void do_statement(struct compile_state *state, struct triple *first)
11610 {
11611         struct triple *label1, *label2, *test, *end;
11612         struct hash_entry *ident;
11613         eat(state, TOK_DO);
11614         /* Generate the needed pieces */
11615         label1 = label(state);
11616         label2 = label(state);
11617         end = label(state);
11618         /* Remember where break and continue go */
11619         start_scope(state);
11620         ident = state->i_break;
11621         symbol(state, ident, &ident->sym_ident, end, end->type);
11622         ident = state->i_continue;
11623         symbol(state, ident, &ident->sym_ident, label2, label2->type);
11624         /* Now include the body */
11625         flatten(state, first, label1);
11626         statement(state, first);
11627         /* Cleanup the break/continue scope */
11628         end_scope(state);
11629         /* Eat the rest of the loop */
11630         eat(state, TOK_WHILE);
11631         eat(state, TOK_LPAREN);
11632         test = read_expr(state, expr(state));
11633         bool(state, test);
11634         eat(state, TOK_RPAREN);
11635         eat(state, TOK_SEMI);
11636         /* Thread the pieces together */
11637         test = ltrue_expr(state, test);
11638         flatten(state, first, label2);
11639         flatten(state, first, test);
11640         flatten(state, first, branch(state, label1, test));
11641         flatten(state, first, end);
11642 }
11643
11644
11645 static void return_statement(struct compile_state *state, struct triple *first)
11646 {
11647         struct triple *jmp, *mv, *dest, *var, *val;
11648         int last;
11649         eat(state, TOK_RETURN);
11650
11651 #warning "FIXME implement a more general excess branch elimination"
11652         val = 0;
11653         /* If we have a return value do some more work */
11654         if (peek(state) != TOK_SEMI) {
11655                 val = read_expr(state, expr(state));
11656         }
11657         eat(state, TOK_SEMI);
11658
11659         /* See if this last statement in a function */
11660         last = ((peek(state) == TOK_RBRACE) && 
11661                 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11662
11663         /* Find the return variable */
11664         var = fresult(state, state->main_function);
11665
11666         /* Find the return destination */
11667         dest = state->i_return->sym_ident->def;
11668         mv = jmp = 0;
11669         /* If needed generate a jump instruction */
11670         if (!last) {
11671                 jmp = branch(state, dest, 0);
11672         }
11673         /* If needed generate an assignment instruction */
11674         if (val) {
11675                 mv = write_expr(state, deref_index(state, var, 1), val);
11676         }
11677         /* Now put the code together */
11678         if (mv) {
11679                 flatten(state, first, mv);
11680                 flatten(state, first, jmp);
11681         }
11682         else if (jmp) {
11683                 flatten(state, first, jmp);
11684         }
11685 }
11686
11687 static void break_statement(struct compile_state *state, struct triple *first)
11688 {
11689         struct triple *dest;
11690         eat(state, TOK_BREAK);
11691         eat(state, TOK_SEMI);
11692         if (!state->i_break->sym_ident) {
11693                 error(state, 0, "break statement not within loop or switch");
11694         }
11695         dest = state->i_break->sym_ident->def;
11696         flatten(state, first, branch(state, dest, 0));
11697 }
11698
11699 static void continue_statement(struct compile_state *state, struct triple *first)
11700 {
11701         struct triple *dest;
11702         eat(state, TOK_CONTINUE);
11703         eat(state, TOK_SEMI);
11704         if (!state->i_continue->sym_ident) {
11705                 error(state, 0, "continue statement outside of a loop");
11706         }
11707         dest = state->i_continue->sym_ident->def;
11708         flatten(state, first, branch(state, dest, 0));
11709 }
11710
11711 static void goto_statement(struct compile_state *state, struct triple *first)
11712 {
11713         struct hash_entry *ident;
11714         eat(state, TOK_GOTO);
11715         ident = eat(state, TOK_IDENT)->ident;
11716         if (!ident->sym_label) {
11717                 /* If this is a forward branch allocate the label now,
11718                  * it will be flattend in the appropriate location later.
11719                  */
11720                 struct triple *ins;
11721                 ins = label(state);
11722                 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
11723         }
11724         eat(state, TOK_SEMI);
11725
11726         flatten(state, first, branch(state, ident->sym_label->def, 0));
11727 }
11728
11729 static void labeled_statement(struct compile_state *state, struct triple *first)
11730 {
11731         struct triple *ins;
11732         struct hash_entry *ident;
11733
11734         ident = eat(state, TOK_IDENT)->ident;
11735         if (ident->sym_label && ident->sym_label->def) {
11736                 ins = ident->sym_label->def;
11737                 put_occurance(ins->occurance);
11738                 ins->occurance = new_occurance(state);
11739         }
11740         else {
11741                 ins = label(state);
11742                 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
11743         }
11744         if (ins->id & TRIPLE_FLAG_FLATTENED) {
11745                 error(state, 0, "label %s already defined", ident->name);
11746         }
11747         flatten(state, first, ins);
11748
11749         eat(state, TOK_COLON);
11750         statement(state, first);
11751 }
11752
11753 static void switch_statement(struct compile_state *state, struct triple *first)
11754 {
11755         struct triple *value, *top, *end, *dbranch;
11756         struct hash_entry *ident;
11757
11758         /* See if we have a valid switch statement */
11759         eat(state, TOK_SWITCH);
11760         eat(state, TOK_LPAREN);
11761         value = expr(state);
11762         integral(state, value);
11763         value = read_expr(state, value);
11764         eat(state, TOK_RPAREN);
11765         /* Generate the needed pieces */
11766         top = label(state);
11767         end = label(state);
11768         dbranch = branch(state, end, 0);
11769         /* Remember where case branches and break goes */
11770         start_scope(state);
11771         ident = state->i_switch;
11772         symbol(state, ident, &ident->sym_ident, value, value->type);
11773         ident = state->i_case;
11774         symbol(state, ident, &ident->sym_ident, top, top->type);
11775         ident = state->i_break;
11776         symbol(state, ident, &ident->sym_ident, end, end->type);
11777         ident = state->i_default;
11778         symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11779         /* Thread them together */
11780         flatten(state, first, value);
11781         flatten(state, first, top);
11782         flatten(state, first, dbranch);
11783         statement(state, first);
11784         flatten(state, first, end);
11785         /* Cleanup the switch scope */
11786         end_scope(state);
11787 }
11788
11789 static void case_statement(struct compile_state *state, struct triple *first)
11790 {
11791         struct triple *cvalue, *dest, *test, *jmp;
11792         struct triple *ptr, *value, *top, *dbranch;
11793
11794         /* See if w have a valid case statement */
11795         eat(state, TOK_CASE);
11796         cvalue = constant_expr(state);
11797         integral(state, cvalue);
11798         if (cvalue->op != OP_INTCONST) {
11799                 error(state, 0, "integer constant expected");
11800         }
11801         eat(state, TOK_COLON);
11802         if (!state->i_case->sym_ident) {
11803                 error(state, 0, "case statement not within a switch");
11804         }
11805
11806         /* Lookup the interesting pieces */
11807         top = state->i_case->sym_ident->def;
11808         value = state->i_switch->sym_ident->def;
11809         dbranch = state->i_default->sym_ident->def;
11810
11811         /* See if this case label has already been used */
11812         for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11813                 if (ptr->op != OP_EQ) {
11814                         continue;
11815                 }
11816                 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11817                         error(state, 0, "duplicate case %d statement",
11818                                 cvalue->u.cval);
11819                 }
11820         }
11821         /* Generate the needed pieces */
11822         dest = label(state);
11823         test = triple(state, OP_EQ, &int_type, value, cvalue);
11824         jmp = branch(state, dest, test);
11825         /* Thread the pieces together */
11826         flatten(state, dbranch, test);
11827         flatten(state, dbranch, jmp);
11828         flatten(state, dbranch, label(state));
11829         flatten(state, first, dest);
11830         statement(state, first);
11831 }
11832
11833 static void default_statement(struct compile_state *state, struct triple *first)
11834 {
11835         struct triple *dest;
11836         struct triple *dbranch, *end;
11837
11838         /* See if we have a valid default statement */
11839         eat(state, TOK_DEFAULT);
11840         eat(state, TOK_COLON);
11841
11842         if (!state->i_case->sym_ident) {
11843                 error(state, 0, "default statement not within a switch");
11844         }
11845
11846         /* Lookup the interesting pieces */
11847         dbranch = state->i_default->sym_ident->def;
11848         end = state->i_break->sym_ident->def;
11849
11850         /* See if a default statement has already happened */
11851         if (TARG(dbranch, 0) != end) {
11852                 error(state, 0, "duplicate default statement");
11853         }
11854
11855         /* Generate the needed pieces */
11856         dest = label(state);
11857
11858         /* Blame the branch on the default statement */
11859         put_occurance(dbranch->occurance);
11860         dbranch->occurance = new_occurance(state);
11861
11862         /* Thread the pieces together */
11863         TARG(dbranch, 0) = dest;
11864         use_triple(dest, dbranch);
11865         flatten(state, first, dest);
11866         statement(state, first);
11867 }
11868
11869 static void asm_statement(struct compile_state *state, struct triple *first)
11870 {
11871         struct asm_info *info;
11872         struct {
11873                 struct triple *constraint;
11874                 struct triple *expr;
11875         } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
11876         struct triple *def, *asm_str;
11877         int out, in, clobbers, more, colons, i;
11878         int flags;
11879
11880         flags = 0;
11881         eat(state, TOK_ASM);
11882         /* For now ignore the qualifiers */
11883         switch(peek(state)) {
11884         case TOK_CONST:
11885                 eat(state, TOK_CONST);
11886                 break;
11887         case TOK_VOLATILE:
11888                 eat(state, TOK_VOLATILE);
11889                 flags |= TRIPLE_FLAG_VOLATILE;
11890                 break;
11891         }
11892         eat(state, TOK_LPAREN);
11893         asm_str = string_constant(state);
11894
11895         colons = 0;
11896         out = in = clobbers = 0;
11897         /* Outputs */
11898         if ((colons == 0) && (peek(state) == TOK_COLON)) {
11899                 eat(state, TOK_COLON);
11900                 colons++;
11901                 more = (peek(state) == TOK_LIT_STRING);
11902                 while(more) {
11903                         struct triple *var;
11904                         struct triple *constraint;
11905                         char *str;
11906                         more = 0;
11907                         if (out > MAX_LHS) {
11908                                 error(state, 0, "Maximum output count exceeded.");
11909                         }
11910                         constraint = string_constant(state);
11911                         str = constraint->u.blob;
11912                         if (str[0] != '=') {
11913                                 error(state, 0, "Output constraint does not start with =");
11914                         }
11915                         constraint->u.blob = str + 1;
11916                         eat(state, TOK_LPAREN);
11917                         var = conditional_expr(state);
11918                         eat(state, TOK_RPAREN);
11919
11920                         lvalue(state, var);
11921                         out_param[out].constraint = constraint;
11922                         out_param[out].expr       = var;
11923                         if (peek(state) == TOK_COMMA) {
11924                                 eat(state, TOK_COMMA);
11925                                 more = 1;
11926                         }
11927                         out++;
11928                 }
11929         }
11930         /* Inputs */
11931         if ((colons == 1) && (peek(state) == TOK_COLON)) {
11932                 eat(state, TOK_COLON);
11933                 colons++;
11934                 more = (peek(state) == TOK_LIT_STRING);
11935                 while(more) {
11936                         struct triple *val;
11937                         struct triple *constraint;
11938                         char *str;
11939                         more = 0;
11940                         if (in > MAX_RHS) {
11941                                 error(state, 0, "Maximum input count exceeded.");
11942                         }
11943                         constraint = string_constant(state);
11944                         str = constraint->u.blob;
11945                         if (digitp(str[0] && str[1] == '\0')) {
11946                                 int val;
11947                                 val = digval(str[0]);
11948                                 if ((val < 0) || (val >= out)) {
11949                                         error(state, 0, "Invalid input constraint %d", val);
11950                                 }
11951                         }
11952                         eat(state, TOK_LPAREN);
11953                         val = conditional_expr(state);
11954                         eat(state, TOK_RPAREN);
11955
11956                         in_param[in].constraint = constraint;
11957                         in_param[in].expr       = val;
11958                         if (peek(state) == TOK_COMMA) {
11959                                 eat(state, TOK_COMMA);
11960                                 more = 1;
11961                         }
11962                         in++;
11963                 }
11964         }
11965
11966         /* Clobber */
11967         if ((colons == 2) && (peek(state) == TOK_COLON)) {
11968                 eat(state, TOK_COLON);
11969                 colons++;
11970                 more = (peek(state) == TOK_LIT_STRING);
11971                 while(more) {
11972                         struct triple *clobber;
11973                         more = 0;
11974                         if ((clobbers + out) > MAX_LHS) {
11975                                 error(state, 0, "Maximum clobber limit exceeded.");
11976                         }
11977                         clobber = string_constant(state);
11978
11979                         clob_param[clobbers].constraint = clobber;
11980                         if (peek(state) == TOK_COMMA) {
11981                                 eat(state, TOK_COMMA);
11982                                 more = 1;
11983                         }
11984                         clobbers++;
11985                 }
11986         }
11987         eat(state, TOK_RPAREN);
11988         eat(state, TOK_SEMI);
11989
11990
11991         info = xcmalloc(sizeof(*info), "asm_info");
11992         info->str = asm_str->u.blob;
11993         free_triple(state, asm_str);
11994
11995         def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
11996         def->u.ainfo = info;
11997         def->id |= flags;
11998
11999         /* Find the register constraints */
12000         for(i = 0; i < out; i++) {
12001                 struct triple *constraint;
12002                 constraint = out_param[i].constraint;
12003                 info->tmpl.lhs[i] = arch_reg_constraint(state, 
12004                         out_param[i].expr->type, constraint->u.blob);
12005                 free_triple(state, constraint);
12006         }
12007         for(; i - out < clobbers; i++) {
12008                 struct triple *constraint;
12009                 constraint = clob_param[i - out].constraint;
12010                 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12011                 free_triple(state, constraint);
12012         }
12013         for(i = 0; i < in; i++) {
12014                 struct triple *constraint;
12015                 const char *str;
12016                 constraint = in_param[i].constraint;
12017                 str = constraint->u.blob;
12018                 if (digitp(str[0]) && str[1] == '\0') {
12019                         struct reg_info cinfo;
12020                         int val;
12021                         val = digval(str[0]);
12022                         cinfo.reg = info->tmpl.lhs[val].reg;
12023                         cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12024                         cinfo.regcm &= info->tmpl.lhs[val].regcm;
12025                         if (cinfo.reg == REG_UNSET) {
12026                                 cinfo.reg = REG_VIRT0 + val;
12027                         }
12028                         if (cinfo.regcm == 0) {
12029                                 error(state, 0, "No registers for %d", val);
12030                         }
12031                         info->tmpl.lhs[val] = cinfo;
12032                         info->tmpl.rhs[i]   = cinfo;
12033                                 
12034                 } else {
12035                         info->tmpl.rhs[i] = arch_reg_constraint(state, 
12036                                 in_param[i].expr->type, str);
12037                 }
12038                 free_triple(state, constraint);
12039         }
12040
12041         /* Now build the helper expressions */
12042         for(i = 0; i < in; i++) {
12043                 RHS(def, i) = read_expr(state, in_param[i].expr);
12044         }
12045         flatten(state, first, def);
12046         for(i = 0; i < (out + clobbers); i++) {
12047                 struct type *type;
12048                 struct triple *piece;
12049                 if (i < out) {
12050                         type = out_param[i].expr->type;
12051                 } else {
12052                         size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12053                         if (size >= SIZEOF_LONG) {
12054                                 type = &ulong_type;
12055                         } 
12056                         else if (size >= SIZEOF_INT) {
12057                                 type = &uint_type;
12058                         }
12059                         else if (size >= SIZEOF_SHORT) {
12060                                 type = &ushort_type;
12061                         }
12062                         else {
12063                                 type = &uchar_type;
12064                         }
12065                 }
12066                 piece = triple(state, OP_PIECE, type, def, 0);
12067                 piece->u.cval = i;
12068                 LHS(def, i) = piece;
12069                 flatten(state, first, piece);
12070         }
12071         /* And write the helpers to their destinations */
12072         for(i = 0; i < out; i++) {
12073                 struct triple *piece;
12074                 piece = LHS(def, i);
12075                 flatten(state, first,
12076                         write_expr(state, out_param[i].expr, piece));
12077         }
12078 }
12079
12080
12081 static int isdecl(int tok)
12082 {
12083         switch(tok) {
12084         case TOK_AUTO:
12085         case TOK_REGISTER:
12086         case TOK_STATIC:
12087         case TOK_EXTERN:
12088         case TOK_TYPEDEF:
12089         case TOK_CONST:
12090         case TOK_RESTRICT:
12091         case TOK_VOLATILE:
12092         case TOK_VOID:
12093         case TOK_CHAR:
12094         case TOK_SHORT:
12095         case TOK_INT:
12096         case TOK_LONG:
12097         case TOK_FLOAT:
12098         case TOK_DOUBLE:
12099         case TOK_SIGNED:
12100         case TOK_UNSIGNED:
12101         case TOK_STRUCT:
12102         case TOK_UNION:
12103         case TOK_ENUM:
12104         case TOK_TYPE_NAME: /* typedef name */
12105                 return 1;
12106         default:
12107                 return 0;
12108         }
12109 }
12110
12111 static void compound_statement(struct compile_state *state, struct triple *first)
12112 {
12113         eat(state, TOK_LBRACE);
12114         start_scope(state);
12115
12116         /* statement-list opt */
12117         while (peek(state) != TOK_RBRACE) {
12118                 statement(state, first);
12119         }
12120         end_scope(state);
12121         eat(state, TOK_RBRACE);
12122 }
12123
12124 static void statement(struct compile_state *state, struct triple *first)
12125 {
12126         int tok;
12127         tok = peek(state);
12128         if (tok == TOK_LBRACE) {
12129                 compound_statement(state, first);
12130         }
12131         else if (tok == TOK_IF) {
12132                 if_statement(state, first); 
12133         }
12134         else if (tok == TOK_FOR) {
12135                 for_statement(state, first);
12136         }
12137         else if (tok == TOK_WHILE) {
12138                 while_statement(state, first);
12139         }
12140         else if (tok == TOK_DO) {
12141                 do_statement(state, first);
12142         }
12143         else if (tok == TOK_RETURN) {
12144                 return_statement(state, first);
12145         }
12146         else if (tok == TOK_BREAK) {
12147                 break_statement(state, first);
12148         }
12149         else if (tok == TOK_CONTINUE) {
12150                 continue_statement(state, first);
12151         }
12152         else if (tok == TOK_GOTO) {
12153                 goto_statement(state, first);
12154         }
12155         else if (tok == TOK_SWITCH) {
12156                 switch_statement(state, first);
12157         }
12158         else if (tok == TOK_ASM) {
12159                 asm_statement(state, first);
12160         }
12161         else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12162                 labeled_statement(state, first); 
12163         }
12164         else if (tok == TOK_CASE) {
12165                 case_statement(state, first);
12166         }
12167         else if (tok == TOK_DEFAULT) {
12168                 default_statement(state, first);
12169         }
12170         else if (isdecl(tok)) {
12171                 /* This handles C99 intermixing of statements and decls */
12172                 decl(state, first);
12173         }
12174         else {
12175                 expr_statement(state, first);
12176         }
12177 }
12178
12179 static struct type *param_decl(struct compile_state *state)
12180 {
12181         struct type *type;
12182         struct hash_entry *ident;
12183         /* Cheat so the declarator will know we are not global */
12184         start_scope(state); 
12185         ident = 0;
12186         type = decl_specifiers(state);
12187         type = declarator(state, type, &ident, 0);
12188         type->field_ident = ident;
12189         end_scope(state);
12190         return type;
12191 }
12192
12193 static struct type *param_type_list(struct compile_state *state, struct type *type)
12194 {
12195         struct type *ftype, **next;
12196         ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
12197         next = &ftype->right;
12198         ftype->elements = 1;
12199         while(peek(state) == TOK_COMMA) {
12200                 eat(state, TOK_COMMA);
12201                 if (peek(state) == TOK_DOTS) {
12202                         eat(state, TOK_DOTS);
12203                         error(state, 0, "variadic functions not supported");
12204                 }
12205                 else {
12206                         *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12207                         next = &((*next)->right);
12208                         ftype->elements++;
12209                 }
12210         }
12211         return ftype;
12212 }
12213
12214 static struct type *type_name(struct compile_state *state)
12215 {
12216         struct type *type;
12217         type = specifier_qualifier_list(state);
12218         /* abstract-declarator (may consume no tokens) */
12219         type = declarator(state, type, 0, 0);
12220         return type;
12221 }
12222
12223 static struct type *direct_declarator(
12224         struct compile_state *state, struct type *type, 
12225         struct hash_entry **pident, int need_ident)
12226 {
12227         struct hash_entry *ident;
12228         struct type *outer;
12229         int op;
12230         outer = 0;
12231         arrays_complete(state, type);
12232         switch(peek(state)) {
12233         case TOK_IDENT:
12234                 ident = eat(state, TOK_IDENT)->ident;
12235                 if (!ident) {
12236                         error(state, 0, "Unexpected identifier found");
12237                 }
12238                 /* The name of what we are declaring */
12239                 *pident = ident;
12240                 break;
12241         case TOK_LPAREN:
12242                 eat(state, TOK_LPAREN);
12243                 outer = declarator(state, type, pident, need_ident);
12244                 eat(state, TOK_RPAREN);
12245                 break;
12246         default:
12247                 if (need_ident) {
12248                         error(state, 0, "Identifier expected");
12249                 }
12250                 break;
12251         }
12252         do {
12253                 op = 1;
12254                 arrays_complete(state, type);
12255                 switch(peek(state)) {
12256                 case TOK_LPAREN:
12257                         eat(state, TOK_LPAREN);
12258                         type = param_type_list(state, type);
12259                         eat(state, TOK_RPAREN);
12260                         break;
12261                 case TOK_LBRACKET:
12262                 {
12263                         unsigned int qualifiers;
12264                         struct triple *value;
12265                         value = 0;
12266                         eat(state, TOK_LBRACKET);
12267                         if (peek(state) != TOK_RBRACKET) {
12268                                 value = constant_expr(state);
12269                                 integral(state, value);
12270                         }
12271                         eat(state, TOK_RBRACKET);
12272
12273                         qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12274                         type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12275                         if (value) {
12276                                 type->elements = value->u.cval;
12277                                 free_triple(state, value);
12278                         } else {
12279                                 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12280                                 op = 0;
12281                         }
12282                 }
12283                         break;
12284                 default:
12285                         op = 0;
12286                         break;
12287                 }
12288         } while(op);
12289         if (outer) {
12290                 struct type *inner;
12291                 arrays_complete(state, type);
12292                 FINISHME();
12293                 for(inner = outer; inner->left; inner = inner->left)
12294                         ;
12295                 inner->left = type;
12296                 type = outer;
12297         }
12298         return type;
12299 }
12300
12301 static struct type *declarator(
12302         struct compile_state *state, struct type *type, 
12303         struct hash_entry **pident, int need_ident)
12304 {
12305         while(peek(state) == TOK_STAR) {
12306                 eat(state, TOK_STAR);
12307                 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12308         }
12309         type = direct_declarator(state, type, pident, need_ident);
12310         return type;
12311 }
12312
12313 static struct type *typedef_name(
12314         struct compile_state *state, unsigned int specifiers)
12315 {
12316         struct hash_entry *ident;
12317         struct type *type;
12318         ident = eat(state, TOK_TYPE_NAME)->ident;
12319         type = ident->sym_ident->type;
12320         specifiers |= type->type & QUAL_MASK;
12321         if ((specifiers & (STOR_MASK | QUAL_MASK)) != 
12322                 (type->type & (STOR_MASK | QUAL_MASK))) {
12323                 type = clone_type(specifiers, type);
12324         }
12325         return type;
12326 }
12327
12328 static struct type *enum_specifier(
12329         struct compile_state *state, unsigned int spec)
12330 {
12331         struct hash_entry *ident;
12332         ulong_t base;
12333         int tok;
12334         struct type *enum_type;
12335         enum_type = 0;
12336         ident = 0;
12337         eat(state, TOK_ENUM);
12338         tok = peek(state);
12339         if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
12340                 ident = eat(state, tok)->ident;
12341         }
12342         base = 0;
12343         if (!ident || (peek(state) == TOK_LBRACE)) {
12344                 struct type **next;
12345                 eat(state, TOK_LBRACE);
12346                 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12347                 enum_type->type_ident = ident;
12348                 next = &enum_type->right;
12349                 do {
12350                         struct hash_entry *eident;
12351                         struct triple *value;
12352                         struct type *entry;
12353                         eident = eat(state, TOK_IDENT)->ident;
12354                         if (eident->sym_ident) {
12355                                 error(state, 0, "%s already declared", 
12356                                         eident->name);
12357                         }
12358                         eident->tok = TOK_ENUM_CONST;
12359                         if (peek(state) == TOK_EQ) {
12360                                 struct triple *val;
12361                                 eat(state, TOK_EQ);
12362                                 val = constant_expr(state);
12363                                 integral(state, val);
12364                                 base = val->u.cval;
12365                         }
12366                         value = int_const(state, &int_type, base);
12367                         symbol(state, eident, &eident->sym_ident, value, &int_type);
12368                         entry = new_type(TYPE_LIST, 0, 0);
12369                         entry->field_ident = eident;
12370                         *next = entry;
12371                         next = &entry->right;
12372                         base += 1;
12373                         if (peek(state) == TOK_COMMA) {
12374                                 eat(state, TOK_COMMA);
12375                         }
12376                 } while(peek(state) != TOK_RBRACE);
12377                 eat(state, TOK_RBRACE);
12378                 if (ident) {
12379                         symbol(state, ident, &ident->sym_tag, 0, enum_type);
12380                 }
12381         }
12382         if (ident && ident->sym_tag &&
12383                 ident->sym_tag->type &&
12384                 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12385                 enum_type = clone_type(spec, ident->sym_tag->type);
12386         }
12387         else if (ident && !enum_type) {
12388                 error(state, 0, "enum %s undeclared", ident->name);
12389         }
12390         return enum_type;
12391 }
12392
12393 static struct type *struct_declarator(
12394         struct compile_state *state, struct type *type, struct hash_entry **ident)
12395 {
12396         if (peek(state) != TOK_COLON) {
12397                 type = declarator(state, type, ident, 1);
12398         }
12399         if (peek(state) == TOK_COLON) {
12400                 struct triple *value;
12401                 eat(state, TOK_COLON);
12402                 value = constant_expr(state);
12403                 if (value->op != OP_INTCONST) {
12404                         error(state, 0, "Invalid constant expression");
12405                 }
12406                 if (value->u.cval > size_of(state, type)) {
12407                         error(state, 0, "bitfield larger than base type");
12408                 }
12409                 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12410                         error(state, 0, "bitfield base not an integer type");
12411                 }
12412                 type = new_type(TYPE_BITFIELD, type, 0);
12413                 type->elements = value->u.cval;
12414         }
12415         return type;
12416 }
12417
12418 static struct type *struct_or_union_specifier(
12419         struct compile_state *state, unsigned int spec)
12420 {
12421         struct type *struct_type;
12422         struct hash_entry *ident;
12423         unsigned int type_main;
12424         unsigned int type_join;
12425         int tok;
12426         struct_type = 0;
12427         ident = 0;
12428         switch(peek(state)) {
12429         case TOK_STRUCT:
12430                 eat(state, TOK_STRUCT);
12431                 type_main = TYPE_STRUCT;
12432                 type_join = TYPE_PRODUCT;
12433                 break;
12434         case TOK_UNION:
12435                 eat(state, TOK_UNION);
12436                 type_main = TYPE_UNION;
12437                 type_join = TYPE_OVERLAP;
12438                 break;
12439         default:
12440                 eat(state, TOK_STRUCT);
12441                 type_main = TYPE_STRUCT;
12442                 type_join = TYPE_PRODUCT;
12443                 break;
12444         }
12445         tok = peek(state);
12446         if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
12447                 ident = eat(state, tok)->ident;
12448         }
12449         if (!ident || (peek(state) == TOK_LBRACE)) {
12450                 ulong_t elements;
12451                 struct type **next;
12452                 elements = 0;
12453                 eat(state, TOK_LBRACE);
12454                 next = &struct_type;
12455                 do {
12456                         struct type *base_type;
12457                         int done;
12458                         base_type = specifier_qualifier_list(state);
12459                         do {
12460                                 struct type *type;
12461                                 struct hash_entry *fident;
12462                                 done = 1;
12463                                 type = struct_declarator(state, base_type, &fident);
12464                                 elements++;
12465                                 if (peek(state) == TOK_COMMA) {
12466                                         done = 0;
12467                                         eat(state, TOK_COMMA);
12468                                 }
12469                                 type = clone_type(0, type);
12470                                 type->field_ident = fident;
12471                                 if (*next) {
12472                                         *next = new_type(type_join, *next, type);
12473                                         next = &((*next)->right);
12474                                 } else {
12475                                         *next = type;
12476                                 }
12477                         } while(!done);
12478                         eat(state, TOK_SEMI);
12479                 } while(peek(state) != TOK_RBRACE);
12480                 eat(state, TOK_RBRACE);
12481                 struct_type = new_type(type_main | spec, struct_type, 0);
12482                 struct_type->type_ident = ident;
12483                 struct_type->elements = elements;
12484                 if (ident) {
12485                         symbol(state, ident, &ident->sym_tag, 0, struct_type);
12486                 }
12487         }
12488         if (ident && ident->sym_tag && 
12489                 ident->sym_tag->type && 
12490                 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
12491                 struct_type = clone_type(spec, ident->sym_tag->type);
12492         }
12493         else if (ident && !struct_type) {
12494                 error(state, 0, "%s %s undeclared", 
12495                         (type_main == TYPE_STRUCT)?"struct" : "union",
12496                         ident->name);
12497         }
12498         return struct_type;
12499 }
12500
12501 static unsigned int storage_class_specifier_opt(struct compile_state *state)
12502 {
12503         unsigned int specifiers;
12504         switch(peek(state)) {
12505         case TOK_AUTO:
12506                 eat(state, TOK_AUTO);
12507                 specifiers = STOR_AUTO;
12508                 break;
12509         case TOK_REGISTER:
12510                 eat(state, TOK_REGISTER);
12511                 specifiers = STOR_REGISTER;
12512                 break;
12513         case TOK_STATIC:
12514                 eat(state, TOK_STATIC);
12515                 specifiers = STOR_STATIC;
12516                 break;
12517         case TOK_EXTERN:
12518                 eat(state, TOK_EXTERN);
12519                 specifiers = STOR_EXTERN;
12520                 break;
12521         case TOK_TYPEDEF:
12522                 eat(state, TOK_TYPEDEF);
12523                 specifiers = STOR_TYPEDEF;
12524                 break;
12525         default:
12526                 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
12527                         specifiers = STOR_LOCAL;
12528                 }
12529                 else {
12530                         specifiers = STOR_AUTO;
12531                 }
12532         }
12533         return specifiers;
12534 }
12535
12536 static unsigned int function_specifier_opt(struct compile_state *state)
12537 {
12538         /* Ignore the inline keyword */
12539         unsigned int specifiers;
12540         specifiers = 0;
12541         switch(peek(state)) {
12542         case TOK_INLINE:
12543                 eat(state, TOK_INLINE);
12544                 specifiers = STOR_INLINE;
12545         }
12546         return specifiers;
12547 }
12548
12549 static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12550 {
12551         int tok = peek(state);
12552         switch(tok) {
12553         case TOK_COMMA:
12554         case TOK_LPAREN:
12555                 /* The empty attribute ignore it */
12556                 break;
12557         case TOK_IDENT:
12558         case TOK_ENUM_CONST:
12559         case TOK_TYPE_NAME:
12560         {
12561                 struct hash_entry *ident;
12562                 ident = eat(state, TOK_IDENT)->ident;
12563
12564                 if (ident == state->i_noinline) {
12565                         if (attributes & ATTRIB_ALWAYS_INLINE) {
12566                                 error(state, 0, "both always_inline and noinline attribtes");
12567                         }
12568                         attributes |= ATTRIB_NOINLINE;
12569                 }
12570                 else if (ident == state->i_always_inline) {
12571                         if (attributes & ATTRIB_NOINLINE) {
12572                                 error(state, 0, "both noinline and always_inline attribtes");
12573                         }
12574                         attributes |= ATTRIB_ALWAYS_INLINE;
12575                 }
12576                 else {
12577                         error(state, 0, "Unknown attribute:%s", ident->name);
12578                 }
12579                 break;
12580         }
12581         default:
12582                 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12583                 break;
12584         }
12585         return attributes;
12586 }
12587
12588 static unsigned int attribute_list(struct compile_state *state, unsigned type)
12589 {
12590         type = attrib(state, type);
12591         while(peek(state) == TOK_COMMA) {
12592                 eat(state, TOK_COMMA);
12593                 type = attrib(state, type);
12594         }
12595         return type;
12596 }
12597
12598 static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12599 {
12600         if (peek(state) == TOK_ATTRIBUTE) {
12601                 eat(state, TOK_ATTRIBUTE);
12602                 eat(state, TOK_LPAREN);
12603                 eat(state, TOK_LPAREN);
12604                 type = attribute_list(state, type);
12605                 eat(state, TOK_RPAREN);
12606                 eat(state, TOK_RPAREN);
12607         }
12608         return type;
12609 }
12610
12611 static unsigned int type_qualifiers(struct compile_state *state)
12612 {
12613         unsigned int specifiers;
12614         int done;
12615         done = 0;
12616         specifiers = QUAL_NONE;
12617         do {
12618                 switch(peek(state)) {
12619                 case TOK_CONST:
12620                         eat(state, TOK_CONST);
12621                         specifiers |= QUAL_CONST;
12622                         break;
12623                 case TOK_VOLATILE:
12624                         eat(state, TOK_VOLATILE);
12625                         specifiers |= QUAL_VOLATILE;
12626                         break;
12627                 case TOK_RESTRICT:
12628                         eat(state, TOK_RESTRICT);
12629                         specifiers |= QUAL_RESTRICT;
12630                         break;
12631                 default:
12632                         done = 1;
12633                         break;
12634                 }
12635         } while(!done);
12636         return specifiers;
12637 }
12638
12639 static struct type *type_specifier(
12640         struct compile_state *state, unsigned int spec)
12641 {
12642         struct type *type;
12643         int tok;
12644         type = 0;
12645         switch((tok = peek(state))) {
12646         case TOK_VOID:
12647                 eat(state, TOK_VOID);
12648                 type = new_type(TYPE_VOID | spec, 0, 0);
12649                 break;
12650         case TOK_CHAR:
12651                 eat(state, TOK_CHAR);
12652                 type = new_type(TYPE_CHAR | spec, 0, 0);
12653                 break;
12654         case TOK_SHORT:
12655                 eat(state, TOK_SHORT);
12656                 if (peek(state) == TOK_INT) {
12657                         eat(state, TOK_INT);
12658                 }
12659                 type = new_type(TYPE_SHORT | spec, 0, 0);
12660                 break;
12661         case TOK_INT:
12662                 eat(state, TOK_INT);
12663                 type = new_type(TYPE_INT | spec, 0, 0);
12664                 break;
12665         case TOK_LONG:
12666                 eat(state, TOK_LONG);
12667                 switch(peek(state)) {
12668                 case TOK_LONG:
12669                         eat(state, TOK_LONG);
12670                         error(state, 0, "long long not supported");
12671                         break;
12672                 case TOK_DOUBLE:
12673                         eat(state, TOK_DOUBLE);
12674                         error(state, 0, "long double not supported");
12675                         break;
12676                 case TOK_INT:
12677                         eat(state, TOK_INT);
12678                         type = new_type(TYPE_LONG | spec, 0, 0);
12679                         break;
12680                 default:
12681                         type = new_type(TYPE_LONG | spec, 0, 0);
12682                         break;
12683                 }
12684                 break;
12685         case TOK_FLOAT:
12686                 eat(state, TOK_FLOAT);
12687                 error(state, 0, "type float not supported");
12688                 break;
12689         case TOK_DOUBLE:
12690                 eat(state, TOK_DOUBLE);
12691                 error(state, 0, "type double not supported");
12692                 break;
12693         case TOK_SIGNED:
12694                 eat(state, TOK_SIGNED);
12695                 switch(peek(state)) {
12696                 case TOK_LONG:
12697                         eat(state, TOK_LONG);
12698                         switch(peek(state)) {
12699                         case TOK_LONG:
12700                                 eat(state, TOK_LONG);
12701                                 error(state, 0, "type long long not supported");
12702                                 break;
12703                         case TOK_INT:
12704                                 eat(state, TOK_INT);
12705                                 type = new_type(TYPE_LONG | spec, 0, 0);
12706                                 break;
12707                         default:
12708                                 type = new_type(TYPE_LONG | spec, 0, 0);
12709                                 break;
12710                         }
12711                         break;
12712                 case TOK_INT:
12713                         eat(state, TOK_INT);
12714                         type = new_type(TYPE_INT | spec, 0, 0);
12715                         break;
12716                 case TOK_SHORT:
12717                         eat(state, TOK_SHORT);
12718                         type = new_type(TYPE_SHORT | spec, 0, 0);
12719                         break;
12720                 case TOK_CHAR:
12721                         eat(state, TOK_CHAR);
12722                         type = new_type(TYPE_CHAR | spec, 0, 0);
12723                         break;
12724                 default:
12725                         type = new_type(TYPE_INT | spec, 0, 0);
12726                         break;
12727                 }
12728                 break;
12729         case TOK_UNSIGNED:
12730                 eat(state, TOK_UNSIGNED);
12731                 switch(peek(state)) {
12732                 case TOK_LONG:
12733                         eat(state, TOK_LONG);
12734                         switch(peek(state)) {
12735                         case TOK_LONG:
12736                                 eat(state, TOK_LONG);
12737                                 error(state, 0, "unsigned long long not supported");
12738                                 break;
12739                         case TOK_INT:
12740                                 eat(state, TOK_INT);
12741                                 type = new_type(TYPE_ULONG | spec, 0, 0);
12742                                 break;
12743                         default:
12744                                 type = new_type(TYPE_ULONG | spec, 0, 0);
12745                                 break;
12746                         }
12747                         break;
12748                 case TOK_INT:
12749                         eat(state, TOK_INT);
12750                         type = new_type(TYPE_UINT | spec, 0, 0);
12751                         break;
12752                 case TOK_SHORT:
12753                         eat(state, TOK_SHORT);
12754                         type = new_type(TYPE_USHORT | spec, 0, 0);
12755                         break;
12756                 case TOK_CHAR:
12757                         eat(state, TOK_CHAR);
12758                         type = new_type(TYPE_UCHAR | spec, 0, 0);
12759                         break;
12760                 default:
12761                         type = new_type(TYPE_UINT | spec, 0, 0);
12762                         break;
12763                 }
12764                 break;
12765                 /* struct or union specifier */
12766         case TOK_STRUCT:
12767         case TOK_UNION:
12768                 type = struct_or_union_specifier(state, spec);
12769                 break;
12770                 /* enum-spefifier */
12771         case TOK_ENUM:
12772                 type = enum_specifier(state, spec);
12773                 break;
12774                 /* typedef name */
12775         case TOK_TYPE_NAME:
12776                 type = typedef_name(state, spec);
12777                 break;
12778         default:
12779                 error(state, 0, "bad type specifier %s", 
12780                         tokens[tok]);
12781                 break;
12782         }
12783         return type;
12784 }
12785
12786 static int istype(int tok)
12787 {
12788         switch(tok) {
12789         case TOK_CONST:
12790         case TOK_RESTRICT:
12791         case TOK_VOLATILE:
12792         case TOK_VOID:
12793         case TOK_CHAR:
12794         case TOK_SHORT:
12795         case TOK_INT:
12796         case TOK_LONG:
12797         case TOK_FLOAT:
12798         case TOK_DOUBLE:
12799         case TOK_SIGNED:
12800         case TOK_UNSIGNED:
12801         case TOK_STRUCT:
12802         case TOK_UNION:
12803         case TOK_ENUM:
12804         case TOK_TYPE_NAME:
12805                 return 1;
12806         default:
12807                 return 0;
12808         }
12809 }
12810
12811
12812 static struct type *specifier_qualifier_list(struct compile_state *state)
12813 {
12814         struct type *type;
12815         unsigned int specifiers = 0;
12816
12817         /* type qualifiers */
12818         specifiers |= type_qualifiers(state);
12819
12820         /* type specifier */
12821         type = type_specifier(state, specifiers);
12822
12823         return type;
12824 }
12825
12826 static int isdecl_specifier(int tok)
12827 {
12828         switch(tok) {
12829                 /* storage class specifier */
12830         case TOK_AUTO:
12831         case TOK_REGISTER:
12832         case TOK_STATIC:
12833         case TOK_EXTERN:
12834         case TOK_TYPEDEF:
12835                 /* type qualifier */
12836         case TOK_CONST:
12837         case TOK_RESTRICT:
12838         case TOK_VOLATILE:
12839                 /* type specifiers */
12840         case TOK_VOID:
12841         case TOK_CHAR:
12842         case TOK_SHORT:
12843         case TOK_INT:
12844         case TOK_LONG:
12845         case TOK_FLOAT:
12846         case TOK_DOUBLE:
12847         case TOK_SIGNED:
12848         case TOK_UNSIGNED:
12849                 /* struct or union specifier */
12850         case TOK_STRUCT:
12851         case TOK_UNION:
12852                 /* enum-spefifier */
12853         case TOK_ENUM:
12854                 /* typedef name */
12855         case TOK_TYPE_NAME:
12856                 /* function specifiers */
12857         case TOK_INLINE:
12858                 return 1;
12859         default:
12860                 return 0;
12861         }
12862 }
12863
12864 static struct type *decl_specifiers(struct compile_state *state)
12865 {
12866         struct type *type;
12867         unsigned int specifiers;
12868         /* I am overly restrictive in the arragement of specifiers supported.
12869          * C is overly flexible in this department it makes interpreting
12870          * the parse tree difficult.
12871          */
12872         specifiers = 0;
12873
12874         /* storage class specifier */
12875         specifiers |= storage_class_specifier_opt(state);
12876
12877         /* function-specifier */
12878         specifiers |= function_specifier_opt(state);
12879
12880         /* attributes */
12881         specifiers |= attributes_opt(state, 0);
12882
12883         /* type qualifier */
12884         specifiers |= type_qualifiers(state);
12885
12886         /* type specifier */
12887         type = type_specifier(state, specifiers);
12888         return type;
12889 }
12890
12891 struct field_info {
12892         struct type *type;
12893         size_t offset;
12894 };
12895
12896 static struct field_info designator(struct compile_state *state, struct type *type)
12897 {
12898         int tok;
12899         struct field_info info;
12900         info.offset = ~0U;
12901         info.type = 0;
12902         do {
12903                 switch(peek(state)) {
12904                 case TOK_LBRACKET:
12905                 {
12906                         struct triple *value;
12907                         if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
12908                                 error(state, 0, "Array designator not in array initializer");
12909                         }
12910                         eat(state, TOK_LBRACKET);
12911                         value = constant_expr(state);
12912                         eat(state, TOK_RBRACKET);
12913
12914                         info.type = type->left;
12915                         info.offset = value->u.cval * size_of(state, info.type);
12916                         break;
12917                 }
12918                 case TOK_DOT:
12919                 {
12920                         struct hash_entry *field;
12921                         if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
12922                                 ((type->type & TYPE_MASK) != TYPE_UNION))
12923                         {
12924                                 error(state, 0, "Struct designator not in struct initializer");
12925                         }
12926                         eat(state, TOK_DOT);
12927                         field = eat(state, TOK_IDENT)->ident;
12928                         info.offset = field_offset(state, type, field);
12929                         info.type   = field_type(state, type, field);
12930                         break;
12931                 }
12932                 default:
12933                         error(state, 0, "Invalid designator");
12934                 }
12935                 tok = peek(state);
12936         } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
12937         eat(state, TOK_EQ);
12938         return info;
12939 }
12940
12941 static struct triple *initializer(
12942         struct compile_state *state, struct type *type)
12943 {
12944         struct triple *result;
12945 #warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
12946         if (peek(state) != TOK_LBRACE) {
12947                 result = assignment_expr(state);
12948                 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
12949                         (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
12950                         ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
12951                         (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
12952                         (equiv_types(type->left, result->type->left))) {
12953                         type->elements = result->type->elements;
12954                 }
12955                 if (is_lvalue(state, result) && 
12956                         ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
12957                         (type->type & TYPE_MASK) != TYPE_ARRAY)
12958                 {
12959                         result = lvalue_conversion(state, result);
12960                 }
12961                 if (!is_init_compatible(state, type, result->type)) {
12962                         error(state, 0, "Incompatible types in initializer");
12963                 }
12964                 if (!equiv_types(type, result->type)) {
12965                         result = mk_cast_expr(state, type, result);
12966                 }
12967         }
12968         else {
12969                 int comma;
12970                 size_t max_offset;
12971                 struct field_info info;
12972                 void *buf;
12973                 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
12974                         ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
12975                         internal_error(state, 0, "unknown initializer type");
12976                 }
12977                 info.offset = 0;
12978                 info.type = type->left;
12979                 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
12980                         info.type = next_field(state, type, 0);
12981                 }
12982                 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
12983                         max_offset = 0;
12984                 } else {
12985                         max_offset = size_of(state, type);
12986                 }
12987                 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
12988                 eat(state, TOK_LBRACE);
12989                 do {
12990                         struct triple *value;
12991                         struct type *value_type;
12992                         size_t value_size;
12993                         void *dest;
12994                         int tok;
12995                         comma = 0;
12996                         tok = peek(state);
12997                         if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
12998                                 info = designator(state, type);
12999                         }
13000                         if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13001                                 (info.offset >= max_offset)) {
13002                                 error(state, 0, "element beyond bounds");
13003                         }
13004                         value_type = info.type;
13005                         value = eval_const_expr(state, initializer(state, value_type));
13006                         value_size = size_of(state, value_type);
13007                         if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13008                                 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13009                                 (max_offset <= info.offset)) {
13010                                 void *old_buf;
13011                                 size_t old_size;
13012                                 old_buf = buf;
13013                                 old_size = max_offset;
13014                                 max_offset = info.offset + value_size;
13015                                 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13016                                 memcpy(buf, old_buf, bits_to_bytes(old_size));
13017                                 xfree(old_buf);
13018                         }
13019                         dest = ((char *)buf) + bits_to_bytes(info.offset);
13020 #if DEBUG_INITIALIZER
13021                         fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n", 
13022                                 dest - buf,
13023                                 bits_to_bytes(max_offset),
13024                                 bits_to_bytes(value_size),
13025                                 value->op);
13026 #endif
13027                         if (value->op == OP_BLOBCONST) {
13028                                 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
13029                         }
13030                         else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13031 #if DEBUG_INITIALIZER
13032                                 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13033 #endif
13034                                 *((uint8_t *)dest) = value->u.cval & 0xff;
13035                         }
13036                         else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
13037                                 *((uint16_t *)dest) = value->u.cval & 0xffff;
13038                         }
13039                         else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
13040                                 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
13041                         }
13042                         else {
13043                                 internal_error(state, 0, "unhandled constant initializer");
13044                         }
13045                         free_triple(state, value);
13046                         if (peek(state) == TOK_COMMA) {
13047                                 eat(state, TOK_COMMA);
13048                                 comma = 1;
13049                         }
13050                         info.offset += value_size;
13051                         if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13052                                 info.type = next_field(state, type, info.type);
13053                                 info.offset = field_offset(state, type, 
13054                                         info.type->field_ident);
13055                         }
13056                 } while(comma && (peek(state) != TOK_RBRACE));
13057                 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13058                         ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13059                         type->elements = max_offset / size_of(state, type->left);
13060                 }
13061                 eat(state, TOK_RBRACE);
13062                 result = triple(state, OP_BLOBCONST, type, 0, 0);
13063                 result->u.blob = buf;
13064         }
13065         return result;
13066 }
13067
13068 static void resolve_branches(struct compile_state *state, struct triple *first)
13069 {
13070         /* Make a second pass and finish anything outstanding
13071          * with respect to branches.  The only outstanding item
13072          * is to see if there are goto to labels that have not
13073          * been defined and to error about them.
13074          */
13075         int i;
13076         struct triple *ins;
13077         /* Also error on branches that do not use their targets */
13078         ins = first;
13079         do {
13080                 if (!triple_is_ret(state, ins)) {
13081                         struct triple **expr ;
13082                         struct triple_set *set;
13083                         expr = triple_targ(state, ins, 0);
13084                         for(; expr; expr = triple_targ(state, ins, expr)) {
13085                                 struct triple *targ;
13086                                 targ = *expr;
13087                                 for(set = targ?targ->use:0; set; set = set->next) {
13088                                         if (set->member == ins) {
13089                                                 break;
13090                                         }
13091                                 }
13092                                 if (!set) {
13093                                         internal_error(state, ins, "targ not used");
13094                                 }
13095                         }
13096                 }
13097                 ins = ins->next;
13098         } while(ins != first);
13099         /* See if there are goto to labels that have not been defined */
13100         for(i = 0; i < HASH_TABLE_SIZE; i++) {
13101                 struct hash_entry *entry;
13102                 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13103                         struct triple *ins;
13104                         if (!entry->sym_label) {
13105                                 continue;
13106                         }
13107                         ins = entry->sym_label->def;
13108                         if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13109                                 error(state, ins, "label `%s' used but not defined",
13110                                         entry->name);
13111                         }
13112                 }
13113         }
13114 }
13115
13116 static struct triple *function_definition(
13117         struct compile_state *state, struct type *type)
13118 {
13119         struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13120         struct triple *fname;
13121         struct type *fname_type;
13122         struct hash_entry *ident;
13123         struct type *param, *crtype, *ctype;
13124         int i;
13125         if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13126                 error(state, 0, "Invalid function header");
13127         }
13128
13129         /* Verify the function type */
13130         if (((type->right->type & TYPE_MASK) != TYPE_VOID)  &&
13131                 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
13132                 (type->right->field_ident == 0)) {
13133                 error(state, 0, "Invalid function parameters");
13134         }
13135         param = type->right;
13136         i = 0;
13137         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13138                 i++;
13139                 if (!param->left->field_ident) {
13140                         error(state, 0, "No identifier for parameter %d\n", i);
13141                 }
13142                 param = param->right;
13143         }
13144         i++;
13145         if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
13146                 error(state, 0, "No identifier for paramter %d\n", i);
13147         }
13148         
13149         /* Get a list of statements for this function. */
13150         def = triple(state, OP_LIST, type, 0, 0);
13151
13152         /* Start a new scope for the passed parameters */
13153         start_scope(state);
13154
13155         /* Put a label at the very start of a function */
13156         first = label(state);
13157         RHS(def, 0) = first;
13158
13159         /* Put a label at the very end of a function */
13160         end = label(state);
13161         flatten(state, first, end);
13162         /* Remember where return goes */
13163         ident = state->i_return;
13164         symbol(state, ident, &ident->sym_ident, end, end->type);
13165
13166         /* Get the initial closure type */
13167         ctype = new_type(TYPE_JOIN, &void_type, 0);
13168         ctype->elements = 1;
13169
13170         /* Add a variable for the return value */
13171         crtype = new_type(TYPE_TUPLE, 
13172                 /* Remove all type qualifiers from the return type */
13173                 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13174         crtype->elements = 2;
13175         result = flatten(state, end, variable(state, crtype));
13176
13177         /* Allocate a variable for the return address */
13178         retvar = flatten(state, end, variable(state, &void_ptr_type));
13179
13180         /* Add in the return instruction */
13181         ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13182         ret = flatten(state, first, ret);
13183
13184         /* Walk through the parameters and create symbol table entries
13185          * for them.
13186          */
13187         param = type->right;
13188         while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13189                 ident = param->left->field_ident;
13190                 tmp = variable(state, param->left);
13191                 var_symbol(state, ident, tmp);
13192                 flatten(state, end, tmp);
13193                 param = param->right;
13194         }
13195         if ((param->type & TYPE_MASK) != TYPE_VOID) {
13196                 /* And don't forget the last parameter */
13197                 ident = param->field_ident;
13198                 tmp = variable(state, param);
13199                 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13200                 flatten(state, end, tmp);
13201         }
13202
13203         /* Add the declaration static const char __func__ [] = "func-name"  */
13204         fname_type = new_type(TYPE_ARRAY, 
13205                 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13206         fname_type->type |= QUAL_CONST | STOR_STATIC;
13207         fname_type->elements = strlen(state->function) + 1;
13208
13209         fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13210         fname->u.blob = (void *)state->function;
13211         fname = flatten(state, end, fname);
13212
13213         ident = state->i___func__;
13214         symbol(state, ident, &ident->sym_ident, fname, fname_type);
13215
13216         /* Remember which function I am compiling.
13217          * Also assume the last defined function is the main function.
13218          */
13219         state->main_function = def;
13220
13221         /* Now get the actual function definition */
13222         compound_statement(state, end);
13223
13224         /* Finish anything unfinished with branches */
13225         resolve_branches(state, first);
13226
13227         /* Remove the parameter scope */
13228         end_scope(state);
13229
13230
13231         /* Remember I have defined a function */
13232         if (!state->functions) {
13233                 state->functions = def;
13234         } else {
13235                 insert_triple(state, state->functions, def);
13236         }
13237         if (state->compiler->debug & DEBUG_INLINE) {
13238                 FILE *fp = state->dbgout;
13239                 fprintf(fp, "\n");
13240                 loc(fp, state, 0);
13241                 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13242                 display_func(state, fp, def);
13243                 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
13244         }
13245
13246         return def;
13247 }
13248
13249 static struct triple *do_decl(struct compile_state *state, 
13250         struct type *type, struct hash_entry *ident)
13251 {
13252         struct triple *def;
13253         def = 0;
13254         /* Clean up the storage types used */
13255         switch (type->type & STOR_MASK) {
13256         case STOR_AUTO:
13257         case STOR_STATIC:
13258                 /* These are the good types I am aiming for */
13259                 break;
13260         case STOR_REGISTER:
13261                 type->type &= ~STOR_MASK;
13262                 type->type |= STOR_AUTO;
13263                 break;
13264         case STOR_LOCAL:
13265         case STOR_EXTERN:
13266                 type->type &= ~STOR_MASK;
13267                 type->type |= STOR_STATIC;
13268                 break;
13269         case STOR_TYPEDEF:
13270                 if (!ident) {
13271                         error(state, 0, "typedef without name");
13272                 }
13273                 symbol(state, ident, &ident->sym_ident, 0, type);
13274                 ident->tok = TOK_TYPE_NAME;
13275                 return 0;
13276                 break;
13277         default:
13278                 internal_error(state, 0, "Undefined storage class");
13279         }
13280         if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13281                 error(state, 0, "Function prototypes not supported");
13282         }
13283         if (ident && 
13284                 ((type->type & STOR_MASK) == STOR_STATIC) &&
13285                 ((type->type & QUAL_CONST) == 0)) {
13286                 error(state, 0, "non const static variables not supported");
13287         }
13288         if (ident) {
13289                 def = variable(state, type);
13290                 var_symbol(state, ident, def);
13291         }
13292         return def;
13293 }
13294
13295 static void decl(struct compile_state *state, struct triple *first)
13296 {
13297         struct type *base_type, *type;
13298         struct hash_entry *ident;
13299         struct triple *def;
13300         int global;
13301         global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13302         base_type = decl_specifiers(state);
13303         ident = 0;
13304         type = declarator(state, base_type, &ident, 0);
13305         type->type = attributes_opt(state, type->type);
13306         if (global && ident && (peek(state) == TOK_LBRACE)) {
13307                 /* function */
13308                 type->type_ident = ident;
13309                 state->function = ident->name;
13310                 def = function_definition(state, type);
13311                 symbol(state, ident, &ident->sym_ident, def, type);
13312                 state->function = 0;
13313         }
13314         else {
13315                 int done;
13316                 flatten(state, first, do_decl(state, type, ident));
13317                 /* type or variable definition */
13318                 do {
13319                         done = 1;
13320                         if (peek(state) == TOK_EQ) {
13321                                 if (!ident) {
13322                                         error(state, 0, "cannot assign to a type");
13323                                 }
13324                                 eat(state, TOK_EQ);
13325                                 flatten(state, first,
13326                                         init_expr(state, 
13327                                                 ident->sym_ident->def, 
13328                                                 initializer(state, type)));
13329                         }
13330                         arrays_complete(state, type);
13331                         if (peek(state) == TOK_COMMA) {
13332                                 eat(state, TOK_COMMA);
13333                                 ident = 0;
13334                                 type = declarator(state, base_type, &ident, 0);
13335                                 flatten(state, first, do_decl(state, type, ident));
13336                                 done = 0;
13337                         }
13338                 } while(!done);
13339                 eat(state, TOK_SEMI);
13340         }
13341 }
13342
13343 static void decls(struct compile_state *state)
13344 {
13345         struct triple *list;
13346         int tok;
13347         list = label(state);
13348         while(1) {
13349                 tok = peek(state);
13350                 if (tok == TOK_EOF) {
13351                         return;
13352                 }
13353                 if (tok == TOK_SPACE) {
13354                         eat(state, TOK_SPACE);
13355                 }
13356                 decl(state, list);
13357                 if (list->next != list) {
13358                         error(state, 0, "global variables not supported");
13359                 }
13360         }
13361 }
13362
13363 /* 
13364  * Function inlining
13365  */
13366 struct triple_reg_set {
13367         struct triple_reg_set *next;
13368         struct triple *member;
13369         struct triple *new;
13370 };
13371 struct reg_block {
13372         struct block *block;
13373         struct triple_reg_set *in;
13374         struct triple_reg_set *out;
13375         int vertex;
13376 };
13377 static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13378 static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13379 static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13380 static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13381 static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13382         void (*cb)(struct compile_state *state, struct block *block, void *arg),
13383         void *arg);
13384 static void print_block(
13385         struct compile_state *state, struct block *block, void *arg);
13386 static int do_triple_set(struct triple_reg_set **head, 
13387         struct triple *member, struct triple *new_member);
13388 static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13389 static struct reg_block *compute_variable_lifetimes(
13390         struct compile_state *state, struct basic_blocks *bb);
13391 static void free_variable_lifetimes(struct compile_state *state, 
13392         struct basic_blocks *bb, struct reg_block *blocks);
13393 static void print_live_variables(struct compile_state *state, 
13394         struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
13395
13396
13397 static struct triple *call(struct compile_state *state,
13398         struct triple *retvar, struct triple *ret_addr, 
13399         struct triple *targ, struct triple *ret)
13400 {
13401         struct triple *call;
13402
13403         if (!retvar || !is_lvalue(state, retvar)) {
13404                 internal_error(state, 0, "writing to a non lvalue?");
13405         }
13406         write_compatible(state, retvar->type, &void_ptr_type);
13407
13408         call = new_triple(state, OP_CALL, &void_type, 1, 0);
13409         TARG(call, 0) = targ;
13410         MISC(call, 0) = ret;
13411         if (!targ || (targ->op != OP_LABEL)) {
13412                 internal_error(state, 0, "call not to a label");
13413         }
13414         if (!ret || (ret->op != OP_RET)) {
13415                 internal_error(state, 0, "call not matched with return");
13416         }
13417         return call;
13418 }
13419
13420 static void walk_functions(struct compile_state *state,
13421         void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13422         void *arg)
13423 {
13424         struct triple *func, *first;
13425         func = first = state->functions;
13426         do {
13427                 cb(state, func, arg);
13428                 func = func->next;
13429         } while(func != first);
13430 }
13431
13432 static void reverse_walk_functions(struct compile_state *state,
13433         void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13434         void *arg)
13435 {
13436         struct triple *func, *first;
13437         func = first = state->functions;
13438         do {
13439                 func = func->prev;
13440                 cb(state, func, arg);
13441         } while(func != first);
13442 }
13443
13444
13445 static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13446 {
13447         struct triple *ptr, *first;
13448         if (func->u.cval == 0) {
13449                 return;
13450         }
13451         ptr = first = RHS(func, 0);
13452         do {
13453                 if (ptr->op == OP_FCALL) {
13454                         struct triple *called_func;
13455                         called_func = MISC(ptr, 0);
13456                         /* Mark the called function as used */
13457                         if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13458                                 called_func->u.cval++;
13459                         }
13460                         /* Remove the called function from the list */
13461                         called_func->prev->next = called_func->next;
13462                         called_func->next->prev = called_func->prev;
13463
13464                         /* Place the called function before me on the list */
13465                         called_func->next       = func;
13466                         called_func->prev       = func->prev;
13467                         called_func->prev->next = called_func;
13468                         called_func->next->prev = called_func;
13469                 }
13470                 ptr = ptr->next;
13471         } while(ptr != first);
13472         func->id |= TRIPLE_FLAG_FLATTENED;
13473 }
13474
13475 static void mark_live_functions(struct compile_state *state)
13476 {
13477         /* Ensure state->main_function is the last function in 
13478          * the list of functions.
13479          */
13480         if ((state->main_function->next != state->functions) ||
13481                 (state->functions->prev != state->main_function)) {
13482                 internal_error(state, 0, 
13483                         "state->main_function is not at the end of the function list ");
13484         }
13485         state->main_function->u.cval = 1;
13486         reverse_walk_functions(state, mark_live, 0);
13487 }
13488
13489 static int local_triple(struct compile_state *state, 
13490         struct triple *func, struct triple *ins)
13491 {
13492         int local = (ins->id & TRIPLE_FLAG_LOCAL);
13493 #if 0
13494         if (!local) {
13495                 FILE *fp = state->errout;
13496                 fprintf(fp, "global: ");
13497                 display_triple(fp, ins);
13498         }
13499 #endif
13500         return local;
13501 }
13502
13503 struct triple *copy_func(struct compile_state *state, struct triple *ofunc, 
13504         struct occurance *base_occurance)
13505 {
13506         struct triple *nfunc;
13507         struct triple *nfirst, *ofirst;
13508         struct triple *new, *old;
13509
13510         if (state->compiler->debug & DEBUG_INLINE) {
13511                 FILE *fp = state->dbgout;
13512                 fprintf(fp, "\n");
13513                 loc(fp, state, 0);
13514                 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13515                 display_func(state, fp, ofunc);
13516                 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
13517         }
13518
13519         /* Make a new copy of the old function */
13520         nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13521         nfirst = 0;
13522         ofirst = old = RHS(ofunc, 0);
13523         do {
13524                 struct triple *new;
13525                 struct occurance *occurance;
13526                 int old_lhs, old_rhs;
13527                 old_lhs = old->lhs;
13528                 old_rhs = old->rhs;
13529                 occurance = inline_occurance(state, base_occurance, old->occurance);
13530                 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13531                         MISC(old, 0)->u.cval += 1;
13532                 }
13533                 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13534                         occurance);
13535                 if (!triple_stores_block(state, new)) {
13536                         memcpy(&new->u, &old->u, sizeof(new->u));
13537                 }
13538                 if (!nfirst) {
13539                         RHS(nfunc, 0) = nfirst = new;
13540                 }
13541                 else {
13542                         insert_triple(state, nfirst, new);
13543                 }
13544                 new->id |= TRIPLE_FLAG_FLATTENED;
13545                 new->id |= old->id & TRIPLE_FLAG_COPY;
13546                 
13547                 /* During the copy remember new as user of old */
13548                 use_triple(old, new);
13549
13550                 /* Remember which instructions are local */
13551                 old->id |= TRIPLE_FLAG_LOCAL;
13552                 old = old->next;
13553         } while(old != ofirst);
13554
13555         /* Make a second pass to fix up any unresolved references */
13556         old = ofirst;
13557         new = nfirst;
13558         do {
13559                 struct triple **oexpr, **nexpr;
13560                 int count, i;
13561                 /* Lookup where the copy is, to join pointers */
13562                 count = TRIPLE_SIZE(old);
13563                 for(i = 0; i < count; i++) {
13564                         oexpr = &old->param[i];
13565                         nexpr = &new->param[i];
13566                         if (*oexpr && !*nexpr) {
13567                                 if (!local_triple(state, ofunc, *oexpr)) {
13568                                         *nexpr = *oexpr;
13569                                 }
13570                                 else if ((*oexpr)->use) {
13571                                         *nexpr = (*oexpr)->use->member;
13572                                 }
13573                                 if (*nexpr == old) {
13574                                         internal_error(state, 0, "new == old?");
13575                                 }
13576                                 use_triple(*nexpr, new);
13577                         }
13578                         if (!*nexpr && *oexpr) {
13579                                 internal_error(state, 0, "Could not copy %d", i);
13580                         }
13581                 }
13582                 old = old->next;
13583                 new = new->next;
13584         } while((old != ofirst) && (new != nfirst));
13585         
13586         /* Make a third pass to cleanup the extra useses */
13587         old = ofirst;
13588         new = nfirst;
13589         do {
13590                 unuse_triple(old, new);
13591                 /* Forget which instructions are local */
13592                 old->id &= ~TRIPLE_FLAG_LOCAL;
13593                 old = old->next;
13594                 new = new->next;
13595         } while ((old != ofirst) && (new != nfirst));
13596         return nfunc;
13597 }
13598
13599 static void expand_inline_call(
13600         struct compile_state *state, struct triple *me, struct triple *fcall)
13601 {
13602         /* Inline the function call */
13603         struct type *ptype;
13604         struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
13605         struct triple *end, *nend;
13606         int pvals, i;
13607
13608         /* Find the triples */
13609         ofunc = MISC(fcall, 0);
13610         if (ofunc->op != OP_LIST) {
13611                 internal_error(state, 0, "improper function");
13612         }
13613         nfunc = copy_func(state, ofunc, fcall->occurance);
13614         /* Prepend the parameter reading into the new function list */
13615         ptype = nfunc->type->right;
13616         pvals = fcall->rhs;
13617         for(i = 0; i < pvals; i++) {
13618                 struct type *atype;
13619                 struct triple *arg, *param;
13620                 atype = ptype;
13621                 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13622                         atype = ptype->left;
13623                 }
13624                 param = farg(state, nfunc, i);
13625                 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13626                         internal_error(state, fcall, "param %d type mismatch", i);
13627                 }
13628                 arg = RHS(fcall, i);
13629                 flatten(state, fcall, write_expr(state, param, arg));
13630                 ptype = ptype->right;
13631         }
13632         result = 0;
13633         if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
13634                 result = read_expr(state, 
13635                         deref_index(state, fresult(state, nfunc), 1));
13636         }
13637         if (state->compiler->debug & DEBUG_INLINE) {
13638                 FILE *fp = state->dbgout;
13639                 fprintf(fp, "\n");
13640                 loc(fp, state, 0);
13641                 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13642                 display_func(state, fp, nfunc);
13643                 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
13644         }
13645
13646         /* 
13647          * Get rid of the extra triples 
13648          */
13649         /* Remove the read of the return address */
13650         ins = RHS(nfunc, 0)->prev->prev;
13651         if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13652                 internal_error(state, ins, "Not return addres read?");
13653         }
13654         release_triple(state, ins);
13655         /* Remove the return instruction */
13656         ins = RHS(nfunc, 0)->prev;
13657         if (ins->op != OP_RET) {
13658                 internal_error(state, ins, "Not return?");
13659         }
13660         release_triple(state, ins);
13661         /* Remove the retaddres variable */
13662         retvar = fretaddr(state, nfunc);
13663         if ((retvar->lhs != 1) || 
13664                 (retvar->op != OP_ADECL) ||
13665                 (retvar->next->op != OP_PIECE) ||
13666                 (MISC(retvar->next, 0) != retvar)) {
13667                 internal_error(state, retvar, "Not the return address?");
13668         }
13669         release_triple(state, retvar->next);
13670         release_triple(state, retvar);
13671
13672         /* Remove the label at the start of the function */
13673         ins = RHS(nfunc, 0);
13674         if (ins->op != OP_LABEL) {
13675                 internal_error(state, ins, "Not label?");
13676         }
13677         nfirst = ins->next;
13678         free_triple(state, ins);
13679         /* Release the new function header */
13680         RHS(nfunc, 0) = 0;
13681         free_triple(state, nfunc);
13682
13683         /* Append the new function list onto the return list */
13684         end = fcall->prev;
13685         nend = nfirst->prev;
13686         end->next    = nfirst;
13687         nfirst->prev = end;
13688         nend->next   = fcall;
13689         fcall->prev  = nend;
13690
13691         /* Now the result reading code */
13692         if (result) {
13693                 result = flatten(state, fcall, result);
13694                 propogate_use(state, fcall, result);
13695         }
13696
13697         /* Release the original fcall instruction */
13698         release_triple(state, fcall);
13699
13700         return;
13701 }
13702
13703 /*
13704  *
13705  * Type of the result variable.
13706  * 
13707  *                                     result
13708  *                                        |
13709  *                             +----------+------------+
13710  *                             |                       |
13711  *                     union of closures         result_type
13712  *                             |
13713  *          +------------------+---------------+
13714  *          |                                  |
13715  *       closure1                    ...   closuerN
13716  *          |                                  | 
13717  *  +----+--+-+--------+-----+       +----+----+---+-----+
13718  *  |    |    |        |     |       |    |        |     |
13719  * var1 var2 var3 ... varN result   var1 var2 ... varN result
13720  *                           |
13721  *                  +--------+---------+
13722  *                  |                  |
13723  *          union of closures     result_type
13724  *                  |
13725  *            +-----+-------------------+
13726  *            |                         |
13727  *         closure1            ...  closureN
13728  *            |                         |
13729  *  +-----+---+----+----+      +----+---+----+-----+
13730  *  |     |        |    |      |    |        |     |
13731  * var1 var2 ... varN result  var1 var2 ... varN result
13732  */
13733
13734 static int add_closure_type(struct compile_state *state, 
13735         struct triple *func, struct type *closure_type)
13736 {
13737         struct type *type, *ctype, **next;
13738         struct triple *var, *new_var;
13739         int i;
13740
13741 #if 0
13742         FILE *fp = state->errout;
13743         fprintf(fp, "original_type: ");
13744         name_of(fp, fresult(state, func)->type);
13745         fprintf(fp, "\n");
13746 #endif
13747         /* find the original type */
13748         var = fresult(state, func);
13749         type = var->type;
13750         if (type->elements != 2) {
13751                 internal_error(state, var, "bad return type");
13752         }
13753
13754         /* Find the complete closure type and update it */
13755         ctype = type->left->left;
13756         next = &ctype->left;
13757         while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13758                 next = &(*next)->right;
13759         }
13760         *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13761         ctype->elements += 1;
13762
13763 #if 0
13764         fprintf(fp, "new_type: ");
13765         name_of(fp, type);
13766         fprintf(fp, "\n");
13767         fprintf(fp, "ctype: %p %d bits: %d ", 
13768                 ctype, ctype->elements, reg_size_of(state, ctype));
13769         name_of(fp, ctype);
13770         fprintf(fp, "\n");
13771 #endif
13772         
13773         /* Regenerate the variable with the new type definition */
13774         new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13775         new_var->id |= TRIPLE_FLAG_FLATTENED;
13776         for(i = 0; i < new_var->lhs; i++) {
13777                 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13778         }
13779         
13780         /* Point everyone at the new variable */
13781         propogate_use(state, var, new_var);
13782
13783         /* Release the original variable */
13784         for(i = 0; i < var->lhs; i++) {
13785                 release_triple(state, LHS(var, i));
13786         }
13787         release_triple(state, var);
13788         
13789         /* Return the index of the added closure type */
13790         return ctype->elements - 1;
13791 }
13792
13793 static struct triple *closure_expr(struct compile_state *state,
13794         struct triple *func, int closure_idx, int var_idx)
13795 {
13796         return deref_index(state,
13797                 deref_index(state,
13798                         deref_index(state, fresult(state, func), 0),
13799                         closure_idx),
13800                 var_idx);
13801 }
13802
13803
13804 static void insert_triple_set(
13805         struct triple_reg_set **head, struct triple *member)
13806 {
13807         struct triple_reg_set *new;
13808         new = xcmalloc(sizeof(*new), "triple_set");
13809         new->member = member;
13810         new->new    = 0;
13811         new->next   = *head;
13812         *head       = new;
13813 }
13814
13815 static int ordered_triple_set(
13816         struct triple_reg_set **head, struct triple *member)
13817 {
13818         struct triple_reg_set **ptr;
13819         if (!member)
13820                 return 0;
13821         ptr = head;
13822         while(*ptr) {
13823                 if (member == (*ptr)->member) {
13824                         return 0;
13825                 }
13826                 /* keep the list ordered */
13827                 if (member->id < (*ptr)->member->id) {
13828                         break;
13829                 }
13830                 ptr = &(*ptr)->next;
13831         }
13832         insert_triple_set(ptr, member);
13833         return 1;
13834 }
13835
13836
13837 static void free_closure_variables(struct compile_state *state,
13838         struct triple_reg_set **enclose)
13839 {
13840         struct triple_reg_set *entry, *next;
13841         for(entry = *enclose; entry; entry = next) {
13842                 next = entry->next;
13843                 do_triple_unset(enclose, entry->member);
13844         }
13845 }
13846
13847 static int lookup_closure_index(struct compile_state *state,
13848         struct triple *me, struct triple *val)
13849 {
13850         struct triple *first, *ins, *next;
13851         first = RHS(me, 0);
13852         ins = next = first;
13853         do {
13854                 struct triple *result;
13855                 struct triple *index0, *index1, *index2, *read, *write;
13856                 ins = next;
13857                 next = ins->next;
13858                 if (ins->op != OP_CALL) {
13859                         continue;
13860                 }
13861                 /* I am at a previous call point examine it closely */
13862                 if (ins->next->op != OP_LABEL) {
13863                         internal_error(state, ins, "call not followed by label");
13864                 }
13865                 /* Does this call does not enclose any variables? */
13866                 if ((ins->next->next->op != OP_INDEX) ||
13867                         (ins->next->next->u.cval != 0) ||
13868                         (result = MISC(ins->next->next, 0)) ||
13869                         (result->id & TRIPLE_FLAG_LOCAL)) {
13870                         continue;
13871                 }
13872                 index0 = ins->next->next;
13873                 /* The pattern is:
13874                  * 0 index result < 0 >
13875                  * 1 index 0 < ? >
13876                  * 2 index 1 < ? >
13877                  * 3 read  2
13878                  * 4 write 3 var
13879                  */
13880                 for(index0 = ins->next->next;
13881                         (index0->op == OP_INDEX) &&
13882                                 (MISC(index0, 0) == result) &&
13883                                 (index0->u.cval == 0) ; 
13884                         index0 = write->next)
13885                 {
13886                         index1 = index0->next;
13887                         index2 = index1->next;
13888                         read   = index2->next;
13889                         write  = read->next;
13890                         if ((index0->op != OP_INDEX) ||
13891                                 (index1->op != OP_INDEX) ||
13892                                 (index2->op != OP_INDEX) ||
13893                                 (read->op != OP_READ) ||
13894                                 (write->op != OP_WRITE) ||
13895                                 (MISC(index1, 0) != index0) ||
13896                                 (MISC(index2, 0) != index1) ||
13897                                 (RHS(read, 0) != index2) ||
13898                                 (RHS(write, 0) != read)) {
13899                                 internal_error(state, index0, "bad var read");
13900                         }
13901                         if (MISC(write, 0) == val) {
13902                                 return index2->u.cval;
13903                         }
13904                 }
13905         } while(next != first);
13906         return -1;
13907 }
13908
13909 static inline int enclose_triple(struct triple *ins)
13910 {
13911         return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
13912 }
13913
13914 static void compute_closure_variables(struct compile_state *state,
13915         struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
13916 {
13917         struct triple_reg_set *set, *vars, **last_var;
13918         struct basic_blocks bb;
13919         struct reg_block *rb;
13920         struct block *block;
13921         struct triple *old_result, *first, *ins;
13922         size_t count, idx;
13923         unsigned long used_indicies;
13924         int i, max_index;
13925 #define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
13926 #define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
13927         struct { 
13928                 unsigned id;
13929                 int index;
13930         } *info;
13931
13932         
13933         /* Find the basic blocks of this function */
13934         bb.func = me;
13935         bb.first = RHS(me, 0);
13936         old_result = 0;
13937         if (!triple_is_ret(state, bb.first->prev)) {
13938                 bb.func = 0;
13939         } else {
13940                 old_result = fresult(state, me);
13941         }
13942         analyze_basic_blocks(state, &bb);
13943
13944         /* Find which variables are currently alive in a given block */
13945         rb = compute_variable_lifetimes(state, &bb);
13946
13947         /* Find the variables that are currently alive */
13948         block = block_of_triple(state, fcall);
13949         if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
13950                 internal_error(state, fcall, "No reg block? block: %p", block);
13951         }
13952
13953 #if DEBUG_EXPLICIT_CLOSURES
13954         print_live_variables(state, &bb, rb, state->dbgout);
13955         fflush(state->dbgout);
13956 #endif
13957
13958         /* Count the number of triples in the function */
13959         first = RHS(me, 0);
13960         ins = first;
13961         count = 0;
13962         do {
13963                 count++;
13964                 ins = ins->next;
13965         } while(ins != first);
13966
13967         /* Allocate some memory to temorary hold the id info */
13968         info = xcmalloc(sizeof(*info) * (count +1), "info");
13969
13970         /* Mark the local function */
13971         first = RHS(me, 0);
13972         ins = first;
13973         idx = 1;
13974         do {
13975                 info[idx].id = ins->id;
13976                 ins->id = TRIPLE_FLAG_LOCAL | idx;
13977                 idx++;
13978                 ins = ins->next;
13979         } while(ins != first);
13980
13981         /* 
13982          * Build the list of variables to enclose.
13983          *
13984          * A target it to put the same variable in the
13985          * same slot for ever call of a given function.
13986          * After coloring this removes all of the variable
13987          * manipulation code.
13988          *
13989          * The list of variables to enclose is built ordered
13990          * program order because except in corner cases this
13991          * gives me the stability of assignment I need.
13992          *
13993          * To gurantee that stability I lookup the variables
13994          * to see where they have been used before and
13995          * I build my final list with the assigned indicies.
13996          */
13997         vars = 0;
13998         if (enclose_triple(old_result)) {
13999                 ordered_triple_set(&vars, old_result);
14000         }
14001         for(set = rb[block->vertex].out; set; set = set->next) {
14002                 if (!enclose_triple(set->member)) {
14003                         continue;
14004                 }
14005                 if ((set->member == fcall) || (set->member == old_result)) {
14006                         continue;
14007                 }
14008                 if (!local_triple(state, me, set->member)) {
14009                         internal_error(state, set->member, "not local?");
14010                 }
14011                 ordered_triple_set(&vars, set->member);
14012         }
14013
14014         /* Lookup the current indicies of the live varialbe */
14015         used_indicies = 0;
14016         max_index = -1;
14017         for(set = vars; set ; set = set->next) {
14018                 struct triple *ins;
14019                 int index;
14020                 ins = set->member;
14021                 index  = lookup_closure_index(state, me, ins);
14022                 info[ID_BITS(ins->id)].index = index;
14023                 if (index < 0) {
14024                         continue;
14025                 }
14026                 if (index >= MAX_INDICIES) {
14027                         internal_error(state, ins, "index unexpectedly large");
14028                 }
14029                 if (used_indicies & (1 << index)) {
14030                         internal_error(state, ins, "index previously used?");
14031                 }
14032                 /* Remember which indicies have been used */
14033                 used_indicies |= (1 << index);
14034                 if (index > max_index) {
14035                         max_index = index;
14036                 }
14037         }
14038
14039         /* Walk through the live variables and make certain
14040          * everything is assigned an index.
14041          */
14042         for(set = vars; set; set = set->next) {
14043                 struct triple *ins;
14044                 int index;
14045                 ins = set->member;
14046                 index = info[ID_BITS(ins->id)].index;
14047                 if (index >= 0) {
14048                         continue;
14049                 }
14050                 /* Find the lowest unused index value */
14051                 for(index = 0; index < MAX_INDICIES; index++) {
14052                         if (!(used_indicies & (1 << index))) {
14053                                 break;
14054                         }
14055                 }
14056                 if (index == MAX_INDICIES) {
14057                         internal_error(state, ins, "no free indicies?");
14058                 }
14059                 info[ID_BITS(ins->id)].index = index;
14060                 /* Remember which indicies have been used */
14061                 used_indicies |= (1 << index);
14062                 if (index > max_index) {
14063                         max_index = index;
14064                 }
14065         }
14066
14067         /* Build the return list of variables with positions matching
14068          * their indicies.
14069          */
14070         *enclose = 0;
14071         last_var = enclose;
14072         for(i = 0; i <= max_index; i++) {
14073                 struct triple *var;
14074                 var = 0;
14075                 if (used_indicies & (1 << i)) {
14076                         for(set = vars; set; set = set->next) {
14077                                 int index;
14078                                 index = info[ID_BITS(set->member->id)].index;
14079                                 if (index == i) {
14080                                         var = set->member;
14081                                         break;
14082                                 }
14083                         }
14084                         if (!var) {
14085                                 internal_error(state, me, "missing variable");
14086                         }
14087                 }
14088                 insert_triple_set(last_var, var);
14089                 last_var = &(*last_var)->next;
14090         }
14091
14092 #if DEBUG_EXPLICIT_CLOSURES
14093         /* Print out the variables to be enclosed */
14094         loc(state->dbgout, state, fcall);
14095         fprintf(state->dbgout, "Alive: \n");
14096         for(set = *enclose; set; set = set->next) {
14097                 display_triple(state->dbgout, set->member);
14098         }
14099         fflush(state->dbgout);
14100 #endif
14101
14102         /* Clear the marks */
14103         ins = first;
14104         do {
14105                 ins->id = info[ID_BITS(ins->id)].id;
14106                 ins = ins->next;
14107         } while(ins != first);
14108
14109         /* Release the ordered list of live variables */
14110         free_closure_variables(state, &vars);
14111
14112         /* Release the storage of the old ids */
14113         xfree(info);
14114
14115         /* Release the variable lifetime information */
14116         free_variable_lifetimes(state, &bb, rb);
14117
14118         /* Release the basic blocks of this function */
14119         free_basic_blocks(state, &bb);
14120 }
14121
14122 static void expand_function_call(
14123         struct compile_state *state, struct triple *me, struct triple *fcall)
14124 {
14125         /* Generate an ordinary function call */
14126         struct type *closure_type, **closure_next;
14127         struct triple *func, *func_first, *func_last, *retvar;
14128         struct triple *first;
14129         struct type *ptype, *rtype;
14130         struct triple *jmp;
14131         struct triple *ret_addr, *ret_loc, *ret_set;
14132         struct triple_reg_set *enclose, *set;
14133         int closure_idx, pvals, i;
14134
14135 #if DEBUG_EXPLICIT_CLOSURES
14136         FILE *fp = state->dbgout;
14137         fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14138         display_func(state, fp, MISC(fcall, 0));
14139         display_func(state, fp, me);
14140         fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14141 #endif
14142
14143         /* Find the triples */
14144         func = MISC(fcall, 0);
14145         func_first = RHS(func, 0);
14146         retvar = fretaddr(state, func);
14147         func_last  = func_first->prev;
14148         first = fcall->next;
14149
14150         /* Find what I need to enclose */
14151         compute_closure_variables(state, me, fcall, &enclose);
14152
14153         /* Compute the closure type */
14154         closure_type = new_type(TYPE_TUPLE, 0, 0);
14155         closure_type->elements = 0;
14156         closure_next = &closure_type->left;
14157         for(set = enclose; set ; set = set->next) {
14158                 struct type *type;
14159                 type = &void_type;
14160                 if (set->member) {
14161                         type = set->member->type;
14162                 }
14163                 if (!*closure_next) {
14164                         *closure_next = type;
14165                 } else {
14166                         *closure_next = new_type(TYPE_PRODUCT, *closure_next, 
14167                                 type);
14168                         closure_next = &(*closure_next)->right;
14169                 }
14170                 closure_type->elements += 1;
14171         }
14172         if (closure_type->elements == 0) {
14173                 closure_type->type = TYPE_VOID;
14174         }
14175
14176
14177 #if DEBUG_EXPLICIT_CLOSURES
14178         fprintf(state->dbgout, "closure type: ");
14179         name_of(state->dbgout, closure_type);
14180         fprintf(state->dbgout, "\n");
14181 #endif
14182
14183         /* Update the called functions closure variable */
14184         closure_idx = add_closure_type(state, func, closure_type);
14185
14186         /* Generate some needed triples */
14187         ret_loc = label(state);
14188         ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14189
14190         /* Pass the parameters to the new function */
14191         ptype = func->type->right;
14192         pvals = fcall->rhs;
14193         for(i = 0; i < pvals; i++) {
14194                 struct type *atype;
14195                 struct triple *arg, *param;
14196                 atype = ptype;
14197                 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14198                         atype = ptype->left;
14199                 }
14200                 param = farg(state, func, i);
14201                 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14202                         internal_error(state, fcall, "param type mismatch");
14203                 }
14204                 arg = RHS(fcall, i);
14205                 flatten(state, first, write_expr(state, param, arg));
14206                 ptype = ptype->right;
14207         }
14208         rtype = func->type->left;
14209
14210         /* Thread the triples together */
14211         ret_loc       = flatten(state, first, ret_loc);
14212
14213         /* Save the active variables in the result variable */
14214         for(i = 0, set = enclose; set ; set = set->next, i++) {
14215                 if (!set->member) {
14216                         continue;
14217                 }
14218                 flatten(state, ret_loc,
14219                         write_expr(state,
14220                                 closure_expr(state, func, closure_idx, i),
14221                                 read_expr(state, set->member)));
14222         }
14223
14224         /* Initialize the return value */
14225         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14226                 flatten(state, ret_loc, 
14227                         write_expr(state, 
14228                                 deref_index(state, fresult(state, func), 1),
14229                                 new_triple(state, OP_UNKNOWNVAL, rtype,  0, 0)));
14230         }
14231
14232         ret_addr      = flatten(state, ret_loc, ret_addr);
14233         ret_set       = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14234         jmp           = flatten(state, ret_loc, 
14235                 call(state, retvar, ret_addr, func_first, func_last));
14236
14237         /* Find the result */
14238         if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14239                 struct triple * result;
14240                 result = flatten(state, first, 
14241                         read_expr(state, 
14242                                 deref_index(state, fresult(state, func), 1)));
14243
14244                 propogate_use(state, fcall, result);
14245         }
14246
14247         /* Release the original fcall instruction */
14248         release_triple(state, fcall);
14249
14250         /* Restore the active variables from the result variable */
14251         for(i = 0, set = enclose; set ; set = set->next, i++) {
14252                 struct triple_set *use, *next;
14253                 struct triple *new;
14254                 struct basic_blocks bb;
14255                 if (!set->member || (set->member == fcall)) {
14256                         continue;
14257                 }
14258                 /* Generate an expression for the value */
14259                 new = flatten(state, first,
14260                         read_expr(state, 
14261                                 closure_expr(state, func, closure_idx, i)));
14262
14263
14264                 /* If the original is an lvalue restore the preserved value */
14265                 if (is_lvalue(state, set->member)) {
14266                         flatten(state, first,
14267                                 write_expr(state, set->member, new));
14268                         continue;
14269                 }
14270                 /*
14271                  * If the original is a value update the dominated uses.
14272                  */
14273                 
14274                 /* Analyze the basic blocks so I can see who dominates whom */
14275                 bb.func = me;
14276                 bb.first = RHS(me, 0);
14277                 if (!triple_is_ret(state, bb.first->prev)) {
14278                         bb.func = 0;
14279                 }
14280                 analyze_basic_blocks(state, &bb);
14281                 
14282
14283 #if DEBUG_EXPLICIT_CLOSURES
14284                 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14285                         set->member, new);
14286 #endif
14287                 /* If fcall dominates the use update the expression */
14288                 for(use = set->member->use; use; use = next) {
14289                         /* Replace use modifies the use chain and 
14290                          * removes use, so I must take a copy of the
14291                          * next entry early.
14292                          */
14293                         next = use->next;
14294                         if (!tdominates(state, fcall, use->member)) {
14295                                 continue;
14296                         }
14297                         replace_use(state, set->member, new, use->member);
14298                 }
14299
14300                 /* Release the basic blocks, the instructions will be
14301                  * different next time, and flatten/insert_triple does
14302                  * not update the block values so I can't cache the analysis.
14303                  */
14304                 free_basic_blocks(state, &bb);
14305         }
14306
14307         /* Release the closure variable list */
14308         free_closure_variables(state, &enclose);
14309
14310         if (state->compiler->debug & DEBUG_INLINE) {
14311                 FILE *fp = state->dbgout;
14312                 fprintf(fp, "\n");
14313                 loc(fp, state, 0);
14314                 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14315                 display_func(state, fp, func);
14316                 display_func(state, fp, me);
14317                 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14318         }
14319
14320         return;
14321 }
14322
14323 static int do_inline(struct compile_state *state, struct triple *func)
14324 {
14325         int do_inline;
14326         int policy;
14327
14328         policy = state->compiler->flags & COMPILER_INLINE_MASK;
14329         switch(policy) {
14330         case COMPILER_INLINE_ALWAYS:
14331                 do_inline = 1;
14332                 if (func->type->type & ATTRIB_NOINLINE) {
14333                         error(state, func, "noinline with always_inline compiler option");
14334                 }
14335                 break;
14336         case COMPILER_INLINE_NEVER:
14337                 do_inline = 0;
14338                 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14339                         error(state, func, "always_inline with noinline compiler option");
14340                 }
14341                 break;
14342         case COMPILER_INLINE_DEFAULTON:
14343                 switch(func->type->type & STOR_MASK) {
14344                 case STOR_STATIC | STOR_INLINE:
14345                 case STOR_LOCAL  | STOR_INLINE:
14346                 case STOR_EXTERN | STOR_INLINE:
14347                         do_inline = 1;
14348                         break;
14349                 default:
14350                         do_inline = 1;
14351                         break;
14352                 }
14353                 break;
14354         case COMPILER_INLINE_DEFAULTOFF:
14355                 switch(func->type->type & STOR_MASK) {
14356                 case STOR_STATIC | STOR_INLINE:
14357                 case STOR_LOCAL  | STOR_INLINE:
14358                 case STOR_EXTERN | STOR_INLINE:
14359                         do_inline = 1;
14360                         break;
14361                 default:
14362                         do_inline = 0;
14363                         break;
14364                 }
14365                 break;
14366         case COMPILER_INLINE_NOPENALTY:
14367                 switch(func->type->type & STOR_MASK) {
14368                 case STOR_STATIC | STOR_INLINE:
14369                 case STOR_LOCAL  | STOR_INLINE:
14370                 case STOR_EXTERN | STOR_INLINE:
14371                         do_inline = 1;
14372                         break;
14373                 default:
14374                         do_inline = (func->u.cval == 1);
14375                         break;
14376                 }
14377                 break;
14378         default:
14379                 do_inline = 0;
14380                 internal_error(state, 0, "Unimplemented inline policy");
14381                 break;
14382         }
14383         /* Force inlining */
14384         if (func->type->type & ATTRIB_NOINLINE) {
14385                 do_inline = 0;
14386         }
14387         if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14388                 do_inline = 1;
14389         }
14390         return do_inline;
14391 }
14392
14393 static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14394 {
14395         struct triple *first, *ptr, *next;
14396         /* If the function is not used don't bother */
14397         if (me->u.cval <= 0) {
14398                 return;
14399         }
14400         if (state->compiler->debug & DEBUG_CALLS2) {
14401                 FILE *fp = state->dbgout;
14402                 fprintf(fp, "in: %s\n",
14403                         me->type->type_ident->name);
14404         }
14405
14406         first = RHS(me, 0);
14407         ptr = next = first;
14408         do {
14409                 struct triple *func, *prev;
14410                 ptr = next;
14411                 prev = ptr->prev;
14412                 next = ptr->next;
14413                 if (ptr->op != OP_FCALL) {
14414                         continue;
14415                 }
14416                 func = MISC(ptr, 0);
14417                 /* See if the function should be inlined */
14418                 if (!do_inline(state, func)) {
14419                         /* Put a label after the fcall */
14420                         post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14421                         continue;
14422                 }
14423                 if (state->compiler->debug & DEBUG_CALLS) {
14424                         FILE *fp = state->dbgout;
14425                         if (state->compiler->debug & DEBUG_CALLS2) {
14426                                 loc(fp, state, ptr);
14427                         }
14428                         fprintf(fp, "inlining %s\n",
14429                                 func->type->type_ident->name);
14430                         fflush(fp);
14431                 }
14432
14433                 /* Update the function use counts */
14434                 func->u.cval -= 1;
14435
14436                 /* Replace the fcall with the called function */
14437                 expand_inline_call(state, me, ptr);
14438
14439                 next = prev->next;
14440         } while (next != first);
14441
14442         ptr = next = first;
14443         do {
14444                 struct triple *prev, *func;
14445                 ptr = next;
14446                 prev = ptr->prev;
14447                 next = ptr->next;
14448                 if (ptr->op != OP_FCALL) {
14449                         continue;
14450                 }
14451                 func = MISC(ptr, 0);
14452                 if (state->compiler->debug & DEBUG_CALLS) {
14453                         FILE *fp = state->dbgout;
14454                         if (state->compiler->debug & DEBUG_CALLS2) {
14455                                 loc(fp, state, ptr);
14456                         }
14457                         fprintf(fp, "calling %s\n",
14458                                 func->type->type_ident->name);
14459                         fflush(fp);
14460                 }
14461                 /* Replace the fcall with the instruction sequence
14462                  * needed to make the call.
14463                  */
14464                 expand_function_call(state, me, ptr);
14465                 next = prev->next;
14466         } while(next != first);
14467 }
14468
14469 static void inline_functions(struct compile_state *state, struct triple *func)
14470 {
14471         inline_function(state, func, 0);
14472         reverse_walk_functions(state, inline_function, 0);
14473 }
14474
14475 static void insert_function(struct compile_state *state,
14476         struct triple *func, void *arg)
14477 {
14478         struct triple *first, *end, *ffirst, *fend;
14479
14480         if (state->compiler->debug & DEBUG_INLINE) {
14481                 FILE *fp = state->errout;
14482                 fprintf(fp, "%s func count: %d\n", 
14483                         func->type->type_ident->name, func->u.cval);
14484         }
14485         if (func->u.cval == 0) {
14486                 return;
14487         }
14488
14489         /* Find the end points of the lists */
14490         first  = arg;
14491         end    = first->prev;
14492         ffirst = RHS(func, 0);
14493         fend   = ffirst->prev;
14494
14495         /* splice the lists together */
14496         end->next    = ffirst;
14497         ffirst->prev = end;
14498         fend->next   = first;
14499         first->prev  = fend;
14500 }
14501
14502 struct triple *input_asm(struct compile_state *state)
14503 {
14504         struct asm_info *info;
14505         struct triple *def;
14506         int i, out;
14507         
14508         info = xcmalloc(sizeof(*info), "asm_info");
14509         info->str = "";
14510
14511         out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14512         memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14513
14514         def = new_triple(state, OP_ASM, &void_type, out, 0);
14515         def->u.ainfo = info;
14516         def->id |= TRIPLE_FLAG_VOLATILE;
14517         
14518         for(i = 0; i < out; i++) {
14519                 struct triple *piece;
14520                 piece = triple(state, OP_PIECE, &int_type, def, 0);
14521                 piece->u.cval = i;
14522                 LHS(def, i) = piece;
14523         }
14524
14525         return def;
14526 }
14527
14528 struct triple *output_asm(struct compile_state *state)
14529 {
14530         struct asm_info *info;
14531         struct triple *def;
14532         int in;
14533         
14534         info = xcmalloc(sizeof(*info), "asm_info");
14535         info->str = "";
14536
14537         in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14538         memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14539
14540         def = new_triple(state, OP_ASM, &void_type, 0, in);
14541         def->u.ainfo = info;
14542         def->id |= TRIPLE_FLAG_VOLATILE;
14543         
14544         return def;
14545 }
14546
14547 static void join_functions(struct compile_state *state)
14548 {
14549         struct triple *jmp, *start, *end, *call, *in, *out, *func;
14550         struct file_state file;
14551         struct type *pnext, *param;
14552         struct type *result_type, *args_type;
14553         int idx;
14554
14555         /* Be clear the functions have not been joined yet */
14556         state->functions_joined = 0;
14557
14558         /* Dummy file state to get debug handing right */
14559         memset(&file, 0, sizeof(file));
14560         file.basename = "";
14561         file.line = 0;
14562         file.report_line = 0;
14563         file.report_name = file.basename;
14564         file.prev = state->file;
14565         state->file = &file;
14566         state->function = "";
14567
14568         if (!state->main_function) {
14569                 error(state, 0, "No functions to compile\n");
14570         }
14571
14572         /* The type of arguments */
14573         args_type   = state->main_function->type->right;
14574         /* The return type without any specifiers */
14575         result_type = clone_type(0, state->main_function->type->left);
14576
14577
14578         /* Verify the external arguments */
14579         if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14580                 error(state, state->main_function, 
14581                         "Too many external input arguments");
14582         }
14583         if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14584                 error(state, state->main_function, 
14585                         "Too many external output arguments");
14586         }
14587
14588         /* Lay down the basic program structure */
14589         end           = label(state);
14590         start         = label(state);
14591         start         = flatten(state, state->first, start);
14592         end           = flatten(state, state->first, end);
14593         in            = input_asm(state);
14594         out           = output_asm(state);
14595         call          = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
14596         MISC(call, 0) = state->main_function;
14597         in            = flatten(state, state->first, in);
14598         call          = flatten(state, state->first, call);
14599         out           = flatten(state, state->first, out);
14600
14601
14602         /* Read the external input arguments */
14603         pnext = args_type;
14604         idx = 0;
14605         while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14606                 struct triple *expr;
14607                 param = pnext;
14608                 pnext = 0;
14609                 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14610                         pnext = param->right;
14611                         param = param->left;
14612                 }
14613                 if (registers_of(state, param) != 1) {
14614                         error(state, state->main_function, 
14615                                 "Arg: %d %s requires multiple registers", 
14616                                 idx + 1, param->field_ident->name);
14617                 }
14618                 expr = read_expr(state, LHS(in, idx));
14619                 RHS(call, idx) = expr;
14620                 expr = flatten(state, call, expr);
14621                 use_triple(expr, call);
14622
14623                 idx++;  
14624         }
14625
14626
14627         /* Write the external output arguments */
14628         pnext = result_type;
14629         if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14630                 pnext = result_type->left;
14631         }
14632         for(idx = 0; idx < out->rhs; idx++) {
14633                 struct triple *expr;
14634                 param = pnext;
14635                 pnext = 0;
14636                 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14637                         pnext = param->right;
14638                         param = param->left;
14639                 }
14640                 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14641                         param = 0;
14642                 }
14643                 if (param) {
14644                         if (registers_of(state, param) != 1) {
14645                                 error(state, state->main_function,
14646                                         "Result: %d %s requires multiple registers",
14647                                         idx, param->field_ident->name);
14648                         }
14649                         expr = read_expr(state, call);
14650                         if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14651                                 expr = deref_field(state, expr, param->field_ident);
14652                         }
14653                 } else {
14654                         expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14655                 }
14656                 flatten(state, out, expr);
14657                 RHS(out, idx) = expr;
14658                 use_triple(expr, out);
14659         }
14660
14661         /* Allocate a dummy containing function */
14662         func = triple(state, OP_LIST, 
14663                 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14664         func->type->type_ident = lookup(state, "", 0);
14665         RHS(func, 0) = state->first;
14666         func->u.cval = 1;
14667
14668         /* See which functions are called, and how often */
14669         mark_live_functions(state);
14670         inline_functions(state, func);
14671         walk_functions(state, insert_function, end);
14672
14673         if (start->next != end) {
14674                 jmp = flatten(state, start, branch(state, end, 0));
14675         }
14676
14677         /* OK now the functions have been joined. */
14678         state->functions_joined = 1;
14679
14680         /* Done now cleanup */
14681         state->file = file.prev;
14682         state->function = 0;
14683 }
14684
14685 /*
14686  * Data structurs for optimation.
14687  */
14688
14689
14690 static int do_use_block(
14691         struct block *used, struct block_set **head, struct block *user, 
14692         int front)
14693 {
14694         struct block_set **ptr, *new;
14695         if (!used)
14696                 return 0;
14697         if (!user)
14698                 return 0;
14699         ptr = head;
14700         while(*ptr) {
14701                 if ((*ptr)->member == user) {
14702                         return 0;
14703                 }
14704                 ptr = &(*ptr)->next;
14705         }
14706         new = xcmalloc(sizeof(*new), "block_set");
14707         new->member = user;
14708         if (front) {
14709                 new->next = *head;
14710                 *head = new;
14711         }
14712         else {
14713                 new->next = 0;
14714                 *ptr = new;
14715         }
14716         return 1;
14717 }
14718 static int do_unuse_block(
14719         struct block *used, struct block_set **head, struct block *unuser)
14720 {
14721         struct block_set *use, **ptr;
14722         int count;
14723         count = 0;
14724         ptr = head;
14725         while(*ptr) {
14726                 use = *ptr;
14727                 if (use->member == unuser) {
14728                         *ptr = use->next;
14729                         memset(use, -1, sizeof(*use));
14730                         xfree(use);
14731                         count += 1;
14732                 }
14733                 else {
14734                         ptr = &use->next;
14735                 }
14736         }
14737         return count;
14738 }
14739
14740 static void use_block(struct block *used, struct block *user)
14741 {
14742         int count;
14743         /* Append new to the head of the list, print_block
14744          * depends on this.
14745          */
14746         count = do_use_block(used, &used->use, user, 1); 
14747         used->users += count;
14748 }
14749 static void unuse_block(struct block *used, struct block *unuser)
14750 {
14751         int count;
14752         count = do_unuse_block(used, &used->use, unuser); 
14753         used->users -= count;
14754 }
14755
14756 static void add_block_edge(struct block *block, struct block *edge, int front)
14757 {
14758         int count;
14759         count = do_use_block(block, &block->edges, edge, front);
14760         block->edge_count += count;
14761 }
14762
14763 static void remove_block_edge(struct block *block, struct block *edge)
14764 {
14765         int count;
14766         count = do_unuse_block(block, &block->edges, edge);
14767         block->edge_count -= count;
14768 }
14769
14770 static void idom_block(struct block *idom, struct block *user)
14771 {
14772         do_use_block(idom, &idom->idominates, user, 0);
14773 }
14774
14775 static void unidom_block(struct block *idom, struct block *unuser)
14776 {
14777         do_unuse_block(idom, &idom->idominates, unuser);
14778 }
14779
14780 static void domf_block(struct block *block, struct block *domf)
14781 {
14782         do_use_block(block, &block->domfrontier, domf, 0);
14783 }
14784
14785 static void undomf_block(struct block *block, struct block *undomf)
14786 {
14787         do_unuse_block(block, &block->domfrontier, undomf);
14788 }
14789
14790 static void ipdom_block(struct block *ipdom, struct block *user)
14791 {
14792         do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14793 }
14794
14795 static void unipdom_block(struct block *ipdom, struct block *unuser)
14796 {
14797         do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14798 }
14799
14800 static void ipdomf_block(struct block *block, struct block *ipdomf)
14801 {
14802         do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14803 }
14804
14805 static void unipdomf_block(struct block *block, struct block *unipdomf)
14806 {
14807         do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14808 }
14809
14810 static int walk_triples(
14811         struct compile_state *state, 
14812         int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
14813         void *arg)
14814 {
14815         struct triple *ptr;
14816         int result;
14817         ptr = state->first;
14818         do {
14819                 result = cb(state, ptr, arg);
14820                 if (ptr->next->prev != ptr) {
14821                         internal_error(state, ptr->next, "bad prev");
14822                 }
14823                 ptr = ptr->next;
14824         } while((result == 0) && (ptr != state->first));
14825         return result;
14826 }
14827
14828 #define PRINT_LIST 1
14829 static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
14830 {
14831         FILE *fp = arg;
14832         int op;
14833         op = ins->op;
14834         if (op == OP_LIST) {
14835 #if !PRINT_LIST
14836                 return 0;
14837 #endif
14838         }
14839         if ((op == OP_LABEL) && (ins->use)) {
14840                 fprintf(fp, "\n%p:\n", ins);
14841         }
14842         display_triple(fp, ins);
14843
14844         if (triple_is_branch(state, ins) && ins->use && 
14845                 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
14846                 internal_error(state, ins, "branch used?");
14847         }
14848         if (triple_is_branch(state, ins)) {
14849                 fprintf(fp, "\n");
14850         }
14851         return 0;
14852 }
14853
14854 static void print_triples(struct compile_state *state)
14855 {
14856         if (state->compiler->debug & DEBUG_TRIPLES) {
14857                 FILE *fp = state->dbgout;
14858                 fprintf(fp, "--------------- triples ---------------\n");
14859                 walk_triples(state, do_print_triple, fp);
14860                 fprintf(fp, "\n");
14861         }
14862 }
14863
14864 struct cf_block {
14865         struct block *block;
14866 };
14867 static void find_cf_blocks(struct cf_block *cf, struct block *block)
14868 {
14869         struct block_set *edge;
14870         if (!block || (cf[block->vertex].block == block)) {
14871                 return;
14872         }
14873         cf[block->vertex].block = block;
14874         for(edge = block->edges; edge; edge = edge->next) {
14875                 find_cf_blocks(cf, edge->member);
14876         }
14877 }
14878
14879 static void print_control_flow(struct compile_state *state,
14880         FILE *fp, struct basic_blocks *bb)
14881 {
14882         struct cf_block *cf;
14883         int i;
14884         fprintf(fp, "\ncontrol flow\n");
14885         cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
14886         find_cf_blocks(cf, bb->first_block);
14887
14888         for(i = 1; i <= bb->last_vertex; i++) {
14889                 struct block *block;
14890                 struct block_set *edge;
14891                 block = cf[i].block;
14892                 if (!block)
14893                         continue;
14894                 fprintf(fp, "(%p) %d:", block, block->vertex);
14895                 for(edge = block->edges; edge; edge = edge->next) {
14896                         fprintf(fp, " %d", edge->member->vertex);
14897                 }
14898                 fprintf(fp, "\n");
14899         }
14900
14901         xfree(cf);
14902 }
14903
14904 static void free_basic_block(struct compile_state *state, struct block *block)
14905 {
14906         struct block_set *edge, *entry;
14907         struct block *child;
14908         if (!block) {
14909                 return;
14910         }
14911         if (block->vertex == -1) {
14912                 return;
14913         }
14914         block->vertex = -1;
14915         for(edge = block->edges; edge; edge = edge->next) {
14916                 if (edge->member) {
14917                         unuse_block(edge->member, block);
14918                 }
14919         }
14920         if (block->idom) {
14921                 unidom_block(block->idom, block);
14922         }
14923         block->idom = 0;
14924         if (block->ipdom) {
14925                 unipdom_block(block->ipdom, block);
14926         }
14927         block->ipdom = 0;
14928         while((entry = block->use)) {
14929                 child = entry->member;
14930                 unuse_block(block, child);
14931                 if (child && (child->vertex != -1)) {
14932                         for(edge = child->edges; edge; edge = edge->next) {
14933                                 edge->member = 0;
14934                         }
14935                 }
14936         }
14937         while((entry = block->idominates)) {
14938                 child = entry->member;
14939                 unidom_block(block, child);
14940                 if (child && (child->vertex != -1)) {
14941                         child->idom = 0;
14942                 }
14943         }
14944         while((entry = block->domfrontier)) {
14945                 child = entry->member;
14946                 undomf_block(block, child);
14947         }
14948         while((entry = block->ipdominates)) {
14949                 child = entry->member;
14950                 unipdom_block(block, child);
14951                 if (child && (child->vertex != -1)) {
14952                         child->ipdom = 0;
14953                 }
14954         }
14955         while((entry = block->ipdomfrontier)) {
14956                 child = entry->member;
14957                 unipdomf_block(block, child);
14958         }
14959         if (block->users != 0) {
14960                 internal_error(state, 0, "block still has users");
14961         }
14962         while((edge = block->edges)) {
14963                 child = edge->member;
14964                 remove_block_edge(block, child);
14965                 
14966                 if (child && (child->vertex != -1)) {
14967                         free_basic_block(state, child);
14968                 }
14969         }
14970         memset(block, -1, sizeof(*block));
14971         xfree(block);
14972 }
14973
14974 static void free_basic_blocks(struct compile_state *state, 
14975         struct basic_blocks *bb)
14976 {
14977         struct triple *first, *ins;
14978         free_basic_block(state, bb->first_block);
14979         bb->last_vertex = 0;
14980         bb->first_block = bb->last_block = 0;
14981         first = bb->first;
14982         ins = first;
14983         do {
14984                 if (triple_stores_block(state, ins)) {
14985                         ins->u.block = 0;
14986                 }
14987                 ins = ins->next;
14988         } while(ins != first);
14989         
14990 }
14991
14992 static struct block *basic_block(struct compile_state *state, 
14993         struct basic_blocks *bb, struct triple *first)
14994 {
14995         struct block *block;
14996         struct triple *ptr;
14997         if (!triple_is_label(state, first)) {
14998                 internal_error(state, first, "block does not start with a label");
14999         }
15000         /* See if this basic block has already been setup */
15001         if (first->u.block != 0) {
15002                 return first->u.block;
15003         }
15004         /* Allocate another basic block structure */
15005         bb->last_vertex += 1;
15006         block = xcmalloc(sizeof(*block), "block");
15007         block->first = block->last = first;
15008         block->vertex = bb->last_vertex;
15009         ptr = first;
15010         do {
15011                 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) { 
15012                         break;
15013                 }
15014                 block->last = ptr;
15015                 /* If ptr->u is not used remember where the baic block is */
15016                 if (triple_stores_block(state, ptr)) {
15017                         ptr->u.block = block;
15018                 }
15019                 if (triple_is_branch(state, ptr)) {
15020                         break;
15021                 }
15022                 ptr = ptr->next;
15023         } while (ptr != bb->first);
15024         if ((ptr == bb->first) ||
15025                 ((ptr->next == bb->first) && (
15026                         triple_is_end(state, ptr) || 
15027                         triple_is_ret(state, ptr))))
15028         {
15029                 /* The block has no outflowing edges */
15030         }
15031         else if (triple_is_label(state, ptr)) {
15032                 struct block *next;
15033                 next = basic_block(state, bb, ptr);
15034                 add_block_edge(block, next, 0);
15035                 use_block(next, block);
15036         }
15037         else if (triple_is_branch(state, ptr)) {
15038                 struct triple **expr, *first;
15039                 struct block *child;
15040                 /* Find the branch targets.
15041                  * I special case the first branch as that magically
15042                  * avoids some difficult cases for the register allocator.
15043                  */
15044                 expr = triple_edge_targ(state, ptr, 0);
15045                 if (!expr) {
15046                         internal_error(state, ptr, "branch without targets");
15047                 }
15048                 first = *expr;
15049                 expr = triple_edge_targ(state, ptr, expr);
15050                 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15051                         if (!*expr) continue;
15052                         child = basic_block(state, bb, *expr);
15053                         use_block(child, block);
15054                         add_block_edge(block, child, 0);
15055                 }
15056                 if (first) {
15057                         child = basic_block(state, bb, first);
15058                         use_block(child, block);
15059                         add_block_edge(block, child, 1);
15060
15061                         /* Be certain the return block of a call is
15062                          * in a basic block.  When it is not find
15063                          * start of the block, insert a label if
15064                          * necessary and build the basic block.
15065                          * Then add a fake edge from the start block
15066                          * to the return block of the function.
15067                          */
15068                         if (state->functions_joined && triple_is_call(state, ptr)
15069                                 && !block_of_triple(state, MISC(ptr, 0))) {
15070                                 struct block *tail;
15071                                 struct triple *start;
15072                                 start = triple_to_block_start(state, MISC(ptr, 0));
15073                                 if (!triple_is_label(state, start)) {
15074                                         start = pre_triple(state,
15075                                                 start, OP_LABEL, &void_type, 0, 0);
15076                                 }
15077                                 tail = basic_block(state, bb, start);
15078                                 add_block_edge(child, tail, 0);
15079                                 use_block(tail, child);
15080                         }
15081                 }
15082         }
15083         else {
15084                 internal_error(state, 0, "Bad basic block split");
15085         }
15086 #if 0
15087 {
15088         struct block_set *edge;
15089         FILE *fp = state->errout;
15090         fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15091                 block, block->vertex, 
15092                 block->first, block->last);
15093         for(edge = block->edges; edge; edge = edge->next) {
15094                 fprintf(fp, " %10p [%2d]",
15095                         edge->member ? edge->member->first : 0,
15096                         edge->member ? edge->member->vertex : -1);
15097         }
15098         fprintf(fp, "\n");
15099 }
15100 #endif
15101         return block;
15102 }
15103
15104
15105 static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15106         void (*cb)(struct compile_state *state, struct block *block, void *arg),
15107         void *arg)
15108 {
15109         struct triple *ptr, *first;
15110         struct block *last_block;
15111         last_block = 0;
15112         first = bb->first;
15113         ptr = first;
15114         do {
15115                 if (triple_stores_block(state, ptr)) {
15116                         struct block *block;
15117                         block = ptr->u.block;
15118                         if (block && (block != last_block)) {
15119                                 cb(state, block, arg);
15120                         }
15121                         last_block = block;
15122                 }
15123                 ptr = ptr->next;
15124         } while(ptr != first);
15125 }
15126
15127 static void print_block(
15128         struct compile_state *state, struct block *block, void *arg)
15129 {
15130         struct block_set *user, *edge;
15131         struct triple *ptr;
15132         FILE *fp = arg;
15133
15134         fprintf(fp, "\nblock: %p (%d) ",
15135                 block, 
15136                 block->vertex);
15137
15138         for(edge = block->edges; edge; edge = edge->next) {
15139                 fprintf(fp, " %p<-%p",
15140                         edge->member,
15141                         (edge->member && edge->member->use)?
15142                         edge->member->use->member : 0);
15143         }
15144         fprintf(fp, "\n");
15145         if (block->first->op == OP_LABEL) {
15146                 fprintf(fp, "%p:\n", block->first);
15147         }
15148         for(ptr = block->first; ; ) {
15149                 display_triple(fp, ptr);
15150                 if (ptr == block->last)
15151                         break;
15152                 ptr = ptr->next;
15153                 if (ptr == block->first) {
15154                         internal_error(state, 0, "missing block last?");
15155                 }
15156         }
15157         fprintf(fp, "users %d: ", block->users);
15158         for(user = block->use; user; user = user->next) {
15159                 fprintf(fp, "%p (%d) ", 
15160                         user->member,
15161                         user->member->vertex);
15162         }
15163         fprintf(fp,"\n\n");
15164 }
15165
15166
15167 static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15168 {
15169         fprintf(fp, "--------------- blocks ---------------\n");
15170         walk_blocks(state, &state->bb, print_block, fp);
15171 }
15172 static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15173 {
15174         static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb);
15175         static void print_dominance_frontiers(struct compile_state *state, FILE *fp, struct basic_blocks *bb);
15176         if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15177                 fprintf(fp, "After %s\n", func);
15178                 romcc_print_blocks(state, fp);
15179                 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15180                         print_dominators(state, fp, &state->bb);
15181                         print_dominance_frontiers(state, fp, &state->bb);
15182                 }
15183                 print_control_flow(state, fp, &state->bb);
15184         }
15185 }
15186
15187 static void prune_nonblock_triples(struct compile_state *state, 
15188         struct basic_blocks *bb)
15189 {
15190         struct block *block;
15191         struct triple *first, *ins, *next;
15192         /* Delete the triples not in a basic block */
15193         block = 0;
15194         first = bb->first;
15195         ins = first;
15196         do {
15197                 next = ins->next;
15198                 if (ins->op == OP_LABEL) {
15199                         block = ins->u.block;
15200                 }
15201                 if (!block) {
15202                         struct triple_set *use;
15203                         for(use = ins->use; use; use = use->next) {
15204                                 struct block *block;
15205                                 block = block_of_triple(state, use->member);
15206                                 if (block != 0) {
15207                                         internal_error(state, ins, "pruning used ins?");
15208                                 }
15209                         }
15210                         release_triple(state, ins);
15211                 }
15212                 if (block && block->last == ins) {
15213                         block = 0;
15214                 }
15215                 ins = next;
15216         } while(ins != first);
15217 }
15218
15219 static void setup_basic_blocks(struct compile_state *state, 
15220         struct basic_blocks *bb)
15221 {
15222         if (!triple_stores_block(state, bb->first)) {
15223                 internal_error(state, 0, "ins will not store block?");
15224         }
15225         /* Initialize the state */
15226         bb->first_block = bb->last_block = 0;
15227         bb->last_vertex = 0;
15228         free_basic_blocks(state, bb);
15229
15230         /* Find the basic blocks */
15231         bb->first_block = basic_block(state, bb, bb->first);
15232
15233         /* Be certain the last instruction of a function, or the
15234          * entire program is in a basic block.  When it is not find 
15235          * the start of the block, insert a label if necessary and build 
15236          * basic block.  Then add a fake edge from the start block
15237          * to the final block.
15238          */
15239         if (!block_of_triple(state, bb->first->prev)) {
15240                 struct triple *start;
15241                 struct block *tail;
15242                 start = triple_to_block_start(state, bb->first->prev);
15243                 if (!triple_is_label(state, start)) {
15244                         start = pre_triple(state,
15245                                 start, OP_LABEL, &void_type, 0, 0);
15246                 }
15247                 tail = basic_block(state, bb, start);
15248                 add_block_edge(bb->first_block, tail, 0);
15249                 use_block(tail, bb->first_block);
15250         }
15251         
15252         /* Find the last basic block.
15253          */
15254         bb->last_block = block_of_triple(state, bb->first->prev);
15255
15256         /* Delete the triples not in a basic block */
15257         prune_nonblock_triples(state, bb);
15258
15259 #if 0
15260         /* If we are debugging print what I have just done */
15261         if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15262                 print_blocks(state, state->dbgout);
15263                 print_control_flow(state, bb);
15264         }
15265 #endif
15266 }
15267
15268
15269 struct sdom_block {
15270         struct block *block;
15271         struct sdom_block *sdominates;
15272         struct sdom_block *sdom_next;
15273         struct sdom_block *sdom;
15274         struct sdom_block *label;
15275         struct sdom_block *parent;
15276         struct sdom_block *ancestor;
15277         int vertex;
15278 };
15279
15280
15281 static void unsdom_block(struct sdom_block *block)
15282 {
15283         struct sdom_block **ptr;
15284         if (!block->sdom_next) {
15285                 return;
15286         }
15287         ptr = &block->sdom->sdominates;
15288         while(*ptr) {
15289                 if ((*ptr) == block) {
15290                         *ptr = block->sdom_next;
15291                         return;
15292                 }
15293                 ptr = &(*ptr)->sdom_next;
15294         }
15295 }
15296
15297 static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15298 {
15299         unsdom_block(block);
15300         block->sdom = sdom;
15301         block->sdom_next = sdom->sdominates;
15302         sdom->sdominates = block;
15303 }
15304
15305
15306
15307 static int initialize_sdblock(struct sdom_block *sd,
15308         struct block *parent, struct block *block, int vertex)
15309 {
15310         struct block_set *edge;
15311         if (!block || (sd[block->vertex].block == block)) {
15312                 return vertex;
15313         }
15314         vertex += 1;
15315         /* Renumber the blocks in a convinient fashion */
15316         block->vertex = vertex;
15317         sd[vertex].block    = block;
15318         sd[vertex].sdom     = &sd[vertex];
15319         sd[vertex].label    = &sd[vertex];
15320         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
15321         sd[vertex].ancestor = 0;
15322         sd[vertex].vertex   = vertex;
15323         for(edge = block->edges; edge; edge = edge->next) {
15324                 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15325         }
15326         return vertex;
15327 }
15328
15329 static int initialize_spdblock(
15330         struct compile_state *state, struct sdom_block *sd,
15331         struct block *parent, struct block *block, int vertex)
15332 {
15333         struct block_set *user;
15334         if (!block || (sd[block->vertex].block == block)) {
15335                 return vertex;
15336         }
15337         vertex += 1;
15338         /* Renumber the blocks in a convinient fashion */
15339         block->vertex = vertex;
15340         sd[vertex].block    = block;
15341         sd[vertex].sdom     = &sd[vertex];
15342         sd[vertex].label    = &sd[vertex];
15343         sd[vertex].parent   = parent? &sd[parent->vertex] : 0;
15344         sd[vertex].ancestor = 0;
15345         sd[vertex].vertex   = vertex;
15346         for(user = block->use; user; user = user->next) {
15347                 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
15348         }
15349         return vertex;
15350 }
15351
15352 static int setup_spdblocks(struct compile_state *state, 
15353         struct basic_blocks *bb, struct sdom_block *sd)
15354 {
15355         struct block *block;
15356         int vertex;
15357         /* Setup as many sdpblocks as possible without using fake edges */
15358         vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
15359
15360         /* Walk through the graph and find unconnected blocks.  Add a
15361          * fake edge from the unconnected blocks to the end of the
15362          * graph. 
15363          */
15364         block = bb->first_block->last->next->u.block;
15365         for(; block && block != bb->first_block; block = block->last->next->u.block) {
15366                 if (sd[block->vertex].block == block) {
15367                         continue;
15368                 }
15369 #if DEBUG_SDP_BLOCKS
15370                 {
15371                         FILE *fp = state->errout;
15372                         fprintf(fp, "Adding %d\n", vertex +1);
15373                 }
15374 #endif
15375                 add_block_edge(block, bb->last_block, 0);
15376                 use_block(bb->last_block, block);
15377
15378                 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
15379         }
15380         return vertex;
15381 }
15382
15383 static void compress_ancestors(struct sdom_block *v)
15384 {
15385         /* This procedure assumes ancestor(v) != 0 */
15386         /* if (ancestor(ancestor(v)) != 0) {
15387          *      compress(ancestor(ancestor(v)));
15388          *      if (semi(label(ancestor(v))) < semi(label(v))) {
15389          *              label(v) = label(ancestor(v));
15390          *      }
15391          *      ancestor(v) = ancestor(ancestor(v));
15392          * }
15393          */
15394         if (!v->ancestor) {
15395                 return;
15396         }
15397         if (v->ancestor->ancestor) {
15398                 compress_ancestors(v->ancestor->ancestor);
15399                 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15400                         v->label = v->ancestor->label;
15401                 }
15402                 v->ancestor = v->ancestor->ancestor;
15403         }
15404 }
15405
15406 static void compute_sdom(struct compile_state *state, 
15407         struct basic_blocks *bb, struct sdom_block *sd)
15408 {
15409         int i;
15410         /* // step 2 
15411          *  for each v <= pred(w) {
15412          *      u = EVAL(v);
15413          *      if (semi[u] < semi[w] { 
15414          *              semi[w] = semi[u]; 
15415          *      } 
15416          * }
15417          * add w to bucket(vertex(semi[w]));
15418          * LINK(parent(w), w);
15419          *
15420          * // step 3
15421          * for each v <= bucket(parent(w)) {
15422          *      delete v from bucket(parent(w));
15423          *      u = EVAL(v);
15424          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15425          * }
15426          */
15427         for(i = bb->last_vertex; i >= 2; i--) {
15428                 struct sdom_block *v, *parent, *next;
15429                 struct block_set *user;
15430                 struct block *block;
15431                 block = sd[i].block;
15432                 parent = sd[i].parent;
15433                 /* Step 2 */
15434                 for(user = block->use; user; user = user->next) {
15435                         struct sdom_block *v, *u;
15436                         v = &sd[user->member->vertex];
15437                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15438                         if (u->sdom->vertex < sd[i].sdom->vertex) {
15439                                 sd[i].sdom = u->sdom;
15440                         }
15441                 }
15442                 sdom_block(sd[i].sdom, &sd[i]);
15443                 sd[i].ancestor = parent;
15444                 /* Step 3 */
15445                 for(v = parent->sdominates; v; v = next) {
15446                         struct sdom_block *u;
15447                         next = v->sdom_next;
15448                         unsdom_block(v);
15449                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15450                         v->block->idom = (u->sdom->vertex < v->sdom->vertex)? 
15451                                 u->block : parent->block;
15452                 }
15453         }
15454 }
15455
15456 static void compute_spdom(struct compile_state *state, 
15457         struct basic_blocks *bb, struct sdom_block *sd)
15458 {
15459         int i;
15460         /* // step 2 
15461          *  for each v <= pred(w) {
15462          *      u = EVAL(v);
15463          *      if (semi[u] < semi[w] { 
15464          *              semi[w] = semi[u]; 
15465          *      } 
15466          * }
15467          * add w to bucket(vertex(semi[w]));
15468          * LINK(parent(w), w);
15469          *
15470          * // step 3
15471          * for each v <= bucket(parent(w)) {
15472          *      delete v from bucket(parent(w));
15473          *      u = EVAL(v);
15474          *      dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15475          * }
15476          */
15477         for(i = bb->last_vertex; i >= 2; i--) {
15478                 struct sdom_block *u, *v, *parent, *next;
15479                 struct block_set *edge;
15480                 struct block *block;
15481                 block = sd[i].block;
15482                 parent = sd[i].parent;
15483                 /* Step 2 */
15484                 for(edge = block->edges; edge; edge = edge->next) {
15485                         v = &sd[edge->member->vertex];
15486                         u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15487                         if (u->sdom->vertex < sd[i].sdom->vertex) {
15488                                 sd[i].sdom = u->sdom;
15489                         }
15490                 }
15491                 sdom_block(sd[i].sdom, &sd[i]);
15492                 sd[i].ancestor = parent;
15493                 /* Step 3 */
15494                 for(v = parent->sdominates; v; v = next) {
15495                         struct sdom_block *u;
15496                         next = v->sdom_next;
15497                         unsdom_block(v);
15498                         u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15499                         v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)? 
15500                                 u->block : parent->block;
15501                 }
15502         }
15503 }
15504
15505 static void compute_idom(struct compile_state *state, 
15506         struct basic_blocks *bb, struct sdom_block *sd)
15507 {
15508         int i;
15509         for(i = 2; i <= bb->last_vertex; i++) {
15510                 struct block *block;
15511                 block = sd[i].block;
15512                 if (block->idom->vertex != sd[i].sdom->vertex) {
15513                         block->idom = block->idom->idom;
15514                 }
15515                 idom_block(block->idom, block);
15516         }
15517         sd[1].block->idom = 0;
15518 }
15519
15520 static void compute_ipdom(struct compile_state *state, 
15521         struct basic_blocks *bb, struct sdom_block *sd)
15522 {
15523         int i;
15524         for(i = 2; i <= bb->last_vertex; i++) {
15525                 struct block *block;
15526                 block = sd[i].block;
15527                 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15528                         block->ipdom = block->ipdom->ipdom;
15529                 }
15530                 ipdom_block(block->ipdom, block);
15531         }
15532         sd[1].block->ipdom = 0;
15533 }
15534
15535         /* Theorem 1:
15536          *   Every vertex of a flowgraph G = (V, E, r) except r has
15537          *   a unique immediate dominator.  
15538          *   The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15539          *   rooted at r, called the dominator tree of G, such that 
15540          *   v dominates w if and only if v is a proper ancestor of w in
15541          *   the dominator tree.
15542          */
15543         /* Lemma 1:  
15544          *   If v and w are vertices of G such that v <= w,
15545          *   than any path from v to w must contain a common ancestor
15546          *   of v and w in T.
15547          */
15548         /* Lemma 2:  For any vertex w != r, idom(w) -> w */
15549         /* Lemma 3:  For any vertex w != r, sdom(w) -> w */
15550         /* Lemma 4:  For any vertex w != r, idom(w) -> sdom(w) */
15551         /* Theorem 2:
15552          *   Let w != r.  Suppose every u for which sdom(w) -> u -> w satisfies
15553          *   sdom(u) >= sdom(w).  Then idom(w) = sdom(w).
15554          */
15555         /* Theorem 3:
15556          *   Let w != r and let u be a vertex for which sdom(u) is 
15557          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
15558          *   Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15559          */
15560         /* Lemma 5:  Let vertices v,w satisfy v -> w.
15561          *           Then v -> idom(w) or idom(w) -> idom(v)
15562          */
15563
15564 static void find_immediate_dominators(struct compile_state *state,
15565         struct basic_blocks *bb)
15566 {
15567         struct sdom_block *sd;
15568         /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15569          *           vi > w for (1 <= i <= k - 1}
15570          */
15571         /* Theorem 4:
15572          *   For any vertex w != r.
15573          *   sdom(w) = min(
15574          *                 {v|(v,w) <= E  and v < w } U 
15575          *                 {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15576          */
15577         /* Corollary 1:
15578          *   Let w != r and let u be a vertex for which sdom(u) is 
15579          *   minimum amoung vertices u satisfying sdom(w) -> u -> w.
15580          *   Then:
15581          *                   { sdom(w) if sdom(w) = sdom(u),
15582          *        idom(w) = {
15583          *                   { idom(u) otherwise
15584          */
15585         /* The algorithm consists of the following 4 steps.
15586          * Step 1.  Carry out a depth-first search of the problem graph.  
15587          *    Number the vertices from 1 to N as they are reached during
15588          *    the search.  Initialize the variables used in succeeding steps.
15589          * Step 2.  Compute the semidominators of all vertices by applying
15590          *    theorem 4.   Carry out the computation vertex by vertex in
15591          *    decreasing order by number.
15592          * Step 3.  Implicitly define the immediate dominator of each vertex
15593          *    by applying Corollary 1.
15594          * Step 4.  Explicitly define the immediate dominator of each vertex,
15595          *    carrying out the computation vertex by vertex in increasing order
15596          *    by number.
15597          */
15598         /* Step 1 initialize the basic block information */
15599         sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15600         initialize_sdblock(sd, 0, bb->first_block, 0);
15601 #if 0
15602         sd[1].size  = 0;
15603         sd[1].label = 0;
15604         sd[1].sdom  = 0;
15605 #endif
15606         /* Step 2 compute the semidominators */
15607         /* Step 3 implicitly define the immediate dominator of each vertex */
15608         compute_sdom(state, bb, sd);
15609         /* Step 4 explicitly define the immediate dominator of each vertex */
15610         compute_idom(state, bb, sd);
15611         xfree(sd);
15612 }
15613
15614 static void find_post_dominators(struct compile_state *state,
15615         struct basic_blocks *bb)
15616 {
15617         struct sdom_block *sd;
15618         int vertex;
15619         /* Step 1 initialize the basic block information */
15620         sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15621
15622         vertex = setup_spdblocks(state, bb, sd);
15623         if (vertex != bb->last_vertex) {
15624                 internal_error(state, 0, "missing %d blocks",
15625                         bb->last_vertex - vertex);
15626         }
15627
15628         /* Step 2 compute the semidominators */
15629         /* Step 3 implicitly define the immediate dominator of each vertex */
15630         compute_spdom(state, bb, sd);
15631         /* Step 4 explicitly define the immediate dominator of each vertex */
15632         compute_ipdom(state, bb, sd);
15633         xfree(sd);
15634 }
15635
15636
15637
15638 static void find_block_domf(struct compile_state *state, struct block *block)
15639 {
15640         struct block *child;
15641         struct block_set *user, *edge;
15642         if (block->domfrontier != 0) {
15643                 internal_error(state, block->first, "domfrontier present?");
15644         }
15645         for(user = block->idominates; user; user = user->next) {
15646                 child = user->member;
15647                 if (child->idom != block) {
15648                         internal_error(state, block->first, "bad idom");
15649                 }
15650                 find_block_domf(state, child);
15651         }
15652         for(edge = block->edges; edge; edge = edge->next) {
15653                 if (edge->member->idom != block) {
15654                         domf_block(block, edge->member);
15655                 }
15656         }
15657         for(user = block->idominates; user; user = user->next) {
15658                 struct block_set *frontier;
15659                 child = user->member;
15660                 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15661                         if (frontier->member->idom != block) {
15662                                 domf_block(block, frontier->member);
15663                         }
15664                 }
15665         }
15666 }
15667
15668 static void find_block_ipdomf(struct compile_state *state, struct block *block)
15669 {
15670         struct block *child;
15671         struct block_set *user;
15672         if (block->ipdomfrontier != 0) {
15673                 internal_error(state, block->first, "ipdomfrontier present?");
15674         }
15675         for(user = block->ipdominates; user; user = user->next) {
15676                 child = user->member;
15677                 if (child->ipdom != block) {
15678                         internal_error(state, block->first, "bad ipdom");
15679                 }
15680                 find_block_ipdomf(state, child);
15681         }
15682         for(user = block->use; user; user = user->next) {
15683                 if (user->member->ipdom != block) {
15684                         ipdomf_block(block, user->member);
15685                 }
15686         }
15687         for(user = block->ipdominates; user; user = user->next) {
15688                 struct block_set *frontier;
15689                 child = user->member;
15690                 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15691                         if (frontier->member->ipdom != block) {
15692                                 ipdomf_block(block, frontier->member);
15693                         }
15694                 }
15695         }
15696 }
15697
15698 static void print_dominated(
15699         struct compile_state *state, struct block *block, void *arg)
15700 {
15701         struct block_set *user;
15702         FILE *fp = arg;
15703
15704         fprintf(fp, "%d:", block->vertex);
15705         for(user = block->idominates; user; user = user->next) {
15706                 fprintf(fp, " %d", user->member->vertex);
15707                 if (user->member->idom != block) {
15708                         internal_error(state, user->member->first, "bad idom");
15709                 }
15710         }
15711         fprintf(fp,"\n");
15712 }
15713
15714 static void print_dominated2(
15715         struct compile_state *state, FILE *fp, int depth, struct block *block)
15716 {
15717         struct block_set *user;
15718         struct triple *ins;
15719         struct occurance *ptr, *ptr2;
15720         const char *filename1, *filename2;
15721         int equal_filenames;
15722         int i;
15723         for(i = 0; i < depth; i++) {
15724                 fprintf(fp, "   ");
15725         }
15726         fprintf(fp, "%3d: %p (%p - %p) @", 
15727                 block->vertex, block, block->first, block->last);
15728         ins = block->first;
15729         while(ins != block->last && (ins->occurance->line == 0)) {
15730                 ins = ins->next;
15731         }
15732         ptr = ins->occurance;
15733         ptr2 = block->last->occurance;
15734         filename1 = ptr->filename? ptr->filename : "";
15735         filename2 = ptr2->filename? ptr2->filename : "";
15736         equal_filenames = (strcmp(filename1, filename2) == 0);
15737         if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15738                 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15739         } else if (equal_filenames) {
15740                 fprintf(fp, " %s:(%d - %d)",
15741                         ptr->filename, ptr->line, ptr2->line);
15742         } else {
15743                 fprintf(fp, " (%s:%d - %s:%d)",
15744                         ptr->filename, ptr->line,
15745                         ptr2->filename, ptr2->line);
15746         }
15747         fprintf(fp, "\n");
15748         for(user = block->idominates; user; user = user->next) {
15749                 print_dominated2(state, fp, depth + 1, user->member);
15750         }
15751 }
15752
15753 static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
15754 {
15755         fprintf(fp, "\ndominates\n");
15756         walk_blocks(state, bb, print_dominated, fp);
15757         fprintf(fp, "dominates\n");
15758         print_dominated2(state, fp, 0, bb->first_block);
15759 }
15760
15761
15762 static int print_frontiers(
15763         struct compile_state *state, FILE *fp, struct block *block, int vertex)
15764 {
15765         struct block_set *user, *edge;
15766
15767         if (!block || (block->vertex != vertex + 1)) {
15768                 return vertex;
15769         }
15770         vertex += 1;
15771
15772         fprintf(fp, "%d:", block->vertex);
15773         for(user = block->domfrontier; user; user = user->next) {
15774                 fprintf(fp, " %d", user->member->vertex);
15775         }
15776         fprintf(fp, "\n");
15777         
15778         for(edge = block->edges; edge; edge = edge->next) {
15779                 vertex = print_frontiers(state, fp, edge->member, vertex);
15780         }
15781         return vertex;
15782 }
15783 static void print_dominance_frontiers(struct compile_state *state,
15784         FILE *fp, struct basic_blocks *bb)
15785 {
15786         fprintf(fp, "\ndominance frontiers\n");
15787         print_frontiers(state, fp, bb->first_block, 0);
15788         
15789 }
15790
15791 static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
15792 {
15793         /* Find the immediate dominators */
15794         find_immediate_dominators(state, bb);
15795         /* Find the dominance frontiers */
15796         find_block_domf(state, bb->first_block);
15797         /* If debuging print the print what I have just found */
15798         if (state->compiler->debug & DEBUG_FDOMINATORS) {
15799                 print_dominators(state, state->dbgout, bb);
15800                 print_dominance_frontiers(state, state->dbgout, bb);
15801                 print_control_flow(state, state->dbgout, bb);
15802         }
15803 }
15804
15805
15806 static void print_ipdominated(
15807         struct compile_state *state, struct block *block, void *arg)
15808 {
15809         struct block_set *user;
15810         FILE *fp = arg;
15811
15812         fprintf(fp, "%d:", block->vertex);
15813         for(user = block->ipdominates; user; user = user->next) {
15814                 fprintf(fp, " %d", user->member->vertex);
15815                 if (user->member->ipdom != block) {
15816                         internal_error(state, user->member->first, "bad ipdom");
15817                 }
15818         }
15819         fprintf(fp, "\n");
15820 }
15821
15822 static void print_ipdominators(struct compile_state *state, FILE *fp,
15823         struct basic_blocks *bb)
15824 {
15825         fprintf(fp, "\nipdominates\n");
15826         walk_blocks(state, bb, print_ipdominated, fp);
15827 }
15828
15829 static int print_pfrontiers(
15830         struct compile_state *state, FILE *fp, struct block *block, int vertex)
15831 {
15832         struct block_set *user;
15833
15834         if (!block || (block->vertex != vertex + 1)) {
15835                 return vertex;
15836         }
15837         vertex += 1;
15838
15839         fprintf(fp, "%d:", block->vertex);
15840         for(user = block->ipdomfrontier; user; user = user->next) {
15841                 fprintf(fp, " %d", user->member->vertex);
15842         }
15843         fprintf(fp, "\n");
15844         for(user = block->use; user; user = user->next) {
15845                 vertex = print_pfrontiers(state, fp, user->member, vertex);
15846         }
15847         return vertex;
15848 }
15849 static void print_ipdominance_frontiers(struct compile_state *state,
15850         FILE *fp, struct basic_blocks *bb)
15851 {
15852         fprintf(fp, "\nipdominance frontiers\n");
15853         print_pfrontiers(state, fp, bb->last_block, 0);
15854         
15855 }
15856
15857 static void analyze_ipdominators(struct compile_state *state,
15858         struct basic_blocks *bb)
15859 {
15860         /* Find the post dominators */
15861         find_post_dominators(state, bb);
15862         /* Find the control dependencies (post dominance frontiers) */
15863         find_block_ipdomf(state, bb->last_block);
15864         /* If debuging print the print what I have just found */
15865         if (state->compiler->debug & DEBUG_RDOMINATORS) {
15866                 print_ipdominators(state, state->dbgout, bb);
15867                 print_ipdominance_frontiers(state, state->dbgout, bb);
15868                 print_control_flow(state, state->dbgout, bb);
15869         }
15870 }
15871
15872 static int bdominates(struct compile_state *state,
15873         struct block *dom, struct block *sub)
15874 {
15875         while(sub && (sub != dom)) {
15876                 sub = sub->idom;
15877         }
15878         return sub == dom;
15879 }
15880
15881 static int tdominates(struct compile_state *state,
15882         struct triple *dom, struct triple *sub)
15883 {
15884         struct block *bdom, *bsub;
15885         int result;
15886         bdom = block_of_triple(state, dom);
15887         bsub = block_of_triple(state, sub);
15888         if (bdom != bsub) {
15889                 result = bdominates(state, bdom, bsub);
15890         } 
15891         else {
15892                 struct triple *ins;
15893                 if (!bdom || !bsub) {
15894                         internal_error(state, dom, "huh?");
15895                 }
15896                 ins = sub;
15897                 while((ins != bsub->first) && (ins != dom)) {
15898                         ins = ins->prev;
15899                 }
15900                 result = (ins == dom);
15901         }
15902         return result;
15903 }
15904
15905 static void analyze_basic_blocks(
15906         struct compile_state *state, struct basic_blocks *bb)
15907 {
15908         setup_basic_blocks(state, bb);
15909         analyze_idominators(state, bb);
15910         analyze_ipdominators(state, bb);
15911 }
15912
15913 static void insert_phi_operations(struct compile_state *state)
15914 {
15915         size_t size;
15916         struct triple *first;
15917         int *has_already, *work;
15918         struct block *work_list, **work_list_tail;
15919         int iter;
15920         struct triple *var, *vnext;
15921
15922         size = sizeof(int) * (state->bb.last_vertex + 1);
15923         has_already = xcmalloc(size, "has_already");
15924         work =        xcmalloc(size, "work");
15925         iter = 0;
15926
15927         first = state->first;
15928         for(var = first->next; var != first ; var = vnext) {
15929                 struct block *block;
15930                 struct triple_set *user, *unext;
15931                 vnext = var->next;
15932
15933                 if (!triple_is_auto_var(state, var) || !var->use) {
15934                         continue;
15935                 }
15936                         
15937                 iter += 1;
15938                 work_list = 0;
15939                 work_list_tail = &work_list;
15940                 for(user = var->use; user; user = unext) {
15941                         unext = user->next;
15942                         if (MISC(var, 0) == user->member) {
15943                                 continue;
15944                         }
15945                         if (user->member->op == OP_READ) {
15946                                 continue;
15947                         }
15948                         if (user->member->op != OP_WRITE) {
15949                                 internal_error(state, user->member, 
15950                                         "bad variable access");
15951                         }
15952                         block = user->member->u.block;
15953                         if (!block) {
15954                                 warning(state, user->member, "dead code");
15955                                 release_triple(state, user->member);
15956                                 continue;
15957                         }
15958                         if (work[block->vertex] >= iter) {
15959                                 continue;
15960                         }
15961                         work[block->vertex] = iter;
15962                         *work_list_tail = block;
15963                         block->work_next = 0;
15964                         work_list_tail = &block->work_next;
15965                 }
15966                 for(block = work_list; block; block = block->work_next) {
15967                         struct block_set *df;
15968                         for(df = block->domfrontier; df; df = df->next) {
15969                                 struct triple *phi;
15970                                 struct block *front;
15971                                 int in_edges;
15972                                 front = df->member;
15973
15974                                 if (has_already[front->vertex] >= iter) {
15975                                         continue;
15976                                 }
15977                                 /* Count how many edges flow into this block */
15978                                 in_edges = front->users;
15979                                 /* Insert a phi function for this variable */
15980                                 get_occurance(var->occurance);
15981                                 phi = alloc_triple(
15982                                         state, OP_PHI, var->type, -1, in_edges, 
15983                                         var->occurance);
15984                                 phi->u.block = front;
15985                                 MISC(phi, 0) = var;
15986                                 use_triple(var, phi);
15987 #if 1
15988                                 if (phi->rhs != in_edges) {
15989                                         internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
15990                                                 phi->rhs, in_edges);
15991                                 }
15992 #endif
15993                                 /* Insert the phi functions immediately after the label */
15994                                 insert_triple(state, front->first->next, phi);
15995                                 if (front->first == front->last) {
15996                                         front->last = front->first->next;
15997                                 }
15998                                 has_already[front->vertex] = iter;
15999                                 transform_to_arch_instruction(state, phi);
16000
16001                                 /* If necessary plan to visit the basic block */
16002                                 if (work[front->vertex] >= iter) {
16003                                         continue;
16004                                 }
16005                                 work[front->vertex] = iter;
16006                                 *work_list_tail = front;
16007                                 front->work_next = 0;
16008                                 work_list_tail = &front->work_next;
16009                         }
16010                 }
16011         }
16012         xfree(has_already);
16013         xfree(work);
16014 }
16015
16016
16017 struct stack {
16018         struct triple_set *top;
16019         unsigned orig_id;
16020 };
16021
16022 static int count_auto_vars(struct compile_state *state)
16023 {
16024         struct triple *first, *ins;
16025         int auto_vars = 0;
16026         first = state->first;
16027         ins = first;
16028         do {
16029                 if (triple_is_auto_var(state, ins)) {
16030                         auto_vars += 1;
16031                 }
16032                 ins = ins->next;
16033         } while(ins != first);
16034         return auto_vars;
16035 }
16036
16037 static void number_auto_vars(struct compile_state *state, struct stack *stacks)
16038 {
16039         struct triple *first, *ins;
16040         int auto_vars = 0;
16041         first = state->first;
16042         ins = first;
16043         do {
16044                 if (triple_is_auto_var(state, ins)) {
16045                         auto_vars += 1;
16046                         stacks[auto_vars].orig_id = ins->id;
16047                         ins->id = auto_vars;
16048                 }
16049                 ins = ins->next;
16050         } while(ins != first);
16051 }
16052
16053 static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
16054 {
16055         struct triple *first, *ins;
16056         first = state->first;
16057         ins = first;
16058         do {
16059                 if (triple_is_auto_var(state, ins)) {
16060                         ins->id = stacks[ins->id].orig_id;
16061                 }
16062                 ins = ins->next;
16063         } while(ins != first);
16064 }
16065
16066 static struct triple *peek_triple(struct stack *stacks, struct triple *var)
16067 {
16068         struct triple_set *head;
16069         struct triple *top_val;
16070         top_val = 0;
16071         head = stacks[var->id].top;
16072         if (head) {
16073                 top_val = head->member;
16074         }
16075         return top_val;
16076 }
16077
16078 static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
16079 {
16080         struct triple_set *new;
16081         /* Append new to the head of the list,
16082          * it's the only sensible behavoir for a stack.
16083          */
16084         new = xcmalloc(sizeof(*new), "triple_set");
16085         new->member = val;
16086         new->next   = stacks[var->id].top;
16087         stacks[var->id].top = new;
16088 }
16089
16090 static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
16091 {
16092         struct triple_set *set, **ptr;
16093         ptr = &stacks[var->id].top;
16094         while(*ptr) {
16095                 set = *ptr;
16096                 if (set->member == oldval) {
16097                         *ptr = set->next;
16098                         xfree(set);
16099                         /* Only free one occurance from the stack */
16100                         return;
16101                 }
16102                 else {
16103                         ptr = &set->next;
16104                 }
16105         }
16106 }
16107
16108 /*
16109  * C(V)
16110  * S(V)
16111  */
16112 static void fixup_block_phi_variables(
16113         struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
16114 {
16115         struct block_set *set;
16116         struct triple *ptr;
16117         int edge;
16118         if (!parent || !block)
16119                 return;
16120         /* Find the edge I am coming in on */
16121         edge = 0;
16122         for(set = block->use; set; set = set->next, edge++) {
16123                 if (set->member == parent) {
16124                         break;
16125                 }
16126         }
16127         if (!set) {
16128                 internal_error(state, 0, "phi input is not on a control predecessor");
16129         }
16130         for(ptr = block->first; ; ptr = ptr->next) {
16131                 if (ptr->op == OP_PHI) {
16132                         struct triple *var, *val, **slot;
16133                         var = MISC(ptr, 0);
16134                         if (!var) {
16135                                 internal_error(state, ptr, "no var???");
16136                         }
16137                         /* Find the current value of the variable */
16138                         val = peek_triple(stacks, var);
16139                         if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
16140                                 internal_error(state, val, "bad value in phi");
16141                         }
16142                         if (edge >= ptr->rhs) {
16143                                 internal_error(state, ptr, "edges > phi rhs");
16144                         }
16145                         slot = &RHS(ptr, edge);
16146                         if ((*slot != 0) && (*slot != val)) {
16147                                 internal_error(state, ptr, "phi already bound on this edge");
16148                         }
16149                         *slot = val;
16150                         use_triple(val, ptr);
16151                 }
16152                 if (ptr == block->last) {
16153                         break;
16154                 }
16155         }
16156 }
16157
16158
16159 static void rename_block_variables(
16160         struct compile_state *state, struct stack *stacks, struct block *block)
16161 {
16162         struct block_set *user, *edge;
16163         struct triple *ptr, *next, *last;
16164         int done;
16165         if (!block)
16166                 return;
16167         last = block->first;
16168         done = 0;
16169         for(ptr = block->first; !done; ptr = next) {
16170                 next = ptr->next;
16171                 if (ptr == block->last) {
16172                         done = 1;
16173                 }
16174                 /* RHS(A) */
16175                 if (ptr->op == OP_READ) {
16176                         struct triple *var, *val;
16177                         var = RHS(ptr, 0);
16178                         if (!triple_is_auto_var(state, var)) {
16179                                 internal_error(state, ptr, "read of non auto var!");
16180                         }
16181                         unuse_triple(var, ptr);
16182                         /* Find the current value of the variable */
16183                         val = peek_triple(stacks, var);
16184                         if (!val) {
16185                                 /* Let the optimizer at variables that are not initially
16186                                  * set.  But give it a bogus value so things seem to
16187                                  * work by accident.  This is useful for bitfields because
16188                                  * setting them always involves a read-modify-write.
16189                                  */
16190                                 if (TYPE_ARITHMETIC(ptr->type->type)) {
16191                                         val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16192                                         val->u.cval = 0xdeadbeaf;
16193                                 } else {
16194                                         val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16195                                 }
16196                         }
16197                         if (!val) {
16198                                 error(state, ptr, "variable used without being set");
16199                         }
16200                         if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16201                                 internal_error(state, val, "bad value in read");
16202                         }
16203                         propogate_use(state, ptr, val);
16204                         release_triple(state, ptr);
16205                         continue;
16206                 }
16207                 /* LHS(A) */
16208                 if (ptr->op == OP_WRITE) {
16209                         struct triple *var, *val, *tval;
16210                         var = MISC(ptr, 0);
16211                         if (!triple_is_auto_var(state, var)) {
16212                                 internal_error(state, ptr, "write to non auto var!");
16213                         }
16214                         tval = val = RHS(ptr, 0);
16215                         if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16216                                 triple_is_auto_var(state, val)) {
16217                                 internal_error(state, ptr, "bad value in write");
16218                         }
16219                         /* Insert a cast if the types differ */
16220                         if (!is_subset_type(ptr->type, val->type)) {
16221                                 if (val->op == OP_INTCONST) {
16222                                         tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16223                                         tval->u.cval = val->u.cval;
16224                                 }
16225                                 else {
16226                                         tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
16227                                         use_triple(val, tval);
16228                                 }
16229                                 transform_to_arch_instruction(state, tval);
16230                                 unuse_triple(val, ptr);
16231                                 RHS(ptr, 0) = tval;
16232                                 use_triple(tval, ptr);
16233                         }
16234                         propogate_use(state, ptr, tval);
16235                         unuse_triple(var, ptr);
16236                         /* Push OP_WRITE ptr->right onto a stack of variable uses */
16237                         push_triple(stacks, var, tval);
16238                 }
16239                 if (ptr->op == OP_PHI) {
16240                         struct triple *var;
16241                         var = MISC(ptr, 0);
16242                         if (!triple_is_auto_var(state, var)) {
16243                                 internal_error(state, ptr, "phi references non auto var!");
16244                         }
16245                         /* Push OP_PHI onto a stack of variable uses */
16246                         push_triple(stacks, var, ptr);
16247                 }
16248                 last = ptr;
16249         }
16250         block->last = last;
16251
16252         /* Fixup PHI functions in the cf successors */
16253         for(edge = block->edges; edge; edge = edge->next) {
16254                 fixup_block_phi_variables(state, stacks, block, edge->member);
16255         }
16256         /* rename variables in the dominated nodes */
16257         for(user = block->idominates; user; user = user->next) {
16258                 rename_block_variables(state, stacks, user->member);
16259         }
16260         /* pop the renamed variable stack */
16261         last = block->first;
16262         done = 0;
16263         for(ptr = block->first; !done ; ptr = next) {
16264                 next = ptr->next;
16265                 if (ptr == block->last) {
16266                         done = 1;
16267                 }
16268                 if (ptr->op == OP_WRITE) {
16269                         struct triple *var;
16270                         var = MISC(ptr, 0);
16271                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
16272                         pop_triple(stacks, var, RHS(ptr, 0));
16273                         release_triple(state, ptr);
16274                         continue;
16275                 }
16276                 if (ptr->op == OP_PHI) {
16277                         struct triple *var;
16278                         var = MISC(ptr, 0);
16279                         /* Pop OP_WRITE ptr->right from the stack of variable uses */
16280                         pop_triple(stacks, var, ptr);
16281                 }
16282                 last = ptr;
16283         }
16284         block->last = last;
16285 }
16286
16287 static void rename_variables(struct compile_state *state)
16288 {
16289         struct stack *stacks;
16290         int auto_vars;
16291
16292         /* Allocate stacks for the Variables */
16293         auto_vars = count_auto_vars(state);
16294         stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
16295
16296         /* Give each auto_var a stack */
16297         number_auto_vars(state, stacks);
16298
16299         /* Rename the variables */
16300         rename_block_variables(state, stacks, state->bb.first_block);
16301
16302         /* Remove the stacks from the auto_vars */
16303         restore_auto_vars(state, stacks);
16304         xfree(stacks);
16305 }
16306
16307 static void prune_block_variables(struct compile_state *state,
16308         struct block *block)
16309 {
16310         struct block_set *user;
16311         struct triple *next, *ptr;
16312         int done;
16313
16314         done = 0;
16315         for(ptr = block->first; !done; ptr = next) {
16316                 /* Be extremely careful I am deleting the list
16317                  * as I walk trhough it.
16318                  */
16319                 next = ptr->next;
16320                 if (ptr == block->last) {
16321                         done = 1;
16322                 }
16323                 if (triple_is_auto_var(state, ptr)) {
16324                         struct triple_set *user, *next;
16325                         for(user = ptr->use; user; user = next) {
16326                                 struct triple *use;
16327                                 next = user->next;
16328                                 use = user->member;
16329                                 if (MISC(ptr, 0) == user->member) {
16330                                         continue;
16331                                 }
16332                                 if (use->op != OP_PHI) {
16333                                         internal_error(state, use, "decl still used");
16334                                 }
16335                                 if (MISC(use, 0) != ptr) {
16336                                         internal_error(state, use, "bad phi use of decl");
16337                                 }
16338                                 unuse_triple(ptr, use);
16339                                 MISC(use, 0) = 0;
16340                         }
16341                         if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16342                                 /* Delete the adecl */
16343                                 release_triple(state, MISC(ptr, 0));
16344                                 /* And the piece */
16345                                 release_triple(state, ptr);
16346                         }
16347                         continue;
16348                 }
16349         }
16350         for(user = block->idominates; user; user = user->next) {
16351                 prune_block_variables(state, user->member);
16352         }
16353 }
16354
16355 struct phi_triple {
16356         struct triple *phi;
16357         unsigned orig_id;
16358         int alive;
16359 };
16360
16361 static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16362 {
16363         struct triple **slot;
16364         int zrhs, i;
16365         if (live[phi->id].alive) {
16366                 return;
16367         }
16368         live[phi->id].alive = 1;
16369         zrhs = phi->rhs;
16370         slot = &RHS(phi, 0);
16371         for(i = 0; i < zrhs; i++) {
16372                 struct triple *used;
16373                 used = slot[i];
16374                 if (used && (used->op == OP_PHI)) {
16375                         keep_phi(state, live, used);
16376                 }
16377         }
16378 }
16379
16380 static void prune_unused_phis(struct compile_state *state)
16381 {
16382         struct triple *first, *phi;
16383         struct phi_triple *live;
16384         int phis, i;
16385         
16386         /* Find the first instruction */
16387         first = state->first;
16388
16389         /* Count how many phi functions I need to process */
16390         phis = 0;
16391         for(phi = first->next; phi != first; phi = phi->next) {
16392                 if (phi->op == OP_PHI) {
16393                         phis += 1;
16394                 }
16395         }
16396         
16397         /* Mark them all dead */
16398         live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16399         phis = 0;
16400         for(phi = first->next; phi != first; phi = phi->next) {
16401                 if (phi->op != OP_PHI) {
16402                         continue;
16403                 }
16404                 live[phis].alive   = 0;
16405                 live[phis].orig_id = phi->id;
16406                 live[phis].phi     = phi;
16407                 phi->id = phis;
16408                 phis += 1;
16409         }
16410         
16411         /* Mark phis alive that are used by non phis */
16412         for(i = 0; i < phis; i++) {
16413                 struct triple_set *set;
16414                 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16415                         if (set->member->op != OP_PHI) {
16416                                 keep_phi(state, live, live[i].phi);
16417                                 break;
16418                         }
16419                 }
16420         }
16421
16422         /* Delete the extraneous phis */
16423         for(i = 0; i < phis; i++) {
16424                 struct triple **slot;
16425                 int zrhs, j;
16426                 if (!live[i].alive) {
16427                         release_triple(state, live[i].phi);
16428                         continue;
16429                 }
16430                 phi = live[i].phi;
16431                 slot = &RHS(phi, 0);
16432                 zrhs = phi->rhs;
16433                 for(j = 0; j < zrhs; j++) {
16434                         if(!slot[j]) {
16435                                 struct triple *unknown;
16436                                 get_occurance(phi->occurance);
16437                                 unknown = flatten(state, state->global_pool,
16438                                         alloc_triple(state, OP_UNKNOWNVAL,
16439                                                 phi->type, 0, 0, phi->occurance));
16440                                 slot[j] = unknown;
16441                                 use_triple(unknown, phi);
16442                                 transform_to_arch_instruction(state, unknown);
16443 #if 0                           
16444                                 warning(state, phi, "variable not set at index %d on all paths to use", j);
16445 #endif
16446                         }
16447                 }
16448         }
16449         xfree(live);
16450 }
16451
16452 static void transform_to_ssa_form(struct compile_state *state)
16453 {
16454         insert_phi_operations(state);
16455         rename_variables(state);
16456
16457         prune_block_variables(state, state->bb.first_block);
16458         prune_unused_phis(state);
16459
16460         print_blocks(state, __func__, state->dbgout);
16461 }
16462
16463
16464 static void clear_vertex(
16465         struct compile_state *state, struct block *block, void *arg)
16466 {
16467         /* Clear the current blocks vertex and the vertex of all
16468          * of the current blocks neighbors in case there are malformed
16469          * blocks with now instructions at this point.
16470          */
16471         struct block_set *user, *edge;
16472         block->vertex = 0;
16473         for(edge = block->edges; edge; edge = edge->next) {
16474                 edge->member->vertex = 0;
16475         }
16476         for(user = block->use; user; user = user->next) {
16477                 user->member->vertex = 0;
16478         }
16479 }
16480
16481 static void mark_live_block(
16482         struct compile_state *state, struct block *block, int *next_vertex)
16483 {
16484         /* See if this is a block that has not been marked */
16485         if (block->vertex != 0) {
16486                 return;
16487         }
16488         block->vertex = *next_vertex;
16489         *next_vertex += 1;
16490         if (triple_is_branch(state, block->last)) {
16491                 struct triple **targ;
16492                 targ = triple_edge_targ(state, block->last, 0);
16493                 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
16494                         if (!*targ) {
16495                                 continue;
16496                         }
16497                         if (!triple_stores_block(state, *targ)) {
16498                                 internal_error(state, 0, "bad targ");
16499                         }
16500                         mark_live_block(state, (*targ)->u.block, next_vertex);
16501                 }
16502                 /* Ensure the last block of a function remains alive */
16503                 if (triple_is_call(state, block->last)) {
16504                         mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16505                 }
16506         }
16507         else if (block->last->next != state->first) {
16508                 struct triple *ins;
16509                 ins = block->last->next;
16510                 if (!triple_stores_block(state, ins)) {
16511                         internal_error(state, 0, "bad block start");
16512                 }
16513                 mark_live_block(state, ins->u.block, next_vertex);
16514         }
16515 }
16516
16517 static void transform_from_ssa_form(struct compile_state *state)
16518 {
16519         /* To get out of ssa form we insert moves on the incoming
16520          * edges to blocks containting phi functions.
16521          */
16522         struct triple *first;
16523         struct triple *phi, *var, *next;
16524         int next_vertex;
16525
16526         /* Walk the control flow to see which blocks remain alive */
16527         walk_blocks(state, &state->bb, clear_vertex, 0);
16528         next_vertex = 1;
16529         mark_live_block(state, state->bb.first_block, &next_vertex);
16530
16531         /* Walk all of the operations to find the phi functions */
16532         first = state->first;
16533         for(phi = first->next; phi != first ; phi = next) {
16534                 struct block_set *set;
16535                 struct block *block;
16536                 struct triple **slot;
16537                 struct triple *var;
16538                 struct triple_set *use, *use_next;
16539                 int edge, writers, readers;
16540                 next = phi->next;
16541                 if (phi->op != OP_PHI) {
16542                         continue;
16543                 }
16544
16545                 block = phi->u.block;
16546                 slot  = &RHS(phi, 0);
16547
16548                 /* If this phi is in a dead block just forget it */
16549                 if (block->vertex == 0) {
16550                         release_triple(state, phi);
16551                         continue;
16552                 }
16553
16554                 /* Forget uses from code in dead blocks */
16555                 for(use = phi->use; use; use = use_next) {
16556                         struct block *ublock;
16557                         struct triple **expr;
16558                         use_next = use->next;
16559                         ublock = block_of_triple(state, use->member);
16560                         if ((use->member == phi) || (ublock->vertex != 0)) {
16561                                 continue;
16562                         }
16563                         expr = triple_rhs(state, use->member, 0);
16564                         for(; expr; expr = triple_rhs(state, use->member, expr)) {
16565                                 if (*expr == phi) {
16566                                         *expr = 0;
16567                                 }
16568                         }
16569                         unuse_triple(phi, use->member);
16570                 }
16571                 /* A variable to replace the phi function */
16572                 if (registers_of(state, phi->type) != 1) {
16573                         internal_error(state, phi, "phi->type does not fit in a single register!");
16574                 }
16575                 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16576                 var = var->next; /* point at the var */
16577                         
16578                 /* Replaces use of phi with var */
16579                 propogate_use(state, phi, var);
16580
16581                 /* Count the readers */
16582                 readers = 0;
16583                 for(use = var->use; use; use = use->next) {
16584                         if (use->member != MISC(var, 0)) {
16585                                 readers++;
16586                         }
16587                 }
16588
16589                 /* Walk all of the incoming edges/blocks and insert moves.
16590                  */
16591                 writers = 0;
16592                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
16593                         struct block *eblock, *vblock;
16594                         struct triple *move;
16595                         struct triple *val, *base;
16596                         eblock = set->member;
16597                         val = slot[edge];
16598                         slot[edge] = 0;
16599                         unuse_triple(val, phi);
16600                         vblock = block_of_triple(state, val);
16601
16602                         /* If we don't have a value that belongs in an OP_WRITE
16603                          * continue on.
16604                          */
16605                         if (!val || (val == &unknown_triple) || (val == phi)
16606                                 || (vblock && (vblock->vertex == 0))) {
16607                                 continue;
16608                         }
16609                         /* If the value should never occur error */
16610                         if (!vblock) {
16611                                 internal_error(state, val, "no vblock?");
16612                                 continue;
16613                         }
16614
16615                         /* If the value occurs in a dead block see if a replacement
16616                          * block can be found.
16617                          */
16618                         while(eblock && (eblock->vertex == 0)) {
16619                                 eblock = eblock->idom;
16620                         }
16621                         /* If not continue on with the next value. */
16622                         if (!eblock || (eblock->vertex == 0)) {
16623                                 continue;
16624                         }
16625
16626                         /* If we have an empty incoming block ignore it. */
16627                         if (!eblock->first) {
16628                                 internal_error(state, 0, "empty block?");
16629                         }
16630                         
16631                         /* Make certain the write is placed in the edge block... */
16632                         /* Walk through the edge block backwards to find an
16633                          * appropriate location for the OP_WRITE.
16634                          */
16635                         for(base = eblock->last; base != eblock->first; base = base->prev) {
16636                                 struct triple **expr;
16637                                 if (base->op == OP_PIECE) {
16638                                         base = MISC(base, 0);
16639                                 }
16640                                 if ((base == var) || (base == val)) {
16641                                         goto out;
16642                                 }
16643                                 expr = triple_lhs(state, base, 0);
16644                                 for(; expr; expr = triple_lhs(state, base, expr)) {
16645                                         if ((*expr) == val) {
16646                                                 goto out;
16647                                         }
16648                                 }
16649                                 expr = triple_rhs(state, base, 0);
16650                                 for(; expr; expr = triple_rhs(state, base, expr)) {
16651                                         if ((*expr) == var) {
16652                                                 goto out;
16653                                         }
16654                                 }
16655                         }
16656                 out:
16657                         if (triple_is_branch(state, base)) {
16658                                 internal_error(state, base,
16659                                         "Could not insert write to phi");
16660                         }
16661                         move = post_triple(state, base, OP_WRITE, var->type, val, var);
16662                         use_triple(val, move);
16663                         use_triple(var, move);
16664                         writers++;
16665                 }
16666                 if (!writers && readers) {
16667                         internal_error(state, var, "no value written to in use phi?");
16668                 }
16669                 /* If var is not used free it */
16670                 if (!writers) {
16671                         release_triple(state, MISC(var, 0));
16672                         release_triple(state, var);
16673                 }
16674                 /* Release the phi function */
16675                 release_triple(state, phi);
16676         }
16677         
16678         /* Walk all of the operations to find the adecls */
16679         for(var = first->next; var != first ; var = var->next) {
16680                 struct triple_set *use, *use_next;
16681                 if (!triple_is_auto_var(state, var)) {
16682                         continue;
16683                 }
16684
16685                 /* Walk through all of the rhs uses of var and
16686                  * replace them with read of var.
16687                  */
16688                 for(use = var->use; use; use = use_next) {
16689                         struct triple *read, *user;
16690                         struct triple **slot;
16691                         int zrhs, i, used;
16692                         use_next = use->next;
16693                         user = use->member;
16694                         
16695                         /* Generate a read of var */
16696                         read = pre_triple(state, user, OP_READ, var->type, var, 0);
16697                         use_triple(var, read);
16698
16699                         /* Find the rhs uses and see if they need to be replaced */
16700                         used = 0;
16701                         zrhs = user->rhs;
16702                         slot = &RHS(user, 0);
16703                         for(i = 0; i < zrhs; i++) {
16704                                 if (slot[i] == var) {
16705                                         slot[i] = read;
16706                                         used = 1;
16707                                 }
16708                         }
16709                         /* If we did use it cleanup the uses */
16710                         if (used) {
16711                                 unuse_triple(var, user);
16712                                 use_triple(read, user);
16713                         } 
16714                         /* If we didn't use it release the extra triple */
16715                         else {
16716                                 release_triple(state, read);
16717                         }
16718                 }
16719         }
16720 }
16721
16722 #define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
16723         FILE *fp = state->dbgout; \
16724         fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
16725         } 
16726
16727 static void rebuild_ssa_form(struct compile_state *state)
16728 {
16729 HI();
16730         transform_from_ssa_form(state);
16731 HI();
16732         state->bb.first = state->first;
16733         free_basic_blocks(state, &state->bb);
16734         analyze_basic_blocks(state, &state->bb);
16735 HI();
16736         insert_phi_operations(state);
16737 HI();
16738         rename_variables(state);
16739 HI();
16740         
16741         prune_block_variables(state, state->bb.first_block);
16742 HI();
16743         prune_unused_phis(state);
16744 HI();
16745 }
16746 #undef HI
16747
16748 /* 
16749  * Register conflict resolution
16750  * =========================================================
16751  */
16752
16753 static struct reg_info find_def_color(
16754         struct compile_state *state, struct triple *def)
16755 {
16756         struct triple_set *set;
16757         struct reg_info info;
16758         info.reg = REG_UNSET;
16759         info.regcm = 0;
16760         if (!triple_is_def(state, def)) {
16761                 return info;
16762         }
16763         info = arch_reg_lhs(state, def, 0);
16764         if (info.reg >= MAX_REGISTERS) {
16765                 info.reg = REG_UNSET;
16766         }
16767         for(set = def->use; set; set = set->next) {
16768                 struct reg_info tinfo;
16769                 int i;
16770                 i = find_rhs_use(state, set->member, def);
16771                 if (i < 0) {
16772                         continue;
16773                 }
16774                 tinfo = arch_reg_rhs(state, set->member, i);
16775                 if (tinfo.reg >= MAX_REGISTERS) {
16776                         tinfo.reg = REG_UNSET;
16777                 }
16778                 if ((tinfo.reg != REG_UNSET) && 
16779                         (info.reg != REG_UNSET) &&
16780                         (tinfo.reg != info.reg)) {
16781                         internal_error(state, def, "register conflict");
16782                 }
16783                 if ((info.regcm & tinfo.regcm) == 0) {
16784                         internal_error(state, def, "regcm conflict %x & %x == 0",
16785                                 info.regcm, tinfo.regcm);
16786                 }
16787                 if (info.reg == REG_UNSET) {
16788                         info.reg = tinfo.reg;
16789                 }
16790                 info.regcm &= tinfo.regcm;
16791         }
16792         if (info.reg >= MAX_REGISTERS) {
16793                 internal_error(state, def, "register out of range");
16794         }
16795         return info;
16796 }
16797
16798 static struct reg_info find_lhs_pre_color(
16799         struct compile_state *state, struct triple *ins, int index)
16800 {
16801         struct reg_info info;
16802         int zlhs, zrhs, i;
16803         zrhs = ins->rhs;
16804         zlhs = ins->lhs;
16805         if (!zlhs && triple_is_def(state, ins)) {
16806                 zlhs = 1;
16807         }
16808         if (index >= zlhs) {
16809                 internal_error(state, ins, "Bad lhs %d", index);
16810         }
16811         info = arch_reg_lhs(state, ins, index);
16812         for(i = 0; i < zrhs; i++) {
16813                 struct reg_info rinfo;
16814                 rinfo = arch_reg_rhs(state, ins, i);
16815                 if ((info.reg == rinfo.reg) &&
16816                         (rinfo.reg >= MAX_REGISTERS)) {
16817                         struct reg_info tinfo;
16818                         tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
16819                         info.reg = tinfo.reg;
16820                         info.regcm &= tinfo.regcm;
16821                         break;
16822                 }
16823         }
16824         if (info.reg >= MAX_REGISTERS) {
16825                 info.reg = REG_UNSET;
16826         }
16827         return info;
16828 }
16829
16830 static struct reg_info find_rhs_post_color(
16831         struct compile_state *state, struct triple *ins, int index);
16832
16833 static struct reg_info find_lhs_post_color(
16834         struct compile_state *state, struct triple *ins, int index)
16835 {
16836         struct triple_set *set;
16837         struct reg_info info;
16838         struct triple *lhs;
16839 #if DEBUG_TRIPLE_COLOR
16840         fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
16841                 ins, index);
16842 #endif
16843         if ((index == 0) && triple_is_def(state, ins)) {
16844                 lhs = ins;
16845         }
16846         else if (index < ins->lhs) {
16847                 lhs = LHS(ins, index);
16848         }
16849         else {
16850                 internal_error(state, ins, "Bad lhs %d", index);
16851                 lhs = 0;
16852         }
16853         info = arch_reg_lhs(state, ins, index);
16854         if (info.reg >= MAX_REGISTERS) {
16855                 info.reg = REG_UNSET;
16856         }
16857         for(set = lhs->use; set; set = set->next) {
16858                 struct reg_info rinfo;
16859                 struct triple *user;
16860                 int zrhs, i;
16861                 user = set->member;
16862                 zrhs = user->rhs;
16863                 for(i = 0; i < zrhs; i++) {
16864                         if (RHS(user, i) != lhs) {
16865                                 continue;
16866                         }
16867                         rinfo = find_rhs_post_color(state, user, i);
16868                         if ((info.reg != REG_UNSET) &&
16869                                 (rinfo.reg != REG_UNSET) &&
16870                                 (info.reg != rinfo.reg)) {
16871                                 internal_error(state, ins, "register conflict");
16872                         }
16873                         if ((info.regcm & rinfo.regcm) == 0) {
16874                                 internal_error(state, ins, "regcm conflict %x & %x == 0",
16875                                         info.regcm, rinfo.regcm);
16876                         }
16877                         if (info.reg == REG_UNSET) {
16878                                 info.reg = rinfo.reg;
16879                         }
16880                         info.regcm &= rinfo.regcm;
16881                 }
16882         }
16883 #if DEBUG_TRIPLE_COLOR
16884         fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
16885                 ins, index, info.reg, info.regcm);
16886 #endif
16887         return info;
16888 }
16889
16890 static struct reg_info find_rhs_post_color(
16891         struct compile_state *state, struct triple *ins, int index)
16892 {
16893         struct reg_info info, rinfo;
16894         int zlhs, i;
16895 #if DEBUG_TRIPLE_COLOR
16896         fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
16897                 ins, index);
16898 #endif
16899         rinfo = arch_reg_rhs(state, ins, index);
16900         zlhs = ins->lhs;
16901         if (!zlhs && triple_is_def(state, ins)) {
16902                 zlhs = 1;
16903         }
16904         info = rinfo;
16905         if (info.reg >= MAX_REGISTERS) {
16906                 info.reg = REG_UNSET;
16907         }
16908         for(i = 0; i < zlhs; i++) {
16909                 struct reg_info linfo;
16910                 linfo = arch_reg_lhs(state, ins, i);
16911                 if ((linfo.reg == rinfo.reg) &&
16912                         (linfo.reg >= MAX_REGISTERS)) {
16913                         struct reg_info tinfo;
16914                         tinfo = find_lhs_post_color(state, ins, i);
16915                         if (tinfo.reg >= MAX_REGISTERS) {
16916                                 tinfo.reg = REG_UNSET;
16917                         }
16918                         info.regcm &= linfo.regcm;
16919                         info.regcm &= tinfo.regcm;
16920                         if (info.reg != REG_UNSET) {
16921                                 internal_error(state, ins, "register conflict");
16922                         }
16923                         if (info.regcm == 0) {
16924                                 internal_error(state, ins, "regcm conflict");
16925                         }
16926                         info.reg = tinfo.reg;
16927                 }
16928         }
16929 #if DEBUG_TRIPLE_COLOR
16930         fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
16931                 ins, index, info.reg, info.regcm);
16932 #endif
16933         return info;
16934 }
16935
16936 static struct reg_info find_lhs_color(
16937         struct compile_state *state, struct triple *ins, int index)
16938 {
16939         struct reg_info pre, post, info;
16940 #if DEBUG_TRIPLE_COLOR
16941         fprintf(state->errout, "find_lhs_color(%p, %d)\n",
16942                 ins, index);
16943 #endif
16944         pre = find_lhs_pre_color(state, ins, index);
16945         post = find_lhs_post_color(state, ins, index);
16946         if ((pre.reg != post.reg) &&
16947                 (pre.reg != REG_UNSET) &&
16948                 (post.reg != REG_UNSET)) {
16949                 internal_error(state, ins, "register conflict");
16950         }
16951         info.regcm = pre.regcm & post.regcm;
16952         info.reg = pre.reg;
16953         if (info.reg == REG_UNSET) {
16954                 info.reg = post.reg;
16955         }
16956 #if DEBUG_TRIPLE_COLOR
16957         fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
16958                 ins, index, info.reg, info.regcm,
16959                 pre.reg, pre.regcm, post.reg, post.regcm);
16960 #endif
16961         return info;
16962 }
16963
16964 static struct triple *post_copy(struct compile_state *state, struct triple *ins)
16965 {
16966         struct triple_set *entry, *next;
16967         struct triple *out;
16968         struct reg_info info, rinfo;
16969
16970         info = arch_reg_lhs(state, ins, 0);
16971         out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
16972         use_triple(RHS(out, 0), out);
16973         /* Get the users of ins to use out instead */
16974         for(entry = ins->use; entry; entry = next) {
16975                 int i;
16976                 next = entry->next;
16977                 if (entry->member == out) {
16978                         continue;
16979                 }
16980                 i = find_rhs_use(state, entry->member, ins);
16981                 if (i < 0) {
16982                         continue;
16983                 }
16984                 rinfo = arch_reg_rhs(state, entry->member, i);
16985                 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
16986                         continue;
16987                 }
16988                 replace_rhs_use(state, ins, out, entry->member);
16989         }
16990         transform_to_arch_instruction(state, out);
16991         return out;
16992 }
16993
16994 static struct triple *typed_pre_copy(
16995         struct compile_state *state, struct type *type, struct triple *ins, int index)
16996 {
16997         /* Carefully insert enough operations so that I can
16998          * enter any operation with a GPR32.
16999          */
17000         struct triple *in;
17001         struct triple **expr;
17002         unsigned classes;
17003         struct reg_info info;
17004         int op;
17005         if (ins->op == OP_PHI) {
17006                 internal_error(state, ins, "pre_copy on a phi?");
17007         }
17008         classes = arch_type_to_regcm(state, type);
17009         info = arch_reg_rhs(state, ins, index);
17010         expr = &RHS(ins, index);
17011         if ((info.regcm & classes) == 0) {
17012                 FILE *fp = state->errout;
17013                 fprintf(fp, "src_type: ");
17014                 name_of(fp, ins->type);
17015                 fprintf(fp, "\ndst_type: ");
17016                 name_of(fp, type);
17017                 fprintf(fp, "\n");
17018                 internal_error(state, ins, "pre_copy with no register classes");
17019         }
17020         op = OP_COPY;
17021         if (!equiv_types(type, (*expr)->type)) {
17022                 op = OP_CONVERT;
17023         }
17024         in = pre_triple(state, ins, op, type, *expr, 0);
17025         unuse_triple(*expr, ins);
17026         *expr = in;
17027         use_triple(RHS(in, 0), in);
17028         use_triple(in, ins);
17029         transform_to_arch_instruction(state, in);
17030         return in;
17031         
17032 }
17033 static struct triple *pre_copy(
17034         struct compile_state *state, struct triple *ins, int index)
17035 {
17036         return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
17037 }
17038
17039
17040 static void insert_copies_to_phi(struct compile_state *state)
17041 {
17042         /* To get out of ssa form we insert moves on the incoming
17043          * edges to blocks containting phi functions.
17044          */
17045         struct triple *first;
17046         struct triple *phi;
17047
17048         /* Walk all of the operations to find the phi functions */
17049         first = state->first;
17050         for(phi = first->next; phi != first ; phi = phi->next) {
17051                 struct block_set *set;
17052                 struct block *block;
17053                 struct triple **slot, *copy;
17054                 int edge;
17055                 if (phi->op != OP_PHI) {
17056                         continue;
17057                 }
17058                 phi->id |= TRIPLE_FLAG_POST_SPLIT;
17059                 block = phi->u.block;
17060                 slot  = &RHS(phi, 0);
17061                 /* Phi's that feed into mandatory live range joins
17062                  * cause nasty complications.  Insert a copy of
17063                  * the phi value so I never have to deal with
17064                  * that in the rest of the code.
17065                  */
17066                 copy = post_copy(state, phi);
17067                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
17068                 /* Walk all of the incoming edges/blocks and insert moves.
17069                  */
17070                 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17071                         struct block *eblock;
17072                         struct triple *move;
17073                         struct triple *val;
17074                         struct triple *ptr;
17075                         eblock = set->member;
17076                         val = slot[edge];
17077
17078                         if (val == phi) {
17079                                 continue;
17080                         }
17081
17082                         get_occurance(val->occurance);
17083                         move = build_triple(state, OP_COPY, val->type, val, 0,
17084                                 val->occurance);
17085                         move->u.block = eblock;
17086                         move->id |= TRIPLE_FLAG_PRE_SPLIT;
17087                         use_triple(val, move);
17088                         
17089                         slot[edge] = move;
17090                         unuse_triple(val, phi);
17091                         use_triple(move, phi);
17092
17093                         /* Walk up the dominator tree until I have found the appropriate block */
17094                         while(eblock && !tdominates(state, val, eblock->last)) {
17095                                 eblock = eblock->idom;
17096                         }
17097                         if (!eblock) {
17098                                 internal_error(state, phi, "Cannot find block dominated by %p",
17099                                         val);
17100                         }
17101
17102                         /* Walk through the block backwards to find
17103                          * an appropriate location for the OP_COPY.
17104                          */
17105                         for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17106                                 struct triple **expr;
17107                                 if (ptr->op == OP_PIECE) {
17108                                         ptr = MISC(ptr, 0);
17109                                 }
17110                                 if ((ptr == phi) || (ptr == val)) {
17111                                         goto out;
17112                                 }
17113                                 expr = triple_lhs(state, ptr, 0);
17114                                 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17115                                         if ((*expr) == val) {
17116                                                 goto out;
17117                                         }
17118                                 }
17119                                 expr = triple_rhs(state, ptr, 0);
17120                                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17121                                         if ((*expr) == phi) {
17122                                                 goto out;
17123                                         }
17124                                 }
17125                         }
17126                 out:
17127                         if (triple_is_branch(state, ptr)) {
17128                                 internal_error(state, ptr,
17129                                         "Could not insert write to phi");
17130                         }
17131                         insert_triple(state, after_lhs(state, ptr), move);
17132                         if (eblock->last == after_lhs(state, ptr)->prev) {
17133                                 eblock->last = move;
17134                         }
17135                         transform_to_arch_instruction(state, move);
17136                 }
17137         }
17138         print_blocks(state, __func__, state->dbgout);
17139 }
17140
17141 struct triple_reg_set;
17142 struct reg_block;
17143
17144
17145 static int do_triple_set(struct triple_reg_set **head, 
17146         struct triple *member, struct triple *new_member)
17147 {
17148         struct triple_reg_set **ptr, *new;
17149         if (!member)
17150                 return 0;
17151         ptr = head;
17152         while(*ptr) {
17153                 if ((*ptr)->member == member) {
17154                         return 0;
17155                 }
17156                 ptr = &(*ptr)->next;
17157         }
17158         new = xcmalloc(sizeof(*new), "triple_set");
17159         new->member = member;
17160         new->new    = new_member;
17161         new->next   = *head;
17162         *head       = new;
17163         return 1;
17164 }
17165
17166 static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17167 {
17168         struct triple_reg_set *entry, **ptr;
17169         ptr = head;
17170         while(*ptr) {
17171                 entry = *ptr;
17172                 if (entry->member == member) {
17173                         *ptr = entry->next;
17174                         xfree(entry);
17175                         return;
17176                 }
17177                 else {
17178                         ptr = &entry->next;
17179                 }
17180         }
17181 }
17182
17183 static int in_triple(struct reg_block *rb, struct triple *in)
17184 {
17185         return do_triple_set(&rb->in, in, 0);
17186 }
17187 static void unin_triple(struct reg_block *rb, struct triple *unin)
17188 {
17189         do_triple_unset(&rb->in, unin);
17190 }
17191
17192 static int out_triple(struct reg_block *rb, struct triple *out)
17193 {
17194         return do_triple_set(&rb->out, out, 0);
17195 }
17196 static void unout_triple(struct reg_block *rb, struct triple *unout)
17197 {
17198         do_triple_unset(&rb->out, unout);
17199 }
17200
17201 static int initialize_regblock(struct reg_block *blocks,
17202         struct block *block, int vertex)
17203 {
17204         struct block_set *user;
17205         if (!block || (blocks[block->vertex].block == block)) {
17206                 return vertex;
17207         }
17208         vertex += 1;
17209         /* Renumber the blocks in a convinient fashion */
17210         block->vertex = vertex;
17211         blocks[vertex].block    = block;
17212         blocks[vertex].vertex   = vertex;
17213         for(user = block->use; user; user = user->next) {
17214                 vertex = initialize_regblock(blocks, user->member, vertex);
17215         }
17216         return vertex;
17217 }
17218
17219 static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17220 {
17221 /* Part to piece is a best attempt and it cannot be correct all by
17222  * itself.  If various values are read as different sizes in different
17223  * parts of the code this function cannot work.  Or rather it cannot
17224  * work in conjunction with compute_variable_liftimes.  As the
17225  * analysis will get confused.
17226  */
17227         struct triple *base;
17228         unsigned reg;
17229         if (!is_lvalue(state, ins)) {
17230                 return ins;
17231         }
17232         base = 0;
17233         reg = 0;
17234         while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17235                 base = MISC(ins, 0);
17236                 switch(ins->op) {
17237                 case OP_INDEX:
17238                         reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17239                         break;
17240                 case OP_DOT:
17241                         reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17242                         break;
17243                 default:
17244                         internal_error(state, ins, "unhandled part");
17245                         break;
17246                 }
17247                 ins = base;
17248         }
17249         if (base) {
17250                 if (reg > base->lhs) {
17251                         internal_error(state, base, "part out of range?");
17252                 }
17253                 ins = LHS(base, reg);
17254         }
17255         return ins;
17256 }
17257
17258 static int this_def(struct compile_state *state, 
17259         struct triple *ins, struct triple *other)
17260 {
17261         if (ins == other) {
17262                 return 1;
17263         }
17264         if (ins->op == OP_WRITE) {
17265                 ins = part_to_piece(state, MISC(ins, 0));
17266         }
17267         return ins == other;
17268 }
17269
17270 static int phi_in(struct compile_state *state, struct reg_block *blocks,
17271         struct reg_block *rb, struct block *suc)
17272 {
17273         /* Read the conditional input set of a successor block
17274          * (i.e. the input to the phi nodes) and place it in the
17275          * current blocks output set.
17276          */
17277         struct block_set *set;
17278         struct triple *ptr;
17279         int edge;
17280         int done, change;
17281         change = 0;
17282         /* Find the edge I am coming in on */
17283         for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17284                 if (set->member == rb->block) {
17285                         break;
17286                 }
17287         }
17288         if (!set) {
17289                 internal_error(state, 0, "Not coming on a control edge?");
17290         }
17291         for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17292                 struct triple **slot, *expr, *ptr2;
17293                 int out_change, done2;
17294                 done = (ptr == suc->last);
17295                 if (ptr->op != OP_PHI) {
17296                         continue;
17297                 }
17298                 slot = &RHS(ptr, 0);
17299                 expr = slot[edge];
17300                 out_change = out_triple(rb, expr);
17301                 if (!out_change) {
17302                         continue;
17303                 }
17304                 /* If we don't define the variable also plast it
17305                  * in the current blocks input set.
17306                  */
17307                 ptr2 = rb->block->first;
17308                 for(done2 = 0; !done2; ptr2 = ptr2->next) {
17309                         if (this_def(state, ptr2, expr)) {
17310                                 break;
17311                         }
17312                         done2 = (ptr2 == rb->block->last);
17313                 }
17314                 if (!done2) {
17315                         continue;
17316                 }
17317                 change |= in_triple(rb, expr);
17318         }
17319         return change;
17320 }
17321
17322 static int reg_in(struct compile_state *state, struct reg_block *blocks,
17323         struct reg_block *rb, struct block *suc)
17324 {
17325         struct triple_reg_set *in_set;
17326         int change;
17327         change = 0;
17328         /* Read the input set of a successor block
17329          * and place it in the current blocks output set.
17330          */
17331         in_set = blocks[suc->vertex].in;
17332         for(; in_set; in_set = in_set->next) {
17333                 int out_change, done;
17334                 struct triple *first, *last, *ptr;
17335                 out_change = out_triple(rb, in_set->member);
17336                 if (!out_change) {
17337                         continue;
17338                 }
17339                 /* If we don't define the variable also place it
17340                  * in the current blocks input set.
17341                  */
17342                 first = rb->block->first;
17343                 last = rb->block->last;
17344                 done = 0;
17345                 for(ptr = first; !done; ptr = ptr->next) {
17346                         if (this_def(state, ptr, in_set->member)) {
17347                                 break;
17348                         }
17349                         done = (ptr == last);
17350                 }
17351                 if (!done) {
17352                         continue;
17353                 }
17354                 change |= in_triple(rb, in_set->member);
17355         }
17356         change |= phi_in(state, blocks, rb, suc);
17357         return change;
17358 }
17359
17360 static int use_in(struct compile_state *state, struct reg_block *rb)
17361 {
17362         /* Find the variables we use but don't define and add
17363          * it to the current blocks input set.
17364          */
17365 #warning "FIXME is this O(N^2) algorithm bad?"
17366         struct block *block;
17367         struct triple *ptr;
17368         int done;
17369         int change;
17370         block = rb->block;
17371         change = 0;
17372         for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17373                 struct triple **expr;
17374                 done = (ptr == block->first);
17375                 /* The variable a phi function uses depends on the
17376                  * control flow, and is handled in phi_in, not
17377                  * here.
17378                  */
17379                 if (ptr->op == OP_PHI) {
17380                         continue;
17381                 }
17382                 expr = triple_rhs(state, ptr, 0);
17383                 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17384                         struct triple *rhs, *test;
17385                         int tdone;
17386                         rhs = part_to_piece(state, *expr);
17387                         if (!rhs) {
17388                                 continue;
17389                         }
17390
17391                         /* See if rhs is defined in this block.
17392                          * A write counts as a definition.
17393                          */
17394                         for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17395                                 tdone = (test == block->first);
17396                                 if (this_def(state, test, rhs)) {
17397                                         rhs = 0;
17398                                         break;
17399                                 }
17400                         }
17401                         /* If I still have a valid rhs add it to in */
17402                         change |= in_triple(rb, rhs);
17403                 }
17404         }
17405         return change;
17406 }
17407
17408 static struct reg_block *compute_variable_lifetimes(
17409         struct compile_state *state, struct basic_blocks *bb)
17410 {
17411         struct reg_block *blocks;
17412         int change;
17413         blocks = xcmalloc(
17414                 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17415         initialize_regblock(blocks, bb->last_block, 0);
17416         do {
17417                 int i;
17418                 change = 0;
17419                 for(i = 1; i <= bb->last_vertex; i++) {
17420                         struct block_set *edge;
17421                         struct reg_block *rb;
17422                         rb = &blocks[i];
17423                         /* Add the all successor's input set to in */
17424                         for(edge = rb->block->edges; edge; edge = edge->next) {
17425                                 change |= reg_in(state, blocks, rb, edge->member);
17426                         }
17427                         /* Add use to in... */
17428                         change |= use_in(state, rb);
17429                 }
17430         } while(change);
17431         return blocks;
17432 }
17433
17434 static void free_variable_lifetimes(struct compile_state *state, 
17435         struct basic_blocks *bb, struct reg_block *blocks)
17436 {
17437         int i;
17438         /* free in_set && out_set on each block */
17439         for(i = 1; i <= bb->last_vertex; i++) {
17440                 struct triple_reg_set *entry, *next;
17441                 struct reg_block *rb;
17442                 rb = &blocks[i];
17443                 for(entry = rb->in; entry ; entry = next) {
17444                         next = entry->next;
17445                         do_triple_unset(&rb->in, entry->member);
17446                 }
17447                 for(entry = rb->out; entry; entry = next) {
17448                         next = entry->next;
17449                         do_triple_unset(&rb->out, entry->member);
17450                 }
17451         }
17452         xfree(blocks);
17453
17454 }
17455
17456 typedef void (*wvl_cb_t)(
17457         struct compile_state *state, 
17458         struct reg_block *blocks, struct triple_reg_set *live, 
17459         struct reg_block *rb, struct triple *ins, void *arg);
17460
17461 static void walk_variable_lifetimes(struct compile_state *state,
17462         struct basic_blocks *bb, struct reg_block *blocks, 
17463         wvl_cb_t cb, void *arg)
17464 {
17465         int i;
17466         
17467         for(i = 1; i <= state->bb.last_vertex; i++) {
17468                 struct triple_reg_set *live;
17469                 struct triple_reg_set *entry, *next;
17470                 struct triple *ptr, *prev;
17471                 struct reg_block *rb;
17472                 struct block *block;
17473                 int done;
17474
17475                 /* Get the blocks */
17476                 rb = &blocks[i];
17477                 block = rb->block;
17478
17479                 /* Copy out into live */
17480                 live = 0;
17481                 for(entry = rb->out; entry; entry = next) {
17482                         next = entry->next;
17483                         do_triple_set(&live, entry->member, entry->new);
17484                 }
17485                 /* Walk through the basic block calculating live */
17486                 for(done = 0, ptr = block->last; !done; ptr = prev) {
17487                         struct triple **expr;
17488
17489                         prev = ptr->prev;
17490                         done = (ptr == block->first);
17491
17492                         /* Ensure the current definition is in live */
17493                         if (triple_is_def(state, ptr)) {
17494                                 do_triple_set(&live, ptr, 0);
17495                         }
17496
17497                         /* Inform the callback function of what is
17498                          * going on.
17499                          */
17500                          cb(state, blocks, live, rb, ptr, arg);
17501                         
17502                         /* Remove the current definition from live */
17503                         do_triple_unset(&live, ptr);
17504
17505                         /* Add the current uses to live.
17506                          *
17507                          * It is safe to skip phi functions because they do
17508                          * not have any block local uses, and the block
17509                          * output sets already properly account for what
17510                          * control flow depedent uses phi functions do have.
17511                          */
17512                         if (ptr->op == OP_PHI) {
17513                                 continue;
17514                         }
17515                         expr = triple_rhs(state, ptr, 0);
17516                         for(;expr; expr = triple_rhs(state, ptr, expr)) {
17517                                 /* If the triple is not a definition skip it. */
17518                                 if (!*expr || !triple_is_def(state, *expr)) {
17519                                         continue;
17520                                 }
17521                                 do_triple_set(&live, *expr, 0);
17522                         }
17523                 }
17524                 /* Free live */
17525                 for(entry = live; entry; entry = next) {
17526                         next = entry->next;
17527                         do_triple_unset(&live, entry->member);
17528                 }
17529         }
17530 }
17531
17532 struct print_live_variable_info {
17533         struct reg_block *rb;
17534         FILE *fp;
17535 };
17536 static void print_live_variables_block(
17537         struct compile_state *state, struct block *block, void *arg)
17538
17539 {
17540         struct print_live_variable_info *info = arg;
17541         struct block_set *edge;
17542         FILE *fp = info->fp;
17543         struct reg_block *rb;
17544         struct triple *ptr;
17545         int phi_present;
17546         int done;
17547         rb = &info->rb[block->vertex];
17548
17549         fprintf(fp, "\nblock: %p (%d),",
17550                 block,  block->vertex);
17551         for(edge = block->edges; edge; edge = edge->next) {
17552                 fprintf(fp, " %p<-%p",
17553                         edge->member, 
17554                         edge->member && edge->member->use?edge->member->use->member : 0);
17555         }
17556         fprintf(fp, "\n");
17557         if (rb->in) {
17558                 struct triple_reg_set *in_set;
17559                 fprintf(fp, "        in:");
17560                 for(in_set = rb->in; in_set; in_set = in_set->next) {
17561                         fprintf(fp, " %-10p", in_set->member);
17562                 }
17563                 fprintf(fp, "\n");
17564         }
17565         phi_present = 0;
17566         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17567                 done = (ptr == block->last);
17568                 if (ptr->op == OP_PHI) {
17569                         phi_present = 1;
17570                         break;
17571                 }
17572         }
17573         if (phi_present) {
17574                 int edge;
17575                 for(edge = 0; edge < block->users; edge++) {
17576                         fprintf(fp, "     in(%d):", edge);
17577                         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17578                                 struct triple **slot;
17579                                 done = (ptr == block->last);
17580                                 if (ptr->op != OP_PHI) {
17581                                         continue;
17582                                 }
17583                                 slot = &RHS(ptr, 0);
17584                                 fprintf(fp, " %-10p", slot[edge]);
17585                         }
17586                         fprintf(fp, "\n");
17587                 }
17588         }
17589         if (block->first->op == OP_LABEL) {
17590                 fprintf(fp, "%p:\n", block->first);
17591         }
17592         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17593                 done = (ptr == block->last);
17594                 display_triple(fp, ptr);
17595         }
17596         if (rb->out) {
17597                 struct triple_reg_set *out_set;
17598                 fprintf(fp, "       out:");
17599                 for(out_set = rb->out; out_set; out_set = out_set->next) {
17600                         fprintf(fp, " %-10p", out_set->member);
17601                 }
17602                 fprintf(fp, "\n");
17603         }
17604         fprintf(fp, "\n");
17605 }
17606
17607 static void print_live_variables(struct compile_state *state, 
17608         struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17609 {
17610         struct print_live_variable_info info;
17611         info.rb = rb;
17612         info.fp = fp;
17613         fprintf(fp, "\nlive variables by block\n");
17614         walk_blocks(state, bb, print_live_variables_block, &info);
17615
17616 }
17617
17618
17619 static int count_triples(struct compile_state *state)
17620 {
17621         struct triple *first, *ins;
17622         int triples = 0;
17623         first = state->first;
17624         ins = first;
17625         do {
17626                 triples++;
17627                 ins = ins->next;
17628         } while (ins != first);
17629         return triples;
17630 }
17631
17632
17633 struct dead_triple {
17634         struct triple *triple;
17635         struct dead_triple *work_next;
17636         struct block *block;
17637         int old_id;
17638         int flags;
17639 #define TRIPLE_FLAG_ALIVE 1
17640 #define TRIPLE_FLAG_FREE  1
17641 };
17642
17643 static void print_dead_triples(struct compile_state *state, 
17644         struct dead_triple *dtriple)
17645 {
17646         struct triple *first, *ins;
17647         struct dead_triple *dt;
17648         FILE *fp;
17649         if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17650                 return;
17651         }
17652         fp = state->dbgout;
17653         fprintf(fp, "--------------- dtriples ---------------\n");
17654         first = state->first;
17655         ins = first;
17656         do {
17657                 dt = &dtriple[ins->id];
17658                 if ((ins->op == OP_LABEL) && (ins->use)) {
17659                         fprintf(fp, "\n%p:\n", ins);
17660                 }
17661                 fprintf(fp, "%c", 
17662                         (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17663                 display_triple(fp, ins);
17664                 if (triple_is_branch(state, ins)) {
17665                         fprintf(fp, "\n");
17666                 }
17667                 ins = ins->next;
17668         } while(ins != first);
17669         fprintf(fp, "\n");
17670 }
17671
17672
17673 static void awaken(
17674         struct compile_state *state,
17675         struct dead_triple *dtriple, struct triple **expr,
17676         struct dead_triple ***work_list_tail)
17677 {
17678         struct triple *triple;
17679         struct dead_triple *dt;
17680         if (!expr) {
17681                 return;
17682         }
17683         triple = *expr;
17684         if (!triple) {
17685                 return;
17686         }
17687         if (triple->id <= 0)  {
17688                 internal_error(state, triple, "bad triple id: %d",
17689                         triple->id);
17690         }
17691         if (triple->op == OP_NOOP) {
17692                 internal_error(state, triple, "awakening noop?");
17693                 return;
17694         }
17695         dt = &dtriple[triple->id];
17696         if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17697                 dt->flags |= TRIPLE_FLAG_ALIVE;
17698                 if (!dt->work_next) {
17699                         **work_list_tail = dt;
17700                         *work_list_tail = &dt->work_next;
17701                 }
17702         }
17703 }
17704
17705 static void eliminate_inefectual_code(struct compile_state *state)
17706 {
17707         struct block *block;
17708         struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17709         int triples, i;
17710         struct triple *first, *final, *ins;
17711
17712         if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17713                 return;
17714         }
17715
17716         /* Setup the work list */
17717         work_list = 0;
17718         work_list_tail = &work_list;
17719
17720         first = state->first;
17721         final = state->first->prev;
17722
17723         /* Count how many triples I have */
17724         triples = count_triples(state);
17725
17726         /* Now put then in an array and mark all of the triples dead */
17727         dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17728         
17729         ins = first;
17730         i = 1;
17731         block = 0;
17732         do {
17733                 dtriple[i].triple = ins;
17734                 dtriple[i].block  = block_of_triple(state, ins);
17735                 dtriple[i].flags  = 0;
17736                 dtriple[i].old_id = ins->id;
17737                 ins->id = i;
17738                 /* See if it is an operation we always keep */
17739                 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
17740                         awaken(state, dtriple, &ins, &work_list_tail);
17741                 }
17742                 i++;
17743                 ins = ins->next;
17744         } while(ins != first);
17745         while(work_list) {
17746                 struct block *block;
17747                 struct dead_triple *dt;
17748                 struct block_set *user;
17749                 struct triple **expr;
17750                 dt = work_list;
17751                 work_list = dt->work_next;
17752                 if (!work_list) {
17753                         work_list_tail = &work_list;
17754                 }
17755                 /* Make certain the block the current instruction is in lives */
17756                 block = block_of_triple(state, dt->triple);
17757                 awaken(state, dtriple, &block->first, &work_list_tail);
17758                 if (triple_is_branch(state, block->last)) {
17759                         awaken(state, dtriple, &block->last, &work_list_tail);
17760                 } else {
17761                         awaken(state, dtriple, &block->last->next, &work_list_tail);
17762                 }
17763
17764                 /* Wake up the data depencencies of this triple */
17765                 expr = 0;
17766                 do {
17767                         expr = triple_rhs(state, dt->triple, expr);
17768                         awaken(state, dtriple, expr, &work_list_tail);
17769                 } while(expr);
17770                 do {
17771                         expr = triple_lhs(state, dt->triple, expr);
17772                         awaken(state, dtriple, expr, &work_list_tail);
17773                 } while(expr);
17774                 do {
17775                         expr = triple_misc(state, dt->triple, expr);
17776                         awaken(state, dtriple, expr, &work_list_tail);
17777                 } while(expr);
17778                 /* Wake up the forward control dependencies */
17779                 do {
17780                         expr = triple_targ(state, dt->triple, expr);
17781                         awaken(state, dtriple, expr, &work_list_tail);
17782                 } while(expr);
17783                 /* Wake up the reverse control dependencies of this triple */
17784                 for(user = dt->block->ipdomfrontier; user; user = user->next) {
17785                         struct triple *last;
17786                         last = user->member->last;
17787                         while((last->op == OP_NOOP) && (last != user->member->first)) {
17788                                 internal_warning(state, last, "awakening noop?");
17789                                 last = last->prev;
17790                         }
17791                         awaken(state, dtriple, &last, &work_list_tail);
17792                 }
17793         }
17794         print_dead_triples(state, dtriple);
17795         for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
17796                 if ((dt->triple->op == OP_NOOP) && 
17797                         (dt->flags & TRIPLE_FLAG_ALIVE)) {
17798                         internal_error(state, dt->triple, "noop effective?");
17799                 }
17800                 dt->triple->id = dt->old_id;    /* Restore the color */
17801                 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17802                         release_triple(state, dt->triple);
17803                 }
17804         }
17805         xfree(dtriple);
17806
17807         rebuild_ssa_form(state);
17808
17809         print_blocks(state, __func__, state->dbgout);
17810 }
17811
17812
17813 static void insert_mandatory_copies(struct compile_state *state)
17814 {
17815         struct triple *ins, *first;
17816
17817         /* The object is with a minimum of inserted copies,
17818          * to resolve in fundamental register conflicts between
17819          * register value producers and consumers.
17820          * Theoretically we may be greater than minimal when we
17821          * are inserting copies before instructions but that
17822          * case should be rare.
17823          */
17824         first = state->first;
17825         ins = first;
17826         do {
17827                 struct triple_set *entry, *next;
17828                 struct triple *tmp;
17829                 struct reg_info info;
17830                 unsigned reg, regcm;
17831                 int do_post_copy, do_pre_copy;
17832                 tmp = 0;
17833                 if (!triple_is_def(state, ins)) {
17834                         goto next;
17835                 }
17836                 /* Find the architecture specific color information */
17837                 info = find_lhs_pre_color(state, ins, 0);
17838                 if (info.reg >= MAX_REGISTERS) {
17839                         info.reg = REG_UNSET;
17840                 }
17841
17842                 reg = REG_UNSET;
17843                 regcm = arch_type_to_regcm(state, ins->type);
17844                 do_post_copy = do_pre_copy = 0;
17845
17846                 /* Walk through the uses of ins and check for conflicts */
17847                 for(entry = ins->use; entry; entry = next) {
17848                         struct reg_info rinfo;
17849                         int i;
17850                         next = entry->next;
17851                         i = find_rhs_use(state, entry->member, ins);
17852                         if (i < 0) {
17853                                 continue;
17854                         }
17855                         
17856                         /* Find the users color requirements */
17857                         rinfo = arch_reg_rhs(state, entry->member, i);
17858                         if (rinfo.reg >= MAX_REGISTERS) {
17859                                 rinfo.reg = REG_UNSET;
17860                         }
17861                         
17862                         /* See if I need a pre_copy */
17863                         if (rinfo.reg != REG_UNSET) {
17864                                 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
17865                                         do_pre_copy = 1;
17866                                 }
17867                                 reg = rinfo.reg;
17868                         }
17869                         regcm &= rinfo.regcm;
17870                         regcm = arch_regcm_normalize(state, regcm);
17871                         if (regcm == 0) {
17872                                 do_pre_copy = 1;
17873                         }
17874                         /* Always use pre_copies for constants.
17875                          * They do not take up any registers until a
17876                          * copy places them in one.
17877                          */
17878                         if ((info.reg == REG_UNNEEDED) && 
17879                                 (rinfo.reg != REG_UNNEEDED)) {
17880                                 do_pre_copy = 1;
17881                         }
17882                 }
17883                 do_post_copy =
17884                         !do_pre_copy &&
17885                         (((info.reg != REG_UNSET) && 
17886                                 (reg != REG_UNSET) &&
17887                                 (info.reg != reg)) ||
17888                         ((info.regcm & regcm) == 0));
17889
17890                 reg = info.reg;
17891                 regcm = info.regcm;
17892                 /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
17893                 for(entry = ins->use; entry; entry = next) {
17894                         struct reg_info rinfo;
17895                         int i;
17896                         next = entry->next;
17897                         i = find_rhs_use(state, entry->member, ins);
17898                         if (i < 0) {
17899                                 continue;
17900                         }
17901                         
17902                         /* Find the users color requirements */
17903                         rinfo = arch_reg_rhs(state, entry->member, i);
17904                         if (rinfo.reg >= MAX_REGISTERS) {
17905                                 rinfo.reg = REG_UNSET;
17906                         }
17907
17908                         /* Now see if it is time to do the pre_copy */
17909                         if (rinfo.reg != REG_UNSET) {
17910                                 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
17911                                         ((regcm & rinfo.regcm) == 0) ||
17912                                         /* Don't let a mandatory coalesce sneak
17913                                          * into a operation that is marked to prevent
17914                                          * coalescing.
17915                                          */
17916                                         ((reg != REG_UNNEEDED) &&
17917                                         ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
17918                                         (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
17919                                         ) {
17920                                         if (do_pre_copy) {
17921                                                 struct triple *user;
17922                                                 user = entry->member;
17923                                                 if (RHS(user, i) != ins) {
17924                                                         internal_error(state, user, "bad rhs");
17925                                                 }
17926                                                 tmp = pre_copy(state, user, i);
17927                                                 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
17928                                                 continue;
17929                                         } else {
17930                                                 do_post_copy = 1;
17931                                         }
17932                                 }
17933                                 reg = rinfo.reg;
17934                         }
17935                         if ((regcm & rinfo.regcm) == 0) {
17936                                 if (do_pre_copy) {
17937                                         struct triple *user;
17938                                         user = entry->member;
17939                                         if (RHS(user, i) != ins) {
17940                                                 internal_error(state, user, "bad rhs");
17941                                         }
17942                                         tmp = pre_copy(state, user, i);
17943                                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
17944                                         continue;
17945                                 } else {
17946                                         do_post_copy = 1;
17947                                 }
17948                         }
17949                         regcm &= rinfo.regcm;
17950                         
17951                 }
17952                 if (do_post_copy) {
17953                         struct reg_info pre, post;
17954                         tmp = post_copy(state, ins);
17955                         tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
17956                         pre = arch_reg_lhs(state, ins, 0);
17957                         post = arch_reg_lhs(state, tmp, 0);
17958                         if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
17959                                 internal_error(state, tmp, "useless copy");
17960                         }
17961                 }
17962         next:
17963                 ins = ins->next;
17964         } while(ins != first);
17965
17966         print_blocks(state, __func__, state->dbgout);
17967 }
17968
17969
17970 struct live_range_edge;
17971 struct live_range_def;
17972 struct live_range {
17973         struct live_range_edge *edges;
17974         struct live_range_def *defs;
17975 /* Note. The list pointed to by defs is kept in order.
17976  * That is baring splits in the flow control
17977  * defs dominates defs->next wich dominates defs->next->next
17978  * etc.
17979  */
17980         unsigned color;
17981         unsigned classes;
17982         unsigned degree;
17983         unsigned length;
17984         struct live_range *group_next, **group_prev;
17985 };
17986
17987 struct live_range_edge {
17988         struct live_range_edge *next;
17989         struct live_range *node;
17990 };
17991
17992 struct live_range_def {
17993         struct live_range_def *next;
17994         struct live_range_def *prev;
17995         struct live_range *lr;
17996         struct triple *def;
17997         unsigned orig_id;
17998 };
17999
18000 #define LRE_HASH_SIZE 2048
18001 struct lre_hash {
18002         struct lre_hash *next;
18003         struct live_range *left;
18004         struct live_range *right;
18005 };
18006
18007
18008 struct reg_state {
18009         struct lre_hash *hash[LRE_HASH_SIZE];
18010         struct reg_block *blocks;
18011         struct live_range_def *lrd;
18012         struct live_range *lr;
18013         struct live_range *low, **low_tail;
18014         struct live_range *high, **high_tail;
18015         unsigned defs;
18016         unsigned ranges;
18017         int passes, max_passes;
18018 };
18019
18020
18021 struct print_interference_block_info {
18022         struct reg_state *rstate;
18023         FILE *fp;
18024         int need_edges;
18025 };
18026 static void print_interference_block(
18027         struct compile_state *state, struct block *block, void *arg)
18028
18029 {
18030         struct print_interference_block_info *info = arg;
18031         struct reg_state *rstate = info->rstate;
18032         struct block_set *edge;
18033         FILE *fp = info->fp;
18034         struct reg_block *rb;
18035         struct triple *ptr;
18036         int phi_present;
18037         int done;
18038         rb = &rstate->blocks[block->vertex];
18039
18040         fprintf(fp, "\nblock: %p (%d),",
18041                 block,  block->vertex);
18042         for(edge = block->edges; edge; edge = edge->next) {
18043                 fprintf(fp, " %p<-%p",
18044                         edge->member, 
18045                         edge->member && edge->member->use?edge->member->use->member : 0);
18046         }
18047         fprintf(fp, "\n");
18048         if (rb->in) {
18049                 struct triple_reg_set *in_set;
18050                 fprintf(fp, "        in:");
18051                 for(in_set = rb->in; in_set; in_set = in_set->next) {
18052                         fprintf(fp, " %-10p", in_set->member);
18053                 }
18054                 fprintf(fp, "\n");
18055         }
18056         phi_present = 0;
18057         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18058                 done = (ptr == block->last);
18059                 if (ptr->op == OP_PHI) {
18060                         phi_present = 1;
18061                         break;
18062                 }
18063         }
18064         if (phi_present) {
18065                 int edge;
18066                 for(edge = 0; edge < block->users; edge++) {
18067                         fprintf(fp, "     in(%d):", edge);
18068                         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18069                                 struct triple **slot;
18070                                 done = (ptr == block->last);
18071                                 if (ptr->op != OP_PHI) {
18072                                         continue;
18073                                 }
18074                                 slot = &RHS(ptr, 0);
18075                                 fprintf(fp, " %-10p", slot[edge]);
18076                         }
18077                         fprintf(fp, "\n");
18078                 }
18079         }
18080         if (block->first->op == OP_LABEL) {
18081                 fprintf(fp, "%p:\n", block->first);
18082         }
18083         for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18084                 struct live_range *lr;
18085                 unsigned id;
18086                 int op;
18087                 op = ptr->op;
18088                 done = (ptr == block->last);
18089                 lr = rstate->lrd[ptr->id].lr;
18090                 
18091                 id = ptr->id;
18092                 ptr->id = rstate->lrd[id].orig_id;
18093                 SET_REG(ptr->id, lr->color);
18094                 display_triple(fp, ptr);
18095                 ptr->id = id;
18096
18097                 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18098                         internal_error(state, ptr, "lr has no defs!");
18099                 }
18100                 if (info->need_edges) {
18101                         if (lr->defs) {
18102                                 struct live_range_def *lrd;
18103                                 fprintf(fp, "       range:");
18104                                 lrd = lr->defs;
18105                                 do {
18106                                         fprintf(fp, " %-10p", lrd->def);
18107                                         lrd = lrd->next;
18108                                 } while(lrd != lr->defs);
18109                                 fprintf(fp, "\n");
18110                         }
18111                         if (lr->edges > 0) {
18112                                 struct live_range_edge *edge;
18113                                 fprintf(fp, "       edges:");
18114                                 for(edge = lr->edges; edge; edge = edge->next) {
18115                                         struct live_range_def *lrd;
18116                                         lrd = edge->node->defs;
18117                                         do {
18118                                                 fprintf(fp, " %-10p", lrd->def);
18119                                                 lrd = lrd->next;
18120                                         } while(lrd != edge->node->defs);
18121                                         fprintf(fp, "|");
18122                                 }
18123                                 fprintf(fp, "\n");
18124                         }
18125                 }
18126                 /* Do a bunch of sanity checks */
18127                 valid_ins(state, ptr);
18128                 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18129                         internal_error(state, ptr, "Invalid triple id: %d",
18130                                 ptr->id);
18131                 }
18132         }
18133         if (rb->out) {
18134                 struct triple_reg_set *out_set;
18135                 fprintf(fp, "       out:");
18136                 for(out_set = rb->out; out_set; out_set = out_set->next) {
18137                         fprintf(fp, " %-10p", out_set->member);
18138                 }
18139                 fprintf(fp, "\n");
18140         }
18141         fprintf(fp, "\n");
18142 }
18143
18144 static void print_interference_blocks(
18145         struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18146 {
18147         struct print_interference_block_info info;
18148         info.rstate = rstate;
18149         info.fp = fp;
18150         info.need_edges = need_edges;
18151         fprintf(fp, "\nlive variables by block\n");
18152         walk_blocks(state, &state->bb, print_interference_block, &info);
18153
18154 }
18155
18156 static unsigned regc_max_size(struct compile_state *state, int classes)
18157 {
18158         unsigned max_size;
18159         int i;
18160         max_size = 0;
18161         for(i = 0; i < MAX_REGC; i++) {
18162                 if (classes & (1 << i)) {
18163                         unsigned size;
18164                         size = arch_regc_size(state, i);
18165                         if (size > max_size) {
18166                                 max_size = size;
18167                         }
18168                 }
18169         }
18170         return max_size;
18171 }
18172
18173 static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18174 {
18175         unsigned equivs[MAX_REG_EQUIVS];
18176         int i;
18177         if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18178                 internal_error(state, 0, "invalid register");
18179         }
18180         if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18181                 internal_error(state, 0, "invalid register");
18182         }
18183         arch_reg_equivs(state, equivs, reg1);
18184         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18185                 if (equivs[i] == reg2) {
18186                         return 1;
18187                 }
18188         }
18189         return 0;
18190 }
18191
18192 static void reg_fill_used(struct compile_state *state, char *used, int reg)
18193 {
18194         unsigned equivs[MAX_REG_EQUIVS];
18195         int i;
18196         if (reg == REG_UNNEEDED) {
18197                 return;
18198         }
18199         arch_reg_equivs(state, equivs, reg);
18200         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18201                 used[equivs[i]] = 1;
18202         }
18203         return;
18204 }
18205
18206 static void reg_inc_used(struct compile_state *state, char *used, int reg)
18207 {
18208         unsigned equivs[MAX_REG_EQUIVS];
18209         int i;
18210         if (reg == REG_UNNEEDED) {
18211                 return;
18212         }
18213         arch_reg_equivs(state, equivs, reg);
18214         for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18215                 used[equivs[i]] += 1;
18216         }
18217         return;
18218 }
18219
18220 static unsigned int hash_live_edge(
18221         struct live_range *left, struct live_range *right)
18222 {
18223         unsigned int hash, val;
18224         unsigned long lval, rval;
18225         lval = ((unsigned long)left)/sizeof(struct live_range);
18226         rval = ((unsigned long)right)/sizeof(struct live_range);
18227         hash = 0;
18228         while(lval) {
18229                 val = lval & 0xff;
18230                 lval >>= 8;
18231                 hash = (hash *263) + val;
18232         }
18233         while(rval) {
18234                 val = rval & 0xff;
18235                 rval >>= 8;
18236                 hash = (hash *263) + val;
18237         }
18238         hash = hash & (LRE_HASH_SIZE - 1);
18239         return hash;
18240 }
18241
18242 static struct lre_hash **lre_probe(struct reg_state *rstate,
18243         struct live_range *left, struct live_range *right)
18244 {
18245         struct lre_hash **ptr;
18246         unsigned int index;
18247         /* Ensure left <= right */
18248         if (left > right) {
18249                 struct live_range *tmp;
18250                 tmp = left;
18251                 left = right;
18252                 right = tmp;
18253         }
18254         index = hash_live_edge(left, right);
18255         
18256         ptr = &rstate->hash[index];
18257         while(*ptr) {
18258                 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18259                         break;
18260                 }
18261                 ptr = &(*ptr)->next;
18262         }
18263         return ptr;
18264 }
18265
18266 static int interfere(struct reg_state *rstate,
18267         struct live_range *left, struct live_range *right)
18268 {
18269         struct lre_hash **ptr;
18270         ptr = lre_probe(rstate, left, right);
18271         return ptr && *ptr;
18272 }
18273
18274 static void add_live_edge(struct reg_state *rstate, 
18275         struct live_range *left, struct live_range *right)
18276 {
18277         /* FIXME the memory allocation overhead is noticeable here... */
18278         struct lre_hash **ptr, *new_hash;
18279         struct live_range_edge *edge;
18280
18281         if (left == right) {
18282                 return;
18283         }
18284         if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18285                 return;
18286         }
18287         /* Ensure left <= right */
18288         if (left > right) {
18289                 struct live_range *tmp;
18290                 tmp = left;
18291                 left = right;
18292                 right = tmp;
18293         }
18294         ptr = lre_probe(rstate, left, right);
18295         if (*ptr) {
18296                 return;
18297         }
18298 #if 0
18299         fprintf(state->errout, "new_live_edge(%p, %p)\n",
18300                 left, right);
18301 #endif
18302         new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18303         new_hash->next  = *ptr;
18304         new_hash->left  = left;
18305         new_hash->right = right;
18306         *ptr = new_hash;
18307
18308         edge = xmalloc(sizeof(*edge), "live_range_edge");
18309         edge->next   = left->edges;
18310         edge->node   = right;
18311         left->edges  = edge;
18312         left->degree += 1;
18313         
18314         edge = xmalloc(sizeof(*edge), "live_range_edge");
18315         edge->next    = right->edges;
18316         edge->node    = left;
18317         right->edges  = edge;
18318         right->degree += 1;
18319 }
18320
18321 static void remove_live_edge(struct reg_state *rstate,
18322         struct live_range *left, struct live_range *right)
18323 {
18324         struct live_range_edge *edge, **ptr;
18325         struct lre_hash **hptr, *entry;
18326         hptr = lre_probe(rstate, left, right);
18327         if (!hptr || !*hptr) {
18328                 return;
18329         }
18330         entry = *hptr;
18331         *hptr = entry->next;
18332         xfree(entry);
18333
18334         for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18335                 edge = *ptr;
18336                 if (edge->node == right) {
18337                         *ptr = edge->next;
18338                         memset(edge, 0, sizeof(*edge));
18339                         xfree(edge);
18340                         right->degree--;
18341                         break;
18342                 }
18343         }
18344         for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18345                 edge = *ptr;
18346                 if (edge->node == left) {
18347                         *ptr = edge->next;
18348                         memset(edge, 0, sizeof(*edge));
18349                         xfree(edge);
18350                         left->degree--;
18351                         break;
18352                 }
18353         }
18354 }
18355
18356 static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18357 {
18358         struct live_range_edge *edge, *next;
18359         for(edge = range->edges; edge; edge = next) {
18360                 next = edge->next;
18361                 remove_live_edge(rstate, range, edge->node);
18362         }
18363 }
18364
18365 static void transfer_live_edges(struct reg_state *rstate, 
18366         struct live_range *dest, struct live_range *src)
18367 {
18368         struct live_range_edge *edge, *next;
18369         for(edge = src->edges; edge; edge = next) {
18370                 struct live_range *other;
18371                 next = edge->next;
18372                 other = edge->node;
18373                 remove_live_edge(rstate, src, other);
18374                 add_live_edge(rstate, dest, other);
18375         }
18376 }
18377
18378
18379 /* Interference graph...
18380  * 
18381  * new(n) --- Return a graph with n nodes but no edges.
18382  * add(g,x,y) --- Return a graph including g with an between x and y
18383  * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18384  *                x and y in the graph g
18385  * degree(g, x) --- Return the degree of the node x in the graph g
18386  * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18387  *
18388  * Implement with a hash table && a set of adjcency vectors.
18389  * The hash table supports constant time implementations of add and interfere.
18390  * The adjacency vectors support an efficient implementation of neighbors.
18391  */
18392
18393 /* 
18394  *     +---------------------------------------------------+
18395  *     |         +--------------+                          |
18396  *     v         v              |                          |
18397  * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select 
18398  *
18399  * -- In simplify implment optimistic coloring... (No backtracking)
18400  * -- Implement Rematerialization it is the only form of spilling we can perform
18401  *    Essentially this means dropping a constant from a register because
18402  *    we can regenerate it later.
18403  *
18404  * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18405  *     coalesce at phi points...
18406  * --- Bias coloring if at all possible do the coalesing a compile time.
18407  *
18408  *
18409  */
18410
18411 static void different_colored(
18412         struct compile_state *state, struct reg_state *rstate, 
18413         struct triple *parent, struct triple *ins)
18414 {
18415         struct live_range *lr;
18416         struct triple **expr;
18417         lr = rstate->lrd[ins->id].lr;
18418         expr = triple_rhs(state, ins, 0);
18419         for(;expr; expr = triple_rhs(state, ins, expr)) {
18420                 struct live_range *lr2;
18421                 if (!*expr || (*expr == parent) || (*expr == ins)) {
18422                         continue;
18423                 }
18424                 lr2 = rstate->lrd[(*expr)->id].lr;
18425                 if (lr->color == lr2->color) {
18426                         internal_error(state, ins, "live range too big");
18427                 }
18428         }
18429 }
18430
18431
18432 static struct live_range *coalesce_ranges(
18433         struct compile_state *state, struct reg_state *rstate,
18434         struct live_range *lr1, struct live_range *lr2)
18435 {
18436         struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18437         unsigned color;
18438         unsigned classes;
18439         if (lr1 == lr2) {
18440                 return lr1;
18441         }
18442         if (!lr1->defs || !lr2->defs) {
18443                 internal_error(state, 0,
18444                         "cannot coalese dead live ranges");
18445         }
18446         if ((lr1->color == REG_UNNEEDED) ||
18447                 (lr2->color == REG_UNNEEDED)) {
18448                 internal_error(state, 0, 
18449                         "cannot coalesce live ranges without a possible color");
18450         }
18451         if ((lr1->color != lr2->color) &&
18452                 (lr1->color != REG_UNSET) &&
18453                 (lr2->color != REG_UNSET)) {
18454                 internal_error(state, lr1->defs->def, 
18455                         "cannot coalesce live ranges of different colors");
18456         }
18457         color = lr1->color;
18458         if (color == REG_UNSET) {
18459                 color = lr2->color;
18460         }
18461         classes = lr1->classes & lr2->classes;
18462         if (!classes) {
18463                 internal_error(state, lr1->defs->def,
18464                         "cannot coalesce live ranges with dissimilar register classes");
18465         }
18466         if (state->compiler->debug & DEBUG_COALESCING) {
18467                 FILE *fp = state->errout;
18468                 fprintf(fp, "coalescing:");
18469                 lrd = lr1->defs;
18470                 do {
18471                         fprintf(fp, " %p", lrd->def);
18472                         lrd = lrd->next;
18473                 } while(lrd != lr1->defs);
18474                 fprintf(fp, " |");
18475                 lrd = lr2->defs;
18476                 do {
18477                         fprintf(fp, " %p", lrd->def);
18478                         lrd = lrd->next;
18479                 } while(lrd != lr2->defs);
18480                 fprintf(fp, "\n");
18481         }
18482         /* If there is a clear dominate live range put it in lr1,
18483          * For purposes of this test phi functions are
18484          * considered dominated by the definitions that feed into
18485          * them. 
18486          */
18487         if ((lr1->defs->prev->def->op == OP_PHI) ||
18488                 ((lr2->defs->prev->def->op != OP_PHI) &&
18489                 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18490                 struct live_range *tmp;
18491                 tmp = lr1;
18492                 lr1 = lr2;
18493                 lr2 = tmp;
18494         }
18495 #if 0
18496         if (lr1->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
18497                 fprintf(state->errout, "lr1 post\n");
18498         }
18499         if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
18500                 fprintf(state->errout, "lr1 pre\n");
18501         }
18502         if (lr2->defs->orig_id  & TRIPLE_FLAG_POST_SPLIT) {
18503                 fprintf(state->errout, "lr2 post\n");
18504         }
18505         if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
18506                 fprintf(state->errout, "lr2 pre\n");
18507         }
18508 #endif
18509 #if 0
18510         fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
18511                 lr1->defs->def,
18512                 lr1->color,
18513                 lr2->defs->def,
18514                 lr2->color);
18515 #endif
18516         
18517         /* Append lr2 onto lr1 */
18518 #warning "FIXME should this be a merge instead of a splice?"
18519         /* This FIXME item applies to the correctness of live_range_end 
18520          * and to the necessity of making multiple passes of coalesce_live_ranges.
18521          * A failure to find some coalesce opportunities in coaleace_live_ranges
18522          * does not impact the correct of the compiler just the efficiency with
18523          * which registers are allocated.
18524          */
18525         head = lr1->defs;
18526         mid1 = lr1->defs->prev;
18527         mid2 = lr2->defs;
18528         end  = lr2->defs->prev;
18529         
18530         head->prev = end;
18531         end->next  = head;
18532
18533         mid1->next = mid2;
18534         mid2->prev = mid1;
18535
18536         /* Fixup the live range in the added live range defs */
18537         lrd = head;
18538         do {
18539                 lrd->lr = lr1;
18540                 lrd = lrd->next;
18541         } while(lrd != head);
18542
18543         /* Mark lr2 as free. */
18544         lr2->defs = 0;
18545         lr2->color = REG_UNNEEDED;
18546         lr2->classes = 0;
18547
18548         if (!lr1->defs) {
18549                 internal_error(state, 0, "lr1->defs == 0 ?");
18550         }
18551
18552         lr1->color   = color;
18553         lr1->classes = classes;
18554
18555         /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18556         transfer_live_edges(rstate, lr1, lr2);
18557
18558         return lr1;
18559 }
18560
18561 static struct live_range_def *live_range_head(
18562         struct compile_state *state, struct live_range *lr,
18563         struct live_range_def *last)
18564 {
18565         struct live_range_def *result;
18566         result = 0;
18567         if (last == 0) {
18568                 result = lr->defs;
18569         }
18570         else if (!tdominates(state, lr->defs->def, last->next->def)) {
18571                 result = last->next;
18572         }
18573         return result;
18574 }
18575
18576 static struct live_range_def *live_range_end(
18577         struct compile_state *state, struct live_range *lr,
18578         struct live_range_def *last)
18579 {
18580         struct live_range_def *result;
18581         result = 0;
18582         if (last == 0) {
18583                 result = lr->defs->prev;
18584         }
18585         else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18586                 result = last->prev;
18587         }
18588         return result;
18589 }
18590
18591
18592 static void initialize_live_ranges(
18593         struct compile_state *state, struct reg_state *rstate)
18594 {
18595         struct triple *ins, *first;
18596         size_t count, size;
18597         int i, j;
18598
18599         first = state->first;
18600         /* First count how many instructions I have.
18601          */
18602         count = count_triples(state);
18603         /* Potentially I need one live range definitions for each
18604          * instruction.
18605          */
18606         rstate->defs = count;
18607         /* Potentially I need one live range for each instruction
18608          * plus an extra for the dummy live range.
18609          */
18610         rstate->ranges = count + 1;
18611         size = sizeof(rstate->lrd[0]) * rstate->defs;
18612         rstate->lrd = xcmalloc(size, "live_range_def");
18613         size = sizeof(rstate->lr[0]) * rstate->ranges;
18614         rstate->lr  = xcmalloc(size, "live_range");
18615
18616         /* Setup the dummy live range */
18617         rstate->lr[0].classes = 0;
18618         rstate->lr[0].color = REG_UNSET;
18619         rstate->lr[0].defs = 0;
18620         i = j = 0;
18621         ins = first;
18622         do {
18623                 /* If the triple is a variable give it a live range */
18624                 if (triple_is_def(state, ins)) {
18625                         struct reg_info info;
18626                         /* Find the architecture specific color information */
18627                         info = find_def_color(state, ins);
18628                         i++;
18629                         rstate->lr[i].defs    = &rstate->lrd[j];
18630                         rstate->lr[i].color   = info.reg;
18631                         rstate->lr[i].classes = info.regcm;
18632                         rstate->lr[i].degree  = 0;
18633                         rstate->lrd[j].lr = &rstate->lr[i];
18634                 } 
18635                 /* Otherwise give the triple the dummy live range. */
18636                 else {
18637                         rstate->lrd[j].lr = &rstate->lr[0];
18638                 }
18639
18640                 /* Initalize the live_range_def */
18641                 rstate->lrd[j].next    = &rstate->lrd[j];
18642                 rstate->lrd[j].prev    = &rstate->lrd[j];
18643                 rstate->lrd[j].def     = ins;
18644                 rstate->lrd[j].orig_id = ins->id;
18645                 ins->id = j;
18646
18647                 j++;
18648                 ins = ins->next;
18649         } while(ins != first);
18650         rstate->ranges = i;
18651
18652         /* Make a second pass to handle achitecture specific register
18653          * constraints.
18654          */
18655         ins = first;
18656         do {
18657                 int zlhs, zrhs, i, j;
18658                 if (ins->id > rstate->defs) {
18659                         internal_error(state, ins, "bad id");
18660                 }
18661                 
18662                 /* Walk through the template of ins and coalesce live ranges */
18663                 zlhs = ins->lhs;
18664                 if ((zlhs == 0) && triple_is_def(state, ins)) {
18665                         zlhs = 1;
18666                 }
18667                 zrhs = ins->rhs;
18668
18669                 if (state->compiler->debug & DEBUG_COALESCING2) {
18670                         fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
18671                                 ins, zlhs, zrhs);
18672                 }
18673
18674                 for(i = 0; i < zlhs; i++) {
18675                         struct reg_info linfo;
18676                         struct live_range_def *lhs;
18677                         linfo = arch_reg_lhs(state, ins, i);
18678                         if (linfo.reg < MAX_REGISTERS) {
18679                                 continue;
18680                         }
18681                         if (triple_is_def(state, ins)) {
18682                                 lhs = &rstate->lrd[ins->id];
18683                         } else {
18684                                 lhs = &rstate->lrd[LHS(ins, i)->id];
18685                         }
18686
18687                         if (state->compiler->debug & DEBUG_COALESCING2) {
18688                                 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
18689                                         i, lhs, linfo.reg);
18690                         }
18691
18692                         for(j = 0; j < zrhs; j++) {
18693                                 struct reg_info rinfo;
18694                                 struct live_range_def *rhs;
18695                                 rinfo = arch_reg_rhs(state, ins, j);
18696                                 if (rinfo.reg < MAX_REGISTERS) {
18697                                         continue;
18698                                 }
18699                                 rhs = &rstate->lrd[RHS(ins, j)->id];
18700
18701                                 if (state->compiler->debug & DEBUG_COALESCING2) {
18702                                         fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
18703                                                 j, rhs, rinfo.reg);
18704                                 }
18705
18706                                 if (rinfo.reg == linfo.reg) {
18707                                         coalesce_ranges(state, rstate, 
18708                                                 lhs->lr, rhs->lr);
18709                                 }
18710                         }
18711                 }
18712                 ins = ins->next;
18713         } while(ins != first);
18714 }
18715
18716 static void graph_ins(
18717         struct compile_state *state, 
18718         struct reg_block *blocks, struct triple_reg_set *live, 
18719         struct reg_block *rb, struct triple *ins, void *arg)
18720 {
18721         struct reg_state *rstate = arg;
18722         struct live_range *def;
18723         struct triple_reg_set *entry;
18724
18725         /* If the triple is not a definition
18726          * we do not have a definition to add to
18727          * the interference graph.
18728          */
18729         if (!triple_is_def(state, ins)) {
18730                 return;
18731         }
18732         def = rstate->lrd[ins->id].lr;
18733         
18734         /* Create an edge between ins and everything that is
18735          * alive, unless the live_range cannot share
18736          * a physical register with ins.
18737          */
18738         for(entry = live; entry; entry = entry->next) {
18739                 struct live_range *lr;
18740                 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18741                         internal_error(state, 0, "bad entry?");
18742                 }
18743                 lr = rstate->lrd[entry->member->id].lr;
18744                 if (def == lr) {
18745                         continue;
18746                 }
18747                 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18748                         continue;
18749                 }
18750                 add_live_edge(rstate, def, lr);
18751         }
18752         return;
18753 }
18754
18755 static struct live_range *get_verify_live_range(
18756         struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18757 {
18758         struct live_range *lr;
18759         struct live_range_def *lrd;
18760         int ins_found;
18761         if ((ins->id < 0) || (ins->id > rstate->defs)) {
18762                 internal_error(state, ins, "bad ins?");
18763         }
18764         lr = rstate->lrd[ins->id].lr;
18765         ins_found = 0;
18766         lrd = lr->defs;
18767         do {
18768                 if (lrd->def == ins) {
18769                         ins_found = 1;
18770                 }
18771                 lrd = lrd->next;
18772         } while(lrd != lr->defs);
18773         if (!ins_found) {
18774                 internal_error(state, ins, "ins not in live range");
18775         }
18776         return lr;
18777 }
18778
18779 static void verify_graph_ins(
18780         struct compile_state *state, 
18781         struct reg_block *blocks, struct triple_reg_set *live, 
18782         struct reg_block *rb, struct triple *ins, void *arg)
18783 {
18784         struct reg_state *rstate = arg;
18785         struct triple_reg_set *entry1, *entry2;
18786
18787
18788         /* Compare live against edges and make certain the code is working */
18789         for(entry1 = live; entry1; entry1 = entry1->next) {
18790                 struct live_range *lr1;
18791                 lr1 = get_verify_live_range(state, rstate, entry1->member);
18792                 for(entry2 = live; entry2; entry2 = entry2->next) {
18793                         struct live_range *lr2;
18794                         struct live_range_edge *edge2;
18795                         int lr1_found;
18796                         int lr2_degree;
18797                         if (entry2 == entry1) {
18798                                 continue;
18799                         }
18800                         lr2 = get_verify_live_range(state, rstate, entry2->member);
18801                         if (lr1 == lr2) {
18802                                 internal_error(state, entry2->member, 
18803                                         "live range with 2 values simultaneously alive");
18804                         }
18805                         if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
18806                                 continue;
18807                         }
18808                         if (!interfere(rstate, lr1, lr2)) {
18809                                 internal_error(state, entry2->member, 
18810                                         "edges don't interfere?");
18811                         }
18812                                 
18813                         lr1_found = 0;
18814                         lr2_degree = 0;
18815                         for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
18816                                 lr2_degree++;
18817                                 if (edge2->node == lr1) {
18818                                         lr1_found = 1;
18819                                 }
18820                         }
18821                         if (lr2_degree != lr2->degree) {
18822                                 internal_error(state, entry2->member,
18823                                         "computed degree: %d does not match reported degree: %d\n",
18824                                         lr2_degree, lr2->degree);
18825                         }
18826                         if (!lr1_found) {
18827                                 internal_error(state, entry2->member, "missing edge");
18828                         }
18829                 }
18830         }
18831         return;
18832 }
18833
18834
18835 static void print_interference_ins(
18836         struct compile_state *state, 
18837         struct reg_block *blocks, struct triple_reg_set *live, 
18838         struct reg_block *rb, struct triple *ins, void *arg)
18839 {
18840         struct reg_state *rstate = arg;
18841         struct live_range *lr;
18842         unsigned id;
18843         FILE *fp = state->dbgout;
18844
18845         lr = rstate->lrd[ins->id].lr;
18846         id = ins->id;
18847         ins->id = rstate->lrd[id].orig_id;
18848         SET_REG(ins->id, lr->color);
18849         display_triple(state->dbgout, ins);
18850         ins->id = id;
18851
18852         if (lr->defs) {
18853                 struct live_range_def *lrd;
18854                 fprintf(fp, "       range:");
18855                 lrd = lr->defs;
18856                 do {
18857                         fprintf(fp, " %-10p", lrd->def);
18858                         lrd = lrd->next;
18859                 } while(lrd != lr->defs);
18860                 fprintf(fp, "\n");
18861         }
18862         if (live) {
18863                 struct triple_reg_set *entry;
18864                 fprintf(fp, "        live:");
18865                 for(entry = live; entry; entry = entry->next) {
18866                         fprintf(fp, " %-10p", entry->member);
18867                 }
18868                 fprintf(fp, "\n");
18869         }
18870         if (lr->edges) {
18871                 struct live_range_edge *entry;
18872                 fprintf(fp, "       edges:");
18873                 for(entry = lr->edges; entry; entry = entry->next) {
18874                         struct live_range_def *lrd;
18875                         lrd = entry->node->defs;
18876                         do {
18877                                 fprintf(fp, " %-10p", lrd->def);
18878                                 lrd = lrd->next;
18879                         } while(lrd != entry->node->defs);
18880                         fprintf(fp, "|");
18881                 }
18882                 fprintf(fp, "\n");
18883         }
18884         if (triple_is_branch(state, ins)) {
18885                 fprintf(fp, "\n");
18886         }
18887         return;
18888 }
18889
18890 static int coalesce_live_ranges(
18891         struct compile_state *state, struct reg_state *rstate)
18892 {
18893         /* At the point where a value is moved from one
18894          * register to another that value requires two
18895          * registers, thus increasing register pressure.
18896          * Live range coaleescing reduces the register
18897          * pressure by keeping a value in one register
18898          * longer.
18899          *
18900          * In the case of a phi function all paths leading
18901          * into it must be allocated to the same register
18902          * otherwise the phi function may not be removed.
18903          *
18904          * Forcing a value to stay in a single register
18905          * for an extended period of time does have
18906          * limitations when applied to non homogenous
18907          * register pool.  
18908          *
18909          * The two cases I have identified are:
18910          * 1) Two forced register assignments may
18911          *    collide.
18912          * 2) Registers may go unused because they
18913          *    are only good for storing the value
18914          *    and not manipulating it.
18915          *
18916          * Because of this I need to split live ranges,
18917          * even outside of the context of coalesced live
18918          * ranges.  The need to split live ranges does
18919          * impose some constraints on live range coalescing.
18920          *
18921          * - Live ranges may not be coalesced across phi
18922          *   functions.  This creates a 2 headed live
18923          *   range that cannot be sanely split.
18924          *
18925          * - phi functions (coalesced in initialize_live_ranges) 
18926          *   are handled as pre split live ranges so we will
18927          *   never attempt to split them.
18928          */
18929         int coalesced;
18930         int i;
18931
18932         coalesced = 0;
18933         for(i = 0; i <= rstate->ranges; i++) {
18934                 struct live_range *lr1;
18935                 struct live_range_def *lrd1;
18936                 lr1 = &rstate->lr[i];
18937                 if (!lr1->defs) {
18938                         continue;
18939                 }
18940                 lrd1 = live_range_end(state, lr1, 0);
18941                 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
18942                         struct triple_set *set;
18943                         if (lrd1->def->op != OP_COPY) {
18944                                 continue;
18945                         }
18946                         /* Skip copies that are the result of a live range split. */
18947                         if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
18948                                 continue;
18949                         }
18950                         for(set = lrd1->def->use; set; set = set->next) {
18951                                 struct live_range_def *lrd2;
18952                                 struct live_range *lr2, *res;
18953
18954                                 lrd2 = &rstate->lrd[set->member->id];
18955
18956                                 /* Don't coalesce with instructions
18957                                  * that are the result of a live range
18958                                  * split.
18959                                  */
18960                                 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
18961                                         continue;
18962                                 }
18963                                 lr2 = rstate->lrd[set->member->id].lr;
18964                                 if (lr1 == lr2) {
18965                                         continue;
18966                                 }
18967                                 if ((lr1->color != lr2->color) &&
18968                                         (lr1->color != REG_UNSET) &&
18969                                         (lr2->color != REG_UNSET)) {
18970                                         continue;
18971                                 }
18972                                 if ((lr1->classes & lr2->classes) == 0) {
18973                                         continue;
18974                                 }
18975                                 
18976                                 if (interfere(rstate, lr1, lr2)) {
18977                                         continue;
18978                                 }
18979
18980                                 res = coalesce_ranges(state, rstate, lr1, lr2);
18981                                 coalesced += 1;
18982                                 if (res != lr1) {
18983                                         goto next;
18984                                 }
18985                         }
18986                 }
18987         next:
18988                 ;
18989         }
18990         return coalesced;
18991 }
18992
18993
18994 static void fix_coalesce_conflicts(struct compile_state *state,
18995         struct reg_block *blocks, struct triple_reg_set *live,
18996         struct reg_block *rb, struct triple *ins, void *arg)
18997 {
18998         int *conflicts = arg;
18999         int zlhs, zrhs, i, j;
19000
19001         /* See if we have a mandatory coalesce operation between
19002          * a lhs and a rhs value.  If so and the rhs value is also
19003          * alive then this triple needs to be pre copied.  Otherwise
19004          * we would have two definitions in the same live range simultaneously
19005          * alive.
19006          */
19007         zlhs = ins->lhs;
19008         if ((zlhs == 0) && triple_is_def(state, ins)) {
19009                 zlhs = 1;
19010         }
19011         zrhs = ins->rhs;
19012         for(i = 0; i < zlhs; i++) {
19013                 struct reg_info linfo;
19014                 linfo = arch_reg_lhs(state, ins, i);
19015                 if (linfo.reg < MAX_REGISTERS) {
19016                         continue;
19017                 }
19018                 for(j = 0; j < zrhs; j++) {
19019                         struct reg_info rinfo;
19020                         struct triple *rhs;
19021                         struct triple_reg_set *set;
19022                         int found;
19023                         found = 0;
19024                         rinfo = arch_reg_rhs(state, ins, j);
19025                         if (rinfo.reg != linfo.reg) {
19026                                 continue;
19027                         }
19028                         rhs = RHS(ins, j);
19029                         for(set = live; set && !found; set = set->next) {
19030                                 if (set->member == rhs) {
19031                                         found = 1;
19032                                 }
19033                         }
19034                         if (found) {
19035                                 struct triple *copy;
19036                                 copy = pre_copy(state, ins, j);
19037                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
19038                                 (*conflicts)++;
19039                         }
19040                 }
19041         }
19042         return;
19043 }
19044
19045 static int correct_coalesce_conflicts(
19046         struct compile_state *state, struct reg_block *blocks)
19047 {
19048         int conflicts;
19049         conflicts = 0;
19050         walk_variable_lifetimes(state, &state->bb, blocks, 
19051                 fix_coalesce_conflicts, &conflicts);
19052         return conflicts;
19053 }
19054
19055 static void replace_set_use(struct compile_state *state,
19056         struct triple_reg_set *head, struct triple *orig, struct triple *new)
19057 {
19058         struct triple_reg_set *set;
19059         for(set = head; set; set = set->next) {
19060                 if (set->member == orig) {
19061                         set->member = new;
19062                 }
19063         }
19064 }
19065
19066 static void replace_block_use(struct compile_state *state, 
19067         struct reg_block *blocks, struct triple *orig, struct triple *new)
19068 {
19069         int i;
19070 #warning "WISHLIST visit just those blocks that need it *"
19071         for(i = 1; i <= state->bb.last_vertex; i++) {
19072                 struct reg_block *rb;
19073                 rb = &blocks[i];
19074                 replace_set_use(state, rb->in, orig, new);
19075                 replace_set_use(state, rb->out, orig, new);
19076         }
19077 }
19078
19079 static void color_instructions(struct compile_state *state)
19080 {
19081         struct triple *ins, *first;
19082         first = state->first;
19083         ins = first;
19084         do {
19085                 if (triple_is_def(state, ins)) {
19086                         struct reg_info info;
19087                         info = find_lhs_color(state, ins, 0);
19088                         if (info.reg >= MAX_REGISTERS) {
19089                                 info.reg = REG_UNSET;
19090                         }
19091                         SET_INFO(ins->id, info);
19092                 }
19093                 ins = ins->next;
19094         } while(ins != first);
19095 }
19096
19097 static struct reg_info read_lhs_color(
19098         struct compile_state *state, struct triple *ins, int index)
19099 {
19100         struct reg_info info;
19101         if ((index == 0) && triple_is_def(state, ins)) {
19102                 info.reg   = ID_REG(ins->id);
19103                 info.regcm = ID_REGCM(ins->id);
19104         }
19105         else if (index < ins->lhs) {
19106                 info = read_lhs_color(state, LHS(ins, index), 0);
19107         }
19108         else {
19109                 internal_error(state, ins, "Bad lhs %d", index);
19110                 info.reg = REG_UNSET;
19111                 info.regcm = 0;
19112         }
19113         return info;
19114 }
19115
19116 static struct triple *resolve_tangle(
19117         struct compile_state *state, struct triple *tangle)
19118 {
19119         struct reg_info info, uinfo;
19120         struct triple_set *set, *next;
19121         struct triple *copy;
19122
19123 #warning "WISHLIST recalculate all affected instructions colors"
19124         info = find_lhs_color(state, tangle, 0);
19125         for(set = tangle->use; set; set = next) {
19126                 struct triple *user;
19127                 int i, zrhs;
19128                 next = set->next;
19129                 user = set->member;
19130                 zrhs = user->rhs;
19131                 for(i = 0; i < zrhs; i++) {
19132                         if (RHS(user, i) != tangle) {
19133                                 continue;
19134                         }
19135                         uinfo = find_rhs_post_color(state, user, i);
19136                         if (uinfo.reg == info.reg) {
19137                                 copy = pre_copy(state, user, i);
19138                                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
19139                                 SET_INFO(copy->id, uinfo);
19140                         }
19141                 }
19142         }
19143         copy = 0;
19144         uinfo = find_lhs_pre_color(state, tangle, 0);
19145         if (uinfo.reg == info.reg) {
19146                 struct reg_info linfo;
19147                 copy = post_copy(state, tangle);
19148                 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
19149                 linfo = find_lhs_color(state, copy, 0);
19150                 SET_INFO(copy->id, linfo);
19151         }
19152         info = find_lhs_color(state, tangle, 0);
19153         SET_INFO(tangle->id, info);
19154         
19155         return copy;
19156 }
19157
19158
19159 static void fix_tangles(struct compile_state *state,
19160         struct reg_block *blocks, struct triple_reg_set *live,
19161         struct reg_block *rb, struct triple *ins, void *arg)
19162 {
19163         int *tangles = arg;
19164         struct triple *tangle;
19165         do {
19166                 char used[MAX_REGISTERS];
19167                 struct triple_reg_set *set;
19168                 tangle = 0;
19169
19170                 /* Find out which registers have multiple uses at this point */
19171                 memset(used, 0, sizeof(used));
19172                 for(set = live; set; set = set->next) {
19173                         struct reg_info info;
19174                         info = read_lhs_color(state, set->member, 0);
19175                         if (info.reg == REG_UNSET) {
19176                                 continue;
19177                         }
19178                         reg_inc_used(state, used, info.reg);
19179                 }
19180                 
19181                 /* Now find the least dominated definition of a register in
19182                  * conflict I have seen so far.
19183                  */
19184                 for(set = live; set; set = set->next) {
19185                         struct reg_info info;
19186                         info = read_lhs_color(state, set->member, 0);
19187                         if (used[info.reg] < 2) {
19188                                 continue;
19189                         }
19190                         /* Changing copies that feed into phi functions
19191                          * is incorrect.
19192                          */
19193                         if (set->member->use && 
19194                                 (set->member->use->member->op == OP_PHI)) {
19195                                 continue;
19196                         }
19197                         if (!tangle || tdominates(state, set->member, tangle)) {
19198                                 tangle = set->member;
19199                         }
19200                 }
19201                 /* If I have found a tangle resolve it */
19202                 if (tangle) {
19203                         struct triple *post_copy;
19204                         (*tangles)++;
19205                         post_copy = resolve_tangle(state, tangle);
19206                         if (post_copy) {
19207                                 replace_block_use(state, blocks, tangle, post_copy);
19208                         }
19209                         if (post_copy && (tangle != ins)) {
19210                                 replace_set_use(state, live, tangle, post_copy);
19211                         }
19212                 }
19213         } while(tangle);
19214         return;
19215 }
19216
19217 static int correct_tangles(
19218         struct compile_state *state, struct reg_block *blocks)
19219 {
19220         int tangles;
19221         tangles = 0;
19222         color_instructions(state);
19223         walk_variable_lifetimes(state, &state->bb, blocks, 
19224                 fix_tangles, &tangles);
19225         return tangles;
19226 }
19227
19228
19229 static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19230 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19231
19232 struct triple *find_constrained_def(
19233         struct compile_state *state, struct live_range *range, struct triple *constrained)
19234 {
19235         struct live_range_def *lrd, *lrd_next;
19236         lrd_next = range->defs;
19237         do {
19238                 struct reg_info info;
19239                 unsigned regcm;
19240
19241                 lrd = lrd_next;
19242                 lrd_next = lrd->next;
19243
19244                 regcm = arch_type_to_regcm(state, lrd->def->type);
19245                 info = find_lhs_color(state, lrd->def, 0);
19246                 regcm      = arch_regcm_reg_normalize(state, regcm);
19247                 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
19248                 /* If the 2 register class masks are equal then
19249                  * the current register class is not constrained.
19250                  */
19251                 if (regcm == info.regcm) {
19252                         continue;
19253                 }
19254                 
19255                 /* If there is just one use.
19256                  * That use cannot accept a larger register class.
19257                  * There are no intervening definitions except
19258                  * definitions that feed into that use.
19259                  * Then a triple is not constrained.
19260                  * FIXME handle this case!
19261                  */
19262 #warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
19263                 
19264
19265                 /* Of the constrained live ranges deal with the
19266                  * least dominated one first.
19267                  */
19268                 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19269                         fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
19270                                 lrd->def, tops(lrd->def->op), regcm, info.regcm);
19271                 }
19272                 if (!constrained || 
19273                         tdominates(state, lrd->def, constrained))
19274                 {
19275                         constrained = lrd->def;
19276                 }
19277         } while(lrd_next != range->defs);
19278         return constrained;
19279 }
19280
19281 static int split_constrained_ranges(
19282         struct compile_state *state, struct reg_state *rstate, 
19283         struct live_range *range)
19284 {
19285         /* Walk through the edges in conflict and our current live
19286          * range, and find definitions that are more severly constrained
19287          * than they type of data they contain require.
19288          * 
19289          * Then pick one of those ranges and relax the constraints.
19290          */
19291         struct live_range_edge *edge;
19292         struct triple *constrained;
19293
19294         constrained = 0;
19295         for(edge = range->edges; edge; edge = edge->next) {
19296                 constrained = find_constrained_def(state, edge->node, constrained);
19297         }
19298 #warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
19299         if (!constrained) {
19300                 constrained = find_constrained_def(state, range, constrained);
19301         }
19302
19303         if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19304                 fprintf(state->errout, "constrained: ");
19305                 display_triple(state->errout, constrained);
19306         }
19307         if (constrained) {
19308                 ids_from_rstate(state, rstate);
19309                 cleanup_rstate(state, rstate);
19310                 resolve_tangle(state, constrained);
19311         }
19312         return !!constrained;
19313 }
19314         
19315 static int split_ranges(
19316         struct compile_state *state, struct reg_state *rstate,
19317         char *used, struct live_range *range)
19318 {
19319         int split;
19320         if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19321                 fprintf(state->errout, "split_ranges %d %s %p\n", 
19322                         rstate->passes, tops(range->defs->def->op), range->defs->def);
19323         }
19324         if ((range->color == REG_UNNEEDED) ||
19325                 (rstate->passes >= rstate->max_passes)) {
19326                 return 0;
19327         }
19328         split = split_constrained_ranges(state, rstate, range);
19329
19330         /* Ideally I would split the live range that will not be used
19331          * for the longest period of time in hopes that this will 
19332          * (a) allow me to spill a register or
19333          * (b) allow me to place a value in another register.
19334          *
19335          * So far I don't have a test case for this, the resolving
19336          * of mandatory constraints has solved all of my
19337          * know issues.  So I have choosen not to write any
19338          * code until I cat get a better feel for cases where
19339          * it would be useful to have.
19340          *
19341          */
19342 #warning "WISHLIST implement live range splitting..."
19343         
19344         if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
19345                 FILE *fp = state->errout;
19346                 print_interference_blocks(state, rstate, fp, 0);
19347                 print_dominators(state, fp, &state->bb);
19348         }
19349         return split;
19350 }
19351
19352 static FILE *cgdebug_fp(struct compile_state *state)
19353 {
19354         FILE *fp;
19355         fp = 0;
19356         if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
19357                 fp = state->errout;
19358         }
19359         if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
19360                 fp = state->dbgout;
19361         }
19362         return fp;
19363 }
19364
19365 static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19366 {
19367         FILE *fp;
19368         fp = cgdebug_fp(state);
19369         if (fp) {
19370                 va_list args;
19371                 va_start(args, fmt);
19372                 vfprintf(fp, fmt, args);
19373                 va_end(args);
19374         }
19375 }
19376
19377 static void cgdebug_flush(struct compile_state *state)
19378 {
19379         FILE *fp;
19380         fp = cgdebug_fp(state);
19381         if (fp) {
19382                 fflush(fp);
19383         }
19384 }
19385
19386 static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19387 {
19388         FILE *fp;
19389         fp = cgdebug_fp(state);
19390         if (fp) {
19391                 loc(fp, state, ins);
19392         }
19393 }
19394
19395 static int select_free_color(struct compile_state *state, 
19396         struct reg_state *rstate, struct live_range *range)
19397 {
19398         struct triple_set *entry;
19399         struct live_range_def *lrd;
19400         struct live_range_def *phi;
19401         struct live_range_edge *edge;
19402         char used[MAX_REGISTERS];
19403         struct triple **expr;
19404
19405         /* Instead of doing just the trivial color select here I try
19406          * a few extra things because a good color selection will help reduce
19407          * copies.
19408          */
19409
19410         /* Find the registers currently in use */
19411         memset(used, 0, sizeof(used));
19412         for(edge = range->edges; edge; edge = edge->next) {
19413                 if (edge->node->color == REG_UNSET) {
19414                         continue;
19415                 }
19416                 reg_fill_used(state, used, edge->node->color);
19417         }
19418
19419         if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
19420                 int i;
19421                 i = 0;
19422                 for(edge = range->edges; edge; edge = edge->next) {
19423                         i++;
19424                 }
19425                 cgdebug_printf(state, "\n%s edges: %d", 
19426                         tops(range->defs->def->op), i);
19427                 cgdebug_loc(state, range->defs->def);
19428                 cgdebug_printf(state, "\n");
19429                 for(i = 0; i < MAX_REGISTERS; i++) {
19430                         if (used[i]) {
19431                                 cgdebug_printf(state, "used: %s\n",
19432                                         arch_reg_str(i));
19433                         }
19434                 }
19435         }       
19436
19437         /* If a color is already assigned see if it will work */
19438         if (range->color != REG_UNSET) {
19439                 struct live_range_def *lrd;
19440                 if (!used[range->color]) {
19441                         return 1;
19442                 }
19443                 for(edge = range->edges; edge; edge = edge->next) {
19444                         if (edge->node->color != range->color) {
19445                                 continue;
19446                         }
19447                         warning(state, edge->node->defs->def, "edge: ");
19448                         lrd = edge->node->defs;
19449                         do {
19450                                 warning(state, lrd->def, " %p %s",
19451                                         lrd->def, tops(lrd->def->op));
19452                                 lrd = lrd->next;
19453                         } while(lrd != edge->node->defs);
19454                 }
19455                 lrd = range->defs;
19456                 warning(state, range->defs->def, "def: ");
19457                 do {
19458                         warning(state, lrd->def, " %p %s",
19459                                 lrd->def, tops(lrd->def->op));
19460                         lrd = lrd->next;
19461                 } while(lrd != range->defs);
19462                 internal_error(state, range->defs->def,
19463                         "live range with already used color %s",
19464                         arch_reg_str(range->color));
19465         }
19466
19467         /* If I feed into an expression reuse it's color.
19468          * This should help remove copies in the case of 2 register instructions
19469          * and phi functions.
19470          */
19471         phi = 0;
19472         lrd = live_range_end(state, range, 0);
19473         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19474                 entry = lrd->def->use;
19475                 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19476                         struct live_range_def *insd;
19477                         unsigned regcm;
19478                         insd = &rstate->lrd[entry->member->id];
19479                         if (insd->lr->defs == 0) {
19480                                 continue;
19481                         }
19482                         if (!phi && (insd->def->op == OP_PHI) &&
19483                                 !interfere(rstate, range, insd->lr)) {
19484                                 phi = insd;
19485                         }
19486                         if (insd->lr->color == REG_UNSET) {
19487                                 continue;
19488                         }
19489                         regcm = insd->lr->classes;
19490                         if (((regcm & range->classes) == 0) ||
19491                                 (used[insd->lr->color])) {
19492                                 continue;
19493                         }
19494                         if (interfere(rstate, range, insd->lr)) {
19495                                 continue;
19496                         }
19497                         range->color = insd->lr->color;
19498                 }
19499         }
19500         /* If I feed into a phi function reuse it's color or the color
19501          * of something else that feeds into the phi function.
19502          */
19503         if (phi) {
19504                 if (phi->lr->color != REG_UNSET) {
19505                         if (used[phi->lr->color]) {
19506                                 range->color = phi->lr->color;
19507                         }
19508                 }
19509                 else {
19510                         expr = triple_rhs(state, phi->def, 0);
19511                         for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19512                                 struct live_range *lr;
19513                                 unsigned regcm;
19514                                 if (!*expr) {
19515                                         continue;
19516                                 }
19517                                 lr = rstate->lrd[(*expr)->id].lr;
19518                                 if (lr->color == REG_UNSET) {
19519                                         continue;
19520                                 }
19521                                 regcm = lr->classes;
19522                                 if (((regcm & range->classes) == 0) ||
19523                                         (used[lr->color])) {
19524                                         continue;
19525                                 }
19526                                 if (interfere(rstate, range, lr)) {
19527                                         continue;
19528                                 }
19529                                 range->color = lr->color;
19530                         }
19531                 }
19532         }
19533         /* If I don't interfere with a rhs node reuse it's color */
19534         lrd = live_range_head(state, range, 0);
19535         for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19536                 expr = triple_rhs(state, lrd->def, 0);
19537                 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
19538                         struct live_range *lr;
19539                         unsigned regcm;
19540                         if (!*expr) {
19541                                 continue;
19542                         }
19543                         lr = rstate->lrd[(*expr)->id].lr;
19544                         if (lr->color == REG_UNSET) {
19545                                 continue;
19546                         }
19547                         regcm = lr->classes;
19548                         if (((regcm & range->classes) == 0) ||
19549                                 (used[lr->color])) {
19550                                 continue;
19551                         }
19552                         if (interfere(rstate, range, lr)) {
19553                                 continue;
19554                         }
19555                         range->color = lr->color;
19556                         break;
19557                 }
19558         }
19559         /* If I have not opportunitically picked a useful color
19560          * pick the first color that is free.
19561          */
19562         if (range->color == REG_UNSET) {
19563                 range->color = 
19564                         arch_select_free_register(state, used, range->classes);
19565         }
19566         if (range->color == REG_UNSET) {
19567                 struct live_range_def *lrd;
19568                 int i;
19569                 if (split_ranges(state, rstate, used, range)) {
19570                         return 0;
19571                 }
19572                 for(edge = range->edges; edge; edge = edge->next) {
19573                         warning(state, edge->node->defs->def, "edge reg %s",
19574                                 arch_reg_str(edge->node->color));
19575                         lrd = edge->node->defs;
19576                         do {
19577                                 warning(state, lrd->def, " %s %p",
19578                                         tops(lrd->def->op), lrd->def);
19579                                 lrd = lrd->next;
19580                         } while(lrd != edge->node->defs);
19581                 }
19582                 warning(state, range->defs->def, "range: ");
19583                 lrd = range->defs;
19584                 do {
19585                         warning(state, lrd->def, " %s %p",
19586                                 tops(lrd->def->op), lrd->def);
19587                         lrd = lrd->next;
19588                 } while(lrd != range->defs);
19589                         
19590                 warning(state, range->defs->def, "classes: %x",
19591                         range->classes);
19592                 for(i = 0; i < MAX_REGISTERS; i++) {
19593                         if (used[i]) {
19594                                 warning(state, range->defs->def, "used: %s",
19595                                         arch_reg_str(i));
19596                         }
19597                 }
19598                 error(state, range->defs->def, "too few registers");
19599         }
19600         range->classes &= arch_reg_regcm(state, range->color);
19601         if ((range->color == REG_UNSET) || (range->classes == 0)) {
19602                 internal_error(state, range->defs->def, "select_free_color did not?");
19603         }
19604         return 1;
19605 }
19606
19607 static int color_graph(struct compile_state *state, struct reg_state *rstate)
19608 {
19609         int colored;
19610         struct live_range_edge *edge;
19611         struct live_range *range;
19612         if (rstate->low) {
19613                 cgdebug_printf(state, "Lo: ");
19614                 range = rstate->low;
19615                 if (*range->group_prev != range) {
19616                         internal_error(state, 0, "lo: *prev != range?");
19617                 }
19618                 *range->group_prev = range->group_next;
19619                 if (range->group_next) {
19620                         range->group_next->group_prev = range->group_prev;
19621                 }
19622                 if (&range->group_next == rstate->low_tail) {
19623                         rstate->low_tail = range->group_prev;
19624                 }
19625                 if (rstate->low == range) {
19626                         internal_error(state, 0, "low: next != prev?");
19627                 }
19628         }
19629         else if (rstate->high) {
19630                 cgdebug_printf(state, "Hi: ");
19631                 range = rstate->high;
19632                 if (*range->group_prev != range) {
19633                         internal_error(state, 0, "hi: *prev != range?");
19634                 }
19635                 *range->group_prev = range->group_next;
19636                 if (range->group_next) {
19637                         range->group_next->group_prev = range->group_prev;
19638                 }
19639                 if (&range->group_next == rstate->high_tail) {
19640                         rstate->high_tail = range->group_prev;
19641                 }
19642                 if (rstate->high == range) {
19643                         internal_error(state, 0, "high: next != prev?");
19644                 }
19645         }
19646         else {
19647                 return 1;
19648         }
19649         cgdebug_printf(state, " %d\n", range - rstate->lr);
19650         range->group_prev = 0;
19651         for(edge = range->edges; edge; edge = edge->next) {
19652                 struct live_range *node;
19653                 node = edge->node;
19654                 /* Move nodes from the high to the low list */
19655                 if (node->group_prev && (node->color == REG_UNSET) &&
19656                         (node->degree == regc_max_size(state, node->classes))) {
19657                         if (*node->group_prev != node) {
19658                                 internal_error(state, 0, "move: *prev != node?");
19659                         }
19660                         *node->group_prev = node->group_next;
19661                         if (node->group_next) {
19662                                 node->group_next->group_prev = node->group_prev;
19663                         }
19664                         if (&node->group_next == rstate->high_tail) {
19665                                 rstate->high_tail = node->group_prev;
19666                         }
19667                         cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
19668                         node->group_prev  = rstate->low_tail;
19669                         node->group_next  = 0;
19670                         *rstate->low_tail = node;
19671                         rstate->low_tail  = &node->group_next;
19672                         if (*node->group_prev != node) {
19673                                 internal_error(state, 0, "move2: *prev != node?");
19674                         }
19675                 }
19676                 node->degree -= 1;
19677         }
19678         colored = color_graph(state, rstate);
19679         if (colored) {
19680                 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
19681                 cgdebug_loc(state, range->defs->def);
19682                 cgdebug_flush(state);
19683                 colored = select_free_color(state, rstate, range);
19684                 if (colored) {
19685                         cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19686                 }
19687         }
19688         return colored;
19689 }
19690
19691 static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19692 {
19693         struct live_range *lr;
19694         struct live_range_edge *edge;
19695         struct triple *ins, *first;
19696         char used[MAX_REGISTERS];
19697         first = state->first;
19698         ins = first;
19699         do {
19700                 if (triple_is_def(state, ins)) {
19701                         if ((ins->id < 0) || (ins->id > rstate->defs)) {
19702                                 internal_error(state, ins, 
19703                                         "triple without a live range def");
19704                         }
19705                         lr = rstate->lrd[ins->id].lr;
19706                         if (lr->color == REG_UNSET) {
19707                                 internal_error(state, ins,
19708                                         "triple without a color");
19709                         }
19710                         /* Find the registers used by the edges */
19711                         memset(used, 0, sizeof(used));
19712                         for(edge = lr->edges; edge; edge = edge->next) {
19713                                 if (edge->node->color == REG_UNSET) {
19714                                         internal_error(state, 0,
19715                                                 "live range without a color");
19716                         }
19717                                 reg_fill_used(state, used, edge->node->color);
19718                         }
19719                         if (used[lr->color]) {
19720                                 internal_error(state, ins,
19721                                         "triple with already used color");
19722                         }
19723                 }
19724                 ins = ins->next;
19725         } while(ins != first);
19726 }
19727
19728 static void color_triples(struct compile_state *state, struct reg_state *rstate)
19729 {
19730         struct live_range_def *lrd;
19731         struct live_range *lr;
19732         struct triple *first, *ins;
19733         first = state->first;
19734         ins = first;
19735         do {
19736                 if ((ins->id < 0) || (ins->id > rstate->defs)) {
19737                         internal_error(state, ins, 
19738                                 "triple without a live range");
19739                 }
19740                 lrd = &rstate->lrd[ins->id];
19741                 lr = lrd->lr;
19742                 ins->id = lrd->orig_id;
19743                 SET_REG(ins->id, lr->color);
19744                 ins = ins->next;
19745         } while (ins != first);
19746 }
19747
19748 static struct live_range *merge_sort_lr(
19749         struct live_range *first, struct live_range *last)
19750 {
19751         struct live_range *mid, *join, **join_tail, *pick;
19752         size_t size;
19753         size = (last - first) + 1;
19754         if (size >= 2) {
19755                 mid = first + size/2;
19756                 first = merge_sort_lr(first, mid -1);
19757                 mid   = merge_sort_lr(mid, last);
19758                 
19759                 join = 0;
19760                 join_tail = &join;
19761                 /* merge the two lists */
19762                 while(first && mid) {
19763                         if ((first->degree < mid->degree) ||
19764                                 ((first->degree == mid->degree) &&
19765                                         (first->length < mid->length))) {
19766                                 pick = first;
19767                                 first = first->group_next;
19768                                 if (first) {
19769                                         first->group_prev = 0;
19770                                 }
19771                         }
19772                         else {
19773                                 pick = mid;
19774                                 mid = mid->group_next;
19775                                 if (mid) {
19776                                         mid->group_prev = 0;
19777                                 }
19778                         }
19779                         pick->group_next = 0;
19780                         pick->group_prev = join_tail;
19781                         *join_tail = pick;
19782                         join_tail = &pick->group_next;
19783                 }
19784                 /* Splice the remaining list */
19785                 pick = (first)? first : mid;
19786                 *join_tail = pick;
19787                 if (pick) { 
19788                         pick->group_prev = join_tail;
19789                 }
19790         }
19791         else {
19792                 if (!first->defs) {
19793                         first = 0;
19794                 }
19795                 join = first;
19796         }
19797         return join;
19798 }
19799
19800 static void ids_from_rstate(struct compile_state *state, 
19801         struct reg_state *rstate)
19802 {
19803         struct triple *ins, *first;
19804         if (!rstate->defs) {
19805                 return;
19806         }
19807         /* Display the graph if desired */
19808         if (state->compiler->debug & DEBUG_INTERFERENCE) {
19809                 FILE *fp = state->dbgout;
19810                 print_interference_blocks(state, rstate, fp, 0);
19811                 print_control_flow(state, fp, &state->bb);
19812                 fflush(fp);
19813         }
19814         first = state->first;
19815         ins = first;
19816         do {
19817                 if (ins->id) {
19818                         struct live_range_def *lrd;
19819                         lrd = &rstate->lrd[ins->id];
19820                         ins->id = lrd->orig_id;
19821                 }
19822                 ins = ins->next;
19823         } while(ins != first);
19824 }
19825
19826 static void cleanup_live_edges(struct reg_state *rstate)
19827 {
19828         int i;
19829         /* Free the edges on each node */
19830         for(i = 1; i <= rstate->ranges; i++) {
19831                 remove_live_edges(rstate, &rstate->lr[i]);
19832         }
19833 }
19834
19835 static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
19836 {
19837         cleanup_live_edges(rstate);
19838         xfree(rstate->lrd);
19839         xfree(rstate->lr);
19840
19841         /* Free the variable lifetime information */
19842         if (rstate->blocks) {
19843                 free_variable_lifetimes(state, &state->bb, rstate->blocks);
19844         }
19845         rstate->defs = 0;
19846         rstate->ranges = 0;
19847         rstate->lrd = 0;
19848         rstate->lr = 0;
19849         rstate->blocks = 0;
19850 }
19851
19852 static void verify_consistency(struct compile_state *state);
19853 static void allocate_registers(struct compile_state *state)
19854 {
19855         struct reg_state rstate;
19856         int colored;
19857
19858         /* Clear out the reg_state */
19859         memset(&rstate, 0, sizeof(rstate));
19860         rstate.max_passes = state->compiler->max_allocation_passes;
19861
19862         do {
19863                 struct live_range **point, **next;
19864                 int conflicts;
19865                 int tangles;
19866                 int coalesced;
19867
19868                 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
19869                         FILE *fp = state->errout;
19870                         fprintf(fp, "pass: %d\n", rstate.passes);
19871                         fflush(fp);
19872                 }
19873
19874                 /* Restore ids */
19875                 ids_from_rstate(state, &rstate);
19876
19877                 /* Cleanup the temporary data structures */
19878                 cleanup_rstate(state, &rstate);
19879
19880                 /* Compute the variable lifetimes */
19881                 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
19882
19883                 /* Fix invalid mandatory live range coalesce conflicts */
19884                 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
19885
19886                 /* Fix two simultaneous uses of the same register.
19887                  * In a few pathlogical cases a partial untangle moves
19888                  * the tangle to a part of the graph we won't revisit.
19889                  * So we keep looping until we have no more tangle fixes
19890                  * to apply.
19891                  */
19892                 do {
19893                         tangles = correct_tangles(state, rstate.blocks);
19894                 } while(tangles);
19895
19896                 
19897                 print_blocks(state, "resolve_tangles", state->dbgout);
19898                 verify_consistency(state);
19899                 
19900                 /* Allocate and initialize the live ranges */
19901                 initialize_live_ranges(state, &rstate);
19902
19903                 /* Note currently doing coalescing in a loop appears to 
19904                  * buys me nothing.  The code is left this way in case
19905                  * there is some value in it.  Or if a future bugfix
19906                  * yields some benefit.
19907                  */
19908                 do {
19909                         if (state->compiler->debug & DEBUG_COALESCING) {
19910                                 fprintf(state->errout, "coalescing\n");
19911                         }
19912
19913                         /* Remove any previous live edge calculations */
19914                         cleanup_live_edges(&rstate);
19915
19916                         /* Compute the interference graph */
19917                         walk_variable_lifetimes(
19918                                 state, &state->bb, rstate.blocks, 
19919                                 graph_ins, &rstate);
19920                         
19921                         /* Display the interference graph if desired */
19922                         if (state->compiler->debug & DEBUG_INTERFERENCE) {
19923                                 print_interference_blocks(state, &rstate, state->dbgout, 1);
19924                                 fprintf(state->dbgout, "\nlive variables by instruction\n");
19925                                 walk_variable_lifetimes(
19926                                         state, &state->bb, rstate.blocks, 
19927                                         print_interference_ins, &rstate);
19928                         }
19929                         
19930                         coalesced = coalesce_live_ranges(state, &rstate);
19931
19932                         if (state->compiler->debug & DEBUG_COALESCING) {
19933                                 fprintf(state->errout, "coalesced: %d\n", coalesced);
19934                         }
19935                 } while(coalesced);
19936
19937 #if DEBUG_CONSISTENCY > 1
19938 # if 0
19939                 fprintf(state->errout, "verify_graph_ins...\n");
19940 # endif
19941                 /* Verify the interference graph */
19942                 walk_variable_lifetimes(
19943                         state, &state->bb, rstate.blocks, 
19944                         verify_graph_ins, &rstate);
19945 # if 0
19946                 fprintf(state->errout, "verify_graph_ins done\n");
19947 #endif
19948 #endif
19949                         
19950                 /* Build the groups low and high.  But with the nodes
19951                  * first sorted by degree order.
19952                  */
19953                 rstate.low_tail  = &rstate.low;
19954                 rstate.high_tail = &rstate.high;
19955                 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
19956                 if (rstate.high) {
19957                         rstate.high->group_prev = &rstate.high;
19958                 }
19959                 for(point = &rstate.high; *point; point = &(*point)->group_next)
19960                         ;
19961                 rstate.high_tail = point;
19962                 /* Walk through the high list and move everything that needs
19963                  * to be onto low.
19964                  */
19965                 for(point = &rstate.high; *point; point = next) {
19966                         struct live_range *range;
19967                         next = &(*point)->group_next;
19968                         range = *point;
19969                         
19970                         /* If it has a low degree or it already has a color
19971                          * place the node in low.
19972                          */
19973                         if ((range->degree < regc_max_size(state, range->classes)) ||
19974                                 (range->color != REG_UNSET)) {
19975                                 cgdebug_printf(state, "Lo: %5d degree %5d%s\n", 
19976                                         range - rstate.lr, range->degree,
19977                                         (range->color != REG_UNSET) ? " (colored)": "");
19978                                 *range->group_prev = range->group_next;
19979                                 if (range->group_next) {
19980                                         range->group_next->group_prev = range->group_prev;
19981                                 }
19982                                 if (&range->group_next == rstate.high_tail) {
19983                                         rstate.high_tail = range->group_prev;
19984                                 }
19985                                 range->group_prev  = rstate.low_tail;
19986                                 range->group_next  = 0;
19987                                 *rstate.low_tail   = range;
19988                                 rstate.low_tail    = &range->group_next;
19989                                 next = point;
19990                         }
19991                         else {
19992                                 cgdebug_printf(state, "hi: %5d degree %5d%s\n", 
19993                                         range - rstate.lr, range->degree,
19994                                         (range->color != REG_UNSET) ? " (colored)": "");
19995                         }
19996                 }
19997                 /* Color the live_ranges */
19998                 colored = color_graph(state, &rstate);
19999                 rstate.passes++;
20000         } while (!colored);
20001
20002         /* Verify the graph was properly colored */
20003         verify_colors(state, &rstate);
20004
20005         /* Move the colors from the graph to the triples */
20006         color_triples(state, &rstate);
20007
20008         /* Cleanup the temporary data structures */
20009         cleanup_rstate(state, &rstate);
20010
20011         /* Display the new graph */
20012         print_blocks(state, __func__, state->dbgout);
20013 }
20014
20015 /* Sparce Conditional Constant Propogation
20016  * =========================================
20017  */
20018 struct ssa_edge;
20019 struct flow_block;
20020 struct lattice_node {
20021         unsigned old_id;
20022         struct triple *def;
20023         struct ssa_edge *out;
20024         struct flow_block *fblock;
20025         struct triple *val;
20026         /* lattice high   val == def
20027          * lattice const  is_const(val)
20028          * lattice low    other
20029          */
20030 };
20031 struct ssa_edge {
20032         struct lattice_node *src;
20033         struct lattice_node *dst;
20034         struct ssa_edge *work_next;
20035         struct ssa_edge *work_prev;
20036         struct ssa_edge *out_next;
20037 };
20038 struct flow_edge {
20039         struct flow_block *src;
20040         struct flow_block *dst;
20041         struct flow_edge *work_next;
20042         struct flow_edge *work_prev;
20043         struct flow_edge *in_next;
20044         struct flow_edge *out_next;
20045         int executable;
20046 };
20047 #define MAX_FLOW_BLOCK_EDGES 3
20048 struct flow_block {
20049         struct block *block;
20050         struct flow_edge *in;
20051         struct flow_edge *out;
20052         struct flow_edge *edges;
20053 };
20054
20055 struct scc_state {
20056         int ins_count;
20057         struct lattice_node *lattice;
20058         struct ssa_edge     *ssa_edges;
20059         struct flow_block   *flow_blocks;
20060         struct flow_edge    *flow_work_list;
20061         struct ssa_edge     *ssa_work_list;
20062 };
20063
20064
20065 static int is_scc_const(struct compile_state *state, struct triple *ins)
20066 {
20067         return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20068 }
20069
20070 static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20071 {
20072         return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20073 }
20074
20075 static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20076 {
20077         return is_scc_const(state, lnode->val);
20078 }
20079
20080 static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20081 {
20082         return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20083 }
20084
20085 static void scc_add_fedge(struct compile_state *state, struct scc_state *scc, 
20086         struct flow_edge *fedge)
20087 {
20088         if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20089                 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
20090                         fedge,
20091                         fedge->src->block?fedge->src->block->last->id: 0,
20092                         fedge->dst->block?fedge->dst->block->first->id: 0);
20093         }
20094         if ((fedge == scc->flow_work_list) ||
20095                 (fedge->work_next != fedge) ||
20096                 (fedge->work_prev != fedge)) {
20097
20098                 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20099                         fprintf(state->errout, "dupped fedge: %p\n",
20100                                 fedge);
20101                 }
20102                 return;
20103         }
20104         if (!scc->flow_work_list) {
20105                 scc->flow_work_list = fedge;
20106                 fedge->work_next = fedge->work_prev = fedge;
20107         }
20108         else {
20109                 struct flow_edge *ftail;
20110                 ftail = scc->flow_work_list->work_prev;
20111                 fedge->work_next = ftail->work_next;
20112                 fedge->work_prev = ftail;
20113                 fedge->work_next->work_prev = fedge;
20114                 fedge->work_prev->work_next = fedge;
20115         }
20116 }
20117
20118 static struct flow_edge *scc_next_fedge(
20119         struct compile_state *state, struct scc_state *scc)
20120 {
20121         struct flow_edge *fedge;
20122         fedge = scc->flow_work_list;
20123         if (fedge) {
20124                 fedge->work_next->work_prev = fedge->work_prev;
20125                 fedge->work_prev->work_next = fedge->work_next;
20126                 if (fedge->work_next != fedge) {
20127                         scc->flow_work_list = fedge->work_next;
20128                 } else {
20129                         scc->flow_work_list = 0;
20130                 }
20131                 fedge->work_next = fedge->work_prev = fedge;
20132         }
20133         return fedge;
20134 }
20135
20136 static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20137         struct ssa_edge *sedge)
20138 {
20139         if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20140                 fprintf(state->errout, "adding sedge: %5d (%4d -> %5d)\n",
20141                         sedge - scc->ssa_edges,
20142                         sedge->src->def->id,
20143                         sedge->dst->def->id);
20144         }
20145         if ((sedge == scc->ssa_work_list) ||
20146                 (sedge->work_next != sedge) ||
20147                 (sedge->work_prev != sedge)) {
20148
20149                 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
20150                         fprintf(state->errout, "dupped sedge: %5d\n",
20151                                 sedge - scc->ssa_edges);
20152                 }
20153                 return;
20154         }
20155         if (!scc->ssa_work_list) {
20156                 scc->ssa_work_list = sedge;
20157                 sedge->work_next = sedge->work_prev = sedge;
20158         }
20159         else {
20160                 struct ssa_edge *stail;
20161                 stail = scc->ssa_work_list->work_prev;
20162                 sedge->work_next = stail->work_next;
20163                 sedge->work_prev = stail;
20164                 sedge->work_next->work_prev = sedge;
20165                 sedge->work_prev->work_next = sedge;
20166         }
20167 }
20168
20169 static struct ssa_edge *scc_next_sedge(
20170         struct compile_state *state, struct scc_state *scc)
20171 {
20172         struct ssa_edge *sedge;
20173         sedge = scc->ssa_work_list;
20174         if (sedge) {
20175                 sedge->work_next->work_prev = sedge->work_prev;
20176                 sedge->work_prev->work_next = sedge->work_next;
20177                 if (sedge->work_next != sedge) {
20178                         scc->ssa_work_list = sedge->work_next;
20179                 } else {
20180                         scc->ssa_work_list = 0;
20181                 }
20182                 sedge->work_next = sedge->work_prev = sedge;
20183         }
20184         return sedge;
20185 }
20186
20187 static void initialize_scc_state(
20188         struct compile_state *state, struct scc_state *scc)
20189 {
20190         int ins_count, ssa_edge_count;
20191         int ins_index, ssa_edge_index, fblock_index;
20192         struct triple *first, *ins;
20193         struct block *block;
20194         struct flow_block *fblock;
20195
20196         memset(scc, 0, sizeof(*scc));
20197
20198         /* Inialize pass zero find out how much memory we need */
20199         first = state->first;
20200         ins = first;
20201         ins_count = ssa_edge_count = 0;
20202         do {
20203                 struct triple_set *edge;
20204                 ins_count += 1;
20205                 for(edge = ins->use; edge; edge = edge->next) {
20206                         ssa_edge_count++;
20207                 }
20208                 ins = ins->next;
20209         } while(ins != first);
20210         if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20211                 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20212                         ins_count, ssa_edge_count, state->bb.last_vertex);
20213         }
20214         scc->ins_count   = ins_count;
20215         scc->lattice     = 
20216                 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20217         scc->ssa_edges   = 
20218                 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20219         scc->flow_blocks = 
20220                 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1), 
20221                         "flow_blocks");
20222
20223         /* Initialize pass one collect up the nodes */
20224         fblock = 0;
20225         block = 0;
20226         ins_index = ssa_edge_index = fblock_index = 0;
20227         ins = first;
20228         do {
20229                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20230                         block = ins->u.block;
20231                         if (!block) {
20232                                 internal_error(state, ins, "label without block");
20233                         }
20234                         fblock_index += 1;
20235                         block->vertex = fblock_index;
20236                         fblock = &scc->flow_blocks[fblock_index];
20237                         fblock->block = block;
20238                         fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20239                                 "flow_edges");
20240                 }
20241                 {
20242                         struct lattice_node *lnode;
20243                         ins_index += 1;
20244                         lnode = &scc->lattice[ins_index];
20245                         lnode->def = ins;
20246                         lnode->out = 0;
20247                         lnode->fblock = fblock;
20248                         lnode->val = ins; /* LATTICE HIGH */
20249                         if (lnode->val->op == OP_UNKNOWNVAL) {
20250                                 lnode->val = 0; /* LATTICE LOW by definition */
20251                         }
20252                         lnode->old_id = ins->id;
20253                         ins->id = ins_index;
20254                 }
20255                 ins = ins->next;
20256         } while(ins != first);
20257         /* Initialize pass two collect up the edges */
20258         block = 0;
20259         fblock = 0;
20260         ins = first;
20261         do {
20262                 {
20263                         struct triple_set *edge;
20264                         struct ssa_edge **stail;
20265                         struct lattice_node *lnode;
20266                         lnode = &scc->lattice[ins->id];
20267                         lnode->out = 0;
20268                         stail = &lnode->out;
20269                         for(edge = ins->use; edge; edge = edge->next) {
20270                                 struct ssa_edge *sedge;
20271                                 ssa_edge_index += 1;
20272                                 sedge = &scc->ssa_edges[ssa_edge_index];
20273                                 *stail = sedge;
20274                                 stail = &sedge->out_next;
20275                                 sedge->src = lnode;
20276                                 sedge->dst = &scc->lattice[edge->member->id];
20277                                 sedge->work_next = sedge->work_prev = sedge;
20278                                 sedge->out_next = 0;
20279                         }
20280                 }
20281                 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20282                         struct flow_edge *fedge, **ftail;
20283                         struct block_set *bedge;
20284                         block = ins->u.block;
20285                         fblock = &scc->flow_blocks[block->vertex];
20286                         fblock->in = 0;
20287                         fblock->out = 0;
20288                         ftail = &fblock->out;
20289
20290                         fedge = fblock->edges;
20291                         bedge = block->edges;
20292                         for(; bedge; bedge = bedge->next, fedge++) {
20293                                 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20294                                 if (fedge->dst->block != bedge->member) {
20295                                         internal_error(state, 0, "block mismatch");
20296                                 }
20297                                 *ftail = fedge;
20298                                 ftail = &fedge->out_next;
20299                                 fedge->out_next = 0;
20300                         }
20301                         for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20302                                 fedge->src = fblock;
20303                                 fedge->work_next = fedge->work_prev = fedge;
20304                                 fedge->executable = 0;
20305                         }
20306                 }
20307                 ins = ins->next;
20308         } while (ins != first);
20309         block = 0;
20310         fblock = 0;
20311         ins = first;
20312         do {
20313                 if ((ins->op  == OP_LABEL) && (block != ins->u.block)) {
20314                         struct flow_edge **ftail;
20315                         struct block_set *bedge;
20316                         block = ins->u.block;
20317                         fblock = &scc->flow_blocks[block->vertex];
20318                         ftail = &fblock->in;
20319                         for(bedge = block->use; bedge; bedge = bedge->next) {
20320                                 struct block *src_block;
20321                                 struct flow_block *sfblock;
20322                                 struct flow_edge *sfedge;
20323                                 src_block = bedge->member;
20324                                 sfblock = &scc->flow_blocks[src_block->vertex];
20325                                 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20326                                         if (sfedge->dst == fblock) {
20327                                                 break;
20328                                         }
20329                                 }
20330                                 if (!sfedge) {
20331                                         internal_error(state, 0, "edge mismatch");
20332                                 }
20333                                 *ftail = sfedge;
20334                                 ftail = &sfedge->in_next;
20335                                 sfedge->in_next = 0;
20336                         }
20337                 }
20338                 ins = ins->next;
20339         } while(ins != first);
20340         /* Setup a dummy block 0 as a node above the start node */
20341         {
20342                 struct flow_block *fblock, *dst;
20343                 struct flow_edge *fedge;
20344                 fblock = &scc->flow_blocks[0];
20345                 fblock->block = 0;
20346                 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
20347                 fblock->in = 0;
20348                 fblock->out = fblock->edges;
20349                 dst = &scc->flow_blocks[state->bb.first_block->vertex];
20350                 fedge = fblock->edges;
20351                 fedge->src        = fblock;
20352                 fedge->dst        = dst;
20353                 fedge->work_next  = fedge;
20354                 fedge->work_prev  = fedge;
20355                 fedge->in_next    = fedge->dst->in;
20356                 fedge->out_next   = 0;
20357                 fedge->executable = 0;
20358                 fedge->dst->in = fedge;
20359                 
20360                 /* Initialize the work lists */
20361                 scc->flow_work_list = 0;
20362                 scc->ssa_work_list  = 0;
20363                 scc_add_fedge(state, scc, fedge);
20364         }
20365         if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20366                 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
20367                         ins_index, ssa_edge_index, fblock_index);
20368         }
20369 }
20370
20371         
20372 static void free_scc_state(
20373         struct compile_state *state, struct scc_state *scc)
20374 {
20375         int i;
20376         for(i = 0; i < state->bb.last_vertex + 1; i++) {
20377                 struct flow_block *fblock;
20378                 fblock = &scc->flow_blocks[i];
20379                 if (fblock->edges) {
20380                         xfree(fblock->edges);
20381                         fblock->edges = 0;
20382                 }
20383         }
20384         xfree(scc->flow_blocks);
20385         xfree(scc->ssa_edges);
20386         xfree(scc->lattice);
20387         
20388 }
20389
20390 static struct lattice_node *triple_to_lattice(
20391         struct compile_state *state, struct scc_state *scc, struct triple *ins)
20392 {
20393         if (ins->id <= 0) {
20394                 internal_error(state, ins, "bad id");
20395         }
20396         return &scc->lattice[ins->id];
20397 }
20398
20399 static struct triple *preserve_lval(
20400         struct compile_state *state, struct lattice_node *lnode)
20401 {
20402         struct triple *old;
20403         /* Preserve the original value */
20404         if (lnode->val) {
20405                 old = dup_triple(state, lnode->val);
20406                 if (lnode->val != lnode->def) {
20407                         xfree(lnode->val);
20408                 }
20409                 lnode->val = 0;
20410         } else {
20411                 old = 0;
20412         }
20413         return old;
20414 }
20415
20416 static int lval_changed(struct compile_state *state, 
20417         struct triple *old, struct lattice_node *lnode)
20418 {
20419         int changed;
20420         /* See if the lattice value has changed */
20421         changed = 1;
20422         if (!old && !lnode->val) {
20423                 changed = 0;
20424         }
20425         if (changed &&
20426                 lnode->val && old &&
20427                 (memcmp(lnode->val->param, old->param,
20428                         TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
20429                 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20430                 changed = 0;
20431         }
20432         if (old) {
20433                 xfree(old);
20434         }
20435         return changed;
20436
20437 }
20438
20439 static void scc_debug_lnode(
20440         struct compile_state *state, struct scc_state *scc,
20441         struct lattice_node *lnode, int changed)
20442 {
20443         if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20444                 display_triple_changes(state->errout, lnode->val, lnode->def);
20445         }
20446         if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20447                 FILE *fp = state->errout;
20448                 struct triple *val, **expr;
20449                 val = lnode->val? lnode->val : lnode->def;
20450                 fprintf(fp, "%p %s %3d %10s (",
20451                         lnode->def, 
20452                         ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20453                         lnode->def->id,
20454                         tops(lnode->def->op));
20455                 expr = triple_rhs(state, lnode->def, 0);
20456                 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20457                         if (*expr) {
20458                                 fprintf(fp, " %d", (*expr)->id);
20459                         }
20460                 }
20461                 if (val->op == OP_INTCONST) {
20462                         fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20463                 }
20464                 fprintf(fp, " ) -> %s %s\n",
20465                         (is_lattice_hi(state, lnode)? "hi":
20466                                 is_lattice_const(state, lnode)? "const" : "lo"),
20467                         changed? "changed" : ""
20468                         );
20469         }
20470 }
20471
20472 static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20473         struct lattice_node *lnode)
20474 {
20475         int changed;
20476         struct triple *old, *scratch;
20477         struct triple **dexpr, **vexpr;
20478         int count, i;
20479         
20480         /* Store the original value */
20481         old = preserve_lval(state, lnode);
20482
20483         /* Reinitialize the value */
20484         lnode->val = scratch = dup_triple(state, lnode->def);
20485         scratch->id = lnode->old_id;
20486         scratch->next     = scratch;
20487         scratch->prev     = scratch;
20488         scratch->use      = 0;
20489
20490         count = TRIPLE_SIZE(scratch);
20491         for(i = 0; i < count; i++) {
20492                 dexpr = &lnode->def->param[i];
20493                 vexpr = &scratch->param[i];
20494                 *vexpr = *dexpr;
20495                 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20496                         (i >= TRIPLE_TARG_OFF(scratch))) &&
20497                         *dexpr) {
20498                         struct lattice_node *tmp;
20499                         tmp = triple_to_lattice(state, scc, *dexpr);
20500                         *vexpr = (tmp->val)? tmp->val : tmp->def;
20501                 }
20502         }
20503         if (triple_is_branch(state, scratch)) {
20504                 scratch->next = lnode->def->next;
20505         }
20506         /* Recompute the value */
20507 #warning "FIXME see if simplify does anything bad"
20508         /* So far it looks like only the strength reduction
20509          * optimization are things I need to worry about.
20510          */
20511         simplify(state, scratch);
20512         /* Cleanup my value */
20513         if (scratch->use) {
20514                 internal_error(state, lnode->def, "scratch used?");
20515         }
20516         if ((scratch->prev != scratch) ||
20517                 ((scratch->next != scratch) &&
20518                         (!triple_is_branch(state, lnode->def) ||
20519                                 (scratch->next != lnode->def->next)))) {
20520                 internal_error(state, lnode->def, "scratch in list?");
20521         }
20522         /* undo any uses... */
20523         count = TRIPLE_SIZE(scratch);
20524         for(i = 0; i < count; i++) {
20525                 vexpr = &scratch->param[i];
20526                 if (*vexpr) {
20527                         unuse_triple(*vexpr, scratch);
20528                 }
20529         }
20530         if (lnode->val->op == OP_UNKNOWNVAL) {
20531                 lnode->val = 0; /* Lattice low by definition */
20532         }
20533         /* Find the case when I am lattice high */
20534         if (lnode->val && 
20535                 (lnode->val->op == lnode->def->op) &&
20536                 (memcmp(lnode->val->param, lnode->def->param, 
20537                         count * sizeof(lnode->val->param[0])) == 0) &&
20538                 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
20539                 lnode->val = lnode->def;
20540         }
20541         /* Only allow lattice high when all of my inputs
20542          * are also lattice high.  Occassionally I can
20543          * have constants with a lattice low input, so
20544          * I do not need to check that case.
20545          */
20546         if (is_lattice_hi(state, lnode)) {
20547                 struct lattice_node *tmp;
20548                 int rhs;
20549                 rhs = lnode->val->rhs;
20550                 for(i = 0; i < rhs; i++) {
20551                         tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20552                         if (!is_lattice_hi(state, tmp)) {
20553                                 lnode->val = 0;
20554                                 break;
20555                         }
20556                 }
20557         }
20558         /* Find the cases that are always lattice lo */
20559         if (lnode->val && 
20560                 triple_is_def(state, lnode->val) &&
20561                 !triple_is_pure(state, lnode->val, lnode->old_id)) {
20562                 lnode->val = 0;
20563         }
20564         /* See if the lattice value has changed */
20565         changed = lval_changed(state, old, lnode);
20566         /* See if this value should not change */
20567         if ((lnode->val != lnode->def) && 
20568                 ((      !triple_is_def(state, lnode->def)  &&
20569                         !triple_is_cbranch(state, lnode->def)) ||
20570                         (lnode->def->op == OP_PIECE))) {
20571 #warning "FIXME constant propogate through expressions with multiple left hand sides"
20572                 if (changed) {
20573                         internal_warning(state, lnode->def, "non def changes value?");
20574                 }
20575                 lnode->val = 0;
20576         }
20577
20578         /* See if we need to free the scratch value */
20579         if (lnode->val != scratch) {
20580                 xfree(scratch);
20581         }
20582         
20583         return changed;
20584 }
20585
20586
20587 static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
20588         struct lattice_node *lnode)
20589 {
20590         struct lattice_node *cond;
20591         struct flow_edge *left, *right;
20592         int changed;
20593
20594         /* Update the branch value */
20595         changed = compute_lnode_val(state, scc, lnode);
20596         scc_debug_lnode(state, scc, lnode, changed);
20597
20598         /* This only applies to conditional branches */
20599         if (!triple_is_cbranch(state, lnode->def)) {
20600                 internal_error(state, lnode->def, "not a conditional branch");
20601         }
20602
20603         if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20604                 struct flow_edge *fedge;
20605                 FILE *fp = state->errout;
20606                 fprintf(fp, "%s: %d (",
20607                         tops(lnode->def->op),
20608                         lnode->def->id);
20609                 
20610                 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
20611                         fprintf(fp, " %d", fedge->dst->block->vertex);
20612                 }
20613                 fprintf(fp, " )");
20614                 if (lnode->def->rhs > 0) {
20615                         fprintf(fp, " <- %d",
20616                                 RHS(lnode->def, 0)->id);
20617                 }
20618                 fprintf(fp, "\n");
20619         }
20620         cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
20621         for(left = cond->fblock->out; left; left = left->out_next) {
20622                 if (left->dst->block->first == lnode->def->next) {
20623                         break;
20624                 }
20625         }
20626         if (!left) {
20627                 internal_error(state, lnode->def, "Cannot find left branch edge");
20628         }
20629         for(right = cond->fblock->out; right; right = right->out_next) {
20630                 if (right->dst->block->first == TARG(lnode->def, 0)) {
20631                         break;
20632                 }
20633         }
20634         if (!right) {
20635                 internal_error(state, lnode->def, "Cannot find right branch edge");
20636         }
20637         /* I should only come here if the controlling expressions value
20638          * has changed, which means it must be either a constant or lo.
20639          */
20640         if (is_lattice_hi(state, cond)) {
20641                 internal_error(state, cond->def, "condition high?");
20642                 return;
20643         }
20644         if (is_lattice_lo(state, cond)) {
20645                 scc_add_fedge(state, scc, left);
20646                 scc_add_fedge(state, scc, right);
20647         }
20648         else if (cond->val->u.cval) {
20649                 scc_add_fedge(state, scc, right);
20650         } else {
20651                 scc_add_fedge(state, scc, left);
20652         }
20653
20654 }
20655
20656
20657 static void scc_add_sedge_dst(struct compile_state *state, 
20658         struct scc_state *scc, struct ssa_edge *sedge)
20659 {
20660         if (triple_is_cbranch(state, sedge->dst->def)) {
20661                 scc_visit_cbranch(state, scc, sedge->dst);
20662         }
20663         else if (triple_is_def(state, sedge->dst->def)) {
20664                 scc_add_sedge(state, scc, sedge);
20665         }
20666 }
20667
20668 static void scc_visit_phi(struct compile_state *state, struct scc_state *scc, 
20669         struct lattice_node *lnode)
20670 {
20671         struct lattice_node *tmp;
20672         struct triple **slot, *old;
20673         struct flow_edge *fedge;
20674         int changed;
20675         int index;
20676         if (lnode->def->op != OP_PHI) {
20677                 internal_error(state, lnode->def, "not phi");
20678         }
20679         /* Store the original value */
20680         old = preserve_lval(state, lnode);
20681
20682         /* default to lattice high */
20683         lnode->val = lnode->def;
20684         slot = &RHS(lnode->def, 0);
20685         index = 0;
20686         for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20687                 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20688                         fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n", 
20689                                 index,
20690                                 fedge->dst->block->vertex,
20691                                 fedge->executable
20692                                 );
20693                 }
20694                 if (!fedge->executable) {
20695                         continue;
20696                 }
20697                 if (!slot[index]) {
20698                         internal_error(state, lnode->def, "no phi value");
20699                 }
20700                 tmp = triple_to_lattice(state, scc, slot[index]);
20701                 /* meet(X, lattice low) = lattice low */
20702                 if (is_lattice_lo(state, tmp)) {
20703                         lnode->val = 0;
20704                 }
20705                 /* meet(X, lattice high) = X */
20706                 else if (is_lattice_hi(state, tmp)) {
20707                         lnode->val = lnode->val;
20708                 }
20709                 /* meet(lattice high, X) = X */
20710                 else if (is_lattice_hi(state, lnode)) {
20711                         lnode->val = dup_triple(state, tmp->val);
20712                         /* Only change the type if necessary */
20713                         if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20714                                 lnode->val->type = lnode->def->type;
20715                         }
20716                 }
20717                 /* meet(const, const) = const or lattice low */
20718                 else if (!constants_equal(state, lnode->val, tmp->val)) {
20719                         lnode->val = 0;
20720                 }
20721
20722                 /* meet(lattice low, X) = lattice low */
20723                 if (is_lattice_lo(state, lnode)) {
20724                         lnode->val = 0;
20725                         break;
20726                 }
20727         }
20728         changed = lval_changed(state, old, lnode);
20729         scc_debug_lnode(state, scc, lnode, changed);
20730
20731         /* If the lattice value has changed update the work lists. */
20732         if (changed) {
20733                 struct ssa_edge *sedge;
20734                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20735                         scc_add_sedge_dst(state, scc, sedge);
20736                 }
20737         }
20738 }
20739
20740
20741 static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20742         struct lattice_node *lnode)
20743 {
20744         int changed;
20745
20746         if (!triple_is_def(state, lnode->def)) {
20747                 internal_warning(state, lnode->def, "not visiting an expression?");
20748         }
20749         changed = compute_lnode_val(state, scc, lnode);
20750         scc_debug_lnode(state, scc, lnode, changed);
20751
20752         if (changed) {
20753                 struct ssa_edge *sedge;
20754                 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20755                         scc_add_sedge_dst(state, scc, sedge);
20756                 }
20757         }
20758 }
20759
20760 static void scc_writeback_values(
20761         struct compile_state *state, struct scc_state *scc)
20762 {
20763         struct triple *first, *ins;
20764         first = state->first;
20765         ins = first;
20766         do {
20767                 struct lattice_node *lnode;
20768                 lnode = triple_to_lattice(state, scc, ins);
20769                 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20770                         if (is_lattice_hi(state, lnode) &&
20771                                 (lnode->val->op != OP_NOOP))
20772                         {
20773                                 struct flow_edge *fedge;
20774                                 int executable;
20775                                 executable = 0;
20776                                 for(fedge = lnode->fblock->in; 
20777                                     !executable && fedge; fedge = fedge->in_next) {
20778                                         executable |= fedge->executable;
20779                                 }
20780                                 if (executable) {
20781                                         internal_warning(state, lnode->def,
20782                                                 "lattice node %d %s->%s still high?",
20783                                                 ins->id, 
20784                                                 tops(lnode->def->op),
20785                                                 tops(lnode->val->op));
20786                                 }
20787                         }
20788                 }
20789
20790                 /* Restore id */
20791                 ins->id = lnode->old_id;
20792                 if (lnode->val && (lnode->val != ins)) {
20793                         /* See if it something I know how to write back */
20794                         switch(lnode->val->op) {
20795                         case OP_INTCONST:
20796                                 mkconst(state, ins, lnode->val->u.cval);
20797                                 break;
20798                         case OP_ADDRCONST:
20799                                 mkaddr_const(state, ins, 
20800                                         MISC(lnode->val, 0), lnode->val->u.cval);
20801                                 break;
20802                         default:
20803                                 /* By default don't copy the changes,
20804                                  * recompute them in place instead.
20805                                  */
20806                                 simplify(state, ins);
20807                                 break;
20808                         }
20809                         if (is_const(lnode->val) &&
20810                                 !constants_equal(state, lnode->val, ins)) {
20811                                 internal_error(state, 0, "constants not equal");
20812                         }
20813                         /* Free the lattice nodes */
20814                         xfree(lnode->val);
20815                         lnode->val = 0;
20816                 }
20817                 ins = ins->next;
20818         } while(ins != first);
20819 }
20820
20821 static void scc_transform(struct compile_state *state)
20822 {
20823         struct scc_state scc;
20824         if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
20825                 return;
20826         }
20827
20828         initialize_scc_state(state, &scc);
20829
20830         while(scc.flow_work_list || scc.ssa_work_list) {
20831                 struct flow_edge *fedge;
20832                 struct ssa_edge *sedge;
20833                 struct flow_edge *fptr;
20834                 while((fedge = scc_next_fedge(state, &scc))) {
20835                         struct block *block;
20836                         struct triple *ptr;
20837                         struct flow_block *fblock;
20838                         int reps;
20839                         int done;
20840                         if (fedge->executable) {
20841                                 continue;
20842                         }
20843                         if (!fedge->dst) {
20844                                 internal_error(state, 0, "fedge without dst");
20845                         }
20846                         if (!fedge->src) {
20847                                 internal_error(state, 0, "fedge without src");
20848                         }
20849                         fedge->executable = 1;
20850                         fblock = fedge->dst;
20851                         block = fblock->block;
20852                         reps = 0;
20853                         for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
20854                                 if (fptr->executable) {
20855                                         reps++;
20856                                 }
20857                         }
20858                         
20859                         if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20860                                 fprintf(state->errout, "vertex: %d reps: %d\n", 
20861                                         block->vertex, reps);
20862                         }
20863
20864                         done = 0;
20865                         for(ptr = block->first; !done; ptr = ptr->next) {
20866                                 struct lattice_node *lnode;
20867                                 done = (ptr == block->last);
20868                                 lnode = &scc.lattice[ptr->id];
20869                                 if (ptr->op == OP_PHI) {
20870                                         scc_visit_phi(state, &scc, lnode);
20871                                 }
20872                                 else if ((reps == 1) && triple_is_def(state, ptr))
20873                                 {
20874                                         scc_visit_expr(state, &scc, lnode);
20875                                 }
20876                         }
20877                         /* Add unconditional branch edges */
20878                         if (!triple_is_cbranch(state, fblock->block->last)) {
20879                                 struct flow_edge *out;
20880                                 for(out = fblock->out; out; out = out->out_next) {
20881                                         scc_add_fedge(state, &scc, out);
20882                                 }
20883                         }
20884                 }
20885                 while((sedge = scc_next_sedge(state, &scc))) {
20886                         struct lattice_node *lnode;
20887                         struct flow_block *fblock;
20888                         lnode = sedge->dst;
20889                         fblock = lnode->fblock;
20890
20891                         if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20892                                 fprintf(state->errout, "sedge: %5d (%5d -> %5d)\n",
20893                                         sedge - scc.ssa_edges,
20894                                         sedge->src->def->id,
20895                                         sedge->dst->def->id);
20896                         }
20897
20898                         if (lnode->def->op == OP_PHI) {
20899                                 scc_visit_phi(state, &scc, lnode);
20900                         }
20901                         else {
20902                                 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
20903                                         if (fptr->executable) {
20904                                                 break;
20905                                         }
20906                                 }
20907                                 if (fptr) {
20908                                         scc_visit_expr(state, &scc, lnode);
20909                                 }
20910                         }
20911                 }
20912         }
20913         
20914         scc_writeback_values(state, &scc);
20915         free_scc_state(state, &scc);
20916         rebuild_ssa_form(state);
20917         
20918         print_blocks(state, __func__, state->dbgout);
20919 }
20920
20921
20922 static void transform_to_arch_instructions(struct compile_state *state)
20923 {
20924         struct triple *ins, *first;
20925         first = state->first;
20926         ins = first;
20927         do {
20928                 ins = transform_to_arch_instruction(state, ins);
20929         } while(ins != first);
20930         
20931         print_blocks(state, __func__, state->dbgout);
20932 }
20933
20934 #if DEBUG_CONSISTENCY
20935 static void verify_uses(struct compile_state *state)
20936 {
20937         struct triple *first, *ins;
20938         struct triple_set *set;
20939         first = state->first;
20940         ins = first;
20941         do {
20942                 struct triple **expr;
20943                 expr = triple_rhs(state, ins, 0);
20944                 for(; expr; expr = triple_rhs(state, ins, expr)) {
20945                         struct triple *rhs;
20946                         rhs = *expr;
20947                         for(set = rhs?rhs->use:0; set; set = set->next) {
20948                                 if (set->member == ins) {
20949                                         break;
20950                                 }
20951                         }
20952                         if (!set) {
20953                                 internal_error(state, ins, "rhs not used");
20954                         }
20955                 }
20956                 expr = triple_lhs(state, ins, 0);
20957                 for(; expr; expr = triple_lhs(state, ins, expr)) {
20958                         struct triple *lhs;
20959                         lhs = *expr;
20960                         for(set =  lhs?lhs->use:0; set; set = set->next) {
20961                                 if (set->member == ins) {
20962                                         break;
20963                                 }
20964                         }
20965                         if (!set) {
20966                                 internal_error(state, ins, "lhs not used");
20967                         }
20968                 }
20969                 expr = triple_misc(state, ins, 0);
20970                 if (ins->op != OP_PHI) {
20971                         for(; expr; expr = triple_targ(state, ins, expr)) {
20972                                 struct triple *misc;
20973                                 misc = *expr;
20974                                 for(set = misc?misc->use:0; set; set = set->next) {
20975                                         if (set->member == ins) {
20976                                                 break;
20977                                         }
20978                                 }
20979                                 if (!set) {
20980                                         internal_error(state, ins, "misc not used");
20981                                 }
20982                         }
20983                 }
20984                 if (!triple_is_ret(state, ins)) {
20985                         expr = triple_targ(state, ins, 0);
20986                         for(; expr; expr = triple_targ(state, ins, expr)) {
20987                                 struct triple *targ;
20988                                 targ = *expr;
20989                                 for(set = targ?targ->use:0; set; set = set->next) {
20990                                         if (set->member == ins) {
20991                                                 break;
20992                                         }
20993                                 }
20994                                 if (!set) {
20995                                         internal_error(state, ins, "targ not used");
20996                                 }
20997                         }
20998                 }
20999                 ins = ins->next;
21000         } while(ins != first);
21001         
21002 }
21003 static void verify_blocks_present(struct compile_state *state)
21004 {
21005         struct triple *first, *ins;
21006         if (!state->bb.first_block) {
21007                 return;
21008         }
21009         first = state->first;
21010         ins = first;
21011         do {
21012                 valid_ins(state, ins);
21013                 if (triple_stores_block(state, ins)) {
21014                         if (!ins->u.block) {
21015                                 internal_error(state, ins, 
21016                                         "%p not in a block?", ins);
21017                         }
21018                 }
21019                 ins = ins->next;
21020         } while(ins != first);
21021         
21022         
21023 }
21024
21025 static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21026 {
21027         struct block_set *bedge;
21028         struct block *targ;
21029         targ = block_of_triple(state, edge);
21030         for(bedge = block->edges; bedge; bedge = bedge->next) {
21031                 if (bedge->member == targ) {
21032                         return 1;
21033                 }
21034         }
21035         return 0;
21036 }
21037
21038 static void verify_blocks(struct compile_state *state)
21039 {
21040         struct triple *ins;
21041         struct block *block;
21042         int blocks;
21043         block = state->bb.first_block;
21044         if (!block) {
21045                 return;
21046         }
21047         blocks = 0;
21048         do {
21049                 int users;
21050                 struct block_set *user, *edge;
21051                 blocks++;
21052                 for(ins = block->first; ins != block->last->next; ins = ins->next) {
21053                         if (triple_stores_block(state, ins) && (ins->u.block != block)) {
21054                                 internal_error(state, ins, "inconsitent block specified");
21055                         }
21056                         valid_ins(state, ins);
21057                 }
21058                 users = 0;
21059                 for(user = block->use; user; user = user->next) {
21060                         users++;
21061                         if (!user->member->first) {
21062                                 internal_error(state, block->first, "user is empty");
21063                         }
21064                         if ((block == state->bb.last_block) &&
21065                                 (user->member == state->bb.first_block)) {
21066                                 continue;
21067                         }
21068                         for(edge = user->member->edges; edge; edge = edge->next) {
21069                                 if (edge->member == block) {
21070                                         break;
21071                                 }
21072                         }
21073                         if (!edge) {
21074                                 internal_error(state, user->member->first,
21075                                         "user does not use block");
21076                         }
21077                 }
21078                 if (triple_is_branch(state, block->last)) {
21079                         struct triple **expr;
21080                         expr = triple_edge_targ(state, block->last, 0);
21081                         for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
21082                                 if (*expr && !edge_present(state, block, *expr)) {
21083                                         internal_error(state, block->last, "no edge to targ");
21084                                 }
21085                         }
21086                 }
21087                 if (!triple_is_ubranch(state, block->last) &&
21088                         (block != state->bb.last_block) &&
21089                         !edge_present(state, block, block->last->next)) {
21090                         internal_error(state, block->last, "no edge to block->last->next");
21091                 }
21092                 for(edge = block->edges; edge; edge = edge->next) {
21093                         for(user = edge->member->use; user; user = user->next) {
21094                                 if (user->member == block) {
21095                                         break;
21096                                 }
21097                         }
21098                         if (!user || user->member != block) {
21099                                 internal_error(state, block->first,
21100                                         "block does not use edge");
21101                         }
21102                         if (!edge->member->first) {
21103                                 internal_error(state, block->first, "edge block is empty");
21104                         }
21105                 }
21106                 if (block->users != users) {
21107                         internal_error(state, block->first, 
21108                                 "computed users %d != stored users %d",
21109                                 users, block->users);
21110                 }
21111                 if (!triple_stores_block(state, block->last->next)) {
21112                         internal_error(state, block->last->next, 
21113                                 "cannot find next block");
21114                 }
21115                 block = block->last->next->u.block;
21116                 if (!block) {
21117                         internal_error(state, block->last->next,
21118                                 "bad next block");
21119                 }
21120         } while(block != state->bb.first_block);
21121         if (blocks != state->bb.last_vertex) {
21122                 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21123                         blocks, state->bb.last_vertex);
21124         }
21125 }
21126
21127 static void verify_domination(struct compile_state *state)
21128 {
21129         struct triple *first, *ins;
21130         struct triple_set *set;
21131         if (!state->bb.first_block) {
21132                 return;
21133         }
21134         
21135         first = state->first;
21136         ins = first;
21137         do {
21138                 for(set = ins->use; set; set = set->next) {
21139                         struct triple **slot;
21140                         struct triple *use_point;
21141                         int i, zrhs;
21142                         use_point = 0;
21143                         zrhs = set->member->rhs;
21144                         slot = &RHS(set->member, 0);
21145                         /* See if the use is on the right hand side */
21146                         for(i = 0; i < zrhs; i++) {
21147                                 if (slot[i] == ins) {
21148                                         break;
21149                                 }
21150                         }
21151                         if (i < zrhs) {
21152                                 use_point = set->member;
21153                                 if (set->member->op == OP_PHI) {
21154                                         struct block_set *bset;
21155                                         int edge;
21156                                         bset = set->member->u.block->use;
21157                                         for(edge = 0; bset && (edge < i); edge++) {
21158                                                 bset = bset->next;
21159                                         }
21160                                         if (!bset) {
21161                                                 internal_error(state, set->member, 
21162                                                         "no edge for phi rhs %d", i);
21163                                         }
21164                                         use_point = bset->member->last;
21165                                 }
21166                         }
21167                         if (use_point &&
21168                                 !tdominates(state, ins, use_point)) {
21169                                 if (is_const(ins)) {
21170                                         internal_warning(state, ins, 
21171                                         "non dominated rhs use point %p?", use_point);
21172                                 }
21173                                 else {
21174                                         internal_error(state, ins, 
21175                                                 "non dominated rhs use point %p?", use_point);
21176                                 }
21177                         }
21178                 }
21179                 ins = ins->next;
21180         } while(ins != first);
21181 }
21182
21183 static void verify_rhs(struct compile_state *state)
21184 {
21185         struct triple *first, *ins;
21186         first = state->first;
21187         ins = first;
21188         do {
21189                 struct triple **slot;
21190                 int zrhs, i;
21191                 zrhs = ins->rhs;
21192                 slot = &RHS(ins, 0);
21193                 for(i = 0; i < zrhs; i++) {
21194                         if (slot[i] == 0) {
21195                                 internal_error(state, ins,
21196                                         "missing rhs %d on %s",
21197                                         i, tops(ins->op));
21198                         }
21199                         if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21200                                 internal_error(state, ins,
21201                                         "ins == rhs[%d] on %s",
21202                                         i, tops(ins->op));
21203                         }
21204                 }
21205                 ins = ins->next;
21206         } while(ins != first);
21207 }
21208
21209 static void verify_piece(struct compile_state *state)
21210 {
21211         struct triple *first, *ins;
21212         first = state->first;
21213         ins = first;
21214         do {
21215                 struct triple *ptr;
21216                 int lhs, i;
21217                 lhs = ins->lhs;
21218                 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21219                         if (ptr != LHS(ins, i)) {
21220                                 internal_error(state, ins, "malformed lhs on %s",
21221                                         tops(ins->op));
21222                         }
21223                         if (ptr->op != OP_PIECE) {
21224                                 internal_error(state, ins, "bad lhs op %s at %d on %s",
21225                                         tops(ptr->op), i, tops(ins->op));
21226                         }
21227                         if (ptr->u.cval != i) {
21228                                 internal_error(state, ins, "bad u.cval of %d %d expected",
21229                                         ptr->u.cval, i);
21230                         }
21231                 }
21232                 ins = ins->next;
21233         } while(ins != first);
21234 }
21235
21236 static void verify_ins_colors(struct compile_state *state)
21237 {
21238         struct triple *first, *ins;
21239         
21240         first = state->first;
21241         ins = first;
21242         do {
21243                 ins = ins->next;
21244         } while(ins != first);
21245 }
21246
21247 static void verify_unknown(struct compile_state *state)
21248 {
21249         struct triple *first, *ins;
21250         if (    (unknown_triple.next != &unknown_triple) ||
21251                 (unknown_triple.prev != &unknown_triple) ||
21252 #if 0
21253                 (unknown_triple.use != 0) ||
21254 #endif
21255                 (unknown_triple.op != OP_UNKNOWNVAL) ||
21256                 (unknown_triple.lhs != 0) ||
21257                 (unknown_triple.rhs != 0) ||
21258                 (unknown_triple.misc != 0) ||
21259                 (unknown_triple.targ != 0) ||
21260                 (unknown_triple.template_id != 0) ||
21261                 (unknown_triple.id != -1) ||
21262                 (unknown_triple.type != &unknown_type) ||
21263                 (unknown_triple.occurance != &dummy_occurance) ||
21264                 (unknown_triple.param[0] != 0) ||
21265                 (unknown_triple.param[1] != 0)) {
21266                 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21267         }
21268         if (    (dummy_occurance.count != 2) ||
21269                 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21270                 (strcmp(dummy_occurance.function, "") != 0) ||
21271                 (dummy_occurance.col != 0) ||
21272                 (dummy_occurance.parent != 0)) {
21273                 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21274         }
21275         if (    (unknown_type.type != TYPE_UNKNOWN)) {
21276                 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21277         }
21278         first = state->first;
21279         ins = first;
21280         do {
21281                 int params, i;
21282                 if (ins == &unknown_triple) {
21283                         internal_error(state, ins, "unknown triple in list");
21284                 }
21285                 params = TRIPLE_SIZE(ins);
21286                 for(i = 0; i < params; i++) {
21287                         if (ins->param[i] == &unknown_triple) {
21288                                 internal_error(state, ins, "unknown triple used!");
21289                         }
21290                 }
21291                 ins = ins->next;
21292         } while(ins != first);
21293 }
21294
21295 static void verify_types(struct compile_state *state)
21296 {
21297         struct triple *first, *ins;
21298         first = state->first;
21299         ins = first;
21300         do {
21301                 struct type *invalid;
21302                 invalid = invalid_type(state, ins->type);
21303                 if (invalid) {
21304                         FILE *fp = state->errout;
21305                         fprintf(fp, "type: ");
21306                         name_of(fp, ins->type);
21307                         fprintf(fp, "\n");
21308                         fprintf(fp, "invalid type: ");
21309                         name_of(fp, invalid);
21310                         fprintf(fp, "\n");
21311                         internal_error(state, ins, "invalid ins type");
21312                 }
21313         } while(ins != first);
21314 }
21315
21316 static void verify_copy(struct compile_state *state)
21317 {
21318         struct triple *first, *ins, *next;
21319         first = state->first;
21320         next = ins = first;
21321         do {
21322                 ins = next;
21323                 next = ins->next;
21324                 if (ins->op != OP_COPY) {
21325                         continue;
21326                 }
21327                 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21328                         FILE *fp = state->errout;
21329                         fprintf(fp, "src type: ");
21330                         name_of(fp, RHS(ins, 0)->type);
21331                         fprintf(fp, "\n");
21332                         fprintf(fp, "dst type: ");
21333                         name_of(fp, ins->type);
21334                         fprintf(fp, "\n");
21335                         internal_error(state, ins, "type mismatch in copy");
21336                 }
21337         } while(next != first);
21338 }
21339
21340 static void verify_consistency(struct compile_state *state)
21341 {
21342         verify_unknown(state);
21343         verify_uses(state);
21344         verify_blocks_present(state);
21345         verify_blocks(state);
21346         verify_domination(state);
21347         verify_rhs(state);
21348         verify_piece(state);
21349         verify_ins_colors(state);
21350         verify_types(state);
21351         verify_copy(state);
21352         if (state->compiler->debug & DEBUG_VERIFICATION) {
21353                 fprintf(state->dbgout, "consistency verified\n");
21354         }
21355 }
21356 #else 
21357 static void verify_consistency(struct compile_state *state) {}
21358 #endif /* DEBUG_CONSISTENCY */
21359
21360 static void optimize(struct compile_state *state)
21361 {
21362         /* Join all of the functions into one giant function */
21363         join_functions(state);
21364
21365         /* Dump what the instruction graph intially looks like */
21366         print_triples(state);
21367
21368         /* Replace structures with simpler data types */
21369         decompose_compound_types(state);
21370         print_triples(state);
21371
21372         verify_consistency(state);
21373         /* Analyze the intermediate code */
21374         state->bb.first = state->first;
21375         analyze_basic_blocks(state, &state->bb);
21376
21377         /* Transform the code to ssa form. */
21378         /*
21379          * The transformation to ssa form puts a phi function
21380          * on each of edge of a dominance frontier where that
21381          * phi function might be needed.  At -O2 if we don't
21382          * eleminate the excess phi functions we can get an
21383          * exponential code size growth.  So I kill the extra
21384          * phi functions early and I kill them often.
21385          */
21386         transform_to_ssa_form(state);
21387         verify_consistency(state);
21388
21389         /* Remove dead code */
21390         eliminate_inefectual_code(state);
21391         verify_consistency(state);
21392
21393         /* Do strength reduction and simple constant optimizations */
21394         simplify_all(state);
21395         verify_consistency(state);
21396         /* Propogate constants throughout the code */
21397         scc_transform(state);
21398         verify_consistency(state);
21399 #warning "WISHLIST implement single use constants (least possible register pressure)"
21400 #warning "WISHLIST implement induction variable elimination"
21401         /* Select architecture instructions and an initial partial
21402          * coloring based on architecture constraints.
21403          */
21404         transform_to_arch_instructions(state);
21405         verify_consistency(state);
21406
21407         /* Remove dead code */
21408         eliminate_inefectual_code(state);
21409         verify_consistency(state);
21410
21411         /* Color all of the variables to see if they will fit in registers */
21412         insert_copies_to_phi(state);
21413         verify_consistency(state);
21414
21415         insert_mandatory_copies(state);
21416         verify_consistency(state);
21417
21418         allocate_registers(state);
21419         verify_consistency(state);
21420
21421         /* Remove the optimization information.
21422          * This is more to check for memory consistency than to free memory.
21423          */
21424         free_basic_blocks(state, &state->bb);
21425 }
21426
21427 static void print_op_asm(struct compile_state *state,
21428         struct triple *ins, FILE *fp)
21429 {
21430         struct asm_info *info;
21431         const char *ptr;
21432         unsigned lhs, rhs, i;
21433         info = ins->u.ainfo;
21434         lhs = ins->lhs;
21435         rhs = ins->rhs;
21436         /* Don't count the clobbers in lhs */
21437         for(i = 0; i < lhs; i++) {
21438                 if (LHS(ins, i)->type == &void_type) {
21439                         break;
21440                 }
21441         }
21442         lhs = i;
21443         fprintf(fp, "#ASM\n");
21444         fputc('\t', fp);
21445         for(ptr = info->str; *ptr; ptr++) {
21446                 char *next;
21447                 unsigned long param;
21448                 struct triple *piece;
21449                 if (*ptr != '%') {
21450                         fputc(*ptr, fp);
21451                         continue;
21452                 }
21453                 ptr++;
21454                 if (*ptr == '%') {
21455                         fputc('%', fp);
21456                         continue;
21457                 }
21458                 param = strtoul(ptr, &next, 10);
21459                 if (ptr == next) {
21460                         error(state, ins, "Invalid asm template");
21461                 }
21462                 if (param >= (lhs + rhs)) {
21463                         error(state, ins, "Invalid param %%%u in asm template",
21464                                 param);
21465                 }
21466                 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21467                 fprintf(fp, "%s", 
21468                         arch_reg_str(ID_REG(piece->id)));
21469                 ptr = next -1;
21470         }
21471         fprintf(fp, "\n#NOT ASM\n");
21472 }
21473
21474
21475 /* Only use the low x86 byte registers.  This allows me
21476  * allocate the entire register when a byte register is used.
21477  */
21478 #define X86_4_8BIT_GPRS 1
21479
21480 /* x86 featrues */
21481 #define X86_MMX_REGS  (1<<0)
21482 #define X86_XMM_REGS  (1<<1)
21483 #define X86_NOOP_COPY (1<<2)
21484
21485 /* The x86 register classes */
21486 #define REGC_FLAGS       0
21487 #define REGC_GPR8        1
21488 #define REGC_GPR16       2
21489 #define REGC_GPR32       3
21490 #define REGC_DIVIDEND64  4
21491 #define REGC_DIVIDEND32  5
21492 #define REGC_MMX         6
21493 #define REGC_XMM         7
21494 #define REGC_GPR32_8     8
21495 #define REGC_GPR16_8     9
21496 #define REGC_GPR8_LO    10
21497 #define REGC_IMM32      11
21498 #define REGC_IMM16      12
21499 #define REGC_IMM8       13
21500 #define LAST_REGC  REGC_IMM8
21501 #if LAST_REGC >= MAX_REGC
21502 #error "MAX_REGC is to low"
21503 #endif
21504
21505 /* Register class masks */
21506 #define REGCM_FLAGS      (1 << REGC_FLAGS)
21507 #define REGCM_GPR8       (1 << REGC_GPR8)
21508 #define REGCM_GPR16      (1 << REGC_GPR16)
21509 #define REGCM_GPR32      (1 << REGC_GPR32)
21510 #define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21511 #define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21512 #define REGCM_MMX        (1 << REGC_MMX)
21513 #define REGCM_XMM        (1 << REGC_XMM)
21514 #define REGCM_GPR32_8    (1 << REGC_GPR32_8)
21515 #define REGCM_GPR16_8    (1 << REGC_GPR16_8)
21516 #define REGCM_GPR8_LO    (1 << REGC_GPR8_LO)
21517 #define REGCM_IMM32      (1 << REGC_IMM32)
21518 #define REGCM_IMM16      (1 << REGC_IMM16)
21519 #define REGCM_IMM8       (1 << REGC_IMM8)
21520 #define REGCM_ALL        ((1 << (LAST_REGC + 1)) - 1)
21521 #define REGCM_IMMALL    (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
21522
21523 /* The x86 registers */
21524 #define REG_EFLAGS  2
21525 #define REGC_FLAGS_FIRST REG_EFLAGS
21526 #define REGC_FLAGS_LAST  REG_EFLAGS
21527 #define REG_AL      3
21528 #define REG_BL      4
21529 #define REG_CL      5
21530 #define REG_DL      6
21531 #define REG_AH      7
21532 #define REG_BH      8
21533 #define REG_CH      9
21534 #define REG_DH      10
21535 #define REGC_GPR8_LO_FIRST REG_AL
21536 #define REGC_GPR8_LO_LAST  REG_DL
21537 #define REGC_GPR8_FIRST  REG_AL
21538 #define REGC_GPR8_LAST   REG_DH
21539 #define REG_AX     11
21540 #define REG_BX     12
21541 #define REG_CX     13
21542 #define REG_DX     14
21543 #define REG_SI     15
21544 #define REG_DI     16
21545 #define REG_BP     17
21546 #define REG_SP     18
21547 #define REGC_GPR16_FIRST REG_AX
21548 #define REGC_GPR16_LAST  REG_SP
21549 #define REG_EAX    19
21550 #define REG_EBX    20
21551 #define REG_ECX    21
21552 #define REG_EDX    22
21553 #define REG_ESI    23
21554 #define REG_EDI    24
21555 #define REG_EBP    25
21556 #define REG_ESP    26
21557 #define REGC_GPR32_FIRST REG_EAX
21558 #define REGC_GPR32_LAST  REG_ESP
21559 #define REG_EDXEAX 27
21560 #define REGC_DIVIDEND64_FIRST REG_EDXEAX
21561 #define REGC_DIVIDEND64_LAST  REG_EDXEAX
21562 #define REG_DXAX   28
21563 #define REGC_DIVIDEND32_FIRST REG_DXAX
21564 #define REGC_DIVIDEND32_LAST  REG_DXAX
21565 #define REG_MMX0   29
21566 #define REG_MMX1   30
21567 #define REG_MMX2   31
21568 #define REG_MMX3   32
21569 #define REG_MMX4   33
21570 #define REG_MMX5   34
21571 #define REG_MMX6   35
21572 #define REG_MMX7   36
21573 #define REGC_MMX_FIRST REG_MMX0
21574 #define REGC_MMX_LAST  REG_MMX7
21575 #define REG_XMM0   37
21576 #define REG_XMM1   38
21577 #define REG_XMM2   39
21578 #define REG_XMM3   40
21579 #define REG_XMM4   41
21580 #define REG_XMM5   42
21581 #define REG_XMM6   43
21582 #define REG_XMM7   44
21583 #define REGC_XMM_FIRST REG_XMM0
21584 #define REGC_XMM_LAST  REG_XMM7
21585 #warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
21586 #define LAST_REG   REG_XMM7
21587
21588 #define REGC_GPR32_8_FIRST REG_EAX
21589 #define REGC_GPR32_8_LAST  REG_EDX
21590 #define REGC_GPR16_8_FIRST REG_AX
21591 #define REGC_GPR16_8_LAST  REG_DX
21592
21593 #define REGC_IMM8_FIRST    -1
21594 #define REGC_IMM8_LAST     -1
21595 #define REGC_IMM16_FIRST   -2
21596 #define REGC_IMM16_LAST    -1
21597 #define REGC_IMM32_FIRST   -4
21598 #define REGC_IMM32_LAST    -1
21599
21600 #if LAST_REG >= MAX_REGISTERS
21601 #error "MAX_REGISTERS to low"
21602 #endif
21603
21604
21605 static unsigned regc_size[LAST_REGC +1] = {
21606         [REGC_FLAGS]      = REGC_FLAGS_LAST      - REGC_FLAGS_FIRST + 1,
21607         [REGC_GPR8]       = REGC_GPR8_LAST       - REGC_GPR8_FIRST + 1,
21608         [REGC_GPR16]      = REGC_GPR16_LAST      - REGC_GPR16_FIRST + 1,
21609         [REGC_GPR32]      = REGC_GPR32_LAST      - REGC_GPR32_FIRST + 1,
21610         [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21611         [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21612         [REGC_MMX]        = REGC_MMX_LAST        - REGC_MMX_FIRST + 1,
21613         [REGC_XMM]        = REGC_XMM_LAST        - REGC_XMM_FIRST + 1,
21614         [REGC_GPR32_8]    = REGC_GPR32_8_LAST    - REGC_GPR32_8_FIRST + 1,
21615         [REGC_GPR16_8]    = REGC_GPR16_8_LAST    - REGC_GPR16_8_FIRST + 1,
21616         [REGC_GPR8_LO]    = REGC_GPR8_LO_LAST    - REGC_GPR8_LO_FIRST + 1,
21617         [REGC_IMM32]      = 0,
21618         [REGC_IMM16]      = 0,
21619         [REGC_IMM8]       = 0,
21620 };
21621
21622 static const struct {
21623         int first, last;
21624 } regcm_bound[LAST_REGC + 1] = {
21625         [REGC_FLAGS]      = { REGC_FLAGS_FIRST,      REGC_FLAGS_LAST },
21626         [REGC_GPR8]       = { REGC_GPR8_FIRST,       REGC_GPR8_LAST },
21627         [REGC_GPR16]      = { REGC_GPR16_FIRST,      REGC_GPR16_LAST },
21628         [REGC_GPR32]      = { REGC_GPR32_FIRST,      REGC_GPR32_LAST },
21629         [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21630         [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21631         [REGC_MMX]        = { REGC_MMX_FIRST,        REGC_MMX_LAST },
21632         [REGC_XMM]        = { REGC_XMM_FIRST,        REGC_XMM_LAST },
21633         [REGC_GPR32_8]    = { REGC_GPR32_8_FIRST,    REGC_GPR32_8_LAST },
21634         [REGC_GPR16_8]    = { REGC_GPR16_8_FIRST,    REGC_GPR16_8_LAST },
21635         [REGC_GPR8_LO]    = { REGC_GPR8_LO_FIRST,    REGC_GPR8_LO_LAST },
21636         [REGC_IMM32]      = { REGC_IMM32_FIRST,      REGC_IMM32_LAST },
21637         [REGC_IMM16]      = { REGC_IMM16_FIRST,      REGC_IMM16_LAST },
21638         [REGC_IMM8]       = { REGC_IMM8_FIRST,       REGC_IMM8_LAST },
21639 };
21640
21641 #if ARCH_INPUT_REGS != 4
21642 #error ARCH_INPUT_REGS size mismatch
21643 #endif
21644 static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21645         { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21646         { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21647         { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21648         { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21649 };
21650
21651 #if ARCH_OUTPUT_REGS != 4
21652 #error ARCH_INPUT_REGS size mismatch
21653 #endif
21654 static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21655         { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21656         { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21657         { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21658         { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21659 };
21660
21661 static void init_arch_state(struct arch_state *arch)
21662 {
21663         memset(arch, 0, sizeof(*arch));
21664         arch->features = 0;
21665 }
21666
21667 static const struct compiler_flag arch_flags[] = {
21668         { "mmx",       X86_MMX_REGS },
21669         { "sse",       X86_XMM_REGS },
21670         { "noop-copy", X86_NOOP_COPY },
21671         { 0,     0 },
21672 };
21673 static const struct compiler_flag arch_cpus[] = {
21674         { "i386", 0 },
21675         { "p2",   X86_MMX_REGS },
21676         { "p3",   X86_MMX_REGS | X86_XMM_REGS },
21677         { "p4",   X86_MMX_REGS | X86_XMM_REGS },
21678         { "k7",   X86_MMX_REGS },
21679         { "k8",   X86_MMX_REGS | X86_XMM_REGS },
21680         { "c3",   X86_MMX_REGS },
21681         { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21682         {  0,     0 }
21683 };
21684 static int arch_encode_flag(struct arch_state *arch, const char *flag)
21685 {
21686         int result;
21687         int act;
21688
21689         act = 1;
21690         result = -1;
21691         if (strncmp(flag, "no-", 3) == 0) {
21692                 flag += 3;
21693                 act = 0;
21694         }
21695         if (act && strncmp(flag, "cpu=", 4) == 0) {
21696                 flag += 4;
21697                 result = set_flag(arch_cpus, &arch->features, 1, flag);
21698         }
21699         else {
21700                 result = set_flag(arch_flags, &arch->features, act, flag);
21701         }
21702         return result;
21703 }
21704
21705 static void arch_usage(FILE *fp)
21706 {
21707         flag_usage(fp, arch_flags, "-m", "-mno-");
21708         flag_usage(fp, arch_cpus, "-mcpu=", 0);
21709 }
21710
21711 static unsigned arch_regc_size(struct compile_state *state, int class)
21712 {
21713         if ((class < 0) || (class > LAST_REGC)) {
21714                 return 0;
21715         }
21716         return regc_size[class];
21717 }
21718
21719 static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21720 {
21721         /* See if two register classes may have overlapping registers */
21722         unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21723                 REGCM_GPR32_8 | REGCM_GPR32 | 
21724                 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
21725
21726         /* Special case for the immediates */
21727         if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21728                 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21729                 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21730                 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) { 
21731                 return 0;
21732         }
21733         return (regcm1 & regcm2) ||
21734                 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21735 }
21736
21737 static void arch_reg_equivs(
21738         struct compile_state *state, unsigned *equiv, int reg)
21739 {
21740         if ((reg < 0) || (reg > LAST_REG)) {
21741                 internal_error(state, 0, "invalid register");
21742         }
21743         *equiv++ = reg;
21744         switch(reg) {
21745         case REG_AL:
21746 #if X86_4_8BIT_GPRS
21747                 *equiv++ = REG_AH;
21748 #endif
21749                 *equiv++ = REG_AX;
21750                 *equiv++ = REG_EAX;
21751                 *equiv++ = REG_DXAX;
21752                 *equiv++ = REG_EDXEAX;
21753                 break;
21754         case REG_AH:
21755 #if X86_4_8BIT_GPRS
21756                 *equiv++ = REG_AL;
21757 #endif
21758                 *equiv++ = REG_AX;
21759                 *equiv++ = REG_EAX;
21760                 *equiv++ = REG_DXAX;
21761                 *equiv++ = REG_EDXEAX;
21762                 break;
21763         case REG_BL:  
21764 #if X86_4_8BIT_GPRS
21765                 *equiv++ = REG_BH;
21766 #endif
21767                 *equiv++ = REG_BX;
21768                 *equiv++ = REG_EBX;
21769                 break;
21770
21771         case REG_BH:
21772 #if X86_4_8BIT_GPRS
21773                 *equiv++ = REG_BL;
21774 #endif
21775                 *equiv++ = REG_BX;
21776                 *equiv++ = REG_EBX;
21777                 break;
21778         case REG_CL:
21779 #if X86_4_8BIT_GPRS
21780                 *equiv++ = REG_CH;
21781 #endif
21782                 *equiv++ = REG_CX;
21783                 *equiv++ = REG_ECX;
21784                 break;
21785
21786         case REG_CH:
21787 #if X86_4_8BIT_GPRS
21788                 *equiv++ = REG_CL;
21789 #endif
21790                 *equiv++ = REG_CX;
21791                 *equiv++ = REG_ECX;
21792                 break;
21793         case REG_DL:
21794 #if X86_4_8BIT_GPRS
21795                 *equiv++ = REG_DH;
21796 #endif
21797                 *equiv++ = REG_DX;
21798                 *equiv++ = REG_EDX;
21799                 *equiv++ = REG_DXAX;
21800                 *equiv++ = REG_EDXEAX;
21801                 break;
21802         case REG_DH:
21803 #if X86_4_8BIT_GPRS
21804                 *equiv++ = REG_DL;
21805 #endif
21806                 *equiv++ = REG_DX;
21807                 *equiv++ = REG_EDX;
21808                 *equiv++ = REG_DXAX;
21809                 *equiv++ = REG_EDXEAX;
21810                 break;
21811         case REG_AX:
21812                 *equiv++ = REG_AL;
21813                 *equiv++ = REG_AH;
21814                 *equiv++ = REG_EAX;
21815                 *equiv++ = REG_DXAX;
21816                 *equiv++ = REG_EDXEAX;
21817                 break;
21818         case REG_BX:
21819                 *equiv++ = REG_BL;
21820                 *equiv++ = REG_BH;
21821                 *equiv++ = REG_EBX;
21822                 break;
21823         case REG_CX:  
21824                 *equiv++ = REG_CL;
21825                 *equiv++ = REG_CH;
21826                 *equiv++ = REG_ECX;
21827                 break;
21828         case REG_DX:  
21829                 *equiv++ = REG_DL;
21830                 *equiv++ = REG_DH;
21831                 *equiv++ = REG_EDX;
21832                 *equiv++ = REG_DXAX;
21833                 *equiv++ = REG_EDXEAX;
21834                 break;
21835         case REG_SI:  
21836                 *equiv++ = REG_ESI;
21837                 break;
21838         case REG_DI:
21839                 *equiv++ = REG_EDI;
21840                 break;
21841         case REG_BP:
21842                 *equiv++ = REG_EBP;
21843                 break;
21844         case REG_SP:
21845                 *equiv++ = REG_ESP;
21846                 break;
21847         case REG_EAX:
21848                 *equiv++ = REG_AL;
21849                 *equiv++ = REG_AH;
21850                 *equiv++ = REG_AX;
21851                 *equiv++ = REG_DXAX;
21852                 *equiv++ = REG_EDXEAX;
21853                 break;
21854         case REG_EBX:
21855                 *equiv++ = REG_BL;
21856                 *equiv++ = REG_BH;
21857                 *equiv++ = REG_BX;
21858                 break;
21859         case REG_ECX:
21860                 *equiv++ = REG_CL;
21861                 *equiv++ = REG_CH;
21862                 *equiv++ = REG_CX;
21863                 break;
21864         case REG_EDX:
21865                 *equiv++ = REG_DL;
21866                 *equiv++ = REG_DH;
21867                 *equiv++ = REG_DX;
21868                 *equiv++ = REG_DXAX;
21869                 *equiv++ = REG_EDXEAX;
21870                 break;
21871         case REG_ESI: 
21872                 *equiv++ = REG_SI;
21873                 break;
21874         case REG_EDI: 
21875                 *equiv++ = REG_DI;
21876                 break;
21877         case REG_EBP: 
21878                 *equiv++ = REG_BP;
21879                 break;
21880         case REG_ESP: 
21881                 *equiv++ = REG_SP;
21882                 break;
21883         case REG_DXAX: 
21884                 *equiv++ = REG_AL;
21885                 *equiv++ = REG_AH;
21886                 *equiv++ = REG_DL;
21887                 *equiv++ = REG_DH;
21888                 *equiv++ = REG_AX;
21889                 *equiv++ = REG_DX;
21890                 *equiv++ = REG_EAX;
21891                 *equiv++ = REG_EDX;
21892                 *equiv++ = REG_EDXEAX;
21893                 break;
21894         case REG_EDXEAX: 
21895                 *equiv++ = REG_AL;
21896                 *equiv++ = REG_AH;
21897                 *equiv++ = REG_DL;
21898                 *equiv++ = REG_DH;
21899                 *equiv++ = REG_AX;
21900                 *equiv++ = REG_DX;
21901                 *equiv++ = REG_EAX;
21902                 *equiv++ = REG_EDX;
21903                 *equiv++ = REG_DXAX;
21904                 break;
21905         }
21906         *equiv++ = REG_UNSET; 
21907 }
21908
21909 static unsigned arch_avail_mask(struct compile_state *state)
21910 {
21911         unsigned avail_mask;
21912         /* REGCM_GPR8 is not available */
21913         avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 | 
21914                 REGCM_GPR32 | REGCM_GPR32_8 | 
21915                 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
21916                 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
21917         if (state->arch->features & X86_MMX_REGS) {
21918                 avail_mask |= REGCM_MMX;
21919         }
21920         if (state->arch->features & X86_XMM_REGS) {
21921                 avail_mask |= REGCM_XMM;
21922         }
21923         return avail_mask;
21924 }
21925
21926 static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
21927 {
21928         unsigned mask, result;
21929         int class, class2;
21930         result = regcm;
21931
21932         for(class = 0, mask = 1; mask; mask <<= 1, class++) {
21933                 if ((result & mask) == 0) {
21934                         continue;
21935                 }
21936                 if (class > LAST_REGC) {
21937                         result &= ~mask;
21938                 }
21939                 for(class2 = 0; class2 <= LAST_REGC; class2++) {
21940                         if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
21941                                 (regcm_bound[class2].last <= regcm_bound[class].last)) {
21942                                 result |= (1 << class2);
21943                         }
21944                 }
21945         }
21946         result &= arch_avail_mask(state);
21947         return result;
21948 }
21949
21950 static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
21951 {
21952         /* Like arch_regcm_normalize except immediate register classes are excluded */
21953         regcm = arch_regcm_normalize(state, regcm);
21954         /* Remove the immediate register classes */
21955         regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
21956         return regcm;
21957         
21958 }
21959
21960 static unsigned arch_reg_regcm(struct compile_state *state, int reg)
21961 {
21962         unsigned mask;
21963         int class;
21964         mask = 0;
21965         for(class = 0; class <= LAST_REGC; class++) {
21966                 if ((reg >= regcm_bound[class].first) &&
21967                         (reg <= regcm_bound[class].last)) {
21968                         mask |= (1 << class);
21969                 }
21970         }
21971         if (!mask) {
21972                 internal_error(state, 0, "reg %d not in any class", reg);
21973         }
21974         return mask;
21975 }
21976
21977 static struct reg_info arch_reg_constraint(
21978         struct compile_state *state, struct type *type, const char *constraint)
21979 {
21980         static const struct {
21981                 char class;
21982                 unsigned int mask;
21983                 unsigned int reg;
21984         } constraints[] = {
21985                 { 'r', REGCM_GPR32,   REG_UNSET },
21986                 { 'g', REGCM_GPR32,   REG_UNSET },
21987                 { 'p', REGCM_GPR32,   REG_UNSET },
21988                 { 'q', REGCM_GPR8_LO, REG_UNSET },
21989                 { 'Q', REGCM_GPR32_8, REG_UNSET },
21990                 { 'x', REGCM_XMM,     REG_UNSET },
21991                 { 'y', REGCM_MMX,     REG_UNSET },
21992                 { 'a', REGCM_GPR32,   REG_EAX },
21993                 { 'b', REGCM_GPR32,   REG_EBX },
21994                 { 'c', REGCM_GPR32,   REG_ECX },
21995                 { 'd', REGCM_GPR32,   REG_EDX },
21996                 { 'D', REGCM_GPR32,   REG_EDI },
21997                 { 'S', REGCM_GPR32,   REG_ESI },
21998                 { '\0', 0, REG_UNSET },
21999         };
22000         unsigned int regcm;
22001         unsigned int mask, reg;
22002         struct reg_info result;
22003         const char *ptr;
22004         regcm = arch_type_to_regcm(state, type);
22005         reg = REG_UNSET;
22006         mask = 0;
22007         for(ptr = constraint; *ptr; ptr++) {
22008                 int i;
22009                 if (*ptr ==  ' ') {
22010                         continue;
22011                 }
22012                 for(i = 0; constraints[i].class != '\0'; i++) {
22013                         if (constraints[i].class == *ptr) {
22014                                 break;
22015                         }
22016                 }
22017                 if (constraints[i].class == '\0') {
22018                         error(state, 0, "invalid register constraint ``%c''", *ptr);
22019                         break;
22020                 }
22021                 if ((constraints[i].mask & regcm) == 0) {
22022                         error(state, 0, "invalid register class %c specified",
22023                                 *ptr);
22024                 }
22025                 mask |= constraints[i].mask;
22026                 if (constraints[i].reg != REG_UNSET) {
22027                         if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22028                                 error(state, 0, "Only one register may be specified");
22029                         }
22030                         reg = constraints[i].reg;
22031                 }
22032         }
22033         result.reg = reg;
22034         result.regcm = mask;
22035         return result;
22036 }
22037
22038 static struct reg_info arch_reg_clobber(
22039         struct compile_state *state, const char *clobber)
22040 {
22041         struct reg_info result;
22042         if (strcmp(clobber, "memory") == 0) {
22043                 result.reg = REG_UNSET;
22044                 result.regcm = 0;
22045         }
22046         else if (strcmp(clobber, "eax") == 0) {
22047                 result.reg = REG_EAX;
22048                 result.regcm = REGCM_GPR32;
22049         }
22050         else if (strcmp(clobber, "ebx") == 0) {
22051                 result.reg = REG_EBX;
22052                 result.regcm = REGCM_GPR32;
22053         }
22054         else if (strcmp(clobber, "ecx") == 0) {
22055                 result.reg = REG_ECX;
22056                 result.regcm = REGCM_GPR32;
22057         }
22058         else if (strcmp(clobber, "edx") == 0) {
22059                 result.reg = REG_EDX;
22060                 result.regcm = REGCM_GPR32;
22061         }
22062         else if (strcmp(clobber, "esi") == 0) {
22063                 result.reg = REG_ESI;
22064                 result.regcm = REGCM_GPR32;
22065         }
22066         else if (strcmp(clobber, "edi") == 0) {
22067                 result.reg = REG_EDI;
22068                 result.regcm = REGCM_GPR32;
22069         }
22070         else if (strcmp(clobber, "ebp") == 0) {
22071                 result.reg = REG_EBP;
22072                 result.regcm = REGCM_GPR32;
22073         }
22074         else if (strcmp(clobber, "esp") == 0) {
22075                 result.reg = REG_ESP;
22076                 result.regcm = REGCM_GPR32;
22077         }
22078         else if (strcmp(clobber, "cc") == 0) {
22079                 result.reg = REG_EFLAGS;
22080                 result.regcm = REGCM_FLAGS;
22081         }
22082         else if ((strncmp(clobber, "xmm", 3) == 0)  &&
22083                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22084                 result.reg = REG_XMM0 + octdigval(clobber[3]);
22085                 result.regcm = REGCM_XMM;
22086         }
22087         else if ((strncmp(clobber, "mm", 2) == 0) &&
22088                 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22089                 result.reg = REG_MMX0 + octdigval(clobber[3]);
22090                 result.regcm = REGCM_MMX;
22091         }
22092         else {
22093                 error(state, 0, "unknown register name `%s' in asm",
22094                         clobber);
22095                 result.reg = REG_UNSET;
22096                 result.regcm = 0;
22097         }
22098         return result;
22099 }
22100
22101 static int do_select_reg(struct compile_state *state, 
22102         char *used, int reg, unsigned classes)
22103 {
22104         unsigned mask;
22105         if (used[reg]) {
22106                 return REG_UNSET;
22107         }
22108         mask = arch_reg_regcm(state, reg);
22109         return (classes & mask) ? reg : REG_UNSET;
22110 }
22111
22112 static int arch_select_free_register(
22113         struct compile_state *state, char *used, int classes)
22114 {
22115         /* Live ranges with the most neighbors are colored first.
22116          *
22117          * Generally it does not matter which colors are given
22118          * as the register allocator attempts to color live ranges
22119          * in an order where you are guaranteed not to run out of colors.
22120          *
22121          * Occasionally the register allocator cannot find an order
22122          * of register selection that will find a free color.  To
22123          * increase the odds the register allocator will work when
22124          * it guesses first give out registers from register classes
22125          * least likely to run out of registers.
22126          * 
22127          */
22128         int i, reg;
22129         reg = REG_UNSET;
22130         for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
22131                 reg = do_select_reg(state, used, i, classes);
22132         }
22133         for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22134                 reg = do_select_reg(state, used, i, classes);
22135         }
22136         for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
22137                 reg = do_select_reg(state, used, i, classes);
22138         }
22139         for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22140                 reg = do_select_reg(state, used, i, classes);
22141         }
22142         for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22143                 reg = do_select_reg(state, used, i, classes);
22144         }
22145         for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22146                 reg = do_select_reg(state, used, i, classes);
22147         }
22148         for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22149                 reg = do_select_reg(state, used, i, classes);
22150         }
22151         for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
22152                 reg = do_select_reg(state, used, i, classes);
22153         }
22154         for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22155                 reg = do_select_reg(state, used, i, classes);
22156         }
22157         return reg;
22158 }
22159
22160
22161 static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type) 
22162 {
22163 #warning "FIXME force types smaller (if legal) before I get here"
22164         unsigned mask;
22165         mask = 0;
22166         switch(type->type & TYPE_MASK) {
22167         case TYPE_ARRAY:
22168         case TYPE_VOID: 
22169                 mask = 0; 
22170                 break;
22171         case TYPE_CHAR:
22172         case TYPE_UCHAR:
22173                 mask = REGCM_GPR8 | REGCM_GPR8_LO |
22174                         REGCM_GPR16 | REGCM_GPR16_8 | 
22175                         REGCM_GPR32 | REGCM_GPR32_8 |
22176                         REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22177                         REGCM_MMX | REGCM_XMM |
22178                         REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
22179                 break;
22180         case TYPE_SHORT:
22181         case TYPE_USHORT:
22182                 mask =  REGCM_GPR16 | REGCM_GPR16_8 |
22183                         REGCM_GPR32 | REGCM_GPR32_8 |
22184                         REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22185                         REGCM_MMX | REGCM_XMM |
22186                         REGCM_IMM32 | REGCM_IMM16;
22187                 break;
22188         case TYPE_ENUM:
22189         case TYPE_INT:
22190         case TYPE_UINT:
22191         case TYPE_LONG:
22192         case TYPE_ULONG:
22193         case TYPE_POINTER:
22194                 mask =  REGCM_GPR32 | REGCM_GPR32_8 |
22195                         REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22196                         REGCM_MMX | REGCM_XMM |
22197                         REGCM_IMM32;
22198                 break;
22199         case TYPE_JOIN:
22200         case TYPE_UNION:
22201                 mask = arch_type_to_regcm(state, type->left);
22202                 break;
22203         case TYPE_OVERLAP:
22204                 mask = arch_type_to_regcm(state, type->left) &
22205                         arch_type_to_regcm(state, type->right);
22206                 break;
22207         case TYPE_BITFIELD:
22208                 mask = arch_type_to_regcm(state, type->left);
22209                 break;
22210         default:
22211                 fprintf(state->errout, "type: ");
22212                 name_of(state->errout, type);
22213                 fprintf(state->errout, "\n");
22214                 internal_error(state, 0, "no register class for type");
22215                 break;
22216         }
22217         mask = arch_regcm_normalize(state, mask);
22218         return mask;
22219 }
22220
22221 static int is_imm32(struct triple *imm)
22222 {
22223         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
22224                 (imm->op == OP_ADDRCONST);
22225         
22226 }
22227 static int is_imm16(struct triple *imm)
22228 {
22229         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22230 }
22231 static int is_imm8(struct triple *imm)
22232 {
22233         return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22234 }
22235
22236 static int get_imm32(struct triple *ins, struct triple **expr)
22237 {
22238         struct triple *imm;
22239         imm = *expr;
22240         while(imm->op == OP_COPY) {
22241                 imm = RHS(imm, 0);
22242         }
22243         if (!is_imm32(imm)) {
22244                 return 0;
22245         }
22246         unuse_triple(*expr, ins);
22247         use_triple(imm, ins);
22248         *expr = imm;
22249         return 1;
22250 }
22251
22252 static int get_imm8(struct triple *ins, struct triple **expr)
22253 {
22254         struct triple *imm;
22255         imm = *expr;
22256         while(imm->op == OP_COPY) {
22257                 imm = RHS(imm, 0);
22258         }
22259         if (!is_imm8(imm)) {
22260                 return 0;
22261         }
22262         unuse_triple(*expr, ins);
22263         use_triple(imm, ins);
22264         *expr = imm;
22265         return 1;
22266 }
22267
22268 #define TEMPLATE_NOP           0
22269 #define TEMPLATE_INTCONST8     1
22270 #define TEMPLATE_INTCONST32    2
22271 #define TEMPLATE_UNKNOWNVAL    3
22272 #define TEMPLATE_COPY8_REG     5
22273 #define TEMPLATE_COPY16_REG    6
22274 #define TEMPLATE_COPY32_REG    7
22275 #define TEMPLATE_COPY_IMM8     8
22276 #define TEMPLATE_COPY_IMM16    9
22277 #define TEMPLATE_COPY_IMM32   10
22278 #define TEMPLATE_PHI8         11
22279 #define TEMPLATE_PHI16        12
22280 #define TEMPLATE_PHI32        13
22281 #define TEMPLATE_STORE8       14
22282 #define TEMPLATE_STORE16      15
22283 #define TEMPLATE_STORE32      16
22284 #define TEMPLATE_LOAD8        17
22285 #define TEMPLATE_LOAD16       18
22286 #define TEMPLATE_LOAD32       19
22287 #define TEMPLATE_BINARY8_REG  20
22288 #define TEMPLATE_BINARY16_REG 21
22289 #define TEMPLATE_BINARY32_REG 22
22290 #define TEMPLATE_BINARY8_IMM  23
22291 #define TEMPLATE_BINARY16_IMM 24
22292 #define TEMPLATE_BINARY32_IMM 25
22293 #define TEMPLATE_SL8_CL       26
22294 #define TEMPLATE_SL16_CL      27
22295 #define TEMPLATE_SL32_CL      28
22296 #define TEMPLATE_SL8_IMM      29
22297 #define TEMPLATE_SL16_IMM     30
22298 #define TEMPLATE_SL32_IMM     31
22299 #define TEMPLATE_UNARY8       32
22300 #define TEMPLATE_UNARY16      33
22301 #define TEMPLATE_UNARY32      34
22302 #define TEMPLATE_CMP8_REG     35
22303 #define TEMPLATE_CMP16_REG    36
22304 #define TEMPLATE_CMP32_REG    37
22305 #define TEMPLATE_CMP8_IMM     38
22306 #define TEMPLATE_CMP16_IMM    39
22307 #define TEMPLATE_CMP32_IMM    40
22308 #define TEMPLATE_TEST8        41
22309 #define TEMPLATE_TEST16       42
22310 #define TEMPLATE_TEST32       43
22311 #define TEMPLATE_SET          44
22312 #define TEMPLATE_JMP          45
22313 #define TEMPLATE_RET          46
22314 #define TEMPLATE_INB_DX       47
22315 #define TEMPLATE_INB_IMM      48
22316 #define TEMPLATE_INW_DX       49
22317 #define TEMPLATE_INW_IMM      50
22318 #define TEMPLATE_INL_DX       51
22319 #define TEMPLATE_INL_IMM      52
22320 #define TEMPLATE_OUTB_DX      53
22321 #define TEMPLATE_OUTB_IMM     54
22322 #define TEMPLATE_OUTW_DX      55
22323 #define TEMPLATE_OUTW_IMM     56
22324 #define TEMPLATE_OUTL_DX      57
22325 #define TEMPLATE_OUTL_IMM     58
22326 #define TEMPLATE_BSF          59
22327 #define TEMPLATE_RDMSR        60
22328 #define TEMPLATE_WRMSR        61
22329 #define TEMPLATE_UMUL8        62
22330 #define TEMPLATE_UMUL16       63
22331 #define TEMPLATE_UMUL32       64
22332 #define TEMPLATE_DIV8         65
22333 #define TEMPLATE_DIV16        66
22334 #define TEMPLATE_DIV32        67
22335 #define LAST_TEMPLATE       TEMPLATE_DIV32
22336 #if LAST_TEMPLATE >= MAX_TEMPLATES
22337 #error "MAX_TEMPLATES to low"
22338 #endif
22339
22340 #define COPY8_REGCM     (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22341 #define COPY16_REGCM    (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)  
22342 #define COPY32_REGCM    (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
22343
22344
22345 static struct ins_template templates[] = {
22346         [TEMPLATE_NOP]      = {
22347                 .lhs = { 
22348                         [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22349                         [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22350                         [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22351                         [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22352                         [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22353                         [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22354                         [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22355                         [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22356                         [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22357                         [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22358                         [10] = { REG_UNNEEDED, REGCM_IMMALL },
22359                         [11] = { REG_UNNEEDED, REGCM_IMMALL },
22360                         [12] = { REG_UNNEEDED, REGCM_IMMALL },
22361                         [13] = { REG_UNNEEDED, REGCM_IMMALL },
22362                         [14] = { REG_UNNEEDED, REGCM_IMMALL },
22363                         [15] = { REG_UNNEEDED, REGCM_IMMALL },
22364                         [16] = { REG_UNNEEDED, REGCM_IMMALL },
22365                         [17] = { REG_UNNEEDED, REGCM_IMMALL },
22366                         [18] = { REG_UNNEEDED, REGCM_IMMALL },
22367                         [19] = { REG_UNNEEDED, REGCM_IMMALL },
22368                         [20] = { REG_UNNEEDED, REGCM_IMMALL },
22369                         [21] = { REG_UNNEEDED, REGCM_IMMALL },
22370                         [22] = { REG_UNNEEDED, REGCM_IMMALL },
22371                         [23] = { REG_UNNEEDED, REGCM_IMMALL },
22372                         [24] = { REG_UNNEEDED, REGCM_IMMALL },
22373                         [25] = { REG_UNNEEDED, REGCM_IMMALL },
22374                         [26] = { REG_UNNEEDED, REGCM_IMMALL },
22375                         [27] = { REG_UNNEEDED, REGCM_IMMALL },
22376                         [28] = { REG_UNNEEDED, REGCM_IMMALL },
22377                         [29] = { REG_UNNEEDED, REGCM_IMMALL },
22378                         [30] = { REG_UNNEEDED, REGCM_IMMALL },
22379                         [31] = { REG_UNNEEDED, REGCM_IMMALL },
22380                         [32] = { REG_UNNEEDED, REGCM_IMMALL },
22381                         [33] = { REG_UNNEEDED, REGCM_IMMALL },
22382                         [34] = { REG_UNNEEDED, REGCM_IMMALL },
22383                         [35] = { REG_UNNEEDED, REGCM_IMMALL },
22384                         [36] = { REG_UNNEEDED, REGCM_IMMALL },
22385                         [37] = { REG_UNNEEDED, REGCM_IMMALL },
22386                         [38] = { REG_UNNEEDED, REGCM_IMMALL },
22387                         [39] = { REG_UNNEEDED, REGCM_IMMALL },
22388                         [40] = { REG_UNNEEDED, REGCM_IMMALL },
22389                         [41] = { REG_UNNEEDED, REGCM_IMMALL },
22390                         [42] = { REG_UNNEEDED, REGCM_IMMALL },
22391                         [43] = { REG_UNNEEDED, REGCM_IMMALL },
22392                         [44] = { REG_UNNEEDED, REGCM_IMMALL },
22393                         [45] = { REG_UNNEEDED, REGCM_IMMALL },
22394                         [46] = { REG_UNNEEDED, REGCM_IMMALL },
22395                         [47] = { REG_UNNEEDED, REGCM_IMMALL },
22396                         [48] = { REG_UNNEEDED, REGCM_IMMALL },
22397                         [49] = { REG_UNNEEDED, REGCM_IMMALL },
22398                         [50] = { REG_UNNEEDED, REGCM_IMMALL },
22399                         [51] = { REG_UNNEEDED, REGCM_IMMALL },
22400                         [52] = { REG_UNNEEDED, REGCM_IMMALL },
22401                         [53] = { REG_UNNEEDED, REGCM_IMMALL },
22402                         [54] = { REG_UNNEEDED, REGCM_IMMALL },
22403                         [55] = { REG_UNNEEDED, REGCM_IMMALL },
22404                         [56] = { REG_UNNEEDED, REGCM_IMMALL },
22405                         [57] = { REG_UNNEEDED, REGCM_IMMALL },
22406                         [58] = { REG_UNNEEDED, REGCM_IMMALL },
22407                         [59] = { REG_UNNEEDED, REGCM_IMMALL },
22408                         [60] = { REG_UNNEEDED, REGCM_IMMALL },
22409                         [61] = { REG_UNNEEDED, REGCM_IMMALL },
22410                         [62] = { REG_UNNEEDED, REGCM_IMMALL },
22411                         [63] = { REG_UNNEEDED, REGCM_IMMALL },
22412                 },
22413         },
22414         [TEMPLATE_INTCONST8] = { 
22415                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22416         },
22417         [TEMPLATE_INTCONST32] = { 
22418                 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22419         },
22420         [TEMPLATE_UNKNOWNVAL] = {
22421                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22422         },
22423         [TEMPLATE_COPY8_REG] = {
22424                 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22425                 .rhs = { [0] = { REG_UNSET, COPY8_REGCM }  },
22426         },
22427         [TEMPLATE_COPY16_REG] = {
22428                 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22429                 .rhs = { [0] = { REG_UNSET, COPY16_REGCM }  },
22430         },
22431         [TEMPLATE_COPY32_REG] = {
22432                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22433                 .rhs = { [0] = { REG_UNSET, COPY32_REGCM }  },
22434         },
22435         [TEMPLATE_COPY_IMM8] = {
22436                 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22437                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22438         },
22439         [TEMPLATE_COPY_IMM16] = {
22440                 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22441                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22442         },
22443         [TEMPLATE_COPY_IMM32] = {
22444                 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22445                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22446         },
22447         [TEMPLATE_PHI8] = { 
22448                 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22449                 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22450         },
22451         [TEMPLATE_PHI16] = { 
22452                 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22453                 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } }, 
22454         },
22455         [TEMPLATE_PHI32] = { 
22456                 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22457                 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } }, 
22458         },
22459         [TEMPLATE_STORE8] = {
22460                 .rhs = { 
22461                         [0] = { REG_UNSET, REGCM_GPR32 },
22462                         [1] = { REG_UNSET, REGCM_GPR8_LO },
22463                 },
22464         },
22465         [TEMPLATE_STORE16] = {
22466                 .rhs = { 
22467                         [0] = { REG_UNSET, REGCM_GPR32 },
22468                         [1] = { REG_UNSET, REGCM_GPR16 },
22469                 },
22470         },
22471         [TEMPLATE_STORE32] = {
22472                 .rhs = { 
22473                         [0] = { REG_UNSET, REGCM_GPR32 },
22474                         [1] = { REG_UNSET, REGCM_GPR32 },
22475                 },
22476         },
22477         [TEMPLATE_LOAD8] = {
22478                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22479                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22480         },
22481         [TEMPLATE_LOAD16] = {
22482                 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22483                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22484         },
22485         [TEMPLATE_LOAD32] = {
22486                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22487                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22488         },
22489         [TEMPLATE_BINARY8_REG] = {
22490                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22491                 .rhs = { 
22492                         [0] = { REG_VIRT0, REGCM_GPR8_LO },
22493                         [1] = { REG_UNSET, REGCM_GPR8_LO },
22494                 },
22495         },
22496         [TEMPLATE_BINARY16_REG] = {
22497                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22498                 .rhs = { 
22499                         [0] = { REG_VIRT0, REGCM_GPR16 },
22500                         [1] = { REG_UNSET, REGCM_GPR16 },
22501                 },
22502         },
22503         [TEMPLATE_BINARY32_REG] = {
22504                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22505                 .rhs = { 
22506                         [0] = { REG_VIRT0, REGCM_GPR32 },
22507                         [1] = { REG_UNSET, REGCM_GPR32 },
22508                 },
22509         },
22510         [TEMPLATE_BINARY8_IMM] = {
22511                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22512                 .rhs = { 
22513                         [0] = { REG_VIRT0,    REGCM_GPR8_LO },
22514                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22515                 },
22516         },
22517         [TEMPLATE_BINARY16_IMM] = {
22518                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22519                 .rhs = { 
22520                         [0] = { REG_VIRT0,    REGCM_GPR16 },
22521                         [1] = { REG_UNNEEDED, REGCM_IMM16 },
22522                 },
22523         },
22524         [TEMPLATE_BINARY32_IMM] = {
22525                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22526                 .rhs = { 
22527                         [0] = { REG_VIRT0,    REGCM_GPR32 },
22528                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
22529                 },
22530         },
22531         [TEMPLATE_SL8_CL] = {
22532                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22533                 .rhs = { 
22534                         [0] = { REG_VIRT0, REGCM_GPR8_LO },
22535                         [1] = { REG_CL, REGCM_GPR8_LO },
22536                 },
22537         },
22538         [TEMPLATE_SL16_CL] = {
22539                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22540                 .rhs = { 
22541                         [0] = { REG_VIRT0, REGCM_GPR16 },
22542                         [1] = { REG_CL, REGCM_GPR8_LO },
22543                 },
22544         },
22545         [TEMPLATE_SL32_CL] = {
22546                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22547                 .rhs = { 
22548                         [0] = { REG_VIRT0, REGCM_GPR32 },
22549                         [1] = { REG_CL, REGCM_GPR8_LO },
22550                 },
22551         },
22552         [TEMPLATE_SL8_IMM] = {
22553                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22554                 .rhs = { 
22555                         [0] = { REG_VIRT0,    REGCM_GPR8_LO },
22556                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22557                 },
22558         },
22559         [TEMPLATE_SL16_IMM] = {
22560                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22561                 .rhs = { 
22562                         [0] = { REG_VIRT0,    REGCM_GPR16 },
22563                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22564                 },
22565         },
22566         [TEMPLATE_SL32_IMM] = {
22567                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22568                 .rhs = { 
22569                         [0] = { REG_VIRT0,    REGCM_GPR32 },
22570                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22571                 },
22572         },
22573         [TEMPLATE_UNARY8] = {
22574                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22575                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22576         },
22577         [TEMPLATE_UNARY16] = {
22578                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22579                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22580         },
22581         [TEMPLATE_UNARY32] = {
22582                 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22583                 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22584         },
22585         [TEMPLATE_CMP8_REG] = {
22586                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22587                 .rhs = {
22588                         [0] = { REG_UNSET, REGCM_GPR8_LO },
22589                         [1] = { REG_UNSET, REGCM_GPR8_LO },
22590                 },
22591         },
22592         [TEMPLATE_CMP16_REG] = {
22593                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22594                 .rhs = {
22595                         [0] = { REG_UNSET, REGCM_GPR16 },
22596                         [1] = { REG_UNSET, REGCM_GPR16 },
22597                 },
22598         },
22599         [TEMPLATE_CMP32_REG] = {
22600                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22601                 .rhs = {
22602                         [0] = { REG_UNSET, REGCM_GPR32 },
22603                         [1] = { REG_UNSET, REGCM_GPR32 },
22604                 },
22605         },
22606         [TEMPLATE_CMP8_IMM] = {
22607                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22608                 .rhs = {
22609                         [0] = { REG_UNSET, REGCM_GPR8_LO },
22610                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22611                 },
22612         },
22613         [TEMPLATE_CMP16_IMM] = {
22614                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22615                 .rhs = {
22616                         [0] = { REG_UNSET, REGCM_GPR16 },
22617                         [1] = { REG_UNNEEDED, REGCM_IMM16 },
22618                 },
22619         },
22620         [TEMPLATE_CMP32_IMM] = {
22621                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22622                 .rhs = {
22623                         [0] = { REG_UNSET, REGCM_GPR32 },
22624                         [1] = { REG_UNNEEDED, REGCM_IMM32 },
22625                 },
22626         },
22627         [TEMPLATE_TEST8] = {
22628                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22629                 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22630         },
22631         [TEMPLATE_TEST16] = {
22632                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22633                 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22634         },
22635         [TEMPLATE_TEST32] = {
22636                 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22637                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22638         },
22639         [TEMPLATE_SET] = {
22640                 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22641                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22642         },
22643         [TEMPLATE_JMP] = {
22644                 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22645         },
22646         [TEMPLATE_RET] = {
22647                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22648         },
22649         [TEMPLATE_INB_DX] = {
22650                 .lhs = { [0] = { REG_AL,  REGCM_GPR8_LO } },  
22651                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22652         },
22653         [TEMPLATE_INB_IMM] = {
22654                 .lhs = { [0] = { REG_AL,  REGCM_GPR8_LO } },  
22655                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22656         },
22657         [TEMPLATE_INW_DX]  = { 
22658                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
22659                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22660         },
22661         [TEMPLATE_INW_IMM] = { 
22662                 .lhs = { [0] = { REG_AX,  REGCM_GPR16 } }, 
22663                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22664         },
22665         [TEMPLATE_INL_DX]  = {
22666                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22667                 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22668         },
22669         [TEMPLATE_INL_IMM] = {
22670                 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22671                 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22672         },
22673         [TEMPLATE_OUTB_DX] = { 
22674                 .rhs = {
22675                         [0] = { REG_AL,  REGCM_GPR8_LO },
22676                         [1] = { REG_DX, REGCM_GPR16 },
22677                 },
22678         },
22679         [TEMPLATE_OUTB_IMM] = { 
22680                 .rhs = {
22681                         [0] = { REG_AL,  REGCM_GPR8_LO },  
22682                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22683                 },
22684         },
22685         [TEMPLATE_OUTW_DX] = { 
22686                 .rhs = {
22687                         [0] = { REG_AX,  REGCM_GPR16 },
22688                         [1] = { REG_DX, REGCM_GPR16 },
22689                 },
22690         },
22691         [TEMPLATE_OUTW_IMM] = {
22692                 .rhs = {
22693                         [0] = { REG_AX,  REGCM_GPR16 }, 
22694                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22695                 },
22696         },
22697         [TEMPLATE_OUTL_DX] = { 
22698                 .rhs = {
22699                         [0] = { REG_EAX, REGCM_GPR32 },
22700                         [1] = { REG_DX, REGCM_GPR16 },
22701                 },
22702         },
22703         [TEMPLATE_OUTL_IMM] = { 
22704                 .rhs = {
22705                         [0] = { REG_EAX, REGCM_GPR32 }, 
22706                         [1] = { REG_UNNEEDED, REGCM_IMM8 },
22707                 },
22708         },
22709         [TEMPLATE_BSF] = {
22710                 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22711                 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22712         },
22713         [TEMPLATE_RDMSR] = {
22714                 .lhs = { 
22715                         [0] = { REG_EAX, REGCM_GPR32 },
22716                         [1] = { REG_EDX, REGCM_GPR32 },
22717                 },
22718                 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22719         },
22720         [TEMPLATE_WRMSR] = {
22721                 .rhs = {
22722                         [0] = { REG_ECX, REGCM_GPR32 },
22723                         [1] = { REG_EAX, REGCM_GPR32 },
22724                         [2] = { REG_EDX, REGCM_GPR32 },
22725                 },
22726         },
22727         [TEMPLATE_UMUL8] = {
22728                 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22729                 .rhs = { 
22730                         [0] = { REG_AL, REGCM_GPR8_LO },
22731                         [1] = { REG_UNSET, REGCM_GPR8_LO },
22732                 },
22733         },
22734         [TEMPLATE_UMUL16] = {
22735                 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22736                 .rhs = { 
22737                         [0] = { REG_AX, REGCM_GPR16 },
22738                         [1] = { REG_UNSET, REGCM_GPR16 },
22739                 },
22740         },
22741         [TEMPLATE_UMUL32] = {
22742                 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
22743                 .rhs = { 
22744                         [0] = { REG_EAX, REGCM_GPR32 },
22745                         [1] = { REG_UNSET, REGCM_GPR32 },
22746                 },
22747         },
22748         [TEMPLATE_DIV8] = {
22749                 .lhs = { 
22750                         [0] = { REG_AL, REGCM_GPR8_LO },
22751                         [1] = { REG_AH, REGCM_GPR8 },
22752                 },
22753                 .rhs = {
22754                         [0] = { REG_AX, REGCM_GPR16 },
22755                         [1] = { REG_UNSET, REGCM_GPR8_LO },
22756                 },
22757         },
22758         [TEMPLATE_DIV16] = {
22759                 .lhs = { 
22760                         [0] = { REG_AX, REGCM_GPR16 },
22761                         [1] = { REG_DX, REGCM_GPR16 },
22762                 },
22763                 .rhs = {
22764                         [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22765                         [1] = { REG_UNSET, REGCM_GPR16 },
22766                 },
22767         },
22768         [TEMPLATE_DIV32] = {
22769                 .lhs = { 
22770                         [0] = { REG_EAX, REGCM_GPR32 },
22771                         [1] = { REG_EDX, REGCM_GPR32 },
22772                 },
22773                 .rhs = {
22774                         [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
22775                         [1] = { REG_UNSET, REGCM_GPR32 },
22776                 },
22777         },
22778 };
22779
22780 static void fixup_branch(struct compile_state *state,
22781         struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
22782         struct triple *left, struct triple *right)
22783 {
22784         struct triple *test;
22785         if (!left) {
22786                 internal_error(state, branch, "no branch test?");
22787         }
22788         test = pre_triple(state, branch,
22789                 cmp_op, cmp_type, left, right);
22790         test->template_id = TEMPLATE_TEST32; 
22791         if (cmp_op == OP_CMP) {
22792                 test->template_id = TEMPLATE_CMP32_REG;
22793                 if (get_imm32(test, &RHS(test, 1))) {
22794                         test->template_id = TEMPLATE_CMP32_IMM;
22795                 }
22796         }
22797         use_triple(RHS(test, 0), test);
22798         use_triple(RHS(test, 1), test);
22799         unuse_triple(RHS(branch, 0), branch);
22800         RHS(branch, 0) = test;
22801         branch->op = jmp_op;
22802         branch->template_id = TEMPLATE_JMP;
22803         use_triple(RHS(branch, 0), branch);
22804 }
22805
22806 static void fixup_branches(struct compile_state *state,
22807         struct triple *cmp, struct triple *use, int jmp_op)
22808 {
22809         struct triple_set *entry, *next;
22810         for(entry = use->use; entry; entry = next) {
22811                 next = entry->next;
22812                 if (entry->member->op == OP_COPY) {
22813                         fixup_branches(state, cmp, entry->member, jmp_op);
22814                 }
22815                 else if (entry->member->op == OP_CBRANCH) {
22816                         struct triple *branch;
22817                         struct triple *left, *right;
22818                         left = right = 0;
22819                         left = RHS(cmp, 0);
22820                         if (cmp->rhs > 1) {
22821                                 right = RHS(cmp, 1);
22822                         }
22823                         branch = entry->member;
22824                         fixup_branch(state, branch, jmp_op, 
22825                                 cmp->op, cmp->type, left, right);
22826                 }
22827         }
22828 }
22829
22830 static void bool_cmp(struct compile_state *state, 
22831         struct triple *ins, int cmp_op, int jmp_op, int set_op)
22832 {
22833         struct triple_set *entry, *next;
22834         struct triple *set, *convert;
22835
22836         /* Put a barrier up before the cmp which preceeds the
22837          * copy instruction.  If a set actually occurs this gives
22838          * us a chance to move variables in registers out of the way.
22839          */
22840
22841         /* Modify the comparison operator */
22842         ins->op = cmp_op;
22843         ins->template_id = TEMPLATE_TEST32;
22844         if (cmp_op == OP_CMP) {
22845                 ins->template_id = TEMPLATE_CMP32_REG;
22846                 if (get_imm32(ins, &RHS(ins, 1))) {
22847                         ins->template_id =  TEMPLATE_CMP32_IMM;
22848                 }
22849         }
22850         /* Generate the instruction sequence that will transform the
22851          * result of the comparison into a logical value.
22852          */
22853         set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
22854         use_triple(ins, set);
22855         set->template_id = TEMPLATE_SET;
22856
22857         convert = set;
22858         if (!equiv_types(ins->type, set->type)) {
22859                 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
22860                 use_triple(set, convert);
22861                 convert->template_id = TEMPLATE_COPY32_REG;
22862         }
22863
22864         for(entry = ins->use; entry; entry = next) {
22865                 next = entry->next;
22866                 if (entry->member == set) {
22867                         continue;
22868                 }
22869                 replace_rhs_use(state, ins, convert, entry->member);
22870         }
22871         fixup_branches(state, ins, convert, jmp_op);
22872 }
22873
22874 struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
22875 {
22876         struct ins_template *template;
22877         struct reg_info result;
22878         int zlhs;
22879         if (ins->op == OP_PIECE) {
22880                 index = ins->u.cval;
22881                 ins = MISC(ins, 0);
22882         }
22883         zlhs = ins->lhs;
22884         if (triple_is_def(state, ins)) {
22885                 zlhs = 1;
22886         }
22887         if (index >= zlhs) {
22888                 internal_error(state, ins, "index %d out of range for %s",
22889                         index, tops(ins->op));
22890         }
22891         switch(ins->op) {
22892         case OP_ASM:
22893                 template = &ins->u.ainfo->tmpl;
22894                 break;
22895         default:
22896                 if (ins->template_id > LAST_TEMPLATE) {
22897                         internal_error(state, ins, "bad template number %d", 
22898                                 ins->template_id);
22899                 }
22900                 template = &templates[ins->template_id];
22901                 break;
22902         }
22903         result = template->lhs[index];
22904         result.regcm = arch_regcm_normalize(state, result.regcm);
22905         if (result.reg != REG_UNNEEDED) {
22906                 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22907         }
22908         if (result.regcm == 0) {
22909                 internal_error(state, ins, "lhs %d regcm == 0", index);
22910         }
22911         return result;
22912 }
22913
22914 struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
22915 {
22916         struct reg_info result;
22917         struct ins_template *template;
22918         if ((index > ins->rhs) ||
22919                 (ins->op == OP_PIECE)) {
22920                 internal_error(state, ins, "index %d out of range for %s\n",
22921                         index, tops(ins->op));
22922         }
22923         switch(ins->op) {
22924         case OP_ASM:
22925                 template = &ins->u.ainfo->tmpl;
22926                 break;
22927         case OP_PHI:
22928                 index = 0;
22929                 /* Fall through */
22930         default:
22931                 if (ins->template_id > LAST_TEMPLATE) {
22932                         internal_error(state, ins, "bad template number %d", 
22933                                 ins->template_id);
22934                 }
22935                 template = &templates[ins->template_id];
22936                 break;
22937         }
22938         result = template->rhs[index];
22939         result.regcm = arch_regcm_normalize(state, result.regcm);
22940         if (result.regcm == 0) {
22941                 internal_error(state, ins, "rhs %d regcm == 0", index);
22942         }
22943         return result;
22944 }
22945
22946 static struct triple *mod_div(struct compile_state *state,
22947         struct triple *ins, int div_op, int index)
22948 {
22949         struct triple *div, *piece0, *piece1;
22950         
22951         /* Generate the appropriate division instruction */
22952         div = post_triple(state, ins, div_op, ins->type, 0, 0);
22953         RHS(div, 0) = RHS(ins, 0);
22954         RHS(div, 1) = RHS(ins, 1);
22955         piece0 = LHS(div, 0);
22956         piece1 = LHS(div, 1);
22957         div->template_id  = TEMPLATE_DIV32;
22958         use_triple(RHS(div, 0), div);
22959         use_triple(RHS(div, 1), div);
22960         use_triple(LHS(div, 0), div);
22961         use_triple(LHS(div, 1), div);
22962
22963         /* Replate uses of ins with the appropriate piece of the div */
22964         propogate_use(state, ins, LHS(div, index));
22965         release_triple(state, ins);
22966
22967         /* Return the address of the next instruction */
22968         return piece1->next;
22969 }
22970
22971 static int noop_adecl(struct triple *adecl)
22972 {
22973         struct triple_set *use;
22974         /* It's a noop if it doesn't specify stoorage */
22975         if (adecl->lhs == 0) {
22976                 return 1;
22977         }
22978         /* Is the adecl used? If not it's a noop */
22979         for(use = adecl->use; use ; use = use->next) {
22980                 if ((use->member->op != OP_PIECE) ||
22981                         (MISC(use->member, 0) != adecl)) {
22982                         return 0;
22983                 }
22984         }
22985         return 1;
22986 }
22987
22988 static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
22989 {
22990         struct triple *mask, *nmask, *shift;
22991         struct triple *val, *val_mask, *val_shift;
22992         struct triple *targ, *targ_mask;
22993         struct triple *new;
22994         ulong_t the_mask, the_nmask;
22995
22996         targ = RHS(ins, 0);
22997         val = RHS(ins, 1);
22998
22999         /* Get constant for the mask value */
23000         the_mask = 1;
23001         the_mask <<= ins->u.bitfield.size;
23002         the_mask -= 1;
23003         the_mask <<= ins->u.bitfield.offset;
23004         mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23005         mask->u.cval = the_mask;
23006
23007         /* Get the inverted mask value */
23008         the_nmask = ~the_mask;
23009         nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23010         nmask->u.cval = the_nmask;
23011
23012         /* Get constant for the shift value */
23013         shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23014         shift->u.cval = ins->u.bitfield.offset;
23015
23016         /* Shift and mask the source value */
23017         val_shift = val;
23018         if (shift->u.cval != 0) {
23019                 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23020                 use_triple(val, val_shift);
23021                 use_triple(shift, val_shift);
23022         }
23023         val_mask = val_shift;
23024         if (is_signed(val->type)) {
23025                 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23026                 use_triple(val_shift, val_mask);
23027                 use_triple(mask, val_mask);
23028         }
23029
23030         /* Mask the target value */
23031         targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23032         use_triple(targ, targ_mask);
23033         use_triple(nmask, targ_mask);
23034
23035         /* Now combined them together */
23036         new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23037         use_triple(targ_mask, new);
23038         use_triple(val_mask, new);
23039
23040         /* Move all of the users over to the new expression */
23041         propogate_use(state, ins, new);
23042
23043         /* Delete the original triple */
23044         release_triple(state, ins);
23045
23046         /* Restart the transformation at mask */
23047         return mask;
23048 }
23049
23050 static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23051 {
23052         struct triple *mask, *shift;
23053         struct triple *val, *val_mask, *val_shift;
23054         ulong_t the_mask;
23055
23056         val = RHS(ins, 0);
23057
23058         /* Get constant for the mask value */
23059         the_mask = 1;
23060         the_mask <<= ins->u.bitfield.size;
23061         the_mask -= 1;
23062         mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23063         mask->u.cval = the_mask;
23064
23065         /* Get constant for the right shift value */
23066         shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23067         shift->u.cval = ins->u.bitfield.offset;
23068
23069         /* Shift arithmetic right, to correct the sign */
23070         val_shift = val;
23071         if (shift->u.cval != 0) {
23072                 int op;
23073                 if (ins->op == OP_SEXTRACT) {
23074                         op = OP_SSR;
23075                 } else {
23076                         op = OP_USR;
23077                 }
23078                 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23079                 use_triple(val, val_shift);
23080                 use_triple(shift, val_shift);
23081         }
23082
23083         /* Finally mask the value */
23084         val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23085         use_triple(val_shift, val_mask);
23086         use_triple(mask,      val_mask);
23087
23088         /* Move all of the users over to the new expression */
23089         propogate_use(state, ins, val_mask);
23090
23091         /* Release the original instruction */
23092         release_triple(state, ins);
23093
23094         return mask;
23095
23096 }
23097
23098 static struct triple *transform_to_arch_instruction(
23099         struct compile_state *state, struct triple *ins)
23100 {
23101         /* Transform from generic 3 address instructions
23102          * to archtecture specific instructions.
23103          * And apply architecture specific constraints to instructions.
23104          * Copies are inserted to preserve the register flexibility
23105          * of 3 address instructions.
23106          */
23107         struct triple *next, *value;
23108         size_t size;
23109         next = ins->next;
23110         switch(ins->op) {
23111         case OP_INTCONST:
23112                 ins->template_id = TEMPLATE_INTCONST32;
23113                 if (ins->u.cval < 256) {
23114                         ins->template_id = TEMPLATE_INTCONST8;
23115                 }
23116                 break;
23117         case OP_ADDRCONST:
23118                 ins->template_id = TEMPLATE_INTCONST32;
23119                 break;
23120         case OP_UNKNOWNVAL:
23121                 ins->template_id = TEMPLATE_UNKNOWNVAL;
23122                 break;
23123         case OP_NOOP:
23124         case OP_SDECL:
23125         case OP_BLOBCONST:
23126         case OP_LABEL:
23127                 ins->template_id = TEMPLATE_NOP;
23128                 break;
23129         case OP_COPY:
23130         case OP_CONVERT:
23131                 size = size_of(state, ins->type);
23132                 value = RHS(ins, 0);
23133                 if (is_imm8(value) && (size <= SIZEOF_I8)) {
23134                         ins->template_id = TEMPLATE_COPY_IMM8;
23135                 }
23136                 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
23137                         ins->template_id = TEMPLATE_COPY_IMM16;
23138                 }
23139                 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
23140                         ins->template_id = TEMPLATE_COPY_IMM32;
23141                 }
23142                 else if (is_const(value)) {
23143                         internal_error(state, ins, "bad constant passed to copy");
23144                 }
23145                 else if (size <= SIZEOF_I8) {
23146                         ins->template_id = TEMPLATE_COPY8_REG;
23147                 }
23148                 else if (size <= SIZEOF_I16) {
23149                         ins->template_id = TEMPLATE_COPY16_REG;
23150                 }
23151                 else if (size <= SIZEOF_I32) {
23152                         ins->template_id = TEMPLATE_COPY32_REG;
23153                 }
23154                 else {
23155                         internal_error(state, ins, "bad type passed to copy");
23156                 }
23157                 break;
23158         case OP_PHI:
23159                 size = size_of(state, ins->type);
23160                 if (size <= SIZEOF_I8) {
23161                         ins->template_id = TEMPLATE_PHI8;
23162                 }
23163                 else if (size <= SIZEOF_I16) {
23164                         ins->template_id = TEMPLATE_PHI16;
23165                 }
23166                 else if (size <= SIZEOF_I32) {
23167                         ins->template_id = TEMPLATE_PHI32;
23168                 }
23169                 else {
23170                         internal_error(state, ins, "bad type passed to phi");
23171                 }
23172                 break;
23173         case OP_ADECL:
23174                 /* Adecls should always be treated as dead code and
23175                  * removed.  If we are not optimizing they may linger.
23176                  */
23177                 if (!noop_adecl(ins)) {
23178                         internal_error(state, ins, "adecl remains?");
23179                 }
23180                 ins->template_id = TEMPLATE_NOP;
23181                 next = after_lhs(state, ins);
23182                 break;
23183         case OP_STORE:
23184                 switch(ins->type->type & TYPE_MASK) {
23185                 case TYPE_CHAR:    case TYPE_UCHAR:
23186                         ins->template_id = TEMPLATE_STORE8;
23187                         break;
23188                 case TYPE_SHORT:   case TYPE_USHORT:
23189                         ins->template_id = TEMPLATE_STORE16;
23190                         break;
23191                 case TYPE_INT:     case TYPE_UINT:
23192                 case TYPE_LONG:    case TYPE_ULONG:
23193                 case TYPE_POINTER:
23194                         ins->template_id = TEMPLATE_STORE32;
23195                         break;
23196                 default:
23197                         internal_error(state, ins, "unknown type in store");
23198                         break;
23199                 }
23200                 break;
23201         case OP_LOAD:
23202                 switch(ins->type->type & TYPE_MASK) {
23203                 case TYPE_CHAR:   case TYPE_UCHAR:
23204                 case TYPE_SHORT:  case TYPE_USHORT:
23205                 case TYPE_INT:    case TYPE_UINT:
23206                 case TYPE_LONG:   case TYPE_ULONG:
23207                 case TYPE_POINTER:
23208                         break;
23209                 default:
23210                         internal_error(state, ins, "unknown type in load");
23211                         break;
23212                 }
23213                 ins->template_id = TEMPLATE_LOAD32;
23214                 break;
23215         case OP_ADD:
23216         case OP_SUB:
23217         case OP_AND:
23218         case OP_XOR:
23219         case OP_OR:
23220         case OP_SMUL:
23221                 ins->template_id = TEMPLATE_BINARY32_REG;
23222                 if (get_imm32(ins, &RHS(ins, 1))) {
23223                         ins->template_id = TEMPLATE_BINARY32_IMM;
23224                 }
23225                 break;
23226         case OP_SDIVT:
23227         case OP_UDIVT:
23228                 ins->template_id = TEMPLATE_DIV32;
23229                 next = after_lhs(state, ins);
23230                 break;
23231         case OP_UMUL:
23232                 ins->template_id = TEMPLATE_UMUL32;
23233                 break;
23234         case OP_UDIV:
23235                 next = mod_div(state, ins, OP_UDIVT, 0);
23236                 break;
23237         case OP_SDIV:
23238                 next = mod_div(state, ins, OP_SDIVT, 0);
23239                 break;
23240         case OP_UMOD:
23241                 next = mod_div(state, ins, OP_UDIVT, 1);
23242                 break;
23243         case OP_SMOD:
23244                 next = mod_div(state, ins, OP_SDIVT, 1);
23245                 break;
23246         case OP_SL:
23247         case OP_SSR:
23248         case OP_USR:
23249                 ins->template_id = TEMPLATE_SL32_CL;
23250                 if (get_imm8(ins, &RHS(ins, 1))) {
23251                         ins->template_id = TEMPLATE_SL32_IMM;
23252                 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23253                         typed_pre_copy(state, &uchar_type, ins, 1);
23254                 }
23255                 break;
23256         case OP_INVERT:
23257         case OP_NEG:
23258                 ins->template_id = TEMPLATE_UNARY32;
23259                 break;
23260         case OP_EQ: 
23261                 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ); 
23262                 break;
23263         case OP_NOTEQ:
23264                 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23265                 break;
23266         case OP_SLESS:
23267                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23268                 break;
23269         case OP_ULESS:
23270                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23271                 break;
23272         case OP_SMORE:
23273                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23274                 break;
23275         case OP_UMORE:
23276                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23277                 break;
23278         case OP_SLESSEQ:
23279                 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23280                 break;
23281         case OP_ULESSEQ:
23282                 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23283                 break;
23284         case OP_SMOREEQ:
23285                 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23286                 break;
23287         case OP_UMOREEQ:
23288                 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23289                 break;
23290         case OP_LTRUE:
23291                 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23292                 break;
23293         case OP_LFALSE:
23294                 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23295                 break;
23296         case OP_BRANCH:
23297                 ins->op = OP_JMP;
23298                 ins->template_id = TEMPLATE_NOP;
23299                 break;
23300         case OP_CBRANCH:
23301                 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST, 
23302                         RHS(ins, 0)->type, RHS(ins, 0), 0);
23303                 break;
23304         case OP_CALL:
23305                 ins->template_id = TEMPLATE_NOP;
23306                 break;
23307         case OP_RET:
23308                 ins->template_id = TEMPLATE_RET;
23309                 break;
23310         case OP_INB:
23311         case OP_INW:
23312         case OP_INL:
23313                 switch(ins->op) {
23314                 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23315                 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23316                 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23317                 }
23318                 if (get_imm8(ins, &RHS(ins, 0))) {
23319                         ins->template_id += 1;
23320                 }
23321                 break;
23322         case OP_OUTB:
23323         case OP_OUTW:
23324         case OP_OUTL:
23325                 switch(ins->op) {
23326                 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23327                 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23328                 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23329                 }
23330                 if (get_imm8(ins, &RHS(ins, 1))) {
23331                         ins->template_id += 1;
23332                 }
23333                 break;
23334         case OP_BSF:
23335         case OP_BSR:
23336                 ins->template_id = TEMPLATE_BSF;
23337                 break;
23338         case OP_RDMSR:
23339                 ins->template_id = TEMPLATE_RDMSR;
23340                 next = after_lhs(state, ins);
23341                 break;
23342         case OP_WRMSR:
23343                 ins->template_id = TEMPLATE_WRMSR;
23344                 break;
23345         case OP_HLT:
23346                 ins->template_id = TEMPLATE_NOP;
23347                 break;
23348         case OP_ASM:
23349                 ins->template_id = TEMPLATE_NOP;
23350                 next = after_lhs(state, ins);
23351                 break;
23352                 /* Already transformed instructions */
23353         case OP_TEST:
23354                 ins->template_id = TEMPLATE_TEST32;
23355                 break;
23356         case OP_CMP:
23357                 ins->template_id = TEMPLATE_CMP32_REG;
23358                 if (get_imm32(ins, &RHS(ins, 1))) {
23359                         ins->template_id = TEMPLATE_CMP32_IMM;
23360                 }
23361                 break;
23362         case OP_JMP:
23363                 ins->template_id = TEMPLATE_NOP;
23364                 break;
23365         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
23366         case OP_JMP_SLESS:   case OP_JMP_ULESS:
23367         case OP_JMP_SMORE:   case OP_JMP_UMORE:
23368         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23369         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23370                 ins->template_id = TEMPLATE_JMP;
23371                 break;
23372         case OP_SET_EQ:      case OP_SET_NOTEQ:
23373         case OP_SET_SLESS:   case OP_SET_ULESS:
23374         case OP_SET_SMORE:   case OP_SET_UMORE:
23375         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23376         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23377                 ins->template_id = TEMPLATE_SET;
23378                 break;
23379         case OP_DEPOSIT:
23380                 next = x86_deposit(state, ins);
23381                 break;
23382         case OP_SEXTRACT:
23383         case OP_UEXTRACT:
23384                 next = x86_extract(state, ins);
23385                 break;
23386                 /* Unhandled instructions */
23387         case OP_PIECE:
23388         default:
23389                 internal_error(state, ins, "unhandled ins: %d %s",
23390                         ins->op, tops(ins->op));
23391                 break;
23392         }
23393         return next;
23394 }
23395
23396 static long next_label(struct compile_state *state)
23397 {
23398         static long label_counter = 1000;
23399         return ++label_counter;
23400 }
23401 static void generate_local_labels(struct compile_state *state)
23402 {
23403         struct triple *first, *label;
23404         first = state->first;
23405         label = first;
23406         do {
23407                 if ((label->op == OP_LABEL) || 
23408                         (label->op == OP_SDECL)) {
23409                         if (label->use) {
23410                                 label->u.cval = next_label(state);
23411                         } else {
23412                                 label->u.cval = 0;
23413                         }
23414                         
23415                 }
23416                 label = label->next;
23417         } while(label != first);
23418 }
23419
23420 static int check_reg(struct compile_state *state, 
23421         struct triple *triple, int classes)
23422 {
23423         unsigned mask;
23424         int reg;
23425         reg = ID_REG(triple->id);
23426         if (reg == REG_UNSET) {
23427                 internal_error(state, triple, "register not set");
23428         }
23429         mask = arch_reg_regcm(state, reg);
23430         if (!(classes & mask)) {
23431                 internal_error(state, triple, "reg %d in wrong class",
23432                         reg);
23433         }
23434         return reg;
23435 }
23436
23437
23438 #if REG_XMM7 != 44
23439 #error "Registers have renumberd fix arch_reg_str"
23440 #endif
23441 static const char *arch_regs[] = {
23442         "%unset",
23443         "%unneeded",
23444         "%eflags",
23445         "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23446         "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23447         "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23448         "%edx:%eax",
23449         "%dx:%ax",
23450         "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23451         "%xmm0", "%xmm1", "%xmm2", "%xmm3", 
23452         "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23453 };
23454 static const char *arch_reg_str(int reg)
23455 {
23456         if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23457                 reg = 0;
23458         }
23459         return arch_regs[reg];
23460 }
23461
23462 static const char *reg(struct compile_state *state, struct triple *triple,
23463         int classes)
23464 {
23465         int reg;
23466         reg = check_reg(state, triple, classes);
23467         return arch_reg_str(reg);
23468 }
23469
23470 static int arch_reg_size(int reg)
23471 {
23472         int size;
23473         size = 0;
23474         if (reg == REG_EFLAGS) {
23475                 size = 32;
23476         }
23477         else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23478                 size = 8;
23479         }
23480         else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23481                 size = 16;
23482         }
23483         else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23484                 size = 32;
23485         }
23486         else if (reg == REG_EDXEAX) {
23487                 size = 64;
23488         }
23489         else if (reg == REG_DXAX) {
23490                 size = 32;
23491         }
23492         else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23493                 size = 64;
23494         }
23495         else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23496                 size = 128;
23497         }
23498         return size;
23499 }
23500
23501 static int reg_size(struct compile_state *state, struct triple *ins)
23502 {
23503         int reg;
23504         reg = ID_REG(ins->id);
23505         if (reg == REG_UNSET) {
23506                 internal_error(state, ins, "register not set");
23507         }
23508         return arch_reg_size(reg);
23509 }
23510         
23511
23512
23513 const char *type_suffix(struct compile_state *state, struct type *type)
23514 {
23515         const char *suffix;
23516         switch(size_of(state, type)) {
23517         case SIZEOF_I8:  suffix = "b"; break;
23518         case SIZEOF_I16: suffix = "w"; break;
23519         case SIZEOF_I32: suffix = "l"; break;
23520         default:
23521                 internal_error(state, 0, "unknown suffix");
23522                 suffix = 0;
23523                 break;
23524         }
23525         return suffix;
23526 }
23527
23528 static void print_const_val(
23529         struct compile_state *state, struct triple *ins, FILE *fp)
23530 {
23531         switch(ins->op) {
23532         case OP_INTCONST:
23533                 fprintf(fp, " $%ld ", 
23534                         (long)(ins->u.cval));
23535                 break;
23536         case OP_ADDRCONST:
23537                 if ((MISC(ins, 0)->op != OP_SDECL) &&
23538                         (MISC(ins, 0)->op != OP_LABEL))
23539                 {
23540                         internal_error(state, ins, "bad base for addrconst");
23541                 }
23542                 if (MISC(ins, 0)->u.cval <= 0) {
23543                         internal_error(state, ins, "unlabeled constant");
23544                 }
23545                 fprintf(fp, " $L%s%lu+%lu ",
23546                         state->compiler->label_prefix, 
23547                         (unsigned long)(MISC(ins, 0)->u.cval),
23548                         (unsigned long)(ins->u.cval));
23549                 break;
23550         default:
23551                 internal_error(state, ins, "unknown constant type");
23552                 break;
23553         }
23554 }
23555
23556 static void print_const(struct compile_state *state,
23557         struct triple *ins, FILE *fp)
23558 {
23559         switch(ins->op) {
23560         case OP_INTCONST:
23561                 switch(ins->type->type & TYPE_MASK) {
23562                 case TYPE_CHAR:
23563                 case TYPE_UCHAR:
23564                         fprintf(fp, ".byte 0x%02lx\n", 
23565                                 (unsigned long)(ins->u.cval));
23566                         break;
23567                 case TYPE_SHORT:
23568                 case TYPE_USHORT:
23569                         fprintf(fp, ".short 0x%04lx\n", 
23570                                 (unsigned long)(ins->u.cval));
23571                         break;
23572                 case TYPE_INT:
23573                 case TYPE_UINT:
23574                 case TYPE_LONG:
23575                 case TYPE_ULONG:
23576                 case TYPE_POINTER:
23577                         fprintf(fp, ".int %lu\n", 
23578                                 (unsigned long)(ins->u.cval));
23579                         break;
23580                 default:
23581                         fprintf(state->errout, "type: ");
23582                         name_of(state->errout, ins->type);
23583                         fprintf(state->errout, "\n");
23584                         internal_error(state, ins, "Unknown constant type. Val: %lu",
23585                                 (unsigned long)(ins->u.cval));
23586                 }
23587                 
23588                 break;
23589         case OP_ADDRCONST:
23590                 if ((MISC(ins, 0)->op != OP_SDECL) &&
23591                         (MISC(ins, 0)->op != OP_LABEL)) {
23592                         internal_error(state, ins, "bad base for addrconst");
23593                 }
23594                 if (MISC(ins, 0)->u.cval <= 0) {
23595                         internal_error(state, ins, "unlabeled constant");
23596                 }
23597                 fprintf(fp, ".int L%s%lu+%lu\n",
23598                         state->compiler->label_prefix,
23599                         (unsigned long)(MISC(ins, 0)->u.cval),
23600                         (unsigned long)(ins->u.cval));
23601                 break;
23602         case OP_BLOBCONST:
23603         {
23604                 unsigned char *blob;
23605                 size_t size, i;
23606                 size = size_of_in_bytes(state, ins->type);
23607                 blob = ins->u.blob;
23608                 for(i = 0; i < size; i++) {
23609                         fprintf(fp, ".byte 0x%02x\n",
23610                                 blob[i]);
23611                 }
23612                 break;
23613         }
23614         default:
23615                 internal_error(state, ins, "Unknown constant type");
23616                 break;
23617         }
23618 }
23619
23620 #define TEXT_SECTION ".rom.text"
23621 #define DATA_SECTION ".rom.data"
23622
23623 static long get_const_pool_ref(
23624         struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
23625 {
23626         size_t fill_bytes;
23627         long ref;
23628         ref = next_label(state);
23629         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
23630         fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
23631         fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
23632         print_const(state, ins, fp);
23633         fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23634         if (fill_bytes) {
23635                 fprintf(fp, ".fill %d, 1, 0\n", fill_bytes);
23636         }
23637         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23638         return ref;
23639 }
23640
23641 static long get_mask_pool_ref(
23642         struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23643 {
23644         long ref;
23645         if (mask == 0xff) {
23646                 ref = 1;
23647         }
23648         else if (mask == 0xffff) {
23649                 ref = 2;
23650         }
23651         else {
23652                 ref = 0;
23653                 internal_error(state, ins, "unhandled mask value");
23654         }
23655         return ref;
23656 }
23657
23658 static void print_binary_op(struct compile_state *state,
23659         const char *op, struct triple *ins, FILE *fp) 
23660 {
23661         unsigned mask;
23662         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
23663         if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
23664                 internal_error(state, ins, "invalid register assignment");
23665         }
23666         if (is_const(RHS(ins, 1))) {
23667                 fprintf(fp, "\t%s ", op);
23668                 print_const_val(state, RHS(ins, 1), fp);
23669                 fprintf(fp, ", %s\n",
23670                         reg(state, RHS(ins, 0), mask));
23671         }
23672         else {
23673                 unsigned lmask, rmask;
23674                 int lreg, rreg;
23675                 lreg = check_reg(state, RHS(ins, 0), mask);
23676                 rreg = check_reg(state, RHS(ins, 1), mask);
23677                 lmask = arch_reg_regcm(state, lreg);
23678                 rmask = arch_reg_regcm(state, rreg);
23679                 mask = lmask & rmask;
23680                 fprintf(fp, "\t%s %s, %s\n",
23681                         op,
23682                         reg(state, RHS(ins, 1), mask),
23683                         reg(state, RHS(ins, 0), mask));
23684         }
23685 }
23686 static void print_unary_op(struct compile_state *state, 
23687         const char *op, struct triple *ins, FILE *fp)
23688 {
23689         unsigned mask;
23690         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
23691         fprintf(fp, "\t%s %s\n",
23692                 op,
23693                 reg(state, RHS(ins, 0), mask));
23694 }
23695
23696 static void print_op_shift(struct compile_state *state,
23697         const char *op, struct triple *ins, FILE *fp)
23698 {
23699         unsigned mask;
23700         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
23701         if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
23702                 internal_error(state, ins, "invalid register assignment");
23703         }
23704         if (is_const(RHS(ins, 1))) {
23705                 fprintf(fp, "\t%s ", op);
23706                 print_const_val(state, RHS(ins, 1), fp);
23707                 fprintf(fp, ", %s\n",
23708                         reg(state, RHS(ins, 0), mask));
23709         }
23710         else {
23711                 fprintf(fp, "\t%s %s, %s\n",
23712                         op,
23713                         reg(state, RHS(ins, 1), REGCM_GPR8_LO),
23714                         reg(state, RHS(ins, 0), mask));
23715         }
23716 }
23717
23718 static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23719 {
23720         const char *op;
23721         int mask;
23722         int dreg;
23723         mask = 0;
23724         switch(ins->op) {
23725         case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
23726         case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23727         case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23728         default:
23729                 internal_error(state, ins, "not an in operation");
23730                 op = 0;
23731                 break;
23732         }
23733         dreg = check_reg(state, ins, mask);
23734         if (!reg_is_reg(state, dreg, REG_EAX)) {
23735                 internal_error(state, ins, "dst != %%eax");
23736         }
23737         if (is_const(RHS(ins, 0))) {
23738                 fprintf(fp, "\t%s ", op);
23739                 print_const_val(state, RHS(ins, 0), fp);
23740                 fprintf(fp, ", %s\n",
23741                         reg(state, ins, mask));
23742         }
23743         else {
23744                 int addr_reg;
23745                 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
23746                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23747                         internal_error(state, ins, "src != %%dx");
23748                 }
23749                 fprintf(fp, "\t%s %s, %s\n",
23750                         op, 
23751                         reg(state, RHS(ins, 0), REGCM_GPR16),
23752                         reg(state, ins, mask));
23753         }
23754 }
23755
23756 static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23757 {
23758         const char *op;
23759         int mask;
23760         int lreg;
23761         mask = 0;
23762         switch(ins->op) {
23763         case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
23764         case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23765         case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23766         default:
23767                 internal_error(state, ins, "not an out operation");
23768                 op = 0;
23769                 break;
23770         }
23771         lreg = check_reg(state, RHS(ins, 0), mask);
23772         if (!reg_is_reg(state, lreg, REG_EAX)) {
23773                 internal_error(state, ins, "src != %%eax");
23774         }
23775         if (is_const(RHS(ins, 1))) {
23776                 fprintf(fp, "\t%s %s,", 
23777                         op, reg(state, RHS(ins, 0), mask));
23778                 print_const_val(state, RHS(ins, 1), fp);
23779                 fprintf(fp, "\n");
23780         }
23781         else {
23782                 int addr_reg;
23783                 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
23784                 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23785                         internal_error(state, ins, "dst != %%dx");
23786                 }
23787                 fprintf(fp, "\t%s %s, %s\n",
23788                         op, 
23789                         reg(state, RHS(ins, 0), mask),
23790                         reg(state, RHS(ins, 1), REGCM_GPR16));
23791         }
23792 }
23793
23794 static void print_op_move(struct compile_state *state,
23795         struct triple *ins, FILE *fp)
23796 {
23797         /* op_move is complex because there are many types
23798          * of registers we can move between.
23799          * Because OP_COPY will be introduced in arbitrary locations
23800          * OP_COPY must not affect flags.
23801          * OP_CONVERT can change the flags and it is the only operation
23802          * where it is expected the types in the registers can change.
23803          */
23804         int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
23805         struct triple *dst, *src;
23806         if (state->arch->features & X86_NOOP_COPY) {
23807                 omit_copy = 0;
23808         }
23809         if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
23810                 src = RHS(ins, 0);
23811                 dst = ins;
23812         }
23813         else {
23814                 internal_error(state, ins, "unknown move operation");
23815                 src = dst = 0;
23816         }
23817         if (reg_size(state, dst) < size_of(state, dst->type)) {
23818                 internal_error(state, ins, "Invalid destination register");
23819         }
23820         if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
23821                 fprintf(state->errout, "src type: ");
23822                 name_of(state->errout, src->type);
23823                 fprintf(state->errout, "\n");
23824                 fprintf(state->errout, "dst type: ");
23825                 name_of(state->errout, dst->type);
23826                 fprintf(state->errout, "\n");
23827                 internal_error(state, ins, "Type mismatch for OP_COPY");
23828         }
23829
23830         if (!is_const(src)) {
23831                 int src_reg, dst_reg;
23832                 int src_regcm, dst_regcm;
23833                 src_reg   = ID_REG(src->id);
23834                 dst_reg   = ID_REG(dst->id);
23835                 src_regcm = arch_reg_regcm(state, src_reg);
23836                 dst_regcm = arch_reg_regcm(state, dst_reg);
23837                 /* If the class is the same just move the register */
23838                 if (src_regcm & dst_regcm & 
23839                         (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
23840                         if ((src_reg != dst_reg) || !omit_copy) {
23841                                 fprintf(fp, "\tmov %s, %s\n",
23842                                         reg(state, src, src_regcm),
23843                                         reg(state, dst, dst_regcm));
23844                         }
23845                 }
23846                 /* Move 32bit to 16bit */
23847                 else if ((src_regcm & REGCM_GPR32) &&
23848                         (dst_regcm & REGCM_GPR16)) {
23849                         src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
23850                         if ((src_reg != dst_reg) || !omit_copy) {
23851                                 fprintf(fp, "\tmovw %s, %s\n",
23852                                         arch_reg_str(src_reg), 
23853                                         arch_reg_str(dst_reg));
23854                         }
23855                 }
23856                 /* Move from 32bit gprs to 16bit gprs */
23857                 else if ((src_regcm & REGCM_GPR32) &&
23858                         (dst_regcm & REGCM_GPR16)) {
23859                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23860                         if ((src_reg != dst_reg) || !omit_copy) {
23861                                 fprintf(fp, "\tmov %s, %s\n",
23862                                         arch_reg_str(src_reg),
23863                                         arch_reg_str(dst_reg));
23864                         }
23865                 }
23866                 /* Move 32bit to 8bit */
23867                 else if ((src_regcm & REGCM_GPR32_8) &&
23868                         (dst_regcm & REGCM_GPR8_LO))
23869                 {
23870                         src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
23871                         if ((src_reg != dst_reg) || !omit_copy) {
23872                                 fprintf(fp, "\tmovb %s, %s\n",
23873                                         arch_reg_str(src_reg),
23874                                         arch_reg_str(dst_reg));
23875                         }
23876                 }
23877                 /* Move 16bit to 8bit */
23878                 else if ((src_regcm & REGCM_GPR16_8) &&
23879                         (dst_regcm & REGCM_GPR8_LO))
23880                 {
23881                         src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
23882                         if ((src_reg != dst_reg) || !omit_copy) {
23883                                 fprintf(fp, "\tmovb %s, %s\n",
23884                                         arch_reg_str(src_reg),
23885                                         arch_reg_str(dst_reg));
23886                         }
23887                 }
23888                 /* Move 8/16bit to 16/32bit */
23889                 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) && 
23890                         (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
23891                         const char *op;
23892                         op = is_signed(src->type)? "movsx": "movzx";
23893                         fprintf(fp, "\t%s %s, %s\n",
23894                                 op,
23895                                 reg(state, src, src_regcm),
23896                                 reg(state, dst, dst_regcm));
23897                 }
23898                 /* Move between sse registers */
23899                 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
23900                         if ((src_reg != dst_reg) || !omit_copy) {
23901                                 fprintf(fp, "\tmovdqa %s, %s\n",
23902                                         reg(state, src, src_regcm),
23903                                         reg(state, dst, dst_regcm));
23904                         }
23905                 }
23906                 /* Move between mmx registers */
23907                 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
23908                         if ((src_reg != dst_reg) || !omit_copy) {
23909                                 fprintf(fp, "\tmovq %s, %s\n",
23910                                         reg(state, src, src_regcm),
23911                                         reg(state, dst, dst_regcm));
23912                         }
23913                 }
23914                 /* Move from sse to mmx registers */
23915                 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
23916                         fprintf(fp, "\tmovdq2q %s, %s\n",
23917                                 reg(state, src, src_regcm),
23918                                 reg(state, dst, dst_regcm));
23919                 }
23920                 /* Move from mmx to sse registers */
23921                 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
23922                         fprintf(fp, "\tmovq2dq %s, %s\n",
23923                                 reg(state, src, src_regcm),
23924                                 reg(state, dst, dst_regcm));
23925                 }
23926                 /* Move between 32bit gprs & mmx/sse registers */
23927                 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
23928                         (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
23929                         fprintf(fp, "\tmovd %s, %s\n",
23930                                 reg(state, src, src_regcm),
23931                                 reg(state, dst, dst_regcm));
23932                 }
23933                 /* Move from 16bit gprs &  mmx/sse registers */
23934                 else if ((src_regcm & REGCM_GPR16) &&
23935                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
23936                         const char *op;
23937                         int mid_reg;
23938                         op = is_signed(src->type)? "movsx":"movzx";
23939                         mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23940                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
23941                                 op,
23942                                 arch_reg_str(src_reg),
23943                                 arch_reg_str(mid_reg),
23944                                 arch_reg_str(mid_reg),
23945                                 arch_reg_str(dst_reg));
23946                 }
23947                 /* Move from mmx/sse registers to 16bit gprs */
23948                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
23949                         (dst_regcm & REGCM_GPR16)) {
23950                         dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23951                         fprintf(fp, "\tmovd %s, %s\n",
23952                                 arch_reg_str(src_reg),
23953                                 arch_reg_str(dst_reg));
23954                 }
23955                 /* Move from gpr to 64bit dividend */
23956                 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))  &&
23957                         (dst_regcm & REGCM_DIVIDEND64)) {
23958                         const char *extend;
23959                         extend = is_signed(src->type)? "cltd":"movl $0, %edx";
23960                         fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
23961                                 arch_reg_str(src_reg), 
23962                                 extend);
23963                 }
23964                 /* Move from 64bit gpr to gpr */
23965                 else if ((src_regcm & REGCM_DIVIDEND64) &&
23966                         (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
23967                         if (dst_regcm & REGCM_GPR32) {
23968                                 src_reg = REG_EAX;
23969                         } 
23970                         else if (dst_regcm & REGCM_GPR16) {
23971                                 src_reg = REG_AX;
23972                         }
23973                         else if (dst_regcm & REGCM_GPR8_LO) {
23974                                 src_reg = REG_AL;
23975                         }
23976                         fprintf(fp, "\tmov %s, %s\n",
23977                                 arch_reg_str(src_reg),
23978                                 arch_reg_str(dst_reg));
23979                 }
23980                 /* Move from mmx/sse registers to 64bit gpr */
23981                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
23982                         (dst_regcm & REGCM_DIVIDEND64)) {
23983                         const char *extend;
23984                         extend = is_signed(src->type)? "cltd": "movl $0, %edx";
23985                         fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
23986                                 arch_reg_str(src_reg),
23987                                 extend);
23988                 }
23989                 /* Move from 64bit gpr to mmx/sse register */
23990                 else if ((src_regcm & REGCM_DIVIDEND64) &&
23991                         (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
23992                         fprintf(fp, "\tmovd %%eax, %s\n",
23993                                 arch_reg_str(dst_reg));
23994                 }
23995 #if X86_4_8BIT_GPRS
23996                 /* Move from 8bit gprs to  mmx/sse registers */
23997                 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
23998                         (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
23999                         const char *op;
24000                         int mid_reg;
24001                         op = is_signed(src->type)? "movsx":"movzx";
24002                         mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24003                         fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24004                                 op,
24005                                 reg(state, src, src_regcm),
24006                                 arch_reg_str(mid_reg),
24007                                 arch_reg_str(mid_reg),
24008                                 reg(state, dst, dst_regcm));
24009                 }
24010                 /* Move from mmx/sse registers and 8bit gprs */
24011                 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24012                         (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
24013                         int mid_reg;
24014                         mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24015                         fprintf(fp, "\tmovd %s, %s\n",
24016                                 reg(state, src, src_regcm),
24017                                 arch_reg_str(mid_reg));
24018                 }
24019                 /* Move from 32bit gprs to 8bit gprs */
24020                 else if ((src_regcm & REGCM_GPR32) &&
24021                         (dst_regcm & REGCM_GPR8_LO)) {
24022                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24023                         if ((src_reg != dst_reg) || !omit_copy) {
24024                                 fprintf(fp, "\tmov %s, %s\n",
24025                                         arch_reg_str(src_reg),
24026                                         arch_reg_str(dst_reg));
24027                         }
24028                 }
24029                 /* Move from 16bit gprs to 8bit gprs */
24030                 else if ((src_regcm & REGCM_GPR16) &&
24031                         (dst_regcm & REGCM_GPR8_LO)) {
24032                         dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24033                         if ((src_reg != dst_reg) || !omit_copy) {
24034                                 fprintf(fp, "\tmov %s, %s\n",
24035                                         arch_reg_str(src_reg),
24036                                         arch_reg_str(dst_reg));
24037                         }
24038                 }
24039 #endif /* X86_4_8BIT_GPRS */
24040                 /* Move from %eax:%edx to %eax:%edx */
24041                 else if ((src_regcm & REGCM_DIVIDEND64) &&
24042                         (dst_regcm & REGCM_DIVIDEND64) &&
24043                         (src_reg == dst_reg)) {
24044                         if (!omit_copy) {
24045                                 fprintf(fp, "\t/*mov %s, %s*/\n",
24046                                         arch_reg_str(src_reg),
24047                                         arch_reg_str(dst_reg));
24048                         }
24049                 }
24050                 else {
24051                         if ((src_regcm & ~REGCM_FLAGS) == 0) {
24052                                 internal_error(state, ins, "attempt to copy from %%eflags!");
24053                         }
24054                         internal_error(state, ins, "unknown copy type");
24055                 }
24056         }
24057         else {
24058                 size_t dst_size;
24059                 int dst_reg;
24060                 int dst_regcm;
24061                 dst_size = size_of(state, dst->type);
24062                 dst_reg = ID_REG(dst->id);
24063                 dst_regcm = arch_reg_regcm(state, dst_reg);
24064                 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24065                         fprintf(fp, "\tmov ");
24066                         print_const_val(state, src, fp);
24067                         fprintf(fp, ", %s\n",
24068                                 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24069                 }
24070                 else if (dst_regcm & REGCM_DIVIDEND64) {
24071                         if (dst_size > SIZEOF_I32) {
24072                                 internal_error(state, ins, "%dbit constant...", dst_size);
24073                         }
24074                         fprintf(fp, "\tmov $0, %%edx\n");
24075                         fprintf(fp, "\tmov ");
24076                         print_const_val(state, src, fp);
24077                         fprintf(fp, ", %%eax\n");
24078                 }
24079                 else if (dst_regcm & REGCM_DIVIDEND32) {
24080                         if (dst_size > SIZEOF_I16) {
24081                                 internal_error(state, ins, "%dbit constant...", dst_size);
24082                         }
24083                         fprintf(fp, "\tmov $0, %%dx\n");
24084                         fprintf(fp, "\tmov ");
24085                         print_const_val(state, src, fp);
24086                         fprintf(fp, ", %%ax");
24087                 }
24088                 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24089                         long ref;
24090                         if (dst_size > SIZEOF_I32) {
24091                                 internal_error(state, ins, "%d bit constant...", dst_size);
24092                         }
24093                         ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
24094                         fprintf(fp, "\tmovd L%s%lu, %s\n",
24095                                 state->compiler->label_prefix, ref,
24096                                 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24097                 }
24098                 else {
24099                         internal_error(state, ins, "unknown copy immediate type");
24100                 }
24101         }
24102         /* Leave now if this is not a type conversion */
24103         if (ins->op != OP_CONVERT) {
24104                 return;
24105         }
24106         /* Now make certain I have not logically overflowed the destination */
24107         if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24108                 (size_of(state, dst->type) < reg_size(state, dst)))
24109         {
24110                 unsigned long mask;
24111                 int dst_reg;
24112                 int dst_regcm;
24113                 if (size_of(state, dst->type) >= 32) {
24114                         fprintf(state->errout, "dst type: ");
24115                         name_of(state->errout, dst->type);
24116                         fprintf(state->errout, "\n");
24117                         internal_error(state, dst, "unhandled dst type size");
24118                 }
24119                 mask = 1;
24120                 mask <<= size_of(state, dst->type);
24121                 mask -= 1;
24122
24123                 dst_reg = ID_REG(dst->id);
24124                 dst_regcm = arch_reg_regcm(state, dst_reg);
24125
24126                 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24127                         fprintf(fp, "\tand $0x%lx, %s\n",
24128                                 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24129                 }
24130                 else if (dst_regcm & REGCM_MMX) {
24131                         long ref;
24132                         ref = get_mask_pool_ref(state, dst, mask, fp);
24133                         fprintf(fp, "\tpand L%s%lu, %s\n",
24134                                 state->compiler->label_prefix, ref,
24135                                 reg(state, dst, REGCM_MMX));
24136                 }
24137                 else if (dst_regcm & REGCM_XMM) {
24138                         long ref;
24139                         ref = get_mask_pool_ref(state, dst, mask, fp);
24140                         fprintf(fp, "\tpand L%s%lu, %s\n",
24141                                 state->compiler->label_prefix, ref,
24142                                 reg(state, dst, REGCM_XMM));
24143                 }
24144                 else {
24145                         fprintf(state->errout, "dst type: ");
24146                         name_of(state->errout, dst->type);
24147                         fprintf(state->errout, "\n");
24148                         fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24149                         internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24150                 }
24151         }
24152         /* Make certain I am properly sign extended */
24153         if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24154                 (is_signed(src->type)))
24155         {
24156                 int bits, reg_bits, shift_bits;
24157                 int dst_reg;
24158                 int dst_regcm;
24159
24160                 bits = size_of(state, src->type);
24161                 reg_bits = reg_size(state, dst);
24162                 if (reg_bits > 32) {
24163                         reg_bits = 32;
24164                 }
24165                 shift_bits = reg_bits - size_of(state, src->type);
24166                 dst_reg = ID_REG(dst->id);
24167                 dst_regcm = arch_reg_regcm(state, dst_reg);
24168
24169                 if (shift_bits < 0) {
24170                         internal_error(state, dst, "negative shift?");
24171                 }
24172
24173                 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24174                         fprintf(fp, "\tshl $%d, %s\n", 
24175                                 shift_bits, 
24176                                 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24177                         fprintf(fp, "\tsar $%d, %s\n", 
24178                                 shift_bits, 
24179                                 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24180                 }
24181                 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24182                         fprintf(fp, "\tpslld $%d, %s\n",
24183                                 shift_bits, 
24184                                 reg(state, dst, REGCM_MMX | REGCM_XMM));
24185                         fprintf(fp, "\tpsrad $%d, %s\n",
24186                                 shift_bits, 
24187                                 reg(state, dst, REGCM_MMX | REGCM_XMM));
24188                 }
24189                 else {
24190                         fprintf(state->errout, "dst type: ");
24191                         name_of(state->errout, dst->type);
24192                         fprintf(state->errout, "\n");
24193                         fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24194                         internal_error(state, dst, "failed to signed extend value");
24195                 }
24196         }
24197 }
24198
24199 static void print_op_load(struct compile_state *state,
24200         struct triple *ins, FILE *fp)
24201 {
24202         struct triple *dst, *src;
24203         const char *op;
24204         dst = ins;
24205         src = RHS(ins, 0);
24206         if (is_const(src) || is_const(dst)) {
24207                 internal_error(state, ins, "unknown load operation");
24208         }
24209         switch(ins->type->type & TYPE_MASK) {
24210         case TYPE_CHAR:   op = "movsbl"; break;
24211         case TYPE_UCHAR:  op = "movzbl"; break;
24212         case TYPE_SHORT:  op = "movswl"; break;
24213         case TYPE_USHORT: op = "movzwl"; break;
24214         case TYPE_INT:    case TYPE_UINT:
24215         case TYPE_LONG:   case TYPE_ULONG:
24216         case TYPE_POINTER:
24217                 op = "movl"; 
24218                 break;
24219         default:
24220                 internal_error(state, ins, "unknown type in load");
24221                 op = "<invalid opcode>";
24222                 break;
24223         }
24224         fprintf(fp, "\t%s (%s), %s\n",
24225                 op, 
24226                 reg(state, src, REGCM_GPR32),
24227                 reg(state, dst, REGCM_GPR32));
24228 }
24229
24230
24231 static void print_op_store(struct compile_state *state,
24232         struct triple *ins, FILE *fp)
24233 {
24234         struct triple *dst, *src;
24235         dst = RHS(ins, 0);
24236         src = RHS(ins, 1);
24237         if (is_const(src) && (src->op == OP_INTCONST)) {
24238                 long_t value;
24239                 value = (long_t)(src->u.cval);
24240                 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24241                         type_suffix(state, src->type),
24242                         (long)(value),
24243                         reg(state, dst, REGCM_GPR32));
24244         }
24245         else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24246                 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24247                         type_suffix(state, src->type),
24248                         reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
24249                         (unsigned long)(dst->u.cval));
24250         }
24251         else {
24252                 if (is_const(src) || is_const(dst)) {
24253                         internal_error(state, ins, "unknown store operation");
24254                 }
24255                 fprintf(fp, "\tmov%s %s, (%s)\n",
24256                         type_suffix(state, src->type),
24257                         reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
24258                         reg(state, dst, REGCM_GPR32));
24259         }
24260         
24261         
24262 }
24263
24264 static void print_op_smul(struct compile_state *state,
24265         struct triple *ins, FILE *fp)
24266 {
24267         if (!is_const(RHS(ins, 1))) {
24268                 fprintf(fp, "\timul %s, %s\n",
24269                         reg(state, RHS(ins, 1), REGCM_GPR32),
24270                         reg(state, RHS(ins, 0), REGCM_GPR32));
24271         }
24272         else {
24273                 fprintf(fp, "\timul ");
24274                 print_const_val(state, RHS(ins, 1), fp);
24275                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
24276         }
24277 }
24278
24279 static void print_op_cmp(struct compile_state *state,
24280         struct triple *ins, FILE *fp)
24281 {
24282         unsigned mask;
24283         int dreg;
24284         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
24285         dreg = check_reg(state, ins, REGCM_FLAGS);
24286         if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24287                 internal_error(state, ins, "bad dest register for cmp");
24288         }
24289         if (is_const(RHS(ins, 1))) {
24290                 fprintf(fp, "\tcmp ");
24291                 print_const_val(state, RHS(ins, 1), fp);
24292                 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
24293         }
24294         else {
24295                 unsigned lmask, rmask;
24296                 int lreg, rreg;
24297                 lreg = check_reg(state, RHS(ins, 0), mask);
24298                 rreg = check_reg(state, RHS(ins, 1), mask);
24299                 lmask = arch_reg_regcm(state, lreg);
24300                 rmask = arch_reg_regcm(state, rreg);
24301                 mask = lmask & rmask;
24302                 fprintf(fp, "\tcmp %s, %s\n",
24303                         reg(state, RHS(ins, 1), mask),
24304                         reg(state, RHS(ins, 0), mask));
24305         }
24306 }
24307
24308 static void print_op_test(struct compile_state *state,
24309         struct triple *ins, FILE *fp)
24310 {
24311         unsigned mask;
24312         mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
24313         fprintf(fp, "\ttest %s, %s\n",
24314                 reg(state, RHS(ins, 0), mask),
24315                 reg(state, RHS(ins, 0), mask));
24316 }
24317
24318 static void print_op_branch(struct compile_state *state,
24319         struct triple *branch, FILE *fp)
24320 {
24321         const char *bop = "j";
24322         if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
24323                 if (branch->rhs != 0) {
24324                         internal_error(state, branch, "jmp with condition?");
24325                 }
24326                 bop = "jmp";
24327         }
24328         else {
24329                 struct triple *ptr;
24330                 if (branch->rhs != 1) {
24331                         internal_error(state, branch, "jmpcc without condition?");
24332                 }
24333                 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24334                 if ((RHS(branch, 0)->op != OP_CMP) &&
24335                         (RHS(branch, 0)->op != OP_TEST)) {
24336                         internal_error(state, branch, "bad branch test");
24337                 }
24338 #warning "FIXME I have observed instructions between the test and branch instructions"
24339                 ptr = RHS(branch, 0);
24340                 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24341                         if (ptr->op != OP_COPY) {
24342                                 internal_error(state, branch, "branch does not follow test");
24343                         }
24344                 }
24345                 switch(branch->op) {
24346                 case OP_JMP_EQ:       bop = "jz";  break;
24347                 case OP_JMP_NOTEQ:    bop = "jnz"; break;
24348                 case OP_JMP_SLESS:    bop = "jl";  break;
24349                 case OP_JMP_ULESS:    bop = "jb";  break;
24350                 case OP_JMP_SMORE:    bop = "jg";  break;
24351                 case OP_JMP_UMORE:    bop = "ja";  break;
24352                 case OP_JMP_SLESSEQ:  bop = "jle"; break;
24353                 case OP_JMP_ULESSEQ:  bop = "jbe"; break;
24354                 case OP_JMP_SMOREEQ:  bop = "jge"; break;
24355                 case OP_JMP_UMOREEQ:  bop = "jae"; break;
24356                 default:
24357                         internal_error(state, branch, "Invalid branch op");
24358                         break;
24359                 }
24360                 
24361         }
24362 #if 1
24363         if (branch->op == OP_CALL) {
24364                 fprintf(fp, "\t/* call */\n");
24365         }
24366 #endif
24367         fprintf(fp, "\t%s L%s%lu\n",
24368                 bop, 
24369                 state->compiler->label_prefix,
24370                 (unsigned long)(TARG(branch, 0)->u.cval));
24371 }
24372
24373 static void print_op_ret(struct compile_state *state,
24374         struct triple *branch, FILE *fp)
24375 {
24376         fprintf(fp, "\tjmp *%s\n",
24377                 reg(state, RHS(branch, 0), REGCM_GPR32));
24378 }
24379
24380 static void print_op_set(struct compile_state *state,
24381         struct triple *set, FILE *fp)
24382 {
24383         const char *sop = "set";
24384         if (set->rhs != 1) {
24385                 internal_error(state, set, "setcc without condition?");
24386         }
24387         check_reg(state, RHS(set, 0), REGCM_FLAGS);
24388         if ((RHS(set, 0)->op != OP_CMP) &&
24389                 (RHS(set, 0)->op != OP_TEST)) {
24390                 internal_error(state, set, "bad set test");
24391         }
24392         if (RHS(set, 0)->next != set) {
24393                 internal_error(state, set, "set does not follow test");
24394         }
24395         switch(set->op) {
24396         case OP_SET_EQ:       sop = "setz";  break;
24397         case OP_SET_NOTEQ:    sop = "setnz"; break;
24398         case OP_SET_SLESS:    sop = "setl";  break;
24399         case OP_SET_ULESS:    sop = "setb";  break;
24400         case OP_SET_SMORE:    sop = "setg";  break;
24401         case OP_SET_UMORE:    sop = "seta";  break;
24402         case OP_SET_SLESSEQ:  sop = "setle"; break;
24403         case OP_SET_ULESSEQ:  sop = "setbe"; break;
24404         case OP_SET_SMOREEQ:  sop = "setge"; break;
24405         case OP_SET_UMOREEQ:  sop = "setae"; break;
24406         default:
24407                 internal_error(state, set, "Invalid set op");
24408                 break;
24409         }
24410         fprintf(fp, "\t%s %s\n",
24411                 sop, reg(state, set, REGCM_GPR8_LO));
24412 }
24413
24414 static void print_op_bit_scan(struct compile_state *state, 
24415         struct triple *ins, FILE *fp) 
24416 {
24417         const char *op;
24418         switch(ins->op) {
24419         case OP_BSF: op = "bsf"; break;
24420         case OP_BSR: op = "bsr"; break;
24421         default: 
24422                 internal_error(state, ins, "unknown bit scan");
24423                 op = 0;
24424                 break;
24425         }
24426         fprintf(fp, 
24427                 "\t%s %s, %s\n"
24428                 "\tjnz 1f\n"
24429                 "\tmovl $-1, %s\n"
24430                 "1:\n",
24431                 op,
24432                 reg(state, RHS(ins, 0), REGCM_GPR32),
24433                 reg(state, ins, REGCM_GPR32),
24434                 reg(state, ins, REGCM_GPR32));
24435 }
24436
24437
24438 static void print_sdecl(struct compile_state *state,
24439         struct triple *ins, FILE *fp)
24440 {
24441         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24442         fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
24443         fprintf(fp, "L%s%lu:\n", 
24444                 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
24445         print_const(state, MISC(ins, 0), fp);
24446         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
24447                 
24448 }
24449
24450 static void print_instruction(struct compile_state *state,
24451         struct triple *ins, FILE *fp)
24452 {
24453         /* Assumption: after I have exted the register allocator
24454          * everything is in a valid register. 
24455          */
24456         switch(ins->op) {
24457         case OP_ASM:
24458                 print_op_asm(state, ins, fp);
24459                 break;
24460         case OP_ADD:    print_binary_op(state, "add", ins, fp); break;
24461         case OP_SUB:    print_binary_op(state, "sub", ins, fp); break;
24462         case OP_AND:    print_binary_op(state, "and", ins, fp); break;
24463         case OP_XOR:    print_binary_op(state, "xor", ins, fp); break;
24464         case OP_OR:     print_binary_op(state, "or",  ins, fp); break;
24465         case OP_SL:     print_op_shift(state, "shl", ins, fp); break;
24466         case OP_USR:    print_op_shift(state, "shr", ins, fp); break;
24467         case OP_SSR:    print_op_shift(state, "sar", ins, fp); break;
24468         case OP_POS:    break;
24469         case OP_NEG:    print_unary_op(state, "neg", ins, fp); break;
24470         case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
24471         case OP_NOOP:
24472         case OP_INTCONST:
24473         case OP_ADDRCONST:
24474         case OP_BLOBCONST:
24475                 /* Don't generate anything here for constants */
24476         case OP_PHI:
24477                 /* Don't generate anything for variable declarations. */
24478                 break;
24479         case OP_UNKNOWNVAL:
24480                 fprintf(fp, " /* unknown %s */\n",
24481                         reg(state, ins, REGCM_ALL));
24482                 break;
24483         case OP_SDECL:
24484                 print_sdecl(state, ins, fp);
24485                 break;
24486         case OP_COPY:   
24487         case OP_CONVERT:
24488                 print_op_move(state, ins, fp);
24489                 break;
24490         case OP_LOAD:
24491                 print_op_load(state, ins, fp);
24492                 break;
24493         case OP_STORE:
24494                 print_op_store(state, ins, fp);
24495                 break;
24496         case OP_SMUL:
24497                 print_op_smul(state, ins, fp);
24498                 break;
24499         case OP_CMP:    print_op_cmp(state, ins, fp); break;
24500         case OP_TEST:   print_op_test(state, ins, fp); break;
24501         case OP_JMP:
24502         case OP_JMP_EQ:      case OP_JMP_NOTEQ:
24503         case OP_JMP_SLESS:   case OP_JMP_ULESS:
24504         case OP_JMP_SMORE:   case OP_JMP_UMORE:
24505         case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24506         case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
24507         case OP_CALL:
24508                 print_op_branch(state, ins, fp);
24509                 break;
24510         case OP_RET:
24511                 print_op_ret(state, ins, fp);
24512                 break;
24513         case OP_SET_EQ:      case OP_SET_NOTEQ:
24514         case OP_SET_SLESS:   case OP_SET_ULESS:
24515         case OP_SET_SMORE:   case OP_SET_UMORE:
24516         case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24517         case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24518                 print_op_set(state, ins, fp);
24519                 break;
24520         case OP_INB:  case OP_INW:  case OP_INL:
24521                 print_op_in(state, ins, fp); 
24522                 break;
24523         case OP_OUTB: case OP_OUTW: case OP_OUTL:
24524                 print_op_out(state, ins, fp); 
24525                 break;
24526         case OP_BSF:
24527         case OP_BSR:
24528                 print_op_bit_scan(state, ins, fp);
24529                 break;
24530         case OP_RDMSR:
24531                 after_lhs(state, ins);
24532                 fprintf(fp, "\trdmsr\n");
24533                 break;
24534         case OP_WRMSR:
24535                 fprintf(fp, "\twrmsr\n");
24536                 break;
24537         case OP_HLT:
24538                 fprintf(fp, "\thlt\n");
24539                 break;
24540         case OP_SDIVT:
24541                 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24542                 break;
24543         case OP_UDIVT:
24544                 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24545                 break;
24546         case OP_UMUL:
24547                 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24548                 break;
24549         case OP_LABEL:
24550                 if (!ins->use) {
24551                         return;
24552                 }
24553                 fprintf(fp, "L%s%lu:\n", 
24554                         state->compiler->label_prefix, (unsigned long)(ins->u.cval));
24555                 break;
24556         case OP_ADECL:
24557                 /* Ignore adecls with no registers error otherwise */
24558                 if (!noop_adecl(ins)) {
24559                         internal_error(state, ins, "adecl remains?");
24560                 }
24561                 break;
24562                 /* Ignore OP_PIECE */
24563         case OP_PIECE:
24564                 break;
24565                 /* Operations that should never get here */
24566         case OP_SDIV: case OP_UDIV:
24567         case OP_SMOD: case OP_UMOD:
24568         case OP_LTRUE:   case OP_LFALSE:  case OP_EQ:      case OP_NOTEQ:
24569         case OP_SLESS:   case OP_ULESS:   case OP_SMORE:   case OP_UMORE:
24570         case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24571         default:
24572                 internal_error(state, ins, "unknown op: %d %s",
24573                         ins->op, tops(ins->op));
24574                 break;
24575         }
24576 }
24577
24578 static void print_instructions(struct compile_state *state)
24579 {
24580         struct triple *first, *ins;
24581         int print_location;
24582         struct occurance *last_occurance;
24583         FILE *fp;
24584         int max_inline_depth;
24585         max_inline_depth = 0;
24586         print_location = 1;
24587         last_occurance = 0;
24588         fp = state->output;
24589         /* Masks for common sizes */
24590         fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24591         fprintf(fp, ".balign 16\n");
24592         fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24593         fprintf(fp, ".int 0xff, 0, 0, 0\n");
24594         fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24595         fprintf(fp, ".int 0xffff, 0, 0, 0\n");
24596         fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
24597         first = state->first;
24598         ins = first;
24599         do {
24600                 if (print_location && 
24601                         last_occurance != ins->occurance) {
24602                         if (!ins->occurance->parent) {
24603                                 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
24604                                         ins->occurance->function,
24605                                         ins->occurance->filename,
24606                                         ins->occurance->line,
24607                                         ins->occurance->col);
24608                         }
24609                         else {
24610                                 struct occurance *ptr;
24611                                 int inline_depth;
24612                                 fprintf(fp, "\t/*\n");
24613                                 inline_depth = 0;
24614                                 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
24615                                         inline_depth++;
24616                                         fprintf(fp, "\t * %s,%s:%d.%d\n",
24617                                                 ptr->function,
24618                                                 ptr->filename,
24619                                                 ptr->line,
24620                                                 ptr->col);
24621                                 }
24622                                 fprintf(fp, "\t */\n");
24623                                 if (inline_depth > max_inline_depth) {
24624                                         max_inline_depth = inline_depth;
24625                                 }
24626                         }
24627                         if (last_occurance) {
24628                                 put_occurance(last_occurance);
24629                         }
24630                         get_occurance(ins->occurance);
24631                         last_occurance = ins->occurance;
24632                 }
24633
24634                 print_instruction(state, ins, fp);
24635                 ins = ins->next;
24636         } while(ins != first);
24637         if (print_location) {
24638                 fprintf(fp, "/* max inline depth %d */\n",
24639                         max_inline_depth);
24640         }
24641 }
24642
24643 static void generate_code(struct compile_state *state)
24644 {
24645         generate_local_labels(state);
24646         print_instructions(state);
24647         
24648 }
24649
24650 static void print_preprocessed_tokens(struct compile_state *state)
24651 {
24652         int tok;
24653         FILE *fp;
24654         int line;
24655         const char *filename;
24656         fp = state->output;
24657         filename = 0;
24658         line = 0;
24659         for(;;) {
24660                 struct token *tk;
24661                 const char *token_str;
24662                 tok = peek(state);
24663                 if (tok == TOK_EOF) {
24664                         break;
24665                 }
24666                 tk = eat(state, tok);
24667                 token_str = 
24668                         tk->ident ? tk->ident->name :
24669                         tk->str_len ? tk->val.str :
24670                         tokens[tk->tok];
24671                 
24672                 if ((state->file->line != line) || 
24673                         (state->file->basename != filename)) {
24674                         int i, col;
24675                         if ((state->file->basename == filename) &&
24676                                 (line < state->file->line)) {
24677                                 while(line < state->file->line) {
24678                                         fprintf(fp, "\n");
24679                                         line++;
24680                                 }
24681                         }
24682                         else {
24683                                 fprintf(fp, "\n#line %d \"%s\"\n",
24684                                         state->file->line, state->file->basename);
24685                         }
24686                         line = state->file->line;
24687                         filename = state->file->basename;
24688                         col = get_col(state->file) - strlen(token_str);
24689                         for(i = 0; i < col; i++) {
24690                                 fprintf(fp, " ");
24691                         }
24692                 }
24693                 
24694                 fprintf(fp, "%s ", token_str);
24695                 
24696                 if (state->compiler->debug & DEBUG_TOKENS) {
24697                         loc(state->dbgout, state, 0);
24698                         fprintf(state->dbgout, "%s <- `%s'\n",
24699                                 tokens[tok], token_str);
24700                 }
24701         }
24702 }
24703
24704 static void compile(const char *filename, 
24705         struct compiler_state *compiler, struct arch_state *arch)
24706 {
24707         int i;
24708         struct compile_state state;
24709         struct triple *ptr;
24710         memset(&state, 0, sizeof(state));
24711         state.compiler = compiler;
24712         state.arch     = arch;
24713         state.file = 0;
24714         for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24715                 memset(&state.token[i], 0, sizeof(state.token[i]));
24716                 state.token[i].tok = -1;
24717         }
24718         /* Remember the output descriptors */
24719         state.errout = stderr;
24720         state.dbgout = stdout;
24721         /* Remember the output filename */
24722         state.output    = fopen(state.compiler->ofilename, "w");
24723         if (!state.output) {
24724                 error(&state, 0, "Cannot open output file %s\n",
24725                         state.compiler->ofilename);
24726         }
24727         /* Make certain a good cleanup happens */
24728         exit_state = &state;
24729         atexit(exit_cleanup);
24730
24731         /* Prep the preprocessor */
24732         state.if_depth = 0;
24733         memset(state.if_bytes, 0, sizeof(state.if_bytes));
24734         /* register the C keywords */
24735         register_keywords(&state);
24736         /* register the keywords the macro preprocessor knows */
24737         register_macro_keywords(&state);
24738         /* generate some builtin macros */
24739         register_builtin_macros(&state);
24740         /* Memorize where some special keywords are. */
24741         state.i_switch        = lookup(&state, "switch", 6);
24742         state.i_case          = lookup(&state, "case", 4);
24743         state.i_continue      = lookup(&state, "continue", 8);
24744         state.i_break         = lookup(&state, "break", 5);
24745         state.i_default       = lookup(&state, "default", 7);
24746         state.i_return        = lookup(&state, "return", 6);
24747         /* Memorize where predefined macros are. */
24748         state.i___VA_ARGS__   = lookup(&state, "__VA_ARGS__", 11);
24749         state.i___FILE__      = lookup(&state, "__FILE__", 8);
24750         state.i___LINE__      = lookup(&state, "__LINE__", 8);
24751         /* Memorize where predefined identifiers are. */
24752         state.i___func__      = lookup(&state, "__func__", 8);
24753         /* Memorize where some attribute keywords are. */
24754         state.i_noinline      = lookup(&state, "noinline", 8);
24755         state.i_always_inline = lookup(&state, "always_inline", 13);
24756
24757         /* Process the command line macros */
24758         process_cmdline_macros(&state);
24759
24760         /* Allocate beginning bounding labels for the function list */
24761         state.first = label(&state);
24762         state.first->id |= TRIPLE_FLAG_VOLATILE;
24763         use_triple(state.first, state.first);
24764         ptr = label(&state);
24765         ptr->id |= TRIPLE_FLAG_VOLATILE;
24766         use_triple(ptr, ptr);
24767         flatten(&state, state.first, ptr);
24768
24769         /* Allocate a label for the pool of global variables */
24770         state.global_pool = label(&state);
24771         state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
24772         flatten(&state, state.first, state.global_pool);
24773
24774         /* Enter the globl definition scope */
24775         start_scope(&state);
24776         register_builtins(&state);
24777         compile_file(&state, filename, 1);
24778
24779         /* Stop if all we want is preprocessor output */
24780         if (state.compiler->flags & COMPILER_CPP_ONLY) {
24781                 print_preprocessed_tokens(&state);
24782                 return;
24783         }
24784
24785         decls(&state);
24786
24787         /* Exit the global definition scope */
24788         end_scope(&state);
24789
24790         /* Now that basic compilation has happened 
24791          * optimize the intermediate code 
24792          */
24793         optimize(&state);
24794
24795         generate_code(&state);
24796         if (state.compiler->debug) {
24797                 fprintf(state.errout, "done\n");
24798         }
24799         exit_state = 0;
24800 }
24801
24802 static void version(FILE *fp)
24803 {
24804         fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
24805 }
24806
24807 static void usage(void)
24808 {
24809         FILE *fp = stdout;
24810         version(fp);
24811         fprintf(fp,
24812                 "\nUsage: romcc [options] <source>.c\n"
24813                 "Compile a C source file generating a binary that does not implicilty use RAM\n"
24814                 "Options: \n"
24815                 "-o <output file name>\n"
24816                 "-f<option>            Specify a generic compiler option\n"
24817                 "-m<option>            Specify a arch dependent option\n"
24818                 "--                    Specify this is the last option\n"
24819                 "\nGeneric compiler options:\n"
24820         );
24821         compiler_usage(fp);
24822         fprintf(fp,
24823                 "\nArchitecture compiler options:\n"
24824         );
24825         arch_usage(fp);
24826         fprintf(fp,
24827                 "\n"
24828         );
24829 }
24830
24831 static void arg_error(char *fmt, ...)
24832 {
24833         va_list args;
24834         va_start(args, fmt);
24835         vfprintf(stderr, fmt, args);
24836         va_end(args);
24837         usage();
24838         exit(1);
24839 }
24840
24841 int main(int argc, char **argv)
24842 {
24843         const char *filename;
24844         struct compiler_state compiler;
24845         struct arch_state arch;
24846         int all_opts;
24847         
24848         
24849         /* I don't want any surprises */
24850         setlocale(LC_ALL, "C");
24851
24852         init_compiler_state(&compiler);
24853         init_arch_state(&arch);
24854         filename = 0;
24855         all_opts = 0;
24856         while(argc > 1) {
24857                 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
24858                         compiler.ofilename = argv[2];
24859                         argv += 2;
24860                         argc -= 2;
24861                 }
24862                 else if (!all_opts && argv[1][0] == '-') {
24863                         int result;
24864                         result = -1;
24865                         if (strcmp(argv[1], "--") == 0) {
24866                                 result = 0;
24867                                 all_opts = 1;
24868                         }
24869                         else if (strncmp(argv[1], "-E", 2) == 0) {
24870                                 result = compiler_encode_flag(&compiler, argv[1]);
24871                         }
24872                         else if (strncmp(argv[1], "-O", 2) == 0) {
24873                                 result = compiler_encode_flag(&compiler, argv[1]);
24874                         }
24875                         else if (strncmp(argv[1], "-I", 2) == 0) {
24876                                 result = compiler_encode_flag(&compiler, argv[1]);
24877                         }
24878                         else if (strncmp(argv[1], "-D", 2) == 0) {
24879                                 result = compiler_encode_flag(&compiler, argv[1]);
24880                         }
24881                         else if (strncmp(argv[1], "-U", 2) == 0) {
24882                                 result = compiler_encode_flag(&compiler, argv[1]);
24883                         }
24884                         else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
24885                                 result = compiler_encode_flag(&compiler, argv[1]+2);
24886                         }
24887                         else if (strncmp(argv[1], "-f", 2) == 0) {
24888                                 result = compiler_encode_flag(&compiler, argv[1]+2);
24889                         }
24890                         else if (strncmp(argv[1], "-m", 2) == 0) {
24891                                 result = arch_encode_flag(&arch, argv[1]+2);
24892                         }
24893                         if (result < 0) {
24894                                 arg_error("Invalid option specified: %s\n",
24895                                         argv[1]);
24896                         }
24897                         argv++;
24898                         argc--;
24899                 }
24900                 else {
24901                         if (filename) {
24902                                 arg_error("Only one filename may be specified\n");
24903                         }
24904                         filename = argv[1];
24905                         argv++;
24906                         argc--;
24907                 }
24908         }
24909         if (!filename) {
24910                 arg_error("No filename specified\n");
24911         }
24912         compile(filename, &compiler, &arch);
24913
24914         return 0;
24915 }