blob: 62fe758ef68f4608e4f64eb5fecc08bd0599275f [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 Reinauer3d0ba1c2015-11-18 17:23:39 -08006#define VERSION_MINOR "80"
7#define RELEASE_DATE "18 November 2015"
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, ...);
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -08001541static void arg_warning(char *fmt, ...);
Eric Biederman90089602004-05-28 14:11:54 +00001542static const char *identifier(const char *str, const char *end);
1543
1544static int append_include_path(struct compiler_state *compiler, const char *str)
1545{
1546 int result;
1547 if (!exists(str, ".")) {
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -08001548 arg_warning("Warning: Nonexistent include path: `%s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001549 str);
1550 }
1551 result = append_string(&compiler->include_path_count,
1552 &compiler->include_paths, str, "include_paths");
1553 return result;
1554}
1555
1556static int append_define(struct compiler_state *compiler, const char *str)
1557{
1558 const char *end, *rest;
1559 int result;
1560
1561 end = strchr(str, '=');
1562 if (!end) {
1563 end = str + strlen(str);
1564 }
1565 rest = identifier(str, end);
1566 if (rest != end) {
1567 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001568 arg_error("Invalid name cannot define macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001569 len, len, str);
1570 }
1571 result = append_string(&compiler->define_count,
1572 &compiler->defines, str, "defines");
1573 return result;
1574}
1575
1576static int append_undef(struct compiler_state *compiler, const char *str)
1577{
1578 const char *end, *rest;
1579 int result;
1580
1581 end = str + strlen(str);
1582 rest = identifier(str, end);
1583 if (rest != end) {
1584 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001585 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001586 len, len, str);
1587 }
1588 result = append_string(&compiler->undef_count,
1589 &compiler->undefs, str, "undefs");
1590 return result;
1591}
1592
1593static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001594 { "trigraphs", COMPILER_TRIGRAPHS },
1595 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001596 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1597 { "simplify", COMPILER_SIMPLIFY },
1598 { "scc-transform", COMPILER_SCC_TRANSFORM },
1599 { "simplify-op", COMPILER_SIMPLIFY_OP },
1600 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1601 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1602 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1603 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1604 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1605 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1606 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1607 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1608 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1609 { 0, 0 },
1610};
1611static const struct compiler_arg romcc_args[] = {
1612 { "inline-policy", COMPILER_INLINE_MASK,
1613 {
1614 { "always", COMPILER_INLINE_ALWAYS, },
1615 { "never", COMPILER_INLINE_NEVER, },
1616 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1617 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1618 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1619 { 0, 0 },
1620 },
1621 },
1622 { 0, 0 },
1623};
1624static const struct compiler_flag romcc_opt_flags[] = {
1625 { "-O", COMPILER_SIMPLIFY },
1626 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001627 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001628 { 0, 0, },
1629};
1630static const struct compiler_flag romcc_debug_flags[] = {
1631 { "all", DEBUG_ALL },
1632 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1633 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1634 { "fdominators", DEBUG_FDOMINATORS },
1635 { "rdominators", DEBUG_RDOMINATORS },
1636 { "triples", DEBUG_TRIPLES },
1637 { "interference", DEBUG_INTERFERENCE },
1638 { "scc-transform", DEBUG_SCC_TRANSFORM },
1639 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1640 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1641 { "inline", DEBUG_INLINE },
1642 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1643 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1644 { "color-graph", DEBUG_COLOR_GRAPH },
1645 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1646 { "coalescing", DEBUG_COALESCING },
1647 { "coalescing2", DEBUG_COALESCING2 },
1648 { "verification", DEBUG_VERIFICATION },
1649 { "calls", DEBUG_CALLS },
1650 { "calls2", DEBUG_CALLS2 },
1651 { "tokens", DEBUG_TOKENS },
1652 { 0, 0 },
1653};
1654
Eric Biederman5ade04a2003-10-22 04:03:46 +00001655static int compiler_encode_flag(
1656 struct compiler_state *compiler, const char *flag)
1657{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001658 int act;
1659 int result;
1660
1661 act = 1;
1662 result = -1;
1663 if (strncmp(flag, "no-", 3) == 0) {
1664 flag += 3;
1665 act = 0;
1666 }
1667 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001668 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1669 }
1670 else if (strncmp(flag, "-E", 2) == 0) {
1671 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1672 }
1673 else if (strncmp(flag, "-I", 2) == 0) {
1674 result = append_include_path(compiler, flag + 2);
1675 }
1676 else if (strncmp(flag, "-D", 2) == 0) {
1677 result = append_define(compiler, flag + 2);
1678 }
1679 else if (strncmp(flag, "-U", 2) == 0) {
1680 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001681 }
1682 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1683 result = 0;
1684 compiler->label_prefix = flag + 13;
1685 }
1686 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1687 unsigned long max_passes;
1688 char *end;
1689 max_passes = strtoul(flag + 22, &end, 10);
1690 if (end[0] == '\0') {
1691 result = 0;
1692 compiler->max_allocation_passes = max_passes;
1693 }
1694 }
1695 else if (act && strcmp(flag, "debug") == 0) {
1696 result = 0;
1697 compiler->debug |= DEBUG_DEFAULT;
1698 }
1699 else if (strncmp(flag, "debug-", 6) == 0) {
1700 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001701 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001702 }
1703 else {
Eric Biederman90089602004-05-28 14:11:54 +00001704 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1705 if (result < 0) {
1706 result = set_arg(romcc_args, &compiler->flags, flag);
1707 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001708 }
1709 return result;
1710}
1711
Eric Biederman90089602004-05-28 14:11:54 +00001712static void compiler_usage(FILE *fp)
1713{
1714 flag_usage(fp, romcc_opt_flags, "", 0);
1715 flag_usage(fp, romcc_flags, "-f", "-fno-");
1716 arg_usage(fp, romcc_args, "-f");
1717 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1718 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1719 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1720 fprintf(fp, "-I<include path>\n");
1721 fprintf(fp, "-D<macro>[=defn]\n");
1722 fprintf(fp, "-U<macro>\n");
1723}
1724
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001725static void do_cleanup(struct compile_state *state)
1726{
1727 if (state->output) {
1728 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001729 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001730 state->output = 0;
1731 }
1732 if (state->dbgout) {
1733 fflush(state->dbgout);
1734 }
1735 if (state->errout) {
1736 fflush(state->errout);
1737 }
1738}
1739
1740static struct compile_state *exit_state;
1741static void exit_cleanup(void)
1742{
1743 if (exit_state) {
1744 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001745 }
1746}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001747
1748static int get_col(struct file_state *file)
1749{
1750 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001751 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001752 ptr = file->line_start;
1753 end = file->pos;
1754 for(col = 0; ptr < end; ptr++) {
1755 if (*ptr != '\t') {
1756 col++;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001757 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001758 else {
1759 col = (col & ~7) + 8;
1760 }
1761 }
1762 return col;
1763}
1764
1765static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1766{
1767 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001768 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001769 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001770 for(spot = triple->occurance; spot; spot = spot->parent) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001771 fprintf(fp, "%s:%d.%d: ",
Eric Biederman5ade04a2003-10-22 04:03:46 +00001772 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001773 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001774 return;
1775 }
1776 if (!state->file) {
1777 return;
1778 }
1779 col = get_col(state->file);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001780 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001781 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001782}
1783
Stefan Reinauer14e22772010-04-27 06:56:47 +00001784static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001785 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001786{
Eric Biederman90089602004-05-28 14:11:54 +00001787 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001788 va_list args;
1789 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001790 loc(fp, state, ptr);
1791 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001792 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001793 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001794 }
Eric Biederman90089602004-05-28 14:11:54 +00001795 fprintf(fp, "Internal compiler error: ");
1796 vfprintf(fp, fmt, args);
1797 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001798 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001799 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001800 abort();
1801}
1802
1803
Stefan Reinauer14e22772010-04-27 06:56:47 +00001804static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001805 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001806{
Eric Biederman90089602004-05-28 14:11:54 +00001807 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001808 va_list args;
1809 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001810 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001811 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001812 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001813 }
Eric Biederman90089602004-05-28 14:11:54 +00001814 fprintf(fp, "Internal compiler warning: ");
1815 vfprintf(fp, fmt, args);
1816 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001817 va_end(args);
1818}
1819
1820
1821
Stefan Reinauer14e22772010-04-27 06:56:47 +00001822static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001823 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001824{
Eric Biederman90089602004-05-28 14:11:54 +00001825 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001826 va_list args;
1827 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001828 loc(fp, state, ptr);
1829 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001830 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001831 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001832 }
Eric Biederman90089602004-05-28 14:11:54 +00001833 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001834 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001835 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001836 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001837 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001838 abort();
1839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001840 exit(1);
1841}
1842
Stefan Reinauer14e22772010-04-27 06:56:47 +00001843static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001844 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001845{
Eric Biederman90089602004-05-28 14:11:54 +00001846 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001847 va_list args;
1848 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001849 loc(fp, state, ptr);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001850 fprintf(fp, "warning: ");
Eric Biederman90089602004-05-28 14:11:54 +00001851 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1852 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1853 }
1854 vfprintf(fp, fmt, args);
1855 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001856 va_end(args);
1857}
1858
Eric Biedermanb138ac82003-04-22 18:44:01 +00001859#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1860
Eric Biederman0babc1c2003-05-09 02:39:00 +00001861static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001862{
1863 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001864 if (op >= OP_MAX) {
1865 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001866 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001867 if (op < 0) {
1868 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001869 }
1870}
1871
Eric Biederman0babc1c2003-05-09 02:39:00 +00001872static void valid_ins(struct compile_state *state, struct triple *ptr)
1873{
1874 valid_op(state, ptr->op);
1875}
1876
Stefan Reinauer50542a82007-10-24 11:14:14 +00001877#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001878static void valid_param_count(struct compile_state *state, struct triple *ins)
1879{
1880 int lhs, rhs, misc, targ;
1881 valid_ins(state, ins);
1882 lhs = table_ops[ins->op].lhs;
1883 rhs = table_ops[ins->op].rhs;
1884 misc = table_ops[ins->op].misc;
1885 targ = table_ops[ins->op].targ;
1886
1887 if ((lhs >= 0) && (ins->lhs != lhs)) {
1888 internal_error(state, ins, "Bad lhs count");
1889 }
1890 if ((rhs >= 0) && (ins->rhs != rhs)) {
1891 internal_error(state, ins, "Bad rhs count");
1892 }
1893 if ((misc >= 0) && (ins->misc != misc)) {
1894 internal_error(state, ins, "Bad misc count");
1895 }
1896 if ((targ >= 0) && (ins->targ != targ)) {
1897 internal_error(state, ins, "Bad targ count");
1898 }
1899}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001900#endif
Eric Biederman90089602004-05-28 14:11:54 +00001901
Eric Biedermanb138ac82003-04-22 18:44:01 +00001902static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001903static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001904static void use_triple(struct triple *used, struct triple *user)
1905{
1906 struct triple_set **ptr, *new;
1907 if (!used)
1908 return;
1909 if (!user)
1910 return;
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001911 ptr = &used->use;
1912 while(*ptr) {
1913 if ((*ptr)->member == user) {
1914 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001915 }
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001916 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001917 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00001918 /* Append new to the head of the list,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001919 * copy_func and rename_block_variables
1920 * depends on this.
1921 */
1922 new = xcmalloc(sizeof(*new), "triple_set");
1923 new->member = user;
1924 new->next = used->use;
1925 used->use = new;
1926}
1927
1928static void unuse_triple(struct triple *used, struct triple *unuser)
1929{
1930 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001931 if (!used) {
1932 return;
1933 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001934 ptr = &used->use;
1935 while(*ptr) {
1936 use = *ptr;
1937 if (use->member == unuser) {
1938 *ptr = use->next;
1939 xfree(use);
1940 }
1941 else {
1942 ptr = &use->next;
1943 }
1944 }
1945}
1946
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001947static void put_occurance(struct occurance *occurance)
1948{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001949 if (occurance) {
1950 occurance->count -= 1;
1951 if (occurance->count <= 0) {
1952 if (occurance->parent) {
1953 put_occurance(occurance->parent);
1954 }
1955 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001956 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001957 }
1958}
1959
1960static void get_occurance(struct occurance *occurance)
1961{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001962 if (occurance) {
1963 occurance->count += 1;
1964 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001965}
1966
1967
1968static struct occurance *new_occurance(struct compile_state *state)
1969{
1970 struct occurance *result, *last;
1971 const char *filename;
1972 const char *function;
1973 int line, col;
1974
1975 function = "";
1976 filename = 0;
1977 line = 0;
1978 col = 0;
1979 if (state->file) {
1980 filename = state->file->report_name;
1981 line = state->file->report_line;
1982 col = get_col(state->file);
1983 }
1984 if (state->function) {
1985 function = state->function;
1986 }
1987 last = state->last_occurance;
1988 if (last &&
1989 (last->col == col) &&
1990 (last->line == line) &&
1991 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001992 ((last->filename == filename) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00001993 (strcmp(last->filename, filename) == 0)))
Eric Biederman83b991a2003-10-11 06:20:25 +00001994 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001995 get_occurance(last);
1996 return last;
1997 }
1998 if (last) {
1999 state->last_occurance = 0;
2000 put_occurance(last);
2001 }
2002 result = xmalloc(sizeof(*result), "occurance");
2003 result->count = 2;
2004 result->filename = filename;
2005 result->function = function;
2006 result->line = line;
2007 result->col = col;
2008 result->parent = 0;
2009 state->last_occurance = result;
2010 return result;
2011}
2012
2013static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002014 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002015{
2016 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002017 if (top->parent) {
2018 internal_error(state, 0, "inlining an already inlined function?");
2019 }
2020 /* If I have a null base treat it that way */
2021 if ((base->parent == 0) &&
2022 (base->col == 0) &&
2023 (base->line == 0) &&
2024 (base->function[0] == '\0') &&
2025 (base->filename[0] == '\0')) {
2026 base = 0;
2027 }
2028 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002029 last = state->last_occurance;
2030 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002031 (last->parent == base) &&
2032 (last->col == top->col) &&
2033 (last->line == top->line) &&
2034 (last->function == top->function) &&
2035 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002036 get_occurance(last);
2037 return last;
2038 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002039 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002040 if (last) {
2041 state->last_occurance = 0;
2042 put_occurance(last);
2043 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002044 /* Generate a new occurance structure */
2045 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002046 result = xmalloc(sizeof(*result), "occurance");
2047 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002048 result->filename = top->filename;
2049 result->function = top->function;
2050 result->line = top->line;
2051 result->col = top->col;
2052 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002053 state->last_occurance = result;
2054 return result;
2055}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002056
2057static struct occurance dummy_occurance = {
2058 .count = 2,
2059 .filename = __FILE__,
2060 .function = "",
2061 .line = __LINE__,
2062 .col = 0,
2063 .parent = 0,
2064};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002065
Eric Biederman90089602004-05-28 14:11:54 +00002066/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002067 * from a triple. Having allows certain sanity checks to pass even
2068 * when the original triple that was pointed to is gone.
2069 */
Eric Biederman90089602004-05-28 14:11:54 +00002070static struct triple unknown_triple = {
2071 .next = &unknown_triple,
2072 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002073 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002074 .op = OP_UNKNOWNVAL,
2075 .lhs = 0,
2076 .rhs = 0,
2077 .misc = 0,
2078 .targ = 0,
2079 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002080 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002081 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002082 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002083 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002084};
2085
Eric Biederman0babc1c2003-05-09 02:39:00 +00002086
Eric Biederman90089602004-05-28 14:11:54 +00002087static size_t registers_of(struct compile_state *state, struct type *type);
2088
Stefan Reinauer14e22772010-04-27 06:56:47 +00002089static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002090 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2091 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002092{
Eric Biederman90089602004-05-28 14:11:54 +00002093 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002094 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002095 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002096 dummy.op = op;
2097 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002098 valid_op(state, op);
2099 lhs = table_ops[op].lhs;
2100 rhs = table_ops[op].rhs;
2101 misc = table_ops[op].misc;
2102 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002103
2104 switch(op) {
2105 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002106 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002107 break;
2108 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002109 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002110 break;
2111 case OP_ADECL:
2112 lhs = registers_of(state, type);
2113 break;
2114 case OP_TUPLE:
2115 lhs = registers_of(state, type);
2116 break;
2117 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002118 rhs = rhs_wanted;
2119 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002120 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002121 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002122 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002123 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002124 }
2125 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002126 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002127 }
2128 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002129 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002130 }
2131 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002132 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002133 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002134
2135 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002136 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002137 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2138
2139 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2140 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002141 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002142 ret->lhs = lhs;
2143 ret->rhs = rhs;
2144 ret->misc = misc;
2145 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002146 ret->type = type;
2147 ret->next = ret;
2148 ret->prev = ret;
2149 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002150 /* A simple sanity check */
2151 if ((ret->op != op) ||
2152 (ret->lhs != lhs) ||
2153 (ret->rhs != rhs) ||
2154 (ret->misc != misc) ||
2155 (ret->targ != targ) ||
2156 (ret->type != type) ||
2157 (ret->next != ret) ||
2158 (ret->prev != ret) ||
2159 (ret->occurance != occurance)) {
2160 internal_error(state, ret, "huh?");
2161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002162 return ret;
2163}
2164
Eric Biederman0babc1c2003-05-09 02:39:00 +00002165struct triple *dup_triple(struct compile_state *state, struct triple *src)
2166{
2167 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002168 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002169 src_lhs = src->lhs;
2170 src_rhs = src->rhs;
2171 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002172 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002173 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002174 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002175 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002176 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002177 return dup;
2178}
2179
Eric Biederman41203d92004-11-08 09:31:09 +00002180static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2181{
2182 struct triple *copy;
2183 copy = dup_triple(state, src);
2184 copy->use = 0;
2185 copy->next = copy->prev = copy;
2186 return copy;
2187}
2188
Stefan Reinauer14e22772010-04-27 06:56:47 +00002189static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002190 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002191{
2192 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002193 struct occurance *occurance;
2194 occurance = new_occurance(state);
2195 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002196 return ret;
2197}
2198
Stefan Reinauer14e22772010-04-27 06:56:47 +00002199static struct triple *build_triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002200 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002201 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002202{
2203 struct triple *ret;
2204 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002205 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002206 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002207 if (count > 0) {
2208 ret->param[0] = left;
2209 }
2210 if (count > 1) {
2211 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002212 }
2213 return ret;
2214}
2215
Stefan Reinauer14e22772010-04-27 06:56:47 +00002216static struct triple *triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002217 int op, struct type *type, struct triple *left, struct triple *right)
2218{
2219 struct triple *ret;
2220 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002221 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002222 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002223 if (count >= 1) {
2224 ret->param[0] = left;
2225 }
2226 if (count >= 2) {
2227 ret->param[1] = right;
2228 }
2229 return ret;
2230}
2231
Stefan Reinauer14e22772010-04-27 06:56:47 +00002232static struct triple *branch(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002233 struct triple *targ, struct triple *test)
2234{
2235 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002236 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002237 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002238 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002239 } else {
2240 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002241 }
2242 TARG(ret, 0) = targ;
2243 /* record the branch target was used */
2244 if (!targ || (targ->op != OP_LABEL)) {
2245 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002246 }
2247 return ret;
2248}
2249
Eric Biederman90089602004-05-28 14:11:54 +00002250static int triple_is_label(struct compile_state *state, struct triple *ins);
2251static int triple_is_call(struct compile_state *state, struct triple *ins);
2252static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002253static void insert_triple(struct compile_state *state,
2254 struct triple *first, struct triple *ptr)
2255{
2256 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002257 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002258 internal_error(state, ptr, "expression already used");
2259 }
2260 ptr->next = first;
2261 ptr->prev = first->prev;
2262 ptr->prev->next = ptr;
2263 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002264
2265 if (triple_is_cbranch(state, ptr->prev) ||
2266 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002267 unuse_triple(first, ptr->prev);
2268 use_triple(ptr, ptr->prev);
2269 }
2270 }
2271}
2272
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002273static int triple_stores_block(struct compile_state *state, struct triple *ins)
2274{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002275 /* This function is used to determine if u.block
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002276 * is utilized to store the current block number.
2277 */
2278 int stores_block;
2279 valid_ins(state, ins);
2280 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2281 return stores_block;
2282}
2283
Eric Biederman90089602004-05-28 14:11:54 +00002284static int triple_is_branch(struct compile_state *state, struct triple *ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002285static struct block *block_of_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002286 struct triple *ins)
2287{
2288 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002289 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002290 return 0;
2291 }
2292 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002293 while(ins != first && !triple_is_branch(state, ins->prev) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00002294 !triple_stores_block(state, ins))
2295 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002296 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002297 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002298 }
2299 ins = ins->prev;
2300 }
Eric Biederman90089602004-05-28 14:11:54 +00002301 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002302}
2303
Eric Biederman90089602004-05-28 14:11:54 +00002304static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002305static struct triple *pre_triple(struct compile_state *state,
2306 struct triple *base,
2307 int op, struct type *type, struct triple *left, struct triple *right)
2308{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002309 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002310 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002311 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002312 /* If I am an OP_PIECE jump to the real instruction */
2313 if (base->op == OP_PIECE) {
2314 base = MISC(base, 0);
2315 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002316 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002317 get_occurance(base->occurance);
2318 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002319 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002320 if (triple_stores_block(state, ret)) {
2321 ret->u.block = block;
2322 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002323 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002324 for(i = 0; i < ret->lhs; i++) {
2325 struct triple *piece;
2326 piece = LHS(ret, i);
2327 insert_triple(state, base, piece);
2328 use_triple(ret, piece);
2329 use_triple(piece, ret);
2330 }
2331 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002332 block->first = ret;
2333 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002334 return ret;
2335}
2336
2337static struct triple *post_triple(struct compile_state *state,
2338 struct triple *base,
2339 int op, struct type *type, struct triple *left, struct triple *right)
2340{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002341 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002342 struct triple *ret, *next;
2343 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002344 /* If I am an OP_PIECE jump to the real instruction */
2345 if (base->op == OP_PIECE) {
2346 base = MISC(base, 0);
2347 }
2348 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002349 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002350 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002351 base = LHS(base, zlhs - 1);
2352 }
2353
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002354 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002355 get_occurance(base->occurance);
2356 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002357 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002358 if (triple_stores_block(state, ret)) {
2359 ret->u.block = block;
2360 }
Eric Biederman90089602004-05-28 14:11:54 +00002361 next = base->next;
2362 insert_triple(state, next, ret);
2363 zlhs = ret->lhs;
2364 for(i = 0; i < zlhs; i++) {
2365 struct triple *piece;
2366 piece = LHS(ret, i);
2367 insert_triple(state, next, piece);
2368 use_triple(ret, piece);
2369 use_triple(piece, ret);
2370 }
2371 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002372 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002373 if (zlhs) {
2374 block->last = LHS(ret, zlhs - 1);
2375 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002376 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002377 return ret;
2378}
2379
Eric Biederman90089602004-05-28 14:11:54 +00002380static struct type *reg_type(
2381 struct compile_state *state, struct type *type, int reg);
2382
2383static void generate_lhs_piece(
2384 struct compile_state *state, struct triple *ins, int index)
2385{
2386 struct type *piece_type;
2387 struct triple *piece;
2388 get_occurance(ins->occurance);
2389 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2390
2391 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2392 piece_type = piece_type->left;
2393 }
2394#if 0
2395{
2396 static void name_of(FILE *fp, struct type *type);
2397 FILE * fp = state->errout;
2398 fprintf(fp, "piece_type(%d): ", index);
2399 name_of(fp, piece_type);
2400 fprintf(fp, "\n");
2401}
2402#endif
2403 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2404 piece->u.cval = index;
2405 LHS(ins, piece->u.cval) = piece;
2406 MISC(piece, 0) = ins;
2407}
2408
2409static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2410{
2411 int i, zlhs;
2412 zlhs = ins->lhs;
2413 for(i = 0; i < zlhs; i++) {
2414 generate_lhs_piece(state, ins, i);
2415 }
2416}
2417
Eric Biedermanb138ac82003-04-22 18:44:01 +00002418static struct triple *label(struct compile_state *state)
2419{
2420 /* Labels don't get a type */
2421 struct triple *result;
2422 result = triple(state, OP_LABEL, &void_type, 0, 0);
2423 return result;
2424}
2425
Eric Biederman90089602004-05-28 14:11:54 +00002426static struct triple *mkprog(struct compile_state *state, ...)
2427{
2428 struct triple *prog, *head, *arg;
2429 va_list args;
2430 int i;
2431
2432 head = label(state);
2433 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2434 RHS(prog, 0) = head;
2435 va_start(args, state);
2436 i = 0;
2437 while((arg = va_arg(args, struct triple *)) != 0) {
2438 if (++i >= 100) {
2439 internal_error(state, 0, "too many arguments to mkprog");
2440 }
2441 flatten(state, head, arg);
2442 }
2443 va_end(args);
2444 prog->type = head->prev->type;
2445 return prog;
2446}
2447static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002448static void display_triple(FILE *fp, struct triple *ins)
2449{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002450 struct occurance *ptr;
2451 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002452 char pre, post, vol;
2453 pre = post = vol = ' ';
2454 if (ins) {
2455 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2456 pre = '^';
2457 }
2458 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2459 post = ',';
2460 }
2461 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2462 vol = 'v';
2463 }
2464 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002465 }
Eric Biederman90089602004-05-28 14:11:54 +00002466 if (ins == 0) {
2467 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002468 }
Eric Biederman90089602004-05-28 14:11:54 +00002469 else if (ins->op == OP_INTCONST) {
2470 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002471 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002472 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002473 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002474 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002475 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002476 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002477 MISC(ins, 0), (unsigned long)(ins->u.cval));
2478 }
2479 else if (ins->op == OP_INDEX) {
2480 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002481 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002482 RHS(ins, 0), (unsigned long)(ins->u.cval));
2483 }
2484 else if (ins->op == OP_PIECE) {
2485 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002486 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002487 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002488 }
2489 else {
2490 int i, count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002491 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
Eric Biederman90089602004-05-28 14:11:54 +00002492 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2493 if (table_ops[ins->op].flags & BITFIELD) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002494 fprintf(fp, " <%2d-%2d:%2d>",
Eric Biederman90089602004-05-28 14:11:54 +00002495 ins->u.bitfield.offset,
2496 ins->u.bitfield.offset + ins->u.bitfield.size,
2497 ins->u.bitfield.size);
2498 }
2499 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002500 for(i = 0; i < count; i++) {
2501 fprintf(fp, " %-10p", ins->param[i]);
2502 }
2503 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002504 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002505 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002506 }
Eric Biederman90089602004-05-28 14:11:54 +00002507 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002508 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002509#if DEBUG_DISPLAY_TYPES
2510 fprintf(fp, " <");
2511 name_of(fp, ins->type);
2512 fprintf(fp, "> ");
2513#endif
2514#if DEBUG_DISPLAY_USES
2515 fprintf(fp, " [");
2516 for(user = ins->use; user; user = user->next) {
2517 fprintf(fp, " %-10p", user->member);
2518 }
2519 fprintf(fp, " ]");
2520#endif
2521 fprintf(fp, " @");
2522 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2523 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002524 ptr->function,
Eric Biederman90089602004-05-28 14:11:54 +00002525 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002526 ptr->line,
Eric Biederman90089602004-05-28 14:11:54 +00002527 ptr->col);
2528 }
2529 if (ins->op == OP_ASM) {
2530 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002531 }
2532 }
Eric Biederman90089602004-05-28 14:11:54 +00002533 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002534 fflush(fp);
2535}
2536
Eric Biederman90089602004-05-28 14:11:54 +00002537static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002538static void display_triple_changes(
2539 FILE *fp, const struct triple *new, const struct triple *orig)
2540{
2541
2542 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002543 new_count = TRIPLE_SIZE(new);
2544 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002545 if ((new->op != orig->op) ||
2546 (new_count != orig_count) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002547 (memcmp(orig->param, new->param,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002548 orig_count * sizeof(orig->param[0])) != 0) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002549 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
Eric Biederman5ade04a2003-10-22 04:03:46 +00002550 {
2551 struct occurance *ptr;
2552 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002553 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002554 if (orig->op == new->op) {
2555 fprintf(fp, " %-11s", tops(orig->op));
2556 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002557 fprintf(fp, " [%-10s %-10s]",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002558 tops(new->op), tops(orig->op));
2559 }
2560 min_count = new_count;
2561 if (min_count > orig_count) {
2562 min_count = orig_count;
2563 }
2564 for(indent = i = 0; i < min_count; i++) {
2565 if (orig->param[i] == new->param[i]) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002566 fprintf(fp, " %-11p",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002567 orig->param[i]);
2568 indent += 12;
2569 } else {
2570 fprintf(fp, " [%-10p %-10p]",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002571 new->param[i],
Eric Biederman5ade04a2003-10-22 04:03:46 +00002572 orig->param[i]);
2573 indent += 24;
2574 }
2575 }
2576 for(; i < orig_count; i++) {
2577 fprintf(fp, " [%-9p]", orig->param[i]);
2578 indent += 12;
2579 }
2580 for(; i < new_count; i++) {
2581 fprintf(fp, " [%-9p]", new->param[i]);
2582 indent += 12;
2583 }
2584 if ((new->op == OP_INTCONST)||
2585 (new->op == OP_ADDRCONST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002586 fprintf(fp, " <0x%08lx>",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002587 (unsigned long)(new->u.cval));
2588 indent += 13;
2589 }
2590 for(;indent < 36; indent++) {
2591 putc(' ', fp);
2592 }
Eric Biederman90089602004-05-28 14:11:54 +00002593
2594#if DEBUG_DISPLAY_TYPES
2595 fprintf(fp, " <");
2596 name_of(fp, new->type);
2597 if (!equiv_types(new->type, orig->type)) {
2598 fprintf(fp, " -- ");
2599 name_of(fp, orig->type);
2600 }
2601 fprintf(fp, "> ");
2602#endif
2603
Eric Biederman5ade04a2003-10-22 04:03:46 +00002604 fprintf(fp, " @");
2605 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2606 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002607 ptr->function,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002608 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002609 ptr->line,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002610 ptr->col);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002611
Eric Biederman5ade04a2003-10-22 04:03:46 +00002612 }
2613 fprintf(fp, "\n");
2614 fflush(fp);
2615 }
2616}
2617
Eric Biederman83b991a2003-10-11 06:20:25 +00002618static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002619{
2620 /* Does the triple have no side effects.
Stefan Reinauer14e22772010-04-27 06:56:47 +00002621 * I.e. Rexecuting the triple with the same arguments
Eric Biedermanb138ac82003-04-22 18:44:01 +00002622 * gives the same value.
2623 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002624 unsigned pure;
2625 valid_ins(state, ins);
2626 pure = PURE_BITS(table_ops[ins->op].flags);
2627 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002628 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002629 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002630 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002631 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002632}
2633
Stefan Reinauer14e22772010-04-27 06:56:47 +00002634static int triple_is_branch_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002635 struct triple *ins, unsigned type)
2636{
2637 /* Is this one of the passed branch types? */
2638 valid_ins(state, ins);
2639 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2640}
2641
Eric Biederman0babc1c2003-05-09 02:39:00 +00002642static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002643{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002644 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002645 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002646 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002647}
2648
Eric Biederman90089602004-05-28 14:11:54 +00002649static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002650{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002651 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002652 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002653}
2654
Eric Biederman90089602004-05-28 14:11:54 +00002655static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002656{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002657 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002658 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002659 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002660 type = BRANCH_BITS(table_ops[ins->op].flags);
2661 return (type != 0) && (type != CBRANCH);
2662}
2663
2664static int triple_is_call(struct compile_state *state, struct triple *ins)
2665{
2666 /* Is this triple a call instruction? */
2667 return triple_is_branch_type(state, ins, CALLBRANCH);
2668}
2669
2670static int triple_is_ret(struct compile_state *state, struct triple *ins)
2671{
2672 /* Is this triple a return instruction? */
2673 return triple_is_branch_type(state, ins, RETBRANCH);
2674}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002675
Stefan Reinauer50542a82007-10-24 11:14:14 +00002676#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002677static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2678{
2679 /* Is this triple an unconditional branch and not a call or a
2680 * return? */
2681 return triple_is_branch_type(state, ins, UBRANCH);
2682}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002683#endif
Eric Biederman90089602004-05-28 14:11:54 +00002684
2685static int triple_is_end(struct compile_state *state, struct triple *ins)
2686{
2687 return triple_is_branch_type(state, ins, ENDBRANCH);
2688}
2689
2690static int triple_is_label(struct compile_state *state, struct triple *ins)
2691{
2692 valid_ins(state, ins);
2693 return (ins->op == OP_LABEL);
2694}
2695
2696static struct triple *triple_to_block_start(
2697 struct compile_state *state, struct triple *start)
2698{
2699 while(!triple_is_branch(state, start->prev) &&
2700 (!triple_is_label(state, start) || !start->use)) {
2701 start = start->prev;
2702 }
2703 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002704}
2705
Eric Biederman0babc1c2003-05-09 02:39:00 +00002706static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002707{
2708 /* This function is used to determine which triples need
2709 * a register.
2710 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002711 int is_def;
2712 valid_ins(state, ins);
2713 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002714 if (ins->lhs >= 1) {
2715 is_def = 0;
2716 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002717 return is_def;
2718}
2719
Eric Biederman83b991a2003-10-11 06:20:25 +00002720static int triple_is_structural(struct compile_state *state, struct triple *ins)
2721{
2722 int is_structural;
2723 valid_ins(state, ins);
2724 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2725 return is_structural;
2726}
2727
Eric Biederman90089602004-05-28 14:11:54 +00002728static int triple_is_part(struct compile_state *state, struct triple *ins)
2729{
2730 int is_part;
2731 valid_ins(state, ins);
2732 is_part = (table_ops[ins->op].flags & PART) == PART;
2733 return is_part;
2734}
2735
2736static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2737{
2738 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2739}
2740
Eric Biederman0babc1c2003-05-09 02:39:00 +00002741static struct triple **triple_iter(struct compile_state *state,
2742 size_t count, struct triple **vector,
2743 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002744{
2745 struct triple **ret;
2746 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002747 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002748 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002749 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002750 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002751 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002752 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002753 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002754 }
2755 return ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002756
Eric Biedermanb138ac82003-04-22 18:44:01 +00002757}
2758
2759static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002760 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002761{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002762 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002763 ins, last);
2764}
2765
2766static struct triple **triple_rhs(struct compile_state *state,
2767 struct triple *ins, struct triple **last)
2768{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002769 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002770 ins, last);
2771}
2772
2773static struct triple **triple_misc(struct compile_state *state,
2774 struct triple *ins, struct triple **last)
2775{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002776 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002777 ins, last);
2778}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002779
Eric Biederman90089602004-05-28 14:11:54 +00002780static struct triple **do_triple_targ(struct compile_state *state,
2781 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002782{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002783 size_t count;
2784 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002785 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002786 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002787 count = ins->targ;
2788 next_is_targ = 0;
2789 if (triple_is_cbranch(state, ins)) {
2790 next_is_targ = 1;
2791 }
2792 if (!call_edges && triple_is_call(state, ins)) {
2793 count = 0;
2794 }
2795 if (next_edges && triple_is_call(state, ins)) {
2796 next_is_targ = 1;
2797 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002798 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002799 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002800 if (!last) {
2801 ret = &ins->next;
2802 } else if (last == &ins->next) {
2803 last = 0;
2804 }
2805 }
2806 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002807 if (!last) {
2808 ret = vector;
2809 }
2810 else if ((last >= vector) && (last < (vector + count - 1))) {
2811 ret = last + 1;
2812 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002813 else if (last == vector + count - 1) {
2814 last = 0;
2815 }
2816 }
Eric Biederman90089602004-05-28 14:11:54 +00002817 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002818 struct triple_set *use;
2819 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002820 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002821 continue;
2822 }
2823 if (!last) {
2824 ret = &use->member->next;
2825 break;
2826 }
2827 else if (last == &use->member->next) {
2828 last = 0;
2829 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002830 }
2831 }
2832 return ret;
2833}
2834
Eric Biederman90089602004-05-28 14:11:54 +00002835static struct triple **triple_targ(struct compile_state *state,
2836 struct triple *ins, struct triple **last)
2837{
2838 return do_triple_targ(state, ins, last, 1, 1);
2839}
2840
2841static struct triple **triple_edge_targ(struct compile_state *state,
2842 struct triple *ins, struct triple **last)
2843{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002844 return do_triple_targ(state, ins, last,
Eric Biederman90089602004-05-28 14:11:54 +00002845 state->functions_joined, !state->functions_joined);
2846}
2847
2848static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2849{
2850 struct triple *next;
2851 int lhs, i;
2852 lhs = ins->lhs;
2853 next = ins->next;
2854 for(i = 0; i < lhs; i++) {
2855 struct triple *piece;
2856 piece = LHS(ins, i);
2857 if (next != piece) {
2858 internal_error(state, ins, "malformed lhs on %s",
2859 tops(ins->op));
2860 }
2861 if (next->op != OP_PIECE) {
2862 internal_error(state, ins, "bad lhs op %s at %d on %s",
2863 tops(next->op), i, tops(ins->op));
2864 }
2865 if (next->u.cval != i) {
2866 internal_error(state, ins, "bad u.cval of %d %d expected",
2867 next->u.cval, i);
2868 }
2869 next = next->next;
2870 }
2871 return next;
2872}
2873
2874/* Function piece accessor functions */
Stefan Reinauer14e22772010-04-27 06:56:47 +00002875static struct triple *do_farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002876 struct triple *func, unsigned index)
2877{
2878 struct type *ftype;
2879 struct triple *first, *arg;
2880 unsigned i;
2881
2882 ftype = func->type;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +11002883 if(index >= (ftype->elements + 2)) {
Eric Biederman90089602004-05-28 14:11:54 +00002884 internal_error(state, func, "bad argument index: %d", index);
2885 }
2886 first = RHS(func, 0);
2887 arg = first->next;
2888 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2889 /* do nothing */
2890 }
2891 if (arg->op != OP_ADECL) {
2892 internal_error(state, 0, "arg not adecl?");
2893 }
2894 return arg;
2895}
2896static struct triple *fresult(struct compile_state *state, struct triple *func)
2897{
2898 return do_farg(state, func, 0);
2899}
2900static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2901{
2902 return do_farg(state, func, 1);
2903}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002904static struct triple *farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002905 struct triple *func, unsigned index)
2906{
2907 return do_farg(state, func, index + 2);
2908}
2909
2910
2911static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2912{
2913 struct triple *first, *ins;
2914 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2915 first = ins = RHS(func, 0);
2916 do {
2917 if (triple_is_label(state, ins) && ins->use) {
2918 fprintf(fp, "%p:\n", ins);
2919 }
2920 display_triple(fp, ins);
2921
2922 if (triple_is_branch(state, ins)) {
2923 fprintf(fp, "\n");
2924 }
2925 if (ins->next->prev != ins) {
2926 internal_error(state, ins->next, "bad prev");
2927 }
2928 ins = ins->next;
2929 } while(ins != first);
2930}
2931
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002932static void verify_use(struct compile_state *state,
2933 struct triple *user, struct triple *used)
2934{
2935 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002936 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002937 for(i = 0; i < size; i++) {
2938 if (user->param[i] == used) {
2939 break;
2940 }
2941 }
2942 if (triple_is_branch(state, user)) {
2943 if (user->next == used) {
2944 i = -1;
2945 }
2946 }
2947 if (i == size) {
2948 internal_error(state, user, "%s(%p) does not use %s(%p)",
2949 tops(user->op), user, tops(used->op), used);
2950 }
2951}
2952
Stefan Reinauer14e22772010-04-27 06:56:47 +00002953static int find_rhs_use(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002954 struct triple *user, struct triple *used)
2955{
2956 struct triple **param;
2957 int size, i;
2958 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002959
2960#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002961#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002962#endif
Eric Biederman90089602004-05-28 14:11:54 +00002963 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002964 param = &RHS(user, 0);
2965 for(i = 0; i < size; i++) {
2966 if (param[i] == used) {
2967 return i;
2968 }
2969 }
2970 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002971}
2972
2973static void free_triple(struct compile_state *state, struct triple *ptr)
2974{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002975 size_t size;
2976 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002977 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002978 ptr->prev->next = ptr->next;
2979 ptr->next->prev = ptr->prev;
2980 if (ptr->use) {
2981 internal_error(state, ptr, "ptr->use != 0");
2982 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002983 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002984 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002985 xfree(ptr);
2986}
2987
2988static void release_triple(struct compile_state *state, struct triple *ptr)
2989{
2990 struct triple_set *set, *next;
2991 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002992 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002993 if (ptr == &unknown_triple) {
2994 return;
2995 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002996 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002997 /* Make certain the we are not the first or last element of a block */
2998 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002999 if (block) {
3000 if ((block->last == ptr) && (block->first == ptr)) {
3001 block->last = block->first = 0;
3002 }
3003 else if (block->last == ptr) {
3004 block->last = ptr->prev;
3005 }
3006 else if (block->first == ptr) {
3007 block->first = ptr->next;
3008 }
Eric Biederman66fe2222003-07-04 15:14:04 +00003009 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003010 /* Remove ptr from use chains where it is the user */
3011 expr = triple_rhs(state, ptr, 0);
3012 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3013 if (*expr) {
3014 unuse_triple(*expr, ptr);
3015 }
3016 }
3017 expr = triple_lhs(state, ptr, 0);
3018 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3019 if (*expr) {
3020 unuse_triple(*expr, ptr);
3021 }
3022 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003023 expr = triple_misc(state, ptr, 0);
3024 for(; expr; expr = triple_misc(state, ptr, expr)) {
3025 if (*expr) {
3026 unuse_triple(*expr, ptr);
3027 }
3028 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003029 expr = triple_targ(state, ptr, 0);
3030 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003031 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003032 unuse_triple(*expr, ptr);
3033 }
3034 }
3035 /* Reomve ptr from use chains where it is used */
3036 for(set = ptr->use; set; set = next) {
3037 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003038 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003039 expr = triple_rhs(state, set->member, 0);
3040 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3041 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003042 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003043 }
3044 }
3045 expr = triple_lhs(state, set->member, 0);
3046 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3047 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003048 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003049 }
3050 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003051 expr = triple_misc(state, set->member, 0);
3052 for(; expr; expr = triple_misc(state, set->member, expr)) {
3053 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003054 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003055 }
3056 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003057 expr = triple_targ(state, set->member, 0);
3058 for(; expr; expr = triple_targ(state, set->member, expr)) {
3059 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003060 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003061 }
3062 }
3063 unuse_triple(ptr, set->member);
3064 }
3065 free_triple(state, ptr);
3066}
3067
Eric Biederman5ade04a2003-10-22 04:03:46 +00003068static void print_triples(struct compile_state *state);
3069static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003070
Jason Schildt27b85112005-08-10 14:31:52 +00003071#define TOK_UNKNOWN 0
3072#define TOK_SPACE 1
3073#define TOK_SEMI 2
3074#define TOK_LBRACE 3
3075#define TOK_RBRACE 4
3076#define TOK_COMMA 5
3077#define TOK_EQ 6
3078#define TOK_COLON 7
3079#define TOK_LBRACKET 8
3080#define TOK_RBRACKET 9
3081#define TOK_LPAREN 10
3082#define TOK_RPAREN 11
3083#define TOK_STAR 12
3084#define TOK_DOTS 13
3085#define TOK_MORE 14
3086#define TOK_LESS 15
3087#define TOK_TIMESEQ 16
3088#define TOK_DIVEQ 17
3089#define TOK_MODEQ 18
3090#define TOK_PLUSEQ 19
3091#define TOK_MINUSEQ 20
3092#define TOK_SLEQ 21
3093#define TOK_SREQ 22
3094#define TOK_ANDEQ 23
3095#define TOK_XOREQ 24
3096#define TOK_OREQ 25
3097#define TOK_EQEQ 26
3098#define TOK_NOTEQ 27
3099#define TOK_QUEST 28
3100#define TOK_LOGOR 29
3101#define TOK_LOGAND 30
3102#define TOK_OR 31
3103#define TOK_AND 32
3104#define TOK_XOR 33
3105#define TOK_LESSEQ 34
3106#define TOK_MOREEQ 35
3107#define TOK_SL 36
3108#define TOK_SR 37
3109#define TOK_PLUS 38
3110#define TOK_MINUS 39
3111#define TOK_DIV 40
3112#define TOK_MOD 41
3113#define TOK_PLUSPLUS 42
3114#define TOK_MINUSMINUS 43
3115#define TOK_BANG 44
3116#define TOK_ARROW 45
3117#define TOK_DOT 46
3118#define TOK_TILDE 47
3119#define TOK_LIT_STRING 48
3120#define TOK_LIT_CHAR 49
3121#define TOK_LIT_INT 50
3122#define TOK_LIT_FLOAT 51
3123#define TOK_MACRO 52
3124#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003125
Jason Schildt27b85112005-08-10 14:31:52 +00003126#define TOK_IDENT 54
3127#define TOK_STRUCT_NAME 55
3128#define TOK_ENUM_CONST 56
3129#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003130
Jason Schildt27b85112005-08-10 14:31:52 +00003131#define TOK_AUTO 58
3132#define TOK_BREAK 59
3133#define TOK_CASE 60
3134#define TOK_CHAR 61
3135#define TOK_CONST 62
3136#define TOK_CONTINUE 63
3137#define TOK_DEFAULT 64
3138#define TOK_DO 65
3139#define TOK_DOUBLE 66
3140#define TOK_ELSE 67
3141#define TOK_ENUM 68
3142#define TOK_EXTERN 69
3143#define TOK_FLOAT 70
3144#define TOK_FOR 71
3145#define TOK_GOTO 72
3146#define TOK_IF 73
3147#define TOK_INLINE 74
3148#define TOK_INT 75
3149#define TOK_LONG 76
3150#define TOK_REGISTER 77
3151#define TOK_RESTRICT 78
3152#define TOK_RETURN 79
3153#define TOK_SHORT 80
3154#define TOK_SIGNED 81
3155#define TOK_SIZEOF 82
3156#define TOK_STATIC 83
3157#define TOK_STRUCT 84
3158#define TOK_SWITCH 85
3159#define TOK_TYPEDEF 86
3160#define TOK_UNION 87
3161#define TOK_UNSIGNED 88
3162#define TOK_VOID 89
3163#define TOK_VOLATILE 90
3164#define TOK_WHILE 91
3165#define TOK_ASM 92
3166#define TOK_ATTRIBUTE 93
3167#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003168#define TOK_FIRST_KEYWORD TOK_AUTO
3169#define TOK_LAST_KEYWORD TOK_ALIGNOF
3170
Eric Biedermancb364952004-11-15 10:46:44 +00003171#define TOK_MDEFINE 100
3172#define TOK_MDEFINED 101
3173#define TOK_MUNDEF 102
3174#define TOK_MINCLUDE 103
3175#define TOK_MLINE 104
3176#define TOK_MERROR 105
3177#define TOK_MWARNING 106
3178#define TOK_MPRAGMA 107
3179#define TOK_MIFDEF 108
3180#define TOK_MIFNDEF 109
3181#define TOK_MELIF 110
3182#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003183
Eric Biedermancb364952004-11-15 10:46:44 +00003184#define TOK_FIRST_MACRO TOK_MDEFINE
3185#define TOK_LAST_MACRO TOK_MENDIF
Stefan Reinauer14e22772010-04-27 06:56:47 +00003186
Eric Biedermancb364952004-11-15 10:46:44 +00003187#define TOK_MIF 112
3188#define TOK_MELSE 113
3189#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003190
Eric Biedermancb364952004-11-15 10:46:44 +00003191#define TOK_EOL 115
3192#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003193
3194static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003195[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003196[TOK_SPACE ] = ":space:",
3197[TOK_SEMI ] = ";",
3198[TOK_LBRACE ] = "{",
3199[TOK_RBRACE ] = "}",
3200[TOK_COMMA ] = ",",
3201[TOK_EQ ] = "=",
3202[TOK_COLON ] = ":",
3203[TOK_LBRACKET ] = "[",
3204[TOK_RBRACKET ] = "]",
3205[TOK_LPAREN ] = "(",
3206[TOK_RPAREN ] = ")",
3207[TOK_STAR ] = "*",
3208[TOK_DOTS ] = "...",
3209[TOK_MORE ] = ">",
3210[TOK_LESS ] = "<",
3211[TOK_TIMESEQ ] = "*=",
3212[TOK_DIVEQ ] = "/=",
3213[TOK_MODEQ ] = "%=",
3214[TOK_PLUSEQ ] = "+=",
3215[TOK_MINUSEQ ] = "-=",
3216[TOK_SLEQ ] = "<<=",
3217[TOK_SREQ ] = ">>=",
3218[TOK_ANDEQ ] = "&=",
3219[TOK_XOREQ ] = "^=",
3220[TOK_OREQ ] = "|=",
3221[TOK_EQEQ ] = "==",
3222[TOK_NOTEQ ] = "!=",
3223[TOK_QUEST ] = "?",
3224[TOK_LOGOR ] = "||",
3225[TOK_LOGAND ] = "&&",
3226[TOK_OR ] = "|",
3227[TOK_AND ] = "&",
3228[TOK_XOR ] = "^",
3229[TOK_LESSEQ ] = "<=",
3230[TOK_MOREEQ ] = ">=",
3231[TOK_SL ] = "<<",
3232[TOK_SR ] = ">>",
3233[TOK_PLUS ] = "+",
3234[TOK_MINUS ] = "-",
3235[TOK_DIV ] = "/",
3236[TOK_MOD ] = "%",
3237[TOK_PLUSPLUS ] = "++",
3238[TOK_MINUSMINUS ] = "--",
3239[TOK_BANG ] = "!",
3240[TOK_ARROW ] = "->",
3241[TOK_DOT ] = ".",
3242[TOK_TILDE ] = "~",
3243[TOK_LIT_STRING ] = ":string:",
3244[TOK_IDENT ] = ":ident:",
3245[TOK_TYPE_NAME ] = ":typename:",
3246[TOK_LIT_CHAR ] = ":char:",
3247[TOK_LIT_INT ] = ":integer:",
3248[TOK_LIT_FLOAT ] = ":float:",
3249[TOK_MACRO ] = "#",
3250[TOK_CONCATENATE ] = "##",
3251
3252[TOK_AUTO ] = "auto",
3253[TOK_BREAK ] = "break",
3254[TOK_CASE ] = "case",
3255[TOK_CHAR ] = "char",
3256[TOK_CONST ] = "const",
3257[TOK_CONTINUE ] = "continue",
3258[TOK_DEFAULT ] = "default",
3259[TOK_DO ] = "do",
3260[TOK_DOUBLE ] = "double",
3261[TOK_ELSE ] = "else",
3262[TOK_ENUM ] = "enum",
3263[TOK_EXTERN ] = "extern",
3264[TOK_FLOAT ] = "float",
3265[TOK_FOR ] = "for",
3266[TOK_GOTO ] = "goto",
3267[TOK_IF ] = "if",
3268[TOK_INLINE ] = "inline",
3269[TOK_INT ] = "int",
3270[TOK_LONG ] = "long",
3271[TOK_REGISTER ] = "register",
3272[TOK_RESTRICT ] = "restrict",
3273[TOK_RETURN ] = "return",
3274[TOK_SHORT ] = "short",
3275[TOK_SIGNED ] = "signed",
3276[TOK_SIZEOF ] = "sizeof",
3277[TOK_STATIC ] = "static",
3278[TOK_STRUCT ] = "struct",
3279[TOK_SWITCH ] = "switch",
3280[TOK_TYPEDEF ] = "typedef",
3281[TOK_UNION ] = "union",
3282[TOK_UNSIGNED ] = "unsigned",
3283[TOK_VOID ] = "void",
3284[TOK_VOLATILE ] = "volatile",
3285[TOK_WHILE ] = "while",
3286[TOK_ASM ] = "asm",
3287[TOK_ATTRIBUTE ] = "__attribute__",
3288[TOK_ALIGNOF ] = "__alignof__",
3289
Eric Biederman41203d92004-11-08 09:31:09 +00003290[TOK_MDEFINE ] = "#define",
3291[TOK_MDEFINED ] = "#defined",
3292[TOK_MUNDEF ] = "#undef",
3293[TOK_MINCLUDE ] = "#include",
3294[TOK_MLINE ] = "#line",
3295[TOK_MERROR ] = "#error",
3296[TOK_MWARNING ] = "#warning",
3297[TOK_MPRAGMA ] = "#pragma",
3298[TOK_MIFDEF ] = "#ifdef",
3299[TOK_MIFNDEF ] = "#ifndef",
3300[TOK_MELIF ] = "#elif",
3301[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003302
Eric Biederman41203d92004-11-08 09:31:09 +00003303[TOK_MIF ] = "#if",
3304[TOK_MELSE ] = "#else",
3305[TOK_MIDENT ] = "#:ident:",
Stefan Reinauer14e22772010-04-27 06:56:47 +00003306[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003307[TOK_EOF ] = "EOF",
3308};
3309
3310static unsigned int hash(const char *str, int str_len)
3311{
3312 unsigned int hash;
3313 const char *end;
3314 end = str + str_len;
3315 hash = 0;
3316 for(; str < end; str++) {
3317 hash = (hash *263) + *str;
3318 }
3319 hash = hash & (HASH_TABLE_SIZE -1);
3320 return hash;
3321}
3322
3323static struct hash_entry *lookup(
3324 struct compile_state *state, const char *name, int name_len)
3325{
3326 struct hash_entry *entry;
3327 unsigned int index;
3328 index = hash(name, name_len);
3329 entry = state->hash_table[index];
Stefan Reinauer14e22772010-04-27 06:56:47 +00003330 while(entry &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003331 ((entry->name_len != name_len) ||
3332 (memcmp(entry->name, name, name_len) != 0))) {
3333 entry = entry->next;
3334 }
3335 if (!entry) {
3336 char *new_name;
3337 /* Get a private copy of the name */
3338 new_name = xmalloc(name_len + 1, "hash_name");
3339 memcpy(new_name, name, name_len);
3340 new_name[name_len] = '\0';
3341
3342 /* Create a new hash entry */
3343 entry = xcmalloc(sizeof(*entry), "hash_entry");
3344 entry->next = state->hash_table[index];
3345 entry->name = new_name;
3346 entry->name_len = name_len;
3347
3348 /* Place the new entry in the hash table */
3349 state->hash_table[index] = entry;
3350 }
3351 return entry;
3352}
3353
3354static void ident_to_keyword(struct compile_state *state, struct token *tk)
3355{
3356 struct hash_entry *entry;
3357 entry = tk->ident;
3358 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3359 (entry->tok == TOK_ENUM_CONST) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00003360 ((entry->tok >= TOK_FIRST_KEYWORD) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003361 (entry->tok <= TOK_LAST_KEYWORD)))) {
3362 tk->tok = entry->tok;
3363 }
3364}
3365
3366static void ident_to_macro(struct compile_state *state, struct token *tk)
3367{
3368 struct hash_entry *entry;
3369 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003370 if (!entry)
3371 return;
3372 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003373 tk->tok = entry->tok;
3374 }
Eric Biederman41203d92004-11-08 09:31:09 +00003375 else if (entry->tok == TOK_IF) {
3376 tk->tok = TOK_MIF;
3377 }
3378 else if (entry->tok == TOK_ELSE) {
3379 tk->tok = TOK_MELSE;
3380 }
3381 else {
3382 tk->tok = TOK_MIDENT;
3383 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003384}
3385
3386static void hash_keyword(
3387 struct compile_state *state, const char *keyword, int tok)
3388{
3389 struct hash_entry *entry;
3390 entry = lookup(state, keyword, strlen(keyword));
3391 if (entry && entry->tok != TOK_UNKNOWN) {
3392 die("keyword %s already hashed", keyword);
3393 }
3394 entry->tok = tok;
3395}
3396
Eric Biederman90089602004-05-28 14:11:54 +00003397static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003398 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003399 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003400{
3401 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003402 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003403 error(state, 0, "%s already defined", ident->name);
3404 }
3405 sym = xcmalloc(sizeof(*sym), "symbol");
3406 sym->ident = ident;
3407 sym->def = def;
3408 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003409 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003410 sym->next = *chain;
3411 *chain = sym;
3412}
3413
Eric Biederman90089602004-05-28 14:11:54 +00003414static void symbol(
3415 struct compile_state *state, struct hash_entry *ident,
3416 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003417{
Eric Biederman90089602004-05-28 14:11:54 +00003418 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3419}
3420
Stefan Reinauer14e22772010-04-27 06:56:47 +00003421static void var_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003422 struct hash_entry *ident, struct triple *def)
3423{
3424 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3425 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003426 }
Eric Biederman90089602004-05-28 14:11:54 +00003427 symbol(state, ident, &ident->sym_ident, def, def->type);
3428}
3429
Stefan Reinauer14e22772010-04-27 06:56:47 +00003430static void label_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003431 struct hash_entry *ident, struct triple *label, int depth)
3432{
3433 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003434}
3435
Eric Biedermanb138ac82003-04-22 18:44:01 +00003436static void start_scope(struct compile_state *state)
3437{
3438 state->scope_depth++;
3439}
3440
Eric Biederman90089602004-05-28 14:11:54 +00003441static void end_scope_syms(struct compile_state *state,
3442 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003443{
3444 struct symbol *sym, *next;
3445 sym = *chain;
3446 while(sym && (sym->scope_depth == depth)) {
3447 next = sym->next;
3448 xfree(sym);
3449 sym = next;
3450 }
3451 *chain = sym;
3452}
3453
3454static void end_scope(struct compile_state *state)
3455{
3456 int i;
3457 int depth;
3458 /* Walk through the hash table and remove all symbols
Stefan Reinauer14e22772010-04-27 06:56:47 +00003459 * in the current scope.
Eric Biedermanb138ac82003-04-22 18:44:01 +00003460 */
3461 depth = state->scope_depth;
3462 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3463 struct hash_entry *entry;
3464 entry = state->hash_table[i];
3465 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003466 end_scope_syms(state, &entry->sym_label, depth);
3467 end_scope_syms(state, &entry->sym_tag, depth);
3468 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003469 entry = entry->next;
3470 }
3471 }
3472 state->scope_depth = depth - 1;
3473}
3474
3475static void register_keywords(struct compile_state *state)
3476{
3477 hash_keyword(state, "auto", TOK_AUTO);
3478 hash_keyword(state, "break", TOK_BREAK);
3479 hash_keyword(state, "case", TOK_CASE);
3480 hash_keyword(state, "char", TOK_CHAR);
3481 hash_keyword(state, "const", TOK_CONST);
3482 hash_keyword(state, "continue", TOK_CONTINUE);
3483 hash_keyword(state, "default", TOK_DEFAULT);
3484 hash_keyword(state, "do", TOK_DO);
3485 hash_keyword(state, "double", TOK_DOUBLE);
3486 hash_keyword(state, "else", TOK_ELSE);
3487 hash_keyword(state, "enum", TOK_ENUM);
3488 hash_keyword(state, "extern", TOK_EXTERN);
3489 hash_keyword(state, "float", TOK_FLOAT);
3490 hash_keyword(state, "for", TOK_FOR);
3491 hash_keyword(state, "goto", TOK_GOTO);
3492 hash_keyword(state, "if", TOK_IF);
3493 hash_keyword(state, "inline", TOK_INLINE);
3494 hash_keyword(state, "int", TOK_INT);
3495 hash_keyword(state, "long", TOK_LONG);
3496 hash_keyword(state, "register", TOK_REGISTER);
3497 hash_keyword(state, "restrict", TOK_RESTRICT);
3498 hash_keyword(state, "return", TOK_RETURN);
3499 hash_keyword(state, "short", TOK_SHORT);
3500 hash_keyword(state, "signed", TOK_SIGNED);
3501 hash_keyword(state, "sizeof", TOK_SIZEOF);
3502 hash_keyword(state, "static", TOK_STATIC);
3503 hash_keyword(state, "struct", TOK_STRUCT);
3504 hash_keyword(state, "switch", TOK_SWITCH);
3505 hash_keyword(state, "typedef", TOK_TYPEDEF);
3506 hash_keyword(state, "union", TOK_UNION);
3507 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3508 hash_keyword(state, "void", TOK_VOID);
3509 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003510 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003511 hash_keyword(state, "while", TOK_WHILE);
3512 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003513 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003514 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3515 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3516}
3517
3518static void register_macro_keywords(struct compile_state *state)
3519{
Eric Biederman41203d92004-11-08 09:31:09 +00003520 hash_keyword(state, "define", TOK_MDEFINE);
3521 hash_keyword(state, "defined", TOK_MDEFINED);
3522 hash_keyword(state, "undef", TOK_MUNDEF);
3523 hash_keyword(state, "include", TOK_MINCLUDE);
3524 hash_keyword(state, "line", TOK_MLINE);
3525 hash_keyword(state, "error", TOK_MERROR);
3526 hash_keyword(state, "warning", TOK_MWARNING);
3527 hash_keyword(state, "pragma", TOK_MPRAGMA);
3528 hash_keyword(state, "ifdef", TOK_MIFDEF);
3529 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3530 hash_keyword(state, "elif", TOK_MELIF);
3531 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003532}
3533
Eric Biederman90089602004-05-28 14:11:54 +00003534
3535static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3536{
3537 if (ident->sym_define != 0) {
3538 struct macro *macro;
3539 struct macro_arg *arg, *anext;
3540 macro = ident->sym_define;
3541 ident->sym_define = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003542
Eric Biederman90089602004-05-28 14:11:54 +00003543 /* Free the macro arguments... */
3544 anext = macro->args;
3545 while(anext) {
3546 arg = anext;
3547 anext = arg->next;
3548 xfree(arg);
3549 }
3550
3551 /* Free the macro buffer */
3552 xfree(macro->buf);
3553
3554 /* Now free the macro itself */
3555 xfree(macro);
3556 }
3557}
3558
Stefan Reinauer14e22772010-04-27 06:56:47 +00003559static void do_define_macro(struct compile_state *state,
3560 struct hash_entry *ident, const char *body,
Eric Biedermancb364952004-11-15 10:46:44 +00003561 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003562{
3563 struct macro *macro;
3564 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003565 size_t body_len;
3566
3567 /* Find the length of the body */
3568 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003569 macro = ident->sym_define;
3570 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003571 int identical_bodies, identical_args;
3572 struct macro_arg *oarg;
3573 /* Explicitly allow identical redfinitions of the same macro */
Stefan Reinauer14e22772010-04-27 06:56:47 +00003574 identical_bodies =
Eric Biedermancb364952004-11-15 10:46:44 +00003575 (macro->buf_len == body_len) &&
3576 (memcmp(macro->buf, body, body_len) == 0);
3577 identical_args = macro->argc == argc;
3578 oarg = macro->args;
3579 arg = args;
3580 while(identical_args && arg) {
3581 identical_args = oarg->ident == arg->ident;
3582 arg = arg->next;
3583 oarg = oarg->next;
3584 }
3585 if (identical_bodies && identical_args) {
3586 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003587 return;
3588 }
3589 error(state, 0, "macro %s already defined\n", ident->name);
3590 }
3591#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003592 fprintf(state->errout, "#define %s: `%*.*s'\n",
3593 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003594#endif
3595 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003596 macro->ident = ident;
3597 macro->buf = body;
3598 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003599 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003600 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003601
3602 ident->sym_define = macro;
3603}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003604
Eric Biedermancb364952004-11-15 10:46:44 +00003605static void define_macro(
3606 struct compile_state *state,
3607 struct hash_entry *ident,
3608 const char *body, int body_len,
3609 int argc, struct macro_arg *args)
3610{
3611 char *buf;
3612 buf = xmalloc(body_len + 1, "macro buf");
3613 memcpy(buf, body, body_len);
3614 buf[body_len] = '\0';
3615 do_define_macro(state, ident, buf, argc, args);
3616}
Eric Biederman90089602004-05-28 14:11:54 +00003617
3618static void register_builtin_macro(struct compile_state *state,
3619 const char *name, const char *value)
3620{
3621 struct hash_entry *ident;
3622
3623 if (value[0] == '(') {
3624 internal_error(state, 0, "Builtin macros with arguments not supported");
3625 }
3626 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003627 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003628}
3629
3630static void register_builtin_macros(struct compile_state *state)
3631{
3632 char buf[30];
3633 char scratch[30];
3634 time_t now;
3635 struct tm *tm;
3636 now = time(NULL);
3637 tm = localtime(&now);
3638
3639 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3640 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3641 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3642 register_builtin_macro(state, "__LINE__", "54321");
3643
3644 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3645 sprintf(buf, "\"%s\"", scratch);
3646 register_builtin_macro(state, "__DATE__", buf);
3647
3648 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3649 sprintf(buf, "\"%s\"", scratch);
3650 register_builtin_macro(state, "__TIME__", buf);
3651
3652 /* I can't be a conforming implementation of C :( */
3653 register_builtin_macro(state, "__STDC__", "0");
3654 /* In particular I don't conform to C99 */
3655 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
Stefan Reinauer14e22772010-04-27 06:56:47 +00003656
Eric Biederman90089602004-05-28 14:11:54 +00003657}
3658
3659static void process_cmdline_macros(struct compile_state *state)
3660{
3661 const char **macro, *name;
3662 struct hash_entry *ident;
3663 for(macro = state->compiler->defines; (name = *macro); macro++) {
3664 const char *body;
3665 size_t name_len;
3666
3667 name_len = strlen(name);
3668 body = strchr(name, '=');
3669 if (!body) {
3670 body = "\0";
3671 } else {
3672 name_len = body - name;
3673 body++;
3674 }
3675 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003676 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003677 }
3678 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3679 ident = lookup(state, name, strlen(name));
3680 undef_macro(state, ident);
3681 }
3682}
3683
Eric Biedermanb138ac82003-04-22 18:44:01 +00003684static int spacep(int c)
3685{
3686 int ret = 0;
3687 switch(c) {
3688 case ' ':
3689 case '\t':
3690 case '\f':
3691 case '\v':
3692 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003693 ret = 1;
3694 break;
3695 }
3696 return ret;
3697}
3698
Eric Biedermanb138ac82003-04-22 18:44:01 +00003699static int digitp(int c)
3700{
3701 int ret = 0;
3702 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003703 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003704 case '5': case '6': case '7': case '8': case '9':
3705 ret = 1;
3706 break;
3707 }
3708 return ret;
3709}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003710static int digval(int c)
3711{
3712 int val = -1;
3713 if ((c >= '0') && (c <= '9')) {
3714 val = c - '0';
3715 }
3716 return val;
3717}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003718
3719static int hexdigitp(int c)
3720{
3721 int ret = 0;
3722 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003723 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003724 case '5': case '6': case '7': case '8': case '9':
3725 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3726 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3727 ret = 1;
3728 break;
3729 }
3730 return ret;
3731}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003732static int hexdigval(int c)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003733{
3734 int val = -1;
3735 if ((c >= '0') && (c <= '9')) {
3736 val = c - '0';
3737 }
3738 else if ((c >= 'A') && (c <= 'F')) {
3739 val = 10 + (c - 'A');
3740 }
3741 else if ((c >= 'a') && (c <= 'f')) {
3742 val = 10 + (c - 'a');
3743 }
3744 return val;
3745}
3746
3747static int octdigitp(int c)
3748{
3749 int ret = 0;
3750 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003751 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003752 case '4': case '5': case '6': case '7':
3753 ret = 1;
3754 break;
3755 }
3756 return ret;
3757}
3758static int octdigval(int c)
3759{
3760 int val = -1;
3761 if ((c >= '0') && (c <= '7')) {
3762 val = c - '0';
3763 }
3764 return val;
3765}
3766
3767static int letterp(int c)
3768{
3769 int ret = 0;
3770 switch(c) {
3771 case 'a': case 'b': case 'c': case 'd': case 'e':
3772 case 'f': case 'g': case 'h': case 'i': case 'j':
3773 case 'k': case 'l': case 'm': case 'n': case 'o':
3774 case 'p': case 'q': case 'r': case 's': case 't':
3775 case 'u': case 'v': case 'w': case 'x': case 'y':
3776 case 'z':
3777 case 'A': case 'B': case 'C': case 'D': case 'E':
3778 case 'F': case 'G': case 'H': case 'I': case 'J':
3779 case 'K': case 'L': case 'M': case 'N': case 'O':
3780 case 'P': case 'Q': case 'R': case 'S': case 'T':
3781 case 'U': case 'V': case 'W': case 'X': case 'Y':
3782 case 'Z':
3783 case '_':
3784 ret = 1;
3785 break;
3786 }
3787 return ret;
3788}
3789
Eric Biederman90089602004-05-28 14:11:54 +00003790static const char *identifier(const char *str, const char *end)
3791{
3792 if (letterp(*str)) {
3793 for(; str < end; str++) {
3794 int c;
3795 c = *str;
3796 if (!letterp(c) && !digitp(c)) {
3797 break;
3798 }
3799 }
3800 }
3801 return str;
3802}
3803
Eric Biedermanb138ac82003-04-22 18:44:01 +00003804static int char_value(struct compile_state *state,
3805 const signed char **strp, const signed char *end)
3806{
3807 const signed char *str;
3808 int c;
3809 str = *strp;
3810 c = *str++;
3811 if ((c == '\\') && (str < end)) {
3812 switch(*str) {
3813 case 'n': c = '\n'; str++; break;
3814 case 't': c = '\t'; str++; break;
3815 case 'v': c = '\v'; str++; break;
3816 case 'b': c = '\b'; str++; break;
3817 case 'r': c = '\r'; str++; break;
3818 case 'f': c = '\f'; str++; break;
3819 case 'a': c = '\a'; str++; break;
3820 case '\\': c = '\\'; str++; break;
3821 case '?': c = '?'; str++; break;
3822 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003823 case '"': c = '"'; str++; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003824 case 'x':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003825 c = 0;
3826 str++;
3827 while((str < end) && hexdigitp(*str)) {
3828 c <<= 4;
3829 c += hexdigval(*str);
3830 str++;
3831 }
3832 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003833 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003834 case '4': case '5': case '6': case '7':
3835 c = 0;
3836 while((str < end) && octdigitp(*str)) {
3837 c <<= 3;
3838 c += octdigval(*str);
3839 str++;
3840 }
3841 break;
3842 default:
3843 error(state, 0, "Invalid character constant");
3844 break;
3845 }
3846 }
3847 *strp = str;
3848 return c;
3849}
3850
Eric Biedermancb364952004-11-15 10:46:44 +00003851static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003852{
Eric Biedermancb364952004-11-15 10:46:44 +00003853 const char *end = file->buf + file->size;
3854 while(pos < end) {
3855 /* Lookup the character */
3856 int size = 1;
3857 int c = *pos;
3858 /* Is this a trigraph? */
3859 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003860 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003861 {
3862 switch(pos[2]) {
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 case '-': c = '~'; break;
3872 }
3873 if (c != '?') {
3874 size = 3;
3875 }
3876 }
3877 /* Is this an escaped newline? */
3878 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003879 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003880 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003881 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003882 /* At the start of a line just eat it */
3883 if (pos == file->pos) {
3884 file->line++;
3885 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003886 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003887 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003888 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003889 }
3890 /* Do I need to ga any farther? */
3891 else if (index == 0) {
3892 break;
3893 }
3894 /* Process a normal character */
3895 else {
3896 pos += size;
3897 index -= 1;
3898 }
3899 }
3900 return pos;
3901}
3902
3903static int get_char(struct file_state *file, const char *pos)
3904{
3905 const char *end = file->buf + file->size;
3906 int c;
3907 c = -1;
3908 pos = next_char(file, pos, 0);
3909 if (pos < end) {
3910 /* Lookup the character */
3911 c = *pos;
3912 /* If it is a trigraph get the trigraph value */
3913 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003914 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003915 {
3916 switch(pos[2]) {
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 case '-': c = '~'; break;
3926 }
3927 }
3928 }
3929 return c;
3930}
3931
3932static void eat_chars(struct file_state *file, const char *targ)
3933{
3934 const char *pos = file->pos;
3935 while(pos < targ) {
3936 /* Do we have a newline? */
3937 if (pos[0] == '\n') {
3938 file->line++;
3939 file->report_line++;
3940 file->line_start = pos + 1;
3941 }
3942 pos++;
3943 }
3944 file->pos = pos;
3945}
3946
3947
3948static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3949{
3950 size_t len;
3951 len = 0;
3952 while(src < end) {
3953 src = next_char(file, src, 1);
3954 len++;
3955 }
3956 return len;
3957}
3958
Stefan Reinauer14e22772010-04-27 06:56:47 +00003959static void char_strcpy(char *dest,
Eric Biedermancb364952004-11-15 10:46:44 +00003960 struct file_state *file, const char *src, const char *end)
3961{
3962 while(src < end) {
3963 int c;
3964 c = get_char(file, src);
3965 src = next_char(file, src, 1);
3966 *dest++ = c;
3967 }
3968}
3969
Stefan Reinauer14e22772010-04-27 06:56:47 +00003970static char *char_strdup(struct file_state *file,
Eric Biedermancb364952004-11-15 10:46:44 +00003971 const char *start, const char *end, const char *id)
3972{
3973 char *str;
3974 size_t str_len;
3975 str_len = char_strlen(file, start, end);
3976 str = xcmalloc(str_len + 1, id);
3977 char_strcpy(str, file, start, end);
3978 str[str_len] = '\0';
3979 return str;
3980}
3981
3982static const char *after_digits(struct file_state *file, const char *ptr)
3983{
3984 while(digitp(get_char(file, ptr))) {
3985 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003986 }
3987 return ptr;
3988}
3989
Eric Biedermancb364952004-11-15 10:46:44 +00003990static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003991{
Eric Biedermancb364952004-11-15 10:46:44 +00003992 while(octdigitp(get_char(file, ptr))) {
3993 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003994 }
3995 return ptr;
3996}
3997
Eric Biedermancb364952004-11-15 10:46:44 +00003998static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003999{
Eric Biedermancb364952004-11-15 10:46:44 +00004000 while(hexdigitp(get_char(file, ptr))) {
4001 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004002 }
4003 return ptr;
4004}
4005
Eric Biedermancb364952004-11-15 10:46:44 +00004006static const char *after_alnums(struct file_state *file, const char *ptr)
4007{
4008 int c;
4009 c = get_char(file, ptr);
4010 while(letterp(c) || digitp(c)) {
4011 ptr = next_char(file, ptr, 1);
4012 c = get_char(file, ptr);
4013 }
4014 return ptr;
4015}
4016
4017static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004018 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004019{
4020 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004021
Eric Biedermanb138ac82003-04-22 18:44:01 +00004022 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004023 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004024
4025 /* Store the copy in the token */
4026 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004027 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004028}
4029
Stefan Reinauer14e22772010-04-27 06:56:47 +00004030static void raw_next_token(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004031 struct file_state *file, struct token *tk)
4032{
4033 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004034 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004035 const char *tokp;
4036 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004037 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004038
Eric Biedermanb138ac82003-04-22 18:44:01 +00004039 tk->str_len = 0;
4040 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004041 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004042 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004043 c = get_char(file, tokp);
4044 tokp = next_char(file, tokp, 1);
4045 eat = 0;
4046 c1 = get_char(file, tokp);
4047 c2 = get_char(file, next_char(file, tokp, 1));
4048 c3 = get_char(file, next_char(file, tokp, 2));
4049
4050 /* The end of the file */
4051 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004052 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004053 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004054 /* Whitespace */
4055 else if (spacep(c)) {
4056 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004057 while (spacep(get_char(file, tokp))) {
4058 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004059 }
4060 }
4061 /* EOL Comments */
4062 else if ((c == '/') && (c1 == '/')) {
4063 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004064 tokp = next_char(file, tokp, 1);
4065 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004066 /* Advance to the next character only after we verify
Stefan Reinauer14e22772010-04-27 06:56:47 +00004067 * the current character is not a newline.
Eric Biederman57183382006-12-02 16:48:48 +00004068 * EOL is special to the preprocessor so we don't
4069 * want to loose any.
4070 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004071 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004072 break;
4073 }
Eric Biederman57183382006-12-02 16:48:48 +00004074 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004075 }
4076 }
4077 /* Comments */
4078 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004079 tokp = next_char(file, tokp, 2);
4080 c = c2;
4081 while((c1 = get_char(file, tokp)) != -1) {
4082 tokp = next_char(file, tokp, 1);
4083 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004084 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004085 break;
4086 }
Eric Biedermancb364952004-11-15 10:46:44 +00004087 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004088 }
4089 if (tok == TOK_UNKNOWN) {
4090 error(state, 0, "unterminated comment");
4091 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004092 }
4093 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004094 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004095 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004096
Eric Biedermancb364952004-11-15 10:46:44 +00004097 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004098 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004099 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004100 }
Eric Biedermancb364952004-11-15 10:46:44 +00004101 while((c = get_char(file, tokp)) != -1) {
4102 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004103 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004104 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004105 }
Eric Biedermancb364952004-11-15 10:46:44 +00004106 else if (c == '\\') {
4107 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004108 }
4109 else if (c == '"') {
4110 tok = TOK_LIT_STRING;
4111 break;
4112 }
4113 }
4114 if (tok == TOK_UNKNOWN) {
4115 error(state, 0, "unterminated string constant");
4116 }
Eric Biedermancb364952004-11-15 10:46:44 +00004117 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004118 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004119 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004120
4121 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004122 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004123 }
4124 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004125 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004126 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004127
Eric Biedermancb364952004-11-15 10:46:44 +00004128 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004129 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004130 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004131 }
Eric Biedermancb364952004-11-15 10:46:44 +00004132 while((c = get_char(file, tokp)) != -1) {
4133 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004134 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004135 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004136 }
Eric Biedermancb364952004-11-15 10:46:44 +00004137 else if (c == '\\') {
4138 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004139 }
4140 else if (c == '\'') {
4141 tok = TOK_LIT_CHAR;
4142 break;
4143 }
4144 }
4145 if (tok == TOK_UNKNOWN) {
4146 error(state, 0, "unterminated character constant");
4147 }
Eric Biedermancb364952004-11-15 10:46:44 +00004148 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004149 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004151
4152 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004153 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004154 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004155 /* integer and floating constants
Eric Biedermanb138ac82003-04-22 18:44:01 +00004156 * Integer Constants
4157 * {digits}
4158 * 0[Xx]{hexdigits}
4159 * 0{octdigit}+
Stefan Reinauer14e22772010-04-27 06:56:47 +00004160 *
Eric Biedermanb138ac82003-04-22 18:44:01 +00004161 * Floating constants
4162 * {digits}.{digits}[Ee][+-]?{digits}
4163 * {digits}.{digits}
4164 * {digits}[Ee][+-]?{digits}
4165 * .{digits}[Ee][+-]?{digits}
4166 * .{digits}
4167 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004168 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004169 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004170 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004171 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004172 is_float = 0;
4173 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004174 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004175 }
4176 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004177 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004178 }
Eric Biedermancb364952004-11-15 10:46:44 +00004179 cn = get_char(file, next);
4180 if (cn == '.') {
4181 next = next_char(file, next, 1);
4182 next = after_digits(file, next);
4183 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004184 }
Eric Biedermancb364952004-11-15 10:46:44 +00004185 cn = get_char(file, next);
4186 if ((cn == 'e') || (cn == 'E')) {
4187 const char *new;
4188 next = next_char(file, next, 1);
4189 cn = get_char(file, next);
4190 if ((cn == '+') || (cn == '-')) {
4191 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004192 }
Eric Biedermancb364952004-11-15 10:46:44 +00004193 new = after_digits(file, next);
4194 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004195 next = new;
4196 }
4197 if (is_float) {
4198 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004199 cn = get_char(file, next);
4200 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4201 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004202 }
4203 }
4204 if (!is_float && digitp(c)) {
4205 tok = TOK_LIT_INT;
4206 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004207 next = next_char(file, tokp, 1);
4208 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004209 }
4210 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004211 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004212 }
4213 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004214 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004215 }
4216 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004217 cn = get_char(file, next);
4218 if ((cn == 'u') || (cn == 'U')) {
4219 next = next_char(file, next, 1);
4220 cn = get_char(file, next);
4221 if ((cn == 'l') || (cn == 'L')) {
4222 next = next_char(file, next, 1);
4223 cn = get_char(file, next);
4224 }
4225 if ((cn == 'l') || (cn == 'L')) {
4226 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004227 }
4228 }
Eric Biedermancb364952004-11-15 10:46:44 +00004229 else if ((cn == 'l') || (cn == 'L')) {
4230 next = next_char(file, next, 1);
4231 cn = get_char(file, next);
4232 if ((cn == 'l') || (cn == 'L')) {
4233 next = next_char(file, next, 1);
4234 cn = get_char(file, next);
4235 }
4236 if ((cn == 'u') || (cn == 'U')) {
4237 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004238 }
4239 }
4240 }
Eric Biedermancb364952004-11-15 10:46:44 +00004241 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004242
4243 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004244 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004245 }
4246 /* identifiers */
4247 else if (letterp(c)) {
4248 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004249
4250 /* Find and save the identifier string */
4251 tokp = after_alnums(file, tokp);
4252 save_string(file, tk, token, tokp, "identifier");
4253
4254 /* Look up to see which identifier it is */
4255 tk->ident = lookup(state, tk->val.str, tk->str_len);
4256
4257 /* Free the identifier string */
4258 tk->str_len = 0;
4259 xfree(tk->val.str);
4260
Eric Biederman90089602004-05-28 14:11:54 +00004261 /* See if this identifier can be macro expanded */
4262 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004263 c = get_char(file, tokp);
4264 if (c == '$') {
4265 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004266 tk->val.notmacro = 1;
4267 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004268 }
4269 /* C99 alternate macro characters */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004270 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004271 eat += 3;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004272 tok = TOK_CONCATENATE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004273 }
Eric Biedermancb364952004-11-15 10:46:44 +00004274 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4275 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4276 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4277 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4278 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4279 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4280 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4281 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4282 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4283 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4284 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4285 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4286 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4287 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4288 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4289 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4290 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4291 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4292 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4293 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4294 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4295 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4296 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4297 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4298 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4299 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4300 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4301 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004302 else if (c == ';') { tok = TOK_SEMI; }
4303 else if (c == '{') { tok = TOK_LBRACE; }
4304 else if (c == '}') { tok = TOK_RBRACE; }
4305 else if (c == ',') { tok = TOK_COMMA; }
4306 else if (c == '=') { tok = TOK_EQ; }
4307 else if (c == ':') { tok = TOK_COLON; }
4308 else if (c == '[') { tok = TOK_LBRACKET; }
4309 else if (c == ']') { tok = TOK_RBRACKET; }
4310 else if (c == '(') { tok = TOK_LPAREN; }
4311 else if (c == ')') { tok = TOK_RPAREN; }
4312 else if (c == '*') { tok = TOK_STAR; }
4313 else if (c == '>') { tok = TOK_MORE; }
4314 else if (c == '<') { tok = TOK_LESS; }
4315 else if (c == '?') { tok = TOK_QUEST; }
4316 else if (c == '|') { tok = TOK_OR; }
4317 else if (c == '&') { tok = TOK_AND; }
4318 else if (c == '^') { tok = TOK_XOR; }
4319 else if (c == '+') { tok = TOK_PLUS; }
4320 else if (c == '-') { tok = TOK_MINUS; }
4321 else if (c == '/') { tok = TOK_DIV; }
4322 else if (c == '%') { tok = TOK_MOD; }
4323 else if (c == '!') { tok = TOK_BANG; }
4324 else if (c == '.') { tok = TOK_DOT; }
4325 else if (c == '~') { tok = TOK_TILDE; }
4326 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004327 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004328
Eric Biedermancb364952004-11-15 10:46:44 +00004329 tokp = next_char(file, tokp, eat);
4330 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004331 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004332 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004333}
4334
4335static void check_tok(struct compile_state *state, struct token *tk, int tok)
4336{
4337 if (tk->tok != tok) {
4338 const char *name1, *name2;
4339 name1 = tokens[tk->tok];
4340 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004341 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004342 name2 = tk->ident->name;
4343 }
4344 error(state, 0, "\tfound %s %s expected %s",
4345 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004346 }
4347}
4348
Eric Biederman90089602004-05-28 14:11:54 +00004349struct macro_arg_value {
4350 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004351 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004352 size_t len;
4353};
4354static struct macro_arg_value *read_macro_args(
Stefan Reinauer14e22772010-04-27 06:56:47 +00004355 struct compile_state *state, struct macro *macro,
Eric Biederman90089602004-05-28 14:11:54 +00004356 struct file_state *file, struct token *tk)
4357{
4358 struct macro_arg_value *argv;
4359 struct macro_arg *arg;
4360 int paren_depth;
4361 int i;
4362
4363 if (macro->argc == 0) {
4364 do {
4365 raw_next_token(state, file, tk);
4366 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004367 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004368 }
4369 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4370 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4371 argv[i].value = 0;
4372 argv[i].len = 0;
4373 argv[i].ident = arg->ident;
4374 }
4375 paren_depth = 0;
4376 i = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004377
Eric Biederman90089602004-05-28 14:11:54 +00004378 for(;;) {
4379 const char *start;
4380 size_t len;
4381 start = file->pos;
4382 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004383
Eric Biederman90089602004-05-28 14:11:54 +00004384 if (!paren_depth && (tk->tok == TOK_COMMA) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004385 (argv[i].ident != state->i___VA_ARGS__))
Eric Biederman90089602004-05-28 14:11:54 +00004386 {
4387 i++;
4388 if (i >= macro->argc) {
4389 error(state, 0, "too many args to %s\n",
4390 macro->ident->name);
4391 }
4392 continue;
4393 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004394
Eric Biederman90089602004-05-28 14:11:54 +00004395 if (tk->tok == TOK_LPAREN) {
4396 paren_depth++;
4397 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004398
Eric Biederman90089602004-05-28 14:11:54 +00004399 if (tk->tok == TOK_RPAREN) {
4400 if (paren_depth == 0) {
4401 break;
4402 }
4403 paren_depth--;
4404 }
4405 if (tk->tok == TOK_EOF) {
4406 error(state, 0, "End of file encountered while parsing macro arguments");
4407 }
Eric Biedermancb364952004-11-15 10:46:44 +00004408
4409 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004410 argv[i].value = xrealloc(
4411 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004412 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004413 argv[i].len += len;
4414 }
4415 if (i != macro->argc -1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00004416 error(state, 0, "missing %s arg %d\n",
Eric Biederman90089602004-05-28 14:11:54 +00004417 macro->ident->name, i +2);
4418 }
4419 return argv;
4420}
4421
4422
4423static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4424{
4425 int i;
4426 for(i = 0; i < macro->argc; i++) {
4427 xfree(argv[i].value);
4428 }
4429 xfree(argv);
4430}
4431
4432struct macro_buf {
4433 char *str;
4434 size_t len, pos;
4435};
4436
Eric Biedermancb364952004-11-15 10:46:44 +00004437static void grow_macro_buf(struct compile_state *state,
4438 const char *id, struct macro_buf *buf,
4439 size_t grow)
4440{
4441 if ((buf->pos + grow) >= buf->len) {
4442 buf->str = xrealloc(buf->str, buf->len + grow, id);
4443 buf->len += grow;
4444 }
4445}
4446
Eric Biederman90089602004-05-28 14:11:54 +00004447static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004448 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004449 const char *fstart, size_t flen)
4450{
Eric Biedermancb364952004-11-15 10:46:44 +00004451 grow_macro_buf(state, id, buf, flen);
4452 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004453#if 0
4454 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4455 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004456 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004457#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004458 buf->pos += flen;
4459}
4460
4461
4462static void append_macro_chars(struct compile_state *state,
4463 const char *id, struct macro_buf *buf,
4464 struct file_state *file, const char *start, const char *end)
4465{
4466 size_t flen;
4467 flen = char_strlen(file, start, end);
4468 grow_macro_buf(state, id, buf, flen);
4469 char_strcpy(buf->str + buf->pos, file, start, end);
4470#if 0
4471 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4472 buf->pos, buf->pos, buf->str,
4473 flen, flen, buf->str + buf->pos);
4474#endif
Eric Biederman90089602004-05-28 14:11:54 +00004475 buf->pos += flen;
4476}
4477
Stefan Reinauer14e22772010-04-27 06:56:47 +00004478static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004479 struct file_state **filep, struct token *tk);
4480
Stefan Reinauer14e22772010-04-27 06:56:47 +00004481static void macro_expand_args(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004482 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4483{
Eric Biedermancb364952004-11-15 10:46:44 +00004484 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004485
Eric Biederman90089602004-05-28 14:11:54 +00004486 for(i = 0; i < macro->argc; i++) {
4487 struct file_state fmacro, *file;
4488 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004489
Eric Biedermancb364952004-11-15 10:46:44 +00004490 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004491 fmacro.basename = argv[i].ident->name;
4492 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004493 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004494 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004495 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004496 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004497 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004498 fmacro.report_line = 1;
4499 fmacro.report_name = fmacro.basename;
4500 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004501 fmacro.macro = 1;
4502 fmacro.trigraphs = 0;
4503 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004504
4505 buf.len = argv[i].len;
4506 buf.str = xmalloc(buf.len, argv[i].ident->name);
4507 buf.pos = 0;
4508
4509 file = &fmacro;
4510 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004511 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004512
Eric Biedermancb364952004-11-15 10:46:44 +00004513 /* If we have recursed into another macro body
4514 * get out of it.
4515 */
Eric Biederman90089602004-05-28 14:11:54 +00004516 if (tk->tok == TOK_EOF) {
4517 struct file_state *old;
4518 old = file;
4519 file = file->prev;
4520 if (!file) {
4521 break;
4522 }
4523 /* old->basename is used keep it */
4524 xfree(old->dirname);
4525 xfree(old->buf);
4526 xfree(old);
4527 continue;
4528 }
4529 else if (tk->ident && tk->ident->sym_define) {
4530 if (compile_macro(state, &file, tk)) {
4531 continue;
4532 }
4533 }
4534
Eric Biedermancb364952004-11-15 10:46:44 +00004535 append_macro_chars(state, macro->ident->name, &buf,
4536 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004537 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004538
Eric Biederman90089602004-05-28 14:11:54 +00004539 xfree(argv[i].value);
4540 argv[i].value = buf.str;
4541 argv[i].len = buf.pos;
4542 }
4543 return;
4544}
4545
4546static void expand_macro(struct compile_state *state,
4547 struct macro *macro, struct macro_buf *buf,
4548 struct macro_arg_value *argv, struct token *tk)
4549{
4550 struct file_state fmacro;
4551 const char space[] = " ";
4552 const char *fstart;
4553 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004554 int i, j;
4555
4556 /* Place the macro body in a dummy file */
4557 fmacro.prev = 0;
4558 fmacro.basename = macro->ident->name;
4559 fmacro.dirname = "";
4560 fmacro.buf = macro->buf;
4561 fmacro.size = macro->buf_len;
4562 fmacro.pos = fmacro.buf;
4563 fmacro.line = 1;
4564 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004565 fmacro.report_line = 1;
4566 fmacro.report_name = fmacro.basename;
4567 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004568 fmacro.macro = 1;
4569 fmacro.trigraphs = 0;
4570 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004571
Eric Biedermancb364952004-11-15 10:46:44 +00004572 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004573 buf->len = macro->buf_len + 3;
4574 buf->str = xmalloc(buf->len, macro->ident->name);
4575 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004576
Eric Biederman90089602004-05-28 14:11:54 +00004577 fstart = fmacro.pos;
4578 raw_next_token(state, &fmacro, tk);
4579 while(tk->tok != TOK_EOF) {
4580 flen = fmacro.pos - fstart;
4581 switch(tk->tok) {
4582 case TOK_IDENT:
Patrick Georgid8051892017-01-02 19:15:44 +01004583 if (macro->argc < 0) {
4584 break;
4585 }
Eric Biederman90089602004-05-28 14:11:54 +00004586 for(i = 0; i < macro->argc; i++) {
4587 if (argv[i].ident == tk->ident) {
4588 break;
4589 }
4590 }
4591 if (i >= macro->argc) {
4592 break;
4593 }
4594 /* Substitute macro parameter */
4595 fstart = argv[i].value;
4596 flen = argv[i].len;
4597 break;
4598 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004599 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004600 break;
4601 }
4602 do {
4603 raw_next_token(state, &fmacro, tk);
4604 } while(tk->tok == TOK_SPACE);
4605 check_tok(state, tk, TOK_IDENT);
4606 for(i = 0; i < macro->argc; i++) {
4607 if (argv[i].ident == tk->ident) {
4608 break;
4609 }
4610 }
4611 if (i >= macro->argc) {
4612 error(state, 0, "parameter `%s' not found",
4613 tk->ident->name);
4614 }
4615 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004616 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004617 for(j = 0; j < argv[i].len; j++) {
4618 char *str = argv[i].value + j;
4619 size_t len = 1;
4620 if (*str == '\\') {
4621 str = "\\";
4622 len = 2;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004623 }
Eric Biederman90089602004-05-28 14:11:54 +00004624 else if (*str == '"') {
4625 str = "\\\"";
4626 len = 2;
4627 }
Eric Biedermancb364952004-11-15 10:46:44 +00004628 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004629 }
Eric Biedermancb364952004-11-15 10:46:44 +00004630 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004631 fstart = 0;
4632 flen = 0;
4633 break;
4634 case TOK_CONCATENATE:
4635 /* Concatenate tokens */
4636 /* Delete the previous whitespace token */
4637 if (buf->str[buf->pos - 1] == ' ') {
4638 buf->pos -= 1;
4639 }
4640 /* Skip the next sequence of whitspace tokens */
4641 do {
4642 fstart = fmacro.pos;
4643 raw_next_token(state, &fmacro, tk);
4644 } while(tk->tok == TOK_SPACE);
4645 /* Restart at the top of the loop.
4646 * I need to process the non white space token.
4647 */
4648 continue;
4649 break;
4650 case TOK_SPACE:
4651 /* Collapse multiple spaces into one */
4652 if (buf->str[buf->pos - 1] != ' ') {
4653 fstart = space;
4654 flen = 1;
4655 } else {
4656 fstart = 0;
4657 flen = 0;
4658 }
4659 break;
4660 default:
4661 break;
4662 }
4663
Eric Biedermancb364952004-11-15 10:46:44 +00004664 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004665
Eric Biederman90089602004-05-28 14:11:54 +00004666 fstart = fmacro.pos;
4667 raw_next_token(state, &fmacro, tk);
4668 }
4669}
4670
4671static void tag_macro_name(struct compile_state *state,
4672 struct macro *macro, struct macro_buf *buf,
4673 struct token *tk)
4674{
4675 /* Guard all instances of the macro name in the replacement
4676 * text from further macro expansion.
4677 */
4678 struct file_state fmacro;
4679 const char *fstart;
4680 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004681
4682 /* Put the old macro expansion buffer in a file */
4683 fmacro.prev = 0;
4684 fmacro.basename = macro->ident->name;
4685 fmacro.dirname = "";
4686 fmacro.buf = buf->str;
4687 fmacro.size = buf->pos;
4688 fmacro.pos = fmacro.buf;
4689 fmacro.line = 1;
4690 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004691 fmacro.report_line = 1;
4692 fmacro.report_name = fmacro.basename;
4693 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004694 fmacro.macro = 1;
4695 fmacro.trigraphs = 0;
4696 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004697
Eric Biedermancb364952004-11-15 10:46:44 +00004698 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004699 buf->len = macro->buf_len + 3;
4700 buf->str = xmalloc(buf->len, macro->ident->name);
4701 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004702
Eric Biederman90089602004-05-28 14:11:54 +00004703 fstart = fmacro.pos;
4704 raw_next_token(state, &fmacro, tk);
4705 while(tk->tok != TOK_EOF) {
4706 flen = fmacro.pos - fstart;
4707 if ((tk->tok == TOK_IDENT) &&
4708 (tk->ident == macro->ident) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004709 (tk->val.notmacro == 0))
Eric Biedermancb364952004-11-15 10:46:44 +00004710 {
4711 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004712 fstart = "$";
4713 flen = 1;
4714 }
4715
Eric Biedermancb364952004-11-15 10:46:44 +00004716 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004717
Eric Biederman90089602004-05-28 14:11:54 +00004718 fstart = fmacro.pos;
4719 raw_next_token(state, &fmacro, tk);
4720 }
4721 xfree(fmacro.buf);
4722}
Eric Biedermancb364952004-11-15 10:46:44 +00004723
Stefan Reinauer14e22772010-04-27 06:56:47 +00004724static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004725 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004726{
4727 struct file_state *file;
4728 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004729 struct macro *macro;
4730 struct macro_arg_value *argv;
4731 struct macro_buf buf;
4732
4733#if 0
4734 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4735#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004736 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004737 macro = ident->sym_define;
4738
4739 /* If this token comes from a macro expansion ignore it */
4740 if (tk->val.notmacro) {
4741 return 0;
4742 }
4743 /* If I am a function like macro and the identifier is not followed
4744 * by a left parenthesis, do nothing.
4745 */
Eric Biedermancb364952004-11-15 10:46:44 +00004746 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004747 return 0;
4748 }
4749
4750 /* Read in the macro arguments */
4751 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004752 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004753 raw_next_token(state, *filep, tk);
4754 check_tok(state, tk, TOK_LPAREN);
4755
4756 argv = read_macro_args(state, macro, *filep, tk);
4757
4758 check_tok(state, tk, TOK_RPAREN);
4759 }
4760 /* Macro expand the macro arguments */
4761 macro_expand_args(state, macro, argv, tk);
4762
4763 buf.str = 0;
4764 buf.len = 0;
4765 buf.pos = 0;
4766 if (ident == state->i___FILE__) {
4767 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4768 buf.str = xmalloc(buf.len, ident->name);
4769 sprintf(buf.str, "\"%s\"", state->file->basename);
4770 buf.pos = strlen(buf.str);
4771 }
4772 else if (ident == state->i___LINE__) {
4773 buf.len = 30;
4774 buf.str = xmalloc(buf.len, ident->name);
4775 sprintf(buf.str, "%d", state->file->line);
4776 buf.pos = strlen(buf.str);
4777 }
4778 else {
4779 expand_macro(state, macro, &buf, argv, tk);
4780 }
4781 /* Tag the macro name with a $ so it will no longer
4782 * be regonized as a canidate for macro expansion.
4783 */
4784 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004785
4786#if 0
4787 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4788 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4789#endif
4790
4791 free_macro_args(macro, argv);
4792
Eric Biedermanb138ac82003-04-22 18:44:01 +00004793 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004794 file->prev = *filep;
4795 file->basename = xstrdup(ident->name);
4796 file->dirname = xstrdup("");
4797 file->buf = buf.str;
4798 file->size = buf.pos;
4799 file->pos = file->buf;
4800 file->line = 1;
4801 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004802 file->report_line = 1;
4803 file->report_name = file->basename;
4804 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004805 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004806 file->trigraphs = 0;
4807 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004808 *filep = file;
4809 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004810}
4811
Eric Biederman90089602004-05-28 14:11:54 +00004812static void eat_tokens(struct compile_state *state, int targ_tok)
4813{
4814 if (state->eat_depth > 0) {
4815 internal_error(state, 0, "Already eating...");
4816 }
4817 state->eat_depth = state->if_depth;
4818 state->eat_targ = targ_tok;
4819}
4820static int if_eat(struct compile_state *state)
4821{
4822 return state->eat_depth > 0;
4823}
4824static int if_value(struct compile_state *state)
4825{
4826 int index, offset;
4827 index = state->if_depth / CHAR_BIT;
4828 offset = state->if_depth % CHAR_BIT;
4829 return !!(state->if_bytes[index] & (1 << (offset)));
4830}
Stefan Reinauer14e22772010-04-27 06:56:47 +00004831static void set_if_value(struct compile_state *state, int value)
Eric Biederman90089602004-05-28 14:11:54 +00004832{
4833 int index, offset;
4834 index = state->if_depth / CHAR_BIT;
4835 offset = state->if_depth % CHAR_BIT;
4836
4837 state->if_bytes[index] &= ~(1 << offset);
4838 if (value) {
4839 state->if_bytes[index] |= (1 << offset);
4840 }
4841}
4842static void in_if(struct compile_state *state, const char *name)
4843{
4844 if (state->if_depth <= 0) {
4845 error(state, 0, "%s without #if", name);
4846 }
4847}
4848static void enter_if(struct compile_state *state)
4849{
4850 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004851 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004852 error(state, 0, "#if depth too great");
4853 }
4854}
4855static void reenter_if(struct compile_state *state, const char *name)
4856{
4857 in_if(state, name);
4858 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004859 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004860 state->eat_depth = 0;
4861 state->eat_targ = 0;
4862 }
4863}
4864static void enter_else(struct compile_state *state, const char *name)
4865{
4866 in_if(state, name);
4867 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004868 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004869 state->eat_depth = 0;
4870 state->eat_targ = 0;
4871 }
4872}
4873static void exit_if(struct compile_state *state, const char *name)
4874{
4875 in_if(state, name);
4876 if (state->eat_depth == state->if_depth) {
4877 state->eat_depth = 0;
4878 state->eat_targ = 0;
4879 }
4880 state->if_depth -= 1;
4881}
4882
Eric Biedermancb364952004-11-15 10:46:44 +00004883static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004884{
4885 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004886 int rescan;
4887
Eric Biedermancb364952004-11-15 10:46:44 +00004888 file = state->file;
4889 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004890 do {
4891 rescan = 0;
4892 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004893 /* Exit out of an include directive or macro call */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004894 if ((tk->tok == TOK_EOF) &&
4895 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004896 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004897 state->file = file->prev;
4898 /* file->basename is used keep it */
4899 xfree(file->dirname);
4900 xfree(file->buf);
4901 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004902 file = 0;
4903 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004904 rescan = 1;
4905 }
Eric Biederman41203d92004-11-08 09:31:09 +00004906 } while(rescan);
4907}
4908
Eric Biedermancb364952004-11-15 10:46:44 +00004909static void pp_token(struct compile_state *state, struct token *tk)
4910{
Eric Biedermancb364952004-11-15 10:46:44 +00004911 int rescan;
4912
4913 raw_token(state, tk);
4914 do {
4915 rescan = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004916 if (tk->tok == TOK_SPACE) {
4917 raw_token(state, tk);
4918 rescan = 1;
4919 }
4920 else if (tk->tok == TOK_IDENT) {
4921 if (state->token_base == 0) {
4922 ident_to_keyword(state, tk);
4923 } else {
4924 ident_to_macro(state, tk);
4925 }
4926 }
4927 } while(rescan);
4928}
4929
Eric Biederman41203d92004-11-08 09:31:09 +00004930static void preprocess(struct compile_state *state, struct token *tk);
4931
4932static void token(struct compile_state *state, struct token *tk)
4933{
4934 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004935 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004936 do {
4937 rescan = 0;
4938 /* Process a macro directive */
4939 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004940 /* Only match preprocessor directives at the start of a line */
4941 const char *ptr;
4942 ptr = state->file->line_start;
4943 while((ptr < tk->pos)
4944 && spacep(get_char(state->file, ptr)))
4945 {
4946 ptr = next_char(state->file, ptr, 1);
4947 }
4948 if (ptr == tk->pos) {
4949 preprocess(state, tk);
4950 rescan = 1;
4951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004952 }
Eric Biederman41203d92004-11-08 09:31:09 +00004953 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004954 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004955 rescan = compile_macro(state, &state->file, tk);
4956 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004957 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004958 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004959 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004960 /* Eat tokens disabled by the preprocessor
4961 * (Unless we are parsing a preprocessor directive
Eric Biedermancb364952004-11-15 10:46:44 +00004962 */
Eric Biedermana649a412004-11-09 00:35:39 +00004963 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004964 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004965 rescan = 1;
4966 }
Eric Biedermana649a412004-11-09 00:35:39 +00004967 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004968 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004969 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004970 rescan = 1;
4971 }
Eric Biedermancb364952004-11-15 10:46:44 +00004972 /* Error on unknown tokens */
4973 else if (tk->tok == TOK_UNKNOWN) {
4974 error(state, 0, "unknown token");
4975 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004976 } while(rescan);
4977}
4978
Eric Biederman41203d92004-11-08 09:31:09 +00004979
4980static inline struct token *get_token(struct compile_state *state, int offset)
4981{
4982 int index;
4983 index = state->token_base + offset;
4984 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4985 internal_error(state, 0, "token array to small");
4986 }
4987 return &state->token[index];
4988}
4989
4990static struct token *do_eat_token(struct compile_state *state, int tok)
4991{
4992 struct token *tk;
4993 int i;
4994 check_tok(state, get_token(state, 1), tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004995
Eric Biederman41203d92004-11-08 09:31:09 +00004996 /* Free the old token value */
4997 tk = get_token(state, 0);
4998 if (tk->str_len) {
4999 memset((void *)tk->val.str, -1, tk->str_len);
5000 xfree(tk->val.str);
5001 }
5002 /* Overwrite the old token with newer tokens */
5003 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
5004 state->token[i] = state->token[i + 1];
5005 }
5006 /* Clear the last token */
5007 memset(&state->token[i], 0, sizeof(state->token[i]));
5008 state->token[i].tok = -1;
5009
5010 /* Return the token */
5011 return tk;
5012}
5013
Eric Biedermancb364952004-11-15 10:46:44 +00005014static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005015{
5016 struct token *tk1;
5017 tk1 = get_token(state, 1);
5018 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005019 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005020 }
5021 return tk1->tok;
5022}
5023
Eric Biedermancb364952004-11-15 10:46:44 +00005024static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005025{
Eric Biedermancb364952004-11-15 10:46:44 +00005026 raw_peek(state);
5027 return do_eat_token(state, tok);
5028}
5029
5030static int pp_peek(struct compile_state *state)
5031{
5032 struct token *tk1;
5033 tk1 = get_token(state, 1);
5034 if (tk1->tok == -1) {
5035 pp_token(state, tk1);
5036 }
5037 return tk1->tok;
5038}
5039
5040static struct token *pp_eat(struct compile_state *state, int tok)
5041{
5042 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005043 return do_eat_token(state, tok);
5044}
5045
Eric Biedermanb138ac82003-04-22 18:44:01 +00005046static int peek(struct compile_state *state)
5047{
Eric Biederman41203d92004-11-08 09:31:09 +00005048 struct token *tk1;
5049 tk1 = get_token(state, 1);
5050 if (tk1->tok == -1) {
5051 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005052 }
Eric Biederman41203d92004-11-08 09:31:09 +00005053 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005054}
5055
5056static int peek2(struct compile_state *state)
5057{
Eric Biederman41203d92004-11-08 09:31:09 +00005058 struct token *tk1, *tk2;
5059 tk1 = get_token(state, 1);
5060 tk2 = get_token(state, 2);
5061 if (tk1->tok == -1) {
5062 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005063 }
Eric Biederman41203d92004-11-08 09:31:09 +00005064 if (tk2->tok == -1) {
5065 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005066 }
Eric Biederman41203d92004-11-08 09:31:09 +00005067 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005068}
5069
Eric Biederman41203d92004-11-08 09:31:09 +00005070static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005071{
Eric Biederman90089602004-05-28 14:11:54 +00005072 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005073 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005074}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005075
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005076static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005077{
Eric Biederman90089602004-05-28 14:11:54 +00005078 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005079 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005080 int subdir_len;
5081 struct file_state *file;
5082 char *basename;
5083 file = xmalloc(sizeof(*file), "file_state");
5084
5085 base = strrchr(filename, '/');
5086 subdir = filename;
5087 if (base != 0) {
5088 subdir_len = base - filename;
5089 base++;
5090 }
5091 else {
5092 base = filename;
5093 subdir_len = 0;
5094 }
5095 basename = xmalloc(strlen(base) +1, "basename");
5096 strcpy(basename, base);
5097 file->basename = basename;
5098
5099 if (getcwd(cwd, sizeof(cwd)) == 0) {
5100 die("cwd buffer to small");
5101 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005102 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005103 file->dirname = xmalloc(subdir_len + 1, "dirname");
5104 memcpy(file->dirname, subdir, subdir_len);
5105 file->dirname[subdir_len] = '\0';
5106 }
5107 else {
Eric Biederman90089602004-05-28 14:11:54 +00005108 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005109 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005110 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005111 /* Find the appropriate directory... */
5112 dir = 0;
5113 if (!state->file && exists(cwd, filename)) {
5114 dir = cwd;
5115 }
5116 if (local && state->file && exists(state->file->dirname, filename)) {
5117 dir = state->file->dirname;
5118 }
Eric Biederman90089602004-05-28 14:11:54 +00005119 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005120 if (exists(*path, filename)) {
5121 dir = *path;
5122 }
5123 }
5124 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005125 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005126 }
5127 dirlen = strlen(dir);
5128 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5129 memcpy(file->dirname, dir, dirlen);
5130 file->dirname[dirlen] = '/';
5131 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5132 file->dirname[dirlen + 1 + subdir_len] = '\0';
5133 }
5134 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005135
5136 file->pos = file->buf;
5137 file->line_start = file->pos;
5138 file->line = 1;
5139
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005140 file->report_line = 1;
5141 file->report_name = file->basename;
5142 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005143 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005144 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5145 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005146
Eric Biedermanb138ac82003-04-22 18:44:01 +00005147 file->prev = state->file;
5148 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005149}
5150
Eric Biederman41203d92004-11-08 09:31:09 +00005151static struct triple *constant_expr(struct compile_state *state);
5152static void integral(struct compile_state *state, struct triple *def);
5153
5154static int mcexpr(struct compile_state *state)
5155{
5156 struct triple *cvalue;
5157 cvalue = constant_expr(state);
5158 integral(state, cvalue);
5159 if (cvalue->op != OP_INTCONST) {
5160 error(state, 0, "integer constant expected");
5161 }
5162 return cvalue->u.cval != 0;
5163}
5164
5165static void preprocess(struct compile_state *state, struct token *current_token)
5166{
5167 /* Doing much more with the preprocessor would require
5168 * a parser and a major restructuring.
5169 * Postpone that for later.
5170 */
Eric Biederman41203d92004-11-08 09:31:09 +00005171 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005172 int tok;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005173
Eric Biedermancb364952004-11-15 10:46:44 +00005174 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005175
5176 old_token_base = state->token_base;
5177 state->token_base = current_token - state->token;
5178
Eric Biedermancb364952004-11-15 10:46:44 +00005179 tok = pp_peek(state);
5180 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005181 case TOK_LIT_INT:
5182 {
5183 struct token *tk;
5184 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005185 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005186 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005187 /* I have a preprocessor line marker parse it */
5188 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005189 const char *token, *base;
5190 char *name, *dir;
5191 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005192 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005193 name = xmalloc(tk->str_len, "report_name");
5194 token = tk->val.str + 1;
5195 base = strrchr(token, '/');
5196 name_len = tk->str_len -2;
5197 if (base != 0) {
5198 dir_len = base - token;
5199 base++;
5200 name_len -= base - token;
5201 } else {
5202 dir_len = 0;
5203 base = token;
5204 }
5205 memcpy(name, base, name_len);
5206 name[name_len] = '\0';
5207 dir = xmalloc(dir_len + 1, "report_dir");
5208 memcpy(dir, token, dir_len);
5209 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005210 state->file->report_line = override_line - 1;
5211 state->file->report_name = name;
5212 state->file->report_dir = dir;
5213 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005214 }
5215 break;
5216 }
5217 case TOK_MLINE:
5218 {
5219 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005220 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005221 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005222 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5223 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005224 const char *token, *base;
5225 char *name, *dir;
5226 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005227 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005228 name = xmalloc(tk->str_len, "report_name");
5229 token = tk->val.str + 1;
5230 base = strrchr(token, '/');
5231 name_len = tk->str_len - 2;
5232 if (base != 0) {
5233 dir_len = base - token;
5234 base++;
5235 name_len -= base - token;
5236 } else {
5237 dir_len = 0;
5238 base = token;
5239 }
5240 memcpy(name, base, name_len);
5241 name[name_len] = '\0';
5242 dir = xmalloc(dir_len + 1, "report_dir");
5243 memcpy(dir, token, dir_len);
5244 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005245 state->file->report_name = name;
5246 state->file->report_dir = dir;
5247 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005248 }
5249 break;
5250 }
5251 case TOK_MUNDEF:
5252 {
5253 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005254 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005255 if (if_eat(state)) /* quit early when #if'd out */
5256 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005257
Eric Biedermancb364952004-11-15 10:46:44 +00005258 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005259
5260 undef_macro(state, ident);
5261 break;
5262 }
5263 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005264 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005265 if (if_eat(state)) /* quit early when #if'd out */
5266 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005267 warning(state, 0, "Ignoring pragma");
Eric Biederman41203d92004-11-08 09:31:09 +00005268 break;
5269 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005270 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005271 reenter_if(state, "#elif");
5272 if (if_eat(state)) /* quit early when #if'd out */
5273 break;
5274 /* If the #if was taken the #elif just disables the following code */
5275 if (if_value(state)) {
5276 eat_tokens(state, TOK_MENDIF);
5277 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005278 /* If the previous #if was not taken see if the #elif enables the
Eric Biederman41203d92004-11-08 09:31:09 +00005279 * trailing code.
5280 */
5281 else {
5282 set_if_value(state, mcexpr(state));
5283 if (!if_value(state)) {
5284 eat_tokens(state, TOK_MELSE);
5285 }
5286 }
5287 break;
5288 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005289 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005290 enter_if(state);
5291 if (if_eat(state)) /* quit early when #if'd out */
5292 break;
5293 set_if_value(state, mcexpr(state));
5294 if (!if_value(state)) {
5295 eat_tokens(state, TOK_MELSE);
5296 }
5297 break;
5298 case TOK_MIFNDEF:
5299 {
5300 struct hash_entry *ident;
5301
Eric Biedermancb364952004-11-15 10:46:44 +00005302 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005303 enter_if(state);
5304 if (if_eat(state)) /* quit early when #if'd out */
5305 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005306 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005307 set_if_value(state, ident->sym_define == 0);
5308 if (!if_value(state)) {
5309 eat_tokens(state, TOK_MELSE);
5310 }
5311 break;
5312 }
5313 case TOK_MIFDEF:
5314 {
5315 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005316 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005317 enter_if(state);
5318 if (if_eat(state)) /* quit early when #if'd out */
5319 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005320 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005321 set_if_value(state, ident->sym_define != 0);
5322 if (!if_value(state)) {
5323 eat_tokens(state, TOK_MELSE);
5324 }
5325 break;
5326 }
5327 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005328 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005329 enter_else(state, "#else");
5330 if (!if_eat(state) && if_value(state)) {
5331 eat_tokens(state, TOK_MENDIF);
5332 }
5333 break;
5334 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005335 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005336 exit_if(state, "#endif");
5337 break;
5338 case TOK_MDEFINE:
5339 {
5340 struct hash_entry *ident;
5341 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005342 const char *mstart, *mend;
5343 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005344
Eric Biedermancb364952004-11-15 10:46:44 +00005345 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005346 if (if_eat(state)) /* quit early when #if'd out */
5347 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005348 ident = pp_eat(state, TOK_MIDENT)->ident;
5349 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005350 args = 0;
5351 larg = &args;
5352
Eric Biederman41203d92004-11-08 09:31:09 +00005353 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005354 if (raw_peek(state) == TOK_LPAREN) {
5355 raw_eat(state, TOK_LPAREN);
5356 argc += 1;
5357
Eric Biederman41203d92004-11-08 09:31:09 +00005358 for(;;) {
5359 struct macro_arg *narg, *arg;
5360 struct hash_entry *aident;
5361 int tok;
5362
Eric Biedermancb364952004-11-15 10:46:44 +00005363 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005364 if (!args && (tok == TOK_RPAREN)) {
5365 break;
5366 }
5367 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005368 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005369 aident = state->i___VA_ARGS__;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005370 }
Eric Biederman41203d92004-11-08 09:31:09 +00005371 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005372 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005373 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005374
Eric Biederman41203d92004-11-08 09:31:09 +00005375 narg = xcmalloc(sizeof(*arg), "macro arg");
5376 narg->ident = aident;
5377
5378 /* Verify I don't have a duplicate identifier */
5379 for(arg = args; arg; arg = arg->next) {
5380 if (arg->ident == narg->ident) {
5381 error(state, 0, "Duplicate macro arg `%s'",
5382 narg->ident->name);
5383 }
5384 }
5385 /* Add the new argument to the end of the list */
5386 *larg = narg;
5387 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005388 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005389
5390 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005391 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005392 break;
5393 }
Eric Biedermancb364952004-11-15 10:46:44 +00005394 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005395 }
Eric Biedermancb364952004-11-15 10:46:44 +00005396 pp_eat(state, TOK_RPAREN);
5397 }
5398 /* Remove leading whitespace */
5399 while(raw_peek(state) == TOK_SPACE) {
5400 raw_eat(state, TOK_SPACE);
5401 }
Eric Biederman41203d92004-11-08 09:31:09 +00005402
Eric Biedermancb364952004-11-15 10:46:44 +00005403 /* Remember the start of the macro body */
5404 tok = raw_peek(state);
5405 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005406
Eric Biedermancb364952004-11-15 10:46:44 +00005407 /* Find the end of the macro */
5408 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5409 raw_eat(state, tok);
5410 /* Remember the end of the last non space token */
5411 raw_peek(state);
5412 if (tok != TOK_SPACE) {
5413 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005414 }
5415 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005416
Eric Biedermancb364952004-11-15 10:46:44 +00005417 /* Now that I have found the body defined the token */
5418 do_define_macro(state, ident,
5419 char_strdup(state->file, mstart, mend, "macro buf"),
5420 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005421 break;
5422 }
5423 case TOK_MERROR:
5424 {
Eric Biedermancb364952004-11-15 10:46:44 +00005425 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005426 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005427
Eric Biedermancb364952004-11-15 10:46:44 +00005428 pp_eat(state, TOK_MERROR);
5429 /* Find the start of the line */
5430 raw_peek(state);
5431 start = get_token(state, 1)->pos;
5432
Eric Biederman41203d92004-11-08 09:31:09 +00005433 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005434 while((tok = raw_peek(state)) != TOK_EOL) {
5435 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005436 }
Eric Biedermancb364952004-11-15 10:46:44 +00005437 end = get_token(state, 1)->pos;
5438 len = end - start;
5439 if (!if_eat(state)) {
5440 error(state, 0, "%*.*s", len, len, start);
5441 }
Eric Biederman41203d92004-11-08 09:31:09 +00005442 break;
5443 }
5444 case TOK_MWARNING:
5445 {
Eric Biedermancb364952004-11-15 10:46:44 +00005446 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005447 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005448
Eric Biedermancb364952004-11-15 10:46:44 +00005449 pp_eat(state, TOK_MWARNING);
5450
5451 /* Find the start of the line */
5452 raw_peek(state);
5453 start = get_token(state, 1)->pos;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005454
Eric Biederman41203d92004-11-08 09:31:09 +00005455 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005456 while((tok = raw_peek(state)) != TOK_EOL) {
5457 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005458 }
Eric Biedermancb364952004-11-15 10:46:44 +00005459 end = get_token(state, 1)->pos;
5460 len = end - start;
5461 if (!if_eat(state)) {
5462 warning(state, 0, "%*.*s", len, len, start);
5463 }
Eric Biederman41203d92004-11-08 09:31:09 +00005464 break;
5465 }
5466 case TOK_MINCLUDE:
5467 {
5468 char *name;
5469 int local;
5470 local = 0;
5471 name = 0;
5472
Eric Biedermancb364952004-11-15 10:46:44 +00005473 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005474 if (if_eat(state)) {
5475 /* Find the end of the line */
5476 while((tok = raw_peek(state)) != TOK_EOL) {
5477 raw_eat(state, tok);
5478 }
5479 break;
5480 }
Eric Biederman41203d92004-11-08 09:31:09 +00005481 tok = peek(state);
5482 if (tok == TOK_LIT_STRING) {
5483 struct token *tk;
5484 const char *token;
5485 int name_len;
5486 tk = eat(state, TOK_LIT_STRING);
5487 name = xmalloc(tk->str_len, "include");
5488 token = tk->val.str +1;
5489 name_len = tk->str_len -2;
5490 if (*token == '"') {
5491 token++;
5492 name_len--;
5493 }
5494 memcpy(name, token, name_len);
5495 name[name_len] = '\0';
5496 local = 1;
5497 }
5498 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005499 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005500 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005501
5502 buf.len = 40;
5503 buf.str = xmalloc(buf.len, "include");
5504 buf.pos = 0;
5505
5506 tok = peek(state);
5507 while((tok != TOK_MORE) &&
5508 (tok != TOK_EOL) && (tok != TOK_EOF))
5509 {
5510 struct token *tk;
5511 tk = eat(state, tok);
5512 append_macro_chars(state, "include", &buf,
5513 state->file, tk->pos, state->file->pos);
5514 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005515 }
Eric Biedermancb364952004-11-15 10:46:44 +00005516 append_macro_text(state, "include", &buf, "\0", 1);
5517 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005518 error(state, 0, "Unterminated include directive");
5519 }
Eric Biederman41203d92004-11-08 09:31:09 +00005520 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005521 local = 0;
5522 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005523 }
5524 else {
5525 error(state, 0, "Invalid include directive");
5526 }
5527 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005528 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005529 error(state, 0, "garbage after include directive");
5530 }
5531 if (!if_eat(state)) {
5532 compile_file(state, name, local);
5533 }
5534 xfree(name);
5535 break;
5536 }
5537 case TOK_EOL:
5538 /* Ignore # without a follwing ident */
5539 break;
5540 default:
5541 {
5542 const char *name1, *name2;
5543 name1 = tokens[tok];
5544 name2 = "";
5545 if (tok == TOK_MIDENT) {
5546 name2 = get_token(state, 1)->ident->name;
5547 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005548 error(state, 0, "Invalid preprocessor directive: %s %s",
Eric Biederman41203d92004-11-08 09:31:09 +00005549 name1, name2);
5550 break;
5551 }
5552 }
5553 /* Consume the rest of the macro line */
5554 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005555 tok = pp_peek(state);
5556 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005557 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5558 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005559 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005560 return;
5561}
5562
Eric Biederman0babc1c2003-05-09 02:39:00 +00005563/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005564
5565static struct type *new_type(
5566 unsigned int type, struct type *left, struct type *right)
5567{
5568 struct type *result;
5569 result = xmalloc(sizeof(*result), "type");
5570 result->type = type;
5571 result->left = left;
5572 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005573 result->field_ident = 0;
5574 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005575 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005576 return result;
5577}
5578
5579static struct type *clone_type(unsigned int specifiers, struct type *old)
5580{
5581 struct type *result;
5582 result = xmalloc(sizeof(*result), "type");
5583 memcpy(result, old, sizeof(*result));
5584 result->type &= TYPE_MASK;
5585 result->type |= specifiers;
5586 return result;
5587}
5588
Eric Biederman90089602004-05-28 14:11:54 +00005589static struct type *dup_type(struct compile_state *state, struct type *orig)
5590{
5591 struct type *new;
5592 new = xcmalloc(sizeof(*new), "type");
5593 new->type = orig->type;
5594 new->field_ident = orig->field_ident;
5595 new->type_ident = orig->type_ident;
5596 new->elements = orig->elements;
5597 if (orig->left) {
5598 new->left = dup_type(state, orig->left);
5599 }
5600 if (orig->right) {
5601 new->right = dup_type(state, orig->right);
5602 }
5603 return new;
5604}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005605
Eric Biederman90089602004-05-28 14:11:54 +00005606
5607static struct type *invalid_type(struct compile_state *state, struct type *type)
5608{
5609 struct type *invalid, *member;
5610 invalid = 0;
5611 if (!type) {
5612 internal_error(state, 0, "type missing?");
5613 }
5614 switch(type->type & TYPE_MASK) {
5615 case TYPE_VOID:
5616 case TYPE_CHAR: case TYPE_UCHAR:
5617 case TYPE_SHORT: case TYPE_USHORT:
5618 case TYPE_INT: case TYPE_UINT:
5619 case TYPE_LONG: case TYPE_ULONG:
5620 case TYPE_LLONG: case TYPE_ULLONG:
5621 case TYPE_POINTER:
5622 case TYPE_ENUM:
5623 break;
5624 case TYPE_BITFIELD:
5625 invalid = invalid_type(state, type->left);
5626 break;
5627 case TYPE_ARRAY:
5628 invalid = invalid_type(state, type->left);
5629 break;
5630 case TYPE_STRUCT:
5631 case TYPE_TUPLE:
5632 member = type->left;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005633 while(member && (invalid == 0) &&
Eric Biederman90089602004-05-28 14:11:54 +00005634 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5635 invalid = invalid_type(state, member->left);
5636 member = member->right;
5637 }
5638 if (!invalid) {
5639 invalid = invalid_type(state, member);
5640 }
5641 break;
5642 case TYPE_UNION:
5643 case TYPE_JOIN:
5644 member = type->left;
5645 while(member && (invalid == 0) &&
5646 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5647 invalid = invalid_type(state, member->left);
5648 member = member->right;
5649 }
5650 if (!invalid) {
5651 invalid = invalid_type(state, member);
5652 }
5653 break;
5654 default:
5655 invalid = type;
5656 break;
5657 }
5658 return invalid;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005659
Eric Biederman90089602004-05-28 14:11:54 +00005660}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005661
Eric Biederman90089602004-05-28 14:11:54 +00005662static struct type void_type = { .type = TYPE_VOID };
5663static struct type char_type = { .type = TYPE_CHAR };
5664static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005665#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005666static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005667#endif
Eric Biederman90089602004-05-28 14:11:54 +00005668static struct type ushort_type = { .type = TYPE_USHORT };
5669static struct type int_type = { .type = TYPE_INT };
5670static struct type uint_type = { .type = TYPE_UINT };
5671static struct type long_type = { .type = TYPE_LONG };
5672static struct type ulong_type = { .type = TYPE_ULONG };
5673static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005674
Eric Biederman5ade04a2003-10-22 04:03:46 +00005675static struct type void_ptr_type = {
5676 .type = TYPE_POINTER,
5677 .left = &void_type,
5678};
5679
Stefan Reinauer50542a82007-10-24 11:14:14 +00005680#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005681static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005682 .type = TYPE_FUNCTION,
5683 .left = &void_type,
5684 .right = &void_type,
5685};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005686#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005687
Eric Biederman90089602004-05-28 14:11:54 +00005688static size_t bits_to_bytes(size_t size)
5689{
5690 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5691}
5692
Eric Biedermanb138ac82003-04-22 18:44:01 +00005693static struct triple *variable(struct compile_state *state, struct type *type)
5694{
5695 struct triple *result;
5696 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005697 result = triple(state, OP_ADECL, type, 0, 0);
5698 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005699 }
5700 else {
5701 result = triple(state, OP_SDECL, type, 0, 0);
5702 }
5703 return result;
5704}
5705
5706static void stor_of(FILE *fp, struct type *type)
5707{
5708 switch(type->type & STOR_MASK) {
5709 case STOR_AUTO:
5710 fprintf(fp, "auto ");
5711 break;
5712 case STOR_STATIC:
5713 fprintf(fp, "static ");
5714 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005715 case STOR_LOCAL:
5716 fprintf(fp, "local ");
5717 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005718 case STOR_EXTERN:
5719 fprintf(fp, "extern ");
5720 break;
5721 case STOR_REGISTER:
5722 fprintf(fp, "register ");
5723 break;
5724 case STOR_TYPEDEF:
5725 fprintf(fp, "typedef ");
5726 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005727 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005728 fprintf(fp, "inline ");
5729 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005730 case STOR_INLINE | STOR_STATIC:
5731 fprintf(fp, "static inline");
5732 break;
5733 case STOR_INLINE | STOR_EXTERN:
5734 fprintf(fp, "extern inline");
5735 break;
5736 default:
5737 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5738 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005739 }
5740}
5741static void qual_of(FILE *fp, struct type *type)
5742{
5743 if (type->type & QUAL_CONST) {
5744 fprintf(fp, " const");
5745 }
5746 if (type->type & QUAL_VOLATILE) {
5747 fprintf(fp, " volatile");
5748 }
5749 if (type->type & QUAL_RESTRICT) {
5750 fprintf(fp, " restrict");
5751 }
5752}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005753
Eric Biedermanb138ac82003-04-22 18:44:01 +00005754static void name_of(FILE *fp, struct type *type)
5755{
Eric Biederman90089602004-05-28 14:11:54 +00005756 unsigned int base_type;
5757 base_type = type->type & TYPE_MASK;
5758 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5759 stor_of(fp, type);
5760 }
5761 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005762 case TYPE_VOID:
5763 fprintf(fp, "void");
5764 qual_of(fp, type);
5765 break;
5766 case TYPE_CHAR:
5767 fprintf(fp, "signed char");
5768 qual_of(fp, type);
5769 break;
5770 case TYPE_UCHAR:
5771 fprintf(fp, "unsigned char");
5772 qual_of(fp, type);
5773 break;
5774 case TYPE_SHORT:
5775 fprintf(fp, "signed short");
5776 qual_of(fp, type);
5777 break;
5778 case TYPE_USHORT:
5779 fprintf(fp, "unsigned short");
5780 qual_of(fp, type);
5781 break;
5782 case TYPE_INT:
5783 fprintf(fp, "signed int");
5784 qual_of(fp, type);
5785 break;
5786 case TYPE_UINT:
5787 fprintf(fp, "unsigned int");
5788 qual_of(fp, type);
5789 break;
5790 case TYPE_LONG:
5791 fprintf(fp, "signed long");
5792 qual_of(fp, type);
5793 break;
5794 case TYPE_ULONG:
5795 fprintf(fp, "unsigned long");
5796 qual_of(fp, type);
5797 break;
5798 case TYPE_POINTER:
5799 name_of(fp, type->left);
5800 fprintf(fp, " * ");
5801 qual_of(fp, type);
5802 break;
5803 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005804 name_of(fp, type->left);
5805 fprintf(fp, ", ");
5806 name_of(fp, type->right);
5807 break;
Eric Biederman90089602004-05-28 14:11:54 +00005808 case TYPE_OVERLAP:
5809 name_of(fp, type->left);
5810 fprintf(fp, ",| ");
5811 name_of(fp, type->right);
5812 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005813 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005814 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005815 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005816 qual_of(fp, type);
5817 break;
5818 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005819 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005820 (type->type_ident)? type->type_ident->name : "");
5821 name_of(fp, type->left);
5822 fprintf(fp, " } ");
5823 qual_of(fp, type);
5824 break;
5825 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005826 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005827 (type->type_ident)? type->type_ident->name : "");
5828 name_of(fp, type->left);
5829 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005830 qual_of(fp, type);
5831 break;
5832 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005833 name_of(fp, type->left);
5834 fprintf(fp, " (*)(");
5835 name_of(fp, type->right);
5836 fprintf(fp, ")");
5837 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 case TYPE_ARRAY:
5839 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005840 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005841 break;
Eric Biederman90089602004-05-28 14:11:54 +00005842 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005843 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005844 name_of(fp, type->left);
5845 fprintf(fp, " } ");
5846 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005847 break;
Eric Biederman90089602004-05-28 14:11:54 +00005848 case TYPE_JOIN:
5849 fprintf(fp, "join { ");
5850 name_of(fp, type->left);
5851 fprintf(fp, " } ");
5852 qual_of(fp, type);
5853 break;
5854 case TYPE_BITFIELD:
5855 name_of(fp, type->left);
5856 fprintf(fp, " : %d ", type->elements);
5857 qual_of(fp, type);
5858 break;
5859 case TYPE_UNKNOWN:
5860 fprintf(fp, "unknown_t");
5861 break;
5862 default:
5863 fprintf(fp, "????: %x", base_type);
5864 break;
5865 }
5866 if (type->field_ident && type->field_ident->name) {
5867 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005868 }
5869}
5870
5871static size_t align_of(struct compile_state *state, struct type *type)
5872{
5873 size_t align;
5874 align = 0;
5875 switch(type->type & TYPE_MASK) {
5876 case TYPE_VOID:
5877 align = 1;
5878 break;
Eric Biederman90089602004-05-28 14:11:54 +00005879 case TYPE_BITFIELD:
5880 align = 1;
5881 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005882 case TYPE_CHAR:
5883 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005884 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005885 break;
5886 case TYPE_SHORT:
5887 case TYPE_USHORT:
5888 align = ALIGNOF_SHORT;
5889 break;
5890 case TYPE_INT:
5891 case TYPE_UINT:
5892 case TYPE_ENUM:
5893 align = ALIGNOF_INT;
5894 break;
5895 case TYPE_LONG:
5896 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005897 align = ALIGNOF_LONG;
5898 break;
Eric Biederman90089602004-05-28 14:11:54 +00005899 case TYPE_POINTER:
5900 align = ALIGNOF_POINTER;
5901 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005902 case TYPE_PRODUCT:
5903 case TYPE_OVERLAP:
5904 {
5905 size_t left_align, right_align;
5906 left_align = align_of(state, type->left);
5907 right_align = align_of(state, type->right);
5908 align = (left_align >= right_align) ? left_align : right_align;
5909 break;
5910 }
5911 case TYPE_ARRAY:
5912 align = align_of(state, type->left);
5913 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005914 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005915 case TYPE_TUPLE:
5916 case TYPE_UNION:
5917 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005918 align = align_of(state, type->left);
5919 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005920 default:
5921 error(state, 0, "alignof not yet defined for type\n");
5922 break;
5923 }
5924 return align;
5925}
5926
Eric Biederman90089602004-05-28 14:11:54 +00005927static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005928{
Eric Biederman90089602004-05-28 14:11:54 +00005929 size_t align;
5930 align = 0;
5931 switch(type->type & TYPE_MASK) {
5932 case TYPE_VOID:
5933 align = 1;
5934 break;
5935 case TYPE_BITFIELD:
5936 align = 1;
5937 break;
5938 case TYPE_CHAR:
5939 case TYPE_UCHAR:
5940 align = REG_ALIGNOF_CHAR;
5941 break;
5942 case TYPE_SHORT:
5943 case TYPE_USHORT:
5944 align = REG_ALIGNOF_SHORT;
5945 break;
5946 case TYPE_INT:
5947 case TYPE_UINT:
5948 case TYPE_ENUM:
5949 align = REG_ALIGNOF_INT;
5950 break;
5951 case TYPE_LONG:
5952 case TYPE_ULONG:
5953 align = REG_ALIGNOF_LONG;
5954 break;
5955 case TYPE_POINTER:
5956 align = REG_ALIGNOF_POINTER;
5957 break;
5958 case TYPE_PRODUCT:
5959 case TYPE_OVERLAP:
5960 {
5961 size_t left_align, right_align;
5962 left_align = reg_align_of(state, type->left);
5963 right_align = reg_align_of(state, type->right);
5964 align = (left_align >= right_align) ? left_align : right_align;
5965 break;
5966 }
5967 case TYPE_ARRAY:
5968 align = reg_align_of(state, type->left);
5969 break;
5970 case TYPE_STRUCT:
5971 case TYPE_UNION:
5972 case TYPE_TUPLE:
5973 case TYPE_JOIN:
5974 align = reg_align_of(state, type->left);
5975 break;
5976 default:
5977 error(state, 0, "alignof not yet defined for type\n");
5978 break;
5979 }
5980 return align;
5981}
5982
5983static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5984{
5985 return bits_to_bytes(align_of(state, type));
5986}
5987static size_t size_of(struct compile_state *state, struct type *type);
5988static size_t reg_size_of(struct compile_state *state, struct type *type);
5989
Stefan Reinauer14e22772010-04-27 06:56:47 +00005990static size_t needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00005991 struct type *type, size_t offset)
5992{
5993 size_t padding, align;
5994 align = align_of(state, type);
5995 /* Align to the next machine word if the bitfield does completely
5996 * fit into the current word.
5997 */
5998 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5999 size_t size;
6000 size = size_of(state, type);
6001 if ((offset + type->elements)/size != offset/size) {
6002 align = size;
6003 }
6004 }
Eric Biederman03b59862003-06-24 14:27:37 +00006005 padding = 0;
6006 if (offset % align) {
6007 padding = align - (offset % align);
6008 }
6009 return padding;
6010}
Eric Biederman90089602004-05-28 14:11:54 +00006011
Stefan Reinauer14e22772010-04-27 06:56:47 +00006012static size_t reg_needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006013 struct type *type, size_t offset)
6014{
6015 size_t padding, align;
6016 align = reg_align_of(state, type);
6017 /* Align to the next register word if the bitfield does completely
6018 * fit into the current register.
6019 */
6020 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00006021 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
Eric Biederman90089602004-05-28 14:11:54 +00006022 {
6023 align = REG_SIZEOF_REG;
6024 }
6025 padding = 0;
6026 if (offset % align) {
6027 padding = align - (offset % align);
6028 }
6029 return padding;
6030}
6031
Eric Biedermanb138ac82003-04-22 18:44:01 +00006032static size_t size_of(struct compile_state *state, struct type *type)
6033{
6034 size_t size;
6035 size = 0;
6036 switch(type->type & TYPE_MASK) {
6037 case TYPE_VOID:
6038 size = 0;
6039 break;
Eric Biederman90089602004-05-28 14:11:54 +00006040 case TYPE_BITFIELD:
6041 size = type->elements;
6042 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006043 case TYPE_CHAR:
6044 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006045 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006046 break;
6047 case TYPE_SHORT:
6048 case TYPE_USHORT:
6049 size = SIZEOF_SHORT;
6050 break;
6051 case TYPE_INT:
6052 case TYPE_UINT:
6053 case TYPE_ENUM:
6054 size = SIZEOF_INT;
6055 break;
6056 case TYPE_LONG:
6057 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006058 size = SIZEOF_LONG;
6059 break;
Eric Biederman90089602004-05-28 14:11:54 +00006060 case TYPE_POINTER:
6061 size = SIZEOF_POINTER;
6062 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006063 case TYPE_PRODUCT:
6064 {
Eric Biederman90089602004-05-28 14:11:54 +00006065 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006066 size = 0;
6067 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006068 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006069 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006070 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006071 }
Eric Biederman90089602004-05-28 14:11:54 +00006072 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006073 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006074 break;
6075 }
6076 case TYPE_OVERLAP:
6077 {
6078 size_t size_left, size_right;
6079 size_left = size_of(state, type->left);
6080 size_right = size_of(state, type->right);
6081 size = (size_left >= size_right)? size_left : size_right;
6082 break;
6083 }
6084 case TYPE_ARRAY:
6085 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6086 internal_error(state, 0, "Invalid array type");
6087 } else {
6088 size = size_of(state, type->left) * type->elements;
6089 }
6090 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006091 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006092 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006093 {
Eric Biederman90089602004-05-28 14:11:54 +00006094 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006095 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006096 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006097 pad = needed_padding(state, type, size);
6098 size = size + pad;
6099 break;
6100 }
6101 case TYPE_UNION:
6102 case TYPE_JOIN:
6103 {
6104 size_t pad;
6105 size = size_of(state, type->left);
6106 /* Pad unions so their size is a multiple of their alignment */
6107 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006108 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006109 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006111 default:
Eric Biederman90089602004-05-28 14:11:54 +00006112 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006113 break;
6114 }
6115 return size;
6116}
6117
Eric Biederman90089602004-05-28 14:11:54 +00006118static size_t reg_size_of(struct compile_state *state, struct type *type)
6119{
6120 size_t size;
6121 size = 0;
6122 switch(type->type & TYPE_MASK) {
6123 case TYPE_VOID:
6124 size = 0;
6125 break;
6126 case TYPE_BITFIELD:
6127 size = type->elements;
6128 break;
6129 case TYPE_CHAR:
6130 case TYPE_UCHAR:
6131 size = REG_SIZEOF_CHAR;
6132 break;
6133 case TYPE_SHORT:
6134 case TYPE_USHORT:
6135 size = REG_SIZEOF_SHORT;
6136 break;
6137 case TYPE_INT:
6138 case TYPE_UINT:
6139 case TYPE_ENUM:
6140 size = REG_SIZEOF_INT;
6141 break;
6142 case TYPE_LONG:
6143 case TYPE_ULONG:
6144 size = REG_SIZEOF_LONG;
6145 break;
6146 case TYPE_POINTER:
6147 size = REG_SIZEOF_POINTER;
6148 break;
6149 case TYPE_PRODUCT:
6150 {
6151 size_t pad;
6152 size = 0;
6153 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6154 pad = reg_needed_padding(state, type->left, size);
6155 size = size + pad + reg_size_of(state, type->left);
6156 type = type->right;
6157 }
6158 pad = reg_needed_padding(state, type, size);
6159 size = size + pad + reg_size_of(state, type);
6160 break;
6161 }
6162 case TYPE_OVERLAP:
6163 {
6164 size_t size_left, size_right;
6165 size_left = reg_size_of(state, type->left);
6166 size_right = reg_size_of(state, type->right);
6167 size = (size_left >= size_right)? size_left : size_right;
6168 break;
6169 }
6170 case TYPE_ARRAY:
6171 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6172 internal_error(state, 0, "Invalid array type");
6173 } else {
6174 size = reg_size_of(state, type->left) * type->elements;
6175 }
6176 break;
6177 case TYPE_STRUCT:
6178 case TYPE_TUPLE:
6179 {
6180 size_t pad;
6181 size = reg_size_of(state, type->left);
6182 /* Pad structures so their size is a multiples of their alignment */
6183 pad = reg_needed_padding(state, type, size);
6184 size = size + pad;
6185 break;
6186 }
6187 case TYPE_UNION:
6188 case TYPE_JOIN:
6189 {
6190 size_t pad;
6191 size = reg_size_of(state, type->left);
6192 /* Pad unions so their size is a multiple of their alignment */
6193 pad = reg_needed_padding(state, type, size);
6194 size = size + pad;
6195 break;
6196 }
6197 default:
6198 internal_error(state, 0, "sizeof not yet defined for type");
6199 break;
6200 }
6201 return size;
6202}
6203
6204static size_t registers_of(struct compile_state *state, struct type *type)
6205{
6206 size_t registers;
6207 registers = reg_size_of(state, type);
6208 registers += REG_SIZEOF_REG - 1;
6209 registers /= REG_SIZEOF_REG;
6210 return registers;
6211}
6212
6213static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6214{
6215 return bits_to_bytes(size_of(state, type));
6216}
6217
Stefan Reinauer14e22772010-04-27 06:56:47 +00006218static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006219 struct type *type, struct hash_entry *field)
6220{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006221 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006222 size_t size;
6223
Eric Biederman0babc1c2003-05-09 02:39:00 +00006224 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006225 member = 0;
6226 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6227 member = type->left;
6228 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6229 size += needed_padding(state, member->left, size);
6230 if (member->left->field_ident == field) {
6231 member = member->left;
6232 break;
6233 }
6234 size += size_of(state, member->left);
6235 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006236 }
Eric Biederman90089602004-05-28 14:11:54 +00006237 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006238 }
Eric Biederman90089602004-05-28 14:11:54 +00006239 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6240 member = type->left;
6241 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6242 if (member->left->field_ident == field) {
6243 member = member->left;
6244 break;
6245 }
6246 member = member->right;
6247 }
6248 }
6249 else {
6250 internal_error(state, 0, "field_offset only works on structures and unions");
6251 }
6252
6253 if (!member || (member->field_ident != field)) {
6254 error(state, 0, "member %s not present", field->name);
6255 }
6256 return size;
6257}
6258
Stefan Reinauer14e22772010-04-27 06:56:47 +00006259static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006260 struct type *type, struct hash_entry *field)
6261{
6262 struct type *member;
6263 size_t size;
6264
6265 size = 0;
6266 member = 0;
6267 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6268 member = type->left;
6269 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6270 size += reg_needed_padding(state, member->left, size);
6271 if (member->left->field_ident == field) {
6272 member = member->left;
6273 break;
6274 }
6275 size += reg_size_of(state, member->left);
6276 member = member->right;
6277 }
6278 }
6279 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6280 member = type->left;
6281 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6282 if (member->left->field_ident == field) {
6283 member = member->left;
6284 break;
6285 }
6286 member = member->right;
6287 }
6288 }
6289 else {
6290 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6291 }
6292
Eric Biederman90089602004-05-28 14:11:54 +00006293 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006294 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006295 }
Patrick Georgif78e6582016-12-15 15:14:59 +01006296 size += reg_needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006297 return size;
6298}
6299
Stefan Reinauer14e22772010-04-27 06:56:47 +00006300static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006301 struct type *type, struct hash_entry *field)
6302{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006303 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006304
6305 member = 0;
6306 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6307 member = type->left;
6308 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6309 if (member->left->field_ident == field) {
6310 member = member->left;
6311 break;
6312 }
6313 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006314 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006315 }
Eric Biederman90089602004-05-28 14:11:54 +00006316 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6317 member = type->left;
6318 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6319 if (member->left->field_ident == field) {
6320 member = member->left;
6321 break;
6322 }
6323 member = member->right;
6324 }
6325 }
6326 else {
6327 internal_error(state, 0, "field_type only works on structures and unions");
6328 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006329
Eric Biederman90089602004-05-28 14:11:54 +00006330 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006331 error(state, 0, "member %s not present", field->name);
6332 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006333 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006334}
6335
Stefan Reinauer14e22772010-04-27 06:56:47 +00006336static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006337 struct type *type, ulong_t index)
6338{
6339 struct type *member;
6340 size_t size;
6341 size = 0;
6342 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6343 size = size_of(state, type->left) * index;
6344 }
6345 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6346 ulong_t i;
6347 member = type->left;
6348 i = 0;
6349 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6350 size += needed_padding(state, member->left, size);
6351 if (i == index) {
6352 member = member->left;
6353 break;
6354 }
6355 size += size_of(state, member->left);
6356 i++;
6357 member = member->right;
6358 }
6359 size += needed_padding(state, member, size);
6360 if (i != index) {
6361 internal_error(state, 0, "Missing member index: %u", index);
6362 }
6363 }
6364 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6365 ulong_t i;
6366 size = 0;
6367 member = type->left;
6368 i = 0;
6369 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6370 if (i == index) {
6371 member = member->left;
6372 break;
6373 }
6374 i++;
6375 member = member->right;
6376 }
6377 if (i != index) {
6378 internal_error(state, 0, "Missing member index: %u", index);
6379 }
6380 }
6381 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006382 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006383 "request for index %u in something not an array, tuple or join",
6384 index);
6385 }
6386 return size;
6387}
6388
Stefan Reinauer14e22772010-04-27 06:56:47 +00006389static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006390 struct type *type, ulong_t index)
6391{
6392 struct type *member;
6393 size_t size;
6394 size = 0;
6395 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6396 size = reg_size_of(state, type->left) * index;
6397 }
6398 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6399 ulong_t i;
6400 member = type->left;
6401 i = 0;
6402 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6403 size += reg_needed_padding(state, member->left, size);
6404 if (i == index) {
6405 member = member->left;
6406 break;
6407 }
6408 size += reg_size_of(state, member->left);
6409 i++;
6410 member = member->right;
6411 }
Eric Biederman90089602004-05-28 14:11:54 +00006412 if (i != index) {
6413 internal_error(state, 0, "Missing member index: %u", index);
6414 }
Patrick Georgiadcad7f2016-12-15 15:24:06 +01006415 size += reg_needed_padding(state, member, size);
Eric Biederman90089602004-05-28 14:11:54 +00006416 }
6417 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6418 ulong_t i;
6419 size = 0;
6420 member = type->left;
6421 i = 0;
6422 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6423 if (i == index) {
6424 member = member->left;
6425 break;
6426 }
6427 i++;
6428 member = member->right;
6429 }
6430 if (i != index) {
6431 internal_error(state, 0, "Missing member index: %u", index);
6432 }
6433 }
6434 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006435 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006436 "request for index %u in something not an array, tuple or join",
6437 index);
6438 }
6439 return size;
6440}
6441
6442static struct type *index_type(struct compile_state *state,
6443 struct type *type, ulong_t index)
6444{
6445 struct type *member;
6446 if (index >= type->elements) {
6447 internal_error(state, 0, "Invalid element %u requested", index);
6448 }
6449 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6450 member = type->left;
6451 }
6452 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6453 ulong_t i;
6454 member = type->left;
6455 i = 0;
6456 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6457 if (i == index) {
6458 member = member->left;
6459 break;
6460 }
6461 i++;
6462 member = member->right;
6463 }
6464 if (i != index) {
6465 internal_error(state, 0, "Missing member index: %u", index);
6466 }
6467 }
6468 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6469 ulong_t i;
6470 member = type->left;
6471 i = 0;
6472 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6473 if (i == index) {
6474 member = member->left;
6475 break;
6476 }
6477 i++;
6478 member = member->right;
6479 }
6480 if (i != index) {
6481 internal_error(state, 0, "Missing member index: %u", index);
6482 }
6483 }
6484 else {
6485 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006486 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006487 "request for index %u in something not an array, tuple or join",
6488 index);
6489 }
6490 return member;
6491}
6492
6493static struct type *unpack_type(struct compile_state *state, struct type *type)
6494{
6495 /* If I have a single register compound type not a bit-field
6496 * find the real type.
6497 */
6498 struct type *start_type;
6499 size_t size;
6500 /* Get out early if I need multiple registers for this type */
6501 size = reg_size_of(state, type);
6502 if (size > REG_SIZEOF_REG) {
6503 return type;
6504 }
6505 /* Get out early if I don't need any registers for this type */
6506 if (size == 0) {
6507 return &void_type;
6508 }
6509 /* Loop until I have no more layers I can remove */
6510 do {
6511 start_type = type;
6512 switch(type->type & TYPE_MASK) {
6513 case TYPE_ARRAY:
6514 /* If I have a single element the unpacked type
6515 * is that element.
6516 */
6517 if (type->elements == 1) {
6518 type = type->left;
6519 }
6520 break;
6521 case TYPE_STRUCT:
6522 case TYPE_TUPLE:
6523 /* If I have a single element the unpacked type
6524 * is that element.
6525 */
6526 if (type->elements == 1) {
6527 type = type->left;
6528 }
6529 /* If I have multiple elements the unpacked
6530 * type is the non-void element.
6531 */
6532 else {
6533 struct type *next, *member;
6534 struct type *sub_type;
6535 sub_type = 0;
6536 next = type->left;
6537 while(next) {
6538 member = next;
6539 next = 0;
6540 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6541 next = member->right;
6542 member = member->left;
6543 }
6544 if (reg_size_of(state, member) > 0) {
6545 if (sub_type) {
6546 internal_error(state, 0, "true compound type in a register");
6547 }
6548 sub_type = member;
6549 }
6550 }
6551 if (sub_type) {
6552 type = sub_type;
6553 }
6554 }
6555 break;
6556
6557 case TYPE_UNION:
6558 case TYPE_JOIN:
6559 /* If I have a single element the unpacked type
6560 * is that element.
6561 */
6562 if (type->elements == 1) {
6563 type = type->left;
6564 }
6565 /* I can't in general unpack union types */
6566 break;
6567 default:
6568 /* If I'm not a compound type I can't unpack it */
6569 break;
6570 }
6571 } while(start_type != type);
6572 switch(type->type & TYPE_MASK) {
6573 case TYPE_STRUCT:
6574 case TYPE_ARRAY:
6575 case TYPE_TUPLE:
6576 internal_error(state, 0, "irredicible type?");
6577 break;
6578 }
6579 return type;
6580}
6581
6582static int equiv_types(struct type *left, struct type *right);
6583static int is_compound_type(struct type *type);
6584
6585static struct type *reg_type(
6586 struct compile_state *state, struct type *type, int reg_offset)
6587{
6588 struct type *member;
6589 size_t size;
6590#if 1
6591 struct type *invalid;
6592 invalid = invalid_type(state, type);
6593 if (invalid) {
6594 fprintf(state->errout, "type: ");
6595 name_of(state->errout, type);
6596 fprintf(state->errout, "\n");
6597 fprintf(state->errout, "invalid: ");
6598 name_of(state->errout, invalid);
6599 fprintf(state->errout, "\n");
6600 internal_error(state, 0, "bad input type?");
6601 }
6602#endif
6603
6604 size = reg_size_of(state, type);
6605 if (reg_offset > size) {
6606 member = 0;
6607 fprintf(state->errout, "type: ");
6608 name_of(state->errout, type);
6609 fprintf(state->errout, "\n");
6610 internal_error(state, 0, "offset outside of type");
6611 }
6612 else {
6613 switch(type->type & TYPE_MASK) {
6614 /* Don't do anything with the basic types */
6615 case TYPE_VOID:
6616 case TYPE_CHAR: case TYPE_UCHAR:
6617 case TYPE_SHORT: case TYPE_USHORT:
6618 case TYPE_INT: case TYPE_UINT:
6619 case TYPE_LONG: case TYPE_ULONG:
6620 case TYPE_LLONG: case TYPE_ULLONG:
6621 case TYPE_FLOAT: case TYPE_DOUBLE:
6622 case TYPE_LDOUBLE:
6623 case TYPE_POINTER:
6624 case TYPE_ENUM:
6625 case TYPE_BITFIELD:
6626 member = type;
6627 break;
6628 case TYPE_ARRAY:
6629 member = type->left;
6630 size = reg_size_of(state, member);
6631 if (size > REG_SIZEOF_REG) {
6632 member = reg_type(state, member, reg_offset % size);
6633 }
6634 break;
6635 case TYPE_STRUCT:
6636 case TYPE_TUPLE:
6637 {
6638 size_t offset;
6639 offset = 0;
6640 member = type->left;
6641 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6642 size = reg_size_of(state, member->left);
6643 offset += reg_needed_padding(state, member->left, offset);
6644 if ((offset + size) > reg_offset) {
6645 member = member->left;
6646 break;
6647 }
6648 offset += size;
6649 member = member->right;
6650 }
6651 offset += reg_needed_padding(state, member, offset);
6652 member = reg_type(state, member, reg_offset - offset);
6653 break;
6654 }
6655 case TYPE_UNION:
6656 case TYPE_JOIN:
6657 {
6658 struct type *join, **jnext, *mnext;
6659 join = new_type(TYPE_JOIN, 0, 0);
6660 jnext = &join->left;
6661 mnext = type->left;
6662 while(mnext) {
6663 size_t size;
6664 member = mnext;
6665 mnext = 0;
6666 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6667 mnext = member->right;
6668 member = member->left;
6669 }
6670 size = reg_size_of(state, member);
6671 if (size > reg_offset) {
6672 struct type *part, *hunt;
6673 part = reg_type(state, member, reg_offset);
6674 /* See if this type is already in the union */
6675 hunt = join->left;
6676 while(hunt) {
6677 struct type *test = hunt;
6678 hunt = 0;
6679 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6680 hunt = test->right;
6681 test = test->left;
6682 }
6683 if (equiv_types(part, test)) {
6684 goto next;
6685 }
6686 }
6687 /* Nope add it */
6688 if (!*jnext) {
6689 *jnext = part;
6690 } else {
6691 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6692 jnext = &(*jnext)->right;
6693 }
6694 join->elements++;
6695 }
6696 next:
6697 ;
6698 }
6699 if (join->elements == 0) {
6700 internal_error(state, 0, "No elements?");
6701 }
6702 member = join;
6703 break;
6704 }
6705 default:
6706 member = 0;
6707 fprintf(state->errout, "type: ");
6708 name_of(state->errout, type);
6709 fprintf(state->errout, "\n");
6710 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006711
Eric Biederman90089602004-05-28 14:11:54 +00006712 }
6713 }
6714 /* If I have a single register compound type not a bit-field
6715 * find the real type.
6716 */
6717 member = unpack_type(state, member);
6718 ;
6719 size = reg_size_of(state, member);
6720 if (size > REG_SIZEOF_REG) {
6721 internal_error(state, 0, "Cannot find type of single register");
6722 }
6723#if 1
6724 invalid = invalid_type(state, member);
6725 if (invalid) {
6726 fprintf(state->errout, "type: ");
6727 name_of(state->errout, member);
6728 fprintf(state->errout, "\n");
6729 fprintf(state->errout, "invalid: ");
6730 name_of(state->errout, invalid);
6731 fprintf(state->errout, "\n");
6732 internal_error(state, 0, "returning bad type?");
6733 }
6734#endif
6735 return member;
6736}
6737
Eric Biederman03b59862003-06-24 14:27:37 +00006738static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006739 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006740{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006741 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006742 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6743 internal_error(state, 0, "next_field only works on structures");
6744 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006745 member = type->left;
6746 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006747 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006748 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006749 break;
6750 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006751 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006752 prev_member = 0;
6753 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006754 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006755 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006756 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006757 prev_member = 0;
6758 }
6759 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006760 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006761 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006762 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006763 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006764}
6765
Stefan Reinauer14e22772010-04-27 06:56:47 +00006766typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006767 size_t ret_offset, size_t mem_offset, void *arg);
6768
6769static void walk_type_fields(struct compile_state *state,
6770 struct type *type, size_t reg_offset, size_t mem_offset,
6771 walk_type_fields_cb_t cb, void *arg);
6772
6773static void walk_struct_fields(struct compile_state *state,
6774 struct type *type, size_t reg_offset, size_t mem_offset,
6775 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006776{
Eric Biederman90089602004-05-28 14:11:54 +00006777 struct type *tptr;
6778 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006779 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006780 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006781 }
Eric Biederman90089602004-05-28 14:11:54 +00006782 tptr = type->left;
6783 for(i = 0; i < type->elements; i++) {
6784 struct type *mtype;
6785 mtype = tptr;
6786 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6787 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006788 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006789 walk_type_fields(state, mtype,
6790 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006791 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006792 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006793 field_offset(state, type, mtype->field_ident),
6794 cb, arg);
6795 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006796 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006797
Eric Biederman90089602004-05-28 14:11:54 +00006798}
6799
6800static void walk_type_fields(struct compile_state *state,
6801 struct type *type, size_t reg_offset, size_t mem_offset,
6802 walk_type_fields_cb_t cb, void *arg)
6803{
6804 switch(type->type & TYPE_MASK) {
6805 case TYPE_STRUCT:
6806 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6807 break;
6808 case TYPE_CHAR:
6809 case TYPE_UCHAR:
6810 case TYPE_SHORT:
6811 case TYPE_USHORT:
6812 case TYPE_INT:
6813 case TYPE_UINT:
6814 case TYPE_LONG:
6815 case TYPE_ULONG:
6816 cb(state, type, reg_offset, mem_offset, arg);
6817 break;
6818 case TYPE_VOID:
6819 break;
6820 default:
6821 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006822 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006823}
6824
Eric Biedermanb138ac82003-04-22 18:44:01 +00006825static void arrays_complete(struct compile_state *state, struct type *type)
6826{
6827 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6828 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6829 error(state, 0, "array size not specified");
6830 }
6831 arrays_complete(state, type->left);
6832 }
6833}
6834
Eric Biederman90089602004-05-28 14:11:54 +00006835static unsigned int get_basic_type(struct type *type)
6836{
6837 unsigned int basic;
6838 basic = type->type & TYPE_MASK;
6839 /* Convert enums to ints */
6840 if (basic == TYPE_ENUM) {
6841 basic = TYPE_INT;
6842 }
6843 /* Convert bitfields to standard types */
6844 else if (basic == TYPE_BITFIELD) {
6845 if (type->elements <= SIZEOF_CHAR) {
6846 basic = TYPE_CHAR;
6847 }
6848 else if (type->elements <= SIZEOF_SHORT) {
6849 basic = TYPE_SHORT;
6850 }
6851 else if (type->elements <= SIZEOF_INT) {
6852 basic = TYPE_INT;
6853 }
6854 else if (type->elements <= SIZEOF_LONG) {
6855 basic = TYPE_LONG;
6856 }
6857 if (!TYPE_SIGNED(type->left->type)) {
6858 basic += 1;
6859 }
6860 }
6861 return basic;
6862}
6863
Eric Biedermanb138ac82003-04-22 18:44:01 +00006864static unsigned int do_integral_promotion(unsigned int type)
6865{
Eric Biederman83b991a2003-10-11 06:20:25 +00006866 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006867 type = TYPE_INT;
6868 }
6869 return type;
6870}
6871
6872static unsigned int do_arithmetic_conversion(
6873 unsigned int left, unsigned int right)
6874{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006875 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6876 return TYPE_LDOUBLE;
6877 }
6878 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6879 return TYPE_DOUBLE;
6880 }
6881 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6882 return TYPE_FLOAT;
6883 }
6884 left = do_integral_promotion(left);
6885 right = do_integral_promotion(right);
6886 /* If both operands have the same size done */
6887 if (left == right) {
6888 return left;
6889 }
6890 /* If both operands have the same signedness pick the larger */
6891 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6892 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6893 }
6894 /* If the signed type can hold everything use it */
6895 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6896 return left;
6897 }
6898 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6899 return right;
6900 }
6901 /* Convert to the unsigned type with the same rank as the signed type */
6902 else if (TYPE_SIGNED(left)) {
6903 return TYPE_MKUNSIGNED(left);
6904 }
6905 else {
6906 return TYPE_MKUNSIGNED(right);
6907 }
6908}
6909
6910/* see if two types are the same except for qualifiers */
6911static int equiv_types(struct type *left, struct type *right)
6912{
6913 unsigned int type;
6914 /* Error if the basic types do not match */
6915 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6916 return 0;
6917 }
6918 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006919 /* If the basic types match and it is a void type we are done */
6920 if (type == TYPE_VOID) {
6921 return 1;
6922 }
Eric Biederman90089602004-05-28 14:11:54 +00006923 /* For bitfields we need to compare the sizes */
6924 else if (type == TYPE_BITFIELD) {
6925 return (left->elements == right->elements) &&
6926 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6927 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006928 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006929 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006930 return 1;
6931 }
6932 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006933 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006934 return equiv_types(left->left, right->left);
6935 }
6936 else if (type == TYPE_ARRAY) {
6937 return (left->elements == right->elements) &&
6938 equiv_types(left->left, right->left);
6939 }
Eric Biederman90089602004-05-28 14:11:54 +00006940 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006941 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006942 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006943 }
Eric Biederman90089602004-05-28 14:11:54 +00006944 /* test for union equality */
6945 else if (type == TYPE_UNION) {
6946 return left->type_ident == right->type_ident;
6947 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006948 /* Test for equivalent functions */
6949 else if (type == TYPE_FUNCTION) {
6950 return equiv_types(left->left, right->left) &&
6951 equiv_types(left->right, right->right);
6952 }
6953 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006954 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006955 else if (type == TYPE_PRODUCT) {
6956 return equiv_types(left->left, right->left) &&
6957 equiv_types(left->right, right->right);
6958 }
Eric Biederman90089602004-05-28 14:11:54 +00006959 /* We should see TYPE_OVERLAP when comparing joins */
6960 else if (type == TYPE_OVERLAP) {
6961 return equiv_types(left->left, right->left) &&
6962 equiv_types(left->right, right->right);
6963 }
6964 /* Test for equivalence of tuples */
6965 else if (type == TYPE_TUPLE) {
6966 return (left->elements == right->elements) &&
6967 equiv_types(left->left, right->left);
6968 }
6969 /* Test for equivalence of joins */
6970 else if (type == TYPE_JOIN) {
6971 return (left->elements == right->elements) &&
6972 equiv_types(left->left, right->left);
6973 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006974 else {
6975 return 0;
6976 }
6977}
6978
6979static int equiv_ptrs(struct type *left, struct type *right)
6980{
6981 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6982 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6983 return 0;
6984 }
6985 return equiv_types(left->left, right->left);
6986}
6987
6988static struct type *compatible_types(struct type *left, struct type *right)
6989{
6990 struct type *result;
6991 unsigned int type, qual_type;
6992 /* Error if the basic types do not match */
6993 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6994 return 0;
6995 }
6996 type = left->type & TYPE_MASK;
6997 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6998 result = 0;
6999 /* if the basic types match and it is an arithmetic type we are done */
7000 if (TYPE_ARITHMETIC(type)) {
7001 result = new_type(qual_type, 0, 0);
7002 }
7003 /* If it is a pointer type recurse and keep testing */
7004 else if (type == TYPE_POINTER) {
7005 result = compatible_types(left->left, right->left);
7006 if (result) {
7007 result = new_type(qual_type, result, 0);
7008 }
7009 }
Eric Biederman90089602004-05-28 14:11:54 +00007010 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007011 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007012 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007013 result = left;
7014 }
7015 }
Eric Biederman90089602004-05-28 14:11:54 +00007016 /* test for union equality */
7017 else if (type == TYPE_UNION) {
7018 if (left->type_ident == right->type_ident) {
7019 result = left;
7020 }
7021 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007022 /* Test for equivalent functions */
7023 else if (type == TYPE_FUNCTION) {
7024 struct type *lf, *rf;
7025 lf = compatible_types(left->left, right->left);
7026 rf = compatible_types(left->right, right->right);
7027 if (lf && rf) {
7028 result = new_type(qual_type, lf, rf);
7029 }
7030 }
7031 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7032 else if (type == TYPE_PRODUCT) {
7033 struct type *lf, *rf;
7034 lf = compatible_types(left->left, right->left);
7035 rf = compatible_types(left->right, right->right);
7036 if (lf && rf) {
7037 result = new_type(qual_type, lf, rf);
7038 }
7039 }
7040 else {
7041 /* Nothing else is compatible */
7042 }
7043 return result;
7044}
7045
Eric Biederman90089602004-05-28 14:11:54 +00007046/* See if left is a equivalent to right or right is a union member of left */
7047static int is_subset_type(struct type *left, struct type *right)
7048{
7049 if (equiv_types(left, right)) {
7050 return 1;
7051 }
7052 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7053 struct type *member, *mnext;
7054 mnext = left->left;
7055 while(mnext) {
7056 member = mnext;
7057 mnext = 0;
7058 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7059 mnext = member->right;
7060 member = member->left;
7061 }
7062 if (is_subset_type( member, right)) {
7063 return 1;
7064 }
7065 }
7066 }
7067 return 0;
7068}
7069
Eric Biedermanb138ac82003-04-22 18:44:01 +00007070static struct type *compatible_ptrs(struct type *left, struct type *right)
7071{
7072 struct type *result;
7073 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7074 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7075 return 0;
7076 }
7077 result = compatible_types(left->left, right->left);
7078 if (result) {
7079 unsigned int qual_type;
7080 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7081 result = new_type(qual_type, result, 0);
7082 }
7083 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007084
Eric Biedermanb138ac82003-04-22 18:44:01 +00007085}
7086static struct triple *integral_promotion(
7087 struct compile_state *state, struct triple *def)
7088{
7089 struct type *type;
7090 type = def->type;
7091 /* As all operations are carried out in registers
7092 * the values are converted on load I just convert
7093 * logical type of the operand.
7094 */
7095 if (TYPE_INTEGER(type->type)) {
7096 unsigned int int_type;
7097 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007098 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007099 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007100 if (def->op != OP_LOAD) {
7101 def->type = new_type(int_type, 0, 0);
7102 }
7103 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007104 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007105 new_type(int_type, 0, 0), def, 0);
7106 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007107 }
7108 }
7109 return def;
7110}
7111
7112
7113static void arithmetic(struct compile_state *state, struct triple *def)
7114{
7115 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007116 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007117 }
7118}
7119
7120static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7121{
7122 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7123 error(state, def, "pointer or arithmetic type expected");
7124 }
7125}
7126
7127static int is_integral(struct triple *ins)
7128{
7129 return TYPE_INTEGER(ins->type->type);
7130}
7131
7132static void integral(struct compile_state *state, struct triple *def)
7133{
7134 if (!is_integral(def)) {
7135 error(state, 0, "integral type expected");
7136 }
7137}
7138
7139
7140static void bool(struct compile_state *state, struct triple *def)
7141{
7142 if (!TYPE_ARITHMETIC(def->type->type) &&
7143 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7144 error(state, 0, "arithmetic or pointer type expected");
7145 }
7146}
7147
7148static int is_signed(struct type *type)
7149{
Eric Biederman90089602004-05-28 14:11:54 +00007150 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7151 type = type->left;
7152 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007153 return !!TYPE_SIGNED(type->type);
7154}
Eric Biederman90089602004-05-28 14:11:54 +00007155static int is_compound_type(struct type *type)
7156{
7157 int is_compound;
7158 switch((type->type & TYPE_MASK)) {
7159 case TYPE_ARRAY:
7160 case TYPE_STRUCT:
7161 case TYPE_TUPLE:
7162 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007163 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007164 is_compound = 1;
7165 break;
7166 default:
7167 is_compound = 0;
7168 break;
7169 }
7170 return is_compound;
7171}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007172
Eric Biederman0babc1c2003-05-09 02:39:00 +00007173/* Is this value located in a register otherwise it must be in memory */
7174static int is_in_reg(struct compile_state *state, struct triple *def)
7175{
7176 int in_reg;
7177 if (def->op == OP_ADECL) {
7178 in_reg = 1;
7179 }
7180 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7181 in_reg = 0;
7182 }
Eric Biederman90089602004-05-28 14:11:54 +00007183 else if (triple_is_part(state, def)) {
7184 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007185 }
7186 else {
Eric Biederman90089602004-05-28 14:11:54 +00007187 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007188 in_reg = -1;
7189 }
7190 return in_reg;
7191}
7192
Eric Biederman90089602004-05-28 14:11:54 +00007193/* Is this an auto or static variable location? Something that can
7194 * be assigned to. Otherwise it must must be a pure value, a temporary.
7195 */
7196static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007197{
7198 int ret;
7199 ret = 0;
7200 if (!def) {
7201 return 0;
7202 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007203 if ((def->op == OP_ADECL) ||
7204 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007205 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007206 (def->op == OP_BLOBCONST) ||
7207 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007208 ret = 1;
7209 }
Eric Biederman90089602004-05-28 14:11:54 +00007210 else if (triple_is_part(state, def)) {
7211 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007212 }
7213 return ret;
7214}
7215
Eric Biederman00443072003-06-24 12:34:45 +00007216static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007217{
7218 if (!def) {
7219 internal_error(state, def, "nothing where lvalue expected?");
7220 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007221 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007222 error(state, def, "lvalue expected");
7223 }
7224}
Eric Biederman00443072003-06-24 12:34:45 +00007225static void lvalue(struct compile_state *state, struct triple *def)
7226{
7227 clvalue(state, def);
7228 if (def->type->type & QUAL_CONST) {
7229 error(state, def, "modifable lvalue expected");
7230 }
7231}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007232
7233static int is_pointer(struct triple *def)
7234{
7235 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7236}
7237
7238static void pointer(struct compile_state *state, struct triple *def)
7239{
7240 if (!is_pointer(def)) {
7241 error(state, def, "pointer expected");
7242 }
7243}
7244
7245static struct triple *int_const(
7246 struct compile_state *state, struct type *type, ulong_t value)
7247{
7248 struct triple *result;
7249 switch(type->type & TYPE_MASK) {
7250 case TYPE_CHAR:
7251 case TYPE_INT: case TYPE_UINT:
7252 case TYPE_LONG: case TYPE_ULONG:
7253 break;
7254 default:
Eric Biederman90089602004-05-28 14:11:54 +00007255 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007256 }
7257 result = triple(state, OP_INTCONST, type, 0, 0);
7258 result->u.cval = value;
7259 return result;
7260}
7261
7262
Eric Biederman83b991a2003-10-11 06:20:25 +00007263static struct triple *read_expr(struct compile_state *state, struct triple *def);
7264
Stefan Reinauer14e22772010-04-27 06:56:47 +00007265static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007266 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007267{
7268 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007269 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007270 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007271
Eric Biederman90089602004-05-28 14:11:54 +00007272 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007273
Stefan Reinauer14e22772010-04-27 06:56:47 +00007274
Eric Biedermanb138ac82003-04-22 18:44:01 +00007275 result = 0;
7276 if (expr->op == OP_ADECL) {
7277 error(state, expr, "address of auto variables not supported");
7278 }
7279 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007280 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007281 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007282 result->u.cval = offset;
7283 }
7284 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007285 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007286 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007287 int_const(state, &ulong_type, offset));
7288 }
Eric Biederman90089602004-05-28 14:11:54 +00007289 else if (expr->op == OP_BLOBCONST) {
7290 FINISHME();
7291 internal_error(state, expr, "not yet implemented");
7292 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007293 else if (expr->op == OP_LIST) {
7294 error(state, 0, "Function addresses not supported");
7295 }
Eric Biederman90089602004-05-28 14:11:54 +00007296 else if (triple_is_part(state, expr)) {
7297 struct triple *part;
7298 part = expr;
7299 expr = MISC(expr, 0);
7300 if (part->op == OP_DOT) {
7301 offset += bits_to_bytes(
7302 field_offset(state, expr->type, part->u.field));
7303 }
7304 else if (part->op == OP_INDEX) {
7305 offset += bits_to_bytes(
7306 index_offset(state, expr->type, part->u.cval));
7307 }
7308 else {
7309 internal_error(state, part, "unhandled part type");
7310 }
7311 result = do_mk_addr_expr(state, expr, type, offset);
7312 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007313 if (!result) {
7314 internal_error(state, expr, "cannot take address of expression");
7315 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007316 return result;
7317}
7318
Eric Biederman0babc1c2003-05-09 02:39:00 +00007319static struct triple *mk_addr_expr(
7320 struct compile_state *state, struct triple *expr, ulong_t offset)
7321{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007322 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007323}
7324
Eric Biedermanb138ac82003-04-22 18:44:01 +00007325static struct triple *mk_deref_expr(
7326 struct compile_state *state, struct triple *expr)
7327{
7328 struct type *base_type;
7329 pointer(state, expr);
7330 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007331 return triple(state, OP_DEREF, base_type, expr, 0);
7332}
7333
Eric Biederman90089602004-05-28 14:11:54 +00007334/* lvalue conversions always apply except when certain operators
7335 * are applied. So I apply apply it when I know no more
7336 * operators will be applied.
7337 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007338static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007339{
Eric Biederman5cd81732004-03-11 15:01:31 +00007340 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007341 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7342 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007343 type = new_type(
7344 TYPE_POINTER | (def->type->type & QUAL_MASK),
7345 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007346 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007347 struct triple *addrconst;
7348 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7349 internal_error(state, def, "bad array constant");
7350 }
7351 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7352 MISC(addrconst, 0) = def;
7353 def = addrconst;
7354 }
7355 else {
Eric Biederman90089602004-05-28 14:11:54 +00007356 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007357 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007358 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007359 /* Transform a function to a pointer to it */
7360 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7361 def = mk_addr_expr(state, def, 0);
7362 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007363 return def;
7364}
7365
Eric Biederman0babc1c2003-05-09 02:39:00 +00007366static struct triple *deref_field(
7367 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7368{
7369 struct triple *result;
7370 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007371 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007372 if (!field) {
7373 internal_error(state, 0, "No field passed to deref_field");
7374 }
7375 result = 0;
7376 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007377 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7378 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007379 error(state, 0, "request for member %s in something not a struct or union",
7380 field->name);
7381 }
Eric Biederman03b59862003-06-24 14:27:37 +00007382 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007383 if ((type->type & STOR_MASK) == STOR_PERM) {
7384 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007385 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007386 result = do_mk_addr_expr(state, expr, member, offset);
7387 result = mk_deref_expr(state, result);
7388 }
7389 else {
7390 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007391 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007392 result->u.field = field;
7393 }
7394 return result;
7395}
7396
Eric Biederman90089602004-05-28 14:11:54 +00007397static struct triple *deref_index(
7398 struct compile_state *state, struct triple *expr, size_t index)
7399{
7400 struct triple *result;
7401 struct type *type, *member;
7402 ulong_t offset;
7403
7404 result = 0;
7405 type = expr->type;
7406 member = index_type(state, type, index);
7407
7408 if ((type->type & STOR_MASK) == STOR_PERM) {
7409 offset = bits_to_bytes(index_offset(state, type, index));
7410 result = do_mk_addr_expr(state, expr, member, offset);
7411 result = mk_deref_expr(state, result);
7412 }
7413 else {
7414 result = triple(state, OP_INDEX, member, expr, 0);
7415 result->u.cval = index;
7416 }
7417 return result;
7418}
7419
Eric Biedermanb138ac82003-04-22 18:44:01 +00007420static struct triple *read_expr(struct compile_state *state, struct triple *def)
7421{
7422 int op;
7423 if (!def) {
7424 return 0;
7425 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007426#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007427#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007428#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007429 /* Transform lvalues into something we can read */
7430 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007431 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007432 return def;
7433 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007434 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007435 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007436 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007437 if (def->op == OP_SDECL) {
7438 def = mk_addr_expr(state, def, 0);
7439 def = mk_deref_expr(state, def);
7440 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007441 op = OP_LOAD;
7442 }
Eric Biederman90089602004-05-28 14:11:54 +00007443 def = triple(state, op, def->type, def, 0);
7444 if (def->type->type & QUAL_VOLATILE) {
7445 def->id |= TRIPLE_FLAG_VOLATILE;
7446 }
7447 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007448}
7449
Stefan Reinauer14e22772010-04-27 06:56:47 +00007450int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007451 struct type *dest, struct type *rval)
7452{
7453 int compatible = 0;
7454 /* Both operands have arithmetic type */
7455 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7456 compatible = 1;
7457 }
7458 /* One operand is a pointer and the other is a pointer to void */
7459 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7460 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7461 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7462 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7463 compatible = 1;
7464 }
7465 /* If both types are the same without qualifiers we are good */
7466 else if (equiv_ptrs(dest, rval)) {
7467 compatible = 1;
7468 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007469 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007470 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007471 compatible = 1;
7472 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007473 return compatible;
7474}
7475
Eric Biedermane058a1e2003-07-12 01:21:31 +00007476static void write_compatible(struct compile_state *state,
7477 struct type *dest, struct type *rval)
7478{
7479 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007480 FILE *fp = state->errout;
7481 fprintf(fp, "dest: ");
7482 name_of(fp, dest);
7483 fprintf(fp,"\nrval: ");
7484 name_of(fp, rval);
7485 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007486 error(state, 0, "Incompatible types in assignment");
7487 }
7488}
7489
Eric Biedermane058a1e2003-07-12 01:21:31 +00007490static int is_init_compatible(struct compile_state *state,
7491 struct type *dest, struct type *rval)
7492{
7493 int compatible = 0;
7494 if (is_write_compatible(state, dest, rval)) {
7495 compatible = 1;
7496 }
7497 else if (equiv_types(dest, rval)) {
7498 compatible = 1;
7499 }
7500 return compatible;
7501}
7502
Eric Biedermanb138ac82003-04-22 18:44:01 +00007503static struct triple *write_expr(
7504 struct compile_state *state, struct triple *dest, struct triple *rval)
7505{
7506 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007507
7508 def = 0;
7509 if (!rval) {
7510 internal_error(state, 0, "missing rval");
7511 }
7512
7513 if (rval->op == OP_LIST) {
7514 internal_error(state, 0, "expression of type OP_LIST?");
7515 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007516 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007517 internal_error(state, 0, "writing to a non lvalue?");
7518 }
Eric Biederman00443072003-06-24 12:34:45 +00007519 if (dest->type->type & QUAL_CONST) {
7520 internal_error(state, 0, "modifable lvalue expexted");
7521 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007522
7523 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007524 if (!equiv_types(dest->type, rval->type)) {
7525 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7526 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007527
7528 /* Now figure out which assignment operator to use */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007529 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007530 def = triple(state, OP_WRITE, dest->type, rval, dest);
7531 if (MISC(def, 0) != dest) {
7532 internal_error(state, def, "huh?");
7533 }
7534 if (RHS(def, 0) != rval) {
7535 internal_error(state, def, "huh?");
7536 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007537 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007538 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007539 }
Eric Biederman90089602004-05-28 14:11:54 +00007540 if (def->type->type & QUAL_VOLATILE) {
7541 def->id |= TRIPLE_FLAG_VOLATILE;
7542 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007543 return def;
7544}
7545
7546static struct triple *init_expr(
7547 struct compile_state *state, struct triple *dest, struct triple *rval)
7548{
7549 struct triple *def;
7550
7551 def = 0;
7552 if (!rval) {
7553 internal_error(state, 0, "missing rval");
7554 }
7555 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7556 rval = read_expr(state, rval);
7557 def = write_expr(state, dest, rval);
7558 }
7559 else {
7560 /* Fill in the array size if necessary */
7561 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7562 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7563 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7564 dest->type->elements = rval->type->elements;
7565 }
7566 }
7567 if (!equiv_types(dest->type, rval->type)) {
7568 error(state, 0, "Incompatible types in inializer");
7569 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007570 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007571 insert_triple(state, dest, rval);
7572 rval->id |= TRIPLE_FLAG_FLATTENED;
7573 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007574 }
7575 return def;
7576}
7577
7578struct type *arithmetic_result(
7579 struct compile_state *state, struct triple *left, struct triple *right)
7580{
7581 struct type *type;
7582 /* Sanity checks to ensure I am working with arithmetic types */
7583 arithmetic(state, left);
7584 arithmetic(state, right);
7585 type = new_type(
7586 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007587 get_basic_type(left->type),
7588 get_basic_type(right->type)),
7589 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007590 return type;
7591}
7592
7593struct type *ptr_arithmetic_result(
7594 struct compile_state *state, struct triple *left, struct triple *right)
7595{
7596 struct type *type;
7597 /* Sanity checks to ensure I am working with the proper types */
7598 ptr_arithmetic(state, left);
7599 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007600 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007601 TYPE_ARITHMETIC(right->type->type)) {
7602 type = arithmetic_result(state, left, right);
7603 }
7604 else if (TYPE_PTR(left->type->type)) {
7605 type = left->type;
7606 }
7607 else {
7608 internal_error(state, 0, "huh?");
7609 type = 0;
7610 }
7611 return type;
7612}
7613
Eric Biedermanb138ac82003-04-22 18:44:01 +00007614/* boolean helper function */
7615
Stefan Reinauer14e22772010-04-27 06:56:47 +00007616static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007617 struct triple *expr)
7618{
7619 switch(expr->op) {
7620 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7621 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7622 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7623 /* If the expression is already boolean do nothing */
7624 break;
7625 default:
7626 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7627 break;
7628 }
7629 return expr;
7630}
7631
Stefan Reinauer14e22772010-04-27 06:56:47 +00007632static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007633 struct triple *expr)
7634{
7635 return triple(state, OP_LFALSE, &int_type, expr, 0);
7636}
7637
Eric Biederman90089602004-05-28 14:11:54 +00007638static struct triple *mkland_expr(
7639 struct compile_state *state,
7640 struct triple *left, struct triple *right)
7641{
7642 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007643 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007644
7645 /* Generate some intermediate triples */
7646 end = label(state);
7647 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007648
Eric Biederman90089602004-05-28 14:11:54 +00007649 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007650 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007651
7652 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007653 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007654 lfalse_expr(state, read_expr(state, var)));
7655 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007656
Eric Biederman90089602004-05-28 14:11:54 +00007657 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007658 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007659
7660 /* An expression for the computed value */
7661 val = read_expr(state, var);
7662
7663 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007664 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007665
Eric Biederman90089602004-05-28 14:11:54 +00007666 return def;
7667}
7668
7669static struct triple *mklor_expr(
7670 struct compile_state *state,
7671 struct triple *left, struct triple *right)
7672{
7673 struct triple *def, *val, *var, *jmp, *mid, *end;
7674
7675 /* Generate some intermediate triples */
7676 end = label(state);
7677 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007678
Eric Biederman90089602004-05-28 14:11:54 +00007679 /* Store the left hand side value */
7680 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007681
Eric Biederman90089602004-05-28 14:11:54 +00007682 /* Jump if the value is true */
7683 jmp = branch(state, end, read_expr(state, var));
7684 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007685
Eric Biederman90089602004-05-28 14:11:54 +00007686 /* Store the right hand side value */
7687 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007688
Eric Biederman90089602004-05-28 14:11:54 +00007689 /* An expression for the computed value*/
7690 val = read_expr(state, var);
7691
7692 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007693 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007694
7695 return def;
7696}
7697
7698static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007699 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007700 struct triple *test, struct triple *left, struct triple *right)
7701{
Eric Biederman90089602004-05-28 14:11:54 +00007702 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007703 struct type *result_type;
7704 unsigned int left_type, right_type;
7705 bool(state, test);
7706 left_type = left->type->type;
7707 right_type = right->type->type;
7708 result_type = 0;
7709 /* Both operands have arithmetic type */
7710 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7711 result_type = arithmetic_result(state, left, right);
7712 }
7713 /* Both operands have void type */
7714 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7715 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7716 result_type = &void_type;
7717 }
7718 /* pointers to the same type... */
7719 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7720 ;
7721 }
7722 /* Both operands are pointers and left is a pointer to void */
7723 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7724 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7725 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7726 result_type = right->type;
7727 }
7728 /* Both operands are pointers and right is a pointer to void */
7729 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7730 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7731 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7732 result_type = left->type;
7733 }
7734 if (!result_type) {
7735 error(state, 0, "Incompatible types in conditional expression");
7736 }
Eric Biederman90089602004-05-28 14:11:54 +00007737 /* Generate some intermediate triples */
7738 mid = label(state);
7739 end = label(state);
7740 var = variable(state, result_type);
7741
7742 /* Branch if the test is false */
7743 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7744 top = label(state);
7745
7746 /* Store the left hand side value */
7747 left = write_expr(state, var, left);
7748
7749 /* Branch to the end */
7750 jmp2 = branch(state, end, 0);
7751
7752 /* Store the right hand side value */
7753 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007754
Eric Biederman90089602004-05-28 14:11:54 +00007755 /* An expression for the computed value */
7756 val = read_expr(state, var);
7757
7758 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007759 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007760
Eric Biedermanb138ac82003-04-22 18:44:01 +00007761 return def;
7762}
7763
7764
Eric Biederman0babc1c2003-05-09 02:39:00 +00007765static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007766{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007767#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007768#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007769#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007770 int count;
7771 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007772 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7773 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007774 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007775 else if (ins->op == OP_DEREF) {
7776 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007777 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007778 else if (ins->op == OP_VAL) {
7779 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007781 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007782 /* Don't figure the depth of a call just guess it is huge */
7783 count = 1000;
7784 }
7785 else {
7786 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007787 expr = triple_rhs(state, ins, 0);
7788 for(;expr; expr = triple_rhs(state, ins, expr)) {
7789 if (*expr) {
7790 int depth;
7791 depth = expr_depth(state, *expr);
7792 if (depth > count) {
7793 count = depth;
7794 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007795 }
7796 }
7797 }
7798 return count + 1;
7799}
7800
Eric Biederman0babc1c2003-05-09 02:39:00 +00007801static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007802 struct compile_state *state, struct triple *first, struct triple *ptr,
7803 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007804{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007805 struct rhs_vector {
7806 int depth;
7807 struct triple **ins;
7808 } vector[MAX_RHS];
7809 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007810 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007811 rhs = ptr->rhs;
7812 lhs = ptr->lhs;
7813 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007814 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007815 ptr->op, tops(ptr->op));
7816 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007817 /* Find the depth of the rhs elements */
7818 for(i = 0; i < rhs; i++) {
7819 vector[i].ins = &RHS(ptr, i);
7820 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007821 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007822 /* Selection sort the rhs */
7823 for(i = 0; i < rhs; i++) {
7824 int j, max = i;
7825 for(j = i + 1; j < rhs; j++ ) {
7826 if (vector[j].depth > vector[max].depth) {
7827 max = j;
7828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007829 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007830 if (max != i) {
7831 struct rhs_vector tmp;
7832 tmp = vector[i];
7833 vector[i] = vector[max];
7834 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007835 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007836 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007837 /* Now flatten the rhs elements */
7838 for(i = 0; i < rhs; i++) {
7839 *vector[i].ins = flatten(state, first, *vector[i].ins);
7840 use_triple(*vector[i].ins, ptr);
7841 }
Eric Biederman90089602004-05-28 14:11:54 +00007842 if (lhs) {
7843 insert_triple(state, first, ptr);
7844 ptr->id |= TRIPLE_FLAG_FLATTENED;
7845 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007846
Eric Biederman90089602004-05-28 14:11:54 +00007847 /* Now flatten the lhs elements */
7848 for(i = 0; i < lhs; i++) {
7849 struct triple **ins = &LHS(ptr, i);
7850 *ins = flatten(state, first, *ins);
7851 use_triple(*ins, ptr);
7852 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007853 }
7854 return ptr;
7855}
7856
Eric Biederman90089602004-05-28 14:11:54 +00007857static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007858 struct compile_state *state, struct triple *first, struct triple *ptr)
7859{
Eric Biederman90089602004-05-28 14:11:54 +00007860 struct triple *head, *body, *val;
7861 head = RHS(ptr, 0);
7862 RHS(ptr, 0) = 0;
7863 val = head->prev;
7864 body = head->next;
7865 release_triple(state, head);
7866 release_triple(state, ptr);
7867 val->next = first;
7868 body->prev = first->prev;
7869 body->prev->next = body;
7870 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007871
Eric Biederman90089602004-05-28 14:11:54 +00007872 if (triple_is_cbranch(state, body->prev) ||
7873 triple_is_call(state, body->prev)) {
7874 unuse_triple(first, body->prev);
7875 use_triple(body, body->prev);
7876 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007877
Eric Biederman90089602004-05-28 14:11:54 +00007878 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7879 internal_error(state, val, "val not flattened?");
7880 }
7881
7882 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007883}
7884
Eric Biederman90089602004-05-28 14:11:54 +00007885
7886static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007887 struct compile_state *state, struct triple *first, struct triple *ptr)
7888{
Eric Biederman90089602004-05-28 14:11:54 +00007889 if (!triple_is_part(state, ptr)) {
7890 internal_error(state, ptr, "not a part");
7891 }
7892 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7893 internal_error(state, ptr, "unexpected args for: %d %s",
7894 ptr->op, tops(ptr->op));
7895 }
7896 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7897 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007898 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007899}
7900
7901static struct triple *flatten(
7902 struct compile_state *state, struct triple *first, struct triple *ptr)
7903{
7904 struct triple *orig_ptr;
7905 if (!ptr)
7906 return 0;
7907 do {
7908 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007909 /* Only flatten triples once */
7910 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7911 return ptr;
7912 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007913 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007914 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007915 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7916 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007917 break;
Eric Biederman90089602004-05-28 14:11:54 +00007918 case OP_PROG:
7919 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007920 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007921 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007922 ptr = flatten_generic(state, first, ptr, 1);
7923 insert_triple(state, first, ptr);
7924 ptr->id |= TRIPLE_FLAG_FLATTENED;
7925 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7926 if (ptr->next != ptr) {
7927 use_triple(ptr->next, ptr);
7928 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007929 break;
7930 case OP_READ:
7931 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007932 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7933 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007934 break;
Eric Biederman90089602004-05-28 14:11:54 +00007935 case OP_WRITE:
7936 ptr = flatten_generic(state, first, ptr, 1);
7937 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7938 use_triple(MISC(ptr, 0), ptr);
7939 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007940 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007941 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007942 break;
7943 case OP_CBRANCH:
7944 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7945 use_triple(RHS(ptr, 0), ptr);
7946 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007947 insert_triple(state, first, ptr);
7948 ptr->id |= TRIPLE_FLAG_FLATTENED;
7949 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007950 if (ptr->next != ptr) {
7951 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007952 }
7953 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007954 case OP_CALL:
7955 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7956 use_triple(MISC(ptr, 0), ptr);
7957 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007958 insert_triple(state, first, ptr);
7959 ptr->id |= TRIPLE_FLAG_FLATTENED;
7960 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007961 if (ptr->next != ptr) {
7962 use_triple(ptr->next, ptr);
7963 }
7964 break;
7965 case OP_RET:
7966 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7967 use_triple(RHS(ptr, 0), ptr);
7968 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007969 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007970 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007971 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007972 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007973 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007974 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007975 break;
7976 case OP_DEREF:
7977 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007978 ptr = RHS(ptr, 0);
7979 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007980 free_triple(state, orig_ptr);
7981 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007982 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007983 if (RHS(ptr, 0)->op == OP_DEREF) {
7984 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007985 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007986 base = MISC(ptr, 0);
7987 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007988 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007989 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007990 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007991 int_const(state, &ulong_type, offset));
7992 free_triple(state, base);
7993 }
Eric Biederman90089602004-05-28 14:11:54 +00007994 else {
7995 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007996 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007997 break;
Eric Biederman90089602004-05-28 14:11:54 +00007998 case OP_INDEX:
7999 if (RHS(ptr, 0)->op == OP_DEREF) {
8000 struct triple *base, *left;
8001 ulong_t offset;
8002 base = MISC(ptr, 0);
8003 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8004 left = RHS(base, 0);
8005 ptr = triple(state, OP_ADD, left->type,
8006 read_expr(state, left),
8007 int_const(state, &long_type, offset));
8008 free_triple(state, base);
8009 }
8010 else {
8011 ptr = flatten_part(state, first, ptr);
8012 }
8013 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008014 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008015 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008016 use_triple(ptr, MISC(ptr, 0));
8017 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008018 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008019 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8020 use_triple(MISC(ptr, 0), ptr);
8021 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008022 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008023 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008024 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8025 use_triple(MISC(ptr, 0), ptr);
8026 insert_triple(state, first, ptr);
8027 ptr->id |= TRIPLE_FLAG_FLATTENED;
8028 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8029 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008030 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008031 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008032 break;
8033 default:
8034 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008035 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008036 break;
8037 }
8038 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008039 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008040 insert_triple(state, first, ptr);
8041 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008042 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008044 return ptr;
8045}
8046
8047static void release_expr(struct compile_state *state, struct triple *expr)
8048{
8049 struct triple *head;
8050 head = label(state);
8051 flatten(state, head, expr);
8052 while(head->next != head) {
8053 release_triple(state, head->next);
8054 }
8055 free_triple(state, head);
8056}
8057
8058static int replace_rhs_use(struct compile_state *state,
8059 struct triple *orig, struct triple *new, struct triple *use)
8060{
8061 struct triple **expr;
8062 int found;
8063 found = 0;
8064 expr = triple_rhs(state, use, 0);
8065 for(;expr; expr = triple_rhs(state, use, expr)) {
8066 if (*expr == orig) {
8067 *expr = new;
8068 found = 1;
8069 }
8070 }
8071 if (found) {
8072 unuse_triple(orig, use);
8073 use_triple(new, use);
8074 }
8075 return found;
8076}
8077
8078static int replace_lhs_use(struct compile_state *state,
8079 struct triple *orig, struct triple *new, struct triple *use)
8080{
8081 struct triple **expr;
8082 int found;
8083 found = 0;
8084 expr = triple_lhs(state, use, 0);
8085 for(;expr; expr = triple_lhs(state, use, expr)) {
8086 if (*expr == orig) {
8087 *expr = new;
8088 found = 1;
8089 }
8090 }
8091 if (found) {
8092 unuse_triple(orig, use);
8093 use_triple(new, use);
8094 }
8095 return found;
8096}
8097
Eric Biederman90089602004-05-28 14:11:54 +00008098static int replace_misc_use(struct compile_state *state,
8099 struct triple *orig, struct triple *new, struct triple *use)
8100{
8101 struct triple **expr;
8102 int found;
8103 found = 0;
8104 expr = triple_misc(state, use, 0);
8105 for(;expr; expr = triple_misc(state, use, expr)) {
8106 if (*expr == orig) {
8107 *expr = new;
8108 found = 1;
8109 }
8110 }
8111 if (found) {
8112 unuse_triple(orig, use);
8113 use_triple(new, use);
8114 }
8115 return found;
8116}
8117
8118static int replace_targ_use(struct compile_state *state,
8119 struct triple *orig, struct triple *new, struct triple *use)
8120{
8121 struct triple **expr;
8122 int found;
8123 found = 0;
8124 expr = triple_targ(state, use, 0);
8125 for(;expr; expr = triple_targ(state, use, expr)) {
8126 if (*expr == orig) {
8127 *expr = new;
8128 found = 1;
8129 }
8130 }
8131 if (found) {
8132 unuse_triple(orig, use);
8133 use_triple(new, use);
8134 }
8135 return found;
8136}
8137
8138static void replace_use(struct compile_state *state,
8139 struct triple *orig, struct triple *new, struct triple *use)
8140{
8141 int found;
8142 found = 0;
8143 found |= replace_rhs_use(state, orig, new, use);
8144 found |= replace_lhs_use(state, orig, new, use);
8145 found |= replace_misc_use(state, orig, new, use);
8146 found |= replace_targ_use(state, orig, new, use);
8147 if (!found) {
8148 internal_error(state, use, "use without use");
8149 }
8150}
8151
Eric Biedermanb138ac82003-04-22 18:44:01 +00008152static void propogate_use(struct compile_state *state,
8153 struct triple *orig, struct triple *new)
8154{
8155 struct triple_set *user, *next;
8156 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008157 /* Careful replace_use modifies the use chain and
8158 * removes use. So we must get a copy of the next
8159 * entry early.
8160 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008161 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008162 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008163 }
8164 if (orig->use) {
8165 internal_error(state, orig, "used after propogate_use");
8166 }
8167}
8168
8169/*
8170 * Code generators
8171 * ===========================
8172 */
8173
Eric Biederman90089602004-05-28 14:11:54 +00008174static struct triple *mk_cast_expr(
8175 struct compile_state *state, struct type *type, struct triple *expr)
8176{
8177 struct triple *def;
8178 def = read_expr(state, expr);
8179 def = triple(state, OP_CONVERT, type, def, 0);
8180 return def;
8181}
8182
Eric Biedermanb138ac82003-04-22 18:44:01 +00008183static struct triple *mk_add_expr(
8184 struct compile_state *state, struct triple *left, struct triple *right)
8185{
8186 struct type *result_type;
8187 /* Put pointer operands on the left */
8188 if (is_pointer(right)) {
8189 struct triple *tmp;
8190 tmp = left;
8191 left = right;
8192 right = tmp;
8193 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008194 left = read_expr(state, left);
8195 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008196 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008197 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008198 struct type *ptr_math;
8199 int op;
8200 if (is_signed(right->type)) {
8201 ptr_math = &long_type;
8202 op = OP_SMUL;
8203 } else {
8204 ptr_math = &ulong_type;
8205 op = OP_UMUL;
8206 }
8207 if (!equiv_types(right->type, ptr_math)) {
8208 right = mk_cast_expr(state, ptr_math, right);
8209 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008210 right = triple(state, op, ptr_math, right,
8211 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008212 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008213 }
8214 return triple(state, OP_ADD, result_type, left, right);
8215}
8216
8217static struct triple *mk_sub_expr(
8218 struct compile_state *state, struct triple *left, struct triple *right)
8219{
8220 struct type *result_type;
8221 result_type = ptr_arithmetic_result(state, left, right);
8222 left = read_expr(state, left);
8223 right = read_expr(state, right);
8224 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008225 struct type *ptr_math;
8226 int op;
8227 if (is_signed(right->type)) {
8228 ptr_math = &long_type;
8229 op = OP_SMUL;
8230 } else {
8231 ptr_math = &ulong_type;
8232 op = OP_UMUL;
8233 }
8234 if (!equiv_types(right->type, ptr_math)) {
8235 right = mk_cast_expr(state, ptr_math, right);
8236 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008237 right = triple(state, op, ptr_math, right,
8238 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008239 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008240 }
8241 return triple(state, OP_SUB, result_type, left, right);
8242}
8243
8244static struct triple *mk_pre_inc_expr(
8245 struct compile_state *state, struct triple *def)
8246{
8247 struct triple *val;
8248 lvalue(state, def);
8249 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8250 return triple(state, OP_VAL, def->type,
8251 write_expr(state, def, val),
8252 val);
8253}
8254
8255static struct triple *mk_pre_dec_expr(
8256 struct compile_state *state, struct triple *def)
8257{
8258 struct triple *val;
8259 lvalue(state, def);
8260 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8261 return triple(state, OP_VAL, def->type,
8262 write_expr(state, def, val),
8263 val);
8264}
8265
8266static struct triple *mk_post_inc_expr(
8267 struct compile_state *state, struct triple *def)
8268{
8269 struct triple *val;
8270 lvalue(state, def);
8271 val = read_expr(state, def);
8272 return triple(state, OP_VAL, def->type,
8273 write_expr(state, def,
8274 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8275 , val);
8276}
8277
8278static struct triple *mk_post_dec_expr(
8279 struct compile_state *state, struct triple *def)
8280{
8281 struct triple *val;
8282 lvalue(state, def);
8283 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008284 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008285 write_expr(state, def,
8286 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8287 , val);
8288}
8289
8290static struct triple *mk_subscript_expr(
8291 struct compile_state *state, struct triple *left, struct triple *right)
8292{
8293 left = read_expr(state, left);
8294 right = read_expr(state, right);
8295 if (!is_pointer(left) && !is_pointer(right)) {
8296 error(state, left, "subscripted value is not a pointer");
8297 }
8298 return mk_deref_expr(state, mk_add_expr(state, left, right));
8299}
8300
Eric Biedermane058a1e2003-07-12 01:21:31 +00008301
Eric Biedermanb138ac82003-04-22 18:44:01 +00008302/*
8303 * Compile time evaluation
8304 * ===========================
8305 */
8306static int is_const(struct triple *ins)
8307{
8308 return IS_CONST_OP(ins->op);
8309}
8310
Eric Biederman83b991a2003-10-11 06:20:25 +00008311static int is_simple_const(struct triple *ins)
8312{
Eric Biederman90089602004-05-28 14:11:54 +00008313 /* Is this a constant that u.cval has the value.
8314 * Or equivalently is this a constant that read_const
8315 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008316 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008317 */
8318 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008319}
8320
Stefan Reinauer14e22772010-04-27 06:56:47 +00008321static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008322 struct triple *left, struct triple *right)
8323{
8324 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008325 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8326 equal = 0;
8327 }
8328 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008329 equal = 0;
8330 }
8331 else if (left->op != right->op) {
8332 equal = 0;
8333 }
8334 else if (!equiv_types(left->type, right->type)) {
8335 equal = 0;
8336 }
8337 else {
8338 equal = 0;
8339 switch(left->op) {
8340 case OP_INTCONST:
8341 if (left->u.cval == right->u.cval) {
8342 equal = 1;
8343 }
8344 break;
8345 case OP_BLOBCONST:
8346 {
Eric Biederman90089602004-05-28 14:11:54 +00008347 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008348 lsize = size_of(state, left->type);
8349 rsize = size_of(state, right->type);
8350 if (lsize != rsize) {
8351 break;
8352 }
Eric Biederman90089602004-05-28 14:11:54 +00008353 bytes = bits_to_bytes(lsize);
8354 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008355 equal = 1;
8356 }
8357 break;
8358 }
8359 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008360 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008361 (left->u.cval == right->u.cval)) {
8362 equal = 1;
8363 }
8364 break;
8365 default:
8366 internal_error(state, left, "uknown constant type");
8367 break;
8368 }
8369 }
8370 return equal;
8371}
8372
8373static int is_zero(struct triple *ins)
8374{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008375 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008376}
8377
8378static int is_one(struct triple *ins)
8379{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008380 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008381}
8382
Stefan Reinauer50542a82007-10-24 11:14:14 +00008383#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008384static long_t bit_count(ulong_t value)
8385{
8386 int count;
8387 int i;
8388 count = 0;
8389 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8390 ulong_t mask;
8391 mask = 1;
8392 mask <<= i;
8393 if (value & mask) {
8394 count++;
8395 }
8396 }
8397 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008398
Eric Biederman530b5192003-07-01 10:05:30 +00008399}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008400#endif
8401
Eric Biedermanb138ac82003-04-22 18:44:01 +00008402static long_t bsr(ulong_t value)
8403{
8404 int i;
8405 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8406 ulong_t mask;
8407 mask = 1;
8408 mask <<= i;
8409 if (value & mask) {
8410 return i;
8411 }
8412 }
8413 return -1;
8414}
8415
8416static long_t bsf(ulong_t value)
8417{
8418 int i;
8419 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8420 ulong_t mask;
8421 mask = 1;
8422 mask <<= 1;
8423 if (value & mask) {
8424 return i;
8425 }
8426 }
8427 return -1;
8428}
8429
Eric Biedermancb364952004-11-15 10:46:44 +00008430static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008431{
8432 return bsr(value);
8433}
8434
8435static long_t tlog2(struct triple *ins)
8436{
Eric Biedermancb364952004-11-15 10:46:44 +00008437 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008438}
8439
8440static int is_pow2(struct triple *ins)
8441{
8442 ulong_t value, mask;
8443 long_t log;
8444 if (!is_const(ins)) {
8445 return 0;
8446 }
8447 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008448 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008449 if (log == -1) {
8450 return 0;
8451 }
8452 mask = 1;
8453 mask <<= log;
8454 return ((value & mask) == value);
8455}
8456
8457static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008458 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008459{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008460 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008461 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008462 case TYPE_SHORT:
8463 case TYPE_INT:
8464 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008465 case TYPE_UCHAR:
8466 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008467 case TYPE_UINT:
8468 case TYPE_ULONG:
8469 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008470 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008471 break;
8472 default:
Eric Biederman90089602004-05-28 14:11:54 +00008473 fprintf(state->errout, "type: ");
8474 name_of(state->errout, rhs->type);
8475 fprintf(state->errout, "\n");
8476 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008477 break;
8478 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008479 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008480 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008481 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008482 return rhs->u.cval;
8483}
8484
Eric Biederman5ade04a2003-10-22 04:03:46 +00008485static long_t read_sconst(struct compile_state *state,
8486 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008487{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008488 return (long_t)(rhs->u.cval);
8489}
8490
Eric Biederman5ade04a2003-10-22 04:03:46 +00008491int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8492{
8493 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008494 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008495 }
8496 return !is_zero(rhs);
8497}
8498
8499int const_eq(struct compile_state *state, struct triple *ins,
8500 struct triple *left, struct triple *right)
8501{
8502 int result;
8503 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008504 internal_warning(state, ins, "non const passed to const_eq");
8505 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008506 }
8507 else if (left == right) {
8508 result = 1;
8509 }
8510 else if (is_simple_const(left) && is_simple_const(right)) {
8511 ulong_t lval, rval;
8512 lval = read_const(state, ins, left);
8513 rval = read_const(state, ins, right);
8514 result = (lval == rval);
8515 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008516 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008517 (right->op == OP_ADDRCONST)) {
8518 result = (MISC(left, 0) == MISC(right, 0)) &&
8519 (left->u.cval == right->u.cval);
8520 }
8521 else {
Eric Biederman90089602004-05-28 14:11:54 +00008522 internal_warning(state, ins, "incomparable constants passed to const_eq");
8523 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008524 }
8525 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008526
Eric Biederman5ade04a2003-10-22 04:03:46 +00008527}
8528
8529int const_ucmp(struct compile_state *state, struct triple *ins,
8530 struct triple *left, struct triple *right)
8531{
8532 int result;
8533 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008534 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008535 result = -2;
8536 }
8537 else if (left == right) {
8538 result = 0;
8539 }
8540 else if (is_simple_const(left) && is_simple_const(right)) {
8541 ulong_t lval, rval;
8542 lval = read_const(state, ins, left);
8543 rval = read_const(state, ins, right);
8544 result = 0;
8545 if (lval > rval) {
8546 result = 1;
8547 } else if (rval > lval) {
8548 result = -1;
8549 }
8550 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008551 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008552 (right->op == OP_ADDRCONST) &&
8553 (MISC(left, 0) == MISC(right, 0))) {
8554 result = 0;
8555 if (left->u.cval > right->u.cval) {
8556 result = 1;
8557 } else if (left->u.cval < right->u.cval) {
8558 result = -1;
8559 }
8560 }
8561 else {
Eric Biederman90089602004-05-28 14:11:54 +00008562 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008563 result = -2;
8564 }
8565 return result;
8566}
8567
8568int const_scmp(struct compile_state *state, struct triple *ins,
8569 struct triple *left, struct triple *right)
8570{
8571 int result;
8572 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008573 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008574 result = -2;
8575 }
8576 else if (left == right) {
8577 result = 0;
8578 }
8579 else if (is_simple_const(left) && is_simple_const(right)) {
8580 long_t lval, rval;
8581 lval = read_sconst(state, ins, left);
8582 rval = read_sconst(state, ins, right);
8583 result = 0;
8584 if (lval > rval) {
8585 result = 1;
8586 } else if (rval > lval) {
8587 result = -1;
8588 }
8589 }
8590 else {
Eric Biederman90089602004-05-28 14:11:54 +00008591 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008592 result = -2;
8593 }
8594 return result;
8595}
8596
Eric Biedermanb138ac82003-04-22 18:44:01 +00008597static void unuse_rhs(struct compile_state *state, struct triple *ins)
8598{
8599 struct triple **expr;
8600 expr = triple_rhs(state, ins, 0);
8601 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008602 if (*expr) {
8603 unuse_triple(*expr, ins);
8604 *expr = 0;
8605 }
8606 }
8607}
8608
8609static void unuse_lhs(struct compile_state *state, struct triple *ins)
8610{
8611 struct triple **expr;
8612 expr = triple_lhs(state, ins, 0);
8613 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008614 unuse_triple(*expr, ins);
8615 *expr = 0;
8616 }
8617}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008618
Stefan Reinauer50542a82007-10-24 11:14:14 +00008619#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008620static void unuse_misc(struct compile_state *state, struct triple *ins)
8621{
8622 struct triple **expr;
8623 expr = triple_misc(state, ins, 0);
8624 for(;expr;expr = triple_misc(state, ins, expr)) {
8625 unuse_triple(*expr, ins);
8626 *expr = 0;
8627 }
8628}
8629
8630static void unuse_targ(struct compile_state *state, struct triple *ins)
8631{
8632 int i;
8633 struct triple **slot;
8634 slot = &TARG(ins, 0);
8635 for(i = 0; i < ins->targ; i++) {
8636 unuse_triple(slot[i], ins);
8637 slot[i] = 0;
8638 }
8639}
8640
Eric Biedermanb138ac82003-04-22 18:44:01 +00008641static void check_lhs(struct compile_state *state, struct triple *ins)
8642{
8643 struct triple **expr;
8644 expr = triple_lhs(state, ins, 0);
8645 for(;expr;expr = triple_lhs(state, ins, expr)) {
8646 internal_error(state, ins, "unexpected lhs");
8647 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008648
Eric Biedermanb138ac82003-04-22 18:44:01 +00008649}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008650#endif
Eric Biederman90089602004-05-28 14:11:54 +00008651
8652static void check_misc(struct compile_state *state, struct triple *ins)
8653{
8654 struct triple **expr;
8655 expr = triple_misc(state, ins, 0);
8656 for(;expr;expr = triple_misc(state, ins, expr)) {
8657 if (*expr) {
8658 internal_error(state, ins, "unexpected misc");
8659 }
8660 }
8661}
8662
Eric Biedermanb138ac82003-04-22 18:44:01 +00008663static void check_targ(struct compile_state *state, struct triple *ins)
8664{
8665 struct triple **expr;
8666 expr = triple_targ(state, ins, 0);
8667 for(;expr;expr = triple_targ(state, ins, expr)) {
8668 internal_error(state, ins, "unexpected targ");
8669 }
8670}
8671
8672static void wipe_ins(struct compile_state *state, struct triple *ins)
8673{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008674 /* Becareful which instructions you replace the wiped
8675 * instruction with, as there are not enough slots
8676 * in all instructions to hold all others.
8677 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008678 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008679 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008680 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008681 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008682 ins->lhs = 0;
8683 ins->rhs = 0;
8684 ins->misc = 0;
8685 ins->targ = 0;
8686}
8687
Stefan Reinauer50542a82007-10-24 11:14:14 +00008688#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008689static void wipe_branch(struct compile_state *state, struct triple *ins)
8690{
8691 /* Becareful which instructions you replace the wiped
8692 * instruction with, as there are not enough slots
8693 * in all instructions to hold all others.
8694 */
8695 unuse_rhs(state, ins);
8696 unuse_lhs(state, ins);
8697 unuse_misc(state, ins);
8698 unuse_targ(state, ins);
8699 ins->lhs = 0;
8700 ins->rhs = 0;
8701 ins->misc = 0;
8702 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008703}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008704#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008705
Stefan Reinauer14e22772010-04-27 06:56:47 +00008706static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008707 struct triple *ins, struct triple *rhs)
8708{
Eric Biederman83b991a2003-10-11 06:20:25 +00008709 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008710 if (!equiv_types(ins->type, rhs->type)) {
8711 FILE *fp = state->errout;
8712 fprintf(fp, "src type: ");
8713 name_of(fp, rhs->type);
8714 fprintf(fp, "\ndst type: ");
8715 name_of(fp, ins->type);
8716 fprintf(fp, "\n");
8717 internal_error(state, ins, "mkcopy type mismatch");
8718 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008719 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008720 wipe_ins(state, ins);
8721 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008722 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008723 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008724 RHS(ins, 0) = rhs;
8725 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008726}
8727
Stefan Reinauer14e22772010-04-27 06:56:47 +00008728static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008729 struct triple *ins, ulong_t value)
8730{
8731 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008732 fprintf(state->errout, "type: ");
8733 name_of(state->errout, ins->type);
8734 fprintf(state->errout, "\n");
8735 internal_error(state, ins, "unknown type to make constant value: %ld",
8736 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008737 }
8738 wipe_ins(state, ins);
8739 ins->op = OP_INTCONST;
8740 ins->u.cval = value;
8741}
8742
8743static void mkaddr_const(struct compile_state *state,
8744 struct triple *ins, struct triple *sdecl, ulong_t value)
8745{
Eric Biederman90089602004-05-28 14:11:54 +00008746 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008747 internal_error(state, ins, "bad base for addrconst");
8748 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008749 wipe_ins(state, ins);
8750 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008751 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008752 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008753 ins->u.cval = value;
8754 use_triple(sdecl, ins);
8755}
8756
Eric Biederman90089602004-05-28 14:11:54 +00008757#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008758static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008759 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008760{
Eric Biederman90089602004-05-28 14:11:54 +00008761 FILE *fp = state->dbgout;
8762 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8763 name_of(fp, tuple->type);
8764 if (tuple->lhs > 0) {
8765 fprintf(fp, " lhs: ");
8766 name_of(fp, LHS(tuple, 0)->type);
8767 }
8768 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008769
Eric Biederman90089602004-05-28 14:11:54 +00008770}
8771#endif
8772
Stefan Reinauer14e22772010-04-27 06:56:47 +00008773static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008774 struct triple *ins, struct triple *tuple)
8775{
8776 struct triple *next;
8777 next = ins->next;
8778 flatten(state, next, tuple);
8779#if DEBUG_DECOMPOSE_PRINT_TUPLES
8780 print_tuple(state, ins, tuple);
8781#endif
8782
8783 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8784 struct triple *tmp;
8785 if (tuple->lhs != 1) {
8786 internal_error(state, tuple, "plain type in multiple registers?");
8787 }
8788 tmp = LHS(tuple, 0);
8789 release_triple(state, tuple);
8790 tuple = tmp;
8791 }
8792
8793 propogate_use(state, ins, tuple);
8794 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008795
Eric Biederman90089602004-05-28 14:11:54 +00008796 return next;
8797}
8798
8799static struct triple *decompose_unknownval(struct compile_state *state,
8800 struct triple *ins)
8801{
8802 struct triple *tuple;
8803 ulong_t i;
8804
8805#if DEBUG_DECOMPOSE_HIRES
8806 FILE *fp = state->dbgout;
8807 fprintf(fp, "unknown type: ");
8808 name_of(fp, ins->type);
8809 fprintf(fp, "\n");
8810#endif
8811
8812 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008813 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008814 ins->occurance);
8815
8816 for(i = 0; i < tuple->lhs; i++) {
8817 struct type *piece_type;
8818 struct triple *unknown;
8819
8820 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8821 get_occurance(tuple->occurance);
8822 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8823 tuple->occurance);
8824 LHS(tuple, i) = unknown;
8825 }
8826 return decompose_with_tuple(state, ins, tuple);
8827}
8828
8829
Stefan Reinauer14e22772010-04-27 06:56:47 +00008830static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008831 struct triple *ins)
8832{
8833 struct triple *tuple, *lval;
8834 ulong_t i;
8835
8836 lval = RHS(ins, 0);
8837
8838 if (lval->op == OP_PIECE) {
8839 return ins->next;
8840 }
8841 get_occurance(ins->occurance);
8842 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8843 ins->occurance);
8844
8845 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008846 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008847 {
8848 internal_error(state, ins, "lhs size inconsistency?");
8849 }
8850 for(i = 0; i < tuple->lhs; i++) {
8851 struct triple *piece, *read, *bitref;
8852 if ((i != 0) || !triple_is_def(state, lval)) {
8853 piece = LHS(lval, i);
8854 } else {
8855 piece = lval;
8856 }
8857
8858 /* See if the piece is really a bitref */
8859 bitref = 0;
8860 if (piece->op == OP_BITREF) {
8861 bitref = piece;
8862 piece = RHS(bitref, 0);
8863 }
8864
8865 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008866 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008867 tuple->occurance);
8868 RHS(read, 0) = piece;
8869
8870 if (bitref) {
8871 struct triple *extract;
8872 int op;
8873 if (is_signed(bitref->type->left)) {
8874 op = OP_SEXTRACT;
8875 } else {
8876 op = OP_UEXTRACT;
8877 }
8878 get_occurance(tuple->occurance);
8879 extract = alloc_triple(state, op, bitref->type, -1, -1,
8880 tuple->occurance);
8881 RHS(extract, 0) = read;
8882 extract->u.bitfield.size = bitref->u.bitfield.size;
8883 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8884
8885 read = extract;
8886 }
8887
8888 LHS(tuple, i) = read;
8889 }
8890 return decompose_with_tuple(state, ins, tuple);
8891}
8892
Stefan Reinauer14e22772010-04-27 06:56:47 +00008893static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008894 struct triple *ins)
8895{
8896 struct triple *tuple, *lval, *val;
8897 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008898
Eric Biederman90089602004-05-28 14:11:54 +00008899 lval = MISC(ins, 0);
8900 val = RHS(ins, 0);
8901 get_occurance(ins->occurance);
8902 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8903 ins->occurance);
8904
8905 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008906 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008907 {
8908 internal_error(state, ins, "lhs size inconsistency?");
8909 }
8910 for(i = 0; i < tuple->lhs; i++) {
8911 struct triple *piece, *write, *pval, *bitref;
8912 if ((i != 0) || !triple_is_def(state, lval)) {
8913 piece = LHS(lval, i);
8914 } else {
8915 piece = lval;
8916 }
8917 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8918 pval = val;
8919 }
8920 else {
8921 if (i > val->lhs) {
8922 internal_error(state, ins, "lhs size inconsistency?");
8923 }
8924 pval = LHS(val, i);
8925 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008926
Eric Biederman90089602004-05-28 14:11:54 +00008927 /* See if the piece is really a bitref */
8928 bitref = 0;
8929 if (piece->op == OP_BITREF) {
8930 struct triple *read, *deposit;
8931 bitref = piece;
8932 piece = RHS(bitref, 0);
8933
8934 /* Read the destination register */
8935 get_occurance(tuple->occurance);
8936 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8937 tuple->occurance);
8938 RHS(read, 0) = piece;
8939
8940 /* Deposit the new bitfield value */
8941 get_occurance(tuple->occurance);
8942 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8943 tuple->occurance);
8944 RHS(deposit, 0) = read;
8945 RHS(deposit, 1) = pval;
8946 deposit->u.bitfield.size = bitref->u.bitfield.size;
8947 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8948
8949 /* Now write the newly generated value */
8950 pval = deposit;
8951 }
8952
8953 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008954 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008955 tuple->occurance);
8956 MISC(write, 0) = piece;
8957 RHS(write, 0) = pval;
8958 LHS(tuple, i) = write;
8959 }
8960 return decompose_with_tuple(state, ins, tuple);
8961}
8962
8963struct decompose_load_info {
8964 struct occurance *occurance;
8965 struct triple *lval;
8966 struct triple *tuple;
8967};
8968static void decompose_load_cb(struct compile_state *state,
8969 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8970{
8971 struct decompose_load_info *info = arg;
8972 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008973
Eric Biederman90089602004-05-28 14:11:54 +00008974 if (reg_offset > info->tuple->lhs) {
8975 internal_error(state, info->tuple, "lhs to small?");
8976 }
8977 get_occurance(info->occurance);
8978 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8979 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8980 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8981}
8982
Stefan Reinauer14e22772010-04-27 06:56:47 +00008983static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008984 struct triple *ins)
8985{
8986 struct triple *tuple;
8987 struct decompose_load_info info;
8988
8989 if (!is_compound_type(ins->type)) {
8990 return ins->next;
8991 }
8992 get_occurance(ins->occurance);
8993 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8994 ins->occurance);
8995
8996 info.occurance = ins->occurance;
8997 info.lval = RHS(ins, 0);
8998 info.tuple = tuple;
8999 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9000
9001 return decompose_with_tuple(state, ins, tuple);
9002}
9003
9004
9005struct decompose_store_info {
9006 struct occurance *occurance;
9007 struct triple *lval;
9008 struct triple *val;
9009 struct triple *tuple;
9010};
9011static void decompose_store_cb(struct compile_state *state,
9012 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9013{
9014 struct decompose_store_info *info = arg;
9015 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009016
Eric Biederman90089602004-05-28 14:11:54 +00009017 if (reg_offset > info->tuple->lhs) {
9018 internal_error(state, info->tuple, "lhs to small?");
9019 }
9020 get_occurance(info->occurance);
9021 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9022 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9023 RHS(store, 1) = LHS(info->val, reg_offset);
9024 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9025}
9026
Stefan Reinauer14e22772010-04-27 06:56:47 +00009027static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009028 struct triple *ins)
9029{
9030 struct triple *tuple;
9031 struct decompose_store_info info;
9032
9033 if (!is_compound_type(ins->type)) {
9034 return ins->next;
9035 }
9036 get_occurance(ins->occurance);
9037 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9038 ins->occurance);
9039
9040 info.occurance = ins->occurance;
9041 info.lval = RHS(ins, 0);
9042 info.val = RHS(ins, 1);
9043 info.tuple = tuple;
9044 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9045
9046 return decompose_with_tuple(state, ins, tuple);
9047}
9048
Stefan Reinauer14e22772010-04-27 06:56:47 +00009049static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009050 struct triple *ins)
9051{
9052 struct triple *tuple, *lval;
9053 struct type *type;
9054 size_t reg_offset;
9055 int i, idx;
9056
9057 lval = MISC(ins, 0);
9058 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9059 idx = reg_offset/REG_SIZEOF_REG;
9060 type = field_type(state, lval->type, ins->u.field);
9061#if DEBUG_DECOMPOSE_HIRES
9062 {
9063 FILE *fp = state->dbgout;
9064 fprintf(fp, "field type: ");
9065 name_of(fp, type);
9066 fprintf(fp, "\n");
9067 }
9068#endif
9069
9070 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009071 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009072 ins->occurance);
9073
9074 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9075 (tuple->lhs != 1))
9076 {
9077 internal_error(state, ins, "multi register bitfield?");
9078 }
9079
9080 for(i = 0; i < tuple->lhs; i++, idx++) {
9081 struct triple *piece;
9082 if (!triple_is_def(state, lval)) {
9083 if (idx > lval->lhs) {
9084 internal_error(state, ins, "inconsistent lhs count");
9085 }
9086 piece = LHS(lval, idx);
9087 } else {
9088 if (idx != 0) {
9089 internal_error(state, ins, "bad reg_offset into def");
9090 }
9091 if (i != 0) {
9092 internal_error(state, ins, "bad reg count from def");
9093 }
9094 piece = lval;
9095 }
9096
9097 /* Remember the offset of the bitfield */
9098 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9099 get_occurance(ins->occurance);
9100 piece = build_triple(state, OP_BITREF, type, piece, 0,
9101 ins->occurance);
9102 piece->u.bitfield.size = size_of(state, type);
9103 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9104 }
9105 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009106 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009107 "request for a nonbitfield sub register?");
9108 }
9109
9110 LHS(tuple, i) = piece;
9111 }
9112
9113 return decompose_with_tuple(state, ins, tuple);
9114}
9115
Stefan Reinauer14e22772010-04-27 06:56:47 +00009116static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009117 struct triple *ins)
9118{
9119 struct triple *tuple, *lval;
9120 struct type *type;
9121 int i, idx;
9122
9123 lval = MISC(ins, 0);
9124 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9125 type = index_type(state, lval->type, ins->u.cval);
9126#if DEBUG_DECOMPOSE_HIRES
9127{
9128 FILE *fp = state->dbgout;
9129 fprintf(fp, "index type: ");
9130 name_of(fp, type);
9131 fprintf(fp, "\n");
9132}
9133#endif
9134
9135 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009136 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009137 ins->occurance);
9138
9139 for(i = 0; i < tuple->lhs; i++, idx++) {
9140 struct triple *piece;
9141 if (!triple_is_def(state, lval)) {
9142 if (idx > lval->lhs) {
9143 internal_error(state, ins, "inconsistent lhs count");
9144 }
9145 piece = LHS(lval, idx);
9146 } else {
9147 if (idx != 0) {
9148 internal_error(state, ins, "bad reg_offset into def");
9149 }
9150 if (i != 0) {
9151 internal_error(state, ins, "bad reg count from def");
9152 }
9153 piece = lval;
9154 }
9155 LHS(tuple, i) = piece;
9156 }
9157
9158 return decompose_with_tuple(state, ins, tuple);
9159}
9160
9161static void decompose_compound_types(struct compile_state *state)
9162{
9163 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009164 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009165 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009166
9167 /* Pass one expand compound values into pseudo registers.
9168 */
9169 next = first;
9170 do {
9171 ins = next;
9172 next = ins->next;
9173 switch(ins->op) {
9174 case OP_UNKNOWNVAL:
9175 next = decompose_unknownval(state, ins);
9176 break;
9177
9178 case OP_READ:
9179 next = decompose_read(state, ins);
9180 break;
9181
9182 case OP_WRITE:
9183 next = decompose_write(state, ins);
9184 break;
9185
9186
9187 /* Be very careful with the load/store logic. These
9188 * operations must convert from the in register layout
9189 * to the in memory layout, which is nontrivial.
9190 */
9191 case OP_LOAD:
9192 next = decompose_load(state, ins);
9193 break;
9194 case OP_STORE:
9195 next = decompose_store(state, ins);
9196 break;
9197
9198 case OP_DOT:
9199 next = decompose_dot(state, ins);
9200 break;
9201 case OP_INDEX:
9202 next = decompose_index(state, ins);
9203 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009204
Eric Biederman90089602004-05-28 14:11:54 +00009205 }
9206#if DEBUG_DECOMPOSE_HIRES
9207 fprintf(fp, "decompose next: %p \n", next);
9208 fflush(fp);
9209 fprintf(fp, "next->op: %d %s\n",
9210 next->op, tops(next->op));
9211 /* High resolution debugging mode */
9212 print_triples(state);
9213#endif
9214 } while (next != first);
9215
9216 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009217 */
9218 ins = first;
9219 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009220 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009221 if (ins->op == OP_TUPLE) {
9222 if (ins->use) {
9223 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009224 }
Eric Biederman90089602004-05-28 14:11:54 +00009225 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009226 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009227 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009228 }
Eric Biederman90089602004-05-28 14:11:54 +00009229 ins = next;
9230 } while(ins != first);
9231 ins = first;
9232 do {
9233 next = ins->next;
9234 if (ins->op == OP_BITREF) {
9235 if (ins->use) {
9236 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +00009237 }
Eric Biederman90089602004-05-28 14:11:54 +00009238 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009239 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009240 }
9241 }
9242 ins = next;
9243 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009244
Eric Biederman0babc1c2003-05-09 02:39:00 +00009245 /* Pass three verify the state and set ->id to 0.
9246 */
Eric Biederman90089602004-05-28 14:11:54 +00009247 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009248 do {
Eric Biederman90089602004-05-28 14:11:54 +00009249 ins = next;
9250 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009251 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009252 if (triple_stores_block(state, ins)) {
9253 ins->u.block = 0;
9254 }
9255 if (triple_is_def(state, ins)) {
9256 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9257 internal_error(state, ins, "multi register value remains?");
9258 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009259 }
9260 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009261 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009262 }
Eric Biederman90089602004-05-28 14:11:54 +00009263 if (ins->op == OP_INDEX) {
9264 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009265 }
Eric Biederman90089602004-05-28 14:11:54 +00009266 if (ins->op == OP_BITREF) {
9267 internal_error(state, ins, "OP_BITREF remains?");
9268 }
9269 if (ins->op == OP_TUPLE) {
9270 internal_error(state, ins, "OP_TUPLE remains?");
9271 }
9272 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009273}
9274
Eric Biedermanb138ac82003-04-22 18:44:01 +00009275/* For those operations that cannot be simplified */
9276static void simplify_noop(struct compile_state *state, struct triple *ins)
9277{
9278 return;
9279}
9280
9281static void simplify_smul(struct compile_state *state, struct triple *ins)
9282{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009283 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009284 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009285 tmp = RHS(ins, 0);
9286 RHS(ins, 0) = RHS(ins, 1);
9287 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009289 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009290 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009291 left = read_sconst(state, ins, RHS(ins, 0));
9292 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009293 mkconst(state, ins, left * right);
9294 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009295 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009296 mkconst(state, ins, 0);
9297 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 else if (is_one(RHS(ins, 1))) {
9299 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009300 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009301 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009303 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009305 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009306 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009307 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009308 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 }
9310}
9311
9312static void simplify_umul(struct compile_state *state, struct triple *ins)
9313{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009315 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009316 tmp = RHS(ins, 0);
9317 RHS(ins, 0) = RHS(ins, 1);
9318 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 }
Eric Biederman90089602004-05-28 14:11:54 +00009320 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009321 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009322 left = read_const(state, ins, RHS(ins, 0));
9323 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009324 mkconst(state, ins, left * right);
9325 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009326 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009327 mkconst(state, ins, 0);
9328 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009329 else if (is_one(RHS(ins, 1))) {
9330 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009331 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009332 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009334 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009335 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009336 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009337 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009338 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009339 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009340 }
9341}
9342
9343static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9344{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009345 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009347 left = read_sconst(state, ins, RHS(ins, 0));
9348 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009349 mkconst(state, ins, left / right);
9350 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009351 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009352 mkconst(state, ins, 0);
9353 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009354 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009355 error(state, ins, "division by zero");
9356 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009357 else if (is_one(RHS(ins, 1))) {
9358 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009359 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009360 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009362 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009364 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009365 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009366 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009367 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 }
9369}
9370
9371static void simplify_udiv(struct compile_state *state, struct triple *ins)
9372{
Eric Biederman90089602004-05-28 14:11:54 +00009373 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009374 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009375 left = read_const(state, ins, RHS(ins, 0));
9376 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 mkconst(state, ins, left / right);
9378 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009379 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009380 mkconst(state, ins, 0);
9381 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009382 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009383 error(state, ins, "division by zero");
9384 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009385 else if (is_one(RHS(ins, 1))) {
9386 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009387 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009388 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009390 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009391 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009392 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009393 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009394 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009395 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 }
9397}
9398
9399static void simplify_smod(struct compile_state *state, struct triple *ins)
9400{
Eric Biederman90089602004-05-28 14:11:54 +00009401 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009403 left = read_const(state, ins, RHS(ins, 0));
9404 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009405 mkconst(state, ins, left % right);
9406 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009407 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009408 mkconst(state, ins, 0);
9409 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009410 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009411 error(state, ins, "division by zero");
9412 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009413 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 mkconst(state, ins, 0);
9415 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009418 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009419 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009420 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009421 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009423 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009424 }
9425}
Eric Biederman83b991a2003-10-11 06:20:25 +00009426
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427static void simplify_umod(struct compile_state *state, struct triple *ins)
9428{
Eric Biederman90089602004-05-28 14:11:54 +00009429 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009431 left = read_const(state, ins, RHS(ins, 0));
9432 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 mkconst(state, ins, left % right);
9434 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009435 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009436 mkconst(state, ins, 0);
9437 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009438 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009439 error(state, ins, "division by zero");
9440 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009441 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 mkconst(state, ins, 0);
9443 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009444 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009448 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009449 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009450 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009451 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009452 }
9453}
9454
9455static void simplify_add(struct compile_state *state, struct triple *ins)
9456{
9457 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009459 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009460 tmp = RHS(ins, 0);
9461 RHS(ins, 0) = RHS(ins, 1);
9462 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009463 }
Eric Biederman90089602004-05-28 14:11:54 +00009464 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009465 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009466 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009467 left = read_const(state, ins, RHS(ins, 0));
9468 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009469 mkconst(state, ins, left + right);
9470 }
Eric Biederman530b5192003-07-01 10:05:30 +00009471 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 struct triple *sdecl;
9473 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009474 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009475 left = RHS(ins, 0)->u.cval;
9476 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009477 mkaddr_const(state, ins, sdecl, left + right);
9478 }
Eric Biederman530b5192003-07-01 10:05:30 +00009479 else {
9480 internal_warning(state, ins, "Optimize me!");
9481 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009482 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009483 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009484 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009485 tmp = RHS(ins, 1);
9486 RHS(ins, 1) = RHS(ins, 0);
9487 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009488 }
9489}
9490
9491static void simplify_sub(struct compile_state *state, struct triple *ins)
9492{
Eric Biederman90089602004-05-28 14:11:54 +00009493 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009494 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009495 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009496 left = read_const(state, ins, RHS(ins, 0));
9497 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009498 mkconst(state, ins, left - right);
9499 }
Eric Biederman530b5192003-07-01 10:05:30 +00009500 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009501 struct triple *sdecl;
9502 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009503 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009504 left = RHS(ins, 0)->u.cval;
9505 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009506 mkaddr_const(state, ins, sdecl, left - right);
9507 }
Eric Biederman530b5192003-07-01 10:05:30 +00009508 else {
9509 internal_warning(state, ins, "Optimize me!");
9510 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009511 }
9512}
9513
9514static void simplify_sl(struct compile_state *state, struct triple *ins)
9515{
Eric Biederman90089602004-05-28 14:11:54 +00009516 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009517 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009518 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009519 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009520 warning(state, ins, "left shift count >= width of type");
9521 }
9522 }
Eric Biederman90089602004-05-28 14:11:54 +00009523 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009524 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009525 left = read_const(state, ins, RHS(ins, 0));
9526 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009527 mkconst(state, ins, left << right);
9528 }
9529}
9530
9531static void simplify_usr(struct compile_state *state, struct triple *ins)
9532{
Eric Biederman90089602004-05-28 14:11:54 +00009533 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009534 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009535 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009536 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009537 warning(state, ins, "right shift count >= width of type");
9538 }
9539 }
Eric Biederman90089602004-05-28 14:11:54 +00009540 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009541 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009542 left = read_const(state, ins, RHS(ins, 0));
9543 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009544 mkconst(state, ins, left >> right);
9545 }
9546}
9547
9548static void simplify_ssr(struct compile_state *state, struct triple *ins)
9549{
Eric Biederman90089602004-05-28 14:11:54 +00009550 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009551 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009552 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009553 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009554 warning(state, ins, "right shift count >= width of type");
9555 }
9556 }
Eric Biederman90089602004-05-28 14:11:54 +00009557 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009558 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009559 left = read_sconst(state, ins, RHS(ins, 0));
9560 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009561 mkconst(state, ins, left >> right);
9562 }
9563}
9564
9565static void simplify_and(struct compile_state *state, struct triple *ins)
9566{
Eric Biederman90089602004-05-28 14:11:54 +00009567 struct triple *left, *right;
9568 left = RHS(ins, 0);
9569 right = RHS(ins, 1);
9570
9571 if (is_simple_const(left) && is_simple_const(right)) {
9572 ulong_t lval, rval;
9573 lval = read_const(state, ins, left);
9574 rval = read_const(state, ins, right);
9575 mkconst(state, ins, lval & rval);
9576 }
9577 else if (is_zero(right) || is_zero(left)) {
9578 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009579 }
9580}
9581
9582static void simplify_or(struct compile_state *state, struct triple *ins)
9583{
Eric Biederman90089602004-05-28 14:11:54 +00009584 struct triple *left, *right;
9585 left = RHS(ins, 0);
9586 right = RHS(ins, 1);
9587
9588 if (is_simple_const(left) && is_simple_const(right)) {
9589 ulong_t lval, rval;
9590 lval = read_const(state, ins, left);
9591 rval = read_const(state, ins, right);
9592 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009593 }
Eric Biederman90089602004-05-28 14:11:54 +00009594#if 0 /* I need to handle type mismatches here... */
9595 else if (is_zero(right)) {
9596 mkcopy(state, ins, left);
9597 }
9598 else if (is_zero(left)) {
9599 mkcopy(state, ins, right);
9600 }
9601#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009602}
9603
9604static void simplify_xor(struct compile_state *state, struct triple *ins)
9605{
Eric Biederman90089602004-05-28 14:11:54 +00009606 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009607 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009608 left = read_const(state, ins, RHS(ins, 0));
9609 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009610 mkconst(state, ins, left ^ right);
9611 }
9612}
9613
9614static void simplify_pos(struct compile_state *state, struct triple *ins)
9615{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009616 if (is_const(RHS(ins, 0))) {
9617 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009618 }
9619 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009620 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009621 }
9622}
9623
9624static void simplify_neg(struct compile_state *state, struct triple *ins)
9625{
Eric Biederman90089602004-05-28 14:11:54 +00009626 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009628 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009629 mkconst(state, ins, -left);
9630 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009631 else if (RHS(ins, 0)->op == OP_NEG) {
9632 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009633 }
9634}
9635
9636static void simplify_invert(struct compile_state *state, struct triple *ins)
9637{
Eric Biederman90089602004-05-28 14:11:54 +00009638 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009639 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009640 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009641 mkconst(state, ins, ~left);
9642 }
9643}
9644
9645static void simplify_eq(struct compile_state *state, struct triple *ins)
9646{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009647 struct triple *left, *right;
9648 left = RHS(ins, 0);
9649 right = RHS(ins, 1);
9650
9651 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009652 int val;
9653 val = const_eq(state, ins, left, right);
9654 if (val >= 0) {
9655 mkconst(state, ins, val == 1);
9656 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009657 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009658 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009659 mkconst(state, ins, 1);
9660 }
9661}
9662
9663static void simplify_noteq(struct compile_state *state, struct triple *ins)
9664{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009665 struct triple *left, *right;
9666 left = RHS(ins, 0);
9667 right = RHS(ins, 1);
9668
9669 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009670 int val;
9671 val = const_eq(state, ins, left, right);
9672 if (val >= 0) {
9673 mkconst(state, ins, val != 1);
9674 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009675 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009676 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009677 mkconst(state, ins, 0);
9678 }
9679}
9680
9681static void simplify_sless(struct compile_state *state, struct triple *ins)
9682{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009683 struct triple *left, *right;
9684 left = RHS(ins, 0);
9685 right = RHS(ins, 1);
9686
9687 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009688 int val;
9689 val = const_scmp(state, ins, left, right);
9690 if ((val >= -1) && (val <= 1)) {
9691 mkconst(state, ins, val < 0);
9692 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009693 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009694 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009695 mkconst(state, ins, 0);
9696 }
9697}
9698
9699static void simplify_uless(struct compile_state *state, struct triple *ins)
9700{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009701 struct triple *left, *right;
9702 left = RHS(ins, 0);
9703 right = RHS(ins, 1);
9704
9705 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009706 int val;
9707 val = const_ucmp(state, ins, left, right);
9708 if ((val >= -1) && (val <= 1)) {
9709 mkconst(state, ins, val < 0);
9710 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009711 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009712 else if (is_zero(right)) {
9713 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009714 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009715 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009716 mkconst(state, ins, 0);
9717 }
9718}
9719
9720static void simplify_smore(struct compile_state *state, struct triple *ins)
9721{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009722 struct triple *left, *right;
9723 left = RHS(ins, 0);
9724 right = RHS(ins, 1);
9725
9726 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009727 int val;
9728 val = const_scmp(state, ins, left, right);
9729 if ((val >= -1) && (val <= 1)) {
9730 mkconst(state, ins, val > 0);
9731 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009732 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009733 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009734 mkconst(state, ins, 0);
9735 }
9736}
9737
9738static void simplify_umore(struct compile_state *state, struct triple *ins)
9739{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009740 struct triple *left, *right;
9741 left = RHS(ins, 0);
9742 right = RHS(ins, 1);
9743
9744 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009745 int val;
9746 val = const_ucmp(state, ins, left, right);
9747 if ((val >= -1) && (val <= 1)) {
9748 mkconst(state, ins, val > 0);
9749 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009750 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009751 else if (is_zero(left)) {
9752 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009753 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009754 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009755 mkconst(state, ins, 0);
9756 }
9757}
9758
9759
9760static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9761{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009762 struct triple *left, *right;
9763 left = RHS(ins, 0);
9764 right = RHS(ins, 1);
9765
9766 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009767 int val;
9768 val = const_scmp(state, ins, left, right);
9769 if ((val >= -1) && (val <= 1)) {
9770 mkconst(state, ins, val <= 0);
9771 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009772 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009773 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009774 mkconst(state, ins, 1);
9775 }
9776}
9777
9778static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9779{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009780 struct triple *left, *right;
9781 left = RHS(ins, 0);
9782 right = RHS(ins, 1);
9783
9784 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009785 int val;
9786 val = const_ucmp(state, ins, left, right);
9787 if ((val >= -1) && (val <= 1)) {
9788 mkconst(state, ins, val <= 0);
9789 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009790 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009791 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009792 mkconst(state, ins, 1);
9793 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009794 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009795 mkconst(state, ins, 1);
9796 }
9797}
9798
9799static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9800{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009801 struct triple *left, *right;
9802 left = RHS(ins, 0);
9803 right = RHS(ins, 1);
9804
9805 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009806 int val;
9807 val = const_scmp(state, ins, left, right);
9808 if ((val >= -1) && (val <= 1)) {
9809 mkconst(state, ins, val >= 0);
9810 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009811 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009812 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009813 mkconst(state, ins, 1);
9814 }
9815}
9816
9817static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9818{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009819 struct triple *left, *right;
9820 left = RHS(ins, 0);
9821 right = RHS(ins, 1);
9822
9823 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009824 int val;
9825 val = const_ucmp(state, ins, left, right);
9826 if ((val >= -1) && (val <= 1)) {
9827 mkconst(state, ins, val >= 0);
9828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009829 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009830 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009831 mkconst(state, ins, 1);
9832 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009833 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009834 mkconst(state, ins, 1);
9835 }
9836}
9837
9838static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9839{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009840 struct triple *rhs;
9841 rhs = RHS(ins, 0);
9842
9843 if (is_const(rhs)) {
9844 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009845 }
9846 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009847 else if ((rhs->use) &&
9848 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009849 int need_copy = 1;
9850 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009851 switch(rhs->op) {
9852 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9853 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9854 case OP_EQ: rhs->op = OP_NOTEQ; break;
9855 case OP_NOTEQ: rhs->op = OP_EQ; break;
9856 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9857 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9858 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9859 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9860 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9861 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9862 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9863 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009864 default:
9865 need_copy = 0;
9866 break;
9867 }
9868 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009869 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009870 }
9871 }
9872}
9873
9874static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9875{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009876 struct triple *rhs;
9877 rhs = RHS(ins, 0);
9878
9879 if (is_const(rhs)) {
9880 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009881 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009882 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009883 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9884 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9885 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009886 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009887 }
9888
9889}
9890
Eric Biederman90089602004-05-28 14:11:54 +00009891static void simplify_load(struct compile_state *state, struct triple *ins)
9892{
9893 struct triple *addr, *sdecl, *blob;
9894
9895 /* If I am doing a load with a constant pointer from a constant
9896 * table get the value.
9897 */
9898 addr = RHS(ins, 0);
9899 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9900 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9901 (blob->op == OP_BLOBCONST)) {
9902 unsigned char buffer[SIZEOF_WORD];
9903 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009904 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009905 ulong_t val;
9906 reg_size = reg_size_of(state, ins->type);
9907 if (reg_size > REG_SIZEOF_REG) {
9908 internal_error(state, ins, "load size greater than register");
9909 }
9910 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009911 end = blob->u.blob;
9912 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009913 src = blob->u.blob;
9914 src += addr->u.cval;
9915
Eric Biederman7dea9552004-06-29 05:38:37 +00009916 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009917 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009918 }
9919
Eric Biederman90089602004-05-28 14:11:54 +00009920 memset(buffer, 0, sizeof(buffer));
9921 memcpy(buffer, src, bits_to_bytes(mem_size));
9922
9923 switch(mem_size) {
9924 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9925 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9926 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9927 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9928 default:
9929 internal_error(state, ins, "mem_size: %d not handled",
9930 mem_size);
9931 val = 0;
9932 break;
9933 }
9934 mkconst(state, ins, val);
9935 }
9936}
9937
9938static void simplify_uextract(struct compile_state *state, struct triple *ins)
9939{
9940 if (is_simple_const(RHS(ins, 0))) {
9941 ulong_t val;
9942 ulong_t mask;
9943 val = read_const(state, ins, RHS(ins, 0));
9944 mask = 1;
9945 mask <<= ins->u.bitfield.size;
9946 mask -= 1;
9947 val >>= ins->u.bitfield.offset;
9948 val &= mask;
9949 mkconst(state, ins, val);
9950 }
9951}
9952
9953static void simplify_sextract(struct compile_state *state, struct triple *ins)
9954{
9955 if (is_simple_const(RHS(ins, 0))) {
9956 ulong_t val;
9957 ulong_t mask;
9958 long_t sval;
9959 val = read_const(state, ins, RHS(ins, 0));
9960 mask = 1;
9961 mask <<= ins->u.bitfield.size;
9962 mask -= 1;
9963 val >>= ins->u.bitfield.offset;
9964 val &= mask;
9965 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9966 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009967 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009968 mkconst(state, ins, sval);
9969 }
9970}
9971
9972static void simplify_deposit(struct compile_state *state, struct triple *ins)
9973{
9974 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9975 ulong_t targ, val;
9976 ulong_t mask;
9977 targ = read_const(state, ins, RHS(ins, 0));
9978 val = read_const(state, ins, RHS(ins, 1));
9979 mask = 1;
9980 mask <<= ins->u.bitfield.size;
9981 mask -= 1;
9982 mask <<= ins->u.bitfield.offset;
9983 targ &= ~mask;
9984 val <<= ins->u.bitfield.offset;
9985 val &= mask;
9986 targ |= val;
9987 mkconst(state, ins, targ);
9988 }
9989}
9990
Eric Biedermanb138ac82003-04-22 18:44:01 +00009991static void simplify_copy(struct compile_state *state, struct triple *ins)
9992{
Eric Biederman90089602004-05-28 14:11:54 +00009993 struct triple *right;
9994 right = RHS(ins, 0);
9995 if (is_subset_type(ins->type, right->type)) {
9996 ins->type = right->type;
9997 }
9998 if (equiv_types(ins->type, right->type)) {
9999 ins->op = OP_COPY;/* I don't need to convert if the types match */
10000 } else {
10001 if (ins->op == OP_COPY) {
10002 internal_error(state, ins, "type mismatch on copy");
10003 }
10004 }
10005 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10006 struct triple *sdecl;
10007 ulong_t offset;
10008 sdecl = MISC(right, 0);
10009 offset = right->u.cval;
10010 mkaddr_const(state, ins, sdecl, offset);
10011 }
10012 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10013 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010014 case OP_INTCONST:
10015 {
10016 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010017 left = read_const(state, ins, right);
10018 /* Ensure I have not overflowed the destination. */
10019 if (size_of(state, right->type) > size_of(state, ins->type)) {
10020 ulong_t mask;
10021 mask = 1;
10022 mask <<= size_of(state, ins->type);
10023 mask -= 1;
10024 left &= mask;
10025 }
10026 /* Ensure I am properly sign extended */
10027 if (size_of(state, right->type) < size_of(state, ins->type) &&
10028 is_signed(right->type)) {
10029 long_t val;
10030 int shift;
10031 shift = SIZEOF_LONG - size_of(state, right->type);
10032 val = left;
10033 val <<= shift;
10034 val >>= shift;
10035 left = val;
10036 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010037 mkconst(state, ins, left);
10038 break;
10039 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010040 default:
10041 internal_error(state, ins, "uknown constant");
10042 break;
10043 }
10044 }
10045}
10046
Eric Biederman83b991a2003-10-11 06:20:25 +000010047static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010048{
10049 struct triple *ptr;
10050 if (!block) {
10051 return 0;
10052 }
10053 ptr = block->first;
10054 do {
10055 if (ptr->op == OP_PHI) {
10056 return 1;
10057 }
10058 ptr = ptr->next;
10059 } while(ptr != block->last);
10060 return 0;
10061}
10062
Eric Biederman83b991a2003-10-11 06:20:25 +000010063static int phi_dependency(struct block *block)
10064{
10065 /* A block has a phi dependency if a phi function
10066 * depends on that block to exist, and makes a block
10067 * that is otherwise useless unsafe to remove.
10068 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010069 if (block) {
10070 struct block_set *edge;
10071 for(edge = block->edges; edge; edge = edge->next) {
10072 if (phi_present(edge->member)) {
10073 return 1;
10074 }
10075 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010076 }
10077 return 0;
10078}
10079
10080static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10081{
10082 struct triple *targ;
10083 targ = TARG(ins, 0);
10084 /* During scc_transform temporary triples are allocated that
10085 * loop back onto themselves. If I see one don't advance the
10086 * target.
10087 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010088 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010089 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010090 targ = targ->next;
10091 }
10092 return targ;
10093}
10094
10095
10096static void simplify_branch(struct compile_state *state, struct triple *ins)
10097{
Eric Biederman90089602004-05-28 14:11:54 +000010098 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010099 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010100 internal_error(state, ins, "not branch");
10101 }
10102 if (ins->use != 0) {
10103 internal_error(state, ins, "branch use");
10104 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010105 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010106 * make modifications to the control flow graph as well
10107 * as to the branch instruction itself. That is handled
10108 * by rebuilding the basic blocks after simplify all is called.
10109 */
10110
10111 /* If we have a branch to an unconditional branch update
10112 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010113 * functions.
10114 * Also only do this a limited number of times so
10115 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010116 */
Eric Biederman90089602004-05-28 14:11:54 +000010117 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010118 do {
10119 struct triple *targ;
10120 simplified = 0;
10121 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010122 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010123 !phi_dependency(targ->u.block))
10124 {
10125 unuse_triple(TARG(ins, 0), ins);
10126 TARG(ins, 0) = TARG(targ, 0);
10127 use_triple(TARG(ins, 0), ins);
10128 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010129 }
Eric Biederman90089602004-05-28 14:11:54 +000010130 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010131
10132 /* If we have a conditional branch with a constant condition
10133 * make it an unconditional branch.
10134 */
Eric Biederman90089602004-05-28 14:11:54 +000010135 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010136 struct triple *targ;
10137 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010138 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010139 unuse_triple(RHS(ins, 0), ins);
10140 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010141 ins->rhs = 0;
10142 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010143 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010144 if (value) {
10145 unuse_triple(ins->next, ins);
10146 TARG(ins, 0) = targ;
10147 }
10148 else {
10149 unuse_triple(targ, ins);
10150 TARG(ins, 0) = ins->next;
10151 }
10152 }
Eric Biederman90089602004-05-28 14:11:54 +000010153
10154 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010155 * make it a noop.
10156 */
10157 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010158 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010159 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010160 unuse_triple(RHS(ins, 0), ins);
10161 unuse_triple(ins->next, ins);
10162 }
Eric Biederman90089602004-05-28 14:11:54 +000010163 ins->lhs = 0;
10164 ins->rhs = 0;
10165 ins->misc = 0;
10166 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010167 ins->op = OP_NOOP;
10168 if (ins->use) {
10169 internal_error(state, ins, "noop use != 0");
10170 }
10171 }
10172}
10173
Eric Biederman530b5192003-07-01 10:05:30 +000010174static void simplify_label(struct compile_state *state, struct triple *ins)
10175{
Eric Biederman83b991a2003-10-11 06:20:25 +000010176 /* Ignore volatile labels */
10177 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010178 return;
10179 }
10180 if (ins->use == 0) {
10181 ins->op = OP_NOOP;
10182 }
10183 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010184 /* In general it is not safe to merge one label that
10185 * imediately follows another. The problem is that the empty
10186 * looking block may have phi functions that depend on it.
10187 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010188 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010189 struct triple_set *user, *next;
10190 ins->op = OP_NOOP;
10191 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010192 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010193 next = user->next;
10194 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010195 expr = triple_targ(state, use, 0);
10196 for(;expr; expr = triple_targ(state, use, expr)) {
10197 if (*expr == ins) {
10198 *expr = ins->prev;
10199 unuse_triple(ins, use);
10200 use_triple(ins->prev, use);
10201 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010202
Eric Biederman530b5192003-07-01 10:05:30 +000010203 }
10204 }
10205 if (ins->use) {
10206 internal_error(state, ins, "noop use != 0");
10207 }
10208 }
10209 }
10210}
10211
Eric Biedermanb138ac82003-04-22 18:44:01 +000010212static void simplify_phi(struct compile_state *state, struct triple *ins)
10213{
Eric Biederman83b991a2003-10-11 06:20:25 +000010214 struct triple **slot;
10215 struct triple *value;
10216 int zrhs, i;
10217 ulong_t cvalue;
10218 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010219 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010220 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010221 return;
10222 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010223 /* See if all of the rhs members of a phi have the same value */
10224 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010225 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010226 for(i = 1; i < zrhs; i++) {
10227 if ( !slot[i] ||
10228 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010229 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010230 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010231 break;
10232 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010233 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010234 if (i == zrhs) {
10235 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010236 return;
10237 }
10238 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010239
Eric Biederman83b991a2003-10-11 06:20:25 +000010240 /* See if all of rhs members of a phi are the same */
10241 value = slot[0];
10242 for(i = 1; i < zrhs; i++) {
10243 if (slot[i] != value) {
10244 break;
10245 }
10246 }
10247 if (i == zrhs) {
10248 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010249 if (!is_subset_type(ins->type, value->type)) {
10250 internal_error(state, ins, "bad input type to phi");
10251 }
10252 /* Make the types match */
10253 if (!equiv_types(ins->type, value->type)) {
10254 ins->type = value->type;
10255 }
10256 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010257 mkcopy(state, ins, value);
10258 return;
10259 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010260}
10261
10262
10263static void simplify_bsf(struct compile_state *state, struct triple *ins)
10264{
Eric Biederman90089602004-05-28 14:11:54 +000010265 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010266 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010267 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010268 mkconst(state, ins, bsf(left));
10269 }
10270}
10271
10272static void simplify_bsr(struct compile_state *state, struct triple *ins)
10273{
Eric Biederman90089602004-05-28 14:11:54 +000010274 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010275 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010276 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010277 mkconst(state, ins, bsr(left));
10278 }
10279}
10280
10281
10282typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010283static const struct simplify_table {
10284 simplify_t func;
10285 unsigned long flag;
10286} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010287#define simplify_sdivt simplify_noop
10288#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010289#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010290
Eric Biederman5ade04a2003-10-22 04:03:46 +000010291[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10292[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10293[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10294[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10295[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10296[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10297[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10298[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10299[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10300[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10301[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10302[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10303[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10304[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10305[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10306[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10307[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10308[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10309[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010310
Eric Biederman5ade04a2003-10-22 04:03:46 +000010311[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10312[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10318[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10319[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10320[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10321[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10322[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010323
Eric Biederman90089602004-05-28 14:11:54 +000010324[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010325[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010326
Eric Biederman90089602004-05-28 14:11:54 +000010327[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10328[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10329[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10330
Eric Biederman5ade04a2003-10-22 04:03:46 +000010331[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010332
Eric Biederman5ade04a2003-10-22 04:03:46 +000010333[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10334[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10335[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010336[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010337
Eric Biederman5ade04a2003-10-22 04:03:46 +000010338[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10339[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10340[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010341[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010342[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10343[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010344
Eric Biederman5ade04a2003-10-22 04:03:46 +000010345[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010346[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010347
Eric Biederman5ade04a2003-10-22 04:03:46 +000010348[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10349[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10350[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10351[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10352[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10353[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10354[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10355[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10356[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010357
Eric Biederman5ade04a2003-10-22 04:03:46 +000010358[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10359[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10360[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10361[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10362[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10363[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10364[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10365[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10366[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010367[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010368[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010369};
10370
Stefan Reinauer14e22772010-04-27 06:56:47 +000010371static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010372 simplify_t do_simplify, struct triple *ins)
10373{
10374#if DEBUG_SIMPLIFY_HIRES
10375 if (state->functions_joined && (do_simplify != simplify_noop)) {
10376 /* High resolution debugging mode */
10377 fprintf(state->dbgout, "simplifing: ");
10378 display_triple(state->dbgout, ins);
10379 }
10380#endif
10381 do_simplify(state, ins);
10382#if DEBUG_SIMPLIFY_HIRES
10383 if (state->functions_joined && (do_simplify != simplify_noop)) {
10384 /* High resolution debugging mode */
10385 fprintf(state->dbgout, "simplified: ");
10386 display_triple(state->dbgout, ins);
10387 }
10388#endif
10389}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010390static void simplify(struct compile_state *state, struct triple *ins)
10391{
10392 int op;
10393 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010394 if (ins == &unknown_triple) {
10395 internal_error(state, ins, "simplifying the unknown triple?");
10396 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010397 do {
10398 op = ins->op;
10399 do_simplify = 0;
Patrick Georgi0dde01c2013-11-11 15:57:09 +010010400 if ((op < 0) || (op >= sizeof(table_simplify)/sizeof(table_simplify[0]))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010401 do_simplify = 0;
10402 }
Eric Biederman90089602004-05-28 14:11:54 +000010403 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010404 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010405 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010406 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010407 !(state->compiler->flags & table_simplify[op].flag)) {
10408 do_simplify = simplify_noop;
10409 }
10410 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010411 do_simplify = simplify_noop;
10412 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010413
Eric Biedermanb138ac82003-04-22 18:44:01 +000010414 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010415 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010416 op, tops(op));
10417 return;
10418 }
Eric Biederman90089602004-05-28 14:11:54 +000010419 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010420 } while(ins->op != op);
10421}
10422
Eric Biederman5ade04a2003-10-22 04:03:46 +000010423static void rebuild_ssa_form(struct compile_state *state);
10424
Eric Biedermanb138ac82003-04-22 18:44:01 +000010425static void simplify_all(struct compile_state *state)
10426{
10427 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010428 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10429 return;
10430 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010431 first = state->first;
10432 ins = first->prev;
10433 do {
10434 simplify(state, ins);
10435 ins = ins->prev;
10436 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010437 ins = first;
10438 do {
10439 simplify(state, ins);
10440 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010441 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010442 rebuild_ssa_form(state);
10443
Eric Biederman90089602004-05-28 14:11:54 +000010444 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010445}
10446
10447/*
10448 * Builtins....
10449 * ============================
10450 */
10451
Eric Biederman0babc1c2003-05-09 02:39:00 +000010452static void register_builtin_function(struct compile_state *state,
10453 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454{
Eric Biederman90089602004-05-28 14:11:54 +000010455 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010456 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010457 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010458 struct file_state file;
10459 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010460 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010461 va_list args;
10462 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010463
10464 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010465 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010466 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010467 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010468 file.report_line = 1;
10469 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010470 file.prev = state->file;
10471 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010472 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010473
10474 /* Find the Parameter count */
10475 valid_op(state, op);
10476 parameters = table_ops[op].rhs;
10477 if (parameters < 0 ) {
10478 internal_error(state, 0, "Invalid builtin parameter count");
10479 }
10480
10481 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010482 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010483 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010484 next = &ftype->right;
10485 va_start(args, rtype);
10486 for(i = 0; i < parameters; i++) {
10487 atype = va_arg(args, struct type *);
10488 if (!*next) {
10489 *next = atype;
10490 } else {
10491 *next = new_type(TYPE_PRODUCT, *next, atype);
10492 next = &((*next)->right);
10493 }
10494 }
10495 if (!*next) {
10496 *next = &void_type;
10497 }
10498 va_end(args);
10499
Eric Biederman90089602004-05-28 14:11:54 +000010500 /* Get the initial closure type */
10501 ctype = new_type(TYPE_JOIN, &void_type, 0);
10502 ctype->elements = 1;
10503
10504 /* Get the return type */
10505 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10506 crtype->elements = 2;
10507
Eric Biedermanb138ac82003-04-22 18:44:01 +000010508 /* Generate the needed triples */
10509 def = triple(state, OP_LIST, ftype, 0, 0);
10510 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010511 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010512 result = flatten(state, first, variable(state, crtype));
10513 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010514 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010515
10516 /* Now string them together */
10517 param = ftype->right;
10518 for(i = 0; i < parameters; i++) {
10519 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10520 atype = param->left;
10521 } else {
10522 atype = param;
10523 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010524 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010525 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010526 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010527 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010528 generate_lhs_pieces(state, work);
10529 for(i = 0; i < parameters; i++) {
10530 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010531 }
Eric Biederman90089602004-05-28 14:11:54 +000010532 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10533 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010534 }
10535 work = flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010536 flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010537 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010538 name_len = strlen(name);
10539 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010540 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010541 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010542
Eric Biedermanb138ac82003-04-22 18:44:01 +000010543 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010544 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010545 state->main_function = 0;
10546
Eric Biederman5ade04a2003-10-22 04:03:46 +000010547 if (!state->functions) {
10548 state->functions = def;
10549 } else {
10550 insert_triple(state, state->functions, def);
10551 }
10552 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010553 FILE *fp = state->dbgout;
10554 fprintf(fp, "\n");
10555 loc(fp, state, 0);
10556 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10557 display_func(state, fp, def);
10558 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010559 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010560}
10561
Eric Biederman0babc1c2003-05-09 02:39:00 +000010562static struct type *partial_struct(struct compile_state *state,
10563 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010564{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010565 struct hash_entry *field_ident;
10566 struct type *result;
10567 int field_name_len;
10568
10569 field_name_len = strlen(field_name);
10570 field_ident = lookup(state, field_name, field_name_len);
10571
10572 result = clone_type(0, type);
10573 result->field_ident = field_ident;
10574
10575 if (rest) {
10576 result = new_type(TYPE_PRODUCT, result, rest);
10577 }
10578 return result;
10579}
10580
10581static struct type *register_builtin_type(struct compile_state *state,
10582 const char *name, struct type *type)
10583{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010584 struct hash_entry *ident;
10585 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010586
Eric Biedermanb138ac82003-04-22 18:44:01 +000010587 name_len = strlen(name);
10588 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010589
Eric Biederman0babc1c2003-05-09 02:39:00 +000010590 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10591 ulong_t elements = 0;
10592 struct type *field;
10593 type = new_type(TYPE_STRUCT, type, 0);
10594 field = type->left;
10595 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10596 elements++;
10597 field = field->right;
10598 }
10599 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010600 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010601 type->type_ident = ident;
10602 type->elements = elements;
10603 }
10604 symbol(state, ident, &ident->sym_ident, 0, type);
10605 ident->tok = TOK_TYPE_NAME;
10606 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010607}
10608
Eric Biederman0babc1c2003-05-09 02:39:00 +000010609
Eric Biedermanb138ac82003-04-22 18:44:01 +000010610static void register_builtins(struct compile_state *state)
10611{
Eric Biederman530b5192003-07-01 10:05:30 +000010612 struct type *div_type, *ldiv_type;
10613 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010614 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010615
Eric Biederman530b5192003-07-01 10:05:30 +000010616 div_type = register_builtin_type(state, "__builtin_div_t",
10617 partial_struct(state, "quot", &int_type,
10618 partial_struct(state, "rem", &int_type, 0)));
10619 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10620 partial_struct(state, "quot", &long_type,
10621 partial_struct(state, "rem", &long_type, 0)));
10622 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10623 partial_struct(state, "quot", &uint_type,
10624 partial_struct(state, "rem", &uint_type, 0)));
10625 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10626 partial_struct(state, "quot", &ulong_type,
10627 partial_struct(state, "rem", &ulong_type, 0)));
10628
10629 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10630 &int_type, &int_type);
10631 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10632 &long_type, &long_type);
10633 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10634 &uint_type, &uint_type);
10635 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10636 &ulong_type, &ulong_type);
10637
Stefan Reinauer14e22772010-04-27 06:56:47 +000010638 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010639 &ushort_type);
10640 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10641 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010642 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010643 &ushort_type);
10644
Stefan Reinauer14e22772010-04-27 06:56:47 +000010645 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010646 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010647 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010648 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010649 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010650 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010651
10652 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010653 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010654 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010655 &int_type);
10656
10657 msr_type = register_builtin_type(state, "__builtin_msr_t",
10658 partial_struct(state, "lo", &ulong_type,
10659 partial_struct(state, "hi", &ulong_type, 0)));
10660
10661 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10662 &ulong_type);
10663 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10664 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010665
10666 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010667 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010668}
10669
10670static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010671 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010672 struct hash_entry **ident, int need_ident);
10673static void decl(struct compile_state *state, struct triple *first);
10674static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010675#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010676static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010677#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010678static struct type *decl_specifiers(struct compile_state *state);
10679static int istype(int tok);
10680static struct triple *expr(struct compile_state *state);
10681static struct triple *assignment_expr(struct compile_state *state);
10682static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010683static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010684
10685static struct triple *call_expr(
10686 struct compile_state *state, struct triple *func)
10687{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010688 struct triple *def;
10689 struct type *param, *type;
10690 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010691
10692 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10693 error(state, 0, "Called object is not a function");
10694 }
10695 if (func->op != OP_LIST) {
10696 internal_error(state, 0, "improper function");
10697 }
10698 eat(state, TOK_LPAREN);
10699 /* Find the return type without any specifiers */
10700 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010701 /* Count the number of rhs entries for OP_FCALL */
10702 param = func->type->right;
10703 pvals = 0;
10704 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10705 pvals++;
10706 param = param->right;
10707 }
10708 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10709 pvals++;
10710 }
10711 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010712 MISC(def, 0) = func;
10713
10714 param = func->type->right;
10715 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010716 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010717 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010718 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010719 arg_type = param;
10720 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10721 arg_type = param->left;
10722 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010723 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010724 RHS(def, index) = val;
10725 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010726 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010727 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010728 }
10729 }
10730 eat(state, TOK_RPAREN);
10731 return def;
10732}
10733
10734
10735static struct triple *character_constant(struct compile_state *state)
10736{
10737 struct triple *def;
10738 struct token *tk;
10739 const signed char *str, *end;
10740 int c;
10741 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010742 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010743 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010744 str_len = tk->str_len - 2;
10745 if (str_len <= 0) {
10746 error(state, 0, "empty character constant");
10747 }
10748 end = str + str_len;
10749 c = char_value(state, &str, end);
10750 if (str != end) {
10751 error(state, 0, "multibyte character constant not supported");
10752 }
10753 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10754 return def;
10755}
10756
10757static struct triple *string_constant(struct compile_state *state)
10758{
10759 struct triple *def;
10760 struct token *tk;
10761 struct type *type;
10762 const signed char *str, *end;
10763 signed char *buf, *ptr;
10764 int str_len;
10765
10766 buf = 0;
10767 type = new_type(TYPE_ARRAY, &char_type, 0);
10768 type->elements = 0;
10769 /* The while loop handles string concatenation */
10770 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010771 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010772 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010773 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010774 if (str_len < 0) {
10775 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010776 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010777 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010778 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010779 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010780 end = str + str_len;
10781 ptr = buf;
10782 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10783 memcpy(buf, ptr, type->elements);
Patrick Georgif23cba02016-12-15 15:12:16 +010010784 free(ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010785 ptr = buf + type->elements;
10786 do {
10787 *ptr++ = char_value(state, &str, end);
10788 } while(str < end);
10789 type->elements = ptr - buf;
10790 } while(peek(state) == TOK_LIT_STRING);
10791 *ptr = '\0';
10792 type->elements += 1;
10793 def = triple(state, OP_BLOBCONST, type, 0, 0);
10794 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010795
Eric Biedermanb138ac82003-04-22 18:44:01 +000010796 return def;
10797}
10798
10799
10800static struct triple *integer_constant(struct compile_state *state)
10801{
10802 struct triple *def;
10803 unsigned long val;
10804 struct token *tk;
10805 char *end;
10806 int u, l, decimal;
10807 struct type *type;
10808
Eric Biederman41203d92004-11-08 09:31:09 +000010809 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010810 errno = 0;
10811 decimal = (tk->val.str[0] != '0');
10812 val = strtoul(tk->val.str, &end, 0);
Patrick Georgib6239b82014-08-09 19:42:08 +020010813 if (errno == ERANGE) {
10814 error(state, 0, "Integer constant out of range");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010815 }
10816 u = l = 0;
10817 if ((*end == 'u') || (*end == 'U')) {
10818 u = 1;
10819 end++;
10820 }
10821 if ((*end == 'l') || (*end == 'L')) {
10822 l = 1;
10823 end++;
10824 }
10825 if ((*end == 'u') || (*end == 'U')) {
10826 u = 1;
10827 end++;
10828 }
10829 if (*end) {
10830 error(state, 0, "Junk at end of integer constant");
10831 }
10832 if (u && l) {
10833 type = &ulong_type;
10834 }
10835 else if (l) {
10836 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010837 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010838 type = &ulong_type;
10839 }
10840 }
10841 else if (u) {
10842 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010843 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010844 type = &ulong_type;
10845 }
10846 }
10847 else {
10848 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010849 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010850 type = &uint_type;
10851 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010852 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010853 type = &ulong_type;
10854 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010855 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010856 type = &long_type;
10857 }
10858 }
10859 def = int_const(state, type, val);
10860 return def;
10861}
10862
10863static struct triple *primary_expr(struct compile_state *state)
10864{
10865 struct triple *def;
10866 int tok;
10867 tok = peek(state);
10868 switch(tok) {
10869 case TOK_IDENT:
10870 {
10871 struct hash_entry *ident;
10872 /* Here ident is either:
10873 * a varable name
10874 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010875 */
Eric Biederman41203d92004-11-08 09:31:09 +000010876 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010877 if (!ident->sym_ident) {
10878 error(state, 0, "%s undeclared", ident->name);
10879 }
10880 def = ident->sym_ident->def;
10881 break;
10882 }
10883 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010884 {
10885 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010886 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010887 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010888 if (!ident->sym_ident) {
10889 error(state, 0, "%s undeclared", ident->name);
10890 }
10891 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010892 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010893 }
Eric Biederman41203d92004-11-08 09:31:09 +000010894 case TOK_MIDENT:
10895 {
10896 struct hash_entry *ident;
10897 ident = eat(state, TOK_MIDENT)->ident;
10898 warning(state, 0, "Replacing undefined macro: %s with 0",
10899 ident->name);
10900 def = int_const(state, &int_type, 0);
10901 break;
10902 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010903 case TOK_LPAREN:
10904 eat(state, TOK_LPAREN);
10905 def = expr(state);
10906 eat(state, TOK_RPAREN);
10907 break;
10908 case TOK_LIT_INT:
10909 def = integer_constant(state);
10910 break;
10911 case TOK_LIT_FLOAT:
10912 eat(state, TOK_LIT_FLOAT);
10913 error(state, 0, "Floating point constants not supported");
10914 def = 0;
10915 FINISHME();
10916 break;
10917 case TOK_LIT_CHAR:
10918 def = character_constant(state);
10919 break;
10920 case TOK_LIT_STRING:
10921 def = string_constant(state);
10922 break;
10923 default:
10924 def = 0;
10925 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10926 }
10927 return def;
10928}
10929
10930static struct triple *postfix_expr(struct compile_state *state)
10931{
10932 struct triple *def;
10933 int postfix;
10934 def = primary_expr(state);
10935 do {
10936 struct triple *left;
10937 int tok;
10938 postfix = 1;
10939 left = def;
10940 switch((tok = peek(state))) {
10941 case TOK_LBRACKET:
10942 eat(state, TOK_LBRACKET);
10943 def = mk_subscript_expr(state, left, expr(state));
10944 eat(state, TOK_RBRACKET);
10945 break;
10946 case TOK_LPAREN:
10947 def = call_expr(state, def);
10948 break;
10949 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010950 {
10951 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010952 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010953 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010954 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010955 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010956 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010957 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010958 {
10959 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010960 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010961 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010962 def = mk_deref_expr(state, read_expr(state, def));
10963 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010964 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010965 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010966 case TOK_PLUSPLUS:
10967 eat(state, TOK_PLUSPLUS);
10968 def = mk_post_inc_expr(state, left);
10969 break;
10970 case TOK_MINUSMINUS:
10971 eat(state, TOK_MINUSMINUS);
10972 def = mk_post_dec_expr(state, left);
10973 break;
10974 default:
10975 postfix = 0;
10976 break;
10977 }
10978 } while(postfix);
10979 return def;
10980}
10981
10982static struct triple *cast_expr(struct compile_state *state);
10983
10984static struct triple *unary_expr(struct compile_state *state)
10985{
10986 struct triple *def, *right;
10987 int tok;
10988 switch((tok = peek(state))) {
10989 case TOK_PLUSPLUS:
10990 eat(state, TOK_PLUSPLUS);
10991 def = mk_pre_inc_expr(state, unary_expr(state));
10992 break;
10993 case TOK_MINUSMINUS:
10994 eat(state, TOK_MINUSMINUS);
10995 def = mk_pre_dec_expr(state, unary_expr(state));
10996 break;
10997 case TOK_AND:
10998 eat(state, TOK_AND);
10999 def = mk_addr_expr(state, cast_expr(state), 0);
11000 break;
11001 case TOK_STAR:
11002 eat(state, TOK_STAR);
11003 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11004 break;
11005 case TOK_PLUS:
11006 eat(state, TOK_PLUS);
11007 right = read_expr(state, cast_expr(state));
11008 arithmetic(state, right);
11009 def = integral_promotion(state, right);
11010 break;
11011 case TOK_MINUS:
11012 eat(state, TOK_MINUS);
11013 right = read_expr(state, cast_expr(state));
11014 arithmetic(state, right);
11015 def = integral_promotion(state, right);
11016 def = triple(state, OP_NEG, def->type, def, 0);
11017 break;
11018 case TOK_TILDE:
11019 eat(state, TOK_TILDE);
11020 right = read_expr(state, cast_expr(state));
11021 integral(state, right);
11022 def = integral_promotion(state, right);
11023 def = triple(state, OP_INVERT, def->type, def, 0);
11024 break;
11025 case TOK_BANG:
11026 eat(state, TOK_BANG);
11027 right = read_expr(state, cast_expr(state));
11028 bool(state, right);
11029 def = lfalse_expr(state, right);
11030 break;
11031 case TOK_SIZEOF:
11032 {
11033 struct type *type;
11034 int tok1, tok2;
11035 eat(state, TOK_SIZEOF);
11036 tok1 = peek(state);
11037 tok2 = peek2(state);
11038 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11039 eat(state, TOK_LPAREN);
11040 type = type_name(state);
11041 eat(state, TOK_RPAREN);
11042 }
11043 else {
11044 struct triple *expr;
11045 expr = unary_expr(state);
11046 type = expr->type;
11047 release_expr(state, expr);
11048 }
Eric Biederman90089602004-05-28 14:11:54 +000011049 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011050 break;
11051 }
11052 case TOK_ALIGNOF:
11053 {
11054 struct type *type;
11055 int tok1, tok2;
11056 eat(state, TOK_ALIGNOF);
11057 tok1 = peek(state);
11058 tok2 = peek2(state);
11059 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11060 eat(state, TOK_LPAREN);
11061 type = type_name(state);
11062 eat(state, TOK_RPAREN);
11063 }
11064 else {
11065 struct triple *expr;
11066 expr = unary_expr(state);
11067 type = expr->type;
11068 release_expr(state, expr);
11069 }
Eric Biederman90089602004-05-28 14:11:54 +000011070 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011071 break;
11072 }
Eric Biederman41203d92004-11-08 09:31:09 +000011073 case TOK_MDEFINED:
11074 {
11075 /* We only come here if we are called from the preprocessor */
11076 struct hash_entry *ident;
11077 int parens;
11078 eat(state, TOK_MDEFINED);
11079 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011080 if (pp_peek(state) == TOK_LPAREN) {
11081 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011082 parens = 1;
11083 }
Eric Biedermancb364952004-11-15 10:46:44 +000011084 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011085 if (parens) {
11086 eat(state, TOK_RPAREN);
11087 }
11088 def = int_const(state, &int_type, ident->sym_define != 0);
11089 break;
11090 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011091 default:
11092 def = postfix_expr(state);
11093 break;
11094 }
11095 return def;
11096}
11097
11098static struct triple *cast_expr(struct compile_state *state)
11099{
11100 struct triple *def;
11101 int tok1, tok2;
11102 tok1 = peek(state);
11103 tok2 = peek2(state);
11104 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11105 struct type *type;
11106 eat(state, TOK_LPAREN);
11107 type = type_name(state);
11108 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011109 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011110 }
11111 else {
11112 def = unary_expr(state);
11113 }
11114 return def;
11115}
11116
11117static struct triple *mult_expr(struct compile_state *state)
11118{
11119 struct triple *def;
11120 int done;
11121 def = cast_expr(state);
11122 do {
11123 struct triple *left, *right;
11124 struct type *result_type;
11125 int tok, op, sign;
11126 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011127 tok = peek(state);
11128 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011129 case TOK_STAR:
11130 case TOK_DIV:
11131 case TOK_MOD:
11132 left = read_expr(state, def);
11133 arithmetic(state, left);
11134
11135 eat(state, tok);
11136
11137 right = read_expr(state, cast_expr(state));
11138 arithmetic(state, right);
11139
11140 result_type = arithmetic_result(state, left, right);
11141 sign = is_signed(result_type);
11142 op = -1;
11143 switch(tok) {
11144 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11145 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11146 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11147 }
11148 def = triple(state, op, result_type, left, right);
11149 break;
11150 default:
11151 done = 1;
11152 break;
11153 }
11154 } while(!done);
11155 return def;
11156}
11157
11158static struct triple *add_expr(struct compile_state *state)
11159{
11160 struct triple *def;
11161 int done;
11162 def = mult_expr(state);
11163 do {
11164 done = 0;
11165 switch( peek(state)) {
11166 case TOK_PLUS:
11167 eat(state, TOK_PLUS);
11168 def = mk_add_expr(state, def, mult_expr(state));
11169 break;
11170 case TOK_MINUS:
11171 eat(state, TOK_MINUS);
11172 def = mk_sub_expr(state, def, mult_expr(state));
11173 break;
11174 default:
11175 done = 1;
11176 break;
11177 }
11178 } while(!done);
11179 return def;
11180}
11181
11182static struct triple *shift_expr(struct compile_state *state)
11183{
11184 struct triple *def;
11185 int done;
11186 def = add_expr(state);
11187 do {
11188 struct triple *left, *right;
11189 int tok, op;
11190 done = 0;
11191 switch((tok = peek(state))) {
11192 case TOK_SL:
11193 case TOK_SR:
11194 left = read_expr(state, def);
11195 integral(state, left);
11196 left = integral_promotion(state, left);
11197
11198 eat(state, tok);
11199
11200 right = read_expr(state, add_expr(state));
11201 integral(state, right);
11202 right = integral_promotion(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011203
11204 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011205 is_signed(left->type)? OP_SSR: OP_USR;
11206
11207 def = triple(state, op, left->type, left, right);
11208 break;
11209 default:
11210 done = 1;
11211 break;
11212 }
11213 } while(!done);
11214 return def;
11215}
11216
11217static struct triple *relational_expr(struct compile_state *state)
11218{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011219#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011220#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011221#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011222 struct triple *def;
11223 int done;
11224 def = shift_expr(state);
11225 do {
11226 struct triple *left, *right;
11227 struct type *arg_type;
11228 int tok, op, sign;
11229 done = 0;
11230 switch((tok = peek(state))) {
11231 case TOK_LESS:
11232 case TOK_MORE:
11233 case TOK_LESSEQ:
11234 case TOK_MOREEQ:
11235 left = read_expr(state, def);
11236 arithmetic(state, left);
11237
11238 eat(state, tok);
11239
11240 right = read_expr(state, shift_expr(state));
11241 arithmetic(state, right);
11242
11243 arg_type = arithmetic_result(state, left, right);
11244 sign = is_signed(arg_type);
11245 op = -1;
11246 switch(tok) {
11247 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11248 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11249 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11250 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11251 }
11252 def = triple(state, op, &int_type, left, right);
11253 break;
11254 default:
11255 done = 1;
11256 break;
11257 }
11258 } while(!done);
11259 return def;
11260}
11261
11262static struct triple *equality_expr(struct compile_state *state)
11263{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011264#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011265#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011266#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011267 struct triple *def;
11268 int done;
11269 def = relational_expr(state);
11270 do {
11271 struct triple *left, *right;
11272 int tok, op;
11273 done = 0;
11274 switch((tok = peek(state))) {
11275 case TOK_EQEQ:
11276 case TOK_NOTEQ:
11277 left = read_expr(state, def);
11278 arithmetic(state, left);
11279 eat(state, tok);
11280 right = read_expr(state, relational_expr(state));
11281 arithmetic(state, right);
11282 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11283 def = triple(state, op, &int_type, left, right);
11284 break;
11285 default:
11286 done = 1;
11287 break;
11288 }
11289 } while(!done);
11290 return def;
11291}
11292
11293static struct triple *and_expr(struct compile_state *state)
11294{
11295 struct triple *def;
11296 def = equality_expr(state);
11297 while(peek(state) == TOK_AND) {
11298 struct triple *left, *right;
11299 struct type *result_type;
11300 left = read_expr(state, def);
11301 integral(state, left);
11302 eat(state, TOK_AND);
11303 right = read_expr(state, equality_expr(state));
11304 integral(state, right);
11305 result_type = arithmetic_result(state, left, right);
11306 def = triple(state, OP_AND, result_type, left, right);
11307 }
11308 return def;
11309}
11310
11311static struct triple *xor_expr(struct compile_state *state)
11312{
11313 struct triple *def;
11314 def = and_expr(state);
11315 while(peek(state) == TOK_XOR) {
11316 struct triple *left, *right;
11317 struct type *result_type;
11318 left = read_expr(state, def);
11319 integral(state, left);
11320 eat(state, TOK_XOR);
11321 right = read_expr(state, and_expr(state));
11322 integral(state, right);
11323 result_type = arithmetic_result(state, left, right);
11324 def = triple(state, OP_XOR, result_type, left, right);
11325 }
11326 return def;
11327}
11328
11329static struct triple *or_expr(struct compile_state *state)
11330{
11331 struct triple *def;
11332 def = xor_expr(state);
11333 while(peek(state) == TOK_OR) {
11334 struct triple *left, *right;
11335 struct type *result_type;
11336 left = read_expr(state, def);
11337 integral(state, left);
11338 eat(state, TOK_OR);
11339 right = read_expr(state, xor_expr(state));
11340 integral(state, right);
11341 result_type = arithmetic_result(state, left, right);
11342 def = triple(state, OP_OR, result_type, left, right);
11343 }
11344 return def;
11345}
11346
11347static struct triple *land_expr(struct compile_state *state)
11348{
11349 struct triple *def;
11350 def = or_expr(state);
11351 while(peek(state) == TOK_LOGAND) {
11352 struct triple *left, *right;
11353 left = read_expr(state, def);
11354 bool(state, left);
11355 eat(state, TOK_LOGAND);
11356 right = read_expr(state, or_expr(state));
11357 bool(state, right);
11358
Eric Biederman90089602004-05-28 14:11:54 +000011359 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011360 ltrue_expr(state, left),
11361 ltrue_expr(state, right));
11362 }
11363 return def;
11364}
11365
11366static struct triple *lor_expr(struct compile_state *state)
11367{
11368 struct triple *def;
11369 def = land_expr(state);
11370 while(peek(state) == TOK_LOGOR) {
11371 struct triple *left, *right;
11372 left = read_expr(state, def);
11373 bool(state, left);
11374 eat(state, TOK_LOGOR);
11375 right = read_expr(state, land_expr(state));
11376 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011377
Stefan Reinauer14e22772010-04-27 06:56:47 +000011378 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011379 ltrue_expr(state, left),
11380 ltrue_expr(state, right));
11381 }
11382 return def;
11383}
11384
11385static struct triple *conditional_expr(struct compile_state *state)
11386{
11387 struct triple *def;
11388 def = lor_expr(state);
11389 if (peek(state) == TOK_QUEST) {
11390 struct triple *test, *left, *right;
11391 bool(state, def);
11392 test = ltrue_expr(state, read_expr(state, def));
11393 eat(state, TOK_QUEST);
11394 left = read_expr(state, expr(state));
11395 eat(state, TOK_COLON);
11396 right = read_expr(state, conditional_expr(state));
11397
Eric Biederman90089602004-05-28 14:11:54 +000011398 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011399 }
11400 return def;
11401}
11402
Eric Biederman41203d92004-11-08 09:31:09 +000011403struct cv_triple {
11404 struct triple *val;
11405 int id;
11406};
11407
11408static void set_cv(struct compile_state *state, struct cv_triple *cv,
11409 struct triple *dest, struct triple *val)
11410{
11411 if (cv[dest->id].val) {
11412 free_triple(state, cv[dest->id].val);
11413 }
11414 cv[dest->id].val = val;
11415}
11416static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11417 struct triple *src)
11418{
11419 return cv[src->id].val;
11420}
11421
Eric Biedermanb138ac82003-04-22 18:44:01 +000011422static struct triple *eval_const_expr(
11423 struct compile_state *state, struct triple *expr)
11424{
11425 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011426 if (is_const(expr)) {
11427 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011428 }
Eric Biederman00443072003-06-24 12:34:45 +000011429 else {
11430 /* If we don't start out as a constant simplify into one */
11431 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011432 struct cv_triple *cv;
11433 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011434 head = label(state); /* dummy initial triple */
11435 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011436 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011437 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011438 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011439 }
Eric Biederman41203d92004-11-08 09:31:09 +000011440 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11441 i = 1;
11442 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11443 cv[i].val = 0;
11444 cv[i].id = ptr->id;
11445 ptr->id = i;
11446 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011447 }
Eric Biederman41203d92004-11-08 09:31:09 +000011448 ptr = head->next;
11449 do {
11450 valid_ins(state, ptr);
11451 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011452 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011453 "unexpected %s in constant expression",
11454 tops(ptr->op));
11455 }
11456 else if (ptr->op == OP_LIST) {
11457 }
11458 else if (triple_is_structural(state, ptr)) {
11459 ptr = ptr->next;
11460 }
11461 else if (triple_is_ubranch(state, ptr)) {
11462 ptr = TARG(ptr, 0);
11463 }
11464 else if (triple_is_cbranch(state, ptr)) {
11465 struct triple *cond_val;
11466 cond_val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011467 if (!cond_val || !is_const(cond_val) ||
11468 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011469 {
11470 internal_error(state, ptr, "bad branch condition");
11471 }
11472 if (cond_val->u.cval == 0) {
11473 ptr = ptr->next;
11474 } else {
11475 ptr = TARG(ptr, 0);
11476 }
11477 }
11478 else if (triple_is_branch(state, ptr)) {
11479 error(state, ptr, "bad branch type in constant expression");
11480 }
11481 else if (ptr->op == OP_WRITE) {
11482 struct triple *val;
11483 val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011484
11485 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011486 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011487 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011488 copy_triple(state, val));
11489 ptr = ptr->next;
11490 }
11491 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011492 set_cv(state, cv, ptr,
11493 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011494 get_cv(state, cv, RHS(ptr, 0))));
11495 ptr = ptr->next;
11496 }
11497 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11498 struct triple *val, **rhs;
11499 val = copy_triple(state, ptr);
11500 rhs = triple_rhs(state, val, 0);
11501 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11502 if (!*rhs) {
11503 internal_error(state, ptr, "Missing rhs");
11504 }
11505 *rhs = get_cv(state, cv, *rhs);
11506 }
11507 simplify(state, val);
11508 set_cv(state, cv, ptr, val);
11509 ptr = ptr->next;
11510 }
11511 else {
11512 error(state, ptr, "impure operation in constant expression");
11513 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011514
Eric Biederman41203d92004-11-08 09:31:09 +000011515 } while(ptr != head);
11516
11517 /* Get the result value */
11518 def = get_cv(state, cv, head->prev);
11519 cv[head->prev->id].val = 0;
11520
11521 /* Free the temporary values */
11522 for(i = 0; i < count; i++) {
11523 if (cv[i].val) {
11524 free_triple(state, cv[i].val);
11525 cv[i].val = 0;
11526 }
11527 }
11528 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011529 /* Free the intermediate expressions */
11530 while(head->next != head) {
11531 release_triple(state, head->next);
11532 }
11533 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011534 }
Eric Biederman41203d92004-11-08 09:31:09 +000011535 if (!is_const(def)) {
11536 error(state, expr, "Not a constant expression");
11537 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011538 return def;
11539}
11540
11541static struct triple *constant_expr(struct compile_state *state)
11542{
11543 return eval_const_expr(state, conditional_expr(state));
11544}
11545
11546static struct triple *assignment_expr(struct compile_state *state)
11547{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011548 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011549 int tok, op, sign;
11550 /* The C grammer in K&R shows assignment expressions
11551 * only taking unary expressions as input on their
11552 * left hand side. But specifies the precedence of
11553 * assignemnt as the lowest operator except for comma.
11554 *
11555 * Allowing conditional expressions on the left hand side
11556 * of an assignement results in a grammar that accepts
11557 * a larger set of statements than standard C. As long
11558 * as the subset of the grammar that is standard C behaves
11559 * correctly this should cause no problems.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011560 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011561 * For the extra token strings accepted by the grammar
11562 * none of them should produce a valid lvalue, so they
11563 * should not produce functioning programs.
11564 *
11565 * GCC has this bug as well, so surprises should be minimal.
11566 */
11567 def = conditional_expr(state);
11568 left = def;
11569 switch((tok = peek(state))) {
11570 case TOK_EQ:
11571 lvalue(state, left);
11572 eat(state, TOK_EQ);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011573 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011574 read_expr(state, assignment_expr(state)));
11575 break;
11576 case TOK_TIMESEQ:
11577 case TOK_DIVEQ:
11578 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011579 lvalue(state, left);
11580 arithmetic(state, left);
11581 eat(state, tok);
11582 right = read_expr(state, assignment_expr(state));
11583 arithmetic(state, right);
11584
11585 sign = is_signed(left->type);
11586 op = -1;
11587 switch(tok) {
11588 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11589 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11590 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011591 }
11592 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011593 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011594 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011595 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011596 case TOK_PLUSEQ:
11597 lvalue(state, left);
11598 eat(state, TOK_PLUSEQ);
11599 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011600 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011601 break;
11602 case TOK_MINUSEQ:
11603 lvalue(state, left);
11604 eat(state, TOK_MINUSEQ);
11605 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011606 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011607 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011608 case TOK_SLEQ:
11609 case TOK_SREQ:
11610 case TOK_ANDEQ:
11611 case TOK_XOREQ:
11612 case TOK_OREQ:
11613 lvalue(state, left);
11614 integral(state, left);
11615 eat(state, tok);
11616 right = read_expr(state, assignment_expr(state));
11617 integral(state, right);
11618 right = integral_promotion(state, right);
11619 sign = is_signed(left->type);
11620 op = -1;
11621 switch(tok) {
11622 case TOK_SLEQ: op = OP_SL; break;
11623 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11624 case TOK_ANDEQ: op = OP_AND; break;
11625 case TOK_XOREQ: op = OP_XOR; break;
11626 case TOK_OREQ: op = OP_OR; break;
11627 }
11628 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011629 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011630 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011631 break;
11632 }
11633 return def;
11634}
11635
11636static struct triple *expr(struct compile_state *state)
11637{
11638 struct triple *def;
11639 def = assignment_expr(state);
11640 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011641 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011642 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011643 }
11644 return def;
11645}
11646
11647static void expr_statement(struct compile_state *state, struct triple *first)
11648{
11649 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011650 /* lvalue conversions always apply except when certian operators
11651 * are applied. I apply the lvalue conversions here
11652 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011653 */
11654 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011655 }
11656 eat(state, TOK_SEMI);
11657}
11658
11659static void if_statement(struct compile_state *state, struct triple *first)
11660{
11661 struct triple *test, *jmp1, *jmp2, *middle, *end;
11662
11663 jmp1 = jmp2 = middle = 0;
11664 eat(state, TOK_IF);
11665 eat(state, TOK_LPAREN);
11666 test = expr(state);
11667 bool(state, test);
11668 /* Cleanup and invert the test */
11669 test = lfalse_expr(state, read_expr(state, test));
11670 eat(state, TOK_RPAREN);
11671 /* Generate the needed pieces */
11672 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011673 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011674 /* Thread the pieces together */
11675 flatten(state, first, test);
11676 flatten(state, first, jmp1);
11677 flatten(state, first, label(state));
11678 statement(state, first);
11679 if (peek(state) == TOK_ELSE) {
11680 eat(state, TOK_ELSE);
11681 /* Generate the rest of the pieces */
11682 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011683 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011684 /* Thread them together */
11685 flatten(state, first, jmp2);
11686 flatten(state, first, middle);
11687 statement(state, first);
11688 flatten(state, first, end);
11689 }
11690 else {
11691 flatten(state, first, middle);
11692 }
11693}
11694
11695static void for_statement(struct compile_state *state, struct triple *first)
11696{
11697 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11698 struct triple *label1, *label2, *label3;
11699 struct hash_entry *ident;
11700
11701 eat(state, TOK_FOR);
11702 eat(state, TOK_LPAREN);
11703 head = test = tail = jmp1 = jmp2 = 0;
11704 if (peek(state) != TOK_SEMI) {
11705 head = expr(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011706 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011707 eat(state, TOK_SEMI);
11708 if (peek(state) != TOK_SEMI) {
11709 test = expr(state);
11710 bool(state, test);
11711 test = ltrue_expr(state, read_expr(state, test));
11712 }
11713 eat(state, TOK_SEMI);
11714 if (peek(state) != TOK_RPAREN) {
11715 tail = expr(state);
11716 }
11717 eat(state, TOK_RPAREN);
11718 /* Generate the needed pieces */
11719 label1 = label(state);
11720 label2 = label(state);
11721 label3 = label(state);
11722 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011723 jmp1 = branch(state, label3, 0);
11724 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011725 }
11726 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011727 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011728 }
11729 end = label(state);
11730 /* Remember where break and continue go */
11731 start_scope(state);
11732 ident = state->i_break;
11733 symbol(state, ident, &ident->sym_ident, end, end->type);
11734 ident = state->i_continue;
11735 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11736 /* Now include the body */
11737 flatten(state, first, head);
11738 flatten(state, first, jmp1);
11739 flatten(state, first, label1);
11740 statement(state, first);
11741 flatten(state, first, label2);
11742 flatten(state, first, tail);
11743 flatten(state, first, label3);
11744 flatten(state, first, test);
11745 flatten(state, first, jmp2);
11746 flatten(state, first, end);
11747 /* Cleanup the break/continue scope */
11748 end_scope(state);
11749}
11750
11751static void while_statement(struct compile_state *state, struct triple *first)
11752{
11753 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11754 struct hash_entry *ident;
11755 eat(state, TOK_WHILE);
11756 eat(state, TOK_LPAREN);
11757 test = expr(state);
11758 bool(state, test);
11759 test = ltrue_expr(state, read_expr(state, test));
11760 eat(state, TOK_RPAREN);
11761 /* Generate the needed pieces */
11762 label1 = label(state);
11763 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011764 jmp1 = branch(state, label2, 0);
11765 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011766 end = label(state);
11767 /* Remember where break and continue go */
11768 start_scope(state);
11769 ident = state->i_break;
11770 symbol(state, ident, &ident->sym_ident, end, end->type);
11771 ident = state->i_continue;
11772 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11773 /* Thread them together */
11774 flatten(state, first, jmp1);
11775 flatten(state, first, label1);
11776 statement(state, first);
11777 flatten(state, first, label2);
11778 flatten(state, first, test);
11779 flatten(state, first, jmp2);
11780 flatten(state, first, end);
11781 /* Cleanup the break/continue scope */
11782 end_scope(state);
11783}
11784
11785static void do_statement(struct compile_state *state, struct triple *first)
11786{
11787 struct triple *label1, *label2, *test, *end;
11788 struct hash_entry *ident;
11789 eat(state, TOK_DO);
11790 /* Generate the needed pieces */
11791 label1 = label(state);
11792 label2 = label(state);
11793 end = label(state);
11794 /* Remember where break and continue go */
11795 start_scope(state);
11796 ident = state->i_break;
11797 symbol(state, ident, &ident->sym_ident, end, end->type);
11798 ident = state->i_continue;
11799 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11800 /* Now include the body */
11801 flatten(state, first, label1);
11802 statement(state, first);
11803 /* Cleanup the break/continue scope */
11804 end_scope(state);
11805 /* Eat the rest of the loop */
11806 eat(state, TOK_WHILE);
11807 eat(state, TOK_LPAREN);
11808 test = read_expr(state, expr(state));
11809 bool(state, test);
11810 eat(state, TOK_RPAREN);
11811 eat(state, TOK_SEMI);
11812 /* Thread the pieces together */
11813 test = ltrue_expr(state, test);
11814 flatten(state, first, label2);
11815 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011816 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011817 flatten(state, first, end);
11818}
11819
11820
11821static void return_statement(struct compile_state *state, struct triple *first)
11822{
11823 struct triple *jmp, *mv, *dest, *var, *val;
11824 int last;
11825 eat(state, TOK_RETURN);
11826
Stefan Reinauer50542a82007-10-24 11:14:14 +000011827#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011828#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011829#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011830 val = 0;
11831 /* If we have a return value do some more work */
11832 if (peek(state) != TOK_SEMI) {
11833 val = read_expr(state, expr(state));
11834 }
11835 eat(state, TOK_SEMI);
11836
11837 /* See if this last statement in a function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011838 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011839 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11840
11841 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011842 var = fresult(state, state->main_function);
11843
Eric Biedermanb138ac82003-04-22 18:44:01 +000011844 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011845 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011846 mv = jmp = 0;
11847 /* If needed generate a jump instruction */
11848 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011849 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011850 }
11851 /* If needed generate an assignment instruction */
11852 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011853 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011854 }
11855 /* Now put the code together */
11856 if (mv) {
11857 flatten(state, first, mv);
11858 flatten(state, first, jmp);
11859 }
11860 else if (jmp) {
11861 flatten(state, first, jmp);
11862 }
11863}
11864
11865static void break_statement(struct compile_state *state, struct triple *first)
11866{
11867 struct triple *dest;
11868 eat(state, TOK_BREAK);
11869 eat(state, TOK_SEMI);
11870 if (!state->i_break->sym_ident) {
11871 error(state, 0, "break statement not within loop or switch");
11872 }
11873 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011874 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011875}
11876
11877static void continue_statement(struct compile_state *state, struct triple *first)
11878{
11879 struct triple *dest;
11880 eat(state, TOK_CONTINUE);
11881 eat(state, TOK_SEMI);
11882 if (!state->i_continue->sym_ident) {
11883 error(state, 0, "continue statement outside of a loop");
11884 }
11885 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011886 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011887}
11888
11889static void goto_statement(struct compile_state *state, struct triple *first)
11890{
Eric Biederman153ea352003-06-20 14:43:20 +000011891 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011892 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011893 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011894 if (!ident->sym_label) {
11895 /* If this is a forward branch allocate the label now,
11896 * it will be flattend in the appropriate location later.
11897 */
11898 struct triple *ins;
11899 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011900 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011901 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011902 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011903
11904 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011905}
11906
11907static void labeled_statement(struct compile_state *state, struct triple *first)
11908{
Eric Biederman153ea352003-06-20 14:43:20 +000011909 struct triple *ins;
11910 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011911
Eric Biederman41203d92004-11-08 09:31:09 +000011912 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011913 if (ident->sym_label && ident->sym_label->def) {
11914 ins = ident->sym_label->def;
11915 put_occurance(ins->occurance);
11916 ins->occurance = new_occurance(state);
11917 }
11918 else {
11919 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011920 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011921 }
11922 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11923 error(state, 0, "label %s already defined", ident->name);
11924 }
11925 flatten(state, first, ins);
11926
Eric Biedermanb138ac82003-04-22 18:44:01 +000011927 eat(state, TOK_COLON);
11928 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011929}
11930
11931static void switch_statement(struct compile_state *state, struct triple *first)
11932{
Eric Biederman83b991a2003-10-11 06:20:25 +000011933 struct triple *value, *top, *end, *dbranch;
11934 struct hash_entry *ident;
11935
11936 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011937 eat(state, TOK_SWITCH);
11938 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011939 value = expr(state);
11940 integral(state, value);
11941 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011942 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011943 /* Generate the needed pieces */
11944 top = label(state);
11945 end = label(state);
11946 dbranch = branch(state, end, 0);
11947 /* Remember where case branches and break goes */
11948 start_scope(state);
11949 ident = state->i_switch;
11950 symbol(state, ident, &ident->sym_ident, value, value->type);
11951 ident = state->i_case;
11952 symbol(state, ident, &ident->sym_ident, top, top->type);
11953 ident = state->i_break;
11954 symbol(state, ident, &ident->sym_ident, end, end->type);
11955 ident = state->i_default;
11956 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11957 /* Thread them together */
11958 flatten(state, first, value);
11959 flatten(state, first, top);
11960 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011961 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011962 flatten(state, first, end);
11963 /* Cleanup the switch scope */
11964 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011965}
11966
11967static void case_statement(struct compile_state *state, struct triple *first)
11968{
Eric Biederman83b991a2003-10-11 06:20:25 +000011969 struct triple *cvalue, *dest, *test, *jmp;
11970 struct triple *ptr, *value, *top, *dbranch;
11971
11972 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011973 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011974 cvalue = constant_expr(state);
11975 integral(state, cvalue);
11976 if (cvalue->op != OP_INTCONST) {
11977 error(state, 0, "integer constant expected");
11978 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011979 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011980 if (!state->i_case->sym_ident) {
11981 error(state, 0, "case statement not within a switch");
11982 }
11983
11984 /* Lookup the interesting pieces */
11985 top = state->i_case->sym_ident->def;
11986 value = state->i_switch->sym_ident->def;
11987 dbranch = state->i_default->sym_ident->def;
11988
11989 /* See if this case label has already been used */
11990 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11991 if (ptr->op != OP_EQ) {
11992 continue;
11993 }
11994 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11995 error(state, 0, "duplicate case %d statement",
11996 cvalue->u.cval);
11997 }
11998 }
11999 /* Generate the needed pieces */
12000 dest = label(state);
12001 test = triple(state, OP_EQ, &int_type, value, cvalue);
12002 jmp = branch(state, dest, test);
12003 /* Thread the pieces together */
12004 flatten(state, dbranch, test);
12005 flatten(state, dbranch, jmp);
12006 flatten(state, dbranch, label(state));
12007 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012008 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012009}
12010
12011static void default_statement(struct compile_state *state, struct triple *first)
12012{
Eric Biederman83b991a2003-10-11 06:20:25 +000012013 struct triple *dest;
12014 struct triple *dbranch, *end;
12015
12016 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012017 eat(state, TOK_DEFAULT);
12018 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012019
12020 if (!state->i_case->sym_ident) {
12021 error(state, 0, "default statement not within a switch");
12022 }
12023
12024 /* Lookup the interesting pieces */
12025 dbranch = state->i_default->sym_ident->def;
12026 end = state->i_break->sym_ident->def;
12027
12028 /* See if a default statement has already happened */
12029 if (TARG(dbranch, 0) != end) {
12030 error(state, 0, "duplicate default statement");
12031 }
12032
12033 /* Generate the needed pieces */
12034 dest = label(state);
12035
Eric Biederman90089602004-05-28 14:11:54 +000012036 /* Blame the branch on the default statement */
12037 put_occurance(dbranch->occurance);
12038 dbranch->occurance = new_occurance(state);
12039
Eric Biederman83b991a2003-10-11 06:20:25 +000012040 /* Thread the pieces together */
12041 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012042 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012043 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012044 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012045}
12046
12047static void asm_statement(struct compile_state *state, struct triple *first)
12048{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012049 struct asm_info *info;
12050 struct {
12051 struct triple *constraint;
12052 struct triple *expr;
12053 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12054 struct triple *def, *asm_str;
12055 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012056 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012057
Eric Biederman90089602004-05-28 14:11:54 +000012058 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012059 eat(state, TOK_ASM);
12060 /* For now ignore the qualifiers */
12061 switch(peek(state)) {
12062 case TOK_CONST:
12063 eat(state, TOK_CONST);
12064 break;
12065 case TOK_VOLATILE:
12066 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012067 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012068 break;
12069 }
12070 eat(state, TOK_LPAREN);
12071 asm_str = string_constant(state);
12072
12073 colons = 0;
12074 out = in = clobbers = 0;
12075 /* Outputs */
12076 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12077 eat(state, TOK_COLON);
12078 colons++;
12079 more = (peek(state) == TOK_LIT_STRING);
12080 while(more) {
12081 struct triple *var;
12082 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012083 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012084 more = 0;
12085 if (out > MAX_LHS) {
12086 error(state, 0, "Maximum output count exceeded.");
12087 }
12088 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012089 str = constraint->u.blob;
12090 if (str[0] != '=') {
12091 error(state, 0, "Output constraint does not start with =");
12092 }
12093 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012094 eat(state, TOK_LPAREN);
12095 var = conditional_expr(state);
12096 eat(state, TOK_RPAREN);
12097
12098 lvalue(state, var);
12099 out_param[out].constraint = constraint;
12100 out_param[out].expr = var;
12101 if (peek(state) == TOK_COMMA) {
12102 eat(state, TOK_COMMA);
12103 more = 1;
12104 }
12105 out++;
12106 }
12107 }
12108 /* Inputs */
12109 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12110 eat(state, TOK_COLON);
12111 colons++;
12112 more = (peek(state) == TOK_LIT_STRING);
12113 while(more) {
12114 struct triple *val;
12115 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012116 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012117 more = 0;
12118 if (in > MAX_RHS) {
12119 error(state, 0, "Maximum input count exceeded.");
12120 }
12121 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012122 str = constraint->u.blob;
12123 if (digitp(str[0] && str[1] == '\0')) {
12124 int val;
12125 val = digval(str[0]);
12126 if ((val < 0) || (val >= out)) {
12127 error(state, 0, "Invalid input constraint %d", val);
12128 }
12129 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012130 eat(state, TOK_LPAREN);
12131 val = conditional_expr(state);
12132 eat(state, TOK_RPAREN);
12133
12134 in_param[in].constraint = constraint;
12135 in_param[in].expr = val;
12136 if (peek(state) == TOK_COMMA) {
12137 eat(state, TOK_COMMA);
12138 more = 1;
12139 }
12140 in++;
12141 }
12142 }
12143
12144 /* Clobber */
12145 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12146 eat(state, TOK_COLON);
12147 colons++;
12148 more = (peek(state) == TOK_LIT_STRING);
12149 while(more) {
12150 struct triple *clobber;
12151 more = 0;
12152 if ((clobbers + out) > MAX_LHS) {
12153 error(state, 0, "Maximum clobber limit exceeded.");
12154 }
12155 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012156
12157 clob_param[clobbers].constraint = clobber;
12158 if (peek(state) == TOK_COMMA) {
12159 eat(state, TOK_COMMA);
12160 more = 1;
12161 }
12162 clobbers++;
12163 }
12164 }
12165 eat(state, TOK_RPAREN);
12166 eat(state, TOK_SEMI);
12167
12168
12169 info = xcmalloc(sizeof(*info), "asm_info");
12170 info->str = asm_str->u.blob;
12171 free_triple(state, asm_str);
12172
12173 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12174 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012175 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012176
12177 /* Find the register constraints */
12178 for(i = 0; i < out; i++) {
12179 struct triple *constraint;
12180 constraint = out_param[i].constraint;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012181 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012182 out_param[i].expr->type, constraint->u.blob);
12183 free_triple(state, constraint);
12184 }
12185 for(; i - out < clobbers; i++) {
12186 struct triple *constraint;
12187 constraint = clob_param[i - out].constraint;
12188 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12189 free_triple(state, constraint);
12190 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012191 for(i = 0; i < in; i++) {
12192 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012193 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012194 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012195 str = constraint->u.blob;
12196 if (digitp(str[0]) && str[1] == '\0') {
12197 struct reg_info cinfo;
12198 int val;
12199 val = digval(str[0]);
12200 cinfo.reg = info->tmpl.lhs[val].reg;
12201 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12202 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12203 if (cinfo.reg == REG_UNSET) {
12204 cinfo.reg = REG_VIRT0 + val;
12205 }
12206 if (cinfo.regcm == 0) {
12207 error(state, 0, "No registers for %d", val);
12208 }
12209 info->tmpl.lhs[val] = cinfo;
12210 info->tmpl.rhs[i] = cinfo;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012211
Eric Biederman8d9c1232003-06-17 08:42:17 +000012212 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012213 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012214 in_param[i].expr->type, str);
12215 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012216 free_triple(state, constraint);
12217 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012218
12219 /* Now build the helper expressions */
12220 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012221 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012222 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012223 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012224 for(i = 0; i < (out + clobbers); i++) {
12225 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012226 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012227 if (i < out) {
12228 type = out_param[i].expr->type;
12229 } else {
12230 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12231 if (size >= SIZEOF_LONG) {
12232 type = &ulong_type;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012233 }
Eric Biederman90089602004-05-28 14:11:54 +000012234 else if (size >= SIZEOF_INT) {
12235 type = &uint_type;
12236 }
12237 else if (size >= SIZEOF_SHORT) {
12238 type = &ushort_type;
12239 }
12240 else {
12241 type = &uchar_type;
12242 }
12243 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012244 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012245 piece->u.cval = i;
12246 LHS(def, i) = piece;
12247 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012248 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012249 /* And write the helpers to their destinations */
12250 for(i = 0; i < out; i++) {
12251 struct triple *piece;
12252 piece = LHS(def, i);
12253 flatten(state, first,
12254 write_expr(state, out_param[i].expr, piece));
12255 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012256}
12257
12258
12259static int isdecl(int tok)
12260{
12261 switch(tok) {
12262 case TOK_AUTO:
12263 case TOK_REGISTER:
12264 case TOK_STATIC:
12265 case TOK_EXTERN:
12266 case TOK_TYPEDEF:
12267 case TOK_CONST:
12268 case TOK_RESTRICT:
12269 case TOK_VOLATILE:
12270 case TOK_VOID:
12271 case TOK_CHAR:
12272 case TOK_SHORT:
12273 case TOK_INT:
12274 case TOK_LONG:
12275 case TOK_FLOAT:
12276 case TOK_DOUBLE:
12277 case TOK_SIGNED:
12278 case TOK_UNSIGNED:
12279 case TOK_STRUCT:
12280 case TOK_UNION:
12281 case TOK_ENUM:
12282 case TOK_TYPE_NAME: /* typedef name */
12283 return 1;
12284 default:
12285 return 0;
12286 }
12287}
12288
12289static void compound_statement(struct compile_state *state, struct triple *first)
12290{
12291 eat(state, TOK_LBRACE);
12292 start_scope(state);
12293
12294 /* statement-list opt */
12295 while (peek(state) != TOK_RBRACE) {
12296 statement(state, first);
12297 }
12298 end_scope(state);
12299 eat(state, TOK_RBRACE);
12300}
12301
12302static void statement(struct compile_state *state, struct triple *first)
12303{
12304 int tok;
12305 tok = peek(state);
12306 if (tok == TOK_LBRACE) {
12307 compound_statement(state, first);
12308 }
12309 else if (tok == TOK_IF) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012310 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012311 }
12312 else if (tok == TOK_FOR) {
12313 for_statement(state, first);
12314 }
12315 else if (tok == TOK_WHILE) {
12316 while_statement(state, first);
12317 }
12318 else if (tok == TOK_DO) {
12319 do_statement(state, first);
12320 }
12321 else if (tok == TOK_RETURN) {
12322 return_statement(state, first);
12323 }
12324 else if (tok == TOK_BREAK) {
12325 break_statement(state, first);
12326 }
12327 else if (tok == TOK_CONTINUE) {
12328 continue_statement(state, first);
12329 }
12330 else if (tok == TOK_GOTO) {
12331 goto_statement(state, first);
12332 }
12333 else if (tok == TOK_SWITCH) {
12334 switch_statement(state, first);
12335 }
12336 else if (tok == TOK_ASM) {
12337 asm_statement(state, first);
12338 }
12339 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012340 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012341 }
12342 else if (tok == TOK_CASE) {
12343 case_statement(state, first);
12344 }
12345 else if (tok == TOK_DEFAULT) {
12346 default_statement(state, first);
12347 }
12348 else if (isdecl(tok)) {
12349 /* This handles C99 intermixing of statements and decls */
12350 decl(state, first);
12351 }
12352 else {
12353 expr_statement(state, first);
12354 }
12355}
12356
12357static struct type *param_decl(struct compile_state *state)
12358{
12359 struct type *type;
12360 struct hash_entry *ident;
12361 /* Cheat so the declarator will know we are not global */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012362 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012363 ident = 0;
12364 type = decl_specifiers(state);
12365 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012366 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012367 end_scope(state);
12368 return type;
12369}
12370
12371static struct type *param_type_list(struct compile_state *state, struct type *type)
12372{
12373 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012374 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012375 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012376 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012377 while(peek(state) == TOK_COMMA) {
12378 eat(state, TOK_COMMA);
12379 if (peek(state) == TOK_DOTS) {
12380 eat(state, TOK_DOTS);
12381 error(state, 0, "variadic functions not supported");
12382 }
12383 else {
12384 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12385 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012386 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012387 }
12388 }
12389 return ftype;
12390}
12391
Eric Biedermanb138ac82003-04-22 18:44:01 +000012392static struct type *type_name(struct compile_state *state)
12393{
12394 struct type *type;
12395 type = specifier_qualifier_list(state);
12396 /* abstract-declarator (may consume no tokens) */
12397 type = declarator(state, type, 0, 0);
12398 return type;
12399}
12400
12401static struct type *direct_declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012402 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012403 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012404{
Eric Biederman41203d92004-11-08 09:31:09 +000012405 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012406 struct type *outer;
12407 int op;
12408 outer = 0;
12409 arrays_complete(state, type);
12410 switch(peek(state)) {
12411 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012412 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012413 if (!ident) {
12414 error(state, 0, "Unexpected identifier found");
12415 }
12416 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012417 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012418 break;
12419 case TOK_LPAREN:
12420 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012421 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012422 eat(state, TOK_RPAREN);
12423 break;
12424 default:
12425 if (need_ident) {
12426 error(state, 0, "Identifier expected");
12427 }
12428 break;
12429 }
12430 do {
12431 op = 1;
12432 arrays_complete(state, type);
12433 switch(peek(state)) {
12434 case TOK_LPAREN:
12435 eat(state, TOK_LPAREN);
12436 type = param_type_list(state, type);
12437 eat(state, TOK_RPAREN);
12438 break;
12439 case TOK_LBRACKET:
12440 {
12441 unsigned int qualifiers;
12442 struct triple *value;
12443 value = 0;
12444 eat(state, TOK_LBRACKET);
12445 if (peek(state) != TOK_RBRACKET) {
12446 value = constant_expr(state);
12447 integral(state, value);
12448 }
12449 eat(state, TOK_RBRACKET);
12450
12451 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12452 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12453 if (value) {
12454 type->elements = value->u.cval;
12455 free_triple(state, value);
12456 } else {
12457 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12458 op = 0;
12459 }
12460 }
12461 break;
12462 default:
12463 op = 0;
12464 break;
12465 }
12466 } while(op);
12467 if (outer) {
12468 struct type *inner;
12469 arrays_complete(state, type);
12470 FINISHME();
12471 for(inner = outer; inner->left; inner = inner->left)
12472 ;
12473 inner->left = type;
12474 type = outer;
12475 }
12476 return type;
12477}
12478
12479static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012480 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012481 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012482{
12483 while(peek(state) == TOK_STAR) {
12484 eat(state, TOK_STAR);
12485 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12486 }
Eric Biederman41203d92004-11-08 09:31:09 +000012487 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012488 return type;
12489}
12490
Eric Biedermanb138ac82003-04-22 18:44:01 +000012491static struct type *typedef_name(
12492 struct compile_state *state, unsigned int specifiers)
12493{
12494 struct hash_entry *ident;
12495 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012496 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012497 type = ident->sym_ident->type;
12498 specifiers |= type->type & QUAL_MASK;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012499 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012500 (type->type & (STOR_MASK | QUAL_MASK))) {
12501 type = clone_type(specifiers, type);
12502 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012503 return type;
12504}
12505
12506static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012507 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012508{
Eric Biederman83b991a2003-10-11 06:20:25 +000012509 struct hash_entry *ident;
12510 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012511 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012512 struct type *enum_type;
12513 enum_type = 0;
12514 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012515 eat(state, TOK_ENUM);
12516 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012517 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012518 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012519 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012520 base = 0;
12521 if (!ident || (peek(state) == TOK_LBRACE)) {
12522 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012523 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012524 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12525 enum_type->type_ident = ident;
12526 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012527 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012528 struct hash_entry *eident;
12529 struct triple *value;
12530 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012531 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012532 if (eident->sym_ident) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012533 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012534 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012535 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012536 eident->tok = TOK_ENUM_CONST;
12537 if (peek(state) == TOK_EQ) {
12538 struct triple *val;
12539 eat(state, TOK_EQ);
12540 val = constant_expr(state);
12541 integral(state, val);
12542 base = val->u.cval;
12543 }
12544 value = int_const(state, &int_type, base);
12545 symbol(state, eident, &eident->sym_ident, value, &int_type);
12546 entry = new_type(TYPE_LIST, 0, 0);
12547 entry->field_ident = eident;
12548 *next = entry;
12549 next = &entry->right;
12550 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012551 if (peek(state) == TOK_COMMA) {
12552 eat(state, TOK_COMMA);
12553 }
12554 } while(peek(state) != TOK_RBRACE);
12555 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012556 if (ident) {
12557 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12558 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012559 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012560 if (ident && ident->sym_tag &&
12561 ident->sym_tag->type &&
12562 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12563 enum_type = clone_type(spec, ident->sym_tag->type);
12564 }
12565 else if (ident && !enum_type) {
12566 error(state, 0, "enum %s undeclared", ident->name);
12567 }
12568 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012569}
12570
Eric Biedermanb138ac82003-04-22 18:44:01 +000012571static struct type *struct_declarator(
12572 struct compile_state *state, struct type *type, struct hash_entry **ident)
12573{
Eric Biederman90089602004-05-28 14:11:54 +000012574 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012575 type = declarator(state, type, ident, 1);
12576 }
Eric Biederman90089602004-05-28 14:11:54 +000012577 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012578 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012579 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012580 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012581 if (value->op != OP_INTCONST) {
12582 error(state, 0, "Invalid constant expression");
12583 }
12584 if (value->u.cval > size_of(state, type)) {
12585 error(state, 0, "bitfield larger than base type");
12586 }
12587 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12588 error(state, 0, "bitfield base not an integer type");
12589 }
12590 type = new_type(TYPE_BITFIELD, type, 0);
12591 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012592 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012593 return type;
12594}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012595
12596static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012597 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012598{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012599 struct type *struct_type;
12600 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012601 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012602 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012603 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012604 struct_type = 0;
12605 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012606 switch(peek(state)) {
12607 case TOK_STRUCT:
12608 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012609 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012610 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012611 break;
12612 case TOK_UNION:
12613 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012614 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012615 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012616 break;
12617 default:
12618 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012619 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012620 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012621 break;
12622 }
12623 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012624 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012625 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012626 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012627 if (!ident || (peek(state) == TOK_LBRACE)) {
12628 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012629 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012630 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012631 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012632 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012633 do {
12634 struct type *base_type;
12635 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012636 base_type = specifier_qualifier_list(state);
12637 do {
12638 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012639 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012640 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012641 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012642 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012643 if (peek(state) == TOK_COMMA) {
12644 done = 0;
12645 eat(state, TOK_COMMA);
12646 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012647 type = clone_type(0, type);
12648 type->field_ident = fident;
12649 if (*next) {
12650 *next = new_type(type_join, *next, type);
12651 next = &((*next)->right);
12652 } else {
12653 *next = type;
12654 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012655 } while(!done);
12656 eat(state, TOK_SEMI);
12657 } while(peek(state) != TOK_RBRACE);
12658 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012659 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012660 struct_type->type_ident = ident;
12661 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012662 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012663 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012664 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012665 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000012666 if (ident && ident->sym_tag &&
12667 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012668 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012669 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012670 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012671 else if (ident && !struct_type) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012672 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012673 (type_main == TYPE_STRUCT)?"struct" : "union",
12674 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012675 }
12676 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012677}
12678
12679static unsigned int storage_class_specifier_opt(struct compile_state *state)
12680{
12681 unsigned int specifiers;
12682 switch(peek(state)) {
12683 case TOK_AUTO:
12684 eat(state, TOK_AUTO);
12685 specifiers = STOR_AUTO;
12686 break;
12687 case TOK_REGISTER:
12688 eat(state, TOK_REGISTER);
12689 specifiers = STOR_REGISTER;
12690 break;
12691 case TOK_STATIC:
12692 eat(state, TOK_STATIC);
12693 specifiers = STOR_STATIC;
12694 break;
12695 case TOK_EXTERN:
12696 eat(state, TOK_EXTERN);
12697 specifiers = STOR_EXTERN;
12698 break;
12699 case TOK_TYPEDEF:
12700 eat(state, TOK_TYPEDEF);
12701 specifiers = STOR_TYPEDEF;
12702 break;
12703 default:
12704 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012705 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012706 }
12707 else {
12708 specifiers = STOR_AUTO;
12709 }
12710 }
12711 return specifiers;
12712}
12713
12714static unsigned int function_specifier_opt(struct compile_state *state)
12715{
12716 /* Ignore the inline keyword */
12717 unsigned int specifiers;
12718 specifiers = 0;
12719 switch(peek(state)) {
12720 case TOK_INLINE:
12721 eat(state, TOK_INLINE);
12722 specifiers = STOR_INLINE;
12723 }
12724 return specifiers;
12725}
12726
Eric Biederman90089602004-05-28 14:11:54 +000012727static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12728{
12729 int tok = peek(state);
12730 switch(tok) {
12731 case TOK_COMMA:
12732 case TOK_LPAREN:
12733 /* The empty attribute ignore it */
12734 break;
12735 case TOK_IDENT:
12736 case TOK_ENUM_CONST:
12737 case TOK_TYPE_NAME:
12738 {
12739 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012740 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012741
12742 if (ident == state->i_noinline) {
12743 if (attributes & ATTRIB_ALWAYS_INLINE) {
12744 error(state, 0, "both always_inline and noinline attribtes");
12745 }
12746 attributes |= ATTRIB_NOINLINE;
12747 }
12748 else if (ident == state->i_always_inline) {
12749 if (attributes & ATTRIB_NOINLINE) {
12750 error(state, 0, "both noinline and always_inline attribtes");
12751 }
12752 attributes |= ATTRIB_ALWAYS_INLINE;
12753 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012754 else if (ident == state->i_noreturn) {
12755 // attribute((noreturn)) does nothing (yet?)
12756 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012757 else if (ident == state->i_unused) {
12758 // attribute((unused)) does nothing (yet?)
12759 }
Stefan Reinauerec664bc2013-05-09 14:06:04 -070012760 else if (ident == state->i_packed) {
12761 // attribute((packed)) does nothing (yet?)
12762 }
Eric Biederman90089602004-05-28 14:11:54 +000012763 else {
12764 error(state, 0, "Unknown attribute:%s", ident->name);
12765 }
12766 break;
12767 }
12768 default:
12769 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12770 break;
12771 }
12772 return attributes;
12773}
12774
12775static unsigned int attribute_list(struct compile_state *state, unsigned type)
12776{
12777 type = attrib(state, type);
12778 while(peek(state) == TOK_COMMA) {
12779 eat(state, TOK_COMMA);
12780 type = attrib(state, type);
12781 }
12782 return type;
12783}
12784
12785static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12786{
12787 if (peek(state) == TOK_ATTRIBUTE) {
12788 eat(state, TOK_ATTRIBUTE);
12789 eat(state, TOK_LPAREN);
12790 eat(state, TOK_LPAREN);
12791 type = attribute_list(state, type);
12792 eat(state, TOK_RPAREN);
12793 eat(state, TOK_RPAREN);
12794 }
12795 return type;
12796}
12797
Eric Biedermanb138ac82003-04-22 18:44:01 +000012798static unsigned int type_qualifiers(struct compile_state *state)
12799{
12800 unsigned int specifiers;
12801 int done;
12802 done = 0;
12803 specifiers = QUAL_NONE;
12804 do {
12805 switch(peek(state)) {
12806 case TOK_CONST:
12807 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012808 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012809 break;
12810 case TOK_VOLATILE:
12811 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012812 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012813 break;
12814 case TOK_RESTRICT:
12815 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012816 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012817 break;
12818 default:
12819 done = 1;
12820 break;
12821 }
12822 } while(!done);
12823 return specifiers;
12824}
12825
12826static struct type *type_specifier(
12827 struct compile_state *state, unsigned int spec)
12828{
12829 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012830 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012831 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012832 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012833 case TOK_VOID:
12834 eat(state, TOK_VOID);
12835 type = new_type(TYPE_VOID | spec, 0, 0);
12836 break;
12837 case TOK_CHAR:
12838 eat(state, TOK_CHAR);
12839 type = new_type(TYPE_CHAR | spec, 0, 0);
12840 break;
12841 case TOK_SHORT:
12842 eat(state, TOK_SHORT);
12843 if (peek(state) == TOK_INT) {
12844 eat(state, TOK_INT);
12845 }
12846 type = new_type(TYPE_SHORT | spec, 0, 0);
12847 break;
12848 case TOK_INT:
12849 eat(state, TOK_INT);
12850 type = new_type(TYPE_INT | spec, 0, 0);
12851 break;
12852 case TOK_LONG:
12853 eat(state, TOK_LONG);
12854 switch(peek(state)) {
12855 case TOK_LONG:
12856 eat(state, TOK_LONG);
12857 error(state, 0, "long long not supported");
12858 break;
12859 case TOK_DOUBLE:
12860 eat(state, TOK_DOUBLE);
12861 error(state, 0, "long double not supported");
12862 break;
12863 case TOK_INT:
12864 eat(state, TOK_INT);
12865 type = new_type(TYPE_LONG | spec, 0, 0);
12866 break;
12867 default:
12868 type = new_type(TYPE_LONG | spec, 0, 0);
12869 break;
12870 }
12871 break;
12872 case TOK_FLOAT:
12873 eat(state, TOK_FLOAT);
12874 error(state, 0, "type float not supported");
12875 break;
12876 case TOK_DOUBLE:
12877 eat(state, TOK_DOUBLE);
12878 error(state, 0, "type double not supported");
12879 break;
12880 case TOK_SIGNED:
12881 eat(state, TOK_SIGNED);
12882 switch(peek(state)) {
12883 case TOK_LONG:
12884 eat(state, TOK_LONG);
12885 switch(peek(state)) {
12886 case TOK_LONG:
12887 eat(state, TOK_LONG);
12888 error(state, 0, "type long long not supported");
12889 break;
12890 case TOK_INT:
12891 eat(state, TOK_INT);
12892 type = new_type(TYPE_LONG | spec, 0, 0);
12893 break;
12894 default:
12895 type = new_type(TYPE_LONG | spec, 0, 0);
12896 break;
12897 }
12898 break;
12899 case TOK_INT:
12900 eat(state, TOK_INT);
12901 type = new_type(TYPE_INT | spec, 0, 0);
12902 break;
12903 case TOK_SHORT:
12904 eat(state, TOK_SHORT);
12905 type = new_type(TYPE_SHORT | spec, 0, 0);
12906 break;
12907 case TOK_CHAR:
12908 eat(state, TOK_CHAR);
12909 type = new_type(TYPE_CHAR | spec, 0, 0);
12910 break;
12911 default:
12912 type = new_type(TYPE_INT | spec, 0, 0);
12913 break;
12914 }
12915 break;
12916 case TOK_UNSIGNED:
12917 eat(state, TOK_UNSIGNED);
12918 switch(peek(state)) {
12919 case TOK_LONG:
12920 eat(state, TOK_LONG);
12921 switch(peek(state)) {
12922 case TOK_LONG:
12923 eat(state, TOK_LONG);
12924 error(state, 0, "unsigned long long not supported");
12925 break;
12926 case TOK_INT:
12927 eat(state, TOK_INT);
12928 type = new_type(TYPE_ULONG | spec, 0, 0);
12929 break;
12930 default:
12931 type = new_type(TYPE_ULONG | spec, 0, 0);
12932 break;
12933 }
12934 break;
12935 case TOK_INT:
12936 eat(state, TOK_INT);
12937 type = new_type(TYPE_UINT | spec, 0, 0);
12938 break;
12939 case TOK_SHORT:
12940 eat(state, TOK_SHORT);
12941 type = new_type(TYPE_USHORT | spec, 0, 0);
12942 break;
12943 case TOK_CHAR:
12944 eat(state, TOK_CHAR);
12945 type = new_type(TYPE_UCHAR | spec, 0, 0);
12946 break;
12947 default:
12948 type = new_type(TYPE_UINT | spec, 0, 0);
12949 break;
12950 }
12951 break;
12952 /* struct or union specifier */
12953 case TOK_STRUCT:
12954 case TOK_UNION:
12955 type = struct_or_union_specifier(state, spec);
12956 break;
12957 /* enum-spefifier */
12958 case TOK_ENUM:
12959 type = enum_specifier(state, spec);
12960 break;
12961 /* typedef name */
12962 case TOK_TYPE_NAME:
12963 type = typedef_name(state, spec);
12964 break;
12965 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012966 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012967 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012968 break;
12969 }
12970 return type;
12971}
12972
12973static int istype(int tok)
12974{
12975 switch(tok) {
12976 case TOK_CONST:
12977 case TOK_RESTRICT:
12978 case TOK_VOLATILE:
12979 case TOK_VOID:
12980 case TOK_CHAR:
12981 case TOK_SHORT:
12982 case TOK_INT:
12983 case TOK_LONG:
12984 case TOK_FLOAT:
12985 case TOK_DOUBLE:
12986 case TOK_SIGNED:
12987 case TOK_UNSIGNED:
12988 case TOK_STRUCT:
12989 case TOK_UNION:
12990 case TOK_ENUM:
12991 case TOK_TYPE_NAME:
12992 return 1;
12993 default:
12994 return 0;
12995 }
12996}
12997
12998
12999static struct type *specifier_qualifier_list(struct compile_state *state)
13000{
13001 struct type *type;
13002 unsigned int specifiers = 0;
13003
13004 /* type qualifiers */
13005 specifiers |= type_qualifiers(state);
13006
13007 /* type specifier */
13008 type = type_specifier(state, specifiers);
13009
13010 return type;
13011}
13012
Stefan Reinauer50542a82007-10-24 11:14:14 +000013013#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013014static int isdecl_specifier(int tok)
13015{
13016 switch(tok) {
13017 /* storage class specifier */
13018 case TOK_AUTO:
13019 case TOK_REGISTER:
13020 case TOK_STATIC:
13021 case TOK_EXTERN:
13022 case TOK_TYPEDEF:
13023 /* type qualifier */
13024 case TOK_CONST:
13025 case TOK_RESTRICT:
13026 case TOK_VOLATILE:
13027 /* type specifiers */
13028 case TOK_VOID:
13029 case TOK_CHAR:
13030 case TOK_SHORT:
13031 case TOK_INT:
13032 case TOK_LONG:
13033 case TOK_FLOAT:
13034 case TOK_DOUBLE:
13035 case TOK_SIGNED:
13036 case TOK_UNSIGNED:
13037 /* struct or union specifier */
13038 case TOK_STRUCT:
13039 case TOK_UNION:
13040 /* enum-spefifier */
13041 case TOK_ENUM:
13042 /* typedef name */
13043 case TOK_TYPE_NAME:
13044 /* function specifiers */
13045 case TOK_INLINE:
13046 return 1;
13047 default:
13048 return 0;
13049 }
13050}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013051#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013052
13053static struct type *decl_specifiers(struct compile_state *state)
13054{
13055 struct type *type;
13056 unsigned int specifiers;
13057 /* I am overly restrictive in the arragement of specifiers supported.
13058 * C is overly flexible in this department it makes interpreting
13059 * the parse tree difficult.
13060 */
13061 specifiers = 0;
13062
13063 /* storage class specifier */
13064 specifiers |= storage_class_specifier_opt(state);
13065
13066 /* function-specifier */
13067 specifiers |= function_specifier_opt(state);
13068
Eric Biederman90089602004-05-28 14:11:54 +000013069 /* attributes */
13070 specifiers |= attributes_opt(state, 0);
13071
Eric Biedermanb138ac82003-04-22 18:44:01 +000013072 /* type qualifier */
13073 specifiers |= type_qualifiers(state);
13074
13075 /* type specifier */
13076 type = type_specifier(state, specifiers);
13077 return type;
13078}
13079
Eric Biederman00443072003-06-24 12:34:45 +000013080struct field_info {
13081 struct type *type;
13082 size_t offset;
13083};
13084
13085static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013086{
13087 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013088 struct field_info info;
13089 info.offset = ~0U;
13090 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013091 do {
13092 switch(peek(state)) {
13093 case TOK_LBRACKET:
13094 {
13095 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013096 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13097 error(state, 0, "Array designator not in array initializer");
13098 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013099 eat(state, TOK_LBRACKET);
13100 value = constant_expr(state);
13101 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013102
13103 info.type = type->left;
13104 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013105 break;
13106 }
13107 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013108 {
13109 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013110 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13111 ((type->type & TYPE_MASK) != TYPE_UNION))
13112 {
Eric Biederman00443072003-06-24 12:34:45 +000013113 error(state, 0, "Struct designator not in struct initializer");
13114 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013116 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013117 info.offset = field_offset(state, type, field);
13118 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013119 break;
Eric Biederman00443072003-06-24 12:34:45 +000013120 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013121 default:
13122 error(state, 0, "Invalid designator");
13123 }
13124 tok = peek(state);
13125 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13126 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013127 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013128}
13129
13130static struct triple *initializer(
13131 struct compile_state *state, struct type *type)
13132{
13133 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013134#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013135#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013136#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013137 if (peek(state) != TOK_LBRACE) {
13138 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013139 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13140 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13141 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13142 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13143 (equiv_types(type->left, result->type->left))) {
13144 type->elements = result->type->elements;
13145 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013146 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013147 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13148 (type->type & TYPE_MASK) != TYPE_ARRAY)
13149 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013150 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013151 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013152 if (!is_init_compatible(state, type, result->type)) {
13153 error(state, 0, "Incompatible types in initializer");
13154 }
13155 if (!equiv_types(type, result->type)) {
13156 result = mk_cast_expr(state, type, result);
13157 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013158 }
13159 else {
13160 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013161 size_t max_offset;
13162 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013163 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013164 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13165 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13166 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013167 }
Eric Biederman00443072003-06-24 12:34:45 +000013168 info.offset = 0;
13169 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013170 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13171 info.type = next_field(state, type, 0);
13172 }
Eric Biederman00443072003-06-24 12:34:45 +000013173 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13174 max_offset = 0;
13175 } else {
13176 max_offset = size_of(state, type);
13177 }
Eric Biederman90089602004-05-28 14:11:54 +000013178 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013179 eat(state, TOK_LBRACE);
13180 do {
13181 struct triple *value;
13182 struct type *value_type;
13183 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013184 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013185 int tok;
13186 comma = 0;
13187 tok = peek(state);
13188 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013189 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013190 }
Eric Biederman00443072003-06-24 12:34:45 +000013191 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13192 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013193 error(state, 0, "element beyond bounds");
13194 }
Eric Biederman00443072003-06-24 12:34:45 +000013195 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013196 value = eval_const_expr(state, initializer(state, value_type));
13197 value_size = size_of(state, value_type);
13198 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013199 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13200 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013201 void *old_buf;
13202 size_t old_size;
13203 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013204 old_size = max_offset;
13205 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013206 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13207 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013208 xfree(old_buf);
13209 }
Eric Biederman90089602004-05-28 14:11:54 +000013210 dest = ((char *)buf) + bits_to_bytes(info.offset);
13211#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013212 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013213 dest - buf,
13214 bits_to_bytes(max_offset),
13215 bits_to_bytes(value_size),
13216 value->op);
13217#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013218 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013219 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013220 }
Eric Biederman90089602004-05-28 14:11:54 +000013221 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13222#if DEBUG_INITIALIZER
13223 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13224#endif
Eric Biederman00443072003-06-24 12:34:45 +000013225 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013226 }
Eric Biederman90089602004-05-28 14:11:54 +000013227 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013228 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013229 }
Eric Biederman90089602004-05-28 14:11:54 +000013230 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013231 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013232 }
13233 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013234 internal_error(state, 0, "unhandled constant initializer");
13235 }
Eric Biederman00443072003-06-24 12:34:45 +000013236 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013237 if (peek(state) == TOK_COMMA) {
13238 eat(state, TOK_COMMA);
13239 comma = 1;
13240 }
Eric Biederman00443072003-06-24 12:34:45 +000013241 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013242 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13243 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013244 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013245 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013246 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013247 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013248 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13249 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13250 type->elements = max_offset / size_of(state, type->left);
13251 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013252 eat(state, TOK_RBRACE);
13253 result = triple(state, OP_BLOBCONST, type, 0, 0);
13254 result->u.blob = buf;
13255 }
13256 return result;
13257}
13258
Eric Biederman90089602004-05-28 14:11:54 +000013259static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013260{
13261 /* Make a second pass and finish anything outstanding
13262 * with respect to branches. The only outstanding item
13263 * is to see if there are goto to labels that have not
13264 * been defined and to error about them.
13265 */
13266 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013267 struct triple *ins;
13268 /* Also error on branches that do not use their targets */
13269 ins = first;
13270 do {
13271 if (!triple_is_ret(state, ins)) {
13272 struct triple **expr ;
13273 struct triple_set *set;
13274 expr = triple_targ(state, ins, 0);
13275 for(; expr; expr = triple_targ(state, ins, expr)) {
13276 struct triple *targ;
13277 targ = *expr;
13278 for(set = targ?targ->use:0; set; set = set->next) {
13279 if (set->member == ins) {
13280 break;
13281 }
13282 }
13283 if (!set) {
13284 internal_error(state, ins, "targ not used");
13285 }
13286 }
13287 }
13288 ins = ins->next;
13289 } while(ins != first);
13290 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013291 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13292 struct hash_entry *entry;
13293 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13294 struct triple *ins;
13295 if (!entry->sym_label) {
13296 continue;
13297 }
13298 ins = entry->sym_label->def;
13299 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13300 error(state, ins, "label `%s' used but not defined",
13301 entry->name);
13302 }
13303 }
13304 }
13305}
13306
Eric Biedermanb138ac82003-04-22 18:44:01 +000013307static struct triple *function_definition(
13308 struct compile_state *state, struct type *type)
13309{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013310 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013311 struct triple *fname;
13312 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013313 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013314 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013315 int i;
13316 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13317 error(state, 0, "Invalid function header");
13318 }
13319
13320 /* Verify the function type */
13321 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13322 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013323 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013324 error(state, 0, "Invalid function parameters");
13325 }
13326 param = type->right;
13327 i = 0;
13328 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13329 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013330 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013331 error(state, 0, "No identifier for parameter %d\n", i);
13332 }
13333 param = param->right;
13334 }
13335 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013336 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013337 error(state, 0, "No identifier for paramter %d\n", i);
13338 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013339
Eric Biedermanb138ac82003-04-22 18:44:01 +000013340 /* Get a list of statements for this function. */
13341 def = triple(state, OP_LIST, type, 0, 0);
13342
13343 /* Start a new scope for the passed parameters */
13344 start_scope(state);
13345
13346 /* Put a label at the very start of a function */
13347 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013348 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013349
13350 /* Put a label at the very end of a function */
13351 end = label(state);
13352 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013353 /* Remember where return goes */
13354 ident = state->i_return;
13355 symbol(state, ident, &ident->sym_ident, end, end->type);
13356
Eric Biederman90089602004-05-28 14:11:54 +000013357 /* Get the initial closure type */
13358 ctype = new_type(TYPE_JOIN, &void_type, 0);
13359 ctype->elements = 1;
13360
13361 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013362 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013363 /* Remove all type qualifiers from the return type */
13364 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13365 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013366 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013367
Eric Biederman5ade04a2003-10-22 04:03:46 +000013368 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013369 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013370
13371 /* Add in the return instruction */
13372 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13373 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013374
13375 /* Walk through the parameters and create symbol table entries
13376 * for them.
13377 */
13378 param = type->right;
13379 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013380 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013381 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013382 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013383 flatten(state, end, tmp);
13384 param = param->right;
13385 }
13386 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13387 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013388 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013389 tmp = variable(state, param);
13390 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13391 flatten(state, end, tmp);
13392 }
Eric Biederman90089602004-05-28 14:11:54 +000013393
13394 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013395 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013396 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13397 fname_type->type |= QUAL_CONST | STOR_STATIC;
13398 fname_type->elements = strlen(state->function) + 1;
13399
13400 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13401 fname->u.blob = (void *)state->function;
13402 fname = flatten(state, end, fname);
13403
13404 ident = state->i___func__;
13405 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013406
13407 /* Remember which function I am compiling.
13408 * Also assume the last defined function is the main function.
13409 */
13410 state->main_function = def;
13411
13412 /* Now get the actual function definition */
13413 compound_statement(state, end);
13414
Eric Biederman153ea352003-06-20 14:43:20 +000013415 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013416 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013417
Eric Biedermanb138ac82003-04-22 18:44:01 +000013418 /* Remove the parameter scope */
13419 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013420
Eric Biederman5ade04a2003-10-22 04:03:46 +000013421
13422 /* Remember I have defined a function */
13423 if (!state->functions) {
13424 state->functions = def;
13425 } else {
13426 insert_triple(state, state->functions, def);
13427 }
13428 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013429 FILE *fp = state->dbgout;
13430 fprintf(fp, "\n");
13431 loc(fp, state, 0);
13432 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13433 display_func(state, fp, def);
13434 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013435 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013436
13437 return def;
13438}
13439
Stefan Reinauer14e22772010-04-27 06:56:47 +000013440static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013441 struct type *type, struct hash_entry *ident)
13442{
13443 struct triple *def;
13444 def = 0;
13445 /* Clean up the storage types used */
13446 switch (type->type & STOR_MASK) {
13447 case STOR_AUTO:
13448 case STOR_STATIC:
13449 /* These are the good types I am aiming for */
13450 break;
13451 case STOR_REGISTER:
13452 type->type &= ~STOR_MASK;
13453 type->type |= STOR_AUTO;
13454 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013455 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013456 case STOR_EXTERN:
13457 type->type &= ~STOR_MASK;
13458 type->type |= STOR_STATIC;
13459 break;
13460 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013461 if (!ident) {
13462 error(state, 0, "typedef without name");
13463 }
13464 symbol(state, ident, &ident->sym_ident, 0, type);
13465 ident->tok = TOK_TYPE_NAME;
13466 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013467 break;
13468 default:
13469 internal_error(state, 0, "Undefined storage class");
13470 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013471 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
Patrick Georgi11a7db32013-02-16 20:16:34 +010013472 // ignore function prototypes
13473 return def;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013474 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013475 if (ident &&
13476 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13477 ((type->type & STOR_MASK) != STOR_STATIC))
13478 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013479 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013480 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013481 ((type->type & QUAL_CONST) == 0)) {
13482 error(state, 0, "non const static variables not supported");
13483 }
13484 if (ident) {
13485 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013486 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013487 }
13488 return def;
13489}
13490
13491static void decl(struct compile_state *state, struct triple *first)
13492{
13493 struct type *base_type, *type;
13494 struct hash_entry *ident;
13495 struct triple *def;
13496 int global;
13497 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13498 base_type = decl_specifiers(state);
13499 ident = 0;
13500 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013501 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013502 if (global && ident && (peek(state) == TOK_LBRACE)) {
13503 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013504 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013505 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013506 def = function_definition(state, type);
13507 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013508 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013509 }
13510 else {
13511 int done;
13512 flatten(state, first, do_decl(state, type, ident));
13513 /* type or variable definition */
13514 do {
13515 done = 1;
13516 if (peek(state) == TOK_EQ) {
13517 if (!ident) {
13518 error(state, 0, "cannot assign to a type");
13519 }
13520 eat(state, TOK_EQ);
13521 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013522 init_expr(state,
13523 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013524 initializer(state, type)));
13525 }
13526 arrays_complete(state, type);
13527 if (peek(state) == TOK_COMMA) {
13528 eat(state, TOK_COMMA);
13529 ident = 0;
13530 type = declarator(state, base_type, &ident, 0);
13531 flatten(state, first, do_decl(state, type, ident));
13532 done = 0;
13533 }
13534 } while(!done);
13535 eat(state, TOK_SEMI);
13536 }
13537}
13538
13539static void decls(struct compile_state *state)
13540{
13541 struct triple *list;
13542 int tok;
13543 list = label(state);
13544 while(1) {
13545 tok = peek(state);
13546 if (tok == TOK_EOF) {
13547 return;
13548 }
13549 if (tok == TOK_SPACE) {
13550 eat(state, TOK_SPACE);
13551 }
13552 decl(state, list);
13553 if (list->next != list) {
13554 error(state, 0, "global variables not supported");
13555 }
13556 }
13557}
13558
Stefan Reinauer14e22772010-04-27 06:56:47 +000013559/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013560 * Function inlining
13561 */
Eric Biederman90089602004-05-28 14:11:54 +000013562struct triple_reg_set {
13563 struct triple_reg_set *next;
13564 struct triple *member;
13565 struct triple *new;
13566};
13567struct reg_block {
13568 struct block *block;
13569 struct triple_reg_set *in;
13570 struct triple_reg_set *out;
13571 int vertex;
13572};
13573static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13574static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13575static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13576static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13577static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13578 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13579 void *arg);
13580static void print_block(
13581 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013582static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013583 struct triple *member, struct triple *new_member);
13584static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13585static struct reg_block *compute_variable_lifetimes(
13586 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013587static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013588 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013589#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013590static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013591 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013592#endif
Eric Biederman90089602004-05-28 14:11:54 +000013593
Eric Biederman5ade04a2003-10-22 04:03:46 +000013594
13595static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013596 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013597 struct triple *targ, struct triple *ret)
13598{
13599 struct triple *call;
13600
13601 if (!retvar || !is_lvalue(state, retvar)) {
13602 internal_error(state, 0, "writing to a non lvalue?");
13603 }
13604 write_compatible(state, retvar->type, &void_ptr_type);
13605
13606 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13607 TARG(call, 0) = targ;
13608 MISC(call, 0) = ret;
13609 if (!targ || (targ->op != OP_LABEL)) {
13610 internal_error(state, 0, "call not to a label");
13611 }
13612 if (!ret || (ret->op != OP_RET)) {
13613 internal_error(state, 0, "call not matched with return");
13614 }
13615 return call;
13616}
13617
Eric Biederman5ade04a2003-10-22 04:03:46 +000013618static void walk_functions(struct compile_state *state,
13619 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13620 void *arg)
13621{
13622 struct triple *func, *first;
13623 func = first = state->functions;
13624 do {
13625 cb(state, func, arg);
13626 func = func->next;
13627 } while(func != first);
13628}
13629
Eric Biederman90089602004-05-28 14:11:54 +000013630static void reverse_walk_functions(struct compile_state *state,
13631 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13632 void *arg)
13633{
13634 struct triple *func, *first;
13635 func = first = state->functions;
13636 do {
13637 func = func->prev;
13638 cb(state, func, arg);
13639 } while(func != first);
13640}
13641
13642
13643static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13644{
13645 struct triple *ptr, *first;
13646 if (func->u.cval == 0) {
13647 return;
13648 }
13649 ptr = first = RHS(func, 0);
13650 do {
13651 if (ptr->op == OP_FCALL) {
13652 struct triple *called_func;
13653 called_func = MISC(ptr, 0);
13654 /* Mark the called function as used */
13655 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13656 called_func->u.cval++;
13657 }
13658 /* Remove the called function from the list */
13659 called_func->prev->next = called_func->next;
13660 called_func->next->prev = called_func->prev;
13661
13662 /* Place the called function before me on the list */
13663 called_func->next = func;
13664 called_func->prev = func->prev;
13665 called_func->prev->next = called_func;
13666 called_func->next->prev = called_func;
13667 }
13668 ptr = ptr->next;
13669 } while(ptr != first);
13670 func->id |= TRIPLE_FLAG_FLATTENED;
13671}
13672
13673static void mark_live_functions(struct compile_state *state)
13674{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013675 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013676 * the list of functions.
13677 */
13678 if ((state->main_function->next != state->functions) ||
13679 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013680 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013681 "state->main_function is not at the end of the function list ");
13682 }
13683 state->main_function->u.cval = 1;
13684 reverse_walk_functions(state, mark_live, 0);
13685}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013686
Stefan Reinauer14e22772010-04-27 06:56:47 +000013687static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013688 struct triple *func, struct triple *ins)
13689{
13690 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13691#if 0
13692 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013693 FILE *fp = state->errout;
13694 fprintf(fp, "global: ");
13695 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013696 }
13697#endif
13698 return local;
13699}
13700
Stefan Reinauer14e22772010-04-27 06:56:47 +000013701struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013702 struct occurance *base_occurance)
13703{
13704 struct triple *nfunc;
13705 struct triple *nfirst, *ofirst;
13706 struct triple *new, *old;
13707
13708 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013709 FILE *fp = state->dbgout;
13710 fprintf(fp, "\n");
13711 loc(fp, state, 0);
13712 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13713 display_func(state, fp, ofunc);
13714 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013715 }
13716
13717 /* Make a new copy of the old function */
13718 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13719 nfirst = 0;
13720 ofirst = old = RHS(ofunc, 0);
13721 do {
13722 struct triple *new;
13723 struct occurance *occurance;
13724 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013725 old_lhs = old->lhs;
13726 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013727 occurance = inline_occurance(state, base_occurance, old->occurance);
13728 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13729 MISC(old, 0)->u.cval += 1;
13730 }
13731 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13732 occurance);
13733 if (!triple_stores_block(state, new)) {
13734 memcpy(&new->u, &old->u, sizeof(new->u));
13735 }
13736 if (!nfirst) {
13737 RHS(nfunc, 0) = nfirst = new;
13738 }
13739 else {
13740 insert_triple(state, nfirst, new);
13741 }
13742 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013743 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013744
Eric Biederman5ade04a2003-10-22 04:03:46 +000013745 /* During the copy remember new as user of old */
13746 use_triple(old, new);
13747
Eric Biederman5ade04a2003-10-22 04:03:46 +000013748 /* Remember which instructions are local */
13749 old->id |= TRIPLE_FLAG_LOCAL;
13750 old = old->next;
13751 } while(old != ofirst);
13752
13753 /* Make a second pass to fix up any unresolved references */
13754 old = ofirst;
13755 new = nfirst;
13756 do {
13757 struct triple **oexpr, **nexpr;
13758 int count, i;
13759 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013760 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013761 for(i = 0; i < count; i++) {
13762 oexpr = &old->param[i];
13763 nexpr = &new->param[i];
13764 if (*oexpr && !*nexpr) {
13765 if (!local_triple(state, ofunc, *oexpr)) {
13766 *nexpr = *oexpr;
13767 }
13768 else if ((*oexpr)->use) {
13769 *nexpr = (*oexpr)->use->member;
13770 }
13771 if (*nexpr == old) {
13772 internal_error(state, 0, "new == old?");
13773 }
13774 use_triple(*nexpr, new);
13775 }
13776 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013777 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013778 }
13779 }
13780 old = old->next;
13781 new = new->next;
13782 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013783
Eric Biederman5ade04a2003-10-22 04:03:46 +000013784 /* Make a third pass to cleanup the extra useses */
13785 old = ofirst;
13786 new = nfirst;
13787 do {
13788 unuse_triple(old, new);
13789 /* Forget which instructions are local */
13790 old->id &= ~TRIPLE_FLAG_LOCAL;
13791 old = old->next;
13792 new = new->next;
13793 } while ((old != ofirst) && (new != nfirst));
13794 return nfunc;
13795}
13796
Eric Biederman90089602004-05-28 14:11:54 +000013797static void expand_inline_call(
13798 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013799{
13800 /* Inline the function call */
13801 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013802 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013803 struct triple *end, *nend;
13804 int pvals, i;
13805
13806 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013807 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013808 if (ofunc->op != OP_LIST) {
13809 internal_error(state, 0, "improper function");
13810 }
Eric Biederman90089602004-05-28 14:11:54 +000013811 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013812 /* Prepend the parameter reading into the new function list */
13813 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013814 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013815 for(i = 0; i < pvals; i++) {
13816 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013817 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013818 atype = ptype;
13819 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13820 atype = ptype->left;
13821 }
Eric Biederman90089602004-05-28 14:11:54 +000013822 param = farg(state, nfunc, i);
13823 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13824 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013825 }
Eric Biederman90089602004-05-28 14:11:54 +000013826 arg = RHS(fcall, i);
13827 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013828 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013829 }
13830 result = 0;
13831 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013832 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013833 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013834 }
13835 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013836 FILE *fp = state->dbgout;
13837 fprintf(fp, "\n");
13838 loc(fp, state, 0);
13839 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13840 display_func(state, fp, nfunc);
13841 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013842 }
13843
Stefan Reinauer14e22772010-04-27 06:56:47 +000013844 /*
13845 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013846 */
13847 /* Remove the read of the return address */
13848 ins = RHS(nfunc, 0)->prev->prev;
13849 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13850 internal_error(state, ins, "Not return addres read?");
13851 }
13852 release_triple(state, ins);
13853 /* Remove the return instruction */
13854 ins = RHS(nfunc, 0)->prev;
13855 if (ins->op != OP_RET) {
13856 internal_error(state, ins, "Not return?");
13857 }
13858 release_triple(state, ins);
13859 /* Remove the retaddres variable */
13860 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013861 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013862 (retvar->op != OP_ADECL) ||
13863 (retvar->next->op != OP_PIECE) ||
13864 (MISC(retvar->next, 0) != retvar)) {
13865 internal_error(state, retvar, "Not the return address?");
13866 }
13867 release_triple(state, retvar->next);
13868 release_triple(state, retvar);
13869
13870 /* Remove the label at the start of the function */
13871 ins = RHS(nfunc, 0);
13872 if (ins->op != OP_LABEL) {
13873 internal_error(state, ins, "Not label?");
13874 }
13875 nfirst = ins->next;
13876 free_triple(state, ins);
13877 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013878 RHS(nfunc, 0) = 0;
13879 free_triple(state, nfunc);
13880
13881 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013882 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013883 nend = nfirst->prev;
13884 end->next = nfirst;
13885 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013886 nend->next = fcall;
13887 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013888
Eric Biederman90089602004-05-28 14:11:54 +000013889 /* Now the result reading code */
13890 if (result) {
13891 result = flatten(state, fcall, result);
13892 propogate_use(state, fcall, result);
13893 }
13894
13895 /* Release the original fcall instruction */
13896 release_triple(state, fcall);
13897
13898 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013899}
13900
Eric Biederman90089602004-05-28 14:11:54 +000013901/*
13902 *
13903 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013904 *
Eric Biederman90089602004-05-28 14:11:54 +000013905 * result
13906 * |
13907 * +----------+------------+
13908 * | |
13909 * union of closures result_type
13910 * |
13911 * +------------------+---------------+
13912 * | |
13913 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013914 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013915 * +----+--+-+--------+-----+ +----+----+---+-----+
13916 * | | | | | | | | |
13917 * var1 var2 var3 ... varN result var1 var2 ... varN result
13918 * |
13919 * +--------+---------+
13920 * | |
13921 * union of closures result_type
13922 * |
13923 * +-----+-------------------+
13924 * | |
13925 * closure1 ... closureN
13926 * | |
13927 * +-----+---+----+----+ +----+---+----+-----+
13928 * | | | | | | | |
13929 * var1 var2 ... varN result var1 var2 ... varN result
13930 */
13931
Stefan Reinauer14e22772010-04-27 06:56:47 +000013932static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013933 struct triple *func, struct type *closure_type)
13934{
13935 struct type *type, *ctype, **next;
13936 struct triple *var, *new_var;
13937 int i;
13938
13939#if 0
13940 FILE *fp = state->errout;
13941 fprintf(fp, "original_type: ");
13942 name_of(fp, fresult(state, func)->type);
13943 fprintf(fp, "\n");
13944#endif
13945 /* find the original type */
13946 var = fresult(state, func);
13947 type = var->type;
13948 if (type->elements != 2) {
13949 internal_error(state, var, "bad return type");
13950 }
13951
13952 /* Find the complete closure type and update it */
13953 ctype = type->left->left;
13954 next = &ctype->left;
13955 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13956 next = &(*next)->right;
13957 }
13958 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13959 ctype->elements += 1;
13960
13961#if 0
13962 fprintf(fp, "new_type: ");
13963 name_of(fp, type);
13964 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013965 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013966 ctype, ctype->elements, reg_size_of(state, ctype));
13967 name_of(fp, ctype);
13968 fprintf(fp, "\n");
13969#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013970
Eric Biederman90089602004-05-28 14:11:54 +000013971 /* Regenerate the variable with the new type definition */
13972 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13973 new_var->id |= TRIPLE_FLAG_FLATTENED;
13974 for(i = 0; i < new_var->lhs; i++) {
13975 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13976 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013977
Eric Biederman90089602004-05-28 14:11:54 +000013978 /* Point everyone at the new variable */
13979 propogate_use(state, var, new_var);
13980
13981 /* Release the original variable */
13982 for(i = 0; i < var->lhs; i++) {
13983 release_triple(state, LHS(var, i));
13984 }
13985 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013986
Eric Biederman90089602004-05-28 14:11:54 +000013987 /* Return the index of the added closure type */
13988 return ctype->elements - 1;
13989}
13990
13991static struct triple *closure_expr(struct compile_state *state,
13992 struct triple *func, int closure_idx, int var_idx)
13993{
13994 return deref_index(state,
13995 deref_index(state,
13996 deref_index(state, fresult(state, func), 0),
13997 closure_idx),
13998 var_idx);
13999}
14000
14001
14002static void insert_triple_set(
14003 struct triple_reg_set **head, struct triple *member)
14004{
14005 struct triple_reg_set *new;
14006 new = xcmalloc(sizeof(*new), "triple_set");
14007 new->member = member;
14008 new->new = 0;
14009 new->next = *head;
14010 *head = new;
14011}
14012
14013static int ordered_triple_set(
14014 struct triple_reg_set **head, struct triple *member)
14015{
14016 struct triple_reg_set **ptr;
14017 if (!member)
14018 return 0;
14019 ptr = head;
14020 while(*ptr) {
14021 if (member == (*ptr)->member) {
14022 return 0;
14023 }
14024 /* keep the list ordered */
14025 if (member->id < (*ptr)->member->id) {
14026 break;
14027 }
14028 ptr = &(*ptr)->next;
14029 }
14030 insert_triple_set(ptr, member);
14031 return 1;
14032}
14033
14034
14035static void free_closure_variables(struct compile_state *state,
14036 struct triple_reg_set **enclose)
14037{
14038 struct triple_reg_set *entry, *next;
14039 for(entry = *enclose; entry; entry = next) {
14040 next = entry->next;
14041 do_triple_unset(enclose, entry->member);
14042 }
14043}
14044
14045static int lookup_closure_index(struct compile_state *state,
14046 struct triple *me, struct triple *val)
14047{
14048 struct triple *first, *ins, *next;
14049 first = RHS(me, 0);
14050 ins = next = first;
14051 do {
14052 struct triple *result;
14053 struct triple *index0, *index1, *index2, *read, *write;
14054 ins = next;
14055 next = ins->next;
14056 if (ins->op != OP_CALL) {
14057 continue;
14058 }
14059 /* I am at a previous call point examine it closely */
14060 if (ins->next->op != OP_LABEL) {
14061 internal_error(state, ins, "call not followed by label");
14062 }
14063 /* Does this call does not enclose any variables? */
14064 if ((ins->next->next->op != OP_INDEX) ||
14065 (ins->next->next->u.cval != 0) ||
14066 (result = MISC(ins->next->next, 0)) ||
14067 (result->id & TRIPLE_FLAG_LOCAL)) {
14068 continue;
14069 }
14070 index0 = ins->next->next;
14071 /* The pattern is:
14072 * 0 index result < 0 >
14073 * 1 index 0 < ? >
14074 * 2 index 1 < ? >
14075 * 3 read 2
14076 * 4 write 3 var
14077 */
14078 for(index0 = ins->next->next;
14079 (index0->op == OP_INDEX) &&
14080 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014081 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014082 index0 = write->next)
14083 {
14084 index1 = index0->next;
14085 index2 = index1->next;
14086 read = index2->next;
14087 write = read->next;
14088 if ((index0->op != OP_INDEX) ||
14089 (index1->op != OP_INDEX) ||
14090 (index2->op != OP_INDEX) ||
14091 (read->op != OP_READ) ||
14092 (write->op != OP_WRITE) ||
14093 (MISC(index1, 0) != index0) ||
14094 (MISC(index2, 0) != index1) ||
14095 (RHS(read, 0) != index2) ||
14096 (RHS(write, 0) != read)) {
14097 internal_error(state, index0, "bad var read");
14098 }
14099 if (MISC(write, 0) == val) {
14100 return index2->u.cval;
14101 }
14102 }
14103 } while(next != first);
14104 return -1;
14105}
14106
14107static inline int enclose_triple(struct triple *ins)
14108{
14109 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14110}
14111
14112static void compute_closure_variables(struct compile_state *state,
14113 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14114{
14115 struct triple_reg_set *set, *vars, **last_var;
14116 struct basic_blocks bb;
14117 struct reg_block *rb;
14118 struct block *block;
14119 struct triple *old_result, *first, *ins;
14120 size_t count, idx;
14121 unsigned long used_indicies;
14122 int i, max_index;
14123#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14124#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014125 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014126 unsigned id;
14127 int index;
14128 } *info;
14129
Stefan Reinauer14e22772010-04-27 06:56:47 +000014130
Eric Biederman90089602004-05-28 14:11:54 +000014131 /* Find the basic blocks of this function */
14132 bb.func = me;
14133 bb.first = RHS(me, 0);
14134 old_result = 0;
14135 if (!triple_is_ret(state, bb.first->prev)) {
14136 bb.func = 0;
14137 } else {
14138 old_result = fresult(state, me);
14139 }
14140 analyze_basic_blocks(state, &bb);
14141
14142 /* Find which variables are currently alive in a given block */
14143 rb = compute_variable_lifetimes(state, &bb);
14144
14145 /* Find the variables that are currently alive */
14146 block = block_of_triple(state, fcall);
14147 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14148 internal_error(state, fcall, "No reg block? block: %p", block);
14149 }
14150
14151#if DEBUG_EXPLICIT_CLOSURES
14152 print_live_variables(state, &bb, rb, state->dbgout);
14153 fflush(state->dbgout);
14154#endif
14155
14156 /* Count the number of triples in the function */
14157 first = RHS(me, 0);
14158 ins = first;
14159 count = 0;
14160 do {
14161 count++;
14162 ins = ins->next;
14163 } while(ins != first);
14164
14165 /* Allocate some memory to temorary hold the id info */
14166 info = xcmalloc(sizeof(*info) * (count +1), "info");
14167
14168 /* Mark the local function */
14169 first = RHS(me, 0);
14170 ins = first;
14171 idx = 1;
14172 do {
14173 info[idx].id = ins->id;
14174 ins->id = TRIPLE_FLAG_LOCAL | idx;
14175 idx++;
14176 ins = ins->next;
14177 } while(ins != first);
14178
Stefan Reinauer14e22772010-04-27 06:56:47 +000014179 /*
Eric Biederman90089602004-05-28 14:11:54 +000014180 * Build the list of variables to enclose.
14181 *
14182 * A target it to put the same variable in the
14183 * same slot for ever call of a given function.
14184 * After coloring this removes all of the variable
14185 * manipulation code.
14186 *
14187 * The list of variables to enclose is built ordered
14188 * program order because except in corner cases this
14189 * gives me the stability of assignment I need.
14190 *
14191 * To gurantee that stability I lookup the variables
14192 * to see where they have been used before and
14193 * I build my final list with the assigned indicies.
14194 */
14195 vars = 0;
14196 if (enclose_triple(old_result)) {
14197 ordered_triple_set(&vars, old_result);
14198 }
14199 for(set = rb[block->vertex].out; set; set = set->next) {
14200 if (!enclose_triple(set->member)) {
14201 continue;
14202 }
14203 if ((set->member == fcall) || (set->member == old_result)) {
14204 continue;
14205 }
14206 if (!local_triple(state, me, set->member)) {
14207 internal_error(state, set->member, "not local?");
14208 }
14209 ordered_triple_set(&vars, set->member);
14210 }
14211
14212 /* Lookup the current indicies of the live varialbe */
14213 used_indicies = 0;
14214 max_index = -1;
14215 for(set = vars; set ; set = set->next) {
14216 struct triple *ins;
14217 int index;
14218 ins = set->member;
14219 index = lookup_closure_index(state, me, ins);
14220 info[ID_BITS(ins->id)].index = index;
14221 if (index < 0) {
14222 continue;
14223 }
14224 if (index >= MAX_INDICIES) {
14225 internal_error(state, ins, "index unexpectedly large");
14226 }
14227 if (used_indicies & (1 << index)) {
14228 internal_error(state, ins, "index previously used?");
14229 }
14230 /* Remember which indicies have been used */
14231 used_indicies |= (1 << index);
14232 if (index > max_index) {
14233 max_index = index;
14234 }
14235 }
14236
14237 /* Walk through the live variables and make certain
14238 * everything is assigned an index.
14239 */
14240 for(set = vars; set; set = set->next) {
14241 struct triple *ins;
14242 int index;
14243 ins = set->member;
14244 index = info[ID_BITS(ins->id)].index;
14245 if (index >= 0) {
14246 continue;
14247 }
14248 /* Find the lowest unused index value */
14249 for(index = 0; index < MAX_INDICIES; index++) {
14250 if (!(used_indicies & (1 << index))) {
14251 break;
14252 }
14253 }
14254 if (index == MAX_INDICIES) {
14255 internal_error(state, ins, "no free indicies?");
14256 }
14257 info[ID_BITS(ins->id)].index = index;
14258 /* Remember which indicies have been used */
14259 used_indicies |= (1 << index);
14260 if (index > max_index) {
14261 max_index = index;
14262 }
14263 }
14264
14265 /* Build the return list of variables with positions matching
14266 * their indicies.
14267 */
14268 *enclose = 0;
14269 last_var = enclose;
14270 for(i = 0; i <= max_index; i++) {
14271 struct triple *var;
14272 var = 0;
14273 if (used_indicies & (1 << i)) {
14274 for(set = vars; set; set = set->next) {
14275 int index;
14276 index = info[ID_BITS(set->member->id)].index;
14277 if (index == i) {
14278 var = set->member;
14279 break;
14280 }
14281 }
14282 if (!var) {
14283 internal_error(state, me, "missing variable");
14284 }
14285 }
14286 insert_triple_set(last_var, var);
14287 last_var = &(*last_var)->next;
14288 }
14289
14290#if DEBUG_EXPLICIT_CLOSURES
14291 /* Print out the variables to be enclosed */
14292 loc(state->dbgout, state, fcall);
14293 fprintf(state->dbgout, "Alive: \n");
14294 for(set = *enclose; set; set = set->next) {
14295 display_triple(state->dbgout, set->member);
14296 }
14297 fflush(state->dbgout);
14298#endif
14299
14300 /* Clear the marks */
14301 ins = first;
14302 do {
14303 ins->id = info[ID_BITS(ins->id)].id;
14304 ins = ins->next;
14305 } while(ins != first);
14306
14307 /* Release the ordered list of live variables */
14308 free_closure_variables(state, &vars);
14309
14310 /* Release the storage of the old ids */
14311 xfree(info);
14312
14313 /* Release the variable lifetime information */
14314 free_variable_lifetimes(state, &bb, rb);
14315
14316 /* Release the basic blocks of this function */
14317 free_basic_blocks(state, &bb);
14318}
14319
14320static void expand_function_call(
14321 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014322{
14323 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014324 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014325 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014326 struct triple *first;
14327 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014328 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014329 struct triple_reg_set *enclose, *set;
14330 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014331
Eric Biederman90089602004-05-28 14:11:54 +000014332#if DEBUG_EXPLICIT_CLOSURES
14333 FILE *fp = state->dbgout;
14334 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14335 display_func(state, fp, MISC(fcall, 0));
14336 display_func(state, fp, me);
14337 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14338#endif
14339
Eric Biederman5ade04a2003-10-22 04:03:46 +000014340 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014341 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014342 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014343 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014344 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014345 first = fcall->next;
14346
14347 /* Find what I need to enclose */
14348 compute_closure_variables(state, me, fcall, &enclose);
14349
14350 /* Compute the closure type */
14351 closure_type = new_type(TYPE_TUPLE, 0, 0);
14352 closure_type->elements = 0;
14353 closure_next = &closure_type->left;
14354 for(set = enclose; set ; set = set->next) {
14355 struct type *type;
14356 type = &void_type;
14357 if (set->member) {
14358 type = set->member->type;
14359 }
14360 if (!*closure_next) {
14361 *closure_next = type;
14362 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014363 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014364 type);
14365 closure_next = &(*closure_next)->right;
14366 }
14367 closure_type->elements += 1;
14368 }
14369 if (closure_type->elements == 0) {
14370 closure_type->type = TYPE_VOID;
14371 }
14372
14373
14374#if DEBUG_EXPLICIT_CLOSURES
14375 fprintf(state->dbgout, "closure type: ");
14376 name_of(state->dbgout, closure_type);
14377 fprintf(state->dbgout, "\n");
14378#endif
14379
14380 /* Update the called functions closure variable */
14381 closure_idx = add_closure_type(state, func, closure_type);
Patrick Georgi61dbbf62017-01-02 18:54:28 +010014382 free(closure_type);
14383 closure_type = NULL;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014384
14385 /* Generate some needed triples */
14386 ret_loc = label(state);
14387 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14388
14389 /* Pass the parameters to the new function */
14390 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014391 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014392 for(i = 0; i < pvals; i++) {
14393 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014394 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014395 atype = ptype;
14396 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14397 atype = ptype->left;
14398 }
Eric Biederman90089602004-05-28 14:11:54 +000014399 param = farg(state, func, i);
14400 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14401 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014402 }
Eric Biederman90089602004-05-28 14:11:54 +000014403 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014404 flatten(state, first, write_expr(state, param, arg));
14405 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014406 }
Eric Biederman90089602004-05-28 14:11:54 +000014407 rtype = func->type->left;
14408
Eric Biederman5ade04a2003-10-22 04:03:46 +000014409 /* Thread the triples together */
14410 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014411
14412 /* Save the active variables in the result variable */
14413 for(i = 0, set = enclose; set ; set = set->next, i++) {
14414 if (!set->member) {
14415 continue;
14416 }
14417 flatten(state, ret_loc,
14418 write_expr(state,
14419 closure_expr(state, func, closure_idx, i),
14420 read_expr(state, set->member)));
14421 }
14422
14423 /* Initialize the return value */
14424 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014425 flatten(state, ret_loc,
14426 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014427 deref_index(state, fresult(state, func), 1),
14428 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14429 }
14430
Eric Biederman5ade04a2003-10-22 04:03:46 +000014431 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014432 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14433 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014434 call(state, retvar, ret_addr, func_first, func_last));
14435
Eric Biederman7dea9552004-06-29 05:38:37 +000014436 /* Find the result */
14437 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14438 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014439 result = flatten(state, first,
14440 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014441 deref_index(state, fresult(state, func), 1)));
14442
14443 propogate_use(state, fcall, result);
14444 }
14445
14446 /* Release the original fcall instruction */
14447 release_triple(state, fcall);
14448
Eric Biederman90089602004-05-28 14:11:54 +000014449 /* Restore the active variables from the result variable */
14450 for(i = 0, set = enclose; set ; set = set->next, i++) {
14451 struct triple_set *use, *next;
14452 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014453 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014454 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014455 continue;
14456 }
Eric Biederman90089602004-05-28 14:11:54 +000014457 /* Generate an expression for the value */
14458 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014459 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014460 closure_expr(state, func, closure_idx, i)));
14461
14462
14463 /* If the original is an lvalue restore the preserved value */
14464 if (is_lvalue(state, set->member)) {
14465 flatten(state, first,
14466 write_expr(state, set->member, new));
14467 continue;
14468 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014469 /*
14470 * If the original is a value update the dominated uses.
14471 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014472
Eric Biederman7dea9552004-06-29 05:38:37 +000014473 /* Analyze the basic blocks so I can see who dominates whom */
14474 bb.func = me;
14475 bb.first = RHS(me, 0);
14476 if (!triple_is_ret(state, bb.first->prev)) {
14477 bb.func = 0;
14478 }
14479 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014480
Eric Biederman90089602004-05-28 14:11:54 +000014481
14482#if DEBUG_EXPLICIT_CLOSURES
14483 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14484 set->member, new);
14485#endif
14486 /* If fcall dominates the use update the expression */
14487 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014488 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014489 * removes use, so I must take a copy of the
14490 * next entry early.
14491 */
14492 next = use->next;
14493 if (!tdominates(state, fcall, use->member)) {
14494 continue;
14495 }
14496 replace_use(state, set->member, new, use->member);
14497 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014498
14499 /* Release the basic blocks, the instructions will be
14500 * different next time, and flatten/insert_triple does
14501 * not update the block values so I can't cache the analysis.
14502 */
14503 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014504 }
14505
Eric Biederman90089602004-05-28 14:11:54 +000014506 /* Release the closure variable list */
14507 free_closure_variables(state, &enclose);
14508
14509 if (state->compiler->debug & DEBUG_INLINE) {
14510 FILE *fp = state->dbgout;
14511 fprintf(fp, "\n");
14512 loc(fp, state, 0);
14513 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14514 display_func(state, fp, func);
14515 display_func(state, fp, me);
14516 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14517 }
14518
14519 return;
14520}
14521
14522static int do_inline(struct compile_state *state, struct triple *func)
14523{
14524 int do_inline;
14525 int policy;
14526
14527 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14528 switch(policy) {
14529 case COMPILER_INLINE_ALWAYS:
14530 do_inline = 1;
14531 if (func->type->type & ATTRIB_NOINLINE) {
14532 error(state, func, "noinline with always_inline compiler option");
14533 }
14534 break;
14535 case COMPILER_INLINE_NEVER:
14536 do_inline = 0;
14537 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14538 error(state, func, "always_inline with noinline compiler option");
14539 }
14540 break;
14541 case COMPILER_INLINE_DEFAULTON:
14542 switch(func->type->type & STOR_MASK) {
14543 case STOR_STATIC | STOR_INLINE:
14544 case STOR_LOCAL | STOR_INLINE:
14545 case STOR_EXTERN | STOR_INLINE:
14546 do_inline = 1;
14547 break;
14548 default:
14549 do_inline = 1;
14550 break;
14551 }
14552 break;
14553 case COMPILER_INLINE_DEFAULTOFF:
14554 switch(func->type->type & STOR_MASK) {
14555 case STOR_STATIC | STOR_INLINE:
14556 case STOR_LOCAL | STOR_INLINE:
14557 case STOR_EXTERN | STOR_INLINE:
14558 do_inline = 1;
14559 break;
14560 default:
14561 do_inline = 0;
14562 break;
14563 }
14564 break;
14565 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014566 switch(func->type->type & STOR_MASK) {
14567 case STOR_STATIC | STOR_INLINE:
14568 case STOR_LOCAL | STOR_INLINE:
14569 case STOR_EXTERN | STOR_INLINE:
14570 do_inline = 1;
14571 break;
14572 default:
14573 do_inline = (func->u.cval == 1);
14574 break;
14575 }
Eric Biederman90089602004-05-28 14:11:54 +000014576 break;
14577 default:
14578 do_inline = 0;
14579 internal_error(state, 0, "Unimplemented inline policy");
14580 break;
14581 }
14582 /* Force inlining */
14583 if (func->type->type & ATTRIB_NOINLINE) {
14584 do_inline = 0;
14585 }
14586 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14587 do_inline = 1;
14588 }
14589 return do_inline;
14590}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014591
Eric Biederman90089602004-05-28 14:11:54 +000014592static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14593{
14594 struct triple *first, *ptr, *next;
14595 /* If the function is not used don't bother */
14596 if (me->u.cval <= 0) {
14597 return;
14598 }
14599 if (state->compiler->debug & DEBUG_CALLS2) {
14600 FILE *fp = state->dbgout;
14601 fprintf(fp, "in: %s\n",
14602 me->type->type_ident->name);
14603 }
14604
14605 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014606 ptr = next = first;
14607 do {
Eric Biederman90089602004-05-28 14:11:54 +000014608 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014609 ptr = next;
14610 prev = ptr->prev;
14611 next = ptr->next;
14612 if (ptr->op != OP_FCALL) {
14613 continue;
14614 }
14615 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014616 /* See if the function should be inlined */
14617 if (!do_inline(state, func)) {
14618 /* Put a label after the fcall */
14619 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14620 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014621 }
Eric Biederman90089602004-05-28 14:11:54 +000014622 if (state->compiler->debug & DEBUG_CALLS) {
14623 FILE *fp = state->dbgout;
14624 if (state->compiler->debug & DEBUG_CALLS2) {
14625 loc(fp, state, ptr);
14626 }
14627 fprintf(fp, "inlining %s\n",
14628 func->type->type_ident->name);
14629 fflush(fp);
14630 }
14631
14632 /* Update the function use counts */
14633 func->u.cval -= 1;
14634
14635 /* Replace the fcall with the called function */
14636 expand_inline_call(state, me, ptr);
14637
14638 next = prev->next;
14639 } while (next != first);
14640
14641 ptr = next = first;
14642 do {
14643 struct triple *prev, *func;
14644 ptr = next;
14645 prev = ptr->prev;
14646 next = ptr->next;
14647 if (ptr->op != OP_FCALL) {
14648 continue;
14649 }
14650 func = MISC(ptr, 0);
14651 if (state->compiler->debug & DEBUG_CALLS) {
14652 FILE *fp = state->dbgout;
14653 if (state->compiler->debug & DEBUG_CALLS2) {
14654 loc(fp, state, ptr);
14655 }
14656 fprintf(fp, "calling %s\n",
14657 func->type->type_ident->name);
14658 fflush(fp);
14659 }
14660 /* Replace the fcall with the instruction sequence
14661 * needed to make the call.
14662 */
14663 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014664 next = prev->next;
14665 } while(next != first);
14666}
Eric Biederman90089602004-05-28 14:11:54 +000014667
14668static void inline_functions(struct compile_state *state, struct triple *func)
14669{
14670 inline_function(state, func, 0);
14671 reverse_walk_functions(state, inline_function, 0);
14672}
14673
Eric Biederman5ade04a2003-10-22 04:03:46 +000014674static void insert_function(struct compile_state *state,
14675 struct triple *func, void *arg)
14676{
14677 struct triple *first, *end, *ffirst, *fend;
14678
14679 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014680 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014681 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014682 func->type->type_ident->name, func->u.cval);
14683 }
14684 if (func->u.cval == 0) {
14685 return;
14686 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014687
14688 /* Find the end points of the lists */
14689 first = arg;
14690 end = first->prev;
14691 ffirst = RHS(func, 0);
14692 fend = ffirst->prev;
14693
14694 /* splice the lists together */
14695 end->next = ffirst;
14696 ffirst->prev = end;
14697 fend->next = first;
14698 first->prev = fend;
14699}
14700
Eric Biederman90089602004-05-28 14:11:54 +000014701struct triple *input_asm(struct compile_state *state)
14702{
14703 struct asm_info *info;
14704 struct triple *def;
14705 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014706
Eric Biederman90089602004-05-28 14:11:54 +000014707 info = xcmalloc(sizeof(*info), "asm_info");
14708 info->str = "";
14709
14710 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14711 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14712
14713 def = new_triple(state, OP_ASM, &void_type, out, 0);
14714 def->u.ainfo = info;
14715 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014716
Eric Biederman90089602004-05-28 14:11:54 +000014717 for(i = 0; i < out; i++) {
14718 struct triple *piece;
14719 piece = triple(state, OP_PIECE, &int_type, def, 0);
14720 piece->u.cval = i;
14721 LHS(def, i) = piece;
14722 }
14723
14724 return def;
14725}
14726
14727struct triple *output_asm(struct compile_state *state)
14728{
14729 struct asm_info *info;
14730 struct triple *def;
14731 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014732
Eric Biederman90089602004-05-28 14:11:54 +000014733 info = xcmalloc(sizeof(*info), "asm_info");
14734 info->str = "";
14735
14736 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14737 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14738
14739 def = new_triple(state, OP_ASM, &void_type, 0, in);
14740 def->u.ainfo = info;
14741 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014742
Eric Biederman90089602004-05-28 14:11:54 +000014743 return def;
14744}
14745
Eric Biederman5ade04a2003-10-22 04:03:46 +000014746static void join_functions(struct compile_state *state)
14747{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014748 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014749 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014750 struct type *pnext, *param;
14751 struct type *result_type, *args_type;
14752 int idx;
14753
14754 /* Be clear the functions have not been joined yet */
14755 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014756
14757 /* Dummy file state to get debug handing right */
14758 memset(&file, 0, sizeof(file));
14759 file.basename = "";
14760 file.line = 0;
14761 file.report_line = 0;
14762 file.report_name = file.basename;
14763 file.prev = state->file;
14764 state->file = &file;
14765 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014766
Eric Biederman41203d92004-11-08 09:31:09 +000014767 if (!state->main_function) {
14768 error(state, 0, "No functions to compile\n");
14769 }
14770
Eric Biederman90089602004-05-28 14:11:54 +000014771 /* The type of arguments */
14772 args_type = state->main_function->type->right;
14773 /* The return type without any specifiers */
14774 result_type = clone_type(0, state->main_function->type->left);
14775
14776
14777 /* Verify the external arguments */
14778 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014779 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014780 "Too many external input arguments");
14781 }
14782 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014783 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014784 "Too many external output arguments");
14785 }
14786
Eric Biederman5ade04a2003-10-22 04:03:46 +000014787 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014788 end = label(state);
14789 start = label(state);
14790 start = flatten(state, state->first, start);
14791 end = flatten(state, state->first, end);
14792 in = input_asm(state);
14793 out = output_asm(state);
14794 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014795 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014796 in = flatten(state, state->first, in);
14797 call = flatten(state, state->first, call);
14798 out = flatten(state, state->first, out);
14799
14800
14801 /* Read the external input arguments */
14802 pnext = args_type;
14803 idx = 0;
14804 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14805 struct triple *expr;
14806 param = pnext;
14807 pnext = 0;
14808 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14809 pnext = param->right;
14810 param = param->left;
14811 }
14812 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014813 error(state, state->main_function,
14814 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014815 idx + 1, param->field_ident->name);
14816 }
14817 expr = read_expr(state, LHS(in, idx));
14818 RHS(call, idx) = expr;
14819 expr = flatten(state, call, expr);
14820 use_triple(expr, call);
14821
Stefan Reinauer14e22772010-04-27 06:56:47 +000014822 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014823 }
14824
14825
14826 /* Write the external output arguments */
14827 pnext = result_type;
14828 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14829 pnext = result_type->left;
14830 }
14831 for(idx = 0; idx < out->rhs; idx++) {
14832 struct triple *expr;
14833 param = pnext;
14834 pnext = 0;
14835 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14836 pnext = param->right;
14837 param = param->left;
14838 }
14839 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14840 param = 0;
14841 }
14842 if (param) {
14843 if (registers_of(state, param) != 1) {
14844 error(state, state->main_function,
14845 "Result: %d %s requires multiple registers",
14846 idx, param->field_ident->name);
14847 }
14848 expr = read_expr(state, call);
14849 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14850 expr = deref_field(state, expr, param->field_ident);
14851 }
14852 } else {
14853 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14854 }
14855 flatten(state, out, expr);
14856 RHS(out, idx) = expr;
14857 use_triple(expr, out);
14858 }
14859
14860 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014861 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014862 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14863 func->type->type_ident = lookup(state, "", 0);
14864 RHS(func, 0) = state->first;
14865 func->u.cval = 1;
14866
Eric Biederman5ade04a2003-10-22 04:03:46 +000014867 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014868 mark_live_functions(state);
14869 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014870 walk_functions(state, insert_function, end);
14871
14872 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014873 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014874 }
14875
Eric Biederman90089602004-05-28 14:11:54 +000014876 /* OK now the functions have been joined. */
14877 state->functions_joined = 1;
14878
Eric Biederman5ade04a2003-10-22 04:03:46 +000014879 /* Done now cleanup */
14880 state->file = file.prev;
14881 state->function = 0;
14882}
14883
Eric Biedermanb138ac82003-04-22 18:44:01 +000014884/*
14885 * Data structurs for optimation.
14886 */
14887
Eric Biederman5ade04a2003-10-22 04:03:46 +000014888
Eric Biederman83b991a2003-10-11 06:20:25 +000014889static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014890 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014891 int front)
14892{
14893 struct block_set **ptr, *new;
14894 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014895 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014896 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014897 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014898 ptr = head;
14899 while(*ptr) {
14900 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014901 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014902 }
14903 ptr = &(*ptr)->next;
14904 }
14905 new = xcmalloc(sizeof(*new), "block_set");
14906 new->member = user;
14907 if (front) {
14908 new->next = *head;
14909 *head = new;
14910 }
14911 else {
14912 new->next = 0;
14913 *ptr = new;
14914 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014915 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014916}
Eric Biederman83b991a2003-10-11 06:20:25 +000014917static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014918 struct block *used, struct block_set **head, struct block *unuser)
14919{
14920 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014921 int count;
14922 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014923 ptr = head;
14924 while(*ptr) {
14925 use = *ptr;
14926 if (use->member == unuser) {
14927 *ptr = use->next;
14928 memset(use, -1, sizeof(*use));
14929 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014930 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014931 }
14932 else {
14933 ptr = &use->next;
14934 }
14935 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014936 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014937}
14938
14939static void use_block(struct block *used, struct block *user)
14940{
Eric Biederman83b991a2003-10-11 06:20:25 +000014941 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014942 /* Append new to the head of the list, print_block
14943 * depends on this.
14944 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014945 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014946 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014947}
14948static void unuse_block(struct block *used, struct block *unuser)
14949{
Eric Biederman83b991a2003-10-11 06:20:25 +000014950 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014951 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014952 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014953}
14954
Eric Biederman5ade04a2003-10-22 04:03:46 +000014955static void add_block_edge(struct block *block, struct block *edge, int front)
14956{
14957 int count;
14958 count = do_use_block(block, &block->edges, edge, front);
14959 block->edge_count += count;
14960}
14961
14962static void remove_block_edge(struct block *block, struct block *edge)
14963{
14964 int count;
14965 count = do_unuse_block(block, &block->edges, edge);
14966 block->edge_count -= count;
14967}
14968
Eric Biedermanb138ac82003-04-22 18:44:01 +000014969static void idom_block(struct block *idom, struct block *user)
14970{
14971 do_use_block(idom, &idom->idominates, user, 0);
14972}
14973
14974static void unidom_block(struct block *idom, struct block *unuser)
14975{
14976 do_unuse_block(idom, &idom->idominates, unuser);
14977}
14978
14979static void domf_block(struct block *block, struct block *domf)
14980{
14981 do_use_block(block, &block->domfrontier, domf, 0);
14982}
14983
14984static void undomf_block(struct block *block, struct block *undomf)
14985{
14986 do_unuse_block(block, &block->domfrontier, undomf);
14987}
14988
14989static void ipdom_block(struct block *ipdom, struct block *user)
14990{
14991 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14992}
14993
14994static void unipdom_block(struct block *ipdom, struct block *unuser)
14995{
14996 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14997}
14998
14999static void ipdomf_block(struct block *block, struct block *ipdomf)
15000{
15001 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
15002}
15003
15004static void unipdomf_block(struct block *block, struct block *unipdomf)
15005{
15006 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15007}
15008
Eric Biederman83b991a2003-10-11 06:20:25 +000015009static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015010 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015011 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15012 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015013{
Eric Biederman83b991a2003-10-11 06:20:25 +000015014 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015015 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015016 ptr = state->first;
15017 do {
Eric Biederman90089602004-05-28 14:11:54 +000015018 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015019 if (ptr->next->prev != ptr) {
15020 internal_error(state, ptr->next, "bad prev");
15021 }
15022 ptr = ptr->next;
15023 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015024 return result;
15025}
15026
Eric Biedermanb138ac82003-04-22 18:44:01 +000015027#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015028static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015029{
Eric Biederman90089602004-05-28 14:11:54 +000015030 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015031 int op;
15032 op = ins->op;
15033 if (op == OP_LIST) {
15034#if !PRINT_LIST
15035 return 0;
15036#endif
15037 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015038 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015039 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015040 }
Eric Biederman90089602004-05-28 14:11:54 +000015041 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015042
Stefan Reinauer14e22772010-04-27 06:56:47 +000015043 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015044 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015045 internal_error(state, ins, "branch used?");
15046 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015047 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015048 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015049 }
15050 return 0;
15051}
15052
Eric Biedermanb138ac82003-04-22 18:44:01 +000015053static void print_triples(struct compile_state *state)
15054{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015055 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015056 FILE *fp = state->dbgout;
15057 fprintf(fp, "--------------- triples ---------------\n");
15058 walk_triples(state, do_print_triple, fp);
15059 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015060 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015061}
15062
15063struct cf_block {
15064 struct block *block;
15065};
15066static void find_cf_blocks(struct cf_block *cf, struct block *block)
15067{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015068 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015069 if (!block || (cf[block->vertex].block == block)) {
15070 return;
15071 }
15072 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015073 for(edge = block->edges; edge; edge = edge->next) {
15074 find_cf_blocks(cf, edge->member);
15075 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015076}
15077
Eric Biederman90089602004-05-28 14:11:54 +000015078static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015079 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015080{
15081 struct cf_block *cf;
15082 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015083 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015084 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15085 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015086
Eric Biederman90089602004-05-28 14:11:54 +000015087 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015088 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015089 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015090 block = cf[i].block;
15091 if (!block)
15092 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015093 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015094 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015095 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015096 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015097 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015098 }
15099
15100 xfree(cf);
15101}
15102
Eric Biedermanb138ac82003-04-22 18:44:01 +000015103static void free_basic_block(struct compile_state *state, struct block *block)
15104{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015105 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015106 struct block *child;
15107 if (!block) {
15108 return;
15109 }
15110 if (block->vertex == -1) {
15111 return;
15112 }
15113 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015114 for(edge = block->edges; edge; edge = edge->next) {
15115 if (edge->member) {
15116 unuse_block(edge->member, block);
15117 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015118 }
15119 if (block->idom) {
15120 unidom_block(block->idom, block);
15121 }
15122 block->idom = 0;
15123 if (block->ipdom) {
15124 unipdom_block(block->ipdom, block);
15125 }
15126 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015127 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015128 child = entry->member;
15129 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015130 if (child && (child->vertex != -1)) {
15131 for(edge = child->edges; edge; edge = edge->next) {
15132 edge->member = 0;
15133 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015134 }
15135 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015136 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015137 child = entry->member;
15138 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015139 if (child && (child->vertex != -1)) {
15140 child->idom = 0;
15141 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015142 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015143 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015144 child = entry->member;
15145 undomf_block(block, child);
15146 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015147 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015148 child = entry->member;
15149 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015150 if (child && (child->vertex != -1)) {
15151 child->ipdom = 0;
15152 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015153 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015154 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015155 child = entry->member;
15156 unipdomf_block(block, child);
15157 }
15158 if (block->users != 0) {
15159 internal_error(state, 0, "block still has users");
15160 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015161 while((edge = block->edges)) {
15162 child = edge->member;
15163 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015164
Eric Biederman5ade04a2003-10-22 04:03:46 +000015165 if (child && (child->vertex != -1)) {
15166 free_basic_block(state, child);
15167 }
15168 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015169 memset(block, -1, sizeof(*block));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015170}
15171
Stefan Reinauer14e22772010-04-27 06:56:47 +000015172static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015173 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015174{
15175 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015176 free_basic_block(state, bb->first_block);
15177 bb->last_vertex = 0;
15178 bb->first_block = bb->last_block = 0;
15179 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015180 ins = first;
15181 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015182 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015183 ins->u.block = 0;
15184 }
15185 ins = ins->next;
15186 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015187
Eric Biedermanb138ac82003-04-22 18:44:01 +000015188}
15189
Stefan Reinauer14e22772010-04-27 06:56:47 +000015190static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015191 struct basic_blocks *bb, struct triple *first)
15192{
15193 struct block *block;
15194 struct triple *ptr;
15195 if (!triple_is_label(state, first)) {
15196 internal_error(state, first, "block does not start with a label");
15197 }
15198 /* See if this basic block has already been setup */
15199 if (first->u.block != 0) {
15200 return first->u.block;
15201 }
15202 /* Allocate another basic block structure */
15203 bb->last_vertex += 1;
15204 block = xcmalloc(sizeof(*block), "block");
15205 block->first = block->last = first;
15206 block->vertex = bb->last_vertex;
15207 ptr = first;
15208 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015209 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015210 break;
15211 }
15212 block->last = ptr;
15213 /* If ptr->u is not used remember where the baic block is */
15214 if (triple_stores_block(state, ptr)) {
15215 ptr->u.block = block;
15216 }
15217 if (triple_is_branch(state, ptr)) {
15218 break;
15219 }
15220 ptr = ptr->next;
15221 } while (ptr != bb->first);
15222 if ((ptr == bb->first) ||
15223 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015224 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015225 triple_is_ret(state, ptr))))
15226 {
15227 /* The block has no outflowing edges */
15228 }
15229 else if (triple_is_label(state, ptr)) {
15230 struct block *next;
15231 next = basic_block(state, bb, ptr);
15232 add_block_edge(block, next, 0);
15233 use_block(next, block);
15234 }
15235 else if (triple_is_branch(state, ptr)) {
15236 struct triple **expr, *first;
15237 struct block *child;
15238 /* Find the branch targets.
15239 * I special case the first branch as that magically
15240 * avoids some difficult cases for the register allocator.
15241 */
15242 expr = triple_edge_targ(state, ptr, 0);
15243 if (!expr) {
15244 internal_error(state, ptr, "branch without targets");
15245 }
15246 first = *expr;
15247 expr = triple_edge_targ(state, ptr, expr);
15248 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15249 if (!*expr) continue;
15250 child = basic_block(state, bb, *expr);
15251 use_block(child, block);
15252 add_block_edge(block, child, 0);
15253 }
15254 if (first) {
15255 child = basic_block(state, bb, first);
15256 use_block(child, block);
15257 add_block_edge(block, child, 1);
15258
15259 /* Be certain the return block of a call is
15260 * in a basic block. When it is not find
15261 * start of the block, insert a label if
15262 * necessary and build the basic block.
15263 * Then add a fake edge from the start block
15264 * to the return block of the function.
15265 */
15266 if (state->functions_joined && triple_is_call(state, ptr)
15267 && !block_of_triple(state, MISC(ptr, 0))) {
15268 struct block *tail;
15269 struct triple *start;
15270 start = triple_to_block_start(state, MISC(ptr, 0));
15271 if (!triple_is_label(state, start)) {
15272 start = pre_triple(state,
15273 start, OP_LABEL, &void_type, 0, 0);
15274 }
15275 tail = basic_block(state, bb, start);
15276 add_block_edge(child, tail, 0);
15277 use_block(tail, child);
15278 }
15279 }
15280 }
15281 else {
15282 internal_error(state, 0, "Bad basic block split");
15283 }
15284#if 0
15285{
15286 struct block_set *edge;
15287 FILE *fp = state->errout;
15288 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015289 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015290 block->first, block->last);
15291 for(edge = block->edges; edge; edge = edge->next) {
15292 fprintf(fp, " %10p [%2d]",
15293 edge->member ? edge->member->first : 0,
15294 edge->member ? edge->member->vertex : -1);
15295 }
15296 fprintf(fp, "\n");
15297}
15298#endif
15299 return block;
15300}
15301
15302
15303static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15304 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15305 void *arg)
15306{
15307 struct triple *ptr, *first;
15308 struct block *last_block;
15309 last_block = 0;
15310 first = bb->first;
15311 ptr = first;
15312 do {
15313 if (triple_stores_block(state, ptr)) {
15314 struct block *block;
15315 block = ptr->u.block;
15316 if (block && (block != last_block)) {
15317 cb(state, block, arg);
15318 }
15319 last_block = block;
15320 }
15321 ptr = ptr->next;
15322 } while(ptr != first);
15323}
15324
15325static void print_block(
15326 struct compile_state *state, struct block *block, void *arg)
15327{
15328 struct block_set *user, *edge;
15329 struct triple *ptr;
15330 FILE *fp = arg;
15331
15332 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015333 block,
Eric Biederman90089602004-05-28 14:11:54 +000015334 block->vertex);
15335
15336 for(edge = block->edges; edge; edge = edge->next) {
15337 fprintf(fp, " %p<-%p",
15338 edge->member,
15339 (edge->member && edge->member->use)?
15340 edge->member->use->member : 0);
15341 }
15342 fprintf(fp, "\n");
15343 if (block->first->op == OP_LABEL) {
15344 fprintf(fp, "%p:\n", block->first);
15345 }
15346 for(ptr = block->first; ; ) {
15347 display_triple(fp, ptr);
15348 if (ptr == block->last)
15349 break;
15350 ptr = ptr->next;
15351 if (ptr == block->first) {
15352 internal_error(state, 0, "missing block last?");
15353 }
15354 }
15355 fprintf(fp, "users %d: ", block->users);
15356 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015357 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015358 user->member,
15359 user->member->vertex);
15360 }
15361 fprintf(fp,"\n\n");
15362}
15363
15364
15365static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15366{
15367 fprintf(fp, "--------------- blocks ---------------\n");
15368 walk_blocks(state, &state->bb, print_block, fp);
15369}
15370static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15371{
15372 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15373 fprintf(fp, "After %s\n", func);
15374 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015375 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15376 print_dominators(state, fp, &state->bb);
15377 print_dominance_frontiers(state, fp, &state->bb);
15378 }
15379 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015380 }
15381}
15382
Stefan Reinauer14e22772010-04-27 06:56:47 +000015383static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015384 struct basic_blocks *bb)
15385{
15386 struct block *block;
15387 struct triple *first, *ins, *next;
15388 /* Delete the triples not in a basic block */
15389 block = 0;
15390 first = bb->first;
15391 ins = first;
15392 do {
15393 next = ins->next;
15394 if (ins->op == OP_LABEL) {
15395 block = ins->u.block;
15396 }
15397 if (!block) {
15398 struct triple_set *use;
15399 for(use = ins->use; use; use = use->next) {
15400 struct block *block;
15401 block = block_of_triple(state, use->member);
15402 if (block != 0) {
15403 internal_error(state, ins, "pruning used ins?");
15404 }
15405 }
15406 release_triple(state, ins);
15407 }
15408 if (block && block->last == ins) {
15409 block = 0;
15410 }
15411 ins = next;
15412 } while(ins != first);
15413}
15414
Stefan Reinauer14e22772010-04-27 06:56:47 +000015415static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015416 struct basic_blocks *bb)
15417{
15418 if (!triple_stores_block(state, bb->first)) {
15419 internal_error(state, 0, "ins will not store block?");
15420 }
15421 /* Initialize the state */
15422 bb->first_block = bb->last_block = 0;
15423 bb->last_vertex = 0;
15424 free_basic_blocks(state, bb);
15425
15426 /* Find the basic blocks */
15427 bb->first_block = basic_block(state, bb, bb->first);
15428
15429 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015430 * entire program is in a basic block. When it is not find
15431 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015432 * basic block. Then add a fake edge from the start block
15433 * to the final block.
15434 */
15435 if (!block_of_triple(state, bb->first->prev)) {
15436 struct triple *start;
15437 struct block *tail;
15438 start = triple_to_block_start(state, bb->first->prev);
15439 if (!triple_is_label(state, start)) {
15440 start = pre_triple(state,
15441 start, OP_LABEL, &void_type, 0, 0);
15442 }
15443 tail = basic_block(state, bb, start);
15444 add_block_edge(bb->first_block, tail, 0);
15445 use_block(tail, bb->first_block);
15446 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015447
Eric Biederman90089602004-05-28 14:11:54 +000015448 /* Find the last basic block.
15449 */
15450 bb->last_block = block_of_triple(state, bb->first->prev);
15451
15452 /* Delete the triples not in a basic block */
15453 prune_nonblock_triples(state, bb);
15454
15455#if 0
15456 /* If we are debugging print what I have just done */
15457 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15458 print_blocks(state, state->dbgout);
15459 print_control_flow(state, bb);
15460 }
15461#endif
15462}
15463
15464
Eric Biedermanb138ac82003-04-22 18:44:01 +000015465struct sdom_block {
15466 struct block *block;
15467 struct sdom_block *sdominates;
15468 struct sdom_block *sdom_next;
15469 struct sdom_block *sdom;
15470 struct sdom_block *label;
15471 struct sdom_block *parent;
15472 struct sdom_block *ancestor;
15473 int vertex;
15474};
15475
15476
15477static void unsdom_block(struct sdom_block *block)
15478{
15479 struct sdom_block **ptr;
15480 if (!block->sdom_next) {
15481 return;
15482 }
15483 ptr = &block->sdom->sdominates;
15484 while(*ptr) {
15485 if ((*ptr) == block) {
15486 *ptr = block->sdom_next;
15487 return;
15488 }
15489 ptr = &(*ptr)->sdom_next;
15490 }
15491}
15492
15493static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15494{
15495 unsdom_block(block);
15496 block->sdom = sdom;
15497 block->sdom_next = sdom->sdominates;
15498 sdom->sdominates = block;
15499}
15500
15501
15502
15503static int initialize_sdblock(struct sdom_block *sd,
15504 struct block *parent, struct block *block, int vertex)
15505{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015506 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015507 if (!block || (sd[block->vertex].block == block)) {
15508 return vertex;
15509 }
15510 vertex += 1;
15511 /* Renumber the blocks in a convinient fashion */
15512 block->vertex = vertex;
15513 sd[vertex].block = block;
15514 sd[vertex].sdom = &sd[vertex];
15515 sd[vertex].label = &sd[vertex];
15516 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15517 sd[vertex].ancestor = 0;
15518 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015519 for(edge = block->edges; edge; edge = edge->next) {
15520 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15521 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015522 return vertex;
15523}
15524
Eric Biederman83b991a2003-10-11 06:20:25 +000015525static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015526 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015527 struct block *parent, struct block *block, int vertex)
15528{
15529 struct block_set *user;
15530 if (!block || (sd[block->vertex].block == block)) {
15531 return vertex;
15532 }
15533 vertex += 1;
15534 /* Renumber the blocks in a convinient fashion */
15535 block->vertex = vertex;
15536 sd[vertex].block = block;
15537 sd[vertex].sdom = &sd[vertex];
15538 sd[vertex].label = &sd[vertex];
15539 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15540 sd[vertex].ancestor = 0;
15541 sd[vertex].vertex = vertex;
15542 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015543 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015544 }
15545 return vertex;
15546}
15547
Stefan Reinauer14e22772010-04-27 06:56:47 +000015548static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015549 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015550{
15551 struct block *block;
15552 int vertex;
15553 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015554 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015555
Eric Biederman5ade04a2003-10-22 04:03:46 +000015556 /* Walk through the graph and find unconnected blocks. Add a
15557 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015558 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015559 */
Eric Biederman90089602004-05-28 14:11:54 +000015560 block = bb->first_block->last->next->u.block;
15561 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015562 if (sd[block->vertex].block == block) {
15563 continue;
15564 }
Eric Biederman530b5192003-07-01 10:05:30 +000015565#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015566 {
15567 FILE *fp = state->errout;
15568 fprintf(fp, "Adding %d\n", vertex +1);
15569 }
Eric Biederman530b5192003-07-01 10:05:30 +000015570#endif
Eric Biederman90089602004-05-28 14:11:54 +000015571 add_block_edge(block, bb->last_block, 0);
15572 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015573
Eric Biederman90089602004-05-28 14:11:54 +000015574 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015575 }
15576 return vertex;
15577}
15578
15579static void compress_ancestors(struct sdom_block *v)
15580{
15581 /* This procedure assumes ancestor(v) != 0 */
15582 /* if (ancestor(ancestor(v)) != 0) {
15583 * compress(ancestor(ancestor(v)));
15584 * if (semi(label(ancestor(v))) < semi(label(v))) {
15585 * label(v) = label(ancestor(v));
15586 * }
15587 * ancestor(v) = ancestor(ancestor(v));
15588 * }
15589 */
15590 if (!v->ancestor) {
15591 return;
15592 }
15593 if (v->ancestor->ancestor) {
15594 compress_ancestors(v->ancestor->ancestor);
15595 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15596 v->label = v->ancestor->label;
15597 }
15598 v->ancestor = v->ancestor->ancestor;
15599 }
15600}
15601
Stefan Reinauer14e22772010-04-27 06:56:47 +000015602static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015603 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015604{
15605 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015606 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015607 * for each v <= pred(w) {
15608 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015609 * if (semi[u] < semi[w] {
15610 * semi[w] = semi[u];
15611 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015612 * }
15613 * add w to bucket(vertex(semi[w]));
15614 * LINK(parent(w), w);
15615 *
15616 * // step 3
15617 * for each v <= bucket(parent(w)) {
15618 * delete v from bucket(parent(w));
15619 * u = EVAL(v);
15620 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15621 * }
15622 */
Eric Biederman90089602004-05-28 14:11:54 +000015623 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015624 struct sdom_block *v, *parent, *next;
15625 struct block_set *user;
15626 struct block *block;
15627 block = sd[i].block;
15628 parent = sd[i].parent;
15629 /* Step 2 */
15630 for(user = block->use; user; user = user->next) {
15631 struct sdom_block *v, *u;
15632 v = &sd[user->member->vertex];
15633 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15634 if (u->sdom->vertex < sd[i].sdom->vertex) {
15635 sd[i].sdom = u->sdom;
15636 }
15637 }
15638 sdom_block(sd[i].sdom, &sd[i]);
15639 sd[i].ancestor = parent;
15640 /* Step 3 */
15641 for(v = parent->sdominates; v; v = next) {
15642 struct sdom_block *u;
15643 next = v->sdom_next;
15644 unsdom_block(v);
15645 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015646 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015647 u->block : parent->block;
15648 }
15649 }
15650}
15651
Stefan Reinauer14e22772010-04-27 06:56:47 +000015652static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015653 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015654{
15655 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015656 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015657 * for each v <= pred(w) {
15658 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015659 * if (semi[u] < semi[w] {
15660 * semi[w] = semi[u];
15661 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015662 * }
15663 * add w to bucket(vertex(semi[w]));
15664 * LINK(parent(w), w);
15665 *
15666 * // step 3
15667 * for each v <= bucket(parent(w)) {
15668 * delete v from bucket(parent(w));
15669 * u = EVAL(v);
15670 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15671 * }
15672 */
Eric Biederman90089602004-05-28 14:11:54 +000015673 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015674 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015675 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015676 struct block *block;
15677 block = sd[i].block;
15678 parent = sd[i].parent;
15679 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015680 for(edge = block->edges; edge; edge = edge->next) {
15681 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015682 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15683 if (u->sdom->vertex < sd[i].sdom->vertex) {
15684 sd[i].sdom = u->sdom;
15685 }
15686 }
15687 sdom_block(sd[i].sdom, &sd[i]);
15688 sd[i].ancestor = parent;
15689 /* Step 3 */
15690 for(v = parent->sdominates; v; v = next) {
15691 struct sdom_block *u;
15692 next = v->sdom_next;
15693 unsdom_block(v);
15694 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015695 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015696 u->block : parent->block;
15697 }
15698 }
15699}
15700
Stefan Reinauer14e22772010-04-27 06:56:47 +000015701static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015702 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015703{
15704 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015705 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015706 struct block *block;
15707 block = sd[i].block;
15708 if (block->idom->vertex != sd[i].sdom->vertex) {
15709 block->idom = block->idom->idom;
15710 }
15711 idom_block(block->idom, block);
15712 }
15713 sd[1].block->idom = 0;
15714}
15715
Stefan Reinauer14e22772010-04-27 06:56:47 +000015716static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015717 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015718{
15719 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015720 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015721 struct block *block;
15722 block = sd[i].block;
15723 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15724 block->ipdom = block->ipdom->ipdom;
15725 }
15726 ipdom_block(block->ipdom, block);
15727 }
15728 sd[1].block->ipdom = 0;
15729}
15730
15731 /* Theorem 1:
15732 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015733 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015734 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015735 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015736 * v dominates w if and only if v is a proper ancestor of w in
15737 * the dominator tree.
15738 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015739 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015740 * If v and w are vertices of G such that v <= w,
15741 * than any path from v to w must contain a common ancestor
15742 * of v and w in T.
15743 */
15744 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15745 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15746 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15747 /* Theorem 2:
15748 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15749 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15750 */
15751 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015752 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015753 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15754 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15755 */
15756 /* Lemma 5: Let vertices v,w satisfy v -> w.
15757 * Then v -> idom(w) or idom(w) -> idom(v)
15758 */
15759
Eric Biederman90089602004-05-28 14:11:54 +000015760static void find_immediate_dominators(struct compile_state *state,
15761 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015762{
15763 struct sdom_block *sd;
15764 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15765 * vi > w for (1 <= i <= k - 1}
15766 */
15767 /* Theorem 4:
15768 * For any vertex w != r.
15769 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015770 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015771 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15772 */
15773 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015774 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015775 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15776 * Then:
15777 * { sdom(w) if sdom(w) = sdom(u),
15778 * idom(w) = {
15779 * { idom(u) otherwise
15780 */
15781 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015782 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015783 * Number the vertices from 1 to N as they are reached during
15784 * the search. Initialize the variables used in succeeding steps.
15785 * Step 2. Compute the semidominators of all vertices by applying
15786 * theorem 4. Carry out the computation vertex by vertex in
15787 * decreasing order by number.
15788 * Step 3. Implicitly define the immediate dominator of each vertex
15789 * by applying Corollary 1.
15790 * Step 4. Explicitly define the immediate dominator of each vertex,
15791 * carrying out the computation vertex by vertex in increasing order
15792 * by number.
15793 */
15794 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015795 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15796 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015797#if 0
15798 sd[1].size = 0;
15799 sd[1].label = 0;
15800 sd[1].sdom = 0;
15801#endif
15802 /* Step 2 compute the semidominators */
15803 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015804 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015805 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015806 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015807 xfree(sd);
15808}
15809
Eric Biederman90089602004-05-28 14:11:54 +000015810static void find_post_dominators(struct compile_state *state,
15811 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015812{
15813 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015814 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015815 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015816 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015817
Eric Biederman90089602004-05-28 14:11:54 +000015818 vertex = setup_spdblocks(state, bb, sd);
15819 if (vertex != bb->last_vertex) {
15820 internal_error(state, 0, "missing %d blocks",
15821 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015822 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015823
15824 /* Step 2 compute the semidominators */
15825 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015826 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015827 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015828 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015829 xfree(sd);
15830}
15831
15832
15833
15834static void find_block_domf(struct compile_state *state, struct block *block)
15835{
15836 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015837 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015838 if (block->domfrontier != 0) {
15839 internal_error(state, block->first, "domfrontier present?");
15840 }
15841 for(user = block->idominates; user; user = user->next) {
15842 child = user->member;
15843 if (child->idom != block) {
15844 internal_error(state, block->first, "bad idom");
15845 }
15846 find_block_domf(state, child);
15847 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015848 for(edge = block->edges; edge; edge = edge->next) {
15849 if (edge->member->idom != block) {
15850 domf_block(block, edge->member);
15851 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015852 }
15853 for(user = block->idominates; user; user = user->next) {
15854 struct block_set *frontier;
15855 child = user->member;
15856 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15857 if (frontier->member->idom != block) {
15858 domf_block(block, frontier->member);
15859 }
15860 }
15861 }
15862}
15863
15864static void find_block_ipdomf(struct compile_state *state, struct block *block)
15865{
15866 struct block *child;
15867 struct block_set *user;
15868 if (block->ipdomfrontier != 0) {
15869 internal_error(state, block->first, "ipdomfrontier present?");
15870 }
15871 for(user = block->ipdominates; user; user = user->next) {
15872 child = user->member;
15873 if (child->ipdom != block) {
15874 internal_error(state, block->first, "bad ipdom");
15875 }
15876 find_block_ipdomf(state, child);
15877 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015878 for(user = block->use; user; user = user->next) {
15879 if (user->member->ipdom != block) {
15880 ipdomf_block(block, user->member);
15881 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015882 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015883 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015884 struct block_set *frontier;
15885 child = user->member;
15886 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15887 if (frontier->member->ipdom != block) {
15888 ipdomf_block(block, frontier->member);
15889 }
15890 }
15891 }
15892}
15893
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015894static void print_dominated(
15895 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896{
15897 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015898 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015899
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015900 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015901 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015902 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015903 if (user->member->idom != block) {
15904 internal_error(state, user->member->first, "bad idom");
15905 }
15906 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015907 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015908}
15909
Eric Biederman5ade04a2003-10-22 04:03:46 +000015910static void print_dominated2(
15911 struct compile_state *state, FILE *fp, int depth, struct block *block)
15912{
15913 struct block_set *user;
15914 struct triple *ins;
15915 struct occurance *ptr, *ptr2;
15916 const char *filename1, *filename2;
15917 int equal_filenames;
15918 int i;
15919 for(i = 0; i < depth; i++) {
15920 fprintf(fp, " ");
15921 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015922 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015923 block->vertex, block, block->first, block->last);
15924 ins = block->first;
15925 while(ins != block->last && (ins->occurance->line == 0)) {
15926 ins = ins->next;
15927 }
15928 ptr = ins->occurance;
15929 ptr2 = block->last->occurance;
15930 filename1 = ptr->filename? ptr->filename : "";
15931 filename2 = ptr2->filename? ptr2->filename : "";
15932 equal_filenames = (strcmp(filename1, filename2) == 0);
15933 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15934 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15935 } else if (equal_filenames) {
15936 fprintf(fp, " %s:(%d - %d)",
15937 ptr->filename, ptr->line, ptr2->line);
15938 } else {
15939 fprintf(fp, " (%s:%d - %s:%d)",
15940 ptr->filename, ptr->line,
15941 ptr2->filename, ptr2->line);
15942 }
15943 fprintf(fp, "\n");
15944 for(user = block->idominates; user; user = user->next) {
15945 print_dominated2(state, fp, depth + 1, user->member);
15946 }
15947}
15948
Eric Biederman90089602004-05-28 14:11:54 +000015949static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015950{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015951 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015952 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015953 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015954 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015955}
15956
15957
15958static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015959 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015960{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015961 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015962
15963 if (!block || (block->vertex != vertex + 1)) {
15964 return vertex;
15965 }
15966 vertex += 1;
15967
Eric Biederman7dea9552004-06-29 05:38:37 +000015968 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015969 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015970 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015971 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015972 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015973
Eric Biederman5ade04a2003-10-22 04:03:46 +000015974 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015975 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015976 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015977 return vertex;
15978}
Eric Biederman90089602004-05-28 14:11:54 +000015979static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015980 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015981{
Eric Biederman7dea9552004-06-29 05:38:37 +000015982 fprintf(fp, "\ndominance frontiers\n");
15983 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015984
Eric Biedermanb138ac82003-04-22 18:44:01 +000015985}
15986
Eric Biederman90089602004-05-28 14:11:54 +000015987static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015988{
15989 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015990 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015991 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015992 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015993 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015994 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015995 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015996 print_dominance_frontiers(state, state->dbgout, bb);
15997 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015998 }
15999}
16000
16001
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016002static void print_ipdominated(
16003 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004{
16005 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016006 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016007
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016008 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016009 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016010 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016011 if (user->member->ipdom != block) {
16012 internal_error(state, user->member->first, "bad ipdom");
16013 }
16014 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016015 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016016}
16017
Eric Biederman90089602004-05-28 14:11:54 +000016018static void print_ipdominators(struct compile_state *state, FILE *fp,
16019 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016020{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016021 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016022 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016023}
16024
16025static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016026 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016027{
16028 struct block_set *user;
16029
16030 if (!block || (block->vertex != vertex + 1)) {
16031 return vertex;
16032 }
16033 vertex += 1;
16034
Eric Biederman7dea9552004-06-29 05:38:37 +000016035 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016036 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016037 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016038 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016039 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016040 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016041 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016042 }
16043 return vertex;
16044}
Eric Biederman90089602004-05-28 14:11:54 +000016045static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016046 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016047{
Eric Biederman7dea9552004-06-29 05:38:37 +000016048 fprintf(fp, "\nipdominance frontiers\n");
16049 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016050
Eric Biedermanb138ac82003-04-22 18:44:01 +000016051}
16052
Eric Biederman90089602004-05-28 14:11:54 +000016053static void analyze_ipdominators(struct compile_state *state,
16054 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016055{
16056 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016057 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016058 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016059 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016060 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016061 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016062 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016063 print_ipdominance_frontiers(state, state->dbgout, bb);
16064 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016065 }
16066}
16067
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016068static int bdominates(struct compile_state *state,
16069 struct block *dom, struct block *sub)
16070{
16071 while(sub && (sub != dom)) {
16072 sub = sub->idom;
16073 }
16074 return sub == dom;
16075}
16076
16077static int tdominates(struct compile_state *state,
16078 struct triple *dom, struct triple *sub)
16079{
16080 struct block *bdom, *bsub;
16081 int result;
16082 bdom = block_of_triple(state, dom);
16083 bsub = block_of_triple(state, sub);
16084 if (bdom != bsub) {
16085 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016086 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016087 else {
16088 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016089 if (!bdom || !bsub) {
16090 internal_error(state, dom, "huh?");
16091 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016092 ins = sub;
16093 while((ins != bsub->first) && (ins != dom)) {
16094 ins = ins->prev;
16095 }
16096 result = (ins == dom);
16097 }
16098 return result;
16099}
16100
Eric Biederman90089602004-05-28 14:11:54 +000016101static void analyze_basic_blocks(
16102 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016103{
Eric Biederman90089602004-05-28 14:11:54 +000016104 setup_basic_blocks(state, bb);
16105 analyze_idominators(state, bb);
16106 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016107}
16108
Eric Biedermanb138ac82003-04-22 18:44:01 +000016109static void insert_phi_operations(struct compile_state *state)
16110{
16111 size_t size;
16112 struct triple *first;
16113 int *has_already, *work;
16114 struct block *work_list, **work_list_tail;
16115 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016116 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016117
Eric Biederman90089602004-05-28 14:11:54 +000016118 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016119 has_already = xcmalloc(size, "has_already");
16120 work = xcmalloc(size, "work");
16121 iter = 0;
16122
Eric Biederman83b991a2003-10-11 06:20:25 +000016123 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016124 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016125 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016126 struct triple_set *user, *unext;
16127 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016128
16129 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016130 continue;
16131 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016132
Eric Biedermanb138ac82003-04-22 18:44:01 +000016133 iter += 1;
16134 work_list = 0;
16135 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016136 for(user = var->use; user; user = unext) {
16137 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016138 if (MISC(var, 0) == user->member) {
16139 continue;
16140 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016141 if (user->member->op == OP_READ) {
16142 continue;
16143 }
16144 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016145 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016146 "bad variable access");
16147 }
16148 block = user->member->u.block;
16149 if (!block) {
16150 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016151 release_triple(state, user->member);
16152 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016153 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016154 if (work[block->vertex] >= iter) {
16155 continue;
16156 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016157 work[block->vertex] = iter;
16158 *work_list_tail = block;
16159 block->work_next = 0;
16160 work_list_tail = &block->work_next;
16161 }
16162 for(block = work_list; block; block = block->work_next) {
16163 struct block_set *df;
16164 for(df = block->domfrontier; df; df = df->next) {
16165 struct triple *phi;
16166 struct block *front;
16167 int in_edges;
16168 front = df->member;
16169
16170 if (has_already[front->vertex] >= iter) {
16171 continue;
16172 }
16173 /* Count how many edges flow into this block */
16174 in_edges = front->users;
16175 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016176 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016177 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016178 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016179 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016180 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016181 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016182 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016183#if 1
16184 if (phi->rhs != in_edges) {
16185 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16186 phi->rhs, in_edges);
16187 }
16188#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016189 /* Insert the phi functions immediately after the label */
16190 insert_triple(state, front->first->next, phi);
16191 if (front->first == front->last) {
16192 front->last = front->first->next;
16193 }
16194 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016195 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016196
Eric Biedermanb138ac82003-04-22 18:44:01 +000016197 /* If necessary plan to visit the basic block */
16198 if (work[front->vertex] >= iter) {
16199 continue;
16200 }
16201 work[front->vertex] = iter;
16202 *work_list_tail = front;
16203 front->work_next = 0;
16204 work_list_tail = &front->work_next;
16205 }
16206 }
16207 }
16208 xfree(has_already);
16209 xfree(work);
16210}
16211
Eric Biederman66fe2222003-07-04 15:14:04 +000016212
Eric Biederman83b991a2003-10-11 06:20:25 +000016213struct stack {
16214 struct triple_set *top;
16215 unsigned orig_id;
16216};
16217
Eric Biederman90089602004-05-28 14:11:54 +000016218static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016219{
16220 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016221 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016222 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016223 ins = first;
16224 do {
Eric Biederman90089602004-05-28 14:11:54 +000016225 if (triple_is_auto_var(state, ins)) {
16226 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016227 }
16228 ins = ins->next;
16229 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016230 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016231}
16232
Eric Biederman90089602004-05-28 14:11:54 +000016233static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016234{
16235 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016236 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016237 first = state->first;
16238 ins = first;
16239 do {
Eric Biederman90089602004-05-28 14:11:54 +000016240 if (triple_is_auto_var(state, ins)) {
16241 auto_vars += 1;
16242 stacks[auto_vars].orig_id = ins->id;
16243 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016244 }
16245 ins = ins->next;
16246 } while(ins != first);
16247}
16248
Eric Biederman90089602004-05-28 14:11:54 +000016249static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016250{
16251 struct triple *first, *ins;
16252 first = state->first;
16253 ins = first;
16254 do {
Eric Biederman90089602004-05-28 14:11:54 +000016255 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016256 ins->id = stacks[ins->id].orig_id;
16257 }
16258 ins = ins->next;
16259 } while(ins != first);
16260}
16261
16262static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016263{
16264 struct triple_set *head;
16265 struct triple *top_val;
16266 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016267 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016268 if (head) {
16269 top_val = head->member;
16270 }
16271 return top_val;
16272}
16273
Eric Biederman83b991a2003-10-11 06:20:25 +000016274static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016275{
16276 struct triple_set *new;
16277 /* Append new to the head of the list,
16278 * it's the only sensible behavoir for a stack.
16279 */
16280 new = xcmalloc(sizeof(*new), "triple_set");
16281 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016282 new->next = stacks[var->id].top;
16283 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016284}
16285
Eric Biederman83b991a2003-10-11 06:20:25 +000016286static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016287{
16288 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016289 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016290 while(*ptr) {
16291 set = *ptr;
16292 if (set->member == oldval) {
16293 *ptr = set->next;
16294 xfree(set);
16295 /* Only free one occurance from the stack */
16296 return;
16297 }
16298 else {
16299 ptr = &set->next;
16300 }
16301 }
16302}
16303
Eric Biedermanb138ac82003-04-22 18:44:01 +000016304/*
16305 * C(V)
16306 * S(V)
16307 */
16308static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016309 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016310{
16311 struct block_set *set;
16312 struct triple *ptr;
16313 int edge;
16314 if (!parent || !block)
16315 return;
16316 /* Find the edge I am coming in on */
16317 edge = 0;
16318 for(set = block->use; set; set = set->next, edge++) {
16319 if (set->member == parent) {
16320 break;
16321 }
16322 }
16323 if (!set) {
16324 internal_error(state, 0, "phi input is not on a control predecessor");
16325 }
16326 for(ptr = block->first; ; ptr = ptr->next) {
16327 if (ptr->op == OP_PHI) {
16328 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016329 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016330 if (!var) {
16331 internal_error(state, ptr, "no var???");
16332 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016333 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016334 val = peek_triple(stacks, var);
16335 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016336 internal_error(state, val, "bad value in phi");
16337 }
Eric Biederman90089602004-05-28 14:11:54 +000016338 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016339 internal_error(state, ptr, "edges > phi rhs");
16340 }
16341 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016342 if ((*slot != 0) && (*slot != val)) {
16343 internal_error(state, ptr, "phi already bound on this edge");
16344 }
16345 *slot = val;
16346 use_triple(val, ptr);
16347 }
16348 if (ptr == block->last) {
16349 break;
16350 }
16351 }
16352}
16353
16354
16355static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016356 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016357{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016358 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016359 struct triple *ptr, *next, *last;
16360 int done;
16361 if (!block)
16362 return;
16363 last = block->first;
16364 done = 0;
16365 for(ptr = block->first; !done; ptr = next) {
16366 next = ptr->next;
16367 if (ptr == block->last) {
16368 done = 1;
16369 }
16370 /* RHS(A) */
16371 if (ptr->op == OP_READ) {
16372 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016373 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016374 if (!triple_is_auto_var(state, var)) {
16375 internal_error(state, ptr, "read of non auto var!");
16376 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016377 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016378 /* Find the current value of the variable */
16379 val = peek_triple(stacks, var);
16380 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016381 /* Let the optimizer at variables that are not initially
16382 * set. But give it a bogus value so things seem to
16383 * work by accident. This is useful for bitfields because
16384 * setting them always involves a read-modify-write.
16385 */
16386 if (TYPE_ARITHMETIC(ptr->type->type)) {
16387 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16388 val->u.cval = 0xdeadbeaf;
16389 } else {
16390 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16391 }
16392 }
16393 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016394 error(state, ptr, "variable used without being set");
16395 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016396 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16397 internal_error(state, val, "bad value in read");
16398 }
16399 propogate_use(state, ptr, val);
16400 release_triple(state, ptr);
16401 continue;
16402 }
16403 /* LHS(A) */
16404 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016405 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016406 var = MISC(ptr, 0);
16407 if (!triple_is_auto_var(state, var)) {
16408 internal_error(state, ptr, "write to non auto var!");
16409 }
16410 tval = val = RHS(ptr, 0);
16411 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16412 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016413 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016414 }
Eric Biederman90089602004-05-28 14:11:54 +000016415 /* Insert a cast if the types differ */
16416 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016417 if (val->op == OP_INTCONST) {
16418 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16419 tval->u.cval = val->u.cval;
16420 }
16421 else {
Eric Biederman90089602004-05-28 14:11:54 +000016422 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016423 use_triple(val, tval);
16424 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016425 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016426 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016427 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016428 use_triple(tval, ptr);
16429 }
16430 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016431 unuse_triple(var, ptr);
16432 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016433 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016434 }
16435 if (ptr->op == OP_PHI) {
16436 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016437 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016438 if (!triple_is_auto_var(state, var)) {
16439 internal_error(state, ptr, "phi references non auto var!");
16440 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016441 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016442 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016443 }
16444 last = ptr;
16445 }
16446 block->last = last;
16447
16448 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016449 for(edge = block->edges; edge; edge = edge->next) {
16450 fixup_block_phi_variables(state, stacks, block, edge->member);
16451 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016452 /* rename variables in the dominated nodes */
16453 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016454 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016455 }
16456 /* pop the renamed variable stack */
16457 last = block->first;
16458 done = 0;
16459 for(ptr = block->first; !done ; ptr = next) {
16460 next = ptr->next;
16461 if (ptr == block->last) {
16462 done = 1;
16463 }
16464 if (ptr->op == OP_WRITE) {
16465 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016466 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016467 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016468 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016469 release_triple(state, ptr);
16470 continue;
16471 }
16472 if (ptr->op == OP_PHI) {
16473 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016474 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016475 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016476 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016477 }
16478 last = ptr;
16479 }
16480 block->last = last;
16481}
16482
Eric Biederman83b991a2003-10-11 06:20:25 +000016483static void rename_variables(struct compile_state *state)
16484{
16485 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016486 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016487
16488 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016489 auto_vars = count_auto_vars(state);
16490 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016491
Eric Biederman90089602004-05-28 14:11:54 +000016492 /* Give each auto_var a stack */
16493 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016494
16495 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016496 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016497
Eric Biederman90089602004-05-28 14:11:54 +000016498 /* Remove the stacks from the auto_vars */
16499 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016500 xfree(stacks);
16501}
16502
Eric Biedermanb138ac82003-04-22 18:44:01 +000016503static void prune_block_variables(struct compile_state *state,
16504 struct block *block)
16505{
16506 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016507 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016508 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016509
Eric Biedermanb138ac82003-04-22 18:44:01 +000016510 done = 0;
16511 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016512 /* Be extremely careful I am deleting the list
16513 * as I walk trhough it.
16514 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016515 next = ptr->next;
16516 if (ptr == block->last) {
16517 done = 1;
16518 }
Eric Biederman90089602004-05-28 14:11:54 +000016519 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016520 struct triple_set *user, *next;
16521 for(user = ptr->use; user; user = next) {
16522 struct triple *use;
16523 next = user->next;
16524 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016525 if (MISC(ptr, 0) == user->member) {
16526 continue;
16527 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016528 if (use->op != OP_PHI) {
16529 internal_error(state, use, "decl still used");
16530 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016531 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016532 internal_error(state, use, "bad phi use of decl");
16533 }
16534 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016535 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016536 }
Eric Biederman90089602004-05-28 14:11:54 +000016537 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16538 /* Delete the adecl */
16539 release_triple(state, MISC(ptr, 0));
16540 /* And the piece */
16541 release_triple(state, ptr);
16542 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016543 continue;
16544 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016545 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016546 for(user = block->idominates; user; user = user->next) {
16547 prune_block_variables(state, user->member);
16548 }
16549}
16550
Eric Biederman66fe2222003-07-04 15:14:04 +000016551struct phi_triple {
16552 struct triple *phi;
16553 unsigned orig_id;
16554 int alive;
16555};
16556
16557static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16558{
16559 struct triple **slot;
16560 int zrhs, i;
16561 if (live[phi->id].alive) {
16562 return;
16563 }
16564 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016565 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016566 slot = &RHS(phi, 0);
16567 for(i = 0; i < zrhs; i++) {
16568 struct triple *used;
16569 used = slot[i];
16570 if (used && (used->op == OP_PHI)) {
16571 keep_phi(state, live, used);
16572 }
16573 }
16574}
16575
16576static void prune_unused_phis(struct compile_state *state)
16577{
16578 struct triple *first, *phi;
16579 struct phi_triple *live;
16580 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016581
Eric Biederman66fe2222003-07-04 15:14:04 +000016582 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016583 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016584
16585 /* Count how many phi functions I need to process */
16586 phis = 0;
16587 for(phi = first->next; phi != first; phi = phi->next) {
16588 if (phi->op == OP_PHI) {
16589 phis += 1;
16590 }
16591 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016592
Eric Biederman66fe2222003-07-04 15:14:04 +000016593 /* Mark them all dead */
16594 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16595 phis = 0;
16596 for(phi = first->next; phi != first; phi = phi->next) {
16597 if (phi->op != OP_PHI) {
16598 continue;
16599 }
16600 live[phis].alive = 0;
16601 live[phis].orig_id = phi->id;
16602 live[phis].phi = phi;
16603 phi->id = phis;
16604 phis += 1;
16605 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016606
Eric Biederman66fe2222003-07-04 15:14:04 +000016607 /* Mark phis alive that are used by non phis */
16608 for(i = 0; i < phis; i++) {
16609 struct triple_set *set;
16610 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16611 if (set->member->op != OP_PHI) {
16612 keep_phi(state, live, live[i].phi);
16613 break;
16614 }
16615 }
16616 }
16617
16618 /* Delete the extraneous phis */
16619 for(i = 0; i < phis; i++) {
16620 struct triple **slot;
16621 int zrhs, j;
16622 if (!live[i].alive) {
16623 release_triple(state, live[i].phi);
16624 continue;
16625 }
16626 phi = live[i].phi;
16627 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016628 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016629 for(j = 0; j < zrhs; j++) {
16630 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016631 struct triple *unknown;
16632 get_occurance(phi->occurance);
16633 unknown = flatten(state, state->global_pool,
16634 alloc_triple(state, OP_UNKNOWNVAL,
16635 phi->type, 0, 0, phi->occurance));
16636 slot[j] = unknown;
16637 use_triple(unknown, phi);
16638 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016639#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016640 warning(state, phi, "variable not set at index %d on all paths to use", j);
16641#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016642 }
16643 }
16644 }
16645 xfree(live);
16646}
16647
Eric Biedermanb138ac82003-04-22 18:44:01 +000016648static void transform_to_ssa_form(struct compile_state *state)
16649{
16650 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016651 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016652
Eric Biederman90089602004-05-28 14:11:54 +000016653 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016654 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016655
Eric Biederman90089602004-05-28 14:11:54 +000016656 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016657}
16658
16659
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016660static void clear_vertex(
16661 struct compile_state *state, struct block *block, void *arg)
16662{
Eric Biederman83b991a2003-10-11 06:20:25 +000016663 /* Clear the current blocks vertex and the vertex of all
16664 * of the current blocks neighbors in case there are malformed
16665 * blocks with now instructions at this point.
16666 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016667 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016668 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016669 for(edge = block->edges; edge; edge = edge->next) {
16670 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016671 }
16672 for(user = block->use; user; user = user->next) {
16673 user->member->vertex = 0;
16674 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016675}
16676
16677static void mark_live_block(
16678 struct compile_state *state, struct block *block, int *next_vertex)
16679{
16680 /* See if this is a block that has not been marked */
16681 if (block->vertex != 0) {
16682 return;
16683 }
16684 block->vertex = *next_vertex;
16685 *next_vertex += 1;
16686 if (triple_is_branch(state, block->last)) {
16687 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016688 targ = triple_edge_targ(state, block->last, 0);
16689 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016690 if (!*targ) {
16691 continue;
16692 }
16693 if (!triple_stores_block(state, *targ)) {
16694 internal_error(state, 0, "bad targ");
16695 }
16696 mark_live_block(state, (*targ)->u.block, next_vertex);
16697 }
Eric Biederman90089602004-05-28 14:11:54 +000016698 /* Ensure the last block of a function remains alive */
16699 if (triple_is_call(state, block->last)) {
16700 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16701 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016702 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016703 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016704 struct triple *ins;
16705 ins = block->last->next;
16706 if (!triple_stores_block(state, ins)) {
16707 internal_error(state, 0, "bad block start");
16708 }
16709 mark_live_block(state, ins->u.block, next_vertex);
16710 }
16711}
16712
Eric Biedermanb138ac82003-04-22 18:44:01 +000016713static void transform_from_ssa_form(struct compile_state *state)
16714{
16715 /* To get out of ssa form we insert moves on the incoming
16716 * edges to blocks containting phi functions.
16717 */
16718 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016719 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016720 int next_vertex;
16721
16722 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016723 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016724 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016725 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016726
16727 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016728 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016729 for(phi = first->next; phi != first ; phi = next) {
16730 struct block_set *set;
16731 struct block *block;
16732 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016733 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016734 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016735 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016736 next = phi->next;
16737 if (phi->op != OP_PHI) {
16738 continue;
16739 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016740
Eric Biedermanb138ac82003-04-22 18:44:01 +000016741 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016742 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016743
Eric Biederman83b991a2003-10-11 06:20:25 +000016744 /* If this phi is in a dead block just forget it */
16745 if (block->vertex == 0) {
16746 release_triple(state, phi);
16747 continue;
16748 }
16749
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016750 /* Forget uses from code in dead blocks */
16751 for(use = phi->use; use; use = use_next) {
16752 struct block *ublock;
16753 struct triple **expr;
16754 use_next = use->next;
16755 ublock = block_of_triple(state, use->member);
16756 if ((use->member == phi) || (ublock->vertex != 0)) {
16757 continue;
16758 }
16759 expr = triple_rhs(state, use->member, 0);
16760 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16761 if (*expr == phi) {
16762 *expr = 0;
16763 }
16764 }
16765 unuse_triple(phi, use->member);
16766 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016767 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016768 if (registers_of(state, phi->type) != 1) {
16769 internal_error(state, phi, "phi->type does not fit in a single register!");
16770 }
16771 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16772 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016773
Eric Biederman83b991a2003-10-11 06:20:25 +000016774 /* Replaces use of phi with var */
16775 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016776
Eric Biederman90089602004-05-28 14:11:54 +000016777 /* Count the readers */
16778 readers = 0;
16779 for(use = var->use; use; use = use->next) {
16780 if (use->member != MISC(var, 0)) {
16781 readers++;
16782 }
16783 }
16784
Eric Biedermanb138ac82003-04-22 18:44:01 +000016785 /* Walk all of the incoming edges/blocks and insert moves.
16786 */
Eric Biederman90089602004-05-28 14:11:54 +000016787 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016788 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016789 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016790 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016791 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016792 eblock = set->member;
16793 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016794 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016795 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016796 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016797
Eric Biederman83b991a2003-10-11 06:20:25 +000016798 /* If we don't have a value that belongs in an OP_WRITE
16799 * continue on.
16800 */
Eric Biederman90089602004-05-28 14:11:54 +000016801 if (!val || (val == &unknown_triple) || (val == phi)
16802 || (vblock && (vblock->vertex == 0))) {
16803 continue;
16804 }
16805 /* If the value should never occur error */
16806 if (!vblock) {
16807 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016808 continue;
16809 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016810
16811 /* If the value occurs in a dead block see if a replacement
16812 * block can be found.
16813 */
16814 while(eblock && (eblock->vertex == 0)) {
16815 eblock = eblock->idom;
16816 }
16817 /* If not continue on with the next value. */
16818 if (!eblock || (eblock->vertex == 0)) {
16819 continue;
16820 }
16821
16822 /* If we have an empty incoming block ignore it. */
16823 if (!eblock->first) {
16824 internal_error(state, 0, "empty block?");
16825 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016826
Eric Biederman530b5192003-07-01 10:05:30 +000016827 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016828 /* Walk through the edge block backwards to find an
16829 * appropriate location for the OP_WRITE.
16830 */
16831 for(base = eblock->last; base != eblock->first; base = base->prev) {
16832 struct triple **expr;
16833 if (base->op == OP_PIECE) {
16834 base = MISC(base, 0);
16835 }
16836 if ((base == var) || (base == val)) {
16837 goto out;
16838 }
16839 expr = triple_lhs(state, base, 0);
16840 for(; expr; expr = triple_lhs(state, base, expr)) {
16841 if ((*expr) == val) {
16842 goto out;
16843 }
16844 }
16845 expr = triple_rhs(state, base, 0);
16846 for(; expr; expr = triple_rhs(state, base, expr)) {
16847 if ((*expr) == var) {
16848 goto out;
16849 }
16850 }
Eric Biederman530b5192003-07-01 10:05:30 +000016851 }
Eric Biederman90089602004-05-28 14:11:54 +000016852 out:
16853 if (triple_is_branch(state, base)) {
16854 internal_error(state, base,
16855 "Could not insert write to phi");
16856 }
16857 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016858 use_triple(val, move);
16859 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016860 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016861 }
Eric Biederman90089602004-05-28 14:11:54 +000016862 if (!writers && readers) {
16863 internal_error(state, var, "no value written to in use phi?");
16864 }
16865 /* If var is not used free it */
16866 if (!writers) {
16867 release_triple(state, MISC(var, 0));
16868 release_triple(state, var);
16869 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016870 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016871 release_triple(state, phi);
16872 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016873
Eric Biederman83b991a2003-10-11 06:20:25 +000016874 /* Walk all of the operations to find the adecls */
16875 for(var = first->next; var != first ; var = var->next) {
16876 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016877 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016878 continue;
16879 }
16880
16881 /* Walk through all of the rhs uses of var and
16882 * replace them with read of var.
16883 */
16884 for(use = var->use; use; use = use_next) {
16885 struct triple *read, *user;
16886 struct triple **slot;
16887 int zrhs, i, used;
16888 use_next = use->next;
16889 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016890
Eric Biederman83b991a2003-10-11 06:20:25 +000016891 /* Generate a read of var */
16892 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16893 use_triple(var, read);
16894
16895 /* Find the rhs uses and see if they need to be replaced */
16896 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016897 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016898 slot = &RHS(user, 0);
16899 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016900 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016901 slot[i] = read;
16902 used = 1;
16903 }
16904 }
16905 /* If we did use it cleanup the uses */
16906 if (used) {
16907 unuse_triple(var, user);
16908 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016909 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016910 /* If we didn't use it release the extra triple */
16911 else {
16912 release_triple(state, read);
16913 }
16914 }
16915 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016916}
16917
Eric Biederman5ade04a2003-10-22 04:03:46 +000016918#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016919 FILE *fp = state->dbgout; \
16920 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016921 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016922
Eric Biederman83b991a2003-10-11 06:20:25 +000016923static void rebuild_ssa_form(struct compile_state *state)
16924{
16925HI();
16926 transform_from_ssa_form(state);
16927HI();
Eric Biederman90089602004-05-28 14:11:54 +000016928 state->bb.first = state->first;
16929 free_basic_blocks(state, &state->bb);
16930 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016931HI();
16932 insert_phi_operations(state);
16933HI();
16934 rename_variables(state);
16935HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016936
Eric Biederman90089602004-05-28 14:11:54 +000016937 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016938HI();
16939 prune_unused_phis(state);
16940HI();
16941}
16942#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016943
Stefan Reinauer14e22772010-04-27 06:56:47 +000016944/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016945 * Register conflict resolution
16946 * =========================================================
16947 */
16948
16949static struct reg_info find_def_color(
16950 struct compile_state *state, struct triple *def)
16951{
16952 struct triple_set *set;
16953 struct reg_info info;
16954 info.reg = REG_UNSET;
16955 info.regcm = 0;
16956 if (!triple_is_def(state, def)) {
16957 return info;
16958 }
16959 info = arch_reg_lhs(state, def, 0);
16960 if (info.reg >= MAX_REGISTERS) {
16961 info.reg = REG_UNSET;
16962 }
16963 for(set = def->use; set; set = set->next) {
16964 struct reg_info tinfo;
16965 int i;
16966 i = find_rhs_use(state, set->member, def);
16967 if (i < 0) {
16968 continue;
16969 }
16970 tinfo = arch_reg_rhs(state, set->member, i);
16971 if (tinfo.reg >= MAX_REGISTERS) {
16972 tinfo.reg = REG_UNSET;
16973 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016974 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016975 (info.reg != REG_UNSET) &&
16976 (tinfo.reg != info.reg)) {
16977 internal_error(state, def, "register conflict");
16978 }
16979 if ((info.regcm & tinfo.regcm) == 0) {
16980 internal_error(state, def, "regcm conflict %x & %x == 0",
16981 info.regcm, tinfo.regcm);
16982 }
16983 if (info.reg == REG_UNSET) {
16984 info.reg = tinfo.reg;
16985 }
16986 info.regcm &= tinfo.regcm;
16987 }
16988 if (info.reg >= MAX_REGISTERS) {
16989 internal_error(state, def, "register out of range");
16990 }
16991 return info;
16992}
16993
16994static struct reg_info find_lhs_pre_color(
16995 struct compile_state *state, struct triple *ins, int index)
16996{
16997 struct reg_info info;
16998 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016999 zrhs = ins->rhs;
17000 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017001 if (!zlhs && triple_is_def(state, ins)) {
17002 zlhs = 1;
17003 }
17004 if (index >= zlhs) {
17005 internal_error(state, ins, "Bad lhs %d", index);
17006 }
17007 info = arch_reg_lhs(state, ins, index);
17008 for(i = 0; i < zrhs; i++) {
17009 struct reg_info rinfo;
17010 rinfo = arch_reg_rhs(state, ins, i);
17011 if ((info.reg == rinfo.reg) &&
17012 (rinfo.reg >= MAX_REGISTERS)) {
17013 struct reg_info tinfo;
17014 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17015 info.reg = tinfo.reg;
17016 info.regcm &= tinfo.regcm;
17017 break;
17018 }
17019 }
17020 if (info.reg >= MAX_REGISTERS) {
17021 info.reg = REG_UNSET;
17022 }
17023 return info;
17024}
17025
17026static struct reg_info find_rhs_post_color(
17027 struct compile_state *state, struct triple *ins, int index);
17028
17029static struct reg_info find_lhs_post_color(
17030 struct compile_state *state, struct triple *ins, int index)
17031{
17032 struct triple_set *set;
17033 struct reg_info info;
17034 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017035#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017036 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017037 ins, index);
17038#endif
17039 if ((index == 0) && triple_is_def(state, ins)) {
17040 lhs = ins;
17041 }
Eric Biederman90089602004-05-28 14:11:54 +000017042 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017043 lhs = LHS(ins, index);
17044 }
17045 else {
17046 internal_error(state, ins, "Bad lhs %d", index);
17047 lhs = 0;
17048 }
17049 info = arch_reg_lhs(state, ins, index);
17050 if (info.reg >= MAX_REGISTERS) {
17051 info.reg = REG_UNSET;
17052 }
17053 for(set = lhs->use; set; set = set->next) {
17054 struct reg_info rinfo;
17055 struct triple *user;
17056 int zrhs, i;
17057 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017058 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017059 for(i = 0; i < zrhs; i++) {
17060 if (RHS(user, i) != lhs) {
17061 continue;
17062 }
17063 rinfo = find_rhs_post_color(state, user, i);
17064 if ((info.reg != REG_UNSET) &&
17065 (rinfo.reg != REG_UNSET) &&
17066 (info.reg != rinfo.reg)) {
17067 internal_error(state, ins, "register conflict");
17068 }
17069 if ((info.regcm & rinfo.regcm) == 0) {
17070 internal_error(state, ins, "regcm conflict %x & %x == 0",
17071 info.regcm, rinfo.regcm);
17072 }
17073 if (info.reg == REG_UNSET) {
17074 info.reg = rinfo.reg;
17075 }
17076 info.regcm &= rinfo.regcm;
17077 }
17078 }
Eric Biederman530b5192003-07-01 10:05:30 +000017079#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017080 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017081 ins, index, info.reg, info.regcm);
17082#endif
17083 return info;
17084}
17085
17086static struct reg_info find_rhs_post_color(
17087 struct compile_state *state, struct triple *ins, int index)
17088{
17089 struct reg_info info, rinfo;
17090 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017091#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017092 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017093 ins, index);
17094#endif
17095 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017096 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017097 if (!zlhs && triple_is_def(state, ins)) {
17098 zlhs = 1;
17099 }
17100 info = rinfo;
17101 if (info.reg >= MAX_REGISTERS) {
17102 info.reg = REG_UNSET;
17103 }
17104 for(i = 0; i < zlhs; i++) {
17105 struct reg_info linfo;
17106 linfo = arch_reg_lhs(state, ins, i);
17107 if ((linfo.reg == rinfo.reg) &&
17108 (linfo.reg >= MAX_REGISTERS)) {
17109 struct reg_info tinfo;
17110 tinfo = find_lhs_post_color(state, ins, i);
17111 if (tinfo.reg >= MAX_REGISTERS) {
17112 tinfo.reg = REG_UNSET;
17113 }
Eric Biederman530b5192003-07-01 10:05:30 +000017114 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017115 info.regcm &= tinfo.regcm;
17116 if (info.reg != REG_UNSET) {
17117 internal_error(state, ins, "register conflict");
17118 }
17119 if (info.regcm == 0) {
17120 internal_error(state, ins, "regcm conflict");
17121 }
17122 info.reg = tinfo.reg;
17123 }
17124 }
Eric Biederman530b5192003-07-01 10:05:30 +000017125#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017126 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017127 ins, index, info.reg, info.regcm);
17128#endif
17129 return info;
17130}
17131
17132static struct reg_info find_lhs_color(
17133 struct compile_state *state, struct triple *ins, int index)
17134{
17135 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017136#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017137 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017138 ins, index);
17139#endif
17140 pre = find_lhs_pre_color(state, ins, index);
17141 post = find_lhs_post_color(state, ins, index);
17142 if ((pre.reg != post.reg) &&
17143 (pre.reg != REG_UNSET) &&
17144 (post.reg != REG_UNSET)) {
17145 internal_error(state, ins, "register conflict");
17146 }
17147 info.regcm = pre.regcm & post.regcm;
17148 info.reg = pre.reg;
17149 if (info.reg == REG_UNSET) {
17150 info.reg = post.reg;
17151 }
Eric Biederman530b5192003-07-01 10:05:30 +000017152#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017153 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017154 ins, index, info.reg, info.regcm,
17155 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017156#endif
17157 return info;
17158}
17159
17160static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17161{
17162 struct triple_set *entry, *next;
17163 struct triple *out;
17164 struct reg_info info, rinfo;
17165
17166 info = arch_reg_lhs(state, ins, 0);
17167 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17168 use_triple(RHS(out, 0), out);
17169 /* Get the users of ins to use out instead */
17170 for(entry = ins->use; entry; entry = next) {
17171 int i;
17172 next = entry->next;
17173 if (entry->member == out) {
17174 continue;
17175 }
17176 i = find_rhs_use(state, entry->member, ins);
17177 if (i < 0) {
17178 continue;
17179 }
17180 rinfo = arch_reg_rhs(state, entry->member, i);
17181 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17182 continue;
17183 }
17184 replace_rhs_use(state, ins, out, entry->member);
17185 }
17186 transform_to_arch_instruction(state, out);
17187 return out;
17188}
17189
Eric Biedermand1ea5392003-06-28 06:49:45 +000017190static struct triple *typed_pre_copy(
17191 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017192{
17193 /* Carefully insert enough operations so that I can
17194 * enter any operation with a GPR32.
17195 */
17196 struct triple *in;
17197 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017198 unsigned classes;
17199 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017200 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017201 if (ins->op == OP_PHI) {
17202 internal_error(state, ins, "pre_copy on a phi?");
17203 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017204 classes = arch_type_to_regcm(state, type);
17205 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017206 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017207 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017208 FILE *fp = state->errout;
17209 fprintf(fp, "src_type: ");
17210 name_of(fp, ins->type);
17211 fprintf(fp, "\ndst_type: ");
17212 name_of(fp, type);
17213 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017214 internal_error(state, ins, "pre_copy with no register classes");
17215 }
Eric Biederman90089602004-05-28 14:11:54 +000017216 op = OP_COPY;
17217 if (!equiv_types(type, (*expr)->type)) {
17218 op = OP_CONVERT;
17219 }
17220 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017221 unuse_triple(*expr, ins);
17222 *expr = in;
17223 use_triple(RHS(in, 0), in);
17224 use_triple(in, ins);
17225 transform_to_arch_instruction(state, in);
17226 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017227
Eric Biedermand1ea5392003-06-28 06:49:45 +000017228}
17229static struct triple *pre_copy(
17230 struct compile_state *state, struct triple *ins, int index)
17231{
17232 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017233}
17234
17235
Eric Biedermanb138ac82003-04-22 18:44:01 +000017236static void insert_copies_to_phi(struct compile_state *state)
17237{
17238 /* To get out of ssa form we insert moves on the incoming
17239 * edges to blocks containting phi functions.
17240 */
17241 struct triple *first;
17242 struct triple *phi;
17243
17244 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017245 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017246 for(phi = first->next; phi != first ; phi = phi->next) {
17247 struct block_set *set;
17248 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017249 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017250 int edge;
17251 if (phi->op != OP_PHI) {
17252 continue;
17253 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017254 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017255 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017256 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017257 /* Phi's that feed into mandatory live range joins
17258 * cause nasty complications. Insert a copy of
17259 * the phi value so I never have to deal with
17260 * that in the rest of the code.
17261 */
17262 copy = post_copy(state, phi);
17263 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017264 /* Walk all of the incoming edges/blocks and insert moves.
17265 */
17266 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17267 struct block *eblock;
17268 struct triple *move;
17269 struct triple *val;
17270 struct triple *ptr;
17271 eblock = set->member;
17272 val = slot[edge];
17273
17274 if (val == phi) {
17275 continue;
17276 }
17277
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017278 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017279 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017280 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017281 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017282 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017283 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017284
Eric Biedermanb138ac82003-04-22 18:44:01 +000017285 slot[edge] = move;
17286 unuse_triple(val, phi);
17287 use_triple(move, phi);
17288
Eric Biederman66fe2222003-07-04 15:14:04 +000017289 /* Walk up the dominator tree until I have found the appropriate block */
17290 while(eblock && !tdominates(state, val, eblock->last)) {
17291 eblock = eblock->idom;
17292 }
17293 if (!eblock) {
17294 internal_error(state, phi, "Cannot find block dominated by %p",
17295 val);
17296 }
17297
Eric Biedermanb138ac82003-04-22 18:44:01 +000017298 /* Walk through the block backwards to find
17299 * an appropriate location for the OP_COPY.
17300 */
17301 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17302 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017303 if (ptr->op == OP_PIECE) {
17304 ptr = MISC(ptr, 0);
17305 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017306 if ((ptr == phi) || (ptr == val)) {
17307 goto out;
17308 }
Eric Biederman90089602004-05-28 14:11:54 +000017309 expr = triple_lhs(state, ptr, 0);
17310 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17311 if ((*expr) == val) {
17312 goto out;
17313 }
17314 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017315 expr = triple_rhs(state, ptr, 0);
17316 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17317 if ((*expr) == phi) {
17318 goto out;
17319 }
17320 }
17321 }
17322 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017323 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017324 internal_error(state, ptr,
17325 "Could not insert write to phi");
17326 }
Eric Biederman90089602004-05-28 14:11:54 +000017327 insert_triple(state, after_lhs(state, ptr), move);
17328 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017329 eblock->last = move;
17330 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017331 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017332 }
17333 }
Eric Biederman90089602004-05-28 14:11:54 +000017334 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335}
17336
Eric Biederman90089602004-05-28 14:11:54 +000017337struct triple_reg_set;
17338struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017339
Eric Biedermanb138ac82003-04-22 18:44:01 +000017340
Stefan Reinauer14e22772010-04-27 06:56:47 +000017341static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017342 struct triple *member, struct triple *new_member)
17343{
17344 struct triple_reg_set **ptr, *new;
17345 if (!member)
17346 return 0;
17347 ptr = head;
17348 while(*ptr) {
17349 if ((*ptr)->member == member) {
17350 return 0;
17351 }
17352 ptr = &(*ptr)->next;
17353 }
17354 new = xcmalloc(sizeof(*new), "triple_set");
17355 new->member = member;
17356 new->new = new_member;
17357 new->next = *head;
17358 *head = new;
17359 return 1;
17360}
17361
17362static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17363{
17364 struct triple_reg_set *entry, **ptr;
17365 ptr = head;
17366 while(*ptr) {
17367 entry = *ptr;
17368 if (entry->member == member) {
17369 *ptr = entry->next;
17370 xfree(entry);
17371 return;
17372 }
17373 else {
17374 ptr = &entry->next;
17375 }
17376 }
17377}
17378
17379static int in_triple(struct reg_block *rb, struct triple *in)
17380{
17381 return do_triple_set(&rb->in, in, 0);
17382}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017383
17384#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017385static void unin_triple(struct reg_block *rb, struct triple *unin)
17386{
17387 do_triple_unset(&rb->in, unin);
17388}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017389#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017390
17391static int out_triple(struct reg_block *rb, struct triple *out)
17392{
17393 return do_triple_set(&rb->out, out, 0);
17394}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017395#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017396static void unout_triple(struct reg_block *rb, struct triple *unout)
17397{
17398 do_triple_unset(&rb->out, unout);
17399}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017400#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017401
17402static int initialize_regblock(struct reg_block *blocks,
17403 struct block *block, int vertex)
17404{
17405 struct block_set *user;
17406 if (!block || (blocks[block->vertex].block == block)) {
17407 return vertex;
17408 }
17409 vertex += 1;
17410 /* Renumber the blocks in a convinient fashion */
17411 block->vertex = vertex;
17412 blocks[vertex].block = block;
17413 blocks[vertex].vertex = vertex;
17414 for(user = block->use; user; user = user->next) {
17415 vertex = initialize_regblock(blocks, user->member, vertex);
17416 }
17417 return vertex;
17418}
17419
Eric Biederman90089602004-05-28 14:11:54 +000017420static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17421{
17422/* Part to piece is a best attempt and it cannot be correct all by
17423 * itself. If various values are read as different sizes in different
17424 * parts of the code this function cannot work. Or rather it cannot
17425 * work in conjunction with compute_variable_liftimes. As the
17426 * analysis will get confused.
17427 */
17428 struct triple *base;
17429 unsigned reg;
17430 if (!is_lvalue(state, ins)) {
17431 return ins;
17432 }
17433 base = 0;
17434 reg = 0;
17435 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17436 base = MISC(ins, 0);
17437 switch(ins->op) {
17438 case OP_INDEX:
17439 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17440 break;
17441 case OP_DOT:
17442 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17443 break;
17444 default:
17445 internal_error(state, ins, "unhandled part");
17446 break;
17447 }
17448 ins = base;
17449 }
17450 if (base) {
17451 if (reg > base->lhs) {
17452 internal_error(state, base, "part out of range?");
17453 }
17454 ins = LHS(base, reg);
17455 }
17456 return ins;
17457}
17458
Stefan Reinauer14e22772010-04-27 06:56:47 +000017459static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017460 struct triple *ins, struct triple *other)
17461{
17462 if (ins == other) {
17463 return 1;
17464 }
17465 if (ins->op == OP_WRITE) {
17466 ins = part_to_piece(state, MISC(ins, 0));
17467 }
17468 return ins == other;
17469}
17470
Eric Biedermanb138ac82003-04-22 18:44:01 +000017471static int phi_in(struct compile_state *state, struct reg_block *blocks,
17472 struct reg_block *rb, struct block *suc)
17473{
17474 /* Read the conditional input set of a successor block
17475 * (i.e. the input to the phi nodes) and place it in the
17476 * current blocks output set.
17477 */
17478 struct block_set *set;
17479 struct triple *ptr;
17480 int edge;
17481 int done, change;
17482 change = 0;
17483 /* Find the edge I am coming in on */
17484 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17485 if (set->member == rb->block) {
17486 break;
17487 }
17488 }
17489 if (!set) {
17490 internal_error(state, 0, "Not coming on a control edge?");
17491 }
17492 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17493 struct triple **slot, *expr, *ptr2;
17494 int out_change, done2;
17495 done = (ptr == suc->last);
17496 if (ptr->op != OP_PHI) {
17497 continue;
17498 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017499 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017500 expr = slot[edge];
17501 out_change = out_triple(rb, expr);
17502 if (!out_change) {
17503 continue;
17504 }
17505 /* If we don't define the variable also plast it
17506 * in the current blocks input set.
17507 */
17508 ptr2 = rb->block->first;
17509 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017510 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017511 break;
17512 }
17513 done2 = (ptr2 == rb->block->last);
17514 }
17515 if (!done2) {
17516 continue;
17517 }
17518 change |= in_triple(rb, expr);
17519 }
17520 return change;
17521}
17522
17523static int reg_in(struct compile_state *state, struct reg_block *blocks,
17524 struct reg_block *rb, struct block *suc)
17525{
17526 struct triple_reg_set *in_set;
17527 int change;
17528 change = 0;
17529 /* Read the input set of a successor block
17530 * and place it in the current blocks output set.
17531 */
17532 in_set = blocks[suc->vertex].in;
17533 for(; in_set; in_set = in_set->next) {
17534 int out_change, done;
17535 struct triple *first, *last, *ptr;
17536 out_change = out_triple(rb, in_set->member);
17537 if (!out_change) {
17538 continue;
17539 }
17540 /* If we don't define the variable also place it
17541 * in the current blocks input set.
17542 */
17543 first = rb->block->first;
17544 last = rb->block->last;
17545 done = 0;
17546 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017547 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017548 break;
17549 }
17550 done = (ptr == last);
17551 }
17552 if (!done) {
17553 continue;
17554 }
17555 change |= in_triple(rb, in_set->member);
17556 }
17557 change |= phi_in(state, blocks, rb, suc);
17558 return change;
17559}
17560
Eric Biedermanb138ac82003-04-22 18:44:01 +000017561static int use_in(struct compile_state *state, struct reg_block *rb)
17562{
17563 /* Find the variables we use but don't define and add
17564 * it to the current blocks input set.
17565 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017566#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017567#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017568#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017569 struct block *block;
17570 struct triple *ptr;
17571 int done;
17572 int change;
17573 block = rb->block;
17574 change = 0;
17575 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17576 struct triple **expr;
17577 done = (ptr == block->first);
17578 /* The variable a phi function uses depends on the
17579 * control flow, and is handled in phi_in, not
17580 * here.
17581 */
17582 if (ptr->op == OP_PHI) {
17583 continue;
17584 }
17585 expr = triple_rhs(state, ptr, 0);
17586 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17587 struct triple *rhs, *test;
17588 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017589 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017590 if (!rhs) {
17591 continue;
17592 }
Eric Biederman90089602004-05-28 14:11:54 +000017593
17594 /* See if rhs is defined in this block.
17595 * A write counts as a definition.
17596 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017597 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17598 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017599 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017600 rhs = 0;
17601 break;
17602 }
17603 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017604 /* If I still have a valid rhs add it to in */
17605 change |= in_triple(rb, rhs);
17606 }
17607 }
17608 return change;
17609}
17610
17611static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017612 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017613{
17614 struct reg_block *blocks;
17615 int change;
17616 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017617 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17618 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017619 do {
17620 int i;
17621 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017622 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017623 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017624 struct reg_block *rb;
17625 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017626 /* Add the all successor's input set to in */
17627 for(edge = rb->block->edges; edge; edge = edge->next) {
17628 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017629 }
17630 /* Add use to in... */
17631 change |= use_in(state, rb);
17632 }
17633 } while(change);
17634 return blocks;
17635}
17636
Stefan Reinauer14e22772010-04-27 06:56:47 +000017637static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017638 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017639{
17640 int i;
17641 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017642 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017643 struct triple_reg_set *entry, *next;
17644 struct reg_block *rb;
17645 rb = &blocks[i];
17646 for(entry = rb->in; entry ; entry = next) {
17647 next = entry->next;
17648 do_triple_unset(&rb->in, entry->member);
17649 }
17650 for(entry = rb->out; entry; entry = next) {
17651 next = entry->next;
17652 do_triple_unset(&rb->out, entry->member);
17653 }
17654 }
17655 xfree(blocks);
17656
17657}
17658
Eric Biedermanf96a8102003-06-16 16:57:34 +000017659typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017660 struct compile_state *state,
17661 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017662 struct reg_block *rb, struct triple *ins, void *arg);
17663
17664static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017665 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017666 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017667{
17668 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017669
Eric Biederman90089602004-05-28 14:11:54 +000017670 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017671 struct triple_reg_set *live;
17672 struct triple_reg_set *entry, *next;
17673 struct triple *ptr, *prev;
17674 struct reg_block *rb;
17675 struct block *block;
17676 int done;
17677
17678 /* Get the blocks */
17679 rb = &blocks[i];
17680 block = rb->block;
17681
17682 /* Copy out into live */
17683 live = 0;
17684 for(entry = rb->out; entry; entry = next) {
17685 next = entry->next;
17686 do_triple_set(&live, entry->member, entry->new);
17687 }
17688 /* Walk through the basic block calculating live */
17689 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017690 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017691
17692 prev = ptr->prev;
17693 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017694
17695 /* Ensure the current definition is in live */
17696 if (triple_is_def(state, ptr)) {
17697 do_triple_set(&live, ptr, 0);
17698 }
17699
17700 /* Inform the callback function of what is
17701 * going on.
17702 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017703 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017704
Eric Biedermanb138ac82003-04-22 18:44:01 +000017705 /* Remove the current definition from live */
17706 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017707
Eric Biedermanb138ac82003-04-22 18:44:01 +000017708 /* Add the current uses to live.
17709 *
17710 * It is safe to skip phi functions because they do
17711 * not have any block local uses, and the block
17712 * output sets already properly account for what
17713 * control flow depedent uses phi functions do have.
17714 */
17715 if (ptr->op == OP_PHI) {
17716 continue;
17717 }
17718 expr = triple_rhs(state, ptr, 0);
17719 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17720 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017721 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017722 continue;
17723 }
17724 do_triple_set(&live, *expr, 0);
17725 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017726 }
17727 /* Free live */
17728 for(entry = live; entry; entry = next) {
17729 next = entry->next;
17730 do_triple_unset(&live, entry->member);
17731 }
17732 }
17733}
17734
Eric Biederman90089602004-05-28 14:11:54 +000017735struct print_live_variable_info {
17736 struct reg_block *rb;
17737 FILE *fp;
17738};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017739#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017740static void print_live_variables_block(
17741 struct compile_state *state, struct block *block, void *arg)
17742
17743{
17744 struct print_live_variable_info *info = arg;
17745 struct block_set *edge;
17746 FILE *fp = info->fp;
17747 struct reg_block *rb;
17748 struct triple *ptr;
17749 int phi_present;
17750 int done;
17751 rb = &info->rb[block->vertex];
17752
17753 fprintf(fp, "\nblock: %p (%d),",
17754 block, block->vertex);
17755 for(edge = block->edges; edge; edge = edge->next) {
17756 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017757 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017758 edge->member && edge->member->use?edge->member->use->member : 0);
17759 }
17760 fprintf(fp, "\n");
17761 if (rb->in) {
17762 struct triple_reg_set *in_set;
17763 fprintf(fp, " in:");
17764 for(in_set = rb->in; in_set; in_set = in_set->next) {
17765 fprintf(fp, " %-10p", in_set->member);
17766 }
17767 fprintf(fp, "\n");
17768 }
17769 phi_present = 0;
17770 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17771 done = (ptr == block->last);
17772 if (ptr->op == OP_PHI) {
17773 phi_present = 1;
17774 break;
17775 }
17776 }
17777 if (phi_present) {
17778 int edge;
17779 for(edge = 0; edge < block->users; edge++) {
17780 fprintf(fp, " in(%d):", edge);
17781 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17782 struct triple **slot;
17783 done = (ptr == block->last);
17784 if (ptr->op != OP_PHI) {
17785 continue;
17786 }
17787 slot = &RHS(ptr, 0);
17788 fprintf(fp, " %-10p", slot[edge]);
17789 }
17790 fprintf(fp, "\n");
17791 }
17792 }
17793 if (block->first->op == OP_LABEL) {
17794 fprintf(fp, "%p:\n", block->first);
17795 }
17796 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17797 done = (ptr == block->last);
17798 display_triple(fp, ptr);
17799 }
17800 if (rb->out) {
17801 struct triple_reg_set *out_set;
17802 fprintf(fp, " out:");
17803 for(out_set = rb->out; out_set; out_set = out_set->next) {
17804 fprintf(fp, " %-10p", out_set->member);
17805 }
17806 fprintf(fp, "\n");
17807 }
17808 fprintf(fp, "\n");
17809}
17810
Stefan Reinauer14e22772010-04-27 06:56:47 +000017811static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017812 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17813{
17814 struct print_live_variable_info info;
17815 info.rb = rb;
17816 info.fp = fp;
17817 fprintf(fp, "\nlive variables by block\n");
17818 walk_blocks(state, bb, print_live_variables_block, &info);
17819
17820}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017821#endif
Eric Biederman90089602004-05-28 14:11:54 +000017822
Eric Biedermanb138ac82003-04-22 18:44:01 +000017823static int count_triples(struct compile_state *state)
17824{
17825 struct triple *first, *ins;
17826 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017827 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017828 ins = first;
17829 do {
17830 triples++;
17831 ins = ins->next;
17832 } while (ins != first);
17833 return triples;
17834}
Eric Biederman66fe2222003-07-04 15:14:04 +000017835
17836
Eric Biedermanb138ac82003-04-22 18:44:01 +000017837struct dead_triple {
17838 struct triple *triple;
17839 struct dead_triple *work_next;
17840 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017841 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017842 int flags;
17843#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017844#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017845};
17846
Stefan Reinauer14e22772010-04-27 06:56:47 +000017847static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017848 struct dead_triple *dtriple)
17849{
17850 struct triple *first, *ins;
17851 struct dead_triple *dt;
17852 FILE *fp;
17853 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17854 return;
17855 }
17856 fp = state->dbgout;
17857 fprintf(fp, "--------------- dtriples ---------------\n");
17858 first = state->first;
17859 ins = first;
17860 do {
17861 dt = &dtriple[ins->id];
17862 if ((ins->op == OP_LABEL) && (ins->use)) {
17863 fprintf(fp, "\n%p:\n", ins);
17864 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017865 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017866 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17867 display_triple(fp, ins);
17868 if (triple_is_branch(state, ins)) {
17869 fprintf(fp, "\n");
17870 }
17871 ins = ins->next;
17872 } while(ins != first);
17873 fprintf(fp, "\n");
17874}
17875
Eric Biedermanb138ac82003-04-22 18:44:01 +000017876
17877static void awaken(
17878 struct compile_state *state,
17879 struct dead_triple *dtriple, struct triple **expr,
17880 struct dead_triple ***work_list_tail)
17881{
17882 struct triple *triple;
17883 struct dead_triple *dt;
17884 if (!expr) {
17885 return;
17886 }
17887 triple = *expr;
17888 if (!triple) {
17889 return;
17890 }
17891 if (triple->id <= 0) {
17892 internal_error(state, triple, "bad triple id: %d",
17893 triple->id);
17894 }
17895 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017896 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017897 return;
17898 }
17899 dt = &dtriple[triple->id];
17900 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17901 dt->flags |= TRIPLE_FLAG_ALIVE;
17902 if (!dt->work_next) {
17903 **work_list_tail = dt;
17904 *work_list_tail = &dt->work_next;
17905 }
17906 }
17907}
17908
17909static void eliminate_inefectual_code(struct compile_state *state)
17910{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017911 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17912 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017913 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017914
Eric Biederman5ade04a2003-10-22 04:03:46 +000017915 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17916 return;
17917 }
17918
Eric Biedermanb138ac82003-04-22 18:44:01 +000017919 /* Setup the work list */
17920 work_list = 0;
17921 work_list_tail = &work_list;
17922
Eric Biederman83b991a2003-10-11 06:20:25 +000017923 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017924
17925 /* Count how many triples I have */
17926 triples = count_triples(state);
17927
17928 /* Now put then in an array and mark all of the triples dead */
17929 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017930
Eric Biedermanb138ac82003-04-22 18:44:01 +000017931 ins = first;
17932 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017933 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017934 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017935 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017936 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017937 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017938 ins->id = i;
17939 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017940 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017941 awaken(state, dtriple, &ins, &work_list_tail);
17942 }
17943 i++;
17944 ins = ins->next;
17945 } while(ins != first);
17946 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017947 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017948 struct dead_triple *dt;
17949 struct block_set *user;
17950 struct triple **expr;
17951 dt = work_list;
17952 work_list = dt->work_next;
17953 if (!work_list) {
17954 work_list_tail = &work_list;
17955 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017956 /* Make certain the block the current instruction is in lives */
17957 block = block_of_triple(state, dt->triple);
17958 awaken(state, dtriple, &block->first, &work_list_tail);
17959 if (triple_is_branch(state, block->last)) {
17960 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017961 } else {
17962 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017963 }
17964
Eric Biedermanb138ac82003-04-22 18:44:01 +000017965 /* Wake up the data depencencies of this triple */
17966 expr = 0;
17967 do {
17968 expr = triple_rhs(state, dt->triple, expr);
17969 awaken(state, dtriple, expr, &work_list_tail);
17970 } while(expr);
17971 do {
17972 expr = triple_lhs(state, dt->triple, expr);
17973 awaken(state, dtriple, expr, &work_list_tail);
17974 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017975 do {
17976 expr = triple_misc(state, dt->triple, expr);
17977 awaken(state, dtriple, expr, &work_list_tail);
17978 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017979 /* Wake up the forward control dependencies */
17980 do {
17981 expr = triple_targ(state, dt->triple, expr);
17982 awaken(state, dtriple, expr, &work_list_tail);
17983 } while(expr);
17984 /* Wake up the reverse control dependencies of this triple */
17985 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017986 struct triple *last;
17987 last = user->member->last;
17988 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017989#if DEBUG_ROMCC_WARNINGS
17990#warning "Should we bring the awakening noops back?"
17991#endif
17992 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017993 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017994 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017995 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017996 }
17997 }
Eric Biederman90089602004-05-28 14:11:54 +000017998 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017999 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018000 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000018001 (dt->flags & TRIPLE_FLAG_ALIVE)) {
18002 internal_error(state, dt->triple, "noop effective?");
18003 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018004 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018005 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018006 release_triple(state, dt->triple);
18007 }
18008 }
18009 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018010
18011 rebuild_ssa_form(state);
18012
Eric Biederman90089602004-05-28 14:11:54 +000018013 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018014}
18015
18016
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018017static void insert_mandatory_copies(struct compile_state *state)
18018{
18019 struct triple *ins, *first;
18020
18021 /* The object is with a minimum of inserted copies,
18022 * to resolve in fundamental register conflicts between
18023 * register value producers and consumers.
18024 * Theoretically we may be greater than minimal when we
18025 * are inserting copies before instructions but that
18026 * case should be rare.
18027 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018028 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018029 ins = first;
18030 do {
18031 struct triple_set *entry, *next;
18032 struct triple *tmp;
18033 struct reg_info info;
18034 unsigned reg, regcm;
18035 int do_post_copy, do_pre_copy;
18036 tmp = 0;
18037 if (!triple_is_def(state, ins)) {
18038 goto next;
18039 }
18040 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018041 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018042 if (info.reg >= MAX_REGISTERS) {
18043 info.reg = REG_UNSET;
18044 }
Eric Biederman90089602004-05-28 14:11:54 +000018045
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018046 reg = REG_UNSET;
18047 regcm = arch_type_to_regcm(state, ins->type);
18048 do_post_copy = do_pre_copy = 0;
18049
18050 /* Walk through the uses of ins and check for conflicts */
18051 for(entry = ins->use; entry; entry = next) {
18052 struct reg_info rinfo;
18053 int i;
18054 next = entry->next;
18055 i = find_rhs_use(state, entry->member, ins);
18056 if (i < 0) {
18057 continue;
18058 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018059
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018060 /* Find the users color requirements */
18061 rinfo = arch_reg_rhs(state, entry->member, i);
18062 if (rinfo.reg >= MAX_REGISTERS) {
18063 rinfo.reg = REG_UNSET;
18064 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018065
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018066 /* See if I need a pre_copy */
18067 if (rinfo.reg != REG_UNSET) {
18068 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18069 do_pre_copy = 1;
18070 }
18071 reg = rinfo.reg;
18072 }
18073 regcm &= rinfo.regcm;
18074 regcm = arch_regcm_normalize(state, regcm);
18075 if (regcm == 0) {
18076 do_pre_copy = 1;
18077 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018078 /* Always use pre_copies for constants.
18079 * They do not take up any registers until a
18080 * copy places them in one.
18081 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018082 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018083 (rinfo.reg != REG_UNNEEDED)) {
18084 do_pre_copy = 1;
18085 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018086 }
18087 do_post_copy =
18088 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018089 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018090 (reg != REG_UNSET) &&
18091 (info.reg != reg)) ||
18092 ((info.regcm & regcm) == 0));
18093
18094 reg = info.reg;
18095 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018096 /* 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 +000018097 for(entry = ins->use; entry; entry = next) {
18098 struct reg_info rinfo;
18099 int i;
18100 next = entry->next;
18101 i = find_rhs_use(state, entry->member, ins);
18102 if (i < 0) {
18103 continue;
18104 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018105
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018106 /* Find the users color requirements */
18107 rinfo = arch_reg_rhs(state, entry->member, i);
18108 if (rinfo.reg >= MAX_REGISTERS) {
18109 rinfo.reg = REG_UNSET;
18110 }
18111
18112 /* Now see if it is time to do the pre_copy */
18113 if (rinfo.reg != REG_UNSET) {
18114 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18115 ((regcm & rinfo.regcm) == 0) ||
18116 /* Don't let a mandatory coalesce sneak
18117 * into a operation that is marked to prevent
18118 * coalescing.
18119 */
18120 ((reg != REG_UNNEEDED) &&
18121 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18122 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18123 ) {
18124 if (do_pre_copy) {
18125 struct triple *user;
18126 user = entry->member;
18127 if (RHS(user, i) != ins) {
18128 internal_error(state, user, "bad rhs");
18129 }
18130 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018131 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018132 continue;
18133 } else {
18134 do_post_copy = 1;
18135 }
18136 }
18137 reg = rinfo.reg;
18138 }
18139 if ((regcm & rinfo.regcm) == 0) {
18140 if (do_pre_copy) {
18141 struct triple *user;
18142 user = entry->member;
18143 if (RHS(user, i) != ins) {
18144 internal_error(state, user, "bad rhs");
18145 }
18146 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018147 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018148 continue;
18149 } else {
18150 do_post_copy = 1;
18151 }
18152 }
18153 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018154
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018155 }
18156 if (do_post_copy) {
18157 struct reg_info pre, post;
18158 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018159 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018160 pre = arch_reg_lhs(state, ins, 0);
18161 post = arch_reg_lhs(state, tmp, 0);
18162 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18163 internal_error(state, tmp, "useless copy");
18164 }
18165 }
18166 next:
18167 ins = ins->next;
18168 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018169
Eric Biederman90089602004-05-28 14:11:54 +000018170 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018171}
18172
18173
Eric Biedermanb138ac82003-04-22 18:44:01 +000018174struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018175struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018176struct live_range {
18177 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018178 struct live_range_def *defs;
18179/* Note. The list pointed to by defs is kept in order.
18180 * That is baring splits in the flow control
18181 * defs dominates defs->next wich dominates defs->next->next
18182 * etc.
18183 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018184 unsigned color;
18185 unsigned classes;
18186 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018187 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018188 struct live_range *group_next, **group_prev;
18189};
18190
18191struct live_range_edge {
18192 struct live_range_edge *next;
18193 struct live_range *node;
18194};
18195
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018196struct live_range_def {
18197 struct live_range_def *next;
18198 struct live_range_def *prev;
18199 struct live_range *lr;
18200 struct triple *def;
18201 unsigned orig_id;
18202};
18203
Eric Biedermanb138ac82003-04-22 18:44:01 +000018204#define LRE_HASH_SIZE 2048
18205struct lre_hash {
18206 struct lre_hash *next;
18207 struct live_range *left;
18208 struct live_range *right;
18209};
18210
18211
18212struct reg_state {
18213 struct lre_hash *hash[LRE_HASH_SIZE];
18214 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018215 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018216 struct live_range *lr;
18217 struct live_range *low, **low_tail;
18218 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018219 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018220 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018221 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018222};
18223
18224
Eric Biedermand1ea5392003-06-28 06:49:45 +000018225struct print_interference_block_info {
18226 struct reg_state *rstate;
18227 FILE *fp;
18228 int need_edges;
18229};
18230static void print_interference_block(
18231 struct compile_state *state, struct block *block, void *arg)
18232
18233{
18234 struct print_interference_block_info *info = arg;
18235 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018236 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018237 FILE *fp = info->fp;
18238 struct reg_block *rb;
18239 struct triple *ptr;
18240 int phi_present;
18241 int done;
18242 rb = &rstate->blocks[block->vertex];
18243
Eric Biederman5ade04a2003-10-22 04:03:46 +000018244 fprintf(fp, "\nblock: %p (%d),",
18245 block, block->vertex);
18246 for(edge = block->edges; edge; edge = edge->next) {
18247 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018248 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018249 edge->member && edge->member->use?edge->member->use->member : 0);
18250 }
18251 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018252 if (rb->in) {
18253 struct triple_reg_set *in_set;
18254 fprintf(fp, " in:");
18255 for(in_set = rb->in; in_set; in_set = in_set->next) {
18256 fprintf(fp, " %-10p", in_set->member);
18257 }
18258 fprintf(fp, "\n");
18259 }
18260 phi_present = 0;
18261 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18262 done = (ptr == block->last);
18263 if (ptr->op == OP_PHI) {
18264 phi_present = 1;
18265 break;
18266 }
18267 }
18268 if (phi_present) {
18269 int edge;
18270 for(edge = 0; edge < block->users; edge++) {
18271 fprintf(fp, " in(%d):", edge);
18272 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18273 struct triple **slot;
18274 done = (ptr == block->last);
18275 if (ptr->op != OP_PHI) {
18276 continue;
18277 }
18278 slot = &RHS(ptr, 0);
18279 fprintf(fp, " %-10p", slot[edge]);
18280 }
18281 fprintf(fp, "\n");
18282 }
18283 }
18284 if (block->first->op == OP_LABEL) {
18285 fprintf(fp, "%p:\n", block->first);
18286 }
18287 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018288 struct live_range *lr;
18289 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018290 done = (ptr == block->last);
18291 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018292
Eric Biedermand1ea5392003-06-28 06:49:45 +000018293 id = ptr->id;
18294 ptr->id = rstate->lrd[id].orig_id;
18295 SET_REG(ptr->id, lr->color);
18296 display_triple(fp, ptr);
18297 ptr->id = id;
18298
18299 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18300 internal_error(state, ptr, "lr has no defs!");
18301 }
18302 if (info->need_edges) {
18303 if (lr->defs) {
18304 struct live_range_def *lrd;
18305 fprintf(fp, " range:");
18306 lrd = lr->defs;
18307 do {
18308 fprintf(fp, " %-10p", lrd->def);
18309 lrd = lrd->next;
18310 } while(lrd != lr->defs);
18311 fprintf(fp, "\n");
18312 }
18313 if (lr->edges > 0) {
18314 struct live_range_edge *edge;
18315 fprintf(fp, " edges:");
18316 for(edge = lr->edges; edge; edge = edge->next) {
18317 struct live_range_def *lrd;
18318 lrd = edge->node->defs;
18319 do {
18320 fprintf(fp, " %-10p", lrd->def);
18321 lrd = lrd->next;
18322 } while(lrd != edge->node->defs);
18323 fprintf(fp, "|");
18324 }
18325 fprintf(fp, "\n");
18326 }
18327 }
18328 /* Do a bunch of sanity checks */
18329 valid_ins(state, ptr);
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018330 if (ptr->id > rstate->defs) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018331 internal_error(state, ptr, "Invalid triple id: %d",
18332 ptr->id);
18333 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018334 }
18335 if (rb->out) {
18336 struct triple_reg_set *out_set;
18337 fprintf(fp, " out:");
18338 for(out_set = rb->out; out_set; out_set = out_set->next) {
18339 fprintf(fp, " %-10p", out_set->member);
18340 }
18341 fprintf(fp, "\n");
18342 }
18343 fprintf(fp, "\n");
18344}
18345
18346static void print_interference_blocks(
18347 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18348{
18349 struct print_interference_block_info info;
18350 info.rstate = rstate;
18351 info.fp = fp;
18352 info.need_edges = need_edges;
18353 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018354 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018355
18356}
18357
Eric Biedermanb138ac82003-04-22 18:44:01 +000018358static unsigned regc_max_size(struct compile_state *state, int classes)
18359{
18360 unsigned max_size;
18361 int i;
18362 max_size = 0;
18363 for(i = 0; i < MAX_REGC; i++) {
18364 if (classes & (1 << i)) {
18365 unsigned size;
18366 size = arch_regc_size(state, i);
18367 if (size > max_size) {
18368 max_size = size;
18369 }
18370 }
18371 }
18372 return max_size;
18373}
18374
18375static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18376{
18377 unsigned equivs[MAX_REG_EQUIVS];
18378 int i;
18379 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18380 internal_error(state, 0, "invalid register");
18381 }
18382 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18383 internal_error(state, 0, "invalid register");
18384 }
18385 arch_reg_equivs(state, equivs, reg1);
18386 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18387 if (equivs[i] == reg2) {
18388 return 1;
18389 }
18390 }
18391 return 0;
18392}
18393
18394static void reg_fill_used(struct compile_state *state, char *used, int reg)
18395{
18396 unsigned equivs[MAX_REG_EQUIVS];
18397 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018398 if (reg == REG_UNNEEDED) {
18399 return;
18400 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018401 arch_reg_equivs(state, equivs, reg);
18402 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18403 used[equivs[i]] = 1;
18404 }
18405 return;
18406}
18407
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018408static void reg_inc_used(struct compile_state *state, char *used, int reg)
18409{
18410 unsigned equivs[MAX_REG_EQUIVS];
18411 int i;
18412 if (reg == REG_UNNEEDED) {
18413 return;
18414 }
18415 arch_reg_equivs(state, equivs, reg);
18416 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18417 used[equivs[i]] += 1;
18418 }
18419 return;
18420}
18421
Eric Biedermanb138ac82003-04-22 18:44:01 +000018422static unsigned int hash_live_edge(
18423 struct live_range *left, struct live_range *right)
18424{
18425 unsigned int hash, val;
18426 unsigned long lval, rval;
18427 lval = ((unsigned long)left)/sizeof(struct live_range);
18428 rval = ((unsigned long)right)/sizeof(struct live_range);
18429 hash = 0;
18430 while(lval) {
18431 val = lval & 0xff;
18432 lval >>= 8;
18433 hash = (hash *263) + val;
18434 }
18435 while(rval) {
18436 val = rval & 0xff;
18437 rval >>= 8;
18438 hash = (hash *263) + val;
18439 }
18440 hash = hash & (LRE_HASH_SIZE - 1);
18441 return hash;
18442}
18443
18444static struct lre_hash **lre_probe(struct reg_state *rstate,
18445 struct live_range *left, struct live_range *right)
18446{
18447 struct lre_hash **ptr;
18448 unsigned int index;
18449 /* Ensure left <= right */
18450 if (left > right) {
18451 struct live_range *tmp;
18452 tmp = left;
18453 left = right;
18454 right = tmp;
18455 }
18456 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018457
Eric Biedermanb138ac82003-04-22 18:44:01 +000018458 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018459 while(*ptr) {
18460 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18461 break;
18462 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018463 ptr = &(*ptr)->next;
18464 }
18465 return ptr;
18466}
18467
18468static int interfere(struct reg_state *rstate,
18469 struct live_range *left, struct live_range *right)
18470{
18471 struct lre_hash **ptr;
18472 ptr = lre_probe(rstate, left, right);
18473 return ptr && *ptr;
18474}
18475
Stefan Reinauer14e22772010-04-27 06:56:47 +000018476static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018477 struct live_range *left, struct live_range *right)
18478{
18479 /* FIXME the memory allocation overhead is noticeable here... */
18480 struct lre_hash **ptr, *new_hash;
18481 struct live_range_edge *edge;
18482
18483 if (left == right) {
18484 return;
18485 }
18486 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18487 return;
18488 }
18489 /* Ensure left <= right */
18490 if (left > right) {
18491 struct live_range *tmp;
18492 tmp = left;
18493 left = right;
18494 right = tmp;
18495 }
18496 ptr = lre_probe(rstate, left, right);
18497 if (*ptr) {
18498 return;
18499 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018500#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018501 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018502 left, right);
18503#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018504 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18505 new_hash->next = *ptr;
18506 new_hash->left = left;
18507 new_hash->right = right;
18508 *ptr = new_hash;
18509
18510 edge = xmalloc(sizeof(*edge), "live_range_edge");
18511 edge->next = left->edges;
18512 edge->node = right;
18513 left->edges = edge;
18514 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018515
Eric Biedermanb138ac82003-04-22 18:44:01 +000018516 edge = xmalloc(sizeof(*edge), "live_range_edge");
18517 edge->next = right->edges;
18518 edge->node = left;
18519 right->edges = edge;
18520 right->degree += 1;
18521}
18522
18523static void remove_live_edge(struct reg_state *rstate,
18524 struct live_range *left, struct live_range *right)
18525{
18526 struct live_range_edge *edge, **ptr;
18527 struct lre_hash **hptr, *entry;
18528 hptr = lre_probe(rstate, left, right);
18529 if (!hptr || !*hptr) {
18530 return;
18531 }
18532 entry = *hptr;
18533 *hptr = entry->next;
18534 xfree(entry);
18535
18536 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18537 edge = *ptr;
18538 if (edge->node == right) {
18539 *ptr = edge->next;
18540 memset(edge, 0, sizeof(*edge));
18541 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018542 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018543 break;
18544 }
18545 }
18546 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18547 edge = *ptr;
18548 if (edge->node == left) {
18549 *ptr = edge->next;
18550 memset(edge, 0, sizeof(*edge));
18551 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018552 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018553 break;
18554 }
18555 }
18556}
18557
18558static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18559{
18560 struct live_range_edge *edge, *next;
18561 for(edge = range->edges; edge; edge = next) {
18562 next = edge->next;
18563 remove_live_edge(rstate, range, edge->node);
18564 }
18565}
18566
Stefan Reinauer14e22772010-04-27 06:56:47 +000018567static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018568 struct live_range *dest, struct live_range *src)
18569{
18570 struct live_range_edge *edge, *next;
18571 for(edge = src->edges; edge; edge = next) {
18572 struct live_range *other;
18573 next = edge->next;
18574 other = edge->node;
18575 remove_live_edge(rstate, src, other);
18576 add_live_edge(rstate, dest, other);
18577 }
18578}
18579
Eric Biedermanb138ac82003-04-22 18:44:01 +000018580
18581/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018582 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018583 * new(n) --- Return a graph with n nodes but no edges.
18584 * add(g,x,y) --- Return a graph including g with an between x and y
18585 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18586 * x and y in the graph g
18587 * degree(g, x) --- Return the degree of the node x in the graph g
18588 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18589 *
18590 * Implement with a hash table && a set of adjcency vectors.
18591 * The hash table supports constant time implementations of add and interfere.
18592 * The adjacency vectors support an efficient implementation of neighbors.
18593 */
18594
Stefan Reinauer14e22772010-04-27 06:56:47 +000018595/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018596 * +---------------------------------------------------+
18597 * | +--------------+ |
18598 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018599 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018600 *
18601 * -- In simplify implment optimistic coloring... (No backtracking)
18602 * -- Implement Rematerialization it is the only form of spilling we can perform
18603 * Essentially this means dropping a constant from a register because
18604 * we can regenerate it later.
18605 *
18606 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18607 * coalesce at phi points...
18608 * --- Bias coloring if at all possible do the coalesing a compile time.
18609 *
18610 *
18611 */
18612
Stefan Reinauer50542a82007-10-24 11:14:14 +000018613#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018614static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018615 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018616 struct triple *parent, struct triple *ins)
18617{
18618 struct live_range *lr;
18619 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018620 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018621 expr = triple_rhs(state, ins, 0);
18622 for(;expr; expr = triple_rhs(state, ins, expr)) {
18623 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018624 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018625 continue;
18626 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018627 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018628 if (lr->color == lr2->color) {
18629 internal_error(state, ins, "live range too big");
18630 }
18631 }
18632}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018633#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018634
18635static struct live_range *coalesce_ranges(
18636 struct compile_state *state, struct reg_state *rstate,
18637 struct live_range *lr1, struct live_range *lr2)
18638{
18639 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18640 unsigned color;
18641 unsigned classes;
18642 if (lr1 == lr2) {
18643 return lr1;
18644 }
18645 if (!lr1->defs || !lr2->defs) {
18646 internal_error(state, 0,
18647 "cannot coalese dead live ranges");
18648 }
18649 if ((lr1->color == REG_UNNEEDED) ||
18650 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018651 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018652 "cannot coalesce live ranges without a possible color");
18653 }
18654 if ((lr1->color != lr2->color) &&
18655 (lr1->color != REG_UNSET) &&
18656 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018657 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018658 "cannot coalesce live ranges of different colors");
18659 }
18660 color = lr1->color;
18661 if (color == REG_UNSET) {
18662 color = lr2->color;
18663 }
18664 classes = lr1->classes & lr2->classes;
18665 if (!classes) {
18666 internal_error(state, lr1->defs->def,
18667 "cannot coalesce live ranges with dissimilar register classes");
18668 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018669 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018670 FILE *fp = state->errout;
18671 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018672 lrd = lr1->defs;
18673 do {
Eric Biederman90089602004-05-28 14:11:54 +000018674 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018675 lrd = lrd->next;
18676 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018677 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018678 lrd = lr2->defs;
18679 do {
Eric Biederman90089602004-05-28 14:11:54 +000018680 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018681 lrd = lrd->next;
18682 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018683 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018684 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018685 /* If there is a clear dominate live range put it in lr1,
18686 * For purposes of this test phi functions are
18687 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018688 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018689 */
18690 if ((lr1->defs->prev->def->op == OP_PHI) ||
18691 ((lr2->defs->prev->def->op != OP_PHI) &&
18692 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18693 struct live_range *tmp;
18694 tmp = lr1;
18695 lr1 = lr2;
18696 lr2 = tmp;
18697 }
18698#if 0
18699 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018700 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018701 }
18702 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018703 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018704 }
18705 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018706 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018707 }
18708 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018709 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018710 }
18711#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018712#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018713 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018714 lr1->defs->def,
18715 lr1->color,
18716 lr2->defs->def,
18717 lr2->color);
18718#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018719
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018720 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018721#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018722#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018723#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018724 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018725 * and to the necessity of making multiple passes of coalesce_live_ranges.
18726 * A failure to find some coalesce opportunities in coaleace_live_ranges
18727 * does not impact the correct of the compiler just the efficiency with
18728 * which registers are allocated.
18729 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018730 head = lr1->defs;
18731 mid1 = lr1->defs->prev;
18732 mid2 = lr2->defs;
18733 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018734
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018735 head->prev = end;
18736 end->next = head;
18737
18738 mid1->next = mid2;
18739 mid2->prev = mid1;
18740
18741 /* Fixup the live range in the added live range defs */
18742 lrd = head;
18743 do {
18744 lrd->lr = lr1;
18745 lrd = lrd->next;
18746 } while(lrd != head);
18747
18748 /* Mark lr2 as free. */
18749 lr2->defs = 0;
18750 lr2->color = REG_UNNEEDED;
18751 lr2->classes = 0;
18752
18753 if (!lr1->defs) {
18754 internal_error(state, 0, "lr1->defs == 0 ?");
18755 }
18756
18757 lr1->color = color;
18758 lr1->classes = classes;
18759
Eric Biederman153ea352003-06-20 14:43:20 +000018760 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18761 transfer_live_edges(rstate, lr1, lr2);
18762
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018763 return lr1;
18764}
18765
18766static struct live_range_def *live_range_head(
18767 struct compile_state *state, struct live_range *lr,
18768 struct live_range_def *last)
18769{
18770 struct live_range_def *result;
18771 result = 0;
18772 if (last == 0) {
18773 result = lr->defs;
18774 }
18775 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18776 result = last->next;
18777 }
18778 return result;
18779}
18780
18781static struct live_range_def *live_range_end(
18782 struct compile_state *state, struct live_range *lr,
18783 struct live_range_def *last)
18784{
18785 struct live_range_def *result;
18786 result = 0;
18787 if (last == 0) {
18788 result = lr->defs->prev;
18789 }
18790 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18791 result = last->prev;
18792 }
18793 return result;
18794}
18795
18796
Eric Biedermanb138ac82003-04-22 18:44:01 +000018797static void initialize_live_ranges(
18798 struct compile_state *state, struct reg_state *rstate)
18799{
18800 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018801 size_t count, size;
18802 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018803
Eric Biederman83b991a2003-10-11 06:20:25 +000018804 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018805 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018806 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018807 count = count_triples(state);
18808 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018809 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018810 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018811 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018812 /* Potentially I need one live range for each instruction
18813 * plus an extra for the dummy live range.
18814 */
18815 rstate->ranges = count + 1;
18816 size = sizeof(rstate->lrd[0]) * rstate->defs;
18817 rstate->lrd = xcmalloc(size, "live_range_def");
18818 size = sizeof(rstate->lr[0]) * rstate->ranges;
18819 rstate->lr = xcmalloc(size, "live_range");
18820
Eric Biedermanb138ac82003-04-22 18:44:01 +000018821 /* Setup the dummy live range */
18822 rstate->lr[0].classes = 0;
18823 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018824 rstate->lr[0].defs = 0;
18825 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018826 ins = first;
18827 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018828 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018829 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018830 struct reg_info info;
18831 /* Find the architecture specific color information */
18832 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018833 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018834 rstate->lr[i].defs = &rstate->lrd[j];
18835 rstate->lr[i].color = info.reg;
18836 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018837 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018838 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018840 /* Otherwise give the triple the dummy live range. */
18841 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018842 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018843 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018844
18845 /* Initalize the live_range_def */
18846 rstate->lrd[j].next = &rstate->lrd[j];
18847 rstate->lrd[j].prev = &rstate->lrd[j];
18848 rstate->lrd[j].def = ins;
18849 rstate->lrd[j].orig_id = ins->id;
18850 ins->id = j;
18851
18852 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018853 ins = ins->next;
18854 } while(ins != first);
18855 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018856
Eric Biedermanb138ac82003-04-22 18:44:01 +000018857 /* Make a second pass to handle achitecture specific register
18858 * constraints.
18859 */
18860 ins = first;
18861 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018862 int zlhs, zrhs, i, j;
18863 if (ins->id > rstate->defs) {
18864 internal_error(state, ins, "bad id");
18865 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018866
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018867 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018868 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018869 if ((zlhs == 0) && triple_is_def(state, ins)) {
18870 zlhs = 1;
18871 }
Eric Biederman90089602004-05-28 14:11:54 +000018872 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018873
Eric Biederman5ade04a2003-10-22 04:03:46 +000018874 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018875 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018876 ins, zlhs, zrhs);
18877 }
18878
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018879 for(i = 0; i < zlhs; i++) {
18880 struct reg_info linfo;
18881 struct live_range_def *lhs;
18882 linfo = arch_reg_lhs(state, ins, i);
18883 if (linfo.reg < MAX_REGISTERS) {
18884 continue;
18885 }
18886 if (triple_is_def(state, ins)) {
18887 lhs = &rstate->lrd[ins->id];
18888 } else {
18889 lhs = &rstate->lrd[LHS(ins, i)->id];
18890 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018891
18892 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018893 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018894 i, lhs, linfo.reg);
18895 }
18896
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018897 for(j = 0; j < zrhs; j++) {
18898 struct reg_info rinfo;
18899 struct live_range_def *rhs;
18900 rinfo = arch_reg_rhs(state, ins, j);
18901 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018902 continue;
18903 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018904 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018905
18906 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018907 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018908 j, rhs, rinfo.reg);
18909 }
18910
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018911 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018912 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018913 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018914 }
18915 }
18916 }
18917 ins = ins->next;
18918 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018919}
18920
Eric Biedermanf96a8102003-06-16 16:57:34 +000018921static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018922 struct compile_state *state,
18923 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018924 struct reg_block *rb, struct triple *ins, void *arg)
18925{
18926 struct reg_state *rstate = arg;
18927 struct live_range *def;
18928 struct triple_reg_set *entry;
18929
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018930 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018931 * we do not have a definition to add to
18932 * the interference graph.
18933 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018934 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018935 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018936 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018937 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018938
Eric Biedermanb138ac82003-04-22 18:44:01 +000018939 /* Create an edge between ins and everything that is
18940 * alive, unless the live_range cannot share
18941 * a physical register with ins.
18942 */
18943 for(entry = live; entry; entry = entry->next) {
18944 struct live_range *lr;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018945 if (entry->member->id > rstate->defs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018946 internal_error(state, 0, "bad entry?");
18947 }
18948 lr = rstate->lrd[entry->member->id].lr;
18949 if (def == lr) {
18950 continue;
18951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018952 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18953 continue;
18954 }
18955 add_live_edge(rstate, def, lr);
18956 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018957 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018958}
18959
Stefan Reinauer50542a82007-10-24 11:14:14 +000018960#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018961static struct live_range *get_verify_live_range(
18962 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18963{
18964 struct live_range *lr;
18965 struct live_range_def *lrd;
18966 int ins_found;
18967 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18968 internal_error(state, ins, "bad ins?");
18969 }
18970 lr = rstate->lrd[ins->id].lr;
18971 ins_found = 0;
18972 lrd = lr->defs;
18973 do {
18974 if (lrd->def == ins) {
18975 ins_found = 1;
18976 }
18977 lrd = lrd->next;
18978 } while(lrd != lr->defs);
18979 if (!ins_found) {
18980 internal_error(state, ins, "ins not in live range");
18981 }
18982 return lr;
18983}
18984
18985static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018986 struct compile_state *state,
18987 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018988 struct reg_block *rb, struct triple *ins, void *arg)
18989{
18990 struct reg_state *rstate = arg;
18991 struct triple_reg_set *entry1, *entry2;
18992
18993
18994 /* Compare live against edges and make certain the code is working */
18995 for(entry1 = live; entry1; entry1 = entry1->next) {
18996 struct live_range *lr1;
18997 lr1 = get_verify_live_range(state, rstate, entry1->member);
18998 for(entry2 = live; entry2; entry2 = entry2->next) {
18999 struct live_range *lr2;
19000 struct live_range_edge *edge2;
19001 int lr1_found;
19002 int lr2_degree;
19003 if (entry2 == entry1) {
19004 continue;
19005 }
19006 lr2 = get_verify_live_range(state, rstate, entry2->member);
19007 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019008 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019009 "live range with 2 values simultaneously alive");
19010 }
19011 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19012 continue;
19013 }
19014 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019015 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019016 "edges don't interfere?");
19017 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019018
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019019 lr1_found = 0;
19020 lr2_degree = 0;
19021 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19022 lr2_degree++;
19023 if (edge2->node == lr1) {
19024 lr1_found = 1;
19025 }
19026 }
19027 if (lr2_degree != lr2->degree) {
19028 internal_error(state, entry2->member,
19029 "computed degree: %d does not match reported degree: %d\n",
19030 lr2_degree, lr2->degree);
19031 }
19032 if (!lr1_found) {
19033 internal_error(state, entry2->member, "missing edge");
19034 }
19035 }
19036 }
19037 return;
19038}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019039#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019040
Eric Biedermanf96a8102003-06-16 16:57:34 +000019041static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019042 struct compile_state *state,
19043 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019044 struct reg_block *rb, struct triple *ins, void *arg)
19045{
19046 struct reg_state *rstate = arg;
19047 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019048 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019049 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019050
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019051 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019052 id = ins->id;
19053 ins->id = rstate->lrd[id].orig_id;
19054 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019055 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019056 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019057
19058 if (lr->defs) {
19059 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019060 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019061 lrd = lr->defs;
19062 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019063 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019064 lrd = lrd->next;
19065 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019066 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019067 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019068 if (live) {
19069 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019070 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019071 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019072 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019073 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019074 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019075 }
19076 if (lr->edges) {
19077 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019078 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019079 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019080 struct live_range_def *lrd;
19081 lrd = entry->node->defs;
19082 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019083 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019084 lrd = lrd->next;
19085 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019086 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019087 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019088 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019089 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019090 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019091 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019092 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019093 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019094}
19095
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019096static int coalesce_live_ranges(
19097 struct compile_state *state, struct reg_state *rstate)
19098{
19099 /* At the point where a value is moved from one
19100 * register to another that value requires two
19101 * registers, thus increasing register pressure.
19102 * Live range coaleescing reduces the register
19103 * pressure by keeping a value in one register
19104 * longer.
19105 *
19106 * In the case of a phi function all paths leading
19107 * into it must be allocated to the same register
19108 * otherwise the phi function may not be removed.
19109 *
19110 * Forcing a value to stay in a single register
19111 * for an extended period of time does have
19112 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019113 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019114 *
19115 * The two cases I have identified are:
19116 * 1) Two forced register assignments may
19117 * collide.
19118 * 2) Registers may go unused because they
19119 * are only good for storing the value
19120 * and not manipulating it.
19121 *
19122 * Because of this I need to split live ranges,
19123 * even outside of the context of coalesced live
19124 * ranges. The need to split live ranges does
19125 * impose some constraints on live range coalescing.
19126 *
19127 * - Live ranges may not be coalesced across phi
19128 * functions. This creates a 2 headed live
19129 * range that cannot be sanely split.
19130 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019131 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019132 * are handled as pre split live ranges so we will
19133 * never attempt to split them.
19134 */
19135 int coalesced;
19136 int i;
19137
19138 coalesced = 0;
19139 for(i = 0; i <= rstate->ranges; i++) {
19140 struct live_range *lr1;
19141 struct live_range_def *lrd1;
19142 lr1 = &rstate->lr[i];
19143 if (!lr1->defs) {
19144 continue;
19145 }
19146 lrd1 = live_range_end(state, lr1, 0);
19147 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19148 struct triple_set *set;
19149 if (lrd1->def->op != OP_COPY) {
19150 continue;
19151 }
19152 /* Skip copies that are the result of a live range split. */
19153 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19154 continue;
19155 }
19156 for(set = lrd1->def->use; set; set = set->next) {
19157 struct live_range_def *lrd2;
19158 struct live_range *lr2, *res;
19159
19160 lrd2 = &rstate->lrd[set->member->id];
19161
19162 /* Don't coalesce with instructions
19163 * that are the result of a live range
19164 * split.
19165 */
19166 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19167 continue;
19168 }
19169 lr2 = rstate->lrd[set->member->id].lr;
19170 if (lr1 == lr2) {
19171 continue;
19172 }
19173 if ((lr1->color != lr2->color) &&
19174 (lr1->color != REG_UNSET) &&
19175 (lr2->color != REG_UNSET)) {
19176 continue;
19177 }
19178 if ((lr1->classes & lr2->classes) == 0) {
19179 continue;
19180 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019181
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019182 if (interfere(rstate, lr1, lr2)) {
19183 continue;
19184 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019185
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019186 res = coalesce_ranges(state, rstate, lr1, lr2);
19187 coalesced += 1;
19188 if (res != lr1) {
19189 goto next;
19190 }
19191 }
19192 }
19193 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019194 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019195 }
19196 return coalesced;
19197}
19198
19199
Eric Biedermanf96a8102003-06-16 16:57:34 +000019200static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019201 struct reg_block *blocks, struct triple_reg_set *live,
19202 struct reg_block *rb, struct triple *ins, void *arg)
19203{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019204 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019205 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019206
19207 /* See if we have a mandatory coalesce operation between
19208 * a lhs and a rhs value. If so and the rhs value is also
19209 * alive then this triple needs to be pre copied. Otherwise
19210 * we would have two definitions in the same live range simultaneously
19211 * alive.
19212 */
Eric Biederman90089602004-05-28 14:11:54 +000019213 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019214 if ((zlhs == 0) && triple_is_def(state, ins)) {
19215 zlhs = 1;
19216 }
Eric Biederman90089602004-05-28 14:11:54 +000019217 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019218 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019219 struct reg_info linfo;
19220 linfo = arch_reg_lhs(state, ins, i);
19221 if (linfo.reg < MAX_REGISTERS) {
19222 continue;
19223 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019224 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019225 struct reg_info rinfo;
19226 struct triple *rhs;
19227 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019228 int found;
19229 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019230 rinfo = arch_reg_rhs(state, ins, j);
19231 if (rinfo.reg != linfo.reg) {
19232 continue;
19233 }
19234 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019235 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019236 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019237 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019238 }
19239 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019240 if (found) {
19241 struct triple *copy;
19242 copy = pre_copy(state, ins, j);
19243 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019244 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019245 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019246 }
19247 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019248 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019249}
19250
Eric Biedermand1ea5392003-06-28 06:49:45 +000019251static int correct_coalesce_conflicts(
19252 struct compile_state *state, struct reg_block *blocks)
19253{
19254 int conflicts;
19255 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019256 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019257 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019258 return conflicts;
19259}
19260
Eric Biedermanf96a8102003-06-16 16:57:34 +000019261static void replace_set_use(struct compile_state *state,
19262 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019263{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019264 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019265 for(set = head; set; set = set->next) {
19266 if (set->member == orig) {
19267 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019268 }
19269 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019270}
19271
Stefan Reinauer14e22772010-04-27 06:56:47 +000019272static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019273 struct reg_block *blocks, struct triple *orig, struct triple *new)
19274{
19275 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019276#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019277#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019278#endif
Eric Biederman90089602004-05-28 14:11:54 +000019279 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019280 struct reg_block *rb;
19281 rb = &blocks[i];
19282 replace_set_use(state, rb->in, orig, new);
19283 replace_set_use(state, rb->out, orig, new);
19284 }
19285}
19286
19287static void color_instructions(struct compile_state *state)
19288{
19289 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019290 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019291 ins = first;
19292 do {
19293 if (triple_is_def(state, ins)) {
19294 struct reg_info info;
19295 info = find_lhs_color(state, ins, 0);
19296 if (info.reg >= MAX_REGISTERS) {
19297 info.reg = REG_UNSET;
19298 }
19299 SET_INFO(ins->id, info);
19300 }
19301 ins = ins->next;
19302 } while(ins != first);
19303}
19304
19305static struct reg_info read_lhs_color(
19306 struct compile_state *state, struct triple *ins, int index)
19307{
19308 struct reg_info info;
19309 if ((index == 0) && triple_is_def(state, ins)) {
19310 info.reg = ID_REG(ins->id);
19311 info.regcm = ID_REGCM(ins->id);
19312 }
Eric Biederman90089602004-05-28 14:11:54 +000019313 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019314 info = read_lhs_color(state, LHS(ins, index), 0);
19315 }
19316 else {
19317 internal_error(state, ins, "Bad lhs %d", index);
19318 info.reg = REG_UNSET;
19319 info.regcm = 0;
19320 }
19321 return info;
19322}
19323
19324static struct triple *resolve_tangle(
19325 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019326{
19327 struct reg_info info, uinfo;
19328 struct triple_set *set, *next;
19329 struct triple *copy;
19330
Stefan Reinauer50542a82007-10-24 11:14:14 +000019331#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019332#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019333#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019334 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019335 for(set = tangle->use; set; set = next) {
19336 struct triple *user;
19337 int i, zrhs;
19338 next = set->next;
19339 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019340 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019341 for(i = 0; i < zrhs; i++) {
19342 if (RHS(user, i) != tangle) {
19343 continue;
19344 }
19345 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019346 if (uinfo.reg == info.reg) {
19347 copy = pre_copy(state, user, i);
19348 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019349 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350 }
19351 }
19352 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019353 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019354 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019355 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019356 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019357 copy = post_copy(state, tangle);
19358 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019359 linfo = find_lhs_color(state, copy, 0);
19360 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019361 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019362 info = find_lhs_color(state, tangle, 0);
19363 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019364
Eric Biedermanf96a8102003-06-16 16:57:34 +000019365 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019366}
19367
19368
Eric Biedermanf96a8102003-06-16 16:57:34 +000019369static void fix_tangles(struct compile_state *state,
19370 struct reg_block *blocks, struct triple_reg_set *live,
19371 struct reg_block *rb, struct triple *ins, void *arg)
19372{
Eric Biederman153ea352003-06-20 14:43:20 +000019373 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019374 struct triple *tangle;
19375 do {
19376 char used[MAX_REGISTERS];
19377 struct triple_reg_set *set;
19378 tangle = 0;
19379
19380 /* Find out which registers have multiple uses at this point */
19381 memset(used, 0, sizeof(used));
19382 for(set = live; set; set = set->next) {
19383 struct reg_info info;
19384 info = read_lhs_color(state, set->member, 0);
19385 if (info.reg == REG_UNSET) {
19386 continue;
19387 }
19388 reg_inc_used(state, used, info.reg);
19389 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019390
Eric Biedermanf96a8102003-06-16 16:57:34 +000019391 /* Now find the least dominated definition of a register in
19392 * conflict I have seen so far.
19393 */
19394 for(set = live; set; set = set->next) {
19395 struct reg_info info;
19396 info = read_lhs_color(state, set->member, 0);
19397 if (used[info.reg] < 2) {
19398 continue;
19399 }
Eric Biederman153ea352003-06-20 14:43:20 +000019400 /* Changing copies that feed into phi functions
19401 * is incorrect.
19402 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019403 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019404 (set->member->use->member->op == OP_PHI)) {
19405 continue;
19406 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019407 if (!tangle || tdominates(state, set->member, tangle)) {
19408 tangle = set->member;
19409 }
19410 }
19411 /* If I have found a tangle resolve it */
19412 if (tangle) {
19413 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019414 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019415 post_copy = resolve_tangle(state, tangle);
19416 if (post_copy) {
19417 replace_block_use(state, blocks, tangle, post_copy);
19418 }
19419 if (post_copy && (tangle != ins)) {
19420 replace_set_use(state, live, tangle, post_copy);
19421 }
19422 }
19423 } while(tangle);
19424 return;
19425}
19426
Eric Biederman153ea352003-06-20 14:43:20 +000019427static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019428 struct compile_state *state, struct reg_block *blocks)
19429{
Eric Biederman153ea352003-06-20 14:43:20 +000019430 int tangles;
19431 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019432 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019433 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019434 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019435 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019436}
19437
Eric Biedermand1ea5392003-06-28 06:49:45 +000019438
19439static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19440static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19441
19442struct triple *find_constrained_def(
19443 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019444{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019445 struct live_range_def *lrd, *lrd_next;
19446 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019447 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019448 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019449 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019450
19451 lrd = lrd_next;
19452 lrd_next = lrd->next;
19453
Eric Biedermand1ea5392003-06-28 06:49:45 +000019454 regcm = arch_type_to_regcm(state, lrd->def->type);
19455 info = find_lhs_color(state, lrd->def, 0);
19456 regcm = arch_regcm_reg_normalize(state, regcm);
19457 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019458 /* If the 2 register class masks are equal then
19459 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019460 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019461 if (regcm == info.regcm) {
19462 continue;
19463 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019464
Eric Biederman5ade04a2003-10-22 04:03:46 +000019465 /* If there is just one use.
19466 * That use cannot accept a larger register class.
19467 * There are no intervening definitions except
19468 * definitions that feed into that use.
19469 * Then a triple is not constrained.
19470 * FIXME handle this case!
19471 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019472#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019473#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019474#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019475
Eric Biederman5ade04a2003-10-22 04:03:46 +000019476
Eric Biedermand1ea5392003-06-28 06:49:45 +000019477 /* Of the constrained live ranges deal with the
19478 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019479 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019480 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019481 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019482 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019483 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019484 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019485 tdominates(state, lrd->def, constrained))
19486 {
19487 constrained = lrd->def;
19488 }
19489 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019490 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019491}
19492
Eric Biedermand1ea5392003-06-28 06:49:45 +000019493static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019494 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019495 struct live_range *range)
19496{
19497 /* Walk through the edges in conflict and our current live
19498 * range, and find definitions that are more severly constrained
19499 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019500 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019501 * Then pick one of those ranges and relax the constraints.
19502 */
19503 struct live_range_edge *edge;
19504 struct triple *constrained;
19505
19506 constrained = 0;
19507 for(edge = range->edges; edge; edge = edge->next) {
19508 constrained = find_constrained_def(state, edge->node, constrained);
19509 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019510#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019511#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019512#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019513 if (!constrained) {
19514 constrained = find_constrained_def(state, range, constrained);
19515 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019516
19517 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019518 fprintf(state->errout, "constrained: ");
19519 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019520 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019521 if (constrained) {
19522 ids_from_rstate(state, rstate);
19523 cleanup_rstate(state, rstate);
19524 resolve_tangle(state, constrained);
19525 }
19526 return !!constrained;
19527}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019528
Eric Biedermand1ea5392003-06-28 06:49:45 +000019529static int split_ranges(
19530 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019531 char *used, struct live_range *range)
19532{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019533 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019534 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019535 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019536 rstate->passes, tops(range->defs->def->op), range->defs->def);
19537 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019538 if ((range->color == REG_UNNEEDED) ||
19539 (rstate->passes >= rstate->max_passes)) {
19540 return 0;
19541 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019542 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019543
Eric Biedermand1ea5392003-06-28 06:49:45 +000019544 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019545 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019546 * (a) allow me to spill a register or
19547 * (b) allow me to place a value in another register.
19548 *
19549 * So far I don't have a test case for this, the resolving
19550 * of mandatory constraints has solved all of my
19551 * know issues. So I have choosen not to write any
19552 * code until I cat get a better feel for cases where
19553 * it would be useful to have.
19554 *
19555 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019556#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019557#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019558#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019559
Eric Biederman5ade04a2003-10-22 04:03:46 +000019560 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019561 FILE *fp = state->errout;
19562 print_interference_blocks(state, rstate, fp, 0);
19563 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019564 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019565 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019566}
19567
Eric Biederman5ade04a2003-10-22 04:03:46 +000019568static FILE *cgdebug_fp(struct compile_state *state)
19569{
19570 FILE *fp;
19571 fp = 0;
19572 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019573 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019574 }
19575 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019576 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019577 }
19578 return fp;
19579}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019580
Eric Biederman5ade04a2003-10-22 04:03:46 +000019581static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19582{
19583 FILE *fp;
19584 fp = cgdebug_fp(state);
19585 if (fp) {
19586 va_list args;
19587 va_start(args, fmt);
19588 vfprintf(fp, fmt, args);
19589 va_end(args);
19590 }
19591}
19592
19593static void cgdebug_flush(struct compile_state *state)
19594{
19595 FILE *fp;
19596 fp = cgdebug_fp(state);
19597 if (fp) {
19598 fflush(fp);
19599 }
19600}
19601
19602static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19603{
19604 FILE *fp;
19605 fp = cgdebug_fp(state);
19606 if (fp) {
19607 loc(fp, state, ins);
19608 }
19609}
19610
Stefan Reinauer14e22772010-04-27 06:56:47 +000019611static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019612 struct reg_state *rstate, struct live_range *range)
19613{
19614 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019615 struct live_range_def *lrd;
19616 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019617 struct live_range_edge *edge;
19618 char used[MAX_REGISTERS];
19619 struct triple **expr;
19620
Eric Biedermanb138ac82003-04-22 18:44:01 +000019621 /* Instead of doing just the trivial color select here I try
19622 * a few extra things because a good color selection will help reduce
19623 * copies.
19624 */
19625
19626 /* Find the registers currently in use */
19627 memset(used, 0, sizeof(used));
19628 for(edge = range->edges; edge; edge = edge->next) {
19629 if (edge->node->color == REG_UNSET) {
19630 continue;
19631 }
19632 reg_fill_used(state, used, edge->node->color);
19633 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019634
19635 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019636 int i;
19637 i = 0;
19638 for(edge = range->edges; edge; edge = edge->next) {
19639 i++;
19640 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019641 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019642 tops(range->defs->def->op), i);
19643 cgdebug_loc(state, range->defs->def);
19644 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019645 for(i = 0; i < MAX_REGISTERS; i++) {
19646 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019647 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019648 arch_reg_str(i));
19649 }
19650 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019651 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019652
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019653 /* If a color is already assigned see if it will work */
19654 if (range->color != REG_UNSET) {
19655 struct live_range_def *lrd;
19656 if (!used[range->color]) {
19657 return 1;
19658 }
19659 for(edge = range->edges; edge; edge = edge->next) {
19660 if (edge->node->color != range->color) {
19661 continue;
19662 }
19663 warning(state, edge->node->defs->def, "edge: ");
19664 lrd = edge->node->defs;
19665 do {
19666 warning(state, lrd->def, " %p %s",
19667 lrd->def, tops(lrd->def->op));
19668 lrd = lrd->next;
19669 } while(lrd != edge->node->defs);
19670 }
19671 lrd = range->defs;
19672 warning(state, range->defs->def, "def: ");
19673 do {
19674 warning(state, lrd->def, " %p %s",
19675 lrd->def, tops(lrd->def->op));
19676 lrd = lrd->next;
19677 } while(lrd != range->defs);
19678 internal_error(state, range->defs->def,
19679 "live range with already used color %s",
19680 arch_reg_str(range->color));
19681 }
19682
Eric Biedermanb138ac82003-04-22 18:44:01 +000019683 /* If I feed into an expression reuse it's color.
19684 * This should help remove copies in the case of 2 register instructions
19685 * and phi functions.
19686 */
19687 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019688 lrd = live_range_end(state, range, 0);
19689 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19690 entry = lrd->def->use;
19691 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19692 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019693 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019694 insd = &rstate->lrd[entry->member->id];
19695 if (insd->lr->defs == 0) {
19696 continue;
19697 }
19698 if (!phi && (insd->def->op == OP_PHI) &&
19699 !interfere(rstate, range, insd->lr)) {
19700 phi = insd;
19701 }
Eric Biederman530b5192003-07-01 10:05:30 +000019702 if (insd->lr->color == REG_UNSET) {
19703 continue;
19704 }
19705 regcm = insd->lr->classes;
19706 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019707 (used[insd->lr->color])) {
19708 continue;
19709 }
19710 if (interfere(rstate, range, insd->lr)) {
19711 continue;
19712 }
19713 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019714 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019715 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019716 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019717 * of something else that feeds into the phi function.
19718 */
19719 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019720 if (phi->lr->color != REG_UNSET) {
19721 if (used[phi->lr->color]) {
19722 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019723 }
19724 }
19725 else {
19726 expr = triple_rhs(state, phi->def, 0);
19727 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19728 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019729 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019730 if (!*expr) {
19731 continue;
19732 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019733 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019734 if (lr->color == REG_UNSET) {
19735 continue;
19736 }
19737 regcm = lr->classes;
19738 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019739 (used[lr->color])) {
19740 continue;
19741 }
19742 if (interfere(rstate, range, lr)) {
19743 continue;
19744 }
19745 range->color = lr->color;
19746 }
19747 }
19748 }
19749 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019750 lrd = live_range_head(state, range, 0);
19751 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19752 expr = triple_rhs(state, lrd->def, 0);
19753 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019754 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019755 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019756 if (!*expr) {
19757 continue;
19758 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019759 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019760 if (lr->color == REG_UNSET) {
19761 continue;
19762 }
19763 regcm = lr->classes;
19764 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019765 (used[lr->color])) {
19766 continue;
19767 }
19768 if (interfere(rstate, range, lr)) {
19769 continue;
19770 }
19771 range->color = lr->color;
19772 break;
19773 }
19774 }
19775 /* If I have not opportunitically picked a useful color
19776 * pick the first color that is free.
19777 */
19778 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019779 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019780 arch_select_free_register(state, used, range->classes);
19781 }
19782 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019783 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019784 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019785 if (split_ranges(state, rstate, used, range)) {
19786 return 0;
19787 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019788 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019789 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019790 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019791 lrd = edge->node->defs;
19792 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019793 warning(state, lrd->def, " %s %p",
19794 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019795 lrd = lrd->next;
19796 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019797 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019798 warning(state, range->defs->def, "range: ");
19799 lrd = range->defs;
19800 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019801 warning(state, lrd->def, " %s %p",
19802 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019803 lrd = lrd->next;
19804 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019805
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019806 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019807 range->classes);
19808 for(i = 0; i < MAX_REGISTERS; i++) {
19809 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019810 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019811 arch_reg_str(i));
19812 }
19813 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019814 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019815 }
Eric Biederman530b5192003-07-01 10:05:30 +000019816 range->classes &= arch_reg_regcm(state, range->color);
19817 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019818 internal_error(state, range->defs->def, "select_free_color did not?");
19819 }
19820 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019821}
19822
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019823static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019824{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019825 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019826 struct live_range_edge *edge;
19827 struct live_range *range;
19828 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019829 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019830 range = rstate->low;
19831 if (*range->group_prev != range) {
19832 internal_error(state, 0, "lo: *prev != range?");
19833 }
19834 *range->group_prev = range->group_next;
19835 if (range->group_next) {
19836 range->group_next->group_prev = range->group_prev;
19837 }
19838 if (&range->group_next == rstate->low_tail) {
19839 rstate->low_tail = range->group_prev;
19840 }
19841 if (rstate->low == range) {
19842 internal_error(state, 0, "low: next != prev?");
19843 }
19844 }
19845 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019846 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019847 range = rstate->high;
19848 if (*range->group_prev != range) {
19849 internal_error(state, 0, "hi: *prev != range?");
19850 }
19851 *range->group_prev = range->group_next;
19852 if (range->group_next) {
19853 range->group_next->group_prev = range->group_prev;
19854 }
19855 if (&range->group_next == rstate->high_tail) {
19856 rstate->high_tail = range->group_prev;
19857 }
19858 if (rstate->high == range) {
19859 internal_error(state, 0, "high: next != prev?");
19860 }
19861 }
19862 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019863 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019864 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019865 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019866 range->group_prev = 0;
19867 for(edge = range->edges; edge; edge = edge->next) {
19868 struct live_range *node;
19869 node = edge->node;
19870 /* Move nodes from the high to the low list */
19871 if (node->group_prev && (node->color == REG_UNSET) &&
19872 (node->degree == regc_max_size(state, node->classes))) {
19873 if (*node->group_prev != node) {
19874 internal_error(state, 0, "move: *prev != node?");
19875 }
19876 *node->group_prev = node->group_next;
19877 if (node->group_next) {
19878 node->group_next->group_prev = node->group_prev;
19879 }
19880 if (&node->group_next == rstate->high_tail) {
19881 rstate->high_tail = node->group_prev;
19882 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019883 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019884 node->group_prev = rstate->low_tail;
19885 node->group_next = 0;
19886 *rstate->low_tail = node;
19887 rstate->low_tail = &node->group_next;
19888 if (*node->group_prev != node) {
19889 internal_error(state, 0, "move2: *prev != node?");
19890 }
19891 }
19892 node->degree -= 1;
19893 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019894 colored = color_graph(state, rstate);
19895 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019896 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019897 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019898 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019899 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019900 if (colored) {
19901 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19902 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019903 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019904 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019905}
19906
Eric Biedermana96d6a92003-05-13 20:45:19 +000019907static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19908{
19909 struct live_range *lr;
19910 struct live_range_edge *edge;
19911 struct triple *ins, *first;
19912 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019913 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019914 ins = first;
19915 do {
19916 if (triple_is_def(state, ins)) {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019917 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019918 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019919 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019920 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019921 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019922 if (lr->color == REG_UNSET) {
19923 internal_error(state, ins,
19924 "triple without a color");
19925 }
19926 /* Find the registers used by the edges */
19927 memset(used, 0, sizeof(used));
19928 for(edge = lr->edges; edge; edge = edge->next) {
19929 if (edge->node->color == REG_UNSET) {
19930 internal_error(state, 0,
19931 "live range without a color");
19932 }
19933 reg_fill_used(state, used, edge->node->color);
19934 }
19935 if (used[lr->color]) {
19936 internal_error(state, ins,
19937 "triple with already used color");
19938 }
19939 }
19940 ins = ins->next;
19941 } while(ins != first);
19942}
19943
Eric Biedermanb138ac82003-04-22 18:44:01 +000019944static void color_triples(struct compile_state *state, struct reg_state *rstate)
19945{
Eric Biederman90089602004-05-28 14:11:54 +000019946 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019947 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019948 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019949 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019950 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019951 do {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019952 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019953 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019954 "triple without a live range");
19955 }
Eric Biederman90089602004-05-28 14:11:54 +000019956 lrd = &rstate->lrd[ins->id];
19957 lr = lrd->lr;
19958 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019959 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019960 ins = ins->next;
19961 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019962}
19963
Eric Biedermanb138ac82003-04-22 18:44:01 +000019964static struct live_range *merge_sort_lr(
19965 struct live_range *first, struct live_range *last)
19966{
19967 struct live_range *mid, *join, **join_tail, *pick;
19968 size_t size;
19969 size = (last - first) + 1;
19970 if (size >= 2) {
19971 mid = first + size/2;
19972 first = merge_sort_lr(first, mid -1);
19973 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019974
Eric Biedermanb138ac82003-04-22 18:44:01 +000019975 join = 0;
19976 join_tail = &join;
19977 /* merge the two lists */
19978 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019979 if ((first->degree < mid->degree) ||
19980 ((first->degree == mid->degree) &&
19981 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019982 pick = first;
19983 first = first->group_next;
19984 if (first) {
19985 first->group_prev = 0;
19986 }
19987 }
19988 else {
19989 pick = mid;
19990 mid = mid->group_next;
19991 if (mid) {
19992 mid->group_prev = 0;
19993 }
19994 }
19995 pick->group_next = 0;
19996 pick->group_prev = join_tail;
19997 *join_tail = pick;
19998 join_tail = &pick->group_next;
19999 }
20000 /* Splice the remaining list */
20001 pick = (first)? first : mid;
20002 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020003 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020004 pick->group_prev = join_tail;
20005 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020006 }
20007 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020008 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020009 first = 0;
20010 }
20011 join = first;
20012 }
20013 return join;
20014}
20015
Stefan Reinauer14e22772010-04-27 06:56:47 +000020016static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020017 struct reg_state *rstate)
20018{
20019 struct triple *ins, *first;
20020 if (!rstate->defs) {
20021 return;
20022 }
20023 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020024 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020025 FILE *fp = state->dbgout;
20026 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020027 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020028 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020029 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020030 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020031 ins = first;
20032 do {
20033 if (ins->id) {
20034 struct live_range_def *lrd;
20035 lrd = &rstate->lrd[ins->id];
20036 ins->id = lrd->orig_id;
20037 }
20038 ins = ins->next;
20039 } while(ins != first);
20040}
20041
20042static void cleanup_live_edges(struct reg_state *rstate)
20043{
20044 int i;
20045 /* Free the edges on each node */
20046 for(i = 1; i <= rstate->ranges; i++) {
20047 remove_live_edges(rstate, &rstate->lr[i]);
20048 }
20049}
20050
20051static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20052{
20053 cleanup_live_edges(rstate);
20054 xfree(rstate->lrd);
20055 xfree(rstate->lr);
20056
20057 /* Free the variable lifetime information */
20058 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020059 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020060 }
20061 rstate->defs = 0;
20062 rstate->ranges = 0;
20063 rstate->lrd = 0;
20064 rstate->lr = 0;
20065 rstate->blocks = 0;
20066}
20067
Eric Biederman153ea352003-06-20 14:43:20 +000020068static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020069static void allocate_registers(struct compile_state *state)
20070{
20071 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020072 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020073
20074 /* Clear out the reg_state */
20075 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020076 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020077
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020078 do {
20079 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020080 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020081 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020082
Eric Biederman5ade04a2003-10-22 04:03:46 +000020083 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020084 FILE *fp = state->errout;
20085 fprintf(fp, "pass: %d\n", rstate.passes);
20086 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020087 }
Eric Biederman153ea352003-06-20 14:43:20 +000020088
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020089 /* Restore ids */
20090 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020091
Eric Biedermanf96a8102003-06-16 16:57:34 +000020092 /* Cleanup the temporary data structures */
20093 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020094
Eric Biedermanf96a8102003-06-16 16:57:34 +000020095 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020096 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020097
Eric Biedermanf96a8102003-06-16 16:57:34 +000020098 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020099 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020100
Eric Biederman153ea352003-06-20 14:43:20 +000020101 /* Fix two simultaneous uses of the same register.
20102 * In a few pathlogical cases a partial untangle moves
20103 * the tangle to a part of the graph we won't revisit.
20104 * So we keep looping until we have no more tangle fixes
20105 * to apply.
20106 */
20107 do {
20108 tangles = correct_tangles(state, rstate.blocks);
20109 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020110
Stefan Reinauer14e22772010-04-27 06:56:47 +000020111
Eric Biederman90089602004-05-28 14:11:54 +000020112 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020113 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020114
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020115 /* Allocate and initialize the live ranges */
20116 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020117
Stefan Reinauer14e22772010-04-27 06:56:47 +000020118 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020119 * buys me nothing. The code is left this way in case
20120 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020121 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020122 */
20123 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020124 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020125 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020126 }
20127
Eric Biederman153ea352003-06-20 14:43:20 +000020128 /* Remove any previous live edge calculations */
20129 cleanup_live_edges(&rstate);
20130
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020131 /* Compute the interference graph */
20132 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020133 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020134 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020135
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020136 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020137 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020138 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020139 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020140 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020141 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020142 print_interference_ins, &rstate);
20143 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020144
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020145 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020146
Eric Biederman5ade04a2003-10-22 04:03:46 +000020147 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020148 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020149 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020150 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020151
Eric Biederman83b991a2003-10-11 06:20:25 +000020152#if DEBUG_CONSISTENCY > 1
20153# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020154 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020155# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020156 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020157 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020158 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020159 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020160# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020161 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020162#endif
20163#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020164
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020165 /* Build the groups low and high. But with the nodes
20166 * first sorted by degree order.
20167 */
20168 rstate.low_tail = &rstate.low;
20169 rstate.high_tail = &rstate.high;
20170 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20171 if (rstate.high) {
20172 rstate.high->group_prev = &rstate.high;
20173 }
20174 for(point = &rstate.high; *point; point = &(*point)->group_next)
20175 ;
20176 rstate.high_tail = point;
20177 /* Walk through the high list and move everything that needs
20178 * to be onto low.
20179 */
20180 for(point = &rstate.high; *point; point = next) {
20181 struct live_range *range;
20182 next = &(*point)->group_next;
20183 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020184
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020185 /* If it has a low degree or it already has a color
20186 * place the node in low.
20187 */
20188 if ((range->degree < regc_max_size(state, range->classes)) ||
20189 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020190 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020191 range - rstate.lr, range->degree,
20192 (range->color != REG_UNSET) ? " (colored)": "");
20193 *range->group_prev = range->group_next;
20194 if (range->group_next) {
20195 range->group_next->group_prev = range->group_prev;
20196 }
20197 if (&range->group_next == rstate.high_tail) {
20198 rstate.high_tail = range->group_prev;
20199 }
20200 range->group_prev = rstate.low_tail;
20201 range->group_next = 0;
20202 *rstate.low_tail = range;
20203 rstate.low_tail = &range->group_next;
20204 next = point;
20205 }
20206 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020207 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020208 range - rstate.lr, range->degree,
20209 (range->color != REG_UNSET) ? " (colored)": "");
20210 }
20211 }
20212 /* Color the live_ranges */
20213 colored = color_graph(state, &rstate);
20214 rstate.passes++;
20215 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020216
Eric Biedermana96d6a92003-05-13 20:45:19 +000020217 /* Verify the graph was properly colored */
20218 verify_colors(state, &rstate);
20219
Eric Biedermanb138ac82003-04-22 18:44:01 +000020220 /* Move the colors from the graph to the triples */
20221 color_triples(state, &rstate);
20222
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020223 /* Cleanup the temporary data structures */
20224 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020225
20226 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020227 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020228}
20229
20230/* Sparce Conditional Constant Propogation
20231 * =========================================
20232 */
20233struct ssa_edge;
20234struct flow_block;
20235struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020236 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020237 struct triple *def;
20238 struct ssa_edge *out;
20239 struct flow_block *fblock;
20240 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020241 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020242 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020243 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020244 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020245};
20246struct ssa_edge {
20247 struct lattice_node *src;
20248 struct lattice_node *dst;
20249 struct ssa_edge *work_next;
20250 struct ssa_edge *work_prev;
20251 struct ssa_edge *out_next;
20252};
20253struct flow_edge {
20254 struct flow_block *src;
20255 struct flow_block *dst;
20256 struct flow_edge *work_next;
20257 struct flow_edge *work_prev;
20258 struct flow_edge *in_next;
20259 struct flow_edge *out_next;
20260 int executable;
20261};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020262#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020263struct flow_block {
20264 struct block *block;
20265 struct flow_edge *in;
20266 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020267 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020268};
20269
20270struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020271 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020272 struct lattice_node *lattice;
20273 struct ssa_edge *ssa_edges;
20274 struct flow_block *flow_blocks;
20275 struct flow_edge *flow_work_list;
20276 struct ssa_edge *ssa_work_list;
20277};
20278
20279
Eric Biederman90089602004-05-28 14:11:54 +000020280static int is_scc_const(struct compile_state *state, struct triple *ins)
20281{
20282 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20283}
20284
20285static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20286{
20287 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20288}
20289
20290static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20291{
20292 return is_scc_const(state, lnode->val);
20293}
20294
20295static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20296{
20297 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20298}
20299
Stefan Reinauer14e22772010-04-27 06:56:47 +000020300static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020301 struct flow_edge *fedge)
20302{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020303 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020304 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020305 fedge,
20306 fedge->src->block?fedge->src->block->last->id: 0,
20307 fedge->dst->block?fedge->dst->block->first->id: 0);
20308 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020309 if ((fedge == scc->flow_work_list) ||
20310 (fedge->work_next != fedge) ||
20311 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020312
20313 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020314 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020315 fedge);
20316 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020317 return;
20318 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020319 if (!scc->flow_work_list) {
20320 scc->flow_work_list = fedge;
20321 fedge->work_next = fedge->work_prev = fedge;
20322 }
20323 else {
20324 struct flow_edge *ftail;
20325 ftail = scc->flow_work_list->work_prev;
20326 fedge->work_next = ftail->work_next;
20327 fedge->work_prev = ftail;
20328 fedge->work_next->work_prev = fedge;
20329 fedge->work_prev->work_next = fedge;
20330 }
20331}
20332
20333static struct flow_edge *scc_next_fedge(
20334 struct compile_state *state, struct scc_state *scc)
20335{
20336 struct flow_edge *fedge;
20337 fedge = scc->flow_work_list;
20338 if (fedge) {
20339 fedge->work_next->work_prev = fedge->work_prev;
20340 fedge->work_prev->work_next = fedge->work_next;
20341 if (fedge->work_next != fedge) {
20342 scc->flow_work_list = fedge->work_next;
20343 } else {
20344 scc->flow_work_list = 0;
20345 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020346 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020347 }
20348 return fedge;
20349}
20350
20351static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20352 struct ssa_edge *sedge)
20353{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020354 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020355 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20356 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020357 sedge->src->def->id,
20358 sedge->dst->def->id);
20359 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020360 if ((sedge == scc->ssa_work_list) ||
20361 (sedge->work_next != sedge) ||
20362 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020363
20364 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020365 fprintf(state->errout, "dupped sedge: %5ld\n",
20366 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020367 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020368 return;
20369 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020370 if (!scc->ssa_work_list) {
20371 scc->ssa_work_list = sedge;
20372 sedge->work_next = sedge->work_prev = sedge;
20373 }
20374 else {
20375 struct ssa_edge *stail;
20376 stail = scc->ssa_work_list->work_prev;
20377 sedge->work_next = stail->work_next;
20378 sedge->work_prev = stail;
20379 sedge->work_next->work_prev = sedge;
20380 sedge->work_prev->work_next = sedge;
20381 }
20382}
20383
20384static struct ssa_edge *scc_next_sedge(
20385 struct compile_state *state, struct scc_state *scc)
20386{
20387 struct ssa_edge *sedge;
20388 sedge = scc->ssa_work_list;
20389 if (sedge) {
20390 sedge->work_next->work_prev = sedge->work_prev;
20391 sedge->work_prev->work_next = sedge->work_next;
20392 if (sedge->work_next != sedge) {
20393 scc->ssa_work_list = sedge->work_next;
20394 } else {
20395 scc->ssa_work_list = 0;
20396 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020397 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020398 }
20399 return sedge;
20400}
20401
20402static void initialize_scc_state(
20403 struct compile_state *state, struct scc_state *scc)
20404{
20405 int ins_count, ssa_edge_count;
20406 int ins_index, ssa_edge_index, fblock_index;
20407 struct triple *first, *ins;
20408 struct block *block;
20409 struct flow_block *fblock;
20410
20411 memset(scc, 0, sizeof(*scc));
20412
20413 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020414 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020415 ins = first;
20416 ins_count = ssa_edge_count = 0;
20417 do {
20418 struct triple_set *edge;
20419 ins_count += 1;
20420 for(edge = ins->use; edge; edge = edge->next) {
20421 ssa_edge_count++;
20422 }
20423 ins = ins->next;
20424 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020425 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020426 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20427 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020428 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020429 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020430 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020431 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020432 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020433 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020434 scc->flow_blocks =
20435 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020436 "flow_blocks");
20437
20438 /* Initialize pass one collect up the nodes */
20439 fblock = 0;
20440 block = 0;
20441 ins_index = ssa_edge_index = fblock_index = 0;
20442 ins = first;
20443 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020444 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20445 block = ins->u.block;
20446 if (!block) {
20447 internal_error(state, ins, "label without block");
20448 }
20449 fblock_index += 1;
20450 block->vertex = fblock_index;
20451 fblock = &scc->flow_blocks[fblock_index];
20452 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020453 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20454 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020455 }
20456 {
20457 struct lattice_node *lnode;
20458 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020459 lnode = &scc->lattice[ins_index];
20460 lnode->def = ins;
20461 lnode->out = 0;
20462 lnode->fblock = fblock;
20463 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020464 if (lnode->val->op == OP_UNKNOWNVAL) {
20465 lnode->val = 0; /* LATTICE LOW by definition */
20466 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020467 lnode->old_id = ins->id;
20468 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020469 }
20470 ins = ins->next;
20471 } while(ins != first);
20472 /* Initialize pass two collect up the edges */
20473 block = 0;
20474 fblock = 0;
20475 ins = first;
20476 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020477 {
20478 struct triple_set *edge;
20479 struct ssa_edge **stail;
20480 struct lattice_node *lnode;
20481 lnode = &scc->lattice[ins->id];
20482 lnode->out = 0;
20483 stail = &lnode->out;
20484 for(edge = ins->use; edge; edge = edge->next) {
20485 struct ssa_edge *sedge;
20486 ssa_edge_index += 1;
20487 sedge = &scc->ssa_edges[ssa_edge_index];
20488 *stail = sedge;
20489 stail = &sedge->out_next;
20490 sedge->src = lnode;
20491 sedge->dst = &scc->lattice[edge->member->id];
20492 sedge->work_next = sedge->work_prev = sedge;
20493 sedge->out_next = 0;
20494 }
20495 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020496 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20497 struct flow_edge *fedge, **ftail;
20498 struct block_set *bedge;
20499 block = ins->u.block;
20500 fblock = &scc->flow_blocks[block->vertex];
20501 fblock->in = 0;
20502 fblock->out = 0;
20503 ftail = &fblock->out;
20504
20505 fedge = fblock->edges;
20506 bedge = block->edges;
20507 for(; bedge; bedge = bedge->next, fedge++) {
20508 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20509 if (fedge->dst->block != bedge->member) {
20510 internal_error(state, 0, "block mismatch");
20511 }
20512 *ftail = fedge;
20513 ftail = &fedge->out_next;
20514 fedge->out_next = 0;
20515 }
20516 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20517 fedge->src = fblock;
20518 fedge->work_next = fedge->work_prev = fedge;
20519 fedge->executable = 0;
20520 }
20521 }
20522 ins = ins->next;
20523 } while (ins != first);
20524 block = 0;
20525 fblock = 0;
20526 ins = first;
20527 do {
20528 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20529 struct flow_edge **ftail;
20530 struct block_set *bedge;
20531 block = ins->u.block;
20532 fblock = &scc->flow_blocks[block->vertex];
20533 ftail = &fblock->in;
20534 for(bedge = block->use; bedge; bedge = bedge->next) {
20535 struct block *src_block;
20536 struct flow_block *sfblock;
20537 struct flow_edge *sfedge;
20538 src_block = bedge->member;
20539 sfblock = &scc->flow_blocks[src_block->vertex];
20540 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20541 if (sfedge->dst == fblock) {
20542 break;
20543 }
20544 }
20545 if (!sfedge) {
20546 internal_error(state, 0, "edge mismatch");
20547 }
20548 *ftail = sfedge;
20549 ftail = &sfedge->in_next;
20550 sfedge->in_next = 0;
20551 }
20552 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020553 ins = ins->next;
20554 } while(ins != first);
20555 /* Setup a dummy block 0 as a node above the start node */
20556 {
20557 struct flow_block *fblock, *dst;
20558 struct flow_edge *fedge;
20559 fblock = &scc->flow_blocks[0];
20560 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020561 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020562 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020563 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020564 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020565 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020566 fedge->src = fblock;
20567 fedge->dst = dst;
20568 fedge->work_next = fedge;
20569 fedge->work_prev = fedge;
20570 fedge->in_next = fedge->dst->in;
20571 fedge->out_next = 0;
20572 fedge->executable = 0;
20573 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020574
Eric Biedermanb138ac82003-04-22 18:44:01 +000020575 /* Initialize the work lists */
20576 scc->flow_work_list = 0;
20577 scc->ssa_work_list = 0;
20578 scc_add_fedge(state, scc, fedge);
20579 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020580 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020581 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020582 ins_index, ssa_edge_index, fblock_index);
20583 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020584}
20585
Stefan Reinauer14e22772010-04-27 06:56:47 +000020586
Eric Biedermanb138ac82003-04-22 18:44:01 +000020587static void free_scc_state(
20588 struct compile_state *state, struct scc_state *scc)
20589{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020590 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020591 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020592 struct flow_block *fblock;
20593 fblock = &scc->flow_blocks[i];
20594 if (fblock->edges) {
20595 xfree(fblock->edges);
20596 fblock->edges = 0;
20597 }
20598 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020599 xfree(scc->flow_blocks);
20600 xfree(scc->ssa_edges);
20601 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020602
Eric Biedermanb138ac82003-04-22 18:44:01 +000020603}
20604
20605static struct lattice_node *triple_to_lattice(
20606 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20607{
20608 if (ins->id <= 0) {
20609 internal_error(state, ins, "bad id");
20610 }
20611 return &scc->lattice[ins->id];
20612}
20613
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020614static struct triple *preserve_lval(
20615 struct compile_state *state, struct lattice_node *lnode)
20616{
20617 struct triple *old;
20618 /* Preserve the original value */
20619 if (lnode->val) {
20620 old = dup_triple(state, lnode->val);
20621 if (lnode->val != lnode->def) {
20622 xfree(lnode->val);
20623 }
20624 lnode->val = 0;
20625 } else {
20626 old = 0;
20627 }
20628 return old;
20629}
20630
Stefan Reinauer14e22772010-04-27 06:56:47 +000020631static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020632 struct triple *old, struct lattice_node *lnode)
20633{
20634 int changed;
20635 /* See if the lattice value has changed */
20636 changed = 1;
20637 if (!old && !lnode->val) {
20638 changed = 0;
20639 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020640 if (changed &&
20641 lnode->val && old &&
20642 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020643 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020644 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20645 changed = 0;
20646 }
20647 if (old) {
20648 xfree(old);
20649 }
20650 return changed;
20651
20652}
20653
Eric Biederman5ade04a2003-10-22 04:03:46 +000020654static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020655 struct compile_state *state, struct scc_state *scc,
20656 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020657{
Eric Biederman90089602004-05-28 14:11:54 +000020658 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20659 display_triple_changes(state->errout, lnode->val, lnode->def);
20660 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020661 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020662 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020663 struct triple *val, **expr;
20664 val = lnode->val? lnode->val : lnode->def;
20665 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020666 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020667 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20668 lnode->def->id,
20669 tops(lnode->def->op));
20670 expr = triple_rhs(state, lnode->def, 0);
20671 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20672 if (*expr) {
20673 fprintf(fp, " %d", (*expr)->id);
20674 }
20675 }
20676 if (val->op == OP_INTCONST) {
20677 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20678 }
20679 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020680 (is_lattice_hi(state, lnode)? "hi":
20681 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020682 changed? "changed" : ""
20683 );
20684 }
20685}
20686
Eric Biedermanb138ac82003-04-22 18:44:01 +000020687static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20688 struct lattice_node *lnode)
20689{
20690 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020691 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020692 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020693 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020694
Eric Biedermanb138ac82003-04-22 18:44:01 +000020695 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020696 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020697
Eric Biedermanb138ac82003-04-22 18:44:01 +000020698 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020699 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020700 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020701 scratch->next = scratch;
20702 scratch->prev = scratch;
20703 scratch->use = 0;
20704
Eric Biederman90089602004-05-28 14:11:54 +000020705 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020706 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020707 dexpr = &lnode->def->param[i];
20708 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020709 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020710 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20711 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020712 *dexpr) {
20713 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020714 tmp = triple_to_lattice(state, scc, *dexpr);
20715 *vexpr = (tmp->val)? tmp->val : tmp->def;
20716 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020717 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020718 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020719 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020720 }
20721 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020722#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020723#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020724#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020725 /* So far it looks like only the strength reduction
20726 * optimization are things I need to worry about.
20727 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020728 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020729 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020730 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020731 internal_error(state, lnode->def, "scratch used?");
20732 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020733 if ((scratch->prev != scratch) ||
20734 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020735 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020736 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020737 internal_error(state, lnode->def, "scratch in list?");
20738 }
20739 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020740 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020741 for(i = 0; i < count; i++) {
20742 vexpr = &scratch->param[i];
20743 if (*vexpr) {
20744 unuse_triple(*vexpr, scratch);
20745 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020746 }
Eric Biederman90089602004-05-28 14:11:54 +000020747 if (lnode->val->op == OP_UNKNOWNVAL) {
20748 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020749 }
Eric Biederman90089602004-05-28 14:11:54 +000020750 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020751 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020752 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020753 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020754 count * sizeof(lnode->val->param[0])) == 0) &&
20755 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020756 lnode->val = lnode->def;
20757 }
Eric Biederman90089602004-05-28 14:11:54 +000020758 /* Only allow lattice high when all of my inputs
20759 * are also lattice high. Occassionally I can
20760 * have constants with a lattice low input, so
20761 * I do not need to check that case.
20762 */
20763 if (is_lattice_hi(state, lnode)) {
20764 struct lattice_node *tmp;
20765 int rhs;
20766 rhs = lnode->val->rhs;
20767 for(i = 0; i < rhs; i++) {
20768 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20769 if (!is_lattice_hi(state, tmp)) {
20770 lnode->val = 0;
20771 break;
20772 }
20773 }
20774 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020775 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020776 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020777 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020778 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020779 lnode->val = 0;
20780 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020781 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020782 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020783 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020784 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020785 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020786 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020787 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020788#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020789#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020790#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020791 if (changed) {
20792 internal_warning(state, lnode->def, "non def changes value?");
20793 }
20794 lnode->val = 0;
20795 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020796
Eric Biederman83b991a2003-10-11 06:20:25 +000020797 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020798 if (lnode->val != scratch) {
20799 xfree(scratch);
20800 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020801
Eric Biedermanb138ac82003-04-22 18:44:01 +000020802 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020803}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020804
Eric Biederman90089602004-05-28 14:11:54 +000020805
20806static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020807 struct lattice_node *lnode)
20808{
20809 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020810 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020811 int changed;
20812
20813 /* Update the branch value */
20814 changed = compute_lnode_val(state, scc, lnode);
20815 scc_debug_lnode(state, scc, lnode, changed);
20816
20817 /* This only applies to conditional branches */
20818 if (!triple_is_cbranch(state, lnode->def)) {
20819 internal_error(state, lnode->def, "not a conditional branch");
20820 }
20821
Eric Biederman5ade04a2003-10-22 04:03:46 +000020822 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020823 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020824 FILE *fp = state->errout;
20825 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020826 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020827 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020828
Eric Biedermanb138ac82003-04-22 18:44:01 +000020829 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020830 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020831 }
Eric Biederman90089602004-05-28 14:11:54 +000020832 fprintf(fp, " )");
20833 if (lnode->def->rhs > 0) {
20834 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020835 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020836 }
Eric Biederman90089602004-05-28 14:11:54 +000020837 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020838 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020839 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020840 for(left = cond->fblock->out; left; left = left->out_next) {
20841 if (left->dst->block->first == lnode->def->next) {
20842 break;
20843 }
20844 }
20845 if (!left) {
20846 internal_error(state, lnode->def, "Cannot find left branch edge");
20847 }
20848 for(right = cond->fblock->out; right; right = right->out_next) {
20849 if (right->dst->block->first == TARG(lnode->def, 0)) {
20850 break;
20851 }
20852 }
20853 if (!right) {
20854 internal_error(state, lnode->def, "Cannot find right branch edge");
20855 }
Eric Biederman90089602004-05-28 14:11:54 +000020856 /* I should only come here if the controlling expressions value
20857 * has changed, which means it must be either a constant or lo.
20858 */
20859 if (is_lattice_hi(state, cond)) {
20860 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020861 return;
20862 }
Eric Biederman90089602004-05-28 14:11:54 +000020863 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020864 scc_add_fedge(state, scc, left);
20865 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020866 }
20867 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020868 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020869 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020870 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020871 }
20872
20873}
20874
Eric Biederman90089602004-05-28 14:11:54 +000020875
Stefan Reinauer14e22772010-04-27 06:56:47 +000020876static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020877 struct scc_state *scc, struct ssa_edge *sedge)
20878{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020879 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020880 scc_visit_cbranch(state, scc, sedge->dst);
20881 }
20882 else if (triple_is_def(state, sedge->dst->def)) {
20883 scc_add_sedge(state, scc, sedge);
20884 }
20885}
20886
Stefan Reinauer14e22772010-04-27 06:56:47 +000020887static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020888 struct lattice_node *lnode)
20889{
20890 struct lattice_node *tmp;
20891 struct triple **slot, *old;
20892 struct flow_edge *fedge;
20893 int changed;
20894 int index;
20895 if (lnode->def->op != OP_PHI) {
20896 internal_error(state, lnode->def, "not phi");
20897 }
20898 /* Store the original value */
20899 old = preserve_lval(state, lnode);
20900
20901 /* default to lattice high */
20902 lnode->val = lnode->def;
20903 slot = &RHS(lnode->def, 0);
20904 index = 0;
20905 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20906 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020907 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020908 index,
20909 fedge->dst->block->vertex,
20910 fedge->executable
20911 );
20912 }
20913 if (!fedge->executable) {
20914 continue;
20915 }
20916 if (!slot[index]) {
20917 internal_error(state, lnode->def, "no phi value");
20918 }
20919 tmp = triple_to_lattice(state, scc, slot[index]);
20920 /* meet(X, lattice low) = lattice low */
20921 if (is_lattice_lo(state, tmp)) {
20922 lnode->val = 0;
20923 }
20924 /* meet(X, lattice high) = X */
20925 else if (is_lattice_hi(state, tmp)) {
Eric Biederman90089602004-05-28 14:11:54 +000020926 }
20927 /* meet(lattice high, X) = X */
20928 else if (is_lattice_hi(state, lnode)) {
20929 lnode->val = dup_triple(state, tmp->val);
20930 /* Only change the type if necessary */
20931 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20932 lnode->val->type = lnode->def->type;
20933 }
20934 }
20935 /* meet(const, const) = const or lattice low */
20936 else if (!constants_equal(state, lnode->val, tmp->val)) {
20937 lnode->val = 0;
20938 }
20939
20940 /* meet(lattice low, X) = lattice low */
20941 if (is_lattice_lo(state, lnode)) {
20942 lnode->val = 0;
20943 break;
20944 }
20945 }
20946 changed = lval_changed(state, old, lnode);
20947 scc_debug_lnode(state, scc, lnode, changed);
20948
20949 /* If the lattice value has changed update the work lists. */
20950 if (changed) {
20951 struct ssa_edge *sedge;
20952 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20953 scc_add_sedge_dst(state, scc, sedge);
20954 }
20955 }
20956}
20957
20958
Eric Biedermanb138ac82003-04-22 18:44:01 +000020959static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20960 struct lattice_node *lnode)
20961{
20962 int changed;
20963
Eric Biederman90089602004-05-28 14:11:54 +000020964 if (!triple_is_def(state, lnode->def)) {
20965 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020966 }
Eric Biederman90089602004-05-28 14:11:54 +000020967 changed = compute_lnode_val(state, scc, lnode);
20968 scc_debug_lnode(state, scc, lnode, changed);
20969
20970 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020971 struct ssa_edge *sedge;
20972 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020973 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020974 }
20975 }
20976}
20977
20978static void scc_writeback_values(
20979 struct compile_state *state, struct scc_state *scc)
20980{
20981 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020982 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020983 ins = first;
20984 do {
20985 struct lattice_node *lnode;
20986 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020987 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020988 if (is_lattice_hi(state, lnode) &&
20989 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020990 {
20991 struct flow_edge *fedge;
20992 int executable;
20993 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020994 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020995 !executable && fedge; fedge = fedge->in_next) {
20996 executable |= fedge->executable;
20997 }
20998 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020999 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021000 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000021001 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021002 tops(lnode->def->op),
21003 tops(lnode->val->op));
21004 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021005 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021006 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021007
Eric Biederman83b991a2003-10-11 06:20:25 +000021008 /* Restore id */
21009 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021010 if (lnode->val && (lnode->val != ins)) {
21011 /* See if it something I know how to write back */
21012 switch(lnode->val->op) {
21013 case OP_INTCONST:
21014 mkconst(state, ins, lnode->val->u.cval);
21015 break;
21016 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021017 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021018 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021019 break;
21020 default:
21021 /* By default don't copy the changes,
21022 * recompute them in place instead.
21023 */
21024 simplify(state, ins);
21025 break;
21026 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021027 if (is_const(lnode->val) &&
21028 !constants_equal(state, lnode->val, ins)) {
21029 internal_error(state, 0, "constants not equal");
21030 }
21031 /* Free the lattice nodes */
21032 xfree(lnode->val);
21033 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021034 }
21035 ins = ins->next;
21036 } while(ins != first);
21037}
21038
21039static void scc_transform(struct compile_state *state)
21040{
21041 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021042 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21043 return;
21044 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021045
21046 initialize_scc_state(state, &scc);
21047
21048 while(scc.flow_work_list || scc.ssa_work_list) {
21049 struct flow_edge *fedge;
21050 struct ssa_edge *sedge;
21051 struct flow_edge *fptr;
21052 while((fedge = scc_next_fedge(state, &scc))) {
21053 struct block *block;
21054 struct triple *ptr;
21055 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021056 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021057 int done;
21058 if (fedge->executable) {
21059 continue;
21060 }
21061 if (!fedge->dst) {
21062 internal_error(state, 0, "fedge without dst");
21063 }
21064 if (!fedge->src) {
21065 internal_error(state, 0, "fedge without src");
21066 }
21067 fedge->executable = 1;
21068 fblock = fedge->dst;
21069 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021070 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021071 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21072 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021073 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021074 }
21075 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021076
Eric Biederman5ade04a2003-10-22 04:03:46 +000021077 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021078 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021079 block->vertex, reps);
21080 }
21081
Eric Biedermanb138ac82003-04-22 18:44:01 +000021082 done = 0;
21083 for(ptr = block->first; !done; ptr = ptr->next) {
21084 struct lattice_node *lnode;
21085 done = (ptr == block->last);
21086 lnode = &scc.lattice[ptr->id];
21087 if (ptr->op == OP_PHI) {
21088 scc_visit_phi(state, &scc, lnode);
21089 }
Eric Biederman90089602004-05-28 14:11:54 +000021090 else if ((reps == 1) && triple_is_def(state, ptr))
21091 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021092 scc_visit_expr(state, &scc, lnode);
21093 }
21094 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021095 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021096 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021097 struct flow_edge *out;
21098 for(out = fblock->out; out; out = out->out_next) {
21099 scc_add_fedge(state, &scc, out);
21100 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021101 }
21102 }
21103 while((sedge = scc_next_sedge(state, &scc))) {
21104 struct lattice_node *lnode;
21105 struct flow_block *fblock;
21106 lnode = sedge->dst;
21107 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021108
21109 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021110 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021111 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021112 sedge->src->def->id,
21113 sedge->dst->def->id);
21114 }
21115
Eric Biedermanb138ac82003-04-22 18:44:01 +000021116 if (lnode->def->op == OP_PHI) {
21117 scc_visit_phi(state, &scc, lnode);
21118 }
21119 else {
21120 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21121 if (fptr->executable) {
21122 break;
21123 }
21124 }
21125 if (fptr) {
21126 scc_visit_expr(state, &scc, lnode);
21127 }
21128 }
21129 }
21130 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021131
Eric Biedermanb138ac82003-04-22 18:44:01 +000021132 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021133 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021134 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021135
Eric Biederman90089602004-05-28 14:11:54 +000021136 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021137}
21138
21139
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021140static void transform_to_arch_instructions(struct compile_state *state)
21141{
21142 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021143 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021144 ins = first;
21145 do {
21146 ins = transform_to_arch_instruction(state, ins);
21147 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021148
Eric Biederman90089602004-05-28 14:11:54 +000021149 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021150}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021151
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021152#if DEBUG_CONSISTENCY
21153static void verify_uses(struct compile_state *state)
21154{
21155 struct triple *first, *ins;
21156 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021157 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021158 ins = first;
21159 do {
21160 struct triple **expr;
21161 expr = triple_rhs(state, ins, 0);
21162 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021163 struct triple *rhs;
21164 rhs = *expr;
21165 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021166 if (set->member == ins) {
21167 break;
21168 }
21169 }
21170 if (!set) {
21171 internal_error(state, ins, "rhs not used");
21172 }
21173 }
21174 expr = triple_lhs(state, ins, 0);
21175 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021176 struct triple *lhs;
21177 lhs = *expr;
21178 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021179 if (set->member == ins) {
21180 break;
21181 }
21182 }
21183 if (!set) {
21184 internal_error(state, ins, "lhs not used");
21185 }
21186 }
Eric Biederman90089602004-05-28 14:11:54 +000021187 expr = triple_misc(state, ins, 0);
21188 if (ins->op != OP_PHI) {
21189 for(; expr; expr = triple_targ(state, ins, expr)) {
21190 struct triple *misc;
21191 misc = *expr;
21192 for(set = misc?misc->use:0; set; set = set->next) {
21193 if (set->member == ins) {
21194 break;
21195 }
21196 }
21197 if (!set) {
21198 internal_error(state, ins, "misc not used");
21199 }
21200 }
21201 }
21202 if (!triple_is_ret(state, ins)) {
21203 expr = triple_targ(state, ins, 0);
21204 for(; expr; expr = triple_targ(state, ins, expr)) {
21205 struct triple *targ;
21206 targ = *expr;
21207 for(set = targ?targ->use:0; set; set = set->next) {
21208 if (set->member == ins) {
21209 break;
21210 }
21211 }
21212 if (!set) {
21213 internal_error(state, ins, "targ not used");
21214 }
21215 }
21216 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021217 ins = ins->next;
21218 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021219
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021220}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021221static void verify_blocks_present(struct compile_state *state)
21222{
21223 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021224 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021225 return;
21226 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021227 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021228 ins = first;
21229 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021230 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021231 if (triple_stores_block(state, ins)) {
21232 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021233 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021234 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021235 }
21236 }
21237 ins = ins->next;
21238 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021239
21240
Eric Biedermand1ea5392003-06-28 06:49:45 +000021241}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021242
21243static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21244{
21245 struct block_set *bedge;
21246 struct block *targ;
21247 targ = block_of_triple(state, edge);
21248 for(bedge = block->edges; bedge; bedge = bedge->next) {
21249 if (bedge->member == targ) {
21250 return 1;
21251 }
21252 }
21253 return 0;
21254}
21255
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021256static void verify_blocks(struct compile_state *state)
21257{
21258 struct triple *ins;
21259 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021260 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021261 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021262 if (!block) {
21263 return;
21264 }
Eric Biederman530b5192003-07-01 10:05:30 +000021265 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021266 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021267 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021268 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021269 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021270 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021271 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021272 internal_error(state, ins, "inconsitent block specified");
21273 }
Eric Biederman530b5192003-07-01 10:05:30 +000021274 valid_ins(state, ins);
21275 }
21276 users = 0;
21277 for(user = block->use; user; user = user->next) {
21278 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021279 if (!user->member->first) {
21280 internal_error(state, block->first, "user is empty");
21281 }
Eric Biederman90089602004-05-28 14:11:54 +000021282 if ((block == state->bb.last_block) &&
21283 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021284 continue;
21285 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021286 for(edge = user->member->edges; edge; edge = edge->next) {
21287 if (edge->member == block) {
21288 break;
21289 }
21290 }
21291 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021292 internal_error(state, user->member->first,
21293 "user does not use block");
21294 }
21295 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021296 if (triple_is_branch(state, block->last)) {
21297 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021298 expr = triple_edge_targ(state, block->last, 0);
21299 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021300 if (*expr && !edge_present(state, block, *expr)) {
21301 internal_error(state, block->last, "no edge to targ");
21302 }
21303 }
Eric Biederman530b5192003-07-01 10:05:30 +000021304 }
Eric Biederman90089602004-05-28 14:11:54 +000021305 if (!triple_is_ubranch(state, block->last) &&
21306 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021307 !edge_present(state, block, block->last->next)) {
21308 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021309 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021310 for(edge = block->edges; edge; edge = edge->next) {
21311 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021312 if (user->member == block) {
21313 break;
21314 }
21315 }
21316 if (!user || user->member != block) {
21317 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021318 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021319 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021320 if (!edge->member->first) {
21321 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021322 }
Eric Biederman530b5192003-07-01 10:05:30 +000021323 }
21324 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021325 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021326 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021327 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021328 }
21329 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021330 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021331 "cannot find next block");
21332 }
21333 block = block->last->next->u.block;
21334 if (!block) {
21335 internal_error(state, block->last->next,
21336 "bad next block");
21337 }
Eric Biederman90089602004-05-28 14:11:54 +000021338 } while(block != state->bb.first_block);
21339 if (blocks != state->bb.last_vertex) {
21340 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21341 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021342 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021343}
21344
21345static void verify_domination(struct compile_state *state)
21346{
21347 struct triple *first, *ins;
21348 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021349 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021350 return;
21351 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021352
Eric Biederman83b991a2003-10-11 06:20:25 +000021353 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021354 ins = first;
21355 do {
21356 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021357 struct triple **slot;
21358 struct triple *use_point;
21359 int i, zrhs;
21360 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021361 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021362 slot = &RHS(set->member, 0);
21363 /* See if the use is on the right hand side */
21364 for(i = 0; i < zrhs; i++) {
21365 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021366 break;
21367 }
21368 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021369 if (i < zrhs) {
21370 use_point = set->member;
21371 if (set->member->op == OP_PHI) {
21372 struct block_set *bset;
21373 int edge;
21374 bset = set->member->u.block->use;
21375 for(edge = 0; bset && (edge < i); edge++) {
21376 bset = bset->next;
21377 }
21378 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021379 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021380 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021381 }
21382 use_point = bset->member->last;
21383 }
21384 }
21385 if (use_point &&
21386 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021387 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021388 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021389 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021390 }
21391 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021392 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021393 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021394 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021395 }
21396 }
21397 ins = ins->next;
21398 } while(ins != first);
21399}
21400
Eric Biederman83b991a2003-10-11 06:20:25 +000021401static void verify_rhs(struct compile_state *state)
21402{
21403 struct triple *first, *ins;
21404 first = state->first;
21405 ins = first;
21406 do {
21407 struct triple **slot;
21408 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021409 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021410 slot = &RHS(ins, 0);
21411 for(i = 0; i < zrhs; i++) {
21412 if (slot[i] == 0) {
21413 internal_error(state, ins,
21414 "missing rhs %d on %s",
21415 i, tops(ins->op));
21416 }
21417 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21418 internal_error(state, ins,
21419 "ins == rhs[%d] on %s",
21420 i, tops(ins->op));
21421 }
21422 }
21423 ins = ins->next;
21424 } while(ins != first);
21425}
21426
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021427static void verify_piece(struct compile_state *state)
21428{
21429 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021430 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021431 ins = first;
21432 do {
21433 struct triple *ptr;
21434 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021435 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021436 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21437 if (ptr != LHS(ins, i)) {
21438 internal_error(state, ins, "malformed lhs on %s",
21439 tops(ins->op));
21440 }
21441 if (ptr->op != OP_PIECE) {
21442 internal_error(state, ins, "bad lhs op %s at %d on %s",
21443 tops(ptr->op), i, tops(ins->op));
21444 }
21445 if (ptr->u.cval != i) {
21446 internal_error(state, ins, "bad u.cval of %d %d expected",
21447 ptr->u.cval, i);
21448 }
21449 }
21450 ins = ins->next;
21451 } while(ins != first);
21452}
Eric Biederman83b991a2003-10-11 06:20:25 +000021453
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021454static void verify_ins_colors(struct compile_state *state)
21455{
21456 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021457
Eric Biederman83b991a2003-10-11 06:20:25 +000021458 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021459 ins = first;
21460 do {
21461 ins = ins->next;
21462 } while(ins != first);
21463}
Eric Biederman90089602004-05-28 14:11:54 +000021464
21465static void verify_unknown(struct compile_state *state)
21466{
21467 struct triple *first, *ins;
21468 if ( (unknown_triple.next != &unknown_triple) ||
21469 (unknown_triple.prev != &unknown_triple) ||
21470#if 0
21471 (unknown_triple.use != 0) ||
21472#endif
21473 (unknown_triple.op != OP_UNKNOWNVAL) ||
21474 (unknown_triple.lhs != 0) ||
21475 (unknown_triple.rhs != 0) ||
21476 (unknown_triple.misc != 0) ||
21477 (unknown_triple.targ != 0) ||
21478 (unknown_triple.template_id != 0) ||
21479 (unknown_triple.id != -1) ||
21480 (unknown_triple.type != &unknown_type) ||
21481 (unknown_triple.occurance != &dummy_occurance) ||
21482 (unknown_triple.param[0] != 0) ||
21483 (unknown_triple.param[1] != 0)) {
21484 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21485 }
21486 if ( (dummy_occurance.count != 2) ||
21487 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21488 (strcmp(dummy_occurance.function, "") != 0) ||
21489 (dummy_occurance.col != 0) ||
21490 (dummy_occurance.parent != 0)) {
21491 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21492 }
21493 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21494 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21495 }
21496 first = state->first;
21497 ins = first;
21498 do {
21499 int params, i;
21500 if (ins == &unknown_triple) {
21501 internal_error(state, ins, "unknown triple in list");
21502 }
21503 params = TRIPLE_SIZE(ins);
21504 for(i = 0; i < params; i++) {
21505 if (ins->param[i] == &unknown_triple) {
21506 internal_error(state, ins, "unknown triple used!");
21507 }
21508 }
21509 ins = ins->next;
21510 } while(ins != first);
21511}
21512
21513static void verify_types(struct compile_state *state)
21514{
21515 struct triple *first, *ins;
21516 first = state->first;
21517 ins = first;
21518 do {
21519 struct type *invalid;
21520 invalid = invalid_type(state, ins->type);
21521 if (invalid) {
21522 FILE *fp = state->errout;
21523 fprintf(fp, "type: ");
21524 name_of(fp, ins->type);
21525 fprintf(fp, "\n");
21526 fprintf(fp, "invalid type: ");
21527 name_of(fp, invalid);
21528 fprintf(fp, "\n");
21529 internal_error(state, ins, "invalid ins type");
21530 }
21531 } while(ins != first);
21532}
21533
21534static void verify_copy(struct compile_state *state)
21535{
21536 struct triple *first, *ins, *next;
21537 first = state->first;
21538 next = ins = first;
21539 do {
21540 ins = next;
21541 next = ins->next;
21542 if (ins->op != OP_COPY) {
21543 continue;
21544 }
21545 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21546 FILE *fp = state->errout;
21547 fprintf(fp, "src type: ");
21548 name_of(fp, RHS(ins, 0)->type);
21549 fprintf(fp, "\n");
21550 fprintf(fp, "dst type: ");
21551 name_of(fp, ins->type);
21552 fprintf(fp, "\n");
21553 internal_error(state, ins, "type mismatch in copy");
21554 }
21555 } while(next != first);
21556}
21557
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021558static void verify_consistency(struct compile_state *state)
21559{
Eric Biederman90089602004-05-28 14:11:54 +000021560 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021561 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021562 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021563 verify_blocks(state);
21564 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021565 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021566 verify_piece(state);
21567 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021568 verify_types(state);
21569 verify_copy(state);
21570 if (state->compiler->debug & DEBUG_VERIFICATION) {
21571 fprintf(state->dbgout, "consistency verified\n");
21572 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021573}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021574#else
Eric Biederman153ea352003-06-20 14:43:20 +000021575static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021576#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021577
21578static void optimize(struct compile_state *state)
21579{
Eric Biederman90089602004-05-28 14:11:54 +000021580 /* Join all of the functions into one giant function */
21581 join_functions(state);
21582
Eric Biederman5ade04a2003-10-22 04:03:46 +000021583 /* Dump what the instruction graph intially looks like */
21584 print_triples(state);
21585
Eric Biederman0babc1c2003-05-09 02:39:00 +000021586 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021587 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021588 print_triples(state);
21589
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021590 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021591 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021592 state->bb.first = state->first;
21593 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021594
Eric Biederman530b5192003-07-01 10:05:30 +000021595 /* Transform the code to ssa form. */
21596 /*
21597 * The transformation to ssa form puts a phi function
21598 * on each of edge of a dominance frontier where that
21599 * phi function might be needed. At -O2 if we don't
21600 * eleminate the excess phi functions we can get an
21601 * exponential code size growth. So I kill the extra
21602 * phi functions early and I kill them often.
21603 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021604 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021605 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021606
Eric Biederman83b991a2003-10-11 06:20:25 +000021607 /* Remove dead code */
21608 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021609 verify_consistency(state);
21610
Eric Biedermanb138ac82003-04-22 18:44:01 +000021611 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021612 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021613 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021614 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021615 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021616 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021617#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021618#warning "WISHLIST implement single use constants (least possible register pressure)"
21619#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021620#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021621 /* Select architecture instructions and an initial partial
21622 * coloring based on architecture constraints.
21623 */
21624 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021625 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021626
Eric Biederman83b991a2003-10-11 06:20:25 +000021627 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021628 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021629 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021630
Eric Biedermanb138ac82003-04-22 18:44:01 +000021631 /* Color all of the variables to see if they will fit in registers */
21632 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021633 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021634
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021635 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021636 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021637
Eric Biedermanb138ac82003-04-22 18:44:01 +000021638 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021639 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021640
Eric Biedermanb138ac82003-04-22 18:44:01 +000021641 /* Remove the optimization information.
21642 * This is more to check for memory consistency than to free memory.
21643 */
Eric Biederman90089602004-05-28 14:11:54 +000021644 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021645}
21646
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021647static void print_op_asm(struct compile_state *state,
21648 struct triple *ins, FILE *fp)
21649{
21650 struct asm_info *info;
21651 const char *ptr;
21652 unsigned lhs, rhs, i;
21653 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021654 lhs = ins->lhs;
21655 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021656 /* Don't count the clobbers in lhs */
21657 for(i = 0; i < lhs; i++) {
21658 if (LHS(ins, i)->type == &void_type) {
21659 break;
21660 }
21661 }
21662 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021663 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021664 fputc('\t', fp);
21665 for(ptr = info->str; *ptr; ptr++) {
21666 char *next;
21667 unsigned long param;
21668 struct triple *piece;
21669 if (*ptr != '%') {
21670 fputc(*ptr, fp);
21671 continue;
21672 }
21673 ptr++;
21674 if (*ptr == '%') {
21675 fputc('%', fp);
21676 continue;
21677 }
21678 param = strtoul(ptr, &next, 10);
21679 if (ptr == next) {
21680 error(state, ins, "Invalid asm template");
21681 }
21682 if (param >= (lhs + rhs)) {
21683 error(state, ins, "Invalid param %%%u in asm template",
21684 param);
21685 }
21686 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021687 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021688 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021689 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021690 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021691 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021692}
21693
21694
21695/* Only use the low x86 byte registers. This allows me
21696 * allocate the entire register when a byte register is used.
21697 */
21698#define X86_4_8BIT_GPRS 1
21699
Eric Biederman83b991a2003-10-11 06:20:25 +000021700/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021701#define X86_MMX_REGS (1<<0)
21702#define X86_XMM_REGS (1<<1)
21703#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021704
Eric Biedermanb138ac82003-04-22 18:44:01 +000021705/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021706#define REGC_FLAGS 0
21707#define REGC_GPR8 1
21708#define REGC_GPR16 2
21709#define REGC_GPR32 3
21710#define REGC_DIVIDEND64 4
21711#define REGC_DIVIDEND32 5
21712#define REGC_MMX 6
21713#define REGC_XMM 7
21714#define REGC_GPR32_8 8
21715#define REGC_GPR16_8 9
21716#define REGC_GPR8_LO 10
21717#define REGC_IMM32 11
21718#define REGC_IMM16 12
21719#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021720#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021721#if LAST_REGC >= MAX_REGC
21722#error "MAX_REGC is to low"
21723#endif
21724
21725/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021726#define REGCM_FLAGS (1 << REGC_FLAGS)
21727#define REGCM_GPR8 (1 << REGC_GPR8)
21728#define REGCM_GPR16 (1 << REGC_GPR16)
21729#define REGCM_GPR32 (1 << REGC_GPR32)
21730#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21731#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21732#define REGCM_MMX (1 << REGC_MMX)
21733#define REGCM_XMM (1 << REGC_XMM)
21734#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21735#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21736#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21737#define REGCM_IMM32 (1 << REGC_IMM32)
21738#define REGCM_IMM16 (1 << REGC_IMM16)
21739#define REGCM_IMM8 (1 << REGC_IMM8)
21740#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021741#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021742
21743/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021744#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021745#define REGC_FLAGS_FIRST REG_EFLAGS
21746#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021747#define REG_AL 3
21748#define REG_BL 4
21749#define REG_CL 5
21750#define REG_DL 6
21751#define REG_AH 7
21752#define REG_BH 8
21753#define REG_CH 9
21754#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021755#define REGC_GPR8_LO_FIRST REG_AL
21756#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021757#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021758#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021759#define REG_AX 11
21760#define REG_BX 12
21761#define REG_CX 13
21762#define REG_DX 14
21763#define REG_SI 15
21764#define REG_DI 16
21765#define REG_BP 17
21766#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021767#define REGC_GPR16_FIRST REG_AX
21768#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021769#define REG_EAX 19
21770#define REG_EBX 20
21771#define REG_ECX 21
21772#define REG_EDX 22
21773#define REG_ESI 23
21774#define REG_EDI 24
21775#define REG_EBP 25
21776#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021777#define REGC_GPR32_FIRST REG_EAX
21778#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021779#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021780#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21781#define REGC_DIVIDEND64_LAST REG_EDXEAX
21782#define REG_DXAX 28
21783#define REGC_DIVIDEND32_FIRST REG_DXAX
21784#define REGC_DIVIDEND32_LAST REG_DXAX
21785#define REG_MMX0 29
21786#define REG_MMX1 30
21787#define REG_MMX2 31
21788#define REG_MMX3 32
21789#define REG_MMX4 33
21790#define REG_MMX5 34
21791#define REG_MMX6 35
21792#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021793#define REGC_MMX_FIRST REG_MMX0
21794#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021795#define REG_XMM0 37
21796#define REG_XMM1 38
21797#define REG_XMM2 39
21798#define REG_XMM3 40
21799#define REG_XMM4 41
21800#define REG_XMM5 42
21801#define REG_XMM6 43
21802#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021803#define REGC_XMM_FIRST REG_XMM0
21804#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021805
21806#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021807#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021808#endif
21809
Eric Biedermanb138ac82003-04-22 18:44:01 +000021810#define LAST_REG REG_XMM7
21811
21812#define REGC_GPR32_8_FIRST REG_EAX
21813#define REGC_GPR32_8_LAST REG_EDX
21814#define REGC_GPR16_8_FIRST REG_AX
21815#define REGC_GPR16_8_LAST REG_DX
21816
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021817#define REGC_IMM8_FIRST -1
21818#define REGC_IMM8_LAST -1
21819#define REGC_IMM16_FIRST -2
21820#define REGC_IMM16_LAST -1
21821#define REGC_IMM32_FIRST -4
21822#define REGC_IMM32_LAST -1
21823
Eric Biedermanb138ac82003-04-22 18:44:01 +000021824#if LAST_REG >= MAX_REGISTERS
21825#error "MAX_REGISTERS to low"
21826#endif
21827
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021828
21829static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021830 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21831 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21832 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21833 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21834 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21835 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21836 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21837 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21838 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21839 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21840 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21841 [REGC_IMM32] = 0,
21842 [REGC_IMM16] = 0,
21843 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021844};
21845
21846static const struct {
21847 int first, last;
21848} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021849 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21850 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21851 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21852 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21853 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21854 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21855 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21856 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21857 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21858 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21859 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21860 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21861 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21862 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021863};
21864
Eric Biederman90089602004-05-28 14:11:54 +000021865#if ARCH_INPUT_REGS != 4
21866#error ARCH_INPUT_REGS size mismatch
21867#endif
21868static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21869 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21870 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21871 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21872 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21873};
21874
21875#if ARCH_OUTPUT_REGS != 4
21876#error ARCH_INPUT_REGS size mismatch
21877#endif
21878static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21879 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21880 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21881 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21882 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21883};
21884
Eric Biederman5ade04a2003-10-22 04:03:46 +000021885static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021886{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021887 memset(arch, 0, sizeof(*arch));
21888 arch->features = 0;
21889}
21890
Eric Biederman90089602004-05-28 14:11:54 +000021891static const struct compiler_flag arch_flags[] = {
21892 { "mmx", X86_MMX_REGS },
21893 { "sse", X86_XMM_REGS },
21894 { "noop-copy", X86_NOOP_COPY },
21895 { 0, 0 },
21896};
21897static const struct compiler_flag arch_cpus[] = {
21898 { "i386", 0 },
21899 { "p2", X86_MMX_REGS },
21900 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21901 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21902 { "k7", X86_MMX_REGS },
21903 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21904 { "c3", X86_MMX_REGS },
21905 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21906 { 0, 0 }
21907};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021908static int arch_encode_flag(struct arch_state *arch, const char *flag)
21909{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021910 int result;
21911 int act;
21912
21913 act = 1;
21914 result = -1;
21915 if (strncmp(flag, "no-", 3) == 0) {
21916 flag += 3;
21917 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021918 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021919 if (act && strncmp(flag, "cpu=", 4) == 0) {
21920 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021921 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021922 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021923 else {
Eric Biederman90089602004-05-28 14:11:54 +000021924 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021925 }
21926 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021927}
21928
Eric Biederman90089602004-05-28 14:11:54 +000021929static void arch_usage(FILE *fp)
21930{
21931 flag_usage(fp, arch_flags, "-m", "-mno-");
21932 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21933}
21934
Eric Biedermanb138ac82003-04-22 18:44:01 +000021935static unsigned arch_regc_size(struct compile_state *state, int class)
21936{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021937 if ((class < 0) || (class > LAST_REGC)) {
21938 return 0;
21939 }
21940 return regc_size[class];
21941}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021942
Eric Biedermanb138ac82003-04-22 18:44:01 +000021943static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21944{
21945 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021946 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021947 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021948 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021949
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021950 /* Special case for the immediates */
21951 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21952 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21953 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021954 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021955 return 0;
21956 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021957 return (regcm1 & regcm2) ||
21958 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21959}
21960
21961static void arch_reg_equivs(
21962 struct compile_state *state, unsigned *equiv, int reg)
21963{
21964 if ((reg < 0) || (reg > LAST_REG)) {
21965 internal_error(state, 0, "invalid register");
21966 }
21967 *equiv++ = reg;
21968 switch(reg) {
21969 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021970#if X86_4_8BIT_GPRS
21971 *equiv++ = REG_AH;
21972#endif
21973 *equiv++ = REG_AX;
21974 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021975 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021976 *equiv++ = REG_EDXEAX;
21977 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021978 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021979#if X86_4_8BIT_GPRS
21980 *equiv++ = REG_AL;
21981#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021982 *equiv++ = REG_AX;
21983 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021984 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021985 *equiv++ = REG_EDXEAX;
21986 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021987 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021988#if X86_4_8BIT_GPRS
21989 *equiv++ = REG_BH;
21990#endif
21991 *equiv++ = REG_BX;
21992 *equiv++ = REG_EBX;
21993 break;
21994
Eric Biedermanb138ac82003-04-22 18:44:01 +000021995 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021996#if X86_4_8BIT_GPRS
21997 *equiv++ = REG_BL;
21998#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021999 *equiv++ = REG_BX;
22000 *equiv++ = REG_EBX;
22001 break;
22002 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022003#if X86_4_8BIT_GPRS
22004 *equiv++ = REG_CH;
22005#endif
22006 *equiv++ = REG_CX;
22007 *equiv++ = REG_ECX;
22008 break;
22009
Eric Biedermanb138ac82003-04-22 18:44:01 +000022010 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022011#if X86_4_8BIT_GPRS
22012 *equiv++ = REG_CL;
22013#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022014 *equiv++ = REG_CX;
22015 *equiv++ = REG_ECX;
22016 break;
22017 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022018#if X86_4_8BIT_GPRS
22019 *equiv++ = REG_DH;
22020#endif
22021 *equiv++ = REG_DX;
22022 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022023 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022024 *equiv++ = REG_EDXEAX;
22025 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022026 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022027#if X86_4_8BIT_GPRS
22028 *equiv++ = REG_DL;
22029#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022030 *equiv++ = REG_DX;
22031 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022032 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022033 *equiv++ = REG_EDXEAX;
22034 break;
22035 case REG_AX:
22036 *equiv++ = REG_AL;
22037 *equiv++ = REG_AH;
22038 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022039 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022040 *equiv++ = REG_EDXEAX;
22041 break;
22042 case REG_BX:
22043 *equiv++ = REG_BL;
22044 *equiv++ = REG_BH;
22045 *equiv++ = REG_EBX;
22046 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022047 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022048 *equiv++ = REG_CL;
22049 *equiv++ = REG_CH;
22050 *equiv++ = REG_ECX;
22051 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022052 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022053 *equiv++ = REG_DL;
22054 *equiv++ = REG_DH;
22055 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022056 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022057 *equiv++ = REG_EDXEAX;
22058 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022059 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022060 *equiv++ = REG_ESI;
22061 break;
22062 case REG_DI:
22063 *equiv++ = REG_EDI;
22064 break;
22065 case REG_BP:
22066 *equiv++ = REG_EBP;
22067 break;
22068 case REG_SP:
22069 *equiv++ = REG_ESP;
22070 break;
22071 case REG_EAX:
22072 *equiv++ = REG_AL;
22073 *equiv++ = REG_AH;
22074 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022075 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022076 *equiv++ = REG_EDXEAX;
22077 break;
22078 case REG_EBX:
22079 *equiv++ = REG_BL;
22080 *equiv++ = REG_BH;
22081 *equiv++ = REG_BX;
22082 break;
22083 case REG_ECX:
22084 *equiv++ = REG_CL;
22085 *equiv++ = REG_CH;
22086 *equiv++ = REG_CX;
22087 break;
22088 case REG_EDX:
22089 *equiv++ = REG_DL;
22090 *equiv++ = REG_DH;
22091 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022092 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022093 *equiv++ = REG_EDXEAX;
22094 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022095 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022096 *equiv++ = REG_SI;
22097 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022098 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022099 *equiv++ = REG_DI;
22100 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022101 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022102 *equiv++ = REG_BP;
22103 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022104 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022105 *equiv++ = REG_SP;
22106 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022107 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022108 *equiv++ = REG_AL;
22109 *equiv++ = REG_AH;
22110 *equiv++ = REG_DL;
22111 *equiv++ = REG_DH;
22112 *equiv++ = REG_AX;
22113 *equiv++ = REG_DX;
22114 *equiv++ = REG_EAX;
22115 *equiv++ = REG_EDX;
22116 *equiv++ = REG_EDXEAX;
22117 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022118 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022119 *equiv++ = REG_AL;
22120 *equiv++ = REG_AH;
22121 *equiv++ = REG_DL;
22122 *equiv++ = REG_DH;
22123 *equiv++ = REG_AX;
22124 *equiv++ = REG_DX;
22125 *equiv++ = REG_EAX;
22126 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022127 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022128 break;
22129 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022130 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022131}
22132
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022133static unsigned arch_avail_mask(struct compile_state *state)
22134{
22135 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022136 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022137 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22138 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022139 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022140 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022141 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022142 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022143 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022144 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022145 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022146 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022147 return avail_mask;
22148}
22149
22150static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22151{
22152 unsigned mask, result;
22153 int class, class2;
22154 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022155
22156 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22157 if ((result & mask) == 0) {
22158 continue;
22159 }
22160 if (class > LAST_REGC) {
22161 result &= ~mask;
22162 }
22163 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22164 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22165 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22166 result |= (1 << class2);
22167 }
22168 }
22169 }
Eric Biederman530b5192003-07-01 10:05:30 +000022170 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022171 return result;
22172}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022173
Eric Biedermand1ea5392003-06-28 06:49:45 +000022174static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22175{
22176 /* Like arch_regcm_normalize except immediate register classes are excluded */
22177 regcm = arch_regcm_normalize(state, regcm);
22178 /* Remove the immediate register classes */
22179 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22180 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022181
Eric Biedermand1ea5392003-06-28 06:49:45 +000022182}
22183
Eric Biedermanb138ac82003-04-22 18:44:01 +000022184static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22185{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022186 unsigned mask;
22187 int class;
22188 mask = 0;
22189 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022190 if ((reg >= regcm_bound[class].first) &&
22191 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022192 mask |= (1 << class);
22193 }
22194 }
22195 if (!mask) {
22196 internal_error(state, 0, "reg %d not in any class", reg);
22197 }
22198 return mask;
22199}
22200
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022201static struct reg_info arch_reg_constraint(
22202 struct compile_state *state, struct type *type, const char *constraint)
22203{
22204 static const struct {
22205 char class;
22206 unsigned int mask;
22207 unsigned int reg;
22208 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022209 { 'r', REGCM_GPR32, REG_UNSET },
22210 { 'g', REGCM_GPR32, REG_UNSET },
22211 { 'p', REGCM_GPR32, REG_UNSET },
22212 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022213 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022214 { 'x', REGCM_XMM, REG_UNSET },
22215 { 'y', REGCM_MMX, REG_UNSET },
22216 { 'a', REGCM_GPR32, REG_EAX },
22217 { 'b', REGCM_GPR32, REG_EBX },
22218 { 'c', REGCM_GPR32, REG_ECX },
22219 { 'd', REGCM_GPR32, REG_EDX },
22220 { 'D', REGCM_GPR32, REG_EDI },
22221 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022222 { '\0', 0, REG_UNSET },
22223 };
22224 unsigned int regcm;
22225 unsigned int mask, reg;
22226 struct reg_info result;
22227 const char *ptr;
22228 regcm = arch_type_to_regcm(state, type);
22229 reg = REG_UNSET;
22230 mask = 0;
22231 for(ptr = constraint; *ptr; ptr++) {
22232 int i;
22233 if (*ptr == ' ') {
22234 continue;
22235 }
22236 for(i = 0; constraints[i].class != '\0'; i++) {
22237 if (constraints[i].class == *ptr) {
22238 break;
22239 }
22240 }
22241 if (constraints[i].class == '\0') {
22242 error(state, 0, "invalid register constraint ``%c''", *ptr);
22243 break;
22244 }
22245 if ((constraints[i].mask & regcm) == 0) {
22246 error(state, 0, "invalid register class %c specified",
22247 *ptr);
22248 }
22249 mask |= constraints[i].mask;
22250 if (constraints[i].reg != REG_UNSET) {
22251 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22252 error(state, 0, "Only one register may be specified");
22253 }
22254 reg = constraints[i].reg;
22255 }
22256 }
22257 result.reg = reg;
22258 result.regcm = mask;
22259 return result;
22260}
22261
22262static struct reg_info arch_reg_clobber(
22263 struct compile_state *state, const char *clobber)
22264{
22265 struct reg_info result;
22266 if (strcmp(clobber, "memory") == 0) {
22267 result.reg = REG_UNSET;
22268 result.regcm = 0;
22269 }
Eric Biederman90089602004-05-28 14:11:54 +000022270 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022271 result.reg = REG_EAX;
22272 result.regcm = REGCM_GPR32;
22273 }
Eric Biederman90089602004-05-28 14:11:54 +000022274 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022275 result.reg = REG_EBX;
22276 result.regcm = REGCM_GPR32;
22277 }
Eric Biederman90089602004-05-28 14:11:54 +000022278 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022279 result.reg = REG_ECX;
22280 result.regcm = REGCM_GPR32;
22281 }
Eric Biederman90089602004-05-28 14:11:54 +000022282 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022283 result.reg = REG_EDX;
22284 result.regcm = REGCM_GPR32;
22285 }
Eric Biederman90089602004-05-28 14:11:54 +000022286 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022287 result.reg = REG_ESI;
22288 result.regcm = REGCM_GPR32;
22289 }
Eric Biederman90089602004-05-28 14:11:54 +000022290 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022291 result.reg = REG_EDI;
22292 result.regcm = REGCM_GPR32;
22293 }
Eric Biederman90089602004-05-28 14:11:54 +000022294 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022295 result.reg = REG_EBP;
22296 result.regcm = REGCM_GPR32;
22297 }
Eric Biederman90089602004-05-28 14:11:54 +000022298 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022299 result.reg = REG_ESP;
22300 result.regcm = REGCM_GPR32;
22301 }
22302 else if (strcmp(clobber, "cc") == 0) {
22303 result.reg = REG_EFLAGS;
22304 result.regcm = REGCM_FLAGS;
22305 }
22306 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22307 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22308 result.reg = REG_XMM0 + octdigval(clobber[3]);
22309 result.regcm = REGCM_XMM;
22310 }
Eric Biederman90089602004-05-28 14:11:54 +000022311 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022312 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22313 result.reg = REG_MMX0 + octdigval(clobber[3]);
22314 result.regcm = REGCM_MMX;
22315 }
22316 else {
Eric Biederman90089602004-05-28 14:11:54 +000022317 error(state, 0, "unknown register name `%s' in asm",
22318 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022319 result.reg = REG_UNSET;
22320 result.regcm = 0;
22321 }
22322 return result;
22323}
22324
Stefan Reinauer14e22772010-04-27 06:56:47 +000022325static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022326 char *used, int reg, unsigned classes)
22327{
22328 unsigned mask;
22329 if (used[reg]) {
22330 return REG_UNSET;
22331 }
22332 mask = arch_reg_regcm(state, reg);
22333 return (classes & mask) ? reg : REG_UNSET;
22334}
22335
22336static int arch_select_free_register(
22337 struct compile_state *state, char *used, int classes)
22338{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022339 /* Live ranges with the most neighbors are colored first.
22340 *
22341 * Generally it does not matter which colors are given
22342 * as the register allocator attempts to color live ranges
22343 * in an order where you are guaranteed not to run out of colors.
22344 *
22345 * Occasionally the register allocator cannot find an order
22346 * of register selection that will find a free color. To
22347 * increase the odds the register allocator will work when
22348 * it guesses first give out registers from register classes
22349 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022350 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022351 */
22352 int i, reg;
22353 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022354 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022355 reg = do_select_reg(state, used, i, classes);
22356 }
22357 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22358 reg = do_select_reg(state, used, i, classes);
22359 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022360 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022361 reg = do_select_reg(state, used, i, classes);
22362 }
22363 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22364 reg = do_select_reg(state, used, i, classes);
22365 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022366 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22367 reg = do_select_reg(state, used, i, classes);
22368 }
Eric Biederman530b5192003-07-01 10:05:30 +000022369 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22370 reg = do_select_reg(state, used, i, classes);
22371 }
22372 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22373 reg = do_select_reg(state, used, i, classes);
22374 }
22375 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022376 reg = do_select_reg(state, used, i, classes);
22377 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022378 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22379 reg = do_select_reg(state, used, i, classes);
22380 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022381 return reg;
22382}
22383
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022384
Stefan Reinauer14e22772010-04-27 06:56:47 +000022385static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022386{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022387
22388#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022389#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022390#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022392 mask = 0;
22393 switch(type->type & TYPE_MASK) {
22394 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022395 case TYPE_VOID:
22396 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022397 break;
22398 case TYPE_CHAR:
22399 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022400 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022401 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022402 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022403 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022404 REGCM_MMX | REGCM_XMM |
22405 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022406 break;
22407 case TYPE_SHORT:
22408 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022409 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022410 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022411 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022412 REGCM_MMX | REGCM_XMM |
22413 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022414 break;
Eric Biederman90089602004-05-28 14:11:54 +000022415 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022416 case TYPE_INT:
22417 case TYPE_UINT:
22418 case TYPE_LONG:
22419 case TYPE_ULONG:
22420 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022421 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022422 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22423 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022424 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022425 break;
Eric Biederman90089602004-05-28 14:11:54 +000022426 case TYPE_JOIN:
22427 case TYPE_UNION:
22428 mask = arch_type_to_regcm(state, type->left);
22429 break;
22430 case TYPE_OVERLAP:
22431 mask = arch_type_to_regcm(state, type->left) &
22432 arch_type_to_regcm(state, type->right);
22433 break;
22434 case TYPE_BITFIELD:
22435 mask = arch_type_to_regcm(state, type->left);
22436 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022437 default:
Eric Biederman90089602004-05-28 14:11:54 +000022438 fprintf(state->errout, "type: ");
22439 name_of(state->errout, type);
22440 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022441 internal_error(state, 0, "no register class for type");
22442 break;
22443 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022444 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022445 return mask;
22446}
22447
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022448static int is_imm32(struct triple *imm)
22449{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022450 // second condition commented out to prevent compiler warning:
22451 // imm->u.cval is always 32bit unsigned, so the comparison is
22452 // always true.
22453 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022454 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022455
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022456}
22457static int is_imm16(struct triple *imm)
22458{
22459 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22460}
22461static int is_imm8(struct triple *imm)
22462{
22463 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22464}
22465
22466static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022467{
22468 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022469 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022470 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022471 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022472 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022473 if (!is_imm32(imm)) {
22474 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022475 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022476 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022477 use_triple(imm, ins);
22478 *expr = imm;
22479 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022480}
22481
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022482static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022483{
22484 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022485 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022486 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022487 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022488 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022489 if (!is_imm8(imm)) {
22490 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022491 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022492 unuse_triple(*expr, ins);
22493 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022494 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022495 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022496}
22497
Eric Biederman530b5192003-07-01 10:05:30 +000022498#define TEMPLATE_NOP 0
22499#define TEMPLATE_INTCONST8 1
22500#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022501#define TEMPLATE_UNKNOWNVAL 3
22502#define TEMPLATE_COPY8_REG 5
22503#define TEMPLATE_COPY16_REG 6
22504#define TEMPLATE_COPY32_REG 7
22505#define TEMPLATE_COPY_IMM8 8
22506#define TEMPLATE_COPY_IMM16 9
22507#define TEMPLATE_COPY_IMM32 10
22508#define TEMPLATE_PHI8 11
22509#define TEMPLATE_PHI16 12
22510#define TEMPLATE_PHI32 13
22511#define TEMPLATE_STORE8 14
22512#define TEMPLATE_STORE16 15
22513#define TEMPLATE_STORE32 16
22514#define TEMPLATE_LOAD8 17
22515#define TEMPLATE_LOAD16 18
22516#define TEMPLATE_LOAD32 19
22517#define TEMPLATE_BINARY8_REG 20
22518#define TEMPLATE_BINARY16_REG 21
22519#define TEMPLATE_BINARY32_REG 22
22520#define TEMPLATE_BINARY8_IMM 23
22521#define TEMPLATE_BINARY16_IMM 24
22522#define TEMPLATE_BINARY32_IMM 25
22523#define TEMPLATE_SL8_CL 26
22524#define TEMPLATE_SL16_CL 27
22525#define TEMPLATE_SL32_CL 28
22526#define TEMPLATE_SL8_IMM 29
22527#define TEMPLATE_SL16_IMM 30
22528#define TEMPLATE_SL32_IMM 31
22529#define TEMPLATE_UNARY8 32
22530#define TEMPLATE_UNARY16 33
22531#define TEMPLATE_UNARY32 34
22532#define TEMPLATE_CMP8_REG 35
22533#define TEMPLATE_CMP16_REG 36
22534#define TEMPLATE_CMP32_REG 37
22535#define TEMPLATE_CMP8_IMM 38
22536#define TEMPLATE_CMP16_IMM 39
22537#define TEMPLATE_CMP32_IMM 40
22538#define TEMPLATE_TEST8 41
22539#define TEMPLATE_TEST16 42
22540#define TEMPLATE_TEST32 43
22541#define TEMPLATE_SET 44
22542#define TEMPLATE_JMP 45
22543#define TEMPLATE_RET 46
22544#define TEMPLATE_INB_DX 47
22545#define TEMPLATE_INB_IMM 48
22546#define TEMPLATE_INW_DX 49
22547#define TEMPLATE_INW_IMM 50
22548#define TEMPLATE_INL_DX 51
22549#define TEMPLATE_INL_IMM 52
22550#define TEMPLATE_OUTB_DX 53
22551#define TEMPLATE_OUTB_IMM 54
22552#define TEMPLATE_OUTW_DX 55
22553#define TEMPLATE_OUTW_IMM 56
22554#define TEMPLATE_OUTL_DX 57
22555#define TEMPLATE_OUTL_IMM 58
22556#define TEMPLATE_BSF 59
22557#define TEMPLATE_RDMSR 60
22558#define TEMPLATE_WRMSR 61
22559#define TEMPLATE_UMUL8 62
22560#define TEMPLATE_UMUL16 63
22561#define TEMPLATE_UMUL32 64
22562#define TEMPLATE_DIV8 65
22563#define TEMPLATE_DIV16 66
22564#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022565#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022566#if LAST_TEMPLATE >= MAX_TEMPLATES
22567#error "MAX_TEMPLATES to low"
22568#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022569
Eric Biederman530b5192003-07-01 10:05:30 +000022570#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 +000022571#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022572#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022573
Eric Biedermanb138ac82003-04-22 18:44:01 +000022574
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022575static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022576 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022577 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022578 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22639 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22640 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22641 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22642 },
22643 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022644 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022645 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22646 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022647 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022648 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22649 },
Eric Biederman90089602004-05-28 14:11:54 +000022650 [TEMPLATE_UNKNOWNVAL] = {
22651 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22652 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022653 [TEMPLATE_COPY8_REG] = {
22654 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22655 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022656 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022657 [TEMPLATE_COPY16_REG] = {
22658 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22659 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22660 },
22661 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022662 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022663 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022664 },
22665 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022666 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022667 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22668 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022669 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022670 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022671 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22672 },
22673 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022674 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022675 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22676 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022677 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022678 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022679 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22680 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022681 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022682 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022683 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022684 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022685 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022686 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022687 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022688 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022689 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022690 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022691 [0] = { REG_UNSET, REGCM_GPR32 },
22692 [1] = { REG_UNSET, REGCM_GPR8_LO },
22693 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022694 },
22695 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022696 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022697 [0] = { REG_UNSET, REGCM_GPR32 },
22698 [1] = { REG_UNSET, REGCM_GPR16 },
22699 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022700 },
22701 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022702 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022703 [0] = { REG_UNSET, REGCM_GPR32 },
22704 [1] = { REG_UNSET, REGCM_GPR32 },
22705 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022706 },
22707 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022708 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022709 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22710 },
22711 [TEMPLATE_LOAD16] = {
22712 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22713 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22714 },
22715 [TEMPLATE_LOAD32] = {
22716 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22717 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22718 },
Eric Biederman530b5192003-07-01 10:05:30 +000022719 [TEMPLATE_BINARY8_REG] = {
22720 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022721 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022722 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22723 [1] = { REG_UNSET, REGCM_GPR8_LO },
22724 },
22725 },
22726 [TEMPLATE_BINARY16_REG] = {
22727 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022728 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022729 [0] = { REG_VIRT0, REGCM_GPR16 },
22730 [1] = { REG_UNSET, REGCM_GPR16 },
22731 },
22732 },
22733 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022734 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022735 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022736 [0] = { REG_VIRT0, REGCM_GPR32 },
22737 [1] = { REG_UNSET, REGCM_GPR32 },
22738 },
22739 },
Eric Biederman530b5192003-07-01 10:05:30 +000022740 [TEMPLATE_BINARY8_IMM] = {
22741 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022742 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022743 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22744 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22745 },
22746 },
22747 [TEMPLATE_BINARY16_IMM] = {
22748 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022749 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022750 [0] = { REG_VIRT0, REGCM_GPR16 },
22751 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22752 },
22753 },
22754 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022755 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022756 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022757 [0] = { REG_VIRT0, REGCM_GPR32 },
22758 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22759 },
22760 },
Eric Biederman530b5192003-07-01 10:05:30 +000022761 [TEMPLATE_SL8_CL] = {
22762 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022763 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022764 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22765 [1] = { REG_CL, REGCM_GPR8_LO },
22766 },
22767 },
22768 [TEMPLATE_SL16_CL] = {
22769 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022770 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022771 [0] = { REG_VIRT0, REGCM_GPR16 },
22772 [1] = { REG_CL, REGCM_GPR8_LO },
22773 },
22774 },
22775 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022776 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022777 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022778 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022779 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022780 },
22781 },
Eric Biederman530b5192003-07-01 10:05:30 +000022782 [TEMPLATE_SL8_IMM] = {
22783 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022784 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022785 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22786 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22787 },
22788 },
22789 [TEMPLATE_SL16_IMM] = {
22790 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022791 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022792 [0] = { REG_VIRT0, REGCM_GPR16 },
22793 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22794 },
22795 },
22796 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022797 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022798 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022799 [0] = { REG_VIRT0, REGCM_GPR32 },
22800 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22801 },
22802 },
Eric Biederman530b5192003-07-01 10:05:30 +000022803 [TEMPLATE_UNARY8] = {
22804 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22805 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22806 },
22807 [TEMPLATE_UNARY16] = {
22808 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22809 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22810 },
22811 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022812 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22813 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22814 },
Eric Biederman530b5192003-07-01 10:05:30 +000022815 [TEMPLATE_CMP8_REG] = {
22816 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22817 .rhs = {
22818 [0] = { REG_UNSET, REGCM_GPR8_LO },
22819 [1] = { REG_UNSET, REGCM_GPR8_LO },
22820 },
22821 },
22822 [TEMPLATE_CMP16_REG] = {
22823 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22824 .rhs = {
22825 [0] = { REG_UNSET, REGCM_GPR16 },
22826 [1] = { REG_UNSET, REGCM_GPR16 },
22827 },
22828 },
22829 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022830 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22831 .rhs = {
22832 [0] = { REG_UNSET, REGCM_GPR32 },
22833 [1] = { REG_UNSET, REGCM_GPR32 },
22834 },
22835 },
Eric Biederman530b5192003-07-01 10:05:30 +000022836 [TEMPLATE_CMP8_IMM] = {
22837 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22838 .rhs = {
22839 [0] = { REG_UNSET, REGCM_GPR8_LO },
22840 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22841 },
22842 },
22843 [TEMPLATE_CMP16_IMM] = {
22844 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22845 .rhs = {
22846 [0] = { REG_UNSET, REGCM_GPR16 },
22847 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22848 },
22849 },
22850 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022851 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22852 .rhs = {
22853 [0] = { REG_UNSET, REGCM_GPR32 },
22854 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22855 },
22856 },
Eric Biederman530b5192003-07-01 10:05:30 +000022857 [TEMPLATE_TEST8] = {
22858 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22859 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22860 },
22861 [TEMPLATE_TEST16] = {
22862 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22863 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22864 },
22865 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022866 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22867 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22868 },
22869 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022870 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022871 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22872 },
22873 [TEMPLATE_JMP] = {
22874 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22875 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022876 [TEMPLATE_RET] = {
22877 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22878 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022879 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022880 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022881 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22882 },
22883 [TEMPLATE_INB_IMM] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022884 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022885 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22886 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022887 [TEMPLATE_INW_DX] = {
22888 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022889 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22890 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022891 [TEMPLATE_INW_IMM] = {
22892 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022893 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22894 },
22895 [TEMPLATE_INL_DX] = {
22896 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22897 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22898 },
22899 [TEMPLATE_INL_IMM] = {
22900 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22901 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22902 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022903 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022904 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022905 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022906 [1] = { REG_DX, REGCM_GPR16 },
22907 },
22908 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022909 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022910 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022911 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022912 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22913 },
22914 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022915 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022916 .rhs = {
22917 [0] = { REG_AX, REGCM_GPR16 },
22918 [1] = { REG_DX, REGCM_GPR16 },
22919 },
22920 },
22921 [TEMPLATE_OUTW_IMM] = {
22922 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022923 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022924 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22925 },
22926 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022927 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022928 .rhs = {
22929 [0] = { REG_EAX, REGCM_GPR32 },
22930 [1] = { REG_DX, REGCM_GPR16 },
22931 },
22932 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022933 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022934 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022935 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022936 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22937 },
22938 },
22939 [TEMPLATE_BSF] = {
22940 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22941 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22942 },
22943 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022944 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022945 [0] = { REG_EAX, REGCM_GPR32 },
22946 [1] = { REG_EDX, REGCM_GPR32 },
22947 },
22948 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22949 },
22950 [TEMPLATE_WRMSR] = {
22951 .rhs = {
22952 [0] = { REG_ECX, REGCM_GPR32 },
22953 [1] = { REG_EAX, REGCM_GPR32 },
22954 [2] = { REG_EDX, REGCM_GPR32 },
22955 },
22956 },
Eric Biederman530b5192003-07-01 10:05:30 +000022957 [TEMPLATE_UMUL8] = {
22958 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022959 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022960 [0] = { REG_AL, REGCM_GPR8_LO },
22961 [1] = { REG_UNSET, REGCM_GPR8_LO },
22962 },
22963 },
22964 [TEMPLATE_UMUL16] = {
22965 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022966 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022967 [0] = { REG_AX, REGCM_GPR16 },
22968 [1] = { REG_UNSET, REGCM_GPR16 },
22969 },
22970 },
22971 [TEMPLATE_UMUL32] = {
22972 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022973 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022974 [0] = { REG_EAX, REGCM_GPR32 },
22975 [1] = { REG_UNSET, REGCM_GPR32 },
22976 },
22977 },
Eric Biederman530b5192003-07-01 10:05:30 +000022978 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022979 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022980 [0] = { REG_AL, REGCM_GPR8_LO },
22981 [1] = { REG_AH, REGCM_GPR8 },
22982 },
22983 .rhs = {
22984 [0] = { REG_AX, REGCM_GPR16 },
22985 [1] = { REG_UNSET, REGCM_GPR8_LO },
22986 },
22987 },
22988 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022989 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022990 [0] = { REG_AX, REGCM_GPR16 },
22991 [1] = { REG_DX, REGCM_GPR16 },
22992 },
22993 .rhs = {
22994 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22995 [1] = { REG_UNSET, REGCM_GPR16 },
22996 },
22997 },
22998 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022999 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023000 [0] = { REG_EAX, REGCM_GPR32 },
23001 [1] = { REG_EDX, REGCM_GPR32 },
23002 },
23003 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023004 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023005 [1] = { REG_UNSET, REGCM_GPR32 },
23006 },
23007 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023008};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023009
Eric Biederman83b991a2003-10-11 06:20:25 +000023010static void fixup_branch(struct compile_state *state,
23011 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23012 struct triple *left, struct triple *right)
23013{
23014 struct triple *test;
23015 if (!left) {
23016 internal_error(state, branch, "no branch test?");
23017 }
23018 test = pre_triple(state, branch,
23019 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023020 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023021 if (cmp_op == OP_CMP) {
23022 test->template_id = TEMPLATE_CMP32_REG;
23023 if (get_imm32(test, &RHS(test, 1))) {
23024 test->template_id = TEMPLATE_CMP32_IMM;
23025 }
23026 }
23027 use_triple(RHS(test, 0), test);
23028 use_triple(RHS(test, 1), test);
23029 unuse_triple(RHS(branch, 0), branch);
23030 RHS(branch, 0) = test;
23031 branch->op = jmp_op;
23032 branch->template_id = TEMPLATE_JMP;
23033 use_triple(RHS(branch, 0), branch);
23034}
23035
Eric Biedermanb138ac82003-04-22 18:44:01 +000023036static void fixup_branches(struct compile_state *state,
23037 struct triple *cmp, struct triple *use, int jmp_op)
23038{
23039 struct triple_set *entry, *next;
23040 for(entry = use->use; entry; entry = next) {
23041 next = entry->next;
23042 if (entry->member->op == OP_COPY) {
23043 fixup_branches(state, cmp, entry->member, jmp_op);
23044 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023045 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023046 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023047 struct triple *left, *right;
23048 left = right = 0;
23049 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023050 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023051 right = RHS(cmp, 1);
23052 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023053 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023054 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023055 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023056 }
23057 }
23058}
23059
Stefan Reinauer14e22772010-04-27 06:56:47 +000023060static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023061 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23062{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023063 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023064 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023065
23066 /* Put a barrier up before the cmp which preceeds the
23067 * copy instruction. If a set actually occurs this gives
23068 * us a chance to move variables in registers out of the way.
23069 */
23070
23071 /* Modify the comparison operator */
23072 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023073 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023074 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023075 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023076 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023077 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023078 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023079 }
23080 /* Generate the instruction sequence that will transform the
23081 * result of the comparison into a logical value.
23082 */
Eric Biederman90089602004-05-28 14:11:54 +000023083 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023084 use_triple(ins, set);
23085 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023086
Eric Biederman90089602004-05-28 14:11:54 +000023087 convert = set;
23088 if (!equiv_types(ins->type, set->type)) {
23089 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23090 use_triple(set, convert);
23091 convert->template_id = TEMPLATE_COPY32_REG;
23092 }
23093
Eric Biedermanb138ac82003-04-22 18:44:01 +000023094 for(entry = ins->use; entry; entry = next) {
23095 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023096 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023097 continue;
23098 }
Eric Biederman90089602004-05-28 14:11:54 +000023099 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023100 }
Eric Biederman90089602004-05-28 14:11:54 +000023101 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023102}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023103
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023104struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23105{
23106 struct ins_template *template;
23107 struct reg_info result;
23108 int zlhs;
23109 if (ins->op == OP_PIECE) {
23110 index = ins->u.cval;
23111 ins = MISC(ins, 0);
23112 }
Eric Biederman90089602004-05-28 14:11:54 +000023113 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023114 if (triple_is_def(state, ins)) {
23115 zlhs = 1;
23116 }
23117 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023118 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023119 index, tops(ins->op));
23120 }
23121 switch(ins->op) {
23122 case OP_ASM:
23123 template = &ins->u.ainfo->tmpl;
23124 break;
23125 default:
23126 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023127 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023128 ins->template_id);
23129 }
23130 template = &templates[ins->template_id];
23131 break;
23132 }
23133 result = template->lhs[index];
23134 result.regcm = arch_regcm_normalize(state, result.regcm);
23135 if (result.reg != REG_UNNEEDED) {
23136 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23137 }
23138 if (result.regcm == 0) {
23139 internal_error(state, ins, "lhs %d regcm == 0", index);
23140 }
23141 return result;
23142}
23143
23144struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23145{
23146 struct reg_info result;
23147 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023148 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023149 (ins->op == OP_PIECE)) {
23150 internal_error(state, ins, "index %d out of range for %s\n",
23151 index, tops(ins->op));
23152 }
23153 switch(ins->op) {
23154 case OP_ASM:
23155 template = &ins->u.ainfo->tmpl;
23156 break;
Eric Biederman90089602004-05-28 14:11:54 +000023157 case OP_PHI:
23158 index = 0;
23159 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023160 default:
23161 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023162 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023163 ins->template_id);
23164 }
23165 template = &templates[ins->template_id];
23166 break;
23167 }
23168 result = template->rhs[index];
23169 result.regcm = arch_regcm_normalize(state, result.regcm);
23170 if (result.regcm == 0) {
23171 internal_error(state, ins, "rhs %d regcm == 0", index);
23172 }
23173 return result;
23174}
23175
Eric Biederman530b5192003-07-01 10:05:30 +000023176static struct triple *mod_div(struct compile_state *state,
23177 struct triple *ins, int div_op, int index)
23178{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023179 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023180
Eric Biederman530b5192003-07-01 10:05:30 +000023181 /* Generate the appropriate division instruction */
23182 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23183 RHS(div, 0) = RHS(ins, 0);
23184 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023185 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023186 div->template_id = TEMPLATE_DIV32;
23187 use_triple(RHS(div, 0), div);
23188 use_triple(RHS(div, 1), div);
23189 use_triple(LHS(div, 0), div);
23190 use_triple(LHS(div, 1), div);
23191
Eric Biederman530b5192003-07-01 10:05:30 +000023192 /* Replate uses of ins with the appropriate piece of the div */
23193 propogate_use(state, ins, LHS(div, index));
23194 release_triple(state, ins);
23195
23196 /* Return the address of the next instruction */
23197 return piece1->next;
23198}
23199
Eric Biederman90089602004-05-28 14:11:54 +000023200static int noop_adecl(struct triple *adecl)
23201{
23202 struct triple_set *use;
23203 /* It's a noop if it doesn't specify stoorage */
23204 if (adecl->lhs == 0) {
23205 return 1;
23206 }
23207 /* Is the adecl used? If not it's a noop */
23208 for(use = adecl->use; use ; use = use->next) {
23209 if ((use->member->op != OP_PIECE) ||
23210 (MISC(use->member, 0) != adecl)) {
23211 return 0;
23212 }
23213 }
23214 return 1;
23215}
23216
23217static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23218{
23219 struct triple *mask, *nmask, *shift;
23220 struct triple *val, *val_mask, *val_shift;
23221 struct triple *targ, *targ_mask;
23222 struct triple *new;
23223 ulong_t the_mask, the_nmask;
23224
23225 targ = RHS(ins, 0);
23226 val = RHS(ins, 1);
23227
23228 /* Get constant for the mask value */
23229 the_mask = 1;
23230 the_mask <<= ins->u.bitfield.size;
23231 the_mask -= 1;
23232 the_mask <<= ins->u.bitfield.offset;
23233 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23234 mask->u.cval = the_mask;
23235
23236 /* Get the inverted mask value */
23237 the_nmask = ~the_mask;
23238 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23239 nmask->u.cval = the_nmask;
23240
23241 /* Get constant for the shift value */
23242 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23243 shift->u.cval = ins->u.bitfield.offset;
23244
23245 /* Shift and mask the source value */
23246 val_shift = val;
23247 if (shift->u.cval != 0) {
23248 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23249 use_triple(val, val_shift);
23250 use_triple(shift, val_shift);
23251 }
23252 val_mask = val_shift;
23253 if (is_signed(val->type)) {
23254 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23255 use_triple(val_shift, val_mask);
23256 use_triple(mask, val_mask);
23257 }
23258
23259 /* Mask the target value */
23260 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23261 use_triple(targ, targ_mask);
23262 use_triple(nmask, targ_mask);
23263
23264 /* Now combined them together */
23265 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23266 use_triple(targ_mask, new);
23267 use_triple(val_mask, new);
23268
23269 /* Move all of the users over to the new expression */
23270 propogate_use(state, ins, new);
23271
23272 /* Delete the original triple */
23273 release_triple(state, ins);
23274
23275 /* Restart the transformation at mask */
23276 return mask;
23277}
23278
23279static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23280{
23281 struct triple *mask, *shift;
23282 struct triple *val, *val_mask, *val_shift;
23283 ulong_t the_mask;
23284
23285 val = RHS(ins, 0);
23286
23287 /* Get constant for the mask value */
23288 the_mask = 1;
23289 the_mask <<= ins->u.bitfield.size;
23290 the_mask -= 1;
23291 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23292 mask->u.cval = the_mask;
23293
23294 /* Get constant for the right shift value */
23295 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23296 shift->u.cval = ins->u.bitfield.offset;
23297
23298 /* Shift arithmetic right, to correct the sign */
23299 val_shift = val;
23300 if (shift->u.cval != 0) {
23301 int op;
23302 if (ins->op == OP_SEXTRACT) {
23303 op = OP_SSR;
23304 } else {
23305 op = OP_USR;
23306 }
23307 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23308 use_triple(val, val_shift);
23309 use_triple(shift, val_shift);
23310 }
23311
23312 /* Finally mask the value */
23313 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23314 use_triple(val_shift, val_mask);
23315 use_triple(mask, val_mask);
23316
23317 /* Move all of the users over to the new expression */
23318 propogate_use(state, ins, val_mask);
23319
23320 /* Release the original instruction */
23321 release_triple(state, ins);
23322
23323 return mask;
23324
23325}
23326
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023327static struct triple *transform_to_arch_instruction(
23328 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023329{
23330 /* Transform from generic 3 address instructions
23331 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023332 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023333 * Copies are inserted to preserve the register flexibility
23334 * of 3 address instructions.
23335 */
Eric Biederman90089602004-05-28 14:11:54 +000023336 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023337 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023338 next = ins->next;
23339 switch(ins->op) {
23340 case OP_INTCONST:
23341 ins->template_id = TEMPLATE_INTCONST32;
23342 if (ins->u.cval < 256) {
23343 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023344 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023345 break;
23346 case OP_ADDRCONST:
23347 ins->template_id = TEMPLATE_INTCONST32;
23348 break;
Eric Biederman90089602004-05-28 14:11:54 +000023349 case OP_UNKNOWNVAL:
23350 ins->template_id = TEMPLATE_UNKNOWNVAL;
23351 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023352 case OP_NOOP:
23353 case OP_SDECL:
23354 case OP_BLOBCONST:
23355 case OP_LABEL:
23356 ins->template_id = TEMPLATE_NOP;
23357 break;
23358 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023359 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023360 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023361 value = RHS(ins, 0);
23362 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023363 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023364 }
Eric Biederman90089602004-05-28 14:11:54 +000023365 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023366 ins->template_id = TEMPLATE_COPY_IMM16;
23367 }
Eric Biederman90089602004-05-28 14:11:54 +000023368 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023369 ins->template_id = TEMPLATE_COPY_IMM32;
23370 }
Eric Biederman90089602004-05-28 14:11:54 +000023371 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023372 internal_error(state, ins, "bad constant passed to copy");
23373 }
Eric Biederman90089602004-05-28 14:11:54 +000023374 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023375 ins->template_id = TEMPLATE_COPY8_REG;
23376 }
Eric Biederman90089602004-05-28 14:11:54 +000023377 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023378 ins->template_id = TEMPLATE_COPY16_REG;
23379 }
Eric Biederman90089602004-05-28 14:11:54 +000023380 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023381 ins->template_id = TEMPLATE_COPY32_REG;
23382 }
23383 else {
23384 internal_error(state, ins, "bad type passed to copy");
23385 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023386 break;
23387 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023388 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023389 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023390 ins->template_id = TEMPLATE_PHI8;
23391 }
Eric Biederman90089602004-05-28 14:11:54 +000023392 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023393 ins->template_id = TEMPLATE_PHI16;
23394 }
Eric Biederman90089602004-05-28 14:11:54 +000023395 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023396 ins->template_id = TEMPLATE_PHI32;
23397 }
23398 else {
23399 internal_error(state, ins, "bad type passed to phi");
23400 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023401 break;
Eric Biederman90089602004-05-28 14:11:54 +000023402 case OP_ADECL:
23403 /* Adecls should always be treated as dead code and
23404 * removed. If we are not optimizing they may linger.
23405 */
23406 if (!noop_adecl(ins)) {
23407 internal_error(state, ins, "adecl remains?");
23408 }
23409 ins->template_id = TEMPLATE_NOP;
23410 next = after_lhs(state, ins);
23411 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023412 case OP_STORE:
23413 switch(ins->type->type & TYPE_MASK) {
23414 case TYPE_CHAR: case TYPE_UCHAR:
23415 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023416 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023417 case TYPE_SHORT: case TYPE_USHORT:
23418 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023419 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023420 case TYPE_INT: case TYPE_UINT:
23421 case TYPE_LONG: case TYPE_ULONG:
23422 case TYPE_POINTER:
23423 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023424 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023425 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023426 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023427 break;
23428 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023429 break;
23430 case OP_LOAD:
23431 switch(ins->type->type & TYPE_MASK) {
23432 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023433 case TYPE_SHORT: case TYPE_USHORT:
23434 case TYPE_INT: case TYPE_UINT:
23435 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023436 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023437 break;
23438 default:
23439 internal_error(state, ins, "unknown type in load");
23440 break;
23441 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023442 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023443 break;
23444 case OP_ADD:
23445 case OP_SUB:
23446 case OP_AND:
23447 case OP_XOR:
23448 case OP_OR:
23449 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023450 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023451 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023452 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023453 }
23454 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023455 case OP_SDIVT:
23456 case OP_UDIVT:
23457 ins->template_id = TEMPLATE_DIV32;
23458 next = after_lhs(state, ins);
23459 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023460 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023461 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023462 break;
23463 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023464 next = mod_div(state, ins, OP_UDIVT, 0);
23465 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023466 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023467 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023468 break;
23469 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023470 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023471 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023472 case OP_SMOD:
23473 next = mod_div(state, ins, OP_SDIVT, 1);
23474 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023475 case OP_SL:
23476 case OP_SSR:
23477 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023478 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023479 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023480 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023481 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23482 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023483 }
23484 break;
23485 case OP_INVERT:
23486 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023487 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023488 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023489 case OP_EQ:
23490 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023491 break;
23492 case OP_NOTEQ:
23493 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23494 break;
23495 case OP_SLESS:
23496 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23497 break;
23498 case OP_ULESS:
23499 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23500 break;
23501 case OP_SMORE:
23502 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23503 break;
23504 case OP_UMORE:
23505 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23506 break;
23507 case OP_SLESSEQ:
23508 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23509 break;
23510 case OP_ULESSEQ:
23511 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23512 break;
23513 case OP_SMOREEQ:
23514 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23515 break;
23516 case OP_UMOREEQ:
23517 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23518 break;
23519 case OP_LTRUE:
23520 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23521 break;
23522 case OP_LFALSE:
23523 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23524 break;
23525 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023526 ins->op = OP_JMP;
23527 ins->template_id = TEMPLATE_NOP;
23528 break;
23529 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023530 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023531 RHS(ins, 0)->type, RHS(ins, 0), 0);
23532 break;
23533 case OP_CALL:
23534 ins->template_id = TEMPLATE_NOP;
23535 break;
23536 case OP_RET:
23537 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023538 break;
23539 case OP_INB:
23540 case OP_INW:
23541 case OP_INL:
23542 switch(ins->op) {
23543 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23544 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23545 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23546 }
23547 if (get_imm8(ins, &RHS(ins, 0))) {
23548 ins->template_id += 1;
23549 }
23550 break;
23551 case OP_OUTB:
23552 case OP_OUTW:
23553 case OP_OUTL:
23554 switch(ins->op) {
23555 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23556 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23557 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23558 }
23559 if (get_imm8(ins, &RHS(ins, 1))) {
23560 ins->template_id += 1;
23561 }
23562 break;
23563 case OP_BSF:
23564 case OP_BSR:
23565 ins->template_id = TEMPLATE_BSF;
23566 break;
23567 case OP_RDMSR:
23568 ins->template_id = TEMPLATE_RDMSR;
23569 next = after_lhs(state, ins);
23570 break;
23571 case OP_WRMSR:
23572 ins->template_id = TEMPLATE_WRMSR;
23573 break;
23574 case OP_HLT:
23575 ins->template_id = TEMPLATE_NOP;
23576 break;
23577 case OP_ASM:
23578 ins->template_id = TEMPLATE_NOP;
23579 next = after_lhs(state, ins);
23580 break;
23581 /* Already transformed instructions */
23582 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023583 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023584 break;
23585 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023586 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023587 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023588 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023589 }
23590 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023591 case OP_JMP:
23592 ins->template_id = TEMPLATE_NOP;
23593 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023594 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23595 case OP_JMP_SLESS: case OP_JMP_ULESS:
23596 case OP_JMP_SMORE: case OP_JMP_UMORE:
23597 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23598 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23599 ins->template_id = TEMPLATE_JMP;
23600 break;
23601 case OP_SET_EQ: case OP_SET_NOTEQ:
23602 case OP_SET_SLESS: case OP_SET_ULESS:
23603 case OP_SET_SMORE: case OP_SET_UMORE:
23604 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23605 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23606 ins->template_id = TEMPLATE_SET;
23607 break;
Eric Biederman90089602004-05-28 14:11:54 +000023608 case OP_DEPOSIT:
23609 next = x86_deposit(state, ins);
23610 break;
23611 case OP_SEXTRACT:
23612 case OP_UEXTRACT:
23613 next = x86_extract(state, ins);
23614 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023615 /* Unhandled instructions */
23616 case OP_PIECE:
23617 default:
Eric Biederman90089602004-05-28 14:11:54 +000023618 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023619 ins->op, tops(ins->op));
23620 break;
23621 }
23622 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023623}
23624
Eric Biederman530b5192003-07-01 10:05:30 +000023625static long next_label(struct compile_state *state)
23626{
Eric Biederman90089602004-05-28 14:11:54 +000023627 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023628 return ++label_counter;
23629}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023630static void generate_local_labels(struct compile_state *state)
23631{
23632 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023633 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023634 label = first;
23635 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023636 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023637 (label->op == OP_SDECL)) {
23638 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023639 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023640 } else {
23641 label->u.cval = 0;
23642 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023643
Eric Biedermanb138ac82003-04-22 18:44:01 +000023644 }
23645 label = label->next;
23646 } while(label != first);
23647}
23648
Stefan Reinauer14e22772010-04-27 06:56:47 +000023649static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023650 struct triple *triple, int classes)
23651{
23652 unsigned mask;
23653 int reg;
23654 reg = ID_REG(triple->id);
23655 if (reg == REG_UNSET) {
23656 internal_error(state, triple, "register not set");
23657 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023658 mask = arch_reg_regcm(state, reg);
23659 if (!(classes & mask)) {
23660 internal_error(state, triple, "reg %d in wrong class",
23661 reg);
23662 }
23663 return reg;
23664}
23665
Eric Biederman90089602004-05-28 14:11:54 +000023666
Eric Biederman530b5192003-07-01 10:05:30 +000023667#if REG_XMM7 != 44
23668#error "Registers have renumberd fix arch_reg_str"
23669#endif
Eric Biederman90089602004-05-28 14:11:54 +000023670static const char *arch_regs[] = {
23671 "%unset",
23672 "%unneeded",
23673 "%eflags",
23674 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23675 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23676 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23677 "%edx:%eax",
23678 "%dx:%ax",
23679 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023680 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023681 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23682};
23683static const char *arch_reg_str(int reg)
23684{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023685 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23686 reg = 0;
23687 }
Eric Biederman90089602004-05-28 14:11:54 +000023688 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023689}
23690
23691static const char *reg(struct compile_state *state, struct triple *triple,
23692 int classes)
23693{
23694 int reg;
23695 reg = check_reg(state, triple, classes);
23696 return arch_reg_str(reg);
23697}
23698
Eric Biederman90089602004-05-28 14:11:54 +000023699static int arch_reg_size(int reg)
23700{
23701 int size;
23702 size = 0;
23703 if (reg == REG_EFLAGS) {
23704 size = 32;
23705 }
23706 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23707 size = 8;
23708 }
23709 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23710 size = 16;
23711 }
23712 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23713 size = 32;
23714 }
23715 else if (reg == REG_EDXEAX) {
23716 size = 64;
23717 }
23718 else if (reg == REG_DXAX) {
23719 size = 32;
23720 }
23721 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23722 size = 64;
23723 }
23724 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23725 size = 128;
23726 }
23727 return size;
23728}
23729
23730static int reg_size(struct compile_state *state, struct triple *ins)
23731{
23732 int reg;
23733 reg = ID_REG(ins->id);
23734 if (reg == REG_UNSET) {
23735 internal_error(state, ins, "register not set");
23736 }
23737 return arch_reg_size(reg);
23738}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023739
Eric Biederman90089602004-05-28 14:11:54 +000023740
23741
Eric Biedermanb138ac82003-04-22 18:44:01 +000023742const char *type_suffix(struct compile_state *state, struct type *type)
23743{
23744 const char *suffix;
23745 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023746 case SIZEOF_I8: suffix = "b"; break;
23747 case SIZEOF_I16: suffix = "w"; break;
23748 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023749 default:
23750 internal_error(state, 0, "unknown suffix");
23751 suffix = 0;
23752 break;
23753 }
23754 return suffix;
23755}
23756
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023757static void print_const_val(
23758 struct compile_state *state, struct triple *ins, FILE *fp)
23759{
23760 switch(ins->op) {
23761 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023762 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023763 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023764 break;
23765 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023766 if ((MISC(ins, 0)->op != OP_SDECL) &&
23767 (MISC(ins, 0)->op != OP_LABEL))
23768 {
Eric Biederman830c9882003-07-04 00:27:33 +000023769 internal_error(state, ins, "bad base for addrconst");
23770 }
23771 if (MISC(ins, 0)->u.cval <= 0) {
23772 internal_error(state, ins, "unlabeled constant");
23773 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023774 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023775 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023776 (unsigned long)(MISC(ins, 0)->u.cval),
23777 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023778 break;
23779 default:
23780 internal_error(state, ins, "unknown constant type");
23781 break;
23782 }
23783}
23784
Eric Biederman530b5192003-07-01 10:05:30 +000023785static void print_const(struct compile_state *state,
23786 struct triple *ins, FILE *fp)
23787{
23788 switch(ins->op) {
23789 case OP_INTCONST:
23790 switch(ins->type->type & TYPE_MASK) {
23791 case TYPE_CHAR:
23792 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023793 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023794 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023795 break;
23796 case TYPE_SHORT:
23797 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023798 fprintf(fp, ".short 0x%04lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023799 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023800 break;
23801 case TYPE_INT:
23802 case TYPE_UINT:
23803 case TYPE_LONG:
23804 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023805 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023806 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023807 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023808 break;
23809 default:
Eric Biederman90089602004-05-28 14:11:54 +000023810 fprintf(state->errout, "type: ");
23811 name_of(state->errout, ins->type);
23812 fprintf(state->errout, "\n");
23813 internal_error(state, ins, "Unknown constant type. Val: %lu",
23814 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023815 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023816
Eric Biederman530b5192003-07-01 10:05:30 +000023817 break;
23818 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023819 if ((MISC(ins, 0)->op != OP_SDECL) &&
23820 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023821 internal_error(state, ins, "bad base for addrconst");
23822 }
23823 if (MISC(ins, 0)->u.cval <= 0) {
23824 internal_error(state, ins, "unlabeled constant");
23825 }
23826 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023827 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023828 (unsigned long)(MISC(ins, 0)->u.cval),
23829 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023830 break;
23831 case OP_BLOBCONST:
23832 {
23833 unsigned char *blob;
23834 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023835 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023836 blob = ins->u.blob;
23837 for(i = 0; i < size; i++) {
23838 fprintf(fp, ".byte 0x%02x\n",
23839 blob[i]);
23840 }
23841 break;
23842 }
23843 default:
23844 internal_error(state, ins, "Unknown constant type");
23845 break;
23846 }
23847}
23848
23849#define TEXT_SECTION ".rom.text"
23850#define DATA_SECTION ".rom.data"
23851
23852static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023853 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023854{
Eric Biederman90089602004-05-28 14:11:54 +000023855 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023856 long ref;
23857 ref = next_label(state);
23858 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023859 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023860 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023861 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023862 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23863 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023864 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023865 }
Eric Biederman530b5192003-07-01 10:05:30 +000023866 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23867 return ref;
23868}
23869
Eric Biederman90089602004-05-28 14:11:54 +000023870static long get_mask_pool_ref(
23871 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23872{
23873 long ref;
23874 if (mask == 0xff) {
23875 ref = 1;
23876 }
23877 else if (mask == 0xffff) {
23878 ref = 2;
23879 }
23880 else {
23881 ref = 0;
23882 internal_error(state, ins, "unhandled mask value");
23883 }
23884 return ref;
23885}
23886
Eric Biedermanb138ac82003-04-22 18:44:01 +000023887static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023888 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023889{
23890 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023891 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023892 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023893 internal_error(state, ins, "invalid register assignment");
23894 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023895 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023896 fprintf(fp, "\t%s ", op);
23897 print_const_val(state, RHS(ins, 1), fp);
23898 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023899 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023900 }
23901 else {
23902 unsigned lmask, rmask;
23903 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023904 lreg = check_reg(state, RHS(ins, 0), mask);
23905 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023906 lmask = arch_reg_regcm(state, lreg);
23907 rmask = arch_reg_regcm(state, rreg);
23908 mask = lmask & rmask;
23909 fprintf(fp, "\t%s %s, %s\n",
23910 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023911 reg(state, RHS(ins, 1), mask),
23912 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023913 }
23914}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023915static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023916 const char *op, struct triple *ins, FILE *fp)
23917{
23918 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023919 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023920 fprintf(fp, "\t%s %s\n",
23921 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023922 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023923}
23924
23925static void print_op_shift(struct compile_state *state,
23926 const char *op, struct triple *ins, FILE *fp)
23927{
23928 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023929 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023930 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023931 internal_error(state, ins, "invalid register assignment");
23932 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023933 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023934 fprintf(fp, "\t%s ", op);
23935 print_const_val(state, RHS(ins, 1), fp);
23936 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023937 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023938 }
23939 else {
23940 fprintf(fp, "\t%s %s, %s\n",
23941 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023942 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023943 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023944 }
23945}
23946
23947static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23948{
23949 const char *op;
23950 int mask;
23951 int dreg;
23952 mask = 0;
23953 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023954 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023955 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23956 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23957 default:
23958 internal_error(state, ins, "not an in operation");
23959 op = 0;
23960 break;
23961 }
23962 dreg = check_reg(state, ins, mask);
23963 if (!reg_is_reg(state, dreg, REG_EAX)) {
23964 internal_error(state, ins, "dst != %%eax");
23965 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023966 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023967 fprintf(fp, "\t%s ", op);
23968 print_const_val(state, RHS(ins, 0), fp);
23969 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023970 reg(state, ins, mask));
23971 }
23972 else {
23973 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023974 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023975 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23976 internal_error(state, ins, "src != %%dx");
23977 }
23978 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023979 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023980 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023981 reg(state, ins, mask));
23982 }
23983}
23984
23985static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23986{
23987 const char *op;
23988 int mask;
23989 int lreg;
23990 mask = 0;
23991 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023992 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023993 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23994 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23995 default:
23996 internal_error(state, ins, "not an out operation");
23997 op = 0;
23998 break;
23999 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024000 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024001 if (!reg_is_reg(state, lreg, REG_EAX)) {
24002 internal_error(state, ins, "src != %%eax");
24003 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024004 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024005 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024006 op, reg(state, RHS(ins, 0), mask));
24007 print_const_val(state, RHS(ins, 1), fp);
24008 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024009 }
24010 else {
24011 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024012 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024013 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24014 internal_error(state, ins, "dst != %%dx");
24015 }
24016 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024017 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024018 reg(state, RHS(ins, 0), mask),
24019 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024020 }
24021}
24022
24023static void print_op_move(struct compile_state *state,
24024 struct triple *ins, FILE *fp)
24025{
24026 /* op_move is complex because there are many types
24027 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024028 * Because OP_COPY will be introduced in arbitrary locations
24029 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024030 * OP_CONVERT can change the flags and it is the only operation
24031 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024032 */
24033 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24034 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024035 if (state->arch->features & X86_NOOP_COPY) {
24036 omit_copy = 0;
24037 }
24038 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024039 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024040 dst = ins;
24041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024042 else {
24043 internal_error(state, ins, "unknown move operation");
24044 src = dst = 0;
24045 }
Eric Biederman90089602004-05-28 14:11:54 +000024046 if (reg_size(state, dst) < size_of(state, dst->type)) {
24047 internal_error(state, ins, "Invalid destination register");
24048 }
24049 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24050 fprintf(state->errout, "src type: ");
24051 name_of(state->errout, src->type);
24052 fprintf(state->errout, "\n");
24053 fprintf(state->errout, "dst type: ");
24054 name_of(state->errout, dst->type);
24055 fprintf(state->errout, "\n");
24056 internal_error(state, ins, "Type mismatch for OP_COPY");
24057 }
24058
Eric Biederman0babc1c2003-05-09 02:39:00 +000024059 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024060 int src_reg, dst_reg;
24061 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024062 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024063 dst_reg = ID_REG(dst->id);
24064 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024065 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024066 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024067 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024068 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024069 if ((src_reg != dst_reg) || !omit_copy) {
24070 fprintf(fp, "\tmov %s, %s\n",
24071 reg(state, src, src_regcm),
24072 reg(state, dst, dst_regcm));
24073 }
24074 }
24075 /* Move 32bit to 16bit */
24076 else if ((src_regcm & REGCM_GPR32) &&
24077 (dst_regcm & REGCM_GPR16)) {
24078 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24079 if ((src_reg != dst_reg) || !omit_copy) {
24080 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024081 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024082 arch_reg_str(dst_reg));
24083 }
24084 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024085 /* Move from 32bit gprs to 16bit gprs */
24086 else if ((src_regcm & REGCM_GPR32) &&
24087 (dst_regcm & REGCM_GPR16)) {
24088 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24089 if ((src_reg != dst_reg) || !omit_copy) {
24090 fprintf(fp, "\tmov %s, %s\n",
24091 arch_reg_str(src_reg),
24092 arch_reg_str(dst_reg));
24093 }
24094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024095 /* Move 32bit to 8bit */
24096 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024097 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024098 {
24099 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24100 if ((src_reg != dst_reg) || !omit_copy) {
24101 fprintf(fp, "\tmovb %s, %s\n",
24102 arch_reg_str(src_reg),
24103 arch_reg_str(dst_reg));
24104 }
24105 }
24106 /* Move 16bit to 8bit */
24107 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024108 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024109 {
24110 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24111 if ((src_reg != dst_reg) || !omit_copy) {
24112 fprintf(fp, "\tmovb %s, %s\n",
24113 arch_reg_str(src_reg),
24114 arch_reg_str(dst_reg));
24115 }
24116 }
24117 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024118 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024119 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024120 const char *op;
24121 op = is_signed(src->type)? "movsx": "movzx";
24122 fprintf(fp, "\t%s %s, %s\n",
24123 op,
24124 reg(state, src, src_regcm),
24125 reg(state, dst, dst_regcm));
24126 }
24127 /* Move between sse registers */
24128 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24129 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024130 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024131 reg(state, src, src_regcm),
24132 reg(state, dst, dst_regcm));
24133 }
24134 }
Eric Biederman530b5192003-07-01 10:05:30 +000024135 /* Move between mmx registers */
24136 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024137 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024138 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024139 reg(state, src, src_regcm),
24140 reg(state, dst, dst_regcm));
24141 }
24142 }
Eric Biederman530b5192003-07-01 10:05:30 +000024143 /* Move from sse to mmx registers */
24144 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24145 fprintf(fp, "\tmovdq2q %s, %s\n",
24146 reg(state, src, src_regcm),
24147 reg(state, dst, dst_regcm));
24148 }
24149 /* Move from mmx to sse registers */
24150 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24151 fprintf(fp, "\tmovq2dq %s, %s\n",
24152 reg(state, src, src_regcm),
24153 reg(state, dst, dst_regcm));
24154 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024155 /* Move between 32bit gprs & mmx/sse registers */
24156 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24157 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24158 fprintf(fp, "\tmovd %s, %s\n",
24159 reg(state, src, src_regcm),
24160 reg(state, dst, dst_regcm));
24161 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024162 /* Move from 16bit gprs & mmx/sse registers */
24163 else if ((src_regcm & REGCM_GPR16) &&
24164 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24165 const char *op;
24166 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024167 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024168 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24169 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24170 op,
24171 arch_reg_str(src_reg),
24172 arch_reg_str(mid_reg),
24173 arch_reg_str(mid_reg),
24174 arch_reg_str(dst_reg));
24175 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024176 /* Move from mmx/sse registers to 16bit gprs */
24177 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24178 (dst_regcm & REGCM_GPR16)) {
24179 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24180 fprintf(fp, "\tmovd %s, %s\n",
24181 arch_reg_str(src_reg),
24182 arch_reg_str(dst_reg));
24183 }
Eric Biederman530b5192003-07-01 10:05:30 +000024184 /* Move from gpr to 64bit dividend */
24185 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24186 (dst_regcm & REGCM_DIVIDEND64)) {
24187 const char *extend;
24188 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24189 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024190 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024191 extend);
24192 }
24193 /* Move from 64bit gpr to gpr */
24194 else if ((src_regcm & REGCM_DIVIDEND64) &&
24195 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24196 if (dst_regcm & REGCM_GPR32) {
24197 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024198 }
Eric Biederman530b5192003-07-01 10:05:30 +000024199 else if (dst_regcm & REGCM_GPR16) {
24200 src_reg = REG_AX;
24201 }
24202 else if (dst_regcm & REGCM_GPR8_LO) {
24203 src_reg = REG_AL;
24204 }
24205 fprintf(fp, "\tmov %s, %s\n",
24206 arch_reg_str(src_reg),
24207 arch_reg_str(dst_reg));
24208 }
24209 /* Move from mmx/sse registers to 64bit gpr */
24210 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24211 (dst_regcm & REGCM_DIVIDEND64)) {
24212 const char *extend;
24213 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24214 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24215 arch_reg_str(src_reg),
24216 extend);
24217 }
24218 /* Move from 64bit gpr to mmx/sse register */
24219 else if ((src_regcm & REGCM_DIVIDEND64) &&
24220 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24221 fprintf(fp, "\tmovd %%eax, %s\n",
24222 arch_reg_str(dst_reg));
24223 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024224#if X86_4_8BIT_GPRS
24225 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024226 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024227 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24228 const char *op;
24229 int mid_reg;
24230 op = is_signed(src->type)? "movsx":"movzx";
24231 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24232 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24233 op,
24234 reg(state, src, src_regcm),
24235 arch_reg_str(mid_reg),
24236 arch_reg_str(mid_reg),
24237 reg(state, dst, dst_regcm));
24238 }
24239 /* Move from mmx/sse registers and 8bit gprs */
24240 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024241 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024242 int mid_reg;
24243 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24244 fprintf(fp, "\tmovd %s, %s\n",
24245 reg(state, src, src_regcm),
24246 arch_reg_str(mid_reg));
24247 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024248 /* Move from 32bit gprs to 8bit gprs */
24249 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024250 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024251 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24252 if ((src_reg != dst_reg) || !omit_copy) {
24253 fprintf(fp, "\tmov %s, %s\n",
24254 arch_reg_str(src_reg),
24255 arch_reg_str(dst_reg));
24256 }
24257 }
24258 /* Move from 16bit gprs to 8bit gprs */
24259 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024260 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024261 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24262 if ((src_reg != dst_reg) || !omit_copy) {
24263 fprintf(fp, "\tmov %s, %s\n",
24264 arch_reg_str(src_reg),
24265 arch_reg_str(dst_reg));
24266 }
24267 }
24268#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024269 /* Move from %eax:%edx to %eax:%edx */
24270 else if ((src_regcm & REGCM_DIVIDEND64) &&
24271 (dst_regcm & REGCM_DIVIDEND64) &&
24272 (src_reg == dst_reg)) {
24273 if (!omit_copy) {
24274 fprintf(fp, "\t/*mov %s, %s*/\n",
24275 arch_reg_str(src_reg),
24276 arch_reg_str(dst_reg));
24277 }
24278 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024279 else {
Eric Biederman90089602004-05-28 14:11:54 +000024280 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24281 internal_error(state, ins, "attempt to copy from %%eflags!");
24282 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024283 internal_error(state, ins, "unknown copy type");
24284 }
24285 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024286 else {
Eric Biederman90089602004-05-28 14:11:54 +000024287 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024288 int dst_reg;
24289 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024290 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024291 dst_reg = ID_REG(dst->id);
24292 dst_regcm = arch_reg_regcm(state, dst_reg);
24293 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24294 fprintf(fp, "\tmov ");
24295 print_const_val(state, src, fp);
24296 fprintf(fp, ", %s\n",
24297 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24298 }
24299 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024300 if (dst_size > SIZEOF_I32) {
24301 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024302 }
24303 fprintf(fp, "\tmov $0, %%edx\n");
24304 fprintf(fp, "\tmov ");
24305 print_const_val(state, src, fp);
24306 fprintf(fp, ", %%eax\n");
24307 }
24308 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024309 if (dst_size > SIZEOF_I16) {
24310 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024311 }
24312 fprintf(fp, "\tmov $0, %%dx\n");
24313 fprintf(fp, "\tmov ");
24314 print_const_val(state, src, fp);
24315 fprintf(fp, ", %%ax");
24316 }
24317 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24318 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024319 if (dst_size > SIZEOF_I32) {
24320 internal_error(state, ins, "%d bit constant...", dst_size);
24321 }
24322 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024323 fprintf(fp, "\tmovd L%s%lu, %s\n",
24324 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024325 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24326 }
24327 else {
24328 internal_error(state, ins, "unknown copy immediate type");
24329 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024330 }
Eric Biederman90089602004-05-28 14:11:54 +000024331 /* Leave now if this is not a type conversion */
24332 if (ins->op != OP_CONVERT) {
24333 return;
24334 }
24335 /* Now make certain I have not logically overflowed the destination */
24336 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24337 (size_of(state, dst->type) < reg_size(state, dst)))
24338 {
24339 unsigned long mask;
24340 int dst_reg;
24341 int dst_regcm;
24342 if (size_of(state, dst->type) >= 32) {
24343 fprintf(state->errout, "dst type: ");
24344 name_of(state->errout, dst->type);
24345 fprintf(state->errout, "\n");
24346 internal_error(state, dst, "unhandled dst type size");
24347 }
24348 mask = 1;
24349 mask <<= size_of(state, dst->type);
24350 mask -= 1;
24351
24352 dst_reg = ID_REG(dst->id);
24353 dst_regcm = arch_reg_regcm(state, dst_reg);
24354
24355 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24356 fprintf(fp, "\tand $0x%lx, %s\n",
24357 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24358 }
24359 else if (dst_regcm & REGCM_MMX) {
24360 long ref;
24361 ref = get_mask_pool_ref(state, dst, mask, fp);
24362 fprintf(fp, "\tpand L%s%lu, %s\n",
24363 state->compiler->label_prefix, ref,
24364 reg(state, dst, REGCM_MMX));
24365 }
24366 else if (dst_regcm & REGCM_XMM) {
24367 long ref;
24368 ref = get_mask_pool_ref(state, dst, mask, fp);
24369 fprintf(fp, "\tpand L%s%lu, %s\n",
24370 state->compiler->label_prefix, ref,
24371 reg(state, dst, REGCM_XMM));
24372 }
24373 else {
24374 fprintf(state->errout, "dst type: ");
24375 name_of(state->errout, dst->type);
24376 fprintf(state->errout, "\n");
24377 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24378 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24379 }
24380 }
24381 /* Make certain I am properly sign extended */
24382 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24383 (is_signed(src->type)))
24384 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024385 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024386 int dst_reg;
24387 int dst_regcm;
24388
Eric Biederman90089602004-05-28 14:11:54 +000024389 reg_bits = reg_size(state, dst);
24390 if (reg_bits > 32) {
24391 reg_bits = 32;
24392 }
24393 shift_bits = reg_bits - size_of(state, src->type);
24394 dst_reg = ID_REG(dst->id);
24395 dst_regcm = arch_reg_regcm(state, dst_reg);
24396
24397 if (shift_bits < 0) {
24398 internal_error(state, dst, "negative shift?");
24399 }
24400
24401 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024402 fprintf(fp, "\tshl $%d, %s\n",
24403 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024404 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024405 fprintf(fp, "\tsar $%d, %s\n",
24406 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024407 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24408 }
24409 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24410 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024411 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024412 reg(state, dst, REGCM_MMX | REGCM_XMM));
24413 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024414 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024415 reg(state, dst, REGCM_MMX | REGCM_XMM));
24416 }
24417 else {
24418 fprintf(state->errout, "dst type: ");
24419 name_of(state->errout, dst->type);
24420 fprintf(state->errout, "\n");
24421 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24422 internal_error(state, dst, "failed to signed extend value");
24423 }
24424 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024425}
24426
24427static void print_op_load(struct compile_state *state,
24428 struct triple *ins, FILE *fp)
24429{
24430 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024431 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024432 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024433 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024434 if (is_const(src) || is_const(dst)) {
24435 internal_error(state, ins, "unknown load operation");
24436 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024437 switch(ins->type->type & TYPE_MASK) {
24438 case TYPE_CHAR: op = "movsbl"; break;
24439 case TYPE_UCHAR: op = "movzbl"; break;
24440 case TYPE_SHORT: op = "movswl"; break;
24441 case TYPE_USHORT: op = "movzwl"; break;
24442 case TYPE_INT: case TYPE_UINT:
24443 case TYPE_LONG: case TYPE_ULONG:
24444 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024445 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024446 break;
24447 default:
24448 internal_error(state, ins, "unknown type in load");
24449 op = "<invalid opcode>";
24450 break;
24451 }
24452 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024453 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024454 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024455 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024456}
24457
24458
24459static void print_op_store(struct compile_state *state,
24460 struct triple *ins, FILE *fp)
24461{
24462 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024463 dst = RHS(ins, 0);
24464 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024465 if (is_const(src) && (src->op == OP_INTCONST)) {
24466 long_t value;
24467 value = (long_t)(src->u.cval);
24468 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24469 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024470 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024471 reg(state, dst, REGCM_GPR32));
24472 }
24473 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24474 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24475 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024476 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024477 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024478 }
24479 else {
24480 if (is_const(src) || is_const(dst)) {
24481 internal_error(state, ins, "unknown store operation");
24482 }
24483 fprintf(fp, "\tmov%s %s, (%s)\n",
24484 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024485 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024486 reg(state, dst, REGCM_GPR32));
24487 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024488
24489
Eric Biedermanb138ac82003-04-22 18:44:01 +000024490}
24491
24492static void print_op_smul(struct compile_state *state,
24493 struct triple *ins, FILE *fp)
24494{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024495 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024496 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024497 reg(state, RHS(ins, 1), REGCM_GPR32),
24498 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024499 }
24500 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024501 fprintf(fp, "\timul ");
24502 print_const_val(state, RHS(ins, 1), fp);
24503 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024504 }
24505}
24506
24507static void print_op_cmp(struct compile_state *state,
24508 struct triple *ins, FILE *fp)
24509{
24510 unsigned mask;
24511 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024512 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024513 dreg = check_reg(state, ins, REGCM_FLAGS);
24514 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24515 internal_error(state, ins, "bad dest register for cmp");
24516 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024517 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024518 fprintf(fp, "\tcmp ");
24519 print_const_val(state, RHS(ins, 1), fp);
24520 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024521 }
24522 else {
24523 unsigned lmask, rmask;
24524 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024525 lreg = check_reg(state, RHS(ins, 0), mask);
24526 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024527 lmask = arch_reg_regcm(state, lreg);
24528 rmask = arch_reg_regcm(state, rreg);
24529 mask = lmask & rmask;
24530 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024531 reg(state, RHS(ins, 1), mask),
24532 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024533 }
24534}
24535
24536static void print_op_test(struct compile_state *state,
24537 struct triple *ins, FILE *fp)
24538{
24539 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024540 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024541 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024542 reg(state, RHS(ins, 0), mask),
24543 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024544}
24545
24546static void print_op_branch(struct compile_state *state,
24547 struct triple *branch, FILE *fp)
24548{
24549 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024550 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024551 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024552 internal_error(state, branch, "jmp with condition?");
24553 }
24554 bop = "jmp";
24555 }
24556 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024557 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024558 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024559 internal_error(state, branch, "jmpcc without condition?");
24560 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024561 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24562 if ((RHS(branch, 0)->op != OP_CMP) &&
24563 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024564 internal_error(state, branch, "bad branch test");
24565 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024566#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024567#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024568#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024569 ptr = RHS(branch, 0);
24570 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24571 if (ptr->op != OP_COPY) {
24572 internal_error(state, branch, "branch does not follow test");
24573 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024574 }
24575 switch(branch->op) {
24576 case OP_JMP_EQ: bop = "jz"; break;
24577 case OP_JMP_NOTEQ: bop = "jnz"; break;
24578 case OP_JMP_SLESS: bop = "jl"; break;
24579 case OP_JMP_ULESS: bop = "jb"; break;
24580 case OP_JMP_SMORE: bop = "jg"; break;
24581 case OP_JMP_UMORE: bop = "ja"; break;
24582 case OP_JMP_SLESSEQ: bop = "jle"; break;
24583 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24584 case OP_JMP_SMOREEQ: bop = "jge"; break;
24585 case OP_JMP_UMOREEQ: bop = "jae"; break;
24586 default:
24587 internal_error(state, branch, "Invalid branch op");
24588 break;
24589 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024590
Eric Biedermanb138ac82003-04-22 18:44:01 +000024591 }
Eric Biederman90089602004-05-28 14:11:54 +000024592#if 1
24593 if (branch->op == OP_CALL) {
24594 fprintf(fp, "\t/* call */\n");
24595 }
24596#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024597 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024598 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024599 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024600 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024601}
24602
Eric Biederman5ade04a2003-10-22 04:03:46 +000024603static void print_op_ret(struct compile_state *state,
24604 struct triple *branch, FILE *fp)
24605{
24606 fprintf(fp, "\tjmp *%s\n",
24607 reg(state, RHS(branch, 0), REGCM_GPR32));
24608}
24609
Eric Biedermanb138ac82003-04-22 18:44:01 +000024610static void print_op_set(struct compile_state *state,
24611 struct triple *set, FILE *fp)
24612{
24613 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024614 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024615 internal_error(state, set, "setcc without condition?");
24616 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024617 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24618 if ((RHS(set, 0)->op != OP_CMP) &&
24619 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024620 internal_error(state, set, "bad set test");
24621 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024622 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024623 internal_error(state, set, "set does not follow test");
24624 }
24625 switch(set->op) {
24626 case OP_SET_EQ: sop = "setz"; break;
24627 case OP_SET_NOTEQ: sop = "setnz"; break;
24628 case OP_SET_SLESS: sop = "setl"; break;
24629 case OP_SET_ULESS: sop = "setb"; break;
24630 case OP_SET_SMORE: sop = "setg"; break;
24631 case OP_SET_UMORE: sop = "seta"; break;
24632 case OP_SET_SLESSEQ: sop = "setle"; break;
24633 case OP_SET_ULESSEQ: sop = "setbe"; break;
24634 case OP_SET_SMOREEQ: sop = "setge"; break;
24635 case OP_SET_UMOREEQ: sop = "setae"; break;
24636 default:
24637 internal_error(state, set, "Invalid set op");
24638 break;
24639 }
24640 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024641 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024642}
24643
Stefan Reinauer14e22772010-04-27 06:56:47 +000024644static void print_op_bit_scan(struct compile_state *state,
24645 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024646{
24647 const char *op;
24648 switch(ins->op) {
24649 case OP_BSF: op = "bsf"; break;
24650 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024651 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024652 internal_error(state, ins, "unknown bit scan");
24653 op = 0;
24654 break;
24655 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024656 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024657 "\t%s %s, %s\n"
24658 "\tjnz 1f\n"
24659 "\tmovl $-1, %s\n"
24660 "1:\n",
24661 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024662 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024663 reg(state, ins, REGCM_GPR32),
24664 reg(state, ins, REGCM_GPR32));
24665}
24666
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024667
Eric Biedermanb138ac82003-04-22 18:44:01 +000024668static void print_sdecl(struct compile_state *state,
24669 struct triple *ins, FILE *fp)
24670{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024671 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024672 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024673 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024674 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024675 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024676 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024677
Eric Biedermanb138ac82003-04-22 18:44:01 +000024678}
24679
24680static void print_instruction(struct compile_state *state,
24681 struct triple *ins, FILE *fp)
24682{
24683 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024684 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024685 */
24686 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024687 case OP_ASM:
24688 print_op_asm(state, ins, fp);
24689 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024690 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24691 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24692 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24693 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24694 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24695 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24696 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24697 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24698 case OP_POS: break;
24699 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24700 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024701 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024702 case OP_INTCONST:
24703 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024704 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024705 /* Don't generate anything here for constants */
24706 case OP_PHI:
24707 /* Don't generate anything for variable declarations. */
24708 break;
Eric Biederman90089602004-05-28 14:11:54 +000024709 case OP_UNKNOWNVAL:
24710 fprintf(fp, " /* unknown %s */\n",
24711 reg(state, ins, REGCM_ALL));
24712 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024713 case OP_SDECL:
24714 print_sdecl(state, ins, fp);
24715 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024716 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024717 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024718 print_op_move(state, ins, fp);
24719 break;
24720 case OP_LOAD:
24721 print_op_load(state, ins, fp);
24722 break;
24723 case OP_STORE:
24724 print_op_store(state, ins, fp);
24725 break;
24726 case OP_SMUL:
24727 print_op_smul(state, ins, fp);
24728 break;
24729 case OP_CMP: print_op_cmp(state, ins, fp); break;
24730 case OP_TEST: print_op_test(state, ins, fp); break;
24731 case OP_JMP:
24732 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24733 case OP_JMP_SLESS: case OP_JMP_ULESS:
24734 case OP_JMP_SMORE: case OP_JMP_UMORE:
24735 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24736 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024737 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024738 print_op_branch(state, ins, fp);
24739 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024740 case OP_RET:
24741 print_op_ret(state, ins, fp);
24742 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024743 case OP_SET_EQ: case OP_SET_NOTEQ:
24744 case OP_SET_SLESS: case OP_SET_ULESS:
24745 case OP_SET_SMORE: case OP_SET_UMORE:
24746 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24747 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24748 print_op_set(state, ins, fp);
24749 break;
24750 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024751 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024752 break;
24753 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024754 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024755 break;
24756 case OP_BSF:
24757 case OP_BSR:
24758 print_op_bit_scan(state, ins, fp);
24759 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024760 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024761 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024762 fprintf(fp, "\trdmsr\n");
24763 break;
24764 case OP_WRMSR:
24765 fprintf(fp, "\twrmsr\n");
24766 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024767 case OP_HLT:
24768 fprintf(fp, "\thlt\n");
24769 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024770 case OP_SDIVT:
24771 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24772 break;
24773 case OP_UDIVT:
24774 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24775 break;
24776 case OP_UMUL:
24777 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24778 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024779 case OP_LABEL:
24780 if (!ins->use) {
24781 return;
24782 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024783 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024784 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024785 break;
Eric Biederman90089602004-05-28 14:11:54 +000024786 case OP_ADECL:
24787 /* Ignore adecls with no registers error otherwise */
24788 if (!noop_adecl(ins)) {
24789 internal_error(state, ins, "adecl remains?");
24790 }
24791 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024792 /* Ignore OP_PIECE */
24793 case OP_PIECE:
24794 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024795 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024796 case OP_SDIV: case OP_UDIV:
24797 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024798 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24799 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24800 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24801 default:
24802 internal_error(state, ins, "unknown op: %d %s",
24803 ins->op, tops(ins->op));
24804 break;
24805 }
24806}
24807
24808static void print_instructions(struct compile_state *state)
24809{
24810 struct triple *first, *ins;
24811 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024812 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024813 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024814 int max_inline_depth;
24815 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024816 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024817 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024818 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024819 /* Masks for common sizes */
24820 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24821 fprintf(fp, ".balign 16\n");
24822 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24823 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24824 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24825 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024826 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024827 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024828 ins = first;
24829 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024830 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024831 last_occurance != ins->occurance) {
24832 if (!ins->occurance->parent) {
24833 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024834 ins->occurance->function?ins->occurance->function:"(null)",
24835 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024836 ins->occurance->line,
24837 ins->occurance->col);
24838 }
24839 else {
24840 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024841 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024842 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024843 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024844 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024845 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024846 fprintf(fp, "\t * %s,%s:%d.%d\n",
24847 ptr->function,
24848 ptr->filename,
24849 ptr->line,
24850 ptr->col);
24851 }
24852 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024853 if (inline_depth > max_inline_depth) {
24854 max_inline_depth = inline_depth;
24855 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024856 }
24857 if (last_occurance) {
24858 put_occurance(last_occurance);
24859 }
24860 get_occurance(ins->occurance);
24861 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024862 }
24863
24864 print_instruction(state, ins, fp);
24865 ins = ins->next;
24866 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024867 if (print_location) {
24868 fprintf(fp, "/* max inline depth %d */\n",
24869 max_inline_depth);
24870 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024871}
Eric Biederman530b5192003-07-01 10:05:30 +000024872
Eric Biedermanb138ac82003-04-22 18:44:01 +000024873static void generate_code(struct compile_state *state)
24874{
24875 generate_local_labels(state);
24876 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024877
Eric Biedermanb138ac82003-04-22 18:44:01 +000024878}
24879
Eric Biederman90089602004-05-28 14:11:54 +000024880static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024881{
Eric Biederman90089602004-05-28 14:11:54 +000024882 int tok;
24883 FILE *fp;
24884 int line;
24885 const char *filename;
24886 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024887 filename = 0;
24888 line = 0;
24889 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024890 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024891 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024892 const char *token_str;
24893 tok = peek(state);
24894 if (tok == TOK_EOF) {
24895 break;
24896 }
Eric Biederman41203d92004-11-08 09:31:09 +000024897 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024898 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024899 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024900 tk->str_len ? tk->val.str :
24901 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024902
24903 file = state->file;
24904 while(file->macro && file->prev) {
24905 file = file->prev;
24906 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024907 if (!file->macro &&
24908 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024909 {
Eric Biederman90089602004-05-28 14:11:54 +000024910 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024911 if ((file->basename == filename) &&
24912 (line < file->line)) {
24913 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024914 fprintf(fp, "\n");
24915 line++;
24916 }
24917 }
24918 else {
24919 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024920 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024921 }
Eric Biederman132368b2004-11-09 08:59:23 +000024922 line = file->line;
24923 filename = file->basename;
24924 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024925 for(i = 0; i < col; i++) {
24926 fprintf(fp, " ");
24927 }
24928 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024929
Eric Biederman90089602004-05-28 14:11:54 +000024930 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024931
Eric Biederman90089602004-05-28 14:11:54 +000024932 if (state->compiler->debug & DEBUG_TOKENS) {
24933 loc(state->dbgout, state, 0);
24934 fprintf(state->dbgout, "%s <- `%s'\n",
24935 tokens[tok], token_str);
24936 }
24937 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024938}
24939
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024940static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024941 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024942{
24943 int i;
24944 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024945 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024946 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024947 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024948 state.compiler = compiler;
24949 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024950 state.file = 0;
24951 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24952 memset(&state.token[i], 0, sizeof(state.token[i]));
24953 state.token[i].tok = -1;
24954 }
Eric Biederman90089602004-05-28 14:11:54 +000024955 /* Remember the output descriptors */
24956 state.errout = stderr;
24957 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024958 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024959 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24960 state.output = stdout;
24961 } else {
24962 state.output = fopen(state.compiler->ofilename, "w");
24963 if (!state.output) {
24964 error(&state, 0, "Cannot open output file %s\n",
24965 state.compiler->ofilename);
24966 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024967 }
Eric Biederman90089602004-05-28 14:11:54 +000024968 /* Make certain a good cleanup happens */
24969 exit_state = &state;
24970 atexit(exit_cleanup);
24971
Eric Biedermanb138ac82003-04-22 18:44:01 +000024972 /* Prep the preprocessor */
24973 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024974 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024975 /* register the C keywords */
24976 register_keywords(&state);
24977 /* register the keywords the macro preprocessor knows */
24978 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024979 /* generate some builtin macros */
24980 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024981 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024982 state.i_switch = lookup(&state, "switch", 6);
24983 state.i_case = lookup(&state, "case", 4);
24984 state.i_continue = lookup(&state, "continue", 8);
24985 state.i_break = lookup(&state, "break", 5);
24986 state.i_default = lookup(&state, "default", 7);
24987 state.i_return = lookup(&state, "return", 6);
24988 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024989 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24990 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24991 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24992 /* Memorize where predefined identifiers are. */
24993 state.i___func__ = lookup(&state, "__func__", 8);
24994 /* Memorize where some attribute keywords are. */
24995 state.i_noinline = lookup(&state, "noinline", 8);
24996 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024997 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020024998 state.i_unused = lookup(&state, "unused", 6);
Stefan Reinauerec664bc2013-05-09 14:06:04 -070024999 state.i_packed = lookup(&state, "packed", 6);
Eric Biederman90089602004-05-28 14:11:54 +000025000
25001 /* Process the command line macros */
25002 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000025003
25004 /* Allocate beginning bounding labels for the function list */
25005 state.first = label(&state);
25006 state.first->id |= TRIPLE_FLAG_VOLATILE;
25007 use_triple(state.first, state.first);
25008 ptr = label(&state);
25009 ptr->id |= TRIPLE_FLAG_VOLATILE;
25010 use_triple(ptr, ptr);
25011 flatten(&state, state.first, ptr);
25012
Eric Biederman5ade04a2003-10-22 04:03:46 +000025013 /* Allocate a label for the pool of global variables */
25014 state.global_pool = label(&state);
25015 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25016 flatten(&state, state.first, state.global_pool);
25017
Eric Biedermanb138ac82003-04-22 18:44:01 +000025018 /* Enter the globl definition scope */
25019 start_scope(&state);
25020 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025021
Eric Biedermanb138ac82003-04-22 18:44:01 +000025022 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025023
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025024 while (includes) {
25025 compile_file(&state, includes->filename, 1);
25026 includes=includes->next;
25027 }
Eric Biederman90089602004-05-28 14:11:54 +000025028
25029 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025030 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025031 print_preprocessed_tokens(&state);
25032 return;
25033 }
25034
Eric Biedermanb138ac82003-04-22 18:44:01 +000025035 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025036
Eric Biedermanb138ac82003-04-22 18:44:01 +000025037 /* Exit the global definition scope */
25038 end_scope(&state);
25039
Stefan Reinauer14e22772010-04-27 06:56:47 +000025040 /* Now that basic compilation has happened
25041 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025042 */
25043 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025044
Eric Biedermanb138ac82003-04-22 18:44:01 +000025045 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025046 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025047 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025048 }
Eric Biederman90089602004-05-28 14:11:54 +000025049 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025050}
25051
Eric Biederman90089602004-05-28 14:11:54 +000025052static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025053{
Eric Biederman90089602004-05-28 14:11:54 +000025054 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025055}
25056
25057static void usage(void)
25058{
Eric Biederman90089602004-05-28 14:11:54 +000025059 FILE *fp = stdout;
25060 version(fp);
25061 fprintf(fp,
25062 "\nUsage: romcc [options] <source>.c\n"
25063 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25064 "Options: \n"
25065 "-o <output file name>\n"
25066 "-f<option> Specify a generic compiler option\n"
25067 "-m<option> Specify a arch dependent option\n"
25068 "-- Specify this is the last option\n"
25069 "\nGeneric compiler options:\n"
25070 );
25071 compiler_usage(fp);
25072 fprintf(fp,
25073 "\nArchitecture compiler options:\n"
25074 );
25075 arch_usage(fp);
25076 fprintf(fp,
25077 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025078 );
25079}
25080
25081static void arg_error(char *fmt, ...)
25082{
25083 va_list args;
25084 va_start(args, fmt);
25085 vfprintf(stderr, fmt, args);
25086 va_end(args);
25087 usage();
25088 exit(1);
25089}
25090
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -080025091static void arg_warning(char *fmt, ...)
25092{
25093 va_list args;
25094
25095 va_start(args, fmt);
25096 vfprintf(stderr, fmt, args);
25097 va_end(args);
25098}
25099
Eric Biedermanb138ac82003-04-22 18:44:01 +000025100int main(int argc, char **argv)
25101{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025102 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025103 struct compiler_state compiler;
25104 struct arch_state arch;
25105 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025106
25107
Eric Biederman90089602004-05-28 14:11:54 +000025108 /* I don't want any surprises */
25109 setlocale(LC_ALL, "C");
25110
Eric Biederman5ade04a2003-10-22 04:03:46 +000025111 init_compiler_state(&compiler);
25112 init_arch_state(&arch);
25113 filename = 0;
25114 all_opts = 0;
25115 while(argc > 1) {
25116 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25117 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025118 argv += 2;
25119 argc -= 2;
25120 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025121 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025122 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025123 result = -1;
25124 if (strcmp(argv[1], "--") == 0) {
25125 result = 0;
25126 all_opts = 1;
25127 }
Eric Biederman90089602004-05-28 14:11:54 +000025128 else if (strncmp(argv[1], "-E", 2) == 0) {
25129 result = compiler_encode_flag(&compiler, argv[1]);
25130 }
25131 else if (strncmp(argv[1], "-O", 2) == 0) {
25132 result = compiler_encode_flag(&compiler, argv[1]);
25133 }
25134 else if (strncmp(argv[1], "-I", 2) == 0) {
25135 result = compiler_encode_flag(&compiler, argv[1]);
25136 }
25137 else if (strncmp(argv[1], "-D", 2) == 0) {
25138 result = compiler_encode_flag(&compiler, argv[1]);
25139 }
25140 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025141 result = compiler_encode_flag(&compiler, argv[1]);
25142 }
25143 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25144 result = compiler_encode_flag(&compiler, argv[1]+2);
25145 }
25146 else if (strncmp(argv[1], "-f", 2) == 0) {
25147 result = compiler_encode_flag(&compiler, argv[1]+2);
25148 }
25149 else if (strncmp(argv[1], "-m", 2) == 0) {
25150 result = arch_encode_flag(&arch, argv[1]+2);
25151 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025152 else if (strncmp(argv[1], "-c", 2) == 0) {
25153 result = 0;
25154 }
25155 else if (strncmp(argv[1], "-S", 2) == 0) {
25156 result = 0;
25157 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025158 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025159 struct filelist *old_head = include_filelist;
25160 include_filelist = malloc(sizeof(struct filelist));
25161 if (!include_filelist) {
25162 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025163 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025164 argv++;
25165 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025166 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025167 include_filelist->next = old_head;
25168 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025169 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025170 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025171 arg_error("Invalid option specified: %s\n",
25172 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025173 }
25174 argv++;
25175 argc--;
25176 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025177 else {
25178 if (filename) {
25179 arg_error("Only one filename may be specified\n");
25180 }
25181 filename = argv[1];
25182 argv++;
25183 argc--;
25184 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025185 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025186 if (!filename) {
25187 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025188 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025189 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025190
25191 return 0;
25192}