blob: da6eca26a7c1e92e0bd3ba8ffa7b6ada59d8da40 [file] [log] [blame]
Eric Biederman90089602004-05-28 14:11:54 +00001#undef VERSION_MAJOR
2#undef VERSION_MINOR
3#undef RELEASE_DATE
4#undef VERSION
5#define VERSION_MAJOR "0"
Eric Biederman41203d92004-11-08 09:31:09 +00006#define VERSION_MINOR "65"
7#define RELEASE_DATE "8 November 2004"
Eric Biederman90089602004-05-28 14:11:54 +00008#define VERSION VERSION_MAJOR "." VERSION_MINOR
9
Eric Biedermanb138ac82003-04-22 18:44:01 +000010#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>
Eric Biedermanb138ac82003-04-22 18:44:01 +000021#include <limits.h>
Eric Biederman90089602004-05-28 14:11:54 +000022#include <locale.h>
23#include <time.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000024
Eric Biederman90089602004-05-28 14:11:54 +000025#define MAX_CWD_SIZE 4096
Eric Biederman5ade04a2003-10-22 04:03:46 +000026#define MAX_ALLOCATION_PASSES 100
27
Eric Biedermana4aef6d2003-12-05 06:16:19 +000028#define DEBUG_CONSISTENCY 1
Eric Biederman530b5192003-07-01 10:05:30 +000029#define DEBUG_SDP_BLOCKS 0
30#define DEBUG_TRIPLE_COLOR 0
Eric Biedermanb138ac82003-04-22 18:44:01 +000031
Eric Biederman90089602004-05-28 14:11:54 +000032#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
Eric Biederman8d9c1232003-06-17 08:42:17 +000049#warning "FIXME give clear error messages about unused variables"
Eric Biederman530b5192003-07-01 10:05:30 +000050#warning "FIXME properly handle multi dimensional arrays"
Eric Biederman90089602004-05-28 14:11:54 +000051#warning "FIXME handle multiple register sizes"
Eric Biederman05f26fc2003-06-11 21:55:00 +000052
Eric Biedermanb138ac82003-04-22 18:44:01 +000053/* 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 */
120static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000132static 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
143static 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
Eric Biederman90089602004-05-28 14:11:54 +0000151static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000162static void xfree(const void *ptr)
163{
164 free((void *)ptr);
165}
166
167static 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
178static void xchdir(const char *path)
179{
180 if (chdir(path) != 0) {
Eric Biederman90089602004-05-28 14:11:54 +0000181 die("chdir to `%s' failed: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +0000182 path, strerror(errno));
183 }
184}
185
186static int exists(const char *dirname, const char *filename)
187{
Eric Biederman90089602004-05-28 14:11:54 +0000188 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)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000200 if ((errno != EACCES) && (errno != EROFS)) {
201 does_exist = 0;
202 }
203 }
Eric Biederman90089602004-05-28 14:11:54 +0000204 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000205 return does_exist;
206}
207
208
209static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
210{
Eric Biederman90089602004-05-28 14:11:54 +0000211 char cwd[MAX_CWD_SIZE];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000212 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 }
Eric Biederman90089602004-05-28 14:11:54 +0000222 if (getcwd(cwd, sizeof(cwd)) == 0) {
223 die("cwd buffer to small");
224 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000225 xchdir(dirname);
226 fd = open(filename, O_RDONLY);
Eric Biederman90089602004-05-28 14:11:54 +0000227 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000228 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
Eric Biederman83b991a2003-10-11 06:20:25 +0000261/* Types on the destination platform */
262#warning "FIXME this assumes 32bit x86 is the destination"
263typedef int8_t schar_t;
264typedef uint8_t uchar_t;
265typedef int8_t char_t;
266typedef int16_t short_t;
267typedef uint16_t ushort_t;
268typedef int32_t int_t;
269typedef uint32_t uint_t;
270typedef int32_t long_t;
271typedef 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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000287
Eric Biederman90089602004-05-28 14:11:54 +0000288#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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000327struct file_state {
328 struct file_state *prev;
329 const char *basename;
330 char *dirname;
331 char *buf;
332 off_t size;
Eric Biederman90089602004-05-28 14:11:54 +0000333 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000334 int line;
Eric Biederman90089602004-05-28 14:11:54 +0000335 const char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000336 int report_line;
337 const char *report_name;
338 const char *report_dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000339};
340struct hash_entry;
341struct token {
342 int tok;
343 struct hash_entry *ident;
344 int str_len;
345 union {
346 ulong_t integer;
347 const char *str;
Eric Biederman90089602004-05-28 14:11:54 +0000348 int notmacro;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000349 } 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
Eric Biederman530b5192003-07-01 10:05:30 +0000381#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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000400
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
Eric Biederman530b5192003-07-01 10:05:30 +0000417/* For OP_STORE ->type holds the type
418 * RHS(0) holds the destination address
419 * RHS(1) holds the value to store.
420 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000421
Eric Biederman90089602004-05-28 14:11:54 +0000422#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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000445
446#define OP_MIN_CONST 50
Eric Biederman90089602004-05-28 14:11:54 +0000447#define OP_MAX_CONST 58
Eric Biedermanb138ac82003-04-22 18:44:01 +0000448#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
449#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000450/* For OP_INTCONST ->type holds the type.
451 * ->u.cval holds the constant value.
452 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000453#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000454/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000455 * information. u.blob holds a pointer to the raw binary
456 * data for the constant initializer.
457 */
458#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000459/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000460 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000461 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000462 */
Eric Biederman90089602004-05-28 14:11:54 +0000463#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 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000469
470#define OP_WRITE 60
471/* OP_WRITE moves one pseudo register to another.
Eric Biederman90089602004-05-28 14:11:54 +0000472 * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
473 * RHS(0) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000474 */
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.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000480 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000481 */
482#define OP_COPY 62
Eric Biederman90089602004-05-28 14:11:54 +0000483/* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
Eric Biederman0babc1c2003-05-09 02:39:00 +0000484 */
Eric Biederman90089602004-05-28 14:11:54 +0000485#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
Eric Biederman0babc1c2003-05-09 02:39:00 +0000490/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000491 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000492 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000493 */
Eric Biederman90089602004-05-28 14:11:54 +0000494#define OP_ASM 65
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000495/* 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 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000501
Eric Biederman90089602004-05-28 14:11:54 +0000502#define OP_DEREF 66
Eric Biedermanb138ac82003-04-22 18:44:01 +0000503/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000504 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000505 * OP_DEREF serves as a place holder to indicate all necessary
506 * checks have been done to indicate a value is an lvalue.
507 */
Eric Biederman90089602004-05-28 14:11:54 +0000508#define OP_DOT 67
Eric Biederman0babc1c2003-05-09 02:39:00 +0000509/* OP_DOT references a submember of a structure lvalue.
Eric Biederman90089602004-05-28 14:11:54 +0000510 * MISC(0) holds the lvalue.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000511 * ->u.field holds the name of the field we want.
512 *
Eric Biederman90089602004-05-28 14:11:54 +0000513 * Not seen after structures are flattened.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000514 */
Eric Biederman90089602004-05-28 14:11:54 +0000515#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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000523/* OP_VAL returns the value of a subexpression of the current expression.
524 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000525 * RHS(0) holds the expression.
526 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000527 * value of the expression.
528 *
529 * Not seen outside of expressions.
530 */
Eric Biederman90089602004-05-28 14:11:54 +0000531
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 ????
Eric Biedermanb138ac82003-04-22 18:44:01 +0000544 */
545
Eric Biederman90089602004-05-28 14:11:54 +0000546#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
Eric Biederman5ade04a2003-10-22 04:03:46 +0000556/* OP_FCALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000557 * MISC(0) holds a pointer to the OP_LIST of a function
558 * RHS(x) holds argument x of a function
559 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000560 * Currently not seen outside of expressions.
561 */
Eric Biederman90089602004-05-28 14:11:54 +0000562#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.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000566 */
567
568/* statements */
569#define OP_LIST 80
Eric Biederman5ade04a2003-10-22 04:03:46 +0000570/* OP_LIST Holds a list of statements that compose a function, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000571 * RHS(0) holds the list of statements.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000572 * A list of all functions is maintained.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000573 */
574
Eric Biederman5ade04a2003-10-22 04:03:46 +0000575#define OP_BRANCH 81 /* an unconditional branch */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000576/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000577 * TARG(0) holds the branch target.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000578 * ->next holds where to branch to if the branch is not taken.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000579 * The branch target can only be a label
Eric Biedermanb138ac82003-04-22 18:44:01 +0000580 */
581
Eric Biederman5ade04a2003-10-22 04:03:46 +0000582#define OP_CBRANCH 82 /* a conditional branch */
583/* For conditional branch instructions
584 * RHS(0) holds the branch condition.
Eric Biederman41203d92004-11-08 09:31:09 +0000585 * TARG(0) holds the branch target.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000586 * ->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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000605/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000606 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000607 */
608
Eric Biederman5ade04a2003-10-22 04:03:46 +0000609#define OP_ADECL 87
610/* OP_ADECL is a triple that establishes an lvalue for assignments.
Eric Biederman90089602004-05-28 14:11:54 +0000611 * 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.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000614 * ->use is a list of statements that use the variable.
Eric Biederman90089602004-05-28 14:11:54 +0000615 *
616 * Although OP_ADECL always has register sized pieces they are not
617 * used until structures are flattened/decomposed into their register
618 * components.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000619 */
620
Eric Biederman5ade04a2003-10-22 04:03:46 +0000621#define OP_SDECL 88
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000622/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000623 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000624 * ->use is a list of statements that use the variable.
625 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000626 */
627
628
Eric Biederman5ade04a2003-10-22 04:03:46 +0000629#define OP_PHI 89
Eric Biedermanb138ac82003-04-22 18:44:01 +0000630/* 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
Eric Biederman5ade04a2003-10-22 04:03:46 +0000634 * is what OP_PHI is generated from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000635 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000636 * 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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000638 * in which OP_PHI resides. The elements of the array point to point
639 * to the variables OP_PHI is derived from.
640 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000641 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000642 */
643
Eric Biederman90089602004-05-28 14:11:54 +0000644#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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000684/* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000719#define OP_RDMSR 138
720#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000721#define OP_HLT 140
722
Eric Biederman0babc1c2003-05-09 02:39:00 +0000723struct op_info {
724 const char *name;
725 unsigned flags;
Eric Biederman90089602004-05-28 14:11:54 +0000726#define PURE 0x001 /* Triple has no side effects */
727#define IMPURE 0x002 /* Triple has side effects */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000728#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
Eric Biederman90089602004-05-28 14:11:54 +0000729#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;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000741};
742
Eric Biederman0babc1c2003-05-09 02:39:00 +0000743#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 }
751static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000752[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
753[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000754[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"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000771
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000772[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"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000784
Eric Biederman90089602004-05-28 14:11:54 +0000785[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"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000791
Eric Biederman83b991a2003-10-11 06:20:25 +0000792[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000793
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000794[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000795[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000796[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman90089602004-05-28 14:11:54 +0000797[OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000798
Eric Biederman90089602004-05-28 14:11:54 +0000799#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"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000801[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
802[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
Eric Biederman90089602004-05-28 14:11:54 +0000803[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"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000806[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
Eric Biederman90089602004-05-28 14:11:54 +0000807[OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"),
808[OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000809
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000810[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
Eric Biederman90089602004-05-28 14:11:54 +0000811[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"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000813/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman90089602004-05-28 14:11:54 +0000814[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 */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000817
Eric Biederman83b991a2003-10-11 06:20:25 +0000818[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
Eric Biederman90089602004-05-28 14:11:54 +0000819[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"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000823[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"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000826/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000827[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000828
Eric Biederman90089602004-05-28 14:11:54 +0000829#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
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000838[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"),
Eric Biederman90089602004-05-28 14:11:54 +0000850[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"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000861
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000862[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"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000873};
874#undef OP
875#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000876
877static 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000886 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000887}
888
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000889struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000890struct triple;
891struct block;
892struct triple_set {
893 struct triple_set *next;
894 struct triple *member;
895};
896
Eric Biederman90089602004-05-28 14:11:54 +0000897#define MAX_LHS 63
898#define MAX_RHS 127
Eric Biederman678d8162003-07-03 03:59:38 +0000899#define MAX_MISC 3
Eric Biederman90089602004-05-28 14:11:54 +0000900#define MAX_TARG 1
Eric Biederman0babc1c2003-05-09 02:39:00 +0000901
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000902struct occurance {
903 int count;
904 const char *filename;
905 const char *function;
906 int line;
907 int col;
908 struct occurance *parent;
909};
Eric Biederman90089602004-05-28 14:11:54 +0000910struct bitfield {
911 ulong_t size : 8;
912 ulong_t offset : 24;
913};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000914struct triple {
915 struct triple *next, *prev;
916 struct triple_set *use;
917 struct type *type;
Eric Biederman90089602004-05-28 14:11:54 +0000918 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)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000934 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000935#define TRIPLE_FLAG_FLATTENED (1 << 31)
936#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
937#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biederman83b991a2003-10-11 06:20:25 +0000938#define TRIPLE_FLAG_VOLATILE (1 << 28)
Eric Biederman90089602004-05-28 14:11:54 +0000939#define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */
940#define TRIPLE_FLAG_LOCAL (1 << 26)
941
942#define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000943 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000944 union {
945 ulong_t cval;
Eric Biederman90089602004-05-28 14:11:54 +0000946 struct bitfield bitfield;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000947 struct block *block;
948 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000949 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000950 struct asm_info *ainfo;
Eric Biederman90089602004-05-28 14:11:54 +0000951 struct triple *func;
952 struct symbol *symbol;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000953 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000954 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000955};
956
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000957struct reg_info {
958 unsigned reg;
959 unsigned regcm;
960};
961struct ins_template {
962 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
963};
964
965struct asm_info {
966 struct ins_template tmpl;
967 char *str;
968};
969
Eric Biedermanb138ac82003-04-22 18:44:01 +0000970struct block_set {
971 struct block_set *next;
972 struct block *member;
973};
974struct block {
975 struct block *work_next;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000976 struct triple *first, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +0000977 int edge_count;
978 struct block_set *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000979 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
991struct symbol {
992 struct symbol *next;
993 struct hash_entry *ident;
994 struct triple *def;
995 struct type *type;
996 int scope_depth;
997};
998
Eric Biederman90089602004-05-28 14:11:54 +0000999struct macro_arg {
1000 struct macro_arg *next;
1001 struct hash_entry *ident;
1002};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001003struct macro {
1004 struct hash_entry *ident;
1005 char *buf;
1006 int buf_len;
Eric Biederman90089602004-05-28 14:11:54 +00001007 int buf_off;
1008 struct macro_arg *args;
1009 int argc;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001010};
1011
1012struct 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;
Eric Biederman83b991a2003-10-11 06:20:25 +00001019 struct symbol *sym_tag;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001020 struct symbol *sym_ident;
1021};
1022
1023#define HASH_TABLE_SIZE 2048
1024
Eric Biederman5ade04a2003-10-22 04:03:46 +00001025struct compiler_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +00001026 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001027 const char *ofilename;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001028 unsigned long flags;
1029 unsigned long debug;
1030 unsigned long max_allocation_passes;
Eric Biederman90089602004-05-28 14:11:54 +00001031
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;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001040};
1041struct arch_state {
1042 unsigned long features;
1043};
Eric Biederman90089602004-05-28 14:11:54 +00001044struct 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
Eric Biederman5ade04a2003-10-22 04:03:46 +00001051struct compile_state {
1052 struct compiler_state *compiler;
1053 struct arch_state *arch;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001054 FILE *output;
Eric Biederman90089602004-05-28 14:11:54 +00001055 FILE *errout;
1056 FILE *dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001057 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001058 struct occurance *last_occurance;
1059 const char *function;
Eric Biederman41203d92004-11-08 09:31:09 +00001060 int token_base;
1061 struct token token[6];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001062 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +00001063 struct hash_entry *i_switch;
1064 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001065 struct hash_entry *i_continue;
1066 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +00001067 struct hash_entry *i_default;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001068 struct hash_entry *i_return;
Eric Biederman90089602004-05-28 14:11:54 +00001069 /* 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001079 int scope_depth;
Eric Biederman90089602004-05-28 14:11:54 +00001080 unsigned char if_bytes[(MAX_CPP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
1081 int if_depth;
1082 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001083 int macro_line;
1084 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001085 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001086 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001087 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001088 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001089 struct basic_blocks bb;
1090 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001091};
1092
Eric Biederman0babc1c2003-05-09 02:39:00 +00001093/* visibility global/local */
1094/* static/auto duration */
1095/* typedef, register, inline */
1096#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001097#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001098/* Visibility */
1099#define STOR_GLOBAL 0x0001
1100/* Duration */
1101#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001102/* Definition locality */
1103#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001104/* Storage specifiers */
1105#define STOR_AUTO 0x0000
1106#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001107#define STOR_LOCAL 0x0003
1108#define STOR_EXTERN 0x0007
1109#define STOR_INLINE 0x0008
1110#define STOR_REGISTER 0x0010
1111#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001112
Eric Biederman5ade04a2003-10-22 04:03:46 +00001113#define QUAL_SHIFT 5
1114#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001115#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001116#define QUAL_CONST 0x0020
1117#define QUAL_VOLATILE 0x0040
1118#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001119
1120#define TYPE_SHIFT 8
1121#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001122#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))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001124#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1125#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001126#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1127#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001128#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 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001144
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
Eric Biederman0babc1c2003-05-09 02:39:00 +00001153#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001154/* 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 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001159#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001160/* 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 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001165#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.
Eric Biederman90089602004-05-28 14:11:54 +00001172 * type->right holds the type of the arguments
1173 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001174 */
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 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001185#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001186/* TYPE_ARRAY is a basic building block when definitng arrays.
1187 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001188 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001189 */
Eric Biederman90089602004-05-28 14:11:54 +00001190#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
Eric Biederman0babc1c2003-05-09 02:39:00 +00001222
Eric Biederman83b991a2003-10-11 06:20:25 +00001223#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001224
1225struct 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
Eric Biederman530b5192003-07-01 10:05:30 +00001233#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001234#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001235#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001236#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001237#define MAX_REGISTERS 75
1238#define REGISTER_BITS 7
1239#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001240#define REG_ERROR 0
1241#define REG_UNSET 1
1242#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001243#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)
Eric Biederman83b991a2003-10-11 06:20:25 +00001249#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
Eric Biederman90089602004-05-28 14:11:54 +00001257#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001258#error "Too many id bits used"
1259#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001260
1261/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001262#define REG_SHIFT 0
1263#define REGC_SHIFT REGISTER_BITS
1264#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001265#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)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001268#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)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001272
Eric Biederman90089602004-05-28 14:11:54 +00001273#define ARCH_INPUT_REGS 4
1274#define ARCH_OUTPUT_REGS 4
1275
1276static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1277static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001278static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001279static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001280static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001281static void arch_reg_equivs(
1282 struct compile_state *state, unsigned *equiv, int reg);
1283static int arch_select_free_register(
1284 struct compile_state *state, char *used, int classes);
1285static unsigned arch_regc_size(struct compile_state *state, int class);
1286static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1287static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1288static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001289static struct reg_info arch_reg_constraint(
1290 struct compile_state *state, struct type *type, const char *constraint);
1291static struct reg_info arch_reg_clobber(
1292 struct compile_state *state, const char *clobber);
1293static struct reg_info arch_reg_lhs(struct compile_state *state,
1294 struct triple *ins, int index);
1295static struct reg_info arch_reg_rhs(struct compile_state *state,
1296 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001297static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001298static struct triple *transform_to_arch_instruction(
1299 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001300static struct triple *flatten(
1301 struct compile_state *state, struct triple *first, struct triple *ptr);
1302
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001303
1304
Eric Biedermanb138ac82003-04-22 18:44:01 +00001305
Eric Biederman5ade04a2003-10-22 04:03:46 +00001306#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
Eric Biederman90089602004-05-28 14:11:54 +00001322#define DEBUG_VERIFICATION 0x00010000
1323#define DEBUG_CALLS 0x00020000
1324#define DEBUG_CALLS2 0x00040000
1325#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001326
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
Eric Biederman90089602004-05-28 14:11:54 +00001335#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
Eric Biederman5ade04a2003-10-22 04:03:46 +00001379
1380#define COMPILER_DEFAULT_FLAGS ( \
1381 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001382 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001383 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 | \
Eric Biederman90089602004-05-28 14:11:54 +00001392 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001393 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001394
Eric Biederman153ea352003-06-20 14:43:20 +00001395#define GLOBAL_SCOPE_DEPTH 1
1396#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001397
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001398static void compile_file(struct compile_state *old_state, const char *filename, int local);
1399
Eric Biederman5ade04a2003-10-22 04:03:46 +00001400
1401
1402static 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;
Eric Biederman90089602004-05-28 14:11:54 +00001410 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");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001416}
1417
1418struct compiler_flag {
1419 const char *name;
1420 unsigned long flag;
1421};
Eric Biederman90089602004-05-28 14:11:54 +00001422
1423struct compiler_arg {
1424 const char *name;
1425 unsigned long mask;
1426 struct compiler_flag flags[16];
1427};
1428
Eric Biederman5ade04a2003-10-22 04:03:46 +00001429static 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
Eric Biederman90089602004-05-28 14:11:54 +00001449static 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
1473static 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
1484static 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
1496static 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
1507static void arg_error(char *fmt, ...);
1508static const char *identifier(const char *str, const char *end);
1509
1510static 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
1522static 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
1542static 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
1559static 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};
1576static 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};
1589static 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};
1595static 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
Eric Biederman5ade04a2003-10-22 04:03:46 +00001620static int compiler_encode_flag(
1621 struct compiler_state *compiler, const char *flag)
1622{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001623 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) {
Eric Biederman90089602004-05-28 14:11:54 +00001633 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001646 }
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;
Eric Biederman90089602004-05-28 14:11:54 +00001666 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001667 }
1668 else {
Eric Biederman90089602004-05-28 14:11:54 +00001669 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1670 if (result < 0) {
1671 result = set_arg(romcc_args, &compiler->flags, flag);
1672 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001673 }
1674 return result;
1675}
1676
Eric Biederman90089602004-05-28 14:11:54 +00001677static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001690static void do_cleanup(struct compile_state *state)
1691{
1692 if (state->output) {
1693 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001694 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001695 state->output = 0;
1696 }
1697 if (state->dbgout) {
1698 fflush(state->dbgout);
1699 }
1700 if (state->errout) {
1701 fflush(state->errout);
1702 }
1703}
1704
1705static struct compile_state *exit_state;
1706static void exit_cleanup(void)
1707{
1708 if (exit_state) {
1709 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001710 }
1711}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001712
1713static int get_col(struct file_state *file)
1714{
1715 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001716 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001717 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
1730static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1731{
1732 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001733 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001734 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001735 for(spot = triple->occurance; spot; spot = spot->parent) {
1736 fprintf(fp, "%s:%d.%d: ",
1737 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001738 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001739 return;
1740 }
1741 if (!state->file) {
1742 return;
1743 }
1744 col = get_col(state->file);
1745 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001746 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001747}
1748
Eric Biederman5ade04a2003-10-22 04:03:46 +00001749static void internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001750 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001751{
Eric Biederman90089602004-05-28 14:11:54 +00001752 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001753 va_list args;
1754 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001755 loc(fp, state, ptr);
1756 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001757 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001758 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001759 }
Eric Biederman90089602004-05-28 14:11:54 +00001760 fprintf(fp, "Internal compiler error: ");
1761 vfprintf(fp, fmt, args);
1762 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001763 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001764 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001765 abort();
1766}
1767
1768
Eric Biederman5ade04a2003-10-22 04:03:46 +00001769static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001770 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001771{
Eric Biederman90089602004-05-28 14:11:54 +00001772 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001773 va_list args;
1774 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001775 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001776 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001777 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001778 }
Eric Biederman90089602004-05-28 14:11:54 +00001779 fprintf(fp, "Internal compiler warning: ");
1780 vfprintf(fp, fmt, args);
1781 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001782 va_end(args);
1783}
1784
1785
1786
Eric Biederman5ade04a2003-10-22 04:03:46 +00001787static void error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001788 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001789{
Eric Biederman90089602004-05-28 14:11:54 +00001790 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001791 va_list args;
1792 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001793 loc(fp, state, ptr);
1794 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001795 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001796 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001797 }
Eric Biederman90089602004-05-28 14:11:54 +00001798 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001799 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001800 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001801 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001802 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001803 abort();
1804 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001805 exit(1);
1806}
1807
Eric Biederman5ade04a2003-10-22 04:03:46 +00001808static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001809 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001810{
Eric Biederman90089602004-05-28 14:11:54 +00001811 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001812 va_list args;
1813 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001814 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");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001821 va_end(args);
1822}
1823
Eric Biedermanb138ac82003-04-22 18:44:01 +00001824#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1825
Eric Biederman0babc1c2003-05-09 02:39:00 +00001826static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001827{
1828 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001829 if (op >= OP_MAX) {
1830 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001831 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001832 if (op < 0) {
1833 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001834 }
1835}
1836
Eric Biederman0babc1c2003-05-09 02:39:00 +00001837static void valid_ins(struct compile_state *state, struct triple *ptr)
1838{
1839 valid_op(state, ptr->op);
1840}
1841
Eric Biederman90089602004-05-28 14:11:54 +00001842static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00001865static 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
1904static 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
1925static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001926static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001927static 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
1951static void unuse_triple(struct triple *used, struct triple *unuser)
1952{
1953 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001954 if (!used) {
1955 return;
1956 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001957 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
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001970static void put_occurance(struct occurance *occurance)
1971{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001972 if (occurance) {
1973 occurance->count -= 1;
1974 if (occurance->count <= 0) {
1975 if (occurance->parent) {
1976 put_occurance(occurance->parent);
1977 }
1978 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001979 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001980 }
1981}
1982
1983static void get_occurance(struct occurance *occurance)
1984{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001985 if (occurance) {
1986 occurance->count += 1;
1987 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001988}
1989
1990
1991static 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) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00002015 ((last->filename == filename) ||
2016 (strcmp(last->filename, filename) == 0)))
2017 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002018 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
2036static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002037 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002038{
2039 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002040 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 */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002052 last = state->last_occurance;
2053 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002054 (last->parent == base) &&
2055 (last->col == top->col) &&
2056 (last->line == top->line) &&
2057 (last->function == top->function) &&
2058 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002059 get_occurance(last);
2060 return last;
2061 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002062 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002063 if (last) {
2064 state->last_occurance = 0;
2065 put_occurance(last);
2066 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002067 /* Generate a new occurance structure */
2068 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002069 result = xmalloc(sizeof(*result), "occurance");
2070 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002071 result->filename = top->filename;
2072 result->function = top->function;
2073 result->line = top->line;
2074 result->col = top->col;
2075 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002076 state->last_occurance = result;
2077 return result;
2078}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002079
2080static struct occurance dummy_occurance = {
2081 .count = 2,
2082 .filename = __FILE__,
2083 .function = "",
2084 .line = __LINE__,
2085 .col = 0,
2086 .parent = 0,
2087};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002088
Eric Biederman90089602004-05-28 14:11:54 +00002089/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002090 * from a triple. Having allows certain sanity checks to pass even
2091 * when the original triple that was pointed to is gone.
2092 */
Eric Biederman90089602004-05-28 14:11:54 +00002093static struct triple unknown_triple = {
2094 .next = &unknown_triple,
2095 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002096 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002097 .op = OP_UNKNOWNVAL,
2098 .lhs = 0,
2099 .rhs = 0,
2100 .misc = 0,
2101 .targ = 0,
2102 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002103 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002104 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002105 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002106 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002107};
2108
Eric Biederman0babc1c2003-05-09 02:39:00 +00002109
Eric Biederman90089602004-05-28 14:11:54 +00002110static size_t registers_of(struct compile_state *state, struct type *type);
2111
2112static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002113 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2114 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002115{
Eric Biederman90089602004-05-28 14:11:54 +00002116 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002117 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002118 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002119 dummy.op = op;
2120 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002121 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;
Eric Biederman90089602004-05-28 14:11:54 +00002126
2127 switch(op) {
2128 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002129 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002130 break;
2131 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002132 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002133 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:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002141 rhs = rhs_wanted;
2142 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002143 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002144 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002145 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002146 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002147 }
2148 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002149 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002150 }
2151 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002152 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002153 }
2154 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002155 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002156 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002157
2158 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002159 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002160 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");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002164 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002165 ret->lhs = lhs;
2166 ret->rhs = rhs;
2167 ret->misc = misc;
2168 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002169 ret->type = type;
2170 ret->next = ret;
2171 ret->prev = ret;
2172 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002173 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002185 return ret;
2186}
2187
Eric Biederman0babc1c2003-05-09 02:39:00 +00002188struct triple *dup_triple(struct compile_state *state, struct triple *src)
2189{
2190 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002191 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002192 src_lhs = src->lhs;
2193 src_rhs = src->rhs;
2194 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002195 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002196 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002197 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002198 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002199 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002200 return dup;
2201}
2202
Eric Biederman41203d92004-11-08 09:31:09 +00002203static 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
Eric Biederman0babc1c2003-05-09 02:39:00 +00002212static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002213 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002214{
2215 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002216 struct occurance *occurance;
2217 occurance = new_occurance(state);
2218 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002219 return ret;
2220}
2221
2222static struct triple *build_triple(struct compile_state *state,
2223 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002224 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002225{
2226 struct triple *ret;
2227 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002228 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002229 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002230 if (count > 0) {
2231 ret->param[0] = left;
2232 }
2233 if (count > 1) {
2234 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002235 }
2236 return ret;
2237}
2238
Eric Biederman0babc1c2003-05-09 02:39:00 +00002239static 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002244 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002245 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002246 if (count >= 1) {
2247 ret->param[0] = left;
2248 }
2249 if (count >= 2) {
2250 ret->param[1] = right;
2251 }
2252 return ret;
2253}
2254
2255static struct triple *branch(struct compile_state *state,
2256 struct triple *targ, struct triple *test)
2257{
2258 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002259 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002260 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002261 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002262 } else {
2263 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002264 }
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");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002269 }
2270 return ret;
2271}
2272
Eric Biederman90089602004-05-28 14:11:54 +00002273static int triple_is_label(struct compile_state *state, struct triple *ins);
2274static int triple_is_call(struct compile_state *state, struct triple *ins);
2275static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002276static void insert_triple(struct compile_state *state,
2277 struct triple *first, struct triple *ptr)
2278{
2279 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002280 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002281 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;
Eric Biederman90089602004-05-28 14:11:54 +00002287
2288 if (triple_is_cbranch(state, ptr->prev) ||
2289 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002290 unuse_triple(first, ptr->prev);
2291 use_triple(ptr, ptr->prev);
2292 }
2293 }
2294}
2295
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002296static 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
Eric Biederman90089602004-05-28 14:11:54 +00002307static int triple_is_branch(struct compile_state *state, struct triple *ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002308static struct block *block_of_triple(struct compile_state *state,
2309 struct triple *ins)
2310{
2311 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002312 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002313 return 0;
2314 }
2315 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002316 while(ins != first && !triple_is_branch(state, ins->prev) &&
2317 !triple_stores_block(state, ins))
2318 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002319 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002320 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002321 }
2322 ins = ins->prev;
2323 }
Eric Biederman90089602004-05-28 14:11:54 +00002324 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002325}
2326
Eric Biederman90089602004-05-28 14:11:54 +00002327static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002328static 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{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002332 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002333 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002334 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002335 /* If I am an OP_PIECE jump to the real instruction */
2336 if (base->op == OP_PIECE) {
2337 base = MISC(base, 0);
2338 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002339 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002340 get_occurance(base->occurance);
2341 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002342 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002343 if (triple_stores_block(state, ret)) {
2344 ret->u.block = block;
2345 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002346 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002347 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)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002355 block->first = ret;
2356 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002357 return ret;
2358}
2359
2360static 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{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002364 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002365 struct triple *ret, *next;
2366 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002367 /* 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 */
Eric Biederman90089602004-05-28 14:11:54 +00002372 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002373 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002374 base = LHS(base, zlhs - 1);
2375 }
2376
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002377 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002378 get_occurance(base->occurance);
2379 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002380 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002381 if (triple_stores_block(state, ret)) {
2382 ret->u.block = block;
2383 }
Eric Biederman90089602004-05-28 14:11:54 +00002384 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)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002395 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002396 if (zlhs) {
2397 block->last = LHS(ret, zlhs - 1);
2398 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002399 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002400 return ret;
2401}
2402
Eric Biederman90089602004-05-28 14:11:54 +00002403static struct type *reg_type(
2404 struct compile_state *state, struct type *type, int reg);
2405
2406static 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
2432static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00002441static 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
Eric Biederman90089602004-05-28 14:11:54 +00002449static 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}
2470static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002471static void display_triple(FILE *fp, struct triple *ins)
2472{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002473 struct occurance *ptr;
2474 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002475 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));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002488 }
Eric Biederman90089602004-05-28 14:11:54 +00002489 if (ins == 0) {
2490 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002491 }
Eric Biederman90089602004-05-28 14:11:54 +00002492 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),
Eric Biederman83b991a2003-10-11 06:20:25 +00002495 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002496 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002497 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002498 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),
Eric Biederman83b991a2003-10-11 06:20:25 +00002510 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002511 }
2512 else {
2513 int i, count;
Eric Biederman90089602004-05-28 14:11:54 +00002514 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002523 for(i = 0; i < count; i++) {
2524 fprintf(fp, " %-10p", ins->param[i]);
2525 }
2526 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002527 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002528 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002529 }
Eric Biederman90089602004-05-28 14:11:54 +00002530 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002531 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002532#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);
Eric Biederman530b5192003-07-01 10:05:30 +00002554 }
2555 }
Eric Biederman90089602004-05-28 14:11:54 +00002556 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002557 fflush(fp);
2558}
2559
Eric Biederman90089602004-05-28 14:11:54 +00002560static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002561static void display_triple_changes(
2562 FILE *fp, const struct triple *new, const struct triple *orig)
2563{
2564
2565 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002566 new_count = TRIPLE_SIZE(new);
2567 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002568 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;
Eric Biederman90089602004-05-28 14:11:54 +00002576 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002577 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 }
Eric Biederman90089602004-05-28 14:11:54 +00002616
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
Eric Biederman5ade04a2003-10-22 04:03:46 +00002627 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
Eric Biederman83b991a2003-10-11 06:20:25 +00002641static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002642{
2643 /* Does the triple have no side effects.
2644 * I.e. Rexecuting the triple with the same arguments
2645 * gives the same value.
2646 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002647 unsigned pure;
2648 valid_ins(state, ins);
2649 pure = PURE_BITS(table_ops[ins->op].flags);
2650 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002651 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002652 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002653 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002654 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002655}
2656
Eric Biederman90089602004-05-28 14:11:54 +00002657static 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
Eric Biederman0babc1c2003-05-09 02:39:00 +00002665static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002666{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002667 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002668 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002669 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002670}
2671
Eric Biederman90089602004-05-28 14:11:54 +00002672static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002673{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002674 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002675 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002676}
2677
Eric Biederman90089602004-05-28 14:11:54 +00002678static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002679{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002680 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002681 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002682 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002683 type = BRANCH_BITS(table_ops[ins->op].flags);
2684 return (type != 0) && (type != CBRANCH);
2685}
2686
2687static 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
2693static 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
2699static 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
2706static int triple_is_end(struct compile_state *state, struct triple *ins)
2707{
2708 return triple_is_branch_type(state, ins, ENDBRANCH);
2709}
2710
2711static 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
2717static 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;
Eric Biederman530b5192003-07-01 10:05:30 +00002725}
2726
Eric Biederman0babc1c2003-05-09 02:39:00 +00002727static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002728{
2729 /* This function is used to determine which triples need
2730 * a register.
2731 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002732 int is_def;
2733 valid_ins(state, ins);
2734 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002735 if (ins->lhs >= 1) {
2736 is_def = 0;
2737 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002738 return is_def;
2739}
2740
Eric Biederman83b991a2003-10-11 06:20:25 +00002741static 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
Eric Biederman90089602004-05-28 14:11:54 +00002749static 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
2757static 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
Eric Biederman0babc1c2003-05-09 02:39:00 +00002762static struct triple **triple_iter(struct compile_state *state,
2763 size_t count, struct triple **vector,
2764 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002765{
2766 struct triple **ret;
2767 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002768 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002769 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002770 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002771 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002772 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002773 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002774 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002775 }
2776 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002777
Eric Biedermanb138ac82003-04-22 18:44:01 +00002778}
2779
2780static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002781 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002782{
Eric Biederman90089602004-05-28 14:11:54 +00002783 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002784 ins, last);
2785}
2786
2787static struct triple **triple_rhs(struct compile_state *state,
2788 struct triple *ins, struct triple **last)
2789{
Eric Biederman90089602004-05-28 14:11:54 +00002790 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002791 ins, last);
2792}
2793
2794static struct triple **triple_misc(struct compile_state *state,
2795 struct triple *ins, struct triple **last)
2796{
Eric Biederman90089602004-05-28 14:11:54 +00002797 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002798 ins, last);
2799}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002800
Eric Biederman90089602004-05-28 14:11:54 +00002801static struct triple **do_triple_targ(struct compile_state *state,
2802 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002803{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002804 size_t count;
2805 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002806 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002807 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002808 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002819 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002820 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002821 if (!last) {
2822 ret = &ins->next;
2823 } else if (last == &ins->next) {
2824 last = 0;
2825 }
2826 }
2827 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002828 if (!last) {
2829 ret = vector;
2830 }
2831 else if ((last >= vector) && (last < (vector + count - 1))) {
2832 ret = last + 1;
2833 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002834 else if (last == vector + count - 1) {
2835 last = 0;
2836 }
2837 }
Eric Biederman90089602004-05-28 14:11:54 +00002838 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002839 struct triple_set *use;
2840 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002841 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002842 continue;
2843 }
2844 if (!last) {
2845 ret = &use->member->next;
2846 break;
2847 }
2848 else if (last == &use->member->next) {
2849 last = 0;
2850 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002851 }
2852 }
2853 return ret;
2854}
2855
Eric Biederman90089602004-05-28 14:11:54 +00002856static 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
2862static 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
2869static 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 */
2896static 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}
2917static struct triple *fresult(struct compile_state *state, struct triple *func)
2918{
2919 return do_farg(state, func, 0);
2920}
2921static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2922{
2923 return do_farg(state, func, 1);
2924}
2925static 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
2932static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002953static void verify_use(struct compile_state *state,
2954 struct triple *user, struct triple *used)
2955{
2956 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002957 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002958 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
2974static 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);
Eric Biederman90089602004-05-28 14:11:54 +00002980#warning "AUDIT ME ->rhs"
2981 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002982 param = &RHS(user, 0);
2983 for(i = 0; i < size; i++) {
2984 if (param[i] == used) {
2985 return i;
2986 }
2987 }
2988 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002989}
2990
2991static void free_triple(struct compile_state *state, struct triple *ptr)
2992{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002993 size_t size;
2994 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002995 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002996 ptr->prev->next = ptr->next;
2997 ptr->next->prev = ptr->prev;
2998 if (ptr->use) {
2999 internal_error(state, ptr, "ptr->use != 0");
3000 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003001 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00003002 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003003 xfree(ptr);
3004}
3005
3006static void release_triple(struct compile_state *state, struct triple *ptr)
3007{
3008 struct triple_set *set, *next;
3009 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00003010 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00003011 if (ptr == &unknown_triple) {
3012 return;
3013 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00003014 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00003015 /* Make certain the we are not the first or last element of a block */
3016 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00003017 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 }
Eric Biederman66fe2222003-07-04 15:14:04 +00003027 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003028 /* 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003041 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003047 expr = triple_targ(state, ptr, 0);
3048 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003049 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003050 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003056 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003057 expr = triple_rhs(state, set->member, 0);
3058 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3059 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003060 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003061 }
3062 }
3063 expr = triple_lhs(state, set->member, 0);
3064 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3065 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003066 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003067 }
3068 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003069 expr = triple_misc(state, set->member, 0);
3070 for(; expr; expr = triple_misc(state, set->member, expr)) {
3071 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003072 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003073 }
3074 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003075 expr = triple_targ(state, set->member, 0);
3076 for(; expr; expr = triple_targ(state, set->member, expr)) {
3077 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003078 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003079 }
3080 }
3081 unuse_triple(ptr, set->member);
3082 }
3083 free_triple(state, ptr);
3084}
3085
Eric Biederman5ade04a2003-10-22 04:03:46 +00003086static void print_triples(struct compile_state *state);
3087static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003088
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
Eric Biederman41203d92004-11-08 09:31:09 +00003189#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
Eric Biedermanb138ac82003-04-22 18:44:01 +00003201
Eric Biederman41203d92004-11-08 09:31:09 +00003202#define TOK_FIRST_MACRO TOK_MDEFINE
3203#define TOK_LAST_MACRO TOK_MENDIF
Eric Biedermanb138ac82003-04-22 18:44:01 +00003204
Eric Biederman41203d92004-11-08 09:31:09 +00003205#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
Eric Biedermanb138ac82003-04-22 18:44:01 +00003211
3212static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003213[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003214[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
Eric Biederman41203d92004-11-08 09:31:09 +00003308[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",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003320
Eric Biederman41203d92004-11-08 09:31:09 +00003321[TOK_MIF ] = "#if",
3322[TOK_MELSE ] = "#else",
3323[TOK_MIDENT ] = "#:ident:",
3324[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003325[TOK_EOF ] = "EOF",
3326};
3327
3328static 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
3341static 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
3372static 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
3384static void ident_to_macro(struct compile_state *state, struct token *tk)
3385{
3386 struct hash_entry *entry;
3387 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003388 if (!entry)
3389 return;
3390 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003391 tk->tok = entry->tok;
3392 }
Eric Biederman41203d92004-11-08 09:31:09 +00003393 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003402}
3403
3404static 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
Eric Biederman90089602004-05-28 14:11:54 +00003415static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003416 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003417 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003418{
3419 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003420 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003421 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;
Eric Biederman90089602004-05-28 14:11:54 +00003427 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003428 sym->next = *chain;
3429 *chain = sym;
3430}
3431
Eric Biederman90089602004-05-28 14:11:54 +00003432static void symbol(
3433 struct compile_state *state, struct hash_entry *ident,
3434 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003435{
Eric Biederman90089602004-05-28 14:11:54 +00003436 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3437}
3438
3439static 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");
Eric Biederman153ea352003-06-20 14:43:20 +00003444 }
Eric Biederman90089602004-05-28 14:11:54 +00003445 symbol(state, ident, &ident->sym_ident, def, def->type);
3446}
3447
3448static 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);
Eric Biederman153ea352003-06-20 14:43:20 +00003452}
3453
Eric Biedermanb138ac82003-04-22 18:44:01 +00003454static void start_scope(struct compile_state *state)
3455{
3456 state->scope_depth++;
3457}
3458
Eric Biederman90089602004-05-28 14:11:54 +00003459static void end_scope_syms(struct compile_state *state,
3460 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003461{
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
3472static 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) {
Eric Biederman90089602004-05-28 14:11:54 +00003484 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003487 entry = entry->next;
3488 }
3489 }
3490 state->scope_depth = depth - 1;
3491}
3492
3493static 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003528 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003529 hash_keyword(state, "while", TOK_WHILE);
3530 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003531 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003532 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3533 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3534}
3535
3536static void register_macro_keywords(struct compile_state *state)
3537{
Eric Biederman41203d92004-11-08 09:31:09 +00003538 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003550}
3551
Eric Biederman90089602004-05-28 14:11:54 +00003552
3553static 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
3577static 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) &&
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +00003589 (memcmp(macro->buf, value, value_len) == 0)) {
Eric Biederman90089602004-05-28 14:11:54 +00003590 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;
Eric Biederman41203d92004-11-08 09:31:09 +00003606 macro->buf = xmalloc(macro->buf_len + 1, "macro buf");
Eric Biederman90089602004-05-28 14:11:54 +00003607
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);
Eric Biederman41203d92004-11-08 09:31:09 +00003614 macro->buf[macro->buf_len] = '\0';
Eric Biederman90089602004-05-28 14:11:54 +00003615
3616 ident->sym_define = macro;
3617}
3618
3619static 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
3631static 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
3660static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00003685static 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':
Eric Biederman41203d92004-11-08 09:31:09 +00003694 ret = 1;
3695 break;
3696 }
3697 return ret;
3698}
3699
3700static int eolp(int c)
3701{
3702 int ret = 0;
3703 switch(c) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003704 case '\n':
3705 ret = 1;
3706 break;
3707 }
3708 return ret;
3709}
3710
3711static 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}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003722static int digval(int c)
3723{
3724 int val = -1;
3725 if ((c >= '0') && (c <= '9')) {
3726 val = c - '0';
3727 }
3728 return val;
3729}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003730
3731static 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}
3744static 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
3759static 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}
3770static 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
3779static 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
Eric Biederman90089602004-05-28 14:11:54 +00003802static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00003816static 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;
Eric Biederman90089602004-05-28 14:11:54 +00003835 case '"': c = '"'; str++; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003836 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
Eric Biederman90089602004-05-28 14:11:54 +00003863static const char *after_digits(const char *ptr, const char *end)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003864{
3865 while((ptr < end) && digitp(*ptr)) {
3866 ptr++;
3867 }
3868 return ptr;
3869}
3870
Eric Biederman90089602004-05-28 14:11:54 +00003871static const char *after_octdigits(const char *ptr, const char *end)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003872{
3873 while((ptr < end) && octdigitp(*ptr)) {
3874 ptr++;
3875 }
3876 return ptr;
3877}
3878
Eric Biederman90089602004-05-28 14:11:54 +00003879static const char *after_hexdigits(const char *ptr, const char *end)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003880{
3881 while((ptr < end) && hexdigitp(*ptr)) {
3882 ptr++;
3883 }
3884 return ptr;
3885}
3886
3887static void save_string(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003888 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003889{
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}
Eric Biederman90089602004-05-28 14:11:54 +00003902
3903static int lparen_peek(struct compile_state *state, struct file_state *file)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003904{
Eric Biederman90089602004-05-28 14:11:54 +00003905 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
3915static void raw_next_token(struct compile_state *state,
3916 struct file_state *file, struct token *tk)
3917{
3918 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003919 int c, c1, c2, c3;
Eric Biederman90089602004-05-28 14:11:54 +00003920 const char *tokp, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003921 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00003922
Eric Biedermanb138ac82003-04-22 18:44:01 +00003923 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 }
Eric Biederman41203d92004-11-08 09:31:09 +00003948 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003955 /* Whitespace */
3956 else if (spacep(c)) {
3957 tok = TOK_SPACE;
3958 while ((tokp < end) && spacep(c)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003959 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') {
Eric Biederman41203d92004-11-08 09:31:09 +00003971 tokp--;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003972 break;
3973 }
3974 }
3975 }
3976 /* Comments */
3977 else if ((c == '/') && (c1 == '*')) {
3978 int line;
Eric Biederman90089602004-05-28 14:11:54 +00003979 const char *line_start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003980 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 }
Eric Biederman41203d92004-11-08 09:31:09 +00003997 if (state->token_base && (line != file->line)) {
3998 error(state, 0,
3999 "multiline comment in preprocessor directive");
4000 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004001 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004002 file->line = line;
4003 file->line_start = line_start;
4004 }
4005 /* string constants */
4006 else if ((c == '"') ||
4007 ((c == 'L') && (c1 == '"'))) {
4008 int line;
Eric Biederman90089602004-05-28 14:11:54 +00004009 const char *line_start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004010 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) {
Eric Biederman41203d92004-11-08 09:31:09 +00004036 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004042 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004043 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004044 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;
Eric Biederman90089602004-05-28 14:11:54 +00004054 const char *line_start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004055 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) {
Eric Biederman41203d92004-11-08 09:31:09 +00004081 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004087 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004088 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004089 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)))) {
Eric Biederman90089602004-05-28 14:11:54 +00004110 const char *next, *new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004111 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;
Eric Biederman90089602004-05-28 14:11:54 +00004181 tokp = identifier(tokp, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004182 tokp -= 1;
4183 tk->ident = lookup(state, token, tokp +1 - token);
Eric Biederman90089602004-05-28 14:11:54 +00004184 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004190 }
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; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004249
4250 file->pos = tokp + 1;
4251 tk->tok = tok;
4252 if (tok == TOK_IDENT) {
Eric Biederman41203d92004-11-08 09:31:09 +00004253 if (state->token_base == 0) {
4254 ident_to_keyword(state, tk);
4255 } else {
4256 ident_to_macro(state, tk);
4257 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004258 }
Eric Biederman90089602004-05-28 14:11:54 +00004259}
4260
4261static void next_token(struct compile_state *state, struct token *tk)
4262{
4263 struct file_state *file;
4264 file = state->file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004265 /* Don't return space tokens. */
Eric Biederman90089602004-05-28 14:11:54 +00004266 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
4283static 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 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004289 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004290 name2 = tk->ident->name;
4291 }
4292 error(state, 0, "\tfound %s %s expected %s",
4293 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004294 }
4295}
4296
Eric Biederman90089602004-05-28 14:11:54 +00004297struct macro_arg_value {
4298 struct hash_entry *ident;
4299 unsigned char *value;
4300 size_t len;
4301};
4302static 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
4371static 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
4380struct macro_buf {
4381 char *str;
4382 size_t len, pos;
4383};
4384
4385static 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
4404static int compile_macro(struct compile_state *state,
4405 struct file_state **filep, struct token *tk);
4406
4407static 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
4470static 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
4586static 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
4632static int compile_macro(struct compile_state *state,
4633 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004634{
4635 struct file_state *file;
4636 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004637 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00004644 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004645 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00004702 file = xmalloc(sizeof(*file), "file_state");
Eric Biederman90089602004-05-28 14:11:54 +00004703 file->basename = xstrdup(ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004704 file->dirname = xstrdup("");
Eric Biederman90089602004-05-28 14:11:54 +00004705 file->buf = buf.str;
4706 file->size = buf.pos - 2;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004707 file->pos = file->buf;
4708 file->line_start = file->pos;
4709 file->line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004710 file->report_line = 1;
4711 file->report_name = file->basename;
4712 file->report_dir = file->dirname;
Eric Biederman90089602004-05-28 14:11:54 +00004713 file->prev = *filep;
4714 *filep = file;
4715 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004716}
4717
Eric Biederman90089602004-05-28 14:11:54 +00004718static 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}
4726static int if_eat(struct compile_state *state)
4727{
4728 return state->eat_depth > 0;
4729}
4730static 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}
4737static 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}
4748static 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}
4754static 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}
4761static void reenter_if(struct compile_state *state, const char *name)
4762{
4763 in_if(state, name);
4764 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004765 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004766 state->eat_depth = 0;
4767 state->eat_targ = 0;
4768 }
4769}
4770static void enter_else(struct compile_state *state, const char *name)
4771{
4772 in_if(state, name);
4773 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004774 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004775 state->eat_depth = 0;
4776 state->eat_targ = 0;
4777 }
4778}
4779static 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
Eric Biederman41203d92004-11-08 09:31:09 +00004789static void cpp_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004790{
4791 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004792 int rescan;
4793
Eric Biederman90089602004-05-28 14:11:54 +00004794 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004795 do {
4796 rescan = 0;
4797 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004798 /* 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 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004803 state->file = file->prev;
4804 /* file->basename is used keep it */
4805 xfree(file->dirname);
4806 xfree(file->buf);
4807 xfree(file);
Eric Biederman90089602004-05-28 14:11:54 +00004808 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004809 rescan = 1;
4810 }
Eric Biederman41203d92004-11-08 09:31:09 +00004811 } while(rescan);
4812}
4813
4814static void preprocess(struct compile_state *state, struct token *tk);
4815
4816static 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004825 rescan = 1;
4826 }
Eric Biederman41203d92004-11-08 09:31:09 +00004827 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004828 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004829 rescan = compile_macro(state, &state->file, tk);
4830 if (rescan) {
Eric Biederman41203d92004-11-08 09:31:09 +00004831 cpp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004832 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004833 }
Eric Biederman41203d92004-11-08 09:31:09 +00004834 /* Eat tokens disabled by the preprocessor */
Eric Biederman90089602004-05-28 14:11:54 +00004835 else if (if_eat(state)) {
Eric Biederman41203d92004-11-08 09:31:09 +00004836 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)) {
Eric Biederman7dd185c2004-11-09 00:05:42 +00004841 cpp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004842 rescan = 1;
4843 }
4844 } while(rescan);
4845}
4846
Eric Biederman41203d92004-11-08 09:31:09 +00004847
4848static 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
4858static 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
4882static 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
4892static struct token *cpp_eat(struct compile_state *state, int tok)
4893{
4894 cpp_peek(state);
4895 return do_eat_token(state, tok);
4896}
4897
Eric Biedermanb138ac82003-04-22 18:44:01 +00004898static int peek(struct compile_state *state)
4899{
Eric Biederman41203d92004-11-08 09:31:09 +00004900 struct token *tk1;
4901 tk1 = get_token(state, 1);
4902 if (tk1->tok == -1) {
4903 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004904 }
Eric Biederman41203d92004-11-08 09:31:09 +00004905 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004906}
4907
4908static int peek2(struct compile_state *state)
4909{
Eric Biederman41203d92004-11-08 09:31:09 +00004910 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004915 }
Eric Biederman41203d92004-11-08 09:31:09 +00004916 if (tk2->tok == -1) {
4917 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004918 }
Eric Biederman41203d92004-11-08 09:31:09 +00004919 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004920}
4921
Eric Biederman41203d92004-11-08 09:31:09 +00004922static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004923{
Eric Biederman90089602004-05-28 14:11:54 +00004924 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00004925 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004926}
Eric Biedermanb138ac82003-04-22 18:44:01 +00004927
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004928static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004929{
Eric Biederman90089602004-05-28 14:11:54 +00004930 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00004931 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004932 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004954 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 {
Eric Biederman90089602004-05-28 14:11:54 +00004960 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004961 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00004962 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004963 /* 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 }
Eric Biederman90089602004-05-28 14:11:54 +00004971 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004972 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004987
4988 file->pos = file->buf;
4989 file->line_start = file->pos;
4990 file->line = 1;
4991
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004992 file->report_line = 1;
4993 file->report_name = file->basename;
4994 file->report_dir = file->dirname;
4995
Eric Biedermanb138ac82003-04-22 18:44:01 +00004996 file->prev = state->file;
4997 state->file = file;
4998
4999 process_trigraphs(state);
5000 splice_lines(state);
5001}
5002
Eric Biederman41203d92004-11-08 09:31:09 +00005003static struct triple *constant_expr(struct compile_state *state);
5004static void integral(struct compile_state *state, struct triple *def);
5005
5006static 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
5017static 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
Eric Biederman0babc1c2003-05-09 02:39:00 +00005395/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005396
5397static 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005405 result->field_ident = 0;
5406 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005407 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005408 return result;
5409}
5410
5411static 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
Eric Biederman90089602004-05-28 14:11:54 +00005421static 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}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005437
Eric Biederman90089602004-05-28 14:11:54 +00005438
5439static 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}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005493
5494#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005495#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005496static inline ulong_t mask_uint(ulong_t x)
5497{
5498 if (SIZEOF_INT < SIZEOF_LONG) {
Eric Biederman90089602004-05-28 14:11:54 +00005499 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005500 x &= mask;
5501 }
5502 return x;
5503}
5504#define MASK_UINT(X) (mask_uint(X))
5505#define MASK_ULONG(X) (X)
5506
Eric Biederman90089602004-05-28 14:11:54 +00005507static struct type void_type = { .type = TYPE_VOID };
5508static struct type char_type = { .type = TYPE_CHAR };
5509static struct type uchar_type = { .type = TYPE_UCHAR };
5510static struct type short_type = { .type = TYPE_SHORT };
5511static struct type ushort_type = { .type = TYPE_USHORT };
5512static struct type int_type = { .type = TYPE_INT };
5513static struct type uint_type = { .type = TYPE_UINT };
5514static struct type long_type = { .type = TYPE_LONG };
5515static struct type ulong_type = { .type = TYPE_ULONG };
5516static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005517
Eric Biederman5ade04a2003-10-22 04:03:46 +00005518static struct type void_ptr_type = {
5519 .type = TYPE_POINTER,
5520 .left = &void_type,
5521};
5522
5523static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005524 .type = TYPE_FUNCTION,
5525 .left = &void_type,
5526 .right = &void_type,
5527};
5528
Eric Biederman90089602004-05-28 14:11:54 +00005529static size_t bits_to_bytes(size_t size)
5530{
5531 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5532}
5533
Eric Biedermanb138ac82003-04-22 18:44:01 +00005534static struct triple *variable(struct compile_state *state, struct type *type)
5535{
5536 struct triple *result;
5537 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005538 result = triple(state, OP_ADECL, type, 0, 0);
5539 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005540 }
5541 else {
5542 result = triple(state, OP_SDECL, type, 0, 0);
5543 }
5544 return result;
5545}
5546
5547static 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005556 case STOR_LOCAL:
5557 fprintf(fp, "local ");
5558 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005559 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005568 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005569 fprintf(fp, "inline ");
5570 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005571 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005580 }
5581}
5582static 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}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005594
Eric Biedermanb138ac82003-04-22 18:44:01 +00005595static void name_of(FILE *fp, struct type *type)
5596{
Eric Biederman90089602004-05-28 14:11:54 +00005597 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) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005603 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:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005645 name_of(fp, type->left);
5646 fprintf(fp, ", ");
5647 name_of(fp, type->right);
5648 break;
Eric Biederman90089602004-05-28 14:11:54 +00005649 case TYPE_OVERLAP:
5650 name_of(fp, type->left);
5651 fprintf(fp, ",| ");
5652 name_of(fp, type->right);
5653 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005654 case TYPE_ENUM:
Eric Biederman90089602004-05-28 14:11:54 +00005655 fprintf(fp, "enum %s",
5656 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005657 qual_of(fp, type);
5658 break;
5659 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005660 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, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005671 qual_of(fp, type);
5672 break;
5673 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005674 name_of(fp, type->left);
5675 fprintf(fp, " (*)(");
5676 name_of(fp, type->right);
5677 fprintf(fp, ")");
5678 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005679 case TYPE_ARRAY:
5680 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005681 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005682 break;
Eric Biederman90089602004-05-28 14:11:54 +00005683 case TYPE_TUPLE:
5684 fprintf(fp, "tuple { ");
5685 name_of(fp, type->left);
5686 fprintf(fp, " } ");
5687 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005688 break;
Eric Biederman90089602004-05-28 14:11:54 +00005689 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005709 }
5710}
5711
5712static 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;
Eric Biederman90089602004-05-28 14:11:54 +00005720 case TYPE_BITFIELD:
5721 align = 1;
5722 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005723 case TYPE_CHAR:
5724 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005725 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005726 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:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005738 align = ALIGNOF_LONG;
5739 break;
Eric Biederman90089602004-05-28 14:11:54 +00005740 case TYPE_POINTER:
5741 align = ALIGNOF_POINTER;
5742 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005743 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005755 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005756 case TYPE_TUPLE:
5757 case TYPE_UNION:
5758 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005759 align = align_of(state, type->left);
5760 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005761 default:
5762 error(state, 0, "alignof not yet defined for type\n");
5763 break;
5764 }
5765 return align;
5766}
5767
Eric Biederman90089602004-05-28 14:11:54 +00005768static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005769{
Eric Biederman90089602004-05-28 14:11:54 +00005770 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
5824static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5825{
5826 return bits_to_bytes(align_of(state, type));
5827}
5828static size_t size_of(struct compile_state *state, struct type *type);
5829static size_t reg_size_of(struct compile_state *state, struct type *type);
5830
5831static 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 }
Eric Biederman03b59862003-06-24 14:27:37 +00005846 padding = 0;
5847 if (offset % align) {
5848 padding = align - (offset % align);
5849 }
5850 return padding;
5851}
Eric Biederman90089602004-05-28 14:11:54 +00005852
5853static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00005873static 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;
Eric Biederman90089602004-05-28 14:11:54 +00005881 case TYPE_BITFIELD:
5882 size = type->elements;
5883 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005884 case TYPE_CHAR:
5885 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005886 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005887 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:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005899 size = SIZEOF_LONG;
5900 break;
Eric Biederman90089602004-05-28 14:11:54 +00005901 case TYPE_POINTER:
5902 size = SIZEOF_POINTER;
5903 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005904 case TYPE_PRODUCT:
5905 {
Eric Biederman90089602004-05-28 14:11:54 +00005906 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00005907 size = 0;
5908 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00005909 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005910 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00005911 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005912 }
Eric Biederman90089602004-05-28 14:11:54 +00005913 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00005914 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005915 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005932 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005933 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00005934 {
Eric Biederman90089602004-05-28 14:11:54 +00005935 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005936 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00005937 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00005938 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);
Eric Biedermane058a1e2003-07-12 01:21:31 +00005949 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005950 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00005951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005952 default:
Eric Biederman90089602004-05-28 14:11:54 +00005953 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005954 break;
5955 }
5956 return size;
5957}
5958
Eric Biederman90089602004-05-28 14:11:54 +00005959static 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
6045static 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
6054static 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
Eric Biederman0babc1c2003-05-09 02:39:00 +00006059static size_t field_offset(struct compile_state *state,
6060 struct type *type, struct hash_entry *field)
6061{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006062 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006063 size_t size;
6064
Eric Biederman0babc1c2003-05-09 02:39:00 +00006065 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006066 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006077 }
Eric Biederman90089602004-05-28 14:11:54 +00006078 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006079 }
Eric Biederman90089602004-05-28 14:11:54 +00006080 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
6100static 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)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006136 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006137 }
6138 return size;
6139}
6140
6141static struct type *field_type(struct compile_state *state,
6142 struct type *type, struct hash_entry *field)
6143{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006144 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006145
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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006155 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006156 }
Eric Biederman90089602004-05-28 14:11:54 +00006157 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)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006172 error(state, 0, "member %s not present", field->name);
6173 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006174 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006175}
6176
Eric Biederman90089602004-05-28 14:11:54 +00006177static 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
6230static 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
6284static 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
6335static 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
6424static int equiv_types(struct type *left, struct type *right);
6425static int is_compound_type(struct type *type);
6426
6427static 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
Eric Biederman03b59862003-06-24 14:27:37 +00006580static struct type *next_field(struct compile_state *state,
6581 struct type *type, struct type *prev_member)
6582{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006583 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006584 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6585 internal_error(state, 0, "next_field only works on structures");
6586 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006587 member = type->left;
6588 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006589 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006590 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006591 break;
6592 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006593 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006594 prev_member = 0;
6595 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006596 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006597 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006598 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006599 prev_member = 0;
6600 }
6601 if (prev_member) {
6602 internal_error(state, 0, "prev_member %s not present",
6603 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006604 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006605 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006606}
6607
Eric Biederman90089602004-05-28 14:11:54 +00006608typedef 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
6611static 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
6615static 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)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006618{
Eric Biederman90089602004-05-28 14:11:54 +00006619 struct type *tptr;
6620 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006621 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006622 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006623 }
Eric Biederman90089602004-05-28 14:11:54 +00006624 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006630 }
Eric Biederman90089602004-05-28 14:11:54 +00006631 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006638 }
Eric Biederman90089602004-05-28 14:11:54 +00006639
6640}
6641
6642static 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");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006664 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006665}
6666
Eric Biedermanb138ac82003-04-22 18:44:01 +00006667static 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
Eric Biederman90089602004-05-28 14:11:54 +00006677static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00006706static unsigned int do_integral_promotion(unsigned int type)
6707{
Eric Biederman83b991a2003-10-11 06:20:25 +00006708 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006709 type = TYPE_INT;
6710 }
6711 return type;
6712}
6713
6714static unsigned int do_arithmetic_conversion(
6715 unsigned int left, unsigned int right)
6716{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006717 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 */
6753static 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;
Eric Biederman530b5192003-07-01 10:05:30 +00006761 /* If the basic types match and it is a void type we are done */
6762 if (type == TYPE_VOID) {
6763 return 1;
6764 }
Eric Biederman90089602004-05-28 14:11:54 +00006765 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006770 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006771 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006772 return 1;
6773 }
6774 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006775 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006776 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 }
Eric Biederman90089602004-05-28 14:11:54 +00006782 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006783 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006784 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006785 }
Eric Biederman90089602004-05-28 14:11:54 +00006786 /* test for union equality */
6787 else if (type == TYPE_UNION) {
6788 return left->type_ident == right->type_ident;
6789 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006790 /* 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 */
Eric Biederman90089602004-05-28 14:11:54 +00006796 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006797 else if (type == TYPE_PRODUCT) {
6798 return equiv_types(left->left, right->left) &&
6799 equiv_types(left->right, right->right);
6800 }
Eric Biederman90089602004-05-28 14:11:54 +00006801 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006816 else {
6817 return 0;
6818 }
6819}
6820
6821static 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
6830static 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 }
Eric Biederman90089602004-05-28 14:11:54 +00006852 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006853 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006854 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006855 result = left;
6856 }
6857 }
Eric Biederman90089602004-05-28 14:11:54 +00006858 /* test for union equality */
6859 else if (type == TYPE_UNION) {
6860 if (left->type_ident == right->type_ident) {
6861 result = left;
6862 }
6863 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006864 /* 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
Eric Biederman90089602004-05-28 14:11:54 +00006888/* See if left is a equivalent to right or right is a union member of left */
6889static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00006912static 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}
6928static 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;
Eric Biederman90089602004-05-28 14:11:54 +00006940 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00006941 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00006942 if (def->op != OP_LOAD) {
6943 def->type = new_type(int_type, 0, 0);
6944 }
6945 else {
Eric Biederman90089602004-05-28 14:11:54 +00006946 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00006947 new_type(int_type, 0, 0), def, 0);
6948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006949 }
6950 }
6951 return def;
6952}
6953
6954
6955static void arithmetic(struct compile_state *state, struct triple *def)
6956{
6957 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00006958 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006959 }
6960}
6961
6962static 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
6969static int is_integral(struct triple *ins)
6970{
6971 return TYPE_INTEGER(ins->type->type);
6972}
6973
6974static 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
6982static 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
6990static int is_signed(struct type *type)
6991{
Eric Biederman90089602004-05-28 14:11:54 +00006992 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6993 type = type->left;
6994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006995 return !!TYPE_SIGNED(type->type);
6996}
Eric Biederman90089602004-05-28 14:11:54 +00006997static 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}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007014
Eric Biederman0babc1c2003-05-09 02:39:00 +00007015/* Is this value located in a register otherwise it must be in memory */
7016static 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 }
Eric Biederman90089602004-05-28 14:11:54 +00007025 else if (triple_is_part(state, def)) {
7026 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007027 }
7028 else {
Eric Biederman90089602004-05-28 14:11:54 +00007029 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007030 in_reg = -1;
7031 }
7032 return in_reg;
7033}
7034
Eric Biederman90089602004-05-28 14:11:54 +00007035/* 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 */
7038static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007039{
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) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007048 (def->op == OP_BLOBCONST) ||
7049 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007050 ret = 1;
7051 }
Eric Biederman90089602004-05-28 14:11:54 +00007052 else if (triple_is_part(state, def)) {
7053 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007054 }
7055 return ret;
7056}
7057
Eric Biederman00443072003-06-24 12:34:45 +00007058static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007059{
7060 if (!def) {
7061 internal_error(state, def, "nothing where lvalue expected?");
7062 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007063 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007064 error(state, def, "lvalue expected");
7065 }
7066}
Eric Biederman00443072003-06-24 12:34:45 +00007067static 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}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007074
7075static int is_pointer(struct triple *def)
7076{
7077 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7078}
7079
7080static void pointer(struct compile_state *state, struct triple *def)
7081{
7082 if (!is_pointer(def)) {
7083 error(state, def, "pointer expected");
7084 }
7085}
7086
7087static 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:
Eric Biederman90089602004-05-28 14:11:54 +00007097 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007098 }
7099 result = triple(state, OP_INTCONST, type, 0, 0);
7100 result->u.cval = value;
7101 return result;
7102}
7103
7104
Eric Biederman83b991a2003-10-11 06:20:25 +00007105static struct triple *read_expr(struct compile_state *state, struct triple *def);
7106
Eric Biederman0babc1c2003-05-09 02:39:00 +00007107static struct triple *do_mk_addr_expr(struct compile_state *state,
7108 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007109{
7110 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007111 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007112 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007113
Eric Biederman90089602004-05-28 14:11:54 +00007114 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007115
Eric Biederman90089602004-05-28 14:11:54 +00007116
Eric Biedermanb138ac82003-04-22 18:44:01 +00007117 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) {
Eric Biederman90089602004-05-28 14:11:54 +00007122 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007123 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007124 result->u.cval = offset;
7125 }
7126 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007127 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007128 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007129 int_const(state, &ulong_type, offset));
7130 }
Eric Biederman90089602004-05-28 14:11:54 +00007131 else if (expr->op == OP_BLOBCONST) {
7132 FINISHME();
7133 internal_error(state, expr, "not yet implemented");
7134 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007135 else if (expr->op == OP_LIST) {
7136 error(state, 0, "Function addresses not supported");
7137 }
Eric Biederman90089602004-05-28 14:11:54 +00007138 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007155 if (!result) {
7156 internal_error(state, expr, "cannot take address of expression");
7157 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007158 return result;
7159}
7160
Eric Biederman0babc1c2003-05-09 02:39:00 +00007161static struct triple *mk_addr_expr(
7162 struct compile_state *state, struct triple *expr, ulong_t offset)
7163{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007164 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007165}
7166
Eric Biedermanb138ac82003-04-22 18:44:01 +00007167static 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007173 return triple(state, OP_DEREF, base_type, expr, 0);
7174}
7175
Eric Biederman90089602004-05-28 14:11:54 +00007176/* 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 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007180static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007181{
Eric Biederman5cd81732004-03-11 15:01:31 +00007182 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007183 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7184 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007185 type = new_type(
7186 TYPE_POINTER | (def->type->type & QUAL_MASK),
7187 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007188 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007189 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 {
Eric Biederman90089602004-05-28 14:11:54 +00007198 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007199 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007200 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007201 /* 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 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007205 return def;
7206}
7207
Eric Biederman0babc1c2003-05-09 02:39:00 +00007208static 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;
Eric Biederman90089602004-05-28 14:11:54 +00007213 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007214 if (!field) {
7215 internal_error(state, 0, "No field passed to deref_field");
7216 }
7217 result = 0;
7218 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007219 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7220 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007221 error(state, 0, "request for member %s in something not a struct or union",
7222 field->name);
7223 }
Eric Biederman03b59862003-06-24 14:27:37 +00007224 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007225 if ((type->type & STOR_MASK) == STOR_PERM) {
7226 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007227 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007228 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. */
Eric Biederman03b59862003-06-24 14:27:37 +00007233 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007234 result->u.field = field;
7235 }
7236 return result;
7237}
7238
Eric Biederman90089602004-05-28 14:11:54 +00007239static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00007262static struct triple *read_expr(struct compile_state *state, struct triple *def)
7263{
7264 int op;
7265 if (!def) {
7266 return 0;
7267 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007268#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);
Eric Biederman90089602004-05-28 14:11:54 +00007271 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007272 return def;
7273 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007274 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007275 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007276 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007277 if (def->op == OP_SDECL) {
7278 def = mk_addr_expr(state, def, 0);
7279 def = mk_deref_expr(state, def);
7280 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007281 op = OP_LOAD;
7282 }
Eric Biederman90089602004-05-28 14:11:54 +00007283 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007288}
7289
Eric Biedermane058a1e2003-07-12 01:21:31 +00007290int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007291 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007309 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007310 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007311 compatible = 1;
7312 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007313 return compatible;
7314}
7315
Eric Biedermane058a1e2003-07-12 01:21:31 +00007316static void write_compatible(struct compile_state *state,
7317 struct type *dest, struct type *rval)
7318{
7319 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007320 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");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007326 error(state, 0, "Incompatible types in assignment");
7327 }
7328}
7329
Eric Biedermane058a1e2003-07-12 01:21:31 +00007330static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00007343static 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007357 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007358 internal_error(state, 0, "writing to a non lvalue?");
7359 }
Eric Biederman00443072003-06-24 12:34:45 +00007360 if (dest->type->type & QUAL_CONST) {
7361 internal_error(state, 0, "modifable lvalue expexted");
7362 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007363
7364 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007365 if (!equiv_types(dest->type, rval->type)) {
7366 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7367 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007368
7369 /* Now figure out which assignment operator to use */
7370 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007371 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007372 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007379 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007380 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007381 }
Eric Biederman90089602004-05-28 14:11:54 +00007382 if (def->type->type & QUAL_VOLATILE) {
7383 def->id |= TRIPLE_FLAG_VOLATILE;
7384 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007385 return def;
7386}
7387
7388static 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007412 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007413 insert_triple(state, dest, rval);
7414 rval->id |= TRIPLE_FLAG_FLATTENED;
7415 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007416 }
7417 return def;
7418}
7419
7420struct 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(
Eric Biederman90089602004-05-28 14:11:54 +00007429 get_basic_type(left->type),
7430 get_basic_type(right->type)),
7431 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007432 return type;
7433}
7434
7435struct 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00007456/* boolean helper function */
7457
7458static 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
7474static 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
Eric Biederman90089602004-05-28 14:11:54 +00007480static 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;
Eric Biederman41203d92004-11-08 09:31:09 +00007485 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007486
7487 /* Generate some intermediate triples */
7488 end = label(state);
7489 var = variable(state, &int_type);
7490
7491 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007492 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007493
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 */
Eric Biederman41203d92004-11-08 09:31:09 +00007500 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007501
7502 /* An expression for the computed value */
7503 val = read_expr(state, var);
7504
7505 /* Generate the prog for a logical and */
Eric Biederman41203d92004-11-08 09:31:09 +00007506 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0);
Eric Biederman90089602004-05-28 14:11:54 +00007507
7508 return def;
7509}
7510
7511static 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
7540static struct triple *mkcond_expr(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007541 struct compile_state *state,
7542 struct triple *test, struct triple *left, struct triple *right)
7543{
Eric Biederman90089602004-05-28 14:11:54 +00007544 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007545 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 }
Eric Biederman90089602004-05-28 14:11:54 +00007579 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00007603 return def;
7604}
7605
7606
Eric Biederman0babc1c2003-05-09 02:39:00 +00007607static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007608{
Eric Biederman90089602004-05-28 14:11:54 +00007609#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Eric Biedermanb138ac82003-04-22 18:44:01 +00007610 int count;
7611 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007612 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7613 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007614 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007615 else if (ins->op == OP_DEREF) {
7616 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007617 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007618 else if (ins->op == OP_VAL) {
7619 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007620 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007621 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007622 /* Don't figure the depth of a call just guess it is huge */
7623 count = 1000;
7624 }
7625 else {
7626 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007627 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007635 }
7636 }
7637 }
7638 return count + 1;
7639}
7640
Eric Biederman0babc1c2003-05-09 02:39:00 +00007641static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007642 struct compile_state *state, struct triple *first, struct triple *ptr,
7643 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007644{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007645 struct rhs_vector {
7646 int depth;
7647 struct triple **ins;
7648 } vector[MAX_RHS];
7649 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007650 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007651 rhs = ptr->rhs;
7652 lhs = ptr->lhs;
7653 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007654 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007655 ptr->op, tops(ptr->op));
7656 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007657 /* 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007661 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007662 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007669 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007670 if (max != i) {
7671 struct rhs_vector tmp;
7672 tmp = vector[i];
7673 vector[i] = vector[max];
7674 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007675 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007676 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007677 /* 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 }
Eric Biederman90089602004-05-28 14:11:54 +00007682 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007693 }
7694 return ptr;
7695}
7696
Eric Biederman90089602004-05-28 14:11:54 +00007697static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007698 struct compile_state *state, struct triple *first, struct triple *ptr)
7699{
Eric Biederman90089602004-05-28 14:11:54 +00007700 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007711
Eric Biederman90089602004-05-28 14:11:54 +00007712 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007717
Eric Biederman90089602004-05-28 14:11:54 +00007718 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7719 internal_error(state, val, "val not flattened?");
7720 }
7721
7722 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007723}
7724
Eric Biederman90089602004-05-28 14:11:54 +00007725
7726static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007727 struct compile_state *state, struct triple *first, struct triple *ptr)
7728{
Eric Biederman90089602004-05-28 14:11:54 +00007729 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007738 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007739}
7740
7741static 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007749 /* Only flatten triples once */
7750 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7751 return ptr;
7752 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007753 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007754 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007755 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7756 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007757 break;
Eric Biederman90089602004-05-28 14:11:54 +00007758 case OP_PROG:
7759 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007760 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007761 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007762 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007769 break;
7770 case OP_READ:
7771 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007772 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7773 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007774 break;
Eric Biederman90089602004-05-28 14:11:54 +00007775 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007781 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007782 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);
Eric Biederman90089602004-05-28 14:11:54 +00007787 insert_triple(state, first, ptr);
7788 ptr->id |= TRIPLE_FLAG_FLATTENED;
7789 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007790 if (ptr->next != ptr) {
7791 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007792 }
7793 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007794 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);
Eric Biederman90089602004-05-28 14:11:54 +00007798 insert_triple(state, first, ptr);
7799 ptr->id |= TRIPLE_FLAG_FLATTENED;
7800 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007801 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007809 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007810 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007811 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007812 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007813 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007814 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007815 break;
7816 case OP_DEREF:
7817 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007818 ptr = RHS(ptr, 0);
7819 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007820 free_triple(state, orig_ptr);
7821 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007822 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007823 if (RHS(ptr, 0)->op == OP_DEREF) {
7824 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007825 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007826 base = MISC(ptr, 0);
7827 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007828 left = RHS(base, 0);
7829 ptr = triple(state, OP_ADD, left->type,
7830 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007831 int_const(state, &ulong_type, offset));
7832 free_triple(state, base);
7833 }
Eric Biederman90089602004-05-28 14:11:54 +00007834 else {
7835 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007836 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007837 break;
Eric Biederman90089602004-05-28 14:11:54 +00007838 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;
Eric Biederman8d9c1232003-06-17 08:42:17 +00007854 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00007855 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00007856 use_triple(ptr, MISC(ptr, 0));
7857 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007858 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007859 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7860 use_triple(MISC(ptr, 0), ptr);
7861 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00007862 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007863 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00007864 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007870 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00007871 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007872 break;
7873 default:
7874 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00007875 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007876 break;
7877 }
7878 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00007879 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007880 insert_triple(state, first, ptr);
7881 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007882 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007883 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007884 return ptr;
7885}
7886
7887static 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
7898static 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
7918static 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
Eric Biederman90089602004-05-28 14:11:54 +00007938static 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
7958static 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
7978static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00007992static 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) {
Eric Biederman90089602004-05-28 14:11:54 +00007997 /* Careful replace_use modifies the use chain and
7998 * removes use. So we must get a copy of the next
7999 * entry early.
8000 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008001 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008002 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008003 }
8004 if (orig->use) {
8005 internal_error(state, orig, "used after propogate_use");
8006 }
8007}
8008
8009/*
8010 * Code generators
8011 * ===========================
8012 */
8013
Eric Biederman90089602004-05-28 14:11:54 +00008014static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00008023static 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008034 left = read_expr(state, left);
8035 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008036 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008037 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008038 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)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008053 }
8054 return triple(state, OP_ADD, result_type, left, right);
8055}
8056
8057static 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)) {
Eric Biederman90089602004-05-28 14:11:54 +00008065 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)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008080 }
8081 return triple(state, OP_SUB, result_type, left, right);
8082}
8083
8084static 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
8095static 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
8106static 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
8118static 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
8130static 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
Eric Biedermane058a1e2003-07-12 01:21:31 +00008141
Eric Biedermanb138ac82003-04-22 18:44:01 +00008142/*
8143 * Compile time evaluation
8144 * ===========================
8145 */
8146static int is_const(struct triple *ins)
8147{
8148 return IS_CONST_OP(ins->op);
8149}
8150
Eric Biederman83b991a2003-10-11 06:20:25 +00008151static int is_simple_const(struct triple *ins)
8152{
Eric Biederman90089602004-05-28 14:11:54 +00008153 /* 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);
Eric Biederman83b991a2003-10-11 06:20:25 +00008159}
8160
Eric Biedermanb138ac82003-04-22 18:44:01 +00008161static int constants_equal(struct compile_state *state,
8162 struct triple *left, struct triple *right)
8163{
8164 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008165 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8166 equal = 0;
8167 }
8168 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008169 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 {
Eric Biederman90089602004-05-28 14:11:54 +00008187 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008188 lsize = size_of(state, left->type);
8189 rsize = size_of(state, right->type);
8190 if (lsize != rsize) {
8191 break;
8192 }
Eric Biederman90089602004-05-28 14:11:54 +00008193 bytes = bits_to_bytes(lsize);
8194 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008195 equal = 1;
8196 }
8197 break;
8198 }
8199 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008200 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008201 (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
8213static int is_zero(struct triple *ins)
8214{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008215 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008216}
8217
8218static int is_one(struct triple *ins)
8219{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008220 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008221}
8222
Eric Biederman530b5192003-07-01 10:05:30 +00008223static 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}
Eric Biedermanb138ac82003-04-22 18:44:01 +00008239static 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
8253static 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
8267static long_t log2(ulong_t value)
8268{
8269 return bsr(value);
8270}
8271
8272static long_t tlog2(struct triple *ins)
8273{
8274 return log2(ins->u.cval);
8275}
8276
8277static 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
8294static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008295 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008296{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008297 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:
Eric Biederman90089602004-05-28 14:11:54 +00008307 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008308 break;
8309 default:
Eric Biederman90089602004-05-28 14:11:54 +00008310 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");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008314 break;
8315 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008316 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008317 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008318 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008319 return rhs->u.cval;
8320}
8321
Eric Biederman5ade04a2003-10-22 04:03:46 +00008322static long_t read_sconst(struct compile_state *state,
8323 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008324{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008325 return (long_t)(rhs->u.cval);
8326}
8327
Eric Biederman5ade04a2003-10-22 04:03:46 +00008328int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8329{
8330 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008331 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008332 }
8333 return !is_zero(rhs);
8334}
8335
8336int 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)) {
Eric Biederman90089602004-05-28 14:11:54 +00008341 internal_warning(state, ins, "non const passed to const_eq");
8342 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008343 }
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 {
Eric Biederman90089602004-05-28 14:11:54 +00008359 internal_warning(state, ins, "incomparable constants passed to const_eq");
8360 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008361 }
8362 return result;
8363
8364}
8365
8366int 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)) {
Eric Biederman90089602004-05-28 14:11:54 +00008371 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008372 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 {
Eric Biederman90089602004-05-28 14:11:54 +00008399 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008400 result = -2;
8401 }
8402 return result;
8403}
8404
8405int 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)) {
Eric Biederman90089602004-05-28 14:11:54 +00008410 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008411 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 {
Eric Biederman90089602004-05-28 14:11:54 +00008428 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008429 result = -2;
8430 }
8431 return result;
8432}
8433
Eric Biedermanb138ac82003-04-22 18:44:01 +00008434static 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)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008439 if (*expr) {
8440 unuse_triple(*expr, ins);
8441 *expr = 0;
8442 }
8443 }
8444}
8445
8446static 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)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008451 unuse_triple(*expr, ins);
8452 *expr = 0;
8453 }
8454}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008455
Eric Biederman90089602004-05-28 14:11:54 +00008456static 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
8466static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00008477static 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}
Eric Biederman90089602004-05-28 14:11:54 +00008486
8487static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00008498static 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
8507static void wipe_ins(struct compile_state *state, struct triple *ins)
8508{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008509 /* 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 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008513 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008514 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008515 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008516 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008517 ins->lhs = 0;
8518 ins->rhs = 0;
8519 ins->misc = 0;
8520 ins->targ = 0;
8521}
8522
8523static 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008537}
8538
8539static void mkcopy(struct compile_state *state,
8540 struct triple *ins, struct triple *rhs)
8541{
Eric Biederman83b991a2003-10-11 06:20:25 +00008542 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008543 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008552 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008553 wipe_ins(state, ins);
8554 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008555 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008556 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008557 RHS(ins, 0) = rhs;
8558 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008559}
8560
8561static void mkconst(struct compile_state *state,
8562 struct triple *ins, ulong_t value)
8563{
8564 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008565 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008570 }
8571 wipe_ins(state, ins);
8572 ins->op = OP_INTCONST;
8573 ins->u.cval = value;
8574}
8575
8576static void mkaddr_const(struct compile_state *state,
8577 struct triple *ins, struct triple *sdecl, ulong_t value)
8578{
Eric Biederman90089602004-05-28 14:11:54 +00008579 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008580 internal_error(state, ins, "bad base for addrconst");
8581 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008582 wipe_ins(state, ins);
8583 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008584 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008585 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008586 ins->u.cval = value;
8587 use_triple(sdecl, ins);
8588}
8589
Eric Biederman90089602004-05-28 14:11:54 +00008590#if DEBUG_DECOMPOSE_PRINT_TUPLES
8591static void print_tuple(struct compile_state *state,
8592 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008593{
Eric Biederman90089602004-05-28 14:11:54 +00008594 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
8606static 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
8632static 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
8663static 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
8726static 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
8796struct decompose_load_info {
8797 struct occurance *occurance;
8798 struct triple *lval;
8799 struct triple *tuple;
8800};
8801static 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
8816static 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
8838struct decompose_store_info {
8839 struct occurance *occurance;
8840 struct triple *lval;
8841 struct triple *val;
8842 struct triple *tuple;
8843};
8844static 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
8860static 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
8882static 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
8949static 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
8994static void decompose_compound_types(struct compile_state *state)
8995{
8996 struct triple *ins, *next, *first;
8997 FILE *fp;
8998 fp = state->dbgout;
Eric Biederman83b991a2003-10-11 06:20:25 +00008999 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009000 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009001
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.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009052 */
9053 ins = first;
9054 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009055 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009056 if (ins->op == OP_TUPLE) {
9057 if (ins->use) {
9058 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009059 }
Eric Biederman90089602004-05-28 14:11:54 +00009060 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009061 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009062 }
Eric Biederman90089602004-05-28 14:11:54 +00009063 }
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 {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009074 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009075 }
9076 }
9077 ins = next;
9078 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009079
Eric Biederman0babc1c2003-05-09 02:39:00 +00009080 /* Pass three verify the state and set ->id to 0.
9081 */
Eric Biederman90089602004-05-28 14:11:54 +00009082 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009083 do {
Eric Biederman90089602004-05-28 14:11:54 +00009084 ins = next;
9085 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009086 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009087 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009094 }
9095 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009096 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009097 }
Eric Biederman90089602004-05-28 14:11:54 +00009098 if (ins->op == OP_INDEX) {
9099 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009100 }
Eric Biederman90089602004-05-28 14:11:54 +00009101 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009108}
9109
Eric Biedermanb138ac82003-04-22 18:44:01 +00009110/* For those operations that cannot be simplified */
9111static void simplify_noop(struct compile_state *state, struct triple *ins)
9112{
9113 return;
9114}
9115
9116static void simplify_smul(struct compile_state *state, struct triple *ins)
9117{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009118 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009119 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009120 tmp = RHS(ins, 0);
9121 RHS(ins, 0) = RHS(ins, 1);
9122 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009123 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009124 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009125 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009126 left = read_sconst(state, ins, RHS(ins, 0));
9127 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009128 mkconst(state, ins, left * right);
9129 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009130 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009131 mkconst(state, ins, 0);
9132 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009133 else if (is_one(RHS(ins, 1))) {
9134 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009135 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009136 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009137 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009138 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009139 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009140 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009141 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009142 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009143 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009144 }
9145}
9146
9147static void simplify_umul(struct compile_state *state, struct triple *ins)
9148{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009149 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009150 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009151 tmp = RHS(ins, 0);
9152 RHS(ins, 0) = RHS(ins, 1);
9153 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009154 }
Eric Biederman90089602004-05-28 14:11:54 +00009155 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009156 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009157 left = read_const(state, ins, RHS(ins, 0));
9158 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009159 mkconst(state, ins, left * right);
9160 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009161 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009162 mkconst(state, ins, 0);
9163 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009164 else if (is_one(RHS(ins, 1))) {
9165 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009166 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009167 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009168 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009169 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009170 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009171 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009172 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009173 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009174 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009175 }
9176}
9177
9178static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9179{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009180 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009181 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009182 left = read_sconst(state, ins, RHS(ins, 0));
9183 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009184 mkconst(state, ins, left / right);
9185 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009186 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009187 mkconst(state, ins, 0);
9188 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009189 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009190 error(state, ins, "division by zero");
9191 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009192 else if (is_one(RHS(ins, 1))) {
9193 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009194 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009195 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009196 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009197 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009198 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009199 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009200 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009201 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009202 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009203 }
9204}
9205
9206static void simplify_udiv(struct compile_state *state, struct triple *ins)
9207{
Eric Biederman90089602004-05-28 14:11:54 +00009208 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009209 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009210 left = read_const(state, ins, RHS(ins, 0));
9211 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009212 mkconst(state, ins, left / right);
9213 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009214 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009215 mkconst(state, ins, 0);
9216 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009217 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009218 error(state, ins, "division by zero");
9219 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009220 else if (is_one(RHS(ins, 1))) {
9221 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009222 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009223 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009224 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009225 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009226 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009227 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009228 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009229 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009230 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009231 }
9232}
9233
9234static void simplify_smod(struct compile_state *state, struct triple *ins)
9235{
Eric Biederman90089602004-05-28 14:11:54 +00009236 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009237 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009238 left = read_const(state, ins, RHS(ins, 0));
9239 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009240 mkconst(state, ins, left % right);
9241 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009242 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009243 mkconst(state, ins, 0);
9244 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009245 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009246 error(state, ins, "division by zero");
9247 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009248 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009249 mkconst(state, ins, 0);
9250 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009251 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009252 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009253 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009254 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009255 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009256 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009257 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009258 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009259 }
9260}
Eric Biederman83b991a2003-10-11 06:20:25 +00009261
Eric Biedermanb138ac82003-04-22 18:44:01 +00009262static void simplify_umod(struct compile_state *state, struct triple *ins)
9263{
Eric Biederman90089602004-05-28 14:11:54 +00009264 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009265 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009266 left = read_const(state, ins, RHS(ins, 0));
9267 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009268 mkconst(state, ins, left % right);
9269 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009270 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009271 mkconst(state, ins, 0);
9272 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009273 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009274 error(state, ins, "division by zero");
9275 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009276 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009277 mkconst(state, ins, 0);
9278 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009279 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009280 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009281 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009282 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009283 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009284 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009285 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009286 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009287 }
9288}
9289
9290static void simplify_add(struct compile_state *state, struct triple *ins)
9291{
9292 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009293 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009294 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009295 tmp = RHS(ins, 0);
9296 RHS(ins, 0) = RHS(ins, 1);
9297 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009298 }
Eric Biederman90089602004-05-28 14:11:54 +00009299 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009300 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009301 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009302 left = read_const(state, ins, RHS(ins, 0));
9303 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 mkconst(state, ins, left + right);
9305 }
Eric Biederman530b5192003-07-01 10:05:30 +00009306 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009307 struct triple *sdecl;
9308 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009309 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009310 left = RHS(ins, 0)->u.cval;
9311 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009312 mkaddr_const(state, ins, sdecl, left + right);
9313 }
Eric Biederman530b5192003-07-01 10:05:30 +00009314 else {
9315 internal_warning(state, ins, "Optimize me!");
9316 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009317 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009318 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009320 tmp = RHS(ins, 1);
9321 RHS(ins, 1) = RHS(ins, 0);
9322 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009323 }
9324}
9325
9326static void simplify_sub(struct compile_state *state, struct triple *ins)
9327{
Eric Biederman90089602004-05-28 14:11:54 +00009328 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009329 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009330 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009331 left = read_const(state, ins, RHS(ins, 0));
9332 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 mkconst(state, ins, left - right);
9334 }
Eric Biederman530b5192003-07-01 10:05:30 +00009335 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009336 struct triple *sdecl;
9337 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009338 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009339 left = RHS(ins, 0)->u.cval;
9340 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009341 mkaddr_const(state, ins, sdecl, left - right);
9342 }
Eric Biederman530b5192003-07-01 10:05:30 +00009343 else {
9344 internal_warning(state, ins, "Optimize me!");
9345 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 }
9347}
9348
9349static void simplify_sl(struct compile_state *state, struct triple *ins)
9350{
Eric Biederman90089602004-05-28 14:11:54 +00009351 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009352 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009353 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009354 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009355 warning(state, ins, "left shift count >= width of type");
9356 }
9357 }
Eric Biederman90089602004-05-28 14:11:54 +00009358 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009359 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009360 left = read_const(state, ins, RHS(ins, 0));
9361 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009362 mkconst(state, ins, left << right);
9363 }
9364}
9365
9366static void simplify_usr(struct compile_state *state, struct triple *ins)
9367{
Eric Biederman90089602004-05-28 14:11:54 +00009368 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009369 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009370 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009371 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009372 warning(state, ins, "right shift count >= width of type");
9373 }
9374 }
Eric Biederman90089602004-05-28 14:11:54 +00009375 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009376 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009377 left = read_const(state, ins, RHS(ins, 0));
9378 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009379 mkconst(state, ins, left >> right);
9380 }
9381}
9382
9383static void simplify_ssr(struct compile_state *state, struct triple *ins)
9384{
Eric Biederman90089602004-05-28 14:11:54 +00009385 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009386 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009387 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009388 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 warning(state, ins, "right shift count >= width of type");
9390 }
9391 }
Eric Biederman90089602004-05-28 14:11:54 +00009392 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009393 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009394 left = read_sconst(state, ins, RHS(ins, 0));
9395 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 mkconst(state, ins, left >> right);
9397 }
9398}
9399
9400static void simplify_and(struct compile_state *state, struct triple *ins)
9401{
Eric Biederman90089602004-05-28 14:11:54 +00009402 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 }
9415}
9416
9417static void simplify_or(struct compile_state *state, struct triple *ins)
9418{
Eric Biederman90089602004-05-28 14:11:54 +00009419 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428 }
Eric Biederman90089602004-05-28 14:11:54 +00009429#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
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437}
9438
9439static void simplify_xor(struct compile_state *state, struct triple *ins)
9440{
Eric Biederman90089602004-05-28 14:11:54 +00009441 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009443 left = read_const(state, ins, RHS(ins, 0));
9444 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 mkconst(state, ins, left ^ right);
9446 }
9447}
9448
9449static void simplify_pos(struct compile_state *state, struct triple *ins)
9450{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009451 if (is_const(RHS(ins, 0))) {
9452 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009453 }
9454 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009455 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009456 }
9457}
9458
9459static void simplify_neg(struct compile_state *state, struct triple *ins)
9460{
Eric Biederman90089602004-05-28 14:11:54 +00009461 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009462 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009463 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009464 mkconst(state, ins, -left);
9465 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009466 else if (RHS(ins, 0)->op == OP_NEG) {
9467 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009468 }
9469}
9470
9471static void simplify_invert(struct compile_state *state, struct triple *ins)
9472{
Eric Biederman90089602004-05-28 14:11:54 +00009473 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009474 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009475 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009476 mkconst(state, ins, ~left);
9477 }
9478}
9479
9480static void simplify_eq(struct compile_state *state, struct triple *ins)
9481{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009482 struct triple *left, *right;
9483 left = RHS(ins, 0);
9484 right = RHS(ins, 1);
9485
9486 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009487 int val;
9488 val = const_eq(state, ins, left, right);
9489 if (val >= 0) {
9490 mkconst(state, ins, val == 1);
9491 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009492 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009493 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009494 mkconst(state, ins, 1);
9495 }
9496}
9497
9498static void simplify_noteq(struct compile_state *state, struct triple *ins)
9499{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009500 struct triple *left, *right;
9501 left = RHS(ins, 0);
9502 right = RHS(ins, 1);
9503
9504 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009505 int val;
9506 val = const_eq(state, ins, left, right);
9507 if (val >= 0) {
9508 mkconst(state, ins, val != 1);
9509 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009510 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009511 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009512 mkconst(state, ins, 0);
9513 }
9514}
9515
9516static void simplify_sless(struct compile_state *state, struct triple *ins)
9517{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009518 struct triple *left, *right;
9519 left = RHS(ins, 0);
9520 right = RHS(ins, 1);
9521
9522 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009523 int val;
9524 val = const_scmp(state, ins, left, right);
9525 if ((val >= -1) && (val <= 1)) {
9526 mkconst(state, ins, val < 0);
9527 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009528 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009529 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009530 mkconst(state, ins, 0);
9531 }
9532}
9533
9534static void simplify_uless(struct compile_state *state, struct triple *ins)
9535{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009536 struct triple *left, *right;
9537 left = RHS(ins, 0);
9538 right = RHS(ins, 1);
9539
9540 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009541 int val;
9542 val = const_ucmp(state, ins, left, right);
9543 if ((val >= -1) && (val <= 1)) {
9544 mkconst(state, ins, val < 0);
9545 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009546 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009547 else if (is_zero(right)) {
9548 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009550 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009551 mkconst(state, ins, 0);
9552 }
9553}
9554
9555static void simplify_smore(struct compile_state *state, struct triple *ins)
9556{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009557 struct triple *left, *right;
9558 left = RHS(ins, 0);
9559 right = RHS(ins, 1);
9560
9561 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009562 int val;
9563 val = const_scmp(state, ins, left, right);
9564 if ((val >= -1) && (val <= 1)) {
9565 mkconst(state, ins, val > 0);
9566 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009567 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009568 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009569 mkconst(state, ins, 0);
9570 }
9571}
9572
9573static void simplify_umore(struct compile_state *state, struct triple *ins)
9574{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009575 struct triple *left, *right;
9576 left = RHS(ins, 0);
9577 right = RHS(ins, 1);
9578
9579 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009580 int val;
9581 val = const_ucmp(state, ins, left, right);
9582 if ((val >= -1) && (val <= 1)) {
9583 mkconst(state, ins, val > 0);
9584 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009585 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009586 else if (is_zero(left)) {
9587 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009588 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009589 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009590 mkconst(state, ins, 0);
9591 }
9592}
9593
9594
9595static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9596{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009597 struct triple *left, *right;
9598 left = RHS(ins, 0);
9599 right = RHS(ins, 1);
9600
9601 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009602 int val;
9603 val = const_scmp(state, ins, left, right);
9604 if ((val >= -1) && (val <= 1)) {
9605 mkconst(state, ins, val <= 0);
9606 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009607 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009608 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009609 mkconst(state, ins, 1);
9610 }
9611}
9612
9613static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9614{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009615 struct triple *left, *right;
9616 left = RHS(ins, 0);
9617 right = RHS(ins, 1);
9618
9619 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009620 int val;
9621 val = const_ucmp(state, ins, left, right);
9622 if ((val >= -1) && (val <= 1)) {
9623 mkconst(state, ins, val <= 0);
9624 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009625 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009626 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 mkconst(state, ins, 1);
9628 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009629 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009630 mkconst(state, ins, 1);
9631 }
9632}
9633
9634static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9635{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009636 struct triple *left, *right;
9637 left = RHS(ins, 0);
9638 right = RHS(ins, 1);
9639
9640 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009641 int val;
9642 val = const_scmp(state, ins, left, right);
9643 if ((val >= -1) && (val <= 1)) {
9644 mkconst(state, ins, val >= 0);
9645 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009646 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009647 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009648 mkconst(state, ins, 1);
9649 }
9650}
9651
9652static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9653{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009654 struct triple *left, *right;
9655 left = RHS(ins, 0);
9656 right = RHS(ins, 1);
9657
9658 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009659 int val;
9660 val = const_ucmp(state, ins, left, right);
9661 if ((val >= -1) && (val <= 1)) {
9662 mkconst(state, ins, val >= 0);
9663 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009664 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009665 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009666 mkconst(state, ins, 1);
9667 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009668 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009669 mkconst(state, ins, 1);
9670 }
9671}
9672
9673static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9674{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009675 struct triple *rhs;
9676 rhs = RHS(ins, 0);
9677
9678 if (is_const(rhs)) {
9679 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009680 }
9681 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009682 else if ((rhs->use) &&
9683 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009684 int need_copy = 1;
9685 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009686 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009699 default:
9700 need_copy = 0;
9701 break;
9702 }
9703 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009704 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009705 }
9706 }
9707}
9708
9709static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9710{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009711 struct triple *rhs;
9712 rhs = RHS(ins, 0);
9713
9714 if (is_const(rhs)) {
9715 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009716 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009717 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009718 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:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009721 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009722 }
9723
9724}
9725
Eric Biederman90089602004-05-28 14:11:54 +00009726static 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;
Eric Biederman7dea9552004-06-29 05:38:37 +00009739 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009740 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);
Eric Biederman7dea9552004-06-29 05:38:37 +00009746 end = blob->u.blob;
9747 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009748 src = blob->u.blob;
9749 src += addr->u.cval;
9750
Eric Biederman7dea9552004-06-29 05:38:37 +00009751 if (src > end) {
9752 error(state, ins, "Load address out of bounds");
9753 }
9754
Eric Biederman90089602004-05-28 14:11:54 +00009755 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
9773static 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
9788static 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
9807static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +00009826static void simplify_copy(struct compile_state *state, struct triple *ins)
9827{
Eric Biederman90089602004-05-28 14:11:54 +00009828 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) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009849 case OP_INTCONST:
9850 {
9851 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +00009852 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009872 mkconst(state, ins, left);
9873 break;
9874 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009875 default:
9876 internal_error(state, ins, "uknown constant");
9877 break;
9878 }
9879 }
9880}
9881
Eric Biederman83b991a2003-10-11 06:20:25 +00009882static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +00009883{
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
Eric Biederman83b991a2003-10-11 06:20:25 +00009898static 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 */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009904 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +00009911 }
9912 return 0;
9913}
9914
9915static 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 */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009923 while(triple_is_structural(state, targ) &&
9924 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +00009925 targ = targ->next;
9926 }
9927 return targ;
9928}
9929
9930
9931static void simplify_branch(struct compile_state *state, struct triple *ins)
9932{
Eric Biederman90089602004-05-28 14:11:54 +00009933 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009934 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +00009935 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
Eric Biederman90089602004-05-28 14:11:54 +00009948 * functions.
9949 * Also only do this a limited number of times so
9950 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +00009951 */
Eric Biederman90089602004-05-28 14:11:54 +00009952 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +00009953 do {
9954 struct triple *targ;
9955 simplified = 0;
9956 targ = branch_target(state, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +00009957 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;
Eric Biederman83b991a2003-10-11 06:20:25 +00009964 }
Eric Biederman90089602004-05-28 14:11:54 +00009965 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +00009966
9967 /* If we have a conditional branch with a constant condition
9968 * make it an unconditional branch.
9969 */
Eric Biederman90089602004-05-28 14:11:54 +00009970 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +00009971 struct triple *targ;
9972 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009973 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +00009974 unuse_triple(RHS(ins, 0), ins);
9975 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00009976 ins->rhs = 0;
9977 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009978 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +00009979 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 }
Eric Biederman90089602004-05-28 14:11:54 +00009988
9989 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +00009990 * make it a noop.
9991 */
9992 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +00009993 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +00009994 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +00009995 unuse_triple(RHS(ins, 0), ins);
9996 unuse_triple(ins->next, ins);
9997 }
Eric Biederman90089602004-05-28 14:11:54 +00009998 ins->lhs = 0;
9999 ins->rhs = 0;
10000 ins->misc = 0;
10001 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010002 ins->op = OP_NOOP;
10003 if (ins->use) {
10004 internal_error(state, ins, "noop use != 0");
10005 }
10006 }
10007}
10008
Eric Biederman530b5192003-07-01 10:05:30 +000010009static void simplify_label(struct compile_state *state, struct triple *ins)
10010{
Eric Biederman83b991a2003-10-11 06:20:25 +000010011 /* Ignore volatile labels */
10012 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010013 return;
10014 }
10015 if (ins->use == 0) {
10016 ins->op = OP_NOOP;
10017 }
10018 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010019 /* 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 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010023 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010024 struct triple_set *user, *next;
10025 ins->op = OP_NOOP;
10026 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010027 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010028 next = user->next;
10029 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010030 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
Eric Biederman530b5192003-07-01 10:05:30 +000010038 }
10039 }
10040 if (ins->use) {
10041 internal_error(state, ins, "noop use != 0");
10042 }
10043 }
10044 }
10045}
10046
Eric Biedermanb138ac82003-04-22 18:44:01 +000010047static void simplify_phi(struct compile_state *state, struct triple *ins)
10048{
Eric Biederman83b991a2003-10-11 06:20:25 +000010049 struct triple **slot;
10050 struct triple *value;
10051 int zrhs, i;
10052 ulong_t cvalue;
10053 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010054 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010055 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010056 return;
10057 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010058 /* See if all of the rhs members of a phi have the same value */
10059 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010060 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010061 for(i = 1; i < zrhs; i++) {
10062 if ( !slot[i] ||
10063 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010064 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010065 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010066 break;
10067 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010068 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010069 if (i == zrhs) {
10070 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010071 return;
10072 }
10073 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010074
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 */
Eric Biederman90089602004-05-28 14:11:54 +000010084 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 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010092 mkcopy(state, ins, value);
10093 return;
10094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010095}
10096
10097
10098static void simplify_bsf(struct compile_state *state, struct triple *ins)
10099{
Eric Biederman90089602004-05-28 14:11:54 +000010100 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010101 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010102 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010103 mkconst(state, ins, bsf(left));
10104 }
10105}
10106
10107static void simplify_bsr(struct compile_state *state, struct triple *ins)
10108{
Eric Biederman90089602004-05-28 14:11:54 +000010109 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010110 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010111 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010112 mkconst(state, ins, bsr(left));
10113 }
10114}
10115
10116
10117typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010118static const struct simplify_table {
10119 simplify_t func;
10120 unsigned long flag;
10121} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010122#define simplify_sdivt simplify_noop
10123#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010124#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010125
Eric Biederman5ade04a2003-10-22 04:03:46 +000010126[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 },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010145
Eric Biederman5ade04a2003-10-22 04:03:46 +000010146[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 },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010158
Eric Biederman90089602004-05-28 14:11:54 +000010159[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010160[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010161
Eric Biederman90089602004-05-28 14:11:54 +000010162[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10163[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10164[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10165
Eric Biederman5ade04a2003-10-22 04:03:46 +000010166[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010167
Eric Biederman5ade04a2003-10-22 04:03:46 +000010168[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10169[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10170[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010171[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010172
Eric Biederman5ade04a2003-10-22 04:03:46 +000010173[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10174[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10175[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010176[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010177[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10178[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010179
Eric Biederman5ade04a2003-10-22 04:03:46 +000010180[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010181[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010182
Eric Biederman5ade04a2003-10-22 04:03:46 +000010183[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 },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010192
Eric Biederman5ade04a2003-10-22 04:03:46 +000010193[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 },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010204};
10205
Eric Biederman90089602004-05-28 14:11:54 +000010206static 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}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010225static void simplify(struct compile_state *state, struct triple *ins)
10226{
10227 int op;
10228 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010229 if (ins == &unknown_triple) {
10230 internal_error(state, ins, "simplifying the unknown triple?");
10231 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010232 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 }
Eric Biederman90089602004-05-28 14:11:54 +000010238 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010239 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010240 }
Eric Biederman90089602004-05-28 14:11:54 +000010241 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)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010246 do_simplify = simplify_noop;
10247 }
10248
Eric Biedermanb138ac82003-04-22 18:44:01 +000010249 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010250 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010251 op, tops(op));
10252 return;
10253 }
Eric Biederman90089602004-05-28 14:11:54 +000010254 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010255 } while(ins->op != op);
10256}
10257
Eric Biederman5ade04a2003-10-22 04:03:46 +000010258static void rebuild_ssa_form(struct compile_state *state);
10259
Eric Biedermanb138ac82003-04-22 18:44:01 +000010260static void simplify_all(struct compile_state *state)
10261{
10262 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010263 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10264 return;
10265 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010266 first = state->first;
10267 ins = first->prev;
10268 do {
10269 simplify(state, ins);
10270 ins = ins->prev;
10271 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010272 ins = first;
10273 do {
10274 simplify(state, ins);
10275 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010276 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010277 rebuild_ssa_form(state);
10278
Eric Biederman90089602004-05-28 14:11:54 +000010279 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010280}
10281
10282/*
10283 * Builtins....
10284 * ============================
10285 */
10286
Eric Biederman0babc1c2003-05-09 02:39:00 +000010287static void register_builtin_function(struct compile_state *state,
10288 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010289{
Eric Biederman90089602004-05-28 14:11:54 +000010290 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010291 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010292 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010293 struct file_state file;
10294 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010295 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010296 va_list args;
10297 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010298
10299 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010300 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010301 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010302 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010303 file.report_line = 1;
10304 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010305 file.prev = state->file;
10306 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010307 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010308
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 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010317 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010318 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010319 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
Eric Biederman90089602004-05-28 14:11:54 +000010335 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000010343 /* Generate the needed triples */
10344 def = triple(state, OP_LIST, ftype, 0, 0);
10345 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010346 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010347 result = flatten(state, first, variable(state, crtype));
10348 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010349 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010350
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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010361 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010362 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010363 generate_lhs_pieces(state, work);
10364 for(i = 0; i < parameters; i++) {
10365 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010366 }
Eric Biederman90089602004-05-28 14:11:54 +000010367 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10368 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010369 }
10370 work = flatten(state, first, work);
10371 last = flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010372 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010373 name_len = strlen(name);
10374 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010375 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010376 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010377
Eric Biedermanb138ac82003-04-22 18:44:01 +000010378 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010379 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010380 state->main_function = 0;
10381
Eric Biederman5ade04a2003-10-22 04:03:46 +000010382 if (!state->functions) {
10383 state->functions = def;
10384 } else {
10385 insert_triple(state, state->functions, def);
10386 }
10387 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010388 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__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010394 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010395}
10396
Eric Biederman0babc1c2003-05-09 02:39:00 +000010397static struct type *partial_struct(struct compile_state *state,
10398 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010399{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010400 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
10416static struct type *register_builtin_type(struct compile_state *state,
10417 const char *name, struct type *type)
10418{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010419 struct hash_entry *ident;
10420 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010421
Eric Biedermanb138ac82003-04-22 18:44:01 +000010422 name_len = strlen(name);
10423 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010424
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++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010435 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010436 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010442}
10443
Eric Biederman0babc1c2003-05-09 02:39:00 +000010444
Eric Biedermanb138ac82003-04-22 18:44:01 +000010445static void register_builtins(struct compile_state *state)
10446{
Eric Biederman530b5192003-07-01 10:05:30 +000010447 struct type *div_type, *ldiv_type;
10448 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010449 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010450
Eric Biederman530b5192003-07-01 10:05:30 +000010451 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
Eric Biederman0babc1c2003-05-09 02:39:00 +000010473 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010486
Eric Biederman0babc1c2003-05-09 02:39:00 +000010487 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010500
Eric Biederman0babc1c2003-05-09 02:39:00 +000010501 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10502 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010503}
10504
10505static struct type *declarator(
10506 struct compile_state *state, struct type *type,
10507 struct hash_entry **ident, int need_ident);
10508static void decl(struct compile_state *state, struct triple *first);
10509static struct type *specifier_qualifier_list(struct compile_state *state);
10510static int isdecl_specifier(int tok);
10511static struct type *decl_specifiers(struct compile_state *state);
10512static int istype(int tok);
10513static struct triple *expr(struct compile_state *state);
10514static struct triple *assignment_expr(struct compile_state *state);
10515static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010516static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010517
10518static struct triple *call_expr(
10519 struct compile_state *state, struct triple *func)
10520{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010521 struct triple *def;
10522 struct type *param, *type;
10523 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010524
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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010534 /* 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010545 MISC(def, 0) = func;
10546
10547 param = func->type->right;
10548 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010549 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010550 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010551 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010552 arg_type = param;
10553 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10554 arg_type = param->left;
10555 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010556 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010557 RHS(def, index) = val;
10558 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010559 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010560 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010561 }
10562 }
10563 eat(state, TOK_RPAREN);
10564 return def;
10565}
10566
10567
10568static 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;
Eric Biederman41203d92004-11-08 09:31:09 +000010575 tk = eat(state, TOK_LIT_CHAR);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010576 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
10590static 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 {
Eric Biederman41203d92004-11-08 09:31:09 +000010604 tk = eat(state, TOK_LIT_STRING);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010605 str = tk->val.str + 1;
10606 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010607 if (str_len < 0) {
10608 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010609 }
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;
Eric Biederman90089602004-05-28 14:11:54 +000010624
Eric Biedermanb138ac82003-04-22 18:44:01 +000010625 return def;
10626}
10627
10628
10629static 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
Eric Biederman41203d92004-11-08 09:31:09 +000010638 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010639 errno = 0;
10640 decimal = (tk->val.str[0] != '0');
10641 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010642 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010643 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000010666 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010667 type = &ulong_type;
10668 }
10669 }
10670 else if (u) {
10671 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010672 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010673 type = &ulong_type;
10674 }
10675 }
10676 else {
10677 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010678 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010679 type = &uint_type;
10680 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010681 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010682 type = &ulong_type;
10683 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010684 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010685 type = &long_type;
10686 }
10687 }
10688 def = int_const(state, type, val);
10689 return def;
10690}
10691
10692static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000010704 */
Eric Biederman41203d92004-11-08 09:31:09 +000010705 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010706 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:
Eric Biederman83b991a2003-10-11 06:20:25 +000010713 {
10714 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010715 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010716 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010717 if (!ident->sym_ident) {
10718 error(state, 0, "%s undeclared", ident->name);
10719 }
10720 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010721 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010722 }
Eric Biederman41203d92004-11-08 09:31:09 +000010723 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010732 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
10759static 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:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010779 {
10780 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010781 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010782 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010783 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010784 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010785 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010786 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010787 {
10788 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010789 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010790 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010791 def = mk_deref_expr(state, read_expr(state, def));
10792 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010793 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010794 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010795 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
10811static struct triple *cast_expr(struct compile_state *state);
10812
10813static 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 }
Eric Biederman90089602004-05-28 14:11:54 +000010878 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010879 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 }
Eric Biederman90089602004-05-28 14:11:54 +000010899 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010900 break;
10901 }
Eric Biederman41203d92004-11-08 09:31:09 +000010902 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010920 default:
10921 def = postfix_expr(state);
10922 break;
10923 }
10924 return def;
10925}
10926
10927static 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);
Eric Biedermane058a1e2003-07-12 01:21:31 +000010938 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010939 }
10940 else {
10941 def = unary_expr(state);
10942 }
10943 return def;
10944}
10945
10946static 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
10986static 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
11010static 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
11045static 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
11088static 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
11117static 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
11135static 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
11153static 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
11171static 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
Eric Biederman90089602004-05-28 14:11:54 +000011183 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011184 ltrue_expr(state, left),
11185 ltrue_expr(state, right));
11186 }
11187 return def;
11188}
11189
11190static 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);
Eric Biederman90089602004-05-28 14:11:54 +000011201
11202 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011203 ltrue_expr(state, left),
11204 ltrue_expr(state, right));
11205 }
11206 return def;
11207}
11208
11209static 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
Eric Biederman90089602004-05-28 14:11:54 +000011222 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011223 }
11224 return def;
11225}
11226
Eric Biederman41203d92004-11-08 09:31:09 +000011227struct cv_triple {
11228 struct triple *val;
11229 int id;
11230};
11231
11232static 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}
11240static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000011246static struct triple *eval_const_expr(
11247 struct compile_state *state, struct triple *expr)
11248{
11249 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011250 if (is_const(expr)) {
11251 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011252 }
Eric Biederman00443072003-06-24 12:34:45 +000011253 else {
11254 /* If we don't start out as a constant simplify into one */
11255 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011256 struct cv_triple *cv;
11257 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011258 head = label(state); /* dummy initial triple */
11259 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011260 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011261 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011262 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011263 }
Eric Biederman41203d92004-11-08 09:31:09 +000011264 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++;
Eric Biederman00443072003-06-24 12:34:45 +000011271 }
Eric Biederman41203d92004-11-08 09:31:09 +000011272 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);
Eric Biederman00443072003-06-24 12:34:45 +000011353 /* Free the intermediate expressions */
11354 while(head->next != head) {
11355 release_triple(state, head->next);
11356 }
11357 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011358 }
Eric Biederman41203d92004-11-08 09:31:09 +000011359 if (!is_const(def)) {
11360 error(state, expr, "Not a constant expression");
11361 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011362 return def;
11363}
11364
11365static struct triple *constant_expr(struct compile_state *state)
11366{
11367 return eval_const_expr(state, conditional_expr(state));
11368}
11369
11370static 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:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011403 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011415 }
11416 def = write_expr(state, left,
11417 triple(state, op, left->type,
11418 read_expr(state, left), right));
11419 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011420 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011432 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
11460static struct triple *expr(struct compile_state *state)
11461{
11462 struct triple *def;
11463 def = assignment_expr(state);
11464 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011465 eat(state, TOK_COMMA);
Eric Biederman90089602004-05-28 14:11:54 +000011466 def = mkprog(state, def, assignment_expr(state), 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011467 }
11468 return def;
11469}
11470
11471static void expr_statement(struct compile_state *state, struct triple *first)
11472{
11473 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011474 /* 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.
Eric Biederman5cd81732004-03-11 15:01:31 +000011477 */
11478 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011479 }
11480 eat(state, TOK_SEMI);
11481}
11482
11483static 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011497 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011498 /* 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011507 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011508 /* 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
11519static 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) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011547 jmp1 = branch(state, label3, 0);
11548 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011549 }
11550 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011551 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011552 }
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
11575static 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011588 jmp1 = branch(state, label2, 0);
11589 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011590 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
11609static 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011640 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011641 flatten(state, first, end);
11642}
11643
11644
11645static 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 */
Eric Biederman90089602004-05-28 14:11:54 +000011664 var = fresult(state, state->main_function);
11665
Eric Biedermanb138ac82003-04-22 18:44:01 +000011666 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011667 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011668 mv = jmp = 0;
11669 /* If needed generate a jump instruction */
11670 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011671 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011672 }
11673 /* If needed generate an assignment instruction */
11674 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011675 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011676 }
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
11687static 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011696 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011697}
11698
11699static 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011708 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011709}
11710
11711static void goto_statement(struct compile_state *state, struct triple *first)
11712{
Eric Biederman153ea352003-06-20 14:43:20 +000011713 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011714 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011715 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011716 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);
Eric Biederman90089602004-05-28 14:11:54 +000011722 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011723 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011724 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011725
11726 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011727}
11728
11729static void labeled_statement(struct compile_state *state, struct triple *first)
11730{
Eric Biederman153ea352003-06-20 14:43:20 +000011731 struct triple *ins;
11732 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011733
Eric Biederman41203d92004-11-08 09:31:09 +000011734 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011735 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);
Eric Biederman90089602004-05-28 14:11:54 +000011742 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011743 }
11744 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11745 error(state, 0, "label %s already defined", ident->name);
11746 }
11747 flatten(state, first, ins);
11748
Eric Biedermanb138ac82003-04-22 18:44:01 +000011749 eat(state, TOK_COLON);
11750 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011751}
11752
11753static void switch_statement(struct compile_state *state, struct triple *first)
11754{
Eric Biederman83b991a2003-10-11 06:20:25 +000011755 struct triple *value, *top, *end, *dbranch;
11756 struct hash_entry *ident;
11757
11758 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011759 eat(state, TOK_SWITCH);
11760 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011761 value = expr(state);
11762 integral(state, value);
11763 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011764 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011765 /* 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011783 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011784 flatten(state, first, end);
11785 /* Cleanup the switch scope */
11786 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011787}
11788
11789static void case_statement(struct compile_state *state, struct triple *first)
11790{
Eric Biederman83b991a2003-10-11 06:20:25 +000011791 struct triple *cvalue, *dest, *test, *jmp;
11792 struct triple *ptr, *value, *top, *dbranch;
11793
11794 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011795 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011796 cvalue = constant_expr(state);
11797 integral(state, cvalue);
11798 if (cvalue->op != OP_INTCONST) {
11799 error(state, 0, "integer constant expected");
11800 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011801 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011802 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011830 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011831}
11832
11833static void default_statement(struct compile_state *state, struct triple *first)
11834{
Eric Biederman83b991a2003-10-11 06:20:25 +000011835 struct triple *dest;
11836 struct triple *dbranch, *end;
11837
11838 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011839 eat(state, TOK_DEFAULT);
11840 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011841
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
Eric Biederman90089602004-05-28 14:11:54 +000011858 /* Blame the branch on the default statement */
11859 put_occurance(dbranch->occurance);
11860 dbranch->occurance = new_occurance(state);
11861
Eric Biederman83b991a2003-10-11 06:20:25 +000011862 /* Thread the pieces together */
11863 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000011864 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000011865 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011866 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011867}
11868
11869static void asm_statement(struct compile_state *state, struct triple *first)
11870{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011871 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;
Eric Biederman90089602004-05-28 14:11:54 +000011878 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011879
Eric Biederman90089602004-05-28 14:11:54 +000011880 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011881 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);
Eric Biederman90089602004-05-28 14:11:54 +000011889 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011890 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;
Eric Biederman8d9c1232003-06-17 08:42:17 +000011905 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011906 more = 0;
11907 if (out > MAX_LHS) {
11908 error(state, 0, "Maximum output count exceeded.");
11909 }
11910 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000011911 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011916 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;
Eric Biederman8d9c1232003-06-17 08:42:17 +000011938 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011939 more = 0;
11940 if (in > MAX_RHS) {
11941 error(state, 0, "Maximum input count exceeded.");
11942 }
11943 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000011944 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011952 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011978
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;
Eric Biederman90089602004-05-28 14:11:54 +000011997 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000011998
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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012013 for(i = 0; i < in; i++) {
12014 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012015 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012016 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012017 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012038 free_triple(state, constraint);
12039 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012040
12041 /* Now build the helper expressions */
12042 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012043 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012044 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012045 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012046 for(i = 0; i < (out + clobbers); i++) {
12047 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012048 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012049 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 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012066 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012067 piece->u.cval = i;
12068 LHS(def, i) = piece;
12069 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012070 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012071 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012078}
12079
12080
12081static 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
12111static 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
12124static 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
12179static 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012188 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012189 end_scope(state);
12190 return type;
12191}
12192
12193static struct type *param_type_list(struct compile_state *state, struct type *type)
12194{
12195 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012196 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012197 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012198 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012199 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);
Eric Biederman90089602004-05-28 14:11:54 +000012208 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012209 }
12210 }
12211 return ftype;
12212}
12213
Eric Biedermanb138ac82003-04-22 18:44:01 +000012214static 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
12223static struct type *direct_declarator(
12224 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012225 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012226{
Eric Biederman41203d92004-11-08 09:31:09 +000012227 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012228 struct type *outer;
12229 int op;
12230 outer = 0;
12231 arrays_complete(state, type);
12232 switch(peek(state)) {
12233 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012234 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012235 if (!ident) {
12236 error(state, 0, "Unexpected identifier found");
12237 }
12238 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012239 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012240 break;
12241 case TOK_LPAREN:
12242 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012243 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012244 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
12301static struct type *declarator(
12302 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012303 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012304{
12305 while(peek(state) == TOK_STAR) {
12306 eat(state, TOK_STAR);
12307 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12308 }
Eric Biederman41203d92004-11-08 09:31:09 +000012309 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012310 return type;
12311}
12312
Eric Biedermanb138ac82003-04-22 18:44:01 +000012313static struct type *typedef_name(
12314 struct compile_state *state, unsigned int specifiers)
12315{
12316 struct hash_entry *ident;
12317 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012318 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012319 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012325 return type;
12326}
12327
12328static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012329 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012330{
Eric Biederman83b991a2003-10-11 06:20:25 +000012331 struct hash_entry *ident;
12332 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012333 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012334 struct type *enum_type;
12335 enum_type = 0;
12336 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012337 eat(state, TOK_ENUM);
12338 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012339 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012340 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012341 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012342 base = 0;
12343 if (!ident || (peek(state) == TOK_LBRACE)) {
12344 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012345 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012346 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12347 enum_type->type_ident = ident;
12348 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012349 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012350 struct hash_entry *eident;
12351 struct triple *value;
12352 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012353 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012354 if (eident->sym_ident) {
12355 error(state, 0, "%s already declared",
12356 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012357 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012358 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012373 if (peek(state) == TOK_COMMA) {
12374 eat(state, TOK_COMMA);
12375 }
12376 } while(peek(state) != TOK_RBRACE);
12377 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012378 if (ident) {
12379 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12380 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012381 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012382 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012391}
12392
Eric Biedermanb138ac82003-04-22 18:44:01 +000012393static struct type *struct_declarator(
12394 struct compile_state *state, struct type *type, struct hash_entry **ident)
12395{
Eric Biederman90089602004-05-28 14:11:54 +000012396 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012397 type = declarator(state, type, ident, 1);
12398 }
Eric Biederman90089602004-05-28 14:11:54 +000012399 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012400 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012401 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012402 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012403 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012414 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012415 return type;
12416}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012417
12418static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012419 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012420{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012421 struct type *struct_type;
12422 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012423 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012424 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012425 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012426 struct_type = 0;
12427 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012428 switch(peek(state)) {
12429 case TOK_STRUCT:
12430 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012431 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012432 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012433 break;
12434 case TOK_UNION:
12435 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012436 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012437 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012438 break;
12439 default:
12440 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012441 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012442 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012443 break;
12444 }
12445 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012446 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012447 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012448 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012449 if (!ident || (peek(state) == TOK_LBRACE)) {
12450 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012451 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012452 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012453 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012454 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012455 do {
12456 struct type *base_type;
12457 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012458 base_type = specifier_qualifier_list(state);
12459 do {
12460 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012461 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012462 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012463 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012464 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012465 if (peek(state) == TOK_COMMA) {
12466 done = 0;
12467 eat(state, TOK_COMMA);
12468 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012469 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012477 } while(!done);
12478 eat(state, TOK_SEMI);
12479 } while(peek(state) != TOK_RBRACE);
12480 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012481 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012482 struct_type->type_ident = ident;
12483 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012484 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012485 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012486 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012487 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012488 if (ident && ident->sym_tag &&
12489 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012490 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012491 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012492 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012493 else if (ident && !struct_type) {
Eric Biederman90089602004-05-28 14:11:54 +000012494 error(state, 0, "%s %s undeclared",
12495 (type_main == TYPE_STRUCT)?"struct" : "union",
12496 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012497 }
12498 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012499}
12500
12501static 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) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012527 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012528 }
12529 else {
12530 specifiers = STOR_AUTO;
12531 }
12532 }
12533 return specifiers;
12534}
12535
12536static 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
Eric Biederman90089602004-05-28 14:11:54 +000012549static 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;
Eric Biederman41203d92004-11-08 09:31:09 +000012562 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012563
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
12588static 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
12598static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000012611static 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);
Eric Biederman90089602004-05-28 14:11:54 +000012621 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012622 break;
12623 case TOK_VOLATILE:
12624 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012625 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012626 break;
12627 case TOK_RESTRICT:
12628 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012629 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012630 break;
12631 default:
12632 done = 1;
12633 break;
12634 }
12635 } while(!done);
12636 return specifiers;
12637}
12638
12639static struct type *type_specifier(
12640 struct compile_state *state, unsigned int spec)
12641{
12642 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012643 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012644 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012645 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012646 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",
Eric Biederman41203d92004-11-08 09:31:09 +000012780 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012781 break;
12782 }
12783 return type;
12784}
12785
12786static 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
12812static 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
12826static 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
12864static 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
Eric Biederman90089602004-05-28 14:11:54 +000012880 /* attributes */
12881 specifiers |= attributes_opt(state, 0);
12882
Eric Biedermanb138ac82003-04-22 18:44:01 +000012883 /* type qualifier */
12884 specifiers |= type_qualifiers(state);
12885
12886 /* type specifier */
12887 type = type_specifier(state, specifiers);
12888 return type;
12889}
12890
Eric Biederman00443072003-06-24 12:34:45 +000012891struct field_info {
12892 struct type *type;
12893 size_t offset;
12894};
12895
12896static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012897{
12898 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000012899 struct field_info info;
12900 info.offset = ~0U;
12901 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012902 do {
12903 switch(peek(state)) {
12904 case TOK_LBRACKET:
12905 {
12906 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000012907 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
12908 error(state, 0, "Array designator not in array initializer");
12909 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012910 eat(state, TOK_LBRACKET);
12911 value = constant_expr(state);
12912 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000012913
12914 info.type = type->left;
12915 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012916 break;
12917 }
12918 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000012919 {
12920 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000012921 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
12922 ((type->type & TYPE_MASK) != TYPE_UNION))
12923 {
Eric Biederman00443072003-06-24 12:34:45 +000012924 error(state, 0, "Struct designator not in struct initializer");
12925 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012926 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000012927 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000012928 info.offset = field_offset(state, type, field);
12929 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012930 break;
Eric Biederman00443072003-06-24 12:34:45 +000012931 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012932 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);
Eric Biederman00443072003-06-24 12:34:45 +000012938 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012939}
12940
12941static struct triple *initializer(
12942 struct compile_state *state, struct type *type)
12943{
12944 struct triple *result;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012945#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Eric Biedermanb138ac82003-04-22 18:44:01 +000012946 if (peek(state) != TOK_LBRACE) {
12947 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012948 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 }
Eric Biederman90089602004-05-28 14:11:54 +000012955 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000012956 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
12957 (type->type & TYPE_MASK) != TYPE_ARRAY)
12958 {
Eric Biederman5cd81732004-03-11 15:01:31 +000012959 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000012960 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012961 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012967 }
12968 else {
12969 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000012970 size_t max_offset;
12971 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012972 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000012973 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
12974 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
12975 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012976 }
Eric Biederman00443072003-06-24 12:34:45 +000012977 info.offset = 0;
12978 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000012979 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
12980 info.type = next_field(state, type, 0);
12981 }
Eric Biederman00443072003-06-24 12:34:45 +000012982 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
12983 max_offset = 0;
12984 } else {
12985 max_offset = size_of(state, type);
12986 }
Eric Biederman90089602004-05-28 14:11:54 +000012987 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000012988 eat(state, TOK_LBRACE);
12989 do {
12990 struct triple *value;
12991 struct type *value_type;
12992 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000012993 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012994 int tok;
12995 comma = 0;
12996 tok = peek(state);
12997 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000012998 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012999 }
Eric Biederman00443072003-06-24 12:34:45 +000013000 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13001 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013002 error(state, 0, "element beyond bounds");
13003 }
Eric Biederman00443072003-06-24 12:34:45 +000013004 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013005 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) &&
Eric Biederman00443072003-06-24 12:34:45 +000013008 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13009 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013010 void *old_buf;
13011 size_t old_size;
13012 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013013 old_size = max_offset;
13014 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013015 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13016 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013017 xfree(old_buf);
13018 }
Eric Biederman90089602004-05-28 14:11:54 +000013019 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000013027 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013028 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013029 }
Eric Biederman90089602004-05-28 14:11:54 +000013030 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
Eric Biederman00443072003-06-24 12:34:45 +000013034 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013035 }
Eric Biederman90089602004-05-28 14:11:54 +000013036 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013037 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013038 }
Eric Biederman90089602004-05-28 14:11:54 +000013039 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013040 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013041 }
13042 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013043 internal_error(state, 0, "unhandled constant initializer");
13044 }
Eric Biederman00443072003-06-24 12:34:45 +000013045 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013046 if (peek(state) == TOK_COMMA) {
13047 eat(state, TOK_COMMA);
13048 comma = 1;
13049 }
Eric Biederman00443072003-06-24 12:34:45 +000013050 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013051 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);
Eric Biederman00443072003-06-24 12:34:45 +000013055 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013056 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013057 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13058 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13059 type->elements = max_offset / size_of(state, type->left);
13060 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013061 eat(state, TOK_RBRACE);
13062 result = triple(state, OP_BLOBCONST, type, 0, 0);
13063 result->u.blob = buf;
13064 }
13065 return result;
13066}
13067
Eric Biederman90089602004-05-28 14:11:54 +000013068static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013069{
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;
Eric Biederman90089602004-05-28 14:11:54 +000013076 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 */
Eric Biederman153ea352003-06-20 14:43:20 +000013100 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000013116static struct triple *function_definition(
13117 struct compile_state *state, struct type *type)
13118{
Eric Biederman90089602004-05-28 14:11:54 +000013119 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13120 struct triple *fname;
13121 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013122 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013123 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013124 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) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013132 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013133 error(state, 0, "Invalid function parameters");
13134 }
13135 param = type->right;
13136 i = 0;
13137 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13138 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013139 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013140 error(state, 0, "No identifier for parameter %d\n", i);
13141 }
13142 param = param->right;
13143 }
13144 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013145 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013146 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);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013157 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013158
13159 /* Put a label at the very end of a function */
13160 end = label(state);
13161 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013162 /* Remember where return goes */
13163 ident = state->i_return;
13164 symbol(state, ident, &ident->sym_ident, end, end->type);
13165
Eric Biederman90089602004-05-28 14:11:54 +000013166 /* 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000013177 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013178 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013179
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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013183
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) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013189 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013190 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013191 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013192 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 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013197 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013198 tmp = variable(state, param);
13199 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13200 flatten(state, end, tmp);
13201 }
Eric Biederman90089602004-05-28 14:11:54 +000013202
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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013215
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
Eric Biederman153ea352003-06-20 14:43:20 +000013224 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013225 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013226
Eric Biedermanb138ac82003-04-22 18:44:01 +000013227 /* Remove the parameter scope */
13228 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013229
Eric Biederman5ade04a2003-10-22 04:03:46 +000013230
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) {
Eric Biederman90089602004-05-28 14:11:54 +000013238 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__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013244 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013245
13246 return def;
13247}
13248
13249static 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013264 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013265 case STOR_EXTERN:
13266 type->type &= ~STOR_MASK;
13267 type->type |= STOR_STATIC;
13268 break;
13269 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013270 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013276 break;
13277 default:
13278 internal_error(state, 0, "Undefined storage class");
13279 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013280 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13281 error(state, 0, "Function prototypes not supported");
13282 }
Eric Biederman00443072003-06-24 12:34:45 +000013283 if (ident &&
13284 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013285 ((type->type & QUAL_CONST) == 0)) {
13286 error(state, 0, "non const static variables not supported");
13287 }
13288 if (ident) {
13289 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013290 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013291 }
13292 return def;
13293}
13294
13295static 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);
Eric Biederman90089602004-05-28 14:11:54 +000013305 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013306 if (global && ident && (peek(state) == TOK_LBRACE)) {
13307 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013308 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013309 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013310 def = function_definition(state, type);
13311 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013312 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013313 }
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
13343static 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000013363/*
13364 * Function inlining
13365 */
Eric Biederman90089602004-05-28 14:11:54 +000013366struct triple_reg_set {
13367 struct triple_reg_set *next;
13368 struct triple *member;
13369 struct triple *new;
13370};
13371struct reg_block {
13372 struct block *block;
13373 struct triple_reg_set *in;
13374 struct triple_reg_set *out;
13375 int vertex;
13376};
13377static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13378static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13379static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13380static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13381static 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);
13384static void print_block(
13385 struct compile_state *state, struct block *block, void *arg);
13386static int do_triple_set(struct triple_reg_set **head,
13387 struct triple *member, struct triple *new_member);
13388static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13389static struct reg_block *compute_variable_lifetimes(
13390 struct compile_state *state, struct basic_blocks *bb);
13391static void free_variable_lifetimes(struct compile_state *state,
13392 struct basic_blocks *bb, struct reg_block *blocks);
13393static void print_live_variables(struct compile_state *state,
13394 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
13395
Eric Biederman5ade04a2003-10-22 04:03:46 +000013396
13397static 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000013420static 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
Eric Biederman90089602004-05-28 14:11:54 +000013432static 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
13445static 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
13475static 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}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013488
13489static 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) {
Eric Biederman90089602004-05-28 14:11:54 +000013495 FILE *fp = state->errout;
13496 fprintf(fp, "global: ");
13497 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013498 }
13499#endif
13500 return local;
13501}
13502
13503struct 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) {
Eric Biederman90089602004-05-28 14:11:54 +000013511 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__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013517 }
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;
Eric Biederman90089602004-05-28 14:11:54 +000013527 old_lhs = old->lhs;
13528 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013529 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;
Eric Biederman90089602004-05-28 14:11:54 +000013545 new->id |= old->id & TRIPLE_FLAG_COPY;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013546
13547 /* During the copy remember new as user of old */
13548 use_triple(old, new);
13549
Eric Biederman5ade04a2003-10-22 04:03:46 +000013550 /* 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 */
Eric Biederman90089602004-05-28 14:11:54 +000013562 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013563 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) {
Eric Biederman90089602004-05-28 14:11:54 +000013579 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013580 }
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
Eric Biederman90089602004-05-28 14:11:54 +000013599static void expand_inline_call(
13600 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013601{
13602 /* Inline the function call */
13603 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013604 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013605 struct triple *end, *nend;
13606 int pvals, i;
13607
13608 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013609 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013610 if (ofunc->op != OP_LIST) {
13611 internal_error(state, 0, "improper function");
13612 }
Eric Biederman90089602004-05-28 14:11:54 +000013613 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013614 /* Prepend the parameter reading into the new function list */
13615 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013616 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013617 for(i = 0; i < pvals; i++) {
13618 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013619 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013620 atype = ptype;
13621 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13622 atype = ptype->left;
13623 }
Eric Biederman90089602004-05-28 14:11:54 +000013624 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013627 }
Eric Biederman90089602004-05-28 14:11:54 +000013628 arg = RHS(fcall, i);
13629 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013630 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013631 }
13632 result = 0;
13633 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman90089602004-05-28 14:11:54 +000013634 result = read_expr(state,
13635 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013636 }
13637 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013638 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__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013644 }
13645
Eric Biederman90089602004-05-28 14:11:54 +000013646 /*
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 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013680 RHS(nfunc, 0) = 0;
13681 free_triple(state, nfunc);
13682
13683 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013684 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013685 nend = nfirst->prev;
13686 end->next = nfirst;
13687 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013688 nend->next = fcall;
13689 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013690
Eric Biederman90089602004-05-28 14:11:54 +000013691 /* 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013701}
13702
Eric Biederman90089602004-05-28 14:11:54 +000013703/*
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
13734static 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
13793static 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
13804static 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
13815static 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
13837static 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
13847static 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
13909static inline int enclose_triple(struct triple *ins)
13910{
13911 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
13912}
13913
13914static 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
14122static void expand_function_call(
14123 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014124{
14125 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014126 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014127 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014128 struct triple *first;
14129 struct type *ptype, *rtype;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014130 struct triple *jmp;
14131 struct triple *ret_addr, *ret_loc, *ret_set;
Eric Biederman90089602004-05-28 14:11:54 +000014132 struct triple_reg_set *enclose, *set;
14133 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014134
Eric Biederman90089602004-05-28 14:11:54 +000014135#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
Eric Biederman5ade04a2003-10-22 04:03:46 +000014143 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014144 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014145 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014146 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014147 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014148 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014185
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;
Eric Biederman90089602004-05-28 14:11:54 +000014192 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014193 for(i = 0; i < pvals; i++) {
14194 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014195 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014196 atype = ptype;
14197 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14198 atype = ptype->left;
14199 }
Eric Biederman90089602004-05-28 14:11:54 +000014200 param = farg(state, func, i);
14201 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14202 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014203 }
Eric Biederman90089602004-05-28 14:11:54 +000014204 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014205 flatten(state, first, write_expr(state, param, arg));
14206 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014207 }
Eric Biederman90089602004-05-28 14:11:54 +000014208 rtype = func->type->left;
14209
Eric Biederman5ade04a2003-10-22 04:03:46 +000014210 /* Thread the triples together */
14211 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014212
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
Eric Biederman5ade04a2003-10-22 04:03:46 +000014232 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
Eric Biederman7dea9552004-06-29 05:38:37 +000014237 /* 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
Eric Biederman90089602004-05-28 14:11:54 +000014250 /* 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;
Eric Biederman7dea9552004-06-29 05:38:37 +000014254 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014255 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014256 continue;
14257 }
Eric Biederman90089602004-05-28 14:11:54 +000014258 /* 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 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014270 /*
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
Eric Biederman90089602004-05-28 14:11:54 +000014282
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 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014299
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);
Eric Biederman90089602004-05-28 14:11:54 +000014305 }
14306
Eric Biederman90089602004-05-28 14:11:54 +000014307 /* 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
14323static 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:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014367 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 }
Eric Biederman90089602004-05-28 14:11:54 +000014377 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}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014392
Eric Biederman90089602004-05-28 14:11:54 +000014393static 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014407 ptr = next = first;
14408 do {
Eric Biederman90089602004-05-28 14:11:54 +000014409 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014410 ptr = next;
14411 prev = ptr->prev;
14412 next = ptr->next;
14413 if (ptr->op != OP_FCALL) {
14414 continue;
14415 }
14416 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014417 /* 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014422 }
Eric Biederman90089602004-05-28 14:11:54 +000014423 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014465 next = prev->next;
14466 } while(next != first);
14467}
Eric Biederman90089602004-05-28 14:11:54 +000014468
14469static 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000014475static 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) {
Eric Biederman90089602004-05-28 14:11:54 +000014481 FILE *fp = state->errout;
14482 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014483 func->type->type_ident->name, func->u.cval);
14484 }
14485 if (func->u.cval == 0) {
14486 return;
14487 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014488
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
Eric Biederman90089602004-05-28 14:11:54 +000014502struct 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
14528struct 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000014547static void join_functions(struct compile_state *state)
14548{
Eric Biederman90089602004-05-28 14:11:54 +000014549 struct triple *jmp, *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014550 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014551 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014557
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 = "";
Eric Biederman90089602004-05-28 14:11:54 +000014567
Eric Biederman41203d92004-11-08 09:31:09 +000014568 if (!state->main_function) {
14569 error(state, 0, "No functions to compile\n");
14570 }
14571
Eric Biederman90089602004-05-28 14:11:54 +000014572 /* 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000014588 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014589 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));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014596 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014597 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000014668 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014669 mark_live_functions(state);
14670 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014671 walk_functions(state, insert_function, end);
14672
14673 if (start->next != end) {
14674 jmp = flatten(state, start, branch(state, end, 0));
14675 }
14676
Eric Biederman90089602004-05-28 14:11:54 +000014677 /* OK now the functions have been joined. */
14678 state->functions_joined = 1;
14679
Eric Biederman5ade04a2003-10-22 04:03:46 +000014680 /* Done now cleanup */
14681 state->file = file.prev;
14682 state->function = 0;
14683}
14684
Eric Biedermanb138ac82003-04-22 18:44:01 +000014685/*
14686 * Data structurs for optimation.
14687 */
14688
Eric Biederman5ade04a2003-10-22 04:03:46 +000014689
Eric Biederman83b991a2003-10-11 06:20:25 +000014690static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014691 struct block *used, struct block_set **head, struct block *user,
14692 int front)
14693{
14694 struct block_set **ptr, *new;
14695 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014696 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014697 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014698 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014699 ptr = head;
14700 while(*ptr) {
14701 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014702 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014703 }
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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014716 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014717}
Eric Biederman83b991a2003-10-11 06:20:25 +000014718static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014719 struct block *used, struct block_set **head, struct block *unuser)
14720{
14721 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014722 int count;
14723 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014724 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);
Eric Biederman83b991a2003-10-11 06:20:25 +000014731 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014732 }
14733 else {
14734 ptr = &use->next;
14735 }
14736 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014737 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014738}
14739
14740static void use_block(struct block *used, struct block *user)
14741{
Eric Biederman83b991a2003-10-11 06:20:25 +000014742 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014743 /* Append new to the head of the list, print_block
14744 * depends on this.
14745 */
Eric Biederman83b991a2003-10-11 06:20:25 +000014746 count = do_use_block(used, &used->use, user, 1);
14747 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014748}
14749static void unuse_block(struct block *used, struct block *unuser)
14750{
Eric Biederman83b991a2003-10-11 06:20:25 +000014751 int count;
14752 count = do_unuse_block(used, &used->use, unuser);
14753 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014754}
14755
Eric Biederman5ade04a2003-10-22 04:03:46 +000014756static 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
14763static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000014770static void idom_block(struct block *idom, struct block *user)
14771{
14772 do_use_block(idom, &idom->idominates, user, 0);
14773}
14774
14775static void unidom_block(struct block *idom, struct block *unuser)
14776{
14777 do_unuse_block(idom, &idom->idominates, unuser);
14778}
14779
14780static void domf_block(struct block *block, struct block *domf)
14781{
14782 do_use_block(block, &block->domfrontier, domf, 0);
14783}
14784
14785static void undomf_block(struct block *block, struct block *undomf)
14786{
14787 do_unuse_block(block, &block->domfrontier, undomf);
14788}
14789
14790static void ipdom_block(struct block *ipdom, struct block *user)
14791{
14792 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14793}
14794
14795static void unipdom_block(struct block *ipdom, struct block *unuser)
14796{
14797 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14798}
14799
14800static void ipdomf_block(struct block *block, struct block *ipdomf)
14801{
14802 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14803}
14804
14805static void unipdomf_block(struct block *block, struct block *unipdomf)
14806{
14807 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14808}
14809
Eric Biederman83b991a2003-10-11 06:20:25 +000014810static int walk_triples(
14811 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000014812 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
14813 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014814{
Eric Biederman83b991a2003-10-11 06:20:25 +000014815 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014816 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000014817 ptr = state->first;
14818 do {
Eric Biederman90089602004-05-28 14:11:54 +000014819 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000014820 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));
Eric Biedermanb138ac82003-04-22 18:44:01 +000014825 return result;
14826}
14827
Eric Biedermanb138ac82003-04-22 18:44:01 +000014828#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000014829static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014830{
Eric Biederman90089602004-05-28 14:11:54 +000014831 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014832 int op;
14833 op = ins->op;
14834 if (op == OP_LIST) {
14835#if !PRINT_LIST
14836 return 0;
14837#endif
14838 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014839 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000014840 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014841 }
Eric Biederman90089602004-05-28 14:11:54 +000014842 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014843
Eric Biederman90089602004-05-28 14:11:54 +000014844 if (triple_is_branch(state, ins) && ins->use &&
14845 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014846 internal_error(state, ins, "branch used?");
14847 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014848 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000014849 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000014850 }
14851 return 0;
14852}
14853
Eric Biedermanb138ac82003-04-22 18:44:01 +000014854static void print_triples(struct compile_state *state)
14855{
Eric Biederman5ade04a2003-10-22 04:03:46 +000014856 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000014857 FILE *fp = state->dbgout;
14858 fprintf(fp, "--------------- triples ---------------\n");
14859 walk_triples(state, do_print_triple, fp);
14860 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014861 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014862}
14863
14864struct cf_block {
14865 struct block *block;
14866};
14867static void find_cf_blocks(struct cf_block *cf, struct block *block)
14868{
Eric Biederman5ade04a2003-10-22 04:03:46 +000014869 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014870 if (!block || (cf[block->vertex].block == block)) {
14871 return;
14872 }
14873 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014874 for(edge = block->edges; edge; edge = edge->next) {
14875 find_cf_blocks(cf, edge->member);
14876 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014877}
14878
Eric Biederman90089602004-05-28 14:11:54 +000014879static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014880 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014881{
14882 struct cf_block *cf;
14883 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000014884 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000014885 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
14886 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014887
Eric Biederman90089602004-05-28 14:11:54 +000014888 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014889 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014890 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014891 block = cf[i].block;
14892 if (!block)
14893 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000014894 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014895 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000014896 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014897 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014898 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000014899 }
14900
14901 xfree(cf);
14902}
14903
Eric Biedermanb138ac82003-04-22 18:44:01 +000014904static void free_basic_block(struct compile_state *state, struct block *block)
14905{
Eric Biederman5ade04a2003-10-22 04:03:46 +000014906 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014907 struct block *child;
14908 if (!block) {
14909 return;
14910 }
14911 if (block->vertex == -1) {
14912 return;
14913 }
14914 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014915 for(edge = block->edges; edge; edge = edge->next) {
14916 if (edge->member) {
14917 unuse_block(edge->member, block);
14918 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014919 }
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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014928 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014929 child = entry->member;
14930 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014931 if (child && (child->vertex != -1)) {
14932 for(edge = child->edges; edge; edge = edge->next) {
14933 edge->member = 0;
14934 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014935 }
14936 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014937 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014938 child = entry->member;
14939 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014940 if (child && (child->vertex != -1)) {
14941 child->idom = 0;
14942 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014943 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014944 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014945 child = entry->member;
14946 undomf_block(block, child);
14947 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014948 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014949 child = entry->member;
14950 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014951 if (child && (child->vertex != -1)) {
14952 child->ipdom = 0;
14953 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014954 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014955 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014956 child = entry->member;
14957 unipdomf_block(block, child);
14958 }
14959 if (block->users != 0) {
14960 internal_error(state, 0, "block still has users");
14961 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014962 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014970 memset(block, -1, sizeof(*block));
14971 xfree(block);
14972}
14973
Eric Biederman90089602004-05-28 14:11:54 +000014974static void free_basic_blocks(struct compile_state *state,
14975 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014976{
14977 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000014978 free_basic_block(state, bb->first_block);
14979 bb->last_vertex = 0;
14980 bb->first_block = bb->last_block = 0;
14981 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014982 ins = first;
14983 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000014984 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014985 ins->u.block = 0;
14986 }
14987 ins = ins->next;
14988 } while(ins != first);
14989
14990}
14991
Eric Biederman90089602004-05-28 14:11:54 +000014992static 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
15105static 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
15127static 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
15167static 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}
15172static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15173{
Eric Biederman7dea9552004-06-29 05:38:37 +000015174 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);
Eric Biederman90089602004-05-28 14:11:54 +000015176 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15177 fprintf(fp, "After %s\n", func);
15178 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015179 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);
Eric Biederman90089602004-05-28 14:11:54 +000015184 }
15185}
15186
15187static 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
15219static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000015269struct 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
15281static 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
15297static 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
15307static int initialize_sdblock(struct sdom_block *sd,
15308 struct block *parent, struct block *block, int vertex)
15309{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015310 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015311 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015323 for(edge = block->edges; edge; edge = edge->next) {
15324 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15325 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015326 return vertex;
15327}
15328
Eric Biederman83b991a2003-10-11 06:20:25 +000015329static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015330 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015331 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) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015347 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015348 }
15349 return vertex;
15350}
15351
Eric Biederman90089602004-05-28 14:11:54 +000015352static int setup_spdblocks(struct compile_state *state,
15353 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015354{
15355 struct block *block;
15356 int vertex;
15357 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015358 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015359
Eric Biederman5ade04a2003-10-22 04:03:46 +000015360 /* Walk through the graph and find unconnected blocks. Add a
15361 * fake edge from the unconnected blocks to the end of the
15362 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015363 */
Eric Biederman90089602004-05-28 14:11:54 +000015364 block = bb->first_block->last->next->u.block;
15365 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015366 if (sd[block->vertex].block == block) {
15367 continue;
15368 }
Eric Biederman530b5192003-07-01 10:05:30 +000015369#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015370 {
15371 FILE *fp = state->errout;
15372 fprintf(fp, "Adding %d\n", vertex +1);
15373 }
Eric Biederman530b5192003-07-01 10:05:30 +000015374#endif
Eric Biederman90089602004-05-28 14:11:54 +000015375 add_block_edge(block, bb->last_block, 0);
15376 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015377
Eric Biederman90089602004-05-28 14:11:54 +000015378 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015379 }
15380 return vertex;
15381}
15382
15383static 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
Eric Biederman90089602004-05-28 14:11:54 +000015406static void compute_sdom(struct compile_state *state,
15407 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015408{
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 */
Eric Biederman90089602004-05-28 14:11:54 +000015427 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015428 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
Eric Biederman90089602004-05-28 14:11:54 +000015456static void compute_spdom(struct compile_state *state,
15457 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015458{
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 */
Eric Biederman90089602004-05-28 14:11:54 +000015477 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015478 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015479 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015480 struct block *block;
15481 block = sd[i].block;
15482 parent = sd[i].parent;
15483 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015484 for(edge = block->edges; edge; edge = edge->next) {
15485 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015486 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
Eric Biederman90089602004-05-28 14:11:54 +000015505static void compute_idom(struct compile_state *state,
15506 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015507{
15508 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015509 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015510 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
Eric Biederman90089602004-05-28 14:11:54 +000015520static void compute_ipdom(struct compile_state *state,
15521 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015522{
15523 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015524 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015525 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
Eric Biederman90089602004-05-28 14:11:54 +000015564static void find_immediate_dominators(struct compile_state *state,
15565 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015566{
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 */
Eric Biederman90089602004-05-28 14:11:54 +000015599 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15600 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015601#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 */
Eric Biederman90089602004-05-28 14:11:54 +000015608 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015609 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015610 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015611 xfree(sd);
15612}
15613
Eric Biederman90089602004-05-28 14:11:54 +000015614static void find_post_dominators(struct compile_state *state,
15615 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015616{
15617 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015618 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015619 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015620 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015621
Eric Biederman90089602004-05-28 14:11:54 +000015622 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);
Eric Biederman530b5192003-07-01 10:05:30 +000015626 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015627
15628 /* Step 2 compute the semidominators */
15629 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015630 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015631 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015632 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015633 xfree(sd);
15634}
15635
15636
15637
15638static void find_block_domf(struct compile_state *state, struct block *block)
15639{
15640 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015641 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015642 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015652 for(edge = block->edges; edge; edge = edge->next) {
15653 if (edge->member->idom != block) {
15654 domf_block(block, edge->member);
15655 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015656 }
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
15668static 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015682 for(user = block->use; user; user = user->next) {
15683 if (user->member->ipdom != block) {
15684 ipdomf_block(block, user->member);
15685 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015686 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015687 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015688 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015698static void print_dominated(
15699 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015700{
15701 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015702 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015703
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015704 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015705 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015706 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015707 if (user->member->idom != block) {
15708 internal_error(state, user->member->first, "bad idom");
15709 }
15710 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015711 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015712}
15713
Eric Biederman5ade04a2003-10-22 04:03:46 +000015714static 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
Eric Biederman90089602004-05-28 14:11:54 +000015753static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015754{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015755 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015756 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015757 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015758 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015759}
15760
15761
15762static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015763 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015764{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015765 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015766
15767 if (!block || (block->vertex != vertex + 1)) {
15768 return vertex;
15769 }
15770 vertex += 1;
15771
Eric Biederman7dea9552004-06-29 05:38:37 +000015772 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015773 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015774 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015775 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015776 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015777
15778 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015779 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015780 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015781 return vertex;
15782}
Eric Biederman90089602004-05-28 14:11:54 +000015783static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015784 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015785{
Eric Biederman7dea9552004-06-29 05:38:37 +000015786 fprintf(fp, "\ndominance frontiers\n");
15787 print_frontiers(state, fp, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015788
15789}
15790
Eric Biederman90089602004-05-28 14:11:54 +000015791static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015792{
15793 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015794 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015795 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015796 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015797 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015798 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015799 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015800 print_dominance_frontiers(state, state->dbgout, bb);
15801 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015802 }
15803}
15804
15805
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015806static void print_ipdominated(
15807 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015808{
15809 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015810 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015812 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015813 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015814 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015815 if (user->member->ipdom != block) {
15816 internal_error(state, user->member->first, "bad ipdom");
15817 }
15818 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015819 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015820}
15821
Eric Biederman90089602004-05-28 14:11:54 +000015822static void print_ipdominators(struct compile_state *state, FILE *fp,
15823 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015824{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015825 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015826 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015827}
15828
15829static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015830 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015831{
15832 struct block_set *user;
15833
15834 if (!block || (block->vertex != vertex + 1)) {
15835 return vertex;
15836 }
15837 vertex += 1;
15838
Eric Biederman7dea9552004-06-29 05:38:37 +000015839 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015840 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015841 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015842 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015843 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015844 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015845 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015846 }
15847 return vertex;
15848}
Eric Biederman90089602004-05-28 14:11:54 +000015849static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015850 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015851{
Eric Biederman7dea9552004-06-29 05:38:37 +000015852 fprintf(fp, "\nipdominance frontiers\n");
15853 print_pfrontiers(state, fp, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015854
15855}
15856
Eric Biederman90089602004-05-28 14:11:54 +000015857static void analyze_ipdominators(struct compile_state *state,
15858 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015859{
15860 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015861 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015862 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000015863 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015864 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015865 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015866 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015867 print_ipdominance_frontiers(state, state->dbgout, bb);
15868 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015869 }
15870}
15871
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015872static 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
15881static 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;
Eric Biederman7dea9552004-06-29 05:38:37 +000015893 if (!bdom || !bsub) {
15894 internal_error(state, dom, "huh?");
15895 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015896 ins = sub;
15897 while((ins != bsub->first) && (ins != dom)) {
15898 ins = ins->prev;
15899 }
15900 result = (ins == dom);
15901 }
15902 return result;
15903}
15904
Eric Biederman90089602004-05-28 14:11:54 +000015905static void analyze_basic_blocks(
15906 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000015907{
Eric Biederman90089602004-05-28 14:11:54 +000015908 setup_basic_blocks(state, bb);
15909 analyze_idominators(state, bb);
15910 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000015911}
15912
Eric Biedermanb138ac82003-04-22 18:44:01 +000015913static 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;
Eric Biedermand1ea5392003-06-28 06:49:45 +000015920 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015921
Eric Biederman90089602004-05-28 14:11:54 +000015922 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015923 has_already = xcmalloc(size, "has_already");
15924 work = xcmalloc(size, "work");
15925 iter = 0;
15926
Eric Biederman83b991a2003-10-11 06:20:25 +000015927 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000015928 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015929 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000015930 struct triple_set *user, *unext;
15931 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000015932
15933 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015934 continue;
15935 }
Eric Biederman90089602004-05-28 14:11:54 +000015936
Eric Biedermanb138ac82003-04-22 18:44:01 +000015937 iter += 1;
15938 work_list = 0;
15939 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000015940 for(user = var->use; user; user = unext) {
15941 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000015942 if (MISC(var, 0) == user->member) {
15943 continue;
15944 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015945 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");
Eric Biedermand1ea5392003-06-28 06:49:45 +000015955 release_triple(state, user->member);
15956 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015957 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000015958 if (work[block->vertex] >= iter) {
15959 continue;
15960 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015961 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 */
Eric Biederman66fe2222003-07-04 15:14:04 +000015980 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015981 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015982 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000015983 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015984 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000015985 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000015987#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
Eric Biedermanb138ac82003-04-22 18:44:01 +000015993 /* 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000015999 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016000
Eric Biedermanb138ac82003-04-22 18:44:01 +000016001 /* 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
Eric Biederman66fe2222003-07-04 15:14:04 +000016016
Eric Biederman83b991a2003-10-11 06:20:25 +000016017struct stack {
16018 struct triple_set *top;
16019 unsigned orig_id;
16020};
16021
Eric Biederman90089602004-05-28 14:11:54 +000016022static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016023{
16024 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016025 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016026 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016027 ins = first;
16028 do {
Eric Biederman90089602004-05-28 14:11:54 +000016029 if (triple_is_auto_var(state, ins)) {
16030 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016031 }
16032 ins = ins->next;
16033 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016034 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016035}
16036
Eric Biederman90089602004-05-28 14:11:54 +000016037static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016038{
16039 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016040 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016041 first = state->first;
16042 ins = first;
16043 do {
Eric Biederman90089602004-05-28 14:11:54 +000016044 if (triple_is_auto_var(state, ins)) {
16045 auto_vars += 1;
16046 stacks[auto_vars].orig_id = ins->id;
16047 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016048 }
16049 ins = ins->next;
16050 } while(ins != first);
16051}
16052
Eric Biederman90089602004-05-28 14:11:54 +000016053static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016054{
16055 struct triple *first, *ins;
16056 first = state->first;
16057 ins = first;
16058 do {
Eric Biederman90089602004-05-28 14:11:54 +000016059 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016060 ins->id = stacks[ins->id].orig_id;
16061 }
16062 ins = ins->next;
16063 } while(ins != first);
16064}
16065
16066static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016067{
16068 struct triple_set *head;
16069 struct triple *top_val;
16070 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016071 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016072 if (head) {
16073 top_val = head->member;
16074 }
16075 return top_val;
16076}
16077
Eric Biederman83b991a2003-10-11 06:20:25 +000016078static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016079{
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;
Eric Biederman83b991a2003-10-11 06:20:25 +000016086 new->next = stacks[var->id].top;
16087 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016088}
16089
Eric Biederman83b991a2003-10-11 06:20:25 +000016090static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016091{
16092 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016093 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016094 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000016108/*
16109 * C(V)
16110 * S(V)
16111 */
16112static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016113 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016114{
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;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016133 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016134 if (!var) {
16135 internal_error(state, ptr, "no var???");
16136 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016137 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016138 val = peek_triple(stacks, var);
16139 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016140 internal_error(state, val, "bad value in phi");
16141 }
Eric Biederman90089602004-05-28 14:11:54 +000016142 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016143 internal_error(state, ptr, "edges > phi rhs");
16144 }
16145 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016146 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
16159static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016160 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016161{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016162 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016163 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016177 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016178 if (!triple_is_auto_var(state, var)) {
16179 internal_error(state, ptr, "read of non auto var!");
16180 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016181 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016182 /* Find the current value of the variable */
16183 val = peek_triple(stacks, var);
16184 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016185 /* 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) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016198 error(state, ptr, "variable used without being set");
16199 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016200 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) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016209 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016210 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)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016217 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016218 }
Eric Biederman90089602004-05-28 14:11:54 +000016219 /* Insert a cast if the types differ */
16220 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016221 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 {
Eric Biederman90089602004-05-28 14:11:54 +000016226 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016227 use_triple(val, tval);
16228 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016229 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016230 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016231 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016232 use_triple(tval, ptr);
16233 }
16234 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016235 unuse_triple(var, ptr);
16236 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016237 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016238 }
16239 if (ptr->op == OP_PHI) {
16240 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016241 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016242 if (!triple_is_auto_var(state, var)) {
16243 internal_error(state, ptr, "phi references non auto var!");
16244 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016245 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016246 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016247 }
16248 last = ptr;
16249 }
16250 block->last = last;
16251
16252 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016253 for(edge = block->edges; edge; edge = edge->next) {
16254 fixup_block_phi_variables(state, stacks, block, edge->member);
16255 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016256 /* rename variables in the dominated nodes */
16257 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016258 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016259 }
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;
Eric Biederman90089602004-05-28 14:11:54 +000016270 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016271 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016272 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016273 release_triple(state, ptr);
16274 continue;
16275 }
16276 if (ptr->op == OP_PHI) {
16277 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016278 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016279 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016280 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016281 }
16282 last = ptr;
16283 }
16284 block->last = last;
16285}
16286
Eric Biederman83b991a2003-10-11 06:20:25 +000016287static void rename_variables(struct compile_state *state)
16288{
16289 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016290 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016291
16292 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016293 auto_vars = count_auto_vars(state);
16294 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016295
Eric Biederman90089602004-05-28 14:11:54 +000016296 /* Give each auto_var a stack */
16297 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016298
16299 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016300 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016301
Eric Biederman90089602004-05-28 14:11:54 +000016302 /* Remove the stacks from the auto_vars */
16303 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016304 xfree(stacks);
16305}
16306
Eric Biedermanb138ac82003-04-22 18:44:01 +000016307static void prune_block_variables(struct compile_state *state,
16308 struct block *block)
16309{
16310 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016311 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016312 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016313
Eric Biedermanb138ac82003-04-22 18:44:01 +000016314 done = 0;
16315 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016316 /* Be extremely careful I am deleting the list
16317 * as I walk trhough it.
16318 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016319 next = ptr->next;
16320 if (ptr == block->last) {
16321 done = 1;
16322 }
Eric Biederman90089602004-05-28 14:11:54 +000016323 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016324 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;
Eric Biederman90089602004-05-28 14:11:54 +000016329 if (MISC(ptr, 0) == user->member) {
16330 continue;
16331 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016332 if (use->op != OP_PHI) {
16333 internal_error(state, use, "decl still used");
16334 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016335 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016336 internal_error(state, use, "bad phi use of decl");
16337 }
16338 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016339 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016340 }
Eric Biederman90089602004-05-28 14:11:54 +000016341 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016347 continue;
16348 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016349 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016350 for(user = block->idominates; user; user = user->next) {
16351 prune_block_variables(state, user->member);
16352 }
16353}
16354
Eric Biederman66fe2222003-07-04 15:14:04 +000016355struct phi_triple {
16356 struct triple *phi;
16357 unsigned orig_id;
16358 int alive;
16359};
16360
16361static 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;
Eric Biederman90089602004-05-28 14:11:54 +000016369 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016370 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
16380static void prune_unused_phis(struct compile_state *state)
16381{
16382 struct triple *first, *phi;
16383 struct phi_triple *live;
16384 int phis, i;
16385
Eric Biederman66fe2222003-07-04 15:14:04 +000016386 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016387 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016388
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);
Eric Biederman90089602004-05-28 14:11:54 +000016432 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016433 for(j = 0; j < zrhs; j++) {
16434 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016435 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
Eric Biederman66fe2222003-07-04 15:14:04 +000016446 }
16447 }
16448 }
16449 xfree(live);
16450}
16451
Eric Biedermanb138ac82003-04-22 18:44:01 +000016452static void transform_to_ssa_form(struct compile_state *state)
16453{
16454 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016455 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016456
Eric Biederman90089602004-05-28 14:11:54 +000016457 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016458 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016459
Eric Biederman90089602004-05-28 14:11:54 +000016460 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016461}
16462
16463
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016464static void clear_vertex(
16465 struct compile_state *state, struct block *block, void *arg)
16466{
Eric Biederman83b991a2003-10-11 06:20:25 +000016467 /* 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 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016471 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016472 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016473 for(edge = block->edges; edge; edge = edge->next) {
16474 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016475 }
16476 for(user = block->use; user; user = user->next) {
16477 user->member->vertex = 0;
16478 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016479}
16480
16481static 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;
Eric Biederman90089602004-05-28 14:11:54 +000016492 targ = triple_edge_targ(state, block->last, 0);
16493 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016494 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 }
Eric Biederman90089602004-05-28 14:11:54 +000016502 /* 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016506 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016507 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016508 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000016517static 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000016523 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016524 int next_vertex;
16525
16526 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016527 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016528 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016529 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016530
16531 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016532 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016533 for(phi = first->next; phi != first ; phi = next) {
16534 struct block_set *set;
16535 struct block *block;
16536 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016537 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016538 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016539 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016540 next = phi->next;
16541 if (phi->op != OP_PHI) {
16542 continue;
16543 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016544
Eric Biedermanb138ac82003-04-22 18:44:01 +000016545 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016546 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016547
Eric Biederman83b991a2003-10-11 06:20:25 +000016548 /* 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016554 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016571 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016572 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
Eric Biederman83b991a2003-10-11 06:20:25 +000016578 /* Replaces use of phi with var */
16579 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016580
Eric Biederman90089602004-05-28 14:11:54 +000016581 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000016589 /* Walk all of the incoming edges/blocks and insert moves.
16590 */
Eric Biederman90089602004-05-28 14:11:54 +000016591 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016592 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016593 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016594 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016595 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016596 eblock = set->member;
16597 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016598 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016599 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016600 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016601
Eric Biederman83b991a2003-10-11 06:20:25 +000016602 /* If we don't have a value that belongs in an OP_WRITE
16603 * continue on.
16604 */
Eric Biederman90089602004-05-28 14:11:54 +000016605 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?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016612 continue;
16613 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016614
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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016630
Eric Biederman530b5192003-07-01 10:05:30 +000016631 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016632 /* 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000016655 }
Eric Biederman90089602004-05-28 14:11:54 +000016656 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016662 use_triple(val, move);
16663 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016664 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016665 }
Eric Biederman90089602004-05-28 14:11:54 +000016666 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016674 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016675 release_triple(state, phi);
16676 }
16677
Eric Biederman83b991a2003-10-11 06:20:25 +000016678 /* 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;
Eric Biederman90089602004-05-28 14:11:54 +000016681 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016682 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;
Eric Biederman90089602004-05-28 14:11:54 +000016701 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016702 slot = &RHS(user, 0);
16703 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016704 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016705 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016720}
16721
Eric Biederman5ade04a2003-10-22 04:03:46 +000016722#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016723 FILE *fp = state->dbgout; \
16724 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Eric Biederman5ade04a2003-10-22 04:03:46 +000016725 }
16726
Eric Biederman83b991a2003-10-11 06:20:25 +000016727static void rebuild_ssa_form(struct compile_state *state)
16728{
16729HI();
16730 transform_from_ssa_form(state);
16731HI();
Eric Biederman90089602004-05-28 14:11:54 +000016732 state->bb.first = state->first;
16733 free_basic_blocks(state, &state->bb);
16734 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016735HI();
16736 insert_phi_operations(state);
16737HI();
16738 rename_variables(state);
16739HI();
16740
Eric Biederman90089602004-05-28 14:11:54 +000016741 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016742HI();
16743 prune_unused_phis(state);
16744HI();
16745}
16746#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016747
16748/*
16749 * Register conflict resolution
16750 * =========================================================
16751 */
16752
16753static 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
16798static 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;
Eric Biederman90089602004-05-28 14:11:54 +000016803 zrhs = ins->rhs;
16804 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016805 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
16830static struct reg_info find_rhs_post_color(
16831 struct compile_state *state, struct triple *ins, int index);
16832
16833static 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;
Eric Biederman530b5192003-07-01 10:05:30 +000016839#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016840 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016841 ins, index);
16842#endif
16843 if ((index == 0) && triple_is_def(state, ins)) {
16844 lhs = ins;
16845 }
Eric Biederman90089602004-05-28 14:11:54 +000016846 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016847 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;
Eric Biederman90089602004-05-28 14:11:54 +000016862 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016863 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000016883#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016884 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016885 ins, index, info.reg, info.regcm);
16886#endif
16887 return info;
16888}
16889
16890static 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;
Eric Biederman530b5192003-07-01 10:05:30 +000016895#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016896 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016897 ins, index);
16898#endif
16899 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000016900 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016901 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000016918 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016919 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000016929#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016930 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016931 ins, index, info.reg, info.regcm);
16932#endif
16933 return info;
16934}
16935
16936static struct reg_info find_lhs_color(
16937 struct compile_state *state, struct triple *ins, int index)
16938{
16939 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000016940#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016941 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016942 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000016956#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016957 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000016958 ins, index, info.reg, info.regcm,
16959 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016960#endif
16961 return info;
16962}
16963
16964static 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
Eric Biedermand1ea5392003-06-28 06:49:45 +000016994static struct triple *typed_pre_copy(
16995 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016996{
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;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017002 unsigned classes;
17003 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017004 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017005 if (ins->op == OP_PHI) {
17006 internal_error(state, ins, "pre_copy on a phi?");
17007 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017008 classes = arch_type_to_regcm(state, type);
17009 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017010 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017011 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017012 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");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017018 internal_error(state, ins, "pre_copy with no register classes");
17019 }
Eric Biederman90089602004-05-28 14:11:54 +000017020 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017025 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;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017031
17032}
17033static 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017037}
17038
17039
Eric Biedermanb138ac82003-04-22 18:44:01 +000017040static 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 */
Eric Biederman83b991a2003-10-11 06:20:25 +000017049 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017050 for(phi = first->next; phi != first ; phi = phi->next) {
17051 struct block_set *set;
17052 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017053 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017054 int edge;
17055 if (phi->op != OP_PHI) {
17056 continue;
17057 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017058 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017059 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017060 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017061 /* 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017068 /* 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
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017082 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017083 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017084 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017085 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017086 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017087 use_triple(val, move);
17088
17089 slot[edge] = move;
17090 unuse_triple(val, phi);
17091 use_triple(move, phi);
17092
Eric Biederman66fe2222003-07-04 15:14:04 +000017093 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000017102 /* 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;
Eric Biederman90089602004-05-28 14:11:54 +000017107 if (ptr->op == OP_PIECE) {
17108 ptr = MISC(ptr, 0);
17109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017110 if ((ptr == phi) || (ptr == val)) {
17111 goto out;
17112 }
Eric Biederman90089602004-05-28 14:11:54 +000017113 expr = triple_lhs(state, ptr, 0);
17114 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17115 if ((*expr) == val) {
17116 goto out;
17117 }
17118 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017119 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:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017127 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017128 internal_error(state, ptr,
17129 "Could not insert write to phi");
17130 }
Eric Biederman90089602004-05-28 14:11:54 +000017131 insert_triple(state, after_lhs(state, ptr), move);
17132 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017133 eblock->last = move;
17134 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017135 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017136 }
17137 }
Eric Biederman90089602004-05-28 14:11:54 +000017138 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017139}
17140
Eric Biederman90089602004-05-28 14:11:54 +000017141struct triple_reg_set;
17142struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017143
Eric Biedermanb138ac82003-04-22 18:44:01 +000017144
17145static 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
17166static 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
17183static int in_triple(struct reg_block *rb, struct triple *in)
17184{
17185 return do_triple_set(&rb->in, in, 0);
17186}
17187static void unin_triple(struct reg_block *rb, struct triple *unin)
17188{
17189 do_triple_unset(&rb->in, unin);
17190}
17191
17192static int out_triple(struct reg_block *rb, struct triple *out)
17193{
17194 return do_triple_set(&rb->out, out, 0);
17195}
17196static void unout_triple(struct reg_block *rb, struct triple *unout)
17197{
17198 do_triple_unset(&rb->out, unout);
17199}
17200
17201static 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
Eric Biederman90089602004-05-28 14:11:54 +000017219static 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
17258static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000017270static 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017298 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017299 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) {
Eric Biederman90089602004-05-28 14:11:54 +000017309 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017310 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
17322static 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) {
Eric Biederman90089602004-05-28 14:11:54 +000017346 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017347 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000017360static 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;
Eric Biederman90089602004-05-28 14:11:54 +000017386 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017387 if (!rhs) {
17388 continue;
17389 }
Eric Biederman90089602004-05-28 14:11:54 +000017390
17391 /* See if rhs is defined in this block.
17392 * A write counts as a definition.
17393 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017394 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17395 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017396 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017397 rhs = 0;
17398 break;
17399 }
17400 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017401 /* If I still have a valid rhs add it to in */
17402 change |= in_triple(rb, rhs);
17403 }
17404 }
17405 return change;
17406}
17407
17408static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017409 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017410{
17411 struct reg_block *blocks;
17412 int change;
17413 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017414 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17415 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017416 do {
17417 int i;
17418 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017419 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017420 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017421 struct reg_block *rb;
17422 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017423 /* 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017426 }
17427 /* Add use to in... */
17428 change |= use_in(state, rb);
17429 }
17430 } while(change);
17431 return blocks;
17432}
17433
Eric Biederman90089602004-05-28 14:11:54 +000017434static void free_variable_lifetimes(struct compile_state *state,
17435 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017436{
17437 int i;
17438 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017439 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017440 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
Eric Biedermanf96a8102003-06-16 16:57:34 +000017456typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000017457 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
17461static void walk_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017462 struct basic_blocks *bb, struct reg_block *blocks,
17463 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017464{
17465 int i;
17466
Eric Biederman90089602004-05-28 14:11:54 +000017467 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017468 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) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017487 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017488
17489 prev = ptr->prev;
17490 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017491
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 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017500 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017501
17502 /* Remove the current definition from live */
17503 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017504
Eric Biedermanb138ac82003-04-22 18:44:01 +000017505 /* 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. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017518 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017519 continue;
17520 }
17521 do_triple_set(&live, *expr, 0);
17522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017523 }
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
Eric Biederman90089602004-05-28 14:11:54 +000017532struct print_live_variable_info {
17533 struct reg_block *rb;
17534 FILE *fp;
17535};
17536static 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
17607static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000017619static int count_triples(struct compile_state *state)
17620{
17621 struct triple *first, *ins;
17622 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017623 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017624 ins = first;
17625 do {
17626 triples++;
17627 ins = ins->next;
17628 } while (ins != first);
17629 return triples;
17630}
Eric Biederman66fe2222003-07-04 15:14:04 +000017631
17632
Eric Biedermanb138ac82003-04-22 18:44:01 +000017633struct dead_triple {
17634 struct triple *triple;
17635 struct dead_triple *work_next;
17636 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017637 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017638 int flags;
17639#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017640#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017641};
17642
Eric Biederman90089602004-05-28 14:11:54 +000017643static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000017672
17673static 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) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017692 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017693 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
17705static 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000017710 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017711
Eric Biederman5ade04a2003-10-22 04:03:46 +000017712 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17713 return;
17714 }
17715
Eric Biedermanb138ac82003-04-22 18:44:01 +000017716 /* Setup the work list */
17717 work_list = 0;
17718 work_list_tail = &work_list;
17719
Eric Biederman83b991a2003-10-11 06:20:25 +000017720 first = state->first;
17721 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017722
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 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017733 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017734 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017735 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017736 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017737 ins->id = i;
17738 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017739 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017740 awaken(state, dtriple, &ins, &work_list_tail);
17741 }
17742 i++;
17743 ins = ins->next;
17744 } while(ins != first);
17745 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017746 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017747 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017755 /* 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);
Eric Biederman90089602004-05-28 14:11:54 +000017760 } else {
17761 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017762 }
17763
Eric Biedermanb138ac82003-04-22 18:44:01 +000017764 /* 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017774 do {
17775 expr = triple_misc(state, dt->triple, expr);
17776 awaken(state, dtriple, expr, &work_list_tail);
17777 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017778 /* 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) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017785 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000017790 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017791 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017792 }
17793 }
Eric Biederman90089602004-05-28 14:11:54 +000017794 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017795 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017800 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017801 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017802 release_triple(state, dt->triple);
17803 }
17804 }
17805 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000017806
17807 rebuild_ssa_form(state);
17808
Eric Biederman90089602004-05-28 14:11:54 +000017809 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017810}
17811
17812
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017813static 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 */
Eric Biederman83b991a2003-10-11 06:20:25 +000017824 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017825 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 */
Eric Biederman90089602004-05-28 14:11:54 +000017837 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017838 if (info.reg >= MAX_REGISTERS) {
17839 info.reg = REG_UNSET;
17840 }
Eric Biederman90089602004-05-28 14:11:54 +000017841
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017842 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 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017874 /* 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017882 }
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;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017892 /* Walk through the uses of ins and do a pre_copy or see if a post_copy is warranted */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017893 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);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017927 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017928 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);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017943 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017944 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);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017955 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017956 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000017965
Eric Biederman90089602004-05-28 14:11:54 +000017966 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017967}
17968
17969
Eric Biedermanb138ac82003-04-22 18:44:01 +000017970struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017971struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017972struct live_range {
17973 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017974 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 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017980 unsigned color;
17981 unsigned classes;
17982 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017983 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017984 struct live_range *group_next, **group_prev;
17985};
17986
17987struct live_range_edge {
17988 struct live_range_edge *next;
17989 struct live_range *node;
17990};
17991
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017992struct 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000018000#define LRE_HASH_SIZE 2048
18001struct lre_hash {
18002 struct lre_hash *next;
18003 struct live_range *left;
18004 struct live_range *right;
18005};
18006
18007
18008struct reg_state {
18009 struct lre_hash *hash[LRE_HASH_SIZE];
18010 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018011 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018012 struct live_range *lr;
18013 struct live_range *low, **low_tail;
18014 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018015 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018016 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018017 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018018};
18019
18020
Eric Biedermand1ea5392003-06-28 06:49:45 +000018021struct print_interference_block_info {
18022 struct reg_state *rstate;
18023 FILE *fp;
18024 int need_edges;
18025};
18026static 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018032 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018033 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000018040 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");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018048 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) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018084 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
Eric Biedermand1ea5392003-06-28 06:49:45 +000018091 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 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018132 }
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
18144static 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");
Eric Biederman90089602004-05-28 14:11:54 +000018152 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018153
18154}
18155
Eric Biedermanb138ac82003-04-22 18:44:01 +000018156static 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
18173static 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
18192static void reg_fill_used(struct compile_state *state, char *used, int reg)
18193{
18194 unsigned equivs[MAX_REG_EQUIVS];
18195 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018196 if (reg == REG_UNNEEDED) {
18197 return;
18198 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018199 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018206static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000018220static 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
18242static 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];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018257 while(*ptr) {
18258 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18259 break;
18260 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018261 ptr = &(*ptr)->next;
18262 }
18263 return ptr;
18264}
18265
18266static 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
18274static 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 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018298#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018299 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018300 left, right);
18301#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018302 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
18321static 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);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018340 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018341 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);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018350 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018351 break;
18352 }
18353 }
18354}
18355
18356static 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
Eric Biederman153ea352003-06-20 14:43:20 +000018365static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000018378
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
18411static 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018417 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018418 expr = triple_rhs(state, ins, 0);
18419 for(;expr; expr = triple_rhs(state, ins, expr)) {
18420 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018421 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018422 continue;
18423 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018424 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018425 if (lr->color == lr2->color) {
18426 internal_error(state, ins, "live range too big");
18427 }
18428 }
18429}
18430
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018431
18432static 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018466 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018467 FILE *fp = state->errout;
18468 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018469 lrd = lr1->defs;
18470 do {
Eric Biederman90089602004-05-28 14:11:54 +000018471 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018472 lrd = lrd->next;
18473 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018474 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018475 lrd = lr2->defs;
18476 do {
Eric Biederman90089602004-05-28 14:11:54 +000018477 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018478 lrd = lrd->next;
18479 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018480 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018481 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018482 /* 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) {
Eric Biederman90089602004-05-28 14:11:54 +000018497 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018498 }
18499 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018500 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018501 }
18502 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018503 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018504 }
18505 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018506 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018507 }
18508#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018509#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018510 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018511 lr1->defs->def,
18512 lr1->color,
18513 lr2->defs->def,
18514 lr2->color);
18515#endif
18516
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018517 /* Append lr2 onto lr1 */
18518#warning "FIXME should this be a merge instead of a splice?"
Eric Biederman153ea352003-06-20 14:43:20 +000018519 /* 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 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018525 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
Eric Biederman153ea352003-06-20 14:43:20 +000018555 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18556 transfer_live_edges(rstate, lr1, lr2);
18557
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018558 return lr1;
18559}
18560
18561static 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
18576static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000018592static void initialize_live_ranges(
18593 struct compile_state *state, struct reg_state *rstate)
18594{
18595 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018596 size_t count, size;
18597 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018598
Eric Biederman83b991a2003-10-11 06:20:25 +000018599 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018600 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018601 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018602 count = count_triples(state);
18603 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018604 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018605 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018606 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018607 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000018616 /* Setup the dummy live range */
18617 rstate->lr[0].classes = 0;
18618 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018619 rstate->lr[0].defs = 0;
18620 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018621 ins = first;
18622 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018623 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018624 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018625 struct reg_info info;
18626 /* Find the architecture specific color information */
18627 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018628 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018629 rstate->lr[i].defs = &rstate->lrd[j];
18630 rstate->lr[i].color = info.reg;
18631 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018632 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018633 rstate->lrd[j].lr = &rstate->lr[i];
18634 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018635 /* Otherwise give the triple the dummy live range. */
18636 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018637 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018638 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018639
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++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018648 ins = ins->next;
18649 } while(ins != first);
18650 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018651
Eric Biedermanb138ac82003-04-22 18:44:01 +000018652 /* Make a second pass to handle achitecture specific register
18653 * constraints.
18654 */
18655 ins = first;
18656 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018657 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 */
Eric Biederman90089602004-05-28 14:11:54 +000018663 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018664 if ((zlhs == 0) && triple_is_def(state, ins)) {
18665 zlhs = 1;
18666 }
Eric Biederman90089602004-05-28 14:11:54 +000018667 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018668
Eric Biederman5ade04a2003-10-22 04:03:46 +000018669 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018670 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018671 ins, zlhs, zrhs);
18672 }
18673
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018674 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018686
18687 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018688 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018689 i, lhs, linfo.reg);
18690 }
18691
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018692 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) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018697 continue;
18698 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018699 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018700
18701 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018702 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018703 j, rhs, rinfo.reg);
18704 }
18705
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018706 if (rinfo.reg == linfo.reg) {
18707 coalesce_ranges(state, rstate,
18708 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018709 }
18710 }
18711 }
18712 ins = ins->next;
18713 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018714}
18715
Eric Biedermanf96a8102003-06-16 16:57:34 +000018716static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018717 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018725 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018726 * we do not have a definition to add to
18727 * the interference graph.
18728 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018729 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018730 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018731 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018732 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018733
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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018740 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018747 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18748 continue;
18749 }
18750 add_live_edge(rstate, def, lr);
18751 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018752 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018753}
18754
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018755static 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
18779static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000018834
Eric Biedermanf96a8102003-06-16 16:57:34 +000018835static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018836 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;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018842 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000018843 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018844
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018845 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018846 id = ins->id;
18847 ins->id = rstate->lrd[id].orig_id;
18848 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000018849 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018850 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018851
18852 if (lr->defs) {
18853 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000018854 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018855 lrd = lr->defs;
18856 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000018857 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018858 lrd = lrd->next;
18859 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000018860 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018861 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018862 if (live) {
18863 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000018864 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018865 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000018866 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018867 }
Eric Biederman7dea9552004-06-29 05:38:37 +000018868 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018869 }
18870 if (lr->edges) {
18871 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000018872 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018873 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018874 struct live_range_def *lrd;
18875 lrd = entry->node->defs;
18876 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000018877 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018878 lrd = lrd->next;
18879 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000018880 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018881 }
Eric Biederman7dea9552004-06-29 05:38:37 +000018882 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018883 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000018884 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000018885 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018886 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018887 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018888}
18889
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018890static 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 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018979
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018980 res = coalesce_ranges(state, rstate, lr1, lr2);
18981 coalesced += 1;
18982 if (res != lr1) {
18983 goto next;
18984 }
18985 }
18986 }
18987 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000018988 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018989 }
18990 return coalesced;
18991}
18992
18993
Eric Biedermanf96a8102003-06-16 16:57:34 +000018994static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018995 struct reg_block *blocks, struct triple_reg_set *live,
18996 struct reg_block *rb, struct triple *ins, void *arg)
18997{
Eric Biedermand1ea5392003-06-28 06:49:45 +000018998 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018999 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019000
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 */
Eric Biederman90089602004-05-28 14:11:54 +000019007 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019008 if ((zlhs == 0) && triple_is_def(state, ins)) {
19009 zlhs = 1;
19010 }
Eric Biederman90089602004-05-28 14:11:54 +000019011 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019012 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019013 struct reg_info linfo;
19014 linfo = arch_reg_lhs(state, ins, i);
19015 if (linfo.reg < MAX_REGISTERS) {
19016 continue;
19017 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019018 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019019 struct reg_info rinfo;
19020 struct triple *rhs;
19021 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019022 int found;
19023 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019024 rinfo = arch_reg_rhs(state, ins, j);
19025 if (rinfo.reg != linfo.reg) {
19026 continue;
19027 }
19028 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019029 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019030 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019031 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019032 }
19033 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019034 if (found) {
19035 struct triple *copy;
19036 copy = pre_copy(state, ins, j);
19037 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019038 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019039 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019040 }
19041 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019042 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019043}
19044
Eric Biedermand1ea5392003-06-28 06:49:45 +000019045static int correct_coalesce_conflicts(
19046 struct compile_state *state, struct reg_block *blocks)
19047{
19048 int conflicts;
19049 conflicts = 0;
Eric Biederman90089602004-05-28 14:11:54 +000019050 walk_variable_lifetimes(state, &state->bb, blocks,
19051 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019052 return conflicts;
19053}
19054
Eric Biedermanf96a8102003-06-16 16:57:34 +000019055static void replace_set_use(struct compile_state *state,
19056 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019057{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019058 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019059 for(set = head; set; set = set->next) {
19060 if (set->member == orig) {
19061 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019062 }
19063 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019064}
19065
Eric Biedermanf96a8102003-06-16 16:57:34 +000019066static 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 *"
Eric Biederman90089602004-05-28 14:11:54 +000019071 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019072 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
19079static void color_instructions(struct compile_state *state)
19080{
19081 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019082 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019083 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
19097static 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 }
Eric Biederman90089602004-05-28 14:11:54 +000019105 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019106 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
19116static struct triple *resolve_tangle(
19117 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019118{
19119 struct reg_info info, uinfo;
19120 struct triple_set *set, *next;
19121 struct triple *copy;
19122
Eric Biedermanf96a8102003-06-16 16:57:34 +000019123#warning "WISHLIST recalculate all affected instructions colors"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019124 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019125 for(set = tangle->use; set; set = next) {
19126 struct triple *user;
19127 int i, zrhs;
19128 next = set->next;
19129 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019130 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019131 for(i = 0; i < zrhs; i++) {
19132 if (RHS(user, i) != tangle) {
19133 continue;
19134 }
19135 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019136 if (uinfo.reg == info.reg) {
19137 copy = pre_copy(state, user, i);
19138 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019139 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019140 }
19141 }
19142 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019143 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019144 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019145 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019146 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019147 copy = post_copy(state, tangle);
19148 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019149 linfo = find_lhs_color(state, copy, 0);
19150 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019151 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019152 info = find_lhs_color(state, tangle, 0);
19153 SET_INFO(tangle->id, info);
19154
19155 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019156}
19157
19158
Eric Biedermanf96a8102003-06-16 16:57:34 +000019159static 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{
Eric Biederman153ea352003-06-20 14:43:20 +000019163 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019164 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 }
Eric Biederman153ea352003-06-20 14:43:20 +000019190 /* 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 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019197 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;
Eric Biederman153ea352003-06-20 14:43:20 +000019204 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019205 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
Eric Biederman153ea352003-06-20 14:43:20 +000019217static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019218 struct compile_state *state, struct reg_block *blocks)
19219{
Eric Biederman153ea352003-06-20 14:43:20 +000019220 int tangles;
19221 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019222 color_instructions(state);
Eric Biederman90089602004-05-28 14:11:54 +000019223 walk_variable_lifetimes(state, &state->bb, blocks,
19224 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019225 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019226}
19227
Eric Biedermand1ea5392003-06-28 06:49:45 +000019228
19229static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19230static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19231
19232struct triple *find_constrained_def(
19233 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019234{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019235 struct live_range_def *lrd, *lrd_next;
19236 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019237 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019238 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019239 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019240
19241 lrd = lrd_next;
19242 lrd_next = lrd->next;
19243
Eric Biedermand1ea5392003-06-28 06:49:45 +000019244 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019248 /* If the 2 register class masks are equal then
19249 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019250 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019251 if (regcm == info.regcm) {
19252 continue;
19253 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019254
Eric Biederman5ade04a2003-10-22 04:03:46 +000019255 /* 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
Eric Biedermand1ea5392003-06-28 06:49:45 +000019265 /* Of the constrained live ranges deal with the
19266 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019267 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019268 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019269 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019270 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019271 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019272 if (!constrained ||
19273 tdominates(state, lrd->def, constrained))
19274 {
19275 constrained = lrd->def;
19276 }
19277 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019278 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019279}
19280
Eric Biedermand1ea5392003-06-28 06:49:45 +000019281static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019282 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019283 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019298#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Eric Biedermand1ea5392003-06-28 06:49:45 +000019299 if (!constrained) {
19300 constrained = find_constrained_def(state, range, constrained);
19301 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019302
19303 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019304 fprintf(state->errout, "constrained: ");
19305 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019306 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019307 if (constrained) {
19308 ids_from_rstate(state, rstate);
19309 cleanup_rstate(state, rstate);
19310 resolve_tangle(state, constrained);
19311 }
19312 return !!constrained;
19313}
19314
19315static int split_ranges(
19316 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019317 char *used, struct live_range *range)
19318{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019319 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019320 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019321 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019322 rstate->passes, tops(range->defs->def->op), range->defs->def);
19323 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019324 if ((range->color == REG_UNNEEDED) ||
19325 (rstate->passes >= rstate->max_passes)) {
19326 return 0;
19327 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019328 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019329
Eric Biedermand1ea5392003-06-28 06:49:45 +000019330 /* 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 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019342#warning "WISHLIST implement live range splitting..."
Eric Biederman5ade04a2003-10-22 04:03:46 +000019343
19344 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019345 FILE *fp = state->errout;
19346 print_interference_blocks(state, rstate, fp, 0);
19347 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019348 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019349 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350}
19351
Eric Biederman5ade04a2003-10-22 04:03:46 +000019352static FILE *cgdebug_fp(struct compile_state *state)
19353{
19354 FILE *fp;
19355 fp = 0;
19356 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019357 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019358 }
19359 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019360 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019361 }
19362 return fp;
19363}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019364
Eric Biederman5ade04a2003-10-22 04:03:46 +000019365static 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
19377static 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
19386static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019395static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019396 struct reg_state *rstate, struct live_range *range)
19397{
19398 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019399 struct live_range_def *lrd;
19400 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019401 struct live_range_edge *edge;
19402 char used[MAX_REGISTERS];
19403 struct triple **expr;
19404
Eric Biedermanb138ac82003-04-22 18:44:01 +000019405 /* 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019418
19419 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019420 int i;
19421 i = 0;
19422 for(edge = range->edges; edge; edge = edge->next) {
19423 i++;
19424 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019425 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");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019429 for(i = 0; i < MAX_REGISTERS; i++) {
19430 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019431 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019432 arch_reg_str(i));
19433 }
19434 }
19435 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019436
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019437 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000019467 /* 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019472 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;
Eric Biederman530b5192003-07-01 10:05:30 +000019477 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019478 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000019486 if (insd->lr->color == REG_UNSET) {
19487 continue;
19488 }
19489 regcm = insd->lr->classes;
19490 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019491 (used[insd->lr->color])) {
19492 continue;
19493 }
19494 if (interfere(rstate, range, insd->lr)) {
19495 continue;
19496 }
19497 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019498 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019499 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019500 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019501 * of something else that feeds into the phi function.
19502 */
19503 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019504 if (phi->lr->color != REG_UNSET) {
19505 if (used[phi->lr->color]) {
19506 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019507 }
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;
Eric Biederman530b5192003-07-01 10:05:30 +000019513 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019514 if (!*expr) {
19515 continue;
19516 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019517 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019518 if (lr->color == REG_UNSET) {
19519 continue;
19520 }
19521 regcm = lr->classes;
19522 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019523 (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 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019534 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)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019538 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019539 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019540 if (!*expr) {
19541 continue;
19542 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019543 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019544 if (lr->color == REG_UNSET) {
19545 continue;
19546 }
19547 regcm = lr->classes;
19548 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019549 (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) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019567 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019568 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019569 if (split_ranges(state, rstate, used, range)) {
19570 return 0;
19571 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019572 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019573 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019574 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019575 lrd = edge->node->defs;
19576 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019577 warning(state, lrd->def, " %s %p",
19578 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019579 lrd = lrd->next;
19580 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019581 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019582 warning(state, range->defs->def, "range: ");
19583 lrd = range->defs;
19584 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019585 warning(state, lrd->def, " %s %p",
19586 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019587 lrd = lrd->next;
19588 } while(lrd != range->defs);
19589
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019590 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019591 range->classes);
19592 for(i = 0; i < MAX_REGISTERS; i++) {
19593 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019594 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019595 arch_reg_str(i));
19596 }
19597 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019598 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019599 }
Eric Biederman530b5192003-07-01 10:05:30 +000019600 range->classes &= arch_reg_regcm(state, range->color);
19601 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019602 internal_error(state, range->defs->def, "select_free_color did not?");
19603 }
19604 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019605}
19606
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019607static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019608{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019609 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019610 struct live_range_edge *edge;
19611 struct live_range *range;
19612 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019613 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019614 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) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019630 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019631 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 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019647 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019648 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019649 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019650 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019667 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019668 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019678 colored = color_graph(state, rstate);
19679 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019680 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019681 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019682 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019683 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019684 if (colored) {
19685 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19686 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019687 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019688 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019689}
19690
Eric Biedermana96d6a92003-05-13 20:45:19 +000019691static 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];
Eric Biederman83b991a2003-10-11 06:20:25 +000019697 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019698 ins = first;
19699 do {
19700 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019701 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019702 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019703 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019704 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019705 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019706 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000019728static void color_triples(struct compile_state *state, struct reg_state *rstate)
19729{
Eric Biederman90089602004-05-28 14:11:54 +000019730 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019731 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019732 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019733 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019734 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019735 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019736 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019737 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019738 "triple without a live range");
19739 }
Eric Biederman90089602004-05-28 14:11:54 +000019740 lrd = &rstate->lrd[ins->id];
19741 lr = lrd->lr;
19742 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019743 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019744 ins = ins->next;
19745 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019746}
19747
Eric Biedermanb138ac82003-04-22 18:44:01 +000019748static 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) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019763 if ((first->degree < mid->degree) ||
19764 ((first->degree == mid->degree) &&
19765 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019766 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019787 if (pick) {
19788 pick->group_prev = join_tail;
19789 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019790 }
19791 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019792 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019793 first = 0;
19794 }
19795 join = first;
19796 }
19797 return join;
19798}
19799
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019800static 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 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019808 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000019809 FILE *fp = state->dbgout;
19810 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000019811 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000019812 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019813 }
Eric Biederman83b991a2003-10-11 06:20:25 +000019814 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019815 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
19826static 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
19835static 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) {
Eric Biederman90089602004-05-28 14:11:54 +000019843 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019844 }
19845 rstate->defs = 0;
19846 rstate->ranges = 0;
19847 rstate->lrd = 0;
19848 rstate->lr = 0;
19849 rstate->blocks = 0;
19850}
19851
Eric Biederman153ea352003-06-20 14:43:20 +000019852static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019853static void allocate_registers(struct compile_state *state)
19854{
19855 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019856 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019857
19858 /* Clear out the reg_state */
19859 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000019860 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019861
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019862 do {
19863 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019864 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000019865 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019866 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019867
Eric Biederman5ade04a2003-10-22 04:03:46 +000019868 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019869 FILE *fp = state->errout;
19870 fprintf(fp, "pass: %d\n", rstate.passes);
19871 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019872 }
Eric Biederman153ea352003-06-20 14:43:20 +000019873
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019874 /* Restore ids */
19875 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019876
Eric Biedermanf96a8102003-06-16 16:57:34 +000019877 /* Cleanup the temporary data structures */
19878 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019879
Eric Biedermanf96a8102003-06-16 16:57:34 +000019880 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000019881 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019882
Eric Biedermanf96a8102003-06-16 16:57:34 +000019883 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000019884 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019885
Eric Biederman153ea352003-06-20 14:43:20 +000019886 /* 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019895
Eric Biederman5ade04a2003-10-22 04:03:46 +000019896
Eric Biederman90089602004-05-28 14:11:54 +000019897 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000019898 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019899
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019900 /* Allocate and initialize the live ranges */
19901 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019902
Eric Biederman90089602004-05-28 14:11:54 +000019903 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000019904 * buys me nothing. The code is left this way in case
19905 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000019906 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000019907 */
19908 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019909 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000019910 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000019911 }
19912
Eric Biederman153ea352003-06-20 14:43:20 +000019913 /* Remove any previous live edge calculations */
19914 cleanup_live_edges(&rstate);
19915
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019916 /* Compute the interference graph */
19917 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000019918 state, &state->bb, rstate.blocks,
19919 graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000019920
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019921 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019922 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000019923 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000019924 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019925 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000019926 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019927 print_interference_ins, &rstate);
19928 }
19929
19930 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000019931
Eric Biederman5ade04a2003-10-22 04:03:46 +000019932 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000019933 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019934 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019935 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000019936
Eric Biederman83b991a2003-10-11 06:20:25 +000019937#if DEBUG_CONSISTENCY > 1
19938# if 0
Eric Biederman90089602004-05-28 14:11:54 +000019939 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000019940# endif
Eric Biederman153ea352003-06-20 14:43:20 +000019941 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000019942 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000019943 state, &state->bb, rstate.blocks,
19944 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000019945# if 0
Eric Biederman90089602004-05-28 14:11:54 +000019946 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000019947#endif
19948#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019949
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)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019975 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019976 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 {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019992 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019993 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020001
Eric Biedermana96d6a92003-05-13 20:45:19 +000020002 /* Verify the graph was properly colored */
20003 verify_colors(state, &rstate);
20004
Eric Biedermanb138ac82003-04-22 18:44:01 +000020005 /* Move the colors from the graph to the triples */
20006 color_triples(state, &rstate);
20007
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020008 /* Cleanup the temporary data structures */
20009 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020010
20011 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020012 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020013}
20014
20015/* Sparce Conditional Constant Propogation
20016 * =========================================
20017 */
20018struct ssa_edge;
20019struct flow_block;
20020struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020021 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020022 struct triple *def;
20023 struct ssa_edge *out;
20024 struct flow_block *fblock;
20025 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020026 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020027 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020028 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020029 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020030};
20031struct 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};
20038struct 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};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020047#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020048struct flow_block {
20049 struct block *block;
20050 struct flow_edge *in;
20051 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020052 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020053};
20054
20055struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020056 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020057 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
Eric Biederman90089602004-05-28 14:11:54 +000020065static 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
20070static 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
20075static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20076{
20077 return is_scc_const(state, lnode->val);
20078}
20079
20080static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000020085static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20086 struct flow_edge *fedge)
20087{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020088 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020089 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020090 fedge,
20091 fedge->src->block?fedge->src->block->last->id: 0,
20092 fedge->dst->block?fedge->dst->block->first->id: 0);
20093 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020094 if ((fedge == scc->flow_work_list) ||
20095 (fedge->work_next != fedge) ||
20096 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020097
20098 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020099 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020100 fedge);
20101 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020102 return;
20103 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020104 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
20118static 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020131 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020132 }
20133 return fedge;
20134}
20135
20136static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20137 struct ssa_edge *sedge)
20138{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020139 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020140 fprintf(state->errout, "adding sedge: %5d (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020141 sedge - scc->ssa_edges,
20142 sedge->src->def->id,
20143 sedge->dst->def->id);
20144 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020145 if ((sedge == scc->ssa_work_list) ||
20146 (sedge->work_next != sedge) ||
20147 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020148
20149 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020150 fprintf(state->errout, "dupped sedge: %5d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020151 sedge - scc->ssa_edges);
20152 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020153 return;
20154 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020155 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
20169static 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020182 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020183 }
20184 return sedge;
20185}
20186
20187static 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 */
Eric Biederman83b991a2003-10-11 06:20:25 +000020199 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020200 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020210 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020211 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20212 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020213 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020214 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020215 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 =
Eric Biederman90089602004-05-28 14:11:54 +000020220 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020221 "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 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020229 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020238 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20239 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020240 }
20241 {
20242 struct lattice_node *lnode;
20243 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020244 lnode = &scc->lattice[ins_index];
20245 lnode->def = ins;
20246 lnode->out = 0;
20247 lnode->fblock = fblock;
20248 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020249 if (lnode->val->op == OP_UNKNOWNVAL) {
20250 lnode->val = 0; /* LATTICE LOW by definition */
20251 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020252 lnode->old_id = ins->id;
20253 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020254 }
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 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020262 {
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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020281 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020338 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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020346 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020347 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020348 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020349 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020350 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020351 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020365 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020366 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020367 ins_index, ssa_edge_index, fblock_index);
20368 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020369}
20370
20371
20372static void free_scc_state(
20373 struct compile_state *state, struct scc_state *scc)
20374{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020375 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020376 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020377 struct flow_block *fblock;
20378 fblock = &scc->flow_blocks[i];
20379 if (fblock->edges) {
20380 xfree(fblock->edges);
20381 fblock->edges = 0;
20382 }
20383 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020384 xfree(scc->flow_blocks);
20385 xfree(scc->ssa_edges);
20386 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020387
Eric Biedermanb138ac82003-04-22 18:44:01 +000020388}
20389
20390static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020399static 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
20416static 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020425 if (changed &&
20426 lnode->val && old &&
20427 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020428 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020429 (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
Eric Biederman5ade04a2003-10-22 04:03:46 +000020439static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020440 struct compile_state *state, struct scc_state *scc,
20441 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020442{
Eric Biederman90089602004-05-28 14:11:54 +000020443 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20444 display_triple_changes(state->errout, lnode->val, lnode->def);
20445 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020446 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020447 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020448 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",
Eric Biederman90089602004-05-28 14:11:54 +000020465 (is_lattice_hi(state, lnode)? "hi":
20466 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020467 changed? "changed" : ""
20468 );
20469 }
20470}
20471
Eric Biedermanb138ac82003-04-22 18:44:01 +000020472static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20473 struct lattice_node *lnode)
20474{
20475 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020476 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020477 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020478 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020479
20480 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020481 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020482
Eric Biedermanb138ac82003-04-22 18:44:01 +000020483 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020484 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020485 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020486 scratch->next = scratch;
20487 scratch->prev = scratch;
20488 scratch->use = 0;
20489
Eric Biederman90089602004-05-28 14:11:54 +000020490 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020491 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020492 dexpr = &lnode->def->param[i];
20493 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020494 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020495 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20496 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020497 *dexpr) {
20498 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020499 tmp = triple_to_lattice(state, scc, *dexpr);
20500 *vexpr = (tmp->val)? tmp->val : tmp->def;
20501 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020502 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020503 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020504 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020505 }
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 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020511 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020512 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020513 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020514 internal_error(state, lnode->def, "scratch used?");
20515 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020516 if ((scratch->prev != scratch) ||
20517 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020518 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020519 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020520 internal_error(state, lnode->def, "scratch in list?");
20521 }
20522 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020523 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020524 for(i = 0; i < count; i++) {
20525 vexpr = &scratch->param[i];
20526 if (*vexpr) {
20527 unuse_triple(*vexpr, scratch);
20528 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020529 }
Eric Biederman90089602004-05-28 14:11:54 +000020530 if (lnode->val->op == OP_UNKNOWNVAL) {
20531 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020532 }
Eric Biederman90089602004-05-28 14:11:54 +000020533 /* Find the case when I am lattice high */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020534 if (lnode->val &&
20535 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020536 (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)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020539 lnode->val = lnode->def;
20540 }
Eric Biederman90089602004-05-28 14:11:54 +000020541 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020558 /* Find the cases that are always lattice lo */
20559 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020560 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020561 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020562 lnode->val = 0;
20563 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020564 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020565 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020566 /* See if this value should not change */
Eric Biederman90089602004-05-28 14:11:54 +000020567 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020568 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020569 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020570 (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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020577
Eric Biederman83b991a2003-10-11 06:20:25 +000020578 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020579 if (lnode->val != scratch) {
20580 xfree(scratch);
20581 }
Eric Biederman90089602004-05-28 14:11:54 +000020582
Eric Biedermanb138ac82003-04-22 18:44:01 +000020583 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020584}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020585
Eric Biederman90089602004-05-28 14:11:54 +000020586
20587static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020588 struct lattice_node *lnode)
20589{
20590 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020591 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020592 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000020603 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020604 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020605 FILE *fp = state->errout;
20606 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020607 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020608 lnode->def->id);
20609
20610 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020611 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020612 }
Eric Biederman90089602004-05-28 14:11:54 +000020613 fprintf(fp, " )");
20614 if (lnode->def->rhs > 0) {
20615 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020616 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020617 }
Eric Biederman90089602004-05-28 14:11:54 +000020618 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020619 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020620 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020621 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 }
Eric Biederman90089602004-05-28 14:11:54 +000020637 /* 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?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020642 return;
20643 }
Eric Biederman90089602004-05-28 14:11:54 +000020644 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020645 scc_add_fedge(state, scc, left);
20646 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020647 }
20648 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020649 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020650 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020651 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020652 }
20653
20654}
20655
Eric Biederman90089602004-05-28 14:11:54 +000020656
20657static void scc_add_sedge_dst(struct compile_state *state,
20658 struct scc_state *scc, struct ssa_edge *sedge)
20659{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020660 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020661 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
20668static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000020741static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20742 struct lattice_node *lnode)
20743{
20744 int changed;
20745
Eric Biederman90089602004-05-28 14:11:54 +000020746 if (!triple_is_def(state, lnode->def)) {
20747 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020748 }
Eric Biederman90089602004-05-28 14:11:54 +000020749 changed = compute_lnode_val(state, scc, lnode);
20750 scc_debug_lnode(state, scc, lnode, changed);
20751
20752 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020753 struct ssa_edge *sedge;
20754 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020755 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020756 }
20757 }
20758}
20759
20760static void scc_writeback_values(
20761 struct compile_state *state, struct scc_state *scc)
20762{
20763 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020764 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020765 ins = first;
20766 do {
20767 struct lattice_node *lnode;
20768 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020769 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020770 if (is_lattice_hi(state, lnode) &&
20771 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020772 {
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) {
Eric Biederman90089602004-05-28 14:11:54 +000020781 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020782 "lattice node %d %s->%s still high?",
20783 ins->id,
20784 tops(lnode->def->op),
20785 tops(lnode->val->op));
20786 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020787 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020788 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020789
Eric Biederman83b991a2003-10-11 06:20:25 +000020790 /* Restore id */
20791 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020792 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,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020800 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020801 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020809 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020816 }
20817 ins = ins->next;
20818 } while(ins != first);
20819}
20820
20821static void scc_transform(struct compile_state *state)
20822{
20823 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020824 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
20825 return;
20826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020827
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;
Eric Biederman83b991a2003-10-11 06:20:25 +000020838 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020839 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000020852 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020853 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
20854 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000020855 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020856 }
20857 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020858
Eric Biederman5ade04a2003-10-22 04:03:46 +000020859 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020860 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020861 block->vertex, reps);
20862 }
20863
Eric Biedermanb138ac82003-04-22 18:44:01 +000020864 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 }
Eric Biederman90089602004-05-28 14:11:54 +000020872 else if ((reps == 1) && triple_is_def(state, ptr))
20873 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020874 scc_visit_expr(state, &scc, lnode);
20875 }
20876 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020877 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000020878 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020879 struct flow_edge *out;
20880 for(out = fblock->out; out; out = out->out_next) {
20881 scc_add_fedge(state, &scc, out);
20882 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020883 }
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;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020890
20891 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020892 fprintf(state->errout, "sedge: %5d (%5d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020893 sedge - scc.ssa_edges,
20894 sedge->src->def->id,
20895 sedge->dst->def->id);
20896 }
20897
Eric Biedermanb138ac82003-04-22 18:44:01 +000020898 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);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020915 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020916 rebuild_ssa_form(state);
20917
Eric Biederman90089602004-05-28 14:11:54 +000020918 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020919}
20920
20921
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020922static void transform_to_arch_instructions(struct compile_state *state)
20923{
20924 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000020925 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020926 ins = first;
20927 do {
20928 ins = transform_to_arch_instruction(state, ins);
20929 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020930
Eric Biederman90089602004-05-28 14:11:54 +000020931 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020932}
Eric Biedermanb138ac82003-04-22 18:44:01 +000020933
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020934#if DEBUG_CONSISTENCY
20935static void verify_uses(struct compile_state *state)
20936{
20937 struct triple *first, *ins;
20938 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000020939 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020940 ins = first;
20941 do {
20942 struct triple **expr;
20943 expr = triple_rhs(state, ins, 0);
20944 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000020945 struct triple *rhs;
20946 rhs = *expr;
20947 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020948 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)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000020958 struct triple *lhs;
20959 lhs = *expr;
20960 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020961 if (set->member == ins) {
20962 break;
20963 }
20964 }
20965 if (!set) {
20966 internal_error(state, ins, "lhs not used");
20967 }
20968 }
Eric Biederman90089602004-05-28 14:11:54 +000020969 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020999 ins = ins->next;
21000 } while(ins != first);
21001
21002}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021003static void verify_blocks_present(struct compile_state *state)
21004{
21005 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021006 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021007 return;
21008 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021009 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021010 ins = first;
21011 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021012 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021013 if (triple_stores_block(state, ins)) {
21014 if (!ins->u.block) {
21015 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021016 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021017 }
21018 }
21019 ins = ins->next;
21020 } while(ins != first);
21021
21022
21023}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021024
21025static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021038static void verify_blocks(struct compile_state *state)
21039{
21040 struct triple *ins;
21041 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021042 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021043 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021044 if (!block) {
21045 return;
21046 }
Eric Biederman530b5192003-07-01 10:05:30 +000021047 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021048 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021049 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021050 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021051 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021052 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021053 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021054 internal_error(state, ins, "inconsitent block specified");
21055 }
Eric Biederman530b5192003-07-01 10:05:30 +000021056 valid_ins(state, ins);
21057 }
21058 users = 0;
21059 for(user = block->use; user; user = user->next) {
21060 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021061 if (!user->member->first) {
21062 internal_error(state, block->first, "user is empty");
21063 }
Eric Biederman90089602004-05-28 14:11:54 +000021064 if ((block == state->bb.last_block) &&
21065 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021066 continue;
21067 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021068 for(edge = user->member->edges; edge; edge = edge->next) {
21069 if (edge->member == block) {
21070 break;
21071 }
21072 }
21073 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021074 internal_error(state, user->member->first,
21075 "user does not use block");
21076 }
21077 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021078 if (triple_is_branch(state, block->last)) {
21079 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021080 expr = triple_edge_targ(state, block->last, 0);
21081 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021082 if (*expr && !edge_present(state, block, *expr)) {
21083 internal_error(state, block->last, "no edge to targ");
21084 }
21085 }
Eric Biederman530b5192003-07-01 10:05:30 +000021086 }
Eric Biederman90089602004-05-28 14:11:54 +000021087 if (!triple_is_ubranch(state, block->last) &&
21088 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021089 !edge_present(state, block, block->last->next)) {
21090 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021091 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021092 for(edge = block->edges; edge; edge = edge->next) {
21093 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021094 if (user->member == block) {
21095 break;
21096 }
21097 }
21098 if (!user || user->member != block) {
21099 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021100 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021101 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021102 if (!edge->member->first) {
21103 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021104 }
Eric Biederman530b5192003-07-01 10:05:30 +000021105 }
21106 if (block->users != users) {
21107 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021108 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021109 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021110 }
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 }
Eric Biederman90089602004-05-28 14:11:54 +000021120 } 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);
Eric Biederman530b5192003-07-01 10:05:30 +000021124 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021125}
21126
21127static void verify_domination(struct compile_state *state)
21128{
21129 struct triple *first, *ins;
21130 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021131 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021132 return;
21133 }
21134
Eric Biederman83b991a2003-10-11 06:20:25 +000021135 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021136 ins = first;
21137 do {
21138 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021139 struct triple **slot;
21140 struct triple *use_point;
21141 int i, zrhs;
21142 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021143 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021144 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) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021148 break;
21149 }
21150 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021151 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,
Eric Biederman90089602004-05-28 14:11:54 +000021162 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021163 }
21164 use_point = bset->member->last;
21165 }
21166 }
21167 if (use_point &&
21168 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021169 if (is_const(ins)) {
21170 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021171 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021172 }
21173 else {
21174 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021175 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021176 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021177 }
21178 }
21179 ins = ins->next;
21180 } while(ins != first);
21181}
21182
Eric Biederman83b991a2003-10-11 06:20:25 +000021183static 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;
Eric Biederman90089602004-05-28 14:11:54 +000021191 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021192 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021209static void verify_piece(struct compile_state *state)
21210{
21211 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021212 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021213 ins = first;
21214 do {
21215 struct triple *ptr;
21216 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021217 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021218 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}
Eric Biederman83b991a2003-10-11 06:20:25 +000021235
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021236static void verify_ins_colors(struct compile_state *state)
21237{
21238 struct triple *first, *ins;
21239
Eric Biederman83b991a2003-10-11 06:20:25 +000021240 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021241 ins = first;
21242 do {
21243 ins = ins->next;
21244 } while(ins != first);
21245}
Eric Biederman90089602004-05-28 14:11:54 +000021246
21247static 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
21295static 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
21316static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021340static void verify_consistency(struct compile_state *state)
21341{
Eric Biederman90089602004-05-28 14:11:54 +000021342 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021343 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021344 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021345 verify_blocks(state);
21346 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021347 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021348 verify_piece(state);
21349 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021350 verify_types(state);
21351 verify_copy(state);
21352 if (state->compiler->debug & DEBUG_VERIFICATION) {
21353 fprintf(state->dbgout, "consistency verified\n");
21354 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021355}
21356#else
Eric Biederman153ea352003-06-20 14:43:20 +000021357static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021358#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021359
21360static void optimize(struct compile_state *state)
21361{
Eric Biederman90089602004-05-28 14:11:54 +000021362 /* Join all of the functions into one giant function */
21363 join_functions(state);
21364
Eric Biederman5ade04a2003-10-22 04:03:46 +000021365 /* Dump what the instruction graph intially looks like */
21366 print_triples(state);
21367
Eric Biederman0babc1c2003-05-09 02:39:00 +000021368 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021369 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021370 print_triples(state);
21371
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021372 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021373 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021374 state->bb.first = state->first;
21375 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021376
Eric Biederman530b5192003-07-01 10:05:30 +000021377 /* 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 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021386 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021387 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021388
Eric Biederman83b991a2003-10-11 06:20:25 +000021389 /* Remove dead code */
21390 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021391 verify_consistency(state);
21392
Eric Biedermanb138ac82003-04-22 18:44:01 +000021393 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021394 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021395 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021396 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021397 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021398 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021399#warning "WISHLIST implement single use constants (least possible register pressure)"
21400#warning "WISHLIST implement induction variable elimination"
Eric Biedermanb138ac82003-04-22 18:44:01 +000021401 /* Select architecture instructions and an initial partial
21402 * coloring based on architecture constraints.
21403 */
21404 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021405 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021406
Eric Biederman83b991a2003-10-11 06:20:25 +000021407 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021408 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021409 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021410
Eric Biedermanb138ac82003-04-22 18:44:01 +000021411 /* Color all of the variables to see if they will fit in registers */
21412 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021413 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021414
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021415 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021416 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021417
Eric Biedermanb138ac82003-04-22 18:44:01 +000021418 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021419 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021420
Eric Biedermanb138ac82003-04-22 18:44:01 +000021421 /* Remove the optimization information.
21422 * This is more to check for memory consistency than to free memory.
21423 */
Eric Biederman90089602004-05-28 14:11:54 +000021424 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021425}
21426
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021427static 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;
Eric Biederman90089602004-05-28 14:11:54 +000021434 lhs = ins->lhs;
21435 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021436 /* 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;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021443 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021444 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)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021469 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021470 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021471 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021472}
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
Eric Biederman83b991a2003-10-11 06:20:25 +000021480/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021481#define X86_MMX_REGS (1<<0)
21482#define X86_XMM_REGS (1<<1)
21483#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021484
Eric Biedermanb138ac82003-04-22 18:44:01 +000021485/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021486#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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021500#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021501#if LAST_REGC >= MAX_REGC
21502#error "MAX_REGC is to low"
21503#endif
21504
21505/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021506#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)
Eric Biederman90089602004-05-28 14:11:54 +000021521#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021522
21523/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021524#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021525#define REGC_FLAGS_FIRST REG_EFLAGS
21526#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021527#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
Eric Biederman530b5192003-07-01 10:05:30 +000021535#define REGC_GPR8_LO_FIRST REG_AL
21536#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021537#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021538#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021539#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
Eric Biedermanb138ac82003-04-22 18:44:01 +000021547#define REGC_GPR16_FIRST REG_AX
21548#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021549#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
Eric Biedermanb138ac82003-04-22 18:44:01 +000021557#define REGC_GPR32_FIRST REG_EAX
21558#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021559#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021560#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
Eric Biedermanb138ac82003-04-22 18:44:01 +000021573#define REGC_MMX_FIRST REG_MMX0
21574#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021575#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
Eric Biedermanb138ac82003-04-22 18:44:01 +000021583#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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021593#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
Eric Biedermanb138ac82003-04-22 18:44:01 +000021600#if LAST_REG >= MAX_REGISTERS
21601#error "MAX_REGISTERS to low"
21602#endif
21603
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021604
21605static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021606 [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,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021620};
21621
21622static const struct {
21623 int first, last;
21624} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021625 [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 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021639};
21640
Eric Biederman90089602004-05-28 14:11:54 +000021641#if ARCH_INPUT_REGS != 4
21642#error ARCH_INPUT_REGS size mismatch
21643#endif
21644static 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
21654static 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
Eric Biederman5ade04a2003-10-22 04:03:46 +000021661static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021662{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021663 memset(arch, 0, sizeof(*arch));
21664 arch->features = 0;
21665}
21666
Eric Biederman90089602004-05-28 14:11:54 +000021667static 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};
21673static 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};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021684static int arch_encode_flag(struct arch_state *arch, const char *flag)
21685{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021686 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;
Eric Biederman83b991a2003-10-11 06:20:25 +000021694 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021695 if (act && strncmp(flag, "cpu=", 4) == 0) {
21696 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021697 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021698 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021699 else {
Eric Biederman90089602004-05-28 14:11:54 +000021700 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021701 }
21702 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021703}
21704
Eric Biederman90089602004-05-28 14:11:54 +000021705static void arch_usage(FILE *fp)
21706{
21707 flag_usage(fp, arch_flags, "-m", "-mno-");
21708 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21709}
21710
Eric Biedermanb138ac82003-04-22 18:44:01 +000021711static unsigned arch_regc_size(struct compile_state *state, int class)
21712{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021713 if ((class < 0) || (class > LAST_REGC)) {
21714 return 0;
21715 }
21716 return regc_size[class];
21717}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021718
Eric Biedermanb138ac82003-04-22 18:44:01 +000021719static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21720{
21721 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021722 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21723 REGCM_GPR32_8 | REGCM_GPR32 |
21724 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021725
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021726 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021733 return (regcm1 & regcm2) ||
21734 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21735}
21736
21737static 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:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021746#if X86_4_8BIT_GPRS
21747 *equiv++ = REG_AH;
21748#endif
21749 *equiv++ = REG_AX;
21750 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021751 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021752 *equiv++ = REG_EDXEAX;
21753 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021754 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021755#if X86_4_8BIT_GPRS
21756 *equiv++ = REG_AL;
21757#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021758 *equiv++ = REG_AX;
21759 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021760 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021761 *equiv++ = REG_EDXEAX;
21762 break;
21763 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021764#if X86_4_8BIT_GPRS
21765 *equiv++ = REG_BH;
21766#endif
21767 *equiv++ = REG_BX;
21768 *equiv++ = REG_EBX;
21769 break;
21770
Eric Biedermanb138ac82003-04-22 18:44:01 +000021771 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021772#if X86_4_8BIT_GPRS
21773 *equiv++ = REG_BL;
21774#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021775 *equiv++ = REG_BX;
21776 *equiv++ = REG_EBX;
21777 break;
21778 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021779#if X86_4_8BIT_GPRS
21780 *equiv++ = REG_CH;
21781#endif
21782 *equiv++ = REG_CX;
21783 *equiv++ = REG_ECX;
21784 break;
21785
Eric Biedermanb138ac82003-04-22 18:44:01 +000021786 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021787#if X86_4_8BIT_GPRS
21788 *equiv++ = REG_CL;
21789#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021790 *equiv++ = REG_CX;
21791 *equiv++ = REG_ECX;
21792 break;
21793 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021794#if X86_4_8BIT_GPRS
21795 *equiv++ = REG_DH;
21796#endif
21797 *equiv++ = REG_DX;
21798 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000021799 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021800 *equiv++ = REG_EDXEAX;
21801 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021802 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021803#if X86_4_8BIT_GPRS
21804 *equiv++ = REG_DL;
21805#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021806 *equiv++ = REG_DX;
21807 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000021808 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021809 *equiv++ = REG_EDXEAX;
21810 break;
21811 case REG_AX:
21812 *equiv++ = REG_AL;
21813 *equiv++ = REG_AH;
21814 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021815 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021816 *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;
Eric Biederman530b5192003-07-01 10:05:30 +000021832 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021833 *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;
Eric Biederman530b5192003-07-01 10:05:30 +000021851 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021852 *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;
Eric Biederman530b5192003-07-01 10:05:30 +000021868 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021869 *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;
Eric Biederman530b5192003-07-01 10:05:30 +000021883 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021894 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;
Eric Biederman530b5192003-07-01 10:05:30 +000021903 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021904 break;
21905 }
21906 *equiv++ = REG_UNSET;
21907}
21908
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021909static unsigned arch_avail_mask(struct compile_state *state)
21910{
21911 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000021912 /* 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 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021916 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021917 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021918 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000021919 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021920 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021921 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021922 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021923 return avail_mask;
21924}
21925
21926static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
21927{
21928 unsigned mask, result;
21929 int class, class2;
21930 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021931
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 }
Eric Biederman530b5192003-07-01 10:05:30 +000021946 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021947 return result;
21948}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021949
Eric Biedermand1ea5392003-06-28 06:49:45 +000021950static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000021960static unsigned arch_reg_regcm(struct compile_state *state, int reg)
21961{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021962 unsigned mask;
21963 int class;
21964 mask = 0;
21965 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021966 if ((reg >= regcm_bound[class].first) &&
21967 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021968 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021977static 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[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021985 { 'r', REGCM_GPR32, REG_UNSET },
21986 { 'g', REGCM_GPR32, REG_UNSET },
21987 { 'p', REGCM_GPR32, REG_UNSET },
21988 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021989 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000021990 { '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 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021998 { '\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
22038static 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 }
Eric Biederman90089602004-05-28 14:11:54 +000022046 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022047 result.reg = REG_EAX;
22048 result.regcm = REGCM_GPR32;
22049 }
Eric Biederman90089602004-05-28 14:11:54 +000022050 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022051 result.reg = REG_EBX;
22052 result.regcm = REGCM_GPR32;
22053 }
Eric Biederman90089602004-05-28 14:11:54 +000022054 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022055 result.reg = REG_ECX;
22056 result.regcm = REGCM_GPR32;
22057 }
Eric Biederman90089602004-05-28 14:11:54 +000022058 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022059 result.reg = REG_EDX;
22060 result.regcm = REGCM_GPR32;
22061 }
Eric Biederman90089602004-05-28 14:11:54 +000022062 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022063 result.reg = REG_ESI;
22064 result.regcm = REGCM_GPR32;
22065 }
Eric Biederman90089602004-05-28 14:11:54 +000022066 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022067 result.reg = REG_EDI;
22068 result.regcm = REGCM_GPR32;
22069 }
Eric Biederman90089602004-05-28 14:11:54 +000022070 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022071 result.reg = REG_EBP;
22072 result.regcm = REGCM_GPR32;
22073 }
Eric Biederman90089602004-05-28 14:11:54 +000022074 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022075 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 }
Eric Biederman90089602004-05-28 14:11:54 +000022087 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022088 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22089 result.reg = REG_MMX0 + octdigval(clobber[3]);
22090 result.regcm = REGCM_MMX;
22091 }
22092 else {
Eric Biederman90089602004-05-28 14:11:54 +000022093 error(state, 0, "unknown register name `%s' in asm",
22094 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022095 result.reg = REG_UNSET;
22096 result.regcm = 0;
22097 }
22098 return result;
22099}
22100
Eric Biedermanb138ac82003-04-22 18:44:01 +000022101static 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
22112static int arch_select_free_register(
22113 struct compile_state *state, char *used, int classes)
22114{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022115 /* 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 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022127 */
22128 int i, reg;
22129 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022130 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022131 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 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022136 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022137 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022142 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22143 reg = do_select_reg(state, used, i, classes);
22144 }
Eric Biederman530b5192003-07-01 10:05:30 +000022145 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++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022152 reg = do_select_reg(state, used, i, classes);
22153 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022154 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22155 reg = do_select_reg(state, used, i, classes);
22156 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022157 return reg;
22158}
22159
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022160
Eric Biedermanb138ac82003-04-22 18:44:01 +000022161static 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"
Eric Biedermanb138ac82003-04-22 18:44:01 +000022164 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022165 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:
Eric Biederman530b5192003-07-01 10:05:30 +000022173 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022174 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022175 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022176 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022177 REGCM_MMX | REGCM_XMM |
22178 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022179 break;
22180 case TYPE_SHORT:
22181 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022182 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022183 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022184 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022185 REGCM_MMX | REGCM_XMM |
22186 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022187 break;
Eric Biederman90089602004-05-28 14:11:54 +000022188 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022189 case TYPE_INT:
22190 case TYPE_UINT:
22191 case TYPE_LONG:
22192 case TYPE_ULONG:
22193 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022194 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022195 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22196 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022197 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022198 break;
Eric Biederman90089602004-05-28 14:11:54 +000022199 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022210 default:
Eric Biederman90089602004-05-28 14:11:54 +000022211 fprintf(state->errout, "type: ");
22212 name_of(state->errout, type);
22213 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022214 internal_error(state, 0, "no register class for type");
22215 break;
22216 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022217 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022218 return mask;
22219}
22220
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022221static int is_imm32(struct triple *imm)
22222{
22223 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
22224 (imm->op == OP_ADDRCONST);
22225
22226}
22227static int is_imm16(struct triple *imm)
22228{
22229 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22230}
22231static int is_imm8(struct triple *imm)
22232{
22233 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22234}
22235
22236static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022237{
22238 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022239 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022240 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022241 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022242 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022243 if (!is_imm32(imm)) {
22244 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022245 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022246 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022247 use_triple(imm, ins);
22248 *expr = imm;
22249 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022250}
22251
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022252static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022253{
22254 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022255 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022256 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022257 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022258 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022259 if (!is_imm8(imm)) {
22260 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022261 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022262 unuse_triple(*expr, ins);
22263 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022264 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022265 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022266}
22267
Eric Biederman530b5192003-07-01 10:05:30 +000022268#define TEMPLATE_NOP 0
22269#define TEMPLATE_INTCONST8 1
22270#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022271#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
Eric Biederman530b5192003-07-01 10:05:30 +000022335#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022336#if LAST_TEMPLATE >= MAX_TEMPLATES
22337#error "MAX_TEMPLATES to low"
22338#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022339
Eric Biederman530b5192003-07-01 10:05:30 +000022340#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)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022343
Eric Biedermanb138ac82003-04-22 18:44:01 +000022344
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022345static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022346 [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 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022414 [TEMPLATE_INTCONST8] = {
22415 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22416 },
22417 [TEMPLATE_INTCONST32] = {
22418 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22419 },
Eric Biederman90089602004-05-28 14:11:54 +000022420 [TEMPLATE_UNKNOWNVAL] = {
22421 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22422 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022423 [TEMPLATE_COPY8_REG] = {
22424 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22425 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022426 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022427 [TEMPLATE_COPY16_REG] = {
22428 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22429 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22430 },
22431 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022432 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022433 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022434 },
22435 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022436 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022437 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22438 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022439 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022440 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022441 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22442 },
22443 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022444 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022445 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22446 },
22447 [TEMPLATE_PHI8] = {
22448 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022449 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22450 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022451 [TEMPLATE_PHI16] = {
22452 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022453 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22454 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022455 [TEMPLATE_PHI32] = {
22456 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022457 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22458 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022459 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022460 .rhs = {
22461 [0] = { REG_UNSET, REGCM_GPR32 },
22462 [1] = { REG_UNSET, REGCM_GPR8_LO },
22463 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022464 },
22465 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022466 .rhs = {
22467 [0] = { REG_UNSET, REGCM_GPR32 },
22468 [1] = { REG_UNSET, REGCM_GPR16 },
22469 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022470 },
22471 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022472 .rhs = {
22473 [0] = { REG_UNSET, REGCM_GPR32 },
22474 [1] = { REG_UNSET, REGCM_GPR32 },
22475 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022476 },
22477 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022478 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022479 .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 },
Eric Biederman530b5192003-07-01 10:05:30 +000022489 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022504 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22505 .rhs = {
22506 [0] = { REG_VIRT0, REGCM_GPR32 },
22507 [1] = { REG_UNSET, REGCM_GPR32 },
22508 },
22509 },
Eric Biederman530b5192003-07-01 10:05:30 +000022510 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022525 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22526 .rhs = {
22527 [0] = { REG_VIRT0, REGCM_GPR32 },
22528 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22529 },
22530 },
Eric Biederman530b5192003-07-01 10:05:30 +000022531 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022546 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22547 .rhs = {
22548 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022549 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022550 },
22551 },
Eric Biederman530b5192003-07-01 10:05:30 +000022552 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022567 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22568 .rhs = {
22569 [0] = { REG_VIRT0, REGCM_GPR32 },
22570 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22571 },
22572 },
Eric Biederman530b5192003-07-01 10:05:30 +000022573 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022582 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22583 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22584 },
Eric Biederman530b5192003-07-01 10:05:30 +000022585 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022600 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22601 .rhs = {
22602 [0] = { REG_UNSET, REGCM_GPR32 },
22603 [1] = { REG_UNSET, REGCM_GPR32 },
22604 },
22605 },
Eric Biederman530b5192003-07-01 10:05:30 +000022606 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022621 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22622 .rhs = {
22623 [0] = { REG_UNSET, REGCM_GPR32 },
22624 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22625 },
22626 },
Eric Biederman530b5192003-07-01 10:05:30 +000022627 [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] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022636 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22637 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22638 },
22639 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022640 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022641 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22642 },
22643 [TEMPLATE_JMP] = {
22644 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22645 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022646 [TEMPLATE_RET] = {
22647 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22648 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022649 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022650 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022651 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22652 },
22653 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022654 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022655 .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 = {
Eric Biederman530b5192003-07-01 10:05:30 +000022675 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022676 [1] = { REG_DX, REGCM_GPR16 },
22677 },
22678 },
22679 [TEMPLATE_OUTB_IMM] = {
22680 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022681 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022682 [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 },
Eric Biederman530b5192003-07-01 10:05:30 +000022727 [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 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022743 .rhs = {
22744 [0] = { REG_EAX, REGCM_GPR32 },
22745 [1] = { REG_UNSET, REGCM_GPR32 },
22746 },
22747 },
Eric Biederman530b5192003-07-01 10:05:30 +000022748 [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] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022769 .lhs = {
22770 [0] = { REG_EAX, REGCM_GPR32 },
22771 [1] = { REG_EDX, REGCM_GPR32 },
22772 },
22773 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022774 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022775 [1] = { REG_UNSET, REGCM_GPR32 },
22776 },
22777 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022778};
Eric Biedermanb138ac82003-04-22 18:44:01 +000022779
Eric Biederman83b991a2003-10-11 06:20:25 +000022780static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000022806static 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 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022815 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022816 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000022817 struct triple *left, *right;
22818 left = right = 0;
22819 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000022820 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022821 right = RHS(cmp, 1);
22822 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022823 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000022824 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000022825 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022826 }
22827 }
22828}
22829
22830static void bool_cmp(struct compile_state *state,
22831 struct triple *ins, int cmp_op, int jmp_op, int set_op)
22832{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022833 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000022834 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022835
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;
Eric Biederman530b5192003-07-01 10:05:30 +000022843 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022844 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000022845 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022846 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000022847 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022848 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022849 }
22850 /* Generate the instruction sequence that will transform the
22851 * result of the comparison into a logical value.
22852 */
Eric Biederman90089602004-05-28 14:11:54 +000022853 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022854 use_triple(ins, set);
22855 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022856
Eric Biederman90089602004-05-28 14:11:54 +000022857 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000022864 for(entry = ins->use; entry; entry = next) {
22865 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022866 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022867 continue;
22868 }
Eric Biederman90089602004-05-28 14:11:54 +000022869 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022870 }
Eric Biederman90089602004-05-28 14:11:54 +000022871 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000022872}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022873
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022874struct 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 }
Eric Biederman90089602004-05-28 14:11:54 +000022883 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022884 if (triple_is_def(state, ins)) {
22885 zlhs = 1;
22886 }
22887 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000022888 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022889 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
22914struct 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;
Eric Biederman90089602004-05-28 14:11:54 +000022918 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022919 (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;
Eric Biederman90089602004-05-28 14:11:54 +000022927 case OP_PHI:
22928 index = 0;
22929 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022930 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
Eric Biederman530b5192003-07-01 10:05:30 +000022946static 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
Eric Biederman530b5192003-07-01 10:05:30 +000022951 /* 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);
Eric Biederman90089602004-05-28 14:11:54 +000022955 piece0 = LHS(div, 0);
22956 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000022957 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
Eric Biederman530b5192003-07-01 10:05:30 +000022963 /* 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
Eric Biederman90089602004-05-28 14:11:54 +000022971static 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
22988static 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
23050static 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
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023098static struct triple *transform_to_arch_instruction(
23099 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023100{
23101 /* Transform from generic 3 address instructions
23102 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023103 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023104 * Copies are inserted to preserve the register flexibility
23105 * of 3 address instructions.
23106 */
Eric Biederman90089602004-05-28 14:11:54 +000023107 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023108 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023109 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023115 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023116 break;
23117 case OP_ADDRCONST:
23118 ins->template_id = TEMPLATE_INTCONST32;
23119 break;
Eric Biederman90089602004-05-28 14:11:54 +000023120 case OP_UNKNOWNVAL:
23121 ins->template_id = TEMPLATE_UNKNOWNVAL;
23122 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023123 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:
Eric Biederman90089602004-05-28 14:11:54 +000023130 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023131 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023132 value = RHS(ins, 0);
23133 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023134 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023135 }
Eric Biederman90089602004-05-28 14:11:54 +000023136 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023137 ins->template_id = TEMPLATE_COPY_IMM16;
23138 }
Eric Biederman90089602004-05-28 14:11:54 +000023139 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023140 ins->template_id = TEMPLATE_COPY_IMM32;
23141 }
Eric Biederman90089602004-05-28 14:11:54 +000023142 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023143 internal_error(state, ins, "bad constant passed to copy");
23144 }
Eric Biederman90089602004-05-28 14:11:54 +000023145 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023146 ins->template_id = TEMPLATE_COPY8_REG;
23147 }
Eric Biederman90089602004-05-28 14:11:54 +000023148 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023149 ins->template_id = TEMPLATE_COPY16_REG;
23150 }
Eric Biederman90089602004-05-28 14:11:54 +000023151 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023152 ins->template_id = TEMPLATE_COPY32_REG;
23153 }
23154 else {
23155 internal_error(state, ins, "bad type passed to copy");
23156 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023157 break;
23158 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023159 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023160 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023161 ins->template_id = TEMPLATE_PHI8;
23162 }
Eric Biederman90089602004-05-28 14:11:54 +000023163 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023164 ins->template_id = TEMPLATE_PHI16;
23165 }
Eric Biederman90089602004-05-28 14:11:54 +000023166 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023167 ins->template_id = TEMPLATE_PHI32;
23168 }
23169 else {
23170 internal_error(state, ins, "bad type passed to phi");
23171 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023172 break;
Eric Biederman90089602004-05-28 14:11:54 +000023173 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023183 case OP_STORE:
23184 switch(ins->type->type & TYPE_MASK) {
23185 case TYPE_CHAR: case TYPE_UCHAR:
23186 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023187 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023188 case TYPE_SHORT: case TYPE_USHORT:
23189 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023190 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023191 case TYPE_INT: case TYPE_UINT:
23192 case TYPE_LONG: case TYPE_ULONG:
23193 case TYPE_POINTER:
23194 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023195 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023196 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023197 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023198 break;
23199 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023200 break;
23201 case OP_LOAD:
23202 switch(ins->type->type & TYPE_MASK) {
23203 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023204 case TYPE_SHORT: case TYPE_USHORT:
23205 case TYPE_INT: case TYPE_UINT:
23206 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023207 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023208 break;
23209 default:
23210 internal_error(state, ins, "unknown type in load");
23211 break;
23212 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023213 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023214 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:
Eric Biederman530b5192003-07-01 10:05:30 +000023221 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023222 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023223 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023224 }
23225 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023226 case OP_SDIVT:
23227 case OP_UDIVT:
23228 ins->template_id = TEMPLATE_DIV32;
23229 next = after_lhs(state, ins);
23230 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023231 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023232 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023233 break;
23234 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023235 next = mod_div(state, ins, OP_UDIVT, 0);
23236 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023237 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023238 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023239 break;
23240 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023241 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023242 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023243 case OP_SMOD:
23244 next = mod_div(state, ins, OP_SDIVT, 1);
23245 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023246 case OP_SL:
23247 case OP_SSR:
23248 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023249 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023250 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023251 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023252 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23253 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023254 }
23255 break;
23256 case OP_INVERT:
23257 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023258 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023259 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:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023297 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023309 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:
Eric Biederman530b5192003-07-01 10:05:30 +000023354 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023355 break;
23356 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023357 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023358 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023359 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023360 }
23361 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023362 case OP_JMP:
23363 ins->template_id = TEMPLATE_NOP;
23364 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023365 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;
Eric Biederman90089602004-05-28 14:11:54 +000023379 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;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023386 /* Unhandled instructions */
23387 case OP_PIECE:
23388 default:
Eric Biederman90089602004-05-28 14:11:54 +000023389 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023390 ins->op, tops(ins->op));
23391 break;
23392 }
23393 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023394}
23395
Eric Biederman530b5192003-07-01 10:05:30 +000023396static long next_label(struct compile_state *state)
23397{
Eric Biederman90089602004-05-28 14:11:54 +000023398 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023399 return ++label_counter;
23400}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023401static void generate_local_labels(struct compile_state *state)
23402{
23403 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023404 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023405 label = first;
23406 do {
23407 if ((label->op == OP_LABEL) ||
23408 (label->op == OP_SDECL)) {
23409 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023410 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023411 } else {
23412 label->u.cval = 0;
23413 }
23414
23415 }
23416 label = label->next;
23417 } while(label != first);
23418}
23419
23420static 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023429 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
Eric Biederman90089602004-05-28 14:11:54 +000023437
Eric Biederman530b5192003-07-01 10:05:30 +000023438#if REG_XMM7 != 44
23439#error "Registers have renumberd fix arch_reg_str"
23440#endif
Eric Biederman90089602004-05-28 14:11:54 +000023441static 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};
23454static const char *arch_reg_str(int reg)
23455{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023456 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23457 reg = 0;
23458 }
Eric Biederman90089602004-05-28 14:11:54 +000023459 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023460}
23461
23462static 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
Eric Biederman90089602004-05-28 14:11:54 +000023470static 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
23501static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000023513const char *type_suffix(struct compile_state *state, struct type *type)
23514{
23515 const char *suffix;
23516 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023517 case SIZEOF_I8: suffix = "b"; break;
23518 case SIZEOF_I16: suffix = "w"; break;
23519 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023520 default:
23521 internal_error(state, 0, "unknown suffix");
23522 suffix = 0;
23523 break;
23524 }
23525 return suffix;
23526}
23527
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023528static 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 ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023534 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023535 break;
23536 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023537 if ((MISC(ins, 0)->op != OP_SDECL) &&
23538 (MISC(ins, 0)->op != OP_LABEL))
23539 {
Eric Biederman830c9882003-07-04 00:27:33 +000023540 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 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023545 fprintf(fp, " $L%s%lu+%lu ",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023546 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023547 (unsigned long)(MISC(ins, 0)->u.cval),
23548 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023549 break;
23550 default:
23551 internal_error(state, ins, "unknown constant type");
23552 break;
23553 }
23554}
23555
Eric Biederman530b5192003-07-01 10:05:30 +000023556static 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:
Eric Biederman83b991a2003-10-11 06:20:25 +000023564 fprintf(fp, ".byte 0x%02lx\n",
23565 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023566 break;
23567 case TYPE_SHORT:
23568 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000023569 fprintf(fp, ".short 0x%04lx\n",
23570 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023571 break;
23572 case TYPE_INT:
23573 case TYPE_UINT:
23574 case TYPE_LONG:
23575 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023576 case TYPE_POINTER:
Eric Biederman83b991a2003-10-11 06:20:25 +000023577 fprintf(fp, ".int %lu\n",
23578 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023579 break;
23580 default:
Eric Biederman90089602004-05-28 14:11:54 +000023581 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));
Eric Biederman530b5192003-07-01 10:05:30 +000023586 }
Eric Biederman90089602004-05-28 14:11:54 +000023587
Eric Biederman530b5192003-07-01 10:05:30 +000023588 break;
23589 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023590 if ((MISC(ins, 0)->op != OP_SDECL) &&
23591 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023592 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",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023598 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023599 (unsigned long)(MISC(ins, 0)->u.cval),
23600 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023601 break;
23602 case OP_BLOBCONST:
23603 {
23604 unsigned char *blob;
23605 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023606 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023607 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
23623static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023624 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023625{
Eric Biederman90089602004-05-28 14:11:54 +000023626 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023627 long ref;
23628 ref = next_label(state);
23629 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biederman90089602004-05-28 14:11:54 +000023630 fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023631 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023632 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023633 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000023637 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23638 return ref;
23639}
23640
Eric Biederman90089602004-05-28 14:11:54 +000023641static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000023658static void print_binary_op(struct compile_state *state,
23659 const char *op, struct triple *ins, FILE *fp)
23660{
23661 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023662 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023663 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023664 internal_error(state, ins, "invalid register assignment");
23665 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023666 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023667 fprintf(fp, "\t%s ", op);
23668 print_const_val(state, RHS(ins, 1), fp);
23669 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023670 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023671 }
23672 else {
23673 unsigned lmask, rmask;
23674 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023675 lreg = check_reg(state, RHS(ins, 0), mask);
23676 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023677 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,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023682 reg(state, RHS(ins, 1), mask),
23683 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023684 }
23685}
23686static void print_unary_op(struct compile_state *state,
23687 const char *op, struct triple *ins, FILE *fp)
23688{
23689 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023690 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023691 fprintf(fp, "\t%s %s\n",
23692 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023693 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023694}
23695
23696static void print_op_shift(struct compile_state *state,
23697 const char *op, struct triple *ins, FILE *fp)
23698{
23699 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023700 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023701 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023702 internal_error(state, ins, "invalid register assignment");
23703 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023704 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023705 fprintf(fp, "\t%s ", op);
23706 print_const_val(state, RHS(ins, 1), fp);
23707 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023708 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023709 }
23710 else {
23711 fprintf(fp, "\t%s %s, %s\n",
23712 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023713 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023714 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023715 }
23716}
23717
23718static 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) {
Eric Biederman530b5192003-07-01 10:05:30 +000023725 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023726 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023737 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023738 fprintf(fp, "\t%s ", op);
23739 print_const_val(state, RHS(ins, 0), fp);
23740 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023741 reg(state, ins, mask));
23742 }
23743 else {
23744 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023745 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023746 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,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023751 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023752 reg(state, ins, mask));
23753 }
23754}
23755
23756static 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) {
Eric Biederman530b5192003-07-01 10:05:30 +000023763 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023764 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023771 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023772 if (!reg_is_reg(state, lreg, REG_EAX)) {
23773 internal_error(state, ins, "src != %%eax");
23774 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023775 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023776 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");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023780 }
23781 else {
23782 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023783 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023784 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,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023789 reg(state, RHS(ins, 0), mask),
23790 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023791 }
23792}
23793
23794static 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.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023799 * Because OP_COPY will be introduced in arbitrary locations
23800 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000023801 * OP_CONVERT can change the flags and it is the only operation
23802 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023803 */
23804 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
23805 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000023806 if (state->arch->features & X86_NOOP_COPY) {
23807 omit_copy = 0;
23808 }
23809 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023810 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023811 dst = ins;
23812 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023813 else {
23814 internal_error(state, ins, "unknown move operation");
23815 src = dst = 0;
23816 }
Eric Biederman90089602004-05-28 14:11:54 +000023817 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
Eric Biederman0babc1c2003-05-09 02:39:00 +000023830 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023831 int src_reg, dst_reg;
23832 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000023833 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023834 dst_reg = ID_REG(dst->id);
23835 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000023836 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023837 /* If the class is the same just move the register */
23838 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000023839 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023840 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 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000023856 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023866 /* Move 32bit to 8bit */
23867 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000023868 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000023869 {
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) &&
Eric Biederman530b5192003-07-01 10:05:30 +000023879 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000023880 {
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 */
Eric Biederman530b5192003-07-01 10:05:30 +000023889 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023890 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023891 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) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023901 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023902 reg(state, src, src_regcm),
23903 reg(state, dst, dst_regcm));
23904 }
23905 }
Eric Biederman530b5192003-07-01 10:05:30 +000023906 /* Move between mmx registers */
23907 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023908 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023909 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023910 reg(state, src, src_regcm),
23911 reg(state, dst, dst_regcm));
23912 }
23913 }
Eric Biederman530b5192003-07-01 10:05:30 +000023914 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023926 /* 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 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000023933 /* 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;
Eric Biederman678d8162003-07-03 03:59:38 +000023938 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000023939 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 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000023947 /* 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 }
Eric Biederman530b5192003-07-01 10:05:30 +000023955 /* 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023995#if X86_4_8BIT_GPRS
23996 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000023997 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023998 (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)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024012 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024013 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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024019 /* Move from 32bit gprs to 8bit gprs */
24020 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024021 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024022 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) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024031 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024032 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 */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024040 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024050 else {
Eric Biederman90089602004-05-28 14:11:54 +000024051 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24052 internal_error(state, ins, "attempt to copy from %%eflags!");
24053 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024054 internal_error(state, ins, "unknown copy type");
24055 }
24056 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024057 else {
Eric Biederman90089602004-05-28 14:11:54 +000024058 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024059 int dst_reg;
24060 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024061 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024062 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) {
Eric Biederman90089602004-05-28 14:11:54 +000024071 if (dst_size > SIZEOF_I32) {
24072 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024073 }
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) {
Eric Biederman90089602004-05-28 14:11:54 +000024080 if (dst_size > SIZEOF_I16) {
24081 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024082 }
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;
Eric Biederman90089602004-05-28 14:11:54 +000024090 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);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024094 fprintf(fp, "\tmovd L%s%lu, %s\n",
24095 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024096 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24097 }
24098 else {
24099 internal_error(state, ins, "unknown copy immediate type");
24100 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024101 }
Eric Biederman90089602004-05-28 14:11:54 +000024102 /* 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024197}
24198
24199static void print_op_load(struct compile_state *state,
24200 struct triple *ins, FILE *fp)
24201{
24202 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024203 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024204 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024205 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024206 if (is_const(src) || is_const(dst)) {
24207 internal_error(state, ins, "unknown load operation");
24208 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024209 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,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024226 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024227 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024228}
24229
24230
24231static void print_op_store(struct compile_state *state,
24232 struct triple *ins, FILE *fp)
24233{
24234 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024235 dst = RHS(ins, 0);
24236 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024237 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),
Eric Biederman83b991a2003-10-11 06:20:25 +000024242 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024243 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),
Eric Biederman530b5192003-07-01 10:05:30 +000024248 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024249 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024250 }
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),
Eric Biederman530b5192003-07-01 10:05:30 +000024257 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024258 reg(state, dst, REGCM_GPR32));
24259 }
24260
24261
24262}
24263
24264static void print_op_smul(struct compile_state *state,
24265 struct triple *ins, FILE *fp)
24266{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024267 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024268 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024269 reg(state, RHS(ins, 1), REGCM_GPR32),
24270 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024271 }
24272 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024273 fprintf(fp, "\timul ");
24274 print_const_val(state, RHS(ins, 1), fp);
24275 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024276 }
24277}
24278
24279static void print_op_cmp(struct compile_state *state,
24280 struct triple *ins, FILE *fp)
24281{
24282 unsigned mask;
24283 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024284 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024285 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 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024289 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024290 fprintf(fp, "\tcmp ");
24291 print_const_val(state, RHS(ins, 1), fp);
24292 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024293 }
24294 else {
24295 unsigned lmask, rmask;
24296 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024297 lreg = check_reg(state, RHS(ins, 0), mask);
24298 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024299 lmask = arch_reg_regcm(state, lreg);
24300 rmask = arch_reg_regcm(state, rreg);
24301 mask = lmask & rmask;
24302 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024303 reg(state, RHS(ins, 1), mask),
24304 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024305 }
24306}
24307
24308static void print_op_test(struct compile_state *state,
24309 struct triple *ins, FILE *fp)
24310{
24311 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024312 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024313 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024314 reg(state, RHS(ins, 0), mask),
24315 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024316}
24317
24318static void print_op_branch(struct compile_state *state,
24319 struct triple *branch, FILE *fp)
24320{
24321 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024322 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024323 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024324 internal_error(state, branch, "jmp with condition?");
24325 }
24326 bop = "jmp";
24327 }
24328 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024329 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024330 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024331 internal_error(state, branch, "jmpcc without condition?");
24332 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024333 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24334 if ((RHS(branch, 0)->op != OP_CMP) &&
24335 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024336 internal_error(state, branch, "bad branch test");
24337 }
24338#warning "FIXME I have observed instructions between the test and branch instructions"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024339 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024344 }
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 }
Eric Biederman90089602004-05-28 14:11:54 +000024362#if 1
24363 if (branch->op == OP_CALL) {
24364 fprintf(fp, "\t/* call */\n");
24365 }
24366#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024367 fprintf(fp, "\t%s L%s%lu\n",
24368 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024369 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024370 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024371}
24372
Eric Biederman5ade04a2003-10-22 04:03:46 +000024373static 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000024380static void print_op_set(struct compile_state *state,
24381 struct triple *set, FILE *fp)
24382{
24383 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024384 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024385 internal_error(state, set, "setcc without condition?");
24386 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024387 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24388 if ((RHS(set, 0)->op != OP_CMP) &&
24389 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024390 internal_error(state, set, "bad set test");
24391 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024392 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024393 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",
Eric Biederman530b5192003-07-01 10:05:30 +000024411 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024412}
24413
24414static 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,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024432 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024433 reg(state, ins, REGCM_GPR32),
24434 reg(state, ins, REGCM_GPR32));
24435}
24436
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024437
Eric Biedermanb138ac82003-04-22 18:44:01 +000024438static void print_sdecl(struct compile_state *state,
24439 struct triple *ins, FILE *fp)
24440{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024441 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biederman90089602004-05-28 14:11:54 +000024442 fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000024443 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024444 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024445 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024446 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024447
24448}
24449
24450static 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) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024457 case OP_ASM:
24458 print_op_asm(state, ins, fp);
24459 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024460 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;
Eric Biederman90089602004-05-28 14:11:54 +000024471 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024472 case OP_INTCONST:
24473 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024474 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024475 /* Don't generate anything here for constants */
24476 case OP_PHI:
24477 /* Don't generate anything for variable declarations. */
24478 break;
Eric Biederman90089602004-05-28 14:11:54 +000024479 case OP_UNKNOWNVAL:
24480 fprintf(fp, " /* unknown %s */\n",
24481 reg(state, ins, REGCM_ALL));
24482 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024483 case OP_SDECL:
24484 print_sdecl(state, ins, fp);
24485 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024486 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024487 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024488 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:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024507 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024508 print_op_branch(state, ins, fp);
24509 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024510 case OP_RET:
24511 print_op_ret(state, ins, fp);
24512 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024513 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024530 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024531 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024532 fprintf(fp, "\trdmsr\n");
24533 break;
24534 case OP_WRMSR:
24535 fprintf(fp, "\twrmsr\n");
24536 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024537 case OP_HLT:
24538 fprintf(fp, "\thlt\n");
24539 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024540 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024549 case OP_LABEL:
24550 if (!ins->use) {
24551 return;
24552 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024553 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024554 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024555 break;
Eric Biederman90089602004-05-28 14:11:54 +000024556 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;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024562 /* Ignore OP_PIECE */
24563 case OP_PIECE:
24564 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024565 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024566 case OP_SDIV: case OP_UDIV:
24567 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024568 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
24578static void print_instructions(struct compile_state *state)
24579{
24580 struct triple *first, *ins;
24581 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024582 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024583 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024584 int max_inline_depth;
24585 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024586 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024587 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024588 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024589 /* 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");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024596 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024597 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024598 ins = first;
24599 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024600 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;
Eric Biederman530b5192003-07-01 10:05:30 +000024611 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024612 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024613 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024614 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024615 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024616 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");
Eric Biederman530b5192003-07-01 10:05:30 +000024623 if (inline_depth > max_inline_depth) {
24624 max_inline_depth = inline_depth;
24625 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024626 }
24627 if (last_occurance) {
24628 put_occurance(last_occurance);
24629 }
24630 get_occurance(ins->occurance);
24631 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024632 }
24633
24634 print_instruction(state, ins, fp);
24635 ins = ins->next;
24636 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024637 if (print_location) {
24638 fprintf(fp, "/* max inline depth %d */\n",
24639 max_inline_depth);
24640 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024641}
Eric Biederman530b5192003-07-01 10:05:30 +000024642
Eric Biedermanb138ac82003-04-22 18:44:01 +000024643static void generate_code(struct compile_state *state)
24644{
24645 generate_local_labels(state);
24646 print_instructions(state);
24647
24648}
24649
Eric Biederman90089602004-05-28 14:11:54 +000024650static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024651{
Eric Biederman90089602004-05-28 14:11:54 +000024652 int tok;
24653 FILE *fp;
24654 int line;
24655 const char *filename;
24656 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024657 filename = 0;
24658 line = 0;
24659 for(;;) {
Eric Biederman41203d92004-11-08 09:31:09 +000024660 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024661 const char *token_str;
24662 tok = peek(state);
24663 if (tok == TOK_EOF) {
24664 break;
24665 }
Eric Biederman41203d92004-11-08 09:31:09 +000024666 tk = eat(state, tok);
Eric Biederman90089602004-05-28 14:11:54 +000024667 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024668 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024669 tk->str_len ? tk->val.str :
24670 tokens[tk->tok];
Eric Biedermanb138ac82003-04-22 18:44:01 +000024671
Eric Biederman90089602004-05-28 14:11:54 +000024672 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 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024702}
24703
Eric Biederman5ade04a2003-10-22 04:03:46 +000024704static void compile(const char *filename,
24705 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024706{
24707 int i;
24708 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024709 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024710 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024711 state.compiler = compiler;
24712 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024713 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 }
Eric Biederman90089602004-05-28 14:11:54 +000024718 /* Remember the output descriptors */
24719 state.errout = stderr;
24720 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024721 /* Remember the output filename */
Eric Biederman5ade04a2003-10-22 04:03:46 +000024722 state.output = fopen(state.compiler->ofilename, "w");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024723 if (!state.output) {
24724 error(&state, 0, "Cannot open output file %s\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024725 state.compiler->ofilename);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024726 }
Eric Biederman90089602004-05-28 14:11:54 +000024727 /* Make certain a good cleanup happens */
24728 exit_state = &state;
24729 atexit(exit_cleanup);
24730
Eric Biedermanb138ac82003-04-22 18:44:01 +000024731 /* Prep the preprocessor */
24732 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024733 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024734 /* register the C keywords */
24735 register_keywords(&state);
24736 /* register the keywords the macro preprocessor knows */
24737 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024738 /* generate some builtin macros */
24739 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024740 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024741 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. */
Eric Biederman90089602004-05-28 14:11:54 +000024748 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);
Eric Biederman83b991a2003-10-11 06:20:25 +000024759
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
Eric Biederman5ade04a2003-10-22 04:03:46 +000024769 /* 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
Eric Biedermanb138ac82003-04-22 18:44:01 +000024774 /* Enter the globl definition scope */
24775 start_scope(&state);
24776 register_builtins(&state);
24777 compile_file(&state, filename, 1);
Eric Biederman90089602004-05-28 14:11:54 +000024778
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
Eric Biedermanb138ac82003-04-22 18:44:01 +000024785 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024786
Eric Biedermanb138ac82003-04-22 18:44:01 +000024787 /* 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);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024794
Eric Biedermanb138ac82003-04-22 18:44:01 +000024795 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024796 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000024797 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024798 }
Eric Biederman90089602004-05-28 14:11:54 +000024799 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024800}
24801
Eric Biederman90089602004-05-28 14:11:54 +000024802static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024803{
Eric Biederman90089602004-05-28 14:11:54 +000024804 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024805}
24806
24807static void usage(void)
24808{
Eric Biederman90089602004-05-28 14:11:54 +000024809 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"
Eric Biedermanb138ac82003-04-22 18:44:01 +000024828 );
24829}
24830
24831static 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
24841int main(int argc, char **argv)
24842{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024843 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024844 struct compiler_state compiler;
24845 struct arch_state arch;
24846 int all_opts;
Eric Biederman90089602004-05-28 14:11:54 +000024847
24848
24849 /* I don't want any surprises */
24850 setlocale(LC_ALL, "C");
24851
Eric Biederman5ade04a2003-10-22 04:03:46 +000024852 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];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024859 argv += 2;
24860 argc -= 2;
24861 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024862 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000024863 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024864 result = -1;
24865 if (strcmp(argv[1], "--") == 0) {
24866 result = 0;
24867 all_opts = 1;
24868 }
Eric Biederman90089602004-05-28 14:11:54 +000024869 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) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000024882 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 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024893 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000024894 arg_error("Invalid option specified: %s\n",
24895 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024896 }
24897 argv++;
24898 argc--;
24899 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024900 else {
24901 if (filename) {
24902 arg_error("Only one filename may be specified\n");
24903 }
24904 filename = argv[1];
24905 argv++;
24906 argc--;
24907 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024908 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024909 if (!filename) {
24910 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024911 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024912 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024913
24914 return 0;
24915}