blob: c487566d0a762e3bb787b10d331200464ce8c37d [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"
6#define VERSION_MINOR "63"
7#define RELEASE_DATE "28 May 2004"
8#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.
585 * TARG(1) holds the branch target.
586 * ->next holds where to branch to if the branch is not taken.
587 * The branch target can only be a label
588 */
589
590#define OP_CALL 83 /* an uncontional branch that will return */
591/* For call instructions
592 * MISC(0) holds the OP_RET that returns from the branch
593 * TARG(0) holds the branch target.
594 * ->next holds where to branch to if the branch is not taken.
595 * The branch target can only be a label
596 */
597
598#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
599/* For call instructions
600 * RHS(0) holds the variable with the return address
601 * The branch target can only be a label
602 */
603
604#define OP_LABEL 86
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 Biedermanb138ac82003-04-22 18:44:01 +00001060 struct token token[4];
1061 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +00001062 struct hash_entry *i_switch;
1063 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001064 struct hash_entry *i_continue;
1065 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +00001066 struct hash_entry *i_default;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001067 struct hash_entry *i_return;
Eric Biederman90089602004-05-28 14:11:54 +00001068 /* Additional hash entries for predefined macros */
1069 struct hash_entry *i_defined;
1070 struct hash_entry *i___VA_ARGS__;
1071 struct hash_entry *i___FILE__;
1072 struct hash_entry *i___LINE__;
1073 /* Additional hash entries for predefined identifiers */
1074 struct hash_entry *i___func__;
1075 /* Additional hash entries for attributes */
1076 struct hash_entry *i_noinline;
1077 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001078 int scope_depth;
Eric Biederman90089602004-05-28 14:11:54 +00001079 unsigned char if_bytes[(MAX_CPP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
1080 int if_depth;
1081 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001082 int macro_line;
1083 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001084 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001085 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001086 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001087 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001088 struct basic_blocks bb;
1089 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001090};
1091
Eric Biederman0babc1c2003-05-09 02:39:00 +00001092/* visibility global/local */
1093/* static/auto duration */
1094/* typedef, register, inline */
1095#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001096#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001097/* Visibility */
1098#define STOR_GLOBAL 0x0001
1099/* Duration */
1100#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001101/* Definition locality */
1102#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001103/* Storage specifiers */
1104#define STOR_AUTO 0x0000
1105#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001106#define STOR_LOCAL 0x0003
1107#define STOR_EXTERN 0x0007
1108#define STOR_INLINE 0x0008
1109#define STOR_REGISTER 0x0010
1110#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001111
Eric Biederman5ade04a2003-10-22 04:03:46 +00001112#define QUAL_SHIFT 5
1113#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001114#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001115#define QUAL_CONST 0x0020
1116#define QUAL_VOLATILE 0x0040
1117#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001118
1119#define TYPE_SHIFT 8
1120#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001121#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1122#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001123#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1124#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001125#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1126#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001127#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1128#define TYPE_DEFAULT 0x0000
1129#define TYPE_VOID 0x0100
1130#define TYPE_CHAR 0x0200
1131#define TYPE_UCHAR 0x0300
1132#define TYPE_SHORT 0x0400
1133#define TYPE_USHORT 0x0500
1134#define TYPE_INT 0x0600
1135#define TYPE_UINT 0x0700
1136#define TYPE_LONG 0x0800
1137#define TYPE_ULONG 0x0900
1138#define TYPE_LLONG 0x0a00 /* long long */
1139#define TYPE_ULLONG 0x0b00
1140#define TYPE_FLOAT 0x0c00
1141#define TYPE_DOUBLE 0x0d00
1142#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001143
1144/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1145#define TYPE_ENUM 0x1600
1146#define TYPE_LIST 0x1700
1147/* TYPE_LIST is a basic building block when defining enumerations
1148 * type->field_ident holds the name of this enumeration entry.
1149 * type->right holds the entry in the list.
1150 */
1151
Eric Biederman0babc1c2003-05-09 02:39:00 +00001152#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001153/* For TYPE_STRUCT
1154 * type->left holds the link list of TYPE_PRODUCT entries that
1155 * make up the structure.
1156 * type->elements hold the length of the linked list
1157 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001158#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001159/* For TYPE_UNION
1160 * type->left holds the link list of TYPE_OVERLAP entries that
1161 * make up the union.
1162 * type->elements hold the length of the linked list
1163 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001164#define TYPE_POINTER 0x1200
1165/* For TYPE_POINTER:
1166 * type->left holds the type pointed to.
1167 */
1168#define TYPE_FUNCTION 0x1300
1169/* For TYPE_FUNCTION:
1170 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001171 * type->right holds the type of the arguments
1172 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001173 */
1174#define TYPE_PRODUCT 0x1400
1175/* TYPE_PRODUCT is a basic building block when defining structures
1176 * type->left holds the type that appears first in memory.
1177 * type->right holds the type that appears next in memory.
1178 */
1179#define TYPE_OVERLAP 0x1500
1180/* TYPE_OVERLAP is a basic building block when defining unions
1181 * type->left and type->right holds to types that overlap
1182 * each other in memory.
1183 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001184#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001185/* TYPE_ARRAY is a basic building block when definitng arrays.
1186 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001187 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001188 */
Eric Biederman90089602004-05-28 14:11:54 +00001189#define TYPE_TUPLE 0x1900
1190/* TYPE_TUPLE is a basic building block when defining
1191 * positionally reference type conglomerations. (i.e. closures)
1192 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1193 * except it has no field names.
1194 * type->left holds the liked list of TYPE_PRODUCT entries that
1195 * make up the closure type.
1196 * type->elements hold the number of elements in the closure.
1197 */
1198#define TYPE_JOIN 0x1a00
1199/* TYPE_JOIN is a basic building block when defining
1200 * positionally reference type conglomerations. (i.e. closures)
1201 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1202 * except it has no field names.
1203 * type->left holds the liked list of TYPE_OVERLAP entries that
1204 * make up the closure type.
1205 * type->elements hold the number of elements in the closure.
1206 */
1207#define TYPE_BITFIELD 0x1b00
1208/* TYPE_BITFIED is the type of a bitfield.
1209 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1210 * type->elements holds the number of bits in the bitfield.
1211 */
1212#define TYPE_UNKNOWN 0x1c00
1213/* TYPE_UNKNOWN is the type of an unknown value.
1214 * Used on unknown consts and other places where I don't know the type.
1215 */
1216
1217#define ATTRIB_SHIFT 16
1218#define ATTRIB_MASK 0xffff0000
1219#define ATTRIB_NOINLINE 0x00010000
1220#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001221
Eric Biederman83b991a2003-10-11 06:20:25 +00001222#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001223
1224struct type {
1225 unsigned int type;
1226 struct type *left, *right;
1227 ulong_t elements;
1228 struct hash_entry *field_ident;
1229 struct hash_entry *type_ident;
1230};
1231
Eric Biederman530b5192003-07-01 10:05:30 +00001232#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001233#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001234#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001235#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001236#define MAX_REGISTERS 75
1237#define REGISTER_BITS 7
1238#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001239#define REG_ERROR 0
1240#define REG_UNSET 1
1241#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001242#define REG_VIRT0 (MAX_REGISTERS + 0)
1243#define REG_VIRT1 (MAX_REGISTERS + 1)
1244#define REG_VIRT2 (MAX_REGISTERS + 2)
1245#define REG_VIRT3 (MAX_REGISTERS + 3)
1246#define REG_VIRT4 (MAX_REGISTERS + 4)
1247#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001248#define REG_VIRT6 (MAX_REGISTERS + 6)
1249#define REG_VIRT7 (MAX_REGISTERS + 7)
1250#define REG_VIRT8 (MAX_REGISTERS + 8)
1251#define REG_VIRT9 (MAX_REGISTERS + 9)
1252
1253#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1254#error "MAX_VIRT_REGISTERS to small"
1255#endif
Eric Biederman90089602004-05-28 14:11:54 +00001256#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001257#error "Too many id bits used"
1258#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001259
1260/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001261#define REG_SHIFT 0
1262#define REGC_SHIFT REGISTER_BITS
1263#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001264#define REG_MASK (MAX_VIRT_REGISTERS -1)
1265#define ID_REG(ID) ((ID) & REG_MASK)
1266#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001267#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1268#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1269#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1270 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001271
Eric Biederman90089602004-05-28 14:11:54 +00001272#define ARCH_INPUT_REGS 4
1273#define ARCH_OUTPUT_REGS 4
1274
1275static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1276static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001277static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001278static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001279static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001280static void arch_reg_equivs(
1281 struct compile_state *state, unsigned *equiv, int reg);
1282static int arch_select_free_register(
1283 struct compile_state *state, char *used, int classes);
1284static unsigned arch_regc_size(struct compile_state *state, int class);
1285static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1286static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1287static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001288static struct reg_info arch_reg_constraint(
1289 struct compile_state *state, struct type *type, const char *constraint);
1290static struct reg_info arch_reg_clobber(
1291 struct compile_state *state, const char *clobber);
1292static struct reg_info arch_reg_lhs(struct compile_state *state,
1293 struct triple *ins, int index);
1294static struct reg_info arch_reg_rhs(struct compile_state *state,
1295 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001296static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001297static struct triple *transform_to_arch_instruction(
1298 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001299static struct triple *flatten(
1300 struct compile_state *state, struct triple *first, struct triple *ptr);
1301
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001302
1303
Eric Biedermanb138ac82003-04-22 18:44:01 +00001304
Eric Biederman5ade04a2003-10-22 04:03:46 +00001305#define DEBUG_ABORT_ON_ERROR 0x00000001
1306#define DEBUG_BASIC_BLOCKS 0x00000002
1307#define DEBUG_FDOMINATORS 0x00000004
1308#define DEBUG_RDOMINATORS 0x00000008
1309#define DEBUG_TRIPLES 0x00000010
1310#define DEBUG_INTERFERENCE 0x00000020
1311#define DEBUG_SCC_TRANSFORM 0x00000040
1312#define DEBUG_SCC_TRANSFORM2 0x00000080
1313#define DEBUG_REBUILD_SSA_FORM 0x00000100
1314#define DEBUG_INLINE 0x00000200
1315#define DEBUG_RANGE_CONFLICTS 0x00000400
1316#define DEBUG_RANGE_CONFLICTS2 0x00000800
1317#define DEBUG_COLOR_GRAPH 0x00001000
1318#define DEBUG_COLOR_GRAPH2 0x00002000
1319#define DEBUG_COALESCING 0x00004000
1320#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001321#define DEBUG_VERIFICATION 0x00010000
1322#define DEBUG_CALLS 0x00020000
1323#define DEBUG_CALLS2 0x00040000
1324#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001325
1326#define DEBUG_DEFAULT ( \
1327 DEBUG_ABORT_ON_ERROR | \
1328 DEBUG_BASIC_BLOCKS | \
1329 DEBUG_FDOMINATORS | \
1330 DEBUG_RDOMINATORS | \
1331 DEBUG_TRIPLES | \
1332 0 )
1333
Eric Biederman90089602004-05-28 14:11:54 +00001334#define DEBUG_ALL ( \
1335 DEBUG_ABORT_ON_ERROR | \
1336 DEBUG_BASIC_BLOCKS | \
1337 DEBUG_FDOMINATORS | \
1338 DEBUG_RDOMINATORS | \
1339 DEBUG_TRIPLES | \
1340 DEBUG_INTERFERENCE | \
1341 DEBUG_SCC_TRANSFORM | \
1342 DEBUG_SCC_TRANSFORM2 | \
1343 DEBUG_REBUILD_SSA_FORM | \
1344 DEBUG_INLINE | \
1345 DEBUG_RANGE_CONFLICTS | \
1346 DEBUG_RANGE_CONFLICTS2 | \
1347 DEBUG_COLOR_GRAPH | \
1348 DEBUG_COLOR_GRAPH2 | \
1349 DEBUG_COALESCING | \
1350 DEBUG_COALESCING2 | \
1351 DEBUG_VERIFICATION | \
1352 DEBUG_CALLS | \
1353 DEBUG_CALLS2 | \
1354 DEBUG_TOKENS | \
1355 0 )
1356
1357#define COMPILER_INLINE_MASK 0x00000007
1358#define COMPILER_INLINE_ALWAYS 0x00000000
1359#define COMPILER_INLINE_NEVER 0x00000001
1360#define COMPILER_INLINE_DEFAULTON 0x00000002
1361#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1362#define COMPILER_INLINE_NOPENALTY 0x00000004
1363#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1364#define COMPILER_SIMPLIFY 0x00000010
1365#define COMPILER_SCC_TRANSFORM 0x00000020
1366#define COMPILER_SIMPLIFY_OP 0x00000040
1367#define COMPILER_SIMPLIFY_PHI 0x00000080
1368#define COMPILER_SIMPLIFY_LABEL 0x00000100
1369#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1370#define COMPILER_SIMPLIFY_COPY 0x00000400
1371#define COMPILER_SIMPLIFY_ARITH 0x00000800
1372#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1373#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1374#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1375#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1376
1377#define COMPILER_CPP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001378
1379#define COMPILER_DEFAULT_FLAGS ( \
1380 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001381 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001382 COMPILER_SIMPLIFY_OP | \
1383 COMPILER_SIMPLIFY_PHI | \
1384 COMPILER_SIMPLIFY_LABEL | \
1385 COMPILER_SIMPLIFY_BRANCH | \
1386 COMPILER_SIMPLIFY_COPY | \
1387 COMPILER_SIMPLIFY_ARITH | \
1388 COMPILER_SIMPLIFY_SHIFT | \
1389 COMPILER_SIMPLIFY_BITWISE | \
1390 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001391 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001392 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001393
Eric Biederman153ea352003-06-20 14:43:20 +00001394#define GLOBAL_SCOPE_DEPTH 1
1395#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001396
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001397static void compile_file(struct compile_state *old_state, const char *filename, int local);
1398
Eric Biederman5ade04a2003-10-22 04:03:46 +00001399
1400
1401static void init_compiler_state(struct compiler_state *compiler)
1402{
1403 memset(compiler, 0, sizeof(*compiler));
1404 compiler->label_prefix = "";
1405 compiler->ofilename = "auto.inc";
1406 compiler->flags = COMPILER_DEFAULT_FLAGS;
1407 compiler->debug = 0;
1408 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001409 compiler->include_path_count = 1;
1410 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1411 compiler->define_count = 1;
1412 compiler->defines = xcmalloc(sizeof(char *), "defines");
1413 compiler->undef_count = 1;
1414 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001415}
1416
1417struct compiler_flag {
1418 const char *name;
1419 unsigned long flag;
1420};
Eric Biederman90089602004-05-28 14:11:54 +00001421
1422struct compiler_arg {
1423 const char *name;
1424 unsigned long mask;
1425 struct compiler_flag flags[16];
1426};
1427
Eric Biederman5ade04a2003-10-22 04:03:46 +00001428static int set_flag(
1429 const struct compiler_flag *ptr, unsigned long *flags,
1430 int act, const char *flag)
1431{
1432 int result = -1;
1433 for(; ptr->name; ptr++) {
1434 if (strcmp(ptr->name, flag) == 0) {
1435 break;
1436 }
1437 }
1438 if (ptr->name) {
1439 result = 0;
1440 *flags &= ~(ptr->flag);
1441 if (act) {
1442 *flags |= ptr->flag;
1443 }
1444 }
1445 return result;
1446}
1447
Eric Biederman90089602004-05-28 14:11:54 +00001448static int set_arg(
1449 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1450{
1451 const char *val;
1452 int result = -1;
1453 int len;
1454 val = strchr(arg, '=');
1455 if (val) {
1456 len = val - arg;
1457 val++;
1458 for(; ptr->name; ptr++) {
1459 if (strncmp(ptr->name, arg, len) == 0) {
1460 break;
1461 }
1462 }
1463 if (ptr->name) {
1464 *flags &= ~ptr->mask;
1465 result = set_flag(&ptr->flags[0], flags, 1, val);
1466 }
1467 }
1468 return result;
1469}
1470
1471
1472static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
1473 const char *prefix, const char *invert_prefix)
1474{
1475 for(;ptr->name; ptr++) {
1476 fprintf(fp, "%s%s\n", prefix, ptr->name);
1477 if (invert_prefix) {
1478 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1479 }
1480 }
1481}
1482
1483static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1484 const char *prefix)
1485{
1486 for(;ptr->name; ptr++) {
1487 const struct compiler_flag *flag;
1488 for(flag = &ptr->flags[0]; flag->name; flag++) {
1489 fprintf(fp, "%s%s=%s\n",
1490 prefix, ptr->name, flag->name);
1491 }
1492 }
1493}
1494
1495static int append_string(size_t *max, const char ***vec, const char *str,
1496 const char *name)
1497{
1498 size_t count;
1499 count = ++(*max);
1500 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1501 (*vec)[count -1] = 0;
1502 (*vec)[count -2] = str;
1503 return 0;
1504}
1505
1506static void arg_error(char *fmt, ...);
1507static const char *identifier(const char *str, const char *end);
1508
1509static int append_include_path(struct compiler_state *compiler, const char *str)
1510{
1511 int result;
1512 if (!exists(str, ".")) {
1513 arg_error("Nonexistent include path: `%s'\n",
1514 str);
1515 }
1516 result = append_string(&compiler->include_path_count,
1517 &compiler->include_paths, str, "include_paths");
1518 return result;
1519}
1520
1521static int append_define(struct compiler_state *compiler, const char *str)
1522{
1523 const char *end, *rest;
1524 int result;
1525
1526 end = strchr(str, '=');
1527 if (!end) {
1528 end = str + strlen(str);
1529 }
1530 rest = identifier(str, end);
1531 if (rest != end) {
1532 int len = end - str - 1;
1533 arg_error("Invalid name cannot define macro: `%*.*s'\n",
1534 len, len, str);
1535 }
1536 result = append_string(&compiler->define_count,
1537 &compiler->defines, str, "defines");
1538 return result;
1539}
1540
1541static int append_undef(struct compiler_state *compiler, const char *str)
1542{
1543 const char *end, *rest;
1544 int result;
1545
1546 end = str + strlen(str);
1547 rest = identifier(str, end);
1548 if (rest != end) {
1549 int len = end - str - 1;
1550 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
1551 len, len, str);
1552 }
1553 result = append_string(&compiler->undef_count,
1554 &compiler->undefs, str, "undefs");
1555 return result;
1556}
1557
1558static const struct compiler_flag romcc_flags[] = {
1559 { "cpp-only", COMPILER_CPP_ONLY },
1560 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1561 { "simplify", COMPILER_SIMPLIFY },
1562 { "scc-transform", COMPILER_SCC_TRANSFORM },
1563 { "simplify-op", COMPILER_SIMPLIFY_OP },
1564 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1565 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1566 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1567 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1568 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1569 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1570 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1571 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1572 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1573 { 0, 0 },
1574};
1575static const struct compiler_arg romcc_args[] = {
1576 { "inline-policy", COMPILER_INLINE_MASK,
1577 {
1578 { "always", COMPILER_INLINE_ALWAYS, },
1579 { "never", COMPILER_INLINE_NEVER, },
1580 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1581 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1582 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1583 { 0, 0 },
1584 },
1585 },
1586 { 0, 0 },
1587};
1588static const struct compiler_flag romcc_opt_flags[] = {
1589 { "-O", COMPILER_SIMPLIFY },
1590 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
1591 { "-E", COMPILER_CPP_ONLY },
1592 { 0, 0, },
1593};
1594static const struct compiler_flag romcc_debug_flags[] = {
1595 { "all", DEBUG_ALL },
1596 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1597 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1598 { "fdominators", DEBUG_FDOMINATORS },
1599 { "rdominators", DEBUG_RDOMINATORS },
1600 { "triples", DEBUG_TRIPLES },
1601 { "interference", DEBUG_INTERFERENCE },
1602 { "scc-transform", DEBUG_SCC_TRANSFORM },
1603 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1604 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1605 { "inline", DEBUG_INLINE },
1606 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1607 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1608 { "color-graph", DEBUG_COLOR_GRAPH },
1609 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1610 { "coalescing", DEBUG_COALESCING },
1611 { "coalescing2", DEBUG_COALESCING2 },
1612 { "verification", DEBUG_VERIFICATION },
1613 { "calls", DEBUG_CALLS },
1614 { "calls2", DEBUG_CALLS2 },
1615 { "tokens", DEBUG_TOKENS },
1616 { 0, 0 },
1617};
1618
Eric Biederman5ade04a2003-10-22 04:03:46 +00001619static int compiler_encode_flag(
1620 struct compiler_state *compiler, const char *flag)
1621{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001622 int act;
1623 int result;
1624
1625 act = 1;
1626 result = -1;
1627 if (strncmp(flag, "no-", 3) == 0) {
1628 flag += 3;
1629 act = 0;
1630 }
1631 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001632 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1633 }
1634 else if (strncmp(flag, "-E", 2) == 0) {
1635 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1636 }
1637 else if (strncmp(flag, "-I", 2) == 0) {
1638 result = append_include_path(compiler, flag + 2);
1639 }
1640 else if (strncmp(flag, "-D", 2) == 0) {
1641 result = append_define(compiler, flag + 2);
1642 }
1643 else if (strncmp(flag, "-U", 2) == 0) {
1644 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001645 }
1646 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1647 result = 0;
1648 compiler->label_prefix = flag + 13;
1649 }
1650 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1651 unsigned long max_passes;
1652 char *end;
1653 max_passes = strtoul(flag + 22, &end, 10);
1654 if (end[0] == '\0') {
1655 result = 0;
1656 compiler->max_allocation_passes = max_passes;
1657 }
1658 }
1659 else if (act && strcmp(flag, "debug") == 0) {
1660 result = 0;
1661 compiler->debug |= DEBUG_DEFAULT;
1662 }
1663 else if (strncmp(flag, "debug-", 6) == 0) {
1664 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001665 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001666 }
1667 else {
Eric Biederman90089602004-05-28 14:11:54 +00001668 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1669 if (result < 0) {
1670 result = set_arg(romcc_args, &compiler->flags, flag);
1671 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001672 }
1673 return result;
1674}
1675
Eric Biederman90089602004-05-28 14:11:54 +00001676static void compiler_usage(FILE *fp)
1677{
1678 flag_usage(fp, romcc_opt_flags, "", 0);
1679 flag_usage(fp, romcc_flags, "-f", "-fno-");
1680 arg_usage(fp, romcc_args, "-f");
1681 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1682 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1683 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1684 fprintf(fp, "-I<include path>\n");
1685 fprintf(fp, "-D<macro>[=defn]\n");
1686 fprintf(fp, "-U<macro>\n");
1687}
1688
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001689static void do_cleanup(struct compile_state *state)
1690{
1691 if (state->output) {
1692 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001693 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001694 state->output = 0;
1695 }
1696 if (state->dbgout) {
1697 fflush(state->dbgout);
1698 }
1699 if (state->errout) {
1700 fflush(state->errout);
1701 }
1702}
1703
1704static struct compile_state *exit_state;
1705static void exit_cleanup(void)
1706{
1707 if (exit_state) {
1708 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001709 }
1710}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001711
1712static int get_col(struct file_state *file)
1713{
1714 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001715 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001716 ptr = file->line_start;
1717 end = file->pos;
1718 for(col = 0; ptr < end; ptr++) {
1719 if (*ptr != '\t') {
1720 col++;
1721 }
1722 else {
1723 col = (col & ~7) + 8;
1724 }
1725 }
1726 return col;
1727}
1728
1729static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1730{
1731 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001732 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001733 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001734 for(spot = triple->occurance; spot; spot = spot->parent) {
1735 fprintf(fp, "%s:%d.%d: ",
1736 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001737 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001738 return;
1739 }
1740 if (!state->file) {
1741 return;
1742 }
1743 col = get_col(state->file);
1744 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001745 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001746}
1747
Eric Biederman5ade04a2003-10-22 04:03:46 +00001748static void internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001749 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001750{
Eric Biederman90089602004-05-28 14:11:54 +00001751 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001752 va_list args;
1753 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001754 loc(fp, state, ptr);
1755 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001756 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001757 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001758 }
Eric Biederman90089602004-05-28 14:11:54 +00001759 fprintf(fp, "Internal compiler error: ");
1760 vfprintf(fp, fmt, args);
1761 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001762 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001763 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001764 abort();
1765}
1766
1767
Eric Biederman5ade04a2003-10-22 04:03:46 +00001768static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001769 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001770{
Eric Biederman90089602004-05-28 14:11:54 +00001771 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001772 va_list args;
1773 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001774 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001775 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001776 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001777 }
Eric Biederman90089602004-05-28 14:11:54 +00001778 fprintf(fp, "Internal compiler warning: ");
1779 vfprintf(fp, fmt, args);
1780 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001781 va_end(args);
1782}
1783
1784
1785
Eric Biederman5ade04a2003-10-22 04:03:46 +00001786static void error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001787 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001788{
Eric Biederman90089602004-05-28 14:11:54 +00001789 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001790 va_list args;
1791 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001792 loc(fp, state, ptr);
1793 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001794 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001795 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001796 }
Eric Biederman90089602004-05-28 14:11:54 +00001797 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001798 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001799 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001800 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001801 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001802 abort();
1803 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001804 exit(1);
1805}
1806
Eric Biederman5ade04a2003-10-22 04:03:46 +00001807static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001808 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001809{
Eric Biederman90089602004-05-28 14:11:54 +00001810 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001811 va_list args;
1812 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001813 loc(fp, state, ptr);
1814 fprintf(fp, "warning: ");
1815 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1816 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1817 }
1818 vfprintf(fp, fmt, args);
1819 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001820 va_end(args);
1821}
1822
Eric Biedermanb138ac82003-04-22 18:44:01 +00001823#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1824
Eric Biederman0babc1c2003-05-09 02:39:00 +00001825static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001826{
1827 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001828 if (op >= OP_MAX) {
1829 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001830 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001831 if (op < 0) {
1832 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001833 }
1834}
1835
Eric Biederman0babc1c2003-05-09 02:39:00 +00001836static void valid_ins(struct compile_state *state, struct triple *ptr)
1837{
1838 valid_op(state, ptr->op);
1839}
1840
Eric Biederman90089602004-05-28 14:11:54 +00001841static void valid_param_count(struct compile_state *state, struct triple *ins)
1842{
1843 int lhs, rhs, misc, targ;
1844 valid_ins(state, ins);
1845 lhs = table_ops[ins->op].lhs;
1846 rhs = table_ops[ins->op].rhs;
1847 misc = table_ops[ins->op].misc;
1848 targ = table_ops[ins->op].targ;
1849
1850 if ((lhs >= 0) && (ins->lhs != lhs)) {
1851 internal_error(state, ins, "Bad lhs count");
1852 }
1853 if ((rhs >= 0) && (ins->rhs != rhs)) {
1854 internal_error(state, ins, "Bad rhs count");
1855 }
1856 if ((misc >= 0) && (ins->misc != misc)) {
1857 internal_error(state, ins, "Bad misc count");
1858 }
1859 if ((targ >= 0) && (ins->targ != targ)) {
1860 internal_error(state, ins, "Bad targ count");
1861 }
1862}
1863
Eric Biedermanb138ac82003-04-22 18:44:01 +00001864static void process_trigraphs(struct compile_state *state)
1865{
1866 char *src, *dest, *end;
1867 struct file_state *file;
1868 file = state->file;
1869 src = dest = file->buf;
1870 end = file->buf + file->size;
1871 while((end - src) >= 3) {
1872 if ((src[0] == '?') && (src[1] == '?')) {
1873 int c = -1;
1874 switch(src[2]) {
1875 case '=': c = '#'; break;
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 }
1885 if (c != -1) {
1886 *dest++ = c;
1887 src += 3;
1888 }
1889 else {
1890 *dest++ = *src++;
1891 }
1892 }
1893 else {
1894 *dest++ = *src++;
1895 }
1896 }
1897 while(src != end) {
1898 *dest++ = *src++;
1899 }
1900 file->size = dest - file->buf;
1901}
1902
1903static void splice_lines(struct compile_state *state)
1904{
1905 char *src, *dest, *end;
1906 struct file_state *file;
1907 file = state->file;
1908 src = dest = file->buf;
1909 end = file->buf + file->size;
1910 while((end - src) >= 2) {
1911 if ((src[0] == '\\') && (src[1] == '\n')) {
1912 src += 2;
1913 }
1914 else {
1915 *dest++ = *src++;
1916 }
1917 }
1918 while(src != end) {
1919 *dest++ = *src++;
1920 }
1921 file->size = dest - file->buf;
1922}
1923
1924static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001925static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001926static void use_triple(struct triple *used, struct triple *user)
1927{
1928 struct triple_set **ptr, *new;
1929 if (!used)
1930 return;
1931 if (!user)
1932 return;
1933 ptr = &used->use;
1934 while(*ptr) {
1935 if ((*ptr)->member == user) {
1936 return;
1937 }
1938 ptr = &(*ptr)->next;
1939 }
1940 /* Append new to the head of the list,
1941 * copy_func and rename_block_variables
1942 * depends on this.
1943 */
1944 new = xcmalloc(sizeof(*new), "triple_set");
1945 new->member = user;
1946 new->next = used->use;
1947 used->use = new;
1948}
1949
1950static void unuse_triple(struct triple *used, struct triple *unuser)
1951{
1952 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001953 if (!used) {
1954 return;
1955 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001956 ptr = &used->use;
1957 while(*ptr) {
1958 use = *ptr;
1959 if (use->member == unuser) {
1960 *ptr = use->next;
1961 xfree(use);
1962 }
1963 else {
1964 ptr = &use->next;
1965 }
1966 }
1967}
1968
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001969static void put_occurance(struct occurance *occurance)
1970{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001971 if (occurance) {
1972 occurance->count -= 1;
1973 if (occurance->count <= 0) {
1974 if (occurance->parent) {
1975 put_occurance(occurance->parent);
1976 }
1977 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001978 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001979 }
1980}
1981
1982static void get_occurance(struct occurance *occurance)
1983{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001984 if (occurance) {
1985 occurance->count += 1;
1986 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001987}
1988
1989
1990static struct occurance *new_occurance(struct compile_state *state)
1991{
1992 struct occurance *result, *last;
1993 const char *filename;
1994 const char *function;
1995 int line, col;
1996
1997 function = "";
1998 filename = 0;
1999 line = 0;
2000 col = 0;
2001 if (state->file) {
2002 filename = state->file->report_name;
2003 line = state->file->report_line;
2004 col = get_col(state->file);
2005 }
2006 if (state->function) {
2007 function = state->function;
2008 }
2009 last = state->last_occurance;
2010 if (last &&
2011 (last->col == col) &&
2012 (last->line == line) &&
2013 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00002014 ((last->filename == filename) ||
2015 (strcmp(last->filename, filename) == 0)))
2016 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002017 get_occurance(last);
2018 return last;
2019 }
2020 if (last) {
2021 state->last_occurance = 0;
2022 put_occurance(last);
2023 }
2024 result = xmalloc(sizeof(*result), "occurance");
2025 result->count = 2;
2026 result->filename = filename;
2027 result->function = function;
2028 result->line = line;
2029 result->col = col;
2030 result->parent = 0;
2031 state->last_occurance = result;
2032 return result;
2033}
2034
2035static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002036 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002037{
2038 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002039 if (top->parent) {
2040 internal_error(state, 0, "inlining an already inlined function?");
2041 }
2042 /* If I have a null base treat it that way */
2043 if ((base->parent == 0) &&
2044 (base->col == 0) &&
2045 (base->line == 0) &&
2046 (base->function[0] == '\0') &&
2047 (base->filename[0] == '\0')) {
2048 base = 0;
2049 }
2050 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002051 last = state->last_occurance;
2052 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002053 (last->parent == base) &&
2054 (last->col == top->col) &&
2055 (last->line == top->line) &&
2056 (last->function == top->function) &&
2057 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002058 get_occurance(last);
2059 return last;
2060 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002061 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002062 if (last) {
2063 state->last_occurance = 0;
2064 put_occurance(last);
2065 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002066 /* Generate a new occurance structure */
2067 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002068 result = xmalloc(sizeof(*result), "occurance");
2069 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002070 result->filename = top->filename;
2071 result->function = top->function;
2072 result->line = top->line;
2073 result->col = top->col;
2074 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002075 state->last_occurance = result;
2076 return result;
2077}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002078
2079static struct occurance dummy_occurance = {
2080 .count = 2,
2081 .filename = __FILE__,
2082 .function = "",
2083 .line = __LINE__,
2084 .col = 0,
2085 .parent = 0,
2086};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002087
Eric Biederman90089602004-05-28 14:11:54 +00002088/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002089 * from a triple. Having allows certain sanity checks to pass even
2090 * when the original triple that was pointed to is gone.
2091 */
Eric Biederman90089602004-05-28 14:11:54 +00002092static struct triple unknown_triple = {
2093 .next = &unknown_triple,
2094 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002095 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002096 .op = OP_UNKNOWNVAL,
2097 .lhs = 0,
2098 .rhs = 0,
2099 .misc = 0,
2100 .targ = 0,
2101 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002102 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002103 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002104 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002105 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002106};
2107
Eric Biederman0babc1c2003-05-09 02:39:00 +00002108
Eric Biederman90089602004-05-28 14:11:54 +00002109static size_t registers_of(struct compile_state *state, struct type *type);
2110
2111static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002112 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2113 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002114{
Eric Biederman90089602004-05-28 14:11:54 +00002115 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002116 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002117 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002118 dummy.op = op;
2119 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002120 valid_op(state, op);
2121 lhs = table_ops[op].lhs;
2122 rhs = table_ops[op].rhs;
2123 misc = table_ops[op].misc;
2124 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002125
2126 switch(op) {
2127 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002128 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002129 break;
2130 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002131 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002132 break;
2133 case OP_ADECL:
2134 lhs = registers_of(state, type);
2135 break;
2136 case OP_TUPLE:
2137 lhs = registers_of(state, type);
2138 break;
2139 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002140 rhs = rhs_wanted;
2141 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002142 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002143 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002144 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002145 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002146 }
2147 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002148 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002149 }
2150 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002151 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002152 }
2153 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002154 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002155 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002156
2157 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002158 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002159 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2160
2161 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2162 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002163 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002164 ret->lhs = lhs;
2165 ret->rhs = rhs;
2166 ret->misc = misc;
2167 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002168 ret->type = type;
2169 ret->next = ret;
2170 ret->prev = ret;
2171 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002172 /* A simple sanity check */
2173 if ((ret->op != op) ||
2174 (ret->lhs != lhs) ||
2175 (ret->rhs != rhs) ||
2176 (ret->misc != misc) ||
2177 (ret->targ != targ) ||
2178 (ret->type != type) ||
2179 (ret->next != ret) ||
2180 (ret->prev != ret) ||
2181 (ret->occurance != occurance)) {
2182 internal_error(state, ret, "huh?");
2183 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002184 return ret;
2185}
2186
Eric Biederman0babc1c2003-05-09 02:39:00 +00002187struct triple *dup_triple(struct compile_state *state, struct triple *src)
2188{
2189 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002190 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002191 src_lhs = src->lhs;
2192 src_rhs = src->rhs;
2193 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002194 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002195 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002196 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002197 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002198 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002199 return dup;
2200}
2201
2202static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002203 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002204{
2205 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002206 struct occurance *occurance;
2207 occurance = new_occurance(state);
2208 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002209 return ret;
2210}
2211
2212static struct triple *build_triple(struct compile_state *state,
2213 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002214 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002215{
2216 struct triple *ret;
2217 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002218 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002219 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002220 if (count > 0) {
2221 ret->param[0] = left;
2222 }
2223 if (count > 1) {
2224 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002225 }
2226 return ret;
2227}
2228
Eric Biederman0babc1c2003-05-09 02:39:00 +00002229static struct triple *triple(struct compile_state *state,
2230 int op, struct type *type, struct triple *left, struct triple *right)
2231{
2232 struct triple *ret;
2233 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002234 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002235 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002236 if (count >= 1) {
2237 ret->param[0] = left;
2238 }
2239 if (count >= 2) {
2240 ret->param[1] = right;
2241 }
2242 return ret;
2243}
2244
2245static struct triple *branch(struct compile_state *state,
2246 struct triple *targ, struct triple *test)
2247{
2248 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002249 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002250 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002251 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002252 } else {
2253 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002254 }
2255 TARG(ret, 0) = targ;
2256 /* record the branch target was used */
2257 if (!targ || (targ->op != OP_LABEL)) {
2258 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002259 }
2260 return ret;
2261}
2262
Eric Biederman90089602004-05-28 14:11:54 +00002263static int triple_is_label(struct compile_state *state, struct triple *ins);
2264static int triple_is_call(struct compile_state *state, struct triple *ins);
2265static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002266static void insert_triple(struct compile_state *state,
2267 struct triple *first, struct triple *ptr)
2268{
2269 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002270 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002271 internal_error(state, ptr, "expression already used");
2272 }
2273 ptr->next = first;
2274 ptr->prev = first->prev;
2275 ptr->prev->next = ptr;
2276 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002277
2278 if (triple_is_cbranch(state, ptr->prev) ||
2279 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002280 unuse_triple(first, ptr->prev);
2281 use_triple(ptr, ptr->prev);
2282 }
2283 }
2284}
2285
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002286static int triple_stores_block(struct compile_state *state, struct triple *ins)
2287{
2288 /* This function is used to determine if u.block
2289 * is utilized to store the current block number.
2290 */
2291 int stores_block;
2292 valid_ins(state, ins);
2293 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2294 return stores_block;
2295}
2296
Eric Biederman90089602004-05-28 14:11:54 +00002297static int triple_is_branch(struct compile_state *state, struct triple *ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002298static struct block *block_of_triple(struct compile_state *state,
2299 struct triple *ins)
2300{
2301 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002302 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002303 return 0;
2304 }
2305 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002306 while(ins != first && !triple_is_branch(state, ins->prev) &&
2307 !triple_stores_block(state, ins))
2308 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002309 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002310 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002311 }
2312 ins = ins->prev;
2313 }
Eric Biederman90089602004-05-28 14:11:54 +00002314 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002315}
2316
Eric Biederman90089602004-05-28 14:11:54 +00002317static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002318static struct triple *pre_triple(struct compile_state *state,
2319 struct triple *base,
2320 int op, struct type *type, struct triple *left, struct triple *right)
2321{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002322 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002323 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002324 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002325 /* If I am an OP_PIECE jump to the real instruction */
2326 if (base->op == OP_PIECE) {
2327 base = MISC(base, 0);
2328 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002329 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002330 get_occurance(base->occurance);
2331 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002332 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002333 if (triple_stores_block(state, ret)) {
2334 ret->u.block = block;
2335 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002336 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002337 for(i = 0; i < ret->lhs; i++) {
2338 struct triple *piece;
2339 piece = LHS(ret, i);
2340 insert_triple(state, base, piece);
2341 use_triple(ret, piece);
2342 use_triple(piece, ret);
2343 }
2344 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002345 block->first = ret;
2346 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002347 return ret;
2348}
2349
2350static struct triple *post_triple(struct compile_state *state,
2351 struct triple *base,
2352 int op, struct type *type, struct triple *left, struct triple *right)
2353{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002354 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002355 struct triple *ret, *next;
2356 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002357 /* If I am an OP_PIECE jump to the real instruction */
2358 if (base->op == OP_PIECE) {
2359 base = MISC(base, 0);
2360 }
2361 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002362 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002363 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002364 base = LHS(base, zlhs - 1);
2365 }
2366
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002367 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002368 get_occurance(base->occurance);
2369 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002370 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002371 if (triple_stores_block(state, ret)) {
2372 ret->u.block = block;
2373 }
Eric Biederman90089602004-05-28 14:11:54 +00002374 next = base->next;
2375 insert_triple(state, next, ret);
2376 zlhs = ret->lhs;
2377 for(i = 0; i < zlhs; i++) {
2378 struct triple *piece;
2379 piece = LHS(ret, i);
2380 insert_triple(state, next, piece);
2381 use_triple(ret, piece);
2382 use_triple(piece, ret);
2383 }
2384 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002385 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002386 if (zlhs) {
2387 block->last = LHS(ret, zlhs - 1);
2388 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002389 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002390 return ret;
2391}
2392
Eric Biederman90089602004-05-28 14:11:54 +00002393static struct type *reg_type(
2394 struct compile_state *state, struct type *type, int reg);
2395
2396static void generate_lhs_piece(
2397 struct compile_state *state, struct triple *ins, int index)
2398{
2399 struct type *piece_type;
2400 struct triple *piece;
2401 get_occurance(ins->occurance);
2402 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2403
2404 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2405 piece_type = piece_type->left;
2406 }
2407#if 0
2408{
2409 static void name_of(FILE *fp, struct type *type);
2410 FILE * fp = state->errout;
2411 fprintf(fp, "piece_type(%d): ", index);
2412 name_of(fp, piece_type);
2413 fprintf(fp, "\n");
2414}
2415#endif
2416 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2417 piece->u.cval = index;
2418 LHS(ins, piece->u.cval) = piece;
2419 MISC(piece, 0) = ins;
2420}
2421
2422static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2423{
2424 int i, zlhs;
2425 zlhs = ins->lhs;
2426 for(i = 0; i < zlhs; i++) {
2427 generate_lhs_piece(state, ins, i);
2428 }
2429}
2430
Eric Biedermanb138ac82003-04-22 18:44:01 +00002431static struct triple *label(struct compile_state *state)
2432{
2433 /* Labels don't get a type */
2434 struct triple *result;
2435 result = triple(state, OP_LABEL, &void_type, 0, 0);
2436 return result;
2437}
2438
Eric Biederman90089602004-05-28 14:11:54 +00002439static struct triple *mkprog(struct compile_state *state, ...)
2440{
2441 struct triple *prog, *head, *arg;
2442 va_list args;
2443 int i;
2444
2445 head = label(state);
2446 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2447 RHS(prog, 0) = head;
2448 va_start(args, state);
2449 i = 0;
2450 while((arg = va_arg(args, struct triple *)) != 0) {
2451 if (++i >= 100) {
2452 internal_error(state, 0, "too many arguments to mkprog");
2453 }
2454 flatten(state, head, arg);
2455 }
2456 va_end(args);
2457 prog->type = head->prev->type;
2458 return prog;
2459}
2460static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002461static void display_triple(FILE *fp, struct triple *ins)
2462{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002463 struct occurance *ptr;
2464 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002465 char pre, post, vol;
2466 pre = post = vol = ' ';
2467 if (ins) {
2468 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2469 pre = '^';
2470 }
2471 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2472 post = ',';
2473 }
2474 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2475 vol = 'v';
2476 }
2477 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002478 }
Eric Biederman90089602004-05-28 14:11:54 +00002479 if (ins == 0) {
2480 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002481 }
Eric Biederman90089602004-05-28 14:11:54 +00002482 else if (ins->op == OP_INTCONST) {
2483 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
2484 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002485 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002486 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002487 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002488 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2489 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2490 MISC(ins, 0), (unsigned long)(ins->u.cval));
2491 }
2492 else if (ins->op == OP_INDEX) {
2493 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2494 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2495 RHS(ins, 0), (unsigned long)(ins->u.cval));
2496 }
2497 else if (ins->op == OP_PIECE) {
2498 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2499 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002500 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002501 }
2502 else {
2503 int i, count;
Eric Biederman90089602004-05-28 14:11:54 +00002504 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
2505 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2506 if (table_ops[ins->op].flags & BITFIELD) {
2507 fprintf(fp, " <%2d-%2d:%2d>",
2508 ins->u.bitfield.offset,
2509 ins->u.bitfield.offset + ins->u.bitfield.size,
2510 ins->u.bitfield.size);
2511 }
2512 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002513 for(i = 0; i < count; i++) {
2514 fprintf(fp, " %-10p", ins->param[i]);
2515 }
2516 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002517 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002518 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002519 }
Eric Biederman90089602004-05-28 14:11:54 +00002520 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002521 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002522#if DEBUG_DISPLAY_TYPES
2523 fprintf(fp, " <");
2524 name_of(fp, ins->type);
2525 fprintf(fp, "> ");
2526#endif
2527#if DEBUG_DISPLAY_USES
2528 fprintf(fp, " [");
2529 for(user = ins->use; user; user = user->next) {
2530 fprintf(fp, " %-10p", user->member);
2531 }
2532 fprintf(fp, " ]");
2533#endif
2534 fprintf(fp, " @");
2535 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2536 fprintf(fp, " %s,%s:%d.%d",
2537 ptr->function,
2538 ptr->filename,
2539 ptr->line,
2540 ptr->col);
2541 }
2542 if (ins->op == OP_ASM) {
2543 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002544 }
2545 }
Eric Biederman90089602004-05-28 14:11:54 +00002546 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002547 fflush(fp);
2548}
2549
Eric Biederman90089602004-05-28 14:11:54 +00002550static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002551static void display_triple_changes(
2552 FILE *fp, const struct triple *new, const struct triple *orig)
2553{
2554
2555 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002556 new_count = TRIPLE_SIZE(new);
2557 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002558 if ((new->op != orig->op) ||
2559 (new_count != orig_count) ||
2560 (memcmp(orig->param, new->param,
2561 orig_count * sizeof(orig->param[0])) != 0) ||
2562 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
2563 {
2564 struct occurance *ptr;
2565 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002566 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002567 if (orig->op == new->op) {
2568 fprintf(fp, " %-11s", tops(orig->op));
2569 } else {
2570 fprintf(fp, " [%-10s %-10s]",
2571 tops(new->op), tops(orig->op));
2572 }
2573 min_count = new_count;
2574 if (min_count > orig_count) {
2575 min_count = orig_count;
2576 }
2577 for(indent = i = 0; i < min_count; i++) {
2578 if (orig->param[i] == new->param[i]) {
2579 fprintf(fp, " %-11p",
2580 orig->param[i]);
2581 indent += 12;
2582 } else {
2583 fprintf(fp, " [%-10p %-10p]",
2584 new->param[i],
2585 orig->param[i]);
2586 indent += 24;
2587 }
2588 }
2589 for(; i < orig_count; i++) {
2590 fprintf(fp, " [%-9p]", orig->param[i]);
2591 indent += 12;
2592 }
2593 for(; i < new_count; i++) {
2594 fprintf(fp, " [%-9p]", new->param[i]);
2595 indent += 12;
2596 }
2597 if ((new->op == OP_INTCONST)||
2598 (new->op == OP_ADDRCONST)) {
2599 fprintf(fp, " <0x%08lx>",
2600 (unsigned long)(new->u.cval));
2601 indent += 13;
2602 }
2603 for(;indent < 36; indent++) {
2604 putc(' ', fp);
2605 }
Eric Biederman90089602004-05-28 14:11:54 +00002606
2607#if DEBUG_DISPLAY_TYPES
2608 fprintf(fp, " <");
2609 name_of(fp, new->type);
2610 if (!equiv_types(new->type, orig->type)) {
2611 fprintf(fp, " -- ");
2612 name_of(fp, orig->type);
2613 }
2614 fprintf(fp, "> ");
2615#endif
2616
Eric Biederman5ade04a2003-10-22 04:03:46 +00002617 fprintf(fp, " @");
2618 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2619 fprintf(fp, " %s,%s:%d.%d",
2620 ptr->function,
2621 ptr->filename,
2622 ptr->line,
2623 ptr->col);
2624
2625 }
2626 fprintf(fp, "\n");
2627 fflush(fp);
2628 }
2629}
2630
Eric Biederman83b991a2003-10-11 06:20:25 +00002631static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002632{
2633 /* Does the triple have no side effects.
2634 * I.e. Rexecuting the triple with the same arguments
2635 * gives the same value.
2636 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002637 unsigned pure;
2638 valid_ins(state, ins);
2639 pure = PURE_BITS(table_ops[ins->op].flags);
2640 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002641 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002642 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002643 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002644 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002645}
2646
Eric Biederman90089602004-05-28 14:11:54 +00002647static int triple_is_branch_type(struct compile_state *state,
2648 struct triple *ins, unsigned type)
2649{
2650 /* Is this one of the passed branch types? */
2651 valid_ins(state, ins);
2652 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2653}
2654
Eric Biederman0babc1c2003-05-09 02:39:00 +00002655static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002656{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002657 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002658 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002659 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002660}
2661
Eric Biederman90089602004-05-28 14:11:54 +00002662static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002663{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002664 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002665 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002666}
2667
Eric Biederman90089602004-05-28 14:11:54 +00002668static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002669{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002670 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002671 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002672 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002673 type = BRANCH_BITS(table_ops[ins->op].flags);
2674 return (type != 0) && (type != CBRANCH);
2675}
2676
2677static int triple_is_call(struct compile_state *state, struct triple *ins)
2678{
2679 /* Is this triple a call instruction? */
2680 return triple_is_branch_type(state, ins, CALLBRANCH);
2681}
2682
2683static int triple_is_ret(struct compile_state *state, struct triple *ins)
2684{
2685 /* Is this triple a return instruction? */
2686 return triple_is_branch_type(state, ins, RETBRANCH);
2687}
2688
2689static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2690{
2691 /* Is this triple an unconditional branch and not a call or a
2692 * return? */
2693 return triple_is_branch_type(state, ins, UBRANCH);
2694}
2695
2696static int triple_is_end(struct compile_state *state, struct triple *ins)
2697{
2698 return triple_is_branch_type(state, ins, ENDBRANCH);
2699}
2700
2701static int triple_is_label(struct compile_state *state, struct triple *ins)
2702{
2703 valid_ins(state, ins);
2704 return (ins->op == OP_LABEL);
2705}
2706
2707static struct triple *triple_to_block_start(
2708 struct compile_state *state, struct triple *start)
2709{
2710 while(!triple_is_branch(state, start->prev) &&
2711 (!triple_is_label(state, start) || !start->use)) {
2712 start = start->prev;
2713 }
2714 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002715}
2716
Eric Biederman0babc1c2003-05-09 02:39:00 +00002717static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002718{
2719 /* This function is used to determine which triples need
2720 * a register.
2721 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002722 int is_def;
2723 valid_ins(state, ins);
2724 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002725 if (ins->lhs >= 1) {
2726 is_def = 0;
2727 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002728 return is_def;
2729}
2730
Eric Biederman83b991a2003-10-11 06:20:25 +00002731static int triple_is_structural(struct compile_state *state, struct triple *ins)
2732{
2733 int is_structural;
2734 valid_ins(state, ins);
2735 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2736 return is_structural;
2737}
2738
Eric Biederman90089602004-05-28 14:11:54 +00002739static int triple_is_part(struct compile_state *state, struct triple *ins)
2740{
2741 int is_part;
2742 valid_ins(state, ins);
2743 is_part = (table_ops[ins->op].flags & PART) == PART;
2744 return is_part;
2745}
2746
2747static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2748{
2749 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2750}
2751
Eric Biederman0babc1c2003-05-09 02:39:00 +00002752static struct triple **triple_iter(struct compile_state *state,
2753 size_t count, struct triple **vector,
2754 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002755{
2756 struct triple **ret;
2757 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002758 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002759 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002760 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002761 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002762 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002763 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002764 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002765 }
2766 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002767
Eric Biedermanb138ac82003-04-22 18:44:01 +00002768}
2769
2770static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002771 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002772{
Eric Biederman90089602004-05-28 14:11:54 +00002773 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002774 ins, last);
2775}
2776
2777static struct triple **triple_rhs(struct compile_state *state,
2778 struct triple *ins, struct triple **last)
2779{
Eric Biederman90089602004-05-28 14:11:54 +00002780 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002781 ins, last);
2782}
2783
2784static struct triple **triple_misc(struct compile_state *state,
2785 struct triple *ins, struct triple **last)
2786{
Eric Biederman90089602004-05-28 14:11:54 +00002787 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002788 ins, last);
2789}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002790
Eric Biederman90089602004-05-28 14:11:54 +00002791static struct triple **do_triple_targ(struct compile_state *state,
2792 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002793{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002794 size_t count;
2795 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002796 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002797 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002798 count = ins->targ;
2799 next_is_targ = 0;
2800 if (triple_is_cbranch(state, ins)) {
2801 next_is_targ = 1;
2802 }
2803 if (!call_edges && triple_is_call(state, ins)) {
2804 count = 0;
2805 }
2806 if (next_edges && triple_is_call(state, ins)) {
2807 next_is_targ = 1;
2808 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002809 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002810 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002811 if (!last) {
2812 ret = &ins->next;
2813 } else if (last == &ins->next) {
2814 last = 0;
2815 }
2816 }
2817 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002818 if (!last) {
2819 ret = vector;
2820 }
2821 else if ((last >= vector) && (last < (vector + count - 1))) {
2822 ret = last + 1;
2823 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002824 else if (last == vector + count - 1) {
2825 last = 0;
2826 }
2827 }
Eric Biederman90089602004-05-28 14:11:54 +00002828 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002829 struct triple_set *use;
2830 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002831 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002832 continue;
2833 }
2834 if (!last) {
2835 ret = &use->member->next;
2836 break;
2837 }
2838 else if (last == &use->member->next) {
2839 last = 0;
2840 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002841 }
2842 }
2843 return ret;
2844}
2845
Eric Biederman90089602004-05-28 14:11:54 +00002846static struct triple **triple_targ(struct compile_state *state,
2847 struct triple *ins, struct triple **last)
2848{
2849 return do_triple_targ(state, ins, last, 1, 1);
2850}
2851
2852static struct triple **triple_edge_targ(struct compile_state *state,
2853 struct triple *ins, struct triple **last)
2854{
2855 return do_triple_targ(state, ins, last,
2856 state->functions_joined, !state->functions_joined);
2857}
2858
2859static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2860{
2861 struct triple *next;
2862 int lhs, i;
2863 lhs = ins->lhs;
2864 next = ins->next;
2865 for(i = 0; i < lhs; i++) {
2866 struct triple *piece;
2867 piece = LHS(ins, i);
2868 if (next != piece) {
2869 internal_error(state, ins, "malformed lhs on %s",
2870 tops(ins->op));
2871 }
2872 if (next->op != OP_PIECE) {
2873 internal_error(state, ins, "bad lhs op %s at %d on %s",
2874 tops(next->op), i, tops(ins->op));
2875 }
2876 if (next->u.cval != i) {
2877 internal_error(state, ins, "bad u.cval of %d %d expected",
2878 next->u.cval, i);
2879 }
2880 next = next->next;
2881 }
2882 return next;
2883}
2884
2885/* Function piece accessor functions */
2886static struct triple *do_farg(struct compile_state *state,
2887 struct triple *func, unsigned index)
2888{
2889 struct type *ftype;
2890 struct triple *first, *arg;
2891 unsigned i;
2892
2893 ftype = func->type;
2894 if((index < 0) || (index >= (ftype->elements + 2))) {
2895 internal_error(state, func, "bad argument index: %d", index);
2896 }
2897 first = RHS(func, 0);
2898 arg = first->next;
2899 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2900 /* do nothing */
2901 }
2902 if (arg->op != OP_ADECL) {
2903 internal_error(state, 0, "arg not adecl?");
2904 }
2905 return arg;
2906}
2907static struct triple *fresult(struct compile_state *state, struct triple *func)
2908{
2909 return do_farg(state, func, 0);
2910}
2911static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2912{
2913 return do_farg(state, func, 1);
2914}
2915static struct triple *farg(struct compile_state *state,
2916 struct triple *func, unsigned index)
2917{
2918 return do_farg(state, func, index + 2);
2919}
2920
2921
2922static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2923{
2924 struct triple *first, *ins;
2925 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2926 first = ins = RHS(func, 0);
2927 do {
2928 if (triple_is_label(state, ins) && ins->use) {
2929 fprintf(fp, "%p:\n", ins);
2930 }
2931 display_triple(fp, ins);
2932
2933 if (triple_is_branch(state, ins)) {
2934 fprintf(fp, "\n");
2935 }
2936 if (ins->next->prev != ins) {
2937 internal_error(state, ins->next, "bad prev");
2938 }
2939 ins = ins->next;
2940 } while(ins != first);
2941}
2942
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002943static void verify_use(struct compile_state *state,
2944 struct triple *user, struct triple *used)
2945{
2946 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002947 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002948 for(i = 0; i < size; i++) {
2949 if (user->param[i] == used) {
2950 break;
2951 }
2952 }
2953 if (triple_is_branch(state, user)) {
2954 if (user->next == used) {
2955 i = -1;
2956 }
2957 }
2958 if (i == size) {
2959 internal_error(state, user, "%s(%p) does not use %s(%p)",
2960 tops(user->op), user, tops(used->op), used);
2961 }
2962}
2963
2964static int find_rhs_use(struct compile_state *state,
2965 struct triple *user, struct triple *used)
2966{
2967 struct triple **param;
2968 int size, i;
2969 verify_use(state, user, used);
Eric Biederman90089602004-05-28 14:11:54 +00002970#warning "AUDIT ME ->rhs"
2971 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002972 param = &RHS(user, 0);
2973 for(i = 0; i < size; i++) {
2974 if (param[i] == used) {
2975 return i;
2976 }
2977 }
2978 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002979}
2980
2981static void free_triple(struct compile_state *state, struct triple *ptr)
2982{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002983 size_t size;
2984 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002985 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002986 ptr->prev->next = ptr->next;
2987 ptr->next->prev = ptr->prev;
2988 if (ptr->use) {
2989 internal_error(state, ptr, "ptr->use != 0");
2990 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002991 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002992 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002993 xfree(ptr);
2994}
2995
2996static void release_triple(struct compile_state *state, struct triple *ptr)
2997{
2998 struct triple_set *set, *next;
2999 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00003000 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00003001 if (ptr == &unknown_triple) {
3002 return;
3003 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00003004 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00003005 /* Make certain the we are not the first or last element of a block */
3006 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00003007 if (block) {
3008 if ((block->last == ptr) && (block->first == ptr)) {
3009 block->last = block->first = 0;
3010 }
3011 else if (block->last == ptr) {
3012 block->last = ptr->prev;
3013 }
3014 else if (block->first == ptr) {
3015 block->first = ptr->next;
3016 }
Eric Biederman66fe2222003-07-04 15:14:04 +00003017 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003018 /* Remove ptr from use chains where it is the user */
3019 expr = triple_rhs(state, ptr, 0);
3020 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3021 if (*expr) {
3022 unuse_triple(*expr, ptr);
3023 }
3024 }
3025 expr = triple_lhs(state, ptr, 0);
3026 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3027 if (*expr) {
3028 unuse_triple(*expr, ptr);
3029 }
3030 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003031 expr = triple_misc(state, ptr, 0);
3032 for(; expr; expr = triple_misc(state, ptr, expr)) {
3033 if (*expr) {
3034 unuse_triple(*expr, ptr);
3035 }
3036 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003037 expr = triple_targ(state, ptr, 0);
3038 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003039 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003040 unuse_triple(*expr, ptr);
3041 }
3042 }
3043 /* Reomve ptr from use chains where it is used */
3044 for(set = ptr->use; set; set = next) {
3045 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003046 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003047 expr = triple_rhs(state, set->member, 0);
3048 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3049 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003050 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003051 }
3052 }
3053 expr = triple_lhs(state, set->member, 0);
3054 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3055 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003056 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003057 }
3058 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003059 expr = triple_misc(state, set->member, 0);
3060 for(; expr; expr = triple_misc(state, set->member, expr)) {
3061 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003062 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003063 }
3064 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003065 expr = triple_targ(state, set->member, 0);
3066 for(; expr; expr = triple_targ(state, set->member, expr)) {
3067 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003068 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003069 }
3070 }
3071 unuse_triple(ptr, set->member);
3072 }
3073 free_triple(state, ptr);
3074}
3075
Eric Biederman5ade04a2003-10-22 04:03:46 +00003076static void print_triples(struct compile_state *state);
3077static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003078
3079#define TOK_UNKNOWN 0
3080#define TOK_SPACE 1
3081#define TOK_SEMI 2
3082#define TOK_LBRACE 3
3083#define TOK_RBRACE 4
3084#define TOK_COMMA 5
3085#define TOK_EQ 6
3086#define TOK_COLON 7
3087#define TOK_LBRACKET 8
3088#define TOK_RBRACKET 9
3089#define TOK_LPAREN 10
3090#define TOK_RPAREN 11
3091#define TOK_STAR 12
3092#define TOK_DOTS 13
3093#define TOK_MORE 14
3094#define TOK_LESS 15
3095#define TOK_TIMESEQ 16
3096#define TOK_DIVEQ 17
3097#define TOK_MODEQ 18
3098#define TOK_PLUSEQ 19
3099#define TOK_MINUSEQ 20
3100#define TOK_SLEQ 21
3101#define TOK_SREQ 22
3102#define TOK_ANDEQ 23
3103#define TOK_XOREQ 24
3104#define TOK_OREQ 25
3105#define TOK_EQEQ 26
3106#define TOK_NOTEQ 27
3107#define TOK_QUEST 28
3108#define TOK_LOGOR 29
3109#define TOK_LOGAND 30
3110#define TOK_OR 31
3111#define TOK_AND 32
3112#define TOK_XOR 33
3113#define TOK_LESSEQ 34
3114#define TOK_MOREEQ 35
3115#define TOK_SL 36
3116#define TOK_SR 37
3117#define TOK_PLUS 38
3118#define TOK_MINUS 39
3119#define TOK_DIV 40
3120#define TOK_MOD 41
3121#define TOK_PLUSPLUS 42
3122#define TOK_MINUSMINUS 43
3123#define TOK_BANG 44
3124#define TOK_ARROW 45
3125#define TOK_DOT 46
3126#define TOK_TILDE 47
3127#define TOK_LIT_STRING 48
3128#define TOK_LIT_CHAR 49
3129#define TOK_LIT_INT 50
3130#define TOK_LIT_FLOAT 51
3131#define TOK_MACRO 52
3132#define TOK_CONCATENATE 53
3133
3134#define TOK_IDENT 54
3135#define TOK_STRUCT_NAME 55
3136#define TOK_ENUM_CONST 56
3137#define TOK_TYPE_NAME 57
3138
3139#define TOK_AUTO 58
3140#define TOK_BREAK 59
3141#define TOK_CASE 60
3142#define TOK_CHAR 61
3143#define TOK_CONST 62
3144#define TOK_CONTINUE 63
3145#define TOK_DEFAULT 64
3146#define TOK_DO 65
3147#define TOK_DOUBLE 66
3148#define TOK_ELSE 67
3149#define TOK_ENUM 68
3150#define TOK_EXTERN 69
3151#define TOK_FLOAT 70
3152#define TOK_FOR 71
3153#define TOK_GOTO 72
3154#define TOK_IF 73
3155#define TOK_INLINE 74
3156#define TOK_INT 75
3157#define TOK_LONG 76
3158#define TOK_REGISTER 77
3159#define TOK_RESTRICT 78
3160#define TOK_RETURN 79
3161#define TOK_SHORT 80
3162#define TOK_SIGNED 81
3163#define TOK_SIZEOF 82
3164#define TOK_STATIC 83
3165#define TOK_STRUCT 84
3166#define TOK_SWITCH 85
3167#define TOK_TYPEDEF 86
3168#define TOK_UNION 87
3169#define TOK_UNSIGNED 88
3170#define TOK_VOID 89
3171#define TOK_VOLATILE 90
3172#define TOK_WHILE 91
3173#define TOK_ASM 92
3174#define TOK_ATTRIBUTE 93
3175#define TOK_ALIGNOF 94
3176#define TOK_FIRST_KEYWORD TOK_AUTO
3177#define TOK_LAST_KEYWORD TOK_ALIGNOF
3178
3179#define TOK_DEFINE 100
3180#define TOK_UNDEF 101
3181#define TOK_INCLUDE 102
3182#define TOK_LINE 103
3183#define TOK_ERROR 104
3184#define TOK_WARNING 105
3185#define TOK_PRAGMA 106
3186#define TOK_IFDEF 107
3187#define TOK_IFNDEF 108
3188#define TOK_ELIF 109
3189#define TOK_ENDIF 110
3190
3191#define TOK_FIRST_MACRO TOK_DEFINE
3192#define TOK_LAST_MACRO TOK_ENDIF
3193
Eric Biederman90089602004-05-28 14:11:54 +00003194#define TOK_DEFINED 111
3195#define TOK_EOF 112
Eric Biedermanb138ac82003-04-22 18:44:01 +00003196
3197static const char *tokens[] = {
3198[TOK_UNKNOWN ] = "unknown",
3199[TOK_SPACE ] = ":space:",
3200[TOK_SEMI ] = ";",
3201[TOK_LBRACE ] = "{",
3202[TOK_RBRACE ] = "}",
3203[TOK_COMMA ] = ",",
3204[TOK_EQ ] = "=",
3205[TOK_COLON ] = ":",
3206[TOK_LBRACKET ] = "[",
3207[TOK_RBRACKET ] = "]",
3208[TOK_LPAREN ] = "(",
3209[TOK_RPAREN ] = ")",
3210[TOK_STAR ] = "*",
3211[TOK_DOTS ] = "...",
3212[TOK_MORE ] = ">",
3213[TOK_LESS ] = "<",
3214[TOK_TIMESEQ ] = "*=",
3215[TOK_DIVEQ ] = "/=",
3216[TOK_MODEQ ] = "%=",
3217[TOK_PLUSEQ ] = "+=",
3218[TOK_MINUSEQ ] = "-=",
3219[TOK_SLEQ ] = "<<=",
3220[TOK_SREQ ] = ">>=",
3221[TOK_ANDEQ ] = "&=",
3222[TOK_XOREQ ] = "^=",
3223[TOK_OREQ ] = "|=",
3224[TOK_EQEQ ] = "==",
3225[TOK_NOTEQ ] = "!=",
3226[TOK_QUEST ] = "?",
3227[TOK_LOGOR ] = "||",
3228[TOK_LOGAND ] = "&&",
3229[TOK_OR ] = "|",
3230[TOK_AND ] = "&",
3231[TOK_XOR ] = "^",
3232[TOK_LESSEQ ] = "<=",
3233[TOK_MOREEQ ] = ">=",
3234[TOK_SL ] = "<<",
3235[TOK_SR ] = ">>",
3236[TOK_PLUS ] = "+",
3237[TOK_MINUS ] = "-",
3238[TOK_DIV ] = "/",
3239[TOK_MOD ] = "%",
3240[TOK_PLUSPLUS ] = "++",
3241[TOK_MINUSMINUS ] = "--",
3242[TOK_BANG ] = "!",
3243[TOK_ARROW ] = "->",
3244[TOK_DOT ] = ".",
3245[TOK_TILDE ] = "~",
3246[TOK_LIT_STRING ] = ":string:",
3247[TOK_IDENT ] = ":ident:",
3248[TOK_TYPE_NAME ] = ":typename:",
3249[TOK_LIT_CHAR ] = ":char:",
3250[TOK_LIT_INT ] = ":integer:",
3251[TOK_LIT_FLOAT ] = ":float:",
3252[TOK_MACRO ] = "#",
3253[TOK_CONCATENATE ] = "##",
3254
3255[TOK_AUTO ] = "auto",
3256[TOK_BREAK ] = "break",
3257[TOK_CASE ] = "case",
3258[TOK_CHAR ] = "char",
3259[TOK_CONST ] = "const",
3260[TOK_CONTINUE ] = "continue",
3261[TOK_DEFAULT ] = "default",
3262[TOK_DO ] = "do",
3263[TOK_DOUBLE ] = "double",
3264[TOK_ELSE ] = "else",
3265[TOK_ENUM ] = "enum",
3266[TOK_EXTERN ] = "extern",
3267[TOK_FLOAT ] = "float",
3268[TOK_FOR ] = "for",
3269[TOK_GOTO ] = "goto",
3270[TOK_IF ] = "if",
3271[TOK_INLINE ] = "inline",
3272[TOK_INT ] = "int",
3273[TOK_LONG ] = "long",
3274[TOK_REGISTER ] = "register",
3275[TOK_RESTRICT ] = "restrict",
3276[TOK_RETURN ] = "return",
3277[TOK_SHORT ] = "short",
3278[TOK_SIGNED ] = "signed",
3279[TOK_SIZEOF ] = "sizeof",
3280[TOK_STATIC ] = "static",
3281[TOK_STRUCT ] = "struct",
3282[TOK_SWITCH ] = "switch",
3283[TOK_TYPEDEF ] = "typedef",
3284[TOK_UNION ] = "union",
3285[TOK_UNSIGNED ] = "unsigned",
3286[TOK_VOID ] = "void",
3287[TOK_VOLATILE ] = "volatile",
3288[TOK_WHILE ] = "while",
3289[TOK_ASM ] = "asm",
3290[TOK_ATTRIBUTE ] = "__attribute__",
3291[TOK_ALIGNOF ] = "__alignof__",
3292
3293[TOK_DEFINE ] = "define",
3294[TOK_UNDEF ] = "undef",
3295[TOK_INCLUDE ] = "include",
3296[TOK_LINE ] = "line",
3297[TOK_ERROR ] = "error",
3298[TOK_WARNING ] = "warning",
3299[TOK_PRAGMA ] = "pragma",
3300[TOK_IFDEF ] = "ifdef",
3301[TOK_IFNDEF ] = "ifndef",
3302[TOK_ELIF ] = "elif",
3303[TOK_ENDIF ] = "endif",
3304
Eric Biederman90089602004-05-28 14:11:54 +00003305[TOK_DEFINED ] = "defined",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003306[TOK_EOF ] = "EOF",
3307};
3308
3309static unsigned int hash(const char *str, int str_len)
3310{
3311 unsigned int hash;
3312 const char *end;
3313 end = str + str_len;
3314 hash = 0;
3315 for(; str < end; str++) {
3316 hash = (hash *263) + *str;
3317 }
3318 hash = hash & (HASH_TABLE_SIZE -1);
3319 return hash;
3320}
3321
3322static struct hash_entry *lookup(
3323 struct compile_state *state, const char *name, int name_len)
3324{
3325 struct hash_entry *entry;
3326 unsigned int index;
3327 index = hash(name, name_len);
3328 entry = state->hash_table[index];
3329 while(entry &&
3330 ((entry->name_len != name_len) ||
3331 (memcmp(entry->name, name, name_len) != 0))) {
3332 entry = entry->next;
3333 }
3334 if (!entry) {
3335 char *new_name;
3336 /* Get a private copy of the name */
3337 new_name = xmalloc(name_len + 1, "hash_name");
3338 memcpy(new_name, name, name_len);
3339 new_name[name_len] = '\0';
3340
3341 /* Create a new hash entry */
3342 entry = xcmalloc(sizeof(*entry), "hash_entry");
3343 entry->next = state->hash_table[index];
3344 entry->name = new_name;
3345 entry->name_len = name_len;
3346
3347 /* Place the new entry in the hash table */
3348 state->hash_table[index] = entry;
3349 }
3350 return entry;
3351}
3352
3353static void ident_to_keyword(struct compile_state *state, struct token *tk)
3354{
3355 struct hash_entry *entry;
3356 entry = tk->ident;
3357 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3358 (entry->tok == TOK_ENUM_CONST) ||
3359 ((entry->tok >= TOK_FIRST_KEYWORD) &&
3360 (entry->tok <= TOK_LAST_KEYWORD)))) {
3361 tk->tok = entry->tok;
3362 }
3363}
3364
3365static void ident_to_macro(struct compile_state *state, struct token *tk)
3366{
3367 struct hash_entry *entry;
3368 entry = tk->ident;
3369 if (entry &&
3370 (entry->tok >= TOK_FIRST_MACRO) &&
3371 (entry->tok <= TOK_LAST_MACRO)) {
3372 tk->tok = entry->tok;
3373 }
3374}
3375
3376static void hash_keyword(
3377 struct compile_state *state, const char *keyword, int tok)
3378{
3379 struct hash_entry *entry;
3380 entry = lookup(state, keyword, strlen(keyword));
3381 if (entry && entry->tok != TOK_UNKNOWN) {
3382 die("keyword %s already hashed", keyword);
3383 }
3384 entry->tok = tok;
3385}
3386
Eric Biederman90089602004-05-28 14:11:54 +00003387static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003388 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003389 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003390{
3391 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003392 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003393 error(state, 0, "%s already defined", ident->name);
3394 }
3395 sym = xcmalloc(sizeof(*sym), "symbol");
3396 sym->ident = ident;
3397 sym->def = def;
3398 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003399 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003400 sym->next = *chain;
3401 *chain = sym;
3402}
3403
Eric Biederman90089602004-05-28 14:11:54 +00003404static void symbol(
3405 struct compile_state *state, struct hash_entry *ident,
3406 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003407{
Eric Biederman90089602004-05-28 14:11:54 +00003408 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3409}
3410
3411static void var_symbol(struct compile_state *state,
3412 struct hash_entry *ident, struct triple *def)
3413{
3414 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3415 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003416 }
Eric Biederman90089602004-05-28 14:11:54 +00003417 symbol(state, ident, &ident->sym_ident, def, def->type);
3418}
3419
3420static void label_symbol(struct compile_state *state,
3421 struct hash_entry *ident, struct triple *label, int depth)
3422{
3423 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003424}
3425
Eric Biedermanb138ac82003-04-22 18:44:01 +00003426static void start_scope(struct compile_state *state)
3427{
3428 state->scope_depth++;
3429}
3430
Eric Biederman90089602004-05-28 14:11:54 +00003431static void end_scope_syms(struct compile_state *state,
3432 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003433{
3434 struct symbol *sym, *next;
3435 sym = *chain;
3436 while(sym && (sym->scope_depth == depth)) {
3437 next = sym->next;
3438 xfree(sym);
3439 sym = next;
3440 }
3441 *chain = sym;
3442}
3443
3444static void end_scope(struct compile_state *state)
3445{
3446 int i;
3447 int depth;
3448 /* Walk through the hash table and remove all symbols
3449 * in the current scope.
3450 */
3451 depth = state->scope_depth;
3452 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3453 struct hash_entry *entry;
3454 entry = state->hash_table[i];
3455 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003456 end_scope_syms(state, &entry->sym_label, depth);
3457 end_scope_syms(state, &entry->sym_tag, depth);
3458 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003459 entry = entry->next;
3460 }
3461 }
3462 state->scope_depth = depth - 1;
3463}
3464
3465static void register_keywords(struct compile_state *state)
3466{
3467 hash_keyword(state, "auto", TOK_AUTO);
3468 hash_keyword(state, "break", TOK_BREAK);
3469 hash_keyword(state, "case", TOK_CASE);
3470 hash_keyword(state, "char", TOK_CHAR);
3471 hash_keyword(state, "const", TOK_CONST);
3472 hash_keyword(state, "continue", TOK_CONTINUE);
3473 hash_keyword(state, "default", TOK_DEFAULT);
3474 hash_keyword(state, "do", TOK_DO);
3475 hash_keyword(state, "double", TOK_DOUBLE);
3476 hash_keyword(state, "else", TOK_ELSE);
3477 hash_keyword(state, "enum", TOK_ENUM);
3478 hash_keyword(state, "extern", TOK_EXTERN);
3479 hash_keyword(state, "float", TOK_FLOAT);
3480 hash_keyword(state, "for", TOK_FOR);
3481 hash_keyword(state, "goto", TOK_GOTO);
3482 hash_keyword(state, "if", TOK_IF);
3483 hash_keyword(state, "inline", TOK_INLINE);
3484 hash_keyword(state, "int", TOK_INT);
3485 hash_keyword(state, "long", TOK_LONG);
3486 hash_keyword(state, "register", TOK_REGISTER);
3487 hash_keyword(state, "restrict", TOK_RESTRICT);
3488 hash_keyword(state, "return", TOK_RETURN);
3489 hash_keyword(state, "short", TOK_SHORT);
3490 hash_keyword(state, "signed", TOK_SIGNED);
3491 hash_keyword(state, "sizeof", TOK_SIZEOF);
3492 hash_keyword(state, "static", TOK_STATIC);
3493 hash_keyword(state, "struct", TOK_STRUCT);
3494 hash_keyword(state, "switch", TOK_SWITCH);
3495 hash_keyword(state, "typedef", TOK_TYPEDEF);
3496 hash_keyword(state, "union", TOK_UNION);
3497 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3498 hash_keyword(state, "void", TOK_VOID);
3499 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003500 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003501 hash_keyword(state, "while", TOK_WHILE);
3502 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003503 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003504 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3505 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3506}
3507
3508static void register_macro_keywords(struct compile_state *state)
3509{
3510 hash_keyword(state, "define", TOK_DEFINE);
3511 hash_keyword(state, "undef", TOK_UNDEF);
3512 hash_keyword(state, "include", TOK_INCLUDE);
3513 hash_keyword(state, "line", TOK_LINE);
3514 hash_keyword(state, "error", TOK_ERROR);
3515 hash_keyword(state, "warning", TOK_WARNING);
3516 hash_keyword(state, "pragma", TOK_PRAGMA);
3517 hash_keyword(state, "ifdef", TOK_IFDEF);
3518 hash_keyword(state, "ifndef", TOK_IFNDEF);
3519 hash_keyword(state, "elif", TOK_ELIF);
3520 hash_keyword(state, "endif", TOK_ENDIF);
3521}
3522
Eric Biederman90089602004-05-28 14:11:54 +00003523
3524static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3525{
3526 if (ident->sym_define != 0) {
3527 struct macro *macro;
3528 struct macro_arg *arg, *anext;
3529 macro = ident->sym_define;
3530 ident->sym_define = 0;
3531
3532 /* Free the macro arguments... */
3533 anext = macro->args;
3534 while(anext) {
3535 arg = anext;
3536 anext = arg->next;
3537 xfree(arg);
3538 }
3539
3540 /* Free the macro buffer */
3541 xfree(macro->buf);
3542
3543 /* Now free the macro itself */
3544 xfree(macro);
3545 }
3546}
3547
3548static void define_macro(
3549 struct compile_state *state,
3550 struct hash_entry *ident,
3551 const char *value, int value_len, int value_off,
3552 struct macro_arg *args)
3553{
3554 struct macro *macro;
3555 struct macro_arg *arg;
3556 macro = ident->sym_define;
3557 if (macro != 0) {
3558 /* Explicitly allow identical redefinitions of the same macro */
3559 if ((macro->buf_len == value_len) &&
3560 (memcmp(macro->buf, value, value_len))) {
3561 return;
3562 }
3563 error(state, 0, "macro %s already defined\n", ident->name);
3564 }
3565#if 0
3566 fprintf(state->errout, "%s: `%*.*s'\n",
3567 ident->name,
3568 value_len - value_off,
3569 value_len - value_off,
3570 value + value_off);
3571#endif
3572 macro = xmalloc(sizeof(*macro), "macro");
3573 macro->ident = ident;
3574 macro->buf_len = value_len;
3575 macro->buf_off = value_off;
3576 macro->args = args;
3577 macro->buf = xmalloc(macro->buf_len + 2, "macro buf");
3578
3579 macro->argc = 0;
3580 for(arg = args; arg; arg = arg->next) {
3581 macro->argc += 1;
3582 }
3583
3584 memcpy(macro->buf, value, macro->buf_len);
3585 macro->buf[macro->buf_len] = '\n';
3586 macro->buf[macro->buf_len+1] = '\0';
3587
3588 ident->sym_define = macro;
3589}
3590
3591static void register_builtin_macro(struct compile_state *state,
3592 const char *name, const char *value)
3593{
3594 struct hash_entry *ident;
3595
3596 if (value[0] == '(') {
3597 internal_error(state, 0, "Builtin macros with arguments not supported");
3598 }
3599 ident = lookup(state, name, strlen(name));
3600 define_macro(state, ident, value, strlen(value), 0, 0);
3601}
3602
3603static void register_builtin_macros(struct compile_state *state)
3604{
3605 char buf[30];
3606 char scratch[30];
3607 time_t now;
3608 struct tm *tm;
3609 now = time(NULL);
3610 tm = localtime(&now);
3611
3612 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3613 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3614 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3615 register_builtin_macro(state, "__LINE__", "54321");
3616
3617 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3618 sprintf(buf, "\"%s\"", scratch);
3619 register_builtin_macro(state, "__DATE__", buf);
3620
3621 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3622 sprintf(buf, "\"%s\"", scratch);
3623 register_builtin_macro(state, "__TIME__", buf);
3624
3625 /* I can't be a conforming implementation of C :( */
3626 register_builtin_macro(state, "__STDC__", "0");
3627 /* In particular I don't conform to C99 */
3628 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3629
3630}
3631
3632static void process_cmdline_macros(struct compile_state *state)
3633{
3634 const char **macro, *name;
3635 struct hash_entry *ident;
3636 for(macro = state->compiler->defines; (name = *macro); macro++) {
3637 const char *body;
3638 size_t name_len;
3639
3640 name_len = strlen(name);
3641 body = strchr(name, '=');
3642 if (!body) {
3643 body = "\0";
3644 } else {
3645 name_len = body - name;
3646 body++;
3647 }
3648 ident = lookup(state, name, name_len);
3649 define_macro(state, ident, body, strlen(body), 0, 0);
3650 }
3651 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3652 ident = lookup(state, name, strlen(name));
3653 undef_macro(state, ident);
3654 }
3655}
3656
Eric Biedermanb138ac82003-04-22 18:44:01 +00003657static int spacep(int c)
3658{
3659 int ret = 0;
3660 switch(c) {
3661 case ' ':
3662 case '\t':
3663 case '\f':
3664 case '\v':
3665 case '\r':
3666 case '\n':
3667 ret = 1;
3668 break;
3669 }
3670 return ret;
3671}
3672
3673static int digitp(int c)
3674{
3675 int ret = 0;
3676 switch(c) {
3677 case '0': case '1': case '2': case '3': case '4':
3678 case '5': case '6': case '7': case '8': case '9':
3679 ret = 1;
3680 break;
3681 }
3682 return ret;
3683}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003684static int digval(int c)
3685{
3686 int val = -1;
3687 if ((c >= '0') && (c <= '9')) {
3688 val = c - '0';
3689 }
3690 return val;
3691}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003692
3693static int hexdigitp(int c)
3694{
3695 int ret = 0;
3696 switch(c) {
3697 case '0': case '1': case '2': case '3': case '4':
3698 case '5': case '6': case '7': case '8': case '9':
3699 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3700 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3701 ret = 1;
3702 break;
3703 }
3704 return ret;
3705}
3706static int hexdigval(int c)
3707{
3708 int val = -1;
3709 if ((c >= '0') && (c <= '9')) {
3710 val = c - '0';
3711 }
3712 else if ((c >= 'A') && (c <= 'F')) {
3713 val = 10 + (c - 'A');
3714 }
3715 else if ((c >= 'a') && (c <= 'f')) {
3716 val = 10 + (c - 'a');
3717 }
3718 return val;
3719}
3720
3721static int octdigitp(int c)
3722{
3723 int ret = 0;
3724 switch(c) {
3725 case '0': case '1': case '2': case '3':
3726 case '4': case '5': case '6': case '7':
3727 ret = 1;
3728 break;
3729 }
3730 return ret;
3731}
3732static int octdigval(int c)
3733{
3734 int val = -1;
3735 if ((c >= '0') && (c <= '7')) {
3736 val = c - '0';
3737 }
3738 return val;
3739}
3740
3741static int letterp(int c)
3742{
3743 int ret = 0;
3744 switch(c) {
3745 case 'a': case 'b': case 'c': case 'd': case 'e':
3746 case 'f': case 'g': case 'h': case 'i': case 'j':
3747 case 'k': case 'l': case 'm': case 'n': case 'o':
3748 case 'p': case 'q': case 'r': case 's': case 't':
3749 case 'u': case 'v': case 'w': case 'x': case 'y':
3750 case 'z':
3751 case 'A': case 'B': case 'C': case 'D': case 'E':
3752 case 'F': case 'G': case 'H': case 'I': case 'J':
3753 case 'K': case 'L': case 'M': case 'N': case 'O':
3754 case 'P': case 'Q': case 'R': case 'S': case 'T':
3755 case 'U': case 'V': case 'W': case 'X': case 'Y':
3756 case 'Z':
3757 case '_':
3758 ret = 1;
3759 break;
3760 }
3761 return ret;
3762}
3763
Eric Biederman90089602004-05-28 14:11:54 +00003764static const char *identifier(const char *str, const char *end)
3765{
3766 if (letterp(*str)) {
3767 for(; str < end; str++) {
3768 int c;
3769 c = *str;
3770 if (!letterp(c) && !digitp(c)) {
3771 break;
3772 }
3773 }
3774 }
3775 return str;
3776}
3777
Eric Biedermanb138ac82003-04-22 18:44:01 +00003778static int char_value(struct compile_state *state,
3779 const signed char **strp, const signed char *end)
3780{
3781 const signed char *str;
3782 int c;
3783 str = *strp;
3784 c = *str++;
3785 if ((c == '\\') && (str < end)) {
3786 switch(*str) {
3787 case 'n': c = '\n'; str++; break;
3788 case 't': c = '\t'; str++; break;
3789 case 'v': c = '\v'; str++; break;
3790 case 'b': c = '\b'; str++; break;
3791 case 'r': c = '\r'; str++; break;
3792 case 'f': c = '\f'; str++; break;
3793 case 'a': c = '\a'; str++; break;
3794 case '\\': c = '\\'; str++; break;
3795 case '?': c = '?'; str++; break;
3796 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003797 case '"': c = '"'; str++; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003798 case 'x':
3799 c = 0;
3800 str++;
3801 while((str < end) && hexdigitp(*str)) {
3802 c <<= 4;
3803 c += hexdigval(*str);
3804 str++;
3805 }
3806 break;
3807 case '0': case '1': case '2': case '3':
3808 case '4': case '5': case '6': case '7':
3809 c = 0;
3810 while((str < end) && octdigitp(*str)) {
3811 c <<= 3;
3812 c += octdigval(*str);
3813 str++;
3814 }
3815 break;
3816 default:
3817 error(state, 0, "Invalid character constant");
3818 break;
3819 }
3820 }
3821 *strp = str;
3822 return c;
3823}
3824
Eric Biederman90089602004-05-28 14:11:54 +00003825static const char *after_digits(const char *ptr, const char *end)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003826{
3827 while((ptr < end) && digitp(*ptr)) {
3828 ptr++;
3829 }
3830 return ptr;
3831}
3832
Eric Biederman90089602004-05-28 14:11:54 +00003833static const char *after_octdigits(const char *ptr, const char *end)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003834{
3835 while((ptr < end) && octdigitp(*ptr)) {
3836 ptr++;
3837 }
3838 return ptr;
3839}
3840
Eric Biederman90089602004-05-28 14:11:54 +00003841static const char *after_hexdigits(const char *ptr, const char *end)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003842{
3843 while((ptr < end) && hexdigitp(*ptr)) {
3844 ptr++;
3845 }
3846 return ptr;
3847}
3848
3849static void save_string(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003850 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003851{
3852 char *str;
3853 int str_len;
3854 /* Create a private copy of the string */
3855 str_len = end - start + 1;
3856 str = xmalloc(str_len + 1, id);
3857 memcpy(str, start, str_len);
3858 str[str_len] = '\0';
3859
3860 /* Store the copy in the token */
3861 tk->val.str = str;
3862 tk->str_len = str_len;
3863}
Eric Biederman90089602004-05-28 14:11:54 +00003864
3865static int lparen_peek(struct compile_state *state, struct file_state *file)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003866{
Eric Biederman90089602004-05-28 14:11:54 +00003867 const char *tokp, *end;
3868 /* Is the next token going to be an lparen?
3869 * Whitespace tokens are significant for seeing if a macro
3870 * should be expanded.
3871 */
3872 tokp = file->pos;
3873 end = file->buf + file->size;
3874 return (tokp < end) && (*tokp == '(');
3875}
3876
3877static void raw_next_token(struct compile_state *state,
3878 struct file_state *file, struct token *tk)
3879{
3880 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003881 int c, c1, c2, c3;
Eric Biederman90089602004-05-28 14:11:54 +00003882 const char *tokp, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003883 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00003884
Eric Biedermanb138ac82003-04-22 18:44:01 +00003885 tk->str_len = 0;
3886 tk->ident = 0;
3887 token = tokp = file->pos;
3888 end = file->buf + file->size;
3889 tok = TOK_UNKNOWN;
3890 c = -1;
3891 if (tokp < end) {
3892 c = *tokp;
3893 }
3894 c1 = -1;
3895 if ((tokp + 1) < end) {
3896 c1 = tokp[1];
3897 }
3898 c2 = -1;
3899 if ((tokp + 2) < end) {
3900 c2 = tokp[2];
3901 }
3902 c3 = -1;
3903 if ((tokp + 3) < end) {
3904 c3 = tokp[3];
3905 }
3906 if (tokp >= end) {
3907 tok = TOK_EOF;
3908 tokp = end;
3909 }
3910 /* Whitespace */
3911 else if (spacep(c)) {
3912 tok = TOK_SPACE;
3913 while ((tokp < end) && spacep(c)) {
3914 if (c == '\n') {
3915 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003916 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003917 file->line_start = tokp + 1;
3918 }
3919 c = *(++tokp);
3920 }
3921 if (!spacep(c)) {
3922 tokp--;
3923 }
3924 }
3925 /* EOL Comments */
3926 else if ((c == '/') && (c1 == '/')) {
3927 tok = TOK_SPACE;
3928 for(tokp += 2; tokp < end; tokp++) {
3929 c = *tokp;
3930 if (c == '\n') {
3931 file->line++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003932 file->report_line++;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003933 file->line_start = tokp +1;
3934 break;
3935 }
3936 }
3937 }
3938 /* Comments */
3939 else if ((c == '/') && (c1 == '*')) {
3940 int line;
Eric Biederman90089602004-05-28 14:11:54 +00003941 const char *line_start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003942 line = file->line;
3943 line_start = file->line_start;
3944 for(tokp += 2; (end - tokp) >= 2; tokp++) {
3945 c = *tokp;
3946 if (c == '\n') {
3947 line++;
3948 line_start = tokp +1;
3949 }
3950 else if ((c == '*') && (tokp[1] == '/')) {
3951 tok = TOK_SPACE;
3952 tokp += 1;
3953 break;
3954 }
3955 }
3956 if (tok == TOK_UNKNOWN) {
3957 error(state, 0, "unterminated comment");
3958 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003959 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003960 file->line = line;
3961 file->line_start = line_start;
3962 }
3963 /* string constants */
3964 else if ((c == '"') ||
3965 ((c == 'L') && (c1 == '"'))) {
3966 int line;
Eric Biederman90089602004-05-28 14:11:54 +00003967 const char *line_start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003968 int wchar;
3969 line = file->line;
3970 line_start = file->line_start;
3971 wchar = 0;
3972 if (c == 'L') {
3973 wchar = 1;
3974 tokp++;
3975 }
3976 for(tokp += 1; tokp < end; tokp++) {
3977 c = *tokp;
3978 if (c == '\n') {
3979 line++;
3980 line_start = tokp + 1;
3981 }
3982 else if ((c == '\\') && (tokp +1 < end)) {
3983 tokp++;
3984 }
3985 else if (c == '"') {
3986 tok = TOK_LIT_STRING;
3987 break;
3988 }
3989 }
3990 if (tok == TOK_UNKNOWN) {
3991 error(state, 0, "unterminated string constant");
3992 }
3993 if (line != file->line) {
3994 warning(state, 0, "multiline string constant");
3995 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00003996 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003997 file->line = line;
3998 file->line_start = line_start;
3999
4000 /* Save the string value */
4001 save_string(state, tk, token, tokp, "literal string");
4002 }
4003 /* character constants */
4004 else if ((c == '\'') ||
4005 ((c == 'L') && (c1 == '\''))) {
4006 int line;
Eric Biederman90089602004-05-28 14:11:54 +00004007 const char *line_start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004008 int wchar;
4009 line = file->line;
4010 line_start = file->line_start;
4011 wchar = 0;
4012 if (c == 'L') {
4013 wchar = 1;
4014 tokp++;
4015 }
4016 for(tokp += 1; tokp < end; tokp++) {
4017 c = *tokp;
4018 if (c == '\n') {
4019 line++;
4020 line_start = tokp + 1;
4021 }
4022 else if ((c == '\\') && (tokp +1 < end)) {
4023 tokp++;
4024 }
4025 else if (c == '\'') {
4026 tok = TOK_LIT_CHAR;
4027 break;
4028 }
4029 }
4030 if (tok == TOK_UNKNOWN) {
4031 error(state, 0, "unterminated character constant");
4032 }
4033 if (line != file->line) {
4034 warning(state, 0, "multiline character constant");
4035 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004036 file->report_line += line - file->line;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004037 file->line = line;
4038 file->line_start = line_start;
4039
4040 /* Save the character value */
4041 save_string(state, tk, token, tokp, "literal character");
4042 }
4043 /* integer and floating constants
4044 * Integer Constants
4045 * {digits}
4046 * 0[Xx]{hexdigits}
4047 * 0{octdigit}+
4048 *
4049 * Floating constants
4050 * {digits}.{digits}[Ee][+-]?{digits}
4051 * {digits}.{digits}
4052 * {digits}[Ee][+-]?{digits}
4053 * .{digits}[Ee][+-]?{digits}
4054 * .{digits}
4055 */
4056
4057 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biederman90089602004-05-28 14:11:54 +00004058 const char *next, *new;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004059 int is_float;
4060 is_float = 0;
4061 if (c != '.') {
4062 next = after_digits(tokp, end);
4063 }
4064 else {
4065 next = tokp;
4066 }
4067 if (next[0] == '.') {
4068 new = after_digits(next, end);
4069 is_float = (new != next);
4070 next = new;
4071 }
4072 if ((next[0] == 'e') || (next[0] == 'E')) {
4073 if (((next + 1) < end) &&
4074 ((next[1] == '+') || (next[1] == '-'))) {
4075 next++;
4076 }
4077 new = after_digits(next, end);
4078 is_float = (new != next);
4079 next = new;
4080 }
4081 if (is_float) {
4082 tok = TOK_LIT_FLOAT;
4083 if ((next < end) && (
4084 (next[0] == 'f') ||
4085 (next[0] == 'F') ||
4086 (next[0] == 'l') ||
4087 (next[0] == 'L'))
4088 ) {
4089 next++;
4090 }
4091 }
4092 if (!is_float && digitp(c)) {
4093 tok = TOK_LIT_INT;
4094 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
4095 next = after_hexdigits(tokp + 2, end);
4096 }
4097 else if (c == '0') {
4098 next = after_octdigits(tokp, end);
4099 }
4100 else {
4101 next = after_digits(tokp, end);
4102 }
4103 /* crazy integer suffixes */
4104 if ((next < end) &&
4105 ((next[0] == 'u') || (next[0] == 'U'))) {
4106 next++;
4107 if ((next < end) &&
4108 ((next[0] == 'l') || (next[0] == 'L'))) {
4109 next++;
4110 }
4111 }
4112 else if ((next < end) &&
4113 ((next[0] == 'l') || (next[0] == 'L'))) {
4114 next++;
4115 if ((next < end) &&
4116 ((next[0] == 'u') || (next[0] == 'U'))) {
4117 next++;
4118 }
4119 }
4120 }
4121 tokp = next - 1;
4122
4123 /* Save the integer/floating point value */
4124 save_string(state, tk, token, tokp, "literal number");
4125 }
4126 /* identifiers */
4127 else if (letterp(c)) {
4128 tok = TOK_IDENT;
Eric Biederman90089602004-05-28 14:11:54 +00004129 tokp = identifier(tokp, end);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004130 tokp -= 1;
4131 tk->ident = lookup(state, token, tokp +1 - token);
Eric Biederman90089602004-05-28 14:11:54 +00004132 /* See if this identifier can be macro expanded */
4133 tk->val.notmacro = 0;
4134 if ((tokp < end) && (tokp[1] == '$')) {
4135 tokp++;
4136 tk->val.notmacro = 1;
4137 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004138 }
4139 /* C99 alternate macro characters */
4140 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
4141 tokp += 3;
4142 tok = TOK_CONCATENATE;
4143 }
4144 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { tokp += 2; tok = TOK_DOTS; }
4145 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { tokp += 2; tok = TOK_SLEQ; }
4146 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { tokp += 2; tok = TOK_SREQ; }
4147 else if ((c == '*') && (c1 == '=')) { tokp += 1; tok = TOK_TIMESEQ; }
4148 else if ((c == '/') && (c1 == '=')) { tokp += 1; tok = TOK_DIVEQ; }
4149 else if ((c == '%') && (c1 == '=')) { tokp += 1; tok = TOK_MODEQ; }
4150 else if ((c == '+') && (c1 == '=')) { tokp += 1; tok = TOK_PLUSEQ; }
4151 else if ((c == '-') && (c1 == '=')) { tokp += 1; tok = TOK_MINUSEQ; }
4152 else if ((c == '&') && (c1 == '=')) { tokp += 1; tok = TOK_ANDEQ; }
4153 else if ((c == '^') && (c1 == '=')) { tokp += 1; tok = TOK_XOREQ; }
4154 else if ((c == '|') && (c1 == '=')) { tokp += 1; tok = TOK_OREQ; }
4155 else if ((c == '=') && (c1 == '=')) { tokp += 1; tok = TOK_EQEQ; }
4156 else if ((c == '!') && (c1 == '=')) { tokp += 1; tok = TOK_NOTEQ; }
4157 else if ((c == '|') && (c1 == '|')) { tokp += 1; tok = TOK_LOGOR; }
4158 else if ((c == '&') && (c1 == '&')) { tokp += 1; tok = TOK_LOGAND; }
4159 else if ((c == '<') && (c1 == '=')) { tokp += 1; tok = TOK_LESSEQ; }
4160 else if ((c == '>') && (c1 == '=')) { tokp += 1; tok = TOK_MOREEQ; }
4161 else if ((c == '<') && (c1 == '<')) { tokp += 1; tok = TOK_SL; }
4162 else if ((c == '>') && (c1 == '>')) { tokp += 1; tok = TOK_SR; }
4163 else if ((c == '+') && (c1 == '+')) { tokp += 1; tok = TOK_PLUSPLUS; }
4164 else if ((c == '-') && (c1 == '-')) { tokp += 1; tok = TOK_MINUSMINUS; }
4165 else if ((c == '-') && (c1 == '>')) { tokp += 1; tok = TOK_ARROW; }
4166 else if ((c == '<') && (c1 == ':')) { tokp += 1; tok = TOK_LBRACKET; }
4167 else if ((c == ':') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACKET; }
4168 else if ((c == '<') && (c1 == '%')) { tokp += 1; tok = TOK_LBRACE; }
4169 else if ((c == '%') && (c1 == '>')) { tokp += 1; tok = TOK_RBRACE; }
4170 else if ((c == '%') && (c1 == ':')) { tokp += 1; tok = TOK_MACRO; }
4171 else if ((c == '#') && (c1 == '#')) { tokp += 1; tok = TOK_CONCATENATE; }
4172 else if (c == ';') { tok = TOK_SEMI; }
4173 else if (c == '{') { tok = TOK_LBRACE; }
4174 else if (c == '}') { tok = TOK_RBRACE; }
4175 else if (c == ',') { tok = TOK_COMMA; }
4176 else if (c == '=') { tok = TOK_EQ; }
4177 else if (c == ':') { tok = TOK_COLON; }
4178 else if (c == '[') { tok = TOK_LBRACKET; }
4179 else if (c == ']') { tok = TOK_RBRACKET; }
4180 else if (c == '(') { tok = TOK_LPAREN; }
4181 else if (c == ')') { tok = TOK_RPAREN; }
4182 else if (c == '*') { tok = TOK_STAR; }
4183 else if (c == '>') { tok = TOK_MORE; }
4184 else if (c == '<') { tok = TOK_LESS; }
4185 else if (c == '?') { tok = TOK_QUEST; }
4186 else if (c == '|') { tok = TOK_OR; }
4187 else if (c == '&') { tok = TOK_AND; }
4188 else if (c == '^') { tok = TOK_XOR; }
4189 else if (c == '+') { tok = TOK_PLUS; }
4190 else if (c == '-') { tok = TOK_MINUS; }
4191 else if (c == '/') { tok = TOK_DIV; }
4192 else if (c == '%') { tok = TOK_MOD; }
4193 else if (c == '!') { tok = TOK_BANG; }
4194 else if (c == '.') { tok = TOK_DOT; }
4195 else if (c == '~') { tok = TOK_TILDE; }
4196 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004197
4198 file->pos = tokp + 1;
4199 tk->tok = tok;
4200 if (tok == TOK_IDENT) {
4201 ident_to_keyword(state, tk);
4202 }
Eric Biederman90089602004-05-28 14:11:54 +00004203}
4204
4205static void next_token(struct compile_state *state, struct token *tk)
4206{
4207 struct file_state *file;
4208 file = state->file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004209 /* Don't return space tokens. */
Eric Biederman90089602004-05-28 14:11:54 +00004210 do {
4211 raw_next_token(state, file, tk);
4212 if (tk->tok == TOK_MACRO) {
4213 /* Only match preprocessor directives at the start of a line */
4214 const char *ptr;
4215 for(ptr = file->line_start; spacep(*ptr); ptr++)
4216 ;
4217 if (ptr != file->pos - 1) {
4218 tk->tok = TOK_UNKNOWN;
4219 }
4220 }
4221 if (tk->tok == TOK_UNKNOWN) {
4222 error(state, 0, "unknown token");
4223 }
4224 } while(tk->tok == TOK_SPACE);
4225}
4226
4227static void check_tok(struct compile_state *state, struct token *tk, int tok)
4228{
4229 if (tk->tok != tok) {
4230 const char *name1, *name2;
4231 name1 = tokens[tk->tok];
4232 name2 = "";
4233 if (tk->tok == TOK_IDENT) {
4234 name2 = tk->ident->name;
4235 }
4236 error(state, 0, "\tfound %s %s expected %s",
4237 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004238 }
4239}
4240
Eric Biederman90089602004-05-28 14:11:54 +00004241struct macro_arg_value {
4242 struct hash_entry *ident;
4243 unsigned char *value;
4244 size_t len;
4245};
4246static struct macro_arg_value *read_macro_args(
4247 struct compile_state *state, struct macro *macro,
4248 struct file_state *file, struct token *tk)
4249{
4250 struct macro_arg_value *argv;
4251 struct macro_arg *arg;
4252 int paren_depth;
4253 int i;
4254
4255 if (macro->argc == 0) {
4256 do {
4257 raw_next_token(state, file, tk);
4258 } while(tk->tok == TOK_SPACE);
4259 return 0;
4260 }
4261 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4262 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4263 argv[i].value = 0;
4264 argv[i].len = 0;
4265 argv[i].ident = arg->ident;
4266 }
4267 paren_depth = 0;
4268 i = 0;
4269
4270 for(;;) {
4271 const char *start;
4272 size_t len;
4273 start = file->pos;
4274 raw_next_token(state, file, tk);
4275
4276 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4277 (argv[i].ident != state->i___VA_ARGS__))
4278 {
4279 i++;
4280 if (i >= macro->argc) {
4281 error(state, 0, "too many args to %s\n",
4282 macro->ident->name);
4283 }
4284 continue;
4285 }
4286
4287 if (tk->tok == TOK_LPAREN) {
4288 paren_depth++;
4289 }
4290
4291 if (tk->tok == TOK_RPAREN) {
4292 if (paren_depth == 0) {
4293 break;
4294 }
4295 paren_depth--;
4296 }
4297 if (tk->tok == TOK_EOF) {
4298 error(state, 0, "End of file encountered while parsing macro arguments");
4299 }
4300
4301 len = file->pos - start;
4302 argv[i].value = xrealloc(
4303 argv[i].value, argv[i].len + len, "macro args");
4304 memcpy(argv[i].value + argv[i].len, start, len);
4305 argv[i].len += len;
4306 }
4307 if (i != macro->argc -1) {
4308 error(state, 0, "missing %s arg %d\n",
4309 macro->ident->name, i +2);
4310 }
4311 return argv;
4312}
4313
4314
4315static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4316{
4317 int i;
4318 for(i = 0; i < macro->argc; i++) {
4319 xfree(argv[i].value);
4320 }
4321 xfree(argv);
4322}
4323
4324struct macro_buf {
4325 char *str;
4326 size_t len, pos;
4327};
4328
4329static void append_macro_text(struct compile_state *state,
4330 struct macro *macro, struct macro_buf *buf,
4331 const char *fstart, size_t flen)
4332{
4333#if 0
4334 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4335 buf->pos, buf->pos, buf->str,
4336 flen, flen, fstart);
4337#endif
4338 if ((buf->pos + flen) < buf->len) {
4339 memcpy(buf->str + buf->pos, fstart, flen);
4340 } else {
4341 buf->str = xrealloc(buf->str, buf->len + flen, macro->ident->name);
4342 memcpy(buf->str + buf->pos, fstart, flen);
4343 buf->len += flen;
4344 }
4345 buf->pos += flen;
4346}
4347
4348static int compile_macro(struct compile_state *state,
4349 struct file_state **filep, struct token *tk);
4350
4351static void macro_expand_args(struct compile_state *state,
4352 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4353{
4354 size_t i;
4355
4356 for(i = 0; i < macro->argc; i++) {
4357 struct file_state fmacro, *file;
4358 struct macro_buf buf;
4359 const char *fstart;
4360 size_t flen;
4361
4362 fmacro.basename = argv[i].ident->name;
4363 fmacro.dirname = "";
4364 fmacro.size = argv[i].len;
4365 fmacro.buf = argv[i].value;
4366 fmacro.pos = fmacro.buf;
4367 fmacro.line_start = fmacro.buf;
4368 fmacro.line = 1;
4369 fmacro.report_line = 1;
4370 fmacro.report_name = fmacro.basename;
4371 fmacro.report_dir = fmacro.dirname;
4372 fmacro.prev = 0;
4373
4374 buf.len = argv[i].len;
4375 buf.str = xmalloc(buf.len, argv[i].ident->name);
4376 buf.pos = 0;
4377
4378 file = &fmacro;
4379 for(;;) {
4380 fstart = file->pos;
4381 raw_next_token(state, file, tk);
4382 flen = file->pos - fstart;
4383
4384 if (tk->tok == TOK_EOF) {
4385 struct file_state *old;
4386 old = file;
4387 file = file->prev;
4388 if (!file) {
4389 break;
4390 }
4391 /* old->basename is used keep it */
4392 xfree(old->dirname);
4393 xfree(old->buf);
4394 xfree(old);
4395 continue;
4396 }
4397 else if (tk->ident && tk->ident->sym_define) {
4398 if (compile_macro(state, &file, tk)) {
4399 continue;
4400 }
4401 }
4402
4403 append_macro_text(state, macro, &buf,
4404 fstart, flen);
4405 }
4406
4407 xfree(argv[i].value);
4408 argv[i].value = buf.str;
4409 argv[i].len = buf.pos;
4410 }
4411 return;
4412}
4413
4414static void expand_macro(struct compile_state *state,
4415 struct macro *macro, struct macro_buf *buf,
4416 struct macro_arg_value *argv, struct token *tk)
4417{
4418 struct file_state fmacro;
4419 const char space[] = " ";
4420 const char *fstart;
4421 size_t flen;
4422 size_t i, j;
4423 fmacro.basename = macro->ident->name;
4424 fmacro.dirname = "";
4425 fmacro.size = macro->buf_len - macro->buf_off;;
4426 fmacro.buf = macro->buf + macro->buf_off;
4427 fmacro.pos = fmacro.buf;
4428 fmacro.line_start = fmacro.buf;
4429 fmacro.line = 1;
4430 fmacro.report_line = 1;
4431 fmacro.report_name = fmacro.basename;
4432 fmacro.report_dir = fmacro.dirname;
4433 fmacro.prev = 0;
4434
4435 buf->len = macro->buf_len + 3;
4436 buf->str = xmalloc(buf->len, macro->ident->name);
4437 buf->pos = 0;
4438
4439 fstart = fmacro.pos;
4440 raw_next_token(state, &fmacro, tk);
4441 while(tk->tok != TOK_EOF) {
4442 flen = fmacro.pos - fstart;
4443 switch(tk->tok) {
4444 case TOK_IDENT:
4445 for(i = 0; i < macro->argc; i++) {
4446 if (argv[i].ident == tk->ident) {
4447 break;
4448 }
4449 }
4450 if (i >= macro->argc) {
4451 break;
4452 }
4453 /* Substitute macro parameter */
4454 fstart = argv[i].value;
4455 flen = argv[i].len;
4456 break;
4457 case TOK_MACRO:
4458 if (!macro->buf_off) {
4459 break;
4460 }
4461 do {
4462 raw_next_token(state, &fmacro, tk);
4463 } while(tk->tok == TOK_SPACE);
4464 check_tok(state, tk, TOK_IDENT);
4465 for(i = 0; i < macro->argc; i++) {
4466 if (argv[i].ident == tk->ident) {
4467 break;
4468 }
4469 }
4470 if (i >= macro->argc) {
4471 error(state, 0, "parameter `%s' not found",
4472 tk->ident->name);
4473 }
4474 /* Stringize token */
4475 append_macro_text(state, macro, buf, "\"", 1);
4476 for(j = 0; j < argv[i].len; j++) {
4477 char *str = argv[i].value + j;
4478 size_t len = 1;
4479 if (*str == '\\') {
4480 str = "\\";
4481 len = 2;
4482 }
4483 else if (*str == '"') {
4484 str = "\\\"";
4485 len = 2;
4486 }
4487 append_macro_text(state, macro, buf, str, len);
4488 }
4489 append_macro_text(state, macro, buf, "\"", 1);
4490 fstart = 0;
4491 flen = 0;
4492 break;
4493 case TOK_CONCATENATE:
4494 /* Concatenate tokens */
4495 /* Delete the previous whitespace token */
4496 if (buf->str[buf->pos - 1] == ' ') {
4497 buf->pos -= 1;
4498 }
4499 /* Skip the next sequence of whitspace tokens */
4500 do {
4501 fstart = fmacro.pos;
4502 raw_next_token(state, &fmacro, tk);
4503 } while(tk->tok == TOK_SPACE);
4504 /* Restart at the top of the loop.
4505 * I need to process the non white space token.
4506 */
4507 continue;
4508 break;
4509 case TOK_SPACE:
4510 /* Collapse multiple spaces into one */
4511 if (buf->str[buf->pos - 1] != ' ') {
4512 fstart = space;
4513 flen = 1;
4514 } else {
4515 fstart = 0;
4516 flen = 0;
4517 }
4518 break;
4519 default:
4520 break;
4521 }
4522
4523 append_macro_text(state, macro, buf, fstart, flen);
4524
4525 fstart = fmacro.pos;
4526 raw_next_token(state, &fmacro, tk);
4527 }
4528}
4529
4530static void tag_macro_name(struct compile_state *state,
4531 struct macro *macro, struct macro_buf *buf,
4532 struct token *tk)
4533{
4534 /* Guard all instances of the macro name in the replacement
4535 * text from further macro expansion.
4536 */
4537 struct file_state fmacro;
4538 const char *fstart;
4539 size_t flen;
4540 fmacro.basename = macro->ident->name;
4541 fmacro.dirname = "";
4542 fmacro.size = buf->pos;
4543 fmacro.buf = buf->str;
4544 fmacro.pos = fmacro.buf;
4545 fmacro.line_start = fmacro.buf;
4546 fmacro.line = 1;
4547 fmacro.report_line = 1;
4548 fmacro.report_name = fmacro.basename;
4549 fmacro.report_dir = fmacro.dirname;
4550 fmacro.prev = 0;
4551
4552 buf->len = macro->buf_len + 3;
4553 buf->str = xmalloc(buf->len, macro->ident->name);
4554 buf->pos = 0;
4555
4556 fstart = fmacro.pos;
4557 raw_next_token(state, &fmacro, tk);
4558 while(tk->tok != TOK_EOF) {
4559 flen = fmacro.pos - fstart;
4560 if ((tk->tok == TOK_IDENT) &&
4561 (tk->ident == macro->ident) &&
4562 (tk->val.notmacro == 0)) {
4563 append_macro_text(state, macro, buf, fstart, flen);
4564 fstart = "$";
4565 flen = 1;
4566 }
4567
4568 append_macro_text(state, macro, buf, fstart, flen);
4569
4570 fstart = fmacro.pos;
4571 raw_next_token(state, &fmacro, tk);
4572 }
4573 xfree(fmacro.buf);
4574}
4575
4576static int compile_macro(struct compile_state *state,
4577 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004578{
4579 struct file_state *file;
4580 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004581 struct macro *macro;
4582 struct macro_arg_value *argv;
4583 struct macro_buf buf;
4584
4585#if 0
4586 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4587#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004588 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004589 macro = ident->sym_define;
4590
4591 /* If this token comes from a macro expansion ignore it */
4592 if (tk->val.notmacro) {
4593 return 0;
4594 }
4595 /* If I am a function like macro and the identifier is not followed
4596 * by a left parenthesis, do nothing.
4597 */
4598 if ((macro->buf_off != 0) && !lparen_peek(state, *filep)) {
4599 return 0;
4600 }
4601
4602 /* Read in the macro arguments */
4603 argv = 0;
4604 if (macro->buf_off) {
4605 raw_next_token(state, *filep, tk);
4606 check_tok(state, tk, TOK_LPAREN);
4607
4608 argv = read_macro_args(state, macro, *filep, tk);
4609
4610 check_tok(state, tk, TOK_RPAREN);
4611 }
4612 /* Macro expand the macro arguments */
4613 macro_expand_args(state, macro, argv, tk);
4614
4615 buf.str = 0;
4616 buf.len = 0;
4617 buf.pos = 0;
4618 if (ident == state->i___FILE__) {
4619 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4620 buf.str = xmalloc(buf.len, ident->name);
4621 sprintf(buf.str, "\"%s\"", state->file->basename);
4622 buf.pos = strlen(buf.str);
4623 }
4624 else if (ident == state->i___LINE__) {
4625 buf.len = 30;
4626 buf.str = xmalloc(buf.len, ident->name);
4627 sprintf(buf.str, "%d", state->file->line);
4628 buf.pos = strlen(buf.str);
4629 }
4630 else {
4631 expand_macro(state, macro, &buf, argv, tk);
4632 }
4633 /* Tag the macro name with a $ so it will no longer
4634 * be regonized as a canidate for macro expansion.
4635 */
4636 tag_macro_name(state, macro, &buf, tk);
4637 append_macro_text(state, macro, &buf, "\n\0", 2);
4638
4639#if 0
4640 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4641 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4642#endif
4643
4644 free_macro_args(macro, argv);
4645
Eric Biedermanb138ac82003-04-22 18:44:01 +00004646 file = xmalloc(sizeof(*file), "file_state");
Eric Biederman90089602004-05-28 14:11:54 +00004647 file->basename = xstrdup(ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004648 file->dirname = xstrdup("");
Eric Biederman90089602004-05-28 14:11:54 +00004649 file->buf = buf.str;
4650 file->size = buf.pos - 2;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004651 file->pos = file->buf;
4652 file->line_start = file->pos;
4653 file->line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004654 file->report_line = 1;
4655 file->report_name = file->basename;
4656 file->report_dir = file->dirname;
Eric Biederman90089602004-05-28 14:11:54 +00004657 file->prev = *filep;
4658 *filep = file;
4659 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004660}
4661
4662
4663static int mpeek(struct compile_state *state, int index)
4664{
4665 struct token *tk;
4666 int rescan;
4667 tk = &state->token[index + 1];
4668 if (tk->tok == -1) {
Eric Biederman90089602004-05-28 14:11:54 +00004669 do {
4670 raw_next_token(state, state->file, tk);
4671 } while(tk->tok == TOK_SPACE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004672 }
4673 do {
4674 rescan = 0;
4675 if ((tk->tok == TOK_EOF) &&
4676 (state->file != state->macro_file) &&
4677 (state->file->prev)) {
4678 struct file_state *file = state->file;
4679 state->file = file->prev;
4680 /* file->basename is used keep it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004681 if (file->report_dir != file->dirname) {
4682 xfree(file->report_dir);
4683 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004684 xfree(file->dirname);
4685 xfree(file->buf);
4686 xfree(file);
Eric Biederman90089602004-05-28 14:11:54 +00004687 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004688 rescan = 1;
4689 }
4690 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004691 rescan = compile_macro(state, &state->file, tk);
4692 if (rescan) {
4693 next_token(state, tk);
4694 }
4695
4696 }
4697 } while(rescan);
4698 /* Don't show the token on the next line */
4699 if (state->macro_line < state->macro_file->line) {
4700 return TOK_EOF;
4701 }
4702 return tk->tok;
4703}
4704
4705static void meat(struct compile_state *state, int index, int tok)
4706{
4707 int i;
4708 int next_tok;
4709 next_tok = mpeek(state, index);
4710 if (next_tok != tok) {
4711 check_tok(state, &state->token[index + 1], tok);
4712 }
4713
4714 /* Free the old token value */
4715 if (state->token[index].str_len) {
4716 memset((void *)(state->token[index].val.str), -1,
4717 state->token[index].str_len);
4718 xfree(state->token[index].val.str);
4719 }
4720 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4721 state->token[i] = state->token[i + 1];
4722 }
4723 memset(&state->token[i], 0, sizeof(state->token[i]));
4724 state->token[i].tok = -1;
4725}
4726
4727static int mpeek_raw(struct compile_state *state, int index)
4728{
4729 struct token *tk;
4730 int rescan;
4731 tk = &state->token[index + 1];
4732 if (tk->tok == -1) {
4733 do {
4734 raw_next_token(state, state->file, tk);
4735 } while(tk->tok == TOK_SPACE);
4736 }
4737 do {
4738 rescan = 0;
4739 if ((tk->tok == TOK_EOF) &&
4740 (state->file != state->macro_file) &&
4741 (state->file->prev)) {
4742 struct file_state *file = state->file;
4743 state->file = file->prev;
4744 /* file->basename is used keep it */
4745 if (file->report_dir != file->dirname) {
4746 xfree(file->report_dir);
4747 }
4748 xfree(file->dirname);
4749 xfree(file->buf);
4750 xfree(file);
4751 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004752 rescan = 1;
4753 }
4754 } while(rescan);
4755 /* Don't show the token on the next line */
4756 if (state->macro_line < state->macro_file->line) {
4757 return TOK_EOF;
4758 }
Eric Biederman90089602004-05-28 14:11:54 +00004759 return tk->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004760}
4761
Eric Biederman90089602004-05-28 14:11:54 +00004762static void meat_raw(struct compile_state *state, int index, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004763{
4764 int next_tok;
4765 int i;
Eric Biederman90089602004-05-28 14:11:54 +00004766 next_tok = mpeek_raw(state, index);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004767 if (next_tok != tok) {
Eric Biederman90089602004-05-28 14:11:54 +00004768 check_tok(state, &state->token[index + 1], tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004769 }
Eric Biederman90089602004-05-28 14:11:54 +00004770
Eric Biedermanb138ac82003-04-22 18:44:01 +00004771 /* Free the old token value */
4772 if (state->token[index].str_len) {
4773 memset((void *)(state->token[index].val.str), -1,
4774 state->token[index].str_len);
4775 xfree(state->token[index].val.str);
4776 }
4777 for(i = index; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4778 state->token[i] = state->token[i + 1];
4779 }
4780 memset(&state->token[i], 0, sizeof(state->token[i]));
4781 state->token[i].tok = -1;
4782}
4783
4784static long_t mcexpr(struct compile_state *state, int index);
4785
4786static long_t mprimary_expr(struct compile_state *state, int index)
4787{
4788 long_t val;
4789 int tok;
4790 tok = mpeek(state, index);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004791 switch(tok) {
4792 case TOK_LPAREN:
4793 meat(state, index, TOK_LPAREN);
4794 val = mcexpr(state, index);
4795 meat(state, index, TOK_RPAREN);
4796 break;
4797 case TOK_LIT_INT:
4798 {
Eric Biederman83b991a2003-10-11 06:20:25 +00004799 long lval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004800 char *end;
4801 meat(state, index, TOK_LIT_INT);
4802 errno = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +00004803 lval = strtol(state->token[index].val.str, &end, 0);
4804 if ((lval > LONG_T_MAX) || (lval < LONG_T_MIN) ||
4805 (((lval == LONG_MIN) || (lval == LONG_MAX)) &&
4806 (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004807 error(state, 0, "Integer constant to large");
4808 }
Eric Biederman83b991a2003-10-11 06:20:25 +00004809 val = lval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004810 break;
4811 }
4812 default:
4813 meat(state, index, TOK_LIT_INT);
4814 val = 0;
4815 }
4816 return val;
4817}
4818static long_t munary_expr(struct compile_state *state, int index)
4819{
4820 long_t val;
Eric Biederman90089602004-05-28 14:11:54 +00004821 int tok;
4822 tok = mpeek(state, index);
4823 if ((tok == TOK_IDENT) &&
4824 (state->token[index + 1].ident == state->i_defined)) {
4825 tok = TOK_DEFINED;
4826 }
4827 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004828 case TOK_PLUS:
4829 meat(state, index, TOK_PLUS);
4830 val = munary_expr(state, index);
4831 val = + val;
4832 break;
4833 case TOK_MINUS:
4834 meat(state, index, TOK_MINUS);
4835 val = munary_expr(state, index);
4836 val = - val;
4837 break;
4838 case TOK_TILDE:
4839 meat(state, index, TOK_BANG);
4840 val = munary_expr(state, index);
4841 val = ~ val;
4842 break;
4843 case TOK_BANG:
4844 meat(state, index, TOK_BANG);
4845 val = munary_expr(state, index);
4846 val = ! val;
4847 break;
Eric Biederman90089602004-05-28 14:11:54 +00004848 case TOK_DEFINED:
4849 {
4850 struct hash_entry *ident;
4851 int parens;
4852 meat(state, index, TOK_IDENT);
4853 parens = 0;
4854 if (mpeek_raw(state, index) == TOK_LPAREN) {
4855 meat(state, index, TOK_LPAREN);
4856 parens = 1;
4857 }
4858 meat_raw(state, index, TOK_IDENT);
4859 ident = state->token[index].ident;
4860 val = ident->sym_define != 0;
4861 if (parens) {
4862 meat(state, index, TOK_RPAREN);
4863 }
4864 break;
4865 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004866 default:
4867 val = mprimary_expr(state, index);
4868 break;
4869 }
4870 return val;
4871
4872}
4873static long_t mmul_expr(struct compile_state *state, int index)
4874{
4875 long_t val;
4876 int done;
4877 val = munary_expr(state, index);
4878 do {
4879 long_t right;
4880 done = 0;
4881 switch(mpeek(state, index)) {
4882 case TOK_STAR:
4883 meat(state, index, TOK_STAR);
4884 right = munary_expr(state, index);
4885 val = val * right;
4886 break;
4887 case TOK_DIV:
4888 meat(state, index, TOK_DIV);
4889 right = munary_expr(state, index);
4890 val = val / right;
4891 break;
4892 case TOK_MOD:
4893 meat(state, index, TOK_MOD);
4894 right = munary_expr(state, index);
4895 val = val % right;
4896 break;
4897 default:
4898 done = 1;
4899 break;
4900 }
4901 } while(!done);
4902
4903 return val;
4904}
4905
4906static long_t madd_expr(struct compile_state *state, int index)
4907{
4908 long_t val;
4909 int done;
4910 val = mmul_expr(state, index);
4911 do {
4912 long_t right;
4913 done = 0;
4914 switch(mpeek(state, index)) {
4915 case TOK_PLUS:
4916 meat(state, index, TOK_PLUS);
4917 right = mmul_expr(state, index);
4918 val = val + right;
4919 break;
4920 case TOK_MINUS:
4921 meat(state, index, TOK_MINUS);
4922 right = mmul_expr(state, index);
4923 val = val - right;
4924 break;
4925 default:
4926 done = 1;
4927 break;
4928 }
4929 } while(!done);
4930
4931 return val;
4932}
4933
4934static long_t mshift_expr(struct compile_state *state, int index)
4935{
4936 long_t val;
4937 int done;
4938 val = madd_expr(state, index);
4939 do {
4940 long_t right;
4941 done = 0;
4942 switch(mpeek(state, index)) {
4943 case TOK_SL:
4944 meat(state, index, TOK_SL);
4945 right = madd_expr(state, index);
4946 val = val << right;
4947 break;
4948 case TOK_SR:
4949 meat(state, index, TOK_SR);
4950 right = madd_expr(state, index);
4951 val = val >> right;
4952 break;
4953 default:
4954 done = 1;
4955 break;
4956 }
4957 } while(!done);
4958
4959 return val;
4960}
4961
4962static long_t mrel_expr(struct compile_state *state, int index)
4963{
4964 long_t val;
4965 int done;
4966 val = mshift_expr(state, index);
4967 do {
4968 long_t right;
4969 done = 0;
4970 switch(mpeek(state, index)) {
4971 case TOK_LESS:
4972 meat(state, index, TOK_LESS);
4973 right = mshift_expr(state, index);
4974 val = val < right;
4975 break;
4976 case TOK_MORE:
4977 meat(state, index, TOK_MORE);
4978 right = mshift_expr(state, index);
4979 val = val > right;
4980 break;
4981 case TOK_LESSEQ:
4982 meat(state, index, TOK_LESSEQ);
4983 right = mshift_expr(state, index);
4984 val = val <= right;
4985 break;
4986 case TOK_MOREEQ:
4987 meat(state, index, TOK_MOREEQ);
4988 right = mshift_expr(state, index);
4989 val = val >= right;
4990 break;
4991 default:
4992 done = 1;
4993 break;
4994 }
4995 } while(!done);
4996 return val;
4997}
4998
4999static long_t meq_expr(struct compile_state *state, int index)
5000{
5001 long_t val;
5002 int done;
5003 val = mrel_expr(state, index);
5004 do {
5005 long_t right;
5006 done = 0;
5007 switch(mpeek(state, index)) {
5008 case TOK_EQEQ:
5009 meat(state, index, TOK_EQEQ);
5010 right = mrel_expr(state, index);
5011 val = val == right;
5012 break;
5013 case TOK_NOTEQ:
5014 meat(state, index, TOK_NOTEQ);
5015 right = mrel_expr(state, index);
5016 val = val != right;
5017 break;
5018 default:
5019 done = 1;
5020 break;
5021 }
5022 } while(!done);
5023 return val;
5024}
5025
5026static long_t mand_expr(struct compile_state *state, int index)
5027{
5028 long_t val;
5029 val = meq_expr(state, index);
Eric Biederman90089602004-05-28 14:11:54 +00005030 while (mpeek(state, index) == TOK_AND) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005031 long_t right;
5032 meat(state, index, TOK_AND);
5033 right = meq_expr(state, index);
5034 val = val & right;
5035 }
5036 return val;
5037}
5038
5039static long_t mxor_expr(struct compile_state *state, int index)
5040{
5041 long_t val;
5042 val = mand_expr(state, index);
Eric Biederman90089602004-05-28 14:11:54 +00005043 while (mpeek(state, index) == TOK_XOR) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005044 long_t right;
5045 meat(state, index, TOK_XOR);
5046 right = mand_expr(state, index);
5047 val = val ^ right;
5048 }
5049 return val;
5050}
5051
5052static long_t mor_expr(struct compile_state *state, int index)
5053{
5054 long_t val;
5055 val = mxor_expr(state, index);
Eric Biederman90089602004-05-28 14:11:54 +00005056 while (mpeek(state, index) == TOK_OR) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005057 long_t right;
5058 meat(state, index, TOK_OR);
5059 right = mxor_expr(state, index);
5060 val = val | right;
5061 }
5062 return val;
5063}
5064
5065static long_t mland_expr(struct compile_state *state, int index)
5066{
5067 long_t val;
5068 val = mor_expr(state, index);
Eric Biederman90089602004-05-28 14:11:54 +00005069 while (mpeek(state, index) == TOK_LOGAND) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005070 long_t right;
5071 meat(state, index, TOK_LOGAND);
5072 right = mor_expr(state, index);
5073 val = val && right;
5074 }
5075 return val;
5076}
5077static long_t mlor_expr(struct compile_state *state, int index)
5078{
5079 long_t val;
5080 val = mland_expr(state, index);
Eric Biederman90089602004-05-28 14:11:54 +00005081 while (mpeek(state, index) == TOK_LOGOR) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005082 long_t right;
5083 meat(state, index, TOK_LOGOR);
5084 right = mland_expr(state, index);
5085 val = val || right;
5086 }
5087 return val;
5088}
5089
5090static long_t mcexpr(struct compile_state *state, int index)
5091{
5092 return mlor_expr(state, index);
5093}
Eric Biederman90089602004-05-28 14:11:54 +00005094
5095static void eat_tokens(struct compile_state *state, int targ_tok)
5096{
5097 if (state->eat_depth > 0) {
5098 internal_error(state, 0, "Already eating...");
5099 }
5100 state->eat_depth = state->if_depth;
5101 state->eat_targ = targ_tok;
5102}
5103static int if_eat(struct compile_state *state)
5104{
5105 return state->eat_depth > 0;
5106}
5107static int if_value(struct compile_state *state)
5108{
5109 int index, offset;
5110 index = state->if_depth / CHAR_BIT;
5111 offset = state->if_depth % CHAR_BIT;
5112 return !!(state->if_bytes[index] & (1 << (offset)));
5113}
5114static void set_if_value(struct compile_state *state, int value)
5115{
5116 int index, offset;
5117 index = state->if_depth / CHAR_BIT;
5118 offset = state->if_depth % CHAR_BIT;
5119
5120 state->if_bytes[index] &= ~(1 << offset);
5121 if (value) {
5122 state->if_bytes[index] |= (1 << offset);
5123 }
5124}
5125static void in_if(struct compile_state *state, const char *name)
5126{
5127 if (state->if_depth <= 0) {
5128 error(state, 0, "%s without #if", name);
5129 }
5130}
5131static void enter_if(struct compile_state *state)
5132{
5133 state->if_depth += 1;
5134 if (state->if_depth > MAX_CPP_IF_DEPTH) {
5135 error(state, 0, "#if depth too great");
5136 }
5137}
5138static void reenter_if(struct compile_state *state, const char *name)
5139{
5140 in_if(state, name);
5141 if ((state->eat_depth == state->if_depth) &&
5142 (state->eat_targ == TOK_ELSE)) {
5143 state->eat_depth = 0;
5144 state->eat_targ = 0;
5145 }
5146}
5147static void enter_else(struct compile_state *state, const char *name)
5148{
5149 in_if(state, name);
5150 if ((state->eat_depth == state->if_depth) &&
5151 (state->eat_targ == TOK_ELSE)) {
5152 state->eat_depth = 0;
5153 state->eat_targ = 0;
5154 }
5155}
5156static void exit_if(struct compile_state *state, const char *name)
5157{
5158 in_if(state, name);
5159 if (state->eat_depth == state->if_depth) {
5160 state->eat_depth = 0;
5161 state->eat_targ = 0;
5162 }
5163 state->if_depth -= 1;
5164}
5165
Eric Biedermanb138ac82003-04-22 18:44:01 +00005166static void preprocess(struct compile_state *state, int index)
5167{
5168 /* Doing much more with the preprocessor would require
5169 * a parser and a major restructuring.
5170 * Postpone that for later.
5171 */
5172 struct file_state *file;
5173 struct token *tk;
5174 int line;
5175 int tok;
5176
5177 file = state->file;
5178 tk = &state->token[index];
5179 state->macro_line = line = file->line;
5180 state->macro_file = file;
5181
Eric Biederman90089602004-05-28 14:11:54 +00005182 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005183 ident_to_macro(state, tk);
5184 if (tk->tok == TOK_IDENT) {
5185 error(state, 0, "undefined preprocessing directive `%s'",
5186 tk->ident->name);
5187 }
5188 switch(tk->tok) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005189 case TOK_LIT_INT:
5190 {
5191 int override_line;
5192 override_line = strtoul(tk->val.str, 0, 10);
Eric Biederman90089602004-05-28 14:11:54 +00005193 next_token(state, tk);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005194 /* I have a cpp line marker parse it */
5195 if (tk->tok == TOK_LIT_STRING) {
5196 const char *token, *base;
5197 char *name, *dir;
5198 int name_len, dir_len;
5199 name = xmalloc(tk->str_len, "report_name");
5200 token = tk->val.str + 1;
5201 base = strrchr(token, '/');
5202 name_len = tk->str_len -2;
5203 if (base != 0) {
5204 dir_len = base - token;
5205 base++;
5206 name_len -= base - token;
5207 } else {
5208 dir_len = 0;
5209 base = token;
5210 }
5211 memcpy(name, base, name_len);
5212 name[name_len] = '\0';
5213 dir = xmalloc(dir_len + 1, "report_dir");
5214 memcpy(dir, token, dir_len);
5215 dir[dir_len] = '\0';
5216 file->report_line = override_line - 1;
5217 file->report_name = name;
5218 file->report_dir = dir;
5219 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005220 break;
Eric Biederman90089602004-05-28 14:11:54 +00005221 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005222 case TOK_LINE:
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005223 meat(state, index, TOK_LIT_INT);
5224 file->report_line = strtoul(tk->val.str, 0, 10) -1;
5225 if (mpeek(state, index) == TOK_LIT_STRING) {
5226 const char *token, *base;
5227 char *name, *dir;
5228 int name_len, dir_len;
5229 meat(state, index, TOK_LIT_STRING);
5230 name = xmalloc(tk->str_len, "report_name");
5231 token = tk->val.str + 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005232 base = strrchr(token, '/');
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005233 name_len = tk->str_len - 2;
5234 if (base != 0) {
5235 dir_len = base - token;
5236 base++;
5237 name_len -= base - token;
5238 } else {
5239 dir_len = 0;
5240 base = token;
5241 }
5242 memcpy(name, base, name_len);
5243 name[name_len] = '\0';
5244 dir = xmalloc(dir_len + 1, "report_dir");
5245 memcpy(dir, token, dir_len);
5246 dir[dir_len] = '\0';
5247 file->report_name = name;
5248 file->report_dir = dir;
5249 }
5250 break;
5251 case TOK_UNDEF:
Eric Biederman90089602004-05-28 14:11:54 +00005252 {
5253 struct hash_entry *ident;
5254 if (if_eat(state)) /* quit early when #if'd out */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005255 break;
Eric Biederman90089602004-05-28 14:11:54 +00005256
5257 meat_raw(state, index, TOK_IDENT);
5258 ident = tk->ident;
5259
5260 undef_macro(state, ident);
5261 break;
5262 }
5263 case TOK_PRAGMA:
5264 if (if_eat(state)) /* quit early when #if'd out */
5265 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005266 warning(state, 0, "Ignoring preprocessor directive: %s",
5267 tk->ident->name);
5268 break;
5269 case TOK_ELIF:
Eric Biederman90089602004-05-28 14:11:54 +00005270 reenter_if(state, "#elif");
5271 if (if_eat(state)) /* quit early when #if'd out */
5272 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005273 /* If the #if was taken the #elif just disables the following code */
Eric Biederman90089602004-05-28 14:11:54 +00005274 if (if_value(state)) {
5275 eat_tokens(state, TOK_ENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005276 }
5277 /* If the previous #if was not taken see if the #elif enables the
5278 * trailing code.
5279 */
Eric Biederman90089602004-05-28 14:11:54 +00005280 else {
5281 set_if_value(state, mcexpr(state, index) != 0);
5282 if (!if_value(state)) {
5283 eat_tokens(state, TOK_ELSE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005284 }
5285 }
5286 break;
5287 case TOK_IF:
Eric Biederman90089602004-05-28 14:11:54 +00005288 enter_if(state);
5289 if (if_eat(state)) /* quit early when #if'd out */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005290 break;
Eric Biederman90089602004-05-28 14:11:54 +00005291 set_if_value(state, mcexpr(state, index) != 0);
5292 if (!if_value(state)) {
5293 eat_tokens(state, TOK_ELSE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005294 }
5295 break;
5296 case TOK_IFNDEF:
Eric Biederman90089602004-05-28 14:11:54 +00005297 enter_if(state);
5298 if (if_eat(state)) /* quit early when #if'd out */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005299 break;
Eric Biederman90089602004-05-28 14:11:54 +00005300 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005301 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
5302 error(state, 0, "Invalid macro name");
5303 }
Eric Biederman90089602004-05-28 14:11:54 +00005304 set_if_value(state, tk->ident->sym_define == 0);
5305 if (!if_value(state)) {
5306 eat_tokens(state, TOK_ELSE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005307 }
5308 break;
5309 case TOK_IFDEF:
Eric Biederman90089602004-05-28 14:11:54 +00005310 enter_if(state);
5311 if (if_eat(state)) /* quit early when #if'd out */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005312 break;
Eric Biederman90089602004-05-28 14:11:54 +00005313 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005314 if ((line != file->line) || (tk->tok != TOK_IDENT)) {
5315 error(state, 0, "Invalid macro name");
5316 }
Eric Biederman90089602004-05-28 14:11:54 +00005317 set_if_value(state, tk->ident->sym_define != 0);
5318 if (!if_value(state)) {
5319 eat_tokens(state, TOK_ELSE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005320 }
5321 break;
5322 case TOK_ELSE:
Eric Biederman90089602004-05-28 14:11:54 +00005323 enter_else(state, "#else");
5324 if (!if_eat(state) && if_value(state)) {
5325 eat_tokens(state, TOK_ENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005326 }
5327 break;
5328 case TOK_ENDIF:
Eric Biederman90089602004-05-28 14:11:54 +00005329 exit_if(state, "#endif");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005330 break;
5331 case TOK_DEFINE:
5332 {
5333 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00005334 struct macro_arg *args, **larg;
5335 const char *start, *mstart, *ptr;
5336
5337 if (if_eat(state)) /* quit early when #if'd out */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005338 break;
5339
Eric Biederman90089602004-05-28 14:11:54 +00005340 meat_raw(state, index, TOK_IDENT);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005341 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00005342 args = 0;
5343 larg = &args;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005344
Eric Biederman90089602004-05-28 14:11:54 +00005345 /* Remember the start of the macro */
5346 start = file->pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005347
Eric Biederman90089602004-05-28 14:11:54 +00005348 /* Find the end of the line. */
5349 for(ptr = start; *ptr != '\n'; ptr++)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005350 ;
5351
Eric Biederman90089602004-05-28 14:11:54 +00005352 /* remove the trailing whitespace */
5353 while(spacep(*ptr)) {
5354 ptr--;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005355 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005356
Eric Biederman90089602004-05-28 14:11:54 +00005357 /* Remove leading whitespace */
5358 while(spacep(*start) && (start < ptr)) {
5359 start++;
5360 }
5361 /* Remember where the macro starts */
5362 mstart = start;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005363
Eric Biederman90089602004-05-28 14:11:54 +00005364 /* Parse macro parameters */
5365 if (lparen_peek(state, state->file)) {
5366 meat_raw(state, index, TOK_LPAREN);
5367
5368 for(;;) {
5369 struct macro_arg *narg, *arg;
5370 struct hash_entry *aident;
5371 int tok;
5372
5373 tok = mpeek_raw(state, index);
5374 if (!args && (tok == TOK_RPAREN)) {
5375 break;
5376 }
5377 else if (tok == TOK_DOTS) {
5378 meat_raw(state, index, TOK_DOTS);
5379 aident = state->i___VA_ARGS__;
5380 }
5381 else {
5382 meat_raw(state, index, TOK_IDENT);
5383 aident = tk->ident;
5384 }
5385
5386 narg = xcmalloc(sizeof(*arg), "macro arg");
5387 narg->ident = aident;
5388
5389 /* Verify I don't have a duplicate identifier */
5390 for(arg = args; arg; arg = arg->next) {
5391 if (arg->ident == narg->ident) {
5392 error(state, 0, "Duplicate macro arg `%s'",
5393 narg->ident->name);
5394 }
5395 }
5396 /* Add the new argument to the end of the list */
5397 *larg = narg;
5398 larg = &narg->next;
5399
5400 if ((aident == state->i___VA_ARGS__) ||
5401 (mpeek(state, index) != TOK_COMMA)) {
5402 break;
5403 }
5404 meat_raw(state, index, TOK_COMMA);
5405 }
5406 meat_raw(state, index, TOK_RPAREN);
5407
5408 /* Get the start of the macro body */
5409 mstart = file->pos;
5410
5411 /* Remove leading whitespace */
5412 while(spacep(*mstart) && (mstart < ptr)) {
5413 mstart++;
5414 }
5415 }
5416 define_macro(state, ident, start, ptr - start + 1,
5417 mstart - start, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005418 break;
5419 }
5420 case TOK_ERROR:
5421 {
Eric Biederman90089602004-05-28 14:11:54 +00005422 const char *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005423 int len;
Eric Biederman90089602004-05-28 14:11:54 +00005424
Eric Biedermanb138ac82003-04-22 18:44:01 +00005425 /* Find the end of the line */
5426 for(end = file->pos; *end != '\n'; end++)
5427 ;
5428 len = (end - file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00005429 if (!if_eat(state)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005430 error(state, 0, "%*.*s", len, len, file->pos);
5431 }
5432 file->pos = end;
5433 break;
5434 }
5435 case TOK_WARNING:
5436 {
Eric Biederman90089602004-05-28 14:11:54 +00005437 const char *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005438 int len;
Eric Biederman90089602004-05-28 14:11:54 +00005439
Eric Biedermanb138ac82003-04-22 18:44:01 +00005440 /* Find the end of the line */
5441 for(end = file->pos; *end != '\n'; end++)
5442 ;
5443 len = (end - file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00005444 if (!if_eat(state)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005445 warning(state, 0, "%*.*s", len, len, file->pos);
5446 }
5447 file->pos = end;
5448 break;
5449 }
5450 case TOK_INCLUDE:
5451 {
5452 char *name;
Eric Biederman90089602004-05-28 14:11:54 +00005453 const char *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005454 int local;
5455 local = 0;
5456 name = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005457 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005458 if (tk->tok == TOK_LIT_STRING) {
5459 const char *token;
5460 int name_len;
5461 name = xmalloc(tk->str_len, "include");
5462 token = tk->val.str +1;
5463 name_len = tk->str_len -2;
5464 if (*token == '"') {
5465 token++;
5466 name_len--;
5467 }
5468 memcpy(name, token, name_len);
5469 name[name_len] = '\0';
5470 local = 1;
5471 }
5472 else if (tk->tok == TOK_LESS) {
Eric Biederman90089602004-05-28 14:11:54 +00005473 const char *start, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005474 start = file->pos;
5475 for(end = start; *end != '\n'; end++) {
5476 if (*end == '>') {
5477 break;
5478 }
5479 }
5480 if (*end == '\n') {
5481 error(state, 0, "Unterminated included directive");
5482 }
5483 name = xmalloc(end - start + 1, "include");
5484 memcpy(name, start, end - start);
5485 name[end - start] = '\0';
5486 file->pos = end +1;
5487 local = 0;
5488 }
5489 else {
5490 error(state, 0, "Invalid include directive");
5491 }
5492 /* Error if there are any characters after the include */
5493 for(ptr = file->pos; *ptr != '\n'; ptr++) {
Eric Biederman8d9c1232003-06-17 08:42:17 +00005494 switch(*ptr) {
5495 case ' ':
5496 case '\t':
5497 case '\v':
5498 break;
5499 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005500 error(state, 0, "garbage after include directive");
5501 }
5502 }
Eric Biederman90089602004-05-28 14:11:54 +00005503 if (!if_eat(state)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005504 compile_file(state, name, local);
5505 }
5506 xfree(name);
Eric Biederman90089602004-05-28 14:11:54 +00005507 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005508 return;
5509 }
5510 default:
5511 /* Ignore # without a following ident */
5512 if (tk->tok == TOK_IDENT) {
5513 error(state, 0, "Invalid preprocessor directive: %s",
5514 tk->ident->name);
5515 }
5516 break;
5517 }
5518 /* Consume the rest of the macro line */
5519 do {
Eric Biederman90089602004-05-28 14:11:54 +00005520 tok = mpeek_raw(state, index);
5521 meat_raw(state, index, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005522 } while(tok != TOK_EOF);
5523 return;
5524}
5525
5526static void token(struct compile_state *state, int index)
5527{
5528 struct file_state *file;
5529 struct token *tk;
5530 int rescan;
5531
5532 tk = &state->token[index];
Eric Biederman90089602004-05-28 14:11:54 +00005533 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005534 do {
5535 rescan = 0;
5536 file = state->file;
5537 if (tk->tok == TOK_EOF && file->prev) {
5538 state->file = file->prev;
5539 /* file->basename is used keep it */
5540 xfree(file->dirname);
5541 xfree(file->buf);
5542 xfree(file);
Eric Biederman90089602004-05-28 14:11:54 +00005543 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005544 rescan = 1;
5545 }
5546 else if (tk->tok == TOK_MACRO) {
5547 preprocess(state, index);
5548 rescan = 1;
5549 }
5550 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00005551 rescan = compile_macro(state, &state->file, tk);
5552 if (rescan) {
5553 next_token(state, tk);
5554 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005555 }
Eric Biederman90089602004-05-28 14:11:54 +00005556 else if (if_eat(state)) {
5557 next_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005558 rescan = 1;
5559 }
5560 } while(rescan);
5561}
5562
5563static int peek(struct compile_state *state)
5564{
5565 if (state->token[1].tok == -1) {
5566 token(state, 1);
5567 }
5568 return state->token[1].tok;
5569}
5570
5571static int peek2(struct compile_state *state)
5572{
5573 if (state->token[1].tok == -1) {
5574 token(state, 1);
5575 }
5576 if (state->token[2].tok == -1) {
5577 token(state, 2);
5578 }
5579 return state->token[2].tok;
5580}
5581
Eric Biederman0babc1c2003-05-09 02:39:00 +00005582static void eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005583{
Eric Biedermanb138ac82003-04-22 18:44:01 +00005584 int i;
Eric Biederman90089602004-05-28 14:11:54 +00005585 peek(state);
5586 check_tok(state, &state->token[1], tok);
5587
Eric Biedermanb138ac82003-04-22 18:44:01 +00005588 /* Free the old token value */
5589 if (state->token[0].str_len) {
5590 xfree((void *)(state->token[0].val.str));
5591 }
5592 for(i = 0; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
5593 state->token[i] = state->token[i + 1];
5594 }
5595 memset(&state->token[i], 0, sizeof(state->token[i]));
5596 state->token[i].tok = -1;
5597}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005598
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005599static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005600{
Eric Biederman90089602004-05-28 14:11:54 +00005601 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005602 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005603 int subdir_len;
5604 struct file_state *file;
5605 char *basename;
5606 file = xmalloc(sizeof(*file), "file_state");
5607
5608 base = strrchr(filename, '/');
5609 subdir = filename;
5610 if (base != 0) {
5611 subdir_len = base - filename;
5612 base++;
5613 }
5614 else {
5615 base = filename;
5616 subdir_len = 0;
5617 }
5618 basename = xmalloc(strlen(base) +1, "basename");
5619 strcpy(basename, base);
5620 file->basename = basename;
5621
5622 if (getcwd(cwd, sizeof(cwd)) == 0) {
5623 die("cwd buffer to small");
5624 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005625 if (subdir[0] == '/') {
5626 file->dirname = xmalloc(subdir_len + 1, "dirname");
5627 memcpy(file->dirname, subdir, subdir_len);
5628 file->dirname[subdir_len] = '\0';
5629 }
5630 else {
Eric Biederman90089602004-05-28 14:11:54 +00005631 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005632 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005633 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005634 /* Find the appropriate directory... */
5635 dir = 0;
5636 if (!state->file && exists(cwd, filename)) {
5637 dir = cwd;
5638 }
5639 if (local && state->file && exists(state->file->dirname, filename)) {
5640 dir = state->file->dirname;
5641 }
Eric Biederman90089602004-05-28 14:11:54 +00005642 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005643 if (exists(*path, filename)) {
5644 dir = *path;
5645 }
5646 }
5647 if (!dir) {
5648 error(state, 0, "Cannot find `%s'\n", filename);
5649 }
5650 dirlen = strlen(dir);
5651 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5652 memcpy(file->dirname, dir, dirlen);
5653 file->dirname[dirlen] = '/';
5654 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5655 file->dirname[dirlen + 1 + subdir_len] = '\0';
5656 }
5657 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005658
5659 file->pos = file->buf;
5660 file->line_start = file->pos;
5661 file->line = 1;
5662
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005663 file->report_line = 1;
5664 file->report_name = file->basename;
5665 file->report_dir = file->dirname;
5666
Eric Biedermanb138ac82003-04-22 18:44:01 +00005667 file->prev = state->file;
5668 state->file = file;
5669
5670 process_trigraphs(state);
5671 splice_lines(state);
5672}
5673
Eric Biederman0babc1c2003-05-09 02:39:00 +00005674/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005675
5676static struct type *new_type(
5677 unsigned int type, struct type *left, struct type *right)
5678{
5679 struct type *result;
5680 result = xmalloc(sizeof(*result), "type");
5681 result->type = type;
5682 result->left = left;
5683 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005684 result->field_ident = 0;
5685 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005686 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005687 return result;
5688}
5689
5690static struct type *clone_type(unsigned int specifiers, struct type *old)
5691{
5692 struct type *result;
5693 result = xmalloc(sizeof(*result), "type");
5694 memcpy(result, old, sizeof(*result));
5695 result->type &= TYPE_MASK;
5696 result->type |= specifiers;
5697 return result;
5698}
5699
Eric Biederman90089602004-05-28 14:11:54 +00005700static struct type *dup_type(struct compile_state *state, struct type *orig)
5701{
5702 struct type *new;
5703 new = xcmalloc(sizeof(*new), "type");
5704 new->type = orig->type;
5705 new->field_ident = orig->field_ident;
5706 new->type_ident = orig->type_ident;
5707 new->elements = orig->elements;
5708 if (orig->left) {
5709 new->left = dup_type(state, orig->left);
5710 }
5711 if (orig->right) {
5712 new->right = dup_type(state, orig->right);
5713 }
5714 return new;
5715}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005716
Eric Biederman90089602004-05-28 14:11:54 +00005717
5718static struct type *invalid_type(struct compile_state *state, struct type *type)
5719{
5720 struct type *invalid, *member;
5721 invalid = 0;
5722 if (!type) {
5723 internal_error(state, 0, "type missing?");
5724 }
5725 switch(type->type & TYPE_MASK) {
5726 case TYPE_VOID:
5727 case TYPE_CHAR: case TYPE_UCHAR:
5728 case TYPE_SHORT: case TYPE_USHORT:
5729 case TYPE_INT: case TYPE_UINT:
5730 case TYPE_LONG: case TYPE_ULONG:
5731 case TYPE_LLONG: case TYPE_ULLONG:
5732 case TYPE_POINTER:
5733 case TYPE_ENUM:
5734 break;
5735 case TYPE_BITFIELD:
5736 invalid = invalid_type(state, type->left);
5737 break;
5738 case TYPE_ARRAY:
5739 invalid = invalid_type(state, type->left);
5740 break;
5741 case TYPE_STRUCT:
5742 case TYPE_TUPLE:
5743 member = type->left;
5744 while(member && (invalid == 0) &&
5745 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5746 invalid = invalid_type(state, member->left);
5747 member = member->right;
5748 }
5749 if (!invalid) {
5750 invalid = invalid_type(state, member);
5751 }
5752 break;
5753 case TYPE_UNION:
5754 case TYPE_JOIN:
5755 member = type->left;
5756 while(member && (invalid == 0) &&
5757 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5758 invalid = invalid_type(state, member->left);
5759 member = member->right;
5760 }
5761 if (!invalid) {
5762 invalid = invalid_type(state, member);
5763 }
5764 break;
5765 default:
5766 invalid = type;
5767 break;
5768 }
5769 return invalid;
5770
5771}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005772
5773#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005774#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005775static inline ulong_t mask_uint(ulong_t x)
5776{
5777 if (SIZEOF_INT < SIZEOF_LONG) {
Eric Biederman90089602004-05-28 14:11:54 +00005778 ulong_t mask = (((ulong_t)1) << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005779 x &= mask;
5780 }
5781 return x;
5782}
5783#define MASK_UINT(X) (mask_uint(X))
5784#define MASK_ULONG(X) (X)
5785
Eric Biederman90089602004-05-28 14:11:54 +00005786static struct type void_type = { .type = TYPE_VOID };
5787static struct type char_type = { .type = TYPE_CHAR };
5788static struct type uchar_type = { .type = TYPE_UCHAR };
5789static struct type short_type = { .type = TYPE_SHORT };
5790static struct type ushort_type = { .type = TYPE_USHORT };
5791static struct type int_type = { .type = TYPE_INT };
5792static struct type uint_type = { .type = TYPE_UINT };
5793static struct type long_type = { .type = TYPE_LONG };
5794static struct type ulong_type = { .type = TYPE_ULONG };
5795static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005796
Eric Biederman5ade04a2003-10-22 04:03:46 +00005797static struct type void_ptr_type = {
5798 .type = TYPE_POINTER,
5799 .left = &void_type,
5800};
5801
5802static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005803 .type = TYPE_FUNCTION,
5804 .left = &void_type,
5805 .right = &void_type,
5806};
5807
Eric Biederman90089602004-05-28 14:11:54 +00005808static size_t bits_to_bytes(size_t size)
5809{
5810 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5811}
5812
Eric Biedermanb138ac82003-04-22 18:44:01 +00005813static struct triple *variable(struct compile_state *state, struct type *type)
5814{
5815 struct triple *result;
5816 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005817 result = triple(state, OP_ADECL, type, 0, 0);
5818 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005819 }
5820 else {
5821 result = triple(state, OP_SDECL, type, 0, 0);
5822 }
5823 return result;
5824}
5825
5826static void stor_of(FILE *fp, struct type *type)
5827{
5828 switch(type->type & STOR_MASK) {
5829 case STOR_AUTO:
5830 fprintf(fp, "auto ");
5831 break;
5832 case STOR_STATIC:
5833 fprintf(fp, "static ");
5834 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005835 case STOR_LOCAL:
5836 fprintf(fp, "local ");
5837 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 case STOR_EXTERN:
5839 fprintf(fp, "extern ");
5840 break;
5841 case STOR_REGISTER:
5842 fprintf(fp, "register ");
5843 break;
5844 case STOR_TYPEDEF:
5845 fprintf(fp, "typedef ");
5846 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005847 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005848 fprintf(fp, "inline ");
5849 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005850 case STOR_INLINE | STOR_STATIC:
5851 fprintf(fp, "static inline");
5852 break;
5853 case STOR_INLINE | STOR_EXTERN:
5854 fprintf(fp, "extern inline");
5855 break;
5856 default:
5857 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5858 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005859 }
5860}
5861static void qual_of(FILE *fp, struct type *type)
5862{
5863 if (type->type & QUAL_CONST) {
5864 fprintf(fp, " const");
5865 }
5866 if (type->type & QUAL_VOLATILE) {
5867 fprintf(fp, " volatile");
5868 }
5869 if (type->type & QUAL_RESTRICT) {
5870 fprintf(fp, " restrict");
5871 }
5872}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005873
Eric Biedermanb138ac82003-04-22 18:44:01 +00005874static void name_of(FILE *fp, struct type *type)
5875{
Eric Biederman90089602004-05-28 14:11:54 +00005876 unsigned int base_type;
5877 base_type = type->type & TYPE_MASK;
5878 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5879 stor_of(fp, type);
5880 }
5881 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005882 case TYPE_VOID:
5883 fprintf(fp, "void");
5884 qual_of(fp, type);
5885 break;
5886 case TYPE_CHAR:
5887 fprintf(fp, "signed char");
5888 qual_of(fp, type);
5889 break;
5890 case TYPE_UCHAR:
5891 fprintf(fp, "unsigned char");
5892 qual_of(fp, type);
5893 break;
5894 case TYPE_SHORT:
5895 fprintf(fp, "signed short");
5896 qual_of(fp, type);
5897 break;
5898 case TYPE_USHORT:
5899 fprintf(fp, "unsigned short");
5900 qual_of(fp, type);
5901 break;
5902 case TYPE_INT:
5903 fprintf(fp, "signed int");
5904 qual_of(fp, type);
5905 break;
5906 case TYPE_UINT:
5907 fprintf(fp, "unsigned int");
5908 qual_of(fp, type);
5909 break;
5910 case TYPE_LONG:
5911 fprintf(fp, "signed long");
5912 qual_of(fp, type);
5913 break;
5914 case TYPE_ULONG:
5915 fprintf(fp, "unsigned long");
5916 qual_of(fp, type);
5917 break;
5918 case TYPE_POINTER:
5919 name_of(fp, type->left);
5920 fprintf(fp, " * ");
5921 qual_of(fp, type);
5922 break;
5923 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005924 name_of(fp, type->left);
5925 fprintf(fp, ", ");
5926 name_of(fp, type->right);
5927 break;
Eric Biederman90089602004-05-28 14:11:54 +00005928 case TYPE_OVERLAP:
5929 name_of(fp, type->left);
5930 fprintf(fp, ",| ");
5931 name_of(fp, type->right);
5932 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005933 case TYPE_ENUM:
Eric Biederman90089602004-05-28 14:11:54 +00005934 fprintf(fp, "enum %s",
5935 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005936 qual_of(fp, type);
5937 break;
5938 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005939 fprintf(fp, "struct %s { ",
5940 (type->type_ident)? type->type_ident->name : "");
5941 name_of(fp, type->left);
5942 fprintf(fp, " } ");
5943 qual_of(fp, type);
5944 break;
5945 case TYPE_UNION:
5946 fprintf(fp, "union %s { ",
5947 (type->type_ident)? type->type_ident->name : "");
5948 name_of(fp, type->left);
5949 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005950 qual_of(fp, type);
5951 break;
5952 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005953 name_of(fp, type->left);
5954 fprintf(fp, " (*)(");
5955 name_of(fp, type->right);
5956 fprintf(fp, ")");
5957 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005958 case TYPE_ARRAY:
5959 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005960 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005961 break;
Eric Biederman90089602004-05-28 14:11:54 +00005962 case TYPE_TUPLE:
5963 fprintf(fp, "tuple { ");
5964 name_of(fp, type->left);
5965 fprintf(fp, " } ");
5966 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005967 break;
Eric Biederman90089602004-05-28 14:11:54 +00005968 case TYPE_JOIN:
5969 fprintf(fp, "join { ");
5970 name_of(fp, type->left);
5971 fprintf(fp, " } ");
5972 qual_of(fp, type);
5973 break;
5974 case TYPE_BITFIELD:
5975 name_of(fp, type->left);
5976 fprintf(fp, " : %d ", type->elements);
5977 qual_of(fp, type);
5978 break;
5979 case TYPE_UNKNOWN:
5980 fprintf(fp, "unknown_t");
5981 break;
5982 default:
5983 fprintf(fp, "????: %x", base_type);
5984 break;
5985 }
5986 if (type->field_ident && type->field_ident->name) {
5987 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005988 }
5989}
5990
5991static size_t align_of(struct compile_state *state, struct type *type)
5992{
5993 size_t align;
5994 align = 0;
5995 switch(type->type & TYPE_MASK) {
5996 case TYPE_VOID:
5997 align = 1;
5998 break;
Eric Biederman90089602004-05-28 14:11:54 +00005999 case TYPE_BITFIELD:
6000 align = 1;
6001 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006002 case TYPE_CHAR:
6003 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006004 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006005 break;
6006 case TYPE_SHORT:
6007 case TYPE_USHORT:
6008 align = ALIGNOF_SHORT;
6009 break;
6010 case TYPE_INT:
6011 case TYPE_UINT:
6012 case TYPE_ENUM:
6013 align = ALIGNOF_INT;
6014 break;
6015 case TYPE_LONG:
6016 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006017 align = ALIGNOF_LONG;
6018 break;
Eric Biederman90089602004-05-28 14:11:54 +00006019 case TYPE_POINTER:
6020 align = ALIGNOF_POINTER;
6021 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006022 case TYPE_PRODUCT:
6023 case TYPE_OVERLAP:
6024 {
6025 size_t left_align, right_align;
6026 left_align = align_of(state, type->left);
6027 right_align = align_of(state, type->right);
6028 align = (left_align >= right_align) ? left_align : right_align;
6029 break;
6030 }
6031 case TYPE_ARRAY:
6032 align = align_of(state, type->left);
6033 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006034 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006035 case TYPE_TUPLE:
6036 case TYPE_UNION:
6037 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00006038 align = align_of(state, type->left);
6039 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006040 default:
6041 error(state, 0, "alignof not yet defined for type\n");
6042 break;
6043 }
6044 return align;
6045}
6046
Eric Biederman90089602004-05-28 14:11:54 +00006047static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00006048{
Eric Biederman90089602004-05-28 14:11:54 +00006049 size_t align;
6050 align = 0;
6051 switch(type->type & TYPE_MASK) {
6052 case TYPE_VOID:
6053 align = 1;
6054 break;
6055 case TYPE_BITFIELD:
6056 align = 1;
6057 break;
6058 case TYPE_CHAR:
6059 case TYPE_UCHAR:
6060 align = REG_ALIGNOF_CHAR;
6061 break;
6062 case TYPE_SHORT:
6063 case TYPE_USHORT:
6064 align = REG_ALIGNOF_SHORT;
6065 break;
6066 case TYPE_INT:
6067 case TYPE_UINT:
6068 case TYPE_ENUM:
6069 align = REG_ALIGNOF_INT;
6070 break;
6071 case TYPE_LONG:
6072 case TYPE_ULONG:
6073 align = REG_ALIGNOF_LONG;
6074 break;
6075 case TYPE_POINTER:
6076 align = REG_ALIGNOF_POINTER;
6077 break;
6078 case TYPE_PRODUCT:
6079 case TYPE_OVERLAP:
6080 {
6081 size_t left_align, right_align;
6082 left_align = reg_align_of(state, type->left);
6083 right_align = reg_align_of(state, type->right);
6084 align = (left_align >= right_align) ? left_align : right_align;
6085 break;
6086 }
6087 case TYPE_ARRAY:
6088 align = reg_align_of(state, type->left);
6089 break;
6090 case TYPE_STRUCT:
6091 case TYPE_UNION:
6092 case TYPE_TUPLE:
6093 case TYPE_JOIN:
6094 align = reg_align_of(state, type->left);
6095 break;
6096 default:
6097 error(state, 0, "alignof not yet defined for type\n");
6098 break;
6099 }
6100 return align;
6101}
6102
6103static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
6104{
6105 return bits_to_bytes(align_of(state, type));
6106}
6107static size_t size_of(struct compile_state *state, struct type *type);
6108static size_t reg_size_of(struct compile_state *state, struct type *type);
6109
6110static size_t needed_padding(struct compile_state *state,
6111 struct type *type, size_t offset)
6112{
6113 size_t padding, align;
6114 align = align_of(state, type);
6115 /* Align to the next machine word if the bitfield does completely
6116 * fit into the current word.
6117 */
6118 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6119 size_t size;
6120 size = size_of(state, type);
6121 if ((offset + type->elements)/size != offset/size) {
6122 align = size;
6123 }
6124 }
Eric Biederman03b59862003-06-24 14:27:37 +00006125 padding = 0;
6126 if (offset % align) {
6127 padding = align - (offset % align);
6128 }
6129 return padding;
6130}
Eric Biederman90089602004-05-28 14:11:54 +00006131
6132static size_t reg_needed_padding(struct compile_state *state,
6133 struct type *type, size_t offset)
6134{
6135 size_t padding, align;
6136 align = reg_align_of(state, type);
6137 /* Align to the next register word if the bitfield does completely
6138 * fit into the current register.
6139 */
6140 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
6141 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
6142 {
6143 align = REG_SIZEOF_REG;
6144 }
6145 padding = 0;
6146 if (offset % align) {
6147 padding = align - (offset % align);
6148 }
6149 return padding;
6150}
6151
Eric Biedermanb138ac82003-04-22 18:44:01 +00006152static size_t size_of(struct compile_state *state, struct type *type)
6153{
6154 size_t size;
6155 size = 0;
6156 switch(type->type & TYPE_MASK) {
6157 case TYPE_VOID:
6158 size = 0;
6159 break;
Eric Biederman90089602004-05-28 14:11:54 +00006160 case TYPE_BITFIELD:
6161 size = type->elements;
6162 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006163 case TYPE_CHAR:
6164 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006165 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006166 break;
6167 case TYPE_SHORT:
6168 case TYPE_USHORT:
6169 size = SIZEOF_SHORT;
6170 break;
6171 case TYPE_INT:
6172 case TYPE_UINT:
6173 case TYPE_ENUM:
6174 size = SIZEOF_INT;
6175 break;
6176 case TYPE_LONG:
6177 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006178 size = SIZEOF_LONG;
6179 break;
Eric Biederman90089602004-05-28 14:11:54 +00006180 case TYPE_POINTER:
6181 size = SIZEOF_POINTER;
6182 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006183 case TYPE_PRODUCT:
6184 {
Eric Biederman90089602004-05-28 14:11:54 +00006185 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006186 size = 0;
6187 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006188 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006189 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006190 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006191 }
Eric Biederman90089602004-05-28 14:11:54 +00006192 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006193 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006194 break;
6195 }
6196 case TYPE_OVERLAP:
6197 {
6198 size_t size_left, size_right;
6199 size_left = size_of(state, type->left);
6200 size_right = size_of(state, type->right);
6201 size = (size_left >= size_right)? size_left : size_right;
6202 break;
6203 }
6204 case TYPE_ARRAY:
6205 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6206 internal_error(state, 0, "Invalid array type");
6207 } else {
6208 size = size_of(state, type->left) * type->elements;
6209 }
6210 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006211 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006212 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006213 {
Eric Biederman90089602004-05-28 14:11:54 +00006214 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006215 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006216 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006217 pad = needed_padding(state, type, size);
6218 size = size + pad;
6219 break;
6220 }
6221 case TYPE_UNION:
6222 case TYPE_JOIN:
6223 {
6224 size_t pad;
6225 size = size_of(state, type->left);
6226 /* Pad unions so their size is a multiple of their alignment */
6227 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006228 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006229 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006230 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006231 default:
Eric Biederman90089602004-05-28 14:11:54 +00006232 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006233 break;
6234 }
6235 return size;
6236}
6237
Eric Biederman90089602004-05-28 14:11:54 +00006238static size_t reg_size_of(struct compile_state *state, struct type *type)
6239{
6240 size_t size;
6241 size = 0;
6242 switch(type->type & TYPE_MASK) {
6243 case TYPE_VOID:
6244 size = 0;
6245 break;
6246 case TYPE_BITFIELD:
6247 size = type->elements;
6248 break;
6249 case TYPE_CHAR:
6250 case TYPE_UCHAR:
6251 size = REG_SIZEOF_CHAR;
6252 break;
6253 case TYPE_SHORT:
6254 case TYPE_USHORT:
6255 size = REG_SIZEOF_SHORT;
6256 break;
6257 case TYPE_INT:
6258 case TYPE_UINT:
6259 case TYPE_ENUM:
6260 size = REG_SIZEOF_INT;
6261 break;
6262 case TYPE_LONG:
6263 case TYPE_ULONG:
6264 size = REG_SIZEOF_LONG;
6265 break;
6266 case TYPE_POINTER:
6267 size = REG_SIZEOF_POINTER;
6268 break;
6269 case TYPE_PRODUCT:
6270 {
6271 size_t pad;
6272 size = 0;
6273 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6274 pad = reg_needed_padding(state, type->left, size);
6275 size = size + pad + reg_size_of(state, type->left);
6276 type = type->right;
6277 }
6278 pad = reg_needed_padding(state, type, size);
6279 size = size + pad + reg_size_of(state, type);
6280 break;
6281 }
6282 case TYPE_OVERLAP:
6283 {
6284 size_t size_left, size_right;
6285 size_left = reg_size_of(state, type->left);
6286 size_right = reg_size_of(state, type->right);
6287 size = (size_left >= size_right)? size_left : size_right;
6288 break;
6289 }
6290 case TYPE_ARRAY:
6291 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6292 internal_error(state, 0, "Invalid array type");
6293 } else {
6294 size = reg_size_of(state, type->left) * type->elements;
6295 }
6296 break;
6297 case TYPE_STRUCT:
6298 case TYPE_TUPLE:
6299 {
6300 size_t pad;
6301 size = reg_size_of(state, type->left);
6302 /* Pad structures so their size is a multiples of their alignment */
6303 pad = reg_needed_padding(state, type, size);
6304 size = size + pad;
6305 break;
6306 }
6307 case TYPE_UNION:
6308 case TYPE_JOIN:
6309 {
6310 size_t pad;
6311 size = reg_size_of(state, type->left);
6312 /* Pad unions so their size is a multiple of their alignment */
6313 pad = reg_needed_padding(state, type, size);
6314 size = size + pad;
6315 break;
6316 }
6317 default:
6318 internal_error(state, 0, "sizeof not yet defined for type");
6319 break;
6320 }
6321 return size;
6322}
6323
6324static size_t registers_of(struct compile_state *state, struct type *type)
6325{
6326 size_t registers;
6327 registers = reg_size_of(state, type);
6328 registers += REG_SIZEOF_REG - 1;
6329 registers /= REG_SIZEOF_REG;
6330 return registers;
6331}
6332
6333static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6334{
6335 return bits_to_bytes(size_of(state, type));
6336}
6337
Eric Biederman0babc1c2003-05-09 02:39:00 +00006338static size_t field_offset(struct compile_state *state,
6339 struct type *type, struct hash_entry *field)
6340{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006341 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006342 size_t size;
6343
Eric Biederman0babc1c2003-05-09 02:39:00 +00006344 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006345 member = 0;
6346 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6347 member = type->left;
6348 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6349 size += needed_padding(state, member->left, size);
6350 if (member->left->field_ident == field) {
6351 member = member->left;
6352 break;
6353 }
6354 size += size_of(state, member->left);
6355 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006356 }
Eric Biederman90089602004-05-28 14:11:54 +00006357 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006358 }
Eric Biederman90089602004-05-28 14:11:54 +00006359 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6360 member = type->left;
6361 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6362 if (member->left->field_ident == field) {
6363 member = member->left;
6364 break;
6365 }
6366 member = member->right;
6367 }
6368 }
6369 else {
6370 internal_error(state, 0, "field_offset only works on structures and unions");
6371 }
6372
6373 if (!member || (member->field_ident != field)) {
6374 error(state, 0, "member %s not present", field->name);
6375 }
6376 return size;
6377}
6378
6379static size_t field_reg_offset(struct compile_state *state,
6380 struct type *type, struct hash_entry *field)
6381{
6382 struct type *member;
6383 size_t size;
6384
6385 size = 0;
6386 member = 0;
6387 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6388 member = type->left;
6389 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6390 size += reg_needed_padding(state, member->left, size);
6391 if (member->left->field_ident == field) {
6392 member = member->left;
6393 break;
6394 }
6395 size += reg_size_of(state, member->left);
6396 member = member->right;
6397 }
6398 }
6399 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6400 member = type->left;
6401 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6402 if (member->left->field_ident == field) {
6403 member = member->left;
6404 break;
6405 }
6406 member = member->right;
6407 }
6408 }
6409 else {
6410 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6411 }
6412
6413 size += reg_needed_padding(state, member, size);
6414 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006415 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006416 }
6417 return size;
6418}
6419
6420static struct type *field_type(struct compile_state *state,
6421 struct type *type, struct hash_entry *field)
6422{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006423 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006424
6425 member = 0;
6426 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6427 member = type->left;
6428 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6429 if (member->left->field_ident == field) {
6430 member = member->left;
6431 break;
6432 }
6433 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006434 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006435 }
Eric Biederman90089602004-05-28 14:11:54 +00006436 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6437 member = type->left;
6438 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6439 if (member->left->field_ident == field) {
6440 member = member->left;
6441 break;
6442 }
6443 member = member->right;
6444 }
6445 }
6446 else {
6447 internal_error(state, 0, "field_type only works on structures and unions");
6448 }
6449
6450 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006451 error(state, 0, "member %s not present", field->name);
6452 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006453 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006454}
6455
Eric Biederman90089602004-05-28 14:11:54 +00006456static size_t index_offset(struct compile_state *state,
6457 struct type *type, ulong_t index)
6458{
6459 struct type *member;
6460 size_t size;
6461 size = 0;
6462 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6463 size = size_of(state, type->left) * index;
6464 }
6465 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6466 ulong_t i;
6467 member = type->left;
6468 i = 0;
6469 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6470 size += needed_padding(state, member->left, size);
6471 if (i == index) {
6472 member = member->left;
6473 break;
6474 }
6475 size += size_of(state, member->left);
6476 i++;
6477 member = member->right;
6478 }
6479 size += needed_padding(state, member, size);
6480 if (i != index) {
6481 internal_error(state, 0, "Missing member index: %u", index);
6482 }
6483 }
6484 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6485 ulong_t i;
6486 size = 0;
6487 member = type->left;
6488 i = 0;
6489 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6490 if (i == index) {
6491 member = member->left;
6492 break;
6493 }
6494 i++;
6495 member = member->right;
6496 }
6497 if (i != index) {
6498 internal_error(state, 0, "Missing member index: %u", index);
6499 }
6500 }
6501 else {
6502 internal_error(state, 0,
6503 "request for index %u in something not an array, tuple or join",
6504 index);
6505 }
6506 return size;
6507}
6508
6509static size_t index_reg_offset(struct compile_state *state,
6510 struct type *type, ulong_t index)
6511{
6512 struct type *member;
6513 size_t size;
6514 size = 0;
6515 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6516 size = reg_size_of(state, type->left) * index;
6517 }
6518 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6519 ulong_t i;
6520 member = type->left;
6521 i = 0;
6522 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6523 size += reg_needed_padding(state, member->left, size);
6524 if (i == index) {
6525 member = member->left;
6526 break;
6527 }
6528 size += reg_size_of(state, member->left);
6529 i++;
6530 member = member->right;
6531 }
6532 size += reg_needed_padding(state, member, size);
6533 if (i != index) {
6534 internal_error(state, 0, "Missing member index: %u", index);
6535 }
6536
6537 }
6538 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6539 ulong_t i;
6540 size = 0;
6541 member = type->left;
6542 i = 0;
6543 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6544 if (i == index) {
6545 member = member->left;
6546 break;
6547 }
6548 i++;
6549 member = member->right;
6550 }
6551 if (i != index) {
6552 internal_error(state, 0, "Missing member index: %u", index);
6553 }
6554 }
6555 else {
6556 internal_error(state, 0,
6557 "request for index %u in something not an array, tuple or join",
6558 index);
6559 }
6560 return size;
6561}
6562
6563static struct type *index_type(struct compile_state *state,
6564 struct type *type, ulong_t index)
6565{
6566 struct type *member;
6567 if (index >= type->elements) {
6568 internal_error(state, 0, "Invalid element %u requested", index);
6569 }
6570 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6571 member = type->left;
6572 }
6573 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6574 ulong_t i;
6575 member = type->left;
6576 i = 0;
6577 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6578 if (i == index) {
6579 member = member->left;
6580 break;
6581 }
6582 i++;
6583 member = member->right;
6584 }
6585 if (i != index) {
6586 internal_error(state, 0, "Missing member index: %u", index);
6587 }
6588 }
6589 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6590 ulong_t i;
6591 member = type->left;
6592 i = 0;
6593 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6594 if (i == index) {
6595 member = member->left;
6596 break;
6597 }
6598 i++;
6599 member = member->right;
6600 }
6601 if (i != index) {
6602 internal_error(state, 0, "Missing member index: %u", index);
6603 }
6604 }
6605 else {
6606 member = 0;
6607 internal_error(state, 0,
6608 "request for index %u in something not an array, tuple or join",
6609 index);
6610 }
6611 return member;
6612}
6613
6614static struct type *unpack_type(struct compile_state *state, struct type *type)
6615{
6616 /* If I have a single register compound type not a bit-field
6617 * find the real type.
6618 */
6619 struct type *start_type;
6620 size_t size;
6621 /* Get out early if I need multiple registers for this type */
6622 size = reg_size_of(state, type);
6623 if (size > REG_SIZEOF_REG) {
6624 return type;
6625 }
6626 /* Get out early if I don't need any registers for this type */
6627 if (size == 0) {
6628 return &void_type;
6629 }
6630 /* Loop until I have no more layers I can remove */
6631 do {
6632 start_type = type;
6633 switch(type->type & TYPE_MASK) {
6634 case TYPE_ARRAY:
6635 /* If I have a single element the unpacked type
6636 * is that element.
6637 */
6638 if (type->elements == 1) {
6639 type = type->left;
6640 }
6641 break;
6642 case TYPE_STRUCT:
6643 case TYPE_TUPLE:
6644 /* If I have a single element the unpacked type
6645 * is that element.
6646 */
6647 if (type->elements == 1) {
6648 type = type->left;
6649 }
6650 /* If I have multiple elements the unpacked
6651 * type is the non-void element.
6652 */
6653 else {
6654 struct type *next, *member;
6655 struct type *sub_type;
6656 sub_type = 0;
6657 next = type->left;
6658 while(next) {
6659 member = next;
6660 next = 0;
6661 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6662 next = member->right;
6663 member = member->left;
6664 }
6665 if (reg_size_of(state, member) > 0) {
6666 if (sub_type) {
6667 internal_error(state, 0, "true compound type in a register");
6668 }
6669 sub_type = member;
6670 }
6671 }
6672 if (sub_type) {
6673 type = sub_type;
6674 }
6675 }
6676 break;
6677
6678 case TYPE_UNION:
6679 case TYPE_JOIN:
6680 /* If I have a single element the unpacked type
6681 * is that element.
6682 */
6683 if (type->elements == 1) {
6684 type = type->left;
6685 }
6686 /* I can't in general unpack union types */
6687 break;
6688 default:
6689 /* If I'm not a compound type I can't unpack it */
6690 break;
6691 }
6692 } while(start_type != type);
6693 switch(type->type & TYPE_MASK) {
6694 case TYPE_STRUCT:
6695 case TYPE_ARRAY:
6696 case TYPE_TUPLE:
6697 internal_error(state, 0, "irredicible type?");
6698 break;
6699 }
6700 return type;
6701}
6702
6703static int equiv_types(struct type *left, struct type *right);
6704static int is_compound_type(struct type *type);
6705
6706static struct type *reg_type(
6707 struct compile_state *state, struct type *type, int reg_offset)
6708{
6709 struct type *member;
6710 size_t size;
6711#if 1
6712 struct type *invalid;
6713 invalid = invalid_type(state, type);
6714 if (invalid) {
6715 fprintf(state->errout, "type: ");
6716 name_of(state->errout, type);
6717 fprintf(state->errout, "\n");
6718 fprintf(state->errout, "invalid: ");
6719 name_of(state->errout, invalid);
6720 fprintf(state->errout, "\n");
6721 internal_error(state, 0, "bad input type?");
6722 }
6723#endif
6724
6725 size = reg_size_of(state, type);
6726 if (reg_offset > size) {
6727 member = 0;
6728 fprintf(state->errout, "type: ");
6729 name_of(state->errout, type);
6730 fprintf(state->errout, "\n");
6731 internal_error(state, 0, "offset outside of type");
6732 }
6733 else {
6734 switch(type->type & TYPE_MASK) {
6735 /* Don't do anything with the basic types */
6736 case TYPE_VOID:
6737 case TYPE_CHAR: case TYPE_UCHAR:
6738 case TYPE_SHORT: case TYPE_USHORT:
6739 case TYPE_INT: case TYPE_UINT:
6740 case TYPE_LONG: case TYPE_ULONG:
6741 case TYPE_LLONG: case TYPE_ULLONG:
6742 case TYPE_FLOAT: case TYPE_DOUBLE:
6743 case TYPE_LDOUBLE:
6744 case TYPE_POINTER:
6745 case TYPE_ENUM:
6746 case TYPE_BITFIELD:
6747 member = type;
6748 break;
6749 case TYPE_ARRAY:
6750 member = type->left;
6751 size = reg_size_of(state, member);
6752 if (size > REG_SIZEOF_REG) {
6753 member = reg_type(state, member, reg_offset % size);
6754 }
6755 break;
6756 case TYPE_STRUCT:
6757 case TYPE_TUPLE:
6758 {
6759 size_t offset;
6760 offset = 0;
6761 member = type->left;
6762 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6763 size = reg_size_of(state, member->left);
6764 offset += reg_needed_padding(state, member->left, offset);
6765 if ((offset + size) > reg_offset) {
6766 member = member->left;
6767 break;
6768 }
6769 offset += size;
6770 member = member->right;
6771 }
6772 offset += reg_needed_padding(state, member, offset);
6773 member = reg_type(state, member, reg_offset - offset);
6774 break;
6775 }
6776 case TYPE_UNION:
6777 case TYPE_JOIN:
6778 {
6779 struct type *join, **jnext, *mnext;
6780 join = new_type(TYPE_JOIN, 0, 0);
6781 jnext = &join->left;
6782 mnext = type->left;
6783 while(mnext) {
6784 size_t size;
6785 member = mnext;
6786 mnext = 0;
6787 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6788 mnext = member->right;
6789 member = member->left;
6790 }
6791 size = reg_size_of(state, member);
6792 if (size > reg_offset) {
6793 struct type *part, *hunt;
6794 part = reg_type(state, member, reg_offset);
6795 /* See if this type is already in the union */
6796 hunt = join->left;
6797 while(hunt) {
6798 struct type *test = hunt;
6799 hunt = 0;
6800 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6801 hunt = test->right;
6802 test = test->left;
6803 }
6804 if (equiv_types(part, test)) {
6805 goto next;
6806 }
6807 }
6808 /* Nope add it */
6809 if (!*jnext) {
6810 *jnext = part;
6811 } else {
6812 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6813 jnext = &(*jnext)->right;
6814 }
6815 join->elements++;
6816 }
6817 next:
6818 ;
6819 }
6820 if (join->elements == 0) {
6821 internal_error(state, 0, "No elements?");
6822 }
6823 member = join;
6824 break;
6825 }
6826 default:
6827 member = 0;
6828 fprintf(state->errout, "type: ");
6829 name_of(state->errout, type);
6830 fprintf(state->errout, "\n");
6831 internal_error(state, 0, "reg_type not yet defined for type");
6832
6833 }
6834 }
6835 /* If I have a single register compound type not a bit-field
6836 * find the real type.
6837 */
6838 member = unpack_type(state, member);
6839 ;
6840 size = reg_size_of(state, member);
6841 if (size > REG_SIZEOF_REG) {
6842 internal_error(state, 0, "Cannot find type of single register");
6843 }
6844#if 1
6845 invalid = invalid_type(state, member);
6846 if (invalid) {
6847 fprintf(state->errout, "type: ");
6848 name_of(state->errout, member);
6849 fprintf(state->errout, "\n");
6850 fprintf(state->errout, "invalid: ");
6851 name_of(state->errout, invalid);
6852 fprintf(state->errout, "\n");
6853 internal_error(state, 0, "returning bad type?");
6854 }
6855#endif
6856 return member;
6857}
6858
Eric Biederman03b59862003-06-24 14:27:37 +00006859static struct type *next_field(struct compile_state *state,
6860 struct type *type, struct type *prev_member)
6861{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006862 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006863 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6864 internal_error(state, 0, "next_field only works on structures");
6865 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006866 member = type->left;
6867 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006868 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006869 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006870 break;
6871 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006872 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006873 prev_member = 0;
6874 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006875 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006876 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006877 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006878 prev_member = 0;
6879 }
6880 if (prev_member) {
6881 internal_error(state, 0, "prev_member %s not present",
6882 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006883 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006884 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006885}
6886
Eric Biederman90089602004-05-28 14:11:54 +00006887typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
6888 size_t ret_offset, size_t mem_offset, void *arg);
6889
6890static void walk_type_fields(struct compile_state *state,
6891 struct type *type, size_t reg_offset, size_t mem_offset,
6892 walk_type_fields_cb_t cb, void *arg);
6893
6894static void walk_struct_fields(struct compile_state *state,
6895 struct type *type, size_t reg_offset, size_t mem_offset,
6896 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006897{
Eric Biederman90089602004-05-28 14:11:54 +00006898 struct type *tptr;
6899 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006900 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006901 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006902 }
Eric Biederman90089602004-05-28 14:11:54 +00006903 tptr = type->left;
6904 for(i = 0; i < type->elements; i++) {
6905 struct type *mtype;
6906 mtype = tptr;
6907 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6908 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006909 }
Eric Biederman90089602004-05-28 14:11:54 +00006910 walk_type_fields(state, mtype,
6911 reg_offset +
6912 field_reg_offset(state, type, mtype->field_ident),
6913 mem_offset +
6914 field_offset(state, type, mtype->field_ident),
6915 cb, arg);
6916 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006917 }
Eric Biederman90089602004-05-28 14:11:54 +00006918
6919}
6920
6921static void walk_type_fields(struct compile_state *state,
6922 struct type *type, size_t reg_offset, size_t mem_offset,
6923 walk_type_fields_cb_t cb, void *arg)
6924{
6925 switch(type->type & TYPE_MASK) {
6926 case TYPE_STRUCT:
6927 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6928 break;
6929 case TYPE_CHAR:
6930 case TYPE_UCHAR:
6931 case TYPE_SHORT:
6932 case TYPE_USHORT:
6933 case TYPE_INT:
6934 case TYPE_UINT:
6935 case TYPE_LONG:
6936 case TYPE_ULONG:
6937 cb(state, type, reg_offset, mem_offset, arg);
6938 break;
6939 case TYPE_VOID:
6940 break;
6941 default:
6942 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006943 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006944}
6945
Eric Biedermanb138ac82003-04-22 18:44:01 +00006946static void arrays_complete(struct compile_state *state, struct type *type)
6947{
6948 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6949 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6950 error(state, 0, "array size not specified");
6951 }
6952 arrays_complete(state, type->left);
6953 }
6954}
6955
Eric Biederman90089602004-05-28 14:11:54 +00006956static unsigned int get_basic_type(struct type *type)
6957{
6958 unsigned int basic;
6959 basic = type->type & TYPE_MASK;
6960 /* Convert enums to ints */
6961 if (basic == TYPE_ENUM) {
6962 basic = TYPE_INT;
6963 }
6964 /* Convert bitfields to standard types */
6965 else if (basic == TYPE_BITFIELD) {
6966 if (type->elements <= SIZEOF_CHAR) {
6967 basic = TYPE_CHAR;
6968 }
6969 else if (type->elements <= SIZEOF_SHORT) {
6970 basic = TYPE_SHORT;
6971 }
6972 else if (type->elements <= SIZEOF_INT) {
6973 basic = TYPE_INT;
6974 }
6975 else if (type->elements <= SIZEOF_LONG) {
6976 basic = TYPE_LONG;
6977 }
6978 if (!TYPE_SIGNED(type->left->type)) {
6979 basic += 1;
6980 }
6981 }
6982 return basic;
6983}
6984
Eric Biedermanb138ac82003-04-22 18:44:01 +00006985static unsigned int do_integral_promotion(unsigned int type)
6986{
Eric Biederman83b991a2003-10-11 06:20:25 +00006987 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006988 type = TYPE_INT;
6989 }
6990 return type;
6991}
6992
6993static unsigned int do_arithmetic_conversion(
6994 unsigned int left, unsigned int right)
6995{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006996 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6997 return TYPE_LDOUBLE;
6998 }
6999 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
7000 return TYPE_DOUBLE;
7001 }
7002 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
7003 return TYPE_FLOAT;
7004 }
7005 left = do_integral_promotion(left);
7006 right = do_integral_promotion(right);
7007 /* If both operands have the same size done */
7008 if (left == right) {
7009 return left;
7010 }
7011 /* If both operands have the same signedness pick the larger */
7012 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
7013 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
7014 }
7015 /* If the signed type can hold everything use it */
7016 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
7017 return left;
7018 }
7019 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
7020 return right;
7021 }
7022 /* Convert to the unsigned type with the same rank as the signed type */
7023 else if (TYPE_SIGNED(left)) {
7024 return TYPE_MKUNSIGNED(left);
7025 }
7026 else {
7027 return TYPE_MKUNSIGNED(right);
7028 }
7029}
7030
7031/* see if two types are the same except for qualifiers */
7032static int equiv_types(struct type *left, struct type *right)
7033{
7034 unsigned int type;
7035 /* Error if the basic types do not match */
7036 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
7037 return 0;
7038 }
7039 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00007040 /* If the basic types match and it is a void type we are done */
7041 if (type == TYPE_VOID) {
7042 return 1;
7043 }
Eric Biederman90089602004-05-28 14:11:54 +00007044 /* For bitfields we need to compare the sizes */
7045 else if (type == TYPE_BITFIELD) {
7046 return (left->elements == right->elements) &&
7047 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
7048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007049 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00007050 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007051 return 1;
7052 }
7053 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00007054 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007055 return equiv_types(left->left, right->left);
7056 }
7057 else if (type == TYPE_ARRAY) {
7058 return (left->elements == right->elements) &&
7059 equiv_types(left->left, right->left);
7060 }
Eric Biederman90089602004-05-28 14:11:54 +00007061 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007062 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007063 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007064 }
Eric Biederman90089602004-05-28 14:11:54 +00007065 /* test for union equality */
7066 else if (type == TYPE_UNION) {
7067 return left->type_ident == right->type_ident;
7068 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007069 /* Test for equivalent functions */
7070 else if (type == TYPE_FUNCTION) {
7071 return equiv_types(left->left, right->left) &&
7072 equiv_types(left->right, right->right);
7073 }
7074 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00007075 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007076 else if (type == TYPE_PRODUCT) {
7077 return equiv_types(left->left, right->left) &&
7078 equiv_types(left->right, right->right);
7079 }
Eric Biederman90089602004-05-28 14:11:54 +00007080 /* We should see TYPE_OVERLAP when comparing joins */
7081 else if (type == TYPE_OVERLAP) {
7082 return equiv_types(left->left, right->left) &&
7083 equiv_types(left->right, right->right);
7084 }
7085 /* Test for equivalence of tuples */
7086 else if (type == TYPE_TUPLE) {
7087 return (left->elements == right->elements) &&
7088 equiv_types(left->left, right->left);
7089 }
7090 /* Test for equivalence of joins */
7091 else if (type == TYPE_JOIN) {
7092 return (left->elements == right->elements) &&
7093 equiv_types(left->left, right->left);
7094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007095 else {
7096 return 0;
7097 }
7098}
7099
7100static int equiv_ptrs(struct type *left, struct type *right)
7101{
7102 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7103 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7104 return 0;
7105 }
7106 return equiv_types(left->left, right->left);
7107}
7108
7109static struct type *compatible_types(struct type *left, struct type *right)
7110{
7111 struct type *result;
7112 unsigned int type, qual_type;
7113 /* Error if the basic types do not match */
7114 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
7115 return 0;
7116 }
7117 type = left->type & TYPE_MASK;
7118 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7119 result = 0;
7120 /* if the basic types match and it is an arithmetic type we are done */
7121 if (TYPE_ARITHMETIC(type)) {
7122 result = new_type(qual_type, 0, 0);
7123 }
7124 /* If it is a pointer type recurse and keep testing */
7125 else if (type == TYPE_POINTER) {
7126 result = compatible_types(left->left, right->left);
7127 if (result) {
7128 result = new_type(qual_type, result, 0);
7129 }
7130 }
Eric Biederman90089602004-05-28 14:11:54 +00007131 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007132 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007133 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007134 result = left;
7135 }
7136 }
Eric Biederman90089602004-05-28 14:11:54 +00007137 /* test for union equality */
7138 else if (type == TYPE_UNION) {
7139 if (left->type_ident == right->type_ident) {
7140 result = left;
7141 }
7142 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007143 /* Test for equivalent functions */
7144 else if (type == TYPE_FUNCTION) {
7145 struct type *lf, *rf;
7146 lf = compatible_types(left->left, right->left);
7147 rf = compatible_types(left->right, right->right);
7148 if (lf && rf) {
7149 result = new_type(qual_type, lf, rf);
7150 }
7151 }
7152 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7153 else if (type == TYPE_PRODUCT) {
7154 struct type *lf, *rf;
7155 lf = compatible_types(left->left, right->left);
7156 rf = compatible_types(left->right, right->right);
7157 if (lf && rf) {
7158 result = new_type(qual_type, lf, rf);
7159 }
7160 }
7161 else {
7162 /* Nothing else is compatible */
7163 }
7164 return result;
7165}
7166
Eric Biederman90089602004-05-28 14:11:54 +00007167/* See if left is a equivalent to right or right is a union member of left */
7168static int is_subset_type(struct type *left, struct type *right)
7169{
7170 if (equiv_types(left, right)) {
7171 return 1;
7172 }
7173 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7174 struct type *member, *mnext;
7175 mnext = left->left;
7176 while(mnext) {
7177 member = mnext;
7178 mnext = 0;
7179 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7180 mnext = member->right;
7181 member = member->left;
7182 }
7183 if (is_subset_type( member, right)) {
7184 return 1;
7185 }
7186 }
7187 }
7188 return 0;
7189}
7190
Eric Biedermanb138ac82003-04-22 18:44:01 +00007191static struct type *compatible_ptrs(struct type *left, struct type *right)
7192{
7193 struct type *result;
7194 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7195 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7196 return 0;
7197 }
7198 result = compatible_types(left->left, right->left);
7199 if (result) {
7200 unsigned int qual_type;
7201 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7202 result = new_type(qual_type, result, 0);
7203 }
7204 return result;
7205
7206}
7207static struct triple *integral_promotion(
7208 struct compile_state *state, struct triple *def)
7209{
7210 struct type *type;
7211 type = def->type;
7212 /* As all operations are carried out in registers
7213 * the values are converted on load I just convert
7214 * logical type of the operand.
7215 */
7216 if (TYPE_INTEGER(type->type)) {
7217 unsigned int int_type;
7218 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007219 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007220 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007221 if (def->op != OP_LOAD) {
7222 def->type = new_type(int_type, 0, 0);
7223 }
7224 else {
Eric Biederman90089602004-05-28 14:11:54 +00007225 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007226 new_type(int_type, 0, 0), def, 0);
7227 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007228 }
7229 }
7230 return def;
7231}
7232
7233
7234static void arithmetic(struct compile_state *state, struct triple *def)
7235{
7236 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007237 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007238 }
7239}
7240
7241static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7242{
7243 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7244 error(state, def, "pointer or arithmetic type expected");
7245 }
7246}
7247
7248static int is_integral(struct triple *ins)
7249{
7250 return TYPE_INTEGER(ins->type->type);
7251}
7252
7253static void integral(struct compile_state *state, struct triple *def)
7254{
7255 if (!is_integral(def)) {
7256 error(state, 0, "integral type expected");
7257 }
7258}
7259
7260
7261static void bool(struct compile_state *state, struct triple *def)
7262{
7263 if (!TYPE_ARITHMETIC(def->type->type) &&
7264 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7265 error(state, 0, "arithmetic or pointer type expected");
7266 }
7267}
7268
7269static int is_signed(struct type *type)
7270{
Eric Biederman90089602004-05-28 14:11:54 +00007271 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7272 type = type->left;
7273 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007274 return !!TYPE_SIGNED(type->type);
7275}
Eric Biederman90089602004-05-28 14:11:54 +00007276static int is_compound_type(struct type *type)
7277{
7278 int is_compound;
7279 switch((type->type & TYPE_MASK)) {
7280 case TYPE_ARRAY:
7281 case TYPE_STRUCT:
7282 case TYPE_TUPLE:
7283 case TYPE_UNION:
7284 case TYPE_JOIN:
7285 is_compound = 1;
7286 break;
7287 default:
7288 is_compound = 0;
7289 break;
7290 }
7291 return is_compound;
7292}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007293
Eric Biederman0babc1c2003-05-09 02:39:00 +00007294/* Is this value located in a register otherwise it must be in memory */
7295static int is_in_reg(struct compile_state *state, struct triple *def)
7296{
7297 int in_reg;
7298 if (def->op == OP_ADECL) {
7299 in_reg = 1;
7300 }
7301 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7302 in_reg = 0;
7303 }
Eric Biederman90089602004-05-28 14:11:54 +00007304 else if (triple_is_part(state, def)) {
7305 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007306 }
7307 else {
Eric Biederman90089602004-05-28 14:11:54 +00007308 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007309 in_reg = -1;
7310 }
7311 return in_reg;
7312}
7313
Eric Biederman90089602004-05-28 14:11:54 +00007314/* Is this an auto or static variable location? Something that can
7315 * be assigned to. Otherwise it must must be a pure value, a temporary.
7316 */
7317static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007318{
7319 int ret;
7320 ret = 0;
7321 if (!def) {
7322 return 0;
7323 }
7324 if ((def->op == OP_ADECL) ||
7325 (def->op == OP_SDECL) ||
7326 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007327 (def->op == OP_BLOBCONST) ||
7328 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007329 ret = 1;
7330 }
Eric Biederman90089602004-05-28 14:11:54 +00007331 else if (triple_is_part(state, def)) {
7332 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007333 }
7334 return ret;
7335}
7336
Eric Biederman00443072003-06-24 12:34:45 +00007337static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007338{
7339 if (!def) {
7340 internal_error(state, def, "nothing where lvalue expected?");
7341 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007342 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007343 error(state, def, "lvalue expected");
7344 }
7345}
Eric Biederman00443072003-06-24 12:34:45 +00007346static void lvalue(struct compile_state *state, struct triple *def)
7347{
7348 clvalue(state, def);
7349 if (def->type->type & QUAL_CONST) {
7350 error(state, def, "modifable lvalue expected");
7351 }
7352}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007353
7354static int is_pointer(struct triple *def)
7355{
7356 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7357}
7358
7359static void pointer(struct compile_state *state, struct triple *def)
7360{
7361 if (!is_pointer(def)) {
7362 error(state, def, "pointer expected");
7363 }
7364}
7365
7366static struct triple *int_const(
7367 struct compile_state *state, struct type *type, ulong_t value)
7368{
7369 struct triple *result;
7370 switch(type->type & TYPE_MASK) {
7371 case TYPE_CHAR:
7372 case TYPE_INT: case TYPE_UINT:
7373 case TYPE_LONG: case TYPE_ULONG:
7374 break;
7375 default:
Eric Biederman90089602004-05-28 14:11:54 +00007376 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007377 }
7378 result = triple(state, OP_INTCONST, type, 0, 0);
7379 result->u.cval = value;
7380 return result;
7381}
7382
7383
Eric Biederman83b991a2003-10-11 06:20:25 +00007384static struct triple *read_expr(struct compile_state *state, struct triple *def);
7385
Eric Biederman0babc1c2003-05-09 02:39:00 +00007386static struct triple *do_mk_addr_expr(struct compile_state *state,
7387 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007388{
7389 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007390 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007391 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007392
Eric Biederman90089602004-05-28 14:11:54 +00007393 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007394
Eric Biederman90089602004-05-28 14:11:54 +00007395
Eric Biedermanb138ac82003-04-22 18:44:01 +00007396 result = 0;
7397 if (expr->op == OP_ADECL) {
7398 error(state, expr, "address of auto variables not supported");
7399 }
7400 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007401 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007402 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007403 result->u.cval = offset;
7404 }
7405 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007406 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007407 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007408 int_const(state, &ulong_type, offset));
7409 }
Eric Biederman90089602004-05-28 14:11:54 +00007410 else if (expr->op == OP_BLOBCONST) {
7411 FINISHME();
7412 internal_error(state, expr, "not yet implemented");
7413 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007414 else if (expr->op == OP_LIST) {
7415 error(state, 0, "Function addresses not supported");
7416 }
Eric Biederman90089602004-05-28 14:11:54 +00007417 else if (triple_is_part(state, expr)) {
7418 struct triple *part;
7419 part = expr;
7420 expr = MISC(expr, 0);
7421 if (part->op == OP_DOT) {
7422 offset += bits_to_bytes(
7423 field_offset(state, expr->type, part->u.field));
7424 }
7425 else if (part->op == OP_INDEX) {
7426 offset += bits_to_bytes(
7427 index_offset(state, expr->type, part->u.cval));
7428 }
7429 else {
7430 internal_error(state, part, "unhandled part type");
7431 }
7432 result = do_mk_addr_expr(state, expr, type, offset);
7433 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007434 if (!result) {
7435 internal_error(state, expr, "cannot take address of expression");
7436 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007437 return result;
7438}
7439
Eric Biederman0babc1c2003-05-09 02:39:00 +00007440static struct triple *mk_addr_expr(
7441 struct compile_state *state, struct triple *expr, ulong_t offset)
7442{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007443 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007444}
7445
Eric Biedermanb138ac82003-04-22 18:44:01 +00007446static struct triple *mk_deref_expr(
7447 struct compile_state *state, struct triple *expr)
7448{
7449 struct type *base_type;
7450 pointer(state, expr);
7451 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007452 return triple(state, OP_DEREF, base_type, expr, 0);
7453}
7454
Eric Biederman90089602004-05-28 14:11:54 +00007455/* lvalue conversions always apply except when certain operators
7456 * are applied. So I apply apply it when I know no more
7457 * operators will be applied.
7458 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007459static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007460{
Eric Biederman5cd81732004-03-11 15:01:31 +00007461 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007462 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7463 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007464 type = new_type(
7465 TYPE_POINTER | (def->type->type & QUAL_MASK),
7466 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007467 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007468 struct triple *addrconst;
7469 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7470 internal_error(state, def, "bad array constant");
7471 }
7472 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7473 MISC(addrconst, 0) = def;
7474 def = addrconst;
7475 }
7476 else {
Eric Biederman90089602004-05-28 14:11:54 +00007477 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007478 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007479 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007480 /* Transform a function to a pointer to it */
7481 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7482 def = mk_addr_expr(state, def, 0);
7483 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007484 return def;
7485}
7486
Eric Biederman0babc1c2003-05-09 02:39:00 +00007487static struct triple *deref_field(
7488 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7489{
7490 struct triple *result;
7491 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007492 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007493 if (!field) {
7494 internal_error(state, 0, "No field passed to deref_field");
7495 }
7496 result = 0;
7497 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007498 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7499 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007500 error(state, 0, "request for member %s in something not a struct or union",
7501 field->name);
7502 }
Eric Biederman03b59862003-06-24 14:27:37 +00007503 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007504 if ((type->type & STOR_MASK) == STOR_PERM) {
7505 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007506 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007507 result = do_mk_addr_expr(state, expr, member, offset);
7508 result = mk_deref_expr(state, result);
7509 }
7510 else {
7511 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007512 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007513 result->u.field = field;
7514 }
7515 return result;
7516}
7517
Eric Biederman90089602004-05-28 14:11:54 +00007518static struct triple *deref_index(
7519 struct compile_state *state, struct triple *expr, size_t index)
7520{
7521 struct triple *result;
7522 struct type *type, *member;
7523 ulong_t offset;
7524
7525 result = 0;
7526 type = expr->type;
7527 member = index_type(state, type, index);
7528
7529 if ((type->type & STOR_MASK) == STOR_PERM) {
7530 offset = bits_to_bytes(index_offset(state, type, index));
7531 result = do_mk_addr_expr(state, expr, member, offset);
7532 result = mk_deref_expr(state, result);
7533 }
7534 else {
7535 result = triple(state, OP_INDEX, member, expr, 0);
7536 result->u.cval = index;
7537 }
7538 return result;
7539}
7540
Eric Biedermanb138ac82003-04-22 18:44:01 +00007541static struct triple *read_expr(struct compile_state *state, struct triple *def)
7542{
7543 int op;
7544 if (!def) {
7545 return 0;
7546 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007547#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
7548 /* Transform lvalues into something we can read */
7549 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007550 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007551 return def;
7552 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007553 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007554 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007555 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007556 if (def->op == OP_SDECL) {
7557 def = mk_addr_expr(state, def, 0);
7558 def = mk_deref_expr(state, def);
7559 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007560 op = OP_LOAD;
7561 }
Eric Biederman90089602004-05-28 14:11:54 +00007562 def = triple(state, op, def->type, def, 0);
7563 if (def->type->type & QUAL_VOLATILE) {
7564 def->id |= TRIPLE_FLAG_VOLATILE;
7565 }
7566 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007567}
7568
Eric Biedermane058a1e2003-07-12 01:21:31 +00007569int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007570 struct type *dest, struct type *rval)
7571{
7572 int compatible = 0;
7573 /* Both operands have arithmetic type */
7574 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7575 compatible = 1;
7576 }
7577 /* One operand is a pointer and the other is a pointer to void */
7578 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7579 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7580 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7581 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7582 compatible = 1;
7583 }
7584 /* If both types are the same without qualifiers we are good */
7585 else if (equiv_ptrs(dest, rval)) {
7586 compatible = 1;
7587 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007588 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007589 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007590 compatible = 1;
7591 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007592 return compatible;
7593}
7594
Eric Biedermane058a1e2003-07-12 01:21:31 +00007595static void write_compatible(struct compile_state *state,
7596 struct type *dest, struct type *rval)
7597{
7598 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007599 FILE *fp = state->errout;
7600 fprintf(fp, "dest: ");
7601 name_of(fp, dest);
7602 fprintf(fp,"\nrval: ");
7603 name_of(fp, rval);
7604 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007605 error(state, 0, "Incompatible types in assignment");
7606 }
7607}
7608
Eric Biedermane058a1e2003-07-12 01:21:31 +00007609static int is_init_compatible(struct compile_state *state,
7610 struct type *dest, struct type *rval)
7611{
7612 int compatible = 0;
7613 if (is_write_compatible(state, dest, rval)) {
7614 compatible = 1;
7615 }
7616 else if (equiv_types(dest, rval)) {
7617 compatible = 1;
7618 }
7619 return compatible;
7620}
7621
Eric Biedermanb138ac82003-04-22 18:44:01 +00007622static struct triple *write_expr(
7623 struct compile_state *state, struct triple *dest, struct triple *rval)
7624{
7625 struct triple *def;
7626 int op;
7627
7628 def = 0;
7629 if (!rval) {
7630 internal_error(state, 0, "missing rval");
7631 }
7632
7633 if (rval->op == OP_LIST) {
7634 internal_error(state, 0, "expression of type OP_LIST?");
7635 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007636 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007637 internal_error(state, 0, "writing to a non lvalue?");
7638 }
Eric Biederman00443072003-06-24 12:34:45 +00007639 if (dest->type->type & QUAL_CONST) {
7640 internal_error(state, 0, "modifable lvalue expexted");
7641 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007642
7643 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007644 if (!equiv_types(dest->type, rval->type)) {
7645 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7646 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007647
7648 /* Now figure out which assignment operator to use */
7649 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007650 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007651 def = triple(state, OP_WRITE, dest->type, rval, dest);
7652 if (MISC(def, 0) != dest) {
7653 internal_error(state, def, "huh?");
7654 }
7655 if (RHS(def, 0) != rval) {
7656 internal_error(state, def, "huh?");
7657 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007658 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007659 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007660 }
Eric Biederman90089602004-05-28 14:11:54 +00007661 if (def->type->type & QUAL_VOLATILE) {
7662 def->id |= TRIPLE_FLAG_VOLATILE;
7663 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007664 return def;
7665}
7666
7667static struct triple *init_expr(
7668 struct compile_state *state, struct triple *dest, struct triple *rval)
7669{
7670 struct triple *def;
7671
7672 def = 0;
7673 if (!rval) {
7674 internal_error(state, 0, "missing rval");
7675 }
7676 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7677 rval = read_expr(state, rval);
7678 def = write_expr(state, dest, rval);
7679 }
7680 else {
7681 /* Fill in the array size if necessary */
7682 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7683 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7684 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7685 dest->type->elements = rval->type->elements;
7686 }
7687 }
7688 if (!equiv_types(dest->type, rval->type)) {
7689 error(state, 0, "Incompatible types in inializer");
7690 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007691 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007692 insert_triple(state, dest, rval);
7693 rval->id |= TRIPLE_FLAG_FLATTENED;
7694 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007695 }
7696 return def;
7697}
7698
7699struct type *arithmetic_result(
7700 struct compile_state *state, struct triple *left, struct triple *right)
7701{
7702 struct type *type;
7703 /* Sanity checks to ensure I am working with arithmetic types */
7704 arithmetic(state, left);
7705 arithmetic(state, right);
7706 type = new_type(
7707 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007708 get_basic_type(left->type),
7709 get_basic_type(right->type)),
7710 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007711 return type;
7712}
7713
7714struct type *ptr_arithmetic_result(
7715 struct compile_state *state, struct triple *left, struct triple *right)
7716{
7717 struct type *type;
7718 /* Sanity checks to ensure I am working with the proper types */
7719 ptr_arithmetic(state, left);
7720 arithmetic(state, right);
7721 if (TYPE_ARITHMETIC(left->type->type) &&
7722 TYPE_ARITHMETIC(right->type->type)) {
7723 type = arithmetic_result(state, left, right);
7724 }
7725 else if (TYPE_PTR(left->type->type)) {
7726 type = left->type;
7727 }
7728 else {
7729 internal_error(state, 0, "huh?");
7730 type = 0;
7731 }
7732 return type;
7733}
7734
Eric Biedermanb138ac82003-04-22 18:44:01 +00007735/* boolean helper function */
7736
7737static struct triple *ltrue_expr(struct compile_state *state,
7738 struct triple *expr)
7739{
7740 switch(expr->op) {
7741 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7742 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7743 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7744 /* If the expression is already boolean do nothing */
7745 break;
7746 default:
7747 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7748 break;
7749 }
7750 return expr;
7751}
7752
7753static struct triple *lfalse_expr(struct compile_state *state,
7754 struct triple *expr)
7755{
7756 return triple(state, OP_LFALSE, &int_type, expr, 0);
7757}
7758
Eric Biederman90089602004-05-28 14:11:54 +00007759static struct triple *mkland_expr(
7760 struct compile_state *state,
7761 struct triple *left, struct triple *right)
7762{
7763 struct triple *def, *val, *var, *jmp, *mid, *end;
7764
7765 /* Generate some intermediate triples */
7766 end = label(state);
7767 var = variable(state, &int_type);
7768
7769 /* Store the left hand side value */
7770 left = write_expr(state, var, left);
7771
7772 /* Jump if the value is false */
7773 jmp = branch(state, end,
7774 lfalse_expr(state, read_expr(state, var)));
7775 mid = label(state);
7776
7777 /* Store the right hand side value */
7778 right = write_expr(state, var, right);
7779
7780 /* An expression for the computed value */
7781 val = read_expr(state, var);
7782
7783 /* Generate the prog for a logical and */
7784 def = mkprog(state, var, left, jmp, mid, right, end, val, 0);
7785
7786 return def;
7787}
7788
7789static struct triple *mklor_expr(
7790 struct compile_state *state,
7791 struct triple *left, struct triple *right)
7792{
7793 struct triple *def, *val, *var, *jmp, *mid, *end;
7794
7795 /* Generate some intermediate triples */
7796 end = label(state);
7797 var = variable(state, &int_type);
7798
7799 /* Store the left hand side value */
7800 left = write_expr(state, var, left);
7801
7802 /* Jump if the value is true */
7803 jmp = branch(state, end, read_expr(state, var));
7804 mid = label(state);
7805
7806 /* Store the right hand side value */
7807 right = write_expr(state, var, right);
7808
7809 /* An expression for the computed value*/
7810 val = read_expr(state, var);
7811
7812 /* Generate the prog for a logical or */
7813 def = mkprog(state, var, left, jmp, mid, right, end, val, 0);
7814
7815 return def;
7816}
7817
7818static struct triple *mkcond_expr(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007819 struct compile_state *state,
7820 struct triple *test, struct triple *left, struct triple *right)
7821{
Eric Biederman90089602004-05-28 14:11:54 +00007822 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007823 struct type *result_type;
7824 unsigned int left_type, right_type;
7825 bool(state, test);
7826 left_type = left->type->type;
7827 right_type = right->type->type;
7828 result_type = 0;
7829 /* Both operands have arithmetic type */
7830 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7831 result_type = arithmetic_result(state, left, right);
7832 }
7833 /* Both operands have void type */
7834 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7835 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7836 result_type = &void_type;
7837 }
7838 /* pointers to the same type... */
7839 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7840 ;
7841 }
7842 /* Both operands are pointers and left is a pointer to void */
7843 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7844 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7845 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7846 result_type = right->type;
7847 }
7848 /* Both operands are pointers and right is a pointer to void */
7849 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7850 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7851 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7852 result_type = left->type;
7853 }
7854 if (!result_type) {
7855 error(state, 0, "Incompatible types in conditional expression");
7856 }
Eric Biederman90089602004-05-28 14:11:54 +00007857 /* Generate some intermediate triples */
7858 mid = label(state);
7859 end = label(state);
7860 var = variable(state, result_type);
7861
7862 /* Branch if the test is false */
7863 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7864 top = label(state);
7865
7866 /* Store the left hand side value */
7867 left = write_expr(state, var, left);
7868
7869 /* Branch to the end */
7870 jmp2 = branch(state, end, 0);
7871
7872 /* Store the right hand side value */
7873 right = write_expr(state, var, right);
7874
7875 /* An expression for the computed value */
7876 val = read_expr(state, var);
7877
7878 /* Generate the prog for a conditional expression */
7879 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0);
7880
Eric Biedermanb138ac82003-04-22 18:44:01 +00007881 return def;
7882}
7883
7884
Eric Biederman0babc1c2003-05-09 02:39:00 +00007885static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007886{
Eric Biederman90089602004-05-28 14:11:54 +00007887#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Eric Biedermanb138ac82003-04-22 18:44:01 +00007888 int count;
7889 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007890 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7891 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007892 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007893 else if (ins->op == OP_DEREF) {
7894 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007895 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007896 else if (ins->op == OP_VAL) {
7897 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007898 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007899 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007900 /* Don't figure the depth of a call just guess it is huge */
7901 count = 1000;
7902 }
7903 else {
7904 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007905 expr = triple_rhs(state, ins, 0);
7906 for(;expr; expr = triple_rhs(state, ins, expr)) {
7907 if (*expr) {
7908 int depth;
7909 depth = expr_depth(state, *expr);
7910 if (depth > count) {
7911 count = depth;
7912 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007913 }
7914 }
7915 }
7916 return count + 1;
7917}
7918
Eric Biederman0babc1c2003-05-09 02:39:00 +00007919static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007920 struct compile_state *state, struct triple *first, struct triple *ptr,
7921 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007922{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007923 struct rhs_vector {
7924 int depth;
7925 struct triple **ins;
7926 } vector[MAX_RHS];
7927 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007928 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007929 rhs = ptr->rhs;
7930 lhs = ptr->lhs;
7931 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007932 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007933 ptr->op, tops(ptr->op));
7934 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007935 /* Find the depth of the rhs elements */
7936 for(i = 0; i < rhs; i++) {
7937 vector[i].ins = &RHS(ptr, i);
7938 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007939 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007940 /* Selection sort the rhs */
7941 for(i = 0; i < rhs; i++) {
7942 int j, max = i;
7943 for(j = i + 1; j < rhs; j++ ) {
7944 if (vector[j].depth > vector[max].depth) {
7945 max = j;
7946 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007947 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007948 if (max != i) {
7949 struct rhs_vector tmp;
7950 tmp = vector[i];
7951 vector[i] = vector[max];
7952 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007953 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007954 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007955 /* Now flatten the rhs elements */
7956 for(i = 0; i < rhs; i++) {
7957 *vector[i].ins = flatten(state, first, *vector[i].ins);
7958 use_triple(*vector[i].ins, ptr);
7959 }
Eric Biederman90089602004-05-28 14:11:54 +00007960 if (lhs) {
7961 insert_triple(state, first, ptr);
7962 ptr->id |= TRIPLE_FLAG_FLATTENED;
7963 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7964
7965 /* Now flatten the lhs elements */
7966 for(i = 0; i < lhs; i++) {
7967 struct triple **ins = &LHS(ptr, i);
7968 *ins = flatten(state, first, *ins);
7969 use_triple(*ins, ptr);
7970 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007971 }
7972 return ptr;
7973}
7974
Eric Biederman90089602004-05-28 14:11:54 +00007975static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007976 struct compile_state *state, struct triple *first, struct triple *ptr)
7977{
Eric Biederman90089602004-05-28 14:11:54 +00007978 struct triple *head, *body, *val;
7979 head = RHS(ptr, 0);
7980 RHS(ptr, 0) = 0;
7981 val = head->prev;
7982 body = head->next;
7983 release_triple(state, head);
7984 release_triple(state, ptr);
7985 val->next = first;
7986 body->prev = first->prev;
7987 body->prev->next = body;
7988 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007989
Eric Biederman90089602004-05-28 14:11:54 +00007990 if (triple_is_cbranch(state, body->prev) ||
7991 triple_is_call(state, body->prev)) {
7992 unuse_triple(first, body->prev);
7993 use_triple(body, body->prev);
7994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007995
Eric Biederman90089602004-05-28 14:11:54 +00007996 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7997 internal_error(state, val, "val not flattened?");
7998 }
7999
8000 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008001}
8002
Eric Biederman90089602004-05-28 14:11:54 +00008003
8004static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00008005 struct compile_state *state, struct triple *first, struct triple *ptr)
8006{
Eric Biederman90089602004-05-28 14:11:54 +00008007 if (!triple_is_part(state, ptr)) {
8008 internal_error(state, ptr, "not a part");
8009 }
8010 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
8011 internal_error(state, ptr, "unexpected args for: %d %s",
8012 ptr->op, tops(ptr->op));
8013 }
8014 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8015 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00008016 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008017}
8018
8019static struct triple *flatten(
8020 struct compile_state *state, struct triple *first, struct triple *ptr)
8021{
8022 struct triple *orig_ptr;
8023 if (!ptr)
8024 return 0;
8025 do {
8026 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008027 /* Only flatten triples once */
8028 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
8029 return ptr;
8030 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008031 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008032 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00008033 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
8034 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008035 break;
Eric Biederman90089602004-05-28 14:11:54 +00008036 case OP_PROG:
8037 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008038 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008039 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00008040 ptr = flatten_generic(state, first, ptr, 1);
8041 insert_triple(state, first, ptr);
8042 ptr->id |= TRIPLE_FLAG_FLATTENED;
8043 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8044 if (ptr->next != ptr) {
8045 use_triple(ptr->next, ptr);
8046 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008047 break;
8048 case OP_READ:
8049 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00008050 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
8051 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008052 break;
Eric Biederman90089602004-05-28 14:11:54 +00008053 case OP_WRITE:
8054 ptr = flatten_generic(state, first, ptr, 1);
8055 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8056 use_triple(MISC(ptr, 0), ptr);
8057 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008058 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00008059 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00008060 break;
8061 case OP_CBRANCH:
8062 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
8063 use_triple(RHS(ptr, 0), ptr);
8064 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00008065 insert_triple(state, first, ptr);
8066 ptr->id |= TRIPLE_FLAG_FLATTENED;
8067 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008068 if (ptr->next != ptr) {
8069 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008070 }
8071 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008072 case OP_CALL:
8073 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8074 use_triple(MISC(ptr, 0), ptr);
8075 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00008076 insert_triple(state, first, ptr);
8077 ptr->id |= TRIPLE_FLAG_FLATTENED;
8078 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008079 if (ptr->next != ptr) {
8080 use_triple(ptr->next, ptr);
8081 }
8082 break;
8083 case OP_RET:
8084 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
8085 use_triple(RHS(ptr, 0), ptr);
8086 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008087 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008088 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008089 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008090 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008091 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008092 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008093 break;
8094 case OP_DEREF:
8095 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00008096 ptr = RHS(ptr, 0);
8097 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008098 free_triple(state, orig_ptr);
8099 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008100 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00008101 if (RHS(ptr, 0)->op == OP_DEREF) {
8102 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00008103 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00008104 base = MISC(ptr, 0);
8105 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00008106 left = RHS(base, 0);
8107 ptr = triple(state, OP_ADD, left->type,
8108 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00008109 int_const(state, &ulong_type, offset));
8110 free_triple(state, base);
8111 }
Eric Biederman90089602004-05-28 14:11:54 +00008112 else {
8113 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008114 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008115 break;
Eric Biederman90089602004-05-28 14:11:54 +00008116 case OP_INDEX:
8117 if (RHS(ptr, 0)->op == OP_DEREF) {
8118 struct triple *base, *left;
8119 ulong_t offset;
8120 base = MISC(ptr, 0);
8121 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8122 left = RHS(base, 0);
8123 ptr = triple(state, OP_ADD, left->type,
8124 read_expr(state, left),
8125 int_const(state, &long_type, offset));
8126 free_triple(state, base);
8127 }
8128 else {
8129 ptr = flatten_part(state, first, ptr);
8130 }
8131 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008132 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008133 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008134 use_triple(ptr, MISC(ptr, 0));
8135 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008136 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008137 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8138 use_triple(MISC(ptr, 0), ptr);
8139 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008140 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008141 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008142 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8143 use_triple(MISC(ptr, 0), ptr);
8144 insert_triple(state, first, ptr);
8145 ptr->id |= TRIPLE_FLAG_FLATTENED;
8146 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8147 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008148 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008149 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008150 break;
8151 default:
8152 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008153 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008154 break;
8155 }
8156 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008157 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008158 insert_triple(state, first, ptr);
8159 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008160 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008162 return ptr;
8163}
8164
8165static void release_expr(struct compile_state *state, struct triple *expr)
8166{
8167 struct triple *head;
8168 head = label(state);
8169 flatten(state, head, expr);
8170 while(head->next != head) {
8171 release_triple(state, head->next);
8172 }
8173 free_triple(state, head);
8174}
8175
8176static int replace_rhs_use(struct compile_state *state,
8177 struct triple *orig, struct triple *new, struct triple *use)
8178{
8179 struct triple **expr;
8180 int found;
8181 found = 0;
8182 expr = triple_rhs(state, use, 0);
8183 for(;expr; expr = triple_rhs(state, use, expr)) {
8184 if (*expr == orig) {
8185 *expr = new;
8186 found = 1;
8187 }
8188 }
8189 if (found) {
8190 unuse_triple(orig, use);
8191 use_triple(new, use);
8192 }
8193 return found;
8194}
8195
8196static int replace_lhs_use(struct compile_state *state,
8197 struct triple *orig, struct triple *new, struct triple *use)
8198{
8199 struct triple **expr;
8200 int found;
8201 found = 0;
8202 expr = triple_lhs(state, use, 0);
8203 for(;expr; expr = triple_lhs(state, use, expr)) {
8204 if (*expr == orig) {
8205 *expr = new;
8206 found = 1;
8207 }
8208 }
8209 if (found) {
8210 unuse_triple(orig, use);
8211 use_triple(new, use);
8212 }
8213 return found;
8214}
8215
Eric Biederman90089602004-05-28 14:11:54 +00008216static int replace_misc_use(struct compile_state *state,
8217 struct triple *orig, struct triple *new, struct triple *use)
8218{
8219 struct triple **expr;
8220 int found;
8221 found = 0;
8222 expr = triple_misc(state, use, 0);
8223 for(;expr; expr = triple_misc(state, use, expr)) {
8224 if (*expr == orig) {
8225 *expr = new;
8226 found = 1;
8227 }
8228 }
8229 if (found) {
8230 unuse_triple(orig, use);
8231 use_triple(new, use);
8232 }
8233 return found;
8234}
8235
8236static int replace_targ_use(struct compile_state *state,
8237 struct triple *orig, struct triple *new, struct triple *use)
8238{
8239 struct triple **expr;
8240 int found;
8241 found = 0;
8242 expr = triple_targ(state, use, 0);
8243 for(;expr; expr = triple_targ(state, use, expr)) {
8244 if (*expr == orig) {
8245 *expr = new;
8246 found = 1;
8247 }
8248 }
8249 if (found) {
8250 unuse_triple(orig, use);
8251 use_triple(new, use);
8252 }
8253 return found;
8254}
8255
8256static void replace_use(struct compile_state *state,
8257 struct triple *orig, struct triple *new, struct triple *use)
8258{
8259 int found;
8260 found = 0;
8261 found |= replace_rhs_use(state, orig, new, use);
8262 found |= replace_lhs_use(state, orig, new, use);
8263 found |= replace_misc_use(state, orig, new, use);
8264 found |= replace_targ_use(state, orig, new, use);
8265 if (!found) {
8266 internal_error(state, use, "use without use");
8267 }
8268}
8269
Eric Biedermanb138ac82003-04-22 18:44:01 +00008270static void propogate_use(struct compile_state *state,
8271 struct triple *orig, struct triple *new)
8272{
8273 struct triple_set *user, *next;
8274 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008275 /* Careful replace_use modifies the use chain and
8276 * removes use. So we must get a copy of the next
8277 * entry early.
8278 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008279 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008280 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008281 }
8282 if (orig->use) {
8283 internal_error(state, orig, "used after propogate_use");
8284 }
8285}
8286
8287/*
8288 * Code generators
8289 * ===========================
8290 */
8291
Eric Biederman90089602004-05-28 14:11:54 +00008292static struct triple *mk_cast_expr(
8293 struct compile_state *state, struct type *type, struct triple *expr)
8294{
8295 struct triple *def;
8296 def = read_expr(state, expr);
8297 def = triple(state, OP_CONVERT, type, def, 0);
8298 return def;
8299}
8300
Eric Biedermanb138ac82003-04-22 18:44:01 +00008301static struct triple *mk_add_expr(
8302 struct compile_state *state, struct triple *left, struct triple *right)
8303{
8304 struct type *result_type;
8305 /* Put pointer operands on the left */
8306 if (is_pointer(right)) {
8307 struct triple *tmp;
8308 tmp = left;
8309 left = right;
8310 right = tmp;
8311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008312 left = read_expr(state, left);
8313 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008314 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008315 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008316 struct type *ptr_math;
8317 int op;
8318 if (is_signed(right->type)) {
8319 ptr_math = &long_type;
8320 op = OP_SMUL;
8321 } else {
8322 ptr_math = &ulong_type;
8323 op = OP_UMUL;
8324 }
8325 if (!equiv_types(right->type, ptr_math)) {
8326 right = mk_cast_expr(state, ptr_math, right);
8327 }
8328 right = triple(state, op, ptr_math, right,
8329 int_const(state, ptr_math,
8330 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008331 }
8332 return triple(state, OP_ADD, result_type, left, right);
8333}
8334
8335static struct triple *mk_sub_expr(
8336 struct compile_state *state, struct triple *left, struct triple *right)
8337{
8338 struct type *result_type;
8339 result_type = ptr_arithmetic_result(state, left, right);
8340 left = read_expr(state, left);
8341 right = read_expr(state, right);
8342 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008343 struct type *ptr_math;
8344 int op;
8345 if (is_signed(right->type)) {
8346 ptr_math = &long_type;
8347 op = OP_SMUL;
8348 } else {
8349 ptr_math = &ulong_type;
8350 op = OP_UMUL;
8351 }
8352 if (!equiv_types(right->type, ptr_math)) {
8353 right = mk_cast_expr(state, ptr_math, right);
8354 }
8355 right = triple(state, op, ptr_math, right,
8356 int_const(state, ptr_math,
8357 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008358 }
8359 return triple(state, OP_SUB, result_type, left, right);
8360}
8361
8362static struct triple *mk_pre_inc_expr(
8363 struct compile_state *state, struct triple *def)
8364{
8365 struct triple *val;
8366 lvalue(state, def);
8367 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8368 return triple(state, OP_VAL, def->type,
8369 write_expr(state, def, val),
8370 val);
8371}
8372
8373static struct triple *mk_pre_dec_expr(
8374 struct compile_state *state, struct triple *def)
8375{
8376 struct triple *val;
8377 lvalue(state, def);
8378 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8379 return triple(state, OP_VAL, def->type,
8380 write_expr(state, def, val),
8381 val);
8382}
8383
8384static struct triple *mk_post_inc_expr(
8385 struct compile_state *state, struct triple *def)
8386{
8387 struct triple *val;
8388 lvalue(state, def);
8389 val = read_expr(state, def);
8390 return triple(state, OP_VAL, def->type,
8391 write_expr(state, def,
8392 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8393 , val);
8394}
8395
8396static struct triple *mk_post_dec_expr(
8397 struct compile_state *state, struct triple *def)
8398{
8399 struct triple *val;
8400 lvalue(state, def);
8401 val = read_expr(state, def);
8402 return triple(state, OP_VAL, def->type,
8403 write_expr(state, def,
8404 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8405 , val);
8406}
8407
8408static struct triple *mk_subscript_expr(
8409 struct compile_state *state, struct triple *left, struct triple *right)
8410{
8411 left = read_expr(state, left);
8412 right = read_expr(state, right);
8413 if (!is_pointer(left) && !is_pointer(right)) {
8414 error(state, left, "subscripted value is not a pointer");
8415 }
8416 return mk_deref_expr(state, mk_add_expr(state, left, right));
8417}
8418
Eric Biedermane058a1e2003-07-12 01:21:31 +00008419
Eric Biedermanb138ac82003-04-22 18:44:01 +00008420/*
8421 * Compile time evaluation
8422 * ===========================
8423 */
8424static int is_const(struct triple *ins)
8425{
8426 return IS_CONST_OP(ins->op);
8427}
8428
Eric Biederman83b991a2003-10-11 06:20:25 +00008429static int is_simple_const(struct triple *ins)
8430{
Eric Biederman90089602004-05-28 14:11:54 +00008431 /* Is this a constant that u.cval has the value.
8432 * Or equivalently is this a constant that read_const
8433 * works on.
8434 * So far only OP_INTCONST qualifies.
8435 */
8436 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008437}
8438
Eric Biedermanb138ac82003-04-22 18:44:01 +00008439static int constants_equal(struct compile_state *state,
8440 struct triple *left, struct triple *right)
8441{
8442 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008443 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8444 equal = 0;
8445 }
8446 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008447 equal = 0;
8448 }
8449 else if (left->op != right->op) {
8450 equal = 0;
8451 }
8452 else if (!equiv_types(left->type, right->type)) {
8453 equal = 0;
8454 }
8455 else {
8456 equal = 0;
8457 switch(left->op) {
8458 case OP_INTCONST:
8459 if (left->u.cval == right->u.cval) {
8460 equal = 1;
8461 }
8462 break;
8463 case OP_BLOBCONST:
8464 {
Eric Biederman90089602004-05-28 14:11:54 +00008465 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008466 lsize = size_of(state, left->type);
8467 rsize = size_of(state, right->type);
8468 if (lsize != rsize) {
8469 break;
8470 }
Eric Biederman90089602004-05-28 14:11:54 +00008471 bytes = bits_to_bytes(lsize);
8472 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008473 equal = 1;
8474 }
8475 break;
8476 }
8477 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008478 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008479 (left->u.cval == right->u.cval)) {
8480 equal = 1;
8481 }
8482 break;
8483 default:
8484 internal_error(state, left, "uknown constant type");
8485 break;
8486 }
8487 }
8488 return equal;
8489}
8490
8491static int is_zero(struct triple *ins)
8492{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008493 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008494}
8495
8496static int is_one(struct triple *ins)
8497{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008498 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008499}
8500
Eric Biederman530b5192003-07-01 10:05:30 +00008501static long_t bit_count(ulong_t value)
8502{
8503 int count;
8504 int i;
8505 count = 0;
8506 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8507 ulong_t mask;
8508 mask = 1;
8509 mask <<= i;
8510 if (value & mask) {
8511 count++;
8512 }
8513 }
8514 return count;
8515
8516}
Eric Biedermanb138ac82003-04-22 18:44:01 +00008517static long_t bsr(ulong_t value)
8518{
8519 int i;
8520 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8521 ulong_t mask;
8522 mask = 1;
8523 mask <<= i;
8524 if (value & mask) {
8525 return i;
8526 }
8527 }
8528 return -1;
8529}
8530
8531static long_t bsf(ulong_t value)
8532{
8533 int i;
8534 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8535 ulong_t mask;
8536 mask = 1;
8537 mask <<= 1;
8538 if (value & mask) {
8539 return i;
8540 }
8541 }
8542 return -1;
8543}
8544
8545static long_t log2(ulong_t value)
8546{
8547 return bsr(value);
8548}
8549
8550static long_t tlog2(struct triple *ins)
8551{
8552 return log2(ins->u.cval);
8553}
8554
8555static int is_pow2(struct triple *ins)
8556{
8557 ulong_t value, mask;
8558 long_t log;
8559 if (!is_const(ins)) {
8560 return 0;
8561 }
8562 value = ins->u.cval;
8563 log = log2(value);
8564 if (log == -1) {
8565 return 0;
8566 }
8567 mask = 1;
8568 mask <<= log;
8569 return ((value & mask) == value);
8570}
8571
8572static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008573 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008574{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008575 switch(rhs->type->type &TYPE_MASK) {
8576 case TYPE_CHAR:
8577 case TYPE_SHORT:
8578 case TYPE_INT:
8579 case TYPE_LONG:
8580 case TYPE_UCHAR:
8581 case TYPE_USHORT:
8582 case TYPE_UINT:
8583 case TYPE_ULONG:
8584 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008585 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008586 break;
8587 default:
Eric Biederman90089602004-05-28 14:11:54 +00008588 fprintf(state->errout, "type: ");
8589 name_of(state->errout, rhs->type);
8590 fprintf(state->errout, "\n");
8591 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008592 break;
8593 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008594 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008595 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008596 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008597 return rhs->u.cval;
8598}
8599
Eric Biederman5ade04a2003-10-22 04:03:46 +00008600static long_t read_sconst(struct compile_state *state,
8601 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008602{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008603 return (long_t)(rhs->u.cval);
8604}
8605
Eric Biederman5ade04a2003-10-22 04:03:46 +00008606int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8607{
8608 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008609 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008610 }
8611 return !is_zero(rhs);
8612}
8613
8614int const_eq(struct compile_state *state, struct triple *ins,
8615 struct triple *left, struct triple *right)
8616{
8617 int result;
8618 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008619 internal_warning(state, ins, "non const passed to const_eq");
8620 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008621 }
8622 else if (left == right) {
8623 result = 1;
8624 }
8625 else if (is_simple_const(left) && is_simple_const(right)) {
8626 ulong_t lval, rval;
8627 lval = read_const(state, ins, left);
8628 rval = read_const(state, ins, right);
8629 result = (lval == rval);
8630 }
8631 else if ((left->op == OP_ADDRCONST) &&
8632 (right->op == OP_ADDRCONST)) {
8633 result = (MISC(left, 0) == MISC(right, 0)) &&
8634 (left->u.cval == right->u.cval);
8635 }
8636 else {
Eric Biederman90089602004-05-28 14:11:54 +00008637 internal_warning(state, ins, "incomparable constants passed to const_eq");
8638 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008639 }
8640 return result;
8641
8642}
8643
8644int const_ucmp(struct compile_state *state, struct triple *ins,
8645 struct triple *left, struct triple *right)
8646{
8647 int result;
8648 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008649 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008650 result = -2;
8651 }
8652 else if (left == right) {
8653 result = 0;
8654 }
8655 else if (is_simple_const(left) && is_simple_const(right)) {
8656 ulong_t lval, rval;
8657 lval = read_const(state, ins, left);
8658 rval = read_const(state, ins, right);
8659 result = 0;
8660 if (lval > rval) {
8661 result = 1;
8662 } else if (rval > lval) {
8663 result = -1;
8664 }
8665 }
8666 else if ((left->op == OP_ADDRCONST) &&
8667 (right->op == OP_ADDRCONST) &&
8668 (MISC(left, 0) == MISC(right, 0))) {
8669 result = 0;
8670 if (left->u.cval > right->u.cval) {
8671 result = 1;
8672 } else if (left->u.cval < right->u.cval) {
8673 result = -1;
8674 }
8675 }
8676 else {
Eric Biederman90089602004-05-28 14:11:54 +00008677 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008678 result = -2;
8679 }
8680 return result;
8681}
8682
8683int const_scmp(struct compile_state *state, struct triple *ins,
8684 struct triple *left, struct triple *right)
8685{
8686 int result;
8687 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008688 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008689 result = -2;
8690 }
8691 else if (left == right) {
8692 result = 0;
8693 }
8694 else if (is_simple_const(left) && is_simple_const(right)) {
8695 long_t lval, rval;
8696 lval = read_sconst(state, ins, left);
8697 rval = read_sconst(state, ins, right);
8698 result = 0;
8699 if (lval > rval) {
8700 result = 1;
8701 } else if (rval > lval) {
8702 result = -1;
8703 }
8704 }
8705 else {
Eric Biederman90089602004-05-28 14:11:54 +00008706 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008707 result = -2;
8708 }
8709 return result;
8710}
8711
Eric Biedermanb138ac82003-04-22 18:44:01 +00008712static void unuse_rhs(struct compile_state *state, struct triple *ins)
8713{
8714 struct triple **expr;
8715 expr = triple_rhs(state, ins, 0);
8716 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008717 if (*expr) {
8718 unuse_triple(*expr, ins);
8719 *expr = 0;
8720 }
8721 }
8722}
8723
8724static void unuse_lhs(struct compile_state *state, struct triple *ins)
8725{
8726 struct triple **expr;
8727 expr = triple_lhs(state, ins, 0);
8728 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008729 unuse_triple(*expr, ins);
8730 *expr = 0;
8731 }
8732}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008733
Eric Biederman90089602004-05-28 14:11:54 +00008734static void unuse_misc(struct compile_state *state, struct triple *ins)
8735{
8736 struct triple **expr;
8737 expr = triple_misc(state, ins, 0);
8738 for(;expr;expr = triple_misc(state, ins, expr)) {
8739 unuse_triple(*expr, ins);
8740 *expr = 0;
8741 }
8742}
8743
8744static void unuse_targ(struct compile_state *state, struct triple *ins)
8745{
8746 int i;
8747 struct triple **slot;
8748 slot = &TARG(ins, 0);
8749 for(i = 0; i < ins->targ; i++) {
8750 unuse_triple(slot[i], ins);
8751 slot[i] = 0;
8752 }
8753}
8754
Eric Biedermanb138ac82003-04-22 18:44:01 +00008755static void check_lhs(struct compile_state *state, struct triple *ins)
8756{
8757 struct triple **expr;
8758 expr = triple_lhs(state, ins, 0);
8759 for(;expr;expr = triple_lhs(state, ins, expr)) {
8760 internal_error(state, ins, "unexpected lhs");
8761 }
8762
8763}
Eric Biederman90089602004-05-28 14:11:54 +00008764
8765static void check_misc(struct compile_state *state, struct triple *ins)
8766{
8767 struct triple **expr;
8768 expr = triple_misc(state, ins, 0);
8769 for(;expr;expr = triple_misc(state, ins, expr)) {
8770 if (*expr) {
8771 internal_error(state, ins, "unexpected misc");
8772 }
8773 }
8774}
8775
Eric Biedermanb138ac82003-04-22 18:44:01 +00008776static void check_targ(struct compile_state *state, struct triple *ins)
8777{
8778 struct triple **expr;
8779 expr = triple_targ(state, ins, 0);
8780 for(;expr;expr = triple_targ(state, ins, expr)) {
8781 internal_error(state, ins, "unexpected targ");
8782 }
8783}
8784
8785static void wipe_ins(struct compile_state *state, struct triple *ins)
8786{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008787 /* Becareful which instructions you replace the wiped
8788 * instruction with, as there are not enough slots
8789 * in all instructions to hold all others.
8790 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008791 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008792 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008793 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008794 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008795 ins->lhs = 0;
8796 ins->rhs = 0;
8797 ins->misc = 0;
8798 ins->targ = 0;
8799}
8800
8801static void wipe_branch(struct compile_state *state, struct triple *ins)
8802{
8803 /* Becareful which instructions you replace the wiped
8804 * instruction with, as there are not enough slots
8805 * in all instructions to hold all others.
8806 */
8807 unuse_rhs(state, ins);
8808 unuse_lhs(state, ins);
8809 unuse_misc(state, ins);
8810 unuse_targ(state, ins);
8811 ins->lhs = 0;
8812 ins->rhs = 0;
8813 ins->misc = 0;
8814 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008815}
8816
8817static void mkcopy(struct compile_state *state,
8818 struct triple *ins, struct triple *rhs)
8819{
Eric Biederman83b991a2003-10-11 06:20:25 +00008820 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008821 if (!equiv_types(ins->type, rhs->type)) {
8822 FILE *fp = state->errout;
8823 fprintf(fp, "src type: ");
8824 name_of(fp, rhs->type);
8825 fprintf(fp, "\ndst type: ");
8826 name_of(fp, ins->type);
8827 fprintf(fp, "\n");
8828 internal_error(state, ins, "mkcopy type mismatch");
8829 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008830 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008831 wipe_ins(state, ins);
8832 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008833 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008834 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008835 RHS(ins, 0) = rhs;
8836 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008837}
8838
8839static void mkconst(struct compile_state *state,
8840 struct triple *ins, ulong_t value)
8841{
8842 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008843 fprintf(state->errout, "type: ");
8844 name_of(state->errout, ins->type);
8845 fprintf(state->errout, "\n");
8846 internal_error(state, ins, "unknown type to make constant value: %ld",
8847 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008848 }
8849 wipe_ins(state, ins);
8850 ins->op = OP_INTCONST;
8851 ins->u.cval = value;
8852}
8853
8854static void mkaddr_const(struct compile_state *state,
8855 struct triple *ins, struct triple *sdecl, ulong_t value)
8856{
Eric Biederman90089602004-05-28 14:11:54 +00008857 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008858 internal_error(state, ins, "bad base for addrconst");
8859 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008860 wipe_ins(state, ins);
8861 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008862 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008863 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008864 ins->u.cval = value;
8865 use_triple(sdecl, ins);
8866}
8867
Eric Biederman90089602004-05-28 14:11:54 +00008868#if DEBUG_DECOMPOSE_PRINT_TUPLES
8869static void print_tuple(struct compile_state *state,
8870 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008871{
Eric Biederman90089602004-05-28 14:11:54 +00008872 FILE *fp = state->dbgout;
8873 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8874 name_of(fp, tuple->type);
8875 if (tuple->lhs > 0) {
8876 fprintf(fp, " lhs: ");
8877 name_of(fp, LHS(tuple, 0)->type);
8878 }
8879 fprintf(fp, "\n");
8880
8881}
8882#endif
8883
8884static struct triple *decompose_with_tuple(struct compile_state *state,
8885 struct triple *ins, struct triple *tuple)
8886{
8887 struct triple *next;
8888 next = ins->next;
8889 flatten(state, next, tuple);
8890#if DEBUG_DECOMPOSE_PRINT_TUPLES
8891 print_tuple(state, ins, tuple);
8892#endif
8893
8894 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8895 struct triple *tmp;
8896 if (tuple->lhs != 1) {
8897 internal_error(state, tuple, "plain type in multiple registers?");
8898 }
8899 tmp = LHS(tuple, 0);
8900 release_triple(state, tuple);
8901 tuple = tmp;
8902 }
8903
8904 propogate_use(state, ins, tuple);
8905 release_triple(state, ins);
8906
8907 return next;
8908}
8909
8910static struct triple *decompose_unknownval(struct compile_state *state,
8911 struct triple *ins)
8912{
8913 struct triple *tuple;
8914 ulong_t i;
8915
8916#if DEBUG_DECOMPOSE_HIRES
8917 FILE *fp = state->dbgout;
8918 fprintf(fp, "unknown type: ");
8919 name_of(fp, ins->type);
8920 fprintf(fp, "\n");
8921#endif
8922
8923 get_occurance(ins->occurance);
8924 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8925 ins->occurance);
8926
8927 for(i = 0; i < tuple->lhs; i++) {
8928 struct type *piece_type;
8929 struct triple *unknown;
8930
8931 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8932 get_occurance(tuple->occurance);
8933 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8934 tuple->occurance);
8935 LHS(tuple, i) = unknown;
8936 }
8937 return decompose_with_tuple(state, ins, tuple);
8938}
8939
8940
8941static struct triple *decompose_read(struct compile_state *state,
8942 struct triple *ins)
8943{
8944 struct triple *tuple, *lval;
8945 ulong_t i;
8946
8947 lval = RHS(ins, 0);
8948
8949 if (lval->op == OP_PIECE) {
8950 return ins->next;
8951 }
8952 get_occurance(ins->occurance);
8953 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8954 ins->occurance);
8955
8956 if ((tuple->lhs != lval->lhs) &&
8957 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
8958 {
8959 internal_error(state, ins, "lhs size inconsistency?");
8960 }
8961 for(i = 0; i < tuple->lhs; i++) {
8962 struct triple *piece, *read, *bitref;
8963 if ((i != 0) || !triple_is_def(state, lval)) {
8964 piece = LHS(lval, i);
8965 } else {
8966 piece = lval;
8967 }
8968
8969 /* See if the piece is really a bitref */
8970 bitref = 0;
8971 if (piece->op == OP_BITREF) {
8972 bitref = piece;
8973 piece = RHS(bitref, 0);
8974 }
8975
8976 get_occurance(tuple->occurance);
8977 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8978 tuple->occurance);
8979 RHS(read, 0) = piece;
8980
8981 if (bitref) {
8982 struct triple *extract;
8983 int op;
8984 if (is_signed(bitref->type->left)) {
8985 op = OP_SEXTRACT;
8986 } else {
8987 op = OP_UEXTRACT;
8988 }
8989 get_occurance(tuple->occurance);
8990 extract = alloc_triple(state, op, bitref->type, -1, -1,
8991 tuple->occurance);
8992 RHS(extract, 0) = read;
8993 extract->u.bitfield.size = bitref->u.bitfield.size;
8994 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8995
8996 read = extract;
8997 }
8998
8999 LHS(tuple, i) = read;
9000 }
9001 return decompose_with_tuple(state, ins, tuple);
9002}
9003
9004static struct triple *decompose_write(struct compile_state *state,
9005 struct triple *ins)
9006{
9007 struct triple *tuple, *lval, *val;
9008 ulong_t i;
9009
9010 lval = MISC(ins, 0);
9011 val = RHS(ins, 0);
9012 get_occurance(ins->occurance);
9013 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9014 ins->occurance);
9015
9016 if ((tuple->lhs != lval->lhs) &&
9017 (!triple_is_def(state, lval) || tuple->lhs != 1))
9018 {
9019 internal_error(state, ins, "lhs size inconsistency?");
9020 }
9021 for(i = 0; i < tuple->lhs; i++) {
9022 struct triple *piece, *write, *pval, *bitref;
9023 if ((i != 0) || !triple_is_def(state, lval)) {
9024 piece = LHS(lval, i);
9025 } else {
9026 piece = lval;
9027 }
9028 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
9029 pval = val;
9030 }
9031 else {
9032 if (i > val->lhs) {
9033 internal_error(state, ins, "lhs size inconsistency?");
9034 }
9035 pval = LHS(val, i);
9036 }
9037
9038 /* See if the piece is really a bitref */
9039 bitref = 0;
9040 if (piece->op == OP_BITREF) {
9041 struct triple *read, *deposit;
9042 bitref = piece;
9043 piece = RHS(bitref, 0);
9044
9045 /* Read the destination register */
9046 get_occurance(tuple->occurance);
9047 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
9048 tuple->occurance);
9049 RHS(read, 0) = piece;
9050
9051 /* Deposit the new bitfield value */
9052 get_occurance(tuple->occurance);
9053 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
9054 tuple->occurance);
9055 RHS(deposit, 0) = read;
9056 RHS(deposit, 1) = pval;
9057 deposit->u.bitfield.size = bitref->u.bitfield.size;
9058 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
9059
9060 /* Now write the newly generated value */
9061 pval = deposit;
9062 }
9063
9064 get_occurance(tuple->occurance);
9065 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
9066 tuple->occurance);
9067 MISC(write, 0) = piece;
9068 RHS(write, 0) = pval;
9069 LHS(tuple, i) = write;
9070 }
9071 return decompose_with_tuple(state, ins, tuple);
9072}
9073
9074struct decompose_load_info {
9075 struct occurance *occurance;
9076 struct triple *lval;
9077 struct triple *tuple;
9078};
9079static void decompose_load_cb(struct compile_state *state,
9080 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9081{
9082 struct decompose_load_info *info = arg;
9083 struct triple *load;
9084
9085 if (reg_offset > info->tuple->lhs) {
9086 internal_error(state, info->tuple, "lhs to small?");
9087 }
9088 get_occurance(info->occurance);
9089 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
9090 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
9091 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
9092}
9093
9094static struct triple *decompose_load(struct compile_state *state,
9095 struct triple *ins)
9096{
9097 struct triple *tuple;
9098 struct decompose_load_info info;
9099
9100 if (!is_compound_type(ins->type)) {
9101 return ins->next;
9102 }
9103 get_occurance(ins->occurance);
9104 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9105 ins->occurance);
9106
9107 info.occurance = ins->occurance;
9108 info.lval = RHS(ins, 0);
9109 info.tuple = tuple;
9110 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9111
9112 return decompose_with_tuple(state, ins, tuple);
9113}
9114
9115
9116struct decompose_store_info {
9117 struct occurance *occurance;
9118 struct triple *lval;
9119 struct triple *val;
9120 struct triple *tuple;
9121};
9122static void decompose_store_cb(struct compile_state *state,
9123 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9124{
9125 struct decompose_store_info *info = arg;
9126 struct triple *store;
9127
9128 if (reg_offset > info->tuple->lhs) {
9129 internal_error(state, info->tuple, "lhs to small?");
9130 }
9131 get_occurance(info->occurance);
9132 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9133 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9134 RHS(store, 1) = LHS(info->val, reg_offset);
9135 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9136}
9137
9138static struct triple *decompose_store(struct compile_state *state,
9139 struct triple *ins)
9140{
9141 struct triple *tuple;
9142 struct decompose_store_info info;
9143
9144 if (!is_compound_type(ins->type)) {
9145 return ins->next;
9146 }
9147 get_occurance(ins->occurance);
9148 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9149 ins->occurance);
9150
9151 info.occurance = ins->occurance;
9152 info.lval = RHS(ins, 0);
9153 info.val = RHS(ins, 1);
9154 info.tuple = tuple;
9155 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9156
9157 return decompose_with_tuple(state, ins, tuple);
9158}
9159
9160static struct triple *decompose_dot(struct compile_state *state,
9161 struct triple *ins)
9162{
9163 struct triple *tuple, *lval;
9164 struct type *type;
9165 size_t reg_offset;
9166 int i, idx;
9167
9168 lval = MISC(ins, 0);
9169 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9170 idx = reg_offset/REG_SIZEOF_REG;
9171 type = field_type(state, lval->type, ins->u.field);
9172#if DEBUG_DECOMPOSE_HIRES
9173 {
9174 FILE *fp = state->dbgout;
9175 fprintf(fp, "field type: ");
9176 name_of(fp, type);
9177 fprintf(fp, "\n");
9178 }
9179#endif
9180
9181 get_occurance(ins->occurance);
9182 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9183 ins->occurance);
9184
9185 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9186 (tuple->lhs != 1))
9187 {
9188 internal_error(state, ins, "multi register bitfield?");
9189 }
9190
9191 for(i = 0; i < tuple->lhs; i++, idx++) {
9192 struct triple *piece;
9193 if (!triple_is_def(state, lval)) {
9194 if (idx > lval->lhs) {
9195 internal_error(state, ins, "inconsistent lhs count");
9196 }
9197 piece = LHS(lval, idx);
9198 } else {
9199 if (idx != 0) {
9200 internal_error(state, ins, "bad reg_offset into def");
9201 }
9202 if (i != 0) {
9203 internal_error(state, ins, "bad reg count from def");
9204 }
9205 piece = lval;
9206 }
9207
9208 /* Remember the offset of the bitfield */
9209 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9210 get_occurance(ins->occurance);
9211 piece = build_triple(state, OP_BITREF, type, piece, 0,
9212 ins->occurance);
9213 piece->u.bitfield.size = size_of(state, type);
9214 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9215 }
9216 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
9217 internal_error(state, ins,
9218 "request for a nonbitfield sub register?");
9219 }
9220
9221 LHS(tuple, i) = piece;
9222 }
9223
9224 return decompose_with_tuple(state, ins, tuple);
9225}
9226
9227static struct triple *decompose_index(struct compile_state *state,
9228 struct triple *ins)
9229{
9230 struct triple *tuple, *lval;
9231 struct type *type;
9232 int i, idx;
9233
9234 lval = MISC(ins, 0);
9235 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9236 type = index_type(state, lval->type, ins->u.cval);
9237#if DEBUG_DECOMPOSE_HIRES
9238{
9239 FILE *fp = state->dbgout;
9240 fprintf(fp, "index type: ");
9241 name_of(fp, type);
9242 fprintf(fp, "\n");
9243}
9244#endif
9245
9246 get_occurance(ins->occurance);
9247 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9248 ins->occurance);
9249
9250 for(i = 0; i < tuple->lhs; i++, idx++) {
9251 struct triple *piece;
9252 if (!triple_is_def(state, lval)) {
9253 if (idx > lval->lhs) {
9254 internal_error(state, ins, "inconsistent lhs count");
9255 }
9256 piece = LHS(lval, idx);
9257 } else {
9258 if (idx != 0) {
9259 internal_error(state, ins, "bad reg_offset into def");
9260 }
9261 if (i != 0) {
9262 internal_error(state, ins, "bad reg count from def");
9263 }
9264 piece = lval;
9265 }
9266 LHS(tuple, i) = piece;
9267 }
9268
9269 return decompose_with_tuple(state, ins, tuple);
9270}
9271
9272static void decompose_compound_types(struct compile_state *state)
9273{
9274 struct triple *ins, *next, *first;
9275 FILE *fp;
9276 fp = state->dbgout;
Eric Biederman83b991a2003-10-11 06:20:25 +00009277 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009278 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009279
9280 /* Pass one expand compound values into pseudo registers.
9281 */
9282 next = first;
9283 do {
9284 ins = next;
9285 next = ins->next;
9286 switch(ins->op) {
9287 case OP_UNKNOWNVAL:
9288 next = decompose_unknownval(state, ins);
9289 break;
9290
9291 case OP_READ:
9292 next = decompose_read(state, ins);
9293 break;
9294
9295 case OP_WRITE:
9296 next = decompose_write(state, ins);
9297 break;
9298
9299
9300 /* Be very careful with the load/store logic. These
9301 * operations must convert from the in register layout
9302 * to the in memory layout, which is nontrivial.
9303 */
9304 case OP_LOAD:
9305 next = decompose_load(state, ins);
9306 break;
9307 case OP_STORE:
9308 next = decompose_store(state, ins);
9309 break;
9310
9311 case OP_DOT:
9312 next = decompose_dot(state, ins);
9313 break;
9314 case OP_INDEX:
9315 next = decompose_index(state, ins);
9316 break;
9317
9318 }
9319#if DEBUG_DECOMPOSE_HIRES
9320 fprintf(fp, "decompose next: %p \n", next);
9321 fflush(fp);
9322 fprintf(fp, "next->op: %d %s\n",
9323 next->op, tops(next->op));
9324 /* High resolution debugging mode */
9325 print_triples(state);
9326#endif
9327 } while (next != first);
9328
9329 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009330 */
9331 ins = first;
9332 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009333 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009334 if (ins->op == OP_TUPLE) {
9335 if (ins->use) {
9336 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009337 }
Eric Biederman90089602004-05-28 14:11:54 +00009338 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009339 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009340 }
Eric Biederman90089602004-05-28 14:11:54 +00009341 }
9342 ins = next;
9343 } while(ins != first);
9344 ins = first;
9345 do {
9346 next = ins->next;
9347 if (ins->op == OP_BITREF) {
9348 if (ins->use) {
9349 internal_error(state, ins, "bitref used");
9350 }
9351 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009352 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009353 }
9354 }
9355 ins = next;
9356 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009357
Eric Biederman0babc1c2003-05-09 02:39:00 +00009358 /* Pass three verify the state and set ->id to 0.
9359 */
Eric Biederman90089602004-05-28 14:11:54 +00009360 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009361 do {
Eric Biederman90089602004-05-28 14:11:54 +00009362 ins = next;
9363 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009364 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009365 if (triple_stores_block(state, ins)) {
9366 ins->u.block = 0;
9367 }
9368 if (triple_is_def(state, ins)) {
9369 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9370 internal_error(state, ins, "multi register value remains?");
9371 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009372 }
9373 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009374 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009375 }
Eric Biederman90089602004-05-28 14:11:54 +00009376 if (ins->op == OP_INDEX) {
9377 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009378 }
Eric Biederman90089602004-05-28 14:11:54 +00009379 if (ins->op == OP_BITREF) {
9380 internal_error(state, ins, "OP_BITREF remains?");
9381 }
9382 if (ins->op == OP_TUPLE) {
9383 internal_error(state, ins, "OP_TUPLE remains?");
9384 }
9385 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009386}
9387
Eric Biedermanb138ac82003-04-22 18:44:01 +00009388/* For those operations that cannot be simplified */
9389static void simplify_noop(struct compile_state *state, struct triple *ins)
9390{
9391 return;
9392}
9393
9394static void simplify_smul(struct compile_state *state, struct triple *ins)
9395{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009396 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009397 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009398 tmp = RHS(ins, 0);
9399 RHS(ins, 0) = RHS(ins, 1);
9400 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009401 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009402 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009403 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009404 left = read_sconst(state, ins, RHS(ins, 0));
9405 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009406 mkconst(state, ins, left * right);
9407 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009408 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009409 mkconst(state, ins, 0);
9410 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009411 else if (is_one(RHS(ins, 1))) {
9412 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009413 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009414 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009415 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009418 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009419 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009420 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009421 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 }
9423}
9424
9425static void simplify_umul(struct compile_state *state, struct triple *ins)
9426{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009427 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009429 tmp = RHS(ins, 0);
9430 RHS(ins, 0) = RHS(ins, 1);
9431 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009432 }
Eric Biederman90089602004-05-28 14:11:54 +00009433 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009434 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009435 left = read_const(state, ins, RHS(ins, 0));
9436 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437 mkconst(state, ins, left * right);
9438 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009439 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440 mkconst(state, ins, 0);
9441 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009442 else if (is_one(RHS(ins, 1))) {
9443 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009444 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009445 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009446 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009447 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009448 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009449 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009450 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009451 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009452 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009453 }
9454}
9455
9456static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9457{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009459 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009460 left = read_sconst(state, ins, RHS(ins, 0));
9461 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009462 mkconst(state, ins, left / right);
9463 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009464 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009465 mkconst(state, ins, 0);
9466 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009467 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009468 error(state, ins, "division by zero");
9469 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009470 else if (is_one(RHS(ins, 1))) {
9471 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009473 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009474 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009475 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009476 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009477 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009478 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009479 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009480 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009481 }
9482}
9483
9484static void simplify_udiv(struct compile_state *state, struct triple *ins)
9485{
Eric Biederman90089602004-05-28 14:11:54 +00009486 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009487 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009488 left = read_const(state, ins, RHS(ins, 0));
9489 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009490 mkconst(state, ins, left / right);
9491 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009492 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009493 mkconst(state, ins, 0);
9494 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009495 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009496 error(state, ins, "division by zero");
9497 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009498 else if (is_one(RHS(ins, 1))) {
9499 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009500 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009501 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009502 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009503 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009504 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009505 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009506 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009507 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009508 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009509 }
9510}
9511
9512static void simplify_smod(struct compile_state *state, struct triple *ins)
9513{
Eric Biederman90089602004-05-28 14:11:54 +00009514 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009515 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009516 left = read_const(state, ins, RHS(ins, 0));
9517 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009518 mkconst(state, ins, left % right);
9519 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009520 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009521 mkconst(state, ins, 0);
9522 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009523 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009524 error(state, ins, "division by zero");
9525 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009526 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009527 mkconst(state, ins, 0);
9528 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009529 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009530 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009531 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009532 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009533 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009534 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009535 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009536 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009537 }
9538}
Eric Biederman83b991a2003-10-11 06:20:25 +00009539
Eric Biedermanb138ac82003-04-22 18:44:01 +00009540static void simplify_umod(struct compile_state *state, struct triple *ins)
9541{
Eric Biederman90089602004-05-28 14:11:54 +00009542 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009543 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009544 left = read_const(state, ins, RHS(ins, 0));
9545 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009546 mkconst(state, ins, left % right);
9547 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009548 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 mkconst(state, ins, 0);
9550 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009551 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009552 error(state, ins, "division by zero");
9553 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009554 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009555 mkconst(state, ins, 0);
9556 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009557 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009558 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009559 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009560 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009561 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009562 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009563 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009564 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009565 }
9566}
9567
9568static void simplify_add(struct compile_state *state, struct triple *ins)
9569{
9570 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009571 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009572 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009573 tmp = RHS(ins, 0);
9574 RHS(ins, 0) = RHS(ins, 1);
9575 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009576 }
Eric Biederman90089602004-05-28 14:11:54 +00009577 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009578 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009579 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009580 left = read_const(state, ins, RHS(ins, 0));
9581 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009582 mkconst(state, ins, left + right);
9583 }
Eric Biederman530b5192003-07-01 10:05:30 +00009584 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009585 struct triple *sdecl;
9586 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009587 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009588 left = RHS(ins, 0)->u.cval;
9589 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009590 mkaddr_const(state, ins, sdecl, left + right);
9591 }
Eric Biederman530b5192003-07-01 10:05:30 +00009592 else {
9593 internal_warning(state, ins, "Optimize me!");
9594 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009595 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009596 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009597 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009598 tmp = RHS(ins, 1);
9599 RHS(ins, 1) = RHS(ins, 0);
9600 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009601 }
9602}
9603
9604static void simplify_sub(struct compile_state *state, struct triple *ins)
9605{
Eric Biederman90089602004-05-28 14:11:54 +00009606 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009607 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009608 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009609 left = read_const(state, ins, RHS(ins, 0));
9610 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009611 mkconst(state, ins, left - right);
9612 }
Eric Biederman530b5192003-07-01 10:05:30 +00009613 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009614 struct triple *sdecl;
9615 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009616 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009617 left = RHS(ins, 0)->u.cval;
9618 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009619 mkaddr_const(state, ins, sdecl, left - right);
9620 }
Eric Biederman530b5192003-07-01 10:05:30 +00009621 else {
9622 internal_warning(state, ins, "Optimize me!");
9623 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009624 }
9625}
9626
9627static void simplify_sl(struct compile_state *state, struct triple *ins)
9628{
Eric Biederman90089602004-05-28 14:11:54 +00009629 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009630 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009631 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009632 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009633 warning(state, ins, "left shift count >= width of type");
9634 }
9635 }
Eric Biederman90089602004-05-28 14:11:54 +00009636 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009637 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009638 left = read_const(state, ins, RHS(ins, 0));
9639 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009640 mkconst(state, ins, left << right);
9641 }
9642}
9643
9644static void simplify_usr(struct compile_state *state, struct triple *ins)
9645{
Eric Biederman90089602004-05-28 14:11:54 +00009646 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009647 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009648 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009649 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009650 warning(state, ins, "right shift count >= width of type");
9651 }
9652 }
Eric Biederman90089602004-05-28 14:11:54 +00009653 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009654 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009655 left = read_const(state, ins, RHS(ins, 0));
9656 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009657 mkconst(state, ins, left >> right);
9658 }
9659}
9660
9661static void simplify_ssr(struct compile_state *state, struct triple *ins)
9662{
Eric Biederman90089602004-05-28 14:11:54 +00009663 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009664 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009665 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009666 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009667 warning(state, ins, "right shift count >= width of type");
9668 }
9669 }
Eric Biederman90089602004-05-28 14:11:54 +00009670 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009671 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009672 left = read_sconst(state, ins, RHS(ins, 0));
9673 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009674 mkconst(state, ins, left >> right);
9675 }
9676}
9677
9678static void simplify_and(struct compile_state *state, struct triple *ins)
9679{
Eric Biederman90089602004-05-28 14:11:54 +00009680 struct triple *left, *right;
9681 left = RHS(ins, 0);
9682 right = RHS(ins, 1);
9683
9684 if (is_simple_const(left) && is_simple_const(right)) {
9685 ulong_t lval, rval;
9686 lval = read_const(state, ins, left);
9687 rval = read_const(state, ins, right);
9688 mkconst(state, ins, lval & rval);
9689 }
9690 else if (is_zero(right) || is_zero(left)) {
9691 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009692 }
9693}
9694
9695static void simplify_or(struct compile_state *state, struct triple *ins)
9696{
Eric Biederman90089602004-05-28 14:11:54 +00009697 struct triple *left, *right;
9698 left = RHS(ins, 0);
9699 right = RHS(ins, 1);
9700
9701 if (is_simple_const(left) && is_simple_const(right)) {
9702 ulong_t lval, rval;
9703 lval = read_const(state, ins, left);
9704 rval = read_const(state, ins, right);
9705 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009706 }
Eric Biederman90089602004-05-28 14:11:54 +00009707#if 0 /* I need to handle type mismatches here... */
9708 else if (is_zero(right)) {
9709 mkcopy(state, ins, left);
9710 }
9711 else if (is_zero(left)) {
9712 mkcopy(state, ins, right);
9713 }
9714#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009715}
9716
9717static void simplify_xor(struct compile_state *state, struct triple *ins)
9718{
Eric Biederman90089602004-05-28 14:11:54 +00009719 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009720 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009721 left = read_const(state, ins, RHS(ins, 0));
9722 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009723 mkconst(state, ins, left ^ right);
9724 }
9725}
9726
9727static void simplify_pos(struct compile_state *state, struct triple *ins)
9728{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009729 if (is_const(RHS(ins, 0))) {
9730 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009731 }
9732 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009733 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009734 }
9735}
9736
9737static void simplify_neg(struct compile_state *state, struct triple *ins)
9738{
Eric Biederman90089602004-05-28 14:11:54 +00009739 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009740 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009741 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009742 mkconst(state, ins, -left);
9743 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009744 else if (RHS(ins, 0)->op == OP_NEG) {
9745 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009746 }
9747}
9748
9749static void simplify_invert(struct compile_state *state, struct triple *ins)
9750{
Eric Biederman90089602004-05-28 14:11:54 +00009751 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009752 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009753 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009754 mkconst(state, ins, ~left);
9755 }
9756}
9757
9758static void simplify_eq(struct compile_state *state, struct triple *ins)
9759{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009760 struct triple *left, *right;
9761 left = RHS(ins, 0);
9762 right = RHS(ins, 1);
9763
9764 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009765 int val;
9766 val = const_eq(state, ins, left, right);
9767 if (val >= 0) {
9768 mkconst(state, ins, val == 1);
9769 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009770 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009771 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009772 mkconst(state, ins, 1);
9773 }
9774}
9775
9776static void simplify_noteq(struct compile_state *state, struct triple *ins)
9777{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009778 struct triple *left, *right;
9779 left = RHS(ins, 0);
9780 right = RHS(ins, 1);
9781
9782 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009783 int val;
9784 val = const_eq(state, ins, left, right);
9785 if (val >= 0) {
9786 mkconst(state, ins, val != 1);
9787 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009788 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009789 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009790 mkconst(state, ins, 0);
9791 }
9792}
9793
9794static void simplify_sless(struct compile_state *state, struct triple *ins)
9795{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009796 struct triple *left, *right;
9797 left = RHS(ins, 0);
9798 right = RHS(ins, 1);
9799
9800 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009801 int val;
9802 val = const_scmp(state, ins, left, right);
9803 if ((val >= -1) && (val <= 1)) {
9804 mkconst(state, ins, val < 0);
9805 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009806 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009807 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009808 mkconst(state, ins, 0);
9809 }
9810}
9811
9812static void simplify_uless(struct compile_state *state, struct triple *ins)
9813{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009814 struct triple *left, *right;
9815 left = RHS(ins, 0);
9816 right = RHS(ins, 1);
9817
9818 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009819 int val;
9820 val = const_ucmp(state, ins, left, right);
9821 if ((val >= -1) && (val <= 1)) {
9822 mkconst(state, ins, val < 0);
9823 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009824 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009825 else if (is_zero(right)) {
9826 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009827 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009828 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009829 mkconst(state, ins, 0);
9830 }
9831}
9832
9833static void simplify_smore(struct compile_state *state, struct triple *ins)
9834{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009835 struct triple *left, *right;
9836 left = RHS(ins, 0);
9837 right = RHS(ins, 1);
9838
9839 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009840 int val;
9841 val = const_scmp(state, ins, left, right);
9842 if ((val >= -1) && (val <= 1)) {
9843 mkconst(state, ins, val > 0);
9844 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009845 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009846 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009847 mkconst(state, ins, 0);
9848 }
9849}
9850
9851static void simplify_umore(struct compile_state *state, struct triple *ins)
9852{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009853 struct triple *left, *right;
9854 left = RHS(ins, 0);
9855 right = RHS(ins, 1);
9856
9857 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009858 int val;
9859 val = const_ucmp(state, ins, left, right);
9860 if ((val >= -1) && (val <= 1)) {
9861 mkconst(state, ins, val > 0);
9862 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009863 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009864 else if (is_zero(left)) {
9865 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009866 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009867 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009868 mkconst(state, ins, 0);
9869 }
9870}
9871
9872
9873static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9874{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009875 struct triple *left, *right;
9876 left = RHS(ins, 0);
9877 right = RHS(ins, 1);
9878
9879 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009880 int val;
9881 val = const_scmp(state, ins, left, right);
9882 if ((val >= -1) && (val <= 1)) {
9883 mkconst(state, ins, val <= 0);
9884 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009885 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009886 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009887 mkconst(state, ins, 1);
9888 }
9889}
9890
9891static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9892{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009893 struct triple *left, *right;
9894 left = RHS(ins, 0);
9895 right = RHS(ins, 1);
9896
9897 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009898 int val;
9899 val = const_ucmp(state, ins, left, right);
9900 if ((val >= -1) && (val <= 1)) {
9901 mkconst(state, ins, val <= 0);
9902 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009903 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009904 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009905 mkconst(state, ins, 1);
9906 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009907 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009908 mkconst(state, ins, 1);
9909 }
9910}
9911
9912static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9913{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009914 struct triple *left, *right;
9915 left = RHS(ins, 0);
9916 right = RHS(ins, 1);
9917
9918 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009919 int val;
9920 val = const_scmp(state, ins, left, right);
9921 if ((val >= -1) && (val <= 1)) {
9922 mkconst(state, ins, val >= 0);
9923 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009924 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009925 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009926 mkconst(state, ins, 1);
9927 }
9928}
9929
9930static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9931{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009932 struct triple *left, *right;
9933 left = RHS(ins, 0);
9934 right = RHS(ins, 1);
9935
9936 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009937 int val;
9938 val = const_ucmp(state, ins, left, right);
9939 if ((val >= -1) && (val <= 1)) {
9940 mkconst(state, ins, val >= 0);
9941 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009942 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009943 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009944 mkconst(state, ins, 1);
9945 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009946 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009947 mkconst(state, ins, 1);
9948 }
9949}
9950
9951static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9952{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009953 struct triple *rhs;
9954 rhs = RHS(ins, 0);
9955
9956 if (is_const(rhs)) {
9957 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009958 }
9959 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009960 else if ((rhs->use) &&
9961 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009962 int need_copy = 1;
9963 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009964 switch(rhs->op) {
9965 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9966 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9967 case OP_EQ: rhs->op = OP_NOTEQ; break;
9968 case OP_NOTEQ: rhs->op = OP_EQ; break;
9969 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9970 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9971 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9972 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9973 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9974 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9975 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9976 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009977 default:
9978 need_copy = 0;
9979 break;
9980 }
9981 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009982 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009983 }
9984 }
9985}
9986
9987static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9988{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009989 struct triple *rhs;
9990 rhs = RHS(ins, 0);
9991
9992 if (is_const(rhs)) {
9993 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009994 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009995 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009996 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9997 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9998 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009999 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010000 }
10001
10002}
10003
Eric Biederman90089602004-05-28 14:11:54 +000010004static void simplify_load(struct compile_state *state, struct triple *ins)
10005{
10006 struct triple *addr, *sdecl, *blob;
10007
10008 /* If I am doing a load with a constant pointer from a constant
10009 * table get the value.
10010 */
10011 addr = RHS(ins, 0);
10012 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
10013 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
10014 (blob->op == OP_BLOBCONST)) {
10015 unsigned char buffer[SIZEOF_WORD];
10016 size_t reg_size, mem_size;
10017 const char *src;
10018 ulong_t val;
10019 reg_size = reg_size_of(state, ins->type);
10020 if (reg_size > REG_SIZEOF_REG) {
10021 internal_error(state, ins, "load size greater than register");
10022 }
10023 mem_size = size_of(state, ins->type);
10024 src = blob->u.blob;
10025 src += addr->u.cval;
10026
10027 memset(buffer, 0, sizeof(buffer));
10028 memcpy(buffer, src, bits_to_bytes(mem_size));
10029
10030 switch(mem_size) {
10031 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
10032 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
10033 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
10034 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
10035 default:
10036 internal_error(state, ins, "mem_size: %d not handled",
10037 mem_size);
10038 val = 0;
10039 break;
10040 }
10041 mkconst(state, ins, val);
10042 }
10043}
10044
10045static void simplify_uextract(struct compile_state *state, struct triple *ins)
10046{
10047 if (is_simple_const(RHS(ins, 0))) {
10048 ulong_t val;
10049 ulong_t mask;
10050 val = read_const(state, ins, RHS(ins, 0));
10051 mask = 1;
10052 mask <<= ins->u.bitfield.size;
10053 mask -= 1;
10054 val >>= ins->u.bitfield.offset;
10055 val &= mask;
10056 mkconst(state, ins, val);
10057 }
10058}
10059
10060static void simplify_sextract(struct compile_state *state, struct triple *ins)
10061{
10062 if (is_simple_const(RHS(ins, 0))) {
10063 ulong_t val;
10064 ulong_t mask;
10065 long_t sval;
10066 val = read_const(state, ins, RHS(ins, 0));
10067 mask = 1;
10068 mask <<= ins->u.bitfield.size;
10069 mask -= 1;
10070 val >>= ins->u.bitfield.offset;
10071 val &= mask;
10072 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
10073 sval = val;
10074 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
10075 mkconst(state, ins, sval);
10076 }
10077}
10078
10079static void simplify_deposit(struct compile_state *state, struct triple *ins)
10080{
10081 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
10082 ulong_t targ, val;
10083 ulong_t mask;
10084 targ = read_const(state, ins, RHS(ins, 0));
10085 val = read_const(state, ins, RHS(ins, 1));
10086 mask = 1;
10087 mask <<= ins->u.bitfield.size;
10088 mask -= 1;
10089 mask <<= ins->u.bitfield.offset;
10090 targ &= ~mask;
10091 val <<= ins->u.bitfield.offset;
10092 val &= mask;
10093 targ |= val;
10094 mkconst(state, ins, targ);
10095 }
10096}
10097
Eric Biedermanb138ac82003-04-22 18:44:01 +000010098static void simplify_copy(struct compile_state *state, struct triple *ins)
10099{
Eric Biederman90089602004-05-28 14:11:54 +000010100 struct triple *right;
10101 right = RHS(ins, 0);
10102 if (is_subset_type(ins->type, right->type)) {
10103 ins->type = right->type;
10104 }
10105 if (equiv_types(ins->type, right->type)) {
10106 ins->op = OP_COPY;/* I don't need to convert if the types match */
10107 } else {
10108 if (ins->op == OP_COPY) {
10109 internal_error(state, ins, "type mismatch on copy");
10110 }
10111 }
10112 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10113 struct triple *sdecl;
10114 ulong_t offset;
10115 sdecl = MISC(right, 0);
10116 offset = right->u.cval;
10117 mkaddr_const(state, ins, sdecl, offset);
10118 }
10119 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10120 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010121 case OP_INTCONST:
10122 {
10123 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010124 left = read_const(state, ins, right);
10125 /* Ensure I have not overflowed the destination. */
10126 if (size_of(state, right->type) > size_of(state, ins->type)) {
10127 ulong_t mask;
10128 mask = 1;
10129 mask <<= size_of(state, ins->type);
10130 mask -= 1;
10131 left &= mask;
10132 }
10133 /* Ensure I am properly sign extended */
10134 if (size_of(state, right->type) < size_of(state, ins->type) &&
10135 is_signed(right->type)) {
10136 long_t val;
10137 int shift;
10138 shift = SIZEOF_LONG - size_of(state, right->type);
10139 val = left;
10140 val <<= shift;
10141 val >>= shift;
10142 left = val;
10143 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010144 mkconst(state, ins, left);
10145 break;
10146 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010147 default:
10148 internal_error(state, ins, "uknown constant");
10149 break;
10150 }
10151 }
10152}
10153
Eric Biederman83b991a2003-10-11 06:20:25 +000010154static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010155{
10156 struct triple *ptr;
10157 if (!block) {
10158 return 0;
10159 }
10160 ptr = block->first;
10161 do {
10162 if (ptr->op == OP_PHI) {
10163 return 1;
10164 }
10165 ptr = ptr->next;
10166 } while(ptr != block->last);
10167 return 0;
10168}
10169
Eric Biederman83b991a2003-10-11 06:20:25 +000010170static int phi_dependency(struct block *block)
10171{
10172 /* A block has a phi dependency if a phi function
10173 * depends on that block to exist, and makes a block
10174 * that is otherwise useless unsafe to remove.
10175 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010176 if (block) {
10177 struct block_set *edge;
10178 for(edge = block->edges; edge; edge = edge->next) {
10179 if (phi_present(edge->member)) {
10180 return 1;
10181 }
10182 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010183 }
10184 return 0;
10185}
10186
10187static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10188{
10189 struct triple *targ;
10190 targ = TARG(ins, 0);
10191 /* During scc_transform temporary triples are allocated that
10192 * loop back onto themselves. If I see one don't advance the
10193 * target.
10194 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010195 while(triple_is_structural(state, targ) &&
10196 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010197 targ = targ->next;
10198 }
10199 return targ;
10200}
10201
10202
10203static void simplify_branch(struct compile_state *state, struct triple *ins)
10204{
Eric Biederman90089602004-05-28 14:11:54 +000010205 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010206 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010207 internal_error(state, ins, "not branch");
10208 }
10209 if (ins->use != 0) {
10210 internal_error(state, ins, "branch use");
10211 }
10212 /* The challenge here with simplify branch is that I need to
10213 * make modifications to the control flow graph as well
10214 * as to the branch instruction itself. That is handled
10215 * by rebuilding the basic blocks after simplify all is called.
10216 */
10217
10218 /* If we have a branch to an unconditional branch update
10219 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010220 * functions.
10221 * Also only do this a limited number of times so
10222 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010223 */
Eric Biederman90089602004-05-28 14:11:54 +000010224 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010225 do {
10226 struct triple *targ;
10227 simplified = 0;
10228 targ = branch_target(state, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010229 if ((targ != ins) && (targ->op == OP_BRANCH) &&
10230 !phi_dependency(targ->u.block))
10231 {
10232 unuse_triple(TARG(ins, 0), ins);
10233 TARG(ins, 0) = TARG(targ, 0);
10234 use_triple(TARG(ins, 0), ins);
10235 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010236 }
Eric Biederman90089602004-05-28 14:11:54 +000010237 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010238
10239 /* If we have a conditional branch with a constant condition
10240 * make it an unconditional branch.
10241 */
Eric Biederman90089602004-05-28 14:11:54 +000010242 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010243 struct triple *targ;
10244 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010245 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010246 unuse_triple(RHS(ins, 0), ins);
10247 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010248 ins->rhs = 0;
10249 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010250 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010251 if (value) {
10252 unuse_triple(ins->next, ins);
10253 TARG(ins, 0) = targ;
10254 }
10255 else {
10256 unuse_triple(targ, ins);
10257 TARG(ins, 0) = ins->next;
10258 }
10259 }
Eric Biederman90089602004-05-28 14:11:54 +000010260
10261 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010262 * make it a noop.
10263 */
10264 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010265 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010266 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010267 unuse_triple(RHS(ins, 0), ins);
10268 unuse_triple(ins->next, ins);
10269 }
Eric Biederman90089602004-05-28 14:11:54 +000010270 ins->lhs = 0;
10271 ins->rhs = 0;
10272 ins->misc = 0;
10273 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010274 ins->op = OP_NOOP;
10275 if (ins->use) {
10276 internal_error(state, ins, "noop use != 0");
10277 }
10278 }
10279}
10280
Eric Biederman530b5192003-07-01 10:05:30 +000010281static void simplify_label(struct compile_state *state, struct triple *ins)
10282{
Eric Biederman83b991a2003-10-11 06:20:25 +000010283 /* Ignore volatile labels */
10284 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010285 return;
10286 }
10287 if (ins->use == 0) {
10288 ins->op = OP_NOOP;
10289 }
10290 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010291 /* In general it is not safe to merge one label that
10292 * imediately follows another. The problem is that the empty
10293 * looking block may have phi functions that depend on it.
10294 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010295 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010296 struct triple_set *user, *next;
10297 ins->op = OP_NOOP;
10298 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010299 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010300 next = user->next;
10301 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010302 expr = triple_targ(state, use, 0);
10303 for(;expr; expr = triple_targ(state, use, expr)) {
10304 if (*expr == ins) {
10305 *expr = ins->prev;
10306 unuse_triple(ins, use);
10307 use_triple(ins->prev, use);
10308 }
10309
Eric Biederman530b5192003-07-01 10:05:30 +000010310 }
10311 }
10312 if (ins->use) {
10313 internal_error(state, ins, "noop use != 0");
10314 }
10315 }
10316 }
10317}
10318
Eric Biedermanb138ac82003-04-22 18:44:01 +000010319static void simplify_phi(struct compile_state *state, struct triple *ins)
10320{
Eric Biederman83b991a2003-10-11 06:20:25 +000010321 struct triple **slot;
10322 struct triple *value;
10323 int zrhs, i;
10324 ulong_t cvalue;
10325 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010326 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010327 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010328 return;
10329 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010330 /* See if all of the rhs members of a phi have the same value */
10331 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010332 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010333 for(i = 1; i < zrhs; i++) {
10334 if ( !slot[i] ||
10335 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010336 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010337 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010338 break;
10339 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010340 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010341 if (i == zrhs) {
10342 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010343 return;
10344 }
10345 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010346
10347 /* See if all of rhs members of a phi are the same */
10348 value = slot[0];
10349 for(i = 1; i < zrhs; i++) {
10350 if (slot[i] != value) {
10351 break;
10352 }
10353 }
10354 if (i == zrhs) {
10355 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010356 if (!is_subset_type(ins->type, value->type)) {
10357 internal_error(state, ins, "bad input type to phi");
10358 }
10359 /* Make the types match */
10360 if (!equiv_types(ins->type, value->type)) {
10361 ins->type = value->type;
10362 }
10363 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010364 mkcopy(state, ins, value);
10365 return;
10366 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010367}
10368
10369
10370static void simplify_bsf(struct compile_state *state, struct triple *ins)
10371{
Eric Biederman90089602004-05-28 14:11:54 +000010372 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010373 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010374 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010375 mkconst(state, ins, bsf(left));
10376 }
10377}
10378
10379static void simplify_bsr(struct compile_state *state, struct triple *ins)
10380{
Eric Biederman90089602004-05-28 14:11:54 +000010381 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010382 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010383 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010384 mkconst(state, ins, bsr(left));
10385 }
10386}
10387
10388
10389typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010390static const struct simplify_table {
10391 simplify_t func;
10392 unsigned long flag;
10393} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010394#define simplify_sdivt simplify_noop
10395#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010396#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010397
Eric Biederman5ade04a2003-10-22 04:03:46 +000010398[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10399[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10400[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10401[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10402[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10403[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10404[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10405[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10406[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10407[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10408[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10409[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10410[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10411[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10412[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10413[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10414[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10415[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10416[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010417
Eric Biederman5ade04a2003-10-22 04:03:46 +000010418[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10419[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10420[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10421[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10422[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10423[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10424[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10425[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10426[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10427[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10428[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10429[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010430
Eric Biederman90089602004-05-28 14:11:54 +000010431[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010432[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010433
Eric Biederman90089602004-05-28 14:11:54 +000010434[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10435[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10436[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10437
Eric Biederman5ade04a2003-10-22 04:03:46 +000010438[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010439
Eric Biederman5ade04a2003-10-22 04:03:46 +000010440[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10441[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10442[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010443[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010444
Eric Biederman5ade04a2003-10-22 04:03:46 +000010445[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10446[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10447[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010448[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010449[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10450[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010451
Eric Biederman5ade04a2003-10-22 04:03:46 +000010452[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010453[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454
Eric Biederman5ade04a2003-10-22 04:03:46 +000010455[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10456[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10457[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10458[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10459[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10460[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10461[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10462[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10463[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010464
Eric Biederman5ade04a2003-10-22 04:03:46 +000010465[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10466[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10467[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10468[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10469[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10470[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10471[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10472[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10473[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10474[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10475[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010476};
10477
Eric Biederman90089602004-05-28 14:11:54 +000010478static inline void debug_simplify(struct compile_state *state,
10479 simplify_t do_simplify, struct triple *ins)
10480{
10481#if DEBUG_SIMPLIFY_HIRES
10482 if (state->functions_joined && (do_simplify != simplify_noop)) {
10483 /* High resolution debugging mode */
10484 fprintf(state->dbgout, "simplifing: ");
10485 display_triple(state->dbgout, ins);
10486 }
10487#endif
10488 do_simplify(state, ins);
10489#if DEBUG_SIMPLIFY_HIRES
10490 if (state->functions_joined && (do_simplify != simplify_noop)) {
10491 /* High resolution debugging mode */
10492 fprintf(state->dbgout, "simplified: ");
10493 display_triple(state->dbgout, ins);
10494 }
10495#endif
10496}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010497static void simplify(struct compile_state *state, struct triple *ins)
10498{
10499 int op;
10500 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010501 if (ins == &unknown_triple) {
10502 internal_error(state, ins, "simplifying the unknown triple?");
10503 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010504 do {
10505 op = ins->op;
10506 do_simplify = 0;
10507 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10508 do_simplify = 0;
10509 }
Eric Biederman90089602004-05-28 14:11:54 +000010510 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010511 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010512 }
Eric Biederman90089602004-05-28 14:11:54 +000010513 if (do_simplify &&
10514 !(state->compiler->flags & table_simplify[op].flag)) {
10515 do_simplify = simplify_noop;
10516 }
10517 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010518 do_simplify = simplify_noop;
10519 }
10520
Eric Biedermanb138ac82003-04-22 18:44:01 +000010521 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010522 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010523 op, tops(op));
10524 return;
10525 }
Eric Biederman90089602004-05-28 14:11:54 +000010526 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010527 } while(ins->op != op);
10528}
10529
Eric Biederman5ade04a2003-10-22 04:03:46 +000010530static void rebuild_ssa_form(struct compile_state *state);
10531
Eric Biedermanb138ac82003-04-22 18:44:01 +000010532static void simplify_all(struct compile_state *state)
10533{
10534 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010535 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10536 return;
10537 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010538 first = state->first;
10539 ins = first->prev;
10540 do {
10541 simplify(state, ins);
10542 ins = ins->prev;
10543 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010544 ins = first;
10545 do {
10546 simplify(state, ins);
10547 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010548 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010549 rebuild_ssa_form(state);
10550
Eric Biederman90089602004-05-28 14:11:54 +000010551 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010552}
10553
10554/*
10555 * Builtins....
10556 * ============================
10557 */
10558
Eric Biederman0babc1c2003-05-09 02:39:00 +000010559static void register_builtin_function(struct compile_state *state,
10560 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010561{
Eric Biederman90089602004-05-28 14:11:54 +000010562 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010563 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010564 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010565 struct file_state file;
10566 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010567 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010568 va_list args;
10569 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010570
10571 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010572 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010573 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010574 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010575 file.report_line = 1;
10576 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010577 file.prev = state->file;
10578 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010579 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010580
10581 /* Find the Parameter count */
10582 valid_op(state, op);
10583 parameters = table_ops[op].rhs;
10584 if (parameters < 0 ) {
10585 internal_error(state, 0, "Invalid builtin parameter count");
10586 }
10587
10588 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010589 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010590 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010591 next = &ftype->right;
10592 va_start(args, rtype);
10593 for(i = 0; i < parameters; i++) {
10594 atype = va_arg(args, struct type *);
10595 if (!*next) {
10596 *next = atype;
10597 } else {
10598 *next = new_type(TYPE_PRODUCT, *next, atype);
10599 next = &((*next)->right);
10600 }
10601 }
10602 if (!*next) {
10603 *next = &void_type;
10604 }
10605 va_end(args);
10606
Eric Biederman90089602004-05-28 14:11:54 +000010607 /* Get the initial closure type */
10608 ctype = new_type(TYPE_JOIN, &void_type, 0);
10609 ctype->elements = 1;
10610
10611 /* Get the return type */
10612 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10613 crtype->elements = 2;
10614
Eric Biedermanb138ac82003-04-22 18:44:01 +000010615 /* Generate the needed triples */
10616 def = triple(state, OP_LIST, ftype, 0, 0);
10617 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010618 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010619 result = flatten(state, first, variable(state, crtype));
10620 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010621 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010622
10623 /* Now string them together */
10624 param = ftype->right;
10625 for(i = 0; i < parameters; i++) {
10626 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10627 atype = param->left;
10628 } else {
10629 atype = param;
10630 }
10631 arg = flatten(state, first, variable(state, atype));
10632 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010633 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010634 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010635 generate_lhs_pieces(state, work);
10636 for(i = 0; i < parameters; i++) {
10637 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010638 }
Eric Biederman90089602004-05-28 14:11:54 +000010639 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10640 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010641 }
10642 work = flatten(state, first, work);
10643 last = flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010644 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010645 name_len = strlen(name);
10646 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010647 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010648 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010649
Eric Biedermanb138ac82003-04-22 18:44:01 +000010650 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010651 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010652 state->main_function = 0;
10653
Eric Biederman5ade04a2003-10-22 04:03:46 +000010654 if (!state->functions) {
10655 state->functions = def;
10656 } else {
10657 insert_triple(state, state->functions, def);
10658 }
10659 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010660 FILE *fp = state->dbgout;
10661 fprintf(fp, "\n");
10662 loc(fp, state, 0);
10663 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10664 display_func(state, fp, def);
10665 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010666 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010667}
10668
Eric Biederman0babc1c2003-05-09 02:39:00 +000010669static struct type *partial_struct(struct compile_state *state,
10670 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010671{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010672 struct hash_entry *field_ident;
10673 struct type *result;
10674 int field_name_len;
10675
10676 field_name_len = strlen(field_name);
10677 field_ident = lookup(state, field_name, field_name_len);
10678
10679 result = clone_type(0, type);
10680 result->field_ident = field_ident;
10681
10682 if (rest) {
10683 result = new_type(TYPE_PRODUCT, result, rest);
10684 }
10685 return result;
10686}
10687
10688static struct type *register_builtin_type(struct compile_state *state,
10689 const char *name, struct type *type)
10690{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010691 struct hash_entry *ident;
10692 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010693
Eric Biedermanb138ac82003-04-22 18:44:01 +000010694 name_len = strlen(name);
10695 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010696
10697 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10698 ulong_t elements = 0;
10699 struct type *field;
10700 type = new_type(TYPE_STRUCT, type, 0);
10701 field = type->left;
10702 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10703 elements++;
10704 field = field->right;
10705 }
10706 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010707 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010708 type->type_ident = ident;
10709 type->elements = elements;
10710 }
10711 symbol(state, ident, &ident->sym_ident, 0, type);
10712 ident->tok = TOK_TYPE_NAME;
10713 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010714}
10715
Eric Biederman0babc1c2003-05-09 02:39:00 +000010716
Eric Biedermanb138ac82003-04-22 18:44:01 +000010717static void register_builtins(struct compile_state *state)
10718{
Eric Biederman530b5192003-07-01 10:05:30 +000010719 struct type *div_type, *ldiv_type;
10720 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010721 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010722
Eric Biederman530b5192003-07-01 10:05:30 +000010723 div_type = register_builtin_type(state, "__builtin_div_t",
10724 partial_struct(state, "quot", &int_type,
10725 partial_struct(state, "rem", &int_type, 0)));
10726 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10727 partial_struct(state, "quot", &long_type,
10728 partial_struct(state, "rem", &long_type, 0)));
10729 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10730 partial_struct(state, "quot", &uint_type,
10731 partial_struct(state, "rem", &uint_type, 0)));
10732 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10733 partial_struct(state, "quot", &ulong_type,
10734 partial_struct(state, "rem", &ulong_type, 0)));
10735
10736 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10737 &int_type, &int_type);
10738 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10739 &long_type, &long_type);
10740 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10741 &uint_type, &uint_type);
10742 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10743 &ulong_type, &ulong_type);
10744
Eric Biederman0babc1c2003-05-09 02:39:00 +000010745 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
10746 &ushort_type);
10747 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10748 &ushort_type);
10749 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
10750 &ushort_type);
10751
10752 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
10753 &uchar_type, &ushort_type);
10754 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
10755 &ushort_type, &ushort_type);
10756 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
10757 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010758
Eric Biederman0babc1c2003-05-09 02:39:00 +000010759 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
10760 &int_type);
10761 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
10762 &int_type);
10763
10764 msr_type = register_builtin_type(state, "__builtin_msr_t",
10765 partial_struct(state, "lo", &ulong_type,
10766 partial_struct(state, "hi", &ulong_type, 0)));
10767
10768 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10769 &ulong_type);
10770 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10771 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010772
Eric Biederman0babc1c2003-05-09 02:39:00 +000010773 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10774 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010775}
10776
10777static struct type *declarator(
10778 struct compile_state *state, struct type *type,
10779 struct hash_entry **ident, int need_ident);
10780static void decl(struct compile_state *state, struct triple *first);
10781static struct type *specifier_qualifier_list(struct compile_state *state);
10782static int isdecl_specifier(int tok);
10783static struct type *decl_specifiers(struct compile_state *state);
10784static int istype(int tok);
10785static struct triple *expr(struct compile_state *state);
10786static struct triple *assignment_expr(struct compile_state *state);
10787static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010788static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010789
10790static struct triple *call_expr(
10791 struct compile_state *state, struct triple *func)
10792{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010793 struct triple *def;
10794 struct type *param, *type;
10795 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010796
10797 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10798 error(state, 0, "Called object is not a function");
10799 }
10800 if (func->op != OP_LIST) {
10801 internal_error(state, 0, "improper function");
10802 }
10803 eat(state, TOK_LPAREN);
10804 /* Find the return type without any specifiers */
10805 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010806 /* Count the number of rhs entries for OP_FCALL */
10807 param = func->type->right;
10808 pvals = 0;
10809 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10810 pvals++;
10811 param = param->right;
10812 }
10813 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10814 pvals++;
10815 }
10816 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010817 MISC(def, 0) = func;
10818
10819 param = func->type->right;
10820 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010821 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010822 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010823 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010824 arg_type = param;
10825 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10826 arg_type = param->left;
10827 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010828 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010829 RHS(def, index) = val;
10830 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010831 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010832 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010833 }
10834 }
10835 eat(state, TOK_RPAREN);
10836 return def;
10837}
10838
10839
10840static struct triple *character_constant(struct compile_state *state)
10841{
10842 struct triple *def;
10843 struct token *tk;
10844 const signed char *str, *end;
10845 int c;
10846 int str_len;
10847 eat(state, TOK_LIT_CHAR);
10848 tk = &state->token[0];
10849 str = tk->val.str + 1;
10850 str_len = tk->str_len - 2;
10851 if (str_len <= 0) {
10852 error(state, 0, "empty character constant");
10853 }
10854 end = str + str_len;
10855 c = char_value(state, &str, end);
10856 if (str != end) {
10857 error(state, 0, "multibyte character constant not supported");
10858 }
10859 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10860 return def;
10861}
10862
10863static struct triple *string_constant(struct compile_state *state)
10864{
10865 struct triple *def;
10866 struct token *tk;
10867 struct type *type;
10868 const signed char *str, *end;
10869 signed char *buf, *ptr;
10870 int str_len;
10871
10872 buf = 0;
10873 type = new_type(TYPE_ARRAY, &char_type, 0);
10874 type->elements = 0;
10875 /* The while loop handles string concatenation */
10876 do {
10877 eat(state, TOK_LIT_STRING);
10878 tk = &state->token[0];
10879 str = tk->val.str + 1;
10880 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010881 if (str_len < 0) {
10882 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010883 }
10884 end = str + str_len;
10885 ptr = buf;
10886 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10887 memcpy(buf, ptr, type->elements);
10888 ptr = buf + type->elements;
10889 do {
10890 *ptr++ = char_value(state, &str, end);
10891 } while(str < end);
10892 type->elements = ptr - buf;
10893 } while(peek(state) == TOK_LIT_STRING);
10894 *ptr = '\0';
10895 type->elements += 1;
10896 def = triple(state, OP_BLOBCONST, type, 0, 0);
10897 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010898
Eric Biedermanb138ac82003-04-22 18:44:01 +000010899 return def;
10900}
10901
10902
10903static struct triple *integer_constant(struct compile_state *state)
10904{
10905 struct triple *def;
10906 unsigned long val;
10907 struct token *tk;
10908 char *end;
10909 int u, l, decimal;
10910 struct type *type;
10911
10912 eat(state, TOK_LIT_INT);
10913 tk = &state->token[0];
10914 errno = 0;
10915 decimal = (tk->val.str[0] != '0');
10916 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010917 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010918 error(state, 0, "Integer constant to large");
10919 }
10920 u = l = 0;
10921 if ((*end == 'u') || (*end == 'U')) {
10922 u = 1;
10923 end++;
10924 }
10925 if ((*end == 'l') || (*end == 'L')) {
10926 l = 1;
10927 end++;
10928 }
10929 if ((*end == 'u') || (*end == 'U')) {
10930 u = 1;
10931 end++;
10932 }
10933 if (*end) {
10934 error(state, 0, "Junk at end of integer constant");
10935 }
10936 if (u && l) {
10937 type = &ulong_type;
10938 }
10939 else if (l) {
10940 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010941 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010942 type = &ulong_type;
10943 }
10944 }
10945 else if (u) {
10946 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010947 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010948 type = &ulong_type;
10949 }
10950 }
10951 else {
10952 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010953 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010954 type = &uint_type;
10955 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010956 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010957 type = &ulong_type;
10958 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010959 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010960 type = &long_type;
10961 }
10962 }
10963 def = int_const(state, type, val);
10964 return def;
10965}
10966
10967static struct triple *primary_expr(struct compile_state *state)
10968{
10969 struct triple *def;
10970 int tok;
10971 tok = peek(state);
10972 switch(tok) {
10973 case TOK_IDENT:
10974 {
10975 struct hash_entry *ident;
10976 /* Here ident is either:
10977 * a varable name
10978 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010979 */
10980 eat(state, TOK_IDENT);
10981 ident = state->token[0].ident;
10982 if (!ident->sym_ident) {
10983 error(state, 0, "%s undeclared", ident->name);
10984 }
10985 def = ident->sym_ident->def;
10986 break;
10987 }
10988 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010989 {
10990 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010991 /* Here ident is an enumeration constant */
10992 eat(state, TOK_ENUM_CONST);
Eric Biederman83b991a2003-10-11 06:20:25 +000010993 ident = state->token[0].ident;
10994 if (!ident->sym_ident) {
10995 error(state, 0, "%s undeclared", ident->name);
10996 }
10997 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010998 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010999 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011000 case TOK_LPAREN:
11001 eat(state, TOK_LPAREN);
11002 def = expr(state);
11003 eat(state, TOK_RPAREN);
11004 break;
11005 case TOK_LIT_INT:
11006 def = integer_constant(state);
11007 break;
11008 case TOK_LIT_FLOAT:
11009 eat(state, TOK_LIT_FLOAT);
11010 error(state, 0, "Floating point constants not supported");
11011 def = 0;
11012 FINISHME();
11013 break;
11014 case TOK_LIT_CHAR:
11015 def = character_constant(state);
11016 break;
11017 case TOK_LIT_STRING:
11018 def = string_constant(state);
11019 break;
11020 default:
11021 def = 0;
11022 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
11023 }
11024 return def;
11025}
11026
11027static struct triple *postfix_expr(struct compile_state *state)
11028{
11029 struct triple *def;
11030 int postfix;
11031 def = primary_expr(state);
11032 do {
11033 struct triple *left;
11034 int tok;
11035 postfix = 1;
11036 left = def;
11037 switch((tok = peek(state))) {
11038 case TOK_LBRACKET:
11039 eat(state, TOK_LBRACKET);
11040 def = mk_subscript_expr(state, left, expr(state));
11041 eat(state, TOK_RBRACKET);
11042 break;
11043 case TOK_LPAREN:
11044 def = call_expr(state, def);
11045 break;
11046 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000011047 {
11048 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011049 eat(state, TOK_DOT);
11050 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011051 field = state->token[0].ident;
11052 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011053 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011054 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011055 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000011056 {
11057 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011058 eat(state, TOK_ARROW);
11059 eat(state, TOK_IDENT);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011060 field = state->token[0].ident;
11061 def = mk_deref_expr(state, read_expr(state, def));
11062 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011063 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011064 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011065 case TOK_PLUSPLUS:
11066 eat(state, TOK_PLUSPLUS);
11067 def = mk_post_inc_expr(state, left);
11068 break;
11069 case TOK_MINUSMINUS:
11070 eat(state, TOK_MINUSMINUS);
11071 def = mk_post_dec_expr(state, left);
11072 break;
11073 default:
11074 postfix = 0;
11075 break;
11076 }
11077 } while(postfix);
11078 return def;
11079}
11080
11081static struct triple *cast_expr(struct compile_state *state);
11082
11083static struct triple *unary_expr(struct compile_state *state)
11084{
11085 struct triple *def, *right;
11086 int tok;
11087 switch((tok = peek(state))) {
11088 case TOK_PLUSPLUS:
11089 eat(state, TOK_PLUSPLUS);
11090 def = mk_pre_inc_expr(state, unary_expr(state));
11091 break;
11092 case TOK_MINUSMINUS:
11093 eat(state, TOK_MINUSMINUS);
11094 def = mk_pre_dec_expr(state, unary_expr(state));
11095 break;
11096 case TOK_AND:
11097 eat(state, TOK_AND);
11098 def = mk_addr_expr(state, cast_expr(state), 0);
11099 break;
11100 case TOK_STAR:
11101 eat(state, TOK_STAR);
11102 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11103 break;
11104 case TOK_PLUS:
11105 eat(state, TOK_PLUS);
11106 right = read_expr(state, cast_expr(state));
11107 arithmetic(state, right);
11108 def = integral_promotion(state, right);
11109 break;
11110 case TOK_MINUS:
11111 eat(state, TOK_MINUS);
11112 right = read_expr(state, cast_expr(state));
11113 arithmetic(state, right);
11114 def = integral_promotion(state, right);
11115 def = triple(state, OP_NEG, def->type, def, 0);
11116 break;
11117 case TOK_TILDE:
11118 eat(state, TOK_TILDE);
11119 right = read_expr(state, cast_expr(state));
11120 integral(state, right);
11121 def = integral_promotion(state, right);
11122 def = triple(state, OP_INVERT, def->type, def, 0);
11123 break;
11124 case TOK_BANG:
11125 eat(state, TOK_BANG);
11126 right = read_expr(state, cast_expr(state));
11127 bool(state, right);
11128 def = lfalse_expr(state, right);
11129 break;
11130 case TOK_SIZEOF:
11131 {
11132 struct type *type;
11133 int tok1, tok2;
11134 eat(state, TOK_SIZEOF);
11135 tok1 = peek(state);
11136 tok2 = peek2(state);
11137 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11138 eat(state, TOK_LPAREN);
11139 type = type_name(state);
11140 eat(state, TOK_RPAREN);
11141 }
11142 else {
11143 struct triple *expr;
11144 expr = unary_expr(state);
11145 type = expr->type;
11146 release_expr(state, expr);
11147 }
Eric Biederman90089602004-05-28 14:11:54 +000011148 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011149 break;
11150 }
11151 case TOK_ALIGNOF:
11152 {
11153 struct type *type;
11154 int tok1, tok2;
11155 eat(state, TOK_ALIGNOF);
11156 tok1 = peek(state);
11157 tok2 = peek2(state);
11158 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11159 eat(state, TOK_LPAREN);
11160 type = type_name(state);
11161 eat(state, TOK_RPAREN);
11162 }
11163 else {
11164 struct triple *expr;
11165 expr = unary_expr(state);
11166 type = expr->type;
11167 release_expr(state, expr);
11168 }
Eric Biederman90089602004-05-28 14:11:54 +000011169 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011170 break;
11171 }
11172 default:
11173 def = postfix_expr(state);
11174 break;
11175 }
11176 return def;
11177}
11178
11179static struct triple *cast_expr(struct compile_state *state)
11180{
11181 struct triple *def;
11182 int tok1, tok2;
11183 tok1 = peek(state);
11184 tok2 = peek2(state);
11185 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11186 struct type *type;
11187 eat(state, TOK_LPAREN);
11188 type = type_name(state);
11189 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011190 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011191 }
11192 else {
11193 def = unary_expr(state);
11194 }
11195 return def;
11196}
11197
11198static struct triple *mult_expr(struct compile_state *state)
11199{
11200 struct triple *def;
11201 int done;
11202 def = cast_expr(state);
11203 do {
11204 struct triple *left, *right;
11205 struct type *result_type;
11206 int tok, op, sign;
11207 done = 0;
11208 switch(tok = (peek(state))) {
11209 case TOK_STAR:
11210 case TOK_DIV:
11211 case TOK_MOD:
11212 left = read_expr(state, def);
11213 arithmetic(state, left);
11214
11215 eat(state, tok);
11216
11217 right = read_expr(state, cast_expr(state));
11218 arithmetic(state, right);
11219
11220 result_type = arithmetic_result(state, left, right);
11221 sign = is_signed(result_type);
11222 op = -1;
11223 switch(tok) {
11224 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11225 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11226 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11227 }
11228 def = triple(state, op, result_type, left, right);
11229 break;
11230 default:
11231 done = 1;
11232 break;
11233 }
11234 } while(!done);
11235 return def;
11236}
11237
11238static struct triple *add_expr(struct compile_state *state)
11239{
11240 struct triple *def;
11241 int done;
11242 def = mult_expr(state);
11243 do {
11244 done = 0;
11245 switch( peek(state)) {
11246 case TOK_PLUS:
11247 eat(state, TOK_PLUS);
11248 def = mk_add_expr(state, def, mult_expr(state));
11249 break;
11250 case TOK_MINUS:
11251 eat(state, TOK_MINUS);
11252 def = mk_sub_expr(state, def, mult_expr(state));
11253 break;
11254 default:
11255 done = 1;
11256 break;
11257 }
11258 } while(!done);
11259 return def;
11260}
11261
11262static struct triple *shift_expr(struct compile_state *state)
11263{
11264 struct triple *def;
11265 int done;
11266 def = add_expr(state);
11267 do {
11268 struct triple *left, *right;
11269 int tok, op;
11270 done = 0;
11271 switch((tok = peek(state))) {
11272 case TOK_SL:
11273 case TOK_SR:
11274 left = read_expr(state, def);
11275 integral(state, left);
11276 left = integral_promotion(state, left);
11277
11278 eat(state, tok);
11279
11280 right = read_expr(state, add_expr(state));
11281 integral(state, right);
11282 right = integral_promotion(state, right);
11283
11284 op = (tok == TOK_SL)? OP_SL :
11285 is_signed(left->type)? OP_SSR: OP_USR;
11286
11287 def = triple(state, op, left->type, left, right);
11288 break;
11289 default:
11290 done = 1;
11291 break;
11292 }
11293 } while(!done);
11294 return def;
11295}
11296
11297static struct triple *relational_expr(struct compile_state *state)
11298{
11299#warning "Extend relational exprs to work on more than arithmetic types"
11300 struct triple *def;
11301 int done;
11302 def = shift_expr(state);
11303 do {
11304 struct triple *left, *right;
11305 struct type *arg_type;
11306 int tok, op, sign;
11307 done = 0;
11308 switch((tok = peek(state))) {
11309 case TOK_LESS:
11310 case TOK_MORE:
11311 case TOK_LESSEQ:
11312 case TOK_MOREEQ:
11313 left = read_expr(state, def);
11314 arithmetic(state, left);
11315
11316 eat(state, tok);
11317
11318 right = read_expr(state, shift_expr(state));
11319 arithmetic(state, right);
11320
11321 arg_type = arithmetic_result(state, left, right);
11322 sign = is_signed(arg_type);
11323 op = -1;
11324 switch(tok) {
11325 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11326 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11327 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11328 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11329 }
11330 def = triple(state, op, &int_type, left, right);
11331 break;
11332 default:
11333 done = 1;
11334 break;
11335 }
11336 } while(!done);
11337 return def;
11338}
11339
11340static struct triple *equality_expr(struct compile_state *state)
11341{
11342#warning "Extend equality exprs to work on more than arithmetic types"
11343 struct triple *def;
11344 int done;
11345 def = relational_expr(state);
11346 do {
11347 struct triple *left, *right;
11348 int tok, op;
11349 done = 0;
11350 switch((tok = peek(state))) {
11351 case TOK_EQEQ:
11352 case TOK_NOTEQ:
11353 left = read_expr(state, def);
11354 arithmetic(state, left);
11355 eat(state, tok);
11356 right = read_expr(state, relational_expr(state));
11357 arithmetic(state, right);
11358 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11359 def = triple(state, op, &int_type, left, right);
11360 break;
11361 default:
11362 done = 1;
11363 break;
11364 }
11365 } while(!done);
11366 return def;
11367}
11368
11369static struct triple *and_expr(struct compile_state *state)
11370{
11371 struct triple *def;
11372 def = equality_expr(state);
11373 while(peek(state) == TOK_AND) {
11374 struct triple *left, *right;
11375 struct type *result_type;
11376 left = read_expr(state, def);
11377 integral(state, left);
11378 eat(state, TOK_AND);
11379 right = read_expr(state, equality_expr(state));
11380 integral(state, right);
11381 result_type = arithmetic_result(state, left, right);
11382 def = triple(state, OP_AND, result_type, left, right);
11383 }
11384 return def;
11385}
11386
11387static struct triple *xor_expr(struct compile_state *state)
11388{
11389 struct triple *def;
11390 def = and_expr(state);
11391 while(peek(state) == TOK_XOR) {
11392 struct triple *left, *right;
11393 struct type *result_type;
11394 left = read_expr(state, def);
11395 integral(state, left);
11396 eat(state, TOK_XOR);
11397 right = read_expr(state, and_expr(state));
11398 integral(state, right);
11399 result_type = arithmetic_result(state, left, right);
11400 def = triple(state, OP_XOR, result_type, left, right);
11401 }
11402 return def;
11403}
11404
11405static struct triple *or_expr(struct compile_state *state)
11406{
11407 struct triple *def;
11408 def = xor_expr(state);
11409 while(peek(state) == TOK_OR) {
11410 struct triple *left, *right;
11411 struct type *result_type;
11412 left = read_expr(state, def);
11413 integral(state, left);
11414 eat(state, TOK_OR);
11415 right = read_expr(state, xor_expr(state));
11416 integral(state, right);
11417 result_type = arithmetic_result(state, left, right);
11418 def = triple(state, OP_OR, result_type, left, right);
11419 }
11420 return def;
11421}
11422
11423static struct triple *land_expr(struct compile_state *state)
11424{
11425 struct triple *def;
11426 def = or_expr(state);
11427 while(peek(state) == TOK_LOGAND) {
11428 struct triple *left, *right;
11429 left = read_expr(state, def);
11430 bool(state, left);
11431 eat(state, TOK_LOGAND);
11432 right = read_expr(state, or_expr(state));
11433 bool(state, right);
11434
Eric Biederman90089602004-05-28 14:11:54 +000011435 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011436 ltrue_expr(state, left),
11437 ltrue_expr(state, right));
11438 }
11439 return def;
11440}
11441
11442static struct triple *lor_expr(struct compile_state *state)
11443{
11444 struct triple *def;
11445 def = land_expr(state);
11446 while(peek(state) == TOK_LOGOR) {
11447 struct triple *left, *right;
11448 left = read_expr(state, def);
11449 bool(state, left);
11450 eat(state, TOK_LOGOR);
11451 right = read_expr(state, land_expr(state));
11452 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011453
11454 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011455 ltrue_expr(state, left),
11456 ltrue_expr(state, right));
11457 }
11458 return def;
11459}
11460
11461static struct triple *conditional_expr(struct compile_state *state)
11462{
11463 struct triple *def;
11464 def = lor_expr(state);
11465 if (peek(state) == TOK_QUEST) {
11466 struct triple *test, *left, *right;
11467 bool(state, def);
11468 test = ltrue_expr(state, read_expr(state, def));
11469 eat(state, TOK_QUEST);
11470 left = read_expr(state, expr(state));
11471 eat(state, TOK_COLON);
11472 right = read_expr(state, conditional_expr(state));
11473
Eric Biederman90089602004-05-28 14:11:54 +000011474 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011475 }
11476 return def;
11477}
11478
11479static struct triple *eval_const_expr(
11480 struct compile_state *state, struct triple *expr)
11481{
11482 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011483 if (is_const(expr)) {
11484 def = expr;
11485 }
11486 else {
11487 /* If we don't start out as a constant simplify into one */
11488 struct triple *head, *ptr;
11489 head = label(state); /* dummy initial triple */
11490 flatten(state, head, expr);
11491 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11492 simplify(state, ptr);
11493 }
11494 /* Remove the constant value the tail of the list */
11495 def = head->prev;
11496 def->prev->next = def->next;
11497 def->next->prev = def->prev;
11498 def->next = def->prev = def;
11499 if (!is_const(def)) {
11500 error(state, 0, "Not a constant expression");
11501 }
11502 /* Free the intermediate expressions */
11503 while(head->next != head) {
11504 release_triple(state, head->next);
11505 }
11506 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011507 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011508 return def;
11509}
11510
11511static struct triple *constant_expr(struct compile_state *state)
11512{
11513 return eval_const_expr(state, conditional_expr(state));
11514}
11515
11516static struct triple *assignment_expr(struct compile_state *state)
11517{
11518 struct triple *def, *left, *right;
11519 int tok, op, sign;
11520 /* The C grammer in K&R shows assignment expressions
11521 * only taking unary expressions as input on their
11522 * left hand side. But specifies the precedence of
11523 * assignemnt as the lowest operator except for comma.
11524 *
11525 * Allowing conditional expressions on the left hand side
11526 * of an assignement results in a grammar that accepts
11527 * a larger set of statements than standard C. As long
11528 * as the subset of the grammar that is standard C behaves
11529 * correctly this should cause no problems.
11530 *
11531 * For the extra token strings accepted by the grammar
11532 * none of them should produce a valid lvalue, so they
11533 * should not produce functioning programs.
11534 *
11535 * GCC has this bug as well, so surprises should be minimal.
11536 */
11537 def = conditional_expr(state);
11538 left = def;
11539 switch((tok = peek(state))) {
11540 case TOK_EQ:
11541 lvalue(state, left);
11542 eat(state, TOK_EQ);
11543 def = write_expr(state, left,
11544 read_expr(state, assignment_expr(state)));
11545 break;
11546 case TOK_TIMESEQ:
11547 case TOK_DIVEQ:
11548 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011549 lvalue(state, left);
11550 arithmetic(state, left);
11551 eat(state, tok);
11552 right = read_expr(state, assignment_expr(state));
11553 arithmetic(state, right);
11554
11555 sign = is_signed(left->type);
11556 op = -1;
11557 switch(tok) {
11558 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11559 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11560 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011561 }
11562 def = write_expr(state, left,
11563 triple(state, op, left->type,
11564 read_expr(state, left), right));
11565 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011566 case TOK_PLUSEQ:
11567 lvalue(state, left);
11568 eat(state, TOK_PLUSEQ);
11569 def = write_expr(state, left,
11570 mk_add_expr(state, left, assignment_expr(state)));
11571 break;
11572 case TOK_MINUSEQ:
11573 lvalue(state, left);
11574 eat(state, TOK_MINUSEQ);
11575 def = write_expr(state, left,
11576 mk_sub_expr(state, left, assignment_expr(state)));
11577 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011578 case TOK_SLEQ:
11579 case TOK_SREQ:
11580 case TOK_ANDEQ:
11581 case TOK_XOREQ:
11582 case TOK_OREQ:
11583 lvalue(state, left);
11584 integral(state, left);
11585 eat(state, tok);
11586 right = read_expr(state, assignment_expr(state));
11587 integral(state, right);
11588 right = integral_promotion(state, right);
11589 sign = is_signed(left->type);
11590 op = -1;
11591 switch(tok) {
11592 case TOK_SLEQ: op = OP_SL; break;
11593 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11594 case TOK_ANDEQ: op = OP_AND; break;
11595 case TOK_XOREQ: op = OP_XOR; break;
11596 case TOK_OREQ: op = OP_OR; break;
11597 }
11598 def = write_expr(state, left,
11599 triple(state, op, left->type,
11600 read_expr(state, left), right));
11601 break;
11602 }
11603 return def;
11604}
11605
11606static struct triple *expr(struct compile_state *state)
11607{
11608 struct triple *def;
11609 def = assignment_expr(state);
11610 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011611 eat(state, TOK_COMMA);
Eric Biederman90089602004-05-28 14:11:54 +000011612 def = mkprog(state, def, assignment_expr(state), 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011613 }
11614 return def;
11615}
11616
11617static void expr_statement(struct compile_state *state, struct triple *first)
11618{
11619 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011620 /* lvalue conversions always apply except when certian operators
11621 * are applied. I apply the lvalue conversions here
11622 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011623 */
11624 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011625 }
11626 eat(state, TOK_SEMI);
11627}
11628
11629static void if_statement(struct compile_state *state, struct triple *first)
11630{
11631 struct triple *test, *jmp1, *jmp2, *middle, *end;
11632
11633 jmp1 = jmp2 = middle = 0;
11634 eat(state, TOK_IF);
11635 eat(state, TOK_LPAREN);
11636 test = expr(state);
11637 bool(state, test);
11638 /* Cleanup and invert the test */
11639 test = lfalse_expr(state, read_expr(state, test));
11640 eat(state, TOK_RPAREN);
11641 /* Generate the needed pieces */
11642 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011643 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011644 /* Thread the pieces together */
11645 flatten(state, first, test);
11646 flatten(state, first, jmp1);
11647 flatten(state, first, label(state));
11648 statement(state, first);
11649 if (peek(state) == TOK_ELSE) {
11650 eat(state, TOK_ELSE);
11651 /* Generate the rest of the pieces */
11652 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011653 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011654 /* Thread them together */
11655 flatten(state, first, jmp2);
11656 flatten(state, first, middle);
11657 statement(state, first);
11658 flatten(state, first, end);
11659 }
11660 else {
11661 flatten(state, first, middle);
11662 }
11663}
11664
11665static void for_statement(struct compile_state *state, struct triple *first)
11666{
11667 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11668 struct triple *label1, *label2, *label3;
11669 struct hash_entry *ident;
11670
11671 eat(state, TOK_FOR);
11672 eat(state, TOK_LPAREN);
11673 head = test = tail = jmp1 = jmp2 = 0;
11674 if (peek(state) != TOK_SEMI) {
11675 head = expr(state);
11676 }
11677 eat(state, TOK_SEMI);
11678 if (peek(state) != TOK_SEMI) {
11679 test = expr(state);
11680 bool(state, test);
11681 test = ltrue_expr(state, read_expr(state, test));
11682 }
11683 eat(state, TOK_SEMI);
11684 if (peek(state) != TOK_RPAREN) {
11685 tail = expr(state);
11686 }
11687 eat(state, TOK_RPAREN);
11688 /* Generate the needed pieces */
11689 label1 = label(state);
11690 label2 = label(state);
11691 label3 = label(state);
11692 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011693 jmp1 = branch(state, label3, 0);
11694 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011695 }
11696 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011697 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011698 }
11699 end = label(state);
11700 /* Remember where break and continue go */
11701 start_scope(state);
11702 ident = state->i_break;
11703 symbol(state, ident, &ident->sym_ident, end, end->type);
11704 ident = state->i_continue;
11705 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11706 /* Now include the body */
11707 flatten(state, first, head);
11708 flatten(state, first, jmp1);
11709 flatten(state, first, label1);
11710 statement(state, first);
11711 flatten(state, first, label2);
11712 flatten(state, first, tail);
11713 flatten(state, first, label3);
11714 flatten(state, first, test);
11715 flatten(state, first, jmp2);
11716 flatten(state, first, end);
11717 /* Cleanup the break/continue scope */
11718 end_scope(state);
11719}
11720
11721static void while_statement(struct compile_state *state, struct triple *first)
11722{
11723 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11724 struct hash_entry *ident;
11725 eat(state, TOK_WHILE);
11726 eat(state, TOK_LPAREN);
11727 test = expr(state);
11728 bool(state, test);
11729 test = ltrue_expr(state, read_expr(state, test));
11730 eat(state, TOK_RPAREN);
11731 /* Generate the needed pieces */
11732 label1 = label(state);
11733 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011734 jmp1 = branch(state, label2, 0);
11735 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011736 end = label(state);
11737 /* Remember where break and continue go */
11738 start_scope(state);
11739 ident = state->i_break;
11740 symbol(state, ident, &ident->sym_ident, end, end->type);
11741 ident = state->i_continue;
11742 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11743 /* Thread them together */
11744 flatten(state, first, jmp1);
11745 flatten(state, first, label1);
11746 statement(state, first);
11747 flatten(state, first, label2);
11748 flatten(state, first, test);
11749 flatten(state, first, jmp2);
11750 flatten(state, first, end);
11751 /* Cleanup the break/continue scope */
11752 end_scope(state);
11753}
11754
11755static void do_statement(struct compile_state *state, struct triple *first)
11756{
11757 struct triple *label1, *label2, *test, *end;
11758 struct hash_entry *ident;
11759 eat(state, TOK_DO);
11760 /* Generate the needed pieces */
11761 label1 = label(state);
11762 label2 = label(state);
11763 end = label(state);
11764 /* Remember where break and continue go */
11765 start_scope(state);
11766 ident = state->i_break;
11767 symbol(state, ident, &ident->sym_ident, end, end->type);
11768 ident = state->i_continue;
11769 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11770 /* Now include the body */
11771 flatten(state, first, label1);
11772 statement(state, first);
11773 /* Cleanup the break/continue scope */
11774 end_scope(state);
11775 /* Eat the rest of the loop */
11776 eat(state, TOK_WHILE);
11777 eat(state, TOK_LPAREN);
11778 test = read_expr(state, expr(state));
11779 bool(state, test);
11780 eat(state, TOK_RPAREN);
11781 eat(state, TOK_SEMI);
11782 /* Thread the pieces together */
11783 test = ltrue_expr(state, test);
11784 flatten(state, first, label2);
11785 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011786 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011787 flatten(state, first, end);
11788}
11789
11790
11791static void return_statement(struct compile_state *state, struct triple *first)
11792{
11793 struct triple *jmp, *mv, *dest, *var, *val;
11794 int last;
11795 eat(state, TOK_RETURN);
11796
11797#warning "FIXME implement a more general excess branch elimination"
11798 val = 0;
11799 /* If we have a return value do some more work */
11800 if (peek(state) != TOK_SEMI) {
11801 val = read_expr(state, expr(state));
11802 }
11803 eat(state, TOK_SEMI);
11804
11805 /* See if this last statement in a function */
11806 last = ((peek(state) == TOK_RBRACE) &&
11807 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11808
11809 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011810 var = fresult(state, state->main_function);
11811
Eric Biedermanb138ac82003-04-22 18:44:01 +000011812 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011813 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011814 mv = jmp = 0;
11815 /* If needed generate a jump instruction */
11816 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011817 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011818 }
11819 /* If needed generate an assignment instruction */
11820 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011821 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011822 }
11823 /* Now put the code together */
11824 if (mv) {
11825 flatten(state, first, mv);
11826 flatten(state, first, jmp);
11827 }
11828 else if (jmp) {
11829 flatten(state, first, jmp);
11830 }
11831}
11832
11833static void break_statement(struct compile_state *state, struct triple *first)
11834{
11835 struct triple *dest;
11836 eat(state, TOK_BREAK);
11837 eat(state, TOK_SEMI);
11838 if (!state->i_break->sym_ident) {
11839 error(state, 0, "break statement not within loop or switch");
11840 }
11841 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011842 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011843}
11844
11845static void continue_statement(struct compile_state *state, struct triple *first)
11846{
11847 struct triple *dest;
11848 eat(state, TOK_CONTINUE);
11849 eat(state, TOK_SEMI);
11850 if (!state->i_continue->sym_ident) {
11851 error(state, 0, "continue statement outside of a loop");
11852 }
11853 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011854 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011855}
11856
11857static void goto_statement(struct compile_state *state, struct triple *first)
11858{
Eric Biederman153ea352003-06-20 14:43:20 +000011859 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011860 eat(state, TOK_GOTO);
11861 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +000011862 ident = state->token[0].ident;
11863 if (!ident->sym_label) {
11864 /* If this is a forward branch allocate the label now,
11865 * it will be flattend in the appropriate location later.
11866 */
11867 struct triple *ins;
11868 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011869 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011870 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011871 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011872
11873 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011874}
11875
11876static void labeled_statement(struct compile_state *state, struct triple *first)
11877{
Eric Biederman153ea352003-06-20 14:43:20 +000011878 struct triple *ins;
11879 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011880 eat(state, TOK_IDENT);
Eric Biederman153ea352003-06-20 14:43:20 +000011881
11882 ident = state->token[0].ident;
11883 if (ident->sym_label && ident->sym_label->def) {
11884 ins = ident->sym_label->def;
11885 put_occurance(ins->occurance);
11886 ins->occurance = new_occurance(state);
11887 }
11888 else {
11889 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011890 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011891 }
11892 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11893 error(state, 0, "label %s already defined", ident->name);
11894 }
11895 flatten(state, first, ins);
11896
Eric Biedermanb138ac82003-04-22 18:44:01 +000011897 eat(state, TOK_COLON);
11898 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011899}
11900
11901static void switch_statement(struct compile_state *state, struct triple *first)
11902{
Eric Biederman83b991a2003-10-11 06:20:25 +000011903 struct triple *value, *top, *end, *dbranch;
11904 struct hash_entry *ident;
11905
11906 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011907 eat(state, TOK_SWITCH);
11908 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011909 value = expr(state);
11910 integral(state, value);
11911 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011912 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011913 /* Generate the needed pieces */
11914 top = label(state);
11915 end = label(state);
11916 dbranch = branch(state, end, 0);
11917 /* Remember where case branches and break goes */
11918 start_scope(state);
11919 ident = state->i_switch;
11920 symbol(state, ident, &ident->sym_ident, value, value->type);
11921 ident = state->i_case;
11922 symbol(state, ident, &ident->sym_ident, top, top->type);
11923 ident = state->i_break;
11924 symbol(state, ident, &ident->sym_ident, end, end->type);
11925 ident = state->i_default;
11926 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11927 /* Thread them together */
11928 flatten(state, first, value);
11929 flatten(state, first, top);
11930 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011931 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011932 flatten(state, first, end);
11933 /* Cleanup the switch scope */
11934 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011935}
11936
11937static void case_statement(struct compile_state *state, struct triple *first)
11938{
Eric Biederman83b991a2003-10-11 06:20:25 +000011939 struct triple *cvalue, *dest, *test, *jmp;
11940 struct triple *ptr, *value, *top, *dbranch;
11941
11942 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011943 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011944 cvalue = constant_expr(state);
11945 integral(state, cvalue);
11946 if (cvalue->op != OP_INTCONST) {
11947 error(state, 0, "integer constant expected");
11948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011949 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011950 if (!state->i_case->sym_ident) {
11951 error(state, 0, "case statement not within a switch");
11952 }
11953
11954 /* Lookup the interesting pieces */
11955 top = state->i_case->sym_ident->def;
11956 value = state->i_switch->sym_ident->def;
11957 dbranch = state->i_default->sym_ident->def;
11958
11959 /* See if this case label has already been used */
11960 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11961 if (ptr->op != OP_EQ) {
11962 continue;
11963 }
11964 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11965 error(state, 0, "duplicate case %d statement",
11966 cvalue->u.cval);
11967 }
11968 }
11969 /* Generate the needed pieces */
11970 dest = label(state);
11971 test = triple(state, OP_EQ, &int_type, value, cvalue);
11972 jmp = branch(state, dest, test);
11973 /* Thread the pieces together */
11974 flatten(state, dbranch, test);
11975 flatten(state, dbranch, jmp);
11976 flatten(state, dbranch, label(state));
11977 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011978 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011979}
11980
11981static void default_statement(struct compile_state *state, struct triple *first)
11982{
Eric Biederman83b991a2003-10-11 06:20:25 +000011983 struct triple *dest;
11984 struct triple *dbranch, *end;
11985
11986 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011987 eat(state, TOK_DEFAULT);
11988 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011989
11990 if (!state->i_case->sym_ident) {
11991 error(state, 0, "default statement not within a switch");
11992 }
11993
11994 /* Lookup the interesting pieces */
11995 dbranch = state->i_default->sym_ident->def;
11996 end = state->i_break->sym_ident->def;
11997
11998 /* See if a default statement has already happened */
11999 if (TARG(dbranch, 0) != end) {
12000 error(state, 0, "duplicate default statement");
12001 }
12002
12003 /* Generate the needed pieces */
12004 dest = label(state);
12005
Eric Biederman90089602004-05-28 14:11:54 +000012006 /* Blame the branch on the default statement */
12007 put_occurance(dbranch->occurance);
12008 dbranch->occurance = new_occurance(state);
12009
Eric Biederman83b991a2003-10-11 06:20:25 +000012010 /* Thread the pieces together */
12011 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012012 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012013 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012014 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012015}
12016
12017static void asm_statement(struct compile_state *state, struct triple *first)
12018{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012019 struct asm_info *info;
12020 struct {
12021 struct triple *constraint;
12022 struct triple *expr;
12023 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12024 struct triple *def, *asm_str;
12025 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012026 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012027
Eric Biederman90089602004-05-28 14:11:54 +000012028 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012029 eat(state, TOK_ASM);
12030 /* For now ignore the qualifiers */
12031 switch(peek(state)) {
12032 case TOK_CONST:
12033 eat(state, TOK_CONST);
12034 break;
12035 case TOK_VOLATILE:
12036 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012037 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012038 break;
12039 }
12040 eat(state, TOK_LPAREN);
12041 asm_str = string_constant(state);
12042
12043 colons = 0;
12044 out = in = clobbers = 0;
12045 /* Outputs */
12046 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12047 eat(state, TOK_COLON);
12048 colons++;
12049 more = (peek(state) == TOK_LIT_STRING);
12050 while(more) {
12051 struct triple *var;
12052 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012053 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012054 more = 0;
12055 if (out > MAX_LHS) {
12056 error(state, 0, "Maximum output count exceeded.");
12057 }
12058 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012059 str = constraint->u.blob;
12060 if (str[0] != '=') {
12061 error(state, 0, "Output constraint does not start with =");
12062 }
12063 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012064 eat(state, TOK_LPAREN);
12065 var = conditional_expr(state);
12066 eat(state, TOK_RPAREN);
12067
12068 lvalue(state, var);
12069 out_param[out].constraint = constraint;
12070 out_param[out].expr = var;
12071 if (peek(state) == TOK_COMMA) {
12072 eat(state, TOK_COMMA);
12073 more = 1;
12074 }
12075 out++;
12076 }
12077 }
12078 /* Inputs */
12079 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12080 eat(state, TOK_COLON);
12081 colons++;
12082 more = (peek(state) == TOK_LIT_STRING);
12083 while(more) {
12084 struct triple *val;
12085 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012086 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012087 more = 0;
12088 if (in > MAX_RHS) {
12089 error(state, 0, "Maximum input count exceeded.");
12090 }
12091 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012092 str = constraint->u.blob;
12093 if (digitp(str[0] && str[1] == '\0')) {
12094 int val;
12095 val = digval(str[0]);
12096 if ((val < 0) || (val >= out)) {
12097 error(state, 0, "Invalid input constraint %d", val);
12098 }
12099 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012100 eat(state, TOK_LPAREN);
12101 val = conditional_expr(state);
12102 eat(state, TOK_RPAREN);
12103
12104 in_param[in].constraint = constraint;
12105 in_param[in].expr = val;
12106 if (peek(state) == TOK_COMMA) {
12107 eat(state, TOK_COMMA);
12108 more = 1;
12109 }
12110 in++;
12111 }
12112 }
12113
12114 /* Clobber */
12115 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12116 eat(state, TOK_COLON);
12117 colons++;
12118 more = (peek(state) == TOK_LIT_STRING);
12119 while(more) {
12120 struct triple *clobber;
12121 more = 0;
12122 if ((clobbers + out) > MAX_LHS) {
12123 error(state, 0, "Maximum clobber limit exceeded.");
12124 }
12125 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012126
12127 clob_param[clobbers].constraint = clobber;
12128 if (peek(state) == TOK_COMMA) {
12129 eat(state, TOK_COMMA);
12130 more = 1;
12131 }
12132 clobbers++;
12133 }
12134 }
12135 eat(state, TOK_RPAREN);
12136 eat(state, TOK_SEMI);
12137
12138
12139 info = xcmalloc(sizeof(*info), "asm_info");
12140 info->str = asm_str->u.blob;
12141 free_triple(state, asm_str);
12142
12143 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12144 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012145 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012146
12147 /* Find the register constraints */
12148 for(i = 0; i < out; i++) {
12149 struct triple *constraint;
12150 constraint = out_param[i].constraint;
12151 info->tmpl.lhs[i] = arch_reg_constraint(state,
12152 out_param[i].expr->type, constraint->u.blob);
12153 free_triple(state, constraint);
12154 }
12155 for(; i - out < clobbers; i++) {
12156 struct triple *constraint;
12157 constraint = clob_param[i - out].constraint;
12158 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12159 free_triple(state, constraint);
12160 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012161 for(i = 0; i < in; i++) {
12162 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012163 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012164 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012165 str = constraint->u.blob;
12166 if (digitp(str[0]) && str[1] == '\0') {
12167 struct reg_info cinfo;
12168 int val;
12169 val = digval(str[0]);
12170 cinfo.reg = info->tmpl.lhs[val].reg;
12171 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12172 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12173 if (cinfo.reg == REG_UNSET) {
12174 cinfo.reg = REG_VIRT0 + val;
12175 }
12176 if (cinfo.regcm == 0) {
12177 error(state, 0, "No registers for %d", val);
12178 }
12179 info->tmpl.lhs[val] = cinfo;
12180 info->tmpl.rhs[i] = cinfo;
12181
12182 } else {
12183 info->tmpl.rhs[i] = arch_reg_constraint(state,
12184 in_param[i].expr->type, str);
12185 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012186 free_triple(state, constraint);
12187 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012188
12189 /* Now build the helper expressions */
12190 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012191 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012192 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012193 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012194 for(i = 0; i < (out + clobbers); i++) {
12195 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012196 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012197 if (i < out) {
12198 type = out_param[i].expr->type;
12199 } else {
12200 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12201 if (size >= SIZEOF_LONG) {
12202 type = &ulong_type;
12203 }
12204 else if (size >= SIZEOF_INT) {
12205 type = &uint_type;
12206 }
12207 else if (size >= SIZEOF_SHORT) {
12208 type = &ushort_type;
12209 }
12210 else {
12211 type = &uchar_type;
12212 }
12213 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012214 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012215 piece->u.cval = i;
12216 LHS(def, i) = piece;
12217 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012218 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012219 /* And write the helpers to their destinations */
12220 for(i = 0; i < out; i++) {
12221 struct triple *piece;
12222 piece = LHS(def, i);
12223 flatten(state, first,
12224 write_expr(state, out_param[i].expr, piece));
12225 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012226}
12227
12228
12229static int isdecl(int tok)
12230{
12231 switch(tok) {
12232 case TOK_AUTO:
12233 case TOK_REGISTER:
12234 case TOK_STATIC:
12235 case TOK_EXTERN:
12236 case TOK_TYPEDEF:
12237 case TOK_CONST:
12238 case TOK_RESTRICT:
12239 case TOK_VOLATILE:
12240 case TOK_VOID:
12241 case TOK_CHAR:
12242 case TOK_SHORT:
12243 case TOK_INT:
12244 case TOK_LONG:
12245 case TOK_FLOAT:
12246 case TOK_DOUBLE:
12247 case TOK_SIGNED:
12248 case TOK_UNSIGNED:
12249 case TOK_STRUCT:
12250 case TOK_UNION:
12251 case TOK_ENUM:
12252 case TOK_TYPE_NAME: /* typedef name */
12253 return 1;
12254 default:
12255 return 0;
12256 }
12257}
12258
12259static void compound_statement(struct compile_state *state, struct triple *first)
12260{
12261 eat(state, TOK_LBRACE);
12262 start_scope(state);
12263
12264 /* statement-list opt */
12265 while (peek(state) != TOK_RBRACE) {
12266 statement(state, first);
12267 }
12268 end_scope(state);
12269 eat(state, TOK_RBRACE);
12270}
12271
12272static void statement(struct compile_state *state, struct triple *first)
12273{
12274 int tok;
12275 tok = peek(state);
12276 if (tok == TOK_LBRACE) {
12277 compound_statement(state, first);
12278 }
12279 else if (tok == TOK_IF) {
12280 if_statement(state, first);
12281 }
12282 else if (tok == TOK_FOR) {
12283 for_statement(state, first);
12284 }
12285 else if (tok == TOK_WHILE) {
12286 while_statement(state, first);
12287 }
12288 else if (tok == TOK_DO) {
12289 do_statement(state, first);
12290 }
12291 else if (tok == TOK_RETURN) {
12292 return_statement(state, first);
12293 }
12294 else if (tok == TOK_BREAK) {
12295 break_statement(state, first);
12296 }
12297 else if (tok == TOK_CONTINUE) {
12298 continue_statement(state, first);
12299 }
12300 else if (tok == TOK_GOTO) {
12301 goto_statement(state, first);
12302 }
12303 else if (tok == TOK_SWITCH) {
12304 switch_statement(state, first);
12305 }
12306 else if (tok == TOK_ASM) {
12307 asm_statement(state, first);
12308 }
12309 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12310 labeled_statement(state, first);
12311 }
12312 else if (tok == TOK_CASE) {
12313 case_statement(state, first);
12314 }
12315 else if (tok == TOK_DEFAULT) {
12316 default_statement(state, first);
12317 }
12318 else if (isdecl(tok)) {
12319 /* This handles C99 intermixing of statements and decls */
12320 decl(state, first);
12321 }
12322 else {
12323 expr_statement(state, first);
12324 }
12325}
12326
12327static struct type *param_decl(struct compile_state *state)
12328{
12329 struct type *type;
12330 struct hash_entry *ident;
12331 /* Cheat so the declarator will know we are not global */
12332 start_scope(state);
12333 ident = 0;
12334 type = decl_specifiers(state);
12335 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012336 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012337 end_scope(state);
12338 return type;
12339}
12340
12341static struct type *param_type_list(struct compile_state *state, struct type *type)
12342{
12343 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012344 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012345 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012346 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012347 while(peek(state) == TOK_COMMA) {
12348 eat(state, TOK_COMMA);
12349 if (peek(state) == TOK_DOTS) {
12350 eat(state, TOK_DOTS);
12351 error(state, 0, "variadic functions not supported");
12352 }
12353 else {
12354 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12355 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012356 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012357 }
12358 }
12359 return ftype;
12360}
12361
Eric Biedermanb138ac82003-04-22 18:44:01 +000012362static struct type *type_name(struct compile_state *state)
12363{
12364 struct type *type;
12365 type = specifier_qualifier_list(state);
12366 /* abstract-declarator (may consume no tokens) */
12367 type = declarator(state, type, 0, 0);
12368 return type;
12369}
12370
12371static struct type *direct_declarator(
12372 struct compile_state *state, struct type *type,
12373 struct hash_entry **ident, int need_ident)
12374{
12375 struct type *outer;
12376 int op;
12377 outer = 0;
12378 arrays_complete(state, type);
12379 switch(peek(state)) {
12380 case TOK_IDENT:
12381 eat(state, TOK_IDENT);
12382 if (!ident) {
12383 error(state, 0, "Unexpected identifier found");
12384 }
12385 /* The name of what we are declaring */
12386 *ident = state->token[0].ident;
12387 break;
12388 case TOK_LPAREN:
12389 eat(state, TOK_LPAREN);
12390 outer = declarator(state, type, ident, need_ident);
12391 eat(state, TOK_RPAREN);
12392 break;
12393 default:
12394 if (need_ident) {
12395 error(state, 0, "Identifier expected");
12396 }
12397 break;
12398 }
12399 do {
12400 op = 1;
12401 arrays_complete(state, type);
12402 switch(peek(state)) {
12403 case TOK_LPAREN:
12404 eat(state, TOK_LPAREN);
12405 type = param_type_list(state, type);
12406 eat(state, TOK_RPAREN);
12407 break;
12408 case TOK_LBRACKET:
12409 {
12410 unsigned int qualifiers;
12411 struct triple *value;
12412 value = 0;
12413 eat(state, TOK_LBRACKET);
12414 if (peek(state) != TOK_RBRACKET) {
12415 value = constant_expr(state);
12416 integral(state, value);
12417 }
12418 eat(state, TOK_RBRACKET);
12419
12420 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12421 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12422 if (value) {
12423 type->elements = value->u.cval;
12424 free_triple(state, value);
12425 } else {
12426 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12427 op = 0;
12428 }
12429 }
12430 break;
12431 default:
12432 op = 0;
12433 break;
12434 }
12435 } while(op);
12436 if (outer) {
12437 struct type *inner;
12438 arrays_complete(state, type);
12439 FINISHME();
12440 for(inner = outer; inner->left; inner = inner->left)
12441 ;
12442 inner->left = type;
12443 type = outer;
12444 }
12445 return type;
12446}
12447
12448static struct type *declarator(
12449 struct compile_state *state, struct type *type,
12450 struct hash_entry **ident, int need_ident)
12451{
12452 while(peek(state) == TOK_STAR) {
12453 eat(state, TOK_STAR);
12454 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12455 }
12456 type = direct_declarator(state, type, ident, need_ident);
12457 return type;
12458}
12459
Eric Biedermanb138ac82003-04-22 18:44:01 +000012460static struct type *typedef_name(
12461 struct compile_state *state, unsigned int specifiers)
12462{
12463 struct hash_entry *ident;
12464 struct type *type;
12465 eat(state, TOK_TYPE_NAME);
12466 ident = state->token[0].ident;
12467 type = ident->sym_ident->type;
12468 specifiers |= type->type & QUAL_MASK;
12469 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
12470 (type->type & (STOR_MASK | QUAL_MASK))) {
12471 type = clone_type(specifiers, type);
12472 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012473 return type;
12474}
12475
12476static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012477 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012478{
Eric Biederman83b991a2003-10-11 06:20:25 +000012479 struct hash_entry *ident;
12480 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012481 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012482 struct type *enum_type;
12483 enum_type = 0;
12484 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012485 eat(state, TOK_ENUM);
12486 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012487 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
12488 eat(state, tok);
12489 ident = state->token[0].ident;
12490
Eric Biedermanb138ac82003-04-22 18:44:01 +000012491 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012492 base = 0;
12493 if (!ident || (peek(state) == TOK_LBRACE)) {
12494 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012495 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012496 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12497 enum_type->type_ident = ident;
12498 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012499 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012500 struct hash_entry *eident;
12501 struct triple *value;
12502 struct type *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012503 eat(state, TOK_IDENT);
Eric Biederman83b991a2003-10-11 06:20:25 +000012504 eident = state->token[0].ident;
12505 if (eident->sym_ident) {
12506 error(state, 0, "%s already declared",
12507 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012508 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012509 eident->tok = TOK_ENUM_CONST;
12510 if (peek(state) == TOK_EQ) {
12511 struct triple *val;
12512 eat(state, TOK_EQ);
12513 val = constant_expr(state);
12514 integral(state, val);
12515 base = val->u.cval;
12516 }
12517 value = int_const(state, &int_type, base);
12518 symbol(state, eident, &eident->sym_ident, value, &int_type);
12519 entry = new_type(TYPE_LIST, 0, 0);
12520 entry->field_ident = eident;
12521 *next = entry;
12522 next = &entry->right;
12523 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012524 if (peek(state) == TOK_COMMA) {
12525 eat(state, TOK_COMMA);
12526 }
12527 } while(peek(state) != TOK_RBRACE);
12528 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012529 if (ident) {
12530 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12531 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012532 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012533 if (ident && ident->sym_tag &&
12534 ident->sym_tag->type &&
12535 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12536 enum_type = clone_type(spec, ident->sym_tag->type);
12537 }
12538 else if (ident && !enum_type) {
12539 error(state, 0, "enum %s undeclared", ident->name);
12540 }
12541 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012542}
12543
Eric Biedermanb138ac82003-04-22 18:44:01 +000012544static struct type *struct_declarator(
12545 struct compile_state *state, struct type *type, struct hash_entry **ident)
12546{
Eric Biederman90089602004-05-28 14:11:54 +000012547 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012548 type = declarator(state, type, ident, 1);
12549 }
Eric Biederman90089602004-05-28 14:11:54 +000012550 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012551 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012552 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012553 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012554 if (value->op != OP_INTCONST) {
12555 error(state, 0, "Invalid constant expression");
12556 }
12557 if (value->u.cval > size_of(state, type)) {
12558 error(state, 0, "bitfield larger than base type");
12559 }
12560 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12561 error(state, 0, "bitfield base not an integer type");
12562 }
12563 type = new_type(TYPE_BITFIELD, type, 0);
12564 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012565 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012566 return type;
12567}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012568
12569static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012570 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012571{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012572 struct type *struct_type;
12573 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012574 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012575 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012576 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012577 struct_type = 0;
12578 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012579 switch(peek(state)) {
12580 case TOK_STRUCT:
12581 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012582 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012583 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012584 break;
12585 case TOK_UNION:
12586 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012587 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012588 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012589 break;
12590 default:
12591 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012592 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012593 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012594 break;
12595 }
12596 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012597 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000012598 eat(state, tok);
12599 ident = state->token[0].ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012600 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012601 if (!ident || (peek(state) == TOK_LBRACE)) {
12602 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012603 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012604 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012605 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012606 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012607 do {
12608 struct type *base_type;
12609 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012610 base_type = specifier_qualifier_list(state);
12611 do {
12612 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012613 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012614 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012615 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012616 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012617 if (peek(state) == TOK_COMMA) {
12618 done = 0;
12619 eat(state, TOK_COMMA);
12620 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012621 type = clone_type(0, type);
12622 type->field_ident = fident;
12623 if (*next) {
12624 *next = new_type(type_join, *next, type);
12625 next = &((*next)->right);
12626 } else {
12627 *next = type;
12628 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012629 } while(!done);
12630 eat(state, TOK_SEMI);
12631 } while(peek(state) != TOK_RBRACE);
12632 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012633 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012634 struct_type->type_ident = ident;
12635 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012636 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012637 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012638 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012639 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012640 if (ident && ident->sym_tag &&
12641 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012642 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012643 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012644 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012645 else if (ident && !struct_type) {
Eric Biederman90089602004-05-28 14:11:54 +000012646 error(state, 0, "%s %s undeclared",
12647 (type_main == TYPE_STRUCT)?"struct" : "union",
12648 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012649 }
12650 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012651}
12652
12653static unsigned int storage_class_specifier_opt(struct compile_state *state)
12654{
12655 unsigned int specifiers;
12656 switch(peek(state)) {
12657 case TOK_AUTO:
12658 eat(state, TOK_AUTO);
12659 specifiers = STOR_AUTO;
12660 break;
12661 case TOK_REGISTER:
12662 eat(state, TOK_REGISTER);
12663 specifiers = STOR_REGISTER;
12664 break;
12665 case TOK_STATIC:
12666 eat(state, TOK_STATIC);
12667 specifiers = STOR_STATIC;
12668 break;
12669 case TOK_EXTERN:
12670 eat(state, TOK_EXTERN);
12671 specifiers = STOR_EXTERN;
12672 break;
12673 case TOK_TYPEDEF:
12674 eat(state, TOK_TYPEDEF);
12675 specifiers = STOR_TYPEDEF;
12676 break;
12677 default:
12678 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012679 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012680 }
12681 else {
12682 specifiers = STOR_AUTO;
12683 }
12684 }
12685 return specifiers;
12686}
12687
12688static unsigned int function_specifier_opt(struct compile_state *state)
12689{
12690 /* Ignore the inline keyword */
12691 unsigned int specifiers;
12692 specifiers = 0;
12693 switch(peek(state)) {
12694 case TOK_INLINE:
12695 eat(state, TOK_INLINE);
12696 specifiers = STOR_INLINE;
12697 }
12698 return specifiers;
12699}
12700
Eric Biederman90089602004-05-28 14:11:54 +000012701static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12702{
12703 int tok = peek(state);
12704 switch(tok) {
12705 case TOK_COMMA:
12706 case TOK_LPAREN:
12707 /* The empty attribute ignore it */
12708 break;
12709 case TOK_IDENT:
12710 case TOK_ENUM_CONST:
12711 case TOK_TYPE_NAME:
12712 {
12713 struct hash_entry *ident;
12714 eat(state, TOK_IDENT);
12715 ident = state->token[0].ident;
12716
12717 if (ident == state->i_noinline) {
12718 if (attributes & ATTRIB_ALWAYS_INLINE) {
12719 error(state, 0, "both always_inline and noinline attribtes");
12720 }
12721 attributes |= ATTRIB_NOINLINE;
12722 }
12723 else if (ident == state->i_always_inline) {
12724 if (attributes & ATTRIB_NOINLINE) {
12725 error(state, 0, "both noinline and always_inline attribtes");
12726 }
12727 attributes |= ATTRIB_ALWAYS_INLINE;
12728 }
12729 else {
12730 error(state, 0, "Unknown attribute:%s", ident->name);
12731 }
12732 break;
12733 }
12734 default:
12735 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12736 break;
12737 }
12738 return attributes;
12739}
12740
12741static unsigned int attribute_list(struct compile_state *state, unsigned type)
12742{
12743 type = attrib(state, type);
12744 while(peek(state) == TOK_COMMA) {
12745 eat(state, TOK_COMMA);
12746 type = attrib(state, type);
12747 }
12748 return type;
12749}
12750
12751static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12752{
12753 if (peek(state) == TOK_ATTRIBUTE) {
12754 eat(state, TOK_ATTRIBUTE);
12755 eat(state, TOK_LPAREN);
12756 eat(state, TOK_LPAREN);
12757 type = attribute_list(state, type);
12758 eat(state, TOK_RPAREN);
12759 eat(state, TOK_RPAREN);
12760 }
12761 return type;
12762}
12763
Eric Biedermanb138ac82003-04-22 18:44:01 +000012764static unsigned int type_qualifiers(struct compile_state *state)
12765{
12766 unsigned int specifiers;
12767 int done;
12768 done = 0;
12769 specifiers = QUAL_NONE;
12770 do {
12771 switch(peek(state)) {
12772 case TOK_CONST:
12773 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012774 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012775 break;
12776 case TOK_VOLATILE:
12777 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012778 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012779 break;
12780 case TOK_RESTRICT:
12781 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012782 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012783 break;
12784 default:
12785 done = 1;
12786 break;
12787 }
12788 } while(!done);
12789 return specifiers;
12790}
12791
12792static struct type *type_specifier(
12793 struct compile_state *state, unsigned int spec)
12794{
12795 struct type *type;
12796 type = 0;
12797 switch(peek(state)) {
12798 case TOK_VOID:
12799 eat(state, TOK_VOID);
12800 type = new_type(TYPE_VOID | spec, 0, 0);
12801 break;
12802 case TOK_CHAR:
12803 eat(state, TOK_CHAR);
12804 type = new_type(TYPE_CHAR | spec, 0, 0);
12805 break;
12806 case TOK_SHORT:
12807 eat(state, TOK_SHORT);
12808 if (peek(state) == TOK_INT) {
12809 eat(state, TOK_INT);
12810 }
12811 type = new_type(TYPE_SHORT | spec, 0, 0);
12812 break;
12813 case TOK_INT:
12814 eat(state, TOK_INT);
12815 type = new_type(TYPE_INT | spec, 0, 0);
12816 break;
12817 case TOK_LONG:
12818 eat(state, TOK_LONG);
12819 switch(peek(state)) {
12820 case TOK_LONG:
12821 eat(state, TOK_LONG);
12822 error(state, 0, "long long not supported");
12823 break;
12824 case TOK_DOUBLE:
12825 eat(state, TOK_DOUBLE);
12826 error(state, 0, "long double not supported");
12827 break;
12828 case TOK_INT:
12829 eat(state, TOK_INT);
12830 type = new_type(TYPE_LONG | spec, 0, 0);
12831 break;
12832 default:
12833 type = new_type(TYPE_LONG | spec, 0, 0);
12834 break;
12835 }
12836 break;
12837 case TOK_FLOAT:
12838 eat(state, TOK_FLOAT);
12839 error(state, 0, "type float not supported");
12840 break;
12841 case TOK_DOUBLE:
12842 eat(state, TOK_DOUBLE);
12843 error(state, 0, "type double not supported");
12844 break;
12845 case TOK_SIGNED:
12846 eat(state, TOK_SIGNED);
12847 switch(peek(state)) {
12848 case TOK_LONG:
12849 eat(state, TOK_LONG);
12850 switch(peek(state)) {
12851 case TOK_LONG:
12852 eat(state, TOK_LONG);
12853 error(state, 0, "type long long not supported");
12854 break;
12855 case TOK_INT:
12856 eat(state, TOK_INT);
12857 type = new_type(TYPE_LONG | spec, 0, 0);
12858 break;
12859 default:
12860 type = new_type(TYPE_LONG | spec, 0, 0);
12861 break;
12862 }
12863 break;
12864 case TOK_INT:
12865 eat(state, TOK_INT);
12866 type = new_type(TYPE_INT | spec, 0, 0);
12867 break;
12868 case TOK_SHORT:
12869 eat(state, TOK_SHORT);
12870 type = new_type(TYPE_SHORT | spec, 0, 0);
12871 break;
12872 case TOK_CHAR:
12873 eat(state, TOK_CHAR);
12874 type = new_type(TYPE_CHAR | spec, 0, 0);
12875 break;
12876 default:
12877 type = new_type(TYPE_INT | spec, 0, 0);
12878 break;
12879 }
12880 break;
12881 case TOK_UNSIGNED:
12882 eat(state, TOK_UNSIGNED);
12883 switch(peek(state)) {
12884 case TOK_LONG:
12885 eat(state, TOK_LONG);
12886 switch(peek(state)) {
12887 case TOK_LONG:
12888 eat(state, TOK_LONG);
12889 error(state, 0, "unsigned long long not supported");
12890 break;
12891 case TOK_INT:
12892 eat(state, TOK_INT);
12893 type = new_type(TYPE_ULONG | spec, 0, 0);
12894 break;
12895 default:
12896 type = new_type(TYPE_ULONG | spec, 0, 0);
12897 break;
12898 }
12899 break;
12900 case TOK_INT:
12901 eat(state, TOK_INT);
12902 type = new_type(TYPE_UINT | spec, 0, 0);
12903 break;
12904 case TOK_SHORT:
12905 eat(state, TOK_SHORT);
12906 type = new_type(TYPE_USHORT | spec, 0, 0);
12907 break;
12908 case TOK_CHAR:
12909 eat(state, TOK_CHAR);
12910 type = new_type(TYPE_UCHAR | spec, 0, 0);
12911 break;
12912 default:
12913 type = new_type(TYPE_UINT | spec, 0, 0);
12914 break;
12915 }
12916 break;
12917 /* struct or union specifier */
12918 case TOK_STRUCT:
12919 case TOK_UNION:
12920 type = struct_or_union_specifier(state, spec);
12921 break;
12922 /* enum-spefifier */
12923 case TOK_ENUM:
12924 type = enum_specifier(state, spec);
12925 break;
12926 /* typedef name */
12927 case TOK_TYPE_NAME:
12928 type = typedef_name(state, spec);
12929 break;
12930 default:
12931 error(state, 0, "bad type specifier %s",
12932 tokens[peek(state)]);
12933 break;
12934 }
12935 return type;
12936}
12937
12938static int istype(int tok)
12939{
12940 switch(tok) {
12941 case TOK_CONST:
12942 case TOK_RESTRICT:
12943 case TOK_VOLATILE:
12944 case TOK_VOID:
12945 case TOK_CHAR:
12946 case TOK_SHORT:
12947 case TOK_INT:
12948 case TOK_LONG:
12949 case TOK_FLOAT:
12950 case TOK_DOUBLE:
12951 case TOK_SIGNED:
12952 case TOK_UNSIGNED:
12953 case TOK_STRUCT:
12954 case TOK_UNION:
12955 case TOK_ENUM:
12956 case TOK_TYPE_NAME:
12957 return 1;
12958 default:
12959 return 0;
12960 }
12961}
12962
12963
12964static struct type *specifier_qualifier_list(struct compile_state *state)
12965{
12966 struct type *type;
12967 unsigned int specifiers = 0;
12968
12969 /* type qualifiers */
12970 specifiers |= type_qualifiers(state);
12971
12972 /* type specifier */
12973 type = type_specifier(state, specifiers);
12974
12975 return type;
12976}
12977
12978static int isdecl_specifier(int tok)
12979{
12980 switch(tok) {
12981 /* storage class specifier */
12982 case TOK_AUTO:
12983 case TOK_REGISTER:
12984 case TOK_STATIC:
12985 case TOK_EXTERN:
12986 case TOK_TYPEDEF:
12987 /* type qualifier */
12988 case TOK_CONST:
12989 case TOK_RESTRICT:
12990 case TOK_VOLATILE:
12991 /* type specifiers */
12992 case TOK_VOID:
12993 case TOK_CHAR:
12994 case TOK_SHORT:
12995 case TOK_INT:
12996 case TOK_LONG:
12997 case TOK_FLOAT:
12998 case TOK_DOUBLE:
12999 case TOK_SIGNED:
13000 case TOK_UNSIGNED:
13001 /* struct or union specifier */
13002 case TOK_STRUCT:
13003 case TOK_UNION:
13004 /* enum-spefifier */
13005 case TOK_ENUM:
13006 /* typedef name */
13007 case TOK_TYPE_NAME:
13008 /* function specifiers */
13009 case TOK_INLINE:
13010 return 1;
13011 default:
13012 return 0;
13013 }
13014}
13015
13016static struct type *decl_specifiers(struct compile_state *state)
13017{
13018 struct type *type;
13019 unsigned int specifiers;
13020 /* I am overly restrictive in the arragement of specifiers supported.
13021 * C is overly flexible in this department it makes interpreting
13022 * the parse tree difficult.
13023 */
13024 specifiers = 0;
13025
13026 /* storage class specifier */
13027 specifiers |= storage_class_specifier_opt(state);
13028
13029 /* function-specifier */
13030 specifiers |= function_specifier_opt(state);
13031
Eric Biederman90089602004-05-28 14:11:54 +000013032 /* attributes */
13033 specifiers |= attributes_opt(state, 0);
13034
Eric Biedermanb138ac82003-04-22 18:44:01 +000013035 /* type qualifier */
13036 specifiers |= type_qualifiers(state);
13037
13038 /* type specifier */
13039 type = type_specifier(state, specifiers);
13040 return type;
13041}
13042
Eric Biederman00443072003-06-24 12:34:45 +000013043struct field_info {
13044 struct type *type;
13045 size_t offset;
13046};
13047
13048static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013049{
13050 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013051 struct field_info info;
13052 info.offset = ~0U;
13053 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013054 do {
13055 switch(peek(state)) {
13056 case TOK_LBRACKET:
13057 {
13058 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013059 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13060 error(state, 0, "Array designator not in array initializer");
13061 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013062 eat(state, TOK_LBRACKET);
13063 value = constant_expr(state);
13064 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013065
13066 info.type = type->left;
13067 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013068 break;
13069 }
13070 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013071 {
13072 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013073 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13074 ((type->type & TYPE_MASK) != TYPE_UNION))
13075 {
Eric Biederman00443072003-06-24 12:34:45 +000013076 error(state, 0, "Struct designator not in struct initializer");
13077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013078 eat(state, TOK_DOT);
13079 eat(state, TOK_IDENT);
Eric Biederman00443072003-06-24 12:34:45 +000013080 field = state->token[0].ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013081 info.offset = field_offset(state, type, field);
13082 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013083 break;
Eric Biederman00443072003-06-24 12:34:45 +000013084 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013085 default:
13086 error(state, 0, "Invalid designator");
13087 }
13088 tok = peek(state);
13089 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13090 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013091 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013092}
13093
13094static struct triple *initializer(
13095 struct compile_state *state, struct type *type)
13096{
13097 struct triple *result;
Eric Biedermane058a1e2003-07-12 01:21:31 +000013098#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Eric Biedermanb138ac82003-04-22 18:44:01 +000013099 if (peek(state) != TOK_LBRACE) {
13100 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013101 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13102 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13103 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13104 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13105 (equiv_types(type->left, result->type->left))) {
13106 type->elements = result->type->elements;
13107 }
Eric Biederman90089602004-05-28 14:11:54 +000013108 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013109 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13110 (type->type & TYPE_MASK) != TYPE_ARRAY)
13111 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013112 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013113 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013114 if (!is_init_compatible(state, type, result->type)) {
13115 error(state, 0, "Incompatible types in initializer");
13116 }
13117 if (!equiv_types(type, result->type)) {
13118 result = mk_cast_expr(state, type, result);
13119 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013120 }
13121 else {
13122 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013123 size_t max_offset;
13124 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013125 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013126 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13127 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13128 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013129 }
Eric Biederman00443072003-06-24 12:34:45 +000013130 info.offset = 0;
13131 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013132 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13133 info.type = next_field(state, type, 0);
13134 }
Eric Biederman00443072003-06-24 12:34:45 +000013135 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13136 max_offset = 0;
13137 } else {
13138 max_offset = size_of(state, type);
13139 }
Eric Biederman90089602004-05-28 14:11:54 +000013140 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013141 eat(state, TOK_LBRACE);
13142 do {
13143 struct triple *value;
13144 struct type *value_type;
13145 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013146 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013147 int tok;
13148 comma = 0;
13149 tok = peek(state);
13150 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013151 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013152 }
Eric Biederman00443072003-06-24 12:34:45 +000013153 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13154 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013155 error(state, 0, "element beyond bounds");
13156 }
Eric Biederman00443072003-06-24 12:34:45 +000013157 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013158 value = eval_const_expr(state, initializer(state, value_type));
13159 value_size = size_of(state, value_type);
13160 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013161 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13162 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013163 void *old_buf;
13164 size_t old_size;
13165 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013166 old_size = max_offset;
13167 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013168 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13169 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013170 xfree(old_buf);
13171 }
Eric Biederman90089602004-05-28 14:11:54 +000013172 dest = ((char *)buf) + bits_to_bytes(info.offset);
13173#if DEBUG_INITIALIZER
13174 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
13175 dest - buf,
13176 bits_to_bytes(max_offset),
13177 bits_to_bytes(value_size),
13178 value->op);
13179#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013180 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013181 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013182 }
Eric Biederman90089602004-05-28 14:11:54 +000013183 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13184#if DEBUG_INITIALIZER
13185 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13186#endif
Eric Biederman00443072003-06-24 12:34:45 +000013187 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013188 }
Eric Biederman90089602004-05-28 14:11:54 +000013189 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013190 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013191 }
Eric Biederman90089602004-05-28 14:11:54 +000013192 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013193 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013194 }
13195 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013196 internal_error(state, 0, "unhandled constant initializer");
13197 }
Eric Biederman00443072003-06-24 12:34:45 +000013198 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013199 if (peek(state) == TOK_COMMA) {
13200 eat(state, TOK_COMMA);
13201 comma = 1;
13202 }
Eric Biederman00443072003-06-24 12:34:45 +000013203 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013204 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13205 info.type = next_field(state, type, info.type);
13206 info.offset = field_offset(state, type,
13207 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013208 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013209 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013210 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13211 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13212 type->elements = max_offset / size_of(state, type->left);
13213 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013214 eat(state, TOK_RBRACE);
13215 result = triple(state, OP_BLOBCONST, type, 0, 0);
13216 result->u.blob = buf;
13217 }
13218 return result;
13219}
13220
Eric Biederman90089602004-05-28 14:11:54 +000013221static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013222{
13223 /* Make a second pass and finish anything outstanding
13224 * with respect to branches. The only outstanding item
13225 * is to see if there are goto to labels that have not
13226 * been defined and to error about them.
13227 */
13228 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013229 struct triple *ins;
13230 /* Also error on branches that do not use their targets */
13231 ins = first;
13232 do {
13233 if (!triple_is_ret(state, ins)) {
13234 struct triple **expr ;
13235 struct triple_set *set;
13236 expr = triple_targ(state, ins, 0);
13237 for(; expr; expr = triple_targ(state, ins, expr)) {
13238 struct triple *targ;
13239 targ = *expr;
13240 for(set = targ?targ->use:0; set; set = set->next) {
13241 if (set->member == ins) {
13242 break;
13243 }
13244 }
13245 if (!set) {
13246 internal_error(state, ins, "targ not used");
13247 }
13248 }
13249 }
13250 ins = ins->next;
13251 } while(ins != first);
13252 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013253 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13254 struct hash_entry *entry;
13255 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13256 struct triple *ins;
13257 if (!entry->sym_label) {
13258 continue;
13259 }
13260 ins = entry->sym_label->def;
13261 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13262 error(state, ins, "label `%s' used but not defined",
13263 entry->name);
13264 }
13265 }
13266 }
13267}
13268
Eric Biedermanb138ac82003-04-22 18:44:01 +000013269static struct triple *function_definition(
13270 struct compile_state *state, struct type *type)
13271{
Eric Biederman90089602004-05-28 14:11:54 +000013272 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13273 struct triple *fname;
13274 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013275 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013276 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013277 int i;
13278 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13279 error(state, 0, "Invalid function header");
13280 }
13281
13282 /* Verify the function type */
13283 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13284 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013285 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013286 error(state, 0, "Invalid function parameters");
13287 }
13288 param = type->right;
13289 i = 0;
13290 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13291 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013292 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013293 error(state, 0, "No identifier for parameter %d\n", i);
13294 }
13295 param = param->right;
13296 }
13297 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013298 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013299 error(state, 0, "No identifier for paramter %d\n", i);
13300 }
13301
13302 /* Get a list of statements for this function. */
13303 def = triple(state, OP_LIST, type, 0, 0);
13304
13305 /* Start a new scope for the passed parameters */
13306 start_scope(state);
13307
13308 /* Put a label at the very start of a function */
13309 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013310 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013311
13312 /* Put a label at the very end of a function */
13313 end = label(state);
13314 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013315 /* Remember where return goes */
13316 ident = state->i_return;
13317 symbol(state, ident, &ident->sym_ident, end, end->type);
13318
Eric Biederman90089602004-05-28 14:11:54 +000013319 /* Get the initial closure type */
13320 ctype = new_type(TYPE_JOIN, &void_type, 0);
13321 ctype->elements = 1;
13322
13323 /* Add a variable for the return value */
13324 crtype = new_type(TYPE_TUPLE,
13325 /* Remove all type qualifiers from the return type */
13326 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13327 crtype->elements = 2;
13328 result = flatten(state, end, variable(state, crtype));
13329
Eric Biederman5ade04a2003-10-22 04:03:46 +000013330 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013331 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013332
13333 /* Add in the return instruction */
13334 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13335 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013336
13337 /* Walk through the parameters and create symbol table entries
13338 * for them.
13339 */
13340 param = type->right;
13341 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013342 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013343 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013344 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013345 flatten(state, end, tmp);
13346 param = param->right;
13347 }
13348 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13349 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013350 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013351 tmp = variable(state, param);
13352 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13353 flatten(state, end, tmp);
13354 }
Eric Biederman90089602004-05-28 14:11:54 +000013355
13356 /* Add the declaration static const char __func__ [] = "func-name" */
13357 fname_type = new_type(TYPE_ARRAY,
13358 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13359 fname_type->type |= QUAL_CONST | STOR_STATIC;
13360 fname_type->elements = strlen(state->function) + 1;
13361
13362 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13363 fname->u.blob = (void *)state->function;
13364 fname = flatten(state, end, fname);
13365
13366 ident = state->i___func__;
13367 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013368
13369 /* Remember which function I am compiling.
13370 * Also assume the last defined function is the main function.
13371 */
13372 state->main_function = def;
13373
13374 /* Now get the actual function definition */
13375 compound_statement(state, end);
13376
Eric Biederman153ea352003-06-20 14:43:20 +000013377 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013378 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013379
Eric Biedermanb138ac82003-04-22 18:44:01 +000013380 /* Remove the parameter scope */
13381 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013382
Eric Biederman5ade04a2003-10-22 04:03:46 +000013383
13384 /* Remember I have defined a function */
13385 if (!state->functions) {
13386 state->functions = def;
13387 } else {
13388 insert_triple(state, state->functions, def);
13389 }
13390 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013391 FILE *fp = state->dbgout;
13392 fprintf(fp, "\n");
13393 loc(fp, state, 0);
13394 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13395 display_func(state, fp, def);
13396 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013397 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013398
13399 return def;
13400}
13401
13402static struct triple *do_decl(struct compile_state *state,
13403 struct type *type, struct hash_entry *ident)
13404{
13405 struct triple *def;
13406 def = 0;
13407 /* Clean up the storage types used */
13408 switch (type->type & STOR_MASK) {
13409 case STOR_AUTO:
13410 case STOR_STATIC:
13411 /* These are the good types I am aiming for */
13412 break;
13413 case STOR_REGISTER:
13414 type->type &= ~STOR_MASK;
13415 type->type |= STOR_AUTO;
13416 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013417 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013418 case STOR_EXTERN:
13419 type->type &= ~STOR_MASK;
13420 type->type |= STOR_STATIC;
13421 break;
13422 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013423 if (!ident) {
13424 error(state, 0, "typedef without name");
13425 }
13426 symbol(state, ident, &ident->sym_ident, 0, type);
13427 ident->tok = TOK_TYPE_NAME;
13428 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013429 break;
13430 default:
13431 internal_error(state, 0, "Undefined storage class");
13432 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013433 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13434 error(state, 0, "Function prototypes not supported");
13435 }
Eric Biederman00443072003-06-24 12:34:45 +000013436 if (ident &&
13437 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013438 ((type->type & QUAL_CONST) == 0)) {
13439 error(state, 0, "non const static variables not supported");
13440 }
13441 if (ident) {
13442 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013443 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013444 }
13445 return def;
13446}
13447
13448static void decl(struct compile_state *state, struct triple *first)
13449{
13450 struct type *base_type, *type;
13451 struct hash_entry *ident;
13452 struct triple *def;
13453 int global;
13454 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13455 base_type = decl_specifiers(state);
13456 ident = 0;
13457 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013458 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013459 if (global && ident && (peek(state) == TOK_LBRACE)) {
13460 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013461 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013462 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013463 def = function_definition(state, type);
13464 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013465 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013466 }
13467 else {
13468 int done;
13469 flatten(state, first, do_decl(state, type, ident));
13470 /* type or variable definition */
13471 do {
13472 done = 1;
13473 if (peek(state) == TOK_EQ) {
13474 if (!ident) {
13475 error(state, 0, "cannot assign to a type");
13476 }
13477 eat(state, TOK_EQ);
13478 flatten(state, first,
13479 init_expr(state,
13480 ident->sym_ident->def,
13481 initializer(state, type)));
13482 }
13483 arrays_complete(state, type);
13484 if (peek(state) == TOK_COMMA) {
13485 eat(state, TOK_COMMA);
13486 ident = 0;
13487 type = declarator(state, base_type, &ident, 0);
13488 flatten(state, first, do_decl(state, type, ident));
13489 done = 0;
13490 }
13491 } while(!done);
13492 eat(state, TOK_SEMI);
13493 }
13494}
13495
13496static void decls(struct compile_state *state)
13497{
13498 struct triple *list;
13499 int tok;
13500 list = label(state);
13501 while(1) {
13502 tok = peek(state);
13503 if (tok == TOK_EOF) {
13504 return;
13505 }
13506 if (tok == TOK_SPACE) {
13507 eat(state, TOK_SPACE);
13508 }
13509 decl(state, list);
13510 if (list->next != list) {
13511 error(state, 0, "global variables not supported");
13512 }
13513 }
13514}
13515
Eric Biederman5ade04a2003-10-22 04:03:46 +000013516/*
13517 * Function inlining
13518 */
Eric Biederman90089602004-05-28 14:11:54 +000013519struct triple_reg_set {
13520 struct triple_reg_set *next;
13521 struct triple *member;
13522 struct triple *new;
13523};
13524struct reg_block {
13525 struct block *block;
13526 struct triple_reg_set *in;
13527 struct triple_reg_set *out;
13528 int vertex;
13529};
13530static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13531static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13532static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13533static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13534static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13535 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13536 void *arg);
13537static void print_block(
13538 struct compile_state *state, struct block *block, void *arg);
13539static int do_triple_set(struct triple_reg_set **head,
13540 struct triple *member, struct triple *new_member);
13541static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13542static struct reg_block *compute_variable_lifetimes(
13543 struct compile_state *state, struct basic_blocks *bb);
13544static void free_variable_lifetimes(struct compile_state *state,
13545 struct basic_blocks *bb, struct reg_block *blocks);
13546static void print_live_variables(struct compile_state *state,
13547 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
13548
Eric Biederman5ade04a2003-10-22 04:03:46 +000013549
13550static struct triple *call(struct compile_state *state,
13551 struct triple *retvar, struct triple *ret_addr,
13552 struct triple *targ, struct triple *ret)
13553{
13554 struct triple *call;
13555
13556 if (!retvar || !is_lvalue(state, retvar)) {
13557 internal_error(state, 0, "writing to a non lvalue?");
13558 }
13559 write_compatible(state, retvar->type, &void_ptr_type);
13560
13561 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13562 TARG(call, 0) = targ;
13563 MISC(call, 0) = ret;
13564 if (!targ || (targ->op != OP_LABEL)) {
13565 internal_error(state, 0, "call not to a label");
13566 }
13567 if (!ret || (ret->op != OP_RET)) {
13568 internal_error(state, 0, "call not matched with return");
13569 }
13570 return call;
13571}
13572
Eric Biederman5ade04a2003-10-22 04:03:46 +000013573static void walk_functions(struct compile_state *state,
13574 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13575 void *arg)
13576{
13577 struct triple *func, *first;
13578 func = first = state->functions;
13579 do {
13580 cb(state, func, arg);
13581 func = func->next;
13582 } while(func != first);
13583}
13584
Eric Biederman90089602004-05-28 14:11:54 +000013585static void reverse_walk_functions(struct compile_state *state,
13586 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13587 void *arg)
13588{
13589 struct triple *func, *first;
13590 func = first = state->functions;
13591 do {
13592 func = func->prev;
13593 cb(state, func, arg);
13594 } while(func != first);
13595}
13596
13597
13598static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13599{
13600 struct triple *ptr, *first;
13601 if (func->u.cval == 0) {
13602 return;
13603 }
13604 ptr = first = RHS(func, 0);
13605 do {
13606 if (ptr->op == OP_FCALL) {
13607 struct triple *called_func;
13608 called_func = MISC(ptr, 0);
13609 /* Mark the called function as used */
13610 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13611 called_func->u.cval++;
13612 }
13613 /* Remove the called function from the list */
13614 called_func->prev->next = called_func->next;
13615 called_func->next->prev = called_func->prev;
13616
13617 /* Place the called function before me on the list */
13618 called_func->next = func;
13619 called_func->prev = func->prev;
13620 called_func->prev->next = called_func;
13621 called_func->next->prev = called_func;
13622 }
13623 ptr = ptr->next;
13624 } while(ptr != first);
13625 func->id |= TRIPLE_FLAG_FLATTENED;
13626}
13627
13628static void mark_live_functions(struct compile_state *state)
13629{
13630 /* Ensure state->main_function is the last function in
13631 * the list of functions.
13632 */
13633 if ((state->main_function->next != state->functions) ||
13634 (state->functions->prev != state->main_function)) {
13635 internal_error(state, 0,
13636 "state->main_function is not at the end of the function list ");
13637 }
13638 state->main_function->u.cval = 1;
13639 reverse_walk_functions(state, mark_live, 0);
13640}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013641
13642static int local_triple(struct compile_state *state,
13643 struct triple *func, struct triple *ins)
13644{
13645 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13646#if 0
13647 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013648 FILE *fp = state->errout;
13649 fprintf(fp, "global: ");
13650 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013651 }
13652#endif
13653 return local;
13654}
13655
13656struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
13657 struct occurance *base_occurance)
13658{
13659 struct triple *nfunc;
13660 struct triple *nfirst, *ofirst;
13661 struct triple *new, *old;
13662
13663 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013664 FILE *fp = state->dbgout;
13665 fprintf(fp, "\n");
13666 loc(fp, state, 0);
13667 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13668 display_func(state, fp, ofunc);
13669 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013670 }
13671
13672 /* Make a new copy of the old function */
13673 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13674 nfirst = 0;
13675 ofirst = old = RHS(ofunc, 0);
13676 do {
13677 struct triple *new;
13678 struct occurance *occurance;
13679 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013680 old_lhs = old->lhs;
13681 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013682 occurance = inline_occurance(state, base_occurance, old->occurance);
13683 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13684 MISC(old, 0)->u.cval += 1;
13685 }
13686 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13687 occurance);
13688 if (!triple_stores_block(state, new)) {
13689 memcpy(&new->u, &old->u, sizeof(new->u));
13690 }
13691 if (!nfirst) {
13692 RHS(nfunc, 0) = nfirst = new;
13693 }
13694 else {
13695 insert_triple(state, nfirst, new);
13696 }
13697 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013698 new->id |= old->id & TRIPLE_FLAG_COPY;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013699
13700 /* During the copy remember new as user of old */
13701 use_triple(old, new);
13702
Eric Biederman5ade04a2003-10-22 04:03:46 +000013703 /* Remember which instructions are local */
13704 old->id |= TRIPLE_FLAG_LOCAL;
13705 old = old->next;
13706 } while(old != ofirst);
13707
13708 /* Make a second pass to fix up any unresolved references */
13709 old = ofirst;
13710 new = nfirst;
13711 do {
13712 struct triple **oexpr, **nexpr;
13713 int count, i;
13714 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013715 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013716 for(i = 0; i < count; i++) {
13717 oexpr = &old->param[i];
13718 nexpr = &new->param[i];
13719 if (*oexpr && !*nexpr) {
13720 if (!local_triple(state, ofunc, *oexpr)) {
13721 *nexpr = *oexpr;
13722 }
13723 else if ((*oexpr)->use) {
13724 *nexpr = (*oexpr)->use->member;
13725 }
13726 if (*nexpr == old) {
13727 internal_error(state, 0, "new == old?");
13728 }
13729 use_triple(*nexpr, new);
13730 }
13731 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013732 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013733 }
13734 }
13735 old = old->next;
13736 new = new->next;
13737 } while((old != ofirst) && (new != nfirst));
13738
13739 /* Make a third pass to cleanup the extra useses */
13740 old = ofirst;
13741 new = nfirst;
13742 do {
13743 unuse_triple(old, new);
13744 /* Forget which instructions are local */
13745 old->id &= ~TRIPLE_FLAG_LOCAL;
13746 old = old->next;
13747 new = new->next;
13748 } while ((old != ofirst) && (new != nfirst));
13749 return nfunc;
13750}
13751
Eric Biederman90089602004-05-28 14:11:54 +000013752static void expand_inline_call(
13753 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013754{
13755 /* Inline the function call */
13756 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013757 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013758 struct triple *end, *nend;
13759 int pvals, i;
13760
13761 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013762 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013763 if (ofunc->op != OP_LIST) {
13764 internal_error(state, 0, "improper function");
13765 }
Eric Biederman90089602004-05-28 14:11:54 +000013766 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013767 /* Prepend the parameter reading into the new function list */
13768 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013769 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013770 for(i = 0; i < pvals; i++) {
13771 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013772 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013773 atype = ptype;
13774 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13775 atype = ptype->left;
13776 }
Eric Biederman90089602004-05-28 14:11:54 +000013777 param = farg(state, nfunc, i);
13778 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13779 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013780 }
Eric Biederman90089602004-05-28 14:11:54 +000013781 arg = RHS(fcall, i);
13782 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013783 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013784 }
13785 result = 0;
13786 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman90089602004-05-28 14:11:54 +000013787 result = read_expr(state,
13788 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013789 }
13790 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013791 FILE *fp = state->dbgout;
13792 fprintf(fp, "\n");
13793 loc(fp, state, 0);
13794 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13795 display_func(state, fp, nfunc);
13796 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013797 }
13798
Eric Biederman90089602004-05-28 14:11:54 +000013799 /*
13800 * Get rid of the extra triples
13801 */
13802 /* Remove the read of the return address */
13803 ins = RHS(nfunc, 0)->prev->prev;
13804 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13805 internal_error(state, ins, "Not return addres read?");
13806 }
13807 release_triple(state, ins);
13808 /* Remove the return instruction */
13809 ins = RHS(nfunc, 0)->prev;
13810 if (ins->op != OP_RET) {
13811 internal_error(state, ins, "Not return?");
13812 }
13813 release_triple(state, ins);
13814 /* Remove the retaddres variable */
13815 retvar = fretaddr(state, nfunc);
13816 if ((retvar->lhs != 1) ||
13817 (retvar->op != OP_ADECL) ||
13818 (retvar->next->op != OP_PIECE) ||
13819 (MISC(retvar->next, 0) != retvar)) {
13820 internal_error(state, retvar, "Not the return address?");
13821 }
13822 release_triple(state, retvar->next);
13823 release_triple(state, retvar);
13824
13825 /* Remove the label at the start of the function */
13826 ins = RHS(nfunc, 0);
13827 if (ins->op != OP_LABEL) {
13828 internal_error(state, ins, "Not label?");
13829 }
13830 nfirst = ins->next;
13831 free_triple(state, ins);
13832 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013833 RHS(nfunc, 0) = 0;
13834 free_triple(state, nfunc);
13835
13836 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013837 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013838 nend = nfirst->prev;
13839 end->next = nfirst;
13840 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013841 nend->next = fcall;
13842 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013843
Eric Biederman90089602004-05-28 14:11:54 +000013844 /* Now the result reading code */
13845 if (result) {
13846 result = flatten(state, fcall, result);
13847 propogate_use(state, fcall, result);
13848 }
13849
13850 /* Release the original fcall instruction */
13851 release_triple(state, fcall);
13852
13853 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013854}
13855
Eric Biederman90089602004-05-28 14:11:54 +000013856/*
13857 *
13858 * Type of the result variable.
13859 *
13860 * result
13861 * |
13862 * +----------+------------+
13863 * | |
13864 * union of closures result_type
13865 * |
13866 * +------------------+---------------+
13867 * | |
13868 * closure1 ... closuerN
13869 * | |
13870 * +----+--+-+--------+-----+ +----+----+---+-----+
13871 * | | | | | | | | |
13872 * var1 var2 var3 ... varN result var1 var2 ... varN result
13873 * |
13874 * +--------+---------+
13875 * | |
13876 * union of closures result_type
13877 * |
13878 * +-----+-------------------+
13879 * | |
13880 * closure1 ... closureN
13881 * | |
13882 * +-----+---+----+----+ +----+---+----+-----+
13883 * | | | | | | | |
13884 * var1 var2 ... varN result var1 var2 ... varN result
13885 */
13886
13887static int add_closure_type(struct compile_state *state,
13888 struct triple *func, struct type *closure_type)
13889{
13890 struct type *type, *ctype, **next;
13891 struct triple *var, *new_var;
13892 int i;
13893
13894#if 0
13895 FILE *fp = state->errout;
13896 fprintf(fp, "original_type: ");
13897 name_of(fp, fresult(state, func)->type);
13898 fprintf(fp, "\n");
13899#endif
13900 /* find the original type */
13901 var = fresult(state, func);
13902 type = var->type;
13903 if (type->elements != 2) {
13904 internal_error(state, var, "bad return type");
13905 }
13906
13907 /* Find the complete closure type and update it */
13908 ctype = type->left->left;
13909 next = &ctype->left;
13910 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13911 next = &(*next)->right;
13912 }
13913 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13914 ctype->elements += 1;
13915
13916#if 0
13917 fprintf(fp, "new_type: ");
13918 name_of(fp, type);
13919 fprintf(fp, "\n");
13920 fprintf(fp, "ctype: %p %d bits: %d ",
13921 ctype, ctype->elements, reg_size_of(state, ctype));
13922 name_of(fp, ctype);
13923 fprintf(fp, "\n");
13924#endif
13925
13926 /* Regenerate the variable with the new type definition */
13927 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13928 new_var->id |= TRIPLE_FLAG_FLATTENED;
13929 for(i = 0; i < new_var->lhs; i++) {
13930 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13931 }
13932
13933 /* Point everyone at the new variable */
13934 propogate_use(state, var, new_var);
13935
13936 /* Release the original variable */
13937 for(i = 0; i < var->lhs; i++) {
13938 release_triple(state, LHS(var, i));
13939 }
13940 release_triple(state, var);
13941
13942 /* Return the index of the added closure type */
13943 return ctype->elements - 1;
13944}
13945
13946static struct triple *closure_expr(struct compile_state *state,
13947 struct triple *func, int closure_idx, int var_idx)
13948{
13949 return deref_index(state,
13950 deref_index(state,
13951 deref_index(state, fresult(state, func), 0),
13952 closure_idx),
13953 var_idx);
13954}
13955
13956
13957static void insert_triple_set(
13958 struct triple_reg_set **head, struct triple *member)
13959{
13960 struct triple_reg_set *new;
13961 new = xcmalloc(sizeof(*new), "triple_set");
13962 new->member = member;
13963 new->new = 0;
13964 new->next = *head;
13965 *head = new;
13966}
13967
13968static int ordered_triple_set(
13969 struct triple_reg_set **head, struct triple *member)
13970{
13971 struct triple_reg_set **ptr;
13972 if (!member)
13973 return 0;
13974 ptr = head;
13975 while(*ptr) {
13976 if (member == (*ptr)->member) {
13977 return 0;
13978 }
13979 /* keep the list ordered */
13980 if (member->id < (*ptr)->member->id) {
13981 break;
13982 }
13983 ptr = &(*ptr)->next;
13984 }
13985 insert_triple_set(ptr, member);
13986 return 1;
13987}
13988
13989
13990static void free_closure_variables(struct compile_state *state,
13991 struct triple_reg_set **enclose)
13992{
13993 struct triple_reg_set *entry, *next;
13994 for(entry = *enclose; entry; entry = next) {
13995 next = entry->next;
13996 do_triple_unset(enclose, entry->member);
13997 }
13998}
13999
14000static int lookup_closure_index(struct compile_state *state,
14001 struct triple *me, struct triple *val)
14002{
14003 struct triple *first, *ins, *next;
14004 first = RHS(me, 0);
14005 ins = next = first;
14006 do {
14007 struct triple *result;
14008 struct triple *index0, *index1, *index2, *read, *write;
14009 ins = next;
14010 next = ins->next;
14011 if (ins->op != OP_CALL) {
14012 continue;
14013 }
14014 /* I am at a previous call point examine it closely */
14015 if (ins->next->op != OP_LABEL) {
14016 internal_error(state, ins, "call not followed by label");
14017 }
14018 /* Does this call does not enclose any variables? */
14019 if ((ins->next->next->op != OP_INDEX) ||
14020 (ins->next->next->u.cval != 0) ||
14021 (result = MISC(ins->next->next, 0)) ||
14022 (result->id & TRIPLE_FLAG_LOCAL)) {
14023 continue;
14024 }
14025 index0 = ins->next->next;
14026 /* The pattern is:
14027 * 0 index result < 0 >
14028 * 1 index 0 < ? >
14029 * 2 index 1 < ? >
14030 * 3 read 2
14031 * 4 write 3 var
14032 */
14033 for(index0 = ins->next->next;
14034 (index0->op == OP_INDEX) &&
14035 (MISC(index0, 0) == result) &&
14036 (index0->u.cval == 0) ;
14037 index0 = write->next)
14038 {
14039 index1 = index0->next;
14040 index2 = index1->next;
14041 read = index2->next;
14042 write = read->next;
14043 if ((index0->op != OP_INDEX) ||
14044 (index1->op != OP_INDEX) ||
14045 (index2->op != OP_INDEX) ||
14046 (read->op != OP_READ) ||
14047 (write->op != OP_WRITE) ||
14048 (MISC(index1, 0) != index0) ||
14049 (MISC(index2, 0) != index1) ||
14050 (RHS(read, 0) != index2) ||
14051 (RHS(write, 0) != read)) {
14052 internal_error(state, index0, "bad var read");
14053 }
14054 if (MISC(write, 0) == val) {
14055 return index2->u.cval;
14056 }
14057 }
14058 } while(next != first);
14059 return -1;
14060}
14061
14062static inline int enclose_triple(struct triple *ins)
14063{
14064 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14065}
14066
14067static void compute_closure_variables(struct compile_state *state,
14068 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14069{
14070 struct triple_reg_set *set, *vars, **last_var;
14071 struct basic_blocks bb;
14072 struct reg_block *rb;
14073 struct block *block;
14074 struct triple *old_result, *first, *ins;
14075 size_t count, idx;
14076 unsigned long used_indicies;
14077 int i, max_index;
14078#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14079#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
14080 struct {
14081 unsigned id;
14082 int index;
14083 } *info;
14084
14085
14086 /* Find the basic blocks of this function */
14087 bb.func = me;
14088 bb.first = RHS(me, 0);
14089 old_result = 0;
14090 if (!triple_is_ret(state, bb.first->prev)) {
14091 bb.func = 0;
14092 } else {
14093 old_result = fresult(state, me);
14094 }
14095 analyze_basic_blocks(state, &bb);
14096
14097 /* Find which variables are currently alive in a given block */
14098 rb = compute_variable_lifetimes(state, &bb);
14099
14100 /* Find the variables that are currently alive */
14101 block = block_of_triple(state, fcall);
14102 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14103 internal_error(state, fcall, "No reg block? block: %p", block);
14104 }
14105
14106#if DEBUG_EXPLICIT_CLOSURES
14107 print_live_variables(state, &bb, rb, state->dbgout);
14108 fflush(state->dbgout);
14109#endif
14110
14111 /* Count the number of triples in the function */
14112 first = RHS(me, 0);
14113 ins = first;
14114 count = 0;
14115 do {
14116 count++;
14117 ins = ins->next;
14118 } while(ins != first);
14119
14120 /* Allocate some memory to temorary hold the id info */
14121 info = xcmalloc(sizeof(*info) * (count +1), "info");
14122
14123 /* Mark the local function */
14124 first = RHS(me, 0);
14125 ins = first;
14126 idx = 1;
14127 do {
14128 info[idx].id = ins->id;
14129 ins->id = TRIPLE_FLAG_LOCAL | idx;
14130 idx++;
14131 ins = ins->next;
14132 } while(ins != first);
14133
14134 /*
14135 * Build the list of variables to enclose.
14136 *
14137 * A target it to put the same variable in the
14138 * same slot for ever call of a given function.
14139 * After coloring this removes all of the variable
14140 * manipulation code.
14141 *
14142 * The list of variables to enclose is built ordered
14143 * program order because except in corner cases this
14144 * gives me the stability of assignment I need.
14145 *
14146 * To gurantee that stability I lookup the variables
14147 * to see where they have been used before and
14148 * I build my final list with the assigned indicies.
14149 */
14150 vars = 0;
14151 if (enclose_triple(old_result)) {
14152 ordered_triple_set(&vars, old_result);
14153 }
14154 for(set = rb[block->vertex].out; set; set = set->next) {
14155 if (!enclose_triple(set->member)) {
14156 continue;
14157 }
14158 if ((set->member == fcall) || (set->member == old_result)) {
14159 continue;
14160 }
14161 if (!local_triple(state, me, set->member)) {
14162 internal_error(state, set->member, "not local?");
14163 }
14164 ordered_triple_set(&vars, set->member);
14165 }
14166
14167 /* Lookup the current indicies of the live varialbe */
14168 used_indicies = 0;
14169 max_index = -1;
14170 for(set = vars; set ; set = set->next) {
14171 struct triple *ins;
14172 int index;
14173 ins = set->member;
14174 index = lookup_closure_index(state, me, ins);
14175 info[ID_BITS(ins->id)].index = index;
14176 if (index < 0) {
14177 continue;
14178 }
14179 if (index >= MAX_INDICIES) {
14180 internal_error(state, ins, "index unexpectedly large");
14181 }
14182 if (used_indicies & (1 << index)) {
14183 internal_error(state, ins, "index previously used?");
14184 }
14185 /* Remember which indicies have been used */
14186 used_indicies |= (1 << index);
14187 if (index > max_index) {
14188 max_index = index;
14189 }
14190 }
14191
14192 /* Walk through the live variables and make certain
14193 * everything is assigned an index.
14194 */
14195 for(set = vars; set; set = set->next) {
14196 struct triple *ins;
14197 int index;
14198 ins = set->member;
14199 index = info[ID_BITS(ins->id)].index;
14200 if (index >= 0) {
14201 continue;
14202 }
14203 /* Find the lowest unused index value */
14204 for(index = 0; index < MAX_INDICIES; index++) {
14205 if (!(used_indicies & (1 << index))) {
14206 break;
14207 }
14208 }
14209 if (index == MAX_INDICIES) {
14210 internal_error(state, ins, "no free indicies?");
14211 }
14212 info[ID_BITS(ins->id)].index = index;
14213 /* Remember which indicies have been used */
14214 used_indicies |= (1 << index);
14215 if (index > max_index) {
14216 max_index = index;
14217 }
14218 }
14219
14220 /* Build the return list of variables with positions matching
14221 * their indicies.
14222 */
14223 *enclose = 0;
14224 last_var = enclose;
14225 for(i = 0; i <= max_index; i++) {
14226 struct triple *var;
14227 var = 0;
14228 if (used_indicies & (1 << i)) {
14229 for(set = vars; set; set = set->next) {
14230 int index;
14231 index = info[ID_BITS(set->member->id)].index;
14232 if (index == i) {
14233 var = set->member;
14234 break;
14235 }
14236 }
14237 if (!var) {
14238 internal_error(state, me, "missing variable");
14239 }
14240 }
14241 insert_triple_set(last_var, var);
14242 last_var = &(*last_var)->next;
14243 }
14244
14245#if DEBUG_EXPLICIT_CLOSURES
14246 /* Print out the variables to be enclosed */
14247 loc(state->dbgout, state, fcall);
14248 fprintf(state->dbgout, "Alive: \n");
14249 for(set = *enclose; set; set = set->next) {
14250 display_triple(state->dbgout, set->member);
14251 }
14252 fflush(state->dbgout);
14253#endif
14254
14255 /* Clear the marks */
14256 ins = first;
14257 do {
14258 ins->id = info[ID_BITS(ins->id)].id;
14259 ins = ins->next;
14260 } while(ins != first);
14261
14262 /* Release the ordered list of live variables */
14263 free_closure_variables(state, &vars);
14264
14265 /* Release the storage of the old ids */
14266 xfree(info);
14267
14268 /* Release the variable lifetime information */
14269 free_variable_lifetimes(state, &bb, rb);
14270
14271 /* Release the basic blocks of this function */
14272 free_basic_blocks(state, &bb);
14273}
14274
14275static void expand_function_call(
14276 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014277{
14278 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014279 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014280 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014281 struct triple *first;
14282 struct type *ptype, *rtype;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014283 struct triple *jmp;
14284 struct triple *ret_addr, *ret_loc, *ret_set;
Eric Biederman90089602004-05-28 14:11:54 +000014285 struct triple_reg_set *enclose, *set;
14286 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014287
Eric Biederman90089602004-05-28 14:11:54 +000014288#if DEBUG_EXPLICIT_CLOSURES
14289 FILE *fp = state->dbgout;
14290 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14291 display_func(state, fp, MISC(fcall, 0));
14292 display_func(state, fp, me);
14293 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14294#endif
14295
Eric Biederman5ade04a2003-10-22 04:03:46 +000014296 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014297 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014298 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014299 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014300 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014301 first = fcall->next;
14302
14303 /* Find what I need to enclose */
14304 compute_closure_variables(state, me, fcall, &enclose);
14305
14306 /* Compute the closure type */
14307 closure_type = new_type(TYPE_TUPLE, 0, 0);
14308 closure_type->elements = 0;
14309 closure_next = &closure_type->left;
14310 for(set = enclose; set ; set = set->next) {
14311 struct type *type;
14312 type = &void_type;
14313 if (set->member) {
14314 type = set->member->type;
14315 }
14316 if (!*closure_next) {
14317 *closure_next = type;
14318 } else {
14319 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
14320 type);
14321 closure_next = &(*closure_next)->right;
14322 }
14323 closure_type->elements += 1;
14324 }
14325 if (closure_type->elements == 0) {
14326 closure_type->type = TYPE_VOID;
14327 }
14328
14329
14330#if DEBUG_EXPLICIT_CLOSURES
14331 fprintf(state->dbgout, "closure type: ");
14332 name_of(state->dbgout, closure_type);
14333 fprintf(state->dbgout, "\n");
14334#endif
14335
14336 /* Update the called functions closure variable */
14337 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014338
14339 /* Generate some needed triples */
14340 ret_loc = label(state);
14341 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14342
14343 /* Pass the parameters to the new function */
14344 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014345 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014346 for(i = 0; i < pvals; i++) {
14347 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014348 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014349 atype = ptype;
14350 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14351 atype = ptype->left;
14352 }
Eric Biederman90089602004-05-28 14:11:54 +000014353 param = farg(state, func, i);
14354 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14355 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014356 }
Eric Biederman90089602004-05-28 14:11:54 +000014357 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014358 flatten(state, first, write_expr(state, param, arg));
14359 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014360 }
Eric Biederman90089602004-05-28 14:11:54 +000014361 rtype = func->type->left;
14362
Eric Biederman5ade04a2003-10-22 04:03:46 +000014363 /* Thread the triples together */
14364 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014365
14366 /* Save the active variables in the result variable */
14367 for(i = 0, set = enclose; set ; set = set->next, i++) {
14368 if (!set->member) {
14369 continue;
14370 }
14371 flatten(state, ret_loc,
14372 write_expr(state,
14373 closure_expr(state, func, closure_idx, i),
14374 read_expr(state, set->member)));
14375 }
14376
14377 /* Initialize the return value */
14378 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14379 flatten(state, ret_loc,
14380 write_expr(state,
14381 deref_index(state, fresult(state, func), 1),
14382 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14383 }
14384
Eric Biederman5ade04a2003-10-22 04:03:46 +000014385 ret_addr = flatten(state, ret_loc, ret_addr);
14386 ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14387 jmp = flatten(state, ret_loc,
14388 call(state, retvar, ret_addr, func_first, func_last));
14389
Eric Biederman90089602004-05-28 14:11:54 +000014390 /* Restore the active variables from the result variable */
14391 for(i = 0, set = enclose; set ; set = set->next, i++) {
14392 struct triple_set *use, *next;
14393 struct triple *new;
14394 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014395 continue;
14396 }
Eric Biederman90089602004-05-28 14:11:54 +000014397 /* Generate an expression for the value */
14398 new = flatten(state, first,
14399 read_expr(state,
14400 closure_expr(state, func, closure_idx, i)));
14401
14402
14403 /* If the original is an lvalue restore the preserved value */
14404 if (is_lvalue(state, set->member)) {
14405 flatten(state, first,
14406 write_expr(state, set->member, new));
14407 continue;
14408 }
14409 /* If the original is a value update the dominated uses */
14410
14411#if DEBUG_EXPLICIT_CLOSURES
14412 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14413 set->member, new);
14414#endif
14415 /* If fcall dominates the use update the expression */
14416 for(use = set->member->use; use; use = next) {
14417 /* Replace use modifies the use chain and
14418 * removes use, so I must take a copy of the
14419 * next entry early.
14420 */
14421 next = use->next;
14422 if (!tdominates(state, fcall, use->member)) {
14423 continue;
14424 }
14425 replace_use(state, set->member, new, use->member);
14426 }
14427 }
14428
14429 /* Find the result */
14430 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14431 struct triple * result;
14432 result = flatten(state, first,
14433 read_expr(state,
14434 deref_index(state, fresult(state, func), 1)));
14435
14436 propogate_use(state, fcall, result);
14437 }
14438
14439 /* Release the original fcall instruction */
14440 release_triple(state, fcall);
14441
14442 /* Release the closure variable list */
14443 free_closure_variables(state, &enclose);
14444
14445 if (state->compiler->debug & DEBUG_INLINE) {
14446 FILE *fp = state->dbgout;
14447 fprintf(fp, "\n");
14448 loc(fp, state, 0);
14449 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14450 display_func(state, fp, func);
14451 display_func(state, fp, me);
14452 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14453 }
14454
14455 return;
14456}
14457
14458static int do_inline(struct compile_state *state, struct triple *func)
14459{
14460 int do_inline;
14461 int policy;
14462
14463 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14464 switch(policy) {
14465 case COMPILER_INLINE_ALWAYS:
14466 do_inline = 1;
14467 if (func->type->type & ATTRIB_NOINLINE) {
14468 error(state, func, "noinline with always_inline compiler option");
14469 }
14470 break;
14471 case COMPILER_INLINE_NEVER:
14472 do_inline = 0;
14473 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14474 error(state, func, "always_inline with noinline compiler option");
14475 }
14476 break;
14477 case COMPILER_INLINE_DEFAULTON:
14478 switch(func->type->type & STOR_MASK) {
14479 case STOR_STATIC | STOR_INLINE:
14480 case STOR_LOCAL | STOR_INLINE:
14481 case STOR_EXTERN | STOR_INLINE:
14482 do_inline = 1;
14483 break;
14484 default:
14485 do_inline = 1;
14486 break;
14487 }
14488 break;
14489 case COMPILER_INLINE_DEFAULTOFF:
14490 switch(func->type->type & STOR_MASK) {
14491 case STOR_STATIC | STOR_INLINE:
14492 case STOR_LOCAL | STOR_INLINE:
14493 case STOR_EXTERN | STOR_INLINE:
14494 do_inline = 1;
14495 break;
14496 default:
14497 do_inline = 0;
14498 break;
14499 }
14500 break;
14501 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014502 switch(func->type->type & STOR_MASK) {
14503 case STOR_STATIC | STOR_INLINE:
14504 case STOR_LOCAL | STOR_INLINE:
14505 case STOR_EXTERN | STOR_INLINE:
14506 do_inline = 1;
14507 break;
14508 default:
14509 do_inline = (func->u.cval == 1);
14510 break;
14511 }
Eric Biederman90089602004-05-28 14:11:54 +000014512 break;
14513 default:
14514 do_inline = 0;
14515 internal_error(state, 0, "Unimplemented inline policy");
14516 break;
14517 }
14518 /* Force inlining */
14519 if (func->type->type & ATTRIB_NOINLINE) {
14520 do_inline = 0;
14521 }
14522 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14523 do_inline = 1;
14524 }
14525 return do_inline;
14526}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014527
Eric Biederman90089602004-05-28 14:11:54 +000014528static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14529{
14530 struct triple *first, *ptr, *next;
14531 /* If the function is not used don't bother */
14532 if (me->u.cval <= 0) {
14533 return;
14534 }
14535 if (state->compiler->debug & DEBUG_CALLS2) {
14536 FILE *fp = state->dbgout;
14537 fprintf(fp, "in: %s\n",
14538 me->type->type_ident->name);
14539 }
14540
14541 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014542 ptr = next = first;
14543 do {
Eric Biederman90089602004-05-28 14:11:54 +000014544 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014545 ptr = next;
14546 prev = ptr->prev;
14547 next = ptr->next;
14548 if (ptr->op != OP_FCALL) {
14549 continue;
14550 }
14551 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014552 /* See if the function should be inlined */
14553 if (!do_inline(state, func)) {
14554 /* Put a label after the fcall */
14555 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14556 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014557 }
Eric Biederman90089602004-05-28 14:11:54 +000014558 if (state->compiler->debug & DEBUG_CALLS) {
14559 FILE *fp = state->dbgout;
14560 if (state->compiler->debug & DEBUG_CALLS2) {
14561 loc(fp, state, ptr);
14562 }
14563 fprintf(fp, "inlining %s\n",
14564 func->type->type_ident->name);
14565 fflush(fp);
14566 }
14567
14568 /* Update the function use counts */
14569 func->u.cval -= 1;
14570
14571 /* Replace the fcall with the called function */
14572 expand_inline_call(state, me, ptr);
14573
14574 next = prev->next;
14575 } while (next != first);
14576
14577 ptr = next = first;
14578 do {
14579 struct triple *prev, *func;
14580 ptr = next;
14581 prev = ptr->prev;
14582 next = ptr->next;
14583 if (ptr->op != OP_FCALL) {
14584 continue;
14585 }
14586 func = MISC(ptr, 0);
14587 if (state->compiler->debug & DEBUG_CALLS) {
14588 FILE *fp = state->dbgout;
14589 if (state->compiler->debug & DEBUG_CALLS2) {
14590 loc(fp, state, ptr);
14591 }
14592 fprintf(fp, "calling %s\n",
14593 func->type->type_ident->name);
14594 fflush(fp);
14595 }
14596 /* Replace the fcall with the instruction sequence
14597 * needed to make the call.
14598 */
14599 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014600 next = prev->next;
14601 } while(next != first);
14602}
Eric Biederman90089602004-05-28 14:11:54 +000014603
14604static void inline_functions(struct compile_state *state, struct triple *func)
14605{
14606 inline_function(state, func, 0);
14607 reverse_walk_functions(state, inline_function, 0);
14608}
14609
Eric Biederman5ade04a2003-10-22 04:03:46 +000014610static void insert_function(struct compile_state *state,
14611 struct triple *func, void *arg)
14612{
14613 struct triple *first, *end, *ffirst, *fend;
14614
14615 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014616 FILE *fp = state->errout;
14617 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014618 func->type->type_ident->name, func->u.cval);
14619 }
14620 if (func->u.cval == 0) {
14621 return;
14622 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014623
14624 /* Find the end points of the lists */
14625 first = arg;
14626 end = first->prev;
14627 ffirst = RHS(func, 0);
14628 fend = ffirst->prev;
14629
14630 /* splice the lists together */
14631 end->next = ffirst;
14632 ffirst->prev = end;
14633 fend->next = first;
14634 first->prev = fend;
14635}
14636
Eric Biederman90089602004-05-28 14:11:54 +000014637struct triple *input_asm(struct compile_state *state)
14638{
14639 struct asm_info *info;
14640 struct triple *def;
14641 int i, out;
14642
14643 info = xcmalloc(sizeof(*info), "asm_info");
14644 info->str = "";
14645
14646 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14647 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14648
14649 def = new_triple(state, OP_ASM, &void_type, out, 0);
14650 def->u.ainfo = info;
14651 def->id |= TRIPLE_FLAG_VOLATILE;
14652
14653 for(i = 0; i < out; i++) {
14654 struct triple *piece;
14655 piece = triple(state, OP_PIECE, &int_type, def, 0);
14656 piece->u.cval = i;
14657 LHS(def, i) = piece;
14658 }
14659
14660 return def;
14661}
14662
14663struct triple *output_asm(struct compile_state *state)
14664{
14665 struct asm_info *info;
14666 struct triple *def;
14667 int in;
14668
14669 info = xcmalloc(sizeof(*info), "asm_info");
14670 info->str = "";
14671
14672 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14673 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14674
14675 def = new_triple(state, OP_ASM, &void_type, 0, in);
14676 def->u.ainfo = info;
14677 def->id |= TRIPLE_FLAG_VOLATILE;
14678
14679 return def;
14680}
14681
Eric Biederman5ade04a2003-10-22 04:03:46 +000014682static void join_functions(struct compile_state *state)
14683{
Eric Biederman90089602004-05-28 14:11:54 +000014684 struct triple *jmp, *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014685 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014686 struct type *pnext, *param;
14687 struct type *result_type, *args_type;
14688 int idx;
14689
14690 /* Be clear the functions have not been joined yet */
14691 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014692
14693 /* Dummy file state to get debug handing right */
14694 memset(&file, 0, sizeof(file));
14695 file.basename = "";
14696 file.line = 0;
14697 file.report_line = 0;
14698 file.report_name = file.basename;
14699 file.prev = state->file;
14700 state->file = &file;
14701 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014702
14703 /* The type of arguments */
14704 args_type = state->main_function->type->right;
14705 /* The return type without any specifiers */
14706 result_type = clone_type(0, state->main_function->type->left);
14707
14708
14709 /* Verify the external arguments */
14710 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14711 error(state, state->main_function,
14712 "Too many external input arguments");
14713 }
14714 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14715 error(state, state->main_function,
14716 "Too many external output arguments");
14717 }
14718
Eric Biederman5ade04a2003-10-22 04:03:46 +000014719 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014720 end = label(state);
14721 start = label(state);
14722 start = flatten(state, state->first, start);
14723 end = flatten(state, state->first, end);
14724 in = input_asm(state);
14725 out = output_asm(state);
14726 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014727 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014728 in = flatten(state, state->first, in);
14729 call = flatten(state, state->first, call);
14730 out = flatten(state, state->first, out);
14731
14732
14733 /* Read the external input arguments */
14734 pnext = args_type;
14735 idx = 0;
14736 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14737 struct triple *expr;
14738 param = pnext;
14739 pnext = 0;
14740 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14741 pnext = param->right;
14742 param = param->left;
14743 }
14744 if (registers_of(state, param) != 1) {
14745 error(state, state->main_function,
14746 "Arg: %d %s requires multiple registers",
14747 idx + 1, param->field_ident->name);
14748 }
14749 expr = read_expr(state, LHS(in, idx));
14750 RHS(call, idx) = expr;
14751 expr = flatten(state, call, expr);
14752 use_triple(expr, call);
14753
14754 idx++;
14755 }
14756
14757
14758 /* Write the external output arguments */
14759 pnext = result_type;
14760 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14761 pnext = result_type->left;
14762 }
14763 for(idx = 0; idx < out->rhs; idx++) {
14764 struct triple *expr;
14765 param = pnext;
14766 pnext = 0;
14767 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14768 pnext = param->right;
14769 param = param->left;
14770 }
14771 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14772 param = 0;
14773 }
14774 if (param) {
14775 if (registers_of(state, param) != 1) {
14776 error(state, state->main_function,
14777 "Result: %d %s requires multiple registers",
14778 idx, param->field_ident->name);
14779 }
14780 expr = read_expr(state, call);
14781 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14782 expr = deref_field(state, expr, param->field_ident);
14783 }
14784 } else {
14785 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14786 }
14787 flatten(state, out, expr);
14788 RHS(out, idx) = expr;
14789 use_triple(expr, out);
14790 }
14791
14792 /* Allocate a dummy containing function */
14793 func = triple(state, OP_LIST,
14794 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14795 func->type->type_ident = lookup(state, "", 0);
14796 RHS(func, 0) = state->first;
14797 func->u.cval = 1;
14798
Eric Biederman5ade04a2003-10-22 04:03:46 +000014799 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014800 mark_live_functions(state);
14801 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014802 walk_functions(state, insert_function, end);
14803
14804 if (start->next != end) {
14805 jmp = flatten(state, start, branch(state, end, 0));
14806 }
14807
Eric Biederman90089602004-05-28 14:11:54 +000014808 /* OK now the functions have been joined. */
14809 state->functions_joined = 1;
14810
Eric Biederman5ade04a2003-10-22 04:03:46 +000014811 /* Done now cleanup */
14812 state->file = file.prev;
14813 state->function = 0;
14814}
14815
Eric Biedermanb138ac82003-04-22 18:44:01 +000014816/*
14817 * Data structurs for optimation.
14818 */
14819
Eric Biederman5ade04a2003-10-22 04:03:46 +000014820
Eric Biederman83b991a2003-10-11 06:20:25 +000014821static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014822 struct block *used, struct block_set **head, struct block *user,
14823 int front)
14824{
14825 struct block_set **ptr, *new;
14826 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014827 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014828 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014829 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014830 ptr = head;
14831 while(*ptr) {
14832 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014833 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014834 }
14835 ptr = &(*ptr)->next;
14836 }
14837 new = xcmalloc(sizeof(*new), "block_set");
14838 new->member = user;
14839 if (front) {
14840 new->next = *head;
14841 *head = new;
14842 }
14843 else {
14844 new->next = 0;
14845 *ptr = new;
14846 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014847 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014848}
Eric Biederman83b991a2003-10-11 06:20:25 +000014849static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014850 struct block *used, struct block_set **head, struct block *unuser)
14851{
14852 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014853 int count;
14854 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014855 ptr = head;
14856 while(*ptr) {
14857 use = *ptr;
14858 if (use->member == unuser) {
14859 *ptr = use->next;
14860 memset(use, -1, sizeof(*use));
14861 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014862 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014863 }
14864 else {
14865 ptr = &use->next;
14866 }
14867 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014868 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014869}
14870
14871static void use_block(struct block *used, struct block *user)
14872{
Eric Biederman83b991a2003-10-11 06:20:25 +000014873 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014874 /* Append new to the head of the list, print_block
14875 * depends on this.
14876 */
Eric Biederman83b991a2003-10-11 06:20:25 +000014877 count = do_use_block(used, &used->use, user, 1);
14878 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014879}
14880static void unuse_block(struct block *used, struct block *unuser)
14881{
Eric Biederman83b991a2003-10-11 06:20:25 +000014882 int count;
14883 count = do_unuse_block(used, &used->use, unuser);
14884 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014885}
14886
Eric Biederman5ade04a2003-10-22 04:03:46 +000014887static void add_block_edge(struct block *block, struct block *edge, int front)
14888{
14889 int count;
14890 count = do_use_block(block, &block->edges, edge, front);
14891 block->edge_count += count;
14892}
14893
14894static void remove_block_edge(struct block *block, struct block *edge)
14895{
14896 int count;
14897 count = do_unuse_block(block, &block->edges, edge);
14898 block->edge_count -= count;
14899}
14900
Eric Biedermanb138ac82003-04-22 18:44:01 +000014901static void idom_block(struct block *idom, struct block *user)
14902{
14903 do_use_block(idom, &idom->idominates, user, 0);
14904}
14905
14906static void unidom_block(struct block *idom, struct block *unuser)
14907{
14908 do_unuse_block(idom, &idom->idominates, unuser);
14909}
14910
14911static void domf_block(struct block *block, struct block *domf)
14912{
14913 do_use_block(block, &block->domfrontier, domf, 0);
14914}
14915
14916static void undomf_block(struct block *block, struct block *undomf)
14917{
14918 do_unuse_block(block, &block->domfrontier, undomf);
14919}
14920
14921static void ipdom_block(struct block *ipdom, struct block *user)
14922{
14923 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14924}
14925
14926static void unipdom_block(struct block *ipdom, struct block *unuser)
14927{
14928 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14929}
14930
14931static void ipdomf_block(struct block *block, struct block *ipdomf)
14932{
14933 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14934}
14935
14936static void unipdomf_block(struct block *block, struct block *unipdomf)
14937{
14938 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14939}
14940
Eric Biederman83b991a2003-10-11 06:20:25 +000014941static int walk_triples(
14942 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000014943 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
14944 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014945{
Eric Biederman83b991a2003-10-11 06:20:25 +000014946 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014947 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000014948 ptr = state->first;
14949 do {
Eric Biederman90089602004-05-28 14:11:54 +000014950 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000014951 if (ptr->next->prev != ptr) {
14952 internal_error(state, ptr->next, "bad prev");
14953 }
14954 ptr = ptr->next;
14955 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000014956 return result;
14957}
14958
Eric Biedermanb138ac82003-04-22 18:44:01 +000014959#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000014960static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014961{
Eric Biederman90089602004-05-28 14:11:54 +000014962 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014963 int op;
14964 op = ins->op;
14965 if (op == OP_LIST) {
14966#if !PRINT_LIST
14967 return 0;
14968#endif
14969 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014970 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000014971 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000014972 }
Eric Biederman90089602004-05-28 14:11:54 +000014973 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000014974
Eric Biederman90089602004-05-28 14:11:54 +000014975 if (triple_is_branch(state, ins) && ins->use &&
14976 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000014977 internal_error(state, ins, "branch used?");
14978 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000014979 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000014980 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000014981 }
14982 return 0;
14983}
14984
Eric Biedermanb138ac82003-04-22 18:44:01 +000014985static void print_triples(struct compile_state *state)
14986{
Eric Biederman5ade04a2003-10-22 04:03:46 +000014987 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000014988 FILE *fp = state->dbgout;
14989 fprintf(fp, "--------------- triples ---------------\n");
14990 walk_triples(state, do_print_triple, fp);
14991 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014992 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000014993}
14994
14995struct cf_block {
14996 struct block *block;
14997};
14998static void find_cf_blocks(struct cf_block *cf, struct block *block)
14999{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015000 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015001 if (!block || (cf[block->vertex].block == block)) {
15002 return;
15003 }
15004 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015005 for(edge = block->edges; edge; edge = edge->next) {
15006 find_cf_blocks(cf, edge->member);
15007 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015008}
15009
Eric Biederman90089602004-05-28 14:11:54 +000015010static void print_control_flow(struct compile_state *state,
15011 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015012{
15013 struct cf_block *cf;
15014 int i;
15015 printf("\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015016 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15017 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015018
Eric Biederman90089602004-05-28 14:11:54 +000015019 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015020 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015021 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015022 block = cf[i].block;
15023 if (!block)
15024 continue;
15025 printf("(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015026 for(edge = block->edges; edge; edge = edge->next) {
15027 printf(" %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015028 }
15029 printf("\n");
15030 }
15031
15032 xfree(cf);
15033}
15034
Eric Biedermanb138ac82003-04-22 18:44:01 +000015035static void free_basic_block(struct compile_state *state, struct block *block)
15036{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015037 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015038 struct block *child;
15039 if (!block) {
15040 return;
15041 }
15042 if (block->vertex == -1) {
15043 return;
15044 }
15045 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015046 for(edge = block->edges; edge; edge = edge->next) {
15047 if (edge->member) {
15048 unuse_block(edge->member, block);
15049 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015050 }
15051 if (block->idom) {
15052 unidom_block(block->idom, block);
15053 }
15054 block->idom = 0;
15055 if (block->ipdom) {
15056 unipdom_block(block->ipdom, block);
15057 }
15058 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015059 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015060 child = entry->member;
15061 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015062 if (child && (child->vertex != -1)) {
15063 for(edge = child->edges; edge; edge = edge->next) {
15064 edge->member = 0;
15065 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015066 }
15067 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015068 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015069 child = entry->member;
15070 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015071 if (child && (child->vertex != -1)) {
15072 child->idom = 0;
15073 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015074 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015075 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015076 child = entry->member;
15077 undomf_block(block, child);
15078 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015079 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015080 child = entry->member;
15081 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015082 if (child && (child->vertex != -1)) {
15083 child->ipdom = 0;
15084 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015085 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015086 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015087 child = entry->member;
15088 unipdomf_block(block, child);
15089 }
15090 if (block->users != 0) {
15091 internal_error(state, 0, "block still has users");
15092 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015093 while((edge = block->edges)) {
15094 child = edge->member;
15095 remove_block_edge(block, child);
15096
15097 if (child && (child->vertex != -1)) {
15098 free_basic_block(state, child);
15099 }
15100 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015101 memset(block, -1, sizeof(*block));
15102 xfree(block);
15103}
15104
Eric Biederman90089602004-05-28 14:11:54 +000015105static void free_basic_blocks(struct compile_state *state,
15106 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015107{
15108 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015109 free_basic_block(state, bb->first_block);
15110 bb->last_vertex = 0;
15111 bb->first_block = bb->last_block = 0;
15112 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015113 ins = first;
15114 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015115 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015116 ins->u.block = 0;
15117 }
15118 ins = ins->next;
15119 } while(ins != first);
15120
15121}
15122
Eric Biederman90089602004-05-28 14:11:54 +000015123static struct block *basic_block(struct compile_state *state,
15124 struct basic_blocks *bb, struct triple *first)
15125{
15126 struct block *block;
15127 struct triple *ptr;
15128 if (!triple_is_label(state, first)) {
15129 internal_error(state, first, "block does not start with a label");
15130 }
15131 /* See if this basic block has already been setup */
15132 if (first->u.block != 0) {
15133 return first->u.block;
15134 }
15135 /* Allocate another basic block structure */
15136 bb->last_vertex += 1;
15137 block = xcmalloc(sizeof(*block), "block");
15138 block->first = block->last = first;
15139 block->vertex = bb->last_vertex;
15140 ptr = first;
15141 do {
15142 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
15143 break;
15144 }
15145 block->last = ptr;
15146 /* If ptr->u is not used remember where the baic block is */
15147 if (triple_stores_block(state, ptr)) {
15148 ptr->u.block = block;
15149 }
15150 if (triple_is_branch(state, ptr)) {
15151 break;
15152 }
15153 ptr = ptr->next;
15154 } while (ptr != bb->first);
15155 if ((ptr == bb->first) ||
15156 ((ptr->next == bb->first) && (
15157 triple_is_end(state, ptr) ||
15158 triple_is_ret(state, ptr))))
15159 {
15160 /* The block has no outflowing edges */
15161 }
15162 else if (triple_is_label(state, ptr)) {
15163 struct block *next;
15164 next = basic_block(state, bb, ptr);
15165 add_block_edge(block, next, 0);
15166 use_block(next, block);
15167 }
15168 else if (triple_is_branch(state, ptr)) {
15169 struct triple **expr, *first;
15170 struct block *child;
15171 /* Find the branch targets.
15172 * I special case the first branch as that magically
15173 * avoids some difficult cases for the register allocator.
15174 */
15175 expr = triple_edge_targ(state, ptr, 0);
15176 if (!expr) {
15177 internal_error(state, ptr, "branch without targets");
15178 }
15179 first = *expr;
15180 expr = triple_edge_targ(state, ptr, expr);
15181 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15182 if (!*expr) continue;
15183 child = basic_block(state, bb, *expr);
15184 use_block(child, block);
15185 add_block_edge(block, child, 0);
15186 }
15187 if (first) {
15188 child = basic_block(state, bb, first);
15189 use_block(child, block);
15190 add_block_edge(block, child, 1);
15191
15192 /* Be certain the return block of a call is
15193 * in a basic block. When it is not find
15194 * start of the block, insert a label if
15195 * necessary and build the basic block.
15196 * Then add a fake edge from the start block
15197 * to the return block of the function.
15198 */
15199 if (state->functions_joined && triple_is_call(state, ptr)
15200 && !block_of_triple(state, MISC(ptr, 0))) {
15201 struct block *tail;
15202 struct triple *start;
15203 start = triple_to_block_start(state, MISC(ptr, 0));
15204 if (!triple_is_label(state, start)) {
15205 start = pre_triple(state,
15206 start, OP_LABEL, &void_type, 0, 0);
15207 }
15208 tail = basic_block(state, bb, start);
15209 add_block_edge(child, tail, 0);
15210 use_block(tail, child);
15211 }
15212 }
15213 }
15214 else {
15215 internal_error(state, 0, "Bad basic block split");
15216 }
15217#if 0
15218{
15219 struct block_set *edge;
15220 FILE *fp = state->errout;
15221 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15222 block, block->vertex,
15223 block->first, block->last);
15224 for(edge = block->edges; edge; edge = edge->next) {
15225 fprintf(fp, " %10p [%2d]",
15226 edge->member ? edge->member->first : 0,
15227 edge->member ? edge->member->vertex : -1);
15228 }
15229 fprintf(fp, "\n");
15230}
15231#endif
15232 return block;
15233}
15234
15235
15236static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15237 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15238 void *arg)
15239{
15240 struct triple *ptr, *first;
15241 struct block *last_block;
15242 last_block = 0;
15243 first = bb->first;
15244 ptr = first;
15245 do {
15246 if (triple_stores_block(state, ptr)) {
15247 struct block *block;
15248 block = ptr->u.block;
15249 if (block && (block != last_block)) {
15250 cb(state, block, arg);
15251 }
15252 last_block = block;
15253 }
15254 ptr = ptr->next;
15255 } while(ptr != first);
15256}
15257
15258static void print_block(
15259 struct compile_state *state, struct block *block, void *arg)
15260{
15261 struct block_set *user, *edge;
15262 struct triple *ptr;
15263 FILE *fp = arg;
15264
15265 fprintf(fp, "\nblock: %p (%d) ",
15266 block,
15267 block->vertex);
15268
15269 for(edge = block->edges; edge; edge = edge->next) {
15270 fprintf(fp, " %p<-%p",
15271 edge->member,
15272 (edge->member && edge->member->use)?
15273 edge->member->use->member : 0);
15274 }
15275 fprintf(fp, "\n");
15276 if (block->first->op == OP_LABEL) {
15277 fprintf(fp, "%p:\n", block->first);
15278 }
15279 for(ptr = block->first; ; ) {
15280 display_triple(fp, ptr);
15281 if (ptr == block->last)
15282 break;
15283 ptr = ptr->next;
15284 if (ptr == block->first) {
15285 internal_error(state, 0, "missing block last?");
15286 }
15287 }
15288 fprintf(fp, "users %d: ", block->users);
15289 for(user = block->use; user; user = user->next) {
15290 fprintf(fp, "%p (%d) ",
15291 user->member,
15292 user->member->vertex);
15293 }
15294 fprintf(fp,"\n\n");
15295}
15296
15297
15298static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15299{
15300 fprintf(fp, "--------------- blocks ---------------\n");
15301 walk_blocks(state, &state->bb, print_block, fp);
15302}
15303static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15304{
15305 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15306 fprintf(fp, "After %s\n", func);
15307 romcc_print_blocks(state, fp);
15308 print_control_flow(state, &state->bb);
15309 }
15310}
15311
15312static void prune_nonblock_triples(struct compile_state *state,
15313 struct basic_blocks *bb)
15314{
15315 struct block *block;
15316 struct triple *first, *ins, *next;
15317 /* Delete the triples not in a basic block */
15318 block = 0;
15319 first = bb->first;
15320 ins = first;
15321 do {
15322 next = ins->next;
15323 if (ins->op == OP_LABEL) {
15324 block = ins->u.block;
15325 }
15326 if (!block) {
15327 struct triple_set *use;
15328 for(use = ins->use; use; use = use->next) {
15329 struct block *block;
15330 block = block_of_triple(state, use->member);
15331 if (block != 0) {
15332 internal_error(state, ins, "pruning used ins?");
15333 }
15334 }
15335 release_triple(state, ins);
15336 }
15337 if (block && block->last == ins) {
15338 block = 0;
15339 }
15340 ins = next;
15341 } while(ins != first);
15342}
15343
15344static void setup_basic_blocks(struct compile_state *state,
15345 struct basic_blocks *bb)
15346{
15347 if (!triple_stores_block(state, bb->first)) {
15348 internal_error(state, 0, "ins will not store block?");
15349 }
15350 /* Initialize the state */
15351 bb->first_block = bb->last_block = 0;
15352 bb->last_vertex = 0;
15353 free_basic_blocks(state, bb);
15354
15355 /* Find the basic blocks */
15356 bb->first_block = basic_block(state, bb, bb->first);
15357
15358 /* Be certain the last instruction of a function, or the
15359 * entire program is in a basic block. When it is not find
15360 * the start of the block, insert a label if necessary and build
15361 * basic block. Then add a fake edge from the start block
15362 * to the final block.
15363 */
15364 if (!block_of_triple(state, bb->first->prev)) {
15365 struct triple *start;
15366 struct block *tail;
15367 start = triple_to_block_start(state, bb->first->prev);
15368 if (!triple_is_label(state, start)) {
15369 start = pre_triple(state,
15370 start, OP_LABEL, &void_type, 0, 0);
15371 }
15372 tail = basic_block(state, bb, start);
15373 add_block_edge(bb->first_block, tail, 0);
15374 use_block(tail, bb->first_block);
15375 }
15376
15377 /* Find the last basic block.
15378 */
15379 bb->last_block = block_of_triple(state, bb->first->prev);
15380
15381 /* Delete the triples not in a basic block */
15382 prune_nonblock_triples(state, bb);
15383
15384#if 0
15385 /* If we are debugging print what I have just done */
15386 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15387 print_blocks(state, state->dbgout);
15388 print_control_flow(state, bb);
15389 }
15390#endif
15391}
15392
15393
Eric Biedermanb138ac82003-04-22 18:44:01 +000015394struct sdom_block {
15395 struct block *block;
15396 struct sdom_block *sdominates;
15397 struct sdom_block *sdom_next;
15398 struct sdom_block *sdom;
15399 struct sdom_block *label;
15400 struct sdom_block *parent;
15401 struct sdom_block *ancestor;
15402 int vertex;
15403};
15404
15405
15406static void unsdom_block(struct sdom_block *block)
15407{
15408 struct sdom_block **ptr;
15409 if (!block->sdom_next) {
15410 return;
15411 }
15412 ptr = &block->sdom->sdominates;
15413 while(*ptr) {
15414 if ((*ptr) == block) {
15415 *ptr = block->sdom_next;
15416 return;
15417 }
15418 ptr = &(*ptr)->sdom_next;
15419 }
15420}
15421
15422static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15423{
15424 unsdom_block(block);
15425 block->sdom = sdom;
15426 block->sdom_next = sdom->sdominates;
15427 sdom->sdominates = block;
15428}
15429
15430
15431
15432static int initialize_sdblock(struct sdom_block *sd,
15433 struct block *parent, struct block *block, int vertex)
15434{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015435 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015436 if (!block || (sd[block->vertex].block == block)) {
15437 return vertex;
15438 }
15439 vertex += 1;
15440 /* Renumber the blocks in a convinient fashion */
15441 block->vertex = vertex;
15442 sd[vertex].block = block;
15443 sd[vertex].sdom = &sd[vertex];
15444 sd[vertex].label = &sd[vertex];
15445 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15446 sd[vertex].ancestor = 0;
15447 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015448 for(edge = block->edges; edge; edge = edge->next) {
15449 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15450 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015451 return vertex;
15452}
15453
Eric Biederman83b991a2003-10-11 06:20:25 +000015454static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015455 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015456 struct block *parent, struct block *block, int vertex)
15457{
15458 struct block_set *user;
15459 if (!block || (sd[block->vertex].block == block)) {
15460 return vertex;
15461 }
15462 vertex += 1;
15463 /* Renumber the blocks in a convinient fashion */
15464 block->vertex = vertex;
15465 sd[vertex].block = block;
15466 sd[vertex].sdom = &sd[vertex];
15467 sd[vertex].label = &sd[vertex];
15468 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15469 sd[vertex].ancestor = 0;
15470 sd[vertex].vertex = vertex;
15471 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015472 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015473 }
15474 return vertex;
15475}
15476
Eric Biederman90089602004-05-28 14:11:54 +000015477static int setup_spdblocks(struct compile_state *state,
15478 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015479{
15480 struct block *block;
15481 int vertex;
15482 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015483 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015484
Eric Biederman5ade04a2003-10-22 04:03:46 +000015485 /* Walk through the graph and find unconnected blocks. Add a
15486 * fake edge from the unconnected blocks to the end of the
15487 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015488 */
Eric Biederman90089602004-05-28 14:11:54 +000015489 block = bb->first_block->last->next->u.block;
15490 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015491 if (sd[block->vertex].block == block) {
15492 continue;
15493 }
Eric Biederman530b5192003-07-01 10:05:30 +000015494#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015495 {
15496 FILE *fp = state->errout;
15497 fprintf(fp, "Adding %d\n", vertex +1);
15498 }
Eric Biederman530b5192003-07-01 10:05:30 +000015499#endif
Eric Biederman90089602004-05-28 14:11:54 +000015500 add_block_edge(block, bb->last_block, 0);
15501 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015502
Eric Biederman90089602004-05-28 14:11:54 +000015503 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015504 }
15505 return vertex;
15506}
15507
15508static void compress_ancestors(struct sdom_block *v)
15509{
15510 /* This procedure assumes ancestor(v) != 0 */
15511 /* if (ancestor(ancestor(v)) != 0) {
15512 * compress(ancestor(ancestor(v)));
15513 * if (semi(label(ancestor(v))) < semi(label(v))) {
15514 * label(v) = label(ancestor(v));
15515 * }
15516 * ancestor(v) = ancestor(ancestor(v));
15517 * }
15518 */
15519 if (!v->ancestor) {
15520 return;
15521 }
15522 if (v->ancestor->ancestor) {
15523 compress_ancestors(v->ancestor->ancestor);
15524 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15525 v->label = v->ancestor->label;
15526 }
15527 v->ancestor = v->ancestor->ancestor;
15528 }
15529}
15530
Eric Biederman90089602004-05-28 14:11:54 +000015531static void compute_sdom(struct compile_state *state,
15532 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015533{
15534 int i;
15535 /* // step 2
15536 * for each v <= pred(w) {
15537 * u = EVAL(v);
15538 * if (semi[u] < semi[w] {
15539 * semi[w] = semi[u];
15540 * }
15541 * }
15542 * add w to bucket(vertex(semi[w]));
15543 * LINK(parent(w), w);
15544 *
15545 * // step 3
15546 * for each v <= bucket(parent(w)) {
15547 * delete v from bucket(parent(w));
15548 * u = EVAL(v);
15549 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15550 * }
15551 */
Eric Biederman90089602004-05-28 14:11:54 +000015552 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015553 struct sdom_block *v, *parent, *next;
15554 struct block_set *user;
15555 struct block *block;
15556 block = sd[i].block;
15557 parent = sd[i].parent;
15558 /* Step 2 */
15559 for(user = block->use; user; user = user->next) {
15560 struct sdom_block *v, *u;
15561 v = &sd[user->member->vertex];
15562 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15563 if (u->sdom->vertex < sd[i].sdom->vertex) {
15564 sd[i].sdom = u->sdom;
15565 }
15566 }
15567 sdom_block(sd[i].sdom, &sd[i]);
15568 sd[i].ancestor = parent;
15569 /* Step 3 */
15570 for(v = parent->sdominates; v; v = next) {
15571 struct sdom_block *u;
15572 next = v->sdom_next;
15573 unsdom_block(v);
15574 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15575 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
15576 u->block : parent->block;
15577 }
15578 }
15579}
15580
Eric Biederman90089602004-05-28 14:11:54 +000015581static void compute_spdom(struct compile_state *state,
15582 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015583{
15584 int i;
15585 /* // step 2
15586 * for each v <= pred(w) {
15587 * u = EVAL(v);
15588 * if (semi[u] < semi[w] {
15589 * semi[w] = semi[u];
15590 * }
15591 * }
15592 * add w to bucket(vertex(semi[w]));
15593 * LINK(parent(w), w);
15594 *
15595 * // step 3
15596 * for each v <= bucket(parent(w)) {
15597 * delete v from bucket(parent(w));
15598 * u = EVAL(v);
15599 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15600 * }
15601 */
Eric Biederman90089602004-05-28 14:11:54 +000015602 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015603 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015604 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015605 struct block *block;
15606 block = sd[i].block;
15607 parent = sd[i].parent;
15608 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015609 for(edge = block->edges; edge; edge = edge->next) {
15610 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015611 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15612 if (u->sdom->vertex < sd[i].sdom->vertex) {
15613 sd[i].sdom = u->sdom;
15614 }
15615 }
15616 sdom_block(sd[i].sdom, &sd[i]);
15617 sd[i].ancestor = parent;
15618 /* Step 3 */
15619 for(v = parent->sdominates; v; v = next) {
15620 struct sdom_block *u;
15621 next = v->sdom_next;
15622 unsdom_block(v);
15623 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15624 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
15625 u->block : parent->block;
15626 }
15627 }
15628}
15629
Eric Biederman90089602004-05-28 14:11:54 +000015630static void compute_idom(struct compile_state *state,
15631 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015632{
15633 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015634 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015635 struct block *block;
15636 block = sd[i].block;
15637 if (block->idom->vertex != sd[i].sdom->vertex) {
15638 block->idom = block->idom->idom;
15639 }
15640 idom_block(block->idom, block);
15641 }
15642 sd[1].block->idom = 0;
15643}
15644
Eric Biederman90089602004-05-28 14:11:54 +000015645static void compute_ipdom(struct compile_state *state,
15646 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015647{
15648 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015649 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015650 struct block *block;
15651 block = sd[i].block;
15652 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15653 block->ipdom = block->ipdom->ipdom;
15654 }
15655 ipdom_block(block->ipdom, block);
15656 }
15657 sd[1].block->ipdom = 0;
15658}
15659
15660 /* Theorem 1:
15661 * Every vertex of a flowgraph G = (V, E, r) except r has
15662 * a unique immediate dominator.
15663 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15664 * rooted at r, called the dominator tree of G, such that
15665 * v dominates w if and only if v is a proper ancestor of w in
15666 * the dominator tree.
15667 */
15668 /* Lemma 1:
15669 * If v and w are vertices of G such that v <= w,
15670 * than any path from v to w must contain a common ancestor
15671 * of v and w in T.
15672 */
15673 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15674 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15675 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15676 /* Theorem 2:
15677 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15678 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15679 */
15680 /* Theorem 3:
15681 * Let w != r and let u be a vertex for which sdom(u) is
15682 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15683 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15684 */
15685 /* Lemma 5: Let vertices v,w satisfy v -> w.
15686 * Then v -> idom(w) or idom(w) -> idom(v)
15687 */
15688
Eric Biederman90089602004-05-28 14:11:54 +000015689static void find_immediate_dominators(struct compile_state *state,
15690 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015691{
15692 struct sdom_block *sd;
15693 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15694 * vi > w for (1 <= i <= k - 1}
15695 */
15696 /* Theorem 4:
15697 * For any vertex w != r.
15698 * sdom(w) = min(
15699 * {v|(v,w) <= E and v < w } U
15700 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15701 */
15702 /* Corollary 1:
15703 * Let w != r and let u be a vertex for which sdom(u) is
15704 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15705 * Then:
15706 * { sdom(w) if sdom(w) = sdom(u),
15707 * idom(w) = {
15708 * { idom(u) otherwise
15709 */
15710 /* The algorithm consists of the following 4 steps.
15711 * Step 1. Carry out a depth-first search of the problem graph.
15712 * Number the vertices from 1 to N as they are reached during
15713 * the search. Initialize the variables used in succeeding steps.
15714 * Step 2. Compute the semidominators of all vertices by applying
15715 * theorem 4. Carry out the computation vertex by vertex in
15716 * decreasing order by number.
15717 * Step 3. Implicitly define the immediate dominator of each vertex
15718 * by applying Corollary 1.
15719 * Step 4. Explicitly define the immediate dominator of each vertex,
15720 * carrying out the computation vertex by vertex in increasing order
15721 * by number.
15722 */
15723 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015724 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15725 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015726#if 0
15727 sd[1].size = 0;
15728 sd[1].label = 0;
15729 sd[1].sdom = 0;
15730#endif
15731 /* Step 2 compute the semidominators */
15732 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015733 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015734 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015735 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015736 xfree(sd);
15737}
15738
Eric Biederman90089602004-05-28 14:11:54 +000015739static void find_post_dominators(struct compile_state *state,
15740 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015741{
15742 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015743 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015744 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015745 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015746
Eric Biederman90089602004-05-28 14:11:54 +000015747 vertex = setup_spdblocks(state, bb, sd);
15748 if (vertex != bb->last_vertex) {
15749 internal_error(state, 0, "missing %d blocks",
15750 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015751 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015752
15753 /* Step 2 compute the semidominators */
15754 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015755 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015756 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015757 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015758 xfree(sd);
15759}
15760
15761
15762
15763static void find_block_domf(struct compile_state *state, struct block *block)
15764{
15765 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015766 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015767 if (block->domfrontier != 0) {
15768 internal_error(state, block->first, "domfrontier present?");
15769 }
15770 for(user = block->idominates; user; user = user->next) {
15771 child = user->member;
15772 if (child->idom != block) {
15773 internal_error(state, block->first, "bad idom");
15774 }
15775 find_block_domf(state, child);
15776 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015777 for(edge = block->edges; edge; edge = edge->next) {
15778 if (edge->member->idom != block) {
15779 domf_block(block, edge->member);
15780 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015781 }
15782 for(user = block->idominates; user; user = user->next) {
15783 struct block_set *frontier;
15784 child = user->member;
15785 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15786 if (frontier->member->idom != block) {
15787 domf_block(block, frontier->member);
15788 }
15789 }
15790 }
15791}
15792
15793static void find_block_ipdomf(struct compile_state *state, struct block *block)
15794{
15795 struct block *child;
15796 struct block_set *user;
15797 if (block->ipdomfrontier != 0) {
15798 internal_error(state, block->first, "ipdomfrontier present?");
15799 }
15800 for(user = block->ipdominates; user; user = user->next) {
15801 child = user->member;
15802 if (child->ipdom != block) {
15803 internal_error(state, block->first, "bad ipdom");
15804 }
15805 find_block_ipdomf(state, child);
15806 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015807 for(user = block->use; user; user = user->next) {
15808 if (user->member->ipdom != block) {
15809 ipdomf_block(block, user->member);
15810 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015812 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015813 struct block_set *frontier;
15814 child = user->member;
15815 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15816 if (frontier->member->ipdom != block) {
15817 ipdomf_block(block, frontier->member);
15818 }
15819 }
15820 }
15821}
15822
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015823static void print_dominated(
15824 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015825{
15826 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015827 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015828
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015829 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015830 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015831 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015832 if (user->member->idom != block) {
15833 internal_error(state, user->member->first, "bad idom");
15834 }
15835 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015836 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015837}
15838
Eric Biederman5ade04a2003-10-22 04:03:46 +000015839static void print_dominated2(
15840 struct compile_state *state, FILE *fp, int depth, struct block *block)
15841{
15842 struct block_set *user;
15843 struct triple *ins;
15844 struct occurance *ptr, *ptr2;
15845 const char *filename1, *filename2;
15846 int equal_filenames;
15847 int i;
15848 for(i = 0; i < depth; i++) {
15849 fprintf(fp, " ");
15850 }
15851 fprintf(fp, "%3d: %p (%p - %p) @",
15852 block->vertex, block, block->first, block->last);
15853 ins = block->first;
15854 while(ins != block->last && (ins->occurance->line == 0)) {
15855 ins = ins->next;
15856 }
15857 ptr = ins->occurance;
15858 ptr2 = block->last->occurance;
15859 filename1 = ptr->filename? ptr->filename : "";
15860 filename2 = ptr2->filename? ptr2->filename : "";
15861 equal_filenames = (strcmp(filename1, filename2) == 0);
15862 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15863 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15864 } else if (equal_filenames) {
15865 fprintf(fp, " %s:(%d - %d)",
15866 ptr->filename, ptr->line, ptr2->line);
15867 } else {
15868 fprintf(fp, " (%s:%d - %s:%d)",
15869 ptr->filename, ptr->line,
15870 ptr2->filename, ptr2->line);
15871 }
15872 fprintf(fp, "\n");
15873 for(user = block->idominates; user; user = user->next) {
15874 print_dominated2(state, fp, depth + 1, user->member);
15875 }
15876}
15877
Eric Biederman90089602004-05-28 14:11:54 +000015878static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015879{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015880 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015881 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015882 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015883 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015884}
15885
15886
15887static int print_frontiers(
15888 struct compile_state *state, struct block *block, int vertex)
15889{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015890 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015891
15892 if (!block || (block->vertex != vertex + 1)) {
15893 return vertex;
15894 }
15895 vertex += 1;
15896
15897 printf("%d:", block->vertex);
15898 for(user = block->domfrontier; user; user = user->next) {
15899 printf(" %d", user->member->vertex);
15900 }
15901 printf("\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015902
15903 for(edge = block->edges; edge; edge = edge->next) {
15904 vertex = print_frontiers(state, edge->member, vertex);
15905 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015906 return vertex;
15907}
Eric Biederman90089602004-05-28 14:11:54 +000015908static void print_dominance_frontiers(struct compile_state *state,
15909 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015910{
15911 printf("\ndominance frontiers\n");
Eric Biederman90089602004-05-28 14:11:54 +000015912 print_frontiers(state, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015913
15914}
15915
Eric Biederman90089602004-05-28 14:11:54 +000015916static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015917{
15918 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015919 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015920 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015921 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015922 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015923 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015924 print_dominators(state, state->dbgout, bb);
15925 print_dominance_frontiers(state, bb);
15926 print_control_flow(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015927 }
15928}
15929
15930
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015931static void print_ipdominated(
15932 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015933{
15934 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015935 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015936
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015937 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015938 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015939 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015940 if (user->member->ipdom != block) {
15941 internal_error(state, user->member->first, "bad ipdom");
15942 }
15943 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015944 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015945}
15946
Eric Biederman90089602004-05-28 14:11:54 +000015947static void print_ipdominators(struct compile_state *state, FILE *fp,
15948 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015949{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015950 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015951 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015952}
15953
15954static int print_pfrontiers(
15955 struct compile_state *state, struct block *block, int vertex)
15956{
15957 struct block_set *user;
15958
15959 if (!block || (block->vertex != vertex + 1)) {
15960 return vertex;
15961 }
15962 vertex += 1;
15963
15964 printf("%d:", block->vertex);
15965 for(user = block->ipdomfrontier; user; user = user->next) {
15966 printf(" %d", user->member->vertex);
15967 }
15968 printf("\n");
15969 for(user = block->use; user; user = user->next) {
15970 vertex = print_pfrontiers(state, user->member, vertex);
15971 }
15972 return vertex;
15973}
Eric Biederman90089602004-05-28 14:11:54 +000015974static void print_ipdominance_frontiers(struct compile_state *state,
15975 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015976{
15977 printf("\nipdominance frontiers\n");
Eric Biederman90089602004-05-28 14:11:54 +000015978 print_pfrontiers(state, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015979
15980}
15981
Eric Biederman90089602004-05-28 14:11:54 +000015982static void analyze_ipdominators(struct compile_state *state,
15983 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015984{
15985 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015986 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015987 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000015988 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015989 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015990 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015991 print_ipdominators(state, state->dbgout, bb);
15992 print_ipdominance_frontiers(state, bb);
15993 print_control_flow(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015994 }
15995}
15996
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015997static int bdominates(struct compile_state *state,
15998 struct block *dom, struct block *sub)
15999{
16000 while(sub && (sub != dom)) {
16001 sub = sub->idom;
16002 }
16003 return sub == dom;
16004}
16005
16006static int tdominates(struct compile_state *state,
16007 struct triple *dom, struct triple *sub)
16008{
16009 struct block *bdom, *bsub;
16010 int result;
16011 bdom = block_of_triple(state, dom);
16012 bsub = block_of_triple(state, sub);
16013 if (bdom != bsub) {
16014 result = bdominates(state, bdom, bsub);
16015 }
16016 else {
16017 struct triple *ins;
16018 ins = sub;
16019 while((ins != bsub->first) && (ins != dom)) {
16020 ins = ins->prev;
16021 }
16022 result = (ins == dom);
16023 }
16024 return result;
16025}
16026
Eric Biederman90089602004-05-28 14:11:54 +000016027static void analyze_basic_blocks(
16028 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016029{
Eric Biederman90089602004-05-28 14:11:54 +000016030 setup_basic_blocks(state, bb);
16031 analyze_idominators(state, bb);
16032 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016033}
16034
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035static void insert_phi_operations(struct compile_state *state)
16036{
16037 size_t size;
16038 struct triple *first;
16039 int *has_already, *work;
16040 struct block *work_list, **work_list_tail;
16041 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016042 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016043
Eric Biederman90089602004-05-28 14:11:54 +000016044 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016045 has_already = xcmalloc(size, "has_already");
16046 work = xcmalloc(size, "work");
16047 iter = 0;
16048
Eric Biederman83b991a2003-10-11 06:20:25 +000016049 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016050 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016051 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016052 struct triple_set *user, *unext;
16053 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016054
16055 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016056 continue;
16057 }
Eric Biederman90089602004-05-28 14:11:54 +000016058
Eric Biedermanb138ac82003-04-22 18:44:01 +000016059 iter += 1;
16060 work_list = 0;
16061 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016062 for(user = var->use; user; user = unext) {
16063 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016064 if (MISC(var, 0) == user->member) {
16065 continue;
16066 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016067 if (user->member->op == OP_READ) {
16068 continue;
16069 }
16070 if (user->member->op != OP_WRITE) {
16071 internal_error(state, user->member,
16072 "bad variable access");
16073 }
16074 block = user->member->u.block;
16075 if (!block) {
16076 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016077 release_triple(state, user->member);
16078 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016079 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016080 if (work[block->vertex] >= iter) {
16081 continue;
16082 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016083 work[block->vertex] = iter;
16084 *work_list_tail = block;
16085 block->work_next = 0;
16086 work_list_tail = &block->work_next;
16087 }
16088 for(block = work_list; block; block = block->work_next) {
16089 struct block_set *df;
16090 for(df = block->domfrontier; df; df = df->next) {
16091 struct triple *phi;
16092 struct block *front;
16093 int in_edges;
16094 front = df->member;
16095
16096 if (has_already[front->vertex] >= iter) {
16097 continue;
16098 }
16099 /* Count how many edges flow into this block */
16100 in_edges = front->users;
16101 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016102 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016103 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016104 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016105 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016106 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016107 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016108 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016109#if 1
16110 if (phi->rhs != in_edges) {
16111 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16112 phi->rhs, in_edges);
16113 }
16114#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016115 /* Insert the phi functions immediately after the label */
16116 insert_triple(state, front->first->next, phi);
16117 if (front->first == front->last) {
16118 front->last = front->first->next;
16119 }
16120 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016121 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016122
Eric Biedermanb138ac82003-04-22 18:44:01 +000016123 /* If necessary plan to visit the basic block */
16124 if (work[front->vertex] >= iter) {
16125 continue;
16126 }
16127 work[front->vertex] = iter;
16128 *work_list_tail = front;
16129 front->work_next = 0;
16130 work_list_tail = &front->work_next;
16131 }
16132 }
16133 }
16134 xfree(has_already);
16135 xfree(work);
16136}
16137
Eric Biederman66fe2222003-07-04 15:14:04 +000016138
Eric Biederman83b991a2003-10-11 06:20:25 +000016139struct stack {
16140 struct triple_set *top;
16141 unsigned orig_id;
16142};
16143
Eric Biederman90089602004-05-28 14:11:54 +000016144static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016145{
16146 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016147 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016148 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016149 ins = first;
16150 do {
Eric Biederman90089602004-05-28 14:11:54 +000016151 if (triple_is_auto_var(state, ins)) {
16152 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016153 }
16154 ins = ins->next;
16155 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016156 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016157}
16158
Eric Biederman90089602004-05-28 14:11:54 +000016159static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016160{
16161 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016162 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016163 first = state->first;
16164 ins = first;
16165 do {
Eric Biederman90089602004-05-28 14:11:54 +000016166 if (triple_is_auto_var(state, ins)) {
16167 auto_vars += 1;
16168 stacks[auto_vars].orig_id = ins->id;
16169 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016170 }
16171 ins = ins->next;
16172 } while(ins != first);
16173}
16174
Eric Biederman90089602004-05-28 14:11:54 +000016175static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016176{
16177 struct triple *first, *ins;
16178 first = state->first;
16179 ins = first;
16180 do {
Eric Biederman90089602004-05-28 14:11:54 +000016181 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016182 ins->id = stacks[ins->id].orig_id;
16183 }
16184 ins = ins->next;
16185 } while(ins != first);
16186}
16187
16188static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016189{
16190 struct triple_set *head;
16191 struct triple *top_val;
16192 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016193 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016194 if (head) {
16195 top_val = head->member;
16196 }
16197 return top_val;
16198}
16199
Eric Biederman83b991a2003-10-11 06:20:25 +000016200static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016201{
16202 struct triple_set *new;
16203 /* Append new to the head of the list,
16204 * it's the only sensible behavoir for a stack.
16205 */
16206 new = xcmalloc(sizeof(*new), "triple_set");
16207 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016208 new->next = stacks[var->id].top;
16209 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016210}
16211
Eric Biederman83b991a2003-10-11 06:20:25 +000016212static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016213{
16214 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016215 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016216 while(*ptr) {
16217 set = *ptr;
16218 if (set->member == oldval) {
16219 *ptr = set->next;
16220 xfree(set);
16221 /* Only free one occurance from the stack */
16222 return;
16223 }
16224 else {
16225 ptr = &set->next;
16226 }
16227 }
16228}
16229
Eric Biedermanb138ac82003-04-22 18:44:01 +000016230/*
16231 * C(V)
16232 * S(V)
16233 */
16234static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016235 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016236{
16237 struct block_set *set;
16238 struct triple *ptr;
16239 int edge;
16240 if (!parent || !block)
16241 return;
16242 /* Find the edge I am coming in on */
16243 edge = 0;
16244 for(set = block->use; set; set = set->next, edge++) {
16245 if (set->member == parent) {
16246 break;
16247 }
16248 }
16249 if (!set) {
16250 internal_error(state, 0, "phi input is not on a control predecessor");
16251 }
16252 for(ptr = block->first; ; ptr = ptr->next) {
16253 if (ptr->op == OP_PHI) {
16254 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016255 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016256 if (!var) {
16257 internal_error(state, ptr, "no var???");
16258 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016259 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016260 val = peek_triple(stacks, var);
16261 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016262 internal_error(state, val, "bad value in phi");
16263 }
Eric Biederman90089602004-05-28 14:11:54 +000016264 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016265 internal_error(state, ptr, "edges > phi rhs");
16266 }
16267 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016268 if ((*slot != 0) && (*slot != val)) {
16269 internal_error(state, ptr, "phi already bound on this edge");
16270 }
16271 *slot = val;
16272 use_triple(val, ptr);
16273 }
16274 if (ptr == block->last) {
16275 break;
16276 }
16277 }
16278}
16279
16280
16281static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016282 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016283{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016284 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016285 struct triple *ptr, *next, *last;
16286 int done;
16287 if (!block)
16288 return;
16289 last = block->first;
16290 done = 0;
16291 for(ptr = block->first; !done; ptr = next) {
16292 next = ptr->next;
16293 if (ptr == block->last) {
16294 done = 1;
16295 }
16296 /* RHS(A) */
16297 if (ptr->op == OP_READ) {
16298 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016299 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016300 if (!triple_is_auto_var(state, var)) {
16301 internal_error(state, ptr, "read of non auto var!");
16302 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016303 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016304 /* Find the current value of the variable */
16305 val = peek_triple(stacks, var);
16306 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016307 /* Let the optimizer at variables that are not initially
16308 * set. But give it a bogus value so things seem to
16309 * work by accident. This is useful for bitfields because
16310 * setting them always involves a read-modify-write.
16311 */
16312 if (TYPE_ARITHMETIC(ptr->type->type)) {
16313 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16314 val->u.cval = 0xdeadbeaf;
16315 } else {
16316 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16317 }
16318 }
16319 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016320 error(state, ptr, "variable used without being set");
16321 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016322 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16323 internal_error(state, val, "bad value in read");
16324 }
16325 propogate_use(state, ptr, val);
16326 release_triple(state, ptr);
16327 continue;
16328 }
16329 /* LHS(A) */
16330 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016331 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016332 var = MISC(ptr, 0);
16333 if (!triple_is_auto_var(state, var)) {
16334 internal_error(state, ptr, "write to non auto var!");
16335 }
16336 tval = val = RHS(ptr, 0);
16337 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16338 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016339 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016340 }
Eric Biederman90089602004-05-28 14:11:54 +000016341 /* Insert a cast if the types differ */
16342 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016343 if (val->op == OP_INTCONST) {
16344 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16345 tval->u.cval = val->u.cval;
16346 }
16347 else {
Eric Biederman90089602004-05-28 14:11:54 +000016348 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016349 use_triple(val, tval);
16350 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016351 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016352 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016353 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016354 use_triple(tval, ptr);
16355 }
16356 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016357 unuse_triple(var, ptr);
16358 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016359 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016360 }
16361 if (ptr->op == OP_PHI) {
16362 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016363 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016364 if (!triple_is_auto_var(state, var)) {
16365 internal_error(state, ptr, "phi references non auto var!");
16366 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016367 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016368 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016369 }
16370 last = ptr;
16371 }
16372 block->last = last;
16373
16374 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016375 for(edge = block->edges; edge; edge = edge->next) {
16376 fixup_block_phi_variables(state, stacks, block, edge->member);
16377 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016378 /* rename variables in the dominated nodes */
16379 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016380 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016381 }
16382 /* pop the renamed variable stack */
16383 last = block->first;
16384 done = 0;
16385 for(ptr = block->first; !done ; ptr = next) {
16386 next = ptr->next;
16387 if (ptr == block->last) {
16388 done = 1;
16389 }
16390 if (ptr->op == OP_WRITE) {
16391 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016392 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016393 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016394 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016395 release_triple(state, ptr);
16396 continue;
16397 }
16398 if (ptr->op == OP_PHI) {
16399 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016400 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016401 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016402 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016403 }
16404 last = ptr;
16405 }
16406 block->last = last;
16407}
16408
Eric Biederman83b991a2003-10-11 06:20:25 +000016409static void rename_variables(struct compile_state *state)
16410{
16411 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016412 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016413
16414 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016415 auto_vars = count_auto_vars(state);
16416 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016417
Eric Biederman90089602004-05-28 14:11:54 +000016418 /* Give each auto_var a stack */
16419 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016420
16421 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016422 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016423
Eric Biederman90089602004-05-28 14:11:54 +000016424 /* Remove the stacks from the auto_vars */
16425 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016426 xfree(stacks);
16427}
16428
Eric Biedermanb138ac82003-04-22 18:44:01 +000016429static void prune_block_variables(struct compile_state *state,
16430 struct block *block)
16431{
16432 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016433 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016434 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016435
Eric Biedermanb138ac82003-04-22 18:44:01 +000016436 done = 0;
16437 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016438 /* Be extremely careful I am deleting the list
16439 * as I walk trhough it.
16440 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016441 next = ptr->next;
16442 if (ptr == block->last) {
16443 done = 1;
16444 }
Eric Biederman90089602004-05-28 14:11:54 +000016445 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016446 struct triple_set *user, *next;
16447 for(user = ptr->use; user; user = next) {
16448 struct triple *use;
16449 next = user->next;
16450 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016451 if (MISC(ptr, 0) == user->member) {
16452 continue;
16453 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016454 if (use->op != OP_PHI) {
16455 internal_error(state, use, "decl still used");
16456 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016457 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016458 internal_error(state, use, "bad phi use of decl");
16459 }
16460 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016461 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016462 }
Eric Biederman90089602004-05-28 14:11:54 +000016463 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16464 /* Delete the adecl */
16465 release_triple(state, MISC(ptr, 0));
16466 /* And the piece */
16467 release_triple(state, ptr);
16468 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016469 continue;
16470 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016471 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016472 for(user = block->idominates; user; user = user->next) {
16473 prune_block_variables(state, user->member);
16474 }
16475}
16476
Eric Biederman66fe2222003-07-04 15:14:04 +000016477struct phi_triple {
16478 struct triple *phi;
16479 unsigned orig_id;
16480 int alive;
16481};
16482
16483static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16484{
16485 struct triple **slot;
16486 int zrhs, i;
16487 if (live[phi->id].alive) {
16488 return;
16489 }
16490 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016491 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016492 slot = &RHS(phi, 0);
16493 for(i = 0; i < zrhs; i++) {
16494 struct triple *used;
16495 used = slot[i];
16496 if (used && (used->op == OP_PHI)) {
16497 keep_phi(state, live, used);
16498 }
16499 }
16500}
16501
16502static void prune_unused_phis(struct compile_state *state)
16503{
16504 struct triple *first, *phi;
16505 struct phi_triple *live;
16506 int phis, i;
16507
Eric Biederman66fe2222003-07-04 15:14:04 +000016508 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016509 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016510
16511 /* Count how many phi functions I need to process */
16512 phis = 0;
16513 for(phi = first->next; phi != first; phi = phi->next) {
16514 if (phi->op == OP_PHI) {
16515 phis += 1;
16516 }
16517 }
16518
16519 /* Mark them all dead */
16520 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16521 phis = 0;
16522 for(phi = first->next; phi != first; phi = phi->next) {
16523 if (phi->op != OP_PHI) {
16524 continue;
16525 }
16526 live[phis].alive = 0;
16527 live[phis].orig_id = phi->id;
16528 live[phis].phi = phi;
16529 phi->id = phis;
16530 phis += 1;
16531 }
16532
16533 /* Mark phis alive that are used by non phis */
16534 for(i = 0; i < phis; i++) {
16535 struct triple_set *set;
16536 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16537 if (set->member->op != OP_PHI) {
16538 keep_phi(state, live, live[i].phi);
16539 break;
16540 }
16541 }
16542 }
16543
16544 /* Delete the extraneous phis */
16545 for(i = 0; i < phis; i++) {
16546 struct triple **slot;
16547 int zrhs, j;
16548 if (!live[i].alive) {
16549 release_triple(state, live[i].phi);
16550 continue;
16551 }
16552 phi = live[i].phi;
16553 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016554 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016555 for(j = 0; j < zrhs; j++) {
16556 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016557 struct triple *unknown;
16558 get_occurance(phi->occurance);
16559 unknown = flatten(state, state->global_pool,
16560 alloc_triple(state, OP_UNKNOWNVAL,
16561 phi->type, 0, 0, phi->occurance));
16562 slot[j] = unknown;
16563 use_triple(unknown, phi);
16564 transform_to_arch_instruction(state, unknown);
16565#if 0
16566 warning(state, phi, "variable not set at index %d on all paths to use", j);
16567#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016568 }
16569 }
16570 }
16571 xfree(live);
16572}
16573
Eric Biedermanb138ac82003-04-22 18:44:01 +000016574static void transform_to_ssa_form(struct compile_state *state)
16575{
16576 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016577 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016578
Eric Biederman90089602004-05-28 14:11:54 +000016579 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016580 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016581
Eric Biederman90089602004-05-28 14:11:54 +000016582 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016583}
16584
16585
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016586static void clear_vertex(
16587 struct compile_state *state, struct block *block, void *arg)
16588{
Eric Biederman83b991a2003-10-11 06:20:25 +000016589 /* Clear the current blocks vertex and the vertex of all
16590 * of the current blocks neighbors in case there are malformed
16591 * blocks with now instructions at this point.
16592 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016593 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016594 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016595 for(edge = block->edges; edge; edge = edge->next) {
16596 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016597 }
16598 for(user = block->use; user; user = user->next) {
16599 user->member->vertex = 0;
16600 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016601}
16602
16603static void mark_live_block(
16604 struct compile_state *state, struct block *block, int *next_vertex)
16605{
16606 /* See if this is a block that has not been marked */
16607 if (block->vertex != 0) {
16608 return;
16609 }
16610 block->vertex = *next_vertex;
16611 *next_vertex += 1;
16612 if (triple_is_branch(state, block->last)) {
16613 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016614 targ = triple_edge_targ(state, block->last, 0);
16615 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016616 if (!*targ) {
16617 continue;
16618 }
16619 if (!triple_stores_block(state, *targ)) {
16620 internal_error(state, 0, "bad targ");
16621 }
16622 mark_live_block(state, (*targ)->u.block, next_vertex);
16623 }
Eric Biederman90089602004-05-28 14:11:54 +000016624 /* Ensure the last block of a function remains alive */
16625 if (triple_is_call(state, block->last)) {
16626 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16627 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016628 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016629 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016630 struct triple *ins;
16631 ins = block->last->next;
16632 if (!triple_stores_block(state, ins)) {
16633 internal_error(state, 0, "bad block start");
16634 }
16635 mark_live_block(state, ins->u.block, next_vertex);
16636 }
16637}
16638
Eric Biedermanb138ac82003-04-22 18:44:01 +000016639static void transform_from_ssa_form(struct compile_state *state)
16640{
16641 /* To get out of ssa form we insert moves on the incoming
16642 * edges to blocks containting phi functions.
16643 */
16644 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016645 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016646 int next_vertex;
16647
16648 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016649 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016650 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016651 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016652
16653 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016654 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016655 for(phi = first->next; phi != first ; phi = next) {
16656 struct block_set *set;
16657 struct block *block;
16658 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016659 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016660 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016661 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016662 next = phi->next;
16663 if (phi->op != OP_PHI) {
16664 continue;
16665 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016666
Eric Biedermanb138ac82003-04-22 18:44:01 +000016667 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016668 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016669
Eric Biederman83b991a2003-10-11 06:20:25 +000016670 /* If this phi is in a dead block just forget it */
16671 if (block->vertex == 0) {
16672 release_triple(state, phi);
16673 continue;
16674 }
16675
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016676 /* Forget uses from code in dead blocks */
16677 for(use = phi->use; use; use = use_next) {
16678 struct block *ublock;
16679 struct triple **expr;
16680 use_next = use->next;
16681 ublock = block_of_triple(state, use->member);
16682 if ((use->member == phi) || (ublock->vertex != 0)) {
16683 continue;
16684 }
16685 expr = triple_rhs(state, use->member, 0);
16686 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16687 if (*expr == phi) {
16688 *expr = 0;
16689 }
16690 }
16691 unuse_triple(phi, use->member);
16692 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016693 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016694 if (registers_of(state, phi->type) != 1) {
16695 internal_error(state, phi, "phi->type does not fit in a single register!");
16696 }
16697 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16698 var = var->next; /* point at the var */
16699
Eric Biederman83b991a2003-10-11 06:20:25 +000016700 /* Replaces use of phi with var */
16701 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016702
Eric Biederman90089602004-05-28 14:11:54 +000016703 /* Count the readers */
16704 readers = 0;
16705 for(use = var->use; use; use = use->next) {
16706 if (use->member != MISC(var, 0)) {
16707 readers++;
16708 }
16709 }
16710
Eric Biedermanb138ac82003-04-22 18:44:01 +000016711 /* Walk all of the incoming edges/blocks and insert moves.
16712 */
Eric Biederman90089602004-05-28 14:11:54 +000016713 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016714 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016715 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016716 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016717 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016718 eblock = set->member;
16719 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016720 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016721 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016722 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016723
Eric Biederman83b991a2003-10-11 06:20:25 +000016724 /* If we don't have a value that belongs in an OP_WRITE
16725 * continue on.
16726 */
Eric Biederman90089602004-05-28 14:11:54 +000016727 if (!val || (val == &unknown_triple) || (val == phi)
16728 || (vblock && (vblock->vertex == 0))) {
16729 continue;
16730 }
16731 /* If the value should never occur error */
16732 if (!vblock) {
16733 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016734 continue;
16735 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016736
16737 /* If the value occurs in a dead block see if a replacement
16738 * block can be found.
16739 */
16740 while(eblock && (eblock->vertex == 0)) {
16741 eblock = eblock->idom;
16742 }
16743 /* If not continue on with the next value. */
16744 if (!eblock || (eblock->vertex == 0)) {
16745 continue;
16746 }
16747
16748 /* If we have an empty incoming block ignore it. */
16749 if (!eblock->first) {
16750 internal_error(state, 0, "empty block?");
16751 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016752
Eric Biederman530b5192003-07-01 10:05:30 +000016753 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016754 /* Walk through the edge block backwards to find an
16755 * appropriate location for the OP_WRITE.
16756 */
16757 for(base = eblock->last; base != eblock->first; base = base->prev) {
16758 struct triple **expr;
16759 if (base->op == OP_PIECE) {
16760 base = MISC(base, 0);
16761 }
16762 if ((base == var) || (base == val)) {
16763 goto out;
16764 }
16765 expr = triple_lhs(state, base, 0);
16766 for(; expr; expr = triple_lhs(state, base, expr)) {
16767 if ((*expr) == val) {
16768 goto out;
16769 }
16770 }
16771 expr = triple_rhs(state, base, 0);
16772 for(; expr; expr = triple_rhs(state, base, expr)) {
16773 if ((*expr) == var) {
16774 goto out;
16775 }
16776 }
Eric Biederman530b5192003-07-01 10:05:30 +000016777 }
Eric Biederman90089602004-05-28 14:11:54 +000016778 out:
16779 if (triple_is_branch(state, base)) {
16780 internal_error(state, base,
16781 "Could not insert write to phi");
16782 }
16783 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016784 use_triple(val, move);
16785 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016786 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016787 }
Eric Biederman90089602004-05-28 14:11:54 +000016788 if (!writers && readers) {
16789 internal_error(state, var, "no value written to in use phi?");
16790 }
16791 /* If var is not used free it */
16792 if (!writers) {
16793 release_triple(state, MISC(var, 0));
16794 release_triple(state, var);
16795 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016796 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016797 release_triple(state, phi);
16798 }
16799
Eric Biederman83b991a2003-10-11 06:20:25 +000016800 /* Walk all of the operations to find the adecls */
16801 for(var = first->next; var != first ; var = var->next) {
16802 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016803 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016804 continue;
16805 }
16806
16807 /* Walk through all of the rhs uses of var and
16808 * replace them with read of var.
16809 */
16810 for(use = var->use; use; use = use_next) {
16811 struct triple *read, *user;
16812 struct triple **slot;
16813 int zrhs, i, used;
16814 use_next = use->next;
16815 user = use->member;
16816
16817 /* Generate a read of var */
16818 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16819 use_triple(var, read);
16820
16821 /* Find the rhs uses and see if they need to be replaced */
16822 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016823 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016824 slot = &RHS(user, 0);
16825 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016826 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016827 slot[i] = read;
16828 used = 1;
16829 }
16830 }
16831 /* If we did use it cleanup the uses */
16832 if (used) {
16833 unuse_triple(var, user);
16834 use_triple(read, user);
16835 }
16836 /* If we didn't use it release the extra triple */
16837 else {
16838 release_triple(state, read);
16839 }
16840 }
16841 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016842}
16843
Eric Biederman5ade04a2003-10-22 04:03:46 +000016844#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016845 FILE *fp = state->dbgout; \
16846 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Eric Biederman5ade04a2003-10-22 04:03:46 +000016847 }
16848
Eric Biederman83b991a2003-10-11 06:20:25 +000016849static void rebuild_ssa_form(struct compile_state *state)
16850{
16851HI();
16852 transform_from_ssa_form(state);
16853HI();
Eric Biederman90089602004-05-28 14:11:54 +000016854 state->bb.first = state->first;
16855 free_basic_blocks(state, &state->bb);
16856 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016857HI();
16858 insert_phi_operations(state);
16859HI();
16860 rename_variables(state);
16861HI();
16862
Eric Biederman90089602004-05-28 14:11:54 +000016863 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016864HI();
16865 prune_unused_phis(state);
16866HI();
16867}
16868#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016869
16870/*
16871 * Register conflict resolution
16872 * =========================================================
16873 */
16874
16875static struct reg_info find_def_color(
16876 struct compile_state *state, struct triple *def)
16877{
16878 struct triple_set *set;
16879 struct reg_info info;
16880 info.reg = REG_UNSET;
16881 info.regcm = 0;
16882 if (!triple_is_def(state, def)) {
16883 return info;
16884 }
16885 info = arch_reg_lhs(state, def, 0);
16886 if (info.reg >= MAX_REGISTERS) {
16887 info.reg = REG_UNSET;
16888 }
16889 for(set = def->use; set; set = set->next) {
16890 struct reg_info tinfo;
16891 int i;
16892 i = find_rhs_use(state, set->member, def);
16893 if (i < 0) {
16894 continue;
16895 }
16896 tinfo = arch_reg_rhs(state, set->member, i);
16897 if (tinfo.reg >= MAX_REGISTERS) {
16898 tinfo.reg = REG_UNSET;
16899 }
16900 if ((tinfo.reg != REG_UNSET) &&
16901 (info.reg != REG_UNSET) &&
16902 (tinfo.reg != info.reg)) {
16903 internal_error(state, def, "register conflict");
16904 }
16905 if ((info.regcm & tinfo.regcm) == 0) {
16906 internal_error(state, def, "regcm conflict %x & %x == 0",
16907 info.regcm, tinfo.regcm);
16908 }
16909 if (info.reg == REG_UNSET) {
16910 info.reg = tinfo.reg;
16911 }
16912 info.regcm &= tinfo.regcm;
16913 }
16914 if (info.reg >= MAX_REGISTERS) {
16915 internal_error(state, def, "register out of range");
16916 }
16917 return info;
16918}
16919
16920static struct reg_info find_lhs_pre_color(
16921 struct compile_state *state, struct triple *ins, int index)
16922{
16923 struct reg_info info;
16924 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016925 zrhs = ins->rhs;
16926 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016927 if (!zlhs && triple_is_def(state, ins)) {
16928 zlhs = 1;
16929 }
16930 if (index >= zlhs) {
16931 internal_error(state, ins, "Bad lhs %d", index);
16932 }
16933 info = arch_reg_lhs(state, ins, index);
16934 for(i = 0; i < zrhs; i++) {
16935 struct reg_info rinfo;
16936 rinfo = arch_reg_rhs(state, ins, i);
16937 if ((info.reg == rinfo.reg) &&
16938 (rinfo.reg >= MAX_REGISTERS)) {
16939 struct reg_info tinfo;
16940 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
16941 info.reg = tinfo.reg;
16942 info.regcm &= tinfo.regcm;
16943 break;
16944 }
16945 }
16946 if (info.reg >= MAX_REGISTERS) {
16947 info.reg = REG_UNSET;
16948 }
16949 return info;
16950}
16951
16952static struct reg_info find_rhs_post_color(
16953 struct compile_state *state, struct triple *ins, int index);
16954
16955static struct reg_info find_lhs_post_color(
16956 struct compile_state *state, struct triple *ins, int index)
16957{
16958 struct triple_set *set;
16959 struct reg_info info;
16960 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000016961#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000016962 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016963 ins, index);
16964#endif
16965 if ((index == 0) && triple_is_def(state, ins)) {
16966 lhs = ins;
16967 }
Eric Biederman90089602004-05-28 14:11:54 +000016968 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016969 lhs = LHS(ins, index);
16970 }
16971 else {
16972 internal_error(state, ins, "Bad lhs %d", index);
16973 lhs = 0;
16974 }
16975 info = arch_reg_lhs(state, ins, index);
16976 if (info.reg >= MAX_REGISTERS) {
16977 info.reg = REG_UNSET;
16978 }
16979 for(set = lhs->use; set; set = set->next) {
16980 struct reg_info rinfo;
16981 struct triple *user;
16982 int zrhs, i;
16983 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000016984 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016985 for(i = 0; i < zrhs; i++) {
16986 if (RHS(user, i) != lhs) {
16987 continue;
16988 }
16989 rinfo = find_rhs_post_color(state, user, i);
16990 if ((info.reg != REG_UNSET) &&
16991 (rinfo.reg != REG_UNSET) &&
16992 (info.reg != rinfo.reg)) {
16993 internal_error(state, ins, "register conflict");
16994 }
16995 if ((info.regcm & rinfo.regcm) == 0) {
16996 internal_error(state, ins, "regcm conflict %x & %x == 0",
16997 info.regcm, rinfo.regcm);
16998 }
16999 if (info.reg == REG_UNSET) {
17000 info.reg = rinfo.reg;
17001 }
17002 info.regcm &= rinfo.regcm;
17003 }
17004 }
Eric Biederman530b5192003-07-01 10:05:30 +000017005#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017006 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017007 ins, index, info.reg, info.regcm);
17008#endif
17009 return info;
17010}
17011
17012static struct reg_info find_rhs_post_color(
17013 struct compile_state *state, struct triple *ins, int index)
17014{
17015 struct reg_info info, rinfo;
17016 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017017#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017018 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017019 ins, index);
17020#endif
17021 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017022 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017023 if (!zlhs && triple_is_def(state, ins)) {
17024 zlhs = 1;
17025 }
17026 info = rinfo;
17027 if (info.reg >= MAX_REGISTERS) {
17028 info.reg = REG_UNSET;
17029 }
17030 for(i = 0; i < zlhs; i++) {
17031 struct reg_info linfo;
17032 linfo = arch_reg_lhs(state, ins, i);
17033 if ((linfo.reg == rinfo.reg) &&
17034 (linfo.reg >= MAX_REGISTERS)) {
17035 struct reg_info tinfo;
17036 tinfo = find_lhs_post_color(state, ins, i);
17037 if (tinfo.reg >= MAX_REGISTERS) {
17038 tinfo.reg = REG_UNSET;
17039 }
Eric Biederman530b5192003-07-01 10:05:30 +000017040 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017041 info.regcm &= tinfo.regcm;
17042 if (info.reg != REG_UNSET) {
17043 internal_error(state, ins, "register conflict");
17044 }
17045 if (info.regcm == 0) {
17046 internal_error(state, ins, "regcm conflict");
17047 }
17048 info.reg = tinfo.reg;
17049 }
17050 }
Eric Biederman530b5192003-07-01 10:05:30 +000017051#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017052 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017053 ins, index, info.reg, info.regcm);
17054#endif
17055 return info;
17056}
17057
17058static struct reg_info find_lhs_color(
17059 struct compile_state *state, struct triple *ins, int index)
17060{
17061 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017062#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017063 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017064 ins, index);
17065#endif
17066 pre = find_lhs_pre_color(state, ins, index);
17067 post = find_lhs_post_color(state, ins, index);
17068 if ((pre.reg != post.reg) &&
17069 (pre.reg != REG_UNSET) &&
17070 (post.reg != REG_UNSET)) {
17071 internal_error(state, ins, "register conflict");
17072 }
17073 info.regcm = pre.regcm & post.regcm;
17074 info.reg = pre.reg;
17075 if (info.reg == REG_UNSET) {
17076 info.reg = post.reg;
17077 }
Eric Biederman530b5192003-07-01 10:05:30 +000017078#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017079 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017080 ins, index, info.reg, info.regcm,
17081 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017082#endif
17083 return info;
17084}
17085
17086static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17087{
17088 struct triple_set *entry, *next;
17089 struct triple *out;
17090 struct reg_info info, rinfo;
17091
17092 info = arch_reg_lhs(state, ins, 0);
17093 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17094 use_triple(RHS(out, 0), out);
17095 /* Get the users of ins to use out instead */
17096 for(entry = ins->use; entry; entry = next) {
17097 int i;
17098 next = entry->next;
17099 if (entry->member == out) {
17100 continue;
17101 }
17102 i = find_rhs_use(state, entry->member, ins);
17103 if (i < 0) {
17104 continue;
17105 }
17106 rinfo = arch_reg_rhs(state, entry->member, i);
17107 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17108 continue;
17109 }
17110 replace_rhs_use(state, ins, out, entry->member);
17111 }
17112 transform_to_arch_instruction(state, out);
17113 return out;
17114}
17115
Eric Biedermand1ea5392003-06-28 06:49:45 +000017116static struct triple *typed_pre_copy(
17117 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017118{
17119 /* Carefully insert enough operations so that I can
17120 * enter any operation with a GPR32.
17121 */
17122 struct triple *in;
17123 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017124 unsigned classes;
17125 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017126 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017127 if (ins->op == OP_PHI) {
17128 internal_error(state, ins, "pre_copy on a phi?");
17129 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017130 classes = arch_type_to_regcm(state, type);
17131 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017132 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017133 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017134 FILE *fp = state->errout;
17135 fprintf(fp, "src_type: ");
17136 name_of(fp, ins->type);
17137 fprintf(fp, "\ndst_type: ");
17138 name_of(fp, type);
17139 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017140 internal_error(state, ins, "pre_copy with no register classes");
17141 }
Eric Biederman90089602004-05-28 14:11:54 +000017142 op = OP_COPY;
17143 if (!equiv_types(type, (*expr)->type)) {
17144 op = OP_CONVERT;
17145 }
17146 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017147 unuse_triple(*expr, ins);
17148 *expr = in;
17149 use_triple(RHS(in, 0), in);
17150 use_triple(in, ins);
17151 transform_to_arch_instruction(state, in);
17152 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017153
17154}
17155static struct triple *pre_copy(
17156 struct compile_state *state, struct triple *ins, int index)
17157{
17158 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017159}
17160
17161
Eric Biedermanb138ac82003-04-22 18:44:01 +000017162static void insert_copies_to_phi(struct compile_state *state)
17163{
17164 /* To get out of ssa form we insert moves on the incoming
17165 * edges to blocks containting phi functions.
17166 */
17167 struct triple *first;
17168 struct triple *phi;
17169
17170 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017171 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017172 for(phi = first->next; phi != first ; phi = phi->next) {
17173 struct block_set *set;
17174 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017175 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017176 int edge;
17177 if (phi->op != OP_PHI) {
17178 continue;
17179 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017180 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017181 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017182 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017183 /* Phi's that feed into mandatory live range joins
17184 * cause nasty complications. Insert a copy of
17185 * the phi value so I never have to deal with
17186 * that in the rest of the code.
17187 */
17188 copy = post_copy(state, phi);
17189 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017190 /* Walk all of the incoming edges/blocks and insert moves.
17191 */
17192 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17193 struct block *eblock;
17194 struct triple *move;
17195 struct triple *val;
17196 struct triple *ptr;
17197 eblock = set->member;
17198 val = slot[edge];
17199
17200 if (val == phi) {
17201 continue;
17202 }
17203
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017204 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017205 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017206 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017207 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017208 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017209 use_triple(val, move);
17210
17211 slot[edge] = move;
17212 unuse_triple(val, phi);
17213 use_triple(move, phi);
17214
Eric Biederman66fe2222003-07-04 15:14:04 +000017215 /* Walk up the dominator tree until I have found the appropriate block */
17216 while(eblock && !tdominates(state, val, eblock->last)) {
17217 eblock = eblock->idom;
17218 }
17219 if (!eblock) {
17220 internal_error(state, phi, "Cannot find block dominated by %p",
17221 val);
17222 }
17223
Eric Biedermanb138ac82003-04-22 18:44:01 +000017224 /* Walk through the block backwards to find
17225 * an appropriate location for the OP_COPY.
17226 */
17227 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17228 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017229 if (ptr->op == OP_PIECE) {
17230 ptr = MISC(ptr, 0);
17231 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017232 if ((ptr == phi) || (ptr == val)) {
17233 goto out;
17234 }
Eric Biederman90089602004-05-28 14:11:54 +000017235 expr = triple_lhs(state, ptr, 0);
17236 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17237 if ((*expr) == val) {
17238 goto out;
17239 }
17240 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017241 expr = triple_rhs(state, ptr, 0);
17242 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17243 if ((*expr) == phi) {
17244 goto out;
17245 }
17246 }
17247 }
17248 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017249 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017250 internal_error(state, ptr,
17251 "Could not insert write to phi");
17252 }
Eric Biederman90089602004-05-28 14:11:54 +000017253 insert_triple(state, after_lhs(state, ptr), move);
17254 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017255 eblock->last = move;
17256 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017257 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017258 }
17259 }
Eric Biederman90089602004-05-28 14:11:54 +000017260 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017261}
17262
Eric Biederman90089602004-05-28 14:11:54 +000017263struct triple_reg_set;
17264struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017265
Eric Biedermanb138ac82003-04-22 18:44:01 +000017266
17267static int do_triple_set(struct triple_reg_set **head,
17268 struct triple *member, struct triple *new_member)
17269{
17270 struct triple_reg_set **ptr, *new;
17271 if (!member)
17272 return 0;
17273 ptr = head;
17274 while(*ptr) {
17275 if ((*ptr)->member == member) {
17276 return 0;
17277 }
17278 ptr = &(*ptr)->next;
17279 }
17280 new = xcmalloc(sizeof(*new), "triple_set");
17281 new->member = member;
17282 new->new = new_member;
17283 new->next = *head;
17284 *head = new;
17285 return 1;
17286}
17287
17288static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17289{
17290 struct triple_reg_set *entry, **ptr;
17291 ptr = head;
17292 while(*ptr) {
17293 entry = *ptr;
17294 if (entry->member == member) {
17295 *ptr = entry->next;
17296 xfree(entry);
17297 return;
17298 }
17299 else {
17300 ptr = &entry->next;
17301 }
17302 }
17303}
17304
17305static int in_triple(struct reg_block *rb, struct triple *in)
17306{
17307 return do_triple_set(&rb->in, in, 0);
17308}
17309static void unin_triple(struct reg_block *rb, struct triple *unin)
17310{
17311 do_triple_unset(&rb->in, unin);
17312}
17313
17314static int out_triple(struct reg_block *rb, struct triple *out)
17315{
17316 return do_triple_set(&rb->out, out, 0);
17317}
17318static void unout_triple(struct reg_block *rb, struct triple *unout)
17319{
17320 do_triple_unset(&rb->out, unout);
17321}
17322
17323static int initialize_regblock(struct reg_block *blocks,
17324 struct block *block, int vertex)
17325{
17326 struct block_set *user;
17327 if (!block || (blocks[block->vertex].block == block)) {
17328 return vertex;
17329 }
17330 vertex += 1;
17331 /* Renumber the blocks in a convinient fashion */
17332 block->vertex = vertex;
17333 blocks[vertex].block = block;
17334 blocks[vertex].vertex = vertex;
17335 for(user = block->use; user; user = user->next) {
17336 vertex = initialize_regblock(blocks, user->member, vertex);
17337 }
17338 return vertex;
17339}
17340
Eric Biederman90089602004-05-28 14:11:54 +000017341static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17342{
17343/* Part to piece is a best attempt and it cannot be correct all by
17344 * itself. If various values are read as different sizes in different
17345 * parts of the code this function cannot work. Or rather it cannot
17346 * work in conjunction with compute_variable_liftimes. As the
17347 * analysis will get confused.
17348 */
17349 struct triple *base;
17350 unsigned reg;
17351 if (!is_lvalue(state, ins)) {
17352 return ins;
17353 }
17354 base = 0;
17355 reg = 0;
17356 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17357 base = MISC(ins, 0);
17358 switch(ins->op) {
17359 case OP_INDEX:
17360 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17361 break;
17362 case OP_DOT:
17363 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17364 break;
17365 default:
17366 internal_error(state, ins, "unhandled part");
17367 break;
17368 }
17369 ins = base;
17370 }
17371 if (base) {
17372 if (reg > base->lhs) {
17373 internal_error(state, base, "part out of range?");
17374 }
17375 ins = LHS(base, reg);
17376 }
17377 return ins;
17378}
17379
17380static int this_def(struct compile_state *state,
17381 struct triple *ins, struct triple *other)
17382{
17383 if (ins == other) {
17384 return 1;
17385 }
17386 if (ins->op == OP_WRITE) {
17387 ins = part_to_piece(state, MISC(ins, 0));
17388 }
17389 return ins == other;
17390}
17391
Eric Biedermanb138ac82003-04-22 18:44:01 +000017392static int phi_in(struct compile_state *state, struct reg_block *blocks,
17393 struct reg_block *rb, struct block *suc)
17394{
17395 /* Read the conditional input set of a successor block
17396 * (i.e. the input to the phi nodes) and place it in the
17397 * current blocks output set.
17398 */
17399 struct block_set *set;
17400 struct triple *ptr;
17401 int edge;
17402 int done, change;
17403 change = 0;
17404 /* Find the edge I am coming in on */
17405 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17406 if (set->member == rb->block) {
17407 break;
17408 }
17409 }
17410 if (!set) {
17411 internal_error(state, 0, "Not coming on a control edge?");
17412 }
17413 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17414 struct triple **slot, *expr, *ptr2;
17415 int out_change, done2;
17416 done = (ptr == suc->last);
17417 if (ptr->op != OP_PHI) {
17418 continue;
17419 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017420 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017421 expr = slot[edge];
17422 out_change = out_triple(rb, expr);
17423 if (!out_change) {
17424 continue;
17425 }
17426 /* If we don't define the variable also plast it
17427 * in the current blocks input set.
17428 */
17429 ptr2 = rb->block->first;
17430 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017431 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017432 break;
17433 }
17434 done2 = (ptr2 == rb->block->last);
17435 }
17436 if (!done2) {
17437 continue;
17438 }
17439 change |= in_triple(rb, expr);
17440 }
17441 return change;
17442}
17443
17444static int reg_in(struct compile_state *state, struct reg_block *blocks,
17445 struct reg_block *rb, struct block *suc)
17446{
17447 struct triple_reg_set *in_set;
17448 int change;
17449 change = 0;
17450 /* Read the input set of a successor block
17451 * and place it in the current blocks output set.
17452 */
17453 in_set = blocks[suc->vertex].in;
17454 for(; in_set; in_set = in_set->next) {
17455 int out_change, done;
17456 struct triple *first, *last, *ptr;
17457 out_change = out_triple(rb, in_set->member);
17458 if (!out_change) {
17459 continue;
17460 }
17461 /* If we don't define the variable also place it
17462 * in the current blocks input set.
17463 */
17464 first = rb->block->first;
17465 last = rb->block->last;
17466 done = 0;
17467 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017468 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017469 break;
17470 }
17471 done = (ptr == last);
17472 }
17473 if (!done) {
17474 continue;
17475 }
17476 change |= in_triple(rb, in_set->member);
17477 }
17478 change |= phi_in(state, blocks, rb, suc);
17479 return change;
17480}
17481
Eric Biedermanb138ac82003-04-22 18:44:01 +000017482static int use_in(struct compile_state *state, struct reg_block *rb)
17483{
17484 /* Find the variables we use but don't define and add
17485 * it to the current blocks input set.
17486 */
17487#warning "FIXME is this O(N^2) algorithm bad?"
17488 struct block *block;
17489 struct triple *ptr;
17490 int done;
17491 int change;
17492 block = rb->block;
17493 change = 0;
17494 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17495 struct triple **expr;
17496 done = (ptr == block->first);
17497 /* The variable a phi function uses depends on the
17498 * control flow, and is handled in phi_in, not
17499 * here.
17500 */
17501 if (ptr->op == OP_PHI) {
17502 continue;
17503 }
17504 expr = triple_rhs(state, ptr, 0);
17505 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17506 struct triple *rhs, *test;
17507 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017508 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017509 if (!rhs) {
17510 continue;
17511 }
Eric Biederman90089602004-05-28 14:11:54 +000017512
17513 /* See if rhs is defined in this block.
17514 * A write counts as a definition.
17515 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017516 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17517 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017518 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017519 rhs = 0;
17520 break;
17521 }
17522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017523 /* If I still have a valid rhs add it to in */
17524 change |= in_triple(rb, rhs);
17525 }
17526 }
17527 return change;
17528}
17529
17530static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017531 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017532{
17533 struct reg_block *blocks;
17534 int change;
17535 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017536 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17537 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017538 do {
17539 int i;
17540 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017541 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017542 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017543 struct reg_block *rb;
17544 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017545 /* Add the all successor's input set to in */
17546 for(edge = rb->block->edges; edge; edge = edge->next) {
17547 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017548 }
17549 /* Add use to in... */
17550 change |= use_in(state, rb);
17551 }
17552 } while(change);
17553 return blocks;
17554}
17555
Eric Biederman90089602004-05-28 14:11:54 +000017556static void free_variable_lifetimes(struct compile_state *state,
17557 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017558{
17559 int i;
17560 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017561 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017562 struct triple_reg_set *entry, *next;
17563 struct reg_block *rb;
17564 rb = &blocks[i];
17565 for(entry = rb->in; entry ; entry = next) {
17566 next = entry->next;
17567 do_triple_unset(&rb->in, entry->member);
17568 }
17569 for(entry = rb->out; entry; entry = next) {
17570 next = entry->next;
17571 do_triple_unset(&rb->out, entry->member);
17572 }
17573 }
17574 xfree(blocks);
17575
17576}
17577
Eric Biedermanf96a8102003-06-16 16:57:34 +000017578typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000017579 struct compile_state *state,
17580 struct reg_block *blocks, struct triple_reg_set *live,
17581 struct reg_block *rb, struct triple *ins, void *arg);
17582
17583static void walk_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017584 struct basic_blocks *bb, struct reg_block *blocks,
17585 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017586{
17587 int i;
17588
Eric Biederman90089602004-05-28 14:11:54 +000017589 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017590 struct triple_reg_set *live;
17591 struct triple_reg_set *entry, *next;
17592 struct triple *ptr, *prev;
17593 struct reg_block *rb;
17594 struct block *block;
17595 int done;
17596
17597 /* Get the blocks */
17598 rb = &blocks[i];
17599 block = rb->block;
17600
17601 /* Copy out into live */
17602 live = 0;
17603 for(entry = rb->out; entry; entry = next) {
17604 next = entry->next;
17605 do_triple_set(&live, entry->member, entry->new);
17606 }
17607 /* Walk through the basic block calculating live */
17608 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017609 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017610
17611 prev = ptr->prev;
17612 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017613
17614 /* Ensure the current definition is in live */
17615 if (triple_is_def(state, ptr)) {
17616 do_triple_set(&live, ptr, 0);
17617 }
17618
17619 /* Inform the callback function of what is
17620 * going on.
17621 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017622 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017623
17624 /* Remove the current definition from live */
17625 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017626
Eric Biedermanb138ac82003-04-22 18:44:01 +000017627 /* Add the current uses to live.
17628 *
17629 * It is safe to skip phi functions because they do
17630 * not have any block local uses, and the block
17631 * output sets already properly account for what
17632 * control flow depedent uses phi functions do have.
17633 */
17634 if (ptr->op == OP_PHI) {
17635 continue;
17636 }
17637 expr = triple_rhs(state, ptr, 0);
17638 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17639 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017640 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017641 continue;
17642 }
17643 do_triple_set(&live, *expr, 0);
17644 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017645 }
17646 /* Free live */
17647 for(entry = live; entry; entry = next) {
17648 next = entry->next;
17649 do_triple_unset(&live, entry->member);
17650 }
17651 }
17652}
17653
Eric Biederman90089602004-05-28 14:11:54 +000017654struct print_live_variable_info {
17655 struct reg_block *rb;
17656 FILE *fp;
17657};
17658static void print_live_variables_block(
17659 struct compile_state *state, struct block *block, void *arg)
17660
17661{
17662 struct print_live_variable_info *info = arg;
17663 struct block_set *edge;
17664 FILE *fp = info->fp;
17665 struct reg_block *rb;
17666 struct triple *ptr;
17667 int phi_present;
17668 int done;
17669 rb = &info->rb[block->vertex];
17670
17671 fprintf(fp, "\nblock: %p (%d),",
17672 block, block->vertex);
17673 for(edge = block->edges; edge; edge = edge->next) {
17674 fprintf(fp, " %p<-%p",
17675 edge->member,
17676 edge->member && edge->member->use?edge->member->use->member : 0);
17677 }
17678 fprintf(fp, "\n");
17679 if (rb->in) {
17680 struct triple_reg_set *in_set;
17681 fprintf(fp, " in:");
17682 for(in_set = rb->in; in_set; in_set = in_set->next) {
17683 fprintf(fp, " %-10p", in_set->member);
17684 }
17685 fprintf(fp, "\n");
17686 }
17687 phi_present = 0;
17688 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17689 done = (ptr == block->last);
17690 if (ptr->op == OP_PHI) {
17691 phi_present = 1;
17692 break;
17693 }
17694 }
17695 if (phi_present) {
17696 int edge;
17697 for(edge = 0; edge < block->users; edge++) {
17698 fprintf(fp, " in(%d):", edge);
17699 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17700 struct triple **slot;
17701 done = (ptr == block->last);
17702 if (ptr->op != OP_PHI) {
17703 continue;
17704 }
17705 slot = &RHS(ptr, 0);
17706 fprintf(fp, " %-10p", slot[edge]);
17707 }
17708 fprintf(fp, "\n");
17709 }
17710 }
17711 if (block->first->op == OP_LABEL) {
17712 fprintf(fp, "%p:\n", block->first);
17713 }
17714 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17715 done = (ptr == block->last);
17716 display_triple(fp, ptr);
17717 }
17718 if (rb->out) {
17719 struct triple_reg_set *out_set;
17720 fprintf(fp, " out:");
17721 for(out_set = rb->out; out_set; out_set = out_set->next) {
17722 fprintf(fp, " %-10p", out_set->member);
17723 }
17724 fprintf(fp, "\n");
17725 }
17726 fprintf(fp, "\n");
17727}
17728
17729static void print_live_variables(struct compile_state *state,
17730 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17731{
17732 struct print_live_variable_info info;
17733 info.rb = rb;
17734 info.fp = fp;
17735 fprintf(fp, "\nlive variables by block\n");
17736 walk_blocks(state, bb, print_live_variables_block, &info);
17737
17738}
17739
17740
Eric Biedermanb138ac82003-04-22 18:44:01 +000017741static int count_triples(struct compile_state *state)
17742{
17743 struct triple *first, *ins;
17744 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017745 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017746 ins = first;
17747 do {
17748 triples++;
17749 ins = ins->next;
17750 } while (ins != first);
17751 return triples;
17752}
Eric Biederman66fe2222003-07-04 15:14:04 +000017753
17754
Eric Biedermanb138ac82003-04-22 18:44:01 +000017755struct dead_triple {
17756 struct triple *triple;
17757 struct dead_triple *work_next;
17758 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017759 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017760 int flags;
17761#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017762#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017763};
17764
Eric Biederman90089602004-05-28 14:11:54 +000017765static void print_dead_triples(struct compile_state *state,
17766 struct dead_triple *dtriple)
17767{
17768 struct triple *first, *ins;
17769 struct dead_triple *dt;
17770 FILE *fp;
17771 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17772 return;
17773 }
17774 fp = state->dbgout;
17775 fprintf(fp, "--------------- dtriples ---------------\n");
17776 first = state->first;
17777 ins = first;
17778 do {
17779 dt = &dtriple[ins->id];
17780 if ((ins->op == OP_LABEL) && (ins->use)) {
17781 fprintf(fp, "\n%p:\n", ins);
17782 }
17783 fprintf(fp, "%c",
17784 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17785 display_triple(fp, ins);
17786 if (triple_is_branch(state, ins)) {
17787 fprintf(fp, "\n");
17788 }
17789 ins = ins->next;
17790 } while(ins != first);
17791 fprintf(fp, "\n");
17792}
17793
Eric Biedermanb138ac82003-04-22 18:44:01 +000017794
17795static void awaken(
17796 struct compile_state *state,
17797 struct dead_triple *dtriple, struct triple **expr,
17798 struct dead_triple ***work_list_tail)
17799{
17800 struct triple *triple;
17801 struct dead_triple *dt;
17802 if (!expr) {
17803 return;
17804 }
17805 triple = *expr;
17806 if (!triple) {
17807 return;
17808 }
17809 if (triple->id <= 0) {
17810 internal_error(state, triple, "bad triple id: %d",
17811 triple->id);
17812 }
17813 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017814 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017815 return;
17816 }
17817 dt = &dtriple[triple->id];
17818 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17819 dt->flags |= TRIPLE_FLAG_ALIVE;
17820 if (!dt->work_next) {
17821 **work_list_tail = dt;
17822 *work_list_tail = &dt->work_next;
17823 }
17824 }
17825}
17826
17827static void eliminate_inefectual_code(struct compile_state *state)
17828{
17829 struct block *block;
17830 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17831 int triples, i;
Eric Biederman83b991a2003-10-11 06:20:25 +000017832 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017833
Eric Biederman5ade04a2003-10-22 04:03:46 +000017834 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17835 return;
17836 }
17837
Eric Biedermanb138ac82003-04-22 18:44:01 +000017838 /* Setup the work list */
17839 work_list = 0;
17840 work_list_tail = &work_list;
17841
Eric Biederman83b991a2003-10-11 06:20:25 +000017842 first = state->first;
17843 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017844
17845 /* Count how many triples I have */
17846 triples = count_triples(state);
17847
17848 /* Now put then in an array and mark all of the triples dead */
17849 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17850
17851 ins = first;
17852 i = 1;
17853 block = 0;
17854 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017855 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017856 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017857 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017858 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017859 ins->id = i;
17860 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017861 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017862 awaken(state, dtriple, &ins, &work_list_tail);
17863 }
17864 i++;
17865 ins = ins->next;
17866 } while(ins != first);
17867 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017868 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017869 struct dead_triple *dt;
17870 struct block_set *user;
17871 struct triple **expr;
17872 dt = work_list;
17873 work_list = dt->work_next;
17874 if (!work_list) {
17875 work_list_tail = &work_list;
17876 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017877 /* Make certain the block the current instruction is in lives */
17878 block = block_of_triple(state, dt->triple);
17879 awaken(state, dtriple, &block->first, &work_list_tail);
17880 if (triple_is_branch(state, block->last)) {
17881 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017882 } else {
17883 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017884 }
17885
Eric Biedermanb138ac82003-04-22 18:44:01 +000017886 /* Wake up the data depencencies of this triple */
17887 expr = 0;
17888 do {
17889 expr = triple_rhs(state, dt->triple, expr);
17890 awaken(state, dtriple, expr, &work_list_tail);
17891 } while(expr);
17892 do {
17893 expr = triple_lhs(state, dt->triple, expr);
17894 awaken(state, dtriple, expr, &work_list_tail);
17895 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017896 do {
17897 expr = triple_misc(state, dt->triple, expr);
17898 awaken(state, dtriple, expr, &work_list_tail);
17899 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017900 /* Wake up the forward control dependencies */
17901 do {
17902 expr = triple_targ(state, dt->triple, expr);
17903 awaken(state, dtriple, expr, &work_list_tail);
17904 } while(expr);
17905 /* Wake up the reverse control dependencies of this triple */
17906 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017907 struct triple *last;
17908 last = user->member->last;
17909 while((last->op == OP_NOOP) && (last != user->member->first)) {
17910 internal_warning(state, last, "awakening noop?");
17911 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017912 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017913 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017914 }
17915 }
Eric Biederman90089602004-05-28 14:11:54 +000017916 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017917 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
17918 if ((dt->triple->op == OP_NOOP) &&
17919 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17920 internal_error(state, dt->triple, "noop effective?");
17921 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017922 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017923 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017924 release_triple(state, dt->triple);
17925 }
17926 }
17927 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000017928
17929 rebuild_ssa_form(state);
17930
Eric Biederman90089602004-05-28 14:11:54 +000017931 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017932}
17933
17934
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017935static void insert_mandatory_copies(struct compile_state *state)
17936{
17937 struct triple *ins, *first;
17938
17939 /* The object is with a minimum of inserted copies,
17940 * to resolve in fundamental register conflicts between
17941 * register value producers and consumers.
17942 * Theoretically we may be greater than minimal when we
17943 * are inserting copies before instructions but that
17944 * case should be rare.
17945 */
Eric Biederman83b991a2003-10-11 06:20:25 +000017946 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017947 ins = first;
17948 do {
17949 struct triple_set *entry, *next;
17950 struct triple *tmp;
17951 struct reg_info info;
17952 unsigned reg, regcm;
17953 int do_post_copy, do_pre_copy;
17954 tmp = 0;
17955 if (!triple_is_def(state, ins)) {
17956 goto next;
17957 }
17958 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000017959 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017960 if (info.reg >= MAX_REGISTERS) {
17961 info.reg = REG_UNSET;
17962 }
Eric Biederman90089602004-05-28 14:11:54 +000017963
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017964 reg = REG_UNSET;
17965 regcm = arch_type_to_regcm(state, ins->type);
17966 do_post_copy = do_pre_copy = 0;
17967
17968 /* Walk through the uses of ins and check for conflicts */
17969 for(entry = ins->use; entry; entry = next) {
17970 struct reg_info rinfo;
17971 int i;
17972 next = entry->next;
17973 i = find_rhs_use(state, entry->member, ins);
17974 if (i < 0) {
17975 continue;
17976 }
17977
17978 /* Find the users color requirements */
17979 rinfo = arch_reg_rhs(state, entry->member, i);
17980 if (rinfo.reg >= MAX_REGISTERS) {
17981 rinfo.reg = REG_UNSET;
17982 }
17983
17984 /* See if I need a pre_copy */
17985 if (rinfo.reg != REG_UNSET) {
17986 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
17987 do_pre_copy = 1;
17988 }
17989 reg = rinfo.reg;
17990 }
17991 regcm &= rinfo.regcm;
17992 regcm = arch_regcm_normalize(state, regcm);
17993 if (regcm == 0) {
17994 do_pre_copy = 1;
17995 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017996 /* Always use pre_copies for constants.
17997 * They do not take up any registers until a
17998 * copy places them in one.
17999 */
18000 if ((info.reg == REG_UNNEEDED) &&
18001 (rinfo.reg != REG_UNNEEDED)) {
18002 do_pre_copy = 1;
18003 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018004 }
18005 do_post_copy =
18006 !do_pre_copy &&
18007 (((info.reg != REG_UNSET) &&
18008 (reg != REG_UNSET) &&
18009 (info.reg != reg)) ||
18010 ((info.regcm & regcm) == 0));
18011
18012 reg = info.reg;
18013 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018014 /* 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 +000018015 for(entry = ins->use; entry; entry = next) {
18016 struct reg_info rinfo;
18017 int i;
18018 next = entry->next;
18019 i = find_rhs_use(state, entry->member, ins);
18020 if (i < 0) {
18021 continue;
18022 }
18023
18024 /* Find the users color requirements */
18025 rinfo = arch_reg_rhs(state, entry->member, i);
18026 if (rinfo.reg >= MAX_REGISTERS) {
18027 rinfo.reg = REG_UNSET;
18028 }
18029
18030 /* Now see if it is time to do the pre_copy */
18031 if (rinfo.reg != REG_UNSET) {
18032 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18033 ((regcm & rinfo.regcm) == 0) ||
18034 /* Don't let a mandatory coalesce sneak
18035 * into a operation that is marked to prevent
18036 * coalescing.
18037 */
18038 ((reg != REG_UNNEEDED) &&
18039 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18040 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18041 ) {
18042 if (do_pre_copy) {
18043 struct triple *user;
18044 user = entry->member;
18045 if (RHS(user, i) != ins) {
18046 internal_error(state, user, "bad rhs");
18047 }
18048 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018049 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018050 continue;
18051 } else {
18052 do_post_copy = 1;
18053 }
18054 }
18055 reg = rinfo.reg;
18056 }
18057 if ((regcm & rinfo.regcm) == 0) {
18058 if (do_pre_copy) {
18059 struct triple *user;
18060 user = entry->member;
18061 if (RHS(user, i) != ins) {
18062 internal_error(state, user, "bad rhs");
18063 }
18064 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018065 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018066 continue;
18067 } else {
18068 do_post_copy = 1;
18069 }
18070 }
18071 regcm &= rinfo.regcm;
18072
18073 }
18074 if (do_post_copy) {
18075 struct reg_info pre, post;
18076 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018077 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018078 pre = arch_reg_lhs(state, ins, 0);
18079 post = arch_reg_lhs(state, tmp, 0);
18080 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18081 internal_error(state, tmp, "useless copy");
18082 }
18083 }
18084 next:
18085 ins = ins->next;
18086 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018087
Eric Biederman90089602004-05-28 14:11:54 +000018088 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018089}
18090
18091
Eric Biedermanb138ac82003-04-22 18:44:01 +000018092struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018093struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018094struct live_range {
18095 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018096 struct live_range_def *defs;
18097/* Note. The list pointed to by defs is kept in order.
18098 * That is baring splits in the flow control
18099 * defs dominates defs->next wich dominates defs->next->next
18100 * etc.
18101 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018102 unsigned color;
18103 unsigned classes;
18104 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018105 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018106 struct live_range *group_next, **group_prev;
18107};
18108
18109struct live_range_edge {
18110 struct live_range_edge *next;
18111 struct live_range *node;
18112};
18113
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018114struct live_range_def {
18115 struct live_range_def *next;
18116 struct live_range_def *prev;
18117 struct live_range *lr;
18118 struct triple *def;
18119 unsigned orig_id;
18120};
18121
Eric Biedermanb138ac82003-04-22 18:44:01 +000018122#define LRE_HASH_SIZE 2048
18123struct lre_hash {
18124 struct lre_hash *next;
18125 struct live_range *left;
18126 struct live_range *right;
18127};
18128
18129
18130struct reg_state {
18131 struct lre_hash *hash[LRE_HASH_SIZE];
18132 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018133 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018134 struct live_range *lr;
18135 struct live_range *low, **low_tail;
18136 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018137 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018138 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018139 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018140};
18141
18142
Eric Biedermand1ea5392003-06-28 06:49:45 +000018143struct print_interference_block_info {
18144 struct reg_state *rstate;
18145 FILE *fp;
18146 int need_edges;
18147};
18148static void print_interference_block(
18149 struct compile_state *state, struct block *block, void *arg)
18150
18151{
18152 struct print_interference_block_info *info = arg;
18153 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018154 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018155 FILE *fp = info->fp;
18156 struct reg_block *rb;
18157 struct triple *ptr;
18158 int phi_present;
18159 int done;
18160 rb = &rstate->blocks[block->vertex];
18161
Eric Biederman5ade04a2003-10-22 04:03:46 +000018162 fprintf(fp, "\nblock: %p (%d),",
18163 block, block->vertex);
18164 for(edge = block->edges; edge; edge = edge->next) {
18165 fprintf(fp, " %p<-%p",
18166 edge->member,
18167 edge->member && edge->member->use?edge->member->use->member : 0);
18168 }
18169 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018170 if (rb->in) {
18171 struct triple_reg_set *in_set;
18172 fprintf(fp, " in:");
18173 for(in_set = rb->in; in_set; in_set = in_set->next) {
18174 fprintf(fp, " %-10p", in_set->member);
18175 }
18176 fprintf(fp, "\n");
18177 }
18178 phi_present = 0;
18179 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18180 done = (ptr == block->last);
18181 if (ptr->op == OP_PHI) {
18182 phi_present = 1;
18183 break;
18184 }
18185 }
18186 if (phi_present) {
18187 int edge;
18188 for(edge = 0; edge < block->users; edge++) {
18189 fprintf(fp, " in(%d):", edge);
18190 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18191 struct triple **slot;
18192 done = (ptr == block->last);
18193 if (ptr->op != OP_PHI) {
18194 continue;
18195 }
18196 slot = &RHS(ptr, 0);
18197 fprintf(fp, " %-10p", slot[edge]);
18198 }
18199 fprintf(fp, "\n");
18200 }
18201 }
18202 if (block->first->op == OP_LABEL) {
18203 fprintf(fp, "%p:\n", block->first);
18204 }
18205 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018206 struct live_range *lr;
18207 unsigned id;
18208 int op;
18209 op = ptr->op;
18210 done = (ptr == block->last);
18211 lr = rstate->lrd[ptr->id].lr;
18212
Eric Biedermand1ea5392003-06-28 06:49:45 +000018213 id = ptr->id;
18214 ptr->id = rstate->lrd[id].orig_id;
18215 SET_REG(ptr->id, lr->color);
18216 display_triple(fp, ptr);
18217 ptr->id = id;
18218
18219 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18220 internal_error(state, ptr, "lr has no defs!");
18221 }
18222 if (info->need_edges) {
18223 if (lr->defs) {
18224 struct live_range_def *lrd;
18225 fprintf(fp, " range:");
18226 lrd = lr->defs;
18227 do {
18228 fprintf(fp, " %-10p", lrd->def);
18229 lrd = lrd->next;
18230 } while(lrd != lr->defs);
18231 fprintf(fp, "\n");
18232 }
18233 if (lr->edges > 0) {
18234 struct live_range_edge *edge;
18235 fprintf(fp, " edges:");
18236 for(edge = lr->edges; edge; edge = edge->next) {
18237 struct live_range_def *lrd;
18238 lrd = edge->node->defs;
18239 do {
18240 fprintf(fp, " %-10p", lrd->def);
18241 lrd = lrd->next;
18242 } while(lrd != edge->node->defs);
18243 fprintf(fp, "|");
18244 }
18245 fprintf(fp, "\n");
18246 }
18247 }
18248 /* Do a bunch of sanity checks */
18249 valid_ins(state, ptr);
18250 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18251 internal_error(state, ptr, "Invalid triple id: %d",
18252 ptr->id);
18253 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018254 }
18255 if (rb->out) {
18256 struct triple_reg_set *out_set;
18257 fprintf(fp, " out:");
18258 for(out_set = rb->out; out_set; out_set = out_set->next) {
18259 fprintf(fp, " %-10p", out_set->member);
18260 }
18261 fprintf(fp, "\n");
18262 }
18263 fprintf(fp, "\n");
18264}
18265
18266static void print_interference_blocks(
18267 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18268{
18269 struct print_interference_block_info info;
18270 info.rstate = rstate;
18271 info.fp = fp;
18272 info.need_edges = need_edges;
18273 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018274 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018275
18276}
18277
Eric Biedermanb138ac82003-04-22 18:44:01 +000018278static unsigned regc_max_size(struct compile_state *state, int classes)
18279{
18280 unsigned max_size;
18281 int i;
18282 max_size = 0;
18283 for(i = 0; i < MAX_REGC; i++) {
18284 if (classes & (1 << i)) {
18285 unsigned size;
18286 size = arch_regc_size(state, i);
18287 if (size > max_size) {
18288 max_size = size;
18289 }
18290 }
18291 }
18292 return max_size;
18293}
18294
18295static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18296{
18297 unsigned equivs[MAX_REG_EQUIVS];
18298 int i;
18299 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18300 internal_error(state, 0, "invalid register");
18301 }
18302 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18303 internal_error(state, 0, "invalid register");
18304 }
18305 arch_reg_equivs(state, equivs, reg1);
18306 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18307 if (equivs[i] == reg2) {
18308 return 1;
18309 }
18310 }
18311 return 0;
18312}
18313
18314static void reg_fill_used(struct compile_state *state, char *used, int reg)
18315{
18316 unsigned equivs[MAX_REG_EQUIVS];
18317 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018318 if (reg == REG_UNNEEDED) {
18319 return;
18320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018321 arch_reg_equivs(state, equivs, reg);
18322 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18323 used[equivs[i]] = 1;
18324 }
18325 return;
18326}
18327
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018328static void reg_inc_used(struct compile_state *state, char *used, int reg)
18329{
18330 unsigned equivs[MAX_REG_EQUIVS];
18331 int i;
18332 if (reg == REG_UNNEEDED) {
18333 return;
18334 }
18335 arch_reg_equivs(state, equivs, reg);
18336 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18337 used[equivs[i]] += 1;
18338 }
18339 return;
18340}
18341
Eric Biedermanb138ac82003-04-22 18:44:01 +000018342static unsigned int hash_live_edge(
18343 struct live_range *left, struct live_range *right)
18344{
18345 unsigned int hash, val;
18346 unsigned long lval, rval;
18347 lval = ((unsigned long)left)/sizeof(struct live_range);
18348 rval = ((unsigned long)right)/sizeof(struct live_range);
18349 hash = 0;
18350 while(lval) {
18351 val = lval & 0xff;
18352 lval >>= 8;
18353 hash = (hash *263) + val;
18354 }
18355 while(rval) {
18356 val = rval & 0xff;
18357 rval >>= 8;
18358 hash = (hash *263) + val;
18359 }
18360 hash = hash & (LRE_HASH_SIZE - 1);
18361 return hash;
18362}
18363
18364static struct lre_hash **lre_probe(struct reg_state *rstate,
18365 struct live_range *left, struct live_range *right)
18366{
18367 struct lre_hash **ptr;
18368 unsigned int index;
18369 /* Ensure left <= right */
18370 if (left > right) {
18371 struct live_range *tmp;
18372 tmp = left;
18373 left = right;
18374 right = tmp;
18375 }
18376 index = hash_live_edge(left, right);
18377
18378 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018379 while(*ptr) {
18380 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18381 break;
18382 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018383 ptr = &(*ptr)->next;
18384 }
18385 return ptr;
18386}
18387
18388static int interfere(struct reg_state *rstate,
18389 struct live_range *left, struct live_range *right)
18390{
18391 struct lre_hash **ptr;
18392 ptr = lre_probe(rstate, left, right);
18393 return ptr && *ptr;
18394}
18395
18396static void add_live_edge(struct reg_state *rstate,
18397 struct live_range *left, struct live_range *right)
18398{
18399 /* FIXME the memory allocation overhead is noticeable here... */
18400 struct lre_hash **ptr, *new_hash;
18401 struct live_range_edge *edge;
18402
18403 if (left == right) {
18404 return;
18405 }
18406 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18407 return;
18408 }
18409 /* Ensure left <= right */
18410 if (left > right) {
18411 struct live_range *tmp;
18412 tmp = left;
18413 left = right;
18414 right = tmp;
18415 }
18416 ptr = lre_probe(rstate, left, right);
18417 if (*ptr) {
18418 return;
18419 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018420#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018421 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018422 left, right);
18423#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018424 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18425 new_hash->next = *ptr;
18426 new_hash->left = left;
18427 new_hash->right = right;
18428 *ptr = new_hash;
18429
18430 edge = xmalloc(sizeof(*edge), "live_range_edge");
18431 edge->next = left->edges;
18432 edge->node = right;
18433 left->edges = edge;
18434 left->degree += 1;
18435
18436 edge = xmalloc(sizeof(*edge), "live_range_edge");
18437 edge->next = right->edges;
18438 edge->node = left;
18439 right->edges = edge;
18440 right->degree += 1;
18441}
18442
18443static void remove_live_edge(struct reg_state *rstate,
18444 struct live_range *left, struct live_range *right)
18445{
18446 struct live_range_edge *edge, **ptr;
18447 struct lre_hash **hptr, *entry;
18448 hptr = lre_probe(rstate, left, right);
18449 if (!hptr || !*hptr) {
18450 return;
18451 }
18452 entry = *hptr;
18453 *hptr = entry->next;
18454 xfree(entry);
18455
18456 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18457 edge = *ptr;
18458 if (edge->node == right) {
18459 *ptr = edge->next;
18460 memset(edge, 0, sizeof(*edge));
18461 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018462 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018463 break;
18464 }
18465 }
18466 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18467 edge = *ptr;
18468 if (edge->node == left) {
18469 *ptr = edge->next;
18470 memset(edge, 0, sizeof(*edge));
18471 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018472 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018473 break;
18474 }
18475 }
18476}
18477
18478static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18479{
18480 struct live_range_edge *edge, *next;
18481 for(edge = range->edges; edge; edge = next) {
18482 next = edge->next;
18483 remove_live_edge(rstate, range, edge->node);
18484 }
18485}
18486
Eric Biederman153ea352003-06-20 14:43:20 +000018487static void transfer_live_edges(struct reg_state *rstate,
18488 struct live_range *dest, struct live_range *src)
18489{
18490 struct live_range_edge *edge, *next;
18491 for(edge = src->edges; edge; edge = next) {
18492 struct live_range *other;
18493 next = edge->next;
18494 other = edge->node;
18495 remove_live_edge(rstate, src, other);
18496 add_live_edge(rstate, dest, other);
18497 }
18498}
18499
Eric Biedermanb138ac82003-04-22 18:44:01 +000018500
18501/* Interference graph...
18502 *
18503 * new(n) --- Return a graph with n nodes but no edges.
18504 * add(g,x,y) --- Return a graph including g with an between x and y
18505 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18506 * x and y in the graph g
18507 * degree(g, x) --- Return the degree of the node x in the graph g
18508 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18509 *
18510 * Implement with a hash table && a set of adjcency vectors.
18511 * The hash table supports constant time implementations of add and interfere.
18512 * The adjacency vectors support an efficient implementation of neighbors.
18513 */
18514
18515/*
18516 * +---------------------------------------------------+
18517 * | +--------------+ |
18518 * v v | |
18519 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
18520 *
18521 * -- In simplify implment optimistic coloring... (No backtracking)
18522 * -- Implement Rematerialization it is the only form of spilling we can perform
18523 * Essentially this means dropping a constant from a register because
18524 * we can regenerate it later.
18525 *
18526 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18527 * coalesce at phi points...
18528 * --- Bias coloring if at all possible do the coalesing a compile time.
18529 *
18530 *
18531 */
18532
18533static void different_colored(
18534 struct compile_state *state, struct reg_state *rstate,
18535 struct triple *parent, struct triple *ins)
18536{
18537 struct live_range *lr;
18538 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018539 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018540 expr = triple_rhs(state, ins, 0);
18541 for(;expr; expr = triple_rhs(state, ins, expr)) {
18542 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018543 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018544 continue;
18545 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018546 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018547 if (lr->color == lr2->color) {
18548 internal_error(state, ins, "live range too big");
18549 }
18550 }
18551}
18552
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018553
18554static struct live_range *coalesce_ranges(
18555 struct compile_state *state, struct reg_state *rstate,
18556 struct live_range *lr1, struct live_range *lr2)
18557{
18558 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18559 unsigned color;
18560 unsigned classes;
18561 if (lr1 == lr2) {
18562 return lr1;
18563 }
18564 if (!lr1->defs || !lr2->defs) {
18565 internal_error(state, 0,
18566 "cannot coalese dead live ranges");
18567 }
18568 if ((lr1->color == REG_UNNEEDED) ||
18569 (lr2->color == REG_UNNEEDED)) {
18570 internal_error(state, 0,
18571 "cannot coalesce live ranges without a possible color");
18572 }
18573 if ((lr1->color != lr2->color) &&
18574 (lr1->color != REG_UNSET) &&
18575 (lr2->color != REG_UNSET)) {
18576 internal_error(state, lr1->defs->def,
18577 "cannot coalesce live ranges of different colors");
18578 }
18579 color = lr1->color;
18580 if (color == REG_UNSET) {
18581 color = lr2->color;
18582 }
18583 classes = lr1->classes & lr2->classes;
18584 if (!classes) {
18585 internal_error(state, lr1->defs->def,
18586 "cannot coalesce live ranges with dissimilar register classes");
18587 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018588 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018589 FILE *fp = state->errout;
18590 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018591 lrd = lr1->defs;
18592 do {
Eric Biederman90089602004-05-28 14:11:54 +000018593 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018594 lrd = lrd->next;
18595 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018596 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018597 lrd = lr2->defs;
18598 do {
Eric Biederman90089602004-05-28 14:11:54 +000018599 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018600 lrd = lrd->next;
18601 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018602 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018603 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018604 /* If there is a clear dominate live range put it in lr1,
18605 * For purposes of this test phi functions are
18606 * considered dominated by the definitions that feed into
18607 * them.
18608 */
18609 if ((lr1->defs->prev->def->op == OP_PHI) ||
18610 ((lr2->defs->prev->def->op != OP_PHI) &&
18611 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18612 struct live_range *tmp;
18613 tmp = lr1;
18614 lr1 = lr2;
18615 lr2 = tmp;
18616 }
18617#if 0
18618 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018619 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018620 }
18621 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018622 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018623 }
18624 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018625 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018626 }
18627 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018628 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018629 }
18630#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018631#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018632 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018633 lr1->defs->def,
18634 lr1->color,
18635 lr2->defs->def,
18636 lr2->color);
18637#endif
18638
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018639 /* Append lr2 onto lr1 */
18640#warning "FIXME should this be a merge instead of a splice?"
Eric Biederman153ea352003-06-20 14:43:20 +000018641 /* This FIXME item applies to the correctness of live_range_end
18642 * and to the necessity of making multiple passes of coalesce_live_ranges.
18643 * A failure to find some coalesce opportunities in coaleace_live_ranges
18644 * does not impact the correct of the compiler just the efficiency with
18645 * which registers are allocated.
18646 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018647 head = lr1->defs;
18648 mid1 = lr1->defs->prev;
18649 mid2 = lr2->defs;
18650 end = lr2->defs->prev;
18651
18652 head->prev = end;
18653 end->next = head;
18654
18655 mid1->next = mid2;
18656 mid2->prev = mid1;
18657
18658 /* Fixup the live range in the added live range defs */
18659 lrd = head;
18660 do {
18661 lrd->lr = lr1;
18662 lrd = lrd->next;
18663 } while(lrd != head);
18664
18665 /* Mark lr2 as free. */
18666 lr2->defs = 0;
18667 lr2->color = REG_UNNEEDED;
18668 lr2->classes = 0;
18669
18670 if (!lr1->defs) {
18671 internal_error(state, 0, "lr1->defs == 0 ?");
18672 }
18673
18674 lr1->color = color;
18675 lr1->classes = classes;
18676
Eric Biederman153ea352003-06-20 14:43:20 +000018677 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18678 transfer_live_edges(rstate, lr1, lr2);
18679
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018680 return lr1;
18681}
18682
18683static struct live_range_def *live_range_head(
18684 struct compile_state *state, struct live_range *lr,
18685 struct live_range_def *last)
18686{
18687 struct live_range_def *result;
18688 result = 0;
18689 if (last == 0) {
18690 result = lr->defs;
18691 }
18692 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18693 result = last->next;
18694 }
18695 return result;
18696}
18697
18698static struct live_range_def *live_range_end(
18699 struct compile_state *state, struct live_range *lr,
18700 struct live_range_def *last)
18701{
18702 struct live_range_def *result;
18703 result = 0;
18704 if (last == 0) {
18705 result = lr->defs->prev;
18706 }
18707 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18708 result = last->prev;
18709 }
18710 return result;
18711}
18712
18713
Eric Biedermanb138ac82003-04-22 18:44:01 +000018714static void initialize_live_ranges(
18715 struct compile_state *state, struct reg_state *rstate)
18716{
18717 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018718 size_t count, size;
18719 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018720
Eric Biederman83b991a2003-10-11 06:20:25 +000018721 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018722 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018723 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018724 count = count_triples(state);
18725 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018726 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018727 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018728 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018729 /* Potentially I need one live range for each instruction
18730 * plus an extra for the dummy live range.
18731 */
18732 rstate->ranges = count + 1;
18733 size = sizeof(rstate->lrd[0]) * rstate->defs;
18734 rstate->lrd = xcmalloc(size, "live_range_def");
18735 size = sizeof(rstate->lr[0]) * rstate->ranges;
18736 rstate->lr = xcmalloc(size, "live_range");
18737
Eric Biedermanb138ac82003-04-22 18:44:01 +000018738 /* Setup the dummy live range */
18739 rstate->lr[0].classes = 0;
18740 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018741 rstate->lr[0].defs = 0;
18742 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018743 ins = first;
18744 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018745 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018746 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018747 struct reg_info info;
18748 /* Find the architecture specific color information */
18749 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018750 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018751 rstate->lr[i].defs = &rstate->lrd[j];
18752 rstate->lr[i].color = info.reg;
18753 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018754 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018755 rstate->lrd[j].lr = &rstate->lr[i];
18756 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018757 /* Otherwise give the triple the dummy live range. */
18758 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018759 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018760 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018761
18762 /* Initalize the live_range_def */
18763 rstate->lrd[j].next = &rstate->lrd[j];
18764 rstate->lrd[j].prev = &rstate->lrd[j];
18765 rstate->lrd[j].def = ins;
18766 rstate->lrd[j].orig_id = ins->id;
18767 ins->id = j;
18768
18769 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018770 ins = ins->next;
18771 } while(ins != first);
18772 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018773
Eric Biedermanb138ac82003-04-22 18:44:01 +000018774 /* Make a second pass to handle achitecture specific register
18775 * constraints.
18776 */
18777 ins = first;
18778 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018779 int zlhs, zrhs, i, j;
18780 if (ins->id > rstate->defs) {
18781 internal_error(state, ins, "bad id");
18782 }
18783
18784 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018785 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018786 if ((zlhs == 0) && triple_is_def(state, ins)) {
18787 zlhs = 1;
18788 }
Eric Biederman90089602004-05-28 14:11:54 +000018789 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018790
Eric Biederman5ade04a2003-10-22 04:03:46 +000018791 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018792 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018793 ins, zlhs, zrhs);
18794 }
18795
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018796 for(i = 0; i < zlhs; i++) {
18797 struct reg_info linfo;
18798 struct live_range_def *lhs;
18799 linfo = arch_reg_lhs(state, ins, i);
18800 if (linfo.reg < MAX_REGISTERS) {
18801 continue;
18802 }
18803 if (triple_is_def(state, ins)) {
18804 lhs = &rstate->lrd[ins->id];
18805 } else {
18806 lhs = &rstate->lrd[LHS(ins, i)->id];
18807 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018808
18809 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018810 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018811 i, lhs, linfo.reg);
18812 }
18813
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018814 for(j = 0; j < zrhs; j++) {
18815 struct reg_info rinfo;
18816 struct live_range_def *rhs;
18817 rinfo = arch_reg_rhs(state, ins, j);
18818 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018819 continue;
18820 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018821 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018822
18823 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018824 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018825 j, rhs, rinfo.reg);
18826 }
18827
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018828 if (rinfo.reg == linfo.reg) {
18829 coalesce_ranges(state, rstate,
18830 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018831 }
18832 }
18833 }
18834 ins = ins->next;
18835 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018836}
18837
Eric Biedermanf96a8102003-06-16 16:57:34 +000018838static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018839 struct compile_state *state,
18840 struct reg_block *blocks, struct triple_reg_set *live,
18841 struct reg_block *rb, struct triple *ins, void *arg)
18842{
18843 struct reg_state *rstate = arg;
18844 struct live_range *def;
18845 struct triple_reg_set *entry;
18846
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018847 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018848 * we do not have a definition to add to
18849 * the interference graph.
18850 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018851 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018852 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018853 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018854 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018855
18856 /* Create an edge between ins and everything that is
18857 * alive, unless the live_range cannot share
18858 * a physical register with ins.
18859 */
18860 for(entry = live; entry; entry = entry->next) {
18861 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018862 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18863 internal_error(state, 0, "bad entry?");
18864 }
18865 lr = rstate->lrd[entry->member->id].lr;
18866 if (def == lr) {
18867 continue;
18868 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018869 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18870 continue;
18871 }
18872 add_live_edge(rstate, def, lr);
18873 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018874 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018875}
18876
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018877static struct live_range *get_verify_live_range(
18878 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18879{
18880 struct live_range *lr;
18881 struct live_range_def *lrd;
18882 int ins_found;
18883 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18884 internal_error(state, ins, "bad ins?");
18885 }
18886 lr = rstate->lrd[ins->id].lr;
18887 ins_found = 0;
18888 lrd = lr->defs;
18889 do {
18890 if (lrd->def == ins) {
18891 ins_found = 1;
18892 }
18893 lrd = lrd->next;
18894 } while(lrd != lr->defs);
18895 if (!ins_found) {
18896 internal_error(state, ins, "ins not in live range");
18897 }
18898 return lr;
18899}
18900
18901static void verify_graph_ins(
18902 struct compile_state *state,
18903 struct reg_block *blocks, struct triple_reg_set *live,
18904 struct reg_block *rb, struct triple *ins, void *arg)
18905{
18906 struct reg_state *rstate = arg;
18907 struct triple_reg_set *entry1, *entry2;
18908
18909
18910 /* Compare live against edges and make certain the code is working */
18911 for(entry1 = live; entry1; entry1 = entry1->next) {
18912 struct live_range *lr1;
18913 lr1 = get_verify_live_range(state, rstate, entry1->member);
18914 for(entry2 = live; entry2; entry2 = entry2->next) {
18915 struct live_range *lr2;
18916 struct live_range_edge *edge2;
18917 int lr1_found;
18918 int lr2_degree;
18919 if (entry2 == entry1) {
18920 continue;
18921 }
18922 lr2 = get_verify_live_range(state, rstate, entry2->member);
18923 if (lr1 == lr2) {
18924 internal_error(state, entry2->member,
18925 "live range with 2 values simultaneously alive");
18926 }
18927 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
18928 continue;
18929 }
18930 if (!interfere(rstate, lr1, lr2)) {
18931 internal_error(state, entry2->member,
18932 "edges don't interfere?");
18933 }
18934
18935 lr1_found = 0;
18936 lr2_degree = 0;
18937 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
18938 lr2_degree++;
18939 if (edge2->node == lr1) {
18940 lr1_found = 1;
18941 }
18942 }
18943 if (lr2_degree != lr2->degree) {
18944 internal_error(state, entry2->member,
18945 "computed degree: %d does not match reported degree: %d\n",
18946 lr2_degree, lr2->degree);
18947 }
18948 if (!lr1_found) {
18949 internal_error(state, entry2->member, "missing edge");
18950 }
18951 }
18952 }
18953 return;
18954}
18955
Eric Biedermanb138ac82003-04-22 18:44:01 +000018956
Eric Biedermanf96a8102003-06-16 16:57:34 +000018957static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018958 struct compile_state *state,
18959 struct reg_block *blocks, struct triple_reg_set *live,
18960 struct reg_block *rb, struct triple *ins, void *arg)
18961{
18962 struct reg_state *rstate = arg;
18963 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018964 unsigned id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018965
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018966 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018967 id = ins->id;
18968 ins->id = rstate->lrd[id].orig_id;
18969 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000018970 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018971 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018972
18973 if (lr->defs) {
18974 struct live_range_def *lrd;
18975 printf(" range:");
18976 lrd = lr->defs;
18977 do {
18978 printf(" %-10p", lrd->def);
18979 lrd = lrd->next;
18980 } while(lrd != lr->defs);
18981 printf("\n");
18982 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018983 if (live) {
18984 struct triple_reg_set *entry;
18985 printf(" live:");
18986 for(entry = live; entry; entry = entry->next) {
18987 printf(" %-10p", entry->member);
18988 }
18989 printf("\n");
18990 }
18991 if (lr->edges) {
18992 struct live_range_edge *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018993 printf(" edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000018994 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018995 struct live_range_def *lrd;
18996 lrd = entry->node->defs;
18997 do {
18998 printf(" %-10p", lrd->def);
18999 lrd = lrd->next;
19000 } while(lrd != entry->node->defs);
19001 printf("|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019002 }
19003 printf("\n");
19004 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019005 if (triple_is_branch(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019006 printf("\n");
19007 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019008 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019009}
19010
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019011static int coalesce_live_ranges(
19012 struct compile_state *state, struct reg_state *rstate)
19013{
19014 /* At the point where a value is moved from one
19015 * register to another that value requires two
19016 * registers, thus increasing register pressure.
19017 * Live range coaleescing reduces the register
19018 * pressure by keeping a value in one register
19019 * longer.
19020 *
19021 * In the case of a phi function all paths leading
19022 * into it must be allocated to the same register
19023 * otherwise the phi function may not be removed.
19024 *
19025 * Forcing a value to stay in a single register
19026 * for an extended period of time does have
19027 * limitations when applied to non homogenous
19028 * register pool.
19029 *
19030 * The two cases I have identified are:
19031 * 1) Two forced register assignments may
19032 * collide.
19033 * 2) Registers may go unused because they
19034 * are only good for storing the value
19035 * and not manipulating it.
19036 *
19037 * Because of this I need to split live ranges,
19038 * even outside of the context of coalesced live
19039 * ranges. The need to split live ranges does
19040 * impose some constraints on live range coalescing.
19041 *
19042 * - Live ranges may not be coalesced across phi
19043 * functions. This creates a 2 headed live
19044 * range that cannot be sanely split.
19045 *
19046 * - phi functions (coalesced in initialize_live_ranges)
19047 * are handled as pre split live ranges so we will
19048 * never attempt to split them.
19049 */
19050 int coalesced;
19051 int i;
19052
19053 coalesced = 0;
19054 for(i = 0; i <= rstate->ranges; i++) {
19055 struct live_range *lr1;
19056 struct live_range_def *lrd1;
19057 lr1 = &rstate->lr[i];
19058 if (!lr1->defs) {
19059 continue;
19060 }
19061 lrd1 = live_range_end(state, lr1, 0);
19062 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19063 struct triple_set *set;
19064 if (lrd1->def->op != OP_COPY) {
19065 continue;
19066 }
19067 /* Skip copies that are the result of a live range split. */
19068 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19069 continue;
19070 }
19071 for(set = lrd1->def->use; set; set = set->next) {
19072 struct live_range_def *lrd2;
19073 struct live_range *lr2, *res;
19074
19075 lrd2 = &rstate->lrd[set->member->id];
19076
19077 /* Don't coalesce with instructions
19078 * that are the result of a live range
19079 * split.
19080 */
19081 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19082 continue;
19083 }
19084 lr2 = rstate->lrd[set->member->id].lr;
19085 if (lr1 == lr2) {
19086 continue;
19087 }
19088 if ((lr1->color != lr2->color) &&
19089 (lr1->color != REG_UNSET) &&
19090 (lr2->color != REG_UNSET)) {
19091 continue;
19092 }
19093 if ((lr1->classes & lr2->classes) == 0) {
19094 continue;
19095 }
19096
19097 if (interfere(rstate, lr1, lr2)) {
19098 continue;
19099 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019100
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019101 res = coalesce_ranges(state, rstate, lr1, lr2);
19102 coalesced += 1;
19103 if (res != lr1) {
19104 goto next;
19105 }
19106 }
19107 }
19108 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019109 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019110 }
19111 return coalesced;
19112}
19113
19114
Eric Biedermanf96a8102003-06-16 16:57:34 +000019115static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019116 struct reg_block *blocks, struct triple_reg_set *live,
19117 struct reg_block *rb, struct triple *ins, void *arg)
19118{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019119 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019120 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019121
19122 /* See if we have a mandatory coalesce operation between
19123 * a lhs and a rhs value. If so and the rhs value is also
19124 * alive then this triple needs to be pre copied. Otherwise
19125 * we would have two definitions in the same live range simultaneously
19126 * alive.
19127 */
Eric Biederman90089602004-05-28 14:11:54 +000019128 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019129 if ((zlhs == 0) && triple_is_def(state, ins)) {
19130 zlhs = 1;
19131 }
Eric Biederman90089602004-05-28 14:11:54 +000019132 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019133 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019134 struct reg_info linfo;
19135 linfo = arch_reg_lhs(state, ins, i);
19136 if (linfo.reg < MAX_REGISTERS) {
19137 continue;
19138 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019139 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019140 struct reg_info rinfo;
19141 struct triple *rhs;
19142 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019143 int found;
19144 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019145 rinfo = arch_reg_rhs(state, ins, j);
19146 if (rinfo.reg != linfo.reg) {
19147 continue;
19148 }
19149 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019150 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019151 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019152 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019153 }
19154 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019155 if (found) {
19156 struct triple *copy;
19157 copy = pre_copy(state, ins, j);
19158 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019159 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019160 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019161 }
19162 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019163 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019164}
19165
Eric Biedermand1ea5392003-06-28 06:49:45 +000019166static int correct_coalesce_conflicts(
19167 struct compile_state *state, struct reg_block *blocks)
19168{
19169 int conflicts;
19170 conflicts = 0;
Eric Biederman90089602004-05-28 14:11:54 +000019171 walk_variable_lifetimes(state, &state->bb, blocks,
19172 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019173 return conflicts;
19174}
19175
Eric Biedermanf96a8102003-06-16 16:57:34 +000019176static void replace_set_use(struct compile_state *state,
19177 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019178{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019179 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019180 for(set = head; set; set = set->next) {
19181 if (set->member == orig) {
19182 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019183 }
19184 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019185}
19186
Eric Biedermanf96a8102003-06-16 16:57:34 +000019187static void replace_block_use(struct compile_state *state,
19188 struct reg_block *blocks, struct triple *orig, struct triple *new)
19189{
19190 int i;
19191#warning "WISHLIST visit just those blocks that need it *"
Eric Biederman90089602004-05-28 14:11:54 +000019192 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019193 struct reg_block *rb;
19194 rb = &blocks[i];
19195 replace_set_use(state, rb->in, orig, new);
19196 replace_set_use(state, rb->out, orig, new);
19197 }
19198}
19199
19200static void color_instructions(struct compile_state *state)
19201{
19202 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019203 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019204 ins = first;
19205 do {
19206 if (triple_is_def(state, ins)) {
19207 struct reg_info info;
19208 info = find_lhs_color(state, ins, 0);
19209 if (info.reg >= MAX_REGISTERS) {
19210 info.reg = REG_UNSET;
19211 }
19212 SET_INFO(ins->id, info);
19213 }
19214 ins = ins->next;
19215 } while(ins != first);
19216}
19217
19218static struct reg_info read_lhs_color(
19219 struct compile_state *state, struct triple *ins, int index)
19220{
19221 struct reg_info info;
19222 if ((index == 0) && triple_is_def(state, ins)) {
19223 info.reg = ID_REG(ins->id);
19224 info.regcm = ID_REGCM(ins->id);
19225 }
Eric Biederman90089602004-05-28 14:11:54 +000019226 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019227 info = read_lhs_color(state, LHS(ins, index), 0);
19228 }
19229 else {
19230 internal_error(state, ins, "Bad lhs %d", index);
19231 info.reg = REG_UNSET;
19232 info.regcm = 0;
19233 }
19234 return info;
19235}
19236
19237static struct triple *resolve_tangle(
19238 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019239{
19240 struct reg_info info, uinfo;
19241 struct triple_set *set, *next;
19242 struct triple *copy;
19243
Eric Biedermanf96a8102003-06-16 16:57:34 +000019244#warning "WISHLIST recalculate all affected instructions colors"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019245 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019246 for(set = tangle->use; set; set = next) {
19247 struct triple *user;
19248 int i, zrhs;
19249 next = set->next;
19250 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019251 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019252 for(i = 0; i < zrhs; i++) {
19253 if (RHS(user, i) != tangle) {
19254 continue;
19255 }
19256 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019257 if (uinfo.reg == info.reg) {
19258 copy = pre_copy(state, user, i);
19259 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019260 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019261 }
19262 }
19263 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019264 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019265 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019266 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019267 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019268 copy = post_copy(state, tangle);
19269 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019270 linfo = find_lhs_color(state, copy, 0);
19271 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019272 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019273 info = find_lhs_color(state, tangle, 0);
19274 SET_INFO(tangle->id, info);
19275
19276 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019277}
19278
19279
Eric Biedermanf96a8102003-06-16 16:57:34 +000019280static void fix_tangles(struct compile_state *state,
19281 struct reg_block *blocks, struct triple_reg_set *live,
19282 struct reg_block *rb, struct triple *ins, void *arg)
19283{
Eric Biederman153ea352003-06-20 14:43:20 +000019284 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019285 struct triple *tangle;
19286 do {
19287 char used[MAX_REGISTERS];
19288 struct triple_reg_set *set;
19289 tangle = 0;
19290
19291 /* Find out which registers have multiple uses at this point */
19292 memset(used, 0, sizeof(used));
19293 for(set = live; set; set = set->next) {
19294 struct reg_info info;
19295 info = read_lhs_color(state, set->member, 0);
19296 if (info.reg == REG_UNSET) {
19297 continue;
19298 }
19299 reg_inc_used(state, used, info.reg);
19300 }
19301
19302 /* Now find the least dominated definition of a register in
19303 * conflict I have seen so far.
19304 */
19305 for(set = live; set; set = set->next) {
19306 struct reg_info info;
19307 info = read_lhs_color(state, set->member, 0);
19308 if (used[info.reg] < 2) {
19309 continue;
19310 }
Eric Biederman153ea352003-06-20 14:43:20 +000019311 /* Changing copies that feed into phi functions
19312 * is incorrect.
19313 */
19314 if (set->member->use &&
19315 (set->member->use->member->op == OP_PHI)) {
19316 continue;
19317 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019318 if (!tangle || tdominates(state, set->member, tangle)) {
19319 tangle = set->member;
19320 }
19321 }
19322 /* If I have found a tangle resolve it */
19323 if (tangle) {
19324 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019325 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019326 post_copy = resolve_tangle(state, tangle);
19327 if (post_copy) {
19328 replace_block_use(state, blocks, tangle, post_copy);
19329 }
19330 if (post_copy && (tangle != ins)) {
19331 replace_set_use(state, live, tangle, post_copy);
19332 }
19333 }
19334 } while(tangle);
19335 return;
19336}
19337
Eric Biederman153ea352003-06-20 14:43:20 +000019338static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019339 struct compile_state *state, struct reg_block *blocks)
19340{
Eric Biederman153ea352003-06-20 14:43:20 +000019341 int tangles;
19342 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019343 color_instructions(state);
Eric Biederman90089602004-05-28 14:11:54 +000019344 walk_variable_lifetimes(state, &state->bb, blocks,
19345 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019346 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019347}
19348
Eric Biedermand1ea5392003-06-28 06:49:45 +000019349
19350static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19351static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19352
19353struct triple *find_constrained_def(
19354 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019355{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019356 struct live_range_def *lrd, *lrd_next;
19357 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019358 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019359 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019360 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019361
19362 lrd = lrd_next;
19363 lrd_next = lrd->next;
19364
Eric Biedermand1ea5392003-06-28 06:49:45 +000019365 regcm = arch_type_to_regcm(state, lrd->def->type);
19366 info = find_lhs_color(state, lrd->def, 0);
19367 regcm = arch_regcm_reg_normalize(state, regcm);
19368 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019369 /* If the 2 register class masks are equal then
19370 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019371 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019372 if (regcm == info.regcm) {
19373 continue;
19374 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019375
Eric Biederman5ade04a2003-10-22 04:03:46 +000019376 /* If there is just one use.
19377 * That use cannot accept a larger register class.
19378 * There are no intervening definitions except
19379 * definitions that feed into that use.
19380 * Then a triple is not constrained.
19381 * FIXME handle this case!
19382 */
19383#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
19384
19385
Eric Biedermand1ea5392003-06-28 06:49:45 +000019386 /* Of the constrained live ranges deal with the
19387 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019388 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019389 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019390 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019391 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019392 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019393 if (!constrained ||
19394 tdominates(state, lrd->def, constrained))
19395 {
19396 constrained = lrd->def;
19397 }
19398 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019399 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019400}
19401
Eric Biedermand1ea5392003-06-28 06:49:45 +000019402static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019403 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019404 struct live_range *range)
19405{
19406 /* Walk through the edges in conflict and our current live
19407 * range, and find definitions that are more severly constrained
19408 * than they type of data they contain require.
19409 *
19410 * Then pick one of those ranges and relax the constraints.
19411 */
19412 struct live_range_edge *edge;
19413 struct triple *constrained;
19414
19415 constrained = 0;
19416 for(edge = range->edges; edge; edge = edge->next) {
19417 constrained = find_constrained_def(state, edge->node, constrained);
19418 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019419#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Eric Biedermand1ea5392003-06-28 06:49:45 +000019420 if (!constrained) {
19421 constrained = find_constrained_def(state, range, constrained);
19422 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019423
19424 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019425 fprintf(state->errout, "constrained: ");
19426 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019427 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019428 if (constrained) {
19429 ids_from_rstate(state, rstate);
19430 cleanup_rstate(state, rstate);
19431 resolve_tangle(state, constrained);
19432 }
19433 return !!constrained;
19434}
19435
19436static int split_ranges(
19437 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019438 char *used, struct live_range *range)
19439{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019440 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019441 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019442 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019443 rstate->passes, tops(range->defs->def->op), range->defs->def);
19444 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019445 if ((range->color == REG_UNNEEDED) ||
19446 (rstate->passes >= rstate->max_passes)) {
19447 return 0;
19448 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019449 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019450
Eric Biedermand1ea5392003-06-28 06:49:45 +000019451 /* Ideally I would split the live range that will not be used
19452 * for the longest period of time in hopes that this will
19453 * (a) allow me to spill a register or
19454 * (b) allow me to place a value in another register.
19455 *
19456 * So far I don't have a test case for this, the resolving
19457 * of mandatory constraints has solved all of my
19458 * know issues. So I have choosen not to write any
19459 * code until I cat get a better feel for cases where
19460 * it would be useful to have.
19461 *
19462 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019463#warning "WISHLIST implement live range splitting..."
Eric Biederman5ade04a2003-10-22 04:03:46 +000019464
19465 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019466 FILE *fp = state->errout;
19467 print_interference_blocks(state, rstate, fp, 0);
19468 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019469 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019470 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019471}
19472
Eric Biederman5ade04a2003-10-22 04:03:46 +000019473static FILE *cgdebug_fp(struct compile_state *state)
19474{
19475 FILE *fp;
19476 fp = 0;
19477 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019478 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019479 }
19480 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019481 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019482 }
19483 return fp;
19484}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019485
Eric Biederman5ade04a2003-10-22 04:03:46 +000019486static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19487{
19488 FILE *fp;
19489 fp = cgdebug_fp(state);
19490 if (fp) {
19491 va_list args;
19492 va_start(args, fmt);
19493 vfprintf(fp, fmt, args);
19494 va_end(args);
19495 }
19496}
19497
19498static void cgdebug_flush(struct compile_state *state)
19499{
19500 FILE *fp;
19501 fp = cgdebug_fp(state);
19502 if (fp) {
19503 fflush(fp);
19504 }
19505}
19506
19507static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19508{
19509 FILE *fp;
19510 fp = cgdebug_fp(state);
19511 if (fp) {
19512 loc(fp, state, ins);
19513 }
19514}
19515
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019516static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019517 struct reg_state *rstate, struct live_range *range)
19518{
19519 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019520 struct live_range_def *lrd;
19521 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019522 struct live_range_edge *edge;
19523 char used[MAX_REGISTERS];
19524 struct triple **expr;
19525
Eric Biedermanb138ac82003-04-22 18:44:01 +000019526 /* Instead of doing just the trivial color select here I try
19527 * a few extra things because a good color selection will help reduce
19528 * copies.
19529 */
19530
19531 /* Find the registers currently in use */
19532 memset(used, 0, sizeof(used));
19533 for(edge = range->edges; edge; edge = edge->next) {
19534 if (edge->node->color == REG_UNSET) {
19535 continue;
19536 }
19537 reg_fill_used(state, used, edge->node->color);
19538 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019539
19540 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019541 int i;
19542 i = 0;
19543 for(edge = range->edges; edge; edge = edge->next) {
19544 i++;
19545 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019546 cgdebug_printf(state, "\n%s edges: %d",
19547 tops(range->defs->def->op), i);
19548 cgdebug_loc(state, range->defs->def);
19549 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019550 for(i = 0; i < MAX_REGISTERS; i++) {
19551 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019552 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019553 arch_reg_str(i));
19554 }
19555 }
19556 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019557
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019558 /* If a color is already assigned see if it will work */
19559 if (range->color != REG_UNSET) {
19560 struct live_range_def *lrd;
19561 if (!used[range->color]) {
19562 return 1;
19563 }
19564 for(edge = range->edges; edge; edge = edge->next) {
19565 if (edge->node->color != range->color) {
19566 continue;
19567 }
19568 warning(state, edge->node->defs->def, "edge: ");
19569 lrd = edge->node->defs;
19570 do {
19571 warning(state, lrd->def, " %p %s",
19572 lrd->def, tops(lrd->def->op));
19573 lrd = lrd->next;
19574 } while(lrd != edge->node->defs);
19575 }
19576 lrd = range->defs;
19577 warning(state, range->defs->def, "def: ");
19578 do {
19579 warning(state, lrd->def, " %p %s",
19580 lrd->def, tops(lrd->def->op));
19581 lrd = lrd->next;
19582 } while(lrd != range->defs);
19583 internal_error(state, range->defs->def,
19584 "live range with already used color %s",
19585 arch_reg_str(range->color));
19586 }
19587
Eric Biedermanb138ac82003-04-22 18:44:01 +000019588 /* If I feed into an expression reuse it's color.
19589 * This should help remove copies in the case of 2 register instructions
19590 * and phi functions.
19591 */
19592 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019593 lrd = live_range_end(state, range, 0);
19594 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19595 entry = lrd->def->use;
19596 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19597 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019598 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019599 insd = &rstate->lrd[entry->member->id];
19600 if (insd->lr->defs == 0) {
19601 continue;
19602 }
19603 if (!phi && (insd->def->op == OP_PHI) &&
19604 !interfere(rstate, range, insd->lr)) {
19605 phi = insd;
19606 }
Eric Biederman530b5192003-07-01 10:05:30 +000019607 if (insd->lr->color == REG_UNSET) {
19608 continue;
19609 }
19610 regcm = insd->lr->classes;
19611 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019612 (used[insd->lr->color])) {
19613 continue;
19614 }
19615 if (interfere(rstate, range, insd->lr)) {
19616 continue;
19617 }
19618 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019619 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019620 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019621 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019622 * of something else that feeds into the phi function.
19623 */
19624 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019625 if (phi->lr->color != REG_UNSET) {
19626 if (used[phi->lr->color]) {
19627 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019628 }
19629 }
19630 else {
19631 expr = triple_rhs(state, phi->def, 0);
19632 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19633 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019634 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019635 if (!*expr) {
19636 continue;
19637 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019638 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019639 if (lr->color == REG_UNSET) {
19640 continue;
19641 }
19642 regcm = lr->classes;
19643 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019644 (used[lr->color])) {
19645 continue;
19646 }
19647 if (interfere(rstate, range, lr)) {
19648 continue;
19649 }
19650 range->color = lr->color;
19651 }
19652 }
19653 }
19654 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019655 lrd = live_range_head(state, range, 0);
19656 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19657 expr = triple_rhs(state, lrd->def, 0);
19658 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019659 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019660 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019661 if (!*expr) {
19662 continue;
19663 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019664 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019665 if (lr->color == REG_UNSET) {
19666 continue;
19667 }
19668 regcm = lr->classes;
19669 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019670 (used[lr->color])) {
19671 continue;
19672 }
19673 if (interfere(rstate, range, lr)) {
19674 continue;
19675 }
19676 range->color = lr->color;
19677 break;
19678 }
19679 }
19680 /* If I have not opportunitically picked a useful color
19681 * pick the first color that is free.
19682 */
19683 if (range->color == REG_UNSET) {
19684 range->color =
19685 arch_select_free_register(state, used, range->classes);
19686 }
19687 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019688 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019689 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019690 if (split_ranges(state, rstate, used, range)) {
19691 return 0;
19692 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019693 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019694 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019695 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019696 lrd = edge->node->defs;
19697 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019698 warning(state, lrd->def, " %s %p",
19699 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019700 lrd = lrd->next;
19701 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019702 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019703 warning(state, range->defs->def, "range: ");
19704 lrd = range->defs;
19705 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019706 warning(state, lrd->def, " %s %p",
19707 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019708 lrd = lrd->next;
19709 } while(lrd != range->defs);
19710
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019711 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019712 range->classes);
19713 for(i = 0; i < MAX_REGISTERS; i++) {
19714 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019715 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019716 arch_reg_str(i));
19717 }
19718 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019719 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019720 }
Eric Biederman530b5192003-07-01 10:05:30 +000019721 range->classes &= arch_reg_regcm(state, range->color);
19722 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019723 internal_error(state, range->defs->def, "select_free_color did not?");
19724 }
19725 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019726}
19727
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019728static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019729{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019730 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019731 struct live_range_edge *edge;
19732 struct live_range *range;
19733 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019734 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019735 range = rstate->low;
19736 if (*range->group_prev != range) {
19737 internal_error(state, 0, "lo: *prev != range?");
19738 }
19739 *range->group_prev = range->group_next;
19740 if (range->group_next) {
19741 range->group_next->group_prev = range->group_prev;
19742 }
19743 if (&range->group_next == rstate->low_tail) {
19744 rstate->low_tail = range->group_prev;
19745 }
19746 if (rstate->low == range) {
19747 internal_error(state, 0, "low: next != prev?");
19748 }
19749 }
19750 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019751 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019752 range = rstate->high;
19753 if (*range->group_prev != range) {
19754 internal_error(state, 0, "hi: *prev != range?");
19755 }
19756 *range->group_prev = range->group_next;
19757 if (range->group_next) {
19758 range->group_next->group_prev = range->group_prev;
19759 }
19760 if (&range->group_next == rstate->high_tail) {
19761 rstate->high_tail = range->group_prev;
19762 }
19763 if (rstate->high == range) {
19764 internal_error(state, 0, "high: next != prev?");
19765 }
19766 }
19767 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019768 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019769 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019770 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019771 range->group_prev = 0;
19772 for(edge = range->edges; edge; edge = edge->next) {
19773 struct live_range *node;
19774 node = edge->node;
19775 /* Move nodes from the high to the low list */
19776 if (node->group_prev && (node->color == REG_UNSET) &&
19777 (node->degree == regc_max_size(state, node->classes))) {
19778 if (*node->group_prev != node) {
19779 internal_error(state, 0, "move: *prev != node?");
19780 }
19781 *node->group_prev = node->group_next;
19782 if (node->group_next) {
19783 node->group_next->group_prev = node->group_prev;
19784 }
19785 if (&node->group_next == rstate->high_tail) {
19786 rstate->high_tail = node->group_prev;
19787 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019788 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019789 node->group_prev = rstate->low_tail;
19790 node->group_next = 0;
19791 *rstate->low_tail = node;
19792 rstate->low_tail = &node->group_next;
19793 if (*node->group_prev != node) {
19794 internal_error(state, 0, "move2: *prev != node?");
19795 }
19796 }
19797 node->degree -= 1;
19798 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019799 colored = color_graph(state, rstate);
19800 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019801 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019802 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019803 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019804 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019805 if (colored) {
19806 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19807 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019808 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019809 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019810}
19811
Eric Biedermana96d6a92003-05-13 20:45:19 +000019812static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19813{
19814 struct live_range *lr;
19815 struct live_range_edge *edge;
19816 struct triple *ins, *first;
19817 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019818 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019819 ins = first;
19820 do {
19821 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019822 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019823 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019824 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019825 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019826 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019827 if (lr->color == REG_UNSET) {
19828 internal_error(state, ins,
19829 "triple without a color");
19830 }
19831 /* Find the registers used by the edges */
19832 memset(used, 0, sizeof(used));
19833 for(edge = lr->edges; edge; edge = edge->next) {
19834 if (edge->node->color == REG_UNSET) {
19835 internal_error(state, 0,
19836 "live range without a color");
19837 }
19838 reg_fill_used(state, used, edge->node->color);
19839 }
19840 if (used[lr->color]) {
19841 internal_error(state, ins,
19842 "triple with already used color");
19843 }
19844 }
19845 ins = ins->next;
19846 } while(ins != first);
19847}
19848
Eric Biedermanb138ac82003-04-22 18:44:01 +000019849static void color_triples(struct compile_state *state, struct reg_state *rstate)
19850{
Eric Biederman90089602004-05-28 14:11:54 +000019851 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019852 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019853 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019854 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019855 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019856 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019857 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019858 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019859 "triple without a live range");
19860 }
Eric Biederman90089602004-05-28 14:11:54 +000019861 lrd = &rstate->lrd[ins->id];
19862 lr = lrd->lr;
19863 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019864 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019865 ins = ins->next;
19866 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019867}
19868
Eric Biedermanb138ac82003-04-22 18:44:01 +000019869static struct live_range *merge_sort_lr(
19870 struct live_range *first, struct live_range *last)
19871{
19872 struct live_range *mid, *join, **join_tail, *pick;
19873 size_t size;
19874 size = (last - first) + 1;
19875 if (size >= 2) {
19876 mid = first + size/2;
19877 first = merge_sort_lr(first, mid -1);
19878 mid = merge_sort_lr(mid, last);
19879
19880 join = 0;
19881 join_tail = &join;
19882 /* merge the two lists */
19883 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019884 if ((first->degree < mid->degree) ||
19885 ((first->degree == mid->degree) &&
19886 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019887 pick = first;
19888 first = first->group_next;
19889 if (first) {
19890 first->group_prev = 0;
19891 }
19892 }
19893 else {
19894 pick = mid;
19895 mid = mid->group_next;
19896 if (mid) {
19897 mid->group_prev = 0;
19898 }
19899 }
19900 pick->group_next = 0;
19901 pick->group_prev = join_tail;
19902 *join_tail = pick;
19903 join_tail = &pick->group_next;
19904 }
19905 /* Splice the remaining list */
19906 pick = (first)? first : mid;
19907 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019908 if (pick) {
19909 pick->group_prev = join_tail;
19910 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019911 }
19912 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019913 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019914 first = 0;
19915 }
19916 join = first;
19917 }
19918 return join;
19919}
19920
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019921static void ids_from_rstate(struct compile_state *state,
19922 struct reg_state *rstate)
19923{
19924 struct triple *ins, *first;
19925 if (!rstate->defs) {
19926 return;
19927 }
19928 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019929 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000019930 FILE *fp = state->dbgout;
19931 print_interference_blocks(state, rstate, fp, 0);
19932 print_control_flow(state, &state->bb);
19933 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019934 }
Eric Biederman83b991a2003-10-11 06:20:25 +000019935 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019936 ins = first;
19937 do {
19938 if (ins->id) {
19939 struct live_range_def *lrd;
19940 lrd = &rstate->lrd[ins->id];
19941 ins->id = lrd->orig_id;
19942 }
19943 ins = ins->next;
19944 } while(ins != first);
19945}
19946
19947static void cleanup_live_edges(struct reg_state *rstate)
19948{
19949 int i;
19950 /* Free the edges on each node */
19951 for(i = 1; i <= rstate->ranges; i++) {
19952 remove_live_edges(rstate, &rstate->lr[i]);
19953 }
19954}
19955
19956static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
19957{
19958 cleanup_live_edges(rstate);
19959 xfree(rstate->lrd);
19960 xfree(rstate->lr);
19961
19962 /* Free the variable lifetime information */
19963 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000019964 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019965 }
19966 rstate->defs = 0;
19967 rstate->ranges = 0;
19968 rstate->lrd = 0;
19969 rstate->lr = 0;
19970 rstate->blocks = 0;
19971}
19972
Eric Biederman153ea352003-06-20 14:43:20 +000019973static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019974static void allocate_registers(struct compile_state *state)
19975{
19976 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019977 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019978
19979 /* Clear out the reg_state */
19980 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000019981 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019982
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019983 do {
19984 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019985 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000019986 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019987 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019988
Eric Biederman5ade04a2003-10-22 04:03:46 +000019989 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019990 FILE *fp = state->errout;
19991 fprintf(fp, "pass: %d\n", rstate.passes);
19992 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019993 }
Eric Biederman153ea352003-06-20 14:43:20 +000019994
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019995 /* Restore ids */
19996 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019997
Eric Biedermanf96a8102003-06-16 16:57:34 +000019998 /* Cleanup the temporary data structures */
19999 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020000
Eric Biedermanf96a8102003-06-16 16:57:34 +000020001 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020002 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020003
Eric Biedermanf96a8102003-06-16 16:57:34 +000020004 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000020005 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020006
Eric Biederman153ea352003-06-20 14:43:20 +000020007 /* Fix two simultaneous uses of the same register.
20008 * In a few pathlogical cases a partial untangle moves
20009 * the tangle to a part of the graph we won't revisit.
20010 * So we keep looping until we have no more tangle fixes
20011 * to apply.
20012 */
20013 do {
20014 tangles = correct_tangles(state, rstate.blocks);
20015 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020016
Eric Biederman5ade04a2003-10-22 04:03:46 +000020017
Eric Biederman90089602004-05-28 14:11:54 +000020018 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020019 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020020
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020021 /* Allocate and initialize the live ranges */
20022 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020023
Eric Biederman90089602004-05-28 14:11:54 +000020024 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020025 * buys me nothing. The code is left this way in case
20026 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020027 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020028 */
20029 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020030 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020031 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020032 }
20033
Eric Biederman153ea352003-06-20 14:43:20 +000020034 /* Remove any previous live edge calculations */
20035 cleanup_live_edges(&rstate);
20036
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020037 /* Compute the interference graph */
20038 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020039 state, &state->bb, rstate.blocks,
20040 graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020041
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020042 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020043 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020044 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020045 printf("\nlive variables by instruction\n");
20046 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020047 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020048 print_interference_ins, &rstate);
20049 }
20050
20051 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020052
Eric Biederman5ade04a2003-10-22 04:03:46 +000020053 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020054 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020055 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020056 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020057
Eric Biederman83b991a2003-10-11 06:20:25 +000020058#if DEBUG_CONSISTENCY > 1
20059# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020060 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020061# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020062 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020063 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020064 state, &state->bb, rstate.blocks,
20065 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020066# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020067 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020068#endif
20069#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020070
20071 /* Build the groups low and high. But with the nodes
20072 * first sorted by degree order.
20073 */
20074 rstate.low_tail = &rstate.low;
20075 rstate.high_tail = &rstate.high;
20076 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20077 if (rstate.high) {
20078 rstate.high->group_prev = &rstate.high;
20079 }
20080 for(point = &rstate.high; *point; point = &(*point)->group_next)
20081 ;
20082 rstate.high_tail = point;
20083 /* Walk through the high list and move everything that needs
20084 * to be onto low.
20085 */
20086 for(point = &rstate.high; *point; point = next) {
20087 struct live_range *range;
20088 next = &(*point)->group_next;
20089 range = *point;
20090
20091 /* If it has a low degree or it already has a color
20092 * place the node in low.
20093 */
20094 if ((range->degree < regc_max_size(state, range->classes)) ||
20095 (range->color != REG_UNSET)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020096 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020097 range - rstate.lr, range->degree,
20098 (range->color != REG_UNSET) ? " (colored)": "");
20099 *range->group_prev = range->group_next;
20100 if (range->group_next) {
20101 range->group_next->group_prev = range->group_prev;
20102 }
20103 if (&range->group_next == rstate.high_tail) {
20104 rstate.high_tail = range->group_prev;
20105 }
20106 range->group_prev = rstate.low_tail;
20107 range->group_next = 0;
20108 *rstate.low_tail = range;
20109 rstate.low_tail = &range->group_next;
20110 next = point;
20111 }
20112 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020113 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020114 range - rstate.lr, range->degree,
20115 (range->color != REG_UNSET) ? " (colored)": "");
20116 }
20117 }
20118 /* Color the live_ranges */
20119 colored = color_graph(state, &rstate);
20120 rstate.passes++;
20121 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020122
Eric Biedermana96d6a92003-05-13 20:45:19 +000020123 /* Verify the graph was properly colored */
20124 verify_colors(state, &rstate);
20125
Eric Biedermanb138ac82003-04-22 18:44:01 +000020126 /* Move the colors from the graph to the triples */
20127 color_triples(state, &rstate);
20128
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020129 /* Cleanup the temporary data structures */
20130 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020131
20132 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020133 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020134}
20135
20136/* Sparce Conditional Constant Propogation
20137 * =========================================
20138 */
20139struct ssa_edge;
20140struct flow_block;
20141struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020142 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020143 struct triple *def;
20144 struct ssa_edge *out;
20145 struct flow_block *fblock;
20146 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020147 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020148 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020149 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020150 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020151};
20152struct ssa_edge {
20153 struct lattice_node *src;
20154 struct lattice_node *dst;
20155 struct ssa_edge *work_next;
20156 struct ssa_edge *work_prev;
20157 struct ssa_edge *out_next;
20158};
20159struct flow_edge {
20160 struct flow_block *src;
20161 struct flow_block *dst;
20162 struct flow_edge *work_next;
20163 struct flow_edge *work_prev;
20164 struct flow_edge *in_next;
20165 struct flow_edge *out_next;
20166 int executable;
20167};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020168#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020169struct flow_block {
20170 struct block *block;
20171 struct flow_edge *in;
20172 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020173 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020174};
20175
20176struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020177 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020178 struct lattice_node *lattice;
20179 struct ssa_edge *ssa_edges;
20180 struct flow_block *flow_blocks;
20181 struct flow_edge *flow_work_list;
20182 struct ssa_edge *ssa_work_list;
20183};
20184
20185
Eric Biederman90089602004-05-28 14:11:54 +000020186static int is_scc_const(struct compile_state *state, struct triple *ins)
20187{
20188 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20189}
20190
20191static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20192{
20193 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20194}
20195
20196static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20197{
20198 return is_scc_const(state, lnode->val);
20199}
20200
20201static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20202{
20203 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20204}
20205
20206
20207
20208
Eric Biedermanb138ac82003-04-22 18:44:01 +000020209static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20210 struct flow_edge *fedge)
20211{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020212 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020213 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020214 fedge,
20215 fedge->src->block?fedge->src->block->last->id: 0,
20216 fedge->dst->block?fedge->dst->block->first->id: 0);
20217 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020218 if ((fedge == scc->flow_work_list) ||
20219 (fedge->work_next != fedge) ||
20220 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020221
20222 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020223 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020224 fedge);
20225 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020226 return;
20227 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020228 if (!scc->flow_work_list) {
20229 scc->flow_work_list = fedge;
20230 fedge->work_next = fedge->work_prev = fedge;
20231 }
20232 else {
20233 struct flow_edge *ftail;
20234 ftail = scc->flow_work_list->work_prev;
20235 fedge->work_next = ftail->work_next;
20236 fedge->work_prev = ftail;
20237 fedge->work_next->work_prev = fedge;
20238 fedge->work_prev->work_next = fedge;
20239 }
20240}
20241
20242static struct flow_edge *scc_next_fedge(
20243 struct compile_state *state, struct scc_state *scc)
20244{
20245 struct flow_edge *fedge;
20246 fedge = scc->flow_work_list;
20247 if (fedge) {
20248 fedge->work_next->work_prev = fedge->work_prev;
20249 fedge->work_prev->work_next = fedge->work_next;
20250 if (fedge->work_next != fedge) {
20251 scc->flow_work_list = fedge->work_next;
20252 } else {
20253 scc->flow_work_list = 0;
20254 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020255 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020256 }
20257 return fedge;
20258}
20259
20260static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20261 struct ssa_edge *sedge)
20262{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020263 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020264 fprintf(state->errout, "adding sedge: %5d (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020265 sedge - scc->ssa_edges,
20266 sedge->src->def->id,
20267 sedge->dst->def->id);
20268 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020269 if ((sedge == scc->ssa_work_list) ||
20270 (sedge->work_next != sedge) ||
20271 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020272
20273 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020274 fprintf(state->errout, "dupped sedge: %5d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020275 sedge - scc->ssa_edges);
20276 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020277 return;
20278 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020279 if (!scc->ssa_work_list) {
20280 scc->ssa_work_list = sedge;
20281 sedge->work_next = sedge->work_prev = sedge;
20282 }
20283 else {
20284 struct ssa_edge *stail;
20285 stail = scc->ssa_work_list->work_prev;
20286 sedge->work_next = stail->work_next;
20287 sedge->work_prev = stail;
20288 sedge->work_next->work_prev = sedge;
20289 sedge->work_prev->work_next = sedge;
20290 }
20291}
20292
20293static struct ssa_edge *scc_next_sedge(
20294 struct compile_state *state, struct scc_state *scc)
20295{
20296 struct ssa_edge *sedge;
20297 sedge = scc->ssa_work_list;
20298 if (sedge) {
20299 sedge->work_next->work_prev = sedge->work_prev;
20300 sedge->work_prev->work_next = sedge->work_next;
20301 if (sedge->work_next != sedge) {
20302 scc->ssa_work_list = sedge->work_next;
20303 } else {
20304 scc->ssa_work_list = 0;
20305 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020306 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020307 }
20308 return sedge;
20309}
20310
Eric Biederman90089602004-05-28 14:11:54 +000020311
Eric Biedermanb138ac82003-04-22 18:44:01 +000020312static void initialize_scc_state(
20313 struct compile_state *state, struct scc_state *scc)
20314{
20315 int ins_count, ssa_edge_count;
20316 int ins_index, ssa_edge_index, fblock_index;
20317 struct triple *first, *ins;
20318 struct block *block;
20319 struct flow_block *fblock;
20320
20321 memset(scc, 0, sizeof(*scc));
20322
20323 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020324 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020325 ins = first;
20326 ins_count = ssa_edge_count = 0;
20327 do {
20328 struct triple_set *edge;
20329 ins_count += 1;
20330 for(edge = ins->use; edge; edge = edge->next) {
20331 ssa_edge_count++;
20332 }
20333 ins = ins->next;
20334 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020335 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020336 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20337 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020338 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020339 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020340 scc->lattice =
20341 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20342 scc->ssa_edges =
20343 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20344 scc->flow_blocks =
Eric Biederman90089602004-05-28 14:11:54 +000020345 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020346 "flow_blocks");
20347
20348 /* Initialize pass one collect up the nodes */
20349 fblock = 0;
20350 block = 0;
20351 ins_index = ssa_edge_index = fblock_index = 0;
20352 ins = first;
20353 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020354 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20355 block = ins->u.block;
20356 if (!block) {
20357 internal_error(state, ins, "label without block");
20358 }
20359 fblock_index += 1;
20360 block->vertex = fblock_index;
20361 fblock = &scc->flow_blocks[fblock_index];
20362 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020363 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20364 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020365 }
20366 {
20367 struct lattice_node *lnode;
20368 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020369 lnode = &scc->lattice[ins_index];
20370 lnode->def = ins;
20371 lnode->out = 0;
20372 lnode->fblock = fblock;
20373 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020374 if (lnode->val->op == OP_UNKNOWNVAL) {
20375 lnode->val = 0; /* LATTICE LOW by definition */
20376 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020377 lnode->old_id = ins->id;
20378 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020379 }
20380 ins = ins->next;
20381 } while(ins != first);
20382 /* Initialize pass two collect up the edges */
20383 block = 0;
20384 fblock = 0;
20385 ins = first;
20386 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020387 {
20388 struct triple_set *edge;
20389 struct ssa_edge **stail;
20390 struct lattice_node *lnode;
20391 lnode = &scc->lattice[ins->id];
20392 lnode->out = 0;
20393 stail = &lnode->out;
20394 for(edge = ins->use; edge; edge = edge->next) {
20395 struct ssa_edge *sedge;
20396 ssa_edge_index += 1;
20397 sedge = &scc->ssa_edges[ssa_edge_index];
20398 *stail = sedge;
20399 stail = &sedge->out_next;
20400 sedge->src = lnode;
20401 sedge->dst = &scc->lattice[edge->member->id];
20402 sedge->work_next = sedge->work_prev = sedge;
20403 sedge->out_next = 0;
20404 }
20405 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020406 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20407 struct flow_edge *fedge, **ftail;
20408 struct block_set *bedge;
20409 block = ins->u.block;
20410 fblock = &scc->flow_blocks[block->vertex];
20411 fblock->in = 0;
20412 fblock->out = 0;
20413 ftail = &fblock->out;
20414
20415 fedge = fblock->edges;
20416 bedge = block->edges;
20417 for(; bedge; bedge = bedge->next, fedge++) {
20418 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20419 if (fedge->dst->block != bedge->member) {
20420 internal_error(state, 0, "block mismatch");
20421 }
20422 *ftail = fedge;
20423 ftail = &fedge->out_next;
20424 fedge->out_next = 0;
20425 }
20426 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20427 fedge->src = fblock;
20428 fedge->work_next = fedge->work_prev = fedge;
20429 fedge->executable = 0;
20430 }
20431 }
20432 ins = ins->next;
20433 } while (ins != first);
20434 block = 0;
20435 fblock = 0;
20436 ins = first;
20437 do {
20438 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20439 struct flow_edge **ftail;
20440 struct block_set *bedge;
20441 block = ins->u.block;
20442 fblock = &scc->flow_blocks[block->vertex];
20443 ftail = &fblock->in;
20444 for(bedge = block->use; bedge; bedge = bedge->next) {
20445 struct block *src_block;
20446 struct flow_block *sfblock;
20447 struct flow_edge *sfedge;
20448 src_block = bedge->member;
20449 sfblock = &scc->flow_blocks[src_block->vertex];
20450 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20451 if (sfedge->dst == fblock) {
20452 break;
20453 }
20454 }
20455 if (!sfedge) {
20456 internal_error(state, 0, "edge mismatch");
20457 }
20458 *ftail = sfedge;
20459 ftail = &sfedge->in_next;
20460 sfedge->in_next = 0;
20461 }
20462 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020463 ins = ins->next;
20464 } while(ins != first);
20465 /* Setup a dummy block 0 as a node above the start node */
20466 {
20467 struct flow_block *fblock, *dst;
20468 struct flow_edge *fedge;
20469 fblock = &scc->flow_blocks[0];
20470 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020471 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020472 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020473 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020474 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020475 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020476 fedge->src = fblock;
20477 fedge->dst = dst;
20478 fedge->work_next = fedge;
20479 fedge->work_prev = fedge;
20480 fedge->in_next = fedge->dst->in;
20481 fedge->out_next = 0;
20482 fedge->executable = 0;
20483 fedge->dst->in = fedge;
20484
20485 /* Initialize the work lists */
20486 scc->flow_work_list = 0;
20487 scc->ssa_work_list = 0;
20488 scc_add_fedge(state, scc, fedge);
20489 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020490 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020491 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020492 ins_index, ssa_edge_index, fblock_index);
20493 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020494}
20495
20496
20497static void free_scc_state(
20498 struct compile_state *state, struct scc_state *scc)
20499{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020500 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020501 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020502 struct flow_block *fblock;
20503 fblock = &scc->flow_blocks[i];
20504 if (fblock->edges) {
20505 xfree(fblock->edges);
20506 fblock->edges = 0;
20507 }
20508 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020509 xfree(scc->flow_blocks);
20510 xfree(scc->ssa_edges);
20511 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020512
Eric Biedermanb138ac82003-04-22 18:44:01 +000020513}
20514
20515static struct lattice_node *triple_to_lattice(
20516 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20517{
20518 if (ins->id <= 0) {
20519 internal_error(state, ins, "bad id");
20520 }
20521 return &scc->lattice[ins->id];
20522}
20523
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020524static struct triple *preserve_lval(
20525 struct compile_state *state, struct lattice_node *lnode)
20526{
20527 struct triple *old;
20528 /* Preserve the original value */
20529 if (lnode->val) {
20530 old = dup_triple(state, lnode->val);
20531 if (lnode->val != lnode->def) {
20532 xfree(lnode->val);
20533 }
20534 lnode->val = 0;
20535 } else {
20536 old = 0;
20537 }
20538 return old;
20539}
20540
20541static int lval_changed(struct compile_state *state,
20542 struct triple *old, struct lattice_node *lnode)
20543{
20544 int changed;
20545 /* See if the lattice value has changed */
20546 changed = 1;
20547 if (!old && !lnode->val) {
20548 changed = 0;
20549 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020550 if (changed &&
20551 lnode->val && old &&
20552 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020553 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020554 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20555 changed = 0;
20556 }
20557 if (old) {
20558 xfree(old);
20559 }
20560 return changed;
20561
20562}
20563
Eric Biederman5ade04a2003-10-22 04:03:46 +000020564static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020565 struct compile_state *state, struct scc_state *scc,
20566 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020567{
Eric Biederman90089602004-05-28 14:11:54 +000020568 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20569 display_triple_changes(state->errout, lnode->val, lnode->def);
20570 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020571 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020572 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020573 struct triple *val, **expr;
20574 val = lnode->val? lnode->val : lnode->def;
20575 fprintf(fp, "%p %s %3d %10s (",
20576 lnode->def,
20577 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20578 lnode->def->id,
20579 tops(lnode->def->op));
20580 expr = triple_rhs(state, lnode->def, 0);
20581 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20582 if (*expr) {
20583 fprintf(fp, " %d", (*expr)->id);
20584 }
20585 }
20586 if (val->op == OP_INTCONST) {
20587 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20588 }
20589 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020590 (is_lattice_hi(state, lnode)? "hi":
20591 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020592 changed? "changed" : ""
20593 );
20594 }
20595}
20596
Eric Biedermanb138ac82003-04-22 18:44:01 +000020597static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20598 struct lattice_node *lnode)
20599{
20600 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020601 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020602 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020603 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020604
20605 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020606 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020607
Eric Biedermanb138ac82003-04-22 18:44:01 +000020608 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020609 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020610 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020611 scratch->next = scratch;
20612 scratch->prev = scratch;
20613 scratch->use = 0;
20614
Eric Biederman90089602004-05-28 14:11:54 +000020615 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020616 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020617 dexpr = &lnode->def->param[i];
20618 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020619 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020620 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20621 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020622 *dexpr) {
20623 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020624 tmp = triple_to_lattice(state, scc, *dexpr);
20625 *vexpr = (tmp->val)? tmp->val : tmp->def;
20626 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020627 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020628 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020629 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020630 }
20631 /* Recompute the value */
20632#warning "FIXME see if simplify does anything bad"
20633 /* So far it looks like only the strength reduction
20634 * optimization are things I need to worry about.
20635 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020636 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020637 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020638 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020639 internal_error(state, lnode->def, "scratch used?");
20640 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020641 if ((scratch->prev != scratch) ||
20642 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020643 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020644 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020645 internal_error(state, lnode->def, "scratch in list?");
20646 }
20647 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020648 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020649 for(i = 0; i < count; i++) {
20650 vexpr = &scratch->param[i];
20651 if (*vexpr) {
20652 unuse_triple(*vexpr, scratch);
20653 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020654 }
Eric Biederman90089602004-05-28 14:11:54 +000020655 if (lnode->val->op == OP_UNKNOWNVAL) {
20656 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020657 }
Eric Biederman90089602004-05-28 14:11:54 +000020658 /* Find the case when I am lattice high */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020659 if (lnode->val &&
20660 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020661 (memcmp(lnode->val->param, lnode->def->param,
20662 count * sizeof(lnode->val->param[0])) == 0) &&
20663 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020664 lnode->val = lnode->def;
20665 }
Eric Biederman90089602004-05-28 14:11:54 +000020666 /* Only allow lattice high when all of my inputs
20667 * are also lattice high. Occassionally I can
20668 * have constants with a lattice low input, so
20669 * I do not need to check that case.
20670 */
20671 if (is_lattice_hi(state, lnode)) {
20672 struct lattice_node *tmp;
20673 int rhs;
20674 rhs = lnode->val->rhs;
20675 for(i = 0; i < rhs; i++) {
20676 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20677 if (!is_lattice_hi(state, tmp)) {
20678 lnode->val = 0;
20679 break;
20680 }
20681 }
20682 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020683 /* Find the cases that are always lattice lo */
20684 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020685 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020686 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020687 lnode->val = 0;
20688 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020689 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020690 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020691 /* See if this value should not change */
Eric Biederman90089602004-05-28 14:11:54 +000020692 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020693 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020694 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020695 (lnode->def->op == OP_PIECE))) {
20696#warning "FIXME constant propogate through expressions with multiple left hand sides"
20697 if (changed) {
20698 internal_warning(state, lnode->def, "non def changes value?");
20699 }
20700 lnode->val = 0;
20701 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020702
Eric Biederman83b991a2003-10-11 06:20:25 +000020703 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020704 if (lnode->val != scratch) {
20705 xfree(scratch);
20706 }
Eric Biederman90089602004-05-28 14:11:54 +000020707
Eric Biedermanb138ac82003-04-22 18:44:01 +000020708 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020709}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020710
Eric Biederman90089602004-05-28 14:11:54 +000020711
20712static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020713 struct lattice_node *lnode)
20714{
20715 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020716 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020717 int changed;
20718
20719 /* Update the branch value */
20720 changed = compute_lnode_val(state, scc, lnode);
20721 scc_debug_lnode(state, scc, lnode, changed);
20722
20723 /* This only applies to conditional branches */
20724 if (!triple_is_cbranch(state, lnode->def)) {
20725 internal_error(state, lnode->def, "not a conditional branch");
20726 }
20727
Eric Biederman5ade04a2003-10-22 04:03:46 +000020728 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020729 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020730 FILE *fp = state->errout;
20731 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020732 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020733 lnode->def->id);
20734
20735 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020736 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020737 }
Eric Biederman90089602004-05-28 14:11:54 +000020738 fprintf(fp, " )");
20739 if (lnode->def->rhs > 0) {
20740 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020741 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020742 }
Eric Biederman90089602004-05-28 14:11:54 +000020743 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020744 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020745 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020746 for(left = cond->fblock->out; left; left = left->out_next) {
20747 if (left->dst->block->first == lnode->def->next) {
20748 break;
20749 }
20750 }
20751 if (!left) {
20752 internal_error(state, lnode->def, "Cannot find left branch edge");
20753 }
20754 for(right = cond->fblock->out; right; right = right->out_next) {
20755 if (right->dst->block->first == TARG(lnode->def, 0)) {
20756 break;
20757 }
20758 }
20759 if (!right) {
20760 internal_error(state, lnode->def, "Cannot find right branch edge");
20761 }
Eric Biederman90089602004-05-28 14:11:54 +000020762 /* I should only come here if the controlling expressions value
20763 * has changed, which means it must be either a constant or lo.
20764 */
20765 if (is_lattice_hi(state, cond)) {
20766 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020767 return;
20768 }
Eric Biederman90089602004-05-28 14:11:54 +000020769 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020770 scc_add_fedge(state, scc, left);
20771 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020772 }
20773 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020774 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020775 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020776 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020777 }
20778
20779}
20780
Eric Biederman90089602004-05-28 14:11:54 +000020781
20782static void scc_add_sedge_dst(struct compile_state *state,
20783 struct scc_state *scc, struct ssa_edge *sedge)
20784{
20785 if (triple_is_branch(state, sedge->dst->def)) {
20786 scc_visit_cbranch(state, scc, sedge->dst);
20787 }
20788 else if (triple_is_def(state, sedge->dst->def)) {
20789 scc_add_sedge(state, scc, sedge);
20790 }
20791}
20792
20793static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
20794 struct lattice_node *lnode)
20795{
20796 struct lattice_node *tmp;
20797 struct triple **slot, *old;
20798 struct flow_edge *fedge;
20799 int changed;
20800 int index;
20801 if (lnode->def->op != OP_PHI) {
20802 internal_error(state, lnode->def, "not phi");
20803 }
20804 /* Store the original value */
20805 old = preserve_lval(state, lnode);
20806
20807 /* default to lattice high */
20808 lnode->val = lnode->def;
20809 slot = &RHS(lnode->def, 0);
20810 index = 0;
20811 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20812 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20813 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
20814 index,
20815 fedge->dst->block->vertex,
20816 fedge->executable
20817 );
20818 }
20819 if (!fedge->executable) {
20820 continue;
20821 }
20822 if (!slot[index]) {
20823 internal_error(state, lnode->def, "no phi value");
20824 }
20825 tmp = triple_to_lattice(state, scc, slot[index]);
20826 /* meet(X, lattice low) = lattice low */
20827 if (is_lattice_lo(state, tmp)) {
20828 lnode->val = 0;
20829 }
20830 /* meet(X, lattice high) = X */
20831 else if (is_lattice_hi(state, tmp)) {
20832 lnode->val = lnode->val;
20833 }
20834 /* meet(lattice high, X) = X */
20835 else if (is_lattice_hi(state, lnode)) {
20836 lnode->val = dup_triple(state, tmp->val);
20837 /* Only change the type if necessary */
20838 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20839 lnode->val->type = lnode->def->type;
20840 }
20841 }
20842 /* meet(const, const) = const or lattice low */
20843 else if (!constants_equal(state, lnode->val, tmp->val)) {
20844 lnode->val = 0;
20845 }
20846
20847 /* meet(lattice low, X) = lattice low */
20848 if (is_lattice_lo(state, lnode)) {
20849 lnode->val = 0;
20850 break;
20851 }
20852 }
20853 changed = lval_changed(state, old, lnode);
20854 scc_debug_lnode(state, scc, lnode, changed);
20855
20856 /* If the lattice value has changed update the work lists. */
20857 if (changed) {
20858 struct ssa_edge *sedge;
20859 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20860 scc_add_sedge_dst(state, scc, sedge);
20861 }
20862 }
20863}
20864
20865
Eric Biedermanb138ac82003-04-22 18:44:01 +000020866static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20867 struct lattice_node *lnode)
20868{
20869 int changed;
20870
Eric Biederman90089602004-05-28 14:11:54 +000020871 if (!triple_is_def(state, lnode->def)) {
20872 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020873 }
Eric Biederman90089602004-05-28 14:11:54 +000020874 changed = compute_lnode_val(state, scc, lnode);
20875 scc_debug_lnode(state, scc, lnode, changed);
20876
20877 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020878 struct ssa_edge *sedge;
20879 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020880 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020881 }
20882 }
20883}
20884
20885static void scc_writeback_values(
20886 struct compile_state *state, struct scc_state *scc)
20887{
20888 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020889 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020890 ins = first;
20891 do {
20892 struct lattice_node *lnode;
20893 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020894 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020895 if (is_lattice_hi(state, lnode) &&
20896 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020897 {
20898 struct flow_edge *fedge;
20899 int executable;
20900 executable = 0;
20901 for(fedge = lnode->fblock->in;
20902 !executable && fedge; fedge = fedge->in_next) {
20903 executable |= fedge->executable;
20904 }
20905 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020906 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020907 "lattice node %d %s->%s still high?",
20908 ins->id,
20909 tops(lnode->def->op),
20910 tops(lnode->val->op));
20911 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020912 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020913 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020914
Eric Biederman83b991a2003-10-11 06:20:25 +000020915 /* Restore id */
20916 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020917 if (lnode->val && (lnode->val != ins)) {
20918 /* See if it something I know how to write back */
20919 switch(lnode->val->op) {
20920 case OP_INTCONST:
20921 mkconst(state, ins, lnode->val->u.cval);
20922 break;
20923 case OP_ADDRCONST:
20924 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020925 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020926 break;
20927 default:
20928 /* By default don't copy the changes,
20929 * recompute them in place instead.
20930 */
20931 simplify(state, ins);
20932 break;
20933 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020934 if (is_const(lnode->val) &&
20935 !constants_equal(state, lnode->val, ins)) {
20936 internal_error(state, 0, "constants not equal");
20937 }
20938 /* Free the lattice nodes */
20939 xfree(lnode->val);
20940 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020941 }
20942 ins = ins->next;
20943 } while(ins != first);
20944}
20945
20946static void scc_transform(struct compile_state *state)
20947{
20948 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020949 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
20950 return;
20951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020952
20953 initialize_scc_state(state, &scc);
20954
20955 while(scc.flow_work_list || scc.ssa_work_list) {
20956 struct flow_edge *fedge;
20957 struct ssa_edge *sedge;
20958 struct flow_edge *fptr;
20959 while((fedge = scc_next_fedge(state, &scc))) {
20960 struct block *block;
20961 struct triple *ptr;
20962 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000020963 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020964 int done;
20965 if (fedge->executable) {
20966 continue;
20967 }
20968 if (!fedge->dst) {
20969 internal_error(state, 0, "fedge without dst");
20970 }
20971 if (!fedge->src) {
20972 internal_error(state, 0, "fedge without src");
20973 }
20974 fedge->executable = 1;
20975 fblock = fedge->dst;
20976 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000020977 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020978 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
20979 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000020980 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020981 }
20982 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020983
Eric Biederman5ade04a2003-10-22 04:03:46 +000020984 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020985 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020986 block->vertex, reps);
20987 }
20988
Eric Biedermanb138ac82003-04-22 18:44:01 +000020989 done = 0;
20990 for(ptr = block->first; !done; ptr = ptr->next) {
20991 struct lattice_node *lnode;
20992 done = (ptr == block->last);
20993 lnode = &scc.lattice[ptr->id];
20994 if (ptr->op == OP_PHI) {
20995 scc_visit_phi(state, &scc, lnode);
20996 }
Eric Biederman90089602004-05-28 14:11:54 +000020997 else if ((reps == 1) && triple_is_def(state, ptr))
20998 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020999 scc_visit_expr(state, &scc, lnode);
21000 }
21001 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021002 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021003 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021004 struct flow_edge *out;
21005 for(out = fblock->out; out; out = out->out_next) {
21006 scc_add_fedge(state, &scc, out);
21007 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021008 }
21009 }
21010 while((sedge = scc_next_sedge(state, &scc))) {
21011 struct lattice_node *lnode;
21012 struct flow_block *fblock;
21013 lnode = sedge->dst;
21014 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021015
21016 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021017 fprintf(state->errout, "sedge: %5d (%5d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021018 sedge - scc.ssa_edges,
21019 sedge->src->def->id,
21020 sedge->dst->def->id);
21021 }
21022
Eric Biedermanb138ac82003-04-22 18:44:01 +000021023 if (lnode->def->op == OP_PHI) {
21024 scc_visit_phi(state, &scc, lnode);
21025 }
21026 else {
21027 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21028 if (fptr->executable) {
21029 break;
21030 }
21031 }
21032 if (fptr) {
21033 scc_visit_expr(state, &scc, lnode);
21034 }
21035 }
21036 }
21037 }
21038
21039 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021040 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021041 rebuild_ssa_form(state);
21042
Eric Biederman90089602004-05-28 14:11:54 +000021043 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021044}
21045
21046
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021047static void transform_to_arch_instructions(struct compile_state *state)
21048{
21049 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021050 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021051 ins = first;
21052 do {
21053 ins = transform_to_arch_instruction(state, ins);
21054 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021055
Eric Biederman90089602004-05-28 14:11:54 +000021056 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021057}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021058
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021059#if DEBUG_CONSISTENCY
21060static void verify_uses(struct compile_state *state)
21061{
21062 struct triple *first, *ins;
21063 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021064 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021065 ins = first;
21066 do {
21067 struct triple **expr;
21068 expr = triple_rhs(state, ins, 0);
21069 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021070 struct triple *rhs;
21071 rhs = *expr;
21072 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021073 if (set->member == ins) {
21074 break;
21075 }
21076 }
21077 if (!set) {
21078 internal_error(state, ins, "rhs not used");
21079 }
21080 }
21081 expr = triple_lhs(state, ins, 0);
21082 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021083 struct triple *lhs;
21084 lhs = *expr;
21085 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021086 if (set->member == ins) {
21087 break;
21088 }
21089 }
21090 if (!set) {
21091 internal_error(state, ins, "lhs not used");
21092 }
21093 }
Eric Biederman90089602004-05-28 14:11:54 +000021094 expr = triple_misc(state, ins, 0);
21095 if (ins->op != OP_PHI) {
21096 for(; expr; expr = triple_targ(state, ins, expr)) {
21097 struct triple *misc;
21098 misc = *expr;
21099 for(set = misc?misc->use:0; set; set = set->next) {
21100 if (set->member == ins) {
21101 break;
21102 }
21103 }
21104 if (!set) {
21105 internal_error(state, ins, "misc not used");
21106 }
21107 }
21108 }
21109 if (!triple_is_ret(state, ins)) {
21110 expr = triple_targ(state, ins, 0);
21111 for(; expr; expr = triple_targ(state, ins, expr)) {
21112 struct triple *targ;
21113 targ = *expr;
21114 for(set = targ?targ->use:0; set; set = set->next) {
21115 if (set->member == ins) {
21116 break;
21117 }
21118 }
21119 if (!set) {
21120 internal_error(state, ins, "targ not used");
21121 }
21122 }
21123 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021124 ins = ins->next;
21125 } while(ins != first);
21126
21127}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021128static void verify_blocks_present(struct compile_state *state)
21129{
21130 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021131 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021132 return;
21133 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021134 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021135 ins = first;
21136 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021137 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021138 if (triple_stores_block(state, ins)) {
21139 if (!ins->u.block) {
21140 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021141 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021142 }
21143 }
21144 ins = ins->next;
21145 } while(ins != first);
21146
21147
21148}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021149
21150static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21151{
21152 struct block_set *bedge;
21153 struct block *targ;
21154 targ = block_of_triple(state, edge);
21155 for(bedge = block->edges; bedge; bedge = bedge->next) {
21156 if (bedge->member == targ) {
21157 return 1;
21158 }
21159 }
21160 return 0;
21161}
21162
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021163static void verify_blocks(struct compile_state *state)
21164{
21165 struct triple *ins;
21166 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021167 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021168 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021169 if (!block) {
21170 return;
21171 }
Eric Biederman530b5192003-07-01 10:05:30 +000021172 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021173 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021174 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021175 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021176 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021177 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021178 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021179 internal_error(state, ins, "inconsitent block specified");
21180 }
Eric Biederman530b5192003-07-01 10:05:30 +000021181 valid_ins(state, ins);
21182 }
21183 users = 0;
21184 for(user = block->use; user; user = user->next) {
21185 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021186 if (!user->member->first) {
21187 internal_error(state, block->first, "user is empty");
21188 }
Eric Biederman90089602004-05-28 14:11:54 +000021189 if ((block == state->bb.last_block) &&
21190 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021191 continue;
21192 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021193 for(edge = user->member->edges; edge; edge = edge->next) {
21194 if (edge->member == block) {
21195 break;
21196 }
21197 }
21198 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021199 internal_error(state, user->member->first,
21200 "user does not use block");
21201 }
21202 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021203 if (triple_is_branch(state, block->last)) {
21204 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021205 expr = triple_edge_targ(state, block->last, 0);
21206 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021207 if (*expr && !edge_present(state, block, *expr)) {
21208 internal_error(state, block->last, "no edge to targ");
21209 }
21210 }
Eric Biederman530b5192003-07-01 10:05:30 +000021211 }
Eric Biederman90089602004-05-28 14:11:54 +000021212 if (!triple_is_ubranch(state, block->last) &&
21213 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021214 !edge_present(state, block, block->last->next)) {
21215 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021216 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021217 for(edge = block->edges; edge; edge = edge->next) {
21218 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021219 if (user->member == block) {
21220 break;
21221 }
21222 }
21223 if (!user || user->member != block) {
21224 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021225 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021226 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021227 if (!edge->member->first) {
21228 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021229 }
Eric Biederman530b5192003-07-01 10:05:30 +000021230 }
21231 if (block->users != users) {
21232 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021233 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021234 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021235 }
21236 if (!triple_stores_block(state, block->last->next)) {
21237 internal_error(state, block->last->next,
21238 "cannot find next block");
21239 }
21240 block = block->last->next->u.block;
21241 if (!block) {
21242 internal_error(state, block->last->next,
21243 "bad next block");
21244 }
Eric Biederman90089602004-05-28 14:11:54 +000021245 } while(block != state->bb.first_block);
21246 if (blocks != state->bb.last_vertex) {
21247 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21248 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021249 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021250}
21251
21252static void verify_domination(struct compile_state *state)
21253{
21254 struct triple *first, *ins;
21255 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021256 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021257 return;
21258 }
21259
Eric Biederman83b991a2003-10-11 06:20:25 +000021260 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021261 ins = first;
21262 do {
21263 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021264 struct triple **slot;
21265 struct triple *use_point;
21266 int i, zrhs;
21267 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021268 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021269 slot = &RHS(set->member, 0);
21270 /* See if the use is on the right hand side */
21271 for(i = 0; i < zrhs; i++) {
21272 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021273 break;
21274 }
21275 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021276 if (i < zrhs) {
21277 use_point = set->member;
21278 if (set->member->op == OP_PHI) {
21279 struct block_set *bset;
21280 int edge;
21281 bset = set->member->u.block->use;
21282 for(edge = 0; bset && (edge < i); edge++) {
21283 bset = bset->next;
21284 }
21285 if (!bset) {
21286 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021287 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021288 }
21289 use_point = bset->member->last;
21290 }
21291 }
21292 if (use_point &&
21293 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021294 if (is_const(ins)) {
21295 internal_warning(state, ins,
Eric Biederman66fe2222003-07-04 15:14:04 +000021296 "non dominated rhs use point?");
Eric Biederman90089602004-05-28 14:11:54 +000021297 }
21298 else {
21299 internal_error(state, ins,
21300 "non dominated rhs use point?");
21301 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021302 }
21303 }
21304 ins = ins->next;
21305 } while(ins != first);
21306}
21307
Eric Biederman83b991a2003-10-11 06:20:25 +000021308static void verify_rhs(struct compile_state *state)
21309{
21310 struct triple *first, *ins;
21311 first = state->first;
21312 ins = first;
21313 do {
21314 struct triple **slot;
21315 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021316 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021317 slot = &RHS(ins, 0);
21318 for(i = 0; i < zrhs; i++) {
21319 if (slot[i] == 0) {
21320 internal_error(state, ins,
21321 "missing rhs %d on %s",
21322 i, tops(ins->op));
21323 }
21324 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21325 internal_error(state, ins,
21326 "ins == rhs[%d] on %s",
21327 i, tops(ins->op));
21328 }
21329 }
21330 ins = ins->next;
21331 } while(ins != first);
21332}
21333
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021334static void verify_piece(struct compile_state *state)
21335{
21336 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021337 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021338 ins = first;
21339 do {
21340 struct triple *ptr;
21341 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021342 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021343 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21344 if (ptr != LHS(ins, i)) {
21345 internal_error(state, ins, "malformed lhs on %s",
21346 tops(ins->op));
21347 }
21348 if (ptr->op != OP_PIECE) {
21349 internal_error(state, ins, "bad lhs op %s at %d on %s",
21350 tops(ptr->op), i, tops(ins->op));
21351 }
21352 if (ptr->u.cval != i) {
21353 internal_error(state, ins, "bad u.cval of %d %d expected",
21354 ptr->u.cval, i);
21355 }
21356 }
21357 ins = ins->next;
21358 } while(ins != first);
21359}
Eric Biederman83b991a2003-10-11 06:20:25 +000021360
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021361static void verify_ins_colors(struct compile_state *state)
21362{
21363 struct triple *first, *ins;
21364
Eric Biederman83b991a2003-10-11 06:20:25 +000021365 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021366 ins = first;
21367 do {
21368 ins = ins->next;
21369 } while(ins != first);
21370}
Eric Biederman90089602004-05-28 14:11:54 +000021371
21372static void verify_unknown(struct compile_state *state)
21373{
21374 struct triple *first, *ins;
21375 if ( (unknown_triple.next != &unknown_triple) ||
21376 (unknown_triple.prev != &unknown_triple) ||
21377#if 0
21378 (unknown_triple.use != 0) ||
21379#endif
21380 (unknown_triple.op != OP_UNKNOWNVAL) ||
21381 (unknown_triple.lhs != 0) ||
21382 (unknown_triple.rhs != 0) ||
21383 (unknown_triple.misc != 0) ||
21384 (unknown_triple.targ != 0) ||
21385 (unknown_triple.template_id != 0) ||
21386 (unknown_triple.id != -1) ||
21387 (unknown_triple.type != &unknown_type) ||
21388 (unknown_triple.occurance != &dummy_occurance) ||
21389 (unknown_triple.param[0] != 0) ||
21390 (unknown_triple.param[1] != 0)) {
21391 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21392 }
21393 if ( (dummy_occurance.count != 2) ||
21394 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21395 (strcmp(dummy_occurance.function, "") != 0) ||
21396 (dummy_occurance.col != 0) ||
21397 (dummy_occurance.parent != 0)) {
21398 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21399 }
21400 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21401 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21402 }
21403 first = state->first;
21404 ins = first;
21405 do {
21406 int params, i;
21407 if (ins == &unknown_triple) {
21408 internal_error(state, ins, "unknown triple in list");
21409 }
21410 params = TRIPLE_SIZE(ins);
21411 for(i = 0; i < params; i++) {
21412 if (ins->param[i] == &unknown_triple) {
21413 internal_error(state, ins, "unknown triple used!");
21414 }
21415 }
21416 ins = ins->next;
21417 } while(ins != first);
21418}
21419
21420static void verify_types(struct compile_state *state)
21421{
21422 struct triple *first, *ins;
21423 first = state->first;
21424 ins = first;
21425 do {
21426 struct type *invalid;
21427 invalid = invalid_type(state, ins->type);
21428 if (invalid) {
21429 FILE *fp = state->errout;
21430 fprintf(fp, "type: ");
21431 name_of(fp, ins->type);
21432 fprintf(fp, "\n");
21433 fprintf(fp, "invalid type: ");
21434 name_of(fp, invalid);
21435 fprintf(fp, "\n");
21436 internal_error(state, ins, "invalid ins type");
21437 }
21438 } while(ins != first);
21439}
21440
21441static void verify_copy(struct compile_state *state)
21442{
21443 struct triple *first, *ins, *next;
21444 first = state->first;
21445 next = ins = first;
21446 do {
21447 ins = next;
21448 next = ins->next;
21449 if (ins->op != OP_COPY) {
21450 continue;
21451 }
21452 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21453 FILE *fp = state->errout;
21454 fprintf(fp, "src type: ");
21455 name_of(fp, RHS(ins, 0)->type);
21456 fprintf(fp, "\n");
21457 fprintf(fp, "dst type: ");
21458 name_of(fp, ins->type);
21459 fprintf(fp, "\n");
21460 internal_error(state, ins, "type mismatch in copy");
21461 }
21462 } while(next != first);
21463}
21464
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021465static void verify_consistency(struct compile_state *state)
21466{
Eric Biederman90089602004-05-28 14:11:54 +000021467 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021468 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021469 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021470 verify_blocks(state);
21471 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021472 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021473 verify_piece(state);
21474 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021475 verify_types(state);
21476 verify_copy(state);
21477 if (state->compiler->debug & DEBUG_VERIFICATION) {
21478 fprintf(state->dbgout, "consistency verified\n");
21479 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021480}
21481#else
Eric Biederman153ea352003-06-20 14:43:20 +000021482static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021483#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021484
21485static void optimize(struct compile_state *state)
21486{
Eric Biederman90089602004-05-28 14:11:54 +000021487 /* Join all of the functions into one giant function */
21488 join_functions(state);
21489
Eric Biederman5ade04a2003-10-22 04:03:46 +000021490 /* Dump what the instruction graph intially looks like */
21491 print_triples(state);
21492
Eric Biederman0babc1c2003-05-09 02:39:00 +000021493 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021494 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021495 print_triples(state);
21496
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021497 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021498 /* Analize the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021499 state->bb.first = state->first;
21500 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021501
Eric Biederman530b5192003-07-01 10:05:30 +000021502 /* Transform the code to ssa form. */
21503 /*
21504 * The transformation to ssa form puts a phi function
21505 * on each of edge of a dominance frontier where that
21506 * phi function might be needed. At -O2 if we don't
21507 * eleminate the excess phi functions we can get an
21508 * exponential code size growth. So I kill the extra
21509 * phi functions early and I kill them often.
21510 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021511 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021512 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021513
Eric Biederman83b991a2003-10-11 06:20:25 +000021514 /* Remove dead code */
21515 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021516 verify_consistency(state);
21517
Eric Biedermanb138ac82003-04-22 18:44:01 +000021518 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021519 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021520 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021521 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021522 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021523 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021524#warning "WISHLIST implement single use constants (least possible register pressure)"
21525#warning "WISHLIST implement induction variable elimination"
Eric Biedermanb138ac82003-04-22 18:44:01 +000021526 /* Select architecture instructions and an initial partial
21527 * coloring based on architecture constraints.
21528 */
21529 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021530 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021531
Eric Biederman83b991a2003-10-11 06:20:25 +000021532 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021533 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021534 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021535
Eric Biedermanb138ac82003-04-22 18:44:01 +000021536 /* Color all of the variables to see if they will fit in registers */
21537 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021538 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021539
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021540 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021541 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021542
Eric Biedermanb138ac82003-04-22 18:44:01 +000021543 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021544 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021545
Eric Biedermanb138ac82003-04-22 18:44:01 +000021546 /* Remove the optimization information.
21547 * This is more to check for memory consistency than to free memory.
21548 */
Eric Biederman90089602004-05-28 14:11:54 +000021549 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021550}
21551
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021552static void print_op_asm(struct compile_state *state,
21553 struct triple *ins, FILE *fp)
21554{
21555 struct asm_info *info;
21556 const char *ptr;
21557 unsigned lhs, rhs, i;
21558 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021559 lhs = ins->lhs;
21560 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021561 /* Don't count the clobbers in lhs */
21562 for(i = 0; i < lhs; i++) {
21563 if (LHS(ins, i)->type == &void_type) {
21564 break;
21565 }
21566 }
21567 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021568 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021569 fputc('\t', fp);
21570 for(ptr = info->str; *ptr; ptr++) {
21571 char *next;
21572 unsigned long param;
21573 struct triple *piece;
21574 if (*ptr != '%') {
21575 fputc(*ptr, fp);
21576 continue;
21577 }
21578 ptr++;
21579 if (*ptr == '%') {
21580 fputc('%', fp);
21581 continue;
21582 }
21583 param = strtoul(ptr, &next, 10);
21584 if (ptr == next) {
21585 error(state, ins, "Invalid asm template");
21586 }
21587 if (param >= (lhs + rhs)) {
21588 error(state, ins, "Invalid param %%%u in asm template",
21589 param);
21590 }
21591 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21592 fprintf(fp, "%s",
21593 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021594 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021595 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021596 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021597}
21598
21599
21600/* Only use the low x86 byte registers. This allows me
21601 * allocate the entire register when a byte register is used.
21602 */
21603#define X86_4_8BIT_GPRS 1
21604
Eric Biederman83b991a2003-10-11 06:20:25 +000021605/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021606#define X86_MMX_REGS (1<<0)
21607#define X86_XMM_REGS (1<<1)
21608#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021609
Eric Biedermanb138ac82003-04-22 18:44:01 +000021610/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021611#define REGC_FLAGS 0
21612#define REGC_GPR8 1
21613#define REGC_GPR16 2
21614#define REGC_GPR32 3
21615#define REGC_DIVIDEND64 4
21616#define REGC_DIVIDEND32 5
21617#define REGC_MMX 6
21618#define REGC_XMM 7
21619#define REGC_GPR32_8 8
21620#define REGC_GPR16_8 9
21621#define REGC_GPR8_LO 10
21622#define REGC_IMM32 11
21623#define REGC_IMM16 12
21624#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021625#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021626#if LAST_REGC >= MAX_REGC
21627#error "MAX_REGC is to low"
21628#endif
21629
21630/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021631#define REGCM_FLAGS (1 << REGC_FLAGS)
21632#define REGCM_GPR8 (1 << REGC_GPR8)
21633#define REGCM_GPR16 (1 << REGC_GPR16)
21634#define REGCM_GPR32 (1 << REGC_GPR32)
21635#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21636#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21637#define REGCM_MMX (1 << REGC_MMX)
21638#define REGCM_XMM (1 << REGC_XMM)
21639#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21640#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21641#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21642#define REGCM_IMM32 (1 << REGC_IMM32)
21643#define REGCM_IMM16 (1 << REGC_IMM16)
21644#define REGCM_IMM8 (1 << REGC_IMM8)
21645#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021646#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021647
21648/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021649#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021650#define REGC_FLAGS_FIRST REG_EFLAGS
21651#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021652#define REG_AL 3
21653#define REG_BL 4
21654#define REG_CL 5
21655#define REG_DL 6
21656#define REG_AH 7
21657#define REG_BH 8
21658#define REG_CH 9
21659#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021660#define REGC_GPR8_LO_FIRST REG_AL
21661#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021662#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021663#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021664#define REG_AX 11
21665#define REG_BX 12
21666#define REG_CX 13
21667#define REG_DX 14
21668#define REG_SI 15
21669#define REG_DI 16
21670#define REG_BP 17
21671#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021672#define REGC_GPR16_FIRST REG_AX
21673#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021674#define REG_EAX 19
21675#define REG_EBX 20
21676#define REG_ECX 21
21677#define REG_EDX 22
21678#define REG_ESI 23
21679#define REG_EDI 24
21680#define REG_EBP 25
21681#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021682#define REGC_GPR32_FIRST REG_EAX
21683#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021684#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021685#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21686#define REGC_DIVIDEND64_LAST REG_EDXEAX
21687#define REG_DXAX 28
21688#define REGC_DIVIDEND32_FIRST REG_DXAX
21689#define REGC_DIVIDEND32_LAST REG_DXAX
21690#define REG_MMX0 29
21691#define REG_MMX1 30
21692#define REG_MMX2 31
21693#define REG_MMX3 32
21694#define REG_MMX4 33
21695#define REG_MMX5 34
21696#define REG_MMX6 35
21697#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021698#define REGC_MMX_FIRST REG_MMX0
21699#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021700#define REG_XMM0 37
21701#define REG_XMM1 38
21702#define REG_XMM2 39
21703#define REG_XMM3 40
21704#define REG_XMM4 41
21705#define REG_XMM5 42
21706#define REG_XMM6 43
21707#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021708#define REGC_XMM_FIRST REG_XMM0
21709#define REGC_XMM_LAST REG_XMM7
21710#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
21711#define LAST_REG REG_XMM7
21712
21713#define REGC_GPR32_8_FIRST REG_EAX
21714#define REGC_GPR32_8_LAST REG_EDX
21715#define REGC_GPR16_8_FIRST REG_AX
21716#define REGC_GPR16_8_LAST REG_DX
21717
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021718#define REGC_IMM8_FIRST -1
21719#define REGC_IMM8_LAST -1
21720#define REGC_IMM16_FIRST -2
21721#define REGC_IMM16_LAST -1
21722#define REGC_IMM32_FIRST -4
21723#define REGC_IMM32_LAST -1
21724
Eric Biedermanb138ac82003-04-22 18:44:01 +000021725#if LAST_REG >= MAX_REGISTERS
21726#error "MAX_REGISTERS to low"
21727#endif
21728
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021729
21730static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021731 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21732 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21733 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21734 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21735 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21736 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21737 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21738 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21739 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21740 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21741 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21742 [REGC_IMM32] = 0,
21743 [REGC_IMM16] = 0,
21744 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021745};
21746
21747static const struct {
21748 int first, last;
21749} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021750 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21751 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21752 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21753 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21754 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21755 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21756 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21757 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21758 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21759 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21760 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21761 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21762 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21763 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021764};
21765
Eric Biederman90089602004-05-28 14:11:54 +000021766#if ARCH_INPUT_REGS != 4
21767#error ARCH_INPUT_REGS size mismatch
21768#endif
21769static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21770 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21771 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21772 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21773 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21774};
21775
21776#if ARCH_OUTPUT_REGS != 4
21777#error ARCH_INPUT_REGS size mismatch
21778#endif
21779static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21780 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21781 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21782 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21783 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21784};
21785
Eric Biederman5ade04a2003-10-22 04:03:46 +000021786static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021787{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021788 memset(arch, 0, sizeof(*arch));
21789 arch->features = 0;
21790}
21791
Eric Biederman90089602004-05-28 14:11:54 +000021792static const struct compiler_flag arch_flags[] = {
21793 { "mmx", X86_MMX_REGS },
21794 { "sse", X86_XMM_REGS },
21795 { "noop-copy", X86_NOOP_COPY },
21796 { 0, 0 },
21797};
21798static const struct compiler_flag arch_cpus[] = {
21799 { "i386", 0 },
21800 { "p2", X86_MMX_REGS },
21801 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21802 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21803 { "k7", X86_MMX_REGS },
21804 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21805 { "c3", X86_MMX_REGS },
21806 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21807 { 0, 0 }
21808};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021809static int arch_encode_flag(struct arch_state *arch, const char *flag)
21810{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021811 int result;
21812 int act;
21813
21814 act = 1;
21815 result = -1;
21816 if (strncmp(flag, "no-", 3) == 0) {
21817 flag += 3;
21818 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021819 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021820 if (act && strncmp(flag, "cpu=", 4) == 0) {
21821 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021822 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021823 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021824 else {
Eric Biederman90089602004-05-28 14:11:54 +000021825 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021826 }
21827 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021828}
21829
Eric Biederman90089602004-05-28 14:11:54 +000021830static void arch_usage(FILE *fp)
21831{
21832 flag_usage(fp, arch_flags, "-m", "-mno-");
21833 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21834}
21835
Eric Biedermanb138ac82003-04-22 18:44:01 +000021836static unsigned arch_regc_size(struct compile_state *state, int class)
21837{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021838 if ((class < 0) || (class > LAST_REGC)) {
21839 return 0;
21840 }
21841 return regc_size[class];
21842}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021843
Eric Biedermanb138ac82003-04-22 18:44:01 +000021844static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21845{
21846 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021847 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21848 REGCM_GPR32_8 | REGCM_GPR32 |
21849 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021850
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021851 /* Special case for the immediates */
21852 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21853 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21854 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21855 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
21856 return 0;
21857 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021858 return (regcm1 & regcm2) ||
21859 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21860}
21861
21862static void arch_reg_equivs(
21863 struct compile_state *state, unsigned *equiv, int reg)
21864{
21865 if ((reg < 0) || (reg > LAST_REG)) {
21866 internal_error(state, 0, "invalid register");
21867 }
21868 *equiv++ = reg;
21869 switch(reg) {
21870 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021871#if X86_4_8BIT_GPRS
21872 *equiv++ = REG_AH;
21873#endif
21874 *equiv++ = REG_AX;
21875 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021876 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021877 *equiv++ = REG_EDXEAX;
21878 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021879 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021880#if X86_4_8BIT_GPRS
21881 *equiv++ = REG_AL;
21882#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021883 *equiv++ = REG_AX;
21884 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021885 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021886 *equiv++ = REG_EDXEAX;
21887 break;
21888 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021889#if X86_4_8BIT_GPRS
21890 *equiv++ = REG_BH;
21891#endif
21892 *equiv++ = REG_BX;
21893 *equiv++ = REG_EBX;
21894 break;
21895
Eric Biedermanb138ac82003-04-22 18:44:01 +000021896 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021897#if X86_4_8BIT_GPRS
21898 *equiv++ = REG_BL;
21899#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021900 *equiv++ = REG_BX;
21901 *equiv++ = REG_EBX;
21902 break;
21903 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021904#if X86_4_8BIT_GPRS
21905 *equiv++ = REG_CH;
21906#endif
21907 *equiv++ = REG_CX;
21908 *equiv++ = REG_ECX;
21909 break;
21910
Eric Biedermanb138ac82003-04-22 18:44:01 +000021911 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021912#if X86_4_8BIT_GPRS
21913 *equiv++ = REG_CL;
21914#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021915 *equiv++ = REG_CX;
21916 *equiv++ = REG_ECX;
21917 break;
21918 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021919#if X86_4_8BIT_GPRS
21920 *equiv++ = REG_DH;
21921#endif
21922 *equiv++ = REG_DX;
21923 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000021924 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021925 *equiv++ = REG_EDXEAX;
21926 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021927 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021928#if X86_4_8BIT_GPRS
21929 *equiv++ = REG_DL;
21930#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021931 *equiv++ = REG_DX;
21932 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000021933 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021934 *equiv++ = REG_EDXEAX;
21935 break;
21936 case REG_AX:
21937 *equiv++ = REG_AL;
21938 *equiv++ = REG_AH;
21939 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021940 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021941 *equiv++ = REG_EDXEAX;
21942 break;
21943 case REG_BX:
21944 *equiv++ = REG_BL;
21945 *equiv++ = REG_BH;
21946 *equiv++ = REG_EBX;
21947 break;
21948 case REG_CX:
21949 *equiv++ = REG_CL;
21950 *equiv++ = REG_CH;
21951 *equiv++ = REG_ECX;
21952 break;
21953 case REG_DX:
21954 *equiv++ = REG_DL;
21955 *equiv++ = REG_DH;
21956 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000021957 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021958 *equiv++ = REG_EDXEAX;
21959 break;
21960 case REG_SI:
21961 *equiv++ = REG_ESI;
21962 break;
21963 case REG_DI:
21964 *equiv++ = REG_EDI;
21965 break;
21966 case REG_BP:
21967 *equiv++ = REG_EBP;
21968 break;
21969 case REG_SP:
21970 *equiv++ = REG_ESP;
21971 break;
21972 case REG_EAX:
21973 *equiv++ = REG_AL;
21974 *equiv++ = REG_AH;
21975 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000021976 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021977 *equiv++ = REG_EDXEAX;
21978 break;
21979 case REG_EBX:
21980 *equiv++ = REG_BL;
21981 *equiv++ = REG_BH;
21982 *equiv++ = REG_BX;
21983 break;
21984 case REG_ECX:
21985 *equiv++ = REG_CL;
21986 *equiv++ = REG_CH;
21987 *equiv++ = REG_CX;
21988 break;
21989 case REG_EDX:
21990 *equiv++ = REG_DL;
21991 *equiv++ = REG_DH;
21992 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000021993 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021994 *equiv++ = REG_EDXEAX;
21995 break;
21996 case REG_ESI:
21997 *equiv++ = REG_SI;
21998 break;
21999 case REG_EDI:
22000 *equiv++ = REG_DI;
22001 break;
22002 case REG_EBP:
22003 *equiv++ = REG_BP;
22004 break;
22005 case REG_ESP:
22006 *equiv++ = REG_SP;
22007 break;
Eric Biederman530b5192003-07-01 10:05:30 +000022008 case REG_DXAX:
22009 *equiv++ = REG_AL;
22010 *equiv++ = REG_AH;
22011 *equiv++ = REG_DL;
22012 *equiv++ = REG_DH;
22013 *equiv++ = REG_AX;
22014 *equiv++ = REG_DX;
22015 *equiv++ = REG_EAX;
22016 *equiv++ = REG_EDX;
22017 *equiv++ = REG_EDXEAX;
22018 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022019 case REG_EDXEAX:
22020 *equiv++ = REG_AL;
22021 *equiv++ = REG_AH;
22022 *equiv++ = REG_DL;
22023 *equiv++ = REG_DH;
22024 *equiv++ = REG_AX;
22025 *equiv++ = REG_DX;
22026 *equiv++ = REG_EAX;
22027 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022028 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022029 break;
22030 }
22031 *equiv++ = REG_UNSET;
22032}
22033
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022034static unsigned arch_avail_mask(struct compile_state *state)
22035{
22036 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022037 /* REGCM_GPR8 is not available */
22038 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22039 REGCM_GPR32 | REGCM_GPR32_8 |
22040 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022041 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022042 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022043 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022044 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022045 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022046 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022047 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022048 return avail_mask;
22049}
22050
22051static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22052{
22053 unsigned mask, result;
22054 int class, class2;
22055 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022056
22057 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22058 if ((result & mask) == 0) {
22059 continue;
22060 }
22061 if (class > LAST_REGC) {
22062 result &= ~mask;
22063 }
22064 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22065 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22066 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22067 result |= (1 << class2);
22068 }
22069 }
22070 }
Eric Biederman530b5192003-07-01 10:05:30 +000022071 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022072 return result;
22073}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022074
Eric Biedermand1ea5392003-06-28 06:49:45 +000022075static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22076{
22077 /* Like arch_regcm_normalize except immediate register classes are excluded */
22078 regcm = arch_regcm_normalize(state, regcm);
22079 /* Remove the immediate register classes */
22080 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22081 return regcm;
22082
22083}
22084
Eric Biedermanb138ac82003-04-22 18:44:01 +000022085static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22086{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022087 unsigned mask;
22088 int class;
22089 mask = 0;
22090 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022091 if ((reg >= regcm_bound[class].first) &&
22092 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022093 mask |= (1 << class);
22094 }
22095 }
22096 if (!mask) {
22097 internal_error(state, 0, "reg %d not in any class", reg);
22098 }
22099 return mask;
22100}
22101
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022102static struct reg_info arch_reg_constraint(
22103 struct compile_state *state, struct type *type, const char *constraint)
22104{
22105 static const struct {
22106 char class;
22107 unsigned int mask;
22108 unsigned int reg;
22109 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022110 { 'r', REGCM_GPR32, REG_UNSET },
22111 { 'g', REGCM_GPR32, REG_UNSET },
22112 { 'p', REGCM_GPR32, REG_UNSET },
22113 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022114 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022115 { 'x', REGCM_XMM, REG_UNSET },
22116 { 'y', REGCM_MMX, REG_UNSET },
22117 { 'a', REGCM_GPR32, REG_EAX },
22118 { 'b', REGCM_GPR32, REG_EBX },
22119 { 'c', REGCM_GPR32, REG_ECX },
22120 { 'd', REGCM_GPR32, REG_EDX },
22121 { 'D', REGCM_GPR32, REG_EDI },
22122 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022123 { '\0', 0, REG_UNSET },
22124 };
22125 unsigned int regcm;
22126 unsigned int mask, reg;
22127 struct reg_info result;
22128 const char *ptr;
22129 regcm = arch_type_to_regcm(state, type);
22130 reg = REG_UNSET;
22131 mask = 0;
22132 for(ptr = constraint; *ptr; ptr++) {
22133 int i;
22134 if (*ptr == ' ') {
22135 continue;
22136 }
22137 for(i = 0; constraints[i].class != '\0'; i++) {
22138 if (constraints[i].class == *ptr) {
22139 break;
22140 }
22141 }
22142 if (constraints[i].class == '\0') {
22143 error(state, 0, "invalid register constraint ``%c''", *ptr);
22144 break;
22145 }
22146 if ((constraints[i].mask & regcm) == 0) {
22147 error(state, 0, "invalid register class %c specified",
22148 *ptr);
22149 }
22150 mask |= constraints[i].mask;
22151 if (constraints[i].reg != REG_UNSET) {
22152 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22153 error(state, 0, "Only one register may be specified");
22154 }
22155 reg = constraints[i].reg;
22156 }
22157 }
22158 result.reg = reg;
22159 result.regcm = mask;
22160 return result;
22161}
22162
22163static struct reg_info arch_reg_clobber(
22164 struct compile_state *state, const char *clobber)
22165{
22166 struct reg_info result;
22167 if (strcmp(clobber, "memory") == 0) {
22168 result.reg = REG_UNSET;
22169 result.regcm = 0;
22170 }
Eric Biederman90089602004-05-28 14:11:54 +000022171 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022172 result.reg = REG_EAX;
22173 result.regcm = REGCM_GPR32;
22174 }
Eric Biederman90089602004-05-28 14:11:54 +000022175 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022176 result.reg = REG_EBX;
22177 result.regcm = REGCM_GPR32;
22178 }
Eric Biederman90089602004-05-28 14:11:54 +000022179 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022180 result.reg = REG_ECX;
22181 result.regcm = REGCM_GPR32;
22182 }
Eric Biederman90089602004-05-28 14:11:54 +000022183 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022184 result.reg = REG_EDX;
22185 result.regcm = REGCM_GPR32;
22186 }
Eric Biederman90089602004-05-28 14:11:54 +000022187 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022188 result.reg = REG_ESI;
22189 result.regcm = REGCM_GPR32;
22190 }
Eric Biederman90089602004-05-28 14:11:54 +000022191 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022192 result.reg = REG_EDI;
22193 result.regcm = REGCM_GPR32;
22194 }
Eric Biederman90089602004-05-28 14:11:54 +000022195 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022196 result.reg = REG_EBP;
22197 result.regcm = REGCM_GPR32;
22198 }
Eric Biederman90089602004-05-28 14:11:54 +000022199 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022200 result.reg = REG_ESP;
22201 result.regcm = REGCM_GPR32;
22202 }
22203 else if (strcmp(clobber, "cc") == 0) {
22204 result.reg = REG_EFLAGS;
22205 result.regcm = REGCM_FLAGS;
22206 }
22207 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22208 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22209 result.reg = REG_XMM0 + octdigval(clobber[3]);
22210 result.regcm = REGCM_XMM;
22211 }
Eric Biederman90089602004-05-28 14:11:54 +000022212 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022213 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22214 result.reg = REG_MMX0 + octdigval(clobber[3]);
22215 result.regcm = REGCM_MMX;
22216 }
22217 else {
Eric Biederman90089602004-05-28 14:11:54 +000022218 error(state, 0, "unknown register name `%s' in asm",
22219 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022220 result.reg = REG_UNSET;
22221 result.regcm = 0;
22222 }
22223 return result;
22224}
22225
Eric Biedermanb138ac82003-04-22 18:44:01 +000022226static int do_select_reg(struct compile_state *state,
22227 char *used, int reg, unsigned classes)
22228{
22229 unsigned mask;
22230 if (used[reg]) {
22231 return REG_UNSET;
22232 }
22233 mask = arch_reg_regcm(state, reg);
22234 return (classes & mask) ? reg : REG_UNSET;
22235}
22236
22237static int arch_select_free_register(
22238 struct compile_state *state, char *used, int classes)
22239{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022240 /* Live ranges with the most neighbors are colored first.
22241 *
22242 * Generally it does not matter which colors are given
22243 * as the register allocator attempts to color live ranges
22244 * in an order where you are guaranteed not to run out of colors.
22245 *
22246 * Occasionally the register allocator cannot find an order
22247 * of register selection that will find a free color. To
22248 * increase the odds the register allocator will work when
22249 * it guesses first give out registers from register classes
22250 * least likely to run out of registers.
22251 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022252 */
22253 int i, reg;
22254 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022255 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022256 reg = do_select_reg(state, used, i, classes);
22257 }
22258 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22259 reg = do_select_reg(state, used, i, classes);
22260 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022261 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022262 reg = do_select_reg(state, used, i, classes);
22263 }
22264 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22265 reg = do_select_reg(state, used, i, classes);
22266 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022267 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22268 reg = do_select_reg(state, used, i, classes);
22269 }
Eric Biederman530b5192003-07-01 10:05:30 +000022270 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22271 reg = do_select_reg(state, used, i, classes);
22272 }
22273 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22274 reg = do_select_reg(state, used, i, classes);
22275 }
22276 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022277 reg = do_select_reg(state, used, i, classes);
22278 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022279 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22280 reg = do_select_reg(state, used, i, classes);
22281 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022282 return reg;
22283}
22284
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022285
Eric Biedermanb138ac82003-04-22 18:44:01 +000022286static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
22287{
22288#warning "FIXME force types smaller (if legal) before I get here"
Eric Biedermanb138ac82003-04-22 18:44:01 +000022289 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022290 mask = 0;
22291 switch(type->type & TYPE_MASK) {
22292 case TYPE_ARRAY:
22293 case TYPE_VOID:
22294 mask = 0;
22295 break;
22296 case TYPE_CHAR:
22297 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022298 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022299 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022300 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022301 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022302 REGCM_MMX | REGCM_XMM |
22303 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022304 break;
22305 case TYPE_SHORT:
22306 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022307 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022308 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022309 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022310 REGCM_MMX | REGCM_XMM |
22311 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022312 break;
Eric Biederman90089602004-05-28 14:11:54 +000022313 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022314 case TYPE_INT:
22315 case TYPE_UINT:
22316 case TYPE_LONG:
22317 case TYPE_ULONG:
22318 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022319 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022320 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22321 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022322 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022323 break;
Eric Biederman90089602004-05-28 14:11:54 +000022324 case TYPE_JOIN:
22325 case TYPE_UNION:
22326 mask = arch_type_to_regcm(state, type->left);
22327 break;
22328 case TYPE_OVERLAP:
22329 mask = arch_type_to_regcm(state, type->left) &
22330 arch_type_to_regcm(state, type->right);
22331 break;
22332 case TYPE_BITFIELD:
22333 mask = arch_type_to_regcm(state, type->left);
22334 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022335 default:
Eric Biederman90089602004-05-28 14:11:54 +000022336 fprintf(state->errout, "type: ");
22337 name_of(state->errout, type);
22338 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022339 internal_error(state, 0, "no register class for type");
22340 break;
22341 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022342 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022343 return mask;
22344}
22345
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022346static int is_imm32(struct triple *imm)
22347{
22348 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
22349 (imm->op == OP_ADDRCONST);
22350
22351}
22352static int is_imm16(struct triple *imm)
22353{
22354 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22355}
22356static int is_imm8(struct triple *imm)
22357{
22358 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22359}
22360
22361static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022362{
22363 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022364 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022365 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022366 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022367 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022368 if (!is_imm32(imm)) {
22369 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022370 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022371 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022372 use_triple(imm, ins);
22373 *expr = imm;
22374 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022375}
22376
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022377static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378{
22379 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022380 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022381 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022382 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022383 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022384 if (!is_imm8(imm)) {
22385 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022386 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022387 unuse_triple(*expr, ins);
22388 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022389 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022390 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391}
22392
Eric Biederman530b5192003-07-01 10:05:30 +000022393#define TEMPLATE_NOP 0
22394#define TEMPLATE_INTCONST8 1
22395#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022396#define TEMPLATE_UNKNOWNVAL 3
22397#define TEMPLATE_COPY8_REG 5
22398#define TEMPLATE_COPY16_REG 6
22399#define TEMPLATE_COPY32_REG 7
22400#define TEMPLATE_COPY_IMM8 8
22401#define TEMPLATE_COPY_IMM16 9
22402#define TEMPLATE_COPY_IMM32 10
22403#define TEMPLATE_PHI8 11
22404#define TEMPLATE_PHI16 12
22405#define TEMPLATE_PHI32 13
22406#define TEMPLATE_STORE8 14
22407#define TEMPLATE_STORE16 15
22408#define TEMPLATE_STORE32 16
22409#define TEMPLATE_LOAD8 17
22410#define TEMPLATE_LOAD16 18
22411#define TEMPLATE_LOAD32 19
22412#define TEMPLATE_BINARY8_REG 20
22413#define TEMPLATE_BINARY16_REG 21
22414#define TEMPLATE_BINARY32_REG 22
22415#define TEMPLATE_BINARY8_IMM 23
22416#define TEMPLATE_BINARY16_IMM 24
22417#define TEMPLATE_BINARY32_IMM 25
22418#define TEMPLATE_SL8_CL 26
22419#define TEMPLATE_SL16_CL 27
22420#define TEMPLATE_SL32_CL 28
22421#define TEMPLATE_SL8_IMM 29
22422#define TEMPLATE_SL16_IMM 30
22423#define TEMPLATE_SL32_IMM 31
22424#define TEMPLATE_UNARY8 32
22425#define TEMPLATE_UNARY16 33
22426#define TEMPLATE_UNARY32 34
22427#define TEMPLATE_CMP8_REG 35
22428#define TEMPLATE_CMP16_REG 36
22429#define TEMPLATE_CMP32_REG 37
22430#define TEMPLATE_CMP8_IMM 38
22431#define TEMPLATE_CMP16_IMM 39
22432#define TEMPLATE_CMP32_IMM 40
22433#define TEMPLATE_TEST8 41
22434#define TEMPLATE_TEST16 42
22435#define TEMPLATE_TEST32 43
22436#define TEMPLATE_SET 44
22437#define TEMPLATE_JMP 45
22438#define TEMPLATE_RET 46
22439#define TEMPLATE_INB_DX 47
22440#define TEMPLATE_INB_IMM 48
22441#define TEMPLATE_INW_DX 49
22442#define TEMPLATE_INW_IMM 50
22443#define TEMPLATE_INL_DX 51
22444#define TEMPLATE_INL_IMM 52
22445#define TEMPLATE_OUTB_DX 53
22446#define TEMPLATE_OUTB_IMM 54
22447#define TEMPLATE_OUTW_DX 55
22448#define TEMPLATE_OUTW_IMM 56
22449#define TEMPLATE_OUTL_DX 57
22450#define TEMPLATE_OUTL_IMM 58
22451#define TEMPLATE_BSF 59
22452#define TEMPLATE_RDMSR 60
22453#define TEMPLATE_WRMSR 61
22454#define TEMPLATE_UMUL8 62
22455#define TEMPLATE_UMUL16 63
22456#define TEMPLATE_UMUL32 64
22457#define TEMPLATE_DIV8 65
22458#define TEMPLATE_DIV16 66
22459#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022460#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022461#if LAST_TEMPLATE >= MAX_TEMPLATES
22462#error "MAX_TEMPLATES to low"
22463#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022464
Eric Biederman530b5192003-07-01 10:05:30 +000022465#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22466#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
22467#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022468
Eric Biedermanb138ac82003-04-22 18:44:01 +000022469
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022470static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022471 [TEMPLATE_NOP] = {
22472 .lhs = {
22473 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22474 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22475 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22476 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22477 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22478 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22479 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22480 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22481 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22482 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22483 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22484 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22485 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22486 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22487 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22488 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22489 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22490 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22491 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22492 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22493 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22494 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22495 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22496 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22497 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22498 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22499 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22500 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22501 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22502 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22503 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22504 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22505 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22506 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22507 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22508 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22509 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22510 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22511 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22512 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22513 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22514 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22515 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22516 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22517 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22518 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22519 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22520 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22521 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22522 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22523 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22524 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22525 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22526 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22527 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22528 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22529 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22530 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22531 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22532 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22533 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22534 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22535 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22536 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22537 },
22538 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022539 [TEMPLATE_INTCONST8] = {
22540 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22541 },
22542 [TEMPLATE_INTCONST32] = {
22543 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22544 },
Eric Biederman90089602004-05-28 14:11:54 +000022545 [TEMPLATE_UNKNOWNVAL] = {
22546 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22547 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022548 [TEMPLATE_COPY8_REG] = {
22549 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22550 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022551 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022552 [TEMPLATE_COPY16_REG] = {
22553 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22554 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22555 },
22556 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022557 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022558 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022559 },
22560 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022561 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022562 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22563 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022564 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022565 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022566 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22567 },
22568 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022569 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022570 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22571 },
22572 [TEMPLATE_PHI8] = {
22573 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022574 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22575 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022576 [TEMPLATE_PHI16] = {
22577 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022578 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22579 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022580 [TEMPLATE_PHI32] = {
22581 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022582 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22583 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022584 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022585 .rhs = {
22586 [0] = { REG_UNSET, REGCM_GPR32 },
22587 [1] = { REG_UNSET, REGCM_GPR8_LO },
22588 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022589 },
22590 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022591 .rhs = {
22592 [0] = { REG_UNSET, REGCM_GPR32 },
22593 [1] = { REG_UNSET, REGCM_GPR16 },
22594 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022595 },
22596 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022597 .rhs = {
22598 [0] = { REG_UNSET, REGCM_GPR32 },
22599 [1] = { REG_UNSET, REGCM_GPR32 },
22600 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022601 },
22602 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022603 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022604 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22605 },
22606 [TEMPLATE_LOAD16] = {
22607 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22608 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22609 },
22610 [TEMPLATE_LOAD32] = {
22611 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22612 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22613 },
Eric Biederman530b5192003-07-01 10:05:30 +000022614 [TEMPLATE_BINARY8_REG] = {
22615 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22616 .rhs = {
22617 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22618 [1] = { REG_UNSET, REGCM_GPR8_LO },
22619 },
22620 },
22621 [TEMPLATE_BINARY16_REG] = {
22622 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22623 .rhs = {
22624 [0] = { REG_VIRT0, REGCM_GPR16 },
22625 [1] = { REG_UNSET, REGCM_GPR16 },
22626 },
22627 },
22628 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022629 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22630 .rhs = {
22631 [0] = { REG_VIRT0, REGCM_GPR32 },
22632 [1] = { REG_UNSET, REGCM_GPR32 },
22633 },
22634 },
Eric Biederman530b5192003-07-01 10:05:30 +000022635 [TEMPLATE_BINARY8_IMM] = {
22636 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22637 .rhs = {
22638 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22639 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22640 },
22641 },
22642 [TEMPLATE_BINARY16_IMM] = {
22643 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22644 .rhs = {
22645 [0] = { REG_VIRT0, REGCM_GPR16 },
22646 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22647 },
22648 },
22649 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022650 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22651 .rhs = {
22652 [0] = { REG_VIRT0, REGCM_GPR32 },
22653 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22654 },
22655 },
Eric Biederman530b5192003-07-01 10:05:30 +000022656 [TEMPLATE_SL8_CL] = {
22657 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22658 .rhs = {
22659 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22660 [1] = { REG_CL, REGCM_GPR8_LO },
22661 },
22662 },
22663 [TEMPLATE_SL16_CL] = {
22664 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22665 .rhs = {
22666 [0] = { REG_VIRT0, REGCM_GPR16 },
22667 [1] = { REG_CL, REGCM_GPR8_LO },
22668 },
22669 },
22670 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022671 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22672 .rhs = {
22673 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022674 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022675 },
22676 },
Eric Biederman530b5192003-07-01 10:05:30 +000022677 [TEMPLATE_SL8_IMM] = {
22678 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22679 .rhs = {
22680 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22681 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22682 },
22683 },
22684 [TEMPLATE_SL16_IMM] = {
22685 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22686 .rhs = {
22687 [0] = { REG_VIRT0, REGCM_GPR16 },
22688 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22689 },
22690 },
22691 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022692 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22693 .rhs = {
22694 [0] = { REG_VIRT0, REGCM_GPR32 },
22695 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22696 },
22697 },
Eric Biederman530b5192003-07-01 10:05:30 +000022698 [TEMPLATE_UNARY8] = {
22699 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22700 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22701 },
22702 [TEMPLATE_UNARY16] = {
22703 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22704 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22705 },
22706 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022707 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22708 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22709 },
Eric Biederman530b5192003-07-01 10:05:30 +000022710 [TEMPLATE_CMP8_REG] = {
22711 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22712 .rhs = {
22713 [0] = { REG_UNSET, REGCM_GPR8_LO },
22714 [1] = { REG_UNSET, REGCM_GPR8_LO },
22715 },
22716 },
22717 [TEMPLATE_CMP16_REG] = {
22718 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22719 .rhs = {
22720 [0] = { REG_UNSET, REGCM_GPR16 },
22721 [1] = { REG_UNSET, REGCM_GPR16 },
22722 },
22723 },
22724 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022725 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22726 .rhs = {
22727 [0] = { REG_UNSET, REGCM_GPR32 },
22728 [1] = { REG_UNSET, REGCM_GPR32 },
22729 },
22730 },
Eric Biederman530b5192003-07-01 10:05:30 +000022731 [TEMPLATE_CMP8_IMM] = {
22732 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22733 .rhs = {
22734 [0] = { REG_UNSET, REGCM_GPR8_LO },
22735 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22736 },
22737 },
22738 [TEMPLATE_CMP16_IMM] = {
22739 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22740 .rhs = {
22741 [0] = { REG_UNSET, REGCM_GPR16 },
22742 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22743 },
22744 },
22745 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022746 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22747 .rhs = {
22748 [0] = { REG_UNSET, REGCM_GPR32 },
22749 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22750 },
22751 },
Eric Biederman530b5192003-07-01 10:05:30 +000022752 [TEMPLATE_TEST8] = {
22753 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22754 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22755 },
22756 [TEMPLATE_TEST16] = {
22757 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22758 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22759 },
22760 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022761 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22762 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22763 },
22764 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022765 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022766 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22767 },
22768 [TEMPLATE_JMP] = {
22769 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22770 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022771 [TEMPLATE_RET] = {
22772 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22773 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022774 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022775 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022776 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22777 },
22778 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022779 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022780 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22781 },
22782 [TEMPLATE_INW_DX] = {
22783 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22784 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22785 },
22786 [TEMPLATE_INW_IMM] = {
22787 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22788 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22789 },
22790 [TEMPLATE_INL_DX] = {
22791 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22792 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22793 },
22794 [TEMPLATE_INL_IMM] = {
22795 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22796 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22797 },
22798 [TEMPLATE_OUTB_DX] = {
22799 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022800 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022801 [1] = { REG_DX, REGCM_GPR16 },
22802 },
22803 },
22804 [TEMPLATE_OUTB_IMM] = {
22805 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022806 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022807 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22808 },
22809 },
22810 [TEMPLATE_OUTW_DX] = {
22811 .rhs = {
22812 [0] = { REG_AX, REGCM_GPR16 },
22813 [1] = { REG_DX, REGCM_GPR16 },
22814 },
22815 },
22816 [TEMPLATE_OUTW_IMM] = {
22817 .rhs = {
22818 [0] = { REG_AX, REGCM_GPR16 },
22819 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22820 },
22821 },
22822 [TEMPLATE_OUTL_DX] = {
22823 .rhs = {
22824 [0] = { REG_EAX, REGCM_GPR32 },
22825 [1] = { REG_DX, REGCM_GPR16 },
22826 },
22827 },
22828 [TEMPLATE_OUTL_IMM] = {
22829 .rhs = {
22830 [0] = { REG_EAX, REGCM_GPR32 },
22831 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22832 },
22833 },
22834 [TEMPLATE_BSF] = {
22835 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22836 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22837 },
22838 [TEMPLATE_RDMSR] = {
22839 .lhs = {
22840 [0] = { REG_EAX, REGCM_GPR32 },
22841 [1] = { REG_EDX, REGCM_GPR32 },
22842 },
22843 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22844 },
22845 [TEMPLATE_WRMSR] = {
22846 .rhs = {
22847 [0] = { REG_ECX, REGCM_GPR32 },
22848 [1] = { REG_EAX, REGCM_GPR32 },
22849 [2] = { REG_EDX, REGCM_GPR32 },
22850 },
22851 },
Eric Biederman530b5192003-07-01 10:05:30 +000022852 [TEMPLATE_UMUL8] = {
22853 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22854 .rhs = {
22855 [0] = { REG_AL, REGCM_GPR8_LO },
22856 [1] = { REG_UNSET, REGCM_GPR8_LO },
22857 },
22858 },
22859 [TEMPLATE_UMUL16] = {
22860 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22861 .rhs = {
22862 [0] = { REG_AX, REGCM_GPR16 },
22863 [1] = { REG_UNSET, REGCM_GPR16 },
22864 },
22865 },
22866 [TEMPLATE_UMUL32] = {
22867 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022868 .rhs = {
22869 [0] = { REG_EAX, REGCM_GPR32 },
22870 [1] = { REG_UNSET, REGCM_GPR32 },
22871 },
22872 },
Eric Biederman530b5192003-07-01 10:05:30 +000022873 [TEMPLATE_DIV8] = {
22874 .lhs = {
22875 [0] = { REG_AL, REGCM_GPR8_LO },
22876 [1] = { REG_AH, REGCM_GPR8 },
22877 },
22878 .rhs = {
22879 [0] = { REG_AX, REGCM_GPR16 },
22880 [1] = { REG_UNSET, REGCM_GPR8_LO },
22881 },
22882 },
22883 [TEMPLATE_DIV16] = {
22884 .lhs = {
22885 [0] = { REG_AX, REGCM_GPR16 },
22886 [1] = { REG_DX, REGCM_GPR16 },
22887 },
22888 .rhs = {
22889 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22890 [1] = { REG_UNSET, REGCM_GPR16 },
22891 },
22892 },
22893 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022894 .lhs = {
22895 [0] = { REG_EAX, REGCM_GPR32 },
22896 [1] = { REG_EDX, REGCM_GPR32 },
22897 },
22898 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022899 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022900 [1] = { REG_UNSET, REGCM_GPR32 },
22901 },
22902 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022903};
Eric Biedermanb138ac82003-04-22 18:44:01 +000022904
Eric Biederman83b991a2003-10-11 06:20:25 +000022905static void fixup_branch(struct compile_state *state,
22906 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
22907 struct triple *left, struct triple *right)
22908{
22909 struct triple *test;
22910 if (!left) {
22911 internal_error(state, branch, "no branch test?");
22912 }
22913 test = pre_triple(state, branch,
22914 cmp_op, cmp_type, left, right);
22915 test->template_id = TEMPLATE_TEST32;
22916 if (cmp_op == OP_CMP) {
22917 test->template_id = TEMPLATE_CMP32_REG;
22918 if (get_imm32(test, &RHS(test, 1))) {
22919 test->template_id = TEMPLATE_CMP32_IMM;
22920 }
22921 }
22922 use_triple(RHS(test, 0), test);
22923 use_triple(RHS(test, 1), test);
22924 unuse_triple(RHS(branch, 0), branch);
22925 RHS(branch, 0) = test;
22926 branch->op = jmp_op;
22927 branch->template_id = TEMPLATE_JMP;
22928 use_triple(RHS(branch, 0), branch);
22929}
22930
Eric Biedermanb138ac82003-04-22 18:44:01 +000022931static void fixup_branches(struct compile_state *state,
22932 struct triple *cmp, struct triple *use, int jmp_op)
22933{
22934 struct triple_set *entry, *next;
22935 for(entry = use->use; entry; entry = next) {
22936 next = entry->next;
22937 if (entry->member->op == OP_COPY) {
22938 fixup_branches(state, cmp, entry->member, jmp_op);
22939 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022940 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022941 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000022942 struct triple *left, *right;
22943 left = right = 0;
22944 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000022945 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022946 right = RHS(cmp, 1);
22947 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022948 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000022949 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000022950 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022951 }
22952 }
22953}
22954
22955static void bool_cmp(struct compile_state *state,
22956 struct triple *ins, int cmp_op, int jmp_op, int set_op)
22957{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022958 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000022959 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022960
22961 /* Put a barrier up before the cmp which preceeds the
22962 * copy instruction. If a set actually occurs this gives
22963 * us a chance to move variables in registers out of the way.
22964 */
22965
22966 /* Modify the comparison operator */
22967 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000022968 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022969 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000022970 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022971 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000022972 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022973 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022974 }
22975 /* Generate the instruction sequence that will transform the
22976 * result of the comparison into a logical value.
22977 */
Eric Biederman90089602004-05-28 14:11:54 +000022978 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022979 use_triple(ins, set);
22980 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022981
Eric Biederman90089602004-05-28 14:11:54 +000022982 convert = set;
22983 if (!equiv_types(ins->type, set->type)) {
22984 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
22985 use_triple(set, convert);
22986 convert->template_id = TEMPLATE_COPY32_REG;
22987 }
22988
Eric Biedermanb138ac82003-04-22 18:44:01 +000022989 for(entry = ins->use; entry; entry = next) {
22990 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022991 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022992 continue;
22993 }
Eric Biederman90089602004-05-28 14:11:54 +000022994 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022995 }
Eric Biederman90089602004-05-28 14:11:54 +000022996 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000022997}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022998
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022999struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23000{
23001 struct ins_template *template;
23002 struct reg_info result;
23003 int zlhs;
23004 if (ins->op == OP_PIECE) {
23005 index = ins->u.cval;
23006 ins = MISC(ins, 0);
23007 }
Eric Biederman90089602004-05-28 14:11:54 +000023008 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023009 if (triple_is_def(state, ins)) {
23010 zlhs = 1;
23011 }
23012 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023013 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023014 index, tops(ins->op));
23015 }
23016 switch(ins->op) {
23017 case OP_ASM:
23018 template = &ins->u.ainfo->tmpl;
23019 break;
23020 default:
23021 if (ins->template_id > LAST_TEMPLATE) {
23022 internal_error(state, ins, "bad template number %d",
23023 ins->template_id);
23024 }
23025 template = &templates[ins->template_id];
23026 break;
23027 }
23028 result = template->lhs[index];
23029 result.regcm = arch_regcm_normalize(state, result.regcm);
23030 if (result.reg != REG_UNNEEDED) {
23031 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23032 }
23033 if (result.regcm == 0) {
23034 internal_error(state, ins, "lhs %d regcm == 0", index);
23035 }
23036 return result;
23037}
23038
23039struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23040{
23041 struct reg_info result;
23042 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023043 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023044 (ins->op == OP_PIECE)) {
23045 internal_error(state, ins, "index %d out of range for %s\n",
23046 index, tops(ins->op));
23047 }
23048 switch(ins->op) {
23049 case OP_ASM:
23050 template = &ins->u.ainfo->tmpl;
23051 break;
Eric Biederman90089602004-05-28 14:11:54 +000023052 case OP_PHI:
23053 index = 0;
23054 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023055 default:
23056 if (ins->template_id > LAST_TEMPLATE) {
23057 internal_error(state, ins, "bad template number %d",
23058 ins->template_id);
23059 }
23060 template = &templates[ins->template_id];
23061 break;
23062 }
23063 result = template->rhs[index];
23064 result.regcm = arch_regcm_normalize(state, result.regcm);
23065 if (result.regcm == 0) {
23066 internal_error(state, ins, "rhs %d regcm == 0", index);
23067 }
23068 return result;
23069}
23070
Eric Biederman530b5192003-07-01 10:05:30 +000023071static struct triple *mod_div(struct compile_state *state,
23072 struct triple *ins, int div_op, int index)
23073{
23074 struct triple *div, *piece0, *piece1;
23075
Eric Biederman530b5192003-07-01 10:05:30 +000023076 /* Generate the appropriate division instruction */
23077 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23078 RHS(div, 0) = RHS(ins, 0);
23079 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023080 piece0 = LHS(div, 0);
23081 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023082 div->template_id = TEMPLATE_DIV32;
23083 use_triple(RHS(div, 0), div);
23084 use_triple(RHS(div, 1), div);
23085 use_triple(LHS(div, 0), div);
23086 use_triple(LHS(div, 1), div);
23087
Eric Biederman530b5192003-07-01 10:05:30 +000023088 /* Replate uses of ins with the appropriate piece of the div */
23089 propogate_use(state, ins, LHS(div, index));
23090 release_triple(state, ins);
23091
23092 /* Return the address of the next instruction */
23093 return piece1->next;
23094}
23095
Eric Biederman90089602004-05-28 14:11:54 +000023096static int noop_adecl(struct triple *adecl)
23097{
23098 struct triple_set *use;
23099 /* It's a noop if it doesn't specify stoorage */
23100 if (adecl->lhs == 0) {
23101 return 1;
23102 }
23103 /* Is the adecl used? If not it's a noop */
23104 for(use = adecl->use; use ; use = use->next) {
23105 if ((use->member->op != OP_PIECE) ||
23106 (MISC(use->member, 0) != adecl)) {
23107 return 0;
23108 }
23109 }
23110 return 1;
23111}
23112
23113static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23114{
23115 struct triple *mask, *nmask, *shift;
23116 struct triple *val, *val_mask, *val_shift;
23117 struct triple *targ, *targ_mask;
23118 struct triple *new;
23119 ulong_t the_mask, the_nmask;
23120
23121 targ = RHS(ins, 0);
23122 val = RHS(ins, 1);
23123
23124 /* Get constant for the mask value */
23125 the_mask = 1;
23126 the_mask <<= ins->u.bitfield.size;
23127 the_mask -= 1;
23128 the_mask <<= ins->u.bitfield.offset;
23129 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23130 mask->u.cval = the_mask;
23131
23132 /* Get the inverted mask value */
23133 the_nmask = ~the_mask;
23134 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23135 nmask->u.cval = the_nmask;
23136
23137 /* Get constant for the shift value */
23138 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23139 shift->u.cval = ins->u.bitfield.offset;
23140
23141 /* Shift and mask the source value */
23142 val_shift = val;
23143 if (shift->u.cval != 0) {
23144 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23145 use_triple(val, val_shift);
23146 use_triple(shift, val_shift);
23147 }
23148 val_mask = val_shift;
23149 if (is_signed(val->type)) {
23150 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23151 use_triple(val_shift, val_mask);
23152 use_triple(mask, val_mask);
23153 }
23154
23155 /* Mask the target value */
23156 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23157 use_triple(targ, targ_mask);
23158 use_triple(nmask, targ_mask);
23159
23160 /* Now combined them together */
23161 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23162 use_triple(targ_mask, new);
23163 use_triple(val_mask, new);
23164
23165 /* Move all of the users over to the new expression */
23166 propogate_use(state, ins, new);
23167
23168 /* Delete the original triple */
23169 release_triple(state, ins);
23170
23171 /* Restart the transformation at mask */
23172 return mask;
23173}
23174
23175static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23176{
23177 struct triple *mask, *shift;
23178 struct triple *val, *val_mask, *val_shift;
23179 ulong_t the_mask;
23180
23181 val = RHS(ins, 0);
23182
23183 /* Get constant for the mask value */
23184 the_mask = 1;
23185 the_mask <<= ins->u.bitfield.size;
23186 the_mask -= 1;
23187 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23188 mask->u.cval = the_mask;
23189
23190 /* Get constant for the right shift value */
23191 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23192 shift->u.cval = ins->u.bitfield.offset;
23193
23194 /* Shift arithmetic right, to correct the sign */
23195 val_shift = val;
23196 if (shift->u.cval != 0) {
23197 int op;
23198 if (ins->op == OP_SEXTRACT) {
23199 op = OP_SSR;
23200 } else {
23201 op = OP_USR;
23202 }
23203 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23204 use_triple(val, val_shift);
23205 use_triple(shift, val_shift);
23206 }
23207
23208 /* Finally mask the value */
23209 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23210 use_triple(val_shift, val_mask);
23211 use_triple(mask, val_mask);
23212
23213 /* Move all of the users over to the new expression */
23214 propogate_use(state, ins, val_mask);
23215
23216 /* Release the original instruction */
23217 release_triple(state, ins);
23218
23219 return mask;
23220
23221}
23222
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023223static struct triple *transform_to_arch_instruction(
23224 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023225{
23226 /* Transform from generic 3 address instructions
23227 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023228 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023229 * Copies are inserted to preserve the register flexibility
23230 * of 3 address instructions.
23231 */
Eric Biederman90089602004-05-28 14:11:54 +000023232 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023233 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023234 next = ins->next;
23235 switch(ins->op) {
23236 case OP_INTCONST:
23237 ins->template_id = TEMPLATE_INTCONST32;
23238 if (ins->u.cval < 256) {
23239 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023240 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023241 break;
23242 case OP_ADDRCONST:
23243 ins->template_id = TEMPLATE_INTCONST32;
23244 break;
Eric Biederman90089602004-05-28 14:11:54 +000023245 case OP_UNKNOWNVAL:
23246 ins->template_id = TEMPLATE_UNKNOWNVAL;
23247 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023248 case OP_NOOP:
23249 case OP_SDECL:
23250 case OP_BLOBCONST:
23251 case OP_LABEL:
23252 ins->template_id = TEMPLATE_NOP;
23253 break;
23254 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023255 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023256 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023257 value = RHS(ins, 0);
23258 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023259 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023260 }
Eric Biederman90089602004-05-28 14:11:54 +000023261 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023262 ins->template_id = TEMPLATE_COPY_IMM16;
23263 }
Eric Biederman90089602004-05-28 14:11:54 +000023264 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023265 ins->template_id = TEMPLATE_COPY_IMM32;
23266 }
Eric Biederman90089602004-05-28 14:11:54 +000023267 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023268 internal_error(state, ins, "bad constant passed to copy");
23269 }
Eric Biederman90089602004-05-28 14:11:54 +000023270 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023271 ins->template_id = TEMPLATE_COPY8_REG;
23272 }
Eric Biederman90089602004-05-28 14:11:54 +000023273 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023274 ins->template_id = TEMPLATE_COPY16_REG;
23275 }
Eric Biederman90089602004-05-28 14:11:54 +000023276 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023277 ins->template_id = TEMPLATE_COPY32_REG;
23278 }
23279 else {
23280 internal_error(state, ins, "bad type passed to copy");
23281 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023282 break;
23283 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023284 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023285 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023286 ins->template_id = TEMPLATE_PHI8;
23287 }
Eric Biederman90089602004-05-28 14:11:54 +000023288 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023289 ins->template_id = TEMPLATE_PHI16;
23290 }
Eric Biederman90089602004-05-28 14:11:54 +000023291 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023292 ins->template_id = TEMPLATE_PHI32;
23293 }
23294 else {
23295 internal_error(state, ins, "bad type passed to phi");
23296 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023297 break;
Eric Biederman90089602004-05-28 14:11:54 +000023298 case OP_ADECL:
23299 /* Adecls should always be treated as dead code and
23300 * removed. If we are not optimizing they may linger.
23301 */
23302 if (!noop_adecl(ins)) {
23303 internal_error(state, ins, "adecl remains?");
23304 }
23305 ins->template_id = TEMPLATE_NOP;
23306 next = after_lhs(state, ins);
23307 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023308 case OP_STORE:
23309 switch(ins->type->type & TYPE_MASK) {
23310 case TYPE_CHAR: case TYPE_UCHAR:
23311 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023312 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023313 case TYPE_SHORT: case TYPE_USHORT:
23314 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023315 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023316 case TYPE_INT: case TYPE_UINT:
23317 case TYPE_LONG: case TYPE_ULONG:
23318 case TYPE_POINTER:
23319 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023320 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023321 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023322 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023323 break;
23324 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023325 break;
23326 case OP_LOAD:
23327 switch(ins->type->type & TYPE_MASK) {
23328 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023329 case TYPE_SHORT: case TYPE_USHORT:
23330 case TYPE_INT: case TYPE_UINT:
23331 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023332 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023333 break;
23334 default:
23335 internal_error(state, ins, "unknown type in load");
23336 break;
23337 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023338 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023339 break;
23340 case OP_ADD:
23341 case OP_SUB:
23342 case OP_AND:
23343 case OP_XOR:
23344 case OP_OR:
23345 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023346 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023347 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023348 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023349 }
23350 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023351 case OP_SDIVT:
23352 case OP_UDIVT:
23353 ins->template_id = TEMPLATE_DIV32;
23354 next = after_lhs(state, ins);
23355 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023356 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023357 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023358 break;
23359 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023360 next = mod_div(state, ins, OP_UDIVT, 0);
23361 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023362 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023363 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023364 break;
23365 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023366 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023367 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023368 case OP_SMOD:
23369 next = mod_div(state, ins, OP_SDIVT, 1);
23370 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023371 case OP_SL:
23372 case OP_SSR:
23373 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023374 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023375 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023376 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023377 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23378 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023379 }
23380 break;
23381 case OP_INVERT:
23382 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023383 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023384 break;
23385 case OP_EQ:
23386 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
23387 break;
23388 case OP_NOTEQ:
23389 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23390 break;
23391 case OP_SLESS:
23392 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23393 break;
23394 case OP_ULESS:
23395 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23396 break;
23397 case OP_SMORE:
23398 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23399 break;
23400 case OP_UMORE:
23401 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23402 break;
23403 case OP_SLESSEQ:
23404 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23405 break;
23406 case OP_ULESSEQ:
23407 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23408 break;
23409 case OP_SMOREEQ:
23410 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23411 break;
23412 case OP_UMOREEQ:
23413 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23414 break;
23415 case OP_LTRUE:
23416 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23417 break;
23418 case OP_LFALSE:
23419 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23420 break;
23421 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023422 ins->op = OP_JMP;
23423 ins->template_id = TEMPLATE_NOP;
23424 break;
23425 case OP_CBRANCH:
23426 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
23427 RHS(ins, 0)->type, RHS(ins, 0), 0);
23428 break;
23429 case OP_CALL:
23430 ins->template_id = TEMPLATE_NOP;
23431 break;
23432 case OP_RET:
23433 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023434 break;
23435 case OP_INB:
23436 case OP_INW:
23437 case OP_INL:
23438 switch(ins->op) {
23439 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23440 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23441 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23442 }
23443 if (get_imm8(ins, &RHS(ins, 0))) {
23444 ins->template_id += 1;
23445 }
23446 break;
23447 case OP_OUTB:
23448 case OP_OUTW:
23449 case OP_OUTL:
23450 switch(ins->op) {
23451 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23452 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23453 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23454 }
23455 if (get_imm8(ins, &RHS(ins, 1))) {
23456 ins->template_id += 1;
23457 }
23458 break;
23459 case OP_BSF:
23460 case OP_BSR:
23461 ins->template_id = TEMPLATE_BSF;
23462 break;
23463 case OP_RDMSR:
23464 ins->template_id = TEMPLATE_RDMSR;
23465 next = after_lhs(state, ins);
23466 break;
23467 case OP_WRMSR:
23468 ins->template_id = TEMPLATE_WRMSR;
23469 break;
23470 case OP_HLT:
23471 ins->template_id = TEMPLATE_NOP;
23472 break;
23473 case OP_ASM:
23474 ins->template_id = TEMPLATE_NOP;
23475 next = after_lhs(state, ins);
23476 break;
23477 /* Already transformed instructions */
23478 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023479 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023480 break;
23481 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023482 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023483 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023484 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023485 }
23486 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023487 case OP_JMP:
23488 ins->template_id = TEMPLATE_NOP;
23489 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023490 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23491 case OP_JMP_SLESS: case OP_JMP_ULESS:
23492 case OP_JMP_SMORE: case OP_JMP_UMORE:
23493 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23494 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23495 ins->template_id = TEMPLATE_JMP;
23496 break;
23497 case OP_SET_EQ: case OP_SET_NOTEQ:
23498 case OP_SET_SLESS: case OP_SET_ULESS:
23499 case OP_SET_SMORE: case OP_SET_UMORE:
23500 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23501 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23502 ins->template_id = TEMPLATE_SET;
23503 break;
Eric Biederman90089602004-05-28 14:11:54 +000023504 case OP_DEPOSIT:
23505 next = x86_deposit(state, ins);
23506 break;
23507 case OP_SEXTRACT:
23508 case OP_UEXTRACT:
23509 next = x86_extract(state, ins);
23510 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023511 /* Unhandled instructions */
23512 case OP_PIECE:
23513 default:
Eric Biederman90089602004-05-28 14:11:54 +000023514 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023515 ins->op, tops(ins->op));
23516 break;
23517 }
23518 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023519}
23520
Eric Biederman530b5192003-07-01 10:05:30 +000023521static long next_label(struct compile_state *state)
23522{
Eric Biederman90089602004-05-28 14:11:54 +000023523 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023524 return ++label_counter;
23525}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023526static void generate_local_labels(struct compile_state *state)
23527{
23528 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023529 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023530 label = first;
23531 do {
23532 if ((label->op == OP_LABEL) ||
23533 (label->op == OP_SDECL)) {
23534 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023535 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023536 } else {
23537 label->u.cval = 0;
23538 }
23539
23540 }
23541 label = label->next;
23542 } while(label != first);
23543}
23544
23545static int check_reg(struct compile_state *state,
23546 struct triple *triple, int classes)
23547{
23548 unsigned mask;
23549 int reg;
23550 reg = ID_REG(triple->id);
23551 if (reg == REG_UNSET) {
23552 internal_error(state, triple, "register not set");
23553 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023554 mask = arch_reg_regcm(state, reg);
23555 if (!(classes & mask)) {
23556 internal_error(state, triple, "reg %d in wrong class",
23557 reg);
23558 }
23559 return reg;
23560}
23561
Eric Biederman90089602004-05-28 14:11:54 +000023562
Eric Biederman530b5192003-07-01 10:05:30 +000023563#if REG_XMM7 != 44
23564#error "Registers have renumberd fix arch_reg_str"
23565#endif
Eric Biederman90089602004-05-28 14:11:54 +000023566static const char *arch_regs[] = {
23567 "%unset",
23568 "%unneeded",
23569 "%eflags",
23570 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23571 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23572 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23573 "%edx:%eax",
23574 "%dx:%ax",
23575 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23576 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
23577 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23578};
23579static const char *arch_reg_str(int reg)
23580{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023581 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23582 reg = 0;
23583 }
Eric Biederman90089602004-05-28 14:11:54 +000023584 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023585}
23586
23587static const char *reg(struct compile_state *state, struct triple *triple,
23588 int classes)
23589{
23590 int reg;
23591 reg = check_reg(state, triple, classes);
23592 return arch_reg_str(reg);
23593}
23594
Eric Biederman90089602004-05-28 14:11:54 +000023595static int arch_reg_size(int reg)
23596{
23597 int size;
23598 size = 0;
23599 if (reg == REG_EFLAGS) {
23600 size = 32;
23601 }
23602 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23603 size = 8;
23604 }
23605 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23606 size = 16;
23607 }
23608 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23609 size = 32;
23610 }
23611 else if (reg == REG_EDXEAX) {
23612 size = 64;
23613 }
23614 else if (reg == REG_DXAX) {
23615 size = 32;
23616 }
23617 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23618 size = 64;
23619 }
23620 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23621 size = 128;
23622 }
23623 return size;
23624}
23625
23626static int reg_size(struct compile_state *state, struct triple *ins)
23627{
23628 int reg;
23629 reg = ID_REG(ins->id);
23630 if (reg == REG_UNSET) {
23631 internal_error(state, ins, "register not set");
23632 }
23633 return arch_reg_size(reg);
23634}
23635
23636
23637
Eric Biedermanb138ac82003-04-22 18:44:01 +000023638const char *type_suffix(struct compile_state *state, struct type *type)
23639{
23640 const char *suffix;
23641 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023642 case SIZEOF_I8: suffix = "b"; break;
23643 case SIZEOF_I16: suffix = "w"; break;
23644 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023645 default:
23646 internal_error(state, 0, "unknown suffix");
23647 suffix = 0;
23648 break;
23649 }
23650 return suffix;
23651}
23652
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023653static void print_const_val(
23654 struct compile_state *state, struct triple *ins, FILE *fp)
23655{
23656 switch(ins->op) {
23657 case OP_INTCONST:
23658 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023659 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023660 break;
23661 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023662 if ((MISC(ins, 0)->op != OP_SDECL) &&
23663 (MISC(ins, 0)->op != OP_LABEL))
23664 {
Eric Biederman830c9882003-07-04 00:27:33 +000023665 internal_error(state, ins, "bad base for addrconst");
23666 }
23667 if (MISC(ins, 0)->u.cval <= 0) {
23668 internal_error(state, ins, "unlabeled constant");
23669 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023670 fprintf(fp, " $L%s%lu+%lu ",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023671 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023672 (unsigned long)(MISC(ins, 0)->u.cval),
23673 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023674 break;
23675 default:
23676 internal_error(state, ins, "unknown constant type");
23677 break;
23678 }
23679}
23680
Eric Biederman530b5192003-07-01 10:05:30 +000023681static void print_const(struct compile_state *state,
23682 struct triple *ins, FILE *fp)
23683{
23684 switch(ins->op) {
23685 case OP_INTCONST:
23686 switch(ins->type->type & TYPE_MASK) {
23687 case TYPE_CHAR:
23688 case TYPE_UCHAR:
Eric Biederman83b991a2003-10-11 06:20:25 +000023689 fprintf(fp, ".byte 0x%02lx\n",
23690 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023691 break;
23692 case TYPE_SHORT:
23693 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000023694 fprintf(fp, ".short 0x%04lx\n",
23695 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023696 break;
23697 case TYPE_INT:
23698 case TYPE_UINT:
23699 case TYPE_LONG:
23700 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023701 case TYPE_POINTER:
Eric Biederman83b991a2003-10-11 06:20:25 +000023702 fprintf(fp, ".int %lu\n",
23703 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023704 break;
23705 default:
Eric Biederman90089602004-05-28 14:11:54 +000023706 fprintf(state->errout, "type: ");
23707 name_of(state->errout, ins->type);
23708 fprintf(state->errout, "\n");
23709 internal_error(state, ins, "Unknown constant type. Val: %lu",
23710 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023711 }
Eric Biederman90089602004-05-28 14:11:54 +000023712
Eric Biederman530b5192003-07-01 10:05:30 +000023713 break;
23714 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023715 if ((MISC(ins, 0)->op != OP_SDECL) &&
23716 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023717 internal_error(state, ins, "bad base for addrconst");
23718 }
23719 if (MISC(ins, 0)->u.cval <= 0) {
23720 internal_error(state, ins, "unlabeled constant");
23721 }
23722 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023723 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023724 (unsigned long)(MISC(ins, 0)->u.cval),
23725 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023726 break;
23727 case OP_BLOBCONST:
23728 {
23729 unsigned char *blob;
23730 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023731 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023732 blob = ins->u.blob;
23733 for(i = 0; i < size; i++) {
23734 fprintf(fp, ".byte 0x%02x\n",
23735 blob[i]);
23736 }
23737 break;
23738 }
23739 default:
23740 internal_error(state, ins, "Unknown constant type");
23741 break;
23742 }
23743}
23744
23745#define TEXT_SECTION ".rom.text"
23746#define DATA_SECTION ".rom.data"
23747
23748static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023749 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023750{
Eric Biederman90089602004-05-28 14:11:54 +000023751 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023752 long ref;
23753 ref = next_label(state);
23754 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biederman90089602004-05-28 14:11:54 +000023755 fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023756 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023757 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023758 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23759 if (fill_bytes) {
23760 fprintf(fp, ".fill %d, 1, 0\n", fill_bytes);
23761 }
Eric Biederman530b5192003-07-01 10:05:30 +000023762 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23763 return ref;
23764}
23765
Eric Biederman90089602004-05-28 14:11:54 +000023766static long get_mask_pool_ref(
23767 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23768{
23769 long ref;
23770 if (mask == 0xff) {
23771 ref = 1;
23772 }
23773 else if (mask == 0xffff) {
23774 ref = 2;
23775 }
23776 else {
23777 ref = 0;
23778 internal_error(state, ins, "unhandled mask value");
23779 }
23780 return ref;
23781}
23782
Eric Biedermanb138ac82003-04-22 18:44:01 +000023783static void print_binary_op(struct compile_state *state,
23784 const char *op, struct triple *ins, FILE *fp)
23785{
23786 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023787 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023788 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023789 internal_error(state, ins, "invalid register assignment");
23790 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023791 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023792 fprintf(fp, "\t%s ", op);
23793 print_const_val(state, RHS(ins, 1), fp);
23794 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023795 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023796 }
23797 else {
23798 unsigned lmask, rmask;
23799 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023800 lreg = check_reg(state, RHS(ins, 0), mask);
23801 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023802 lmask = arch_reg_regcm(state, lreg);
23803 rmask = arch_reg_regcm(state, rreg);
23804 mask = lmask & rmask;
23805 fprintf(fp, "\t%s %s, %s\n",
23806 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023807 reg(state, RHS(ins, 1), mask),
23808 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023809 }
23810}
23811static void print_unary_op(struct compile_state *state,
23812 const char *op, struct triple *ins, FILE *fp)
23813{
23814 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023815 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023816 fprintf(fp, "\t%s %s\n",
23817 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023818 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023819}
23820
23821static void print_op_shift(struct compile_state *state,
23822 const char *op, struct triple *ins, FILE *fp)
23823{
23824 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023825 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023826 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023827 internal_error(state, ins, "invalid register assignment");
23828 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023829 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023830 fprintf(fp, "\t%s ", op);
23831 print_const_val(state, RHS(ins, 1), fp);
23832 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023833 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023834 }
23835 else {
23836 fprintf(fp, "\t%s %s, %s\n",
23837 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023838 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023839 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023840 }
23841}
23842
23843static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23844{
23845 const char *op;
23846 int mask;
23847 int dreg;
23848 mask = 0;
23849 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023850 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023851 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23852 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23853 default:
23854 internal_error(state, ins, "not an in operation");
23855 op = 0;
23856 break;
23857 }
23858 dreg = check_reg(state, ins, mask);
23859 if (!reg_is_reg(state, dreg, REG_EAX)) {
23860 internal_error(state, ins, "dst != %%eax");
23861 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023862 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023863 fprintf(fp, "\t%s ", op);
23864 print_const_val(state, RHS(ins, 0), fp);
23865 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023866 reg(state, ins, mask));
23867 }
23868 else {
23869 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023870 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023871 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23872 internal_error(state, ins, "src != %%dx");
23873 }
23874 fprintf(fp, "\t%s %s, %s\n",
23875 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023876 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023877 reg(state, ins, mask));
23878 }
23879}
23880
23881static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23882{
23883 const char *op;
23884 int mask;
23885 int lreg;
23886 mask = 0;
23887 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023888 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023889 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23890 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23891 default:
23892 internal_error(state, ins, "not an out operation");
23893 op = 0;
23894 break;
23895 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023896 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023897 if (!reg_is_reg(state, lreg, REG_EAX)) {
23898 internal_error(state, ins, "src != %%eax");
23899 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023900 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023901 fprintf(fp, "\t%s %s,",
23902 op, reg(state, RHS(ins, 0), mask));
23903 print_const_val(state, RHS(ins, 1), fp);
23904 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023905 }
23906 else {
23907 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023908 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023909 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23910 internal_error(state, ins, "dst != %%dx");
23911 }
23912 fprintf(fp, "\t%s %s, %s\n",
23913 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023914 reg(state, RHS(ins, 0), mask),
23915 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023916 }
23917}
23918
23919static void print_op_move(struct compile_state *state,
23920 struct triple *ins, FILE *fp)
23921{
23922 /* op_move is complex because there are many types
23923 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023924 * Because OP_COPY will be introduced in arbitrary locations
23925 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000023926 * OP_CONVERT can change the flags and it is the only operation
23927 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023928 */
23929 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
23930 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000023931 if (state->arch->features & X86_NOOP_COPY) {
23932 omit_copy = 0;
23933 }
23934 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023935 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023936 dst = ins;
23937 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023938 else {
23939 internal_error(state, ins, "unknown move operation");
23940 src = dst = 0;
23941 }
Eric Biederman90089602004-05-28 14:11:54 +000023942 if (reg_size(state, dst) < size_of(state, dst->type)) {
23943 internal_error(state, ins, "Invalid destination register");
23944 }
23945 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
23946 fprintf(state->errout, "src type: ");
23947 name_of(state->errout, src->type);
23948 fprintf(state->errout, "\n");
23949 fprintf(state->errout, "dst type: ");
23950 name_of(state->errout, dst->type);
23951 fprintf(state->errout, "\n");
23952 internal_error(state, ins, "Type mismatch for OP_COPY");
23953 }
23954
Eric Biederman0babc1c2003-05-09 02:39:00 +000023955 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023956 int src_reg, dst_reg;
23957 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000023958 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023959 dst_reg = ID_REG(dst->id);
23960 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000023961 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023962 /* If the class is the same just move the register */
23963 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000023964 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023965 if ((src_reg != dst_reg) || !omit_copy) {
23966 fprintf(fp, "\tmov %s, %s\n",
23967 reg(state, src, src_regcm),
23968 reg(state, dst, dst_regcm));
23969 }
23970 }
23971 /* Move 32bit to 16bit */
23972 else if ((src_regcm & REGCM_GPR32) &&
23973 (dst_regcm & REGCM_GPR16)) {
23974 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
23975 if ((src_reg != dst_reg) || !omit_copy) {
23976 fprintf(fp, "\tmovw %s, %s\n",
23977 arch_reg_str(src_reg),
23978 arch_reg_str(dst_reg));
23979 }
23980 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000023981 /* Move from 32bit gprs to 16bit gprs */
23982 else if ((src_regcm & REGCM_GPR32) &&
23983 (dst_regcm & REGCM_GPR16)) {
23984 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
23985 if ((src_reg != dst_reg) || !omit_copy) {
23986 fprintf(fp, "\tmov %s, %s\n",
23987 arch_reg_str(src_reg),
23988 arch_reg_str(dst_reg));
23989 }
23990 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023991 /* Move 32bit to 8bit */
23992 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000023993 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000023994 {
23995 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
23996 if ((src_reg != dst_reg) || !omit_copy) {
23997 fprintf(fp, "\tmovb %s, %s\n",
23998 arch_reg_str(src_reg),
23999 arch_reg_str(dst_reg));
24000 }
24001 }
24002 /* Move 16bit to 8bit */
24003 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024004 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024005 {
24006 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24007 if ((src_reg != dst_reg) || !omit_copy) {
24008 fprintf(fp, "\tmovb %s, %s\n",
24009 arch_reg_str(src_reg),
24010 arch_reg_str(dst_reg));
24011 }
24012 }
24013 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000024014 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024015 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024016 const char *op;
24017 op = is_signed(src->type)? "movsx": "movzx";
24018 fprintf(fp, "\t%s %s, %s\n",
24019 op,
24020 reg(state, src, src_regcm),
24021 reg(state, dst, dst_regcm));
24022 }
24023 /* Move between sse registers */
24024 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24025 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024026 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024027 reg(state, src, src_regcm),
24028 reg(state, dst, dst_regcm));
24029 }
24030 }
Eric Biederman530b5192003-07-01 10:05:30 +000024031 /* Move between mmx registers */
24032 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024033 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024034 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024035 reg(state, src, src_regcm),
24036 reg(state, dst, dst_regcm));
24037 }
24038 }
Eric Biederman530b5192003-07-01 10:05:30 +000024039 /* Move from sse to mmx registers */
24040 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24041 fprintf(fp, "\tmovdq2q %s, %s\n",
24042 reg(state, src, src_regcm),
24043 reg(state, dst, dst_regcm));
24044 }
24045 /* Move from mmx to sse registers */
24046 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24047 fprintf(fp, "\tmovq2dq %s, %s\n",
24048 reg(state, src, src_regcm),
24049 reg(state, dst, dst_regcm));
24050 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024051 /* Move between 32bit gprs & mmx/sse registers */
24052 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24053 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24054 fprintf(fp, "\tmovd %s, %s\n",
24055 reg(state, src, src_regcm),
24056 reg(state, dst, dst_regcm));
24057 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024058 /* Move from 16bit gprs & mmx/sse registers */
24059 else if ((src_regcm & REGCM_GPR16) &&
24060 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24061 const char *op;
24062 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024063 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024064 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24065 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24066 op,
24067 arch_reg_str(src_reg),
24068 arch_reg_str(mid_reg),
24069 arch_reg_str(mid_reg),
24070 arch_reg_str(dst_reg));
24071 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024072 /* Move from mmx/sse registers to 16bit gprs */
24073 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24074 (dst_regcm & REGCM_GPR16)) {
24075 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24076 fprintf(fp, "\tmovd %s, %s\n",
24077 arch_reg_str(src_reg),
24078 arch_reg_str(dst_reg));
24079 }
Eric Biederman530b5192003-07-01 10:05:30 +000024080 /* Move from gpr to 64bit dividend */
24081 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24082 (dst_regcm & REGCM_DIVIDEND64)) {
24083 const char *extend;
24084 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24085 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
24086 arch_reg_str(src_reg),
24087 extend);
24088 }
24089 /* Move from 64bit gpr to gpr */
24090 else if ((src_regcm & REGCM_DIVIDEND64) &&
24091 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24092 if (dst_regcm & REGCM_GPR32) {
24093 src_reg = REG_EAX;
24094 }
24095 else if (dst_regcm & REGCM_GPR16) {
24096 src_reg = REG_AX;
24097 }
24098 else if (dst_regcm & REGCM_GPR8_LO) {
24099 src_reg = REG_AL;
24100 }
24101 fprintf(fp, "\tmov %s, %s\n",
24102 arch_reg_str(src_reg),
24103 arch_reg_str(dst_reg));
24104 }
24105 /* Move from mmx/sse registers to 64bit gpr */
24106 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24107 (dst_regcm & REGCM_DIVIDEND64)) {
24108 const char *extend;
24109 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24110 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24111 arch_reg_str(src_reg),
24112 extend);
24113 }
24114 /* Move from 64bit gpr to mmx/sse register */
24115 else if ((src_regcm & REGCM_DIVIDEND64) &&
24116 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24117 fprintf(fp, "\tmovd %%eax, %s\n",
24118 arch_reg_str(dst_reg));
24119 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024120#if X86_4_8BIT_GPRS
24121 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024122 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024123 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24124 const char *op;
24125 int mid_reg;
24126 op = is_signed(src->type)? "movsx":"movzx";
24127 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24128 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24129 op,
24130 reg(state, src, src_regcm),
24131 arch_reg_str(mid_reg),
24132 arch_reg_str(mid_reg),
24133 reg(state, dst, dst_regcm));
24134 }
24135 /* Move from mmx/sse registers and 8bit gprs */
24136 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024137 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024138 int mid_reg;
24139 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24140 fprintf(fp, "\tmovd %s, %s\n",
24141 reg(state, src, src_regcm),
24142 arch_reg_str(mid_reg));
24143 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024144 /* Move from 32bit gprs to 8bit gprs */
24145 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024146 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024147 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24148 if ((src_reg != dst_reg) || !omit_copy) {
24149 fprintf(fp, "\tmov %s, %s\n",
24150 arch_reg_str(src_reg),
24151 arch_reg_str(dst_reg));
24152 }
24153 }
24154 /* Move from 16bit gprs to 8bit gprs */
24155 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024156 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024157 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24158 if ((src_reg != dst_reg) || !omit_copy) {
24159 fprintf(fp, "\tmov %s, %s\n",
24160 arch_reg_str(src_reg),
24161 arch_reg_str(dst_reg));
24162 }
24163 }
24164#endif /* X86_4_8BIT_GPRS */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024165 else {
Eric Biederman90089602004-05-28 14:11:54 +000024166 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24167 internal_error(state, ins, "attempt to copy from %%eflags!");
24168 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024169 internal_error(state, ins, "unknown copy type");
24170 }
24171 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024172 else {
Eric Biederman90089602004-05-28 14:11:54 +000024173 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024174 int dst_reg;
24175 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024176 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024177 dst_reg = ID_REG(dst->id);
24178 dst_regcm = arch_reg_regcm(state, dst_reg);
24179 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24180 fprintf(fp, "\tmov ");
24181 print_const_val(state, src, fp);
24182 fprintf(fp, ", %s\n",
24183 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24184 }
24185 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024186 if (dst_size > SIZEOF_I32) {
24187 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024188 }
24189 fprintf(fp, "\tmov $0, %%edx\n");
24190 fprintf(fp, "\tmov ");
24191 print_const_val(state, src, fp);
24192 fprintf(fp, ", %%eax\n");
24193 }
24194 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024195 if (dst_size > SIZEOF_I16) {
24196 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024197 }
24198 fprintf(fp, "\tmov $0, %%dx\n");
24199 fprintf(fp, "\tmov ");
24200 print_const_val(state, src, fp);
24201 fprintf(fp, ", %%ax");
24202 }
24203 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24204 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024205 if (dst_size > SIZEOF_I32) {
24206 internal_error(state, ins, "%d bit constant...", dst_size);
24207 }
24208 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024209 fprintf(fp, "\tmovd L%s%lu, %s\n",
24210 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024211 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24212 }
24213 else {
24214 internal_error(state, ins, "unknown copy immediate type");
24215 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024216 }
Eric Biederman90089602004-05-28 14:11:54 +000024217 /* Leave now if this is not a type conversion */
24218 if (ins->op != OP_CONVERT) {
24219 return;
24220 }
24221 /* Now make certain I have not logically overflowed the destination */
24222 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24223 (size_of(state, dst->type) < reg_size(state, dst)))
24224 {
24225 unsigned long mask;
24226 int dst_reg;
24227 int dst_regcm;
24228 if (size_of(state, dst->type) >= 32) {
24229 fprintf(state->errout, "dst type: ");
24230 name_of(state->errout, dst->type);
24231 fprintf(state->errout, "\n");
24232 internal_error(state, dst, "unhandled dst type size");
24233 }
24234 mask = 1;
24235 mask <<= size_of(state, dst->type);
24236 mask -= 1;
24237
24238 dst_reg = ID_REG(dst->id);
24239 dst_regcm = arch_reg_regcm(state, dst_reg);
24240
24241 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24242 fprintf(fp, "\tand $0x%lx, %s\n",
24243 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24244 }
24245 else if (dst_regcm & REGCM_MMX) {
24246 long ref;
24247 ref = get_mask_pool_ref(state, dst, mask, fp);
24248 fprintf(fp, "\tpand L%s%lu, %s\n",
24249 state->compiler->label_prefix, ref,
24250 reg(state, dst, REGCM_MMX));
24251 }
24252 else if (dst_regcm & REGCM_XMM) {
24253 long ref;
24254 ref = get_mask_pool_ref(state, dst, mask, fp);
24255 fprintf(fp, "\tpand L%s%lu, %s\n",
24256 state->compiler->label_prefix, ref,
24257 reg(state, dst, REGCM_XMM));
24258 }
24259 else {
24260 fprintf(state->errout, "dst type: ");
24261 name_of(state->errout, dst->type);
24262 fprintf(state->errout, "\n");
24263 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24264 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24265 }
24266 }
24267 /* Make certain I am properly sign extended */
24268 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24269 (is_signed(src->type)))
24270 {
24271 int bits, reg_bits, shift_bits;
24272 int dst_reg;
24273 int dst_regcm;
24274
24275 bits = size_of(state, src->type);
24276 reg_bits = reg_size(state, dst);
24277 if (reg_bits > 32) {
24278 reg_bits = 32;
24279 }
24280 shift_bits = reg_bits - size_of(state, src->type);
24281 dst_reg = ID_REG(dst->id);
24282 dst_regcm = arch_reg_regcm(state, dst_reg);
24283
24284 if (shift_bits < 0) {
24285 internal_error(state, dst, "negative shift?");
24286 }
24287
24288 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24289 fprintf(fp, "\tshl $%d, %s\n",
24290 shift_bits,
24291 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24292 fprintf(fp, "\tsar $%d, %s\n",
24293 shift_bits,
24294 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24295 }
24296 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24297 fprintf(fp, "\tpslld $%d, %s\n",
24298 shift_bits,
24299 reg(state, dst, REGCM_MMX | REGCM_XMM));
24300 fprintf(fp, "\tpsrad $%d, %s\n",
24301 shift_bits,
24302 reg(state, dst, REGCM_MMX | REGCM_XMM));
24303 }
24304 else {
24305 fprintf(state->errout, "dst type: ");
24306 name_of(state->errout, dst->type);
24307 fprintf(state->errout, "\n");
24308 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24309 internal_error(state, dst, "failed to signed extend value");
24310 }
24311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024312}
24313
24314static void print_op_load(struct compile_state *state,
24315 struct triple *ins, FILE *fp)
24316{
24317 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024318 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024319 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024320 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024321 if (is_const(src) || is_const(dst)) {
24322 internal_error(state, ins, "unknown load operation");
24323 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024324 switch(ins->type->type & TYPE_MASK) {
24325 case TYPE_CHAR: op = "movsbl"; break;
24326 case TYPE_UCHAR: op = "movzbl"; break;
24327 case TYPE_SHORT: op = "movswl"; break;
24328 case TYPE_USHORT: op = "movzwl"; break;
24329 case TYPE_INT: case TYPE_UINT:
24330 case TYPE_LONG: case TYPE_ULONG:
24331 case TYPE_POINTER:
24332 op = "movl";
24333 break;
24334 default:
24335 internal_error(state, ins, "unknown type in load");
24336 op = "<invalid opcode>";
24337 break;
24338 }
24339 fprintf(fp, "\t%s (%s), %s\n",
24340 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024341 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024342 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024343}
24344
24345
24346static void print_op_store(struct compile_state *state,
24347 struct triple *ins, FILE *fp)
24348{
24349 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024350 dst = RHS(ins, 0);
24351 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024352 if (is_const(src) && (src->op == OP_INTCONST)) {
24353 long_t value;
24354 value = (long_t)(src->u.cval);
24355 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24356 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024357 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024358 reg(state, dst, REGCM_GPR32));
24359 }
24360 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24361 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24362 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024363 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024364 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024365 }
24366 else {
24367 if (is_const(src) || is_const(dst)) {
24368 internal_error(state, ins, "unknown store operation");
24369 }
24370 fprintf(fp, "\tmov%s %s, (%s)\n",
24371 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024372 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024373 reg(state, dst, REGCM_GPR32));
24374 }
24375
24376
24377}
24378
24379static void print_op_smul(struct compile_state *state,
24380 struct triple *ins, FILE *fp)
24381{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024382 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024383 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024384 reg(state, RHS(ins, 1), REGCM_GPR32),
24385 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024386 }
24387 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024388 fprintf(fp, "\timul ");
24389 print_const_val(state, RHS(ins, 1), fp);
24390 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024391 }
24392}
24393
24394static void print_op_cmp(struct compile_state *state,
24395 struct triple *ins, FILE *fp)
24396{
24397 unsigned mask;
24398 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024399 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024400 dreg = check_reg(state, ins, REGCM_FLAGS);
24401 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24402 internal_error(state, ins, "bad dest register for cmp");
24403 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024404 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024405 fprintf(fp, "\tcmp ");
24406 print_const_val(state, RHS(ins, 1), fp);
24407 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024408 }
24409 else {
24410 unsigned lmask, rmask;
24411 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024412 lreg = check_reg(state, RHS(ins, 0), mask);
24413 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024414 lmask = arch_reg_regcm(state, lreg);
24415 rmask = arch_reg_regcm(state, rreg);
24416 mask = lmask & rmask;
24417 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024418 reg(state, RHS(ins, 1), mask),
24419 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024420 }
24421}
24422
24423static void print_op_test(struct compile_state *state,
24424 struct triple *ins, FILE *fp)
24425{
24426 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024427 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024428 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024429 reg(state, RHS(ins, 0), mask),
24430 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024431}
24432
24433static void print_op_branch(struct compile_state *state,
24434 struct triple *branch, FILE *fp)
24435{
24436 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024437 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024438 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024439 internal_error(state, branch, "jmp with condition?");
24440 }
24441 bop = "jmp";
24442 }
24443 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024444 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024445 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024446 internal_error(state, branch, "jmpcc without condition?");
24447 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024448 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24449 if ((RHS(branch, 0)->op != OP_CMP) &&
24450 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024451 internal_error(state, branch, "bad branch test");
24452 }
24453#warning "FIXME I have observed instructions between the test and branch instructions"
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024454 ptr = RHS(branch, 0);
24455 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24456 if (ptr->op != OP_COPY) {
24457 internal_error(state, branch, "branch does not follow test");
24458 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024459 }
24460 switch(branch->op) {
24461 case OP_JMP_EQ: bop = "jz"; break;
24462 case OP_JMP_NOTEQ: bop = "jnz"; break;
24463 case OP_JMP_SLESS: bop = "jl"; break;
24464 case OP_JMP_ULESS: bop = "jb"; break;
24465 case OP_JMP_SMORE: bop = "jg"; break;
24466 case OP_JMP_UMORE: bop = "ja"; break;
24467 case OP_JMP_SLESSEQ: bop = "jle"; break;
24468 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24469 case OP_JMP_SMOREEQ: bop = "jge"; break;
24470 case OP_JMP_UMOREEQ: bop = "jae"; break;
24471 default:
24472 internal_error(state, branch, "Invalid branch op");
24473 break;
24474 }
24475
24476 }
Eric Biederman90089602004-05-28 14:11:54 +000024477#if 1
24478 if (branch->op == OP_CALL) {
24479 fprintf(fp, "\t/* call */\n");
24480 }
24481#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024482 fprintf(fp, "\t%s L%s%lu\n",
24483 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024484 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024485 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024486}
24487
Eric Biederman5ade04a2003-10-22 04:03:46 +000024488static void print_op_ret(struct compile_state *state,
24489 struct triple *branch, FILE *fp)
24490{
24491 fprintf(fp, "\tjmp *%s\n",
24492 reg(state, RHS(branch, 0), REGCM_GPR32));
24493}
24494
Eric Biedermanb138ac82003-04-22 18:44:01 +000024495static void print_op_set(struct compile_state *state,
24496 struct triple *set, FILE *fp)
24497{
24498 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024499 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024500 internal_error(state, set, "setcc without condition?");
24501 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024502 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24503 if ((RHS(set, 0)->op != OP_CMP) &&
24504 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024505 internal_error(state, set, "bad set test");
24506 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024507 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024508 internal_error(state, set, "set does not follow test");
24509 }
24510 switch(set->op) {
24511 case OP_SET_EQ: sop = "setz"; break;
24512 case OP_SET_NOTEQ: sop = "setnz"; break;
24513 case OP_SET_SLESS: sop = "setl"; break;
24514 case OP_SET_ULESS: sop = "setb"; break;
24515 case OP_SET_SMORE: sop = "setg"; break;
24516 case OP_SET_UMORE: sop = "seta"; break;
24517 case OP_SET_SLESSEQ: sop = "setle"; break;
24518 case OP_SET_ULESSEQ: sop = "setbe"; break;
24519 case OP_SET_SMOREEQ: sop = "setge"; break;
24520 case OP_SET_UMOREEQ: sop = "setae"; break;
24521 default:
24522 internal_error(state, set, "Invalid set op");
24523 break;
24524 }
24525 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024526 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024527}
24528
24529static void print_op_bit_scan(struct compile_state *state,
24530 struct triple *ins, FILE *fp)
24531{
24532 const char *op;
24533 switch(ins->op) {
24534 case OP_BSF: op = "bsf"; break;
24535 case OP_BSR: op = "bsr"; break;
24536 default:
24537 internal_error(state, ins, "unknown bit scan");
24538 op = 0;
24539 break;
24540 }
24541 fprintf(fp,
24542 "\t%s %s, %s\n"
24543 "\tjnz 1f\n"
24544 "\tmovl $-1, %s\n"
24545 "1:\n",
24546 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024547 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024548 reg(state, ins, REGCM_GPR32),
24549 reg(state, ins, REGCM_GPR32));
24550}
24551
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024552
Eric Biedermanb138ac82003-04-22 18:44:01 +000024553static void print_sdecl(struct compile_state *state,
24554 struct triple *ins, FILE *fp)
24555{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024556 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Eric Biederman90089602004-05-28 14:11:54 +000024557 fprintf(fp, ".balign %d\n", align_of_in_bytes(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000024558 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024559 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024560 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024561 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024562
24563}
24564
24565static void print_instruction(struct compile_state *state,
24566 struct triple *ins, FILE *fp)
24567{
24568 /* Assumption: after I have exted the register allocator
24569 * everything is in a valid register.
24570 */
24571 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024572 case OP_ASM:
24573 print_op_asm(state, ins, fp);
24574 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024575 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24576 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24577 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24578 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24579 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24580 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24581 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24582 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24583 case OP_POS: break;
24584 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24585 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024586 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024587 case OP_INTCONST:
24588 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024589 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024590 /* Don't generate anything here for constants */
24591 case OP_PHI:
24592 /* Don't generate anything for variable declarations. */
24593 break;
Eric Biederman90089602004-05-28 14:11:54 +000024594 case OP_UNKNOWNVAL:
24595 fprintf(fp, " /* unknown %s */\n",
24596 reg(state, ins, REGCM_ALL));
24597 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024598 case OP_SDECL:
24599 print_sdecl(state, ins, fp);
24600 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024601 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024602 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024603 print_op_move(state, ins, fp);
24604 break;
24605 case OP_LOAD:
24606 print_op_load(state, ins, fp);
24607 break;
24608 case OP_STORE:
24609 print_op_store(state, ins, fp);
24610 break;
24611 case OP_SMUL:
24612 print_op_smul(state, ins, fp);
24613 break;
24614 case OP_CMP: print_op_cmp(state, ins, fp); break;
24615 case OP_TEST: print_op_test(state, ins, fp); break;
24616 case OP_JMP:
24617 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24618 case OP_JMP_SLESS: case OP_JMP_ULESS:
24619 case OP_JMP_SMORE: case OP_JMP_UMORE:
24620 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24621 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024622 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024623 print_op_branch(state, ins, fp);
24624 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024625 case OP_RET:
24626 print_op_ret(state, ins, fp);
24627 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024628 case OP_SET_EQ: case OP_SET_NOTEQ:
24629 case OP_SET_SLESS: case OP_SET_ULESS:
24630 case OP_SET_SMORE: case OP_SET_UMORE:
24631 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24632 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24633 print_op_set(state, ins, fp);
24634 break;
24635 case OP_INB: case OP_INW: case OP_INL:
24636 print_op_in(state, ins, fp);
24637 break;
24638 case OP_OUTB: case OP_OUTW: case OP_OUTL:
24639 print_op_out(state, ins, fp);
24640 break;
24641 case OP_BSF:
24642 case OP_BSR:
24643 print_op_bit_scan(state, ins, fp);
24644 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024645 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024646 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024647 fprintf(fp, "\trdmsr\n");
24648 break;
24649 case OP_WRMSR:
24650 fprintf(fp, "\twrmsr\n");
24651 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024652 case OP_HLT:
24653 fprintf(fp, "\thlt\n");
24654 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024655 case OP_SDIVT:
24656 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24657 break;
24658 case OP_UDIVT:
24659 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24660 break;
24661 case OP_UMUL:
24662 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24663 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024664 case OP_LABEL:
24665 if (!ins->use) {
24666 return;
24667 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024668 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024669 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024670 break;
Eric Biederman90089602004-05-28 14:11:54 +000024671 case OP_ADECL:
24672 /* Ignore adecls with no registers error otherwise */
24673 if (!noop_adecl(ins)) {
24674 internal_error(state, ins, "adecl remains?");
24675 }
24676 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024677 /* Ignore OP_PIECE */
24678 case OP_PIECE:
24679 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024680 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024681 case OP_SDIV: case OP_UDIV:
24682 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024683 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24684 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24685 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24686 default:
24687 internal_error(state, ins, "unknown op: %d %s",
24688 ins->op, tops(ins->op));
24689 break;
24690 }
24691}
24692
24693static void print_instructions(struct compile_state *state)
24694{
24695 struct triple *first, *ins;
24696 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024697 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024698 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024699 int max_inline_depth;
24700 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024701 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024702 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024703 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024704 /* Masks for common sizes */
24705 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24706 fprintf(fp, ".balign 16\n");
24707 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24708 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24709 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24710 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024711 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024712 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024713 ins = first;
24714 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024715 if (print_location &&
24716 last_occurance != ins->occurance) {
24717 if (!ins->occurance->parent) {
24718 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
24719 ins->occurance->function,
24720 ins->occurance->filename,
24721 ins->occurance->line,
24722 ins->occurance->col);
24723 }
24724 else {
24725 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024726 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024727 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024728 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024729 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024730 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024731 fprintf(fp, "\t * %s,%s:%d.%d\n",
24732 ptr->function,
24733 ptr->filename,
24734 ptr->line,
24735 ptr->col);
24736 }
24737 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024738 if (inline_depth > max_inline_depth) {
24739 max_inline_depth = inline_depth;
24740 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024741 }
24742 if (last_occurance) {
24743 put_occurance(last_occurance);
24744 }
24745 get_occurance(ins->occurance);
24746 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024747 }
24748
24749 print_instruction(state, ins, fp);
24750 ins = ins->next;
24751 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024752 if (print_location) {
24753 fprintf(fp, "/* max inline depth %d */\n",
24754 max_inline_depth);
24755 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024756}
Eric Biederman530b5192003-07-01 10:05:30 +000024757
Eric Biedermanb138ac82003-04-22 18:44:01 +000024758static void generate_code(struct compile_state *state)
24759{
24760 generate_local_labels(state);
24761 print_instructions(state);
24762
24763}
24764
Eric Biederman90089602004-05-28 14:11:54 +000024765static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024766{
24767 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024768 int tok;
24769 FILE *fp;
24770 int line;
24771 const char *filename;
24772 fp = state->output;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024773 tk = &state->token[0];
Eric Biederman90089602004-05-28 14:11:54 +000024774 filename = 0;
24775 line = 0;
24776 for(;;) {
24777 const char *token_str;
24778 tok = peek(state);
24779 if (tok == TOK_EOF) {
24780 break;
24781 }
24782 eat(state, tok);
24783 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024784 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024785 tk->str_len ? tk->val.str :
24786 tokens[tk->tok];
Eric Biedermanb138ac82003-04-22 18:44:01 +000024787
Eric Biederman90089602004-05-28 14:11:54 +000024788 if ((state->file->line != line) ||
24789 (state->file->basename != filename)) {
24790 int i, col;
24791 if ((state->file->basename == filename) &&
24792 (line < state->file->line)) {
24793 while(line < state->file->line) {
24794 fprintf(fp, "\n");
24795 line++;
24796 }
24797 }
24798 else {
24799 fprintf(fp, "\n#line %d \"%s\"\n",
24800 state->file->line, state->file->basename);
24801 }
24802 line = state->file->line;
24803 filename = state->file->basename;
24804 col = get_col(state->file) - strlen(token_str);
24805 for(i = 0; i < col; i++) {
24806 fprintf(fp, " ");
24807 }
24808 }
24809
24810 fprintf(fp, "%s ", token_str);
24811
24812 if (state->compiler->debug & DEBUG_TOKENS) {
24813 loc(state->dbgout, state, 0);
24814 fprintf(state->dbgout, "%s <- `%s'\n",
24815 tokens[tok], token_str);
24816 }
24817 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024818}
24819
Eric Biederman5ade04a2003-10-22 04:03:46 +000024820static void compile(const char *filename,
24821 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024822{
24823 int i;
24824 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024825 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024826 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024827 state.compiler = compiler;
24828 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024829 state.file = 0;
24830 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24831 memset(&state.token[i], 0, sizeof(state.token[i]));
24832 state.token[i].tok = -1;
24833 }
Eric Biederman90089602004-05-28 14:11:54 +000024834 /* Remember the output descriptors */
24835 state.errout = stderr;
24836 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024837 /* Remember the output filename */
Eric Biederman5ade04a2003-10-22 04:03:46 +000024838 state.output = fopen(state.compiler->ofilename, "w");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024839 if (!state.output) {
24840 error(&state, 0, "Cannot open output file %s\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024841 state.compiler->ofilename);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024842 }
Eric Biederman90089602004-05-28 14:11:54 +000024843 /* Make certain a good cleanup happens */
24844 exit_state = &state;
24845 atexit(exit_cleanup);
24846
Eric Biedermanb138ac82003-04-22 18:44:01 +000024847 /* Prep the preprocessor */
24848 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024849 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024850 /* register the C keywords */
24851 register_keywords(&state);
24852 /* register the keywords the macro preprocessor knows */
24853 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024854 /* generate some builtin macros */
24855 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024856 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024857 state.i_switch = lookup(&state, "switch", 6);
24858 state.i_case = lookup(&state, "case", 4);
24859 state.i_continue = lookup(&state, "continue", 8);
24860 state.i_break = lookup(&state, "break", 5);
24861 state.i_default = lookup(&state, "default", 7);
24862 state.i_return = lookup(&state, "return", 6);
24863 /* Memorize where predefined macros are. */
24864 state.i_defined = lookup(&state, "defined", 7);
24865 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24866 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24867 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24868 /* Memorize where predefined identifiers are. */
24869 state.i___func__ = lookup(&state, "__func__", 8);
24870 /* Memorize where some attribute keywords are. */
24871 state.i_noinline = lookup(&state, "noinline", 8);
24872 state.i_always_inline = lookup(&state, "always_inline", 13);
24873
24874 /* Process the command line macros */
24875 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024876
24877 /* Allocate beginning bounding labels for the function list */
24878 state.first = label(&state);
24879 state.first->id |= TRIPLE_FLAG_VOLATILE;
24880 use_triple(state.first, state.first);
24881 ptr = label(&state);
24882 ptr->id |= TRIPLE_FLAG_VOLATILE;
24883 use_triple(ptr, ptr);
24884 flatten(&state, state.first, ptr);
24885
Eric Biederman5ade04a2003-10-22 04:03:46 +000024886 /* Allocate a label for the pool of global variables */
24887 state.global_pool = label(&state);
24888 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
24889 flatten(&state, state.first, state.global_pool);
24890
Eric Biedermanb138ac82003-04-22 18:44:01 +000024891 /* Enter the globl definition scope */
24892 start_scope(&state);
24893 register_builtins(&state);
24894 compile_file(&state, filename, 1);
Eric Biederman90089602004-05-28 14:11:54 +000024895
24896 /* Stop if all we want is preprocessor output */
24897 if (state.compiler->flags & COMPILER_CPP_ONLY) {
24898 print_preprocessed_tokens(&state);
24899 return;
24900 }
24901
Eric Biedermanb138ac82003-04-22 18:44:01 +000024902 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024903
Eric Biedermanb138ac82003-04-22 18:44:01 +000024904 /* Exit the global definition scope */
24905 end_scope(&state);
24906
24907 /* Now that basic compilation has happened
24908 * optimize the intermediate code
24909 */
24910 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024911
Eric Biedermanb138ac82003-04-22 18:44:01 +000024912 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024913 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000024914 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024915 }
Eric Biederman90089602004-05-28 14:11:54 +000024916 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024917}
24918
Eric Biederman90089602004-05-28 14:11:54 +000024919static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024920{
Eric Biederman90089602004-05-28 14:11:54 +000024921 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024922}
24923
24924static void usage(void)
24925{
Eric Biederman90089602004-05-28 14:11:54 +000024926 FILE *fp = stdout;
24927 version(fp);
24928 fprintf(fp,
24929 "\nUsage: romcc [options] <source>.c\n"
24930 "Compile a C source file generating a binary that does not implicilty use RAM\n"
24931 "Options: \n"
24932 "-o <output file name>\n"
24933 "-f<option> Specify a generic compiler option\n"
24934 "-m<option> Specify a arch dependent option\n"
24935 "-- Specify this is the last option\n"
24936 "\nGeneric compiler options:\n"
24937 );
24938 compiler_usage(fp);
24939 fprintf(fp,
24940 "\nArchitecture compiler options:\n"
24941 );
24942 arch_usage(fp);
24943 fprintf(fp,
24944 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000024945 );
24946}
24947
24948static void arg_error(char *fmt, ...)
24949{
24950 va_list args;
24951 va_start(args, fmt);
24952 vfprintf(stderr, fmt, args);
24953 va_end(args);
24954 usage();
24955 exit(1);
24956}
24957
24958int main(int argc, char **argv)
24959{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024960 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024961 struct compiler_state compiler;
24962 struct arch_state arch;
24963 int all_opts;
Eric Biederman90089602004-05-28 14:11:54 +000024964
24965
24966 /* I don't want any surprises */
24967 setlocale(LC_ALL, "C");
24968
Eric Biederman5ade04a2003-10-22 04:03:46 +000024969 init_compiler_state(&compiler);
24970 init_arch_state(&arch);
24971 filename = 0;
24972 all_opts = 0;
24973 while(argc > 1) {
24974 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
24975 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024976 argv += 2;
24977 argc -= 2;
24978 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024979 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000024980 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024981 result = -1;
24982 if (strcmp(argv[1], "--") == 0) {
24983 result = 0;
24984 all_opts = 1;
24985 }
Eric Biederman90089602004-05-28 14:11:54 +000024986 else if (strncmp(argv[1], "-E", 2) == 0) {
24987 result = compiler_encode_flag(&compiler, argv[1]);
24988 }
24989 else if (strncmp(argv[1], "-O", 2) == 0) {
24990 result = compiler_encode_flag(&compiler, argv[1]);
24991 }
24992 else if (strncmp(argv[1], "-I", 2) == 0) {
24993 result = compiler_encode_flag(&compiler, argv[1]);
24994 }
24995 else if (strncmp(argv[1], "-D", 2) == 0) {
24996 result = compiler_encode_flag(&compiler, argv[1]);
24997 }
24998 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000024999 result = compiler_encode_flag(&compiler, argv[1]);
25000 }
25001 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25002 result = compiler_encode_flag(&compiler, argv[1]+2);
25003 }
25004 else if (strncmp(argv[1], "-f", 2) == 0) {
25005 result = compiler_encode_flag(&compiler, argv[1]+2);
25006 }
25007 else if (strncmp(argv[1], "-m", 2) == 0) {
25008 result = arch_encode_flag(&arch, argv[1]+2);
25009 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025010 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025011 arg_error("Invalid option specified: %s\n",
25012 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025013 }
25014 argv++;
25015 argc--;
25016 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025017 else {
25018 if (filename) {
25019 arg_error("Only one filename may be specified\n");
25020 }
25021 filename = argv[1];
25022 argv++;
25023 argc--;
25024 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025025 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025026 if (!filename) {
25027 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025028 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025029 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025030
25031 return 0;
25032}