blob: 01d836cef394df0e02973ccb0773600e0eedab6e [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 Reinauerec664bc2013-05-09 14:06:04 -07006#define VERSION_MINOR "73"
7#define RELEASE_DATE "08 May 2013"
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 Reinauer14e22772010-04-27 06:56:47 +000028/* NOTE: Before you even start thinking to touch anything
Stefan Reinauer50542a82007-10-24 11:14:14 +000029 * in this code, set DEBUG_ROMCC_WARNINGS to 1 to get an
Stefan Reinauer14e22772010-04-27 06:56:47 +000030 * insight on the original author's thoughts. We introduced
Stefan Reinauer50542a82007-10-24 11:14:14 +000031 * 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.
Stefan Reinauer14e22772010-04-27 06:56:47 +000064 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000065 * AAA
66 * +---/
67 * /
68 * / +--->CCC
69 * | | / \
70 * | | DDD EEE break;
71 * | | \ \
72 * | | FFF \
73 * \| / \ \
74 * |\ GGG HHH | continue;
75 * | \ \ | |
76 * | \ III | /
Stefan Reinauer14e22772010-04-27 06:56:47 +000077 * | \ | / /
78 * | vvv /
79 * +----BBB /
Eric Biedermanb138ac82003-04-22 18:44:01 +000080 * | /
81 * vv
82 * JJJ
83 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000084 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000085 * AAA
86 * +-----+ | +----+
87 * | \ | / |
88 * | BBB +-+ |
89 * | / \ / | |
90 * | CCC JJJ / /
Stefan Reinauer14e22772010-04-27 06:56:47 +000091 * | / \ / /
92 * | DDD EEE / /
Eric Biedermanb138ac82003-04-22 18:44:01 +000093 * | | +-/ /
Stefan Reinauer14e22772010-04-27 06:56:47 +000094 * | FFF /
95 * | / \ /
96 * | GGG HHH /
Eric Biedermanb138ac82003-04-22 18:44:01 +000097 * | | +-/
98 * | III
Stefan Reinauer14e22772010-04-27 06:56:47 +000099 * +--+
Eric Biedermanb138ac82003-04-22 18:44:01 +0000100 *
Stefan Reinauer14e22772010-04-27 06:56:47 +0000101 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000102 * 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 )
Stefan Reinauer14e22772010-04-27 06:56:47 +0000120 * / \
Eric Biedermanb138ac82003-04-22 18:44:01 +0000121 * 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.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000128 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000129 */
Stefan Reinauerb4d3af82010-02-11 03:21:29 +0000130
131struct filelist {
132 const char *filename;
133 struct filelist *next;
134};
135
136struct filelist *include_filelist = NULL;
137
Stefan Reinauerbed872d2010-04-03 00:03:07 +0000138static void __attribute__((noreturn)) die(char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000139{
140 va_list args;
141
142 va_start(args, fmt);
143 vfprintf(stderr, fmt, args);
144 va_end(args);
145 fflush(stdout);
146 fflush(stderr);
147 exit(1);
148}
149
Eric Biedermanb138ac82003-04-22 18:44:01 +0000150static void *xmalloc(size_t size, const char *name)
151{
152 void *buf;
153 buf = malloc(size);
154 if (!buf) {
155 die("Cannot malloc %ld bytes to hold %s: %s\n",
156 size + 0UL, name, strerror(errno));
157 }
158 return buf;
159}
160
161static void *xcmalloc(size_t size, const char *name)
162{
163 void *buf;
164 buf = xmalloc(size, name);
165 memset(buf, 0, size);
166 return buf;
167}
168
Eric Biederman90089602004-05-28 14:11:54 +0000169static void *xrealloc(void *ptr, size_t size, const char *name)
170{
171 void *buf;
172 buf = realloc(ptr, size);
173 if (!buf) {
174 die("Cannot realloc %ld bytes to hold %s: %s\n",
175 size + 0UL, name, strerror(errno));
176 }
177 return buf;
178}
179
Eric Biedermanb138ac82003-04-22 18:44:01 +0000180static void xfree(const void *ptr)
181{
182 free((void *)ptr);
183}
184
185static char *xstrdup(const char *str)
186{
187 char *new;
188 int len;
189 len = strlen(str);
190 new = xmalloc(len + 1, "xstrdup string");
191 memcpy(new, str, len);
192 new[len] = '\0';
193 return new;
194}
195
196static void xchdir(const char *path)
197{
198 if (chdir(path) != 0) {
Eric Biederman90089602004-05-28 14:11:54 +0000199 die("chdir to `%s' failed: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +0000200 path, strerror(errno));
201 }
202}
203
204static int exists(const char *dirname, const char *filename)
205{
Eric Biederman90089602004-05-28 14:11:54 +0000206 char cwd[MAX_CWD_SIZE];
207 int does_exist;
208
209 if (getcwd(cwd, sizeof(cwd)) == 0) {
210 die("cwd buffer to small");
211 }
212
213 does_exist = 1;
214 if (chdir(dirname) != 0) {
215 does_exist = 0;
216 }
217 if (does_exist && (access(filename, O_RDONLY) < 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000218 if ((errno != EACCES) && (errno != EROFS)) {
219 does_exist = 0;
220 }
221 }
Eric Biederman90089602004-05-28 14:11:54 +0000222 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000223 return does_exist;
224}
225
Patrick Georgi32eeff42014-08-11 09:27:18 +0200226static off_t get_file_size(FILE *f)
227{
228 struct stat s;
229 int fd = fileno(f);
230 if (fd == -1) return -1;
231 if (fstat(fd, &s) == -1) return -1;
232 return s.st_size;
233}
Eric Biedermanb138ac82003-04-22 18:44:01 +0000234
235static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
236{
Eric Biederman90089602004-05-28 14:11:54 +0000237 char cwd[MAX_CWD_SIZE];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000238 char *buf;
239 off_t size, progress;
240 ssize_t result;
Patrick Georgi26774f22009-11-21 19:54:02 +0000241 FILE* file;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000242
Eric Biedermanb138ac82003-04-22 18:44:01 +0000243 if (!filename) {
244 *r_size = 0;
245 return 0;
246 }
Eric Biederman90089602004-05-28 14:11:54 +0000247 if (getcwd(cwd, sizeof(cwd)) == 0) {
248 die("cwd buffer to small");
249 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000250 xchdir(dirname);
Patrick Georgi26774f22009-11-21 19:54:02 +0000251 file = fopen(filename, "rb");
Eric Biederman90089602004-05-28 14:11:54 +0000252 xchdir(cwd);
Patrick Georgi26774f22009-11-21 19:54:02 +0000253 if (file == NULL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000254 die("Cannot open '%s' : %s\n",
255 filename, strerror(errno));
256 }
Patrick Georgi32eeff42014-08-11 09:27:18 +0200257 size = get_file_size(file);
258 if (size == -1) {
259 die("Could not fetch size of '%s': %s\n", filename, strerror(errno));
260 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000261 *r_size = size +1;
262 buf = xmalloc(size +2, filename);
263 buf[size] = '\n'; /* Make certain the file is newline terminated */
264 buf[size+1] = '\0'; /* Null terminate the file for good measure */
265 progress = 0;
266 while(progress < size) {
Patrick Georgi26774f22009-11-21 19:54:02 +0000267 result = fread(buf + progress, 1, size - progress, file);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000268 if (result < 0) {
269 if ((errno == EINTR) || (errno == EAGAIN))
270 continue;
271 die("read on %s of %ld bytes failed: %s\n",
272 filename, (size - progress)+ 0UL, strerror(errno));
273 }
274 progress += result;
275 }
Patrick Georgi26774f22009-11-21 19:54:02 +0000276 fclose(file);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000277 return buf;
278}
279
Eric Biederman83b991a2003-10-11 06:20:25 +0000280/* Types on the destination platform */
Stefan Reinauer50542a82007-10-24 11:14:14 +0000281#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +0000282#warning "FIXME this assumes 32bit x86 is the destination"
Stefan Reinauer50542a82007-10-24 11:14:14 +0000283#endif
Eric Biederman83b991a2003-10-11 06:20:25 +0000284typedef int8_t schar_t;
285typedef uint8_t uchar_t;
286typedef int8_t char_t;
287typedef int16_t short_t;
288typedef uint16_t ushort_t;
289typedef int32_t int_t;
290typedef uint32_t uint_t;
291typedef int32_t long_t;
Patrick Georgia84a99b2009-05-26 14:03:51 +0000292#define ulong_t uint32_t
Eric Biederman83b991a2003-10-11 06:20:25 +0000293
294#define SCHAR_T_MIN (-128)
295#define SCHAR_T_MAX 127
296#define UCHAR_T_MAX 255
297#define CHAR_T_MIN SCHAR_T_MIN
298#define CHAR_T_MAX SCHAR_T_MAX
299#define SHRT_T_MIN (-32768)
300#define SHRT_T_MAX 32767
301#define USHRT_T_MAX 65535
302#define INT_T_MIN (-LONG_T_MAX - 1)
303#define INT_T_MAX 2147483647
304#define UINT_T_MAX 4294967295U
305#define LONG_T_MIN (-LONG_T_MAX - 1)
306#define LONG_T_MAX 2147483647
307#define ULONG_T_MAX 4294967295U
Eric Biedermanb138ac82003-04-22 18:44:01 +0000308
Eric Biederman90089602004-05-28 14:11:54 +0000309#define SIZEOF_I8 8
310#define SIZEOF_I16 16
311#define SIZEOF_I32 32
312#define SIZEOF_I64 64
313
314#define SIZEOF_CHAR 8
315#define SIZEOF_SHORT 16
316#define SIZEOF_INT 32
317#define SIZEOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
318
319
320#define ALIGNOF_CHAR 8
321#define ALIGNOF_SHORT 16
322#define ALIGNOF_INT 32
323#define ALIGNOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
324
325#define REG_SIZEOF_REG 32
326#define REG_SIZEOF_CHAR REG_SIZEOF_REG
327#define REG_SIZEOF_SHORT REG_SIZEOF_REG
328#define REG_SIZEOF_INT REG_SIZEOF_REG
329#define REG_SIZEOF_LONG REG_SIZEOF_REG
330
331#define REG_ALIGNOF_REG REG_SIZEOF_REG
332#define REG_ALIGNOF_CHAR REG_SIZEOF_REG
333#define REG_ALIGNOF_SHORT REG_SIZEOF_REG
334#define REG_ALIGNOF_INT REG_SIZEOF_REG
335#define REG_ALIGNOF_LONG REG_SIZEOF_REG
336
337/* Additional definitions for clarity.
338 * I currently assume a long is the largest native
339 * machine word and that a pointer fits into it.
340 */
341#define SIZEOF_WORD SIZEOF_LONG
342#define SIZEOF_POINTER SIZEOF_LONG
343#define ALIGNOF_WORD ALIGNOF_LONG
344#define ALIGNOF_POINTER ALIGNOF_LONG
345#define REG_SIZEOF_POINTER REG_SIZEOF_LONG
346#define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG
347
Eric Biedermanb138ac82003-04-22 18:44:01 +0000348struct file_state {
349 struct file_state *prev;
350 const char *basename;
351 char *dirname;
Eric Biedermancb364952004-11-15 10:46:44 +0000352 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000353 off_t size;
Eric Biederman90089602004-05-28 14:11:54 +0000354 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000355 int line;
Eric Biederman90089602004-05-28 14:11:54 +0000356 const char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000357 int report_line;
358 const char *report_name;
359 const char *report_dir;
Eric Biedermancb364952004-11-15 10:46:44 +0000360 int macro : 1;
361 int trigraphs : 1;
362 int join_lines : 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000363};
364struct hash_entry;
365struct token {
366 int tok;
367 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +0000368 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000369 int str_len;
370 union {
371 ulong_t integer;
372 const char *str;
Eric Biederman90089602004-05-28 14:11:54 +0000373 int notmacro;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000374 } val;
375};
376
377/* I have two classes of types:
378 * Operational types.
379 * Logical types. (The type the C standard says the operation is of)
380 *
381 * The operational types are:
382 * chars
383 * shorts
384 * ints
385 * longs
386 *
387 * floats
388 * doubles
389 * long doubles
390 *
391 * pointer
392 */
393
394
395/* Machine model.
396 * No memory is useable by the compiler.
397 * There is no floating point support.
398 * All operations take place in general purpose registers.
399 * There is one type of general purpose register.
400 * Unsigned longs are stored in that general purpose register.
401 */
402
403/* Operations on general purpose registers.
404 */
405
Eric Biederman530b5192003-07-01 10:05:30 +0000406#define OP_SDIVT 0
407#define OP_UDIVT 1
408#define OP_SMUL 2
409#define OP_UMUL 3
410#define OP_SDIV 4
411#define OP_UDIV 5
412#define OP_SMOD 6
413#define OP_UMOD 7
414#define OP_ADD 8
415#define OP_SUB 9
416#define OP_SL 10
417#define OP_USR 11
Stefan Reinauer14e22772010-04-27 06:56:47 +0000418#define OP_SSR 12
419#define OP_AND 13
Eric Biederman530b5192003-07-01 10:05:30 +0000420#define OP_XOR 14
421#define OP_OR 15
422#define OP_POS 16 /* Dummy positive operator don't use it */
423#define OP_NEG 17
424#define OP_INVERT 18
Stefan Reinauer14e22772010-04-27 06:56:47 +0000425
Eric Biedermanb138ac82003-04-22 18:44:01 +0000426#define OP_EQ 20
427#define OP_NOTEQ 21
428#define OP_SLESS 22
429#define OP_ULESS 23
430#define OP_SMORE 24
431#define OP_UMORE 25
432#define OP_SLESSEQ 26
433#define OP_ULESSEQ 27
434#define OP_SMOREEQ 28
435#define OP_UMOREEQ 29
Stefan Reinauer14e22772010-04-27 06:56:47 +0000436
Eric Biedermanb138ac82003-04-22 18:44:01 +0000437#define OP_LFALSE 30 /* Test if the expression is logically false */
438#define OP_LTRUE 31 /* Test if the expression is logcially true */
439
440#define OP_LOAD 32
441#define OP_STORE 33
Eric Biederman530b5192003-07-01 10:05:30 +0000442/* For OP_STORE ->type holds the type
443 * RHS(0) holds the destination address
444 * RHS(1) holds the value to store.
445 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000446
Eric Biederman90089602004-05-28 14:11:54 +0000447#define OP_UEXTRACT 34
448/* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register
449 * RHS(0) holds the psuedo register to extract from
450 * ->type holds the size of the bitfield.
451 * ->u.bitfield.size holds the size of the bitfield.
452 * ->u.bitfield.offset holds the offset to extract from
453 */
454#define OP_SEXTRACT 35
455/* OP_SEXTRACT extracts a signed bitfield from a pseudo register
456 * RHS(0) holds the psuedo register to extract from
457 * ->type holds the size of the bitfield.
458 * ->u.bitfield.size holds the size of the bitfield.
459 * ->u.bitfield.offset holds the offset to extract from
460 */
461#define OP_DEPOSIT 36
462/* OP_DEPOSIT replaces a bitfield with a new value.
463 * RHS(0) holds the value to replace a bitifield in.
464 * RHS(1) holds the replacement value
465 * ->u.bitfield.size holds the size of the bitfield.
466 * ->u.bitfield.offset holds the deposit into
467 */
468
469#define OP_NOOP 37
Eric Biedermanb138ac82003-04-22 18:44:01 +0000470
471#define OP_MIN_CONST 50
Eric Biederman90089602004-05-28 14:11:54 +0000472#define OP_MAX_CONST 58
Eric Biedermanb138ac82003-04-22 18:44:01 +0000473#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
474#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000475/* For OP_INTCONST ->type holds the type.
476 * ->u.cval holds the constant value.
477 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000478#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000479/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000480 * information. u.blob holds a pointer to the raw binary
481 * data for the constant initializer.
482 */
483#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000484/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000485 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000486 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000487 */
Eric Biederman90089602004-05-28 14:11:54 +0000488#define OP_UNKNOWNVAL 59
489/* For OP_UNKNOWNAL ->type holds the type.
490 * For some reason we don't know what value this type has.
491 * This allows for variables that have don't have values
492 * assigned yet, or variables whose value we simply do not know.
493 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000494
Stefan Reinauer14e22772010-04-27 06:56:47 +0000495#define OP_WRITE 60
Eric Biedermanb138ac82003-04-22 18:44:01 +0000496/* OP_WRITE moves one pseudo register to another.
Eric Biederman90089602004-05-28 14:11:54 +0000497 * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
498 * RHS(0) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000499 */
500
501#define OP_READ 61
502/* OP_READ reads the value of a variable and makes
503 * it available for the pseudo operation.
504 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000505 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000506 */
507#define OP_COPY 62
Eric Biederman90089602004-05-28 14:11:54 +0000508/* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
Eric Biederman0babc1c2003-05-09 02:39:00 +0000509 */
Eric Biederman90089602004-05-28 14:11:54 +0000510#define OP_CONVERT 63
511/* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0).
512 * And then the type is converted appropriately.
513 */
514#define OP_PIECE 64
Eric Biederman0babc1c2003-05-09 02:39:00 +0000515/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000516 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000517 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000518 */
Eric Biederman90089602004-05-28 14:11:54 +0000519#define OP_ASM 65
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000520/* OP_ASM holds a sequence of assembly instructions, the result
521 * of a C asm directive.
522 * RHS(x) holds input value x to the assembly sequence.
523 * LHS(x) holds the output value x from the assembly sequence.
524 * u.blob holds the string of assembly instructions.
525 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000526
Eric Biederman90089602004-05-28 14:11:54 +0000527#define OP_DEREF 66
Eric Biedermanb138ac82003-04-22 18:44:01 +0000528/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000529 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000530 * OP_DEREF serves as a place holder to indicate all necessary
531 * checks have been done to indicate a value is an lvalue.
532 */
Eric Biederman90089602004-05-28 14:11:54 +0000533#define OP_DOT 67
Eric Biederman0babc1c2003-05-09 02:39:00 +0000534/* OP_DOT references a submember of a structure lvalue.
Eric Biederman90089602004-05-28 14:11:54 +0000535 * MISC(0) holds the lvalue.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000536 * ->u.field holds the name of the field we want.
537 *
Eric Biederman90089602004-05-28 14:11:54 +0000538 * Not seen after structures are flattened.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000539 */
Eric Biederman90089602004-05-28 14:11:54 +0000540#define OP_INDEX 68
541/* OP_INDEX references a submember of a tuple or array lvalue.
542 * MISC(0) holds the lvalue.
543 * ->u.cval holds the index into the lvalue.
544 *
545 * Not seen after structures are flattened.
546 */
547#define OP_VAL 69
Eric Biedermanb138ac82003-04-22 18:44:01 +0000548/* OP_VAL returns the value of a subexpression of the current expression.
549 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000550 * RHS(0) holds the expression.
551 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000552 * value of the expression.
553 *
554 * Not seen outside of expressions.
555 */
Eric Biederman90089602004-05-28 14:11:54 +0000556
557#define OP_TUPLE 70
558/* OP_TUPLE is an array of triples that are either variable
559 * or values for a structure or an array. It is used as
560 * a place holder when flattening compound types.
561 * The value represented by an OP_TUPLE is held in N registers.
562 * LHS(0..N-1) refer to those registers.
563 * ->use is a list of statements that use the value.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000564 *
Eric Biederman90089602004-05-28 14:11:54 +0000565 * Although OP_TUPLE always has register sized pieces they are not
566 * used until structures are flattened/decomposed into their register
Stefan Reinauer14e22772010-04-27 06:56:47 +0000567 * components.
Eric Biederman90089602004-05-28 14:11:54 +0000568 * ???? registers ????
Eric Biedermanb138ac82003-04-22 18:44:01 +0000569 */
570
Eric Biederman90089602004-05-28 14:11:54 +0000571#define OP_BITREF 71
572/* OP_BITREF describes a bitfield as an lvalue.
573 * RHS(0) holds the register value.
574 * ->type holds the type of the bitfield.
575 * ->u.bitfield.size holds the size of the bitfield.
576 * ->u.bitfield.offset holds the offset of the bitfield in the register
577 */
578
579
580#define OP_FCALL 72
Stefan Reinauer14e22772010-04-27 06:56:47 +0000581/* OP_FCALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000582 * MISC(0) holds a pointer to the OP_LIST of a function
583 * RHS(x) holds argument x of a function
Stefan Reinauer14e22772010-04-27 06:56:47 +0000584 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000585 * Currently not seen outside of expressions.
586 */
Eric Biederman90089602004-05-28 14:11:54 +0000587#define OP_PROG 73
588/* OP_PROG is an expression that holds a list of statements, or
589 * expressions. The final expression is the value of the expression.
590 * RHS(0) holds the start of the list.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000591 */
592
593/* statements */
594#define OP_LIST 80
Eric Biederman5ade04a2003-10-22 04:03:46 +0000595/* OP_LIST Holds a list of statements that compose a function, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000596 * RHS(0) holds the list of statements.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000597 * A list of all functions is maintained.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000598 */
599
Eric Biederman5ade04a2003-10-22 04:03:46 +0000600#define OP_BRANCH 81 /* an unconditional branch */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000601/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000602 * TARG(0) holds the branch target.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000603 * ->next holds where to branch to if the branch is not taken.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000604 * The branch target can only be a label
Eric Biedermanb138ac82003-04-22 18:44:01 +0000605 */
606
Eric Biederman5ade04a2003-10-22 04:03:46 +0000607#define OP_CBRANCH 82 /* a conditional branch */
608/* For conditional branch instructions
609 * RHS(0) holds the branch condition.
Eric Biederman41203d92004-11-08 09:31:09 +0000610 * TARG(0) holds the branch target.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000611 * ->next holds where to branch to if the branch is not taken.
612 * The branch target can only be a label
613 */
614
615#define OP_CALL 83 /* an uncontional branch that will return */
616/* For call instructions
617 * MISC(0) holds the OP_RET that returns from the branch
618 * TARG(0) holds the branch target.
619 * ->next holds where to branch to if the branch is not taken.
620 * The branch target can only be a label
621 */
622
623#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
624/* For call instructions
625 * RHS(0) holds the variable with the return address
626 * The branch target can only be a label
627 */
628
629#define OP_LABEL 86
Eric Biedermanb138ac82003-04-22 18:44:01 +0000630/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000631 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000632 */
633
Stefan Reinauer14e22772010-04-27 06:56:47 +0000634#define OP_ADECL 87
Eric Biederman5ade04a2003-10-22 04:03:46 +0000635/* OP_ADECL is a triple that establishes an lvalue for assignments.
Eric Biederman90089602004-05-28 14:11:54 +0000636 * A variable takes N registers to contain.
637 * LHS(0..N-1) refer to an OP_PIECE triple that represents
638 * the Xth register that the variable is stored in.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000639 * ->use is a list of statements that use the variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000640 *
Eric Biederman90089602004-05-28 14:11:54 +0000641 * Although OP_ADECL always has register sized pieces they are not
642 * used until structures are flattened/decomposed into their register
Stefan Reinauer14e22772010-04-27 06:56:47 +0000643 * components.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000644 */
645
Eric Biederman5ade04a2003-10-22 04:03:46 +0000646#define OP_SDECL 88
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000647/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000648 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000649 * ->use is a list of statements that use the variable.
650 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000651 */
652
653
Eric Biederman5ade04a2003-10-22 04:03:46 +0000654#define OP_PHI 89
Stefan Reinauer14e22772010-04-27 06:56:47 +0000655/* OP_PHI is a triple used in SSA form code.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000656 * It is used when multiple code paths merge and a variable needs
657 * a single assignment from any of those code paths.
658 * The operation is a cross between OP_DECL and OP_WRITE, which
Eric Biederman5ade04a2003-10-22 04:03:46 +0000659 * is what OP_PHI is generated from.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000660 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000661 * RHS(x) points to the value from code path x
662 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000663 * in which OP_PHI resides. The elements of the array point to point
664 * to the variables OP_PHI is derived from.
665 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000666 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000667 */
668
Eric Biederman90089602004-05-28 14:11:54 +0000669#if 0
670/* continuation helpers
671 */
672#define OP_CPS_BRANCH 90 /* an unconditional branch */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000673/* OP_CPS_BRANCH calls a continuation
Eric Biederman90089602004-05-28 14:11:54 +0000674 * RHS(x) holds argument x of the function
675 * TARG(0) holds OP_CPS_START target
676 */
677#define OP_CPS_CBRANCH 91 /* a conditional branch */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000678/* OP_CPS_CBRANCH conditionally calls one of two continuations
Eric Biederman90089602004-05-28 14:11:54 +0000679 * RHS(0) holds the branch condition
680 * RHS(x + 1) holds argument x of the function
681 * TARG(0) holds the OP_CPS_START to jump to when true
682 * ->next holds the OP_CPS_START to jump to when false
683 */
684#define OP_CPS_CALL 92 /* an uncontional branch that will return */
685/* For OP_CPS_CALL instructions
686 * RHS(x) holds argument x of the function
687 * MISC(0) holds the OP_CPS_RET that returns from the branch
688 * TARG(0) holds the branch target.
689 * ->next holds where the OP_CPS_RET will return to.
690 */
691#define OP_CPS_RET 93
Stefan Reinauer14e22772010-04-27 06:56:47 +0000692/* OP_CPS_RET conditionally calls one of two continuations
Eric Biederman90089602004-05-28 14:11:54 +0000693 * RHS(0) holds the variable with the return function address
694 * RHS(x + 1) holds argument x of the function
695 * The branch target may be any OP_CPS_START
696 */
697#define OP_CPS_END 94
698/* OP_CPS_END is the triple at the end of the program.
699 * For most practical purposes it is a branch.
700 */
701#define OP_CPS_START 95
702/* OP_CPS_START is a triple at the start of a continuation
703 * The arguments variables takes N registers to contain.
704 * LHS(0..N-1) refer to an OP_PIECE triple that represents
705 * the Xth register that the arguments are stored in.
706 */
707#endif
708
Eric Biedermanb138ac82003-04-22 18:44:01 +0000709/* Architecture specific instructions */
710#define OP_CMP 100
711#define OP_TEST 101
712#define OP_SET_EQ 102
713#define OP_SET_NOTEQ 103
714#define OP_SET_SLESS 104
715#define OP_SET_ULESS 105
716#define OP_SET_SMORE 106
717#define OP_SET_UMORE 107
718#define OP_SET_SLESSEQ 108
719#define OP_SET_ULESSEQ 109
720#define OP_SET_SMOREEQ 110
721#define OP_SET_UMOREEQ 111
722
723#define OP_JMP 112
724#define OP_JMP_EQ 113
725#define OP_JMP_NOTEQ 114
726#define OP_JMP_SLESS 115
727#define OP_JMP_ULESS 116
728#define OP_JMP_SMORE 117
729#define OP_JMP_UMORE 118
730#define OP_JMP_SLESSEQ 119
731#define OP_JMP_ULESSEQ 120
732#define OP_JMP_SMOREEQ 121
733#define OP_JMP_UMOREEQ 122
734
735/* Builtin operators that it is just simpler to use the compiler for */
736#define OP_INB 130
737#define OP_INW 131
738#define OP_INL 132
739#define OP_OUTB 133
740#define OP_OUTW 134
741#define OP_OUTL 135
742#define OP_BSF 136
743#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000744#define OP_RDMSR 138
745#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000746#define OP_HLT 140
747
Eric Biederman0babc1c2003-05-09 02:39:00 +0000748struct op_info {
749 const char *name;
750 unsigned flags;
Eric Biederman90089602004-05-28 14:11:54 +0000751#define PURE 0x001 /* Triple has no side effects */
752#define IMPURE 0x002 /* Triple has side effects */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000753#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
Eric Biederman90089602004-05-28 14:11:54 +0000754#define DEF 0x004 /* Triple is a variable definition */
755#define BLOCK 0x008 /* Triple stores the current block */
756#define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */
757#define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 )
758#define UBRANCH 0x020 /* Triple is an unconditional branch instruction */
759#define CBRANCH 0x040 /* Triple is a conditional branch instruction */
760#define RETBRANCH 0x060 /* Triple is a return instruction */
761#define CALLBRANCH 0x080 /* Triple is a call instruction */
762#define ENDBRANCH 0x0a0 /* Triple is an end instruction */
763#define PART 0x100 /* Triple is really part of another triple */
764#define BITFIELD 0x200 /* Triple manipulates a bitfield */
765 signed char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000766};
767
Eric Biederman0babc1c2003-05-09 02:39:00 +0000768#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
769 .name = (NAME), \
770 .flags = (FLAGS), \
771 .lhs = (LHS), \
772 .rhs = (RHS), \
773 .misc = (MISC), \
774 .targ = (TARG), \
775 }
776static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000777[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
778[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000779[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
780[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
781[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
782[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
783[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
784[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
785[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
786[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
787[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
788[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
789[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
790[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
791[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
792[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
793[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
794[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
795[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000796
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000797[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
798[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
799[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
800[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
801[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
802[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
803[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
804[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
805[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
806[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
807[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
808[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000809
Eric Biederman90089602004-05-28 14:11:54 +0000810[OP_LOAD ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "load"),
811[OP_STORE ] = OP( 0, 2, 0, 0, PURE | BLOCK , "store"),
812
813[OP_UEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "uextract"),
814[OP_SEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "sextract"),
815[OP_DEPOSIT ] = OP( 0, 2, 0, 0, PURE | DEF | BITFIELD, "deposit"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000816
Eric Biederman83b991a2003-10-11 06:20:25 +0000817[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000818
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000819[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000820[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000821[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman90089602004-05-28 14:11:54 +0000822[OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000823
Stefan Reinauer50542a82007-10-24 11:14:14 +0000824#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +0000825#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 +0000826#endif
Eric Biederman90089602004-05-28 14:11:54 +0000827[OP_WRITE ] = OP( 0, 1, 1, 0, PURE | DEF | BLOCK, "write"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000828[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
829[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
Eric Biederman90089602004-05-28 14:11:54 +0000830[OP_CONVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "convert"),
831[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"),
832[OP_ASM ] = OP(-1, -1, 0, 0, PURE, "asm"),
Stefan Reinauer14e22772010-04-27 06:56:47 +0000833[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
Eric Biederman90089602004-05-28 14:11:54 +0000834[OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"),
835[OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000836
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000837[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
Eric Biederman90089602004-05-28 14:11:54 +0000838[OP_TUPLE ] = OP(-1, 0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"),
839[OP_BITREF ] = OP( 0, 1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000840/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman90089602004-05-28 14:11:54 +0000841[OP_FCALL ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"),
842[OP_PROG ] = OP( 0, 1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"),
843/* The sizes of OP_FCALL depends upon context */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000844
Eric Biederman83b991a2003-10-11 06:20:25 +0000845[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
Eric Biederman90089602004-05-28 14:11:54 +0000846[OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "branch"),
847[OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"),
848[OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"),
849[OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000850[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
851[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
852[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000853/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000854[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000855
Eric Biederman90089602004-05-28 14:11:54 +0000856#if 0
857[OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH, "cps_branch"),
858[OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH, "cps_cbranch"),
859[OP_CPS_CALL ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH, "cps_call"),
860[OP_CPS_RET ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH, "cps_ret"),
861[OP_CPS_END ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"),
862[OP_CPS_START ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "cps_start"),
863#endif
864
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000865[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
866[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
867[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
868[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
869[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
870[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
871[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
872[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
873[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
874[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
875[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
876[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
Eric Biederman90089602004-05-28 14:11:54 +0000877[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"),
878[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"),
879[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"),
880[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"),
881[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"),
882[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"),
883[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"),
884[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"),
885[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"),
886[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"),
887[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000888
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000889[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
890[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
891[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
892[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
893[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
894[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
895[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
896[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
897[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
898[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
899[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000900};
901#undef OP
902#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000903
Stefan Reinauer14e22772010-04-27 06:56:47 +0000904static const char *tops(int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000905{
906 static const char unknown[] = "unknown op";
907 if (index < 0) {
908 return unknown;
909 }
Patrick Georgi0dde01c2013-11-11 15:57:09 +0100910 if (index >= OP_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000911 return unknown;
912 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000913 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000914}
915
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000916struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000917struct triple;
918struct block;
919struct triple_set {
920 struct triple_set *next;
921 struct triple *member;
922};
923
Eric Biederman90089602004-05-28 14:11:54 +0000924#define MAX_LHS 63
925#define MAX_RHS 127
Eric Biederman678d8162003-07-03 03:59:38 +0000926#define MAX_MISC 3
Eric Biederman90089602004-05-28 14:11:54 +0000927#define MAX_TARG 1
Eric Biederman0babc1c2003-05-09 02:39:00 +0000928
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000929struct occurance {
930 int count;
931 const char *filename;
932 const char *function;
933 int line;
934 int col;
935 struct occurance *parent;
936};
Eric Biederman90089602004-05-28 14:11:54 +0000937struct bitfield {
938 ulong_t size : 8;
939 ulong_t offset : 24;
940};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000941struct triple {
942 struct triple *next, *prev;
943 struct triple_set *use;
944 struct type *type;
Eric Biederman90089602004-05-28 14:11:54 +0000945 unsigned int op : 8;
946 unsigned int template_id : 7;
947 unsigned int lhs : 6;
948 unsigned int rhs : 7;
949 unsigned int misc : 2;
950 unsigned int targ : 1;
951#define TRIPLE_SIZE(TRIPLE) \
952 ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ)
953#define TRIPLE_LHS_OFF(PTR) (0)
954#define TRIPLE_RHS_OFF(PTR) (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs)
955#define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs)
956#define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc)
957#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)])
958#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)])
959#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)])
960#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000961 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000962#define TRIPLE_FLAG_FLATTENED (1 << 31)
963#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
964#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biederman83b991a2003-10-11 06:20:25 +0000965#define TRIPLE_FLAG_VOLATILE (1 << 28)
Eric Biederman90089602004-05-28 14:11:54 +0000966#define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */
967#define TRIPLE_FLAG_LOCAL (1 << 26)
968
969#define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000970 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000971 union {
972 ulong_t cval;
Eric Biederman90089602004-05-28 14:11:54 +0000973 struct bitfield bitfield;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000974 struct block *block;
975 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000976 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000977 struct asm_info *ainfo;
Eric Biederman90089602004-05-28 14:11:54 +0000978 struct triple *func;
979 struct symbol *symbol;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000980 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000981 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000982};
983
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000984struct reg_info {
985 unsigned reg;
986 unsigned regcm;
987};
988struct ins_template {
989 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
990};
991
992struct asm_info {
993 struct ins_template tmpl;
994 char *str;
995};
996
Eric Biedermanb138ac82003-04-22 18:44:01 +0000997struct block_set {
998 struct block_set *next;
999 struct block *member;
1000};
1001struct block {
1002 struct block *work_next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001003 struct triple *first, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001004 int edge_count;
1005 struct block_set *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001006 int users;
1007 struct block_set *use;
1008 struct block_set *idominates;
1009 struct block_set *domfrontier;
1010 struct block *idom;
1011 struct block_set *ipdominates;
1012 struct block_set *ipdomfrontier;
1013 struct block *ipdom;
1014 int vertex;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001015
Eric Biedermanb138ac82003-04-22 18:44:01 +00001016};
1017
1018struct symbol {
1019 struct symbol *next;
1020 struct hash_entry *ident;
1021 struct triple *def;
1022 struct type *type;
1023 int scope_depth;
1024};
1025
Eric Biederman90089602004-05-28 14:11:54 +00001026struct macro_arg {
1027 struct macro_arg *next;
1028 struct hash_entry *ident;
1029};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001030struct macro {
1031 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00001032 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001033 int buf_len;
Eric Biederman90089602004-05-28 14:11:54 +00001034 struct macro_arg *args;
1035 int argc;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001036};
1037
1038struct hash_entry {
1039 struct hash_entry *next;
1040 const char *name;
1041 int name_len;
1042 int tok;
1043 struct macro *sym_define;
1044 struct symbol *sym_label;
Eric Biederman83b991a2003-10-11 06:20:25 +00001045 struct symbol *sym_tag;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001046 struct symbol *sym_ident;
1047};
1048
1049#define HASH_TABLE_SIZE 2048
1050
Eric Biederman5ade04a2003-10-22 04:03:46 +00001051struct compiler_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +00001052 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001053 const char *ofilename;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001054 unsigned long flags;
1055 unsigned long debug;
1056 unsigned long max_allocation_passes;
Eric Biederman90089602004-05-28 14:11:54 +00001057
1058 size_t include_path_count;
1059 const char **include_paths;
1060
1061 size_t define_count;
1062 const char **defines;
1063
1064 size_t undef_count;
1065 const char **undefs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001066};
1067struct arch_state {
1068 unsigned long features;
1069};
Eric Biederman90089602004-05-28 14:11:54 +00001070struct basic_blocks {
1071 struct triple *func;
1072 struct triple *first;
1073 struct block *first_block, *last_block;
1074 int last_vertex;
1075};
Eric Biedermancb364952004-11-15 10:46:44 +00001076#define MAX_PP_IF_DEPTH 63
Eric Biederman5ade04a2003-10-22 04:03:46 +00001077struct compile_state {
1078 struct compiler_state *compiler;
1079 struct arch_state *arch;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001080 FILE *output;
Eric Biederman90089602004-05-28 14:11:54 +00001081 FILE *errout;
1082 FILE *dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001083 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001084 struct occurance *last_occurance;
1085 const char *function;
Eric Biederman41203d92004-11-08 09:31:09 +00001086 int token_base;
1087 struct token token[6];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001088 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +00001089 struct hash_entry *i_switch;
1090 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001091 struct hash_entry *i_continue;
1092 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +00001093 struct hash_entry *i_default;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001094 struct hash_entry *i_return;
Stefan Reinauerc89c4d42010-02-28 18:37:38 +00001095 struct hash_entry *i_noreturn;
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -07001096 struct hash_entry *i_unused;
Stefan Reinauerec664bc2013-05-09 14:06:04 -07001097 struct hash_entry *i_packed;
Eric Biederman90089602004-05-28 14:11:54 +00001098 /* Additional hash entries for predefined macros */
1099 struct hash_entry *i_defined;
1100 struct hash_entry *i___VA_ARGS__;
1101 struct hash_entry *i___FILE__;
1102 struct hash_entry *i___LINE__;
1103 /* Additional hash entries for predefined identifiers */
1104 struct hash_entry *i___func__;
1105 /* Additional hash entries for attributes */
1106 struct hash_entry *i_noinline;
1107 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001108 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001109 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001110 int if_depth;
1111 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001112 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001113 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001114 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001115 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001116 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001117 struct basic_blocks bb;
1118 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001119};
1120
Eric Biederman0babc1c2003-05-09 02:39:00 +00001121/* visibility global/local */
1122/* static/auto duration */
1123/* typedef, register, inline */
1124#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001125#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001126/* Visibility */
1127#define STOR_GLOBAL 0x0001
1128/* Duration */
1129#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001130/* Definition locality */
1131#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001132/* Storage specifiers */
1133#define STOR_AUTO 0x0000
1134#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001135#define STOR_LOCAL 0x0003
1136#define STOR_EXTERN 0x0007
1137#define STOR_INLINE 0x0008
1138#define STOR_REGISTER 0x0010
1139#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001140
Eric Biederman5ade04a2003-10-22 04:03:46 +00001141#define QUAL_SHIFT 5
1142#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001143#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001144#define QUAL_CONST 0x0020
1145#define QUAL_VOLATILE 0x0040
1146#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001147
1148#define TYPE_SHIFT 8
1149#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001150#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1151#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001152#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1153#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001154#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1155#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001156#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1157#define TYPE_DEFAULT 0x0000
1158#define TYPE_VOID 0x0100
1159#define TYPE_CHAR 0x0200
1160#define TYPE_UCHAR 0x0300
1161#define TYPE_SHORT 0x0400
1162#define TYPE_USHORT 0x0500
1163#define TYPE_INT 0x0600
1164#define TYPE_UINT 0x0700
1165#define TYPE_LONG 0x0800
1166#define TYPE_ULONG 0x0900
1167#define TYPE_LLONG 0x0a00 /* long long */
1168#define TYPE_ULLONG 0x0b00
1169#define TYPE_FLOAT 0x0c00
1170#define TYPE_DOUBLE 0x0d00
1171#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001172
1173/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1174#define TYPE_ENUM 0x1600
1175#define TYPE_LIST 0x1700
1176/* TYPE_LIST is a basic building block when defining enumerations
1177 * type->field_ident holds the name of this enumeration entry.
1178 * type->right holds the entry in the list.
1179 */
1180
Eric Biederman0babc1c2003-05-09 02:39:00 +00001181#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001182/* For TYPE_STRUCT
1183 * type->left holds the link list of TYPE_PRODUCT entries that
1184 * make up the structure.
1185 * type->elements hold the length of the linked list
1186 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001187#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001188/* For TYPE_UNION
1189 * type->left holds the link list of TYPE_OVERLAP entries that
1190 * make up the union.
1191 * type->elements hold the length of the linked list
1192 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001193#define TYPE_POINTER 0x1200
Eric Biederman0babc1c2003-05-09 02:39:00 +00001194/* For TYPE_POINTER:
1195 * type->left holds the type pointed to.
1196 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001197#define TYPE_FUNCTION 0x1300
Eric Biederman0babc1c2003-05-09 02:39:00 +00001198/* For TYPE_FUNCTION:
1199 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001200 * type->right holds the type of the arguments
1201 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001202 */
1203#define TYPE_PRODUCT 0x1400
1204/* TYPE_PRODUCT is a basic building block when defining structures
1205 * type->left holds the type that appears first in memory.
1206 * type->right holds the type that appears next in memory.
1207 */
1208#define TYPE_OVERLAP 0x1500
1209/* TYPE_OVERLAP is a basic building block when defining unions
1210 * type->left and type->right holds to types that overlap
1211 * each other in memory.
1212 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001213#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001214/* TYPE_ARRAY is a basic building block when definitng arrays.
1215 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001216 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001217 */
Eric Biederman90089602004-05-28 14:11:54 +00001218#define TYPE_TUPLE 0x1900
Stefan Reinauer14e22772010-04-27 06:56:47 +00001219/* TYPE_TUPLE is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001220 * positionally reference type conglomerations. (i.e. closures)
1221 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1222 * except it has no field names.
1223 * type->left holds the liked list of TYPE_PRODUCT entries that
1224 * make up the closure type.
1225 * type->elements hold the number of elements in the closure.
1226 */
1227#define TYPE_JOIN 0x1a00
Stefan Reinauer14e22772010-04-27 06:56:47 +00001228/* TYPE_JOIN is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001229 * positionally reference type conglomerations. (i.e. closures)
1230 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1231 * except it has no field names.
1232 * type->left holds the liked list of TYPE_OVERLAP entries that
1233 * make up the closure type.
1234 * type->elements hold the number of elements in the closure.
1235 */
1236#define TYPE_BITFIELD 0x1b00
1237/* TYPE_BITFIED is the type of a bitfield.
1238 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1239 * type->elements holds the number of bits in the bitfield.
1240 */
1241#define TYPE_UNKNOWN 0x1c00
1242/* TYPE_UNKNOWN is the type of an unknown value.
1243 * Used on unknown consts and other places where I don't know the type.
1244 */
1245
1246#define ATTRIB_SHIFT 16
1247#define ATTRIB_MASK 0xffff0000
1248#define ATTRIB_NOINLINE 0x00010000
1249#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001250
Eric Biederman83b991a2003-10-11 06:20:25 +00001251#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001252
1253struct type {
1254 unsigned int type;
1255 struct type *left, *right;
1256 ulong_t elements;
1257 struct hash_entry *field_ident;
1258 struct hash_entry *type_ident;
1259};
1260
Eric Biederman530b5192003-07-01 10:05:30 +00001261#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001262#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001263#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001264#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001265#define MAX_REGISTERS 75
1266#define REGISTER_BITS 7
1267#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001268#define REG_ERROR 0
1269#define REG_UNSET 1
1270#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001271#define REG_VIRT0 (MAX_REGISTERS + 0)
1272#define REG_VIRT1 (MAX_REGISTERS + 1)
1273#define REG_VIRT2 (MAX_REGISTERS + 2)
1274#define REG_VIRT3 (MAX_REGISTERS + 3)
1275#define REG_VIRT4 (MAX_REGISTERS + 4)
1276#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001277#define REG_VIRT6 (MAX_REGISTERS + 6)
1278#define REG_VIRT7 (MAX_REGISTERS + 7)
1279#define REG_VIRT8 (MAX_REGISTERS + 8)
1280#define REG_VIRT9 (MAX_REGISTERS + 9)
1281
1282#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1283#error "MAX_VIRT_REGISTERS to small"
1284#endif
Eric Biederman90089602004-05-28 14:11:54 +00001285#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001286#error "Too many id bits used"
1287#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001288
1289/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001290#define REG_SHIFT 0
1291#define REGC_SHIFT REGISTER_BITS
1292#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001293#define REG_MASK (MAX_VIRT_REGISTERS -1)
1294#define ID_REG(ID) ((ID) & REG_MASK)
1295#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001296#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1297#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1298#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1299 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001300
Eric Biederman90089602004-05-28 14:11:54 +00001301#define ARCH_INPUT_REGS 4
1302#define ARCH_OUTPUT_REGS 4
1303
1304static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1305static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001306static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001307static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001308static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001309static void arch_reg_equivs(
1310 struct compile_state *state, unsigned *equiv, int reg);
1311static int arch_select_free_register(
1312 struct compile_state *state, char *used, int classes);
1313static unsigned arch_regc_size(struct compile_state *state, int class);
1314static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1315static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1316static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001317static struct reg_info arch_reg_constraint(
1318 struct compile_state *state, struct type *type, const char *constraint);
1319static struct reg_info arch_reg_clobber(
1320 struct compile_state *state, const char *clobber);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001321static struct reg_info arch_reg_lhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001322 struct triple *ins, int index);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001323static struct reg_info arch_reg_rhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001324 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001325static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001326static struct triple *transform_to_arch_instruction(
1327 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001328static struct triple *flatten(
1329 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001330static void print_dominators(struct compile_state *state,
1331 FILE *fp, struct basic_blocks *bb);
1332static void print_dominance_frontiers(struct compile_state *state,
1333 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001334
1335
Eric Biedermanb138ac82003-04-22 18:44:01 +00001336
Eric Biederman5ade04a2003-10-22 04:03:46 +00001337#define DEBUG_ABORT_ON_ERROR 0x00000001
1338#define DEBUG_BASIC_BLOCKS 0x00000002
1339#define DEBUG_FDOMINATORS 0x00000004
1340#define DEBUG_RDOMINATORS 0x00000008
1341#define DEBUG_TRIPLES 0x00000010
1342#define DEBUG_INTERFERENCE 0x00000020
1343#define DEBUG_SCC_TRANSFORM 0x00000040
1344#define DEBUG_SCC_TRANSFORM2 0x00000080
1345#define DEBUG_REBUILD_SSA_FORM 0x00000100
1346#define DEBUG_INLINE 0x00000200
1347#define DEBUG_RANGE_CONFLICTS 0x00000400
1348#define DEBUG_RANGE_CONFLICTS2 0x00000800
1349#define DEBUG_COLOR_GRAPH 0x00001000
1350#define DEBUG_COLOR_GRAPH2 0x00002000
1351#define DEBUG_COALESCING 0x00004000
1352#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001353#define DEBUG_VERIFICATION 0x00010000
1354#define DEBUG_CALLS 0x00020000
1355#define DEBUG_CALLS2 0x00040000
1356#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001357
1358#define DEBUG_DEFAULT ( \
1359 DEBUG_ABORT_ON_ERROR | \
1360 DEBUG_BASIC_BLOCKS | \
1361 DEBUG_FDOMINATORS | \
1362 DEBUG_RDOMINATORS | \
1363 DEBUG_TRIPLES | \
1364 0 )
1365
Eric Biederman90089602004-05-28 14:11:54 +00001366#define DEBUG_ALL ( \
1367 DEBUG_ABORT_ON_ERROR | \
1368 DEBUG_BASIC_BLOCKS | \
1369 DEBUG_FDOMINATORS | \
1370 DEBUG_RDOMINATORS | \
1371 DEBUG_TRIPLES | \
1372 DEBUG_INTERFERENCE | \
1373 DEBUG_SCC_TRANSFORM | \
1374 DEBUG_SCC_TRANSFORM2 | \
1375 DEBUG_REBUILD_SSA_FORM | \
1376 DEBUG_INLINE | \
1377 DEBUG_RANGE_CONFLICTS | \
1378 DEBUG_RANGE_CONFLICTS2 | \
1379 DEBUG_COLOR_GRAPH | \
1380 DEBUG_COLOR_GRAPH2 | \
1381 DEBUG_COALESCING | \
1382 DEBUG_COALESCING2 | \
1383 DEBUG_VERIFICATION | \
1384 DEBUG_CALLS | \
1385 DEBUG_CALLS2 | \
1386 DEBUG_TOKENS | \
1387 0 )
1388
1389#define COMPILER_INLINE_MASK 0x00000007
1390#define COMPILER_INLINE_ALWAYS 0x00000000
1391#define COMPILER_INLINE_NEVER 0x00000001
1392#define COMPILER_INLINE_DEFAULTON 0x00000002
1393#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1394#define COMPILER_INLINE_NOPENALTY 0x00000004
1395#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1396#define COMPILER_SIMPLIFY 0x00000010
1397#define COMPILER_SCC_TRANSFORM 0x00000020
1398#define COMPILER_SIMPLIFY_OP 0x00000040
1399#define COMPILER_SIMPLIFY_PHI 0x00000080
1400#define COMPILER_SIMPLIFY_LABEL 0x00000100
1401#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1402#define COMPILER_SIMPLIFY_COPY 0x00000400
1403#define COMPILER_SIMPLIFY_ARITH 0x00000800
1404#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1405#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1406#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1407#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1408
Eric Biedermancb364952004-11-15 10:46:44 +00001409#define COMPILER_TRIGRAPHS 0x40000000
1410#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001411
1412#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001413 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001414 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001415 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001416 COMPILER_SIMPLIFY_OP | \
1417 COMPILER_SIMPLIFY_PHI | \
1418 COMPILER_SIMPLIFY_LABEL | \
1419 COMPILER_SIMPLIFY_BRANCH | \
1420 COMPILER_SIMPLIFY_COPY | \
1421 COMPILER_SIMPLIFY_ARITH | \
1422 COMPILER_SIMPLIFY_SHIFT | \
1423 COMPILER_SIMPLIFY_BITWISE | \
1424 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001425 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001426 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001427
Eric Biederman153ea352003-06-20 14:43:20 +00001428#define GLOBAL_SCOPE_DEPTH 1
1429#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001430
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001431static void compile_file(struct compile_state *old_state, const char *filename, int local);
1432
Eric Biederman5ade04a2003-10-22 04:03:46 +00001433
1434
1435static void init_compiler_state(struct compiler_state *compiler)
1436{
1437 memset(compiler, 0, sizeof(*compiler));
1438 compiler->label_prefix = "";
1439 compiler->ofilename = "auto.inc";
1440 compiler->flags = COMPILER_DEFAULT_FLAGS;
1441 compiler->debug = 0;
1442 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001443 compiler->include_path_count = 1;
1444 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1445 compiler->define_count = 1;
1446 compiler->defines = xcmalloc(sizeof(char *), "defines");
1447 compiler->undef_count = 1;
1448 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001449}
1450
1451struct compiler_flag {
1452 const char *name;
1453 unsigned long flag;
1454};
Eric Biederman90089602004-05-28 14:11:54 +00001455
1456struct compiler_arg {
1457 const char *name;
1458 unsigned long mask;
1459 struct compiler_flag flags[16];
1460};
1461
Eric Biederman5ade04a2003-10-22 04:03:46 +00001462static int set_flag(
1463 const struct compiler_flag *ptr, unsigned long *flags,
1464 int act, const char *flag)
1465{
1466 int result = -1;
1467 for(; ptr->name; ptr++) {
1468 if (strcmp(ptr->name, flag) == 0) {
1469 break;
1470 }
1471 }
1472 if (ptr->name) {
1473 result = 0;
1474 *flags &= ~(ptr->flag);
1475 if (act) {
1476 *flags |= ptr->flag;
1477 }
1478 }
1479 return result;
1480}
1481
Eric Biederman90089602004-05-28 14:11:54 +00001482static int set_arg(
1483 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1484{
1485 const char *val;
1486 int result = -1;
1487 int len;
1488 val = strchr(arg, '=');
1489 if (val) {
1490 len = val - arg;
1491 val++;
1492 for(; ptr->name; ptr++) {
1493 if (strncmp(ptr->name, arg, len) == 0) {
1494 break;
1495 }
1496 }
1497 if (ptr->name) {
1498 *flags &= ~ptr->mask;
1499 result = set_flag(&ptr->flags[0], flags, 1, val);
1500 }
1501 }
1502 return result;
1503}
Eric Biederman90089602004-05-28 14:11:54 +00001504
Stefan Reinauer14e22772010-04-27 06:56:47 +00001505
1506static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001507 const char *prefix, const char *invert_prefix)
1508{
1509 for(;ptr->name; ptr++) {
1510 fprintf(fp, "%s%s\n", prefix, ptr->name);
1511 if (invert_prefix) {
1512 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1513 }
1514 }
1515}
1516
1517static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1518 const char *prefix)
1519{
1520 for(;ptr->name; ptr++) {
1521 const struct compiler_flag *flag;
1522 for(flag = &ptr->flags[0]; flag->name; flag++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001523 fprintf(fp, "%s%s=%s\n",
Eric Biederman90089602004-05-28 14:11:54 +00001524 prefix, ptr->name, flag->name);
1525 }
1526 }
1527}
1528
1529static int append_string(size_t *max, const char ***vec, const char *str,
1530 const char *name)
1531{
1532 size_t count;
1533 count = ++(*max);
1534 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1535 (*vec)[count -1] = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001536 (*vec)[count -2] = str;
Eric Biederman90089602004-05-28 14:11:54 +00001537 return 0;
1538}
1539
1540static void arg_error(char *fmt, ...);
1541static const char *identifier(const char *str, const char *end);
1542
1543static int append_include_path(struct compiler_state *compiler, const char *str)
1544{
1545 int result;
1546 if (!exists(str, ".")) {
1547 arg_error("Nonexistent include path: `%s'\n",
1548 str);
1549 }
1550 result = append_string(&compiler->include_path_count,
1551 &compiler->include_paths, str, "include_paths");
1552 return result;
1553}
1554
1555static int append_define(struct compiler_state *compiler, const char *str)
1556{
1557 const char *end, *rest;
1558 int result;
1559
1560 end = strchr(str, '=');
1561 if (!end) {
1562 end = str + strlen(str);
1563 }
1564 rest = identifier(str, end);
1565 if (rest != end) {
1566 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001567 arg_error("Invalid name cannot define macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001568 len, len, str);
1569 }
1570 result = append_string(&compiler->define_count,
1571 &compiler->defines, str, "defines");
1572 return result;
1573}
1574
1575static int append_undef(struct compiler_state *compiler, const char *str)
1576{
1577 const char *end, *rest;
1578 int result;
1579
1580 end = str + strlen(str);
1581 rest = identifier(str, end);
1582 if (rest != end) {
1583 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001584 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001585 len, len, str);
1586 }
1587 result = append_string(&compiler->undef_count,
1588 &compiler->undefs, str, "undefs");
1589 return result;
1590}
1591
1592static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001593 { "trigraphs", COMPILER_TRIGRAPHS },
1594 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001595 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1596 { "simplify", COMPILER_SIMPLIFY },
1597 { "scc-transform", COMPILER_SCC_TRANSFORM },
1598 { "simplify-op", COMPILER_SIMPLIFY_OP },
1599 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1600 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1601 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1602 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1603 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1604 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1605 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1606 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1607 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1608 { 0, 0 },
1609};
1610static const struct compiler_arg romcc_args[] = {
1611 { "inline-policy", COMPILER_INLINE_MASK,
1612 {
1613 { "always", COMPILER_INLINE_ALWAYS, },
1614 { "never", COMPILER_INLINE_NEVER, },
1615 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1616 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1617 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1618 { 0, 0 },
1619 },
1620 },
1621 { 0, 0 },
1622};
1623static const struct compiler_flag romcc_opt_flags[] = {
1624 { "-O", COMPILER_SIMPLIFY },
1625 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001626 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001627 { 0, 0, },
1628};
1629static const struct compiler_flag romcc_debug_flags[] = {
1630 { "all", DEBUG_ALL },
1631 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1632 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1633 { "fdominators", DEBUG_FDOMINATORS },
1634 { "rdominators", DEBUG_RDOMINATORS },
1635 { "triples", DEBUG_TRIPLES },
1636 { "interference", DEBUG_INTERFERENCE },
1637 { "scc-transform", DEBUG_SCC_TRANSFORM },
1638 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1639 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1640 { "inline", DEBUG_INLINE },
1641 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1642 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1643 { "color-graph", DEBUG_COLOR_GRAPH },
1644 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1645 { "coalescing", DEBUG_COALESCING },
1646 { "coalescing2", DEBUG_COALESCING2 },
1647 { "verification", DEBUG_VERIFICATION },
1648 { "calls", DEBUG_CALLS },
1649 { "calls2", DEBUG_CALLS2 },
1650 { "tokens", DEBUG_TOKENS },
1651 { 0, 0 },
1652};
1653
Eric Biederman5ade04a2003-10-22 04:03:46 +00001654static int compiler_encode_flag(
1655 struct compiler_state *compiler, const char *flag)
1656{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001657 int act;
1658 int result;
1659
1660 act = 1;
1661 result = -1;
1662 if (strncmp(flag, "no-", 3) == 0) {
1663 flag += 3;
1664 act = 0;
1665 }
1666 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001667 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1668 }
1669 else if (strncmp(flag, "-E", 2) == 0) {
1670 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1671 }
1672 else if (strncmp(flag, "-I", 2) == 0) {
1673 result = append_include_path(compiler, flag + 2);
1674 }
1675 else if (strncmp(flag, "-D", 2) == 0) {
1676 result = append_define(compiler, flag + 2);
1677 }
1678 else if (strncmp(flag, "-U", 2) == 0) {
1679 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001680 }
1681 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1682 result = 0;
1683 compiler->label_prefix = flag + 13;
1684 }
1685 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1686 unsigned long max_passes;
1687 char *end;
1688 max_passes = strtoul(flag + 22, &end, 10);
1689 if (end[0] == '\0') {
1690 result = 0;
1691 compiler->max_allocation_passes = max_passes;
1692 }
1693 }
1694 else if (act && strcmp(flag, "debug") == 0) {
1695 result = 0;
1696 compiler->debug |= DEBUG_DEFAULT;
1697 }
1698 else if (strncmp(flag, "debug-", 6) == 0) {
1699 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001700 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001701 }
1702 else {
Eric Biederman90089602004-05-28 14:11:54 +00001703 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1704 if (result < 0) {
1705 result = set_arg(romcc_args, &compiler->flags, flag);
1706 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001707 }
1708 return result;
1709}
1710
Eric Biederman90089602004-05-28 14:11:54 +00001711static void compiler_usage(FILE *fp)
1712{
1713 flag_usage(fp, romcc_opt_flags, "", 0);
1714 flag_usage(fp, romcc_flags, "-f", "-fno-");
1715 arg_usage(fp, romcc_args, "-f");
1716 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1717 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1718 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1719 fprintf(fp, "-I<include path>\n");
1720 fprintf(fp, "-D<macro>[=defn]\n");
1721 fprintf(fp, "-U<macro>\n");
1722}
1723
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001724static void do_cleanup(struct compile_state *state)
1725{
1726 if (state->output) {
1727 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001728 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001729 state->output = 0;
1730 }
1731 if (state->dbgout) {
1732 fflush(state->dbgout);
1733 }
1734 if (state->errout) {
1735 fflush(state->errout);
1736 }
1737}
1738
1739static struct compile_state *exit_state;
1740static void exit_cleanup(void)
1741{
1742 if (exit_state) {
1743 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001744 }
1745}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001746
1747static int get_col(struct file_state *file)
1748{
1749 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001750 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001751 ptr = file->line_start;
1752 end = file->pos;
1753 for(col = 0; ptr < end; ptr++) {
1754 if (*ptr != '\t') {
1755 col++;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001756 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001757 else {
1758 col = (col & ~7) + 8;
1759 }
1760 }
1761 return col;
1762}
1763
1764static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1765{
1766 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001767 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001768 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001769 for(spot = triple->occurance; spot; spot = spot->parent) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001770 fprintf(fp, "%s:%d.%d: ",
Eric Biederman5ade04a2003-10-22 04:03:46 +00001771 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001772 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001773 return;
1774 }
1775 if (!state->file) {
1776 return;
1777 }
1778 col = get_col(state->file);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001779 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001780 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001781}
1782
Stefan Reinauer14e22772010-04-27 06:56:47 +00001783static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001784 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001785{
Eric Biederman90089602004-05-28 14:11:54 +00001786 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001787 va_list args;
1788 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001789 loc(fp, state, ptr);
1790 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001791 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001792 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001793 }
Eric Biederman90089602004-05-28 14:11:54 +00001794 fprintf(fp, "Internal compiler error: ");
1795 vfprintf(fp, fmt, args);
1796 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001797 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001798 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001799 abort();
1800}
1801
1802
Stefan Reinauer14e22772010-04-27 06:56:47 +00001803static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001804 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001805{
Eric Biederman90089602004-05-28 14:11:54 +00001806 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001807 va_list args;
1808 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001809 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001810 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001811 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001812 }
Eric Biederman90089602004-05-28 14:11:54 +00001813 fprintf(fp, "Internal compiler warning: ");
1814 vfprintf(fp, fmt, args);
1815 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001816 va_end(args);
1817}
1818
1819
1820
Stefan Reinauer14e22772010-04-27 06:56:47 +00001821static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001822 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001823{
Eric Biederman90089602004-05-28 14:11:54 +00001824 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001825 va_list args;
1826 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001827 loc(fp, state, ptr);
1828 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001829 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001830 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001831 }
Eric Biederman90089602004-05-28 14:11:54 +00001832 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001833 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001834 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001835 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001836 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001837 abort();
1838 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001839 exit(1);
1840}
1841
Stefan Reinauer14e22772010-04-27 06:56:47 +00001842static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001843 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001844{
Eric Biederman90089602004-05-28 14:11:54 +00001845 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001846 va_list args;
1847 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001848 loc(fp, state, ptr);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001849 fprintf(fp, "warning: ");
Eric Biederman90089602004-05-28 14:11:54 +00001850 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1851 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1852 }
1853 vfprintf(fp, fmt, args);
1854 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001855 va_end(args);
1856}
1857
Eric Biedermanb138ac82003-04-22 18:44:01 +00001858#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1859
Eric Biederman0babc1c2003-05-09 02:39:00 +00001860static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001861{
1862 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001863 if (op >= OP_MAX) {
1864 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001865 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001866 if (op < 0) {
1867 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001868 }
1869}
1870
Eric Biederman0babc1c2003-05-09 02:39:00 +00001871static void valid_ins(struct compile_state *state, struct triple *ptr)
1872{
1873 valid_op(state, ptr->op);
1874}
1875
Stefan Reinauer50542a82007-10-24 11:14:14 +00001876#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001877static void valid_param_count(struct compile_state *state, struct triple *ins)
1878{
1879 int lhs, rhs, misc, targ;
1880 valid_ins(state, ins);
1881 lhs = table_ops[ins->op].lhs;
1882 rhs = table_ops[ins->op].rhs;
1883 misc = table_ops[ins->op].misc;
1884 targ = table_ops[ins->op].targ;
1885
1886 if ((lhs >= 0) && (ins->lhs != lhs)) {
1887 internal_error(state, ins, "Bad lhs count");
1888 }
1889 if ((rhs >= 0) && (ins->rhs != rhs)) {
1890 internal_error(state, ins, "Bad rhs count");
1891 }
1892 if ((misc >= 0) && (ins->misc != misc)) {
1893 internal_error(state, ins, "Bad misc count");
1894 }
1895 if ((targ >= 0) && (ins->targ != targ)) {
1896 internal_error(state, ins, "Bad targ count");
1897 }
1898}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001899#endif
Eric Biederman90089602004-05-28 14:11:54 +00001900
Eric Biedermanb138ac82003-04-22 18:44:01 +00001901static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001902static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001903static void use_triple(struct triple *used, struct triple *user)
1904{
1905 struct triple_set **ptr, *new;
1906 if (!used)
1907 return;
1908 if (!user)
1909 return;
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001910 ptr = &used->use;
1911 while(*ptr) {
1912 if ((*ptr)->member == user) {
1913 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001914 }
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001915 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001916 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00001917 /* Append new to the head of the list,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001918 * copy_func and rename_block_variables
1919 * depends on this.
1920 */
1921 new = xcmalloc(sizeof(*new), "triple_set");
1922 new->member = user;
1923 new->next = used->use;
1924 used->use = new;
1925}
1926
1927static void unuse_triple(struct triple *used, struct triple *unuser)
1928{
1929 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001930 if (!used) {
1931 return;
1932 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001933 ptr = &used->use;
1934 while(*ptr) {
1935 use = *ptr;
1936 if (use->member == unuser) {
1937 *ptr = use->next;
1938 xfree(use);
1939 }
1940 else {
1941 ptr = &use->next;
1942 }
1943 }
1944}
1945
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001946static void put_occurance(struct occurance *occurance)
1947{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001948 if (occurance) {
1949 occurance->count -= 1;
1950 if (occurance->count <= 0) {
1951 if (occurance->parent) {
1952 put_occurance(occurance->parent);
1953 }
1954 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001955 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001956 }
1957}
1958
1959static void get_occurance(struct occurance *occurance)
1960{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001961 if (occurance) {
1962 occurance->count += 1;
1963 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001964}
1965
1966
1967static struct occurance *new_occurance(struct compile_state *state)
1968{
1969 struct occurance *result, *last;
1970 const char *filename;
1971 const char *function;
1972 int line, col;
1973
1974 function = "";
1975 filename = 0;
1976 line = 0;
1977 col = 0;
1978 if (state->file) {
1979 filename = state->file->report_name;
1980 line = state->file->report_line;
1981 col = get_col(state->file);
1982 }
1983 if (state->function) {
1984 function = state->function;
1985 }
1986 last = state->last_occurance;
1987 if (last &&
1988 (last->col == col) &&
1989 (last->line == line) &&
1990 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001991 ((last->filename == filename) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00001992 (strcmp(last->filename, filename) == 0)))
Eric Biederman83b991a2003-10-11 06:20:25 +00001993 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001994 get_occurance(last);
1995 return last;
1996 }
1997 if (last) {
1998 state->last_occurance = 0;
1999 put_occurance(last);
2000 }
2001 result = xmalloc(sizeof(*result), "occurance");
2002 result->count = 2;
2003 result->filename = filename;
2004 result->function = function;
2005 result->line = line;
2006 result->col = col;
2007 result->parent = 0;
2008 state->last_occurance = result;
2009 return result;
2010}
2011
2012static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002013 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002014{
2015 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002016 if (top->parent) {
2017 internal_error(state, 0, "inlining an already inlined function?");
2018 }
2019 /* If I have a null base treat it that way */
2020 if ((base->parent == 0) &&
2021 (base->col == 0) &&
2022 (base->line == 0) &&
2023 (base->function[0] == '\0') &&
2024 (base->filename[0] == '\0')) {
2025 base = 0;
2026 }
2027 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002028 last = state->last_occurance;
2029 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002030 (last->parent == base) &&
2031 (last->col == top->col) &&
2032 (last->line == top->line) &&
2033 (last->function == top->function) &&
2034 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002035 get_occurance(last);
2036 return last;
2037 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002038 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002039 if (last) {
2040 state->last_occurance = 0;
2041 put_occurance(last);
2042 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002043 /* Generate a new occurance structure */
2044 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002045 result = xmalloc(sizeof(*result), "occurance");
2046 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002047 result->filename = top->filename;
2048 result->function = top->function;
2049 result->line = top->line;
2050 result->col = top->col;
2051 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002052 state->last_occurance = result;
2053 return result;
2054}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002055
2056static struct occurance dummy_occurance = {
2057 .count = 2,
2058 .filename = __FILE__,
2059 .function = "",
2060 .line = __LINE__,
2061 .col = 0,
2062 .parent = 0,
2063};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002064
Eric Biederman90089602004-05-28 14:11:54 +00002065/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002066 * from a triple. Having allows certain sanity checks to pass even
2067 * when the original triple that was pointed to is gone.
2068 */
Eric Biederman90089602004-05-28 14:11:54 +00002069static struct triple unknown_triple = {
2070 .next = &unknown_triple,
2071 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002072 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002073 .op = OP_UNKNOWNVAL,
2074 .lhs = 0,
2075 .rhs = 0,
2076 .misc = 0,
2077 .targ = 0,
2078 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002079 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002080 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002081 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002082 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002083};
2084
Eric Biederman0babc1c2003-05-09 02:39:00 +00002085
Eric Biederman90089602004-05-28 14:11:54 +00002086static size_t registers_of(struct compile_state *state, struct type *type);
2087
Stefan Reinauer14e22772010-04-27 06:56:47 +00002088static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002089 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2090 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002091{
Eric Biederman90089602004-05-28 14:11:54 +00002092 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002093 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002094 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002095 dummy.op = op;
2096 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002097 valid_op(state, op);
2098 lhs = table_ops[op].lhs;
2099 rhs = table_ops[op].rhs;
2100 misc = table_ops[op].misc;
2101 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002102
2103 switch(op) {
2104 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002105 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002106 break;
2107 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002108 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002109 break;
2110 case OP_ADECL:
2111 lhs = registers_of(state, type);
2112 break;
2113 case OP_TUPLE:
2114 lhs = registers_of(state, type);
2115 break;
2116 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002117 rhs = rhs_wanted;
2118 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002119 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002120 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002121 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002122 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002123 }
2124 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002125 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002126 }
2127 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002128 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002129 }
2130 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002131 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002132 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002133
2134 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002135 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002136 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2137
2138 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2139 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002140 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002141 ret->lhs = lhs;
2142 ret->rhs = rhs;
2143 ret->misc = misc;
2144 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002145 ret->type = type;
2146 ret->next = ret;
2147 ret->prev = ret;
2148 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002149 /* A simple sanity check */
2150 if ((ret->op != op) ||
2151 (ret->lhs != lhs) ||
2152 (ret->rhs != rhs) ||
2153 (ret->misc != misc) ||
2154 (ret->targ != targ) ||
2155 (ret->type != type) ||
2156 (ret->next != ret) ||
2157 (ret->prev != ret) ||
2158 (ret->occurance != occurance)) {
2159 internal_error(state, ret, "huh?");
2160 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002161 return ret;
2162}
2163
Eric Biederman0babc1c2003-05-09 02:39:00 +00002164struct triple *dup_triple(struct compile_state *state, struct triple *src)
2165{
2166 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002167 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002168 src_lhs = src->lhs;
2169 src_rhs = src->rhs;
2170 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002171 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002172 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002173 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002174 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002175 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002176 return dup;
2177}
2178
Eric Biederman41203d92004-11-08 09:31:09 +00002179static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2180{
2181 struct triple *copy;
2182 copy = dup_triple(state, src);
2183 copy->use = 0;
2184 copy->next = copy->prev = copy;
2185 return copy;
2186}
2187
Stefan Reinauer14e22772010-04-27 06:56:47 +00002188static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002189 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002190{
2191 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002192 struct occurance *occurance;
2193 occurance = new_occurance(state);
2194 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002195 return ret;
2196}
2197
Stefan Reinauer14e22772010-04-27 06:56:47 +00002198static struct triple *build_triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002199 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002200 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002201{
2202 struct triple *ret;
2203 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002204 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002205 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002206 if (count > 0) {
2207 ret->param[0] = left;
2208 }
2209 if (count > 1) {
2210 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002211 }
2212 return ret;
2213}
2214
Stefan Reinauer14e22772010-04-27 06:56:47 +00002215static struct triple *triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002216 int op, struct type *type, struct triple *left, struct triple *right)
2217{
2218 struct triple *ret;
2219 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002220 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002221 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002222 if (count >= 1) {
2223 ret->param[0] = left;
2224 }
2225 if (count >= 2) {
2226 ret->param[1] = right;
2227 }
2228 return ret;
2229}
2230
Stefan Reinauer14e22772010-04-27 06:56:47 +00002231static struct triple *branch(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002232 struct triple *targ, struct triple *test)
2233{
2234 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002235 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002236 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002237 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002238 } else {
2239 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002240 }
2241 TARG(ret, 0) = targ;
2242 /* record the branch target was used */
2243 if (!targ || (targ->op != OP_LABEL)) {
2244 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002245 }
2246 return ret;
2247}
2248
Eric Biederman90089602004-05-28 14:11:54 +00002249static int triple_is_label(struct compile_state *state, struct triple *ins);
2250static int triple_is_call(struct compile_state *state, struct triple *ins);
2251static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002252static void insert_triple(struct compile_state *state,
2253 struct triple *first, struct triple *ptr)
2254{
2255 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002256 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002257 internal_error(state, ptr, "expression already used");
2258 }
2259 ptr->next = first;
2260 ptr->prev = first->prev;
2261 ptr->prev->next = ptr;
2262 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002263
2264 if (triple_is_cbranch(state, ptr->prev) ||
2265 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002266 unuse_triple(first, ptr->prev);
2267 use_triple(ptr, ptr->prev);
2268 }
2269 }
2270}
2271
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002272static int triple_stores_block(struct compile_state *state, struct triple *ins)
2273{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002274 /* This function is used to determine if u.block
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002275 * is utilized to store the current block number.
2276 */
2277 int stores_block;
2278 valid_ins(state, ins);
2279 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2280 return stores_block;
2281}
2282
Eric Biederman90089602004-05-28 14:11:54 +00002283static int triple_is_branch(struct compile_state *state, struct triple *ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002284static struct block *block_of_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002285 struct triple *ins)
2286{
2287 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002288 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002289 return 0;
2290 }
2291 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002292 while(ins != first && !triple_is_branch(state, ins->prev) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00002293 !triple_stores_block(state, ins))
2294 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002295 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002296 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002297 }
2298 ins = ins->prev;
2299 }
Eric Biederman90089602004-05-28 14:11:54 +00002300 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002301}
2302
Eric Biederman90089602004-05-28 14:11:54 +00002303static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002304static struct triple *pre_triple(struct compile_state *state,
2305 struct triple *base,
2306 int op, struct type *type, struct triple *left, struct triple *right)
2307{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002308 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002309 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002310 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002311 /* If I am an OP_PIECE jump to the real instruction */
2312 if (base->op == OP_PIECE) {
2313 base = MISC(base, 0);
2314 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002315 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002316 get_occurance(base->occurance);
2317 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002318 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002319 if (triple_stores_block(state, ret)) {
2320 ret->u.block = block;
2321 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002322 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002323 for(i = 0; i < ret->lhs; i++) {
2324 struct triple *piece;
2325 piece = LHS(ret, i);
2326 insert_triple(state, base, piece);
2327 use_triple(ret, piece);
2328 use_triple(piece, ret);
2329 }
2330 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002331 block->first = ret;
2332 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002333 return ret;
2334}
2335
2336static struct triple *post_triple(struct compile_state *state,
2337 struct triple *base,
2338 int op, struct type *type, struct triple *left, struct triple *right)
2339{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002340 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002341 struct triple *ret, *next;
2342 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002343 /* If I am an OP_PIECE jump to the real instruction */
2344 if (base->op == OP_PIECE) {
2345 base = MISC(base, 0);
2346 }
2347 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002348 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002349 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002350 base = LHS(base, zlhs - 1);
2351 }
2352
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002353 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002354 get_occurance(base->occurance);
2355 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002356 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002357 if (triple_stores_block(state, ret)) {
2358 ret->u.block = block;
2359 }
Eric Biederman90089602004-05-28 14:11:54 +00002360 next = base->next;
2361 insert_triple(state, next, ret);
2362 zlhs = ret->lhs;
2363 for(i = 0; i < zlhs; i++) {
2364 struct triple *piece;
2365 piece = LHS(ret, i);
2366 insert_triple(state, next, piece);
2367 use_triple(ret, piece);
2368 use_triple(piece, ret);
2369 }
2370 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002371 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002372 if (zlhs) {
2373 block->last = LHS(ret, zlhs - 1);
2374 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002375 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002376 return ret;
2377}
2378
Eric Biederman90089602004-05-28 14:11:54 +00002379static struct type *reg_type(
2380 struct compile_state *state, struct type *type, int reg);
2381
2382static void generate_lhs_piece(
2383 struct compile_state *state, struct triple *ins, int index)
2384{
2385 struct type *piece_type;
2386 struct triple *piece;
2387 get_occurance(ins->occurance);
2388 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2389
2390 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2391 piece_type = piece_type->left;
2392 }
2393#if 0
2394{
2395 static void name_of(FILE *fp, struct type *type);
2396 FILE * fp = state->errout;
2397 fprintf(fp, "piece_type(%d): ", index);
2398 name_of(fp, piece_type);
2399 fprintf(fp, "\n");
2400}
2401#endif
2402 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2403 piece->u.cval = index;
2404 LHS(ins, piece->u.cval) = piece;
2405 MISC(piece, 0) = ins;
2406}
2407
2408static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2409{
2410 int i, zlhs;
2411 zlhs = ins->lhs;
2412 for(i = 0; i < zlhs; i++) {
2413 generate_lhs_piece(state, ins, i);
2414 }
2415}
2416
Eric Biedermanb138ac82003-04-22 18:44:01 +00002417static struct triple *label(struct compile_state *state)
2418{
2419 /* Labels don't get a type */
2420 struct triple *result;
2421 result = triple(state, OP_LABEL, &void_type, 0, 0);
2422 return result;
2423}
2424
Eric Biederman90089602004-05-28 14:11:54 +00002425static struct triple *mkprog(struct compile_state *state, ...)
2426{
2427 struct triple *prog, *head, *arg;
2428 va_list args;
2429 int i;
2430
2431 head = label(state);
2432 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2433 RHS(prog, 0) = head;
2434 va_start(args, state);
2435 i = 0;
2436 while((arg = va_arg(args, struct triple *)) != 0) {
2437 if (++i >= 100) {
2438 internal_error(state, 0, "too many arguments to mkprog");
2439 }
2440 flatten(state, head, arg);
2441 }
2442 va_end(args);
2443 prog->type = head->prev->type;
2444 return prog;
2445}
2446static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002447static void display_triple(FILE *fp, struct triple *ins)
2448{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002449 struct occurance *ptr;
2450 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002451 char pre, post, vol;
2452 pre = post = vol = ' ';
2453 if (ins) {
2454 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2455 pre = '^';
2456 }
2457 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2458 post = ',';
2459 }
2460 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2461 vol = 'v';
2462 }
2463 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002464 }
Eric Biederman90089602004-05-28 14:11:54 +00002465 if (ins == 0) {
2466 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002467 }
Eric Biederman90089602004-05-28 14:11:54 +00002468 else if (ins->op == OP_INTCONST) {
2469 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002470 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002471 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002472 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002473 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002474 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002475 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002476 MISC(ins, 0), (unsigned long)(ins->u.cval));
2477 }
2478 else if (ins->op == OP_INDEX) {
2479 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002480 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002481 RHS(ins, 0), (unsigned long)(ins->u.cval));
2482 }
2483 else if (ins->op == OP_PIECE) {
2484 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002485 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002486 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002487 }
2488 else {
2489 int i, count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002490 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
Eric Biederman90089602004-05-28 14:11:54 +00002491 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2492 if (table_ops[ins->op].flags & BITFIELD) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002493 fprintf(fp, " <%2d-%2d:%2d>",
Eric Biederman90089602004-05-28 14:11:54 +00002494 ins->u.bitfield.offset,
2495 ins->u.bitfield.offset + ins->u.bitfield.size,
2496 ins->u.bitfield.size);
2497 }
2498 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002499 for(i = 0; i < count; i++) {
2500 fprintf(fp, " %-10p", ins->param[i]);
2501 }
2502 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002503 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002504 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002505 }
Eric Biederman90089602004-05-28 14:11:54 +00002506 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002507 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002508#if DEBUG_DISPLAY_TYPES
2509 fprintf(fp, " <");
2510 name_of(fp, ins->type);
2511 fprintf(fp, "> ");
2512#endif
2513#if DEBUG_DISPLAY_USES
2514 fprintf(fp, " [");
2515 for(user = ins->use; user; user = user->next) {
2516 fprintf(fp, " %-10p", user->member);
2517 }
2518 fprintf(fp, " ]");
2519#endif
2520 fprintf(fp, " @");
2521 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2522 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002523 ptr->function,
Eric Biederman90089602004-05-28 14:11:54 +00002524 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002525 ptr->line,
Eric Biederman90089602004-05-28 14:11:54 +00002526 ptr->col);
2527 }
2528 if (ins->op == OP_ASM) {
2529 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002530 }
2531 }
Eric Biederman90089602004-05-28 14:11:54 +00002532 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002533 fflush(fp);
2534}
2535
Eric Biederman90089602004-05-28 14:11:54 +00002536static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002537static void display_triple_changes(
2538 FILE *fp, const struct triple *new, const struct triple *orig)
2539{
2540
2541 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002542 new_count = TRIPLE_SIZE(new);
2543 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002544 if ((new->op != orig->op) ||
2545 (new_count != orig_count) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002546 (memcmp(orig->param, new->param,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002547 orig_count * sizeof(orig->param[0])) != 0) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002548 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
Eric Biederman5ade04a2003-10-22 04:03:46 +00002549 {
2550 struct occurance *ptr;
2551 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002552 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002553 if (orig->op == new->op) {
2554 fprintf(fp, " %-11s", tops(orig->op));
2555 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002556 fprintf(fp, " [%-10s %-10s]",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002557 tops(new->op), tops(orig->op));
2558 }
2559 min_count = new_count;
2560 if (min_count > orig_count) {
2561 min_count = orig_count;
2562 }
2563 for(indent = i = 0; i < min_count; i++) {
2564 if (orig->param[i] == new->param[i]) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002565 fprintf(fp, " %-11p",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002566 orig->param[i]);
2567 indent += 12;
2568 } else {
2569 fprintf(fp, " [%-10p %-10p]",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002570 new->param[i],
Eric Biederman5ade04a2003-10-22 04:03:46 +00002571 orig->param[i]);
2572 indent += 24;
2573 }
2574 }
2575 for(; i < orig_count; i++) {
2576 fprintf(fp, " [%-9p]", orig->param[i]);
2577 indent += 12;
2578 }
2579 for(; i < new_count; i++) {
2580 fprintf(fp, " [%-9p]", new->param[i]);
2581 indent += 12;
2582 }
2583 if ((new->op == OP_INTCONST)||
2584 (new->op == OP_ADDRCONST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002585 fprintf(fp, " <0x%08lx>",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002586 (unsigned long)(new->u.cval));
2587 indent += 13;
2588 }
2589 for(;indent < 36; indent++) {
2590 putc(' ', fp);
2591 }
Eric Biederman90089602004-05-28 14:11:54 +00002592
2593#if DEBUG_DISPLAY_TYPES
2594 fprintf(fp, " <");
2595 name_of(fp, new->type);
2596 if (!equiv_types(new->type, orig->type)) {
2597 fprintf(fp, " -- ");
2598 name_of(fp, orig->type);
2599 }
2600 fprintf(fp, "> ");
2601#endif
2602
Eric Biederman5ade04a2003-10-22 04:03:46 +00002603 fprintf(fp, " @");
2604 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2605 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002606 ptr->function,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002607 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002608 ptr->line,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002609 ptr->col);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002610
Eric Biederman5ade04a2003-10-22 04:03:46 +00002611 }
2612 fprintf(fp, "\n");
2613 fflush(fp);
2614 }
2615}
2616
Eric Biederman83b991a2003-10-11 06:20:25 +00002617static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002618{
2619 /* Does the triple have no side effects.
Stefan Reinauer14e22772010-04-27 06:56:47 +00002620 * I.e. Rexecuting the triple with the same arguments
Eric Biedermanb138ac82003-04-22 18:44:01 +00002621 * gives the same value.
2622 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002623 unsigned pure;
2624 valid_ins(state, ins);
2625 pure = PURE_BITS(table_ops[ins->op].flags);
2626 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002627 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002628 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002629 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002630 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002631}
2632
Stefan Reinauer14e22772010-04-27 06:56:47 +00002633static int triple_is_branch_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002634 struct triple *ins, unsigned type)
2635{
2636 /* Is this one of the passed branch types? */
2637 valid_ins(state, ins);
2638 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2639}
2640
Eric Biederman0babc1c2003-05-09 02:39:00 +00002641static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002642{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002643 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002644 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002645 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002646}
2647
Eric Biederman90089602004-05-28 14:11:54 +00002648static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002649{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002650 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002651 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002652}
2653
Eric Biederman90089602004-05-28 14:11:54 +00002654static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002655{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002656 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002657 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002658 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002659 type = BRANCH_BITS(table_ops[ins->op].flags);
2660 return (type != 0) && (type != CBRANCH);
2661}
2662
2663static int triple_is_call(struct compile_state *state, struct triple *ins)
2664{
2665 /* Is this triple a call instruction? */
2666 return triple_is_branch_type(state, ins, CALLBRANCH);
2667}
2668
2669static int triple_is_ret(struct compile_state *state, struct triple *ins)
2670{
2671 /* Is this triple a return instruction? */
2672 return triple_is_branch_type(state, ins, RETBRANCH);
2673}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002674
Stefan Reinauer50542a82007-10-24 11:14:14 +00002675#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002676static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2677{
2678 /* Is this triple an unconditional branch and not a call or a
2679 * return? */
2680 return triple_is_branch_type(state, ins, UBRANCH);
2681}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002682#endif
Eric Biederman90089602004-05-28 14:11:54 +00002683
2684static int triple_is_end(struct compile_state *state, struct triple *ins)
2685{
2686 return triple_is_branch_type(state, ins, ENDBRANCH);
2687}
2688
2689static int triple_is_label(struct compile_state *state, struct triple *ins)
2690{
2691 valid_ins(state, ins);
2692 return (ins->op == OP_LABEL);
2693}
2694
2695static struct triple *triple_to_block_start(
2696 struct compile_state *state, struct triple *start)
2697{
2698 while(!triple_is_branch(state, start->prev) &&
2699 (!triple_is_label(state, start) || !start->use)) {
2700 start = start->prev;
2701 }
2702 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002703}
2704
Eric Biederman0babc1c2003-05-09 02:39:00 +00002705static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002706{
2707 /* This function is used to determine which triples need
2708 * a register.
2709 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002710 int is_def;
2711 valid_ins(state, ins);
2712 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002713 if (ins->lhs >= 1) {
2714 is_def = 0;
2715 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002716 return is_def;
2717}
2718
Eric Biederman83b991a2003-10-11 06:20:25 +00002719static int triple_is_structural(struct compile_state *state, struct triple *ins)
2720{
2721 int is_structural;
2722 valid_ins(state, ins);
2723 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2724 return is_structural;
2725}
2726
Eric Biederman90089602004-05-28 14:11:54 +00002727static int triple_is_part(struct compile_state *state, struct triple *ins)
2728{
2729 int is_part;
2730 valid_ins(state, ins);
2731 is_part = (table_ops[ins->op].flags & PART) == PART;
2732 return is_part;
2733}
2734
2735static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2736{
2737 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2738}
2739
Eric Biederman0babc1c2003-05-09 02:39:00 +00002740static struct triple **triple_iter(struct compile_state *state,
2741 size_t count, struct triple **vector,
2742 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002743{
2744 struct triple **ret;
2745 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002746 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002747 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002748 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002749 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002750 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002751 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002752 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002753 }
2754 return ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002755
Eric Biedermanb138ac82003-04-22 18:44:01 +00002756}
2757
2758static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002759 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002760{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002761 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002762 ins, last);
2763}
2764
2765static struct triple **triple_rhs(struct compile_state *state,
2766 struct triple *ins, struct triple **last)
2767{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002768 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002769 ins, last);
2770}
2771
2772static struct triple **triple_misc(struct compile_state *state,
2773 struct triple *ins, struct triple **last)
2774{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002775 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002776 ins, last);
2777}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002778
Eric Biederman90089602004-05-28 14:11:54 +00002779static struct triple **do_triple_targ(struct compile_state *state,
2780 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002781{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002782 size_t count;
2783 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002784 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002785 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002786 count = ins->targ;
2787 next_is_targ = 0;
2788 if (triple_is_cbranch(state, ins)) {
2789 next_is_targ = 1;
2790 }
2791 if (!call_edges && triple_is_call(state, ins)) {
2792 count = 0;
2793 }
2794 if (next_edges && triple_is_call(state, ins)) {
2795 next_is_targ = 1;
2796 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002797 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002798 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002799 if (!last) {
2800 ret = &ins->next;
2801 } else if (last == &ins->next) {
2802 last = 0;
2803 }
2804 }
2805 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002806 if (!last) {
2807 ret = vector;
2808 }
2809 else if ((last >= vector) && (last < (vector + count - 1))) {
2810 ret = last + 1;
2811 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002812 else if (last == vector + count - 1) {
2813 last = 0;
2814 }
2815 }
Eric Biederman90089602004-05-28 14:11:54 +00002816 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002817 struct triple_set *use;
2818 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002819 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002820 continue;
2821 }
2822 if (!last) {
2823 ret = &use->member->next;
2824 break;
2825 }
2826 else if (last == &use->member->next) {
2827 last = 0;
2828 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002829 }
2830 }
2831 return ret;
2832}
2833
Eric Biederman90089602004-05-28 14:11:54 +00002834static struct triple **triple_targ(struct compile_state *state,
2835 struct triple *ins, struct triple **last)
2836{
2837 return do_triple_targ(state, ins, last, 1, 1);
2838}
2839
2840static struct triple **triple_edge_targ(struct compile_state *state,
2841 struct triple *ins, struct triple **last)
2842{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002843 return do_triple_targ(state, ins, last,
Eric Biederman90089602004-05-28 14:11:54 +00002844 state->functions_joined, !state->functions_joined);
2845}
2846
2847static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2848{
2849 struct triple *next;
2850 int lhs, i;
2851 lhs = ins->lhs;
2852 next = ins->next;
2853 for(i = 0; i < lhs; i++) {
2854 struct triple *piece;
2855 piece = LHS(ins, i);
2856 if (next != piece) {
2857 internal_error(state, ins, "malformed lhs on %s",
2858 tops(ins->op));
2859 }
2860 if (next->op != OP_PIECE) {
2861 internal_error(state, ins, "bad lhs op %s at %d on %s",
2862 tops(next->op), i, tops(ins->op));
2863 }
2864 if (next->u.cval != i) {
2865 internal_error(state, ins, "bad u.cval of %d %d expected",
2866 next->u.cval, i);
2867 }
2868 next = next->next;
2869 }
2870 return next;
2871}
2872
2873/* Function piece accessor functions */
Stefan Reinauer14e22772010-04-27 06:56:47 +00002874static struct triple *do_farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002875 struct triple *func, unsigned index)
2876{
2877 struct type *ftype;
2878 struct triple *first, *arg;
2879 unsigned i;
2880
2881 ftype = func->type;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +11002882 if(index >= (ftype->elements + 2)) {
Eric Biederman90089602004-05-28 14:11:54 +00002883 internal_error(state, func, "bad argument index: %d", index);
2884 }
2885 first = RHS(func, 0);
2886 arg = first->next;
2887 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2888 /* do nothing */
2889 }
2890 if (arg->op != OP_ADECL) {
2891 internal_error(state, 0, "arg not adecl?");
2892 }
2893 return arg;
2894}
2895static struct triple *fresult(struct compile_state *state, struct triple *func)
2896{
2897 return do_farg(state, func, 0);
2898}
2899static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2900{
2901 return do_farg(state, func, 1);
2902}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002903static struct triple *farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002904 struct triple *func, unsigned index)
2905{
2906 return do_farg(state, func, index + 2);
2907}
2908
2909
2910static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2911{
2912 struct triple *first, *ins;
2913 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2914 first = ins = RHS(func, 0);
2915 do {
2916 if (triple_is_label(state, ins) && ins->use) {
2917 fprintf(fp, "%p:\n", ins);
2918 }
2919 display_triple(fp, ins);
2920
2921 if (triple_is_branch(state, ins)) {
2922 fprintf(fp, "\n");
2923 }
2924 if (ins->next->prev != ins) {
2925 internal_error(state, ins->next, "bad prev");
2926 }
2927 ins = ins->next;
2928 } while(ins != first);
2929}
2930
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002931static void verify_use(struct compile_state *state,
2932 struct triple *user, struct triple *used)
2933{
2934 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002935 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002936 for(i = 0; i < size; i++) {
2937 if (user->param[i] == used) {
2938 break;
2939 }
2940 }
2941 if (triple_is_branch(state, user)) {
2942 if (user->next == used) {
2943 i = -1;
2944 }
2945 }
2946 if (i == size) {
2947 internal_error(state, user, "%s(%p) does not use %s(%p)",
2948 tops(user->op), user, tops(used->op), used);
2949 }
2950}
2951
Stefan Reinauer14e22772010-04-27 06:56:47 +00002952static int find_rhs_use(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002953 struct triple *user, struct triple *used)
2954{
2955 struct triple **param;
2956 int size, i;
2957 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002958
2959#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002960#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002961#endif
Eric Biederman90089602004-05-28 14:11:54 +00002962 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002963 param = &RHS(user, 0);
2964 for(i = 0; i < size; i++) {
2965 if (param[i] == used) {
2966 return i;
2967 }
2968 }
2969 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002970}
2971
2972static void free_triple(struct compile_state *state, struct triple *ptr)
2973{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002974 size_t size;
2975 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002976 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002977 ptr->prev->next = ptr->next;
2978 ptr->next->prev = ptr->prev;
2979 if (ptr->use) {
2980 internal_error(state, ptr, "ptr->use != 0");
2981 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002982 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002983 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002984 xfree(ptr);
2985}
2986
2987static void release_triple(struct compile_state *state, struct triple *ptr)
2988{
2989 struct triple_set *set, *next;
2990 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002991 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002992 if (ptr == &unknown_triple) {
2993 return;
2994 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002995 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002996 /* Make certain the we are not the first or last element of a block */
2997 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002998 if (block) {
2999 if ((block->last == ptr) && (block->first == ptr)) {
3000 block->last = block->first = 0;
3001 }
3002 else if (block->last == ptr) {
3003 block->last = ptr->prev;
3004 }
3005 else if (block->first == ptr) {
3006 block->first = ptr->next;
3007 }
Eric Biederman66fe2222003-07-04 15:14:04 +00003008 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003009 /* Remove ptr from use chains where it is the user */
3010 expr = triple_rhs(state, ptr, 0);
3011 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3012 if (*expr) {
3013 unuse_triple(*expr, ptr);
3014 }
3015 }
3016 expr = triple_lhs(state, ptr, 0);
3017 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3018 if (*expr) {
3019 unuse_triple(*expr, ptr);
3020 }
3021 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003022 expr = triple_misc(state, ptr, 0);
3023 for(; expr; expr = triple_misc(state, ptr, expr)) {
3024 if (*expr) {
3025 unuse_triple(*expr, ptr);
3026 }
3027 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003028 expr = triple_targ(state, ptr, 0);
3029 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003030 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003031 unuse_triple(*expr, ptr);
3032 }
3033 }
3034 /* Reomve ptr from use chains where it is used */
3035 for(set = ptr->use; set; set = next) {
3036 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003037 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003038 expr = triple_rhs(state, set->member, 0);
3039 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3040 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003041 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003042 }
3043 }
3044 expr = triple_lhs(state, set->member, 0);
3045 for(; expr; expr = triple_lhs(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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003050 expr = triple_misc(state, set->member, 0);
3051 for(; expr; expr = triple_misc(state, set->member, expr)) {
3052 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003053 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003054 }
3055 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003056 expr = triple_targ(state, set->member, 0);
3057 for(; expr; expr = triple_targ(state, set->member, expr)) {
3058 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003059 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003060 }
3061 }
3062 unuse_triple(ptr, set->member);
3063 }
3064 free_triple(state, ptr);
3065}
3066
Eric Biederman5ade04a2003-10-22 04:03:46 +00003067static void print_triples(struct compile_state *state);
3068static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003069
Jason Schildt27b85112005-08-10 14:31:52 +00003070#define TOK_UNKNOWN 0
3071#define TOK_SPACE 1
3072#define TOK_SEMI 2
3073#define TOK_LBRACE 3
3074#define TOK_RBRACE 4
3075#define TOK_COMMA 5
3076#define TOK_EQ 6
3077#define TOK_COLON 7
3078#define TOK_LBRACKET 8
3079#define TOK_RBRACKET 9
3080#define TOK_LPAREN 10
3081#define TOK_RPAREN 11
3082#define TOK_STAR 12
3083#define TOK_DOTS 13
3084#define TOK_MORE 14
3085#define TOK_LESS 15
3086#define TOK_TIMESEQ 16
3087#define TOK_DIVEQ 17
3088#define TOK_MODEQ 18
3089#define TOK_PLUSEQ 19
3090#define TOK_MINUSEQ 20
3091#define TOK_SLEQ 21
3092#define TOK_SREQ 22
3093#define TOK_ANDEQ 23
3094#define TOK_XOREQ 24
3095#define TOK_OREQ 25
3096#define TOK_EQEQ 26
3097#define TOK_NOTEQ 27
3098#define TOK_QUEST 28
3099#define TOK_LOGOR 29
3100#define TOK_LOGAND 30
3101#define TOK_OR 31
3102#define TOK_AND 32
3103#define TOK_XOR 33
3104#define TOK_LESSEQ 34
3105#define TOK_MOREEQ 35
3106#define TOK_SL 36
3107#define TOK_SR 37
3108#define TOK_PLUS 38
3109#define TOK_MINUS 39
3110#define TOK_DIV 40
3111#define TOK_MOD 41
3112#define TOK_PLUSPLUS 42
3113#define TOK_MINUSMINUS 43
3114#define TOK_BANG 44
3115#define TOK_ARROW 45
3116#define TOK_DOT 46
3117#define TOK_TILDE 47
3118#define TOK_LIT_STRING 48
3119#define TOK_LIT_CHAR 49
3120#define TOK_LIT_INT 50
3121#define TOK_LIT_FLOAT 51
3122#define TOK_MACRO 52
3123#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003124
Jason Schildt27b85112005-08-10 14:31:52 +00003125#define TOK_IDENT 54
3126#define TOK_STRUCT_NAME 55
3127#define TOK_ENUM_CONST 56
3128#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003129
Jason Schildt27b85112005-08-10 14:31:52 +00003130#define TOK_AUTO 58
3131#define TOK_BREAK 59
3132#define TOK_CASE 60
3133#define TOK_CHAR 61
3134#define TOK_CONST 62
3135#define TOK_CONTINUE 63
3136#define TOK_DEFAULT 64
3137#define TOK_DO 65
3138#define TOK_DOUBLE 66
3139#define TOK_ELSE 67
3140#define TOK_ENUM 68
3141#define TOK_EXTERN 69
3142#define TOK_FLOAT 70
3143#define TOK_FOR 71
3144#define TOK_GOTO 72
3145#define TOK_IF 73
3146#define TOK_INLINE 74
3147#define TOK_INT 75
3148#define TOK_LONG 76
3149#define TOK_REGISTER 77
3150#define TOK_RESTRICT 78
3151#define TOK_RETURN 79
3152#define TOK_SHORT 80
3153#define TOK_SIGNED 81
3154#define TOK_SIZEOF 82
3155#define TOK_STATIC 83
3156#define TOK_STRUCT 84
3157#define TOK_SWITCH 85
3158#define TOK_TYPEDEF 86
3159#define TOK_UNION 87
3160#define TOK_UNSIGNED 88
3161#define TOK_VOID 89
3162#define TOK_VOLATILE 90
3163#define TOK_WHILE 91
3164#define TOK_ASM 92
3165#define TOK_ATTRIBUTE 93
3166#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003167#define TOK_FIRST_KEYWORD TOK_AUTO
3168#define TOK_LAST_KEYWORD TOK_ALIGNOF
3169
Eric Biedermancb364952004-11-15 10:46:44 +00003170#define TOK_MDEFINE 100
3171#define TOK_MDEFINED 101
3172#define TOK_MUNDEF 102
3173#define TOK_MINCLUDE 103
3174#define TOK_MLINE 104
3175#define TOK_MERROR 105
3176#define TOK_MWARNING 106
3177#define TOK_MPRAGMA 107
3178#define TOK_MIFDEF 108
3179#define TOK_MIFNDEF 109
3180#define TOK_MELIF 110
3181#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003182
Eric Biedermancb364952004-11-15 10:46:44 +00003183#define TOK_FIRST_MACRO TOK_MDEFINE
3184#define TOK_LAST_MACRO TOK_MENDIF
Stefan Reinauer14e22772010-04-27 06:56:47 +00003185
Eric Biedermancb364952004-11-15 10:46:44 +00003186#define TOK_MIF 112
3187#define TOK_MELSE 113
3188#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003189
Eric Biedermancb364952004-11-15 10:46:44 +00003190#define TOK_EOL 115
3191#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003192
3193static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003194[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003195[TOK_SPACE ] = ":space:",
3196[TOK_SEMI ] = ";",
3197[TOK_LBRACE ] = "{",
3198[TOK_RBRACE ] = "}",
3199[TOK_COMMA ] = ",",
3200[TOK_EQ ] = "=",
3201[TOK_COLON ] = ":",
3202[TOK_LBRACKET ] = "[",
3203[TOK_RBRACKET ] = "]",
3204[TOK_LPAREN ] = "(",
3205[TOK_RPAREN ] = ")",
3206[TOK_STAR ] = "*",
3207[TOK_DOTS ] = "...",
3208[TOK_MORE ] = ">",
3209[TOK_LESS ] = "<",
3210[TOK_TIMESEQ ] = "*=",
3211[TOK_DIVEQ ] = "/=",
3212[TOK_MODEQ ] = "%=",
3213[TOK_PLUSEQ ] = "+=",
3214[TOK_MINUSEQ ] = "-=",
3215[TOK_SLEQ ] = "<<=",
3216[TOK_SREQ ] = ">>=",
3217[TOK_ANDEQ ] = "&=",
3218[TOK_XOREQ ] = "^=",
3219[TOK_OREQ ] = "|=",
3220[TOK_EQEQ ] = "==",
3221[TOK_NOTEQ ] = "!=",
3222[TOK_QUEST ] = "?",
3223[TOK_LOGOR ] = "||",
3224[TOK_LOGAND ] = "&&",
3225[TOK_OR ] = "|",
3226[TOK_AND ] = "&",
3227[TOK_XOR ] = "^",
3228[TOK_LESSEQ ] = "<=",
3229[TOK_MOREEQ ] = ">=",
3230[TOK_SL ] = "<<",
3231[TOK_SR ] = ">>",
3232[TOK_PLUS ] = "+",
3233[TOK_MINUS ] = "-",
3234[TOK_DIV ] = "/",
3235[TOK_MOD ] = "%",
3236[TOK_PLUSPLUS ] = "++",
3237[TOK_MINUSMINUS ] = "--",
3238[TOK_BANG ] = "!",
3239[TOK_ARROW ] = "->",
3240[TOK_DOT ] = ".",
3241[TOK_TILDE ] = "~",
3242[TOK_LIT_STRING ] = ":string:",
3243[TOK_IDENT ] = ":ident:",
3244[TOK_TYPE_NAME ] = ":typename:",
3245[TOK_LIT_CHAR ] = ":char:",
3246[TOK_LIT_INT ] = ":integer:",
3247[TOK_LIT_FLOAT ] = ":float:",
3248[TOK_MACRO ] = "#",
3249[TOK_CONCATENATE ] = "##",
3250
3251[TOK_AUTO ] = "auto",
3252[TOK_BREAK ] = "break",
3253[TOK_CASE ] = "case",
3254[TOK_CHAR ] = "char",
3255[TOK_CONST ] = "const",
3256[TOK_CONTINUE ] = "continue",
3257[TOK_DEFAULT ] = "default",
3258[TOK_DO ] = "do",
3259[TOK_DOUBLE ] = "double",
3260[TOK_ELSE ] = "else",
3261[TOK_ENUM ] = "enum",
3262[TOK_EXTERN ] = "extern",
3263[TOK_FLOAT ] = "float",
3264[TOK_FOR ] = "for",
3265[TOK_GOTO ] = "goto",
3266[TOK_IF ] = "if",
3267[TOK_INLINE ] = "inline",
3268[TOK_INT ] = "int",
3269[TOK_LONG ] = "long",
3270[TOK_REGISTER ] = "register",
3271[TOK_RESTRICT ] = "restrict",
3272[TOK_RETURN ] = "return",
3273[TOK_SHORT ] = "short",
3274[TOK_SIGNED ] = "signed",
3275[TOK_SIZEOF ] = "sizeof",
3276[TOK_STATIC ] = "static",
3277[TOK_STRUCT ] = "struct",
3278[TOK_SWITCH ] = "switch",
3279[TOK_TYPEDEF ] = "typedef",
3280[TOK_UNION ] = "union",
3281[TOK_UNSIGNED ] = "unsigned",
3282[TOK_VOID ] = "void",
3283[TOK_VOLATILE ] = "volatile",
3284[TOK_WHILE ] = "while",
3285[TOK_ASM ] = "asm",
3286[TOK_ATTRIBUTE ] = "__attribute__",
3287[TOK_ALIGNOF ] = "__alignof__",
3288
Eric Biederman41203d92004-11-08 09:31:09 +00003289[TOK_MDEFINE ] = "#define",
3290[TOK_MDEFINED ] = "#defined",
3291[TOK_MUNDEF ] = "#undef",
3292[TOK_MINCLUDE ] = "#include",
3293[TOK_MLINE ] = "#line",
3294[TOK_MERROR ] = "#error",
3295[TOK_MWARNING ] = "#warning",
3296[TOK_MPRAGMA ] = "#pragma",
3297[TOK_MIFDEF ] = "#ifdef",
3298[TOK_MIFNDEF ] = "#ifndef",
3299[TOK_MELIF ] = "#elif",
3300[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003301
Eric Biederman41203d92004-11-08 09:31:09 +00003302[TOK_MIF ] = "#if",
3303[TOK_MELSE ] = "#else",
3304[TOK_MIDENT ] = "#:ident:",
Stefan Reinauer14e22772010-04-27 06:56:47 +00003305[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003306[TOK_EOF ] = "EOF",
3307};
3308
3309static unsigned int hash(const char *str, int str_len)
3310{
3311 unsigned int hash;
3312 const char *end;
3313 end = str + str_len;
3314 hash = 0;
3315 for(; str < end; str++) {
3316 hash = (hash *263) + *str;
3317 }
3318 hash = hash & (HASH_TABLE_SIZE -1);
3319 return hash;
3320}
3321
3322static struct hash_entry *lookup(
3323 struct compile_state *state, const char *name, int name_len)
3324{
3325 struct hash_entry *entry;
3326 unsigned int index;
3327 index = hash(name, name_len);
3328 entry = state->hash_table[index];
Stefan Reinauer14e22772010-04-27 06:56:47 +00003329 while(entry &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003330 ((entry->name_len != name_len) ||
3331 (memcmp(entry->name, name, name_len) != 0))) {
3332 entry = entry->next;
3333 }
3334 if (!entry) {
3335 char *new_name;
3336 /* Get a private copy of the name */
3337 new_name = xmalloc(name_len + 1, "hash_name");
3338 memcpy(new_name, name, name_len);
3339 new_name[name_len] = '\0';
3340
3341 /* Create a new hash entry */
3342 entry = xcmalloc(sizeof(*entry), "hash_entry");
3343 entry->next = state->hash_table[index];
3344 entry->name = new_name;
3345 entry->name_len = name_len;
3346
3347 /* Place the new entry in the hash table */
3348 state->hash_table[index] = entry;
3349 }
3350 return entry;
3351}
3352
3353static void ident_to_keyword(struct compile_state *state, struct token *tk)
3354{
3355 struct hash_entry *entry;
3356 entry = tk->ident;
3357 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3358 (entry->tok == TOK_ENUM_CONST) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00003359 ((entry->tok >= TOK_FIRST_KEYWORD) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003360 (entry->tok <= TOK_LAST_KEYWORD)))) {
3361 tk->tok = entry->tok;
3362 }
3363}
3364
3365static void ident_to_macro(struct compile_state *state, struct token *tk)
3366{
3367 struct hash_entry *entry;
3368 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003369 if (!entry)
3370 return;
3371 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003372 tk->tok = entry->tok;
3373 }
Eric Biederman41203d92004-11-08 09:31:09 +00003374 else if (entry->tok == TOK_IF) {
3375 tk->tok = TOK_MIF;
3376 }
3377 else if (entry->tok == TOK_ELSE) {
3378 tk->tok = TOK_MELSE;
3379 }
3380 else {
3381 tk->tok = TOK_MIDENT;
3382 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003383}
3384
3385static void hash_keyword(
3386 struct compile_state *state, const char *keyword, int tok)
3387{
3388 struct hash_entry *entry;
3389 entry = lookup(state, keyword, strlen(keyword));
3390 if (entry && entry->tok != TOK_UNKNOWN) {
3391 die("keyword %s already hashed", keyword);
3392 }
3393 entry->tok = tok;
3394}
3395
Eric Biederman90089602004-05-28 14:11:54 +00003396static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003397 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003398 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003399{
3400 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003401 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003402 error(state, 0, "%s already defined", ident->name);
3403 }
3404 sym = xcmalloc(sizeof(*sym), "symbol");
3405 sym->ident = ident;
3406 sym->def = def;
3407 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003408 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003409 sym->next = *chain;
3410 *chain = sym;
3411}
3412
Eric Biederman90089602004-05-28 14:11:54 +00003413static void symbol(
3414 struct compile_state *state, struct hash_entry *ident,
3415 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003416{
Eric Biederman90089602004-05-28 14:11:54 +00003417 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3418}
3419
Stefan Reinauer14e22772010-04-27 06:56:47 +00003420static void var_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003421 struct hash_entry *ident, struct triple *def)
3422{
3423 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3424 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003425 }
Eric Biederman90089602004-05-28 14:11:54 +00003426 symbol(state, ident, &ident->sym_ident, def, def->type);
3427}
3428
Stefan Reinauer14e22772010-04-27 06:56:47 +00003429static void label_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003430 struct hash_entry *ident, struct triple *label, int depth)
3431{
3432 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003433}
3434
Eric Biedermanb138ac82003-04-22 18:44:01 +00003435static void start_scope(struct compile_state *state)
3436{
3437 state->scope_depth++;
3438}
3439
Eric Biederman90089602004-05-28 14:11:54 +00003440static void end_scope_syms(struct compile_state *state,
3441 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003442{
3443 struct symbol *sym, *next;
3444 sym = *chain;
3445 while(sym && (sym->scope_depth == depth)) {
3446 next = sym->next;
3447 xfree(sym);
3448 sym = next;
3449 }
3450 *chain = sym;
3451}
3452
3453static void end_scope(struct compile_state *state)
3454{
3455 int i;
3456 int depth;
3457 /* Walk through the hash table and remove all symbols
Stefan Reinauer14e22772010-04-27 06:56:47 +00003458 * in the current scope.
Eric Biedermanb138ac82003-04-22 18:44:01 +00003459 */
3460 depth = state->scope_depth;
3461 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3462 struct hash_entry *entry;
3463 entry = state->hash_table[i];
3464 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003465 end_scope_syms(state, &entry->sym_label, depth);
3466 end_scope_syms(state, &entry->sym_tag, depth);
3467 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003468 entry = entry->next;
3469 }
3470 }
3471 state->scope_depth = depth - 1;
3472}
3473
3474static void register_keywords(struct compile_state *state)
3475{
3476 hash_keyword(state, "auto", TOK_AUTO);
3477 hash_keyword(state, "break", TOK_BREAK);
3478 hash_keyword(state, "case", TOK_CASE);
3479 hash_keyword(state, "char", TOK_CHAR);
3480 hash_keyword(state, "const", TOK_CONST);
3481 hash_keyword(state, "continue", TOK_CONTINUE);
3482 hash_keyword(state, "default", TOK_DEFAULT);
3483 hash_keyword(state, "do", TOK_DO);
3484 hash_keyword(state, "double", TOK_DOUBLE);
3485 hash_keyword(state, "else", TOK_ELSE);
3486 hash_keyword(state, "enum", TOK_ENUM);
3487 hash_keyword(state, "extern", TOK_EXTERN);
3488 hash_keyword(state, "float", TOK_FLOAT);
3489 hash_keyword(state, "for", TOK_FOR);
3490 hash_keyword(state, "goto", TOK_GOTO);
3491 hash_keyword(state, "if", TOK_IF);
3492 hash_keyword(state, "inline", TOK_INLINE);
3493 hash_keyword(state, "int", TOK_INT);
3494 hash_keyword(state, "long", TOK_LONG);
3495 hash_keyword(state, "register", TOK_REGISTER);
3496 hash_keyword(state, "restrict", TOK_RESTRICT);
3497 hash_keyword(state, "return", TOK_RETURN);
3498 hash_keyword(state, "short", TOK_SHORT);
3499 hash_keyword(state, "signed", TOK_SIGNED);
3500 hash_keyword(state, "sizeof", TOK_SIZEOF);
3501 hash_keyword(state, "static", TOK_STATIC);
3502 hash_keyword(state, "struct", TOK_STRUCT);
3503 hash_keyword(state, "switch", TOK_SWITCH);
3504 hash_keyword(state, "typedef", TOK_TYPEDEF);
3505 hash_keyword(state, "union", TOK_UNION);
3506 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3507 hash_keyword(state, "void", TOK_VOID);
3508 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003509 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003510 hash_keyword(state, "while", TOK_WHILE);
3511 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003512 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003513 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3514 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3515}
3516
3517static void register_macro_keywords(struct compile_state *state)
3518{
Eric Biederman41203d92004-11-08 09:31:09 +00003519 hash_keyword(state, "define", TOK_MDEFINE);
3520 hash_keyword(state, "defined", TOK_MDEFINED);
3521 hash_keyword(state, "undef", TOK_MUNDEF);
3522 hash_keyword(state, "include", TOK_MINCLUDE);
3523 hash_keyword(state, "line", TOK_MLINE);
3524 hash_keyword(state, "error", TOK_MERROR);
3525 hash_keyword(state, "warning", TOK_MWARNING);
3526 hash_keyword(state, "pragma", TOK_MPRAGMA);
3527 hash_keyword(state, "ifdef", TOK_MIFDEF);
3528 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3529 hash_keyword(state, "elif", TOK_MELIF);
3530 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003531}
3532
Eric Biederman90089602004-05-28 14:11:54 +00003533
3534static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3535{
3536 if (ident->sym_define != 0) {
3537 struct macro *macro;
3538 struct macro_arg *arg, *anext;
3539 macro = ident->sym_define;
3540 ident->sym_define = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003541
Eric Biederman90089602004-05-28 14:11:54 +00003542 /* Free the macro arguments... */
3543 anext = macro->args;
3544 while(anext) {
3545 arg = anext;
3546 anext = arg->next;
3547 xfree(arg);
3548 }
3549
3550 /* Free the macro buffer */
3551 xfree(macro->buf);
3552
3553 /* Now free the macro itself */
3554 xfree(macro);
3555 }
3556}
3557
Stefan Reinauer14e22772010-04-27 06:56:47 +00003558static void do_define_macro(struct compile_state *state,
3559 struct hash_entry *ident, const char *body,
Eric Biedermancb364952004-11-15 10:46:44 +00003560 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003561{
3562 struct macro *macro;
3563 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003564 size_t body_len;
3565
3566 /* Find the length of the body */
3567 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003568 macro = ident->sym_define;
3569 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003570 int identical_bodies, identical_args;
3571 struct macro_arg *oarg;
3572 /* Explicitly allow identical redfinitions of the same macro */
Stefan Reinauer14e22772010-04-27 06:56:47 +00003573 identical_bodies =
Eric Biedermancb364952004-11-15 10:46:44 +00003574 (macro->buf_len == body_len) &&
3575 (memcmp(macro->buf, body, body_len) == 0);
3576 identical_args = macro->argc == argc;
3577 oarg = macro->args;
3578 arg = args;
3579 while(identical_args && arg) {
3580 identical_args = oarg->ident == arg->ident;
3581 arg = arg->next;
3582 oarg = oarg->next;
3583 }
3584 if (identical_bodies && identical_args) {
3585 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003586 return;
3587 }
3588 error(state, 0, "macro %s already defined\n", ident->name);
3589 }
3590#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003591 fprintf(state->errout, "#define %s: `%*.*s'\n",
3592 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003593#endif
3594 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003595 macro->ident = ident;
3596 macro->buf = body;
3597 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003598 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003599 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003600
3601 ident->sym_define = macro;
3602}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003603
Eric Biedermancb364952004-11-15 10:46:44 +00003604static void define_macro(
3605 struct compile_state *state,
3606 struct hash_entry *ident,
3607 const char *body, int body_len,
3608 int argc, struct macro_arg *args)
3609{
3610 char *buf;
3611 buf = xmalloc(body_len + 1, "macro buf");
3612 memcpy(buf, body, body_len);
3613 buf[body_len] = '\0';
3614 do_define_macro(state, ident, buf, argc, args);
3615}
Eric Biederman90089602004-05-28 14:11:54 +00003616
3617static void register_builtin_macro(struct compile_state *state,
3618 const char *name, const char *value)
3619{
3620 struct hash_entry *ident;
3621
3622 if (value[0] == '(') {
3623 internal_error(state, 0, "Builtin macros with arguments not supported");
3624 }
3625 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003626 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003627}
3628
3629static void register_builtin_macros(struct compile_state *state)
3630{
3631 char buf[30];
3632 char scratch[30];
3633 time_t now;
3634 struct tm *tm;
3635 now = time(NULL);
3636 tm = localtime(&now);
3637
3638 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3639 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3640 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3641 register_builtin_macro(state, "__LINE__", "54321");
3642
3643 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3644 sprintf(buf, "\"%s\"", scratch);
3645 register_builtin_macro(state, "__DATE__", buf);
3646
3647 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3648 sprintf(buf, "\"%s\"", scratch);
3649 register_builtin_macro(state, "__TIME__", buf);
3650
3651 /* I can't be a conforming implementation of C :( */
3652 register_builtin_macro(state, "__STDC__", "0");
3653 /* In particular I don't conform to C99 */
3654 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
Stefan Reinauer14e22772010-04-27 06:56:47 +00003655
Eric Biederman90089602004-05-28 14:11:54 +00003656}
3657
3658static void process_cmdline_macros(struct compile_state *state)
3659{
3660 const char **macro, *name;
3661 struct hash_entry *ident;
3662 for(macro = state->compiler->defines; (name = *macro); macro++) {
3663 const char *body;
3664 size_t name_len;
3665
3666 name_len = strlen(name);
3667 body = strchr(name, '=');
3668 if (!body) {
3669 body = "\0";
3670 } else {
3671 name_len = body - name;
3672 body++;
3673 }
3674 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003675 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003676 }
3677 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3678 ident = lookup(state, name, strlen(name));
3679 undef_macro(state, ident);
3680 }
3681}
3682
Eric Biedermanb138ac82003-04-22 18:44:01 +00003683static int spacep(int c)
3684{
3685 int ret = 0;
3686 switch(c) {
3687 case ' ':
3688 case '\t':
3689 case '\f':
3690 case '\v':
3691 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003692 ret = 1;
3693 break;
3694 }
3695 return ret;
3696}
3697
Eric Biedermanb138ac82003-04-22 18:44:01 +00003698static int digitp(int c)
3699{
3700 int ret = 0;
3701 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003702 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003703 case '5': case '6': case '7': case '8': case '9':
3704 ret = 1;
3705 break;
3706 }
3707 return ret;
3708}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003709static int digval(int c)
3710{
3711 int val = -1;
3712 if ((c >= '0') && (c <= '9')) {
3713 val = c - '0';
3714 }
3715 return val;
3716}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003717
3718static int hexdigitp(int c)
3719{
3720 int ret = 0;
3721 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003722 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003723 case '5': case '6': case '7': case '8': case '9':
3724 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3725 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3726 ret = 1;
3727 break;
3728 }
3729 return ret;
3730}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003731static int hexdigval(int c)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003732{
3733 int val = -1;
3734 if ((c >= '0') && (c <= '9')) {
3735 val = c - '0';
3736 }
3737 else if ((c >= 'A') && (c <= 'F')) {
3738 val = 10 + (c - 'A');
3739 }
3740 else if ((c >= 'a') && (c <= 'f')) {
3741 val = 10 + (c - 'a');
3742 }
3743 return val;
3744}
3745
3746static int octdigitp(int c)
3747{
3748 int ret = 0;
3749 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003750 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003751 case '4': case '5': case '6': case '7':
3752 ret = 1;
3753 break;
3754 }
3755 return ret;
3756}
3757static int octdigval(int c)
3758{
3759 int val = -1;
3760 if ((c >= '0') && (c <= '7')) {
3761 val = c - '0';
3762 }
3763 return val;
3764}
3765
3766static int letterp(int c)
3767{
3768 int ret = 0;
3769 switch(c) {
3770 case 'a': case 'b': case 'c': case 'd': case 'e':
3771 case 'f': case 'g': case 'h': case 'i': case 'j':
3772 case 'k': case 'l': case 'm': case 'n': case 'o':
3773 case 'p': case 'q': case 'r': case 's': case 't':
3774 case 'u': case 'v': case 'w': case 'x': case 'y':
3775 case 'z':
3776 case 'A': case 'B': case 'C': case 'D': case 'E':
3777 case 'F': case 'G': case 'H': case 'I': case 'J':
3778 case 'K': case 'L': case 'M': case 'N': case 'O':
3779 case 'P': case 'Q': case 'R': case 'S': case 'T':
3780 case 'U': case 'V': case 'W': case 'X': case 'Y':
3781 case 'Z':
3782 case '_':
3783 ret = 1;
3784 break;
3785 }
3786 return ret;
3787}
3788
Eric Biederman90089602004-05-28 14:11:54 +00003789static const char *identifier(const char *str, const char *end)
3790{
3791 if (letterp(*str)) {
3792 for(; str < end; str++) {
3793 int c;
3794 c = *str;
3795 if (!letterp(c) && !digitp(c)) {
3796 break;
3797 }
3798 }
3799 }
3800 return str;
3801}
3802
Eric Biedermanb138ac82003-04-22 18:44:01 +00003803static int char_value(struct compile_state *state,
3804 const signed char **strp, const signed char *end)
3805{
3806 const signed char *str;
3807 int c;
3808 str = *strp;
3809 c = *str++;
3810 if ((c == '\\') && (str < end)) {
3811 switch(*str) {
3812 case 'n': c = '\n'; str++; break;
3813 case 't': c = '\t'; str++; break;
3814 case 'v': c = '\v'; str++; break;
3815 case 'b': c = '\b'; str++; break;
3816 case 'r': c = '\r'; str++; break;
3817 case 'f': c = '\f'; str++; break;
3818 case 'a': c = '\a'; str++; break;
3819 case '\\': c = '\\'; str++; break;
3820 case '?': c = '?'; str++; break;
3821 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003822 case '"': c = '"'; str++; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003823 case 'x':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003824 c = 0;
3825 str++;
3826 while((str < end) && hexdigitp(*str)) {
3827 c <<= 4;
3828 c += hexdigval(*str);
3829 str++;
3830 }
3831 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003832 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003833 case '4': case '5': case '6': case '7':
3834 c = 0;
3835 while((str < end) && octdigitp(*str)) {
3836 c <<= 3;
3837 c += octdigval(*str);
3838 str++;
3839 }
3840 break;
3841 default:
3842 error(state, 0, "Invalid character constant");
3843 break;
3844 }
3845 }
3846 *strp = str;
3847 return c;
3848}
3849
Eric Biedermancb364952004-11-15 10:46:44 +00003850static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003851{
Eric Biedermancb364952004-11-15 10:46:44 +00003852 const char *end = file->buf + file->size;
3853 while(pos < end) {
3854 /* Lookup the character */
3855 int size = 1;
3856 int c = *pos;
3857 /* Is this a trigraph? */
3858 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003859 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003860 {
3861 switch(pos[2]) {
3862 case '=': c = '#'; break;
3863 case '/': c = '\\'; break;
3864 case '\'': c = '^'; break;
3865 case '(': c = '['; break;
3866 case ')': c = ']'; break;
3867 case '!': c = '!'; break;
3868 case '<': c = '{'; break;
3869 case '>': c = '}'; break;
3870 case '-': c = '~'; break;
3871 }
3872 if (c != '?') {
3873 size = 3;
3874 }
3875 }
3876 /* Is this an escaped newline? */
3877 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003878 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003879 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003880 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003881 /* At the start of a line just eat it */
3882 if (pos == file->pos) {
3883 file->line++;
3884 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003885 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003886 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003887 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003888 }
3889 /* Do I need to ga any farther? */
3890 else if (index == 0) {
3891 break;
3892 }
3893 /* Process a normal character */
3894 else {
3895 pos += size;
3896 index -= 1;
3897 }
3898 }
3899 return pos;
3900}
3901
3902static int get_char(struct file_state *file, const char *pos)
3903{
3904 const char *end = file->buf + file->size;
3905 int c;
3906 c = -1;
3907 pos = next_char(file, pos, 0);
3908 if (pos < end) {
3909 /* Lookup the character */
3910 c = *pos;
3911 /* If it is a trigraph get the trigraph value */
3912 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003913 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003914 {
3915 switch(pos[2]) {
3916 case '=': c = '#'; break;
3917 case '/': c = '\\'; break;
3918 case '\'': c = '^'; break;
3919 case '(': c = '['; break;
3920 case ')': c = ']'; break;
3921 case '!': c = '!'; break;
3922 case '<': c = '{'; break;
3923 case '>': c = '}'; break;
3924 case '-': c = '~'; break;
3925 }
3926 }
3927 }
3928 return c;
3929}
3930
3931static void eat_chars(struct file_state *file, const char *targ)
3932{
3933 const char *pos = file->pos;
3934 while(pos < targ) {
3935 /* Do we have a newline? */
3936 if (pos[0] == '\n') {
3937 file->line++;
3938 file->report_line++;
3939 file->line_start = pos + 1;
3940 }
3941 pos++;
3942 }
3943 file->pos = pos;
3944}
3945
3946
3947static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3948{
3949 size_t len;
3950 len = 0;
3951 while(src < end) {
3952 src = next_char(file, src, 1);
3953 len++;
3954 }
3955 return len;
3956}
3957
Stefan Reinauer14e22772010-04-27 06:56:47 +00003958static void char_strcpy(char *dest,
Eric Biedermancb364952004-11-15 10:46:44 +00003959 struct file_state *file, const char *src, const char *end)
3960{
3961 while(src < end) {
3962 int c;
3963 c = get_char(file, src);
3964 src = next_char(file, src, 1);
3965 *dest++ = c;
3966 }
3967}
3968
Stefan Reinauer14e22772010-04-27 06:56:47 +00003969static char *char_strdup(struct file_state *file,
Eric Biedermancb364952004-11-15 10:46:44 +00003970 const char *start, const char *end, const char *id)
3971{
3972 char *str;
3973 size_t str_len;
3974 str_len = char_strlen(file, start, end);
3975 str = xcmalloc(str_len + 1, id);
3976 char_strcpy(str, file, start, end);
3977 str[str_len] = '\0';
3978 return str;
3979}
3980
3981static const char *after_digits(struct file_state *file, const char *ptr)
3982{
3983 while(digitp(get_char(file, ptr))) {
3984 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003985 }
3986 return ptr;
3987}
3988
Eric Biedermancb364952004-11-15 10:46:44 +00003989static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003990{
Eric Biedermancb364952004-11-15 10:46:44 +00003991 while(octdigitp(get_char(file, ptr))) {
3992 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003993 }
3994 return ptr;
3995}
3996
Eric Biedermancb364952004-11-15 10:46:44 +00003997static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003998{
Eric Biedermancb364952004-11-15 10:46:44 +00003999 while(hexdigitp(get_char(file, ptr))) {
4000 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004001 }
4002 return ptr;
4003}
4004
Eric Biedermancb364952004-11-15 10:46:44 +00004005static const char *after_alnums(struct file_state *file, const char *ptr)
4006{
4007 int c;
4008 c = get_char(file, ptr);
4009 while(letterp(c) || digitp(c)) {
4010 ptr = next_char(file, ptr, 1);
4011 c = get_char(file, ptr);
4012 }
4013 return ptr;
4014}
4015
4016static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004017 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004018{
4019 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004020
Eric Biedermanb138ac82003-04-22 18:44:01 +00004021 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004022 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004023
4024 /* Store the copy in the token */
4025 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004026 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004027}
4028
Stefan Reinauer14e22772010-04-27 06:56:47 +00004029static void raw_next_token(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004030 struct file_state *file, struct token *tk)
4031{
4032 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004033 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004034 const char *tokp;
4035 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004036 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004037
Eric Biedermanb138ac82003-04-22 18:44:01 +00004038 tk->str_len = 0;
4039 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004040 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004041 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004042 c = get_char(file, tokp);
4043 tokp = next_char(file, tokp, 1);
4044 eat = 0;
4045 c1 = get_char(file, tokp);
4046 c2 = get_char(file, next_char(file, tokp, 1));
4047 c3 = get_char(file, next_char(file, tokp, 2));
4048
4049 /* The end of the file */
4050 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004051 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004052 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004053 /* Whitespace */
4054 else if (spacep(c)) {
4055 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004056 while (spacep(get_char(file, tokp))) {
4057 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004058 }
4059 }
4060 /* EOL Comments */
4061 else if ((c == '/') && (c1 == '/')) {
4062 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004063 tokp = next_char(file, tokp, 1);
4064 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004065 /* Advance to the next character only after we verify
Stefan Reinauer14e22772010-04-27 06:56:47 +00004066 * the current character is not a newline.
Eric Biederman57183382006-12-02 16:48:48 +00004067 * EOL is special to the preprocessor so we don't
4068 * want to loose any.
4069 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004070 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004071 break;
4072 }
Eric Biederman57183382006-12-02 16:48:48 +00004073 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004074 }
4075 }
4076 /* Comments */
4077 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004078 tokp = next_char(file, tokp, 2);
4079 c = c2;
4080 while((c1 = get_char(file, tokp)) != -1) {
4081 tokp = next_char(file, tokp, 1);
4082 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004083 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004084 break;
4085 }
Eric Biedermancb364952004-11-15 10:46:44 +00004086 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004087 }
4088 if (tok == TOK_UNKNOWN) {
4089 error(state, 0, "unterminated comment");
4090 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004091 }
4092 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004093 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004094 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004095
Eric Biedermancb364952004-11-15 10:46:44 +00004096 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004097 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004098 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004099 }
Eric Biedermancb364952004-11-15 10:46:44 +00004100 while((c = get_char(file, tokp)) != -1) {
4101 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004102 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004103 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004104 }
Eric Biedermancb364952004-11-15 10:46:44 +00004105 else if (c == '\\') {
4106 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004107 }
4108 else if (c == '"') {
4109 tok = TOK_LIT_STRING;
4110 break;
4111 }
4112 }
4113 if (tok == TOK_UNKNOWN) {
4114 error(state, 0, "unterminated string constant");
4115 }
Eric Biedermancb364952004-11-15 10:46:44 +00004116 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004117 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004118 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004119
4120 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004121 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004122 }
4123 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004124 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004125 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004126
Eric Biedermancb364952004-11-15 10:46:44 +00004127 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004128 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004129 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004130 }
Eric Biedermancb364952004-11-15 10:46:44 +00004131 while((c = get_char(file, tokp)) != -1) {
4132 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004133 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004134 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004135 }
Eric Biedermancb364952004-11-15 10:46:44 +00004136 else if (c == '\\') {
4137 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004138 }
4139 else if (c == '\'') {
4140 tok = TOK_LIT_CHAR;
4141 break;
4142 }
4143 }
4144 if (tok == TOK_UNKNOWN) {
4145 error(state, 0, "unterminated character constant");
4146 }
Eric Biedermancb364952004-11-15 10:46:44 +00004147 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004148 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004149 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004150
4151 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004152 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004153 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004154 /* integer and floating constants
Eric Biedermanb138ac82003-04-22 18:44:01 +00004155 * Integer Constants
4156 * {digits}
4157 * 0[Xx]{hexdigits}
4158 * 0{octdigit}+
Stefan Reinauer14e22772010-04-27 06:56:47 +00004159 *
Eric Biedermanb138ac82003-04-22 18:44:01 +00004160 * Floating constants
4161 * {digits}.{digits}[Ee][+-]?{digits}
4162 * {digits}.{digits}
4163 * {digits}[Ee][+-]?{digits}
4164 * .{digits}[Ee][+-]?{digits}
4165 * .{digits}
4166 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004167 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004168 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004169 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004170 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004171 is_float = 0;
4172 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004173 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004174 }
4175 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004176 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004177 }
Eric Biedermancb364952004-11-15 10:46:44 +00004178 cn = get_char(file, next);
4179 if (cn == '.') {
4180 next = next_char(file, next, 1);
4181 next = after_digits(file, next);
4182 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004183 }
Eric Biedermancb364952004-11-15 10:46:44 +00004184 cn = get_char(file, next);
4185 if ((cn == 'e') || (cn == 'E')) {
4186 const char *new;
4187 next = next_char(file, next, 1);
4188 cn = get_char(file, next);
4189 if ((cn == '+') || (cn == '-')) {
4190 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004191 }
Eric Biedermancb364952004-11-15 10:46:44 +00004192 new = after_digits(file, next);
4193 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004194 next = new;
4195 }
4196 if (is_float) {
4197 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004198 cn = get_char(file, next);
4199 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4200 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004201 }
4202 }
4203 if (!is_float && digitp(c)) {
4204 tok = TOK_LIT_INT;
4205 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004206 next = next_char(file, tokp, 1);
4207 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004208 }
4209 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004210 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004211 }
4212 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004213 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004214 }
4215 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004216 cn = get_char(file, next);
4217 if ((cn == 'u') || (cn == 'U')) {
4218 next = next_char(file, next, 1);
4219 cn = get_char(file, next);
4220 if ((cn == 'l') || (cn == 'L')) {
4221 next = next_char(file, next, 1);
4222 cn = get_char(file, next);
4223 }
4224 if ((cn == 'l') || (cn == 'L')) {
4225 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004226 }
4227 }
Eric Biedermancb364952004-11-15 10:46:44 +00004228 else if ((cn == 'l') || (cn == 'L')) {
4229 next = next_char(file, next, 1);
4230 cn = get_char(file, next);
4231 if ((cn == 'l') || (cn == 'L')) {
4232 next = next_char(file, next, 1);
4233 cn = get_char(file, next);
4234 }
4235 if ((cn == 'u') || (cn == 'U')) {
4236 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004237 }
4238 }
4239 }
Eric Biedermancb364952004-11-15 10:46:44 +00004240 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004241
4242 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004243 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004244 }
4245 /* identifiers */
4246 else if (letterp(c)) {
4247 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004248
4249 /* Find and save the identifier string */
4250 tokp = after_alnums(file, tokp);
4251 save_string(file, tk, token, tokp, "identifier");
4252
4253 /* Look up to see which identifier it is */
4254 tk->ident = lookup(state, tk->val.str, tk->str_len);
4255
4256 /* Free the identifier string */
4257 tk->str_len = 0;
4258 xfree(tk->val.str);
4259
Eric Biederman90089602004-05-28 14:11:54 +00004260 /* See if this identifier can be macro expanded */
4261 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004262 c = get_char(file, tokp);
4263 if (c == '$') {
4264 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004265 tk->val.notmacro = 1;
4266 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004267 }
4268 /* C99 alternate macro characters */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004269 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004270 eat += 3;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004271 tok = TOK_CONCATENATE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004272 }
Eric Biedermancb364952004-11-15 10:46:44 +00004273 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4274 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4275 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4276 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4277 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4278 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4279 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4280 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4281 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4282 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4283 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4284 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4285 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4286 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4287 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4288 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4289 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4290 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4291 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4292 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4293 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4294 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4295 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4296 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4297 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4298 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4299 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4300 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004301 else if (c == ';') { tok = TOK_SEMI; }
4302 else if (c == '{') { tok = TOK_LBRACE; }
4303 else if (c == '}') { tok = TOK_RBRACE; }
4304 else if (c == ',') { tok = TOK_COMMA; }
4305 else if (c == '=') { tok = TOK_EQ; }
4306 else if (c == ':') { tok = TOK_COLON; }
4307 else if (c == '[') { tok = TOK_LBRACKET; }
4308 else if (c == ']') { tok = TOK_RBRACKET; }
4309 else if (c == '(') { tok = TOK_LPAREN; }
4310 else if (c == ')') { tok = TOK_RPAREN; }
4311 else if (c == '*') { tok = TOK_STAR; }
4312 else if (c == '>') { tok = TOK_MORE; }
4313 else if (c == '<') { tok = TOK_LESS; }
4314 else if (c == '?') { tok = TOK_QUEST; }
4315 else if (c == '|') { tok = TOK_OR; }
4316 else if (c == '&') { tok = TOK_AND; }
4317 else if (c == '^') { tok = TOK_XOR; }
4318 else if (c == '+') { tok = TOK_PLUS; }
4319 else if (c == '-') { tok = TOK_MINUS; }
4320 else if (c == '/') { tok = TOK_DIV; }
4321 else if (c == '%') { tok = TOK_MOD; }
4322 else if (c == '!') { tok = TOK_BANG; }
4323 else if (c == '.') { tok = TOK_DOT; }
4324 else if (c == '~') { tok = TOK_TILDE; }
4325 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004326 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004327
Eric Biedermancb364952004-11-15 10:46:44 +00004328 tokp = next_char(file, tokp, eat);
4329 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004330 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004331 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004332}
4333
4334static void check_tok(struct compile_state *state, struct token *tk, int tok)
4335{
4336 if (tk->tok != tok) {
4337 const char *name1, *name2;
4338 name1 = tokens[tk->tok];
4339 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004340 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004341 name2 = tk->ident->name;
4342 }
4343 error(state, 0, "\tfound %s %s expected %s",
4344 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004345 }
4346}
4347
Eric Biederman90089602004-05-28 14:11:54 +00004348struct macro_arg_value {
4349 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004350 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004351 size_t len;
4352};
4353static struct macro_arg_value *read_macro_args(
Stefan Reinauer14e22772010-04-27 06:56:47 +00004354 struct compile_state *state, struct macro *macro,
Eric Biederman90089602004-05-28 14:11:54 +00004355 struct file_state *file, struct token *tk)
4356{
4357 struct macro_arg_value *argv;
4358 struct macro_arg *arg;
4359 int paren_depth;
4360 int i;
4361
4362 if (macro->argc == 0) {
4363 do {
4364 raw_next_token(state, file, tk);
4365 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004366 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004367 }
4368 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4369 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4370 argv[i].value = 0;
4371 argv[i].len = 0;
4372 argv[i].ident = arg->ident;
4373 }
4374 paren_depth = 0;
4375 i = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004376
Eric Biederman90089602004-05-28 14:11:54 +00004377 for(;;) {
4378 const char *start;
4379 size_t len;
4380 start = file->pos;
4381 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004382
Eric Biederman90089602004-05-28 14:11:54 +00004383 if (!paren_depth && (tk->tok == TOK_COMMA) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004384 (argv[i].ident != state->i___VA_ARGS__))
Eric Biederman90089602004-05-28 14:11:54 +00004385 {
4386 i++;
4387 if (i >= macro->argc) {
4388 error(state, 0, "too many args to %s\n",
4389 macro->ident->name);
4390 }
4391 continue;
4392 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004393
Eric Biederman90089602004-05-28 14:11:54 +00004394 if (tk->tok == TOK_LPAREN) {
4395 paren_depth++;
4396 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004397
Eric Biederman90089602004-05-28 14:11:54 +00004398 if (tk->tok == TOK_RPAREN) {
4399 if (paren_depth == 0) {
4400 break;
4401 }
4402 paren_depth--;
4403 }
4404 if (tk->tok == TOK_EOF) {
4405 error(state, 0, "End of file encountered while parsing macro arguments");
4406 }
Eric Biedermancb364952004-11-15 10:46:44 +00004407
4408 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004409 argv[i].value = xrealloc(
4410 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004411 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004412 argv[i].len += len;
4413 }
4414 if (i != macro->argc -1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00004415 error(state, 0, "missing %s arg %d\n",
Eric Biederman90089602004-05-28 14:11:54 +00004416 macro->ident->name, i +2);
4417 }
4418 return argv;
4419}
4420
4421
4422static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4423{
4424 int i;
4425 for(i = 0; i < macro->argc; i++) {
4426 xfree(argv[i].value);
4427 }
4428 xfree(argv);
4429}
4430
4431struct macro_buf {
4432 char *str;
4433 size_t len, pos;
4434};
4435
Eric Biedermancb364952004-11-15 10:46:44 +00004436static void grow_macro_buf(struct compile_state *state,
4437 const char *id, struct macro_buf *buf,
4438 size_t grow)
4439{
4440 if ((buf->pos + grow) >= buf->len) {
4441 buf->str = xrealloc(buf->str, buf->len + grow, id);
4442 buf->len += grow;
4443 }
4444}
4445
Eric Biederman90089602004-05-28 14:11:54 +00004446static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004447 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004448 const char *fstart, size_t flen)
4449{
Eric Biedermancb364952004-11-15 10:46:44 +00004450 grow_macro_buf(state, id, buf, flen);
4451 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004452#if 0
4453 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4454 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004455 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004456#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004457 buf->pos += flen;
4458}
4459
4460
4461static void append_macro_chars(struct compile_state *state,
4462 const char *id, struct macro_buf *buf,
4463 struct file_state *file, const char *start, const char *end)
4464{
4465 size_t flen;
4466 flen = char_strlen(file, start, end);
4467 grow_macro_buf(state, id, buf, flen);
4468 char_strcpy(buf->str + buf->pos, file, start, end);
4469#if 0
4470 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4471 buf->pos, buf->pos, buf->str,
4472 flen, flen, buf->str + buf->pos);
4473#endif
Eric Biederman90089602004-05-28 14:11:54 +00004474 buf->pos += flen;
4475}
4476
Stefan Reinauer14e22772010-04-27 06:56:47 +00004477static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004478 struct file_state **filep, struct token *tk);
4479
Stefan Reinauer14e22772010-04-27 06:56:47 +00004480static void macro_expand_args(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004481 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4482{
Eric Biedermancb364952004-11-15 10:46:44 +00004483 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004484
Eric Biederman90089602004-05-28 14:11:54 +00004485 for(i = 0; i < macro->argc; i++) {
4486 struct file_state fmacro, *file;
4487 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004488
Eric Biedermancb364952004-11-15 10:46:44 +00004489 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004490 fmacro.basename = argv[i].ident->name;
4491 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004492 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004493 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004494 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004495 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004496 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004497 fmacro.report_line = 1;
4498 fmacro.report_name = fmacro.basename;
4499 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004500 fmacro.macro = 1;
4501 fmacro.trigraphs = 0;
4502 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004503
4504 buf.len = argv[i].len;
4505 buf.str = xmalloc(buf.len, argv[i].ident->name);
4506 buf.pos = 0;
4507
4508 file = &fmacro;
4509 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004510 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004511
Eric Biedermancb364952004-11-15 10:46:44 +00004512 /* If we have recursed into another macro body
4513 * get out of it.
4514 */
Eric Biederman90089602004-05-28 14:11:54 +00004515 if (tk->tok == TOK_EOF) {
4516 struct file_state *old;
4517 old = file;
4518 file = file->prev;
4519 if (!file) {
4520 break;
4521 }
4522 /* old->basename is used keep it */
4523 xfree(old->dirname);
4524 xfree(old->buf);
4525 xfree(old);
4526 continue;
4527 }
4528 else if (tk->ident && tk->ident->sym_define) {
4529 if (compile_macro(state, &file, tk)) {
4530 continue;
4531 }
4532 }
4533
Eric Biedermancb364952004-11-15 10:46:44 +00004534 append_macro_chars(state, macro->ident->name, &buf,
4535 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004536 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004537
Eric Biederman90089602004-05-28 14:11:54 +00004538 xfree(argv[i].value);
4539 argv[i].value = buf.str;
4540 argv[i].len = buf.pos;
4541 }
4542 return;
4543}
4544
4545static void expand_macro(struct compile_state *state,
4546 struct macro *macro, struct macro_buf *buf,
4547 struct macro_arg_value *argv, struct token *tk)
4548{
4549 struct file_state fmacro;
4550 const char space[] = " ";
4551 const char *fstart;
4552 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004553 int i, j;
4554
4555 /* Place the macro body in a dummy file */
4556 fmacro.prev = 0;
4557 fmacro.basename = macro->ident->name;
4558 fmacro.dirname = "";
4559 fmacro.buf = macro->buf;
4560 fmacro.size = macro->buf_len;
4561 fmacro.pos = fmacro.buf;
4562 fmacro.line = 1;
4563 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004564 fmacro.report_line = 1;
4565 fmacro.report_name = fmacro.basename;
4566 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004567 fmacro.macro = 1;
4568 fmacro.trigraphs = 0;
4569 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004570
Eric Biedermancb364952004-11-15 10:46:44 +00004571 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004572 buf->len = macro->buf_len + 3;
4573 buf->str = xmalloc(buf->len, macro->ident->name);
4574 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004575
Eric Biederman90089602004-05-28 14:11:54 +00004576 fstart = fmacro.pos;
4577 raw_next_token(state, &fmacro, tk);
4578 while(tk->tok != TOK_EOF) {
4579 flen = fmacro.pos - fstart;
4580 switch(tk->tok) {
4581 case TOK_IDENT:
4582 for(i = 0; i < macro->argc; i++) {
4583 if (argv[i].ident == tk->ident) {
4584 break;
4585 }
4586 }
4587 if (i >= macro->argc) {
4588 break;
4589 }
4590 /* Substitute macro parameter */
4591 fstart = argv[i].value;
4592 flen = argv[i].len;
4593 break;
4594 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004595 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004596 break;
4597 }
4598 do {
4599 raw_next_token(state, &fmacro, tk);
4600 } while(tk->tok == TOK_SPACE);
4601 check_tok(state, tk, TOK_IDENT);
4602 for(i = 0; i < macro->argc; i++) {
4603 if (argv[i].ident == tk->ident) {
4604 break;
4605 }
4606 }
4607 if (i >= macro->argc) {
4608 error(state, 0, "parameter `%s' not found",
4609 tk->ident->name);
4610 }
4611 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004612 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004613 for(j = 0; j < argv[i].len; j++) {
4614 char *str = argv[i].value + j;
4615 size_t len = 1;
4616 if (*str == '\\') {
4617 str = "\\";
4618 len = 2;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004619 }
Eric Biederman90089602004-05-28 14:11:54 +00004620 else if (*str == '"') {
4621 str = "\\\"";
4622 len = 2;
4623 }
Eric Biedermancb364952004-11-15 10:46:44 +00004624 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004625 }
Eric Biedermancb364952004-11-15 10:46:44 +00004626 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004627 fstart = 0;
4628 flen = 0;
4629 break;
4630 case TOK_CONCATENATE:
4631 /* Concatenate tokens */
4632 /* Delete the previous whitespace token */
4633 if (buf->str[buf->pos - 1] == ' ') {
4634 buf->pos -= 1;
4635 }
4636 /* Skip the next sequence of whitspace tokens */
4637 do {
4638 fstart = fmacro.pos;
4639 raw_next_token(state, &fmacro, tk);
4640 } while(tk->tok == TOK_SPACE);
4641 /* Restart at the top of the loop.
4642 * I need to process the non white space token.
4643 */
4644 continue;
4645 break;
4646 case TOK_SPACE:
4647 /* Collapse multiple spaces into one */
4648 if (buf->str[buf->pos - 1] != ' ') {
4649 fstart = space;
4650 flen = 1;
4651 } else {
4652 fstart = 0;
4653 flen = 0;
4654 }
4655 break;
4656 default:
4657 break;
4658 }
4659
Eric Biedermancb364952004-11-15 10:46:44 +00004660 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004661
Eric Biederman90089602004-05-28 14:11:54 +00004662 fstart = fmacro.pos;
4663 raw_next_token(state, &fmacro, tk);
4664 }
4665}
4666
4667static void tag_macro_name(struct compile_state *state,
4668 struct macro *macro, struct macro_buf *buf,
4669 struct token *tk)
4670{
4671 /* Guard all instances of the macro name in the replacement
4672 * text from further macro expansion.
4673 */
4674 struct file_state fmacro;
4675 const char *fstart;
4676 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004677
4678 /* Put the old macro expansion buffer in a file */
4679 fmacro.prev = 0;
4680 fmacro.basename = macro->ident->name;
4681 fmacro.dirname = "";
4682 fmacro.buf = buf->str;
4683 fmacro.size = buf->pos;
4684 fmacro.pos = fmacro.buf;
4685 fmacro.line = 1;
4686 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004687 fmacro.report_line = 1;
4688 fmacro.report_name = fmacro.basename;
4689 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004690 fmacro.macro = 1;
4691 fmacro.trigraphs = 0;
4692 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004693
Eric Biedermancb364952004-11-15 10:46:44 +00004694 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004695 buf->len = macro->buf_len + 3;
4696 buf->str = xmalloc(buf->len, macro->ident->name);
4697 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004698
Eric Biederman90089602004-05-28 14:11:54 +00004699 fstart = fmacro.pos;
4700 raw_next_token(state, &fmacro, tk);
4701 while(tk->tok != TOK_EOF) {
4702 flen = fmacro.pos - fstart;
4703 if ((tk->tok == TOK_IDENT) &&
4704 (tk->ident == macro->ident) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004705 (tk->val.notmacro == 0))
Eric Biedermancb364952004-11-15 10:46:44 +00004706 {
4707 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004708 fstart = "$";
4709 flen = 1;
4710 }
4711
Eric Biedermancb364952004-11-15 10:46:44 +00004712 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004713
Eric Biederman90089602004-05-28 14:11:54 +00004714 fstart = fmacro.pos;
4715 raw_next_token(state, &fmacro, tk);
4716 }
4717 xfree(fmacro.buf);
4718}
Eric Biedermancb364952004-11-15 10:46:44 +00004719
Stefan Reinauer14e22772010-04-27 06:56:47 +00004720static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004721 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004722{
4723 struct file_state *file;
4724 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004725 struct macro *macro;
4726 struct macro_arg_value *argv;
4727 struct macro_buf buf;
4728
4729#if 0
4730 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4731#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004732 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004733 macro = ident->sym_define;
4734
4735 /* If this token comes from a macro expansion ignore it */
4736 if (tk->val.notmacro) {
4737 return 0;
4738 }
4739 /* If I am a function like macro and the identifier is not followed
4740 * by a left parenthesis, do nothing.
4741 */
Eric Biedermancb364952004-11-15 10:46:44 +00004742 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004743 return 0;
4744 }
4745
4746 /* Read in the macro arguments */
4747 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004748 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004749 raw_next_token(state, *filep, tk);
4750 check_tok(state, tk, TOK_LPAREN);
4751
4752 argv = read_macro_args(state, macro, *filep, tk);
4753
4754 check_tok(state, tk, TOK_RPAREN);
4755 }
4756 /* Macro expand the macro arguments */
4757 macro_expand_args(state, macro, argv, tk);
4758
4759 buf.str = 0;
4760 buf.len = 0;
4761 buf.pos = 0;
4762 if (ident == state->i___FILE__) {
4763 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4764 buf.str = xmalloc(buf.len, ident->name);
4765 sprintf(buf.str, "\"%s\"", state->file->basename);
4766 buf.pos = strlen(buf.str);
4767 }
4768 else if (ident == state->i___LINE__) {
4769 buf.len = 30;
4770 buf.str = xmalloc(buf.len, ident->name);
4771 sprintf(buf.str, "%d", state->file->line);
4772 buf.pos = strlen(buf.str);
4773 }
4774 else {
4775 expand_macro(state, macro, &buf, argv, tk);
4776 }
4777 /* Tag the macro name with a $ so it will no longer
4778 * be regonized as a canidate for macro expansion.
4779 */
4780 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004781
4782#if 0
4783 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4784 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4785#endif
4786
4787 free_macro_args(macro, argv);
4788
Eric Biedermanb138ac82003-04-22 18:44:01 +00004789 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004790 file->prev = *filep;
4791 file->basename = xstrdup(ident->name);
4792 file->dirname = xstrdup("");
4793 file->buf = buf.str;
4794 file->size = buf.pos;
4795 file->pos = file->buf;
4796 file->line = 1;
4797 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004798 file->report_line = 1;
4799 file->report_name = file->basename;
4800 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004801 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004802 file->trigraphs = 0;
4803 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004804 *filep = file;
4805 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004806}
4807
Eric Biederman90089602004-05-28 14:11:54 +00004808static void eat_tokens(struct compile_state *state, int targ_tok)
4809{
4810 if (state->eat_depth > 0) {
4811 internal_error(state, 0, "Already eating...");
4812 }
4813 state->eat_depth = state->if_depth;
4814 state->eat_targ = targ_tok;
4815}
4816static int if_eat(struct compile_state *state)
4817{
4818 return state->eat_depth > 0;
4819}
4820static int if_value(struct compile_state *state)
4821{
4822 int index, offset;
4823 index = state->if_depth / CHAR_BIT;
4824 offset = state->if_depth % CHAR_BIT;
4825 return !!(state->if_bytes[index] & (1 << (offset)));
4826}
Stefan Reinauer14e22772010-04-27 06:56:47 +00004827static void set_if_value(struct compile_state *state, int value)
Eric Biederman90089602004-05-28 14:11:54 +00004828{
4829 int index, offset;
4830 index = state->if_depth / CHAR_BIT;
4831 offset = state->if_depth % CHAR_BIT;
4832
4833 state->if_bytes[index] &= ~(1 << offset);
4834 if (value) {
4835 state->if_bytes[index] |= (1 << offset);
4836 }
4837}
4838static void in_if(struct compile_state *state, const char *name)
4839{
4840 if (state->if_depth <= 0) {
4841 error(state, 0, "%s without #if", name);
4842 }
4843}
4844static void enter_if(struct compile_state *state)
4845{
4846 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004847 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004848 error(state, 0, "#if depth too great");
4849 }
4850}
4851static void reenter_if(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 enter_else(struct compile_state *state, const char *name)
4861{
4862 in_if(state, name);
4863 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004864 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004865 state->eat_depth = 0;
4866 state->eat_targ = 0;
4867 }
4868}
4869static void exit_if(struct compile_state *state, const char *name)
4870{
4871 in_if(state, name);
4872 if (state->eat_depth == state->if_depth) {
4873 state->eat_depth = 0;
4874 state->eat_targ = 0;
4875 }
4876 state->if_depth -= 1;
4877}
4878
Eric Biedermancb364952004-11-15 10:46:44 +00004879static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004880{
4881 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004882 int rescan;
4883
Eric Biedermancb364952004-11-15 10:46:44 +00004884 file = state->file;
4885 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004886 do {
4887 rescan = 0;
4888 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004889 /* Exit out of an include directive or macro call */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004890 if ((tk->tok == TOK_EOF) &&
4891 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004892 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004893 state->file = file->prev;
4894 /* file->basename is used keep it */
4895 xfree(file->dirname);
4896 xfree(file->buf);
4897 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004898 file = 0;
4899 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004900 rescan = 1;
4901 }
Eric Biederman41203d92004-11-08 09:31:09 +00004902 } while(rescan);
4903}
4904
Eric Biedermancb364952004-11-15 10:46:44 +00004905static void pp_token(struct compile_state *state, struct token *tk)
4906{
Eric Biedermancb364952004-11-15 10:46:44 +00004907 int rescan;
4908
4909 raw_token(state, tk);
4910 do {
4911 rescan = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004912 if (tk->tok == TOK_SPACE) {
4913 raw_token(state, tk);
4914 rescan = 1;
4915 }
4916 else if (tk->tok == TOK_IDENT) {
4917 if (state->token_base == 0) {
4918 ident_to_keyword(state, tk);
4919 } else {
4920 ident_to_macro(state, tk);
4921 }
4922 }
4923 } while(rescan);
4924}
4925
Eric Biederman41203d92004-11-08 09:31:09 +00004926static void preprocess(struct compile_state *state, struct token *tk);
4927
4928static void token(struct compile_state *state, struct token *tk)
4929{
4930 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004931 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004932 do {
4933 rescan = 0;
4934 /* Process a macro directive */
4935 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004936 /* Only match preprocessor directives at the start of a line */
4937 const char *ptr;
4938 ptr = state->file->line_start;
4939 while((ptr < tk->pos)
4940 && spacep(get_char(state->file, ptr)))
4941 {
4942 ptr = next_char(state->file, ptr, 1);
4943 }
4944 if (ptr == tk->pos) {
4945 preprocess(state, tk);
4946 rescan = 1;
4947 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004948 }
Eric Biederman41203d92004-11-08 09:31:09 +00004949 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004950 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004951 rescan = compile_macro(state, &state->file, tk);
4952 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004953 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004954 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004955 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004956 /* Eat tokens disabled by the preprocessor
4957 * (Unless we are parsing a preprocessor directive
Eric Biedermancb364952004-11-15 10:46:44 +00004958 */
Eric Biedermana649a412004-11-09 00:35:39 +00004959 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004960 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004961 rescan = 1;
4962 }
Eric Biedermana649a412004-11-09 00:35:39 +00004963 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004964 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004965 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004966 rescan = 1;
4967 }
Eric Biedermancb364952004-11-15 10:46:44 +00004968 /* Error on unknown tokens */
4969 else if (tk->tok == TOK_UNKNOWN) {
4970 error(state, 0, "unknown token");
4971 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004972 } while(rescan);
4973}
4974
Eric Biederman41203d92004-11-08 09:31:09 +00004975
4976static inline struct token *get_token(struct compile_state *state, int offset)
4977{
4978 int index;
4979 index = state->token_base + offset;
4980 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4981 internal_error(state, 0, "token array to small");
4982 }
4983 return &state->token[index];
4984}
4985
4986static struct token *do_eat_token(struct compile_state *state, int tok)
4987{
4988 struct token *tk;
4989 int i;
4990 check_tok(state, get_token(state, 1), tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004991
Eric Biederman41203d92004-11-08 09:31:09 +00004992 /* Free the old token value */
4993 tk = get_token(state, 0);
4994 if (tk->str_len) {
4995 memset((void *)tk->val.str, -1, tk->str_len);
4996 xfree(tk->val.str);
4997 }
4998 /* Overwrite the old token with newer tokens */
4999 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
5000 state->token[i] = state->token[i + 1];
5001 }
5002 /* Clear the last token */
5003 memset(&state->token[i], 0, sizeof(state->token[i]));
5004 state->token[i].tok = -1;
5005
5006 /* Return the token */
5007 return tk;
5008}
5009
Eric Biedermancb364952004-11-15 10:46:44 +00005010static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005011{
5012 struct token *tk1;
5013 tk1 = get_token(state, 1);
5014 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005015 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005016 }
5017 return tk1->tok;
5018}
5019
Eric Biedermancb364952004-11-15 10:46:44 +00005020static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005021{
Eric Biedermancb364952004-11-15 10:46:44 +00005022 raw_peek(state);
5023 return do_eat_token(state, tok);
5024}
5025
5026static int pp_peek(struct compile_state *state)
5027{
5028 struct token *tk1;
5029 tk1 = get_token(state, 1);
5030 if (tk1->tok == -1) {
5031 pp_token(state, tk1);
5032 }
5033 return tk1->tok;
5034}
5035
5036static struct token *pp_eat(struct compile_state *state, int tok)
5037{
5038 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005039 return do_eat_token(state, tok);
5040}
5041
Eric Biedermanb138ac82003-04-22 18:44:01 +00005042static int peek(struct compile_state *state)
5043{
Eric Biederman41203d92004-11-08 09:31:09 +00005044 struct token *tk1;
5045 tk1 = get_token(state, 1);
5046 if (tk1->tok == -1) {
5047 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005048 }
Eric Biederman41203d92004-11-08 09:31:09 +00005049 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005050}
5051
5052static int peek2(struct compile_state *state)
5053{
Eric Biederman41203d92004-11-08 09:31:09 +00005054 struct token *tk1, *tk2;
5055 tk1 = get_token(state, 1);
5056 tk2 = get_token(state, 2);
5057 if (tk1->tok == -1) {
5058 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005059 }
Eric Biederman41203d92004-11-08 09:31:09 +00005060 if (tk2->tok == -1) {
5061 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005062 }
Eric Biederman41203d92004-11-08 09:31:09 +00005063 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005064}
5065
Eric Biederman41203d92004-11-08 09:31:09 +00005066static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005067{
Eric Biederman90089602004-05-28 14:11:54 +00005068 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005069 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005070}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005071
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005072static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005073{
Eric Biederman90089602004-05-28 14:11:54 +00005074 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005075 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005076 int subdir_len;
5077 struct file_state *file;
5078 char *basename;
5079 file = xmalloc(sizeof(*file), "file_state");
5080
5081 base = strrchr(filename, '/');
5082 subdir = filename;
5083 if (base != 0) {
5084 subdir_len = base - filename;
5085 base++;
5086 }
5087 else {
5088 base = filename;
5089 subdir_len = 0;
5090 }
5091 basename = xmalloc(strlen(base) +1, "basename");
5092 strcpy(basename, base);
5093 file->basename = basename;
5094
5095 if (getcwd(cwd, sizeof(cwd)) == 0) {
5096 die("cwd buffer to small");
5097 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005098 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005099 file->dirname = xmalloc(subdir_len + 1, "dirname");
5100 memcpy(file->dirname, subdir, subdir_len);
5101 file->dirname[subdir_len] = '\0';
5102 }
5103 else {
Eric Biederman90089602004-05-28 14:11:54 +00005104 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005105 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005106 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005107 /* Find the appropriate directory... */
5108 dir = 0;
5109 if (!state->file && exists(cwd, filename)) {
5110 dir = cwd;
5111 }
5112 if (local && state->file && exists(state->file->dirname, filename)) {
5113 dir = state->file->dirname;
5114 }
Eric Biederman90089602004-05-28 14:11:54 +00005115 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005116 if (exists(*path, filename)) {
5117 dir = *path;
5118 }
5119 }
5120 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005121 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005122 }
5123 dirlen = strlen(dir);
5124 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5125 memcpy(file->dirname, dir, dirlen);
5126 file->dirname[dirlen] = '/';
5127 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5128 file->dirname[dirlen + 1 + subdir_len] = '\0';
5129 }
5130 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005131
5132 file->pos = file->buf;
5133 file->line_start = file->pos;
5134 file->line = 1;
5135
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005136 file->report_line = 1;
5137 file->report_name = file->basename;
5138 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005139 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005140 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5141 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005142
Eric Biedermanb138ac82003-04-22 18:44:01 +00005143 file->prev = state->file;
5144 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005145}
5146
Eric Biederman41203d92004-11-08 09:31:09 +00005147static struct triple *constant_expr(struct compile_state *state);
5148static void integral(struct compile_state *state, struct triple *def);
5149
5150static int mcexpr(struct compile_state *state)
5151{
5152 struct triple *cvalue;
5153 cvalue = constant_expr(state);
5154 integral(state, cvalue);
5155 if (cvalue->op != OP_INTCONST) {
5156 error(state, 0, "integer constant expected");
5157 }
5158 return cvalue->u.cval != 0;
5159}
5160
5161static void preprocess(struct compile_state *state, struct token *current_token)
5162{
5163 /* Doing much more with the preprocessor would require
5164 * a parser and a major restructuring.
5165 * Postpone that for later.
5166 */
Eric Biederman41203d92004-11-08 09:31:09 +00005167 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005168 int tok;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005169
Eric Biedermancb364952004-11-15 10:46:44 +00005170 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005171
5172 old_token_base = state->token_base;
5173 state->token_base = current_token - state->token;
5174
Eric Biedermancb364952004-11-15 10:46:44 +00005175 tok = pp_peek(state);
5176 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005177 case TOK_LIT_INT:
5178 {
5179 struct token *tk;
5180 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005181 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005182 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005183 /* I have a preprocessor line marker parse it */
5184 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005185 const char *token, *base;
5186 char *name, *dir;
5187 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005188 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005189 name = xmalloc(tk->str_len, "report_name");
5190 token = tk->val.str + 1;
5191 base = strrchr(token, '/');
5192 name_len = tk->str_len -2;
5193 if (base != 0) {
5194 dir_len = base - token;
5195 base++;
5196 name_len -= base - token;
5197 } else {
5198 dir_len = 0;
5199 base = token;
5200 }
5201 memcpy(name, base, name_len);
5202 name[name_len] = '\0';
5203 dir = xmalloc(dir_len + 1, "report_dir");
5204 memcpy(dir, token, dir_len);
5205 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005206 state->file->report_line = override_line - 1;
5207 state->file->report_name = name;
5208 state->file->report_dir = dir;
5209 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005210 }
5211 break;
5212 }
5213 case TOK_MLINE:
5214 {
5215 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005216 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005217 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005218 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5219 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005220 const char *token, *base;
5221 char *name, *dir;
5222 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005223 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005224 name = xmalloc(tk->str_len, "report_name");
5225 token = tk->val.str + 1;
5226 base = strrchr(token, '/');
5227 name_len = tk->str_len - 2;
5228 if (base != 0) {
5229 dir_len = base - token;
5230 base++;
5231 name_len -= base - token;
5232 } else {
5233 dir_len = 0;
5234 base = token;
5235 }
5236 memcpy(name, base, name_len);
5237 name[name_len] = '\0';
5238 dir = xmalloc(dir_len + 1, "report_dir");
5239 memcpy(dir, token, dir_len);
5240 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005241 state->file->report_name = name;
5242 state->file->report_dir = dir;
5243 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005244 }
5245 break;
5246 }
5247 case TOK_MUNDEF:
5248 {
5249 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005250 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005251 if (if_eat(state)) /* quit early when #if'd out */
5252 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005253
Eric Biedermancb364952004-11-15 10:46:44 +00005254 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005255
5256 undef_macro(state, ident);
5257 break;
5258 }
5259 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005260 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005261 if (if_eat(state)) /* quit early when #if'd out */
5262 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005263 warning(state, 0, "Ignoring pragma");
Eric Biederman41203d92004-11-08 09:31:09 +00005264 break;
5265 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005266 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005267 reenter_if(state, "#elif");
5268 if (if_eat(state)) /* quit early when #if'd out */
5269 break;
5270 /* If the #if was taken the #elif just disables the following code */
5271 if (if_value(state)) {
5272 eat_tokens(state, TOK_MENDIF);
5273 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005274 /* If the previous #if was not taken see if the #elif enables the
Eric Biederman41203d92004-11-08 09:31:09 +00005275 * trailing code.
5276 */
5277 else {
5278 set_if_value(state, mcexpr(state));
5279 if (!if_value(state)) {
5280 eat_tokens(state, TOK_MELSE);
5281 }
5282 }
5283 break;
5284 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005285 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005286 enter_if(state);
5287 if (if_eat(state)) /* quit early when #if'd out */
5288 break;
5289 set_if_value(state, mcexpr(state));
5290 if (!if_value(state)) {
5291 eat_tokens(state, TOK_MELSE);
5292 }
5293 break;
5294 case TOK_MIFNDEF:
5295 {
5296 struct hash_entry *ident;
5297
Eric Biedermancb364952004-11-15 10:46:44 +00005298 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005299 enter_if(state);
5300 if (if_eat(state)) /* quit early when #if'd out */
5301 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005302 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005303 set_if_value(state, ident->sym_define == 0);
5304 if (!if_value(state)) {
5305 eat_tokens(state, TOK_MELSE);
5306 }
5307 break;
5308 }
5309 case TOK_MIFDEF:
5310 {
5311 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005312 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005313 enter_if(state);
5314 if (if_eat(state)) /* quit early when #if'd out */
5315 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005316 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005317 set_if_value(state, ident->sym_define != 0);
5318 if (!if_value(state)) {
5319 eat_tokens(state, TOK_MELSE);
5320 }
5321 break;
5322 }
5323 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005324 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005325 enter_else(state, "#else");
5326 if (!if_eat(state) && if_value(state)) {
5327 eat_tokens(state, TOK_MENDIF);
5328 }
5329 break;
5330 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005331 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005332 exit_if(state, "#endif");
5333 break;
5334 case TOK_MDEFINE:
5335 {
5336 struct hash_entry *ident;
5337 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005338 const char *mstart, *mend;
5339 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005340
Eric Biedermancb364952004-11-15 10:46:44 +00005341 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005342 if (if_eat(state)) /* quit early when #if'd out */
5343 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005344 ident = pp_eat(state, TOK_MIDENT)->ident;
5345 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005346 args = 0;
5347 larg = &args;
5348
Eric Biederman41203d92004-11-08 09:31:09 +00005349 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005350 if (raw_peek(state) == TOK_LPAREN) {
5351 raw_eat(state, TOK_LPAREN);
5352 argc += 1;
5353
Eric Biederman41203d92004-11-08 09:31:09 +00005354 for(;;) {
5355 struct macro_arg *narg, *arg;
5356 struct hash_entry *aident;
5357 int tok;
5358
Eric Biedermancb364952004-11-15 10:46:44 +00005359 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005360 if (!args && (tok == TOK_RPAREN)) {
5361 break;
5362 }
5363 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005364 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005365 aident = state->i___VA_ARGS__;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005366 }
Eric Biederman41203d92004-11-08 09:31:09 +00005367 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005368 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005369 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005370
Eric Biederman41203d92004-11-08 09:31:09 +00005371 narg = xcmalloc(sizeof(*arg), "macro arg");
5372 narg->ident = aident;
5373
5374 /* Verify I don't have a duplicate identifier */
5375 for(arg = args; arg; arg = arg->next) {
5376 if (arg->ident == narg->ident) {
5377 error(state, 0, "Duplicate macro arg `%s'",
5378 narg->ident->name);
5379 }
5380 }
5381 /* Add the new argument to the end of the list */
5382 *larg = narg;
5383 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005384 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005385
5386 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005387 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005388 break;
5389 }
Eric Biedermancb364952004-11-15 10:46:44 +00005390 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005391 }
Eric Biedermancb364952004-11-15 10:46:44 +00005392 pp_eat(state, TOK_RPAREN);
5393 }
5394 /* Remove leading whitespace */
5395 while(raw_peek(state) == TOK_SPACE) {
5396 raw_eat(state, TOK_SPACE);
5397 }
Eric Biederman41203d92004-11-08 09:31:09 +00005398
Eric Biedermancb364952004-11-15 10:46:44 +00005399 /* Remember the start of the macro body */
5400 tok = raw_peek(state);
5401 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005402
Eric Biedermancb364952004-11-15 10:46:44 +00005403 /* Find the end of the macro */
5404 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5405 raw_eat(state, tok);
5406 /* Remember the end of the last non space token */
5407 raw_peek(state);
5408 if (tok != TOK_SPACE) {
5409 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005410 }
5411 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005412
Eric Biedermancb364952004-11-15 10:46:44 +00005413 /* Now that I have found the body defined the token */
5414 do_define_macro(state, ident,
5415 char_strdup(state->file, mstart, mend, "macro buf"),
5416 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005417 break;
5418 }
5419 case TOK_MERROR:
5420 {
Eric Biedermancb364952004-11-15 10:46:44 +00005421 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005422 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005423
Eric Biedermancb364952004-11-15 10:46:44 +00005424 pp_eat(state, TOK_MERROR);
5425 /* Find the start of the line */
5426 raw_peek(state);
5427 start = get_token(state, 1)->pos;
5428
Eric Biederman41203d92004-11-08 09:31:09 +00005429 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005430 while((tok = raw_peek(state)) != TOK_EOL) {
5431 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005432 }
Eric Biedermancb364952004-11-15 10:46:44 +00005433 end = get_token(state, 1)->pos;
5434 len = end - start;
5435 if (!if_eat(state)) {
5436 error(state, 0, "%*.*s", len, len, start);
5437 }
Eric Biederman41203d92004-11-08 09:31:09 +00005438 break;
5439 }
5440 case TOK_MWARNING:
5441 {
Eric Biedermancb364952004-11-15 10:46:44 +00005442 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005443 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005444
Eric Biedermancb364952004-11-15 10:46:44 +00005445 pp_eat(state, TOK_MWARNING);
5446
5447 /* Find the start of the line */
5448 raw_peek(state);
5449 start = get_token(state, 1)->pos;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005450
Eric Biederman41203d92004-11-08 09:31:09 +00005451 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005452 while((tok = raw_peek(state)) != TOK_EOL) {
5453 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005454 }
Eric Biedermancb364952004-11-15 10:46:44 +00005455 end = get_token(state, 1)->pos;
5456 len = end - start;
5457 if (!if_eat(state)) {
5458 warning(state, 0, "%*.*s", len, len, start);
5459 }
Eric Biederman41203d92004-11-08 09:31:09 +00005460 break;
5461 }
5462 case TOK_MINCLUDE:
5463 {
5464 char *name;
5465 int local;
5466 local = 0;
5467 name = 0;
5468
Eric Biedermancb364952004-11-15 10:46:44 +00005469 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005470 if (if_eat(state)) {
5471 /* Find the end of the line */
5472 while((tok = raw_peek(state)) != TOK_EOL) {
5473 raw_eat(state, tok);
5474 }
5475 break;
5476 }
Eric Biederman41203d92004-11-08 09:31:09 +00005477 tok = peek(state);
5478 if (tok == TOK_LIT_STRING) {
5479 struct token *tk;
5480 const char *token;
5481 int name_len;
5482 tk = eat(state, TOK_LIT_STRING);
5483 name = xmalloc(tk->str_len, "include");
5484 token = tk->val.str +1;
5485 name_len = tk->str_len -2;
5486 if (*token == '"') {
5487 token++;
5488 name_len--;
5489 }
5490 memcpy(name, token, name_len);
5491 name[name_len] = '\0';
5492 local = 1;
5493 }
5494 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005495 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005496 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005497
5498 buf.len = 40;
5499 buf.str = xmalloc(buf.len, "include");
5500 buf.pos = 0;
5501
5502 tok = peek(state);
5503 while((tok != TOK_MORE) &&
5504 (tok != TOK_EOL) && (tok != TOK_EOF))
5505 {
5506 struct token *tk;
5507 tk = eat(state, tok);
5508 append_macro_chars(state, "include", &buf,
5509 state->file, tk->pos, state->file->pos);
5510 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005511 }
Eric Biedermancb364952004-11-15 10:46:44 +00005512 append_macro_text(state, "include", &buf, "\0", 1);
5513 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005514 error(state, 0, "Unterminated include directive");
5515 }
Eric Biederman41203d92004-11-08 09:31:09 +00005516 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005517 local = 0;
5518 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005519 }
5520 else {
5521 error(state, 0, "Invalid include directive");
5522 }
5523 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005524 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005525 error(state, 0, "garbage after include directive");
5526 }
5527 if (!if_eat(state)) {
5528 compile_file(state, name, local);
5529 }
5530 xfree(name);
5531 break;
5532 }
5533 case TOK_EOL:
5534 /* Ignore # without a follwing ident */
5535 break;
5536 default:
5537 {
5538 const char *name1, *name2;
5539 name1 = tokens[tok];
5540 name2 = "";
5541 if (tok == TOK_MIDENT) {
5542 name2 = get_token(state, 1)->ident->name;
5543 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005544 error(state, 0, "Invalid preprocessor directive: %s %s",
Eric Biederman41203d92004-11-08 09:31:09 +00005545 name1, name2);
5546 break;
5547 }
5548 }
5549 /* Consume the rest of the macro line */
5550 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005551 tok = pp_peek(state);
5552 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005553 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5554 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005555 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005556 return;
5557}
5558
Eric Biederman0babc1c2003-05-09 02:39:00 +00005559/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005560
5561static struct type *new_type(
5562 unsigned int type, struct type *left, struct type *right)
5563{
5564 struct type *result;
5565 result = xmalloc(sizeof(*result), "type");
5566 result->type = type;
5567 result->left = left;
5568 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005569 result->field_ident = 0;
5570 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005571 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005572 return result;
5573}
5574
5575static struct type *clone_type(unsigned int specifiers, struct type *old)
5576{
5577 struct type *result;
5578 result = xmalloc(sizeof(*result), "type");
5579 memcpy(result, old, sizeof(*result));
5580 result->type &= TYPE_MASK;
5581 result->type |= specifiers;
5582 return result;
5583}
5584
Eric Biederman90089602004-05-28 14:11:54 +00005585static struct type *dup_type(struct compile_state *state, struct type *orig)
5586{
5587 struct type *new;
5588 new = xcmalloc(sizeof(*new), "type");
5589 new->type = orig->type;
5590 new->field_ident = orig->field_ident;
5591 new->type_ident = orig->type_ident;
5592 new->elements = orig->elements;
5593 if (orig->left) {
5594 new->left = dup_type(state, orig->left);
5595 }
5596 if (orig->right) {
5597 new->right = dup_type(state, orig->right);
5598 }
5599 return new;
5600}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005601
Eric Biederman90089602004-05-28 14:11:54 +00005602
5603static struct type *invalid_type(struct compile_state *state, struct type *type)
5604{
5605 struct type *invalid, *member;
5606 invalid = 0;
5607 if (!type) {
5608 internal_error(state, 0, "type missing?");
5609 }
5610 switch(type->type & TYPE_MASK) {
5611 case TYPE_VOID:
5612 case TYPE_CHAR: case TYPE_UCHAR:
5613 case TYPE_SHORT: case TYPE_USHORT:
5614 case TYPE_INT: case TYPE_UINT:
5615 case TYPE_LONG: case TYPE_ULONG:
5616 case TYPE_LLONG: case TYPE_ULLONG:
5617 case TYPE_POINTER:
5618 case TYPE_ENUM:
5619 break;
5620 case TYPE_BITFIELD:
5621 invalid = invalid_type(state, type->left);
5622 break;
5623 case TYPE_ARRAY:
5624 invalid = invalid_type(state, type->left);
5625 break;
5626 case TYPE_STRUCT:
5627 case TYPE_TUPLE:
5628 member = type->left;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005629 while(member && (invalid == 0) &&
Eric Biederman90089602004-05-28 14:11:54 +00005630 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5631 invalid = invalid_type(state, member->left);
5632 member = member->right;
5633 }
5634 if (!invalid) {
5635 invalid = invalid_type(state, member);
5636 }
5637 break;
5638 case TYPE_UNION:
5639 case TYPE_JOIN:
5640 member = type->left;
5641 while(member && (invalid == 0) &&
5642 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5643 invalid = invalid_type(state, member->left);
5644 member = member->right;
5645 }
5646 if (!invalid) {
5647 invalid = invalid_type(state, member);
5648 }
5649 break;
5650 default:
5651 invalid = type;
5652 break;
5653 }
5654 return invalid;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005655
Eric Biederman90089602004-05-28 14:11:54 +00005656}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005657
Eric Biederman90089602004-05-28 14:11:54 +00005658static struct type void_type = { .type = TYPE_VOID };
5659static struct type char_type = { .type = TYPE_CHAR };
5660static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005661#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005662static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005663#endif
Eric Biederman90089602004-05-28 14:11:54 +00005664static struct type ushort_type = { .type = TYPE_USHORT };
5665static struct type int_type = { .type = TYPE_INT };
5666static struct type uint_type = { .type = TYPE_UINT };
5667static struct type long_type = { .type = TYPE_LONG };
5668static struct type ulong_type = { .type = TYPE_ULONG };
5669static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005670
Eric Biederman5ade04a2003-10-22 04:03:46 +00005671static struct type void_ptr_type = {
5672 .type = TYPE_POINTER,
5673 .left = &void_type,
5674};
5675
Stefan Reinauer50542a82007-10-24 11:14:14 +00005676#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005677static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005678 .type = TYPE_FUNCTION,
5679 .left = &void_type,
5680 .right = &void_type,
5681};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005682#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005683
Eric Biederman90089602004-05-28 14:11:54 +00005684static size_t bits_to_bytes(size_t size)
5685{
5686 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5687}
5688
Eric Biedermanb138ac82003-04-22 18:44:01 +00005689static struct triple *variable(struct compile_state *state, struct type *type)
5690{
5691 struct triple *result;
5692 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005693 result = triple(state, OP_ADECL, type, 0, 0);
5694 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005695 }
5696 else {
5697 result = triple(state, OP_SDECL, type, 0, 0);
5698 }
5699 return result;
5700}
5701
5702static void stor_of(FILE *fp, struct type *type)
5703{
5704 switch(type->type & STOR_MASK) {
5705 case STOR_AUTO:
5706 fprintf(fp, "auto ");
5707 break;
5708 case STOR_STATIC:
5709 fprintf(fp, "static ");
5710 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005711 case STOR_LOCAL:
5712 fprintf(fp, "local ");
5713 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005714 case STOR_EXTERN:
5715 fprintf(fp, "extern ");
5716 break;
5717 case STOR_REGISTER:
5718 fprintf(fp, "register ");
5719 break;
5720 case STOR_TYPEDEF:
5721 fprintf(fp, "typedef ");
5722 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005723 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005724 fprintf(fp, "inline ");
5725 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005726 case STOR_INLINE | STOR_STATIC:
5727 fprintf(fp, "static inline");
5728 break;
5729 case STOR_INLINE | STOR_EXTERN:
5730 fprintf(fp, "extern inline");
5731 break;
5732 default:
5733 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5734 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005735 }
5736}
5737static void qual_of(FILE *fp, struct type *type)
5738{
5739 if (type->type & QUAL_CONST) {
5740 fprintf(fp, " const");
5741 }
5742 if (type->type & QUAL_VOLATILE) {
5743 fprintf(fp, " volatile");
5744 }
5745 if (type->type & QUAL_RESTRICT) {
5746 fprintf(fp, " restrict");
5747 }
5748}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005749
Eric Biedermanb138ac82003-04-22 18:44:01 +00005750static void name_of(FILE *fp, struct type *type)
5751{
Eric Biederman90089602004-05-28 14:11:54 +00005752 unsigned int base_type;
5753 base_type = type->type & TYPE_MASK;
5754 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5755 stor_of(fp, type);
5756 }
5757 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005758 case TYPE_VOID:
5759 fprintf(fp, "void");
5760 qual_of(fp, type);
5761 break;
5762 case TYPE_CHAR:
5763 fprintf(fp, "signed char");
5764 qual_of(fp, type);
5765 break;
5766 case TYPE_UCHAR:
5767 fprintf(fp, "unsigned char");
5768 qual_of(fp, type);
5769 break;
5770 case TYPE_SHORT:
5771 fprintf(fp, "signed short");
5772 qual_of(fp, type);
5773 break;
5774 case TYPE_USHORT:
5775 fprintf(fp, "unsigned short");
5776 qual_of(fp, type);
5777 break;
5778 case TYPE_INT:
5779 fprintf(fp, "signed int");
5780 qual_of(fp, type);
5781 break;
5782 case TYPE_UINT:
5783 fprintf(fp, "unsigned int");
5784 qual_of(fp, type);
5785 break;
5786 case TYPE_LONG:
5787 fprintf(fp, "signed long");
5788 qual_of(fp, type);
5789 break;
5790 case TYPE_ULONG:
5791 fprintf(fp, "unsigned long");
5792 qual_of(fp, type);
5793 break;
5794 case TYPE_POINTER:
5795 name_of(fp, type->left);
5796 fprintf(fp, " * ");
5797 qual_of(fp, type);
5798 break;
5799 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005800 name_of(fp, type->left);
5801 fprintf(fp, ", ");
5802 name_of(fp, type->right);
5803 break;
Eric Biederman90089602004-05-28 14:11:54 +00005804 case TYPE_OVERLAP:
5805 name_of(fp, type->left);
5806 fprintf(fp, ",| ");
5807 name_of(fp, type->right);
5808 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005809 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005810 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005811 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005812 qual_of(fp, type);
5813 break;
5814 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005815 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005816 (type->type_ident)? type->type_ident->name : "");
5817 name_of(fp, type->left);
5818 fprintf(fp, " } ");
5819 qual_of(fp, type);
5820 break;
5821 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005822 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005823 (type->type_ident)? type->type_ident->name : "");
5824 name_of(fp, type->left);
5825 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005826 qual_of(fp, type);
5827 break;
5828 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005829 name_of(fp, type->left);
5830 fprintf(fp, " (*)(");
5831 name_of(fp, type->right);
5832 fprintf(fp, ")");
5833 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005834 case TYPE_ARRAY:
5835 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005836 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005837 break;
Eric Biederman90089602004-05-28 14:11:54 +00005838 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005839 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005840 name_of(fp, type->left);
5841 fprintf(fp, " } ");
5842 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005843 break;
Eric Biederman90089602004-05-28 14:11:54 +00005844 case TYPE_JOIN:
5845 fprintf(fp, "join { ");
5846 name_of(fp, type->left);
5847 fprintf(fp, " } ");
5848 qual_of(fp, type);
5849 break;
5850 case TYPE_BITFIELD:
5851 name_of(fp, type->left);
5852 fprintf(fp, " : %d ", type->elements);
5853 qual_of(fp, type);
5854 break;
5855 case TYPE_UNKNOWN:
5856 fprintf(fp, "unknown_t");
5857 break;
5858 default:
5859 fprintf(fp, "????: %x", base_type);
5860 break;
5861 }
5862 if (type->field_ident && type->field_ident->name) {
5863 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005864 }
5865}
5866
5867static size_t align_of(struct compile_state *state, struct type *type)
5868{
5869 size_t align;
5870 align = 0;
5871 switch(type->type & TYPE_MASK) {
5872 case TYPE_VOID:
5873 align = 1;
5874 break;
Eric Biederman90089602004-05-28 14:11:54 +00005875 case TYPE_BITFIELD:
5876 align = 1;
5877 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005878 case TYPE_CHAR:
5879 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005880 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005881 break;
5882 case TYPE_SHORT:
5883 case TYPE_USHORT:
5884 align = ALIGNOF_SHORT;
5885 break;
5886 case TYPE_INT:
5887 case TYPE_UINT:
5888 case TYPE_ENUM:
5889 align = ALIGNOF_INT;
5890 break;
5891 case TYPE_LONG:
5892 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005893 align = ALIGNOF_LONG;
5894 break;
Eric Biederman90089602004-05-28 14:11:54 +00005895 case TYPE_POINTER:
5896 align = ALIGNOF_POINTER;
5897 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005898 case TYPE_PRODUCT:
5899 case TYPE_OVERLAP:
5900 {
5901 size_t left_align, right_align;
5902 left_align = align_of(state, type->left);
5903 right_align = align_of(state, type->right);
5904 align = (left_align >= right_align) ? left_align : right_align;
5905 break;
5906 }
5907 case TYPE_ARRAY:
5908 align = align_of(state, type->left);
5909 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005910 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005911 case TYPE_TUPLE:
5912 case TYPE_UNION:
5913 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005914 align = align_of(state, type->left);
5915 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005916 default:
5917 error(state, 0, "alignof not yet defined for type\n");
5918 break;
5919 }
5920 return align;
5921}
5922
Eric Biederman90089602004-05-28 14:11:54 +00005923static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005924{
Eric Biederman90089602004-05-28 14:11:54 +00005925 size_t align;
5926 align = 0;
5927 switch(type->type & TYPE_MASK) {
5928 case TYPE_VOID:
5929 align = 1;
5930 break;
5931 case TYPE_BITFIELD:
5932 align = 1;
5933 break;
5934 case TYPE_CHAR:
5935 case TYPE_UCHAR:
5936 align = REG_ALIGNOF_CHAR;
5937 break;
5938 case TYPE_SHORT:
5939 case TYPE_USHORT:
5940 align = REG_ALIGNOF_SHORT;
5941 break;
5942 case TYPE_INT:
5943 case TYPE_UINT:
5944 case TYPE_ENUM:
5945 align = REG_ALIGNOF_INT;
5946 break;
5947 case TYPE_LONG:
5948 case TYPE_ULONG:
5949 align = REG_ALIGNOF_LONG;
5950 break;
5951 case TYPE_POINTER:
5952 align = REG_ALIGNOF_POINTER;
5953 break;
5954 case TYPE_PRODUCT:
5955 case TYPE_OVERLAP:
5956 {
5957 size_t left_align, right_align;
5958 left_align = reg_align_of(state, type->left);
5959 right_align = reg_align_of(state, type->right);
5960 align = (left_align >= right_align) ? left_align : right_align;
5961 break;
5962 }
5963 case TYPE_ARRAY:
5964 align = reg_align_of(state, type->left);
5965 break;
5966 case TYPE_STRUCT:
5967 case TYPE_UNION:
5968 case TYPE_TUPLE:
5969 case TYPE_JOIN:
5970 align = reg_align_of(state, type->left);
5971 break;
5972 default:
5973 error(state, 0, "alignof not yet defined for type\n");
5974 break;
5975 }
5976 return align;
5977}
5978
5979static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5980{
5981 return bits_to_bytes(align_of(state, type));
5982}
5983static size_t size_of(struct compile_state *state, struct type *type);
5984static size_t reg_size_of(struct compile_state *state, struct type *type);
5985
Stefan Reinauer14e22772010-04-27 06:56:47 +00005986static size_t needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00005987 struct type *type, size_t offset)
5988{
5989 size_t padding, align;
5990 align = align_of(state, type);
5991 /* Align to the next machine word if the bitfield does completely
5992 * fit into the current word.
5993 */
5994 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5995 size_t size;
5996 size = size_of(state, type);
5997 if ((offset + type->elements)/size != offset/size) {
5998 align = size;
5999 }
6000 }
Eric Biederman03b59862003-06-24 14:27:37 +00006001 padding = 0;
6002 if (offset % align) {
6003 padding = align - (offset % align);
6004 }
6005 return padding;
6006}
Eric Biederman90089602004-05-28 14:11:54 +00006007
Stefan Reinauer14e22772010-04-27 06:56:47 +00006008static size_t reg_needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006009 struct type *type, size_t offset)
6010{
6011 size_t padding, align;
6012 align = reg_align_of(state, type);
6013 /* Align to the next register word if the bitfield does completely
6014 * fit into the current register.
6015 */
6016 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00006017 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
Eric Biederman90089602004-05-28 14:11:54 +00006018 {
6019 align = REG_SIZEOF_REG;
6020 }
6021 padding = 0;
6022 if (offset % align) {
6023 padding = align - (offset % align);
6024 }
6025 return padding;
6026}
6027
Eric Biedermanb138ac82003-04-22 18:44:01 +00006028static size_t size_of(struct compile_state *state, struct type *type)
6029{
6030 size_t size;
6031 size = 0;
6032 switch(type->type & TYPE_MASK) {
6033 case TYPE_VOID:
6034 size = 0;
6035 break;
Eric Biederman90089602004-05-28 14:11:54 +00006036 case TYPE_BITFIELD:
6037 size = type->elements;
6038 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006039 case TYPE_CHAR:
6040 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006041 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006042 break;
6043 case TYPE_SHORT:
6044 case TYPE_USHORT:
6045 size = SIZEOF_SHORT;
6046 break;
6047 case TYPE_INT:
6048 case TYPE_UINT:
6049 case TYPE_ENUM:
6050 size = SIZEOF_INT;
6051 break;
6052 case TYPE_LONG:
6053 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006054 size = SIZEOF_LONG;
6055 break;
Eric Biederman90089602004-05-28 14:11:54 +00006056 case TYPE_POINTER:
6057 size = SIZEOF_POINTER;
6058 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006059 case TYPE_PRODUCT:
6060 {
Eric Biederman90089602004-05-28 14:11:54 +00006061 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006062 size = 0;
6063 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006064 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006065 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006066 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006067 }
Eric Biederman90089602004-05-28 14:11:54 +00006068 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006069 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006070 break;
6071 }
6072 case TYPE_OVERLAP:
6073 {
6074 size_t size_left, size_right;
6075 size_left = size_of(state, type->left);
6076 size_right = size_of(state, type->right);
6077 size = (size_left >= size_right)? size_left : size_right;
6078 break;
6079 }
6080 case TYPE_ARRAY:
6081 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6082 internal_error(state, 0, "Invalid array type");
6083 } else {
6084 size = size_of(state, type->left) * type->elements;
6085 }
6086 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006087 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006088 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006089 {
Eric Biederman90089602004-05-28 14:11:54 +00006090 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006091 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006092 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006093 pad = needed_padding(state, type, size);
6094 size = size + pad;
6095 break;
6096 }
6097 case TYPE_UNION:
6098 case TYPE_JOIN:
6099 {
6100 size_t pad;
6101 size = size_of(state, type->left);
6102 /* Pad unions so their size is a multiple of their alignment */
6103 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006104 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006105 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006106 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006107 default:
Eric Biederman90089602004-05-28 14:11:54 +00006108 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006109 break;
6110 }
6111 return size;
6112}
6113
Eric Biederman90089602004-05-28 14:11:54 +00006114static size_t reg_size_of(struct compile_state *state, struct type *type)
6115{
6116 size_t size;
6117 size = 0;
6118 switch(type->type & TYPE_MASK) {
6119 case TYPE_VOID:
6120 size = 0;
6121 break;
6122 case TYPE_BITFIELD:
6123 size = type->elements;
6124 break;
6125 case TYPE_CHAR:
6126 case TYPE_UCHAR:
6127 size = REG_SIZEOF_CHAR;
6128 break;
6129 case TYPE_SHORT:
6130 case TYPE_USHORT:
6131 size = REG_SIZEOF_SHORT;
6132 break;
6133 case TYPE_INT:
6134 case TYPE_UINT:
6135 case TYPE_ENUM:
6136 size = REG_SIZEOF_INT;
6137 break;
6138 case TYPE_LONG:
6139 case TYPE_ULONG:
6140 size = REG_SIZEOF_LONG;
6141 break;
6142 case TYPE_POINTER:
6143 size = REG_SIZEOF_POINTER;
6144 break;
6145 case TYPE_PRODUCT:
6146 {
6147 size_t pad;
6148 size = 0;
6149 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6150 pad = reg_needed_padding(state, type->left, size);
6151 size = size + pad + reg_size_of(state, type->left);
6152 type = type->right;
6153 }
6154 pad = reg_needed_padding(state, type, size);
6155 size = size + pad + reg_size_of(state, type);
6156 break;
6157 }
6158 case TYPE_OVERLAP:
6159 {
6160 size_t size_left, size_right;
6161 size_left = reg_size_of(state, type->left);
6162 size_right = reg_size_of(state, type->right);
6163 size = (size_left >= size_right)? size_left : size_right;
6164 break;
6165 }
6166 case TYPE_ARRAY:
6167 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6168 internal_error(state, 0, "Invalid array type");
6169 } else {
6170 size = reg_size_of(state, type->left) * type->elements;
6171 }
6172 break;
6173 case TYPE_STRUCT:
6174 case TYPE_TUPLE:
6175 {
6176 size_t pad;
6177 size = reg_size_of(state, type->left);
6178 /* Pad structures so their size is a multiples of their alignment */
6179 pad = reg_needed_padding(state, type, size);
6180 size = size + pad;
6181 break;
6182 }
6183 case TYPE_UNION:
6184 case TYPE_JOIN:
6185 {
6186 size_t pad;
6187 size = reg_size_of(state, type->left);
6188 /* Pad unions so their size is a multiple of their alignment */
6189 pad = reg_needed_padding(state, type, size);
6190 size = size + pad;
6191 break;
6192 }
6193 default:
6194 internal_error(state, 0, "sizeof not yet defined for type");
6195 break;
6196 }
6197 return size;
6198}
6199
6200static size_t registers_of(struct compile_state *state, struct type *type)
6201{
6202 size_t registers;
6203 registers = reg_size_of(state, type);
6204 registers += REG_SIZEOF_REG - 1;
6205 registers /= REG_SIZEOF_REG;
6206 return registers;
6207}
6208
6209static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6210{
6211 return bits_to_bytes(size_of(state, type));
6212}
6213
Stefan Reinauer14e22772010-04-27 06:56:47 +00006214static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006215 struct type *type, struct hash_entry *field)
6216{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006217 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006218 size_t size;
6219
Eric Biederman0babc1c2003-05-09 02:39:00 +00006220 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006221 member = 0;
6222 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6223 member = type->left;
6224 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6225 size += needed_padding(state, member->left, size);
6226 if (member->left->field_ident == field) {
6227 member = member->left;
6228 break;
6229 }
6230 size += size_of(state, member->left);
6231 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006232 }
Eric Biederman90089602004-05-28 14:11:54 +00006233 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006234 }
Eric Biederman90089602004-05-28 14:11:54 +00006235 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6236 member = type->left;
6237 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6238 if (member->left->field_ident == field) {
6239 member = member->left;
6240 break;
6241 }
6242 member = member->right;
6243 }
6244 }
6245 else {
6246 internal_error(state, 0, "field_offset only works on structures and unions");
6247 }
6248
6249 if (!member || (member->field_ident != field)) {
6250 error(state, 0, "member %s not present", field->name);
6251 }
6252 return size;
6253}
6254
Stefan Reinauer14e22772010-04-27 06:56:47 +00006255static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006256 struct type *type, struct hash_entry *field)
6257{
6258 struct type *member;
6259 size_t size;
6260
6261 size = 0;
6262 member = 0;
6263 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6264 member = type->left;
6265 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6266 size += reg_needed_padding(state, member->left, size);
6267 if (member->left->field_ident == field) {
6268 member = member->left;
6269 break;
6270 }
6271 size += reg_size_of(state, member->left);
6272 member = member->right;
6273 }
6274 }
6275 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6276 member = type->left;
6277 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6278 if (member->left->field_ident == field) {
6279 member = member->left;
6280 break;
6281 }
6282 member = member->right;
6283 }
6284 }
6285 else {
6286 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6287 }
6288
6289 size += reg_needed_padding(state, member, size);
6290 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006291 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006292 }
6293 return size;
6294}
6295
Stefan Reinauer14e22772010-04-27 06:56:47 +00006296static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006297 struct type *type, struct hash_entry *field)
6298{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006299 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006300
6301 member = 0;
6302 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6303 member = type->left;
6304 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6305 if (member->left->field_ident == field) {
6306 member = member->left;
6307 break;
6308 }
6309 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006310 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006311 }
Eric Biederman90089602004-05-28 14:11:54 +00006312 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6313 member = type->left;
6314 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6315 if (member->left->field_ident == field) {
6316 member = member->left;
6317 break;
6318 }
6319 member = member->right;
6320 }
6321 }
6322 else {
6323 internal_error(state, 0, "field_type only works on structures and unions");
6324 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006325
Eric Biederman90089602004-05-28 14:11:54 +00006326 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006327 error(state, 0, "member %s not present", field->name);
6328 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006329 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006330}
6331
Stefan Reinauer14e22772010-04-27 06:56:47 +00006332static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006333 struct type *type, ulong_t index)
6334{
6335 struct type *member;
6336 size_t size;
6337 size = 0;
6338 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6339 size = size_of(state, type->left) * index;
6340 }
6341 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6342 ulong_t i;
6343 member = type->left;
6344 i = 0;
6345 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6346 size += needed_padding(state, member->left, size);
6347 if (i == index) {
6348 member = member->left;
6349 break;
6350 }
6351 size += size_of(state, member->left);
6352 i++;
6353 member = member->right;
6354 }
6355 size += needed_padding(state, member, size);
6356 if (i != index) {
6357 internal_error(state, 0, "Missing member index: %u", index);
6358 }
6359 }
6360 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6361 ulong_t i;
6362 size = 0;
6363 member = type->left;
6364 i = 0;
6365 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6366 if (i == index) {
6367 member = member->left;
6368 break;
6369 }
6370 i++;
6371 member = member->right;
6372 }
6373 if (i != index) {
6374 internal_error(state, 0, "Missing member index: %u", index);
6375 }
6376 }
6377 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006378 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006379 "request for index %u in something not an array, tuple or join",
6380 index);
6381 }
6382 return size;
6383}
6384
Stefan Reinauer14e22772010-04-27 06:56:47 +00006385static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006386 struct type *type, ulong_t index)
6387{
6388 struct type *member;
6389 size_t size;
6390 size = 0;
6391 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6392 size = reg_size_of(state, type->left) * index;
6393 }
6394 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6395 ulong_t i;
6396 member = type->left;
6397 i = 0;
6398 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6399 size += reg_needed_padding(state, member->left, size);
6400 if (i == index) {
6401 member = member->left;
6402 break;
6403 }
6404 size += reg_size_of(state, member->left);
6405 i++;
6406 member = member->right;
6407 }
6408 size += reg_needed_padding(state, member, size);
6409 if (i != index) {
6410 internal_error(state, 0, "Missing member index: %u", index);
6411 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006412
Eric Biederman90089602004-05-28 14:11:54 +00006413 }
6414 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6415 ulong_t i;
6416 size = 0;
6417 member = type->left;
6418 i = 0;
6419 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6420 if (i == index) {
6421 member = member->left;
6422 break;
6423 }
6424 i++;
6425 member = member->right;
6426 }
6427 if (i != index) {
6428 internal_error(state, 0, "Missing member index: %u", index);
6429 }
6430 }
6431 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006432 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006433 "request for index %u in something not an array, tuple or join",
6434 index);
6435 }
6436 return size;
6437}
6438
6439static struct type *index_type(struct compile_state *state,
6440 struct type *type, ulong_t index)
6441{
6442 struct type *member;
6443 if (index >= type->elements) {
6444 internal_error(state, 0, "Invalid element %u requested", index);
6445 }
6446 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6447 member = type->left;
6448 }
6449 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6450 ulong_t i;
6451 member = type->left;
6452 i = 0;
6453 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6454 if (i == index) {
6455 member = member->left;
6456 break;
6457 }
6458 i++;
6459 member = member->right;
6460 }
6461 if (i != index) {
6462 internal_error(state, 0, "Missing member index: %u", index);
6463 }
6464 }
6465 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6466 ulong_t i;
6467 member = type->left;
6468 i = 0;
6469 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6470 if (i == index) {
6471 member = member->left;
6472 break;
6473 }
6474 i++;
6475 member = member->right;
6476 }
6477 if (i != index) {
6478 internal_error(state, 0, "Missing member index: %u", index);
6479 }
6480 }
6481 else {
6482 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006483 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006484 "request for index %u in something not an array, tuple or join",
6485 index);
6486 }
6487 return member;
6488}
6489
6490static struct type *unpack_type(struct compile_state *state, struct type *type)
6491{
6492 /* If I have a single register compound type not a bit-field
6493 * find the real type.
6494 */
6495 struct type *start_type;
6496 size_t size;
6497 /* Get out early if I need multiple registers for this type */
6498 size = reg_size_of(state, type);
6499 if (size > REG_SIZEOF_REG) {
6500 return type;
6501 }
6502 /* Get out early if I don't need any registers for this type */
6503 if (size == 0) {
6504 return &void_type;
6505 }
6506 /* Loop until I have no more layers I can remove */
6507 do {
6508 start_type = type;
6509 switch(type->type & TYPE_MASK) {
6510 case TYPE_ARRAY:
6511 /* If I have a single element the unpacked type
6512 * is that element.
6513 */
6514 if (type->elements == 1) {
6515 type = type->left;
6516 }
6517 break;
6518 case TYPE_STRUCT:
6519 case TYPE_TUPLE:
6520 /* If I have a single element the unpacked type
6521 * is that element.
6522 */
6523 if (type->elements == 1) {
6524 type = type->left;
6525 }
6526 /* If I have multiple elements the unpacked
6527 * type is the non-void element.
6528 */
6529 else {
6530 struct type *next, *member;
6531 struct type *sub_type;
6532 sub_type = 0;
6533 next = type->left;
6534 while(next) {
6535 member = next;
6536 next = 0;
6537 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6538 next = member->right;
6539 member = member->left;
6540 }
6541 if (reg_size_of(state, member) > 0) {
6542 if (sub_type) {
6543 internal_error(state, 0, "true compound type in a register");
6544 }
6545 sub_type = member;
6546 }
6547 }
6548 if (sub_type) {
6549 type = sub_type;
6550 }
6551 }
6552 break;
6553
6554 case TYPE_UNION:
6555 case TYPE_JOIN:
6556 /* If I have a single element the unpacked type
6557 * is that element.
6558 */
6559 if (type->elements == 1) {
6560 type = type->left;
6561 }
6562 /* I can't in general unpack union types */
6563 break;
6564 default:
6565 /* If I'm not a compound type I can't unpack it */
6566 break;
6567 }
6568 } while(start_type != type);
6569 switch(type->type & TYPE_MASK) {
6570 case TYPE_STRUCT:
6571 case TYPE_ARRAY:
6572 case TYPE_TUPLE:
6573 internal_error(state, 0, "irredicible type?");
6574 break;
6575 }
6576 return type;
6577}
6578
6579static int equiv_types(struct type *left, struct type *right);
6580static int is_compound_type(struct type *type);
6581
6582static struct type *reg_type(
6583 struct compile_state *state, struct type *type, int reg_offset)
6584{
6585 struct type *member;
6586 size_t size;
6587#if 1
6588 struct type *invalid;
6589 invalid = invalid_type(state, type);
6590 if (invalid) {
6591 fprintf(state->errout, "type: ");
6592 name_of(state->errout, type);
6593 fprintf(state->errout, "\n");
6594 fprintf(state->errout, "invalid: ");
6595 name_of(state->errout, invalid);
6596 fprintf(state->errout, "\n");
6597 internal_error(state, 0, "bad input type?");
6598 }
6599#endif
6600
6601 size = reg_size_of(state, type);
6602 if (reg_offset > size) {
6603 member = 0;
6604 fprintf(state->errout, "type: ");
6605 name_of(state->errout, type);
6606 fprintf(state->errout, "\n");
6607 internal_error(state, 0, "offset outside of type");
6608 }
6609 else {
6610 switch(type->type & TYPE_MASK) {
6611 /* Don't do anything with the basic types */
6612 case TYPE_VOID:
6613 case TYPE_CHAR: case TYPE_UCHAR:
6614 case TYPE_SHORT: case TYPE_USHORT:
6615 case TYPE_INT: case TYPE_UINT:
6616 case TYPE_LONG: case TYPE_ULONG:
6617 case TYPE_LLONG: case TYPE_ULLONG:
6618 case TYPE_FLOAT: case TYPE_DOUBLE:
6619 case TYPE_LDOUBLE:
6620 case TYPE_POINTER:
6621 case TYPE_ENUM:
6622 case TYPE_BITFIELD:
6623 member = type;
6624 break;
6625 case TYPE_ARRAY:
6626 member = type->left;
6627 size = reg_size_of(state, member);
6628 if (size > REG_SIZEOF_REG) {
6629 member = reg_type(state, member, reg_offset % size);
6630 }
6631 break;
6632 case TYPE_STRUCT:
6633 case TYPE_TUPLE:
6634 {
6635 size_t offset;
6636 offset = 0;
6637 member = type->left;
6638 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6639 size = reg_size_of(state, member->left);
6640 offset += reg_needed_padding(state, member->left, offset);
6641 if ((offset + size) > reg_offset) {
6642 member = member->left;
6643 break;
6644 }
6645 offset += size;
6646 member = member->right;
6647 }
6648 offset += reg_needed_padding(state, member, offset);
6649 member = reg_type(state, member, reg_offset - offset);
6650 break;
6651 }
6652 case TYPE_UNION:
6653 case TYPE_JOIN:
6654 {
6655 struct type *join, **jnext, *mnext;
6656 join = new_type(TYPE_JOIN, 0, 0);
6657 jnext = &join->left;
6658 mnext = type->left;
6659 while(mnext) {
6660 size_t size;
6661 member = mnext;
6662 mnext = 0;
6663 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6664 mnext = member->right;
6665 member = member->left;
6666 }
6667 size = reg_size_of(state, member);
6668 if (size > reg_offset) {
6669 struct type *part, *hunt;
6670 part = reg_type(state, member, reg_offset);
6671 /* See if this type is already in the union */
6672 hunt = join->left;
6673 while(hunt) {
6674 struct type *test = hunt;
6675 hunt = 0;
6676 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6677 hunt = test->right;
6678 test = test->left;
6679 }
6680 if (equiv_types(part, test)) {
6681 goto next;
6682 }
6683 }
6684 /* Nope add it */
6685 if (!*jnext) {
6686 *jnext = part;
6687 } else {
6688 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6689 jnext = &(*jnext)->right;
6690 }
6691 join->elements++;
6692 }
6693 next:
6694 ;
6695 }
6696 if (join->elements == 0) {
6697 internal_error(state, 0, "No elements?");
6698 }
6699 member = join;
6700 break;
6701 }
6702 default:
6703 member = 0;
6704 fprintf(state->errout, "type: ");
6705 name_of(state->errout, type);
6706 fprintf(state->errout, "\n");
6707 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006708
Eric Biederman90089602004-05-28 14:11:54 +00006709 }
6710 }
6711 /* If I have a single register compound type not a bit-field
6712 * find the real type.
6713 */
6714 member = unpack_type(state, member);
6715 ;
6716 size = reg_size_of(state, member);
6717 if (size > REG_SIZEOF_REG) {
6718 internal_error(state, 0, "Cannot find type of single register");
6719 }
6720#if 1
6721 invalid = invalid_type(state, member);
6722 if (invalid) {
6723 fprintf(state->errout, "type: ");
6724 name_of(state->errout, member);
6725 fprintf(state->errout, "\n");
6726 fprintf(state->errout, "invalid: ");
6727 name_of(state->errout, invalid);
6728 fprintf(state->errout, "\n");
6729 internal_error(state, 0, "returning bad type?");
6730 }
6731#endif
6732 return member;
6733}
6734
Eric Biederman03b59862003-06-24 14:27:37 +00006735static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006736 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006737{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006738 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006739 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6740 internal_error(state, 0, "next_field only works on structures");
6741 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006742 member = type->left;
6743 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006744 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006745 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006746 break;
6747 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006748 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006749 prev_member = 0;
6750 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006751 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006752 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006753 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006754 prev_member = 0;
6755 }
6756 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006757 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006758 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006759 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006760 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006761}
6762
Stefan Reinauer14e22772010-04-27 06:56:47 +00006763typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006764 size_t ret_offset, size_t mem_offset, void *arg);
6765
6766static void walk_type_fields(struct compile_state *state,
6767 struct type *type, size_t reg_offset, size_t mem_offset,
6768 walk_type_fields_cb_t cb, void *arg);
6769
6770static void walk_struct_fields(struct compile_state *state,
6771 struct type *type, size_t reg_offset, size_t mem_offset,
6772 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006773{
Eric Biederman90089602004-05-28 14:11:54 +00006774 struct type *tptr;
6775 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006776 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006777 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006778 }
Eric Biederman90089602004-05-28 14:11:54 +00006779 tptr = type->left;
6780 for(i = 0; i < type->elements; i++) {
6781 struct type *mtype;
6782 mtype = tptr;
6783 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6784 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006785 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006786 walk_type_fields(state, mtype,
6787 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006788 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006789 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006790 field_offset(state, type, mtype->field_ident),
6791 cb, arg);
6792 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006793 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006794
Eric Biederman90089602004-05-28 14:11:54 +00006795}
6796
6797static void walk_type_fields(struct compile_state *state,
6798 struct type *type, size_t reg_offset, size_t mem_offset,
6799 walk_type_fields_cb_t cb, void *arg)
6800{
6801 switch(type->type & TYPE_MASK) {
6802 case TYPE_STRUCT:
6803 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6804 break;
6805 case TYPE_CHAR:
6806 case TYPE_UCHAR:
6807 case TYPE_SHORT:
6808 case TYPE_USHORT:
6809 case TYPE_INT:
6810 case TYPE_UINT:
6811 case TYPE_LONG:
6812 case TYPE_ULONG:
6813 cb(state, type, reg_offset, mem_offset, arg);
6814 break;
6815 case TYPE_VOID:
6816 break;
6817 default:
6818 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006819 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006820}
6821
Eric Biedermanb138ac82003-04-22 18:44:01 +00006822static void arrays_complete(struct compile_state *state, struct type *type)
6823{
6824 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6825 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6826 error(state, 0, "array size not specified");
6827 }
6828 arrays_complete(state, type->left);
6829 }
6830}
6831
Eric Biederman90089602004-05-28 14:11:54 +00006832static unsigned int get_basic_type(struct type *type)
6833{
6834 unsigned int basic;
6835 basic = type->type & TYPE_MASK;
6836 /* Convert enums to ints */
6837 if (basic == TYPE_ENUM) {
6838 basic = TYPE_INT;
6839 }
6840 /* Convert bitfields to standard types */
6841 else if (basic == TYPE_BITFIELD) {
6842 if (type->elements <= SIZEOF_CHAR) {
6843 basic = TYPE_CHAR;
6844 }
6845 else if (type->elements <= SIZEOF_SHORT) {
6846 basic = TYPE_SHORT;
6847 }
6848 else if (type->elements <= SIZEOF_INT) {
6849 basic = TYPE_INT;
6850 }
6851 else if (type->elements <= SIZEOF_LONG) {
6852 basic = TYPE_LONG;
6853 }
6854 if (!TYPE_SIGNED(type->left->type)) {
6855 basic += 1;
6856 }
6857 }
6858 return basic;
6859}
6860
Eric Biedermanb138ac82003-04-22 18:44:01 +00006861static unsigned int do_integral_promotion(unsigned int type)
6862{
Eric Biederman83b991a2003-10-11 06:20:25 +00006863 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006864 type = TYPE_INT;
6865 }
6866 return type;
6867}
6868
6869static unsigned int do_arithmetic_conversion(
6870 unsigned int left, unsigned int right)
6871{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006872 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6873 return TYPE_LDOUBLE;
6874 }
6875 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6876 return TYPE_DOUBLE;
6877 }
6878 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6879 return TYPE_FLOAT;
6880 }
6881 left = do_integral_promotion(left);
6882 right = do_integral_promotion(right);
6883 /* If both operands have the same size done */
6884 if (left == right) {
6885 return left;
6886 }
6887 /* If both operands have the same signedness pick the larger */
6888 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6889 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6890 }
6891 /* If the signed type can hold everything use it */
6892 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6893 return left;
6894 }
6895 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6896 return right;
6897 }
6898 /* Convert to the unsigned type with the same rank as the signed type */
6899 else if (TYPE_SIGNED(left)) {
6900 return TYPE_MKUNSIGNED(left);
6901 }
6902 else {
6903 return TYPE_MKUNSIGNED(right);
6904 }
6905}
6906
6907/* see if two types are the same except for qualifiers */
6908static int equiv_types(struct type *left, struct type *right)
6909{
6910 unsigned int type;
6911 /* Error if the basic types do not match */
6912 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6913 return 0;
6914 }
6915 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006916 /* If the basic types match and it is a void type we are done */
6917 if (type == TYPE_VOID) {
6918 return 1;
6919 }
Eric Biederman90089602004-05-28 14:11:54 +00006920 /* For bitfields we need to compare the sizes */
6921 else if (type == TYPE_BITFIELD) {
6922 return (left->elements == right->elements) &&
6923 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6924 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006925 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006926 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006927 return 1;
6928 }
6929 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006930 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006931 return equiv_types(left->left, right->left);
6932 }
6933 else if (type == TYPE_ARRAY) {
6934 return (left->elements == right->elements) &&
6935 equiv_types(left->left, right->left);
6936 }
Eric Biederman90089602004-05-28 14:11:54 +00006937 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006938 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006939 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006940 }
Eric Biederman90089602004-05-28 14:11:54 +00006941 /* test for union equality */
6942 else if (type == TYPE_UNION) {
6943 return left->type_ident == right->type_ident;
6944 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006945 /* Test for equivalent functions */
6946 else if (type == TYPE_FUNCTION) {
6947 return equiv_types(left->left, right->left) &&
6948 equiv_types(left->right, right->right);
6949 }
6950 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006951 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006952 else if (type == TYPE_PRODUCT) {
6953 return equiv_types(left->left, right->left) &&
6954 equiv_types(left->right, right->right);
6955 }
Eric Biederman90089602004-05-28 14:11:54 +00006956 /* We should see TYPE_OVERLAP when comparing joins */
6957 else if (type == TYPE_OVERLAP) {
6958 return equiv_types(left->left, right->left) &&
6959 equiv_types(left->right, right->right);
6960 }
6961 /* Test for equivalence of tuples */
6962 else if (type == TYPE_TUPLE) {
6963 return (left->elements == right->elements) &&
6964 equiv_types(left->left, right->left);
6965 }
6966 /* Test for equivalence of joins */
6967 else if (type == TYPE_JOIN) {
6968 return (left->elements == right->elements) &&
6969 equiv_types(left->left, right->left);
6970 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006971 else {
6972 return 0;
6973 }
6974}
6975
6976static int equiv_ptrs(struct type *left, struct type *right)
6977{
6978 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6979 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6980 return 0;
6981 }
6982 return equiv_types(left->left, right->left);
6983}
6984
6985static struct type *compatible_types(struct type *left, struct type *right)
6986{
6987 struct type *result;
6988 unsigned int type, qual_type;
6989 /* Error if the basic types do not match */
6990 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6991 return 0;
6992 }
6993 type = left->type & TYPE_MASK;
6994 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6995 result = 0;
6996 /* if the basic types match and it is an arithmetic type we are done */
6997 if (TYPE_ARITHMETIC(type)) {
6998 result = new_type(qual_type, 0, 0);
6999 }
7000 /* If it is a pointer type recurse and keep testing */
7001 else if (type == TYPE_POINTER) {
7002 result = compatible_types(left->left, right->left);
7003 if (result) {
7004 result = new_type(qual_type, result, 0);
7005 }
7006 }
Eric Biederman90089602004-05-28 14:11:54 +00007007 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007008 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007009 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007010 result = left;
7011 }
7012 }
Eric Biederman90089602004-05-28 14:11:54 +00007013 /* test for union equality */
7014 else if (type == TYPE_UNION) {
7015 if (left->type_ident == right->type_ident) {
7016 result = left;
7017 }
7018 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007019 /* Test for equivalent functions */
7020 else if (type == TYPE_FUNCTION) {
7021 struct type *lf, *rf;
7022 lf = compatible_types(left->left, right->left);
7023 rf = compatible_types(left->right, right->right);
7024 if (lf && rf) {
7025 result = new_type(qual_type, lf, rf);
7026 }
7027 }
7028 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7029 else if (type == TYPE_PRODUCT) {
7030 struct type *lf, *rf;
7031 lf = compatible_types(left->left, right->left);
7032 rf = compatible_types(left->right, right->right);
7033 if (lf && rf) {
7034 result = new_type(qual_type, lf, rf);
7035 }
7036 }
7037 else {
7038 /* Nothing else is compatible */
7039 }
7040 return result;
7041}
7042
Eric Biederman90089602004-05-28 14:11:54 +00007043/* See if left is a equivalent to right or right is a union member of left */
7044static int is_subset_type(struct type *left, struct type *right)
7045{
7046 if (equiv_types(left, right)) {
7047 return 1;
7048 }
7049 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7050 struct type *member, *mnext;
7051 mnext = left->left;
7052 while(mnext) {
7053 member = mnext;
7054 mnext = 0;
7055 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7056 mnext = member->right;
7057 member = member->left;
7058 }
7059 if (is_subset_type( member, right)) {
7060 return 1;
7061 }
7062 }
7063 }
7064 return 0;
7065}
7066
Eric Biedermanb138ac82003-04-22 18:44:01 +00007067static struct type *compatible_ptrs(struct type *left, struct type *right)
7068{
7069 struct type *result;
7070 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7071 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7072 return 0;
7073 }
7074 result = compatible_types(left->left, right->left);
7075 if (result) {
7076 unsigned int qual_type;
7077 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7078 result = new_type(qual_type, result, 0);
7079 }
7080 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007081
Eric Biedermanb138ac82003-04-22 18:44:01 +00007082}
7083static struct triple *integral_promotion(
7084 struct compile_state *state, struct triple *def)
7085{
7086 struct type *type;
7087 type = def->type;
7088 /* As all operations are carried out in registers
7089 * the values are converted on load I just convert
7090 * logical type of the operand.
7091 */
7092 if (TYPE_INTEGER(type->type)) {
7093 unsigned int int_type;
7094 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007095 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007096 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007097 if (def->op != OP_LOAD) {
7098 def->type = new_type(int_type, 0, 0);
7099 }
7100 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007101 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007102 new_type(int_type, 0, 0), def, 0);
7103 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007104 }
7105 }
7106 return def;
7107}
7108
7109
7110static void arithmetic(struct compile_state *state, struct triple *def)
7111{
7112 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007113 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007114 }
7115}
7116
7117static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7118{
7119 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7120 error(state, def, "pointer or arithmetic type expected");
7121 }
7122}
7123
7124static int is_integral(struct triple *ins)
7125{
7126 return TYPE_INTEGER(ins->type->type);
7127}
7128
7129static void integral(struct compile_state *state, struct triple *def)
7130{
7131 if (!is_integral(def)) {
7132 error(state, 0, "integral type expected");
7133 }
7134}
7135
7136
7137static void bool(struct compile_state *state, struct triple *def)
7138{
7139 if (!TYPE_ARITHMETIC(def->type->type) &&
7140 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7141 error(state, 0, "arithmetic or pointer type expected");
7142 }
7143}
7144
7145static int is_signed(struct type *type)
7146{
Eric Biederman90089602004-05-28 14:11:54 +00007147 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7148 type = type->left;
7149 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007150 return !!TYPE_SIGNED(type->type);
7151}
Eric Biederman90089602004-05-28 14:11:54 +00007152static int is_compound_type(struct type *type)
7153{
7154 int is_compound;
7155 switch((type->type & TYPE_MASK)) {
7156 case TYPE_ARRAY:
7157 case TYPE_STRUCT:
7158 case TYPE_TUPLE:
7159 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007160 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007161 is_compound = 1;
7162 break;
7163 default:
7164 is_compound = 0;
7165 break;
7166 }
7167 return is_compound;
7168}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007169
Eric Biederman0babc1c2003-05-09 02:39:00 +00007170/* Is this value located in a register otherwise it must be in memory */
7171static int is_in_reg(struct compile_state *state, struct triple *def)
7172{
7173 int in_reg;
7174 if (def->op == OP_ADECL) {
7175 in_reg = 1;
7176 }
7177 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7178 in_reg = 0;
7179 }
Eric Biederman90089602004-05-28 14:11:54 +00007180 else if (triple_is_part(state, def)) {
7181 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007182 }
7183 else {
Eric Biederman90089602004-05-28 14:11:54 +00007184 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007185 in_reg = -1;
7186 }
7187 return in_reg;
7188}
7189
Eric Biederman90089602004-05-28 14:11:54 +00007190/* Is this an auto or static variable location? Something that can
7191 * be assigned to. Otherwise it must must be a pure value, a temporary.
7192 */
7193static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007194{
7195 int ret;
7196 ret = 0;
7197 if (!def) {
7198 return 0;
7199 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007200 if ((def->op == OP_ADECL) ||
7201 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007202 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007203 (def->op == OP_BLOBCONST) ||
7204 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007205 ret = 1;
7206 }
Eric Biederman90089602004-05-28 14:11:54 +00007207 else if (triple_is_part(state, def)) {
7208 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007209 }
7210 return ret;
7211}
7212
Eric Biederman00443072003-06-24 12:34:45 +00007213static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007214{
7215 if (!def) {
7216 internal_error(state, def, "nothing where lvalue expected?");
7217 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007218 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007219 error(state, def, "lvalue expected");
7220 }
7221}
Eric Biederman00443072003-06-24 12:34:45 +00007222static void lvalue(struct compile_state *state, struct triple *def)
7223{
7224 clvalue(state, def);
7225 if (def->type->type & QUAL_CONST) {
7226 error(state, def, "modifable lvalue expected");
7227 }
7228}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007229
7230static int is_pointer(struct triple *def)
7231{
7232 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7233}
7234
7235static void pointer(struct compile_state *state, struct triple *def)
7236{
7237 if (!is_pointer(def)) {
7238 error(state, def, "pointer expected");
7239 }
7240}
7241
7242static struct triple *int_const(
7243 struct compile_state *state, struct type *type, ulong_t value)
7244{
7245 struct triple *result;
7246 switch(type->type & TYPE_MASK) {
7247 case TYPE_CHAR:
7248 case TYPE_INT: case TYPE_UINT:
7249 case TYPE_LONG: case TYPE_ULONG:
7250 break;
7251 default:
Eric Biederman90089602004-05-28 14:11:54 +00007252 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007253 }
7254 result = triple(state, OP_INTCONST, type, 0, 0);
7255 result->u.cval = value;
7256 return result;
7257}
7258
7259
Eric Biederman83b991a2003-10-11 06:20:25 +00007260static struct triple *read_expr(struct compile_state *state, struct triple *def);
7261
Stefan Reinauer14e22772010-04-27 06:56:47 +00007262static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007263 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007264{
7265 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007266 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007267 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007268
Eric Biederman90089602004-05-28 14:11:54 +00007269 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007270
Stefan Reinauer14e22772010-04-27 06:56:47 +00007271
Eric Biedermanb138ac82003-04-22 18:44:01 +00007272 result = 0;
7273 if (expr->op == OP_ADECL) {
7274 error(state, expr, "address of auto variables not supported");
7275 }
7276 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007277 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007278 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007279 result->u.cval = offset;
7280 }
7281 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007282 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007283 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007284 int_const(state, &ulong_type, offset));
7285 }
Eric Biederman90089602004-05-28 14:11:54 +00007286 else if (expr->op == OP_BLOBCONST) {
7287 FINISHME();
7288 internal_error(state, expr, "not yet implemented");
7289 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007290 else if (expr->op == OP_LIST) {
7291 error(state, 0, "Function addresses not supported");
7292 }
Eric Biederman90089602004-05-28 14:11:54 +00007293 else if (triple_is_part(state, expr)) {
7294 struct triple *part;
7295 part = expr;
7296 expr = MISC(expr, 0);
7297 if (part->op == OP_DOT) {
7298 offset += bits_to_bytes(
7299 field_offset(state, expr->type, part->u.field));
7300 }
7301 else if (part->op == OP_INDEX) {
7302 offset += bits_to_bytes(
7303 index_offset(state, expr->type, part->u.cval));
7304 }
7305 else {
7306 internal_error(state, part, "unhandled part type");
7307 }
7308 result = do_mk_addr_expr(state, expr, type, offset);
7309 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007310 if (!result) {
7311 internal_error(state, expr, "cannot take address of expression");
7312 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007313 return result;
7314}
7315
Eric Biederman0babc1c2003-05-09 02:39:00 +00007316static struct triple *mk_addr_expr(
7317 struct compile_state *state, struct triple *expr, ulong_t offset)
7318{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007319 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007320}
7321
Eric Biedermanb138ac82003-04-22 18:44:01 +00007322static struct triple *mk_deref_expr(
7323 struct compile_state *state, struct triple *expr)
7324{
7325 struct type *base_type;
7326 pointer(state, expr);
7327 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007328 return triple(state, OP_DEREF, base_type, expr, 0);
7329}
7330
Eric Biederman90089602004-05-28 14:11:54 +00007331/* lvalue conversions always apply except when certain operators
7332 * are applied. So I apply apply it when I know no more
7333 * operators will be applied.
7334 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007335static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007336{
Eric Biederman5cd81732004-03-11 15:01:31 +00007337 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007338 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7339 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007340 type = new_type(
7341 TYPE_POINTER | (def->type->type & QUAL_MASK),
7342 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007343 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007344 struct triple *addrconst;
7345 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7346 internal_error(state, def, "bad array constant");
7347 }
7348 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7349 MISC(addrconst, 0) = def;
7350 def = addrconst;
7351 }
7352 else {
Eric Biederman90089602004-05-28 14:11:54 +00007353 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007354 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007355 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007356 /* Transform a function to a pointer to it */
7357 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7358 def = mk_addr_expr(state, def, 0);
7359 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007360 return def;
7361}
7362
Eric Biederman0babc1c2003-05-09 02:39:00 +00007363static struct triple *deref_field(
7364 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7365{
7366 struct triple *result;
7367 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007368 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007369 if (!field) {
7370 internal_error(state, 0, "No field passed to deref_field");
7371 }
7372 result = 0;
7373 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007374 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7375 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007376 error(state, 0, "request for member %s in something not a struct or union",
7377 field->name);
7378 }
Eric Biederman03b59862003-06-24 14:27:37 +00007379 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007380 if ((type->type & STOR_MASK) == STOR_PERM) {
7381 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007382 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007383 result = do_mk_addr_expr(state, expr, member, offset);
7384 result = mk_deref_expr(state, result);
7385 }
7386 else {
7387 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007388 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007389 result->u.field = field;
7390 }
7391 return result;
7392}
7393
Eric Biederman90089602004-05-28 14:11:54 +00007394static struct triple *deref_index(
7395 struct compile_state *state, struct triple *expr, size_t index)
7396{
7397 struct triple *result;
7398 struct type *type, *member;
7399 ulong_t offset;
7400
7401 result = 0;
7402 type = expr->type;
7403 member = index_type(state, type, index);
7404
7405 if ((type->type & STOR_MASK) == STOR_PERM) {
7406 offset = bits_to_bytes(index_offset(state, type, index));
7407 result = do_mk_addr_expr(state, expr, member, offset);
7408 result = mk_deref_expr(state, result);
7409 }
7410 else {
7411 result = triple(state, OP_INDEX, member, expr, 0);
7412 result->u.cval = index;
7413 }
7414 return result;
7415}
7416
Eric Biedermanb138ac82003-04-22 18:44:01 +00007417static struct triple *read_expr(struct compile_state *state, struct triple *def)
7418{
7419 int op;
7420 if (!def) {
7421 return 0;
7422 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007423#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007424#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007425#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007426 /* Transform lvalues into something we can read */
7427 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007428 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007429 return def;
7430 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007431 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007432 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007433 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007434 if (def->op == OP_SDECL) {
7435 def = mk_addr_expr(state, def, 0);
7436 def = mk_deref_expr(state, def);
7437 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007438 op = OP_LOAD;
7439 }
Eric Biederman90089602004-05-28 14:11:54 +00007440 def = triple(state, op, def->type, def, 0);
7441 if (def->type->type & QUAL_VOLATILE) {
7442 def->id |= TRIPLE_FLAG_VOLATILE;
7443 }
7444 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007445}
7446
Stefan Reinauer14e22772010-04-27 06:56:47 +00007447int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007448 struct type *dest, struct type *rval)
7449{
7450 int compatible = 0;
7451 /* Both operands have arithmetic type */
7452 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7453 compatible = 1;
7454 }
7455 /* One operand is a pointer and the other is a pointer to void */
7456 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7457 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7458 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7459 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7460 compatible = 1;
7461 }
7462 /* If both types are the same without qualifiers we are good */
7463 else if (equiv_ptrs(dest, rval)) {
7464 compatible = 1;
7465 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007466 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007467 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007468 compatible = 1;
7469 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007470 return compatible;
7471}
7472
Eric Biedermane058a1e2003-07-12 01:21:31 +00007473static void write_compatible(struct compile_state *state,
7474 struct type *dest, struct type *rval)
7475{
7476 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007477 FILE *fp = state->errout;
7478 fprintf(fp, "dest: ");
7479 name_of(fp, dest);
7480 fprintf(fp,"\nrval: ");
7481 name_of(fp, rval);
7482 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007483 error(state, 0, "Incompatible types in assignment");
7484 }
7485}
7486
Eric Biedermane058a1e2003-07-12 01:21:31 +00007487static int is_init_compatible(struct compile_state *state,
7488 struct type *dest, struct type *rval)
7489{
7490 int compatible = 0;
7491 if (is_write_compatible(state, dest, rval)) {
7492 compatible = 1;
7493 }
7494 else if (equiv_types(dest, rval)) {
7495 compatible = 1;
7496 }
7497 return compatible;
7498}
7499
Eric Biedermanb138ac82003-04-22 18:44:01 +00007500static struct triple *write_expr(
7501 struct compile_state *state, struct triple *dest, struct triple *rval)
7502{
7503 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007504
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 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007526 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007527 def = triple(state, OP_WRITE, dest->type, rval, dest);
7528 if (MISC(def, 0) != dest) {
7529 internal_error(state, def, "huh?");
7530 }
7531 if (RHS(def, 0) != rval) {
7532 internal_error(state, def, "huh?");
7533 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007534 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007535 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007536 }
Eric Biederman90089602004-05-28 14:11:54 +00007537 if (def->type->type & QUAL_VOLATILE) {
7538 def->id |= TRIPLE_FLAG_VOLATILE;
7539 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007540 return def;
7541}
7542
7543static struct triple *init_expr(
7544 struct compile_state *state, struct triple *dest, struct triple *rval)
7545{
7546 struct triple *def;
7547
7548 def = 0;
7549 if (!rval) {
7550 internal_error(state, 0, "missing rval");
7551 }
7552 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7553 rval = read_expr(state, rval);
7554 def = write_expr(state, dest, rval);
7555 }
7556 else {
7557 /* Fill in the array size if necessary */
7558 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7559 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7560 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7561 dest->type->elements = rval->type->elements;
7562 }
7563 }
7564 if (!equiv_types(dest->type, rval->type)) {
7565 error(state, 0, "Incompatible types in inializer");
7566 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007567 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007568 insert_triple(state, dest, rval);
7569 rval->id |= TRIPLE_FLAG_FLATTENED;
7570 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007571 }
7572 return def;
7573}
7574
7575struct type *arithmetic_result(
7576 struct compile_state *state, struct triple *left, struct triple *right)
7577{
7578 struct type *type;
7579 /* Sanity checks to ensure I am working with arithmetic types */
7580 arithmetic(state, left);
7581 arithmetic(state, right);
7582 type = new_type(
7583 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007584 get_basic_type(left->type),
7585 get_basic_type(right->type)),
7586 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007587 return type;
7588}
7589
7590struct type *ptr_arithmetic_result(
7591 struct compile_state *state, struct triple *left, struct triple *right)
7592{
7593 struct type *type;
7594 /* Sanity checks to ensure I am working with the proper types */
7595 ptr_arithmetic(state, left);
7596 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007597 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007598 TYPE_ARITHMETIC(right->type->type)) {
7599 type = arithmetic_result(state, left, right);
7600 }
7601 else if (TYPE_PTR(left->type->type)) {
7602 type = left->type;
7603 }
7604 else {
7605 internal_error(state, 0, "huh?");
7606 type = 0;
7607 }
7608 return type;
7609}
7610
Eric Biedermanb138ac82003-04-22 18:44:01 +00007611/* boolean helper function */
7612
Stefan Reinauer14e22772010-04-27 06:56:47 +00007613static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007614 struct triple *expr)
7615{
7616 switch(expr->op) {
7617 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7618 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7619 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7620 /* If the expression is already boolean do nothing */
7621 break;
7622 default:
7623 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7624 break;
7625 }
7626 return expr;
7627}
7628
Stefan Reinauer14e22772010-04-27 06:56:47 +00007629static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007630 struct triple *expr)
7631{
7632 return triple(state, OP_LFALSE, &int_type, expr, 0);
7633}
7634
Eric Biederman90089602004-05-28 14:11:54 +00007635static struct triple *mkland_expr(
7636 struct compile_state *state,
7637 struct triple *left, struct triple *right)
7638{
7639 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007640 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007641
7642 /* Generate some intermediate triples */
7643 end = label(state);
7644 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007645
Eric Biederman90089602004-05-28 14:11:54 +00007646 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007647 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007648
7649 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007650 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007651 lfalse_expr(state, read_expr(state, var)));
7652 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007653
Eric Biederman90089602004-05-28 14:11:54 +00007654 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007655 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007656
7657 /* An expression for the computed value */
7658 val = read_expr(state, var);
7659
7660 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007661 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007662
Eric Biederman90089602004-05-28 14:11:54 +00007663 return def;
7664}
7665
7666static struct triple *mklor_expr(
7667 struct compile_state *state,
7668 struct triple *left, struct triple *right)
7669{
7670 struct triple *def, *val, *var, *jmp, *mid, *end;
7671
7672 /* Generate some intermediate triples */
7673 end = label(state);
7674 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007675
Eric Biederman90089602004-05-28 14:11:54 +00007676 /* Store the left hand side value */
7677 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007678
Eric Biederman90089602004-05-28 14:11:54 +00007679 /* Jump if the value is true */
7680 jmp = branch(state, end, read_expr(state, var));
7681 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007682
Eric Biederman90089602004-05-28 14:11:54 +00007683 /* Store the right hand side value */
7684 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007685
Eric Biederman90089602004-05-28 14:11:54 +00007686 /* An expression for the computed value*/
7687 val = read_expr(state, var);
7688
7689 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007690 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007691
7692 return def;
7693}
7694
7695static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007696 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007697 struct triple *test, struct triple *left, struct triple *right)
7698{
Eric Biederman90089602004-05-28 14:11:54 +00007699 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007700 struct type *result_type;
7701 unsigned int left_type, right_type;
7702 bool(state, test);
7703 left_type = left->type->type;
7704 right_type = right->type->type;
7705 result_type = 0;
7706 /* Both operands have arithmetic type */
7707 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7708 result_type = arithmetic_result(state, left, right);
7709 }
7710 /* Both operands have void type */
7711 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7712 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7713 result_type = &void_type;
7714 }
7715 /* pointers to the same type... */
7716 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7717 ;
7718 }
7719 /* Both operands are pointers and left is a pointer to void */
7720 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7721 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7722 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7723 result_type = right->type;
7724 }
7725 /* Both operands are pointers and right is a pointer to void */
7726 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7727 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7728 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7729 result_type = left->type;
7730 }
7731 if (!result_type) {
7732 error(state, 0, "Incompatible types in conditional expression");
7733 }
Eric Biederman90089602004-05-28 14:11:54 +00007734 /* Generate some intermediate triples */
7735 mid = label(state);
7736 end = label(state);
7737 var = variable(state, result_type);
7738
7739 /* Branch if the test is false */
7740 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7741 top = label(state);
7742
7743 /* Store the left hand side value */
7744 left = write_expr(state, var, left);
7745
7746 /* Branch to the end */
7747 jmp2 = branch(state, end, 0);
7748
7749 /* Store the right hand side value */
7750 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007751
Eric Biederman90089602004-05-28 14:11:54 +00007752 /* An expression for the computed value */
7753 val = read_expr(state, var);
7754
7755 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007756 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007757
Eric Biedermanb138ac82003-04-22 18:44:01 +00007758 return def;
7759}
7760
7761
Eric Biederman0babc1c2003-05-09 02:39:00 +00007762static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007763{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007764#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007765#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007766#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007767 int count;
7768 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007769 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7770 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007771 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007772 else if (ins->op == OP_DEREF) {
7773 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007774 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007775 else if (ins->op == OP_VAL) {
7776 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007777 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007778 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007779 /* Don't figure the depth of a call just guess it is huge */
7780 count = 1000;
7781 }
7782 else {
7783 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007784 expr = triple_rhs(state, ins, 0);
7785 for(;expr; expr = triple_rhs(state, ins, expr)) {
7786 if (*expr) {
7787 int depth;
7788 depth = expr_depth(state, *expr);
7789 if (depth > count) {
7790 count = depth;
7791 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007792 }
7793 }
7794 }
7795 return count + 1;
7796}
7797
Eric Biederman0babc1c2003-05-09 02:39:00 +00007798static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007799 struct compile_state *state, struct triple *first, struct triple *ptr,
7800 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007801{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007802 struct rhs_vector {
7803 int depth;
7804 struct triple **ins;
7805 } vector[MAX_RHS];
7806 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007807 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007808 rhs = ptr->rhs;
7809 lhs = ptr->lhs;
7810 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007811 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007812 ptr->op, tops(ptr->op));
7813 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007814 /* Find the depth of the rhs elements */
7815 for(i = 0; i < rhs; i++) {
7816 vector[i].ins = &RHS(ptr, i);
7817 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007818 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007819 /* Selection sort the rhs */
7820 for(i = 0; i < rhs; i++) {
7821 int j, max = i;
7822 for(j = i + 1; j < rhs; j++ ) {
7823 if (vector[j].depth > vector[max].depth) {
7824 max = j;
7825 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007826 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007827 if (max != i) {
7828 struct rhs_vector tmp;
7829 tmp = vector[i];
7830 vector[i] = vector[max];
7831 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007832 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007833 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007834 /* Now flatten the rhs elements */
7835 for(i = 0; i < rhs; i++) {
7836 *vector[i].ins = flatten(state, first, *vector[i].ins);
7837 use_triple(*vector[i].ins, ptr);
7838 }
Eric Biederman90089602004-05-28 14:11:54 +00007839 if (lhs) {
7840 insert_triple(state, first, ptr);
7841 ptr->id |= TRIPLE_FLAG_FLATTENED;
7842 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007843
Eric Biederman90089602004-05-28 14:11:54 +00007844 /* Now flatten the lhs elements */
7845 for(i = 0; i < lhs; i++) {
7846 struct triple **ins = &LHS(ptr, i);
7847 *ins = flatten(state, first, *ins);
7848 use_triple(*ins, ptr);
7849 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007850 }
7851 return ptr;
7852}
7853
Eric Biederman90089602004-05-28 14:11:54 +00007854static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007855 struct compile_state *state, struct triple *first, struct triple *ptr)
7856{
Eric Biederman90089602004-05-28 14:11:54 +00007857 struct triple *head, *body, *val;
7858 head = RHS(ptr, 0);
7859 RHS(ptr, 0) = 0;
7860 val = head->prev;
7861 body = head->next;
7862 release_triple(state, head);
7863 release_triple(state, ptr);
7864 val->next = first;
7865 body->prev = first->prev;
7866 body->prev->next = body;
7867 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007868
Eric Biederman90089602004-05-28 14:11:54 +00007869 if (triple_is_cbranch(state, body->prev) ||
7870 triple_is_call(state, body->prev)) {
7871 unuse_triple(first, body->prev);
7872 use_triple(body, body->prev);
7873 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007874
Eric Biederman90089602004-05-28 14:11:54 +00007875 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7876 internal_error(state, val, "val not flattened?");
7877 }
7878
7879 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007880}
7881
Eric Biederman90089602004-05-28 14:11:54 +00007882
7883static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007884 struct compile_state *state, struct triple *first, struct triple *ptr)
7885{
Eric Biederman90089602004-05-28 14:11:54 +00007886 if (!triple_is_part(state, ptr)) {
7887 internal_error(state, ptr, "not a part");
7888 }
7889 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7890 internal_error(state, ptr, "unexpected args for: %d %s",
7891 ptr->op, tops(ptr->op));
7892 }
7893 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7894 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007895 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007896}
7897
7898static struct triple *flatten(
7899 struct compile_state *state, struct triple *first, struct triple *ptr)
7900{
7901 struct triple *orig_ptr;
7902 if (!ptr)
7903 return 0;
7904 do {
7905 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007906 /* Only flatten triples once */
7907 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7908 return ptr;
7909 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007910 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007911 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007912 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7913 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007914 break;
Eric Biederman90089602004-05-28 14:11:54 +00007915 case OP_PROG:
7916 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007917 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007918 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007919 ptr = flatten_generic(state, first, ptr, 1);
7920 insert_triple(state, first, ptr);
7921 ptr->id |= TRIPLE_FLAG_FLATTENED;
7922 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7923 if (ptr->next != ptr) {
7924 use_triple(ptr->next, ptr);
7925 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007926 break;
7927 case OP_READ:
7928 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007929 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7930 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007931 break;
Eric Biederman90089602004-05-28 14:11:54 +00007932 case OP_WRITE:
7933 ptr = flatten_generic(state, first, ptr, 1);
7934 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7935 use_triple(MISC(ptr, 0), ptr);
7936 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007937 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007938 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007939 break;
7940 case OP_CBRANCH:
7941 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7942 use_triple(RHS(ptr, 0), ptr);
7943 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007944 insert_triple(state, first, ptr);
7945 ptr->id |= TRIPLE_FLAG_FLATTENED;
7946 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007947 if (ptr->next != ptr) {
7948 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007949 }
7950 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007951 case OP_CALL:
7952 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7953 use_triple(MISC(ptr, 0), ptr);
7954 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007955 insert_triple(state, first, ptr);
7956 ptr->id |= TRIPLE_FLAG_FLATTENED;
7957 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007958 if (ptr->next != ptr) {
7959 use_triple(ptr->next, ptr);
7960 }
7961 break;
7962 case OP_RET:
7963 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7964 use_triple(RHS(ptr, 0), ptr);
7965 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007966 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007967 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007968 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007969 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007970 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007971 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007972 break;
7973 case OP_DEREF:
7974 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007975 ptr = RHS(ptr, 0);
7976 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007977 free_triple(state, orig_ptr);
7978 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007979 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007980 if (RHS(ptr, 0)->op == OP_DEREF) {
7981 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007982 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007983 base = MISC(ptr, 0);
7984 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007985 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007986 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007987 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007988 int_const(state, &ulong_type, offset));
7989 free_triple(state, base);
7990 }
Eric Biederman90089602004-05-28 14:11:54 +00007991 else {
7992 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007993 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007994 break;
Eric Biederman90089602004-05-28 14:11:54 +00007995 case OP_INDEX:
7996 if (RHS(ptr, 0)->op == OP_DEREF) {
7997 struct triple *base, *left;
7998 ulong_t offset;
7999 base = MISC(ptr, 0);
8000 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8001 left = RHS(base, 0);
8002 ptr = triple(state, OP_ADD, left->type,
8003 read_expr(state, left),
8004 int_const(state, &long_type, offset));
8005 free_triple(state, base);
8006 }
8007 else {
8008 ptr = flatten_part(state, first, ptr);
8009 }
8010 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008011 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008012 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008013 use_triple(ptr, MISC(ptr, 0));
8014 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008015 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008016 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8017 use_triple(MISC(ptr, 0), ptr);
8018 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008019 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008020 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008021 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8022 use_triple(MISC(ptr, 0), ptr);
8023 insert_triple(state, first, ptr);
8024 ptr->id |= TRIPLE_FLAG_FLATTENED;
8025 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8026 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008027 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008028 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008029 break;
8030 default:
8031 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008032 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008033 break;
8034 }
8035 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008036 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008037 insert_triple(state, first, ptr);
8038 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008039 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008040 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008041 return ptr;
8042}
8043
8044static void release_expr(struct compile_state *state, struct triple *expr)
8045{
8046 struct triple *head;
8047 head = label(state);
8048 flatten(state, head, expr);
8049 while(head->next != head) {
8050 release_triple(state, head->next);
8051 }
8052 free_triple(state, head);
8053}
8054
8055static int replace_rhs_use(struct compile_state *state,
8056 struct triple *orig, struct triple *new, struct triple *use)
8057{
8058 struct triple **expr;
8059 int found;
8060 found = 0;
8061 expr = triple_rhs(state, use, 0);
8062 for(;expr; expr = triple_rhs(state, use, expr)) {
8063 if (*expr == orig) {
8064 *expr = new;
8065 found = 1;
8066 }
8067 }
8068 if (found) {
8069 unuse_triple(orig, use);
8070 use_triple(new, use);
8071 }
8072 return found;
8073}
8074
8075static int replace_lhs_use(struct compile_state *state,
8076 struct triple *orig, struct triple *new, struct triple *use)
8077{
8078 struct triple **expr;
8079 int found;
8080 found = 0;
8081 expr = triple_lhs(state, use, 0);
8082 for(;expr; expr = triple_lhs(state, use, expr)) {
8083 if (*expr == orig) {
8084 *expr = new;
8085 found = 1;
8086 }
8087 }
8088 if (found) {
8089 unuse_triple(orig, use);
8090 use_triple(new, use);
8091 }
8092 return found;
8093}
8094
Eric Biederman90089602004-05-28 14:11:54 +00008095static int replace_misc_use(struct compile_state *state,
8096 struct triple *orig, struct triple *new, struct triple *use)
8097{
8098 struct triple **expr;
8099 int found;
8100 found = 0;
8101 expr = triple_misc(state, use, 0);
8102 for(;expr; expr = triple_misc(state, use, expr)) {
8103 if (*expr == orig) {
8104 *expr = new;
8105 found = 1;
8106 }
8107 }
8108 if (found) {
8109 unuse_triple(orig, use);
8110 use_triple(new, use);
8111 }
8112 return found;
8113}
8114
8115static int replace_targ_use(struct compile_state *state,
8116 struct triple *orig, struct triple *new, struct triple *use)
8117{
8118 struct triple **expr;
8119 int found;
8120 found = 0;
8121 expr = triple_targ(state, use, 0);
8122 for(;expr; expr = triple_targ(state, use, expr)) {
8123 if (*expr == orig) {
8124 *expr = new;
8125 found = 1;
8126 }
8127 }
8128 if (found) {
8129 unuse_triple(orig, use);
8130 use_triple(new, use);
8131 }
8132 return found;
8133}
8134
8135static void replace_use(struct compile_state *state,
8136 struct triple *orig, struct triple *new, struct triple *use)
8137{
8138 int found;
8139 found = 0;
8140 found |= replace_rhs_use(state, orig, new, use);
8141 found |= replace_lhs_use(state, orig, new, use);
8142 found |= replace_misc_use(state, orig, new, use);
8143 found |= replace_targ_use(state, orig, new, use);
8144 if (!found) {
8145 internal_error(state, use, "use without use");
8146 }
8147}
8148
Eric Biedermanb138ac82003-04-22 18:44:01 +00008149static void propogate_use(struct compile_state *state,
8150 struct triple *orig, struct triple *new)
8151{
8152 struct triple_set *user, *next;
8153 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008154 /* Careful replace_use modifies the use chain and
8155 * removes use. So we must get a copy of the next
8156 * entry early.
8157 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008158 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008159 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008160 }
8161 if (orig->use) {
8162 internal_error(state, orig, "used after propogate_use");
8163 }
8164}
8165
8166/*
8167 * Code generators
8168 * ===========================
8169 */
8170
Eric Biederman90089602004-05-28 14:11:54 +00008171static struct triple *mk_cast_expr(
8172 struct compile_state *state, struct type *type, struct triple *expr)
8173{
8174 struct triple *def;
8175 def = read_expr(state, expr);
8176 def = triple(state, OP_CONVERT, type, def, 0);
8177 return def;
8178}
8179
Eric Biedermanb138ac82003-04-22 18:44:01 +00008180static struct triple *mk_add_expr(
8181 struct compile_state *state, struct triple *left, struct triple *right)
8182{
8183 struct type *result_type;
8184 /* Put pointer operands on the left */
8185 if (is_pointer(right)) {
8186 struct triple *tmp;
8187 tmp = left;
8188 left = right;
8189 right = tmp;
8190 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008191 left = read_expr(state, left);
8192 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008193 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008194 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008195 struct type *ptr_math;
8196 int op;
8197 if (is_signed(right->type)) {
8198 ptr_math = &long_type;
8199 op = OP_SMUL;
8200 } else {
8201 ptr_math = &ulong_type;
8202 op = OP_UMUL;
8203 }
8204 if (!equiv_types(right->type, ptr_math)) {
8205 right = mk_cast_expr(state, ptr_math, right);
8206 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008207 right = triple(state, op, ptr_math, right,
8208 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008209 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008210 }
8211 return triple(state, OP_ADD, result_type, left, right);
8212}
8213
8214static struct triple *mk_sub_expr(
8215 struct compile_state *state, struct triple *left, struct triple *right)
8216{
8217 struct type *result_type;
8218 result_type = ptr_arithmetic_result(state, left, right);
8219 left = read_expr(state, left);
8220 right = read_expr(state, right);
8221 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008222 struct type *ptr_math;
8223 int op;
8224 if (is_signed(right->type)) {
8225 ptr_math = &long_type;
8226 op = OP_SMUL;
8227 } else {
8228 ptr_math = &ulong_type;
8229 op = OP_UMUL;
8230 }
8231 if (!equiv_types(right->type, ptr_math)) {
8232 right = mk_cast_expr(state, ptr_math, right);
8233 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008234 right = triple(state, op, ptr_math, right,
8235 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008236 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008237 }
8238 return triple(state, OP_SUB, result_type, left, right);
8239}
8240
8241static struct triple *mk_pre_inc_expr(
8242 struct compile_state *state, struct triple *def)
8243{
8244 struct triple *val;
8245 lvalue(state, def);
8246 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8247 return triple(state, OP_VAL, def->type,
8248 write_expr(state, def, val),
8249 val);
8250}
8251
8252static struct triple *mk_pre_dec_expr(
8253 struct compile_state *state, struct triple *def)
8254{
8255 struct triple *val;
8256 lvalue(state, def);
8257 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8258 return triple(state, OP_VAL, def->type,
8259 write_expr(state, def, val),
8260 val);
8261}
8262
8263static struct triple *mk_post_inc_expr(
8264 struct compile_state *state, struct triple *def)
8265{
8266 struct triple *val;
8267 lvalue(state, def);
8268 val = read_expr(state, def);
8269 return triple(state, OP_VAL, def->type,
8270 write_expr(state, def,
8271 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8272 , val);
8273}
8274
8275static struct triple *mk_post_dec_expr(
8276 struct compile_state *state, struct triple *def)
8277{
8278 struct triple *val;
8279 lvalue(state, def);
8280 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008281 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008282 write_expr(state, def,
8283 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8284 , val);
8285}
8286
8287static struct triple *mk_subscript_expr(
8288 struct compile_state *state, struct triple *left, struct triple *right)
8289{
8290 left = read_expr(state, left);
8291 right = read_expr(state, right);
8292 if (!is_pointer(left) && !is_pointer(right)) {
8293 error(state, left, "subscripted value is not a pointer");
8294 }
8295 return mk_deref_expr(state, mk_add_expr(state, left, right));
8296}
8297
Eric Biedermane058a1e2003-07-12 01:21:31 +00008298
Eric Biedermanb138ac82003-04-22 18:44:01 +00008299/*
8300 * Compile time evaluation
8301 * ===========================
8302 */
8303static int is_const(struct triple *ins)
8304{
8305 return IS_CONST_OP(ins->op);
8306}
8307
Eric Biederman83b991a2003-10-11 06:20:25 +00008308static int is_simple_const(struct triple *ins)
8309{
Eric Biederman90089602004-05-28 14:11:54 +00008310 /* Is this a constant that u.cval has the value.
8311 * Or equivalently is this a constant that read_const
8312 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008313 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008314 */
8315 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008316}
8317
Stefan Reinauer14e22772010-04-27 06:56:47 +00008318static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008319 struct triple *left, struct triple *right)
8320{
8321 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008322 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8323 equal = 0;
8324 }
8325 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008326 equal = 0;
8327 }
8328 else if (left->op != right->op) {
8329 equal = 0;
8330 }
8331 else if (!equiv_types(left->type, right->type)) {
8332 equal = 0;
8333 }
8334 else {
8335 equal = 0;
8336 switch(left->op) {
8337 case OP_INTCONST:
8338 if (left->u.cval == right->u.cval) {
8339 equal = 1;
8340 }
8341 break;
8342 case OP_BLOBCONST:
8343 {
Eric Biederman90089602004-05-28 14:11:54 +00008344 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008345 lsize = size_of(state, left->type);
8346 rsize = size_of(state, right->type);
8347 if (lsize != rsize) {
8348 break;
8349 }
Eric Biederman90089602004-05-28 14:11:54 +00008350 bytes = bits_to_bytes(lsize);
8351 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008352 equal = 1;
8353 }
8354 break;
8355 }
8356 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008357 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008358 (left->u.cval == right->u.cval)) {
8359 equal = 1;
8360 }
8361 break;
8362 default:
8363 internal_error(state, left, "uknown constant type");
8364 break;
8365 }
8366 }
8367 return equal;
8368}
8369
8370static int is_zero(struct triple *ins)
8371{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008372 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008373}
8374
8375static int is_one(struct triple *ins)
8376{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008377 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008378}
8379
Stefan Reinauer50542a82007-10-24 11:14:14 +00008380#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008381static long_t bit_count(ulong_t value)
8382{
8383 int count;
8384 int i;
8385 count = 0;
8386 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8387 ulong_t mask;
8388 mask = 1;
8389 mask <<= i;
8390 if (value & mask) {
8391 count++;
8392 }
8393 }
8394 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008395
Eric Biederman530b5192003-07-01 10:05:30 +00008396}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008397#endif
8398
Eric Biedermanb138ac82003-04-22 18:44:01 +00008399static long_t bsr(ulong_t value)
8400{
8401 int i;
8402 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8403 ulong_t mask;
8404 mask = 1;
8405 mask <<= i;
8406 if (value & mask) {
8407 return i;
8408 }
8409 }
8410 return -1;
8411}
8412
8413static long_t bsf(ulong_t value)
8414{
8415 int i;
8416 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8417 ulong_t mask;
8418 mask = 1;
8419 mask <<= 1;
8420 if (value & mask) {
8421 return i;
8422 }
8423 }
8424 return -1;
8425}
8426
Eric Biedermancb364952004-11-15 10:46:44 +00008427static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008428{
8429 return bsr(value);
8430}
8431
8432static long_t tlog2(struct triple *ins)
8433{
Eric Biedermancb364952004-11-15 10:46:44 +00008434 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008435}
8436
8437static int is_pow2(struct triple *ins)
8438{
8439 ulong_t value, mask;
8440 long_t log;
8441 if (!is_const(ins)) {
8442 return 0;
8443 }
8444 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008445 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008446 if (log == -1) {
8447 return 0;
8448 }
8449 mask = 1;
8450 mask <<= log;
8451 return ((value & mask) == value);
8452}
8453
8454static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008455 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008456{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008457 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008458 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008459 case TYPE_SHORT:
8460 case TYPE_INT:
8461 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008462 case TYPE_UCHAR:
8463 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008464 case TYPE_UINT:
8465 case TYPE_ULONG:
8466 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008467 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008468 break;
8469 default:
Eric Biederman90089602004-05-28 14:11:54 +00008470 fprintf(state->errout, "type: ");
8471 name_of(state->errout, rhs->type);
8472 fprintf(state->errout, "\n");
8473 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008474 break;
8475 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008476 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008477 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008478 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008479 return rhs->u.cval;
8480}
8481
Eric Biederman5ade04a2003-10-22 04:03:46 +00008482static long_t read_sconst(struct compile_state *state,
8483 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008484{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008485 return (long_t)(rhs->u.cval);
8486}
8487
Eric Biederman5ade04a2003-10-22 04:03:46 +00008488int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8489{
8490 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008491 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008492 }
8493 return !is_zero(rhs);
8494}
8495
8496int const_eq(struct compile_state *state, struct triple *ins,
8497 struct triple *left, struct triple *right)
8498{
8499 int result;
8500 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008501 internal_warning(state, ins, "non const passed to const_eq");
8502 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008503 }
8504 else if (left == right) {
8505 result = 1;
8506 }
8507 else if (is_simple_const(left) && is_simple_const(right)) {
8508 ulong_t lval, rval;
8509 lval = read_const(state, ins, left);
8510 rval = read_const(state, ins, right);
8511 result = (lval == rval);
8512 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008513 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008514 (right->op == OP_ADDRCONST)) {
8515 result = (MISC(left, 0) == MISC(right, 0)) &&
8516 (left->u.cval == right->u.cval);
8517 }
8518 else {
Eric Biederman90089602004-05-28 14:11:54 +00008519 internal_warning(state, ins, "incomparable constants passed to const_eq");
8520 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008521 }
8522 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008523
Eric Biederman5ade04a2003-10-22 04:03:46 +00008524}
8525
8526int const_ucmp(struct compile_state *state, struct triple *ins,
8527 struct triple *left, struct triple *right)
8528{
8529 int result;
8530 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008531 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008532 result = -2;
8533 }
8534 else if (left == right) {
8535 result = 0;
8536 }
8537 else if (is_simple_const(left) && is_simple_const(right)) {
8538 ulong_t lval, rval;
8539 lval = read_const(state, ins, left);
8540 rval = read_const(state, ins, right);
8541 result = 0;
8542 if (lval > rval) {
8543 result = 1;
8544 } else if (rval > lval) {
8545 result = -1;
8546 }
8547 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008548 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008549 (right->op == OP_ADDRCONST) &&
8550 (MISC(left, 0) == MISC(right, 0))) {
8551 result = 0;
8552 if (left->u.cval > right->u.cval) {
8553 result = 1;
8554 } else if (left->u.cval < right->u.cval) {
8555 result = -1;
8556 }
8557 }
8558 else {
Eric Biederman90089602004-05-28 14:11:54 +00008559 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008560 result = -2;
8561 }
8562 return result;
8563}
8564
8565int const_scmp(struct compile_state *state, struct triple *ins,
8566 struct triple *left, struct triple *right)
8567{
8568 int result;
8569 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008570 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008571 result = -2;
8572 }
8573 else if (left == right) {
8574 result = 0;
8575 }
8576 else if (is_simple_const(left) && is_simple_const(right)) {
8577 long_t lval, rval;
8578 lval = read_sconst(state, ins, left);
8579 rval = read_sconst(state, ins, right);
8580 result = 0;
8581 if (lval > rval) {
8582 result = 1;
8583 } else if (rval > lval) {
8584 result = -1;
8585 }
8586 }
8587 else {
Eric Biederman90089602004-05-28 14:11:54 +00008588 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008589 result = -2;
8590 }
8591 return result;
8592}
8593
Eric Biedermanb138ac82003-04-22 18:44:01 +00008594static void unuse_rhs(struct compile_state *state, struct triple *ins)
8595{
8596 struct triple **expr;
8597 expr = triple_rhs(state, ins, 0);
8598 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008599 if (*expr) {
8600 unuse_triple(*expr, ins);
8601 *expr = 0;
8602 }
8603 }
8604}
8605
8606static void unuse_lhs(struct compile_state *state, struct triple *ins)
8607{
8608 struct triple **expr;
8609 expr = triple_lhs(state, ins, 0);
8610 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008611 unuse_triple(*expr, ins);
8612 *expr = 0;
8613 }
8614}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008615
Stefan Reinauer50542a82007-10-24 11:14:14 +00008616#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008617static void unuse_misc(struct compile_state *state, struct triple *ins)
8618{
8619 struct triple **expr;
8620 expr = triple_misc(state, ins, 0);
8621 for(;expr;expr = triple_misc(state, ins, expr)) {
8622 unuse_triple(*expr, ins);
8623 *expr = 0;
8624 }
8625}
8626
8627static void unuse_targ(struct compile_state *state, struct triple *ins)
8628{
8629 int i;
8630 struct triple **slot;
8631 slot = &TARG(ins, 0);
8632 for(i = 0; i < ins->targ; i++) {
8633 unuse_triple(slot[i], ins);
8634 slot[i] = 0;
8635 }
8636}
8637
Eric Biedermanb138ac82003-04-22 18:44:01 +00008638static void check_lhs(struct compile_state *state, struct triple *ins)
8639{
8640 struct triple **expr;
8641 expr = triple_lhs(state, ins, 0);
8642 for(;expr;expr = triple_lhs(state, ins, expr)) {
8643 internal_error(state, ins, "unexpected lhs");
8644 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008645
Eric Biedermanb138ac82003-04-22 18:44:01 +00008646}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008647#endif
Eric Biederman90089602004-05-28 14:11:54 +00008648
8649static void check_misc(struct compile_state *state, struct triple *ins)
8650{
8651 struct triple **expr;
8652 expr = triple_misc(state, ins, 0);
8653 for(;expr;expr = triple_misc(state, ins, expr)) {
8654 if (*expr) {
8655 internal_error(state, ins, "unexpected misc");
8656 }
8657 }
8658}
8659
Eric Biedermanb138ac82003-04-22 18:44:01 +00008660static void check_targ(struct compile_state *state, struct triple *ins)
8661{
8662 struct triple **expr;
8663 expr = triple_targ(state, ins, 0);
8664 for(;expr;expr = triple_targ(state, ins, expr)) {
8665 internal_error(state, ins, "unexpected targ");
8666 }
8667}
8668
8669static void wipe_ins(struct compile_state *state, struct triple *ins)
8670{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008671 /* Becareful which instructions you replace the wiped
8672 * instruction with, as there are not enough slots
8673 * in all instructions to hold all others.
8674 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008675 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008676 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008677 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008678 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008679 ins->lhs = 0;
8680 ins->rhs = 0;
8681 ins->misc = 0;
8682 ins->targ = 0;
8683}
8684
Stefan Reinauer50542a82007-10-24 11:14:14 +00008685#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008686static void wipe_branch(struct compile_state *state, struct triple *ins)
8687{
8688 /* Becareful which instructions you replace the wiped
8689 * instruction with, as there are not enough slots
8690 * in all instructions to hold all others.
8691 */
8692 unuse_rhs(state, ins);
8693 unuse_lhs(state, ins);
8694 unuse_misc(state, ins);
8695 unuse_targ(state, ins);
8696 ins->lhs = 0;
8697 ins->rhs = 0;
8698 ins->misc = 0;
8699 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008700}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008701#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008702
Stefan Reinauer14e22772010-04-27 06:56:47 +00008703static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008704 struct triple *ins, struct triple *rhs)
8705{
Eric Biederman83b991a2003-10-11 06:20:25 +00008706 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008707 if (!equiv_types(ins->type, rhs->type)) {
8708 FILE *fp = state->errout;
8709 fprintf(fp, "src type: ");
8710 name_of(fp, rhs->type);
8711 fprintf(fp, "\ndst type: ");
8712 name_of(fp, ins->type);
8713 fprintf(fp, "\n");
8714 internal_error(state, ins, "mkcopy type mismatch");
8715 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008716 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008717 wipe_ins(state, ins);
8718 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008719 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008720 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008721 RHS(ins, 0) = rhs;
8722 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008723}
8724
Stefan Reinauer14e22772010-04-27 06:56:47 +00008725static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008726 struct triple *ins, ulong_t value)
8727{
8728 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008729 fprintf(state->errout, "type: ");
8730 name_of(state->errout, ins->type);
8731 fprintf(state->errout, "\n");
8732 internal_error(state, ins, "unknown type to make constant value: %ld",
8733 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008734 }
8735 wipe_ins(state, ins);
8736 ins->op = OP_INTCONST;
8737 ins->u.cval = value;
8738}
8739
8740static void mkaddr_const(struct compile_state *state,
8741 struct triple *ins, struct triple *sdecl, ulong_t value)
8742{
Eric Biederman90089602004-05-28 14:11:54 +00008743 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008744 internal_error(state, ins, "bad base for addrconst");
8745 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008746 wipe_ins(state, ins);
8747 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008748 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008749 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008750 ins->u.cval = value;
8751 use_triple(sdecl, ins);
8752}
8753
Eric Biederman90089602004-05-28 14:11:54 +00008754#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008755static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008756 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008757{
Eric Biederman90089602004-05-28 14:11:54 +00008758 FILE *fp = state->dbgout;
8759 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8760 name_of(fp, tuple->type);
8761 if (tuple->lhs > 0) {
8762 fprintf(fp, " lhs: ");
8763 name_of(fp, LHS(tuple, 0)->type);
8764 }
8765 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008766
Eric Biederman90089602004-05-28 14:11:54 +00008767}
8768#endif
8769
Stefan Reinauer14e22772010-04-27 06:56:47 +00008770static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008771 struct triple *ins, struct triple *tuple)
8772{
8773 struct triple *next;
8774 next = ins->next;
8775 flatten(state, next, tuple);
8776#if DEBUG_DECOMPOSE_PRINT_TUPLES
8777 print_tuple(state, ins, tuple);
8778#endif
8779
8780 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8781 struct triple *tmp;
8782 if (tuple->lhs != 1) {
8783 internal_error(state, tuple, "plain type in multiple registers?");
8784 }
8785 tmp = LHS(tuple, 0);
8786 release_triple(state, tuple);
8787 tuple = tmp;
8788 }
8789
8790 propogate_use(state, ins, tuple);
8791 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008792
Eric Biederman90089602004-05-28 14:11:54 +00008793 return next;
8794}
8795
8796static struct triple *decompose_unknownval(struct compile_state *state,
8797 struct triple *ins)
8798{
8799 struct triple *tuple;
8800 ulong_t i;
8801
8802#if DEBUG_DECOMPOSE_HIRES
8803 FILE *fp = state->dbgout;
8804 fprintf(fp, "unknown type: ");
8805 name_of(fp, ins->type);
8806 fprintf(fp, "\n");
8807#endif
8808
8809 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008810 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008811 ins->occurance);
8812
8813 for(i = 0; i < tuple->lhs; i++) {
8814 struct type *piece_type;
8815 struct triple *unknown;
8816
8817 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8818 get_occurance(tuple->occurance);
8819 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8820 tuple->occurance);
8821 LHS(tuple, i) = unknown;
8822 }
8823 return decompose_with_tuple(state, ins, tuple);
8824}
8825
8826
Stefan Reinauer14e22772010-04-27 06:56:47 +00008827static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008828 struct triple *ins)
8829{
8830 struct triple *tuple, *lval;
8831 ulong_t i;
8832
8833 lval = RHS(ins, 0);
8834
8835 if (lval->op == OP_PIECE) {
8836 return ins->next;
8837 }
8838 get_occurance(ins->occurance);
8839 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8840 ins->occurance);
8841
8842 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008843 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008844 {
8845 internal_error(state, ins, "lhs size inconsistency?");
8846 }
8847 for(i = 0; i < tuple->lhs; i++) {
8848 struct triple *piece, *read, *bitref;
8849 if ((i != 0) || !triple_is_def(state, lval)) {
8850 piece = LHS(lval, i);
8851 } else {
8852 piece = lval;
8853 }
8854
8855 /* See if the piece is really a bitref */
8856 bitref = 0;
8857 if (piece->op == OP_BITREF) {
8858 bitref = piece;
8859 piece = RHS(bitref, 0);
8860 }
8861
8862 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008863 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008864 tuple->occurance);
8865 RHS(read, 0) = piece;
8866
8867 if (bitref) {
8868 struct triple *extract;
8869 int op;
8870 if (is_signed(bitref->type->left)) {
8871 op = OP_SEXTRACT;
8872 } else {
8873 op = OP_UEXTRACT;
8874 }
8875 get_occurance(tuple->occurance);
8876 extract = alloc_triple(state, op, bitref->type, -1, -1,
8877 tuple->occurance);
8878 RHS(extract, 0) = read;
8879 extract->u.bitfield.size = bitref->u.bitfield.size;
8880 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8881
8882 read = extract;
8883 }
8884
8885 LHS(tuple, i) = read;
8886 }
8887 return decompose_with_tuple(state, ins, tuple);
8888}
8889
Stefan Reinauer14e22772010-04-27 06:56:47 +00008890static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008891 struct triple *ins)
8892{
8893 struct triple *tuple, *lval, *val;
8894 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008895
Eric Biederman90089602004-05-28 14:11:54 +00008896 lval = MISC(ins, 0);
8897 val = RHS(ins, 0);
8898 get_occurance(ins->occurance);
8899 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8900 ins->occurance);
8901
8902 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008903 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008904 {
8905 internal_error(state, ins, "lhs size inconsistency?");
8906 }
8907 for(i = 0; i < tuple->lhs; i++) {
8908 struct triple *piece, *write, *pval, *bitref;
8909 if ((i != 0) || !triple_is_def(state, lval)) {
8910 piece = LHS(lval, i);
8911 } else {
8912 piece = lval;
8913 }
8914 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8915 pval = val;
8916 }
8917 else {
8918 if (i > val->lhs) {
8919 internal_error(state, ins, "lhs size inconsistency?");
8920 }
8921 pval = LHS(val, i);
8922 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008923
Eric Biederman90089602004-05-28 14:11:54 +00008924 /* See if the piece is really a bitref */
8925 bitref = 0;
8926 if (piece->op == OP_BITREF) {
8927 struct triple *read, *deposit;
8928 bitref = piece;
8929 piece = RHS(bitref, 0);
8930
8931 /* Read the destination register */
8932 get_occurance(tuple->occurance);
8933 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8934 tuple->occurance);
8935 RHS(read, 0) = piece;
8936
8937 /* Deposit the new bitfield value */
8938 get_occurance(tuple->occurance);
8939 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8940 tuple->occurance);
8941 RHS(deposit, 0) = read;
8942 RHS(deposit, 1) = pval;
8943 deposit->u.bitfield.size = bitref->u.bitfield.size;
8944 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8945
8946 /* Now write the newly generated value */
8947 pval = deposit;
8948 }
8949
8950 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008951 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008952 tuple->occurance);
8953 MISC(write, 0) = piece;
8954 RHS(write, 0) = pval;
8955 LHS(tuple, i) = write;
8956 }
8957 return decompose_with_tuple(state, ins, tuple);
8958}
8959
8960struct decompose_load_info {
8961 struct occurance *occurance;
8962 struct triple *lval;
8963 struct triple *tuple;
8964};
8965static void decompose_load_cb(struct compile_state *state,
8966 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8967{
8968 struct decompose_load_info *info = arg;
8969 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008970
Eric Biederman90089602004-05-28 14:11:54 +00008971 if (reg_offset > info->tuple->lhs) {
8972 internal_error(state, info->tuple, "lhs to small?");
8973 }
8974 get_occurance(info->occurance);
8975 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8976 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8977 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8978}
8979
Stefan Reinauer14e22772010-04-27 06:56:47 +00008980static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008981 struct triple *ins)
8982{
8983 struct triple *tuple;
8984 struct decompose_load_info info;
8985
8986 if (!is_compound_type(ins->type)) {
8987 return ins->next;
8988 }
8989 get_occurance(ins->occurance);
8990 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8991 ins->occurance);
8992
8993 info.occurance = ins->occurance;
8994 info.lval = RHS(ins, 0);
8995 info.tuple = tuple;
8996 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8997
8998 return decompose_with_tuple(state, ins, tuple);
8999}
9000
9001
9002struct decompose_store_info {
9003 struct occurance *occurance;
9004 struct triple *lval;
9005 struct triple *val;
9006 struct triple *tuple;
9007};
9008static void decompose_store_cb(struct compile_state *state,
9009 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9010{
9011 struct decompose_store_info *info = arg;
9012 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009013
Eric Biederman90089602004-05-28 14:11:54 +00009014 if (reg_offset > info->tuple->lhs) {
9015 internal_error(state, info->tuple, "lhs to small?");
9016 }
9017 get_occurance(info->occurance);
9018 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9019 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9020 RHS(store, 1) = LHS(info->val, reg_offset);
9021 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9022}
9023
Stefan Reinauer14e22772010-04-27 06:56:47 +00009024static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009025 struct triple *ins)
9026{
9027 struct triple *tuple;
9028 struct decompose_store_info info;
9029
9030 if (!is_compound_type(ins->type)) {
9031 return ins->next;
9032 }
9033 get_occurance(ins->occurance);
9034 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9035 ins->occurance);
9036
9037 info.occurance = ins->occurance;
9038 info.lval = RHS(ins, 0);
9039 info.val = RHS(ins, 1);
9040 info.tuple = tuple;
9041 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9042
9043 return decompose_with_tuple(state, ins, tuple);
9044}
9045
Stefan Reinauer14e22772010-04-27 06:56:47 +00009046static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009047 struct triple *ins)
9048{
9049 struct triple *tuple, *lval;
9050 struct type *type;
9051 size_t reg_offset;
9052 int i, idx;
9053
9054 lval = MISC(ins, 0);
9055 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9056 idx = reg_offset/REG_SIZEOF_REG;
9057 type = field_type(state, lval->type, ins->u.field);
9058#if DEBUG_DECOMPOSE_HIRES
9059 {
9060 FILE *fp = state->dbgout;
9061 fprintf(fp, "field type: ");
9062 name_of(fp, type);
9063 fprintf(fp, "\n");
9064 }
9065#endif
9066
9067 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009068 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009069 ins->occurance);
9070
9071 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9072 (tuple->lhs != 1))
9073 {
9074 internal_error(state, ins, "multi register bitfield?");
9075 }
9076
9077 for(i = 0; i < tuple->lhs; i++, idx++) {
9078 struct triple *piece;
9079 if (!triple_is_def(state, lval)) {
9080 if (idx > lval->lhs) {
9081 internal_error(state, ins, "inconsistent lhs count");
9082 }
9083 piece = LHS(lval, idx);
9084 } else {
9085 if (idx != 0) {
9086 internal_error(state, ins, "bad reg_offset into def");
9087 }
9088 if (i != 0) {
9089 internal_error(state, ins, "bad reg count from def");
9090 }
9091 piece = lval;
9092 }
9093
9094 /* Remember the offset of the bitfield */
9095 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9096 get_occurance(ins->occurance);
9097 piece = build_triple(state, OP_BITREF, type, piece, 0,
9098 ins->occurance);
9099 piece->u.bitfield.size = size_of(state, type);
9100 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9101 }
9102 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009103 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009104 "request for a nonbitfield sub register?");
9105 }
9106
9107 LHS(tuple, i) = piece;
9108 }
9109
9110 return decompose_with_tuple(state, ins, tuple);
9111}
9112
Stefan Reinauer14e22772010-04-27 06:56:47 +00009113static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009114 struct triple *ins)
9115{
9116 struct triple *tuple, *lval;
9117 struct type *type;
9118 int i, idx;
9119
9120 lval = MISC(ins, 0);
9121 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9122 type = index_type(state, lval->type, ins->u.cval);
9123#if DEBUG_DECOMPOSE_HIRES
9124{
9125 FILE *fp = state->dbgout;
9126 fprintf(fp, "index type: ");
9127 name_of(fp, type);
9128 fprintf(fp, "\n");
9129}
9130#endif
9131
9132 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009133 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009134 ins->occurance);
9135
9136 for(i = 0; i < tuple->lhs; i++, idx++) {
9137 struct triple *piece;
9138 if (!triple_is_def(state, lval)) {
9139 if (idx > lval->lhs) {
9140 internal_error(state, ins, "inconsistent lhs count");
9141 }
9142 piece = LHS(lval, idx);
9143 } else {
9144 if (idx != 0) {
9145 internal_error(state, ins, "bad reg_offset into def");
9146 }
9147 if (i != 0) {
9148 internal_error(state, ins, "bad reg count from def");
9149 }
9150 piece = lval;
9151 }
9152 LHS(tuple, i) = piece;
9153 }
9154
9155 return decompose_with_tuple(state, ins, tuple);
9156}
9157
9158static void decompose_compound_types(struct compile_state *state)
9159{
9160 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009161 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009162 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009163
9164 /* Pass one expand compound values into pseudo registers.
9165 */
9166 next = first;
9167 do {
9168 ins = next;
9169 next = ins->next;
9170 switch(ins->op) {
9171 case OP_UNKNOWNVAL:
9172 next = decompose_unknownval(state, ins);
9173 break;
9174
9175 case OP_READ:
9176 next = decompose_read(state, ins);
9177 break;
9178
9179 case OP_WRITE:
9180 next = decompose_write(state, ins);
9181 break;
9182
9183
9184 /* Be very careful with the load/store logic. These
9185 * operations must convert from the in register layout
9186 * to the in memory layout, which is nontrivial.
9187 */
9188 case OP_LOAD:
9189 next = decompose_load(state, ins);
9190 break;
9191 case OP_STORE:
9192 next = decompose_store(state, ins);
9193 break;
9194
9195 case OP_DOT:
9196 next = decompose_dot(state, ins);
9197 break;
9198 case OP_INDEX:
9199 next = decompose_index(state, ins);
9200 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009201
Eric Biederman90089602004-05-28 14:11:54 +00009202 }
9203#if DEBUG_DECOMPOSE_HIRES
9204 fprintf(fp, "decompose next: %p \n", next);
9205 fflush(fp);
9206 fprintf(fp, "next->op: %d %s\n",
9207 next->op, tops(next->op));
9208 /* High resolution debugging mode */
9209 print_triples(state);
9210#endif
9211 } while (next != first);
9212
9213 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009214 */
9215 ins = first;
9216 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009217 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009218 if (ins->op == OP_TUPLE) {
9219 if (ins->use) {
9220 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009221 }
Eric Biederman90089602004-05-28 14:11:54 +00009222 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009223 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009224 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009225 }
Eric Biederman90089602004-05-28 14:11:54 +00009226 ins = next;
9227 } while(ins != first);
9228 ins = first;
9229 do {
9230 next = ins->next;
9231 if (ins->op == OP_BITREF) {
9232 if (ins->use) {
9233 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +00009234 }
Eric Biederman90089602004-05-28 14:11:54 +00009235 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009236 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009237 }
9238 }
9239 ins = next;
9240 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009241
Eric Biederman0babc1c2003-05-09 02:39:00 +00009242 /* Pass three verify the state and set ->id to 0.
9243 */
Eric Biederman90089602004-05-28 14:11:54 +00009244 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009245 do {
Eric Biederman90089602004-05-28 14:11:54 +00009246 ins = next;
9247 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009248 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009249 if (triple_stores_block(state, ins)) {
9250 ins->u.block = 0;
9251 }
9252 if (triple_is_def(state, ins)) {
9253 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9254 internal_error(state, ins, "multi register value remains?");
9255 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009256 }
9257 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009258 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009259 }
Eric Biederman90089602004-05-28 14:11:54 +00009260 if (ins->op == OP_INDEX) {
9261 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009262 }
Eric Biederman90089602004-05-28 14:11:54 +00009263 if (ins->op == OP_BITREF) {
9264 internal_error(state, ins, "OP_BITREF remains?");
9265 }
9266 if (ins->op == OP_TUPLE) {
9267 internal_error(state, ins, "OP_TUPLE remains?");
9268 }
9269 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009270}
9271
Eric Biedermanb138ac82003-04-22 18:44:01 +00009272/* For those operations that cannot be simplified */
9273static void simplify_noop(struct compile_state *state, struct triple *ins)
9274{
9275 return;
9276}
9277
9278static void simplify_smul(struct compile_state *state, struct triple *ins)
9279{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009280 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009281 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009282 tmp = RHS(ins, 0);
9283 RHS(ins, 0) = RHS(ins, 1);
9284 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009285 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009286 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009287 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009288 left = read_sconst(state, ins, RHS(ins, 0));
9289 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009290 mkconst(state, ins, left * right);
9291 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009292 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009293 mkconst(state, ins, 0);
9294 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009295 else if (is_one(RHS(ins, 1))) {
9296 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009297 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009299 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009300 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009301 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009302 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009303 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009305 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009306 }
9307}
9308
9309static void simplify_umul(struct compile_state *state, struct triple *ins)
9310{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009311 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009312 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009313 tmp = RHS(ins, 0);
9314 RHS(ins, 0) = RHS(ins, 1);
9315 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009316 }
Eric Biederman90089602004-05-28 14:11:54 +00009317 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009318 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009319 left = read_const(state, ins, RHS(ins, 0));
9320 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009321 mkconst(state, ins, left * right);
9322 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009323 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009324 mkconst(state, ins, 0);
9325 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009326 else if (is_one(RHS(ins, 1))) {
9327 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009328 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009329 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009330 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009331 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009332 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009333 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009334 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009335 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009336 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009337 }
9338}
9339
9340static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9341{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009342 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009343 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009344 left = read_sconst(state, ins, RHS(ins, 0));
9345 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 mkconst(state, ins, left / right);
9347 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009348 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009349 mkconst(state, ins, 0);
9350 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009351 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009352 error(state, ins, "division by zero");
9353 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009354 else if (is_one(RHS(ins, 1))) {
9355 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009356 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009357 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009358 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009359 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009360 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009361 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009362 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009364 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009365 }
9366}
9367
9368static void simplify_udiv(struct compile_state *state, struct triple *ins)
9369{
Eric Biederman90089602004-05-28 14:11:54 +00009370 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009371 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009372 left = read_const(state, ins, RHS(ins, 0));
9373 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009374 mkconst(state, ins, left / right);
9375 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009376 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 mkconst(state, ins, 0);
9378 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009379 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009380 error(state, ins, "division by zero");
9381 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009382 else if (is_one(RHS(ins, 1))) {
9383 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009384 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009385 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009386 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009387 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009388 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009389 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009390 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009391 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009392 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009393 }
9394}
9395
9396static void simplify_smod(struct compile_state *state, struct triple *ins)
9397{
Eric Biederman90089602004-05-28 14:11:54 +00009398 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009399 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009400 left = read_const(state, ins, RHS(ins, 0));
9401 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 mkconst(state, ins, left % right);
9403 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009404 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009405 mkconst(state, ins, 0);
9406 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009407 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009408 error(state, ins, "division by zero");
9409 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009410 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009411 mkconst(state, ins, 0);
9412 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009413 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009415 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009416 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009417 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009418 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009419 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009420 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009421 }
9422}
Eric Biederman83b991a2003-10-11 06:20:25 +00009423
Eric Biedermanb138ac82003-04-22 18:44:01 +00009424static void simplify_umod(struct compile_state *state, struct triple *ins)
9425{
Eric Biederman90089602004-05-28 14:11:54 +00009426 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009428 left = read_const(state, ins, RHS(ins, 0));
9429 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 mkconst(state, ins, left % right);
9431 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009432 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 mkconst(state, ins, 0);
9434 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009435 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009436 error(state, ins, "division by zero");
9437 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009438 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009439 mkconst(state, ins, 0);
9440 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009441 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009443 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009444 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009445 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009448 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009449 }
9450}
9451
9452static void simplify_add(struct compile_state *state, struct triple *ins)
9453{
9454 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009455 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009456 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009457 tmp = RHS(ins, 0);
9458 RHS(ins, 0) = RHS(ins, 1);
9459 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009460 }
Eric Biederman90089602004-05-28 14:11:54 +00009461 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009462 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009463 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009464 left = read_const(state, ins, RHS(ins, 0));
9465 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009466 mkconst(state, ins, left + right);
9467 }
Eric Biederman530b5192003-07-01 10:05:30 +00009468 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009469 struct triple *sdecl;
9470 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009471 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009472 left = RHS(ins, 0)->u.cval;
9473 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009474 mkaddr_const(state, ins, sdecl, left + right);
9475 }
Eric Biederman530b5192003-07-01 10:05:30 +00009476 else {
9477 internal_warning(state, ins, "Optimize me!");
9478 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009479 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009480 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009481 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009482 tmp = RHS(ins, 1);
9483 RHS(ins, 1) = RHS(ins, 0);
9484 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009485 }
9486}
9487
9488static void simplify_sub(struct compile_state *state, struct triple *ins)
9489{
Eric Biederman90089602004-05-28 14:11:54 +00009490 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009491 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009492 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009493 left = read_const(state, ins, RHS(ins, 0));
9494 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009495 mkconst(state, ins, left - right);
9496 }
Eric Biederman530b5192003-07-01 10:05:30 +00009497 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009498 struct triple *sdecl;
9499 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009500 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009501 left = RHS(ins, 0)->u.cval;
9502 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009503 mkaddr_const(state, ins, sdecl, left - right);
9504 }
Eric Biederman530b5192003-07-01 10:05:30 +00009505 else {
9506 internal_warning(state, ins, "Optimize me!");
9507 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009508 }
9509}
9510
9511static void simplify_sl(struct compile_state *state, struct triple *ins)
9512{
Eric Biederman90089602004-05-28 14:11:54 +00009513 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009514 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009515 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009516 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009517 warning(state, ins, "left shift count >= width of type");
9518 }
9519 }
Eric Biederman90089602004-05-28 14:11:54 +00009520 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009521 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009522 left = read_const(state, ins, RHS(ins, 0));
9523 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009524 mkconst(state, ins, left << right);
9525 }
9526}
9527
9528static void simplify_usr(struct compile_state *state, struct triple *ins)
9529{
Eric Biederman90089602004-05-28 14:11:54 +00009530 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009531 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009532 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009533 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009534 warning(state, ins, "right shift count >= width of type");
9535 }
9536 }
Eric Biederman90089602004-05-28 14:11:54 +00009537 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009538 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009539 left = read_const(state, ins, RHS(ins, 0));
9540 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009541 mkconst(state, ins, left >> right);
9542 }
9543}
9544
9545static void simplify_ssr(struct compile_state *state, struct triple *ins)
9546{
Eric Biederman90089602004-05-28 14:11:54 +00009547 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009548 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009549 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009550 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009551 warning(state, ins, "right shift count >= width of type");
9552 }
9553 }
Eric Biederman90089602004-05-28 14:11:54 +00009554 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009555 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009556 left = read_sconst(state, ins, RHS(ins, 0));
9557 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009558 mkconst(state, ins, left >> right);
9559 }
9560}
9561
9562static void simplify_and(struct compile_state *state, struct triple *ins)
9563{
Eric Biederman90089602004-05-28 14:11:54 +00009564 struct triple *left, *right;
9565 left = RHS(ins, 0);
9566 right = RHS(ins, 1);
9567
9568 if (is_simple_const(left) && is_simple_const(right)) {
9569 ulong_t lval, rval;
9570 lval = read_const(state, ins, left);
9571 rval = read_const(state, ins, right);
9572 mkconst(state, ins, lval & rval);
9573 }
9574 else if (is_zero(right) || is_zero(left)) {
9575 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009576 }
9577}
9578
9579static void simplify_or(struct compile_state *state, struct triple *ins)
9580{
Eric Biederman90089602004-05-28 14:11:54 +00009581 struct triple *left, *right;
9582 left = RHS(ins, 0);
9583 right = RHS(ins, 1);
9584
9585 if (is_simple_const(left) && is_simple_const(right)) {
9586 ulong_t lval, rval;
9587 lval = read_const(state, ins, left);
9588 rval = read_const(state, ins, right);
9589 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009590 }
Eric Biederman90089602004-05-28 14:11:54 +00009591#if 0 /* I need to handle type mismatches here... */
9592 else if (is_zero(right)) {
9593 mkcopy(state, ins, left);
9594 }
9595 else if (is_zero(left)) {
9596 mkcopy(state, ins, right);
9597 }
9598#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009599}
9600
9601static void simplify_xor(struct compile_state *state, struct triple *ins)
9602{
Eric Biederman90089602004-05-28 14:11:54 +00009603 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009604 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009605 left = read_const(state, ins, RHS(ins, 0));
9606 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009607 mkconst(state, ins, left ^ right);
9608 }
9609}
9610
9611static void simplify_pos(struct compile_state *state, struct triple *ins)
9612{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009613 if (is_const(RHS(ins, 0))) {
9614 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009615 }
9616 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009617 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009618 }
9619}
9620
9621static void simplify_neg(struct compile_state *state, struct triple *ins)
9622{
Eric Biederman90089602004-05-28 14:11:54 +00009623 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009624 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009625 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009626 mkconst(state, ins, -left);
9627 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009628 else if (RHS(ins, 0)->op == OP_NEG) {
9629 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009630 }
9631}
9632
9633static void simplify_invert(struct compile_state *state, struct triple *ins)
9634{
Eric Biederman90089602004-05-28 14:11:54 +00009635 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009636 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009637 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009638 mkconst(state, ins, ~left);
9639 }
9640}
9641
9642static void simplify_eq(struct compile_state *state, struct triple *ins)
9643{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009644 struct triple *left, *right;
9645 left = RHS(ins, 0);
9646 right = RHS(ins, 1);
9647
9648 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009649 int val;
9650 val = const_eq(state, ins, left, right);
9651 if (val >= 0) {
9652 mkconst(state, ins, val == 1);
9653 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009654 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009655 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009656 mkconst(state, ins, 1);
9657 }
9658}
9659
9660static void simplify_noteq(struct compile_state *state, struct triple *ins)
9661{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009662 struct triple *left, *right;
9663 left = RHS(ins, 0);
9664 right = RHS(ins, 1);
9665
9666 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009667 int val;
9668 val = const_eq(state, ins, left, right);
9669 if (val >= 0) {
9670 mkconst(state, ins, val != 1);
9671 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009672 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009673 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009674 mkconst(state, ins, 0);
9675 }
9676}
9677
9678static void simplify_sless(struct compile_state *state, struct triple *ins)
9679{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009680 struct triple *left, *right;
9681 left = RHS(ins, 0);
9682 right = RHS(ins, 1);
9683
9684 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009685 int val;
9686 val = const_scmp(state, ins, left, right);
9687 if ((val >= -1) && (val <= 1)) {
9688 mkconst(state, ins, val < 0);
9689 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009690 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009691 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009692 mkconst(state, ins, 0);
9693 }
9694}
9695
9696static void simplify_uless(struct compile_state *state, struct triple *ins)
9697{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009698 struct triple *left, *right;
9699 left = RHS(ins, 0);
9700 right = RHS(ins, 1);
9701
9702 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009703 int val;
9704 val = const_ucmp(state, ins, left, right);
9705 if ((val >= -1) && (val <= 1)) {
9706 mkconst(state, ins, val < 0);
9707 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009708 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009709 else if (is_zero(right)) {
9710 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009711 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009712 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009713 mkconst(state, ins, 0);
9714 }
9715}
9716
9717static void simplify_smore(struct compile_state *state, struct triple *ins)
9718{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009719 struct triple *left, *right;
9720 left = RHS(ins, 0);
9721 right = RHS(ins, 1);
9722
9723 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009724 int val;
9725 val = const_scmp(state, ins, left, right);
9726 if ((val >= -1) && (val <= 1)) {
9727 mkconst(state, ins, val > 0);
9728 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009729 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009730 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009731 mkconst(state, ins, 0);
9732 }
9733}
9734
9735static void simplify_umore(struct compile_state *state, struct triple *ins)
9736{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009737 struct triple *left, *right;
9738 left = RHS(ins, 0);
9739 right = RHS(ins, 1);
9740
9741 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009742 int val;
9743 val = const_ucmp(state, ins, left, right);
9744 if ((val >= -1) && (val <= 1)) {
9745 mkconst(state, ins, val > 0);
9746 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009747 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009748 else if (is_zero(left)) {
9749 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009750 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009751 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009752 mkconst(state, ins, 0);
9753 }
9754}
9755
9756
9757static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9758{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009759 struct triple *left, *right;
9760 left = RHS(ins, 0);
9761 right = RHS(ins, 1);
9762
9763 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009764 int val;
9765 val = const_scmp(state, ins, left, right);
9766 if ((val >= -1) && (val <= 1)) {
9767 mkconst(state, ins, val <= 0);
9768 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009769 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009770 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009771 mkconst(state, ins, 1);
9772 }
9773}
9774
9775static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9776{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009777 struct triple *left, *right;
9778 left = RHS(ins, 0);
9779 right = RHS(ins, 1);
9780
9781 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009782 int val;
9783 val = const_ucmp(state, ins, left, right);
9784 if ((val >= -1) && (val <= 1)) {
9785 mkconst(state, ins, val <= 0);
9786 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009787 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009788 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009789 mkconst(state, ins, 1);
9790 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009791 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009792 mkconst(state, ins, 1);
9793 }
9794}
9795
9796static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9797{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009798 struct triple *left, *right;
9799 left = RHS(ins, 0);
9800 right = RHS(ins, 1);
9801
9802 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009803 int val;
9804 val = const_scmp(state, ins, left, right);
9805 if ((val >= -1) && (val <= 1)) {
9806 mkconst(state, ins, val >= 0);
9807 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009808 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009809 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009810 mkconst(state, ins, 1);
9811 }
9812}
9813
9814static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9815{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009816 struct triple *left, *right;
9817 left = RHS(ins, 0);
9818 right = RHS(ins, 1);
9819
9820 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009821 int val;
9822 val = const_ucmp(state, ins, left, right);
9823 if ((val >= -1) && (val <= 1)) {
9824 mkconst(state, ins, val >= 0);
9825 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009826 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009827 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009828 mkconst(state, ins, 1);
9829 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009830 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009831 mkconst(state, ins, 1);
9832 }
9833}
9834
9835static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9836{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009837 struct triple *rhs;
9838 rhs = RHS(ins, 0);
9839
9840 if (is_const(rhs)) {
9841 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009842 }
9843 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009844 else if ((rhs->use) &&
9845 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009846 int need_copy = 1;
9847 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009848 switch(rhs->op) {
9849 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9850 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9851 case OP_EQ: rhs->op = OP_NOTEQ; break;
9852 case OP_NOTEQ: rhs->op = OP_EQ; break;
9853 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9854 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9855 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9856 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9857 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9858 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9859 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9860 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009861 default:
9862 need_copy = 0;
9863 break;
9864 }
9865 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009866 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009867 }
9868 }
9869}
9870
9871static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9872{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009873 struct triple *rhs;
9874 rhs = RHS(ins, 0);
9875
9876 if (is_const(rhs)) {
9877 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009878 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009879 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009880 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9881 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9882 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009883 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009884 }
9885
9886}
9887
Eric Biederman90089602004-05-28 14:11:54 +00009888static void simplify_load(struct compile_state *state, struct triple *ins)
9889{
9890 struct triple *addr, *sdecl, *blob;
9891
9892 /* If I am doing a load with a constant pointer from a constant
9893 * table get the value.
9894 */
9895 addr = RHS(ins, 0);
9896 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9897 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9898 (blob->op == OP_BLOBCONST)) {
9899 unsigned char buffer[SIZEOF_WORD];
9900 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009901 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009902 ulong_t val;
9903 reg_size = reg_size_of(state, ins->type);
9904 if (reg_size > REG_SIZEOF_REG) {
9905 internal_error(state, ins, "load size greater than register");
9906 }
9907 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009908 end = blob->u.blob;
9909 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009910 src = blob->u.blob;
9911 src += addr->u.cval;
9912
Eric Biederman7dea9552004-06-29 05:38:37 +00009913 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009914 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009915 }
9916
Eric Biederman90089602004-05-28 14:11:54 +00009917 memset(buffer, 0, sizeof(buffer));
9918 memcpy(buffer, src, bits_to_bytes(mem_size));
9919
9920 switch(mem_size) {
9921 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9922 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9923 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9924 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9925 default:
9926 internal_error(state, ins, "mem_size: %d not handled",
9927 mem_size);
9928 val = 0;
9929 break;
9930 }
9931 mkconst(state, ins, val);
9932 }
9933}
9934
9935static void simplify_uextract(struct compile_state *state, struct triple *ins)
9936{
9937 if (is_simple_const(RHS(ins, 0))) {
9938 ulong_t val;
9939 ulong_t mask;
9940 val = read_const(state, ins, RHS(ins, 0));
9941 mask = 1;
9942 mask <<= ins->u.bitfield.size;
9943 mask -= 1;
9944 val >>= ins->u.bitfield.offset;
9945 val &= mask;
9946 mkconst(state, ins, val);
9947 }
9948}
9949
9950static void simplify_sextract(struct compile_state *state, struct triple *ins)
9951{
9952 if (is_simple_const(RHS(ins, 0))) {
9953 ulong_t val;
9954 ulong_t mask;
9955 long_t sval;
9956 val = read_const(state, ins, RHS(ins, 0));
9957 mask = 1;
9958 mask <<= ins->u.bitfield.size;
9959 mask -= 1;
9960 val >>= ins->u.bitfield.offset;
9961 val &= mask;
9962 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9963 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009964 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009965 mkconst(state, ins, sval);
9966 }
9967}
9968
9969static void simplify_deposit(struct compile_state *state, struct triple *ins)
9970{
9971 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9972 ulong_t targ, val;
9973 ulong_t mask;
9974 targ = read_const(state, ins, RHS(ins, 0));
9975 val = read_const(state, ins, RHS(ins, 1));
9976 mask = 1;
9977 mask <<= ins->u.bitfield.size;
9978 mask -= 1;
9979 mask <<= ins->u.bitfield.offset;
9980 targ &= ~mask;
9981 val <<= ins->u.bitfield.offset;
9982 val &= mask;
9983 targ |= val;
9984 mkconst(state, ins, targ);
9985 }
9986}
9987
Eric Biedermanb138ac82003-04-22 18:44:01 +00009988static void simplify_copy(struct compile_state *state, struct triple *ins)
9989{
Eric Biederman90089602004-05-28 14:11:54 +00009990 struct triple *right;
9991 right = RHS(ins, 0);
9992 if (is_subset_type(ins->type, right->type)) {
9993 ins->type = right->type;
9994 }
9995 if (equiv_types(ins->type, right->type)) {
9996 ins->op = OP_COPY;/* I don't need to convert if the types match */
9997 } else {
9998 if (ins->op == OP_COPY) {
9999 internal_error(state, ins, "type mismatch on copy");
10000 }
10001 }
10002 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10003 struct triple *sdecl;
10004 ulong_t offset;
10005 sdecl = MISC(right, 0);
10006 offset = right->u.cval;
10007 mkaddr_const(state, ins, sdecl, offset);
10008 }
10009 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10010 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010011 case OP_INTCONST:
10012 {
10013 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010014 left = read_const(state, ins, right);
10015 /* Ensure I have not overflowed the destination. */
10016 if (size_of(state, right->type) > size_of(state, ins->type)) {
10017 ulong_t mask;
10018 mask = 1;
10019 mask <<= size_of(state, ins->type);
10020 mask -= 1;
10021 left &= mask;
10022 }
10023 /* Ensure I am properly sign extended */
10024 if (size_of(state, right->type) < size_of(state, ins->type) &&
10025 is_signed(right->type)) {
10026 long_t val;
10027 int shift;
10028 shift = SIZEOF_LONG - size_of(state, right->type);
10029 val = left;
10030 val <<= shift;
10031 val >>= shift;
10032 left = val;
10033 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010034 mkconst(state, ins, left);
10035 break;
10036 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010037 default:
10038 internal_error(state, ins, "uknown constant");
10039 break;
10040 }
10041 }
10042}
10043
Eric Biederman83b991a2003-10-11 06:20:25 +000010044static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010045{
10046 struct triple *ptr;
10047 if (!block) {
10048 return 0;
10049 }
10050 ptr = block->first;
10051 do {
10052 if (ptr->op == OP_PHI) {
10053 return 1;
10054 }
10055 ptr = ptr->next;
10056 } while(ptr != block->last);
10057 return 0;
10058}
10059
Eric Biederman83b991a2003-10-11 06:20:25 +000010060static int phi_dependency(struct block *block)
10061{
10062 /* A block has a phi dependency if a phi function
10063 * depends on that block to exist, and makes a block
10064 * that is otherwise useless unsafe to remove.
10065 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010066 if (block) {
10067 struct block_set *edge;
10068 for(edge = block->edges; edge; edge = edge->next) {
10069 if (phi_present(edge->member)) {
10070 return 1;
10071 }
10072 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010073 }
10074 return 0;
10075}
10076
10077static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10078{
10079 struct triple *targ;
10080 targ = TARG(ins, 0);
10081 /* During scc_transform temporary triples are allocated that
10082 * loop back onto themselves. If I see one don't advance the
10083 * target.
10084 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010085 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010086 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010087 targ = targ->next;
10088 }
10089 return targ;
10090}
10091
10092
10093static void simplify_branch(struct compile_state *state, struct triple *ins)
10094{
Eric Biederman90089602004-05-28 14:11:54 +000010095 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010096 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010097 internal_error(state, ins, "not branch");
10098 }
10099 if (ins->use != 0) {
10100 internal_error(state, ins, "branch use");
10101 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010102 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010103 * make modifications to the control flow graph as well
10104 * as to the branch instruction itself. That is handled
10105 * by rebuilding the basic blocks after simplify all is called.
10106 */
10107
10108 /* If we have a branch to an unconditional branch update
10109 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010110 * functions.
10111 * Also only do this a limited number of times so
10112 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010113 */
Eric Biederman90089602004-05-28 14:11:54 +000010114 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010115 do {
10116 struct triple *targ;
10117 simplified = 0;
10118 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010119 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010120 !phi_dependency(targ->u.block))
10121 {
10122 unuse_triple(TARG(ins, 0), ins);
10123 TARG(ins, 0) = TARG(targ, 0);
10124 use_triple(TARG(ins, 0), ins);
10125 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010126 }
Eric Biederman90089602004-05-28 14:11:54 +000010127 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010128
10129 /* If we have a conditional branch with a constant condition
10130 * make it an unconditional branch.
10131 */
Eric Biederman90089602004-05-28 14:11:54 +000010132 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010133 struct triple *targ;
10134 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010135 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010136 unuse_triple(RHS(ins, 0), ins);
10137 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010138 ins->rhs = 0;
10139 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010140 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010141 if (value) {
10142 unuse_triple(ins->next, ins);
10143 TARG(ins, 0) = targ;
10144 }
10145 else {
10146 unuse_triple(targ, ins);
10147 TARG(ins, 0) = ins->next;
10148 }
10149 }
Eric Biederman90089602004-05-28 14:11:54 +000010150
10151 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010152 * make it a noop.
10153 */
10154 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010155 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010156 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010157 unuse_triple(RHS(ins, 0), ins);
10158 unuse_triple(ins->next, ins);
10159 }
Eric Biederman90089602004-05-28 14:11:54 +000010160 ins->lhs = 0;
10161 ins->rhs = 0;
10162 ins->misc = 0;
10163 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010164 ins->op = OP_NOOP;
10165 if (ins->use) {
10166 internal_error(state, ins, "noop use != 0");
10167 }
10168 }
10169}
10170
Eric Biederman530b5192003-07-01 10:05:30 +000010171static void simplify_label(struct compile_state *state, struct triple *ins)
10172{
Eric Biederman83b991a2003-10-11 06:20:25 +000010173 /* Ignore volatile labels */
10174 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010175 return;
10176 }
10177 if (ins->use == 0) {
10178 ins->op = OP_NOOP;
10179 }
10180 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010181 /* In general it is not safe to merge one label that
10182 * imediately follows another. The problem is that the empty
10183 * looking block may have phi functions that depend on it.
10184 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010185 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010186 struct triple_set *user, *next;
10187 ins->op = OP_NOOP;
10188 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010189 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010190 next = user->next;
10191 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010192 expr = triple_targ(state, use, 0);
10193 for(;expr; expr = triple_targ(state, use, expr)) {
10194 if (*expr == ins) {
10195 *expr = ins->prev;
10196 unuse_triple(ins, use);
10197 use_triple(ins->prev, use);
10198 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010199
Eric Biederman530b5192003-07-01 10:05:30 +000010200 }
10201 }
10202 if (ins->use) {
10203 internal_error(state, ins, "noop use != 0");
10204 }
10205 }
10206 }
10207}
10208
Eric Biedermanb138ac82003-04-22 18:44:01 +000010209static void simplify_phi(struct compile_state *state, struct triple *ins)
10210{
Eric Biederman83b991a2003-10-11 06:20:25 +000010211 struct triple **slot;
10212 struct triple *value;
10213 int zrhs, i;
10214 ulong_t cvalue;
10215 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010216 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010217 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010218 return;
10219 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010220 /* See if all of the rhs members of a phi have the same value */
10221 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010222 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010223 for(i = 1; i < zrhs; i++) {
10224 if ( !slot[i] ||
10225 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010226 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010227 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010228 break;
10229 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010230 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010231 if (i == zrhs) {
10232 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010233 return;
10234 }
10235 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010236
Eric Biederman83b991a2003-10-11 06:20:25 +000010237 /* See if all of rhs members of a phi are the same */
10238 value = slot[0];
10239 for(i = 1; i < zrhs; i++) {
10240 if (slot[i] != value) {
10241 break;
10242 }
10243 }
10244 if (i == zrhs) {
10245 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010246 if (!is_subset_type(ins->type, value->type)) {
10247 internal_error(state, ins, "bad input type to phi");
10248 }
10249 /* Make the types match */
10250 if (!equiv_types(ins->type, value->type)) {
10251 ins->type = value->type;
10252 }
10253 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010254 mkcopy(state, ins, value);
10255 return;
10256 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010257}
10258
10259
10260static void simplify_bsf(struct compile_state *state, struct triple *ins)
10261{
Eric Biederman90089602004-05-28 14:11:54 +000010262 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010263 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010264 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010265 mkconst(state, ins, bsf(left));
10266 }
10267}
10268
10269static void simplify_bsr(struct compile_state *state, struct triple *ins)
10270{
Eric Biederman90089602004-05-28 14:11:54 +000010271 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010272 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010273 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010274 mkconst(state, ins, bsr(left));
10275 }
10276}
10277
10278
10279typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010280static const struct simplify_table {
10281 simplify_t func;
10282 unsigned long flag;
10283} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010284#define simplify_sdivt simplify_noop
10285#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010286#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010287
Eric Biederman5ade04a2003-10-22 04:03:46 +000010288[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10289[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10290[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10291[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10292[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10293[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10294[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10295[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10296[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10297[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10298[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10299[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10300[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10301[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10302[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10303[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10304[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10305[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10306[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010307
Eric Biederman5ade04a2003-10-22 04:03:46 +000010308[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10309[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10310[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10311[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10312[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10318[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10319[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010320
Eric Biederman90089602004-05-28 14:11:54 +000010321[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010322[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010323
Eric Biederman90089602004-05-28 14:11:54 +000010324[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10325[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10326[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10327
Eric Biederman5ade04a2003-10-22 04:03:46 +000010328[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010329
Eric Biederman5ade04a2003-10-22 04:03:46 +000010330[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10331[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10332[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010333[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010334
Eric Biederman5ade04a2003-10-22 04:03:46 +000010335[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10336[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10337[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010338[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010339[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10340[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010341
Eric Biederman5ade04a2003-10-22 04:03:46 +000010342[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010343[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010344
Eric Biederman5ade04a2003-10-22 04:03:46 +000010345[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10346[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10347[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10348[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10349[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10350[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10351[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10352[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10353[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010354
Eric Biederman5ade04a2003-10-22 04:03:46 +000010355[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10356[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10357[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10358[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10359[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10360[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10361[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10362[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10363[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010364[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010365[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010366};
10367
Stefan Reinauer14e22772010-04-27 06:56:47 +000010368static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010369 simplify_t do_simplify, struct triple *ins)
10370{
10371#if DEBUG_SIMPLIFY_HIRES
10372 if (state->functions_joined && (do_simplify != simplify_noop)) {
10373 /* High resolution debugging mode */
10374 fprintf(state->dbgout, "simplifing: ");
10375 display_triple(state->dbgout, ins);
10376 }
10377#endif
10378 do_simplify(state, ins);
10379#if DEBUG_SIMPLIFY_HIRES
10380 if (state->functions_joined && (do_simplify != simplify_noop)) {
10381 /* High resolution debugging mode */
10382 fprintf(state->dbgout, "simplified: ");
10383 display_triple(state->dbgout, ins);
10384 }
10385#endif
10386}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010387static void simplify(struct compile_state *state, struct triple *ins)
10388{
10389 int op;
10390 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010391 if (ins == &unknown_triple) {
10392 internal_error(state, ins, "simplifying the unknown triple?");
10393 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010394 do {
10395 op = ins->op;
10396 do_simplify = 0;
Patrick Georgi0dde01c2013-11-11 15:57:09 +010010397 if ((op < 0) || (op >= sizeof(table_simplify)/sizeof(table_simplify[0]))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010398 do_simplify = 0;
10399 }
Eric Biederman90089602004-05-28 14:11:54 +000010400 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010401 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010402 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010403 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010404 !(state->compiler->flags & table_simplify[op].flag)) {
10405 do_simplify = simplify_noop;
10406 }
10407 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010408 do_simplify = simplify_noop;
10409 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010410
Eric Biedermanb138ac82003-04-22 18:44:01 +000010411 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010412 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010413 op, tops(op));
10414 return;
10415 }
Eric Biederman90089602004-05-28 14:11:54 +000010416 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010417 } while(ins->op != op);
10418}
10419
Eric Biederman5ade04a2003-10-22 04:03:46 +000010420static void rebuild_ssa_form(struct compile_state *state);
10421
Eric Biedermanb138ac82003-04-22 18:44:01 +000010422static void simplify_all(struct compile_state *state)
10423{
10424 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010425 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10426 return;
10427 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010428 first = state->first;
10429 ins = first->prev;
10430 do {
10431 simplify(state, ins);
10432 ins = ins->prev;
10433 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010434 ins = first;
10435 do {
10436 simplify(state, ins);
10437 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010438 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010439 rebuild_ssa_form(state);
10440
Eric Biederman90089602004-05-28 14:11:54 +000010441 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010442}
10443
10444/*
10445 * Builtins....
10446 * ============================
10447 */
10448
Eric Biederman0babc1c2003-05-09 02:39:00 +000010449static void register_builtin_function(struct compile_state *state,
10450 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010451{
Eric Biederman90089602004-05-28 14:11:54 +000010452 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010453 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010455 struct file_state file;
10456 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010457 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010458 va_list args;
10459 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010460
10461 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010462 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010463 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010464 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010465 file.report_line = 1;
10466 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010467 file.prev = state->file;
10468 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010469 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010470
10471 /* Find the Parameter count */
10472 valid_op(state, op);
10473 parameters = table_ops[op].rhs;
10474 if (parameters < 0 ) {
10475 internal_error(state, 0, "Invalid builtin parameter count");
10476 }
10477
10478 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010479 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010480 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010481 next = &ftype->right;
10482 va_start(args, rtype);
10483 for(i = 0; i < parameters; i++) {
10484 atype = va_arg(args, struct type *);
10485 if (!*next) {
10486 *next = atype;
10487 } else {
10488 *next = new_type(TYPE_PRODUCT, *next, atype);
10489 next = &((*next)->right);
10490 }
10491 }
10492 if (!*next) {
10493 *next = &void_type;
10494 }
10495 va_end(args);
10496
Eric Biederman90089602004-05-28 14:11:54 +000010497 /* Get the initial closure type */
10498 ctype = new_type(TYPE_JOIN, &void_type, 0);
10499 ctype->elements = 1;
10500
10501 /* Get the return type */
10502 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10503 crtype->elements = 2;
10504
Eric Biedermanb138ac82003-04-22 18:44:01 +000010505 /* Generate the needed triples */
10506 def = triple(state, OP_LIST, ftype, 0, 0);
10507 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010508 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010509 result = flatten(state, first, variable(state, crtype));
10510 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010511 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010512
10513 /* Now string them together */
10514 param = ftype->right;
10515 for(i = 0; i < parameters; i++) {
10516 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10517 atype = param->left;
10518 } else {
10519 atype = param;
10520 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010521 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010522 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010523 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010524 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010525 generate_lhs_pieces(state, work);
10526 for(i = 0; i < parameters; i++) {
10527 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010528 }
Eric Biederman90089602004-05-28 14:11:54 +000010529 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10530 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010531 }
10532 work = flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010533 flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010534 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010535 name_len = strlen(name);
10536 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010537 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010538 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010539
Eric Biedermanb138ac82003-04-22 18:44:01 +000010540 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010541 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010542 state->main_function = 0;
10543
Eric Biederman5ade04a2003-10-22 04:03:46 +000010544 if (!state->functions) {
10545 state->functions = def;
10546 } else {
10547 insert_triple(state, state->functions, def);
10548 }
10549 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010550 FILE *fp = state->dbgout;
10551 fprintf(fp, "\n");
10552 loc(fp, state, 0);
10553 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10554 display_func(state, fp, def);
10555 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010556 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010557}
10558
Eric Biederman0babc1c2003-05-09 02:39:00 +000010559static struct type *partial_struct(struct compile_state *state,
10560 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010561{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010562 struct hash_entry *field_ident;
10563 struct type *result;
10564 int field_name_len;
10565
10566 field_name_len = strlen(field_name);
10567 field_ident = lookup(state, field_name, field_name_len);
10568
10569 result = clone_type(0, type);
10570 result->field_ident = field_ident;
10571
10572 if (rest) {
10573 result = new_type(TYPE_PRODUCT, result, rest);
10574 }
10575 return result;
10576}
10577
10578static struct type *register_builtin_type(struct compile_state *state,
10579 const char *name, struct type *type)
10580{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010581 struct hash_entry *ident;
10582 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010583
Eric Biedermanb138ac82003-04-22 18:44:01 +000010584 name_len = strlen(name);
10585 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010586
Eric Biederman0babc1c2003-05-09 02:39:00 +000010587 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10588 ulong_t elements = 0;
10589 struct type *field;
10590 type = new_type(TYPE_STRUCT, type, 0);
10591 field = type->left;
10592 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10593 elements++;
10594 field = field->right;
10595 }
10596 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010597 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010598 type->type_ident = ident;
10599 type->elements = elements;
10600 }
10601 symbol(state, ident, &ident->sym_ident, 0, type);
10602 ident->tok = TOK_TYPE_NAME;
10603 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010604}
10605
Eric Biederman0babc1c2003-05-09 02:39:00 +000010606
Eric Biedermanb138ac82003-04-22 18:44:01 +000010607static void register_builtins(struct compile_state *state)
10608{
Eric Biederman530b5192003-07-01 10:05:30 +000010609 struct type *div_type, *ldiv_type;
10610 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010611 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010612
Eric Biederman530b5192003-07-01 10:05:30 +000010613 div_type = register_builtin_type(state, "__builtin_div_t",
10614 partial_struct(state, "quot", &int_type,
10615 partial_struct(state, "rem", &int_type, 0)));
10616 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10617 partial_struct(state, "quot", &long_type,
10618 partial_struct(state, "rem", &long_type, 0)));
10619 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10620 partial_struct(state, "quot", &uint_type,
10621 partial_struct(state, "rem", &uint_type, 0)));
10622 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10623 partial_struct(state, "quot", &ulong_type,
10624 partial_struct(state, "rem", &ulong_type, 0)));
10625
10626 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10627 &int_type, &int_type);
10628 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10629 &long_type, &long_type);
10630 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10631 &uint_type, &uint_type);
10632 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10633 &ulong_type, &ulong_type);
10634
Stefan Reinauer14e22772010-04-27 06:56:47 +000010635 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010636 &ushort_type);
10637 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10638 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010639 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010640 &ushort_type);
10641
Stefan Reinauer14e22772010-04-27 06:56:47 +000010642 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010643 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010644 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010645 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010646 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010647 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010648
10649 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010650 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010651 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010652 &int_type);
10653
10654 msr_type = register_builtin_type(state, "__builtin_msr_t",
10655 partial_struct(state, "lo", &ulong_type,
10656 partial_struct(state, "hi", &ulong_type, 0)));
10657
10658 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10659 &ulong_type);
10660 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10661 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010662
10663 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010664 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010665}
10666
10667static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010668 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010669 struct hash_entry **ident, int need_ident);
10670static void decl(struct compile_state *state, struct triple *first);
10671static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010672#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010673static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010674#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010675static struct type *decl_specifiers(struct compile_state *state);
10676static int istype(int tok);
10677static struct triple *expr(struct compile_state *state);
10678static struct triple *assignment_expr(struct compile_state *state);
10679static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010680static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010681
10682static struct triple *call_expr(
10683 struct compile_state *state, struct triple *func)
10684{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010685 struct triple *def;
10686 struct type *param, *type;
10687 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010688
10689 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10690 error(state, 0, "Called object is not a function");
10691 }
10692 if (func->op != OP_LIST) {
10693 internal_error(state, 0, "improper function");
10694 }
10695 eat(state, TOK_LPAREN);
10696 /* Find the return type without any specifiers */
10697 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010698 /* Count the number of rhs entries for OP_FCALL */
10699 param = func->type->right;
10700 pvals = 0;
10701 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10702 pvals++;
10703 param = param->right;
10704 }
10705 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10706 pvals++;
10707 }
10708 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010709 MISC(def, 0) = func;
10710
10711 param = func->type->right;
10712 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010713 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010714 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010715 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010716 arg_type = param;
10717 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10718 arg_type = param->left;
10719 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010720 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010721 RHS(def, index) = val;
10722 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010723 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010724 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010725 }
10726 }
10727 eat(state, TOK_RPAREN);
10728 return def;
10729}
10730
10731
10732static struct triple *character_constant(struct compile_state *state)
10733{
10734 struct triple *def;
10735 struct token *tk;
10736 const signed char *str, *end;
10737 int c;
10738 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010739 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010740 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010741 str_len = tk->str_len - 2;
10742 if (str_len <= 0) {
10743 error(state, 0, "empty character constant");
10744 }
10745 end = str + str_len;
10746 c = char_value(state, &str, end);
10747 if (str != end) {
10748 error(state, 0, "multibyte character constant not supported");
10749 }
10750 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10751 return def;
10752}
10753
10754static struct triple *string_constant(struct compile_state *state)
10755{
10756 struct triple *def;
10757 struct token *tk;
10758 struct type *type;
10759 const signed char *str, *end;
10760 signed char *buf, *ptr;
10761 int str_len;
10762
10763 buf = 0;
10764 type = new_type(TYPE_ARRAY, &char_type, 0);
10765 type->elements = 0;
10766 /* The while loop handles string concatenation */
10767 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010768 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010769 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010770 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010771 if (str_len < 0) {
10772 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010773 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010774 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010775 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010776 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010777 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);
Patrick Georgib6239b82014-08-09 19:42:08 +020010809 if (errno == ERANGE) {
10810 error(state, 0, "Integer constant out of range");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010811 }
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);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011199
11200 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011201 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
Stefan Reinauer14e22772010-04-27 06:56:47 +000011374 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)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011448 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011449 "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));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011463 if (!cond_val || !is_const(cond_val) ||
11464 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011465 {
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));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011480
11481 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011482 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011483 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011484 copy_triple(state, val));
11485 ptr = ptr->next;
11486 }
11487 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011488 set_cv(state, cv, ptr,
11489 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011490 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 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011510
Eric Biederman41203d92004-11-08 09:31:09 +000011511 } 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{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011544 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011545 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.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011556 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011557 * 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);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011569 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011570 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,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011589 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011590 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011591 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,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011596 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011597 break;
11598 case TOK_MINUSEQ:
11599 lvalue(state, left);
11600 eat(state, TOK_MINUSEQ);
11601 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011602 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011603 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,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011625 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011626 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011627 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);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011702 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011703 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 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011834 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011835 (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;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012177 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012178 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;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012207
Eric Biederman8d9c1232003-06-17 08:42:17 +000012208 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012209 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012210 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;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012229 }
Eric Biederman90089602004-05-28 14:11:54 +000012230 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) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012306 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012307 }
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)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012336 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012337 }
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 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012358 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012359 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(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012398 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(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012476 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;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012495 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012496 (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) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012529 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012530 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 }
Stefan Reinauer14e22772010-04-27 06:56:47 +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) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012668 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012669 (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 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012750 else if (ident == state->i_noreturn) {
12751 // attribute((noreturn)) does nothing (yet?)
12752 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012753 else if (ident == state->i_unused) {
12754 // attribute((unused)) does nothing (yet?)
12755 }
Stefan Reinauerec664bc2013-05-09 14:06:04 -070012756 else if (ident == state->i_packed) {
12757 // attribute((packed)) does nothing (yet?)
12758 }
Eric Biederman90089602004-05-28 14:11:54 +000012759 else {
12760 error(state, 0, "Unknown attribute:%s", ident->name);
12761 }
12762 break;
12763 }
12764 default:
12765 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12766 break;
12767 }
12768 return attributes;
12769}
12770
12771static unsigned int attribute_list(struct compile_state *state, unsigned type)
12772{
12773 type = attrib(state, type);
12774 while(peek(state) == TOK_COMMA) {
12775 eat(state, TOK_COMMA);
12776 type = attrib(state, type);
12777 }
12778 return type;
12779}
12780
12781static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12782{
12783 if (peek(state) == TOK_ATTRIBUTE) {
12784 eat(state, TOK_ATTRIBUTE);
12785 eat(state, TOK_LPAREN);
12786 eat(state, TOK_LPAREN);
12787 type = attribute_list(state, type);
12788 eat(state, TOK_RPAREN);
12789 eat(state, TOK_RPAREN);
12790 }
12791 return type;
12792}
12793
Eric Biedermanb138ac82003-04-22 18:44:01 +000012794static unsigned int type_qualifiers(struct compile_state *state)
12795{
12796 unsigned int specifiers;
12797 int done;
12798 done = 0;
12799 specifiers = QUAL_NONE;
12800 do {
12801 switch(peek(state)) {
12802 case TOK_CONST:
12803 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012804 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012805 break;
12806 case TOK_VOLATILE:
12807 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012808 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012809 break;
12810 case TOK_RESTRICT:
12811 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012812 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012813 break;
12814 default:
12815 done = 1;
12816 break;
12817 }
12818 } while(!done);
12819 return specifiers;
12820}
12821
12822static struct type *type_specifier(
12823 struct compile_state *state, unsigned int spec)
12824{
12825 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012826 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012827 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012828 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012829 case TOK_VOID:
12830 eat(state, TOK_VOID);
12831 type = new_type(TYPE_VOID | spec, 0, 0);
12832 break;
12833 case TOK_CHAR:
12834 eat(state, TOK_CHAR);
12835 type = new_type(TYPE_CHAR | spec, 0, 0);
12836 break;
12837 case TOK_SHORT:
12838 eat(state, TOK_SHORT);
12839 if (peek(state) == TOK_INT) {
12840 eat(state, TOK_INT);
12841 }
12842 type = new_type(TYPE_SHORT | spec, 0, 0);
12843 break;
12844 case TOK_INT:
12845 eat(state, TOK_INT);
12846 type = new_type(TYPE_INT | spec, 0, 0);
12847 break;
12848 case TOK_LONG:
12849 eat(state, TOK_LONG);
12850 switch(peek(state)) {
12851 case TOK_LONG:
12852 eat(state, TOK_LONG);
12853 error(state, 0, "long long not supported");
12854 break;
12855 case TOK_DOUBLE:
12856 eat(state, TOK_DOUBLE);
12857 error(state, 0, "long double not supported");
12858 break;
12859 case TOK_INT:
12860 eat(state, TOK_INT);
12861 type = new_type(TYPE_LONG | spec, 0, 0);
12862 break;
12863 default:
12864 type = new_type(TYPE_LONG | spec, 0, 0);
12865 break;
12866 }
12867 break;
12868 case TOK_FLOAT:
12869 eat(state, TOK_FLOAT);
12870 error(state, 0, "type float not supported");
12871 break;
12872 case TOK_DOUBLE:
12873 eat(state, TOK_DOUBLE);
12874 error(state, 0, "type double not supported");
12875 break;
12876 case TOK_SIGNED:
12877 eat(state, TOK_SIGNED);
12878 switch(peek(state)) {
12879 case TOK_LONG:
12880 eat(state, TOK_LONG);
12881 switch(peek(state)) {
12882 case TOK_LONG:
12883 eat(state, TOK_LONG);
12884 error(state, 0, "type long long not supported");
12885 break;
12886 case TOK_INT:
12887 eat(state, TOK_INT);
12888 type = new_type(TYPE_LONG | spec, 0, 0);
12889 break;
12890 default:
12891 type = new_type(TYPE_LONG | spec, 0, 0);
12892 break;
12893 }
12894 break;
12895 case TOK_INT:
12896 eat(state, TOK_INT);
12897 type = new_type(TYPE_INT | spec, 0, 0);
12898 break;
12899 case TOK_SHORT:
12900 eat(state, TOK_SHORT);
12901 type = new_type(TYPE_SHORT | spec, 0, 0);
12902 break;
12903 case TOK_CHAR:
12904 eat(state, TOK_CHAR);
12905 type = new_type(TYPE_CHAR | spec, 0, 0);
12906 break;
12907 default:
12908 type = new_type(TYPE_INT | spec, 0, 0);
12909 break;
12910 }
12911 break;
12912 case TOK_UNSIGNED:
12913 eat(state, TOK_UNSIGNED);
12914 switch(peek(state)) {
12915 case TOK_LONG:
12916 eat(state, TOK_LONG);
12917 switch(peek(state)) {
12918 case TOK_LONG:
12919 eat(state, TOK_LONG);
12920 error(state, 0, "unsigned long long not supported");
12921 break;
12922 case TOK_INT:
12923 eat(state, TOK_INT);
12924 type = new_type(TYPE_ULONG | spec, 0, 0);
12925 break;
12926 default:
12927 type = new_type(TYPE_ULONG | spec, 0, 0);
12928 break;
12929 }
12930 break;
12931 case TOK_INT:
12932 eat(state, TOK_INT);
12933 type = new_type(TYPE_UINT | spec, 0, 0);
12934 break;
12935 case TOK_SHORT:
12936 eat(state, TOK_SHORT);
12937 type = new_type(TYPE_USHORT | spec, 0, 0);
12938 break;
12939 case TOK_CHAR:
12940 eat(state, TOK_CHAR);
12941 type = new_type(TYPE_UCHAR | spec, 0, 0);
12942 break;
12943 default:
12944 type = new_type(TYPE_UINT | spec, 0, 0);
12945 break;
12946 }
12947 break;
12948 /* struct or union specifier */
12949 case TOK_STRUCT:
12950 case TOK_UNION:
12951 type = struct_or_union_specifier(state, spec);
12952 break;
12953 /* enum-spefifier */
12954 case TOK_ENUM:
12955 type = enum_specifier(state, spec);
12956 break;
12957 /* typedef name */
12958 case TOK_TYPE_NAME:
12959 type = typedef_name(state, spec);
12960 break;
12961 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012962 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012963 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012964 break;
12965 }
12966 return type;
12967}
12968
12969static int istype(int tok)
12970{
12971 switch(tok) {
12972 case TOK_CONST:
12973 case TOK_RESTRICT:
12974 case TOK_VOLATILE:
12975 case TOK_VOID:
12976 case TOK_CHAR:
12977 case TOK_SHORT:
12978 case TOK_INT:
12979 case TOK_LONG:
12980 case TOK_FLOAT:
12981 case TOK_DOUBLE:
12982 case TOK_SIGNED:
12983 case TOK_UNSIGNED:
12984 case TOK_STRUCT:
12985 case TOK_UNION:
12986 case TOK_ENUM:
12987 case TOK_TYPE_NAME:
12988 return 1;
12989 default:
12990 return 0;
12991 }
12992}
12993
12994
12995static struct type *specifier_qualifier_list(struct compile_state *state)
12996{
12997 struct type *type;
12998 unsigned int specifiers = 0;
12999
13000 /* type qualifiers */
13001 specifiers |= type_qualifiers(state);
13002
13003 /* type specifier */
13004 type = type_specifier(state, specifiers);
13005
13006 return type;
13007}
13008
Stefan Reinauer50542a82007-10-24 11:14:14 +000013009#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013010static int isdecl_specifier(int tok)
13011{
13012 switch(tok) {
13013 /* storage class specifier */
13014 case TOK_AUTO:
13015 case TOK_REGISTER:
13016 case TOK_STATIC:
13017 case TOK_EXTERN:
13018 case TOK_TYPEDEF:
13019 /* type qualifier */
13020 case TOK_CONST:
13021 case TOK_RESTRICT:
13022 case TOK_VOLATILE:
13023 /* type specifiers */
13024 case TOK_VOID:
13025 case TOK_CHAR:
13026 case TOK_SHORT:
13027 case TOK_INT:
13028 case TOK_LONG:
13029 case TOK_FLOAT:
13030 case TOK_DOUBLE:
13031 case TOK_SIGNED:
13032 case TOK_UNSIGNED:
13033 /* struct or union specifier */
13034 case TOK_STRUCT:
13035 case TOK_UNION:
13036 /* enum-spefifier */
13037 case TOK_ENUM:
13038 /* typedef name */
13039 case TOK_TYPE_NAME:
13040 /* function specifiers */
13041 case TOK_INLINE:
13042 return 1;
13043 default:
13044 return 0;
13045 }
13046}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013047#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013048
13049static struct type *decl_specifiers(struct compile_state *state)
13050{
13051 struct type *type;
13052 unsigned int specifiers;
13053 /* I am overly restrictive in the arragement of specifiers supported.
13054 * C is overly flexible in this department it makes interpreting
13055 * the parse tree difficult.
13056 */
13057 specifiers = 0;
13058
13059 /* storage class specifier */
13060 specifiers |= storage_class_specifier_opt(state);
13061
13062 /* function-specifier */
13063 specifiers |= function_specifier_opt(state);
13064
Eric Biederman90089602004-05-28 14:11:54 +000013065 /* attributes */
13066 specifiers |= attributes_opt(state, 0);
13067
Eric Biedermanb138ac82003-04-22 18:44:01 +000013068 /* type qualifier */
13069 specifiers |= type_qualifiers(state);
13070
13071 /* type specifier */
13072 type = type_specifier(state, specifiers);
13073 return type;
13074}
13075
Eric Biederman00443072003-06-24 12:34:45 +000013076struct field_info {
13077 struct type *type;
13078 size_t offset;
13079};
13080
13081static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013082{
13083 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013084 struct field_info info;
13085 info.offset = ~0U;
13086 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013087 do {
13088 switch(peek(state)) {
13089 case TOK_LBRACKET:
13090 {
13091 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013092 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13093 error(state, 0, "Array designator not in array initializer");
13094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013095 eat(state, TOK_LBRACKET);
13096 value = constant_expr(state);
13097 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013098
13099 info.type = type->left;
13100 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013101 break;
13102 }
13103 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013104 {
13105 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013106 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13107 ((type->type & TYPE_MASK) != TYPE_UNION))
13108 {
Eric Biederman00443072003-06-24 12:34:45 +000013109 error(state, 0, "Struct designator not in struct initializer");
13110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013111 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013112 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013113 info.offset = field_offset(state, type, field);
13114 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115 break;
Eric Biederman00443072003-06-24 12:34:45 +000013116 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013117 default:
13118 error(state, 0, "Invalid designator");
13119 }
13120 tok = peek(state);
13121 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13122 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013123 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013124}
13125
13126static struct triple *initializer(
13127 struct compile_state *state, struct type *type)
13128{
13129 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013130#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013131#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013132#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013133 if (peek(state) != TOK_LBRACE) {
13134 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013135 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13136 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13137 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13138 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13139 (equiv_types(type->left, result->type->left))) {
13140 type->elements = result->type->elements;
13141 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013142 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013143 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13144 (type->type & TYPE_MASK) != TYPE_ARRAY)
13145 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013146 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013147 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013148 if (!is_init_compatible(state, type, result->type)) {
13149 error(state, 0, "Incompatible types in initializer");
13150 }
13151 if (!equiv_types(type, result->type)) {
13152 result = mk_cast_expr(state, type, result);
13153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013154 }
13155 else {
13156 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013157 size_t max_offset;
13158 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013159 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013160 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13161 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13162 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013163 }
Eric Biederman00443072003-06-24 12:34:45 +000013164 info.offset = 0;
13165 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013166 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13167 info.type = next_field(state, type, 0);
13168 }
Eric Biederman00443072003-06-24 12:34:45 +000013169 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13170 max_offset = 0;
13171 } else {
13172 max_offset = size_of(state, type);
13173 }
Eric Biederman90089602004-05-28 14:11:54 +000013174 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013175 eat(state, TOK_LBRACE);
13176 do {
13177 struct triple *value;
13178 struct type *value_type;
13179 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013180 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013181 int tok;
13182 comma = 0;
13183 tok = peek(state);
13184 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013185 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013186 }
Eric Biederman00443072003-06-24 12:34:45 +000013187 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13188 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013189 error(state, 0, "element beyond bounds");
13190 }
Eric Biederman00443072003-06-24 12:34:45 +000013191 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013192 value = eval_const_expr(state, initializer(state, value_type));
13193 value_size = size_of(state, value_type);
13194 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013195 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13196 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013197 void *old_buf;
13198 size_t old_size;
13199 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013200 old_size = max_offset;
13201 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013202 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13203 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013204 xfree(old_buf);
13205 }
Eric Biederman90089602004-05-28 14:11:54 +000013206 dest = ((char *)buf) + bits_to_bytes(info.offset);
13207#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013208 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013209 dest - buf,
13210 bits_to_bytes(max_offset),
13211 bits_to_bytes(value_size),
13212 value->op);
13213#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013214 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013215 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
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_I8)) {
13218#if DEBUG_INITIALIZER
13219 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13220#endif
Eric Biederman00443072003-06-24 12:34:45 +000013221 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013222 }
Eric Biederman90089602004-05-28 14:11:54 +000013223 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013224 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013225 }
Eric Biederman90089602004-05-28 14:11:54 +000013226 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013227 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013228 }
13229 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013230 internal_error(state, 0, "unhandled constant initializer");
13231 }
Eric Biederman00443072003-06-24 12:34:45 +000013232 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013233 if (peek(state) == TOK_COMMA) {
13234 eat(state, TOK_COMMA);
13235 comma = 1;
13236 }
Eric Biederman00443072003-06-24 12:34:45 +000013237 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013238 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13239 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013240 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013241 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013242 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013243 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013244 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13245 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13246 type->elements = max_offset / size_of(state, type->left);
13247 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013248 eat(state, TOK_RBRACE);
13249 result = triple(state, OP_BLOBCONST, type, 0, 0);
13250 result->u.blob = buf;
13251 }
13252 return result;
13253}
13254
Eric Biederman90089602004-05-28 14:11:54 +000013255static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013256{
13257 /* Make a second pass and finish anything outstanding
13258 * with respect to branches. The only outstanding item
13259 * is to see if there are goto to labels that have not
13260 * been defined and to error about them.
13261 */
13262 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013263 struct triple *ins;
13264 /* Also error on branches that do not use their targets */
13265 ins = first;
13266 do {
13267 if (!triple_is_ret(state, ins)) {
13268 struct triple **expr ;
13269 struct triple_set *set;
13270 expr = triple_targ(state, ins, 0);
13271 for(; expr; expr = triple_targ(state, ins, expr)) {
13272 struct triple *targ;
13273 targ = *expr;
13274 for(set = targ?targ->use:0; set; set = set->next) {
13275 if (set->member == ins) {
13276 break;
13277 }
13278 }
13279 if (!set) {
13280 internal_error(state, ins, "targ not used");
13281 }
13282 }
13283 }
13284 ins = ins->next;
13285 } while(ins != first);
13286 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013287 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13288 struct hash_entry *entry;
13289 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13290 struct triple *ins;
13291 if (!entry->sym_label) {
13292 continue;
13293 }
13294 ins = entry->sym_label->def;
13295 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13296 error(state, ins, "label `%s' used but not defined",
13297 entry->name);
13298 }
13299 }
13300 }
13301}
13302
Eric Biedermanb138ac82003-04-22 18:44:01 +000013303static struct triple *function_definition(
13304 struct compile_state *state, struct type *type)
13305{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013306 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013307 struct triple *fname;
13308 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013309 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013310 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013311 int i;
13312 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13313 error(state, 0, "Invalid function header");
13314 }
13315
13316 /* Verify the function type */
13317 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13318 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013319 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013320 error(state, 0, "Invalid function parameters");
13321 }
13322 param = type->right;
13323 i = 0;
13324 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13325 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013326 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013327 error(state, 0, "No identifier for parameter %d\n", i);
13328 }
13329 param = param->right;
13330 }
13331 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013332 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013333 error(state, 0, "No identifier for paramter %d\n", i);
13334 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013335
Eric Biedermanb138ac82003-04-22 18:44:01 +000013336 /* Get a list of statements for this function. */
13337 def = triple(state, OP_LIST, type, 0, 0);
13338
13339 /* Start a new scope for the passed parameters */
13340 start_scope(state);
13341
13342 /* Put a label at the very start of a function */
13343 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013344 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013345
13346 /* Put a label at the very end of a function */
13347 end = label(state);
13348 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013349 /* Remember where return goes */
13350 ident = state->i_return;
13351 symbol(state, ident, &ident->sym_ident, end, end->type);
13352
Eric Biederman90089602004-05-28 14:11:54 +000013353 /* Get the initial closure type */
13354 ctype = new_type(TYPE_JOIN, &void_type, 0);
13355 ctype->elements = 1;
13356
13357 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013358 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013359 /* Remove all type qualifiers from the return type */
13360 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13361 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013362 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013363
Eric Biederman5ade04a2003-10-22 04:03:46 +000013364 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013365 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013366
13367 /* Add in the return instruction */
13368 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13369 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013370
13371 /* Walk through the parameters and create symbol table entries
13372 * for them.
13373 */
13374 param = type->right;
13375 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013376 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013377 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013378 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013379 flatten(state, end, tmp);
13380 param = param->right;
13381 }
13382 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13383 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013384 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013385 tmp = variable(state, param);
13386 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13387 flatten(state, end, tmp);
13388 }
Eric Biederman90089602004-05-28 14:11:54 +000013389
13390 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013391 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013392 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13393 fname_type->type |= QUAL_CONST | STOR_STATIC;
13394 fname_type->elements = strlen(state->function) + 1;
13395
13396 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13397 fname->u.blob = (void *)state->function;
13398 fname = flatten(state, end, fname);
13399
13400 ident = state->i___func__;
13401 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013402
13403 /* Remember which function I am compiling.
13404 * Also assume the last defined function is the main function.
13405 */
13406 state->main_function = def;
13407
13408 /* Now get the actual function definition */
13409 compound_statement(state, end);
13410
Eric Biederman153ea352003-06-20 14:43:20 +000013411 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013412 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013413
Eric Biedermanb138ac82003-04-22 18:44:01 +000013414 /* Remove the parameter scope */
13415 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013416
Eric Biederman5ade04a2003-10-22 04:03:46 +000013417
13418 /* Remember I have defined a function */
13419 if (!state->functions) {
13420 state->functions = def;
13421 } else {
13422 insert_triple(state, state->functions, def);
13423 }
13424 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013425 FILE *fp = state->dbgout;
13426 fprintf(fp, "\n");
13427 loc(fp, state, 0);
13428 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13429 display_func(state, fp, def);
13430 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013431 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013432
13433 return def;
13434}
13435
Stefan Reinauer14e22772010-04-27 06:56:47 +000013436static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013437 struct type *type, struct hash_entry *ident)
13438{
13439 struct triple *def;
13440 def = 0;
13441 /* Clean up the storage types used */
13442 switch (type->type & STOR_MASK) {
13443 case STOR_AUTO:
13444 case STOR_STATIC:
13445 /* These are the good types I am aiming for */
13446 break;
13447 case STOR_REGISTER:
13448 type->type &= ~STOR_MASK;
13449 type->type |= STOR_AUTO;
13450 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013451 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013452 case STOR_EXTERN:
13453 type->type &= ~STOR_MASK;
13454 type->type |= STOR_STATIC;
13455 break;
13456 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013457 if (!ident) {
13458 error(state, 0, "typedef without name");
13459 }
13460 symbol(state, ident, &ident->sym_ident, 0, type);
13461 ident->tok = TOK_TYPE_NAME;
13462 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013463 break;
13464 default:
13465 internal_error(state, 0, "Undefined storage class");
13466 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013467 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
Patrick Georgi11a7db32013-02-16 20:16:34 +010013468 // ignore function prototypes
13469 return def;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013470 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013471 if (ident &&
13472 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13473 ((type->type & STOR_MASK) != STOR_STATIC))
13474 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013475 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013476 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013477 ((type->type & QUAL_CONST) == 0)) {
13478 error(state, 0, "non const static variables not supported");
13479 }
13480 if (ident) {
13481 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013482 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013483 }
13484 return def;
13485}
13486
13487static void decl(struct compile_state *state, struct triple *first)
13488{
13489 struct type *base_type, *type;
13490 struct hash_entry *ident;
13491 struct triple *def;
13492 int global;
13493 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13494 base_type = decl_specifiers(state);
13495 ident = 0;
13496 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013497 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013498 if (global && ident && (peek(state) == TOK_LBRACE)) {
13499 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013500 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013501 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013502 def = function_definition(state, type);
13503 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013504 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013505 }
13506 else {
13507 int done;
13508 flatten(state, first, do_decl(state, type, ident));
13509 /* type or variable definition */
13510 do {
13511 done = 1;
13512 if (peek(state) == TOK_EQ) {
13513 if (!ident) {
13514 error(state, 0, "cannot assign to a type");
13515 }
13516 eat(state, TOK_EQ);
13517 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013518 init_expr(state,
13519 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013520 initializer(state, type)));
13521 }
13522 arrays_complete(state, type);
13523 if (peek(state) == TOK_COMMA) {
13524 eat(state, TOK_COMMA);
13525 ident = 0;
13526 type = declarator(state, base_type, &ident, 0);
13527 flatten(state, first, do_decl(state, type, ident));
13528 done = 0;
13529 }
13530 } while(!done);
13531 eat(state, TOK_SEMI);
13532 }
13533}
13534
13535static void decls(struct compile_state *state)
13536{
13537 struct triple *list;
13538 int tok;
13539 list = label(state);
13540 while(1) {
13541 tok = peek(state);
13542 if (tok == TOK_EOF) {
13543 return;
13544 }
13545 if (tok == TOK_SPACE) {
13546 eat(state, TOK_SPACE);
13547 }
13548 decl(state, list);
13549 if (list->next != list) {
13550 error(state, 0, "global variables not supported");
13551 }
13552 }
13553}
13554
Stefan Reinauer14e22772010-04-27 06:56:47 +000013555/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013556 * Function inlining
13557 */
Eric Biederman90089602004-05-28 14:11:54 +000013558struct triple_reg_set {
13559 struct triple_reg_set *next;
13560 struct triple *member;
13561 struct triple *new;
13562};
13563struct reg_block {
13564 struct block *block;
13565 struct triple_reg_set *in;
13566 struct triple_reg_set *out;
13567 int vertex;
13568};
13569static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13570static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13571static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13572static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13573static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13574 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13575 void *arg);
13576static void print_block(
13577 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013578static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013579 struct triple *member, struct triple *new_member);
13580static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13581static struct reg_block *compute_variable_lifetimes(
13582 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013583static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013584 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013585#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013586static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013587 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013588#endif
Eric Biederman90089602004-05-28 14:11:54 +000013589
Eric Biederman5ade04a2003-10-22 04:03:46 +000013590
13591static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013592 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013593 struct triple *targ, struct triple *ret)
13594{
13595 struct triple *call;
13596
13597 if (!retvar || !is_lvalue(state, retvar)) {
13598 internal_error(state, 0, "writing to a non lvalue?");
13599 }
13600 write_compatible(state, retvar->type, &void_ptr_type);
13601
13602 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13603 TARG(call, 0) = targ;
13604 MISC(call, 0) = ret;
13605 if (!targ || (targ->op != OP_LABEL)) {
13606 internal_error(state, 0, "call not to a label");
13607 }
13608 if (!ret || (ret->op != OP_RET)) {
13609 internal_error(state, 0, "call not matched with return");
13610 }
13611 return call;
13612}
13613
Eric Biederman5ade04a2003-10-22 04:03:46 +000013614static void walk_functions(struct compile_state *state,
13615 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13616 void *arg)
13617{
13618 struct triple *func, *first;
13619 func = first = state->functions;
13620 do {
13621 cb(state, func, arg);
13622 func = func->next;
13623 } while(func != first);
13624}
13625
Eric Biederman90089602004-05-28 14:11:54 +000013626static void reverse_walk_functions(struct compile_state *state,
13627 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13628 void *arg)
13629{
13630 struct triple *func, *first;
13631 func = first = state->functions;
13632 do {
13633 func = func->prev;
13634 cb(state, func, arg);
13635 } while(func != first);
13636}
13637
13638
13639static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13640{
13641 struct triple *ptr, *first;
13642 if (func->u.cval == 0) {
13643 return;
13644 }
13645 ptr = first = RHS(func, 0);
13646 do {
13647 if (ptr->op == OP_FCALL) {
13648 struct triple *called_func;
13649 called_func = MISC(ptr, 0);
13650 /* Mark the called function as used */
13651 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13652 called_func->u.cval++;
13653 }
13654 /* Remove the called function from the list */
13655 called_func->prev->next = called_func->next;
13656 called_func->next->prev = called_func->prev;
13657
13658 /* Place the called function before me on the list */
13659 called_func->next = func;
13660 called_func->prev = func->prev;
13661 called_func->prev->next = called_func;
13662 called_func->next->prev = called_func;
13663 }
13664 ptr = ptr->next;
13665 } while(ptr != first);
13666 func->id |= TRIPLE_FLAG_FLATTENED;
13667}
13668
13669static void mark_live_functions(struct compile_state *state)
13670{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013671 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013672 * the list of functions.
13673 */
13674 if ((state->main_function->next != state->functions) ||
13675 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013676 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013677 "state->main_function is not at the end of the function list ");
13678 }
13679 state->main_function->u.cval = 1;
13680 reverse_walk_functions(state, mark_live, 0);
13681}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013682
Stefan Reinauer14e22772010-04-27 06:56:47 +000013683static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013684 struct triple *func, struct triple *ins)
13685{
13686 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13687#if 0
13688 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013689 FILE *fp = state->errout;
13690 fprintf(fp, "global: ");
13691 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013692 }
13693#endif
13694 return local;
13695}
13696
Stefan Reinauer14e22772010-04-27 06:56:47 +000013697struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013698 struct occurance *base_occurance)
13699{
13700 struct triple *nfunc;
13701 struct triple *nfirst, *ofirst;
13702 struct triple *new, *old;
13703
13704 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013705 FILE *fp = state->dbgout;
13706 fprintf(fp, "\n");
13707 loc(fp, state, 0);
13708 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13709 display_func(state, fp, ofunc);
13710 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013711 }
13712
13713 /* Make a new copy of the old function */
13714 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13715 nfirst = 0;
13716 ofirst = old = RHS(ofunc, 0);
13717 do {
13718 struct triple *new;
13719 struct occurance *occurance;
13720 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013721 old_lhs = old->lhs;
13722 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013723 occurance = inline_occurance(state, base_occurance, old->occurance);
13724 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13725 MISC(old, 0)->u.cval += 1;
13726 }
13727 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13728 occurance);
13729 if (!triple_stores_block(state, new)) {
13730 memcpy(&new->u, &old->u, sizeof(new->u));
13731 }
13732 if (!nfirst) {
13733 RHS(nfunc, 0) = nfirst = new;
13734 }
13735 else {
13736 insert_triple(state, nfirst, new);
13737 }
13738 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013739 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013740
Eric Biederman5ade04a2003-10-22 04:03:46 +000013741 /* During the copy remember new as user of old */
13742 use_triple(old, new);
13743
Eric Biederman5ade04a2003-10-22 04:03:46 +000013744 /* Remember which instructions are local */
13745 old->id |= TRIPLE_FLAG_LOCAL;
13746 old = old->next;
13747 } while(old != ofirst);
13748
13749 /* Make a second pass to fix up any unresolved references */
13750 old = ofirst;
13751 new = nfirst;
13752 do {
13753 struct triple **oexpr, **nexpr;
13754 int count, i;
13755 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013756 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013757 for(i = 0; i < count; i++) {
13758 oexpr = &old->param[i];
13759 nexpr = &new->param[i];
13760 if (*oexpr && !*nexpr) {
13761 if (!local_triple(state, ofunc, *oexpr)) {
13762 *nexpr = *oexpr;
13763 }
13764 else if ((*oexpr)->use) {
13765 *nexpr = (*oexpr)->use->member;
13766 }
13767 if (*nexpr == old) {
13768 internal_error(state, 0, "new == old?");
13769 }
13770 use_triple(*nexpr, new);
13771 }
13772 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013773 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013774 }
13775 }
13776 old = old->next;
13777 new = new->next;
13778 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013779
Eric Biederman5ade04a2003-10-22 04:03:46 +000013780 /* Make a third pass to cleanup the extra useses */
13781 old = ofirst;
13782 new = nfirst;
13783 do {
13784 unuse_triple(old, new);
13785 /* Forget which instructions are local */
13786 old->id &= ~TRIPLE_FLAG_LOCAL;
13787 old = old->next;
13788 new = new->next;
13789 } while ((old != ofirst) && (new != nfirst));
13790 return nfunc;
13791}
13792
Eric Biederman90089602004-05-28 14:11:54 +000013793static void expand_inline_call(
13794 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013795{
13796 /* Inline the function call */
13797 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013798 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013799 struct triple *end, *nend;
13800 int pvals, i;
13801
13802 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013803 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013804 if (ofunc->op != OP_LIST) {
13805 internal_error(state, 0, "improper function");
13806 }
Eric Biederman90089602004-05-28 14:11:54 +000013807 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013808 /* Prepend the parameter reading into the new function list */
13809 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013810 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013811 for(i = 0; i < pvals; i++) {
13812 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013813 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013814 atype = ptype;
13815 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13816 atype = ptype->left;
13817 }
Eric Biederman90089602004-05-28 14:11:54 +000013818 param = farg(state, nfunc, i);
13819 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13820 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013821 }
Eric Biederman90089602004-05-28 14:11:54 +000013822 arg = RHS(fcall, i);
13823 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013824 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013825 }
13826 result = 0;
13827 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013828 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013829 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013830 }
13831 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013832 FILE *fp = state->dbgout;
13833 fprintf(fp, "\n");
13834 loc(fp, state, 0);
13835 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13836 display_func(state, fp, nfunc);
13837 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013838 }
13839
Stefan Reinauer14e22772010-04-27 06:56:47 +000013840 /*
13841 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013842 */
13843 /* Remove the read of the return address */
13844 ins = RHS(nfunc, 0)->prev->prev;
13845 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13846 internal_error(state, ins, "Not return addres read?");
13847 }
13848 release_triple(state, ins);
13849 /* Remove the return instruction */
13850 ins = RHS(nfunc, 0)->prev;
13851 if (ins->op != OP_RET) {
13852 internal_error(state, ins, "Not return?");
13853 }
13854 release_triple(state, ins);
13855 /* Remove the retaddres variable */
13856 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013857 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013858 (retvar->op != OP_ADECL) ||
13859 (retvar->next->op != OP_PIECE) ||
13860 (MISC(retvar->next, 0) != retvar)) {
13861 internal_error(state, retvar, "Not the return address?");
13862 }
13863 release_triple(state, retvar->next);
13864 release_triple(state, retvar);
13865
13866 /* Remove the label at the start of the function */
13867 ins = RHS(nfunc, 0);
13868 if (ins->op != OP_LABEL) {
13869 internal_error(state, ins, "Not label?");
13870 }
13871 nfirst = ins->next;
13872 free_triple(state, ins);
13873 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013874 RHS(nfunc, 0) = 0;
13875 free_triple(state, nfunc);
13876
13877 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013878 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013879 nend = nfirst->prev;
13880 end->next = nfirst;
13881 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013882 nend->next = fcall;
13883 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013884
Eric Biederman90089602004-05-28 14:11:54 +000013885 /* Now the result reading code */
13886 if (result) {
13887 result = flatten(state, fcall, result);
13888 propogate_use(state, fcall, result);
13889 }
13890
13891 /* Release the original fcall instruction */
13892 release_triple(state, fcall);
13893
13894 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013895}
13896
Eric Biederman90089602004-05-28 14:11:54 +000013897/*
13898 *
13899 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013900 *
Eric Biederman90089602004-05-28 14:11:54 +000013901 * result
13902 * |
13903 * +----------+------------+
13904 * | |
13905 * union of closures result_type
13906 * |
13907 * +------------------+---------------+
13908 * | |
13909 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013910 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013911 * +----+--+-+--------+-----+ +----+----+---+-----+
13912 * | | | | | | | | |
13913 * var1 var2 var3 ... varN result var1 var2 ... varN result
13914 * |
13915 * +--------+---------+
13916 * | |
13917 * union of closures result_type
13918 * |
13919 * +-----+-------------------+
13920 * | |
13921 * closure1 ... closureN
13922 * | |
13923 * +-----+---+----+----+ +----+---+----+-----+
13924 * | | | | | | | |
13925 * var1 var2 ... varN result var1 var2 ... varN result
13926 */
13927
Stefan Reinauer14e22772010-04-27 06:56:47 +000013928static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013929 struct triple *func, struct type *closure_type)
13930{
13931 struct type *type, *ctype, **next;
13932 struct triple *var, *new_var;
13933 int i;
13934
13935#if 0
13936 FILE *fp = state->errout;
13937 fprintf(fp, "original_type: ");
13938 name_of(fp, fresult(state, func)->type);
13939 fprintf(fp, "\n");
13940#endif
13941 /* find the original type */
13942 var = fresult(state, func);
13943 type = var->type;
13944 if (type->elements != 2) {
13945 internal_error(state, var, "bad return type");
13946 }
13947
13948 /* Find the complete closure type and update it */
13949 ctype = type->left->left;
13950 next = &ctype->left;
13951 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13952 next = &(*next)->right;
13953 }
13954 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13955 ctype->elements += 1;
13956
13957#if 0
13958 fprintf(fp, "new_type: ");
13959 name_of(fp, type);
13960 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013961 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013962 ctype, ctype->elements, reg_size_of(state, ctype));
13963 name_of(fp, ctype);
13964 fprintf(fp, "\n");
13965#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013966
Eric Biederman90089602004-05-28 14:11:54 +000013967 /* Regenerate the variable with the new type definition */
13968 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13969 new_var->id |= TRIPLE_FLAG_FLATTENED;
13970 for(i = 0; i < new_var->lhs; i++) {
13971 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13972 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013973
Eric Biederman90089602004-05-28 14:11:54 +000013974 /* Point everyone at the new variable */
13975 propogate_use(state, var, new_var);
13976
13977 /* Release the original variable */
13978 for(i = 0; i < var->lhs; i++) {
13979 release_triple(state, LHS(var, i));
13980 }
13981 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013982
Eric Biederman90089602004-05-28 14:11:54 +000013983 /* Return the index of the added closure type */
13984 return ctype->elements - 1;
13985}
13986
13987static struct triple *closure_expr(struct compile_state *state,
13988 struct triple *func, int closure_idx, int var_idx)
13989{
13990 return deref_index(state,
13991 deref_index(state,
13992 deref_index(state, fresult(state, func), 0),
13993 closure_idx),
13994 var_idx);
13995}
13996
13997
13998static void insert_triple_set(
13999 struct triple_reg_set **head, struct triple *member)
14000{
14001 struct triple_reg_set *new;
14002 new = xcmalloc(sizeof(*new), "triple_set");
14003 new->member = member;
14004 new->new = 0;
14005 new->next = *head;
14006 *head = new;
14007}
14008
14009static int ordered_triple_set(
14010 struct triple_reg_set **head, struct triple *member)
14011{
14012 struct triple_reg_set **ptr;
14013 if (!member)
14014 return 0;
14015 ptr = head;
14016 while(*ptr) {
14017 if (member == (*ptr)->member) {
14018 return 0;
14019 }
14020 /* keep the list ordered */
14021 if (member->id < (*ptr)->member->id) {
14022 break;
14023 }
14024 ptr = &(*ptr)->next;
14025 }
14026 insert_triple_set(ptr, member);
14027 return 1;
14028}
14029
14030
14031static void free_closure_variables(struct compile_state *state,
14032 struct triple_reg_set **enclose)
14033{
14034 struct triple_reg_set *entry, *next;
14035 for(entry = *enclose; entry; entry = next) {
14036 next = entry->next;
14037 do_triple_unset(enclose, entry->member);
14038 }
14039}
14040
14041static int lookup_closure_index(struct compile_state *state,
14042 struct triple *me, struct triple *val)
14043{
14044 struct triple *first, *ins, *next;
14045 first = RHS(me, 0);
14046 ins = next = first;
14047 do {
14048 struct triple *result;
14049 struct triple *index0, *index1, *index2, *read, *write;
14050 ins = next;
14051 next = ins->next;
14052 if (ins->op != OP_CALL) {
14053 continue;
14054 }
14055 /* I am at a previous call point examine it closely */
14056 if (ins->next->op != OP_LABEL) {
14057 internal_error(state, ins, "call not followed by label");
14058 }
14059 /* Does this call does not enclose any variables? */
14060 if ((ins->next->next->op != OP_INDEX) ||
14061 (ins->next->next->u.cval != 0) ||
14062 (result = MISC(ins->next->next, 0)) ||
14063 (result->id & TRIPLE_FLAG_LOCAL)) {
14064 continue;
14065 }
14066 index0 = ins->next->next;
14067 /* The pattern is:
14068 * 0 index result < 0 >
14069 * 1 index 0 < ? >
14070 * 2 index 1 < ? >
14071 * 3 read 2
14072 * 4 write 3 var
14073 */
14074 for(index0 = ins->next->next;
14075 (index0->op == OP_INDEX) &&
14076 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014077 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014078 index0 = write->next)
14079 {
14080 index1 = index0->next;
14081 index2 = index1->next;
14082 read = index2->next;
14083 write = read->next;
14084 if ((index0->op != OP_INDEX) ||
14085 (index1->op != OP_INDEX) ||
14086 (index2->op != OP_INDEX) ||
14087 (read->op != OP_READ) ||
14088 (write->op != OP_WRITE) ||
14089 (MISC(index1, 0) != index0) ||
14090 (MISC(index2, 0) != index1) ||
14091 (RHS(read, 0) != index2) ||
14092 (RHS(write, 0) != read)) {
14093 internal_error(state, index0, "bad var read");
14094 }
14095 if (MISC(write, 0) == val) {
14096 return index2->u.cval;
14097 }
14098 }
14099 } while(next != first);
14100 return -1;
14101}
14102
14103static inline int enclose_triple(struct triple *ins)
14104{
14105 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14106}
14107
14108static void compute_closure_variables(struct compile_state *state,
14109 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14110{
14111 struct triple_reg_set *set, *vars, **last_var;
14112 struct basic_blocks bb;
14113 struct reg_block *rb;
14114 struct block *block;
14115 struct triple *old_result, *first, *ins;
14116 size_t count, idx;
14117 unsigned long used_indicies;
14118 int i, max_index;
14119#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14120#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014121 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014122 unsigned id;
14123 int index;
14124 } *info;
14125
Stefan Reinauer14e22772010-04-27 06:56:47 +000014126
Eric Biederman90089602004-05-28 14:11:54 +000014127 /* Find the basic blocks of this function */
14128 bb.func = me;
14129 bb.first = RHS(me, 0);
14130 old_result = 0;
14131 if (!triple_is_ret(state, bb.first->prev)) {
14132 bb.func = 0;
14133 } else {
14134 old_result = fresult(state, me);
14135 }
14136 analyze_basic_blocks(state, &bb);
14137
14138 /* Find which variables are currently alive in a given block */
14139 rb = compute_variable_lifetimes(state, &bb);
14140
14141 /* Find the variables that are currently alive */
14142 block = block_of_triple(state, fcall);
14143 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14144 internal_error(state, fcall, "No reg block? block: %p", block);
14145 }
14146
14147#if DEBUG_EXPLICIT_CLOSURES
14148 print_live_variables(state, &bb, rb, state->dbgout);
14149 fflush(state->dbgout);
14150#endif
14151
14152 /* Count the number of triples in the function */
14153 first = RHS(me, 0);
14154 ins = first;
14155 count = 0;
14156 do {
14157 count++;
14158 ins = ins->next;
14159 } while(ins != first);
14160
14161 /* Allocate some memory to temorary hold the id info */
14162 info = xcmalloc(sizeof(*info) * (count +1), "info");
14163
14164 /* Mark the local function */
14165 first = RHS(me, 0);
14166 ins = first;
14167 idx = 1;
14168 do {
14169 info[idx].id = ins->id;
14170 ins->id = TRIPLE_FLAG_LOCAL | idx;
14171 idx++;
14172 ins = ins->next;
14173 } while(ins != first);
14174
Stefan Reinauer14e22772010-04-27 06:56:47 +000014175 /*
Eric Biederman90089602004-05-28 14:11:54 +000014176 * Build the list of variables to enclose.
14177 *
14178 * A target it to put the same variable in the
14179 * same slot for ever call of a given function.
14180 * After coloring this removes all of the variable
14181 * manipulation code.
14182 *
14183 * The list of variables to enclose is built ordered
14184 * program order because except in corner cases this
14185 * gives me the stability of assignment I need.
14186 *
14187 * To gurantee that stability I lookup the variables
14188 * to see where they have been used before and
14189 * I build my final list with the assigned indicies.
14190 */
14191 vars = 0;
14192 if (enclose_triple(old_result)) {
14193 ordered_triple_set(&vars, old_result);
14194 }
14195 for(set = rb[block->vertex].out; set; set = set->next) {
14196 if (!enclose_triple(set->member)) {
14197 continue;
14198 }
14199 if ((set->member == fcall) || (set->member == old_result)) {
14200 continue;
14201 }
14202 if (!local_triple(state, me, set->member)) {
14203 internal_error(state, set->member, "not local?");
14204 }
14205 ordered_triple_set(&vars, set->member);
14206 }
14207
14208 /* Lookup the current indicies of the live varialbe */
14209 used_indicies = 0;
14210 max_index = -1;
14211 for(set = vars; set ; set = set->next) {
14212 struct triple *ins;
14213 int index;
14214 ins = set->member;
14215 index = lookup_closure_index(state, me, ins);
14216 info[ID_BITS(ins->id)].index = index;
14217 if (index < 0) {
14218 continue;
14219 }
14220 if (index >= MAX_INDICIES) {
14221 internal_error(state, ins, "index unexpectedly large");
14222 }
14223 if (used_indicies & (1 << index)) {
14224 internal_error(state, ins, "index previously used?");
14225 }
14226 /* Remember which indicies have been used */
14227 used_indicies |= (1 << index);
14228 if (index > max_index) {
14229 max_index = index;
14230 }
14231 }
14232
14233 /* Walk through the live variables and make certain
14234 * everything is assigned an index.
14235 */
14236 for(set = vars; set; set = set->next) {
14237 struct triple *ins;
14238 int index;
14239 ins = set->member;
14240 index = info[ID_BITS(ins->id)].index;
14241 if (index >= 0) {
14242 continue;
14243 }
14244 /* Find the lowest unused index value */
14245 for(index = 0; index < MAX_INDICIES; index++) {
14246 if (!(used_indicies & (1 << index))) {
14247 break;
14248 }
14249 }
14250 if (index == MAX_INDICIES) {
14251 internal_error(state, ins, "no free indicies?");
14252 }
14253 info[ID_BITS(ins->id)].index = index;
14254 /* Remember which indicies have been used */
14255 used_indicies |= (1 << index);
14256 if (index > max_index) {
14257 max_index = index;
14258 }
14259 }
14260
14261 /* Build the return list of variables with positions matching
14262 * their indicies.
14263 */
14264 *enclose = 0;
14265 last_var = enclose;
14266 for(i = 0; i <= max_index; i++) {
14267 struct triple *var;
14268 var = 0;
14269 if (used_indicies & (1 << i)) {
14270 for(set = vars; set; set = set->next) {
14271 int index;
14272 index = info[ID_BITS(set->member->id)].index;
14273 if (index == i) {
14274 var = set->member;
14275 break;
14276 }
14277 }
14278 if (!var) {
14279 internal_error(state, me, "missing variable");
14280 }
14281 }
14282 insert_triple_set(last_var, var);
14283 last_var = &(*last_var)->next;
14284 }
14285
14286#if DEBUG_EXPLICIT_CLOSURES
14287 /* Print out the variables to be enclosed */
14288 loc(state->dbgout, state, fcall);
14289 fprintf(state->dbgout, "Alive: \n");
14290 for(set = *enclose; set; set = set->next) {
14291 display_triple(state->dbgout, set->member);
14292 }
14293 fflush(state->dbgout);
14294#endif
14295
14296 /* Clear the marks */
14297 ins = first;
14298 do {
14299 ins->id = info[ID_BITS(ins->id)].id;
14300 ins = ins->next;
14301 } while(ins != first);
14302
14303 /* Release the ordered list of live variables */
14304 free_closure_variables(state, &vars);
14305
14306 /* Release the storage of the old ids */
14307 xfree(info);
14308
14309 /* Release the variable lifetime information */
14310 free_variable_lifetimes(state, &bb, rb);
14311
14312 /* Release the basic blocks of this function */
14313 free_basic_blocks(state, &bb);
14314}
14315
14316static void expand_function_call(
14317 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014318{
14319 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014320 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014321 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014322 struct triple *first;
14323 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014324 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014325 struct triple_reg_set *enclose, *set;
14326 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014327
Eric Biederman90089602004-05-28 14:11:54 +000014328#if DEBUG_EXPLICIT_CLOSURES
14329 FILE *fp = state->dbgout;
14330 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14331 display_func(state, fp, MISC(fcall, 0));
14332 display_func(state, fp, me);
14333 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14334#endif
14335
Eric Biederman5ade04a2003-10-22 04:03:46 +000014336 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014337 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014338 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014339 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014340 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014341 first = fcall->next;
14342
14343 /* Find what I need to enclose */
14344 compute_closure_variables(state, me, fcall, &enclose);
14345
14346 /* Compute the closure type */
14347 closure_type = new_type(TYPE_TUPLE, 0, 0);
14348 closure_type->elements = 0;
14349 closure_next = &closure_type->left;
14350 for(set = enclose; set ; set = set->next) {
14351 struct type *type;
14352 type = &void_type;
14353 if (set->member) {
14354 type = set->member->type;
14355 }
14356 if (!*closure_next) {
14357 *closure_next = type;
14358 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014359 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014360 type);
14361 closure_next = &(*closure_next)->right;
14362 }
14363 closure_type->elements += 1;
14364 }
14365 if (closure_type->elements == 0) {
14366 closure_type->type = TYPE_VOID;
14367 }
14368
14369
14370#if DEBUG_EXPLICIT_CLOSURES
14371 fprintf(state->dbgout, "closure type: ");
14372 name_of(state->dbgout, closure_type);
14373 fprintf(state->dbgout, "\n");
14374#endif
14375
14376 /* Update the called functions closure variable */
14377 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014378
14379 /* Generate some needed triples */
14380 ret_loc = label(state);
14381 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14382
14383 /* Pass the parameters to the new function */
14384 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014385 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014386 for(i = 0; i < pvals; i++) {
14387 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014388 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014389 atype = ptype;
14390 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14391 atype = ptype->left;
14392 }
Eric Biederman90089602004-05-28 14:11:54 +000014393 param = farg(state, func, i);
14394 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14395 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014396 }
Eric Biederman90089602004-05-28 14:11:54 +000014397 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014398 flatten(state, first, write_expr(state, param, arg));
14399 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014400 }
Eric Biederman90089602004-05-28 14:11:54 +000014401 rtype = func->type->left;
14402
Eric Biederman5ade04a2003-10-22 04:03:46 +000014403 /* Thread the triples together */
14404 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014405
14406 /* Save the active variables in the result variable */
14407 for(i = 0, set = enclose; set ; set = set->next, i++) {
14408 if (!set->member) {
14409 continue;
14410 }
14411 flatten(state, ret_loc,
14412 write_expr(state,
14413 closure_expr(state, func, closure_idx, i),
14414 read_expr(state, set->member)));
14415 }
14416
14417 /* Initialize the return value */
14418 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014419 flatten(state, ret_loc,
14420 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014421 deref_index(state, fresult(state, func), 1),
14422 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14423 }
14424
Eric Biederman5ade04a2003-10-22 04:03:46 +000014425 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014426 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14427 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014428 call(state, retvar, ret_addr, func_first, func_last));
14429
Eric Biederman7dea9552004-06-29 05:38:37 +000014430 /* Find the result */
14431 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14432 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014433 result = flatten(state, first,
14434 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014435 deref_index(state, fresult(state, func), 1)));
14436
14437 propogate_use(state, fcall, result);
14438 }
14439
14440 /* Release the original fcall instruction */
14441 release_triple(state, fcall);
14442
Eric Biederman90089602004-05-28 14:11:54 +000014443 /* Restore the active variables from the result variable */
14444 for(i = 0, set = enclose; set ; set = set->next, i++) {
14445 struct triple_set *use, *next;
14446 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014447 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014448 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014449 continue;
14450 }
Eric Biederman90089602004-05-28 14:11:54 +000014451 /* Generate an expression for the value */
14452 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014453 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014454 closure_expr(state, func, closure_idx, i)));
14455
14456
14457 /* If the original is an lvalue restore the preserved value */
14458 if (is_lvalue(state, set->member)) {
14459 flatten(state, first,
14460 write_expr(state, set->member, new));
14461 continue;
14462 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014463 /*
14464 * If the original is a value update the dominated uses.
14465 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014466
Eric Biederman7dea9552004-06-29 05:38:37 +000014467 /* Analyze the basic blocks so I can see who dominates whom */
14468 bb.func = me;
14469 bb.first = RHS(me, 0);
14470 if (!triple_is_ret(state, bb.first->prev)) {
14471 bb.func = 0;
14472 }
14473 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014474
Eric Biederman90089602004-05-28 14:11:54 +000014475
14476#if DEBUG_EXPLICIT_CLOSURES
14477 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14478 set->member, new);
14479#endif
14480 /* If fcall dominates the use update the expression */
14481 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014482 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014483 * removes use, so I must take a copy of the
14484 * next entry early.
14485 */
14486 next = use->next;
14487 if (!tdominates(state, fcall, use->member)) {
14488 continue;
14489 }
14490 replace_use(state, set->member, new, use->member);
14491 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014492
14493 /* Release the basic blocks, the instructions will be
14494 * different next time, and flatten/insert_triple does
14495 * not update the block values so I can't cache the analysis.
14496 */
14497 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014498 }
14499
Eric Biederman90089602004-05-28 14:11:54 +000014500 /* Release the closure variable list */
14501 free_closure_variables(state, &enclose);
14502
14503 if (state->compiler->debug & DEBUG_INLINE) {
14504 FILE *fp = state->dbgout;
14505 fprintf(fp, "\n");
14506 loc(fp, state, 0);
14507 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14508 display_func(state, fp, func);
14509 display_func(state, fp, me);
14510 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14511 }
14512
14513 return;
14514}
14515
14516static int do_inline(struct compile_state *state, struct triple *func)
14517{
14518 int do_inline;
14519 int policy;
14520
14521 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14522 switch(policy) {
14523 case COMPILER_INLINE_ALWAYS:
14524 do_inline = 1;
14525 if (func->type->type & ATTRIB_NOINLINE) {
14526 error(state, func, "noinline with always_inline compiler option");
14527 }
14528 break;
14529 case COMPILER_INLINE_NEVER:
14530 do_inline = 0;
14531 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14532 error(state, func, "always_inline with noinline compiler option");
14533 }
14534 break;
14535 case COMPILER_INLINE_DEFAULTON:
14536 switch(func->type->type & STOR_MASK) {
14537 case STOR_STATIC | STOR_INLINE:
14538 case STOR_LOCAL | STOR_INLINE:
14539 case STOR_EXTERN | STOR_INLINE:
14540 do_inline = 1;
14541 break;
14542 default:
14543 do_inline = 1;
14544 break;
14545 }
14546 break;
14547 case COMPILER_INLINE_DEFAULTOFF:
14548 switch(func->type->type & STOR_MASK) {
14549 case STOR_STATIC | STOR_INLINE:
14550 case STOR_LOCAL | STOR_INLINE:
14551 case STOR_EXTERN | STOR_INLINE:
14552 do_inline = 1;
14553 break;
14554 default:
14555 do_inline = 0;
14556 break;
14557 }
14558 break;
14559 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014560 switch(func->type->type & STOR_MASK) {
14561 case STOR_STATIC | STOR_INLINE:
14562 case STOR_LOCAL | STOR_INLINE:
14563 case STOR_EXTERN | STOR_INLINE:
14564 do_inline = 1;
14565 break;
14566 default:
14567 do_inline = (func->u.cval == 1);
14568 break;
14569 }
Eric Biederman90089602004-05-28 14:11:54 +000014570 break;
14571 default:
14572 do_inline = 0;
14573 internal_error(state, 0, "Unimplemented inline policy");
14574 break;
14575 }
14576 /* Force inlining */
14577 if (func->type->type & ATTRIB_NOINLINE) {
14578 do_inline = 0;
14579 }
14580 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14581 do_inline = 1;
14582 }
14583 return do_inline;
14584}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014585
Eric Biederman90089602004-05-28 14:11:54 +000014586static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14587{
14588 struct triple *first, *ptr, *next;
14589 /* If the function is not used don't bother */
14590 if (me->u.cval <= 0) {
14591 return;
14592 }
14593 if (state->compiler->debug & DEBUG_CALLS2) {
14594 FILE *fp = state->dbgout;
14595 fprintf(fp, "in: %s\n",
14596 me->type->type_ident->name);
14597 }
14598
14599 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014600 ptr = next = first;
14601 do {
Eric Biederman90089602004-05-28 14:11:54 +000014602 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014603 ptr = next;
14604 prev = ptr->prev;
14605 next = ptr->next;
14606 if (ptr->op != OP_FCALL) {
14607 continue;
14608 }
14609 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014610 /* See if the function should be inlined */
14611 if (!do_inline(state, func)) {
14612 /* Put a label after the fcall */
14613 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14614 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014615 }
Eric Biederman90089602004-05-28 14:11:54 +000014616 if (state->compiler->debug & DEBUG_CALLS) {
14617 FILE *fp = state->dbgout;
14618 if (state->compiler->debug & DEBUG_CALLS2) {
14619 loc(fp, state, ptr);
14620 }
14621 fprintf(fp, "inlining %s\n",
14622 func->type->type_ident->name);
14623 fflush(fp);
14624 }
14625
14626 /* Update the function use counts */
14627 func->u.cval -= 1;
14628
14629 /* Replace the fcall with the called function */
14630 expand_inline_call(state, me, ptr);
14631
14632 next = prev->next;
14633 } while (next != first);
14634
14635 ptr = next = first;
14636 do {
14637 struct triple *prev, *func;
14638 ptr = next;
14639 prev = ptr->prev;
14640 next = ptr->next;
14641 if (ptr->op != OP_FCALL) {
14642 continue;
14643 }
14644 func = MISC(ptr, 0);
14645 if (state->compiler->debug & DEBUG_CALLS) {
14646 FILE *fp = state->dbgout;
14647 if (state->compiler->debug & DEBUG_CALLS2) {
14648 loc(fp, state, ptr);
14649 }
14650 fprintf(fp, "calling %s\n",
14651 func->type->type_ident->name);
14652 fflush(fp);
14653 }
14654 /* Replace the fcall with the instruction sequence
14655 * needed to make the call.
14656 */
14657 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014658 next = prev->next;
14659 } while(next != first);
14660}
Eric Biederman90089602004-05-28 14:11:54 +000014661
14662static void inline_functions(struct compile_state *state, struct triple *func)
14663{
14664 inline_function(state, func, 0);
14665 reverse_walk_functions(state, inline_function, 0);
14666}
14667
Eric Biederman5ade04a2003-10-22 04:03:46 +000014668static void insert_function(struct compile_state *state,
14669 struct triple *func, void *arg)
14670{
14671 struct triple *first, *end, *ffirst, *fend;
14672
14673 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014674 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014675 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014676 func->type->type_ident->name, func->u.cval);
14677 }
14678 if (func->u.cval == 0) {
14679 return;
14680 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014681
14682 /* Find the end points of the lists */
14683 first = arg;
14684 end = first->prev;
14685 ffirst = RHS(func, 0);
14686 fend = ffirst->prev;
14687
14688 /* splice the lists together */
14689 end->next = ffirst;
14690 ffirst->prev = end;
14691 fend->next = first;
14692 first->prev = fend;
14693}
14694
Eric Biederman90089602004-05-28 14:11:54 +000014695struct triple *input_asm(struct compile_state *state)
14696{
14697 struct asm_info *info;
14698 struct triple *def;
14699 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014700
Eric Biederman90089602004-05-28 14:11:54 +000014701 info = xcmalloc(sizeof(*info), "asm_info");
14702 info->str = "";
14703
14704 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14705 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14706
14707 def = new_triple(state, OP_ASM, &void_type, out, 0);
14708 def->u.ainfo = info;
14709 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014710
Eric Biederman90089602004-05-28 14:11:54 +000014711 for(i = 0; i < out; i++) {
14712 struct triple *piece;
14713 piece = triple(state, OP_PIECE, &int_type, def, 0);
14714 piece->u.cval = i;
14715 LHS(def, i) = piece;
14716 }
14717
14718 return def;
14719}
14720
14721struct triple *output_asm(struct compile_state *state)
14722{
14723 struct asm_info *info;
14724 struct triple *def;
14725 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014726
Eric Biederman90089602004-05-28 14:11:54 +000014727 info = xcmalloc(sizeof(*info), "asm_info");
14728 info->str = "";
14729
14730 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14731 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14732
14733 def = new_triple(state, OP_ASM, &void_type, 0, in);
14734 def->u.ainfo = info;
14735 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014736
Eric Biederman90089602004-05-28 14:11:54 +000014737 return def;
14738}
14739
Eric Biederman5ade04a2003-10-22 04:03:46 +000014740static void join_functions(struct compile_state *state)
14741{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014742 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014743 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014744 struct type *pnext, *param;
14745 struct type *result_type, *args_type;
14746 int idx;
14747
14748 /* Be clear the functions have not been joined yet */
14749 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014750
14751 /* Dummy file state to get debug handing right */
14752 memset(&file, 0, sizeof(file));
14753 file.basename = "";
14754 file.line = 0;
14755 file.report_line = 0;
14756 file.report_name = file.basename;
14757 file.prev = state->file;
14758 state->file = &file;
14759 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014760
Eric Biederman41203d92004-11-08 09:31:09 +000014761 if (!state->main_function) {
14762 error(state, 0, "No functions to compile\n");
14763 }
14764
Eric Biederman90089602004-05-28 14:11:54 +000014765 /* The type of arguments */
14766 args_type = state->main_function->type->right;
14767 /* The return type without any specifiers */
14768 result_type = clone_type(0, state->main_function->type->left);
14769
14770
14771 /* Verify the external arguments */
14772 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014773 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014774 "Too many external input arguments");
14775 }
14776 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014777 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014778 "Too many external output arguments");
14779 }
14780
Eric Biederman5ade04a2003-10-22 04:03:46 +000014781 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014782 end = label(state);
14783 start = label(state);
14784 start = flatten(state, state->first, start);
14785 end = flatten(state, state->first, end);
14786 in = input_asm(state);
14787 out = output_asm(state);
14788 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014789 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014790 in = flatten(state, state->first, in);
14791 call = flatten(state, state->first, call);
14792 out = flatten(state, state->first, out);
14793
14794
14795 /* Read the external input arguments */
14796 pnext = args_type;
14797 idx = 0;
14798 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14799 struct triple *expr;
14800 param = pnext;
14801 pnext = 0;
14802 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14803 pnext = param->right;
14804 param = param->left;
14805 }
14806 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014807 error(state, state->main_function,
14808 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014809 idx + 1, param->field_ident->name);
14810 }
14811 expr = read_expr(state, LHS(in, idx));
14812 RHS(call, idx) = expr;
14813 expr = flatten(state, call, expr);
14814 use_triple(expr, call);
14815
Stefan Reinauer14e22772010-04-27 06:56:47 +000014816 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014817 }
14818
14819
14820 /* Write the external output arguments */
14821 pnext = result_type;
14822 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14823 pnext = result_type->left;
14824 }
14825 for(idx = 0; idx < out->rhs; idx++) {
14826 struct triple *expr;
14827 param = pnext;
14828 pnext = 0;
14829 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14830 pnext = param->right;
14831 param = param->left;
14832 }
14833 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14834 param = 0;
14835 }
14836 if (param) {
14837 if (registers_of(state, param) != 1) {
14838 error(state, state->main_function,
14839 "Result: %d %s requires multiple registers",
14840 idx, param->field_ident->name);
14841 }
14842 expr = read_expr(state, call);
14843 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14844 expr = deref_field(state, expr, param->field_ident);
14845 }
14846 } else {
14847 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14848 }
14849 flatten(state, out, expr);
14850 RHS(out, idx) = expr;
14851 use_triple(expr, out);
14852 }
14853
14854 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014855 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014856 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14857 func->type->type_ident = lookup(state, "", 0);
14858 RHS(func, 0) = state->first;
14859 func->u.cval = 1;
14860
Eric Biederman5ade04a2003-10-22 04:03:46 +000014861 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014862 mark_live_functions(state);
14863 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014864 walk_functions(state, insert_function, end);
14865
14866 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014867 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014868 }
14869
Eric Biederman90089602004-05-28 14:11:54 +000014870 /* OK now the functions have been joined. */
14871 state->functions_joined = 1;
14872
Eric Biederman5ade04a2003-10-22 04:03:46 +000014873 /* Done now cleanup */
14874 state->file = file.prev;
14875 state->function = 0;
14876}
14877
Eric Biedermanb138ac82003-04-22 18:44:01 +000014878/*
14879 * Data structurs for optimation.
14880 */
14881
Eric Biederman5ade04a2003-10-22 04:03:46 +000014882
Eric Biederman83b991a2003-10-11 06:20:25 +000014883static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014884 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014885 int front)
14886{
14887 struct block_set **ptr, *new;
14888 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014889 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014890 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014891 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014892 ptr = head;
14893 while(*ptr) {
14894 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014895 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014896 }
14897 ptr = &(*ptr)->next;
14898 }
14899 new = xcmalloc(sizeof(*new), "block_set");
14900 new->member = user;
14901 if (front) {
14902 new->next = *head;
14903 *head = new;
14904 }
14905 else {
14906 new->next = 0;
14907 *ptr = new;
14908 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014909 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014910}
Eric Biederman83b991a2003-10-11 06:20:25 +000014911static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014912 struct block *used, struct block_set **head, struct block *unuser)
14913{
14914 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014915 int count;
14916 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014917 ptr = head;
14918 while(*ptr) {
14919 use = *ptr;
14920 if (use->member == unuser) {
14921 *ptr = use->next;
14922 memset(use, -1, sizeof(*use));
14923 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014924 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014925 }
14926 else {
14927 ptr = &use->next;
14928 }
14929 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014930 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014931}
14932
14933static void use_block(struct block *used, struct block *user)
14934{
Eric Biederman83b991a2003-10-11 06:20:25 +000014935 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014936 /* Append new to the head of the list, print_block
14937 * depends on this.
14938 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014939 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014940 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014941}
14942static void unuse_block(struct block *used, struct block *unuser)
14943{
Eric Biederman83b991a2003-10-11 06:20:25 +000014944 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014945 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014946 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014947}
14948
Eric Biederman5ade04a2003-10-22 04:03:46 +000014949static void add_block_edge(struct block *block, struct block *edge, int front)
14950{
14951 int count;
14952 count = do_use_block(block, &block->edges, edge, front);
14953 block->edge_count += count;
14954}
14955
14956static void remove_block_edge(struct block *block, struct block *edge)
14957{
14958 int count;
14959 count = do_unuse_block(block, &block->edges, edge);
14960 block->edge_count -= count;
14961}
14962
Eric Biedermanb138ac82003-04-22 18:44:01 +000014963static void idom_block(struct block *idom, struct block *user)
14964{
14965 do_use_block(idom, &idom->idominates, user, 0);
14966}
14967
14968static void unidom_block(struct block *idom, struct block *unuser)
14969{
14970 do_unuse_block(idom, &idom->idominates, unuser);
14971}
14972
14973static void domf_block(struct block *block, struct block *domf)
14974{
14975 do_use_block(block, &block->domfrontier, domf, 0);
14976}
14977
14978static void undomf_block(struct block *block, struct block *undomf)
14979{
14980 do_unuse_block(block, &block->domfrontier, undomf);
14981}
14982
14983static void ipdom_block(struct block *ipdom, struct block *user)
14984{
14985 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14986}
14987
14988static void unipdom_block(struct block *ipdom, struct block *unuser)
14989{
14990 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14991}
14992
14993static void ipdomf_block(struct block *block, struct block *ipdomf)
14994{
14995 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14996}
14997
14998static void unipdomf_block(struct block *block, struct block *unipdomf)
14999{
15000 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15001}
15002
Eric Biederman83b991a2003-10-11 06:20:25 +000015003static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015004 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015005 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15006 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015007{
Eric Biederman83b991a2003-10-11 06:20:25 +000015008 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015009 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015010 ptr = state->first;
15011 do {
Eric Biederman90089602004-05-28 14:11:54 +000015012 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015013 if (ptr->next->prev != ptr) {
15014 internal_error(state, ptr->next, "bad prev");
15015 }
15016 ptr = ptr->next;
15017 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015018 return result;
15019}
15020
Eric Biedermanb138ac82003-04-22 18:44:01 +000015021#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015022static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015023{
Eric Biederman90089602004-05-28 14:11:54 +000015024 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015025 int op;
15026 op = ins->op;
15027 if (op == OP_LIST) {
15028#if !PRINT_LIST
15029 return 0;
15030#endif
15031 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015032 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015033 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015034 }
Eric Biederman90089602004-05-28 14:11:54 +000015035 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015036
Stefan Reinauer14e22772010-04-27 06:56:47 +000015037 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015038 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015039 internal_error(state, ins, "branch used?");
15040 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015041 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015042 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015043 }
15044 return 0;
15045}
15046
Eric Biedermanb138ac82003-04-22 18:44:01 +000015047static void print_triples(struct compile_state *state)
15048{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015049 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015050 FILE *fp = state->dbgout;
15051 fprintf(fp, "--------------- triples ---------------\n");
15052 walk_triples(state, do_print_triple, fp);
15053 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015054 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015055}
15056
15057struct cf_block {
15058 struct block *block;
15059};
15060static void find_cf_blocks(struct cf_block *cf, struct block *block)
15061{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015062 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015063 if (!block || (cf[block->vertex].block == block)) {
15064 return;
15065 }
15066 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015067 for(edge = block->edges; edge; edge = edge->next) {
15068 find_cf_blocks(cf, edge->member);
15069 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015070}
15071
Eric Biederman90089602004-05-28 14:11:54 +000015072static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015073 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015074{
15075 struct cf_block *cf;
15076 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015077 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015078 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15079 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015080
Eric Biederman90089602004-05-28 14:11:54 +000015081 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015082 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015083 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015084 block = cf[i].block;
15085 if (!block)
15086 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015087 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015088 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015089 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015090 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015091 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015092 }
15093
15094 xfree(cf);
15095}
15096
Eric Biedermanb138ac82003-04-22 18:44:01 +000015097static void free_basic_block(struct compile_state *state, struct block *block)
15098{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015099 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015100 struct block *child;
15101 if (!block) {
15102 return;
15103 }
15104 if (block->vertex == -1) {
15105 return;
15106 }
15107 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015108 for(edge = block->edges; edge; edge = edge->next) {
15109 if (edge->member) {
15110 unuse_block(edge->member, block);
15111 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015112 }
15113 if (block->idom) {
15114 unidom_block(block->idom, block);
15115 }
15116 block->idom = 0;
15117 if (block->ipdom) {
15118 unipdom_block(block->ipdom, block);
15119 }
15120 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015121 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015122 child = entry->member;
15123 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015124 if (child && (child->vertex != -1)) {
15125 for(edge = child->edges; edge; edge = edge->next) {
15126 edge->member = 0;
15127 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015128 }
15129 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015130 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015131 child = entry->member;
15132 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015133 if (child && (child->vertex != -1)) {
15134 child->idom = 0;
15135 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015136 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015137 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015138 child = entry->member;
15139 undomf_block(block, child);
15140 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015141 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015142 child = entry->member;
15143 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015144 if (child && (child->vertex != -1)) {
15145 child->ipdom = 0;
15146 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015147 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015148 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015149 child = entry->member;
15150 unipdomf_block(block, child);
15151 }
15152 if (block->users != 0) {
15153 internal_error(state, 0, "block still has users");
15154 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015155 while((edge = block->edges)) {
15156 child = edge->member;
15157 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015158
Eric Biederman5ade04a2003-10-22 04:03:46 +000015159 if (child && (child->vertex != -1)) {
15160 free_basic_block(state, child);
15161 }
15162 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015163 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015164#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015165 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015166#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015167}
15168
Stefan Reinauer14e22772010-04-27 06:56:47 +000015169static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015170 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015171{
15172 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015173 free_basic_block(state, bb->first_block);
15174 bb->last_vertex = 0;
15175 bb->first_block = bb->last_block = 0;
15176 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015177 ins = first;
15178 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015179 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015180 ins->u.block = 0;
15181 }
15182 ins = ins->next;
15183 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015184
Eric Biedermanb138ac82003-04-22 18:44:01 +000015185}
15186
Stefan Reinauer14e22772010-04-27 06:56:47 +000015187static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015188 struct basic_blocks *bb, struct triple *first)
15189{
15190 struct block *block;
15191 struct triple *ptr;
15192 if (!triple_is_label(state, first)) {
15193 internal_error(state, first, "block does not start with a label");
15194 }
15195 /* See if this basic block has already been setup */
15196 if (first->u.block != 0) {
15197 return first->u.block;
15198 }
15199 /* Allocate another basic block structure */
15200 bb->last_vertex += 1;
15201 block = xcmalloc(sizeof(*block), "block");
15202 block->first = block->last = first;
15203 block->vertex = bb->last_vertex;
15204 ptr = first;
15205 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015206 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015207 break;
15208 }
15209 block->last = ptr;
15210 /* If ptr->u is not used remember where the baic block is */
15211 if (triple_stores_block(state, ptr)) {
15212 ptr->u.block = block;
15213 }
15214 if (triple_is_branch(state, ptr)) {
15215 break;
15216 }
15217 ptr = ptr->next;
15218 } while (ptr != bb->first);
15219 if ((ptr == bb->first) ||
15220 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015221 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015222 triple_is_ret(state, ptr))))
15223 {
15224 /* The block has no outflowing edges */
15225 }
15226 else if (triple_is_label(state, ptr)) {
15227 struct block *next;
15228 next = basic_block(state, bb, ptr);
15229 add_block_edge(block, next, 0);
15230 use_block(next, block);
15231 }
15232 else if (triple_is_branch(state, ptr)) {
15233 struct triple **expr, *first;
15234 struct block *child;
15235 /* Find the branch targets.
15236 * I special case the first branch as that magically
15237 * avoids some difficult cases for the register allocator.
15238 */
15239 expr = triple_edge_targ(state, ptr, 0);
15240 if (!expr) {
15241 internal_error(state, ptr, "branch without targets");
15242 }
15243 first = *expr;
15244 expr = triple_edge_targ(state, ptr, expr);
15245 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15246 if (!*expr) continue;
15247 child = basic_block(state, bb, *expr);
15248 use_block(child, block);
15249 add_block_edge(block, child, 0);
15250 }
15251 if (first) {
15252 child = basic_block(state, bb, first);
15253 use_block(child, block);
15254 add_block_edge(block, child, 1);
15255
15256 /* Be certain the return block of a call is
15257 * in a basic block. When it is not find
15258 * start of the block, insert a label if
15259 * necessary and build the basic block.
15260 * Then add a fake edge from the start block
15261 * to the return block of the function.
15262 */
15263 if (state->functions_joined && triple_is_call(state, ptr)
15264 && !block_of_triple(state, MISC(ptr, 0))) {
15265 struct block *tail;
15266 struct triple *start;
15267 start = triple_to_block_start(state, MISC(ptr, 0));
15268 if (!triple_is_label(state, start)) {
15269 start = pre_triple(state,
15270 start, OP_LABEL, &void_type, 0, 0);
15271 }
15272 tail = basic_block(state, bb, start);
15273 add_block_edge(child, tail, 0);
15274 use_block(tail, child);
15275 }
15276 }
15277 }
15278 else {
15279 internal_error(state, 0, "Bad basic block split");
15280 }
15281#if 0
15282{
15283 struct block_set *edge;
15284 FILE *fp = state->errout;
15285 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015286 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015287 block->first, block->last);
15288 for(edge = block->edges; edge; edge = edge->next) {
15289 fprintf(fp, " %10p [%2d]",
15290 edge->member ? edge->member->first : 0,
15291 edge->member ? edge->member->vertex : -1);
15292 }
15293 fprintf(fp, "\n");
15294}
15295#endif
15296 return block;
15297}
15298
15299
15300static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15301 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15302 void *arg)
15303{
15304 struct triple *ptr, *first;
15305 struct block *last_block;
15306 last_block = 0;
15307 first = bb->first;
15308 ptr = first;
15309 do {
15310 if (triple_stores_block(state, ptr)) {
15311 struct block *block;
15312 block = ptr->u.block;
15313 if (block && (block != last_block)) {
15314 cb(state, block, arg);
15315 }
15316 last_block = block;
15317 }
15318 ptr = ptr->next;
15319 } while(ptr != first);
15320}
15321
15322static void print_block(
15323 struct compile_state *state, struct block *block, void *arg)
15324{
15325 struct block_set *user, *edge;
15326 struct triple *ptr;
15327 FILE *fp = arg;
15328
15329 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015330 block,
Eric Biederman90089602004-05-28 14:11:54 +000015331 block->vertex);
15332
15333 for(edge = block->edges; edge; edge = edge->next) {
15334 fprintf(fp, " %p<-%p",
15335 edge->member,
15336 (edge->member && edge->member->use)?
15337 edge->member->use->member : 0);
15338 }
15339 fprintf(fp, "\n");
15340 if (block->first->op == OP_LABEL) {
15341 fprintf(fp, "%p:\n", block->first);
15342 }
15343 for(ptr = block->first; ; ) {
15344 display_triple(fp, ptr);
15345 if (ptr == block->last)
15346 break;
15347 ptr = ptr->next;
15348 if (ptr == block->first) {
15349 internal_error(state, 0, "missing block last?");
15350 }
15351 }
15352 fprintf(fp, "users %d: ", block->users);
15353 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015354 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015355 user->member,
15356 user->member->vertex);
15357 }
15358 fprintf(fp,"\n\n");
15359}
15360
15361
15362static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15363{
15364 fprintf(fp, "--------------- blocks ---------------\n");
15365 walk_blocks(state, &state->bb, print_block, fp);
15366}
15367static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15368{
15369 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15370 fprintf(fp, "After %s\n", func);
15371 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015372 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15373 print_dominators(state, fp, &state->bb);
15374 print_dominance_frontiers(state, fp, &state->bb);
15375 }
15376 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015377 }
15378}
15379
Stefan Reinauer14e22772010-04-27 06:56:47 +000015380static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015381 struct basic_blocks *bb)
15382{
15383 struct block *block;
15384 struct triple *first, *ins, *next;
15385 /* Delete the triples not in a basic block */
15386 block = 0;
15387 first = bb->first;
15388 ins = first;
15389 do {
15390 next = ins->next;
15391 if (ins->op == OP_LABEL) {
15392 block = ins->u.block;
15393 }
15394 if (!block) {
15395 struct triple_set *use;
15396 for(use = ins->use; use; use = use->next) {
15397 struct block *block;
15398 block = block_of_triple(state, use->member);
15399 if (block != 0) {
15400 internal_error(state, ins, "pruning used ins?");
15401 }
15402 }
15403 release_triple(state, ins);
15404 }
15405 if (block && block->last == ins) {
15406 block = 0;
15407 }
15408 ins = next;
15409 } while(ins != first);
15410}
15411
Stefan Reinauer14e22772010-04-27 06:56:47 +000015412static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015413 struct basic_blocks *bb)
15414{
15415 if (!triple_stores_block(state, bb->first)) {
15416 internal_error(state, 0, "ins will not store block?");
15417 }
15418 /* Initialize the state */
15419 bb->first_block = bb->last_block = 0;
15420 bb->last_vertex = 0;
15421 free_basic_blocks(state, bb);
15422
15423 /* Find the basic blocks */
15424 bb->first_block = basic_block(state, bb, bb->first);
15425
15426 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015427 * entire program is in a basic block. When it is not find
15428 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015429 * basic block. Then add a fake edge from the start block
15430 * to the final block.
15431 */
15432 if (!block_of_triple(state, bb->first->prev)) {
15433 struct triple *start;
15434 struct block *tail;
15435 start = triple_to_block_start(state, bb->first->prev);
15436 if (!triple_is_label(state, start)) {
15437 start = pre_triple(state,
15438 start, OP_LABEL, &void_type, 0, 0);
15439 }
15440 tail = basic_block(state, bb, start);
15441 add_block_edge(bb->first_block, tail, 0);
15442 use_block(tail, bb->first_block);
15443 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015444
Eric Biederman90089602004-05-28 14:11:54 +000015445 /* Find the last basic block.
15446 */
15447 bb->last_block = block_of_triple(state, bb->first->prev);
15448
15449 /* Delete the triples not in a basic block */
15450 prune_nonblock_triples(state, bb);
15451
15452#if 0
15453 /* If we are debugging print what I have just done */
15454 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15455 print_blocks(state, state->dbgout);
15456 print_control_flow(state, bb);
15457 }
15458#endif
15459}
15460
15461
Eric Biedermanb138ac82003-04-22 18:44:01 +000015462struct sdom_block {
15463 struct block *block;
15464 struct sdom_block *sdominates;
15465 struct sdom_block *sdom_next;
15466 struct sdom_block *sdom;
15467 struct sdom_block *label;
15468 struct sdom_block *parent;
15469 struct sdom_block *ancestor;
15470 int vertex;
15471};
15472
15473
15474static void unsdom_block(struct sdom_block *block)
15475{
15476 struct sdom_block **ptr;
15477 if (!block->sdom_next) {
15478 return;
15479 }
15480 ptr = &block->sdom->sdominates;
15481 while(*ptr) {
15482 if ((*ptr) == block) {
15483 *ptr = block->sdom_next;
15484 return;
15485 }
15486 ptr = &(*ptr)->sdom_next;
15487 }
15488}
15489
15490static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15491{
15492 unsdom_block(block);
15493 block->sdom = sdom;
15494 block->sdom_next = sdom->sdominates;
15495 sdom->sdominates = block;
15496}
15497
15498
15499
15500static int initialize_sdblock(struct sdom_block *sd,
15501 struct block *parent, struct block *block, int vertex)
15502{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015503 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015504 if (!block || (sd[block->vertex].block == block)) {
15505 return vertex;
15506 }
15507 vertex += 1;
15508 /* Renumber the blocks in a convinient fashion */
15509 block->vertex = vertex;
15510 sd[vertex].block = block;
15511 sd[vertex].sdom = &sd[vertex];
15512 sd[vertex].label = &sd[vertex];
15513 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15514 sd[vertex].ancestor = 0;
15515 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015516 for(edge = block->edges; edge; edge = edge->next) {
15517 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15518 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015519 return vertex;
15520}
15521
Eric Biederman83b991a2003-10-11 06:20:25 +000015522static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015523 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015524 struct block *parent, struct block *block, int vertex)
15525{
15526 struct block_set *user;
15527 if (!block || (sd[block->vertex].block == block)) {
15528 return vertex;
15529 }
15530 vertex += 1;
15531 /* Renumber the blocks in a convinient fashion */
15532 block->vertex = vertex;
15533 sd[vertex].block = block;
15534 sd[vertex].sdom = &sd[vertex];
15535 sd[vertex].label = &sd[vertex];
15536 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15537 sd[vertex].ancestor = 0;
15538 sd[vertex].vertex = vertex;
15539 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015540 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015541 }
15542 return vertex;
15543}
15544
Stefan Reinauer14e22772010-04-27 06:56:47 +000015545static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015546 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015547{
15548 struct block *block;
15549 int vertex;
15550 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015551 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015552
Eric Biederman5ade04a2003-10-22 04:03:46 +000015553 /* Walk through the graph and find unconnected blocks. Add a
15554 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015555 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015556 */
Eric Biederman90089602004-05-28 14:11:54 +000015557 block = bb->first_block->last->next->u.block;
15558 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015559 if (sd[block->vertex].block == block) {
15560 continue;
15561 }
Eric Biederman530b5192003-07-01 10:05:30 +000015562#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015563 {
15564 FILE *fp = state->errout;
15565 fprintf(fp, "Adding %d\n", vertex +1);
15566 }
Eric Biederman530b5192003-07-01 10:05:30 +000015567#endif
Eric Biederman90089602004-05-28 14:11:54 +000015568 add_block_edge(block, bb->last_block, 0);
15569 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015570
Eric Biederman90089602004-05-28 14:11:54 +000015571 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015572 }
15573 return vertex;
15574}
15575
15576static void compress_ancestors(struct sdom_block *v)
15577{
15578 /* This procedure assumes ancestor(v) != 0 */
15579 /* if (ancestor(ancestor(v)) != 0) {
15580 * compress(ancestor(ancestor(v)));
15581 * if (semi(label(ancestor(v))) < semi(label(v))) {
15582 * label(v) = label(ancestor(v));
15583 * }
15584 * ancestor(v) = ancestor(ancestor(v));
15585 * }
15586 */
15587 if (!v->ancestor) {
15588 return;
15589 }
15590 if (v->ancestor->ancestor) {
15591 compress_ancestors(v->ancestor->ancestor);
15592 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15593 v->label = v->ancestor->label;
15594 }
15595 v->ancestor = v->ancestor->ancestor;
15596 }
15597}
15598
Stefan Reinauer14e22772010-04-27 06:56:47 +000015599static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015600 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015601{
15602 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015603 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015604 * for each v <= pred(w) {
15605 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015606 * if (semi[u] < semi[w] {
15607 * semi[w] = semi[u];
15608 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015609 * }
15610 * add w to bucket(vertex(semi[w]));
15611 * LINK(parent(w), w);
15612 *
15613 * // step 3
15614 * for each v <= bucket(parent(w)) {
15615 * delete v from bucket(parent(w));
15616 * u = EVAL(v);
15617 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15618 * }
15619 */
Eric Biederman90089602004-05-28 14:11:54 +000015620 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015621 struct sdom_block *v, *parent, *next;
15622 struct block_set *user;
15623 struct block *block;
15624 block = sd[i].block;
15625 parent = sd[i].parent;
15626 /* Step 2 */
15627 for(user = block->use; user; user = user->next) {
15628 struct sdom_block *v, *u;
15629 v = &sd[user->member->vertex];
15630 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15631 if (u->sdom->vertex < sd[i].sdom->vertex) {
15632 sd[i].sdom = u->sdom;
15633 }
15634 }
15635 sdom_block(sd[i].sdom, &sd[i]);
15636 sd[i].ancestor = parent;
15637 /* Step 3 */
15638 for(v = parent->sdominates; v; v = next) {
15639 struct sdom_block *u;
15640 next = v->sdom_next;
15641 unsdom_block(v);
15642 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015643 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015644 u->block : parent->block;
15645 }
15646 }
15647}
15648
Stefan Reinauer14e22772010-04-27 06:56:47 +000015649static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015650 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015651{
15652 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015653 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015654 * for each v <= pred(w) {
15655 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015656 * if (semi[u] < semi[w] {
15657 * semi[w] = semi[u];
15658 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015659 * }
15660 * add w to bucket(vertex(semi[w]));
15661 * LINK(parent(w), w);
15662 *
15663 * // step 3
15664 * for each v <= bucket(parent(w)) {
15665 * delete v from bucket(parent(w));
15666 * u = EVAL(v);
15667 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15668 * }
15669 */
Eric Biederman90089602004-05-28 14:11:54 +000015670 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015671 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015672 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015673 struct block *block;
15674 block = sd[i].block;
15675 parent = sd[i].parent;
15676 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015677 for(edge = block->edges; edge; edge = edge->next) {
15678 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015679 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15680 if (u->sdom->vertex < sd[i].sdom->vertex) {
15681 sd[i].sdom = u->sdom;
15682 }
15683 }
15684 sdom_block(sd[i].sdom, &sd[i]);
15685 sd[i].ancestor = parent;
15686 /* Step 3 */
15687 for(v = parent->sdominates; v; v = next) {
15688 struct sdom_block *u;
15689 next = v->sdom_next;
15690 unsdom_block(v);
15691 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015692 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015693 u->block : parent->block;
15694 }
15695 }
15696}
15697
Stefan Reinauer14e22772010-04-27 06:56:47 +000015698static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015699 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->idom->vertex != sd[i].sdom->vertex) {
15706 block->idom = block->idom->idom;
15707 }
15708 idom_block(block->idom, block);
15709 }
15710 sd[1].block->idom = 0;
15711}
15712
Stefan Reinauer14e22772010-04-27 06:56:47 +000015713static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015714 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015715{
15716 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015717 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015718 struct block *block;
15719 block = sd[i].block;
15720 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15721 block->ipdom = block->ipdom->ipdom;
15722 }
15723 ipdom_block(block->ipdom, block);
15724 }
15725 sd[1].block->ipdom = 0;
15726}
15727
15728 /* Theorem 1:
15729 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015730 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015731 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015732 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015733 * v dominates w if and only if v is a proper ancestor of w in
15734 * the dominator tree.
15735 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015736 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015737 * If v and w are vertices of G such that v <= w,
15738 * than any path from v to w must contain a common ancestor
15739 * of v and w in T.
15740 */
15741 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15742 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15743 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15744 /* Theorem 2:
15745 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15746 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15747 */
15748 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015749 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015750 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15751 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15752 */
15753 /* Lemma 5: Let vertices v,w satisfy v -> w.
15754 * Then v -> idom(w) or idom(w) -> idom(v)
15755 */
15756
Eric Biederman90089602004-05-28 14:11:54 +000015757static void find_immediate_dominators(struct compile_state *state,
15758 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015759{
15760 struct sdom_block *sd;
15761 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15762 * vi > w for (1 <= i <= k - 1}
15763 */
15764 /* Theorem 4:
15765 * For any vertex w != r.
15766 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015767 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015768 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15769 */
15770 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015771 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015772 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15773 * Then:
15774 * { sdom(w) if sdom(w) = sdom(u),
15775 * idom(w) = {
15776 * { idom(u) otherwise
15777 */
15778 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015779 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015780 * Number the vertices from 1 to N as they are reached during
15781 * the search. Initialize the variables used in succeeding steps.
15782 * Step 2. Compute the semidominators of all vertices by applying
15783 * theorem 4. Carry out the computation vertex by vertex in
15784 * decreasing order by number.
15785 * Step 3. Implicitly define the immediate dominator of each vertex
15786 * by applying Corollary 1.
15787 * Step 4. Explicitly define the immediate dominator of each vertex,
15788 * carrying out the computation vertex by vertex in increasing order
15789 * by number.
15790 */
15791 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015792 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15793 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015794#if 0
15795 sd[1].size = 0;
15796 sd[1].label = 0;
15797 sd[1].sdom = 0;
15798#endif
15799 /* Step 2 compute the semidominators */
15800 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015801 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015802 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015803 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015804 xfree(sd);
15805}
15806
Eric Biederman90089602004-05-28 14:11:54 +000015807static void find_post_dominators(struct compile_state *state,
15808 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015809{
15810 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015811 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015812 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015813 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015814
Eric Biederman90089602004-05-28 14:11:54 +000015815 vertex = setup_spdblocks(state, bb, sd);
15816 if (vertex != bb->last_vertex) {
15817 internal_error(state, 0, "missing %d blocks",
15818 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015819 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015820
15821 /* Step 2 compute the semidominators */
15822 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015823 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015824 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015825 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015826 xfree(sd);
15827}
15828
15829
15830
15831static void find_block_domf(struct compile_state *state, struct block *block)
15832{
15833 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015834 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015835 if (block->domfrontier != 0) {
15836 internal_error(state, block->first, "domfrontier present?");
15837 }
15838 for(user = block->idominates; user; user = user->next) {
15839 child = user->member;
15840 if (child->idom != block) {
15841 internal_error(state, block->first, "bad idom");
15842 }
15843 find_block_domf(state, child);
15844 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015845 for(edge = block->edges; edge; edge = edge->next) {
15846 if (edge->member->idom != block) {
15847 domf_block(block, edge->member);
15848 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015849 }
15850 for(user = block->idominates; user; user = user->next) {
15851 struct block_set *frontier;
15852 child = user->member;
15853 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15854 if (frontier->member->idom != block) {
15855 domf_block(block, frontier->member);
15856 }
15857 }
15858 }
15859}
15860
15861static void find_block_ipdomf(struct compile_state *state, struct block *block)
15862{
15863 struct block *child;
15864 struct block_set *user;
15865 if (block->ipdomfrontier != 0) {
15866 internal_error(state, block->first, "ipdomfrontier present?");
15867 }
15868 for(user = block->ipdominates; user; user = user->next) {
15869 child = user->member;
15870 if (child->ipdom != block) {
15871 internal_error(state, block->first, "bad ipdom");
15872 }
15873 find_block_ipdomf(state, child);
15874 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015875 for(user = block->use; user; user = user->next) {
15876 if (user->member->ipdom != block) {
15877 ipdomf_block(block, user->member);
15878 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015879 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015880 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015881 struct block_set *frontier;
15882 child = user->member;
15883 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15884 if (frontier->member->ipdom != block) {
15885 ipdomf_block(block, frontier->member);
15886 }
15887 }
15888 }
15889}
15890
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015891static void print_dominated(
15892 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015893{
15894 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015897 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015898 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015899 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015900 if (user->member->idom != block) {
15901 internal_error(state, user->member->first, "bad idom");
15902 }
15903 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015904 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015905}
15906
Eric Biederman5ade04a2003-10-22 04:03:46 +000015907static void print_dominated2(
15908 struct compile_state *state, FILE *fp, int depth, struct block *block)
15909{
15910 struct block_set *user;
15911 struct triple *ins;
15912 struct occurance *ptr, *ptr2;
15913 const char *filename1, *filename2;
15914 int equal_filenames;
15915 int i;
15916 for(i = 0; i < depth; i++) {
15917 fprintf(fp, " ");
15918 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015919 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015920 block->vertex, block, block->first, block->last);
15921 ins = block->first;
15922 while(ins != block->last && (ins->occurance->line == 0)) {
15923 ins = ins->next;
15924 }
15925 ptr = ins->occurance;
15926 ptr2 = block->last->occurance;
15927 filename1 = ptr->filename? ptr->filename : "";
15928 filename2 = ptr2->filename? ptr2->filename : "";
15929 equal_filenames = (strcmp(filename1, filename2) == 0);
15930 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15931 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15932 } else if (equal_filenames) {
15933 fprintf(fp, " %s:(%d - %d)",
15934 ptr->filename, ptr->line, ptr2->line);
15935 } else {
15936 fprintf(fp, " (%s:%d - %s:%d)",
15937 ptr->filename, ptr->line,
15938 ptr2->filename, ptr2->line);
15939 }
15940 fprintf(fp, "\n");
15941 for(user = block->idominates; user; user = user->next) {
15942 print_dominated2(state, fp, depth + 1, user->member);
15943 }
15944}
15945
Eric Biederman90089602004-05-28 14:11:54 +000015946static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015947{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015948 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015949 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015950 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015951 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015952}
15953
15954
15955static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015956 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015957{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015958 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015959
15960 if (!block || (block->vertex != vertex + 1)) {
15961 return vertex;
15962 }
15963 vertex += 1;
15964
Eric Biederman7dea9552004-06-29 05:38:37 +000015965 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015966 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015967 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015968 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015969 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015970
Eric Biederman5ade04a2003-10-22 04:03:46 +000015971 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015972 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015973 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015974 return vertex;
15975}
Eric Biederman90089602004-05-28 14:11:54 +000015976static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015977 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015978{
Eric Biederman7dea9552004-06-29 05:38:37 +000015979 fprintf(fp, "\ndominance frontiers\n");
15980 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015981
Eric Biedermanb138ac82003-04-22 18:44:01 +000015982}
15983
Eric Biederman90089602004-05-28 14:11:54 +000015984static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015985{
15986 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015987 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015988 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015989 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015990 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015991 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015992 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015993 print_dominance_frontiers(state, state->dbgout, bb);
15994 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015995 }
15996}
15997
15998
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015999static void print_ipdominated(
16000 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016001{
16002 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016005 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016006 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016007 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016008 if (user->member->ipdom != block) {
16009 internal_error(state, user->member->first, "bad ipdom");
16010 }
16011 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016012 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016013}
16014
Eric Biederman90089602004-05-28 14:11:54 +000016015static void print_ipdominators(struct compile_state *state, FILE *fp,
16016 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016017{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016018 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016019 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016020}
16021
16022static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016023 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016024{
16025 struct block_set *user;
16026
16027 if (!block || (block->vertex != vertex + 1)) {
16028 return vertex;
16029 }
16030 vertex += 1;
16031
Eric Biederman7dea9552004-06-29 05:38:37 +000016032 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016033 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016034 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016036 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016037 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016038 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016039 }
16040 return vertex;
16041}
Eric Biederman90089602004-05-28 14:11:54 +000016042static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016043 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016044{
Eric Biederman7dea9552004-06-29 05:38:37 +000016045 fprintf(fp, "\nipdominance frontiers\n");
16046 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016047
Eric Biedermanb138ac82003-04-22 18:44:01 +000016048}
16049
Eric Biederman90089602004-05-28 14:11:54 +000016050static void analyze_ipdominators(struct compile_state *state,
16051 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016052{
16053 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016054 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016055 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016056 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016057 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016058 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016059 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016060 print_ipdominance_frontiers(state, state->dbgout, bb);
16061 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016062 }
16063}
16064
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016065static int bdominates(struct compile_state *state,
16066 struct block *dom, struct block *sub)
16067{
16068 while(sub && (sub != dom)) {
16069 sub = sub->idom;
16070 }
16071 return sub == dom;
16072}
16073
16074static int tdominates(struct compile_state *state,
16075 struct triple *dom, struct triple *sub)
16076{
16077 struct block *bdom, *bsub;
16078 int result;
16079 bdom = block_of_triple(state, dom);
16080 bsub = block_of_triple(state, sub);
16081 if (bdom != bsub) {
16082 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016083 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016084 else {
16085 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016086 if (!bdom || !bsub) {
16087 internal_error(state, dom, "huh?");
16088 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016089 ins = sub;
16090 while((ins != bsub->first) && (ins != dom)) {
16091 ins = ins->prev;
16092 }
16093 result = (ins == dom);
16094 }
16095 return result;
16096}
16097
Eric Biederman90089602004-05-28 14:11:54 +000016098static void analyze_basic_blocks(
16099 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016100{
Eric Biederman90089602004-05-28 14:11:54 +000016101 setup_basic_blocks(state, bb);
16102 analyze_idominators(state, bb);
16103 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016104}
16105
Eric Biedermanb138ac82003-04-22 18:44:01 +000016106static void insert_phi_operations(struct compile_state *state)
16107{
16108 size_t size;
16109 struct triple *first;
16110 int *has_already, *work;
16111 struct block *work_list, **work_list_tail;
16112 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016113 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016114
Eric Biederman90089602004-05-28 14:11:54 +000016115 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016116 has_already = xcmalloc(size, "has_already");
16117 work = xcmalloc(size, "work");
16118 iter = 0;
16119
Eric Biederman83b991a2003-10-11 06:20:25 +000016120 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016121 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016122 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016123 struct triple_set *user, *unext;
16124 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016125
16126 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016127 continue;
16128 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016129
Eric Biedermanb138ac82003-04-22 18:44:01 +000016130 iter += 1;
16131 work_list = 0;
16132 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016133 for(user = var->use; user; user = unext) {
16134 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016135 if (MISC(var, 0) == user->member) {
16136 continue;
16137 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016138 if (user->member->op == OP_READ) {
16139 continue;
16140 }
16141 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016142 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016143 "bad variable access");
16144 }
16145 block = user->member->u.block;
16146 if (!block) {
16147 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016148 release_triple(state, user->member);
16149 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016150 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016151 if (work[block->vertex] >= iter) {
16152 continue;
16153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016154 work[block->vertex] = iter;
16155 *work_list_tail = block;
16156 block->work_next = 0;
16157 work_list_tail = &block->work_next;
16158 }
16159 for(block = work_list; block; block = block->work_next) {
16160 struct block_set *df;
16161 for(df = block->domfrontier; df; df = df->next) {
16162 struct triple *phi;
16163 struct block *front;
16164 int in_edges;
16165 front = df->member;
16166
16167 if (has_already[front->vertex] >= iter) {
16168 continue;
16169 }
16170 /* Count how many edges flow into this block */
16171 in_edges = front->users;
16172 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016173 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016174 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016175 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016176 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016177 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016178 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016179 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016180#if 1
16181 if (phi->rhs != in_edges) {
16182 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16183 phi->rhs, in_edges);
16184 }
16185#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016186 /* Insert the phi functions immediately after the label */
16187 insert_triple(state, front->first->next, phi);
16188 if (front->first == front->last) {
16189 front->last = front->first->next;
16190 }
16191 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016192 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016193
Eric Biedermanb138ac82003-04-22 18:44:01 +000016194 /* If necessary plan to visit the basic block */
16195 if (work[front->vertex] >= iter) {
16196 continue;
16197 }
16198 work[front->vertex] = iter;
16199 *work_list_tail = front;
16200 front->work_next = 0;
16201 work_list_tail = &front->work_next;
16202 }
16203 }
16204 }
16205 xfree(has_already);
16206 xfree(work);
16207}
16208
Eric Biederman66fe2222003-07-04 15:14:04 +000016209
Eric Biederman83b991a2003-10-11 06:20:25 +000016210struct stack {
16211 struct triple_set *top;
16212 unsigned orig_id;
16213};
16214
Eric Biederman90089602004-05-28 14:11:54 +000016215static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +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;
Eric Biederman66fe2222003-07-04 15:14:04 +000016220 ins = first;
16221 do {
Eric Biederman90089602004-05-28 14:11:54 +000016222 if (triple_is_auto_var(state, ins)) {
16223 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016224 }
16225 ins = ins->next;
16226 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016227 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016228}
16229
Eric Biederman90089602004-05-28 14:11:54 +000016230static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016231{
16232 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016233 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016234 first = state->first;
16235 ins = first;
16236 do {
Eric Biederman90089602004-05-28 14:11:54 +000016237 if (triple_is_auto_var(state, ins)) {
16238 auto_vars += 1;
16239 stacks[auto_vars].orig_id = ins->id;
16240 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016241 }
16242 ins = ins->next;
16243 } while(ins != first);
16244}
16245
Eric Biederman90089602004-05-28 14:11:54 +000016246static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016247{
16248 struct triple *first, *ins;
16249 first = state->first;
16250 ins = first;
16251 do {
Eric Biederman90089602004-05-28 14:11:54 +000016252 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016253 ins->id = stacks[ins->id].orig_id;
16254 }
16255 ins = ins->next;
16256 } while(ins != first);
16257}
16258
16259static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016260{
16261 struct triple_set *head;
16262 struct triple *top_val;
16263 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016264 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016265 if (head) {
16266 top_val = head->member;
16267 }
16268 return top_val;
16269}
16270
Eric Biederman83b991a2003-10-11 06:20:25 +000016271static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016272{
16273 struct triple_set *new;
16274 /* Append new to the head of the list,
16275 * it's the only sensible behavoir for a stack.
16276 */
16277 new = xcmalloc(sizeof(*new), "triple_set");
16278 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016279 new->next = stacks[var->id].top;
16280 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016281}
16282
Eric Biederman83b991a2003-10-11 06:20:25 +000016283static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016284{
16285 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016286 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016287 while(*ptr) {
16288 set = *ptr;
16289 if (set->member == oldval) {
16290 *ptr = set->next;
16291 xfree(set);
16292 /* Only free one occurance from the stack */
16293 return;
16294 }
16295 else {
16296 ptr = &set->next;
16297 }
16298 }
16299}
16300
Eric Biedermanb138ac82003-04-22 18:44:01 +000016301/*
16302 * C(V)
16303 * S(V)
16304 */
16305static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016306 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016307{
16308 struct block_set *set;
16309 struct triple *ptr;
16310 int edge;
16311 if (!parent || !block)
16312 return;
16313 /* Find the edge I am coming in on */
16314 edge = 0;
16315 for(set = block->use; set; set = set->next, edge++) {
16316 if (set->member == parent) {
16317 break;
16318 }
16319 }
16320 if (!set) {
16321 internal_error(state, 0, "phi input is not on a control predecessor");
16322 }
16323 for(ptr = block->first; ; ptr = ptr->next) {
16324 if (ptr->op == OP_PHI) {
16325 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016326 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016327 if (!var) {
16328 internal_error(state, ptr, "no var???");
16329 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016330 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016331 val = peek_triple(stacks, var);
16332 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016333 internal_error(state, val, "bad value in phi");
16334 }
Eric Biederman90089602004-05-28 14:11:54 +000016335 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016336 internal_error(state, ptr, "edges > phi rhs");
16337 }
16338 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016339 if ((*slot != 0) && (*slot != val)) {
16340 internal_error(state, ptr, "phi already bound on this edge");
16341 }
16342 *slot = val;
16343 use_triple(val, ptr);
16344 }
16345 if (ptr == block->last) {
16346 break;
16347 }
16348 }
16349}
16350
16351
16352static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016353 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016354{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016355 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016356 struct triple *ptr, *next, *last;
16357 int done;
16358 if (!block)
16359 return;
16360 last = block->first;
16361 done = 0;
16362 for(ptr = block->first; !done; ptr = next) {
16363 next = ptr->next;
16364 if (ptr == block->last) {
16365 done = 1;
16366 }
16367 /* RHS(A) */
16368 if (ptr->op == OP_READ) {
16369 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016370 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016371 if (!triple_is_auto_var(state, var)) {
16372 internal_error(state, ptr, "read of non auto var!");
16373 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016374 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016375 /* Find the current value of the variable */
16376 val = peek_triple(stacks, var);
16377 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016378 /* Let the optimizer at variables that are not initially
16379 * set. But give it a bogus value so things seem to
16380 * work by accident. This is useful for bitfields because
16381 * setting them always involves a read-modify-write.
16382 */
16383 if (TYPE_ARITHMETIC(ptr->type->type)) {
16384 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16385 val->u.cval = 0xdeadbeaf;
16386 } else {
16387 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16388 }
16389 }
16390 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016391 error(state, ptr, "variable used without being set");
16392 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016393 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16394 internal_error(state, val, "bad value in read");
16395 }
16396 propogate_use(state, ptr, val);
16397 release_triple(state, ptr);
16398 continue;
16399 }
16400 /* LHS(A) */
16401 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016402 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016403 var = MISC(ptr, 0);
16404 if (!triple_is_auto_var(state, var)) {
16405 internal_error(state, ptr, "write to non auto var!");
16406 }
16407 tval = val = RHS(ptr, 0);
16408 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16409 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016410 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016411 }
Eric Biederman90089602004-05-28 14:11:54 +000016412 /* Insert a cast if the types differ */
16413 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016414 if (val->op == OP_INTCONST) {
16415 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16416 tval->u.cval = val->u.cval;
16417 }
16418 else {
Eric Biederman90089602004-05-28 14:11:54 +000016419 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016420 use_triple(val, tval);
16421 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016422 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016423 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016424 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016425 use_triple(tval, ptr);
16426 }
16427 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016428 unuse_triple(var, ptr);
16429 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016430 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016431 }
16432 if (ptr->op == OP_PHI) {
16433 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016434 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016435 if (!triple_is_auto_var(state, var)) {
16436 internal_error(state, ptr, "phi references non auto var!");
16437 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016438 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016439 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016440 }
16441 last = ptr;
16442 }
16443 block->last = last;
16444
16445 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016446 for(edge = block->edges; edge; edge = edge->next) {
16447 fixup_block_phi_variables(state, stacks, block, edge->member);
16448 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016449 /* rename variables in the dominated nodes */
16450 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016451 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016452 }
16453 /* pop the renamed variable stack */
16454 last = block->first;
16455 done = 0;
16456 for(ptr = block->first; !done ; ptr = next) {
16457 next = ptr->next;
16458 if (ptr == block->last) {
16459 done = 1;
16460 }
16461 if (ptr->op == OP_WRITE) {
16462 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016463 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016464 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016465 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016466 release_triple(state, ptr);
16467 continue;
16468 }
16469 if (ptr->op == OP_PHI) {
16470 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016471 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016472 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016473 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016474 }
16475 last = ptr;
16476 }
16477 block->last = last;
16478}
16479
Eric Biederman83b991a2003-10-11 06:20:25 +000016480static void rename_variables(struct compile_state *state)
16481{
16482 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016483 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016484
16485 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016486 auto_vars = count_auto_vars(state);
16487 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016488
Eric Biederman90089602004-05-28 14:11:54 +000016489 /* Give each auto_var a stack */
16490 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016491
16492 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016493 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016494
Eric Biederman90089602004-05-28 14:11:54 +000016495 /* Remove the stacks from the auto_vars */
16496 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016497 xfree(stacks);
16498}
16499
Eric Biedermanb138ac82003-04-22 18:44:01 +000016500static void prune_block_variables(struct compile_state *state,
16501 struct block *block)
16502{
16503 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016504 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016505 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016506
Eric Biedermanb138ac82003-04-22 18:44:01 +000016507 done = 0;
16508 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016509 /* Be extremely careful I am deleting the list
16510 * as I walk trhough it.
16511 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016512 next = ptr->next;
16513 if (ptr == block->last) {
16514 done = 1;
16515 }
Eric Biederman90089602004-05-28 14:11:54 +000016516 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016517 struct triple_set *user, *next;
16518 for(user = ptr->use; user; user = next) {
16519 struct triple *use;
16520 next = user->next;
16521 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016522 if (MISC(ptr, 0) == user->member) {
16523 continue;
16524 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016525 if (use->op != OP_PHI) {
16526 internal_error(state, use, "decl still used");
16527 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016528 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016529 internal_error(state, use, "bad phi use of decl");
16530 }
16531 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016532 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016533 }
Eric Biederman90089602004-05-28 14:11:54 +000016534 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16535 /* Delete the adecl */
16536 release_triple(state, MISC(ptr, 0));
16537 /* And the piece */
16538 release_triple(state, ptr);
16539 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016540 continue;
16541 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016542 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016543 for(user = block->idominates; user; user = user->next) {
16544 prune_block_variables(state, user->member);
16545 }
16546}
16547
Eric Biederman66fe2222003-07-04 15:14:04 +000016548struct phi_triple {
16549 struct triple *phi;
16550 unsigned orig_id;
16551 int alive;
16552};
16553
16554static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16555{
16556 struct triple **slot;
16557 int zrhs, i;
16558 if (live[phi->id].alive) {
16559 return;
16560 }
16561 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016562 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016563 slot = &RHS(phi, 0);
16564 for(i = 0; i < zrhs; i++) {
16565 struct triple *used;
16566 used = slot[i];
16567 if (used && (used->op == OP_PHI)) {
16568 keep_phi(state, live, used);
16569 }
16570 }
16571}
16572
16573static void prune_unused_phis(struct compile_state *state)
16574{
16575 struct triple *first, *phi;
16576 struct phi_triple *live;
16577 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016578
Eric Biederman66fe2222003-07-04 15:14:04 +000016579 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016580 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016581
16582 /* Count how many phi functions I need to process */
16583 phis = 0;
16584 for(phi = first->next; phi != first; phi = phi->next) {
16585 if (phi->op == OP_PHI) {
16586 phis += 1;
16587 }
16588 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016589
Eric Biederman66fe2222003-07-04 15:14:04 +000016590 /* Mark them all dead */
16591 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16592 phis = 0;
16593 for(phi = first->next; phi != first; phi = phi->next) {
16594 if (phi->op != OP_PHI) {
16595 continue;
16596 }
16597 live[phis].alive = 0;
16598 live[phis].orig_id = phi->id;
16599 live[phis].phi = phi;
16600 phi->id = phis;
16601 phis += 1;
16602 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016603
Eric Biederman66fe2222003-07-04 15:14:04 +000016604 /* Mark phis alive that are used by non phis */
16605 for(i = 0; i < phis; i++) {
16606 struct triple_set *set;
16607 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16608 if (set->member->op != OP_PHI) {
16609 keep_phi(state, live, live[i].phi);
16610 break;
16611 }
16612 }
16613 }
16614
16615 /* Delete the extraneous phis */
16616 for(i = 0; i < phis; i++) {
16617 struct triple **slot;
16618 int zrhs, j;
16619 if (!live[i].alive) {
16620 release_triple(state, live[i].phi);
16621 continue;
16622 }
16623 phi = live[i].phi;
16624 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016625 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016626 for(j = 0; j < zrhs; j++) {
16627 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016628 struct triple *unknown;
16629 get_occurance(phi->occurance);
16630 unknown = flatten(state, state->global_pool,
16631 alloc_triple(state, OP_UNKNOWNVAL,
16632 phi->type, 0, 0, phi->occurance));
16633 slot[j] = unknown;
16634 use_triple(unknown, phi);
16635 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016636#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016637 warning(state, phi, "variable not set at index %d on all paths to use", j);
16638#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016639 }
16640 }
16641 }
16642 xfree(live);
16643}
16644
Eric Biedermanb138ac82003-04-22 18:44:01 +000016645static void transform_to_ssa_form(struct compile_state *state)
16646{
16647 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016648 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016649
Eric Biederman90089602004-05-28 14:11:54 +000016650 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016651 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016652
Eric Biederman90089602004-05-28 14:11:54 +000016653 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016654}
16655
16656
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016657static void clear_vertex(
16658 struct compile_state *state, struct block *block, void *arg)
16659{
Eric Biederman83b991a2003-10-11 06:20:25 +000016660 /* Clear the current blocks vertex and the vertex of all
16661 * of the current blocks neighbors in case there are malformed
16662 * blocks with now instructions at this point.
16663 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016664 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016665 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016666 for(edge = block->edges; edge; edge = edge->next) {
16667 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016668 }
16669 for(user = block->use; user; user = user->next) {
16670 user->member->vertex = 0;
16671 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016672}
16673
16674static void mark_live_block(
16675 struct compile_state *state, struct block *block, int *next_vertex)
16676{
16677 /* See if this is a block that has not been marked */
16678 if (block->vertex != 0) {
16679 return;
16680 }
16681 block->vertex = *next_vertex;
16682 *next_vertex += 1;
16683 if (triple_is_branch(state, block->last)) {
16684 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016685 targ = triple_edge_targ(state, block->last, 0);
16686 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016687 if (!*targ) {
16688 continue;
16689 }
16690 if (!triple_stores_block(state, *targ)) {
16691 internal_error(state, 0, "bad targ");
16692 }
16693 mark_live_block(state, (*targ)->u.block, next_vertex);
16694 }
Eric Biederman90089602004-05-28 14:11:54 +000016695 /* Ensure the last block of a function remains alive */
16696 if (triple_is_call(state, block->last)) {
16697 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16698 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016699 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016700 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016701 struct triple *ins;
16702 ins = block->last->next;
16703 if (!triple_stores_block(state, ins)) {
16704 internal_error(state, 0, "bad block start");
16705 }
16706 mark_live_block(state, ins->u.block, next_vertex);
16707 }
16708}
16709
Eric Biedermanb138ac82003-04-22 18:44:01 +000016710static void transform_from_ssa_form(struct compile_state *state)
16711{
16712 /* To get out of ssa form we insert moves on the incoming
16713 * edges to blocks containting phi functions.
16714 */
16715 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016716 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016717 int next_vertex;
16718
16719 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016720 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016721 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016722 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016723
16724 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016725 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016726 for(phi = first->next; phi != first ; phi = next) {
16727 struct block_set *set;
16728 struct block *block;
16729 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016730 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016731 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016732 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016733 next = phi->next;
16734 if (phi->op != OP_PHI) {
16735 continue;
16736 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016737
Eric Biedermanb138ac82003-04-22 18:44:01 +000016738 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016739 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016740
Eric Biederman83b991a2003-10-11 06:20:25 +000016741 /* If this phi is in a dead block just forget it */
16742 if (block->vertex == 0) {
16743 release_triple(state, phi);
16744 continue;
16745 }
16746
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016747 /* Forget uses from code in dead blocks */
16748 for(use = phi->use; use; use = use_next) {
16749 struct block *ublock;
16750 struct triple **expr;
16751 use_next = use->next;
16752 ublock = block_of_triple(state, use->member);
16753 if ((use->member == phi) || (ublock->vertex != 0)) {
16754 continue;
16755 }
16756 expr = triple_rhs(state, use->member, 0);
16757 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16758 if (*expr == phi) {
16759 *expr = 0;
16760 }
16761 }
16762 unuse_triple(phi, use->member);
16763 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016764 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016765 if (registers_of(state, phi->type) != 1) {
16766 internal_error(state, phi, "phi->type does not fit in a single register!");
16767 }
16768 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16769 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016770
Eric Biederman83b991a2003-10-11 06:20:25 +000016771 /* Replaces use of phi with var */
16772 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016773
Eric Biederman90089602004-05-28 14:11:54 +000016774 /* Count the readers */
16775 readers = 0;
16776 for(use = var->use; use; use = use->next) {
16777 if (use->member != MISC(var, 0)) {
16778 readers++;
16779 }
16780 }
16781
Eric Biedermanb138ac82003-04-22 18:44:01 +000016782 /* Walk all of the incoming edges/blocks and insert moves.
16783 */
Eric Biederman90089602004-05-28 14:11:54 +000016784 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016785 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016786 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016787 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016788 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016789 eblock = set->member;
16790 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016791 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016792 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016793 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016794
Eric Biederman83b991a2003-10-11 06:20:25 +000016795 /* If we don't have a value that belongs in an OP_WRITE
16796 * continue on.
16797 */
Eric Biederman90089602004-05-28 14:11:54 +000016798 if (!val || (val == &unknown_triple) || (val == phi)
16799 || (vblock && (vblock->vertex == 0))) {
16800 continue;
16801 }
16802 /* If the value should never occur error */
16803 if (!vblock) {
16804 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016805 continue;
16806 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016807
16808 /* If the value occurs in a dead block see if a replacement
16809 * block can be found.
16810 */
16811 while(eblock && (eblock->vertex == 0)) {
16812 eblock = eblock->idom;
16813 }
16814 /* If not continue on with the next value. */
16815 if (!eblock || (eblock->vertex == 0)) {
16816 continue;
16817 }
16818
16819 /* If we have an empty incoming block ignore it. */
16820 if (!eblock->first) {
16821 internal_error(state, 0, "empty block?");
16822 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016823
Eric Biederman530b5192003-07-01 10:05:30 +000016824 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016825 /* Walk through the edge block backwards to find an
16826 * appropriate location for the OP_WRITE.
16827 */
16828 for(base = eblock->last; base != eblock->first; base = base->prev) {
16829 struct triple **expr;
16830 if (base->op == OP_PIECE) {
16831 base = MISC(base, 0);
16832 }
16833 if ((base == var) || (base == val)) {
16834 goto out;
16835 }
16836 expr = triple_lhs(state, base, 0);
16837 for(; expr; expr = triple_lhs(state, base, expr)) {
16838 if ((*expr) == val) {
16839 goto out;
16840 }
16841 }
16842 expr = triple_rhs(state, base, 0);
16843 for(; expr; expr = triple_rhs(state, base, expr)) {
16844 if ((*expr) == var) {
16845 goto out;
16846 }
16847 }
Eric Biederman530b5192003-07-01 10:05:30 +000016848 }
Eric Biederman90089602004-05-28 14:11:54 +000016849 out:
16850 if (triple_is_branch(state, base)) {
16851 internal_error(state, base,
16852 "Could not insert write to phi");
16853 }
16854 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016855 use_triple(val, move);
16856 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016857 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016858 }
Eric Biederman90089602004-05-28 14:11:54 +000016859 if (!writers && readers) {
16860 internal_error(state, var, "no value written to in use phi?");
16861 }
16862 /* If var is not used free it */
16863 if (!writers) {
16864 release_triple(state, MISC(var, 0));
16865 release_triple(state, var);
16866 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016867 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016868 release_triple(state, phi);
16869 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016870
Eric Biederman83b991a2003-10-11 06:20:25 +000016871 /* Walk all of the operations to find the adecls */
16872 for(var = first->next; var != first ; var = var->next) {
16873 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016874 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016875 continue;
16876 }
16877
16878 /* Walk through all of the rhs uses of var and
16879 * replace them with read of var.
16880 */
16881 for(use = var->use; use; use = use_next) {
16882 struct triple *read, *user;
16883 struct triple **slot;
16884 int zrhs, i, used;
16885 use_next = use->next;
16886 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016887
Eric Biederman83b991a2003-10-11 06:20:25 +000016888 /* Generate a read of var */
16889 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16890 use_triple(var, read);
16891
16892 /* Find the rhs uses and see if they need to be replaced */
16893 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016894 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016895 slot = &RHS(user, 0);
16896 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016897 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016898 slot[i] = read;
16899 used = 1;
16900 }
16901 }
16902 /* If we did use it cleanup the uses */
16903 if (used) {
16904 unuse_triple(var, user);
16905 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016906 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016907 /* If we didn't use it release the extra triple */
16908 else {
16909 release_triple(state, read);
16910 }
16911 }
16912 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016913}
16914
Eric Biederman5ade04a2003-10-22 04:03:46 +000016915#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016916 FILE *fp = state->dbgout; \
16917 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016918 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016919
Eric Biederman83b991a2003-10-11 06:20:25 +000016920static void rebuild_ssa_form(struct compile_state *state)
16921{
16922HI();
16923 transform_from_ssa_form(state);
16924HI();
Eric Biederman90089602004-05-28 14:11:54 +000016925 state->bb.first = state->first;
16926 free_basic_blocks(state, &state->bb);
16927 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016928HI();
16929 insert_phi_operations(state);
16930HI();
16931 rename_variables(state);
16932HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016933
Eric Biederman90089602004-05-28 14:11:54 +000016934 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016935HI();
16936 prune_unused_phis(state);
16937HI();
16938}
16939#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016940
Stefan Reinauer14e22772010-04-27 06:56:47 +000016941/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016942 * Register conflict resolution
16943 * =========================================================
16944 */
16945
16946static struct reg_info find_def_color(
16947 struct compile_state *state, struct triple *def)
16948{
16949 struct triple_set *set;
16950 struct reg_info info;
16951 info.reg = REG_UNSET;
16952 info.regcm = 0;
16953 if (!triple_is_def(state, def)) {
16954 return info;
16955 }
16956 info = arch_reg_lhs(state, def, 0);
16957 if (info.reg >= MAX_REGISTERS) {
16958 info.reg = REG_UNSET;
16959 }
16960 for(set = def->use; set; set = set->next) {
16961 struct reg_info tinfo;
16962 int i;
16963 i = find_rhs_use(state, set->member, def);
16964 if (i < 0) {
16965 continue;
16966 }
16967 tinfo = arch_reg_rhs(state, set->member, i);
16968 if (tinfo.reg >= MAX_REGISTERS) {
16969 tinfo.reg = REG_UNSET;
16970 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016971 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016972 (info.reg != REG_UNSET) &&
16973 (tinfo.reg != info.reg)) {
16974 internal_error(state, def, "register conflict");
16975 }
16976 if ((info.regcm & tinfo.regcm) == 0) {
16977 internal_error(state, def, "regcm conflict %x & %x == 0",
16978 info.regcm, tinfo.regcm);
16979 }
16980 if (info.reg == REG_UNSET) {
16981 info.reg = tinfo.reg;
16982 }
16983 info.regcm &= tinfo.regcm;
16984 }
16985 if (info.reg >= MAX_REGISTERS) {
16986 internal_error(state, def, "register out of range");
16987 }
16988 return info;
16989}
16990
16991static struct reg_info find_lhs_pre_color(
16992 struct compile_state *state, struct triple *ins, int index)
16993{
16994 struct reg_info info;
16995 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016996 zrhs = ins->rhs;
16997 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016998 if (!zlhs && triple_is_def(state, ins)) {
16999 zlhs = 1;
17000 }
17001 if (index >= zlhs) {
17002 internal_error(state, ins, "Bad lhs %d", index);
17003 }
17004 info = arch_reg_lhs(state, ins, index);
17005 for(i = 0; i < zrhs; i++) {
17006 struct reg_info rinfo;
17007 rinfo = arch_reg_rhs(state, ins, i);
17008 if ((info.reg == rinfo.reg) &&
17009 (rinfo.reg >= MAX_REGISTERS)) {
17010 struct reg_info tinfo;
17011 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17012 info.reg = tinfo.reg;
17013 info.regcm &= tinfo.regcm;
17014 break;
17015 }
17016 }
17017 if (info.reg >= MAX_REGISTERS) {
17018 info.reg = REG_UNSET;
17019 }
17020 return info;
17021}
17022
17023static struct reg_info find_rhs_post_color(
17024 struct compile_state *state, struct triple *ins, int index);
17025
17026static struct reg_info find_lhs_post_color(
17027 struct compile_state *state, struct triple *ins, int index)
17028{
17029 struct triple_set *set;
17030 struct reg_info info;
17031 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017032#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017033 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017034 ins, index);
17035#endif
17036 if ((index == 0) && triple_is_def(state, ins)) {
17037 lhs = ins;
17038 }
Eric Biederman90089602004-05-28 14:11:54 +000017039 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017040 lhs = LHS(ins, index);
17041 }
17042 else {
17043 internal_error(state, ins, "Bad lhs %d", index);
17044 lhs = 0;
17045 }
17046 info = arch_reg_lhs(state, ins, index);
17047 if (info.reg >= MAX_REGISTERS) {
17048 info.reg = REG_UNSET;
17049 }
17050 for(set = lhs->use; set; set = set->next) {
17051 struct reg_info rinfo;
17052 struct triple *user;
17053 int zrhs, i;
17054 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017055 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017056 for(i = 0; i < zrhs; i++) {
17057 if (RHS(user, i) != lhs) {
17058 continue;
17059 }
17060 rinfo = find_rhs_post_color(state, user, i);
17061 if ((info.reg != REG_UNSET) &&
17062 (rinfo.reg != REG_UNSET) &&
17063 (info.reg != rinfo.reg)) {
17064 internal_error(state, ins, "register conflict");
17065 }
17066 if ((info.regcm & rinfo.regcm) == 0) {
17067 internal_error(state, ins, "regcm conflict %x & %x == 0",
17068 info.regcm, rinfo.regcm);
17069 }
17070 if (info.reg == REG_UNSET) {
17071 info.reg = rinfo.reg;
17072 }
17073 info.regcm &= rinfo.regcm;
17074 }
17075 }
Eric Biederman530b5192003-07-01 10:05:30 +000017076#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017077 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017078 ins, index, info.reg, info.regcm);
17079#endif
17080 return info;
17081}
17082
17083static struct reg_info find_rhs_post_color(
17084 struct compile_state *state, struct triple *ins, int index)
17085{
17086 struct reg_info info, rinfo;
17087 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017088#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017089 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017090 ins, index);
17091#endif
17092 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017093 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017094 if (!zlhs && triple_is_def(state, ins)) {
17095 zlhs = 1;
17096 }
17097 info = rinfo;
17098 if (info.reg >= MAX_REGISTERS) {
17099 info.reg = REG_UNSET;
17100 }
17101 for(i = 0; i < zlhs; i++) {
17102 struct reg_info linfo;
17103 linfo = arch_reg_lhs(state, ins, i);
17104 if ((linfo.reg == rinfo.reg) &&
17105 (linfo.reg >= MAX_REGISTERS)) {
17106 struct reg_info tinfo;
17107 tinfo = find_lhs_post_color(state, ins, i);
17108 if (tinfo.reg >= MAX_REGISTERS) {
17109 tinfo.reg = REG_UNSET;
17110 }
Eric Biederman530b5192003-07-01 10:05:30 +000017111 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017112 info.regcm &= tinfo.regcm;
17113 if (info.reg != REG_UNSET) {
17114 internal_error(state, ins, "register conflict");
17115 }
17116 if (info.regcm == 0) {
17117 internal_error(state, ins, "regcm conflict");
17118 }
17119 info.reg = tinfo.reg;
17120 }
17121 }
Eric Biederman530b5192003-07-01 10:05:30 +000017122#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017123 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017124 ins, index, info.reg, info.regcm);
17125#endif
17126 return info;
17127}
17128
17129static struct reg_info find_lhs_color(
17130 struct compile_state *state, struct triple *ins, int index)
17131{
17132 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017133#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017134 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017135 ins, index);
17136#endif
17137 pre = find_lhs_pre_color(state, ins, index);
17138 post = find_lhs_post_color(state, ins, index);
17139 if ((pre.reg != post.reg) &&
17140 (pre.reg != REG_UNSET) &&
17141 (post.reg != REG_UNSET)) {
17142 internal_error(state, ins, "register conflict");
17143 }
17144 info.regcm = pre.regcm & post.regcm;
17145 info.reg = pre.reg;
17146 if (info.reg == REG_UNSET) {
17147 info.reg = post.reg;
17148 }
Eric Biederman530b5192003-07-01 10:05:30 +000017149#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017150 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017151 ins, index, info.reg, info.regcm,
17152 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017153#endif
17154 return info;
17155}
17156
17157static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17158{
17159 struct triple_set *entry, *next;
17160 struct triple *out;
17161 struct reg_info info, rinfo;
17162
17163 info = arch_reg_lhs(state, ins, 0);
17164 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17165 use_triple(RHS(out, 0), out);
17166 /* Get the users of ins to use out instead */
17167 for(entry = ins->use; entry; entry = next) {
17168 int i;
17169 next = entry->next;
17170 if (entry->member == out) {
17171 continue;
17172 }
17173 i = find_rhs_use(state, entry->member, ins);
17174 if (i < 0) {
17175 continue;
17176 }
17177 rinfo = arch_reg_rhs(state, entry->member, i);
17178 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17179 continue;
17180 }
17181 replace_rhs_use(state, ins, out, entry->member);
17182 }
17183 transform_to_arch_instruction(state, out);
17184 return out;
17185}
17186
Eric Biedermand1ea5392003-06-28 06:49:45 +000017187static struct triple *typed_pre_copy(
17188 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017189{
17190 /* Carefully insert enough operations so that I can
17191 * enter any operation with a GPR32.
17192 */
17193 struct triple *in;
17194 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017195 unsigned classes;
17196 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017197 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017198 if (ins->op == OP_PHI) {
17199 internal_error(state, ins, "pre_copy on a phi?");
17200 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017201 classes = arch_type_to_regcm(state, type);
17202 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017203 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017204 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017205 FILE *fp = state->errout;
17206 fprintf(fp, "src_type: ");
17207 name_of(fp, ins->type);
17208 fprintf(fp, "\ndst_type: ");
17209 name_of(fp, type);
17210 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017211 internal_error(state, ins, "pre_copy with no register classes");
17212 }
Eric Biederman90089602004-05-28 14:11:54 +000017213 op = OP_COPY;
17214 if (!equiv_types(type, (*expr)->type)) {
17215 op = OP_CONVERT;
17216 }
17217 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017218 unuse_triple(*expr, ins);
17219 *expr = in;
17220 use_triple(RHS(in, 0), in);
17221 use_triple(in, ins);
17222 transform_to_arch_instruction(state, in);
17223 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017224
Eric Biedermand1ea5392003-06-28 06:49:45 +000017225}
17226static struct triple *pre_copy(
17227 struct compile_state *state, struct triple *ins, int index)
17228{
17229 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017230}
17231
17232
Eric Biedermanb138ac82003-04-22 18:44:01 +000017233static void insert_copies_to_phi(struct compile_state *state)
17234{
17235 /* To get out of ssa form we insert moves on the incoming
17236 * edges to blocks containting phi functions.
17237 */
17238 struct triple *first;
17239 struct triple *phi;
17240
17241 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017242 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017243 for(phi = first->next; phi != first ; phi = phi->next) {
17244 struct block_set *set;
17245 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017246 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017247 int edge;
17248 if (phi->op != OP_PHI) {
17249 continue;
17250 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017251 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017252 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017253 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017254 /* Phi's that feed into mandatory live range joins
17255 * cause nasty complications. Insert a copy of
17256 * the phi value so I never have to deal with
17257 * that in the rest of the code.
17258 */
17259 copy = post_copy(state, phi);
17260 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017261 /* Walk all of the incoming edges/blocks and insert moves.
17262 */
17263 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17264 struct block *eblock;
17265 struct triple *move;
17266 struct triple *val;
17267 struct triple *ptr;
17268 eblock = set->member;
17269 val = slot[edge];
17270
17271 if (val == phi) {
17272 continue;
17273 }
17274
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017275 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017276 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017277 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017278 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017279 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017280 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017281
Eric Biedermanb138ac82003-04-22 18:44:01 +000017282 slot[edge] = move;
17283 unuse_triple(val, phi);
17284 use_triple(move, phi);
17285
Eric Biederman66fe2222003-07-04 15:14:04 +000017286 /* Walk up the dominator tree until I have found the appropriate block */
17287 while(eblock && !tdominates(state, val, eblock->last)) {
17288 eblock = eblock->idom;
17289 }
17290 if (!eblock) {
17291 internal_error(state, phi, "Cannot find block dominated by %p",
17292 val);
17293 }
17294
Eric Biedermanb138ac82003-04-22 18:44:01 +000017295 /* Walk through the block backwards to find
17296 * an appropriate location for the OP_COPY.
17297 */
17298 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17299 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017300 if (ptr->op == OP_PIECE) {
17301 ptr = MISC(ptr, 0);
17302 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017303 if ((ptr == phi) || (ptr == val)) {
17304 goto out;
17305 }
Eric Biederman90089602004-05-28 14:11:54 +000017306 expr = triple_lhs(state, ptr, 0);
17307 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17308 if ((*expr) == val) {
17309 goto out;
17310 }
17311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017312 expr = triple_rhs(state, ptr, 0);
17313 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17314 if ((*expr) == phi) {
17315 goto out;
17316 }
17317 }
17318 }
17319 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017320 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017321 internal_error(state, ptr,
17322 "Could not insert write to phi");
17323 }
Eric Biederman90089602004-05-28 14:11:54 +000017324 insert_triple(state, after_lhs(state, ptr), move);
17325 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017326 eblock->last = move;
17327 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017328 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017329 }
17330 }
Eric Biederman90089602004-05-28 14:11:54 +000017331 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017332}
17333
Eric Biederman90089602004-05-28 14:11:54 +000017334struct triple_reg_set;
17335struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017336
Eric Biedermanb138ac82003-04-22 18:44:01 +000017337
Stefan Reinauer14e22772010-04-27 06:56:47 +000017338static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017339 struct triple *member, struct triple *new_member)
17340{
17341 struct triple_reg_set **ptr, *new;
17342 if (!member)
17343 return 0;
17344 ptr = head;
17345 while(*ptr) {
17346 if ((*ptr)->member == member) {
17347 return 0;
17348 }
17349 ptr = &(*ptr)->next;
17350 }
17351 new = xcmalloc(sizeof(*new), "triple_set");
17352 new->member = member;
17353 new->new = new_member;
17354 new->next = *head;
17355 *head = new;
17356 return 1;
17357}
17358
17359static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17360{
17361 struct triple_reg_set *entry, **ptr;
17362 ptr = head;
17363 while(*ptr) {
17364 entry = *ptr;
17365 if (entry->member == member) {
17366 *ptr = entry->next;
17367 xfree(entry);
17368 return;
17369 }
17370 else {
17371 ptr = &entry->next;
17372 }
17373 }
17374}
17375
17376static int in_triple(struct reg_block *rb, struct triple *in)
17377{
17378 return do_triple_set(&rb->in, in, 0);
17379}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017380
17381#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017382static void unin_triple(struct reg_block *rb, struct triple *unin)
17383{
17384 do_triple_unset(&rb->in, unin);
17385}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017386#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017387
17388static int out_triple(struct reg_block *rb, struct triple *out)
17389{
17390 return do_triple_set(&rb->out, out, 0);
17391}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017392#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017393static void unout_triple(struct reg_block *rb, struct triple *unout)
17394{
17395 do_triple_unset(&rb->out, unout);
17396}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017397#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017398
17399static int initialize_regblock(struct reg_block *blocks,
17400 struct block *block, int vertex)
17401{
17402 struct block_set *user;
17403 if (!block || (blocks[block->vertex].block == block)) {
17404 return vertex;
17405 }
17406 vertex += 1;
17407 /* Renumber the blocks in a convinient fashion */
17408 block->vertex = vertex;
17409 blocks[vertex].block = block;
17410 blocks[vertex].vertex = vertex;
17411 for(user = block->use; user; user = user->next) {
17412 vertex = initialize_regblock(blocks, user->member, vertex);
17413 }
17414 return vertex;
17415}
17416
Eric Biederman90089602004-05-28 14:11:54 +000017417static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17418{
17419/* Part to piece is a best attempt and it cannot be correct all by
17420 * itself. If various values are read as different sizes in different
17421 * parts of the code this function cannot work. Or rather it cannot
17422 * work in conjunction with compute_variable_liftimes. As the
17423 * analysis will get confused.
17424 */
17425 struct triple *base;
17426 unsigned reg;
17427 if (!is_lvalue(state, ins)) {
17428 return ins;
17429 }
17430 base = 0;
17431 reg = 0;
17432 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17433 base = MISC(ins, 0);
17434 switch(ins->op) {
17435 case OP_INDEX:
17436 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17437 break;
17438 case OP_DOT:
17439 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17440 break;
17441 default:
17442 internal_error(state, ins, "unhandled part");
17443 break;
17444 }
17445 ins = base;
17446 }
17447 if (base) {
17448 if (reg > base->lhs) {
17449 internal_error(state, base, "part out of range?");
17450 }
17451 ins = LHS(base, reg);
17452 }
17453 return ins;
17454}
17455
Stefan Reinauer14e22772010-04-27 06:56:47 +000017456static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017457 struct triple *ins, struct triple *other)
17458{
17459 if (ins == other) {
17460 return 1;
17461 }
17462 if (ins->op == OP_WRITE) {
17463 ins = part_to_piece(state, MISC(ins, 0));
17464 }
17465 return ins == other;
17466}
17467
Eric Biedermanb138ac82003-04-22 18:44:01 +000017468static int phi_in(struct compile_state *state, struct reg_block *blocks,
17469 struct reg_block *rb, struct block *suc)
17470{
17471 /* Read the conditional input set of a successor block
17472 * (i.e. the input to the phi nodes) and place it in the
17473 * current blocks output set.
17474 */
17475 struct block_set *set;
17476 struct triple *ptr;
17477 int edge;
17478 int done, change;
17479 change = 0;
17480 /* Find the edge I am coming in on */
17481 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17482 if (set->member == rb->block) {
17483 break;
17484 }
17485 }
17486 if (!set) {
17487 internal_error(state, 0, "Not coming on a control edge?");
17488 }
17489 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17490 struct triple **slot, *expr, *ptr2;
17491 int out_change, done2;
17492 done = (ptr == suc->last);
17493 if (ptr->op != OP_PHI) {
17494 continue;
17495 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017496 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017497 expr = slot[edge];
17498 out_change = out_triple(rb, expr);
17499 if (!out_change) {
17500 continue;
17501 }
17502 /* If we don't define the variable also plast it
17503 * in the current blocks input set.
17504 */
17505 ptr2 = rb->block->first;
17506 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017507 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017508 break;
17509 }
17510 done2 = (ptr2 == rb->block->last);
17511 }
17512 if (!done2) {
17513 continue;
17514 }
17515 change |= in_triple(rb, expr);
17516 }
17517 return change;
17518}
17519
17520static int reg_in(struct compile_state *state, struct reg_block *blocks,
17521 struct reg_block *rb, struct block *suc)
17522{
17523 struct triple_reg_set *in_set;
17524 int change;
17525 change = 0;
17526 /* Read the input set of a successor block
17527 * and place it in the current blocks output set.
17528 */
17529 in_set = blocks[suc->vertex].in;
17530 for(; in_set; in_set = in_set->next) {
17531 int out_change, done;
17532 struct triple *first, *last, *ptr;
17533 out_change = out_triple(rb, in_set->member);
17534 if (!out_change) {
17535 continue;
17536 }
17537 /* If we don't define the variable also place it
17538 * in the current blocks input set.
17539 */
17540 first = rb->block->first;
17541 last = rb->block->last;
17542 done = 0;
17543 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017544 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017545 break;
17546 }
17547 done = (ptr == last);
17548 }
17549 if (!done) {
17550 continue;
17551 }
17552 change |= in_triple(rb, in_set->member);
17553 }
17554 change |= phi_in(state, blocks, rb, suc);
17555 return change;
17556}
17557
Eric Biedermanb138ac82003-04-22 18:44:01 +000017558static int use_in(struct compile_state *state, struct reg_block *rb)
17559{
17560 /* Find the variables we use but don't define and add
17561 * it to the current blocks input set.
17562 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017563#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017564#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017565#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017566 struct block *block;
17567 struct triple *ptr;
17568 int done;
17569 int change;
17570 block = rb->block;
17571 change = 0;
17572 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17573 struct triple **expr;
17574 done = (ptr == block->first);
17575 /* The variable a phi function uses depends on the
17576 * control flow, and is handled in phi_in, not
17577 * here.
17578 */
17579 if (ptr->op == OP_PHI) {
17580 continue;
17581 }
17582 expr = triple_rhs(state, ptr, 0);
17583 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17584 struct triple *rhs, *test;
17585 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017586 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017587 if (!rhs) {
17588 continue;
17589 }
Eric Biederman90089602004-05-28 14:11:54 +000017590
17591 /* See if rhs is defined in this block.
17592 * A write counts as a definition.
17593 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017594 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17595 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017596 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017597 rhs = 0;
17598 break;
17599 }
17600 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017601 /* If I still have a valid rhs add it to in */
17602 change |= in_triple(rb, rhs);
17603 }
17604 }
17605 return change;
17606}
17607
17608static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017609 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017610{
17611 struct reg_block *blocks;
17612 int change;
17613 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017614 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17615 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017616 do {
17617 int i;
17618 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017619 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017620 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017621 struct reg_block *rb;
17622 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017623 /* Add the all successor's input set to in */
17624 for(edge = rb->block->edges; edge; edge = edge->next) {
17625 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017626 }
17627 /* Add use to in... */
17628 change |= use_in(state, rb);
17629 }
17630 } while(change);
17631 return blocks;
17632}
17633
Stefan Reinauer14e22772010-04-27 06:56:47 +000017634static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017635 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017636{
17637 int i;
17638 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017639 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017640 struct triple_reg_set *entry, *next;
17641 struct reg_block *rb;
17642 rb = &blocks[i];
17643 for(entry = rb->in; entry ; entry = next) {
17644 next = entry->next;
17645 do_triple_unset(&rb->in, entry->member);
17646 }
17647 for(entry = rb->out; entry; entry = next) {
17648 next = entry->next;
17649 do_triple_unset(&rb->out, entry->member);
17650 }
17651 }
17652 xfree(blocks);
17653
17654}
17655
Eric Biedermanf96a8102003-06-16 16:57:34 +000017656typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017657 struct compile_state *state,
17658 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017659 struct reg_block *rb, struct triple *ins, void *arg);
17660
17661static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017662 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017663 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017664{
17665 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017666
Eric Biederman90089602004-05-28 14:11:54 +000017667 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017668 struct triple_reg_set *live;
17669 struct triple_reg_set *entry, *next;
17670 struct triple *ptr, *prev;
17671 struct reg_block *rb;
17672 struct block *block;
17673 int done;
17674
17675 /* Get the blocks */
17676 rb = &blocks[i];
17677 block = rb->block;
17678
17679 /* Copy out into live */
17680 live = 0;
17681 for(entry = rb->out; entry; entry = next) {
17682 next = entry->next;
17683 do_triple_set(&live, entry->member, entry->new);
17684 }
17685 /* Walk through the basic block calculating live */
17686 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017687 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017688
17689 prev = ptr->prev;
17690 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017691
17692 /* Ensure the current definition is in live */
17693 if (triple_is_def(state, ptr)) {
17694 do_triple_set(&live, ptr, 0);
17695 }
17696
17697 /* Inform the callback function of what is
17698 * going on.
17699 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017700 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017701
Eric Biedermanb138ac82003-04-22 18:44:01 +000017702 /* Remove the current definition from live */
17703 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017704
Eric Biedermanb138ac82003-04-22 18:44:01 +000017705 /* Add the current uses to live.
17706 *
17707 * It is safe to skip phi functions because they do
17708 * not have any block local uses, and the block
17709 * output sets already properly account for what
17710 * control flow depedent uses phi functions do have.
17711 */
17712 if (ptr->op == OP_PHI) {
17713 continue;
17714 }
17715 expr = triple_rhs(state, ptr, 0);
17716 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17717 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017718 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017719 continue;
17720 }
17721 do_triple_set(&live, *expr, 0);
17722 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017723 }
17724 /* Free live */
17725 for(entry = live; entry; entry = next) {
17726 next = entry->next;
17727 do_triple_unset(&live, entry->member);
17728 }
17729 }
17730}
17731
Eric Biederman90089602004-05-28 14:11:54 +000017732struct print_live_variable_info {
17733 struct reg_block *rb;
17734 FILE *fp;
17735};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017736#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017737static void print_live_variables_block(
17738 struct compile_state *state, struct block *block, void *arg)
17739
17740{
17741 struct print_live_variable_info *info = arg;
17742 struct block_set *edge;
17743 FILE *fp = info->fp;
17744 struct reg_block *rb;
17745 struct triple *ptr;
17746 int phi_present;
17747 int done;
17748 rb = &info->rb[block->vertex];
17749
17750 fprintf(fp, "\nblock: %p (%d),",
17751 block, block->vertex);
17752 for(edge = block->edges; edge; edge = edge->next) {
17753 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017754 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017755 edge->member && edge->member->use?edge->member->use->member : 0);
17756 }
17757 fprintf(fp, "\n");
17758 if (rb->in) {
17759 struct triple_reg_set *in_set;
17760 fprintf(fp, " in:");
17761 for(in_set = rb->in; in_set; in_set = in_set->next) {
17762 fprintf(fp, " %-10p", in_set->member);
17763 }
17764 fprintf(fp, "\n");
17765 }
17766 phi_present = 0;
17767 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17768 done = (ptr == block->last);
17769 if (ptr->op == OP_PHI) {
17770 phi_present = 1;
17771 break;
17772 }
17773 }
17774 if (phi_present) {
17775 int edge;
17776 for(edge = 0; edge < block->users; edge++) {
17777 fprintf(fp, " in(%d):", edge);
17778 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17779 struct triple **slot;
17780 done = (ptr == block->last);
17781 if (ptr->op != OP_PHI) {
17782 continue;
17783 }
17784 slot = &RHS(ptr, 0);
17785 fprintf(fp, " %-10p", slot[edge]);
17786 }
17787 fprintf(fp, "\n");
17788 }
17789 }
17790 if (block->first->op == OP_LABEL) {
17791 fprintf(fp, "%p:\n", block->first);
17792 }
17793 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17794 done = (ptr == block->last);
17795 display_triple(fp, ptr);
17796 }
17797 if (rb->out) {
17798 struct triple_reg_set *out_set;
17799 fprintf(fp, " out:");
17800 for(out_set = rb->out; out_set; out_set = out_set->next) {
17801 fprintf(fp, " %-10p", out_set->member);
17802 }
17803 fprintf(fp, "\n");
17804 }
17805 fprintf(fp, "\n");
17806}
17807
Stefan Reinauer14e22772010-04-27 06:56:47 +000017808static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017809 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17810{
17811 struct print_live_variable_info info;
17812 info.rb = rb;
17813 info.fp = fp;
17814 fprintf(fp, "\nlive variables by block\n");
17815 walk_blocks(state, bb, print_live_variables_block, &info);
17816
17817}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017818#endif
Eric Biederman90089602004-05-28 14:11:54 +000017819
Eric Biedermanb138ac82003-04-22 18:44:01 +000017820static int count_triples(struct compile_state *state)
17821{
17822 struct triple *first, *ins;
17823 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017824 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017825 ins = first;
17826 do {
17827 triples++;
17828 ins = ins->next;
17829 } while (ins != first);
17830 return triples;
17831}
Eric Biederman66fe2222003-07-04 15:14:04 +000017832
17833
Eric Biedermanb138ac82003-04-22 18:44:01 +000017834struct dead_triple {
17835 struct triple *triple;
17836 struct dead_triple *work_next;
17837 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017838 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017839 int flags;
17840#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017841#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017842};
17843
Stefan Reinauer14e22772010-04-27 06:56:47 +000017844static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017845 struct dead_triple *dtriple)
17846{
17847 struct triple *first, *ins;
17848 struct dead_triple *dt;
17849 FILE *fp;
17850 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17851 return;
17852 }
17853 fp = state->dbgout;
17854 fprintf(fp, "--------------- dtriples ---------------\n");
17855 first = state->first;
17856 ins = first;
17857 do {
17858 dt = &dtriple[ins->id];
17859 if ((ins->op == OP_LABEL) && (ins->use)) {
17860 fprintf(fp, "\n%p:\n", ins);
17861 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017862 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017863 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17864 display_triple(fp, ins);
17865 if (triple_is_branch(state, ins)) {
17866 fprintf(fp, "\n");
17867 }
17868 ins = ins->next;
17869 } while(ins != first);
17870 fprintf(fp, "\n");
17871}
17872
Eric Biedermanb138ac82003-04-22 18:44:01 +000017873
17874static void awaken(
17875 struct compile_state *state,
17876 struct dead_triple *dtriple, struct triple **expr,
17877 struct dead_triple ***work_list_tail)
17878{
17879 struct triple *triple;
17880 struct dead_triple *dt;
17881 if (!expr) {
17882 return;
17883 }
17884 triple = *expr;
17885 if (!triple) {
17886 return;
17887 }
17888 if (triple->id <= 0) {
17889 internal_error(state, triple, "bad triple id: %d",
17890 triple->id);
17891 }
17892 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017893 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017894 return;
17895 }
17896 dt = &dtriple[triple->id];
17897 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17898 dt->flags |= TRIPLE_FLAG_ALIVE;
17899 if (!dt->work_next) {
17900 **work_list_tail = dt;
17901 *work_list_tail = &dt->work_next;
17902 }
17903 }
17904}
17905
17906static void eliminate_inefectual_code(struct compile_state *state)
17907{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017908 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17909 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017910 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017911
Eric Biederman5ade04a2003-10-22 04:03:46 +000017912 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17913 return;
17914 }
17915
Eric Biedermanb138ac82003-04-22 18:44:01 +000017916 /* Setup the work list */
17917 work_list = 0;
17918 work_list_tail = &work_list;
17919
Eric Biederman83b991a2003-10-11 06:20:25 +000017920 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017921
17922 /* Count how many triples I have */
17923 triples = count_triples(state);
17924
17925 /* Now put then in an array and mark all of the triples dead */
17926 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017927
Eric Biedermanb138ac82003-04-22 18:44:01 +000017928 ins = first;
17929 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017930 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017931 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017932 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017933 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017934 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017935 ins->id = i;
17936 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017937 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017938 awaken(state, dtriple, &ins, &work_list_tail);
17939 }
17940 i++;
17941 ins = ins->next;
17942 } while(ins != first);
17943 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017944 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017945 struct dead_triple *dt;
17946 struct block_set *user;
17947 struct triple **expr;
17948 dt = work_list;
17949 work_list = dt->work_next;
17950 if (!work_list) {
17951 work_list_tail = &work_list;
17952 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017953 /* Make certain the block the current instruction is in lives */
17954 block = block_of_triple(state, dt->triple);
17955 awaken(state, dtriple, &block->first, &work_list_tail);
17956 if (triple_is_branch(state, block->last)) {
17957 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017958 } else {
17959 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017960 }
17961
Eric Biedermanb138ac82003-04-22 18:44:01 +000017962 /* Wake up the data depencencies of this triple */
17963 expr = 0;
17964 do {
17965 expr = triple_rhs(state, dt->triple, expr);
17966 awaken(state, dtriple, expr, &work_list_tail);
17967 } while(expr);
17968 do {
17969 expr = triple_lhs(state, dt->triple, expr);
17970 awaken(state, dtriple, expr, &work_list_tail);
17971 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017972 do {
17973 expr = triple_misc(state, dt->triple, expr);
17974 awaken(state, dtriple, expr, &work_list_tail);
17975 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017976 /* Wake up the forward control dependencies */
17977 do {
17978 expr = triple_targ(state, dt->triple, expr);
17979 awaken(state, dtriple, expr, &work_list_tail);
17980 } while(expr);
17981 /* Wake up the reverse control dependencies of this triple */
17982 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017983 struct triple *last;
17984 last = user->member->last;
17985 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017986#if DEBUG_ROMCC_WARNINGS
17987#warning "Should we bring the awakening noops back?"
17988#endif
17989 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017990 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017991 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017992 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017993 }
17994 }
Eric Biederman90089602004-05-28 14:11:54 +000017995 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017996 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000017997 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000017998 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17999 internal_error(state, dt->triple, "noop effective?");
18000 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018001 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018002 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018003 release_triple(state, dt->triple);
18004 }
18005 }
18006 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018007
18008 rebuild_ssa_form(state);
18009
Eric Biederman90089602004-05-28 14:11:54 +000018010 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018011}
18012
18013
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018014static void insert_mandatory_copies(struct compile_state *state)
18015{
18016 struct triple *ins, *first;
18017
18018 /* The object is with a minimum of inserted copies,
18019 * to resolve in fundamental register conflicts between
18020 * register value producers and consumers.
18021 * Theoretically we may be greater than minimal when we
18022 * are inserting copies before instructions but that
18023 * case should be rare.
18024 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018025 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018026 ins = first;
18027 do {
18028 struct triple_set *entry, *next;
18029 struct triple *tmp;
18030 struct reg_info info;
18031 unsigned reg, regcm;
18032 int do_post_copy, do_pre_copy;
18033 tmp = 0;
18034 if (!triple_is_def(state, ins)) {
18035 goto next;
18036 }
18037 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018038 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018039 if (info.reg >= MAX_REGISTERS) {
18040 info.reg = REG_UNSET;
18041 }
Eric Biederman90089602004-05-28 14:11:54 +000018042
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018043 reg = REG_UNSET;
18044 regcm = arch_type_to_regcm(state, ins->type);
18045 do_post_copy = do_pre_copy = 0;
18046
18047 /* Walk through the uses of ins and check for conflicts */
18048 for(entry = ins->use; entry; entry = next) {
18049 struct reg_info rinfo;
18050 int i;
18051 next = entry->next;
18052 i = find_rhs_use(state, entry->member, ins);
18053 if (i < 0) {
18054 continue;
18055 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018056
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018057 /* Find the users color requirements */
18058 rinfo = arch_reg_rhs(state, entry->member, i);
18059 if (rinfo.reg >= MAX_REGISTERS) {
18060 rinfo.reg = REG_UNSET;
18061 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018062
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018063 /* See if I need a pre_copy */
18064 if (rinfo.reg != REG_UNSET) {
18065 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18066 do_pre_copy = 1;
18067 }
18068 reg = rinfo.reg;
18069 }
18070 regcm &= rinfo.regcm;
18071 regcm = arch_regcm_normalize(state, regcm);
18072 if (regcm == 0) {
18073 do_pre_copy = 1;
18074 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018075 /* Always use pre_copies for constants.
18076 * They do not take up any registers until a
18077 * copy places them in one.
18078 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018079 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018080 (rinfo.reg != REG_UNNEEDED)) {
18081 do_pre_copy = 1;
18082 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018083 }
18084 do_post_copy =
18085 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018086 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018087 (reg != REG_UNSET) &&
18088 (info.reg != reg)) ||
18089 ((info.regcm & regcm) == 0));
18090
18091 reg = info.reg;
18092 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018093 /* 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 +000018094 for(entry = ins->use; entry; entry = next) {
18095 struct reg_info rinfo;
18096 int i;
18097 next = entry->next;
18098 i = find_rhs_use(state, entry->member, ins);
18099 if (i < 0) {
18100 continue;
18101 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018102
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018103 /* Find the users color requirements */
18104 rinfo = arch_reg_rhs(state, entry->member, i);
18105 if (rinfo.reg >= MAX_REGISTERS) {
18106 rinfo.reg = REG_UNSET;
18107 }
18108
18109 /* Now see if it is time to do the pre_copy */
18110 if (rinfo.reg != REG_UNSET) {
18111 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18112 ((regcm & rinfo.regcm) == 0) ||
18113 /* Don't let a mandatory coalesce sneak
18114 * into a operation that is marked to prevent
18115 * coalescing.
18116 */
18117 ((reg != REG_UNNEEDED) &&
18118 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18119 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18120 ) {
18121 if (do_pre_copy) {
18122 struct triple *user;
18123 user = entry->member;
18124 if (RHS(user, i) != ins) {
18125 internal_error(state, user, "bad rhs");
18126 }
18127 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018128 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018129 continue;
18130 } else {
18131 do_post_copy = 1;
18132 }
18133 }
18134 reg = rinfo.reg;
18135 }
18136 if ((regcm & rinfo.regcm) == 0) {
18137 if (do_pre_copy) {
18138 struct triple *user;
18139 user = entry->member;
18140 if (RHS(user, i) != ins) {
18141 internal_error(state, user, "bad rhs");
18142 }
18143 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018144 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018145 continue;
18146 } else {
18147 do_post_copy = 1;
18148 }
18149 }
18150 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018151
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018152 }
18153 if (do_post_copy) {
18154 struct reg_info pre, post;
18155 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018156 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018157 pre = arch_reg_lhs(state, ins, 0);
18158 post = arch_reg_lhs(state, tmp, 0);
18159 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18160 internal_error(state, tmp, "useless copy");
18161 }
18162 }
18163 next:
18164 ins = ins->next;
18165 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018166
Eric Biederman90089602004-05-28 14:11:54 +000018167 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018168}
18169
18170
Eric Biedermanb138ac82003-04-22 18:44:01 +000018171struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018172struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018173struct live_range {
18174 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018175 struct live_range_def *defs;
18176/* Note. The list pointed to by defs is kept in order.
18177 * That is baring splits in the flow control
18178 * defs dominates defs->next wich dominates defs->next->next
18179 * etc.
18180 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018181 unsigned color;
18182 unsigned classes;
18183 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018184 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018185 struct live_range *group_next, **group_prev;
18186};
18187
18188struct live_range_edge {
18189 struct live_range_edge *next;
18190 struct live_range *node;
18191};
18192
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018193struct live_range_def {
18194 struct live_range_def *next;
18195 struct live_range_def *prev;
18196 struct live_range *lr;
18197 struct triple *def;
18198 unsigned orig_id;
18199};
18200
Eric Biedermanb138ac82003-04-22 18:44:01 +000018201#define LRE_HASH_SIZE 2048
18202struct lre_hash {
18203 struct lre_hash *next;
18204 struct live_range *left;
18205 struct live_range *right;
18206};
18207
18208
18209struct reg_state {
18210 struct lre_hash *hash[LRE_HASH_SIZE];
18211 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018212 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018213 struct live_range *lr;
18214 struct live_range *low, **low_tail;
18215 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018216 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018217 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018218 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018219};
18220
18221
Eric Biedermand1ea5392003-06-28 06:49:45 +000018222struct print_interference_block_info {
18223 struct reg_state *rstate;
18224 FILE *fp;
18225 int need_edges;
18226};
18227static void print_interference_block(
18228 struct compile_state *state, struct block *block, void *arg)
18229
18230{
18231 struct print_interference_block_info *info = arg;
18232 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018233 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018234 FILE *fp = info->fp;
18235 struct reg_block *rb;
18236 struct triple *ptr;
18237 int phi_present;
18238 int done;
18239 rb = &rstate->blocks[block->vertex];
18240
Eric Biederman5ade04a2003-10-22 04:03:46 +000018241 fprintf(fp, "\nblock: %p (%d),",
18242 block, block->vertex);
18243 for(edge = block->edges; edge; edge = edge->next) {
18244 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018245 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018246 edge->member && edge->member->use?edge->member->use->member : 0);
18247 }
18248 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018249 if (rb->in) {
18250 struct triple_reg_set *in_set;
18251 fprintf(fp, " in:");
18252 for(in_set = rb->in; in_set; in_set = in_set->next) {
18253 fprintf(fp, " %-10p", in_set->member);
18254 }
18255 fprintf(fp, "\n");
18256 }
18257 phi_present = 0;
18258 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18259 done = (ptr == block->last);
18260 if (ptr->op == OP_PHI) {
18261 phi_present = 1;
18262 break;
18263 }
18264 }
18265 if (phi_present) {
18266 int edge;
18267 for(edge = 0; edge < block->users; edge++) {
18268 fprintf(fp, " in(%d):", edge);
18269 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18270 struct triple **slot;
18271 done = (ptr == block->last);
18272 if (ptr->op != OP_PHI) {
18273 continue;
18274 }
18275 slot = &RHS(ptr, 0);
18276 fprintf(fp, " %-10p", slot[edge]);
18277 }
18278 fprintf(fp, "\n");
18279 }
18280 }
18281 if (block->first->op == OP_LABEL) {
18282 fprintf(fp, "%p:\n", block->first);
18283 }
18284 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018285 struct live_range *lr;
18286 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018287 done = (ptr == block->last);
18288 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018289
Eric Biedermand1ea5392003-06-28 06:49:45 +000018290 id = ptr->id;
18291 ptr->id = rstate->lrd[id].orig_id;
18292 SET_REG(ptr->id, lr->color);
18293 display_triple(fp, ptr);
18294 ptr->id = id;
18295
18296 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18297 internal_error(state, ptr, "lr has no defs!");
18298 }
18299 if (info->need_edges) {
18300 if (lr->defs) {
18301 struct live_range_def *lrd;
18302 fprintf(fp, " range:");
18303 lrd = lr->defs;
18304 do {
18305 fprintf(fp, " %-10p", lrd->def);
18306 lrd = lrd->next;
18307 } while(lrd != lr->defs);
18308 fprintf(fp, "\n");
18309 }
18310 if (lr->edges > 0) {
18311 struct live_range_edge *edge;
18312 fprintf(fp, " edges:");
18313 for(edge = lr->edges; edge; edge = edge->next) {
18314 struct live_range_def *lrd;
18315 lrd = edge->node->defs;
18316 do {
18317 fprintf(fp, " %-10p", lrd->def);
18318 lrd = lrd->next;
18319 } while(lrd != edge->node->defs);
18320 fprintf(fp, "|");
18321 }
18322 fprintf(fp, "\n");
18323 }
18324 }
18325 /* Do a bunch of sanity checks */
18326 valid_ins(state, ptr);
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018327 if (ptr->id > rstate->defs) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018328 internal_error(state, ptr, "Invalid triple id: %d",
18329 ptr->id);
18330 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018331 }
18332 if (rb->out) {
18333 struct triple_reg_set *out_set;
18334 fprintf(fp, " out:");
18335 for(out_set = rb->out; out_set; out_set = out_set->next) {
18336 fprintf(fp, " %-10p", out_set->member);
18337 }
18338 fprintf(fp, "\n");
18339 }
18340 fprintf(fp, "\n");
18341}
18342
18343static void print_interference_blocks(
18344 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18345{
18346 struct print_interference_block_info info;
18347 info.rstate = rstate;
18348 info.fp = fp;
18349 info.need_edges = need_edges;
18350 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018351 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018352
18353}
18354
Eric Biedermanb138ac82003-04-22 18:44:01 +000018355static unsigned regc_max_size(struct compile_state *state, int classes)
18356{
18357 unsigned max_size;
18358 int i;
18359 max_size = 0;
18360 for(i = 0; i < MAX_REGC; i++) {
18361 if (classes & (1 << i)) {
18362 unsigned size;
18363 size = arch_regc_size(state, i);
18364 if (size > max_size) {
18365 max_size = size;
18366 }
18367 }
18368 }
18369 return max_size;
18370}
18371
18372static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18373{
18374 unsigned equivs[MAX_REG_EQUIVS];
18375 int i;
18376 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18377 internal_error(state, 0, "invalid register");
18378 }
18379 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18380 internal_error(state, 0, "invalid register");
18381 }
18382 arch_reg_equivs(state, equivs, reg1);
18383 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18384 if (equivs[i] == reg2) {
18385 return 1;
18386 }
18387 }
18388 return 0;
18389}
18390
18391static void reg_fill_used(struct compile_state *state, char *used, int reg)
18392{
18393 unsigned equivs[MAX_REG_EQUIVS];
18394 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018395 if (reg == REG_UNNEEDED) {
18396 return;
18397 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018398 arch_reg_equivs(state, equivs, reg);
18399 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18400 used[equivs[i]] = 1;
18401 }
18402 return;
18403}
18404
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018405static void reg_inc_used(struct compile_state *state, char *used, int reg)
18406{
18407 unsigned equivs[MAX_REG_EQUIVS];
18408 int i;
18409 if (reg == REG_UNNEEDED) {
18410 return;
18411 }
18412 arch_reg_equivs(state, equivs, reg);
18413 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18414 used[equivs[i]] += 1;
18415 }
18416 return;
18417}
18418
Eric Biedermanb138ac82003-04-22 18:44:01 +000018419static unsigned int hash_live_edge(
18420 struct live_range *left, struct live_range *right)
18421{
18422 unsigned int hash, val;
18423 unsigned long lval, rval;
18424 lval = ((unsigned long)left)/sizeof(struct live_range);
18425 rval = ((unsigned long)right)/sizeof(struct live_range);
18426 hash = 0;
18427 while(lval) {
18428 val = lval & 0xff;
18429 lval >>= 8;
18430 hash = (hash *263) + val;
18431 }
18432 while(rval) {
18433 val = rval & 0xff;
18434 rval >>= 8;
18435 hash = (hash *263) + val;
18436 }
18437 hash = hash & (LRE_HASH_SIZE - 1);
18438 return hash;
18439}
18440
18441static struct lre_hash **lre_probe(struct reg_state *rstate,
18442 struct live_range *left, struct live_range *right)
18443{
18444 struct lre_hash **ptr;
18445 unsigned int index;
18446 /* Ensure left <= right */
18447 if (left > right) {
18448 struct live_range *tmp;
18449 tmp = left;
18450 left = right;
18451 right = tmp;
18452 }
18453 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018454
Eric Biedermanb138ac82003-04-22 18:44:01 +000018455 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018456 while(*ptr) {
18457 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18458 break;
18459 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018460 ptr = &(*ptr)->next;
18461 }
18462 return ptr;
18463}
18464
18465static int interfere(struct reg_state *rstate,
18466 struct live_range *left, struct live_range *right)
18467{
18468 struct lre_hash **ptr;
18469 ptr = lre_probe(rstate, left, right);
18470 return ptr && *ptr;
18471}
18472
Stefan Reinauer14e22772010-04-27 06:56:47 +000018473static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018474 struct live_range *left, struct live_range *right)
18475{
18476 /* FIXME the memory allocation overhead is noticeable here... */
18477 struct lre_hash **ptr, *new_hash;
18478 struct live_range_edge *edge;
18479
18480 if (left == right) {
18481 return;
18482 }
18483 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18484 return;
18485 }
18486 /* Ensure left <= right */
18487 if (left > right) {
18488 struct live_range *tmp;
18489 tmp = left;
18490 left = right;
18491 right = tmp;
18492 }
18493 ptr = lre_probe(rstate, left, right);
18494 if (*ptr) {
18495 return;
18496 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018497#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018498 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018499 left, right);
18500#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018501 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18502 new_hash->next = *ptr;
18503 new_hash->left = left;
18504 new_hash->right = right;
18505 *ptr = new_hash;
18506
18507 edge = xmalloc(sizeof(*edge), "live_range_edge");
18508 edge->next = left->edges;
18509 edge->node = right;
18510 left->edges = edge;
18511 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018512
Eric Biedermanb138ac82003-04-22 18:44:01 +000018513 edge = xmalloc(sizeof(*edge), "live_range_edge");
18514 edge->next = right->edges;
18515 edge->node = left;
18516 right->edges = edge;
18517 right->degree += 1;
18518}
18519
18520static void remove_live_edge(struct reg_state *rstate,
18521 struct live_range *left, struct live_range *right)
18522{
18523 struct live_range_edge *edge, **ptr;
18524 struct lre_hash **hptr, *entry;
18525 hptr = lre_probe(rstate, left, right);
18526 if (!hptr || !*hptr) {
18527 return;
18528 }
18529 entry = *hptr;
18530 *hptr = entry->next;
18531 xfree(entry);
18532
18533 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18534 edge = *ptr;
18535 if (edge->node == right) {
18536 *ptr = edge->next;
18537 memset(edge, 0, sizeof(*edge));
18538 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018539 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018540 break;
18541 }
18542 }
18543 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18544 edge = *ptr;
18545 if (edge->node == left) {
18546 *ptr = edge->next;
18547 memset(edge, 0, sizeof(*edge));
18548 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018549 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018550 break;
18551 }
18552 }
18553}
18554
18555static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18556{
18557 struct live_range_edge *edge, *next;
18558 for(edge = range->edges; edge; edge = next) {
18559 next = edge->next;
18560 remove_live_edge(rstate, range, edge->node);
18561 }
18562}
18563
Stefan Reinauer14e22772010-04-27 06:56:47 +000018564static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018565 struct live_range *dest, struct live_range *src)
18566{
18567 struct live_range_edge *edge, *next;
18568 for(edge = src->edges; edge; edge = next) {
18569 struct live_range *other;
18570 next = edge->next;
18571 other = edge->node;
18572 remove_live_edge(rstate, src, other);
18573 add_live_edge(rstate, dest, other);
18574 }
18575}
18576
Eric Biedermanb138ac82003-04-22 18:44:01 +000018577
18578/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018579 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018580 * new(n) --- Return a graph with n nodes but no edges.
18581 * add(g,x,y) --- Return a graph including g with an between x and y
18582 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18583 * x and y in the graph g
18584 * degree(g, x) --- Return the degree of the node x in the graph g
18585 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18586 *
18587 * Implement with a hash table && a set of adjcency vectors.
18588 * The hash table supports constant time implementations of add and interfere.
18589 * The adjacency vectors support an efficient implementation of neighbors.
18590 */
18591
Stefan Reinauer14e22772010-04-27 06:56:47 +000018592/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018593 * +---------------------------------------------------+
18594 * | +--------------+ |
18595 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018596 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018597 *
18598 * -- In simplify implment optimistic coloring... (No backtracking)
18599 * -- Implement Rematerialization it is the only form of spilling we can perform
18600 * Essentially this means dropping a constant from a register because
18601 * we can regenerate it later.
18602 *
18603 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18604 * coalesce at phi points...
18605 * --- Bias coloring if at all possible do the coalesing a compile time.
18606 *
18607 *
18608 */
18609
Stefan Reinauer50542a82007-10-24 11:14:14 +000018610#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018611static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018612 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018613 struct triple *parent, struct triple *ins)
18614{
18615 struct live_range *lr;
18616 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018617 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018618 expr = triple_rhs(state, ins, 0);
18619 for(;expr; expr = triple_rhs(state, ins, expr)) {
18620 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018621 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018622 continue;
18623 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018624 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018625 if (lr->color == lr2->color) {
18626 internal_error(state, ins, "live range too big");
18627 }
18628 }
18629}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018630#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018631
18632static struct live_range *coalesce_ranges(
18633 struct compile_state *state, struct reg_state *rstate,
18634 struct live_range *lr1, struct live_range *lr2)
18635{
18636 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18637 unsigned color;
18638 unsigned classes;
18639 if (lr1 == lr2) {
18640 return lr1;
18641 }
18642 if (!lr1->defs || !lr2->defs) {
18643 internal_error(state, 0,
18644 "cannot coalese dead live ranges");
18645 }
18646 if ((lr1->color == REG_UNNEEDED) ||
18647 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018648 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018649 "cannot coalesce live ranges without a possible color");
18650 }
18651 if ((lr1->color != lr2->color) &&
18652 (lr1->color != REG_UNSET) &&
18653 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018654 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018655 "cannot coalesce live ranges of different colors");
18656 }
18657 color = lr1->color;
18658 if (color == REG_UNSET) {
18659 color = lr2->color;
18660 }
18661 classes = lr1->classes & lr2->classes;
18662 if (!classes) {
18663 internal_error(state, lr1->defs->def,
18664 "cannot coalesce live ranges with dissimilar register classes");
18665 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018666 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018667 FILE *fp = state->errout;
18668 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018669 lrd = lr1->defs;
18670 do {
Eric Biederman90089602004-05-28 14:11:54 +000018671 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018672 lrd = lrd->next;
18673 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018674 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018675 lrd = lr2->defs;
18676 do {
Eric Biederman90089602004-05-28 14:11:54 +000018677 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018678 lrd = lrd->next;
18679 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018680 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018681 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018682 /* If there is a clear dominate live range put it in lr1,
18683 * For purposes of this test phi functions are
18684 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018685 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018686 */
18687 if ((lr1->defs->prev->def->op == OP_PHI) ||
18688 ((lr2->defs->prev->def->op != OP_PHI) &&
18689 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18690 struct live_range *tmp;
18691 tmp = lr1;
18692 lr1 = lr2;
18693 lr2 = tmp;
18694 }
18695#if 0
18696 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018697 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018698 }
18699 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018700 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018701 }
18702 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018703 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018704 }
18705 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018706 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018707 }
18708#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018709#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018710 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018711 lr1->defs->def,
18712 lr1->color,
18713 lr2->defs->def,
18714 lr2->color);
18715#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018716
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018717 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018718#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018719#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018720#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018721 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018722 * and to the necessity of making multiple passes of coalesce_live_ranges.
18723 * A failure to find some coalesce opportunities in coaleace_live_ranges
18724 * does not impact the correct of the compiler just the efficiency with
18725 * which registers are allocated.
18726 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018727 head = lr1->defs;
18728 mid1 = lr1->defs->prev;
18729 mid2 = lr2->defs;
18730 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018731
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018732 head->prev = end;
18733 end->next = head;
18734
18735 mid1->next = mid2;
18736 mid2->prev = mid1;
18737
18738 /* Fixup the live range in the added live range defs */
18739 lrd = head;
18740 do {
18741 lrd->lr = lr1;
18742 lrd = lrd->next;
18743 } while(lrd != head);
18744
18745 /* Mark lr2 as free. */
18746 lr2->defs = 0;
18747 lr2->color = REG_UNNEEDED;
18748 lr2->classes = 0;
18749
18750 if (!lr1->defs) {
18751 internal_error(state, 0, "lr1->defs == 0 ?");
18752 }
18753
18754 lr1->color = color;
18755 lr1->classes = classes;
18756
Eric Biederman153ea352003-06-20 14:43:20 +000018757 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18758 transfer_live_edges(rstate, lr1, lr2);
18759
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018760 return lr1;
18761}
18762
18763static struct live_range_def *live_range_head(
18764 struct compile_state *state, struct live_range *lr,
18765 struct live_range_def *last)
18766{
18767 struct live_range_def *result;
18768 result = 0;
18769 if (last == 0) {
18770 result = lr->defs;
18771 }
18772 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18773 result = last->next;
18774 }
18775 return result;
18776}
18777
18778static struct live_range_def *live_range_end(
18779 struct compile_state *state, struct live_range *lr,
18780 struct live_range_def *last)
18781{
18782 struct live_range_def *result;
18783 result = 0;
18784 if (last == 0) {
18785 result = lr->defs->prev;
18786 }
18787 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18788 result = last->prev;
18789 }
18790 return result;
18791}
18792
18793
Eric Biedermanb138ac82003-04-22 18:44:01 +000018794static void initialize_live_ranges(
18795 struct compile_state *state, struct reg_state *rstate)
18796{
18797 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018798 size_t count, size;
18799 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018800
Eric Biederman83b991a2003-10-11 06:20:25 +000018801 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018802 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018803 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018804 count = count_triples(state);
18805 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018806 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018807 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018808 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018809 /* Potentially I need one live range for each instruction
18810 * plus an extra for the dummy live range.
18811 */
18812 rstate->ranges = count + 1;
18813 size = sizeof(rstate->lrd[0]) * rstate->defs;
18814 rstate->lrd = xcmalloc(size, "live_range_def");
18815 size = sizeof(rstate->lr[0]) * rstate->ranges;
18816 rstate->lr = xcmalloc(size, "live_range");
18817
Eric Biedermanb138ac82003-04-22 18:44:01 +000018818 /* Setup the dummy live range */
18819 rstate->lr[0].classes = 0;
18820 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018821 rstate->lr[0].defs = 0;
18822 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018823 ins = first;
18824 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018825 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018826 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018827 struct reg_info info;
18828 /* Find the architecture specific color information */
18829 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018830 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018831 rstate->lr[i].defs = &rstate->lrd[j];
18832 rstate->lr[i].color = info.reg;
18833 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018834 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018835 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018836 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018837 /* Otherwise give the triple the dummy live range. */
18838 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018839 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018840 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018841
18842 /* Initalize the live_range_def */
18843 rstate->lrd[j].next = &rstate->lrd[j];
18844 rstate->lrd[j].prev = &rstate->lrd[j];
18845 rstate->lrd[j].def = ins;
18846 rstate->lrd[j].orig_id = ins->id;
18847 ins->id = j;
18848
18849 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018850 ins = ins->next;
18851 } while(ins != first);
18852 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018853
Eric Biedermanb138ac82003-04-22 18:44:01 +000018854 /* Make a second pass to handle achitecture specific register
18855 * constraints.
18856 */
18857 ins = first;
18858 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018859 int zlhs, zrhs, i, j;
18860 if (ins->id > rstate->defs) {
18861 internal_error(state, ins, "bad id");
18862 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018863
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018864 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018865 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018866 if ((zlhs == 0) && triple_is_def(state, ins)) {
18867 zlhs = 1;
18868 }
Eric Biederman90089602004-05-28 14:11:54 +000018869 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018870
Eric Biederman5ade04a2003-10-22 04:03:46 +000018871 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018872 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018873 ins, zlhs, zrhs);
18874 }
18875
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018876 for(i = 0; i < zlhs; i++) {
18877 struct reg_info linfo;
18878 struct live_range_def *lhs;
18879 linfo = arch_reg_lhs(state, ins, i);
18880 if (linfo.reg < MAX_REGISTERS) {
18881 continue;
18882 }
18883 if (triple_is_def(state, ins)) {
18884 lhs = &rstate->lrd[ins->id];
18885 } else {
18886 lhs = &rstate->lrd[LHS(ins, i)->id];
18887 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018888
18889 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018890 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018891 i, lhs, linfo.reg);
18892 }
18893
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018894 for(j = 0; j < zrhs; j++) {
18895 struct reg_info rinfo;
18896 struct live_range_def *rhs;
18897 rinfo = arch_reg_rhs(state, ins, j);
18898 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018899 continue;
18900 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018901 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018902
18903 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018904 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018905 j, rhs, rinfo.reg);
18906 }
18907
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018908 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018909 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018910 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018911 }
18912 }
18913 }
18914 ins = ins->next;
18915 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018916}
18917
Eric Biedermanf96a8102003-06-16 16:57:34 +000018918static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018919 struct compile_state *state,
18920 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018921 struct reg_block *rb, struct triple *ins, void *arg)
18922{
18923 struct reg_state *rstate = arg;
18924 struct live_range *def;
18925 struct triple_reg_set *entry;
18926
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018927 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018928 * we do not have a definition to add to
18929 * the interference graph.
18930 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018931 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018932 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018933 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018934 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018935
Eric Biedermanb138ac82003-04-22 18:44:01 +000018936 /* Create an edge between ins and everything that is
18937 * alive, unless the live_range cannot share
18938 * a physical register with ins.
18939 */
18940 for(entry = live; entry; entry = entry->next) {
18941 struct live_range *lr;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018942 if (entry->member->id > rstate->defs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018943 internal_error(state, 0, "bad entry?");
18944 }
18945 lr = rstate->lrd[entry->member->id].lr;
18946 if (def == lr) {
18947 continue;
18948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018949 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18950 continue;
18951 }
18952 add_live_edge(rstate, def, lr);
18953 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018954 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018955}
18956
Stefan Reinauer50542a82007-10-24 11:14:14 +000018957#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018958static struct live_range *get_verify_live_range(
18959 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18960{
18961 struct live_range *lr;
18962 struct live_range_def *lrd;
18963 int ins_found;
18964 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18965 internal_error(state, ins, "bad ins?");
18966 }
18967 lr = rstate->lrd[ins->id].lr;
18968 ins_found = 0;
18969 lrd = lr->defs;
18970 do {
18971 if (lrd->def == ins) {
18972 ins_found = 1;
18973 }
18974 lrd = lrd->next;
18975 } while(lrd != lr->defs);
18976 if (!ins_found) {
18977 internal_error(state, ins, "ins not in live range");
18978 }
18979 return lr;
18980}
18981
18982static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018983 struct compile_state *state,
18984 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018985 struct reg_block *rb, struct triple *ins, void *arg)
18986{
18987 struct reg_state *rstate = arg;
18988 struct triple_reg_set *entry1, *entry2;
18989
18990
18991 /* Compare live against edges and make certain the code is working */
18992 for(entry1 = live; entry1; entry1 = entry1->next) {
18993 struct live_range *lr1;
18994 lr1 = get_verify_live_range(state, rstate, entry1->member);
18995 for(entry2 = live; entry2; entry2 = entry2->next) {
18996 struct live_range *lr2;
18997 struct live_range_edge *edge2;
18998 int lr1_found;
18999 int lr2_degree;
19000 if (entry2 == entry1) {
19001 continue;
19002 }
19003 lr2 = get_verify_live_range(state, rstate, entry2->member);
19004 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019005 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019006 "live range with 2 values simultaneously alive");
19007 }
19008 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19009 continue;
19010 }
19011 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019012 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019013 "edges don't interfere?");
19014 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019015
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019016 lr1_found = 0;
19017 lr2_degree = 0;
19018 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19019 lr2_degree++;
19020 if (edge2->node == lr1) {
19021 lr1_found = 1;
19022 }
19023 }
19024 if (lr2_degree != lr2->degree) {
19025 internal_error(state, entry2->member,
19026 "computed degree: %d does not match reported degree: %d\n",
19027 lr2_degree, lr2->degree);
19028 }
19029 if (!lr1_found) {
19030 internal_error(state, entry2->member, "missing edge");
19031 }
19032 }
19033 }
19034 return;
19035}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019036#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019037
Eric Biedermanf96a8102003-06-16 16:57:34 +000019038static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019039 struct compile_state *state,
19040 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019041 struct reg_block *rb, struct triple *ins, void *arg)
19042{
19043 struct reg_state *rstate = arg;
19044 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019045 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019046 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019047
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019048 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019049 id = ins->id;
19050 ins->id = rstate->lrd[id].orig_id;
19051 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019052 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019053 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019054
19055 if (lr->defs) {
19056 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019057 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019058 lrd = lr->defs;
19059 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019060 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019061 lrd = lrd->next;
19062 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019063 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019064 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019065 if (live) {
19066 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019067 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019068 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019069 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019070 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019071 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019072 }
19073 if (lr->edges) {
19074 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019075 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019076 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019077 struct live_range_def *lrd;
19078 lrd = entry->node->defs;
19079 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019080 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019081 lrd = lrd->next;
19082 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019083 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019084 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019085 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019086 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019087 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019088 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019089 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019090 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019091}
19092
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019093static int coalesce_live_ranges(
19094 struct compile_state *state, struct reg_state *rstate)
19095{
19096 /* At the point where a value is moved from one
19097 * register to another that value requires two
19098 * registers, thus increasing register pressure.
19099 * Live range coaleescing reduces the register
19100 * pressure by keeping a value in one register
19101 * longer.
19102 *
19103 * In the case of a phi function all paths leading
19104 * into it must be allocated to the same register
19105 * otherwise the phi function may not be removed.
19106 *
19107 * Forcing a value to stay in a single register
19108 * for an extended period of time does have
19109 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019110 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019111 *
19112 * The two cases I have identified are:
19113 * 1) Two forced register assignments may
19114 * collide.
19115 * 2) Registers may go unused because they
19116 * are only good for storing the value
19117 * and not manipulating it.
19118 *
19119 * Because of this I need to split live ranges,
19120 * even outside of the context of coalesced live
19121 * ranges. The need to split live ranges does
19122 * impose some constraints on live range coalescing.
19123 *
19124 * - Live ranges may not be coalesced across phi
19125 * functions. This creates a 2 headed live
19126 * range that cannot be sanely split.
19127 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019128 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019129 * are handled as pre split live ranges so we will
19130 * never attempt to split them.
19131 */
19132 int coalesced;
19133 int i;
19134
19135 coalesced = 0;
19136 for(i = 0; i <= rstate->ranges; i++) {
19137 struct live_range *lr1;
19138 struct live_range_def *lrd1;
19139 lr1 = &rstate->lr[i];
19140 if (!lr1->defs) {
19141 continue;
19142 }
19143 lrd1 = live_range_end(state, lr1, 0);
19144 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19145 struct triple_set *set;
19146 if (lrd1->def->op != OP_COPY) {
19147 continue;
19148 }
19149 /* Skip copies that are the result of a live range split. */
19150 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19151 continue;
19152 }
19153 for(set = lrd1->def->use; set; set = set->next) {
19154 struct live_range_def *lrd2;
19155 struct live_range *lr2, *res;
19156
19157 lrd2 = &rstate->lrd[set->member->id];
19158
19159 /* Don't coalesce with instructions
19160 * that are the result of a live range
19161 * split.
19162 */
19163 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19164 continue;
19165 }
19166 lr2 = rstate->lrd[set->member->id].lr;
19167 if (lr1 == lr2) {
19168 continue;
19169 }
19170 if ((lr1->color != lr2->color) &&
19171 (lr1->color != REG_UNSET) &&
19172 (lr2->color != REG_UNSET)) {
19173 continue;
19174 }
19175 if ((lr1->classes & lr2->classes) == 0) {
19176 continue;
19177 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019178
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019179 if (interfere(rstate, lr1, lr2)) {
19180 continue;
19181 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019182
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019183 res = coalesce_ranges(state, rstate, lr1, lr2);
19184 coalesced += 1;
19185 if (res != lr1) {
19186 goto next;
19187 }
19188 }
19189 }
19190 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019191 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019192 }
19193 return coalesced;
19194}
19195
19196
Eric Biedermanf96a8102003-06-16 16:57:34 +000019197static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019198 struct reg_block *blocks, struct triple_reg_set *live,
19199 struct reg_block *rb, struct triple *ins, void *arg)
19200{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019201 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019202 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019203
19204 /* See if we have a mandatory coalesce operation between
19205 * a lhs and a rhs value. If so and the rhs value is also
19206 * alive then this triple needs to be pre copied. Otherwise
19207 * we would have two definitions in the same live range simultaneously
19208 * alive.
19209 */
Eric Biederman90089602004-05-28 14:11:54 +000019210 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019211 if ((zlhs == 0) && triple_is_def(state, ins)) {
19212 zlhs = 1;
19213 }
Eric Biederman90089602004-05-28 14:11:54 +000019214 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019215 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019216 struct reg_info linfo;
19217 linfo = arch_reg_lhs(state, ins, i);
19218 if (linfo.reg < MAX_REGISTERS) {
19219 continue;
19220 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019221 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019222 struct reg_info rinfo;
19223 struct triple *rhs;
19224 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019225 int found;
19226 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019227 rinfo = arch_reg_rhs(state, ins, j);
19228 if (rinfo.reg != linfo.reg) {
19229 continue;
19230 }
19231 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019232 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019233 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019234 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019235 }
19236 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019237 if (found) {
19238 struct triple *copy;
19239 copy = pre_copy(state, ins, j);
19240 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019241 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019242 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019243 }
19244 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019245 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019246}
19247
Eric Biedermand1ea5392003-06-28 06:49:45 +000019248static int correct_coalesce_conflicts(
19249 struct compile_state *state, struct reg_block *blocks)
19250{
19251 int conflicts;
19252 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019253 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019254 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019255 return conflicts;
19256}
19257
Eric Biedermanf96a8102003-06-16 16:57:34 +000019258static void replace_set_use(struct compile_state *state,
19259 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019260{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019261 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019262 for(set = head; set; set = set->next) {
19263 if (set->member == orig) {
19264 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019265 }
19266 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019267}
19268
Stefan Reinauer14e22772010-04-27 06:56:47 +000019269static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019270 struct reg_block *blocks, struct triple *orig, struct triple *new)
19271{
19272 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019273#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019274#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019275#endif
Eric Biederman90089602004-05-28 14:11:54 +000019276 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019277 struct reg_block *rb;
19278 rb = &blocks[i];
19279 replace_set_use(state, rb->in, orig, new);
19280 replace_set_use(state, rb->out, orig, new);
19281 }
19282}
19283
19284static void color_instructions(struct compile_state *state)
19285{
19286 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019287 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019288 ins = first;
19289 do {
19290 if (triple_is_def(state, ins)) {
19291 struct reg_info info;
19292 info = find_lhs_color(state, ins, 0);
19293 if (info.reg >= MAX_REGISTERS) {
19294 info.reg = REG_UNSET;
19295 }
19296 SET_INFO(ins->id, info);
19297 }
19298 ins = ins->next;
19299 } while(ins != first);
19300}
19301
19302static struct reg_info read_lhs_color(
19303 struct compile_state *state, struct triple *ins, int index)
19304{
19305 struct reg_info info;
19306 if ((index == 0) && triple_is_def(state, ins)) {
19307 info.reg = ID_REG(ins->id);
19308 info.regcm = ID_REGCM(ins->id);
19309 }
Eric Biederman90089602004-05-28 14:11:54 +000019310 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019311 info = read_lhs_color(state, LHS(ins, index), 0);
19312 }
19313 else {
19314 internal_error(state, ins, "Bad lhs %d", index);
19315 info.reg = REG_UNSET;
19316 info.regcm = 0;
19317 }
19318 return info;
19319}
19320
19321static struct triple *resolve_tangle(
19322 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019323{
19324 struct reg_info info, uinfo;
19325 struct triple_set *set, *next;
19326 struct triple *copy;
19327
Stefan Reinauer50542a82007-10-24 11:14:14 +000019328#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019329#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019330#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019331 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019332 for(set = tangle->use; set; set = next) {
19333 struct triple *user;
19334 int i, zrhs;
19335 next = set->next;
19336 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019337 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019338 for(i = 0; i < zrhs; i++) {
19339 if (RHS(user, i) != tangle) {
19340 continue;
19341 }
19342 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019343 if (uinfo.reg == info.reg) {
19344 copy = pre_copy(state, user, i);
19345 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019346 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019347 }
19348 }
19349 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019350 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019351 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019352 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019353 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019354 copy = post_copy(state, tangle);
19355 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019356 linfo = find_lhs_color(state, copy, 0);
19357 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019358 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019359 info = find_lhs_color(state, tangle, 0);
19360 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019361
Eric Biedermanf96a8102003-06-16 16:57:34 +000019362 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019363}
19364
19365
Eric Biedermanf96a8102003-06-16 16:57:34 +000019366static void fix_tangles(struct compile_state *state,
19367 struct reg_block *blocks, struct triple_reg_set *live,
19368 struct reg_block *rb, struct triple *ins, void *arg)
19369{
Eric Biederman153ea352003-06-20 14:43:20 +000019370 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019371 struct triple *tangle;
19372 do {
19373 char used[MAX_REGISTERS];
19374 struct triple_reg_set *set;
19375 tangle = 0;
19376
19377 /* Find out which registers have multiple uses at this point */
19378 memset(used, 0, sizeof(used));
19379 for(set = live; set; set = set->next) {
19380 struct reg_info info;
19381 info = read_lhs_color(state, set->member, 0);
19382 if (info.reg == REG_UNSET) {
19383 continue;
19384 }
19385 reg_inc_used(state, used, info.reg);
19386 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019387
Eric Biedermanf96a8102003-06-16 16:57:34 +000019388 /* Now find the least dominated definition of a register in
19389 * conflict I have seen so far.
19390 */
19391 for(set = live; set; set = set->next) {
19392 struct reg_info info;
19393 info = read_lhs_color(state, set->member, 0);
19394 if (used[info.reg] < 2) {
19395 continue;
19396 }
Eric Biederman153ea352003-06-20 14:43:20 +000019397 /* Changing copies that feed into phi functions
19398 * is incorrect.
19399 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019400 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019401 (set->member->use->member->op == OP_PHI)) {
19402 continue;
19403 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019404 if (!tangle || tdominates(state, set->member, tangle)) {
19405 tangle = set->member;
19406 }
19407 }
19408 /* If I have found a tangle resolve it */
19409 if (tangle) {
19410 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019411 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019412 post_copy = resolve_tangle(state, tangle);
19413 if (post_copy) {
19414 replace_block_use(state, blocks, tangle, post_copy);
19415 }
19416 if (post_copy && (tangle != ins)) {
19417 replace_set_use(state, live, tangle, post_copy);
19418 }
19419 }
19420 } while(tangle);
19421 return;
19422}
19423
Eric Biederman153ea352003-06-20 14:43:20 +000019424static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019425 struct compile_state *state, struct reg_block *blocks)
19426{
Eric Biederman153ea352003-06-20 14:43:20 +000019427 int tangles;
19428 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019429 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019430 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019431 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019432 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019433}
19434
Eric Biedermand1ea5392003-06-28 06:49:45 +000019435
19436static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19437static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19438
19439struct triple *find_constrained_def(
19440 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019441{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019442 struct live_range_def *lrd, *lrd_next;
19443 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019444 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019445 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019446 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019447
19448 lrd = lrd_next;
19449 lrd_next = lrd->next;
19450
Eric Biedermand1ea5392003-06-28 06:49:45 +000019451 regcm = arch_type_to_regcm(state, lrd->def->type);
19452 info = find_lhs_color(state, lrd->def, 0);
19453 regcm = arch_regcm_reg_normalize(state, regcm);
19454 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019455 /* If the 2 register class masks are equal then
19456 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019457 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019458 if (regcm == info.regcm) {
19459 continue;
19460 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019461
Eric Biederman5ade04a2003-10-22 04:03:46 +000019462 /* If there is just one use.
19463 * That use cannot accept a larger register class.
19464 * There are no intervening definitions except
19465 * definitions that feed into that use.
19466 * Then a triple is not constrained.
19467 * FIXME handle this case!
19468 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019469#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019470#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019471#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019472
Eric Biederman5ade04a2003-10-22 04:03:46 +000019473
Eric Biedermand1ea5392003-06-28 06:49:45 +000019474 /* Of the constrained live ranges deal with the
19475 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019476 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019477 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019478 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019479 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019480 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019481 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019482 tdominates(state, lrd->def, constrained))
19483 {
19484 constrained = lrd->def;
19485 }
19486 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019487 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019488}
19489
Eric Biedermand1ea5392003-06-28 06:49:45 +000019490static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019491 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019492 struct live_range *range)
19493{
19494 /* Walk through the edges in conflict and our current live
19495 * range, and find definitions that are more severly constrained
19496 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019497 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019498 * Then pick one of those ranges and relax the constraints.
19499 */
19500 struct live_range_edge *edge;
19501 struct triple *constrained;
19502
19503 constrained = 0;
19504 for(edge = range->edges; edge; edge = edge->next) {
19505 constrained = find_constrained_def(state, edge->node, constrained);
19506 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019507#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019508#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019509#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019510 if (!constrained) {
19511 constrained = find_constrained_def(state, range, constrained);
19512 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019513
19514 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019515 fprintf(state->errout, "constrained: ");
19516 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019517 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019518 if (constrained) {
19519 ids_from_rstate(state, rstate);
19520 cleanup_rstate(state, rstate);
19521 resolve_tangle(state, constrained);
19522 }
19523 return !!constrained;
19524}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019525
Eric Biedermand1ea5392003-06-28 06:49:45 +000019526static int split_ranges(
19527 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019528 char *used, struct live_range *range)
19529{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019530 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019531 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019532 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019533 rstate->passes, tops(range->defs->def->op), range->defs->def);
19534 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019535 if ((range->color == REG_UNNEEDED) ||
19536 (rstate->passes >= rstate->max_passes)) {
19537 return 0;
19538 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019539 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019540
Eric Biedermand1ea5392003-06-28 06:49:45 +000019541 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019542 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019543 * (a) allow me to spill a register or
19544 * (b) allow me to place a value in another register.
19545 *
19546 * So far I don't have a test case for this, the resolving
19547 * of mandatory constraints has solved all of my
19548 * know issues. So I have choosen not to write any
19549 * code until I cat get a better feel for cases where
19550 * it would be useful to have.
19551 *
19552 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019553#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019554#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019555#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019556
Eric Biederman5ade04a2003-10-22 04:03:46 +000019557 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019558 FILE *fp = state->errout;
19559 print_interference_blocks(state, rstate, fp, 0);
19560 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019561 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019562 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019563}
19564
Eric Biederman5ade04a2003-10-22 04:03:46 +000019565static FILE *cgdebug_fp(struct compile_state *state)
19566{
19567 FILE *fp;
19568 fp = 0;
19569 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019570 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019571 }
19572 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019573 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019574 }
19575 return fp;
19576}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019577
Eric Biederman5ade04a2003-10-22 04:03:46 +000019578static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19579{
19580 FILE *fp;
19581 fp = cgdebug_fp(state);
19582 if (fp) {
19583 va_list args;
19584 va_start(args, fmt);
19585 vfprintf(fp, fmt, args);
19586 va_end(args);
19587 }
19588}
19589
19590static void cgdebug_flush(struct compile_state *state)
19591{
19592 FILE *fp;
19593 fp = cgdebug_fp(state);
19594 if (fp) {
19595 fflush(fp);
19596 }
19597}
19598
19599static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19600{
19601 FILE *fp;
19602 fp = cgdebug_fp(state);
19603 if (fp) {
19604 loc(fp, state, ins);
19605 }
19606}
19607
Stefan Reinauer14e22772010-04-27 06:56:47 +000019608static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019609 struct reg_state *rstate, struct live_range *range)
19610{
19611 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019612 struct live_range_def *lrd;
19613 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019614 struct live_range_edge *edge;
19615 char used[MAX_REGISTERS];
19616 struct triple **expr;
19617
Eric Biedermanb138ac82003-04-22 18:44:01 +000019618 /* Instead of doing just the trivial color select here I try
19619 * a few extra things because a good color selection will help reduce
19620 * copies.
19621 */
19622
19623 /* Find the registers currently in use */
19624 memset(used, 0, sizeof(used));
19625 for(edge = range->edges; edge; edge = edge->next) {
19626 if (edge->node->color == REG_UNSET) {
19627 continue;
19628 }
19629 reg_fill_used(state, used, edge->node->color);
19630 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019631
19632 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019633 int i;
19634 i = 0;
19635 for(edge = range->edges; edge; edge = edge->next) {
19636 i++;
19637 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019638 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019639 tops(range->defs->def->op), i);
19640 cgdebug_loc(state, range->defs->def);
19641 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019642 for(i = 0; i < MAX_REGISTERS; i++) {
19643 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019644 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019645 arch_reg_str(i));
19646 }
19647 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019648 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019649
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019650 /* If a color is already assigned see if it will work */
19651 if (range->color != REG_UNSET) {
19652 struct live_range_def *lrd;
19653 if (!used[range->color]) {
19654 return 1;
19655 }
19656 for(edge = range->edges; edge; edge = edge->next) {
19657 if (edge->node->color != range->color) {
19658 continue;
19659 }
19660 warning(state, edge->node->defs->def, "edge: ");
19661 lrd = edge->node->defs;
19662 do {
19663 warning(state, lrd->def, " %p %s",
19664 lrd->def, tops(lrd->def->op));
19665 lrd = lrd->next;
19666 } while(lrd != edge->node->defs);
19667 }
19668 lrd = range->defs;
19669 warning(state, range->defs->def, "def: ");
19670 do {
19671 warning(state, lrd->def, " %p %s",
19672 lrd->def, tops(lrd->def->op));
19673 lrd = lrd->next;
19674 } while(lrd != range->defs);
19675 internal_error(state, range->defs->def,
19676 "live range with already used color %s",
19677 arch_reg_str(range->color));
19678 }
19679
Eric Biedermanb138ac82003-04-22 18:44:01 +000019680 /* If I feed into an expression reuse it's color.
19681 * This should help remove copies in the case of 2 register instructions
19682 * and phi functions.
19683 */
19684 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019685 lrd = live_range_end(state, range, 0);
19686 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19687 entry = lrd->def->use;
19688 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19689 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019690 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019691 insd = &rstate->lrd[entry->member->id];
19692 if (insd->lr->defs == 0) {
19693 continue;
19694 }
19695 if (!phi && (insd->def->op == OP_PHI) &&
19696 !interfere(rstate, range, insd->lr)) {
19697 phi = insd;
19698 }
Eric Biederman530b5192003-07-01 10:05:30 +000019699 if (insd->lr->color == REG_UNSET) {
19700 continue;
19701 }
19702 regcm = insd->lr->classes;
19703 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019704 (used[insd->lr->color])) {
19705 continue;
19706 }
19707 if (interfere(rstate, range, insd->lr)) {
19708 continue;
19709 }
19710 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019711 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019712 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019713 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019714 * of something else that feeds into the phi function.
19715 */
19716 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019717 if (phi->lr->color != REG_UNSET) {
19718 if (used[phi->lr->color]) {
19719 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019720 }
19721 }
19722 else {
19723 expr = triple_rhs(state, phi->def, 0);
19724 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19725 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019726 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019727 if (!*expr) {
19728 continue;
19729 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019730 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019731 if (lr->color == REG_UNSET) {
19732 continue;
19733 }
19734 regcm = lr->classes;
19735 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019736 (used[lr->color])) {
19737 continue;
19738 }
19739 if (interfere(rstate, range, lr)) {
19740 continue;
19741 }
19742 range->color = lr->color;
19743 }
19744 }
19745 }
19746 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019747 lrd = live_range_head(state, range, 0);
19748 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19749 expr = triple_rhs(state, lrd->def, 0);
19750 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019751 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019752 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019753 if (!*expr) {
19754 continue;
19755 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019756 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019757 if (lr->color == REG_UNSET) {
19758 continue;
19759 }
19760 regcm = lr->classes;
19761 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019762 (used[lr->color])) {
19763 continue;
19764 }
19765 if (interfere(rstate, range, lr)) {
19766 continue;
19767 }
19768 range->color = lr->color;
19769 break;
19770 }
19771 }
19772 /* If I have not opportunitically picked a useful color
19773 * pick the first color that is free.
19774 */
19775 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019776 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019777 arch_select_free_register(state, used, range->classes);
19778 }
19779 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019780 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019781 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019782 if (split_ranges(state, rstate, used, range)) {
19783 return 0;
19784 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019785 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019786 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019787 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019788 lrd = edge->node->defs;
19789 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019790 warning(state, lrd->def, " %s %p",
19791 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019792 lrd = lrd->next;
19793 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019794 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019795 warning(state, range->defs->def, "range: ");
19796 lrd = range->defs;
19797 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019798 warning(state, lrd->def, " %s %p",
19799 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019800 lrd = lrd->next;
19801 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019802
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019803 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019804 range->classes);
19805 for(i = 0; i < MAX_REGISTERS; i++) {
19806 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019807 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019808 arch_reg_str(i));
19809 }
19810 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019811 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019812 }
Eric Biederman530b5192003-07-01 10:05:30 +000019813 range->classes &= arch_reg_regcm(state, range->color);
19814 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019815 internal_error(state, range->defs->def, "select_free_color did not?");
19816 }
19817 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019818}
19819
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019820static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019821{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019822 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019823 struct live_range_edge *edge;
19824 struct live_range *range;
19825 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019826 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019827 range = rstate->low;
19828 if (*range->group_prev != range) {
19829 internal_error(state, 0, "lo: *prev != range?");
19830 }
19831 *range->group_prev = range->group_next;
19832 if (range->group_next) {
19833 range->group_next->group_prev = range->group_prev;
19834 }
19835 if (&range->group_next == rstate->low_tail) {
19836 rstate->low_tail = range->group_prev;
19837 }
19838 if (rstate->low == range) {
19839 internal_error(state, 0, "low: next != prev?");
19840 }
19841 }
19842 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019843 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019844 range = rstate->high;
19845 if (*range->group_prev != range) {
19846 internal_error(state, 0, "hi: *prev != range?");
19847 }
19848 *range->group_prev = range->group_next;
19849 if (range->group_next) {
19850 range->group_next->group_prev = range->group_prev;
19851 }
19852 if (&range->group_next == rstate->high_tail) {
19853 rstate->high_tail = range->group_prev;
19854 }
19855 if (rstate->high == range) {
19856 internal_error(state, 0, "high: next != prev?");
19857 }
19858 }
19859 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019860 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019861 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019862 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019863 range->group_prev = 0;
19864 for(edge = range->edges; edge; edge = edge->next) {
19865 struct live_range *node;
19866 node = edge->node;
19867 /* Move nodes from the high to the low list */
19868 if (node->group_prev && (node->color == REG_UNSET) &&
19869 (node->degree == regc_max_size(state, node->classes))) {
19870 if (*node->group_prev != node) {
19871 internal_error(state, 0, "move: *prev != node?");
19872 }
19873 *node->group_prev = node->group_next;
19874 if (node->group_next) {
19875 node->group_next->group_prev = node->group_prev;
19876 }
19877 if (&node->group_next == rstate->high_tail) {
19878 rstate->high_tail = node->group_prev;
19879 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019880 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019881 node->group_prev = rstate->low_tail;
19882 node->group_next = 0;
19883 *rstate->low_tail = node;
19884 rstate->low_tail = &node->group_next;
19885 if (*node->group_prev != node) {
19886 internal_error(state, 0, "move2: *prev != node?");
19887 }
19888 }
19889 node->degree -= 1;
19890 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019891 colored = color_graph(state, rstate);
19892 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019893 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019894 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019895 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019896 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019897 if (colored) {
19898 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19899 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019900 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019901 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019902}
19903
Eric Biedermana96d6a92003-05-13 20:45:19 +000019904static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19905{
19906 struct live_range *lr;
19907 struct live_range_edge *edge;
19908 struct triple *ins, *first;
19909 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019910 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019911 ins = first;
19912 do {
19913 if (triple_is_def(state, ins)) {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019914 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019915 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019916 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019917 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019918 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019919 if (lr->color == REG_UNSET) {
19920 internal_error(state, ins,
19921 "triple without a color");
19922 }
19923 /* Find the registers used by the edges */
19924 memset(used, 0, sizeof(used));
19925 for(edge = lr->edges; edge; edge = edge->next) {
19926 if (edge->node->color == REG_UNSET) {
19927 internal_error(state, 0,
19928 "live range without a color");
19929 }
19930 reg_fill_used(state, used, edge->node->color);
19931 }
19932 if (used[lr->color]) {
19933 internal_error(state, ins,
19934 "triple with already used color");
19935 }
19936 }
19937 ins = ins->next;
19938 } while(ins != first);
19939}
19940
Eric Biedermanb138ac82003-04-22 18:44:01 +000019941static void color_triples(struct compile_state *state, struct reg_state *rstate)
19942{
Eric Biederman90089602004-05-28 14:11:54 +000019943 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019944 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019945 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019946 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019947 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019948 do {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019949 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019950 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019951 "triple without a live range");
19952 }
Eric Biederman90089602004-05-28 14:11:54 +000019953 lrd = &rstate->lrd[ins->id];
19954 lr = lrd->lr;
19955 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019956 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019957 ins = ins->next;
19958 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019959}
19960
Eric Biedermanb138ac82003-04-22 18:44:01 +000019961static struct live_range *merge_sort_lr(
19962 struct live_range *first, struct live_range *last)
19963{
19964 struct live_range *mid, *join, **join_tail, *pick;
19965 size_t size;
19966 size = (last - first) + 1;
19967 if (size >= 2) {
19968 mid = first + size/2;
19969 first = merge_sort_lr(first, mid -1);
19970 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019971
Eric Biedermanb138ac82003-04-22 18:44:01 +000019972 join = 0;
19973 join_tail = &join;
19974 /* merge the two lists */
19975 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019976 if ((first->degree < mid->degree) ||
19977 ((first->degree == mid->degree) &&
19978 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019979 pick = first;
19980 first = first->group_next;
19981 if (first) {
19982 first->group_prev = 0;
19983 }
19984 }
19985 else {
19986 pick = mid;
19987 mid = mid->group_next;
19988 if (mid) {
19989 mid->group_prev = 0;
19990 }
19991 }
19992 pick->group_next = 0;
19993 pick->group_prev = join_tail;
19994 *join_tail = pick;
19995 join_tail = &pick->group_next;
19996 }
19997 /* Splice the remaining list */
19998 pick = (first)? first : mid;
19999 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020000 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020001 pick->group_prev = join_tail;
20002 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020003 }
20004 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020005 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020006 first = 0;
20007 }
20008 join = first;
20009 }
20010 return join;
20011}
20012
Stefan Reinauer14e22772010-04-27 06:56:47 +000020013static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020014 struct reg_state *rstate)
20015{
20016 struct triple *ins, *first;
20017 if (!rstate->defs) {
20018 return;
20019 }
20020 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020021 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020022 FILE *fp = state->dbgout;
20023 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020024 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020025 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020026 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020027 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020028 ins = first;
20029 do {
20030 if (ins->id) {
20031 struct live_range_def *lrd;
20032 lrd = &rstate->lrd[ins->id];
20033 ins->id = lrd->orig_id;
20034 }
20035 ins = ins->next;
20036 } while(ins != first);
20037}
20038
20039static void cleanup_live_edges(struct reg_state *rstate)
20040{
20041 int i;
20042 /* Free the edges on each node */
20043 for(i = 1; i <= rstate->ranges; i++) {
20044 remove_live_edges(rstate, &rstate->lr[i]);
20045 }
20046}
20047
20048static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20049{
20050 cleanup_live_edges(rstate);
20051 xfree(rstate->lrd);
20052 xfree(rstate->lr);
20053
20054 /* Free the variable lifetime information */
20055 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020056 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020057 }
20058 rstate->defs = 0;
20059 rstate->ranges = 0;
20060 rstate->lrd = 0;
20061 rstate->lr = 0;
20062 rstate->blocks = 0;
20063}
20064
Eric Biederman153ea352003-06-20 14:43:20 +000020065static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020066static void allocate_registers(struct compile_state *state)
20067{
20068 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020069 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020070
20071 /* Clear out the reg_state */
20072 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020073 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020074
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020075 do {
20076 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020077 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020078 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020079
Eric Biederman5ade04a2003-10-22 04:03:46 +000020080 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020081 FILE *fp = state->errout;
20082 fprintf(fp, "pass: %d\n", rstate.passes);
20083 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020084 }
Eric Biederman153ea352003-06-20 14:43:20 +000020085
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020086 /* Restore ids */
20087 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020088
Eric Biedermanf96a8102003-06-16 16:57:34 +000020089 /* Cleanup the temporary data structures */
20090 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020091
Eric Biedermanf96a8102003-06-16 16:57:34 +000020092 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020093 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020094
Eric Biedermanf96a8102003-06-16 16:57:34 +000020095 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020096 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020097
Eric Biederman153ea352003-06-20 14:43:20 +000020098 /* Fix two simultaneous uses of the same register.
20099 * In a few pathlogical cases a partial untangle moves
20100 * the tangle to a part of the graph we won't revisit.
20101 * So we keep looping until we have no more tangle fixes
20102 * to apply.
20103 */
20104 do {
20105 tangles = correct_tangles(state, rstate.blocks);
20106 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020107
Stefan Reinauer14e22772010-04-27 06:56:47 +000020108
Eric Biederman90089602004-05-28 14:11:54 +000020109 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020110 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020111
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020112 /* Allocate and initialize the live ranges */
20113 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020114
Stefan Reinauer14e22772010-04-27 06:56:47 +000020115 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020116 * buys me nothing. The code is left this way in case
20117 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020118 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020119 */
20120 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020121 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020122 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020123 }
20124
Eric Biederman153ea352003-06-20 14:43:20 +000020125 /* Remove any previous live edge calculations */
20126 cleanup_live_edges(&rstate);
20127
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020128 /* Compute the interference graph */
20129 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020130 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020131 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020132
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020133 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020134 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020135 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020136 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020137 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020138 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020139 print_interference_ins, &rstate);
20140 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020141
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020142 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020143
Eric Biederman5ade04a2003-10-22 04:03:46 +000020144 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020145 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020146 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020147 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020148
Eric Biederman83b991a2003-10-11 06:20:25 +000020149#if DEBUG_CONSISTENCY > 1
20150# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020151 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020152# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020153 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020154 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020155 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020156 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020157# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020158 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020159#endif
20160#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020161
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020162 /* Build the groups low and high. But with the nodes
20163 * first sorted by degree order.
20164 */
20165 rstate.low_tail = &rstate.low;
20166 rstate.high_tail = &rstate.high;
20167 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20168 if (rstate.high) {
20169 rstate.high->group_prev = &rstate.high;
20170 }
20171 for(point = &rstate.high; *point; point = &(*point)->group_next)
20172 ;
20173 rstate.high_tail = point;
20174 /* Walk through the high list and move everything that needs
20175 * to be onto low.
20176 */
20177 for(point = &rstate.high; *point; point = next) {
20178 struct live_range *range;
20179 next = &(*point)->group_next;
20180 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020181
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020182 /* If it has a low degree or it already has a color
20183 * place the node in low.
20184 */
20185 if ((range->degree < regc_max_size(state, range->classes)) ||
20186 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020187 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020188 range - rstate.lr, range->degree,
20189 (range->color != REG_UNSET) ? " (colored)": "");
20190 *range->group_prev = range->group_next;
20191 if (range->group_next) {
20192 range->group_next->group_prev = range->group_prev;
20193 }
20194 if (&range->group_next == rstate.high_tail) {
20195 rstate.high_tail = range->group_prev;
20196 }
20197 range->group_prev = rstate.low_tail;
20198 range->group_next = 0;
20199 *rstate.low_tail = range;
20200 rstate.low_tail = &range->group_next;
20201 next = point;
20202 }
20203 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020204 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020205 range - rstate.lr, range->degree,
20206 (range->color != REG_UNSET) ? " (colored)": "");
20207 }
20208 }
20209 /* Color the live_ranges */
20210 colored = color_graph(state, &rstate);
20211 rstate.passes++;
20212 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020213
Eric Biedermana96d6a92003-05-13 20:45:19 +000020214 /* Verify the graph was properly colored */
20215 verify_colors(state, &rstate);
20216
Eric Biedermanb138ac82003-04-22 18:44:01 +000020217 /* Move the colors from the graph to the triples */
20218 color_triples(state, &rstate);
20219
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020220 /* Cleanup the temporary data structures */
20221 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020222
20223 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020224 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020225}
20226
20227/* Sparce Conditional Constant Propogation
20228 * =========================================
20229 */
20230struct ssa_edge;
20231struct flow_block;
20232struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020233 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020234 struct triple *def;
20235 struct ssa_edge *out;
20236 struct flow_block *fblock;
20237 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020238 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020239 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020240 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020241 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020242};
20243struct ssa_edge {
20244 struct lattice_node *src;
20245 struct lattice_node *dst;
20246 struct ssa_edge *work_next;
20247 struct ssa_edge *work_prev;
20248 struct ssa_edge *out_next;
20249};
20250struct flow_edge {
20251 struct flow_block *src;
20252 struct flow_block *dst;
20253 struct flow_edge *work_next;
20254 struct flow_edge *work_prev;
20255 struct flow_edge *in_next;
20256 struct flow_edge *out_next;
20257 int executable;
20258};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020259#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020260struct flow_block {
20261 struct block *block;
20262 struct flow_edge *in;
20263 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020264 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020265};
20266
20267struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020268 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020269 struct lattice_node *lattice;
20270 struct ssa_edge *ssa_edges;
20271 struct flow_block *flow_blocks;
20272 struct flow_edge *flow_work_list;
20273 struct ssa_edge *ssa_work_list;
20274};
20275
20276
Eric Biederman90089602004-05-28 14:11:54 +000020277static int is_scc_const(struct compile_state *state, struct triple *ins)
20278{
20279 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20280}
20281
20282static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20283{
20284 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20285}
20286
20287static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20288{
20289 return is_scc_const(state, lnode->val);
20290}
20291
20292static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20293{
20294 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20295}
20296
Stefan Reinauer14e22772010-04-27 06:56:47 +000020297static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020298 struct flow_edge *fedge)
20299{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020300 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020301 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020302 fedge,
20303 fedge->src->block?fedge->src->block->last->id: 0,
20304 fedge->dst->block?fedge->dst->block->first->id: 0);
20305 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020306 if ((fedge == scc->flow_work_list) ||
20307 (fedge->work_next != fedge) ||
20308 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020309
20310 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020311 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020312 fedge);
20313 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020314 return;
20315 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020316 if (!scc->flow_work_list) {
20317 scc->flow_work_list = fedge;
20318 fedge->work_next = fedge->work_prev = fedge;
20319 }
20320 else {
20321 struct flow_edge *ftail;
20322 ftail = scc->flow_work_list->work_prev;
20323 fedge->work_next = ftail->work_next;
20324 fedge->work_prev = ftail;
20325 fedge->work_next->work_prev = fedge;
20326 fedge->work_prev->work_next = fedge;
20327 }
20328}
20329
20330static struct flow_edge *scc_next_fedge(
20331 struct compile_state *state, struct scc_state *scc)
20332{
20333 struct flow_edge *fedge;
20334 fedge = scc->flow_work_list;
20335 if (fedge) {
20336 fedge->work_next->work_prev = fedge->work_prev;
20337 fedge->work_prev->work_next = fedge->work_next;
20338 if (fedge->work_next != fedge) {
20339 scc->flow_work_list = fedge->work_next;
20340 } else {
20341 scc->flow_work_list = 0;
20342 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020343 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020344 }
20345 return fedge;
20346}
20347
20348static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20349 struct ssa_edge *sedge)
20350{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020351 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020352 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20353 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020354 sedge->src->def->id,
20355 sedge->dst->def->id);
20356 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020357 if ((sedge == scc->ssa_work_list) ||
20358 (sedge->work_next != sedge) ||
20359 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020360
20361 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020362 fprintf(state->errout, "dupped sedge: %5ld\n",
20363 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020364 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020365 return;
20366 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020367 if (!scc->ssa_work_list) {
20368 scc->ssa_work_list = sedge;
20369 sedge->work_next = sedge->work_prev = sedge;
20370 }
20371 else {
20372 struct ssa_edge *stail;
20373 stail = scc->ssa_work_list->work_prev;
20374 sedge->work_next = stail->work_next;
20375 sedge->work_prev = stail;
20376 sedge->work_next->work_prev = sedge;
20377 sedge->work_prev->work_next = sedge;
20378 }
20379}
20380
20381static struct ssa_edge *scc_next_sedge(
20382 struct compile_state *state, struct scc_state *scc)
20383{
20384 struct ssa_edge *sedge;
20385 sedge = scc->ssa_work_list;
20386 if (sedge) {
20387 sedge->work_next->work_prev = sedge->work_prev;
20388 sedge->work_prev->work_next = sedge->work_next;
20389 if (sedge->work_next != sedge) {
20390 scc->ssa_work_list = sedge->work_next;
20391 } else {
20392 scc->ssa_work_list = 0;
20393 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020394 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020395 }
20396 return sedge;
20397}
20398
20399static void initialize_scc_state(
20400 struct compile_state *state, struct scc_state *scc)
20401{
20402 int ins_count, ssa_edge_count;
20403 int ins_index, ssa_edge_index, fblock_index;
20404 struct triple *first, *ins;
20405 struct block *block;
20406 struct flow_block *fblock;
20407
20408 memset(scc, 0, sizeof(*scc));
20409
20410 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020411 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020412 ins = first;
20413 ins_count = ssa_edge_count = 0;
20414 do {
20415 struct triple_set *edge;
20416 ins_count += 1;
20417 for(edge = ins->use; edge; edge = edge->next) {
20418 ssa_edge_count++;
20419 }
20420 ins = ins->next;
20421 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020422 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020423 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20424 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020425 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020426 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020427 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020428 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020429 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020430 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020431 scc->flow_blocks =
20432 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020433 "flow_blocks");
20434
20435 /* Initialize pass one collect up the nodes */
20436 fblock = 0;
20437 block = 0;
20438 ins_index = ssa_edge_index = fblock_index = 0;
20439 ins = first;
20440 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020441 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20442 block = ins->u.block;
20443 if (!block) {
20444 internal_error(state, ins, "label without block");
20445 }
20446 fblock_index += 1;
20447 block->vertex = fblock_index;
20448 fblock = &scc->flow_blocks[fblock_index];
20449 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020450 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20451 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020452 }
20453 {
20454 struct lattice_node *lnode;
20455 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020456 lnode = &scc->lattice[ins_index];
20457 lnode->def = ins;
20458 lnode->out = 0;
20459 lnode->fblock = fblock;
20460 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020461 if (lnode->val->op == OP_UNKNOWNVAL) {
20462 lnode->val = 0; /* LATTICE LOW by definition */
20463 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020464 lnode->old_id = ins->id;
20465 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020466 }
20467 ins = ins->next;
20468 } while(ins != first);
20469 /* Initialize pass two collect up the edges */
20470 block = 0;
20471 fblock = 0;
20472 ins = first;
20473 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020474 {
20475 struct triple_set *edge;
20476 struct ssa_edge **stail;
20477 struct lattice_node *lnode;
20478 lnode = &scc->lattice[ins->id];
20479 lnode->out = 0;
20480 stail = &lnode->out;
20481 for(edge = ins->use; edge; edge = edge->next) {
20482 struct ssa_edge *sedge;
20483 ssa_edge_index += 1;
20484 sedge = &scc->ssa_edges[ssa_edge_index];
20485 *stail = sedge;
20486 stail = &sedge->out_next;
20487 sedge->src = lnode;
20488 sedge->dst = &scc->lattice[edge->member->id];
20489 sedge->work_next = sedge->work_prev = sedge;
20490 sedge->out_next = 0;
20491 }
20492 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020493 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20494 struct flow_edge *fedge, **ftail;
20495 struct block_set *bedge;
20496 block = ins->u.block;
20497 fblock = &scc->flow_blocks[block->vertex];
20498 fblock->in = 0;
20499 fblock->out = 0;
20500 ftail = &fblock->out;
20501
20502 fedge = fblock->edges;
20503 bedge = block->edges;
20504 for(; bedge; bedge = bedge->next, fedge++) {
20505 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20506 if (fedge->dst->block != bedge->member) {
20507 internal_error(state, 0, "block mismatch");
20508 }
20509 *ftail = fedge;
20510 ftail = &fedge->out_next;
20511 fedge->out_next = 0;
20512 }
20513 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20514 fedge->src = fblock;
20515 fedge->work_next = fedge->work_prev = fedge;
20516 fedge->executable = 0;
20517 }
20518 }
20519 ins = ins->next;
20520 } while (ins != first);
20521 block = 0;
20522 fblock = 0;
20523 ins = first;
20524 do {
20525 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20526 struct flow_edge **ftail;
20527 struct block_set *bedge;
20528 block = ins->u.block;
20529 fblock = &scc->flow_blocks[block->vertex];
20530 ftail = &fblock->in;
20531 for(bedge = block->use; bedge; bedge = bedge->next) {
20532 struct block *src_block;
20533 struct flow_block *sfblock;
20534 struct flow_edge *sfedge;
20535 src_block = bedge->member;
20536 sfblock = &scc->flow_blocks[src_block->vertex];
20537 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20538 if (sfedge->dst == fblock) {
20539 break;
20540 }
20541 }
20542 if (!sfedge) {
20543 internal_error(state, 0, "edge mismatch");
20544 }
20545 *ftail = sfedge;
20546 ftail = &sfedge->in_next;
20547 sfedge->in_next = 0;
20548 }
20549 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020550 ins = ins->next;
20551 } while(ins != first);
20552 /* Setup a dummy block 0 as a node above the start node */
20553 {
20554 struct flow_block *fblock, *dst;
20555 struct flow_edge *fedge;
20556 fblock = &scc->flow_blocks[0];
20557 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020558 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020559 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020560 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020561 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020562 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020563 fedge->src = fblock;
20564 fedge->dst = dst;
20565 fedge->work_next = fedge;
20566 fedge->work_prev = fedge;
20567 fedge->in_next = fedge->dst->in;
20568 fedge->out_next = 0;
20569 fedge->executable = 0;
20570 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020571
Eric Biedermanb138ac82003-04-22 18:44:01 +000020572 /* Initialize the work lists */
20573 scc->flow_work_list = 0;
20574 scc->ssa_work_list = 0;
20575 scc_add_fedge(state, scc, fedge);
20576 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020577 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020578 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020579 ins_index, ssa_edge_index, fblock_index);
20580 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020581}
20582
Stefan Reinauer14e22772010-04-27 06:56:47 +000020583
Eric Biedermanb138ac82003-04-22 18:44:01 +000020584static void free_scc_state(
20585 struct compile_state *state, struct scc_state *scc)
20586{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020587 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020588 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020589 struct flow_block *fblock;
20590 fblock = &scc->flow_blocks[i];
20591 if (fblock->edges) {
20592 xfree(fblock->edges);
20593 fblock->edges = 0;
20594 }
20595 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020596 xfree(scc->flow_blocks);
20597 xfree(scc->ssa_edges);
20598 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020599
Eric Biedermanb138ac82003-04-22 18:44:01 +000020600}
20601
20602static struct lattice_node *triple_to_lattice(
20603 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20604{
20605 if (ins->id <= 0) {
20606 internal_error(state, ins, "bad id");
20607 }
20608 return &scc->lattice[ins->id];
20609}
20610
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020611static struct triple *preserve_lval(
20612 struct compile_state *state, struct lattice_node *lnode)
20613{
20614 struct triple *old;
20615 /* Preserve the original value */
20616 if (lnode->val) {
20617 old = dup_triple(state, lnode->val);
20618 if (lnode->val != lnode->def) {
20619 xfree(lnode->val);
20620 }
20621 lnode->val = 0;
20622 } else {
20623 old = 0;
20624 }
20625 return old;
20626}
20627
Stefan Reinauer14e22772010-04-27 06:56:47 +000020628static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020629 struct triple *old, struct lattice_node *lnode)
20630{
20631 int changed;
20632 /* See if the lattice value has changed */
20633 changed = 1;
20634 if (!old && !lnode->val) {
20635 changed = 0;
20636 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020637 if (changed &&
20638 lnode->val && old &&
20639 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020640 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020641 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20642 changed = 0;
20643 }
20644 if (old) {
20645 xfree(old);
20646 }
20647 return changed;
20648
20649}
20650
Eric Biederman5ade04a2003-10-22 04:03:46 +000020651static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020652 struct compile_state *state, struct scc_state *scc,
20653 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020654{
Eric Biederman90089602004-05-28 14:11:54 +000020655 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20656 display_triple_changes(state->errout, lnode->val, lnode->def);
20657 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020658 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020659 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020660 struct triple *val, **expr;
20661 val = lnode->val? lnode->val : lnode->def;
20662 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020663 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020664 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20665 lnode->def->id,
20666 tops(lnode->def->op));
20667 expr = triple_rhs(state, lnode->def, 0);
20668 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20669 if (*expr) {
20670 fprintf(fp, " %d", (*expr)->id);
20671 }
20672 }
20673 if (val->op == OP_INTCONST) {
20674 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20675 }
20676 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020677 (is_lattice_hi(state, lnode)? "hi":
20678 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020679 changed? "changed" : ""
20680 );
20681 }
20682}
20683
Eric Biedermanb138ac82003-04-22 18:44:01 +000020684static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20685 struct lattice_node *lnode)
20686{
20687 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020688 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020689 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020690 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020691
Eric Biedermanb138ac82003-04-22 18:44:01 +000020692 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020693 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694
Eric Biedermanb138ac82003-04-22 18:44:01 +000020695 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020696 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020697 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020698 scratch->next = scratch;
20699 scratch->prev = scratch;
20700 scratch->use = 0;
20701
Eric Biederman90089602004-05-28 14:11:54 +000020702 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020703 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020704 dexpr = &lnode->def->param[i];
20705 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020706 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020707 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20708 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020709 *dexpr) {
20710 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020711 tmp = triple_to_lattice(state, scc, *dexpr);
20712 *vexpr = (tmp->val)? tmp->val : tmp->def;
20713 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020714 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020715 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020716 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020717 }
20718 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020719#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020720#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020721#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020722 /* So far it looks like only the strength reduction
20723 * optimization are things I need to worry about.
20724 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020725 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020726 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020727 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020728 internal_error(state, lnode->def, "scratch used?");
20729 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020730 if ((scratch->prev != scratch) ||
20731 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020732 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020733 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020734 internal_error(state, lnode->def, "scratch in list?");
20735 }
20736 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020737 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020738 for(i = 0; i < count; i++) {
20739 vexpr = &scratch->param[i];
20740 if (*vexpr) {
20741 unuse_triple(*vexpr, scratch);
20742 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020743 }
Eric Biederman90089602004-05-28 14:11:54 +000020744 if (lnode->val->op == OP_UNKNOWNVAL) {
20745 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020746 }
Eric Biederman90089602004-05-28 14:11:54 +000020747 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020748 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020749 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020750 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020751 count * sizeof(lnode->val->param[0])) == 0) &&
20752 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020753 lnode->val = lnode->def;
20754 }
Eric Biederman90089602004-05-28 14:11:54 +000020755 /* Only allow lattice high when all of my inputs
20756 * are also lattice high. Occassionally I can
20757 * have constants with a lattice low input, so
20758 * I do not need to check that case.
20759 */
20760 if (is_lattice_hi(state, lnode)) {
20761 struct lattice_node *tmp;
20762 int rhs;
20763 rhs = lnode->val->rhs;
20764 for(i = 0; i < rhs; i++) {
20765 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20766 if (!is_lattice_hi(state, tmp)) {
20767 lnode->val = 0;
20768 break;
20769 }
20770 }
20771 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020772 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020773 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020774 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020775 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020776 lnode->val = 0;
20777 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020778 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020779 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020780 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020781 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020782 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020783 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020784 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020785#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020786#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020787#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020788 if (changed) {
20789 internal_warning(state, lnode->def, "non def changes value?");
20790 }
20791 lnode->val = 0;
20792 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020793
Eric Biederman83b991a2003-10-11 06:20:25 +000020794 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020795 if (lnode->val != scratch) {
20796 xfree(scratch);
20797 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020798
Eric Biedermanb138ac82003-04-22 18:44:01 +000020799 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020800}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020801
Eric Biederman90089602004-05-28 14:11:54 +000020802
20803static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020804 struct lattice_node *lnode)
20805{
20806 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020807 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020808 int changed;
20809
20810 /* Update the branch value */
20811 changed = compute_lnode_val(state, scc, lnode);
20812 scc_debug_lnode(state, scc, lnode, changed);
20813
20814 /* This only applies to conditional branches */
20815 if (!triple_is_cbranch(state, lnode->def)) {
20816 internal_error(state, lnode->def, "not a conditional branch");
20817 }
20818
Eric Biederman5ade04a2003-10-22 04:03:46 +000020819 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020820 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020821 FILE *fp = state->errout;
20822 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020823 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020825
Eric Biedermanb138ac82003-04-22 18:44:01 +000020826 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020827 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020828 }
Eric Biederman90089602004-05-28 14:11:54 +000020829 fprintf(fp, " )");
20830 if (lnode->def->rhs > 0) {
20831 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020832 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020833 }
Eric Biederman90089602004-05-28 14:11:54 +000020834 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020835 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020836 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020837 for(left = cond->fblock->out; left; left = left->out_next) {
20838 if (left->dst->block->first == lnode->def->next) {
20839 break;
20840 }
20841 }
20842 if (!left) {
20843 internal_error(state, lnode->def, "Cannot find left branch edge");
20844 }
20845 for(right = cond->fblock->out; right; right = right->out_next) {
20846 if (right->dst->block->first == TARG(lnode->def, 0)) {
20847 break;
20848 }
20849 }
20850 if (!right) {
20851 internal_error(state, lnode->def, "Cannot find right branch edge");
20852 }
Eric Biederman90089602004-05-28 14:11:54 +000020853 /* I should only come here if the controlling expressions value
20854 * has changed, which means it must be either a constant or lo.
20855 */
20856 if (is_lattice_hi(state, cond)) {
20857 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020858 return;
20859 }
Eric Biederman90089602004-05-28 14:11:54 +000020860 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020861 scc_add_fedge(state, scc, left);
20862 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020863 }
20864 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020865 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020866 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020867 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020868 }
20869
20870}
20871
Eric Biederman90089602004-05-28 14:11:54 +000020872
Stefan Reinauer14e22772010-04-27 06:56:47 +000020873static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020874 struct scc_state *scc, struct ssa_edge *sedge)
20875{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020876 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020877 scc_visit_cbranch(state, scc, sedge->dst);
20878 }
20879 else if (triple_is_def(state, sedge->dst->def)) {
20880 scc_add_sedge(state, scc, sedge);
20881 }
20882}
20883
Stefan Reinauer14e22772010-04-27 06:56:47 +000020884static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020885 struct lattice_node *lnode)
20886{
20887 struct lattice_node *tmp;
20888 struct triple **slot, *old;
20889 struct flow_edge *fedge;
20890 int changed;
20891 int index;
20892 if (lnode->def->op != OP_PHI) {
20893 internal_error(state, lnode->def, "not phi");
20894 }
20895 /* Store the original value */
20896 old = preserve_lval(state, lnode);
20897
20898 /* default to lattice high */
20899 lnode->val = lnode->def;
20900 slot = &RHS(lnode->def, 0);
20901 index = 0;
20902 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20903 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020904 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020905 index,
20906 fedge->dst->block->vertex,
20907 fedge->executable
20908 );
20909 }
20910 if (!fedge->executable) {
20911 continue;
20912 }
20913 if (!slot[index]) {
20914 internal_error(state, lnode->def, "no phi value");
20915 }
20916 tmp = triple_to_lattice(state, scc, slot[index]);
20917 /* meet(X, lattice low) = lattice low */
20918 if (is_lattice_lo(state, tmp)) {
20919 lnode->val = 0;
20920 }
20921 /* meet(X, lattice high) = X */
20922 else if (is_lattice_hi(state, tmp)) {
20923 lnode->val = lnode->val;
20924 }
20925 /* meet(lattice high, X) = X */
20926 else if (is_lattice_hi(state, lnode)) {
20927 lnode->val = dup_triple(state, tmp->val);
20928 /* Only change the type if necessary */
20929 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20930 lnode->val->type = lnode->def->type;
20931 }
20932 }
20933 /* meet(const, const) = const or lattice low */
20934 else if (!constants_equal(state, lnode->val, tmp->val)) {
20935 lnode->val = 0;
20936 }
20937
20938 /* meet(lattice low, X) = lattice low */
20939 if (is_lattice_lo(state, lnode)) {
20940 lnode->val = 0;
20941 break;
20942 }
20943 }
20944 changed = lval_changed(state, old, lnode);
20945 scc_debug_lnode(state, scc, lnode, changed);
20946
20947 /* If the lattice value has changed update the work lists. */
20948 if (changed) {
20949 struct ssa_edge *sedge;
20950 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20951 scc_add_sedge_dst(state, scc, sedge);
20952 }
20953 }
20954}
20955
20956
Eric Biedermanb138ac82003-04-22 18:44:01 +000020957static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20958 struct lattice_node *lnode)
20959{
20960 int changed;
20961
Eric Biederman90089602004-05-28 14:11:54 +000020962 if (!triple_is_def(state, lnode->def)) {
20963 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020964 }
Eric Biederman90089602004-05-28 14:11:54 +000020965 changed = compute_lnode_val(state, scc, lnode);
20966 scc_debug_lnode(state, scc, lnode, changed);
20967
20968 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020969 struct ssa_edge *sedge;
20970 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020971 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020972 }
20973 }
20974}
20975
20976static void scc_writeback_values(
20977 struct compile_state *state, struct scc_state *scc)
20978{
20979 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020980 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020981 ins = first;
20982 do {
20983 struct lattice_node *lnode;
20984 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020985 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020986 if (is_lattice_hi(state, lnode) &&
20987 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020988 {
20989 struct flow_edge *fedge;
20990 int executable;
20991 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020992 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020993 !executable && fedge; fedge = fedge->in_next) {
20994 executable |= fedge->executable;
20995 }
20996 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020997 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020998 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020999 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021000 tops(lnode->def->op),
21001 tops(lnode->val->op));
21002 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021003 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021004 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021005
Eric Biederman83b991a2003-10-11 06:20:25 +000021006 /* Restore id */
21007 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021008 if (lnode->val && (lnode->val != ins)) {
21009 /* See if it something I know how to write back */
21010 switch(lnode->val->op) {
21011 case OP_INTCONST:
21012 mkconst(state, ins, lnode->val->u.cval);
21013 break;
21014 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021015 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021016 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021017 break;
21018 default:
21019 /* By default don't copy the changes,
21020 * recompute them in place instead.
21021 */
21022 simplify(state, ins);
21023 break;
21024 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021025 if (is_const(lnode->val) &&
21026 !constants_equal(state, lnode->val, ins)) {
21027 internal_error(state, 0, "constants not equal");
21028 }
21029 /* Free the lattice nodes */
21030 xfree(lnode->val);
21031 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021032 }
21033 ins = ins->next;
21034 } while(ins != first);
21035}
21036
21037static void scc_transform(struct compile_state *state)
21038{
21039 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021040 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21041 return;
21042 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021043
21044 initialize_scc_state(state, &scc);
21045
21046 while(scc.flow_work_list || scc.ssa_work_list) {
21047 struct flow_edge *fedge;
21048 struct ssa_edge *sedge;
21049 struct flow_edge *fptr;
21050 while((fedge = scc_next_fedge(state, &scc))) {
21051 struct block *block;
21052 struct triple *ptr;
21053 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021054 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021055 int done;
21056 if (fedge->executable) {
21057 continue;
21058 }
21059 if (!fedge->dst) {
21060 internal_error(state, 0, "fedge without dst");
21061 }
21062 if (!fedge->src) {
21063 internal_error(state, 0, "fedge without src");
21064 }
21065 fedge->executable = 1;
21066 fblock = fedge->dst;
21067 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021068 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021069 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21070 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021071 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021072 }
21073 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021074
Eric Biederman5ade04a2003-10-22 04:03:46 +000021075 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021076 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021077 block->vertex, reps);
21078 }
21079
Eric Biedermanb138ac82003-04-22 18:44:01 +000021080 done = 0;
21081 for(ptr = block->first; !done; ptr = ptr->next) {
21082 struct lattice_node *lnode;
21083 done = (ptr == block->last);
21084 lnode = &scc.lattice[ptr->id];
21085 if (ptr->op == OP_PHI) {
21086 scc_visit_phi(state, &scc, lnode);
21087 }
Eric Biederman90089602004-05-28 14:11:54 +000021088 else if ((reps == 1) && triple_is_def(state, ptr))
21089 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021090 scc_visit_expr(state, &scc, lnode);
21091 }
21092 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021093 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021094 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021095 struct flow_edge *out;
21096 for(out = fblock->out; out; out = out->out_next) {
21097 scc_add_fedge(state, &scc, out);
21098 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021099 }
21100 }
21101 while((sedge = scc_next_sedge(state, &scc))) {
21102 struct lattice_node *lnode;
21103 struct flow_block *fblock;
21104 lnode = sedge->dst;
21105 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021106
21107 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021108 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021109 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021110 sedge->src->def->id,
21111 sedge->dst->def->id);
21112 }
21113
Eric Biedermanb138ac82003-04-22 18:44:01 +000021114 if (lnode->def->op == OP_PHI) {
21115 scc_visit_phi(state, &scc, lnode);
21116 }
21117 else {
21118 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21119 if (fptr->executable) {
21120 break;
21121 }
21122 }
21123 if (fptr) {
21124 scc_visit_expr(state, &scc, lnode);
21125 }
21126 }
21127 }
21128 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021129
Eric Biedermanb138ac82003-04-22 18:44:01 +000021130 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021131 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021132 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021133
Eric Biederman90089602004-05-28 14:11:54 +000021134 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021135}
21136
21137
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021138static void transform_to_arch_instructions(struct compile_state *state)
21139{
21140 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021141 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021142 ins = first;
21143 do {
21144 ins = transform_to_arch_instruction(state, ins);
21145 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021146
Eric Biederman90089602004-05-28 14:11:54 +000021147 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021148}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021149
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021150#if DEBUG_CONSISTENCY
21151static void verify_uses(struct compile_state *state)
21152{
21153 struct triple *first, *ins;
21154 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021155 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021156 ins = first;
21157 do {
21158 struct triple **expr;
21159 expr = triple_rhs(state, ins, 0);
21160 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021161 struct triple *rhs;
21162 rhs = *expr;
21163 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021164 if (set->member == ins) {
21165 break;
21166 }
21167 }
21168 if (!set) {
21169 internal_error(state, ins, "rhs not used");
21170 }
21171 }
21172 expr = triple_lhs(state, ins, 0);
21173 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021174 struct triple *lhs;
21175 lhs = *expr;
21176 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021177 if (set->member == ins) {
21178 break;
21179 }
21180 }
21181 if (!set) {
21182 internal_error(state, ins, "lhs not used");
21183 }
21184 }
Eric Biederman90089602004-05-28 14:11:54 +000021185 expr = triple_misc(state, ins, 0);
21186 if (ins->op != OP_PHI) {
21187 for(; expr; expr = triple_targ(state, ins, expr)) {
21188 struct triple *misc;
21189 misc = *expr;
21190 for(set = misc?misc->use:0; set; set = set->next) {
21191 if (set->member == ins) {
21192 break;
21193 }
21194 }
21195 if (!set) {
21196 internal_error(state, ins, "misc not used");
21197 }
21198 }
21199 }
21200 if (!triple_is_ret(state, ins)) {
21201 expr = triple_targ(state, ins, 0);
21202 for(; expr; expr = triple_targ(state, ins, expr)) {
21203 struct triple *targ;
21204 targ = *expr;
21205 for(set = targ?targ->use:0; set; set = set->next) {
21206 if (set->member == ins) {
21207 break;
21208 }
21209 }
21210 if (!set) {
21211 internal_error(state, ins, "targ not used");
21212 }
21213 }
21214 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021215 ins = ins->next;
21216 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021217
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021218}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021219static void verify_blocks_present(struct compile_state *state)
21220{
21221 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021222 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021223 return;
21224 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021225 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021226 ins = first;
21227 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021228 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021229 if (triple_stores_block(state, ins)) {
21230 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021231 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021232 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021233 }
21234 }
21235 ins = ins->next;
21236 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021237
21238
Eric Biedermand1ea5392003-06-28 06:49:45 +000021239}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021240
21241static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21242{
21243 struct block_set *bedge;
21244 struct block *targ;
21245 targ = block_of_triple(state, edge);
21246 for(bedge = block->edges; bedge; bedge = bedge->next) {
21247 if (bedge->member == targ) {
21248 return 1;
21249 }
21250 }
21251 return 0;
21252}
21253
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021254static void verify_blocks(struct compile_state *state)
21255{
21256 struct triple *ins;
21257 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021258 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021259 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021260 if (!block) {
21261 return;
21262 }
Eric Biederman530b5192003-07-01 10:05:30 +000021263 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021264 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021265 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021266 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021267 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021268 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021269 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021270 internal_error(state, ins, "inconsitent block specified");
21271 }
Eric Biederman530b5192003-07-01 10:05:30 +000021272 valid_ins(state, ins);
21273 }
21274 users = 0;
21275 for(user = block->use; user; user = user->next) {
21276 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021277 if (!user->member->first) {
21278 internal_error(state, block->first, "user is empty");
21279 }
Eric Biederman90089602004-05-28 14:11:54 +000021280 if ((block == state->bb.last_block) &&
21281 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021282 continue;
21283 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021284 for(edge = user->member->edges; edge; edge = edge->next) {
21285 if (edge->member == block) {
21286 break;
21287 }
21288 }
21289 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021290 internal_error(state, user->member->first,
21291 "user does not use block");
21292 }
21293 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021294 if (triple_is_branch(state, block->last)) {
21295 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021296 expr = triple_edge_targ(state, block->last, 0);
21297 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021298 if (*expr && !edge_present(state, block, *expr)) {
21299 internal_error(state, block->last, "no edge to targ");
21300 }
21301 }
Eric Biederman530b5192003-07-01 10:05:30 +000021302 }
Eric Biederman90089602004-05-28 14:11:54 +000021303 if (!triple_is_ubranch(state, block->last) &&
21304 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021305 !edge_present(state, block, block->last->next)) {
21306 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021307 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021308 for(edge = block->edges; edge; edge = edge->next) {
21309 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021310 if (user->member == block) {
21311 break;
21312 }
21313 }
21314 if (!user || user->member != block) {
21315 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021316 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021317 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021318 if (!edge->member->first) {
21319 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021320 }
Eric Biederman530b5192003-07-01 10:05:30 +000021321 }
21322 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021323 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021324 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021325 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021326 }
21327 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021328 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021329 "cannot find next block");
21330 }
21331 block = block->last->next->u.block;
21332 if (!block) {
21333 internal_error(state, block->last->next,
21334 "bad next block");
21335 }
Eric Biederman90089602004-05-28 14:11:54 +000021336 } while(block != state->bb.first_block);
21337 if (blocks != state->bb.last_vertex) {
21338 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21339 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021340 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021341}
21342
21343static void verify_domination(struct compile_state *state)
21344{
21345 struct triple *first, *ins;
21346 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021347 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021348 return;
21349 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021350
Eric Biederman83b991a2003-10-11 06:20:25 +000021351 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021352 ins = first;
21353 do {
21354 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021355 struct triple **slot;
21356 struct triple *use_point;
21357 int i, zrhs;
21358 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021359 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021360 slot = &RHS(set->member, 0);
21361 /* See if the use is on the right hand side */
21362 for(i = 0; i < zrhs; i++) {
21363 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021364 break;
21365 }
21366 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021367 if (i < zrhs) {
21368 use_point = set->member;
21369 if (set->member->op == OP_PHI) {
21370 struct block_set *bset;
21371 int edge;
21372 bset = set->member->u.block->use;
21373 for(edge = 0; bset && (edge < i); edge++) {
21374 bset = bset->next;
21375 }
21376 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021377 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021378 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021379 }
21380 use_point = bset->member->last;
21381 }
21382 }
21383 if (use_point &&
21384 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021385 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021386 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021387 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021388 }
21389 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021390 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021391 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021392 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021393 }
21394 }
21395 ins = ins->next;
21396 } while(ins != first);
21397}
21398
Eric Biederman83b991a2003-10-11 06:20:25 +000021399static void verify_rhs(struct compile_state *state)
21400{
21401 struct triple *first, *ins;
21402 first = state->first;
21403 ins = first;
21404 do {
21405 struct triple **slot;
21406 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021407 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021408 slot = &RHS(ins, 0);
21409 for(i = 0; i < zrhs; i++) {
21410 if (slot[i] == 0) {
21411 internal_error(state, ins,
21412 "missing rhs %d on %s",
21413 i, tops(ins->op));
21414 }
21415 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21416 internal_error(state, ins,
21417 "ins == rhs[%d] on %s",
21418 i, tops(ins->op));
21419 }
21420 }
21421 ins = ins->next;
21422 } while(ins != first);
21423}
21424
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021425static void verify_piece(struct compile_state *state)
21426{
21427 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021428 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021429 ins = first;
21430 do {
21431 struct triple *ptr;
21432 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021433 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021434 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21435 if (ptr != LHS(ins, i)) {
21436 internal_error(state, ins, "malformed lhs on %s",
21437 tops(ins->op));
21438 }
21439 if (ptr->op != OP_PIECE) {
21440 internal_error(state, ins, "bad lhs op %s at %d on %s",
21441 tops(ptr->op), i, tops(ins->op));
21442 }
21443 if (ptr->u.cval != i) {
21444 internal_error(state, ins, "bad u.cval of %d %d expected",
21445 ptr->u.cval, i);
21446 }
21447 }
21448 ins = ins->next;
21449 } while(ins != first);
21450}
Eric Biederman83b991a2003-10-11 06:20:25 +000021451
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021452static void verify_ins_colors(struct compile_state *state)
21453{
21454 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021455
Eric Biederman83b991a2003-10-11 06:20:25 +000021456 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021457 ins = first;
21458 do {
21459 ins = ins->next;
21460 } while(ins != first);
21461}
Eric Biederman90089602004-05-28 14:11:54 +000021462
21463static void verify_unknown(struct compile_state *state)
21464{
21465 struct triple *first, *ins;
21466 if ( (unknown_triple.next != &unknown_triple) ||
21467 (unknown_triple.prev != &unknown_triple) ||
21468#if 0
21469 (unknown_triple.use != 0) ||
21470#endif
21471 (unknown_triple.op != OP_UNKNOWNVAL) ||
21472 (unknown_triple.lhs != 0) ||
21473 (unknown_triple.rhs != 0) ||
21474 (unknown_triple.misc != 0) ||
21475 (unknown_triple.targ != 0) ||
21476 (unknown_triple.template_id != 0) ||
21477 (unknown_triple.id != -1) ||
21478 (unknown_triple.type != &unknown_type) ||
21479 (unknown_triple.occurance != &dummy_occurance) ||
21480 (unknown_triple.param[0] != 0) ||
21481 (unknown_triple.param[1] != 0)) {
21482 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21483 }
21484 if ( (dummy_occurance.count != 2) ||
21485 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21486 (strcmp(dummy_occurance.function, "") != 0) ||
21487 (dummy_occurance.col != 0) ||
21488 (dummy_occurance.parent != 0)) {
21489 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21490 }
21491 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21492 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21493 }
21494 first = state->first;
21495 ins = first;
21496 do {
21497 int params, i;
21498 if (ins == &unknown_triple) {
21499 internal_error(state, ins, "unknown triple in list");
21500 }
21501 params = TRIPLE_SIZE(ins);
21502 for(i = 0; i < params; i++) {
21503 if (ins->param[i] == &unknown_triple) {
21504 internal_error(state, ins, "unknown triple used!");
21505 }
21506 }
21507 ins = ins->next;
21508 } while(ins != first);
21509}
21510
21511static void verify_types(struct compile_state *state)
21512{
21513 struct triple *first, *ins;
21514 first = state->first;
21515 ins = first;
21516 do {
21517 struct type *invalid;
21518 invalid = invalid_type(state, ins->type);
21519 if (invalid) {
21520 FILE *fp = state->errout;
21521 fprintf(fp, "type: ");
21522 name_of(fp, ins->type);
21523 fprintf(fp, "\n");
21524 fprintf(fp, "invalid type: ");
21525 name_of(fp, invalid);
21526 fprintf(fp, "\n");
21527 internal_error(state, ins, "invalid ins type");
21528 }
21529 } while(ins != first);
21530}
21531
21532static void verify_copy(struct compile_state *state)
21533{
21534 struct triple *first, *ins, *next;
21535 first = state->first;
21536 next = ins = first;
21537 do {
21538 ins = next;
21539 next = ins->next;
21540 if (ins->op != OP_COPY) {
21541 continue;
21542 }
21543 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21544 FILE *fp = state->errout;
21545 fprintf(fp, "src type: ");
21546 name_of(fp, RHS(ins, 0)->type);
21547 fprintf(fp, "\n");
21548 fprintf(fp, "dst type: ");
21549 name_of(fp, ins->type);
21550 fprintf(fp, "\n");
21551 internal_error(state, ins, "type mismatch in copy");
21552 }
21553 } while(next != first);
21554}
21555
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021556static void verify_consistency(struct compile_state *state)
21557{
Eric Biederman90089602004-05-28 14:11:54 +000021558 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021559 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021560 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021561 verify_blocks(state);
21562 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021563 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021564 verify_piece(state);
21565 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021566 verify_types(state);
21567 verify_copy(state);
21568 if (state->compiler->debug & DEBUG_VERIFICATION) {
21569 fprintf(state->dbgout, "consistency verified\n");
21570 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021571}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021572#else
Eric Biederman153ea352003-06-20 14:43:20 +000021573static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021574#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021575
21576static void optimize(struct compile_state *state)
21577{
Eric Biederman90089602004-05-28 14:11:54 +000021578 /* Join all of the functions into one giant function */
21579 join_functions(state);
21580
Eric Biederman5ade04a2003-10-22 04:03:46 +000021581 /* Dump what the instruction graph intially looks like */
21582 print_triples(state);
21583
Eric Biederman0babc1c2003-05-09 02:39:00 +000021584 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021585 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021586 print_triples(state);
21587
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021588 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021589 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021590 state->bb.first = state->first;
21591 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021592
Eric Biederman530b5192003-07-01 10:05:30 +000021593 /* Transform the code to ssa form. */
21594 /*
21595 * The transformation to ssa form puts a phi function
21596 * on each of edge of a dominance frontier where that
21597 * phi function might be needed. At -O2 if we don't
21598 * eleminate the excess phi functions we can get an
21599 * exponential code size growth. So I kill the extra
21600 * phi functions early and I kill them often.
21601 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021602 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021603 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021604
Eric Biederman83b991a2003-10-11 06:20:25 +000021605 /* Remove dead code */
21606 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021607 verify_consistency(state);
21608
Eric Biedermanb138ac82003-04-22 18:44:01 +000021609 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021610 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021611 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021612 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021613 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021614 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021615#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021616#warning "WISHLIST implement single use constants (least possible register pressure)"
21617#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021618#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021619 /* Select architecture instructions and an initial partial
21620 * coloring based on architecture constraints.
21621 */
21622 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021623 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021624
Eric Biederman83b991a2003-10-11 06:20:25 +000021625 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021626 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021627 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021628
Eric Biedermanb138ac82003-04-22 18:44:01 +000021629 /* Color all of the variables to see if they will fit in registers */
21630 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021631 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021632
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021633 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021634 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021635
Eric Biedermanb138ac82003-04-22 18:44:01 +000021636 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021637 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021638
Eric Biedermanb138ac82003-04-22 18:44:01 +000021639 /* Remove the optimization information.
21640 * This is more to check for memory consistency than to free memory.
21641 */
Eric Biederman90089602004-05-28 14:11:54 +000021642 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021643}
21644
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021645static void print_op_asm(struct compile_state *state,
21646 struct triple *ins, FILE *fp)
21647{
21648 struct asm_info *info;
21649 const char *ptr;
21650 unsigned lhs, rhs, i;
21651 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021652 lhs = ins->lhs;
21653 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021654 /* Don't count the clobbers in lhs */
21655 for(i = 0; i < lhs; i++) {
21656 if (LHS(ins, i)->type == &void_type) {
21657 break;
21658 }
21659 }
21660 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021661 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021662 fputc('\t', fp);
21663 for(ptr = info->str; *ptr; ptr++) {
21664 char *next;
21665 unsigned long param;
21666 struct triple *piece;
21667 if (*ptr != '%') {
21668 fputc(*ptr, fp);
21669 continue;
21670 }
21671 ptr++;
21672 if (*ptr == '%') {
21673 fputc('%', fp);
21674 continue;
21675 }
21676 param = strtoul(ptr, &next, 10);
21677 if (ptr == next) {
21678 error(state, ins, "Invalid asm template");
21679 }
21680 if (param >= (lhs + rhs)) {
21681 error(state, ins, "Invalid param %%%u in asm template",
21682 param);
21683 }
21684 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021685 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021686 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021687 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021688 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021689 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021690}
21691
21692
21693/* Only use the low x86 byte registers. This allows me
21694 * allocate the entire register when a byte register is used.
21695 */
21696#define X86_4_8BIT_GPRS 1
21697
Eric Biederman83b991a2003-10-11 06:20:25 +000021698/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021699#define X86_MMX_REGS (1<<0)
21700#define X86_XMM_REGS (1<<1)
21701#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021702
Eric Biedermanb138ac82003-04-22 18:44:01 +000021703/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021704#define REGC_FLAGS 0
21705#define REGC_GPR8 1
21706#define REGC_GPR16 2
21707#define REGC_GPR32 3
21708#define REGC_DIVIDEND64 4
21709#define REGC_DIVIDEND32 5
21710#define REGC_MMX 6
21711#define REGC_XMM 7
21712#define REGC_GPR32_8 8
21713#define REGC_GPR16_8 9
21714#define REGC_GPR8_LO 10
21715#define REGC_IMM32 11
21716#define REGC_IMM16 12
21717#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021718#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021719#if LAST_REGC >= MAX_REGC
21720#error "MAX_REGC is to low"
21721#endif
21722
21723/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021724#define REGCM_FLAGS (1 << REGC_FLAGS)
21725#define REGCM_GPR8 (1 << REGC_GPR8)
21726#define REGCM_GPR16 (1 << REGC_GPR16)
21727#define REGCM_GPR32 (1 << REGC_GPR32)
21728#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21729#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21730#define REGCM_MMX (1 << REGC_MMX)
21731#define REGCM_XMM (1 << REGC_XMM)
21732#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21733#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21734#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21735#define REGCM_IMM32 (1 << REGC_IMM32)
21736#define REGCM_IMM16 (1 << REGC_IMM16)
21737#define REGCM_IMM8 (1 << REGC_IMM8)
21738#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021739#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021740
21741/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021742#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021743#define REGC_FLAGS_FIRST REG_EFLAGS
21744#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021745#define REG_AL 3
21746#define REG_BL 4
21747#define REG_CL 5
21748#define REG_DL 6
21749#define REG_AH 7
21750#define REG_BH 8
21751#define REG_CH 9
21752#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021753#define REGC_GPR8_LO_FIRST REG_AL
21754#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021755#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021756#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021757#define REG_AX 11
21758#define REG_BX 12
21759#define REG_CX 13
21760#define REG_DX 14
21761#define REG_SI 15
21762#define REG_DI 16
21763#define REG_BP 17
21764#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021765#define REGC_GPR16_FIRST REG_AX
21766#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021767#define REG_EAX 19
21768#define REG_EBX 20
21769#define REG_ECX 21
21770#define REG_EDX 22
21771#define REG_ESI 23
21772#define REG_EDI 24
21773#define REG_EBP 25
21774#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021775#define REGC_GPR32_FIRST REG_EAX
21776#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021777#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021778#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21779#define REGC_DIVIDEND64_LAST REG_EDXEAX
21780#define REG_DXAX 28
21781#define REGC_DIVIDEND32_FIRST REG_DXAX
21782#define REGC_DIVIDEND32_LAST REG_DXAX
21783#define REG_MMX0 29
21784#define REG_MMX1 30
21785#define REG_MMX2 31
21786#define REG_MMX3 32
21787#define REG_MMX4 33
21788#define REG_MMX5 34
21789#define REG_MMX6 35
21790#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021791#define REGC_MMX_FIRST REG_MMX0
21792#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021793#define REG_XMM0 37
21794#define REG_XMM1 38
21795#define REG_XMM2 39
21796#define REG_XMM3 40
21797#define REG_XMM4 41
21798#define REG_XMM5 42
21799#define REG_XMM6 43
21800#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021801#define REGC_XMM_FIRST REG_XMM0
21802#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021803
21804#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021805#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021806#endif
21807
Eric Biedermanb138ac82003-04-22 18:44:01 +000021808#define LAST_REG REG_XMM7
21809
21810#define REGC_GPR32_8_FIRST REG_EAX
21811#define REGC_GPR32_8_LAST REG_EDX
21812#define REGC_GPR16_8_FIRST REG_AX
21813#define REGC_GPR16_8_LAST REG_DX
21814
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021815#define REGC_IMM8_FIRST -1
21816#define REGC_IMM8_LAST -1
21817#define REGC_IMM16_FIRST -2
21818#define REGC_IMM16_LAST -1
21819#define REGC_IMM32_FIRST -4
21820#define REGC_IMM32_LAST -1
21821
Eric Biedermanb138ac82003-04-22 18:44:01 +000021822#if LAST_REG >= MAX_REGISTERS
21823#error "MAX_REGISTERS to low"
21824#endif
21825
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021826
21827static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021828 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21829 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21830 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21831 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21832 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21833 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21834 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21835 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21836 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21837 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21838 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21839 [REGC_IMM32] = 0,
21840 [REGC_IMM16] = 0,
21841 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021842};
21843
21844static const struct {
21845 int first, last;
21846} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021847 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21848 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21849 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21850 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21851 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21852 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21853 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21854 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21855 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21856 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21857 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21858 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21859 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21860 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021861};
21862
Eric Biederman90089602004-05-28 14:11:54 +000021863#if ARCH_INPUT_REGS != 4
21864#error ARCH_INPUT_REGS size mismatch
21865#endif
21866static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21867 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21868 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21869 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21870 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21871};
21872
21873#if ARCH_OUTPUT_REGS != 4
21874#error ARCH_INPUT_REGS size mismatch
21875#endif
21876static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21877 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21878 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21879 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21880 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21881};
21882
Eric Biederman5ade04a2003-10-22 04:03:46 +000021883static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021884{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021885 memset(arch, 0, sizeof(*arch));
21886 arch->features = 0;
21887}
21888
Eric Biederman90089602004-05-28 14:11:54 +000021889static const struct compiler_flag arch_flags[] = {
21890 { "mmx", X86_MMX_REGS },
21891 { "sse", X86_XMM_REGS },
21892 { "noop-copy", X86_NOOP_COPY },
21893 { 0, 0 },
21894};
21895static const struct compiler_flag arch_cpus[] = {
21896 { "i386", 0 },
21897 { "p2", X86_MMX_REGS },
21898 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21899 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21900 { "k7", X86_MMX_REGS },
21901 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21902 { "c3", X86_MMX_REGS },
21903 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21904 { 0, 0 }
21905};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021906static int arch_encode_flag(struct arch_state *arch, const char *flag)
21907{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021908 int result;
21909 int act;
21910
21911 act = 1;
21912 result = -1;
21913 if (strncmp(flag, "no-", 3) == 0) {
21914 flag += 3;
21915 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021916 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021917 if (act && strncmp(flag, "cpu=", 4) == 0) {
21918 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021919 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021920 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021921 else {
Eric Biederman90089602004-05-28 14:11:54 +000021922 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021923 }
21924 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021925}
21926
Eric Biederman90089602004-05-28 14:11:54 +000021927static void arch_usage(FILE *fp)
21928{
21929 flag_usage(fp, arch_flags, "-m", "-mno-");
21930 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21931}
21932
Eric Biedermanb138ac82003-04-22 18:44:01 +000021933static unsigned arch_regc_size(struct compile_state *state, int class)
21934{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021935 if ((class < 0) || (class > LAST_REGC)) {
21936 return 0;
21937 }
21938 return regc_size[class];
21939}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021940
Eric Biedermanb138ac82003-04-22 18:44:01 +000021941static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21942{
21943 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021944 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021945 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021946 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021947
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021948 /* Special case for the immediates */
21949 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21950 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21951 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021952 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021953 return 0;
21954 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021955 return (regcm1 & regcm2) ||
21956 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21957}
21958
21959static void arch_reg_equivs(
21960 struct compile_state *state, unsigned *equiv, int reg)
21961{
21962 if ((reg < 0) || (reg > LAST_REG)) {
21963 internal_error(state, 0, "invalid register");
21964 }
21965 *equiv++ = reg;
21966 switch(reg) {
21967 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021968#if X86_4_8BIT_GPRS
21969 *equiv++ = REG_AH;
21970#endif
21971 *equiv++ = REG_AX;
21972 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021973 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021974 *equiv++ = REG_EDXEAX;
21975 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021976 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021977#if X86_4_8BIT_GPRS
21978 *equiv++ = REG_AL;
21979#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021980 *equiv++ = REG_AX;
21981 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021982 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021983 *equiv++ = REG_EDXEAX;
21984 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021985 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021986#if X86_4_8BIT_GPRS
21987 *equiv++ = REG_BH;
21988#endif
21989 *equiv++ = REG_BX;
21990 *equiv++ = REG_EBX;
21991 break;
21992
Eric Biedermanb138ac82003-04-22 18:44:01 +000021993 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021994#if X86_4_8BIT_GPRS
21995 *equiv++ = REG_BL;
21996#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021997 *equiv++ = REG_BX;
21998 *equiv++ = REG_EBX;
21999 break;
22000 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022001#if X86_4_8BIT_GPRS
22002 *equiv++ = REG_CH;
22003#endif
22004 *equiv++ = REG_CX;
22005 *equiv++ = REG_ECX;
22006 break;
22007
Eric Biedermanb138ac82003-04-22 18:44:01 +000022008 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022009#if X86_4_8BIT_GPRS
22010 *equiv++ = REG_CL;
22011#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022012 *equiv++ = REG_CX;
22013 *equiv++ = REG_ECX;
22014 break;
22015 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022016#if X86_4_8BIT_GPRS
22017 *equiv++ = REG_DH;
22018#endif
22019 *equiv++ = REG_DX;
22020 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022021 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022022 *equiv++ = REG_EDXEAX;
22023 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022024 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022025#if X86_4_8BIT_GPRS
22026 *equiv++ = REG_DL;
22027#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022028 *equiv++ = REG_DX;
22029 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022030 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022031 *equiv++ = REG_EDXEAX;
22032 break;
22033 case REG_AX:
22034 *equiv++ = REG_AL;
22035 *equiv++ = REG_AH;
22036 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022037 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022038 *equiv++ = REG_EDXEAX;
22039 break;
22040 case REG_BX:
22041 *equiv++ = REG_BL;
22042 *equiv++ = REG_BH;
22043 *equiv++ = REG_EBX;
22044 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022045 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022046 *equiv++ = REG_CL;
22047 *equiv++ = REG_CH;
22048 *equiv++ = REG_ECX;
22049 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022050 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022051 *equiv++ = REG_DL;
22052 *equiv++ = REG_DH;
22053 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022054 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022055 *equiv++ = REG_EDXEAX;
22056 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022057 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022058 *equiv++ = REG_ESI;
22059 break;
22060 case REG_DI:
22061 *equiv++ = REG_EDI;
22062 break;
22063 case REG_BP:
22064 *equiv++ = REG_EBP;
22065 break;
22066 case REG_SP:
22067 *equiv++ = REG_ESP;
22068 break;
22069 case REG_EAX:
22070 *equiv++ = REG_AL;
22071 *equiv++ = REG_AH;
22072 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022073 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022074 *equiv++ = REG_EDXEAX;
22075 break;
22076 case REG_EBX:
22077 *equiv++ = REG_BL;
22078 *equiv++ = REG_BH;
22079 *equiv++ = REG_BX;
22080 break;
22081 case REG_ECX:
22082 *equiv++ = REG_CL;
22083 *equiv++ = REG_CH;
22084 *equiv++ = REG_CX;
22085 break;
22086 case REG_EDX:
22087 *equiv++ = REG_DL;
22088 *equiv++ = REG_DH;
22089 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022090 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022091 *equiv++ = REG_EDXEAX;
22092 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022093 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022094 *equiv++ = REG_SI;
22095 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022096 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022097 *equiv++ = REG_DI;
22098 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022099 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022100 *equiv++ = REG_BP;
22101 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022102 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022103 *equiv++ = REG_SP;
22104 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022105 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022106 *equiv++ = REG_AL;
22107 *equiv++ = REG_AH;
22108 *equiv++ = REG_DL;
22109 *equiv++ = REG_DH;
22110 *equiv++ = REG_AX;
22111 *equiv++ = REG_DX;
22112 *equiv++ = REG_EAX;
22113 *equiv++ = REG_EDX;
22114 *equiv++ = REG_EDXEAX;
22115 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022116 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022117 *equiv++ = REG_AL;
22118 *equiv++ = REG_AH;
22119 *equiv++ = REG_DL;
22120 *equiv++ = REG_DH;
22121 *equiv++ = REG_AX;
22122 *equiv++ = REG_DX;
22123 *equiv++ = REG_EAX;
22124 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022125 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022126 break;
22127 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022128 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022129}
22130
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022131static unsigned arch_avail_mask(struct compile_state *state)
22132{
22133 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022134 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022135 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22136 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022137 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022138 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022139 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022140 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022141 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022142 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022143 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022144 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022145 return avail_mask;
22146}
22147
22148static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22149{
22150 unsigned mask, result;
22151 int class, class2;
22152 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022153
22154 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22155 if ((result & mask) == 0) {
22156 continue;
22157 }
22158 if (class > LAST_REGC) {
22159 result &= ~mask;
22160 }
22161 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22162 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22163 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22164 result |= (1 << class2);
22165 }
22166 }
22167 }
Eric Biederman530b5192003-07-01 10:05:30 +000022168 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022169 return result;
22170}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022171
Eric Biedermand1ea5392003-06-28 06:49:45 +000022172static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22173{
22174 /* Like arch_regcm_normalize except immediate register classes are excluded */
22175 regcm = arch_regcm_normalize(state, regcm);
22176 /* Remove the immediate register classes */
22177 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22178 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022179
Eric Biedermand1ea5392003-06-28 06:49:45 +000022180}
22181
Eric Biedermanb138ac82003-04-22 18:44:01 +000022182static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22183{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022184 unsigned mask;
22185 int class;
22186 mask = 0;
22187 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022188 if ((reg >= regcm_bound[class].first) &&
22189 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022190 mask |= (1 << class);
22191 }
22192 }
22193 if (!mask) {
22194 internal_error(state, 0, "reg %d not in any class", reg);
22195 }
22196 return mask;
22197}
22198
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022199static struct reg_info arch_reg_constraint(
22200 struct compile_state *state, struct type *type, const char *constraint)
22201{
22202 static const struct {
22203 char class;
22204 unsigned int mask;
22205 unsigned int reg;
22206 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022207 { 'r', REGCM_GPR32, REG_UNSET },
22208 { 'g', REGCM_GPR32, REG_UNSET },
22209 { 'p', REGCM_GPR32, REG_UNSET },
22210 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022211 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022212 { 'x', REGCM_XMM, REG_UNSET },
22213 { 'y', REGCM_MMX, REG_UNSET },
22214 { 'a', REGCM_GPR32, REG_EAX },
22215 { 'b', REGCM_GPR32, REG_EBX },
22216 { 'c', REGCM_GPR32, REG_ECX },
22217 { 'd', REGCM_GPR32, REG_EDX },
22218 { 'D', REGCM_GPR32, REG_EDI },
22219 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022220 { '\0', 0, REG_UNSET },
22221 };
22222 unsigned int regcm;
22223 unsigned int mask, reg;
22224 struct reg_info result;
22225 const char *ptr;
22226 regcm = arch_type_to_regcm(state, type);
22227 reg = REG_UNSET;
22228 mask = 0;
22229 for(ptr = constraint; *ptr; ptr++) {
22230 int i;
22231 if (*ptr == ' ') {
22232 continue;
22233 }
22234 for(i = 0; constraints[i].class != '\0'; i++) {
22235 if (constraints[i].class == *ptr) {
22236 break;
22237 }
22238 }
22239 if (constraints[i].class == '\0') {
22240 error(state, 0, "invalid register constraint ``%c''", *ptr);
22241 break;
22242 }
22243 if ((constraints[i].mask & regcm) == 0) {
22244 error(state, 0, "invalid register class %c specified",
22245 *ptr);
22246 }
22247 mask |= constraints[i].mask;
22248 if (constraints[i].reg != REG_UNSET) {
22249 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22250 error(state, 0, "Only one register may be specified");
22251 }
22252 reg = constraints[i].reg;
22253 }
22254 }
22255 result.reg = reg;
22256 result.regcm = mask;
22257 return result;
22258}
22259
22260static struct reg_info arch_reg_clobber(
22261 struct compile_state *state, const char *clobber)
22262{
22263 struct reg_info result;
22264 if (strcmp(clobber, "memory") == 0) {
22265 result.reg = REG_UNSET;
22266 result.regcm = 0;
22267 }
Eric Biederman90089602004-05-28 14:11:54 +000022268 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022269 result.reg = REG_EAX;
22270 result.regcm = REGCM_GPR32;
22271 }
Eric Biederman90089602004-05-28 14:11:54 +000022272 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022273 result.reg = REG_EBX;
22274 result.regcm = REGCM_GPR32;
22275 }
Eric Biederman90089602004-05-28 14:11:54 +000022276 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022277 result.reg = REG_ECX;
22278 result.regcm = REGCM_GPR32;
22279 }
Eric Biederman90089602004-05-28 14:11:54 +000022280 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022281 result.reg = REG_EDX;
22282 result.regcm = REGCM_GPR32;
22283 }
Eric Biederman90089602004-05-28 14:11:54 +000022284 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022285 result.reg = REG_ESI;
22286 result.regcm = REGCM_GPR32;
22287 }
Eric Biederman90089602004-05-28 14:11:54 +000022288 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022289 result.reg = REG_EDI;
22290 result.regcm = REGCM_GPR32;
22291 }
Eric Biederman90089602004-05-28 14:11:54 +000022292 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022293 result.reg = REG_EBP;
22294 result.regcm = REGCM_GPR32;
22295 }
Eric Biederman90089602004-05-28 14:11:54 +000022296 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022297 result.reg = REG_ESP;
22298 result.regcm = REGCM_GPR32;
22299 }
22300 else if (strcmp(clobber, "cc") == 0) {
22301 result.reg = REG_EFLAGS;
22302 result.regcm = REGCM_FLAGS;
22303 }
22304 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22305 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22306 result.reg = REG_XMM0 + octdigval(clobber[3]);
22307 result.regcm = REGCM_XMM;
22308 }
Eric Biederman90089602004-05-28 14:11:54 +000022309 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022310 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22311 result.reg = REG_MMX0 + octdigval(clobber[3]);
22312 result.regcm = REGCM_MMX;
22313 }
22314 else {
Eric Biederman90089602004-05-28 14:11:54 +000022315 error(state, 0, "unknown register name `%s' in asm",
22316 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022317 result.reg = REG_UNSET;
22318 result.regcm = 0;
22319 }
22320 return result;
22321}
22322
Stefan Reinauer14e22772010-04-27 06:56:47 +000022323static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022324 char *used, int reg, unsigned classes)
22325{
22326 unsigned mask;
22327 if (used[reg]) {
22328 return REG_UNSET;
22329 }
22330 mask = arch_reg_regcm(state, reg);
22331 return (classes & mask) ? reg : REG_UNSET;
22332}
22333
22334static int arch_select_free_register(
22335 struct compile_state *state, char *used, int classes)
22336{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022337 /* Live ranges with the most neighbors are colored first.
22338 *
22339 * Generally it does not matter which colors are given
22340 * as the register allocator attempts to color live ranges
22341 * in an order where you are guaranteed not to run out of colors.
22342 *
22343 * Occasionally the register allocator cannot find an order
22344 * of register selection that will find a free color. To
22345 * increase the odds the register allocator will work when
22346 * it guesses first give out registers from register classes
22347 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022348 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022349 */
22350 int i, reg;
22351 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022352 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022353 reg = do_select_reg(state, used, i, classes);
22354 }
22355 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22356 reg = do_select_reg(state, used, i, classes);
22357 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022358 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022359 reg = do_select_reg(state, used, i, classes);
22360 }
22361 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22362 reg = do_select_reg(state, used, i, classes);
22363 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022364 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22365 reg = do_select_reg(state, used, i, classes);
22366 }
Eric Biederman530b5192003-07-01 10:05:30 +000022367 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22368 reg = do_select_reg(state, used, i, classes);
22369 }
22370 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22371 reg = do_select_reg(state, used, i, classes);
22372 }
22373 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022374 reg = do_select_reg(state, used, i, classes);
22375 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022376 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22377 reg = do_select_reg(state, used, i, classes);
22378 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022379 return reg;
22380}
22381
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022382
Stefan Reinauer14e22772010-04-27 06:56:47 +000022383static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022384{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022385
22386#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022387#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022388#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022389 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022390 mask = 0;
22391 switch(type->type & TYPE_MASK) {
22392 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022393 case TYPE_VOID:
22394 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022395 break;
22396 case TYPE_CHAR:
22397 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022398 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022399 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022400 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022401 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022402 REGCM_MMX | REGCM_XMM |
22403 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022404 break;
22405 case TYPE_SHORT:
22406 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022407 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022408 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022409 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022410 REGCM_MMX | REGCM_XMM |
22411 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022412 break;
Eric Biederman90089602004-05-28 14:11:54 +000022413 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022414 case TYPE_INT:
22415 case TYPE_UINT:
22416 case TYPE_LONG:
22417 case TYPE_ULONG:
22418 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022419 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022420 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22421 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022422 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022423 break;
Eric Biederman90089602004-05-28 14:11:54 +000022424 case TYPE_JOIN:
22425 case TYPE_UNION:
22426 mask = arch_type_to_regcm(state, type->left);
22427 break;
22428 case TYPE_OVERLAP:
22429 mask = arch_type_to_regcm(state, type->left) &
22430 arch_type_to_regcm(state, type->right);
22431 break;
22432 case TYPE_BITFIELD:
22433 mask = arch_type_to_regcm(state, type->left);
22434 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022435 default:
Eric Biederman90089602004-05-28 14:11:54 +000022436 fprintf(state->errout, "type: ");
22437 name_of(state->errout, type);
22438 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022439 internal_error(state, 0, "no register class for type");
22440 break;
22441 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022442 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022443 return mask;
22444}
22445
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022446static int is_imm32(struct triple *imm)
22447{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022448 // second condition commented out to prevent compiler warning:
22449 // imm->u.cval is always 32bit unsigned, so the comparison is
22450 // always true.
22451 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022452 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022453
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022454}
22455static int is_imm16(struct triple *imm)
22456{
22457 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22458}
22459static int is_imm8(struct triple *imm)
22460{
22461 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22462}
22463
22464static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022465{
22466 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022467 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022468 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022469 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022470 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022471 if (!is_imm32(imm)) {
22472 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022473 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022474 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022475 use_triple(imm, ins);
22476 *expr = imm;
22477 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022478}
22479
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022480static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022481{
22482 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022483 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022484 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022485 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022486 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022487 if (!is_imm8(imm)) {
22488 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022489 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022490 unuse_triple(*expr, ins);
22491 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022492 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022493 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022494}
22495
Eric Biederman530b5192003-07-01 10:05:30 +000022496#define TEMPLATE_NOP 0
22497#define TEMPLATE_INTCONST8 1
22498#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022499#define TEMPLATE_UNKNOWNVAL 3
22500#define TEMPLATE_COPY8_REG 5
22501#define TEMPLATE_COPY16_REG 6
22502#define TEMPLATE_COPY32_REG 7
22503#define TEMPLATE_COPY_IMM8 8
22504#define TEMPLATE_COPY_IMM16 9
22505#define TEMPLATE_COPY_IMM32 10
22506#define TEMPLATE_PHI8 11
22507#define TEMPLATE_PHI16 12
22508#define TEMPLATE_PHI32 13
22509#define TEMPLATE_STORE8 14
22510#define TEMPLATE_STORE16 15
22511#define TEMPLATE_STORE32 16
22512#define TEMPLATE_LOAD8 17
22513#define TEMPLATE_LOAD16 18
22514#define TEMPLATE_LOAD32 19
22515#define TEMPLATE_BINARY8_REG 20
22516#define TEMPLATE_BINARY16_REG 21
22517#define TEMPLATE_BINARY32_REG 22
22518#define TEMPLATE_BINARY8_IMM 23
22519#define TEMPLATE_BINARY16_IMM 24
22520#define TEMPLATE_BINARY32_IMM 25
22521#define TEMPLATE_SL8_CL 26
22522#define TEMPLATE_SL16_CL 27
22523#define TEMPLATE_SL32_CL 28
22524#define TEMPLATE_SL8_IMM 29
22525#define TEMPLATE_SL16_IMM 30
22526#define TEMPLATE_SL32_IMM 31
22527#define TEMPLATE_UNARY8 32
22528#define TEMPLATE_UNARY16 33
22529#define TEMPLATE_UNARY32 34
22530#define TEMPLATE_CMP8_REG 35
22531#define TEMPLATE_CMP16_REG 36
22532#define TEMPLATE_CMP32_REG 37
22533#define TEMPLATE_CMP8_IMM 38
22534#define TEMPLATE_CMP16_IMM 39
22535#define TEMPLATE_CMP32_IMM 40
22536#define TEMPLATE_TEST8 41
22537#define TEMPLATE_TEST16 42
22538#define TEMPLATE_TEST32 43
22539#define TEMPLATE_SET 44
22540#define TEMPLATE_JMP 45
22541#define TEMPLATE_RET 46
22542#define TEMPLATE_INB_DX 47
22543#define TEMPLATE_INB_IMM 48
22544#define TEMPLATE_INW_DX 49
22545#define TEMPLATE_INW_IMM 50
22546#define TEMPLATE_INL_DX 51
22547#define TEMPLATE_INL_IMM 52
22548#define TEMPLATE_OUTB_DX 53
22549#define TEMPLATE_OUTB_IMM 54
22550#define TEMPLATE_OUTW_DX 55
22551#define TEMPLATE_OUTW_IMM 56
22552#define TEMPLATE_OUTL_DX 57
22553#define TEMPLATE_OUTL_IMM 58
22554#define TEMPLATE_BSF 59
22555#define TEMPLATE_RDMSR 60
22556#define TEMPLATE_WRMSR 61
22557#define TEMPLATE_UMUL8 62
22558#define TEMPLATE_UMUL16 63
22559#define TEMPLATE_UMUL32 64
22560#define TEMPLATE_DIV8 65
22561#define TEMPLATE_DIV16 66
22562#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022563#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022564#if LAST_TEMPLATE >= MAX_TEMPLATES
22565#error "MAX_TEMPLATES to low"
22566#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022567
Eric Biederman530b5192003-07-01 10:05:30 +000022568#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
Stefan Reinauer14e22772010-04-27 06:56:47 +000022569#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022570#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022571
Eric Biedermanb138ac82003-04-22 18:44:01 +000022572
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022573static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022574 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022575 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022576 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22577 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22639 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22640 },
22641 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022642 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022643 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22644 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022645 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022646 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22647 },
Eric Biederman90089602004-05-28 14:11:54 +000022648 [TEMPLATE_UNKNOWNVAL] = {
22649 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22650 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022651 [TEMPLATE_COPY8_REG] = {
22652 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22653 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022654 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022655 [TEMPLATE_COPY16_REG] = {
22656 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22657 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22658 },
22659 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022660 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022661 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022662 },
22663 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022664 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022665 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22666 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022667 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022668 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022669 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22670 },
22671 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022672 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022673 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22674 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022675 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022676 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022677 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22678 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022679 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022680 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022681 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022682 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022683 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022684 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022685 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022686 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022687 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022688 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022689 [0] = { REG_UNSET, REGCM_GPR32 },
22690 [1] = { REG_UNSET, REGCM_GPR8_LO },
22691 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022692 },
22693 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022694 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022695 [0] = { REG_UNSET, REGCM_GPR32 },
22696 [1] = { REG_UNSET, REGCM_GPR16 },
22697 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022698 },
22699 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022700 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022701 [0] = { REG_UNSET, REGCM_GPR32 },
22702 [1] = { REG_UNSET, REGCM_GPR32 },
22703 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022704 },
22705 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022706 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022707 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22708 },
22709 [TEMPLATE_LOAD16] = {
22710 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22711 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22712 },
22713 [TEMPLATE_LOAD32] = {
22714 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22715 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22716 },
Eric Biederman530b5192003-07-01 10:05:30 +000022717 [TEMPLATE_BINARY8_REG] = {
22718 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022719 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022720 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22721 [1] = { REG_UNSET, REGCM_GPR8_LO },
22722 },
22723 },
22724 [TEMPLATE_BINARY16_REG] = {
22725 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022726 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022727 [0] = { REG_VIRT0, REGCM_GPR16 },
22728 [1] = { REG_UNSET, REGCM_GPR16 },
22729 },
22730 },
22731 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022732 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022733 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022734 [0] = { REG_VIRT0, REGCM_GPR32 },
22735 [1] = { REG_UNSET, REGCM_GPR32 },
22736 },
22737 },
Eric Biederman530b5192003-07-01 10:05:30 +000022738 [TEMPLATE_BINARY8_IMM] = {
22739 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022740 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022741 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22742 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22743 },
22744 },
22745 [TEMPLATE_BINARY16_IMM] = {
22746 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022747 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022748 [0] = { REG_VIRT0, REGCM_GPR16 },
22749 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22750 },
22751 },
22752 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022753 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022754 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022755 [0] = { REG_VIRT0, REGCM_GPR32 },
22756 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22757 },
22758 },
Eric Biederman530b5192003-07-01 10:05:30 +000022759 [TEMPLATE_SL8_CL] = {
22760 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022761 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022762 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22763 [1] = { REG_CL, REGCM_GPR8_LO },
22764 },
22765 },
22766 [TEMPLATE_SL16_CL] = {
22767 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022768 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022769 [0] = { REG_VIRT0, REGCM_GPR16 },
22770 [1] = { REG_CL, REGCM_GPR8_LO },
22771 },
22772 },
22773 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022774 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022775 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022776 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022777 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022778 },
22779 },
Eric Biederman530b5192003-07-01 10:05:30 +000022780 [TEMPLATE_SL8_IMM] = {
22781 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022782 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022783 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22784 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22785 },
22786 },
22787 [TEMPLATE_SL16_IMM] = {
22788 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022789 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022790 [0] = { REG_VIRT0, REGCM_GPR16 },
22791 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22792 },
22793 },
22794 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022795 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022796 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022797 [0] = { REG_VIRT0, REGCM_GPR32 },
22798 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22799 },
22800 },
Eric Biederman530b5192003-07-01 10:05:30 +000022801 [TEMPLATE_UNARY8] = {
22802 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22803 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22804 },
22805 [TEMPLATE_UNARY16] = {
22806 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22807 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22808 },
22809 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022810 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22811 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22812 },
Eric Biederman530b5192003-07-01 10:05:30 +000022813 [TEMPLATE_CMP8_REG] = {
22814 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22815 .rhs = {
22816 [0] = { REG_UNSET, REGCM_GPR8_LO },
22817 [1] = { REG_UNSET, REGCM_GPR8_LO },
22818 },
22819 },
22820 [TEMPLATE_CMP16_REG] = {
22821 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22822 .rhs = {
22823 [0] = { REG_UNSET, REGCM_GPR16 },
22824 [1] = { REG_UNSET, REGCM_GPR16 },
22825 },
22826 },
22827 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022828 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22829 .rhs = {
22830 [0] = { REG_UNSET, REGCM_GPR32 },
22831 [1] = { REG_UNSET, REGCM_GPR32 },
22832 },
22833 },
Eric Biederman530b5192003-07-01 10:05:30 +000022834 [TEMPLATE_CMP8_IMM] = {
22835 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22836 .rhs = {
22837 [0] = { REG_UNSET, REGCM_GPR8_LO },
22838 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22839 },
22840 },
22841 [TEMPLATE_CMP16_IMM] = {
22842 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22843 .rhs = {
22844 [0] = { REG_UNSET, REGCM_GPR16 },
22845 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22846 },
22847 },
22848 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022849 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22850 .rhs = {
22851 [0] = { REG_UNSET, REGCM_GPR32 },
22852 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22853 },
22854 },
Eric Biederman530b5192003-07-01 10:05:30 +000022855 [TEMPLATE_TEST8] = {
22856 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22857 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22858 },
22859 [TEMPLATE_TEST16] = {
22860 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22861 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22862 },
22863 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022864 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22865 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22866 },
22867 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022868 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022869 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22870 },
22871 [TEMPLATE_JMP] = {
22872 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22873 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022874 [TEMPLATE_RET] = {
22875 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22876 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022877 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022878 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022879 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22880 },
22881 [TEMPLATE_INB_IMM] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022882 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022883 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22884 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022885 [TEMPLATE_INW_DX] = {
22886 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022887 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22888 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022889 [TEMPLATE_INW_IMM] = {
22890 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022891 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22892 },
22893 [TEMPLATE_INL_DX] = {
22894 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22895 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22896 },
22897 [TEMPLATE_INL_IMM] = {
22898 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22899 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22900 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022901 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022902 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022903 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022904 [1] = { REG_DX, REGCM_GPR16 },
22905 },
22906 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022907 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022908 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022909 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022910 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22911 },
22912 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022913 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022914 .rhs = {
22915 [0] = { REG_AX, REGCM_GPR16 },
22916 [1] = { REG_DX, REGCM_GPR16 },
22917 },
22918 },
22919 [TEMPLATE_OUTW_IMM] = {
22920 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022921 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022922 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22923 },
22924 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022925 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022926 .rhs = {
22927 [0] = { REG_EAX, REGCM_GPR32 },
22928 [1] = { REG_DX, REGCM_GPR16 },
22929 },
22930 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022931 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022932 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022933 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022934 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22935 },
22936 },
22937 [TEMPLATE_BSF] = {
22938 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22939 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22940 },
22941 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022942 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022943 [0] = { REG_EAX, REGCM_GPR32 },
22944 [1] = { REG_EDX, REGCM_GPR32 },
22945 },
22946 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22947 },
22948 [TEMPLATE_WRMSR] = {
22949 .rhs = {
22950 [0] = { REG_ECX, REGCM_GPR32 },
22951 [1] = { REG_EAX, REGCM_GPR32 },
22952 [2] = { REG_EDX, REGCM_GPR32 },
22953 },
22954 },
Eric Biederman530b5192003-07-01 10:05:30 +000022955 [TEMPLATE_UMUL8] = {
22956 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022957 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022958 [0] = { REG_AL, REGCM_GPR8_LO },
22959 [1] = { REG_UNSET, REGCM_GPR8_LO },
22960 },
22961 },
22962 [TEMPLATE_UMUL16] = {
22963 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022964 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022965 [0] = { REG_AX, REGCM_GPR16 },
22966 [1] = { REG_UNSET, REGCM_GPR16 },
22967 },
22968 },
22969 [TEMPLATE_UMUL32] = {
22970 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022971 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022972 [0] = { REG_EAX, REGCM_GPR32 },
22973 [1] = { REG_UNSET, REGCM_GPR32 },
22974 },
22975 },
Eric Biederman530b5192003-07-01 10:05:30 +000022976 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022977 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022978 [0] = { REG_AL, REGCM_GPR8_LO },
22979 [1] = { REG_AH, REGCM_GPR8 },
22980 },
22981 .rhs = {
22982 [0] = { REG_AX, REGCM_GPR16 },
22983 [1] = { REG_UNSET, REGCM_GPR8_LO },
22984 },
22985 },
22986 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022987 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022988 [0] = { REG_AX, REGCM_GPR16 },
22989 [1] = { REG_DX, REGCM_GPR16 },
22990 },
22991 .rhs = {
22992 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22993 [1] = { REG_UNSET, REGCM_GPR16 },
22994 },
22995 },
22996 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022997 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022998 [0] = { REG_EAX, REGCM_GPR32 },
22999 [1] = { REG_EDX, REGCM_GPR32 },
23000 },
23001 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023002 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023003 [1] = { REG_UNSET, REGCM_GPR32 },
23004 },
23005 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023006};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023007
Eric Biederman83b991a2003-10-11 06:20:25 +000023008static void fixup_branch(struct compile_state *state,
23009 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23010 struct triple *left, struct triple *right)
23011{
23012 struct triple *test;
23013 if (!left) {
23014 internal_error(state, branch, "no branch test?");
23015 }
23016 test = pre_triple(state, branch,
23017 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023018 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023019 if (cmp_op == OP_CMP) {
23020 test->template_id = TEMPLATE_CMP32_REG;
23021 if (get_imm32(test, &RHS(test, 1))) {
23022 test->template_id = TEMPLATE_CMP32_IMM;
23023 }
23024 }
23025 use_triple(RHS(test, 0), test);
23026 use_triple(RHS(test, 1), test);
23027 unuse_triple(RHS(branch, 0), branch);
23028 RHS(branch, 0) = test;
23029 branch->op = jmp_op;
23030 branch->template_id = TEMPLATE_JMP;
23031 use_triple(RHS(branch, 0), branch);
23032}
23033
Eric Biedermanb138ac82003-04-22 18:44:01 +000023034static void fixup_branches(struct compile_state *state,
23035 struct triple *cmp, struct triple *use, int jmp_op)
23036{
23037 struct triple_set *entry, *next;
23038 for(entry = use->use; entry; entry = next) {
23039 next = entry->next;
23040 if (entry->member->op == OP_COPY) {
23041 fixup_branches(state, cmp, entry->member, jmp_op);
23042 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023043 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023044 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023045 struct triple *left, *right;
23046 left = right = 0;
23047 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023048 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023049 right = RHS(cmp, 1);
23050 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023051 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023052 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023053 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023054 }
23055 }
23056}
23057
Stefan Reinauer14e22772010-04-27 06:56:47 +000023058static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023059 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23060{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023061 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023062 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023063
23064 /* Put a barrier up before the cmp which preceeds the
23065 * copy instruction. If a set actually occurs this gives
23066 * us a chance to move variables in registers out of the way.
23067 */
23068
23069 /* Modify the comparison operator */
23070 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023071 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023072 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023073 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023074 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023075 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023076 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023077 }
23078 /* Generate the instruction sequence that will transform the
23079 * result of the comparison into a logical value.
23080 */
Eric Biederman90089602004-05-28 14:11:54 +000023081 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023082 use_triple(ins, set);
23083 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023084
Eric Biederman90089602004-05-28 14:11:54 +000023085 convert = set;
23086 if (!equiv_types(ins->type, set->type)) {
23087 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23088 use_triple(set, convert);
23089 convert->template_id = TEMPLATE_COPY32_REG;
23090 }
23091
Eric Biedermanb138ac82003-04-22 18:44:01 +000023092 for(entry = ins->use; entry; entry = next) {
23093 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023094 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023095 continue;
23096 }
Eric Biederman90089602004-05-28 14:11:54 +000023097 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023098 }
Eric Biederman90089602004-05-28 14:11:54 +000023099 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023100}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023101
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023102struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23103{
23104 struct ins_template *template;
23105 struct reg_info result;
23106 int zlhs;
23107 if (ins->op == OP_PIECE) {
23108 index = ins->u.cval;
23109 ins = MISC(ins, 0);
23110 }
Eric Biederman90089602004-05-28 14:11:54 +000023111 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023112 if (triple_is_def(state, ins)) {
23113 zlhs = 1;
23114 }
23115 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023116 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023117 index, tops(ins->op));
23118 }
23119 switch(ins->op) {
23120 case OP_ASM:
23121 template = &ins->u.ainfo->tmpl;
23122 break;
23123 default:
23124 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023125 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023126 ins->template_id);
23127 }
23128 template = &templates[ins->template_id];
23129 break;
23130 }
23131 result = template->lhs[index];
23132 result.regcm = arch_regcm_normalize(state, result.regcm);
23133 if (result.reg != REG_UNNEEDED) {
23134 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23135 }
23136 if (result.regcm == 0) {
23137 internal_error(state, ins, "lhs %d regcm == 0", index);
23138 }
23139 return result;
23140}
23141
23142struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23143{
23144 struct reg_info result;
23145 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023146 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023147 (ins->op == OP_PIECE)) {
23148 internal_error(state, ins, "index %d out of range for %s\n",
23149 index, tops(ins->op));
23150 }
23151 switch(ins->op) {
23152 case OP_ASM:
23153 template = &ins->u.ainfo->tmpl;
23154 break;
Eric Biederman90089602004-05-28 14:11:54 +000023155 case OP_PHI:
23156 index = 0;
23157 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023158 default:
23159 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023160 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023161 ins->template_id);
23162 }
23163 template = &templates[ins->template_id];
23164 break;
23165 }
23166 result = template->rhs[index];
23167 result.regcm = arch_regcm_normalize(state, result.regcm);
23168 if (result.regcm == 0) {
23169 internal_error(state, ins, "rhs %d regcm == 0", index);
23170 }
23171 return result;
23172}
23173
Eric Biederman530b5192003-07-01 10:05:30 +000023174static struct triple *mod_div(struct compile_state *state,
23175 struct triple *ins, int div_op, int index)
23176{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023177 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023178
Eric Biederman530b5192003-07-01 10:05:30 +000023179 /* Generate the appropriate division instruction */
23180 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23181 RHS(div, 0) = RHS(ins, 0);
23182 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023183 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023184 div->template_id = TEMPLATE_DIV32;
23185 use_triple(RHS(div, 0), div);
23186 use_triple(RHS(div, 1), div);
23187 use_triple(LHS(div, 0), div);
23188 use_triple(LHS(div, 1), div);
23189
Eric Biederman530b5192003-07-01 10:05:30 +000023190 /* Replate uses of ins with the appropriate piece of the div */
23191 propogate_use(state, ins, LHS(div, index));
23192 release_triple(state, ins);
23193
23194 /* Return the address of the next instruction */
23195 return piece1->next;
23196}
23197
Eric Biederman90089602004-05-28 14:11:54 +000023198static int noop_adecl(struct triple *adecl)
23199{
23200 struct triple_set *use;
23201 /* It's a noop if it doesn't specify stoorage */
23202 if (adecl->lhs == 0) {
23203 return 1;
23204 }
23205 /* Is the adecl used? If not it's a noop */
23206 for(use = adecl->use; use ; use = use->next) {
23207 if ((use->member->op != OP_PIECE) ||
23208 (MISC(use->member, 0) != adecl)) {
23209 return 0;
23210 }
23211 }
23212 return 1;
23213}
23214
23215static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23216{
23217 struct triple *mask, *nmask, *shift;
23218 struct triple *val, *val_mask, *val_shift;
23219 struct triple *targ, *targ_mask;
23220 struct triple *new;
23221 ulong_t the_mask, the_nmask;
23222
23223 targ = RHS(ins, 0);
23224 val = RHS(ins, 1);
23225
23226 /* Get constant for the mask value */
23227 the_mask = 1;
23228 the_mask <<= ins->u.bitfield.size;
23229 the_mask -= 1;
23230 the_mask <<= ins->u.bitfield.offset;
23231 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23232 mask->u.cval = the_mask;
23233
23234 /* Get the inverted mask value */
23235 the_nmask = ~the_mask;
23236 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23237 nmask->u.cval = the_nmask;
23238
23239 /* Get constant for the shift value */
23240 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23241 shift->u.cval = ins->u.bitfield.offset;
23242
23243 /* Shift and mask the source value */
23244 val_shift = val;
23245 if (shift->u.cval != 0) {
23246 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23247 use_triple(val, val_shift);
23248 use_triple(shift, val_shift);
23249 }
23250 val_mask = val_shift;
23251 if (is_signed(val->type)) {
23252 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23253 use_triple(val_shift, val_mask);
23254 use_triple(mask, val_mask);
23255 }
23256
23257 /* Mask the target value */
23258 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23259 use_triple(targ, targ_mask);
23260 use_triple(nmask, targ_mask);
23261
23262 /* Now combined them together */
23263 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23264 use_triple(targ_mask, new);
23265 use_triple(val_mask, new);
23266
23267 /* Move all of the users over to the new expression */
23268 propogate_use(state, ins, new);
23269
23270 /* Delete the original triple */
23271 release_triple(state, ins);
23272
23273 /* Restart the transformation at mask */
23274 return mask;
23275}
23276
23277static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23278{
23279 struct triple *mask, *shift;
23280 struct triple *val, *val_mask, *val_shift;
23281 ulong_t the_mask;
23282
23283 val = RHS(ins, 0);
23284
23285 /* Get constant for the mask value */
23286 the_mask = 1;
23287 the_mask <<= ins->u.bitfield.size;
23288 the_mask -= 1;
23289 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23290 mask->u.cval = the_mask;
23291
23292 /* Get constant for the right shift value */
23293 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23294 shift->u.cval = ins->u.bitfield.offset;
23295
23296 /* Shift arithmetic right, to correct the sign */
23297 val_shift = val;
23298 if (shift->u.cval != 0) {
23299 int op;
23300 if (ins->op == OP_SEXTRACT) {
23301 op = OP_SSR;
23302 } else {
23303 op = OP_USR;
23304 }
23305 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23306 use_triple(val, val_shift);
23307 use_triple(shift, val_shift);
23308 }
23309
23310 /* Finally mask the value */
23311 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23312 use_triple(val_shift, val_mask);
23313 use_triple(mask, val_mask);
23314
23315 /* Move all of the users over to the new expression */
23316 propogate_use(state, ins, val_mask);
23317
23318 /* Release the original instruction */
23319 release_triple(state, ins);
23320
23321 return mask;
23322
23323}
23324
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023325static struct triple *transform_to_arch_instruction(
23326 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023327{
23328 /* Transform from generic 3 address instructions
23329 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023330 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023331 * Copies are inserted to preserve the register flexibility
23332 * of 3 address instructions.
23333 */
Eric Biederman90089602004-05-28 14:11:54 +000023334 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023335 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023336 next = ins->next;
23337 switch(ins->op) {
23338 case OP_INTCONST:
23339 ins->template_id = TEMPLATE_INTCONST32;
23340 if (ins->u.cval < 256) {
23341 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023342 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023343 break;
23344 case OP_ADDRCONST:
23345 ins->template_id = TEMPLATE_INTCONST32;
23346 break;
Eric Biederman90089602004-05-28 14:11:54 +000023347 case OP_UNKNOWNVAL:
23348 ins->template_id = TEMPLATE_UNKNOWNVAL;
23349 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023350 case OP_NOOP:
23351 case OP_SDECL:
23352 case OP_BLOBCONST:
23353 case OP_LABEL:
23354 ins->template_id = TEMPLATE_NOP;
23355 break;
23356 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023357 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023358 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023359 value = RHS(ins, 0);
23360 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023361 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023362 }
Eric Biederman90089602004-05-28 14:11:54 +000023363 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023364 ins->template_id = TEMPLATE_COPY_IMM16;
23365 }
Eric Biederman90089602004-05-28 14:11:54 +000023366 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023367 ins->template_id = TEMPLATE_COPY_IMM32;
23368 }
Eric Biederman90089602004-05-28 14:11:54 +000023369 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023370 internal_error(state, ins, "bad constant passed to copy");
23371 }
Eric Biederman90089602004-05-28 14:11:54 +000023372 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023373 ins->template_id = TEMPLATE_COPY8_REG;
23374 }
Eric Biederman90089602004-05-28 14:11:54 +000023375 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023376 ins->template_id = TEMPLATE_COPY16_REG;
23377 }
Eric Biederman90089602004-05-28 14:11:54 +000023378 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023379 ins->template_id = TEMPLATE_COPY32_REG;
23380 }
23381 else {
23382 internal_error(state, ins, "bad type passed to copy");
23383 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023384 break;
23385 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023386 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023387 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023388 ins->template_id = TEMPLATE_PHI8;
23389 }
Eric Biederman90089602004-05-28 14:11:54 +000023390 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023391 ins->template_id = TEMPLATE_PHI16;
23392 }
Eric Biederman90089602004-05-28 14:11:54 +000023393 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023394 ins->template_id = TEMPLATE_PHI32;
23395 }
23396 else {
23397 internal_error(state, ins, "bad type passed to phi");
23398 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023399 break;
Eric Biederman90089602004-05-28 14:11:54 +000023400 case OP_ADECL:
23401 /* Adecls should always be treated as dead code and
23402 * removed. If we are not optimizing they may linger.
23403 */
23404 if (!noop_adecl(ins)) {
23405 internal_error(state, ins, "adecl remains?");
23406 }
23407 ins->template_id = TEMPLATE_NOP;
23408 next = after_lhs(state, ins);
23409 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023410 case OP_STORE:
23411 switch(ins->type->type & TYPE_MASK) {
23412 case TYPE_CHAR: case TYPE_UCHAR:
23413 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023414 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023415 case TYPE_SHORT: case TYPE_USHORT:
23416 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023417 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023418 case TYPE_INT: case TYPE_UINT:
23419 case TYPE_LONG: case TYPE_ULONG:
23420 case TYPE_POINTER:
23421 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023422 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023423 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023424 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023425 break;
23426 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023427 break;
23428 case OP_LOAD:
23429 switch(ins->type->type & TYPE_MASK) {
23430 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023431 case TYPE_SHORT: case TYPE_USHORT:
23432 case TYPE_INT: case TYPE_UINT:
23433 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023434 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023435 break;
23436 default:
23437 internal_error(state, ins, "unknown type in load");
23438 break;
23439 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023440 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023441 break;
23442 case OP_ADD:
23443 case OP_SUB:
23444 case OP_AND:
23445 case OP_XOR:
23446 case OP_OR:
23447 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023448 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023449 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023450 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023451 }
23452 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023453 case OP_SDIVT:
23454 case OP_UDIVT:
23455 ins->template_id = TEMPLATE_DIV32;
23456 next = after_lhs(state, ins);
23457 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023458 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023459 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023460 break;
23461 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023462 next = mod_div(state, ins, OP_UDIVT, 0);
23463 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023464 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023465 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023466 break;
23467 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023468 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023469 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023470 case OP_SMOD:
23471 next = mod_div(state, ins, OP_SDIVT, 1);
23472 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023473 case OP_SL:
23474 case OP_SSR:
23475 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023476 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023477 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023478 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023479 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23480 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023481 }
23482 break;
23483 case OP_INVERT:
23484 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023485 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023486 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023487 case OP_EQ:
23488 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023489 break;
23490 case OP_NOTEQ:
23491 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23492 break;
23493 case OP_SLESS:
23494 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23495 break;
23496 case OP_ULESS:
23497 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23498 break;
23499 case OP_SMORE:
23500 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23501 break;
23502 case OP_UMORE:
23503 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23504 break;
23505 case OP_SLESSEQ:
23506 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23507 break;
23508 case OP_ULESSEQ:
23509 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23510 break;
23511 case OP_SMOREEQ:
23512 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23513 break;
23514 case OP_UMOREEQ:
23515 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23516 break;
23517 case OP_LTRUE:
23518 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23519 break;
23520 case OP_LFALSE:
23521 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23522 break;
23523 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023524 ins->op = OP_JMP;
23525 ins->template_id = TEMPLATE_NOP;
23526 break;
23527 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023528 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023529 RHS(ins, 0)->type, RHS(ins, 0), 0);
23530 break;
23531 case OP_CALL:
23532 ins->template_id = TEMPLATE_NOP;
23533 break;
23534 case OP_RET:
23535 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023536 break;
23537 case OP_INB:
23538 case OP_INW:
23539 case OP_INL:
23540 switch(ins->op) {
23541 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23542 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23543 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23544 }
23545 if (get_imm8(ins, &RHS(ins, 0))) {
23546 ins->template_id += 1;
23547 }
23548 break;
23549 case OP_OUTB:
23550 case OP_OUTW:
23551 case OP_OUTL:
23552 switch(ins->op) {
23553 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23554 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23555 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23556 }
23557 if (get_imm8(ins, &RHS(ins, 1))) {
23558 ins->template_id += 1;
23559 }
23560 break;
23561 case OP_BSF:
23562 case OP_BSR:
23563 ins->template_id = TEMPLATE_BSF;
23564 break;
23565 case OP_RDMSR:
23566 ins->template_id = TEMPLATE_RDMSR;
23567 next = after_lhs(state, ins);
23568 break;
23569 case OP_WRMSR:
23570 ins->template_id = TEMPLATE_WRMSR;
23571 break;
23572 case OP_HLT:
23573 ins->template_id = TEMPLATE_NOP;
23574 break;
23575 case OP_ASM:
23576 ins->template_id = TEMPLATE_NOP;
23577 next = after_lhs(state, ins);
23578 break;
23579 /* Already transformed instructions */
23580 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023581 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023582 break;
23583 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023584 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023585 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023586 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023587 }
23588 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023589 case OP_JMP:
23590 ins->template_id = TEMPLATE_NOP;
23591 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023592 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23593 case OP_JMP_SLESS: case OP_JMP_ULESS:
23594 case OP_JMP_SMORE: case OP_JMP_UMORE:
23595 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23596 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23597 ins->template_id = TEMPLATE_JMP;
23598 break;
23599 case OP_SET_EQ: case OP_SET_NOTEQ:
23600 case OP_SET_SLESS: case OP_SET_ULESS:
23601 case OP_SET_SMORE: case OP_SET_UMORE:
23602 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23603 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23604 ins->template_id = TEMPLATE_SET;
23605 break;
Eric Biederman90089602004-05-28 14:11:54 +000023606 case OP_DEPOSIT:
23607 next = x86_deposit(state, ins);
23608 break;
23609 case OP_SEXTRACT:
23610 case OP_UEXTRACT:
23611 next = x86_extract(state, ins);
23612 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023613 /* Unhandled instructions */
23614 case OP_PIECE:
23615 default:
Eric Biederman90089602004-05-28 14:11:54 +000023616 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023617 ins->op, tops(ins->op));
23618 break;
23619 }
23620 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023621}
23622
Eric Biederman530b5192003-07-01 10:05:30 +000023623static long next_label(struct compile_state *state)
23624{
Eric Biederman90089602004-05-28 14:11:54 +000023625 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023626 return ++label_counter;
23627}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023628static void generate_local_labels(struct compile_state *state)
23629{
23630 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023631 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023632 label = first;
23633 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023634 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023635 (label->op == OP_SDECL)) {
23636 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023637 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023638 } else {
23639 label->u.cval = 0;
23640 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023641
Eric Biedermanb138ac82003-04-22 18:44:01 +000023642 }
23643 label = label->next;
23644 } while(label != first);
23645}
23646
Stefan Reinauer14e22772010-04-27 06:56:47 +000023647static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023648 struct triple *triple, int classes)
23649{
23650 unsigned mask;
23651 int reg;
23652 reg = ID_REG(triple->id);
23653 if (reg == REG_UNSET) {
23654 internal_error(state, triple, "register not set");
23655 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023656 mask = arch_reg_regcm(state, reg);
23657 if (!(classes & mask)) {
23658 internal_error(state, triple, "reg %d in wrong class",
23659 reg);
23660 }
23661 return reg;
23662}
23663
Eric Biederman90089602004-05-28 14:11:54 +000023664
Eric Biederman530b5192003-07-01 10:05:30 +000023665#if REG_XMM7 != 44
23666#error "Registers have renumberd fix arch_reg_str"
23667#endif
Eric Biederman90089602004-05-28 14:11:54 +000023668static const char *arch_regs[] = {
23669 "%unset",
23670 "%unneeded",
23671 "%eflags",
23672 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23673 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23674 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23675 "%edx:%eax",
23676 "%dx:%ax",
23677 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023678 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023679 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23680};
23681static const char *arch_reg_str(int reg)
23682{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023683 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23684 reg = 0;
23685 }
Eric Biederman90089602004-05-28 14:11:54 +000023686 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023687}
23688
23689static const char *reg(struct compile_state *state, struct triple *triple,
23690 int classes)
23691{
23692 int reg;
23693 reg = check_reg(state, triple, classes);
23694 return arch_reg_str(reg);
23695}
23696
Eric Biederman90089602004-05-28 14:11:54 +000023697static int arch_reg_size(int reg)
23698{
23699 int size;
23700 size = 0;
23701 if (reg == REG_EFLAGS) {
23702 size = 32;
23703 }
23704 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23705 size = 8;
23706 }
23707 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23708 size = 16;
23709 }
23710 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23711 size = 32;
23712 }
23713 else if (reg == REG_EDXEAX) {
23714 size = 64;
23715 }
23716 else if (reg == REG_DXAX) {
23717 size = 32;
23718 }
23719 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23720 size = 64;
23721 }
23722 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23723 size = 128;
23724 }
23725 return size;
23726}
23727
23728static int reg_size(struct compile_state *state, struct triple *ins)
23729{
23730 int reg;
23731 reg = ID_REG(ins->id);
23732 if (reg == REG_UNSET) {
23733 internal_error(state, ins, "register not set");
23734 }
23735 return arch_reg_size(reg);
23736}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023737
Eric Biederman90089602004-05-28 14:11:54 +000023738
23739
Eric Biedermanb138ac82003-04-22 18:44:01 +000023740const char *type_suffix(struct compile_state *state, struct type *type)
23741{
23742 const char *suffix;
23743 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023744 case SIZEOF_I8: suffix = "b"; break;
23745 case SIZEOF_I16: suffix = "w"; break;
23746 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023747 default:
23748 internal_error(state, 0, "unknown suffix");
23749 suffix = 0;
23750 break;
23751 }
23752 return suffix;
23753}
23754
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023755static void print_const_val(
23756 struct compile_state *state, struct triple *ins, FILE *fp)
23757{
23758 switch(ins->op) {
23759 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023760 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023761 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023762 break;
23763 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023764 if ((MISC(ins, 0)->op != OP_SDECL) &&
23765 (MISC(ins, 0)->op != OP_LABEL))
23766 {
Eric Biederman830c9882003-07-04 00:27:33 +000023767 internal_error(state, ins, "bad base for addrconst");
23768 }
23769 if (MISC(ins, 0)->u.cval <= 0) {
23770 internal_error(state, ins, "unlabeled constant");
23771 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023772 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023773 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023774 (unsigned long)(MISC(ins, 0)->u.cval),
23775 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023776 break;
23777 default:
23778 internal_error(state, ins, "unknown constant type");
23779 break;
23780 }
23781}
23782
Eric Biederman530b5192003-07-01 10:05:30 +000023783static void print_const(struct compile_state *state,
23784 struct triple *ins, FILE *fp)
23785{
23786 switch(ins->op) {
23787 case OP_INTCONST:
23788 switch(ins->type->type & TYPE_MASK) {
23789 case TYPE_CHAR:
23790 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023791 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023792 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023793 break;
23794 case TYPE_SHORT:
23795 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023796 fprintf(fp, ".short 0x%04lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023797 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023798 break;
23799 case TYPE_INT:
23800 case TYPE_UINT:
23801 case TYPE_LONG:
23802 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023803 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023804 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023805 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023806 break;
23807 default:
Eric Biederman90089602004-05-28 14:11:54 +000023808 fprintf(state->errout, "type: ");
23809 name_of(state->errout, ins->type);
23810 fprintf(state->errout, "\n");
23811 internal_error(state, ins, "Unknown constant type. Val: %lu",
23812 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023813 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023814
Eric Biederman530b5192003-07-01 10:05:30 +000023815 break;
23816 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023817 if ((MISC(ins, 0)->op != OP_SDECL) &&
23818 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023819 internal_error(state, ins, "bad base for addrconst");
23820 }
23821 if (MISC(ins, 0)->u.cval <= 0) {
23822 internal_error(state, ins, "unlabeled constant");
23823 }
23824 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023825 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023826 (unsigned long)(MISC(ins, 0)->u.cval),
23827 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023828 break;
23829 case OP_BLOBCONST:
23830 {
23831 unsigned char *blob;
23832 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023833 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023834 blob = ins->u.blob;
23835 for(i = 0; i < size; i++) {
23836 fprintf(fp, ".byte 0x%02x\n",
23837 blob[i]);
23838 }
23839 break;
23840 }
23841 default:
23842 internal_error(state, ins, "Unknown constant type");
23843 break;
23844 }
23845}
23846
23847#define TEXT_SECTION ".rom.text"
23848#define DATA_SECTION ".rom.data"
23849
23850static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023851 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023852{
Eric Biederman90089602004-05-28 14:11:54 +000023853 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023854 long ref;
23855 ref = next_label(state);
23856 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023857 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023858 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023859 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023860 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23861 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023862 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023863 }
Eric Biederman530b5192003-07-01 10:05:30 +000023864 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23865 return ref;
23866}
23867
Eric Biederman90089602004-05-28 14:11:54 +000023868static long get_mask_pool_ref(
23869 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23870{
23871 long ref;
23872 if (mask == 0xff) {
23873 ref = 1;
23874 }
23875 else if (mask == 0xffff) {
23876 ref = 2;
23877 }
23878 else {
23879 ref = 0;
23880 internal_error(state, ins, "unhandled mask value");
23881 }
23882 return ref;
23883}
23884
Eric Biedermanb138ac82003-04-22 18:44:01 +000023885static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023886 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023887{
23888 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023889 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023890 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023891 internal_error(state, ins, "invalid register assignment");
23892 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023893 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023894 fprintf(fp, "\t%s ", op);
23895 print_const_val(state, RHS(ins, 1), fp);
23896 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023897 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023898 }
23899 else {
23900 unsigned lmask, rmask;
23901 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023902 lreg = check_reg(state, RHS(ins, 0), mask);
23903 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023904 lmask = arch_reg_regcm(state, lreg);
23905 rmask = arch_reg_regcm(state, rreg);
23906 mask = lmask & rmask;
23907 fprintf(fp, "\t%s %s, %s\n",
23908 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023909 reg(state, RHS(ins, 1), mask),
23910 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023911 }
23912}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023913static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023914 const char *op, struct triple *ins, FILE *fp)
23915{
23916 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023917 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023918 fprintf(fp, "\t%s %s\n",
23919 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023920 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023921}
23922
23923static void print_op_shift(struct compile_state *state,
23924 const char *op, struct triple *ins, FILE *fp)
23925{
23926 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023927 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023928 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023929 internal_error(state, ins, "invalid register assignment");
23930 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023931 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023932 fprintf(fp, "\t%s ", op);
23933 print_const_val(state, RHS(ins, 1), fp);
23934 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023935 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023936 }
23937 else {
23938 fprintf(fp, "\t%s %s, %s\n",
23939 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023940 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023941 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023942 }
23943}
23944
23945static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23946{
23947 const char *op;
23948 int mask;
23949 int dreg;
23950 mask = 0;
23951 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023952 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023953 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23954 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23955 default:
23956 internal_error(state, ins, "not an in operation");
23957 op = 0;
23958 break;
23959 }
23960 dreg = check_reg(state, ins, mask);
23961 if (!reg_is_reg(state, dreg, REG_EAX)) {
23962 internal_error(state, ins, "dst != %%eax");
23963 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023964 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023965 fprintf(fp, "\t%s ", op);
23966 print_const_val(state, RHS(ins, 0), fp);
23967 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023968 reg(state, ins, mask));
23969 }
23970 else {
23971 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023972 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023973 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23974 internal_error(state, ins, "src != %%dx");
23975 }
23976 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023977 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023978 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023979 reg(state, ins, mask));
23980 }
23981}
23982
23983static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23984{
23985 const char *op;
23986 int mask;
23987 int lreg;
23988 mask = 0;
23989 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023990 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023991 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23992 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23993 default:
23994 internal_error(state, ins, "not an out operation");
23995 op = 0;
23996 break;
23997 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023998 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023999 if (!reg_is_reg(state, lreg, REG_EAX)) {
24000 internal_error(state, ins, "src != %%eax");
24001 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024002 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024003 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024004 op, reg(state, RHS(ins, 0), mask));
24005 print_const_val(state, RHS(ins, 1), fp);
24006 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024007 }
24008 else {
24009 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024010 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024011 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24012 internal_error(state, ins, "dst != %%dx");
24013 }
24014 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024015 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024016 reg(state, RHS(ins, 0), mask),
24017 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024018 }
24019}
24020
24021static void print_op_move(struct compile_state *state,
24022 struct triple *ins, FILE *fp)
24023{
24024 /* op_move is complex because there are many types
24025 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024026 * Because OP_COPY will be introduced in arbitrary locations
24027 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024028 * OP_CONVERT can change the flags and it is the only operation
24029 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024030 */
24031 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24032 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024033 if (state->arch->features & X86_NOOP_COPY) {
24034 omit_copy = 0;
24035 }
24036 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024037 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024038 dst = ins;
24039 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024040 else {
24041 internal_error(state, ins, "unknown move operation");
24042 src = dst = 0;
24043 }
Eric Biederman90089602004-05-28 14:11:54 +000024044 if (reg_size(state, dst) < size_of(state, dst->type)) {
24045 internal_error(state, ins, "Invalid destination register");
24046 }
24047 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24048 fprintf(state->errout, "src type: ");
24049 name_of(state->errout, src->type);
24050 fprintf(state->errout, "\n");
24051 fprintf(state->errout, "dst type: ");
24052 name_of(state->errout, dst->type);
24053 fprintf(state->errout, "\n");
24054 internal_error(state, ins, "Type mismatch for OP_COPY");
24055 }
24056
Eric Biederman0babc1c2003-05-09 02:39:00 +000024057 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024058 int src_reg, dst_reg;
24059 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024060 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024061 dst_reg = ID_REG(dst->id);
24062 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024063 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024064 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024065 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024066 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024067 if ((src_reg != dst_reg) || !omit_copy) {
24068 fprintf(fp, "\tmov %s, %s\n",
24069 reg(state, src, src_regcm),
24070 reg(state, dst, dst_regcm));
24071 }
24072 }
24073 /* Move 32bit to 16bit */
24074 else if ((src_regcm & REGCM_GPR32) &&
24075 (dst_regcm & REGCM_GPR16)) {
24076 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24077 if ((src_reg != dst_reg) || !omit_copy) {
24078 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024079 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024080 arch_reg_str(dst_reg));
24081 }
24082 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024083 /* Move from 32bit gprs to 16bit gprs */
24084 else if ((src_regcm & REGCM_GPR32) &&
24085 (dst_regcm & REGCM_GPR16)) {
24086 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24087 if ((src_reg != dst_reg) || !omit_copy) {
24088 fprintf(fp, "\tmov %s, %s\n",
24089 arch_reg_str(src_reg),
24090 arch_reg_str(dst_reg));
24091 }
24092 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024093 /* Move 32bit to 8bit */
24094 else if ((src_regcm & REGCM_GPR32_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_GPR32_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 16bit to 8bit */
24105 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024106 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024107 {
24108 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24109 if ((src_reg != dst_reg) || !omit_copy) {
24110 fprintf(fp, "\tmovb %s, %s\n",
24111 arch_reg_str(src_reg),
24112 arch_reg_str(dst_reg));
24113 }
24114 }
24115 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024116 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024117 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024118 const char *op;
24119 op = is_signed(src->type)? "movsx": "movzx";
24120 fprintf(fp, "\t%s %s, %s\n",
24121 op,
24122 reg(state, src, src_regcm),
24123 reg(state, dst, dst_regcm));
24124 }
24125 /* Move between sse registers */
24126 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24127 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024128 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024129 reg(state, src, src_regcm),
24130 reg(state, dst, dst_regcm));
24131 }
24132 }
Eric Biederman530b5192003-07-01 10:05:30 +000024133 /* Move between mmx registers */
24134 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024135 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024136 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024137 reg(state, src, src_regcm),
24138 reg(state, dst, dst_regcm));
24139 }
24140 }
Eric Biederman530b5192003-07-01 10:05:30 +000024141 /* Move from sse to mmx registers */
24142 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24143 fprintf(fp, "\tmovdq2q %s, %s\n",
24144 reg(state, src, src_regcm),
24145 reg(state, dst, dst_regcm));
24146 }
24147 /* Move from mmx to sse registers */
24148 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24149 fprintf(fp, "\tmovq2dq %s, %s\n",
24150 reg(state, src, src_regcm),
24151 reg(state, dst, dst_regcm));
24152 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024153 /* Move between 32bit gprs & mmx/sse registers */
24154 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24155 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24156 fprintf(fp, "\tmovd %s, %s\n",
24157 reg(state, src, src_regcm),
24158 reg(state, dst, dst_regcm));
24159 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024160 /* Move from 16bit gprs & mmx/sse registers */
24161 else if ((src_regcm & REGCM_GPR16) &&
24162 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24163 const char *op;
24164 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024165 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024166 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24167 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24168 op,
24169 arch_reg_str(src_reg),
24170 arch_reg_str(mid_reg),
24171 arch_reg_str(mid_reg),
24172 arch_reg_str(dst_reg));
24173 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024174 /* Move from mmx/sse registers to 16bit gprs */
24175 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24176 (dst_regcm & REGCM_GPR16)) {
24177 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24178 fprintf(fp, "\tmovd %s, %s\n",
24179 arch_reg_str(src_reg),
24180 arch_reg_str(dst_reg));
24181 }
Eric Biederman530b5192003-07-01 10:05:30 +000024182 /* Move from gpr to 64bit dividend */
24183 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24184 (dst_regcm & REGCM_DIVIDEND64)) {
24185 const char *extend;
24186 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24187 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024188 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024189 extend);
24190 }
24191 /* Move from 64bit gpr to gpr */
24192 else if ((src_regcm & REGCM_DIVIDEND64) &&
24193 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24194 if (dst_regcm & REGCM_GPR32) {
24195 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024196 }
Eric Biederman530b5192003-07-01 10:05:30 +000024197 else if (dst_regcm & REGCM_GPR16) {
24198 src_reg = REG_AX;
24199 }
24200 else if (dst_regcm & REGCM_GPR8_LO) {
24201 src_reg = REG_AL;
24202 }
24203 fprintf(fp, "\tmov %s, %s\n",
24204 arch_reg_str(src_reg),
24205 arch_reg_str(dst_reg));
24206 }
24207 /* Move from mmx/sse registers to 64bit gpr */
24208 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24209 (dst_regcm & REGCM_DIVIDEND64)) {
24210 const char *extend;
24211 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24212 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24213 arch_reg_str(src_reg),
24214 extend);
24215 }
24216 /* Move from 64bit gpr to mmx/sse register */
24217 else if ((src_regcm & REGCM_DIVIDEND64) &&
24218 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24219 fprintf(fp, "\tmovd %%eax, %s\n",
24220 arch_reg_str(dst_reg));
24221 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024222#if X86_4_8BIT_GPRS
24223 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024224 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024225 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24226 const char *op;
24227 int mid_reg;
24228 op = is_signed(src->type)? "movsx":"movzx";
24229 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24230 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24231 op,
24232 reg(state, src, src_regcm),
24233 arch_reg_str(mid_reg),
24234 arch_reg_str(mid_reg),
24235 reg(state, dst, dst_regcm));
24236 }
24237 /* Move from mmx/sse registers and 8bit gprs */
24238 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024239 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024240 int mid_reg;
24241 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24242 fprintf(fp, "\tmovd %s, %s\n",
24243 reg(state, src, src_regcm),
24244 arch_reg_str(mid_reg));
24245 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024246 /* Move from 32bit gprs to 8bit gprs */
24247 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024248 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024249 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24250 if ((src_reg != dst_reg) || !omit_copy) {
24251 fprintf(fp, "\tmov %s, %s\n",
24252 arch_reg_str(src_reg),
24253 arch_reg_str(dst_reg));
24254 }
24255 }
24256 /* Move from 16bit gprs to 8bit gprs */
24257 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024258 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024259 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24260 if ((src_reg != dst_reg) || !omit_copy) {
24261 fprintf(fp, "\tmov %s, %s\n",
24262 arch_reg_str(src_reg),
24263 arch_reg_str(dst_reg));
24264 }
24265 }
24266#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024267 /* Move from %eax:%edx to %eax:%edx */
24268 else if ((src_regcm & REGCM_DIVIDEND64) &&
24269 (dst_regcm & REGCM_DIVIDEND64) &&
24270 (src_reg == dst_reg)) {
24271 if (!omit_copy) {
24272 fprintf(fp, "\t/*mov %s, %s*/\n",
24273 arch_reg_str(src_reg),
24274 arch_reg_str(dst_reg));
24275 }
24276 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024277 else {
Eric Biederman90089602004-05-28 14:11:54 +000024278 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24279 internal_error(state, ins, "attempt to copy from %%eflags!");
24280 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024281 internal_error(state, ins, "unknown copy type");
24282 }
24283 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024284 else {
Eric Biederman90089602004-05-28 14:11:54 +000024285 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024286 int dst_reg;
24287 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024288 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024289 dst_reg = ID_REG(dst->id);
24290 dst_regcm = arch_reg_regcm(state, dst_reg);
24291 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24292 fprintf(fp, "\tmov ");
24293 print_const_val(state, src, fp);
24294 fprintf(fp, ", %s\n",
24295 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24296 }
24297 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024298 if (dst_size > SIZEOF_I32) {
24299 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024300 }
24301 fprintf(fp, "\tmov $0, %%edx\n");
24302 fprintf(fp, "\tmov ");
24303 print_const_val(state, src, fp);
24304 fprintf(fp, ", %%eax\n");
24305 }
24306 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024307 if (dst_size > SIZEOF_I16) {
24308 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024309 }
24310 fprintf(fp, "\tmov $0, %%dx\n");
24311 fprintf(fp, "\tmov ");
24312 print_const_val(state, src, fp);
24313 fprintf(fp, ", %%ax");
24314 }
24315 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24316 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024317 if (dst_size > SIZEOF_I32) {
24318 internal_error(state, ins, "%d bit constant...", dst_size);
24319 }
24320 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024321 fprintf(fp, "\tmovd L%s%lu, %s\n",
24322 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024323 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24324 }
24325 else {
24326 internal_error(state, ins, "unknown copy immediate type");
24327 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024328 }
Eric Biederman90089602004-05-28 14:11:54 +000024329 /* Leave now if this is not a type conversion */
24330 if (ins->op != OP_CONVERT) {
24331 return;
24332 }
24333 /* Now make certain I have not logically overflowed the destination */
24334 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24335 (size_of(state, dst->type) < reg_size(state, dst)))
24336 {
24337 unsigned long mask;
24338 int dst_reg;
24339 int dst_regcm;
24340 if (size_of(state, dst->type) >= 32) {
24341 fprintf(state->errout, "dst type: ");
24342 name_of(state->errout, dst->type);
24343 fprintf(state->errout, "\n");
24344 internal_error(state, dst, "unhandled dst type size");
24345 }
24346 mask = 1;
24347 mask <<= size_of(state, dst->type);
24348 mask -= 1;
24349
24350 dst_reg = ID_REG(dst->id);
24351 dst_regcm = arch_reg_regcm(state, dst_reg);
24352
24353 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24354 fprintf(fp, "\tand $0x%lx, %s\n",
24355 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24356 }
24357 else if (dst_regcm & REGCM_MMX) {
24358 long ref;
24359 ref = get_mask_pool_ref(state, dst, mask, fp);
24360 fprintf(fp, "\tpand L%s%lu, %s\n",
24361 state->compiler->label_prefix, ref,
24362 reg(state, dst, REGCM_MMX));
24363 }
24364 else if (dst_regcm & REGCM_XMM) {
24365 long ref;
24366 ref = get_mask_pool_ref(state, dst, mask, fp);
24367 fprintf(fp, "\tpand L%s%lu, %s\n",
24368 state->compiler->label_prefix, ref,
24369 reg(state, dst, REGCM_XMM));
24370 }
24371 else {
24372 fprintf(state->errout, "dst type: ");
24373 name_of(state->errout, dst->type);
24374 fprintf(state->errout, "\n");
24375 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24376 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24377 }
24378 }
24379 /* Make certain I am properly sign extended */
24380 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24381 (is_signed(src->type)))
24382 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024383 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024384 int dst_reg;
24385 int dst_regcm;
24386
Eric Biederman90089602004-05-28 14:11:54 +000024387 reg_bits = reg_size(state, dst);
24388 if (reg_bits > 32) {
24389 reg_bits = 32;
24390 }
24391 shift_bits = reg_bits - size_of(state, src->type);
24392 dst_reg = ID_REG(dst->id);
24393 dst_regcm = arch_reg_regcm(state, dst_reg);
24394
24395 if (shift_bits < 0) {
24396 internal_error(state, dst, "negative shift?");
24397 }
24398
24399 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024400 fprintf(fp, "\tshl $%d, %s\n",
24401 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024402 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024403 fprintf(fp, "\tsar $%d, %s\n",
24404 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024405 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24406 }
24407 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24408 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024409 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024410 reg(state, dst, REGCM_MMX | REGCM_XMM));
24411 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024412 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024413 reg(state, dst, REGCM_MMX | REGCM_XMM));
24414 }
24415 else {
24416 fprintf(state->errout, "dst type: ");
24417 name_of(state->errout, dst->type);
24418 fprintf(state->errout, "\n");
24419 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24420 internal_error(state, dst, "failed to signed extend value");
24421 }
24422 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024423}
24424
24425static void print_op_load(struct compile_state *state,
24426 struct triple *ins, FILE *fp)
24427{
24428 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024429 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024430 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024431 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024432 if (is_const(src) || is_const(dst)) {
24433 internal_error(state, ins, "unknown load operation");
24434 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024435 switch(ins->type->type & TYPE_MASK) {
24436 case TYPE_CHAR: op = "movsbl"; break;
24437 case TYPE_UCHAR: op = "movzbl"; break;
24438 case TYPE_SHORT: op = "movswl"; break;
24439 case TYPE_USHORT: op = "movzwl"; break;
24440 case TYPE_INT: case TYPE_UINT:
24441 case TYPE_LONG: case TYPE_ULONG:
24442 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024443 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024444 break;
24445 default:
24446 internal_error(state, ins, "unknown type in load");
24447 op = "<invalid opcode>";
24448 break;
24449 }
24450 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024451 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024452 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024453 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024454}
24455
24456
24457static void print_op_store(struct compile_state *state,
24458 struct triple *ins, FILE *fp)
24459{
24460 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024461 dst = RHS(ins, 0);
24462 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024463 if (is_const(src) && (src->op == OP_INTCONST)) {
24464 long_t value;
24465 value = (long_t)(src->u.cval);
24466 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24467 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024468 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024469 reg(state, dst, REGCM_GPR32));
24470 }
24471 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24472 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24473 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024474 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024475 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024476 }
24477 else {
24478 if (is_const(src) || is_const(dst)) {
24479 internal_error(state, ins, "unknown store operation");
24480 }
24481 fprintf(fp, "\tmov%s %s, (%s)\n",
24482 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024483 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024484 reg(state, dst, REGCM_GPR32));
24485 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024486
24487
Eric Biedermanb138ac82003-04-22 18:44:01 +000024488}
24489
24490static void print_op_smul(struct compile_state *state,
24491 struct triple *ins, FILE *fp)
24492{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024493 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024494 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024495 reg(state, RHS(ins, 1), REGCM_GPR32),
24496 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024497 }
24498 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024499 fprintf(fp, "\timul ");
24500 print_const_val(state, RHS(ins, 1), fp);
24501 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024502 }
24503}
24504
24505static void print_op_cmp(struct compile_state *state,
24506 struct triple *ins, FILE *fp)
24507{
24508 unsigned mask;
24509 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024510 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024511 dreg = check_reg(state, ins, REGCM_FLAGS);
24512 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24513 internal_error(state, ins, "bad dest register for cmp");
24514 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024515 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024516 fprintf(fp, "\tcmp ");
24517 print_const_val(state, RHS(ins, 1), fp);
24518 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024519 }
24520 else {
24521 unsigned lmask, rmask;
24522 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024523 lreg = check_reg(state, RHS(ins, 0), mask);
24524 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024525 lmask = arch_reg_regcm(state, lreg);
24526 rmask = arch_reg_regcm(state, rreg);
24527 mask = lmask & rmask;
24528 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024529 reg(state, RHS(ins, 1), mask),
24530 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024531 }
24532}
24533
24534static void print_op_test(struct compile_state *state,
24535 struct triple *ins, FILE *fp)
24536{
24537 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024538 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024539 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024540 reg(state, RHS(ins, 0), mask),
24541 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024542}
24543
24544static void print_op_branch(struct compile_state *state,
24545 struct triple *branch, FILE *fp)
24546{
24547 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024548 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024549 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024550 internal_error(state, branch, "jmp with condition?");
24551 }
24552 bop = "jmp";
24553 }
24554 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024555 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024556 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024557 internal_error(state, branch, "jmpcc without condition?");
24558 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024559 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24560 if ((RHS(branch, 0)->op != OP_CMP) &&
24561 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024562 internal_error(state, branch, "bad branch test");
24563 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024564#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024565#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024566#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024567 ptr = RHS(branch, 0);
24568 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24569 if (ptr->op != OP_COPY) {
24570 internal_error(state, branch, "branch does not follow test");
24571 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024572 }
24573 switch(branch->op) {
24574 case OP_JMP_EQ: bop = "jz"; break;
24575 case OP_JMP_NOTEQ: bop = "jnz"; break;
24576 case OP_JMP_SLESS: bop = "jl"; break;
24577 case OP_JMP_ULESS: bop = "jb"; break;
24578 case OP_JMP_SMORE: bop = "jg"; break;
24579 case OP_JMP_UMORE: bop = "ja"; break;
24580 case OP_JMP_SLESSEQ: bop = "jle"; break;
24581 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24582 case OP_JMP_SMOREEQ: bop = "jge"; break;
24583 case OP_JMP_UMOREEQ: bop = "jae"; break;
24584 default:
24585 internal_error(state, branch, "Invalid branch op");
24586 break;
24587 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024588
Eric Biedermanb138ac82003-04-22 18:44:01 +000024589 }
Eric Biederman90089602004-05-28 14:11:54 +000024590#if 1
24591 if (branch->op == OP_CALL) {
24592 fprintf(fp, "\t/* call */\n");
24593 }
24594#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024595 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024596 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024597 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024598 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024599}
24600
Eric Biederman5ade04a2003-10-22 04:03:46 +000024601static void print_op_ret(struct compile_state *state,
24602 struct triple *branch, FILE *fp)
24603{
24604 fprintf(fp, "\tjmp *%s\n",
24605 reg(state, RHS(branch, 0), REGCM_GPR32));
24606}
24607
Eric Biedermanb138ac82003-04-22 18:44:01 +000024608static void print_op_set(struct compile_state *state,
24609 struct triple *set, FILE *fp)
24610{
24611 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024612 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024613 internal_error(state, set, "setcc without condition?");
24614 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024615 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24616 if ((RHS(set, 0)->op != OP_CMP) &&
24617 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024618 internal_error(state, set, "bad set test");
24619 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024620 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024621 internal_error(state, set, "set does not follow test");
24622 }
24623 switch(set->op) {
24624 case OP_SET_EQ: sop = "setz"; break;
24625 case OP_SET_NOTEQ: sop = "setnz"; break;
24626 case OP_SET_SLESS: sop = "setl"; break;
24627 case OP_SET_ULESS: sop = "setb"; break;
24628 case OP_SET_SMORE: sop = "setg"; break;
24629 case OP_SET_UMORE: sop = "seta"; break;
24630 case OP_SET_SLESSEQ: sop = "setle"; break;
24631 case OP_SET_ULESSEQ: sop = "setbe"; break;
24632 case OP_SET_SMOREEQ: sop = "setge"; break;
24633 case OP_SET_UMOREEQ: sop = "setae"; break;
24634 default:
24635 internal_error(state, set, "Invalid set op");
24636 break;
24637 }
24638 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024639 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024640}
24641
Stefan Reinauer14e22772010-04-27 06:56:47 +000024642static void print_op_bit_scan(struct compile_state *state,
24643 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024644{
24645 const char *op;
24646 switch(ins->op) {
24647 case OP_BSF: op = "bsf"; break;
24648 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024649 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024650 internal_error(state, ins, "unknown bit scan");
24651 op = 0;
24652 break;
24653 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024654 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024655 "\t%s %s, %s\n"
24656 "\tjnz 1f\n"
24657 "\tmovl $-1, %s\n"
24658 "1:\n",
24659 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024660 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024661 reg(state, ins, REGCM_GPR32),
24662 reg(state, ins, REGCM_GPR32));
24663}
24664
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024665
Eric Biedermanb138ac82003-04-22 18:44:01 +000024666static void print_sdecl(struct compile_state *state,
24667 struct triple *ins, FILE *fp)
24668{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024669 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024670 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024671 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024672 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024673 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024674 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024675
Eric Biedermanb138ac82003-04-22 18:44:01 +000024676}
24677
24678static void print_instruction(struct compile_state *state,
24679 struct triple *ins, FILE *fp)
24680{
24681 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024682 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024683 */
24684 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024685 case OP_ASM:
24686 print_op_asm(state, ins, fp);
24687 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024688 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24689 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24690 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24691 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24692 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24693 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24694 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24695 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24696 case OP_POS: break;
24697 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24698 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024699 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024700 case OP_INTCONST:
24701 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024702 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024703 /* Don't generate anything here for constants */
24704 case OP_PHI:
24705 /* Don't generate anything for variable declarations. */
24706 break;
Eric Biederman90089602004-05-28 14:11:54 +000024707 case OP_UNKNOWNVAL:
24708 fprintf(fp, " /* unknown %s */\n",
24709 reg(state, ins, REGCM_ALL));
24710 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024711 case OP_SDECL:
24712 print_sdecl(state, ins, fp);
24713 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024714 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024715 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024716 print_op_move(state, ins, fp);
24717 break;
24718 case OP_LOAD:
24719 print_op_load(state, ins, fp);
24720 break;
24721 case OP_STORE:
24722 print_op_store(state, ins, fp);
24723 break;
24724 case OP_SMUL:
24725 print_op_smul(state, ins, fp);
24726 break;
24727 case OP_CMP: print_op_cmp(state, ins, fp); break;
24728 case OP_TEST: print_op_test(state, ins, fp); break;
24729 case OP_JMP:
24730 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24731 case OP_JMP_SLESS: case OP_JMP_ULESS:
24732 case OP_JMP_SMORE: case OP_JMP_UMORE:
24733 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24734 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024735 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024736 print_op_branch(state, ins, fp);
24737 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024738 case OP_RET:
24739 print_op_ret(state, ins, fp);
24740 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024741 case OP_SET_EQ: case OP_SET_NOTEQ:
24742 case OP_SET_SLESS: case OP_SET_ULESS:
24743 case OP_SET_SMORE: case OP_SET_UMORE:
24744 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24745 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24746 print_op_set(state, ins, fp);
24747 break;
24748 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024749 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024750 break;
24751 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024752 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024753 break;
24754 case OP_BSF:
24755 case OP_BSR:
24756 print_op_bit_scan(state, ins, fp);
24757 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024758 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024759 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024760 fprintf(fp, "\trdmsr\n");
24761 break;
24762 case OP_WRMSR:
24763 fprintf(fp, "\twrmsr\n");
24764 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024765 case OP_HLT:
24766 fprintf(fp, "\thlt\n");
24767 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024768 case OP_SDIVT:
24769 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24770 break;
24771 case OP_UDIVT:
24772 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24773 break;
24774 case OP_UMUL:
24775 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24776 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024777 case OP_LABEL:
24778 if (!ins->use) {
24779 return;
24780 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024781 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024782 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024783 break;
Eric Biederman90089602004-05-28 14:11:54 +000024784 case OP_ADECL:
24785 /* Ignore adecls with no registers error otherwise */
24786 if (!noop_adecl(ins)) {
24787 internal_error(state, ins, "adecl remains?");
24788 }
24789 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024790 /* Ignore OP_PIECE */
24791 case OP_PIECE:
24792 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024793 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024794 case OP_SDIV: case OP_UDIV:
24795 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024796 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24797 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24798 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24799 default:
24800 internal_error(state, ins, "unknown op: %d %s",
24801 ins->op, tops(ins->op));
24802 break;
24803 }
24804}
24805
24806static void print_instructions(struct compile_state *state)
24807{
24808 struct triple *first, *ins;
24809 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024810 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024811 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024812 int max_inline_depth;
24813 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024814 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024815 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024816 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024817 /* Masks for common sizes */
24818 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24819 fprintf(fp, ".balign 16\n");
24820 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24821 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24822 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24823 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024824 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024825 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024826 ins = first;
24827 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024828 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024829 last_occurance != ins->occurance) {
24830 if (!ins->occurance->parent) {
24831 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024832 ins->occurance->function?ins->occurance->function:"(null)",
24833 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024834 ins->occurance->line,
24835 ins->occurance->col);
24836 }
24837 else {
24838 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024839 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024840 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024841 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024842 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024843 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024844 fprintf(fp, "\t * %s,%s:%d.%d\n",
24845 ptr->function,
24846 ptr->filename,
24847 ptr->line,
24848 ptr->col);
24849 }
24850 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024851 if (inline_depth > max_inline_depth) {
24852 max_inline_depth = inline_depth;
24853 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024854 }
24855 if (last_occurance) {
24856 put_occurance(last_occurance);
24857 }
24858 get_occurance(ins->occurance);
24859 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024860 }
24861
24862 print_instruction(state, ins, fp);
24863 ins = ins->next;
24864 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024865 if (print_location) {
24866 fprintf(fp, "/* max inline depth %d */\n",
24867 max_inline_depth);
24868 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024869}
Eric Biederman530b5192003-07-01 10:05:30 +000024870
Eric Biedermanb138ac82003-04-22 18:44:01 +000024871static void generate_code(struct compile_state *state)
24872{
24873 generate_local_labels(state);
24874 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024875
Eric Biedermanb138ac82003-04-22 18:44:01 +000024876}
24877
Eric Biederman90089602004-05-28 14:11:54 +000024878static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024879{
Eric Biederman90089602004-05-28 14:11:54 +000024880 int tok;
24881 FILE *fp;
24882 int line;
24883 const char *filename;
24884 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024885 filename = 0;
24886 line = 0;
24887 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024888 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024889 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024890 const char *token_str;
24891 tok = peek(state);
24892 if (tok == TOK_EOF) {
24893 break;
24894 }
Eric Biederman41203d92004-11-08 09:31:09 +000024895 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024896 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024897 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024898 tk->str_len ? tk->val.str :
24899 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024900
24901 file = state->file;
24902 while(file->macro && file->prev) {
24903 file = file->prev;
24904 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024905 if (!file->macro &&
24906 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024907 {
Eric Biederman90089602004-05-28 14:11:54 +000024908 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024909 if ((file->basename == filename) &&
24910 (line < file->line)) {
24911 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024912 fprintf(fp, "\n");
24913 line++;
24914 }
24915 }
24916 else {
24917 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024918 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024919 }
Eric Biederman132368b2004-11-09 08:59:23 +000024920 line = file->line;
24921 filename = file->basename;
24922 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024923 for(i = 0; i < col; i++) {
24924 fprintf(fp, " ");
24925 }
24926 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024927
Eric Biederman90089602004-05-28 14:11:54 +000024928 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024929
Eric Biederman90089602004-05-28 14:11:54 +000024930 if (state->compiler->debug & DEBUG_TOKENS) {
24931 loc(state->dbgout, state, 0);
24932 fprintf(state->dbgout, "%s <- `%s'\n",
24933 tokens[tok], token_str);
24934 }
24935 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024936}
24937
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024938static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024939 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024940{
24941 int i;
24942 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024943 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024944 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024945 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024946 state.compiler = compiler;
24947 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024948 state.file = 0;
24949 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24950 memset(&state.token[i], 0, sizeof(state.token[i]));
24951 state.token[i].tok = -1;
24952 }
Eric Biederman90089602004-05-28 14:11:54 +000024953 /* Remember the output descriptors */
24954 state.errout = stderr;
24955 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024956 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024957 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24958 state.output = stdout;
24959 } else {
24960 state.output = fopen(state.compiler->ofilename, "w");
24961 if (!state.output) {
24962 error(&state, 0, "Cannot open output file %s\n",
24963 state.compiler->ofilename);
24964 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024965 }
Eric Biederman90089602004-05-28 14:11:54 +000024966 /* Make certain a good cleanup happens */
24967 exit_state = &state;
24968 atexit(exit_cleanup);
24969
Eric Biedermanb138ac82003-04-22 18:44:01 +000024970 /* Prep the preprocessor */
24971 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024972 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024973 /* register the C keywords */
24974 register_keywords(&state);
24975 /* register the keywords the macro preprocessor knows */
24976 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024977 /* generate some builtin macros */
24978 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024979 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024980 state.i_switch = lookup(&state, "switch", 6);
24981 state.i_case = lookup(&state, "case", 4);
24982 state.i_continue = lookup(&state, "continue", 8);
24983 state.i_break = lookup(&state, "break", 5);
24984 state.i_default = lookup(&state, "default", 7);
24985 state.i_return = lookup(&state, "return", 6);
24986 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024987 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24988 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24989 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24990 /* Memorize where predefined identifiers are. */
24991 state.i___func__ = lookup(&state, "__func__", 8);
24992 /* Memorize where some attribute keywords are. */
24993 state.i_noinline = lookup(&state, "noinline", 8);
24994 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024995 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020024996 state.i_unused = lookup(&state, "unused", 6);
Stefan Reinauerec664bc2013-05-09 14:06:04 -070024997 state.i_packed = lookup(&state, "packed", 6);
Eric Biederman90089602004-05-28 14:11:54 +000024998
24999 /* Process the command line macros */
25000 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000025001
25002 /* Allocate beginning bounding labels for the function list */
25003 state.first = label(&state);
25004 state.first->id |= TRIPLE_FLAG_VOLATILE;
25005 use_triple(state.first, state.first);
25006 ptr = label(&state);
25007 ptr->id |= TRIPLE_FLAG_VOLATILE;
25008 use_triple(ptr, ptr);
25009 flatten(&state, state.first, ptr);
25010
Eric Biederman5ade04a2003-10-22 04:03:46 +000025011 /* Allocate a label for the pool of global variables */
25012 state.global_pool = label(&state);
25013 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25014 flatten(&state, state.first, state.global_pool);
25015
Eric Biedermanb138ac82003-04-22 18:44:01 +000025016 /* Enter the globl definition scope */
25017 start_scope(&state);
25018 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025019
Eric Biedermanb138ac82003-04-22 18:44:01 +000025020 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025021
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025022 while (includes) {
25023 compile_file(&state, includes->filename, 1);
25024 includes=includes->next;
25025 }
Eric Biederman90089602004-05-28 14:11:54 +000025026
25027 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025028 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025029 print_preprocessed_tokens(&state);
25030 return;
25031 }
25032
Eric Biedermanb138ac82003-04-22 18:44:01 +000025033 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025034
Eric Biedermanb138ac82003-04-22 18:44:01 +000025035 /* Exit the global definition scope */
25036 end_scope(&state);
25037
Stefan Reinauer14e22772010-04-27 06:56:47 +000025038 /* Now that basic compilation has happened
25039 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025040 */
25041 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025042
Eric Biedermanb138ac82003-04-22 18:44:01 +000025043 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025044 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025045 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025046 }
Eric Biederman90089602004-05-28 14:11:54 +000025047 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025048}
25049
Eric Biederman90089602004-05-28 14:11:54 +000025050static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025051{
Eric Biederman90089602004-05-28 14:11:54 +000025052 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025053}
25054
25055static void usage(void)
25056{
Eric Biederman90089602004-05-28 14:11:54 +000025057 FILE *fp = stdout;
25058 version(fp);
25059 fprintf(fp,
25060 "\nUsage: romcc [options] <source>.c\n"
25061 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25062 "Options: \n"
25063 "-o <output file name>\n"
25064 "-f<option> Specify a generic compiler option\n"
25065 "-m<option> Specify a arch dependent option\n"
25066 "-- Specify this is the last option\n"
25067 "\nGeneric compiler options:\n"
25068 );
25069 compiler_usage(fp);
25070 fprintf(fp,
25071 "\nArchitecture compiler options:\n"
25072 );
25073 arch_usage(fp);
25074 fprintf(fp,
25075 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025076 );
25077}
25078
25079static void arg_error(char *fmt, ...)
25080{
25081 va_list args;
25082 va_start(args, fmt);
25083 vfprintf(stderr, fmt, args);
25084 va_end(args);
25085 usage();
25086 exit(1);
25087}
25088
25089int main(int argc, char **argv)
25090{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025091 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025092 struct compiler_state compiler;
25093 struct arch_state arch;
25094 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025095
25096
Eric Biederman90089602004-05-28 14:11:54 +000025097 /* I don't want any surprises */
25098 setlocale(LC_ALL, "C");
25099
Eric Biederman5ade04a2003-10-22 04:03:46 +000025100 init_compiler_state(&compiler);
25101 init_arch_state(&arch);
25102 filename = 0;
25103 all_opts = 0;
25104 while(argc > 1) {
25105 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25106 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025107 argv += 2;
25108 argc -= 2;
25109 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025110 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025111 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025112 result = -1;
25113 if (strcmp(argv[1], "--") == 0) {
25114 result = 0;
25115 all_opts = 1;
25116 }
Eric Biederman90089602004-05-28 14:11:54 +000025117 else if (strncmp(argv[1], "-E", 2) == 0) {
25118 result = compiler_encode_flag(&compiler, argv[1]);
25119 }
25120 else if (strncmp(argv[1], "-O", 2) == 0) {
25121 result = compiler_encode_flag(&compiler, argv[1]);
25122 }
25123 else if (strncmp(argv[1], "-I", 2) == 0) {
25124 result = compiler_encode_flag(&compiler, argv[1]);
25125 }
25126 else if (strncmp(argv[1], "-D", 2) == 0) {
25127 result = compiler_encode_flag(&compiler, argv[1]);
25128 }
25129 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025130 result = compiler_encode_flag(&compiler, argv[1]);
25131 }
25132 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25133 result = compiler_encode_flag(&compiler, argv[1]+2);
25134 }
25135 else if (strncmp(argv[1], "-f", 2) == 0) {
25136 result = compiler_encode_flag(&compiler, argv[1]+2);
25137 }
25138 else if (strncmp(argv[1], "-m", 2) == 0) {
25139 result = arch_encode_flag(&arch, argv[1]+2);
25140 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025141 else if (strncmp(argv[1], "-c", 2) == 0) {
25142 result = 0;
25143 }
25144 else if (strncmp(argv[1], "-S", 2) == 0) {
25145 result = 0;
25146 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025147 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025148 struct filelist *old_head = include_filelist;
25149 include_filelist = malloc(sizeof(struct filelist));
25150 if (!include_filelist) {
25151 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025152 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025153 argv++;
25154 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025155 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025156 include_filelist->next = old_head;
25157 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025158 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025159 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025160 arg_error("Invalid option specified: %s\n",
25161 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025162 }
25163 argv++;
25164 argc--;
25165 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025166 else {
25167 if (filename) {
25168 arg_error("Only one filename may be specified\n");
25169 }
25170 filename = argv[1];
25171 argv++;
25172 argc--;
25173 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025174 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025175 if (!filename) {
25176 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025177 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025178 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025179
25180 return 0;
25181}