blob: 5ab94274afc1ebf944b770b8202a47fd87aa4cb8 [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"
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +00006#define VERSION_MINOR "71"
7#define RELEASE_DATE "03 April 2009"
Eric Biederman90089602004-05-28 14:11:54 +00008#define VERSION VERSION_MAJOR "." VERSION_MINOR
9
Eric Biedermanb138ac82003-04-22 18:44:01 +000010#include <stdarg.h>
11#include <errno.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <stdio.h>
20#include <string.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000021#include <limits.h>
Eric Biederman90089602004-05-28 14:11:54 +000022#include <locale.h>
23#include <time.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000024
Eric Biederman90089602004-05-28 14:11:54 +000025#define MAX_CWD_SIZE 4096
Eric Biederman5ade04a2003-10-22 04:03:46 +000026#define MAX_ALLOCATION_PASSES 100
27
Stefan Reinauer50542a82007-10-24 11:14:14 +000028/* NOTE: Before you even start thinking to touch anything
29 * in this code, set DEBUG_ROMCC_WARNINGS to 1 to get an
30 * insight on the original author's thoughts. We introduced
31 * this switch as romcc was about the only thing producing
32 * massive warnings in our code..
33 */
34#define DEBUG_ROMCC_WARNINGS 0
35
Eric Biedermana4aef6d2003-12-05 06:16:19 +000036#define DEBUG_CONSISTENCY 1
Eric Biederman530b5192003-07-01 10:05:30 +000037#define DEBUG_SDP_BLOCKS 0
38#define DEBUG_TRIPLE_COLOR 0
Eric Biedermanb138ac82003-04-22 18:44:01 +000039
Eric Biederman90089602004-05-28 14:11:54 +000040#define DEBUG_DISPLAY_USES 1
41#define DEBUG_DISPLAY_TYPES 1
42#define DEBUG_REPLACE_CLOSURE_TYPE_HIRES 0
43#define DEBUG_DECOMPOSE_PRINT_TUPLES 0
44#define DEBUG_DECOMPOSE_HIRES 0
45#define DEBUG_INITIALIZER 0
46#define DEBUG_UPDATE_CLOSURE_TYPE 0
47#define DEBUG_LOCAL_TRIPLE 0
48#define DEBUG_BASIC_BLOCKS_VERBOSE 0
49#define DEBUG_CPS_RENAME_VARIABLES_HIRES 0
50#define DEBUG_SIMPLIFY_HIRES 0
51#define DEBUG_SHRINKING 0
52#define DEBUG_COALESCE_HITCHES 0
53#define DEBUG_CODE_ELIMINATION 0
54
55#define DEBUG_EXPLICIT_CLOSURES 0
56
Stefan Reinauer50542a82007-10-24 11:14:14 +000057#if DEBUG_ROMCC_WARNINGS
Eric Biederman8d9c1232003-06-17 08:42:17 +000058#warning "FIXME give clear error messages about unused variables"
Eric Biederman530b5192003-07-01 10:05:30 +000059#warning "FIXME properly handle multi dimensional arrays"
Eric Biederman90089602004-05-28 14:11:54 +000060#warning "FIXME handle multiple register sizes"
Stefan Reinauer50542a82007-10-24 11:14:14 +000061#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000062
Eric Biedermanb138ac82003-04-22 18:44:01 +000063/* Control flow graph of a loop without goto.
64 *
65 * AAA
66 * +---/
67 * /
68 * / +--->CCC
69 * | | / \
70 * | | DDD EEE break;
71 * | | \ \
72 * | | FFF \
73 * \| / \ \
74 * |\ GGG HHH | continue;
75 * | \ \ | |
76 * | \ III | /
77 * | \ | / /
78 * | vvv /
79 * +----BBB /
80 * | /
81 * vv
82 * JJJ
83 *
84 *
85 * AAA
86 * +-----+ | +----+
87 * | \ | / |
88 * | BBB +-+ |
89 * | / \ / | |
90 * | CCC JJJ / /
91 * | / \ / /
92 * | DDD EEE / /
93 * | | +-/ /
94 * | FFF /
95 * | / \ /
96 * | GGG HHH /
97 * | | +-/
98 * | III
99 * +--+
100 *
101 *
102 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
103 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
104 *
105 *
106 * [] == DFlocal(X) U DF(X)
107 * () == DFup(X)
108 *
109 * Dominator graph of the same nodes.
110 *
111 * AAA AAA: [ ] ()
112 * / \
113 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
114 * |
115 * CCC CCC: [ ] ( BBB, JJJ )
116 * / \
117 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
118 * |
119 * FFF FFF: [ ] ( BBB )
120 * / \
121 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
122 * |
123 * III III: [ BBB ] ()
124 *
125 *
126 * BBB and JJJ are definitely the dominance frontier.
127 * Where do I place phi functions and how do I make that decision.
128 *
129 */
130static void die(char *fmt, ...)
131{
132 va_list args;
133
134 va_start(args, fmt);
135 vfprintf(stderr, fmt, args);
136 va_end(args);
137 fflush(stdout);
138 fflush(stderr);
139 exit(1);
140}
141
Eric Biedermanb138ac82003-04-22 18:44:01 +0000142static void *xmalloc(size_t size, const char *name)
143{
144 void *buf;
145 buf = malloc(size);
146 if (!buf) {
147 die("Cannot malloc %ld bytes to hold %s: %s\n",
148 size + 0UL, name, strerror(errno));
149 }
150 return buf;
151}
152
153static void *xcmalloc(size_t size, const char *name)
154{
155 void *buf;
156 buf = xmalloc(size, name);
157 memset(buf, 0, size);
158 return buf;
159}
160
Eric Biederman90089602004-05-28 14:11:54 +0000161static void *xrealloc(void *ptr, size_t size, const char *name)
162{
163 void *buf;
164 buf = realloc(ptr, size);
165 if (!buf) {
166 die("Cannot realloc %ld bytes to hold %s: %s\n",
167 size + 0UL, name, strerror(errno));
168 }
169 return buf;
170}
171
Eric Biedermanb138ac82003-04-22 18:44:01 +0000172static void xfree(const void *ptr)
173{
174 free((void *)ptr);
175}
176
177static char *xstrdup(const char *str)
178{
179 char *new;
180 int len;
181 len = strlen(str);
182 new = xmalloc(len + 1, "xstrdup string");
183 memcpy(new, str, len);
184 new[len] = '\0';
185 return new;
186}
187
188static void xchdir(const char *path)
189{
190 if (chdir(path) != 0) {
Eric Biederman90089602004-05-28 14:11:54 +0000191 die("chdir to `%s' failed: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +0000192 path, strerror(errno));
193 }
194}
195
196static int exists(const char *dirname, const char *filename)
197{
Eric Biederman90089602004-05-28 14:11:54 +0000198 char cwd[MAX_CWD_SIZE];
199 int does_exist;
200
201 if (getcwd(cwd, sizeof(cwd)) == 0) {
202 die("cwd buffer to small");
203 }
204
205 does_exist = 1;
206 if (chdir(dirname) != 0) {
207 does_exist = 0;
208 }
209 if (does_exist && (access(filename, O_RDONLY) < 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000210 if ((errno != EACCES) && (errno != EROFS)) {
211 does_exist = 0;
212 }
213 }
Eric Biederman90089602004-05-28 14:11:54 +0000214 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000215 return does_exist;
216}
217
218
219static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
220{
Eric Biederman90089602004-05-28 14:11:54 +0000221 char cwd[MAX_CWD_SIZE];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000222 int fd;
223 char *buf;
224 off_t size, progress;
225 ssize_t result;
226 struct stat stats;
227
228 if (!filename) {
229 *r_size = 0;
230 return 0;
231 }
Eric Biederman90089602004-05-28 14:11:54 +0000232 if (getcwd(cwd, sizeof(cwd)) == 0) {
233 die("cwd buffer to small");
234 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000235 xchdir(dirname);
236 fd = open(filename, O_RDONLY);
Eric Biederman90089602004-05-28 14:11:54 +0000237 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000238 if (fd < 0) {
239 die("Cannot open '%s' : %s\n",
240 filename, strerror(errno));
241 }
242 result = fstat(fd, &stats);
243 if (result < 0) {
244 die("Cannot stat: %s: %s\n",
245 filename, strerror(errno));
246 }
247 size = stats.st_size;
248 *r_size = size +1;
249 buf = xmalloc(size +2, filename);
250 buf[size] = '\n'; /* Make certain the file is newline terminated */
251 buf[size+1] = '\0'; /* Null terminate the file for good measure */
252 progress = 0;
253 while(progress < size) {
254 result = read(fd, buf + progress, size - progress);
255 if (result < 0) {
256 if ((errno == EINTR) || (errno == EAGAIN))
257 continue;
258 die("read on %s of %ld bytes failed: %s\n",
259 filename, (size - progress)+ 0UL, strerror(errno));
260 }
261 progress += result;
262 }
263 result = close(fd);
264 if (result < 0) {
265 die("Close of %s failed: %s\n",
266 filename, strerror(errno));
267 }
268 return buf;
269}
270
Eric Biederman83b991a2003-10-11 06:20:25 +0000271/* Types on the destination platform */
Stefan Reinauer50542a82007-10-24 11:14:14 +0000272#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +0000273#warning "FIXME this assumes 32bit x86 is the destination"
Stefan Reinauer50542a82007-10-24 11:14:14 +0000274#endif
Eric Biederman83b991a2003-10-11 06:20:25 +0000275typedef int8_t schar_t;
276typedef uint8_t uchar_t;
277typedef int8_t char_t;
278typedef int16_t short_t;
279typedef uint16_t ushort_t;
280typedef int32_t int_t;
281typedef uint32_t uint_t;
282typedef int32_t long_t;
Patrick Georgia84a99b2009-05-26 14:03:51 +0000283#define ulong_t uint32_t
Eric Biederman83b991a2003-10-11 06:20:25 +0000284
285#define SCHAR_T_MIN (-128)
286#define SCHAR_T_MAX 127
287#define UCHAR_T_MAX 255
288#define CHAR_T_MIN SCHAR_T_MIN
289#define CHAR_T_MAX SCHAR_T_MAX
290#define SHRT_T_MIN (-32768)
291#define SHRT_T_MAX 32767
292#define USHRT_T_MAX 65535
293#define INT_T_MIN (-LONG_T_MAX - 1)
294#define INT_T_MAX 2147483647
295#define UINT_T_MAX 4294967295U
296#define LONG_T_MIN (-LONG_T_MAX - 1)
297#define LONG_T_MAX 2147483647
298#define ULONG_T_MAX 4294967295U
Eric Biedermanb138ac82003-04-22 18:44:01 +0000299
Eric Biederman90089602004-05-28 14:11:54 +0000300#define SIZEOF_I8 8
301#define SIZEOF_I16 16
302#define SIZEOF_I32 32
303#define SIZEOF_I64 64
304
305#define SIZEOF_CHAR 8
306#define SIZEOF_SHORT 16
307#define SIZEOF_INT 32
308#define SIZEOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
309
310
311#define ALIGNOF_CHAR 8
312#define ALIGNOF_SHORT 16
313#define ALIGNOF_INT 32
314#define ALIGNOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
315
316#define REG_SIZEOF_REG 32
317#define REG_SIZEOF_CHAR REG_SIZEOF_REG
318#define REG_SIZEOF_SHORT REG_SIZEOF_REG
319#define REG_SIZEOF_INT REG_SIZEOF_REG
320#define REG_SIZEOF_LONG REG_SIZEOF_REG
321
322#define REG_ALIGNOF_REG REG_SIZEOF_REG
323#define REG_ALIGNOF_CHAR REG_SIZEOF_REG
324#define REG_ALIGNOF_SHORT REG_SIZEOF_REG
325#define REG_ALIGNOF_INT REG_SIZEOF_REG
326#define REG_ALIGNOF_LONG REG_SIZEOF_REG
327
328/* Additional definitions for clarity.
329 * I currently assume a long is the largest native
330 * machine word and that a pointer fits into it.
331 */
332#define SIZEOF_WORD SIZEOF_LONG
333#define SIZEOF_POINTER SIZEOF_LONG
334#define ALIGNOF_WORD ALIGNOF_LONG
335#define ALIGNOF_POINTER ALIGNOF_LONG
336#define REG_SIZEOF_POINTER REG_SIZEOF_LONG
337#define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG
338
Eric Biedermanb138ac82003-04-22 18:44:01 +0000339struct file_state {
340 struct file_state *prev;
341 const char *basename;
342 char *dirname;
Eric Biedermancb364952004-11-15 10:46:44 +0000343 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000344 off_t size;
Eric Biederman90089602004-05-28 14:11:54 +0000345 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000346 int line;
Eric Biederman90089602004-05-28 14:11:54 +0000347 const char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000348 int report_line;
349 const char *report_name;
350 const char *report_dir;
Eric Biedermancb364952004-11-15 10:46:44 +0000351 int macro : 1;
352 int trigraphs : 1;
353 int join_lines : 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000354};
355struct hash_entry;
356struct token {
357 int tok;
358 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +0000359 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000360 int str_len;
361 union {
362 ulong_t integer;
363 const char *str;
Eric Biederman90089602004-05-28 14:11:54 +0000364 int notmacro;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000365 } val;
366};
367
368/* I have two classes of types:
369 * Operational types.
370 * Logical types. (The type the C standard says the operation is of)
371 *
372 * The operational types are:
373 * chars
374 * shorts
375 * ints
376 * longs
377 *
378 * floats
379 * doubles
380 * long doubles
381 *
382 * pointer
383 */
384
385
386/* Machine model.
387 * No memory is useable by the compiler.
388 * There is no floating point support.
389 * All operations take place in general purpose registers.
390 * There is one type of general purpose register.
391 * Unsigned longs are stored in that general purpose register.
392 */
393
394/* Operations on general purpose registers.
395 */
396
Eric Biederman530b5192003-07-01 10:05:30 +0000397#define OP_SDIVT 0
398#define OP_UDIVT 1
399#define OP_SMUL 2
400#define OP_UMUL 3
401#define OP_SDIV 4
402#define OP_UDIV 5
403#define OP_SMOD 6
404#define OP_UMOD 7
405#define OP_ADD 8
406#define OP_SUB 9
407#define OP_SL 10
408#define OP_USR 11
409#define OP_SSR 12
410#define OP_AND 13
411#define OP_XOR 14
412#define OP_OR 15
413#define OP_POS 16 /* Dummy positive operator don't use it */
414#define OP_NEG 17
415#define OP_INVERT 18
Eric Biedermanb138ac82003-04-22 18:44:01 +0000416
417#define OP_EQ 20
418#define OP_NOTEQ 21
419#define OP_SLESS 22
420#define OP_ULESS 23
421#define OP_SMORE 24
422#define OP_UMORE 25
423#define OP_SLESSEQ 26
424#define OP_ULESSEQ 27
425#define OP_SMOREEQ 28
426#define OP_UMOREEQ 29
427
428#define OP_LFALSE 30 /* Test if the expression is logically false */
429#define OP_LTRUE 31 /* Test if the expression is logcially true */
430
431#define OP_LOAD 32
432#define OP_STORE 33
Eric Biederman530b5192003-07-01 10:05:30 +0000433/* For OP_STORE ->type holds the type
434 * RHS(0) holds the destination address
435 * RHS(1) holds the value to store.
436 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000437
Eric Biederman90089602004-05-28 14:11:54 +0000438#define OP_UEXTRACT 34
439/* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register
440 * RHS(0) holds the psuedo register to extract from
441 * ->type holds the size of the bitfield.
442 * ->u.bitfield.size holds the size of the bitfield.
443 * ->u.bitfield.offset holds the offset to extract from
444 */
445#define OP_SEXTRACT 35
446/* OP_SEXTRACT extracts a signed bitfield from a pseudo register
447 * RHS(0) holds the psuedo register to extract from
448 * ->type holds the size of the bitfield.
449 * ->u.bitfield.size holds the size of the bitfield.
450 * ->u.bitfield.offset holds the offset to extract from
451 */
452#define OP_DEPOSIT 36
453/* OP_DEPOSIT replaces a bitfield with a new value.
454 * RHS(0) holds the value to replace a bitifield in.
455 * RHS(1) holds the replacement value
456 * ->u.bitfield.size holds the size of the bitfield.
457 * ->u.bitfield.offset holds the deposit into
458 */
459
460#define OP_NOOP 37
Eric Biedermanb138ac82003-04-22 18:44:01 +0000461
462#define OP_MIN_CONST 50
Eric Biederman90089602004-05-28 14:11:54 +0000463#define OP_MAX_CONST 58
Eric Biedermanb138ac82003-04-22 18:44:01 +0000464#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
465#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000466/* For OP_INTCONST ->type holds the type.
467 * ->u.cval holds the constant value.
468 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000469#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000470/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000471 * information. u.blob holds a pointer to the raw binary
472 * data for the constant initializer.
473 */
474#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000475/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000476 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000477 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000478 */
Eric Biederman90089602004-05-28 14:11:54 +0000479#define OP_UNKNOWNVAL 59
480/* For OP_UNKNOWNAL ->type holds the type.
481 * For some reason we don't know what value this type has.
482 * This allows for variables that have don't have values
483 * assigned yet, or variables whose value we simply do not know.
484 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000485
486#define OP_WRITE 60
487/* OP_WRITE moves one pseudo register to another.
Eric Biederman90089602004-05-28 14:11:54 +0000488 * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
489 * RHS(0) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000490 */
491
492#define OP_READ 61
493/* OP_READ reads the value of a variable and makes
494 * it available for the pseudo operation.
495 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000496 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000497 */
498#define OP_COPY 62
Eric Biederman90089602004-05-28 14:11:54 +0000499/* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
Eric Biederman0babc1c2003-05-09 02:39:00 +0000500 */
Eric Biederman90089602004-05-28 14:11:54 +0000501#define OP_CONVERT 63
502/* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0).
503 * And then the type is converted appropriately.
504 */
505#define OP_PIECE 64
Eric Biederman0babc1c2003-05-09 02:39:00 +0000506/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000507 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000508 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000509 */
Eric Biederman90089602004-05-28 14:11:54 +0000510#define OP_ASM 65
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000511/* OP_ASM holds a sequence of assembly instructions, the result
512 * of a C asm directive.
513 * RHS(x) holds input value x to the assembly sequence.
514 * LHS(x) holds the output value x from the assembly sequence.
515 * u.blob holds the string of assembly instructions.
516 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000517
Eric Biederman90089602004-05-28 14:11:54 +0000518#define OP_DEREF 66
Eric Biedermanb138ac82003-04-22 18:44:01 +0000519/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000520 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000521 * OP_DEREF serves as a place holder to indicate all necessary
522 * checks have been done to indicate a value is an lvalue.
523 */
Eric Biederman90089602004-05-28 14:11:54 +0000524#define OP_DOT 67
Eric Biederman0babc1c2003-05-09 02:39:00 +0000525/* OP_DOT references a submember of a structure lvalue.
Eric Biederman90089602004-05-28 14:11:54 +0000526 * MISC(0) holds the lvalue.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000527 * ->u.field holds the name of the field we want.
528 *
Eric Biederman90089602004-05-28 14:11:54 +0000529 * Not seen after structures are flattened.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000530 */
Eric Biederman90089602004-05-28 14:11:54 +0000531#define OP_INDEX 68
532/* OP_INDEX references a submember of a tuple or array lvalue.
533 * MISC(0) holds the lvalue.
534 * ->u.cval holds the index into the lvalue.
535 *
536 * Not seen after structures are flattened.
537 */
538#define OP_VAL 69
Eric Biedermanb138ac82003-04-22 18:44:01 +0000539/* OP_VAL returns the value of a subexpression of the current expression.
540 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000541 * RHS(0) holds the expression.
542 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000543 * value of the expression.
544 *
545 * Not seen outside of expressions.
546 */
Eric Biederman90089602004-05-28 14:11:54 +0000547
548#define OP_TUPLE 70
549/* OP_TUPLE is an array of triples that are either variable
550 * or values for a structure or an array. It is used as
551 * a place holder when flattening compound types.
552 * The value represented by an OP_TUPLE is held in N registers.
553 * LHS(0..N-1) refer to those registers.
554 * ->use is a list of statements that use the value.
555 *
556 * Although OP_TUPLE always has register sized pieces they are not
557 * used until structures are flattened/decomposed into their register
558 * components.
559 * ???? registers ????
Eric Biedermanb138ac82003-04-22 18:44:01 +0000560 */
561
Eric Biederman90089602004-05-28 14:11:54 +0000562#define OP_BITREF 71
563/* OP_BITREF describes a bitfield as an lvalue.
564 * RHS(0) holds the register value.
565 * ->type holds the type of the bitfield.
566 * ->u.bitfield.size holds the size of the bitfield.
567 * ->u.bitfield.offset holds the offset of the bitfield in the register
568 */
569
570
571#define OP_FCALL 72
Eric Biederman5ade04a2003-10-22 04:03:46 +0000572/* OP_FCALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000573 * MISC(0) holds a pointer to the OP_LIST of a function
574 * RHS(x) holds argument x of a function
575 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000576 * Currently not seen outside of expressions.
577 */
Eric Biederman90089602004-05-28 14:11:54 +0000578#define OP_PROG 73
579/* OP_PROG is an expression that holds a list of statements, or
580 * expressions. The final expression is the value of the expression.
581 * RHS(0) holds the start of the list.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000582 */
583
584/* statements */
585#define OP_LIST 80
Eric Biederman5ade04a2003-10-22 04:03:46 +0000586/* OP_LIST Holds a list of statements that compose a function, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000587 * RHS(0) holds the list of statements.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000588 * A list of all functions is maintained.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000589 */
590
Eric Biederman5ade04a2003-10-22 04:03:46 +0000591#define OP_BRANCH 81 /* an unconditional branch */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000592/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000593 * TARG(0) holds the branch target.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000594 * ->next holds where to branch to if the branch is not taken.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000595 * The branch target can only be a label
Eric Biedermanb138ac82003-04-22 18:44:01 +0000596 */
597
Eric Biederman5ade04a2003-10-22 04:03:46 +0000598#define OP_CBRANCH 82 /* a conditional branch */
599/* For conditional branch instructions
600 * RHS(0) holds the branch condition.
Eric Biederman41203d92004-11-08 09:31:09 +0000601 * TARG(0) holds the branch target.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000602 * ->next holds where to branch to if the branch is not taken.
603 * The branch target can only be a label
604 */
605
606#define OP_CALL 83 /* an uncontional branch that will return */
607/* For call instructions
608 * MISC(0) holds the OP_RET that returns from the branch
609 * TARG(0) holds the branch target.
610 * ->next holds where to branch to if the branch is not taken.
611 * The branch target can only be a label
612 */
613
614#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
615/* For call instructions
616 * RHS(0) holds the variable with the return address
617 * The branch target can only be a label
618 */
619
620#define OP_LABEL 86
Eric Biedermanb138ac82003-04-22 18:44:01 +0000621/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000622 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000623 */
624
Eric Biederman5ade04a2003-10-22 04:03:46 +0000625#define OP_ADECL 87
626/* OP_ADECL is a triple that establishes an lvalue for assignments.
Eric Biederman90089602004-05-28 14:11:54 +0000627 * A variable takes N registers to contain.
628 * LHS(0..N-1) refer to an OP_PIECE triple that represents
629 * the Xth register that the variable is stored in.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000630 * ->use is a list of statements that use the variable.
Eric Biederman90089602004-05-28 14:11:54 +0000631 *
632 * Although OP_ADECL always has register sized pieces they are not
633 * used until structures are flattened/decomposed into their register
634 * components.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000635 */
636
Eric Biederman5ade04a2003-10-22 04:03:46 +0000637#define OP_SDECL 88
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000638/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000639 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000640 * ->use is a list of statements that use the variable.
641 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000642 */
643
644
Eric Biederman5ade04a2003-10-22 04:03:46 +0000645#define OP_PHI 89
Eric Biedermanb138ac82003-04-22 18:44:01 +0000646/* OP_PHI is a triple used in SSA form code.
647 * It is used when multiple code paths merge and a variable needs
648 * a single assignment from any of those code paths.
649 * The operation is a cross between OP_DECL and OP_WRITE, which
Eric Biederman5ade04a2003-10-22 04:03:46 +0000650 * is what OP_PHI is generated from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000651 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000652 * RHS(x) points to the value from code path x
653 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000654 * in which OP_PHI resides. The elements of the array point to point
655 * to the variables OP_PHI is derived from.
656 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000657 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000658 */
659
Eric Biederman90089602004-05-28 14:11:54 +0000660#if 0
661/* continuation helpers
662 */
663#define OP_CPS_BRANCH 90 /* an unconditional branch */
664/* OP_CPS_BRANCH calls a continuation
665 * RHS(x) holds argument x of the function
666 * TARG(0) holds OP_CPS_START target
667 */
668#define OP_CPS_CBRANCH 91 /* a conditional branch */
669/* OP_CPS_CBRANCH conditionally calls one of two continuations
670 * RHS(0) holds the branch condition
671 * RHS(x + 1) holds argument x of the function
672 * TARG(0) holds the OP_CPS_START to jump to when true
673 * ->next holds the OP_CPS_START to jump to when false
674 */
675#define OP_CPS_CALL 92 /* an uncontional branch that will return */
676/* For OP_CPS_CALL instructions
677 * RHS(x) holds argument x of the function
678 * MISC(0) holds the OP_CPS_RET that returns from the branch
679 * TARG(0) holds the branch target.
680 * ->next holds where the OP_CPS_RET will return to.
681 */
682#define OP_CPS_RET 93
683/* OP_CPS_RET conditionally calls one of two continuations
684 * RHS(0) holds the variable with the return function address
685 * RHS(x + 1) holds argument x of the function
686 * The branch target may be any OP_CPS_START
687 */
688#define OP_CPS_END 94
689/* OP_CPS_END is the triple at the end of the program.
690 * For most practical purposes it is a branch.
691 */
692#define OP_CPS_START 95
693/* OP_CPS_START is a triple at the start of a continuation
694 * The arguments variables takes N registers to contain.
695 * LHS(0..N-1) refer to an OP_PIECE triple that represents
696 * the Xth register that the arguments are stored in.
697 */
698#endif
699
Eric Biedermanb138ac82003-04-22 18:44:01 +0000700/* Architecture specific instructions */
701#define OP_CMP 100
702#define OP_TEST 101
703#define OP_SET_EQ 102
704#define OP_SET_NOTEQ 103
705#define OP_SET_SLESS 104
706#define OP_SET_ULESS 105
707#define OP_SET_SMORE 106
708#define OP_SET_UMORE 107
709#define OP_SET_SLESSEQ 108
710#define OP_SET_ULESSEQ 109
711#define OP_SET_SMOREEQ 110
712#define OP_SET_UMOREEQ 111
713
714#define OP_JMP 112
715#define OP_JMP_EQ 113
716#define OP_JMP_NOTEQ 114
717#define OP_JMP_SLESS 115
718#define OP_JMP_ULESS 116
719#define OP_JMP_SMORE 117
720#define OP_JMP_UMORE 118
721#define OP_JMP_SLESSEQ 119
722#define OP_JMP_ULESSEQ 120
723#define OP_JMP_SMOREEQ 121
724#define OP_JMP_UMOREEQ 122
725
726/* Builtin operators that it is just simpler to use the compiler for */
727#define OP_INB 130
728#define OP_INW 131
729#define OP_INL 132
730#define OP_OUTB 133
731#define OP_OUTW 134
732#define OP_OUTL 135
733#define OP_BSF 136
734#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000735#define OP_RDMSR 138
736#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000737#define OP_HLT 140
738
Eric Biederman0babc1c2003-05-09 02:39:00 +0000739struct op_info {
740 const char *name;
741 unsigned flags;
Eric Biederman90089602004-05-28 14:11:54 +0000742#define PURE 0x001 /* Triple has no side effects */
743#define IMPURE 0x002 /* Triple has side effects */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000744#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
Eric Biederman90089602004-05-28 14:11:54 +0000745#define DEF 0x004 /* Triple is a variable definition */
746#define BLOCK 0x008 /* Triple stores the current block */
747#define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */
748#define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 )
749#define UBRANCH 0x020 /* Triple is an unconditional branch instruction */
750#define CBRANCH 0x040 /* Triple is a conditional branch instruction */
751#define RETBRANCH 0x060 /* Triple is a return instruction */
752#define CALLBRANCH 0x080 /* Triple is a call instruction */
753#define ENDBRANCH 0x0a0 /* Triple is an end instruction */
754#define PART 0x100 /* Triple is really part of another triple */
755#define BITFIELD 0x200 /* Triple manipulates a bitfield */
756 signed char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000757};
758
Eric Biederman0babc1c2003-05-09 02:39:00 +0000759#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
760 .name = (NAME), \
761 .flags = (FLAGS), \
762 .lhs = (LHS), \
763 .rhs = (RHS), \
764 .misc = (MISC), \
765 .targ = (TARG), \
766 }
767static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000768[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
769[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000770[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
771[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
772[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
773[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
774[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
775[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
776[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
777[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
778[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
779[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
780[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
781[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
782[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
783[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
784[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
785[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
786[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000787
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000788[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
789[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
790[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
791[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
792[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
793[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
794[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
795[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
796[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
797[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
798[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
799[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000800
Eric Biederman90089602004-05-28 14:11:54 +0000801[OP_LOAD ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "load"),
802[OP_STORE ] = OP( 0, 2, 0, 0, PURE | BLOCK , "store"),
803
804[OP_UEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "uextract"),
805[OP_SEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "sextract"),
806[OP_DEPOSIT ] = OP( 0, 2, 0, 0, PURE | DEF | BITFIELD, "deposit"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000807
Eric Biederman83b991a2003-10-11 06:20:25 +0000808[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000809
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000810[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000811[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000812[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman90089602004-05-28 14:11:54 +0000813[OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000814
Stefan Reinauer50542a82007-10-24 11:14:14 +0000815#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +0000816#warning "FIXME is it correct for OP_WRITE to be a def? I currently use it as one..."
Stefan Reinauer50542a82007-10-24 11:14:14 +0000817#endif
Eric Biederman90089602004-05-28 14:11:54 +0000818[OP_WRITE ] = OP( 0, 1, 1, 0, PURE | DEF | BLOCK, "write"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000819[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
820[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
Eric Biederman90089602004-05-28 14:11:54 +0000821[OP_CONVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "convert"),
822[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"),
823[OP_ASM ] = OP(-1, -1, 0, 0, PURE, "asm"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000824[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
Eric Biederman90089602004-05-28 14:11:54 +0000825[OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"),
826[OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000827
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000828[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
Eric Biederman90089602004-05-28 14:11:54 +0000829[OP_TUPLE ] = OP(-1, 0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"),
830[OP_BITREF ] = OP( 0, 1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000831/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman90089602004-05-28 14:11:54 +0000832[OP_FCALL ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"),
833[OP_PROG ] = OP( 0, 1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"),
834/* The sizes of OP_FCALL depends upon context */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000835
Eric Biederman83b991a2003-10-11 06:20:25 +0000836[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
Eric Biederman90089602004-05-28 14:11:54 +0000837[OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "branch"),
838[OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"),
839[OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"),
840[OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000841[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
842[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
843[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000844/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000845[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000846
Eric Biederman90089602004-05-28 14:11:54 +0000847#if 0
848[OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH, "cps_branch"),
849[OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH, "cps_cbranch"),
850[OP_CPS_CALL ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH, "cps_call"),
851[OP_CPS_RET ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH, "cps_ret"),
852[OP_CPS_END ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"),
853[OP_CPS_START ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "cps_start"),
854#endif
855
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000856[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
857[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
858[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
859[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
860[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
861[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
862[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
863[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
864[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
865[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
866[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
867[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
Eric Biederman90089602004-05-28 14:11:54 +0000868[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"),
869[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"),
870[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"),
871[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"),
872[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"),
873[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"),
874[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"),
875[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"),
876[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"),
877[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"),
878[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000879
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000880[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
881[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
882[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
883[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
884[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
885[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
886[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
887[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
888[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
889[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
890[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000891};
892#undef OP
893#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000894
895static const char *tops(int index)
896{
897 static const char unknown[] = "unknown op";
898 if (index < 0) {
899 return unknown;
900 }
901 if (index > OP_MAX) {
902 return unknown;
903 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000904 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000905}
906
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000907struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000908struct triple;
909struct block;
910struct triple_set {
911 struct triple_set *next;
912 struct triple *member;
913};
914
Eric Biederman90089602004-05-28 14:11:54 +0000915#define MAX_LHS 63
916#define MAX_RHS 127
Eric Biederman678d8162003-07-03 03:59:38 +0000917#define MAX_MISC 3
Eric Biederman90089602004-05-28 14:11:54 +0000918#define MAX_TARG 1
Eric Biederman0babc1c2003-05-09 02:39:00 +0000919
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000920struct occurance {
921 int count;
922 const char *filename;
923 const char *function;
924 int line;
925 int col;
926 struct occurance *parent;
927};
Eric Biederman90089602004-05-28 14:11:54 +0000928struct bitfield {
929 ulong_t size : 8;
930 ulong_t offset : 24;
931};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000932struct triple {
933 struct triple *next, *prev;
934 struct triple_set *use;
935 struct type *type;
Eric Biederman90089602004-05-28 14:11:54 +0000936 unsigned int op : 8;
937 unsigned int template_id : 7;
938 unsigned int lhs : 6;
939 unsigned int rhs : 7;
940 unsigned int misc : 2;
941 unsigned int targ : 1;
942#define TRIPLE_SIZE(TRIPLE) \
943 ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ)
944#define TRIPLE_LHS_OFF(PTR) (0)
945#define TRIPLE_RHS_OFF(PTR) (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs)
946#define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs)
947#define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc)
948#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)])
949#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)])
950#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)])
951#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000952 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000953#define TRIPLE_FLAG_FLATTENED (1 << 31)
954#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
955#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biederman83b991a2003-10-11 06:20:25 +0000956#define TRIPLE_FLAG_VOLATILE (1 << 28)
Eric Biederman90089602004-05-28 14:11:54 +0000957#define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */
958#define TRIPLE_FLAG_LOCAL (1 << 26)
959
960#define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000961 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000962 union {
963 ulong_t cval;
Eric Biederman90089602004-05-28 14:11:54 +0000964 struct bitfield bitfield;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000965 struct block *block;
966 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000967 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000968 struct asm_info *ainfo;
Eric Biederman90089602004-05-28 14:11:54 +0000969 struct triple *func;
970 struct symbol *symbol;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000971 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000972 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000973};
974
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000975struct reg_info {
976 unsigned reg;
977 unsigned regcm;
978};
979struct ins_template {
980 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
981};
982
983struct asm_info {
984 struct ins_template tmpl;
985 char *str;
986};
987
Eric Biedermanb138ac82003-04-22 18:44:01 +0000988struct block_set {
989 struct block_set *next;
990 struct block *member;
991};
992struct block {
993 struct block *work_next;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000994 struct triple *first, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +0000995 int edge_count;
996 struct block_set *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000997 int users;
998 struct block_set *use;
999 struct block_set *idominates;
1000 struct block_set *domfrontier;
1001 struct block *idom;
1002 struct block_set *ipdominates;
1003 struct block_set *ipdomfrontier;
1004 struct block *ipdom;
1005 int vertex;
1006
1007};
1008
1009struct symbol {
1010 struct symbol *next;
1011 struct hash_entry *ident;
1012 struct triple *def;
1013 struct type *type;
1014 int scope_depth;
1015};
1016
Eric Biederman90089602004-05-28 14:11:54 +00001017struct macro_arg {
1018 struct macro_arg *next;
1019 struct hash_entry *ident;
1020};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001021struct macro {
1022 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00001023 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001024 int buf_len;
Eric Biederman90089602004-05-28 14:11:54 +00001025 struct macro_arg *args;
1026 int argc;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001027};
1028
1029struct hash_entry {
1030 struct hash_entry *next;
1031 const char *name;
1032 int name_len;
1033 int tok;
1034 struct macro *sym_define;
1035 struct symbol *sym_label;
Eric Biederman83b991a2003-10-11 06:20:25 +00001036 struct symbol *sym_tag;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001037 struct symbol *sym_ident;
1038};
1039
1040#define HASH_TABLE_SIZE 2048
1041
Eric Biederman5ade04a2003-10-22 04:03:46 +00001042struct compiler_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +00001043 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001044 const char *ofilename;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001045 unsigned long flags;
1046 unsigned long debug;
1047 unsigned long max_allocation_passes;
Eric Biederman90089602004-05-28 14:11:54 +00001048
1049 size_t include_path_count;
1050 const char **include_paths;
1051
1052 size_t define_count;
1053 const char **defines;
1054
1055 size_t undef_count;
1056 const char **undefs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001057};
1058struct arch_state {
1059 unsigned long features;
1060};
Eric Biederman90089602004-05-28 14:11:54 +00001061struct basic_blocks {
1062 struct triple *func;
1063 struct triple *first;
1064 struct block *first_block, *last_block;
1065 int last_vertex;
1066};
Eric Biedermancb364952004-11-15 10:46:44 +00001067#define MAX_PP_IF_DEPTH 63
Eric Biederman5ade04a2003-10-22 04:03:46 +00001068struct compile_state {
1069 struct compiler_state *compiler;
1070 struct arch_state *arch;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001071 FILE *output;
Eric Biederman90089602004-05-28 14:11:54 +00001072 FILE *errout;
1073 FILE *dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001074 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001075 struct occurance *last_occurance;
1076 const char *function;
Eric Biederman41203d92004-11-08 09:31:09 +00001077 int token_base;
1078 struct token token[6];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001079 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +00001080 struct hash_entry *i_switch;
1081 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001082 struct hash_entry *i_continue;
1083 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +00001084 struct hash_entry *i_default;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001085 struct hash_entry *i_return;
Eric Biederman90089602004-05-28 14:11:54 +00001086 /* Additional hash entries for predefined macros */
1087 struct hash_entry *i_defined;
1088 struct hash_entry *i___VA_ARGS__;
1089 struct hash_entry *i___FILE__;
1090 struct hash_entry *i___LINE__;
1091 /* Additional hash entries for predefined identifiers */
1092 struct hash_entry *i___func__;
1093 /* Additional hash entries for attributes */
1094 struct hash_entry *i_noinline;
1095 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001096 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001097 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001098 int if_depth;
1099 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001100 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001101 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001102 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001103 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001104 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001105 struct basic_blocks bb;
1106 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001107};
1108
Eric Biederman0babc1c2003-05-09 02:39:00 +00001109/* visibility global/local */
1110/* static/auto duration */
1111/* typedef, register, inline */
1112#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001113#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001114/* Visibility */
1115#define STOR_GLOBAL 0x0001
1116/* Duration */
1117#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001118/* Definition locality */
1119#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001120/* Storage specifiers */
1121#define STOR_AUTO 0x0000
1122#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001123#define STOR_LOCAL 0x0003
1124#define STOR_EXTERN 0x0007
1125#define STOR_INLINE 0x0008
1126#define STOR_REGISTER 0x0010
1127#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001128
Eric Biederman5ade04a2003-10-22 04:03:46 +00001129#define QUAL_SHIFT 5
1130#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001131#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001132#define QUAL_CONST 0x0020
1133#define QUAL_VOLATILE 0x0040
1134#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001135
1136#define TYPE_SHIFT 8
1137#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001138#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1139#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001140#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1141#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001142#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1143#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001144#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1145#define TYPE_DEFAULT 0x0000
1146#define TYPE_VOID 0x0100
1147#define TYPE_CHAR 0x0200
1148#define TYPE_UCHAR 0x0300
1149#define TYPE_SHORT 0x0400
1150#define TYPE_USHORT 0x0500
1151#define TYPE_INT 0x0600
1152#define TYPE_UINT 0x0700
1153#define TYPE_LONG 0x0800
1154#define TYPE_ULONG 0x0900
1155#define TYPE_LLONG 0x0a00 /* long long */
1156#define TYPE_ULLONG 0x0b00
1157#define TYPE_FLOAT 0x0c00
1158#define TYPE_DOUBLE 0x0d00
1159#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001160
1161/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1162#define TYPE_ENUM 0x1600
1163#define TYPE_LIST 0x1700
1164/* TYPE_LIST is a basic building block when defining enumerations
1165 * type->field_ident holds the name of this enumeration entry.
1166 * type->right holds the entry in the list.
1167 */
1168
Eric Biederman0babc1c2003-05-09 02:39:00 +00001169#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001170/* For TYPE_STRUCT
1171 * type->left holds the link list of TYPE_PRODUCT entries that
1172 * make up the structure.
1173 * type->elements hold the length of the linked list
1174 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001175#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001176/* For TYPE_UNION
1177 * type->left holds the link list of TYPE_OVERLAP entries that
1178 * make up the union.
1179 * type->elements hold the length of the linked list
1180 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001181#define TYPE_POINTER 0x1200
1182/* For TYPE_POINTER:
1183 * type->left holds the type pointed to.
1184 */
1185#define TYPE_FUNCTION 0x1300
1186/* For TYPE_FUNCTION:
1187 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001188 * type->right holds the type of the arguments
1189 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001190 */
1191#define TYPE_PRODUCT 0x1400
1192/* TYPE_PRODUCT is a basic building block when defining structures
1193 * type->left holds the type that appears first in memory.
1194 * type->right holds the type that appears next in memory.
1195 */
1196#define TYPE_OVERLAP 0x1500
1197/* TYPE_OVERLAP is a basic building block when defining unions
1198 * type->left and type->right holds to types that overlap
1199 * each other in memory.
1200 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001201#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001202/* TYPE_ARRAY is a basic building block when definitng arrays.
1203 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001204 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001205 */
Eric Biederman90089602004-05-28 14:11:54 +00001206#define TYPE_TUPLE 0x1900
1207/* TYPE_TUPLE is a basic building block when defining
1208 * positionally reference type conglomerations. (i.e. closures)
1209 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1210 * except it has no field names.
1211 * type->left holds the liked list of TYPE_PRODUCT entries that
1212 * make up the closure type.
1213 * type->elements hold the number of elements in the closure.
1214 */
1215#define TYPE_JOIN 0x1a00
1216/* TYPE_JOIN is a basic building block when defining
1217 * positionally reference type conglomerations. (i.e. closures)
1218 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1219 * except it has no field names.
1220 * type->left holds the liked list of TYPE_OVERLAP entries that
1221 * make up the closure type.
1222 * type->elements hold the number of elements in the closure.
1223 */
1224#define TYPE_BITFIELD 0x1b00
1225/* TYPE_BITFIED is the type of a bitfield.
1226 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1227 * type->elements holds the number of bits in the bitfield.
1228 */
1229#define TYPE_UNKNOWN 0x1c00
1230/* TYPE_UNKNOWN is the type of an unknown value.
1231 * Used on unknown consts and other places where I don't know the type.
1232 */
1233
1234#define ATTRIB_SHIFT 16
1235#define ATTRIB_MASK 0xffff0000
1236#define ATTRIB_NOINLINE 0x00010000
1237#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001238
Eric Biederman83b991a2003-10-11 06:20:25 +00001239#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001240
1241struct type {
1242 unsigned int type;
1243 struct type *left, *right;
1244 ulong_t elements;
1245 struct hash_entry *field_ident;
1246 struct hash_entry *type_ident;
1247};
1248
Eric Biederman530b5192003-07-01 10:05:30 +00001249#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001250#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001251#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001252#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001253#define MAX_REGISTERS 75
1254#define REGISTER_BITS 7
1255#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001256#define REG_ERROR 0
1257#define REG_UNSET 1
1258#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001259#define REG_VIRT0 (MAX_REGISTERS + 0)
1260#define REG_VIRT1 (MAX_REGISTERS + 1)
1261#define REG_VIRT2 (MAX_REGISTERS + 2)
1262#define REG_VIRT3 (MAX_REGISTERS + 3)
1263#define REG_VIRT4 (MAX_REGISTERS + 4)
1264#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001265#define REG_VIRT6 (MAX_REGISTERS + 6)
1266#define REG_VIRT7 (MAX_REGISTERS + 7)
1267#define REG_VIRT8 (MAX_REGISTERS + 8)
1268#define REG_VIRT9 (MAX_REGISTERS + 9)
1269
1270#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1271#error "MAX_VIRT_REGISTERS to small"
1272#endif
Eric Biederman90089602004-05-28 14:11:54 +00001273#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001274#error "Too many id bits used"
1275#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001276
1277/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001278#define REG_SHIFT 0
1279#define REGC_SHIFT REGISTER_BITS
1280#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001281#define REG_MASK (MAX_VIRT_REGISTERS -1)
1282#define ID_REG(ID) ((ID) & REG_MASK)
1283#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001284#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1285#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1286#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1287 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001288
Eric Biederman90089602004-05-28 14:11:54 +00001289#define ARCH_INPUT_REGS 4
1290#define ARCH_OUTPUT_REGS 4
1291
1292static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1293static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001294static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001295static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001296static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001297static void arch_reg_equivs(
1298 struct compile_state *state, unsigned *equiv, int reg);
1299static int arch_select_free_register(
1300 struct compile_state *state, char *used, int classes);
1301static unsigned arch_regc_size(struct compile_state *state, int class);
1302static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1303static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1304static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001305static struct reg_info arch_reg_constraint(
1306 struct compile_state *state, struct type *type, const char *constraint);
1307static struct reg_info arch_reg_clobber(
1308 struct compile_state *state, const char *clobber);
1309static struct reg_info arch_reg_lhs(struct compile_state *state,
1310 struct triple *ins, int index);
1311static struct reg_info arch_reg_rhs(struct compile_state *state,
1312 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001313static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001314static struct triple *transform_to_arch_instruction(
1315 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001316static struct triple *flatten(
1317 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001318static void print_dominators(struct compile_state *state,
1319 FILE *fp, struct basic_blocks *bb);
1320static void print_dominance_frontiers(struct compile_state *state,
1321 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001322
1323
Eric Biedermanb138ac82003-04-22 18:44:01 +00001324
Eric Biederman5ade04a2003-10-22 04:03:46 +00001325#define DEBUG_ABORT_ON_ERROR 0x00000001
1326#define DEBUG_BASIC_BLOCKS 0x00000002
1327#define DEBUG_FDOMINATORS 0x00000004
1328#define DEBUG_RDOMINATORS 0x00000008
1329#define DEBUG_TRIPLES 0x00000010
1330#define DEBUG_INTERFERENCE 0x00000020
1331#define DEBUG_SCC_TRANSFORM 0x00000040
1332#define DEBUG_SCC_TRANSFORM2 0x00000080
1333#define DEBUG_REBUILD_SSA_FORM 0x00000100
1334#define DEBUG_INLINE 0x00000200
1335#define DEBUG_RANGE_CONFLICTS 0x00000400
1336#define DEBUG_RANGE_CONFLICTS2 0x00000800
1337#define DEBUG_COLOR_GRAPH 0x00001000
1338#define DEBUG_COLOR_GRAPH2 0x00002000
1339#define DEBUG_COALESCING 0x00004000
1340#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001341#define DEBUG_VERIFICATION 0x00010000
1342#define DEBUG_CALLS 0x00020000
1343#define DEBUG_CALLS2 0x00040000
1344#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001345
1346#define DEBUG_DEFAULT ( \
1347 DEBUG_ABORT_ON_ERROR | \
1348 DEBUG_BASIC_BLOCKS | \
1349 DEBUG_FDOMINATORS | \
1350 DEBUG_RDOMINATORS | \
1351 DEBUG_TRIPLES | \
1352 0 )
1353
Eric Biederman90089602004-05-28 14:11:54 +00001354#define DEBUG_ALL ( \
1355 DEBUG_ABORT_ON_ERROR | \
1356 DEBUG_BASIC_BLOCKS | \
1357 DEBUG_FDOMINATORS | \
1358 DEBUG_RDOMINATORS | \
1359 DEBUG_TRIPLES | \
1360 DEBUG_INTERFERENCE | \
1361 DEBUG_SCC_TRANSFORM | \
1362 DEBUG_SCC_TRANSFORM2 | \
1363 DEBUG_REBUILD_SSA_FORM | \
1364 DEBUG_INLINE | \
1365 DEBUG_RANGE_CONFLICTS | \
1366 DEBUG_RANGE_CONFLICTS2 | \
1367 DEBUG_COLOR_GRAPH | \
1368 DEBUG_COLOR_GRAPH2 | \
1369 DEBUG_COALESCING | \
1370 DEBUG_COALESCING2 | \
1371 DEBUG_VERIFICATION | \
1372 DEBUG_CALLS | \
1373 DEBUG_CALLS2 | \
1374 DEBUG_TOKENS | \
1375 0 )
1376
1377#define COMPILER_INLINE_MASK 0x00000007
1378#define COMPILER_INLINE_ALWAYS 0x00000000
1379#define COMPILER_INLINE_NEVER 0x00000001
1380#define COMPILER_INLINE_DEFAULTON 0x00000002
1381#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1382#define COMPILER_INLINE_NOPENALTY 0x00000004
1383#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1384#define COMPILER_SIMPLIFY 0x00000010
1385#define COMPILER_SCC_TRANSFORM 0x00000020
1386#define COMPILER_SIMPLIFY_OP 0x00000040
1387#define COMPILER_SIMPLIFY_PHI 0x00000080
1388#define COMPILER_SIMPLIFY_LABEL 0x00000100
1389#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1390#define COMPILER_SIMPLIFY_COPY 0x00000400
1391#define COMPILER_SIMPLIFY_ARITH 0x00000800
1392#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1393#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1394#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1395#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1396
Eric Biedermancb364952004-11-15 10:46:44 +00001397#define COMPILER_TRIGRAPHS 0x40000000
1398#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001399
1400#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001401 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001402 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001403 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001404 COMPILER_SIMPLIFY_OP | \
1405 COMPILER_SIMPLIFY_PHI | \
1406 COMPILER_SIMPLIFY_LABEL | \
1407 COMPILER_SIMPLIFY_BRANCH | \
1408 COMPILER_SIMPLIFY_COPY | \
1409 COMPILER_SIMPLIFY_ARITH | \
1410 COMPILER_SIMPLIFY_SHIFT | \
1411 COMPILER_SIMPLIFY_BITWISE | \
1412 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001413 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001414 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001415
Eric Biederman153ea352003-06-20 14:43:20 +00001416#define GLOBAL_SCOPE_DEPTH 1
1417#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001418
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001419static void compile_file(struct compile_state *old_state, const char *filename, int local);
1420
Eric Biederman5ade04a2003-10-22 04:03:46 +00001421
1422
1423static void init_compiler_state(struct compiler_state *compiler)
1424{
1425 memset(compiler, 0, sizeof(*compiler));
1426 compiler->label_prefix = "";
1427 compiler->ofilename = "auto.inc";
1428 compiler->flags = COMPILER_DEFAULT_FLAGS;
1429 compiler->debug = 0;
1430 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001431 compiler->include_path_count = 1;
1432 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1433 compiler->define_count = 1;
1434 compiler->defines = xcmalloc(sizeof(char *), "defines");
1435 compiler->undef_count = 1;
1436 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001437}
1438
1439struct compiler_flag {
1440 const char *name;
1441 unsigned long flag;
1442};
Eric Biederman90089602004-05-28 14:11:54 +00001443
1444struct compiler_arg {
1445 const char *name;
1446 unsigned long mask;
1447 struct compiler_flag flags[16];
1448};
1449
Eric Biederman5ade04a2003-10-22 04:03:46 +00001450static int set_flag(
1451 const struct compiler_flag *ptr, unsigned long *flags,
1452 int act, const char *flag)
1453{
1454 int result = -1;
1455 for(; ptr->name; ptr++) {
1456 if (strcmp(ptr->name, flag) == 0) {
1457 break;
1458 }
1459 }
1460 if (ptr->name) {
1461 result = 0;
1462 *flags &= ~(ptr->flag);
1463 if (act) {
1464 *flags |= ptr->flag;
1465 }
1466 }
1467 return result;
1468}
1469
Eric Biederman90089602004-05-28 14:11:54 +00001470static int set_arg(
1471 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1472{
1473 const char *val;
1474 int result = -1;
1475 int len;
1476 val = strchr(arg, '=');
1477 if (val) {
1478 len = val - arg;
1479 val++;
1480 for(; ptr->name; ptr++) {
1481 if (strncmp(ptr->name, arg, len) == 0) {
1482 break;
1483 }
1484 }
1485 if (ptr->name) {
1486 *flags &= ~ptr->mask;
1487 result = set_flag(&ptr->flags[0], flags, 1, val);
1488 }
1489 }
1490 return result;
1491}
1492
1493
1494static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
1495 const char *prefix, const char *invert_prefix)
1496{
1497 for(;ptr->name; ptr++) {
1498 fprintf(fp, "%s%s\n", prefix, ptr->name);
1499 if (invert_prefix) {
1500 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1501 }
1502 }
1503}
1504
1505static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1506 const char *prefix)
1507{
1508 for(;ptr->name; ptr++) {
1509 const struct compiler_flag *flag;
1510 for(flag = &ptr->flags[0]; flag->name; flag++) {
1511 fprintf(fp, "%s%s=%s\n",
1512 prefix, ptr->name, flag->name);
1513 }
1514 }
1515}
1516
1517static int append_string(size_t *max, const char ***vec, const char *str,
1518 const char *name)
1519{
1520 size_t count;
1521 count = ++(*max);
1522 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1523 (*vec)[count -1] = 0;
1524 (*vec)[count -2] = str;
1525 return 0;
1526}
1527
1528static void arg_error(char *fmt, ...);
1529static const char *identifier(const char *str, const char *end);
1530
1531static int append_include_path(struct compiler_state *compiler, const char *str)
1532{
1533 int result;
1534 if (!exists(str, ".")) {
1535 arg_error("Nonexistent include path: `%s'\n",
1536 str);
1537 }
1538 result = append_string(&compiler->include_path_count,
1539 &compiler->include_paths, str, "include_paths");
1540 return result;
1541}
1542
1543static int append_define(struct compiler_state *compiler, const char *str)
1544{
1545 const char *end, *rest;
1546 int result;
1547
1548 end = strchr(str, '=');
1549 if (!end) {
1550 end = str + strlen(str);
1551 }
1552 rest = identifier(str, end);
1553 if (rest != end) {
1554 int len = end - str - 1;
1555 arg_error("Invalid name cannot define macro: `%*.*s'\n",
1556 len, len, str);
1557 }
1558 result = append_string(&compiler->define_count,
1559 &compiler->defines, str, "defines");
1560 return result;
1561}
1562
1563static int append_undef(struct compiler_state *compiler, const char *str)
1564{
1565 const char *end, *rest;
1566 int result;
1567
1568 end = str + strlen(str);
1569 rest = identifier(str, end);
1570 if (rest != end) {
1571 int len = end - str - 1;
1572 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
1573 len, len, str);
1574 }
1575 result = append_string(&compiler->undef_count,
1576 &compiler->undefs, str, "undefs");
1577 return result;
1578}
1579
1580static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001581 { "trigraphs", COMPILER_TRIGRAPHS },
1582 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001583 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1584 { "simplify", COMPILER_SIMPLIFY },
1585 { "scc-transform", COMPILER_SCC_TRANSFORM },
1586 { "simplify-op", COMPILER_SIMPLIFY_OP },
1587 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1588 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1589 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1590 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1591 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1592 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1593 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1594 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1595 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1596 { 0, 0 },
1597};
1598static const struct compiler_arg romcc_args[] = {
1599 { "inline-policy", COMPILER_INLINE_MASK,
1600 {
1601 { "always", COMPILER_INLINE_ALWAYS, },
1602 { "never", COMPILER_INLINE_NEVER, },
1603 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1604 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1605 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1606 { 0, 0 },
1607 },
1608 },
1609 { 0, 0 },
1610};
1611static const struct compiler_flag romcc_opt_flags[] = {
1612 { "-O", COMPILER_SIMPLIFY },
1613 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001614 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001615 { 0, 0, },
1616};
1617static const struct compiler_flag romcc_debug_flags[] = {
1618 { "all", DEBUG_ALL },
1619 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1620 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1621 { "fdominators", DEBUG_FDOMINATORS },
1622 { "rdominators", DEBUG_RDOMINATORS },
1623 { "triples", DEBUG_TRIPLES },
1624 { "interference", DEBUG_INTERFERENCE },
1625 { "scc-transform", DEBUG_SCC_TRANSFORM },
1626 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1627 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1628 { "inline", DEBUG_INLINE },
1629 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1630 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1631 { "color-graph", DEBUG_COLOR_GRAPH },
1632 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1633 { "coalescing", DEBUG_COALESCING },
1634 { "coalescing2", DEBUG_COALESCING2 },
1635 { "verification", DEBUG_VERIFICATION },
1636 { "calls", DEBUG_CALLS },
1637 { "calls2", DEBUG_CALLS2 },
1638 { "tokens", DEBUG_TOKENS },
1639 { 0, 0 },
1640};
1641
Eric Biederman5ade04a2003-10-22 04:03:46 +00001642static int compiler_encode_flag(
1643 struct compiler_state *compiler, const char *flag)
1644{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001645 int act;
1646 int result;
1647
1648 act = 1;
1649 result = -1;
1650 if (strncmp(flag, "no-", 3) == 0) {
1651 flag += 3;
1652 act = 0;
1653 }
1654 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001655 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1656 }
1657 else if (strncmp(flag, "-E", 2) == 0) {
1658 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1659 }
1660 else if (strncmp(flag, "-I", 2) == 0) {
1661 result = append_include_path(compiler, flag + 2);
1662 }
1663 else if (strncmp(flag, "-D", 2) == 0) {
1664 result = append_define(compiler, flag + 2);
1665 }
1666 else if (strncmp(flag, "-U", 2) == 0) {
1667 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001668 }
1669 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1670 result = 0;
1671 compiler->label_prefix = flag + 13;
1672 }
1673 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1674 unsigned long max_passes;
1675 char *end;
1676 max_passes = strtoul(flag + 22, &end, 10);
1677 if (end[0] == '\0') {
1678 result = 0;
1679 compiler->max_allocation_passes = max_passes;
1680 }
1681 }
1682 else if (act && strcmp(flag, "debug") == 0) {
1683 result = 0;
1684 compiler->debug |= DEBUG_DEFAULT;
1685 }
1686 else if (strncmp(flag, "debug-", 6) == 0) {
1687 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001688 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001689 }
1690 else {
Eric Biederman90089602004-05-28 14:11:54 +00001691 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1692 if (result < 0) {
1693 result = set_arg(romcc_args, &compiler->flags, flag);
1694 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001695 }
1696 return result;
1697}
1698
Eric Biederman90089602004-05-28 14:11:54 +00001699static void compiler_usage(FILE *fp)
1700{
1701 flag_usage(fp, romcc_opt_flags, "", 0);
1702 flag_usage(fp, romcc_flags, "-f", "-fno-");
1703 arg_usage(fp, romcc_args, "-f");
1704 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1705 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1706 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1707 fprintf(fp, "-I<include path>\n");
1708 fprintf(fp, "-D<macro>[=defn]\n");
1709 fprintf(fp, "-U<macro>\n");
1710}
1711
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001712static void do_cleanup(struct compile_state *state)
1713{
1714 if (state->output) {
1715 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001716 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001717 state->output = 0;
1718 }
1719 if (state->dbgout) {
1720 fflush(state->dbgout);
1721 }
1722 if (state->errout) {
1723 fflush(state->errout);
1724 }
1725}
1726
1727static struct compile_state *exit_state;
1728static void exit_cleanup(void)
1729{
1730 if (exit_state) {
1731 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001732 }
1733}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001734
1735static int get_col(struct file_state *file)
1736{
1737 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001738 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001739 ptr = file->line_start;
1740 end = file->pos;
1741 for(col = 0; ptr < end; ptr++) {
1742 if (*ptr != '\t') {
1743 col++;
1744 }
1745 else {
1746 col = (col & ~7) + 8;
1747 }
1748 }
1749 return col;
1750}
1751
1752static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1753{
1754 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001755 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001756 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001757 for(spot = triple->occurance; spot; spot = spot->parent) {
1758 fprintf(fp, "%s:%d.%d: ",
1759 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001760 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001761 return;
1762 }
1763 if (!state->file) {
1764 return;
1765 }
1766 col = get_col(state->file);
1767 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001768 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001769}
1770
Eric Biederman5ade04a2003-10-22 04:03:46 +00001771static void internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001772 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001773{
Eric Biederman90089602004-05-28 14:11:54 +00001774 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001775 va_list args;
1776 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001777 loc(fp, state, ptr);
1778 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001779 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001780 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001781 }
Eric Biederman90089602004-05-28 14:11:54 +00001782 fprintf(fp, "Internal compiler error: ");
1783 vfprintf(fp, fmt, args);
1784 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001785 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001786 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001787 abort();
1788}
1789
1790
Eric Biederman5ade04a2003-10-22 04:03:46 +00001791static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001792 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001793{
Eric Biederman90089602004-05-28 14:11:54 +00001794 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001795 va_list args;
1796 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001797 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001798 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001799 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001800 }
Eric Biederman90089602004-05-28 14:11:54 +00001801 fprintf(fp, "Internal compiler warning: ");
1802 vfprintf(fp, fmt, args);
1803 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001804 va_end(args);
1805}
1806
1807
1808
Eric Biederman5ade04a2003-10-22 04:03:46 +00001809static void error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001810 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001811{
Eric Biederman90089602004-05-28 14:11:54 +00001812 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001813 va_list args;
1814 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001815 loc(fp, state, ptr);
1816 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001817 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001818 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001819 }
Eric Biederman90089602004-05-28 14:11:54 +00001820 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001821 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001822 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001823 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001824 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001825 abort();
1826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001827 exit(1);
1828}
1829
Eric Biederman5ade04a2003-10-22 04:03:46 +00001830static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001831 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001832{
Eric Biederman90089602004-05-28 14:11:54 +00001833 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001834 va_list args;
1835 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001836 loc(fp, state, ptr);
1837 fprintf(fp, "warning: ");
1838 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1839 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1840 }
1841 vfprintf(fp, fmt, args);
1842 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001843 va_end(args);
1844}
1845
Eric Biedermanb138ac82003-04-22 18:44:01 +00001846#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1847
Eric Biederman0babc1c2003-05-09 02:39:00 +00001848static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001849{
1850 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001851 if (op >= OP_MAX) {
1852 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001853 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001854 if (op < 0) {
1855 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001856 }
1857}
1858
Eric Biederman0babc1c2003-05-09 02:39:00 +00001859static void valid_ins(struct compile_state *state, struct triple *ptr)
1860{
1861 valid_op(state, ptr->op);
1862}
1863
Stefan Reinauer50542a82007-10-24 11:14:14 +00001864#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001865static void valid_param_count(struct compile_state *state, struct triple *ins)
1866{
1867 int lhs, rhs, misc, targ;
1868 valid_ins(state, ins);
1869 lhs = table_ops[ins->op].lhs;
1870 rhs = table_ops[ins->op].rhs;
1871 misc = table_ops[ins->op].misc;
1872 targ = table_ops[ins->op].targ;
1873
1874 if ((lhs >= 0) && (ins->lhs != lhs)) {
1875 internal_error(state, ins, "Bad lhs count");
1876 }
1877 if ((rhs >= 0) && (ins->rhs != rhs)) {
1878 internal_error(state, ins, "Bad rhs count");
1879 }
1880 if ((misc >= 0) && (ins->misc != misc)) {
1881 internal_error(state, ins, "Bad misc count");
1882 }
1883 if ((targ >= 0) && (ins->targ != targ)) {
1884 internal_error(state, ins, "Bad targ count");
1885 }
1886}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001887#endif
Eric Biederman90089602004-05-28 14:11:54 +00001888
Eric Biedermanb138ac82003-04-22 18:44:01 +00001889static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001890static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001891static void use_triple(struct triple *used, struct triple *user)
1892{
1893 struct triple_set **ptr, *new;
1894 if (!used)
1895 return;
1896 if (!user)
1897 return;
1898 ptr = &used->use;
1899 while(*ptr) {
1900 if ((*ptr)->member == user) {
1901 return;
1902 }
1903 ptr = &(*ptr)->next;
1904 }
1905 /* Append new to the head of the list,
1906 * copy_func and rename_block_variables
1907 * depends on this.
1908 */
1909 new = xcmalloc(sizeof(*new), "triple_set");
1910 new->member = user;
1911 new->next = used->use;
1912 used->use = new;
1913}
1914
1915static void unuse_triple(struct triple *used, struct triple *unuser)
1916{
1917 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001918 if (!used) {
1919 return;
1920 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001921 ptr = &used->use;
1922 while(*ptr) {
1923 use = *ptr;
1924 if (use->member == unuser) {
1925 *ptr = use->next;
1926 xfree(use);
1927 }
1928 else {
1929 ptr = &use->next;
1930 }
1931 }
1932}
1933
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001934static void put_occurance(struct occurance *occurance)
1935{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001936 if (occurance) {
1937 occurance->count -= 1;
1938 if (occurance->count <= 0) {
1939 if (occurance->parent) {
1940 put_occurance(occurance->parent);
1941 }
1942 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001943 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001944 }
1945}
1946
1947static void get_occurance(struct occurance *occurance)
1948{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001949 if (occurance) {
1950 occurance->count += 1;
1951 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001952}
1953
1954
1955static struct occurance *new_occurance(struct compile_state *state)
1956{
1957 struct occurance *result, *last;
1958 const char *filename;
1959 const char *function;
1960 int line, col;
1961
1962 function = "";
1963 filename = 0;
1964 line = 0;
1965 col = 0;
1966 if (state->file) {
1967 filename = state->file->report_name;
1968 line = state->file->report_line;
1969 col = get_col(state->file);
1970 }
1971 if (state->function) {
1972 function = state->function;
1973 }
1974 last = state->last_occurance;
1975 if (last &&
1976 (last->col == col) &&
1977 (last->line == line) &&
1978 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001979 ((last->filename == filename) ||
1980 (strcmp(last->filename, filename) == 0)))
1981 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001982 get_occurance(last);
1983 return last;
1984 }
1985 if (last) {
1986 state->last_occurance = 0;
1987 put_occurance(last);
1988 }
1989 result = xmalloc(sizeof(*result), "occurance");
1990 result->count = 2;
1991 result->filename = filename;
1992 result->function = function;
1993 result->line = line;
1994 result->col = col;
1995 result->parent = 0;
1996 state->last_occurance = result;
1997 return result;
1998}
1999
2000static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002001 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002002{
2003 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002004 if (top->parent) {
2005 internal_error(state, 0, "inlining an already inlined function?");
2006 }
2007 /* If I have a null base treat it that way */
2008 if ((base->parent == 0) &&
2009 (base->col == 0) &&
2010 (base->line == 0) &&
2011 (base->function[0] == '\0') &&
2012 (base->filename[0] == '\0')) {
2013 base = 0;
2014 }
2015 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002016 last = state->last_occurance;
2017 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002018 (last->parent == base) &&
2019 (last->col == top->col) &&
2020 (last->line == top->line) &&
2021 (last->function == top->function) &&
2022 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002023 get_occurance(last);
2024 return last;
2025 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002026 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002027 if (last) {
2028 state->last_occurance = 0;
2029 put_occurance(last);
2030 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002031 /* Generate a new occurance structure */
2032 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002033 result = xmalloc(sizeof(*result), "occurance");
2034 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002035 result->filename = top->filename;
2036 result->function = top->function;
2037 result->line = top->line;
2038 result->col = top->col;
2039 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002040 state->last_occurance = result;
2041 return result;
2042}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002043
2044static struct occurance dummy_occurance = {
2045 .count = 2,
2046 .filename = __FILE__,
2047 .function = "",
2048 .line = __LINE__,
2049 .col = 0,
2050 .parent = 0,
2051};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002052
Eric Biederman90089602004-05-28 14:11:54 +00002053/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002054 * from a triple. Having allows certain sanity checks to pass even
2055 * when the original triple that was pointed to is gone.
2056 */
Eric Biederman90089602004-05-28 14:11:54 +00002057static struct triple unknown_triple = {
2058 .next = &unknown_triple,
2059 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002060 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002061 .op = OP_UNKNOWNVAL,
2062 .lhs = 0,
2063 .rhs = 0,
2064 .misc = 0,
2065 .targ = 0,
2066 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002067 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002068 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002069 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002070 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002071};
2072
Eric Biederman0babc1c2003-05-09 02:39:00 +00002073
Eric Biederman90089602004-05-28 14:11:54 +00002074static size_t registers_of(struct compile_state *state, struct type *type);
2075
2076static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002077 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2078 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002079{
Eric Biederman90089602004-05-28 14:11:54 +00002080 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002081 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002082 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002083 dummy.op = op;
2084 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002085 valid_op(state, op);
2086 lhs = table_ops[op].lhs;
2087 rhs = table_ops[op].rhs;
2088 misc = table_ops[op].misc;
2089 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002090
2091 switch(op) {
2092 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002093 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002094 break;
2095 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002096 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002097 break;
2098 case OP_ADECL:
2099 lhs = registers_of(state, type);
2100 break;
2101 case OP_TUPLE:
2102 lhs = registers_of(state, type);
2103 break;
2104 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002105 rhs = rhs_wanted;
2106 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002107 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002108 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002109 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002110 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002111 }
2112 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002113 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002114 }
2115 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002116 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002117 }
2118 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002119 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002120 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002121
2122 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002123 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002124 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2125
2126 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2127 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002128 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002129 ret->lhs = lhs;
2130 ret->rhs = rhs;
2131 ret->misc = misc;
2132 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002133 ret->type = type;
2134 ret->next = ret;
2135 ret->prev = ret;
2136 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002137 /* A simple sanity check */
2138 if ((ret->op != op) ||
2139 (ret->lhs != lhs) ||
2140 (ret->rhs != rhs) ||
2141 (ret->misc != misc) ||
2142 (ret->targ != targ) ||
2143 (ret->type != type) ||
2144 (ret->next != ret) ||
2145 (ret->prev != ret) ||
2146 (ret->occurance != occurance)) {
2147 internal_error(state, ret, "huh?");
2148 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002149 return ret;
2150}
2151
Eric Biederman0babc1c2003-05-09 02:39:00 +00002152struct triple *dup_triple(struct compile_state *state, struct triple *src)
2153{
2154 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002155 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002156 src_lhs = src->lhs;
2157 src_rhs = src->rhs;
2158 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002159 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002160 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002161 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002162 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002163 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002164 return dup;
2165}
2166
Eric Biederman41203d92004-11-08 09:31:09 +00002167static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2168{
2169 struct triple *copy;
2170 copy = dup_triple(state, src);
2171 copy->use = 0;
2172 copy->next = copy->prev = copy;
2173 return copy;
2174}
2175
Eric Biederman0babc1c2003-05-09 02:39:00 +00002176static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002177 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002178{
2179 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002180 struct occurance *occurance;
2181 occurance = new_occurance(state);
2182 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002183 return ret;
2184}
2185
2186static struct triple *build_triple(struct compile_state *state,
2187 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002188 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002189{
2190 struct triple *ret;
2191 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002192 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002193 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002194 if (count > 0) {
2195 ret->param[0] = left;
2196 }
2197 if (count > 1) {
2198 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002199 }
2200 return ret;
2201}
2202
Eric Biederman0babc1c2003-05-09 02:39:00 +00002203static struct triple *triple(struct compile_state *state,
2204 int op, struct type *type, struct triple *left, struct triple *right)
2205{
2206 struct triple *ret;
2207 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002208 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002209 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002210 if (count >= 1) {
2211 ret->param[0] = left;
2212 }
2213 if (count >= 2) {
2214 ret->param[1] = right;
2215 }
2216 return ret;
2217}
2218
2219static struct triple *branch(struct compile_state *state,
2220 struct triple *targ, struct triple *test)
2221{
2222 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002223 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002224 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002225 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002226 } else {
2227 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002228 }
2229 TARG(ret, 0) = targ;
2230 /* record the branch target was used */
2231 if (!targ || (targ->op != OP_LABEL)) {
2232 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002233 }
2234 return ret;
2235}
2236
Eric Biederman90089602004-05-28 14:11:54 +00002237static int triple_is_label(struct compile_state *state, struct triple *ins);
2238static int triple_is_call(struct compile_state *state, struct triple *ins);
2239static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002240static void insert_triple(struct compile_state *state,
2241 struct triple *first, struct triple *ptr)
2242{
2243 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002244 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002245 internal_error(state, ptr, "expression already used");
2246 }
2247 ptr->next = first;
2248 ptr->prev = first->prev;
2249 ptr->prev->next = ptr;
2250 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002251
2252 if (triple_is_cbranch(state, ptr->prev) ||
2253 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002254 unuse_triple(first, ptr->prev);
2255 use_triple(ptr, ptr->prev);
2256 }
2257 }
2258}
2259
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002260static int triple_stores_block(struct compile_state *state, struct triple *ins)
2261{
2262 /* This function is used to determine if u.block
2263 * is utilized to store the current block number.
2264 */
2265 int stores_block;
2266 valid_ins(state, ins);
2267 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2268 return stores_block;
2269}
2270
Eric Biederman90089602004-05-28 14:11:54 +00002271static int triple_is_branch(struct compile_state *state, struct triple *ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002272static struct block *block_of_triple(struct compile_state *state,
2273 struct triple *ins)
2274{
2275 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002276 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002277 return 0;
2278 }
2279 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002280 while(ins != first && !triple_is_branch(state, ins->prev) &&
2281 !triple_stores_block(state, ins))
2282 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002283 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002284 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002285 }
2286 ins = ins->prev;
2287 }
Eric Biederman90089602004-05-28 14:11:54 +00002288 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002289}
2290
Eric Biederman90089602004-05-28 14:11:54 +00002291static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002292static struct triple *pre_triple(struct compile_state *state,
2293 struct triple *base,
2294 int op, struct type *type, struct triple *left, struct triple *right)
2295{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002296 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002297 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002298 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002299 /* If I am an OP_PIECE jump to the real instruction */
2300 if (base->op == OP_PIECE) {
2301 base = MISC(base, 0);
2302 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002303 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002304 get_occurance(base->occurance);
2305 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002306 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002307 if (triple_stores_block(state, ret)) {
2308 ret->u.block = block;
2309 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002310 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002311 for(i = 0; i < ret->lhs; i++) {
2312 struct triple *piece;
2313 piece = LHS(ret, i);
2314 insert_triple(state, base, piece);
2315 use_triple(ret, piece);
2316 use_triple(piece, ret);
2317 }
2318 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002319 block->first = ret;
2320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002321 return ret;
2322}
2323
2324static struct triple *post_triple(struct compile_state *state,
2325 struct triple *base,
2326 int op, struct type *type, struct triple *left, struct triple *right)
2327{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002328 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002329 struct triple *ret, *next;
2330 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002331 /* If I am an OP_PIECE jump to the real instruction */
2332 if (base->op == OP_PIECE) {
2333 base = MISC(base, 0);
2334 }
2335 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002336 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002337 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002338 base = LHS(base, zlhs - 1);
2339 }
2340
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002341 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002342 get_occurance(base->occurance);
2343 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002344 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002345 if (triple_stores_block(state, ret)) {
2346 ret->u.block = block;
2347 }
Eric Biederman90089602004-05-28 14:11:54 +00002348 next = base->next;
2349 insert_triple(state, next, ret);
2350 zlhs = ret->lhs;
2351 for(i = 0; i < zlhs; i++) {
2352 struct triple *piece;
2353 piece = LHS(ret, i);
2354 insert_triple(state, next, piece);
2355 use_triple(ret, piece);
2356 use_triple(piece, ret);
2357 }
2358 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002359 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002360 if (zlhs) {
2361 block->last = LHS(ret, zlhs - 1);
2362 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002363 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002364 return ret;
2365}
2366
Eric Biederman90089602004-05-28 14:11:54 +00002367static struct type *reg_type(
2368 struct compile_state *state, struct type *type, int reg);
2369
2370static void generate_lhs_piece(
2371 struct compile_state *state, struct triple *ins, int index)
2372{
2373 struct type *piece_type;
2374 struct triple *piece;
2375 get_occurance(ins->occurance);
2376 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2377
2378 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2379 piece_type = piece_type->left;
2380 }
2381#if 0
2382{
2383 static void name_of(FILE *fp, struct type *type);
2384 FILE * fp = state->errout;
2385 fprintf(fp, "piece_type(%d): ", index);
2386 name_of(fp, piece_type);
2387 fprintf(fp, "\n");
2388}
2389#endif
2390 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2391 piece->u.cval = index;
2392 LHS(ins, piece->u.cval) = piece;
2393 MISC(piece, 0) = ins;
2394}
2395
2396static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2397{
2398 int i, zlhs;
2399 zlhs = ins->lhs;
2400 for(i = 0; i < zlhs; i++) {
2401 generate_lhs_piece(state, ins, i);
2402 }
2403}
2404
Eric Biedermanb138ac82003-04-22 18:44:01 +00002405static struct triple *label(struct compile_state *state)
2406{
2407 /* Labels don't get a type */
2408 struct triple *result;
2409 result = triple(state, OP_LABEL, &void_type, 0, 0);
2410 return result;
2411}
2412
Eric Biederman90089602004-05-28 14:11:54 +00002413static struct triple *mkprog(struct compile_state *state, ...)
2414{
2415 struct triple *prog, *head, *arg;
2416 va_list args;
2417 int i;
2418
2419 head = label(state);
2420 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2421 RHS(prog, 0) = head;
2422 va_start(args, state);
2423 i = 0;
2424 while((arg = va_arg(args, struct triple *)) != 0) {
2425 if (++i >= 100) {
2426 internal_error(state, 0, "too many arguments to mkprog");
2427 }
2428 flatten(state, head, arg);
2429 }
2430 va_end(args);
2431 prog->type = head->prev->type;
2432 return prog;
2433}
2434static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002435static void display_triple(FILE *fp, struct triple *ins)
2436{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002437 struct occurance *ptr;
2438 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002439 char pre, post, vol;
2440 pre = post = vol = ' ';
2441 if (ins) {
2442 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2443 pre = '^';
2444 }
2445 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2446 post = ',';
2447 }
2448 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2449 vol = 'v';
2450 }
2451 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002452 }
Eric Biederman90089602004-05-28 14:11:54 +00002453 if (ins == 0) {
2454 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002455 }
Eric Biederman90089602004-05-28 14:11:54 +00002456 else if (ins->op == OP_INTCONST) {
2457 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
2458 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002459 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002460 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002461 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002462 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2463 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2464 MISC(ins, 0), (unsigned long)(ins->u.cval));
2465 }
2466 else if (ins->op == OP_INDEX) {
2467 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2468 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2469 RHS(ins, 0), (unsigned long)(ins->u.cval));
2470 }
2471 else if (ins->op == OP_PIECE) {
2472 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2473 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002474 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002475 }
2476 else {
2477 int i, count;
Eric Biederman90089602004-05-28 14:11:54 +00002478 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
2479 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2480 if (table_ops[ins->op].flags & BITFIELD) {
2481 fprintf(fp, " <%2d-%2d:%2d>",
2482 ins->u.bitfield.offset,
2483 ins->u.bitfield.offset + ins->u.bitfield.size,
2484 ins->u.bitfield.size);
2485 }
2486 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002487 for(i = 0; i < count; i++) {
2488 fprintf(fp, " %-10p", ins->param[i]);
2489 }
2490 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002491 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002492 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002493 }
Eric Biederman90089602004-05-28 14:11:54 +00002494 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002495 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002496#if DEBUG_DISPLAY_TYPES
2497 fprintf(fp, " <");
2498 name_of(fp, ins->type);
2499 fprintf(fp, "> ");
2500#endif
2501#if DEBUG_DISPLAY_USES
2502 fprintf(fp, " [");
2503 for(user = ins->use; user; user = user->next) {
2504 fprintf(fp, " %-10p", user->member);
2505 }
2506 fprintf(fp, " ]");
2507#endif
2508 fprintf(fp, " @");
2509 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2510 fprintf(fp, " %s,%s:%d.%d",
2511 ptr->function,
2512 ptr->filename,
2513 ptr->line,
2514 ptr->col);
2515 }
2516 if (ins->op == OP_ASM) {
2517 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002518 }
2519 }
Eric Biederman90089602004-05-28 14:11:54 +00002520 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002521 fflush(fp);
2522}
2523
Eric Biederman90089602004-05-28 14:11:54 +00002524static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002525static void display_triple_changes(
2526 FILE *fp, const struct triple *new, const struct triple *orig)
2527{
2528
2529 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002530 new_count = TRIPLE_SIZE(new);
2531 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002532 if ((new->op != orig->op) ||
2533 (new_count != orig_count) ||
2534 (memcmp(orig->param, new->param,
2535 orig_count * sizeof(orig->param[0])) != 0) ||
2536 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
2537 {
2538 struct occurance *ptr;
2539 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002540 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002541 if (orig->op == new->op) {
2542 fprintf(fp, " %-11s", tops(orig->op));
2543 } else {
2544 fprintf(fp, " [%-10s %-10s]",
2545 tops(new->op), tops(orig->op));
2546 }
2547 min_count = new_count;
2548 if (min_count > orig_count) {
2549 min_count = orig_count;
2550 }
2551 for(indent = i = 0; i < min_count; i++) {
2552 if (orig->param[i] == new->param[i]) {
2553 fprintf(fp, " %-11p",
2554 orig->param[i]);
2555 indent += 12;
2556 } else {
2557 fprintf(fp, " [%-10p %-10p]",
2558 new->param[i],
2559 orig->param[i]);
2560 indent += 24;
2561 }
2562 }
2563 for(; i < orig_count; i++) {
2564 fprintf(fp, " [%-9p]", orig->param[i]);
2565 indent += 12;
2566 }
2567 for(; i < new_count; i++) {
2568 fprintf(fp, " [%-9p]", new->param[i]);
2569 indent += 12;
2570 }
2571 if ((new->op == OP_INTCONST)||
2572 (new->op == OP_ADDRCONST)) {
2573 fprintf(fp, " <0x%08lx>",
2574 (unsigned long)(new->u.cval));
2575 indent += 13;
2576 }
2577 for(;indent < 36; indent++) {
2578 putc(' ', fp);
2579 }
Eric Biederman90089602004-05-28 14:11:54 +00002580
2581#if DEBUG_DISPLAY_TYPES
2582 fprintf(fp, " <");
2583 name_of(fp, new->type);
2584 if (!equiv_types(new->type, orig->type)) {
2585 fprintf(fp, " -- ");
2586 name_of(fp, orig->type);
2587 }
2588 fprintf(fp, "> ");
2589#endif
2590
Eric Biederman5ade04a2003-10-22 04:03:46 +00002591 fprintf(fp, " @");
2592 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2593 fprintf(fp, " %s,%s:%d.%d",
2594 ptr->function,
2595 ptr->filename,
2596 ptr->line,
2597 ptr->col);
2598
2599 }
2600 fprintf(fp, "\n");
2601 fflush(fp);
2602 }
2603}
2604
Eric Biederman83b991a2003-10-11 06:20:25 +00002605static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002606{
2607 /* Does the triple have no side effects.
2608 * I.e. Rexecuting the triple with the same arguments
2609 * gives the same value.
2610 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002611 unsigned pure;
2612 valid_ins(state, ins);
2613 pure = PURE_BITS(table_ops[ins->op].flags);
2614 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002615 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002616 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002617 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002618 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002619}
2620
Eric Biederman90089602004-05-28 14:11:54 +00002621static int triple_is_branch_type(struct compile_state *state,
2622 struct triple *ins, unsigned type)
2623{
2624 /* Is this one of the passed branch types? */
2625 valid_ins(state, ins);
2626 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2627}
2628
Eric Biederman0babc1c2003-05-09 02:39:00 +00002629static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002630{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002631 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002632 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002633 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002634}
2635
Eric Biederman90089602004-05-28 14:11:54 +00002636static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002637{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002638 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002639 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002640}
2641
Eric Biederman90089602004-05-28 14:11:54 +00002642static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002643{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002644 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002645 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002646 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002647 type = BRANCH_BITS(table_ops[ins->op].flags);
2648 return (type != 0) && (type != CBRANCH);
2649}
2650
2651static int triple_is_call(struct compile_state *state, struct triple *ins)
2652{
2653 /* Is this triple a call instruction? */
2654 return triple_is_branch_type(state, ins, CALLBRANCH);
2655}
2656
2657static int triple_is_ret(struct compile_state *state, struct triple *ins)
2658{
2659 /* Is this triple a return instruction? */
2660 return triple_is_branch_type(state, ins, RETBRANCH);
2661}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002662
2663#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002664static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2665{
2666 /* Is this triple an unconditional branch and not a call or a
2667 * return? */
2668 return triple_is_branch_type(state, ins, UBRANCH);
2669}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002670#endif
Eric Biederman90089602004-05-28 14:11:54 +00002671
2672static int triple_is_end(struct compile_state *state, struct triple *ins)
2673{
2674 return triple_is_branch_type(state, ins, ENDBRANCH);
2675}
2676
2677static int triple_is_label(struct compile_state *state, struct triple *ins)
2678{
2679 valid_ins(state, ins);
2680 return (ins->op == OP_LABEL);
2681}
2682
2683static struct triple *triple_to_block_start(
2684 struct compile_state *state, struct triple *start)
2685{
2686 while(!triple_is_branch(state, start->prev) &&
2687 (!triple_is_label(state, start) || !start->use)) {
2688 start = start->prev;
2689 }
2690 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002691}
2692
Eric Biederman0babc1c2003-05-09 02:39:00 +00002693static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002694{
2695 /* This function is used to determine which triples need
2696 * a register.
2697 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002698 int is_def;
2699 valid_ins(state, ins);
2700 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002701 if (ins->lhs >= 1) {
2702 is_def = 0;
2703 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002704 return is_def;
2705}
2706
Eric Biederman83b991a2003-10-11 06:20:25 +00002707static int triple_is_structural(struct compile_state *state, struct triple *ins)
2708{
2709 int is_structural;
2710 valid_ins(state, ins);
2711 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2712 return is_structural;
2713}
2714
Eric Biederman90089602004-05-28 14:11:54 +00002715static int triple_is_part(struct compile_state *state, struct triple *ins)
2716{
2717 int is_part;
2718 valid_ins(state, ins);
2719 is_part = (table_ops[ins->op].flags & PART) == PART;
2720 return is_part;
2721}
2722
2723static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2724{
2725 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2726}
2727
Eric Biederman0babc1c2003-05-09 02:39:00 +00002728static struct triple **triple_iter(struct compile_state *state,
2729 size_t count, struct triple **vector,
2730 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002731{
2732 struct triple **ret;
2733 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002734 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002735 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002736 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002737 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002738 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002739 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002740 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002741 }
2742 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002743
Eric Biedermanb138ac82003-04-22 18:44:01 +00002744}
2745
2746static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002747 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002748{
Eric Biederman90089602004-05-28 14:11:54 +00002749 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002750 ins, last);
2751}
2752
2753static struct triple **triple_rhs(struct compile_state *state,
2754 struct triple *ins, struct triple **last)
2755{
Eric Biederman90089602004-05-28 14:11:54 +00002756 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002757 ins, last);
2758}
2759
2760static struct triple **triple_misc(struct compile_state *state,
2761 struct triple *ins, struct triple **last)
2762{
Eric Biederman90089602004-05-28 14:11:54 +00002763 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002764 ins, last);
2765}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002766
Eric Biederman90089602004-05-28 14:11:54 +00002767static struct triple **do_triple_targ(struct compile_state *state,
2768 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002769{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002770 size_t count;
2771 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002772 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002773 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002774 count = ins->targ;
2775 next_is_targ = 0;
2776 if (triple_is_cbranch(state, ins)) {
2777 next_is_targ = 1;
2778 }
2779 if (!call_edges && triple_is_call(state, ins)) {
2780 count = 0;
2781 }
2782 if (next_edges && triple_is_call(state, ins)) {
2783 next_is_targ = 1;
2784 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002785 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002786 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002787 if (!last) {
2788 ret = &ins->next;
2789 } else if (last == &ins->next) {
2790 last = 0;
2791 }
2792 }
2793 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002794 if (!last) {
2795 ret = vector;
2796 }
2797 else if ((last >= vector) && (last < (vector + count - 1))) {
2798 ret = last + 1;
2799 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002800 else if (last == vector + count - 1) {
2801 last = 0;
2802 }
2803 }
Eric Biederman90089602004-05-28 14:11:54 +00002804 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002805 struct triple_set *use;
2806 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002807 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002808 continue;
2809 }
2810 if (!last) {
2811 ret = &use->member->next;
2812 break;
2813 }
2814 else if (last == &use->member->next) {
2815 last = 0;
2816 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002817 }
2818 }
2819 return ret;
2820}
2821
Eric Biederman90089602004-05-28 14:11:54 +00002822static struct triple **triple_targ(struct compile_state *state,
2823 struct triple *ins, struct triple **last)
2824{
2825 return do_triple_targ(state, ins, last, 1, 1);
2826}
2827
2828static struct triple **triple_edge_targ(struct compile_state *state,
2829 struct triple *ins, struct triple **last)
2830{
2831 return do_triple_targ(state, ins, last,
2832 state->functions_joined, !state->functions_joined);
2833}
2834
2835static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2836{
2837 struct triple *next;
2838 int lhs, i;
2839 lhs = ins->lhs;
2840 next = ins->next;
2841 for(i = 0; i < lhs; i++) {
2842 struct triple *piece;
2843 piece = LHS(ins, i);
2844 if (next != piece) {
2845 internal_error(state, ins, "malformed lhs on %s",
2846 tops(ins->op));
2847 }
2848 if (next->op != OP_PIECE) {
2849 internal_error(state, ins, "bad lhs op %s at %d on %s",
2850 tops(next->op), i, tops(ins->op));
2851 }
2852 if (next->u.cval != i) {
2853 internal_error(state, ins, "bad u.cval of %d %d expected",
2854 next->u.cval, i);
2855 }
2856 next = next->next;
2857 }
2858 return next;
2859}
2860
2861/* Function piece accessor functions */
2862static struct triple *do_farg(struct compile_state *state,
2863 struct triple *func, unsigned index)
2864{
2865 struct type *ftype;
2866 struct triple *first, *arg;
2867 unsigned i;
2868
2869 ftype = func->type;
2870 if((index < 0) || (index >= (ftype->elements + 2))) {
2871 internal_error(state, func, "bad argument index: %d", index);
2872 }
2873 first = RHS(func, 0);
2874 arg = first->next;
2875 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2876 /* do nothing */
2877 }
2878 if (arg->op != OP_ADECL) {
2879 internal_error(state, 0, "arg not adecl?");
2880 }
2881 return arg;
2882}
2883static struct triple *fresult(struct compile_state *state, struct triple *func)
2884{
2885 return do_farg(state, func, 0);
2886}
2887static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2888{
2889 return do_farg(state, func, 1);
2890}
2891static struct triple *farg(struct compile_state *state,
2892 struct triple *func, unsigned index)
2893{
2894 return do_farg(state, func, index + 2);
2895}
2896
2897
2898static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2899{
2900 struct triple *first, *ins;
2901 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2902 first = ins = RHS(func, 0);
2903 do {
2904 if (triple_is_label(state, ins) && ins->use) {
2905 fprintf(fp, "%p:\n", ins);
2906 }
2907 display_triple(fp, ins);
2908
2909 if (triple_is_branch(state, ins)) {
2910 fprintf(fp, "\n");
2911 }
2912 if (ins->next->prev != ins) {
2913 internal_error(state, ins->next, "bad prev");
2914 }
2915 ins = ins->next;
2916 } while(ins != first);
2917}
2918
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002919static void verify_use(struct compile_state *state,
2920 struct triple *user, struct triple *used)
2921{
2922 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002923 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002924 for(i = 0; i < size; i++) {
2925 if (user->param[i] == used) {
2926 break;
2927 }
2928 }
2929 if (triple_is_branch(state, user)) {
2930 if (user->next == used) {
2931 i = -1;
2932 }
2933 }
2934 if (i == size) {
2935 internal_error(state, user, "%s(%p) does not use %s(%p)",
2936 tops(user->op), user, tops(used->op), used);
2937 }
2938}
2939
2940static int find_rhs_use(struct compile_state *state,
2941 struct triple *user, struct triple *used)
2942{
2943 struct triple **param;
2944 int size, i;
2945 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002946
2947#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002948#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002949#endif
Eric Biederman90089602004-05-28 14:11:54 +00002950 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002951 param = &RHS(user, 0);
2952 for(i = 0; i < size; i++) {
2953 if (param[i] == used) {
2954 return i;
2955 }
2956 }
2957 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002958}
2959
2960static void free_triple(struct compile_state *state, struct triple *ptr)
2961{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002962 size_t size;
2963 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002964 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002965 ptr->prev->next = ptr->next;
2966 ptr->next->prev = ptr->prev;
2967 if (ptr->use) {
2968 internal_error(state, ptr, "ptr->use != 0");
2969 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002970 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002971 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002972 xfree(ptr);
2973}
2974
2975static void release_triple(struct compile_state *state, struct triple *ptr)
2976{
2977 struct triple_set *set, *next;
2978 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002979 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002980 if (ptr == &unknown_triple) {
2981 return;
2982 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002983 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002984 /* Make certain the we are not the first or last element of a block */
2985 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002986 if (block) {
2987 if ((block->last == ptr) && (block->first == ptr)) {
2988 block->last = block->first = 0;
2989 }
2990 else if (block->last == ptr) {
2991 block->last = ptr->prev;
2992 }
2993 else if (block->first == ptr) {
2994 block->first = ptr->next;
2995 }
Eric Biederman66fe2222003-07-04 15:14:04 +00002996 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002997 /* Remove ptr from use chains where it is the user */
2998 expr = triple_rhs(state, ptr, 0);
2999 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3000 if (*expr) {
3001 unuse_triple(*expr, ptr);
3002 }
3003 }
3004 expr = triple_lhs(state, ptr, 0);
3005 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3006 if (*expr) {
3007 unuse_triple(*expr, ptr);
3008 }
3009 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003010 expr = triple_misc(state, ptr, 0);
3011 for(; expr; expr = triple_misc(state, ptr, expr)) {
3012 if (*expr) {
3013 unuse_triple(*expr, ptr);
3014 }
3015 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003016 expr = triple_targ(state, ptr, 0);
3017 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003018 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003019 unuse_triple(*expr, ptr);
3020 }
3021 }
3022 /* Reomve ptr from use chains where it is used */
3023 for(set = ptr->use; set; set = next) {
3024 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003025 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003026 expr = triple_rhs(state, set->member, 0);
3027 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3028 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003029 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003030 }
3031 }
3032 expr = triple_lhs(state, set->member, 0);
3033 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3034 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003035 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003036 }
3037 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003038 expr = triple_misc(state, set->member, 0);
3039 for(; expr; expr = triple_misc(state, set->member, expr)) {
3040 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003041 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003042 }
3043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003044 expr = triple_targ(state, set->member, 0);
3045 for(; expr; expr = triple_targ(state, set->member, expr)) {
3046 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003047 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003048 }
3049 }
3050 unuse_triple(ptr, set->member);
3051 }
3052 free_triple(state, ptr);
3053}
3054
Eric Biederman5ade04a2003-10-22 04:03:46 +00003055static void print_triples(struct compile_state *state);
3056static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003057
Jason Schildt27b85112005-08-10 14:31:52 +00003058#define TOK_UNKNOWN 0
3059#define TOK_SPACE 1
3060#define TOK_SEMI 2
3061#define TOK_LBRACE 3
3062#define TOK_RBRACE 4
3063#define TOK_COMMA 5
3064#define TOK_EQ 6
3065#define TOK_COLON 7
3066#define TOK_LBRACKET 8
3067#define TOK_RBRACKET 9
3068#define TOK_LPAREN 10
3069#define TOK_RPAREN 11
3070#define TOK_STAR 12
3071#define TOK_DOTS 13
3072#define TOK_MORE 14
3073#define TOK_LESS 15
3074#define TOK_TIMESEQ 16
3075#define TOK_DIVEQ 17
3076#define TOK_MODEQ 18
3077#define TOK_PLUSEQ 19
3078#define TOK_MINUSEQ 20
3079#define TOK_SLEQ 21
3080#define TOK_SREQ 22
3081#define TOK_ANDEQ 23
3082#define TOK_XOREQ 24
3083#define TOK_OREQ 25
3084#define TOK_EQEQ 26
3085#define TOK_NOTEQ 27
3086#define TOK_QUEST 28
3087#define TOK_LOGOR 29
3088#define TOK_LOGAND 30
3089#define TOK_OR 31
3090#define TOK_AND 32
3091#define TOK_XOR 33
3092#define TOK_LESSEQ 34
3093#define TOK_MOREEQ 35
3094#define TOK_SL 36
3095#define TOK_SR 37
3096#define TOK_PLUS 38
3097#define TOK_MINUS 39
3098#define TOK_DIV 40
3099#define TOK_MOD 41
3100#define TOK_PLUSPLUS 42
3101#define TOK_MINUSMINUS 43
3102#define TOK_BANG 44
3103#define TOK_ARROW 45
3104#define TOK_DOT 46
3105#define TOK_TILDE 47
3106#define TOK_LIT_STRING 48
3107#define TOK_LIT_CHAR 49
3108#define TOK_LIT_INT 50
3109#define TOK_LIT_FLOAT 51
3110#define TOK_MACRO 52
3111#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003112
Jason Schildt27b85112005-08-10 14:31:52 +00003113#define TOK_IDENT 54
3114#define TOK_STRUCT_NAME 55
3115#define TOK_ENUM_CONST 56
3116#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003117
Jason Schildt27b85112005-08-10 14:31:52 +00003118#define TOK_AUTO 58
3119#define TOK_BREAK 59
3120#define TOK_CASE 60
3121#define TOK_CHAR 61
3122#define TOK_CONST 62
3123#define TOK_CONTINUE 63
3124#define TOK_DEFAULT 64
3125#define TOK_DO 65
3126#define TOK_DOUBLE 66
3127#define TOK_ELSE 67
3128#define TOK_ENUM 68
3129#define TOK_EXTERN 69
3130#define TOK_FLOAT 70
3131#define TOK_FOR 71
3132#define TOK_GOTO 72
3133#define TOK_IF 73
3134#define TOK_INLINE 74
3135#define TOK_INT 75
3136#define TOK_LONG 76
3137#define TOK_REGISTER 77
3138#define TOK_RESTRICT 78
3139#define TOK_RETURN 79
3140#define TOK_SHORT 80
3141#define TOK_SIGNED 81
3142#define TOK_SIZEOF 82
3143#define TOK_STATIC 83
3144#define TOK_STRUCT 84
3145#define TOK_SWITCH 85
3146#define TOK_TYPEDEF 86
3147#define TOK_UNION 87
3148#define TOK_UNSIGNED 88
3149#define TOK_VOID 89
3150#define TOK_VOLATILE 90
3151#define TOK_WHILE 91
3152#define TOK_ASM 92
3153#define TOK_ATTRIBUTE 93
3154#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003155#define TOK_FIRST_KEYWORD TOK_AUTO
3156#define TOK_LAST_KEYWORD TOK_ALIGNOF
3157
Eric Biedermancb364952004-11-15 10:46:44 +00003158#define TOK_MDEFINE 100
3159#define TOK_MDEFINED 101
3160#define TOK_MUNDEF 102
3161#define TOK_MINCLUDE 103
3162#define TOK_MLINE 104
3163#define TOK_MERROR 105
3164#define TOK_MWARNING 106
3165#define TOK_MPRAGMA 107
3166#define TOK_MIFDEF 108
3167#define TOK_MIFNDEF 109
3168#define TOK_MELIF 110
3169#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003170
Eric Biedermancb364952004-11-15 10:46:44 +00003171#define TOK_FIRST_MACRO TOK_MDEFINE
3172#define TOK_LAST_MACRO TOK_MENDIF
Eric Biedermanb138ac82003-04-22 18:44:01 +00003173
Eric Biedermancb364952004-11-15 10:46:44 +00003174#define TOK_MIF 112
3175#define TOK_MELSE 113
3176#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003177
Eric Biedermancb364952004-11-15 10:46:44 +00003178#define TOK_EOL 115
3179#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003180
3181static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003182[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003183[TOK_SPACE ] = ":space:",
3184[TOK_SEMI ] = ";",
3185[TOK_LBRACE ] = "{",
3186[TOK_RBRACE ] = "}",
3187[TOK_COMMA ] = ",",
3188[TOK_EQ ] = "=",
3189[TOK_COLON ] = ":",
3190[TOK_LBRACKET ] = "[",
3191[TOK_RBRACKET ] = "]",
3192[TOK_LPAREN ] = "(",
3193[TOK_RPAREN ] = ")",
3194[TOK_STAR ] = "*",
3195[TOK_DOTS ] = "...",
3196[TOK_MORE ] = ">",
3197[TOK_LESS ] = "<",
3198[TOK_TIMESEQ ] = "*=",
3199[TOK_DIVEQ ] = "/=",
3200[TOK_MODEQ ] = "%=",
3201[TOK_PLUSEQ ] = "+=",
3202[TOK_MINUSEQ ] = "-=",
3203[TOK_SLEQ ] = "<<=",
3204[TOK_SREQ ] = ">>=",
3205[TOK_ANDEQ ] = "&=",
3206[TOK_XOREQ ] = "^=",
3207[TOK_OREQ ] = "|=",
3208[TOK_EQEQ ] = "==",
3209[TOK_NOTEQ ] = "!=",
3210[TOK_QUEST ] = "?",
3211[TOK_LOGOR ] = "||",
3212[TOK_LOGAND ] = "&&",
3213[TOK_OR ] = "|",
3214[TOK_AND ] = "&",
3215[TOK_XOR ] = "^",
3216[TOK_LESSEQ ] = "<=",
3217[TOK_MOREEQ ] = ">=",
3218[TOK_SL ] = "<<",
3219[TOK_SR ] = ">>",
3220[TOK_PLUS ] = "+",
3221[TOK_MINUS ] = "-",
3222[TOK_DIV ] = "/",
3223[TOK_MOD ] = "%",
3224[TOK_PLUSPLUS ] = "++",
3225[TOK_MINUSMINUS ] = "--",
3226[TOK_BANG ] = "!",
3227[TOK_ARROW ] = "->",
3228[TOK_DOT ] = ".",
3229[TOK_TILDE ] = "~",
3230[TOK_LIT_STRING ] = ":string:",
3231[TOK_IDENT ] = ":ident:",
3232[TOK_TYPE_NAME ] = ":typename:",
3233[TOK_LIT_CHAR ] = ":char:",
3234[TOK_LIT_INT ] = ":integer:",
3235[TOK_LIT_FLOAT ] = ":float:",
3236[TOK_MACRO ] = "#",
3237[TOK_CONCATENATE ] = "##",
3238
3239[TOK_AUTO ] = "auto",
3240[TOK_BREAK ] = "break",
3241[TOK_CASE ] = "case",
3242[TOK_CHAR ] = "char",
3243[TOK_CONST ] = "const",
3244[TOK_CONTINUE ] = "continue",
3245[TOK_DEFAULT ] = "default",
3246[TOK_DO ] = "do",
3247[TOK_DOUBLE ] = "double",
3248[TOK_ELSE ] = "else",
3249[TOK_ENUM ] = "enum",
3250[TOK_EXTERN ] = "extern",
3251[TOK_FLOAT ] = "float",
3252[TOK_FOR ] = "for",
3253[TOK_GOTO ] = "goto",
3254[TOK_IF ] = "if",
3255[TOK_INLINE ] = "inline",
3256[TOK_INT ] = "int",
3257[TOK_LONG ] = "long",
3258[TOK_REGISTER ] = "register",
3259[TOK_RESTRICT ] = "restrict",
3260[TOK_RETURN ] = "return",
3261[TOK_SHORT ] = "short",
3262[TOK_SIGNED ] = "signed",
3263[TOK_SIZEOF ] = "sizeof",
3264[TOK_STATIC ] = "static",
3265[TOK_STRUCT ] = "struct",
3266[TOK_SWITCH ] = "switch",
3267[TOK_TYPEDEF ] = "typedef",
3268[TOK_UNION ] = "union",
3269[TOK_UNSIGNED ] = "unsigned",
3270[TOK_VOID ] = "void",
3271[TOK_VOLATILE ] = "volatile",
3272[TOK_WHILE ] = "while",
3273[TOK_ASM ] = "asm",
3274[TOK_ATTRIBUTE ] = "__attribute__",
3275[TOK_ALIGNOF ] = "__alignof__",
3276
Eric Biederman41203d92004-11-08 09:31:09 +00003277[TOK_MDEFINE ] = "#define",
3278[TOK_MDEFINED ] = "#defined",
3279[TOK_MUNDEF ] = "#undef",
3280[TOK_MINCLUDE ] = "#include",
3281[TOK_MLINE ] = "#line",
3282[TOK_MERROR ] = "#error",
3283[TOK_MWARNING ] = "#warning",
3284[TOK_MPRAGMA ] = "#pragma",
3285[TOK_MIFDEF ] = "#ifdef",
3286[TOK_MIFNDEF ] = "#ifndef",
3287[TOK_MELIF ] = "#elif",
3288[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003289
Eric Biederman41203d92004-11-08 09:31:09 +00003290[TOK_MIF ] = "#if",
3291[TOK_MELSE ] = "#else",
3292[TOK_MIDENT ] = "#:ident:",
3293[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003294[TOK_EOF ] = "EOF",
3295};
3296
3297static unsigned int hash(const char *str, int str_len)
3298{
3299 unsigned int hash;
3300 const char *end;
3301 end = str + str_len;
3302 hash = 0;
3303 for(; str < end; str++) {
3304 hash = (hash *263) + *str;
3305 }
3306 hash = hash & (HASH_TABLE_SIZE -1);
3307 return hash;
3308}
3309
3310static struct hash_entry *lookup(
3311 struct compile_state *state, const char *name, int name_len)
3312{
3313 struct hash_entry *entry;
3314 unsigned int index;
3315 index = hash(name, name_len);
3316 entry = state->hash_table[index];
3317 while(entry &&
3318 ((entry->name_len != name_len) ||
3319 (memcmp(entry->name, name, name_len) != 0))) {
3320 entry = entry->next;
3321 }
3322 if (!entry) {
3323 char *new_name;
3324 /* Get a private copy of the name */
3325 new_name = xmalloc(name_len + 1, "hash_name");
3326 memcpy(new_name, name, name_len);
3327 new_name[name_len] = '\0';
3328
3329 /* Create a new hash entry */
3330 entry = xcmalloc(sizeof(*entry), "hash_entry");
3331 entry->next = state->hash_table[index];
3332 entry->name = new_name;
3333 entry->name_len = name_len;
3334
3335 /* Place the new entry in the hash table */
3336 state->hash_table[index] = entry;
3337 }
3338 return entry;
3339}
3340
3341static void ident_to_keyword(struct compile_state *state, struct token *tk)
3342{
3343 struct hash_entry *entry;
3344 entry = tk->ident;
3345 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3346 (entry->tok == TOK_ENUM_CONST) ||
3347 ((entry->tok >= TOK_FIRST_KEYWORD) &&
3348 (entry->tok <= TOK_LAST_KEYWORD)))) {
3349 tk->tok = entry->tok;
3350 }
3351}
3352
3353static void ident_to_macro(struct compile_state *state, struct token *tk)
3354{
3355 struct hash_entry *entry;
3356 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003357 if (!entry)
3358 return;
3359 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003360 tk->tok = entry->tok;
3361 }
Eric Biederman41203d92004-11-08 09:31:09 +00003362 else if (entry->tok == TOK_IF) {
3363 tk->tok = TOK_MIF;
3364 }
3365 else if (entry->tok == TOK_ELSE) {
3366 tk->tok = TOK_MELSE;
3367 }
3368 else {
3369 tk->tok = TOK_MIDENT;
3370 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003371}
3372
3373static void hash_keyword(
3374 struct compile_state *state, const char *keyword, int tok)
3375{
3376 struct hash_entry *entry;
3377 entry = lookup(state, keyword, strlen(keyword));
3378 if (entry && entry->tok != TOK_UNKNOWN) {
3379 die("keyword %s already hashed", keyword);
3380 }
3381 entry->tok = tok;
3382}
3383
Eric Biederman90089602004-05-28 14:11:54 +00003384static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003385 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003386 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003387{
3388 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003389 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003390 error(state, 0, "%s already defined", ident->name);
3391 }
3392 sym = xcmalloc(sizeof(*sym), "symbol");
3393 sym->ident = ident;
3394 sym->def = def;
3395 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003396 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003397 sym->next = *chain;
3398 *chain = sym;
3399}
3400
Eric Biederman90089602004-05-28 14:11:54 +00003401static void symbol(
3402 struct compile_state *state, struct hash_entry *ident,
3403 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003404{
Eric Biederman90089602004-05-28 14:11:54 +00003405 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3406}
3407
3408static void var_symbol(struct compile_state *state,
3409 struct hash_entry *ident, struct triple *def)
3410{
3411 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3412 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003413 }
Eric Biederman90089602004-05-28 14:11:54 +00003414 symbol(state, ident, &ident->sym_ident, def, def->type);
3415}
3416
3417static void label_symbol(struct compile_state *state,
3418 struct hash_entry *ident, struct triple *label, int depth)
3419{
3420 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003421}
3422
Eric Biedermanb138ac82003-04-22 18:44:01 +00003423static void start_scope(struct compile_state *state)
3424{
3425 state->scope_depth++;
3426}
3427
Eric Biederman90089602004-05-28 14:11:54 +00003428static void end_scope_syms(struct compile_state *state,
3429 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003430{
3431 struct symbol *sym, *next;
3432 sym = *chain;
3433 while(sym && (sym->scope_depth == depth)) {
3434 next = sym->next;
3435 xfree(sym);
3436 sym = next;
3437 }
3438 *chain = sym;
3439}
3440
3441static void end_scope(struct compile_state *state)
3442{
3443 int i;
3444 int depth;
3445 /* Walk through the hash table and remove all symbols
3446 * in the current scope.
3447 */
3448 depth = state->scope_depth;
3449 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3450 struct hash_entry *entry;
3451 entry = state->hash_table[i];
3452 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003453 end_scope_syms(state, &entry->sym_label, depth);
3454 end_scope_syms(state, &entry->sym_tag, depth);
3455 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003456 entry = entry->next;
3457 }
3458 }
3459 state->scope_depth = depth - 1;
3460}
3461
3462static void register_keywords(struct compile_state *state)
3463{
3464 hash_keyword(state, "auto", TOK_AUTO);
3465 hash_keyword(state, "break", TOK_BREAK);
3466 hash_keyword(state, "case", TOK_CASE);
3467 hash_keyword(state, "char", TOK_CHAR);
3468 hash_keyword(state, "const", TOK_CONST);
3469 hash_keyword(state, "continue", TOK_CONTINUE);
3470 hash_keyword(state, "default", TOK_DEFAULT);
3471 hash_keyword(state, "do", TOK_DO);
3472 hash_keyword(state, "double", TOK_DOUBLE);
3473 hash_keyword(state, "else", TOK_ELSE);
3474 hash_keyword(state, "enum", TOK_ENUM);
3475 hash_keyword(state, "extern", TOK_EXTERN);
3476 hash_keyword(state, "float", TOK_FLOAT);
3477 hash_keyword(state, "for", TOK_FOR);
3478 hash_keyword(state, "goto", TOK_GOTO);
3479 hash_keyword(state, "if", TOK_IF);
3480 hash_keyword(state, "inline", TOK_INLINE);
3481 hash_keyword(state, "int", TOK_INT);
3482 hash_keyword(state, "long", TOK_LONG);
3483 hash_keyword(state, "register", TOK_REGISTER);
3484 hash_keyword(state, "restrict", TOK_RESTRICT);
3485 hash_keyword(state, "return", TOK_RETURN);
3486 hash_keyword(state, "short", TOK_SHORT);
3487 hash_keyword(state, "signed", TOK_SIGNED);
3488 hash_keyword(state, "sizeof", TOK_SIZEOF);
3489 hash_keyword(state, "static", TOK_STATIC);
3490 hash_keyword(state, "struct", TOK_STRUCT);
3491 hash_keyword(state, "switch", TOK_SWITCH);
3492 hash_keyword(state, "typedef", TOK_TYPEDEF);
3493 hash_keyword(state, "union", TOK_UNION);
3494 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3495 hash_keyword(state, "void", TOK_VOID);
3496 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003497 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003498 hash_keyword(state, "while", TOK_WHILE);
3499 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003500 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003501 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3502 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3503}
3504
3505static void register_macro_keywords(struct compile_state *state)
3506{
Eric Biederman41203d92004-11-08 09:31:09 +00003507 hash_keyword(state, "define", TOK_MDEFINE);
3508 hash_keyword(state, "defined", TOK_MDEFINED);
3509 hash_keyword(state, "undef", TOK_MUNDEF);
3510 hash_keyword(state, "include", TOK_MINCLUDE);
3511 hash_keyword(state, "line", TOK_MLINE);
3512 hash_keyword(state, "error", TOK_MERROR);
3513 hash_keyword(state, "warning", TOK_MWARNING);
3514 hash_keyword(state, "pragma", TOK_MPRAGMA);
3515 hash_keyword(state, "ifdef", TOK_MIFDEF);
3516 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3517 hash_keyword(state, "elif", TOK_MELIF);
3518 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003519}
3520
Eric Biederman90089602004-05-28 14:11:54 +00003521
3522static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3523{
3524 if (ident->sym_define != 0) {
3525 struct macro *macro;
3526 struct macro_arg *arg, *anext;
3527 macro = ident->sym_define;
3528 ident->sym_define = 0;
3529
3530 /* Free the macro arguments... */
3531 anext = macro->args;
3532 while(anext) {
3533 arg = anext;
3534 anext = arg->next;
3535 xfree(arg);
3536 }
3537
3538 /* Free the macro buffer */
3539 xfree(macro->buf);
3540
3541 /* Now free the macro itself */
3542 xfree(macro);
3543 }
3544}
3545
Eric Biedermancb364952004-11-15 10:46:44 +00003546static void do_define_macro(struct compile_state *state,
3547 struct hash_entry *ident, const char *body,
3548 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003549{
3550 struct macro *macro;
3551 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003552 size_t body_len;
3553
3554 /* Find the length of the body */
3555 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003556 macro = ident->sym_define;
3557 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003558 int identical_bodies, identical_args;
3559 struct macro_arg *oarg;
3560 /* Explicitly allow identical redfinitions of the same macro */
3561 identical_bodies =
3562 (macro->buf_len == body_len) &&
3563 (memcmp(macro->buf, body, body_len) == 0);
3564 identical_args = macro->argc == argc;
3565 oarg = macro->args;
3566 arg = args;
3567 while(identical_args && arg) {
3568 identical_args = oarg->ident == arg->ident;
3569 arg = arg->next;
3570 oarg = oarg->next;
3571 }
3572 if (identical_bodies && identical_args) {
3573 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003574 return;
3575 }
3576 error(state, 0, "macro %s already defined\n", ident->name);
3577 }
3578#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003579 fprintf(state->errout, "#define %s: `%*.*s'\n",
3580 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003581#endif
3582 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003583 macro->ident = ident;
3584 macro->buf = body;
3585 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003586 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003587 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003588
3589 ident->sym_define = macro;
3590}
Eric Biedermancb364952004-11-15 10:46:44 +00003591
3592static void define_macro(
3593 struct compile_state *state,
3594 struct hash_entry *ident,
3595 const char *body, int body_len,
3596 int argc, struct macro_arg *args)
3597{
3598 char *buf;
3599 buf = xmalloc(body_len + 1, "macro buf");
3600 memcpy(buf, body, body_len);
3601 buf[body_len] = '\0';
3602 do_define_macro(state, ident, buf, argc, args);
3603}
Eric Biederman90089602004-05-28 14:11:54 +00003604
3605static void register_builtin_macro(struct compile_state *state,
3606 const char *name, const char *value)
3607{
3608 struct hash_entry *ident;
3609
3610 if (value[0] == '(') {
3611 internal_error(state, 0, "Builtin macros with arguments not supported");
3612 }
3613 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003614 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003615}
3616
3617static void register_builtin_macros(struct compile_state *state)
3618{
3619 char buf[30];
3620 char scratch[30];
3621 time_t now;
3622 struct tm *tm;
3623 now = time(NULL);
3624 tm = localtime(&now);
3625
3626 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3627 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3628 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3629 register_builtin_macro(state, "__LINE__", "54321");
3630
3631 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3632 sprintf(buf, "\"%s\"", scratch);
3633 register_builtin_macro(state, "__DATE__", buf);
3634
3635 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3636 sprintf(buf, "\"%s\"", scratch);
3637 register_builtin_macro(state, "__TIME__", buf);
3638
3639 /* I can't be a conforming implementation of C :( */
3640 register_builtin_macro(state, "__STDC__", "0");
3641 /* In particular I don't conform to C99 */
3642 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3643
3644}
3645
3646static void process_cmdline_macros(struct compile_state *state)
3647{
3648 const char **macro, *name;
3649 struct hash_entry *ident;
3650 for(macro = state->compiler->defines; (name = *macro); macro++) {
3651 const char *body;
3652 size_t name_len;
3653
3654 name_len = strlen(name);
3655 body = strchr(name, '=');
3656 if (!body) {
3657 body = "\0";
3658 } else {
3659 name_len = body - name;
3660 body++;
3661 }
3662 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003663 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003664 }
3665 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3666 ident = lookup(state, name, strlen(name));
3667 undef_macro(state, ident);
3668 }
3669}
3670
Eric Biedermanb138ac82003-04-22 18:44:01 +00003671static int spacep(int c)
3672{
3673 int ret = 0;
3674 switch(c) {
3675 case ' ':
3676 case '\t':
3677 case '\f':
3678 case '\v':
3679 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003680 ret = 1;
3681 break;
3682 }
3683 return ret;
3684}
3685
Eric Biedermanb138ac82003-04-22 18:44:01 +00003686static int digitp(int c)
3687{
3688 int ret = 0;
3689 switch(c) {
3690 case '0': case '1': case '2': case '3': case '4':
3691 case '5': case '6': case '7': case '8': case '9':
3692 ret = 1;
3693 break;
3694 }
3695 return ret;
3696}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003697static int digval(int c)
3698{
3699 int val = -1;
3700 if ((c >= '0') && (c <= '9')) {
3701 val = c - '0';
3702 }
3703 return val;
3704}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003705
3706static int hexdigitp(int c)
3707{
3708 int ret = 0;
3709 switch(c) {
3710 case '0': case '1': case '2': case '3': case '4':
3711 case '5': case '6': case '7': case '8': case '9':
3712 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3713 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3714 ret = 1;
3715 break;
3716 }
3717 return ret;
3718}
3719static int hexdigval(int c)
3720{
3721 int val = -1;
3722 if ((c >= '0') && (c <= '9')) {
3723 val = c - '0';
3724 }
3725 else if ((c >= 'A') && (c <= 'F')) {
3726 val = 10 + (c - 'A');
3727 }
3728 else if ((c >= 'a') && (c <= 'f')) {
3729 val = 10 + (c - 'a');
3730 }
3731 return val;
3732}
3733
3734static int octdigitp(int c)
3735{
3736 int ret = 0;
3737 switch(c) {
3738 case '0': case '1': case '2': case '3':
3739 case '4': case '5': case '6': case '7':
3740 ret = 1;
3741 break;
3742 }
3743 return ret;
3744}
3745static int octdigval(int c)
3746{
3747 int val = -1;
3748 if ((c >= '0') && (c <= '7')) {
3749 val = c - '0';
3750 }
3751 return val;
3752}
3753
3754static int letterp(int c)
3755{
3756 int ret = 0;
3757 switch(c) {
3758 case 'a': case 'b': case 'c': case 'd': case 'e':
3759 case 'f': case 'g': case 'h': case 'i': case 'j':
3760 case 'k': case 'l': case 'm': case 'n': case 'o':
3761 case 'p': case 'q': case 'r': case 's': case 't':
3762 case 'u': case 'v': case 'w': case 'x': case 'y':
3763 case 'z':
3764 case 'A': case 'B': case 'C': case 'D': case 'E':
3765 case 'F': case 'G': case 'H': case 'I': case 'J':
3766 case 'K': case 'L': case 'M': case 'N': case 'O':
3767 case 'P': case 'Q': case 'R': case 'S': case 'T':
3768 case 'U': case 'V': case 'W': case 'X': case 'Y':
3769 case 'Z':
3770 case '_':
3771 ret = 1;
3772 break;
3773 }
3774 return ret;
3775}
3776
Eric Biederman90089602004-05-28 14:11:54 +00003777static const char *identifier(const char *str, const char *end)
3778{
3779 if (letterp(*str)) {
3780 for(; str < end; str++) {
3781 int c;
3782 c = *str;
3783 if (!letterp(c) && !digitp(c)) {
3784 break;
3785 }
3786 }
3787 }
3788 return str;
3789}
3790
Eric Biedermanb138ac82003-04-22 18:44:01 +00003791static int char_value(struct compile_state *state,
3792 const signed char **strp, const signed char *end)
3793{
3794 const signed char *str;
3795 int c;
3796 str = *strp;
3797 c = *str++;
3798 if ((c == '\\') && (str < end)) {
3799 switch(*str) {
3800 case 'n': c = '\n'; str++; break;
3801 case 't': c = '\t'; str++; break;
3802 case 'v': c = '\v'; str++; break;
3803 case 'b': c = '\b'; str++; break;
3804 case 'r': c = '\r'; str++; break;
3805 case 'f': c = '\f'; str++; break;
3806 case 'a': c = '\a'; str++; break;
3807 case '\\': c = '\\'; str++; break;
3808 case '?': c = '?'; str++; break;
3809 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003810 case '"': c = '"'; str++; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003811 case 'x':
3812 c = 0;
3813 str++;
3814 while((str < end) && hexdigitp(*str)) {
3815 c <<= 4;
3816 c += hexdigval(*str);
3817 str++;
3818 }
3819 break;
3820 case '0': case '1': case '2': case '3':
3821 case '4': case '5': case '6': case '7':
3822 c = 0;
3823 while((str < end) && octdigitp(*str)) {
3824 c <<= 3;
3825 c += octdigval(*str);
3826 str++;
3827 }
3828 break;
3829 default:
3830 error(state, 0, "Invalid character constant");
3831 break;
3832 }
3833 }
3834 *strp = str;
3835 return c;
3836}
3837
Eric Biedermancb364952004-11-15 10:46:44 +00003838static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003839{
Eric Biedermancb364952004-11-15 10:46:44 +00003840 const char *end = file->buf + file->size;
3841 while(pos < end) {
3842 /* Lookup the character */
3843 int size = 1;
3844 int c = *pos;
3845 /* Is this a trigraph? */
3846 if (file->trigraphs &&
3847 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3848 {
3849 switch(pos[2]) {
3850 case '=': c = '#'; break;
3851 case '/': c = '\\'; break;
3852 case '\'': c = '^'; break;
3853 case '(': c = '['; break;
3854 case ')': c = ']'; break;
3855 case '!': c = '!'; break;
3856 case '<': c = '{'; break;
3857 case '>': c = '}'; break;
3858 case '-': c = '~'; break;
3859 }
3860 if (c != '?') {
3861 size = 3;
3862 }
3863 }
3864 /* Is this an escaped newline? */
3865 if (file->join_lines &&
3866 (c == '\\') && (pos + size < end) && (pos[1] == '\n'))
3867 {
3868 /* At the start of a line just eat it */
3869 if (pos == file->pos) {
3870 file->line++;
3871 file->report_line++;
3872 file->line_start = pos + size + 1;
3873 }
3874 pos += size + 1;
3875 }
3876 /* Do I need to ga any farther? */
3877 else if (index == 0) {
3878 break;
3879 }
3880 /* Process a normal character */
3881 else {
3882 pos += size;
3883 index -= 1;
3884 }
3885 }
3886 return pos;
3887}
3888
3889static int get_char(struct file_state *file, const char *pos)
3890{
3891 const char *end = file->buf + file->size;
3892 int c;
3893 c = -1;
3894 pos = next_char(file, pos, 0);
3895 if (pos < end) {
3896 /* Lookup the character */
3897 c = *pos;
3898 /* If it is a trigraph get the trigraph value */
3899 if (file->trigraphs &&
3900 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3901 {
3902 switch(pos[2]) {
3903 case '=': c = '#'; break;
3904 case '/': c = '\\'; break;
3905 case '\'': c = '^'; break;
3906 case '(': c = '['; break;
3907 case ')': c = ']'; break;
3908 case '!': c = '!'; break;
3909 case '<': c = '{'; break;
3910 case '>': c = '}'; break;
3911 case '-': c = '~'; break;
3912 }
3913 }
3914 }
3915 return c;
3916}
3917
3918static void eat_chars(struct file_state *file, const char *targ)
3919{
3920 const char *pos = file->pos;
3921 while(pos < targ) {
3922 /* Do we have a newline? */
3923 if (pos[0] == '\n') {
3924 file->line++;
3925 file->report_line++;
3926 file->line_start = pos + 1;
3927 }
3928 pos++;
3929 }
3930 file->pos = pos;
3931}
3932
3933
3934static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3935{
3936 size_t len;
3937 len = 0;
3938 while(src < end) {
3939 src = next_char(file, src, 1);
3940 len++;
3941 }
3942 return len;
3943}
3944
3945static void char_strcpy(char *dest,
3946 struct file_state *file, const char *src, const char *end)
3947{
3948 while(src < end) {
3949 int c;
3950 c = get_char(file, src);
3951 src = next_char(file, src, 1);
3952 *dest++ = c;
3953 }
3954}
3955
3956static char *char_strdup(struct file_state *file,
3957 const char *start, const char *end, const char *id)
3958{
3959 char *str;
3960 size_t str_len;
3961 str_len = char_strlen(file, start, end);
3962 str = xcmalloc(str_len + 1, id);
3963 char_strcpy(str, file, start, end);
3964 str[str_len] = '\0';
3965 return str;
3966}
3967
3968static const char *after_digits(struct file_state *file, const char *ptr)
3969{
3970 while(digitp(get_char(file, ptr))) {
3971 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003972 }
3973 return ptr;
3974}
3975
Eric Biedermancb364952004-11-15 10:46:44 +00003976static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003977{
Eric Biedermancb364952004-11-15 10:46:44 +00003978 while(octdigitp(get_char(file, ptr))) {
3979 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003980 }
3981 return ptr;
3982}
3983
Eric Biedermancb364952004-11-15 10:46:44 +00003984static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003985{
Eric Biedermancb364952004-11-15 10:46:44 +00003986 while(hexdigitp(get_char(file, ptr))) {
3987 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003988 }
3989 return ptr;
3990}
3991
Eric Biedermancb364952004-11-15 10:46:44 +00003992static const char *after_alnums(struct file_state *file, const char *ptr)
3993{
3994 int c;
3995 c = get_char(file, ptr);
3996 while(letterp(c) || digitp(c)) {
3997 ptr = next_char(file, ptr, 1);
3998 c = get_char(file, ptr);
3999 }
4000 return ptr;
4001}
4002
4003static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004004 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004005{
4006 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004007
Eric Biedermanb138ac82003-04-22 18:44:01 +00004008 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004009 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004010
4011 /* Store the copy in the token */
4012 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004013 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004014}
4015
4016static void raw_next_token(struct compile_state *state,
4017 struct file_state *file, struct token *tk)
4018{
4019 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004020 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004021 const char *tokp;
4022 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004023 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004024
Eric Biedermanb138ac82003-04-22 18:44:01 +00004025 tk->str_len = 0;
4026 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004027 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004028 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004029 c = get_char(file, tokp);
4030 tokp = next_char(file, tokp, 1);
4031 eat = 0;
4032 c1 = get_char(file, tokp);
4033 c2 = get_char(file, next_char(file, tokp, 1));
4034 c3 = get_char(file, next_char(file, tokp, 2));
4035
4036 /* The end of the file */
4037 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004038 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004039 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004040 /* Whitespace */
4041 else if (spacep(c)) {
4042 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004043 while (spacep(get_char(file, tokp))) {
4044 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004045 }
4046 }
4047 /* EOL Comments */
4048 else if ((c == '/') && (c1 == '/')) {
4049 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004050 tokp = next_char(file, tokp, 1);
4051 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004052 /* Advance to the next character only after we verify
4053 * the current character is not a newline.
4054 * EOL is special to the preprocessor so we don't
4055 * want to loose any.
4056 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004057 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004058 break;
4059 }
Eric Biederman57183382006-12-02 16:48:48 +00004060 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004061 }
4062 }
4063 /* Comments */
4064 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004065 tokp = next_char(file, tokp, 2);
4066 c = c2;
4067 while((c1 = get_char(file, tokp)) != -1) {
4068 tokp = next_char(file, tokp, 1);
4069 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004070 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004071 break;
4072 }
Eric Biedermancb364952004-11-15 10:46:44 +00004073 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004074 }
4075 if (tok == TOK_UNKNOWN) {
4076 error(state, 0, "unterminated comment");
4077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004078 }
4079 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004080 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
4081 int wchar, multiline;
4082
Eric Biedermanb138ac82003-04-22 18:44:01 +00004083 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004084 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004085 if (c == 'L') {
4086 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004087 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004088 }
Eric Biedermancb364952004-11-15 10:46:44 +00004089 while((c = get_char(file, tokp)) != -1) {
4090 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004091 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004092 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004093 }
Eric Biedermancb364952004-11-15 10:46:44 +00004094 else if (c == '\\') {
4095 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004096 }
4097 else if (c == '"') {
4098 tok = TOK_LIT_STRING;
4099 break;
4100 }
4101 }
4102 if (tok == TOK_UNKNOWN) {
4103 error(state, 0, "unterminated string constant");
4104 }
Eric Biedermancb364952004-11-15 10:46:44 +00004105 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004106 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004108
4109 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004110 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004111 }
4112 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004113 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
4114 int wchar, multiline;
4115
Eric Biedermanb138ac82003-04-22 18:44:01 +00004116 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004117 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004118 if (c == 'L') {
4119 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004120 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004121 }
Eric Biedermancb364952004-11-15 10:46:44 +00004122 while((c = get_char(file, tokp)) != -1) {
4123 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004124 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004125 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004126 }
Eric Biedermancb364952004-11-15 10:46:44 +00004127 else if (c == '\\') {
4128 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004129 }
4130 else if (c == '\'') {
4131 tok = TOK_LIT_CHAR;
4132 break;
4133 }
4134 }
4135 if (tok == TOK_UNKNOWN) {
4136 error(state, 0, "unterminated character constant");
4137 }
Eric Biedermancb364952004-11-15 10:46:44 +00004138 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004139 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004140 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004141
4142 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004143 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004144 }
4145 /* integer and floating constants
4146 * Integer Constants
4147 * {digits}
4148 * 0[Xx]{hexdigits}
4149 * 0{octdigit}+
4150 *
4151 * Floating constants
4152 * {digits}.{digits}[Ee][+-]?{digits}
4153 * {digits}.{digits}
4154 * {digits}[Ee][+-]?{digits}
4155 * .{digits}[Ee][+-]?{digits}
4156 * .{digits}
4157 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004158 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004159 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004160 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004161 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004162 is_float = 0;
4163 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004164 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004165 }
4166 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004167 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004168 }
Eric Biedermancb364952004-11-15 10:46:44 +00004169 cn = get_char(file, next);
4170 if (cn == '.') {
4171 next = next_char(file, next, 1);
4172 next = after_digits(file, next);
4173 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004174 }
Eric Biedermancb364952004-11-15 10:46:44 +00004175 cn = get_char(file, next);
4176 if ((cn == 'e') || (cn == 'E')) {
4177 const char *new;
4178 next = next_char(file, next, 1);
4179 cn = get_char(file, next);
4180 if ((cn == '+') || (cn == '-')) {
4181 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004182 }
Eric Biedermancb364952004-11-15 10:46:44 +00004183 new = after_digits(file, next);
4184 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004185 next = new;
4186 }
4187 if (is_float) {
4188 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004189 cn = get_char(file, next);
4190 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4191 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004192 }
4193 }
4194 if (!is_float && digitp(c)) {
4195 tok = TOK_LIT_INT;
4196 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004197 next = next_char(file, tokp, 1);
4198 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004199 }
4200 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004201 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004202 }
4203 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004204 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004205 }
4206 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004207 cn = get_char(file, next);
4208 if ((cn == 'u') || (cn == 'U')) {
4209 next = next_char(file, next, 1);
4210 cn = get_char(file, next);
4211 if ((cn == 'l') || (cn == 'L')) {
4212 next = next_char(file, next, 1);
4213 cn = get_char(file, next);
4214 }
4215 if ((cn == 'l') || (cn == 'L')) {
4216 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004217 }
4218 }
Eric Biedermancb364952004-11-15 10:46:44 +00004219 else if ((cn == 'l') || (cn == 'L')) {
4220 next = next_char(file, next, 1);
4221 cn = get_char(file, next);
4222 if ((cn == 'l') || (cn == 'L')) {
4223 next = next_char(file, next, 1);
4224 cn = get_char(file, next);
4225 }
4226 if ((cn == 'u') || (cn == 'U')) {
4227 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004228 }
4229 }
4230 }
Eric Biedermancb364952004-11-15 10:46:44 +00004231 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004232
4233 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004234 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004235 }
4236 /* identifiers */
4237 else if (letterp(c)) {
4238 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004239
4240 /* Find and save the identifier string */
4241 tokp = after_alnums(file, tokp);
4242 save_string(file, tk, token, tokp, "identifier");
4243
4244 /* Look up to see which identifier it is */
4245 tk->ident = lookup(state, tk->val.str, tk->str_len);
4246
4247 /* Free the identifier string */
4248 tk->str_len = 0;
4249 xfree(tk->val.str);
4250
Eric Biederman90089602004-05-28 14:11:54 +00004251 /* See if this identifier can be macro expanded */
4252 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004253 c = get_char(file, tokp);
4254 if (c == '$') {
4255 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004256 tk->val.notmacro = 1;
4257 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004258 }
4259 /* C99 alternate macro characters */
4260 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004261 eat += 3;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004262 tok = TOK_CONCATENATE;
4263 }
Eric Biedermancb364952004-11-15 10:46:44 +00004264 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4265 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4266 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4267 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4268 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4269 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4270 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4271 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4272 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4273 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4274 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4275 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4276 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4277 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4278 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4279 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4280 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4281 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4282 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4283 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4284 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4285 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4286 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4287 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4288 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4289 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4290 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4291 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004292 else if (c == ';') { tok = TOK_SEMI; }
4293 else if (c == '{') { tok = TOK_LBRACE; }
4294 else if (c == '}') { tok = TOK_RBRACE; }
4295 else if (c == ',') { tok = TOK_COMMA; }
4296 else if (c == '=') { tok = TOK_EQ; }
4297 else if (c == ':') { tok = TOK_COLON; }
4298 else if (c == '[') { tok = TOK_LBRACKET; }
4299 else if (c == ']') { tok = TOK_RBRACKET; }
4300 else if (c == '(') { tok = TOK_LPAREN; }
4301 else if (c == ')') { tok = TOK_RPAREN; }
4302 else if (c == '*') { tok = TOK_STAR; }
4303 else if (c == '>') { tok = TOK_MORE; }
4304 else if (c == '<') { tok = TOK_LESS; }
4305 else if (c == '?') { tok = TOK_QUEST; }
4306 else if (c == '|') { tok = TOK_OR; }
4307 else if (c == '&') { tok = TOK_AND; }
4308 else if (c == '^') { tok = TOK_XOR; }
4309 else if (c == '+') { tok = TOK_PLUS; }
4310 else if (c == '-') { tok = TOK_MINUS; }
4311 else if (c == '/') { tok = TOK_DIV; }
4312 else if (c == '%') { tok = TOK_MOD; }
4313 else if (c == '!') { tok = TOK_BANG; }
4314 else if (c == '.') { tok = TOK_DOT; }
4315 else if (c == '~') { tok = TOK_TILDE; }
4316 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004317 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004318
Eric Biedermancb364952004-11-15 10:46:44 +00004319 tokp = next_char(file, tokp, eat);
4320 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004321 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004322 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004323}
4324
4325static void check_tok(struct compile_state *state, struct token *tk, int tok)
4326{
4327 if (tk->tok != tok) {
4328 const char *name1, *name2;
4329 name1 = tokens[tk->tok];
4330 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004331 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004332 name2 = tk->ident->name;
4333 }
4334 error(state, 0, "\tfound %s %s expected %s",
4335 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004336 }
4337}
4338
Eric Biederman90089602004-05-28 14:11:54 +00004339struct macro_arg_value {
4340 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004341 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004342 size_t len;
4343};
4344static struct macro_arg_value *read_macro_args(
4345 struct compile_state *state, struct macro *macro,
4346 struct file_state *file, struct token *tk)
4347{
4348 struct macro_arg_value *argv;
4349 struct macro_arg *arg;
4350 int paren_depth;
4351 int i;
4352
4353 if (macro->argc == 0) {
4354 do {
4355 raw_next_token(state, file, tk);
4356 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004357 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004358 }
4359 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4360 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4361 argv[i].value = 0;
4362 argv[i].len = 0;
4363 argv[i].ident = arg->ident;
4364 }
4365 paren_depth = 0;
4366 i = 0;
4367
4368 for(;;) {
4369 const char *start;
4370 size_t len;
4371 start = file->pos;
4372 raw_next_token(state, file, tk);
4373
4374 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4375 (argv[i].ident != state->i___VA_ARGS__))
4376 {
4377 i++;
4378 if (i >= macro->argc) {
4379 error(state, 0, "too many args to %s\n",
4380 macro->ident->name);
4381 }
4382 continue;
4383 }
4384
4385 if (tk->tok == TOK_LPAREN) {
4386 paren_depth++;
4387 }
4388
4389 if (tk->tok == TOK_RPAREN) {
4390 if (paren_depth == 0) {
4391 break;
4392 }
4393 paren_depth--;
4394 }
4395 if (tk->tok == TOK_EOF) {
4396 error(state, 0, "End of file encountered while parsing macro arguments");
4397 }
Eric Biedermancb364952004-11-15 10:46:44 +00004398
4399 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004400 argv[i].value = xrealloc(
4401 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004402 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004403 argv[i].len += len;
4404 }
4405 if (i != macro->argc -1) {
4406 error(state, 0, "missing %s arg %d\n",
4407 macro->ident->name, i +2);
4408 }
4409 return argv;
4410}
4411
4412
4413static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4414{
4415 int i;
4416 for(i = 0; i < macro->argc; i++) {
4417 xfree(argv[i].value);
4418 }
4419 xfree(argv);
4420}
4421
4422struct macro_buf {
4423 char *str;
4424 size_t len, pos;
4425};
4426
Eric Biedermancb364952004-11-15 10:46:44 +00004427static void grow_macro_buf(struct compile_state *state,
4428 const char *id, struct macro_buf *buf,
4429 size_t grow)
4430{
4431 if ((buf->pos + grow) >= buf->len) {
4432 buf->str = xrealloc(buf->str, buf->len + grow, id);
4433 buf->len += grow;
4434 }
4435}
4436
Eric Biederman90089602004-05-28 14:11:54 +00004437static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004438 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004439 const char *fstart, size_t flen)
4440{
Eric Biedermancb364952004-11-15 10:46:44 +00004441 grow_macro_buf(state, id, buf, flen);
4442 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004443#if 0
4444 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4445 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004446 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004447#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004448 buf->pos += flen;
4449}
4450
4451
4452static void append_macro_chars(struct compile_state *state,
4453 const char *id, struct macro_buf *buf,
4454 struct file_state *file, const char *start, const char *end)
4455{
4456 size_t flen;
4457 flen = char_strlen(file, start, end);
4458 grow_macro_buf(state, id, buf, flen);
4459 char_strcpy(buf->str + buf->pos, file, start, end);
4460#if 0
4461 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4462 buf->pos, buf->pos, buf->str,
4463 flen, flen, buf->str + buf->pos);
4464#endif
Eric Biederman90089602004-05-28 14:11:54 +00004465 buf->pos += flen;
4466}
4467
4468static int compile_macro(struct compile_state *state,
4469 struct file_state **filep, struct token *tk);
4470
4471static void macro_expand_args(struct compile_state *state,
4472 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4473{
Eric Biedermancb364952004-11-15 10:46:44 +00004474 int i;
Eric Biederman90089602004-05-28 14:11:54 +00004475
4476 for(i = 0; i < macro->argc; i++) {
4477 struct file_state fmacro, *file;
4478 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004479
Eric Biedermancb364952004-11-15 10:46:44 +00004480 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004481 fmacro.basename = argv[i].ident->name;
4482 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004483 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004484 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004485 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004486 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004487 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004488 fmacro.report_line = 1;
4489 fmacro.report_name = fmacro.basename;
4490 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004491 fmacro.macro = 1;
4492 fmacro.trigraphs = 0;
4493 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004494
4495 buf.len = argv[i].len;
4496 buf.str = xmalloc(buf.len, argv[i].ident->name);
4497 buf.pos = 0;
4498
4499 file = &fmacro;
4500 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004501 raw_next_token(state, file, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004502
Eric Biedermancb364952004-11-15 10:46:44 +00004503 /* If we have recursed into another macro body
4504 * get out of it.
4505 */
Eric Biederman90089602004-05-28 14:11:54 +00004506 if (tk->tok == TOK_EOF) {
4507 struct file_state *old;
4508 old = file;
4509 file = file->prev;
4510 if (!file) {
4511 break;
4512 }
4513 /* old->basename is used keep it */
4514 xfree(old->dirname);
4515 xfree(old->buf);
4516 xfree(old);
4517 continue;
4518 }
4519 else if (tk->ident && tk->ident->sym_define) {
4520 if (compile_macro(state, &file, tk)) {
4521 continue;
4522 }
4523 }
4524
Eric Biedermancb364952004-11-15 10:46:44 +00004525 append_macro_chars(state, macro->ident->name, &buf,
4526 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004527 }
4528
4529 xfree(argv[i].value);
4530 argv[i].value = buf.str;
4531 argv[i].len = buf.pos;
4532 }
4533 return;
4534}
4535
4536static void expand_macro(struct compile_state *state,
4537 struct macro *macro, struct macro_buf *buf,
4538 struct macro_arg_value *argv, struct token *tk)
4539{
4540 struct file_state fmacro;
4541 const char space[] = " ";
4542 const char *fstart;
4543 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004544 int i, j;
4545
4546 /* Place the macro body in a dummy file */
4547 fmacro.prev = 0;
4548 fmacro.basename = macro->ident->name;
4549 fmacro.dirname = "";
4550 fmacro.buf = macro->buf;
4551 fmacro.size = macro->buf_len;
4552 fmacro.pos = fmacro.buf;
4553 fmacro.line = 1;
4554 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004555 fmacro.report_line = 1;
4556 fmacro.report_name = fmacro.basename;
4557 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004558 fmacro.macro = 1;
4559 fmacro.trigraphs = 0;
4560 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004561
Eric Biedermancb364952004-11-15 10:46:44 +00004562 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004563 buf->len = macro->buf_len + 3;
4564 buf->str = xmalloc(buf->len, macro->ident->name);
4565 buf->pos = 0;
4566
4567 fstart = fmacro.pos;
4568 raw_next_token(state, &fmacro, tk);
4569 while(tk->tok != TOK_EOF) {
4570 flen = fmacro.pos - fstart;
4571 switch(tk->tok) {
4572 case TOK_IDENT:
4573 for(i = 0; i < macro->argc; i++) {
4574 if (argv[i].ident == tk->ident) {
4575 break;
4576 }
4577 }
4578 if (i >= macro->argc) {
4579 break;
4580 }
4581 /* Substitute macro parameter */
4582 fstart = argv[i].value;
4583 flen = argv[i].len;
4584 break;
4585 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004586 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004587 break;
4588 }
4589 do {
4590 raw_next_token(state, &fmacro, tk);
4591 } while(tk->tok == TOK_SPACE);
4592 check_tok(state, tk, TOK_IDENT);
4593 for(i = 0; i < macro->argc; i++) {
4594 if (argv[i].ident == tk->ident) {
4595 break;
4596 }
4597 }
4598 if (i >= macro->argc) {
4599 error(state, 0, "parameter `%s' not found",
4600 tk->ident->name);
4601 }
4602 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004603 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004604 for(j = 0; j < argv[i].len; j++) {
4605 char *str = argv[i].value + j;
4606 size_t len = 1;
4607 if (*str == '\\') {
4608 str = "\\";
4609 len = 2;
4610 }
4611 else if (*str == '"') {
4612 str = "\\\"";
4613 len = 2;
4614 }
Eric Biedermancb364952004-11-15 10:46:44 +00004615 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004616 }
Eric Biedermancb364952004-11-15 10:46:44 +00004617 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004618 fstart = 0;
4619 flen = 0;
4620 break;
4621 case TOK_CONCATENATE:
4622 /* Concatenate tokens */
4623 /* Delete the previous whitespace token */
4624 if (buf->str[buf->pos - 1] == ' ') {
4625 buf->pos -= 1;
4626 }
4627 /* Skip the next sequence of whitspace tokens */
4628 do {
4629 fstart = fmacro.pos;
4630 raw_next_token(state, &fmacro, tk);
4631 } while(tk->tok == TOK_SPACE);
4632 /* Restart at the top of the loop.
4633 * I need to process the non white space token.
4634 */
4635 continue;
4636 break;
4637 case TOK_SPACE:
4638 /* Collapse multiple spaces into one */
4639 if (buf->str[buf->pos - 1] != ' ') {
4640 fstart = space;
4641 flen = 1;
4642 } else {
4643 fstart = 0;
4644 flen = 0;
4645 }
4646 break;
4647 default:
4648 break;
4649 }
4650
Eric Biedermancb364952004-11-15 10:46:44 +00004651 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004652
4653 fstart = fmacro.pos;
4654 raw_next_token(state, &fmacro, tk);
4655 }
4656}
4657
4658static void tag_macro_name(struct compile_state *state,
4659 struct macro *macro, struct macro_buf *buf,
4660 struct token *tk)
4661{
4662 /* Guard all instances of the macro name in the replacement
4663 * text from further macro expansion.
4664 */
4665 struct file_state fmacro;
4666 const char *fstart;
4667 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004668
4669 /* Put the old macro expansion buffer in a file */
4670 fmacro.prev = 0;
4671 fmacro.basename = macro->ident->name;
4672 fmacro.dirname = "";
4673 fmacro.buf = buf->str;
4674 fmacro.size = buf->pos;
4675 fmacro.pos = fmacro.buf;
4676 fmacro.line = 1;
4677 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004678 fmacro.report_line = 1;
4679 fmacro.report_name = fmacro.basename;
4680 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004681 fmacro.macro = 1;
4682 fmacro.trigraphs = 0;
4683 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004684
Eric Biedermancb364952004-11-15 10:46:44 +00004685 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004686 buf->len = macro->buf_len + 3;
4687 buf->str = xmalloc(buf->len, macro->ident->name);
4688 buf->pos = 0;
4689
4690 fstart = fmacro.pos;
4691 raw_next_token(state, &fmacro, tk);
4692 while(tk->tok != TOK_EOF) {
4693 flen = fmacro.pos - fstart;
4694 if ((tk->tok == TOK_IDENT) &&
4695 (tk->ident == macro->ident) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004696 (tk->val.notmacro == 0))
4697 {
4698 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004699 fstart = "$";
4700 flen = 1;
4701 }
4702
Eric Biedermancb364952004-11-15 10:46:44 +00004703 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004704
4705 fstart = fmacro.pos;
4706 raw_next_token(state, &fmacro, tk);
4707 }
4708 xfree(fmacro.buf);
4709}
Eric Biedermancb364952004-11-15 10:46:44 +00004710
Eric Biederman90089602004-05-28 14:11:54 +00004711static int compile_macro(struct compile_state *state,
4712 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004713{
4714 struct file_state *file;
4715 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004716 struct macro *macro;
4717 struct macro_arg_value *argv;
4718 struct macro_buf buf;
4719
4720#if 0
4721 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4722#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004723 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004724 macro = ident->sym_define;
4725
4726 /* If this token comes from a macro expansion ignore it */
4727 if (tk->val.notmacro) {
4728 return 0;
4729 }
4730 /* If I am a function like macro and the identifier is not followed
4731 * by a left parenthesis, do nothing.
4732 */
Eric Biedermancb364952004-11-15 10:46:44 +00004733 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004734 return 0;
4735 }
4736
4737 /* Read in the macro arguments */
4738 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004739 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004740 raw_next_token(state, *filep, tk);
4741 check_tok(state, tk, TOK_LPAREN);
4742
4743 argv = read_macro_args(state, macro, *filep, tk);
4744
4745 check_tok(state, tk, TOK_RPAREN);
4746 }
4747 /* Macro expand the macro arguments */
4748 macro_expand_args(state, macro, argv, tk);
4749
4750 buf.str = 0;
4751 buf.len = 0;
4752 buf.pos = 0;
4753 if (ident == state->i___FILE__) {
4754 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4755 buf.str = xmalloc(buf.len, ident->name);
4756 sprintf(buf.str, "\"%s\"", state->file->basename);
4757 buf.pos = strlen(buf.str);
4758 }
4759 else if (ident == state->i___LINE__) {
4760 buf.len = 30;
4761 buf.str = xmalloc(buf.len, ident->name);
4762 sprintf(buf.str, "%d", state->file->line);
4763 buf.pos = strlen(buf.str);
4764 }
4765 else {
4766 expand_macro(state, macro, &buf, argv, tk);
4767 }
4768 /* Tag the macro name with a $ so it will no longer
4769 * be regonized as a canidate for macro expansion.
4770 */
4771 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004772
4773#if 0
4774 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4775 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4776#endif
4777
4778 free_macro_args(macro, argv);
4779
Eric Biedermanb138ac82003-04-22 18:44:01 +00004780 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004781 file->prev = *filep;
4782 file->basename = xstrdup(ident->name);
4783 file->dirname = xstrdup("");
4784 file->buf = buf.str;
4785 file->size = buf.pos;
4786 file->pos = file->buf;
4787 file->line = 1;
4788 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004789 file->report_line = 1;
4790 file->report_name = file->basename;
4791 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004792 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004793 file->trigraphs = 0;
4794 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004795 *filep = file;
4796 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004797}
4798
Eric Biederman90089602004-05-28 14:11:54 +00004799static void eat_tokens(struct compile_state *state, int targ_tok)
4800{
4801 if (state->eat_depth > 0) {
4802 internal_error(state, 0, "Already eating...");
4803 }
4804 state->eat_depth = state->if_depth;
4805 state->eat_targ = targ_tok;
4806}
4807static int if_eat(struct compile_state *state)
4808{
4809 return state->eat_depth > 0;
4810}
4811static int if_value(struct compile_state *state)
4812{
4813 int index, offset;
4814 index = state->if_depth / CHAR_BIT;
4815 offset = state->if_depth % CHAR_BIT;
4816 return !!(state->if_bytes[index] & (1 << (offset)));
4817}
4818static void set_if_value(struct compile_state *state, int value)
4819{
4820 int index, offset;
4821 index = state->if_depth / CHAR_BIT;
4822 offset = state->if_depth % CHAR_BIT;
4823
4824 state->if_bytes[index] &= ~(1 << offset);
4825 if (value) {
4826 state->if_bytes[index] |= (1 << offset);
4827 }
4828}
4829static void in_if(struct compile_state *state, const char *name)
4830{
4831 if (state->if_depth <= 0) {
4832 error(state, 0, "%s without #if", name);
4833 }
4834}
4835static void enter_if(struct compile_state *state)
4836{
4837 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004838 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004839 error(state, 0, "#if depth too great");
4840 }
4841}
4842static void reenter_if(struct compile_state *state, const char *name)
4843{
4844 in_if(state, name);
4845 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004846 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004847 state->eat_depth = 0;
4848 state->eat_targ = 0;
4849 }
4850}
4851static void enter_else(struct compile_state *state, const char *name)
4852{
4853 in_if(state, name);
4854 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004855 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004856 state->eat_depth = 0;
4857 state->eat_targ = 0;
4858 }
4859}
4860static void exit_if(struct compile_state *state, const char *name)
4861{
4862 in_if(state, name);
4863 if (state->eat_depth == state->if_depth) {
4864 state->eat_depth = 0;
4865 state->eat_targ = 0;
4866 }
4867 state->if_depth -= 1;
4868}
4869
Eric Biedermancb364952004-11-15 10:46:44 +00004870static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004871{
4872 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004873 int rescan;
4874
Eric Biedermancb364952004-11-15 10:46:44 +00004875 file = state->file;
4876 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004877 do {
4878 rescan = 0;
4879 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004880 /* Exit out of an include directive or macro call */
4881 if ((tk->tok == TOK_EOF) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004882 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004883 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004884 state->file = file->prev;
4885 /* file->basename is used keep it */
4886 xfree(file->dirname);
4887 xfree(file->buf);
4888 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004889 file = 0;
4890 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004891 rescan = 1;
4892 }
Eric Biederman41203d92004-11-08 09:31:09 +00004893 } while(rescan);
4894}
4895
Eric Biedermancb364952004-11-15 10:46:44 +00004896static void pp_token(struct compile_state *state, struct token *tk)
4897{
4898 struct file_state *file;
4899 int rescan;
4900
4901 raw_token(state, tk);
4902 do {
4903 rescan = 0;
4904 file = state->file;
4905 if (tk->tok == TOK_SPACE) {
4906 raw_token(state, tk);
4907 rescan = 1;
4908 }
4909 else if (tk->tok == TOK_IDENT) {
4910 if (state->token_base == 0) {
4911 ident_to_keyword(state, tk);
4912 } else {
4913 ident_to_macro(state, tk);
4914 }
4915 }
4916 } while(rescan);
4917}
4918
Eric Biederman41203d92004-11-08 09:31:09 +00004919static void preprocess(struct compile_state *state, struct token *tk);
4920
4921static void token(struct compile_state *state, struct token *tk)
4922{
4923 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004924 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004925 do {
4926 rescan = 0;
4927 /* Process a macro directive */
4928 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004929 /* Only match preprocessor directives at the start of a line */
4930 const char *ptr;
4931 ptr = state->file->line_start;
4932 while((ptr < tk->pos)
4933 && spacep(get_char(state->file, ptr)))
4934 {
4935 ptr = next_char(state->file, ptr, 1);
4936 }
4937 if (ptr == tk->pos) {
4938 preprocess(state, tk);
4939 rescan = 1;
4940 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004941 }
Eric Biederman41203d92004-11-08 09:31:09 +00004942 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004943 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004944 rescan = compile_macro(state, &state->file, tk);
4945 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004946 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004947 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004948 }
Eric Biedermancb364952004-11-15 10:46:44 +00004949 /* Eat tokens disabled by the preprocessor
4950 * (Unless we are parsing a preprocessor directive
4951 */
Eric Biedermana649a412004-11-09 00:35:39 +00004952 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004953 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004954 rescan = 1;
4955 }
Eric Biedermana649a412004-11-09 00:35:39 +00004956 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004957 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004958 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004959 rescan = 1;
4960 }
Eric Biedermancb364952004-11-15 10:46:44 +00004961 /* Error on unknown tokens */
4962 else if (tk->tok == TOK_UNKNOWN) {
4963 error(state, 0, "unknown token");
4964 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004965 } while(rescan);
4966}
4967
Eric Biederman41203d92004-11-08 09:31:09 +00004968
4969static inline struct token *get_token(struct compile_state *state, int offset)
4970{
4971 int index;
4972 index = state->token_base + offset;
4973 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4974 internal_error(state, 0, "token array to small");
4975 }
4976 return &state->token[index];
4977}
4978
4979static struct token *do_eat_token(struct compile_state *state, int tok)
4980{
4981 struct token *tk;
4982 int i;
4983 check_tok(state, get_token(state, 1), tok);
4984
4985 /* Free the old token value */
4986 tk = get_token(state, 0);
4987 if (tk->str_len) {
4988 memset((void *)tk->val.str, -1, tk->str_len);
4989 xfree(tk->val.str);
4990 }
4991 /* Overwrite the old token with newer tokens */
4992 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4993 state->token[i] = state->token[i + 1];
4994 }
4995 /* Clear the last token */
4996 memset(&state->token[i], 0, sizeof(state->token[i]));
4997 state->token[i].tok = -1;
4998
4999 /* Return the token */
5000 return tk;
5001}
5002
Eric Biedermancb364952004-11-15 10:46:44 +00005003static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005004{
5005 struct token *tk1;
5006 tk1 = get_token(state, 1);
5007 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005008 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005009 }
5010 return tk1->tok;
5011}
5012
Eric Biedermancb364952004-11-15 10:46:44 +00005013static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005014{
Eric Biedermancb364952004-11-15 10:46:44 +00005015 raw_peek(state);
5016 return do_eat_token(state, tok);
5017}
5018
5019static int pp_peek(struct compile_state *state)
5020{
5021 struct token *tk1;
5022 tk1 = get_token(state, 1);
5023 if (tk1->tok == -1) {
5024 pp_token(state, tk1);
5025 }
5026 return tk1->tok;
5027}
5028
5029static struct token *pp_eat(struct compile_state *state, int tok)
5030{
5031 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005032 return do_eat_token(state, tok);
5033}
5034
Eric Biedermanb138ac82003-04-22 18:44:01 +00005035static int peek(struct compile_state *state)
5036{
Eric Biederman41203d92004-11-08 09:31:09 +00005037 struct token *tk1;
5038 tk1 = get_token(state, 1);
5039 if (tk1->tok == -1) {
5040 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005041 }
Eric Biederman41203d92004-11-08 09:31:09 +00005042 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005043}
5044
5045static int peek2(struct compile_state *state)
5046{
Eric Biederman41203d92004-11-08 09:31:09 +00005047 struct token *tk1, *tk2;
5048 tk1 = get_token(state, 1);
5049 tk2 = get_token(state, 2);
5050 if (tk1->tok == -1) {
5051 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005052 }
Eric Biederman41203d92004-11-08 09:31:09 +00005053 if (tk2->tok == -1) {
5054 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005055 }
Eric Biederman41203d92004-11-08 09:31:09 +00005056 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005057}
5058
Eric Biederman41203d92004-11-08 09:31:09 +00005059static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005060{
Eric Biederman90089602004-05-28 14:11:54 +00005061 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005062 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005063}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005064
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005065static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005066{
Eric Biederman90089602004-05-28 14:11:54 +00005067 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005068 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005069 int subdir_len;
5070 struct file_state *file;
5071 char *basename;
5072 file = xmalloc(sizeof(*file), "file_state");
5073
5074 base = strrchr(filename, '/');
5075 subdir = filename;
5076 if (base != 0) {
5077 subdir_len = base - filename;
5078 base++;
5079 }
5080 else {
5081 base = filename;
5082 subdir_len = 0;
5083 }
5084 basename = xmalloc(strlen(base) +1, "basename");
5085 strcpy(basename, base);
5086 file->basename = basename;
5087
5088 if (getcwd(cwd, sizeof(cwd)) == 0) {
5089 die("cwd buffer to small");
5090 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00005091 if (subdir[0] == '/') {
5092 file->dirname = xmalloc(subdir_len + 1, "dirname");
5093 memcpy(file->dirname, subdir, subdir_len);
5094 file->dirname[subdir_len] = '\0';
5095 }
5096 else {
Eric Biederman90089602004-05-28 14:11:54 +00005097 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005098 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005099 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005100 /* Find the appropriate directory... */
5101 dir = 0;
5102 if (!state->file && exists(cwd, filename)) {
5103 dir = cwd;
5104 }
5105 if (local && state->file && exists(state->file->dirname, filename)) {
5106 dir = state->file->dirname;
5107 }
Eric Biederman90089602004-05-28 14:11:54 +00005108 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005109 if (exists(*path, filename)) {
5110 dir = *path;
5111 }
5112 }
5113 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005114 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005115 }
5116 dirlen = strlen(dir);
5117 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5118 memcpy(file->dirname, dir, dirlen);
5119 file->dirname[dirlen] = '/';
5120 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5121 file->dirname[dirlen + 1 + subdir_len] = '\0';
5122 }
5123 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005124
5125 file->pos = file->buf;
5126 file->line_start = file->pos;
5127 file->line = 1;
5128
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005129 file->report_line = 1;
5130 file->report_name = file->basename;
5131 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005132 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005133 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5134 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005135
Eric Biedermanb138ac82003-04-22 18:44:01 +00005136 file->prev = state->file;
5137 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005138}
5139
Eric Biederman41203d92004-11-08 09:31:09 +00005140static struct triple *constant_expr(struct compile_state *state);
5141static void integral(struct compile_state *state, struct triple *def);
5142
5143static int mcexpr(struct compile_state *state)
5144{
5145 struct triple *cvalue;
5146 cvalue = constant_expr(state);
5147 integral(state, cvalue);
5148 if (cvalue->op != OP_INTCONST) {
5149 error(state, 0, "integer constant expected");
5150 }
5151 return cvalue->u.cval != 0;
5152}
5153
5154static void preprocess(struct compile_state *state, struct token *current_token)
5155{
5156 /* Doing much more with the preprocessor would require
5157 * a parser and a major restructuring.
5158 * Postpone that for later.
5159 */
Eric Biederman41203d92004-11-08 09:31:09 +00005160 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005161 int tok;
5162
Eric Biedermancb364952004-11-15 10:46:44 +00005163 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005164
5165 old_token_base = state->token_base;
5166 state->token_base = current_token - state->token;
5167
Eric Biedermancb364952004-11-15 10:46:44 +00005168 tok = pp_peek(state);
5169 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005170 case TOK_LIT_INT:
5171 {
5172 struct token *tk;
5173 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005174 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005175 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005176 /* I have a preprocessor line marker parse it */
5177 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005178 const char *token, *base;
5179 char *name, *dir;
5180 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005181 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005182 name = xmalloc(tk->str_len, "report_name");
5183 token = tk->val.str + 1;
5184 base = strrchr(token, '/');
5185 name_len = tk->str_len -2;
5186 if (base != 0) {
5187 dir_len = base - token;
5188 base++;
5189 name_len -= base - token;
5190 } else {
5191 dir_len = 0;
5192 base = token;
5193 }
5194 memcpy(name, base, name_len);
5195 name[name_len] = '\0';
5196 dir = xmalloc(dir_len + 1, "report_dir");
5197 memcpy(dir, token, dir_len);
5198 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005199 state->file->report_line = override_line - 1;
5200 state->file->report_name = name;
5201 state->file->report_dir = dir;
5202 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005203 }
5204 break;
5205 }
5206 case TOK_MLINE:
5207 {
5208 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005209 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005210 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005211 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5212 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005213 const char *token, *base;
5214 char *name, *dir;
5215 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005216 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005217 name = xmalloc(tk->str_len, "report_name");
5218 token = tk->val.str + 1;
5219 base = strrchr(token, '/');
5220 name_len = tk->str_len - 2;
5221 if (base != 0) {
5222 dir_len = base - token;
5223 base++;
5224 name_len -= base - token;
5225 } else {
5226 dir_len = 0;
5227 base = token;
5228 }
5229 memcpy(name, base, name_len);
5230 name[name_len] = '\0';
5231 dir = xmalloc(dir_len + 1, "report_dir");
5232 memcpy(dir, token, dir_len);
5233 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005234 state->file->report_name = name;
5235 state->file->report_dir = dir;
5236 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005237 }
5238 break;
5239 }
5240 case TOK_MUNDEF:
5241 {
5242 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005243 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005244 if (if_eat(state)) /* quit early when #if'd out */
5245 break;
5246
Eric Biedermancb364952004-11-15 10:46:44 +00005247 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005248
5249 undef_macro(state, ident);
5250 break;
5251 }
5252 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005253 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005254 if (if_eat(state)) /* quit early when #if'd out */
5255 break;
5256 warning(state, 0, "Ignoring pragma");
5257 break;
5258 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005259 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005260 reenter_if(state, "#elif");
5261 if (if_eat(state)) /* quit early when #if'd out */
5262 break;
5263 /* If the #if was taken the #elif just disables the following code */
5264 if (if_value(state)) {
5265 eat_tokens(state, TOK_MENDIF);
5266 }
5267 /* If the previous #if was not taken see if the #elif enables the
5268 * trailing code.
5269 */
5270 else {
5271 set_if_value(state, mcexpr(state));
5272 if (!if_value(state)) {
5273 eat_tokens(state, TOK_MELSE);
5274 }
5275 }
5276 break;
5277 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005278 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005279 enter_if(state);
5280 if (if_eat(state)) /* quit early when #if'd out */
5281 break;
5282 set_if_value(state, mcexpr(state));
5283 if (!if_value(state)) {
5284 eat_tokens(state, TOK_MELSE);
5285 }
5286 break;
5287 case TOK_MIFNDEF:
5288 {
5289 struct hash_entry *ident;
5290
Eric Biedermancb364952004-11-15 10:46:44 +00005291 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005292 enter_if(state);
5293 if (if_eat(state)) /* quit early when #if'd out */
5294 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005295 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005296 set_if_value(state, ident->sym_define == 0);
5297 if (!if_value(state)) {
5298 eat_tokens(state, TOK_MELSE);
5299 }
5300 break;
5301 }
5302 case TOK_MIFDEF:
5303 {
5304 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005305 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005306 enter_if(state);
5307 if (if_eat(state)) /* quit early when #if'd out */
5308 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005309 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005310 set_if_value(state, ident->sym_define != 0);
5311 if (!if_value(state)) {
5312 eat_tokens(state, TOK_MELSE);
5313 }
5314 break;
5315 }
5316 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005317 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005318 enter_else(state, "#else");
5319 if (!if_eat(state) && if_value(state)) {
5320 eat_tokens(state, TOK_MENDIF);
5321 }
5322 break;
5323 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005324 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005325 exit_if(state, "#endif");
5326 break;
5327 case TOK_MDEFINE:
5328 {
5329 struct hash_entry *ident;
5330 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005331 const char *mstart, *mend;
5332 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005333
Eric Biedermancb364952004-11-15 10:46:44 +00005334 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005335 if (if_eat(state)) /* quit early when #if'd out */
5336 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005337 ident = pp_eat(state, TOK_MIDENT)->ident;
5338 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005339 args = 0;
5340 larg = &args;
5341
Eric Biederman41203d92004-11-08 09:31:09 +00005342 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005343 if (raw_peek(state) == TOK_LPAREN) {
5344 raw_eat(state, TOK_LPAREN);
5345 argc += 1;
5346
Eric Biederman41203d92004-11-08 09:31:09 +00005347 for(;;) {
5348 struct macro_arg *narg, *arg;
5349 struct hash_entry *aident;
5350 int tok;
5351
Eric Biedermancb364952004-11-15 10:46:44 +00005352 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005353 if (!args && (tok == TOK_RPAREN)) {
5354 break;
5355 }
5356 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005357 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005358 aident = state->i___VA_ARGS__;
5359 }
5360 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005361 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005362 }
5363
5364 narg = xcmalloc(sizeof(*arg), "macro arg");
5365 narg->ident = aident;
5366
5367 /* Verify I don't have a duplicate identifier */
5368 for(arg = args; arg; arg = arg->next) {
5369 if (arg->ident == narg->ident) {
5370 error(state, 0, "Duplicate macro arg `%s'",
5371 narg->ident->name);
5372 }
5373 }
5374 /* Add the new argument to the end of the list */
5375 *larg = narg;
5376 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005377 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005378
5379 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005380 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005381 break;
5382 }
Eric Biedermancb364952004-11-15 10:46:44 +00005383 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005384 }
Eric Biedermancb364952004-11-15 10:46:44 +00005385 pp_eat(state, TOK_RPAREN);
5386 }
5387 /* Remove leading whitespace */
5388 while(raw_peek(state) == TOK_SPACE) {
5389 raw_eat(state, TOK_SPACE);
5390 }
Eric Biederman41203d92004-11-08 09:31:09 +00005391
Eric Biedermancb364952004-11-15 10:46:44 +00005392 /* Remember the start of the macro body */
5393 tok = raw_peek(state);
5394 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005395
Eric Biedermancb364952004-11-15 10:46:44 +00005396 /* Find the end of the macro */
5397 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5398 raw_eat(state, tok);
5399 /* Remember the end of the last non space token */
5400 raw_peek(state);
5401 if (tok != TOK_SPACE) {
5402 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005403 }
5404 }
Eric Biedermancb364952004-11-15 10:46:44 +00005405
5406 /* Now that I have found the body defined the token */
5407 do_define_macro(state, ident,
5408 char_strdup(state->file, mstart, mend, "macro buf"),
5409 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005410 break;
5411 }
5412 case TOK_MERROR:
5413 {
Eric Biedermancb364952004-11-15 10:46:44 +00005414 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005415 int len;
5416
Eric Biedermancb364952004-11-15 10:46:44 +00005417 pp_eat(state, TOK_MERROR);
5418 /* Find the start of the line */
5419 raw_peek(state);
5420 start = get_token(state, 1)->pos;
5421
Eric Biederman41203d92004-11-08 09:31:09 +00005422 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005423 while((tok = raw_peek(state)) != TOK_EOL) {
5424 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005425 }
Eric Biedermancb364952004-11-15 10:46:44 +00005426 end = get_token(state, 1)->pos;
5427 len = end - start;
5428 if (!if_eat(state)) {
5429 error(state, 0, "%*.*s", len, len, start);
5430 }
Eric Biederman41203d92004-11-08 09:31:09 +00005431 break;
5432 }
5433 case TOK_MWARNING:
5434 {
Eric Biedermancb364952004-11-15 10:46:44 +00005435 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005436 int len;
5437
Eric Biedermancb364952004-11-15 10:46:44 +00005438 pp_eat(state, TOK_MWARNING);
5439
5440 /* Find the start of the line */
5441 raw_peek(state);
5442 start = get_token(state, 1)->pos;
5443
Eric Biederman41203d92004-11-08 09:31:09 +00005444 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005445 while((tok = raw_peek(state)) != TOK_EOL) {
5446 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005447 }
Eric Biedermancb364952004-11-15 10:46:44 +00005448 end = get_token(state, 1)->pos;
5449 len = end - start;
5450 if (!if_eat(state)) {
5451 warning(state, 0, "%*.*s", len, len, start);
5452 }
Eric Biederman41203d92004-11-08 09:31:09 +00005453 break;
5454 }
5455 case TOK_MINCLUDE:
5456 {
5457 char *name;
5458 int local;
5459 local = 0;
5460 name = 0;
5461
Eric Biedermancb364952004-11-15 10:46:44 +00005462 pp_eat(state, TOK_MINCLUDE);
Eric Biederman41203d92004-11-08 09:31:09 +00005463 tok = peek(state);
5464 if (tok == TOK_LIT_STRING) {
5465 struct token *tk;
5466 const char *token;
5467 int name_len;
5468 tk = eat(state, TOK_LIT_STRING);
5469 name = xmalloc(tk->str_len, "include");
5470 token = tk->val.str +1;
5471 name_len = tk->str_len -2;
5472 if (*token == '"') {
5473 token++;
5474 name_len--;
5475 }
5476 memcpy(name, token, name_len);
5477 name[name_len] = '\0';
5478 local = 1;
5479 }
5480 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005481 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005482 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005483
5484 buf.len = 40;
5485 buf.str = xmalloc(buf.len, "include");
5486 buf.pos = 0;
5487
5488 tok = peek(state);
5489 while((tok != TOK_MORE) &&
5490 (tok != TOK_EOL) && (tok != TOK_EOF))
5491 {
5492 struct token *tk;
5493 tk = eat(state, tok);
5494 append_macro_chars(state, "include", &buf,
5495 state->file, tk->pos, state->file->pos);
5496 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005497 }
Eric Biedermancb364952004-11-15 10:46:44 +00005498 append_macro_text(state, "include", &buf, "\0", 1);
5499 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005500 error(state, 0, "Unterminated include directive");
5501 }
Eric Biederman41203d92004-11-08 09:31:09 +00005502 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005503 local = 0;
5504 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005505 }
5506 else {
5507 error(state, 0, "Invalid include directive");
5508 }
5509 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005510 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005511 error(state, 0, "garbage after include directive");
5512 }
5513 if (!if_eat(state)) {
5514 compile_file(state, name, local);
5515 }
5516 xfree(name);
5517 break;
5518 }
5519 case TOK_EOL:
5520 /* Ignore # without a follwing ident */
5521 break;
5522 default:
5523 {
5524 const char *name1, *name2;
5525 name1 = tokens[tok];
5526 name2 = "";
5527 if (tok == TOK_MIDENT) {
5528 name2 = get_token(state, 1)->ident->name;
5529 }
5530 error(state, 0, "Invalid preprocessor directive: %s %s",
5531 name1, name2);
5532 break;
5533 }
5534 }
5535 /* Consume the rest of the macro line */
5536 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005537 tok = pp_peek(state);
5538 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005539 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5540 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005541 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005542 return;
5543}
5544
Eric Biederman0babc1c2003-05-09 02:39:00 +00005545/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005546
5547static struct type *new_type(
5548 unsigned int type, struct type *left, struct type *right)
5549{
5550 struct type *result;
5551 result = xmalloc(sizeof(*result), "type");
5552 result->type = type;
5553 result->left = left;
5554 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005555 result->field_ident = 0;
5556 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005557 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005558 return result;
5559}
5560
5561static struct type *clone_type(unsigned int specifiers, struct type *old)
5562{
5563 struct type *result;
5564 result = xmalloc(sizeof(*result), "type");
5565 memcpy(result, old, sizeof(*result));
5566 result->type &= TYPE_MASK;
5567 result->type |= specifiers;
5568 return result;
5569}
5570
Eric Biederman90089602004-05-28 14:11:54 +00005571static struct type *dup_type(struct compile_state *state, struct type *orig)
5572{
5573 struct type *new;
5574 new = xcmalloc(sizeof(*new), "type");
5575 new->type = orig->type;
5576 new->field_ident = orig->field_ident;
5577 new->type_ident = orig->type_ident;
5578 new->elements = orig->elements;
5579 if (orig->left) {
5580 new->left = dup_type(state, orig->left);
5581 }
5582 if (orig->right) {
5583 new->right = dup_type(state, orig->right);
5584 }
5585 return new;
5586}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005587
Eric Biederman90089602004-05-28 14:11:54 +00005588
5589static struct type *invalid_type(struct compile_state *state, struct type *type)
5590{
5591 struct type *invalid, *member;
5592 invalid = 0;
5593 if (!type) {
5594 internal_error(state, 0, "type missing?");
5595 }
5596 switch(type->type & TYPE_MASK) {
5597 case TYPE_VOID:
5598 case TYPE_CHAR: case TYPE_UCHAR:
5599 case TYPE_SHORT: case TYPE_USHORT:
5600 case TYPE_INT: case TYPE_UINT:
5601 case TYPE_LONG: case TYPE_ULONG:
5602 case TYPE_LLONG: case TYPE_ULLONG:
5603 case TYPE_POINTER:
5604 case TYPE_ENUM:
5605 break;
5606 case TYPE_BITFIELD:
5607 invalid = invalid_type(state, type->left);
5608 break;
5609 case TYPE_ARRAY:
5610 invalid = invalid_type(state, type->left);
5611 break;
5612 case TYPE_STRUCT:
5613 case TYPE_TUPLE:
5614 member = type->left;
5615 while(member && (invalid == 0) &&
5616 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5617 invalid = invalid_type(state, member->left);
5618 member = member->right;
5619 }
5620 if (!invalid) {
5621 invalid = invalid_type(state, member);
5622 }
5623 break;
5624 case TYPE_UNION:
5625 case TYPE_JOIN:
5626 member = type->left;
5627 while(member && (invalid == 0) &&
5628 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5629 invalid = invalid_type(state, member->left);
5630 member = member->right;
5631 }
5632 if (!invalid) {
5633 invalid = invalid_type(state, member);
5634 }
5635 break;
5636 default:
5637 invalid = type;
5638 break;
5639 }
5640 return invalid;
5641
5642}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005643
5644#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005645#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005646static inline ulong_t mask_uint(ulong_t x)
5647{
5648 if (SIZEOF_INT < SIZEOF_LONG) {
Stefan Reinauer50542a82007-10-24 11:14:14 +00005649 ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005650 x &= mask;
5651 }
5652 return x;
5653}
5654#define MASK_UINT(X) (mask_uint(X))
5655#define MASK_ULONG(X) (X)
5656
Eric Biederman90089602004-05-28 14:11:54 +00005657static struct type void_type = { .type = TYPE_VOID };
5658static struct type char_type = { .type = TYPE_CHAR };
5659static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005660#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005661static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005662#endif
Eric Biederman90089602004-05-28 14:11:54 +00005663static struct type ushort_type = { .type = TYPE_USHORT };
5664static struct type int_type = { .type = TYPE_INT };
5665static struct type uint_type = { .type = TYPE_UINT };
5666static struct type long_type = { .type = TYPE_LONG };
5667static struct type ulong_type = { .type = TYPE_ULONG };
5668static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005669
Eric Biederman5ade04a2003-10-22 04:03:46 +00005670static struct type void_ptr_type = {
5671 .type = TYPE_POINTER,
5672 .left = &void_type,
5673};
5674
Stefan Reinauer50542a82007-10-24 11:14:14 +00005675#if DEBUG_ROMCC_WARNING
Eric Biederman5ade04a2003-10-22 04:03:46 +00005676static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005677 .type = TYPE_FUNCTION,
5678 .left = &void_type,
5679 .right = &void_type,
5680};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005681#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005682
Eric Biederman90089602004-05-28 14:11:54 +00005683static size_t bits_to_bytes(size_t size)
5684{
5685 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5686}
5687
Eric Biedermanb138ac82003-04-22 18:44:01 +00005688static struct triple *variable(struct compile_state *state, struct type *type)
5689{
5690 struct triple *result;
5691 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005692 result = triple(state, OP_ADECL, type, 0, 0);
5693 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005694 }
5695 else {
5696 result = triple(state, OP_SDECL, type, 0, 0);
5697 }
5698 return result;
5699}
5700
5701static void stor_of(FILE *fp, struct type *type)
5702{
5703 switch(type->type & STOR_MASK) {
5704 case STOR_AUTO:
5705 fprintf(fp, "auto ");
5706 break;
5707 case STOR_STATIC:
5708 fprintf(fp, "static ");
5709 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005710 case STOR_LOCAL:
5711 fprintf(fp, "local ");
5712 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005713 case STOR_EXTERN:
5714 fprintf(fp, "extern ");
5715 break;
5716 case STOR_REGISTER:
5717 fprintf(fp, "register ");
5718 break;
5719 case STOR_TYPEDEF:
5720 fprintf(fp, "typedef ");
5721 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005722 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005723 fprintf(fp, "inline ");
5724 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005725 case STOR_INLINE | STOR_STATIC:
5726 fprintf(fp, "static inline");
5727 break;
5728 case STOR_INLINE | STOR_EXTERN:
5729 fprintf(fp, "extern inline");
5730 break;
5731 default:
5732 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5733 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005734 }
5735}
5736static void qual_of(FILE *fp, struct type *type)
5737{
5738 if (type->type & QUAL_CONST) {
5739 fprintf(fp, " const");
5740 }
5741 if (type->type & QUAL_VOLATILE) {
5742 fprintf(fp, " volatile");
5743 }
5744 if (type->type & QUAL_RESTRICT) {
5745 fprintf(fp, " restrict");
5746 }
5747}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005748
Eric Biedermanb138ac82003-04-22 18:44:01 +00005749static void name_of(FILE *fp, struct type *type)
5750{
Eric Biederman90089602004-05-28 14:11:54 +00005751 unsigned int base_type;
5752 base_type = type->type & TYPE_MASK;
5753 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5754 stor_of(fp, type);
5755 }
5756 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005757 case TYPE_VOID:
5758 fprintf(fp, "void");
5759 qual_of(fp, type);
5760 break;
5761 case TYPE_CHAR:
5762 fprintf(fp, "signed char");
5763 qual_of(fp, type);
5764 break;
5765 case TYPE_UCHAR:
5766 fprintf(fp, "unsigned char");
5767 qual_of(fp, type);
5768 break;
5769 case TYPE_SHORT:
5770 fprintf(fp, "signed short");
5771 qual_of(fp, type);
5772 break;
5773 case TYPE_USHORT:
5774 fprintf(fp, "unsigned short");
5775 qual_of(fp, type);
5776 break;
5777 case TYPE_INT:
5778 fprintf(fp, "signed int");
5779 qual_of(fp, type);
5780 break;
5781 case TYPE_UINT:
5782 fprintf(fp, "unsigned int");
5783 qual_of(fp, type);
5784 break;
5785 case TYPE_LONG:
5786 fprintf(fp, "signed long");
5787 qual_of(fp, type);
5788 break;
5789 case TYPE_ULONG:
5790 fprintf(fp, "unsigned long");
5791 qual_of(fp, type);
5792 break;
5793 case TYPE_POINTER:
5794 name_of(fp, type->left);
5795 fprintf(fp, " * ");
5796 qual_of(fp, type);
5797 break;
5798 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005799 name_of(fp, type->left);
5800 fprintf(fp, ", ");
5801 name_of(fp, type->right);
5802 break;
Eric Biederman90089602004-05-28 14:11:54 +00005803 case TYPE_OVERLAP:
5804 name_of(fp, type->left);
5805 fprintf(fp, ",| ");
5806 name_of(fp, type->right);
5807 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005808 case TYPE_ENUM:
Eric Biederman90089602004-05-28 14:11:54 +00005809 fprintf(fp, "enum %s",
5810 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005811 qual_of(fp, type);
5812 break;
5813 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005814 fprintf(fp, "struct %s { ",
5815 (type->type_ident)? type->type_ident->name : "");
5816 name_of(fp, type->left);
5817 fprintf(fp, " } ");
5818 qual_of(fp, type);
5819 break;
5820 case TYPE_UNION:
5821 fprintf(fp, "union %s { ",
5822 (type->type_ident)? type->type_ident->name : "");
5823 name_of(fp, type->left);
5824 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005825 qual_of(fp, type);
5826 break;
5827 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005828 name_of(fp, type->left);
5829 fprintf(fp, " (*)(");
5830 name_of(fp, type->right);
5831 fprintf(fp, ")");
5832 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005833 case TYPE_ARRAY:
5834 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005835 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005836 break;
Eric Biederman90089602004-05-28 14:11:54 +00005837 case TYPE_TUPLE:
5838 fprintf(fp, "tuple { ");
5839 name_of(fp, type->left);
5840 fprintf(fp, " } ");
5841 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005842 break;
Eric Biederman90089602004-05-28 14:11:54 +00005843 case TYPE_JOIN:
5844 fprintf(fp, "join { ");
5845 name_of(fp, type->left);
5846 fprintf(fp, " } ");
5847 qual_of(fp, type);
5848 break;
5849 case TYPE_BITFIELD:
5850 name_of(fp, type->left);
5851 fprintf(fp, " : %d ", type->elements);
5852 qual_of(fp, type);
5853 break;
5854 case TYPE_UNKNOWN:
5855 fprintf(fp, "unknown_t");
5856 break;
5857 default:
5858 fprintf(fp, "????: %x", base_type);
5859 break;
5860 }
5861 if (type->field_ident && type->field_ident->name) {
5862 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005863 }
5864}
5865
5866static size_t align_of(struct compile_state *state, struct type *type)
5867{
5868 size_t align;
5869 align = 0;
5870 switch(type->type & TYPE_MASK) {
5871 case TYPE_VOID:
5872 align = 1;
5873 break;
Eric Biederman90089602004-05-28 14:11:54 +00005874 case TYPE_BITFIELD:
5875 align = 1;
5876 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005877 case TYPE_CHAR:
5878 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005879 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005880 break;
5881 case TYPE_SHORT:
5882 case TYPE_USHORT:
5883 align = ALIGNOF_SHORT;
5884 break;
5885 case TYPE_INT:
5886 case TYPE_UINT:
5887 case TYPE_ENUM:
5888 align = ALIGNOF_INT;
5889 break;
5890 case TYPE_LONG:
5891 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005892 align = ALIGNOF_LONG;
5893 break;
Eric Biederman90089602004-05-28 14:11:54 +00005894 case TYPE_POINTER:
5895 align = ALIGNOF_POINTER;
5896 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005897 case TYPE_PRODUCT:
5898 case TYPE_OVERLAP:
5899 {
5900 size_t left_align, right_align;
5901 left_align = align_of(state, type->left);
5902 right_align = align_of(state, type->right);
5903 align = (left_align >= right_align) ? left_align : right_align;
5904 break;
5905 }
5906 case TYPE_ARRAY:
5907 align = align_of(state, type->left);
5908 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005909 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005910 case TYPE_TUPLE:
5911 case TYPE_UNION:
5912 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005913 align = align_of(state, type->left);
5914 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005915 default:
5916 error(state, 0, "alignof not yet defined for type\n");
5917 break;
5918 }
5919 return align;
5920}
5921
Eric Biederman90089602004-05-28 14:11:54 +00005922static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005923{
Eric Biederman90089602004-05-28 14:11:54 +00005924 size_t align;
5925 align = 0;
5926 switch(type->type & TYPE_MASK) {
5927 case TYPE_VOID:
5928 align = 1;
5929 break;
5930 case TYPE_BITFIELD:
5931 align = 1;
5932 break;
5933 case TYPE_CHAR:
5934 case TYPE_UCHAR:
5935 align = REG_ALIGNOF_CHAR;
5936 break;
5937 case TYPE_SHORT:
5938 case TYPE_USHORT:
5939 align = REG_ALIGNOF_SHORT;
5940 break;
5941 case TYPE_INT:
5942 case TYPE_UINT:
5943 case TYPE_ENUM:
5944 align = REG_ALIGNOF_INT;
5945 break;
5946 case TYPE_LONG:
5947 case TYPE_ULONG:
5948 align = REG_ALIGNOF_LONG;
5949 break;
5950 case TYPE_POINTER:
5951 align = REG_ALIGNOF_POINTER;
5952 break;
5953 case TYPE_PRODUCT:
5954 case TYPE_OVERLAP:
5955 {
5956 size_t left_align, right_align;
5957 left_align = reg_align_of(state, type->left);
5958 right_align = reg_align_of(state, type->right);
5959 align = (left_align >= right_align) ? left_align : right_align;
5960 break;
5961 }
5962 case TYPE_ARRAY:
5963 align = reg_align_of(state, type->left);
5964 break;
5965 case TYPE_STRUCT:
5966 case TYPE_UNION:
5967 case TYPE_TUPLE:
5968 case TYPE_JOIN:
5969 align = reg_align_of(state, type->left);
5970 break;
5971 default:
5972 error(state, 0, "alignof not yet defined for type\n");
5973 break;
5974 }
5975 return align;
5976}
5977
5978static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5979{
5980 return bits_to_bytes(align_of(state, type));
5981}
5982static size_t size_of(struct compile_state *state, struct type *type);
5983static size_t reg_size_of(struct compile_state *state, struct type *type);
5984
5985static size_t needed_padding(struct compile_state *state,
5986 struct type *type, size_t offset)
5987{
5988 size_t padding, align;
5989 align = align_of(state, type);
5990 /* Align to the next machine word if the bitfield does completely
5991 * fit into the current word.
5992 */
5993 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5994 size_t size;
5995 size = size_of(state, type);
5996 if ((offset + type->elements)/size != offset/size) {
5997 align = size;
5998 }
5999 }
Eric Biederman03b59862003-06-24 14:27:37 +00006000 padding = 0;
6001 if (offset % align) {
6002 padding = align - (offset % align);
6003 }
6004 return padding;
6005}
Eric Biederman90089602004-05-28 14:11:54 +00006006
6007static size_t reg_needed_padding(struct compile_state *state,
6008 struct type *type, size_t offset)
6009{
6010 size_t padding, align;
6011 align = reg_align_of(state, type);
6012 /* Align to the next register word if the bitfield does completely
6013 * fit into the current register.
6014 */
6015 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
6016 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
6017 {
6018 align = REG_SIZEOF_REG;
6019 }
6020 padding = 0;
6021 if (offset % align) {
6022 padding = align - (offset % align);
6023 }
6024 return padding;
6025}
6026
Eric Biedermanb138ac82003-04-22 18:44:01 +00006027static size_t size_of(struct compile_state *state, struct type *type)
6028{
6029 size_t size;
6030 size = 0;
6031 switch(type->type & TYPE_MASK) {
6032 case TYPE_VOID:
6033 size = 0;
6034 break;
Eric Biederman90089602004-05-28 14:11:54 +00006035 case TYPE_BITFIELD:
6036 size = type->elements;
6037 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006038 case TYPE_CHAR:
6039 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006040 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006041 break;
6042 case TYPE_SHORT:
6043 case TYPE_USHORT:
6044 size = SIZEOF_SHORT;
6045 break;
6046 case TYPE_INT:
6047 case TYPE_UINT:
6048 case TYPE_ENUM:
6049 size = SIZEOF_INT;
6050 break;
6051 case TYPE_LONG:
6052 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006053 size = SIZEOF_LONG;
6054 break;
Eric Biederman90089602004-05-28 14:11:54 +00006055 case TYPE_POINTER:
6056 size = SIZEOF_POINTER;
6057 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006058 case TYPE_PRODUCT:
6059 {
Eric Biederman90089602004-05-28 14:11:54 +00006060 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006061 size = 0;
6062 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006063 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006064 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006065 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006066 }
Eric Biederman90089602004-05-28 14:11:54 +00006067 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006068 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006069 break;
6070 }
6071 case TYPE_OVERLAP:
6072 {
6073 size_t size_left, size_right;
6074 size_left = size_of(state, type->left);
6075 size_right = size_of(state, type->right);
6076 size = (size_left >= size_right)? size_left : size_right;
6077 break;
6078 }
6079 case TYPE_ARRAY:
6080 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6081 internal_error(state, 0, "Invalid array type");
6082 } else {
6083 size = size_of(state, type->left) * type->elements;
6084 }
6085 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006086 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006087 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006088 {
Eric Biederman90089602004-05-28 14:11:54 +00006089 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006090 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006091 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006092 pad = needed_padding(state, type, size);
6093 size = size + pad;
6094 break;
6095 }
6096 case TYPE_UNION:
6097 case TYPE_JOIN:
6098 {
6099 size_t pad;
6100 size = size_of(state, type->left);
6101 /* Pad unions so their size is a multiple of their alignment */
6102 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006103 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006104 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006105 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006106 default:
Eric Biederman90089602004-05-28 14:11:54 +00006107 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006108 break;
6109 }
6110 return size;
6111}
6112
Eric Biederman90089602004-05-28 14:11:54 +00006113static size_t reg_size_of(struct compile_state *state, struct type *type)
6114{
6115 size_t size;
6116 size = 0;
6117 switch(type->type & TYPE_MASK) {
6118 case TYPE_VOID:
6119 size = 0;
6120 break;
6121 case TYPE_BITFIELD:
6122 size = type->elements;
6123 break;
6124 case TYPE_CHAR:
6125 case TYPE_UCHAR:
6126 size = REG_SIZEOF_CHAR;
6127 break;
6128 case TYPE_SHORT:
6129 case TYPE_USHORT:
6130 size = REG_SIZEOF_SHORT;
6131 break;
6132 case TYPE_INT:
6133 case TYPE_UINT:
6134 case TYPE_ENUM:
6135 size = REG_SIZEOF_INT;
6136 break;
6137 case TYPE_LONG:
6138 case TYPE_ULONG:
6139 size = REG_SIZEOF_LONG;
6140 break;
6141 case TYPE_POINTER:
6142 size = REG_SIZEOF_POINTER;
6143 break;
6144 case TYPE_PRODUCT:
6145 {
6146 size_t pad;
6147 size = 0;
6148 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6149 pad = reg_needed_padding(state, type->left, size);
6150 size = size + pad + reg_size_of(state, type->left);
6151 type = type->right;
6152 }
6153 pad = reg_needed_padding(state, type, size);
6154 size = size + pad + reg_size_of(state, type);
6155 break;
6156 }
6157 case TYPE_OVERLAP:
6158 {
6159 size_t size_left, size_right;
6160 size_left = reg_size_of(state, type->left);
6161 size_right = reg_size_of(state, type->right);
6162 size = (size_left >= size_right)? size_left : size_right;
6163 break;
6164 }
6165 case TYPE_ARRAY:
6166 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6167 internal_error(state, 0, "Invalid array type");
6168 } else {
6169 size = reg_size_of(state, type->left) * type->elements;
6170 }
6171 break;
6172 case TYPE_STRUCT:
6173 case TYPE_TUPLE:
6174 {
6175 size_t pad;
6176 size = reg_size_of(state, type->left);
6177 /* Pad structures so their size is a multiples of their alignment */
6178 pad = reg_needed_padding(state, type, size);
6179 size = size + pad;
6180 break;
6181 }
6182 case TYPE_UNION:
6183 case TYPE_JOIN:
6184 {
6185 size_t pad;
6186 size = reg_size_of(state, type->left);
6187 /* Pad unions so their size is a multiple of their alignment */
6188 pad = reg_needed_padding(state, type, size);
6189 size = size + pad;
6190 break;
6191 }
6192 default:
6193 internal_error(state, 0, "sizeof not yet defined for type");
6194 break;
6195 }
6196 return size;
6197}
6198
6199static size_t registers_of(struct compile_state *state, struct type *type)
6200{
6201 size_t registers;
6202 registers = reg_size_of(state, type);
6203 registers += REG_SIZEOF_REG - 1;
6204 registers /= REG_SIZEOF_REG;
6205 return registers;
6206}
6207
6208static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6209{
6210 return bits_to_bytes(size_of(state, type));
6211}
6212
Eric Biederman0babc1c2003-05-09 02:39:00 +00006213static size_t field_offset(struct compile_state *state,
6214 struct type *type, struct hash_entry *field)
6215{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006216 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006217 size_t size;
6218
Eric Biederman0babc1c2003-05-09 02:39:00 +00006219 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006220 member = 0;
6221 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6222 member = type->left;
6223 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6224 size += needed_padding(state, member->left, size);
6225 if (member->left->field_ident == field) {
6226 member = member->left;
6227 break;
6228 }
6229 size += size_of(state, member->left);
6230 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006231 }
Eric Biederman90089602004-05-28 14:11:54 +00006232 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006233 }
Eric Biederman90089602004-05-28 14:11:54 +00006234 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6235 member = type->left;
6236 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6237 if (member->left->field_ident == field) {
6238 member = member->left;
6239 break;
6240 }
6241 member = member->right;
6242 }
6243 }
6244 else {
6245 internal_error(state, 0, "field_offset only works on structures and unions");
6246 }
6247
6248 if (!member || (member->field_ident != field)) {
6249 error(state, 0, "member %s not present", field->name);
6250 }
6251 return size;
6252}
6253
6254static size_t field_reg_offset(struct compile_state *state,
6255 struct type *type, struct hash_entry *field)
6256{
6257 struct type *member;
6258 size_t size;
6259
6260 size = 0;
6261 member = 0;
6262 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6263 member = type->left;
6264 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6265 size += reg_needed_padding(state, member->left, size);
6266 if (member->left->field_ident == field) {
6267 member = member->left;
6268 break;
6269 }
6270 size += reg_size_of(state, member->left);
6271 member = member->right;
6272 }
6273 }
6274 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6275 member = type->left;
6276 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6277 if (member->left->field_ident == field) {
6278 member = member->left;
6279 break;
6280 }
6281 member = member->right;
6282 }
6283 }
6284 else {
6285 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6286 }
6287
6288 size += reg_needed_padding(state, member, size);
6289 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006290 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006291 }
6292 return size;
6293}
6294
6295static struct type *field_type(struct compile_state *state,
6296 struct type *type, struct hash_entry *field)
6297{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006298 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006299
6300 member = 0;
6301 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6302 member = type->left;
6303 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6304 if (member->left->field_ident == field) {
6305 member = member->left;
6306 break;
6307 }
6308 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006309 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006310 }
Eric Biederman90089602004-05-28 14:11:54 +00006311 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6312 member = type->left;
6313 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6314 if (member->left->field_ident == field) {
6315 member = member->left;
6316 break;
6317 }
6318 member = member->right;
6319 }
6320 }
6321 else {
6322 internal_error(state, 0, "field_type only works on structures and unions");
6323 }
6324
6325 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006326 error(state, 0, "member %s not present", field->name);
6327 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006328 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006329}
6330
Eric Biederman90089602004-05-28 14:11:54 +00006331static size_t index_offset(struct compile_state *state,
6332 struct type *type, ulong_t index)
6333{
6334 struct type *member;
6335 size_t size;
6336 size = 0;
6337 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6338 size = size_of(state, type->left) * index;
6339 }
6340 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6341 ulong_t i;
6342 member = type->left;
6343 i = 0;
6344 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6345 size += needed_padding(state, member->left, size);
6346 if (i == index) {
6347 member = member->left;
6348 break;
6349 }
6350 size += size_of(state, member->left);
6351 i++;
6352 member = member->right;
6353 }
6354 size += needed_padding(state, member, size);
6355 if (i != index) {
6356 internal_error(state, 0, "Missing member index: %u", index);
6357 }
6358 }
6359 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6360 ulong_t i;
6361 size = 0;
6362 member = type->left;
6363 i = 0;
6364 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6365 if (i == index) {
6366 member = member->left;
6367 break;
6368 }
6369 i++;
6370 member = member->right;
6371 }
6372 if (i != index) {
6373 internal_error(state, 0, "Missing member index: %u", index);
6374 }
6375 }
6376 else {
6377 internal_error(state, 0,
6378 "request for index %u in something not an array, tuple or join",
6379 index);
6380 }
6381 return size;
6382}
6383
6384static size_t index_reg_offset(struct compile_state *state,
6385 struct type *type, ulong_t index)
6386{
6387 struct type *member;
6388 size_t size;
6389 size = 0;
6390 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6391 size = reg_size_of(state, type->left) * index;
6392 }
6393 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6394 ulong_t i;
6395 member = type->left;
6396 i = 0;
6397 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6398 size += reg_needed_padding(state, member->left, size);
6399 if (i == index) {
6400 member = member->left;
6401 break;
6402 }
6403 size += reg_size_of(state, member->left);
6404 i++;
6405 member = member->right;
6406 }
6407 size += reg_needed_padding(state, member, size);
6408 if (i != index) {
6409 internal_error(state, 0, "Missing member index: %u", index);
6410 }
6411
6412 }
6413 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6414 ulong_t i;
6415 size = 0;
6416 member = type->left;
6417 i = 0;
6418 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6419 if (i == index) {
6420 member = member->left;
6421 break;
6422 }
6423 i++;
6424 member = member->right;
6425 }
6426 if (i != index) {
6427 internal_error(state, 0, "Missing member index: %u", index);
6428 }
6429 }
6430 else {
6431 internal_error(state, 0,
6432 "request for index %u in something not an array, tuple or join",
6433 index);
6434 }
6435 return size;
6436}
6437
6438static struct type *index_type(struct compile_state *state,
6439 struct type *type, ulong_t index)
6440{
6441 struct type *member;
6442 if (index >= type->elements) {
6443 internal_error(state, 0, "Invalid element %u requested", index);
6444 }
6445 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6446 member = type->left;
6447 }
6448 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6449 ulong_t i;
6450 member = type->left;
6451 i = 0;
6452 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6453 if (i == index) {
6454 member = member->left;
6455 break;
6456 }
6457 i++;
6458 member = member->right;
6459 }
6460 if (i != index) {
6461 internal_error(state, 0, "Missing member index: %u", index);
6462 }
6463 }
6464 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6465 ulong_t i;
6466 member = type->left;
6467 i = 0;
6468 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6469 if (i == index) {
6470 member = member->left;
6471 break;
6472 }
6473 i++;
6474 member = member->right;
6475 }
6476 if (i != index) {
6477 internal_error(state, 0, "Missing member index: %u", index);
6478 }
6479 }
6480 else {
6481 member = 0;
6482 internal_error(state, 0,
6483 "request for index %u in something not an array, tuple or join",
6484 index);
6485 }
6486 return member;
6487}
6488
6489static struct type *unpack_type(struct compile_state *state, struct type *type)
6490{
6491 /* If I have a single register compound type not a bit-field
6492 * find the real type.
6493 */
6494 struct type *start_type;
6495 size_t size;
6496 /* Get out early if I need multiple registers for this type */
6497 size = reg_size_of(state, type);
6498 if (size > REG_SIZEOF_REG) {
6499 return type;
6500 }
6501 /* Get out early if I don't need any registers for this type */
6502 if (size == 0) {
6503 return &void_type;
6504 }
6505 /* Loop until I have no more layers I can remove */
6506 do {
6507 start_type = type;
6508 switch(type->type & TYPE_MASK) {
6509 case TYPE_ARRAY:
6510 /* If I have a single element the unpacked type
6511 * is that element.
6512 */
6513 if (type->elements == 1) {
6514 type = type->left;
6515 }
6516 break;
6517 case TYPE_STRUCT:
6518 case TYPE_TUPLE:
6519 /* If I have a single element the unpacked type
6520 * is that element.
6521 */
6522 if (type->elements == 1) {
6523 type = type->left;
6524 }
6525 /* If I have multiple elements the unpacked
6526 * type is the non-void element.
6527 */
6528 else {
6529 struct type *next, *member;
6530 struct type *sub_type;
6531 sub_type = 0;
6532 next = type->left;
6533 while(next) {
6534 member = next;
6535 next = 0;
6536 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6537 next = member->right;
6538 member = member->left;
6539 }
6540 if (reg_size_of(state, member) > 0) {
6541 if (sub_type) {
6542 internal_error(state, 0, "true compound type in a register");
6543 }
6544 sub_type = member;
6545 }
6546 }
6547 if (sub_type) {
6548 type = sub_type;
6549 }
6550 }
6551 break;
6552
6553 case TYPE_UNION:
6554 case TYPE_JOIN:
6555 /* If I have a single element the unpacked type
6556 * is that element.
6557 */
6558 if (type->elements == 1) {
6559 type = type->left;
6560 }
6561 /* I can't in general unpack union types */
6562 break;
6563 default:
6564 /* If I'm not a compound type I can't unpack it */
6565 break;
6566 }
6567 } while(start_type != type);
6568 switch(type->type & TYPE_MASK) {
6569 case TYPE_STRUCT:
6570 case TYPE_ARRAY:
6571 case TYPE_TUPLE:
6572 internal_error(state, 0, "irredicible type?");
6573 break;
6574 }
6575 return type;
6576}
6577
6578static int equiv_types(struct type *left, struct type *right);
6579static int is_compound_type(struct type *type);
6580
6581static struct type *reg_type(
6582 struct compile_state *state, struct type *type, int reg_offset)
6583{
6584 struct type *member;
6585 size_t size;
6586#if 1
6587 struct type *invalid;
6588 invalid = invalid_type(state, type);
6589 if (invalid) {
6590 fprintf(state->errout, "type: ");
6591 name_of(state->errout, type);
6592 fprintf(state->errout, "\n");
6593 fprintf(state->errout, "invalid: ");
6594 name_of(state->errout, invalid);
6595 fprintf(state->errout, "\n");
6596 internal_error(state, 0, "bad input type?");
6597 }
6598#endif
6599
6600 size = reg_size_of(state, type);
6601 if (reg_offset > size) {
6602 member = 0;
6603 fprintf(state->errout, "type: ");
6604 name_of(state->errout, type);
6605 fprintf(state->errout, "\n");
6606 internal_error(state, 0, "offset outside of type");
6607 }
6608 else {
6609 switch(type->type & TYPE_MASK) {
6610 /* Don't do anything with the basic types */
6611 case TYPE_VOID:
6612 case TYPE_CHAR: case TYPE_UCHAR:
6613 case TYPE_SHORT: case TYPE_USHORT:
6614 case TYPE_INT: case TYPE_UINT:
6615 case TYPE_LONG: case TYPE_ULONG:
6616 case TYPE_LLONG: case TYPE_ULLONG:
6617 case TYPE_FLOAT: case TYPE_DOUBLE:
6618 case TYPE_LDOUBLE:
6619 case TYPE_POINTER:
6620 case TYPE_ENUM:
6621 case TYPE_BITFIELD:
6622 member = type;
6623 break;
6624 case TYPE_ARRAY:
6625 member = type->left;
6626 size = reg_size_of(state, member);
6627 if (size > REG_SIZEOF_REG) {
6628 member = reg_type(state, member, reg_offset % size);
6629 }
6630 break;
6631 case TYPE_STRUCT:
6632 case TYPE_TUPLE:
6633 {
6634 size_t offset;
6635 offset = 0;
6636 member = type->left;
6637 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6638 size = reg_size_of(state, member->left);
6639 offset += reg_needed_padding(state, member->left, offset);
6640 if ((offset + size) > reg_offset) {
6641 member = member->left;
6642 break;
6643 }
6644 offset += size;
6645 member = member->right;
6646 }
6647 offset += reg_needed_padding(state, member, offset);
6648 member = reg_type(state, member, reg_offset - offset);
6649 break;
6650 }
6651 case TYPE_UNION:
6652 case TYPE_JOIN:
6653 {
6654 struct type *join, **jnext, *mnext;
6655 join = new_type(TYPE_JOIN, 0, 0);
6656 jnext = &join->left;
6657 mnext = type->left;
6658 while(mnext) {
6659 size_t size;
6660 member = mnext;
6661 mnext = 0;
6662 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6663 mnext = member->right;
6664 member = member->left;
6665 }
6666 size = reg_size_of(state, member);
6667 if (size > reg_offset) {
6668 struct type *part, *hunt;
6669 part = reg_type(state, member, reg_offset);
6670 /* See if this type is already in the union */
6671 hunt = join->left;
6672 while(hunt) {
6673 struct type *test = hunt;
6674 hunt = 0;
6675 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6676 hunt = test->right;
6677 test = test->left;
6678 }
6679 if (equiv_types(part, test)) {
6680 goto next;
6681 }
6682 }
6683 /* Nope add it */
6684 if (!*jnext) {
6685 *jnext = part;
6686 } else {
6687 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6688 jnext = &(*jnext)->right;
6689 }
6690 join->elements++;
6691 }
6692 next:
6693 ;
6694 }
6695 if (join->elements == 0) {
6696 internal_error(state, 0, "No elements?");
6697 }
6698 member = join;
6699 break;
6700 }
6701 default:
6702 member = 0;
6703 fprintf(state->errout, "type: ");
6704 name_of(state->errout, type);
6705 fprintf(state->errout, "\n");
6706 internal_error(state, 0, "reg_type not yet defined for type");
6707
6708 }
6709 }
6710 /* If I have a single register compound type not a bit-field
6711 * find the real type.
6712 */
6713 member = unpack_type(state, member);
6714 ;
6715 size = reg_size_of(state, member);
6716 if (size > REG_SIZEOF_REG) {
6717 internal_error(state, 0, "Cannot find type of single register");
6718 }
6719#if 1
6720 invalid = invalid_type(state, member);
6721 if (invalid) {
6722 fprintf(state->errout, "type: ");
6723 name_of(state->errout, member);
6724 fprintf(state->errout, "\n");
6725 fprintf(state->errout, "invalid: ");
6726 name_of(state->errout, invalid);
6727 fprintf(state->errout, "\n");
6728 internal_error(state, 0, "returning bad type?");
6729 }
6730#endif
6731 return member;
6732}
6733
Eric Biederman03b59862003-06-24 14:27:37 +00006734static struct type *next_field(struct compile_state *state,
6735 struct type *type, struct type *prev_member)
6736{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006737 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006738 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6739 internal_error(state, 0, "next_field only works on structures");
6740 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006741 member = type->left;
6742 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006743 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006744 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006745 break;
6746 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006747 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006748 prev_member = 0;
6749 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006750 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006751 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006752 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006753 prev_member = 0;
6754 }
6755 if (prev_member) {
6756 internal_error(state, 0, "prev_member %s not present",
6757 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006758 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006759 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006760}
6761
Eric Biederman90089602004-05-28 14:11:54 +00006762typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
6763 size_t ret_offset, size_t mem_offset, void *arg);
6764
6765static void walk_type_fields(struct compile_state *state,
6766 struct type *type, size_t reg_offset, size_t mem_offset,
6767 walk_type_fields_cb_t cb, void *arg);
6768
6769static void walk_struct_fields(struct compile_state *state,
6770 struct type *type, size_t reg_offset, size_t mem_offset,
6771 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006772{
Eric Biederman90089602004-05-28 14:11:54 +00006773 struct type *tptr;
6774 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006775 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006776 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006777 }
Eric Biederman90089602004-05-28 14:11:54 +00006778 tptr = type->left;
6779 for(i = 0; i < type->elements; i++) {
6780 struct type *mtype;
6781 mtype = tptr;
6782 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6783 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006784 }
Eric Biederman90089602004-05-28 14:11:54 +00006785 walk_type_fields(state, mtype,
6786 reg_offset +
6787 field_reg_offset(state, type, mtype->field_ident),
6788 mem_offset +
6789 field_offset(state, type, mtype->field_ident),
6790 cb, arg);
6791 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006792 }
Eric Biederman90089602004-05-28 14:11:54 +00006793
6794}
6795
6796static void walk_type_fields(struct compile_state *state,
6797 struct type *type, size_t reg_offset, size_t mem_offset,
6798 walk_type_fields_cb_t cb, void *arg)
6799{
6800 switch(type->type & TYPE_MASK) {
6801 case TYPE_STRUCT:
6802 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6803 break;
6804 case TYPE_CHAR:
6805 case TYPE_UCHAR:
6806 case TYPE_SHORT:
6807 case TYPE_USHORT:
6808 case TYPE_INT:
6809 case TYPE_UINT:
6810 case TYPE_LONG:
6811 case TYPE_ULONG:
6812 cb(state, type, reg_offset, mem_offset, arg);
6813 break;
6814 case TYPE_VOID:
6815 break;
6816 default:
6817 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006818 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006819}
6820
Eric Biedermanb138ac82003-04-22 18:44:01 +00006821static void arrays_complete(struct compile_state *state, struct type *type)
6822{
6823 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6824 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6825 error(state, 0, "array size not specified");
6826 }
6827 arrays_complete(state, type->left);
6828 }
6829}
6830
Eric Biederman90089602004-05-28 14:11:54 +00006831static unsigned int get_basic_type(struct type *type)
6832{
6833 unsigned int basic;
6834 basic = type->type & TYPE_MASK;
6835 /* Convert enums to ints */
6836 if (basic == TYPE_ENUM) {
6837 basic = TYPE_INT;
6838 }
6839 /* Convert bitfields to standard types */
6840 else if (basic == TYPE_BITFIELD) {
6841 if (type->elements <= SIZEOF_CHAR) {
6842 basic = TYPE_CHAR;
6843 }
6844 else if (type->elements <= SIZEOF_SHORT) {
6845 basic = TYPE_SHORT;
6846 }
6847 else if (type->elements <= SIZEOF_INT) {
6848 basic = TYPE_INT;
6849 }
6850 else if (type->elements <= SIZEOF_LONG) {
6851 basic = TYPE_LONG;
6852 }
6853 if (!TYPE_SIGNED(type->left->type)) {
6854 basic += 1;
6855 }
6856 }
6857 return basic;
6858}
6859
Eric Biedermanb138ac82003-04-22 18:44:01 +00006860static unsigned int do_integral_promotion(unsigned int type)
6861{
Eric Biederman83b991a2003-10-11 06:20:25 +00006862 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006863 type = TYPE_INT;
6864 }
6865 return type;
6866}
6867
6868static unsigned int do_arithmetic_conversion(
6869 unsigned int left, unsigned int right)
6870{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006871 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6872 return TYPE_LDOUBLE;
6873 }
6874 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6875 return TYPE_DOUBLE;
6876 }
6877 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6878 return TYPE_FLOAT;
6879 }
6880 left = do_integral_promotion(left);
6881 right = do_integral_promotion(right);
6882 /* If both operands have the same size done */
6883 if (left == right) {
6884 return left;
6885 }
6886 /* If both operands have the same signedness pick the larger */
6887 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6888 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6889 }
6890 /* If the signed type can hold everything use it */
6891 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6892 return left;
6893 }
6894 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6895 return right;
6896 }
6897 /* Convert to the unsigned type with the same rank as the signed type */
6898 else if (TYPE_SIGNED(left)) {
6899 return TYPE_MKUNSIGNED(left);
6900 }
6901 else {
6902 return TYPE_MKUNSIGNED(right);
6903 }
6904}
6905
6906/* see if two types are the same except for qualifiers */
6907static int equiv_types(struct type *left, struct type *right)
6908{
6909 unsigned int type;
6910 /* Error if the basic types do not match */
6911 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6912 return 0;
6913 }
6914 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006915 /* If the basic types match and it is a void type we are done */
6916 if (type == TYPE_VOID) {
6917 return 1;
6918 }
Eric Biederman90089602004-05-28 14:11:54 +00006919 /* For bitfields we need to compare the sizes */
6920 else if (type == TYPE_BITFIELD) {
6921 return (left->elements == right->elements) &&
6922 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6923 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006924 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006925 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006926 return 1;
6927 }
6928 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006929 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006930 return equiv_types(left->left, right->left);
6931 }
6932 else if (type == TYPE_ARRAY) {
6933 return (left->elements == right->elements) &&
6934 equiv_types(left->left, right->left);
6935 }
Eric Biederman90089602004-05-28 14:11:54 +00006936 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006937 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006938 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006939 }
Eric Biederman90089602004-05-28 14:11:54 +00006940 /* test for union equality */
6941 else if (type == TYPE_UNION) {
6942 return left->type_ident == right->type_ident;
6943 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006944 /* Test for equivalent functions */
6945 else if (type == TYPE_FUNCTION) {
6946 return equiv_types(left->left, right->left) &&
6947 equiv_types(left->right, right->right);
6948 }
6949 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006950 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006951 else if (type == TYPE_PRODUCT) {
6952 return equiv_types(left->left, right->left) &&
6953 equiv_types(left->right, right->right);
6954 }
Eric Biederman90089602004-05-28 14:11:54 +00006955 /* We should see TYPE_OVERLAP when comparing joins */
6956 else if (type == TYPE_OVERLAP) {
6957 return equiv_types(left->left, right->left) &&
6958 equiv_types(left->right, right->right);
6959 }
6960 /* Test for equivalence of tuples */
6961 else if (type == TYPE_TUPLE) {
6962 return (left->elements == right->elements) &&
6963 equiv_types(left->left, right->left);
6964 }
6965 /* Test for equivalence of joins */
6966 else if (type == TYPE_JOIN) {
6967 return (left->elements == right->elements) &&
6968 equiv_types(left->left, right->left);
6969 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006970 else {
6971 return 0;
6972 }
6973}
6974
6975static int equiv_ptrs(struct type *left, struct type *right)
6976{
6977 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6978 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6979 return 0;
6980 }
6981 return equiv_types(left->left, right->left);
6982}
6983
6984static struct type *compatible_types(struct type *left, struct type *right)
6985{
6986 struct type *result;
6987 unsigned int type, qual_type;
6988 /* Error if the basic types do not match */
6989 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6990 return 0;
6991 }
6992 type = left->type & TYPE_MASK;
6993 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6994 result = 0;
6995 /* if the basic types match and it is an arithmetic type we are done */
6996 if (TYPE_ARITHMETIC(type)) {
6997 result = new_type(qual_type, 0, 0);
6998 }
6999 /* If it is a pointer type recurse and keep testing */
7000 else if (type == TYPE_POINTER) {
7001 result = compatible_types(left->left, right->left);
7002 if (result) {
7003 result = new_type(qual_type, result, 0);
7004 }
7005 }
Eric Biederman90089602004-05-28 14:11:54 +00007006 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007007 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007008 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007009 result = left;
7010 }
7011 }
Eric Biederman90089602004-05-28 14:11:54 +00007012 /* test for union equality */
7013 else if (type == TYPE_UNION) {
7014 if (left->type_ident == right->type_ident) {
7015 result = left;
7016 }
7017 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007018 /* Test for equivalent functions */
7019 else if (type == TYPE_FUNCTION) {
7020 struct type *lf, *rf;
7021 lf = compatible_types(left->left, right->left);
7022 rf = compatible_types(left->right, right->right);
7023 if (lf && rf) {
7024 result = new_type(qual_type, lf, rf);
7025 }
7026 }
7027 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7028 else if (type == TYPE_PRODUCT) {
7029 struct type *lf, *rf;
7030 lf = compatible_types(left->left, right->left);
7031 rf = compatible_types(left->right, right->right);
7032 if (lf && rf) {
7033 result = new_type(qual_type, lf, rf);
7034 }
7035 }
7036 else {
7037 /* Nothing else is compatible */
7038 }
7039 return result;
7040}
7041
Eric Biederman90089602004-05-28 14:11:54 +00007042/* See if left is a equivalent to right or right is a union member of left */
7043static int is_subset_type(struct type *left, struct type *right)
7044{
7045 if (equiv_types(left, right)) {
7046 return 1;
7047 }
7048 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7049 struct type *member, *mnext;
7050 mnext = left->left;
7051 while(mnext) {
7052 member = mnext;
7053 mnext = 0;
7054 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7055 mnext = member->right;
7056 member = member->left;
7057 }
7058 if (is_subset_type( member, right)) {
7059 return 1;
7060 }
7061 }
7062 }
7063 return 0;
7064}
7065
Eric Biedermanb138ac82003-04-22 18:44:01 +00007066static struct type *compatible_ptrs(struct type *left, struct type *right)
7067{
7068 struct type *result;
7069 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7070 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7071 return 0;
7072 }
7073 result = compatible_types(left->left, right->left);
7074 if (result) {
7075 unsigned int qual_type;
7076 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7077 result = new_type(qual_type, result, 0);
7078 }
7079 return result;
7080
7081}
7082static struct triple *integral_promotion(
7083 struct compile_state *state, struct triple *def)
7084{
7085 struct type *type;
7086 type = def->type;
7087 /* As all operations are carried out in registers
7088 * the values are converted on load I just convert
7089 * logical type of the operand.
7090 */
7091 if (TYPE_INTEGER(type->type)) {
7092 unsigned int int_type;
7093 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007094 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007095 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007096 if (def->op != OP_LOAD) {
7097 def->type = new_type(int_type, 0, 0);
7098 }
7099 else {
Eric Biederman90089602004-05-28 14:11:54 +00007100 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007101 new_type(int_type, 0, 0), def, 0);
7102 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007103 }
7104 }
7105 return def;
7106}
7107
7108
7109static void arithmetic(struct compile_state *state, struct triple *def)
7110{
7111 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007112 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007113 }
7114}
7115
7116static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7117{
7118 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7119 error(state, def, "pointer or arithmetic type expected");
7120 }
7121}
7122
7123static int is_integral(struct triple *ins)
7124{
7125 return TYPE_INTEGER(ins->type->type);
7126}
7127
7128static void integral(struct compile_state *state, struct triple *def)
7129{
7130 if (!is_integral(def)) {
7131 error(state, 0, "integral type expected");
7132 }
7133}
7134
7135
7136static void bool(struct compile_state *state, struct triple *def)
7137{
7138 if (!TYPE_ARITHMETIC(def->type->type) &&
7139 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7140 error(state, 0, "arithmetic or pointer type expected");
7141 }
7142}
7143
7144static int is_signed(struct type *type)
7145{
Eric Biederman90089602004-05-28 14:11:54 +00007146 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7147 type = type->left;
7148 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007149 return !!TYPE_SIGNED(type->type);
7150}
Eric Biederman90089602004-05-28 14:11:54 +00007151static int is_compound_type(struct type *type)
7152{
7153 int is_compound;
7154 switch((type->type & TYPE_MASK)) {
7155 case TYPE_ARRAY:
7156 case TYPE_STRUCT:
7157 case TYPE_TUPLE:
7158 case TYPE_UNION:
7159 case TYPE_JOIN:
7160 is_compound = 1;
7161 break;
7162 default:
7163 is_compound = 0;
7164 break;
7165 }
7166 return is_compound;
7167}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007168
Eric Biederman0babc1c2003-05-09 02:39:00 +00007169/* Is this value located in a register otherwise it must be in memory */
7170static int is_in_reg(struct compile_state *state, struct triple *def)
7171{
7172 int in_reg;
7173 if (def->op == OP_ADECL) {
7174 in_reg = 1;
7175 }
7176 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7177 in_reg = 0;
7178 }
Eric Biederman90089602004-05-28 14:11:54 +00007179 else if (triple_is_part(state, def)) {
7180 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007181 }
7182 else {
Eric Biederman90089602004-05-28 14:11:54 +00007183 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007184 in_reg = -1;
7185 }
7186 return in_reg;
7187}
7188
Eric Biederman90089602004-05-28 14:11:54 +00007189/* Is this an auto or static variable location? Something that can
7190 * be assigned to. Otherwise it must must be a pure value, a temporary.
7191 */
7192static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007193{
7194 int ret;
7195 ret = 0;
7196 if (!def) {
7197 return 0;
7198 }
7199 if ((def->op == OP_ADECL) ||
7200 (def->op == OP_SDECL) ||
7201 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007202 (def->op == OP_BLOBCONST) ||
7203 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007204 ret = 1;
7205 }
Eric Biederman90089602004-05-28 14:11:54 +00007206 else if (triple_is_part(state, def)) {
7207 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007208 }
7209 return ret;
7210}
7211
Eric Biederman00443072003-06-24 12:34:45 +00007212static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007213{
7214 if (!def) {
7215 internal_error(state, def, "nothing where lvalue expected?");
7216 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007217 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007218 error(state, def, "lvalue expected");
7219 }
7220}
Eric Biederman00443072003-06-24 12:34:45 +00007221static void lvalue(struct compile_state *state, struct triple *def)
7222{
7223 clvalue(state, def);
7224 if (def->type->type & QUAL_CONST) {
7225 error(state, def, "modifable lvalue expected");
7226 }
7227}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007228
7229static int is_pointer(struct triple *def)
7230{
7231 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7232}
7233
7234static void pointer(struct compile_state *state, struct triple *def)
7235{
7236 if (!is_pointer(def)) {
7237 error(state, def, "pointer expected");
7238 }
7239}
7240
7241static struct triple *int_const(
7242 struct compile_state *state, struct type *type, ulong_t value)
7243{
7244 struct triple *result;
7245 switch(type->type & TYPE_MASK) {
7246 case TYPE_CHAR:
7247 case TYPE_INT: case TYPE_UINT:
7248 case TYPE_LONG: case TYPE_ULONG:
7249 break;
7250 default:
Eric Biederman90089602004-05-28 14:11:54 +00007251 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007252 }
7253 result = triple(state, OP_INTCONST, type, 0, 0);
7254 result->u.cval = value;
7255 return result;
7256}
7257
7258
Eric Biederman83b991a2003-10-11 06:20:25 +00007259static struct triple *read_expr(struct compile_state *state, struct triple *def);
7260
Eric Biederman0babc1c2003-05-09 02:39:00 +00007261static struct triple *do_mk_addr_expr(struct compile_state *state,
7262 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007263{
7264 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007265 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007266 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007267
Eric Biederman90089602004-05-28 14:11:54 +00007268 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007269
Eric Biederman90089602004-05-28 14:11:54 +00007270
Eric Biedermanb138ac82003-04-22 18:44:01 +00007271 result = 0;
7272 if (expr->op == OP_ADECL) {
7273 error(state, expr, "address of auto variables not supported");
7274 }
7275 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007276 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007277 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007278 result->u.cval = offset;
7279 }
7280 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007281 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007282 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007283 int_const(state, &ulong_type, offset));
7284 }
Eric Biederman90089602004-05-28 14:11:54 +00007285 else if (expr->op == OP_BLOBCONST) {
7286 FINISHME();
7287 internal_error(state, expr, "not yet implemented");
7288 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007289 else if (expr->op == OP_LIST) {
7290 error(state, 0, "Function addresses not supported");
7291 }
Eric Biederman90089602004-05-28 14:11:54 +00007292 else if (triple_is_part(state, expr)) {
7293 struct triple *part;
7294 part = expr;
7295 expr = MISC(expr, 0);
7296 if (part->op == OP_DOT) {
7297 offset += bits_to_bytes(
7298 field_offset(state, expr->type, part->u.field));
7299 }
7300 else if (part->op == OP_INDEX) {
7301 offset += bits_to_bytes(
7302 index_offset(state, expr->type, part->u.cval));
7303 }
7304 else {
7305 internal_error(state, part, "unhandled part type");
7306 }
7307 result = do_mk_addr_expr(state, expr, type, offset);
7308 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007309 if (!result) {
7310 internal_error(state, expr, "cannot take address of expression");
7311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007312 return result;
7313}
7314
Eric Biederman0babc1c2003-05-09 02:39:00 +00007315static struct triple *mk_addr_expr(
7316 struct compile_state *state, struct triple *expr, ulong_t offset)
7317{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007318 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007319}
7320
Eric Biedermanb138ac82003-04-22 18:44:01 +00007321static struct triple *mk_deref_expr(
7322 struct compile_state *state, struct triple *expr)
7323{
7324 struct type *base_type;
7325 pointer(state, expr);
7326 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007327 return triple(state, OP_DEREF, base_type, expr, 0);
7328}
7329
Eric Biederman90089602004-05-28 14:11:54 +00007330/* lvalue conversions always apply except when certain operators
7331 * are applied. So I apply apply it when I know no more
7332 * operators will be applied.
7333 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007334static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007335{
Eric Biederman5cd81732004-03-11 15:01:31 +00007336 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007337 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7338 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007339 type = new_type(
7340 TYPE_POINTER | (def->type->type & QUAL_MASK),
7341 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007342 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007343 struct triple *addrconst;
7344 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7345 internal_error(state, def, "bad array constant");
7346 }
7347 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7348 MISC(addrconst, 0) = def;
7349 def = addrconst;
7350 }
7351 else {
Eric Biederman90089602004-05-28 14:11:54 +00007352 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007353 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007354 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007355 /* Transform a function to a pointer to it */
7356 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7357 def = mk_addr_expr(state, def, 0);
7358 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007359 return def;
7360}
7361
Eric Biederman0babc1c2003-05-09 02:39:00 +00007362static struct triple *deref_field(
7363 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7364{
7365 struct triple *result;
7366 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007367 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007368 if (!field) {
7369 internal_error(state, 0, "No field passed to deref_field");
7370 }
7371 result = 0;
7372 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007373 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7374 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007375 error(state, 0, "request for member %s in something not a struct or union",
7376 field->name);
7377 }
Eric Biederman03b59862003-06-24 14:27:37 +00007378 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007379 if ((type->type & STOR_MASK) == STOR_PERM) {
7380 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007381 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007382 result = do_mk_addr_expr(state, expr, member, offset);
7383 result = mk_deref_expr(state, result);
7384 }
7385 else {
7386 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007387 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007388 result->u.field = field;
7389 }
7390 return result;
7391}
7392
Eric Biederman90089602004-05-28 14:11:54 +00007393static struct triple *deref_index(
7394 struct compile_state *state, struct triple *expr, size_t index)
7395{
7396 struct triple *result;
7397 struct type *type, *member;
7398 ulong_t offset;
7399
7400 result = 0;
7401 type = expr->type;
7402 member = index_type(state, type, index);
7403
7404 if ((type->type & STOR_MASK) == STOR_PERM) {
7405 offset = bits_to_bytes(index_offset(state, type, index));
7406 result = do_mk_addr_expr(state, expr, member, offset);
7407 result = mk_deref_expr(state, result);
7408 }
7409 else {
7410 result = triple(state, OP_INDEX, member, expr, 0);
7411 result->u.cval = index;
7412 }
7413 return result;
7414}
7415
Eric Biedermanb138ac82003-04-22 18:44:01 +00007416static struct triple *read_expr(struct compile_state *state, struct triple *def)
7417{
7418 int op;
7419 if (!def) {
7420 return 0;
7421 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007422#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007423#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007424#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007425 /* Transform lvalues into something we can read */
7426 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007427 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007428 return def;
7429 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007430 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007431 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007432 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007433 if (def->op == OP_SDECL) {
7434 def = mk_addr_expr(state, def, 0);
7435 def = mk_deref_expr(state, def);
7436 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007437 op = OP_LOAD;
7438 }
Eric Biederman90089602004-05-28 14:11:54 +00007439 def = triple(state, op, def->type, def, 0);
7440 if (def->type->type & QUAL_VOLATILE) {
7441 def->id |= TRIPLE_FLAG_VOLATILE;
7442 }
7443 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007444}
7445
Eric Biedermane058a1e2003-07-12 01:21:31 +00007446int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007447 struct type *dest, struct type *rval)
7448{
7449 int compatible = 0;
7450 /* Both operands have arithmetic type */
7451 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7452 compatible = 1;
7453 }
7454 /* One operand is a pointer and the other is a pointer to void */
7455 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7456 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7457 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7458 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7459 compatible = 1;
7460 }
7461 /* If both types are the same without qualifiers we are good */
7462 else if (equiv_ptrs(dest, rval)) {
7463 compatible = 1;
7464 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007465 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007466 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007467 compatible = 1;
7468 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007469 return compatible;
7470}
7471
Eric Biedermane058a1e2003-07-12 01:21:31 +00007472static void write_compatible(struct compile_state *state,
7473 struct type *dest, struct type *rval)
7474{
7475 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007476 FILE *fp = state->errout;
7477 fprintf(fp, "dest: ");
7478 name_of(fp, dest);
7479 fprintf(fp,"\nrval: ");
7480 name_of(fp, rval);
7481 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007482 error(state, 0, "Incompatible types in assignment");
7483 }
7484}
7485
Eric Biedermane058a1e2003-07-12 01:21:31 +00007486static int is_init_compatible(struct compile_state *state,
7487 struct type *dest, struct type *rval)
7488{
7489 int compatible = 0;
7490 if (is_write_compatible(state, dest, rval)) {
7491 compatible = 1;
7492 }
7493 else if (equiv_types(dest, rval)) {
7494 compatible = 1;
7495 }
7496 return compatible;
7497}
7498
Eric Biedermanb138ac82003-04-22 18:44:01 +00007499static struct triple *write_expr(
7500 struct compile_state *state, struct triple *dest, struct triple *rval)
7501{
7502 struct triple *def;
7503 int op;
7504
7505 def = 0;
7506 if (!rval) {
7507 internal_error(state, 0, "missing rval");
7508 }
7509
7510 if (rval->op == OP_LIST) {
7511 internal_error(state, 0, "expression of type OP_LIST?");
7512 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007513 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007514 internal_error(state, 0, "writing to a non lvalue?");
7515 }
Eric Biederman00443072003-06-24 12:34:45 +00007516 if (dest->type->type & QUAL_CONST) {
7517 internal_error(state, 0, "modifable lvalue expexted");
7518 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007519
7520 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007521 if (!equiv_types(dest->type, rval->type)) {
7522 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7523 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007524
7525 /* Now figure out which assignment operator to use */
7526 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007527 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007528 def = triple(state, OP_WRITE, dest->type, rval, dest);
7529 if (MISC(def, 0) != dest) {
7530 internal_error(state, def, "huh?");
7531 }
7532 if (RHS(def, 0) != rval) {
7533 internal_error(state, def, "huh?");
7534 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007535 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007536 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007537 }
Eric Biederman90089602004-05-28 14:11:54 +00007538 if (def->type->type & QUAL_VOLATILE) {
7539 def->id |= TRIPLE_FLAG_VOLATILE;
7540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007541 return def;
7542}
7543
7544static struct triple *init_expr(
7545 struct compile_state *state, struct triple *dest, struct triple *rval)
7546{
7547 struct triple *def;
7548
7549 def = 0;
7550 if (!rval) {
7551 internal_error(state, 0, "missing rval");
7552 }
7553 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7554 rval = read_expr(state, rval);
7555 def = write_expr(state, dest, rval);
7556 }
7557 else {
7558 /* Fill in the array size if necessary */
7559 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7560 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7561 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7562 dest->type->elements = rval->type->elements;
7563 }
7564 }
7565 if (!equiv_types(dest->type, rval->type)) {
7566 error(state, 0, "Incompatible types in inializer");
7567 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007568 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007569 insert_triple(state, dest, rval);
7570 rval->id |= TRIPLE_FLAG_FLATTENED;
7571 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007572 }
7573 return def;
7574}
7575
7576struct type *arithmetic_result(
7577 struct compile_state *state, struct triple *left, struct triple *right)
7578{
7579 struct type *type;
7580 /* Sanity checks to ensure I am working with arithmetic types */
7581 arithmetic(state, left);
7582 arithmetic(state, right);
7583 type = new_type(
7584 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007585 get_basic_type(left->type),
7586 get_basic_type(right->type)),
7587 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007588 return type;
7589}
7590
7591struct type *ptr_arithmetic_result(
7592 struct compile_state *state, struct triple *left, struct triple *right)
7593{
7594 struct type *type;
7595 /* Sanity checks to ensure I am working with the proper types */
7596 ptr_arithmetic(state, left);
7597 arithmetic(state, right);
7598 if (TYPE_ARITHMETIC(left->type->type) &&
7599 TYPE_ARITHMETIC(right->type->type)) {
7600 type = arithmetic_result(state, left, right);
7601 }
7602 else if (TYPE_PTR(left->type->type)) {
7603 type = left->type;
7604 }
7605 else {
7606 internal_error(state, 0, "huh?");
7607 type = 0;
7608 }
7609 return type;
7610}
7611
Eric Biedermanb138ac82003-04-22 18:44:01 +00007612/* boolean helper function */
7613
7614static struct triple *ltrue_expr(struct compile_state *state,
7615 struct triple *expr)
7616{
7617 switch(expr->op) {
7618 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7619 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7620 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7621 /* If the expression is already boolean do nothing */
7622 break;
7623 default:
7624 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7625 break;
7626 }
7627 return expr;
7628}
7629
7630static struct triple *lfalse_expr(struct compile_state *state,
7631 struct triple *expr)
7632{
7633 return triple(state, OP_LFALSE, &int_type, expr, 0);
7634}
7635
Eric Biederman90089602004-05-28 14:11:54 +00007636static struct triple *mkland_expr(
7637 struct compile_state *state,
7638 struct triple *left, struct triple *right)
7639{
7640 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007641 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007642
7643 /* Generate some intermediate triples */
7644 end = label(state);
7645 var = variable(state, &int_type);
7646
7647 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007648 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007649
7650 /* Jump if the value is false */
7651 jmp = branch(state, end,
7652 lfalse_expr(state, read_expr(state, var)));
7653 mid = label(state);
7654
7655 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007656 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007657
7658 /* An expression for the computed value */
7659 val = read_expr(state, var);
7660
7661 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007662 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007663
7664 return def;
7665}
7666
7667static struct triple *mklor_expr(
7668 struct compile_state *state,
7669 struct triple *left, struct triple *right)
7670{
7671 struct triple *def, *val, *var, *jmp, *mid, *end;
7672
7673 /* Generate some intermediate triples */
7674 end = label(state);
7675 var = variable(state, &int_type);
7676
7677 /* Store the left hand side value */
7678 left = write_expr(state, var, left);
7679
7680 /* Jump if the value is true */
7681 jmp = branch(state, end, read_expr(state, var));
7682 mid = label(state);
7683
7684 /* Store the right hand side value */
7685 right = write_expr(state, var, right);
7686
7687 /* An expression for the computed value*/
7688 val = read_expr(state, var);
7689
7690 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007691 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007692
7693 return def;
7694}
7695
7696static struct triple *mkcond_expr(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007697 struct compile_state *state,
7698 struct triple *test, struct triple *left, struct triple *right)
7699{
Eric Biederman90089602004-05-28 14:11:54 +00007700 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007701 struct type *result_type;
7702 unsigned int left_type, right_type;
7703 bool(state, test);
7704 left_type = left->type->type;
7705 right_type = right->type->type;
7706 result_type = 0;
7707 /* Both operands have arithmetic type */
7708 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7709 result_type = arithmetic_result(state, left, right);
7710 }
7711 /* Both operands have void type */
7712 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7713 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7714 result_type = &void_type;
7715 }
7716 /* pointers to the same type... */
7717 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7718 ;
7719 }
7720 /* Both operands are pointers and left is a pointer to void */
7721 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7722 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7723 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7724 result_type = right->type;
7725 }
7726 /* Both operands are pointers and right is a pointer to void */
7727 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7728 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7729 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7730 result_type = left->type;
7731 }
7732 if (!result_type) {
7733 error(state, 0, "Incompatible types in conditional expression");
7734 }
Eric Biederman90089602004-05-28 14:11:54 +00007735 /* Generate some intermediate triples */
7736 mid = label(state);
7737 end = label(state);
7738 var = variable(state, result_type);
7739
7740 /* Branch if the test is false */
7741 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7742 top = label(state);
7743
7744 /* Store the left hand side value */
7745 left = write_expr(state, var, left);
7746
7747 /* Branch to the end */
7748 jmp2 = branch(state, end, 0);
7749
7750 /* Store the right hand side value */
7751 right = write_expr(state, var, right);
7752
7753 /* An expression for the computed value */
7754 val = read_expr(state, var);
7755
7756 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007757 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007758
Eric Biedermanb138ac82003-04-22 18:44:01 +00007759 return def;
7760}
7761
7762
Eric Biederman0babc1c2003-05-09 02:39:00 +00007763static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007764{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007765#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007766#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007767#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007768 int count;
7769 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007770 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7771 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007772 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007773 else if (ins->op == OP_DEREF) {
7774 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007775 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007776 else if (ins->op == OP_VAL) {
7777 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007778 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007779 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780 /* Don't figure the depth of a call just guess it is huge */
7781 count = 1000;
7782 }
7783 else {
7784 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007785 expr = triple_rhs(state, ins, 0);
7786 for(;expr; expr = triple_rhs(state, ins, expr)) {
7787 if (*expr) {
7788 int depth;
7789 depth = expr_depth(state, *expr);
7790 if (depth > count) {
7791 count = depth;
7792 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007793 }
7794 }
7795 }
7796 return count + 1;
7797}
7798
Eric Biederman0babc1c2003-05-09 02:39:00 +00007799static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007800 struct compile_state *state, struct triple *first, struct triple *ptr,
7801 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007802{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007803 struct rhs_vector {
7804 int depth;
7805 struct triple **ins;
7806 } vector[MAX_RHS];
7807 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007808 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007809 rhs = ptr->rhs;
7810 lhs = ptr->lhs;
7811 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007812 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007813 ptr->op, tops(ptr->op));
7814 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007815 /* Find the depth of the rhs elements */
7816 for(i = 0; i < rhs; i++) {
7817 vector[i].ins = &RHS(ptr, i);
7818 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007819 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007820 /* Selection sort the rhs */
7821 for(i = 0; i < rhs; i++) {
7822 int j, max = i;
7823 for(j = i + 1; j < rhs; j++ ) {
7824 if (vector[j].depth > vector[max].depth) {
7825 max = j;
7826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007827 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007828 if (max != i) {
7829 struct rhs_vector tmp;
7830 tmp = vector[i];
7831 vector[i] = vector[max];
7832 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007833 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007834 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007835 /* Now flatten the rhs elements */
7836 for(i = 0; i < rhs; i++) {
7837 *vector[i].ins = flatten(state, first, *vector[i].ins);
7838 use_triple(*vector[i].ins, ptr);
7839 }
Eric Biederman90089602004-05-28 14:11:54 +00007840 if (lhs) {
7841 insert_triple(state, first, ptr);
7842 ptr->id |= TRIPLE_FLAG_FLATTENED;
7843 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7844
7845 /* Now flatten the lhs elements */
7846 for(i = 0; i < lhs; i++) {
7847 struct triple **ins = &LHS(ptr, i);
7848 *ins = flatten(state, first, *ins);
7849 use_triple(*ins, ptr);
7850 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007851 }
7852 return ptr;
7853}
7854
Eric Biederman90089602004-05-28 14:11:54 +00007855static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007856 struct compile_state *state, struct triple *first, struct triple *ptr)
7857{
Eric Biederman90089602004-05-28 14:11:54 +00007858 struct triple *head, *body, *val;
7859 head = RHS(ptr, 0);
7860 RHS(ptr, 0) = 0;
7861 val = head->prev;
7862 body = head->next;
7863 release_triple(state, head);
7864 release_triple(state, ptr);
7865 val->next = first;
7866 body->prev = first->prev;
7867 body->prev->next = body;
7868 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007869
Eric Biederman90089602004-05-28 14:11:54 +00007870 if (triple_is_cbranch(state, body->prev) ||
7871 triple_is_call(state, body->prev)) {
7872 unuse_triple(first, body->prev);
7873 use_triple(body, body->prev);
7874 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007875
Eric Biederman90089602004-05-28 14:11:54 +00007876 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7877 internal_error(state, val, "val not flattened?");
7878 }
7879
7880 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007881}
7882
Eric Biederman90089602004-05-28 14:11:54 +00007883
7884static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007885 struct compile_state *state, struct triple *first, struct triple *ptr)
7886{
Eric Biederman90089602004-05-28 14:11:54 +00007887 if (!triple_is_part(state, ptr)) {
7888 internal_error(state, ptr, "not a part");
7889 }
7890 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7891 internal_error(state, ptr, "unexpected args for: %d %s",
7892 ptr->op, tops(ptr->op));
7893 }
7894 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7895 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007896 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007897}
7898
7899static struct triple *flatten(
7900 struct compile_state *state, struct triple *first, struct triple *ptr)
7901{
7902 struct triple *orig_ptr;
7903 if (!ptr)
7904 return 0;
7905 do {
7906 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007907 /* Only flatten triples once */
7908 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7909 return ptr;
7910 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007911 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007912 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007913 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7914 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007915 break;
Eric Biederman90089602004-05-28 14:11:54 +00007916 case OP_PROG:
7917 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007918 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007919 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007920 ptr = flatten_generic(state, first, ptr, 1);
7921 insert_triple(state, first, ptr);
7922 ptr->id |= TRIPLE_FLAG_FLATTENED;
7923 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7924 if (ptr->next != ptr) {
7925 use_triple(ptr->next, ptr);
7926 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007927 break;
7928 case OP_READ:
7929 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007930 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7931 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007932 break;
Eric Biederman90089602004-05-28 14:11:54 +00007933 case OP_WRITE:
7934 ptr = flatten_generic(state, first, ptr, 1);
7935 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7936 use_triple(MISC(ptr, 0), ptr);
7937 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007938 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007939 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007940 break;
7941 case OP_CBRANCH:
7942 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7943 use_triple(RHS(ptr, 0), ptr);
7944 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007945 insert_triple(state, first, ptr);
7946 ptr->id |= TRIPLE_FLAG_FLATTENED;
7947 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007948 if (ptr->next != ptr) {
7949 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007950 }
7951 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007952 case OP_CALL:
7953 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7954 use_triple(MISC(ptr, 0), ptr);
7955 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007956 insert_triple(state, first, ptr);
7957 ptr->id |= TRIPLE_FLAG_FLATTENED;
7958 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007959 if (ptr->next != ptr) {
7960 use_triple(ptr->next, ptr);
7961 }
7962 break;
7963 case OP_RET:
7964 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7965 use_triple(RHS(ptr, 0), ptr);
7966 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007967 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007968 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007969 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007970 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007971 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007972 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007973 break;
7974 case OP_DEREF:
7975 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007976 ptr = RHS(ptr, 0);
7977 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007978 free_triple(state, orig_ptr);
7979 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007980 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007981 if (RHS(ptr, 0)->op == OP_DEREF) {
7982 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007983 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007984 base = MISC(ptr, 0);
7985 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007986 left = RHS(base, 0);
7987 ptr = triple(state, OP_ADD, left->type,
7988 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007989 int_const(state, &ulong_type, offset));
7990 free_triple(state, base);
7991 }
Eric Biederman90089602004-05-28 14:11:54 +00007992 else {
7993 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007995 break;
Eric Biederman90089602004-05-28 14:11:54 +00007996 case OP_INDEX:
7997 if (RHS(ptr, 0)->op == OP_DEREF) {
7998 struct triple *base, *left;
7999 ulong_t offset;
8000 base = MISC(ptr, 0);
8001 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8002 left = RHS(base, 0);
8003 ptr = triple(state, OP_ADD, left->type,
8004 read_expr(state, left),
8005 int_const(state, &long_type, offset));
8006 free_triple(state, base);
8007 }
8008 else {
8009 ptr = flatten_part(state, first, ptr);
8010 }
8011 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008012 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008013 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008014 use_triple(ptr, MISC(ptr, 0));
8015 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008016 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008017 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8018 use_triple(MISC(ptr, 0), ptr);
8019 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008020 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008021 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008022 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8023 use_triple(MISC(ptr, 0), ptr);
8024 insert_triple(state, first, ptr);
8025 ptr->id |= TRIPLE_FLAG_FLATTENED;
8026 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8027 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008028 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008029 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008030 break;
8031 default:
8032 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008033 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008034 break;
8035 }
8036 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008037 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008038 insert_triple(state, first, ptr);
8039 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008040 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008042 return ptr;
8043}
8044
8045static void release_expr(struct compile_state *state, struct triple *expr)
8046{
8047 struct triple *head;
8048 head = label(state);
8049 flatten(state, head, expr);
8050 while(head->next != head) {
8051 release_triple(state, head->next);
8052 }
8053 free_triple(state, head);
8054}
8055
8056static int replace_rhs_use(struct compile_state *state,
8057 struct triple *orig, struct triple *new, struct triple *use)
8058{
8059 struct triple **expr;
8060 int found;
8061 found = 0;
8062 expr = triple_rhs(state, use, 0);
8063 for(;expr; expr = triple_rhs(state, use, expr)) {
8064 if (*expr == orig) {
8065 *expr = new;
8066 found = 1;
8067 }
8068 }
8069 if (found) {
8070 unuse_triple(orig, use);
8071 use_triple(new, use);
8072 }
8073 return found;
8074}
8075
8076static int replace_lhs_use(struct compile_state *state,
8077 struct triple *orig, struct triple *new, struct triple *use)
8078{
8079 struct triple **expr;
8080 int found;
8081 found = 0;
8082 expr = triple_lhs(state, use, 0);
8083 for(;expr; expr = triple_lhs(state, use, expr)) {
8084 if (*expr == orig) {
8085 *expr = new;
8086 found = 1;
8087 }
8088 }
8089 if (found) {
8090 unuse_triple(orig, use);
8091 use_triple(new, use);
8092 }
8093 return found;
8094}
8095
Eric Biederman90089602004-05-28 14:11:54 +00008096static int replace_misc_use(struct compile_state *state,
8097 struct triple *orig, struct triple *new, struct triple *use)
8098{
8099 struct triple **expr;
8100 int found;
8101 found = 0;
8102 expr = triple_misc(state, use, 0);
8103 for(;expr; expr = triple_misc(state, use, expr)) {
8104 if (*expr == orig) {
8105 *expr = new;
8106 found = 1;
8107 }
8108 }
8109 if (found) {
8110 unuse_triple(orig, use);
8111 use_triple(new, use);
8112 }
8113 return found;
8114}
8115
8116static int replace_targ_use(struct compile_state *state,
8117 struct triple *orig, struct triple *new, struct triple *use)
8118{
8119 struct triple **expr;
8120 int found;
8121 found = 0;
8122 expr = triple_targ(state, use, 0);
8123 for(;expr; expr = triple_targ(state, use, expr)) {
8124 if (*expr == orig) {
8125 *expr = new;
8126 found = 1;
8127 }
8128 }
8129 if (found) {
8130 unuse_triple(orig, use);
8131 use_triple(new, use);
8132 }
8133 return found;
8134}
8135
8136static void replace_use(struct compile_state *state,
8137 struct triple *orig, struct triple *new, struct triple *use)
8138{
8139 int found;
8140 found = 0;
8141 found |= replace_rhs_use(state, orig, new, use);
8142 found |= replace_lhs_use(state, orig, new, use);
8143 found |= replace_misc_use(state, orig, new, use);
8144 found |= replace_targ_use(state, orig, new, use);
8145 if (!found) {
8146 internal_error(state, use, "use without use");
8147 }
8148}
8149
Eric Biedermanb138ac82003-04-22 18:44:01 +00008150static void propogate_use(struct compile_state *state,
8151 struct triple *orig, struct triple *new)
8152{
8153 struct triple_set *user, *next;
8154 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008155 /* Careful replace_use modifies the use chain and
8156 * removes use. So we must get a copy of the next
8157 * entry early.
8158 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008159 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008160 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008161 }
8162 if (orig->use) {
8163 internal_error(state, orig, "used after propogate_use");
8164 }
8165}
8166
8167/*
8168 * Code generators
8169 * ===========================
8170 */
8171
Eric Biederman90089602004-05-28 14:11:54 +00008172static struct triple *mk_cast_expr(
8173 struct compile_state *state, struct type *type, struct triple *expr)
8174{
8175 struct triple *def;
8176 def = read_expr(state, expr);
8177 def = triple(state, OP_CONVERT, type, def, 0);
8178 return def;
8179}
8180
Eric Biedermanb138ac82003-04-22 18:44:01 +00008181static struct triple *mk_add_expr(
8182 struct compile_state *state, struct triple *left, struct triple *right)
8183{
8184 struct type *result_type;
8185 /* Put pointer operands on the left */
8186 if (is_pointer(right)) {
8187 struct triple *tmp;
8188 tmp = left;
8189 left = right;
8190 right = tmp;
8191 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008192 left = read_expr(state, left);
8193 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008194 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008195 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008196 struct type *ptr_math;
8197 int op;
8198 if (is_signed(right->type)) {
8199 ptr_math = &long_type;
8200 op = OP_SMUL;
8201 } else {
8202 ptr_math = &ulong_type;
8203 op = OP_UMUL;
8204 }
8205 if (!equiv_types(right->type, ptr_math)) {
8206 right = mk_cast_expr(state, ptr_math, right);
8207 }
8208 right = triple(state, op, ptr_math, right,
8209 int_const(state, ptr_math,
8210 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008211 }
8212 return triple(state, OP_ADD, result_type, left, right);
8213}
8214
8215static struct triple *mk_sub_expr(
8216 struct compile_state *state, struct triple *left, struct triple *right)
8217{
8218 struct type *result_type;
8219 result_type = ptr_arithmetic_result(state, left, right);
8220 left = read_expr(state, left);
8221 right = read_expr(state, right);
8222 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008223 struct type *ptr_math;
8224 int op;
8225 if (is_signed(right->type)) {
8226 ptr_math = &long_type;
8227 op = OP_SMUL;
8228 } else {
8229 ptr_math = &ulong_type;
8230 op = OP_UMUL;
8231 }
8232 if (!equiv_types(right->type, ptr_math)) {
8233 right = mk_cast_expr(state, ptr_math, right);
8234 }
8235 right = triple(state, op, ptr_math, right,
8236 int_const(state, ptr_math,
8237 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008238 }
8239 return triple(state, OP_SUB, result_type, left, right);
8240}
8241
8242static struct triple *mk_pre_inc_expr(
8243 struct compile_state *state, struct triple *def)
8244{
8245 struct triple *val;
8246 lvalue(state, def);
8247 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8248 return triple(state, OP_VAL, def->type,
8249 write_expr(state, def, val),
8250 val);
8251}
8252
8253static struct triple *mk_pre_dec_expr(
8254 struct compile_state *state, struct triple *def)
8255{
8256 struct triple *val;
8257 lvalue(state, def);
8258 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8259 return triple(state, OP_VAL, def->type,
8260 write_expr(state, def, val),
8261 val);
8262}
8263
8264static struct triple *mk_post_inc_expr(
8265 struct compile_state *state, struct triple *def)
8266{
8267 struct triple *val;
8268 lvalue(state, def);
8269 val = read_expr(state, def);
8270 return triple(state, OP_VAL, def->type,
8271 write_expr(state, def,
8272 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8273 , val);
8274}
8275
8276static struct triple *mk_post_dec_expr(
8277 struct compile_state *state, struct triple *def)
8278{
8279 struct triple *val;
8280 lvalue(state, def);
8281 val = read_expr(state, def);
8282 return triple(state, OP_VAL, def->type,
8283 write_expr(state, def,
8284 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8285 , val);
8286}
8287
8288static struct triple *mk_subscript_expr(
8289 struct compile_state *state, struct triple *left, struct triple *right)
8290{
8291 left = read_expr(state, left);
8292 right = read_expr(state, right);
8293 if (!is_pointer(left) && !is_pointer(right)) {
8294 error(state, left, "subscripted value is not a pointer");
8295 }
8296 return mk_deref_expr(state, mk_add_expr(state, left, right));
8297}
8298
Eric Biedermane058a1e2003-07-12 01:21:31 +00008299
Eric Biedermanb138ac82003-04-22 18:44:01 +00008300/*
8301 * Compile time evaluation
8302 * ===========================
8303 */
8304static int is_const(struct triple *ins)
8305{
8306 return IS_CONST_OP(ins->op);
8307}
8308
Eric Biederman83b991a2003-10-11 06:20:25 +00008309static int is_simple_const(struct triple *ins)
8310{
Eric Biederman90089602004-05-28 14:11:54 +00008311 /* Is this a constant that u.cval has the value.
8312 * Or equivalently is this a constant that read_const
8313 * works on.
8314 * So far only OP_INTCONST qualifies.
8315 */
8316 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008317}
8318
Eric Biedermanb138ac82003-04-22 18:44:01 +00008319static int constants_equal(struct compile_state *state,
8320 struct triple *left, struct triple *right)
8321{
8322 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008323 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8324 equal = 0;
8325 }
8326 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008327 equal = 0;
8328 }
8329 else if (left->op != right->op) {
8330 equal = 0;
8331 }
8332 else if (!equiv_types(left->type, right->type)) {
8333 equal = 0;
8334 }
8335 else {
8336 equal = 0;
8337 switch(left->op) {
8338 case OP_INTCONST:
8339 if (left->u.cval == right->u.cval) {
8340 equal = 1;
8341 }
8342 break;
8343 case OP_BLOBCONST:
8344 {
Eric Biederman90089602004-05-28 14:11:54 +00008345 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008346 lsize = size_of(state, left->type);
8347 rsize = size_of(state, right->type);
8348 if (lsize != rsize) {
8349 break;
8350 }
Eric Biederman90089602004-05-28 14:11:54 +00008351 bytes = bits_to_bytes(lsize);
8352 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008353 equal = 1;
8354 }
8355 break;
8356 }
8357 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008358 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008359 (left->u.cval == right->u.cval)) {
8360 equal = 1;
8361 }
8362 break;
8363 default:
8364 internal_error(state, left, "uknown constant type");
8365 break;
8366 }
8367 }
8368 return equal;
8369}
8370
8371static int is_zero(struct triple *ins)
8372{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008373 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008374}
8375
8376static int is_one(struct triple *ins)
8377{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008378 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008379}
8380
Stefan Reinauer50542a82007-10-24 11:14:14 +00008381#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008382static long_t bit_count(ulong_t value)
8383{
8384 int count;
8385 int i;
8386 count = 0;
8387 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8388 ulong_t mask;
8389 mask = 1;
8390 mask <<= i;
8391 if (value & mask) {
8392 count++;
8393 }
8394 }
8395 return count;
8396
8397}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008398#endif
8399
Eric Biedermanb138ac82003-04-22 18:44:01 +00008400static long_t bsr(ulong_t value)
8401{
8402 int i;
8403 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8404 ulong_t mask;
8405 mask = 1;
8406 mask <<= i;
8407 if (value & mask) {
8408 return i;
8409 }
8410 }
8411 return -1;
8412}
8413
8414static long_t bsf(ulong_t value)
8415{
8416 int i;
8417 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8418 ulong_t mask;
8419 mask = 1;
8420 mask <<= 1;
8421 if (value & mask) {
8422 return i;
8423 }
8424 }
8425 return -1;
8426}
8427
Eric Biedermancb364952004-11-15 10:46:44 +00008428static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008429{
8430 return bsr(value);
8431}
8432
8433static long_t tlog2(struct triple *ins)
8434{
Eric Biedermancb364952004-11-15 10:46:44 +00008435 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008436}
8437
8438static int is_pow2(struct triple *ins)
8439{
8440 ulong_t value, mask;
8441 long_t log;
8442 if (!is_const(ins)) {
8443 return 0;
8444 }
8445 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008446 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008447 if (log == -1) {
8448 return 0;
8449 }
8450 mask = 1;
8451 mask <<= log;
8452 return ((value & mask) == value);
8453}
8454
8455static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008456 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008457{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008458 switch(rhs->type->type &TYPE_MASK) {
8459 case TYPE_CHAR:
8460 case TYPE_SHORT:
8461 case TYPE_INT:
8462 case TYPE_LONG:
8463 case TYPE_UCHAR:
8464 case TYPE_USHORT:
8465 case TYPE_UINT:
8466 case TYPE_ULONG:
8467 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008468 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008469 break;
8470 default:
Eric Biederman90089602004-05-28 14:11:54 +00008471 fprintf(state->errout, "type: ");
8472 name_of(state->errout, rhs->type);
8473 fprintf(state->errout, "\n");
8474 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008475 break;
8476 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008477 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008478 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008479 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008480 return rhs->u.cval;
8481}
8482
Eric Biederman5ade04a2003-10-22 04:03:46 +00008483static long_t read_sconst(struct compile_state *state,
8484 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008485{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008486 return (long_t)(rhs->u.cval);
8487}
8488
Eric Biederman5ade04a2003-10-22 04:03:46 +00008489int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8490{
8491 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008492 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008493 }
8494 return !is_zero(rhs);
8495}
8496
8497int const_eq(struct compile_state *state, struct triple *ins,
8498 struct triple *left, struct triple *right)
8499{
8500 int result;
8501 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008502 internal_warning(state, ins, "non const passed to const_eq");
8503 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008504 }
8505 else if (left == right) {
8506 result = 1;
8507 }
8508 else if (is_simple_const(left) && is_simple_const(right)) {
8509 ulong_t lval, rval;
8510 lval = read_const(state, ins, left);
8511 rval = read_const(state, ins, right);
8512 result = (lval == rval);
8513 }
8514 else if ((left->op == OP_ADDRCONST) &&
8515 (right->op == OP_ADDRCONST)) {
8516 result = (MISC(left, 0) == MISC(right, 0)) &&
8517 (left->u.cval == right->u.cval);
8518 }
8519 else {
Eric Biederman90089602004-05-28 14:11:54 +00008520 internal_warning(state, ins, "incomparable constants passed to const_eq");
8521 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008522 }
8523 return result;
8524
8525}
8526
8527int const_ucmp(struct compile_state *state, struct triple *ins,
8528 struct triple *left, struct triple *right)
8529{
8530 int result;
8531 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008532 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008533 result = -2;
8534 }
8535 else if (left == right) {
8536 result = 0;
8537 }
8538 else if (is_simple_const(left) && is_simple_const(right)) {
8539 ulong_t lval, rval;
8540 lval = read_const(state, ins, left);
8541 rval = read_const(state, ins, right);
8542 result = 0;
8543 if (lval > rval) {
8544 result = 1;
8545 } else if (rval > lval) {
8546 result = -1;
8547 }
8548 }
8549 else if ((left->op == OP_ADDRCONST) &&
8550 (right->op == OP_ADDRCONST) &&
8551 (MISC(left, 0) == MISC(right, 0))) {
8552 result = 0;
8553 if (left->u.cval > right->u.cval) {
8554 result = 1;
8555 } else if (left->u.cval < right->u.cval) {
8556 result = -1;
8557 }
8558 }
8559 else {
Eric Biederman90089602004-05-28 14:11:54 +00008560 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008561 result = -2;
8562 }
8563 return result;
8564}
8565
8566int const_scmp(struct compile_state *state, struct triple *ins,
8567 struct triple *left, struct triple *right)
8568{
8569 int result;
8570 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008571 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008572 result = -2;
8573 }
8574 else if (left == right) {
8575 result = 0;
8576 }
8577 else if (is_simple_const(left) && is_simple_const(right)) {
8578 long_t lval, rval;
8579 lval = read_sconst(state, ins, left);
8580 rval = read_sconst(state, ins, right);
8581 result = 0;
8582 if (lval > rval) {
8583 result = 1;
8584 } else if (rval > lval) {
8585 result = -1;
8586 }
8587 }
8588 else {
Eric Biederman90089602004-05-28 14:11:54 +00008589 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008590 result = -2;
8591 }
8592 return result;
8593}
8594
Eric Biedermanb138ac82003-04-22 18:44:01 +00008595static void unuse_rhs(struct compile_state *state, struct triple *ins)
8596{
8597 struct triple **expr;
8598 expr = triple_rhs(state, ins, 0);
8599 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008600 if (*expr) {
8601 unuse_triple(*expr, ins);
8602 *expr = 0;
8603 }
8604 }
8605}
8606
8607static void unuse_lhs(struct compile_state *state, struct triple *ins)
8608{
8609 struct triple **expr;
8610 expr = triple_lhs(state, ins, 0);
8611 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008612 unuse_triple(*expr, ins);
8613 *expr = 0;
8614 }
8615}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008616
Stefan Reinauer50542a82007-10-24 11:14:14 +00008617#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008618static void unuse_misc(struct compile_state *state, struct triple *ins)
8619{
8620 struct triple **expr;
8621 expr = triple_misc(state, ins, 0);
8622 for(;expr;expr = triple_misc(state, ins, expr)) {
8623 unuse_triple(*expr, ins);
8624 *expr = 0;
8625 }
8626}
8627
8628static void unuse_targ(struct compile_state *state, struct triple *ins)
8629{
8630 int i;
8631 struct triple **slot;
8632 slot = &TARG(ins, 0);
8633 for(i = 0; i < ins->targ; i++) {
8634 unuse_triple(slot[i], ins);
8635 slot[i] = 0;
8636 }
8637}
8638
Eric Biedermanb138ac82003-04-22 18:44:01 +00008639static void check_lhs(struct compile_state *state, struct triple *ins)
8640{
8641 struct triple **expr;
8642 expr = triple_lhs(state, ins, 0);
8643 for(;expr;expr = triple_lhs(state, ins, expr)) {
8644 internal_error(state, ins, "unexpected lhs");
8645 }
8646
8647}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008648#endif
Eric Biederman90089602004-05-28 14:11:54 +00008649
8650static void check_misc(struct compile_state *state, struct triple *ins)
8651{
8652 struct triple **expr;
8653 expr = triple_misc(state, ins, 0);
8654 for(;expr;expr = triple_misc(state, ins, expr)) {
8655 if (*expr) {
8656 internal_error(state, ins, "unexpected misc");
8657 }
8658 }
8659}
8660
Eric Biedermanb138ac82003-04-22 18:44:01 +00008661static void check_targ(struct compile_state *state, struct triple *ins)
8662{
8663 struct triple **expr;
8664 expr = triple_targ(state, ins, 0);
8665 for(;expr;expr = triple_targ(state, ins, expr)) {
8666 internal_error(state, ins, "unexpected targ");
8667 }
8668}
8669
8670static void wipe_ins(struct compile_state *state, struct triple *ins)
8671{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008672 /* Becareful which instructions you replace the wiped
8673 * instruction with, as there are not enough slots
8674 * in all instructions to hold all others.
8675 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008676 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008677 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008678 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008679 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008680 ins->lhs = 0;
8681 ins->rhs = 0;
8682 ins->misc = 0;
8683 ins->targ = 0;
8684}
8685
Stefan Reinauer50542a82007-10-24 11:14:14 +00008686#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008687static void wipe_branch(struct compile_state *state, struct triple *ins)
8688{
8689 /* Becareful which instructions you replace the wiped
8690 * instruction with, as there are not enough slots
8691 * in all instructions to hold all others.
8692 */
8693 unuse_rhs(state, ins);
8694 unuse_lhs(state, ins);
8695 unuse_misc(state, ins);
8696 unuse_targ(state, ins);
8697 ins->lhs = 0;
8698 ins->rhs = 0;
8699 ins->misc = 0;
8700 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008701}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008702#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008703
8704static void mkcopy(struct compile_state *state,
8705 struct triple *ins, struct triple *rhs)
8706{
Eric Biederman83b991a2003-10-11 06:20:25 +00008707 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008708 if (!equiv_types(ins->type, rhs->type)) {
8709 FILE *fp = state->errout;
8710 fprintf(fp, "src type: ");
8711 name_of(fp, rhs->type);
8712 fprintf(fp, "\ndst type: ");
8713 name_of(fp, ins->type);
8714 fprintf(fp, "\n");
8715 internal_error(state, ins, "mkcopy type mismatch");
8716 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008717 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008718 wipe_ins(state, ins);
8719 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008720 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008721 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008722 RHS(ins, 0) = rhs;
8723 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008724}
8725
8726static void mkconst(struct compile_state *state,
8727 struct triple *ins, ulong_t value)
8728{
8729 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008730 fprintf(state->errout, "type: ");
8731 name_of(state->errout, ins->type);
8732 fprintf(state->errout, "\n");
8733 internal_error(state, ins, "unknown type to make constant value: %ld",
8734 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008735 }
8736 wipe_ins(state, ins);
8737 ins->op = OP_INTCONST;
8738 ins->u.cval = value;
8739}
8740
8741static void mkaddr_const(struct compile_state *state,
8742 struct triple *ins, struct triple *sdecl, ulong_t value)
8743{
Eric Biederman90089602004-05-28 14:11:54 +00008744 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008745 internal_error(state, ins, "bad base for addrconst");
8746 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008747 wipe_ins(state, ins);
8748 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008749 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008750 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008751 ins->u.cval = value;
8752 use_triple(sdecl, ins);
8753}
8754
Eric Biederman90089602004-05-28 14:11:54 +00008755#if DEBUG_DECOMPOSE_PRINT_TUPLES
8756static void print_tuple(struct compile_state *state,
8757 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008758{
Eric Biederman90089602004-05-28 14:11:54 +00008759 FILE *fp = state->dbgout;
8760 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8761 name_of(fp, tuple->type);
8762 if (tuple->lhs > 0) {
8763 fprintf(fp, " lhs: ");
8764 name_of(fp, LHS(tuple, 0)->type);
8765 }
8766 fprintf(fp, "\n");
8767
8768}
8769#endif
8770
8771static struct triple *decompose_with_tuple(struct compile_state *state,
8772 struct triple *ins, struct triple *tuple)
8773{
8774 struct triple *next;
8775 next = ins->next;
8776 flatten(state, next, tuple);
8777#if DEBUG_DECOMPOSE_PRINT_TUPLES
8778 print_tuple(state, ins, tuple);
8779#endif
8780
8781 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8782 struct triple *tmp;
8783 if (tuple->lhs != 1) {
8784 internal_error(state, tuple, "plain type in multiple registers?");
8785 }
8786 tmp = LHS(tuple, 0);
8787 release_triple(state, tuple);
8788 tuple = tmp;
8789 }
8790
8791 propogate_use(state, ins, tuple);
8792 release_triple(state, ins);
8793
8794 return next;
8795}
8796
8797static struct triple *decompose_unknownval(struct compile_state *state,
8798 struct triple *ins)
8799{
8800 struct triple *tuple;
8801 ulong_t i;
8802
8803#if DEBUG_DECOMPOSE_HIRES
8804 FILE *fp = state->dbgout;
8805 fprintf(fp, "unknown type: ");
8806 name_of(fp, ins->type);
8807 fprintf(fp, "\n");
8808#endif
8809
8810 get_occurance(ins->occurance);
8811 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8812 ins->occurance);
8813
8814 for(i = 0; i < tuple->lhs; i++) {
8815 struct type *piece_type;
8816 struct triple *unknown;
8817
8818 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8819 get_occurance(tuple->occurance);
8820 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8821 tuple->occurance);
8822 LHS(tuple, i) = unknown;
8823 }
8824 return decompose_with_tuple(state, ins, tuple);
8825}
8826
8827
8828static struct triple *decompose_read(struct compile_state *state,
8829 struct triple *ins)
8830{
8831 struct triple *tuple, *lval;
8832 ulong_t i;
8833
8834 lval = RHS(ins, 0);
8835
8836 if (lval->op == OP_PIECE) {
8837 return ins->next;
8838 }
8839 get_occurance(ins->occurance);
8840 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8841 ins->occurance);
8842
8843 if ((tuple->lhs != lval->lhs) &&
8844 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
8845 {
8846 internal_error(state, ins, "lhs size inconsistency?");
8847 }
8848 for(i = 0; i < tuple->lhs; i++) {
8849 struct triple *piece, *read, *bitref;
8850 if ((i != 0) || !triple_is_def(state, lval)) {
8851 piece = LHS(lval, i);
8852 } else {
8853 piece = lval;
8854 }
8855
8856 /* See if the piece is really a bitref */
8857 bitref = 0;
8858 if (piece->op == OP_BITREF) {
8859 bitref = piece;
8860 piece = RHS(bitref, 0);
8861 }
8862
8863 get_occurance(tuple->occurance);
8864 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8865 tuple->occurance);
8866 RHS(read, 0) = piece;
8867
8868 if (bitref) {
8869 struct triple *extract;
8870 int op;
8871 if (is_signed(bitref->type->left)) {
8872 op = OP_SEXTRACT;
8873 } else {
8874 op = OP_UEXTRACT;
8875 }
8876 get_occurance(tuple->occurance);
8877 extract = alloc_triple(state, op, bitref->type, -1, -1,
8878 tuple->occurance);
8879 RHS(extract, 0) = read;
8880 extract->u.bitfield.size = bitref->u.bitfield.size;
8881 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8882
8883 read = extract;
8884 }
8885
8886 LHS(tuple, i) = read;
8887 }
8888 return decompose_with_tuple(state, ins, tuple);
8889}
8890
8891static struct triple *decompose_write(struct compile_state *state,
8892 struct triple *ins)
8893{
8894 struct triple *tuple, *lval, *val;
8895 ulong_t i;
8896
8897 lval = MISC(ins, 0);
8898 val = RHS(ins, 0);
8899 get_occurance(ins->occurance);
8900 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8901 ins->occurance);
8902
8903 if ((tuple->lhs != lval->lhs) &&
8904 (!triple_is_def(state, lval) || tuple->lhs != 1))
8905 {
8906 internal_error(state, ins, "lhs size inconsistency?");
8907 }
8908 for(i = 0; i < tuple->lhs; i++) {
8909 struct triple *piece, *write, *pval, *bitref;
8910 if ((i != 0) || !triple_is_def(state, lval)) {
8911 piece = LHS(lval, i);
8912 } else {
8913 piece = lval;
8914 }
8915 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8916 pval = val;
8917 }
8918 else {
8919 if (i > val->lhs) {
8920 internal_error(state, ins, "lhs size inconsistency?");
8921 }
8922 pval = LHS(val, i);
8923 }
8924
8925 /* See if the piece is really a bitref */
8926 bitref = 0;
8927 if (piece->op == OP_BITREF) {
8928 struct triple *read, *deposit;
8929 bitref = piece;
8930 piece = RHS(bitref, 0);
8931
8932 /* Read the destination register */
8933 get_occurance(tuple->occurance);
8934 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8935 tuple->occurance);
8936 RHS(read, 0) = piece;
8937
8938 /* Deposit the new bitfield value */
8939 get_occurance(tuple->occurance);
8940 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8941 tuple->occurance);
8942 RHS(deposit, 0) = read;
8943 RHS(deposit, 1) = pval;
8944 deposit->u.bitfield.size = bitref->u.bitfield.size;
8945 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8946
8947 /* Now write the newly generated value */
8948 pval = deposit;
8949 }
8950
8951 get_occurance(tuple->occurance);
8952 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
8953 tuple->occurance);
8954 MISC(write, 0) = piece;
8955 RHS(write, 0) = pval;
8956 LHS(tuple, i) = write;
8957 }
8958 return decompose_with_tuple(state, ins, tuple);
8959}
8960
8961struct decompose_load_info {
8962 struct occurance *occurance;
8963 struct triple *lval;
8964 struct triple *tuple;
8965};
8966static void decompose_load_cb(struct compile_state *state,
8967 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8968{
8969 struct decompose_load_info *info = arg;
8970 struct triple *load;
8971
8972 if (reg_offset > info->tuple->lhs) {
8973 internal_error(state, info->tuple, "lhs to small?");
8974 }
8975 get_occurance(info->occurance);
8976 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8977 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8978 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8979}
8980
8981static struct triple *decompose_load(struct compile_state *state,
8982 struct triple *ins)
8983{
8984 struct triple *tuple;
8985 struct decompose_load_info info;
8986
8987 if (!is_compound_type(ins->type)) {
8988 return ins->next;
8989 }
8990 get_occurance(ins->occurance);
8991 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8992 ins->occurance);
8993
8994 info.occurance = ins->occurance;
8995 info.lval = RHS(ins, 0);
8996 info.tuple = tuple;
8997 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8998
8999 return decompose_with_tuple(state, ins, tuple);
9000}
9001
9002
9003struct decompose_store_info {
9004 struct occurance *occurance;
9005 struct triple *lval;
9006 struct triple *val;
9007 struct triple *tuple;
9008};
9009static void decompose_store_cb(struct compile_state *state,
9010 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9011{
9012 struct decompose_store_info *info = arg;
9013 struct triple *store;
9014
9015 if (reg_offset > info->tuple->lhs) {
9016 internal_error(state, info->tuple, "lhs to small?");
9017 }
9018 get_occurance(info->occurance);
9019 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9020 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9021 RHS(store, 1) = LHS(info->val, reg_offset);
9022 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9023}
9024
9025static struct triple *decompose_store(struct compile_state *state,
9026 struct triple *ins)
9027{
9028 struct triple *tuple;
9029 struct decompose_store_info info;
9030
9031 if (!is_compound_type(ins->type)) {
9032 return ins->next;
9033 }
9034 get_occurance(ins->occurance);
9035 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9036 ins->occurance);
9037
9038 info.occurance = ins->occurance;
9039 info.lval = RHS(ins, 0);
9040 info.val = RHS(ins, 1);
9041 info.tuple = tuple;
9042 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9043
9044 return decompose_with_tuple(state, ins, tuple);
9045}
9046
9047static struct triple *decompose_dot(struct compile_state *state,
9048 struct triple *ins)
9049{
9050 struct triple *tuple, *lval;
9051 struct type *type;
9052 size_t reg_offset;
9053 int i, idx;
9054
9055 lval = MISC(ins, 0);
9056 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9057 idx = reg_offset/REG_SIZEOF_REG;
9058 type = field_type(state, lval->type, ins->u.field);
9059#if DEBUG_DECOMPOSE_HIRES
9060 {
9061 FILE *fp = state->dbgout;
9062 fprintf(fp, "field type: ");
9063 name_of(fp, type);
9064 fprintf(fp, "\n");
9065 }
9066#endif
9067
9068 get_occurance(ins->occurance);
9069 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9070 ins->occurance);
9071
9072 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9073 (tuple->lhs != 1))
9074 {
9075 internal_error(state, ins, "multi register bitfield?");
9076 }
9077
9078 for(i = 0; i < tuple->lhs; i++, idx++) {
9079 struct triple *piece;
9080 if (!triple_is_def(state, lval)) {
9081 if (idx > lval->lhs) {
9082 internal_error(state, ins, "inconsistent lhs count");
9083 }
9084 piece = LHS(lval, idx);
9085 } else {
9086 if (idx != 0) {
9087 internal_error(state, ins, "bad reg_offset into def");
9088 }
9089 if (i != 0) {
9090 internal_error(state, ins, "bad reg count from def");
9091 }
9092 piece = lval;
9093 }
9094
9095 /* Remember the offset of the bitfield */
9096 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9097 get_occurance(ins->occurance);
9098 piece = build_triple(state, OP_BITREF, type, piece, 0,
9099 ins->occurance);
9100 piece->u.bitfield.size = size_of(state, type);
9101 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9102 }
9103 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
9104 internal_error(state, ins,
9105 "request for a nonbitfield sub register?");
9106 }
9107
9108 LHS(tuple, i) = piece;
9109 }
9110
9111 return decompose_with_tuple(state, ins, tuple);
9112}
9113
9114static struct triple *decompose_index(struct compile_state *state,
9115 struct triple *ins)
9116{
9117 struct triple *tuple, *lval;
9118 struct type *type;
9119 int i, idx;
9120
9121 lval = MISC(ins, 0);
9122 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9123 type = index_type(state, lval->type, ins->u.cval);
9124#if DEBUG_DECOMPOSE_HIRES
9125{
9126 FILE *fp = state->dbgout;
9127 fprintf(fp, "index type: ");
9128 name_of(fp, type);
9129 fprintf(fp, "\n");
9130}
9131#endif
9132
9133 get_occurance(ins->occurance);
9134 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9135 ins->occurance);
9136
9137 for(i = 0; i < tuple->lhs; i++, idx++) {
9138 struct triple *piece;
9139 if (!triple_is_def(state, lval)) {
9140 if (idx > lval->lhs) {
9141 internal_error(state, ins, "inconsistent lhs count");
9142 }
9143 piece = LHS(lval, idx);
9144 } else {
9145 if (idx != 0) {
9146 internal_error(state, ins, "bad reg_offset into def");
9147 }
9148 if (i != 0) {
9149 internal_error(state, ins, "bad reg count from def");
9150 }
9151 piece = lval;
9152 }
9153 LHS(tuple, i) = piece;
9154 }
9155
9156 return decompose_with_tuple(state, ins, tuple);
9157}
9158
9159static void decompose_compound_types(struct compile_state *state)
9160{
9161 struct triple *ins, *next, *first;
9162 FILE *fp;
9163 fp = state->dbgout;
Eric Biederman83b991a2003-10-11 06:20:25 +00009164 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009165 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009166
9167 /* Pass one expand compound values into pseudo registers.
9168 */
9169 next = first;
9170 do {
9171 ins = next;
9172 next = ins->next;
9173 switch(ins->op) {
9174 case OP_UNKNOWNVAL:
9175 next = decompose_unknownval(state, ins);
9176 break;
9177
9178 case OP_READ:
9179 next = decompose_read(state, ins);
9180 break;
9181
9182 case OP_WRITE:
9183 next = decompose_write(state, ins);
9184 break;
9185
9186
9187 /* Be very careful with the load/store logic. These
9188 * operations must convert from the in register layout
9189 * to the in memory layout, which is nontrivial.
9190 */
9191 case OP_LOAD:
9192 next = decompose_load(state, ins);
9193 break;
9194 case OP_STORE:
9195 next = decompose_store(state, ins);
9196 break;
9197
9198 case OP_DOT:
9199 next = decompose_dot(state, ins);
9200 break;
9201 case OP_INDEX:
9202 next = decompose_index(state, ins);
9203 break;
9204
9205 }
9206#if DEBUG_DECOMPOSE_HIRES
9207 fprintf(fp, "decompose next: %p \n", next);
9208 fflush(fp);
9209 fprintf(fp, "next->op: %d %s\n",
9210 next->op, tops(next->op));
9211 /* High resolution debugging mode */
9212 print_triples(state);
9213#endif
9214 } while (next != first);
9215
9216 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009217 */
9218 ins = first;
9219 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009220 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009221 if (ins->op == OP_TUPLE) {
9222 if (ins->use) {
9223 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009224 }
Eric Biederman90089602004-05-28 14:11:54 +00009225 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009226 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009227 }
Eric Biederman90089602004-05-28 14:11:54 +00009228 }
9229 ins = next;
9230 } while(ins != first);
9231 ins = first;
9232 do {
9233 next = ins->next;
9234 if (ins->op == OP_BITREF) {
9235 if (ins->use) {
9236 internal_error(state, ins, "bitref used");
9237 }
9238 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009239 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009240 }
9241 }
9242 ins = next;
9243 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009244
Eric Biederman0babc1c2003-05-09 02:39:00 +00009245 /* Pass three verify the state and set ->id to 0.
9246 */
Eric Biederman90089602004-05-28 14:11:54 +00009247 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009248 do {
Eric Biederman90089602004-05-28 14:11:54 +00009249 ins = next;
9250 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009251 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009252 if (triple_stores_block(state, ins)) {
9253 ins->u.block = 0;
9254 }
9255 if (triple_is_def(state, ins)) {
9256 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9257 internal_error(state, ins, "multi register value remains?");
9258 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009259 }
9260 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009261 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009262 }
Eric Biederman90089602004-05-28 14:11:54 +00009263 if (ins->op == OP_INDEX) {
9264 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009265 }
Eric Biederman90089602004-05-28 14:11:54 +00009266 if (ins->op == OP_BITREF) {
9267 internal_error(state, ins, "OP_BITREF remains?");
9268 }
9269 if (ins->op == OP_TUPLE) {
9270 internal_error(state, ins, "OP_TUPLE remains?");
9271 }
9272 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009273}
9274
Eric Biedermanb138ac82003-04-22 18:44:01 +00009275/* For those operations that cannot be simplified */
9276static void simplify_noop(struct compile_state *state, struct triple *ins)
9277{
9278 return;
9279}
9280
9281static void simplify_smul(struct compile_state *state, struct triple *ins)
9282{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009283 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009284 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009285 tmp = RHS(ins, 0);
9286 RHS(ins, 0) = RHS(ins, 1);
9287 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009289 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009290 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009291 left = read_sconst(state, ins, RHS(ins, 0));
9292 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009293 mkconst(state, ins, left * right);
9294 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009295 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009296 mkconst(state, ins, 0);
9297 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 else if (is_one(RHS(ins, 1))) {
9299 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009300 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009301 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009303 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009305 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009306 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009307 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009308 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 }
9310}
9311
9312static void simplify_umul(struct compile_state *state, struct triple *ins)
9313{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009315 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009316 tmp = RHS(ins, 0);
9317 RHS(ins, 0) = RHS(ins, 1);
9318 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 }
Eric Biederman90089602004-05-28 14:11:54 +00009320 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009321 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009322 left = read_const(state, ins, RHS(ins, 0));
9323 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009324 mkconst(state, ins, left * right);
9325 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009326 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009327 mkconst(state, ins, 0);
9328 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009329 else if (is_one(RHS(ins, 1))) {
9330 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009331 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009332 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009334 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009335 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009336 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009337 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009338 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009339 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009340 }
9341}
9342
9343static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9344{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009345 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009347 left = read_sconst(state, ins, RHS(ins, 0));
9348 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009349 mkconst(state, ins, left / right);
9350 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009351 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009352 mkconst(state, ins, 0);
9353 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009354 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009355 error(state, ins, "division by zero");
9356 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009357 else if (is_one(RHS(ins, 1))) {
9358 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009359 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009360 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009362 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009364 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009365 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009366 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009367 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 }
9369}
9370
9371static void simplify_udiv(struct compile_state *state, struct triple *ins)
9372{
Eric Biederman90089602004-05-28 14:11:54 +00009373 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009374 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009375 left = read_const(state, ins, RHS(ins, 0));
9376 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 mkconst(state, ins, left / right);
9378 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009379 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009380 mkconst(state, ins, 0);
9381 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009382 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009383 error(state, ins, "division by zero");
9384 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009385 else if (is_one(RHS(ins, 1))) {
9386 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009387 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009388 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009390 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009391 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009392 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009393 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009394 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009395 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 }
9397}
9398
9399static void simplify_smod(struct compile_state *state, struct triple *ins)
9400{
Eric Biederman90089602004-05-28 14:11:54 +00009401 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009403 left = read_const(state, ins, RHS(ins, 0));
9404 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009405 mkconst(state, ins, left % right);
9406 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009407 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009408 mkconst(state, ins, 0);
9409 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009410 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009411 error(state, ins, "division by zero");
9412 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009413 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 mkconst(state, ins, 0);
9415 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009418 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009419 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009420 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009421 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009423 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009424 }
9425}
Eric Biederman83b991a2003-10-11 06:20:25 +00009426
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427static void simplify_umod(struct compile_state *state, struct triple *ins)
9428{
Eric Biederman90089602004-05-28 14:11:54 +00009429 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009431 left = read_const(state, ins, RHS(ins, 0));
9432 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 mkconst(state, ins, left % right);
9434 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009435 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009436 mkconst(state, ins, 0);
9437 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009438 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009439 error(state, ins, "division by zero");
9440 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009441 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 mkconst(state, ins, 0);
9443 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009444 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009448 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009449 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009450 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009451 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009452 }
9453}
9454
9455static void simplify_add(struct compile_state *state, struct triple *ins)
9456{
9457 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009459 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009460 tmp = RHS(ins, 0);
9461 RHS(ins, 0) = RHS(ins, 1);
9462 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009463 }
Eric Biederman90089602004-05-28 14:11:54 +00009464 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009465 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009466 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009467 left = read_const(state, ins, RHS(ins, 0));
9468 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009469 mkconst(state, ins, left + right);
9470 }
Eric Biederman530b5192003-07-01 10:05:30 +00009471 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 struct triple *sdecl;
9473 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009474 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009475 left = RHS(ins, 0)->u.cval;
9476 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009477 mkaddr_const(state, ins, sdecl, left + right);
9478 }
Eric Biederman530b5192003-07-01 10:05:30 +00009479 else {
9480 internal_warning(state, ins, "Optimize me!");
9481 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009482 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009483 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009484 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009485 tmp = RHS(ins, 1);
9486 RHS(ins, 1) = RHS(ins, 0);
9487 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009488 }
9489}
9490
9491static void simplify_sub(struct compile_state *state, struct triple *ins)
9492{
Eric Biederman90089602004-05-28 14:11:54 +00009493 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009494 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009495 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009496 left = read_const(state, ins, RHS(ins, 0));
9497 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009498 mkconst(state, ins, left - right);
9499 }
Eric Biederman530b5192003-07-01 10:05:30 +00009500 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009501 struct triple *sdecl;
9502 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009503 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009504 left = RHS(ins, 0)->u.cval;
9505 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009506 mkaddr_const(state, ins, sdecl, left - right);
9507 }
Eric Biederman530b5192003-07-01 10:05:30 +00009508 else {
9509 internal_warning(state, ins, "Optimize me!");
9510 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009511 }
9512}
9513
9514static void simplify_sl(struct compile_state *state, struct triple *ins)
9515{
Eric Biederman90089602004-05-28 14:11:54 +00009516 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009517 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009518 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009519 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009520 warning(state, ins, "left shift count >= width of type");
9521 }
9522 }
Eric Biederman90089602004-05-28 14:11:54 +00009523 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009524 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009525 left = read_const(state, ins, RHS(ins, 0));
9526 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009527 mkconst(state, ins, left << right);
9528 }
9529}
9530
9531static void simplify_usr(struct compile_state *state, struct triple *ins)
9532{
Eric Biederman90089602004-05-28 14:11:54 +00009533 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009534 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009535 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009536 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009537 warning(state, ins, "right shift count >= width of type");
9538 }
9539 }
Eric Biederman90089602004-05-28 14:11:54 +00009540 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009541 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009542 left = read_const(state, ins, RHS(ins, 0));
9543 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009544 mkconst(state, ins, left >> right);
9545 }
9546}
9547
9548static void simplify_ssr(struct compile_state *state, struct triple *ins)
9549{
Eric Biederman90089602004-05-28 14:11:54 +00009550 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009551 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009552 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009553 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009554 warning(state, ins, "right shift count >= width of type");
9555 }
9556 }
Eric Biederman90089602004-05-28 14:11:54 +00009557 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009558 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009559 left = read_sconst(state, ins, RHS(ins, 0));
9560 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009561 mkconst(state, ins, left >> right);
9562 }
9563}
9564
9565static void simplify_and(struct compile_state *state, struct triple *ins)
9566{
Eric Biederman90089602004-05-28 14:11:54 +00009567 struct triple *left, *right;
9568 left = RHS(ins, 0);
9569 right = RHS(ins, 1);
9570
9571 if (is_simple_const(left) && is_simple_const(right)) {
9572 ulong_t lval, rval;
9573 lval = read_const(state, ins, left);
9574 rval = read_const(state, ins, right);
9575 mkconst(state, ins, lval & rval);
9576 }
9577 else if (is_zero(right) || is_zero(left)) {
9578 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009579 }
9580}
9581
9582static void simplify_or(struct compile_state *state, struct triple *ins)
9583{
Eric Biederman90089602004-05-28 14:11:54 +00009584 struct triple *left, *right;
9585 left = RHS(ins, 0);
9586 right = RHS(ins, 1);
9587
9588 if (is_simple_const(left) && is_simple_const(right)) {
9589 ulong_t lval, rval;
9590 lval = read_const(state, ins, left);
9591 rval = read_const(state, ins, right);
9592 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009593 }
Eric Biederman90089602004-05-28 14:11:54 +00009594#if 0 /* I need to handle type mismatches here... */
9595 else if (is_zero(right)) {
9596 mkcopy(state, ins, left);
9597 }
9598 else if (is_zero(left)) {
9599 mkcopy(state, ins, right);
9600 }
9601#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009602}
9603
9604static void simplify_xor(struct compile_state *state, struct triple *ins)
9605{
Eric Biederman90089602004-05-28 14:11:54 +00009606 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009607 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009608 left = read_const(state, ins, RHS(ins, 0));
9609 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009610 mkconst(state, ins, left ^ right);
9611 }
9612}
9613
9614static void simplify_pos(struct compile_state *state, struct triple *ins)
9615{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009616 if (is_const(RHS(ins, 0))) {
9617 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009618 }
9619 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009620 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009621 }
9622}
9623
9624static void simplify_neg(struct compile_state *state, struct triple *ins)
9625{
Eric Biederman90089602004-05-28 14:11:54 +00009626 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009628 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009629 mkconst(state, ins, -left);
9630 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009631 else if (RHS(ins, 0)->op == OP_NEG) {
9632 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009633 }
9634}
9635
9636static void simplify_invert(struct compile_state *state, struct triple *ins)
9637{
Eric Biederman90089602004-05-28 14:11:54 +00009638 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009639 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009640 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009641 mkconst(state, ins, ~left);
9642 }
9643}
9644
9645static void simplify_eq(struct compile_state *state, struct triple *ins)
9646{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009647 struct triple *left, *right;
9648 left = RHS(ins, 0);
9649 right = RHS(ins, 1);
9650
9651 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009652 int val;
9653 val = const_eq(state, ins, left, right);
9654 if (val >= 0) {
9655 mkconst(state, ins, val == 1);
9656 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009657 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009658 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009659 mkconst(state, ins, 1);
9660 }
9661}
9662
9663static void simplify_noteq(struct compile_state *state, struct triple *ins)
9664{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009665 struct triple *left, *right;
9666 left = RHS(ins, 0);
9667 right = RHS(ins, 1);
9668
9669 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009670 int val;
9671 val = const_eq(state, ins, left, right);
9672 if (val >= 0) {
9673 mkconst(state, ins, val != 1);
9674 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009675 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009676 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009677 mkconst(state, ins, 0);
9678 }
9679}
9680
9681static void simplify_sless(struct compile_state *state, struct triple *ins)
9682{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009683 struct triple *left, *right;
9684 left = RHS(ins, 0);
9685 right = RHS(ins, 1);
9686
9687 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009688 int val;
9689 val = const_scmp(state, ins, left, right);
9690 if ((val >= -1) && (val <= 1)) {
9691 mkconst(state, ins, val < 0);
9692 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009693 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009694 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009695 mkconst(state, ins, 0);
9696 }
9697}
9698
9699static void simplify_uless(struct compile_state *state, struct triple *ins)
9700{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009701 struct triple *left, *right;
9702 left = RHS(ins, 0);
9703 right = RHS(ins, 1);
9704
9705 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009706 int val;
9707 val = const_ucmp(state, ins, left, right);
9708 if ((val >= -1) && (val <= 1)) {
9709 mkconst(state, ins, val < 0);
9710 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009711 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009712 else if (is_zero(right)) {
9713 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009714 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009715 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009716 mkconst(state, ins, 0);
9717 }
9718}
9719
9720static void simplify_smore(struct compile_state *state, struct triple *ins)
9721{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009722 struct triple *left, *right;
9723 left = RHS(ins, 0);
9724 right = RHS(ins, 1);
9725
9726 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009727 int val;
9728 val = const_scmp(state, ins, left, right);
9729 if ((val >= -1) && (val <= 1)) {
9730 mkconst(state, ins, val > 0);
9731 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009732 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009733 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009734 mkconst(state, ins, 0);
9735 }
9736}
9737
9738static void simplify_umore(struct compile_state *state, struct triple *ins)
9739{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009740 struct triple *left, *right;
9741 left = RHS(ins, 0);
9742 right = RHS(ins, 1);
9743
9744 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009745 int val;
9746 val = const_ucmp(state, ins, left, right);
9747 if ((val >= -1) && (val <= 1)) {
9748 mkconst(state, ins, val > 0);
9749 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009750 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009751 else if (is_zero(left)) {
9752 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009753 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009754 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009755 mkconst(state, ins, 0);
9756 }
9757}
9758
9759
9760static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9761{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009762 struct triple *left, *right;
9763 left = RHS(ins, 0);
9764 right = RHS(ins, 1);
9765
9766 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009767 int val;
9768 val = const_scmp(state, ins, left, right);
9769 if ((val >= -1) && (val <= 1)) {
9770 mkconst(state, ins, val <= 0);
9771 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009772 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009773 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009774 mkconst(state, ins, 1);
9775 }
9776}
9777
9778static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9779{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009780 struct triple *left, *right;
9781 left = RHS(ins, 0);
9782 right = RHS(ins, 1);
9783
9784 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009785 int val;
9786 val = const_ucmp(state, ins, left, right);
9787 if ((val >= -1) && (val <= 1)) {
9788 mkconst(state, ins, val <= 0);
9789 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009790 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009791 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009792 mkconst(state, ins, 1);
9793 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009794 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009795 mkconst(state, ins, 1);
9796 }
9797}
9798
9799static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9800{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009801 struct triple *left, *right;
9802 left = RHS(ins, 0);
9803 right = RHS(ins, 1);
9804
9805 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009806 int val;
9807 val = const_scmp(state, ins, left, right);
9808 if ((val >= -1) && (val <= 1)) {
9809 mkconst(state, ins, val >= 0);
9810 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009811 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009812 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009813 mkconst(state, ins, 1);
9814 }
9815}
9816
9817static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9818{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009819 struct triple *left, *right;
9820 left = RHS(ins, 0);
9821 right = RHS(ins, 1);
9822
9823 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009824 int val;
9825 val = const_ucmp(state, ins, left, right);
9826 if ((val >= -1) && (val <= 1)) {
9827 mkconst(state, ins, val >= 0);
9828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009829 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009830 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009831 mkconst(state, ins, 1);
9832 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009833 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009834 mkconst(state, ins, 1);
9835 }
9836}
9837
9838static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9839{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009840 struct triple *rhs;
9841 rhs = RHS(ins, 0);
9842
9843 if (is_const(rhs)) {
9844 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009845 }
9846 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009847 else if ((rhs->use) &&
9848 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009849 int need_copy = 1;
9850 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009851 switch(rhs->op) {
9852 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9853 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9854 case OP_EQ: rhs->op = OP_NOTEQ; break;
9855 case OP_NOTEQ: rhs->op = OP_EQ; break;
9856 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9857 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9858 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9859 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9860 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9861 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9862 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9863 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009864 default:
9865 need_copy = 0;
9866 break;
9867 }
9868 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009869 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009870 }
9871 }
9872}
9873
9874static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9875{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009876 struct triple *rhs;
9877 rhs = RHS(ins, 0);
9878
9879 if (is_const(rhs)) {
9880 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009881 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009882 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009883 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9884 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9885 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009886 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009887 }
9888
9889}
9890
Eric Biederman90089602004-05-28 14:11:54 +00009891static void simplify_load(struct compile_state *state, struct triple *ins)
9892{
9893 struct triple *addr, *sdecl, *blob;
9894
9895 /* If I am doing a load with a constant pointer from a constant
9896 * table get the value.
9897 */
9898 addr = RHS(ins, 0);
9899 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9900 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9901 (blob->op == OP_BLOBCONST)) {
9902 unsigned char buffer[SIZEOF_WORD];
9903 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009904 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009905 ulong_t val;
9906 reg_size = reg_size_of(state, ins->type);
9907 if (reg_size > REG_SIZEOF_REG) {
9908 internal_error(state, ins, "load size greater than register");
9909 }
9910 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009911 end = blob->u.blob;
9912 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009913 src = blob->u.blob;
9914 src += addr->u.cval;
9915
Eric Biederman7dea9552004-06-29 05:38:37 +00009916 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009917 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009918 }
9919
Eric Biederman90089602004-05-28 14:11:54 +00009920 memset(buffer, 0, sizeof(buffer));
9921 memcpy(buffer, src, bits_to_bytes(mem_size));
9922
9923 switch(mem_size) {
9924 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9925 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9926 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9927 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9928 default:
9929 internal_error(state, ins, "mem_size: %d not handled",
9930 mem_size);
9931 val = 0;
9932 break;
9933 }
9934 mkconst(state, ins, val);
9935 }
9936}
9937
9938static void simplify_uextract(struct compile_state *state, struct triple *ins)
9939{
9940 if (is_simple_const(RHS(ins, 0))) {
9941 ulong_t val;
9942 ulong_t mask;
9943 val = read_const(state, ins, RHS(ins, 0));
9944 mask = 1;
9945 mask <<= ins->u.bitfield.size;
9946 mask -= 1;
9947 val >>= ins->u.bitfield.offset;
9948 val &= mask;
9949 mkconst(state, ins, val);
9950 }
9951}
9952
9953static void simplify_sextract(struct compile_state *state, struct triple *ins)
9954{
9955 if (is_simple_const(RHS(ins, 0))) {
9956 ulong_t val;
9957 ulong_t mask;
9958 long_t sval;
9959 val = read_const(state, ins, RHS(ins, 0));
9960 mask = 1;
9961 mask <<= ins->u.bitfield.size;
9962 mask -= 1;
9963 val >>= ins->u.bitfield.offset;
9964 val &= mask;
9965 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9966 sval = val;
9967 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
9968 mkconst(state, ins, sval);
9969 }
9970}
9971
9972static void simplify_deposit(struct compile_state *state, struct triple *ins)
9973{
9974 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9975 ulong_t targ, val;
9976 ulong_t mask;
9977 targ = read_const(state, ins, RHS(ins, 0));
9978 val = read_const(state, ins, RHS(ins, 1));
9979 mask = 1;
9980 mask <<= ins->u.bitfield.size;
9981 mask -= 1;
9982 mask <<= ins->u.bitfield.offset;
9983 targ &= ~mask;
9984 val <<= ins->u.bitfield.offset;
9985 val &= mask;
9986 targ |= val;
9987 mkconst(state, ins, targ);
9988 }
9989}
9990
Eric Biedermanb138ac82003-04-22 18:44:01 +00009991static void simplify_copy(struct compile_state *state, struct triple *ins)
9992{
Eric Biederman90089602004-05-28 14:11:54 +00009993 struct triple *right;
9994 right = RHS(ins, 0);
9995 if (is_subset_type(ins->type, right->type)) {
9996 ins->type = right->type;
9997 }
9998 if (equiv_types(ins->type, right->type)) {
9999 ins->op = OP_COPY;/* I don't need to convert if the types match */
10000 } else {
10001 if (ins->op == OP_COPY) {
10002 internal_error(state, ins, "type mismatch on copy");
10003 }
10004 }
10005 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10006 struct triple *sdecl;
10007 ulong_t offset;
10008 sdecl = MISC(right, 0);
10009 offset = right->u.cval;
10010 mkaddr_const(state, ins, sdecl, offset);
10011 }
10012 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10013 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010014 case OP_INTCONST:
10015 {
10016 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010017 left = read_const(state, ins, right);
10018 /* Ensure I have not overflowed the destination. */
10019 if (size_of(state, right->type) > size_of(state, ins->type)) {
10020 ulong_t mask;
10021 mask = 1;
10022 mask <<= size_of(state, ins->type);
10023 mask -= 1;
10024 left &= mask;
10025 }
10026 /* Ensure I am properly sign extended */
10027 if (size_of(state, right->type) < size_of(state, ins->type) &&
10028 is_signed(right->type)) {
10029 long_t val;
10030 int shift;
10031 shift = SIZEOF_LONG - size_of(state, right->type);
10032 val = left;
10033 val <<= shift;
10034 val >>= shift;
10035 left = val;
10036 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010037 mkconst(state, ins, left);
10038 break;
10039 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010040 default:
10041 internal_error(state, ins, "uknown constant");
10042 break;
10043 }
10044 }
10045}
10046
Eric Biederman83b991a2003-10-11 06:20:25 +000010047static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010048{
10049 struct triple *ptr;
10050 if (!block) {
10051 return 0;
10052 }
10053 ptr = block->first;
10054 do {
10055 if (ptr->op == OP_PHI) {
10056 return 1;
10057 }
10058 ptr = ptr->next;
10059 } while(ptr != block->last);
10060 return 0;
10061}
10062
Eric Biederman83b991a2003-10-11 06:20:25 +000010063static int phi_dependency(struct block *block)
10064{
10065 /* A block has a phi dependency if a phi function
10066 * depends on that block to exist, and makes a block
10067 * that is otherwise useless unsafe to remove.
10068 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010069 if (block) {
10070 struct block_set *edge;
10071 for(edge = block->edges; edge; edge = edge->next) {
10072 if (phi_present(edge->member)) {
10073 return 1;
10074 }
10075 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010076 }
10077 return 0;
10078}
10079
10080static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10081{
10082 struct triple *targ;
10083 targ = TARG(ins, 0);
10084 /* During scc_transform temporary triples are allocated that
10085 * loop back onto themselves. If I see one don't advance the
10086 * target.
10087 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010088 while(triple_is_structural(state, targ) &&
10089 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010090 targ = targ->next;
10091 }
10092 return targ;
10093}
10094
10095
10096static void simplify_branch(struct compile_state *state, struct triple *ins)
10097{
Eric Biederman90089602004-05-28 14:11:54 +000010098 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010099 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010100 internal_error(state, ins, "not branch");
10101 }
10102 if (ins->use != 0) {
10103 internal_error(state, ins, "branch use");
10104 }
10105 /* The challenge here with simplify branch is that I need to
10106 * make modifications to the control flow graph as well
10107 * as to the branch instruction itself. That is handled
10108 * by rebuilding the basic blocks after simplify all is called.
10109 */
10110
10111 /* If we have a branch to an unconditional branch update
10112 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010113 * functions.
10114 * Also only do this a limited number of times so
10115 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010116 */
Eric Biederman90089602004-05-28 14:11:54 +000010117 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010118 do {
10119 struct triple *targ;
10120 simplified = 0;
10121 targ = branch_target(state, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010122 if ((targ != ins) && (targ->op == OP_BRANCH) &&
10123 !phi_dependency(targ->u.block))
10124 {
10125 unuse_triple(TARG(ins, 0), ins);
10126 TARG(ins, 0) = TARG(targ, 0);
10127 use_triple(TARG(ins, 0), ins);
10128 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010129 }
Eric Biederman90089602004-05-28 14:11:54 +000010130 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010131
10132 /* If we have a conditional branch with a constant condition
10133 * make it an unconditional branch.
10134 */
Eric Biederman90089602004-05-28 14:11:54 +000010135 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010136 struct triple *targ;
10137 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010138 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010139 unuse_triple(RHS(ins, 0), ins);
10140 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010141 ins->rhs = 0;
10142 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010143 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010144 if (value) {
10145 unuse_triple(ins->next, ins);
10146 TARG(ins, 0) = targ;
10147 }
10148 else {
10149 unuse_triple(targ, ins);
10150 TARG(ins, 0) = ins->next;
10151 }
10152 }
Eric Biederman90089602004-05-28 14:11:54 +000010153
10154 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010155 * make it a noop.
10156 */
10157 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010158 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010159 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010160 unuse_triple(RHS(ins, 0), ins);
10161 unuse_triple(ins->next, ins);
10162 }
Eric Biederman90089602004-05-28 14:11:54 +000010163 ins->lhs = 0;
10164 ins->rhs = 0;
10165 ins->misc = 0;
10166 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010167 ins->op = OP_NOOP;
10168 if (ins->use) {
10169 internal_error(state, ins, "noop use != 0");
10170 }
10171 }
10172}
10173
Eric Biederman530b5192003-07-01 10:05:30 +000010174static void simplify_label(struct compile_state *state, struct triple *ins)
10175{
Eric Biederman83b991a2003-10-11 06:20:25 +000010176 /* Ignore volatile labels */
10177 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010178 return;
10179 }
10180 if (ins->use == 0) {
10181 ins->op = OP_NOOP;
10182 }
10183 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010184 /* In general it is not safe to merge one label that
10185 * imediately follows another. The problem is that the empty
10186 * looking block may have phi functions that depend on it.
10187 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010188 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010189 struct triple_set *user, *next;
10190 ins->op = OP_NOOP;
10191 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010192 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010193 next = user->next;
10194 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010195 expr = triple_targ(state, use, 0);
10196 for(;expr; expr = triple_targ(state, use, expr)) {
10197 if (*expr == ins) {
10198 *expr = ins->prev;
10199 unuse_triple(ins, use);
10200 use_triple(ins->prev, use);
10201 }
10202
Eric Biederman530b5192003-07-01 10:05:30 +000010203 }
10204 }
10205 if (ins->use) {
10206 internal_error(state, ins, "noop use != 0");
10207 }
10208 }
10209 }
10210}
10211
Eric Biedermanb138ac82003-04-22 18:44:01 +000010212static void simplify_phi(struct compile_state *state, struct triple *ins)
10213{
Eric Biederman83b991a2003-10-11 06:20:25 +000010214 struct triple **slot;
10215 struct triple *value;
10216 int zrhs, i;
10217 ulong_t cvalue;
10218 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010219 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010220 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010221 return;
10222 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010223 /* See if all of the rhs members of a phi have the same value */
10224 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010225 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010226 for(i = 1; i < zrhs; i++) {
10227 if ( !slot[i] ||
10228 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010229 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010230 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010231 break;
10232 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010233 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010234 if (i == zrhs) {
10235 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010236 return;
10237 }
10238 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010239
10240 /* See if all of rhs members of a phi are the same */
10241 value = slot[0];
10242 for(i = 1; i < zrhs; i++) {
10243 if (slot[i] != value) {
10244 break;
10245 }
10246 }
10247 if (i == zrhs) {
10248 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010249 if (!is_subset_type(ins->type, value->type)) {
10250 internal_error(state, ins, "bad input type to phi");
10251 }
10252 /* Make the types match */
10253 if (!equiv_types(ins->type, value->type)) {
10254 ins->type = value->type;
10255 }
10256 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010257 mkcopy(state, ins, value);
10258 return;
10259 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010260}
10261
10262
10263static void simplify_bsf(struct compile_state *state, struct triple *ins)
10264{
Eric Biederman90089602004-05-28 14:11:54 +000010265 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010266 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010267 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010268 mkconst(state, ins, bsf(left));
10269 }
10270}
10271
10272static void simplify_bsr(struct compile_state *state, struct triple *ins)
10273{
Eric Biederman90089602004-05-28 14:11:54 +000010274 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010275 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010276 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010277 mkconst(state, ins, bsr(left));
10278 }
10279}
10280
10281
10282typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010283static const struct simplify_table {
10284 simplify_t func;
10285 unsigned long flag;
10286} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010287#define simplify_sdivt simplify_noop
10288#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010289#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010290
Eric Biederman5ade04a2003-10-22 04:03:46 +000010291[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10292[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10293[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10294[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10295[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10296[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10297[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10298[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10299[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10300[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10301[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10302[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10303[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10304[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10305[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10306[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10307[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10308[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10309[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010310
Eric Biederman5ade04a2003-10-22 04:03:46 +000010311[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10312[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10318[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10319[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10320[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10321[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10322[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010323
Eric Biederman90089602004-05-28 14:11:54 +000010324[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010325[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010326
Eric Biederman90089602004-05-28 14:11:54 +000010327[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10328[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10329[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10330
Eric Biederman5ade04a2003-10-22 04:03:46 +000010331[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010332
Eric Biederman5ade04a2003-10-22 04:03:46 +000010333[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10334[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10335[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010336[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010337
Eric Biederman5ade04a2003-10-22 04:03:46 +000010338[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10339[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10340[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010341[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010342[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10343[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010344
Eric Biederman5ade04a2003-10-22 04:03:46 +000010345[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010346[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010347
Eric Biederman5ade04a2003-10-22 04:03:46 +000010348[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10349[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10350[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10351[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10352[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10353[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10354[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10355[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10356[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010357
Eric Biederman5ade04a2003-10-22 04:03:46 +000010358[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10359[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10360[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10361[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10362[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10363[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10364[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10365[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10366[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10367[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10368[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010369};
10370
Eric Biederman90089602004-05-28 14:11:54 +000010371static inline void debug_simplify(struct compile_state *state,
10372 simplify_t do_simplify, struct triple *ins)
10373{
10374#if DEBUG_SIMPLIFY_HIRES
10375 if (state->functions_joined && (do_simplify != simplify_noop)) {
10376 /* High resolution debugging mode */
10377 fprintf(state->dbgout, "simplifing: ");
10378 display_triple(state->dbgout, ins);
10379 }
10380#endif
10381 do_simplify(state, ins);
10382#if DEBUG_SIMPLIFY_HIRES
10383 if (state->functions_joined && (do_simplify != simplify_noop)) {
10384 /* High resolution debugging mode */
10385 fprintf(state->dbgout, "simplified: ");
10386 display_triple(state->dbgout, ins);
10387 }
10388#endif
10389}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010390static void simplify(struct compile_state *state, struct triple *ins)
10391{
10392 int op;
10393 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010394 if (ins == &unknown_triple) {
10395 internal_error(state, ins, "simplifying the unknown triple?");
10396 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010397 do {
10398 op = ins->op;
10399 do_simplify = 0;
10400 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10401 do_simplify = 0;
10402 }
Eric Biederman90089602004-05-28 14:11:54 +000010403 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010404 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010405 }
Eric Biederman90089602004-05-28 14:11:54 +000010406 if (do_simplify &&
10407 !(state->compiler->flags & table_simplify[op].flag)) {
10408 do_simplify = simplify_noop;
10409 }
10410 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010411 do_simplify = simplify_noop;
10412 }
10413
Eric Biedermanb138ac82003-04-22 18:44:01 +000010414 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010415 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010416 op, tops(op));
10417 return;
10418 }
Eric Biederman90089602004-05-28 14:11:54 +000010419 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010420 } while(ins->op != op);
10421}
10422
Eric Biederman5ade04a2003-10-22 04:03:46 +000010423static void rebuild_ssa_form(struct compile_state *state);
10424
Eric Biedermanb138ac82003-04-22 18:44:01 +000010425static void simplify_all(struct compile_state *state)
10426{
10427 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010428 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10429 return;
10430 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010431 first = state->first;
10432 ins = first->prev;
10433 do {
10434 simplify(state, ins);
10435 ins = ins->prev;
10436 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010437 ins = first;
10438 do {
10439 simplify(state, ins);
10440 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010441 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010442 rebuild_ssa_form(state);
10443
Eric Biederman90089602004-05-28 14:11:54 +000010444 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010445}
10446
10447/*
10448 * Builtins....
10449 * ============================
10450 */
10451
Eric Biederman0babc1c2003-05-09 02:39:00 +000010452static void register_builtin_function(struct compile_state *state,
10453 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454{
Eric Biederman90089602004-05-28 14:11:54 +000010455 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010456 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010457 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010458 struct file_state file;
10459 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010460 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010461 va_list args;
10462 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010463
10464 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010465 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010466 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010467 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010468 file.report_line = 1;
10469 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010470 file.prev = state->file;
10471 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010472 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010473
10474 /* Find the Parameter count */
10475 valid_op(state, op);
10476 parameters = table_ops[op].rhs;
10477 if (parameters < 0 ) {
10478 internal_error(state, 0, "Invalid builtin parameter count");
10479 }
10480
10481 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010482 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010483 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010484 next = &ftype->right;
10485 va_start(args, rtype);
10486 for(i = 0; i < parameters; i++) {
10487 atype = va_arg(args, struct type *);
10488 if (!*next) {
10489 *next = atype;
10490 } else {
10491 *next = new_type(TYPE_PRODUCT, *next, atype);
10492 next = &((*next)->right);
10493 }
10494 }
10495 if (!*next) {
10496 *next = &void_type;
10497 }
10498 va_end(args);
10499
Eric Biederman90089602004-05-28 14:11:54 +000010500 /* Get the initial closure type */
10501 ctype = new_type(TYPE_JOIN, &void_type, 0);
10502 ctype->elements = 1;
10503
10504 /* Get the return type */
10505 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10506 crtype->elements = 2;
10507
Eric Biedermanb138ac82003-04-22 18:44:01 +000010508 /* Generate the needed triples */
10509 def = triple(state, OP_LIST, ftype, 0, 0);
10510 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010511 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010512 result = flatten(state, first, variable(state, crtype));
10513 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010514 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010515
10516 /* Now string them together */
10517 param = ftype->right;
10518 for(i = 0; i < parameters; i++) {
10519 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10520 atype = param->left;
10521 } else {
10522 atype = param;
10523 }
10524 arg = flatten(state, first, variable(state, atype));
10525 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010526 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010527 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010528 generate_lhs_pieces(state, work);
10529 for(i = 0; i < parameters; i++) {
10530 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010531 }
Eric Biederman90089602004-05-28 14:11:54 +000010532 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10533 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010534 }
10535 work = flatten(state, first, work);
10536 last = flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010537 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010538 name_len = strlen(name);
10539 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010540 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010541 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010542
Eric Biedermanb138ac82003-04-22 18:44:01 +000010543 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010544 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010545 state->main_function = 0;
10546
Eric Biederman5ade04a2003-10-22 04:03:46 +000010547 if (!state->functions) {
10548 state->functions = def;
10549 } else {
10550 insert_triple(state, state->functions, def);
10551 }
10552 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010553 FILE *fp = state->dbgout;
10554 fprintf(fp, "\n");
10555 loc(fp, state, 0);
10556 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10557 display_func(state, fp, def);
10558 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010559 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010560}
10561
Eric Biederman0babc1c2003-05-09 02:39:00 +000010562static struct type *partial_struct(struct compile_state *state,
10563 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010564{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010565 struct hash_entry *field_ident;
10566 struct type *result;
10567 int field_name_len;
10568
10569 field_name_len = strlen(field_name);
10570 field_ident = lookup(state, field_name, field_name_len);
10571
10572 result = clone_type(0, type);
10573 result->field_ident = field_ident;
10574
10575 if (rest) {
10576 result = new_type(TYPE_PRODUCT, result, rest);
10577 }
10578 return result;
10579}
10580
10581static struct type *register_builtin_type(struct compile_state *state,
10582 const char *name, struct type *type)
10583{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010584 struct hash_entry *ident;
10585 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010586
Eric Biedermanb138ac82003-04-22 18:44:01 +000010587 name_len = strlen(name);
10588 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010589
10590 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10591 ulong_t elements = 0;
10592 struct type *field;
10593 type = new_type(TYPE_STRUCT, type, 0);
10594 field = type->left;
10595 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10596 elements++;
10597 field = field->right;
10598 }
10599 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010600 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010601 type->type_ident = ident;
10602 type->elements = elements;
10603 }
10604 symbol(state, ident, &ident->sym_ident, 0, type);
10605 ident->tok = TOK_TYPE_NAME;
10606 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010607}
10608
Eric Biederman0babc1c2003-05-09 02:39:00 +000010609
Eric Biedermanb138ac82003-04-22 18:44:01 +000010610static void register_builtins(struct compile_state *state)
10611{
Eric Biederman530b5192003-07-01 10:05:30 +000010612 struct type *div_type, *ldiv_type;
10613 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010614 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010615
Eric Biederman530b5192003-07-01 10:05:30 +000010616 div_type = register_builtin_type(state, "__builtin_div_t",
10617 partial_struct(state, "quot", &int_type,
10618 partial_struct(state, "rem", &int_type, 0)));
10619 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10620 partial_struct(state, "quot", &long_type,
10621 partial_struct(state, "rem", &long_type, 0)));
10622 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10623 partial_struct(state, "quot", &uint_type,
10624 partial_struct(state, "rem", &uint_type, 0)));
10625 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10626 partial_struct(state, "quot", &ulong_type,
10627 partial_struct(state, "rem", &ulong_type, 0)));
10628
10629 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10630 &int_type, &int_type);
10631 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10632 &long_type, &long_type);
10633 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10634 &uint_type, &uint_type);
10635 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10636 &ulong_type, &ulong_type);
10637
Eric Biederman0babc1c2003-05-09 02:39:00 +000010638 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
10639 &ushort_type);
10640 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10641 &ushort_type);
10642 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
10643 &ushort_type);
10644
10645 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
10646 &uchar_type, &ushort_type);
10647 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
10648 &ushort_type, &ushort_type);
10649 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
10650 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010651
Eric Biederman0babc1c2003-05-09 02:39:00 +000010652 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
10653 &int_type);
10654 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
10655 &int_type);
10656
10657 msr_type = register_builtin_type(state, "__builtin_msr_t",
10658 partial_struct(state, "lo", &ulong_type,
10659 partial_struct(state, "hi", &ulong_type, 0)));
10660
10661 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10662 &ulong_type);
10663 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10664 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010665
Eric Biederman0babc1c2003-05-09 02:39:00 +000010666 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10667 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010668}
10669
10670static struct type *declarator(
10671 struct compile_state *state, struct type *type,
10672 struct hash_entry **ident, int need_ident);
10673static void decl(struct compile_state *state, struct triple *first);
10674static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010675#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010676static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010677#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010678static struct type *decl_specifiers(struct compile_state *state);
10679static int istype(int tok);
10680static struct triple *expr(struct compile_state *state);
10681static struct triple *assignment_expr(struct compile_state *state);
10682static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010683static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010684
10685static struct triple *call_expr(
10686 struct compile_state *state, struct triple *func)
10687{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010688 struct triple *def;
10689 struct type *param, *type;
10690 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010691
10692 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10693 error(state, 0, "Called object is not a function");
10694 }
10695 if (func->op != OP_LIST) {
10696 internal_error(state, 0, "improper function");
10697 }
10698 eat(state, TOK_LPAREN);
10699 /* Find the return type without any specifiers */
10700 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010701 /* Count the number of rhs entries for OP_FCALL */
10702 param = func->type->right;
10703 pvals = 0;
10704 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10705 pvals++;
10706 param = param->right;
10707 }
10708 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10709 pvals++;
10710 }
10711 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010712 MISC(def, 0) = func;
10713
10714 param = func->type->right;
10715 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010716 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010717 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010718 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010719 arg_type = param;
10720 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10721 arg_type = param->left;
10722 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010723 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010724 RHS(def, index) = val;
10725 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010726 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010727 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010728 }
10729 }
10730 eat(state, TOK_RPAREN);
10731 return def;
10732}
10733
10734
10735static struct triple *character_constant(struct compile_state *state)
10736{
10737 struct triple *def;
10738 struct token *tk;
10739 const signed char *str, *end;
10740 int c;
10741 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010742 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010743 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010744 str_len = tk->str_len - 2;
10745 if (str_len <= 0) {
10746 error(state, 0, "empty character constant");
10747 }
10748 end = str + str_len;
10749 c = char_value(state, &str, end);
10750 if (str != end) {
10751 error(state, 0, "multibyte character constant not supported");
10752 }
10753 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10754 return def;
10755}
10756
10757static struct triple *string_constant(struct compile_state *state)
10758{
10759 struct triple *def;
10760 struct token *tk;
10761 struct type *type;
10762 const signed char *str, *end;
10763 signed char *buf, *ptr;
10764 int str_len;
10765
10766 buf = 0;
10767 type = new_type(TYPE_ARRAY, &char_type, 0);
10768 type->elements = 0;
10769 /* The while loop handles string concatenation */
10770 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010771 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010772 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010773 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010774 if (str_len < 0) {
10775 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010776 }
10777 end = str + str_len;
10778 ptr = buf;
10779 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10780 memcpy(buf, ptr, type->elements);
10781 ptr = buf + type->elements;
10782 do {
10783 *ptr++ = char_value(state, &str, end);
10784 } while(str < end);
10785 type->elements = ptr - buf;
10786 } while(peek(state) == TOK_LIT_STRING);
10787 *ptr = '\0';
10788 type->elements += 1;
10789 def = triple(state, OP_BLOBCONST, type, 0, 0);
10790 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010791
Eric Biedermanb138ac82003-04-22 18:44:01 +000010792 return def;
10793}
10794
10795
10796static struct triple *integer_constant(struct compile_state *state)
10797{
10798 struct triple *def;
10799 unsigned long val;
10800 struct token *tk;
10801 char *end;
10802 int u, l, decimal;
10803 struct type *type;
10804
Eric Biederman41203d92004-11-08 09:31:09 +000010805 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010806 errno = 0;
10807 decimal = (tk->val.str[0] != '0');
10808 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010809 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010810 error(state, 0, "Integer constant to large");
10811 }
10812 u = l = 0;
10813 if ((*end == 'u') || (*end == 'U')) {
10814 u = 1;
10815 end++;
10816 }
10817 if ((*end == 'l') || (*end == 'L')) {
10818 l = 1;
10819 end++;
10820 }
10821 if ((*end == 'u') || (*end == 'U')) {
10822 u = 1;
10823 end++;
10824 }
10825 if (*end) {
10826 error(state, 0, "Junk at end of integer constant");
10827 }
10828 if (u && l) {
10829 type = &ulong_type;
10830 }
10831 else if (l) {
10832 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010833 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010834 type = &ulong_type;
10835 }
10836 }
10837 else if (u) {
10838 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010839 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010840 type = &ulong_type;
10841 }
10842 }
10843 else {
10844 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010845 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010846 type = &uint_type;
10847 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010848 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010849 type = &ulong_type;
10850 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010851 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010852 type = &long_type;
10853 }
10854 }
10855 def = int_const(state, type, val);
10856 return def;
10857}
10858
10859static struct triple *primary_expr(struct compile_state *state)
10860{
10861 struct triple *def;
10862 int tok;
10863 tok = peek(state);
10864 switch(tok) {
10865 case TOK_IDENT:
10866 {
10867 struct hash_entry *ident;
10868 /* Here ident is either:
10869 * a varable name
10870 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010871 */
Eric Biederman41203d92004-11-08 09:31:09 +000010872 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010873 if (!ident->sym_ident) {
10874 error(state, 0, "%s undeclared", ident->name);
10875 }
10876 def = ident->sym_ident->def;
10877 break;
10878 }
10879 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010880 {
10881 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010882 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010883 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010884 if (!ident->sym_ident) {
10885 error(state, 0, "%s undeclared", ident->name);
10886 }
10887 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010888 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010889 }
Eric Biederman41203d92004-11-08 09:31:09 +000010890 case TOK_MIDENT:
10891 {
10892 struct hash_entry *ident;
10893 ident = eat(state, TOK_MIDENT)->ident;
10894 warning(state, 0, "Replacing undefined macro: %s with 0",
10895 ident->name);
10896 def = int_const(state, &int_type, 0);
10897 break;
10898 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010899 case TOK_LPAREN:
10900 eat(state, TOK_LPAREN);
10901 def = expr(state);
10902 eat(state, TOK_RPAREN);
10903 break;
10904 case TOK_LIT_INT:
10905 def = integer_constant(state);
10906 break;
10907 case TOK_LIT_FLOAT:
10908 eat(state, TOK_LIT_FLOAT);
10909 error(state, 0, "Floating point constants not supported");
10910 def = 0;
10911 FINISHME();
10912 break;
10913 case TOK_LIT_CHAR:
10914 def = character_constant(state);
10915 break;
10916 case TOK_LIT_STRING:
10917 def = string_constant(state);
10918 break;
10919 default:
10920 def = 0;
10921 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10922 }
10923 return def;
10924}
10925
10926static struct triple *postfix_expr(struct compile_state *state)
10927{
10928 struct triple *def;
10929 int postfix;
10930 def = primary_expr(state);
10931 do {
10932 struct triple *left;
10933 int tok;
10934 postfix = 1;
10935 left = def;
10936 switch((tok = peek(state))) {
10937 case TOK_LBRACKET:
10938 eat(state, TOK_LBRACKET);
10939 def = mk_subscript_expr(state, left, expr(state));
10940 eat(state, TOK_RBRACKET);
10941 break;
10942 case TOK_LPAREN:
10943 def = call_expr(state, def);
10944 break;
10945 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010946 {
10947 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010948 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010949 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010950 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010951 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010952 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010953 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010954 {
10955 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010956 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010957 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010958 def = mk_deref_expr(state, read_expr(state, def));
10959 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010960 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010961 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010962 case TOK_PLUSPLUS:
10963 eat(state, TOK_PLUSPLUS);
10964 def = mk_post_inc_expr(state, left);
10965 break;
10966 case TOK_MINUSMINUS:
10967 eat(state, TOK_MINUSMINUS);
10968 def = mk_post_dec_expr(state, left);
10969 break;
10970 default:
10971 postfix = 0;
10972 break;
10973 }
10974 } while(postfix);
10975 return def;
10976}
10977
10978static struct triple *cast_expr(struct compile_state *state);
10979
10980static struct triple *unary_expr(struct compile_state *state)
10981{
10982 struct triple *def, *right;
10983 int tok;
10984 switch((tok = peek(state))) {
10985 case TOK_PLUSPLUS:
10986 eat(state, TOK_PLUSPLUS);
10987 def = mk_pre_inc_expr(state, unary_expr(state));
10988 break;
10989 case TOK_MINUSMINUS:
10990 eat(state, TOK_MINUSMINUS);
10991 def = mk_pre_dec_expr(state, unary_expr(state));
10992 break;
10993 case TOK_AND:
10994 eat(state, TOK_AND);
10995 def = mk_addr_expr(state, cast_expr(state), 0);
10996 break;
10997 case TOK_STAR:
10998 eat(state, TOK_STAR);
10999 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11000 break;
11001 case TOK_PLUS:
11002 eat(state, TOK_PLUS);
11003 right = read_expr(state, cast_expr(state));
11004 arithmetic(state, right);
11005 def = integral_promotion(state, right);
11006 break;
11007 case TOK_MINUS:
11008 eat(state, TOK_MINUS);
11009 right = read_expr(state, cast_expr(state));
11010 arithmetic(state, right);
11011 def = integral_promotion(state, right);
11012 def = triple(state, OP_NEG, def->type, def, 0);
11013 break;
11014 case TOK_TILDE:
11015 eat(state, TOK_TILDE);
11016 right = read_expr(state, cast_expr(state));
11017 integral(state, right);
11018 def = integral_promotion(state, right);
11019 def = triple(state, OP_INVERT, def->type, def, 0);
11020 break;
11021 case TOK_BANG:
11022 eat(state, TOK_BANG);
11023 right = read_expr(state, cast_expr(state));
11024 bool(state, right);
11025 def = lfalse_expr(state, right);
11026 break;
11027 case TOK_SIZEOF:
11028 {
11029 struct type *type;
11030 int tok1, tok2;
11031 eat(state, TOK_SIZEOF);
11032 tok1 = peek(state);
11033 tok2 = peek2(state);
11034 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11035 eat(state, TOK_LPAREN);
11036 type = type_name(state);
11037 eat(state, TOK_RPAREN);
11038 }
11039 else {
11040 struct triple *expr;
11041 expr = unary_expr(state);
11042 type = expr->type;
11043 release_expr(state, expr);
11044 }
Eric Biederman90089602004-05-28 14:11:54 +000011045 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011046 break;
11047 }
11048 case TOK_ALIGNOF:
11049 {
11050 struct type *type;
11051 int tok1, tok2;
11052 eat(state, TOK_ALIGNOF);
11053 tok1 = peek(state);
11054 tok2 = peek2(state);
11055 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11056 eat(state, TOK_LPAREN);
11057 type = type_name(state);
11058 eat(state, TOK_RPAREN);
11059 }
11060 else {
11061 struct triple *expr;
11062 expr = unary_expr(state);
11063 type = expr->type;
11064 release_expr(state, expr);
11065 }
Eric Biederman90089602004-05-28 14:11:54 +000011066 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011067 break;
11068 }
Eric Biederman41203d92004-11-08 09:31:09 +000011069 case TOK_MDEFINED:
11070 {
11071 /* We only come here if we are called from the preprocessor */
11072 struct hash_entry *ident;
11073 int parens;
11074 eat(state, TOK_MDEFINED);
11075 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011076 if (pp_peek(state) == TOK_LPAREN) {
11077 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011078 parens = 1;
11079 }
Eric Biedermancb364952004-11-15 10:46:44 +000011080 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011081 if (parens) {
11082 eat(state, TOK_RPAREN);
11083 }
11084 def = int_const(state, &int_type, ident->sym_define != 0);
11085 break;
11086 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011087 default:
11088 def = postfix_expr(state);
11089 break;
11090 }
11091 return def;
11092}
11093
11094static struct triple *cast_expr(struct compile_state *state)
11095{
11096 struct triple *def;
11097 int tok1, tok2;
11098 tok1 = peek(state);
11099 tok2 = peek2(state);
11100 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11101 struct type *type;
11102 eat(state, TOK_LPAREN);
11103 type = type_name(state);
11104 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011105 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011106 }
11107 else {
11108 def = unary_expr(state);
11109 }
11110 return def;
11111}
11112
11113static struct triple *mult_expr(struct compile_state *state)
11114{
11115 struct triple *def;
11116 int done;
11117 def = cast_expr(state);
11118 do {
11119 struct triple *left, *right;
11120 struct type *result_type;
11121 int tok, op, sign;
11122 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011123 tok = peek(state);
11124 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011125 case TOK_STAR:
11126 case TOK_DIV:
11127 case TOK_MOD:
11128 left = read_expr(state, def);
11129 arithmetic(state, left);
11130
11131 eat(state, tok);
11132
11133 right = read_expr(state, cast_expr(state));
11134 arithmetic(state, right);
11135
11136 result_type = arithmetic_result(state, left, right);
11137 sign = is_signed(result_type);
11138 op = -1;
11139 switch(tok) {
11140 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11141 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11142 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11143 }
11144 def = triple(state, op, result_type, left, right);
11145 break;
11146 default:
11147 done = 1;
11148 break;
11149 }
11150 } while(!done);
11151 return def;
11152}
11153
11154static struct triple *add_expr(struct compile_state *state)
11155{
11156 struct triple *def;
11157 int done;
11158 def = mult_expr(state);
11159 do {
11160 done = 0;
11161 switch( peek(state)) {
11162 case TOK_PLUS:
11163 eat(state, TOK_PLUS);
11164 def = mk_add_expr(state, def, mult_expr(state));
11165 break;
11166 case TOK_MINUS:
11167 eat(state, TOK_MINUS);
11168 def = mk_sub_expr(state, def, mult_expr(state));
11169 break;
11170 default:
11171 done = 1;
11172 break;
11173 }
11174 } while(!done);
11175 return def;
11176}
11177
11178static struct triple *shift_expr(struct compile_state *state)
11179{
11180 struct triple *def;
11181 int done;
11182 def = add_expr(state);
11183 do {
11184 struct triple *left, *right;
11185 int tok, op;
11186 done = 0;
11187 switch((tok = peek(state))) {
11188 case TOK_SL:
11189 case TOK_SR:
11190 left = read_expr(state, def);
11191 integral(state, left);
11192 left = integral_promotion(state, left);
11193
11194 eat(state, tok);
11195
11196 right = read_expr(state, add_expr(state));
11197 integral(state, right);
11198 right = integral_promotion(state, right);
11199
11200 op = (tok == TOK_SL)? OP_SL :
11201 is_signed(left->type)? OP_SSR: OP_USR;
11202
11203 def = triple(state, op, left->type, left, right);
11204 break;
11205 default:
11206 done = 1;
11207 break;
11208 }
11209 } while(!done);
11210 return def;
11211}
11212
11213static struct triple *relational_expr(struct compile_state *state)
11214{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011215#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011216#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011217#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011218 struct triple *def;
11219 int done;
11220 def = shift_expr(state);
11221 do {
11222 struct triple *left, *right;
11223 struct type *arg_type;
11224 int tok, op, sign;
11225 done = 0;
11226 switch((tok = peek(state))) {
11227 case TOK_LESS:
11228 case TOK_MORE:
11229 case TOK_LESSEQ:
11230 case TOK_MOREEQ:
11231 left = read_expr(state, def);
11232 arithmetic(state, left);
11233
11234 eat(state, tok);
11235
11236 right = read_expr(state, shift_expr(state));
11237 arithmetic(state, right);
11238
11239 arg_type = arithmetic_result(state, left, right);
11240 sign = is_signed(arg_type);
11241 op = -1;
11242 switch(tok) {
11243 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11244 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11245 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11246 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11247 }
11248 def = triple(state, op, &int_type, left, right);
11249 break;
11250 default:
11251 done = 1;
11252 break;
11253 }
11254 } while(!done);
11255 return def;
11256}
11257
11258static struct triple *equality_expr(struct compile_state *state)
11259{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011260#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011261#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011262#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011263 struct triple *def;
11264 int done;
11265 def = relational_expr(state);
11266 do {
11267 struct triple *left, *right;
11268 int tok, op;
11269 done = 0;
11270 switch((tok = peek(state))) {
11271 case TOK_EQEQ:
11272 case TOK_NOTEQ:
11273 left = read_expr(state, def);
11274 arithmetic(state, left);
11275 eat(state, tok);
11276 right = read_expr(state, relational_expr(state));
11277 arithmetic(state, right);
11278 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11279 def = triple(state, op, &int_type, left, right);
11280 break;
11281 default:
11282 done = 1;
11283 break;
11284 }
11285 } while(!done);
11286 return def;
11287}
11288
11289static struct triple *and_expr(struct compile_state *state)
11290{
11291 struct triple *def;
11292 def = equality_expr(state);
11293 while(peek(state) == TOK_AND) {
11294 struct triple *left, *right;
11295 struct type *result_type;
11296 left = read_expr(state, def);
11297 integral(state, left);
11298 eat(state, TOK_AND);
11299 right = read_expr(state, equality_expr(state));
11300 integral(state, right);
11301 result_type = arithmetic_result(state, left, right);
11302 def = triple(state, OP_AND, result_type, left, right);
11303 }
11304 return def;
11305}
11306
11307static struct triple *xor_expr(struct compile_state *state)
11308{
11309 struct triple *def;
11310 def = and_expr(state);
11311 while(peek(state) == TOK_XOR) {
11312 struct triple *left, *right;
11313 struct type *result_type;
11314 left = read_expr(state, def);
11315 integral(state, left);
11316 eat(state, TOK_XOR);
11317 right = read_expr(state, and_expr(state));
11318 integral(state, right);
11319 result_type = arithmetic_result(state, left, right);
11320 def = triple(state, OP_XOR, result_type, left, right);
11321 }
11322 return def;
11323}
11324
11325static struct triple *or_expr(struct compile_state *state)
11326{
11327 struct triple *def;
11328 def = xor_expr(state);
11329 while(peek(state) == TOK_OR) {
11330 struct triple *left, *right;
11331 struct type *result_type;
11332 left = read_expr(state, def);
11333 integral(state, left);
11334 eat(state, TOK_OR);
11335 right = read_expr(state, xor_expr(state));
11336 integral(state, right);
11337 result_type = arithmetic_result(state, left, right);
11338 def = triple(state, OP_OR, result_type, left, right);
11339 }
11340 return def;
11341}
11342
11343static struct triple *land_expr(struct compile_state *state)
11344{
11345 struct triple *def;
11346 def = or_expr(state);
11347 while(peek(state) == TOK_LOGAND) {
11348 struct triple *left, *right;
11349 left = read_expr(state, def);
11350 bool(state, left);
11351 eat(state, TOK_LOGAND);
11352 right = read_expr(state, or_expr(state));
11353 bool(state, right);
11354
Eric Biederman90089602004-05-28 14:11:54 +000011355 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011356 ltrue_expr(state, left),
11357 ltrue_expr(state, right));
11358 }
11359 return def;
11360}
11361
11362static struct triple *lor_expr(struct compile_state *state)
11363{
11364 struct triple *def;
11365 def = land_expr(state);
11366 while(peek(state) == TOK_LOGOR) {
11367 struct triple *left, *right;
11368 left = read_expr(state, def);
11369 bool(state, left);
11370 eat(state, TOK_LOGOR);
11371 right = read_expr(state, land_expr(state));
11372 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011373
11374 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011375 ltrue_expr(state, left),
11376 ltrue_expr(state, right));
11377 }
11378 return def;
11379}
11380
11381static struct triple *conditional_expr(struct compile_state *state)
11382{
11383 struct triple *def;
11384 def = lor_expr(state);
11385 if (peek(state) == TOK_QUEST) {
11386 struct triple *test, *left, *right;
11387 bool(state, def);
11388 test = ltrue_expr(state, read_expr(state, def));
11389 eat(state, TOK_QUEST);
11390 left = read_expr(state, expr(state));
11391 eat(state, TOK_COLON);
11392 right = read_expr(state, conditional_expr(state));
11393
Eric Biederman90089602004-05-28 14:11:54 +000011394 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011395 }
11396 return def;
11397}
11398
Eric Biederman41203d92004-11-08 09:31:09 +000011399struct cv_triple {
11400 struct triple *val;
11401 int id;
11402};
11403
11404static void set_cv(struct compile_state *state, struct cv_triple *cv,
11405 struct triple *dest, struct triple *val)
11406{
11407 if (cv[dest->id].val) {
11408 free_triple(state, cv[dest->id].val);
11409 }
11410 cv[dest->id].val = val;
11411}
11412static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11413 struct triple *src)
11414{
11415 return cv[src->id].val;
11416}
11417
Eric Biedermanb138ac82003-04-22 18:44:01 +000011418static struct triple *eval_const_expr(
11419 struct compile_state *state, struct triple *expr)
11420{
11421 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011422 if (is_const(expr)) {
11423 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011424 }
Eric Biederman00443072003-06-24 12:34:45 +000011425 else {
11426 /* If we don't start out as a constant simplify into one */
11427 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011428 struct cv_triple *cv;
11429 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011430 head = label(state); /* dummy initial triple */
11431 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011432 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011433 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011434 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011435 }
Eric Biederman41203d92004-11-08 09:31:09 +000011436 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11437 i = 1;
11438 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11439 cv[i].val = 0;
11440 cv[i].id = ptr->id;
11441 ptr->id = i;
11442 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011443 }
Eric Biederman41203d92004-11-08 09:31:09 +000011444 ptr = head->next;
11445 do {
11446 valid_ins(state, ptr);
11447 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
11448 internal_error(state, ptr,
11449 "unexpected %s in constant expression",
11450 tops(ptr->op));
11451 }
11452 else if (ptr->op == OP_LIST) {
11453 }
11454 else if (triple_is_structural(state, ptr)) {
11455 ptr = ptr->next;
11456 }
11457 else if (triple_is_ubranch(state, ptr)) {
11458 ptr = TARG(ptr, 0);
11459 }
11460 else if (triple_is_cbranch(state, ptr)) {
11461 struct triple *cond_val;
11462 cond_val = get_cv(state, cv, RHS(ptr, 0));
11463 if (!cond_val || !is_const(cond_val) ||
11464 (cond_val->op != OP_INTCONST))
11465 {
11466 internal_error(state, ptr, "bad branch condition");
11467 }
11468 if (cond_val->u.cval == 0) {
11469 ptr = ptr->next;
11470 } else {
11471 ptr = TARG(ptr, 0);
11472 }
11473 }
11474 else if (triple_is_branch(state, ptr)) {
11475 error(state, ptr, "bad branch type in constant expression");
11476 }
11477 else if (ptr->op == OP_WRITE) {
11478 struct triple *val;
11479 val = get_cv(state, cv, RHS(ptr, 0));
11480
11481 set_cv(state, cv, MISC(ptr, 0),
11482 copy_triple(state, val));
11483 set_cv(state, cv, ptr,
11484 copy_triple(state, val));
11485 ptr = ptr->next;
11486 }
11487 else if (ptr->op == OP_READ) {
11488 set_cv(state, cv, ptr,
11489 copy_triple(state,
11490 get_cv(state, cv, RHS(ptr, 0))));
11491 ptr = ptr->next;
11492 }
11493 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11494 struct triple *val, **rhs;
11495 val = copy_triple(state, ptr);
11496 rhs = triple_rhs(state, val, 0);
11497 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11498 if (!*rhs) {
11499 internal_error(state, ptr, "Missing rhs");
11500 }
11501 *rhs = get_cv(state, cv, *rhs);
11502 }
11503 simplify(state, val);
11504 set_cv(state, cv, ptr, val);
11505 ptr = ptr->next;
11506 }
11507 else {
11508 error(state, ptr, "impure operation in constant expression");
11509 }
11510
11511 } while(ptr != head);
11512
11513 /* Get the result value */
11514 def = get_cv(state, cv, head->prev);
11515 cv[head->prev->id].val = 0;
11516
11517 /* Free the temporary values */
11518 for(i = 0; i < count; i++) {
11519 if (cv[i].val) {
11520 free_triple(state, cv[i].val);
11521 cv[i].val = 0;
11522 }
11523 }
11524 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011525 /* Free the intermediate expressions */
11526 while(head->next != head) {
11527 release_triple(state, head->next);
11528 }
11529 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011530 }
Eric Biederman41203d92004-11-08 09:31:09 +000011531 if (!is_const(def)) {
11532 error(state, expr, "Not a constant expression");
11533 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011534 return def;
11535}
11536
11537static struct triple *constant_expr(struct compile_state *state)
11538{
11539 return eval_const_expr(state, conditional_expr(state));
11540}
11541
11542static struct triple *assignment_expr(struct compile_state *state)
11543{
11544 struct triple *def, *left, *right;
11545 int tok, op, sign;
11546 /* The C grammer in K&R shows assignment expressions
11547 * only taking unary expressions as input on their
11548 * left hand side. But specifies the precedence of
11549 * assignemnt as the lowest operator except for comma.
11550 *
11551 * Allowing conditional expressions on the left hand side
11552 * of an assignement results in a grammar that accepts
11553 * a larger set of statements than standard C. As long
11554 * as the subset of the grammar that is standard C behaves
11555 * correctly this should cause no problems.
11556 *
11557 * For the extra token strings accepted by the grammar
11558 * none of them should produce a valid lvalue, so they
11559 * should not produce functioning programs.
11560 *
11561 * GCC has this bug as well, so surprises should be minimal.
11562 */
11563 def = conditional_expr(state);
11564 left = def;
11565 switch((tok = peek(state))) {
11566 case TOK_EQ:
11567 lvalue(state, left);
11568 eat(state, TOK_EQ);
11569 def = write_expr(state, left,
11570 read_expr(state, assignment_expr(state)));
11571 break;
11572 case TOK_TIMESEQ:
11573 case TOK_DIVEQ:
11574 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011575 lvalue(state, left);
11576 arithmetic(state, left);
11577 eat(state, tok);
11578 right = read_expr(state, assignment_expr(state));
11579 arithmetic(state, right);
11580
11581 sign = is_signed(left->type);
11582 op = -1;
11583 switch(tok) {
11584 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11585 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11586 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011587 }
11588 def = write_expr(state, left,
11589 triple(state, op, left->type,
11590 read_expr(state, left), right));
11591 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011592 case TOK_PLUSEQ:
11593 lvalue(state, left);
11594 eat(state, TOK_PLUSEQ);
11595 def = write_expr(state, left,
11596 mk_add_expr(state, left, assignment_expr(state)));
11597 break;
11598 case TOK_MINUSEQ:
11599 lvalue(state, left);
11600 eat(state, TOK_MINUSEQ);
11601 def = write_expr(state, left,
11602 mk_sub_expr(state, left, assignment_expr(state)));
11603 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011604 case TOK_SLEQ:
11605 case TOK_SREQ:
11606 case TOK_ANDEQ:
11607 case TOK_XOREQ:
11608 case TOK_OREQ:
11609 lvalue(state, left);
11610 integral(state, left);
11611 eat(state, tok);
11612 right = read_expr(state, assignment_expr(state));
11613 integral(state, right);
11614 right = integral_promotion(state, right);
11615 sign = is_signed(left->type);
11616 op = -1;
11617 switch(tok) {
11618 case TOK_SLEQ: op = OP_SL; break;
11619 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11620 case TOK_ANDEQ: op = OP_AND; break;
11621 case TOK_XOREQ: op = OP_XOR; break;
11622 case TOK_OREQ: op = OP_OR; break;
11623 }
11624 def = write_expr(state, left,
11625 triple(state, op, left->type,
11626 read_expr(state, left), right));
11627 break;
11628 }
11629 return def;
11630}
11631
11632static struct triple *expr(struct compile_state *state)
11633{
11634 struct triple *def;
11635 def = assignment_expr(state);
11636 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011637 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011638 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011639 }
11640 return def;
11641}
11642
11643static void expr_statement(struct compile_state *state, struct triple *first)
11644{
11645 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011646 /* lvalue conversions always apply except when certian operators
11647 * are applied. I apply the lvalue conversions here
11648 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011649 */
11650 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011651 }
11652 eat(state, TOK_SEMI);
11653}
11654
11655static void if_statement(struct compile_state *state, struct triple *first)
11656{
11657 struct triple *test, *jmp1, *jmp2, *middle, *end;
11658
11659 jmp1 = jmp2 = middle = 0;
11660 eat(state, TOK_IF);
11661 eat(state, TOK_LPAREN);
11662 test = expr(state);
11663 bool(state, test);
11664 /* Cleanup and invert the test */
11665 test = lfalse_expr(state, read_expr(state, test));
11666 eat(state, TOK_RPAREN);
11667 /* Generate the needed pieces */
11668 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011669 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011670 /* Thread the pieces together */
11671 flatten(state, first, test);
11672 flatten(state, first, jmp1);
11673 flatten(state, first, label(state));
11674 statement(state, first);
11675 if (peek(state) == TOK_ELSE) {
11676 eat(state, TOK_ELSE);
11677 /* Generate the rest of the pieces */
11678 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011679 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011680 /* Thread them together */
11681 flatten(state, first, jmp2);
11682 flatten(state, first, middle);
11683 statement(state, first);
11684 flatten(state, first, end);
11685 }
11686 else {
11687 flatten(state, first, middle);
11688 }
11689}
11690
11691static void for_statement(struct compile_state *state, struct triple *first)
11692{
11693 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11694 struct triple *label1, *label2, *label3;
11695 struct hash_entry *ident;
11696
11697 eat(state, TOK_FOR);
11698 eat(state, TOK_LPAREN);
11699 head = test = tail = jmp1 = jmp2 = 0;
11700 if (peek(state) != TOK_SEMI) {
11701 head = expr(state);
11702 }
11703 eat(state, TOK_SEMI);
11704 if (peek(state) != TOK_SEMI) {
11705 test = expr(state);
11706 bool(state, test);
11707 test = ltrue_expr(state, read_expr(state, test));
11708 }
11709 eat(state, TOK_SEMI);
11710 if (peek(state) != TOK_RPAREN) {
11711 tail = expr(state);
11712 }
11713 eat(state, TOK_RPAREN);
11714 /* Generate the needed pieces */
11715 label1 = label(state);
11716 label2 = label(state);
11717 label3 = label(state);
11718 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011719 jmp1 = branch(state, label3, 0);
11720 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011721 }
11722 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011723 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011724 }
11725 end = label(state);
11726 /* Remember where break and continue go */
11727 start_scope(state);
11728 ident = state->i_break;
11729 symbol(state, ident, &ident->sym_ident, end, end->type);
11730 ident = state->i_continue;
11731 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11732 /* Now include the body */
11733 flatten(state, first, head);
11734 flatten(state, first, jmp1);
11735 flatten(state, first, label1);
11736 statement(state, first);
11737 flatten(state, first, label2);
11738 flatten(state, first, tail);
11739 flatten(state, first, label3);
11740 flatten(state, first, test);
11741 flatten(state, first, jmp2);
11742 flatten(state, first, end);
11743 /* Cleanup the break/continue scope */
11744 end_scope(state);
11745}
11746
11747static void while_statement(struct compile_state *state, struct triple *first)
11748{
11749 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11750 struct hash_entry *ident;
11751 eat(state, TOK_WHILE);
11752 eat(state, TOK_LPAREN);
11753 test = expr(state);
11754 bool(state, test);
11755 test = ltrue_expr(state, read_expr(state, test));
11756 eat(state, TOK_RPAREN);
11757 /* Generate the needed pieces */
11758 label1 = label(state);
11759 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011760 jmp1 = branch(state, label2, 0);
11761 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011762 end = label(state);
11763 /* Remember where break and continue go */
11764 start_scope(state);
11765 ident = state->i_break;
11766 symbol(state, ident, &ident->sym_ident, end, end->type);
11767 ident = state->i_continue;
11768 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11769 /* Thread them together */
11770 flatten(state, first, jmp1);
11771 flatten(state, first, label1);
11772 statement(state, first);
11773 flatten(state, first, label2);
11774 flatten(state, first, test);
11775 flatten(state, first, jmp2);
11776 flatten(state, first, end);
11777 /* Cleanup the break/continue scope */
11778 end_scope(state);
11779}
11780
11781static void do_statement(struct compile_state *state, struct triple *first)
11782{
11783 struct triple *label1, *label2, *test, *end;
11784 struct hash_entry *ident;
11785 eat(state, TOK_DO);
11786 /* Generate the needed pieces */
11787 label1 = label(state);
11788 label2 = label(state);
11789 end = label(state);
11790 /* Remember where break and continue go */
11791 start_scope(state);
11792 ident = state->i_break;
11793 symbol(state, ident, &ident->sym_ident, end, end->type);
11794 ident = state->i_continue;
11795 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11796 /* Now include the body */
11797 flatten(state, first, label1);
11798 statement(state, first);
11799 /* Cleanup the break/continue scope */
11800 end_scope(state);
11801 /* Eat the rest of the loop */
11802 eat(state, TOK_WHILE);
11803 eat(state, TOK_LPAREN);
11804 test = read_expr(state, expr(state));
11805 bool(state, test);
11806 eat(state, TOK_RPAREN);
11807 eat(state, TOK_SEMI);
11808 /* Thread the pieces together */
11809 test = ltrue_expr(state, test);
11810 flatten(state, first, label2);
11811 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011812 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011813 flatten(state, first, end);
11814}
11815
11816
11817static void return_statement(struct compile_state *state, struct triple *first)
11818{
11819 struct triple *jmp, *mv, *dest, *var, *val;
11820 int last;
11821 eat(state, TOK_RETURN);
11822
Stefan Reinauer50542a82007-10-24 11:14:14 +000011823#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011824#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011825#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011826 val = 0;
11827 /* If we have a return value do some more work */
11828 if (peek(state) != TOK_SEMI) {
11829 val = read_expr(state, expr(state));
11830 }
11831 eat(state, TOK_SEMI);
11832
11833 /* See if this last statement in a function */
11834 last = ((peek(state) == TOK_RBRACE) &&
11835 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11836
11837 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011838 var = fresult(state, state->main_function);
11839
Eric Biedermanb138ac82003-04-22 18:44:01 +000011840 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011841 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011842 mv = jmp = 0;
11843 /* If needed generate a jump instruction */
11844 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011845 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011846 }
11847 /* If needed generate an assignment instruction */
11848 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011849 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011850 }
11851 /* Now put the code together */
11852 if (mv) {
11853 flatten(state, first, mv);
11854 flatten(state, first, jmp);
11855 }
11856 else if (jmp) {
11857 flatten(state, first, jmp);
11858 }
11859}
11860
11861static void break_statement(struct compile_state *state, struct triple *first)
11862{
11863 struct triple *dest;
11864 eat(state, TOK_BREAK);
11865 eat(state, TOK_SEMI);
11866 if (!state->i_break->sym_ident) {
11867 error(state, 0, "break statement not within loop or switch");
11868 }
11869 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011870 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011871}
11872
11873static void continue_statement(struct compile_state *state, struct triple *first)
11874{
11875 struct triple *dest;
11876 eat(state, TOK_CONTINUE);
11877 eat(state, TOK_SEMI);
11878 if (!state->i_continue->sym_ident) {
11879 error(state, 0, "continue statement outside of a loop");
11880 }
11881 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011882 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011883}
11884
11885static void goto_statement(struct compile_state *state, struct triple *first)
11886{
Eric Biederman153ea352003-06-20 14:43:20 +000011887 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011888 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011889 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011890 if (!ident->sym_label) {
11891 /* If this is a forward branch allocate the label now,
11892 * it will be flattend in the appropriate location later.
11893 */
11894 struct triple *ins;
11895 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011896 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011897 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011898 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011899
11900 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011901}
11902
11903static void labeled_statement(struct compile_state *state, struct triple *first)
11904{
Eric Biederman153ea352003-06-20 14:43:20 +000011905 struct triple *ins;
11906 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011907
Eric Biederman41203d92004-11-08 09:31:09 +000011908 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011909 if (ident->sym_label && ident->sym_label->def) {
11910 ins = ident->sym_label->def;
11911 put_occurance(ins->occurance);
11912 ins->occurance = new_occurance(state);
11913 }
11914 else {
11915 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011916 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011917 }
11918 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11919 error(state, 0, "label %s already defined", ident->name);
11920 }
11921 flatten(state, first, ins);
11922
Eric Biedermanb138ac82003-04-22 18:44:01 +000011923 eat(state, TOK_COLON);
11924 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011925}
11926
11927static void switch_statement(struct compile_state *state, struct triple *first)
11928{
Eric Biederman83b991a2003-10-11 06:20:25 +000011929 struct triple *value, *top, *end, *dbranch;
11930 struct hash_entry *ident;
11931
11932 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011933 eat(state, TOK_SWITCH);
11934 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011935 value = expr(state);
11936 integral(state, value);
11937 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011938 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011939 /* Generate the needed pieces */
11940 top = label(state);
11941 end = label(state);
11942 dbranch = branch(state, end, 0);
11943 /* Remember where case branches and break goes */
11944 start_scope(state);
11945 ident = state->i_switch;
11946 symbol(state, ident, &ident->sym_ident, value, value->type);
11947 ident = state->i_case;
11948 symbol(state, ident, &ident->sym_ident, top, top->type);
11949 ident = state->i_break;
11950 symbol(state, ident, &ident->sym_ident, end, end->type);
11951 ident = state->i_default;
11952 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11953 /* Thread them together */
11954 flatten(state, first, value);
11955 flatten(state, first, top);
11956 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011957 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011958 flatten(state, first, end);
11959 /* Cleanup the switch scope */
11960 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011961}
11962
11963static void case_statement(struct compile_state *state, struct triple *first)
11964{
Eric Biederman83b991a2003-10-11 06:20:25 +000011965 struct triple *cvalue, *dest, *test, *jmp;
11966 struct triple *ptr, *value, *top, *dbranch;
11967
11968 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011969 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011970 cvalue = constant_expr(state);
11971 integral(state, cvalue);
11972 if (cvalue->op != OP_INTCONST) {
11973 error(state, 0, "integer constant expected");
11974 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011975 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011976 if (!state->i_case->sym_ident) {
11977 error(state, 0, "case statement not within a switch");
11978 }
11979
11980 /* Lookup the interesting pieces */
11981 top = state->i_case->sym_ident->def;
11982 value = state->i_switch->sym_ident->def;
11983 dbranch = state->i_default->sym_ident->def;
11984
11985 /* See if this case label has already been used */
11986 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11987 if (ptr->op != OP_EQ) {
11988 continue;
11989 }
11990 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11991 error(state, 0, "duplicate case %d statement",
11992 cvalue->u.cval);
11993 }
11994 }
11995 /* Generate the needed pieces */
11996 dest = label(state);
11997 test = triple(state, OP_EQ, &int_type, value, cvalue);
11998 jmp = branch(state, dest, test);
11999 /* Thread the pieces together */
12000 flatten(state, dbranch, test);
12001 flatten(state, dbranch, jmp);
12002 flatten(state, dbranch, label(state));
12003 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012004 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012005}
12006
12007static void default_statement(struct compile_state *state, struct triple *first)
12008{
Eric Biederman83b991a2003-10-11 06:20:25 +000012009 struct triple *dest;
12010 struct triple *dbranch, *end;
12011
12012 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012013 eat(state, TOK_DEFAULT);
12014 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012015
12016 if (!state->i_case->sym_ident) {
12017 error(state, 0, "default statement not within a switch");
12018 }
12019
12020 /* Lookup the interesting pieces */
12021 dbranch = state->i_default->sym_ident->def;
12022 end = state->i_break->sym_ident->def;
12023
12024 /* See if a default statement has already happened */
12025 if (TARG(dbranch, 0) != end) {
12026 error(state, 0, "duplicate default statement");
12027 }
12028
12029 /* Generate the needed pieces */
12030 dest = label(state);
12031
Eric Biederman90089602004-05-28 14:11:54 +000012032 /* Blame the branch on the default statement */
12033 put_occurance(dbranch->occurance);
12034 dbranch->occurance = new_occurance(state);
12035
Eric Biederman83b991a2003-10-11 06:20:25 +000012036 /* Thread the pieces together */
12037 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012038 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012039 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012040 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012041}
12042
12043static void asm_statement(struct compile_state *state, struct triple *first)
12044{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012045 struct asm_info *info;
12046 struct {
12047 struct triple *constraint;
12048 struct triple *expr;
12049 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12050 struct triple *def, *asm_str;
12051 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012052 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012053
Eric Biederman90089602004-05-28 14:11:54 +000012054 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012055 eat(state, TOK_ASM);
12056 /* For now ignore the qualifiers */
12057 switch(peek(state)) {
12058 case TOK_CONST:
12059 eat(state, TOK_CONST);
12060 break;
12061 case TOK_VOLATILE:
12062 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012063 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012064 break;
12065 }
12066 eat(state, TOK_LPAREN);
12067 asm_str = string_constant(state);
12068
12069 colons = 0;
12070 out = in = clobbers = 0;
12071 /* Outputs */
12072 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12073 eat(state, TOK_COLON);
12074 colons++;
12075 more = (peek(state) == TOK_LIT_STRING);
12076 while(more) {
12077 struct triple *var;
12078 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012079 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012080 more = 0;
12081 if (out > MAX_LHS) {
12082 error(state, 0, "Maximum output count exceeded.");
12083 }
12084 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012085 str = constraint->u.blob;
12086 if (str[0] != '=') {
12087 error(state, 0, "Output constraint does not start with =");
12088 }
12089 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012090 eat(state, TOK_LPAREN);
12091 var = conditional_expr(state);
12092 eat(state, TOK_RPAREN);
12093
12094 lvalue(state, var);
12095 out_param[out].constraint = constraint;
12096 out_param[out].expr = var;
12097 if (peek(state) == TOK_COMMA) {
12098 eat(state, TOK_COMMA);
12099 more = 1;
12100 }
12101 out++;
12102 }
12103 }
12104 /* Inputs */
12105 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12106 eat(state, TOK_COLON);
12107 colons++;
12108 more = (peek(state) == TOK_LIT_STRING);
12109 while(more) {
12110 struct triple *val;
12111 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012112 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012113 more = 0;
12114 if (in > MAX_RHS) {
12115 error(state, 0, "Maximum input count exceeded.");
12116 }
12117 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012118 str = constraint->u.blob;
12119 if (digitp(str[0] && str[1] == '\0')) {
12120 int val;
12121 val = digval(str[0]);
12122 if ((val < 0) || (val >= out)) {
12123 error(state, 0, "Invalid input constraint %d", val);
12124 }
12125 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012126 eat(state, TOK_LPAREN);
12127 val = conditional_expr(state);
12128 eat(state, TOK_RPAREN);
12129
12130 in_param[in].constraint = constraint;
12131 in_param[in].expr = val;
12132 if (peek(state) == TOK_COMMA) {
12133 eat(state, TOK_COMMA);
12134 more = 1;
12135 }
12136 in++;
12137 }
12138 }
12139
12140 /* Clobber */
12141 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12142 eat(state, TOK_COLON);
12143 colons++;
12144 more = (peek(state) == TOK_LIT_STRING);
12145 while(more) {
12146 struct triple *clobber;
12147 more = 0;
12148 if ((clobbers + out) > MAX_LHS) {
12149 error(state, 0, "Maximum clobber limit exceeded.");
12150 }
12151 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012152
12153 clob_param[clobbers].constraint = clobber;
12154 if (peek(state) == TOK_COMMA) {
12155 eat(state, TOK_COMMA);
12156 more = 1;
12157 }
12158 clobbers++;
12159 }
12160 }
12161 eat(state, TOK_RPAREN);
12162 eat(state, TOK_SEMI);
12163
12164
12165 info = xcmalloc(sizeof(*info), "asm_info");
12166 info->str = asm_str->u.blob;
12167 free_triple(state, asm_str);
12168
12169 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12170 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012171 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012172
12173 /* Find the register constraints */
12174 for(i = 0; i < out; i++) {
12175 struct triple *constraint;
12176 constraint = out_param[i].constraint;
12177 info->tmpl.lhs[i] = arch_reg_constraint(state,
12178 out_param[i].expr->type, constraint->u.blob);
12179 free_triple(state, constraint);
12180 }
12181 for(; i - out < clobbers; i++) {
12182 struct triple *constraint;
12183 constraint = clob_param[i - out].constraint;
12184 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12185 free_triple(state, constraint);
12186 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012187 for(i = 0; i < in; i++) {
12188 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012189 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012190 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012191 str = constraint->u.blob;
12192 if (digitp(str[0]) && str[1] == '\0') {
12193 struct reg_info cinfo;
12194 int val;
12195 val = digval(str[0]);
12196 cinfo.reg = info->tmpl.lhs[val].reg;
12197 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12198 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12199 if (cinfo.reg == REG_UNSET) {
12200 cinfo.reg = REG_VIRT0 + val;
12201 }
12202 if (cinfo.regcm == 0) {
12203 error(state, 0, "No registers for %d", val);
12204 }
12205 info->tmpl.lhs[val] = cinfo;
12206 info->tmpl.rhs[i] = cinfo;
12207
12208 } else {
12209 info->tmpl.rhs[i] = arch_reg_constraint(state,
12210 in_param[i].expr->type, str);
12211 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012212 free_triple(state, constraint);
12213 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012214
12215 /* Now build the helper expressions */
12216 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012217 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012218 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012219 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012220 for(i = 0; i < (out + clobbers); i++) {
12221 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012222 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012223 if (i < out) {
12224 type = out_param[i].expr->type;
12225 } else {
12226 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12227 if (size >= SIZEOF_LONG) {
12228 type = &ulong_type;
12229 }
12230 else if (size >= SIZEOF_INT) {
12231 type = &uint_type;
12232 }
12233 else if (size >= SIZEOF_SHORT) {
12234 type = &ushort_type;
12235 }
12236 else {
12237 type = &uchar_type;
12238 }
12239 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012240 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012241 piece->u.cval = i;
12242 LHS(def, i) = piece;
12243 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012244 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012245 /* And write the helpers to their destinations */
12246 for(i = 0; i < out; i++) {
12247 struct triple *piece;
12248 piece = LHS(def, i);
12249 flatten(state, first,
12250 write_expr(state, out_param[i].expr, piece));
12251 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012252}
12253
12254
12255static int isdecl(int tok)
12256{
12257 switch(tok) {
12258 case TOK_AUTO:
12259 case TOK_REGISTER:
12260 case TOK_STATIC:
12261 case TOK_EXTERN:
12262 case TOK_TYPEDEF:
12263 case TOK_CONST:
12264 case TOK_RESTRICT:
12265 case TOK_VOLATILE:
12266 case TOK_VOID:
12267 case TOK_CHAR:
12268 case TOK_SHORT:
12269 case TOK_INT:
12270 case TOK_LONG:
12271 case TOK_FLOAT:
12272 case TOK_DOUBLE:
12273 case TOK_SIGNED:
12274 case TOK_UNSIGNED:
12275 case TOK_STRUCT:
12276 case TOK_UNION:
12277 case TOK_ENUM:
12278 case TOK_TYPE_NAME: /* typedef name */
12279 return 1;
12280 default:
12281 return 0;
12282 }
12283}
12284
12285static void compound_statement(struct compile_state *state, struct triple *first)
12286{
12287 eat(state, TOK_LBRACE);
12288 start_scope(state);
12289
12290 /* statement-list opt */
12291 while (peek(state) != TOK_RBRACE) {
12292 statement(state, first);
12293 }
12294 end_scope(state);
12295 eat(state, TOK_RBRACE);
12296}
12297
12298static void statement(struct compile_state *state, struct triple *first)
12299{
12300 int tok;
12301 tok = peek(state);
12302 if (tok == TOK_LBRACE) {
12303 compound_statement(state, first);
12304 }
12305 else if (tok == TOK_IF) {
12306 if_statement(state, first);
12307 }
12308 else if (tok == TOK_FOR) {
12309 for_statement(state, first);
12310 }
12311 else if (tok == TOK_WHILE) {
12312 while_statement(state, first);
12313 }
12314 else if (tok == TOK_DO) {
12315 do_statement(state, first);
12316 }
12317 else if (tok == TOK_RETURN) {
12318 return_statement(state, first);
12319 }
12320 else if (tok == TOK_BREAK) {
12321 break_statement(state, first);
12322 }
12323 else if (tok == TOK_CONTINUE) {
12324 continue_statement(state, first);
12325 }
12326 else if (tok == TOK_GOTO) {
12327 goto_statement(state, first);
12328 }
12329 else if (tok == TOK_SWITCH) {
12330 switch_statement(state, first);
12331 }
12332 else if (tok == TOK_ASM) {
12333 asm_statement(state, first);
12334 }
12335 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12336 labeled_statement(state, first);
12337 }
12338 else if (tok == TOK_CASE) {
12339 case_statement(state, first);
12340 }
12341 else if (tok == TOK_DEFAULT) {
12342 default_statement(state, first);
12343 }
12344 else if (isdecl(tok)) {
12345 /* This handles C99 intermixing of statements and decls */
12346 decl(state, first);
12347 }
12348 else {
12349 expr_statement(state, first);
12350 }
12351}
12352
12353static struct type *param_decl(struct compile_state *state)
12354{
12355 struct type *type;
12356 struct hash_entry *ident;
12357 /* Cheat so the declarator will know we are not global */
12358 start_scope(state);
12359 ident = 0;
12360 type = decl_specifiers(state);
12361 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012362 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012363 end_scope(state);
12364 return type;
12365}
12366
12367static struct type *param_type_list(struct compile_state *state, struct type *type)
12368{
12369 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012370 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012371 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012372 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012373 while(peek(state) == TOK_COMMA) {
12374 eat(state, TOK_COMMA);
12375 if (peek(state) == TOK_DOTS) {
12376 eat(state, TOK_DOTS);
12377 error(state, 0, "variadic functions not supported");
12378 }
12379 else {
12380 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12381 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012382 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012383 }
12384 }
12385 return ftype;
12386}
12387
Eric Biedermanb138ac82003-04-22 18:44:01 +000012388static struct type *type_name(struct compile_state *state)
12389{
12390 struct type *type;
12391 type = specifier_qualifier_list(state);
12392 /* abstract-declarator (may consume no tokens) */
12393 type = declarator(state, type, 0, 0);
12394 return type;
12395}
12396
12397static struct type *direct_declarator(
12398 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012399 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012400{
Eric Biederman41203d92004-11-08 09:31:09 +000012401 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012402 struct type *outer;
12403 int op;
12404 outer = 0;
12405 arrays_complete(state, type);
12406 switch(peek(state)) {
12407 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012408 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012409 if (!ident) {
12410 error(state, 0, "Unexpected identifier found");
12411 }
12412 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012413 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012414 break;
12415 case TOK_LPAREN:
12416 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012417 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012418 eat(state, TOK_RPAREN);
12419 break;
12420 default:
12421 if (need_ident) {
12422 error(state, 0, "Identifier expected");
12423 }
12424 break;
12425 }
12426 do {
12427 op = 1;
12428 arrays_complete(state, type);
12429 switch(peek(state)) {
12430 case TOK_LPAREN:
12431 eat(state, TOK_LPAREN);
12432 type = param_type_list(state, type);
12433 eat(state, TOK_RPAREN);
12434 break;
12435 case TOK_LBRACKET:
12436 {
12437 unsigned int qualifiers;
12438 struct triple *value;
12439 value = 0;
12440 eat(state, TOK_LBRACKET);
12441 if (peek(state) != TOK_RBRACKET) {
12442 value = constant_expr(state);
12443 integral(state, value);
12444 }
12445 eat(state, TOK_RBRACKET);
12446
12447 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12448 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12449 if (value) {
12450 type->elements = value->u.cval;
12451 free_triple(state, value);
12452 } else {
12453 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12454 op = 0;
12455 }
12456 }
12457 break;
12458 default:
12459 op = 0;
12460 break;
12461 }
12462 } while(op);
12463 if (outer) {
12464 struct type *inner;
12465 arrays_complete(state, type);
12466 FINISHME();
12467 for(inner = outer; inner->left; inner = inner->left)
12468 ;
12469 inner->left = type;
12470 type = outer;
12471 }
12472 return type;
12473}
12474
12475static struct type *declarator(
12476 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012477 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012478{
12479 while(peek(state) == TOK_STAR) {
12480 eat(state, TOK_STAR);
12481 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12482 }
Eric Biederman41203d92004-11-08 09:31:09 +000012483 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012484 return type;
12485}
12486
Eric Biedermanb138ac82003-04-22 18:44:01 +000012487static struct type *typedef_name(
12488 struct compile_state *state, unsigned int specifiers)
12489{
12490 struct hash_entry *ident;
12491 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012492 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012493 type = ident->sym_ident->type;
12494 specifiers |= type->type & QUAL_MASK;
12495 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
12496 (type->type & (STOR_MASK | QUAL_MASK))) {
12497 type = clone_type(specifiers, type);
12498 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012499 return type;
12500}
12501
12502static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012503 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012504{
Eric Biederman83b991a2003-10-11 06:20:25 +000012505 struct hash_entry *ident;
12506 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012507 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012508 struct type *enum_type;
12509 enum_type = 0;
12510 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012511 eat(state, TOK_ENUM);
12512 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012513 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012514 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012515 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012516 base = 0;
12517 if (!ident || (peek(state) == TOK_LBRACE)) {
12518 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012519 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012520 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12521 enum_type->type_ident = ident;
12522 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012523 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012524 struct hash_entry *eident;
12525 struct triple *value;
12526 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012527 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012528 if (eident->sym_ident) {
12529 error(state, 0, "%s already declared",
12530 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012531 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012532 eident->tok = TOK_ENUM_CONST;
12533 if (peek(state) == TOK_EQ) {
12534 struct triple *val;
12535 eat(state, TOK_EQ);
12536 val = constant_expr(state);
12537 integral(state, val);
12538 base = val->u.cval;
12539 }
12540 value = int_const(state, &int_type, base);
12541 symbol(state, eident, &eident->sym_ident, value, &int_type);
12542 entry = new_type(TYPE_LIST, 0, 0);
12543 entry->field_ident = eident;
12544 *next = entry;
12545 next = &entry->right;
12546 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012547 if (peek(state) == TOK_COMMA) {
12548 eat(state, TOK_COMMA);
12549 }
12550 } while(peek(state) != TOK_RBRACE);
12551 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012552 if (ident) {
12553 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12554 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012555 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012556 if (ident && ident->sym_tag &&
12557 ident->sym_tag->type &&
12558 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12559 enum_type = clone_type(spec, ident->sym_tag->type);
12560 }
12561 else if (ident && !enum_type) {
12562 error(state, 0, "enum %s undeclared", ident->name);
12563 }
12564 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012565}
12566
Eric Biedermanb138ac82003-04-22 18:44:01 +000012567static struct type *struct_declarator(
12568 struct compile_state *state, struct type *type, struct hash_entry **ident)
12569{
Eric Biederman90089602004-05-28 14:11:54 +000012570 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012571 type = declarator(state, type, ident, 1);
12572 }
Eric Biederman90089602004-05-28 14:11:54 +000012573 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012574 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012575 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012576 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012577 if (value->op != OP_INTCONST) {
12578 error(state, 0, "Invalid constant expression");
12579 }
12580 if (value->u.cval > size_of(state, type)) {
12581 error(state, 0, "bitfield larger than base type");
12582 }
12583 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12584 error(state, 0, "bitfield base not an integer type");
12585 }
12586 type = new_type(TYPE_BITFIELD, type, 0);
12587 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012588 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012589 return type;
12590}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012591
12592static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012593 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012594{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012595 struct type *struct_type;
12596 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012597 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012598 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012599 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012600 struct_type = 0;
12601 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012602 switch(peek(state)) {
12603 case TOK_STRUCT:
12604 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012605 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012606 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012607 break;
12608 case TOK_UNION:
12609 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012610 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012611 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012612 break;
12613 default:
12614 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012615 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012616 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012617 break;
12618 }
12619 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012620 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012621 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012622 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012623 if (!ident || (peek(state) == TOK_LBRACE)) {
12624 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012625 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012626 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012627 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012628 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012629 do {
12630 struct type *base_type;
12631 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012632 base_type = specifier_qualifier_list(state);
12633 do {
12634 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012635 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012636 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012637 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012638 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012639 if (peek(state) == TOK_COMMA) {
12640 done = 0;
12641 eat(state, TOK_COMMA);
12642 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012643 type = clone_type(0, type);
12644 type->field_ident = fident;
12645 if (*next) {
12646 *next = new_type(type_join, *next, type);
12647 next = &((*next)->right);
12648 } else {
12649 *next = type;
12650 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012651 } while(!done);
12652 eat(state, TOK_SEMI);
12653 } while(peek(state) != TOK_RBRACE);
12654 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012655 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012656 struct_type->type_ident = ident;
12657 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012658 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012659 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012660 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012661 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012662 if (ident && ident->sym_tag &&
12663 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012664 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012665 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012666 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012667 else if (ident && !struct_type) {
Eric Biederman90089602004-05-28 14:11:54 +000012668 error(state, 0, "%s %s undeclared",
12669 (type_main == TYPE_STRUCT)?"struct" : "union",
12670 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012671 }
12672 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012673}
12674
12675static unsigned int storage_class_specifier_opt(struct compile_state *state)
12676{
12677 unsigned int specifiers;
12678 switch(peek(state)) {
12679 case TOK_AUTO:
12680 eat(state, TOK_AUTO);
12681 specifiers = STOR_AUTO;
12682 break;
12683 case TOK_REGISTER:
12684 eat(state, TOK_REGISTER);
12685 specifiers = STOR_REGISTER;
12686 break;
12687 case TOK_STATIC:
12688 eat(state, TOK_STATIC);
12689 specifiers = STOR_STATIC;
12690 break;
12691 case TOK_EXTERN:
12692 eat(state, TOK_EXTERN);
12693 specifiers = STOR_EXTERN;
12694 break;
12695 case TOK_TYPEDEF:
12696 eat(state, TOK_TYPEDEF);
12697 specifiers = STOR_TYPEDEF;
12698 break;
12699 default:
12700 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012701 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012702 }
12703 else {
12704 specifiers = STOR_AUTO;
12705 }
12706 }
12707 return specifiers;
12708}
12709
12710static unsigned int function_specifier_opt(struct compile_state *state)
12711{
12712 /* Ignore the inline keyword */
12713 unsigned int specifiers;
12714 specifiers = 0;
12715 switch(peek(state)) {
12716 case TOK_INLINE:
12717 eat(state, TOK_INLINE);
12718 specifiers = STOR_INLINE;
12719 }
12720 return specifiers;
12721}
12722
Eric Biederman90089602004-05-28 14:11:54 +000012723static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12724{
12725 int tok = peek(state);
12726 switch(tok) {
12727 case TOK_COMMA:
12728 case TOK_LPAREN:
12729 /* The empty attribute ignore it */
12730 break;
12731 case TOK_IDENT:
12732 case TOK_ENUM_CONST:
12733 case TOK_TYPE_NAME:
12734 {
12735 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012736 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012737
12738 if (ident == state->i_noinline) {
12739 if (attributes & ATTRIB_ALWAYS_INLINE) {
12740 error(state, 0, "both always_inline and noinline attribtes");
12741 }
12742 attributes |= ATTRIB_NOINLINE;
12743 }
12744 else if (ident == state->i_always_inline) {
12745 if (attributes & ATTRIB_NOINLINE) {
12746 error(state, 0, "both noinline and always_inline attribtes");
12747 }
12748 attributes |= ATTRIB_ALWAYS_INLINE;
12749 }
12750 else {
12751 error(state, 0, "Unknown attribute:%s", ident->name);
12752 }
12753 break;
12754 }
12755 default:
12756 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12757 break;
12758 }
12759 return attributes;
12760}
12761
12762static unsigned int attribute_list(struct compile_state *state, unsigned type)
12763{
12764 type = attrib(state, type);
12765 while(peek(state) == TOK_COMMA) {
12766 eat(state, TOK_COMMA);
12767 type = attrib(state, type);
12768 }
12769 return type;
12770}
12771
12772static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12773{
12774 if (peek(state) == TOK_ATTRIBUTE) {
12775 eat(state, TOK_ATTRIBUTE);
12776 eat(state, TOK_LPAREN);
12777 eat(state, TOK_LPAREN);
12778 type = attribute_list(state, type);
12779 eat(state, TOK_RPAREN);
12780 eat(state, TOK_RPAREN);
12781 }
12782 return type;
12783}
12784
Eric Biedermanb138ac82003-04-22 18:44:01 +000012785static unsigned int type_qualifiers(struct compile_state *state)
12786{
12787 unsigned int specifiers;
12788 int done;
12789 done = 0;
12790 specifiers = QUAL_NONE;
12791 do {
12792 switch(peek(state)) {
12793 case TOK_CONST:
12794 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012795 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012796 break;
12797 case TOK_VOLATILE:
12798 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012799 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012800 break;
12801 case TOK_RESTRICT:
12802 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012803 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012804 break;
12805 default:
12806 done = 1;
12807 break;
12808 }
12809 } while(!done);
12810 return specifiers;
12811}
12812
12813static struct type *type_specifier(
12814 struct compile_state *state, unsigned int spec)
12815{
12816 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012817 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012818 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012819 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012820 case TOK_VOID:
12821 eat(state, TOK_VOID);
12822 type = new_type(TYPE_VOID | spec, 0, 0);
12823 break;
12824 case TOK_CHAR:
12825 eat(state, TOK_CHAR);
12826 type = new_type(TYPE_CHAR | spec, 0, 0);
12827 break;
12828 case TOK_SHORT:
12829 eat(state, TOK_SHORT);
12830 if (peek(state) == TOK_INT) {
12831 eat(state, TOK_INT);
12832 }
12833 type = new_type(TYPE_SHORT | spec, 0, 0);
12834 break;
12835 case TOK_INT:
12836 eat(state, TOK_INT);
12837 type = new_type(TYPE_INT | spec, 0, 0);
12838 break;
12839 case TOK_LONG:
12840 eat(state, TOK_LONG);
12841 switch(peek(state)) {
12842 case TOK_LONG:
12843 eat(state, TOK_LONG);
12844 error(state, 0, "long long not supported");
12845 break;
12846 case TOK_DOUBLE:
12847 eat(state, TOK_DOUBLE);
12848 error(state, 0, "long double not supported");
12849 break;
12850 case TOK_INT:
12851 eat(state, TOK_INT);
12852 type = new_type(TYPE_LONG | spec, 0, 0);
12853 break;
12854 default:
12855 type = new_type(TYPE_LONG | spec, 0, 0);
12856 break;
12857 }
12858 break;
12859 case TOK_FLOAT:
12860 eat(state, TOK_FLOAT);
12861 error(state, 0, "type float not supported");
12862 break;
12863 case TOK_DOUBLE:
12864 eat(state, TOK_DOUBLE);
12865 error(state, 0, "type double not supported");
12866 break;
12867 case TOK_SIGNED:
12868 eat(state, TOK_SIGNED);
12869 switch(peek(state)) {
12870 case TOK_LONG:
12871 eat(state, TOK_LONG);
12872 switch(peek(state)) {
12873 case TOK_LONG:
12874 eat(state, TOK_LONG);
12875 error(state, 0, "type long long not supported");
12876 break;
12877 case TOK_INT:
12878 eat(state, TOK_INT);
12879 type = new_type(TYPE_LONG | spec, 0, 0);
12880 break;
12881 default:
12882 type = new_type(TYPE_LONG | spec, 0, 0);
12883 break;
12884 }
12885 break;
12886 case TOK_INT:
12887 eat(state, TOK_INT);
12888 type = new_type(TYPE_INT | spec, 0, 0);
12889 break;
12890 case TOK_SHORT:
12891 eat(state, TOK_SHORT);
12892 type = new_type(TYPE_SHORT | spec, 0, 0);
12893 break;
12894 case TOK_CHAR:
12895 eat(state, TOK_CHAR);
12896 type = new_type(TYPE_CHAR | spec, 0, 0);
12897 break;
12898 default:
12899 type = new_type(TYPE_INT | spec, 0, 0);
12900 break;
12901 }
12902 break;
12903 case TOK_UNSIGNED:
12904 eat(state, TOK_UNSIGNED);
12905 switch(peek(state)) {
12906 case TOK_LONG:
12907 eat(state, TOK_LONG);
12908 switch(peek(state)) {
12909 case TOK_LONG:
12910 eat(state, TOK_LONG);
12911 error(state, 0, "unsigned long long not supported");
12912 break;
12913 case TOK_INT:
12914 eat(state, TOK_INT);
12915 type = new_type(TYPE_ULONG | spec, 0, 0);
12916 break;
12917 default:
12918 type = new_type(TYPE_ULONG | spec, 0, 0);
12919 break;
12920 }
12921 break;
12922 case TOK_INT:
12923 eat(state, TOK_INT);
12924 type = new_type(TYPE_UINT | spec, 0, 0);
12925 break;
12926 case TOK_SHORT:
12927 eat(state, TOK_SHORT);
12928 type = new_type(TYPE_USHORT | spec, 0, 0);
12929 break;
12930 case TOK_CHAR:
12931 eat(state, TOK_CHAR);
12932 type = new_type(TYPE_UCHAR | spec, 0, 0);
12933 break;
12934 default:
12935 type = new_type(TYPE_UINT | spec, 0, 0);
12936 break;
12937 }
12938 break;
12939 /* struct or union specifier */
12940 case TOK_STRUCT:
12941 case TOK_UNION:
12942 type = struct_or_union_specifier(state, spec);
12943 break;
12944 /* enum-spefifier */
12945 case TOK_ENUM:
12946 type = enum_specifier(state, spec);
12947 break;
12948 /* typedef name */
12949 case TOK_TYPE_NAME:
12950 type = typedef_name(state, spec);
12951 break;
12952 default:
12953 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012954 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012955 break;
12956 }
12957 return type;
12958}
12959
12960static int istype(int tok)
12961{
12962 switch(tok) {
12963 case TOK_CONST:
12964 case TOK_RESTRICT:
12965 case TOK_VOLATILE:
12966 case TOK_VOID:
12967 case TOK_CHAR:
12968 case TOK_SHORT:
12969 case TOK_INT:
12970 case TOK_LONG:
12971 case TOK_FLOAT:
12972 case TOK_DOUBLE:
12973 case TOK_SIGNED:
12974 case TOK_UNSIGNED:
12975 case TOK_STRUCT:
12976 case TOK_UNION:
12977 case TOK_ENUM:
12978 case TOK_TYPE_NAME:
12979 return 1;
12980 default:
12981 return 0;
12982 }
12983}
12984
12985
12986static struct type *specifier_qualifier_list(struct compile_state *state)
12987{
12988 struct type *type;
12989 unsigned int specifiers = 0;
12990
12991 /* type qualifiers */
12992 specifiers |= type_qualifiers(state);
12993
12994 /* type specifier */
12995 type = type_specifier(state, specifiers);
12996
12997 return type;
12998}
12999
Stefan Reinauer50542a82007-10-24 11:14:14 +000013000#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013001static int isdecl_specifier(int tok)
13002{
13003 switch(tok) {
13004 /* storage class specifier */
13005 case TOK_AUTO:
13006 case TOK_REGISTER:
13007 case TOK_STATIC:
13008 case TOK_EXTERN:
13009 case TOK_TYPEDEF:
13010 /* type qualifier */
13011 case TOK_CONST:
13012 case TOK_RESTRICT:
13013 case TOK_VOLATILE:
13014 /* type specifiers */
13015 case TOK_VOID:
13016 case TOK_CHAR:
13017 case TOK_SHORT:
13018 case TOK_INT:
13019 case TOK_LONG:
13020 case TOK_FLOAT:
13021 case TOK_DOUBLE:
13022 case TOK_SIGNED:
13023 case TOK_UNSIGNED:
13024 /* struct or union specifier */
13025 case TOK_STRUCT:
13026 case TOK_UNION:
13027 /* enum-spefifier */
13028 case TOK_ENUM:
13029 /* typedef name */
13030 case TOK_TYPE_NAME:
13031 /* function specifiers */
13032 case TOK_INLINE:
13033 return 1;
13034 default:
13035 return 0;
13036 }
13037}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013038#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013039
13040static struct type *decl_specifiers(struct compile_state *state)
13041{
13042 struct type *type;
13043 unsigned int specifiers;
13044 /* I am overly restrictive in the arragement of specifiers supported.
13045 * C is overly flexible in this department it makes interpreting
13046 * the parse tree difficult.
13047 */
13048 specifiers = 0;
13049
13050 /* storage class specifier */
13051 specifiers |= storage_class_specifier_opt(state);
13052
13053 /* function-specifier */
13054 specifiers |= function_specifier_opt(state);
13055
Eric Biederman90089602004-05-28 14:11:54 +000013056 /* attributes */
13057 specifiers |= attributes_opt(state, 0);
13058
Eric Biedermanb138ac82003-04-22 18:44:01 +000013059 /* type qualifier */
13060 specifiers |= type_qualifiers(state);
13061
13062 /* type specifier */
13063 type = type_specifier(state, specifiers);
13064 return type;
13065}
13066
Eric Biederman00443072003-06-24 12:34:45 +000013067struct field_info {
13068 struct type *type;
13069 size_t offset;
13070};
13071
13072static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013073{
13074 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013075 struct field_info info;
13076 info.offset = ~0U;
13077 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013078 do {
13079 switch(peek(state)) {
13080 case TOK_LBRACKET:
13081 {
13082 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013083 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13084 error(state, 0, "Array designator not in array initializer");
13085 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013086 eat(state, TOK_LBRACKET);
13087 value = constant_expr(state);
13088 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013089
13090 info.type = type->left;
13091 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013092 break;
13093 }
13094 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013095 {
13096 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013097 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13098 ((type->type & TYPE_MASK) != TYPE_UNION))
13099 {
Eric Biederman00443072003-06-24 12:34:45 +000013100 error(state, 0, "Struct designator not in struct initializer");
13101 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013102 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013103 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013104 info.offset = field_offset(state, type, field);
13105 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013106 break;
Eric Biederman00443072003-06-24 12:34:45 +000013107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013108 default:
13109 error(state, 0, "Invalid designator");
13110 }
13111 tok = peek(state);
13112 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13113 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013114 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115}
13116
13117static struct triple *initializer(
13118 struct compile_state *state, struct type *type)
13119{
13120 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013121#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013122#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013123#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013124 if (peek(state) != TOK_LBRACE) {
13125 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013126 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13127 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13128 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13129 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13130 (equiv_types(type->left, result->type->left))) {
13131 type->elements = result->type->elements;
13132 }
Eric Biederman90089602004-05-28 14:11:54 +000013133 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013134 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13135 (type->type & TYPE_MASK) != TYPE_ARRAY)
13136 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013137 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013138 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013139 if (!is_init_compatible(state, type, result->type)) {
13140 error(state, 0, "Incompatible types in initializer");
13141 }
13142 if (!equiv_types(type, result->type)) {
13143 result = mk_cast_expr(state, type, result);
13144 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013145 }
13146 else {
13147 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013148 size_t max_offset;
13149 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013150 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013151 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13152 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13153 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013154 }
Eric Biederman00443072003-06-24 12:34:45 +000013155 info.offset = 0;
13156 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013157 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13158 info.type = next_field(state, type, 0);
13159 }
Eric Biederman00443072003-06-24 12:34:45 +000013160 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13161 max_offset = 0;
13162 } else {
13163 max_offset = size_of(state, type);
13164 }
Eric Biederman90089602004-05-28 14:11:54 +000013165 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013166 eat(state, TOK_LBRACE);
13167 do {
13168 struct triple *value;
13169 struct type *value_type;
13170 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013171 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013172 int tok;
13173 comma = 0;
13174 tok = peek(state);
13175 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013176 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013177 }
Eric Biederman00443072003-06-24 12:34:45 +000013178 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13179 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013180 error(state, 0, "element beyond bounds");
13181 }
Eric Biederman00443072003-06-24 12:34:45 +000013182 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013183 value = eval_const_expr(state, initializer(state, value_type));
13184 value_size = size_of(state, value_type);
13185 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013186 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13187 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013188 void *old_buf;
13189 size_t old_size;
13190 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013191 old_size = max_offset;
13192 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013193 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13194 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013195 xfree(old_buf);
13196 }
Eric Biederman90089602004-05-28 14:11:54 +000013197 dest = ((char *)buf) + bits_to_bytes(info.offset);
13198#if DEBUG_INITIALIZER
13199 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
13200 dest - buf,
13201 bits_to_bytes(max_offset),
13202 bits_to_bytes(value_size),
13203 value->op);
13204#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013205 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013206 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013207 }
Eric Biederman90089602004-05-28 14:11:54 +000013208 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13209#if DEBUG_INITIALIZER
13210 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13211#endif
Eric Biederman00443072003-06-24 12:34:45 +000013212 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013213 }
Eric Biederman90089602004-05-28 14:11:54 +000013214 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013215 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013216 }
Eric Biederman90089602004-05-28 14:11:54 +000013217 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013218 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013219 }
13220 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013221 internal_error(state, 0, "unhandled constant initializer");
13222 }
Eric Biederman00443072003-06-24 12:34:45 +000013223 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013224 if (peek(state) == TOK_COMMA) {
13225 eat(state, TOK_COMMA);
13226 comma = 1;
13227 }
Eric Biederman00443072003-06-24 12:34:45 +000013228 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013229 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13230 info.type = next_field(state, type, info.type);
13231 info.offset = field_offset(state, type,
13232 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013233 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013234 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013235 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13236 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13237 type->elements = max_offset / size_of(state, type->left);
13238 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013239 eat(state, TOK_RBRACE);
13240 result = triple(state, OP_BLOBCONST, type, 0, 0);
13241 result->u.blob = buf;
13242 }
13243 return result;
13244}
13245
Eric Biederman90089602004-05-28 14:11:54 +000013246static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013247{
13248 /* Make a second pass and finish anything outstanding
13249 * with respect to branches. The only outstanding item
13250 * is to see if there are goto to labels that have not
13251 * been defined and to error about them.
13252 */
13253 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013254 struct triple *ins;
13255 /* Also error on branches that do not use their targets */
13256 ins = first;
13257 do {
13258 if (!triple_is_ret(state, ins)) {
13259 struct triple **expr ;
13260 struct triple_set *set;
13261 expr = triple_targ(state, ins, 0);
13262 for(; expr; expr = triple_targ(state, ins, expr)) {
13263 struct triple *targ;
13264 targ = *expr;
13265 for(set = targ?targ->use:0; set; set = set->next) {
13266 if (set->member == ins) {
13267 break;
13268 }
13269 }
13270 if (!set) {
13271 internal_error(state, ins, "targ not used");
13272 }
13273 }
13274 }
13275 ins = ins->next;
13276 } while(ins != first);
13277 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013278 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13279 struct hash_entry *entry;
13280 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13281 struct triple *ins;
13282 if (!entry->sym_label) {
13283 continue;
13284 }
13285 ins = entry->sym_label->def;
13286 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13287 error(state, ins, "label `%s' used but not defined",
13288 entry->name);
13289 }
13290 }
13291 }
13292}
13293
Eric Biedermanb138ac82003-04-22 18:44:01 +000013294static struct triple *function_definition(
13295 struct compile_state *state, struct type *type)
13296{
Eric Biederman90089602004-05-28 14:11:54 +000013297 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13298 struct triple *fname;
13299 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013300 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013301 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013302 int i;
13303 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13304 error(state, 0, "Invalid function header");
13305 }
13306
13307 /* Verify the function type */
13308 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13309 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013310 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013311 error(state, 0, "Invalid function parameters");
13312 }
13313 param = type->right;
13314 i = 0;
13315 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13316 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013317 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013318 error(state, 0, "No identifier for parameter %d\n", i);
13319 }
13320 param = param->right;
13321 }
13322 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013323 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013324 error(state, 0, "No identifier for paramter %d\n", i);
13325 }
13326
13327 /* Get a list of statements for this function. */
13328 def = triple(state, OP_LIST, type, 0, 0);
13329
13330 /* Start a new scope for the passed parameters */
13331 start_scope(state);
13332
13333 /* Put a label at the very start of a function */
13334 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013335 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013336
13337 /* Put a label at the very end of a function */
13338 end = label(state);
13339 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013340 /* Remember where return goes */
13341 ident = state->i_return;
13342 symbol(state, ident, &ident->sym_ident, end, end->type);
13343
Eric Biederman90089602004-05-28 14:11:54 +000013344 /* Get the initial closure type */
13345 ctype = new_type(TYPE_JOIN, &void_type, 0);
13346 ctype->elements = 1;
13347
13348 /* Add a variable for the return value */
13349 crtype = new_type(TYPE_TUPLE,
13350 /* Remove all type qualifiers from the return type */
13351 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13352 crtype->elements = 2;
13353 result = flatten(state, end, variable(state, crtype));
13354
Eric Biederman5ade04a2003-10-22 04:03:46 +000013355 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013356 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013357
13358 /* Add in the return instruction */
13359 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13360 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013361
13362 /* Walk through the parameters and create symbol table entries
13363 * for them.
13364 */
13365 param = type->right;
13366 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013367 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013368 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013369 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013370 flatten(state, end, tmp);
13371 param = param->right;
13372 }
13373 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13374 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013375 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013376 tmp = variable(state, param);
13377 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13378 flatten(state, end, tmp);
13379 }
Eric Biederman90089602004-05-28 14:11:54 +000013380
13381 /* Add the declaration static const char __func__ [] = "func-name" */
13382 fname_type = new_type(TYPE_ARRAY,
13383 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13384 fname_type->type |= QUAL_CONST | STOR_STATIC;
13385 fname_type->elements = strlen(state->function) + 1;
13386
13387 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13388 fname->u.blob = (void *)state->function;
13389 fname = flatten(state, end, fname);
13390
13391 ident = state->i___func__;
13392 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013393
13394 /* Remember which function I am compiling.
13395 * Also assume the last defined function is the main function.
13396 */
13397 state->main_function = def;
13398
13399 /* Now get the actual function definition */
13400 compound_statement(state, end);
13401
Eric Biederman153ea352003-06-20 14:43:20 +000013402 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013403 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013404
Eric Biedermanb138ac82003-04-22 18:44:01 +000013405 /* Remove the parameter scope */
13406 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013407
Eric Biederman5ade04a2003-10-22 04:03:46 +000013408
13409 /* Remember I have defined a function */
13410 if (!state->functions) {
13411 state->functions = def;
13412 } else {
13413 insert_triple(state, state->functions, def);
13414 }
13415 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013416 FILE *fp = state->dbgout;
13417 fprintf(fp, "\n");
13418 loc(fp, state, 0);
13419 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13420 display_func(state, fp, def);
13421 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013422 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013423
13424 return def;
13425}
13426
13427static struct triple *do_decl(struct compile_state *state,
13428 struct type *type, struct hash_entry *ident)
13429{
13430 struct triple *def;
13431 def = 0;
13432 /* Clean up the storage types used */
13433 switch (type->type & STOR_MASK) {
13434 case STOR_AUTO:
13435 case STOR_STATIC:
13436 /* These are the good types I am aiming for */
13437 break;
13438 case STOR_REGISTER:
13439 type->type &= ~STOR_MASK;
13440 type->type |= STOR_AUTO;
13441 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013442 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013443 case STOR_EXTERN:
13444 type->type &= ~STOR_MASK;
13445 type->type |= STOR_STATIC;
13446 break;
13447 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013448 if (!ident) {
13449 error(state, 0, "typedef without name");
13450 }
13451 symbol(state, ident, &ident->sym_ident, 0, type);
13452 ident->tok = TOK_TYPE_NAME;
13453 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013454 break;
13455 default:
13456 internal_error(state, 0, "Undefined storage class");
13457 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013458 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13459 error(state, 0, "Function prototypes not supported");
13460 }
Eric Biederman00443072003-06-24 12:34:45 +000013461 if (ident &&
13462 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013463 ((type->type & QUAL_CONST) == 0)) {
13464 error(state, 0, "non const static variables not supported");
13465 }
13466 if (ident) {
13467 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013468 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013469 }
13470 return def;
13471}
13472
13473static void decl(struct compile_state *state, struct triple *first)
13474{
13475 struct type *base_type, *type;
13476 struct hash_entry *ident;
13477 struct triple *def;
13478 int global;
13479 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13480 base_type = decl_specifiers(state);
13481 ident = 0;
13482 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013483 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013484 if (global && ident && (peek(state) == TOK_LBRACE)) {
13485 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013486 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013487 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013488 def = function_definition(state, type);
13489 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013490 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013491 }
13492 else {
13493 int done;
13494 flatten(state, first, do_decl(state, type, ident));
13495 /* type or variable definition */
13496 do {
13497 done = 1;
13498 if (peek(state) == TOK_EQ) {
13499 if (!ident) {
13500 error(state, 0, "cannot assign to a type");
13501 }
13502 eat(state, TOK_EQ);
13503 flatten(state, first,
13504 init_expr(state,
13505 ident->sym_ident->def,
13506 initializer(state, type)));
13507 }
13508 arrays_complete(state, type);
13509 if (peek(state) == TOK_COMMA) {
13510 eat(state, TOK_COMMA);
13511 ident = 0;
13512 type = declarator(state, base_type, &ident, 0);
13513 flatten(state, first, do_decl(state, type, ident));
13514 done = 0;
13515 }
13516 } while(!done);
13517 eat(state, TOK_SEMI);
13518 }
13519}
13520
13521static void decls(struct compile_state *state)
13522{
13523 struct triple *list;
13524 int tok;
13525 list = label(state);
13526 while(1) {
13527 tok = peek(state);
13528 if (tok == TOK_EOF) {
13529 return;
13530 }
13531 if (tok == TOK_SPACE) {
13532 eat(state, TOK_SPACE);
13533 }
13534 decl(state, list);
13535 if (list->next != list) {
13536 error(state, 0, "global variables not supported");
13537 }
13538 }
13539}
13540
Eric Biederman5ade04a2003-10-22 04:03:46 +000013541/*
13542 * Function inlining
13543 */
Eric Biederman90089602004-05-28 14:11:54 +000013544struct triple_reg_set {
13545 struct triple_reg_set *next;
13546 struct triple *member;
13547 struct triple *new;
13548};
13549struct reg_block {
13550 struct block *block;
13551 struct triple_reg_set *in;
13552 struct triple_reg_set *out;
13553 int vertex;
13554};
13555static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13556static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13557static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13558static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13559static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13560 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13561 void *arg);
13562static void print_block(
13563 struct compile_state *state, struct block *block, void *arg);
13564static int do_triple_set(struct triple_reg_set **head,
13565 struct triple *member, struct triple *new_member);
13566static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13567static struct reg_block *compute_variable_lifetimes(
13568 struct compile_state *state, struct basic_blocks *bb);
13569static void free_variable_lifetimes(struct compile_state *state,
13570 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013571#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000013572static void print_live_variables(struct compile_state *state,
13573 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013574#endif
Eric Biederman90089602004-05-28 14:11:54 +000013575
Eric Biederman5ade04a2003-10-22 04:03:46 +000013576
13577static struct triple *call(struct compile_state *state,
13578 struct triple *retvar, struct triple *ret_addr,
13579 struct triple *targ, struct triple *ret)
13580{
13581 struct triple *call;
13582
13583 if (!retvar || !is_lvalue(state, retvar)) {
13584 internal_error(state, 0, "writing to a non lvalue?");
13585 }
13586 write_compatible(state, retvar->type, &void_ptr_type);
13587
13588 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13589 TARG(call, 0) = targ;
13590 MISC(call, 0) = ret;
13591 if (!targ || (targ->op != OP_LABEL)) {
13592 internal_error(state, 0, "call not to a label");
13593 }
13594 if (!ret || (ret->op != OP_RET)) {
13595 internal_error(state, 0, "call not matched with return");
13596 }
13597 return call;
13598}
13599
Eric Biederman5ade04a2003-10-22 04:03:46 +000013600static void walk_functions(struct compile_state *state,
13601 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13602 void *arg)
13603{
13604 struct triple *func, *first;
13605 func = first = state->functions;
13606 do {
13607 cb(state, func, arg);
13608 func = func->next;
13609 } while(func != first);
13610}
13611
Eric Biederman90089602004-05-28 14:11:54 +000013612static void reverse_walk_functions(struct compile_state *state,
13613 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13614 void *arg)
13615{
13616 struct triple *func, *first;
13617 func = first = state->functions;
13618 do {
13619 func = func->prev;
13620 cb(state, func, arg);
13621 } while(func != first);
13622}
13623
13624
13625static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13626{
13627 struct triple *ptr, *first;
13628 if (func->u.cval == 0) {
13629 return;
13630 }
13631 ptr = first = RHS(func, 0);
13632 do {
13633 if (ptr->op == OP_FCALL) {
13634 struct triple *called_func;
13635 called_func = MISC(ptr, 0);
13636 /* Mark the called function as used */
13637 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13638 called_func->u.cval++;
13639 }
13640 /* Remove the called function from the list */
13641 called_func->prev->next = called_func->next;
13642 called_func->next->prev = called_func->prev;
13643
13644 /* Place the called function before me on the list */
13645 called_func->next = func;
13646 called_func->prev = func->prev;
13647 called_func->prev->next = called_func;
13648 called_func->next->prev = called_func;
13649 }
13650 ptr = ptr->next;
13651 } while(ptr != first);
13652 func->id |= TRIPLE_FLAG_FLATTENED;
13653}
13654
13655static void mark_live_functions(struct compile_state *state)
13656{
13657 /* Ensure state->main_function is the last function in
13658 * the list of functions.
13659 */
13660 if ((state->main_function->next != state->functions) ||
13661 (state->functions->prev != state->main_function)) {
13662 internal_error(state, 0,
13663 "state->main_function is not at the end of the function list ");
13664 }
13665 state->main_function->u.cval = 1;
13666 reverse_walk_functions(state, mark_live, 0);
13667}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013668
13669static int local_triple(struct compile_state *state,
13670 struct triple *func, struct triple *ins)
13671{
13672 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13673#if 0
13674 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013675 FILE *fp = state->errout;
13676 fprintf(fp, "global: ");
13677 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013678 }
13679#endif
13680 return local;
13681}
13682
13683struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
13684 struct occurance *base_occurance)
13685{
13686 struct triple *nfunc;
13687 struct triple *nfirst, *ofirst;
13688 struct triple *new, *old;
13689
13690 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013691 FILE *fp = state->dbgout;
13692 fprintf(fp, "\n");
13693 loc(fp, state, 0);
13694 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13695 display_func(state, fp, ofunc);
13696 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013697 }
13698
13699 /* Make a new copy of the old function */
13700 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13701 nfirst = 0;
13702 ofirst = old = RHS(ofunc, 0);
13703 do {
13704 struct triple *new;
13705 struct occurance *occurance;
13706 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013707 old_lhs = old->lhs;
13708 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013709 occurance = inline_occurance(state, base_occurance, old->occurance);
13710 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13711 MISC(old, 0)->u.cval += 1;
13712 }
13713 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13714 occurance);
13715 if (!triple_stores_block(state, new)) {
13716 memcpy(&new->u, &old->u, sizeof(new->u));
13717 }
13718 if (!nfirst) {
13719 RHS(nfunc, 0) = nfirst = new;
13720 }
13721 else {
13722 insert_triple(state, nfirst, new);
13723 }
13724 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013725 new->id |= old->id & TRIPLE_FLAG_COPY;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013726
13727 /* During the copy remember new as user of old */
13728 use_triple(old, new);
13729
Eric Biederman5ade04a2003-10-22 04:03:46 +000013730 /* Remember which instructions are local */
13731 old->id |= TRIPLE_FLAG_LOCAL;
13732 old = old->next;
13733 } while(old != ofirst);
13734
13735 /* Make a second pass to fix up any unresolved references */
13736 old = ofirst;
13737 new = nfirst;
13738 do {
13739 struct triple **oexpr, **nexpr;
13740 int count, i;
13741 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013742 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013743 for(i = 0; i < count; i++) {
13744 oexpr = &old->param[i];
13745 nexpr = &new->param[i];
13746 if (*oexpr && !*nexpr) {
13747 if (!local_triple(state, ofunc, *oexpr)) {
13748 *nexpr = *oexpr;
13749 }
13750 else if ((*oexpr)->use) {
13751 *nexpr = (*oexpr)->use->member;
13752 }
13753 if (*nexpr == old) {
13754 internal_error(state, 0, "new == old?");
13755 }
13756 use_triple(*nexpr, new);
13757 }
13758 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013759 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013760 }
13761 }
13762 old = old->next;
13763 new = new->next;
13764 } while((old != ofirst) && (new != nfirst));
13765
13766 /* Make a third pass to cleanup the extra useses */
13767 old = ofirst;
13768 new = nfirst;
13769 do {
13770 unuse_triple(old, new);
13771 /* Forget which instructions are local */
13772 old->id &= ~TRIPLE_FLAG_LOCAL;
13773 old = old->next;
13774 new = new->next;
13775 } while ((old != ofirst) && (new != nfirst));
13776 return nfunc;
13777}
13778
Eric Biederman90089602004-05-28 14:11:54 +000013779static void expand_inline_call(
13780 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013781{
13782 /* Inline the function call */
13783 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013784 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013785 struct triple *end, *nend;
13786 int pvals, i;
13787
13788 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013789 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013790 if (ofunc->op != OP_LIST) {
13791 internal_error(state, 0, "improper function");
13792 }
Eric Biederman90089602004-05-28 14:11:54 +000013793 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013794 /* Prepend the parameter reading into the new function list */
13795 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013796 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013797 for(i = 0; i < pvals; i++) {
13798 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013799 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013800 atype = ptype;
13801 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13802 atype = ptype->left;
13803 }
Eric Biederman90089602004-05-28 14:11:54 +000013804 param = farg(state, nfunc, i);
13805 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13806 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013807 }
Eric Biederman90089602004-05-28 14:11:54 +000013808 arg = RHS(fcall, i);
13809 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013810 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013811 }
13812 result = 0;
13813 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman90089602004-05-28 14:11:54 +000013814 result = read_expr(state,
13815 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013816 }
13817 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013818 FILE *fp = state->dbgout;
13819 fprintf(fp, "\n");
13820 loc(fp, state, 0);
13821 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13822 display_func(state, fp, nfunc);
13823 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013824 }
13825
Eric Biederman90089602004-05-28 14:11:54 +000013826 /*
13827 * Get rid of the extra triples
13828 */
13829 /* Remove the read of the return address */
13830 ins = RHS(nfunc, 0)->prev->prev;
13831 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13832 internal_error(state, ins, "Not return addres read?");
13833 }
13834 release_triple(state, ins);
13835 /* Remove the return instruction */
13836 ins = RHS(nfunc, 0)->prev;
13837 if (ins->op != OP_RET) {
13838 internal_error(state, ins, "Not return?");
13839 }
13840 release_triple(state, ins);
13841 /* Remove the retaddres variable */
13842 retvar = fretaddr(state, nfunc);
13843 if ((retvar->lhs != 1) ||
13844 (retvar->op != OP_ADECL) ||
13845 (retvar->next->op != OP_PIECE) ||
13846 (MISC(retvar->next, 0) != retvar)) {
13847 internal_error(state, retvar, "Not the return address?");
13848 }
13849 release_triple(state, retvar->next);
13850 release_triple(state, retvar);
13851
13852 /* Remove the label at the start of the function */
13853 ins = RHS(nfunc, 0);
13854 if (ins->op != OP_LABEL) {
13855 internal_error(state, ins, "Not label?");
13856 }
13857 nfirst = ins->next;
13858 free_triple(state, ins);
13859 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013860 RHS(nfunc, 0) = 0;
13861 free_triple(state, nfunc);
13862
13863 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013864 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013865 nend = nfirst->prev;
13866 end->next = nfirst;
13867 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013868 nend->next = fcall;
13869 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013870
Eric Biederman90089602004-05-28 14:11:54 +000013871 /* Now the result reading code */
13872 if (result) {
13873 result = flatten(state, fcall, result);
13874 propogate_use(state, fcall, result);
13875 }
13876
13877 /* Release the original fcall instruction */
13878 release_triple(state, fcall);
13879
13880 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013881}
13882
Eric Biederman90089602004-05-28 14:11:54 +000013883/*
13884 *
13885 * Type of the result variable.
13886 *
13887 * result
13888 * |
13889 * +----------+------------+
13890 * | |
13891 * union of closures result_type
13892 * |
13893 * +------------------+---------------+
13894 * | |
13895 * closure1 ... closuerN
13896 * | |
13897 * +----+--+-+--------+-----+ +----+----+---+-----+
13898 * | | | | | | | | |
13899 * var1 var2 var3 ... varN result var1 var2 ... varN result
13900 * |
13901 * +--------+---------+
13902 * | |
13903 * union of closures result_type
13904 * |
13905 * +-----+-------------------+
13906 * | |
13907 * closure1 ... closureN
13908 * | |
13909 * +-----+---+----+----+ +----+---+----+-----+
13910 * | | | | | | | |
13911 * var1 var2 ... varN result var1 var2 ... varN result
13912 */
13913
13914static int add_closure_type(struct compile_state *state,
13915 struct triple *func, struct type *closure_type)
13916{
13917 struct type *type, *ctype, **next;
13918 struct triple *var, *new_var;
13919 int i;
13920
13921#if 0
13922 FILE *fp = state->errout;
13923 fprintf(fp, "original_type: ");
13924 name_of(fp, fresult(state, func)->type);
13925 fprintf(fp, "\n");
13926#endif
13927 /* find the original type */
13928 var = fresult(state, func);
13929 type = var->type;
13930 if (type->elements != 2) {
13931 internal_error(state, var, "bad return type");
13932 }
13933
13934 /* Find the complete closure type and update it */
13935 ctype = type->left->left;
13936 next = &ctype->left;
13937 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13938 next = &(*next)->right;
13939 }
13940 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13941 ctype->elements += 1;
13942
13943#if 0
13944 fprintf(fp, "new_type: ");
13945 name_of(fp, type);
13946 fprintf(fp, "\n");
13947 fprintf(fp, "ctype: %p %d bits: %d ",
13948 ctype, ctype->elements, reg_size_of(state, ctype));
13949 name_of(fp, ctype);
13950 fprintf(fp, "\n");
13951#endif
13952
13953 /* Regenerate the variable with the new type definition */
13954 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13955 new_var->id |= TRIPLE_FLAG_FLATTENED;
13956 for(i = 0; i < new_var->lhs; i++) {
13957 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13958 }
13959
13960 /* Point everyone at the new variable */
13961 propogate_use(state, var, new_var);
13962
13963 /* Release the original variable */
13964 for(i = 0; i < var->lhs; i++) {
13965 release_triple(state, LHS(var, i));
13966 }
13967 release_triple(state, var);
13968
13969 /* Return the index of the added closure type */
13970 return ctype->elements - 1;
13971}
13972
13973static struct triple *closure_expr(struct compile_state *state,
13974 struct triple *func, int closure_idx, int var_idx)
13975{
13976 return deref_index(state,
13977 deref_index(state,
13978 deref_index(state, fresult(state, func), 0),
13979 closure_idx),
13980 var_idx);
13981}
13982
13983
13984static void insert_triple_set(
13985 struct triple_reg_set **head, struct triple *member)
13986{
13987 struct triple_reg_set *new;
13988 new = xcmalloc(sizeof(*new), "triple_set");
13989 new->member = member;
13990 new->new = 0;
13991 new->next = *head;
13992 *head = new;
13993}
13994
13995static int ordered_triple_set(
13996 struct triple_reg_set **head, struct triple *member)
13997{
13998 struct triple_reg_set **ptr;
13999 if (!member)
14000 return 0;
14001 ptr = head;
14002 while(*ptr) {
14003 if (member == (*ptr)->member) {
14004 return 0;
14005 }
14006 /* keep the list ordered */
14007 if (member->id < (*ptr)->member->id) {
14008 break;
14009 }
14010 ptr = &(*ptr)->next;
14011 }
14012 insert_triple_set(ptr, member);
14013 return 1;
14014}
14015
14016
14017static void free_closure_variables(struct compile_state *state,
14018 struct triple_reg_set **enclose)
14019{
14020 struct triple_reg_set *entry, *next;
14021 for(entry = *enclose; entry; entry = next) {
14022 next = entry->next;
14023 do_triple_unset(enclose, entry->member);
14024 }
14025}
14026
14027static int lookup_closure_index(struct compile_state *state,
14028 struct triple *me, struct triple *val)
14029{
14030 struct triple *first, *ins, *next;
14031 first = RHS(me, 0);
14032 ins = next = first;
14033 do {
14034 struct triple *result;
14035 struct triple *index0, *index1, *index2, *read, *write;
14036 ins = next;
14037 next = ins->next;
14038 if (ins->op != OP_CALL) {
14039 continue;
14040 }
14041 /* I am at a previous call point examine it closely */
14042 if (ins->next->op != OP_LABEL) {
14043 internal_error(state, ins, "call not followed by label");
14044 }
14045 /* Does this call does not enclose any variables? */
14046 if ((ins->next->next->op != OP_INDEX) ||
14047 (ins->next->next->u.cval != 0) ||
14048 (result = MISC(ins->next->next, 0)) ||
14049 (result->id & TRIPLE_FLAG_LOCAL)) {
14050 continue;
14051 }
14052 index0 = ins->next->next;
14053 /* The pattern is:
14054 * 0 index result < 0 >
14055 * 1 index 0 < ? >
14056 * 2 index 1 < ? >
14057 * 3 read 2
14058 * 4 write 3 var
14059 */
14060 for(index0 = ins->next->next;
14061 (index0->op == OP_INDEX) &&
14062 (MISC(index0, 0) == result) &&
14063 (index0->u.cval == 0) ;
14064 index0 = write->next)
14065 {
14066 index1 = index0->next;
14067 index2 = index1->next;
14068 read = index2->next;
14069 write = read->next;
14070 if ((index0->op != OP_INDEX) ||
14071 (index1->op != OP_INDEX) ||
14072 (index2->op != OP_INDEX) ||
14073 (read->op != OP_READ) ||
14074 (write->op != OP_WRITE) ||
14075 (MISC(index1, 0) != index0) ||
14076 (MISC(index2, 0) != index1) ||
14077 (RHS(read, 0) != index2) ||
14078 (RHS(write, 0) != read)) {
14079 internal_error(state, index0, "bad var read");
14080 }
14081 if (MISC(write, 0) == val) {
14082 return index2->u.cval;
14083 }
14084 }
14085 } while(next != first);
14086 return -1;
14087}
14088
14089static inline int enclose_triple(struct triple *ins)
14090{
14091 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14092}
14093
14094static void compute_closure_variables(struct compile_state *state,
14095 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14096{
14097 struct triple_reg_set *set, *vars, **last_var;
14098 struct basic_blocks bb;
14099 struct reg_block *rb;
14100 struct block *block;
14101 struct triple *old_result, *first, *ins;
14102 size_t count, idx;
14103 unsigned long used_indicies;
14104 int i, max_index;
14105#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14106#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
14107 struct {
14108 unsigned id;
14109 int index;
14110 } *info;
14111
14112
14113 /* Find the basic blocks of this function */
14114 bb.func = me;
14115 bb.first = RHS(me, 0);
14116 old_result = 0;
14117 if (!triple_is_ret(state, bb.first->prev)) {
14118 bb.func = 0;
14119 } else {
14120 old_result = fresult(state, me);
14121 }
14122 analyze_basic_blocks(state, &bb);
14123
14124 /* Find which variables are currently alive in a given block */
14125 rb = compute_variable_lifetimes(state, &bb);
14126
14127 /* Find the variables that are currently alive */
14128 block = block_of_triple(state, fcall);
14129 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14130 internal_error(state, fcall, "No reg block? block: %p", block);
14131 }
14132
14133#if DEBUG_EXPLICIT_CLOSURES
14134 print_live_variables(state, &bb, rb, state->dbgout);
14135 fflush(state->dbgout);
14136#endif
14137
14138 /* Count the number of triples in the function */
14139 first = RHS(me, 0);
14140 ins = first;
14141 count = 0;
14142 do {
14143 count++;
14144 ins = ins->next;
14145 } while(ins != first);
14146
14147 /* Allocate some memory to temorary hold the id info */
14148 info = xcmalloc(sizeof(*info) * (count +1), "info");
14149
14150 /* Mark the local function */
14151 first = RHS(me, 0);
14152 ins = first;
14153 idx = 1;
14154 do {
14155 info[idx].id = ins->id;
14156 ins->id = TRIPLE_FLAG_LOCAL | idx;
14157 idx++;
14158 ins = ins->next;
14159 } while(ins != first);
14160
14161 /*
14162 * Build the list of variables to enclose.
14163 *
14164 * A target it to put the same variable in the
14165 * same slot for ever call of a given function.
14166 * After coloring this removes all of the variable
14167 * manipulation code.
14168 *
14169 * The list of variables to enclose is built ordered
14170 * program order because except in corner cases this
14171 * gives me the stability of assignment I need.
14172 *
14173 * To gurantee that stability I lookup the variables
14174 * to see where they have been used before and
14175 * I build my final list with the assigned indicies.
14176 */
14177 vars = 0;
14178 if (enclose_triple(old_result)) {
14179 ordered_triple_set(&vars, old_result);
14180 }
14181 for(set = rb[block->vertex].out; set; set = set->next) {
14182 if (!enclose_triple(set->member)) {
14183 continue;
14184 }
14185 if ((set->member == fcall) || (set->member == old_result)) {
14186 continue;
14187 }
14188 if (!local_triple(state, me, set->member)) {
14189 internal_error(state, set->member, "not local?");
14190 }
14191 ordered_triple_set(&vars, set->member);
14192 }
14193
14194 /* Lookup the current indicies of the live varialbe */
14195 used_indicies = 0;
14196 max_index = -1;
14197 for(set = vars; set ; set = set->next) {
14198 struct triple *ins;
14199 int index;
14200 ins = set->member;
14201 index = lookup_closure_index(state, me, ins);
14202 info[ID_BITS(ins->id)].index = index;
14203 if (index < 0) {
14204 continue;
14205 }
14206 if (index >= MAX_INDICIES) {
14207 internal_error(state, ins, "index unexpectedly large");
14208 }
14209 if (used_indicies & (1 << index)) {
14210 internal_error(state, ins, "index previously used?");
14211 }
14212 /* Remember which indicies have been used */
14213 used_indicies |= (1 << index);
14214 if (index > max_index) {
14215 max_index = index;
14216 }
14217 }
14218
14219 /* Walk through the live variables and make certain
14220 * everything is assigned an index.
14221 */
14222 for(set = vars; set; set = set->next) {
14223 struct triple *ins;
14224 int index;
14225 ins = set->member;
14226 index = info[ID_BITS(ins->id)].index;
14227 if (index >= 0) {
14228 continue;
14229 }
14230 /* Find the lowest unused index value */
14231 for(index = 0; index < MAX_INDICIES; index++) {
14232 if (!(used_indicies & (1 << index))) {
14233 break;
14234 }
14235 }
14236 if (index == MAX_INDICIES) {
14237 internal_error(state, ins, "no free indicies?");
14238 }
14239 info[ID_BITS(ins->id)].index = index;
14240 /* Remember which indicies have been used */
14241 used_indicies |= (1 << index);
14242 if (index > max_index) {
14243 max_index = index;
14244 }
14245 }
14246
14247 /* Build the return list of variables with positions matching
14248 * their indicies.
14249 */
14250 *enclose = 0;
14251 last_var = enclose;
14252 for(i = 0; i <= max_index; i++) {
14253 struct triple *var;
14254 var = 0;
14255 if (used_indicies & (1 << i)) {
14256 for(set = vars; set; set = set->next) {
14257 int index;
14258 index = info[ID_BITS(set->member->id)].index;
14259 if (index == i) {
14260 var = set->member;
14261 break;
14262 }
14263 }
14264 if (!var) {
14265 internal_error(state, me, "missing variable");
14266 }
14267 }
14268 insert_triple_set(last_var, var);
14269 last_var = &(*last_var)->next;
14270 }
14271
14272#if DEBUG_EXPLICIT_CLOSURES
14273 /* Print out the variables to be enclosed */
14274 loc(state->dbgout, state, fcall);
14275 fprintf(state->dbgout, "Alive: \n");
14276 for(set = *enclose; set; set = set->next) {
14277 display_triple(state->dbgout, set->member);
14278 }
14279 fflush(state->dbgout);
14280#endif
14281
14282 /* Clear the marks */
14283 ins = first;
14284 do {
14285 ins->id = info[ID_BITS(ins->id)].id;
14286 ins = ins->next;
14287 } while(ins != first);
14288
14289 /* Release the ordered list of live variables */
14290 free_closure_variables(state, &vars);
14291
14292 /* Release the storage of the old ids */
14293 xfree(info);
14294
14295 /* Release the variable lifetime information */
14296 free_variable_lifetimes(state, &bb, rb);
14297
14298 /* Release the basic blocks of this function */
14299 free_basic_blocks(state, &bb);
14300}
14301
14302static void expand_function_call(
14303 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014304{
14305 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014306 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014307 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014308 struct triple *first;
14309 struct type *ptype, *rtype;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014310 struct triple *jmp;
14311 struct triple *ret_addr, *ret_loc, *ret_set;
Eric Biederman90089602004-05-28 14:11:54 +000014312 struct triple_reg_set *enclose, *set;
14313 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014314
Eric Biederman90089602004-05-28 14:11:54 +000014315#if DEBUG_EXPLICIT_CLOSURES
14316 FILE *fp = state->dbgout;
14317 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14318 display_func(state, fp, MISC(fcall, 0));
14319 display_func(state, fp, me);
14320 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14321#endif
14322
Eric Biederman5ade04a2003-10-22 04:03:46 +000014323 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014324 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014325 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014326 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014327 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014328 first = fcall->next;
14329
14330 /* Find what I need to enclose */
14331 compute_closure_variables(state, me, fcall, &enclose);
14332
14333 /* Compute the closure type */
14334 closure_type = new_type(TYPE_TUPLE, 0, 0);
14335 closure_type->elements = 0;
14336 closure_next = &closure_type->left;
14337 for(set = enclose; set ; set = set->next) {
14338 struct type *type;
14339 type = &void_type;
14340 if (set->member) {
14341 type = set->member->type;
14342 }
14343 if (!*closure_next) {
14344 *closure_next = type;
14345 } else {
14346 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
14347 type);
14348 closure_next = &(*closure_next)->right;
14349 }
14350 closure_type->elements += 1;
14351 }
14352 if (closure_type->elements == 0) {
14353 closure_type->type = TYPE_VOID;
14354 }
14355
14356
14357#if DEBUG_EXPLICIT_CLOSURES
14358 fprintf(state->dbgout, "closure type: ");
14359 name_of(state->dbgout, closure_type);
14360 fprintf(state->dbgout, "\n");
14361#endif
14362
14363 /* Update the called functions closure variable */
14364 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014365
14366 /* Generate some needed triples */
14367 ret_loc = label(state);
14368 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14369
14370 /* Pass the parameters to the new function */
14371 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014372 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014373 for(i = 0; i < pvals; i++) {
14374 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014375 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014376 atype = ptype;
14377 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14378 atype = ptype->left;
14379 }
Eric Biederman90089602004-05-28 14:11:54 +000014380 param = farg(state, func, i);
14381 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14382 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014383 }
Eric Biederman90089602004-05-28 14:11:54 +000014384 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014385 flatten(state, first, write_expr(state, param, arg));
14386 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014387 }
Eric Biederman90089602004-05-28 14:11:54 +000014388 rtype = func->type->left;
14389
Eric Biederman5ade04a2003-10-22 04:03:46 +000014390 /* Thread the triples together */
14391 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014392
14393 /* Save the active variables in the result variable */
14394 for(i = 0, set = enclose; set ; set = set->next, i++) {
14395 if (!set->member) {
14396 continue;
14397 }
14398 flatten(state, ret_loc,
14399 write_expr(state,
14400 closure_expr(state, func, closure_idx, i),
14401 read_expr(state, set->member)));
14402 }
14403
14404 /* Initialize the return value */
14405 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14406 flatten(state, ret_loc,
14407 write_expr(state,
14408 deref_index(state, fresult(state, func), 1),
14409 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14410 }
14411
Eric Biederman5ade04a2003-10-22 04:03:46 +000014412 ret_addr = flatten(state, ret_loc, ret_addr);
14413 ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14414 jmp = flatten(state, ret_loc,
14415 call(state, retvar, ret_addr, func_first, func_last));
14416
Eric Biederman7dea9552004-06-29 05:38:37 +000014417 /* Find the result */
14418 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14419 struct triple * result;
14420 result = flatten(state, first,
14421 read_expr(state,
14422 deref_index(state, fresult(state, func), 1)));
14423
14424 propogate_use(state, fcall, result);
14425 }
14426
14427 /* Release the original fcall instruction */
14428 release_triple(state, fcall);
14429
Eric Biederman90089602004-05-28 14:11:54 +000014430 /* Restore the active variables from the result variable */
14431 for(i = 0, set = enclose; set ; set = set->next, i++) {
14432 struct triple_set *use, *next;
14433 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014434 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014435 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014436 continue;
14437 }
Eric Biederman90089602004-05-28 14:11:54 +000014438 /* Generate an expression for the value */
14439 new = flatten(state, first,
14440 read_expr(state,
14441 closure_expr(state, func, closure_idx, i)));
14442
14443
14444 /* If the original is an lvalue restore the preserved value */
14445 if (is_lvalue(state, set->member)) {
14446 flatten(state, first,
14447 write_expr(state, set->member, new));
14448 continue;
14449 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014450 /*
14451 * If the original is a value update the dominated uses.
14452 */
14453
14454 /* Analyze the basic blocks so I can see who dominates whom */
14455 bb.func = me;
14456 bb.first = RHS(me, 0);
14457 if (!triple_is_ret(state, bb.first->prev)) {
14458 bb.func = 0;
14459 }
14460 analyze_basic_blocks(state, &bb);
14461
Eric Biederman90089602004-05-28 14:11:54 +000014462
14463#if DEBUG_EXPLICIT_CLOSURES
14464 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14465 set->member, new);
14466#endif
14467 /* If fcall dominates the use update the expression */
14468 for(use = set->member->use; use; use = next) {
14469 /* Replace use modifies the use chain and
14470 * removes use, so I must take a copy of the
14471 * next entry early.
14472 */
14473 next = use->next;
14474 if (!tdominates(state, fcall, use->member)) {
14475 continue;
14476 }
14477 replace_use(state, set->member, new, use->member);
14478 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014479
14480 /* Release the basic blocks, the instructions will be
14481 * different next time, and flatten/insert_triple does
14482 * not update the block values so I can't cache the analysis.
14483 */
14484 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014485 }
14486
Eric Biederman90089602004-05-28 14:11:54 +000014487 /* Release the closure variable list */
14488 free_closure_variables(state, &enclose);
14489
14490 if (state->compiler->debug & DEBUG_INLINE) {
14491 FILE *fp = state->dbgout;
14492 fprintf(fp, "\n");
14493 loc(fp, state, 0);
14494 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14495 display_func(state, fp, func);
14496 display_func(state, fp, me);
14497 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14498 }
14499
14500 return;
14501}
14502
14503static int do_inline(struct compile_state *state, struct triple *func)
14504{
14505 int do_inline;
14506 int policy;
14507
14508 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14509 switch(policy) {
14510 case COMPILER_INLINE_ALWAYS:
14511 do_inline = 1;
14512 if (func->type->type & ATTRIB_NOINLINE) {
14513 error(state, func, "noinline with always_inline compiler option");
14514 }
14515 break;
14516 case COMPILER_INLINE_NEVER:
14517 do_inline = 0;
14518 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14519 error(state, func, "always_inline with noinline compiler option");
14520 }
14521 break;
14522 case COMPILER_INLINE_DEFAULTON:
14523 switch(func->type->type & STOR_MASK) {
14524 case STOR_STATIC | STOR_INLINE:
14525 case STOR_LOCAL | STOR_INLINE:
14526 case STOR_EXTERN | STOR_INLINE:
14527 do_inline = 1;
14528 break;
14529 default:
14530 do_inline = 1;
14531 break;
14532 }
14533 break;
14534 case COMPILER_INLINE_DEFAULTOFF:
14535 switch(func->type->type & STOR_MASK) {
14536 case STOR_STATIC | STOR_INLINE:
14537 case STOR_LOCAL | STOR_INLINE:
14538 case STOR_EXTERN | STOR_INLINE:
14539 do_inline = 1;
14540 break;
14541 default:
14542 do_inline = 0;
14543 break;
14544 }
14545 break;
14546 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014547 switch(func->type->type & STOR_MASK) {
14548 case STOR_STATIC | STOR_INLINE:
14549 case STOR_LOCAL | STOR_INLINE:
14550 case STOR_EXTERN | STOR_INLINE:
14551 do_inline = 1;
14552 break;
14553 default:
14554 do_inline = (func->u.cval == 1);
14555 break;
14556 }
Eric Biederman90089602004-05-28 14:11:54 +000014557 break;
14558 default:
14559 do_inline = 0;
14560 internal_error(state, 0, "Unimplemented inline policy");
14561 break;
14562 }
14563 /* Force inlining */
14564 if (func->type->type & ATTRIB_NOINLINE) {
14565 do_inline = 0;
14566 }
14567 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14568 do_inline = 1;
14569 }
14570 return do_inline;
14571}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014572
Eric Biederman90089602004-05-28 14:11:54 +000014573static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14574{
14575 struct triple *first, *ptr, *next;
14576 /* If the function is not used don't bother */
14577 if (me->u.cval <= 0) {
14578 return;
14579 }
14580 if (state->compiler->debug & DEBUG_CALLS2) {
14581 FILE *fp = state->dbgout;
14582 fprintf(fp, "in: %s\n",
14583 me->type->type_ident->name);
14584 }
14585
14586 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014587 ptr = next = first;
14588 do {
Eric Biederman90089602004-05-28 14:11:54 +000014589 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014590 ptr = next;
14591 prev = ptr->prev;
14592 next = ptr->next;
14593 if (ptr->op != OP_FCALL) {
14594 continue;
14595 }
14596 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014597 /* See if the function should be inlined */
14598 if (!do_inline(state, func)) {
14599 /* Put a label after the fcall */
14600 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14601 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014602 }
Eric Biederman90089602004-05-28 14:11:54 +000014603 if (state->compiler->debug & DEBUG_CALLS) {
14604 FILE *fp = state->dbgout;
14605 if (state->compiler->debug & DEBUG_CALLS2) {
14606 loc(fp, state, ptr);
14607 }
14608 fprintf(fp, "inlining %s\n",
14609 func->type->type_ident->name);
14610 fflush(fp);
14611 }
14612
14613 /* Update the function use counts */
14614 func->u.cval -= 1;
14615
14616 /* Replace the fcall with the called function */
14617 expand_inline_call(state, me, ptr);
14618
14619 next = prev->next;
14620 } while (next != first);
14621
14622 ptr = next = first;
14623 do {
14624 struct triple *prev, *func;
14625 ptr = next;
14626 prev = ptr->prev;
14627 next = ptr->next;
14628 if (ptr->op != OP_FCALL) {
14629 continue;
14630 }
14631 func = MISC(ptr, 0);
14632 if (state->compiler->debug & DEBUG_CALLS) {
14633 FILE *fp = state->dbgout;
14634 if (state->compiler->debug & DEBUG_CALLS2) {
14635 loc(fp, state, ptr);
14636 }
14637 fprintf(fp, "calling %s\n",
14638 func->type->type_ident->name);
14639 fflush(fp);
14640 }
14641 /* Replace the fcall with the instruction sequence
14642 * needed to make the call.
14643 */
14644 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014645 next = prev->next;
14646 } while(next != first);
14647}
Eric Biederman90089602004-05-28 14:11:54 +000014648
14649static void inline_functions(struct compile_state *state, struct triple *func)
14650{
14651 inline_function(state, func, 0);
14652 reverse_walk_functions(state, inline_function, 0);
14653}
14654
Eric Biederman5ade04a2003-10-22 04:03:46 +000014655static void insert_function(struct compile_state *state,
14656 struct triple *func, void *arg)
14657{
14658 struct triple *first, *end, *ffirst, *fend;
14659
14660 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014661 FILE *fp = state->errout;
14662 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014663 func->type->type_ident->name, func->u.cval);
14664 }
14665 if (func->u.cval == 0) {
14666 return;
14667 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014668
14669 /* Find the end points of the lists */
14670 first = arg;
14671 end = first->prev;
14672 ffirst = RHS(func, 0);
14673 fend = ffirst->prev;
14674
14675 /* splice the lists together */
14676 end->next = ffirst;
14677 ffirst->prev = end;
14678 fend->next = first;
14679 first->prev = fend;
14680}
14681
Eric Biederman90089602004-05-28 14:11:54 +000014682struct triple *input_asm(struct compile_state *state)
14683{
14684 struct asm_info *info;
14685 struct triple *def;
14686 int i, out;
14687
14688 info = xcmalloc(sizeof(*info), "asm_info");
14689 info->str = "";
14690
14691 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14692 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14693
14694 def = new_triple(state, OP_ASM, &void_type, out, 0);
14695 def->u.ainfo = info;
14696 def->id |= TRIPLE_FLAG_VOLATILE;
14697
14698 for(i = 0; i < out; i++) {
14699 struct triple *piece;
14700 piece = triple(state, OP_PIECE, &int_type, def, 0);
14701 piece->u.cval = i;
14702 LHS(def, i) = piece;
14703 }
14704
14705 return def;
14706}
14707
14708struct triple *output_asm(struct compile_state *state)
14709{
14710 struct asm_info *info;
14711 struct triple *def;
14712 int in;
14713
14714 info = xcmalloc(sizeof(*info), "asm_info");
14715 info->str = "";
14716
14717 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14718 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14719
14720 def = new_triple(state, OP_ASM, &void_type, 0, in);
14721 def->u.ainfo = info;
14722 def->id |= TRIPLE_FLAG_VOLATILE;
14723
14724 return def;
14725}
14726
Eric Biederman5ade04a2003-10-22 04:03:46 +000014727static void join_functions(struct compile_state *state)
14728{
Eric Biederman90089602004-05-28 14:11:54 +000014729 struct triple *jmp, *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014730 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014731 struct type *pnext, *param;
14732 struct type *result_type, *args_type;
14733 int idx;
14734
14735 /* Be clear the functions have not been joined yet */
14736 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014737
14738 /* Dummy file state to get debug handing right */
14739 memset(&file, 0, sizeof(file));
14740 file.basename = "";
14741 file.line = 0;
14742 file.report_line = 0;
14743 file.report_name = file.basename;
14744 file.prev = state->file;
14745 state->file = &file;
14746 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014747
Eric Biederman41203d92004-11-08 09:31:09 +000014748 if (!state->main_function) {
14749 error(state, 0, "No functions to compile\n");
14750 }
14751
Eric Biederman90089602004-05-28 14:11:54 +000014752 /* The type of arguments */
14753 args_type = state->main_function->type->right;
14754 /* The return type without any specifiers */
14755 result_type = clone_type(0, state->main_function->type->left);
14756
14757
14758 /* Verify the external arguments */
14759 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14760 error(state, state->main_function,
14761 "Too many external input arguments");
14762 }
14763 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14764 error(state, state->main_function,
14765 "Too many external output arguments");
14766 }
14767
Eric Biederman5ade04a2003-10-22 04:03:46 +000014768 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014769 end = label(state);
14770 start = label(state);
14771 start = flatten(state, state->first, start);
14772 end = flatten(state, state->first, end);
14773 in = input_asm(state);
14774 out = output_asm(state);
14775 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014776 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014777 in = flatten(state, state->first, in);
14778 call = flatten(state, state->first, call);
14779 out = flatten(state, state->first, out);
14780
14781
14782 /* Read the external input arguments */
14783 pnext = args_type;
14784 idx = 0;
14785 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14786 struct triple *expr;
14787 param = pnext;
14788 pnext = 0;
14789 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14790 pnext = param->right;
14791 param = param->left;
14792 }
14793 if (registers_of(state, param) != 1) {
14794 error(state, state->main_function,
14795 "Arg: %d %s requires multiple registers",
14796 idx + 1, param->field_ident->name);
14797 }
14798 expr = read_expr(state, LHS(in, idx));
14799 RHS(call, idx) = expr;
14800 expr = flatten(state, call, expr);
14801 use_triple(expr, call);
14802
14803 idx++;
14804 }
14805
14806
14807 /* Write the external output arguments */
14808 pnext = result_type;
14809 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14810 pnext = result_type->left;
14811 }
14812 for(idx = 0; idx < out->rhs; idx++) {
14813 struct triple *expr;
14814 param = pnext;
14815 pnext = 0;
14816 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14817 pnext = param->right;
14818 param = param->left;
14819 }
14820 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14821 param = 0;
14822 }
14823 if (param) {
14824 if (registers_of(state, param) != 1) {
14825 error(state, state->main_function,
14826 "Result: %d %s requires multiple registers",
14827 idx, param->field_ident->name);
14828 }
14829 expr = read_expr(state, call);
14830 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14831 expr = deref_field(state, expr, param->field_ident);
14832 }
14833 } else {
14834 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14835 }
14836 flatten(state, out, expr);
14837 RHS(out, idx) = expr;
14838 use_triple(expr, out);
14839 }
14840
14841 /* Allocate a dummy containing function */
14842 func = triple(state, OP_LIST,
14843 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14844 func->type->type_ident = lookup(state, "", 0);
14845 RHS(func, 0) = state->first;
14846 func->u.cval = 1;
14847
Eric Biederman5ade04a2003-10-22 04:03:46 +000014848 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014849 mark_live_functions(state);
14850 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014851 walk_functions(state, insert_function, end);
14852
14853 if (start->next != end) {
14854 jmp = flatten(state, start, branch(state, end, 0));
14855 }
14856
Eric Biederman90089602004-05-28 14:11:54 +000014857 /* OK now the functions have been joined. */
14858 state->functions_joined = 1;
14859
Eric Biederman5ade04a2003-10-22 04:03:46 +000014860 /* Done now cleanup */
14861 state->file = file.prev;
14862 state->function = 0;
14863}
14864
Eric Biedermanb138ac82003-04-22 18:44:01 +000014865/*
14866 * Data structurs for optimation.
14867 */
14868
Eric Biederman5ade04a2003-10-22 04:03:46 +000014869
Eric Biederman83b991a2003-10-11 06:20:25 +000014870static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014871 struct block *used, struct block_set **head, struct block *user,
14872 int front)
14873{
14874 struct block_set **ptr, *new;
14875 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014876 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014877 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014878 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014879 ptr = head;
14880 while(*ptr) {
14881 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014882 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014883 }
14884 ptr = &(*ptr)->next;
14885 }
14886 new = xcmalloc(sizeof(*new), "block_set");
14887 new->member = user;
14888 if (front) {
14889 new->next = *head;
14890 *head = new;
14891 }
14892 else {
14893 new->next = 0;
14894 *ptr = new;
14895 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014896 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014897}
Eric Biederman83b991a2003-10-11 06:20:25 +000014898static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014899 struct block *used, struct block_set **head, struct block *unuser)
14900{
14901 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014902 int count;
14903 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014904 ptr = head;
14905 while(*ptr) {
14906 use = *ptr;
14907 if (use->member == unuser) {
14908 *ptr = use->next;
14909 memset(use, -1, sizeof(*use));
14910 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014911 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014912 }
14913 else {
14914 ptr = &use->next;
14915 }
14916 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014917 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014918}
14919
14920static void use_block(struct block *used, struct block *user)
14921{
Eric Biederman83b991a2003-10-11 06:20:25 +000014922 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014923 /* Append new to the head of the list, print_block
14924 * depends on this.
14925 */
Eric Biederman83b991a2003-10-11 06:20:25 +000014926 count = do_use_block(used, &used->use, user, 1);
14927 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014928}
14929static void unuse_block(struct block *used, struct block *unuser)
14930{
Eric Biederman83b991a2003-10-11 06:20:25 +000014931 int count;
14932 count = do_unuse_block(used, &used->use, unuser);
14933 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014934}
14935
Eric Biederman5ade04a2003-10-22 04:03:46 +000014936static void add_block_edge(struct block *block, struct block *edge, int front)
14937{
14938 int count;
14939 count = do_use_block(block, &block->edges, edge, front);
14940 block->edge_count += count;
14941}
14942
14943static void remove_block_edge(struct block *block, struct block *edge)
14944{
14945 int count;
14946 count = do_unuse_block(block, &block->edges, edge);
14947 block->edge_count -= count;
14948}
14949
Eric Biedermanb138ac82003-04-22 18:44:01 +000014950static void idom_block(struct block *idom, struct block *user)
14951{
14952 do_use_block(idom, &idom->idominates, user, 0);
14953}
14954
14955static void unidom_block(struct block *idom, struct block *unuser)
14956{
14957 do_unuse_block(idom, &idom->idominates, unuser);
14958}
14959
14960static void domf_block(struct block *block, struct block *domf)
14961{
14962 do_use_block(block, &block->domfrontier, domf, 0);
14963}
14964
14965static void undomf_block(struct block *block, struct block *undomf)
14966{
14967 do_unuse_block(block, &block->domfrontier, undomf);
14968}
14969
14970static void ipdom_block(struct block *ipdom, struct block *user)
14971{
14972 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14973}
14974
14975static void unipdom_block(struct block *ipdom, struct block *unuser)
14976{
14977 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14978}
14979
14980static void ipdomf_block(struct block *block, struct block *ipdomf)
14981{
14982 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14983}
14984
14985static void unipdomf_block(struct block *block, struct block *unipdomf)
14986{
14987 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14988}
14989
Eric Biederman83b991a2003-10-11 06:20:25 +000014990static int walk_triples(
14991 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000014992 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
14993 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014994{
Eric Biederman83b991a2003-10-11 06:20:25 +000014995 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014996 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000014997 ptr = state->first;
14998 do {
Eric Biederman90089602004-05-28 14:11:54 +000014999 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015000 if (ptr->next->prev != ptr) {
15001 internal_error(state, ptr->next, "bad prev");
15002 }
15003 ptr = ptr->next;
15004 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015005 return result;
15006}
15007
Eric Biedermanb138ac82003-04-22 18:44:01 +000015008#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015009static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015010{
Eric Biederman90089602004-05-28 14:11:54 +000015011 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015012 int op;
15013 op = ins->op;
15014 if (op == OP_LIST) {
15015#if !PRINT_LIST
15016 return 0;
15017#endif
15018 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015019 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015020 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015021 }
Eric Biederman90089602004-05-28 14:11:54 +000015022 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015023
Eric Biederman90089602004-05-28 14:11:54 +000015024 if (triple_is_branch(state, ins) && ins->use &&
15025 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015026 internal_error(state, ins, "branch used?");
15027 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015028 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015029 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015030 }
15031 return 0;
15032}
15033
Eric Biedermanb138ac82003-04-22 18:44:01 +000015034static void print_triples(struct compile_state *state)
15035{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015036 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015037 FILE *fp = state->dbgout;
15038 fprintf(fp, "--------------- triples ---------------\n");
15039 walk_triples(state, do_print_triple, fp);
15040 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015042}
15043
15044struct cf_block {
15045 struct block *block;
15046};
15047static void find_cf_blocks(struct cf_block *cf, struct block *block)
15048{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015049 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015050 if (!block || (cf[block->vertex].block == block)) {
15051 return;
15052 }
15053 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015054 for(edge = block->edges; edge; edge = edge->next) {
15055 find_cf_blocks(cf, edge->member);
15056 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015057}
15058
Eric Biederman90089602004-05-28 14:11:54 +000015059static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015060 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015061{
15062 struct cf_block *cf;
15063 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015064 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015065 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15066 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015067
Eric Biederman90089602004-05-28 14:11:54 +000015068 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015069 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015070 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015071 block = cf[i].block;
15072 if (!block)
15073 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015074 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015075 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015076 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015077 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015078 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015079 }
15080
15081 xfree(cf);
15082}
15083
Eric Biedermanb138ac82003-04-22 18:44:01 +000015084static void free_basic_block(struct compile_state *state, struct block *block)
15085{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015086 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015087 struct block *child;
15088 if (!block) {
15089 return;
15090 }
15091 if (block->vertex == -1) {
15092 return;
15093 }
15094 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015095 for(edge = block->edges; edge; edge = edge->next) {
15096 if (edge->member) {
15097 unuse_block(edge->member, block);
15098 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015099 }
15100 if (block->idom) {
15101 unidom_block(block->idom, block);
15102 }
15103 block->idom = 0;
15104 if (block->ipdom) {
15105 unipdom_block(block->ipdom, block);
15106 }
15107 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015108 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015109 child = entry->member;
15110 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015111 if (child && (child->vertex != -1)) {
15112 for(edge = child->edges; edge; edge = edge->next) {
15113 edge->member = 0;
15114 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015115 }
15116 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015117 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015118 child = entry->member;
15119 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015120 if (child && (child->vertex != -1)) {
15121 child->idom = 0;
15122 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015123 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015124 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015125 child = entry->member;
15126 undomf_block(block, child);
15127 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015128 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015129 child = entry->member;
15130 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015131 if (child && (child->vertex != -1)) {
15132 child->ipdom = 0;
15133 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015134 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015135 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015136 child = entry->member;
15137 unipdomf_block(block, child);
15138 }
15139 if (block->users != 0) {
15140 internal_error(state, 0, "block still has users");
15141 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015142 while((edge = block->edges)) {
15143 child = edge->member;
15144 remove_block_edge(block, child);
15145
15146 if (child && (child->vertex != -1)) {
15147 free_basic_block(state, child);
15148 }
15149 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015150 memset(block, -1, sizeof(*block));
15151 xfree(block);
15152}
15153
Eric Biederman90089602004-05-28 14:11:54 +000015154static void free_basic_blocks(struct compile_state *state,
15155 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015156{
15157 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015158 free_basic_block(state, bb->first_block);
15159 bb->last_vertex = 0;
15160 bb->first_block = bb->last_block = 0;
15161 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015162 ins = first;
15163 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015164 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015165 ins->u.block = 0;
15166 }
15167 ins = ins->next;
15168 } while(ins != first);
15169
15170}
15171
Eric Biederman90089602004-05-28 14:11:54 +000015172static struct block *basic_block(struct compile_state *state,
15173 struct basic_blocks *bb, struct triple *first)
15174{
15175 struct block *block;
15176 struct triple *ptr;
15177 if (!triple_is_label(state, first)) {
15178 internal_error(state, first, "block does not start with a label");
15179 }
15180 /* See if this basic block has already been setup */
15181 if (first->u.block != 0) {
15182 return first->u.block;
15183 }
15184 /* Allocate another basic block structure */
15185 bb->last_vertex += 1;
15186 block = xcmalloc(sizeof(*block), "block");
15187 block->first = block->last = first;
15188 block->vertex = bb->last_vertex;
15189 ptr = first;
15190 do {
15191 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
15192 break;
15193 }
15194 block->last = ptr;
15195 /* If ptr->u is not used remember where the baic block is */
15196 if (triple_stores_block(state, ptr)) {
15197 ptr->u.block = block;
15198 }
15199 if (triple_is_branch(state, ptr)) {
15200 break;
15201 }
15202 ptr = ptr->next;
15203 } while (ptr != bb->first);
15204 if ((ptr == bb->first) ||
15205 ((ptr->next == bb->first) && (
15206 triple_is_end(state, ptr) ||
15207 triple_is_ret(state, ptr))))
15208 {
15209 /* The block has no outflowing edges */
15210 }
15211 else if (triple_is_label(state, ptr)) {
15212 struct block *next;
15213 next = basic_block(state, bb, ptr);
15214 add_block_edge(block, next, 0);
15215 use_block(next, block);
15216 }
15217 else if (triple_is_branch(state, ptr)) {
15218 struct triple **expr, *first;
15219 struct block *child;
15220 /* Find the branch targets.
15221 * I special case the first branch as that magically
15222 * avoids some difficult cases for the register allocator.
15223 */
15224 expr = triple_edge_targ(state, ptr, 0);
15225 if (!expr) {
15226 internal_error(state, ptr, "branch without targets");
15227 }
15228 first = *expr;
15229 expr = triple_edge_targ(state, ptr, expr);
15230 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15231 if (!*expr) continue;
15232 child = basic_block(state, bb, *expr);
15233 use_block(child, block);
15234 add_block_edge(block, child, 0);
15235 }
15236 if (first) {
15237 child = basic_block(state, bb, first);
15238 use_block(child, block);
15239 add_block_edge(block, child, 1);
15240
15241 /* Be certain the return block of a call is
15242 * in a basic block. When it is not find
15243 * start of the block, insert a label if
15244 * necessary and build the basic block.
15245 * Then add a fake edge from the start block
15246 * to the return block of the function.
15247 */
15248 if (state->functions_joined && triple_is_call(state, ptr)
15249 && !block_of_triple(state, MISC(ptr, 0))) {
15250 struct block *tail;
15251 struct triple *start;
15252 start = triple_to_block_start(state, MISC(ptr, 0));
15253 if (!triple_is_label(state, start)) {
15254 start = pre_triple(state,
15255 start, OP_LABEL, &void_type, 0, 0);
15256 }
15257 tail = basic_block(state, bb, start);
15258 add_block_edge(child, tail, 0);
15259 use_block(tail, child);
15260 }
15261 }
15262 }
15263 else {
15264 internal_error(state, 0, "Bad basic block split");
15265 }
15266#if 0
15267{
15268 struct block_set *edge;
15269 FILE *fp = state->errout;
15270 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15271 block, block->vertex,
15272 block->first, block->last);
15273 for(edge = block->edges; edge; edge = edge->next) {
15274 fprintf(fp, " %10p [%2d]",
15275 edge->member ? edge->member->first : 0,
15276 edge->member ? edge->member->vertex : -1);
15277 }
15278 fprintf(fp, "\n");
15279}
15280#endif
15281 return block;
15282}
15283
15284
15285static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15286 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15287 void *arg)
15288{
15289 struct triple *ptr, *first;
15290 struct block *last_block;
15291 last_block = 0;
15292 first = bb->first;
15293 ptr = first;
15294 do {
15295 if (triple_stores_block(state, ptr)) {
15296 struct block *block;
15297 block = ptr->u.block;
15298 if (block && (block != last_block)) {
15299 cb(state, block, arg);
15300 }
15301 last_block = block;
15302 }
15303 ptr = ptr->next;
15304 } while(ptr != first);
15305}
15306
15307static void print_block(
15308 struct compile_state *state, struct block *block, void *arg)
15309{
15310 struct block_set *user, *edge;
15311 struct triple *ptr;
15312 FILE *fp = arg;
15313
15314 fprintf(fp, "\nblock: %p (%d) ",
15315 block,
15316 block->vertex);
15317
15318 for(edge = block->edges; edge; edge = edge->next) {
15319 fprintf(fp, " %p<-%p",
15320 edge->member,
15321 (edge->member && edge->member->use)?
15322 edge->member->use->member : 0);
15323 }
15324 fprintf(fp, "\n");
15325 if (block->first->op == OP_LABEL) {
15326 fprintf(fp, "%p:\n", block->first);
15327 }
15328 for(ptr = block->first; ; ) {
15329 display_triple(fp, ptr);
15330 if (ptr == block->last)
15331 break;
15332 ptr = ptr->next;
15333 if (ptr == block->first) {
15334 internal_error(state, 0, "missing block last?");
15335 }
15336 }
15337 fprintf(fp, "users %d: ", block->users);
15338 for(user = block->use; user; user = user->next) {
15339 fprintf(fp, "%p (%d) ",
15340 user->member,
15341 user->member->vertex);
15342 }
15343 fprintf(fp,"\n\n");
15344}
15345
15346
15347static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15348{
15349 fprintf(fp, "--------------- blocks ---------------\n");
15350 walk_blocks(state, &state->bb, print_block, fp);
15351}
15352static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15353{
15354 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15355 fprintf(fp, "After %s\n", func);
15356 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015357 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15358 print_dominators(state, fp, &state->bb);
15359 print_dominance_frontiers(state, fp, &state->bb);
15360 }
15361 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015362 }
15363}
15364
15365static void prune_nonblock_triples(struct compile_state *state,
15366 struct basic_blocks *bb)
15367{
15368 struct block *block;
15369 struct triple *first, *ins, *next;
15370 /* Delete the triples not in a basic block */
15371 block = 0;
15372 first = bb->first;
15373 ins = first;
15374 do {
15375 next = ins->next;
15376 if (ins->op == OP_LABEL) {
15377 block = ins->u.block;
15378 }
15379 if (!block) {
15380 struct triple_set *use;
15381 for(use = ins->use; use; use = use->next) {
15382 struct block *block;
15383 block = block_of_triple(state, use->member);
15384 if (block != 0) {
15385 internal_error(state, ins, "pruning used ins?");
15386 }
15387 }
15388 release_triple(state, ins);
15389 }
15390 if (block && block->last == ins) {
15391 block = 0;
15392 }
15393 ins = next;
15394 } while(ins != first);
15395}
15396
15397static void setup_basic_blocks(struct compile_state *state,
15398 struct basic_blocks *bb)
15399{
15400 if (!triple_stores_block(state, bb->first)) {
15401 internal_error(state, 0, "ins will not store block?");
15402 }
15403 /* Initialize the state */
15404 bb->first_block = bb->last_block = 0;
15405 bb->last_vertex = 0;
15406 free_basic_blocks(state, bb);
15407
15408 /* Find the basic blocks */
15409 bb->first_block = basic_block(state, bb, bb->first);
15410
15411 /* Be certain the last instruction of a function, or the
15412 * entire program is in a basic block. When it is not find
15413 * the start of the block, insert a label if necessary and build
15414 * basic block. Then add a fake edge from the start block
15415 * to the final block.
15416 */
15417 if (!block_of_triple(state, bb->first->prev)) {
15418 struct triple *start;
15419 struct block *tail;
15420 start = triple_to_block_start(state, bb->first->prev);
15421 if (!triple_is_label(state, start)) {
15422 start = pre_triple(state,
15423 start, OP_LABEL, &void_type, 0, 0);
15424 }
15425 tail = basic_block(state, bb, start);
15426 add_block_edge(bb->first_block, tail, 0);
15427 use_block(tail, bb->first_block);
15428 }
15429
15430 /* Find the last basic block.
15431 */
15432 bb->last_block = block_of_triple(state, bb->first->prev);
15433
15434 /* Delete the triples not in a basic block */
15435 prune_nonblock_triples(state, bb);
15436
15437#if 0
15438 /* If we are debugging print what I have just done */
15439 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15440 print_blocks(state, state->dbgout);
15441 print_control_flow(state, bb);
15442 }
15443#endif
15444}
15445
15446
Eric Biedermanb138ac82003-04-22 18:44:01 +000015447struct sdom_block {
15448 struct block *block;
15449 struct sdom_block *sdominates;
15450 struct sdom_block *sdom_next;
15451 struct sdom_block *sdom;
15452 struct sdom_block *label;
15453 struct sdom_block *parent;
15454 struct sdom_block *ancestor;
15455 int vertex;
15456};
15457
15458
15459static void unsdom_block(struct sdom_block *block)
15460{
15461 struct sdom_block **ptr;
15462 if (!block->sdom_next) {
15463 return;
15464 }
15465 ptr = &block->sdom->sdominates;
15466 while(*ptr) {
15467 if ((*ptr) == block) {
15468 *ptr = block->sdom_next;
15469 return;
15470 }
15471 ptr = &(*ptr)->sdom_next;
15472 }
15473}
15474
15475static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15476{
15477 unsdom_block(block);
15478 block->sdom = sdom;
15479 block->sdom_next = sdom->sdominates;
15480 sdom->sdominates = block;
15481}
15482
15483
15484
15485static int initialize_sdblock(struct sdom_block *sd,
15486 struct block *parent, struct block *block, int vertex)
15487{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015488 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015489 if (!block || (sd[block->vertex].block == block)) {
15490 return vertex;
15491 }
15492 vertex += 1;
15493 /* Renumber the blocks in a convinient fashion */
15494 block->vertex = vertex;
15495 sd[vertex].block = block;
15496 sd[vertex].sdom = &sd[vertex];
15497 sd[vertex].label = &sd[vertex];
15498 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15499 sd[vertex].ancestor = 0;
15500 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015501 for(edge = block->edges; edge; edge = edge->next) {
15502 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15503 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015504 return vertex;
15505}
15506
Eric Biederman83b991a2003-10-11 06:20:25 +000015507static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015508 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015509 struct block *parent, struct block *block, int vertex)
15510{
15511 struct block_set *user;
15512 if (!block || (sd[block->vertex].block == block)) {
15513 return vertex;
15514 }
15515 vertex += 1;
15516 /* Renumber the blocks in a convinient fashion */
15517 block->vertex = vertex;
15518 sd[vertex].block = block;
15519 sd[vertex].sdom = &sd[vertex];
15520 sd[vertex].label = &sd[vertex];
15521 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15522 sd[vertex].ancestor = 0;
15523 sd[vertex].vertex = vertex;
15524 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015525 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015526 }
15527 return vertex;
15528}
15529
Eric Biederman90089602004-05-28 14:11:54 +000015530static int setup_spdblocks(struct compile_state *state,
15531 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015532{
15533 struct block *block;
15534 int vertex;
15535 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015536 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015537
Eric Biederman5ade04a2003-10-22 04:03:46 +000015538 /* Walk through the graph and find unconnected blocks. Add a
15539 * fake edge from the unconnected blocks to the end of the
15540 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015541 */
Eric Biederman90089602004-05-28 14:11:54 +000015542 block = bb->first_block->last->next->u.block;
15543 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015544 if (sd[block->vertex].block == block) {
15545 continue;
15546 }
Eric Biederman530b5192003-07-01 10:05:30 +000015547#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015548 {
15549 FILE *fp = state->errout;
15550 fprintf(fp, "Adding %d\n", vertex +1);
15551 }
Eric Biederman530b5192003-07-01 10:05:30 +000015552#endif
Eric Biederman90089602004-05-28 14:11:54 +000015553 add_block_edge(block, bb->last_block, 0);
15554 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015555
Eric Biederman90089602004-05-28 14:11:54 +000015556 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015557 }
15558 return vertex;
15559}
15560
15561static void compress_ancestors(struct sdom_block *v)
15562{
15563 /* This procedure assumes ancestor(v) != 0 */
15564 /* if (ancestor(ancestor(v)) != 0) {
15565 * compress(ancestor(ancestor(v)));
15566 * if (semi(label(ancestor(v))) < semi(label(v))) {
15567 * label(v) = label(ancestor(v));
15568 * }
15569 * ancestor(v) = ancestor(ancestor(v));
15570 * }
15571 */
15572 if (!v->ancestor) {
15573 return;
15574 }
15575 if (v->ancestor->ancestor) {
15576 compress_ancestors(v->ancestor->ancestor);
15577 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15578 v->label = v->ancestor->label;
15579 }
15580 v->ancestor = v->ancestor->ancestor;
15581 }
15582}
15583
Eric Biederman90089602004-05-28 14:11:54 +000015584static void compute_sdom(struct compile_state *state,
15585 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015586{
15587 int i;
15588 /* // step 2
15589 * for each v <= pred(w) {
15590 * u = EVAL(v);
15591 * if (semi[u] < semi[w] {
15592 * semi[w] = semi[u];
15593 * }
15594 * }
15595 * add w to bucket(vertex(semi[w]));
15596 * LINK(parent(w), w);
15597 *
15598 * // step 3
15599 * for each v <= bucket(parent(w)) {
15600 * delete v from bucket(parent(w));
15601 * u = EVAL(v);
15602 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15603 * }
15604 */
Eric Biederman90089602004-05-28 14:11:54 +000015605 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015606 struct sdom_block *v, *parent, *next;
15607 struct block_set *user;
15608 struct block *block;
15609 block = sd[i].block;
15610 parent = sd[i].parent;
15611 /* Step 2 */
15612 for(user = block->use; user; user = user->next) {
15613 struct sdom_block *v, *u;
15614 v = &sd[user->member->vertex];
15615 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15616 if (u->sdom->vertex < sd[i].sdom->vertex) {
15617 sd[i].sdom = u->sdom;
15618 }
15619 }
15620 sdom_block(sd[i].sdom, &sd[i]);
15621 sd[i].ancestor = parent;
15622 /* Step 3 */
15623 for(v = parent->sdominates; v; v = next) {
15624 struct sdom_block *u;
15625 next = v->sdom_next;
15626 unsdom_block(v);
15627 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15628 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
15629 u->block : parent->block;
15630 }
15631 }
15632}
15633
Eric Biederman90089602004-05-28 14:11:54 +000015634static void compute_spdom(struct compile_state *state,
15635 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015636{
15637 int i;
15638 /* // step 2
15639 * for each v <= pred(w) {
15640 * u = EVAL(v);
15641 * if (semi[u] < semi[w] {
15642 * semi[w] = semi[u];
15643 * }
15644 * }
15645 * add w to bucket(vertex(semi[w]));
15646 * LINK(parent(w), w);
15647 *
15648 * // step 3
15649 * for each v <= bucket(parent(w)) {
15650 * delete v from bucket(parent(w));
15651 * u = EVAL(v);
15652 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15653 * }
15654 */
Eric Biederman90089602004-05-28 14:11:54 +000015655 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015656 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015657 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015658 struct block *block;
15659 block = sd[i].block;
15660 parent = sd[i].parent;
15661 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015662 for(edge = block->edges; edge; edge = edge->next) {
15663 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015664 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15665 if (u->sdom->vertex < sd[i].sdom->vertex) {
15666 sd[i].sdom = u->sdom;
15667 }
15668 }
15669 sdom_block(sd[i].sdom, &sd[i]);
15670 sd[i].ancestor = parent;
15671 /* Step 3 */
15672 for(v = parent->sdominates; v; v = next) {
15673 struct sdom_block *u;
15674 next = v->sdom_next;
15675 unsdom_block(v);
15676 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15677 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
15678 u->block : parent->block;
15679 }
15680 }
15681}
15682
Eric Biederman90089602004-05-28 14:11:54 +000015683static void compute_idom(struct compile_state *state,
15684 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015685{
15686 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015687 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015688 struct block *block;
15689 block = sd[i].block;
15690 if (block->idom->vertex != sd[i].sdom->vertex) {
15691 block->idom = block->idom->idom;
15692 }
15693 idom_block(block->idom, block);
15694 }
15695 sd[1].block->idom = 0;
15696}
15697
Eric Biederman90089602004-05-28 14:11:54 +000015698static void compute_ipdom(struct compile_state *state,
15699 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015700{
15701 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015702 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015703 struct block *block;
15704 block = sd[i].block;
15705 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15706 block->ipdom = block->ipdom->ipdom;
15707 }
15708 ipdom_block(block->ipdom, block);
15709 }
15710 sd[1].block->ipdom = 0;
15711}
15712
15713 /* Theorem 1:
15714 * Every vertex of a flowgraph G = (V, E, r) except r has
15715 * a unique immediate dominator.
15716 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15717 * rooted at r, called the dominator tree of G, such that
15718 * v dominates w if and only if v is a proper ancestor of w in
15719 * the dominator tree.
15720 */
15721 /* Lemma 1:
15722 * If v and w are vertices of G such that v <= w,
15723 * than any path from v to w must contain a common ancestor
15724 * of v and w in T.
15725 */
15726 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15727 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15728 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15729 /* Theorem 2:
15730 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15731 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15732 */
15733 /* Theorem 3:
15734 * Let w != r and let u be a vertex for which sdom(u) is
15735 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15736 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15737 */
15738 /* Lemma 5: Let vertices v,w satisfy v -> w.
15739 * Then v -> idom(w) or idom(w) -> idom(v)
15740 */
15741
Eric Biederman90089602004-05-28 14:11:54 +000015742static void find_immediate_dominators(struct compile_state *state,
15743 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015744{
15745 struct sdom_block *sd;
15746 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15747 * vi > w for (1 <= i <= k - 1}
15748 */
15749 /* Theorem 4:
15750 * For any vertex w != r.
15751 * sdom(w) = min(
15752 * {v|(v,w) <= E and v < w } U
15753 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15754 */
15755 /* Corollary 1:
15756 * Let w != r and let u be a vertex for which sdom(u) is
15757 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15758 * Then:
15759 * { sdom(w) if sdom(w) = sdom(u),
15760 * idom(w) = {
15761 * { idom(u) otherwise
15762 */
15763 /* The algorithm consists of the following 4 steps.
15764 * Step 1. Carry out a depth-first search of the problem graph.
15765 * Number the vertices from 1 to N as they are reached during
15766 * the search. Initialize the variables used in succeeding steps.
15767 * Step 2. Compute the semidominators of all vertices by applying
15768 * theorem 4. Carry out the computation vertex by vertex in
15769 * decreasing order by number.
15770 * Step 3. Implicitly define the immediate dominator of each vertex
15771 * by applying Corollary 1.
15772 * Step 4. Explicitly define the immediate dominator of each vertex,
15773 * carrying out the computation vertex by vertex in increasing order
15774 * by number.
15775 */
15776 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015777 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15778 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015779#if 0
15780 sd[1].size = 0;
15781 sd[1].label = 0;
15782 sd[1].sdom = 0;
15783#endif
15784 /* Step 2 compute the semidominators */
15785 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015786 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015787 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015788 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015789 xfree(sd);
15790}
15791
Eric Biederman90089602004-05-28 14:11:54 +000015792static void find_post_dominators(struct compile_state *state,
15793 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015794{
15795 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015796 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015797 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015798 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015799
Eric Biederman90089602004-05-28 14:11:54 +000015800 vertex = setup_spdblocks(state, bb, sd);
15801 if (vertex != bb->last_vertex) {
15802 internal_error(state, 0, "missing %d blocks",
15803 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015804 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015805
15806 /* Step 2 compute the semidominators */
15807 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015808 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015809 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015810 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811 xfree(sd);
15812}
15813
15814
15815
15816static void find_block_domf(struct compile_state *state, struct block *block)
15817{
15818 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015819 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015820 if (block->domfrontier != 0) {
15821 internal_error(state, block->first, "domfrontier present?");
15822 }
15823 for(user = block->idominates; user; user = user->next) {
15824 child = user->member;
15825 if (child->idom != block) {
15826 internal_error(state, block->first, "bad idom");
15827 }
15828 find_block_domf(state, child);
15829 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015830 for(edge = block->edges; edge; edge = edge->next) {
15831 if (edge->member->idom != block) {
15832 domf_block(block, edge->member);
15833 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015834 }
15835 for(user = block->idominates; user; user = user->next) {
15836 struct block_set *frontier;
15837 child = user->member;
15838 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15839 if (frontier->member->idom != block) {
15840 domf_block(block, frontier->member);
15841 }
15842 }
15843 }
15844}
15845
15846static void find_block_ipdomf(struct compile_state *state, struct block *block)
15847{
15848 struct block *child;
15849 struct block_set *user;
15850 if (block->ipdomfrontier != 0) {
15851 internal_error(state, block->first, "ipdomfrontier present?");
15852 }
15853 for(user = block->ipdominates; user; user = user->next) {
15854 child = user->member;
15855 if (child->ipdom != block) {
15856 internal_error(state, block->first, "bad ipdom");
15857 }
15858 find_block_ipdomf(state, child);
15859 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015860 for(user = block->use; user; user = user->next) {
15861 if (user->member->ipdom != block) {
15862 ipdomf_block(block, user->member);
15863 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015864 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015865 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015866 struct block_set *frontier;
15867 child = user->member;
15868 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15869 if (frontier->member->ipdom != block) {
15870 ipdomf_block(block, frontier->member);
15871 }
15872 }
15873 }
15874}
15875
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015876static void print_dominated(
15877 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015878{
15879 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015880 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015881
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015882 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015883 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015884 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015885 if (user->member->idom != block) {
15886 internal_error(state, user->member->first, "bad idom");
15887 }
15888 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015889 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015890}
15891
Eric Biederman5ade04a2003-10-22 04:03:46 +000015892static void print_dominated2(
15893 struct compile_state *state, FILE *fp, int depth, struct block *block)
15894{
15895 struct block_set *user;
15896 struct triple *ins;
15897 struct occurance *ptr, *ptr2;
15898 const char *filename1, *filename2;
15899 int equal_filenames;
15900 int i;
15901 for(i = 0; i < depth; i++) {
15902 fprintf(fp, " ");
15903 }
15904 fprintf(fp, "%3d: %p (%p - %p) @",
15905 block->vertex, block, block->first, block->last);
15906 ins = block->first;
15907 while(ins != block->last && (ins->occurance->line == 0)) {
15908 ins = ins->next;
15909 }
15910 ptr = ins->occurance;
15911 ptr2 = block->last->occurance;
15912 filename1 = ptr->filename? ptr->filename : "";
15913 filename2 = ptr2->filename? ptr2->filename : "";
15914 equal_filenames = (strcmp(filename1, filename2) == 0);
15915 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15916 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15917 } else if (equal_filenames) {
15918 fprintf(fp, " %s:(%d - %d)",
15919 ptr->filename, ptr->line, ptr2->line);
15920 } else {
15921 fprintf(fp, " (%s:%d - %s:%d)",
15922 ptr->filename, ptr->line,
15923 ptr2->filename, ptr2->line);
15924 }
15925 fprintf(fp, "\n");
15926 for(user = block->idominates; user; user = user->next) {
15927 print_dominated2(state, fp, depth + 1, user->member);
15928 }
15929}
15930
Eric Biederman90089602004-05-28 14:11:54 +000015931static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015932{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015933 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015934 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015935 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015936 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015937}
15938
15939
15940static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015941 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015942{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015943 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015944
15945 if (!block || (block->vertex != vertex + 1)) {
15946 return vertex;
15947 }
15948 vertex += 1;
15949
Eric Biederman7dea9552004-06-29 05:38:37 +000015950 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015951 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015952 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015953 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015954 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015955
15956 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015957 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015958 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015959 return vertex;
15960}
Eric Biederman90089602004-05-28 14:11:54 +000015961static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015962 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015963{
Eric Biederman7dea9552004-06-29 05:38:37 +000015964 fprintf(fp, "\ndominance frontiers\n");
15965 print_frontiers(state, fp, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015966
15967}
15968
Eric Biederman90089602004-05-28 14:11:54 +000015969static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015970{
15971 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015972 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015973 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015974 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015975 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015976 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015977 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015978 print_dominance_frontiers(state, state->dbgout, bb);
15979 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015980 }
15981}
15982
15983
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015984static void print_ipdominated(
15985 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986{
15987 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015988 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015989
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015990 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015991 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015992 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015993 if (user->member->ipdom != block) {
15994 internal_error(state, user->member->first, "bad ipdom");
15995 }
15996 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015997 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015998}
15999
Eric Biederman90089602004-05-28 14:11:54 +000016000static void print_ipdominators(struct compile_state *state, FILE *fp,
16001 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016002{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016004 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016005}
16006
16007static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016008 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016009{
16010 struct block_set *user;
16011
16012 if (!block || (block->vertex != vertex + 1)) {
16013 return vertex;
16014 }
16015 vertex += 1;
16016
Eric Biederman7dea9552004-06-29 05:38:37 +000016017 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016018 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016019 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016020 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016021 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016022 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016023 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016024 }
16025 return vertex;
16026}
Eric Biederman90089602004-05-28 14:11:54 +000016027static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016028 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016029{
Eric Biederman7dea9552004-06-29 05:38:37 +000016030 fprintf(fp, "\nipdominance frontiers\n");
16031 print_pfrontiers(state, fp, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016032
16033}
16034
Eric Biederman90089602004-05-28 14:11:54 +000016035static void analyze_ipdominators(struct compile_state *state,
16036 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016037{
16038 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016039 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016040 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016041 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016042 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016043 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016044 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016045 print_ipdominance_frontiers(state, state->dbgout, bb);
16046 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016047 }
16048}
16049
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016050static int bdominates(struct compile_state *state,
16051 struct block *dom, struct block *sub)
16052{
16053 while(sub && (sub != dom)) {
16054 sub = sub->idom;
16055 }
16056 return sub == dom;
16057}
16058
16059static int tdominates(struct compile_state *state,
16060 struct triple *dom, struct triple *sub)
16061{
16062 struct block *bdom, *bsub;
16063 int result;
16064 bdom = block_of_triple(state, dom);
16065 bsub = block_of_triple(state, sub);
16066 if (bdom != bsub) {
16067 result = bdominates(state, bdom, bsub);
16068 }
16069 else {
16070 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016071 if (!bdom || !bsub) {
16072 internal_error(state, dom, "huh?");
16073 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016074 ins = sub;
16075 while((ins != bsub->first) && (ins != dom)) {
16076 ins = ins->prev;
16077 }
16078 result = (ins == dom);
16079 }
16080 return result;
16081}
16082
Eric Biederman90089602004-05-28 14:11:54 +000016083static void analyze_basic_blocks(
16084 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016085{
Eric Biederman90089602004-05-28 14:11:54 +000016086 setup_basic_blocks(state, bb);
16087 analyze_idominators(state, bb);
16088 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016089}
16090
Eric Biedermanb138ac82003-04-22 18:44:01 +000016091static void insert_phi_operations(struct compile_state *state)
16092{
16093 size_t size;
16094 struct triple *first;
16095 int *has_already, *work;
16096 struct block *work_list, **work_list_tail;
16097 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016098 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016099
Eric Biederman90089602004-05-28 14:11:54 +000016100 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016101 has_already = xcmalloc(size, "has_already");
16102 work = xcmalloc(size, "work");
16103 iter = 0;
16104
Eric Biederman83b991a2003-10-11 06:20:25 +000016105 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016106 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016107 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016108 struct triple_set *user, *unext;
16109 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016110
16111 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016112 continue;
16113 }
Eric Biederman90089602004-05-28 14:11:54 +000016114
Eric Biedermanb138ac82003-04-22 18:44:01 +000016115 iter += 1;
16116 work_list = 0;
16117 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016118 for(user = var->use; user; user = unext) {
16119 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016120 if (MISC(var, 0) == user->member) {
16121 continue;
16122 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016123 if (user->member->op == OP_READ) {
16124 continue;
16125 }
16126 if (user->member->op != OP_WRITE) {
16127 internal_error(state, user->member,
16128 "bad variable access");
16129 }
16130 block = user->member->u.block;
16131 if (!block) {
16132 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016133 release_triple(state, user->member);
16134 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016135 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016136 if (work[block->vertex] >= iter) {
16137 continue;
16138 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016139 work[block->vertex] = iter;
16140 *work_list_tail = block;
16141 block->work_next = 0;
16142 work_list_tail = &block->work_next;
16143 }
16144 for(block = work_list; block; block = block->work_next) {
16145 struct block_set *df;
16146 for(df = block->domfrontier; df; df = df->next) {
16147 struct triple *phi;
16148 struct block *front;
16149 int in_edges;
16150 front = df->member;
16151
16152 if (has_already[front->vertex] >= iter) {
16153 continue;
16154 }
16155 /* Count how many edges flow into this block */
16156 in_edges = front->users;
16157 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016158 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016159 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016160 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016161 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016162 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016163 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016164 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016165#if 1
16166 if (phi->rhs != in_edges) {
16167 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16168 phi->rhs, in_edges);
16169 }
16170#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016171 /* Insert the phi functions immediately after the label */
16172 insert_triple(state, front->first->next, phi);
16173 if (front->first == front->last) {
16174 front->last = front->first->next;
16175 }
16176 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016177 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016178
Eric Biedermanb138ac82003-04-22 18:44:01 +000016179 /* If necessary plan to visit the basic block */
16180 if (work[front->vertex] >= iter) {
16181 continue;
16182 }
16183 work[front->vertex] = iter;
16184 *work_list_tail = front;
16185 front->work_next = 0;
16186 work_list_tail = &front->work_next;
16187 }
16188 }
16189 }
16190 xfree(has_already);
16191 xfree(work);
16192}
16193
Eric Biederman66fe2222003-07-04 15:14:04 +000016194
Eric Biederman83b991a2003-10-11 06:20:25 +000016195struct stack {
16196 struct triple_set *top;
16197 unsigned orig_id;
16198};
16199
Eric Biederman90089602004-05-28 14:11:54 +000016200static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016201{
16202 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016203 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016204 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016205 ins = first;
16206 do {
Eric Biederman90089602004-05-28 14:11:54 +000016207 if (triple_is_auto_var(state, ins)) {
16208 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016209 }
16210 ins = ins->next;
16211 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016212 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016213}
16214
Eric Biederman90089602004-05-28 14:11:54 +000016215static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016216{
16217 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016218 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016219 first = state->first;
16220 ins = first;
16221 do {
Eric Biederman90089602004-05-28 14:11:54 +000016222 if (triple_is_auto_var(state, ins)) {
16223 auto_vars += 1;
16224 stacks[auto_vars].orig_id = ins->id;
16225 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016226 }
16227 ins = ins->next;
16228 } while(ins != first);
16229}
16230
Eric Biederman90089602004-05-28 14:11:54 +000016231static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016232{
16233 struct triple *first, *ins;
16234 first = state->first;
16235 ins = first;
16236 do {
Eric Biederman90089602004-05-28 14:11:54 +000016237 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016238 ins->id = stacks[ins->id].orig_id;
16239 }
16240 ins = ins->next;
16241 } while(ins != first);
16242}
16243
16244static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016245{
16246 struct triple_set *head;
16247 struct triple *top_val;
16248 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016249 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016250 if (head) {
16251 top_val = head->member;
16252 }
16253 return top_val;
16254}
16255
Eric Biederman83b991a2003-10-11 06:20:25 +000016256static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016257{
16258 struct triple_set *new;
16259 /* Append new to the head of the list,
16260 * it's the only sensible behavoir for a stack.
16261 */
16262 new = xcmalloc(sizeof(*new), "triple_set");
16263 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016264 new->next = stacks[var->id].top;
16265 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016266}
16267
Eric Biederman83b991a2003-10-11 06:20:25 +000016268static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016269{
16270 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016271 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016272 while(*ptr) {
16273 set = *ptr;
16274 if (set->member == oldval) {
16275 *ptr = set->next;
16276 xfree(set);
16277 /* Only free one occurance from the stack */
16278 return;
16279 }
16280 else {
16281 ptr = &set->next;
16282 }
16283 }
16284}
16285
Eric Biedermanb138ac82003-04-22 18:44:01 +000016286/*
16287 * C(V)
16288 * S(V)
16289 */
16290static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016291 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016292{
16293 struct block_set *set;
16294 struct triple *ptr;
16295 int edge;
16296 if (!parent || !block)
16297 return;
16298 /* Find the edge I am coming in on */
16299 edge = 0;
16300 for(set = block->use; set; set = set->next, edge++) {
16301 if (set->member == parent) {
16302 break;
16303 }
16304 }
16305 if (!set) {
16306 internal_error(state, 0, "phi input is not on a control predecessor");
16307 }
16308 for(ptr = block->first; ; ptr = ptr->next) {
16309 if (ptr->op == OP_PHI) {
16310 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016311 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016312 if (!var) {
16313 internal_error(state, ptr, "no var???");
16314 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016315 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016316 val = peek_triple(stacks, var);
16317 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016318 internal_error(state, val, "bad value in phi");
16319 }
Eric Biederman90089602004-05-28 14:11:54 +000016320 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016321 internal_error(state, ptr, "edges > phi rhs");
16322 }
16323 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016324 if ((*slot != 0) && (*slot != val)) {
16325 internal_error(state, ptr, "phi already bound on this edge");
16326 }
16327 *slot = val;
16328 use_triple(val, ptr);
16329 }
16330 if (ptr == block->last) {
16331 break;
16332 }
16333 }
16334}
16335
16336
16337static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016338 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016339{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016340 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016341 struct triple *ptr, *next, *last;
16342 int done;
16343 if (!block)
16344 return;
16345 last = block->first;
16346 done = 0;
16347 for(ptr = block->first; !done; ptr = next) {
16348 next = ptr->next;
16349 if (ptr == block->last) {
16350 done = 1;
16351 }
16352 /* RHS(A) */
16353 if (ptr->op == OP_READ) {
16354 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016355 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016356 if (!triple_is_auto_var(state, var)) {
16357 internal_error(state, ptr, "read of non auto var!");
16358 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016359 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016360 /* Find the current value of the variable */
16361 val = peek_triple(stacks, var);
16362 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016363 /* Let the optimizer at variables that are not initially
16364 * set. But give it a bogus value so things seem to
16365 * work by accident. This is useful for bitfields because
16366 * setting them always involves a read-modify-write.
16367 */
16368 if (TYPE_ARITHMETIC(ptr->type->type)) {
16369 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16370 val->u.cval = 0xdeadbeaf;
16371 } else {
16372 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16373 }
16374 }
16375 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016376 error(state, ptr, "variable used without being set");
16377 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016378 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16379 internal_error(state, val, "bad value in read");
16380 }
16381 propogate_use(state, ptr, val);
16382 release_triple(state, ptr);
16383 continue;
16384 }
16385 /* LHS(A) */
16386 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016387 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016388 var = MISC(ptr, 0);
16389 if (!triple_is_auto_var(state, var)) {
16390 internal_error(state, ptr, "write to non auto var!");
16391 }
16392 tval = val = RHS(ptr, 0);
16393 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16394 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016395 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016396 }
Eric Biederman90089602004-05-28 14:11:54 +000016397 /* Insert a cast if the types differ */
16398 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016399 if (val->op == OP_INTCONST) {
16400 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16401 tval->u.cval = val->u.cval;
16402 }
16403 else {
Eric Biederman90089602004-05-28 14:11:54 +000016404 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016405 use_triple(val, tval);
16406 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016407 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016408 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016409 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016410 use_triple(tval, ptr);
16411 }
16412 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016413 unuse_triple(var, ptr);
16414 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016415 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016416 }
16417 if (ptr->op == OP_PHI) {
16418 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016419 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016420 if (!triple_is_auto_var(state, var)) {
16421 internal_error(state, ptr, "phi references non auto var!");
16422 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016423 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016424 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016425 }
16426 last = ptr;
16427 }
16428 block->last = last;
16429
16430 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016431 for(edge = block->edges; edge; edge = edge->next) {
16432 fixup_block_phi_variables(state, stacks, block, edge->member);
16433 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016434 /* rename variables in the dominated nodes */
16435 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016436 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016437 }
16438 /* pop the renamed variable stack */
16439 last = block->first;
16440 done = 0;
16441 for(ptr = block->first; !done ; ptr = next) {
16442 next = ptr->next;
16443 if (ptr == block->last) {
16444 done = 1;
16445 }
16446 if (ptr->op == OP_WRITE) {
16447 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016448 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016449 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016450 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016451 release_triple(state, ptr);
16452 continue;
16453 }
16454 if (ptr->op == OP_PHI) {
16455 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016456 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016457 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016458 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016459 }
16460 last = ptr;
16461 }
16462 block->last = last;
16463}
16464
Eric Biederman83b991a2003-10-11 06:20:25 +000016465static void rename_variables(struct compile_state *state)
16466{
16467 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016468 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016469
16470 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016471 auto_vars = count_auto_vars(state);
16472 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016473
Eric Biederman90089602004-05-28 14:11:54 +000016474 /* Give each auto_var a stack */
16475 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016476
16477 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016478 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016479
Eric Biederman90089602004-05-28 14:11:54 +000016480 /* Remove the stacks from the auto_vars */
16481 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016482 xfree(stacks);
16483}
16484
Eric Biedermanb138ac82003-04-22 18:44:01 +000016485static void prune_block_variables(struct compile_state *state,
16486 struct block *block)
16487{
16488 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016489 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016490 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016491
Eric Biedermanb138ac82003-04-22 18:44:01 +000016492 done = 0;
16493 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016494 /* Be extremely careful I am deleting the list
16495 * as I walk trhough it.
16496 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016497 next = ptr->next;
16498 if (ptr == block->last) {
16499 done = 1;
16500 }
Eric Biederman90089602004-05-28 14:11:54 +000016501 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016502 struct triple_set *user, *next;
16503 for(user = ptr->use; user; user = next) {
16504 struct triple *use;
16505 next = user->next;
16506 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016507 if (MISC(ptr, 0) == user->member) {
16508 continue;
16509 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016510 if (use->op != OP_PHI) {
16511 internal_error(state, use, "decl still used");
16512 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016513 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016514 internal_error(state, use, "bad phi use of decl");
16515 }
16516 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016517 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016518 }
Eric Biederman90089602004-05-28 14:11:54 +000016519 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16520 /* Delete the adecl */
16521 release_triple(state, MISC(ptr, 0));
16522 /* And the piece */
16523 release_triple(state, ptr);
16524 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016525 continue;
16526 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016527 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016528 for(user = block->idominates; user; user = user->next) {
16529 prune_block_variables(state, user->member);
16530 }
16531}
16532
Eric Biederman66fe2222003-07-04 15:14:04 +000016533struct phi_triple {
16534 struct triple *phi;
16535 unsigned orig_id;
16536 int alive;
16537};
16538
16539static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16540{
16541 struct triple **slot;
16542 int zrhs, i;
16543 if (live[phi->id].alive) {
16544 return;
16545 }
16546 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016547 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016548 slot = &RHS(phi, 0);
16549 for(i = 0; i < zrhs; i++) {
16550 struct triple *used;
16551 used = slot[i];
16552 if (used && (used->op == OP_PHI)) {
16553 keep_phi(state, live, used);
16554 }
16555 }
16556}
16557
16558static void prune_unused_phis(struct compile_state *state)
16559{
16560 struct triple *first, *phi;
16561 struct phi_triple *live;
16562 int phis, i;
16563
Eric Biederman66fe2222003-07-04 15:14:04 +000016564 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016565 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016566
16567 /* Count how many phi functions I need to process */
16568 phis = 0;
16569 for(phi = first->next; phi != first; phi = phi->next) {
16570 if (phi->op == OP_PHI) {
16571 phis += 1;
16572 }
16573 }
16574
16575 /* Mark them all dead */
16576 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16577 phis = 0;
16578 for(phi = first->next; phi != first; phi = phi->next) {
16579 if (phi->op != OP_PHI) {
16580 continue;
16581 }
16582 live[phis].alive = 0;
16583 live[phis].orig_id = phi->id;
16584 live[phis].phi = phi;
16585 phi->id = phis;
16586 phis += 1;
16587 }
16588
16589 /* Mark phis alive that are used by non phis */
16590 for(i = 0; i < phis; i++) {
16591 struct triple_set *set;
16592 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16593 if (set->member->op != OP_PHI) {
16594 keep_phi(state, live, live[i].phi);
16595 break;
16596 }
16597 }
16598 }
16599
16600 /* Delete the extraneous phis */
16601 for(i = 0; i < phis; i++) {
16602 struct triple **slot;
16603 int zrhs, j;
16604 if (!live[i].alive) {
16605 release_triple(state, live[i].phi);
16606 continue;
16607 }
16608 phi = live[i].phi;
16609 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016610 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016611 for(j = 0; j < zrhs; j++) {
16612 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016613 struct triple *unknown;
16614 get_occurance(phi->occurance);
16615 unknown = flatten(state, state->global_pool,
16616 alloc_triple(state, OP_UNKNOWNVAL,
16617 phi->type, 0, 0, phi->occurance));
16618 slot[j] = unknown;
16619 use_triple(unknown, phi);
16620 transform_to_arch_instruction(state, unknown);
16621#if 0
16622 warning(state, phi, "variable not set at index %d on all paths to use", j);
16623#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016624 }
16625 }
16626 }
16627 xfree(live);
16628}
16629
Eric Biedermanb138ac82003-04-22 18:44:01 +000016630static void transform_to_ssa_form(struct compile_state *state)
16631{
16632 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016633 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016634
Eric Biederman90089602004-05-28 14:11:54 +000016635 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016636 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016637
Eric Biederman90089602004-05-28 14:11:54 +000016638 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016639}
16640
16641
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016642static void clear_vertex(
16643 struct compile_state *state, struct block *block, void *arg)
16644{
Eric Biederman83b991a2003-10-11 06:20:25 +000016645 /* Clear the current blocks vertex and the vertex of all
16646 * of the current blocks neighbors in case there are malformed
16647 * blocks with now instructions at this point.
16648 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016649 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016650 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016651 for(edge = block->edges; edge; edge = edge->next) {
16652 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016653 }
16654 for(user = block->use; user; user = user->next) {
16655 user->member->vertex = 0;
16656 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016657}
16658
16659static void mark_live_block(
16660 struct compile_state *state, struct block *block, int *next_vertex)
16661{
16662 /* See if this is a block that has not been marked */
16663 if (block->vertex != 0) {
16664 return;
16665 }
16666 block->vertex = *next_vertex;
16667 *next_vertex += 1;
16668 if (triple_is_branch(state, block->last)) {
16669 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016670 targ = triple_edge_targ(state, block->last, 0);
16671 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016672 if (!*targ) {
16673 continue;
16674 }
16675 if (!triple_stores_block(state, *targ)) {
16676 internal_error(state, 0, "bad targ");
16677 }
16678 mark_live_block(state, (*targ)->u.block, next_vertex);
16679 }
Eric Biederman90089602004-05-28 14:11:54 +000016680 /* Ensure the last block of a function remains alive */
16681 if (triple_is_call(state, block->last)) {
16682 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16683 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016684 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016685 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016686 struct triple *ins;
16687 ins = block->last->next;
16688 if (!triple_stores_block(state, ins)) {
16689 internal_error(state, 0, "bad block start");
16690 }
16691 mark_live_block(state, ins->u.block, next_vertex);
16692 }
16693}
16694
Eric Biedermanb138ac82003-04-22 18:44:01 +000016695static void transform_from_ssa_form(struct compile_state *state)
16696{
16697 /* To get out of ssa form we insert moves on the incoming
16698 * edges to blocks containting phi functions.
16699 */
16700 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016701 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016702 int next_vertex;
16703
16704 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016705 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016706 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016707 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016708
16709 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016710 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016711 for(phi = first->next; phi != first ; phi = next) {
16712 struct block_set *set;
16713 struct block *block;
16714 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016715 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016716 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016717 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016718 next = phi->next;
16719 if (phi->op != OP_PHI) {
16720 continue;
16721 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016722
Eric Biedermanb138ac82003-04-22 18:44:01 +000016723 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016724 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016725
Eric Biederman83b991a2003-10-11 06:20:25 +000016726 /* If this phi is in a dead block just forget it */
16727 if (block->vertex == 0) {
16728 release_triple(state, phi);
16729 continue;
16730 }
16731
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016732 /* Forget uses from code in dead blocks */
16733 for(use = phi->use; use; use = use_next) {
16734 struct block *ublock;
16735 struct triple **expr;
16736 use_next = use->next;
16737 ublock = block_of_triple(state, use->member);
16738 if ((use->member == phi) || (ublock->vertex != 0)) {
16739 continue;
16740 }
16741 expr = triple_rhs(state, use->member, 0);
16742 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16743 if (*expr == phi) {
16744 *expr = 0;
16745 }
16746 }
16747 unuse_triple(phi, use->member);
16748 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016749 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016750 if (registers_of(state, phi->type) != 1) {
16751 internal_error(state, phi, "phi->type does not fit in a single register!");
16752 }
16753 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16754 var = var->next; /* point at the var */
16755
Eric Biederman83b991a2003-10-11 06:20:25 +000016756 /* Replaces use of phi with var */
16757 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016758
Eric Biederman90089602004-05-28 14:11:54 +000016759 /* Count the readers */
16760 readers = 0;
16761 for(use = var->use; use; use = use->next) {
16762 if (use->member != MISC(var, 0)) {
16763 readers++;
16764 }
16765 }
16766
Eric Biedermanb138ac82003-04-22 18:44:01 +000016767 /* Walk all of the incoming edges/blocks and insert moves.
16768 */
Eric Biederman90089602004-05-28 14:11:54 +000016769 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016770 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016771 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016772 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016773 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016774 eblock = set->member;
16775 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016776 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016777 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016778 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016779
Eric Biederman83b991a2003-10-11 06:20:25 +000016780 /* If we don't have a value that belongs in an OP_WRITE
16781 * continue on.
16782 */
Eric Biederman90089602004-05-28 14:11:54 +000016783 if (!val || (val == &unknown_triple) || (val == phi)
16784 || (vblock && (vblock->vertex == 0))) {
16785 continue;
16786 }
16787 /* If the value should never occur error */
16788 if (!vblock) {
16789 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016790 continue;
16791 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016792
16793 /* If the value occurs in a dead block see if a replacement
16794 * block can be found.
16795 */
16796 while(eblock && (eblock->vertex == 0)) {
16797 eblock = eblock->idom;
16798 }
16799 /* If not continue on with the next value. */
16800 if (!eblock || (eblock->vertex == 0)) {
16801 continue;
16802 }
16803
16804 /* If we have an empty incoming block ignore it. */
16805 if (!eblock->first) {
16806 internal_error(state, 0, "empty block?");
16807 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016808
Eric Biederman530b5192003-07-01 10:05:30 +000016809 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016810 /* Walk through the edge block backwards to find an
16811 * appropriate location for the OP_WRITE.
16812 */
16813 for(base = eblock->last; base != eblock->first; base = base->prev) {
16814 struct triple **expr;
16815 if (base->op == OP_PIECE) {
16816 base = MISC(base, 0);
16817 }
16818 if ((base == var) || (base == val)) {
16819 goto out;
16820 }
16821 expr = triple_lhs(state, base, 0);
16822 for(; expr; expr = triple_lhs(state, base, expr)) {
16823 if ((*expr) == val) {
16824 goto out;
16825 }
16826 }
16827 expr = triple_rhs(state, base, 0);
16828 for(; expr; expr = triple_rhs(state, base, expr)) {
16829 if ((*expr) == var) {
16830 goto out;
16831 }
16832 }
Eric Biederman530b5192003-07-01 10:05:30 +000016833 }
Eric Biederman90089602004-05-28 14:11:54 +000016834 out:
16835 if (triple_is_branch(state, base)) {
16836 internal_error(state, base,
16837 "Could not insert write to phi");
16838 }
16839 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016840 use_triple(val, move);
16841 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016842 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016843 }
Eric Biederman90089602004-05-28 14:11:54 +000016844 if (!writers && readers) {
16845 internal_error(state, var, "no value written to in use phi?");
16846 }
16847 /* If var is not used free it */
16848 if (!writers) {
16849 release_triple(state, MISC(var, 0));
16850 release_triple(state, var);
16851 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016852 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016853 release_triple(state, phi);
16854 }
16855
Eric Biederman83b991a2003-10-11 06:20:25 +000016856 /* Walk all of the operations to find the adecls */
16857 for(var = first->next; var != first ; var = var->next) {
16858 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016859 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016860 continue;
16861 }
16862
16863 /* Walk through all of the rhs uses of var and
16864 * replace them with read of var.
16865 */
16866 for(use = var->use; use; use = use_next) {
16867 struct triple *read, *user;
16868 struct triple **slot;
16869 int zrhs, i, used;
16870 use_next = use->next;
16871 user = use->member;
16872
16873 /* Generate a read of var */
16874 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16875 use_triple(var, read);
16876
16877 /* Find the rhs uses and see if they need to be replaced */
16878 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016879 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016880 slot = &RHS(user, 0);
16881 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016882 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016883 slot[i] = read;
16884 used = 1;
16885 }
16886 }
16887 /* If we did use it cleanup the uses */
16888 if (used) {
16889 unuse_triple(var, user);
16890 use_triple(read, user);
16891 }
16892 /* If we didn't use it release the extra triple */
16893 else {
16894 release_triple(state, read);
16895 }
16896 }
16897 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016898}
16899
Eric Biederman5ade04a2003-10-22 04:03:46 +000016900#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016901 FILE *fp = state->dbgout; \
16902 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Eric Biederman5ade04a2003-10-22 04:03:46 +000016903 }
16904
Eric Biederman83b991a2003-10-11 06:20:25 +000016905static void rebuild_ssa_form(struct compile_state *state)
16906{
16907HI();
16908 transform_from_ssa_form(state);
16909HI();
Eric Biederman90089602004-05-28 14:11:54 +000016910 state->bb.first = state->first;
16911 free_basic_blocks(state, &state->bb);
16912 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016913HI();
16914 insert_phi_operations(state);
16915HI();
16916 rename_variables(state);
16917HI();
16918
Eric Biederman90089602004-05-28 14:11:54 +000016919 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016920HI();
16921 prune_unused_phis(state);
16922HI();
16923}
16924#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016925
16926/*
16927 * Register conflict resolution
16928 * =========================================================
16929 */
16930
16931static struct reg_info find_def_color(
16932 struct compile_state *state, struct triple *def)
16933{
16934 struct triple_set *set;
16935 struct reg_info info;
16936 info.reg = REG_UNSET;
16937 info.regcm = 0;
16938 if (!triple_is_def(state, def)) {
16939 return info;
16940 }
16941 info = arch_reg_lhs(state, def, 0);
16942 if (info.reg >= MAX_REGISTERS) {
16943 info.reg = REG_UNSET;
16944 }
16945 for(set = def->use; set; set = set->next) {
16946 struct reg_info tinfo;
16947 int i;
16948 i = find_rhs_use(state, set->member, def);
16949 if (i < 0) {
16950 continue;
16951 }
16952 tinfo = arch_reg_rhs(state, set->member, i);
16953 if (tinfo.reg >= MAX_REGISTERS) {
16954 tinfo.reg = REG_UNSET;
16955 }
16956 if ((tinfo.reg != REG_UNSET) &&
16957 (info.reg != REG_UNSET) &&
16958 (tinfo.reg != info.reg)) {
16959 internal_error(state, def, "register conflict");
16960 }
16961 if ((info.regcm & tinfo.regcm) == 0) {
16962 internal_error(state, def, "regcm conflict %x & %x == 0",
16963 info.regcm, tinfo.regcm);
16964 }
16965 if (info.reg == REG_UNSET) {
16966 info.reg = tinfo.reg;
16967 }
16968 info.regcm &= tinfo.regcm;
16969 }
16970 if (info.reg >= MAX_REGISTERS) {
16971 internal_error(state, def, "register out of range");
16972 }
16973 return info;
16974}
16975
16976static struct reg_info find_lhs_pre_color(
16977 struct compile_state *state, struct triple *ins, int index)
16978{
16979 struct reg_info info;
16980 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016981 zrhs = ins->rhs;
16982 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016983 if (!zlhs && triple_is_def(state, ins)) {
16984 zlhs = 1;
16985 }
16986 if (index >= zlhs) {
16987 internal_error(state, ins, "Bad lhs %d", index);
16988 }
16989 info = arch_reg_lhs(state, ins, index);
16990 for(i = 0; i < zrhs; i++) {
16991 struct reg_info rinfo;
16992 rinfo = arch_reg_rhs(state, ins, i);
16993 if ((info.reg == rinfo.reg) &&
16994 (rinfo.reg >= MAX_REGISTERS)) {
16995 struct reg_info tinfo;
16996 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
16997 info.reg = tinfo.reg;
16998 info.regcm &= tinfo.regcm;
16999 break;
17000 }
17001 }
17002 if (info.reg >= MAX_REGISTERS) {
17003 info.reg = REG_UNSET;
17004 }
17005 return info;
17006}
17007
17008static struct reg_info find_rhs_post_color(
17009 struct compile_state *state, struct triple *ins, int index);
17010
17011static struct reg_info find_lhs_post_color(
17012 struct compile_state *state, struct triple *ins, int index)
17013{
17014 struct triple_set *set;
17015 struct reg_info info;
17016 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017017#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017018 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017019 ins, index);
17020#endif
17021 if ((index == 0) && triple_is_def(state, ins)) {
17022 lhs = ins;
17023 }
Eric Biederman90089602004-05-28 14:11:54 +000017024 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017025 lhs = LHS(ins, index);
17026 }
17027 else {
17028 internal_error(state, ins, "Bad lhs %d", index);
17029 lhs = 0;
17030 }
17031 info = arch_reg_lhs(state, ins, index);
17032 if (info.reg >= MAX_REGISTERS) {
17033 info.reg = REG_UNSET;
17034 }
17035 for(set = lhs->use; set; set = set->next) {
17036 struct reg_info rinfo;
17037 struct triple *user;
17038 int zrhs, i;
17039 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017040 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017041 for(i = 0; i < zrhs; i++) {
17042 if (RHS(user, i) != lhs) {
17043 continue;
17044 }
17045 rinfo = find_rhs_post_color(state, user, i);
17046 if ((info.reg != REG_UNSET) &&
17047 (rinfo.reg != REG_UNSET) &&
17048 (info.reg != rinfo.reg)) {
17049 internal_error(state, ins, "register conflict");
17050 }
17051 if ((info.regcm & rinfo.regcm) == 0) {
17052 internal_error(state, ins, "regcm conflict %x & %x == 0",
17053 info.regcm, rinfo.regcm);
17054 }
17055 if (info.reg == REG_UNSET) {
17056 info.reg = rinfo.reg;
17057 }
17058 info.regcm &= rinfo.regcm;
17059 }
17060 }
Eric Biederman530b5192003-07-01 10:05:30 +000017061#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017062 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017063 ins, index, info.reg, info.regcm);
17064#endif
17065 return info;
17066}
17067
17068static struct reg_info find_rhs_post_color(
17069 struct compile_state *state, struct triple *ins, int index)
17070{
17071 struct reg_info info, rinfo;
17072 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017073#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017074 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017075 ins, index);
17076#endif
17077 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017078 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017079 if (!zlhs && triple_is_def(state, ins)) {
17080 zlhs = 1;
17081 }
17082 info = rinfo;
17083 if (info.reg >= MAX_REGISTERS) {
17084 info.reg = REG_UNSET;
17085 }
17086 for(i = 0; i < zlhs; i++) {
17087 struct reg_info linfo;
17088 linfo = arch_reg_lhs(state, ins, i);
17089 if ((linfo.reg == rinfo.reg) &&
17090 (linfo.reg >= MAX_REGISTERS)) {
17091 struct reg_info tinfo;
17092 tinfo = find_lhs_post_color(state, ins, i);
17093 if (tinfo.reg >= MAX_REGISTERS) {
17094 tinfo.reg = REG_UNSET;
17095 }
Eric Biederman530b5192003-07-01 10:05:30 +000017096 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017097 info.regcm &= tinfo.regcm;
17098 if (info.reg != REG_UNSET) {
17099 internal_error(state, ins, "register conflict");
17100 }
17101 if (info.regcm == 0) {
17102 internal_error(state, ins, "regcm conflict");
17103 }
17104 info.reg = tinfo.reg;
17105 }
17106 }
Eric Biederman530b5192003-07-01 10:05:30 +000017107#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017108 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017109 ins, index, info.reg, info.regcm);
17110#endif
17111 return info;
17112}
17113
17114static struct reg_info find_lhs_color(
17115 struct compile_state *state, struct triple *ins, int index)
17116{
17117 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017118#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017119 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017120 ins, index);
17121#endif
17122 pre = find_lhs_pre_color(state, ins, index);
17123 post = find_lhs_post_color(state, ins, index);
17124 if ((pre.reg != post.reg) &&
17125 (pre.reg != REG_UNSET) &&
17126 (post.reg != REG_UNSET)) {
17127 internal_error(state, ins, "register conflict");
17128 }
17129 info.regcm = pre.regcm & post.regcm;
17130 info.reg = pre.reg;
17131 if (info.reg == REG_UNSET) {
17132 info.reg = post.reg;
17133 }
Eric Biederman530b5192003-07-01 10:05:30 +000017134#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017135 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017136 ins, index, info.reg, info.regcm,
17137 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017138#endif
17139 return info;
17140}
17141
17142static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17143{
17144 struct triple_set *entry, *next;
17145 struct triple *out;
17146 struct reg_info info, rinfo;
17147
17148 info = arch_reg_lhs(state, ins, 0);
17149 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17150 use_triple(RHS(out, 0), out);
17151 /* Get the users of ins to use out instead */
17152 for(entry = ins->use; entry; entry = next) {
17153 int i;
17154 next = entry->next;
17155 if (entry->member == out) {
17156 continue;
17157 }
17158 i = find_rhs_use(state, entry->member, ins);
17159 if (i < 0) {
17160 continue;
17161 }
17162 rinfo = arch_reg_rhs(state, entry->member, i);
17163 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17164 continue;
17165 }
17166 replace_rhs_use(state, ins, out, entry->member);
17167 }
17168 transform_to_arch_instruction(state, out);
17169 return out;
17170}
17171
Eric Biedermand1ea5392003-06-28 06:49:45 +000017172static struct triple *typed_pre_copy(
17173 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017174{
17175 /* Carefully insert enough operations so that I can
17176 * enter any operation with a GPR32.
17177 */
17178 struct triple *in;
17179 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017180 unsigned classes;
17181 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017182 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017183 if (ins->op == OP_PHI) {
17184 internal_error(state, ins, "pre_copy on a phi?");
17185 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017186 classes = arch_type_to_regcm(state, type);
17187 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017188 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017189 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017190 FILE *fp = state->errout;
17191 fprintf(fp, "src_type: ");
17192 name_of(fp, ins->type);
17193 fprintf(fp, "\ndst_type: ");
17194 name_of(fp, type);
17195 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017196 internal_error(state, ins, "pre_copy with no register classes");
17197 }
Eric Biederman90089602004-05-28 14:11:54 +000017198 op = OP_COPY;
17199 if (!equiv_types(type, (*expr)->type)) {
17200 op = OP_CONVERT;
17201 }
17202 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017203 unuse_triple(*expr, ins);
17204 *expr = in;
17205 use_triple(RHS(in, 0), in);
17206 use_triple(in, ins);
17207 transform_to_arch_instruction(state, in);
17208 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017209
17210}
17211static struct triple *pre_copy(
17212 struct compile_state *state, struct triple *ins, int index)
17213{
17214 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017215}
17216
17217
Eric Biedermanb138ac82003-04-22 18:44:01 +000017218static void insert_copies_to_phi(struct compile_state *state)
17219{
17220 /* To get out of ssa form we insert moves on the incoming
17221 * edges to blocks containting phi functions.
17222 */
17223 struct triple *first;
17224 struct triple *phi;
17225
17226 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017227 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017228 for(phi = first->next; phi != first ; phi = phi->next) {
17229 struct block_set *set;
17230 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017231 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017232 int edge;
17233 if (phi->op != OP_PHI) {
17234 continue;
17235 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017236 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017237 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017238 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017239 /* Phi's that feed into mandatory live range joins
17240 * cause nasty complications. Insert a copy of
17241 * the phi value so I never have to deal with
17242 * that in the rest of the code.
17243 */
17244 copy = post_copy(state, phi);
17245 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017246 /* Walk all of the incoming edges/blocks and insert moves.
17247 */
17248 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17249 struct block *eblock;
17250 struct triple *move;
17251 struct triple *val;
17252 struct triple *ptr;
17253 eblock = set->member;
17254 val = slot[edge];
17255
17256 if (val == phi) {
17257 continue;
17258 }
17259
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017260 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017261 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017262 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017263 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017264 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017265 use_triple(val, move);
17266
17267 slot[edge] = move;
17268 unuse_triple(val, phi);
17269 use_triple(move, phi);
17270
Eric Biederman66fe2222003-07-04 15:14:04 +000017271 /* Walk up the dominator tree until I have found the appropriate block */
17272 while(eblock && !tdominates(state, val, eblock->last)) {
17273 eblock = eblock->idom;
17274 }
17275 if (!eblock) {
17276 internal_error(state, phi, "Cannot find block dominated by %p",
17277 val);
17278 }
17279
Eric Biedermanb138ac82003-04-22 18:44:01 +000017280 /* Walk through the block backwards to find
17281 * an appropriate location for the OP_COPY.
17282 */
17283 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17284 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017285 if (ptr->op == OP_PIECE) {
17286 ptr = MISC(ptr, 0);
17287 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017288 if ((ptr == phi) || (ptr == val)) {
17289 goto out;
17290 }
Eric Biederman90089602004-05-28 14:11:54 +000017291 expr = triple_lhs(state, ptr, 0);
17292 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17293 if ((*expr) == val) {
17294 goto out;
17295 }
17296 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017297 expr = triple_rhs(state, ptr, 0);
17298 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17299 if ((*expr) == phi) {
17300 goto out;
17301 }
17302 }
17303 }
17304 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017305 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017306 internal_error(state, ptr,
17307 "Could not insert write to phi");
17308 }
Eric Biederman90089602004-05-28 14:11:54 +000017309 insert_triple(state, after_lhs(state, ptr), move);
17310 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017311 eblock->last = move;
17312 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017313 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017314 }
17315 }
Eric Biederman90089602004-05-28 14:11:54 +000017316 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017317}
17318
Eric Biederman90089602004-05-28 14:11:54 +000017319struct triple_reg_set;
17320struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017321
Eric Biedermanb138ac82003-04-22 18:44:01 +000017322
17323static int do_triple_set(struct triple_reg_set **head,
17324 struct triple *member, struct triple *new_member)
17325{
17326 struct triple_reg_set **ptr, *new;
17327 if (!member)
17328 return 0;
17329 ptr = head;
17330 while(*ptr) {
17331 if ((*ptr)->member == member) {
17332 return 0;
17333 }
17334 ptr = &(*ptr)->next;
17335 }
17336 new = xcmalloc(sizeof(*new), "triple_set");
17337 new->member = member;
17338 new->new = new_member;
17339 new->next = *head;
17340 *head = new;
17341 return 1;
17342}
17343
17344static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17345{
17346 struct triple_reg_set *entry, **ptr;
17347 ptr = head;
17348 while(*ptr) {
17349 entry = *ptr;
17350 if (entry->member == member) {
17351 *ptr = entry->next;
17352 xfree(entry);
17353 return;
17354 }
17355 else {
17356 ptr = &entry->next;
17357 }
17358 }
17359}
17360
17361static int in_triple(struct reg_block *rb, struct triple *in)
17362{
17363 return do_triple_set(&rb->in, in, 0);
17364}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017365
17366#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017367static void unin_triple(struct reg_block *rb, struct triple *unin)
17368{
17369 do_triple_unset(&rb->in, unin);
17370}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017371#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017372
17373static int out_triple(struct reg_block *rb, struct triple *out)
17374{
17375 return do_triple_set(&rb->out, out, 0);
17376}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017377#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017378static void unout_triple(struct reg_block *rb, struct triple *unout)
17379{
17380 do_triple_unset(&rb->out, unout);
17381}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017382#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017383
17384static int initialize_regblock(struct reg_block *blocks,
17385 struct block *block, int vertex)
17386{
17387 struct block_set *user;
17388 if (!block || (blocks[block->vertex].block == block)) {
17389 return vertex;
17390 }
17391 vertex += 1;
17392 /* Renumber the blocks in a convinient fashion */
17393 block->vertex = vertex;
17394 blocks[vertex].block = block;
17395 blocks[vertex].vertex = vertex;
17396 for(user = block->use; user; user = user->next) {
17397 vertex = initialize_regblock(blocks, user->member, vertex);
17398 }
17399 return vertex;
17400}
17401
Eric Biederman90089602004-05-28 14:11:54 +000017402static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17403{
17404/* Part to piece is a best attempt and it cannot be correct all by
17405 * itself. If various values are read as different sizes in different
17406 * parts of the code this function cannot work. Or rather it cannot
17407 * work in conjunction with compute_variable_liftimes. As the
17408 * analysis will get confused.
17409 */
17410 struct triple *base;
17411 unsigned reg;
17412 if (!is_lvalue(state, ins)) {
17413 return ins;
17414 }
17415 base = 0;
17416 reg = 0;
17417 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17418 base = MISC(ins, 0);
17419 switch(ins->op) {
17420 case OP_INDEX:
17421 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17422 break;
17423 case OP_DOT:
17424 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17425 break;
17426 default:
17427 internal_error(state, ins, "unhandled part");
17428 break;
17429 }
17430 ins = base;
17431 }
17432 if (base) {
17433 if (reg > base->lhs) {
17434 internal_error(state, base, "part out of range?");
17435 }
17436 ins = LHS(base, reg);
17437 }
17438 return ins;
17439}
17440
17441static int this_def(struct compile_state *state,
17442 struct triple *ins, struct triple *other)
17443{
17444 if (ins == other) {
17445 return 1;
17446 }
17447 if (ins->op == OP_WRITE) {
17448 ins = part_to_piece(state, MISC(ins, 0));
17449 }
17450 return ins == other;
17451}
17452
Eric Biedermanb138ac82003-04-22 18:44:01 +000017453static int phi_in(struct compile_state *state, struct reg_block *blocks,
17454 struct reg_block *rb, struct block *suc)
17455{
17456 /* Read the conditional input set of a successor block
17457 * (i.e. the input to the phi nodes) and place it in the
17458 * current blocks output set.
17459 */
17460 struct block_set *set;
17461 struct triple *ptr;
17462 int edge;
17463 int done, change;
17464 change = 0;
17465 /* Find the edge I am coming in on */
17466 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17467 if (set->member == rb->block) {
17468 break;
17469 }
17470 }
17471 if (!set) {
17472 internal_error(state, 0, "Not coming on a control edge?");
17473 }
17474 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17475 struct triple **slot, *expr, *ptr2;
17476 int out_change, done2;
17477 done = (ptr == suc->last);
17478 if (ptr->op != OP_PHI) {
17479 continue;
17480 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017481 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017482 expr = slot[edge];
17483 out_change = out_triple(rb, expr);
17484 if (!out_change) {
17485 continue;
17486 }
17487 /* If we don't define the variable also plast it
17488 * in the current blocks input set.
17489 */
17490 ptr2 = rb->block->first;
17491 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017492 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017493 break;
17494 }
17495 done2 = (ptr2 == rb->block->last);
17496 }
17497 if (!done2) {
17498 continue;
17499 }
17500 change |= in_triple(rb, expr);
17501 }
17502 return change;
17503}
17504
17505static int reg_in(struct compile_state *state, struct reg_block *blocks,
17506 struct reg_block *rb, struct block *suc)
17507{
17508 struct triple_reg_set *in_set;
17509 int change;
17510 change = 0;
17511 /* Read the input set of a successor block
17512 * and place it in the current blocks output set.
17513 */
17514 in_set = blocks[suc->vertex].in;
17515 for(; in_set; in_set = in_set->next) {
17516 int out_change, done;
17517 struct triple *first, *last, *ptr;
17518 out_change = out_triple(rb, in_set->member);
17519 if (!out_change) {
17520 continue;
17521 }
17522 /* If we don't define the variable also place it
17523 * in the current blocks input set.
17524 */
17525 first = rb->block->first;
17526 last = rb->block->last;
17527 done = 0;
17528 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017529 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017530 break;
17531 }
17532 done = (ptr == last);
17533 }
17534 if (!done) {
17535 continue;
17536 }
17537 change |= in_triple(rb, in_set->member);
17538 }
17539 change |= phi_in(state, blocks, rb, suc);
17540 return change;
17541}
17542
Eric Biedermanb138ac82003-04-22 18:44:01 +000017543static int use_in(struct compile_state *state, struct reg_block *rb)
17544{
17545 /* Find the variables we use but don't define and add
17546 * it to the current blocks input set.
17547 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017548#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017549#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017550#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017551 struct block *block;
17552 struct triple *ptr;
17553 int done;
17554 int change;
17555 block = rb->block;
17556 change = 0;
17557 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17558 struct triple **expr;
17559 done = (ptr == block->first);
17560 /* The variable a phi function uses depends on the
17561 * control flow, and is handled in phi_in, not
17562 * here.
17563 */
17564 if (ptr->op == OP_PHI) {
17565 continue;
17566 }
17567 expr = triple_rhs(state, ptr, 0);
17568 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17569 struct triple *rhs, *test;
17570 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017571 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017572 if (!rhs) {
17573 continue;
17574 }
Eric Biederman90089602004-05-28 14:11:54 +000017575
17576 /* See if rhs is defined in this block.
17577 * A write counts as a definition.
17578 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017579 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17580 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017581 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017582 rhs = 0;
17583 break;
17584 }
17585 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017586 /* If I still have a valid rhs add it to in */
17587 change |= in_triple(rb, rhs);
17588 }
17589 }
17590 return change;
17591}
17592
17593static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017594 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017595{
17596 struct reg_block *blocks;
17597 int change;
17598 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017599 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17600 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017601 do {
17602 int i;
17603 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017604 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017605 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017606 struct reg_block *rb;
17607 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017608 /* Add the all successor's input set to in */
17609 for(edge = rb->block->edges; edge; edge = edge->next) {
17610 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017611 }
17612 /* Add use to in... */
17613 change |= use_in(state, rb);
17614 }
17615 } while(change);
17616 return blocks;
17617}
17618
Eric Biederman90089602004-05-28 14:11:54 +000017619static void free_variable_lifetimes(struct compile_state *state,
17620 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017621{
17622 int i;
17623 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017624 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017625 struct triple_reg_set *entry, *next;
17626 struct reg_block *rb;
17627 rb = &blocks[i];
17628 for(entry = rb->in; entry ; entry = next) {
17629 next = entry->next;
17630 do_triple_unset(&rb->in, entry->member);
17631 }
17632 for(entry = rb->out; entry; entry = next) {
17633 next = entry->next;
17634 do_triple_unset(&rb->out, entry->member);
17635 }
17636 }
17637 xfree(blocks);
17638
17639}
17640
Eric Biedermanf96a8102003-06-16 16:57:34 +000017641typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000017642 struct compile_state *state,
17643 struct reg_block *blocks, struct triple_reg_set *live,
17644 struct reg_block *rb, struct triple *ins, void *arg);
17645
17646static void walk_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017647 struct basic_blocks *bb, struct reg_block *blocks,
17648 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017649{
17650 int i;
17651
Eric Biederman90089602004-05-28 14:11:54 +000017652 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017653 struct triple_reg_set *live;
17654 struct triple_reg_set *entry, *next;
17655 struct triple *ptr, *prev;
17656 struct reg_block *rb;
17657 struct block *block;
17658 int done;
17659
17660 /* Get the blocks */
17661 rb = &blocks[i];
17662 block = rb->block;
17663
17664 /* Copy out into live */
17665 live = 0;
17666 for(entry = rb->out; entry; entry = next) {
17667 next = entry->next;
17668 do_triple_set(&live, entry->member, entry->new);
17669 }
17670 /* Walk through the basic block calculating live */
17671 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017672 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017673
17674 prev = ptr->prev;
17675 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017676
17677 /* Ensure the current definition is in live */
17678 if (triple_is_def(state, ptr)) {
17679 do_triple_set(&live, ptr, 0);
17680 }
17681
17682 /* Inform the callback function of what is
17683 * going on.
17684 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017685 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017686
17687 /* Remove the current definition from live */
17688 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017689
Eric Biedermanb138ac82003-04-22 18:44:01 +000017690 /* Add the current uses to live.
17691 *
17692 * It is safe to skip phi functions because they do
17693 * not have any block local uses, and the block
17694 * output sets already properly account for what
17695 * control flow depedent uses phi functions do have.
17696 */
17697 if (ptr->op == OP_PHI) {
17698 continue;
17699 }
17700 expr = triple_rhs(state, ptr, 0);
17701 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17702 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017703 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017704 continue;
17705 }
17706 do_triple_set(&live, *expr, 0);
17707 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017708 }
17709 /* Free live */
17710 for(entry = live; entry; entry = next) {
17711 next = entry->next;
17712 do_triple_unset(&live, entry->member);
17713 }
17714 }
17715}
17716
Eric Biederman90089602004-05-28 14:11:54 +000017717struct print_live_variable_info {
17718 struct reg_block *rb;
17719 FILE *fp;
17720};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017721#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017722static void print_live_variables_block(
17723 struct compile_state *state, struct block *block, void *arg)
17724
17725{
17726 struct print_live_variable_info *info = arg;
17727 struct block_set *edge;
17728 FILE *fp = info->fp;
17729 struct reg_block *rb;
17730 struct triple *ptr;
17731 int phi_present;
17732 int done;
17733 rb = &info->rb[block->vertex];
17734
17735 fprintf(fp, "\nblock: %p (%d),",
17736 block, block->vertex);
17737 for(edge = block->edges; edge; edge = edge->next) {
17738 fprintf(fp, " %p<-%p",
17739 edge->member,
17740 edge->member && edge->member->use?edge->member->use->member : 0);
17741 }
17742 fprintf(fp, "\n");
17743 if (rb->in) {
17744 struct triple_reg_set *in_set;
17745 fprintf(fp, " in:");
17746 for(in_set = rb->in; in_set; in_set = in_set->next) {
17747 fprintf(fp, " %-10p", in_set->member);
17748 }
17749 fprintf(fp, "\n");
17750 }
17751 phi_present = 0;
17752 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17753 done = (ptr == block->last);
17754 if (ptr->op == OP_PHI) {
17755 phi_present = 1;
17756 break;
17757 }
17758 }
17759 if (phi_present) {
17760 int edge;
17761 for(edge = 0; edge < block->users; edge++) {
17762 fprintf(fp, " in(%d):", edge);
17763 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17764 struct triple **slot;
17765 done = (ptr == block->last);
17766 if (ptr->op != OP_PHI) {
17767 continue;
17768 }
17769 slot = &RHS(ptr, 0);
17770 fprintf(fp, " %-10p", slot[edge]);
17771 }
17772 fprintf(fp, "\n");
17773 }
17774 }
17775 if (block->first->op == OP_LABEL) {
17776 fprintf(fp, "%p:\n", block->first);
17777 }
17778 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17779 done = (ptr == block->last);
17780 display_triple(fp, ptr);
17781 }
17782 if (rb->out) {
17783 struct triple_reg_set *out_set;
17784 fprintf(fp, " out:");
17785 for(out_set = rb->out; out_set; out_set = out_set->next) {
17786 fprintf(fp, " %-10p", out_set->member);
17787 }
17788 fprintf(fp, "\n");
17789 }
17790 fprintf(fp, "\n");
17791}
17792
17793static void print_live_variables(struct compile_state *state,
17794 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17795{
17796 struct print_live_variable_info info;
17797 info.rb = rb;
17798 info.fp = fp;
17799 fprintf(fp, "\nlive variables by block\n");
17800 walk_blocks(state, bb, print_live_variables_block, &info);
17801
17802}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017803#endif
Eric Biederman90089602004-05-28 14:11:54 +000017804
Eric Biedermanb138ac82003-04-22 18:44:01 +000017805static int count_triples(struct compile_state *state)
17806{
17807 struct triple *first, *ins;
17808 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017809 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017810 ins = first;
17811 do {
17812 triples++;
17813 ins = ins->next;
17814 } while (ins != first);
17815 return triples;
17816}
Eric Biederman66fe2222003-07-04 15:14:04 +000017817
17818
Eric Biedermanb138ac82003-04-22 18:44:01 +000017819struct dead_triple {
17820 struct triple *triple;
17821 struct dead_triple *work_next;
17822 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017823 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017824 int flags;
17825#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017826#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017827};
17828
Eric Biederman90089602004-05-28 14:11:54 +000017829static void print_dead_triples(struct compile_state *state,
17830 struct dead_triple *dtriple)
17831{
17832 struct triple *first, *ins;
17833 struct dead_triple *dt;
17834 FILE *fp;
17835 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17836 return;
17837 }
17838 fp = state->dbgout;
17839 fprintf(fp, "--------------- dtriples ---------------\n");
17840 first = state->first;
17841 ins = first;
17842 do {
17843 dt = &dtriple[ins->id];
17844 if ((ins->op == OP_LABEL) && (ins->use)) {
17845 fprintf(fp, "\n%p:\n", ins);
17846 }
17847 fprintf(fp, "%c",
17848 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17849 display_triple(fp, ins);
17850 if (triple_is_branch(state, ins)) {
17851 fprintf(fp, "\n");
17852 }
17853 ins = ins->next;
17854 } while(ins != first);
17855 fprintf(fp, "\n");
17856}
17857
Eric Biedermanb138ac82003-04-22 18:44:01 +000017858
17859static void awaken(
17860 struct compile_state *state,
17861 struct dead_triple *dtriple, struct triple **expr,
17862 struct dead_triple ***work_list_tail)
17863{
17864 struct triple *triple;
17865 struct dead_triple *dt;
17866 if (!expr) {
17867 return;
17868 }
17869 triple = *expr;
17870 if (!triple) {
17871 return;
17872 }
17873 if (triple->id <= 0) {
17874 internal_error(state, triple, "bad triple id: %d",
17875 triple->id);
17876 }
17877 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017878 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017879 return;
17880 }
17881 dt = &dtriple[triple->id];
17882 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17883 dt->flags |= TRIPLE_FLAG_ALIVE;
17884 if (!dt->work_next) {
17885 **work_list_tail = dt;
17886 *work_list_tail = &dt->work_next;
17887 }
17888 }
17889}
17890
17891static void eliminate_inefectual_code(struct compile_state *state)
17892{
17893 struct block *block;
17894 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17895 int triples, i;
Eric Biederman83b991a2003-10-11 06:20:25 +000017896 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017897
Eric Biederman5ade04a2003-10-22 04:03:46 +000017898 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17899 return;
17900 }
17901
Eric Biedermanb138ac82003-04-22 18:44:01 +000017902 /* Setup the work list */
17903 work_list = 0;
17904 work_list_tail = &work_list;
17905
Eric Biederman83b991a2003-10-11 06:20:25 +000017906 first = state->first;
17907 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017908
17909 /* Count how many triples I have */
17910 triples = count_triples(state);
17911
17912 /* Now put then in an array and mark all of the triples dead */
17913 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17914
17915 ins = first;
17916 i = 1;
17917 block = 0;
17918 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017919 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017920 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017921 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017922 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017923 ins->id = i;
17924 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017925 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017926 awaken(state, dtriple, &ins, &work_list_tail);
17927 }
17928 i++;
17929 ins = ins->next;
17930 } while(ins != first);
17931 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017932 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017933 struct dead_triple *dt;
17934 struct block_set *user;
17935 struct triple **expr;
17936 dt = work_list;
17937 work_list = dt->work_next;
17938 if (!work_list) {
17939 work_list_tail = &work_list;
17940 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017941 /* Make certain the block the current instruction is in lives */
17942 block = block_of_triple(state, dt->triple);
17943 awaken(state, dtriple, &block->first, &work_list_tail);
17944 if (triple_is_branch(state, block->last)) {
17945 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017946 } else {
17947 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017948 }
17949
Eric Biedermanb138ac82003-04-22 18:44:01 +000017950 /* Wake up the data depencencies of this triple */
17951 expr = 0;
17952 do {
17953 expr = triple_rhs(state, dt->triple, expr);
17954 awaken(state, dtriple, expr, &work_list_tail);
17955 } while(expr);
17956 do {
17957 expr = triple_lhs(state, dt->triple, expr);
17958 awaken(state, dtriple, expr, &work_list_tail);
17959 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017960 do {
17961 expr = triple_misc(state, dt->triple, expr);
17962 awaken(state, dtriple, expr, &work_list_tail);
17963 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017964 /* Wake up the forward control dependencies */
17965 do {
17966 expr = triple_targ(state, dt->triple, expr);
17967 awaken(state, dtriple, expr, &work_list_tail);
17968 } while(expr);
17969 /* Wake up the reverse control dependencies of this triple */
17970 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017971 struct triple *last;
17972 last = user->member->last;
17973 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017974#if DEBUG_ROMCC_WARNINGS
17975#warning "Should we bring the awakening noops back?"
17976#endif
17977 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017978 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017979 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017980 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017981 }
17982 }
Eric Biederman90089602004-05-28 14:11:54 +000017983 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017984 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
17985 if ((dt->triple->op == OP_NOOP) &&
17986 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17987 internal_error(state, dt->triple, "noop effective?");
17988 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017989 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017990 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017991 release_triple(state, dt->triple);
17992 }
17993 }
17994 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000017995
17996 rebuild_ssa_form(state);
17997
Eric Biederman90089602004-05-28 14:11:54 +000017998 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017999}
18000
18001
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018002static void insert_mandatory_copies(struct compile_state *state)
18003{
18004 struct triple *ins, *first;
18005
18006 /* The object is with a minimum of inserted copies,
18007 * to resolve in fundamental register conflicts between
18008 * register value producers and consumers.
18009 * Theoretically we may be greater than minimal when we
18010 * are inserting copies before instructions but that
18011 * case should be rare.
18012 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018013 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018014 ins = first;
18015 do {
18016 struct triple_set *entry, *next;
18017 struct triple *tmp;
18018 struct reg_info info;
18019 unsigned reg, regcm;
18020 int do_post_copy, do_pre_copy;
18021 tmp = 0;
18022 if (!triple_is_def(state, ins)) {
18023 goto next;
18024 }
18025 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018026 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018027 if (info.reg >= MAX_REGISTERS) {
18028 info.reg = REG_UNSET;
18029 }
Eric Biederman90089602004-05-28 14:11:54 +000018030
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018031 reg = REG_UNSET;
18032 regcm = arch_type_to_regcm(state, ins->type);
18033 do_post_copy = do_pre_copy = 0;
18034
18035 /* Walk through the uses of ins and check for conflicts */
18036 for(entry = ins->use; entry; entry = next) {
18037 struct reg_info rinfo;
18038 int i;
18039 next = entry->next;
18040 i = find_rhs_use(state, entry->member, ins);
18041 if (i < 0) {
18042 continue;
18043 }
18044
18045 /* Find the users color requirements */
18046 rinfo = arch_reg_rhs(state, entry->member, i);
18047 if (rinfo.reg >= MAX_REGISTERS) {
18048 rinfo.reg = REG_UNSET;
18049 }
18050
18051 /* See if I need a pre_copy */
18052 if (rinfo.reg != REG_UNSET) {
18053 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18054 do_pre_copy = 1;
18055 }
18056 reg = rinfo.reg;
18057 }
18058 regcm &= rinfo.regcm;
18059 regcm = arch_regcm_normalize(state, regcm);
18060 if (regcm == 0) {
18061 do_pre_copy = 1;
18062 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018063 /* Always use pre_copies for constants.
18064 * They do not take up any registers until a
18065 * copy places them in one.
18066 */
18067 if ((info.reg == REG_UNNEEDED) &&
18068 (rinfo.reg != REG_UNNEEDED)) {
18069 do_pre_copy = 1;
18070 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018071 }
18072 do_post_copy =
18073 !do_pre_copy &&
18074 (((info.reg != REG_UNSET) &&
18075 (reg != REG_UNSET) &&
18076 (info.reg != reg)) ||
18077 ((info.regcm & regcm) == 0));
18078
18079 reg = info.reg;
18080 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018081 /* 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 +000018082 for(entry = ins->use; entry; entry = next) {
18083 struct reg_info rinfo;
18084 int i;
18085 next = entry->next;
18086 i = find_rhs_use(state, entry->member, ins);
18087 if (i < 0) {
18088 continue;
18089 }
18090
18091 /* Find the users color requirements */
18092 rinfo = arch_reg_rhs(state, entry->member, i);
18093 if (rinfo.reg >= MAX_REGISTERS) {
18094 rinfo.reg = REG_UNSET;
18095 }
18096
18097 /* Now see if it is time to do the pre_copy */
18098 if (rinfo.reg != REG_UNSET) {
18099 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18100 ((regcm & rinfo.regcm) == 0) ||
18101 /* Don't let a mandatory coalesce sneak
18102 * into a operation that is marked to prevent
18103 * coalescing.
18104 */
18105 ((reg != REG_UNNEEDED) &&
18106 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18107 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18108 ) {
18109 if (do_pre_copy) {
18110 struct triple *user;
18111 user = entry->member;
18112 if (RHS(user, i) != ins) {
18113 internal_error(state, user, "bad rhs");
18114 }
18115 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018116 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018117 continue;
18118 } else {
18119 do_post_copy = 1;
18120 }
18121 }
18122 reg = rinfo.reg;
18123 }
18124 if ((regcm & rinfo.regcm) == 0) {
18125 if (do_pre_copy) {
18126 struct triple *user;
18127 user = entry->member;
18128 if (RHS(user, i) != ins) {
18129 internal_error(state, user, "bad rhs");
18130 }
18131 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018132 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018133 continue;
18134 } else {
18135 do_post_copy = 1;
18136 }
18137 }
18138 regcm &= rinfo.regcm;
18139
18140 }
18141 if (do_post_copy) {
18142 struct reg_info pre, post;
18143 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018144 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018145 pre = arch_reg_lhs(state, ins, 0);
18146 post = arch_reg_lhs(state, tmp, 0);
18147 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18148 internal_error(state, tmp, "useless copy");
18149 }
18150 }
18151 next:
18152 ins = ins->next;
18153 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018154
Eric Biederman90089602004-05-28 14:11:54 +000018155 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018156}
18157
18158
Eric Biedermanb138ac82003-04-22 18:44:01 +000018159struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018160struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018161struct live_range {
18162 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018163 struct live_range_def *defs;
18164/* Note. The list pointed to by defs is kept in order.
18165 * That is baring splits in the flow control
18166 * defs dominates defs->next wich dominates defs->next->next
18167 * etc.
18168 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018169 unsigned color;
18170 unsigned classes;
18171 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018172 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018173 struct live_range *group_next, **group_prev;
18174};
18175
18176struct live_range_edge {
18177 struct live_range_edge *next;
18178 struct live_range *node;
18179};
18180
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018181struct live_range_def {
18182 struct live_range_def *next;
18183 struct live_range_def *prev;
18184 struct live_range *lr;
18185 struct triple *def;
18186 unsigned orig_id;
18187};
18188
Eric Biedermanb138ac82003-04-22 18:44:01 +000018189#define LRE_HASH_SIZE 2048
18190struct lre_hash {
18191 struct lre_hash *next;
18192 struct live_range *left;
18193 struct live_range *right;
18194};
18195
18196
18197struct reg_state {
18198 struct lre_hash *hash[LRE_HASH_SIZE];
18199 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018200 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018201 struct live_range *lr;
18202 struct live_range *low, **low_tail;
18203 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018204 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018205 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018206 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018207};
18208
18209
Eric Biedermand1ea5392003-06-28 06:49:45 +000018210struct print_interference_block_info {
18211 struct reg_state *rstate;
18212 FILE *fp;
18213 int need_edges;
18214};
18215static void print_interference_block(
18216 struct compile_state *state, struct block *block, void *arg)
18217
18218{
18219 struct print_interference_block_info *info = arg;
18220 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018221 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018222 FILE *fp = info->fp;
18223 struct reg_block *rb;
18224 struct triple *ptr;
18225 int phi_present;
18226 int done;
18227 rb = &rstate->blocks[block->vertex];
18228
Eric Biederman5ade04a2003-10-22 04:03:46 +000018229 fprintf(fp, "\nblock: %p (%d),",
18230 block, block->vertex);
18231 for(edge = block->edges; edge; edge = edge->next) {
18232 fprintf(fp, " %p<-%p",
18233 edge->member,
18234 edge->member && edge->member->use?edge->member->use->member : 0);
18235 }
18236 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018237 if (rb->in) {
18238 struct triple_reg_set *in_set;
18239 fprintf(fp, " in:");
18240 for(in_set = rb->in; in_set; in_set = in_set->next) {
18241 fprintf(fp, " %-10p", in_set->member);
18242 }
18243 fprintf(fp, "\n");
18244 }
18245 phi_present = 0;
18246 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18247 done = (ptr == block->last);
18248 if (ptr->op == OP_PHI) {
18249 phi_present = 1;
18250 break;
18251 }
18252 }
18253 if (phi_present) {
18254 int edge;
18255 for(edge = 0; edge < block->users; edge++) {
18256 fprintf(fp, " in(%d):", edge);
18257 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18258 struct triple **slot;
18259 done = (ptr == block->last);
18260 if (ptr->op != OP_PHI) {
18261 continue;
18262 }
18263 slot = &RHS(ptr, 0);
18264 fprintf(fp, " %-10p", slot[edge]);
18265 }
18266 fprintf(fp, "\n");
18267 }
18268 }
18269 if (block->first->op == OP_LABEL) {
18270 fprintf(fp, "%p:\n", block->first);
18271 }
18272 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018273 struct live_range *lr;
18274 unsigned id;
18275 int op;
18276 op = ptr->op;
18277 done = (ptr == block->last);
18278 lr = rstate->lrd[ptr->id].lr;
18279
Eric Biedermand1ea5392003-06-28 06:49:45 +000018280 id = ptr->id;
18281 ptr->id = rstate->lrd[id].orig_id;
18282 SET_REG(ptr->id, lr->color);
18283 display_triple(fp, ptr);
18284 ptr->id = id;
18285
18286 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18287 internal_error(state, ptr, "lr has no defs!");
18288 }
18289 if (info->need_edges) {
18290 if (lr->defs) {
18291 struct live_range_def *lrd;
18292 fprintf(fp, " range:");
18293 lrd = lr->defs;
18294 do {
18295 fprintf(fp, " %-10p", lrd->def);
18296 lrd = lrd->next;
18297 } while(lrd != lr->defs);
18298 fprintf(fp, "\n");
18299 }
18300 if (lr->edges > 0) {
18301 struct live_range_edge *edge;
18302 fprintf(fp, " edges:");
18303 for(edge = lr->edges; edge; edge = edge->next) {
18304 struct live_range_def *lrd;
18305 lrd = edge->node->defs;
18306 do {
18307 fprintf(fp, " %-10p", lrd->def);
18308 lrd = lrd->next;
18309 } while(lrd != edge->node->defs);
18310 fprintf(fp, "|");
18311 }
18312 fprintf(fp, "\n");
18313 }
18314 }
18315 /* Do a bunch of sanity checks */
18316 valid_ins(state, ptr);
18317 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18318 internal_error(state, ptr, "Invalid triple id: %d",
18319 ptr->id);
18320 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018321 }
18322 if (rb->out) {
18323 struct triple_reg_set *out_set;
18324 fprintf(fp, " out:");
18325 for(out_set = rb->out; out_set; out_set = out_set->next) {
18326 fprintf(fp, " %-10p", out_set->member);
18327 }
18328 fprintf(fp, "\n");
18329 }
18330 fprintf(fp, "\n");
18331}
18332
18333static void print_interference_blocks(
18334 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18335{
18336 struct print_interference_block_info info;
18337 info.rstate = rstate;
18338 info.fp = fp;
18339 info.need_edges = need_edges;
18340 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018341 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018342
18343}
18344
Eric Biedermanb138ac82003-04-22 18:44:01 +000018345static unsigned regc_max_size(struct compile_state *state, int classes)
18346{
18347 unsigned max_size;
18348 int i;
18349 max_size = 0;
18350 for(i = 0; i < MAX_REGC; i++) {
18351 if (classes & (1 << i)) {
18352 unsigned size;
18353 size = arch_regc_size(state, i);
18354 if (size > max_size) {
18355 max_size = size;
18356 }
18357 }
18358 }
18359 return max_size;
18360}
18361
18362static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18363{
18364 unsigned equivs[MAX_REG_EQUIVS];
18365 int i;
18366 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18367 internal_error(state, 0, "invalid register");
18368 }
18369 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18370 internal_error(state, 0, "invalid register");
18371 }
18372 arch_reg_equivs(state, equivs, reg1);
18373 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18374 if (equivs[i] == reg2) {
18375 return 1;
18376 }
18377 }
18378 return 0;
18379}
18380
18381static void reg_fill_used(struct compile_state *state, char *used, int reg)
18382{
18383 unsigned equivs[MAX_REG_EQUIVS];
18384 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018385 if (reg == REG_UNNEEDED) {
18386 return;
18387 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018388 arch_reg_equivs(state, equivs, reg);
18389 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18390 used[equivs[i]] = 1;
18391 }
18392 return;
18393}
18394
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018395static void reg_inc_used(struct compile_state *state, char *used, int reg)
18396{
18397 unsigned equivs[MAX_REG_EQUIVS];
18398 int i;
18399 if (reg == REG_UNNEEDED) {
18400 return;
18401 }
18402 arch_reg_equivs(state, equivs, reg);
18403 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18404 used[equivs[i]] += 1;
18405 }
18406 return;
18407}
18408
Eric Biedermanb138ac82003-04-22 18:44:01 +000018409static unsigned int hash_live_edge(
18410 struct live_range *left, struct live_range *right)
18411{
18412 unsigned int hash, val;
18413 unsigned long lval, rval;
18414 lval = ((unsigned long)left)/sizeof(struct live_range);
18415 rval = ((unsigned long)right)/sizeof(struct live_range);
18416 hash = 0;
18417 while(lval) {
18418 val = lval & 0xff;
18419 lval >>= 8;
18420 hash = (hash *263) + val;
18421 }
18422 while(rval) {
18423 val = rval & 0xff;
18424 rval >>= 8;
18425 hash = (hash *263) + val;
18426 }
18427 hash = hash & (LRE_HASH_SIZE - 1);
18428 return hash;
18429}
18430
18431static struct lre_hash **lre_probe(struct reg_state *rstate,
18432 struct live_range *left, struct live_range *right)
18433{
18434 struct lre_hash **ptr;
18435 unsigned int index;
18436 /* Ensure left <= right */
18437 if (left > right) {
18438 struct live_range *tmp;
18439 tmp = left;
18440 left = right;
18441 right = tmp;
18442 }
18443 index = hash_live_edge(left, right);
18444
18445 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018446 while(*ptr) {
18447 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18448 break;
18449 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018450 ptr = &(*ptr)->next;
18451 }
18452 return ptr;
18453}
18454
18455static int interfere(struct reg_state *rstate,
18456 struct live_range *left, struct live_range *right)
18457{
18458 struct lre_hash **ptr;
18459 ptr = lre_probe(rstate, left, right);
18460 return ptr && *ptr;
18461}
18462
18463static void add_live_edge(struct reg_state *rstate,
18464 struct live_range *left, struct live_range *right)
18465{
18466 /* FIXME the memory allocation overhead is noticeable here... */
18467 struct lre_hash **ptr, *new_hash;
18468 struct live_range_edge *edge;
18469
18470 if (left == right) {
18471 return;
18472 }
18473 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18474 return;
18475 }
18476 /* Ensure left <= right */
18477 if (left > right) {
18478 struct live_range *tmp;
18479 tmp = left;
18480 left = right;
18481 right = tmp;
18482 }
18483 ptr = lre_probe(rstate, left, right);
18484 if (*ptr) {
18485 return;
18486 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018487#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018488 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018489 left, right);
18490#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018491 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18492 new_hash->next = *ptr;
18493 new_hash->left = left;
18494 new_hash->right = right;
18495 *ptr = new_hash;
18496
18497 edge = xmalloc(sizeof(*edge), "live_range_edge");
18498 edge->next = left->edges;
18499 edge->node = right;
18500 left->edges = edge;
18501 left->degree += 1;
18502
18503 edge = xmalloc(sizeof(*edge), "live_range_edge");
18504 edge->next = right->edges;
18505 edge->node = left;
18506 right->edges = edge;
18507 right->degree += 1;
18508}
18509
18510static void remove_live_edge(struct reg_state *rstate,
18511 struct live_range *left, struct live_range *right)
18512{
18513 struct live_range_edge *edge, **ptr;
18514 struct lre_hash **hptr, *entry;
18515 hptr = lre_probe(rstate, left, right);
18516 if (!hptr || !*hptr) {
18517 return;
18518 }
18519 entry = *hptr;
18520 *hptr = entry->next;
18521 xfree(entry);
18522
18523 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18524 edge = *ptr;
18525 if (edge->node == right) {
18526 *ptr = edge->next;
18527 memset(edge, 0, sizeof(*edge));
18528 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018529 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018530 break;
18531 }
18532 }
18533 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18534 edge = *ptr;
18535 if (edge->node == left) {
18536 *ptr = edge->next;
18537 memset(edge, 0, sizeof(*edge));
18538 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018539 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018540 break;
18541 }
18542 }
18543}
18544
18545static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18546{
18547 struct live_range_edge *edge, *next;
18548 for(edge = range->edges; edge; edge = next) {
18549 next = edge->next;
18550 remove_live_edge(rstate, range, edge->node);
18551 }
18552}
18553
Eric Biederman153ea352003-06-20 14:43:20 +000018554static void transfer_live_edges(struct reg_state *rstate,
18555 struct live_range *dest, struct live_range *src)
18556{
18557 struct live_range_edge *edge, *next;
18558 for(edge = src->edges; edge; edge = next) {
18559 struct live_range *other;
18560 next = edge->next;
18561 other = edge->node;
18562 remove_live_edge(rstate, src, other);
18563 add_live_edge(rstate, dest, other);
18564 }
18565}
18566
Eric Biedermanb138ac82003-04-22 18:44:01 +000018567
18568/* Interference graph...
18569 *
18570 * new(n) --- Return a graph with n nodes but no edges.
18571 * add(g,x,y) --- Return a graph including g with an between x and y
18572 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18573 * x and y in the graph g
18574 * degree(g, x) --- Return the degree of the node x in the graph g
18575 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18576 *
18577 * Implement with a hash table && a set of adjcency vectors.
18578 * The hash table supports constant time implementations of add and interfere.
18579 * The adjacency vectors support an efficient implementation of neighbors.
18580 */
18581
18582/*
18583 * +---------------------------------------------------+
18584 * | +--------------+ |
18585 * v v | |
18586 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
18587 *
18588 * -- In simplify implment optimistic coloring... (No backtracking)
18589 * -- Implement Rematerialization it is the only form of spilling we can perform
18590 * Essentially this means dropping a constant from a register because
18591 * we can regenerate it later.
18592 *
18593 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18594 * coalesce at phi points...
18595 * --- Bias coloring if at all possible do the coalesing a compile time.
18596 *
18597 *
18598 */
18599
Stefan Reinauer50542a82007-10-24 11:14:14 +000018600#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018601static void different_colored(
18602 struct compile_state *state, struct reg_state *rstate,
18603 struct triple *parent, struct triple *ins)
18604{
18605 struct live_range *lr;
18606 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018607 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018608 expr = triple_rhs(state, ins, 0);
18609 for(;expr; expr = triple_rhs(state, ins, expr)) {
18610 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018611 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018612 continue;
18613 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018614 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018615 if (lr->color == lr2->color) {
18616 internal_error(state, ins, "live range too big");
18617 }
18618 }
18619}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018620#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018621
18622static struct live_range *coalesce_ranges(
18623 struct compile_state *state, struct reg_state *rstate,
18624 struct live_range *lr1, struct live_range *lr2)
18625{
18626 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18627 unsigned color;
18628 unsigned classes;
18629 if (lr1 == lr2) {
18630 return lr1;
18631 }
18632 if (!lr1->defs || !lr2->defs) {
18633 internal_error(state, 0,
18634 "cannot coalese dead live ranges");
18635 }
18636 if ((lr1->color == REG_UNNEEDED) ||
18637 (lr2->color == REG_UNNEEDED)) {
18638 internal_error(state, 0,
18639 "cannot coalesce live ranges without a possible color");
18640 }
18641 if ((lr1->color != lr2->color) &&
18642 (lr1->color != REG_UNSET) &&
18643 (lr2->color != REG_UNSET)) {
18644 internal_error(state, lr1->defs->def,
18645 "cannot coalesce live ranges of different colors");
18646 }
18647 color = lr1->color;
18648 if (color == REG_UNSET) {
18649 color = lr2->color;
18650 }
18651 classes = lr1->classes & lr2->classes;
18652 if (!classes) {
18653 internal_error(state, lr1->defs->def,
18654 "cannot coalesce live ranges with dissimilar register classes");
18655 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018656 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018657 FILE *fp = state->errout;
18658 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018659 lrd = lr1->defs;
18660 do {
Eric Biederman90089602004-05-28 14:11:54 +000018661 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018662 lrd = lrd->next;
18663 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018664 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018665 lrd = lr2->defs;
18666 do {
Eric Biederman90089602004-05-28 14:11:54 +000018667 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018668 lrd = lrd->next;
18669 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018670 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018671 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018672 /* If there is a clear dominate live range put it in lr1,
18673 * For purposes of this test phi functions are
18674 * considered dominated by the definitions that feed into
18675 * them.
18676 */
18677 if ((lr1->defs->prev->def->op == OP_PHI) ||
18678 ((lr2->defs->prev->def->op != OP_PHI) &&
18679 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18680 struct live_range *tmp;
18681 tmp = lr1;
18682 lr1 = lr2;
18683 lr2 = tmp;
18684 }
18685#if 0
18686 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018687 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018688 }
18689 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018690 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018691 }
18692 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018693 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018694 }
18695 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018696 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018697 }
18698#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018699#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018700 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018701 lr1->defs->def,
18702 lr1->color,
18703 lr2->defs->def,
18704 lr2->color);
18705#endif
18706
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018707 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018708#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018709#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018710#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018711 /* This FIXME item applies to the correctness of live_range_end
18712 * and to the necessity of making multiple passes of coalesce_live_ranges.
18713 * A failure to find some coalesce opportunities in coaleace_live_ranges
18714 * does not impact the correct of the compiler just the efficiency with
18715 * which registers are allocated.
18716 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018717 head = lr1->defs;
18718 mid1 = lr1->defs->prev;
18719 mid2 = lr2->defs;
18720 end = lr2->defs->prev;
18721
18722 head->prev = end;
18723 end->next = head;
18724
18725 mid1->next = mid2;
18726 mid2->prev = mid1;
18727
18728 /* Fixup the live range in the added live range defs */
18729 lrd = head;
18730 do {
18731 lrd->lr = lr1;
18732 lrd = lrd->next;
18733 } while(lrd != head);
18734
18735 /* Mark lr2 as free. */
18736 lr2->defs = 0;
18737 lr2->color = REG_UNNEEDED;
18738 lr2->classes = 0;
18739
18740 if (!lr1->defs) {
18741 internal_error(state, 0, "lr1->defs == 0 ?");
18742 }
18743
18744 lr1->color = color;
18745 lr1->classes = classes;
18746
Eric Biederman153ea352003-06-20 14:43:20 +000018747 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18748 transfer_live_edges(rstate, lr1, lr2);
18749
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018750 return lr1;
18751}
18752
18753static struct live_range_def *live_range_head(
18754 struct compile_state *state, struct live_range *lr,
18755 struct live_range_def *last)
18756{
18757 struct live_range_def *result;
18758 result = 0;
18759 if (last == 0) {
18760 result = lr->defs;
18761 }
18762 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18763 result = last->next;
18764 }
18765 return result;
18766}
18767
18768static struct live_range_def *live_range_end(
18769 struct compile_state *state, struct live_range *lr,
18770 struct live_range_def *last)
18771{
18772 struct live_range_def *result;
18773 result = 0;
18774 if (last == 0) {
18775 result = lr->defs->prev;
18776 }
18777 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18778 result = last->prev;
18779 }
18780 return result;
18781}
18782
18783
Eric Biedermanb138ac82003-04-22 18:44:01 +000018784static void initialize_live_ranges(
18785 struct compile_state *state, struct reg_state *rstate)
18786{
18787 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018788 size_t count, size;
18789 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018790
Eric Biederman83b991a2003-10-11 06:20:25 +000018791 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018792 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018793 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018794 count = count_triples(state);
18795 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018796 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018797 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018798 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018799 /* Potentially I need one live range for each instruction
18800 * plus an extra for the dummy live range.
18801 */
18802 rstate->ranges = count + 1;
18803 size = sizeof(rstate->lrd[0]) * rstate->defs;
18804 rstate->lrd = xcmalloc(size, "live_range_def");
18805 size = sizeof(rstate->lr[0]) * rstate->ranges;
18806 rstate->lr = xcmalloc(size, "live_range");
18807
Eric Biedermanb138ac82003-04-22 18:44:01 +000018808 /* Setup the dummy live range */
18809 rstate->lr[0].classes = 0;
18810 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018811 rstate->lr[0].defs = 0;
18812 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018813 ins = first;
18814 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018815 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018816 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018817 struct reg_info info;
18818 /* Find the architecture specific color information */
18819 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018820 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018821 rstate->lr[i].defs = &rstate->lrd[j];
18822 rstate->lr[i].color = info.reg;
18823 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018824 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018825 rstate->lrd[j].lr = &rstate->lr[i];
18826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018827 /* Otherwise give the triple the dummy live range. */
18828 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018829 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018830 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018831
18832 /* Initalize the live_range_def */
18833 rstate->lrd[j].next = &rstate->lrd[j];
18834 rstate->lrd[j].prev = &rstate->lrd[j];
18835 rstate->lrd[j].def = ins;
18836 rstate->lrd[j].orig_id = ins->id;
18837 ins->id = j;
18838
18839 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018840 ins = ins->next;
18841 } while(ins != first);
18842 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018843
Eric Biedermanb138ac82003-04-22 18:44:01 +000018844 /* Make a second pass to handle achitecture specific register
18845 * constraints.
18846 */
18847 ins = first;
18848 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018849 int zlhs, zrhs, i, j;
18850 if (ins->id > rstate->defs) {
18851 internal_error(state, ins, "bad id");
18852 }
18853
18854 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018855 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018856 if ((zlhs == 0) && triple_is_def(state, ins)) {
18857 zlhs = 1;
18858 }
Eric Biederman90089602004-05-28 14:11:54 +000018859 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018860
Eric Biederman5ade04a2003-10-22 04:03:46 +000018861 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018862 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018863 ins, zlhs, zrhs);
18864 }
18865
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018866 for(i = 0; i < zlhs; i++) {
18867 struct reg_info linfo;
18868 struct live_range_def *lhs;
18869 linfo = arch_reg_lhs(state, ins, i);
18870 if (linfo.reg < MAX_REGISTERS) {
18871 continue;
18872 }
18873 if (triple_is_def(state, ins)) {
18874 lhs = &rstate->lrd[ins->id];
18875 } else {
18876 lhs = &rstate->lrd[LHS(ins, i)->id];
18877 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018878
18879 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018880 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018881 i, lhs, linfo.reg);
18882 }
18883
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018884 for(j = 0; j < zrhs; j++) {
18885 struct reg_info rinfo;
18886 struct live_range_def *rhs;
18887 rinfo = arch_reg_rhs(state, ins, j);
18888 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018889 continue;
18890 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018891 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018892
18893 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018894 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018895 j, rhs, rinfo.reg);
18896 }
18897
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018898 if (rinfo.reg == linfo.reg) {
18899 coalesce_ranges(state, rstate,
18900 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018901 }
18902 }
18903 }
18904 ins = ins->next;
18905 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018906}
18907
Eric Biedermanf96a8102003-06-16 16:57:34 +000018908static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018909 struct compile_state *state,
18910 struct reg_block *blocks, struct triple_reg_set *live,
18911 struct reg_block *rb, struct triple *ins, void *arg)
18912{
18913 struct reg_state *rstate = arg;
18914 struct live_range *def;
18915 struct triple_reg_set *entry;
18916
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018917 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018918 * we do not have a definition to add to
18919 * the interference graph.
18920 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018921 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018922 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018923 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018924 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018925
18926 /* Create an edge between ins and everything that is
18927 * alive, unless the live_range cannot share
18928 * a physical register with ins.
18929 */
18930 for(entry = live; entry; entry = entry->next) {
18931 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018932 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18933 internal_error(state, 0, "bad entry?");
18934 }
18935 lr = rstate->lrd[entry->member->id].lr;
18936 if (def == lr) {
18937 continue;
18938 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018939 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18940 continue;
18941 }
18942 add_live_edge(rstate, def, lr);
18943 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018944 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018945}
18946
Stefan Reinauer50542a82007-10-24 11:14:14 +000018947#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018948static struct live_range *get_verify_live_range(
18949 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18950{
18951 struct live_range *lr;
18952 struct live_range_def *lrd;
18953 int ins_found;
18954 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18955 internal_error(state, ins, "bad ins?");
18956 }
18957 lr = rstate->lrd[ins->id].lr;
18958 ins_found = 0;
18959 lrd = lr->defs;
18960 do {
18961 if (lrd->def == ins) {
18962 ins_found = 1;
18963 }
18964 lrd = lrd->next;
18965 } while(lrd != lr->defs);
18966 if (!ins_found) {
18967 internal_error(state, ins, "ins not in live range");
18968 }
18969 return lr;
18970}
18971
18972static void verify_graph_ins(
18973 struct compile_state *state,
18974 struct reg_block *blocks, struct triple_reg_set *live,
18975 struct reg_block *rb, struct triple *ins, void *arg)
18976{
18977 struct reg_state *rstate = arg;
18978 struct triple_reg_set *entry1, *entry2;
18979
18980
18981 /* Compare live against edges and make certain the code is working */
18982 for(entry1 = live; entry1; entry1 = entry1->next) {
18983 struct live_range *lr1;
18984 lr1 = get_verify_live_range(state, rstate, entry1->member);
18985 for(entry2 = live; entry2; entry2 = entry2->next) {
18986 struct live_range *lr2;
18987 struct live_range_edge *edge2;
18988 int lr1_found;
18989 int lr2_degree;
18990 if (entry2 == entry1) {
18991 continue;
18992 }
18993 lr2 = get_verify_live_range(state, rstate, entry2->member);
18994 if (lr1 == lr2) {
18995 internal_error(state, entry2->member,
18996 "live range with 2 values simultaneously alive");
18997 }
18998 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
18999 continue;
19000 }
19001 if (!interfere(rstate, lr1, lr2)) {
19002 internal_error(state, entry2->member,
19003 "edges don't interfere?");
19004 }
19005
19006 lr1_found = 0;
19007 lr2_degree = 0;
19008 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19009 lr2_degree++;
19010 if (edge2->node == lr1) {
19011 lr1_found = 1;
19012 }
19013 }
19014 if (lr2_degree != lr2->degree) {
19015 internal_error(state, entry2->member,
19016 "computed degree: %d does not match reported degree: %d\n",
19017 lr2_degree, lr2->degree);
19018 }
19019 if (!lr1_found) {
19020 internal_error(state, entry2->member, "missing edge");
19021 }
19022 }
19023 }
19024 return;
19025}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019026#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019027
Eric Biedermanf96a8102003-06-16 16:57:34 +000019028static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000019029 struct compile_state *state,
19030 struct reg_block *blocks, struct triple_reg_set *live,
19031 struct reg_block *rb, struct triple *ins, void *arg)
19032{
19033 struct reg_state *rstate = arg;
19034 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019035 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019036 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019037
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019038 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019039 id = ins->id;
19040 ins->id = rstate->lrd[id].orig_id;
19041 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019042 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019043 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019044
19045 if (lr->defs) {
19046 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019047 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019048 lrd = lr->defs;
19049 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019050 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019051 lrd = lrd->next;
19052 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019053 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019054 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019055 if (live) {
19056 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019057 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019058 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019059 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019060 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019061 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019062 }
19063 if (lr->edges) {
19064 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019065 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019066 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019067 struct live_range_def *lrd;
19068 lrd = entry->node->defs;
19069 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019070 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019071 lrd = lrd->next;
19072 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019073 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019074 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019075 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019076 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019077 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019078 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019079 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019080 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019081}
19082
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019083static int coalesce_live_ranges(
19084 struct compile_state *state, struct reg_state *rstate)
19085{
19086 /* At the point where a value is moved from one
19087 * register to another that value requires two
19088 * registers, thus increasing register pressure.
19089 * Live range coaleescing reduces the register
19090 * pressure by keeping a value in one register
19091 * longer.
19092 *
19093 * In the case of a phi function all paths leading
19094 * into it must be allocated to the same register
19095 * otherwise the phi function may not be removed.
19096 *
19097 * Forcing a value to stay in a single register
19098 * for an extended period of time does have
19099 * limitations when applied to non homogenous
19100 * register pool.
19101 *
19102 * The two cases I have identified are:
19103 * 1) Two forced register assignments may
19104 * collide.
19105 * 2) Registers may go unused because they
19106 * are only good for storing the value
19107 * and not manipulating it.
19108 *
19109 * Because of this I need to split live ranges,
19110 * even outside of the context of coalesced live
19111 * ranges. The need to split live ranges does
19112 * impose some constraints on live range coalescing.
19113 *
19114 * - Live ranges may not be coalesced across phi
19115 * functions. This creates a 2 headed live
19116 * range that cannot be sanely split.
19117 *
19118 * - phi functions (coalesced in initialize_live_ranges)
19119 * are handled as pre split live ranges so we will
19120 * never attempt to split them.
19121 */
19122 int coalesced;
19123 int i;
19124
19125 coalesced = 0;
19126 for(i = 0; i <= rstate->ranges; i++) {
19127 struct live_range *lr1;
19128 struct live_range_def *lrd1;
19129 lr1 = &rstate->lr[i];
19130 if (!lr1->defs) {
19131 continue;
19132 }
19133 lrd1 = live_range_end(state, lr1, 0);
19134 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19135 struct triple_set *set;
19136 if (lrd1->def->op != OP_COPY) {
19137 continue;
19138 }
19139 /* Skip copies that are the result of a live range split. */
19140 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19141 continue;
19142 }
19143 for(set = lrd1->def->use; set; set = set->next) {
19144 struct live_range_def *lrd2;
19145 struct live_range *lr2, *res;
19146
19147 lrd2 = &rstate->lrd[set->member->id];
19148
19149 /* Don't coalesce with instructions
19150 * that are the result of a live range
19151 * split.
19152 */
19153 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19154 continue;
19155 }
19156 lr2 = rstate->lrd[set->member->id].lr;
19157 if (lr1 == lr2) {
19158 continue;
19159 }
19160 if ((lr1->color != lr2->color) &&
19161 (lr1->color != REG_UNSET) &&
19162 (lr2->color != REG_UNSET)) {
19163 continue;
19164 }
19165 if ((lr1->classes & lr2->classes) == 0) {
19166 continue;
19167 }
19168
19169 if (interfere(rstate, lr1, lr2)) {
19170 continue;
19171 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019172
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019173 res = coalesce_ranges(state, rstate, lr1, lr2);
19174 coalesced += 1;
19175 if (res != lr1) {
19176 goto next;
19177 }
19178 }
19179 }
19180 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019181 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019182 }
19183 return coalesced;
19184}
19185
19186
Eric Biedermanf96a8102003-06-16 16:57:34 +000019187static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019188 struct reg_block *blocks, struct triple_reg_set *live,
19189 struct reg_block *rb, struct triple *ins, void *arg)
19190{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019191 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019192 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019193
19194 /* See if we have a mandatory coalesce operation between
19195 * a lhs and a rhs value. If so and the rhs value is also
19196 * alive then this triple needs to be pre copied. Otherwise
19197 * we would have two definitions in the same live range simultaneously
19198 * alive.
19199 */
Eric Biederman90089602004-05-28 14:11:54 +000019200 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019201 if ((zlhs == 0) && triple_is_def(state, ins)) {
19202 zlhs = 1;
19203 }
Eric Biederman90089602004-05-28 14:11:54 +000019204 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019205 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019206 struct reg_info linfo;
19207 linfo = arch_reg_lhs(state, ins, i);
19208 if (linfo.reg < MAX_REGISTERS) {
19209 continue;
19210 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019211 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019212 struct reg_info rinfo;
19213 struct triple *rhs;
19214 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019215 int found;
19216 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019217 rinfo = arch_reg_rhs(state, ins, j);
19218 if (rinfo.reg != linfo.reg) {
19219 continue;
19220 }
19221 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019222 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019223 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019224 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019225 }
19226 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019227 if (found) {
19228 struct triple *copy;
19229 copy = pre_copy(state, ins, j);
19230 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019231 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019232 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019233 }
19234 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019235 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019236}
19237
Eric Biedermand1ea5392003-06-28 06:49:45 +000019238static int correct_coalesce_conflicts(
19239 struct compile_state *state, struct reg_block *blocks)
19240{
19241 int conflicts;
19242 conflicts = 0;
Eric Biederman90089602004-05-28 14:11:54 +000019243 walk_variable_lifetimes(state, &state->bb, blocks,
19244 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019245 return conflicts;
19246}
19247
Eric Biedermanf96a8102003-06-16 16:57:34 +000019248static void replace_set_use(struct compile_state *state,
19249 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019250{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019251 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019252 for(set = head; set; set = set->next) {
19253 if (set->member == orig) {
19254 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019255 }
19256 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019257}
19258
Eric Biedermanf96a8102003-06-16 16:57:34 +000019259static void replace_block_use(struct compile_state *state,
19260 struct reg_block *blocks, struct triple *orig, struct triple *new)
19261{
19262 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019263#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019264#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019265#endif
Eric Biederman90089602004-05-28 14:11:54 +000019266 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019267 struct reg_block *rb;
19268 rb = &blocks[i];
19269 replace_set_use(state, rb->in, orig, new);
19270 replace_set_use(state, rb->out, orig, new);
19271 }
19272}
19273
19274static void color_instructions(struct compile_state *state)
19275{
19276 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019277 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019278 ins = first;
19279 do {
19280 if (triple_is_def(state, ins)) {
19281 struct reg_info info;
19282 info = find_lhs_color(state, ins, 0);
19283 if (info.reg >= MAX_REGISTERS) {
19284 info.reg = REG_UNSET;
19285 }
19286 SET_INFO(ins->id, info);
19287 }
19288 ins = ins->next;
19289 } while(ins != first);
19290}
19291
19292static struct reg_info read_lhs_color(
19293 struct compile_state *state, struct triple *ins, int index)
19294{
19295 struct reg_info info;
19296 if ((index == 0) && triple_is_def(state, ins)) {
19297 info.reg = ID_REG(ins->id);
19298 info.regcm = ID_REGCM(ins->id);
19299 }
Eric Biederman90089602004-05-28 14:11:54 +000019300 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019301 info = read_lhs_color(state, LHS(ins, index), 0);
19302 }
19303 else {
19304 internal_error(state, ins, "Bad lhs %d", index);
19305 info.reg = REG_UNSET;
19306 info.regcm = 0;
19307 }
19308 return info;
19309}
19310
19311static struct triple *resolve_tangle(
19312 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019313{
19314 struct reg_info info, uinfo;
19315 struct triple_set *set, *next;
19316 struct triple *copy;
19317
Stefan Reinauer50542a82007-10-24 11:14:14 +000019318#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019319#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019320#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019321 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019322 for(set = tangle->use; set; set = next) {
19323 struct triple *user;
19324 int i, zrhs;
19325 next = set->next;
19326 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019327 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019328 for(i = 0; i < zrhs; i++) {
19329 if (RHS(user, i) != tangle) {
19330 continue;
19331 }
19332 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019333 if (uinfo.reg == info.reg) {
19334 copy = pre_copy(state, user, i);
19335 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019336 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019337 }
19338 }
19339 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019340 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019341 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019342 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019343 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019344 copy = post_copy(state, tangle);
19345 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019346 linfo = find_lhs_color(state, copy, 0);
19347 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019348 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019349 info = find_lhs_color(state, tangle, 0);
19350 SET_INFO(tangle->id, info);
19351
19352 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019353}
19354
19355
Eric Biedermanf96a8102003-06-16 16:57:34 +000019356static void fix_tangles(struct compile_state *state,
19357 struct reg_block *blocks, struct triple_reg_set *live,
19358 struct reg_block *rb, struct triple *ins, void *arg)
19359{
Eric Biederman153ea352003-06-20 14:43:20 +000019360 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019361 struct triple *tangle;
19362 do {
19363 char used[MAX_REGISTERS];
19364 struct triple_reg_set *set;
19365 tangle = 0;
19366
19367 /* Find out which registers have multiple uses at this point */
19368 memset(used, 0, sizeof(used));
19369 for(set = live; set; set = set->next) {
19370 struct reg_info info;
19371 info = read_lhs_color(state, set->member, 0);
19372 if (info.reg == REG_UNSET) {
19373 continue;
19374 }
19375 reg_inc_used(state, used, info.reg);
19376 }
19377
19378 /* Now find the least dominated definition of a register in
19379 * conflict I have seen so far.
19380 */
19381 for(set = live; set; set = set->next) {
19382 struct reg_info info;
19383 info = read_lhs_color(state, set->member, 0);
19384 if (used[info.reg] < 2) {
19385 continue;
19386 }
Eric Biederman153ea352003-06-20 14:43:20 +000019387 /* Changing copies that feed into phi functions
19388 * is incorrect.
19389 */
19390 if (set->member->use &&
19391 (set->member->use->member->op == OP_PHI)) {
19392 continue;
19393 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019394 if (!tangle || tdominates(state, set->member, tangle)) {
19395 tangle = set->member;
19396 }
19397 }
19398 /* If I have found a tangle resolve it */
19399 if (tangle) {
19400 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019401 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019402 post_copy = resolve_tangle(state, tangle);
19403 if (post_copy) {
19404 replace_block_use(state, blocks, tangle, post_copy);
19405 }
19406 if (post_copy && (tangle != ins)) {
19407 replace_set_use(state, live, tangle, post_copy);
19408 }
19409 }
19410 } while(tangle);
19411 return;
19412}
19413
Eric Biederman153ea352003-06-20 14:43:20 +000019414static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019415 struct compile_state *state, struct reg_block *blocks)
19416{
Eric Biederman153ea352003-06-20 14:43:20 +000019417 int tangles;
19418 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019419 color_instructions(state);
Eric Biederman90089602004-05-28 14:11:54 +000019420 walk_variable_lifetimes(state, &state->bb, blocks,
19421 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019422 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019423}
19424
Eric Biedermand1ea5392003-06-28 06:49:45 +000019425
19426static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19427static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19428
19429struct triple *find_constrained_def(
19430 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019431{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019432 struct live_range_def *lrd, *lrd_next;
19433 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019434 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019435 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019436 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019437
19438 lrd = lrd_next;
19439 lrd_next = lrd->next;
19440
Eric Biedermand1ea5392003-06-28 06:49:45 +000019441 regcm = arch_type_to_regcm(state, lrd->def->type);
19442 info = find_lhs_color(state, lrd->def, 0);
19443 regcm = arch_regcm_reg_normalize(state, regcm);
19444 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019445 /* If the 2 register class masks are equal then
19446 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019447 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019448 if (regcm == info.regcm) {
19449 continue;
19450 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019451
Eric Biederman5ade04a2003-10-22 04:03:46 +000019452 /* If there is just one use.
19453 * That use cannot accept a larger register class.
19454 * There are no intervening definitions except
19455 * definitions that feed into that use.
19456 * Then a triple is not constrained.
19457 * FIXME handle this case!
19458 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019459#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019460#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019461#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019462
19463
Eric Biedermand1ea5392003-06-28 06:49:45 +000019464 /* Of the constrained live ranges deal with the
19465 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019466 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019467 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019468 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019469 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019470 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019471 if (!constrained ||
19472 tdominates(state, lrd->def, constrained))
19473 {
19474 constrained = lrd->def;
19475 }
19476 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019477 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019478}
19479
Eric Biedermand1ea5392003-06-28 06:49:45 +000019480static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019481 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019482 struct live_range *range)
19483{
19484 /* Walk through the edges in conflict and our current live
19485 * range, and find definitions that are more severly constrained
19486 * than they type of data they contain require.
19487 *
19488 * Then pick one of those ranges and relax the constraints.
19489 */
19490 struct live_range_edge *edge;
19491 struct triple *constrained;
19492
19493 constrained = 0;
19494 for(edge = range->edges; edge; edge = edge->next) {
19495 constrained = find_constrained_def(state, edge->node, constrained);
19496 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019497#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019498#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019499#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019500 if (!constrained) {
19501 constrained = find_constrained_def(state, range, constrained);
19502 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019503
19504 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019505 fprintf(state->errout, "constrained: ");
19506 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019507 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019508 if (constrained) {
19509 ids_from_rstate(state, rstate);
19510 cleanup_rstate(state, rstate);
19511 resolve_tangle(state, constrained);
19512 }
19513 return !!constrained;
19514}
19515
19516static int split_ranges(
19517 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019518 char *used, struct live_range *range)
19519{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019520 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019521 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019522 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019523 rstate->passes, tops(range->defs->def->op), range->defs->def);
19524 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019525 if ((range->color == REG_UNNEEDED) ||
19526 (rstate->passes >= rstate->max_passes)) {
19527 return 0;
19528 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019529 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019530
Eric Biedermand1ea5392003-06-28 06:49:45 +000019531 /* Ideally I would split the live range that will not be used
19532 * for the longest period of time in hopes that this will
19533 * (a) allow me to spill a register or
19534 * (b) allow me to place a value in another register.
19535 *
19536 * So far I don't have a test case for this, the resolving
19537 * of mandatory constraints has solved all of my
19538 * know issues. So I have choosen not to write any
19539 * code until I cat get a better feel for cases where
19540 * it would be useful to have.
19541 *
19542 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019543#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019544#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019545#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019546
19547 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019548 FILE *fp = state->errout;
19549 print_interference_blocks(state, rstate, fp, 0);
19550 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019551 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019552 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019553}
19554
Eric Biederman5ade04a2003-10-22 04:03:46 +000019555static FILE *cgdebug_fp(struct compile_state *state)
19556{
19557 FILE *fp;
19558 fp = 0;
19559 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019560 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019561 }
19562 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019563 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019564 }
19565 return fp;
19566}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019567
Eric Biederman5ade04a2003-10-22 04:03:46 +000019568static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19569{
19570 FILE *fp;
19571 fp = cgdebug_fp(state);
19572 if (fp) {
19573 va_list args;
19574 va_start(args, fmt);
19575 vfprintf(fp, fmt, args);
19576 va_end(args);
19577 }
19578}
19579
19580static void cgdebug_flush(struct compile_state *state)
19581{
19582 FILE *fp;
19583 fp = cgdebug_fp(state);
19584 if (fp) {
19585 fflush(fp);
19586 }
19587}
19588
19589static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19590{
19591 FILE *fp;
19592 fp = cgdebug_fp(state);
19593 if (fp) {
19594 loc(fp, state, ins);
19595 }
19596}
19597
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019598static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019599 struct reg_state *rstate, struct live_range *range)
19600{
19601 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019602 struct live_range_def *lrd;
19603 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019604 struct live_range_edge *edge;
19605 char used[MAX_REGISTERS];
19606 struct triple **expr;
19607
Eric Biedermanb138ac82003-04-22 18:44:01 +000019608 /* Instead of doing just the trivial color select here I try
19609 * a few extra things because a good color selection will help reduce
19610 * copies.
19611 */
19612
19613 /* Find the registers currently in use */
19614 memset(used, 0, sizeof(used));
19615 for(edge = range->edges; edge; edge = edge->next) {
19616 if (edge->node->color == REG_UNSET) {
19617 continue;
19618 }
19619 reg_fill_used(state, used, edge->node->color);
19620 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019621
19622 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019623 int i;
19624 i = 0;
19625 for(edge = range->edges; edge; edge = edge->next) {
19626 i++;
19627 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019628 cgdebug_printf(state, "\n%s edges: %d",
19629 tops(range->defs->def->op), i);
19630 cgdebug_loc(state, range->defs->def);
19631 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019632 for(i = 0; i < MAX_REGISTERS; i++) {
19633 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019634 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019635 arch_reg_str(i));
19636 }
19637 }
19638 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019639
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019640 /* If a color is already assigned see if it will work */
19641 if (range->color != REG_UNSET) {
19642 struct live_range_def *lrd;
19643 if (!used[range->color]) {
19644 return 1;
19645 }
19646 for(edge = range->edges; edge; edge = edge->next) {
19647 if (edge->node->color != range->color) {
19648 continue;
19649 }
19650 warning(state, edge->node->defs->def, "edge: ");
19651 lrd = edge->node->defs;
19652 do {
19653 warning(state, lrd->def, " %p %s",
19654 lrd->def, tops(lrd->def->op));
19655 lrd = lrd->next;
19656 } while(lrd != edge->node->defs);
19657 }
19658 lrd = range->defs;
19659 warning(state, range->defs->def, "def: ");
19660 do {
19661 warning(state, lrd->def, " %p %s",
19662 lrd->def, tops(lrd->def->op));
19663 lrd = lrd->next;
19664 } while(lrd != range->defs);
19665 internal_error(state, range->defs->def,
19666 "live range with already used color %s",
19667 arch_reg_str(range->color));
19668 }
19669
Eric Biedermanb138ac82003-04-22 18:44:01 +000019670 /* If I feed into an expression reuse it's color.
19671 * This should help remove copies in the case of 2 register instructions
19672 * and phi functions.
19673 */
19674 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019675 lrd = live_range_end(state, range, 0);
19676 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19677 entry = lrd->def->use;
19678 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19679 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019680 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019681 insd = &rstate->lrd[entry->member->id];
19682 if (insd->lr->defs == 0) {
19683 continue;
19684 }
19685 if (!phi && (insd->def->op == OP_PHI) &&
19686 !interfere(rstate, range, insd->lr)) {
19687 phi = insd;
19688 }
Eric Biederman530b5192003-07-01 10:05:30 +000019689 if (insd->lr->color == REG_UNSET) {
19690 continue;
19691 }
19692 regcm = insd->lr->classes;
19693 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019694 (used[insd->lr->color])) {
19695 continue;
19696 }
19697 if (interfere(rstate, range, insd->lr)) {
19698 continue;
19699 }
19700 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019701 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019702 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019703 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019704 * of something else that feeds into the phi function.
19705 */
19706 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019707 if (phi->lr->color != REG_UNSET) {
19708 if (used[phi->lr->color]) {
19709 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019710 }
19711 }
19712 else {
19713 expr = triple_rhs(state, phi->def, 0);
19714 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19715 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019716 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019717 if (!*expr) {
19718 continue;
19719 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019720 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019721 if (lr->color == REG_UNSET) {
19722 continue;
19723 }
19724 regcm = lr->classes;
19725 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019726 (used[lr->color])) {
19727 continue;
19728 }
19729 if (interfere(rstate, range, lr)) {
19730 continue;
19731 }
19732 range->color = lr->color;
19733 }
19734 }
19735 }
19736 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019737 lrd = live_range_head(state, range, 0);
19738 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19739 expr = triple_rhs(state, lrd->def, 0);
19740 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019741 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019742 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019743 if (!*expr) {
19744 continue;
19745 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019746 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019747 if (lr->color == REG_UNSET) {
19748 continue;
19749 }
19750 regcm = lr->classes;
19751 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019752 (used[lr->color])) {
19753 continue;
19754 }
19755 if (interfere(rstate, range, lr)) {
19756 continue;
19757 }
19758 range->color = lr->color;
19759 break;
19760 }
19761 }
19762 /* If I have not opportunitically picked a useful color
19763 * pick the first color that is free.
19764 */
19765 if (range->color == REG_UNSET) {
19766 range->color =
19767 arch_select_free_register(state, used, range->classes);
19768 }
19769 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019770 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019771 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019772 if (split_ranges(state, rstate, used, range)) {
19773 return 0;
19774 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019775 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019776 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019777 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019778 lrd = edge->node->defs;
19779 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019780 warning(state, lrd->def, " %s %p",
19781 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019782 lrd = lrd->next;
19783 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019784 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019785 warning(state, range->defs->def, "range: ");
19786 lrd = range->defs;
19787 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019788 warning(state, lrd->def, " %s %p",
19789 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019790 lrd = lrd->next;
19791 } while(lrd != range->defs);
19792
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019793 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019794 range->classes);
19795 for(i = 0; i < MAX_REGISTERS; i++) {
19796 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019797 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019798 arch_reg_str(i));
19799 }
19800 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019801 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019802 }
Eric Biederman530b5192003-07-01 10:05:30 +000019803 range->classes &= arch_reg_regcm(state, range->color);
19804 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019805 internal_error(state, range->defs->def, "select_free_color did not?");
19806 }
19807 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019808}
19809
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019810static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019811{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019812 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019813 struct live_range_edge *edge;
19814 struct live_range *range;
19815 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019816 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019817 range = rstate->low;
19818 if (*range->group_prev != range) {
19819 internal_error(state, 0, "lo: *prev != range?");
19820 }
19821 *range->group_prev = range->group_next;
19822 if (range->group_next) {
19823 range->group_next->group_prev = range->group_prev;
19824 }
19825 if (&range->group_next == rstate->low_tail) {
19826 rstate->low_tail = range->group_prev;
19827 }
19828 if (rstate->low == range) {
19829 internal_error(state, 0, "low: next != prev?");
19830 }
19831 }
19832 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019833 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019834 range = rstate->high;
19835 if (*range->group_prev != range) {
19836 internal_error(state, 0, "hi: *prev != range?");
19837 }
19838 *range->group_prev = range->group_next;
19839 if (range->group_next) {
19840 range->group_next->group_prev = range->group_prev;
19841 }
19842 if (&range->group_next == rstate->high_tail) {
19843 rstate->high_tail = range->group_prev;
19844 }
19845 if (rstate->high == range) {
19846 internal_error(state, 0, "high: next != prev?");
19847 }
19848 }
19849 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019850 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019851 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019852 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019853 range->group_prev = 0;
19854 for(edge = range->edges; edge; edge = edge->next) {
19855 struct live_range *node;
19856 node = edge->node;
19857 /* Move nodes from the high to the low list */
19858 if (node->group_prev && (node->color == REG_UNSET) &&
19859 (node->degree == regc_max_size(state, node->classes))) {
19860 if (*node->group_prev != node) {
19861 internal_error(state, 0, "move: *prev != node?");
19862 }
19863 *node->group_prev = node->group_next;
19864 if (node->group_next) {
19865 node->group_next->group_prev = node->group_prev;
19866 }
19867 if (&node->group_next == rstate->high_tail) {
19868 rstate->high_tail = node->group_prev;
19869 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019870 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019871 node->group_prev = rstate->low_tail;
19872 node->group_next = 0;
19873 *rstate->low_tail = node;
19874 rstate->low_tail = &node->group_next;
19875 if (*node->group_prev != node) {
19876 internal_error(state, 0, "move2: *prev != node?");
19877 }
19878 }
19879 node->degree -= 1;
19880 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019881 colored = color_graph(state, rstate);
19882 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019883 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019884 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019885 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019886 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019887 if (colored) {
19888 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19889 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019890 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019891 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019892}
19893
Eric Biedermana96d6a92003-05-13 20:45:19 +000019894static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19895{
19896 struct live_range *lr;
19897 struct live_range_edge *edge;
19898 struct triple *ins, *first;
19899 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019900 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019901 ins = first;
19902 do {
19903 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019904 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019905 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019906 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019907 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019908 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019909 if (lr->color == REG_UNSET) {
19910 internal_error(state, ins,
19911 "triple without a color");
19912 }
19913 /* Find the registers used by the edges */
19914 memset(used, 0, sizeof(used));
19915 for(edge = lr->edges; edge; edge = edge->next) {
19916 if (edge->node->color == REG_UNSET) {
19917 internal_error(state, 0,
19918 "live range without a color");
19919 }
19920 reg_fill_used(state, used, edge->node->color);
19921 }
19922 if (used[lr->color]) {
19923 internal_error(state, ins,
19924 "triple with already used color");
19925 }
19926 }
19927 ins = ins->next;
19928 } while(ins != first);
19929}
19930
Eric Biedermanb138ac82003-04-22 18:44:01 +000019931static void color_triples(struct compile_state *state, struct reg_state *rstate)
19932{
Eric Biederman90089602004-05-28 14:11:54 +000019933 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019934 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019935 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019936 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019937 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019938 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019939 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019940 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019941 "triple without a live range");
19942 }
Eric Biederman90089602004-05-28 14:11:54 +000019943 lrd = &rstate->lrd[ins->id];
19944 lr = lrd->lr;
19945 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019946 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019947 ins = ins->next;
19948 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019949}
19950
Eric Biedermanb138ac82003-04-22 18:44:01 +000019951static struct live_range *merge_sort_lr(
19952 struct live_range *first, struct live_range *last)
19953{
19954 struct live_range *mid, *join, **join_tail, *pick;
19955 size_t size;
19956 size = (last - first) + 1;
19957 if (size >= 2) {
19958 mid = first + size/2;
19959 first = merge_sort_lr(first, mid -1);
19960 mid = merge_sort_lr(mid, last);
19961
19962 join = 0;
19963 join_tail = &join;
19964 /* merge the two lists */
19965 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019966 if ((first->degree < mid->degree) ||
19967 ((first->degree == mid->degree) &&
19968 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019969 pick = first;
19970 first = first->group_next;
19971 if (first) {
19972 first->group_prev = 0;
19973 }
19974 }
19975 else {
19976 pick = mid;
19977 mid = mid->group_next;
19978 if (mid) {
19979 mid->group_prev = 0;
19980 }
19981 }
19982 pick->group_next = 0;
19983 pick->group_prev = join_tail;
19984 *join_tail = pick;
19985 join_tail = &pick->group_next;
19986 }
19987 /* Splice the remaining list */
19988 pick = (first)? first : mid;
19989 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019990 if (pick) {
19991 pick->group_prev = join_tail;
19992 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019993 }
19994 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019995 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019996 first = 0;
19997 }
19998 join = first;
19999 }
20000 return join;
20001}
20002
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020003static void ids_from_rstate(struct compile_state *state,
20004 struct reg_state *rstate)
20005{
20006 struct triple *ins, *first;
20007 if (!rstate->defs) {
20008 return;
20009 }
20010 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020011 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020012 FILE *fp = state->dbgout;
20013 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020014 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020015 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020016 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020017 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020018 ins = first;
20019 do {
20020 if (ins->id) {
20021 struct live_range_def *lrd;
20022 lrd = &rstate->lrd[ins->id];
20023 ins->id = lrd->orig_id;
20024 }
20025 ins = ins->next;
20026 } while(ins != first);
20027}
20028
20029static void cleanup_live_edges(struct reg_state *rstate)
20030{
20031 int i;
20032 /* Free the edges on each node */
20033 for(i = 1; i <= rstate->ranges; i++) {
20034 remove_live_edges(rstate, &rstate->lr[i]);
20035 }
20036}
20037
20038static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20039{
20040 cleanup_live_edges(rstate);
20041 xfree(rstate->lrd);
20042 xfree(rstate->lr);
20043
20044 /* Free the variable lifetime information */
20045 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020046 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020047 }
20048 rstate->defs = 0;
20049 rstate->ranges = 0;
20050 rstate->lrd = 0;
20051 rstate->lr = 0;
20052 rstate->blocks = 0;
20053}
20054
Eric Biederman153ea352003-06-20 14:43:20 +000020055static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020056static void allocate_registers(struct compile_state *state)
20057{
20058 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020059 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020060
20061 /* Clear out the reg_state */
20062 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020063 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020064
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020065 do {
20066 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000020067 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000020068 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020069 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020070
Eric Biederman5ade04a2003-10-22 04:03:46 +000020071 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020072 FILE *fp = state->errout;
20073 fprintf(fp, "pass: %d\n", rstate.passes);
20074 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020075 }
Eric Biederman153ea352003-06-20 14:43:20 +000020076
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020077 /* Restore ids */
20078 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020079
Eric Biedermanf96a8102003-06-16 16:57:34 +000020080 /* Cleanup the temporary data structures */
20081 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020082
Eric Biedermanf96a8102003-06-16 16:57:34 +000020083 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020084 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020085
Eric Biedermanf96a8102003-06-16 16:57:34 +000020086 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000020087 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020088
Eric Biederman153ea352003-06-20 14:43:20 +000020089 /* Fix two simultaneous uses of the same register.
20090 * In a few pathlogical cases a partial untangle moves
20091 * the tangle to a part of the graph we won't revisit.
20092 * So we keep looping until we have no more tangle fixes
20093 * to apply.
20094 */
20095 do {
20096 tangles = correct_tangles(state, rstate.blocks);
20097 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020098
Eric Biederman5ade04a2003-10-22 04:03:46 +000020099
Eric Biederman90089602004-05-28 14:11:54 +000020100 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020101 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020102
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020103 /* Allocate and initialize the live ranges */
20104 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020105
Eric Biederman90089602004-05-28 14:11:54 +000020106 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020107 * buys me nothing. The code is left this way in case
20108 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020109 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020110 */
20111 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020112 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020113 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020114 }
20115
Eric Biederman153ea352003-06-20 14:43:20 +000020116 /* Remove any previous live edge calculations */
20117 cleanup_live_edges(&rstate);
20118
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020119 /* Compute the interference graph */
20120 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020121 state, &state->bb, rstate.blocks,
20122 graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020123
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020124 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020125 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020126 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020127 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020128 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020129 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020130 print_interference_ins, &rstate);
20131 }
20132
20133 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020134
Eric Biederman5ade04a2003-10-22 04:03:46 +000020135 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020136 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020137 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020138 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020139
Eric Biederman83b991a2003-10-11 06:20:25 +000020140#if DEBUG_CONSISTENCY > 1
20141# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020142 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020143# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020144 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020145 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020146 state, &state->bb, rstate.blocks,
20147 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020148# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020149 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020150#endif
20151#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020152
20153 /* Build the groups low and high. But with the nodes
20154 * first sorted by degree order.
20155 */
20156 rstate.low_tail = &rstate.low;
20157 rstate.high_tail = &rstate.high;
20158 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20159 if (rstate.high) {
20160 rstate.high->group_prev = &rstate.high;
20161 }
20162 for(point = &rstate.high; *point; point = &(*point)->group_next)
20163 ;
20164 rstate.high_tail = point;
20165 /* Walk through the high list and move everything that needs
20166 * to be onto low.
20167 */
20168 for(point = &rstate.high; *point; point = next) {
20169 struct live_range *range;
20170 next = &(*point)->group_next;
20171 range = *point;
20172
20173 /* If it has a low degree or it already has a color
20174 * place the node in low.
20175 */
20176 if ((range->degree < regc_max_size(state, range->classes)) ||
20177 (range->color != REG_UNSET)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020178 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020179 range - rstate.lr, range->degree,
20180 (range->color != REG_UNSET) ? " (colored)": "");
20181 *range->group_prev = range->group_next;
20182 if (range->group_next) {
20183 range->group_next->group_prev = range->group_prev;
20184 }
20185 if (&range->group_next == rstate.high_tail) {
20186 rstate.high_tail = range->group_prev;
20187 }
20188 range->group_prev = rstate.low_tail;
20189 range->group_next = 0;
20190 *rstate.low_tail = range;
20191 rstate.low_tail = &range->group_next;
20192 next = point;
20193 }
20194 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020195 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020196 range - rstate.lr, range->degree,
20197 (range->color != REG_UNSET) ? " (colored)": "");
20198 }
20199 }
20200 /* Color the live_ranges */
20201 colored = color_graph(state, &rstate);
20202 rstate.passes++;
20203 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020204
Eric Biedermana96d6a92003-05-13 20:45:19 +000020205 /* Verify the graph was properly colored */
20206 verify_colors(state, &rstate);
20207
Eric Biedermanb138ac82003-04-22 18:44:01 +000020208 /* Move the colors from the graph to the triples */
20209 color_triples(state, &rstate);
20210
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020211 /* Cleanup the temporary data structures */
20212 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020213
20214 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020215 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020216}
20217
20218/* Sparce Conditional Constant Propogation
20219 * =========================================
20220 */
20221struct ssa_edge;
20222struct flow_block;
20223struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020224 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020225 struct triple *def;
20226 struct ssa_edge *out;
20227 struct flow_block *fblock;
20228 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020229 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020230 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020231 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020232 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020233};
20234struct ssa_edge {
20235 struct lattice_node *src;
20236 struct lattice_node *dst;
20237 struct ssa_edge *work_next;
20238 struct ssa_edge *work_prev;
20239 struct ssa_edge *out_next;
20240};
20241struct flow_edge {
20242 struct flow_block *src;
20243 struct flow_block *dst;
20244 struct flow_edge *work_next;
20245 struct flow_edge *work_prev;
20246 struct flow_edge *in_next;
20247 struct flow_edge *out_next;
20248 int executable;
20249};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020250#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020251struct flow_block {
20252 struct block *block;
20253 struct flow_edge *in;
20254 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020255 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020256};
20257
20258struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020259 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020260 struct lattice_node *lattice;
20261 struct ssa_edge *ssa_edges;
20262 struct flow_block *flow_blocks;
20263 struct flow_edge *flow_work_list;
20264 struct ssa_edge *ssa_work_list;
20265};
20266
20267
Eric Biederman90089602004-05-28 14:11:54 +000020268static int is_scc_const(struct compile_state *state, struct triple *ins)
20269{
20270 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20271}
20272
20273static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20274{
20275 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20276}
20277
20278static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20279{
20280 return is_scc_const(state, lnode->val);
20281}
20282
20283static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20284{
20285 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20286}
20287
Eric Biedermanb138ac82003-04-22 18:44:01 +000020288static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20289 struct flow_edge *fedge)
20290{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020291 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020292 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020293 fedge,
20294 fedge->src->block?fedge->src->block->last->id: 0,
20295 fedge->dst->block?fedge->dst->block->first->id: 0);
20296 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020297 if ((fedge == scc->flow_work_list) ||
20298 (fedge->work_next != fedge) ||
20299 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020300
20301 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020302 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020303 fedge);
20304 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020305 return;
20306 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020307 if (!scc->flow_work_list) {
20308 scc->flow_work_list = fedge;
20309 fedge->work_next = fedge->work_prev = fedge;
20310 }
20311 else {
20312 struct flow_edge *ftail;
20313 ftail = scc->flow_work_list->work_prev;
20314 fedge->work_next = ftail->work_next;
20315 fedge->work_prev = ftail;
20316 fedge->work_next->work_prev = fedge;
20317 fedge->work_prev->work_next = fedge;
20318 }
20319}
20320
20321static struct flow_edge *scc_next_fedge(
20322 struct compile_state *state, struct scc_state *scc)
20323{
20324 struct flow_edge *fedge;
20325 fedge = scc->flow_work_list;
20326 if (fedge) {
20327 fedge->work_next->work_prev = fedge->work_prev;
20328 fedge->work_prev->work_next = fedge->work_next;
20329 if (fedge->work_next != fedge) {
20330 scc->flow_work_list = fedge->work_next;
20331 } else {
20332 scc->flow_work_list = 0;
20333 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020334 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020335 }
20336 return fedge;
20337}
20338
20339static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20340 struct ssa_edge *sedge)
20341{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020342 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020343 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20344 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020345 sedge->src->def->id,
20346 sedge->dst->def->id);
20347 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020348 if ((sedge == scc->ssa_work_list) ||
20349 (sedge->work_next != sedge) ||
20350 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020351
20352 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020353 fprintf(state->errout, "dupped sedge: %5ld\n",
20354 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020355 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020356 return;
20357 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020358 if (!scc->ssa_work_list) {
20359 scc->ssa_work_list = sedge;
20360 sedge->work_next = sedge->work_prev = sedge;
20361 }
20362 else {
20363 struct ssa_edge *stail;
20364 stail = scc->ssa_work_list->work_prev;
20365 sedge->work_next = stail->work_next;
20366 sedge->work_prev = stail;
20367 sedge->work_next->work_prev = sedge;
20368 sedge->work_prev->work_next = sedge;
20369 }
20370}
20371
20372static struct ssa_edge *scc_next_sedge(
20373 struct compile_state *state, struct scc_state *scc)
20374{
20375 struct ssa_edge *sedge;
20376 sedge = scc->ssa_work_list;
20377 if (sedge) {
20378 sedge->work_next->work_prev = sedge->work_prev;
20379 sedge->work_prev->work_next = sedge->work_next;
20380 if (sedge->work_next != sedge) {
20381 scc->ssa_work_list = sedge->work_next;
20382 } else {
20383 scc->ssa_work_list = 0;
20384 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020385 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020386 }
20387 return sedge;
20388}
20389
20390static void initialize_scc_state(
20391 struct compile_state *state, struct scc_state *scc)
20392{
20393 int ins_count, ssa_edge_count;
20394 int ins_index, ssa_edge_index, fblock_index;
20395 struct triple *first, *ins;
20396 struct block *block;
20397 struct flow_block *fblock;
20398
20399 memset(scc, 0, sizeof(*scc));
20400
20401 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020402 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020403 ins = first;
20404 ins_count = ssa_edge_count = 0;
20405 do {
20406 struct triple_set *edge;
20407 ins_count += 1;
20408 for(edge = ins->use; edge; edge = edge->next) {
20409 ssa_edge_count++;
20410 }
20411 ins = ins->next;
20412 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020413 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020414 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20415 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020416 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020417 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020418 scc->lattice =
20419 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20420 scc->ssa_edges =
20421 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20422 scc->flow_blocks =
Eric Biederman90089602004-05-28 14:11:54 +000020423 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020424 "flow_blocks");
20425
20426 /* Initialize pass one collect up the nodes */
20427 fblock = 0;
20428 block = 0;
20429 ins_index = ssa_edge_index = fblock_index = 0;
20430 ins = first;
20431 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020432 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20433 block = ins->u.block;
20434 if (!block) {
20435 internal_error(state, ins, "label without block");
20436 }
20437 fblock_index += 1;
20438 block->vertex = fblock_index;
20439 fblock = &scc->flow_blocks[fblock_index];
20440 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020441 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20442 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020443 }
20444 {
20445 struct lattice_node *lnode;
20446 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020447 lnode = &scc->lattice[ins_index];
20448 lnode->def = ins;
20449 lnode->out = 0;
20450 lnode->fblock = fblock;
20451 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020452 if (lnode->val->op == OP_UNKNOWNVAL) {
20453 lnode->val = 0; /* LATTICE LOW by definition */
20454 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020455 lnode->old_id = ins->id;
20456 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020457 }
20458 ins = ins->next;
20459 } while(ins != first);
20460 /* Initialize pass two collect up the edges */
20461 block = 0;
20462 fblock = 0;
20463 ins = first;
20464 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020465 {
20466 struct triple_set *edge;
20467 struct ssa_edge **stail;
20468 struct lattice_node *lnode;
20469 lnode = &scc->lattice[ins->id];
20470 lnode->out = 0;
20471 stail = &lnode->out;
20472 for(edge = ins->use; edge; edge = edge->next) {
20473 struct ssa_edge *sedge;
20474 ssa_edge_index += 1;
20475 sedge = &scc->ssa_edges[ssa_edge_index];
20476 *stail = sedge;
20477 stail = &sedge->out_next;
20478 sedge->src = lnode;
20479 sedge->dst = &scc->lattice[edge->member->id];
20480 sedge->work_next = sedge->work_prev = sedge;
20481 sedge->out_next = 0;
20482 }
20483 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020484 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20485 struct flow_edge *fedge, **ftail;
20486 struct block_set *bedge;
20487 block = ins->u.block;
20488 fblock = &scc->flow_blocks[block->vertex];
20489 fblock->in = 0;
20490 fblock->out = 0;
20491 ftail = &fblock->out;
20492
20493 fedge = fblock->edges;
20494 bedge = block->edges;
20495 for(; bedge; bedge = bedge->next, fedge++) {
20496 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20497 if (fedge->dst->block != bedge->member) {
20498 internal_error(state, 0, "block mismatch");
20499 }
20500 *ftail = fedge;
20501 ftail = &fedge->out_next;
20502 fedge->out_next = 0;
20503 }
20504 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20505 fedge->src = fblock;
20506 fedge->work_next = fedge->work_prev = fedge;
20507 fedge->executable = 0;
20508 }
20509 }
20510 ins = ins->next;
20511 } while (ins != first);
20512 block = 0;
20513 fblock = 0;
20514 ins = first;
20515 do {
20516 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20517 struct flow_edge **ftail;
20518 struct block_set *bedge;
20519 block = ins->u.block;
20520 fblock = &scc->flow_blocks[block->vertex];
20521 ftail = &fblock->in;
20522 for(bedge = block->use; bedge; bedge = bedge->next) {
20523 struct block *src_block;
20524 struct flow_block *sfblock;
20525 struct flow_edge *sfedge;
20526 src_block = bedge->member;
20527 sfblock = &scc->flow_blocks[src_block->vertex];
20528 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20529 if (sfedge->dst == fblock) {
20530 break;
20531 }
20532 }
20533 if (!sfedge) {
20534 internal_error(state, 0, "edge mismatch");
20535 }
20536 *ftail = sfedge;
20537 ftail = &sfedge->in_next;
20538 sfedge->in_next = 0;
20539 }
20540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020541 ins = ins->next;
20542 } while(ins != first);
20543 /* Setup a dummy block 0 as a node above the start node */
20544 {
20545 struct flow_block *fblock, *dst;
20546 struct flow_edge *fedge;
20547 fblock = &scc->flow_blocks[0];
20548 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020549 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020550 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020551 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020552 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020553 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020554 fedge->src = fblock;
20555 fedge->dst = dst;
20556 fedge->work_next = fedge;
20557 fedge->work_prev = fedge;
20558 fedge->in_next = fedge->dst->in;
20559 fedge->out_next = 0;
20560 fedge->executable = 0;
20561 fedge->dst->in = fedge;
20562
20563 /* Initialize the work lists */
20564 scc->flow_work_list = 0;
20565 scc->ssa_work_list = 0;
20566 scc_add_fedge(state, scc, fedge);
20567 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020568 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020569 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020570 ins_index, ssa_edge_index, fblock_index);
20571 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020572}
20573
20574
20575static void free_scc_state(
20576 struct compile_state *state, struct scc_state *scc)
20577{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020578 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020579 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020580 struct flow_block *fblock;
20581 fblock = &scc->flow_blocks[i];
20582 if (fblock->edges) {
20583 xfree(fblock->edges);
20584 fblock->edges = 0;
20585 }
20586 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020587 xfree(scc->flow_blocks);
20588 xfree(scc->ssa_edges);
20589 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020590
Eric Biedermanb138ac82003-04-22 18:44:01 +000020591}
20592
20593static struct lattice_node *triple_to_lattice(
20594 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20595{
20596 if (ins->id <= 0) {
20597 internal_error(state, ins, "bad id");
20598 }
20599 return &scc->lattice[ins->id];
20600}
20601
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020602static struct triple *preserve_lval(
20603 struct compile_state *state, struct lattice_node *lnode)
20604{
20605 struct triple *old;
20606 /* Preserve the original value */
20607 if (lnode->val) {
20608 old = dup_triple(state, lnode->val);
20609 if (lnode->val != lnode->def) {
20610 xfree(lnode->val);
20611 }
20612 lnode->val = 0;
20613 } else {
20614 old = 0;
20615 }
20616 return old;
20617}
20618
20619static int lval_changed(struct compile_state *state,
20620 struct triple *old, struct lattice_node *lnode)
20621{
20622 int changed;
20623 /* See if the lattice value has changed */
20624 changed = 1;
20625 if (!old && !lnode->val) {
20626 changed = 0;
20627 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020628 if (changed &&
20629 lnode->val && old &&
20630 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020631 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020632 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20633 changed = 0;
20634 }
20635 if (old) {
20636 xfree(old);
20637 }
20638 return changed;
20639
20640}
20641
Eric Biederman5ade04a2003-10-22 04:03:46 +000020642static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020643 struct compile_state *state, struct scc_state *scc,
20644 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020645{
Eric Biederman90089602004-05-28 14:11:54 +000020646 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20647 display_triple_changes(state->errout, lnode->val, lnode->def);
20648 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020649 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020650 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020651 struct triple *val, **expr;
20652 val = lnode->val? lnode->val : lnode->def;
20653 fprintf(fp, "%p %s %3d %10s (",
20654 lnode->def,
20655 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20656 lnode->def->id,
20657 tops(lnode->def->op));
20658 expr = triple_rhs(state, lnode->def, 0);
20659 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20660 if (*expr) {
20661 fprintf(fp, " %d", (*expr)->id);
20662 }
20663 }
20664 if (val->op == OP_INTCONST) {
20665 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20666 }
20667 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020668 (is_lattice_hi(state, lnode)? "hi":
20669 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020670 changed? "changed" : ""
20671 );
20672 }
20673}
20674
Eric Biedermanb138ac82003-04-22 18:44:01 +000020675static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20676 struct lattice_node *lnode)
20677{
20678 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020679 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020680 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020681 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020682
20683 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020684 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020685
Eric Biedermanb138ac82003-04-22 18:44:01 +000020686 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020687 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020688 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020689 scratch->next = scratch;
20690 scratch->prev = scratch;
20691 scratch->use = 0;
20692
Eric Biederman90089602004-05-28 14:11:54 +000020693 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020695 dexpr = &lnode->def->param[i];
20696 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020697 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020698 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20699 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020700 *dexpr) {
20701 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702 tmp = triple_to_lattice(state, scc, *dexpr);
20703 *vexpr = (tmp->val)? tmp->val : tmp->def;
20704 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020705 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020706 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020707 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020708 }
20709 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020710#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020711#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020712#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020713 /* So far it looks like only the strength reduction
20714 * optimization are things I need to worry about.
20715 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020716 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020717 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020718 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020719 internal_error(state, lnode->def, "scratch used?");
20720 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020721 if ((scratch->prev != scratch) ||
20722 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020723 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020724 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020725 internal_error(state, lnode->def, "scratch in list?");
20726 }
20727 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020728 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020729 for(i = 0; i < count; i++) {
20730 vexpr = &scratch->param[i];
20731 if (*vexpr) {
20732 unuse_triple(*vexpr, scratch);
20733 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020734 }
Eric Biederman90089602004-05-28 14:11:54 +000020735 if (lnode->val->op == OP_UNKNOWNVAL) {
20736 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020737 }
Eric Biederman90089602004-05-28 14:11:54 +000020738 /* Find the case when I am lattice high */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020739 if (lnode->val &&
20740 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020741 (memcmp(lnode->val->param, lnode->def->param,
20742 count * sizeof(lnode->val->param[0])) == 0) &&
20743 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020744 lnode->val = lnode->def;
20745 }
Eric Biederman90089602004-05-28 14:11:54 +000020746 /* Only allow lattice high when all of my inputs
20747 * are also lattice high. Occassionally I can
20748 * have constants with a lattice low input, so
20749 * I do not need to check that case.
20750 */
20751 if (is_lattice_hi(state, lnode)) {
20752 struct lattice_node *tmp;
20753 int rhs;
20754 rhs = lnode->val->rhs;
20755 for(i = 0; i < rhs; i++) {
20756 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20757 if (!is_lattice_hi(state, tmp)) {
20758 lnode->val = 0;
20759 break;
20760 }
20761 }
20762 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020763 /* Find the cases that are always lattice lo */
20764 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020765 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020766 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020767 lnode->val = 0;
20768 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020769 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020770 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020771 /* See if this value should not change */
Eric Biederman90089602004-05-28 14:11:54 +000020772 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020773 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020774 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020775 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020776#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020777#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020778#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020779 if (changed) {
20780 internal_warning(state, lnode->def, "non def changes value?");
20781 }
20782 lnode->val = 0;
20783 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020784
Eric Biederman83b991a2003-10-11 06:20:25 +000020785 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020786 if (lnode->val != scratch) {
20787 xfree(scratch);
20788 }
Eric Biederman90089602004-05-28 14:11:54 +000020789
Eric Biedermanb138ac82003-04-22 18:44:01 +000020790 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020791}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020792
Eric Biederman90089602004-05-28 14:11:54 +000020793
20794static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020795 struct lattice_node *lnode)
20796{
20797 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020798 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020799 int changed;
20800
20801 /* Update the branch value */
20802 changed = compute_lnode_val(state, scc, lnode);
20803 scc_debug_lnode(state, scc, lnode, changed);
20804
20805 /* This only applies to conditional branches */
20806 if (!triple_is_cbranch(state, lnode->def)) {
20807 internal_error(state, lnode->def, "not a conditional branch");
20808 }
20809
Eric Biederman5ade04a2003-10-22 04:03:46 +000020810 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020811 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020812 FILE *fp = state->errout;
20813 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020814 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020815 lnode->def->id);
20816
20817 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020818 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020819 }
Eric Biederman90089602004-05-28 14:11:54 +000020820 fprintf(fp, " )");
20821 if (lnode->def->rhs > 0) {
20822 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020823 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 }
Eric Biederman90089602004-05-28 14:11:54 +000020825 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020826 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020827 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020828 for(left = cond->fblock->out; left; left = left->out_next) {
20829 if (left->dst->block->first == lnode->def->next) {
20830 break;
20831 }
20832 }
20833 if (!left) {
20834 internal_error(state, lnode->def, "Cannot find left branch edge");
20835 }
20836 for(right = cond->fblock->out; right; right = right->out_next) {
20837 if (right->dst->block->first == TARG(lnode->def, 0)) {
20838 break;
20839 }
20840 }
20841 if (!right) {
20842 internal_error(state, lnode->def, "Cannot find right branch edge");
20843 }
Eric Biederman90089602004-05-28 14:11:54 +000020844 /* I should only come here if the controlling expressions value
20845 * has changed, which means it must be either a constant or lo.
20846 */
20847 if (is_lattice_hi(state, cond)) {
20848 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020849 return;
20850 }
Eric Biederman90089602004-05-28 14:11:54 +000020851 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020852 scc_add_fedge(state, scc, left);
20853 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020854 }
20855 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020856 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020857 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020858 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020859 }
20860
20861}
20862
Eric Biederman90089602004-05-28 14:11:54 +000020863
20864static void scc_add_sedge_dst(struct compile_state *state,
20865 struct scc_state *scc, struct ssa_edge *sedge)
20866{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020867 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020868 scc_visit_cbranch(state, scc, sedge->dst);
20869 }
20870 else if (triple_is_def(state, sedge->dst->def)) {
20871 scc_add_sedge(state, scc, sedge);
20872 }
20873}
20874
20875static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
20876 struct lattice_node *lnode)
20877{
20878 struct lattice_node *tmp;
20879 struct triple **slot, *old;
20880 struct flow_edge *fedge;
20881 int changed;
20882 int index;
20883 if (lnode->def->op != OP_PHI) {
20884 internal_error(state, lnode->def, "not phi");
20885 }
20886 /* Store the original value */
20887 old = preserve_lval(state, lnode);
20888
20889 /* default to lattice high */
20890 lnode->val = lnode->def;
20891 slot = &RHS(lnode->def, 0);
20892 index = 0;
20893 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20894 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20895 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
20896 index,
20897 fedge->dst->block->vertex,
20898 fedge->executable
20899 );
20900 }
20901 if (!fedge->executable) {
20902 continue;
20903 }
20904 if (!slot[index]) {
20905 internal_error(state, lnode->def, "no phi value");
20906 }
20907 tmp = triple_to_lattice(state, scc, slot[index]);
20908 /* meet(X, lattice low) = lattice low */
20909 if (is_lattice_lo(state, tmp)) {
20910 lnode->val = 0;
20911 }
20912 /* meet(X, lattice high) = X */
20913 else if (is_lattice_hi(state, tmp)) {
20914 lnode->val = lnode->val;
20915 }
20916 /* meet(lattice high, X) = X */
20917 else if (is_lattice_hi(state, lnode)) {
20918 lnode->val = dup_triple(state, tmp->val);
20919 /* Only change the type if necessary */
20920 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20921 lnode->val->type = lnode->def->type;
20922 }
20923 }
20924 /* meet(const, const) = const or lattice low */
20925 else if (!constants_equal(state, lnode->val, tmp->val)) {
20926 lnode->val = 0;
20927 }
20928
20929 /* meet(lattice low, X) = lattice low */
20930 if (is_lattice_lo(state, lnode)) {
20931 lnode->val = 0;
20932 break;
20933 }
20934 }
20935 changed = lval_changed(state, old, lnode);
20936 scc_debug_lnode(state, scc, lnode, changed);
20937
20938 /* If the lattice value has changed update the work lists. */
20939 if (changed) {
20940 struct ssa_edge *sedge;
20941 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20942 scc_add_sedge_dst(state, scc, sedge);
20943 }
20944 }
20945}
20946
20947
Eric Biedermanb138ac82003-04-22 18:44:01 +000020948static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20949 struct lattice_node *lnode)
20950{
20951 int changed;
20952
Eric Biederman90089602004-05-28 14:11:54 +000020953 if (!triple_is_def(state, lnode->def)) {
20954 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020955 }
Eric Biederman90089602004-05-28 14:11:54 +000020956 changed = compute_lnode_val(state, scc, lnode);
20957 scc_debug_lnode(state, scc, lnode, changed);
20958
20959 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020960 struct ssa_edge *sedge;
20961 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020962 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020963 }
20964 }
20965}
20966
20967static void scc_writeback_values(
20968 struct compile_state *state, struct scc_state *scc)
20969{
20970 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020971 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020972 ins = first;
20973 do {
20974 struct lattice_node *lnode;
20975 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020976 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020977 if (is_lattice_hi(state, lnode) &&
20978 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020979 {
20980 struct flow_edge *fedge;
20981 int executable;
20982 executable = 0;
20983 for(fedge = lnode->fblock->in;
20984 !executable && fedge; fedge = fedge->in_next) {
20985 executable |= fedge->executable;
20986 }
20987 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020988 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020989 "lattice node %d %s->%s still high?",
20990 ins->id,
20991 tops(lnode->def->op),
20992 tops(lnode->val->op));
20993 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020995 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020996
Eric Biederman83b991a2003-10-11 06:20:25 +000020997 /* Restore id */
20998 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020999 if (lnode->val && (lnode->val != ins)) {
21000 /* See if it something I know how to write back */
21001 switch(lnode->val->op) {
21002 case OP_INTCONST:
21003 mkconst(state, ins, lnode->val->u.cval);
21004 break;
21005 case OP_ADDRCONST:
21006 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021007 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021008 break;
21009 default:
21010 /* By default don't copy the changes,
21011 * recompute them in place instead.
21012 */
21013 simplify(state, ins);
21014 break;
21015 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021016 if (is_const(lnode->val) &&
21017 !constants_equal(state, lnode->val, ins)) {
21018 internal_error(state, 0, "constants not equal");
21019 }
21020 /* Free the lattice nodes */
21021 xfree(lnode->val);
21022 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021023 }
21024 ins = ins->next;
21025 } while(ins != first);
21026}
21027
21028static void scc_transform(struct compile_state *state)
21029{
21030 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021031 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21032 return;
21033 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021034
21035 initialize_scc_state(state, &scc);
21036
21037 while(scc.flow_work_list || scc.ssa_work_list) {
21038 struct flow_edge *fedge;
21039 struct ssa_edge *sedge;
21040 struct flow_edge *fptr;
21041 while((fedge = scc_next_fedge(state, &scc))) {
21042 struct block *block;
21043 struct triple *ptr;
21044 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021045 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021046 int done;
21047 if (fedge->executable) {
21048 continue;
21049 }
21050 if (!fedge->dst) {
21051 internal_error(state, 0, "fedge without dst");
21052 }
21053 if (!fedge->src) {
21054 internal_error(state, 0, "fedge without src");
21055 }
21056 fedge->executable = 1;
21057 fblock = fedge->dst;
21058 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021059 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021060 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21061 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021062 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021063 }
21064 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021065
Eric Biederman5ade04a2003-10-22 04:03:46 +000021066 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021067 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021068 block->vertex, reps);
21069 }
21070
Eric Biedermanb138ac82003-04-22 18:44:01 +000021071 done = 0;
21072 for(ptr = block->first; !done; ptr = ptr->next) {
21073 struct lattice_node *lnode;
21074 done = (ptr == block->last);
21075 lnode = &scc.lattice[ptr->id];
21076 if (ptr->op == OP_PHI) {
21077 scc_visit_phi(state, &scc, lnode);
21078 }
Eric Biederman90089602004-05-28 14:11:54 +000021079 else if ((reps == 1) && triple_is_def(state, ptr))
21080 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021081 scc_visit_expr(state, &scc, lnode);
21082 }
21083 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021084 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021085 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021086 struct flow_edge *out;
21087 for(out = fblock->out; out; out = out->out_next) {
21088 scc_add_fedge(state, &scc, out);
21089 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021090 }
21091 }
21092 while((sedge = scc_next_sedge(state, &scc))) {
21093 struct lattice_node *lnode;
21094 struct flow_block *fblock;
21095 lnode = sedge->dst;
21096 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021097
21098 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021099 fprintf(state->errout, "sedge: %5d (%5d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021100 sedge - scc.ssa_edges,
21101 sedge->src->def->id,
21102 sedge->dst->def->id);
21103 }
21104
Eric Biedermanb138ac82003-04-22 18:44:01 +000021105 if (lnode->def->op == OP_PHI) {
21106 scc_visit_phi(state, &scc, lnode);
21107 }
21108 else {
21109 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21110 if (fptr->executable) {
21111 break;
21112 }
21113 }
21114 if (fptr) {
21115 scc_visit_expr(state, &scc, lnode);
21116 }
21117 }
21118 }
21119 }
21120
21121 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021122 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021123 rebuild_ssa_form(state);
21124
Eric Biederman90089602004-05-28 14:11:54 +000021125 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021126}
21127
21128
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021129static void transform_to_arch_instructions(struct compile_state *state)
21130{
21131 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021132 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021133 ins = first;
21134 do {
21135 ins = transform_to_arch_instruction(state, ins);
21136 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021137
Eric Biederman90089602004-05-28 14:11:54 +000021138 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021139}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021140
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021141#if DEBUG_CONSISTENCY
21142static void verify_uses(struct compile_state *state)
21143{
21144 struct triple *first, *ins;
21145 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021146 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021147 ins = first;
21148 do {
21149 struct triple **expr;
21150 expr = triple_rhs(state, ins, 0);
21151 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021152 struct triple *rhs;
21153 rhs = *expr;
21154 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021155 if (set->member == ins) {
21156 break;
21157 }
21158 }
21159 if (!set) {
21160 internal_error(state, ins, "rhs not used");
21161 }
21162 }
21163 expr = triple_lhs(state, ins, 0);
21164 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021165 struct triple *lhs;
21166 lhs = *expr;
21167 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021168 if (set->member == ins) {
21169 break;
21170 }
21171 }
21172 if (!set) {
21173 internal_error(state, ins, "lhs not used");
21174 }
21175 }
Eric Biederman90089602004-05-28 14:11:54 +000021176 expr = triple_misc(state, ins, 0);
21177 if (ins->op != OP_PHI) {
21178 for(; expr; expr = triple_targ(state, ins, expr)) {
21179 struct triple *misc;
21180 misc = *expr;
21181 for(set = misc?misc->use:0; set; set = set->next) {
21182 if (set->member == ins) {
21183 break;
21184 }
21185 }
21186 if (!set) {
21187 internal_error(state, ins, "misc not used");
21188 }
21189 }
21190 }
21191 if (!triple_is_ret(state, ins)) {
21192 expr = triple_targ(state, ins, 0);
21193 for(; expr; expr = triple_targ(state, ins, expr)) {
21194 struct triple *targ;
21195 targ = *expr;
21196 for(set = targ?targ->use:0; set; set = set->next) {
21197 if (set->member == ins) {
21198 break;
21199 }
21200 }
21201 if (!set) {
21202 internal_error(state, ins, "targ not used");
21203 }
21204 }
21205 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021206 ins = ins->next;
21207 } while(ins != first);
21208
21209}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021210static void verify_blocks_present(struct compile_state *state)
21211{
21212 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021213 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021214 return;
21215 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021216 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021217 ins = first;
21218 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021219 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021220 if (triple_stores_block(state, ins)) {
21221 if (!ins->u.block) {
21222 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021223 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021224 }
21225 }
21226 ins = ins->next;
21227 } while(ins != first);
21228
21229
21230}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021231
21232static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21233{
21234 struct block_set *bedge;
21235 struct block *targ;
21236 targ = block_of_triple(state, edge);
21237 for(bedge = block->edges; bedge; bedge = bedge->next) {
21238 if (bedge->member == targ) {
21239 return 1;
21240 }
21241 }
21242 return 0;
21243}
21244
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021245static void verify_blocks(struct compile_state *state)
21246{
21247 struct triple *ins;
21248 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021249 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021250 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021251 if (!block) {
21252 return;
21253 }
Eric Biederman530b5192003-07-01 10:05:30 +000021254 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021255 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021256 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021257 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021258 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021259 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021260 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021261 internal_error(state, ins, "inconsitent block specified");
21262 }
Eric Biederman530b5192003-07-01 10:05:30 +000021263 valid_ins(state, ins);
21264 }
21265 users = 0;
21266 for(user = block->use; user; user = user->next) {
21267 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021268 if (!user->member->first) {
21269 internal_error(state, block->first, "user is empty");
21270 }
Eric Biederman90089602004-05-28 14:11:54 +000021271 if ((block == state->bb.last_block) &&
21272 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021273 continue;
21274 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021275 for(edge = user->member->edges; edge; edge = edge->next) {
21276 if (edge->member == block) {
21277 break;
21278 }
21279 }
21280 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021281 internal_error(state, user->member->first,
21282 "user does not use block");
21283 }
21284 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021285 if (triple_is_branch(state, block->last)) {
21286 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021287 expr = triple_edge_targ(state, block->last, 0);
21288 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021289 if (*expr && !edge_present(state, block, *expr)) {
21290 internal_error(state, block->last, "no edge to targ");
21291 }
21292 }
Eric Biederman530b5192003-07-01 10:05:30 +000021293 }
Eric Biederman90089602004-05-28 14:11:54 +000021294 if (!triple_is_ubranch(state, block->last) &&
21295 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021296 !edge_present(state, block, block->last->next)) {
21297 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021298 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021299 for(edge = block->edges; edge; edge = edge->next) {
21300 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021301 if (user->member == block) {
21302 break;
21303 }
21304 }
21305 if (!user || user->member != block) {
21306 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021307 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021308 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021309 if (!edge->member->first) {
21310 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021311 }
Eric Biederman530b5192003-07-01 10:05:30 +000021312 }
21313 if (block->users != users) {
21314 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021315 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021316 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021317 }
21318 if (!triple_stores_block(state, block->last->next)) {
21319 internal_error(state, block->last->next,
21320 "cannot find next block");
21321 }
21322 block = block->last->next->u.block;
21323 if (!block) {
21324 internal_error(state, block->last->next,
21325 "bad next block");
21326 }
Eric Biederman90089602004-05-28 14:11:54 +000021327 } while(block != state->bb.first_block);
21328 if (blocks != state->bb.last_vertex) {
21329 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21330 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021331 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021332}
21333
21334static void verify_domination(struct compile_state *state)
21335{
21336 struct triple *first, *ins;
21337 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021338 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021339 return;
21340 }
21341
Eric Biederman83b991a2003-10-11 06:20:25 +000021342 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021343 ins = first;
21344 do {
21345 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021346 struct triple **slot;
21347 struct triple *use_point;
21348 int i, zrhs;
21349 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021350 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021351 slot = &RHS(set->member, 0);
21352 /* See if the use is on the right hand side */
21353 for(i = 0; i < zrhs; i++) {
21354 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021355 break;
21356 }
21357 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021358 if (i < zrhs) {
21359 use_point = set->member;
21360 if (set->member->op == OP_PHI) {
21361 struct block_set *bset;
21362 int edge;
21363 bset = set->member->u.block->use;
21364 for(edge = 0; bset && (edge < i); edge++) {
21365 bset = bset->next;
21366 }
21367 if (!bset) {
21368 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021369 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021370 }
21371 use_point = bset->member->last;
21372 }
21373 }
21374 if (use_point &&
21375 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021376 if (is_const(ins)) {
21377 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021378 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021379 }
21380 else {
21381 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021382 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021383 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021384 }
21385 }
21386 ins = ins->next;
21387 } while(ins != first);
21388}
21389
Eric Biederman83b991a2003-10-11 06:20:25 +000021390static void verify_rhs(struct compile_state *state)
21391{
21392 struct triple *first, *ins;
21393 first = state->first;
21394 ins = first;
21395 do {
21396 struct triple **slot;
21397 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021398 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021399 slot = &RHS(ins, 0);
21400 for(i = 0; i < zrhs; i++) {
21401 if (slot[i] == 0) {
21402 internal_error(state, ins,
21403 "missing rhs %d on %s",
21404 i, tops(ins->op));
21405 }
21406 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21407 internal_error(state, ins,
21408 "ins == rhs[%d] on %s",
21409 i, tops(ins->op));
21410 }
21411 }
21412 ins = ins->next;
21413 } while(ins != first);
21414}
21415
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021416static void verify_piece(struct compile_state *state)
21417{
21418 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021419 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021420 ins = first;
21421 do {
21422 struct triple *ptr;
21423 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021424 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021425 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21426 if (ptr != LHS(ins, i)) {
21427 internal_error(state, ins, "malformed lhs on %s",
21428 tops(ins->op));
21429 }
21430 if (ptr->op != OP_PIECE) {
21431 internal_error(state, ins, "bad lhs op %s at %d on %s",
21432 tops(ptr->op), i, tops(ins->op));
21433 }
21434 if (ptr->u.cval != i) {
21435 internal_error(state, ins, "bad u.cval of %d %d expected",
21436 ptr->u.cval, i);
21437 }
21438 }
21439 ins = ins->next;
21440 } while(ins != first);
21441}
Eric Biederman83b991a2003-10-11 06:20:25 +000021442
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021443static void verify_ins_colors(struct compile_state *state)
21444{
21445 struct triple *first, *ins;
21446
Eric Biederman83b991a2003-10-11 06:20:25 +000021447 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021448 ins = first;
21449 do {
21450 ins = ins->next;
21451 } while(ins != first);
21452}
Eric Biederman90089602004-05-28 14:11:54 +000021453
21454static void verify_unknown(struct compile_state *state)
21455{
21456 struct triple *first, *ins;
21457 if ( (unknown_triple.next != &unknown_triple) ||
21458 (unknown_triple.prev != &unknown_triple) ||
21459#if 0
21460 (unknown_triple.use != 0) ||
21461#endif
21462 (unknown_triple.op != OP_UNKNOWNVAL) ||
21463 (unknown_triple.lhs != 0) ||
21464 (unknown_triple.rhs != 0) ||
21465 (unknown_triple.misc != 0) ||
21466 (unknown_triple.targ != 0) ||
21467 (unknown_triple.template_id != 0) ||
21468 (unknown_triple.id != -1) ||
21469 (unknown_triple.type != &unknown_type) ||
21470 (unknown_triple.occurance != &dummy_occurance) ||
21471 (unknown_triple.param[0] != 0) ||
21472 (unknown_triple.param[1] != 0)) {
21473 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21474 }
21475 if ( (dummy_occurance.count != 2) ||
21476 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21477 (strcmp(dummy_occurance.function, "") != 0) ||
21478 (dummy_occurance.col != 0) ||
21479 (dummy_occurance.parent != 0)) {
21480 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21481 }
21482 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21483 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21484 }
21485 first = state->first;
21486 ins = first;
21487 do {
21488 int params, i;
21489 if (ins == &unknown_triple) {
21490 internal_error(state, ins, "unknown triple in list");
21491 }
21492 params = TRIPLE_SIZE(ins);
21493 for(i = 0; i < params; i++) {
21494 if (ins->param[i] == &unknown_triple) {
21495 internal_error(state, ins, "unknown triple used!");
21496 }
21497 }
21498 ins = ins->next;
21499 } while(ins != first);
21500}
21501
21502static void verify_types(struct compile_state *state)
21503{
21504 struct triple *first, *ins;
21505 first = state->first;
21506 ins = first;
21507 do {
21508 struct type *invalid;
21509 invalid = invalid_type(state, ins->type);
21510 if (invalid) {
21511 FILE *fp = state->errout;
21512 fprintf(fp, "type: ");
21513 name_of(fp, ins->type);
21514 fprintf(fp, "\n");
21515 fprintf(fp, "invalid type: ");
21516 name_of(fp, invalid);
21517 fprintf(fp, "\n");
21518 internal_error(state, ins, "invalid ins type");
21519 }
21520 } while(ins != first);
21521}
21522
21523static void verify_copy(struct compile_state *state)
21524{
21525 struct triple *first, *ins, *next;
21526 first = state->first;
21527 next = ins = first;
21528 do {
21529 ins = next;
21530 next = ins->next;
21531 if (ins->op != OP_COPY) {
21532 continue;
21533 }
21534 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21535 FILE *fp = state->errout;
21536 fprintf(fp, "src type: ");
21537 name_of(fp, RHS(ins, 0)->type);
21538 fprintf(fp, "\n");
21539 fprintf(fp, "dst type: ");
21540 name_of(fp, ins->type);
21541 fprintf(fp, "\n");
21542 internal_error(state, ins, "type mismatch in copy");
21543 }
21544 } while(next != first);
21545}
21546
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021547static void verify_consistency(struct compile_state *state)
21548{
Eric Biederman90089602004-05-28 14:11:54 +000021549 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021550 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021551 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021552 verify_blocks(state);
21553 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021554 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021555 verify_piece(state);
21556 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021557 verify_types(state);
21558 verify_copy(state);
21559 if (state->compiler->debug & DEBUG_VERIFICATION) {
21560 fprintf(state->dbgout, "consistency verified\n");
21561 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021562}
21563#else
Eric Biederman153ea352003-06-20 14:43:20 +000021564static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021565#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021566
21567static void optimize(struct compile_state *state)
21568{
Eric Biederman90089602004-05-28 14:11:54 +000021569 /* Join all of the functions into one giant function */
21570 join_functions(state);
21571
Eric Biederman5ade04a2003-10-22 04:03:46 +000021572 /* Dump what the instruction graph intially looks like */
21573 print_triples(state);
21574
Eric Biederman0babc1c2003-05-09 02:39:00 +000021575 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021576 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021577 print_triples(state);
21578
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021579 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021580 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021581 state->bb.first = state->first;
21582 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021583
Eric Biederman530b5192003-07-01 10:05:30 +000021584 /* Transform the code to ssa form. */
21585 /*
21586 * The transformation to ssa form puts a phi function
21587 * on each of edge of a dominance frontier where that
21588 * phi function might be needed. At -O2 if we don't
21589 * eleminate the excess phi functions we can get an
21590 * exponential code size growth. So I kill the extra
21591 * phi functions early and I kill them often.
21592 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021593 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021594 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021595
Eric Biederman83b991a2003-10-11 06:20:25 +000021596 /* Remove dead code */
21597 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021598 verify_consistency(state);
21599
Eric Biedermanb138ac82003-04-22 18:44:01 +000021600 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021601 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021602 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021603 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021604 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021605 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021606#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021607#warning "WISHLIST implement single use constants (least possible register pressure)"
21608#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021609#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021610 /* Select architecture instructions and an initial partial
21611 * coloring based on architecture constraints.
21612 */
21613 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021614 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021615
Eric Biederman83b991a2003-10-11 06:20:25 +000021616 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021617 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021618 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021619
Eric Biedermanb138ac82003-04-22 18:44:01 +000021620 /* Color all of the variables to see if they will fit in registers */
21621 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021622 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021623
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021624 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021625 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021626
Eric Biedermanb138ac82003-04-22 18:44:01 +000021627 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021628 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021629
Eric Biedermanb138ac82003-04-22 18:44:01 +000021630 /* Remove the optimization information.
21631 * This is more to check for memory consistency than to free memory.
21632 */
Eric Biederman90089602004-05-28 14:11:54 +000021633 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021634}
21635
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021636static void print_op_asm(struct compile_state *state,
21637 struct triple *ins, FILE *fp)
21638{
21639 struct asm_info *info;
21640 const char *ptr;
21641 unsigned lhs, rhs, i;
21642 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021643 lhs = ins->lhs;
21644 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021645 /* Don't count the clobbers in lhs */
21646 for(i = 0; i < lhs; i++) {
21647 if (LHS(ins, i)->type == &void_type) {
21648 break;
21649 }
21650 }
21651 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021652 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021653 fputc('\t', fp);
21654 for(ptr = info->str; *ptr; ptr++) {
21655 char *next;
21656 unsigned long param;
21657 struct triple *piece;
21658 if (*ptr != '%') {
21659 fputc(*ptr, fp);
21660 continue;
21661 }
21662 ptr++;
21663 if (*ptr == '%') {
21664 fputc('%', fp);
21665 continue;
21666 }
21667 param = strtoul(ptr, &next, 10);
21668 if (ptr == next) {
21669 error(state, ins, "Invalid asm template");
21670 }
21671 if (param >= (lhs + rhs)) {
21672 error(state, ins, "Invalid param %%%u in asm template",
21673 param);
21674 }
21675 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21676 fprintf(fp, "%s",
21677 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021678 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021679 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021680 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021681}
21682
21683
21684/* Only use the low x86 byte registers. This allows me
21685 * allocate the entire register when a byte register is used.
21686 */
21687#define X86_4_8BIT_GPRS 1
21688
Eric Biederman83b991a2003-10-11 06:20:25 +000021689/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021690#define X86_MMX_REGS (1<<0)
21691#define X86_XMM_REGS (1<<1)
21692#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021693
Eric Biedermanb138ac82003-04-22 18:44:01 +000021694/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021695#define REGC_FLAGS 0
21696#define REGC_GPR8 1
21697#define REGC_GPR16 2
21698#define REGC_GPR32 3
21699#define REGC_DIVIDEND64 4
21700#define REGC_DIVIDEND32 5
21701#define REGC_MMX 6
21702#define REGC_XMM 7
21703#define REGC_GPR32_8 8
21704#define REGC_GPR16_8 9
21705#define REGC_GPR8_LO 10
21706#define REGC_IMM32 11
21707#define REGC_IMM16 12
21708#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021709#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021710#if LAST_REGC >= MAX_REGC
21711#error "MAX_REGC is to low"
21712#endif
21713
21714/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021715#define REGCM_FLAGS (1 << REGC_FLAGS)
21716#define REGCM_GPR8 (1 << REGC_GPR8)
21717#define REGCM_GPR16 (1 << REGC_GPR16)
21718#define REGCM_GPR32 (1 << REGC_GPR32)
21719#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21720#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21721#define REGCM_MMX (1 << REGC_MMX)
21722#define REGCM_XMM (1 << REGC_XMM)
21723#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21724#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21725#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21726#define REGCM_IMM32 (1 << REGC_IMM32)
21727#define REGCM_IMM16 (1 << REGC_IMM16)
21728#define REGCM_IMM8 (1 << REGC_IMM8)
21729#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021730#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021731
21732/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021733#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021734#define REGC_FLAGS_FIRST REG_EFLAGS
21735#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021736#define REG_AL 3
21737#define REG_BL 4
21738#define REG_CL 5
21739#define REG_DL 6
21740#define REG_AH 7
21741#define REG_BH 8
21742#define REG_CH 9
21743#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021744#define REGC_GPR8_LO_FIRST REG_AL
21745#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021746#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021747#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021748#define REG_AX 11
21749#define REG_BX 12
21750#define REG_CX 13
21751#define REG_DX 14
21752#define REG_SI 15
21753#define REG_DI 16
21754#define REG_BP 17
21755#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021756#define REGC_GPR16_FIRST REG_AX
21757#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021758#define REG_EAX 19
21759#define REG_EBX 20
21760#define REG_ECX 21
21761#define REG_EDX 22
21762#define REG_ESI 23
21763#define REG_EDI 24
21764#define REG_EBP 25
21765#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021766#define REGC_GPR32_FIRST REG_EAX
21767#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021768#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021769#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21770#define REGC_DIVIDEND64_LAST REG_EDXEAX
21771#define REG_DXAX 28
21772#define REGC_DIVIDEND32_FIRST REG_DXAX
21773#define REGC_DIVIDEND32_LAST REG_DXAX
21774#define REG_MMX0 29
21775#define REG_MMX1 30
21776#define REG_MMX2 31
21777#define REG_MMX3 32
21778#define REG_MMX4 33
21779#define REG_MMX5 34
21780#define REG_MMX6 35
21781#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021782#define REGC_MMX_FIRST REG_MMX0
21783#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021784#define REG_XMM0 37
21785#define REG_XMM1 38
21786#define REG_XMM2 39
21787#define REG_XMM3 40
21788#define REG_XMM4 41
21789#define REG_XMM5 42
21790#define REG_XMM6 43
21791#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021792#define REGC_XMM_FIRST REG_XMM0
21793#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021794
21795#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021796#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021797#endif
21798
Eric Biedermanb138ac82003-04-22 18:44:01 +000021799#define LAST_REG REG_XMM7
21800
21801#define REGC_GPR32_8_FIRST REG_EAX
21802#define REGC_GPR32_8_LAST REG_EDX
21803#define REGC_GPR16_8_FIRST REG_AX
21804#define REGC_GPR16_8_LAST REG_DX
21805
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021806#define REGC_IMM8_FIRST -1
21807#define REGC_IMM8_LAST -1
21808#define REGC_IMM16_FIRST -2
21809#define REGC_IMM16_LAST -1
21810#define REGC_IMM32_FIRST -4
21811#define REGC_IMM32_LAST -1
21812
Eric Biedermanb138ac82003-04-22 18:44:01 +000021813#if LAST_REG >= MAX_REGISTERS
21814#error "MAX_REGISTERS to low"
21815#endif
21816
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021817
21818static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021819 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21820 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21821 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21822 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21823 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21824 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21825 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21826 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21827 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21828 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21829 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21830 [REGC_IMM32] = 0,
21831 [REGC_IMM16] = 0,
21832 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021833};
21834
21835static const struct {
21836 int first, last;
21837} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021838 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21839 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21840 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21841 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21842 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21843 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21844 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21845 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21846 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21847 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21848 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21849 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21850 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21851 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021852};
21853
Eric Biederman90089602004-05-28 14:11:54 +000021854#if ARCH_INPUT_REGS != 4
21855#error ARCH_INPUT_REGS size mismatch
21856#endif
21857static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21858 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21859 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21860 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21861 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21862};
21863
21864#if ARCH_OUTPUT_REGS != 4
21865#error ARCH_INPUT_REGS size mismatch
21866#endif
21867static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21868 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21869 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21870 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21871 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21872};
21873
Eric Biederman5ade04a2003-10-22 04:03:46 +000021874static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021875{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021876 memset(arch, 0, sizeof(*arch));
21877 arch->features = 0;
21878}
21879
Eric Biederman90089602004-05-28 14:11:54 +000021880static const struct compiler_flag arch_flags[] = {
21881 { "mmx", X86_MMX_REGS },
21882 { "sse", X86_XMM_REGS },
21883 { "noop-copy", X86_NOOP_COPY },
21884 { 0, 0 },
21885};
21886static const struct compiler_flag arch_cpus[] = {
21887 { "i386", 0 },
21888 { "p2", X86_MMX_REGS },
21889 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21890 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21891 { "k7", X86_MMX_REGS },
21892 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21893 { "c3", X86_MMX_REGS },
21894 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21895 { 0, 0 }
21896};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021897static int arch_encode_flag(struct arch_state *arch, const char *flag)
21898{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021899 int result;
21900 int act;
21901
21902 act = 1;
21903 result = -1;
21904 if (strncmp(flag, "no-", 3) == 0) {
21905 flag += 3;
21906 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021907 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021908 if (act && strncmp(flag, "cpu=", 4) == 0) {
21909 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021910 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021911 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021912 else {
Eric Biederman90089602004-05-28 14:11:54 +000021913 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021914 }
21915 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021916}
21917
Eric Biederman90089602004-05-28 14:11:54 +000021918static void arch_usage(FILE *fp)
21919{
21920 flag_usage(fp, arch_flags, "-m", "-mno-");
21921 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21922}
21923
Eric Biedermanb138ac82003-04-22 18:44:01 +000021924static unsigned arch_regc_size(struct compile_state *state, int class)
21925{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021926 if ((class < 0) || (class > LAST_REGC)) {
21927 return 0;
21928 }
21929 return regc_size[class];
21930}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021931
Eric Biedermanb138ac82003-04-22 18:44:01 +000021932static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21933{
21934 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021935 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21936 REGCM_GPR32_8 | REGCM_GPR32 |
21937 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021938
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021939 /* Special case for the immediates */
21940 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21941 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21942 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21943 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
21944 return 0;
21945 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021946 return (regcm1 & regcm2) ||
21947 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21948}
21949
21950static void arch_reg_equivs(
21951 struct compile_state *state, unsigned *equiv, int reg)
21952{
21953 if ((reg < 0) || (reg > LAST_REG)) {
21954 internal_error(state, 0, "invalid register");
21955 }
21956 *equiv++ = reg;
21957 switch(reg) {
21958 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021959#if X86_4_8BIT_GPRS
21960 *equiv++ = REG_AH;
21961#endif
21962 *equiv++ = REG_AX;
21963 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021964 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021965 *equiv++ = REG_EDXEAX;
21966 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021967 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021968#if X86_4_8BIT_GPRS
21969 *equiv++ = REG_AL;
21970#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021971 *equiv++ = REG_AX;
21972 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021973 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021974 *equiv++ = REG_EDXEAX;
21975 break;
21976 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021977#if X86_4_8BIT_GPRS
21978 *equiv++ = REG_BH;
21979#endif
21980 *equiv++ = REG_BX;
21981 *equiv++ = REG_EBX;
21982 break;
21983
Eric Biedermanb138ac82003-04-22 18:44:01 +000021984 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021985#if X86_4_8BIT_GPRS
21986 *equiv++ = REG_BL;
21987#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021988 *equiv++ = REG_BX;
21989 *equiv++ = REG_EBX;
21990 break;
21991 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021992#if X86_4_8BIT_GPRS
21993 *equiv++ = REG_CH;
21994#endif
21995 *equiv++ = REG_CX;
21996 *equiv++ = REG_ECX;
21997 break;
21998
Eric Biedermanb138ac82003-04-22 18:44:01 +000021999 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022000#if X86_4_8BIT_GPRS
22001 *equiv++ = REG_CL;
22002#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022003 *equiv++ = REG_CX;
22004 *equiv++ = REG_ECX;
22005 break;
22006 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022007#if X86_4_8BIT_GPRS
22008 *equiv++ = REG_DH;
22009#endif
22010 *equiv++ = REG_DX;
22011 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022012 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022013 *equiv++ = REG_EDXEAX;
22014 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022015 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022016#if X86_4_8BIT_GPRS
22017 *equiv++ = REG_DL;
22018#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022019 *equiv++ = REG_DX;
22020 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022021 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022022 *equiv++ = REG_EDXEAX;
22023 break;
22024 case REG_AX:
22025 *equiv++ = REG_AL;
22026 *equiv++ = REG_AH;
22027 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022028 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022029 *equiv++ = REG_EDXEAX;
22030 break;
22031 case REG_BX:
22032 *equiv++ = REG_BL;
22033 *equiv++ = REG_BH;
22034 *equiv++ = REG_EBX;
22035 break;
22036 case REG_CX:
22037 *equiv++ = REG_CL;
22038 *equiv++ = REG_CH;
22039 *equiv++ = REG_ECX;
22040 break;
22041 case REG_DX:
22042 *equiv++ = REG_DL;
22043 *equiv++ = REG_DH;
22044 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022045 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022046 *equiv++ = REG_EDXEAX;
22047 break;
22048 case REG_SI:
22049 *equiv++ = REG_ESI;
22050 break;
22051 case REG_DI:
22052 *equiv++ = REG_EDI;
22053 break;
22054 case REG_BP:
22055 *equiv++ = REG_EBP;
22056 break;
22057 case REG_SP:
22058 *equiv++ = REG_ESP;
22059 break;
22060 case REG_EAX:
22061 *equiv++ = REG_AL;
22062 *equiv++ = REG_AH;
22063 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022064 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022065 *equiv++ = REG_EDXEAX;
22066 break;
22067 case REG_EBX:
22068 *equiv++ = REG_BL;
22069 *equiv++ = REG_BH;
22070 *equiv++ = REG_BX;
22071 break;
22072 case REG_ECX:
22073 *equiv++ = REG_CL;
22074 *equiv++ = REG_CH;
22075 *equiv++ = REG_CX;
22076 break;
22077 case REG_EDX:
22078 *equiv++ = REG_DL;
22079 *equiv++ = REG_DH;
22080 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022081 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022082 *equiv++ = REG_EDXEAX;
22083 break;
22084 case REG_ESI:
22085 *equiv++ = REG_SI;
22086 break;
22087 case REG_EDI:
22088 *equiv++ = REG_DI;
22089 break;
22090 case REG_EBP:
22091 *equiv++ = REG_BP;
22092 break;
22093 case REG_ESP:
22094 *equiv++ = REG_SP;
22095 break;
Eric Biederman530b5192003-07-01 10:05:30 +000022096 case REG_DXAX:
22097 *equiv++ = REG_AL;
22098 *equiv++ = REG_AH;
22099 *equiv++ = REG_DL;
22100 *equiv++ = REG_DH;
22101 *equiv++ = REG_AX;
22102 *equiv++ = REG_DX;
22103 *equiv++ = REG_EAX;
22104 *equiv++ = REG_EDX;
22105 *equiv++ = REG_EDXEAX;
22106 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022107 case REG_EDXEAX:
22108 *equiv++ = REG_AL;
22109 *equiv++ = REG_AH;
22110 *equiv++ = REG_DL;
22111 *equiv++ = REG_DH;
22112 *equiv++ = REG_AX;
22113 *equiv++ = REG_DX;
22114 *equiv++ = REG_EAX;
22115 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022116 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022117 break;
22118 }
22119 *equiv++ = REG_UNSET;
22120}
22121
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022122static unsigned arch_avail_mask(struct compile_state *state)
22123{
22124 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022125 /* REGCM_GPR8 is not available */
22126 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22127 REGCM_GPR32 | REGCM_GPR32_8 |
22128 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022129 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022130 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022131 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022132 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022133 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022134 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022135 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022136 return avail_mask;
22137}
22138
22139static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22140{
22141 unsigned mask, result;
22142 int class, class2;
22143 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022144
22145 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22146 if ((result & mask) == 0) {
22147 continue;
22148 }
22149 if (class > LAST_REGC) {
22150 result &= ~mask;
22151 }
22152 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22153 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22154 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22155 result |= (1 << class2);
22156 }
22157 }
22158 }
Eric Biederman530b5192003-07-01 10:05:30 +000022159 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022160 return result;
22161}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022162
Eric Biedermand1ea5392003-06-28 06:49:45 +000022163static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22164{
22165 /* Like arch_regcm_normalize except immediate register classes are excluded */
22166 regcm = arch_regcm_normalize(state, regcm);
22167 /* Remove the immediate register classes */
22168 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22169 return regcm;
22170
22171}
22172
Eric Biedermanb138ac82003-04-22 18:44:01 +000022173static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22174{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022175 unsigned mask;
22176 int class;
22177 mask = 0;
22178 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022179 if ((reg >= regcm_bound[class].first) &&
22180 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022181 mask |= (1 << class);
22182 }
22183 }
22184 if (!mask) {
22185 internal_error(state, 0, "reg %d not in any class", reg);
22186 }
22187 return mask;
22188}
22189
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022190static struct reg_info arch_reg_constraint(
22191 struct compile_state *state, struct type *type, const char *constraint)
22192{
22193 static const struct {
22194 char class;
22195 unsigned int mask;
22196 unsigned int reg;
22197 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022198 { 'r', REGCM_GPR32, REG_UNSET },
22199 { 'g', REGCM_GPR32, REG_UNSET },
22200 { 'p', REGCM_GPR32, REG_UNSET },
22201 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022202 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022203 { 'x', REGCM_XMM, REG_UNSET },
22204 { 'y', REGCM_MMX, REG_UNSET },
22205 { 'a', REGCM_GPR32, REG_EAX },
22206 { 'b', REGCM_GPR32, REG_EBX },
22207 { 'c', REGCM_GPR32, REG_ECX },
22208 { 'd', REGCM_GPR32, REG_EDX },
22209 { 'D', REGCM_GPR32, REG_EDI },
22210 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022211 { '\0', 0, REG_UNSET },
22212 };
22213 unsigned int regcm;
22214 unsigned int mask, reg;
22215 struct reg_info result;
22216 const char *ptr;
22217 regcm = arch_type_to_regcm(state, type);
22218 reg = REG_UNSET;
22219 mask = 0;
22220 for(ptr = constraint; *ptr; ptr++) {
22221 int i;
22222 if (*ptr == ' ') {
22223 continue;
22224 }
22225 for(i = 0; constraints[i].class != '\0'; i++) {
22226 if (constraints[i].class == *ptr) {
22227 break;
22228 }
22229 }
22230 if (constraints[i].class == '\0') {
22231 error(state, 0, "invalid register constraint ``%c''", *ptr);
22232 break;
22233 }
22234 if ((constraints[i].mask & regcm) == 0) {
22235 error(state, 0, "invalid register class %c specified",
22236 *ptr);
22237 }
22238 mask |= constraints[i].mask;
22239 if (constraints[i].reg != REG_UNSET) {
22240 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22241 error(state, 0, "Only one register may be specified");
22242 }
22243 reg = constraints[i].reg;
22244 }
22245 }
22246 result.reg = reg;
22247 result.regcm = mask;
22248 return result;
22249}
22250
22251static struct reg_info arch_reg_clobber(
22252 struct compile_state *state, const char *clobber)
22253{
22254 struct reg_info result;
22255 if (strcmp(clobber, "memory") == 0) {
22256 result.reg = REG_UNSET;
22257 result.regcm = 0;
22258 }
Eric Biederman90089602004-05-28 14:11:54 +000022259 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022260 result.reg = REG_EAX;
22261 result.regcm = REGCM_GPR32;
22262 }
Eric Biederman90089602004-05-28 14:11:54 +000022263 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022264 result.reg = REG_EBX;
22265 result.regcm = REGCM_GPR32;
22266 }
Eric Biederman90089602004-05-28 14:11:54 +000022267 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022268 result.reg = REG_ECX;
22269 result.regcm = REGCM_GPR32;
22270 }
Eric Biederman90089602004-05-28 14:11:54 +000022271 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022272 result.reg = REG_EDX;
22273 result.regcm = REGCM_GPR32;
22274 }
Eric Biederman90089602004-05-28 14:11:54 +000022275 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022276 result.reg = REG_ESI;
22277 result.regcm = REGCM_GPR32;
22278 }
Eric Biederman90089602004-05-28 14:11:54 +000022279 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022280 result.reg = REG_EDI;
22281 result.regcm = REGCM_GPR32;
22282 }
Eric Biederman90089602004-05-28 14:11:54 +000022283 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022284 result.reg = REG_EBP;
22285 result.regcm = REGCM_GPR32;
22286 }
Eric Biederman90089602004-05-28 14:11:54 +000022287 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022288 result.reg = REG_ESP;
22289 result.regcm = REGCM_GPR32;
22290 }
22291 else if (strcmp(clobber, "cc") == 0) {
22292 result.reg = REG_EFLAGS;
22293 result.regcm = REGCM_FLAGS;
22294 }
22295 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22296 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22297 result.reg = REG_XMM0 + octdigval(clobber[3]);
22298 result.regcm = REGCM_XMM;
22299 }
Eric Biederman90089602004-05-28 14:11:54 +000022300 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022301 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22302 result.reg = REG_MMX0 + octdigval(clobber[3]);
22303 result.regcm = REGCM_MMX;
22304 }
22305 else {
Eric Biederman90089602004-05-28 14:11:54 +000022306 error(state, 0, "unknown register name `%s' in asm",
22307 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022308 result.reg = REG_UNSET;
22309 result.regcm = 0;
22310 }
22311 return result;
22312}
22313
Eric Biedermanb138ac82003-04-22 18:44:01 +000022314static int do_select_reg(struct compile_state *state,
22315 char *used, int reg, unsigned classes)
22316{
22317 unsigned mask;
22318 if (used[reg]) {
22319 return REG_UNSET;
22320 }
22321 mask = arch_reg_regcm(state, reg);
22322 return (classes & mask) ? reg : REG_UNSET;
22323}
22324
22325static int arch_select_free_register(
22326 struct compile_state *state, char *used, int classes)
22327{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022328 /* Live ranges with the most neighbors are colored first.
22329 *
22330 * Generally it does not matter which colors are given
22331 * as the register allocator attempts to color live ranges
22332 * in an order where you are guaranteed not to run out of colors.
22333 *
22334 * Occasionally the register allocator cannot find an order
22335 * of register selection that will find a free color. To
22336 * increase the odds the register allocator will work when
22337 * it guesses first give out registers from register classes
22338 * least likely to run out of registers.
22339 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022340 */
22341 int i, reg;
22342 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022343 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022344 reg = do_select_reg(state, used, i, classes);
22345 }
22346 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22347 reg = do_select_reg(state, used, i, classes);
22348 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022349 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022350 reg = do_select_reg(state, used, i, classes);
22351 }
22352 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22353 reg = do_select_reg(state, used, i, classes);
22354 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022355 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22356 reg = do_select_reg(state, used, i, classes);
22357 }
Eric Biederman530b5192003-07-01 10:05:30 +000022358 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22359 reg = do_select_reg(state, used, i, classes);
22360 }
22361 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22362 reg = do_select_reg(state, used, i, classes);
22363 }
22364 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022365 reg = do_select_reg(state, used, i, classes);
22366 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022367 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22368 reg = do_select_reg(state, used, i, classes);
22369 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022370 return reg;
22371}
22372
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022373
Eric Biedermanb138ac82003-04-22 18:44:01 +000022374static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
22375{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022376
22377#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022379#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022380 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022381 mask = 0;
22382 switch(type->type & TYPE_MASK) {
22383 case TYPE_ARRAY:
22384 case TYPE_VOID:
22385 mask = 0;
22386 break;
22387 case TYPE_CHAR:
22388 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022389 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022390 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022392 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022393 REGCM_MMX | REGCM_XMM |
22394 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022395 break;
22396 case TYPE_SHORT:
22397 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022398 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022399 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022400 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022401 REGCM_MMX | REGCM_XMM |
22402 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022403 break;
Eric Biederman90089602004-05-28 14:11:54 +000022404 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022405 case TYPE_INT:
22406 case TYPE_UINT:
22407 case TYPE_LONG:
22408 case TYPE_ULONG:
22409 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022410 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022411 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22412 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022413 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022414 break;
Eric Biederman90089602004-05-28 14:11:54 +000022415 case TYPE_JOIN:
22416 case TYPE_UNION:
22417 mask = arch_type_to_regcm(state, type->left);
22418 break;
22419 case TYPE_OVERLAP:
22420 mask = arch_type_to_regcm(state, type->left) &
22421 arch_type_to_regcm(state, type->right);
22422 break;
22423 case TYPE_BITFIELD:
22424 mask = arch_type_to_regcm(state, type->left);
22425 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022426 default:
Eric Biederman90089602004-05-28 14:11:54 +000022427 fprintf(state->errout, "type: ");
22428 name_of(state->errout, type);
22429 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022430 internal_error(state, 0, "no register class for type");
22431 break;
22432 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022433 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022434 return mask;
22435}
22436
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022437static int is_imm32(struct triple *imm)
22438{
22439 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
22440 (imm->op == OP_ADDRCONST);
22441
22442}
22443static int is_imm16(struct triple *imm)
22444{
22445 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22446}
22447static int is_imm8(struct triple *imm)
22448{
22449 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22450}
22451
22452static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022453{
22454 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022455 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022456 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022457 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022458 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022459 if (!is_imm32(imm)) {
22460 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022461 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022462 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022463 use_triple(imm, ins);
22464 *expr = imm;
22465 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022466}
22467
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022468static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022469{
22470 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022471 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022472 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022473 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022474 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022475 if (!is_imm8(imm)) {
22476 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022477 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022478 unuse_triple(*expr, ins);
22479 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022480 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022481 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022482}
22483
Eric Biederman530b5192003-07-01 10:05:30 +000022484#define TEMPLATE_NOP 0
22485#define TEMPLATE_INTCONST8 1
22486#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022487#define TEMPLATE_UNKNOWNVAL 3
22488#define TEMPLATE_COPY8_REG 5
22489#define TEMPLATE_COPY16_REG 6
22490#define TEMPLATE_COPY32_REG 7
22491#define TEMPLATE_COPY_IMM8 8
22492#define TEMPLATE_COPY_IMM16 9
22493#define TEMPLATE_COPY_IMM32 10
22494#define TEMPLATE_PHI8 11
22495#define TEMPLATE_PHI16 12
22496#define TEMPLATE_PHI32 13
22497#define TEMPLATE_STORE8 14
22498#define TEMPLATE_STORE16 15
22499#define TEMPLATE_STORE32 16
22500#define TEMPLATE_LOAD8 17
22501#define TEMPLATE_LOAD16 18
22502#define TEMPLATE_LOAD32 19
22503#define TEMPLATE_BINARY8_REG 20
22504#define TEMPLATE_BINARY16_REG 21
22505#define TEMPLATE_BINARY32_REG 22
22506#define TEMPLATE_BINARY8_IMM 23
22507#define TEMPLATE_BINARY16_IMM 24
22508#define TEMPLATE_BINARY32_IMM 25
22509#define TEMPLATE_SL8_CL 26
22510#define TEMPLATE_SL16_CL 27
22511#define TEMPLATE_SL32_CL 28
22512#define TEMPLATE_SL8_IMM 29
22513#define TEMPLATE_SL16_IMM 30
22514#define TEMPLATE_SL32_IMM 31
22515#define TEMPLATE_UNARY8 32
22516#define TEMPLATE_UNARY16 33
22517#define TEMPLATE_UNARY32 34
22518#define TEMPLATE_CMP8_REG 35
22519#define TEMPLATE_CMP16_REG 36
22520#define TEMPLATE_CMP32_REG 37
22521#define TEMPLATE_CMP8_IMM 38
22522#define TEMPLATE_CMP16_IMM 39
22523#define TEMPLATE_CMP32_IMM 40
22524#define TEMPLATE_TEST8 41
22525#define TEMPLATE_TEST16 42
22526#define TEMPLATE_TEST32 43
22527#define TEMPLATE_SET 44
22528#define TEMPLATE_JMP 45
22529#define TEMPLATE_RET 46
22530#define TEMPLATE_INB_DX 47
22531#define TEMPLATE_INB_IMM 48
22532#define TEMPLATE_INW_DX 49
22533#define TEMPLATE_INW_IMM 50
22534#define TEMPLATE_INL_DX 51
22535#define TEMPLATE_INL_IMM 52
22536#define TEMPLATE_OUTB_DX 53
22537#define TEMPLATE_OUTB_IMM 54
22538#define TEMPLATE_OUTW_DX 55
22539#define TEMPLATE_OUTW_IMM 56
22540#define TEMPLATE_OUTL_DX 57
22541#define TEMPLATE_OUTL_IMM 58
22542#define TEMPLATE_BSF 59
22543#define TEMPLATE_RDMSR 60
22544#define TEMPLATE_WRMSR 61
22545#define TEMPLATE_UMUL8 62
22546#define TEMPLATE_UMUL16 63
22547#define TEMPLATE_UMUL32 64
22548#define TEMPLATE_DIV8 65
22549#define TEMPLATE_DIV16 66
22550#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022551#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022552#if LAST_TEMPLATE >= MAX_TEMPLATES
22553#error "MAX_TEMPLATES to low"
22554#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022555
Eric Biederman530b5192003-07-01 10:05:30 +000022556#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22557#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
22558#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022559
Eric Biedermanb138ac82003-04-22 18:44:01 +000022560
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022561static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022562 [TEMPLATE_NOP] = {
22563 .lhs = {
22564 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22565 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22566 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22567 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22568 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22569 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22570 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22571 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22572 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22573 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22574 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22575 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22576 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22577 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22628 },
22629 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022630 [TEMPLATE_INTCONST8] = {
22631 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22632 },
22633 [TEMPLATE_INTCONST32] = {
22634 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22635 },
Eric Biederman90089602004-05-28 14:11:54 +000022636 [TEMPLATE_UNKNOWNVAL] = {
22637 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22638 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022639 [TEMPLATE_COPY8_REG] = {
22640 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22641 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022642 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022643 [TEMPLATE_COPY16_REG] = {
22644 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22645 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22646 },
22647 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022648 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022649 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022650 },
22651 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022652 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022653 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22654 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022655 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022656 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022657 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22658 },
22659 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022660 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022661 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22662 },
22663 [TEMPLATE_PHI8] = {
22664 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022665 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22666 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022667 [TEMPLATE_PHI16] = {
22668 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022669 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22670 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022671 [TEMPLATE_PHI32] = {
22672 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022673 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22674 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022675 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022676 .rhs = {
22677 [0] = { REG_UNSET, REGCM_GPR32 },
22678 [1] = { REG_UNSET, REGCM_GPR8_LO },
22679 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022680 },
22681 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022682 .rhs = {
22683 [0] = { REG_UNSET, REGCM_GPR32 },
22684 [1] = { REG_UNSET, REGCM_GPR16 },
22685 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022686 },
22687 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022688 .rhs = {
22689 [0] = { REG_UNSET, REGCM_GPR32 },
22690 [1] = { REG_UNSET, REGCM_GPR32 },
22691 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022692 },
22693 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022694 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022695 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22696 },
22697 [TEMPLATE_LOAD16] = {
22698 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22699 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22700 },
22701 [TEMPLATE_LOAD32] = {
22702 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22703 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22704 },
Eric Biederman530b5192003-07-01 10:05:30 +000022705 [TEMPLATE_BINARY8_REG] = {
22706 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22707 .rhs = {
22708 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22709 [1] = { REG_UNSET, REGCM_GPR8_LO },
22710 },
22711 },
22712 [TEMPLATE_BINARY16_REG] = {
22713 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22714 .rhs = {
22715 [0] = { REG_VIRT0, REGCM_GPR16 },
22716 [1] = { REG_UNSET, REGCM_GPR16 },
22717 },
22718 },
22719 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022720 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22721 .rhs = {
22722 [0] = { REG_VIRT0, REGCM_GPR32 },
22723 [1] = { REG_UNSET, REGCM_GPR32 },
22724 },
22725 },
Eric Biederman530b5192003-07-01 10:05:30 +000022726 [TEMPLATE_BINARY8_IMM] = {
22727 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22728 .rhs = {
22729 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22730 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22731 },
22732 },
22733 [TEMPLATE_BINARY16_IMM] = {
22734 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22735 .rhs = {
22736 [0] = { REG_VIRT0, REGCM_GPR16 },
22737 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22738 },
22739 },
22740 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022741 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22742 .rhs = {
22743 [0] = { REG_VIRT0, REGCM_GPR32 },
22744 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22745 },
22746 },
Eric Biederman530b5192003-07-01 10:05:30 +000022747 [TEMPLATE_SL8_CL] = {
22748 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22749 .rhs = {
22750 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22751 [1] = { REG_CL, REGCM_GPR8_LO },
22752 },
22753 },
22754 [TEMPLATE_SL16_CL] = {
22755 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22756 .rhs = {
22757 [0] = { REG_VIRT0, REGCM_GPR16 },
22758 [1] = { REG_CL, REGCM_GPR8_LO },
22759 },
22760 },
22761 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022762 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22763 .rhs = {
22764 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022765 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022766 },
22767 },
Eric Biederman530b5192003-07-01 10:05:30 +000022768 [TEMPLATE_SL8_IMM] = {
22769 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22770 .rhs = {
22771 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22772 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22773 },
22774 },
22775 [TEMPLATE_SL16_IMM] = {
22776 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22777 .rhs = {
22778 [0] = { REG_VIRT0, REGCM_GPR16 },
22779 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22780 },
22781 },
22782 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022783 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22784 .rhs = {
22785 [0] = { REG_VIRT0, REGCM_GPR32 },
22786 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22787 },
22788 },
Eric Biederman530b5192003-07-01 10:05:30 +000022789 [TEMPLATE_UNARY8] = {
22790 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22791 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22792 },
22793 [TEMPLATE_UNARY16] = {
22794 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22795 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22796 },
22797 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022798 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22799 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22800 },
Eric Biederman530b5192003-07-01 10:05:30 +000022801 [TEMPLATE_CMP8_REG] = {
22802 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22803 .rhs = {
22804 [0] = { REG_UNSET, REGCM_GPR8_LO },
22805 [1] = { REG_UNSET, REGCM_GPR8_LO },
22806 },
22807 },
22808 [TEMPLATE_CMP16_REG] = {
22809 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22810 .rhs = {
22811 [0] = { REG_UNSET, REGCM_GPR16 },
22812 [1] = { REG_UNSET, REGCM_GPR16 },
22813 },
22814 },
22815 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022816 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22817 .rhs = {
22818 [0] = { REG_UNSET, REGCM_GPR32 },
22819 [1] = { REG_UNSET, REGCM_GPR32 },
22820 },
22821 },
Eric Biederman530b5192003-07-01 10:05:30 +000022822 [TEMPLATE_CMP8_IMM] = {
22823 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22824 .rhs = {
22825 [0] = { REG_UNSET, REGCM_GPR8_LO },
22826 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22827 },
22828 },
22829 [TEMPLATE_CMP16_IMM] = {
22830 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22831 .rhs = {
22832 [0] = { REG_UNSET, REGCM_GPR16 },
22833 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22834 },
22835 },
22836 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022837 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22838 .rhs = {
22839 [0] = { REG_UNSET, REGCM_GPR32 },
22840 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22841 },
22842 },
Eric Biederman530b5192003-07-01 10:05:30 +000022843 [TEMPLATE_TEST8] = {
22844 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22845 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22846 },
22847 [TEMPLATE_TEST16] = {
22848 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22849 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22850 },
22851 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022852 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22853 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22854 },
22855 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022856 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022857 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22858 },
22859 [TEMPLATE_JMP] = {
22860 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22861 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022862 [TEMPLATE_RET] = {
22863 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22864 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022865 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022866 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022867 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22868 },
22869 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022870 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022871 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22872 },
22873 [TEMPLATE_INW_DX] = {
22874 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22875 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22876 },
22877 [TEMPLATE_INW_IMM] = {
22878 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22879 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22880 },
22881 [TEMPLATE_INL_DX] = {
22882 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22883 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22884 },
22885 [TEMPLATE_INL_IMM] = {
22886 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22887 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22888 },
22889 [TEMPLATE_OUTB_DX] = {
22890 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022891 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022892 [1] = { REG_DX, REGCM_GPR16 },
22893 },
22894 },
22895 [TEMPLATE_OUTB_IMM] = {
22896 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022897 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022898 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22899 },
22900 },
22901 [TEMPLATE_OUTW_DX] = {
22902 .rhs = {
22903 [0] = { REG_AX, REGCM_GPR16 },
22904 [1] = { REG_DX, REGCM_GPR16 },
22905 },
22906 },
22907 [TEMPLATE_OUTW_IMM] = {
22908 .rhs = {
22909 [0] = { REG_AX, REGCM_GPR16 },
22910 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22911 },
22912 },
22913 [TEMPLATE_OUTL_DX] = {
22914 .rhs = {
22915 [0] = { REG_EAX, REGCM_GPR32 },
22916 [1] = { REG_DX, REGCM_GPR16 },
22917 },
22918 },
22919 [TEMPLATE_OUTL_IMM] = {
22920 .rhs = {
22921 [0] = { REG_EAX, REGCM_GPR32 },
22922 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22923 },
22924 },
22925 [TEMPLATE_BSF] = {
22926 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22927 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22928 },
22929 [TEMPLATE_RDMSR] = {
22930 .lhs = {
22931 [0] = { REG_EAX, REGCM_GPR32 },
22932 [1] = { REG_EDX, REGCM_GPR32 },
22933 },
22934 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22935 },
22936 [TEMPLATE_WRMSR] = {
22937 .rhs = {
22938 [0] = { REG_ECX, REGCM_GPR32 },
22939 [1] = { REG_EAX, REGCM_GPR32 },
22940 [2] = { REG_EDX, REGCM_GPR32 },
22941 },
22942 },
Eric Biederman530b5192003-07-01 10:05:30 +000022943 [TEMPLATE_UMUL8] = {
22944 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22945 .rhs = {
22946 [0] = { REG_AL, REGCM_GPR8_LO },
22947 [1] = { REG_UNSET, REGCM_GPR8_LO },
22948 },
22949 },
22950 [TEMPLATE_UMUL16] = {
22951 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22952 .rhs = {
22953 [0] = { REG_AX, REGCM_GPR16 },
22954 [1] = { REG_UNSET, REGCM_GPR16 },
22955 },
22956 },
22957 [TEMPLATE_UMUL32] = {
22958 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022959 .rhs = {
22960 [0] = { REG_EAX, REGCM_GPR32 },
22961 [1] = { REG_UNSET, REGCM_GPR32 },
22962 },
22963 },
Eric Biederman530b5192003-07-01 10:05:30 +000022964 [TEMPLATE_DIV8] = {
22965 .lhs = {
22966 [0] = { REG_AL, REGCM_GPR8_LO },
22967 [1] = { REG_AH, REGCM_GPR8 },
22968 },
22969 .rhs = {
22970 [0] = { REG_AX, REGCM_GPR16 },
22971 [1] = { REG_UNSET, REGCM_GPR8_LO },
22972 },
22973 },
22974 [TEMPLATE_DIV16] = {
22975 .lhs = {
22976 [0] = { REG_AX, REGCM_GPR16 },
22977 [1] = { REG_DX, REGCM_GPR16 },
22978 },
22979 .rhs = {
22980 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22981 [1] = { REG_UNSET, REGCM_GPR16 },
22982 },
22983 },
22984 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022985 .lhs = {
22986 [0] = { REG_EAX, REGCM_GPR32 },
22987 [1] = { REG_EDX, REGCM_GPR32 },
22988 },
22989 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022990 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022991 [1] = { REG_UNSET, REGCM_GPR32 },
22992 },
22993 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022994};
Eric Biedermanb138ac82003-04-22 18:44:01 +000022995
Eric Biederman83b991a2003-10-11 06:20:25 +000022996static void fixup_branch(struct compile_state *state,
22997 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
22998 struct triple *left, struct triple *right)
22999{
23000 struct triple *test;
23001 if (!left) {
23002 internal_error(state, branch, "no branch test?");
23003 }
23004 test = pre_triple(state, branch,
23005 cmp_op, cmp_type, left, right);
23006 test->template_id = TEMPLATE_TEST32;
23007 if (cmp_op == OP_CMP) {
23008 test->template_id = TEMPLATE_CMP32_REG;
23009 if (get_imm32(test, &RHS(test, 1))) {
23010 test->template_id = TEMPLATE_CMP32_IMM;
23011 }
23012 }
23013 use_triple(RHS(test, 0), test);
23014 use_triple(RHS(test, 1), test);
23015 unuse_triple(RHS(branch, 0), branch);
23016 RHS(branch, 0) = test;
23017 branch->op = jmp_op;
23018 branch->template_id = TEMPLATE_JMP;
23019 use_triple(RHS(branch, 0), branch);
23020}
23021
Eric Biedermanb138ac82003-04-22 18:44:01 +000023022static void fixup_branches(struct compile_state *state,
23023 struct triple *cmp, struct triple *use, int jmp_op)
23024{
23025 struct triple_set *entry, *next;
23026 for(entry = use->use; entry; entry = next) {
23027 next = entry->next;
23028 if (entry->member->op == OP_COPY) {
23029 fixup_branches(state, cmp, entry->member, jmp_op);
23030 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023031 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023032 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023033 struct triple *left, *right;
23034 left = right = 0;
23035 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023036 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023037 right = RHS(cmp, 1);
23038 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023039 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000023040 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023041 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023042 }
23043 }
23044}
23045
23046static void bool_cmp(struct compile_state *state,
23047 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23048{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023049 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023050 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023051
23052 /* Put a barrier up before the cmp which preceeds the
23053 * copy instruction. If a set actually occurs this gives
23054 * us a chance to move variables in registers out of the way.
23055 */
23056
23057 /* Modify the comparison operator */
23058 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023059 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023060 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023061 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023062 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023063 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023064 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023065 }
23066 /* Generate the instruction sequence that will transform the
23067 * result of the comparison into a logical value.
23068 */
Eric Biederman90089602004-05-28 14:11:54 +000023069 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023070 use_triple(ins, set);
23071 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023072
Eric Biederman90089602004-05-28 14:11:54 +000023073 convert = set;
23074 if (!equiv_types(ins->type, set->type)) {
23075 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23076 use_triple(set, convert);
23077 convert->template_id = TEMPLATE_COPY32_REG;
23078 }
23079
Eric Biedermanb138ac82003-04-22 18:44:01 +000023080 for(entry = ins->use; entry; entry = next) {
23081 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023082 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023083 continue;
23084 }
Eric Biederman90089602004-05-28 14:11:54 +000023085 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023086 }
Eric Biederman90089602004-05-28 14:11:54 +000023087 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023088}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023089
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023090struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23091{
23092 struct ins_template *template;
23093 struct reg_info result;
23094 int zlhs;
23095 if (ins->op == OP_PIECE) {
23096 index = ins->u.cval;
23097 ins = MISC(ins, 0);
23098 }
Eric Biederman90089602004-05-28 14:11:54 +000023099 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023100 if (triple_is_def(state, ins)) {
23101 zlhs = 1;
23102 }
23103 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023104 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023105 index, tops(ins->op));
23106 }
23107 switch(ins->op) {
23108 case OP_ASM:
23109 template = &ins->u.ainfo->tmpl;
23110 break;
23111 default:
23112 if (ins->template_id > LAST_TEMPLATE) {
23113 internal_error(state, ins, "bad template number %d",
23114 ins->template_id);
23115 }
23116 template = &templates[ins->template_id];
23117 break;
23118 }
23119 result = template->lhs[index];
23120 result.regcm = arch_regcm_normalize(state, result.regcm);
23121 if (result.reg != REG_UNNEEDED) {
23122 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23123 }
23124 if (result.regcm == 0) {
23125 internal_error(state, ins, "lhs %d regcm == 0", index);
23126 }
23127 return result;
23128}
23129
23130struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23131{
23132 struct reg_info result;
23133 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023134 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023135 (ins->op == OP_PIECE)) {
23136 internal_error(state, ins, "index %d out of range for %s\n",
23137 index, tops(ins->op));
23138 }
23139 switch(ins->op) {
23140 case OP_ASM:
23141 template = &ins->u.ainfo->tmpl;
23142 break;
Eric Biederman90089602004-05-28 14:11:54 +000023143 case OP_PHI:
23144 index = 0;
23145 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023146 default:
23147 if (ins->template_id > LAST_TEMPLATE) {
23148 internal_error(state, ins, "bad template number %d",
23149 ins->template_id);
23150 }
23151 template = &templates[ins->template_id];
23152 break;
23153 }
23154 result = template->rhs[index];
23155 result.regcm = arch_regcm_normalize(state, result.regcm);
23156 if (result.regcm == 0) {
23157 internal_error(state, ins, "rhs %d regcm == 0", index);
23158 }
23159 return result;
23160}
23161
Eric Biederman530b5192003-07-01 10:05:30 +000023162static struct triple *mod_div(struct compile_state *state,
23163 struct triple *ins, int div_op, int index)
23164{
23165 struct triple *div, *piece0, *piece1;
23166
Eric Biederman530b5192003-07-01 10:05:30 +000023167 /* Generate the appropriate division instruction */
23168 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23169 RHS(div, 0) = RHS(ins, 0);
23170 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023171 piece0 = LHS(div, 0);
23172 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023173 div->template_id = TEMPLATE_DIV32;
23174 use_triple(RHS(div, 0), div);
23175 use_triple(RHS(div, 1), div);
23176 use_triple(LHS(div, 0), div);
23177 use_triple(LHS(div, 1), div);
23178
Eric Biederman530b5192003-07-01 10:05:30 +000023179 /* Replate uses of ins with the appropriate piece of the div */
23180 propogate_use(state, ins, LHS(div, index));
23181 release_triple(state, ins);
23182
23183 /* Return the address of the next instruction */
23184 return piece1->next;
23185}
23186
Eric Biederman90089602004-05-28 14:11:54 +000023187static int noop_adecl(struct triple *adecl)
23188{
23189 struct triple_set *use;
23190 /* It's a noop if it doesn't specify stoorage */
23191 if (adecl->lhs == 0) {
23192 return 1;
23193 }
23194 /* Is the adecl used? If not it's a noop */
23195 for(use = adecl->use; use ; use = use->next) {
23196 if ((use->member->op != OP_PIECE) ||
23197 (MISC(use->member, 0) != adecl)) {
23198 return 0;
23199 }
23200 }
23201 return 1;
23202}
23203
23204static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23205{
23206 struct triple *mask, *nmask, *shift;
23207 struct triple *val, *val_mask, *val_shift;
23208 struct triple *targ, *targ_mask;
23209 struct triple *new;
23210 ulong_t the_mask, the_nmask;
23211
23212 targ = RHS(ins, 0);
23213 val = RHS(ins, 1);
23214
23215 /* Get constant for the mask value */
23216 the_mask = 1;
23217 the_mask <<= ins->u.bitfield.size;
23218 the_mask -= 1;
23219 the_mask <<= ins->u.bitfield.offset;
23220 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23221 mask->u.cval = the_mask;
23222
23223 /* Get the inverted mask value */
23224 the_nmask = ~the_mask;
23225 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23226 nmask->u.cval = the_nmask;
23227
23228 /* Get constant for the shift value */
23229 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23230 shift->u.cval = ins->u.bitfield.offset;
23231
23232 /* Shift and mask the source value */
23233 val_shift = val;
23234 if (shift->u.cval != 0) {
23235 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23236 use_triple(val, val_shift);
23237 use_triple(shift, val_shift);
23238 }
23239 val_mask = val_shift;
23240 if (is_signed(val->type)) {
23241 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23242 use_triple(val_shift, val_mask);
23243 use_triple(mask, val_mask);
23244 }
23245
23246 /* Mask the target value */
23247 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23248 use_triple(targ, targ_mask);
23249 use_triple(nmask, targ_mask);
23250
23251 /* Now combined them together */
23252 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23253 use_triple(targ_mask, new);
23254 use_triple(val_mask, new);
23255
23256 /* Move all of the users over to the new expression */
23257 propogate_use(state, ins, new);
23258
23259 /* Delete the original triple */
23260 release_triple(state, ins);
23261
23262 /* Restart the transformation at mask */
23263 return mask;
23264}
23265
23266static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23267{
23268 struct triple *mask, *shift;
23269 struct triple *val, *val_mask, *val_shift;
23270 ulong_t the_mask;
23271
23272 val = RHS(ins, 0);
23273
23274 /* Get constant for the mask value */
23275 the_mask = 1;
23276 the_mask <<= ins->u.bitfield.size;
23277 the_mask -= 1;
23278 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23279 mask->u.cval = the_mask;
23280
23281 /* Get constant for the right shift value */
23282 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23283 shift->u.cval = ins->u.bitfield.offset;
23284
23285 /* Shift arithmetic right, to correct the sign */
23286 val_shift = val;
23287 if (shift->u.cval != 0) {
23288 int op;
23289 if (ins->op == OP_SEXTRACT) {
23290 op = OP_SSR;
23291 } else {
23292 op = OP_USR;
23293 }
23294 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23295 use_triple(val, val_shift);
23296 use_triple(shift, val_shift);
23297 }
23298
23299 /* Finally mask the value */
23300 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23301 use_triple(val_shift, val_mask);
23302 use_triple(mask, val_mask);
23303
23304 /* Move all of the users over to the new expression */
23305 propogate_use(state, ins, val_mask);
23306
23307 /* Release the original instruction */
23308 release_triple(state, ins);
23309
23310 return mask;
23311
23312}
23313
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023314static struct triple *transform_to_arch_instruction(
23315 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023316{
23317 /* Transform from generic 3 address instructions
23318 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023319 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023320 * Copies are inserted to preserve the register flexibility
23321 * of 3 address instructions.
23322 */
Eric Biederman90089602004-05-28 14:11:54 +000023323 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023324 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023325 next = ins->next;
23326 switch(ins->op) {
23327 case OP_INTCONST:
23328 ins->template_id = TEMPLATE_INTCONST32;
23329 if (ins->u.cval < 256) {
23330 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023331 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023332 break;
23333 case OP_ADDRCONST:
23334 ins->template_id = TEMPLATE_INTCONST32;
23335 break;
Eric Biederman90089602004-05-28 14:11:54 +000023336 case OP_UNKNOWNVAL:
23337 ins->template_id = TEMPLATE_UNKNOWNVAL;
23338 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023339 case OP_NOOP:
23340 case OP_SDECL:
23341 case OP_BLOBCONST:
23342 case OP_LABEL:
23343 ins->template_id = TEMPLATE_NOP;
23344 break;
23345 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023346 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023347 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023348 value = RHS(ins, 0);
23349 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023350 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023351 }
Eric Biederman90089602004-05-28 14:11:54 +000023352 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023353 ins->template_id = TEMPLATE_COPY_IMM16;
23354 }
Eric Biederman90089602004-05-28 14:11:54 +000023355 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023356 ins->template_id = TEMPLATE_COPY_IMM32;
23357 }
Eric Biederman90089602004-05-28 14:11:54 +000023358 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023359 internal_error(state, ins, "bad constant passed to copy");
23360 }
Eric Biederman90089602004-05-28 14:11:54 +000023361 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023362 ins->template_id = TEMPLATE_COPY8_REG;
23363 }
Eric Biederman90089602004-05-28 14:11:54 +000023364 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023365 ins->template_id = TEMPLATE_COPY16_REG;
23366 }
Eric Biederman90089602004-05-28 14:11:54 +000023367 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023368 ins->template_id = TEMPLATE_COPY32_REG;
23369 }
23370 else {
23371 internal_error(state, ins, "bad type passed to copy");
23372 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023373 break;
23374 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023375 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023376 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023377 ins->template_id = TEMPLATE_PHI8;
23378 }
Eric Biederman90089602004-05-28 14:11:54 +000023379 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023380 ins->template_id = TEMPLATE_PHI16;
23381 }
Eric Biederman90089602004-05-28 14:11:54 +000023382 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023383 ins->template_id = TEMPLATE_PHI32;
23384 }
23385 else {
23386 internal_error(state, ins, "bad type passed to phi");
23387 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023388 break;
Eric Biederman90089602004-05-28 14:11:54 +000023389 case OP_ADECL:
23390 /* Adecls should always be treated as dead code and
23391 * removed. If we are not optimizing they may linger.
23392 */
23393 if (!noop_adecl(ins)) {
23394 internal_error(state, ins, "adecl remains?");
23395 }
23396 ins->template_id = TEMPLATE_NOP;
23397 next = after_lhs(state, ins);
23398 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023399 case OP_STORE:
23400 switch(ins->type->type & TYPE_MASK) {
23401 case TYPE_CHAR: case TYPE_UCHAR:
23402 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023403 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023404 case TYPE_SHORT: case TYPE_USHORT:
23405 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023406 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023407 case TYPE_INT: case TYPE_UINT:
23408 case TYPE_LONG: case TYPE_ULONG:
23409 case TYPE_POINTER:
23410 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023411 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023412 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023413 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023414 break;
23415 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023416 break;
23417 case OP_LOAD:
23418 switch(ins->type->type & TYPE_MASK) {
23419 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023420 case TYPE_SHORT: case TYPE_USHORT:
23421 case TYPE_INT: case TYPE_UINT:
23422 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023423 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023424 break;
23425 default:
23426 internal_error(state, ins, "unknown type in load");
23427 break;
23428 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023429 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023430 break;
23431 case OP_ADD:
23432 case OP_SUB:
23433 case OP_AND:
23434 case OP_XOR:
23435 case OP_OR:
23436 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023437 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023438 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023439 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023440 }
23441 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023442 case OP_SDIVT:
23443 case OP_UDIVT:
23444 ins->template_id = TEMPLATE_DIV32;
23445 next = after_lhs(state, ins);
23446 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023447 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023448 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023449 break;
23450 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023451 next = mod_div(state, ins, OP_UDIVT, 0);
23452 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023453 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023454 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023455 break;
23456 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023457 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023458 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023459 case OP_SMOD:
23460 next = mod_div(state, ins, OP_SDIVT, 1);
23461 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023462 case OP_SL:
23463 case OP_SSR:
23464 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023465 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023466 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023467 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023468 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23469 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023470 }
23471 break;
23472 case OP_INVERT:
23473 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023474 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023475 break;
23476 case OP_EQ:
23477 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
23478 break;
23479 case OP_NOTEQ:
23480 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23481 break;
23482 case OP_SLESS:
23483 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23484 break;
23485 case OP_ULESS:
23486 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23487 break;
23488 case OP_SMORE:
23489 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23490 break;
23491 case OP_UMORE:
23492 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23493 break;
23494 case OP_SLESSEQ:
23495 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23496 break;
23497 case OP_ULESSEQ:
23498 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23499 break;
23500 case OP_SMOREEQ:
23501 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23502 break;
23503 case OP_UMOREEQ:
23504 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23505 break;
23506 case OP_LTRUE:
23507 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23508 break;
23509 case OP_LFALSE:
23510 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23511 break;
23512 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023513 ins->op = OP_JMP;
23514 ins->template_id = TEMPLATE_NOP;
23515 break;
23516 case OP_CBRANCH:
23517 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
23518 RHS(ins, 0)->type, RHS(ins, 0), 0);
23519 break;
23520 case OP_CALL:
23521 ins->template_id = TEMPLATE_NOP;
23522 break;
23523 case OP_RET:
23524 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023525 break;
23526 case OP_INB:
23527 case OP_INW:
23528 case OP_INL:
23529 switch(ins->op) {
23530 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23531 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23532 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23533 }
23534 if (get_imm8(ins, &RHS(ins, 0))) {
23535 ins->template_id += 1;
23536 }
23537 break;
23538 case OP_OUTB:
23539 case OP_OUTW:
23540 case OP_OUTL:
23541 switch(ins->op) {
23542 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23543 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23544 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23545 }
23546 if (get_imm8(ins, &RHS(ins, 1))) {
23547 ins->template_id += 1;
23548 }
23549 break;
23550 case OP_BSF:
23551 case OP_BSR:
23552 ins->template_id = TEMPLATE_BSF;
23553 break;
23554 case OP_RDMSR:
23555 ins->template_id = TEMPLATE_RDMSR;
23556 next = after_lhs(state, ins);
23557 break;
23558 case OP_WRMSR:
23559 ins->template_id = TEMPLATE_WRMSR;
23560 break;
23561 case OP_HLT:
23562 ins->template_id = TEMPLATE_NOP;
23563 break;
23564 case OP_ASM:
23565 ins->template_id = TEMPLATE_NOP;
23566 next = after_lhs(state, ins);
23567 break;
23568 /* Already transformed instructions */
23569 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023570 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023571 break;
23572 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023573 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023574 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023575 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023576 }
23577 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023578 case OP_JMP:
23579 ins->template_id = TEMPLATE_NOP;
23580 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023581 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23582 case OP_JMP_SLESS: case OP_JMP_ULESS:
23583 case OP_JMP_SMORE: case OP_JMP_UMORE:
23584 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23585 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23586 ins->template_id = TEMPLATE_JMP;
23587 break;
23588 case OP_SET_EQ: case OP_SET_NOTEQ:
23589 case OP_SET_SLESS: case OP_SET_ULESS:
23590 case OP_SET_SMORE: case OP_SET_UMORE:
23591 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23592 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23593 ins->template_id = TEMPLATE_SET;
23594 break;
Eric Biederman90089602004-05-28 14:11:54 +000023595 case OP_DEPOSIT:
23596 next = x86_deposit(state, ins);
23597 break;
23598 case OP_SEXTRACT:
23599 case OP_UEXTRACT:
23600 next = x86_extract(state, ins);
23601 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023602 /* Unhandled instructions */
23603 case OP_PIECE:
23604 default:
Eric Biederman90089602004-05-28 14:11:54 +000023605 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023606 ins->op, tops(ins->op));
23607 break;
23608 }
23609 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023610}
23611
Eric Biederman530b5192003-07-01 10:05:30 +000023612static long next_label(struct compile_state *state)
23613{
Eric Biederman90089602004-05-28 14:11:54 +000023614 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023615 return ++label_counter;
23616}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023617static void generate_local_labels(struct compile_state *state)
23618{
23619 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023620 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023621 label = first;
23622 do {
23623 if ((label->op == OP_LABEL) ||
23624 (label->op == OP_SDECL)) {
23625 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023626 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023627 } else {
23628 label->u.cval = 0;
23629 }
23630
23631 }
23632 label = label->next;
23633 } while(label != first);
23634}
23635
23636static int check_reg(struct compile_state *state,
23637 struct triple *triple, int classes)
23638{
23639 unsigned mask;
23640 int reg;
23641 reg = ID_REG(triple->id);
23642 if (reg == REG_UNSET) {
23643 internal_error(state, triple, "register not set");
23644 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023645 mask = arch_reg_regcm(state, reg);
23646 if (!(classes & mask)) {
23647 internal_error(state, triple, "reg %d in wrong class",
23648 reg);
23649 }
23650 return reg;
23651}
23652
Eric Biederman90089602004-05-28 14:11:54 +000023653
Eric Biederman530b5192003-07-01 10:05:30 +000023654#if REG_XMM7 != 44
23655#error "Registers have renumberd fix arch_reg_str"
23656#endif
Eric Biederman90089602004-05-28 14:11:54 +000023657static const char *arch_regs[] = {
23658 "%unset",
23659 "%unneeded",
23660 "%eflags",
23661 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23662 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23663 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23664 "%edx:%eax",
23665 "%dx:%ax",
23666 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23667 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
23668 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23669};
23670static const char *arch_reg_str(int reg)
23671{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023672 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23673 reg = 0;
23674 }
Eric Biederman90089602004-05-28 14:11:54 +000023675 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023676}
23677
23678static const char *reg(struct compile_state *state, struct triple *triple,
23679 int classes)
23680{
23681 int reg;
23682 reg = check_reg(state, triple, classes);
23683 return arch_reg_str(reg);
23684}
23685
Eric Biederman90089602004-05-28 14:11:54 +000023686static int arch_reg_size(int reg)
23687{
23688 int size;
23689 size = 0;
23690 if (reg == REG_EFLAGS) {
23691 size = 32;
23692 }
23693 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23694 size = 8;
23695 }
23696 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23697 size = 16;
23698 }
23699 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23700 size = 32;
23701 }
23702 else if (reg == REG_EDXEAX) {
23703 size = 64;
23704 }
23705 else if (reg == REG_DXAX) {
23706 size = 32;
23707 }
23708 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23709 size = 64;
23710 }
23711 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23712 size = 128;
23713 }
23714 return size;
23715}
23716
23717static int reg_size(struct compile_state *state, struct triple *ins)
23718{
23719 int reg;
23720 reg = ID_REG(ins->id);
23721 if (reg == REG_UNSET) {
23722 internal_error(state, ins, "register not set");
23723 }
23724 return arch_reg_size(reg);
23725}
23726
23727
23728
Eric Biedermanb138ac82003-04-22 18:44:01 +000023729const char *type_suffix(struct compile_state *state, struct type *type)
23730{
23731 const char *suffix;
23732 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023733 case SIZEOF_I8: suffix = "b"; break;
23734 case SIZEOF_I16: suffix = "w"; break;
23735 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023736 default:
23737 internal_error(state, 0, "unknown suffix");
23738 suffix = 0;
23739 break;
23740 }
23741 return suffix;
23742}
23743
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023744static void print_const_val(
23745 struct compile_state *state, struct triple *ins, FILE *fp)
23746{
23747 switch(ins->op) {
23748 case OP_INTCONST:
23749 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023750 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023751 break;
23752 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023753 if ((MISC(ins, 0)->op != OP_SDECL) &&
23754 (MISC(ins, 0)->op != OP_LABEL))
23755 {
Eric Biederman830c9882003-07-04 00:27:33 +000023756 internal_error(state, ins, "bad base for addrconst");
23757 }
23758 if (MISC(ins, 0)->u.cval <= 0) {
23759 internal_error(state, ins, "unlabeled constant");
23760 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023761 fprintf(fp, " $L%s%lu+%lu ",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023762 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023763 (unsigned long)(MISC(ins, 0)->u.cval),
23764 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023765 break;
23766 default:
23767 internal_error(state, ins, "unknown constant type");
23768 break;
23769 }
23770}
23771
Eric Biederman530b5192003-07-01 10:05:30 +000023772static void print_const(struct compile_state *state,
23773 struct triple *ins, FILE *fp)
23774{
23775 switch(ins->op) {
23776 case OP_INTCONST:
23777 switch(ins->type->type & TYPE_MASK) {
23778 case TYPE_CHAR:
23779 case TYPE_UCHAR:
Eric Biederman83b991a2003-10-11 06:20:25 +000023780 fprintf(fp, ".byte 0x%02lx\n",
23781 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023782 break;
23783 case TYPE_SHORT:
23784 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000023785 fprintf(fp, ".short 0x%04lx\n",
23786 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023787 break;
23788 case TYPE_INT:
23789 case TYPE_UINT:
23790 case TYPE_LONG:
23791 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023792 case TYPE_POINTER:
Eric Biederman83b991a2003-10-11 06:20:25 +000023793 fprintf(fp, ".int %lu\n",
23794 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023795 break;
23796 default:
Eric Biederman90089602004-05-28 14:11:54 +000023797 fprintf(state->errout, "type: ");
23798 name_of(state->errout, ins->type);
23799 fprintf(state->errout, "\n");
23800 internal_error(state, ins, "Unknown constant type. Val: %lu",
23801 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023802 }
Eric Biederman90089602004-05-28 14:11:54 +000023803
Eric Biederman530b5192003-07-01 10:05:30 +000023804 break;
23805 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023806 if ((MISC(ins, 0)->op != OP_SDECL) &&
23807 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023808 internal_error(state, ins, "bad base for addrconst");
23809 }
23810 if (MISC(ins, 0)->u.cval <= 0) {
23811 internal_error(state, ins, "unlabeled constant");
23812 }
23813 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023814 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023815 (unsigned long)(MISC(ins, 0)->u.cval),
23816 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023817 break;
23818 case OP_BLOBCONST:
23819 {
23820 unsigned char *blob;
23821 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023822 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023823 blob = ins->u.blob;
23824 for(i = 0; i < size; i++) {
23825 fprintf(fp, ".byte 0x%02x\n",
23826 blob[i]);
23827 }
23828 break;
23829 }
23830 default:
23831 internal_error(state, ins, "Unknown constant type");
23832 break;
23833 }
23834}
23835
23836#define TEXT_SECTION ".rom.text"
23837#define DATA_SECTION ".rom.data"
23838
23839static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023840 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023841{
Eric Biederman90089602004-05-28 14:11:54 +000023842 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023843 long ref;
23844 ref = next_label(state);
23845 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Stefan Reinauerbe84b012009-04-04 13:20:33 +000023846 fprintf(fp, ".balign %ld\n", align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023847 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023848 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023849 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23850 if (fill_bytes) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000023851 fprintf(fp, ".fill %ld, 1, 0\n", fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023852 }
Eric Biederman530b5192003-07-01 10:05:30 +000023853 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23854 return ref;
23855}
23856
Eric Biederman90089602004-05-28 14:11:54 +000023857static long get_mask_pool_ref(
23858 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23859{
23860 long ref;
23861 if (mask == 0xff) {
23862 ref = 1;
23863 }
23864 else if (mask == 0xffff) {
23865 ref = 2;
23866 }
23867 else {
23868 ref = 0;
23869 internal_error(state, ins, "unhandled mask value");
23870 }
23871 return ref;
23872}
23873
Eric Biedermanb138ac82003-04-22 18:44:01 +000023874static void print_binary_op(struct compile_state *state,
23875 const char *op, struct triple *ins, FILE *fp)
23876{
23877 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023878 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023879 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023880 internal_error(state, ins, "invalid register assignment");
23881 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023882 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023883 fprintf(fp, "\t%s ", op);
23884 print_const_val(state, RHS(ins, 1), fp);
23885 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023886 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023887 }
23888 else {
23889 unsigned lmask, rmask;
23890 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023891 lreg = check_reg(state, RHS(ins, 0), mask);
23892 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023893 lmask = arch_reg_regcm(state, lreg);
23894 rmask = arch_reg_regcm(state, rreg);
23895 mask = lmask & rmask;
23896 fprintf(fp, "\t%s %s, %s\n",
23897 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023898 reg(state, RHS(ins, 1), mask),
23899 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023900 }
23901}
23902static void print_unary_op(struct compile_state *state,
23903 const char *op, struct triple *ins, FILE *fp)
23904{
23905 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023906 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023907 fprintf(fp, "\t%s %s\n",
23908 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023909 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023910}
23911
23912static void print_op_shift(struct compile_state *state,
23913 const char *op, struct triple *ins, FILE *fp)
23914{
23915 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023916 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023917 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023918 internal_error(state, ins, "invalid register assignment");
23919 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023920 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023921 fprintf(fp, "\t%s ", op);
23922 print_const_val(state, RHS(ins, 1), fp);
23923 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023924 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023925 }
23926 else {
23927 fprintf(fp, "\t%s %s, %s\n",
23928 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023929 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023930 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023931 }
23932}
23933
23934static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23935{
23936 const char *op;
23937 int mask;
23938 int dreg;
23939 mask = 0;
23940 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023941 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023942 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23943 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23944 default:
23945 internal_error(state, ins, "not an in operation");
23946 op = 0;
23947 break;
23948 }
23949 dreg = check_reg(state, ins, mask);
23950 if (!reg_is_reg(state, dreg, REG_EAX)) {
23951 internal_error(state, ins, "dst != %%eax");
23952 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023953 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023954 fprintf(fp, "\t%s ", op);
23955 print_const_val(state, RHS(ins, 0), fp);
23956 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023957 reg(state, ins, mask));
23958 }
23959 else {
23960 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023961 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023962 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23963 internal_error(state, ins, "src != %%dx");
23964 }
23965 fprintf(fp, "\t%s %s, %s\n",
23966 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023967 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023968 reg(state, ins, mask));
23969 }
23970}
23971
23972static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23973{
23974 const char *op;
23975 int mask;
23976 int lreg;
23977 mask = 0;
23978 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023979 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023980 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23981 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23982 default:
23983 internal_error(state, ins, "not an out operation");
23984 op = 0;
23985 break;
23986 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023987 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023988 if (!reg_is_reg(state, lreg, REG_EAX)) {
23989 internal_error(state, ins, "src != %%eax");
23990 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023991 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023992 fprintf(fp, "\t%s %s,",
23993 op, reg(state, RHS(ins, 0), mask));
23994 print_const_val(state, RHS(ins, 1), fp);
23995 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023996 }
23997 else {
23998 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023999 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024000 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24001 internal_error(state, ins, "dst != %%dx");
24002 }
24003 fprintf(fp, "\t%s %s, %s\n",
24004 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024005 reg(state, RHS(ins, 0), mask),
24006 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024007 }
24008}
24009
24010static void print_op_move(struct compile_state *state,
24011 struct triple *ins, FILE *fp)
24012{
24013 /* op_move is complex because there are many types
24014 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024015 * Because OP_COPY will be introduced in arbitrary locations
24016 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024017 * OP_CONVERT can change the flags and it is the only operation
24018 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024019 */
24020 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24021 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024022 if (state->arch->features & X86_NOOP_COPY) {
24023 omit_copy = 0;
24024 }
24025 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024026 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024027 dst = ins;
24028 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024029 else {
24030 internal_error(state, ins, "unknown move operation");
24031 src = dst = 0;
24032 }
Eric Biederman90089602004-05-28 14:11:54 +000024033 if (reg_size(state, dst) < size_of(state, dst->type)) {
24034 internal_error(state, ins, "Invalid destination register");
24035 }
24036 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24037 fprintf(state->errout, "src type: ");
24038 name_of(state->errout, src->type);
24039 fprintf(state->errout, "\n");
24040 fprintf(state->errout, "dst type: ");
24041 name_of(state->errout, dst->type);
24042 fprintf(state->errout, "\n");
24043 internal_error(state, ins, "Type mismatch for OP_COPY");
24044 }
24045
Eric Biederman0babc1c2003-05-09 02:39:00 +000024046 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024047 int src_reg, dst_reg;
24048 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024049 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024050 dst_reg = ID_REG(dst->id);
24051 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024052 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024053 /* If the class is the same just move the register */
24054 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024055 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024056 if ((src_reg != dst_reg) || !omit_copy) {
24057 fprintf(fp, "\tmov %s, %s\n",
24058 reg(state, src, src_regcm),
24059 reg(state, dst, dst_regcm));
24060 }
24061 }
24062 /* Move 32bit to 16bit */
24063 else if ((src_regcm & REGCM_GPR32) &&
24064 (dst_regcm & REGCM_GPR16)) {
24065 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24066 if ((src_reg != dst_reg) || !omit_copy) {
24067 fprintf(fp, "\tmovw %s, %s\n",
24068 arch_reg_str(src_reg),
24069 arch_reg_str(dst_reg));
24070 }
24071 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024072 /* Move from 32bit gprs to 16bit gprs */
24073 else if ((src_regcm & REGCM_GPR32) &&
24074 (dst_regcm & REGCM_GPR16)) {
24075 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24076 if ((src_reg != dst_reg) || !omit_copy) {
24077 fprintf(fp, "\tmov %s, %s\n",
24078 arch_reg_str(src_reg),
24079 arch_reg_str(dst_reg));
24080 }
24081 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024082 /* Move 32bit to 8bit */
24083 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024084 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024085 {
24086 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24087 if ((src_reg != dst_reg) || !omit_copy) {
24088 fprintf(fp, "\tmovb %s, %s\n",
24089 arch_reg_str(src_reg),
24090 arch_reg_str(dst_reg));
24091 }
24092 }
24093 /* Move 16bit to 8bit */
24094 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024095 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024096 {
24097 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24098 if ((src_reg != dst_reg) || !omit_copy) {
24099 fprintf(fp, "\tmovb %s, %s\n",
24100 arch_reg_str(src_reg),
24101 arch_reg_str(dst_reg));
24102 }
24103 }
24104 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000024105 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024106 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024107 const char *op;
24108 op = is_signed(src->type)? "movsx": "movzx";
24109 fprintf(fp, "\t%s %s, %s\n",
24110 op,
24111 reg(state, src, src_regcm),
24112 reg(state, dst, dst_regcm));
24113 }
24114 /* Move between sse registers */
24115 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24116 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024117 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024118 reg(state, src, src_regcm),
24119 reg(state, dst, dst_regcm));
24120 }
24121 }
Eric Biederman530b5192003-07-01 10:05:30 +000024122 /* Move between mmx registers */
24123 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024124 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024125 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024126 reg(state, src, src_regcm),
24127 reg(state, dst, dst_regcm));
24128 }
24129 }
Eric Biederman530b5192003-07-01 10:05:30 +000024130 /* Move from sse to mmx registers */
24131 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24132 fprintf(fp, "\tmovdq2q %s, %s\n",
24133 reg(state, src, src_regcm),
24134 reg(state, dst, dst_regcm));
24135 }
24136 /* Move from mmx to sse registers */
24137 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24138 fprintf(fp, "\tmovq2dq %s, %s\n",
24139 reg(state, src, src_regcm),
24140 reg(state, dst, dst_regcm));
24141 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024142 /* Move between 32bit gprs & mmx/sse registers */
24143 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24144 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24145 fprintf(fp, "\tmovd %s, %s\n",
24146 reg(state, src, src_regcm),
24147 reg(state, dst, dst_regcm));
24148 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024149 /* Move from 16bit gprs & mmx/sse registers */
24150 else if ((src_regcm & REGCM_GPR16) &&
24151 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24152 const char *op;
24153 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024154 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024155 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24156 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24157 op,
24158 arch_reg_str(src_reg),
24159 arch_reg_str(mid_reg),
24160 arch_reg_str(mid_reg),
24161 arch_reg_str(dst_reg));
24162 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024163 /* Move from mmx/sse registers to 16bit gprs */
24164 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24165 (dst_regcm & REGCM_GPR16)) {
24166 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24167 fprintf(fp, "\tmovd %s, %s\n",
24168 arch_reg_str(src_reg),
24169 arch_reg_str(dst_reg));
24170 }
Eric Biederman530b5192003-07-01 10:05:30 +000024171 /* Move from gpr to 64bit dividend */
24172 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24173 (dst_regcm & REGCM_DIVIDEND64)) {
24174 const char *extend;
24175 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24176 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
24177 arch_reg_str(src_reg),
24178 extend);
24179 }
24180 /* Move from 64bit gpr to gpr */
24181 else if ((src_regcm & REGCM_DIVIDEND64) &&
24182 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24183 if (dst_regcm & REGCM_GPR32) {
24184 src_reg = REG_EAX;
24185 }
24186 else if (dst_regcm & REGCM_GPR16) {
24187 src_reg = REG_AX;
24188 }
24189 else if (dst_regcm & REGCM_GPR8_LO) {
24190 src_reg = REG_AL;
24191 }
24192 fprintf(fp, "\tmov %s, %s\n",
24193 arch_reg_str(src_reg),
24194 arch_reg_str(dst_reg));
24195 }
24196 /* Move from mmx/sse registers to 64bit gpr */
24197 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24198 (dst_regcm & REGCM_DIVIDEND64)) {
24199 const char *extend;
24200 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24201 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24202 arch_reg_str(src_reg),
24203 extend);
24204 }
24205 /* Move from 64bit gpr to mmx/sse register */
24206 else if ((src_regcm & REGCM_DIVIDEND64) &&
24207 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24208 fprintf(fp, "\tmovd %%eax, %s\n",
24209 arch_reg_str(dst_reg));
24210 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024211#if X86_4_8BIT_GPRS
24212 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024213 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024214 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24215 const char *op;
24216 int mid_reg;
24217 op = is_signed(src->type)? "movsx":"movzx";
24218 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24219 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24220 op,
24221 reg(state, src, src_regcm),
24222 arch_reg_str(mid_reg),
24223 arch_reg_str(mid_reg),
24224 reg(state, dst, dst_regcm));
24225 }
24226 /* Move from mmx/sse registers and 8bit gprs */
24227 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024228 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024229 int mid_reg;
24230 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24231 fprintf(fp, "\tmovd %s, %s\n",
24232 reg(state, src, src_regcm),
24233 arch_reg_str(mid_reg));
24234 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024235 /* Move from 32bit gprs to 8bit gprs */
24236 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024237 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024238 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24239 if ((src_reg != dst_reg) || !omit_copy) {
24240 fprintf(fp, "\tmov %s, %s\n",
24241 arch_reg_str(src_reg),
24242 arch_reg_str(dst_reg));
24243 }
24244 }
24245 /* Move from 16bit gprs to 8bit gprs */
24246 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024247 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024248 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24249 if ((src_reg != dst_reg) || !omit_copy) {
24250 fprintf(fp, "\tmov %s, %s\n",
24251 arch_reg_str(src_reg),
24252 arch_reg_str(dst_reg));
24253 }
24254 }
24255#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024256 /* Move from %eax:%edx to %eax:%edx */
24257 else if ((src_regcm & REGCM_DIVIDEND64) &&
24258 (dst_regcm & REGCM_DIVIDEND64) &&
24259 (src_reg == dst_reg)) {
24260 if (!omit_copy) {
24261 fprintf(fp, "\t/*mov %s, %s*/\n",
24262 arch_reg_str(src_reg),
24263 arch_reg_str(dst_reg));
24264 }
24265 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024266 else {
Eric Biederman90089602004-05-28 14:11:54 +000024267 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24268 internal_error(state, ins, "attempt to copy from %%eflags!");
24269 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024270 internal_error(state, ins, "unknown copy type");
24271 }
24272 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024273 else {
Eric Biederman90089602004-05-28 14:11:54 +000024274 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024275 int dst_reg;
24276 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024277 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024278 dst_reg = ID_REG(dst->id);
24279 dst_regcm = arch_reg_regcm(state, dst_reg);
24280 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24281 fprintf(fp, "\tmov ");
24282 print_const_val(state, src, fp);
24283 fprintf(fp, ", %s\n",
24284 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24285 }
24286 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024287 if (dst_size > SIZEOF_I32) {
24288 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024289 }
24290 fprintf(fp, "\tmov $0, %%edx\n");
24291 fprintf(fp, "\tmov ");
24292 print_const_val(state, src, fp);
24293 fprintf(fp, ", %%eax\n");
24294 }
24295 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024296 if (dst_size > SIZEOF_I16) {
24297 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024298 }
24299 fprintf(fp, "\tmov $0, %%dx\n");
24300 fprintf(fp, "\tmov ");
24301 print_const_val(state, src, fp);
24302 fprintf(fp, ", %%ax");
24303 }
24304 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24305 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024306 if (dst_size > SIZEOF_I32) {
24307 internal_error(state, ins, "%d bit constant...", dst_size);
24308 }
24309 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024310 fprintf(fp, "\tmovd L%s%lu, %s\n",
24311 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024312 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24313 }
24314 else {
24315 internal_error(state, ins, "unknown copy immediate type");
24316 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024317 }
Eric Biederman90089602004-05-28 14:11:54 +000024318 /* Leave now if this is not a type conversion */
24319 if (ins->op != OP_CONVERT) {
24320 return;
24321 }
24322 /* Now make certain I have not logically overflowed the destination */
24323 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24324 (size_of(state, dst->type) < reg_size(state, dst)))
24325 {
24326 unsigned long mask;
24327 int dst_reg;
24328 int dst_regcm;
24329 if (size_of(state, dst->type) >= 32) {
24330 fprintf(state->errout, "dst type: ");
24331 name_of(state->errout, dst->type);
24332 fprintf(state->errout, "\n");
24333 internal_error(state, dst, "unhandled dst type size");
24334 }
24335 mask = 1;
24336 mask <<= size_of(state, dst->type);
24337 mask -= 1;
24338
24339 dst_reg = ID_REG(dst->id);
24340 dst_regcm = arch_reg_regcm(state, dst_reg);
24341
24342 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24343 fprintf(fp, "\tand $0x%lx, %s\n",
24344 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24345 }
24346 else if (dst_regcm & REGCM_MMX) {
24347 long ref;
24348 ref = get_mask_pool_ref(state, dst, mask, fp);
24349 fprintf(fp, "\tpand L%s%lu, %s\n",
24350 state->compiler->label_prefix, ref,
24351 reg(state, dst, REGCM_MMX));
24352 }
24353 else if (dst_regcm & REGCM_XMM) {
24354 long ref;
24355 ref = get_mask_pool_ref(state, dst, mask, fp);
24356 fprintf(fp, "\tpand L%s%lu, %s\n",
24357 state->compiler->label_prefix, ref,
24358 reg(state, dst, REGCM_XMM));
24359 }
24360 else {
24361 fprintf(state->errout, "dst type: ");
24362 name_of(state->errout, dst->type);
24363 fprintf(state->errout, "\n");
24364 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24365 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24366 }
24367 }
24368 /* Make certain I am properly sign extended */
24369 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24370 (is_signed(src->type)))
24371 {
24372 int bits, reg_bits, shift_bits;
24373 int dst_reg;
24374 int dst_regcm;
24375
24376 bits = size_of(state, src->type);
24377 reg_bits = reg_size(state, dst);
24378 if (reg_bits > 32) {
24379 reg_bits = 32;
24380 }
24381 shift_bits = reg_bits - size_of(state, src->type);
24382 dst_reg = ID_REG(dst->id);
24383 dst_regcm = arch_reg_regcm(state, dst_reg);
24384
24385 if (shift_bits < 0) {
24386 internal_error(state, dst, "negative shift?");
24387 }
24388
24389 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24390 fprintf(fp, "\tshl $%d, %s\n",
24391 shift_bits,
24392 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24393 fprintf(fp, "\tsar $%d, %s\n",
24394 shift_bits,
24395 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24396 }
24397 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24398 fprintf(fp, "\tpslld $%d, %s\n",
24399 shift_bits,
24400 reg(state, dst, REGCM_MMX | REGCM_XMM));
24401 fprintf(fp, "\tpsrad $%d, %s\n",
24402 shift_bits,
24403 reg(state, dst, REGCM_MMX | REGCM_XMM));
24404 }
24405 else {
24406 fprintf(state->errout, "dst type: ");
24407 name_of(state->errout, dst->type);
24408 fprintf(state->errout, "\n");
24409 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24410 internal_error(state, dst, "failed to signed extend value");
24411 }
24412 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024413}
24414
24415static void print_op_load(struct compile_state *state,
24416 struct triple *ins, FILE *fp)
24417{
24418 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024419 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024420 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024421 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024422 if (is_const(src) || is_const(dst)) {
24423 internal_error(state, ins, "unknown load operation");
24424 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024425 switch(ins->type->type & TYPE_MASK) {
24426 case TYPE_CHAR: op = "movsbl"; break;
24427 case TYPE_UCHAR: op = "movzbl"; break;
24428 case TYPE_SHORT: op = "movswl"; break;
24429 case TYPE_USHORT: op = "movzwl"; break;
24430 case TYPE_INT: case TYPE_UINT:
24431 case TYPE_LONG: case TYPE_ULONG:
24432 case TYPE_POINTER:
24433 op = "movl";
24434 break;
24435 default:
24436 internal_error(state, ins, "unknown type in load");
24437 op = "<invalid opcode>";
24438 break;
24439 }
24440 fprintf(fp, "\t%s (%s), %s\n",
24441 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024442 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024443 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024444}
24445
24446
24447static void print_op_store(struct compile_state *state,
24448 struct triple *ins, FILE *fp)
24449{
24450 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024451 dst = RHS(ins, 0);
24452 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024453 if (is_const(src) && (src->op == OP_INTCONST)) {
24454 long_t value;
24455 value = (long_t)(src->u.cval);
24456 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24457 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024458 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024459 reg(state, dst, REGCM_GPR32));
24460 }
24461 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24462 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24463 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024464 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024465 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024466 }
24467 else {
24468 if (is_const(src) || is_const(dst)) {
24469 internal_error(state, ins, "unknown store operation");
24470 }
24471 fprintf(fp, "\tmov%s %s, (%s)\n",
24472 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024473 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024474 reg(state, dst, REGCM_GPR32));
24475 }
24476
24477
24478}
24479
24480static void print_op_smul(struct compile_state *state,
24481 struct triple *ins, FILE *fp)
24482{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024483 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024484 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024485 reg(state, RHS(ins, 1), REGCM_GPR32),
24486 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024487 }
24488 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024489 fprintf(fp, "\timul ");
24490 print_const_val(state, RHS(ins, 1), fp);
24491 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024492 }
24493}
24494
24495static void print_op_cmp(struct compile_state *state,
24496 struct triple *ins, FILE *fp)
24497{
24498 unsigned mask;
24499 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024500 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024501 dreg = check_reg(state, ins, REGCM_FLAGS);
24502 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24503 internal_error(state, ins, "bad dest register for cmp");
24504 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024505 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024506 fprintf(fp, "\tcmp ");
24507 print_const_val(state, RHS(ins, 1), fp);
24508 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024509 }
24510 else {
24511 unsigned lmask, rmask;
24512 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024513 lreg = check_reg(state, RHS(ins, 0), mask);
24514 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024515 lmask = arch_reg_regcm(state, lreg);
24516 rmask = arch_reg_regcm(state, rreg);
24517 mask = lmask & rmask;
24518 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024519 reg(state, RHS(ins, 1), mask),
24520 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024521 }
24522}
24523
24524static void print_op_test(struct compile_state *state,
24525 struct triple *ins, FILE *fp)
24526{
24527 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024528 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024529 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024530 reg(state, RHS(ins, 0), mask),
24531 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024532}
24533
24534static void print_op_branch(struct compile_state *state,
24535 struct triple *branch, FILE *fp)
24536{
24537 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024538 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024539 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024540 internal_error(state, branch, "jmp with condition?");
24541 }
24542 bop = "jmp";
24543 }
24544 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024545 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024546 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024547 internal_error(state, branch, "jmpcc without condition?");
24548 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024549 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24550 if ((RHS(branch, 0)->op != OP_CMP) &&
24551 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024552 internal_error(state, branch, "bad branch test");
24553 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024554#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024555#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024556#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024557 ptr = RHS(branch, 0);
24558 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24559 if (ptr->op != OP_COPY) {
24560 internal_error(state, branch, "branch does not follow test");
24561 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024562 }
24563 switch(branch->op) {
24564 case OP_JMP_EQ: bop = "jz"; break;
24565 case OP_JMP_NOTEQ: bop = "jnz"; break;
24566 case OP_JMP_SLESS: bop = "jl"; break;
24567 case OP_JMP_ULESS: bop = "jb"; break;
24568 case OP_JMP_SMORE: bop = "jg"; break;
24569 case OP_JMP_UMORE: bop = "ja"; break;
24570 case OP_JMP_SLESSEQ: bop = "jle"; break;
24571 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24572 case OP_JMP_SMOREEQ: bop = "jge"; break;
24573 case OP_JMP_UMOREEQ: bop = "jae"; break;
24574 default:
24575 internal_error(state, branch, "Invalid branch op");
24576 break;
24577 }
24578
24579 }
Eric Biederman90089602004-05-28 14:11:54 +000024580#if 1
24581 if (branch->op == OP_CALL) {
24582 fprintf(fp, "\t/* call */\n");
24583 }
24584#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024585 fprintf(fp, "\t%s L%s%lu\n",
24586 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024587 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024588 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024589}
24590
Eric Biederman5ade04a2003-10-22 04:03:46 +000024591static void print_op_ret(struct compile_state *state,
24592 struct triple *branch, FILE *fp)
24593{
24594 fprintf(fp, "\tjmp *%s\n",
24595 reg(state, RHS(branch, 0), REGCM_GPR32));
24596}
24597
Eric Biedermanb138ac82003-04-22 18:44:01 +000024598static void print_op_set(struct compile_state *state,
24599 struct triple *set, FILE *fp)
24600{
24601 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024602 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024603 internal_error(state, set, "setcc without condition?");
24604 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024605 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24606 if ((RHS(set, 0)->op != OP_CMP) &&
24607 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024608 internal_error(state, set, "bad set test");
24609 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024610 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024611 internal_error(state, set, "set does not follow test");
24612 }
24613 switch(set->op) {
24614 case OP_SET_EQ: sop = "setz"; break;
24615 case OP_SET_NOTEQ: sop = "setnz"; break;
24616 case OP_SET_SLESS: sop = "setl"; break;
24617 case OP_SET_ULESS: sop = "setb"; break;
24618 case OP_SET_SMORE: sop = "setg"; break;
24619 case OP_SET_UMORE: sop = "seta"; break;
24620 case OP_SET_SLESSEQ: sop = "setle"; break;
24621 case OP_SET_ULESSEQ: sop = "setbe"; break;
24622 case OP_SET_SMOREEQ: sop = "setge"; break;
24623 case OP_SET_UMOREEQ: sop = "setae"; break;
24624 default:
24625 internal_error(state, set, "Invalid set op");
24626 break;
24627 }
24628 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024629 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024630}
24631
24632static void print_op_bit_scan(struct compile_state *state,
24633 struct triple *ins, FILE *fp)
24634{
24635 const char *op;
24636 switch(ins->op) {
24637 case OP_BSF: op = "bsf"; break;
24638 case OP_BSR: op = "bsr"; break;
24639 default:
24640 internal_error(state, ins, "unknown bit scan");
24641 op = 0;
24642 break;
24643 }
24644 fprintf(fp,
24645 "\t%s %s, %s\n"
24646 "\tjnz 1f\n"
24647 "\tmovl $-1, %s\n"
24648 "1:\n",
24649 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024650 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024651 reg(state, ins, REGCM_GPR32),
24652 reg(state, ins, REGCM_GPR32));
24653}
24654
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024655
Eric Biedermanb138ac82003-04-22 18:44:01 +000024656static void print_sdecl(struct compile_state *state,
24657 struct triple *ins, FILE *fp)
24658{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024659 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Stefan Reinauerbe84b012009-04-04 13:20:33 +000024660 fprintf(fp, ".balign %ld\n", align_of_in_bytes(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000024661 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024662 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024663 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024664 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024665
24666}
24667
24668static void print_instruction(struct compile_state *state,
24669 struct triple *ins, FILE *fp)
24670{
24671 /* Assumption: after I have exted the register allocator
24672 * everything is in a valid register.
24673 */
24674 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024675 case OP_ASM:
24676 print_op_asm(state, ins, fp);
24677 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024678 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24679 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24680 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24681 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24682 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24683 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24684 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24685 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24686 case OP_POS: break;
24687 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24688 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024689 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024690 case OP_INTCONST:
24691 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024692 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024693 /* Don't generate anything here for constants */
24694 case OP_PHI:
24695 /* Don't generate anything for variable declarations. */
24696 break;
Eric Biederman90089602004-05-28 14:11:54 +000024697 case OP_UNKNOWNVAL:
24698 fprintf(fp, " /* unknown %s */\n",
24699 reg(state, ins, REGCM_ALL));
24700 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024701 case OP_SDECL:
24702 print_sdecl(state, ins, fp);
24703 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024704 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024705 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024706 print_op_move(state, ins, fp);
24707 break;
24708 case OP_LOAD:
24709 print_op_load(state, ins, fp);
24710 break;
24711 case OP_STORE:
24712 print_op_store(state, ins, fp);
24713 break;
24714 case OP_SMUL:
24715 print_op_smul(state, ins, fp);
24716 break;
24717 case OP_CMP: print_op_cmp(state, ins, fp); break;
24718 case OP_TEST: print_op_test(state, ins, fp); break;
24719 case OP_JMP:
24720 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24721 case OP_JMP_SLESS: case OP_JMP_ULESS:
24722 case OP_JMP_SMORE: case OP_JMP_UMORE:
24723 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24724 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024725 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024726 print_op_branch(state, ins, fp);
24727 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024728 case OP_RET:
24729 print_op_ret(state, ins, fp);
24730 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024731 case OP_SET_EQ: case OP_SET_NOTEQ:
24732 case OP_SET_SLESS: case OP_SET_ULESS:
24733 case OP_SET_SMORE: case OP_SET_UMORE:
24734 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24735 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24736 print_op_set(state, ins, fp);
24737 break;
24738 case OP_INB: case OP_INW: case OP_INL:
24739 print_op_in(state, ins, fp);
24740 break;
24741 case OP_OUTB: case OP_OUTW: case OP_OUTL:
24742 print_op_out(state, ins, fp);
24743 break;
24744 case OP_BSF:
24745 case OP_BSR:
24746 print_op_bit_scan(state, ins, fp);
24747 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024748 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024749 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024750 fprintf(fp, "\trdmsr\n");
24751 break;
24752 case OP_WRMSR:
24753 fprintf(fp, "\twrmsr\n");
24754 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024755 case OP_HLT:
24756 fprintf(fp, "\thlt\n");
24757 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024758 case OP_SDIVT:
24759 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24760 break;
24761 case OP_UDIVT:
24762 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24763 break;
24764 case OP_UMUL:
24765 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24766 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024767 case OP_LABEL:
24768 if (!ins->use) {
24769 return;
24770 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024771 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024772 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024773 break;
Eric Biederman90089602004-05-28 14:11:54 +000024774 case OP_ADECL:
24775 /* Ignore adecls with no registers error otherwise */
24776 if (!noop_adecl(ins)) {
24777 internal_error(state, ins, "adecl remains?");
24778 }
24779 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024780 /* Ignore OP_PIECE */
24781 case OP_PIECE:
24782 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024783 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024784 case OP_SDIV: case OP_UDIV:
24785 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024786 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24787 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24788 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24789 default:
24790 internal_error(state, ins, "unknown op: %d %s",
24791 ins->op, tops(ins->op));
24792 break;
24793 }
24794}
24795
24796static void print_instructions(struct compile_state *state)
24797{
24798 struct triple *first, *ins;
24799 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024800 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024801 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024802 int max_inline_depth;
24803 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024804 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024805 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024806 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024807 /* Masks for common sizes */
24808 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24809 fprintf(fp, ".balign 16\n");
24810 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24811 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24812 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24813 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024814 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024815 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024816 ins = first;
24817 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024818 if (print_location &&
24819 last_occurance != ins->occurance) {
24820 if (!ins->occurance->parent) {
24821 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024822 ins->occurance->function?ins->occurance->function:"(null)",
24823 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024824 ins->occurance->line,
24825 ins->occurance->col);
24826 }
24827 else {
24828 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024829 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024830 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024831 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024832 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024833 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024834 fprintf(fp, "\t * %s,%s:%d.%d\n",
24835 ptr->function,
24836 ptr->filename,
24837 ptr->line,
24838 ptr->col);
24839 }
24840 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024841 if (inline_depth > max_inline_depth) {
24842 max_inline_depth = inline_depth;
24843 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024844 }
24845 if (last_occurance) {
24846 put_occurance(last_occurance);
24847 }
24848 get_occurance(ins->occurance);
24849 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024850 }
24851
24852 print_instruction(state, ins, fp);
24853 ins = ins->next;
24854 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024855 if (print_location) {
24856 fprintf(fp, "/* max inline depth %d */\n",
24857 max_inline_depth);
24858 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024859}
Eric Biederman530b5192003-07-01 10:05:30 +000024860
Eric Biedermanb138ac82003-04-22 18:44:01 +000024861static void generate_code(struct compile_state *state)
24862{
24863 generate_local_labels(state);
24864 print_instructions(state);
24865
24866}
24867
Eric Biederman90089602004-05-28 14:11:54 +000024868static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024869{
Eric Biederman90089602004-05-28 14:11:54 +000024870 int tok;
24871 FILE *fp;
24872 int line;
24873 const char *filename;
24874 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024875 filename = 0;
24876 line = 0;
24877 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024878 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024879 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024880 const char *token_str;
24881 tok = peek(state);
24882 if (tok == TOK_EOF) {
24883 break;
24884 }
Eric Biederman41203d92004-11-08 09:31:09 +000024885 tk = eat(state, tok);
Eric Biederman90089602004-05-28 14:11:54 +000024886 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024887 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024888 tk->str_len ? tk->val.str :
24889 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024890
24891 file = state->file;
24892 while(file->macro && file->prev) {
24893 file = file->prev;
24894 }
24895 if (!file->macro &&
24896 ((file->line != line) || (file->basename != filename)))
24897 {
Eric Biederman90089602004-05-28 14:11:54 +000024898 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024899 if ((file->basename == filename) &&
24900 (line < file->line)) {
24901 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024902 fprintf(fp, "\n");
24903 line++;
24904 }
24905 }
24906 else {
24907 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024908 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024909 }
Eric Biederman132368b2004-11-09 08:59:23 +000024910 line = file->line;
24911 filename = file->basename;
24912 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024913 for(i = 0; i < col; i++) {
24914 fprintf(fp, " ");
24915 }
24916 }
24917
24918 fprintf(fp, "%s ", token_str);
24919
24920 if (state->compiler->debug & DEBUG_TOKENS) {
24921 loc(state->dbgout, state, 0);
24922 fprintf(state->dbgout, "%s <- `%s'\n",
24923 tokens[tok], token_str);
24924 }
24925 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024926}
24927
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000024928static void compile(const char *filename, const char *includefile,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024929 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024930{
24931 int i;
24932 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024933 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024934 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024935 state.compiler = compiler;
24936 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024937 state.file = 0;
24938 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24939 memset(&state.token[i], 0, sizeof(state.token[i]));
24940 state.token[i].tok = -1;
24941 }
Eric Biederman90089602004-05-28 14:11:54 +000024942 /* Remember the output descriptors */
24943 state.errout = stderr;
24944 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024945 /* Remember the output filename */
Eric Biederman5ade04a2003-10-22 04:03:46 +000024946 state.output = fopen(state.compiler->ofilename, "w");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024947 if (!state.output) {
24948 error(&state, 0, "Cannot open output file %s\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024949 state.compiler->ofilename);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024950 }
Eric Biederman90089602004-05-28 14:11:54 +000024951 /* Make certain a good cleanup happens */
24952 exit_state = &state;
24953 atexit(exit_cleanup);
24954
Eric Biedermanb138ac82003-04-22 18:44:01 +000024955 /* Prep the preprocessor */
24956 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024957 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024958 /* register the C keywords */
24959 register_keywords(&state);
24960 /* register the keywords the macro preprocessor knows */
24961 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024962 /* generate some builtin macros */
24963 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024964 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024965 state.i_switch = lookup(&state, "switch", 6);
24966 state.i_case = lookup(&state, "case", 4);
24967 state.i_continue = lookup(&state, "continue", 8);
24968 state.i_break = lookup(&state, "break", 5);
24969 state.i_default = lookup(&state, "default", 7);
24970 state.i_return = lookup(&state, "return", 6);
24971 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024972 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24973 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24974 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24975 /* Memorize where predefined identifiers are. */
24976 state.i___func__ = lookup(&state, "__func__", 8);
24977 /* Memorize where some attribute keywords are. */
24978 state.i_noinline = lookup(&state, "noinline", 8);
24979 state.i_always_inline = lookup(&state, "always_inline", 13);
24980
24981 /* Process the command line macros */
24982 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024983
24984 /* Allocate beginning bounding labels for the function list */
24985 state.first = label(&state);
24986 state.first->id |= TRIPLE_FLAG_VOLATILE;
24987 use_triple(state.first, state.first);
24988 ptr = label(&state);
24989 ptr->id |= TRIPLE_FLAG_VOLATILE;
24990 use_triple(ptr, ptr);
24991 flatten(&state, state.first, ptr);
24992
Eric Biederman5ade04a2003-10-22 04:03:46 +000024993 /* Allocate a label for the pool of global variables */
24994 state.global_pool = label(&state);
24995 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
24996 flatten(&state, state.first, state.global_pool);
24997
Eric Biedermanb138ac82003-04-22 18:44:01 +000024998 /* Enter the globl definition scope */
24999 start_scope(&state);
25000 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025001
Eric Biedermanb138ac82003-04-22 18:44:01 +000025002 compile_file(&state, filename, 1);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025003 if (includefile)
25004 compile_file(&state, includefile, 1);
Eric Biederman90089602004-05-28 14:11:54 +000025005
25006 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025007 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025008 print_preprocessed_tokens(&state);
25009 return;
25010 }
25011
Eric Biedermanb138ac82003-04-22 18:44:01 +000025012 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025013
Eric Biedermanb138ac82003-04-22 18:44:01 +000025014 /* Exit the global definition scope */
25015 end_scope(&state);
25016
25017 /* Now that basic compilation has happened
25018 * optimize the intermediate code
25019 */
25020 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025021
Eric Biedermanb138ac82003-04-22 18:44:01 +000025022 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025023 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025024 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025025 }
Eric Biederman90089602004-05-28 14:11:54 +000025026 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025027}
25028
Eric Biederman90089602004-05-28 14:11:54 +000025029static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025030{
Eric Biederman90089602004-05-28 14:11:54 +000025031 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025032}
25033
25034static void usage(void)
25035{
Eric Biederman90089602004-05-28 14:11:54 +000025036 FILE *fp = stdout;
25037 version(fp);
25038 fprintf(fp,
25039 "\nUsage: romcc [options] <source>.c\n"
25040 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25041 "Options: \n"
25042 "-o <output file name>\n"
25043 "-f<option> Specify a generic compiler option\n"
25044 "-m<option> Specify a arch dependent option\n"
25045 "-- Specify this is the last option\n"
25046 "\nGeneric compiler options:\n"
25047 );
25048 compiler_usage(fp);
25049 fprintf(fp,
25050 "\nArchitecture compiler options:\n"
25051 );
25052 arch_usage(fp);
25053 fprintf(fp,
25054 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025055 );
25056}
25057
25058static void arg_error(char *fmt, ...)
25059{
25060 va_list args;
25061 va_start(args, fmt);
25062 vfprintf(stderr, fmt, args);
25063 va_end(args);
25064 usage();
25065 exit(1);
25066}
25067
25068int main(int argc, char **argv)
25069{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025070 const char *filename;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025071 const char *includefile = NULL;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025072 struct compiler_state compiler;
25073 struct arch_state arch;
25074 int all_opts;
Eric Biederman90089602004-05-28 14:11:54 +000025075
25076
25077 /* I don't want any surprises */
25078 setlocale(LC_ALL, "C");
25079
Eric Biederman5ade04a2003-10-22 04:03:46 +000025080 init_compiler_state(&compiler);
25081 init_arch_state(&arch);
25082 filename = 0;
25083 all_opts = 0;
25084 while(argc > 1) {
25085 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25086 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025087 argv += 2;
25088 argc -= 2;
25089 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025090 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025091 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025092 result = -1;
25093 if (strcmp(argv[1], "--") == 0) {
25094 result = 0;
25095 all_opts = 1;
25096 }
Eric Biederman90089602004-05-28 14:11:54 +000025097 else if (strncmp(argv[1], "-E", 2) == 0) {
25098 result = compiler_encode_flag(&compiler, argv[1]);
25099 }
25100 else if (strncmp(argv[1], "-O", 2) == 0) {
25101 result = compiler_encode_flag(&compiler, argv[1]);
25102 }
25103 else if (strncmp(argv[1], "-I", 2) == 0) {
25104 result = compiler_encode_flag(&compiler, argv[1]);
25105 }
25106 else if (strncmp(argv[1], "-D", 2) == 0) {
25107 result = compiler_encode_flag(&compiler, argv[1]);
25108 }
25109 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025110 result = compiler_encode_flag(&compiler, argv[1]);
25111 }
25112 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25113 result = compiler_encode_flag(&compiler, argv[1]+2);
25114 }
25115 else if (strncmp(argv[1], "-f", 2) == 0) {
25116 result = compiler_encode_flag(&compiler, argv[1]+2);
25117 }
25118 else if (strncmp(argv[1], "-m", 2) == 0) {
25119 result = arch_encode_flag(&arch, argv[1]+2);
25120 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025121 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025122 if (includefile) {
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025123 arg_error("Only one -include option may be specified.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025124 } else {
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025125 argv++;
25126 argc--;
25127 includefile = argv[1];
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025128 result = 0;
25129 }
25130 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025131 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025132 arg_error("Invalid option specified: %s\n",
25133 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025134 }
25135 argv++;
25136 argc--;
25137 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025138 else {
25139 if (filename) {
25140 arg_error("Only one filename may be specified\n");
25141 }
25142 filename = argv[1];
25143 argv++;
25144 argc--;
25145 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025146 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025147 if (!filename) {
25148 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025149 }
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025150 compile(filename, includefile, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025151
25152 return 0;
25153}