blob: c97312cc44bc1260025681926d66e3a34f19ec91 [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:
4583 for(i = 0; i < macro->argc; i++) {
4584 if (argv[i].ident == tk->ident) {
4585 break;
4586 }
4587 }
4588 if (i >= macro->argc) {
4589 break;
4590 }
4591 /* Substitute macro parameter */
4592 fstart = argv[i].value;
4593 flen = argv[i].len;
4594 break;
4595 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004596 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004597 break;
4598 }
4599 do {
4600 raw_next_token(state, &fmacro, tk);
4601 } while(tk->tok == TOK_SPACE);
4602 check_tok(state, tk, TOK_IDENT);
4603 for(i = 0; i < macro->argc; i++) {
4604 if (argv[i].ident == tk->ident) {
4605 break;
4606 }
4607 }
4608 if (i >= macro->argc) {
4609 error(state, 0, "parameter `%s' not found",
4610 tk->ident->name);
4611 }
4612 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004613 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004614 for(j = 0; j < argv[i].len; j++) {
4615 char *str = argv[i].value + j;
4616 size_t len = 1;
4617 if (*str == '\\') {
4618 str = "\\";
4619 len = 2;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004620 }
Eric Biederman90089602004-05-28 14:11:54 +00004621 else if (*str == '"') {
4622 str = "\\\"";
4623 len = 2;
4624 }
Eric Biedermancb364952004-11-15 10:46:44 +00004625 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004626 }
Eric Biedermancb364952004-11-15 10:46:44 +00004627 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004628 fstart = 0;
4629 flen = 0;
4630 break;
4631 case TOK_CONCATENATE:
4632 /* Concatenate tokens */
4633 /* Delete the previous whitespace token */
4634 if (buf->str[buf->pos - 1] == ' ') {
4635 buf->pos -= 1;
4636 }
4637 /* Skip the next sequence of whitspace tokens */
4638 do {
4639 fstart = fmacro.pos;
4640 raw_next_token(state, &fmacro, tk);
4641 } while(tk->tok == TOK_SPACE);
4642 /* Restart at the top of the loop.
4643 * I need to process the non white space token.
4644 */
4645 continue;
4646 break;
4647 case TOK_SPACE:
4648 /* Collapse multiple spaces into one */
4649 if (buf->str[buf->pos - 1] != ' ') {
4650 fstart = space;
4651 flen = 1;
4652 } else {
4653 fstart = 0;
4654 flen = 0;
4655 }
4656 break;
4657 default:
4658 break;
4659 }
4660
Eric Biedermancb364952004-11-15 10:46:44 +00004661 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004662
Eric Biederman90089602004-05-28 14:11:54 +00004663 fstart = fmacro.pos;
4664 raw_next_token(state, &fmacro, tk);
4665 }
4666}
4667
4668static void tag_macro_name(struct compile_state *state,
4669 struct macro *macro, struct macro_buf *buf,
4670 struct token *tk)
4671{
4672 /* Guard all instances of the macro name in the replacement
4673 * text from further macro expansion.
4674 */
4675 struct file_state fmacro;
4676 const char *fstart;
4677 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004678
4679 /* Put the old macro expansion buffer in a file */
4680 fmacro.prev = 0;
4681 fmacro.basename = macro->ident->name;
4682 fmacro.dirname = "";
4683 fmacro.buf = buf->str;
4684 fmacro.size = buf->pos;
4685 fmacro.pos = fmacro.buf;
4686 fmacro.line = 1;
4687 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004688 fmacro.report_line = 1;
4689 fmacro.report_name = fmacro.basename;
4690 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004691 fmacro.macro = 1;
4692 fmacro.trigraphs = 0;
4693 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004694
Eric Biedermancb364952004-11-15 10:46:44 +00004695 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004696 buf->len = macro->buf_len + 3;
4697 buf->str = xmalloc(buf->len, macro->ident->name);
4698 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004699
Eric Biederman90089602004-05-28 14:11:54 +00004700 fstart = fmacro.pos;
4701 raw_next_token(state, &fmacro, tk);
4702 while(tk->tok != TOK_EOF) {
4703 flen = fmacro.pos - fstart;
4704 if ((tk->tok == TOK_IDENT) &&
4705 (tk->ident == macro->ident) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004706 (tk->val.notmacro == 0))
Eric Biedermancb364952004-11-15 10:46:44 +00004707 {
4708 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004709 fstart = "$";
4710 flen = 1;
4711 }
4712
Eric Biedermancb364952004-11-15 10:46:44 +00004713 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004714
Eric Biederman90089602004-05-28 14:11:54 +00004715 fstart = fmacro.pos;
4716 raw_next_token(state, &fmacro, tk);
4717 }
4718 xfree(fmacro.buf);
4719}
Eric Biedermancb364952004-11-15 10:46:44 +00004720
Stefan Reinauer14e22772010-04-27 06:56:47 +00004721static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004722 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004723{
4724 struct file_state *file;
4725 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004726 struct macro *macro;
4727 struct macro_arg_value *argv;
4728 struct macro_buf buf;
4729
4730#if 0
4731 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4732#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004733 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004734 macro = ident->sym_define;
4735
4736 /* If this token comes from a macro expansion ignore it */
4737 if (tk->val.notmacro) {
4738 return 0;
4739 }
4740 /* If I am a function like macro and the identifier is not followed
4741 * by a left parenthesis, do nothing.
4742 */
Eric Biedermancb364952004-11-15 10:46:44 +00004743 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004744 return 0;
4745 }
4746
4747 /* Read in the macro arguments */
4748 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004749 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004750 raw_next_token(state, *filep, tk);
4751 check_tok(state, tk, TOK_LPAREN);
4752
4753 argv = read_macro_args(state, macro, *filep, tk);
4754
4755 check_tok(state, tk, TOK_RPAREN);
4756 }
4757 /* Macro expand the macro arguments */
4758 macro_expand_args(state, macro, argv, tk);
4759
4760 buf.str = 0;
4761 buf.len = 0;
4762 buf.pos = 0;
4763 if (ident == state->i___FILE__) {
4764 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4765 buf.str = xmalloc(buf.len, ident->name);
4766 sprintf(buf.str, "\"%s\"", state->file->basename);
4767 buf.pos = strlen(buf.str);
4768 }
4769 else if (ident == state->i___LINE__) {
4770 buf.len = 30;
4771 buf.str = xmalloc(buf.len, ident->name);
4772 sprintf(buf.str, "%d", state->file->line);
4773 buf.pos = strlen(buf.str);
4774 }
4775 else {
4776 expand_macro(state, macro, &buf, argv, tk);
4777 }
4778 /* Tag the macro name with a $ so it will no longer
4779 * be regonized as a canidate for macro expansion.
4780 */
4781 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004782
4783#if 0
4784 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4785 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4786#endif
4787
4788 free_macro_args(macro, argv);
4789
Eric Biedermanb138ac82003-04-22 18:44:01 +00004790 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004791 file->prev = *filep;
4792 file->basename = xstrdup(ident->name);
4793 file->dirname = xstrdup("");
4794 file->buf = buf.str;
4795 file->size = buf.pos;
4796 file->pos = file->buf;
4797 file->line = 1;
4798 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004799 file->report_line = 1;
4800 file->report_name = file->basename;
4801 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004802 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004803 file->trigraphs = 0;
4804 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004805 *filep = file;
4806 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004807}
4808
Eric Biederman90089602004-05-28 14:11:54 +00004809static void eat_tokens(struct compile_state *state, int targ_tok)
4810{
4811 if (state->eat_depth > 0) {
4812 internal_error(state, 0, "Already eating...");
4813 }
4814 state->eat_depth = state->if_depth;
4815 state->eat_targ = targ_tok;
4816}
4817static int if_eat(struct compile_state *state)
4818{
4819 return state->eat_depth > 0;
4820}
4821static int if_value(struct compile_state *state)
4822{
4823 int index, offset;
4824 index = state->if_depth / CHAR_BIT;
4825 offset = state->if_depth % CHAR_BIT;
4826 return !!(state->if_bytes[index] & (1 << (offset)));
4827}
Stefan Reinauer14e22772010-04-27 06:56:47 +00004828static void set_if_value(struct compile_state *state, int value)
Eric Biederman90089602004-05-28 14:11:54 +00004829{
4830 int index, offset;
4831 index = state->if_depth / CHAR_BIT;
4832 offset = state->if_depth % CHAR_BIT;
4833
4834 state->if_bytes[index] &= ~(1 << offset);
4835 if (value) {
4836 state->if_bytes[index] |= (1 << offset);
4837 }
4838}
4839static void in_if(struct compile_state *state, const char *name)
4840{
4841 if (state->if_depth <= 0) {
4842 error(state, 0, "%s without #if", name);
4843 }
4844}
4845static void enter_if(struct compile_state *state)
4846{
4847 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004848 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004849 error(state, 0, "#if depth too great");
4850 }
4851}
4852static void reenter_if(struct compile_state *state, const char *name)
4853{
4854 in_if(state, name);
4855 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004856 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004857 state->eat_depth = 0;
4858 state->eat_targ = 0;
4859 }
4860}
4861static void enter_else(struct compile_state *state, const char *name)
4862{
4863 in_if(state, name);
4864 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004865 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004866 state->eat_depth = 0;
4867 state->eat_targ = 0;
4868 }
4869}
4870static void exit_if(struct compile_state *state, const char *name)
4871{
4872 in_if(state, name);
4873 if (state->eat_depth == state->if_depth) {
4874 state->eat_depth = 0;
4875 state->eat_targ = 0;
4876 }
4877 state->if_depth -= 1;
4878}
4879
Eric Biedermancb364952004-11-15 10:46:44 +00004880static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004881{
4882 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004883 int rescan;
4884
Eric Biedermancb364952004-11-15 10:46:44 +00004885 file = state->file;
4886 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004887 do {
4888 rescan = 0;
4889 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004890 /* Exit out of an include directive or macro call */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004891 if ((tk->tok == TOK_EOF) &&
4892 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004893 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004894 state->file = file->prev;
4895 /* file->basename is used keep it */
4896 xfree(file->dirname);
4897 xfree(file->buf);
4898 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004899 file = 0;
4900 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004901 rescan = 1;
4902 }
Eric Biederman41203d92004-11-08 09:31:09 +00004903 } while(rescan);
4904}
4905
Eric Biedermancb364952004-11-15 10:46:44 +00004906static void pp_token(struct compile_state *state, struct token *tk)
4907{
Eric Biedermancb364952004-11-15 10:46:44 +00004908 int rescan;
4909
4910 raw_token(state, tk);
4911 do {
4912 rescan = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004913 if (tk->tok == TOK_SPACE) {
4914 raw_token(state, tk);
4915 rescan = 1;
4916 }
4917 else if (tk->tok == TOK_IDENT) {
4918 if (state->token_base == 0) {
4919 ident_to_keyword(state, tk);
4920 } else {
4921 ident_to_macro(state, tk);
4922 }
4923 }
4924 } while(rescan);
4925}
4926
Eric Biederman41203d92004-11-08 09:31:09 +00004927static void preprocess(struct compile_state *state, struct token *tk);
4928
4929static void token(struct compile_state *state, struct token *tk)
4930{
4931 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004932 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004933 do {
4934 rescan = 0;
4935 /* Process a macro directive */
4936 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004937 /* Only match preprocessor directives at the start of a line */
4938 const char *ptr;
4939 ptr = state->file->line_start;
4940 while((ptr < tk->pos)
4941 && spacep(get_char(state->file, ptr)))
4942 {
4943 ptr = next_char(state->file, ptr, 1);
4944 }
4945 if (ptr == tk->pos) {
4946 preprocess(state, tk);
4947 rescan = 1;
4948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004949 }
Eric Biederman41203d92004-11-08 09:31:09 +00004950 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004951 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004952 rescan = compile_macro(state, &state->file, tk);
4953 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004954 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004955 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004956 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004957 /* Eat tokens disabled by the preprocessor
4958 * (Unless we are parsing a preprocessor directive
Eric Biedermancb364952004-11-15 10:46:44 +00004959 */
Eric Biedermana649a412004-11-09 00:35:39 +00004960 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004961 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004962 rescan = 1;
4963 }
Eric Biedermana649a412004-11-09 00:35:39 +00004964 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004965 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004966 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004967 rescan = 1;
4968 }
Eric Biedermancb364952004-11-15 10:46:44 +00004969 /* Error on unknown tokens */
4970 else if (tk->tok == TOK_UNKNOWN) {
4971 error(state, 0, "unknown token");
4972 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004973 } while(rescan);
4974}
4975
Eric Biederman41203d92004-11-08 09:31:09 +00004976
4977static inline struct token *get_token(struct compile_state *state, int offset)
4978{
4979 int index;
4980 index = state->token_base + offset;
4981 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4982 internal_error(state, 0, "token array to small");
4983 }
4984 return &state->token[index];
4985}
4986
4987static struct token *do_eat_token(struct compile_state *state, int tok)
4988{
4989 struct token *tk;
4990 int i;
4991 check_tok(state, get_token(state, 1), tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004992
Eric Biederman41203d92004-11-08 09:31:09 +00004993 /* Free the old token value */
4994 tk = get_token(state, 0);
4995 if (tk->str_len) {
4996 memset((void *)tk->val.str, -1, tk->str_len);
4997 xfree(tk->val.str);
4998 }
4999 /* Overwrite the old token with newer tokens */
5000 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
5001 state->token[i] = state->token[i + 1];
5002 }
5003 /* Clear the last token */
5004 memset(&state->token[i], 0, sizeof(state->token[i]));
5005 state->token[i].tok = -1;
5006
5007 /* Return the token */
5008 return tk;
5009}
5010
Eric Biedermancb364952004-11-15 10:46:44 +00005011static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005012{
5013 struct token *tk1;
5014 tk1 = get_token(state, 1);
5015 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005016 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005017 }
5018 return tk1->tok;
5019}
5020
Eric Biedermancb364952004-11-15 10:46:44 +00005021static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005022{
Eric Biedermancb364952004-11-15 10:46:44 +00005023 raw_peek(state);
5024 return do_eat_token(state, tok);
5025}
5026
5027static int pp_peek(struct compile_state *state)
5028{
5029 struct token *tk1;
5030 tk1 = get_token(state, 1);
5031 if (tk1->tok == -1) {
5032 pp_token(state, tk1);
5033 }
5034 return tk1->tok;
5035}
5036
5037static struct token *pp_eat(struct compile_state *state, int tok)
5038{
5039 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005040 return do_eat_token(state, tok);
5041}
5042
Eric Biedermanb138ac82003-04-22 18:44:01 +00005043static int peek(struct compile_state *state)
5044{
Eric Biederman41203d92004-11-08 09:31:09 +00005045 struct token *tk1;
5046 tk1 = get_token(state, 1);
5047 if (tk1->tok == -1) {
5048 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005049 }
Eric Biederman41203d92004-11-08 09:31:09 +00005050 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005051}
5052
5053static int peek2(struct compile_state *state)
5054{
Eric Biederman41203d92004-11-08 09:31:09 +00005055 struct token *tk1, *tk2;
5056 tk1 = get_token(state, 1);
5057 tk2 = get_token(state, 2);
5058 if (tk1->tok == -1) {
5059 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005060 }
Eric Biederman41203d92004-11-08 09:31:09 +00005061 if (tk2->tok == -1) {
5062 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005063 }
Eric Biederman41203d92004-11-08 09:31:09 +00005064 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005065}
5066
Eric Biederman41203d92004-11-08 09:31:09 +00005067static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005068{
Eric Biederman90089602004-05-28 14:11:54 +00005069 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005070 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005071}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005072
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005073static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005074{
Eric Biederman90089602004-05-28 14:11:54 +00005075 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005076 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005077 int subdir_len;
5078 struct file_state *file;
5079 char *basename;
5080 file = xmalloc(sizeof(*file), "file_state");
5081
5082 base = strrchr(filename, '/');
5083 subdir = filename;
5084 if (base != 0) {
5085 subdir_len = base - filename;
5086 base++;
5087 }
5088 else {
5089 base = filename;
5090 subdir_len = 0;
5091 }
5092 basename = xmalloc(strlen(base) +1, "basename");
5093 strcpy(basename, base);
5094 file->basename = basename;
5095
5096 if (getcwd(cwd, sizeof(cwd)) == 0) {
5097 die("cwd buffer to small");
5098 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005099 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005100 file->dirname = xmalloc(subdir_len + 1, "dirname");
5101 memcpy(file->dirname, subdir, subdir_len);
5102 file->dirname[subdir_len] = '\0';
5103 }
5104 else {
Eric Biederman90089602004-05-28 14:11:54 +00005105 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005106 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005107 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005108 /* Find the appropriate directory... */
5109 dir = 0;
5110 if (!state->file && exists(cwd, filename)) {
5111 dir = cwd;
5112 }
5113 if (local && state->file && exists(state->file->dirname, filename)) {
5114 dir = state->file->dirname;
5115 }
Eric Biederman90089602004-05-28 14:11:54 +00005116 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005117 if (exists(*path, filename)) {
5118 dir = *path;
5119 }
5120 }
5121 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005122 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005123 }
5124 dirlen = strlen(dir);
5125 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5126 memcpy(file->dirname, dir, dirlen);
5127 file->dirname[dirlen] = '/';
5128 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5129 file->dirname[dirlen + 1 + subdir_len] = '\0';
5130 }
5131 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005132
5133 file->pos = file->buf;
5134 file->line_start = file->pos;
5135 file->line = 1;
5136
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005137 file->report_line = 1;
5138 file->report_name = file->basename;
5139 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005140 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005141 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5142 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005143
Eric Biedermanb138ac82003-04-22 18:44:01 +00005144 file->prev = state->file;
5145 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005146}
5147
Eric Biederman41203d92004-11-08 09:31:09 +00005148static struct triple *constant_expr(struct compile_state *state);
5149static void integral(struct compile_state *state, struct triple *def);
5150
5151static int mcexpr(struct compile_state *state)
5152{
5153 struct triple *cvalue;
5154 cvalue = constant_expr(state);
5155 integral(state, cvalue);
5156 if (cvalue->op != OP_INTCONST) {
5157 error(state, 0, "integer constant expected");
5158 }
5159 return cvalue->u.cval != 0;
5160}
5161
5162static void preprocess(struct compile_state *state, struct token *current_token)
5163{
5164 /* Doing much more with the preprocessor would require
5165 * a parser and a major restructuring.
5166 * Postpone that for later.
5167 */
Eric Biederman41203d92004-11-08 09:31:09 +00005168 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005169 int tok;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005170
Eric Biedermancb364952004-11-15 10:46:44 +00005171 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005172
5173 old_token_base = state->token_base;
5174 state->token_base = current_token - state->token;
5175
Eric Biedermancb364952004-11-15 10:46:44 +00005176 tok = pp_peek(state);
5177 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005178 case TOK_LIT_INT:
5179 {
5180 struct token *tk;
5181 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005182 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005183 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005184 /* I have a preprocessor line marker parse it */
5185 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005186 const char *token, *base;
5187 char *name, *dir;
5188 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005189 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005190 name = xmalloc(tk->str_len, "report_name");
5191 token = tk->val.str + 1;
5192 base = strrchr(token, '/');
5193 name_len = tk->str_len -2;
5194 if (base != 0) {
5195 dir_len = base - token;
5196 base++;
5197 name_len -= base - token;
5198 } else {
5199 dir_len = 0;
5200 base = token;
5201 }
5202 memcpy(name, base, name_len);
5203 name[name_len] = '\0';
5204 dir = xmalloc(dir_len + 1, "report_dir");
5205 memcpy(dir, token, dir_len);
5206 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005207 state->file->report_line = override_line - 1;
5208 state->file->report_name = name;
5209 state->file->report_dir = dir;
5210 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005211 }
5212 break;
5213 }
5214 case TOK_MLINE:
5215 {
5216 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005217 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005218 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005219 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5220 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005221 const char *token, *base;
5222 char *name, *dir;
5223 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005224 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005225 name = xmalloc(tk->str_len, "report_name");
5226 token = tk->val.str + 1;
5227 base = strrchr(token, '/');
5228 name_len = tk->str_len - 2;
5229 if (base != 0) {
5230 dir_len = base - token;
5231 base++;
5232 name_len -= base - token;
5233 } else {
5234 dir_len = 0;
5235 base = token;
5236 }
5237 memcpy(name, base, name_len);
5238 name[name_len] = '\0';
5239 dir = xmalloc(dir_len + 1, "report_dir");
5240 memcpy(dir, token, dir_len);
5241 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005242 state->file->report_name = name;
5243 state->file->report_dir = dir;
5244 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005245 }
5246 break;
5247 }
5248 case TOK_MUNDEF:
5249 {
5250 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005251 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005252 if (if_eat(state)) /* quit early when #if'd out */
5253 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005254
Eric Biedermancb364952004-11-15 10:46:44 +00005255 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005256
5257 undef_macro(state, ident);
5258 break;
5259 }
5260 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005261 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005262 if (if_eat(state)) /* quit early when #if'd out */
5263 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005264 warning(state, 0, "Ignoring pragma");
Eric Biederman41203d92004-11-08 09:31:09 +00005265 break;
5266 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005267 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005268 reenter_if(state, "#elif");
5269 if (if_eat(state)) /* quit early when #if'd out */
5270 break;
5271 /* If the #if was taken the #elif just disables the following code */
5272 if (if_value(state)) {
5273 eat_tokens(state, TOK_MENDIF);
5274 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005275 /* If the previous #if was not taken see if the #elif enables the
Eric Biederman41203d92004-11-08 09:31:09 +00005276 * trailing code.
5277 */
5278 else {
5279 set_if_value(state, mcexpr(state));
5280 if (!if_value(state)) {
5281 eat_tokens(state, TOK_MELSE);
5282 }
5283 }
5284 break;
5285 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005286 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005287 enter_if(state);
5288 if (if_eat(state)) /* quit early when #if'd out */
5289 break;
5290 set_if_value(state, mcexpr(state));
5291 if (!if_value(state)) {
5292 eat_tokens(state, TOK_MELSE);
5293 }
5294 break;
5295 case TOK_MIFNDEF:
5296 {
5297 struct hash_entry *ident;
5298
Eric Biedermancb364952004-11-15 10:46:44 +00005299 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005300 enter_if(state);
5301 if (if_eat(state)) /* quit early when #if'd out */
5302 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005303 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005304 set_if_value(state, ident->sym_define == 0);
5305 if (!if_value(state)) {
5306 eat_tokens(state, TOK_MELSE);
5307 }
5308 break;
5309 }
5310 case TOK_MIFDEF:
5311 {
5312 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005313 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005314 enter_if(state);
5315 if (if_eat(state)) /* quit early when #if'd out */
5316 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005317 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005318 set_if_value(state, ident->sym_define != 0);
5319 if (!if_value(state)) {
5320 eat_tokens(state, TOK_MELSE);
5321 }
5322 break;
5323 }
5324 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005325 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005326 enter_else(state, "#else");
5327 if (!if_eat(state) && if_value(state)) {
5328 eat_tokens(state, TOK_MENDIF);
5329 }
5330 break;
5331 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005332 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005333 exit_if(state, "#endif");
5334 break;
5335 case TOK_MDEFINE:
5336 {
5337 struct hash_entry *ident;
5338 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005339 const char *mstart, *mend;
5340 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005341
Eric Biedermancb364952004-11-15 10:46:44 +00005342 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005343 if (if_eat(state)) /* quit early when #if'd out */
5344 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005345 ident = pp_eat(state, TOK_MIDENT)->ident;
5346 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005347 args = 0;
5348 larg = &args;
5349
Eric Biederman41203d92004-11-08 09:31:09 +00005350 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005351 if (raw_peek(state) == TOK_LPAREN) {
5352 raw_eat(state, TOK_LPAREN);
5353 argc += 1;
5354
Eric Biederman41203d92004-11-08 09:31:09 +00005355 for(;;) {
5356 struct macro_arg *narg, *arg;
5357 struct hash_entry *aident;
5358 int tok;
5359
Eric Biedermancb364952004-11-15 10:46:44 +00005360 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005361 if (!args && (tok == TOK_RPAREN)) {
5362 break;
5363 }
5364 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005365 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005366 aident = state->i___VA_ARGS__;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005367 }
Eric Biederman41203d92004-11-08 09:31:09 +00005368 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005369 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005370 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005371
Eric Biederman41203d92004-11-08 09:31:09 +00005372 narg = xcmalloc(sizeof(*arg), "macro arg");
5373 narg->ident = aident;
5374
5375 /* Verify I don't have a duplicate identifier */
5376 for(arg = args; arg; arg = arg->next) {
5377 if (arg->ident == narg->ident) {
5378 error(state, 0, "Duplicate macro arg `%s'",
5379 narg->ident->name);
5380 }
5381 }
5382 /* Add the new argument to the end of the list */
5383 *larg = narg;
5384 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005385 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005386
5387 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005388 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005389 break;
5390 }
Eric Biedermancb364952004-11-15 10:46:44 +00005391 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005392 }
Eric Biedermancb364952004-11-15 10:46:44 +00005393 pp_eat(state, TOK_RPAREN);
5394 }
5395 /* Remove leading whitespace */
5396 while(raw_peek(state) == TOK_SPACE) {
5397 raw_eat(state, TOK_SPACE);
5398 }
Eric Biederman41203d92004-11-08 09:31:09 +00005399
Eric Biedermancb364952004-11-15 10:46:44 +00005400 /* Remember the start of the macro body */
5401 tok = raw_peek(state);
5402 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005403
Eric Biedermancb364952004-11-15 10:46:44 +00005404 /* Find the end of the macro */
5405 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5406 raw_eat(state, tok);
5407 /* Remember the end of the last non space token */
5408 raw_peek(state);
5409 if (tok != TOK_SPACE) {
5410 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005411 }
5412 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005413
Eric Biedermancb364952004-11-15 10:46:44 +00005414 /* Now that I have found the body defined the token */
5415 do_define_macro(state, ident,
5416 char_strdup(state->file, mstart, mend, "macro buf"),
5417 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005418 break;
5419 }
5420 case TOK_MERROR:
5421 {
Eric Biedermancb364952004-11-15 10:46:44 +00005422 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005423 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005424
Eric Biedermancb364952004-11-15 10:46:44 +00005425 pp_eat(state, TOK_MERROR);
5426 /* Find the start of the line */
5427 raw_peek(state);
5428 start = get_token(state, 1)->pos;
5429
Eric Biederman41203d92004-11-08 09:31:09 +00005430 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005431 while((tok = raw_peek(state)) != TOK_EOL) {
5432 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005433 }
Eric Biedermancb364952004-11-15 10:46:44 +00005434 end = get_token(state, 1)->pos;
5435 len = end - start;
5436 if (!if_eat(state)) {
5437 error(state, 0, "%*.*s", len, len, start);
5438 }
Eric Biederman41203d92004-11-08 09:31:09 +00005439 break;
5440 }
5441 case TOK_MWARNING:
5442 {
Eric Biedermancb364952004-11-15 10:46:44 +00005443 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005444 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005445
Eric Biedermancb364952004-11-15 10:46:44 +00005446 pp_eat(state, TOK_MWARNING);
5447
5448 /* Find the start of the line */
5449 raw_peek(state);
5450 start = get_token(state, 1)->pos;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005451
Eric Biederman41203d92004-11-08 09:31:09 +00005452 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005453 while((tok = raw_peek(state)) != TOK_EOL) {
5454 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005455 }
Eric Biedermancb364952004-11-15 10:46:44 +00005456 end = get_token(state, 1)->pos;
5457 len = end - start;
5458 if (!if_eat(state)) {
5459 warning(state, 0, "%*.*s", len, len, start);
5460 }
Eric Biederman41203d92004-11-08 09:31:09 +00005461 break;
5462 }
5463 case TOK_MINCLUDE:
5464 {
5465 char *name;
5466 int local;
5467 local = 0;
5468 name = 0;
5469
Eric Biedermancb364952004-11-15 10:46:44 +00005470 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005471 if (if_eat(state)) {
5472 /* Find the end of the line */
5473 while((tok = raw_peek(state)) != TOK_EOL) {
5474 raw_eat(state, tok);
5475 }
5476 break;
5477 }
Eric Biederman41203d92004-11-08 09:31:09 +00005478 tok = peek(state);
5479 if (tok == TOK_LIT_STRING) {
5480 struct token *tk;
5481 const char *token;
5482 int name_len;
5483 tk = eat(state, TOK_LIT_STRING);
5484 name = xmalloc(tk->str_len, "include");
5485 token = tk->val.str +1;
5486 name_len = tk->str_len -2;
5487 if (*token == '"') {
5488 token++;
5489 name_len--;
5490 }
5491 memcpy(name, token, name_len);
5492 name[name_len] = '\0';
5493 local = 1;
5494 }
5495 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005496 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005497 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005498
5499 buf.len = 40;
5500 buf.str = xmalloc(buf.len, "include");
5501 buf.pos = 0;
5502
5503 tok = peek(state);
5504 while((tok != TOK_MORE) &&
5505 (tok != TOK_EOL) && (tok != TOK_EOF))
5506 {
5507 struct token *tk;
5508 tk = eat(state, tok);
5509 append_macro_chars(state, "include", &buf,
5510 state->file, tk->pos, state->file->pos);
5511 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005512 }
Eric Biedermancb364952004-11-15 10:46:44 +00005513 append_macro_text(state, "include", &buf, "\0", 1);
5514 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005515 error(state, 0, "Unterminated include directive");
5516 }
Eric Biederman41203d92004-11-08 09:31:09 +00005517 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005518 local = 0;
5519 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005520 }
5521 else {
5522 error(state, 0, "Invalid include directive");
5523 }
5524 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005525 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005526 error(state, 0, "garbage after include directive");
5527 }
5528 if (!if_eat(state)) {
5529 compile_file(state, name, local);
5530 }
5531 xfree(name);
5532 break;
5533 }
5534 case TOK_EOL:
5535 /* Ignore # without a follwing ident */
5536 break;
5537 default:
5538 {
5539 const char *name1, *name2;
5540 name1 = tokens[tok];
5541 name2 = "";
5542 if (tok == TOK_MIDENT) {
5543 name2 = get_token(state, 1)->ident->name;
5544 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005545 error(state, 0, "Invalid preprocessor directive: %s %s",
Eric Biederman41203d92004-11-08 09:31:09 +00005546 name1, name2);
5547 break;
5548 }
5549 }
5550 /* Consume the rest of the macro line */
5551 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005552 tok = pp_peek(state);
5553 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005554 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5555 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005556 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005557 return;
5558}
5559
Eric Biederman0babc1c2003-05-09 02:39:00 +00005560/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005561
5562static struct type *new_type(
5563 unsigned int type, struct type *left, struct type *right)
5564{
5565 struct type *result;
5566 result = xmalloc(sizeof(*result), "type");
5567 result->type = type;
5568 result->left = left;
5569 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005570 result->field_ident = 0;
5571 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005572 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005573 return result;
5574}
5575
5576static struct type *clone_type(unsigned int specifiers, struct type *old)
5577{
5578 struct type *result;
5579 result = xmalloc(sizeof(*result), "type");
5580 memcpy(result, old, sizeof(*result));
5581 result->type &= TYPE_MASK;
5582 result->type |= specifiers;
5583 return result;
5584}
5585
Eric Biederman90089602004-05-28 14:11:54 +00005586static struct type *dup_type(struct compile_state *state, struct type *orig)
5587{
5588 struct type *new;
5589 new = xcmalloc(sizeof(*new), "type");
5590 new->type = orig->type;
5591 new->field_ident = orig->field_ident;
5592 new->type_ident = orig->type_ident;
5593 new->elements = orig->elements;
5594 if (orig->left) {
5595 new->left = dup_type(state, orig->left);
5596 }
5597 if (orig->right) {
5598 new->right = dup_type(state, orig->right);
5599 }
5600 return new;
5601}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005602
Eric Biederman90089602004-05-28 14:11:54 +00005603
5604static struct type *invalid_type(struct compile_state *state, struct type *type)
5605{
5606 struct type *invalid, *member;
5607 invalid = 0;
5608 if (!type) {
5609 internal_error(state, 0, "type missing?");
5610 }
5611 switch(type->type & TYPE_MASK) {
5612 case TYPE_VOID:
5613 case TYPE_CHAR: case TYPE_UCHAR:
5614 case TYPE_SHORT: case TYPE_USHORT:
5615 case TYPE_INT: case TYPE_UINT:
5616 case TYPE_LONG: case TYPE_ULONG:
5617 case TYPE_LLONG: case TYPE_ULLONG:
5618 case TYPE_POINTER:
5619 case TYPE_ENUM:
5620 break;
5621 case TYPE_BITFIELD:
5622 invalid = invalid_type(state, type->left);
5623 break;
5624 case TYPE_ARRAY:
5625 invalid = invalid_type(state, type->left);
5626 break;
5627 case TYPE_STRUCT:
5628 case TYPE_TUPLE:
5629 member = type->left;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005630 while(member && (invalid == 0) &&
Eric Biederman90089602004-05-28 14:11:54 +00005631 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5632 invalid = invalid_type(state, member->left);
5633 member = member->right;
5634 }
5635 if (!invalid) {
5636 invalid = invalid_type(state, member);
5637 }
5638 break;
5639 case TYPE_UNION:
5640 case TYPE_JOIN:
5641 member = type->left;
5642 while(member && (invalid == 0) &&
5643 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5644 invalid = invalid_type(state, member->left);
5645 member = member->right;
5646 }
5647 if (!invalid) {
5648 invalid = invalid_type(state, member);
5649 }
5650 break;
5651 default:
5652 invalid = type;
5653 break;
5654 }
5655 return invalid;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005656
Eric Biederman90089602004-05-28 14:11:54 +00005657}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005658
Eric Biederman90089602004-05-28 14:11:54 +00005659static struct type void_type = { .type = TYPE_VOID };
5660static struct type char_type = { .type = TYPE_CHAR };
5661static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005662#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005663static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005664#endif
Eric Biederman90089602004-05-28 14:11:54 +00005665static struct type ushort_type = { .type = TYPE_USHORT };
5666static struct type int_type = { .type = TYPE_INT };
5667static struct type uint_type = { .type = TYPE_UINT };
5668static struct type long_type = { .type = TYPE_LONG };
5669static struct type ulong_type = { .type = TYPE_ULONG };
5670static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005671
Eric Biederman5ade04a2003-10-22 04:03:46 +00005672static struct type void_ptr_type = {
5673 .type = TYPE_POINTER,
5674 .left = &void_type,
5675};
5676
Stefan Reinauer50542a82007-10-24 11:14:14 +00005677#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005678static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005679 .type = TYPE_FUNCTION,
5680 .left = &void_type,
5681 .right = &void_type,
5682};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005683#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005684
Eric Biederman90089602004-05-28 14:11:54 +00005685static size_t bits_to_bytes(size_t size)
5686{
5687 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5688}
5689
Eric Biedermanb138ac82003-04-22 18:44:01 +00005690static struct triple *variable(struct compile_state *state, struct type *type)
5691{
5692 struct triple *result;
5693 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005694 result = triple(state, OP_ADECL, type, 0, 0);
5695 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005696 }
5697 else {
5698 result = triple(state, OP_SDECL, type, 0, 0);
5699 }
5700 return result;
5701}
5702
5703static void stor_of(FILE *fp, struct type *type)
5704{
5705 switch(type->type & STOR_MASK) {
5706 case STOR_AUTO:
5707 fprintf(fp, "auto ");
5708 break;
5709 case STOR_STATIC:
5710 fprintf(fp, "static ");
5711 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005712 case STOR_LOCAL:
5713 fprintf(fp, "local ");
5714 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005715 case STOR_EXTERN:
5716 fprintf(fp, "extern ");
5717 break;
5718 case STOR_REGISTER:
5719 fprintf(fp, "register ");
5720 break;
5721 case STOR_TYPEDEF:
5722 fprintf(fp, "typedef ");
5723 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005724 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005725 fprintf(fp, "inline ");
5726 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005727 case STOR_INLINE | STOR_STATIC:
5728 fprintf(fp, "static inline");
5729 break;
5730 case STOR_INLINE | STOR_EXTERN:
5731 fprintf(fp, "extern inline");
5732 break;
5733 default:
5734 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5735 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005736 }
5737}
5738static void qual_of(FILE *fp, struct type *type)
5739{
5740 if (type->type & QUAL_CONST) {
5741 fprintf(fp, " const");
5742 }
5743 if (type->type & QUAL_VOLATILE) {
5744 fprintf(fp, " volatile");
5745 }
5746 if (type->type & QUAL_RESTRICT) {
5747 fprintf(fp, " restrict");
5748 }
5749}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005750
Eric Biedermanb138ac82003-04-22 18:44:01 +00005751static void name_of(FILE *fp, struct type *type)
5752{
Eric Biederman90089602004-05-28 14:11:54 +00005753 unsigned int base_type;
5754 base_type = type->type & TYPE_MASK;
5755 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5756 stor_of(fp, type);
5757 }
5758 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005759 case TYPE_VOID:
5760 fprintf(fp, "void");
5761 qual_of(fp, type);
5762 break;
5763 case TYPE_CHAR:
5764 fprintf(fp, "signed char");
5765 qual_of(fp, type);
5766 break;
5767 case TYPE_UCHAR:
5768 fprintf(fp, "unsigned char");
5769 qual_of(fp, type);
5770 break;
5771 case TYPE_SHORT:
5772 fprintf(fp, "signed short");
5773 qual_of(fp, type);
5774 break;
5775 case TYPE_USHORT:
5776 fprintf(fp, "unsigned short");
5777 qual_of(fp, type);
5778 break;
5779 case TYPE_INT:
5780 fprintf(fp, "signed int");
5781 qual_of(fp, type);
5782 break;
5783 case TYPE_UINT:
5784 fprintf(fp, "unsigned int");
5785 qual_of(fp, type);
5786 break;
5787 case TYPE_LONG:
5788 fprintf(fp, "signed long");
5789 qual_of(fp, type);
5790 break;
5791 case TYPE_ULONG:
5792 fprintf(fp, "unsigned long");
5793 qual_of(fp, type);
5794 break;
5795 case TYPE_POINTER:
5796 name_of(fp, type->left);
5797 fprintf(fp, " * ");
5798 qual_of(fp, type);
5799 break;
5800 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005801 name_of(fp, type->left);
5802 fprintf(fp, ", ");
5803 name_of(fp, type->right);
5804 break;
Eric Biederman90089602004-05-28 14:11:54 +00005805 case TYPE_OVERLAP:
5806 name_of(fp, type->left);
5807 fprintf(fp, ",| ");
5808 name_of(fp, type->right);
5809 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005810 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005811 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005812 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005813 qual_of(fp, type);
5814 break;
5815 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005816 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005817 (type->type_ident)? type->type_ident->name : "");
5818 name_of(fp, type->left);
5819 fprintf(fp, " } ");
5820 qual_of(fp, type);
5821 break;
5822 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005823 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005824 (type->type_ident)? type->type_ident->name : "");
5825 name_of(fp, type->left);
5826 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005827 qual_of(fp, type);
5828 break;
5829 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005830 name_of(fp, type->left);
5831 fprintf(fp, " (*)(");
5832 name_of(fp, type->right);
5833 fprintf(fp, ")");
5834 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005835 case TYPE_ARRAY:
5836 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005837 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 break;
Eric Biederman90089602004-05-28 14:11:54 +00005839 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005840 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005841 name_of(fp, type->left);
5842 fprintf(fp, " } ");
5843 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005844 break;
Eric Biederman90089602004-05-28 14:11:54 +00005845 case TYPE_JOIN:
5846 fprintf(fp, "join { ");
5847 name_of(fp, type->left);
5848 fprintf(fp, " } ");
5849 qual_of(fp, type);
5850 break;
5851 case TYPE_BITFIELD:
5852 name_of(fp, type->left);
5853 fprintf(fp, " : %d ", type->elements);
5854 qual_of(fp, type);
5855 break;
5856 case TYPE_UNKNOWN:
5857 fprintf(fp, "unknown_t");
5858 break;
5859 default:
5860 fprintf(fp, "????: %x", base_type);
5861 break;
5862 }
5863 if (type->field_ident && type->field_ident->name) {
5864 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005865 }
5866}
5867
5868static size_t align_of(struct compile_state *state, struct type *type)
5869{
5870 size_t align;
5871 align = 0;
5872 switch(type->type & TYPE_MASK) {
5873 case TYPE_VOID:
5874 align = 1;
5875 break;
Eric Biederman90089602004-05-28 14:11:54 +00005876 case TYPE_BITFIELD:
5877 align = 1;
5878 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005879 case TYPE_CHAR:
5880 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005881 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005882 break;
5883 case TYPE_SHORT:
5884 case TYPE_USHORT:
5885 align = ALIGNOF_SHORT;
5886 break;
5887 case TYPE_INT:
5888 case TYPE_UINT:
5889 case TYPE_ENUM:
5890 align = ALIGNOF_INT;
5891 break;
5892 case TYPE_LONG:
5893 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005894 align = ALIGNOF_LONG;
5895 break;
Eric Biederman90089602004-05-28 14:11:54 +00005896 case TYPE_POINTER:
5897 align = ALIGNOF_POINTER;
5898 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005899 case TYPE_PRODUCT:
5900 case TYPE_OVERLAP:
5901 {
5902 size_t left_align, right_align;
5903 left_align = align_of(state, type->left);
5904 right_align = align_of(state, type->right);
5905 align = (left_align >= right_align) ? left_align : right_align;
5906 break;
5907 }
5908 case TYPE_ARRAY:
5909 align = align_of(state, type->left);
5910 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005911 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005912 case TYPE_TUPLE:
5913 case TYPE_UNION:
5914 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005915 align = align_of(state, type->left);
5916 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005917 default:
5918 error(state, 0, "alignof not yet defined for type\n");
5919 break;
5920 }
5921 return align;
5922}
5923
Eric Biederman90089602004-05-28 14:11:54 +00005924static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005925{
Eric Biederman90089602004-05-28 14:11:54 +00005926 size_t align;
5927 align = 0;
5928 switch(type->type & TYPE_MASK) {
5929 case TYPE_VOID:
5930 align = 1;
5931 break;
5932 case TYPE_BITFIELD:
5933 align = 1;
5934 break;
5935 case TYPE_CHAR:
5936 case TYPE_UCHAR:
5937 align = REG_ALIGNOF_CHAR;
5938 break;
5939 case TYPE_SHORT:
5940 case TYPE_USHORT:
5941 align = REG_ALIGNOF_SHORT;
5942 break;
5943 case TYPE_INT:
5944 case TYPE_UINT:
5945 case TYPE_ENUM:
5946 align = REG_ALIGNOF_INT;
5947 break;
5948 case TYPE_LONG:
5949 case TYPE_ULONG:
5950 align = REG_ALIGNOF_LONG;
5951 break;
5952 case TYPE_POINTER:
5953 align = REG_ALIGNOF_POINTER;
5954 break;
5955 case TYPE_PRODUCT:
5956 case TYPE_OVERLAP:
5957 {
5958 size_t left_align, right_align;
5959 left_align = reg_align_of(state, type->left);
5960 right_align = reg_align_of(state, type->right);
5961 align = (left_align >= right_align) ? left_align : right_align;
5962 break;
5963 }
5964 case TYPE_ARRAY:
5965 align = reg_align_of(state, type->left);
5966 break;
5967 case TYPE_STRUCT:
5968 case TYPE_UNION:
5969 case TYPE_TUPLE:
5970 case TYPE_JOIN:
5971 align = reg_align_of(state, type->left);
5972 break;
5973 default:
5974 error(state, 0, "alignof not yet defined for type\n");
5975 break;
5976 }
5977 return align;
5978}
5979
5980static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5981{
5982 return bits_to_bytes(align_of(state, type));
5983}
5984static size_t size_of(struct compile_state *state, struct type *type);
5985static size_t reg_size_of(struct compile_state *state, struct type *type);
5986
Stefan Reinauer14e22772010-04-27 06:56:47 +00005987static size_t needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00005988 struct type *type, size_t offset)
5989{
5990 size_t padding, align;
5991 align = align_of(state, type);
5992 /* Align to the next machine word if the bitfield does completely
5993 * fit into the current word.
5994 */
5995 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5996 size_t size;
5997 size = size_of(state, type);
5998 if ((offset + type->elements)/size != offset/size) {
5999 align = size;
6000 }
6001 }
Eric Biederman03b59862003-06-24 14:27:37 +00006002 padding = 0;
6003 if (offset % align) {
6004 padding = align - (offset % align);
6005 }
6006 return padding;
6007}
Eric Biederman90089602004-05-28 14:11:54 +00006008
Stefan Reinauer14e22772010-04-27 06:56:47 +00006009static size_t reg_needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006010 struct type *type, size_t offset)
6011{
6012 size_t padding, align;
6013 align = reg_align_of(state, type);
6014 /* Align to the next register word if the bitfield does completely
6015 * fit into the current register.
6016 */
6017 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00006018 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
Eric Biederman90089602004-05-28 14:11:54 +00006019 {
6020 align = REG_SIZEOF_REG;
6021 }
6022 padding = 0;
6023 if (offset % align) {
6024 padding = align - (offset % align);
6025 }
6026 return padding;
6027}
6028
Eric Biedermanb138ac82003-04-22 18:44:01 +00006029static size_t size_of(struct compile_state *state, struct type *type)
6030{
6031 size_t size;
6032 size = 0;
6033 switch(type->type & TYPE_MASK) {
6034 case TYPE_VOID:
6035 size = 0;
6036 break;
Eric Biederman90089602004-05-28 14:11:54 +00006037 case TYPE_BITFIELD:
6038 size = type->elements;
6039 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006040 case TYPE_CHAR:
6041 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006042 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006043 break;
6044 case TYPE_SHORT:
6045 case TYPE_USHORT:
6046 size = SIZEOF_SHORT;
6047 break;
6048 case TYPE_INT:
6049 case TYPE_UINT:
6050 case TYPE_ENUM:
6051 size = SIZEOF_INT;
6052 break;
6053 case TYPE_LONG:
6054 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006055 size = SIZEOF_LONG;
6056 break;
Eric Biederman90089602004-05-28 14:11:54 +00006057 case TYPE_POINTER:
6058 size = SIZEOF_POINTER;
6059 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006060 case TYPE_PRODUCT:
6061 {
Eric Biederman90089602004-05-28 14:11:54 +00006062 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006063 size = 0;
6064 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006065 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006066 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006067 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006068 }
Eric Biederman90089602004-05-28 14:11:54 +00006069 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006070 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006071 break;
6072 }
6073 case TYPE_OVERLAP:
6074 {
6075 size_t size_left, size_right;
6076 size_left = size_of(state, type->left);
6077 size_right = size_of(state, type->right);
6078 size = (size_left >= size_right)? size_left : size_right;
6079 break;
6080 }
6081 case TYPE_ARRAY:
6082 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6083 internal_error(state, 0, "Invalid array type");
6084 } else {
6085 size = size_of(state, type->left) * type->elements;
6086 }
6087 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006088 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006089 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006090 {
Eric Biederman90089602004-05-28 14:11:54 +00006091 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006092 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006093 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006094 pad = needed_padding(state, type, size);
6095 size = size + pad;
6096 break;
6097 }
6098 case TYPE_UNION:
6099 case TYPE_JOIN:
6100 {
6101 size_t pad;
6102 size = size_of(state, type->left);
6103 /* Pad unions so their size is a multiple of their alignment */
6104 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006105 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006106 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006108 default:
Eric Biederman90089602004-05-28 14:11:54 +00006109 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006110 break;
6111 }
6112 return size;
6113}
6114
Eric Biederman90089602004-05-28 14:11:54 +00006115static size_t reg_size_of(struct compile_state *state, struct type *type)
6116{
6117 size_t size;
6118 size = 0;
6119 switch(type->type & TYPE_MASK) {
6120 case TYPE_VOID:
6121 size = 0;
6122 break;
6123 case TYPE_BITFIELD:
6124 size = type->elements;
6125 break;
6126 case TYPE_CHAR:
6127 case TYPE_UCHAR:
6128 size = REG_SIZEOF_CHAR;
6129 break;
6130 case TYPE_SHORT:
6131 case TYPE_USHORT:
6132 size = REG_SIZEOF_SHORT;
6133 break;
6134 case TYPE_INT:
6135 case TYPE_UINT:
6136 case TYPE_ENUM:
6137 size = REG_SIZEOF_INT;
6138 break;
6139 case TYPE_LONG:
6140 case TYPE_ULONG:
6141 size = REG_SIZEOF_LONG;
6142 break;
6143 case TYPE_POINTER:
6144 size = REG_SIZEOF_POINTER;
6145 break;
6146 case TYPE_PRODUCT:
6147 {
6148 size_t pad;
6149 size = 0;
6150 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6151 pad = reg_needed_padding(state, type->left, size);
6152 size = size + pad + reg_size_of(state, type->left);
6153 type = type->right;
6154 }
6155 pad = reg_needed_padding(state, type, size);
6156 size = size + pad + reg_size_of(state, type);
6157 break;
6158 }
6159 case TYPE_OVERLAP:
6160 {
6161 size_t size_left, size_right;
6162 size_left = reg_size_of(state, type->left);
6163 size_right = reg_size_of(state, type->right);
6164 size = (size_left >= size_right)? size_left : size_right;
6165 break;
6166 }
6167 case TYPE_ARRAY:
6168 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6169 internal_error(state, 0, "Invalid array type");
6170 } else {
6171 size = reg_size_of(state, type->left) * type->elements;
6172 }
6173 break;
6174 case TYPE_STRUCT:
6175 case TYPE_TUPLE:
6176 {
6177 size_t pad;
6178 size = reg_size_of(state, type->left);
6179 /* Pad structures so their size is a multiples of their alignment */
6180 pad = reg_needed_padding(state, type, size);
6181 size = size + pad;
6182 break;
6183 }
6184 case TYPE_UNION:
6185 case TYPE_JOIN:
6186 {
6187 size_t pad;
6188 size = reg_size_of(state, type->left);
6189 /* Pad unions so their size is a multiple of their alignment */
6190 pad = reg_needed_padding(state, type, size);
6191 size = size + pad;
6192 break;
6193 }
6194 default:
6195 internal_error(state, 0, "sizeof not yet defined for type");
6196 break;
6197 }
6198 return size;
6199}
6200
6201static size_t registers_of(struct compile_state *state, struct type *type)
6202{
6203 size_t registers;
6204 registers = reg_size_of(state, type);
6205 registers += REG_SIZEOF_REG - 1;
6206 registers /= REG_SIZEOF_REG;
6207 return registers;
6208}
6209
6210static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6211{
6212 return bits_to_bytes(size_of(state, type));
6213}
6214
Stefan Reinauer14e22772010-04-27 06:56:47 +00006215static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006216 struct type *type, struct hash_entry *field)
6217{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006218 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006219 size_t size;
6220
Eric Biederman0babc1c2003-05-09 02:39:00 +00006221 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006222 member = 0;
6223 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6224 member = type->left;
6225 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6226 size += needed_padding(state, member->left, size);
6227 if (member->left->field_ident == field) {
6228 member = member->left;
6229 break;
6230 }
6231 size += size_of(state, member->left);
6232 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006233 }
Eric Biederman90089602004-05-28 14:11:54 +00006234 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006235 }
Eric Biederman90089602004-05-28 14:11:54 +00006236 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6237 member = type->left;
6238 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6239 if (member->left->field_ident == field) {
6240 member = member->left;
6241 break;
6242 }
6243 member = member->right;
6244 }
6245 }
6246 else {
6247 internal_error(state, 0, "field_offset only works on structures and unions");
6248 }
6249
6250 if (!member || (member->field_ident != field)) {
6251 error(state, 0, "member %s not present", field->name);
6252 }
6253 return size;
6254}
6255
Stefan Reinauer14e22772010-04-27 06:56:47 +00006256static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006257 struct type *type, struct hash_entry *field)
6258{
6259 struct type *member;
6260 size_t size;
6261
6262 size = 0;
6263 member = 0;
6264 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6265 member = type->left;
6266 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6267 size += reg_needed_padding(state, member->left, size);
6268 if (member->left->field_ident == field) {
6269 member = member->left;
6270 break;
6271 }
6272 size += reg_size_of(state, member->left);
6273 member = member->right;
6274 }
6275 }
6276 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6277 member = type->left;
6278 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6279 if (member->left->field_ident == field) {
6280 member = member->left;
6281 break;
6282 }
6283 member = member->right;
6284 }
6285 }
6286 else {
6287 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6288 }
6289
6290 size += reg_needed_padding(state, member, size);
6291 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006292 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006293 }
6294 return size;
6295}
6296
Stefan Reinauer14e22772010-04-27 06:56:47 +00006297static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006298 struct type *type, struct hash_entry *field)
6299{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006300 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006301
6302 member = 0;
6303 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6304 member = type->left;
6305 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6306 if (member->left->field_ident == field) {
6307 member = member->left;
6308 break;
6309 }
6310 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006311 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006312 }
Eric Biederman90089602004-05-28 14:11:54 +00006313 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6314 member = type->left;
6315 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6316 if (member->left->field_ident == field) {
6317 member = member->left;
6318 break;
6319 }
6320 member = member->right;
6321 }
6322 }
6323 else {
6324 internal_error(state, 0, "field_type only works on structures and unions");
6325 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006326
Eric Biederman90089602004-05-28 14:11:54 +00006327 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006328 error(state, 0, "member %s not present", field->name);
6329 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006330 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006331}
6332
Stefan Reinauer14e22772010-04-27 06:56:47 +00006333static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006334 struct type *type, ulong_t index)
6335{
6336 struct type *member;
6337 size_t size;
6338 size = 0;
6339 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6340 size = size_of(state, type->left) * index;
6341 }
6342 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6343 ulong_t i;
6344 member = type->left;
6345 i = 0;
6346 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6347 size += needed_padding(state, member->left, size);
6348 if (i == index) {
6349 member = member->left;
6350 break;
6351 }
6352 size += size_of(state, member->left);
6353 i++;
6354 member = member->right;
6355 }
6356 size += needed_padding(state, member, size);
6357 if (i != index) {
6358 internal_error(state, 0, "Missing member index: %u", index);
6359 }
6360 }
6361 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6362 ulong_t i;
6363 size = 0;
6364 member = type->left;
6365 i = 0;
6366 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6367 if (i == index) {
6368 member = member->left;
6369 break;
6370 }
6371 i++;
6372 member = member->right;
6373 }
6374 if (i != index) {
6375 internal_error(state, 0, "Missing member index: %u", index);
6376 }
6377 }
6378 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006379 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006380 "request for index %u in something not an array, tuple or join",
6381 index);
6382 }
6383 return size;
6384}
6385
Stefan Reinauer14e22772010-04-27 06:56:47 +00006386static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006387 struct type *type, ulong_t index)
6388{
6389 struct type *member;
6390 size_t size;
6391 size = 0;
6392 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6393 size = reg_size_of(state, type->left) * index;
6394 }
6395 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6396 ulong_t i;
6397 member = type->left;
6398 i = 0;
6399 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6400 size += reg_needed_padding(state, member->left, size);
6401 if (i == index) {
6402 member = member->left;
6403 break;
6404 }
6405 size += reg_size_of(state, member->left);
6406 i++;
6407 member = member->right;
6408 }
6409 size += reg_needed_padding(state, member, size);
6410 if (i != index) {
6411 internal_error(state, 0, "Missing member index: %u", index);
6412 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006413
Eric Biederman90089602004-05-28 14:11:54 +00006414 }
6415 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6416 ulong_t i;
6417 size = 0;
6418 member = type->left;
6419 i = 0;
6420 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6421 if (i == index) {
6422 member = member->left;
6423 break;
6424 }
6425 i++;
6426 member = member->right;
6427 }
6428 if (i != index) {
6429 internal_error(state, 0, "Missing member index: %u", index);
6430 }
6431 }
6432 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006433 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006434 "request for index %u in something not an array, tuple or join",
6435 index);
6436 }
6437 return size;
6438}
6439
6440static struct type *index_type(struct compile_state *state,
6441 struct type *type, ulong_t index)
6442{
6443 struct type *member;
6444 if (index >= type->elements) {
6445 internal_error(state, 0, "Invalid element %u requested", index);
6446 }
6447 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6448 member = type->left;
6449 }
6450 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6451 ulong_t i;
6452 member = type->left;
6453 i = 0;
6454 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6455 if (i == index) {
6456 member = member->left;
6457 break;
6458 }
6459 i++;
6460 member = member->right;
6461 }
6462 if (i != index) {
6463 internal_error(state, 0, "Missing member index: %u", index);
6464 }
6465 }
6466 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6467 ulong_t i;
6468 member = type->left;
6469 i = 0;
6470 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6471 if (i == index) {
6472 member = member->left;
6473 break;
6474 }
6475 i++;
6476 member = member->right;
6477 }
6478 if (i != index) {
6479 internal_error(state, 0, "Missing member index: %u", index);
6480 }
6481 }
6482 else {
6483 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006484 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006485 "request for index %u in something not an array, tuple or join",
6486 index);
6487 }
6488 return member;
6489}
6490
6491static struct type *unpack_type(struct compile_state *state, struct type *type)
6492{
6493 /* If I have a single register compound type not a bit-field
6494 * find the real type.
6495 */
6496 struct type *start_type;
6497 size_t size;
6498 /* Get out early if I need multiple registers for this type */
6499 size = reg_size_of(state, type);
6500 if (size > REG_SIZEOF_REG) {
6501 return type;
6502 }
6503 /* Get out early if I don't need any registers for this type */
6504 if (size == 0) {
6505 return &void_type;
6506 }
6507 /* Loop until I have no more layers I can remove */
6508 do {
6509 start_type = type;
6510 switch(type->type & TYPE_MASK) {
6511 case TYPE_ARRAY:
6512 /* If I have a single element the unpacked type
6513 * is that element.
6514 */
6515 if (type->elements == 1) {
6516 type = type->left;
6517 }
6518 break;
6519 case TYPE_STRUCT:
6520 case TYPE_TUPLE:
6521 /* If I have a single element the unpacked type
6522 * is that element.
6523 */
6524 if (type->elements == 1) {
6525 type = type->left;
6526 }
6527 /* If I have multiple elements the unpacked
6528 * type is the non-void element.
6529 */
6530 else {
6531 struct type *next, *member;
6532 struct type *sub_type;
6533 sub_type = 0;
6534 next = type->left;
6535 while(next) {
6536 member = next;
6537 next = 0;
6538 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6539 next = member->right;
6540 member = member->left;
6541 }
6542 if (reg_size_of(state, member) > 0) {
6543 if (sub_type) {
6544 internal_error(state, 0, "true compound type in a register");
6545 }
6546 sub_type = member;
6547 }
6548 }
6549 if (sub_type) {
6550 type = sub_type;
6551 }
6552 }
6553 break;
6554
6555 case TYPE_UNION:
6556 case TYPE_JOIN:
6557 /* If I have a single element the unpacked type
6558 * is that element.
6559 */
6560 if (type->elements == 1) {
6561 type = type->left;
6562 }
6563 /* I can't in general unpack union types */
6564 break;
6565 default:
6566 /* If I'm not a compound type I can't unpack it */
6567 break;
6568 }
6569 } while(start_type != type);
6570 switch(type->type & TYPE_MASK) {
6571 case TYPE_STRUCT:
6572 case TYPE_ARRAY:
6573 case TYPE_TUPLE:
6574 internal_error(state, 0, "irredicible type?");
6575 break;
6576 }
6577 return type;
6578}
6579
6580static int equiv_types(struct type *left, struct type *right);
6581static int is_compound_type(struct type *type);
6582
6583static struct type *reg_type(
6584 struct compile_state *state, struct type *type, int reg_offset)
6585{
6586 struct type *member;
6587 size_t size;
6588#if 1
6589 struct type *invalid;
6590 invalid = invalid_type(state, type);
6591 if (invalid) {
6592 fprintf(state->errout, "type: ");
6593 name_of(state->errout, type);
6594 fprintf(state->errout, "\n");
6595 fprintf(state->errout, "invalid: ");
6596 name_of(state->errout, invalid);
6597 fprintf(state->errout, "\n");
6598 internal_error(state, 0, "bad input type?");
6599 }
6600#endif
6601
6602 size = reg_size_of(state, type);
6603 if (reg_offset > size) {
6604 member = 0;
6605 fprintf(state->errout, "type: ");
6606 name_of(state->errout, type);
6607 fprintf(state->errout, "\n");
6608 internal_error(state, 0, "offset outside of type");
6609 }
6610 else {
6611 switch(type->type & TYPE_MASK) {
6612 /* Don't do anything with the basic types */
6613 case TYPE_VOID:
6614 case TYPE_CHAR: case TYPE_UCHAR:
6615 case TYPE_SHORT: case TYPE_USHORT:
6616 case TYPE_INT: case TYPE_UINT:
6617 case TYPE_LONG: case TYPE_ULONG:
6618 case TYPE_LLONG: case TYPE_ULLONG:
6619 case TYPE_FLOAT: case TYPE_DOUBLE:
6620 case TYPE_LDOUBLE:
6621 case TYPE_POINTER:
6622 case TYPE_ENUM:
6623 case TYPE_BITFIELD:
6624 member = type;
6625 break;
6626 case TYPE_ARRAY:
6627 member = type->left;
6628 size = reg_size_of(state, member);
6629 if (size > REG_SIZEOF_REG) {
6630 member = reg_type(state, member, reg_offset % size);
6631 }
6632 break;
6633 case TYPE_STRUCT:
6634 case TYPE_TUPLE:
6635 {
6636 size_t offset;
6637 offset = 0;
6638 member = type->left;
6639 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6640 size = reg_size_of(state, member->left);
6641 offset += reg_needed_padding(state, member->left, offset);
6642 if ((offset + size) > reg_offset) {
6643 member = member->left;
6644 break;
6645 }
6646 offset += size;
6647 member = member->right;
6648 }
6649 offset += reg_needed_padding(state, member, offset);
6650 member = reg_type(state, member, reg_offset - offset);
6651 break;
6652 }
6653 case TYPE_UNION:
6654 case TYPE_JOIN:
6655 {
6656 struct type *join, **jnext, *mnext;
6657 join = new_type(TYPE_JOIN, 0, 0);
6658 jnext = &join->left;
6659 mnext = type->left;
6660 while(mnext) {
6661 size_t size;
6662 member = mnext;
6663 mnext = 0;
6664 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6665 mnext = member->right;
6666 member = member->left;
6667 }
6668 size = reg_size_of(state, member);
6669 if (size > reg_offset) {
6670 struct type *part, *hunt;
6671 part = reg_type(state, member, reg_offset);
6672 /* See if this type is already in the union */
6673 hunt = join->left;
6674 while(hunt) {
6675 struct type *test = hunt;
6676 hunt = 0;
6677 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6678 hunt = test->right;
6679 test = test->left;
6680 }
6681 if (equiv_types(part, test)) {
6682 goto next;
6683 }
6684 }
6685 /* Nope add it */
6686 if (!*jnext) {
6687 *jnext = part;
6688 } else {
6689 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6690 jnext = &(*jnext)->right;
6691 }
6692 join->elements++;
6693 }
6694 next:
6695 ;
6696 }
6697 if (join->elements == 0) {
6698 internal_error(state, 0, "No elements?");
6699 }
6700 member = join;
6701 break;
6702 }
6703 default:
6704 member = 0;
6705 fprintf(state->errout, "type: ");
6706 name_of(state->errout, type);
6707 fprintf(state->errout, "\n");
6708 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006709
Eric Biederman90089602004-05-28 14:11:54 +00006710 }
6711 }
6712 /* If I have a single register compound type not a bit-field
6713 * find the real type.
6714 */
6715 member = unpack_type(state, member);
6716 ;
6717 size = reg_size_of(state, member);
6718 if (size > REG_SIZEOF_REG) {
6719 internal_error(state, 0, "Cannot find type of single register");
6720 }
6721#if 1
6722 invalid = invalid_type(state, member);
6723 if (invalid) {
6724 fprintf(state->errout, "type: ");
6725 name_of(state->errout, member);
6726 fprintf(state->errout, "\n");
6727 fprintf(state->errout, "invalid: ");
6728 name_of(state->errout, invalid);
6729 fprintf(state->errout, "\n");
6730 internal_error(state, 0, "returning bad type?");
6731 }
6732#endif
6733 return member;
6734}
6735
Eric Biederman03b59862003-06-24 14:27:37 +00006736static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006737 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006738{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006739 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006740 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6741 internal_error(state, 0, "next_field only works on structures");
6742 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006743 member = type->left;
6744 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006745 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006746 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006747 break;
6748 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006749 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006750 prev_member = 0;
6751 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006752 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006753 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006754 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006755 prev_member = 0;
6756 }
6757 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006758 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006759 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006760 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006761 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006762}
6763
Stefan Reinauer14e22772010-04-27 06:56:47 +00006764typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006765 size_t ret_offset, size_t mem_offset, void *arg);
6766
6767static void walk_type_fields(struct compile_state *state,
6768 struct type *type, size_t reg_offset, size_t mem_offset,
6769 walk_type_fields_cb_t cb, void *arg);
6770
6771static void walk_struct_fields(struct compile_state *state,
6772 struct type *type, size_t reg_offset, size_t mem_offset,
6773 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006774{
Eric Biederman90089602004-05-28 14:11:54 +00006775 struct type *tptr;
6776 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006777 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006778 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006779 }
Eric Biederman90089602004-05-28 14:11:54 +00006780 tptr = type->left;
6781 for(i = 0; i < type->elements; i++) {
6782 struct type *mtype;
6783 mtype = tptr;
6784 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6785 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006786 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006787 walk_type_fields(state, mtype,
6788 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006789 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006790 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006791 field_offset(state, type, mtype->field_ident),
6792 cb, arg);
6793 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006794 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006795
Eric Biederman90089602004-05-28 14:11:54 +00006796}
6797
6798static void walk_type_fields(struct compile_state *state,
6799 struct type *type, size_t reg_offset, size_t mem_offset,
6800 walk_type_fields_cb_t cb, void *arg)
6801{
6802 switch(type->type & TYPE_MASK) {
6803 case TYPE_STRUCT:
6804 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6805 break;
6806 case TYPE_CHAR:
6807 case TYPE_UCHAR:
6808 case TYPE_SHORT:
6809 case TYPE_USHORT:
6810 case TYPE_INT:
6811 case TYPE_UINT:
6812 case TYPE_LONG:
6813 case TYPE_ULONG:
6814 cb(state, type, reg_offset, mem_offset, arg);
6815 break;
6816 case TYPE_VOID:
6817 break;
6818 default:
6819 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006820 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006821}
6822
Eric Biedermanb138ac82003-04-22 18:44:01 +00006823static void arrays_complete(struct compile_state *state, struct type *type)
6824{
6825 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6826 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6827 error(state, 0, "array size not specified");
6828 }
6829 arrays_complete(state, type->left);
6830 }
6831}
6832
Eric Biederman90089602004-05-28 14:11:54 +00006833static unsigned int get_basic_type(struct type *type)
6834{
6835 unsigned int basic;
6836 basic = type->type & TYPE_MASK;
6837 /* Convert enums to ints */
6838 if (basic == TYPE_ENUM) {
6839 basic = TYPE_INT;
6840 }
6841 /* Convert bitfields to standard types */
6842 else if (basic == TYPE_BITFIELD) {
6843 if (type->elements <= SIZEOF_CHAR) {
6844 basic = TYPE_CHAR;
6845 }
6846 else if (type->elements <= SIZEOF_SHORT) {
6847 basic = TYPE_SHORT;
6848 }
6849 else if (type->elements <= SIZEOF_INT) {
6850 basic = TYPE_INT;
6851 }
6852 else if (type->elements <= SIZEOF_LONG) {
6853 basic = TYPE_LONG;
6854 }
6855 if (!TYPE_SIGNED(type->left->type)) {
6856 basic += 1;
6857 }
6858 }
6859 return basic;
6860}
6861
Eric Biedermanb138ac82003-04-22 18:44:01 +00006862static unsigned int do_integral_promotion(unsigned int type)
6863{
Eric Biederman83b991a2003-10-11 06:20:25 +00006864 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006865 type = TYPE_INT;
6866 }
6867 return type;
6868}
6869
6870static unsigned int do_arithmetic_conversion(
6871 unsigned int left, unsigned int right)
6872{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006873 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6874 return TYPE_LDOUBLE;
6875 }
6876 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6877 return TYPE_DOUBLE;
6878 }
6879 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6880 return TYPE_FLOAT;
6881 }
6882 left = do_integral_promotion(left);
6883 right = do_integral_promotion(right);
6884 /* If both operands have the same size done */
6885 if (left == right) {
6886 return left;
6887 }
6888 /* If both operands have the same signedness pick the larger */
6889 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6890 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6891 }
6892 /* If the signed type can hold everything use it */
6893 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6894 return left;
6895 }
6896 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6897 return right;
6898 }
6899 /* Convert to the unsigned type with the same rank as the signed type */
6900 else if (TYPE_SIGNED(left)) {
6901 return TYPE_MKUNSIGNED(left);
6902 }
6903 else {
6904 return TYPE_MKUNSIGNED(right);
6905 }
6906}
6907
6908/* see if two types are the same except for qualifiers */
6909static int equiv_types(struct type *left, struct type *right)
6910{
6911 unsigned int type;
6912 /* Error if the basic types do not match */
6913 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6914 return 0;
6915 }
6916 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006917 /* If the basic types match and it is a void type we are done */
6918 if (type == TYPE_VOID) {
6919 return 1;
6920 }
Eric Biederman90089602004-05-28 14:11:54 +00006921 /* For bitfields we need to compare the sizes */
6922 else if (type == TYPE_BITFIELD) {
6923 return (left->elements == right->elements) &&
6924 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6925 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006926 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006927 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006928 return 1;
6929 }
6930 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006931 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006932 return equiv_types(left->left, right->left);
6933 }
6934 else if (type == TYPE_ARRAY) {
6935 return (left->elements == right->elements) &&
6936 equiv_types(left->left, right->left);
6937 }
Eric Biederman90089602004-05-28 14:11:54 +00006938 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006939 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006940 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006941 }
Eric Biederman90089602004-05-28 14:11:54 +00006942 /* test for union equality */
6943 else if (type == TYPE_UNION) {
6944 return left->type_ident == right->type_ident;
6945 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006946 /* Test for equivalent functions */
6947 else if (type == TYPE_FUNCTION) {
6948 return equiv_types(left->left, right->left) &&
6949 equiv_types(left->right, right->right);
6950 }
6951 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006952 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006953 else if (type == TYPE_PRODUCT) {
6954 return equiv_types(left->left, right->left) &&
6955 equiv_types(left->right, right->right);
6956 }
Eric Biederman90089602004-05-28 14:11:54 +00006957 /* We should see TYPE_OVERLAP when comparing joins */
6958 else if (type == TYPE_OVERLAP) {
6959 return equiv_types(left->left, right->left) &&
6960 equiv_types(left->right, right->right);
6961 }
6962 /* Test for equivalence of tuples */
6963 else if (type == TYPE_TUPLE) {
6964 return (left->elements == right->elements) &&
6965 equiv_types(left->left, right->left);
6966 }
6967 /* Test for equivalence of joins */
6968 else if (type == TYPE_JOIN) {
6969 return (left->elements == right->elements) &&
6970 equiv_types(left->left, right->left);
6971 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006972 else {
6973 return 0;
6974 }
6975}
6976
6977static int equiv_ptrs(struct type *left, struct type *right)
6978{
6979 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6980 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6981 return 0;
6982 }
6983 return equiv_types(left->left, right->left);
6984}
6985
6986static struct type *compatible_types(struct type *left, struct type *right)
6987{
6988 struct type *result;
6989 unsigned int type, qual_type;
6990 /* Error if the basic types do not match */
6991 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6992 return 0;
6993 }
6994 type = left->type & TYPE_MASK;
6995 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6996 result = 0;
6997 /* if the basic types match and it is an arithmetic type we are done */
6998 if (TYPE_ARITHMETIC(type)) {
6999 result = new_type(qual_type, 0, 0);
7000 }
7001 /* If it is a pointer type recurse and keep testing */
7002 else if (type == TYPE_POINTER) {
7003 result = compatible_types(left->left, right->left);
7004 if (result) {
7005 result = new_type(qual_type, result, 0);
7006 }
7007 }
Eric Biederman90089602004-05-28 14:11:54 +00007008 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007009 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007010 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007011 result = left;
7012 }
7013 }
Eric Biederman90089602004-05-28 14:11:54 +00007014 /* test for union equality */
7015 else if (type == TYPE_UNION) {
7016 if (left->type_ident == right->type_ident) {
7017 result = left;
7018 }
7019 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007020 /* Test for equivalent functions */
7021 else if (type == TYPE_FUNCTION) {
7022 struct type *lf, *rf;
7023 lf = compatible_types(left->left, right->left);
7024 rf = compatible_types(left->right, right->right);
7025 if (lf && rf) {
7026 result = new_type(qual_type, lf, rf);
7027 }
7028 }
7029 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7030 else if (type == TYPE_PRODUCT) {
7031 struct type *lf, *rf;
7032 lf = compatible_types(left->left, right->left);
7033 rf = compatible_types(left->right, right->right);
7034 if (lf && rf) {
7035 result = new_type(qual_type, lf, rf);
7036 }
7037 }
7038 else {
7039 /* Nothing else is compatible */
7040 }
7041 return result;
7042}
7043
Eric Biederman90089602004-05-28 14:11:54 +00007044/* See if left is a equivalent to right or right is a union member of left */
7045static int is_subset_type(struct type *left, struct type *right)
7046{
7047 if (equiv_types(left, right)) {
7048 return 1;
7049 }
7050 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7051 struct type *member, *mnext;
7052 mnext = left->left;
7053 while(mnext) {
7054 member = mnext;
7055 mnext = 0;
7056 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7057 mnext = member->right;
7058 member = member->left;
7059 }
7060 if (is_subset_type( member, right)) {
7061 return 1;
7062 }
7063 }
7064 }
7065 return 0;
7066}
7067
Eric Biedermanb138ac82003-04-22 18:44:01 +00007068static struct type *compatible_ptrs(struct type *left, struct type *right)
7069{
7070 struct type *result;
7071 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7072 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7073 return 0;
7074 }
7075 result = compatible_types(left->left, right->left);
7076 if (result) {
7077 unsigned int qual_type;
7078 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7079 result = new_type(qual_type, result, 0);
7080 }
7081 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007082
Eric Biedermanb138ac82003-04-22 18:44:01 +00007083}
7084static struct triple *integral_promotion(
7085 struct compile_state *state, struct triple *def)
7086{
7087 struct type *type;
7088 type = def->type;
7089 /* As all operations are carried out in registers
7090 * the values are converted on load I just convert
7091 * logical type of the operand.
7092 */
7093 if (TYPE_INTEGER(type->type)) {
7094 unsigned int int_type;
7095 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007096 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007097 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007098 if (def->op != OP_LOAD) {
7099 def->type = new_type(int_type, 0, 0);
7100 }
7101 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007102 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007103 new_type(int_type, 0, 0), def, 0);
7104 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007105 }
7106 }
7107 return def;
7108}
7109
7110
7111static void arithmetic(struct compile_state *state, struct triple *def)
7112{
7113 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007114 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007115 }
7116}
7117
7118static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7119{
7120 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7121 error(state, def, "pointer or arithmetic type expected");
7122 }
7123}
7124
7125static int is_integral(struct triple *ins)
7126{
7127 return TYPE_INTEGER(ins->type->type);
7128}
7129
7130static void integral(struct compile_state *state, struct triple *def)
7131{
7132 if (!is_integral(def)) {
7133 error(state, 0, "integral type expected");
7134 }
7135}
7136
7137
7138static void bool(struct compile_state *state, struct triple *def)
7139{
7140 if (!TYPE_ARITHMETIC(def->type->type) &&
7141 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7142 error(state, 0, "arithmetic or pointer type expected");
7143 }
7144}
7145
7146static int is_signed(struct type *type)
7147{
Eric Biederman90089602004-05-28 14:11:54 +00007148 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7149 type = type->left;
7150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007151 return !!TYPE_SIGNED(type->type);
7152}
Eric Biederman90089602004-05-28 14:11:54 +00007153static int is_compound_type(struct type *type)
7154{
7155 int is_compound;
7156 switch((type->type & TYPE_MASK)) {
7157 case TYPE_ARRAY:
7158 case TYPE_STRUCT:
7159 case TYPE_TUPLE:
7160 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007161 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007162 is_compound = 1;
7163 break;
7164 default:
7165 is_compound = 0;
7166 break;
7167 }
7168 return is_compound;
7169}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007170
Eric Biederman0babc1c2003-05-09 02:39:00 +00007171/* Is this value located in a register otherwise it must be in memory */
7172static int is_in_reg(struct compile_state *state, struct triple *def)
7173{
7174 int in_reg;
7175 if (def->op == OP_ADECL) {
7176 in_reg = 1;
7177 }
7178 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7179 in_reg = 0;
7180 }
Eric Biederman90089602004-05-28 14:11:54 +00007181 else if (triple_is_part(state, def)) {
7182 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007183 }
7184 else {
Eric Biederman90089602004-05-28 14:11:54 +00007185 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007186 in_reg = -1;
7187 }
7188 return in_reg;
7189}
7190
Eric Biederman90089602004-05-28 14:11:54 +00007191/* Is this an auto or static variable location? Something that can
7192 * be assigned to. Otherwise it must must be a pure value, a temporary.
7193 */
7194static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007195{
7196 int ret;
7197 ret = 0;
7198 if (!def) {
7199 return 0;
7200 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007201 if ((def->op == OP_ADECL) ||
7202 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007203 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007204 (def->op == OP_BLOBCONST) ||
7205 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007206 ret = 1;
7207 }
Eric Biederman90089602004-05-28 14:11:54 +00007208 else if (triple_is_part(state, def)) {
7209 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007210 }
7211 return ret;
7212}
7213
Eric Biederman00443072003-06-24 12:34:45 +00007214static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007215{
7216 if (!def) {
7217 internal_error(state, def, "nothing where lvalue expected?");
7218 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007219 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007220 error(state, def, "lvalue expected");
7221 }
7222}
Eric Biederman00443072003-06-24 12:34:45 +00007223static void lvalue(struct compile_state *state, struct triple *def)
7224{
7225 clvalue(state, def);
7226 if (def->type->type & QUAL_CONST) {
7227 error(state, def, "modifable lvalue expected");
7228 }
7229}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007230
7231static int is_pointer(struct triple *def)
7232{
7233 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7234}
7235
7236static void pointer(struct compile_state *state, struct triple *def)
7237{
7238 if (!is_pointer(def)) {
7239 error(state, def, "pointer expected");
7240 }
7241}
7242
7243static struct triple *int_const(
7244 struct compile_state *state, struct type *type, ulong_t value)
7245{
7246 struct triple *result;
7247 switch(type->type & TYPE_MASK) {
7248 case TYPE_CHAR:
7249 case TYPE_INT: case TYPE_UINT:
7250 case TYPE_LONG: case TYPE_ULONG:
7251 break;
7252 default:
Eric Biederman90089602004-05-28 14:11:54 +00007253 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007254 }
7255 result = triple(state, OP_INTCONST, type, 0, 0);
7256 result->u.cval = value;
7257 return result;
7258}
7259
7260
Eric Biederman83b991a2003-10-11 06:20:25 +00007261static struct triple *read_expr(struct compile_state *state, struct triple *def);
7262
Stefan Reinauer14e22772010-04-27 06:56:47 +00007263static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007264 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007265{
7266 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007267 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007268 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007269
Eric Biederman90089602004-05-28 14:11:54 +00007270 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007271
Stefan Reinauer14e22772010-04-27 06:56:47 +00007272
Eric Biedermanb138ac82003-04-22 18:44:01 +00007273 result = 0;
7274 if (expr->op == OP_ADECL) {
7275 error(state, expr, "address of auto variables not supported");
7276 }
7277 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007278 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007279 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007280 result->u.cval = offset;
7281 }
7282 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007283 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007284 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007285 int_const(state, &ulong_type, offset));
7286 }
Eric Biederman90089602004-05-28 14:11:54 +00007287 else if (expr->op == OP_BLOBCONST) {
7288 FINISHME();
7289 internal_error(state, expr, "not yet implemented");
7290 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007291 else if (expr->op == OP_LIST) {
7292 error(state, 0, "Function addresses not supported");
7293 }
Eric Biederman90089602004-05-28 14:11:54 +00007294 else if (triple_is_part(state, expr)) {
7295 struct triple *part;
7296 part = expr;
7297 expr = MISC(expr, 0);
7298 if (part->op == OP_DOT) {
7299 offset += bits_to_bytes(
7300 field_offset(state, expr->type, part->u.field));
7301 }
7302 else if (part->op == OP_INDEX) {
7303 offset += bits_to_bytes(
7304 index_offset(state, expr->type, part->u.cval));
7305 }
7306 else {
7307 internal_error(state, part, "unhandled part type");
7308 }
7309 result = do_mk_addr_expr(state, expr, type, offset);
7310 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007311 if (!result) {
7312 internal_error(state, expr, "cannot take address of expression");
7313 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007314 return result;
7315}
7316
Eric Biederman0babc1c2003-05-09 02:39:00 +00007317static struct triple *mk_addr_expr(
7318 struct compile_state *state, struct triple *expr, ulong_t offset)
7319{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007320 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007321}
7322
Eric Biedermanb138ac82003-04-22 18:44:01 +00007323static struct triple *mk_deref_expr(
7324 struct compile_state *state, struct triple *expr)
7325{
7326 struct type *base_type;
7327 pointer(state, expr);
7328 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007329 return triple(state, OP_DEREF, base_type, expr, 0);
7330}
7331
Eric Biederman90089602004-05-28 14:11:54 +00007332/* lvalue conversions always apply except when certain operators
7333 * are applied. So I apply apply it when I know no more
7334 * operators will be applied.
7335 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007336static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007337{
Eric Biederman5cd81732004-03-11 15:01:31 +00007338 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007339 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7340 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007341 type = new_type(
7342 TYPE_POINTER | (def->type->type & QUAL_MASK),
7343 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007344 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007345 struct triple *addrconst;
7346 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7347 internal_error(state, def, "bad array constant");
7348 }
7349 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7350 MISC(addrconst, 0) = def;
7351 def = addrconst;
7352 }
7353 else {
Eric Biederman90089602004-05-28 14:11:54 +00007354 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007355 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007356 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007357 /* Transform a function to a pointer to it */
7358 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7359 def = mk_addr_expr(state, def, 0);
7360 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007361 return def;
7362}
7363
Eric Biederman0babc1c2003-05-09 02:39:00 +00007364static struct triple *deref_field(
7365 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7366{
7367 struct triple *result;
7368 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007369 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007370 if (!field) {
7371 internal_error(state, 0, "No field passed to deref_field");
7372 }
7373 result = 0;
7374 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007375 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7376 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007377 error(state, 0, "request for member %s in something not a struct or union",
7378 field->name);
7379 }
Eric Biederman03b59862003-06-24 14:27:37 +00007380 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007381 if ((type->type & STOR_MASK) == STOR_PERM) {
7382 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007383 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007384 result = do_mk_addr_expr(state, expr, member, offset);
7385 result = mk_deref_expr(state, result);
7386 }
7387 else {
7388 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007389 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007390 result->u.field = field;
7391 }
7392 return result;
7393}
7394
Eric Biederman90089602004-05-28 14:11:54 +00007395static struct triple *deref_index(
7396 struct compile_state *state, struct triple *expr, size_t index)
7397{
7398 struct triple *result;
7399 struct type *type, *member;
7400 ulong_t offset;
7401
7402 result = 0;
7403 type = expr->type;
7404 member = index_type(state, type, index);
7405
7406 if ((type->type & STOR_MASK) == STOR_PERM) {
7407 offset = bits_to_bytes(index_offset(state, type, index));
7408 result = do_mk_addr_expr(state, expr, member, offset);
7409 result = mk_deref_expr(state, result);
7410 }
7411 else {
7412 result = triple(state, OP_INDEX, member, expr, 0);
7413 result->u.cval = index;
7414 }
7415 return result;
7416}
7417
Eric Biedermanb138ac82003-04-22 18:44:01 +00007418static struct triple *read_expr(struct compile_state *state, struct triple *def)
7419{
7420 int op;
7421 if (!def) {
7422 return 0;
7423 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007424#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007425#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007426#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007427 /* Transform lvalues into something we can read */
7428 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007429 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007430 return def;
7431 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007432 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007433 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007434 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007435 if (def->op == OP_SDECL) {
7436 def = mk_addr_expr(state, def, 0);
7437 def = mk_deref_expr(state, def);
7438 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007439 op = OP_LOAD;
7440 }
Eric Biederman90089602004-05-28 14:11:54 +00007441 def = triple(state, op, def->type, def, 0);
7442 if (def->type->type & QUAL_VOLATILE) {
7443 def->id |= TRIPLE_FLAG_VOLATILE;
7444 }
7445 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007446}
7447
Stefan Reinauer14e22772010-04-27 06:56:47 +00007448int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007449 struct type *dest, struct type *rval)
7450{
7451 int compatible = 0;
7452 /* Both operands have arithmetic type */
7453 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7454 compatible = 1;
7455 }
7456 /* One operand is a pointer and the other is a pointer to void */
7457 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7458 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7459 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7460 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7461 compatible = 1;
7462 }
7463 /* If both types are the same without qualifiers we are good */
7464 else if (equiv_ptrs(dest, rval)) {
7465 compatible = 1;
7466 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007467 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007468 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007469 compatible = 1;
7470 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007471 return compatible;
7472}
7473
Eric Biedermane058a1e2003-07-12 01:21:31 +00007474static void write_compatible(struct compile_state *state,
7475 struct type *dest, struct type *rval)
7476{
7477 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007478 FILE *fp = state->errout;
7479 fprintf(fp, "dest: ");
7480 name_of(fp, dest);
7481 fprintf(fp,"\nrval: ");
7482 name_of(fp, rval);
7483 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007484 error(state, 0, "Incompatible types in assignment");
7485 }
7486}
7487
Eric Biedermane058a1e2003-07-12 01:21:31 +00007488static int is_init_compatible(struct compile_state *state,
7489 struct type *dest, struct type *rval)
7490{
7491 int compatible = 0;
7492 if (is_write_compatible(state, dest, rval)) {
7493 compatible = 1;
7494 }
7495 else if (equiv_types(dest, rval)) {
7496 compatible = 1;
7497 }
7498 return compatible;
7499}
7500
Eric Biedermanb138ac82003-04-22 18:44:01 +00007501static struct triple *write_expr(
7502 struct compile_state *state, struct triple *dest, struct triple *rval)
7503{
7504 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007505
7506 def = 0;
7507 if (!rval) {
7508 internal_error(state, 0, "missing rval");
7509 }
7510
7511 if (rval->op == OP_LIST) {
7512 internal_error(state, 0, "expression of type OP_LIST?");
7513 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007514 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007515 internal_error(state, 0, "writing to a non lvalue?");
7516 }
Eric Biederman00443072003-06-24 12:34:45 +00007517 if (dest->type->type & QUAL_CONST) {
7518 internal_error(state, 0, "modifable lvalue expexted");
7519 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007520
7521 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007522 if (!equiv_types(dest->type, rval->type)) {
7523 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7524 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007525
7526 /* Now figure out which assignment operator to use */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007527 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007528 def = triple(state, OP_WRITE, dest->type, rval, dest);
7529 if (MISC(def, 0) != dest) {
7530 internal_error(state, def, "huh?");
7531 }
7532 if (RHS(def, 0) != rval) {
7533 internal_error(state, def, "huh?");
7534 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007535 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007536 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007537 }
Eric Biederman90089602004-05-28 14:11:54 +00007538 if (def->type->type & QUAL_VOLATILE) {
7539 def->id |= TRIPLE_FLAG_VOLATILE;
7540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007541 return def;
7542}
7543
7544static struct triple *init_expr(
7545 struct compile_state *state, struct triple *dest, struct triple *rval)
7546{
7547 struct triple *def;
7548
7549 def = 0;
7550 if (!rval) {
7551 internal_error(state, 0, "missing rval");
7552 }
7553 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7554 rval = read_expr(state, rval);
7555 def = write_expr(state, dest, rval);
7556 }
7557 else {
7558 /* Fill in the array size if necessary */
7559 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7560 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7561 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7562 dest->type->elements = rval->type->elements;
7563 }
7564 }
7565 if (!equiv_types(dest->type, rval->type)) {
7566 error(state, 0, "Incompatible types in inializer");
7567 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007568 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007569 insert_triple(state, dest, rval);
7570 rval->id |= TRIPLE_FLAG_FLATTENED;
7571 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007572 }
7573 return def;
7574}
7575
7576struct type *arithmetic_result(
7577 struct compile_state *state, struct triple *left, struct triple *right)
7578{
7579 struct type *type;
7580 /* Sanity checks to ensure I am working with arithmetic types */
7581 arithmetic(state, left);
7582 arithmetic(state, right);
7583 type = new_type(
7584 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007585 get_basic_type(left->type),
7586 get_basic_type(right->type)),
7587 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007588 return type;
7589}
7590
7591struct type *ptr_arithmetic_result(
7592 struct compile_state *state, struct triple *left, struct triple *right)
7593{
7594 struct type *type;
7595 /* Sanity checks to ensure I am working with the proper types */
7596 ptr_arithmetic(state, left);
7597 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007598 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007599 TYPE_ARITHMETIC(right->type->type)) {
7600 type = arithmetic_result(state, left, right);
7601 }
7602 else if (TYPE_PTR(left->type->type)) {
7603 type = left->type;
7604 }
7605 else {
7606 internal_error(state, 0, "huh?");
7607 type = 0;
7608 }
7609 return type;
7610}
7611
Eric Biedermanb138ac82003-04-22 18:44:01 +00007612/* boolean helper function */
7613
Stefan Reinauer14e22772010-04-27 06:56:47 +00007614static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007615 struct triple *expr)
7616{
7617 switch(expr->op) {
7618 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7619 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7620 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7621 /* If the expression is already boolean do nothing */
7622 break;
7623 default:
7624 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7625 break;
7626 }
7627 return expr;
7628}
7629
Stefan Reinauer14e22772010-04-27 06:56:47 +00007630static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007631 struct triple *expr)
7632{
7633 return triple(state, OP_LFALSE, &int_type, expr, 0);
7634}
7635
Eric Biederman90089602004-05-28 14:11:54 +00007636static struct triple *mkland_expr(
7637 struct compile_state *state,
7638 struct triple *left, struct triple *right)
7639{
7640 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007641 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007642
7643 /* Generate some intermediate triples */
7644 end = label(state);
7645 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007646
Eric Biederman90089602004-05-28 14:11:54 +00007647 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007648 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007649
7650 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007651 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007652 lfalse_expr(state, read_expr(state, var)));
7653 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007654
Eric Biederman90089602004-05-28 14:11:54 +00007655 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007656 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007657
7658 /* An expression for the computed value */
7659 val = read_expr(state, var);
7660
7661 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007662 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007663
Eric Biederman90089602004-05-28 14:11:54 +00007664 return def;
7665}
7666
7667static struct triple *mklor_expr(
7668 struct compile_state *state,
7669 struct triple *left, struct triple *right)
7670{
7671 struct triple *def, *val, *var, *jmp, *mid, *end;
7672
7673 /* Generate some intermediate triples */
7674 end = label(state);
7675 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007676
Eric Biederman90089602004-05-28 14:11:54 +00007677 /* Store the left hand side value */
7678 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007679
Eric Biederman90089602004-05-28 14:11:54 +00007680 /* Jump if the value is true */
7681 jmp = branch(state, end, read_expr(state, var));
7682 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007683
Eric Biederman90089602004-05-28 14:11:54 +00007684 /* Store the right hand side value */
7685 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007686
Eric Biederman90089602004-05-28 14:11:54 +00007687 /* An expression for the computed value*/
7688 val = read_expr(state, var);
7689
7690 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007691 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007692
7693 return def;
7694}
7695
7696static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007697 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007698 struct triple *test, struct triple *left, struct triple *right)
7699{
Eric Biederman90089602004-05-28 14:11:54 +00007700 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007701 struct type *result_type;
7702 unsigned int left_type, right_type;
7703 bool(state, test);
7704 left_type = left->type->type;
7705 right_type = right->type->type;
7706 result_type = 0;
7707 /* Both operands have arithmetic type */
7708 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7709 result_type = arithmetic_result(state, left, right);
7710 }
7711 /* Both operands have void type */
7712 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7713 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7714 result_type = &void_type;
7715 }
7716 /* pointers to the same type... */
7717 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7718 ;
7719 }
7720 /* Both operands are pointers and left is a pointer to void */
7721 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7722 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7723 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7724 result_type = right->type;
7725 }
7726 /* Both operands are pointers and right is a pointer to void */
7727 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7728 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7729 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7730 result_type = left->type;
7731 }
7732 if (!result_type) {
7733 error(state, 0, "Incompatible types in conditional expression");
7734 }
Eric Biederman90089602004-05-28 14:11:54 +00007735 /* Generate some intermediate triples */
7736 mid = label(state);
7737 end = label(state);
7738 var = variable(state, result_type);
7739
7740 /* Branch if the test is false */
7741 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7742 top = label(state);
7743
7744 /* Store the left hand side value */
7745 left = write_expr(state, var, left);
7746
7747 /* Branch to the end */
7748 jmp2 = branch(state, end, 0);
7749
7750 /* Store the right hand side value */
7751 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007752
Eric Biederman90089602004-05-28 14:11:54 +00007753 /* An expression for the computed value */
7754 val = read_expr(state, var);
7755
7756 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007757 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007758
Eric Biedermanb138ac82003-04-22 18:44:01 +00007759 return def;
7760}
7761
7762
Eric Biederman0babc1c2003-05-09 02:39:00 +00007763static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007764{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007765#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007766#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007767#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007768 int count;
7769 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007770 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7771 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007772 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007773 else if (ins->op == OP_DEREF) {
7774 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007775 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007776 else if (ins->op == OP_VAL) {
7777 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007778 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007779 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780 /* Don't figure the depth of a call just guess it is huge */
7781 count = 1000;
7782 }
7783 else {
7784 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007785 expr = triple_rhs(state, ins, 0);
7786 for(;expr; expr = triple_rhs(state, ins, expr)) {
7787 if (*expr) {
7788 int depth;
7789 depth = expr_depth(state, *expr);
7790 if (depth > count) {
7791 count = depth;
7792 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007793 }
7794 }
7795 }
7796 return count + 1;
7797}
7798
Eric Biederman0babc1c2003-05-09 02:39:00 +00007799static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007800 struct compile_state *state, struct triple *first, struct triple *ptr,
7801 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007802{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007803 struct rhs_vector {
7804 int depth;
7805 struct triple **ins;
7806 } vector[MAX_RHS];
7807 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007808 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007809 rhs = ptr->rhs;
7810 lhs = ptr->lhs;
7811 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007812 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007813 ptr->op, tops(ptr->op));
7814 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007815 /* Find the depth of the rhs elements */
7816 for(i = 0; i < rhs; i++) {
7817 vector[i].ins = &RHS(ptr, i);
7818 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007819 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007820 /* Selection sort the rhs */
7821 for(i = 0; i < rhs; i++) {
7822 int j, max = i;
7823 for(j = i + 1; j < rhs; j++ ) {
7824 if (vector[j].depth > vector[max].depth) {
7825 max = j;
7826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007827 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007828 if (max != i) {
7829 struct rhs_vector tmp;
7830 tmp = vector[i];
7831 vector[i] = vector[max];
7832 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007833 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007834 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007835 /* Now flatten the rhs elements */
7836 for(i = 0; i < rhs; i++) {
7837 *vector[i].ins = flatten(state, first, *vector[i].ins);
7838 use_triple(*vector[i].ins, ptr);
7839 }
Eric Biederman90089602004-05-28 14:11:54 +00007840 if (lhs) {
7841 insert_triple(state, first, ptr);
7842 ptr->id |= TRIPLE_FLAG_FLATTENED;
7843 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007844
Eric Biederman90089602004-05-28 14:11:54 +00007845 /* Now flatten the lhs elements */
7846 for(i = 0; i < lhs; i++) {
7847 struct triple **ins = &LHS(ptr, i);
7848 *ins = flatten(state, first, *ins);
7849 use_triple(*ins, ptr);
7850 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007851 }
7852 return ptr;
7853}
7854
Eric Biederman90089602004-05-28 14:11:54 +00007855static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007856 struct compile_state *state, struct triple *first, struct triple *ptr)
7857{
Eric Biederman90089602004-05-28 14:11:54 +00007858 struct triple *head, *body, *val;
7859 head = RHS(ptr, 0);
7860 RHS(ptr, 0) = 0;
7861 val = head->prev;
7862 body = head->next;
7863 release_triple(state, head);
7864 release_triple(state, ptr);
7865 val->next = first;
7866 body->prev = first->prev;
7867 body->prev->next = body;
7868 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007869
Eric Biederman90089602004-05-28 14:11:54 +00007870 if (triple_is_cbranch(state, body->prev) ||
7871 triple_is_call(state, body->prev)) {
7872 unuse_triple(first, body->prev);
7873 use_triple(body, body->prev);
7874 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007875
Eric Biederman90089602004-05-28 14:11:54 +00007876 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7877 internal_error(state, val, "val not flattened?");
7878 }
7879
7880 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007881}
7882
Eric Biederman90089602004-05-28 14:11:54 +00007883
7884static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007885 struct compile_state *state, struct triple *first, struct triple *ptr)
7886{
Eric Biederman90089602004-05-28 14:11:54 +00007887 if (!triple_is_part(state, ptr)) {
7888 internal_error(state, ptr, "not a part");
7889 }
7890 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7891 internal_error(state, ptr, "unexpected args for: %d %s",
7892 ptr->op, tops(ptr->op));
7893 }
7894 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7895 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007896 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007897}
7898
7899static struct triple *flatten(
7900 struct compile_state *state, struct triple *first, struct triple *ptr)
7901{
7902 struct triple *orig_ptr;
7903 if (!ptr)
7904 return 0;
7905 do {
7906 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007907 /* Only flatten triples once */
7908 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7909 return ptr;
7910 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007911 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007912 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007913 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7914 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007915 break;
Eric Biederman90089602004-05-28 14:11:54 +00007916 case OP_PROG:
7917 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007918 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007919 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007920 ptr = flatten_generic(state, first, ptr, 1);
7921 insert_triple(state, first, ptr);
7922 ptr->id |= TRIPLE_FLAG_FLATTENED;
7923 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7924 if (ptr->next != ptr) {
7925 use_triple(ptr->next, ptr);
7926 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007927 break;
7928 case OP_READ:
7929 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007930 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7931 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007932 break;
Eric Biederman90089602004-05-28 14:11:54 +00007933 case OP_WRITE:
7934 ptr = flatten_generic(state, first, ptr, 1);
7935 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7936 use_triple(MISC(ptr, 0), ptr);
7937 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007938 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007939 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007940 break;
7941 case OP_CBRANCH:
7942 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7943 use_triple(RHS(ptr, 0), ptr);
7944 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007945 insert_triple(state, first, ptr);
7946 ptr->id |= TRIPLE_FLAG_FLATTENED;
7947 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007948 if (ptr->next != ptr) {
7949 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007950 }
7951 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007952 case OP_CALL:
7953 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7954 use_triple(MISC(ptr, 0), ptr);
7955 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007956 insert_triple(state, first, ptr);
7957 ptr->id |= TRIPLE_FLAG_FLATTENED;
7958 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007959 if (ptr->next != ptr) {
7960 use_triple(ptr->next, ptr);
7961 }
7962 break;
7963 case OP_RET:
7964 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7965 use_triple(RHS(ptr, 0), ptr);
7966 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007967 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007968 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007969 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007970 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007971 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007972 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007973 break;
7974 case OP_DEREF:
7975 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007976 ptr = RHS(ptr, 0);
7977 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007978 free_triple(state, orig_ptr);
7979 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007980 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007981 if (RHS(ptr, 0)->op == OP_DEREF) {
7982 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007983 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007984 base = MISC(ptr, 0);
7985 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007986 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007987 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007988 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007989 int_const(state, &ulong_type, offset));
7990 free_triple(state, base);
7991 }
Eric Biederman90089602004-05-28 14:11:54 +00007992 else {
7993 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007995 break;
Eric Biederman90089602004-05-28 14:11:54 +00007996 case OP_INDEX:
7997 if (RHS(ptr, 0)->op == OP_DEREF) {
7998 struct triple *base, *left;
7999 ulong_t offset;
8000 base = MISC(ptr, 0);
8001 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8002 left = RHS(base, 0);
8003 ptr = triple(state, OP_ADD, left->type,
8004 read_expr(state, left),
8005 int_const(state, &long_type, offset));
8006 free_triple(state, base);
8007 }
8008 else {
8009 ptr = flatten_part(state, first, ptr);
8010 }
8011 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008012 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008013 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008014 use_triple(ptr, MISC(ptr, 0));
8015 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008016 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008017 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8018 use_triple(MISC(ptr, 0), ptr);
8019 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008020 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008021 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008022 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8023 use_triple(MISC(ptr, 0), ptr);
8024 insert_triple(state, first, ptr);
8025 ptr->id |= TRIPLE_FLAG_FLATTENED;
8026 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8027 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008028 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008029 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008030 break;
8031 default:
8032 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008033 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008034 break;
8035 }
8036 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008037 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008038 insert_triple(state, first, ptr);
8039 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008040 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008042 return ptr;
8043}
8044
8045static void release_expr(struct compile_state *state, struct triple *expr)
8046{
8047 struct triple *head;
8048 head = label(state);
8049 flatten(state, head, expr);
8050 while(head->next != head) {
8051 release_triple(state, head->next);
8052 }
8053 free_triple(state, head);
8054}
8055
8056static int replace_rhs_use(struct compile_state *state,
8057 struct triple *orig, struct triple *new, struct triple *use)
8058{
8059 struct triple **expr;
8060 int found;
8061 found = 0;
8062 expr = triple_rhs(state, use, 0);
8063 for(;expr; expr = triple_rhs(state, use, expr)) {
8064 if (*expr == orig) {
8065 *expr = new;
8066 found = 1;
8067 }
8068 }
8069 if (found) {
8070 unuse_triple(orig, use);
8071 use_triple(new, use);
8072 }
8073 return found;
8074}
8075
8076static int replace_lhs_use(struct compile_state *state,
8077 struct triple *orig, struct triple *new, struct triple *use)
8078{
8079 struct triple **expr;
8080 int found;
8081 found = 0;
8082 expr = triple_lhs(state, use, 0);
8083 for(;expr; expr = triple_lhs(state, use, expr)) {
8084 if (*expr == orig) {
8085 *expr = new;
8086 found = 1;
8087 }
8088 }
8089 if (found) {
8090 unuse_triple(orig, use);
8091 use_triple(new, use);
8092 }
8093 return found;
8094}
8095
Eric Biederman90089602004-05-28 14:11:54 +00008096static int replace_misc_use(struct compile_state *state,
8097 struct triple *orig, struct triple *new, struct triple *use)
8098{
8099 struct triple **expr;
8100 int found;
8101 found = 0;
8102 expr = triple_misc(state, use, 0);
8103 for(;expr; expr = triple_misc(state, use, expr)) {
8104 if (*expr == orig) {
8105 *expr = new;
8106 found = 1;
8107 }
8108 }
8109 if (found) {
8110 unuse_triple(orig, use);
8111 use_triple(new, use);
8112 }
8113 return found;
8114}
8115
8116static int replace_targ_use(struct compile_state *state,
8117 struct triple *orig, struct triple *new, struct triple *use)
8118{
8119 struct triple **expr;
8120 int found;
8121 found = 0;
8122 expr = triple_targ(state, use, 0);
8123 for(;expr; expr = triple_targ(state, use, expr)) {
8124 if (*expr == orig) {
8125 *expr = new;
8126 found = 1;
8127 }
8128 }
8129 if (found) {
8130 unuse_triple(orig, use);
8131 use_triple(new, use);
8132 }
8133 return found;
8134}
8135
8136static void replace_use(struct compile_state *state,
8137 struct triple *orig, struct triple *new, struct triple *use)
8138{
8139 int found;
8140 found = 0;
8141 found |= replace_rhs_use(state, orig, new, use);
8142 found |= replace_lhs_use(state, orig, new, use);
8143 found |= replace_misc_use(state, orig, new, use);
8144 found |= replace_targ_use(state, orig, new, use);
8145 if (!found) {
8146 internal_error(state, use, "use without use");
8147 }
8148}
8149
Eric Biedermanb138ac82003-04-22 18:44:01 +00008150static void propogate_use(struct compile_state *state,
8151 struct triple *orig, struct triple *new)
8152{
8153 struct triple_set *user, *next;
8154 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008155 /* Careful replace_use modifies the use chain and
8156 * removes use. So we must get a copy of the next
8157 * entry early.
8158 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008159 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008160 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008161 }
8162 if (orig->use) {
8163 internal_error(state, orig, "used after propogate_use");
8164 }
8165}
8166
8167/*
8168 * Code generators
8169 * ===========================
8170 */
8171
Eric Biederman90089602004-05-28 14:11:54 +00008172static struct triple *mk_cast_expr(
8173 struct compile_state *state, struct type *type, struct triple *expr)
8174{
8175 struct triple *def;
8176 def = read_expr(state, expr);
8177 def = triple(state, OP_CONVERT, type, def, 0);
8178 return def;
8179}
8180
Eric Biedermanb138ac82003-04-22 18:44:01 +00008181static struct triple *mk_add_expr(
8182 struct compile_state *state, struct triple *left, struct triple *right)
8183{
8184 struct type *result_type;
8185 /* Put pointer operands on the left */
8186 if (is_pointer(right)) {
8187 struct triple *tmp;
8188 tmp = left;
8189 left = right;
8190 right = tmp;
8191 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008192 left = read_expr(state, left);
8193 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008194 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008195 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008196 struct type *ptr_math;
8197 int op;
8198 if (is_signed(right->type)) {
8199 ptr_math = &long_type;
8200 op = OP_SMUL;
8201 } else {
8202 ptr_math = &ulong_type;
8203 op = OP_UMUL;
8204 }
8205 if (!equiv_types(right->type, ptr_math)) {
8206 right = mk_cast_expr(state, ptr_math, right);
8207 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008208 right = triple(state, op, ptr_math, right,
8209 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008210 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008211 }
8212 return triple(state, OP_ADD, result_type, left, right);
8213}
8214
8215static struct triple *mk_sub_expr(
8216 struct compile_state *state, struct triple *left, struct triple *right)
8217{
8218 struct type *result_type;
8219 result_type = ptr_arithmetic_result(state, left, right);
8220 left = read_expr(state, left);
8221 right = read_expr(state, right);
8222 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008223 struct type *ptr_math;
8224 int op;
8225 if (is_signed(right->type)) {
8226 ptr_math = &long_type;
8227 op = OP_SMUL;
8228 } else {
8229 ptr_math = &ulong_type;
8230 op = OP_UMUL;
8231 }
8232 if (!equiv_types(right->type, ptr_math)) {
8233 right = mk_cast_expr(state, ptr_math, right);
8234 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008235 right = triple(state, op, ptr_math, right,
8236 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008237 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008238 }
8239 return triple(state, OP_SUB, result_type, left, right);
8240}
8241
8242static struct triple *mk_pre_inc_expr(
8243 struct compile_state *state, struct triple *def)
8244{
8245 struct triple *val;
8246 lvalue(state, def);
8247 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8248 return triple(state, OP_VAL, def->type,
8249 write_expr(state, def, val),
8250 val);
8251}
8252
8253static struct triple *mk_pre_dec_expr(
8254 struct compile_state *state, struct triple *def)
8255{
8256 struct triple *val;
8257 lvalue(state, def);
8258 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8259 return triple(state, OP_VAL, def->type,
8260 write_expr(state, def, val),
8261 val);
8262}
8263
8264static struct triple *mk_post_inc_expr(
8265 struct compile_state *state, struct triple *def)
8266{
8267 struct triple *val;
8268 lvalue(state, def);
8269 val = read_expr(state, def);
8270 return triple(state, OP_VAL, def->type,
8271 write_expr(state, def,
8272 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8273 , val);
8274}
8275
8276static struct triple *mk_post_dec_expr(
8277 struct compile_state *state, struct triple *def)
8278{
8279 struct triple *val;
8280 lvalue(state, def);
8281 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008282 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008283 write_expr(state, def,
8284 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8285 , val);
8286}
8287
8288static struct triple *mk_subscript_expr(
8289 struct compile_state *state, struct triple *left, struct triple *right)
8290{
8291 left = read_expr(state, left);
8292 right = read_expr(state, right);
8293 if (!is_pointer(left) && !is_pointer(right)) {
8294 error(state, left, "subscripted value is not a pointer");
8295 }
8296 return mk_deref_expr(state, mk_add_expr(state, left, right));
8297}
8298
Eric Biedermane058a1e2003-07-12 01:21:31 +00008299
Eric Biedermanb138ac82003-04-22 18:44:01 +00008300/*
8301 * Compile time evaluation
8302 * ===========================
8303 */
8304static int is_const(struct triple *ins)
8305{
8306 return IS_CONST_OP(ins->op);
8307}
8308
Eric Biederman83b991a2003-10-11 06:20:25 +00008309static int is_simple_const(struct triple *ins)
8310{
Eric Biederman90089602004-05-28 14:11:54 +00008311 /* Is this a constant that u.cval has the value.
8312 * Or equivalently is this a constant that read_const
8313 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008314 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008315 */
8316 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008317}
8318
Stefan Reinauer14e22772010-04-27 06:56:47 +00008319static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008320 struct triple *left, struct triple *right)
8321{
8322 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008323 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8324 equal = 0;
8325 }
8326 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008327 equal = 0;
8328 }
8329 else if (left->op != right->op) {
8330 equal = 0;
8331 }
8332 else if (!equiv_types(left->type, right->type)) {
8333 equal = 0;
8334 }
8335 else {
8336 equal = 0;
8337 switch(left->op) {
8338 case OP_INTCONST:
8339 if (left->u.cval == right->u.cval) {
8340 equal = 1;
8341 }
8342 break;
8343 case OP_BLOBCONST:
8344 {
Eric Biederman90089602004-05-28 14:11:54 +00008345 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008346 lsize = size_of(state, left->type);
8347 rsize = size_of(state, right->type);
8348 if (lsize != rsize) {
8349 break;
8350 }
Eric Biederman90089602004-05-28 14:11:54 +00008351 bytes = bits_to_bytes(lsize);
8352 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008353 equal = 1;
8354 }
8355 break;
8356 }
8357 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008358 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008359 (left->u.cval == right->u.cval)) {
8360 equal = 1;
8361 }
8362 break;
8363 default:
8364 internal_error(state, left, "uknown constant type");
8365 break;
8366 }
8367 }
8368 return equal;
8369}
8370
8371static int is_zero(struct triple *ins)
8372{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008373 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008374}
8375
8376static int is_one(struct triple *ins)
8377{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008378 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008379}
8380
Stefan Reinauer50542a82007-10-24 11:14:14 +00008381#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008382static long_t bit_count(ulong_t value)
8383{
8384 int count;
8385 int i;
8386 count = 0;
8387 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8388 ulong_t mask;
8389 mask = 1;
8390 mask <<= i;
8391 if (value & mask) {
8392 count++;
8393 }
8394 }
8395 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008396
Eric Biederman530b5192003-07-01 10:05:30 +00008397}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008398#endif
8399
Eric Biedermanb138ac82003-04-22 18:44:01 +00008400static long_t bsr(ulong_t value)
8401{
8402 int i;
8403 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8404 ulong_t mask;
8405 mask = 1;
8406 mask <<= i;
8407 if (value & mask) {
8408 return i;
8409 }
8410 }
8411 return -1;
8412}
8413
8414static long_t bsf(ulong_t value)
8415{
8416 int i;
8417 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8418 ulong_t mask;
8419 mask = 1;
8420 mask <<= 1;
8421 if (value & mask) {
8422 return i;
8423 }
8424 }
8425 return -1;
8426}
8427
Eric Biedermancb364952004-11-15 10:46:44 +00008428static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008429{
8430 return bsr(value);
8431}
8432
8433static long_t tlog2(struct triple *ins)
8434{
Eric Biedermancb364952004-11-15 10:46:44 +00008435 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008436}
8437
8438static int is_pow2(struct triple *ins)
8439{
8440 ulong_t value, mask;
8441 long_t log;
8442 if (!is_const(ins)) {
8443 return 0;
8444 }
8445 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008446 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008447 if (log == -1) {
8448 return 0;
8449 }
8450 mask = 1;
8451 mask <<= log;
8452 return ((value & mask) == value);
8453}
8454
8455static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008456 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008457{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008458 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008459 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008460 case TYPE_SHORT:
8461 case TYPE_INT:
8462 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008463 case TYPE_UCHAR:
8464 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008465 case TYPE_UINT:
8466 case TYPE_ULONG:
8467 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008468 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008469 break;
8470 default:
Eric Biederman90089602004-05-28 14:11:54 +00008471 fprintf(state->errout, "type: ");
8472 name_of(state->errout, rhs->type);
8473 fprintf(state->errout, "\n");
8474 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008475 break;
8476 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008477 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008478 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008479 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008480 return rhs->u.cval;
8481}
8482
Eric Biederman5ade04a2003-10-22 04:03:46 +00008483static long_t read_sconst(struct compile_state *state,
8484 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008485{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008486 return (long_t)(rhs->u.cval);
8487}
8488
Eric Biederman5ade04a2003-10-22 04:03:46 +00008489int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8490{
8491 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008492 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008493 }
8494 return !is_zero(rhs);
8495}
8496
8497int const_eq(struct compile_state *state, struct triple *ins,
8498 struct triple *left, struct triple *right)
8499{
8500 int result;
8501 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008502 internal_warning(state, ins, "non const passed to const_eq");
8503 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008504 }
8505 else if (left == right) {
8506 result = 1;
8507 }
8508 else if (is_simple_const(left) && is_simple_const(right)) {
8509 ulong_t lval, rval;
8510 lval = read_const(state, ins, left);
8511 rval = read_const(state, ins, right);
8512 result = (lval == rval);
8513 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008514 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008515 (right->op == OP_ADDRCONST)) {
8516 result = (MISC(left, 0) == MISC(right, 0)) &&
8517 (left->u.cval == right->u.cval);
8518 }
8519 else {
Eric Biederman90089602004-05-28 14:11:54 +00008520 internal_warning(state, ins, "incomparable constants passed to const_eq");
8521 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008522 }
8523 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008524
Eric Biederman5ade04a2003-10-22 04:03:46 +00008525}
8526
8527int const_ucmp(struct compile_state *state, struct triple *ins,
8528 struct triple *left, struct triple *right)
8529{
8530 int result;
8531 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008532 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008533 result = -2;
8534 }
8535 else if (left == right) {
8536 result = 0;
8537 }
8538 else if (is_simple_const(left) && is_simple_const(right)) {
8539 ulong_t lval, rval;
8540 lval = read_const(state, ins, left);
8541 rval = read_const(state, ins, right);
8542 result = 0;
8543 if (lval > rval) {
8544 result = 1;
8545 } else if (rval > lval) {
8546 result = -1;
8547 }
8548 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008549 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008550 (right->op == OP_ADDRCONST) &&
8551 (MISC(left, 0) == MISC(right, 0))) {
8552 result = 0;
8553 if (left->u.cval > right->u.cval) {
8554 result = 1;
8555 } else if (left->u.cval < right->u.cval) {
8556 result = -1;
8557 }
8558 }
8559 else {
Eric Biederman90089602004-05-28 14:11:54 +00008560 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008561 result = -2;
8562 }
8563 return result;
8564}
8565
8566int const_scmp(struct compile_state *state, struct triple *ins,
8567 struct triple *left, struct triple *right)
8568{
8569 int result;
8570 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008571 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008572 result = -2;
8573 }
8574 else if (left == right) {
8575 result = 0;
8576 }
8577 else if (is_simple_const(left) && is_simple_const(right)) {
8578 long_t lval, rval;
8579 lval = read_sconst(state, ins, left);
8580 rval = read_sconst(state, ins, right);
8581 result = 0;
8582 if (lval > rval) {
8583 result = 1;
8584 } else if (rval > lval) {
8585 result = -1;
8586 }
8587 }
8588 else {
Eric Biederman90089602004-05-28 14:11:54 +00008589 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008590 result = -2;
8591 }
8592 return result;
8593}
8594
Eric Biedermanb138ac82003-04-22 18:44:01 +00008595static void unuse_rhs(struct compile_state *state, struct triple *ins)
8596{
8597 struct triple **expr;
8598 expr = triple_rhs(state, ins, 0);
8599 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008600 if (*expr) {
8601 unuse_triple(*expr, ins);
8602 *expr = 0;
8603 }
8604 }
8605}
8606
8607static void unuse_lhs(struct compile_state *state, struct triple *ins)
8608{
8609 struct triple **expr;
8610 expr = triple_lhs(state, ins, 0);
8611 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008612 unuse_triple(*expr, ins);
8613 *expr = 0;
8614 }
8615}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008616
Stefan Reinauer50542a82007-10-24 11:14:14 +00008617#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008618static void unuse_misc(struct compile_state *state, struct triple *ins)
8619{
8620 struct triple **expr;
8621 expr = triple_misc(state, ins, 0);
8622 for(;expr;expr = triple_misc(state, ins, expr)) {
8623 unuse_triple(*expr, ins);
8624 *expr = 0;
8625 }
8626}
8627
8628static void unuse_targ(struct compile_state *state, struct triple *ins)
8629{
8630 int i;
8631 struct triple **slot;
8632 slot = &TARG(ins, 0);
8633 for(i = 0; i < ins->targ; i++) {
8634 unuse_triple(slot[i], ins);
8635 slot[i] = 0;
8636 }
8637}
8638
Eric Biedermanb138ac82003-04-22 18:44:01 +00008639static void check_lhs(struct compile_state *state, struct triple *ins)
8640{
8641 struct triple **expr;
8642 expr = triple_lhs(state, ins, 0);
8643 for(;expr;expr = triple_lhs(state, ins, expr)) {
8644 internal_error(state, ins, "unexpected lhs");
8645 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008646
Eric Biedermanb138ac82003-04-22 18:44:01 +00008647}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008648#endif
Eric Biederman90089602004-05-28 14:11:54 +00008649
8650static void check_misc(struct compile_state *state, struct triple *ins)
8651{
8652 struct triple **expr;
8653 expr = triple_misc(state, ins, 0);
8654 for(;expr;expr = triple_misc(state, ins, expr)) {
8655 if (*expr) {
8656 internal_error(state, ins, "unexpected misc");
8657 }
8658 }
8659}
8660
Eric Biedermanb138ac82003-04-22 18:44:01 +00008661static void check_targ(struct compile_state *state, struct triple *ins)
8662{
8663 struct triple **expr;
8664 expr = triple_targ(state, ins, 0);
8665 for(;expr;expr = triple_targ(state, ins, expr)) {
8666 internal_error(state, ins, "unexpected targ");
8667 }
8668}
8669
8670static void wipe_ins(struct compile_state *state, struct triple *ins)
8671{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008672 /* Becareful which instructions you replace the wiped
8673 * instruction with, as there are not enough slots
8674 * in all instructions to hold all others.
8675 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008676 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008677 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008678 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008679 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008680 ins->lhs = 0;
8681 ins->rhs = 0;
8682 ins->misc = 0;
8683 ins->targ = 0;
8684}
8685
Stefan Reinauer50542a82007-10-24 11:14:14 +00008686#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008687static void wipe_branch(struct compile_state *state, struct triple *ins)
8688{
8689 /* Becareful which instructions you replace the wiped
8690 * instruction with, as there are not enough slots
8691 * in all instructions to hold all others.
8692 */
8693 unuse_rhs(state, ins);
8694 unuse_lhs(state, ins);
8695 unuse_misc(state, ins);
8696 unuse_targ(state, ins);
8697 ins->lhs = 0;
8698 ins->rhs = 0;
8699 ins->misc = 0;
8700 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008701}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008702#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008703
Stefan Reinauer14e22772010-04-27 06:56:47 +00008704static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008705 struct triple *ins, struct triple *rhs)
8706{
Eric Biederman83b991a2003-10-11 06:20:25 +00008707 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008708 if (!equiv_types(ins->type, rhs->type)) {
8709 FILE *fp = state->errout;
8710 fprintf(fp, "src type: ");
8711 name_of(fp, rhs->type);
8712 fprintf(fp, "\ndst type: ");
8713 name_of(fp, ins->type);
8714 fprintf(fp, "\n");
8715 internal_error(state, ins, "mkcopy type mismatch");
8716 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008717 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008718 wipe_ins(state, ins);
8719 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008720 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008721 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008722 RHS(ins, 0) = rhs;
8723 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008724}
8725
Stefan Reinauer14e22772010-04-27 06:56:47 +00008726static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008727 struct triple *ins, ulong_t value)
8728{
8729 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008730 fprintf(state->errout, "type: ");
8731 name_of(state->errout, ins->type);
8732 fprintf(state->errout, "\n");
8733 internal_error(state, ins, "unknown type to make constant value: %ld",
8734 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008735 }
8736 wipe_ins(state, ins);
8737 ins->op = OP_INTCONST;
8738 ins->u.cval = value;
8739}
8740
8741static void mkaddr_const(struct compile_state *state,
8742 struct triple *ins, struct triple *sdecl, ulong_t value)
8743{
Eric Biederman90089602004-05-28 14:11:54 +00008744 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008745 internal_error(state, ins, "bad base for addrconst");
8746 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008747 wipe_ins(state, ins);
8748 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008749 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008750 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008751 ins->u.cval = value;
8752 use_triple(sdecl, ins);
8753}
8754
Eric Biederman90089602004-05-28 14:11:54 +00008755#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008756static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008757 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008758{
Eric Biederman90089602004-05-28 14:11:54 +00008759 FILE *fp = state->dbgout;
8760 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8761 name_of(fp, tuple->type);
8762 if (tuple->lhs > 0) {
8763 fprintf(fp, " lhs: ");
8764 name_of(fp, LHS(tuple, 0)->type);
8765 }
8766 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008767
Eric Biederman90089602004-05-28 14:11:54 +00008768}
8769#endif
8770
Stefan Reinauer14e22772010-04-27 06:56:47 +00008771static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008772 struct triple *ins, struct triple *tuple)
8773{
8774 struct triple *next;
8775 next = ins->next;
8776 flatten(state, next, tuple);
8777#if DEBUG_DECOMPOSE_PRINT_TUPLES
8778 print_tuple(state, ins, tuple);
8779#endif
8780
8781 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8782 struct triple *tmp;
8783 if (tuple->lhs != 1) {
8784 internal_error(state, tuple, "plain type in multiple registers?");
8785 }
8786 tmp = LHS(tuple, 0);
8787 release_triple(state, tuple);
8788 tuple = tmp;
8789 }
8790
8791 propogate_use(state, ins, tuple);
8792 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008793
Eric Biederman90089602004-05-28 14:11:54 +00008794 return next;
8795}
8796
8797static struct triple *decompose_unknownval(struct compile_state *state,
8798 struct triple *ins)
8799{
8800 struct triple *tuple;
8801 ulong_t i;
8802
8803#if DEBUG_DECOMPOSE_HIRES
8804 FILE *fp = state->dbgout;
8805 fprintf(fp, "unknown type: ");
8806 name_of(fp, ins->type);
8807 fprintf(fp, "\n");
8808#endif
8809
8810 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008811 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008812 ins->occurance);
8813
8814 for(i = 0; i < tuple->lhs; i++) {
8815 struct type *piece_type;
8816 struct triple *unknown;
8817
8818 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8819 get_occurance(tuple->occurance);
8820 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8821 tuple->occurance);
8822 LHS(tuple, i) = unknown;
8823 }
8824 return decompose_with_tuple(state, ins, tuple);
8825}
8826
8827
Stefan Reinauer14e22772010-04-27 06:56:47 +00008828static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008829 struct triple *ins)
8830{
8831 struct triple *tuple, *lval;
8832 ulong_t i;
8833
8834 lval = RHS(ins, 0);
8835
8836 if (lval->op == OP_PIECE) {
8837 return ins->next;
8838 }
8839 get_occurance(ins->occurance);
8840 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8841 ins->occurance);
8842
8843 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008844 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008845 {
8846 internal_error(state, ins, "lhs size inconsistency?");
8847 }
8848 for(i = 0; i < tuple->lhs; i++) {
8849 struct triple *piece, *read, *bitref;
8850 if ((i != 0) || !triple_is_def(state, lval)) {
8851 piece = LHS(lval, i);
8852 } else {
8853 piece = lval;
8854 }
8855
8856 /* See if the piece is really a bitref */
8857 bitref = 0;
8858 if (piece->op == OP_BITREF) {
8859 bitref = piece;
8860 piece = RHS(bitref, 0);
8861 }
8862
8863 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008864 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008865 tuple->occurance);
8866 RHS(read, 0) = piece;
8867
8868 if (bitref) {
8869 struct triple *extract;
8870 int op;
8871 if (is_signed(bitref->type->left)) {
8872 op = OP_SEXTRACT;
8873 } else {
8874 op = OP_UEXTRACT;
8875 }
8876 get_occurance(tuple->occurance);
8877 extract = alloc_triple(state, op, bitref->type, -1, -1,
8878 tuple->occurance);
8879 RHS(extract, 0) = read;
8880 extract->u.bitfield.size = bitref->u.bitfield.size;
8881 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8882
8883 read = extract;
8884 }
8885
8886 LHS(tuple, i) = read;
8887 }
8888 return decompose_with_tuple(state, ins, tuple);
8889}
8890
Stefan Reinauer14e22772010-04-27 06:56:47 +00008891static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008892 struct triple *ins)
8893{
8894 struct triple *tuple, *lval, *val;
8895 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008896
Eric Biederman90089602004-05-28 14:11:54 +00008897 lval = MISC(ins, 0);
8898 val = RHS(ins, 0);
8899 get_occurance(ins->occurance);
8900 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8901 ins->occurance);
8902
8903 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008904 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008905 {
8906 internal_error(state, ins, "lhs size inconsistency?");
8907 }
8908 for(i = 0; i < tuple->lhs; i++) {
8909 struct triple *piece, *write, *pval, *bitref;
8910 if ((i != 0) || !triple_is_def(state, lval)) {
8911 piece = LHS(lval, i);
8912 } else {
8913 piece = lval;
8914 }
8915 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8916 pval = val;
8917 }
8918 else {
8919 if (i > val->lhs) {
8920 internal_error(state, ins, "lhs size inconsistency?");
8921 }
8922 pval = LHS(val, i);
8923 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008924
Eric Biederman90089602004-05-28 14:11:54 +00008925 /* See if the piece is really a bitref */
8926 bitref = 0;
8927 if (piece->op == OP_BITREF) {
8928 struct triple *read, *deposit;
8929 bitref = piece;
8930 piece = RHS(bitref, 0);
8931
8932 /* Read the destination register */
8933 get_occurance(tuple->occurance);
8934 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8935 tuple->occurance);
8936 RHS(read, 0) = piece;
8937
8938 /* Deposit the new bitfield value */
8939 get_occurance(tuple->occurance);
8940 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8941 tuple->occurance);
8942 RHS(deposit, 0) = read;
8943 RHS(deposit, 1) = pval;
8944 deposit->u.bitfield.size = bitref->u.bitfield.size;
8945 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8946
8947 /* Now write the newly generated value */
8948 pval = deposit;
8949 }
8950
8951 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008952 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008953 tuple->occurance);
8954 MISC(write, 0) = piece;
8955 RHS(write, 0) = pval;
8956 LHS(tuple, i) = write;
8957 }
8958 return decompose_with_tuple(state, ins, tuple);
8959}
8960
8961struct decompose_load_info {
8962 struct occurance *occurance;
8963 struct triple *lval;
8964 struct triple *tuple;
8965};
8966static void decompose_load_cb(struct compile_state *state,
8967 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8968{
8969 struct decompose_load_info *info = arg;
8970 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008971
Eric Biederman90089602004-05-28 14:11:54 +00008972 if (reg_offset > info->tuple->lhs) {
8973 internal_error(state, info->tuple, "lhs to small?");
8974 }
8975 get_occurance(info->occurance);
8976 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8977 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8978 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8979}
8980
Stefan Reinauer14e22772010-04-27 06:56:47 +00008981static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008982 struct triple *ins)
8983{
8984 struct triple *tuple;
8985 struct decompose_load_info info;
8986
8987 if (!is_compound_type(ins->type)) {
8988 return ins->next;
8989 }
8990 get_occurance(ins->occurance);
8991 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8992 ins->occurance);
8993
8994 info.occurance = ins->occurance;
8995 info.lval = RHS(ins, 0);
8996 info.tuple = tuple;
8997 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8998
8999 return decompose_with_tuple(state, ins, tuple);
9000}
9001
9002
9003struct decompose_store_info {
9004 struct occurance *occurance;
9005 struct triple *lval;
9006 struct triple *val;
9007 struct triple *tuple;
9008};
9009static void decompose_store_cb(struct compile_state *state,
9010 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9011{
9012 struct decompose_store_info *info = arg;
9013 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009014
Eric Biederman90089602004-05-28 14:11:54 +00009015 if (reg_offset > info->tuple->lhs) {
9016 internal_error(state, info->tuple, "lhs to small?");
9017 }
9018 get_occurance(info->occurance);
9019 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9020 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9021 RHS(store, 1) = LHS(info->val, reg_offset);
9022 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9023}
9024
Stefan Reinauer14e22772010-04-27 06:56:47 +00009025static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009026 struct triple *ins)
9027{
9028 struct triple *tuple;
9029 struct decompose_store_info info;
9030
9031 if (!is_compound_type(ins->type)) {
9032 return ins->next;
9033 }
9034 get_occurance(ins->occurance);
9035 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9036 ins->occurance);
9037
9038 info.occurance = ins->occurance;
9039 info.lval = RHS(ins, 0);
9040 info.val = RHS(ins, 1);
9041 info.tuple = tuple;
9042 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9043
9044 return decompose_with_tuple(state, ins, tuple);
9045}
9046
Stefan Reinauer14e22772010-04-27 06:56:47 +00009047static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009048 struct triple *ins)
9049{
9050 struct triple *tuple, *lval;
9051 struct type *type;
9052 size_t reg_offset;
9053 int i, idx;
9054
9055 lval = MISC(ins, 0);
9056 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9057 idx = reg_offset/REG_SIZEOF_REG;
9058 type = field_type(state, lval->type, ins->u.field);
9059#if DEBUG_DECOMPOSE_HIRES
9060 {
9061 FILE *fp = state->dbgout;
9062 fprintf(fp, "field type: ");
9063 name_of(fp, type);
9064 fprintf(fp, "\n");
9065 }
9066#endif
9067
9068 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009069 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009070 ins->occurance);
9071
9072 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9073 (tuple->lhs != 1))
9074 {
9075 internal_error(state, ins, "multi register bitfield?");
9076 }
9077
9078 for(i = 0; i < tuple->lhs; i++, idx++) {
9079 struct triple *piece;
9080 if (!triple_is_def(state, lval)) {
9081 if (idx > lval->lhs) {
9082 internal_error(state, ins, "inconsistent lhs count");
9083 }
9084 piece = LHS(lval, idx);
9085 } else {
9086 if (idx != 0) {
9087 internal_error(state, ins, "bad reg_offset into def");
9088 }
9089 if (i != 0) {
9090 internal_error(state, ins, "bad reg count from def");
9091 }
9092 piece = lval;
9093 }
9094
9095 /* Remember the offset of the bitfield */
9096 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9097 get_occurance(ins->occurance);
9098 piece = build_triple(state, OP_BITREF, type, piece, 0,
9099 ins->occurance);
9100 piece->u.bitfield.size = size_of(state, type);
9101 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9102 }
9103 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009104 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009105 "request for a nonbitfield sub register?");
9106 }
9107
9108 LHS(tuple, i) = piece;
9109 }
9110
9111 return decompose_with_tuple(state, ins, tuple);
9112}
9113
Stefan Reinauer14e22772010-04-27 06:56:47 +00009114static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009115 struct triple *ins)
9116{
9117 struct triple *tuple, *lval;
9118 struct type *type;
9119 int i, idx;
9120
9121 lval = MISC(ins, 0);
9122 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9123 type = index_type(state, lval->type, ins->u.cval);
9124#if DEBUG_DECOMPOSE_HIRES
9125{
9126 FILE *fp = state->dbgout;
9127 fprintf(fp, "index type: ");
9128 name_of(fp, type);
9129 fprintf(fp, "\n");
9130}
9131#endif
9132
9133 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009134 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009135 ins->occurance);
9136
9137 for(i = 0; i < tuple->lhs; i++, idx++) {
9138 struct triple *piece;
9139 if (!triple_is_def(state, lval)) {
9140 if (idx > lval->lhs) {
9141 internal_error(state, ins, "inconsistent lhs count");
9142 }
9143 piece = LHS(lval, idx);
9144 } else {
9145 if (idx != 0) {
9146 internal_error(state, ins, "bad reg_offset into def");
9147 }
9148 if (i != 0) {
9149 internal_error(state, ins, "bad reg count from def");
9150 }
9151 piece = lval;
9152 }
9153 LHS(tuple, i) = piece;
9154 }
9155
9156 return decompose_with_tuple(state, ins, tuple);
9157}
9158
9159static void decompose_compound_types(struct compile_state *state)
9160{
9161 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009162 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009163 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009164
9165 /* Pass one expand compound values into pseudo registers.
9166 */
9167 next = first;
9168 do {
9169 ins = next;
9170 next = ins->next;
9171 switch(ins->op) {
9172 case OP_UNKNOWNVAL:
9173 next = decompose_unknownval(state, ins);
9174 break;
9175
9176 case OP_READ:
9177 next = decompose_read(state, ins);
9178 break;
9179
9180 case OP_WRITE:
9181 next = decompose_write(state, ins);
9182 break;
9183
9184
9185 /* Be very careful with the load/store logic. These
9186 * operations must convert from the in register layout
9187 * to the in memory layout, which is nontrivial.
9188 */
9189 case OP_LOAD:
9190 next = decompose_load(state, ins);
9191 break;
9192 case OP_STORE:
9193 next = decompose_store(state, ins);
9194 break;
9195
9196 case OP_DOT:
9197 next = decompose_dot(state, ins);
9198 break;
9199 case OP_INDEX:
9200 next = decompose_index(state, ins);
9201 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009202
Eric Biederman90089602004-05-28 14:11:54 +00009203 }
9204#if DEBUG_DECOMPOSE_HIRES
9205 fprintf(fp, "decompose next: %p \n", next);
9206 fflush(fp);
9207 fprintf(fp, "next->op: %d %s\n",
9208 next->op, tops(next->op));
9209 /* High resolution debugging mode */
9210 print_triples(state);
9211#endif
9212 } while (next != first);
9213
9214 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009215 */
9216 ins = first;
9217 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009218 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009219 if (ins->op == OP_TUPLE) {
9220 if (ins->use) {
9221 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009222 }
Eric Biederman90089602004-05-28 14:11:54 +00009223 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009224 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009225 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009226 }
Eric Biederman90089602004-05-28 14:11:54 +00009227 ins = next;
9228 } while(ins != first);
9229 ins = first;
9230 do {
9231 next = ins->next;
9232 if (ins->op == OP_BITREF) {
9233 if (ins->use) {
9234 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +00009235 }
Eric Biederman90089602004-05-28 14:11:54 +00009236 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009237 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009238 }
9239 }
9240 ins = next;
9241 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009242
Eric Biederman0babc1c2003-05-09 02:39:00 +00009243 /* Pass three verify the state and set ->id to 0.
9244 */
Eric Biederman90089602004-05-28 14:11:54 +00009245 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009246 do {
Eric Biederman90089602004-05-28 14:11:54 +00009247 ins = next;
9248 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009249 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009250 if (triple_stores_block(state, ins)) {
9251 ins->u.block = 0;
9252 }
9253 if (triple_is_def(state, ins)) {
9254 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9255 internal_error(state, ins, "multi register value remains?");
9256 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009257 }
9258 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009259 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009260 }
Eric Biederman90089602004-05-28 14:11:54 +00009261 if (ins->op == OP_INDEX) {
9262 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009263 }
Eric Biederman90089602004-05-28 14:11:54 +00009264 if (ins->op == OP_BITREF) {
9265 internal_error(state, ins, "OP_BITREF remains?");
9266 }
9267 if (ins->op == OP_TUPLE) {
9268 internal_error(state, ins, "OP_TUPLE remains?");
9269 }
9270 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009271}
9272
Eric Biedermanb138ac82003-04-22 18:44:01 +00009273/* For those operations that cannot be simplified */
9274static void simplify_noop(struct compile_state *state, struct triple *ins)
9275{
9276 return;
9277}
9278
9279static void simplify_smul(struct compile_state *state, struct triple *ins)
9280{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009281 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009282 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009283 tmp = RHS(ins, 0);
9284 RHS(ins, 0) = RHS(ins, 1);
9285 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009286 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009287 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009289 left = read_sconst(state, ins, RHS(ins, 0));
9290 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009291 mkconst(state, ins, left * right);
9292 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009293 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009294 mkconst(state, ins, 0);
9295 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009296 else if (is_one(RHS(ins, 1))) {
9297 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009298 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009299 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009300 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009301 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009303 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009304 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009305 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009306 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009307 }
9308}
9309
9310static void simplify_umul(struct compile_state *state, struct triple *ins)
9311{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009312 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009313 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 tmp = RHS(ins, 0);
9315 RHS(ins, 0) = RHS(ins, 1);
9316 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009317 }
Eric Biederman90089602004-05-28 14:11:54 +00009318 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009320 left = read_const(state, ins, RHS(ins, 0));
9321 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009322 mkconst(state, ins, left * right);
9323 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009324 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009325 mkconst(state, ins, 0);
9326 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009327 else if (is_one(RHS(ins, 1))) {
9328 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009329 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009330 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009331 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009332 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009334 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009335 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009336 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009337 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009338 }
9339}
9340
9341static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9342{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009343 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009344 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009345 left = read_sconst(state, ins, RHS(ins, 0));
9346 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009347 mkconst(state, ins, left / right);
9348 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009349 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009350 mkconst(state, ins, 0);
9351 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009352 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009353 error(state, ins, "division by zero");
9354 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009355 else if (is_one(RHS(ins, 1))) {
9356 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009357 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009358 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009359 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009360 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009362 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009363 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009364 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009365 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009366 }
9367}
9368
9369static void simplify_udiv(struct compile_state *state, struct triple *ins)
9370{
Eric Biederman90089602004-05-28 14:11:54 +00009371 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009372 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009373 left = read_const(state, ins, RHS(ins, 0));
9374 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009375 mkconst(state, ins, left / right);
9376 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009377 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009378 mkconst(state, ins, 0);
9379 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009380 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009381 error(state, ins, "division by zero");
9382 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009383 else if (is_one(RHS(ins, 1))) {
9384 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009385 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009386 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009387 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009388 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009390 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009391 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009392 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009393 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009394 }
9395}
9396
9397static void simplify_smod(struct compile_state *state, struct triple *ins)
9398{
Eric Biederman90089602004-05-28 14:11:54 +00009399 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009400 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009401 left = read_const(state, ins, RHS(ins, 0));
9402 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009403 mkconst(state, ins, left % right);
9404 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009405 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009406 mkconst(state, ins, 0);
9407 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009408 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009409 error(state, ins, "division by zero");
9410 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009411 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009412 mkconst(state, ins, 0);
9413 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009414 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009415 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009418 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009419 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009420 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009421 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 }
9423}
Eric Biederman83b991a2003-10-11 06:20:25 +00009424
Eric Biedermanb138ac82003-04-22 18:44:01 +00009425static void simplify_umod(struct compile_state *state, struct triple *ins)
9426{
Eric Biederman90089602004-05-28 14:11:54 +00009427 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009429 left = read_const(state, ins, RHS(ins, 0));
9430 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009431 mkconst(state, ins, left % right);
9432 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009433 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009434 mkconst(state, ins, 0);
9435 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009436 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437 error(state, ins, "division by zero");
9438 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009439 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440 mkconst(state, ins, 0);
9441 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009442 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009443 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009444 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009446 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009447 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009448 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009449 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009450 }
9451}
9452
9453static void simplify_add(struct compile_state *state, struct triple *ins)
9454{
9455 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009456 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009457 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458 tmp = RHS(ins, 0);
9459 RHS(ins, 0) = RHS(ins, 1);
9460 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009461 }
Eric Biederman90089602004-05-28 14:11:54 +00009462 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009463 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009464 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009465 left = read_const(state, ins, RHS(ins, 0));
9466 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009467 mkconst(state, ins, left + right);
9468 }
Eric Biederman530b5192003-07-01 10:05:30 +00009469 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009470 struct triple *sdecl;
9471 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009472 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009473 left = RHS(ins, 0)->u.cval;
9474 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009475 mkaddr_const(state, ins, sdecl, left + right);
9476 }
Eric Biederman530b5192003-07-01 10:05:30 +00009477 else {
9478 internal_warning(state, ins, "Optimize me!");
9479 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009480 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009481 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009482 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009483 tmp = RHS(ins, 1);
9484 RHS(ins, 1) = RHS(ins, 0);
9485 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009486 }
9487}
9488
9489static void simplify_sub(struct compile_state *state, struct triple *ins)
9490{
Eric Biederman90089602004-05-28 14:11:54 +00009491 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009492 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009493 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009494 left = read_const(state, ins, RHS(ins, 0));
9495 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009496 mkconst(state, ins, left - right);
9497 }
Eric Biederman530b5192003-07-01 10:05:30 +00009498 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009499 struct triple *sdecl;
9500 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009501 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009502 left = RHS(ins, 0)->u.cval;
9503 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009504 mkaddr_const(state, ins, sdecl, left - right);
9505 }
Eric Biederman530b5192003-07-01 10:05:30 +00009506 else {
9507 internal_warning(state, ins, "Optimize me!");
9508 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009509 }
9510}
9511
9512static void simplify_sl(struct compile_state *state, struct triple *ins)
9513{
Eric Biederman90089602004-05-28 14:11:54 +00009514 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009515 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009516 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009517 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009518 warning(state, ins, "left shift count >= width of type");
9519 }
9520 }
Eric Biederman90089602004-05-28 14:11:54 +00009521 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009522 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009523 left = read_const(state, ins, RHS(ins, 0));
9524 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009525 mkconst(state, ins, left << right);
9526 }
9527}
9528
9529static void simplify_usr(struct compile_state *state, struct triple *ins)
9530{
Eric Biederman90089602004-05-28 14:11:54 +00009531 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009532 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009533 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009534 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009535 warning(state, ins, "right shift count >= width of type");
9536 }
9537 }
Eric Biederman90089602004-05-28 14:11:54 +00009538 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009539 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009540 left = read_const(state, ins, RHS(ins, 0));
9541 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009542 mkconst(state, ins, left >> right);
9543 }
9544}
9545
9546static void simplify_ssr(struct compile_state *state, struct triple *ins)
9547{
Eric Biederman90089602004-05-28 14:11:54 +00009548 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009550 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009551 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009552 warning(state, ins, "right shift count >= width of type");
9553 }
9554 }
Eric Biederman90089602004-05-28 14:11:54 +00009555 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009556 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009557 left = read_sconst(state, ins, RHS(ins, 0));
9558 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009559 mkconst(state, ins, left >> right);
9560 }
9561}
9562
9563static void simplify_and(struct compile_state *state, struct triple *ins)
9564{
Eric Biederman90089602004-05-28 14:11:54 +00009565 struct triple *left, *right;
9566 left = RHS(ins, 0);
9567 right = RHS(ins, 1);
9568
9569 if (is_simple_const(left) && is_simple_const(right)) {
9570 ulong_t lval, rval;
9571 lval = read_const(state, ins, left);
9572 rval = read_const(state, ins, right);
9573 mkconst(state, ins, lval & rval);
9574 }
9575 else if (is_zero(right) || is_zero(left)) {
9576 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009577 }
9578}
9579
9580static void simplify_or(struct compile_state *state, struct triple *ins)
9581{
Eric Biederman90089602004-05-28 14:11:54 +00009582 struct triple *left, *right;
9583 left = RHS(ins, 0);
9584 right = RHS(ins, 1);
9585
9586 if (is_simple_const(left) && is_simple_const(right)) {
9587 ulong_t lval, rval;
9588 lval = read_const(state, ins, left);
9589 rval = read_const(state, ins, right);
9590 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009591 }
Eric Biederman90089602004-05-28 14:11:54 +00009592#if 0 /* I need to handle type mismatches here... */
9593 else if (is_zero(right)) {
9594 mkcopy(state, ins, left);
9595 }
9596 else if (is_zero(left)) {
9597 mkcopy(state, ins, right);
9598 }
9599#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009600}
9601
9602static void simplify_xor(struct compile_state *state, struct triple *ins)
9603{
Eric Biederman90089602004-05-28 14:11:54 +00009604 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009605 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009606 left = read_const(state, ins, RHS(ins, 0));
9607 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009608 mkconst(state, ins, left ^ right);
9609 }
9610}
9611
9612static void simplify_pos(struct compile_state *state, struct triple *ins)
9613{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009614 if (is_const(RHS(ins, 0))) {
9615 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009616 }
9617 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009618 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009619 }
9620}
9621
9622static void simplify_neg(struct compile_state *state, struct triple *ins)
9623{
Eric Biederman90089602004-05-28 14:11:54 +00009624 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009625 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009626 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 mkconst(state, ins, -left);
9628 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009629 else if (RHS(ins, 0)->op == OP_NEG) {
9630 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009631 }
9632}
9633
9634static void simplify_invert(struct compile_state *state, struct triple *ins)
9635{
Eric Biederman90089602004-05-28 14:11:54 +00009636 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009637 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009638 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009639 mkconst(state, ins, ~left);
9640 }
9641}
9642
9643static void simplify_eq(struct compile_state *state, struct triple *ins)
9644{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009645 struct triple *left, *right;
9646 left = RHS(ins, 0);
9647 right = RHS(ins, 1);
9648
9649 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009650 int val;
9651 val = const_eq(state, ins, left, right);
9652 if (val >= 0) {
9653 mkconst(state, ins, val == 1);
9654 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009655 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009656 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009657 mkconst(state, ins, 1);
9658 }
9659}
9660
9661static void simplify_noteq(struct compile_state *state, struct triple *ins)
9662{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009663 struct triple *left, *right;
9664 left = RHS(ins, 0);
9665 right = RHS(ins, 1);
9666
9667 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009668 int val;
9669 val = const_eq(state, ins, left, right);
9670 if (val >= 0) {
9671 mkconst(state, ins, val != 1);
9672 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009673 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009674 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009675 mkconst(state, ins, 0);
9676 }
9677}
9678
9679static void simplify_sless(struct compile_state *state, struct triple *ins)
9680{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009681 struct triple *left, *right;
9682 left = RHS(ins, 0);
9683 right = RHS(ins, 1);
9684
9685 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009686 int val;
9687 val = const_scmp(state, ins, left, right);
9688 if ((val >= -1) && (val <= 1)) {
9689 mkconst(state, ins, val < 0);
9690 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009691 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009692 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009693 mkconst(state, ins, 0);
9694 }
9695}
9696
9697static void simplify_uless(struct compile_state *state, struct triple *ins)
9698{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009699 struct triple *left, *right;
9700 left = RHS(ins, 0);
9701 right = RHS(ins, 1);
9702
9703 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009704 int val;
9705 val = const_ucmp(state, ins, left, right);
9706 if ((val >= -1) && (val <= 1)) {
9707 mkconst(state, ins, val < 0);
9708 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009709 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009710 else if (is_zero(right)) {
9711 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009712 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009713 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009714 mkconst(state, ins, 0);
9715 }
9716}
9717
9718static void simplify_smore(struct compile_state *state, struct triple *ins)
9719{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009720 struct triple *left, *right;
9721 left = RHS(ins, 0);
9722 right = RHS(ins, 1);
9723
9724 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009725 int val;
9726 val = const_scmp(state, ins, left, right);
9727 if ((val >= -1) && (val <= 1)) {
9728 mkconst(state, ins, val > 0);
9729 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009730 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009731 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009732 mkconst(state, ins, 0);
9733 }
9734}
9735
9736static void simplify_umore(struct compile_state *state, struct triple *ins)
9737{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009738 struct triple *left, *right;
9739 left = RHS(ins, 0);
9740 right = RHS(ins, 1);
9741
9742 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009743 int val;
9744 val = const_ucmp(state, ins, left, right);
9745 if ((val >= -1) && (val <= 1)) {
9746 mkconst(state, ins, val > 0);
9747 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009748 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009749 else if (is_zero(left)) {
9750 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009751 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009752 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009753 mkconst(state, ins, 0);
9754 }
9755}
9756
9757
9758static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9759{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009760 struct triple *left, *right;
9761 left = RHS(ins, 0);
9762 right = RHS(ins, 1);
9763
9764 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009765 int val;
9766 val = const_scmp(state, ins, left, right);
9767 if ((val >= -1) && (val <= 1)) {
9768 mkconst(state, ins, val <= 0);
9769 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009770 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009771 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009772 mkconst(state, ins, 1);
9773 }
9774}
9775
9776static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9777{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009778 struct triple *left, *right;
9779 left = RHS(ins, 0);
9780 right = RHS(ins, 1);
9781
9782 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009783 int val;
9784 val = const_ucmp(state, ins, left, right);
9785 if ((val >= -1) && (val <= 1)) {
9786 mkconst(state, ins, val <= 0);
9787 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009788 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009789 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009790 mkconst(state, ins, 1);
9791 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009792 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009793 mkconst(state, ins, 1);
9794 }
9795}
9796
9797static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9798{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009799 struct triple *left, *right;
9800 left = RHS(ins, 0);
9801 right = RHS(ins, 1);
9802
9803 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009804 int val;
9805 val = const_scmp(state, ins, left, right);
9806 if ((val >= -1) && (val <= 1)) {
9807 mkconst(state, ins, val >= 0);
9808 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009809 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009810 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009811 mkconst(state, ins, 1);
9812 }
9813}
9814
9815static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9816{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009817 struct triple *left, *right;
9818 left = RHS(ins, 0);
9819 right = RHS(ins, 1);
9820
9821 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009822 int val;
9823 val = const_ucmp(state, ins, left, right);
9824 if ((val >= -1) && (val <= 1)) {
9825 mkconst(state, ins, val >= 0);
9826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009827 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009828 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009829 mkconst(state, ins, 1);
9830 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009831 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009832 mkconst(state, ins, 1);
9833 }
9834}
9835
9836static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9837{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009838 struct triple *rhs;
9839 rhs = RHS(ins, 0);
9840
9841 if (is_const(rhs)) {
9842 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009843 }
9844 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009845 else if ((rhs->use) &&
9846 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009847 int need_copy = 1;
9848 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009849 switch(rhs->op) {
9850 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9851 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9852 case OP_EQ: rhs->op = OP_NOTEQ; break;
9853 case OP_NOTEQ: rhs->op = OP_EQ; break;
9854 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9855 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9856 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9857 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9858 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9859 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9860 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9861 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009862 default:
9863 need_copy = 0;
9864 break;
9865 }
9866 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009867 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009868 }
9869 }
9870}
9871
9872static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9873{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009874 struct triple *rhs;
9875 rhs = RHS(ins, 0);
9876
9877 if (is_const(rhs)) {
9878 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009879 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009880 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009881 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9882 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9883 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009884 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009885 }
9886
9887}
9888
Eric Biederman90089602004-05-28 14:11:54 +00009889static void simplify_load(struct compile_state *state, struct triple *ins)
9890{
9891 struct triple *addr, *sdecl, *blob;
9892
9893 /* If I am doing a load with a constant pointer from a constant
9894 * table get the value.
9895 */
9896 addr = RHS(ins, 0);
9897 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9898 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9899 (blob->op == OP_BLOBCONST)) {
9900 unsigned char buffer[SIZEOF_WORD];
9901 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009902 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009903 ulong_t val;
9904 reg_size = reg_size_of(state, ins->type);
9905 if (reg_size > REG_SIZEOF_REG) {
9906 internal_error(state, ins, "load size greater than register");
9907 }
9908 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009909 end = blob->u.blob;
9910 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009911 src = blob->u.blob;
9912 src += addr->u.cval;
9913
Eric Biederman7dea9552004-06-29 05:38:37 +00009914 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009915 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009916 }
9917
Eric Biederman90089602004-05-28 14:11:54 +00009918 memset(buffer, 0, sizeof(buffer));
9919 memcpy(buffer, src, bits_to_bytes(mem_size));
9920
9921 switch(mem_size) {
9922 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9923 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9924 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9925 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9926 default:
9927 internal_error(state, ins, "mem_size: %d not handled",
9928 mem_size);
9929 val = 0;
9930 break;
9931 }
9932 mkconst(state, ins, val);
9933 }
9934}
9935
9936static void simplify_uextract(struct compile_state *state, struct triple *ins)
9937{
9938 if (is_simple_const(RHS(ins, 0))) {
9939 ulong_t val;
9940 ulong_t mask;
9941 val = read_const(state, ins, RHS(ins, 0));
9942 mask = 1;
9943 mask <<= ins->u.bitfield.size;
9944 mask -= 1;
9945 val >>= ins->u.bitfield.offset;
9946 val &= mask;
9947 mkconst(state, ins, val);
9948 }
9949}
9950
9951static void simplify_sextract(struct compile_state *state, struct triple *ins)
9952{
9953 if (is_simple_const(RHS(ins, 0))) {
9954 ulong_t val;
9955 ulong_t mask;
9956 long_t sval;
9957 val = read_const(state, ins, RHS(ins, 0));
9958 mask = 1;
9959 mask <<= ins->u.bitfield.size;
9960 mask -= 1;
9961 val >>= ins->u.bitfield.offset;
9962 val &= mask;
9963 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9964 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009965 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009966 mkconst(state, ins, sval);
9967 }
9968}
9969
9970static void simplify_deposit(struct compile_state *state, struct triple *ins)
9971{
9972 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9973 ulong_t targ, val;
9974 ulong_t mask;
9975 targ = read_const(state, ins, RHS(ins, 0));
9976 val = read_const(state, ins, RHS(ins, 1));
9977 mask = 1;
9978 mask <<= ins->u.bitfield.size;
9979 mask -= 1;
9980 mask <<= ins->u.bitfield.offset;
9981 targ &= ~mask;
9982 val <<= ins->u.bitfield.offset;
9983 val &= mask;
9984 targ |= val;
9985 mkconst(state, ins, targ);
9986 }
9987}
9988
Eric Biedermanb138ac82003-04-22 18:44:01 +00009989static void simplify_copy(struct compile_state *state, struct triple *ins)
9990{
Eric Biederman90089602004-05-28 14:11:54 +00009991 struct triple *right;
9992 right = RHS(ins, 0);
9993 if (is_subset_type(ins->type, right->type)) {
9994 ins->type = right->type;
9995 }
9996 if (equiv_types(ins->type, right->type)) {
9997 ins->op = OP_COPY;/* I don't need to convert if the types match */
9998 } else {
9999 if (ins->op == OP_COPY) {
10000 internal_error(state, ins, "type mismatch on copy");
10001 }
10002 }
10003 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10004 struct triple *sdecl;
10005 ulong_t offset;
10006 sdecl = MISC(right, 0);
10007 offset = right->u.cval;
10008 mkaddr_const(state, ins, sdecl, offset);
10009 }
10010 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10011 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010012 case OP_INTCONST:
10013 {
10014 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010015 left = read_const(state, ins, right);
10016 /* Ensure I have not overflowed the destination. */
10017 if (size_of(state, right->type) > size_of(state, ins->type)) {
10018 ulong_t mask;
10019 mask = 1;
10020 mask <<= size_of(state, ins->type);
10021 mask -= 1;
10022 left &= mask;
10023 }
10024 /* Ensure I am properly sign extended */
10025 if (size_of(state, right->type) < size_of(state, ins->type) &&
10026 is_signed(right->type)) {
10027 long_t val;
10028 int shift;
10029 shift = SIZEOF_LONG - size_of(state, right->type);
10030 val = left;
10031 val <<= shift;
10032 val >>= shift;
10033 left = val;
10034 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010035 mkconst(state, ins, left);
10036 break;
10037 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010038 default:
10039 internal_error(state, ins, "uknown constant");
10040 break;
10041 }
10042 }
10043}
10044
Eric Biederman83b991a2003-10-11 06:20:25 +000010045static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010046{
10047 struct triple *ptr;
10048 if (!block) {
10049 return 0;
10050 }
10051 ptr = block->first;
10052 do {
10053 if (ptr->op == OP_PHI) {
10054 return 1;
10055 }
10056 ptr = ptr->next;
10057 } while(ptr != block->last);
10058 return 0;
10059}
10060
Eric Biederman83b991a2003-10-11 06:20:25 +000010061static int phi_dependency(struct block *block)
10062{
10063 /* A block has a phi dependency if a phi function
10064 * depends on that block to exist, and makes a block
10065 * that is otherwise useless unsafe to remove.
10066 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010067 if (block) {
10068 struct block_set *edge;
10069 for(edge = block->edges; edge; edge = edge->next) {
10070 if (phi_present(edge->member)) {
10071 return 1;
10072 }
10073 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010074 }
10075 return 0;
10076}
10077
10078static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10079{
10080 struct triple *targ;
10081 targ = TARG(ins, 0);
10082 /* During scc_transform temporary triples are allocated that
10083 * loop back onto themselves. If I see one don't advance the
10084 * target.
10085 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010086 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010087 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010088 targ = targ->next;
10089 }
10090 return targ;
10091}
10092
10093
10094static void simplify_branch(struct compile_state *state, struct triple *ins)
10095{
Eric Biederman90089602004-05-28 14:11:54 +000010096 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010097 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010098 internal_error(state, ins, "not branch");
10099 }
10100 if (ins->use != 0) {
10101 internal_error(state, ins, "branch use");
10102 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010103 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010104 * make modifications to the control flow graph as well
10105 * as to the branch instruction itself. That is handled
10106 * by rebuilding the basic blocks after simplify all is called.
10107 */
10108
10109 /* If we have a branch to an unconditional branch update
10110 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010111 * functions.
10112 * Also only do this a limited number of times so
10113 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010114 */
Eric Biederman90089602004-05-28 14:11:54 +000010115 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010116 do {
10117 struct triple *targ;
10118 simplified = 0;
10119 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010120 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010121 !phi_dependency(targ->u.block))
10122 {
10123 unuse_triple(TARG(ins, 0), ins);
10124 TARG(ins, 0) = TARG(targ, 0);
10125 use_triple(TARG(ins, 0), ins);
10126 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010127 }
Eric Biederman90089602004-05-28 14:11:54 +000010128 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010129
10130 /* If we have a conditional branch with a constant condition
10131 * make it an unconditional branch.
10132 */
Eric Biederman90089602004-05-28 14:11:54 +000010133 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010134 struct triple *targ;
10135 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010136 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010137 unuse_triple(RHS(ins, 0), ins);
10138 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010139 ins->rhs = 0;
10140 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010141 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010142 if (value) {
10143 unuse_triple(ins->next, ins);
10144 TARG(ins, 0) = targ;
10145 }
10146 else {
10147 unuse_triple(targ, ins);
10148 TARG(ins, 0) = ins->next;
10149 }
10150 }
Eric Biederman90089602004-05-28 14:11:54 +000010151
10152 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010153 * make it a noop.
10154 */
10155 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010156 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010157 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010158 unuse_triple(RHS(ins, 0), ins);
10159 unuse_triple(ins->next, ins);
10160 }
Eric Biederman90089602004-05-28 14:11:54 +000010161 ins->lhs = 0;
10162 ins->rhs = 0;
10163 ins->misc = 0;
10164 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010165 ins->op = OP_NOOP;
10166 if (ins->use) {
10167 internal_error(state, ins, "noop use != 0");
10168 }
10169 }
10170}
10171
Eric Biederman530b5192003-07-01 10:05:30 +000010172static void simplify_label(struct compile_state *state, struct triple *ins)
10173{
Eric Biederman83b991a2003-10-11 06:20:25 +000010174 /* Ignore volatile labels */
10175 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010176 return;
10177 }
10178 if (ins->use == 0) {
10179 ins->op = OP_NOOP;
10180 }
10181 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010182 /* In general it is not safe to merge one label that
10183 * imediately follows another. The problem is that the empty
10184 * looking block may have phi functions that depend on it.
10185 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010186 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010187 struct triple_set *user, *next;
10188 ins->op = OP_NOOP;
10189 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010190 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010191 next = user->next;
10192 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010193 expr = triple_targ(state, use, 0);
10194 for(;expr; expr = triple_targ(state, use, expr)) {
10195 if (*expr == ins) {
10196 *expr = ins->prev;
10197 unuse_triple(ins, use);
10198 use_triple(ins->prev, use);
10199 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010200
Eric Biederman530b5192003-07-01 10:05:30 +000010201 }
10202 }
10203 if (ins->use) {
10204 internal_error(state, ins, "noop use != 0");
10205 }
10206 }
10207 }
10208}
10209
Eric Biedermanb138ac82003-04-22 18:44:01 +000010210static void simplify_phi(struct compile_state *state, struct triple *ins)
10211{
Eric Biederman83b991a2003-10-11 06:20:25 +000010212 struct triple **slot;
10213 struct triple *value;
10214 int zrhs, i;
10215 ulong_t cvalue;
10216 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010217 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010218 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010219 return;
10220 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010221 /* See if all of the rhs members of a phi have the same value */
10222 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010223 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010224 for(i = 1; i < zrhs; i++) {
10225 if ( !slot[i] ||
10226 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010227 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010228 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010229 break;
10230 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010231 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010232 if (i == zrhs) {
10233 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010234 return;
10235 }
10236 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010237
Eric Biederman83b991a2003-10-11 06:20:25 +000010238 /* See if all of rhs members of a phi are the same */
10239 value = slot[0];
10240 for(i = 1; i < zrhs; i++) {
10241 if (slot[i] != value) {
10242 break;
10243 }
10244 }
10245 if (i == zrhs) {
10246 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010247 if (!is_subset_type(ins->type, value->type)) {
10248 internal_error(state, ins, "bad input type to phi");
10249 }
10250 /* Make the types match */
10251 if (!equiv_types(ins->type, value->type)) {
10252 ins->type = value->type;
10253 }
10254 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010255 mkcopy(state, ins, value);
10256 return;
10257 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010258}
10259
10260
10261static void simplify_bsf(struct compile_state *state, struct triple *ins)
10262{
Eric Biederman90089602004-05-28 14:11:54 +000010263 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010264 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010265 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010266 mkconst(state, ins, bsf(left));
10267 }
10268}
10269
10270static void simplify_bsr(struct compile_state *state, struct triple *ins)
10271{
Eric Biederman90089602004-05-28 14:11:54 +000010272 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010273 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010274 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010275 mkconst(state, ins, bsr(left));
10276 }
10277}
10278
10279
10280typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010281static const struct simplify_table {
10282 simplify_t func;
10283 unsigned long flag;
10284} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010285#define simplify_sdivt simplify_noop
10286#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010287#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010288
Eric Biederman5ade04a2003-10-22 04:03:46 +000010289[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10290[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10291[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10292[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10293[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10294[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10295[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10296[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10297[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10298[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10299[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10300[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10301[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10302[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10303[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10304[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10305[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10306[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10307[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010308
Eric Biederman5ade04a2003-10-22 04:03:46 +000010309[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10310[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10311[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10312[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10318[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10319[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10320[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010321
Eric Biederman90089602004-05-28 14:11:54 +000010322[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010323[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010324
Eric Biederman90089602004-05-28 14:11:54 +000010325[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10326[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10327[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10328
Eric Biederman5ade04a2003-10-22 04:03:46 +000010329[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010330
Eric Biederman5ade04a2003-10-22 04:03:46 +000010331[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10332[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10333[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010334[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010335
Eric Biederman5ade04a2003-10-22 04:03:46 +000010336[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10337[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10338[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010339[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010340[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10341[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010342
Eric Biederman5ade04a2003-10-22 04:03:46 +000010343[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010344[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010345
Eric Biederman5ade04a2003-10-22 04:03:46 +000010346[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10347[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10348[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10349[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10350[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10351[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10352[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10353[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10354[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010355
Eric Biederman5ade04a2003-10-22 04:03:46 +000010356[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10357[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10358[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10359[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10360[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10361[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10362[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10363[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10364[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010365[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010366[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010367};
10368
Stefan Reinauer14e22772010-04-27 06:56:47 +000010369static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010370 simplify_t do_simplify, struct triple *ins)
10371{
10372#if DEBUG_SIMPLIFY_HIRES
10373 if (state->functions_joined && (do_simplify != simplify_noop)) {
10374 /* High resolution debugging mode */
10375 fprintf(state->dbgout, "simplifing: ");
10376 display_triple(state->dbgout, ins);
10377 }
10378#endif
10379 do_simplify(state, ins);
10380#if DEBUG_SIMPLIFY_HIRES
10381 if (state->functions_joined && (do_simplify != simplify_noop)) {
10382 /* High resolution debugging mode */
10383 fprintf(state->dbgout, "simplified: ");
10384 display_triple(state->dbgout, ins);
10385 }
10386#endif
10387}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010388static void simplify(struct compile_state *state, struct triple *ins)
10389{
10390 int op;
10391 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010392 if (ins == &unknown_triple) {
10393 internal_error(state, ins, "simplifying the unknown triple?");
10394 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010395 do {
10396 op = ins->op;
10397 do_simplify = 0;
Patrick Georgi0dde01c2013-11-11 15:57:09 +010010398 if ((op < 0) || (op >= sizeof(table_simplify)/sizeof(table_simplify[0]))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010399 do_simplify = 0;
10400 }
Eric Biederman90089602004-05-28 14:11:54 +000010401 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010402 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010403 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010404 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010405 !(state->compiler->flags & table_simplify[op].flag)) {
10406 do_simplify = simplify_noop;
10407 }
10408 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010409 do_simplify = simplify_noop;
10410 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010411
Eric Biedermanb138ac82003-04-22 18:44:01 +000010412 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010413 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010414 op, tops(op));
10415 return;
10416 }
Eric Biederman90089602004-05-28 14:11:54 +000010417 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010418 } while(ins->op != op);
10419}
10420
Eric Biederman5ade04a2003-10-22 04:03:46 +000010421static void rebuild_ssa_form(struct compile_state *state);
10422
Eric Biedermanb138ac82003-04-22 18:44:01 +000010423static void simplify_all(struct compile_state *state)
10424{
10425 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010426 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10427 return;
10428 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010429 first = state->first;
10430 ins = first->prev;
10431 do {
10432 simplify(state, ins);
10433 ins = ins->prev;
10434 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010435 ins = first;
10436 do {
10437 simplify(state, ins);
10438 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010439 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010440 rebuild_ssa_form(state);
10441
Eric Biederman90089602004-05-28 14:11:54 +000010442 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010443}
10444
10445/*
10446 * Builtins....
10447 * ============================
10448 */
10449
Eric Biederman0babc1c2003-05-09 02:39:00 +000010450static void register_builtin_function(struct compile_state *state,
10451 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010452{
Eric Biederman90089602004-05-28 14:11:54 +000010453 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010454 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010455 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010456 struct file_state file;
10457 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010458 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010459 va_list args;
10460 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010461
10462 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010463 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010464 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010465 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010466 file.report_line = 1;
10467 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010468 file.prev = state->file;
10469 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010470 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010471
10472 /* Find the Parameter count */
10473 valid_op(state, op);
10474 parameters = table_ops[op].rhs;
10475 if (parameters < 0 ) {
10476 internal_error(state, 0, "Invalid builtin parameter count");
10477 }
10478
10479 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010480 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010481 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010482 next = &ftype->right;
10483 va_start(args, rtype);
10484 for(i = 0; i < parameters; i++) {
10485 atype = va_arg(args, struct type *);
10486 if (!*next) {
10487 *next = atype;
10488 } else {
10489 *next = new_type(TYPE_PRODUCT, *next, atype);
10490 next = &((*next)->right);
10491 }
10492 }
10493 if (!*next) {
10494 *next = &void_type;
10495 }
10496 va_end(args);
10497
Eric Biederman90089602004-05-28 14:11:54 +000010498 /* Get the initial closure type */
10499 ctype = new_type(TYPE_JOIN, &void_type, 0);
10500 ctype->elements = 1;
10501
10502 /* Get the return type */
10503 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10504 crtype->elements = 2;
10505
Eric Biedermanb138ac82003-04-22 18:44:01 +000010506 /* Generate the needed triples */
10507 def = triple(state, OP_LIST, ftype, 0, 0);
10508 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010509 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010510 result = flatten(state, first, variable(state, crtype));
10511 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010512 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010513
10514 /* Now string them together */
10515 param = ftype->right;
10516 for(i = 0; i < parameters; i++) {
10517 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10518 atype = param->left;
10519 } else {
10520 atype = param;
10521 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010522 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010523 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010524 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010525 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010526 generate_lhs_pieces(state, work);
10527 for(i = 0; i < parameters; i++) {
10528 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010529 }
Eric Biederman90089602004-05-28 14:11:54 +000010530 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10531 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010532 }
10533 work = flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010534 flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010535 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010536 name_len = strlen(name);
10537 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010538 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010539 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010540
Eric Biedermanb138ac82003-04-22 18:44:01 +000010541 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010542 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010543 state->main_function = 0;
10544
Eric Biederman5ade04a2003-10-22 04:03:46 +000010545 if (!state->functions) {
10546 state->functions = def;
10547 } else {
10548 insert_triple(state, state->functions, def);
10549 }
10550 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010551 FILE *fp = state->dbgout;
10552 fprintf(fp, "\n");
10553 loc(fp, state, 0);
10554 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10555 display_func(state, fp, def);
10556 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010557 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010558}
10559
Eric Biederman0babc1c2003-05-09 02:39:00 +000010560static struct type *partial_struct(struct compile_state *state,
10561 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010562{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010563 struct hash_entry *field_ident;
10564 struct type *result;
10565 int field_name_len;
10566
10567 field_name_len = strlen(field_name);
10568 field_ident = lookup(state, field_name, field_name_len);
10569
10570 result = clone_type(0, type);
10571 result->field_ident = field_ident;
10572
10573 if (rest) {
10574 result = new_type(TYPE_PRODUCT, result, rest);
10575 }
10576 return result;
10577}
10578
10579static struct type *register_builtin_type(struct compile_state *state,
10580 const char *name, struct type *type)
10581{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010582 struct hash_entry *ident;
10583 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010584
Eric Biedermanb138ac82003-04-22 18:44:01 +000010585 name_len = strlen(name);
10586 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010587
Eric Biederman0babc1c2003-05-09 02:39:00 +000010588 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10589 ulong_t elements = 0;
10590 struct type *field;
10591 type = new_type(TYPE_STRUCT, type, 0);
10592 field = type->left;
10593 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10594 elements++;
10595 field = field->right;
10596 }
10597 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010598 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010599 type->type_ident = ident;
10600 type->elements = elements;
10601 }
10602 symbol(state, ident, &ident->sym_ident, 0, type);
10603 ident->tok = TOK_TYPE_NAME;
10604 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010605}
10606
Eric Biederman0babc1c2003-05-09 02:39:00 +000010607
Eric Biedermanb138ac82003-04-22 18:44:01 +000010608static void register_builtins(struct compile_state *state)
10609{
Eric Biederman530b5192003-07-01 10:05:30 +000010610 struct type *div_type, *ldiv_type;
10611 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010612 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010613
Eric Biederman530b5192003-07-01 10:05:30 +000010614 div_type = register_builtin_type(state, "__builtin_div_t",
10615 partial_struct(state, "quot", &int_type,
10616 partial_struct(state, "rem", &int_type, 0)));
10617 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10618 partial_struct(state, "quot", &long_type,
10619 partial_struct(state, "rem", &long_type, 0)));
10620 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10621 partial_struct(state, "quot", &uint_type,
10622 partial_struct(state, "rem", &uint_type, 0)));
10623 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10624 partial_struct(state, "quot", &ulong_type,
10625 partial_struct(state, "rem", &ulong_type, 0)));
10626
10627 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10628 &int_type, &int_type);
10629 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10630 &long_type, &long_type);
10631 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10632 &uint_type, &uint_type);
10633 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10634 &ulong_type, &ulong_type);
10635
Stefan Reinauer14e22772010-04-27 06:56:47 +000010636 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010637 &ushort_type);
10638 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10639 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010640 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010641 &ushort_type);
10642
Stefan Reinauer14e22772010-04-27 06:56:47 +000010643 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010644 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010645 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010646 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010647 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010648 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010649
10650 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010651 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010652 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010653 &int_type);
10654
10655 msr_type = register_builtin_type(state, "__builtin_msr_t",
10656 partial_struct(state, "lo", &ulong_type,
10657 partial_struct(state, "hi", &ulong_type, 0)));
10658
10659 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10660 &ulong_type);
10661 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10662 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010663
10664 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010665 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010666}
10667
10668static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010669 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010670 struct hash_entry **ident, int need_ident);
10671static void decl(struct compile_state *state, struct triple *first);
10672static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010673#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010674static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010675#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010676static struct type *decl_specifiers(struct compile_state *state);
10677static int istype(int tok);
10678static struct triple *expr(struct compile_state *state);
10679static struct triple *assignment_expr(struct compile_state *state);
10680static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010681static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010682
10683static struct triple *call_expr(
10684 struct compile_state *state, struct triple *func)
10685{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010686 struct triple *def;
10687 struct type *param, *type;
10688 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010689
10690 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10691 error(state, 0, "Called object is not a function");
10692 }
10693 if (func->op != OP_LIST) {
10694 internal_error(state, 0, "improper function");
10695 }
10696 eat(state, TOK_LPAREN);
10697 /* Find the return type without any specifiers */
10698 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010699 /* Count the number of rhs entries for OP_FCALL */
10700 param = func->type->right;
10701 pvals = 0;
10702 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10703 pvals++;
10704 param = param->right;
10705 }
10706 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10707 pvals++;
10708 }
10709 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010710 MISC(def, 0) = func;
10711
10712 param = func->type->right;
10713 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010714 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010715 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010716 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010717 arg_type = param;
10718 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10719 arg_type = param->left;
10720 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010721 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010722 RHS(def, index) = val;
10723 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010724 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010725 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010726 }
10727 }
10728 eat(state, TOK_RPAREN);
10729 return def;
10730}
10731
10732
10733static struct triple *character_constant(struct compile_state *state)
10734{
10735 struct triple *def;
10736 struct token *tk;
10737 const signed char *str, *end;
10738 int c;
10739 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010740 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010741 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010742 str_len = tk->str_len - 2;
10743 if (str_len <= 0) {
10744 error(state, 0, "empty character constant");
10745 }
10746 end = str + str_len;
10747 c = char_value(state, &str, end);
10748 if (str != end) {
10749 error(state, 0, "multibyte character constant not supported");
10750 }
10751 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10752 return def;
10753}
10754
10755static struct triple *string_constant(struct compile_state *state)
10756{
10757 struct triple *def;
10758 struct token *tk;
10759 struct type *type;
10760 const signed char *str, *end;
10761 signed char *buf, *ptr;
10762 int str_len;
10763
10764 buf = 0;
10765 type = new_type(TYPE_ARRAY, &char_type, 0);
10766 type->elements = 0;
10767 /* The while loop handles string concatenation */
10768 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010769 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010770 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010771 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010772 if (str_len < 0) {
10773 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010774 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010775 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010776 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010777 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010778 end = str + str_len;
10779 ptr = buf;
10780 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10781 memcpy(buf, ptr, type->elements);
10782 ptr = buf + type->elements;
10783 do {
10784 *ptr++ = char_value(state, &str, end);
10785 } while(str < end);
10786 type->elements = ptr - buf;
10787 } while(peek(state) == TOK_LIT_STRING);
10788 *ptr = '\0';
10789 type->elements += 1;
10790 def = triple(state, OP_BLOBCONST, type, 0, 0);
10791 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010792
Eric Biedermanb138ac82003-04-22 18:44:01 +000010793 return def;
10794}
10795
10796
10797static struct triple *integer_constant(struct compile_state *state)
10798{
10799 struct triple *def;
10800 unsigned long val;
10801 struct token *tk;
10802 char *end;
10803 int u, l, decimal;
10804 struct type *type;
10805
Eric Biederman41203d92004-11-08 09:31:09 +000010806 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010807 errno = 0;
10808 decimal = (tk->val.str[0] != '0');
10809 val = strtoul(tk->val.str, &end, 0);
Patrick Georgib6239b82014-08-09 19:42:08 +020010810 if (errno == ERANGE) {
10811 error(state, 0, "Integer constant out of range");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010812 }
10813 u = l = 0;
10814 if ((*end == 'u') || (*end == 'U')) {
10815 u = 1;
10816 end++;
10817 }
10818 if ((*end == 'l') || (*end == 'L')) {
10819 l = 1;
10820 end++;
10821 }
10822 if ((*end == 'u') || (*end == 'U')) {
10823 u = 1;
10824 end++;
10825 }
10826 if (*end) {
10827 error(state, 0, "Junk at end of integer constant");
10828 }
10829 if (u && l) {
10830 type = &ulong_type;
10831 }
10832 else if (l) {
10833 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010834 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010835 type = &ulong_type;
10836 }
10837 }
10838 else if (u) {
10839 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010840 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010841 type = &ulong_type;
10842 }
10843 }
10844 else {
10845 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010846 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010847 type = &uint_type;
10848 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010849 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010850 type = &ulong_type;
10851 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010852 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010853 type = &long_type;
10854 }
10855 }
10856 def = int_const(state, type, val);
10857 return def;
10858}
10859
10860static struct triple *primary_expr(struct compile_state *state)
10861{
10862 struct triple *def;
10863 int tok;
10864 tok = peek(state);
10865 switch(tok) {
10866 case TOK_IDENT:
10867 {
10868 struct hash_entry *ident;
10869 /* Here ident is either:
10870 * a varable name
10871 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010872 */
Eric Biederman41203d92004-11-08 09:31:09 +000010873 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010874 if (!ident->sym_ident) {
10875 error(state, 0, "%s undeclared", ident->name);
10876 }
10877 def = ident->sym_ident->def;
10878 break;
10879 }
10880 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010881 {
10882 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010883 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010884 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010885 if (!ident->sym_ident) {
10886 error(state, 0, "%s undeclared", ident->name);
10887 }
10888 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010889 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010890 }
Eric Biederman41203d92004-11-08 09:31:09 +000010891 case TOK_MIDENT:
10892 {
10893 struct hash_entry *ident;
10894 ident = eat(state, TOK_MIDENT)->ident;
10895 warning(state, 0, "Replacing undefined macro: %s with 0",
10896 ident->name);
10897 def = int_const(state, &int_type, 0);
10898 break;
10899 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010900 case TOK_LPAREN:
10901 eat(state, TOK_LPAREN);
10902 def = expr(state);
10903 eat(state, TOK_RPAREN);
10904 break;
10905 case TOK_LIT_INT:
10906 def = integer_constant(state);
10907 break;
10908 case TOK_LIT_FLOAT:
10909 eat(state, TOK_LIT_FLOAT);
10910 error(state, 0, "Floating point constants not supported");
10911 def = 0;
10912 FINISHME();
10913 break;
10914 case TOK_LIT_CHAR:
10915 def = character_constant(state);
10916 break;
10917 case TOK_LIT_STRING:
10918 def = string_constant(state);
10919 break;
10920 default:
10921 def = 0;
10922 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10923 }
10924 return def;
10925}
10926
10927static struct triple *postfix_expr(struct compile_state *state)
10928{
10929 struct triple *def;
10930 int postfix;
10931 def = primary_expr(state);
10932 do {
10933 struct triple *left;
10934 int tok;
10935 postfix = 1;
10936 left = def;
10937 switch((tok = peek(state))) {
10938 case TOK_LBRACKET:
10939 eat(state, TOK_LBRACKET);
10940 def = mk_subscript_expr(state, left, expr(state));
10941 eat(state, TOK_RBRACKET);
10942 break;
10943 case TOK_LPAREN:
10944 def = call_expr(state, def);
10945 break;
10946 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010947 {
10948 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010949 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010950 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010951 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010952 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010953 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010954 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010955 {
10956 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010957 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010958 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010959 def = mk_deref_expr(state, read_expr(state, def));
10960 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010961 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010962 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010963 case TOK_PLUSPLUS:
10964 eat(state, TOK_PLUSPLUS);
10965 def = mk_post_inc_expr(state, left);
10966 break;
10967 case TOK_MINUSMINUS:
10968 eat(state, TOK_MINUSMINUS);
10969 def = mk_post_dec_expr(state, left);
10970 break;
10971 default:
10972 postfix = 0;
10973 break;
10974 }
10975 } while(postfix);
10976 return def;
10977}
10978
10979static struct triple *cast_expr(struct compile_state *state);
10980
10981static struct triple *unary_expr(struct compile_state *state)
10982{
10983 struct triple *def, *right;
10984 int tok;
10985 switch((tok = peek(state))) {
10986 case TOK_PLUSPLUS:
10987 eat(state, TOK_PLUSPLUS);
10988 def = mk_pre_inc_expr(state, unary_expr(state));
10989 break;
10990 case TOK_MINUSMINUS:
10991 eat(state, TOK_MINUSMINUS);
10992 def = mk_pre_dec_expr(state, unary_expr(state));
10993 break;
10994 case TOK_AND:
10995 eat(state, TOK_AND);
10996 def = mk_addr_expr(state, cast_expr(state), 0);
10997 break;
10998 case TOK_STAR:
10999 eat(state, TOK_STAR);
11000 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11001 break;
11002 case TOK_PLUS:
11003 eat(state, TOK_PLUS);
11004 right = read_expr(state, cast_expr(state));
11005 arithmetic(state, right);
11006 def = integral_promotion(state, right);
11007 break;
11008 case TOK_MINUS:
11009 eat(state, TOK_MINUS);
11010 right = read_expr(state, cast_expr(state));
11011 arithmetic(state, right);
11012 def = integral_promotion(state, right);
11013 def = triple(state, OP_NEG, def->type, def, 0);
11014 break;
11015 case TOK_TILDE:
11016 eat(state, TOK_TILDE);
11017 right = read_expr(state, cast_expr(state));
11018 integral(state, right);
11019 def = integral_promotion(state, right);
11020 def = triple(state, OP_INVERT, def->type, def, 0);
11021 break;
11022 case TOK_BANG:
11023 eat(state, TOK_BANG);
11024 right = read_expr(state, cast_expr(state));
11025 bool(state, right);
11026 def = lfalse_expr(state, right);
11027 break;
11028 case TOK_SIZEOF:
11029 {
11030 struct type *type;
11031 int tok1, tok2;
11032 eat(state, TOK_SIZEOF);
11033 tok1 = peek(state);
11034 tok2 = peek2(state);
11035 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11036 eat(state, TOK_LPAREN);
11037 type = type_name(state);
11038 eat(state, TOK_RPAREN);
11039 }
11040 else {
11041 struct triple *expr;
11042 expr = unary_expr(state);
11043 type = expr->type;
11044 release_expr(state, expr);
11045 }
Eric Biederman90089602004-05-28 14:11:54 +000011046 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011047 break;
11048 }
11049 case TOK_ALIGNOF:
11050 {
11051 struct type *type;
11052 int tok1, tok2;
11053 eat(state, TOK_ALIGNOF);
11054 tok1 = peek(state);
11055 tok2 = peek2(state);
11056 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11057 eat(state, TOK_LPAREN);
11058 type = type_name(state);
11059 eat(state, TOK_RPAREN);
11060 }
11061 else {
11062 struct triple *expr;
11063 expr = unary_expr(state);
11064 type = expr->type;
11065 release_expr(state, expr);
11066 }
Eric Biederman90089602004-05-28 14:11:54 +000011067 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011068 break;
11069 }
Eric Biederman41203d92004-11-08 09:31:09 +000011070 case TOK_MDEFINED:
11071 {
11072 /* We only come here if we are called from the preprocessor */
11073 struct hash_entry *ident;
11074 int parens;
11075 eat(state, TOK_MDEFINED);
11076 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011077 if (pp_peek(state) == TOK_LPAREN) {
11078 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011079 parens = 1;
11080 }
Eric Biedermancb364952004-11-15 10:46:44 +000011081 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011082 if (parens) {
11083 eat(state, TOK_RPAREN);
11084 }
11085 def = int_const(state, &int_type, ident->sym_define != 0);
11086 break;
11087 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011088 default:
11089 def = postfix_expr(state);
11090 break;
11091 }
11092 return def;
11093}
11094
11095static struct triple *cast_expr(struct compile_state *state)
11096{
11097 struct triple *def;
11098 int tok1, tok2;
11099 tok1 = peek(state);
11100 tok2 = peek2(state);
11101 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11102 struct type *type;
11103 eat(state, TOK_LPAREN);
11104 type = type_name(state);
11105 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011106 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011107 }
11108 else {
11109 def = unary_expr(state);
11110 }
11111 return def;
11112}
11113
11114static struct triple *mult_expr(struct compile_state *state)
11115{
11116 struct triple *def;
11117 int done;
11118 def = cast_expr(state);
11119 do {
11120 struct triple *left, *right;
11121 struct type *result_type;
11122 int tok, op, sign;
11123 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011124 tok = peek(state);
11125 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011126 case TOK_STAR:
11127 case TOK_DIV:
11128 case TOK_MOD:
11129 left = read_expr(state, def);
11130 arithmetic(state, left);
11131
11132 eat(state, tok);
11133
11134 right = read_expr(state, cast_expr(state));
11135 arithmetic(state, right);
11136
11137 result_type = arithmetic_result(state, left, right);
11138 sign = is_signed(result_type);
11139 op = -1;
11140 switch(tok) {
11141 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11142 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11143 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11144 }
11145 def = triple(state, op, result_type, left, right);
11146 break;
11147 default:
11148 done = 1;
11149 break;
11150 }
11151 } while(!done);
11152 return def;
11153}
11154
11155static struct triple *add_expr(struct compile_state *state)
11156{
11157 struct triple *def;
11158 int done;
11159 def = mult_expr(state);
11160 do {
11161 done = 0;
11162 switch( peek(state)) {
11163 case TOK_PLUS:
11164 eat(state, TOK_PLUS);
11165 def = mk_add_expr(state, def, mult_expr(state));
11166 break;
11167 case TOK_MINUS:
11168 eat(state, TOK_MINUS);
11169 def = mk_sub_expr(state, def, mult_expr(state));
11170 break;
11171 default:
11172 done = 1;
11173 break;
11174 }
11175 } while(!done);
11176 return def;
11177}
11178
11179static struct triple *shift_expr(struct compile_state *state)
11180{
11181 struct triple *def;
11182 int done;
11183 def = add_expr(state);
11184 do {
11185 struct triple *left, *right;
11186 int tok, op;
11187 done = 0;
11188 switch((tok = peek(state))) {
11189 case TOK_SL:
11190 case TOK_SR:
11191 left = read_expr(state, def);
11192 integral(state, left);
11193 left = integral_promotion(state, left);
11194
11195 eat(state, tok);
11196
11197 right = read_expr(state, add_expr(state));
11198 integral(state, right);
11199 right = integral_promotion(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011200
11201 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011202 is_signed(left->type)? OP_SSR: OP_USR;
11203
11204 def = triple(state, op, left->type, left, right);
11205 break;
11206 default:
11207 done = 1;
11208 break;
11209 }
11210 } while(!done);
11211 return def;
11212}
11213
11214static struct triple *relational_expr(struct compile_state *state)
11215{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011216#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011217#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011218#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011219 struct triple *def;
11220 int done;
11221 def = shift_expr(state);
11222 do {
11223 struct triple *left, *right;
11224 struct type *arg_type;
11225 int tok, op, sign;
11226 done = 0;
11227 switch((tok = peek(state))) {
11228 case TOK_LESS:
11229 case TOK_MORE:
11230 case TOK_LESSEQ:
11231 case TOK_MOREEQ:
11232 left = read_expr(state, def);
11233 arithmetic(state, left);
11234
11235 eat(state, tok);
11236
11237 right = read_expr(state, shift_expr(state));
11238 arithmetic(state, right);
11239
11240 arg_type = arithmetic_result(state, left, right);
11241 sign = is_signed(arg_type);
11242 op = -1;
11243 switch(tok) {
11244 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11245 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11246 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11247 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11248 }
11249 def = triple(state, op, &int_type, left, right);
11250 break;
11251 default:
11252 done = 1;
11253 break;
11254 }
11255 } while(!done);
11256 return def;
11257}
11258
11259static struct triple *equality_expr(struct compile_state *state)
11260{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011261#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011262#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011263#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011264 struct triple *def;
11265 int done;
11266 def = relational_expr(state);
11267 do {
11268 struct triple *left, *right;
11269 int tok, op;
11270 done = 0;
11271 switch((tok = peek(state))) {
11272 case TOK_EQEQ:
11273 case TOK_NOTEQ:
11274 left = read_expr(state, def);
11275 arithmetic(state, left);
11276 eat(state, tok);
11277 right = read_expr(state, relational_expr(state));
11278 arithmetic(state, right);
11279 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11280 def = triple(state, op, &int_type, left, right);
11281 break;
11282 default:
11283 done = 1;
11284 break;
11285 }
11286 } while(!done);
11287 return def;
11288}
11289
11290static struct triple *and_expr(struct compile_state *state)
11291{
11292 struct triple *def;
11293 def = equality_expr(state);
11294 while(peek(state) == TOK_AND) {
11295 struct triple *left, *right;
11296 struct type *result_type;
11297 left = read_expr(state, def);
11298 integral(state, left);
11299 eat(state, TOK_AND);
11300 right = read_expr(state, equality_expr(state));
11301 integral(state, right);
11302 result_type = arithmetic_result(state, left, right);
11303 def = triple(state, OP_AND, result_type, left, right);
11304 }
11305 return def;
11306}
11307
11308static struct triple *xor_expr(struct compile_state *state)
11309{
11310 struct triple *def;
11311 def = and_expr(state);
11312 while(peek(state) == TOK_XOR) {
11313 struct triple *left, *right;
11314 struct type *result_type;
11315 left = read_expr(state, def);
11316 integral(state, left);
11317 eat(state, TOK_XOR);
11318 right = read_expr(state, and_expr(state));
11319 integral(state, right);
11320 result_type = arithmetic_result(state, left, right);
11321 def = triple(state, OP_XOR, result_type, left, right);
11322 }
11323 return def;
11324}
11325
11326static struct triple *or_expr(struct compile_state *state)
11327{
11328 struct triple *def;
11329 def = xor_expr(state);
11330 while(peek(state) == TOK_OR) {
11331 struct triple *left, *right;
11332 struct type *result_type;
11333 left = read_expr(state, def);
11334 integral(state, left);
11335 eat(state, TOK_OR);
11336 right = read_expr(state, xor_expr(state));
11337 integral(state, right);
11338 result_type = arithmetic_result(state, left, right);
11339 def = triple(state, OP_OR, result_type, left, right);
11340 }
11341 return def;
11342}
11343
11344static struct triple *land_expr(struct compile_state *state)
11345{
11346 struct triple *def;
11347 def = or_expr(state);
11348 while(peek(state) == TOK_LOGAND) {
11349 struct triple *left, *right;
11350 left = read_expr(state, def);
11351 bool(state, left);
11352 eat(state, TOK_LOGAND);
11353 right = read_expr(state, or_expr(state));
11354 bool(state, right);
11355
Eric Biederman90089602004-05-28 14:11:54 +000011356 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011357 ltrue_expr(state, left),
11358 ltrue_expr(state, right));
11359 }
11360 return def;
11361}
11362
11363static struct triple *lor_expr(struct compile_state *state)
11364{
11365 struct triple *def;
11366 def = land_expr(state);
11367 while(peek(state) == TOK_LOGOR) {
11368 struct triple *left, *right;
11369 left = read_expr(state, def);
11370 bool(state, left);
11371 eat(state, TOK_LOGOR);
11372 right = read_expr(state, land_expr(state));
11373 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011374
Stefan Reinauer14e22772010-04-27 06:56:47 +000011375 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011376 ltrue_expr(state, left),
11377 ltrue_expr(state, right));
11378 }
11379 return def;
11380}
11381
11382static struct triple *conditional_expr(struct compile_state *state)
11383{
11384 struct triple *def;
11385 def = lor_expr(state);
11386 if (peek(state) == TOK_QUEST) {
11387 struct triple *test, *left, *right;
11388 bool(state, def);
11389 test = ltrue_expr(state, read_expr(state, def));
11390 eat(state, TOK_QUEST);
11391 left = read_expr(state, expr(state));
11392 eat(state, TOK_COLON);
11393 right = read_expr(state, conditional_expr(state));
11394
Eric Biederman90089602004-05-28 14:11:54 +000011395 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011396 }
11397 return def;
11398}
11399
Eric Biederman41203d92004-11-08 09:31:09 +000011400struct cv_triple {
11401 struct triple *val;
11402 int id;
11403};
11404
11405static void set_cv(struct compile_state *state, struct cv_triple *cv,
11406 struct triple *dest, struct triple *val)
11407{
11408 if (cv[dest->id].val) {
11409 free_triple(state, cv[dest->id].val);
11410 }
11411 cv[dest->id].val = val;
11412}
11413static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11414 struct triple *src)
11415{
11416 return cv[src->id].val;
11417}
11418
Eric Biedermanb138ac82003-04-22 18:44:01 +000011419static struct triple *eval_const_expr(
11420 struct compile_state *state, struct triple *expr)
11421{
11422 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011423 if (is_const(expr)) {
11424 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011425 }
Eric Biederman00443072003-06-24 12:34:45 +000011426 else {
11427 /* If we don't start out as a constant simplify into one */
11428 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011429 struct cv_triple *cv;
11430 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011431 head = label(state); /* dummy initial triple */
11432 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011433 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011434 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011435 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011436 }
Eric Biederman41203d92004-11-08 09:31:09 +000011437 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11438 i = 1;
11439 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11440 cv[i].val = 0;
11441 cv[i].id = ptr->id;
11442 ptr->id = i;
11443 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011444 }
Eric Biederman41203d92004-11-08 09:31:09 +000011445 ptr = head->next;
11446 do {
11447 valid_ins(state, ptr);
11448 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011449 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011450 "unexpected %s in constant expression",
11451 tops(ptr->op));
11452 }
11453 else if (ptr->op == OP_LIST) {
11454 }
11455 else if (triple_is_structural(state, ptr)) {
11456 ptr = ptr->next;
11457 }
11458 else if (triple_is_ubranch(state, ptr)) {
11459 ptr = TARG(ptr, 0);
11460 }
11461 else if (triple_is_cbranch(state, ptr)) {
11462 struct triple *cond_val;
11463 cond_val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011464 if (!cond_val || !is_const(cond_val) ||
11465 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011466 {
11467 internal_error(state, ptr, "bad branch condition");
11468 }
11469 if (cond_val->u.cval == 0) {
11470 ptr = ptr->next;
11471 } else {
11472 ptr = TARG(ptr, 0);
11473 }
11474 }
11475 else if (triple_is_branch(state, ptr)) {
11476 error(state, ptr, "bad branch type in constant expression");
11477 }
11478 else if (ptr->op == OP_WRITE) {
11479 struct triple *val;
11480 val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011481
11482 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011483 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011484 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011485 copy_triple(state, val));
11486 ptr = ptr->next;
11487 }
11488 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011489 set_cv(state, cv, ptr,
11490 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011491 get_cv(state, cv, RHS(ptr, 0))));
11492 ptr = ptr->next;
11493 }
11494 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11495 struct triple *val, **rhs;
11496 val = copy_triple(state, ptr);
11497 rhs = triple_rhs(state, val, 0);
11498 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11499 if (!*rhs) {
11500 internal_error(state, ptr, "Missing rhs");
11501 }
11502 *rhs = get_cv(state, cv, *rhs);
11503 }
11504 simplify(state, val);
11505 set_cv(state, cv, ptr, val);
11506 ptr = ptr->next;
11507 }
11508 else {
11509 error(state, ptr, "impure operation in constant expression");
11510 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011511
Eric Biederman41203d92004-11-08 09:31:09 +000011512 } while(ptr != head);
11513
11514 /* Get the result value */
11515 def = get_cv(state, cv, head->prev);
11516 cv[head->prev->id].val = 0;
11517
11518 /* Free the temporary values */
11519 for(i = 0; i < count; i++) {
11520 if (cv[i].val) {
11521 free_triple(state, cv[i].val);
11522 cv[i].val = 0;
11523 }
11524 }
11525 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011526 /* Free the intermediate expressions */
11527 while(head->next != head) {
11528 release_triple(state, head->next);
11529 }
11530 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011531 }
Eric Biederman41203d92004-11-08 09:31:09 +000011532 if (!is_const(def)) {
11533 error(state, expr, "Not a constant expression");
11534 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011535 return def;
11536}
11537
11538static struct triple *constant_expr(struct compile_state *state)
11539{
11540 return eval_const_expr(state, conditional_expr(state));
11541}
11542
11543static struct triple *assignment_expr(struct compile_state *state)
11544{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011545 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011546 int tok, op, sign;
11547 /* The C grammer in K&R shows assignment expressions
11548 * only taking unary expressions as input on their
11549 * left hand side. But specifies the precedence of
11550 * assignemnt as the lowest operator except for comma.
11551 *
11552 * Allowing conditional expressions on the left hand side
11553 * of an assignement results in a grammar that accepts
11554 * a larger set of statements than standard C. As long
11555 * as the subset of the grammar that is standard C behaves
11556 * correctly this should cause no problems.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011557 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011558 * For the extra token strings accepted by the grammar
11559 * none of them should produce a valid lvalue, so they
11560 * should not produce functioning programs.
11561 *
11562 * GCC has this bug as well, so surprises should be minimal.
11563 */
11564 def = conditional_expr(state);
11565 left = def;
11566 switch((tok = peek(state))) {
11567 case TOK_EQ:
11568 lvalue(state, left);
11569 eat(state, TOK_EQ);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011570 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011571 read_expr(state, assignment_expr(state)));
11572 break;
11573 case TOK_TIMESEQ:
11574 case TOK_DIVEQ:
11575 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011576 lvalue(state, left);
11577 arithmetic(state, left);
11578 eat(state, tok);
11579 right = read_expr(state, assignment_expr(state));
11580 arithmetic(state, right);
11581
11582 sign = is_signed(left->type);
11583 op = -1;
11584 switch(tok) {
11585 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11586 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11587 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011588 }
11589 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011590 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011591 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011592 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011593 case TOK_PLUSEQ:
11594 lvalue(state, left);
11595 eat(state, TOK_PLUSEQ);
11596 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011597 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011598 break;
11599 case TOK_MINUSEQ:
11600 lvalue(state, left);
11601 eat(state, TOK_MINUSEQ);
11602 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011603 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011604 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011605 case TOK_SLEQ:
11606 case TOK_SREQ:
11607 case TOK_ANDEQ:
11608 case TOK_XOREQ:
11609 case TOK_OREQ:
11610 lvalue(state, left);
11611 integral(state, left);
11612 eat(state, tok);
11613 right = read_expr(state, assignment_expr(state));
11614 integral(state, right);
11615 right = integral_promotion(state, right);
11616 sign = is_signed(left->type);
11617 op = -1;
11618 switch(tok) {
11619 case TOK_SLEQ: op = OP_SL; break;
11620 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11621 case TOK_ANDEQ: op = OP_AND; break;
11622 case TOK_XOREQ: op = OP_XOR; break;
11623 case TOK_OREQ: op = OP_OR; break;
11624 }
11625 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011626 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011627 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011628 break;
11629 }
11630 return def;
11631}
11632
11633static struct triple *expr(struct compile_state *state)
11634{
11635 struct triple *def;
11636 def = assignment_expr(state);
11637 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011638 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011639 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011640 }
11641 return def;
11642}
11643
11644static void expr_statement(struct compile_state *state, struct triple *first)
11645{
11646 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011647 /* lvalue conversions always apply except when certian operators
11648 * are applied. I apply the lvalue conversions here
11649 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011650 */
11651 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011652 }
11653 eat(state, TOK_SEMI);
11654}
11655
11656static void if_statement(struct compile_state *state, struct triple *first)
11657{
11658 struct triple *test, *jmp1, *jmp2, *middle, *end;
11659
11660 jmp1 = jmp2 = middle = 0;
11661 eat(state, TOK_IF);
11662 eat(state, TOK_LPAREN);
11663 test = expr(state);
11664 bool(state, test);
11665 /* Cleanup and invert the test */
11666 test = lfalse_expr(state, read_expr(state, test));
11667 eat(state, TOK_RPAREN);
11668 /* Generate the needed pieces */
11669 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011670 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011671 /* Thread the pieces together */
11672 flatten(state, first, test);
11673 flatten(state, first, jmp1);
11674 flatten(state, first, label(state));
11675 statement(state, first);
11676 if (peek(state) == TOK_ELSE) {
11677 eat(state, TOK_ELSE);
11678 /* Generate the rest of the pieces */
11679 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011680 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011681 /* Thread them together */
11682 flatten(state, first, jmp2);
11683 flatten(state, first, middle);
11684 statement(state, first);
11685 flatten(state, first, end);
11686 }
11687 else {
11688 flatten(state, first, middle);
11689 }
11690}
11691
11692static void for_statement(struct compile_state *state, struct triple *first)
11693{
11694 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11695 struct triple *label1, *label2, *label3;
11696 struct hash_entry *ident;
11697
11698 eat(state, TOK_FOR);
11699 eat(state, TOK_LPAREN);
11700 head = test = tail = jmp1 = jmp2 = 0;
11701 if (peek(state) != TOK_SEMI) {
11702 head = expr(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011703 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011704 eat(state, TOK_SEMI);
11705 if (peek(state) != TOK_SEMI) {
11706 test = expr(state);
11707 bool(state, test);
11708 test = ltrue_expr(state, read_expr(state, test));
11709 }
11710 eat(state, TOK_SEMI);
11711 if (peek(state) != TOK_RPAREN) {
11712 tail = expr(state);
11713 }
11714 eat(state, TOK_RPAREN);
11715 /* Generate the needed pieces */
11716 label1 = label(state);
11717 label2 = label(state);
11718 label3 = label(state);
11719 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011720 jmp1 = branch(state, label3, 0);
11721 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011722 }
11723 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011724 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011725 }
11726 end = label(state);
11727 /* Remember where break and continue go */
11728 start_scope(state);
11729 ident = state->i_break;
11730 symbol(state, ident, &ident->sym_ident, end, end->type);
11731 ident = state->i_continue;
11732 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11733 /* Now include the body */
11734 flatten(state, first, head);
11735 flatten(state, first, jmp1);
11736 flatten(state, first, label1);
11737 statement(state, first);
11738 flatten(state, first, label2);
11739 flatten(state, first, tail);
11740 flatten(state, first, label3);
11741 flatten(state, first, test);
11742 flatten(state, first, jmp2);
11743 flatten(state, first, end);
11744 /* Cleanup the break/continue scope */
11745 end_scope(state);
11746}
11747
11748static void while_statement(struct compile_state *state, struct triple *first)
11749{
11750 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11751 struct hash_entry *ident;
11752 eat(state, TOK_WHILE);
11753 eat(state, TOK_LPAREN);
11754 test = expr(state);
11755 bool(state, test);
11756 test = ltrue_expr(state, read_expr(state, test));
11757 eat(state, TOK_RPAREN);
11758 /* Generate the needed pieces */
11759 label1 = label(state);
11760 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011761 jmp1 = branch(state, label2, 0);
11762 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011763 end = label(state);
11764 /* Remember where break and continue go */
11765 start_scope(state);
11766 ident = state->i_break;
11767 symbol(state, ident, &ident->sym_ident, end, end->type);
11768 ident = state->i_continue;
11769 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11770 /* Thread them together */
11771 flatten(state, first, jmp1);
11772 flatten(state, first, label1);
11773 statement(state, first);
11774 flatten(state, first, label2);
11775 flatten(state, first, test);
11776 flatten(state, first, jmp2);
11777 flatten(state, first, end);
11778 /* Cleanup the break/continue scope */
11779 end_scope(state);
11780}
11781
11782static void do_statement(struct compile_state *state, struct triple *first)
11783{
11784 struct triple *label1, *label2, *test, *end;
11785 struct hash_entry *ident;
11786 eat(state, TOK_DO);
11787 /* Generate the needed pieces */
11788 label1 = label(state);
11789 label2 = label(state);
11790 end = label(state);
11791 /* Remember where break and continue go */
11792 start_scope(state);
11793 ident = state->i_break;
11794 symbol(state, ident, &ident->sym_ident, end, end->type);
11795 ident = state->i_continue;
11796 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11797 /* Now include the body */
11798 flatten(state, first, label1);
11799 statement(state, first);
11800 /* Cleanup the break/continue scope */
11801 end_scope(state);
11802 /* Eat the rest of the loop */
11803 eat(state, TOK_WHILE);
11804 eat(state, TOK_LPAREN);
11805 test = read_expr(state, expr(state));
11806 bool(state, test);
11807 eat(state, TOK_RPAREN);
11808 eat(state, TOK_SEMI);
11809 /* Thread the pieces together */
11810 test = ltrue_expr(state, test);
11811 flatten(state, first, label2);
11812 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011813 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011814 flatten(state, first, end);
11815}
11816
11817
11818static void return_statement(struct compile_state *state, struct triple *first)
11819{
11820 struct triple *jmp, *mv, *dest, *var, *val;
11821 int last;
11822 eat(state, TOK_RETURN);
11823
Stefan Reinauer50542a82007-10-24 11:14:14 +000011824#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011825#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011826#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011827 val = 0;
11828 /* If we have a return value do some more work */
11829 if (peek(state) != TOK_SEMI) {
11830 val = read_expr(state, expr(state));
11831 }
11832 eat(state, TOK_SEMI);
11833
11834 /* See if this last statement in a function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011835 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011836 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11837
11838 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011839 var = fresult(state, state->main_function);
11840
Eric Biedermanb138ac82003-04-22 18:44:01 +000011841 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011842 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011843 mv = jmp = 0;
11844 /* If needed generate a jump instruction */
11845 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011846 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011847 }
11848 /* If needed generate an assignment instruction */
11849 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011850 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011851 }
11852 /* Now put the code together */
11853 if (mv) {
11854 flatten(state, first, mv);
11855 flatten(state, first, jmp);
11856 }
11857 else if (jmp) {
11858 flatten(state, first, jmp);
11859 }
11860}
11861
11862static void break_statement(struct compile_state *state, struct triple *first)
11863{
11864 struct triple *dest;
11865 eat(state, TOK_BREAK);
11866 eat(state, TOK_SEMI);
11867 if (!state->i_break->sym_ident) {
11868 error(state, 0, "break statement not within loop or switch");
11869 }
11870 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011871 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011872}
11873
11874static void continue_statement(struct compile_state *state, struct triple *first)
11875{
11876 struct triple *dest;
11877 eat(state, TOK_CONTINUE);
11878 eat(state, TOK_SEMI);
11879 if (!state->i_continue->sym_ident) {
11880 error(state, 0, "continue statement outside of a loop");
11881 }
11882 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011883 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011884}
11885
11886static void goto_statement(struct compile_state *state, struct triple *first)
11887{
Eric Biederman153ea352003-06-20 14:43:20 +000011888 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011889 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011890 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011891 if (!ident->sym_label) {
11892 /* If this is a forward branch allocate the label now,
11893 * it will be flattend in the appropriate location later.
11894 */
11895 struct triple *ins;
11896 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011897 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011898 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011899 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011900
11901 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011902}
11903
11904static void labeled_statement(struct compile_state *state, struct triple *first)
11905{
Eric Biederman153ea352003-06-20 14:43:20 +000011906 struct triple *ins;
11907 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011908
Eric Biederman41203d92004-11-08 09:31:09 +000011909 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011910 if (ident->sym_label && ident->sym_label->def) {
11911 ins = ident->sym_label->def;
11912 put_occurance(ins->occurance);
11913 ins->occurance = new_occurance(state);
11914 }
11915 else {
11916 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011917 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011918 }
11919 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11920 error(state, 0, "label %s already defined", ident->name);
11921 }
11922 flatten(state, first, ins);
11923
Eric Biedermanb138ac82003-04-22 18:44:01 +000011924 eat(state, TOK_COLON);
11925 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011926}
11927
11928static void switch_statement(struct compile_state *state, struct triple *first)
11929{
Eric Biederman83b991a2003-10-11 06:20:25 +000011930 struct triple *value, *top, *end, *dbranch;
11931 struct hash_entry *ident;
11932
11933 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011934 eat(state, TOK_SWITCH);
11935 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011936 value = expr(state);
11937 integral(state, value);
11938 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011939 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011940 /* Generate the needed pieces */
11941 top = label(state);
11942 end = label(state);
11943 dbranch = branch(state, end, 0);
11944 /* Remember where case branches and break goes */
11945 start_scope(state);
11946 ident = state->i_switch;
11947 symbol(state, ident, &ident->sym_ident, value, value->type);
11948 ident = state->i_case;
11949 symbol(state, ident, &ident->sym_ident, top, top->type);
11950 ident = state->i_break;
11951 symbol(state, ident, &ident->sym_ident, end, end->type);
11952 ident = state->i_default;
11953 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11954 /* Thread them together */
11955 flatten(state, first, value);
11956 flatten(state, first, top);
11957 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011958 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011959 flatten(state, first, end);
11960 /* Cleanup the switch scope */
11961 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011962}
11963
11964static void case_statement(struct compile_state *state, struct triple *first)
11965{
Eric Biederman83b991a2003-10-11 06:20:25 +000011966 struct triple *cvalue, *dest, *test, *jmp;
11967 struct triple *ptr, *value, *top, *dbranch;
11968
11969 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011970 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011971 cvalue = constant_expr(state);
11972 integral(state, cvalue);
11973 if (cvalue->op != OP_INTCONST) {
11974 error(state, 0, "integer constant expected");
11975 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011976 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011977 if (!state->i_case->sym_ident) {
11978 error(state, 0, "case statement not within a switch");
11979 }
11980
11981 /* Lookup the interesting pieces */
11982 top = state->i_case->sym_ident->def;
11983 value = state->i_switch->sym_ident->def;
11984 dbranch = state->i_default->sym_ident->def;
11985
11986 /* See if this case label has already been used */
11987 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11988 if (ptr->op != OP_EQ) {
11989 continue;
11990 }
11991 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11992 error(state, 0, "duplicate case %d statement",
11993 cvalue->u.cval);
11994 }
11995 }
11996 /* Generate the needed pieces */
11997 dest = label(state);
11998 test = triple(state, OP_EQ, &int_type, value, cvalue);
11999 jmp = branch(state, dest, test);
12000 /* Thread the pieces together */
12001 flatten(state, dbranch, test);
12002 flatten(state, dbranch, jmp);
12003 flatten(state, dbranch, label(state));
12004 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012005 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012006}
12007
12008static void default_statement(struct compile_state *state, struct triple *first)
12009{
Eric Biederman83b991a2003-10-11 06:20:25 +000012010 struct triple *dest;
12011 struct triple *dbranch, *end;
12012
12013 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012014 eat(state, TOK_DEFAULT);
12015 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012016
12017 if (!state->i_case->sym_ident) {
12018 error(state, 0, "default statement not within a switch");
12019 }
12020
12021 /* Lookup the interesting pieces */
12022 dbranch = state->i_default->sym_ident->def;
12023 end = state->i_break->sym_ident->def;
12024
12025 /* See if a default statement has already happened */
12026 if (TARG(dbranch, 0) != end) {
12027 error(state, 0, "duplicate default statement");
12028 }
12029
12030 /* Generate the needed pieces */
12031 dest = label(state);
12032
Eric Biederman90089602004-05-28 14:11:54 +000012033 /* Blame the branch on the default statement */
12034 put_occurance(dbranch->occurance);
12035 dbranch->occurance = new_occurance(state);
12036
Eric Biederman83b991a2003-10-11 06:20:25 +000012037 /* Thread the pieces together */
12038 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012039 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012040 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012041 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012042}
12043
12044static void asm_statement(struct compile_state *state, struct triple *first)
12045{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012046 struct asm_info *info;
12047 struct {
12048 struct triple *constraint;
12049 struct triple *expr;
12050 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12051 struct triple *def, *asm_str;
12052 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012053 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012054
Eric Biederman90089602004-05-28 14:11:54 +000012055 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012056 eat(state, TOK_ASM);
12057 /* For now ignore the qualifiers */
12058 switch(peek(state)) {
12059 case TOK_CONST:
12060 eat(state, TOK_CONST);
12061 break;
12062 case TOK_VOLATILE:
12063 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012064 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012065 break;
12066 }
12067 eat(state, TOK_LPAREN);
12068 asm_str = string_constant(state);
12069
12070 colons = 0;
12071 out = in = clobbers = 0;
12072 /* Outputs */
12073 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12074 eat(state, TOK_COLON);
12075 colons++;
12076 more = (peek(state) == TOK_LIT_STRING);
12077 while(more) {
12078 struct triple *var;
12079 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012080 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012081 more = 0;
12082 if (out > MAX_LHS) {
12083 error(state, 0, "Maximum output count exceeded.");
12084 }
12085 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012086 str = constraint->u.blob;
12087 if (str[0] != '=') {
12088 error(state, 0, "Output constraint does not start with =");
12089 }
12090 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012091 eat(state, TOK_LPAREN);
12092 var = conditional_expr(state);
12093 eat(state, TOK_RPAREN);
12094
12095 lvalue(state, var);
12096 out_param[out].constraint = constraint;
12097 out_param[out].expr = var;
12098 if (peek(state) == TOK_COMMA) {
12099 eat(state, TOK_COMMA);
12100 more = 1;
12101 }
12102 out++;
12103 }
12104 }
12105 /* Inputs */
12106 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12107 eat(state, TOK_COLON);
12108 colons++;
12109 more = (peek(state) == TOK_LIT_STRING);
12110 while(more) {
12111 struct triple *val;
12112 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012113 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012114 more = 0;
12115 if (in > MAX_RHS) {
12116 error(state, 0, "Maximum input count exceeded.");
12117 }
12118 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012119 str = constraint->u.blob;
12120 if (digitp(str[0] && str[1] == '\0')) {
12121 int val;
12122 val = digval(str[0]);
12123 if ((val < 0) || (val >= out)) {
12124 error(state, 0, "Invalid input constraint %d", val);
12125 }
12126 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012127 eat(state, TOK_LPAREN);
12128 val = conditional_expr(state);
12129 eat(state, TOK_RPAREN);
12130
12131 in_param[in].constraint = constraint;
12132 in_param[in].expr = val;
12133 if (peek(state) == TOK_COMMA) {
12134 eat(state, TOK_COMMA);
12135 more = 1;
12136 }
12137 in++;
12138 }
12139 }
12140
12141 /* Clobber */
12142 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12143 eat(state, TOK_COLON);
12144 colons++;
12145 more = (peek(state) == TOK_LIT_STRING);
12146 while(more) {
12147 struct triple *clobber;
12148 more = 0;
12149 if ((clobbers + out) > MAX_LHS) {
12150 error(state, 0, "Maximum clobber limit exceeded.");
12151 }
12152 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012153
12154 clob_param[clobbers].constraint = clobber;
12155 if (peek(state) == TOK_COMMA) {
12156 eat(state, TOK_COMMA);
12157 more = 1;
12158 }
12159 clobbers++;
12160 }
12161 }
12162 eat(state, TOK_RPAREN);
12163 eat(state, TOK_SEMI);
12164
12165
12166 info = xcmalloc(sizeof(*info), "asm_info");
12167 info->str = asm_str->u.blob;
12168 free_triple(state, asm_str);
12169
12170 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12171 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012172 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012173
12174 /* Find the register constraints */
12175 for(i = 0; i < out; i++) {
12176 struct triple *constraint;
12177 constraint = out_param[i].constraint;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012178 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012179 out_param[i].expr->type, constraint->u.blob);
12180 free_triple(state, constraint);
12181 }
12182 for(; i - out < clobbers; i++) {
12183 struct triple *constraint;
12184 constraint = clob_param[i - out].constraint;
12185 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12186 free_triple(state, constraint);
12187 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012188 for(i = 0; i < in; i++) {
12189 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012190 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012191 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012192 str = constraint->u.blob;
12193 if (digitp(str[0]) && str[1] == '\0') {
12194 struct reg_info cinfo;
12195 int val;
12196 val = digval(str[0]);
12197 cinfo.reg = info->tmpl.lhs[val].reg;
12198 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12199 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12200 if (cinfo.reg == REG_UNSET) {
12201 cinfo.reg = REG_VIRT0 + val;
12202 }
12203 if (cinfo.regcm == 0) {
12204 error(state, 0, "No registers for %d", val);
12205 }
12206 info->tmpl.lhs[val] = cinfo;
12207 info->tmpl.rhs[i] = cinfo;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012208
Eric Biederman8d9c1232003-06-17 08:42:17 +000012209 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012210 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012211 in_param[i].expr->type, str);
12212 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012213 free_triple(state, constraint);
12214 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012215
12216 /* Now build the helper expressions */
12217 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012218 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012219 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012220 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012221 for(i = 0; i < (out + clobbers); i++) {
12222 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012223 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012224 if (i < out) {
12225 type = out_param[i].expr->type;
12226 } else {
12227 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12228 if (size >= SIZEOF_LONG) {
12229 type = &ulong_type;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012230 }
Eric Biederman90089602004-05-28 14:11:54 +000012231 else if (size >= SIZEOF_INT) {
12232 type = &uint_type;
12233 }
12234 else if (size >= SIZEOF_SHORT) {
12235 type = &ushort_type;
12236 }
12237 else {
12238 type = &uchar_type;
12239 }
12240 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012241 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012242 piece->u.cval = i;
12243 LHS(def, i) = piece;
12244 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012245 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012246 /* And write the helpers to their destinations */
12247 for(i = 0; i < out; i++) {
12248 struct triple *piece;
12249 piece = LHS(def, i);
12250 flatten(state, first,
12251 write_expr(state, out_param[i].expr, piece));
12252 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012253}
12254
12255
12256static int isdecl(int tok)
12257{
12258 switch(tok) {
12259 case TOK_AUTO:
12260 case TOK_REGISTER:
12261 case TOK_STATIC:
12262 case TOK_EXTERN:
12263 case TOK_TYPEDEF:
12264 case TOK_CONST:
12265 case TOK_RESTRICT:
12266 case TOK_VOLATILE:
12267 case TOK_VOID:
12268 case TOK_CHAR:
12269 case TOK_SHORT:
12270 case TOK_INT:
12271 case TOK_LONG:
12272 case TOK_FLOAT:
12273 case TOK_DOUBLE:
12274 case TOK_SIGNED:
12275 case TOK_UNSIGNED:
12276 case TOK_STRUCT:
12277 case TOK_UNION:
12278 case TOK_ENUM:
12279 case TOK_TYPE_NAME: /* typedef name */
12280 return 1;
12281 default:
12282 return 0;
12283 }
12284}
12285
12286static void compound_statement(struct compile_state *state, struct triple *first)
12287{
12288 eat(state, TOK_LBRACE);
12289 start_scope(state);
12290
12291 /* statement-list opt */
12292 while (peek(state) != TOK_RBRACE) {
12293 statement(state, first);
12294 }
12295 end_scope(state);
12296 eat(state, TOK_RBRACE);
12297}
12298
12299static void statement(struct compile_state *state, struct triple *first)
12300{
12301 int tok;
12302 tok = peek(state);
12303 if (tok == TOK_LBRACE) {
12304 compound_statement(state, first);
12305 }
12306 else if (tok == TOK_IF) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012307 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012308 }
12309 else if (tok == TOK_FOR) {
12310 for_statement(state, first);
12311 }
12312 else if (tok == TOK_WHILE) {
12313 while_statement(state, first);
12314 }
12315 else if (tok == TOK_DO) {
12316 do_statement(state, first);
12317 }
12318 else if (tok == TOK_RETURN) {
12319 return_statement(state, first);
12320 }
12321 else if (tok == TOK_BREAK) {
12322 break_statement(state, first);
12323 }
12324 else if (tok == TOK_CONTINUE) {
12325 continue_statement(state, first);
12326 }
12327 else if (tok == TOK_GOTO) {
12328 goto_statement(state, first);
12329 }
12330 else if (tok == TOK_SWITCH) {
12331 switch_statement(state, first);
12332 }
12333 else if (tok == TOK_ASM) {
12334 asm_statement(state, first);
12335 }
12336 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012337 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012338 }
12339 else if (tok == TOK_CASE) {
12340 case_statement(state, first);
12341 }
12342 else if (tok == TOK_DEFAULT) {
12343 default_statement(state, first);
12344 }
12345 else if (isdecl(tok)) {
12346 /* This handles C99 intermixing of statements and decls */
12347 decl(state, first);
12348 }
12349 else {
12350 expr_statement(state, first);
12351 }
12352}
12353
12354static struct type *param_decl(struct compile_state *state)
12355{
12356 struct type *type;
12357 struct hash_entry *ident;
12358 /* Cheat so the declarator will know we are not global */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012359 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012360 ident = 0;
12361 type = decl_specifiers(state);
12362 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012363 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012364 end_scope(state);
12365 return type;
12366}
12367
12368static struct type *param_type_list(struct compile_state *state, struct type *type)
12369{
12370 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012371 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012372 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012373 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012374 while(peek(state) == TOK_COMMA) {
12375 eat(state, TOK_COMMA);
12376 if (peek(state) == TOK_DOTS) {
12377 eat(state, TOK_DOTS);
12378 error(state, 0, "variadic functions not supported");
12379 }
12380 else {
12381 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12382 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012383 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012384 }
12385 }
12386 return ftype;
12387}
12388
Eric Biedermanb138ac82003-04-22 18:44:01 +000012389static struct type *type_name(struct compile_state *state)
12390{
12391 struct type *type;
12392 type = specifier_qualifier_list(state);
12393 /* abstract-declarator (may consume no tokens) */
12394 type = declarator(state, type, 0, 0);
12395 return type;
12396}
12397
12398static struct type *direct_declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012399 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012400 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012401{
Eric Biederman41203d92004-11-08 09:31:09 +000012402 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012403 struct type *outer;
12404 int op;
12405 outer = 0;
12406 arrays_complete(state, type);
12407 switch(peek(state)) {
12408 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012409 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012410 if (!ident) {
12411 error(state, 0, "Unexpected identifier found");
12412 }
12413 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012414 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012415 break;
12416 case TOK_LPAREN:
12417 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012418 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012419 eat(state, TOK_RPAREN);
12420 break;
12421 default:
12422 if (need_ident) {
12423 error(state, 0, "Identifier expected");
12424 }
12425 break;
12426 }
12427 do {
12428 op = 1;
12429 arrays_complete(state, type);
12430 switch(peek(state)) {
12431 case TOK_LPAREN:
12432 eat(state, TOK_LPAREN);
12433 type = param_type_list(state, type);
12434 eat(state, TOK_RPAREN);
12435 break;
12436 case TOK_LBRACKET:
12437 {
12438 unsigned int qualifiers;
12439 struct triple *value;
12440 value = 0;
12441 eat(state, TOK_LBRACKET);
12442 if (peek(state) != TOK_RBRACKET) {
12443 value = constant_expr(state);
12444 integral(state, value);
12445 }
12446 eat(state, TOK_RBRACKET);
12447
12448 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12449 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12450 if (value) {
12451 type->elements = value->u.cval;
12452 free_triple(state, value);
12453 } else {
12454 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12455 op = 0;
12456 }
12457 }
12458 break;
12459 default:
12460 op = 0;
12461 break;
12462 }
12463 } while(op);
12464 if (outer) {
12465 struct type *inner;
12466 arrays_complete(state, type);
12467 FINISHME();
12468 for(inner = outer; inner->left; inner = inner->left)
12469 ;
12470 inner->left = type;
12471 type = outer;
12472 }
12473 return type;
12474}
12475
12476static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012477 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012478 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012479{
12480 while(peek(state) == TOK_STAR) {
12481 eat(state, TOK_STAR);
12482 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12483 }
Eric Biederman41203d92004-11-08 09:31:09 +000012484 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012485 return type;
12486}
12487
Eric Biedermanb138ac82003-04-22 18:44:01 +000012488static struct type *typedef_name(
12489 struct compile_state *state, unsigned int specifiers)
12490{
12491 struct hash_entry *ident;
12492 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012493 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012494 type = ident->sym_ident->type;
12495 specifiers |= type->type & QUAL_MASK;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012496 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012497 (type->type & (STOR_MASK | QUAL_MASK))) {
12498 type = clone_type(specifiers, type);
12499 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012500 return type;
12501}
12502
12503static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012504 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012505{
Eric Biederman83b991a2003-10-11 06:20:25 +000012506 struct hash_entry *ident;
12507 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012508 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012509 struct type *enum_type;
12510 enum_type = 0;
12511 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012512 eat(state, TOK_ENUM);
12513 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012514 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012515 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012516 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012517 base = 0;
12518 if (!ident || (peek(state) == TOK_LBRACE)) {
12519 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012520 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012521 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12522 enum_type->type_ident = ident;
12523 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012524 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012525 struct hash_entry *eident;
12526 struct triple *value;
12527 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012528 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012529 if (eident->sym_ident) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012530 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012531 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012532 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012533 eident->tok = TOK_ENUM_CONST;
12534 if (peek(state) == TOK_EQ) {
12535 struct triple *val;
12536 eat(state, TOK_EQ);
12537 val = constant_expr(state);
12538 integral(state, val);
12539 base = val->u.cval;
12540 }
12541 value = int_const(state, &int_type, base);
12542 symbol(state, eident, &eident->sym_ident, value, &int_type);
12543 entry = new_type(TYPE_LIST, 0, 0);
12544 entry->field_ident = eident;
12545 *next = entry;
12546 next = &entry->right;
12547 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012548 if (peek(state) == TOK_COMMA) {
12549 eat(state, TOK_COMMA);
12550 }
12551 } while(peek(state) != TOK_RBRACE);
12552 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012553 if (ident) {
12554 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12555 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012556 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012557 if (ident && ident->sym_tag &&
12558 ident->sym_tag->type &&
12559 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12560 enum_type = clone_type(spec, ident->sym_tag->type);
12561 }
12562 else if (ident && !enum_type) {
12563 error(state, 0, "enum %s undeclared", ident->name);
12564 }
12565 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012566}
12567
Eric Biedermanb138ac82003-04-22 18:44:01 +000012568static struct type *struct_declarator(
12569 struct compile_state *state, struct type *type, struct hash_entry **ident)
12570{
Eric Biederman90089602004-05-28 14:11:54 +000012571 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012572 type = declarator(state, type, ident, 1);
12573 }
Eric Biederman90089602004-05-28 14:11:54 +000012574 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012575 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012576 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012577 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012578 if (value->op != OP_INTCONST) {
12579 error(state, 0, "Invalid constant expression");
12580 }
12581 if (value->u.cval > size_of(state, type)) {
12582 error(state, 0, "bitfield larger than base type");
12583 }
12584 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12585 error(state, 0, "bitfield base not an integer type");
12586 }
12587 type = new_type(TYPE_BITFIELD, type, 0);
12588 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012589 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012590 return type;
12591}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012592
12593static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012594 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012595{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012596 struct type *struct_type;
12597 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012598 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012599 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012600 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012601 struct_type = 0;
12602 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012603 switch(peek(state)) {
12604 case TOK_STRUCT:
12605 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012606 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012607 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012608 break;
12609 case TOK_UNION:
12610 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012611 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012612 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012613 break;
12614 default:
12615 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012616 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012617 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012618 break;
12619 }
12620 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012621 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012622 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012623 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012624 if (!ident || (peek(state) == TOK_LBRACE)) {
12625 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012626 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012627 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012628 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012629 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012630 do {
12631 struct type *base_type;
12632 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012633 base_type = specifier_qualifier_list(state);
12634 do {
12635 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012636 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012637 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012638 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012639 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012640 if (peek(state) == TOK_COMMA) {
12641 done = 0;
12642 eat(state, TOK_COMMA);
12643 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012644 type = clone_type(0, type);
12645 type->field_ident = fident;
12646 if (*next) {
12647 *next = new_type(type_join, *next, type);
12648 next = &((*next)->right);
12649 } else {
12650 *next = type;
12651 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012652 } while(!done);
12653 eat(state, TOK_SEMI);
12654 } while(peek(state) != TOK_RBRACE);
12655 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012656 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012657 struct_type->type_ident = ident;
12658 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012659 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012660 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012661 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012662 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000012663 if (ident && ident->sym_tag &&
12664 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012665 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012666 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012667 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012668 else if (ident && !struct_type) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012669 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012670 (type_main == TYPE_STRUCT)?"struct" : "union",
12671 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012672 }
12673 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012674}
12675
12676static unsigned int storage_class_specifier_opt(struct compile_state *state)
12677{
12678 unsigned int specifiers;
12679 switch(peek(state)) {
12680 case TOK_AUTO:
12681 eat(state, TOK_AUTO);
12682 specifiers = STOR_AUTO;
12683 break;
12684 case TOK_REGISTER:
12685 eat(state, TOK_REGISTER);
12686 specifiers = STOR_REGISTER;
12687 break;
12688 case TOK_STATIC:
12689 eat(state, TOK_STATIC);
12690 specifiers = STOR_STATIC;
12691 break;
12692 case TOK_EXTERN:
12693 eat(state, TOK_EXTERN);
12694 specifiers = STOR_EXTERN;
12695 break;
12696 case TOK_TYPEDEF:
12697 eat(state, TOK_TYPEDEF);
12698 specifiers = STOR_TYPEDEF;
12699 break;
12700 default:
12701 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012702 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012703 }
12704 else {
12705 specifiers = STOR_AUTO;
12706 }
12707 }
12708 return specifiers;
12709}
12710
12711static unsigned int function_specifier_opt(struct compile_state *state)
12712{
12713 /* Ignore the inline keyword */
12714 unsigned int specifiers;
12715 specifiers = 0;
12716 switch(peek(state)) {
12717 case TOK_INLINE:
12718 eat(state, TOK_INLINE);
12719 specifiers = STOR_INLINE;
12720 }
12721 return specifiers;
12722}
12723
Eric Biederman90089602004-05-28 14:11:54 +000012724static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12725{
12726 int tok = peek(state);
12727 switch(tok) {
12728 case TOK_COMMA:
12729 case TOK_LPAREN:
12730 /* The empty attribute ignore it */
12731 break;
12732 case TOK_IDENT:
12733 case TOK_ENUM_CONST:
12734 case TOK_TYPE_NAME:
12735 {
12736 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012737 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012738
12739 if (ident == state->i_noinline) {
12740 if (attributes & ATTRIB_ALWAYS_INLINE) {
12741 error(state, 0, "both always_inline and noinline attribtes");
12742 }
12743 attributes |= ATTRIB_NOINLINE;
12744 }
12745 else if (ident == state->i_always_inline) {
12746 if (attributes & ATTRIB_NOINLINE) {
12747 error(state, 0, "both noinline and always_inline attribtes");
12748 }
12749 attributes |= ATTRIB_ALWAYS_INLINE;
12750 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012751 else if (ident == state->i_noreturn) {
12752 // attribute((noreturn)) does nothing (yet?)
12753 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012754 else if (ident == state->i_unused) {
12755 // attribute((unused)) does nothing (yet?)
12756 }
Stefan Reinauerec664bc2013-05-09 14:06:04 -070012757 else if (ident == state->i_packed) {
12758 // attribute((packed)) does nothing (yet?)
12759 }
Eric Biederman90089602004-05-28 14:11:54 +000012760 else {
12761 error(state, 0, "Unknown attribute:%s", ident->name);
12762 }
12763 break;
12764 }
12765 default:
12766 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12767 break;
12768 }
12769 return attributes;
12770}
12771
12772static unsigned int attribute_list(struct compile_state *state, unsigned type)
12773{
12774 type = attrib(state, type);
12775 while(peek(state) == TOK_COMMA) {
12776 eat(state, TOK_COMMA);
12777 type = attrib(state, type);
12778 }
12779 return type;
12780}
12781
12782static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12783{
12784 if (peek(state) == TOK_ATTRIBUTE) {
12785 eat(state, TOK_ATTRIBUTE);
12786 eat(state, TOK_LPAREN);
12787 eat(state, TOK_LPAREN);
12788 type = attribute_list(state, type);
12789 eat(state, TOK_RPAREN);
12790 eat(state, TOK_RPAREN);
12791 }
12792 return type;
12793}
12794
Eric Biedermanb138ac82003-04-22 18:44:01 +000012795static unsigned int type_qualifiers(struct compile_state *state)
12796{
12797 unsigned int specifiers;
12798 int done;
12799 done = 0;
12800 specifiers = QUAL_NONE;
12801 do {
12802 switch(peek(state)) {
12803 case TOK_CONST:
12804 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012805 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012806 break;
12807 case TOK_VOLATILE:
12808 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012809 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012810 break;
12811 case TOK_RESTRICT:
12812 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012813 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012814 break;
12815 default:
12816 done = 1;
12817 break;
12818 }
12819 } while(!done);
12820 return specifiers;
12821}
12822
12823static struct type *type_specifier(
12824 struct compile_state *state, unsigned int spec)
12825{
12826 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012827 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012828 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012829 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012830 case TOK_VOID:
12831 eat(state, TOK_VOID);
12832 type = new_type(TYPE_VOID | spec, 0, 0);
12833 break;
12834 case TOK_CHAR:
12835 eat(state, TOK_CHAR);
12836 type = new_type(TYPE_CHAR | spec, 0, 0);
12837 break;
12838 case TOK_SHORT:
12839 eat(state, TOK_SHORT);
12840 if (peek(state) == TOK_INT) {
12841 eat(state, TOK_INT);
12842 }
12843 type = new_type(TYPE_SHORT | spec, 0, 0);
12844 break;
12845 case TOK_INT:
12846 eat(state, TOK_INT);
12847 type = new_type(TYPE_INT | spec, 0, 0);
12848 break;
12849 case TOK_LONG:
12850 eat(state, TOK_LONG);
12851 switch(peek(state)) {
12852 case TOK_LONG:
12853 eat(state, TOK_LONG);
12854 error(state, 0, "long long not supported");
12855 break;
12856 case TOK_DOUBLE:
12857 eat(state, TOK_DOUBLE);
12858 error(state, 0, "long double not supported");
12859 break;
12860 case TOK_INT:
12861 eat(state, TOK_INT);
12862 type = new_type(TYPE_LONG | spec, 0, 0);
12863 break;
12864 default:
12865 type = new_type(TYPE_LONG | spec, 0, 0);
12866 break;
12867 }
12868 break;
12869 case TOK_FLOAT:
12870 eat(state, TOK_FLOAT);
12871 error(state, 0, "type float not supported");
12872 break;
12873 case TOK_DOUBLE:
12874 eat(state, TOK_DOUBLE);
12875 error(state, 0, "type double not supported");
12876 break;
12877 case TOK_SIGNED:
12878 eat(state, TOK_SIGNED);
12879 switch(peek(state)) {
12880 case TOK_LONG:
12881 eat(state, TOK_LONG);
12882 switch(peek(state)) {
12883 case TOK_LONG:
12884 eat(state, TOK_LONG);
12885 error(state, 0, "type long long not supported");
12886 break;
12887 case TOK_INT:
12888 eat(state, TOK_INT);
12889 type = new_type(TYPE_LONG | spec, 0, 0);
12890 break;
12891 default:
12892 type = new_type(TYPE_LONG | spec, 0, 0);
12893 break;
12894 }
12895 break;
12896 case TOK_INT:
12897 eat(state, TOK_INT);
12898 type = new_type(TYPE_INT | spec, 0, 0);
12899 break;
12900 case TOK_SHORT:
12901 eat(state, TOK_SHORT);
12902 type = new_type(TYPE_SHORT | spec, 0, 0);
12903 break;
12904 case TOK_CHAR:
12905 eat(state, TOK_CHAR);
12906 type = new_type(TYPE_CHAR | spec, 0, 0);
12907 break;
12908 default:
12909 type = new_type(TYPE_INT | spec, 0, 0);
12910 break;
12911 }
12912 break;
12913 case TOK_UNSIGNED:
12914 eat(state, TOK_UNSIGNED);
12915 switch(peek(state)) {
12916 case TOK_LONG:
12917 eat(state, TOK_LONG);
12918 switch(peek(state)) {
12919 case TOK_LONG:
12920 eat(state, TOK_LONG);
12921 error(state, 0, "unsigned long long not supported");
12922 break;
12923 case TOK_INT:
12924 eat(state, TOK_INT);
12925 type = new_type(TYPE_ULONG | spec, 0, 0);
12926 break;
12927 default:
12928 type = new_type(TYPE_ULONG | spec, 0, 0);
12929 break;
12930 }
12931 break;
12932 case TOK_INT:
12933 eat(state, TOK_INT);
12934 type = new_type(TYPE_UINT | spec, 0, 0);
12935 break;
12936 case TOK_SHORT:
12937 eat(state, TOK_SHORT);
12938 type = new_type(TYPE_USHORT | spec, 0, 0);
12939 break;
12940 case TOK_CHAR:
12941 eat(state, TOK_CHAR);
12942 type = new_type(TYPE_UCHAR | spec, 0, 0);
12943 break;
12944 default:
12945 type = new_type(TYPE_UINT | spec, 0, 0);
12946 break;
12947 }
12948 break;
12949 /* struct or union specifier */
12950 case TOK_STRUCT:
12951 case TOK_UNION:
12952 type = struct_or_union_specifier(state, spec);
12953 break;
12954 /* enum-spefifier */
12955 case TOK_ENUM:
12956 type = enum_specifier(state, spec);
12957 break;
12958 /* typedef name */
12959 case TOK_TYPE_NAME:
12960 type = typedef_name(state, spec);
12961 break;
12962 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012963 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012964 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012965 break;
12966 }
12967 return type;
12968}
12969
12970static int istype(int tok)
12971{
12972 switch(tok) {
12973 case TOK_CONST:
12974 case TOK_RESTRICT:
12975 case TOK_VOLATILE:
12976 case TOK_VOID:
12977 case TOK_CHAR:
12978 case TOK_SHORT:
12979 case TOK_INT:
12980 case TOK_LONG:
12981 case TOK_FLOAT:
12982 case TOK_DOUBLE:
12983 case TOK_SIGNED:
12984 case TOK_UNSIGNED:
12985 case TOK_STRUCT:
12986 case TOK_UNION:
12987 case TOK_ENUM:
12988 case TOK_TYPE_NAME:
12989 return 1;
12990 default:
12991 return 0;
12992 }
12993}
12994
12995
12996static struct type *specifier_qualifier_list(struct compile_state *state)
12997{
12998 struct type *type;
12999 unsigned int specifiers = 0;
13000
13001 /* type qualifiers */
13002 specifiers |= type_qualifiers(state);
13003
13004 /* type specifier */
13005 type = type_specifier(state, specifiers);
13006
13007 return type;
13008}
13009
Stefan Reinauer50542a82007-10-24 11:14:14 +000013010#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013011static int isdecl_specifier(int tok)
13012{
13013 switch(tok) {
13014 /* storage class specifier */
13015 case TOK_AUTO:
13016 case TOK_REGISTER:
13017 case TOK_STATIC:
13018 case TOK_EXTERN:
13019 case TOK_TYPEDEF:
13020 /* type qualifier */
13021 case TOK_CONST:
13022 case TOK_RESTRICT:
13023 case TOK_VOLATILE:
13024 /* type specifiers */
13025 case TOK_VOID:
13026 case TOK_CHAR:
13027 case TOK_SHORT:
13028 case TOK_INT:
13029 case TOK_LONG:
13030 case TOK_FLOAT:
13031 case TOK_DOUBLE:
13032 case TOK_SIGNED:
13033 case TOK_UNSIGNED:
13034 /* struct or union specifier */
13035 case TOK_STRUCT:
13036 case TOK_UNION:
13037 /* enum-spefifier */
13038 case TOK_ENUM:
13039 /* typedef name */
13040 case TOK_TYPE_NAME:
13041 /* function specifiers */
13042 case TOK_INLINE:
13043 return 1;
13044 default:
13045 return 0;
13046 }
13047}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013048#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013049
13050static struct type *decl_specifiers(struct compile_state *state)
13051{
13052 struct type *type;
13053 unsigned int specifiers;
13054 /* I am overly restrictive in the arragement of specifiers supported.
13055 * C is overly flexible in this department it makes interpreting
13056 * the parse tree difficult.
13057 */
13058 specifiers = 0;
13059
13060 /* storage class specifier */
13061 specifiers |= storage_class_specifier_opt(state);
13062
13063 /* function-specifier */
13064 specifiers |= function_specifier_opt(state);
13065
Eric Biederman90089602004-05-28 14:11:54 +000013066 /* attributes */
13067 specifiers |= attributes_opt(state, 0);
13068
Eric Biedermanb138ac82003-04-22 18:44:01 +000013069 /* type qualifier */
13070 specifiers |= type_qualifiers(state);
13071
13072 /* type specifier */
13073 type = type_specifier(state, specifiers);
13074 return type;
13075}
13076
Eric Biederman00443072003-06-24 12:34:45 +000013077struct field_info {
13078 struct type *type;
13079 size_t offset;
13080};
13081
13082static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013083{
13084 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013085 struct field_info info;
13086 info.offset = ~0U;
13087 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013088 do {
13089 switch(peek(state)) {
13090 case TOK_LBRACKET:
13091 {
13092 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013093 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13094 error(state, 0, "Array designator not in array initializer");
13095 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013096 eat(state, TOK_LBRACKET);
13097 value = constant_expr(state);
13098 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013099
13100 info.type = type->left;
13101 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013102 break;
13103 }
13104 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013105 {
13106 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013107 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13108 ((type->type & TYPE_MASK) != TYPE_UNION))
13109 {
Eric Biederman00443072003-06-24 12:34:45 +000013110 error(state, 0, "Struct designator not in struct initializer");
13111 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013112 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013113 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013114 info.offset = field_offset(state, type, field);
13115 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013116 break;
Eric Biederman00443072003-06-24 12:34:45 +000013117 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013118 default:
13119 error(state, 0, "Invalid designator");
13120 }
13121 tok = peek(state);
13122 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13123 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013124 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013125}
13126
13127static struct triple *initializer(
13128 struct compile_state *state, struct type *type)
13129{
13130 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013131#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013132#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013133#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013134 if (peek(state) != TOK_LBRACE) {
13135 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013136 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13137 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13138 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13139 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13140 (equiv_types(type->left, result->type->left))) {
13141 type->elements = result->type->elements;
13142 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013143 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013144 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13145 (type->type & TYPE_MASK) != TYPE_ARRAY)
13146 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013147 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013148 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013149 if (!is_init_compatible(state, type, result->type)) {
13150 error(state, 0, "Incompatible types in initializer");
13151 }
13152 if (!equiv_types(type, result->type)) {
13153 result = mk_cast_expr(state, type, result);
13154 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013155 }
13156 else {
13157 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013158 size_t max_offset;
13159 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013160 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013161 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13162 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13163 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013164 }
Eric Biederman00443072003-06-24 12:34:45 +000013165 info.offset = 0;
13166 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013167 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13168 info.type = next_field(state, type, 0);
13169 }
Eric Biederman00443072003-06-24 12:34:45 +000013170 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13171 max_offset = 0;
13172 } else {
13173 max_offset = size_of(state, type);
13174 }
Eric Biederman90089602004-05-28 14:11:54 +000013175 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013176 eat(state, TOK_LBRACE);
13177 do {
13178 struct triple *value;
13179 struct type *value_type;
13180 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013181 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013182 int tok;
13183 comma = 0;
13184 tok = peek(state);
13185 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013186 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013187 }
Eric Biederman00443072003-06-24 12:34:45 +000013188 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13189 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013190 error(state, 0, "element beyond bounds");
13191 }
Eric Biederman00443072003-06-24 12:34:45 +000013192 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013193 value = eval_const_expr(state, initializer(state, value_type));
13194 value_size = size_of(state, value_type);
13195 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013196 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13197 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013198 void *old_buf;
13199 size_t old_size;
13200 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013201 old_size = max_offset;
13202 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013203 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13204 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013205 xfree(old_buf);
13206 }
Eric Biederman90089602004-05-28 14:11:54 +000013207 dest = ((char *)buf) + bits_to_bytes(info.offset);
13208#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013209 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013210 dest - buf,
13211 bits_to_bytes(max_offset),
13212 bits_to_bytes(value_size),
13213 value->op);
13214#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013215 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013216 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013217 }
Eric Biederman90089602004-05-28 14:11:54 +000013218 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13219#if DEBUG_INITIALIZER
13220 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13221#endif
Eric Biederman00443072003-06-24 12:34:45 +000013222 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013223 }
Eric Biederman90089602004-05-28 14:11:54 +000013224 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013225 *((uint16_t *)dest) = value->u.cval & 0xffff;
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_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013228 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013229 }
13230 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013231 internal_error(state, 0, "unhandled constant initializer");
13232 }
Eric Biederman00443072003-06-24 12:34:45 +000013233 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013234 if (peek(state) == TOK_COMMA) {
13235 eat(state, TOK_COMMA);
13236 comma = 1;
13237 }
Eric Biederman00443072003-06-24 12:34:45 +000013238 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013239 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13240 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013241 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013242 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013243 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013244 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013245 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13246 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13247 type->elements = max_offset / size_of(state, type->left);
13248 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013249 eat(state, TOK_RBRACE);
13250 result = triple(state, OP_BLOBCONST, type, 0, 0);
13251 result->u.blob = buf;
13252 }
13253 return result;
13254}
13255
Eric Biederman90089602004-05-28 14:11:54 +000013256static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013257{
13258 /* Make a second pass and finish anything outstanding
13259 * with respect to branches. The only outstanding item
13260 * is to see if there are goto to labels that have not
13261 * been defined and to error about them.
13262 */
13263 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013264 struct triple *ins;
13265 /* Also error on branches that do not use their targets */
13266 ins = first;
13267 do {
13268 if (!triple_is_ret(state, ins)) {
13269 struct triple **expr ;
13270 struct triple_set *set;
13271 expr = triple_targ(state, ins, 0);
13272 for(; expr; expr = triple_targ(state, ins, expr)) {
13273 struct triple *targ;
13274 targ = *expr;
13275 for(set = targ?targ->use:0; set; set = set->next) {
13276 if (set->member == ins) {
13277 break;
13278 }
13279 }
13280 if (!set) {
13281 internal_error(state, ins, "targ not used");
13282 }
13283 }
13284 }
13285 ins = ins->next;
13286 } while(ins != first);
13287 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013288 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13289 struct hash_entry *entry;
13290 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13291 struct triple *ins;
13292 if (!entry->sym_label) {
13293 continue;
13294 }
13295 ins = entry->sym_label->def;
13296 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13297 error(state, ins, "label `%s' used but not defined",
13298 entry->name);
13299 }
13300 }
13301 }
13302}
13303
Eric Biedermanb138ac82003-04-22 18:44:01 +000013304static struct triple *function_definition(
13305 struct compile_state *state, struct type *type)
13306{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013307 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013308 struct triple *fname;
13309 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013310 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013311 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013312 int i;
13313 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13314 error(state, 0, "Invalid function header");
13315 }
13316
13317 /* Verify the function type */
13318 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13319 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013320 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013321 error(state, 0, "Invalid function parameters");
13322 }
13323 param = type->right;
13324 i = 0;
13325 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13326 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013327 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013328 error(state, 0, "No identifier for parameter %d\n", i);
13329 }
13330 param = param->right;
13331 }
13332 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013333 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013334 error(state, 0, "No identifier for paramter %d\n", i);
13335 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013336
Eric Biedermanb138ac82003-04-22 18:44:01 +000013337 /* Get a list of statements for this function. */
13338 def = triple(state, OP_LIST, type, 0, 0);
13339
13340 /* Start a new scope for the passed parameters */
13341 start_scope(state);
13342
13343 /* Put a label at the very start of a function */
13344 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013345 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013346
13347 /* Put a label at the very end of a function */
13348 end = label(state);
13349 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013350 /* Remember where return goes */
13351 ident = state->i_return;
13352 symbol(state, ident, &ident->sym_ident, end, end->type);
13353
Eric Biederman90089602004-05-28 14:11:54 +000013354 /* Get the initial closure type */
13355 ctype = new_type(TYPE_JOIN, &void_type, 0);
13356 ctype->elements = 1;
13357
13358 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013359 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013360 /* Remove all type qualifiers from the return type */
13361 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13362 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013363 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013364
Eric Biederman5ade04a2003-10-22 04:03:46 +000013365 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013366 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013367
13368 /* Add in the return instruction */
13369 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13370 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013371
13372 /* Walk through the parameters and create symbol table entries
13373 * for them.
13374 */
13375 param = type->right;
13376 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013377 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013378 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013379 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013380 flatten(state, end, tmp);
13381 param = param->right;
13382 }
13383 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13384 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013385 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013386 tmp = variable(state, param);
13387 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13388 flatten(state, end, tmp);
13389 }
Eric Biederman90089602004-05-28 14:11:54 +000013390
13391 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013392 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013393 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13394 fname_type->type |= QUAL_CONST | STOR_STATIC;
13395 fname_type->elements = strlen(state->function) + 1;
13396
13397 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13398 fname->u.blob = (void *)state->function;
13399 fname = flatten(state, end, fname);
13400
13401 ident = state->i___func__;
13402 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013403
13404 /* Remember which function I am compiling.
13405 * Also assume the last defined function is the main function.
13406 */
13407 state->main_function = def;
13408
13409 /* Now get the actual function definition */
13410 compound_statement(state, end);
13411
Eric Biederman153ea352003-06-20 14:43:20 +000013412 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013413 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013414
Eric Biedermanb138ac82003-04-22 18:44:01 +000013415 /* Remove the parameter scope */
13416 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013417
Eric Biederman5ade04a2003-10-22 04:03:46 +000013418
13419 /* Remember I have defined a function */
13420 if (!state->functions) {
13421 state->functions = def;
13422 } else {
13423 insert_triple(state, state->functions, def);
13424 }
13425 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013426 FILE *fp = state->dbgout;
13427 fprintf(fp, "\n");
13428 loc(fp, state, 0);
13429 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13430 display_func(state, fp, def);
13431 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013432 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013433
13434 return def;
13435}
13436
Stefan Reinauer14e22772010-04-27 06:56:47 +000013437static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013438 struct type *type, struct hash_entry *ident)
13439{
13440 struct triple *def;
13441 def = 0;
13442 /* Clean up the storage types used */
13443 switch (type->type & STOR_MASK) {
13444 case STOR_AUTO:
13445 case STOR_STATIC:
13446 /* These are the good types I am aiming for */
13447 break;
13448 case STOR_REGISTER:
13449 type->type &= ~STOR_MASK;
13450 type->type |= STOR_AUTO;
13451 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013452 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013453 case STOR_EXTERN:
13454 type->type &= ~STOR_MASK;
13455 type->type |= STOR_STATIC;
13456 break;
13457 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013458 if (!ident) {
13459 error(state, 0, "typedef without name");
13460 }
13461 symbol(state, ident, &ident->sym_ident, 0, type);
13462 ident->tok = TOK_TYPE_NAME;
13463 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013464 break;
13465 default:
13466 internal_error(state, 0, "Undefined storage class");
13467 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013468 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
Patrick Georgi11a7db32013-02-16 20:16:34 +010013469 // ignore function prototypes
13470 return def;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013471 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013472 if (ident &&
13473 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13474 ((type->type & STOR_MASK) != STOR_STATIC))
13475 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013476 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013477 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013478 ((type->type & QUAL_CONST) == 0)) {
13479 error(state, 0, "non const static variables not supported");
13480 }
13481 if (ident) {
13482 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013483 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013484 }
13485 return def;
13486}
13487
13488static void decl(struct compile_state *state, struct triple *first)
13489{
13490 struct type *base_type, *type;
13491 struct hash_entry *ident;
13492 struct triple *def;
13493 int global;
13494 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13495 base_type = decl_specifiers(state);
13496 ident = 0;
13497 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013498 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013499 if (global && ident && (peek(state) == TOK_LBRACE)) {
13500 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013501 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013502 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013503 def = function_definition(state, type);
13504 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013505 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013506 }
13507 else {
13508 int done;
13509 flatten(state, first, do_decl(state, type, ident));
13510 /* type or variable definition */
13511 do {
13512 done = 1;
13513 if (peek(state) == TOK_EQ) {
13514 if (!ident) {
13515 error(state, 0, "cannot assign to a type");
13516 }
13517 eat(state, TOK_EQ);
13518 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013519 init_expr(state,
13520 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013521 initializer(state, type)));
13522 }
13523 arrays_complete(state, type);
13524 if (peek(state) == TOK_COMMA) {
13525 eat(state, TOK_COMMA);
13526 ident = 0;
13527 type = declarator(state, base_type, &ident, 0);
13528 flatten(state, first, do_decl(state, type, ident));
13529 done = 0;
13530 }
13531 } while(!done);
13532 eat(state, TOK_SEMI);
13533 }
13534}
13535
13536static void decls(struct compile_state *state)
13537{
13538 struct triple *list;
13539 int tok;
13540 list = label(state);
13541 while(1) {
13542 tok = peek(state);
13543 if (tok == TOK_EOF) {
13544 return;
13545 }
13546 if (tok == TOK_SPACE) {
13547 eat(state, TOK_SPACE);
13548 }
13549 decl(state, list);
13550 if (list->next != list) {
13551 error(state, 0, "global variables not supported");
13552 }
13553 }
13554}
13555
Stefan Reinauer14e22772010-04-27 06:56:47 +000013556/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013557 * Function inlining
13558 */
Eric Biederman90089602004-05-28 14:11:54 +000013559struct triple_reg_set {
13560 struct triple_reg_set *next;
13561 struct triple *member;
13562 struct triple *new;
13563};
13564struct reg_block {
13565 struct block *block;
13566 struct triple_reg_set *in;
13567 struct triple_reg_set *out;
13568 int vertex;
13569};
13570static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13571static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13572static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13573static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13574static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13575 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13576 void *arg);
13577static void print_block(
13578 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013579static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013580 struct triple *member, struct triple *new_member);
13581static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13582static struct reg_block *compute_variable_lifetimes(
13583 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013584static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013585 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013586#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013587static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013588 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013589#endif
Eric Biederman90089602004-05-28 14:11:54 +000013590
Eric Biederman5ade04a2003-10-22 04:03:46 +000013591
13592static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013593 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013594 struct triple *targ, struct triple *ret)
13595{
13596 struct triple *call;
13597
13598 if (!retvar || !is_lvalue(state, retvar)) {
13599 internal_error(state, 0, "writing to a non lvalue?");
13600 }
13601 write_compatible(state, retvar->type, &void_ptr_type);
13602
13603 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13604 TARG(call, 0) = targ;
13605 MISC(call, 0) = ret;
13606 if (!targ || (targ->op != OP_LABEL)) {
13607 internal_error(state, 0, "call not to a label");
13608 }
13609 if (!ret || (ret->op != OP_RET)) {
13610 internal_error(state, 0, "call not matched with return");
13611 }
13612 return call;
13613}
13614
Eric Biederman5ade04a2003-10-22 04:03:46 +000013615static void walk_functions(struct compile_state *state,
13616 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13617 void *arg)
13618{
13619 struct triple *func, *first;
13620 func = first = state->functions;
13621 do {
13622 cb(state, func, arg);
13623 func = func->next;
13624 } while(func != first);
13625}
13626
Eric Biederman90089602004-05-28 14:11:54 +000013627static void reverse_walk_functions(struct compile_state *state,
13628 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13629 void *arg)
13630{
13631 struct triple *func, *first;
13632 func = first = state->functions;
13633 do {
13634 func = func->prev;
13635 cb(state, func, arg);
13636 } while(func != first);
13637}
13638
13639
13640static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13641{
13642 struct triple *ptr, *first;
13643 if (func->u.cval == 0) {
13644 return;
13645 }
13646 ptr = first = RHS(func, 0);
13647 do {
13648 if (ptr->op == OP_FCALL) {
13649 struct triple *called_func;
13650 called_func = MISC(ptr, 0);
13651 /* Mark the called function as used */
13652 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13653 called_func->u.cval++;
13654 }
13655 /* Remove the called function from the list */
13656 called_func->prev->next = called_func->next;
13657 called_func->next->prev = called_func->prev;
13658
13659 /* Place the called function before me on the list */
13660 called_func->next = func;
13661 called_func->prev = func->prev;
13662 called_func->prev->next = called_func;
13663 called_func->next->prev = called_func;
13664 }
13665 ptr = ptr->next;
13666 } while(ptr != first);
13667 func->id |= TRIPLE_FLAG_FLATTENED;
13668}
13669
13670static void mark_live_functions(struct compile_state *state)
13671{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013672 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013673 * the list of functions.
13674 */
13675 if ((state->main_function->next != state->functions) ||
13676 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013677 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013678 "state->main_function is not at the end of the function list ");
13679 }
13680 state->main_function->u.cval = 1;
13681 reverse_walk_functions(state, mark_live, 0);
13682}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013683
Stefan Reinauer14e22772010-04-27 06:56:47 +000013684static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013685 struct triple *func, struct triple *ins)
13686{
13687 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13688#if 0
13689 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013690 FILE *fp = state->errout;
13691 fprintf(fp, "global: ");
13692 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013693 }
13694#endif
13695 return local;
13696}
13697
Stefan Reinauer14e22772010-04-27 06:56:47 +000013698struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013699 struct occurance *base_occurance)
13700{
13701 struct triple *nfunc;
13702 struct triple *nfirst, *ofirst;
13703 struct triple *new, *old;
13704
13705 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013706 FILE *fp = state->dbgout;
13707 fprintf(fp, "\n");
13708 loc(fp, state, 0);
13709 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13710 display_func(state, fp, ofunc);
13711 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013712 }
13713
13714 /* Make a new copy of the old function */
13715 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13716 nfirst = 0;
13717 ofirst = old = RHS(ofunc, 0);
13718 do {
13719 struct triple *new;
13720 struct occurance *occurance;
13721 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013722 old_lhs = old->lhs;
13723 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013724 occurance = inline_occurance(state, base_occurance, old->occurance);
13725 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13726 MISC(old, 0)->u.cval += 1;
13727 }
13728 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13729 occurance);
13730 if (!triple_stores_block(state, new)) {
13731 memcpy(&new->u, &old->u, sizeof(new->u));
13732 }
13733 if (!nfirst) {
13734 RHS(nfunc, 0) = nfirst = new;
13735 }
13736 else {
13737 insert_triple(state, nfirst, new);
13738 }
13739 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013740 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013741
Eric Biederman5ade04a2003-10-22 04:03:46 +000013742 /* During the copy remember new as user of old */
13743 use_triple(old, new);
13744
Eric Biederman5ade04a2003-10-22 04:03:46 +000013745 /* Remember which instructions are local */
13746 old->id |= TRIPLE_FLAG_LOCAL;
13747 old = old->next;
13748 } while(old != ofirst);
13749
13750 /* Make a second pass to fix up any unresolved references */
13751 old = ofirst;
13752 new = nfirst;
13753 do {
13754 struct triple **oexpr, **nexpr;
13755 int count, i;
13756 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013757 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013758 for(i = 0; i < count; i++) {
13759 oexpr = &old->param[i];
13760 nexpr = &new->param[i];
13761 if (*oexpr && !*nexpr) {
13762 if (!local_triple(state, ofunc, *oexpr)) {
13763 *nexpr = *oexpr;
13764 }
13765 else if ((*oexpr)->use) {
13766 *nexpr = (*oexpr)->use->member;
13767 }
13768 if (*nexpr == old) {
13769 internal_error(state, 0, "new == old?");
13770 }
13771 use_triple(*nexpr, new);
13772 }
13773 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013774 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013775 }
13776 }
13777 old = old->next;
13778 new = new->next;
13779 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013780
Eric Biederman5ade04a2003-10-22 04:03:46 +000013781 /* Make a third pass to cleanup the extra useses */
13782 old = ofirst;
13783 new = nfirst;
13784 do {
13785 unuse_triple(old, new);
13786 /* Forget which instructions are local */
13787 old->id &= ~TRIPLE_FLAG_LOCAL;
13788 old = old->next;
13789 new = new->next;
13790 } while ((old != ofirst) && (new != nfirst));
13791 return nfunc;
13792}
13793
Eric Biederman90089602004-05-28 14:11:54 +000013794static void expand_inline_call(
13795 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013796{
13797 /* Inline the function call */
13798 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013799 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013800 struct triple *end, *nend;
13801 int pvals, i;
13802
13803 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013804 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013805 if (ofunc->op != OP_LIST) {
13806 internal_error(state, 0, "improper function");
13807 }
Eric Biederman90089602004-05-28 14:11:54 +000013808 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013809 /* Prepend the parameter reading into the new function list */
13810 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013811 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013812 for(i = 0; i < pvals; i++) {
13813 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013814 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013815 atype = ptype;
13816 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13817 atype = ptype->left;
13818 }
Eric Biederman90089602004-05-28 14:11:54 +000013819 param = farg(state, nfunc, i);
13820 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13821 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013822 }
Eric Biederman90089602004-05-28 14:11:54 +000013823 arg = RHS(fcall, i);
13824 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013825 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013826 }
13827 result = 0;
13828 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013829 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013830 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013831 }
13832 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013833 FILE *fp = state->dbgout;
13834 fprintf(fp, "\n");
13835 loc(fp, state, 0);
13836 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13837 display_func(state, fp, nfunc);
13838 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013839 }
13840
Stefan Reinauer14e22772010-04-27 06:56:47 +000013841 /*
13842 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013843 */
13844 /* Remove the read of the return address */
13845 ins = RHS(nfunc, 0)->prev->prev;
13846 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13847 internal_error(state, ins, "Not return addres read?");
13848 }
13849 release_triple(state, ins);
13850 /* Remove the return instruction */
13851 ins = RHS(nfunc, 0)->prev;
13852 if (ins->op != OP_RET) {
13853 internal_error(state, ins, "Not return?");
13854 }
13855 release_triple(state, ins);
13856 /* Remove the retaddres variable */
13857 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013858 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013859 (retvar->op != OP_ADECL) ||
13860 (retvar->next->op != OP_PIECE) ||
13861 (MISC(retvar->next, 0) != retvar)) {
13862 internal_error(state, retvar, "Not the return address?");
13863 }
13864 release_triple(state, retvar->next);
13865 release_triple(state, retvar);
13866
13867 /* Remove the label at the start of the function */
13868 ins = RHS(nfunc, 0);
13869 if (ins->op != OP_LABEL) {
13870 internal_error(state, ins, "Not label?");
13871 }
13872 nfirst = ins->next;
13873 free_triple(state, ins);
13874 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013875 RHS(nfunc, 0) = 0;
13876 free_triple(state, nfunc);
13877
13878 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013879 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013880 nend = nfirst->prev;
13881 end->next = nfirst;
13882 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013883 nend->next = fcall;
13884 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013885
Eric Biederman90089602004-05-28 14:11:54 +000013886 /* Now the result reading code */
13887 if (result) {
13888 result = flatten(state, fcall, result);
13889 propogate_use(state, fcall, result);
13890 }
13891
13892 /* Release the original fcall instruction */
13893 release_triple(state, fcall);
13894
13895 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013896}
13897
Eric Biederman90089602004-05-28 14:11:54 +000013898/*
13899 *
13900 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013901 *
Eric Biederman90089602004-05-28 14:11:54 +000013902 * result
13903 * |
13904 * +----------+------------+
13905 * | |
13906 * union of closures result_type
13907 * |
13908 * +------------------+---------------+
13909 * | |
13910 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013911 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013912 * +----+--+-+--------+-----+ +----+----+---+-----+
13913 * | | | | | | | | |
13914 * var1 var2 var3 ... varN result var1 var2 ... varN result
13915 * |
13916 * +--------+---------+
13917 * | |
13918 * union of closures result_type
13919 * |
13920 * +-----+-------------------+
13921 * | |
13922 * closure1 ... closureN
13923 * | |
13924 * +-----+---+----+----+ +----+---+----+-----+
13925 * | | | | | | | |
13926 * var1 var2 ... varN result var1 var2 ... varN result
13927 */
13928
Stefan Reinauer14e22772010-04-27 06:56:47 +000013929static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013930 struct triple *func, struct type *closure_type)
13931{
13932 struct type *type, *ctype, **next;
13933 struct triple *var, *new_var;
13934 int i;
13935
13936#if 0
13937 FILE *fp = state->errout;
13938 fprintf(fp, "original_type: ");
13939 name_of(fp, fresult(state, func)->type);
13940 fprintf(fp, "\n");
13941#endif
13942 /* find the original type */
13943 var = fresult(state, func);
13944 type = var->type;
13945 if (type->elements != 2) {
13946 internal_error(state, var, "bad return type");
13947 }
13948
13949 /* Find the complete closure type and update it */
13950 ctype = type->left->left;
13951 next = &ctype->left;
13952 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13953 next = &(*next)->right;
13954 }
13955 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13956 ctype->elements += 1;
13957
13958#if 0
13959 fprintf(fp, "new_type: ");
13960 name_of(fp, type);
13961 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013962 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013963 ctype, ctype->elements, reg_size_of(state, ctype));
13964 name_of(fp, ctype);
13965 fprintf(fp, "\n");
13966#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013967
Eric Biederman90089602004-05-28 14:11:54 +000013968 /* Regenerate the variable with the new type definition */
13969 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13970 new_var->id |= TRIPLE_FLAG_FLATTENED;
13971 for(i = 0; i < new_var->lhs; i++) {
13972 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13973 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013974
Eric Biederman90089602004-05-28 14:11:54 +000013975 /* Point everyone at the new variable */
13976 propogate_use(state, var, new_var);
13977
13978 /* Release the original variable */
13979 for(i = 0; i < var->lhs; i++) {
13980 release_triple(state, LHS(var, i));
13981 }
13982 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013983
Eric Biederman90089602004-05-28 14:11:54 +000013984 /* Return the index of the added closure type */
13985 return ctype->elements - 1;
13986}
13987
13988static struct triple *closure_expr(struct compile_state *state,
13989 struct triple *func, int closure_idx, int var_idx)
13990{
13991 return deref_index(state,
13992 deref_index(state,
13993 deref_index(state, fresult(state, func), 0),
13994 closure_idx),
13995 var_idx);
13996}
13997
13998
13999static void insert_triple_set(
14000 struct triple_reg_set **head, struct triple *member)
14001{
14002 struct triple_reg_set *new;
14003 new = xcmalloc(sizeof(*new), "triple_set");
14004 new->member = member;
14005 new->new = 0;
14006 new->next = *head;
14007 *head = new;
14008}
14009
14010static int ordered_triple_set(
14011 struct triple_reg_set **head, struct triple *member)
14012{
14013 struct triple_reg_set **ptr;
14014 if (!member)
14015 return 0;
14016 ptr = head;
14017 while(*ptr) {
14018 if (member == (*ptr)->member) {
14019 return 0;
14020 }
14021 /* keep the list ordered */
14022 if (member->id < (*ptr)->member->id) {
14023 break;
14024 }
14025 ptr = &(*ptr)->next;
14026 }
14027 insert_triple_set(ptr, member);
14028 return 1;
14029}
14030
14031
14032static void free_closure_variables(struct compile_state *state,
14033 struct triple_reg_set **enclose)
14034{
14035 struct triple_reg_set *entry, *next;
14036 for(entry = *enclose; entry; entry = next) {
14037 next = entry->next;
14038 do_triple_unset(enclose, entry->member);
14039 }
14040}
14041
14042static int lookup_closure_index(struct compile_state *state,
14043 struct triple *me, struct triple *val)
14044{
14045 struct triple *first, *ins, *next;
14046 first = RHS(me, 0);
14047 ins = next = first;
14048 do {
14049 struct triple *result;
14050 struct triple *index0, *index1, *index2, *read, *write;
14051 ins = next;
14052 next = ins->next;
14053 if (ins->op != OP_CALL) {
14054 continue;
14055 }
14056 /* I am at a previous call point examine it closely */
14057 if (ins->next->op != OP_LABEL) {
14058 internal_error(state, ins, "call not followed by label");
14059 }
14060 /* Does this call does not enclose any variables? */
14061 if ((ins->next->next->op != OP_INDEX) ||
14062 (ins->next->next->u.cval != 0) ||
14063 (result = MISC(ins->next->next, 0)) ||
14064 (result->id & TRIPLE_FLAG_LOCAL)) {
14065 continue;
14066 }
14067 index0 = ins->next->next;
14068 /* The pattern is:
14069 * 0 index result < 0 >
14070 * 1 index 0 < ? >
14071 * 2 index 1 < ? >
14072 * 3 read 2
14073 * 4 write 3 var
14074 */
14075 for(index0 = ins->next->next;
14076 (index0->op == OP_INDEX) &&
14077 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014078 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014079 index0 = write->next)
14080 {
14081 index1 = index0->next;
14082 index2 = index1->next;
14083 read = index2->next;
14084 write = read->next;
14085 if ((index0->op != OP_INDEX) ||
14086 (index1->op != OP_INDEX) ||
14087 (index2->op != OP_INDEX) ||
14088 (read->op != OP_READ) ||
14089 (write->op != OP_WRITE) ||
14090 (MISC(index1, 0) != index0) ||
14091 (MISC(index2, 0) != index1) ||
14092 (RHS(read, 0) != index2) ||
14093 (RHS(write, 0) != read)) {
14094 internal_error(state, index0, "bad var read");
14095 }
14096 if (MISC(write, 0) == val) {
14097 return index2->u.cval;
14098 }
14099 }
14100 } while(next != first);
14101 return -1;
14102}
14103
14104static inline int enclose_triple(struct triple *ins)
14105{
14106 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14107}
14108
14109static void compute_closure_variables(struct compile_state *state,
14110 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14111{
14112 struct triple_reg_set *set, *vars, **last_var;
14113 struct basic_blocks bb;
14114 struct reg_block *rb;
14115 struct block *block;
14116 struct triple *old_result, *first, *ins;
14117 size_t count, idx;
14118 unsigned long used_indicies;
14119 int i, max_index;
14120#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14121#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014122 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014123 unsigned id;
14124 int index;
14125 } *info;
14126
Stefan Reinauer14e22772010-04-27 06:56:47 +000014127
Eric Biederman90089602004-05-28 14:11:54 +000014128 /* Find the basic blocks of this function */
14129 bb.func = me;
14130 bb.first = RHS(me, 0);
14131 old_result = 0;
14132 if (!triple_is_ret(state, bb.first->prev)) {
14133 bb.func = 0;
14134 } else {
14135 old_result = fresult(state, me);
14136 }
14137 analyze_basic_blocks(state, &bb);
14138
14139 /* Find which variables are currently alive in a given block */
14140 rb = compute_variable_lifetimes(state, &bb);
14141
14142 /* Find the variables that are currently alive */
14143 block = block_of_triple(state, fcall);
14144 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14145 internal_error(state, fcall, "No reg block? block: %p", block);
14146 }
14147
14148#if DEBUG_EXPLICIT_CLOSURES
14149 print_live_variables(state, &bb, rb, state->dbgout);
14150 fflush(state->dbgout);
14151#endif
14152
14153 /* Count the number of triples in the function */
14154 first = RHS(me, 0);
14155 ins = first;
14156 count = 0;
14157 do {
14158 count++;
14159 ins = ins->next;
14160 } while(ins != first);
14161
14162 /* Allocate some memory to temorary hold the id info */
14163 info = xcmalloc(sizeof(*info) * (count +1), "info");
14164
14165 /* Mark the local function */
14166 first = RHS(me, 0);
14167 ins = first;
14168 idx = 1;
14169 do {
14170 info[idx].id = ins->id;
14171 ins->id = TRIPLE_FLAG_LOCAL | idx;
14172 idx++;
14173 ins = ins->next;
14174 } while(ins != first);
14175
Stefan Reinauer14e22772010-04-27 06:56:47 +000014176 /*
Eric Biederman90089602004-05-28 14:11:54 +000014177 * Build the list of variables to enclose.
14178 *
14179 * A target it to put the same variable in the
14180 * same slot for ever call of a given function.
14181 * After coloring this removes all of the variable
14182 * manipulation code.
14183 *
14184 * The list of variables to enclose is built ordered
14185 * program order because except in corner cases this
14186 * gives me the stability of assignment I need.
14187 *
14188 * To gurantee that stability I lookup the variables
14189 * to see where they have been used before and
14190 * I build my final list with the assigned indicies.
14191 */
14192 vars = 0;
14193 if (enclose_triple(old_result)) {
14194 ordered_triple_set(&vars, old_result);
14195 }
14196 for(set = rb[block->vertex].out; set; set = set->next) {
14197 if (!enclose_triple(set->member)) {
14198 continue;
14199 }
14200 if ((set->member == fcall) || (set->member == old_result)) {
14201 continue;
14202 }
14203 if (!local_triple(state, me, set->member)) {
14204 internal_error(state, set->member, "not local?");
14205 }
14206 ordered_triple_set(&vars, set->member);
14207 }
14208
14209 /* Lookup the current indicies of the live varialbe */
14210 used_indicies = 0;
14211 max_index = -1;
14212 for(set = vars; set ; set = set->next) {
14213 struct triple *ins;
14214 int index;
14215 ins = set->member;
14216 index = lookup_closure_index(state, me, ins);
14217 info[ID_BITS(ins->id)].index = index;
14218 if (index < 0) {
14219 continue;
14220 }
14221 if (index >= MAX_INDICIES) {
14222 internal_error(state, ins, "index unexpectedly large");
14223 }
14224 if (used_indicies & (1 << index)) {
14225 internal_error(state, ins, "index previously used?");
14226 }
14227 /* Remember which indicies have been used */
14228 used_indicies |= (1 << index);
14229 if (index > max_index) {
14230 max_index = index;
14231 }
14232 }
14233
14234 /* Walk through the live variables and make certain
14235 * everything is assigned an index.
14236 */
14237 for(set = vars; set; set = set->next) {
14238 struct triple *ins;
14239 int index;
14240 ins = set->member;
14241 index = info[ID_BITS(ins->id)].index;
14242 if (index >= 0) {
14243 continue;
14244 }
14245 /* Find the lowest unused index value */
14246 for(index = 0; index < MAX_INDICIES; index++) {
14247 if (!(used_indicies & (1 << index))) {
14248 break;
14249 }
14250 }
14251 if (index == MAX_INDICIES) {
14252 internal_error(state, ins, "no free indicies?");
14253 }
14254 info[ID_BITS(ins->id)].index = index;
14255 /* Remember which indicies have been used */
14256 used_indicies |= (1 << index);
14257 if (index > max_index) {
14258 max_index = index;
14259 }
14260 }
14261
14262 /* Build the return list of variables with positions matching
14263 * their indicies.
14264 */
14265 *enclose = 0;
14266 last_var = enclose;
14267 for(i = 0; i <= max_index; i++) {
14268 struct triple *var;
14269 var = 0;
14270 if (used_indicies & (1 << i)) {
14271 for(set = vars; set; set = set->next) {
14272 int index;
14273 index = info[ID_BITS(set->member->id)].index;
14274 if (index == i) {
14275 var = set->member;
14276 break;
14277 }
14278 }
14279 if (!var) {
14280 internal_error(state, me, "missing variable");
14281 }
14282 }
14283 insert_triple_set(last_var, var);
14284 last_var = &(*last_var)->next;
14285 }
14286
14287#if DEBUG_EXPLICIT_CLOSURES
14288 /* Print out the variables to be enclosed */
14289 loc(state->dbgout, state, fcall);
14290 fprintf(state->dbgout, "Alive: \n");
14291 for(set = *enclose; set; set = set->next) {
14292 display_triple(state->dbgout, set->member);
14293 }
14294 fflush(state->dbgout);
14295#endif
14296
14297 /* Clear the marks */
14298 ins = first;
14299 do {
14300 ins->id = info[ID_BITS(ins->id)].id;
14301 ins = ins->next;
14302 } while(ins != first);
14303
14304 /* Release the ordered list of live variables */
14305 free_closure_variables(state, &vars);
14306
14307 /* Release the storage of the old ids */
14308 xfree(info);
14309
14310 /* Release the variable lifetime information */
14311 free_variable_lifetimes(state, &bb, rb);
14312
14313 /* Release the basic blocks of this function */
14314 free_basic_blocks(state, &bb);
14315}
14316
14317static void expand_function_call(
14318 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014319{
14320 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014321 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014322 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014323 struct triple *first;
14324 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014325 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014326 struct triple_reg_set *enclose, *set;
14327 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014328
Eric Biederman90089602004-05-28 14:11:54 +000014329#if DEBUG_EXPLICIT_CLOSURES
14330 FILE *fp = state->dbgout;
14331 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14332 display_func(state, fp, MISC(fcall, 0));
14333 display_func(state, fp, me);
14334 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14335#endif
14336
Eric Biederman5ade04a2003-10-22 04:03:46 +000014337 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014338 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014339 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014340 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014341 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014342 first = fcall->next;
14343
14344 /* Find what I need to enclose */
14345 compute_closure_variables(state, me, fcall, &enclose);
14346
14347 /* Compute the closure type */
14348 closure_type = new_type(TYPE_TUPLE, 0, 0);
14349 closure_type->elements = 0;
14350 closure_next = &closure_type->left;
14351 for(set = enclose; set ; set = set->next) {
14352 struct type *type;
14353 type = &void_type;
14354 if (set->member) {
14355 type = set->member->type;
14356 }
14357 if (!*closure_next) {
14358 *closure_next = type;
14359 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014360 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014361 type);
14362 closure_next = &(*closure_next)->right;
14363 }
14364 closure_type->elements += 1;
14365 }
14366 if (closure_type->elements == 0) {
14367 closure_type->type = TYPE_VOID;
14368 }
14369
14370
14371#if DEBUG_EXPLICIT_CLOSURES
14372 fprintf(state->dbgout, "closure type: ");
14373 name_of(state->dbgout, closure_type);
14374 fprintf(state->dbgout, "\n");
14375#endif
14376
14377 /* Update the called functions closure variable */
14378 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014379
14380 /* Generate some needed triples */
14381 ret_loc = label(state);
14382 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14383
14384 /* Pass the parameters to the new function */
14385 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014386 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014387 for(i = 0; i < pvals; i++) {
14388 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014389 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014390 atype = ptype;
14391 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14392 atype = ptype->left;
14393 }
Eric Biederman90089602004-05-28 14:11:54 +000014394 param = farg(state, func, i);
14395 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14396 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014397 }
Eric Biederman90089602004-05-28 14:11:54 +000014398 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014399 flatten(state, first, write_expr(state, param, arg));
14400 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014401 }
Eric Biederman90089602004-05-28 14:11:54 +000014402 rtype = func->type->left;
14403
Eric Biederman5ade04a2003-10-22 04:03:46 +000014404 /* Thread the triples together */
14405 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014406
14407 /* Save the active variables in the result variable */
14408 for(i = 0, set = enclose; set ; set = set->next, i++) {
14409 if (!set->member) {
14410 continue;
14411 }
14412 flatten(state, ret_loc,
14413 write_expr(state,
14414 closure_expr(state, func, closure_idx, i),
14415 read_expr(state, set->member)));
14416 }
14417
14418 /* Initialize the return value */
14419 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014420 flatten(state, ret_loc,
14421 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014422 deref_index(state, fresult(state, func), 1),
14423 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14424 }
14425
Eric Biederman5ade04a2003-10-22 04:03:46 +000014426 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014427 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14428 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014429 call(state, retvar, ret_addr, func_first, func_last));
14430
Eric Biederman7dea9552004-06-29 05:38:37 +000014431 /* Find the result */
14432 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14433 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014434 result = flatten(state, first,
14435 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014436 deref_index(state, fresult(state, func), 1)));
14437
14438 propogate_use(state, fcall, result);
14439 }
14440
14441 /* Release the original fcall instruction */
14442 release_triple(state, fcall);
14443
Eric Biederman90089602004-05-28 14:11:54 +000014444 /* Restore the active variables from the result variable */
14445 for(i = 0, set = enclose; set ; set = set->next, i++) {
14446 struct triple_set *use, *next;
14447 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014448 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014449 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014450 continue;
14451 }
Eric Biederman90089602004-05-28 14:11:54 +000014452 /* Generate an expression for the value */
14453 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014454 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014455 closure_expr(state, func, closure_idx, i)));
14456
14457
14458 /* If the original is an lvalue restore the preserved value */
14459 if (is_lvalue(state, set->member)) {
14460 flatten(state, first,
14461 write_expr(state, set->member, new));
14462 continue;
14463 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014464 /*
14465 * If the original is a value update the dominated uses.
14466 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014467
Eric Biederman7dea9552004-06-29 05:38:37 +000014468 /* Analyze the basic blocks so I can see who dominates whom */
14469 bb.func = me;
14470 bb.first = RHS(me, 0);
14471 if (!triple_is_ret(state, bb.first->prev)) {
14472 bb.func = 0;
14473 }
14474 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014475
Eric Biederman90089602004-05-28 14:11:54 +000014476
14477#if DEBUG_EXPLICIT_CLOSURES
14478 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14479 set->member, new);
14480#endif
14481 /* If fcall dominates the use update the expression */
14482 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014483 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014484 * removes use, so I must take a copy of the
14485 * next entry early.
14486 */
14487 next = use->next;
14488 if (!tdominates(state, fcall, use->member)) {
14489 continue;
14490 }
14491 replace_use(state, set->member, new, use->member);
14492 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014493
14494 /* Release the basic blocks, the instructions will be
14495 * different next time, and flatten/insert_triple does
14496 * not update the block values so I can't cache the analysis.
14497 */
14498 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014499 }
14500
Eric Biederman90089602004-05-28 14:11:54 +000014501 /* Release the closure variable list */
14502 free_closure_variables(state, &enclose);
14503
14504 if (state->compiler->debug & DEBUG_INLINE) {
14505 FILE *fp = state->dbgout;
14506 fprintf(fp, "\n");
14507 loc(fp, state, 0);
14508 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14509 display_func(state, fp, func);
14510 display_func(state, fp, me);
14511 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14512 }
14513
14514 return;
14515}
14516
14517static int do_inline(struct compile_state *state, struct triple *func)
14518{
14519 int do_inline;
14520 int policy;
14521
14522 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14523 switch(policy) {
14524 case COMPILER_INLINE_ALWAYS:
14525 do_inline = 1;
14526 if (func->type->type & ATTRIB_NOINLINE) {
14527 error(state, func, "noinline with always_inline compiler option");
14528 }
14529 break;
14530 case COMPILER_INLINE_NEVER:
14531 do_inline = 0;
14532 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14533 error(state, func, "always_inline with noinline compiler option");
14534 }
14535 break;
14536 case COMPILER_INLINE_DEFAULTON:
14537 switch(func->type->type & STOR_MASK) {
14538 case STOR_STATIC | STOR_INLINE:
14539 case STOR_LOCAL | STOR_INLINE:
14540 case STOR_EXTERN | STOR_INLINE:
14541 do_inline = 1;
14542 break;
14543 default:
14544 do_inline = 1;
14545 break;
14546 }
14547 break;
14548 case COMPILER_INLINE_DEFAULTOFF:
14549 switch(func->type->type & STOR_MASK) {
14550 case STOR_STATIC | STOR_INLINE:
14551 case STOR_LOCAL | STOR_INLINE:
14552 case STOR_EXTERN | STOR_INLINE:
14553 do_inline = 1;
14554 break;
14555 default:
14556 do_inline = 0;
14557 break;
14558 }
14559 break;
14560 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014561 switch(func->type->type & STOR_MASK) {
14562 case STOR_STATIC | STOR_INLINE:
14563 case STOR_LOCAL | STOR_INLINE:
14564 case STOR_EXTERN | STOR_INLINE:
14565 do_inline = 1;
14566 break;
14567 default:
14568 do_inline = (func->u.cval == 1);
14569 break;
14570 }
Eric Biederman90089602004-05-28 14:11:54 +000014571 break;
14572 default:
14573 do_inline = 0;
14574 internal_error(state, 0, "Unimplemented inline policy");
14575 break;
14576 }
14577 /* Force inlining */
14578 if (func->type->type & ATTRIB_NOINLINE) {
14579 do_inline = 0;
14580 }
14581 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14582 do_inline = 1;
14583 }
14584 return do_inline;
14585}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014586
Eric Biederman90089602004-05-28 14:11:54 +000014587static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14588{
14589 struct triple *first, *ptr, *next;
14590 /* If the function is not used don't bother */
14591 if (me->u.cval <= 0) {
14592 return;
14593 }
14594 if (state->compiler->debug & DEBUG_CALLS2) {
14595 FILE *fp = state->dbgout;
14596 fprintf(fp, "in: %s\n",
14597 me->type->type_ident->name);
14598 }
14599
14600 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014601 ptr = next = first;
14602 do {
Eric Biederman90089602004-05-28 14:11:54 +000014603 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014604 ptr = next;
14605 prev = ptr->prev;
14606 next = ptr->next;
14607 if (ptr->op != OP_FCALL) {
14608 continue;
14609 }
14610 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014611 /* See if the function should be inlined */
14612 if (!do_inline(state, func)) {
14613 /* Put a label after the fcall */
14614 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14615 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014616 }
Eric Biederman90089602004-05-28 14:11:54 +000014617 if (state->compiler->debug & DEBUG_CALLS) {
14618 FILE *fp = state->dbgout;
14619 if (state->compiler->debug & DEBUG_CALLS2) {
14620 loc(fp, state, ptr);
14621 }
14622 fprintf(fp, "inlining %s\n",
14623 func->type->type_ident->name);
14624 fflush(fp);
14625 }
14626
14627 /* Update the function use counts */
14628 func->u.cval -= 1;
14629
14630 /* Replace the fcall with the called function */
14631 expand_inline_call(state, me, ptr);
14632
14633 next = prev->next;
14634 } while (next != first);
14635
14636 ptr = next = first;
14637 do {
14638 struct triple *prev, *func;
14639 ptr = next;
14640 prev = ptr->prev;
14641 next = ptr->next;
14642 if (ptr->op != OP_FCALL) {
14643 continue;
14644 }
14645 func = MISC(ptr, 0);
14646 if (state->compiler->debug & DEBUG_CALLS) {
14647 FILE *fp = state->dbgout;
14648 if (state->compiler->debug & DEBUG_CALLS2) {
14649 loc(fp, state, ptr);
14650 }
14651 fprintf(fp, "calling %s\n",
14652 func->type->type_ident->name);
14653 fflush(fp);
14654 }
14655 /* Replace the fcall with the instruction sequence
14656 * needed to make the call.
14657 */
14658 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014659 next = prev->next;
14660 } while(next != first);
14661}
Eric Biederman90089602004-05-28 14:11:54 +000014662
14663static void inline_functions(struct compile_state *state, struct triple *func)
14664{
14665 inline_function(state, func, 0);
14666 reverse_walk_functions(state, inline_function, 0);
14667}
14668
Eric Biederman5ade04a2003-10-22 04:03:46 +000014669static void insert_function(struct compile_state *state,
14670 struct triple *func, void *arg)
14671{
14672 struct triple *first, *end, *ffirst, *fend;
14673
14674 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014675 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014676 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014677 func->type->type_ident->name, func->u.cval);
14678 }
14679 if (func->u.cval == 0) {
14680 return;
14681 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014682
14683 /* Find the end points of the lists */
14684 first = arg;
14685 end = first->prev;
14686 ffirst = RHS(func, 0);
14687 fend = ffirst->prev;
14688
14689 /* splice the lists together */
14690 end->next = ffirst;
14691 ffirst->prev = end;
14692 fend->next = first;
14693 first->prev = fend;
14694}
14695
Eric Biederman90089602004-05-28 14:11:54 +000014696struct triple *input_asm(struct compile_state *state)
14697{
14698 struct asm_info *info;
14699 struct triple *def;
14700 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014701
Eric Biederman90089602004-05-28 14:11:54 +000014702 info = xcmalloc(sizeof(*info), "asm_info");
14703 info->str = "";
14704
14705 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14706 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14707
14708 def = new_triple(state, OP_ASM, &void_type, out, 0);
14709 def->u.ainfo = info;
14710 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014711
Eric Biederman90089602004-05-28 14:11:54 +000014712 for(i = 0; i < out; i++) {
14713 struct triple *piece;
14714 piece = triple(state, OP_PIECE, &int_type, def, 0);
14715 piece->u.cval = i;
14716 LHS(def, i) = piece;
14717 }
14718
14719 return def;
14720}
14721
14722struct triple *output_asm(struct compile_state *state)
14723{
14724 struct asm_info *info;
14725 struct triple *def;
14726 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014727
Eric Biederman90089602004-05-28 14:11:54 +000014728 info = xcmalloc(sizeof(*info), "asm_info");
14729 info->str = "";
14730
14731 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14732 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14733
14734 def = new_triple(state, OP_ASM, &void_type, 0, in);
14735 def->u.ainfo = info;
14736 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014737
Eric Biederman90089602004-05-28 14:11:54 +000014738 return def;
14739}
14740
Eric Biederman5ade04a2003-10-22 04:03:46 +000014741static void join_functions(struct compile_state *state)
14742{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014743 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014744 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014745 struct type *pnext, *param;
14746 struct type *result_type, *args_type;
14747 int idx;
14748
14749 /* Be clear the functions have not been joined yet */
14750 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014751
14752 /* Dummy file state to get debug handing right */
14753 memset(&file, 0, sizeof(file));
14754 file.basename = "";
14755 file.line = 0;
14756 file.report_line = 0;
14757 file.report_name = file.basename;
14758 file.prev = state->file;
14759 state->file = &file;
14760 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014761
Eric Biederman41203d92004-11-08 09:31:09 +000014762 if (!state->main_function) {
14763 error(state, 0, "No functions to compile\n");
14764 }
14765
Eric Biederman90089602004-05-28 14:11:54 +000014766 /* The type of arguments */
14767 args_type = state->main_function->type->right;
14768 /* The return type without any specifiers */
14769 result_type = clone_type(0, state->main_function->type->left);
14770
14771
14772 /* Verify the external arguments */
14773 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014774 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014775 "Too many external input arguments");
14776 }
14777 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014778 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014779 "Too many external output arguments");
14780 }
14781
Eric Biederman5ade04a2003-10-22 04:03:46 +000014782 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014783 end = label(state);
14784 start = label(state);
14785 start = flatten(state, state->first, start);
14786 end = flatten(state, state->first, end);
14787 in = input_asm(state);
14788 out = output_asm(state);
14789 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014790 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014791 in = flatten(state, state->first, in);
14792 call = flatten(state, state->first, call);
14793 out = flatten(state, state->first, out);
14794
14795
14796 /* Read the external input arguments */
14797 pnext = args_type;
14798 idx = 0;
14799 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14800 struct triple *expr;
14801 param = pnext;
14802 pnext = 0;
14803 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14804 pnext = param->right;
14805 param = param->left;
14806 }
14807 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014808 error(state, state->main_function,
14809 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014810 idx + 1, param->field_ident->name);
14811 }
14812 expr = read_expr(state, LHS(in, idx));
14813 RHS(call, idx) = expr;
14814 expr = flatten(state, call, expr);
14815 use_triple(expr, call);
14816
Stefan Reinauer14e22772010-04-27 06:56:47 +000014817 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014818 }
14819
14820
14821 /* Write the external output arguments */
14822 pnext = result_type;
14823 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14824 pnext = result_type->left;
14825 }
14826 for(idx = 0; idx < out->rhs; idx++) {
14827 struct triple *expr;
14828 param = pnext;
14829 pnext = 0;
14830 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14831 pnext = param->right;
14832 param = param->left;
14833 }
14834 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14835 param = 0;
14836 }
14837 if (param) {
14838 if (registers_of(state, param) != 1) {
14839 error(state, state->main_function,
14840 "Result: %d %s requires multiple registers",
14841 idx, param->field_ident->name);
14842 }
14843 expr = read_expr(state, call);
14844 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14845 expr = deref_field(state, expr, param->field_ident);
14846 }
14847 } else {
14848 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14849 }
14850 flatten(state, out, expr);
14851 RHS(out, idx) = expr;
14852 use_triple(expr, out);
14853 }
14854
14855 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014856 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014857 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14858 func->type->type_ident = lookup(state, "", 0);
14859 RHS(func, 0) = state->first;
14860 func->u.cval = 1;
14861
Eric Biederman5ade04a2003-10-22 04:03:46 +000014862 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014863 mark_live_functions(state);
14864 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014865 walk_functions(state, insert_function, end);
14866
14867 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014868 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014869 }
14870
Eric Biederman90089602004-05-28 14:11:54 +000014871 /* OK now the functions have been joined. */
14872 state->functions_joined = 1;
14873
Eric Biederman5ade04a2003-10-22 04:03:46 +000014874 /* Done now cleanup */
14875 state->file = file.prev;
14876 state->function = 0;
14877}
14878
Eric Biedermanb138ac82003-04-22 18:44:01 +000014879/*
14880 * Data structurs for optimation.
14881 */
14882
Eric Biederman5ade04a2003-10-22 04:03:46 +000014883
Eric Biederman83b991a2003-10-11 06:20:25 +000014884static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014885 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014886 int front)
14887{
14888 struct block_set **ptr, *new;
14889 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014890 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014891 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014892 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014893 ptr = head;
14894 while(*ptr) {
14895 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014896 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014897 }
14898 ptr = &(*ptr)->next;
14899 }
14900 new = xcmalloc(sizeof(*new), "block_set");
14901 new->member = user;
14902 if (front) {
14903 new->next = *head;
14904 *head = new;
14905 }
14906 else {
14907 new->next = 0;
14908 *ptr = new;
14909 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014910 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014911}
Eric Biederman83b991a2003-10-11 06:20:25 +000014912static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014913 struct block *used, struct block_set **head, struct block *unuser)
14914{
14915 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014916 int count;
14917 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014918 ptr = head;
14919 while(*ptr) {
14920 use = *ptr;
14921 if (use->member == unuser) {
14922 *ptr = use->next;
14923 memset(use, -1, sizeof(*use));
14924 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014925 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014926 }
14927 else {
14928 ptr = &use->next;
14929 }
14930 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014931 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014932}
14933
14934static void use_block(struct block *used, struct block *user)
14935{
Eric Biederman83b991a2003-10-11 06:20:25 +000014936 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014937 /* Append new to the head of the list, print_block
14938 * depends on this.
14939 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014940 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014941 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014942}
14943static void unuse_block(struct block *used, struct block *unuser)
14944{
Eric Biederman83b991a2003-10-11 06:20:25 +000014945 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014946 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014947 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014948}
14949
Eric Biederman5ade04a2003-10-22 04:03:46 +000014950static void add_block_edge(struct block *block, struct block *edge, int front)
14951{
14952 int count;
14953 count = do_use_block(block, &block->edges, edge, front);
14954 block->edge_count += count;
14955}
14956
14957static void remove_block_edge(struct block *block, struct block *edge)
14958{
14959 int count;
14960 count = do_unuse_block(block, &block->edges, edge);
14961 block->edge_count -= count;
14962}
14963
Eric Biedermanb138ac82003-04-22 18:44:01 +000014964static void idom_block(struct block *idom, struct block *user)
14965{
14966 do_use_block(idom, &idom->idominates, user, 0);
14967}
14968
14969static void unidom_block(struct block *idom, struct block *unuser)
14970{
14971 do_unuse_block(idom, &idom->idominates, unuser);
14972}
14973
14974static void domf_block(struct block *block, struct block *domf)
14975{
14976 do_use_block(block, &block->domfrontier, domf, 0);
14977}
14978
14979static void undomf_block(struct block *block, struct block *undomf)
14980{
14981 do_unuse_block(block, &block->domfrontier, undomf);
14982}
14983
14984static void ipdom_block(struct block *ipdom, struct block *user)
14985{
14986 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14987}
14988
14989static void unipdom_block(struct block *ipdom, struct block *unuser)
14990{
14991 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14992}
14993
14994static void ipdomf_block(struct block *block, struct block *ipdomf)
14995{
14996 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14997}
14998
14999static void unipdomf_block(struct block *block, struct block *unipdomf)
15000{
15001 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15002}
15003
Eric Biederman83b991a2003-10-11 06:20:25 +000015004static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015005 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015006 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15007 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015008{
Eric Biederman83b991a2003-10-11 06:20:25 +000015009 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015010 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015011 ptr = state->first;
15012 do {
Eric Biederman90089602004-05-28 14:11:54 +000015013 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015014 if (ptr->next->prev != ptr) {
15015 internal_error(state, ptr->next, "bad prev");
15016 }
15017 ptr = ptr->next;
15018 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015019 return result;
15020}
15021
Eric Biedermanb138ac82003-04-22 18:44:01 +000015022#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015023static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015024{
Eric Biederman90089602004-05-28 14:11:54 +000015025 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015026 int op;
15027 op = ins->op;
15028 if (op == OP_LIST) {
15029#if !PRINT_LIST
15030 return 0;
15031#endif
15032 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015033 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015034 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015035 }
Eric Biederman90089602004-05-28 14:11:54 +000015036 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015037
Stefan Reinauer14e22772010-04-27 06:56:47 +000015038 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015039 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015040 internal_error(state, ins, "branch used?");
15041 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015042 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015043 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015044 }
15045 return 0;
15046}
15047
Eric Biedermanb138ac82003-04-22 18:44:01 +000015048static void print_triples(struct compile_state *state)
15049{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015050 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015051 FILE *fp = state->dbgout;
15052 fprintf(fp, "--------------- triples ---------------\n");
15053 walk_triples(state, do_print_triple, fp);
15054 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015055 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015056}
15057
15058struct cf_block {
15059 struct block *block;
15060};
15061static void find_cf_blocks(struct cf_block *cf, struct block *block)
15062{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015063 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015064 if (!block || (cf[block->vertex].block == block)) {
15065 return;
15066 }
15067 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015068 for(edge = block->edges; edge; edge = edge->next) {
15069 find_cf_blocks(cf, edge->member);
15070 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015071}
15072
Eric Biederman90089602004-05-28 14:11:54 +000015073static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015074 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015075{
15076 struct cf_block *cf;
15077 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015078 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015079 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15080 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015081
Eric Biederman90089602004-05-28 14:11:54 +000015082 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015083 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015084 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015085 block = cf[i].block;
15086 if (!block)
15087 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015088 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015089 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015090 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015091 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015092 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015093 }
15094
15095 xfree(cf);
15096}
15097
Eric Biedermanb138ac82003-04-22 18:44:01 +000015098static void free_basic_block(struct compile_state *state, struct block *block)
15099{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015100 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015101 struct block *child;
15102 if (!block) {
15103 return;
15104 }
15105 if (block->vertex == -1) {
15106 return;
15107 }
15108 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015109 for(edge = block->edges; edge; edge = edge->next) {
15110 if (edge->member) {
15111 unuse_block(edge->member, block);
15112 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015113 }
15114 if (block->idom) {
15115 unidom_block(block->idom, block);
15116 }
15117 block->idom = 0;
15118 if (block->ipdom) {
15119 unipdom_block(block->ipdom, block);
15120 }
15121 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015122 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015123 child = entry->member;
15124 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015125 if (child && (child->vertex != -1)) {
15126 for(edge = child->edges; edge; edge = edge->next) {
15127 edge->member = 0;
15128 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015129 }
15130 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015131 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015132 child = entry->member;
15133 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015134 if (child && (child->vertex != -1)) {
15135 child->idom = 0;
15136 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015137 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015138 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015139 child = entry->member;
15140 undomf_block(block, child);
15141 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015142 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015143 child = entry->member;
15144 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015145 if (child && (child->vertex != -1)) {
15146 child->ipdom = 0;
15147 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015148 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015149 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015150 child = entry->member;
15151 unipdomf_block(block, child);
15152 }
15153 if (block->users != 0) {
15154 internal_error(state, 0, "block still has users");
15155 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015156 while((edge = block->edges)) {
15157 child = edge->member;
15158 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015159
Eric Biederman5ade04a2003-10-22 04:03:46 +000015160 if (child && (child->vertex != -1)) {
15161 free_basic_block(state, child);
15162 }
15163 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015164 memset(block, -1, sizeof(*block));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015165}
15166
Stefan Reinauer14e22772010-04-27 06:56:47 +000015167static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015168 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015169{
15170 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015171 free_basic_block(state, bb->first_block);
15172 bb->last_vertex = 0;
15173 bb->first_block = bb->last_block = 0;
15174 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015175 ins = first;
15176 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015177 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015178 ins->u.block = 0;
15179 }
15180 ins = ins->next;
15181 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015182
Eric Biedermanb138ac82003-04-22 18:44:01 +000015183}
15184
Stefan Reinauer14e22772010-04-27 06:56:47 +000015185static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015186 struct basic_blocks *bb, struct triple *first)
15187{
15188 struct block *block;
15189 struct triple *ptr;
15190 if (!triple_is_label(state, first)) {
15191 internal_error(state, first, "block does not start with a label");
15192 }
15193 /* See if this basic block has already been setup */
15194 if (first->u.block != 0) {
15195 return first->u.block;
15196 }
15197 /* Allocate another basic block structure */
15198 bb->last_vertex += 1;
15199 block = xcmalloc(sizeof(*block), "block");
15200 block->first = block->last = first;
15201 block->vertex = bb->last_vertex;
15202 ptr = first;
15203 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015204 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015205 break;
15206 }
15207 block->last = ptr;
15208 /* If ptr->u is not used remember where the baic block is */
15209 if (triple_stores_block(state, ptr)) {
15210 ptr->u.block = block;
15211 }
15212 if (triple_is_branch(state, ptr)) {
15213 break;
15214 }
15215 ptr = ptr->next;
15216 } while (ptr != bb->first);
15217 if ((ptr == bb->first) ||
15218 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015219 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015220 triple_is_ret(state, ptr))))
15221 {
15222 /* The block has no outflowing edges */
15223 }
15224 else if (triple_is_label(state, ptr)) {
15225 struct block *next;
15226 next = basic_block(state, bb, ptr);
15227 add_block_edge(block, next, 0);
15228 use_block(next, block);
15229 }
15230 else if (triple_is_branch(state, ptr)) {
15231 struct triple **expr, *first;
15232 struct block *child;
15233 /* Find the branch targets.
15234 * I special case the first branch as that magically
15235 * avoids some difficult cases for the register allocator.
15236 */
15237 expr = triple_edge_targ(state, ptr, 0);
15238 if (!expr) {
15239 internal_error(state, ptr, "branch without targets");
15240 }
15241 first = *expr;
15242 expr = triple_edge_targ(state, ptr, expr);
15243 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15244 if (!*expr) continue;
15245 child = basic_block(state, bb, *expr);
15246 use_block(child, block);
15247 add_block_edge(block, child, 0);
15248 }
15249 if (first) {
15250 child = basic_block(state, bb, first);
15251 use_block(child, block);
15252 add_block_edge(block, child, 1);
15253
15254 /* Be certain the return block of a call is
15255 * in a basic block. When it is not find
15256 * start of the block, insert a label if
15257 * necessary and build the basic block.
15258 * Then add a fake edge from the start block
15259 * to the return block of the function.
15260 */
15261 if (state->functions_joined && triple_is_call(state, ptr)
15262 && !block_of_triple(state, MISC(ptr, 0))) {
15263 struct block *tail;
15264 struct triple *start;
15265 start = triple_to_block_start(state, MISC(ptr, 0));
15266 if (!triple_is_label(state, start)) {
15267 start = pre_triple(state,
15268 start, OP_LABEL, &void_type, 0, 0);
15269 }
15270 tail = basic_block(state, bb, start);
15271 add_block_edge(child, tail, 0);
15272 use_block(tail, child);
15273 }
15274 }
15275 }
15276 else {
15277 internal_error(state, 0, "Bad basic block split");
15278 }
15279#if 0
15280{
15281 struct block_set *edge;
15282 FILE *fp = state->errout;
15283 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015284 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015285 block->first, block->last);
15286 for(edge = block->edges; edge; edge = edge->next) {
15287 fprintf(fp, " %10p [%2d]",
15288 edge->member ? edge->member->first : 0,
15289 edge->member ? edge->member->vertex : -1);
15290 }
15291 fprintf(fp, "\n");
15292}
15293#endif
15294 return block;
15295}
15296
15297
15298static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15299 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15300 void *arg)
15301{
15302 struct triple *ptr, *first;
15303 struct block *last_block;
15304 last_block = 0;
15305 first = bb->first;
15306 ptr = first;
15307 do {
15308 if (triple_stores_block(state, ptr)) {
15309 struct block *block;
15310 block = ptr->u.block;
15311 if (block && (block != last_block)) {
15312 cb(state, block, arg);
15313 }
15314 last_block = block;
15315 }
15316 ptr = ptr->next;
15317 } while(ptr != first);
15318}
15319
15320static void print_block(
15321 struct compile_state *state, struct block *block, void *arg)
15322{
15323 struct block_set *user, *edge;
15324 struct triple *ptr;
15325 FILE *fp = arg;
15326
15327 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015328 block,
Eric Biederman90089602004-05-28 14:11:54 +000015329 block->vertex);
15330
15331 for(edge = block->edges; edge; edge = edge->next) {
15332 fprintf(fp, " %p<-%p",
15333 edge->member,
15334 (edge->member && edge->member->use)?
15335 edge->member->use->member : 0);
15336 }
15337 fprintf(fp, "\n");
15338 if (block->first->op == OP_LABEL) {
15339 fprintf(fp, "%p:\n", block->first);
15340 }
15341 for(ptr = block->first; ; ) {
15342 display_triple(fp, ptr);
15343 if (ptr == block->last)
15344 break;
15345 ptr = ptr->next;
15346 if (ptr == block->first) {
15347 internal_error(state, 0, "missing block last?");
15348 }
15349 }
15350 fprintf(fp, "users %d: ", block->users);
15351 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015352 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015353 user->member,
15354 user->member->vertex);
15355 }
15356 fprintf(fp,"\n\n");
15357}
15358
15359
15360static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15361{
15362 fprintf(fp, "--------------- blocks ---------------\n");
15363 walk_blocks(state, &state->bb, print_block, fp);
15364}
15365static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15366{
15367 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15368 fprintf(fp, "After %s\n", func);
15369 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015370 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15371 print_dominators(state, fp, &state->bb);
15372 print_dominance_frontiers(state, fp, &state->bb);
15373 }
15374 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015375 }
15376}
15377
Stefan Reinauer14e22772010-04-27 06:56:47 +000015378static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015379 struct basic_blocks *bb)
15380{
15381 struct block *block;
15382 struct triple *first, *ins, *next;
15383 /* Delete the triples not in a basic block */
15384 block = 0;
15385 first = bb->first;
15386 ins = first;
15387 do {
15388 next = ins->next;
15389 if (ins->op == OP_LABEL) {
15390 block = ins->u.block;
15391 }
15392 if (!block) {
15393 struct triple_set *use;
15394 for(use = ins->use; use; use = use->next) {
15395 struct block *block;
15396 block = block_of_triple(state, use->member);
15397 if (block != 0) {
15398 internal_error(state, ins, "pruning used ins?");
15399 }
15400 }
15401 release_triple(state, ins);
15402 }
15403 if (block && block->last == ins) {
15404 block = 0;
15405 }
15406 ins = next;
15407 } while(ins != first);
15408}
15409
Stefan Reinauer14e22772010-04-27 06:56:47 +000015410static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015411 struct basic_blocks *bb)
15412{
15413 if (!triple_stores_block(state, bb->first)) {
15414 internal_error(state, 0, "ins will not store block?");
15415 }
15416 /* Initialize the state */
15417 bb->first_block = bb->last_block = 0;
15418 bb->last_vertex = 0;
15419 free_basic_blocks(state, bb);
15420
15421 /* Find the basic blocks */
15422 bb->first_block = basic_block(state, bb, bb->first);
15423
15424 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015425 * entire program is in a basic block. When it is not find
15426 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015427 * basic block. Then add a fake edge from the start block
15428 * to the final block.
15429 */
15430 if (!block_of_triple(state, bb->first->prev)) {
15431 struct triple *start;
15432 struct block *tail;
15433 start = triple_to_block_start(state, bb->first->prev);
15434 if (!triple_is_label(state, start)) {
15435 start = pre_triple(state,
15436 start, OP_LABEL, &void_type, 0, 0);
15437 }
15438 tail = basic_block(state, bb, start);
15439 add_block_edge(bb->first_block, tail, 0);
15440 use_block(tail, bb->first_block);
15441 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015442
Eric Biederman90089602004-05-28 14:11:54 +000015443 /* Find the last basic block.
15444 */
15445 bb->last_block = block_of_triple(state, bb->first->prev);
15446
15447 /* Delete the triples not in a basic block */
15448 prune_nonblock_triples(state, bb);
15449
15450#if 0
15451 /* If we are debugging print what I have just done */
15452 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15453 print_blocks(state, state->dbgout);
15454 print_control_flow(state, bb);
15455 }
15456#endif
15457}
15458
15459
Eric Biedermanb138ac82003-04-22 18:44:01 +000015460struct sdom_block {
15461 struct block *block;
15462 struct sdom_block *sdominates;
15463 struct sdom_block *sdom_next;
15464 struct sdom_block *sdom;
15465 struct sdom_block *label;
15466 struct sdom_block *parent;
15467 struct sdom_block *ancestor;
15468 int vertex;
15469};
15470
15471
15472static void unsdom_block(struct sdom_block *block)
15473{
15474 struct sdom_block **ptr;
15475 if (!block->sdom_next) {
15476 return;
15477 }
15478 ptr = &block->sdom->sdominates;
15479 while(*ptr) {
15480 if ((*ptr) == block) {
15481 *ptr = block->sdom_next;
15482 return;
15483 }
15484 ptr = &(*ptr)->sdom_next;
15485 }
15486}
15487
15488static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15489{
15490 unsdom_block(block);
15491 block->sdom = sdom;
15492 block->sdom_next = sdom->sdominates;
15493 sdom->sdominates = block;
15494}
15495
15496
15497
15498static int initialize_sdblock(struct sdom_block *sd,
15499 struct block *parent, struct block *block, int vertex)
15500{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015501 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015502 if (!block || (sd[block->vertex].block == block)) {
15503 return vertex;
15504 }
15505 vertex += 1;
15506 /* Renumber the blocks in a convinient fashion */
15507 block->vertex = vertex;
15508 sd[vertex].block = block;
15509 sd[vertex].sdom = &sd[vertex];
15510 sd[vertex].label = &sd[vertex];
15511 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15512 sd[vertex].ancestor = 0;
15513 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015514 for(edge = block->edges; edge; edge = edge->next) {
15515 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15516 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015517 return vertex;
15518}
15519
Eric Biederman83b991a2003-10-11 06:20:25 +000015520static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015521 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015522 struct block *parent, struct block *block, int vertex)
15523{
15524 struct block_set *user;
15525 if (!block || (sd[block->vertex].block == block)) {
15526 return vertex;
15527 }
15528 vertex += 1;
15529 /* Renumber the blocks in a convinient fashion */
15530 block->vertex = vertex;
15531 sd[vertex].block = block;
15532 sd[vertex].sdom = &sd[vertex];
15533 sd[vertex].label = &sd[vertex];
15534 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15535 sd[vertex].ancestor = 0;
15536 sd[vertex].vertex = vertex;
15537 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015538 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015539 }
15540 return vertex;
15541}
15542
Stefan Reinauer14e22772010-04-27 06:56:47 +000015543static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015544 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015545{
15546 struct block *block;
15547 int vertex;
15548 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015549 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015550
Eric Biederman5ade04a2003-10-22 04:03:46 +000015551 /* Walk through the graph and find unconnected blocks. Add a
15552 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015553 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015554 */
Eric Biederman90089602004-05-28 14:11:54 +000015555 block = bb->first_block->last->next->u.block;
15556 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015557 if (sd[block->vertex].block == block) {
15558 continue;
15559 }
Eric Biederman530b5192003-07-01 10:05:30 +000015560#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015561 {
15562 FILE *fp = state->errout;
15563 fprintf(fp, "Adding %d\n", vertex +1);
15564 }
Eric Biederman530b5192003-07-01 10:05:30 +000015565#endif
Eric Biederman90089602004-05-28 14:11:54 +000015566 add_block_edge(block, bb->last_block, 0);
15567 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015568
Eric Biederman90089602004-05-28 14:11:54 +000015569 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015570 }
15571 return vertex;
15572}
15573
15574static void compress_ancestors(struct sdom_block *v)
15575{
15576 /* This procedure assumes ancestor(v) != 0 */
15577 /* if (ancestor(ancestor(v)) != 0) {
15578 * compress(ancestor(ancestor(v)));
15579 * if (semi(label(ancestor(v))) < semi(label(v))) {
15580 * label(v) = label(ancestor(v));
15581 * }
15582 * ancestor(v) = ancestor(ancestor(v));
15583 * }
15584 */
15585 if (!v->ancestor) {
15586 return;
15587 }
15588 if (v->ancestor->ancestor) {
15589 compress_ancestors(v->ancestor->ancestor);
15590 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15591 v->label = v->ancestor->label;
15592 }
15593 v->ancestor = v->ancestor->ancestor;
15594 }
15595}
15596
Stefan Reinauer14e22772010-04-27 06:56:47 +000015597static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015598 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015599{
15600 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015601 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015602 * for each v <= pred(w) {
15603 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015604 * if (semi[u] < semi[w] {
15605 * semi[w] = semi[u];
15606 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015607 * }
15608 * add w to bucket(vertex(semi[w]));
15609 * LINK(parent(w), w);
15610 *
15611 * // step 3
15612 * for each v <= bucket(parent(w)) {
15613 * delete v from bucket(parent(w));
15614 * u = EVAL(v);
15615 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15616 * }
15617 */
Eric Biederman90089602004-05-28 14:11:54 +000015618 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015619 struct sdom_block *v, *parent, *next;
15620 struct block_set *user;
15621 struct block *block;
15622 block = sd[i].block;
15623 parent = sd[i].parent;
15624 /* Step 2 */
15625 for(user = block->use; user; user = user->next) {
15626 struct sdom_block *v, *u;
15627 v = &sd[user->member->vertex];
15628 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15629 if (u->sdom->vertex < sd[i].sdom->vertex) {
15630 sd[i].sdom = u->sdom;
15631 }
15632 }
15633 sdom_block(sd[i].sdom, &sd[i]);
15634 sd[i].ancestor = parent;
15635 /* Step 3 */
15636 for(v = parent->sdominates; v; v = next) {
15637 struct sdom_block *u;
15638 next = v->sdom_next;
15639 unsdom_block(v);
15640 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015641 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015642 u->block : parent->block;
15643 }
15644 }
15645}
15646
Stefan Reinauer14e22772010-04-27 06:56:47 +000015647static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015648 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015649{
15650 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015651 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015652 * for each v <= pred(w) {
15653 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015654 * if (semi[u] < semi[w] {
15655 * semi[w] = semi[u];
15656 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015657 * }
15658 * add w to bucket(vertex(semi[w]));
15659 * LINK(parent(w), w);
15660 *
15661 * // step 3
15662 * for each v <= bucket(parent(w)) {
15663 * delete v from bucket(parent(w));
15664 * u = EVAL(v);
15665 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15666 * }
15667 */
Eric Biederman90089602004-05-28 14:11:54 +000015668 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015669 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015670 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015671 struct block *block;
15672 block = sd[i].block;
15673 parent = sd[i].parent;
15674 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015675 for(edge = block->edges; edge; edge = edge->next) {
15676 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015677 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15678 if (u->sdom->vertex < sd[i].sdom->vertex) {
15679 sd[i].sdom = u->sdom;
15680 }
15681 }
15682 sdom_block(sd[i].sdom, &sd[i]);
15683 sd[i].ancestor = parent;
15684 /* Step 3 */
15685 for(v = parent->sdominates; v; v = next) {
15686 struct sdom_block *u;
15687 next = v->sdom_next;
15688 unsdom_block(v);
15689 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015690 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015691 u->block : parent->block;
15692 }
15693 }
15694}
15695
Stefan Reinauer14e22772010-04-27 06:56:47 +000015696static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015697 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015698{
15699 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015700 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015701 struct block *block;
15702 block = sd[i].block;
15703 if (block->idom->vertex != sd[i].sdom->vertex) {
15704 block->idom = block->idom->idom;
15705 }
15706 idom_block(block->idom, block);
15707 }
15708 sd[1].block->idom = 0;
15709}
15710
Stefan Reinauer14e22772010-04-27 06:56:47 +000015711static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015712 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015713{
15714 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015715 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015716 struct block *block;
15717 block = sd[i].block;
15718 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15719 block->ipdom = block->ipdom->ipdom;
15720 }
15721 ipdom_block(block->ipdom, block);
15722 }
15723 sd[1].block->ipdom = 0;
15724}
15725
15726 /* Theorem 1:
15727 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015728 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015729 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015730 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015731 * v dominates w if and only if v is a proper ancestor of w in
15732 * the dominator tree.
15733 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015734 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015735 * If v and w are vertices of G such that v <= w,
15736 * than any path from v to w must contain a common ancestor
15737 * of v and w in T.
15738 */
15739 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15740 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15741 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15742 /* Theorem 2:
15743 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15744 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15745 */
15746 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015747 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015748 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15749 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15750 */
15751 /* Lemma 5: Let vertices v,w satisfy v -> w.
15752 * Then v -> idom(w) or idom(w) -> idom(v)
15753 */
15754
Eric Biederman90089602004-05-28 14:11:54 +000015755static void find_immediate_dominators(struct compile_state *state,
15756 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015757{
15758 struct sdom_block *sd;
15759 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15760 * vi > w for (1 <= i <= k - 1}
15761 */
15762 /* Theorem 4:
15763 * For any vertex w != r.
15764 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015765 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015766 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15767 */
15768 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015769 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015770 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15771 * Then:
15772 * { sdom(w) if sdom(w) = sdom(u),
15773 * idom(w) = {
15774 * { idom(u) otherwise
15775 */
15776 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015777 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015778 * Number the vertices from 1 to N as they are reached during
15779 * the search. Initialize the variables used in succeeding steps.
15780 * Step 2. Compute the semidominators of all vertices by applying
15781 * theorem 4. Carry out the computation vertex by vertex in
15782 * decreasing order by number.
15783 * Step 3. Implicitly define the immediate dominator of each vertex
15784 * by applying Corollary 1.
15785 * Step 4. Explicitly define the immediate dominator of each vertex,
15786 * carrying out the computation vertex by vertex in increasing order
15787 * by number.
15788 */
15789 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015790 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15791 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015792#if 0
15793 sd[1].size = 0;
15794 sd[1].label = 0;
15795 sd[1].sdom = 0;
15796#endif
15797 /* Step 2 compute the semidominators */
15798 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015799 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015800 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015801 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015802 xfree(sd);
15803}
15804
Eric Biederman90089602004-05-28 14:11:54 +000015805static void find_post_dominators(struct compile_state *state,
15806 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015807{
15808 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015809 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015810 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015811 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015812
Eric Biederman90089602004-05-28 14:11:54 +000015813 vertex = setup_spdblocks(state, bb, sd);
15814 if (vertex != bb->last_vertex) {
15815 internal_error(state, 0, "missing %d blocks",
15816 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015817 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015818
15819 /* Step 2 compute the semidominators */
15820 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015821 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015822 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015823 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015824 xfree(sd);
15825}
15826
15827
15828
15829static void find_block_domf(struct compile_state *state, struct block *block)
15830{
15831 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015832 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015833 if (block->domfrontier != 0) {
15834 internal_error(state, block->first, "domfrontier present?");
15835 }
15836 for(user = block->idominates; user; user = user->next) {
15837 child = user->member;
15838 if (child->idom != block) {
15839 internal_error(state, block->first, "bad idom");
15840 }
15841 find_block_domf(state, child);
15842 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015843 for(edge = block->edges; edge; edge = edge->next) {
15844 if (edge->member->idom != block) {
15845 domf_block(block, edge->member);
15846 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015847 }
15848 for(user = block->idominates; user; user = user->next) {
15849 struct block_set *frontier;
15850 child = user->member;
15851 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15852 if (frontier->member->idom != block) {
15853 domf_block(block, frontier->member);
15854 }
15855 }
15856 }
15857}
15858
15859static void find_block_ipdomf(struct compile_state *state, struct block *block)
15860{
15861 struct block *child;
15862 struct block_set *user;
15863 if (block->ipdomfrontier != 0) {
15864 internal_error(state, block->first, "ipdomfrontier present?");
15865 }
15866 for(user = block->ipdominates; user; user = user->next) {
15867 child = user->member;
15868 if (child->ipdom != block) {
15869 internal_error(state, block->first, "bad ipdom");
15870 }
15871 find_block_ipdomf(state, child);
15872 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015873 for(user = block->use; user; user = user->next) {
15874 if (user->member->ipdom != block) {
15875 ipdomf_block(block, user->member);
15876 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015877 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015878 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015879 struct block_set *frontier;
15880 child = user->member;
15881 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15882 if (frontier->member->ipdom != block) {
15883 ipdomf_block(block, frontier->member);
15884 }
15885 }
15886 }
15887}
15888
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015889static void print_dominated(
15890 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015891{
15892 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015893 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015894
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015897 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015898 if (user->member->idom != block) {
15899 internal_error(state, user->member->first, "bad idom");
15900 }
15901 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015902 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015903}
15904
Eric Biederman5ade04a2003-10-22 04:03:46 +000015905static void print_dominated2(
15906 struct compile_state *state, FILE *fp, int depth, struct block *block)
15907{
15908 struct block_set *user;
15909 struct triple *ins;
15910 struct occurance *ptr, *ptr2;
15911 const char *filename1, *filename2;
15912 int equal_filenames;
15913 int i;
15914 for(i = 0; i < depth; i++) {
15915 fprintf(fp, " ");
15916 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015917 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015918 block->vertex, block, block->first, block->last);
15919 ins = block->first;
15920 while(ins != block->last && (ins->occurance->line == 0)) {
15921 ins = ins->next;
15922 }
15923 ptr = ins->occurance;
15924 ptr2 = block->last->occurance;
15925 filename1 = ptr->filename? ptr->filename : "";
15926 filename2 = ptr2->filename? ptr2->filename : "";
15927 equal_filenames = (strcmp(filename1, filename2) == 0);
15928 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15929 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15930 } else if (equal_filenames) {
15931 fprintf(fp, " %s:(%d - %d)",
15932 ptr->filename, ptr->line, ptr2->line);
15933 } else {
15934 fprintf(fp, " (%s:%d - %s:%d)",
15935 ptr->filename, ptr->line,
15936 ptr2->filename, ptr2->line);
15937 }
15938 fprintf(fp, "\n");
15939 for(user = block->idominates; user; user = user->next) {
15940 print_dominated2(state, fp, depth + 1, user->member);
15941 }
15942}
15943
Eric Biederman90089602004-05-28 14:11:54 +000015944static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015945{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015946 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015947 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015948 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015949 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015950}
15951
15952
15953static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015954 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015955{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015956 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015957
15958 if (!block || (block->vertex != vertex + 1)) {
15959 return vertex;
15960 }
15961 vertex += 1;
15962
Eric Biederman7dea9552004-06-29 05:38:37 +000015963 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015964 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015965 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015966 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015967 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015968
Eric Biederman5ade04a2003-10-22 04:03:46 +000015969 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015970 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015971 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015972 return vertex;
15973}
Eric Biederman90089602004-05-28 14:11:54 +000015974static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015975 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015976{
Eric Biederman7dea9552004-06-29 05:38:37 +000015977 fprintf(fp, "\ndominance frontiers\n");
15978 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015979
Eric Biedermanb138ac82003-04-22 18:44:01 +000015980}
15981
Eric Biederman90089602004-05-28 14:11:54 +000015982static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015983{
15984 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015985 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015987 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015988 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015989 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015990 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015991 print_dominance_frontiers(state, state->dbgout, bb);
15992 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015993 }
15994}
15995
15996
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015997static void print_ipdominated(
15998 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015999{
16000 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016001 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016002
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016005 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016006 if (user->member->ipdom != block) {
16007 internal_error(state, user->member->first, "bad ipdom");
16008 }
16009 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016010 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016011}
16012
Eric Biederman90089602004-05-28 14:11:54 +000016013static void print_ipdominators(struct compile_state *state, FILE *fp,
16014 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016015{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016016 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016017 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016018}
16019
16020static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016021 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016022{
16023 struct block_set *user;
16024
16025 if (!block || (block->vertex != vertex + 1)) {
16026 return vertex;
16027 }
16028 vertex += 1;
16029
Eric Biederman7dea9552004-06-29 05:38:37 +000016030 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016031 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016032 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016033 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016034 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016036 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016037 }
16038 return vertex;
16039}
Eric Biederman90089602004-05-28 14:11:54 +000016040static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016041 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016042{
Eric Biederman7dea9552004-06-29 05:38:37 +000016043 fprintf(fp, "\nipdominance frontiers\n");
16044 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016045
Eric Biedermanb138ac82003-04-22 18:44:01 +000016046}
16047
Eric Biederman90089602004-05-28 14:11:54 +000016048static void analyze_ipdominators(struct compile_state *state,
16049 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016050{
16051 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016052 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016053 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016054 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016055 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016056 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016057 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016058 print_ipdominance_frontiers(state, state->dbgout, bb);
16059 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016060 }
16061}
16062
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016063static int bdominates(struct compile_state *state,
16064 struct block *dom, struct block *sub)
16065{
16066 while(sub && (sub != dom)) {
16067 sub = sub->idom;
16068 }
16069 return sub == dom;
16070}
16071
16072static int tdominates(struct compile_state *state,
16073 struct triple *dom, struct triple *sub)
16074{
16075 struct block *bdom, *bsub;
16076 int result;
16077 bdom = block_of_triple(state, dom);
16078 bsub = block_of_triple(state, sub);
16079 if (bdom != bsub) {
16080 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016081 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016082 else {
16083 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016084 if (!bdom || !bsub) {
16085 internal_error(state, dom, "huh?");
16086 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016087 ins = sub;
16088 while((ins != bsub->first) && (ins != dom)) {
16089 ins = ins->prev;
16090 }
16091 result = (ins == dom);
16092 }
16093 return result;
16094}
16095
Eric Biederman90089602004-05-28 14:11:54 +000016096static void analyze_basic_blocks(
16097 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016098{
Eric Biederman90089602004-05-28 14:11:54 +000016099 setup_basic_blocks(state, bb);
16100 analyze_idominators(state, bb);
16101 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016102}
16103
Eric Biedermanb138ac82003-04-22 18:44:01 +000016104static void insert_phi_operations(struct compile_state *state)
16105{
16106 size_t size;
16107 struct triple *first;
16108 int *has_already, *work;
16109 struct block *work_list, **work_list_tail;
16110 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016111 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016112
Eric Biederman90089602004-05-28 14:11:54 +000016113 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016114 has_already = xcmalloc(size, "has_already");
16115 work = xcmalloc(size, "work");
16116 iter = 0;
16117
Eric Biederman83b991a2003-10-11 06:20:25 +000016118 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016119 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016120 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016121 struct triple_set *user, *unext;
16122 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016123
16124 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016125 continue;
16126 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016127
Eric Biedermanb138ac82003-04-22 18:44:01 +000016128 iter += 1;
16129 work_list = 0;
16130 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016131 for(user = var->use; user; user = unext) {
16132 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016133 if (MISC(var, 0) == user->member) {
16134 continue;
16135 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016136 if (user->member->op == OP_READ) {
16137 continue;
16138 }
16139 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016140 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016141 "bad variable access");
16142 }
16143 block = user->member->u.block;
16144 if (!block) {
16145 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016146 release_triple(state, user->member);
16147 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016148 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016149 if (work[block->vertex] >= iter) {
16150 continue;
16151 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016152 work[block->vertex] = iter;
16153 *work_list_tail = block;
16154 block->work_next = 0;
16155 work_list_tail = &block->work_next;
16156 }
16157 for(block = work_list; block; block = block->work_next) {
16158 struct block_set *df;
16159 for(df = block->domfrontier; df; df = df->next) {
16160 struct triple *phi;
16161 struct block *front;
16162 int in_edges;
16163 front = df->member;
16164
16165 if (has_already[front->vertex] >= iter) {
16166 continue;
16167 }
16168 /* Count how many edges flow into this block */
16169 in_edges = front->users;
16170 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016171 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016172 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016173 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016174 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016175 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016176 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016177 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016178#if 1
16179 if (phi->rhs != in_edges) {
16180 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16181 phi->rhs, in_edges);
16182 }
16183#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016184 /* Insert the phi functions immediately after the label */
16185 insert_triple(state, front->first->next, phi);
16186 if (front->first == front->last) {
16187 front->last = front->first->next;
16188 }
16189 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016190 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016191
Eric Biedermanb138ac82003-04-22 18:44:01 +000016192 /* If necessary plan to visit the basic block */
16193 if (work[front->vertex] >= iter) {
16194 continue;
16195 }
16196 work[front->vertex] = iter;
16197 *work_list_tail = front;
16198 front->work_next = 0;
16199 work_list_tail = &front->work_next;
16200 }
16201 }
16202 }
16203 xfree(has_already);
16204 xfree(work);
16205}
16206
Eric Biederman66fe2222003-07-04 15:14:04 +000016207
Eric Biederman83b991a2003-10-11 06:20:25 +000016208struct stack {
16209 struct triple_set *top;
16210 unsigned orig_id;
16211};
16212
Eric Biederman90089602004-05-28 14:11:54 +000016213static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016214{
16215 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016216 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016217 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016218 ins = first;
16219 do {
Eric Biederman90089602004-05-28 14:11:54 +000016220 if (triple_is_auto_var(state, ins)) {
16221 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016222 }
16223 ins = ins->next;
16224 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016225 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016226}
16227
Eric Biederman90089602004-05-28 14:11:54 +000016228static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016229{
16230 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016231 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016232 first = state->first;
16233 ins = first;
16234 do {
Eric Biederman90089602004-05-28 14:11:54 +000016235 if (triple_is_auto_var(state, ins)) {
16236 auto_vars += 1;
16237 stacks[auto_vars].orig_id = ins->id;
16238 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016239 }
16240 ins = ins->next;
16241 } while(ins != first);
16242}
16243
Eric Biederman90089602004-05-28 14:11:54 +000016244static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016245{
16246 struct triple *first, *ins;
16247 first = state->first;
16248 ins = first;
16249 do {
Eric Biederman90089602004-05-28 14:11:54 +000016250 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016251 ins->id = stacks[ins->id].orig_id;
16252 }
16253 ins = ins->next;
16254 } while(ins != first);
16255}
16256
16257static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016258{
16259 struct triple_set *head;
16260 struct triple *top_val;
16261 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016262 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016263 if (head) {
16264 top_val = head->member;
16265 }
16266 return top_val;
16267}
16268
Eric Biederman83b991a2003-10-11 06:20:25 +000016269static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016270{
16271 struct triple_set *new;
16272 /* Append new to the head of the list,
16273 * it's the only sensible behavoir for a stack.
16274 */
16275 new = xcmalloc(sizeof(*new), "triple_set");
16276 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016277 new->next = stacks[var->id].top;
16278 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016279}
16280
Eric Biederman83b991a2003-10-11 06:20:25 +000016281static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016282{
16283 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016284 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016285 while(*ptr) {
16286 set = *ptr;
16287 if (set->member == oldval) {
16288 *ptr = set->next;
16289 xfree(set);
16290 /* Only free one occurance from the stack */
16291 return;
16292 }
16293 else {
16294 ptr = &set->next;
16295 }
16296 }
16297}
16298
Eric Biedermanb138ac82003-04-22 18:44:01 +000016299/*
16300 * C(V)
16301 * S(V)
16302 */
16303static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016304 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016305{
16306 struct block_set *set;
16307 struct triple *ptr;
16308 int edge;
16309 if (!parent || !block)
16310 return;
16311 /* Find the edge I am coming in on */
16312 edge = 0;
16313 for(set = block->use; set; set = set->next, edge++) {
16314 if (set->member == parent) {
16315 break;
16316 }
16317 }
16318 if (!set) {
16319 internal_error(state, 0, "phi input is not on a control predecessor");
16320 }
16321 for(ptr = block->first; ; ptr = ptr->next) {
16322 if (ptr->op == OP_PHI) {
16323 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016324 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016325 if (!var) {
16326 internal_error(state, ptr, "no var???");
16327 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016328 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016329 val = peek_triple(stacks, var);
16330 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016331 internal_error(state, val, "bad value in phi");
16332 }
Eric Biederman90089602004-05-28 14:11:54 +000016333 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016334 internal_error(state, ptr, "edges > phi rhs");
16335 }
16336 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016337 if ((*slot != 0) && (*slot != val)) {
16338 internal_error(state, ptr, "phi already bound on this edge");
16339 }
16340 *slot = val;
16341 use_triple(val, ptr);
16342 }
16343 if (ptr == block->last) {
16344 break;
16345 }
16346 }
16347}
16348
16349
16350static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016351 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016352{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016353 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016354 struct triple *ptr, *next, *last;
16355 int done;
16356 if (!block)
16357 return;
16358 last = block->first;
16359 done = 0;
16360 for(ptr = block->first; !done; ptr = next) {
16361 next = ptr->next;
16362 if (ptr == block->last) {
16363 done = 1;
16364 }
16365 /* RHS(A) */
16366 if (ptr->op == OP_READ) {
16367 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016368 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016369 if (!triple_is_auto_var(state, var)) {
16370 internal_error(state, ptr, "read of non auto var!");
16371 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016372 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016373 /* Find the current value of the variable */
16374 val = peek_triple(stacks, var);
16375 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016376 /* Let the optimizer at variables that are not initially
16377 * set. But give it a bogus value so things seem to
16378 * work by accident. This is useful for bitfields because
16379 * setting them always involves a read-modify-write.
16380 */
16381 if (TYPE_ARITHMETIC(ptr->type->type)) {
16382 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16383 val->u.cval = 0xdeadbeaf;
16384 } else {
16385 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16386 }
16387 }
16388 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016389 error(state, ptr, "variable used without being set");
16390 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016391 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16392 internal_error(state, val, "bad value in read");
16393 }
16394 propogate_use(state, ptr, val);
16395 release_triple(state, ptr);
16396 continue;
16397 }
16398 /* LHS(A) */
16399 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016400 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016401 var = MISC(ptr, 0);
16402 if (!triple_is_auto_var(state, var)) {
16403 internal_error(state, ptr, "write to non auto var!");
16404 }
16405 tval = val = RHS(ptr, 0);
16406 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16407 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016408 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016409 }
Eric Biederman90089602004-05-28 14:11:54 +000016410 /* Insert a cast if the types differ */
16411 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016412 if (val->op == OP_INTCONST) {
16413 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16414 tval->u.cval = val->u.cval;
16415 }
16416 else {
Eric Biederman90089602004-05-28 14:11:54 +000016417 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016418 use_triple(val, tval);
16419 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016420 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016421 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016422 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016423 use_triple(tval, ptr);
16424 }
16425 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016426 unuse_triple(var, ptr);
16427 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016428 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016429 }
16430 if (ptr->op == OP_PHI) {
16431 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016432 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016433 if (!triple_is_auto_var(state, var)) {
16434 internal_error(state, ptr, "phi references non auto var!");
16435 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016436 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016437 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016438 }
16439 last = ptr;
16440 }
16441 block->last = last;
16442
16443 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016444 for(edge = block->edges; edge; edge = edge->next) {
16445 fixup_block_phi_variables(state, stacks, block, edge->member);
16446 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016447 /* rename variables in the dominated nodes */
16448 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016449 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016450 }
16451 /* pop the renamed variable stack */
16452 last = block->first;
16453 done = 0;
16454 for(ptr = block->first; !done ; ptr = next) {
16455 next = ptr->next;
16456 if (ptr == block->last) {
16457 done = 1;
16458 }
16459 if (ptr->op == OP_WRITE) {
16460 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016461 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016462 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016463 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016464 release_triple(state, ptr);
16465 continue;
16466 }
16467 if (ptr->op == OP_PHI) {
16468 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016469 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016470 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016471 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016472 }
16473 last = ptr;
16474 }
16475 block->last = last;
16476}
16477
Eric Biederman83b991a2003-10-11 06:20:25 +000016478static void rename_variables(struct compile_state *state)
16479{
16480 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016481 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016482
16483 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016484 auto_vars = count_auto_vars(state);
16485 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016486
Eric Biederman90089602004-05-28 14:11:54 +000016487 /* Give each auto_var a stack */
16488 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016489
16490 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016491 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016492
Eric Biederman90089602004-05-28 14:11:54 +000016493 /* Remove the stacks from the auto_vars */
16494 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016495 xfree(stacks);
16496}
16497
Eric Biedermanb138ac82003-04-22 18:44:01 +000016498static void prune_block_variables(struct compile_state *state,
16499 struct block *block)
16500{
16501 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016502 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016503 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016504
Eric Biedermanb138ac82003-04-22 18:44:01 +000016505 done = 0;
16506 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016507 /* Be extremely careful I am deleting the list
16508 * as I walk trhough it.
16509 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016510 next = ptr->next;
16511 if (ptr == block->last) {
16512 done = 1;
16513 }
Eric Biederman90089602004-05-28 14:11:54 +000016514 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016515 struct triple_set *user, *next;
16516 for(user = ptr->use; user; user = next) {
16517 struct triple *use;
16518 next = user->next;
16519 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016520 if (MISC(ptr, 0) == user->member) {
16521 continue;
16522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016523 if (use->op != OP_PHI) {
16524 internal_error(state, use, "decl still used");
16525 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016526 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016527 internal_error(state, use, "bad phi use of decl");
16528 }
16529 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016530 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016531 }
Eric Biederman90089602004-05-28 14:11:54 +000016532 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16533 /* Delete the adecl */
16534 release_triple(state, MISC(ptr, 0));
16535 /* And the piece */
16536 release_triple(state, ptr);
16537 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016538 continue;
16539 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016541 for(user = block->idominates; user; user = user->next) {
16542 prune_block_variables(state, user->member);
16543 }
16544}
16545
Eric Biederman66fe2222003-07-04 15:14:04 +000016546struct phi_triple {
16547 struct triple *phi;
16548 unsigned orig_id;
16549 int alive;
16550};
16551
16552static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16553{
16554 struct triple **slot;
16555 int zrhs, i;
16556 if (live[phi->id].alive) {
16557 return;
16558 }
16559 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016560 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016561 slot = &RHS(phi, 0);
16562 for(i = 0; i < zrhs; i++) {
16563 struct triple *used;
16564 used = slot[i];
16565 if (used && (used->op == OP_PHI)) {
16566 keep_phi(state, live, used);
16567 }
16568 }
16569}
16570
16571static void prune_unused_phis(struct compile_state *state)
16572{
16573 struct triple *first, *phi;
16574 struct phi_triple *live;
16575 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016576
Eric Biederman66fe2222003-07-04 15:14:04 +000016577 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016578 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016579
16580 /* Count how many phi functions I need to process */
16581 phis = 0;
16582 for(phi = first->next; phi != first; phi = phi->next) {
16583 if (phi->op == OP_PHI) {
16584 phis += 1;
16585 }
16586 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016587
Eric Biederman66fe2222003-07-04 15:14:04 +000016588 /* Mark them all dead */
16589 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16590 phis = 0;
16591 for(phi = first->next; phi != first; phi = phi->next) {
16592 if (phi->op != OP_PHI) {
16593 continue;
16594 }
16595 live[phis].alive = 0;
16596 live[phis].orig_id = phi->id;
16597 live[phis].phi = phi;
16598 phi->id = phis;
16599 phis += 1;
16600 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016601
Eric Biederman66fe2222003-07-04 15:14:04 +000016602 /* Mark phis alive that are used by non phis */
16603 for(i = 0; i < phis; i++) {
16604 struct triple_set *set;
16605 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16606 if (set->member->op != OP_PHI) {
16607 keep_phi(state, live, live[i].phi);
16608 break;
16609 }
16610 }
16611 }
16612
16613 /* Delete the extraneous phis */
16614 for(i = 0; i < phis; i++) {
16615 struct triple **slot;
16616 int zrhs, j;
16617 if (!live[i].alive) {
16618 release_triple(state, live[i].phi);
16619 continue;
16620 }
16621 phi = live[i].phi;
16622 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016623 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016624 for(j = 0; j < zrhs; j++) {
16625 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016626 struct triple *unknown;
16627 get_occurance(phi->occurance);
16628 unknown = flatten(state, state->global_pool,
16629 alloc_triple(state, OP_UNKNOWNVAL,
16630 phi->type, 0, 0, phi->occurance));
16631 slot[j] = unknown;
16632 use_triple(unknown, phi);
16633 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016634#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016635 warning(state, phi, "variable not set at index %d on all paths to use", j);
16636#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016637 }
16638 }
16639 }
16640 xfree(live);
16641}
16642
Eric Biedermanb138ac82003-04-22 18:44:01 +000016643static void transform_to_ssa_form(struct compile_state *state)
16644{
16645 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016646 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016647
Eric Biederman90089602004-05-28 14:11:54 +000016648 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016649 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016650
Eric Biederman90089602004-05-28 14:11:54 +000016651 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016652}
16653
16654
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016655static void clear_vertex(
16656 struct compile_state *state, struct block *block, void *arg)
16657{
Eric Biederman83b991a2003-10-11 06:20:25 +000016658 /* Clear the current blocks vertex and the vertex of all
16659 * of the current blocks neighbors in case there are malformed
16660 * blocks with now instructions at this point.
16661 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016662 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016663 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016664 for(edge = block->edges; edge; edge = edge->next) {
16665 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016666 }
16667 for(user = block->use; user; user = user->next) {
16668 user->member->vertex = 0;
16669 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016670}
16671
16672static void mark_live_block(
16673 struct compile_state *state, struct block *block, int *next_vertex)
16674{
16675 /* See if this is a block that has not been marked */
16676 if (block->vertex != 0) {
16677 return;
16678 }
16679 block->vertex = *next_vertex;
16680 *next_vertex += 1;
16681 if (triple_is_branch(state, block->last)) {
16682 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016683 targ = triple_edge_targ(state, block->last, 0);
16684 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016685 if (!*targ) {
16686 continue;
16687 }
16688 if (!triple_stores_block(state, *targ)) {
16689 internal_error(state, 0, "bad targ");
16690 }
16691 mark_live_block(state, (*targ)->u.block, next_vertex);
16692 }
Eric Biederman90089602004-05-28 14:11:54 +000016693 /* Ensure the last block of a function remains alive */
16694 if (triple_is_call(state, block->last)) {
16695 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16696 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016697 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016698 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016699 struct triple *ins;
16700 ins = block->last->next;
16701 if (!triple_stores_block(state, ins)) {
16702 internal_error(state, 0, "bad block start");
16703 }
16704 mark_live_block(state, ins->u.block, next_vertex);
16705 }
16706}
16707
Eric Biedermanb138ac82003-04-22 18:44:01 +000016708static void transform_from_ssa_form(struct compile_state *state)
16709{
16710 /* To get out of ssa form we insert moves on the incoming
16711 * edges to blocks containting phi functions.
16712 */
16713 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016714 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016715 int next_vertex;
16716
16717 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016718 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016719 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016720 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016721
16722 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016723 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016724 for(phi = first->next; phi != first ; phi = next) {
16725 struct block_set *set;
16726 struct block *block;
16727 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016728 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016729 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016730 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016731 next = phi->next;
16732 if (phi->op != OP_PHI) {
16733 continue;
16734 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016735
Eric Biedermanb138ac82003-04-22 18:44:01 +000016736 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016737 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016738
Eric Biederman83b991a2003-10-11 06:20:25 +000016739 /* If this phi is in a dead block just forget it */
16740 if (block->vertex == 0) {
16741 release_triple(state, phi);
16742 continue;
16743 }
16744
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016745 /* Forget uses from code in dead blocks */
16746 for(use = phi->use; use; use = use_next) {
16747 struct block *ublock;
16748 struct triple **expr;
16749 use_next = use->next;
16750 ublock = block_of_triple(state, use->member);
16751 if ((use->member == phi) || (ublock->vertex != 0)) {
16752 continue;
16753 }
16754 expr = triple_rhs(state, use->member, 0);
16755 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16756 if (*expr == phi) {
16757 *expr = 0;
16758 }
16759 }
16760 unuse_triple(phi, use->member);
16761 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016762 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016763 if (registers_of(state, phi->type) != 1) {
16764 internal_error(state, phi, "phi->type does not fit in a single register!");
16765 }
16766 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16767 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016768
Eric Biederman83b991a2003-10-11 06:20:25 +000016769 /* Replaces use of phi with var */
16770 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016771
Eric Biederman90089602004-05-28 14:11:54 +000016772 /* Count the readers */
16773 readers = 0;
16774 for(use = var->use; use; use = use->next) {
16775 if (use->member != MISC(var, 0)) {
16776 readers++;
16777 }
16778 }
16779
Eric Biedermanb138ac82003-04-22 18:44:01 +000016780 /* Walk all of the incoming edges/blocks and insert moves.
16781 */
Eric Biederman90089602004-05-28 14:11:54 +000016782 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016783 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016784 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016785 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016786 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016787 eblock = set->member;
16788 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016789 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016790 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016791 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016792
Eric Biederman83b991a2003-10-11 06:20:25 +000016793 /* If we don't have a value that belongs in an OP_WRITE
16794 * continue on.
16795 */
Eric Biederman90089602004-05-28 14:11:54 +000016796 if (!val || (val == &unknown_triple) || (val == phi)
16797 || (vblock && (vblock->vertex == 0))) {
16798 continue;
16799 }
16800 /* If the value should never occur error */
16801 if (!vblock) {
16802 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016803 continue;
16804 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016805
16806 /* If the value occurs in a dead block see if a replacement
16807 * block can be found.
16808 */
16809 while(eblock && (eblock->vertex == 0)) {
16810 eblock = eblock->idom;
16811 }
16812 /* If not continue on with the next value. */
16813 if (!eblock || (eblock->vertex == 0)) {
16814 continue;
16815 }
16816
16817 /* If we have an empty incoming block ignore it. */
16818 if (!eblock->first) {
16819 internal_error(state, 0, "empty block?");
16820 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016821
Eric Biederman530b5192003-07-01 10:05:30 +000016822 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016823 /* Walk through the edge block backwards to find an
16824 * appropriate location for the OP_WRITE.
16825 */
16826 for(base = eblock->last; base != eblock->first; base = base->prev) {
16827 struct triple **expr;
16828 if (base->op == OP_PIECE) {
16829 base = MISC(base, 0);
16830 }
16831 if ((base == var) || (base == val)) {
16832 goto out;
16833 }
16834 expr = triple_lhs(state, base, 0);
16835 for(; expr; expr = triple_lhs(state, base, expr)) {
16836 if ((*expr) == val) {
16837 goto out;
16838 }
16839 }
16840 expr = triple_rhs(state, base, 0);
16841 for(; expr; expr = triple_rhs(state, base, expr)) {
16842 if ((*expr) == var) {
16843 goto out;
16844 }
16845 }
Eric Biederman530b5192003-07-01 10:05:30 +000016846 }
Eric Biederman90089602004-05-28 14:11:54 +000016847 out:
16848 if (triple_is_branch(state, base)) {
16849 internal_error(state, base,
16850 "Could not insert write to phi");
16851 }
16852 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016853 use_triple(val, move);
16854 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016855 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016856 }
Eric Biederman90089602004-05-28 14:11:54 +000016857 if (!writers && readers) {
16858 internal_error(state, var, "no value written to in use phi?");
16859 }
16860 /* If var is not used free it */
16861 if (!writers) {
16862 release_triple(state, MISC(var, 0));
16863 release_triple(state, var);
16864 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016865 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016866 release_triple(state, phi);
16867 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016868
Eric Biederman83b991a2003-10-11 06:20:25 +000016869 /* Walk all of the operations to find the adecls */
16870 for(var = first->next; var != first ; var = var->next) {
16871 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016872 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016873 continue;
16874 }
16875
16876 /* Walk through all of the rhs uses of var and
16877 * replace them with read of var.
16878 */
16879 for(use = var->use; use; use = use_next) {
16880 struct triple *read, *user;
16881 struct triple **slot;
16882 int zrhs, i, used;
16883 use_next = use->next;
16884 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016885
Eric Biederman83b991a2003-10-11 06:20:25 +000016886 /* Generate a read of var */
16887 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16888 use_triple(var, read);
16889
16890 /* Find the rhs uses and see if they need to be replaced */
16891 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016892 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016893 slot = &RHS(user, 0);
16894 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016895 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016896 slot[i] = read;
16897 used = 1;
16898 }
16899 }
16900 /* If we did use it cleanup the uses */
16901 if (used) {
16902 unuse_triple(var, user);
16903 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016904 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016905 /* If we didn't use it release the extra triple */
16906 else {
16907 release_triple(state, read);
16908 }
16909 }
16910 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016911}
16912
Eric Biederman5ade04a2003-10-22 04:03:46 +000016913#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016914 FILE *fp = state->dbgout; \
16915 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016916 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016917
Eric Biederman83b991a2003-10-11 06:20:25 +000016918static void rebuild_ssa_form(struct compile_state *state)
16919{
16920HI();
16921 transform_from_ssa_form(state);
16922HI();
Eric Biederman90089602004-05-28 14:11:54 +000016923 state->bb.first = state->first;
16924 free_basic_blocks(state, &state->bb);
16925 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016926HI();
16927 insert_phi_operations(state);
16928HI();
16929 rename_variables(state);
16930HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016931
Eric Biederman90089602004-05-28 14:11:54 +000016932 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016933HI();
16934 prune_unused_phis(state);
16935HI();
16936}
16937#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016938
Stefan Reinauer14e22772010-04-27 06:56:47 +000016939/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016940 * Register conflict resolution
16941 * =========================================================
16942 */
16943
16944static struct reg_info find_def_color(
16945 struct compile_state *state, struct triple *def)
16946{
16947 struct triple_set *set;
16948 struct reg_info info;
16949 info.reg = REG_UNSET;
16950 info.regcm = 0;
16951 if (!triple_is_def(state, def)) {
16952 return info;
16953 }
16954 info = arch_reg_lhs(state, def, 0);
16955 if (info.reg >= MAX_REGISTERS) {
16956 info.reg = REG_UNSET;
16957 }
16958 for(set = def->use; set; set = set->next) {
16959 struct reg_info tinfo;
16960 int i;
16961 i = find_rhs_use(state, set->member, def);
16962 if (i < 0) {
16963 continue;
16964 }
16965 tinfo = arch_reg_rhs(state, set->member, i);
16966 if (tinfo.reg >= MAX_REGISTERS) {
16967 tinfo.reg = REG_UNSET;
16968 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016969 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016970 (info.reg != REG_UNSET) &&
16971 (tinfo.reg != info.reg)) {
16972 internal_error(state, def, "register conflict");
16973 }
16974 if ((info.regcm & tinfo.regcm) == 0) {
16975 internal_error(state, def, "regcm conflict %x & %x == 0",
16976 info.regcm, tinfo.regcm);
16977 }
16978 if (info.reg == REG_UNSET) {
16979 info.reg = tinfo.reg;
16980 }
16981 info.regcm &= tinfo.regcm;
16982 }
16983 if (info.reg >= MAX_REGISTERS) {
16984 internal_error(state, def, "register out of range");
16985 }
16986 return info;
16987}
16988
16989static struct reg_info find_lhs_pre_color(
16990 struct compile_state *state, struct triple *ins, int index)
16991{
16992 struct reg_info info;
16993 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016994 zrhs = ins->rhs;
16995 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016996 if (!zlhs && triple_is_def(state, ins)) {
16997 zlhs = 1;
16998 }
16999 if (index >= zlhs) {
17000 internal_error(state, ins, "Bad lhs %d", index);
17001 }
17002 info = arch_reg_lhs(state, ins, index);
17003 for(i = 0; i < zrhs; i++) {
17004 struct reg_info rinfo;
17005 rinfo = arch_reg_rhs(state, ins, i);
17006 if ((info.reg == rinfo.reg) &&
17007 (rinfo.reg >= MAX_REGISTERS)) {
17008 struct reg_info tinfo;
17009 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17010 info.reg = tinfo.reg;
17011 info.regcm &= tinfo.regcm;
17012 break;
17013 }
17014 }
17015 if (info.reg >= MAX_REGISTERS) {
17016 info.reg = REG_UNSET;
17017 }
17018 return info;
17019}
17020
17021static struct reg_info find_rhs_post_color(
17022 struct compile_state *state, struct triple *ins, int index);
17023
17024static struct reg_info find_lhs_post_color(
17025 struct compile_state *state, struct triple *ins, int index)
17026{
17027 struct triple_set *set;
17028 struct reg_info info;
17029 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017030#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017031 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017032 ins, index);
17033#endif
17034 if ((index == 0) && triple_is_def(state, ins)) {
17035 lhs = ins;
17036 }
Eric Biederman90089602004-05-28 14:11:54 +000017037 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017038 lhs = LHS(ins, index);
17039 }
17040 else {
17041 internal_error(state, ins, "Bad lhs %d", index);
17042 lhs = 0;
17043 }
17044 info = arch_reg_lhs(state, ins, index);
17045 if (info.reg >= MAX_REGISTERS) {
17046 info.reg = REG_UNSET;
17047 }
17048 for(set = lhs->use; set; set = set->next) {
17049 struct reg_info rinfo;
17050 struct triple *user;
17051 int zrhs, i;
17052 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017053 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017054 for(i = 0; i < zrhs; i++) {
17055 if (RHS(user, i) != lhs) {
17056 continue;
17057 }
17058 rinfo = find_rhs_post_color(state, user, i);
17059 if ((info.reg != REG_UNSET) &&
17060 (rinfo.reg != REG_UNSET) &&
17061 (info.reg != rinfo.reg)) {
17062 internal_error(state, ins, "register conflict");
17063 }
17064 if ((info.regcm & rinfo.regcm) == 0) {
17065 internal_error(state, ins, "regcm conflict %x & %x == 0",
17066 info.regcm, rinfo.regcm);
17067 }
17068 if (info.reg == REG_UNSET) {
17069 info.reg = rinfo.reg;
17070 }
17071 info.regcm &= rinfo.regcm;
17072 }
17073 }
Eric Biederman530b5192003-07-01 10:05:30 +000017074#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017075 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017076 ins, index, info.reg, info.regcm);
17077#endif
17078 return info;
17079}
17080
17081static struct reg_info find_rhs_post_color(
17082 struct compile_state *state, struct triple *ins, int index)
17083{
17084 struct reg_info info, rinfo;
17085 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017086#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017087 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017088 ins, index);
17089#endif
17090 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017091 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017092 if (!zlhs && triple_is_def(state, ins)) {
17093 zlhs = 1;
17094 }
17095 info = rinfo;
17096 if (info.reg >= MAX_REGISTERS) {
17097 info.reg = REG_UNSET;
17098 }
17099 for(i = 0; i < zlhs; i++) {
17100 struct reg_info linfo;
17101 linfo = arch_reg_lhs(state, ins, i);
17102 if ((linfo.reg == rinfo.reg) &&
17103 (linfo.reg >= MAX_REGISTERS)) {
17104 struct reg_info tinfo;
17105 tinfo = find_lhs_post_color(state, ins, i);
17106 if (tinfo.reg >= MAX_REGISTERS) {
17107 tinfo.reg = REG_UNSET;
17108 }
Eric Biederman530b5192003-07-01 10:05:30 +000017109 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017110 info.regcm &= tinfo.regcm;
17111 if (info.reg != REG_UNSET) {
17112 internal_error(state, ins, "register conflict");
17113 }
17114 if (info.regcm == 0) {
17115 internal_error(state, ins, "regcm conflict");
17116 }
17117 info.reg = tinfo.reg;
17118 }
17119 }
Eric Biederman530b5192003-07-01 10:05:30 +000017120#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017121 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017122 ins, index, info.reg, info.regcm);
17123#endif
17124 return info;
17125}
17126
17127static struct reg_info find_lhs_color(
17128 struct compile_state *state, struct triple *ins, int index)
17129{
17130 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017131#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017132 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017133 ins, index);
17134#endif
17135 pre = find_lhs_pre_color(state, ins, index);
17136 post = find_lhs_post_color(state, ins, index);
17137 if ((pre.reg != post.reg) &&
17138 (pre.reg != REG_UNSET) &&
17139 (post.reg != REG_UNSET)) {
17140 internal_error(state, ins, "register conflict");
17141 }
17142 info.regcm = pre.regcm & post.regcm;
17143 info.reg = pre.reg;
17144 if (info.reg == REG_UNSET) {
17145 info.reg = post.reg;
17146 }
Eric Biederman530b5192003-07-01 10:05:30 +000017147#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017148 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017149 ins, index, info.reg, info.regcm,
17150 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017151#endif
17152 return info;
17153}
17154
17155static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17156{
17157 struct triple_set *entry, *next;
17158 struct triple *out;
17159 struct reg_info info, rinfo;
17160
17161 info = arch_reg_lhs(state, ins, 0);
17162 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17163 use_triple(RHS(out, 0), out);
17164 /* Get the users of ins to use out instead */
17165 for(entry = ins->use; entry; entry = next) {
17166 int i;
17167 next = entry->next;
17168 if (entry->member == out) {
17169 continue;
17170 }
17171 i = find_rhs_use(state, entry->member, ins);
17172 if (i < 0) {
17173 continue;
17174 }
17175 rinfo = arch_reg_rhs(state, entry->member, i);
17176 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17177 continue;
17178 }
17179 replace_rhs_use(state, ins, out, entry->member);
17180 }
17181 transform_to_arch_instruction(state, out);
17182 return out;
17183}
17184
Eric Biedermand1ea5392003-06-28 06:49:45 +000017185static struct triple *typed_pre_copy(
17186 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017187{
17188 /* Carefully insert enough operations so that I can
17189 * enter any operation with a GPR32.
17190 */
17191 struct triple *in;
17192 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017193 unsigned classes;
17194 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017195 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017196 if (ins->op == OP_PHI) {
17197 internal_error(state, ins, "pre_copy on a phi?");
17198 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017199 classes = arch_type_to_regcm(state, type);
17200 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017201 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017202 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017203 FILE *fp = state->errout;
17204 fprintf(fp, "src_type: ");
17205 name_of(fp, ins->type);
17206 fprintf(fp, "\ndst_type: ");
17207 name_of(fp, type);
17208 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017209 internal_error(state, ins, "pre_copy with no register classes");
17210 }
Eric Biederman90089602004-05-28 14:11:54 +000017211 op = OP_COPY;
17212 if (!equiv_types(type, (*expr)->type)) {
17213 op = OP_CONVERT;
17214 }
17215 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017216 unuse_triple(*expr, ins);
17217 *expr = in;
17218 use_triple(RHS(in, 0), in);
17219 use_triple(in, ins);
17220 transform_to_arch_instruction(state, in);
17221 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017222
Eric Biedermand1ea5392003-06-28 06:49:45 +000017223}
17224static struct triple *pre_copy(
17225 struct compile_state *state, struct triple *ins, int index)
17226{
17227 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017228}
17229
17230
Eric Biedermanb138ac82003-04-22 18:44:01 +000017231static void insert_copies_to_phi(struct compile_state *state)
17232{
17233 /* To get out of ssa form we insert moves on the incoming
17234 * edges to blocks containting phi functions.
17235 */
17236 struct triple *first;
17237 struct triple *phi;
17238
17239 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017240 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017241 for(phi = first->next; phi != first ; phi = phi->next) {
17242 struct block_set *set;
17243 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017244 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017245 int edge;
17246 if (phi->op != OP_PHI) {
17247 continue;
17248 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017249 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017250 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017251 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017252 /* Phi's that feed into mandatory live range joins
17253 * cause nasty complications. Insert a copy of
17254 * the phi value so I never have to deal with
17255 * that in the rest of the code.
17256 */
17257 copy = post_copy(state, phi);
17258 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017259 /* Walk all of the incoming edges/blocks and insert moves.
17260 */
17261 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17262 struct block *eblock;
17263 struct triple *move;
17264 struct triple *val;
17265 struct triple *ptr;
17266 eblock = set->member;
17267 val = slot[edge];
17268
17269 if (val == phi) {
17270 continue;
17271 }
17272
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017273 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017274 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017275 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017276 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017277 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017278 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017279
Eric Biedermanb138ac82003-04-22 18:44:01 +000017280 slot[edge] = move;
17281 unuse_triple(val, phi);
17282 use_triple(move, phi);
17283
Eric Biederman66fe2222003-07-04 15:14:04 +000017284 /* Walk up the dominator tree until I have found the appropriate block */
17285 while(eblock && !tdominates(state, val, eblock->last)) {
17286 eblock = eblock->idom;
17287 }
17288 if (!eblock) {
17289 internal_error(state, phi, "Cannot find block dominated by %p",
17290 val);
17291 }
17292
Eric Biedermanb138ac82003-04-22 18:44:01 +000017293 /* Walk through the block backwards to find
17294 * an appropriate location for the OP_COPY.
17295 */
17296 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17297 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017298 if (ptr->op == OP_PIECE) {
17299 ptr = MISC(ptr, 0);
17300 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017301 if ((ptr == phi) || (ptr == val)) {
17302 goto out;
17303 }
Eric Biederman90089602004-05-28 14:11:54 +000017304 expr = triple_lhs(state, ptr, 0);
17305 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17306 if ((*expr) == val) {
17307 goto out;
17308 }
17309 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017310 expr = triple_rhs(state, ptr, 0);
17311 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17312 if ((*expr) == phi) {
17313 goto out;
17314 }
17315 }
17316 }
17317 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017318 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017319 internal_error(state, ptr,
17320 "Could not insert write to phi");
17321 }
Eric Biederman90089602004-05-28 14:11:54 +000017322 insert_triple(state, after_lhs(state, ptr), move);
17323 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017324 eblock->last = move;
17325 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017326 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017327 }
17328 }
Eric Biederman90089602004-05-28 14:11:54 +000017329 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017330}
17331
Eric Biederman90089602004-05-28 14:11:54 +000017332struct triple_reg_set;
17333struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017334
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335
Stefan Reinauer14e22772010-04-27 06:56:47 +000017336static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017337 struct triple *member, struct triple *new_member)
17338{
17339 struct triple_reg_set **ptr, *new;
17340 if (!member)
17341 return 0;
17342 ptr = head;
17343 while(*ptr) {
17344 if ((*ptr)->member == member) {
17345 return 0;
17346 }
17347 ptr = &(*ptr)->next;
17348 }
17349 new = xcmalloc(sizeof(*new), "triple_set");
17350 new->member = member;
17351 new->new = new_member;
17352 new->next = *head;
17353 *head = new;
17354 return 1;
17355}
17356
17357static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17358{
17359 struct triple_reg_set *entry, **ptr;
17360 ptr = head;
17361 while(*ptr) {
17362 entry = *ptr;
17363 if (entry->member == member) {
17364 *ptr = entry->next;
17365 xfree(entry);
17366 return;
17367 }
17368 else {
17369 ptr = &entry->next;
17370 }
17371 }
17372}
17373
17374static int in_triple(struct reg_block *rb, struct triple *in)
17375{
17376 return do_triple_set(&rb->in, in, 0);
17377}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017378
17379#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017380static void unin_triple(struct reg_block *rb, struct triple *unin)
17381{
17382 do_triple_unset(&rb->in, unin);
17383}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017384#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017385
17386static int out_triple(struct reg_block *rb, struct triple *out)
17387{
17388 return do_triple_set(&rb->out, out, 0);
17389}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017390#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017391static void unout_triple(struct reg_block *rb, struct triple *unout)
17392{
17393 do_triple_unset(&rb->out, unout);
17394}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017395#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017396
17397static int initialize_regblock(struct reg_block *blocks,
17398 struct block *block, int vertex)
17399{
17400 struct block_set *user;
17401 if (!block || (blocks[block->vertex].block == block)) {
17402 return vertex;
17403 }
17404 vertex += 1;
17405 /* Renumber the blocks in a convinient fashion */
17406 block->vertex = vertex;
17407 blocks[vertex].block = block;
17408 blocks[vertex].vertex = vertex;
17409 for(user = block->use; user; user = user->next) {
17410 vertex = initialize_regblock(blocks, user->member, vertex);
17411 }
17412 return vertex;
17413}
17414
Eric Biederman90089602004-05-28 14:11:54 +000017415static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17416{
17417/* Part to piece is a best attempt and it cannot be correct all by
17418 * itself. If various values are read as different sizes in different
17419 * parts of the code this function cannot work. Or rather it cannot
17420 * work in conjunction with compute_variable_liftimes. As the
17421 * analysis will get confused.
17422 */
17423 struct triple *base;
17424 unsigned reg;
17425 if (!is_lvalue(state, ins)) {
17426 return ins;
17427 }
17428 base = 0;
17429 reg = 0;
17430 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17431 base = MISC(ins, 0);
17432 switch(ins->op) {
17433 case OP_INDEX:
17434 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17435 break;
17436 case OP_DOT:
17437 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17438 break;
17439 default:
17440 internal_error(state, ins, "unhandled part");
17441 break;
17442 }
17443 ins = base;
17444 }
17445 if (base) {
17446 if (reg > base->lhs) {
17447 internal_error(state, base, "part out of range?");
17448 }
17449 ins = LHS(base, reg);
17450 }
17451 return ins;
17452}
17453
Stefan Reinauer14e22772010-04-27 06:56:47 +000017454static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017455 struct triple *ins, struct triple *other)
17456{
17457 if (ins == other) {
17458 return 1;
17459 }
17460 if (ins->op == OP_WRITE) {
17461 ins = part_to_piece(state, MISC(ins, 0));
17462 }
17463 return ins == other;
17464}
17465
Eric Biedermanb138ac82003-04-22 18:44:01 +000017466static int phi_in(struct compile_state *state, struct reg_block *blocks,
17467 struct reg_block *rb, struct block *suc)
17468{
17469 /* Read the conditional input set of a successor block
17470 * (i.e. the input to the phi nodes) and place it in the
17471 * current blocks output set.
17472 */
17473 struct block_set *set;
17474 struct triple *ptr;
17475 int edge;
17476 int done, change;
17477 change = 0;
17478 /* Find the edge I am coming in on */
17479 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17480 if (set->member == rb->block) {
17481 break;
17482 }
17483 }
17484 if (!set) {
17485 internal_error(state, 0, "Not coming on a control edge?");
17486 }
17487 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17488 struct triple **slot, *expr, *ptr2;
17489 int out_change, done2;
17490 done = (ptr == suc->last);
17491 if (ptr->op != OP_PHI) {
17492 continue;
17493 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017494 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017495 expr = slot[edge];
17496 out_change = out_triple(rb, expr);
17497 if (!out_change) {
17498 continue;
17499 }
17500 /* If we don't define the variable also plast it
17501 * in the current blocks input set.
17502 */
17503 ptr2 = rb->block->first;
17504 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017505 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017506 break;
17507 }
17508 done2 = (ptr2 == rb->block->last);
17509 }
17510 if (!done2) {
17511 continue;
17512 }
17513 change |= in_triple(rb, expr);
17514 }
17515 return change;
17516}
17517
17518static int reg_in(struct compile_state *state, struct reg_block *blocks,
17519 struct reg_block *rb, struct block *suc)
17520{
17521 struct triple_reg_set *in_set;
17522 int change;
17523 change = 0;
17524 /* Read the input set of a successor block
17525 * and place it in the current blocks output set.
17526 */
17527 in_set = blocks[suc->vertex].in;
17528 for(; in_set; in_set = in_set->next) {
17529 int out_change, done;
17530 struct triple *first, *last, *ptr;
17531 out_change = out_triple(rb, in_set->member);
17532 if (!out_change) {
17533 continue;
17534 }
17535 /* If we don't define the variable also place it
17536 * in the current blocks input set.
17537 */
17538 first = rb->block->first;
17539 last = rb->block->last;
17540 done = 0;
17541 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017542 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017543 break;
17544 }
17545 done = (ptr == last);
17546 }
17547 if (!done) {
17548 continue;
17549 }
17550 change |= in_triple(rb, in_set->member);
17551 }
17552 change |= phi_in(state, blocks, rb, suc);
17553 return change;
17554}
17555
Eric Biedermanb138ac82003-04-22 18:44:01 +000017556static int use_in(struct compile_state *state, struct reg_block *rb)
17557{
17558 /* Find the variables we use but don't define and add
17559 * it to the current blocks input set.
17560 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017561#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017562#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017563#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017564 struct block *block;
17565 struct triple *ptr;
17566 int done;
17567 int change;
17568 block = rb->block;
17569 change = 0;
17570 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17571 struct triple **expr;
17572 done = (ptr == block->first);
17573 /* The variable a phi function uses depends on the
17574 * control flow, and is handled in phi_in, not
17575 * here.
17576 */
17577 if (ptr->op == OP_PHI) {
17578 continue;
17579 }
17580 expr = triple_rhs(state, ptr, 0);
17581 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17582 struct triple *rhs, *test;
17583 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017584 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017585 if (!rhs) {
17586 continue;
17587 }
Eric Biederman90089602004-05-28 14:11:54 +000017588
17589 /* See if rhs is defined in this block.
17590 * A write counts as a definition.
17591 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017592 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17593 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017594 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017595 rhs = 0;
17596 break;
17597 }
17598 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017599 /* If I still have a valid rhs add it to in */
17600 change |= in_triple(rb, rhs);
17601 }
17602 }
17603 return change;
17604}
17605
17606static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017607 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017608{
17609 struct reg_block *blocks;
17610 int change;
17611 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017612 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17613 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017614 do {
17615 int i;
17616 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017617 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017618 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017619 struct reg_block *rb;
17620 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017621 /* Add the all successor's input set to in */
17622 for(edge = rb->block->edges; edge; edge = edge->next) {
17623 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017624 }
17625 /* Add use to in... */
17626 change |= use_in(state, rb);
17627 }
17628 } while(change);
17629 return blocks;
17630}
17631
Stefan Reinauer14e22772010-04-27 06:56:47 +000017632static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017633 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017634{
17635 int i;
17636 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017637 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017638 struct triple_reg_set *entry, *next;
17639 struct reg_block *rb;
17640 rb = &blocks[i];
17641 for(entry = rb->in; entry ; entry = next) {
17642 next = entry->next;
17643 do_triple_unset(&rb->in, entry->member);
17644 }
17645 for(entry = rb->out; entry; entry = next) {
17646 next = entry->next;
17647 do_triple_unset(&rb->out, entry->member);
17648 }
17649 }
17650 xfree(blocks);
17651
17652}
17653
Eric Biedermanf96a8102003-06-16 16:57:34 +000017654typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017655 struct compile_state *state,
17656 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017657 struct reg_block *rb, struct triple *ins, void *arg);
17658
17659static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017660 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017661 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017662{
17663 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017664
Eric Biederman90089602004-05-28 14:11:54 +000017665 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017666 struct triple_reg_set *live;
17667 struct triple_reg_set *entry, *next;
17668 struct triple *ptr, *prev;
17669 struct reg_block *rb;
17670 struct block *block;
17671 int done;
17672
17673 /* Get the blocks */
17674 rb = &blocks[i];
17675 block = rb->block;
17676
17677 /* Copy out into live */
17678 live = 0;
17679 for(entry = rb->out; entry; entry = next) {
17680 next = entry->next;
17681 do_triple_set(&live, entry->member, entry->new);
17682 }
17683 /* Walk through the basic block calculating live */
17684 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017685 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017686
17687 prev = ptr->prev;
17688 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017689
17690 /* Ensure the current definition is in live */
17691 if (triple_is_def(state, ptr)) {
17692 do_triple_set(&live, ptr, 0);
17693 }
17694
17695 /* Inform the callback function of what is
17696 * going on.
17697 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017698 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017699
Eric Biedermanb138ac82003-04-22 18:44:01 +000017700 /* Remove the current definition from live */
17701 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017702
Eric Biedermanb138ac82003-04-22 18:44:01 +000017703 /* Add the current uses to live.
17704 *
17705 * It is safe to skip phi functions because they do
17706 * not have any block local uses, and the block
17707 * output sets already properly account for what
17708 * control flow depedent uses phi functions do have.
17709 */
17710 if (ptr->op == OP_PHI) {
17711 continue;
17712 }
17713 expr = triple_rhs(state, ptr, 0);
17714 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17715 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017716 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017717 continue;
17718 }
17719 do_triple_set(&live, *expr, 0);
17720 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017721 }
17722 /* Free live */
17723 for(entry = live; entry; entry = next) {
17724 next = entry->next;
17725 do_triple_unset(&live, entry->member);
17726 }
17727 }
17728}
17729
Eric Biederman90089602004-05-28 14:11:54 +000017730struct print_live_variable_info {
17731 struct reg_block *rb;
17732 FILE *fp;
17733};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017734#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017735static void print_live_variables_block(
17736 struct compile_state *state, struct block *block, void *arg)
17737
17738{
17739 struct print_live_variable_info *info = arg;
17740 struct block_set *edge;
17741 FILE *fp = info->fp;
17742 struct reg_block *rb;
17743 struct triple *ptr;
17744 int phi_present;
17745 int done;
17746 rb = &info->rb[block->vertex];
17747
17748 fprintf(fp, "\nblock: %p (%d),",
17749 block, block->vertex);
17750 for(edge = block->edges; edge; edge = edge->next) {
17751 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017752 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017753 edge->member && edge->member->use?edge->member->use->member : 0);
17754 }
17755 fprintf(fp, "\n");
17756 if (rb->in) {
17757 struct triple_reg_set *in_set;
17758 fprintf(fp, " in:");
17759 for(in_set = rb->in; in_set; in_set = in_set->next) {
17760 fprintf(fp, " %-10p", in_set->member);
17761 }
17762 fprintf(fp, "\n");
17763 }
17764 phi_present = 0;
17765 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17766 done = (ptr == block->last);
17767 if (ptr->op == OP_PHI) {
17768 phi_present = 1;
17769 break;
17770 }
17771 }
17772 if (phi_present) {
17773 int edge;
17774 for(edge = 0; edge < block->users; edge++) {
17775 fprintf(fp, " in(%d):", edge);
17776 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17777 struct triple **slot;
17778 done = (ptr == block->last);
17779 if (ptr->op != OP_PHI) {
17780 continue;
17781 }
17782 slot = &RHS(ptr, 0);
17783 fprintf(fp, " %-10p", slot[edge]);
17784 }
17785 fprintf(fp, "\n");
17786 }
17787 }
17788 if (block->first->op == OP_LABEL) {
17789 fprintf(fp, "%p:\n", block->first);
17790 }
17791 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17792 done = (ptr == block->last);
17793 display_triple(fp, ptr);
17794 }
17795 if (rb->out) {
17796 struct triple_reg_set *out_set;
17797 fprintf(fp, " out:");
17798 for(out_set = rb->out; out_set; out_set = out_set->next) {
17799 fprintf(fp, " %-10p", out_set->member);
17800 }
17801 fprintf(fp, "\n");
17802 }
17803 fprintf(fp, "\n");
17804}
17805
Stefan Reinauer14e22772010-04-27 06:56:47 +000017806static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017807 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17808{
17809 struct print_live_variable_info info;
17810 info.rb = rb;
17811 info.fp = fp;
17812 fprintf(fp, "\nlive variables by block\n");
17813 walk_blocks(state, bb, print_live_variables_block, &info);
17814
17815}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017816#endif
Eric Biederman90089602004-05-28 14:11:54 +000017817
Eric Biedermanb138ac82003-04-22 18:44:01 +000017818static int count_triples(struct compile_state *state)
17819{
17820 struct triple *first, *ins;
17821 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017822 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017823 ins = first;
17824 do {
17825 triples++;
17826 ins = ins->next;
17827 } while (ins != first);
17828 return triples;
17829}
Eric Biederman66fe2222003-07-04 15:14:04 +000017830
17831
Eric Biedermanb138ac82003-04-22 18:44:01 +000017832struct dead_triple {
17833 struct triple *triple;
17834 struct dead_triple *work_next;
17835 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017836 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017837 int flags;
17838#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017839#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017840};
17841
Stefan Reinauer14e22772010-04-27 06:56:47 +000017842static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017843 struct dead_triple *dtriple)
17844{
17845 struct triple *first, *ins;
17846 struct dead_triple *dt;
17847 FILE *fp;
17848 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17849 return;
17850 }
17851 fp = state->dbgout;
17852 fprintf(fp, "--------------- dtriples ---------------\n");
17853 first = state->first;
17854 ins = first;
17855 do {
17856 dt = &dtriple[ins->id];
17857 if ((ins->op == OP_LABEL) && (ins->use)) {
17858 fprintf(fp, "\n%p:\n", ins);
17859 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017860 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017861 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17862 display_triple(fp, ins);
17863 if (triple_is_branch(state, ins)) {
17864 fprintf(fp, "\n");
17865 }
17866 ins = ins->next;
17867 } while(ins != first);
17868 fprintf(fp, "\n");
17869}
17870
Eric Biedermanb138ac82003-04-22 18:44:01 +000017871
17872static void awaken(
17873 struct compile_state *state,
17874 struct dead_triple *dtriple, struct triple **expr,
17875 struct dead_triple ***work_list_tail)
17876{
17877 struct triple *triple;
17878 struct dead_triple *dt;
17879 if (!expr) {
17880 return;
17881 }
17882 triple = *expr;
17883 if (!triple) {
17884 return;
17885 }
17886 if (triple->id <= 0) {
17887 internal_error(state, triple, "bad triple id: %d",
17888 triple->id);
17889 }
17890 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017891 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017892 return;
17893 }
17894 dt = &dtriple[triple->id];
17895 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17896 dt->flags |= TRIPLE_FLAG_ALIVE;
17897 if (!dt->work_next) {
17898 **work_list_tail = dt;
17899 *work_list_tail = &dt->work_next;
17900 }
17901 }
17902}
17903
17904static void eliminate_inefectual_code(struct compile_state *state)
17905{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017906 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17907 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017908 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017909
Eric Biederman5ade04a2003-10-22 04:03:46 +000017910 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17911 return;
17912 }
17913
Eric Biedermanb138ac82003-04-22 18:44:01 +000017914 /* Setup the work list */
17915 work_list = 0;
17916 work_list_tail = &work_list;
17917
Eric Biederman83b991a2003-10-11 06:20:25 +000017918 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017919
17920 /* Count how many triples I have */
17921 triples = count_triples(state);
17922
17923 /* Now put then in an array and mark all of the triples dead */
17924 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017925
Eric Biedermanb138ac82003-04-22 18:44:01 +000017926 ins = first;
17927 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017928 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017929 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017930 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017931 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017932 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017933 ins->id = i;
17934 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017935 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017936 awaken(state, dtriple, &ins, &work_list_tail);
17937 }
17938 i++;
17939 ins = ins->next;
17940 } while(ins != first);
17941 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017942 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017943 struct dead_triple *dt;
17944 struct block_set *user;
17945 struct triple **expr;
17946 dt = work_list;
17947 work_list = dt->work_next;
17948 if (!work_list) {
17949 work_list_tail = &work_list;
17950 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017951 /* Make certain the block the current instruction is in lives */
17952 block = block_of_triple(state, dt->triple);
17953 awaken(state, dtriple, &block->first, &work_list_tail);
17954 if (triple_is_branch(state, block->last)) {
17955 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017956 } else {
17957 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017958 }
17959
Eric Biedermanb138ac82003-04-22 18:44:01 +000017960 /* Wake up the data depencencies of this triple */
17961 expr = 0;
17962 do {
17963 expr = triple_rhs(state, dt->triple, expr);
17964 awaken(state, dtriple, expr, &work_list_tail);
17965 } while(expr);
17966 do {
17967 expr = triple_lhs(state, dt->triple, expr);
17968 awaken(state, dtriple, expr, &work_list_tail);
17969 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017970 do {
17971 expr = triple_misc(state, dt->triple, expr);
17972 awaken(state, dtriple, expr, &work_list_tail);
17973 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017974 /* Wake up the forward control dependencies */
17975 do {
17976 expr = triple_targ(state, dt->triple, expr);
17977 awaken(state, dtriple, expr, &work_list_tail);
17978 } while(expr);
17979 /* Wake up the reverse control dependencies of this triple */
17980 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017981 struct triple *last;
17982 last = user->member->last;
17983 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017984#if DEBUG_ROMCC_WARNINGS
17985#warning "Should we bring the awakening noops back?"
17986#endif
17987 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017988 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017989 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017990 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017991 }
17992 }
Eric Biederman90089602004-05-28 14:11:54 +000017993 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017994 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000017995 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000017996 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17997 internal_error(state, dt->triple, "noop effective?");
17998 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017999 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018000 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018001 release_triple(state, dt->triple);
18002 }
18003 }
18004 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018005
18006 rebuild_ssa_form(state);
18007
Eric Biederman90089602004-05-28 14:11:54 +000018008 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018009}
18010
18011
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018012static void insert_mandatory_copies(struct compile_state *state)
18013{
18014 struct triple *ins, *first;
18015
18016 /* The object is with a minimum of inserted copies,
18017 * to resolve in fundamental register conflicts between
18018 * register value producers and consumers.
18019 * Theoretically we may be greater than minimal when we
18020 * are inserting copies before instructions but that
18021 * case should be rare.
18022 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018023 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018024 ins = first;
18025 do {
18026 struct triple_set *entry, *next;
18027 struct triple *tmp;
18028 struct reg_info info;
18029 unsigned reg, regcm;
18030 int do_post_copy, do_pre_copy;
18031 tmp = 0;
18032 if (!triple_is_def(state, ins)) {
18033 goto next;
18034 }
18035 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018036 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018037 if (info.reg >= MAX_REGISTERS) {
18038 info.reg = REG_UNSET;
18039 }
Eric Biederman90089602004-05-28 14:11:54 +000018040
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018041 reg = REG_UNSET;
18042 regcm = arch_type_to_regcm(state, ins->type);
18043 do_post_copy = do_pre_copy = 0;
18044
18045 /* Walk through the uses of ins and check for conflicts */
18046 for(entry = ins->use; entry; entry = next) {
18047 struct reg_info rinfo;
18048 int i;
18049 next = entry->next;
18050 i = find_rhs_use(state, entry->member, ins);
18051 if (i < 0) {
18052 continue;
18053 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018054
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018055 /* Find the users color requirements */
18056 rinfo = arch_reg_rhs(state, entry->member, i);
18057 if (rinfo.reg >= MAX_REGISTERS) {
18058 rinfo.reg = REG_UNSET;
18059 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018060
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018061 /* See if I need a pre_copy */
18062 if (rinfo.reg != REG_UNSET) {
18063 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18064 do_pre_copy = 1;
18065 }
18066 reg = rinfo.reg;
18067 }
18068 regcm &= rinfo.regcm;
18069 regcm = arch_regcm_normalize(state, regcm);
18070 if (regcm == 0) {
18071 do_pre_copy = 1;
18072 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018073 /* Always use pre_copies for constants.
18074 * They do not take up any registers until a
18075 * copy places them in one.
18076 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018077 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018078 (rinfo.reg != REG_UNNEEDED)) {
18079 do_pre_copy = 1;
18080 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018081 }
18082 do_post_copy =
18083 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018084 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018085 (reg != REG_UNSET) &&
18086 (info.reg != reg)) ||
18087 ((info.regcm & regcm) == 0));
18088
18089 reg = info.reg;
18090 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018091 /* 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 +000018092 for(entry = ins->use; entry; entry = next) {
18093 struct reg_info rinfo;
18094 int i;
18095 next = entry->next;
18096 i = find_rhs_use(state, entry->member, ins);
18097 if (i < 0) {
18098 continue;
18099 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018100
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018101 /* Find the users color requirements */
18102 rinfo = arch_reg_rhs(state, entry->member, i);
18103 if (rinfo.reg >= MAX_REGISTERS) {
18104 rinfo.reg = REG_UNSET;
18105 }
18106
18107 /* Now see if it is time to do the pre_copy */
18108 if (rinfo.reg != REG_UNSET) {
18109 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18110 ((regcm & rinfo.regcm) == 0) ||
18111 /* Don't let a mandatory coalesce sneak
18112 * into a operation that is marked to prevent
18113 * coalescing.
18114 */
18115 ((reg != REG_UNNEEDED) &&
18116 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18117 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18118 ) {
18119 if (do_pre_copy) {
18120 struct triple *user;
18121 user = entry->member;
18122 if (RHS(user, i) != ins) {
18123 internal_error(state, user, "bad rhs");
18124 }
18125 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018126 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018127 continue;
18128 } else {
18129 do_post_copy = 1;
18130 }
18131 }
18132 reg = rinfo.reg;
18133 }
18134 if ((regcm & rinfo.regcm) == 0) {
18135 if (do_pre_copy) {
18136 struct triple *user;
18137 user = entry->member;
18138 if (RHS(user, i) != ins) {
18139 internal_error(state, user, "bad rhs");
18140 }
18141 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018142 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018143 continue;
18144 } else {
18145 do_post_copy = 1;
18146 }
18147 }
18148 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018149
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018150 }
18151 if (do_post_copy) {
18152 struct reg_info pre, post;
18153 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018154 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018155 pre = arch_reg_lhs(state, ins, 0);
18156 post = arch_reg_lhs(state, tmp, 0);
18157 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18158 internal_error(state, tmp, "useless copy");
18159 }
18160 }
18161 next:
18162 ins = ins->next;
18163 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018164
Eric Biederman90089602004-05-28 14:11:54 +000018165 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018166}
18167
18168
Eric Biedermanb138ac82003-04-22 18:44:01 +000018169struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018170struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018171struct live_range {
18172 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018173 struct live_range_def *defs;
18174/* Note. The list pointed to by defs is kept in order.
18175 * That is baring splits in the flow control
18176 * defs dominates defs->next wich dominates defs->next->next
18177 * etc.
18178 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018179 unsigned color;
18180 unsigned classes;
18181 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018182 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018183 struct live_range *group_next, **group_prev;
18184};
18185
18186struct live_range_edge {
18187 struct live_range_edge *next;
18188 struct live_range *node;
18189};
18190
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018191struct live_range_def {
18192 struct live_range_def *next;
18193 struct live_range_def *prev;
18194 struct live_range *lr;
18195 struct triple *def;
18196 unsigned orig_id;
18197};
18198
Eric Biedermanb138ac82003-04-22 18:44:01 +000018199#define LRE_HASH_SIZE 2048
18200struct lre_hash {
18201 struct lre_hash *next;
18202 struct live_range *left;
18203 struct live_range *right;
18204};
18205
18206
18207struct reg_state {
18208 struct lre_hash *hash[LRE_HASH_SIZE];
18209 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018210 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018211 struct live_range *lr;
18212 struct live_range *low, **low_tail;
18213 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018214 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018215 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018216 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018217};
18218
18219
Eric Biedermand1ea5392003-06-28 06:49:45 +000018220struct print_interference_block_info {
18221 struct reg_state *rstate;
18222 FILE *fp;
18223 int need_edges;
18224};
18225static void print_interference_block(
18226 struct compile_state *state, struct block *block, void *arg)
18227
18228{
18229 struct print_interference_block_info *info = arg;
18230 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018231 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018232 FILE *fp = info->fp;
18233 struct reg_block *rb;
18234 struct triple *ptr;
18235 int phi_present;
18236 int done;
18237 rb = &rstate->blocks[block->vertex];
18238
Eric Biederman5ade04a2003-10-22 04:03:46 +000018239 fprintf(fp, "\nblock: %p (%d),",
18240 block, block->vertex);
18241 for(edge = block->edges; edge; edge = edge->next) {
18242 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018243 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018244 edge->member && edge->member->use?edge->member->use->member : 0);
18245 }
18246 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018247 if (rb->in) {
18248 struct triple_reg_set *in_set;
18249 fprintf(fp, " in:");
18250 for(in_set = rb->in; in_set; in_set = in_set->next) {
18251 fprintf(fp, " %-10p", in_set->member);
18252 }
18253 fprintf(fp, "\n");
18254 }
18255 phi_present = 0;
18256 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18257 done = (ptr == block->last);
18258 if (ptr->op == OP_PHI) {
18259 phi_present = 1;
18260 break;
18261 }
18262 }
18263 if (phi_present) {
18264 int edge;
18265 for(edge = 0; edge < block->users; edge++) {
18266 fprintf(fp, " in(%d):", edge);
18267 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18268 struct triple **slot;
18269 done = (ptr == block->last);
18270 if (ptr->op != OP_PHI) {
18271 continue;
18272 }
18273 slot = &RHS(ptr, 0);
18274 fprintf(fp, " %-10p", slot[edge]);
18275 }
18276 fprintf(fp, "\n");
18277 }
18278 }
18279 if (block->first->op == OP_LABEL) {
18280 fprintf(fp, "%p:\n", block->first);
18281 }
18282 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018283 struct live_range *lr;
18284 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018285 done = (ptr == block->last);
18286 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018287
Eric Biedermand1ea5392003-06-28 06:49:45 +000018288 id = ptr->id;
18289 ptr->id = rstate->lrd[id].orig_id;
18290 SET_REG(ptr->id, lr->color);
18291 display_triple(fp, ptr);
18292 ptr->id = id;
18293
18294 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18295 internal_error(state, ptr, "lr has no defs!");
18296 }
18297 if (info->need_edges) {
18298 if (lr->defs) {
18299 struct live_range_def *lrd;
18300 fprintf(fp, " range:");
18301 lrd = lr->defs;
18302 do {
18303 fprintf(fp, " %-10p", lrd->def);
18304 lrd = lrd->next;
18305 } while(lrd != lr->defs);
18306 fprintf(fp, "\n");
18307 }
18308 if (lr->edges > 0) {
18309 struct live_range_edge *edge;
18310 fprintf(fp, " edges:");
18311 for(edge = lr->edges; edge; edge = edge->next) {
18312 struct live_range_def *lrd;
18313 lrd = edge->node->defs;
18314 do {
18315 fprintf(fp, " %-10p", lrd->def);
18316 lrd = lrd->next;
18317 } while(lrd != edge->node->defs);
18318 fprintf(fp, "|");
18319 }
18320 fprintf(fp, "\n");
18321 }
18322 }
18323 /* Do a bunch of sanity checks */
18324 valid_ins(state, ptr);
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018325 if (ptr->id > rstate->defs) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018326 internal_error(state, ptr, "Invalid triple id: %d",
18327 ptr->id);
18328 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018329 }
18330 if (rb->out) {
18331 struct triple_reg_set *out_set;
18332 fprintf(fp, " out:");
18333 for(out_set = rb->out; out_set; out_set = out_set->next) {
18334 fprintf(fp, " %-10p", out_set->member);
18335 }
18336 fprintf(fp, "\n");
18337 }
18338 fprintf(fp, "\n");
18339}
18340
18341static void print_interference_blocks(
18342 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18343{
18344 struct print_interference_block_info info;
18345 info.rstate = rstate;
18346 info.fp = fp;
18347 info.need_edges = need_edges;
18348 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018349 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018350
18351}
18352
Eric Biedermanb138ac82003-04-22 18:44:01 +000018353static unsigned regc_max_size(struct compile_state *state, int classes)
18354{
18355 unsigned max_size;
18356 int i;
18357 max_size = 0;
18358 for(i = 0; i < MAX_REGC; i++) {
18359 if (classes & (1 << i)) {
18360 unsigned size;
18361 size = arch_regc_size(state, i);
18362 if (size > max_size) {
18363 max_size = size;
18364 }
18365 }
18366 }
18367 return max_size;
18368}
18369
18370static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18371{
18372 unsigned equivs[MAX_REG_EQUIVS];
18373 int i;
18374 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18375 internal_error(state, 0, "invalid register");
18376 }
18377 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18378 internal_error(state, 0, "invalid register");
18379 }
18380 arch_reg_equivs(state, equivs, reg1);
18381 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18382 if (equivs[i] == reg2) {
18383 return 1;
18384 }
18385 }
18386 return 0;
18387}
18388
18389static void reg_fill_used(struct compile_state *state, char *used, int reg)
18390{
18391 unsigned equivs[MAX_REG_EQUIVS];
18392 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018393 if (reg == REG_UNNEEDED) {
18394 return;
18395 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018396 arch_reg_equivs(state, equivs, reg);
18397 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18398 used[equivs[i]] = 1;
18399 }
18400 return;
18401}
18402
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018403static void reg_inc_used(struct compile_state *state, char *used, int reg)
18404{
18405 unsigned equivs[MAX_REG_EQUIVS];
18406 int i;
18407 if (reg == REG_UNNEEDED) {
18408 return;
18409 }
18410 arch_reg_equivs(state, equivs, reg);
18411 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18412 used[equivs[i]] += 1;
18413 }
18414 return;
18415}
18416
Eric Biedermanb138ac82003-04-22 18:44:01 +000018417static unsigned int hash_live_edge(
18418 struct live_range *left, struct live_range *right)
18419{
18420 unsigned int hash, val;
18421 unsigned long lval, rval;
18422 lval = ((unsigned long)left)/sizeof(struct live_range);
18423 rval = ((unsigned long)right)/sizeof(struct live_range);
18424 hash = 0;
18425 while(lval) {
18426 val = lval & 0xff;
18427 lval >>= 8;
18428 hash = (hash *263) + val;
18429 }
18430 while(rval) {
18431 val = rval & 0xff;
18432 rval >>= 8;
18433 hash = (hash *263) + val;
18434 }
18435 hash = hash & (LRE_HASH_SIZE - 1);
18436 return hash;
18437}
18438
18439static struct lre_hash **lre_probe(struct reg_state *rstate,
18440 struct live_range *left, struct live_range *right)
18441{
18442 struct lre_hash **ptr;
18443 unsigned int index;
18444 /* Ensure left <= right */
18445 if (left > right) {
18446 struct live_range *tmp;
18447 tmp = left;
18448 left = right;
18449 right = tmp;
18450 }
18451 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018452
Eric Biedermanb138ac82003-04-22 18:44:01 +000018453 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018454 while(*ptr) {
18455 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18456 break;
18457 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018458 ptr = &(*ptr)->next;
18459 }
18460 return ptr;
18461}
18462
18463static int interfere(struct reg_state *rstate,
18464 struct live_range *left, struct live_range *right)
18465{
18466 struct lre_hash **ptr;
18467 ptr = lre_probe(rstate, left, right);
18468 return ptr && *ptr;
18469}
18470
Stefan Reinauer14e22772010-04-27 06:56:47 +000018471static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018472 struct live_range *left, struct live_range *right)
18473{
18474 /* FIXME the memory allocation overhead is noticeable here... */
18475 struct lre_hash **ptr, *new_hash;
18476 struct live_range_edge *edge;
18477
18478 if (left == right) {
18479 return;
18480 }
18481 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18482 return;
18483 }
18484 /* Ensure left <= right */
18485 if (left > right) {
18486 struct live_range *tmp;
18487 tmp = left;
18488 left = right;
18489 right = tmp;
18490 }
18491 ptr = lre_probe(rstate, left, right);
18492 if (*ptr) {
18493 return;
18494 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018495#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018496 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018497 left, right);
18498#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018499 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18500 new_hash->next = *ptr;
18501 new_hash->left = left;
18502 new_hash->right = right;
18503 *ptr = new_hash;
18504
18505 edge = xmalloc(sizeof(*edge), "live_range_edge");
18506 edge->next = left->edges;
18507 edge->node = right;
18508 left->edges = edge;
18509 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018510
Eric Biedermanb138ac82003-04-22 18:44:01 +000018511 edge = xmalloc(sizeof(*edge), "live_range_edge");
18512 edge->next = right->edges;
18513 edge->node = left;
18514 right->edges = edge;
18515 right->degree += 1;
18516}
18517
18518static void remove_live_edge(struct reg_state *rstate,
18519 struct live_range *left, struct live_range *right)
18520{
18521 struct live_range_edge *edge, **ptr;
18522 struct lre_hash **hptr, *entry;
18523 hptr = lre_probe(rstate, left, right);
18524 if (!hptr || !*hptr) {
18525 return;
18526 }
18527 entry = *hptr;
18528 *hptr = entry->next;
18529 xfree(entry);
18530
18531 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18532 edge = *ptr;
18533 if (edge->node == right) {
18534 *ptr = edge->next;
18535 memset(edge, 0, sizeof(*edge));
18536 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018537 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018538 break;
18539 }
18540 }
18541 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18542 edge = *ptr;
18543 if (edge->node == left) {
18544 *ptr = edge->next;
18545 memset(edge, 0, sizeof(*edge));
18546 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018547 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018548 break;
18549 }
18550 }
18551}
18552
18553static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18554{
18555 struct live_range_edge *edge, *next;
18556 for(edge = range->edges; edge; edge = next) {
18557 next = edge->next;
18558 remove_live_edge(rstate, range, edge->node);
18559 }
18560}
18561
Stefan Reinauer14e22772010-04-27 06:56:47 +000018562static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018563 struct live_range *dest, struct live_range *src)
18564{
18565 struct live_range_edge *edge, *next;
18566 for(edge = src->edges; edge; edge = next) {
18567 struct live_range *other;
18568 next = edge->next;
18569 other = edge->node;
18570 remove_live_edge(rstate, src, other);
18571 add_live_edge(rstate, dest, other);
18572 }
18573}
18574
Eric Biedermanb138ac82003-04-22 18:44:01 +000018575
18576/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018577 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018578 * new(n) --- Return a graph with n nodes but no edges.
18579 * add(g,x,y) --- Return a graph including g with an between x and y
18580 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18581 * x and y in the graph g
18582 * degree(g, x) --- Return the degree of the node x in the graph g
18583 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18584 *
18585 * Implement with a hash table && a set of adjcency vectors.
18586 * The hash table supports constant time implementations of add and interfere.
18587 * The adjacency vectors support an efficient implementation of neighbors.
18588 */
18589
Stefan Reinauer14e22772010-04-27 06:56:47 +000018590/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018591 * +---------------------------------------------------+
18592 * | +--------------+ |
18593 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018594 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018595 *
18596 * -- In simplify implment optimistic coloring... (No backtracking)
18597 * -- Implement Rematerialization it is the only form of spilling we can perform
18598 * Essentially this means dropping a constant from a register because
18599 * we can regenerate it later.
18600 *
18601 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18602 * coalesce at phi points...
18603 * --- Bias coloring if at all possible do the coalesing a compile time.
18604 *
18605 *
18606 */
18607
Stefan Reinauer50542a82007-10-24 11:14:14 +000018608#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018609static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018610 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018611 struct triple *parent, struct triple *ins)
18612{
18613 struct live_range *lr;
18614 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018615 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018616 expr = triple_rhs(state, ins, 0);
18617 for(;expr; expr = triple_rhs(state, ins, expr)) {
18618 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018619 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018620 continue;
18621 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018622 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018623 if (lr->color == lr2->color) {
18624 internal_error(state, ins, "live range too big");
18625 }
18626 }
18627}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018628#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018629
18630static struct live_range *coalesce_ranges(
18631 struct compile_state *state, struct reg_state *rstate,
18632 struct live_range *lr1, struct live_range *lr2)
18633{
18634 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18635 unsigned color;
18636 unsigned classes;
18637 if (lr1 == lr2) {
18638 return lr1;
18639 }
18640 if (!lr1->defs || !lr2->defs) {
18641 internal_error(state, 0,
18642 "cannot coalese dead live ranges");
18643 }
18644 if ((lr1->color == REG_UNNEEDED) ||
18645 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018646 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018647 "cannot coalesce live ranges without a possible color");
18648 }
18649 if ((lr1->color != lr2->color) &&
18650 (lr1->color != REG_UNSET) &&
18651 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018652 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018653 "cannot coalesce live ranges of different colors");
18654 }
18655 color = lr1->color;
18656 if (color == REG_UNSET) {
18657 color = lr2->color;
18658 }
18659 classes = lr1->classes & lr2->classes;
18660 if (!classes) {
18661 internal_error(state, lr1->defs->def,
18662 "cannot coalesce live ranges with dissimilar register classes");
18663 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018664 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018665 FILE *fp = state->errout;
18666 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018667 lrd = lr1->defs;
18668 do {
Eric Biederman90089602004-05-28 14:11:54 +000018669 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018670 lrd = lrd->next;
18671 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018672 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018673 lrd = lr2->defs;
18674 do {
Eric Biederman90089602004-05-28 14:11:54 +000018675 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018676 lrd = lrd->next;
18677 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018678 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018679 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018680 /* If there is a clear dominate live range put it in lr1,
18681 * For purposes of this test phi functions are
18682 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018683 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018684 */
18685 if ((lr1->defs->prev->def->op == OP_PHI) ||
18686 ((lr2->defs->prev->def->op != OP_PHI) &&
18687 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18688 struct live_range *tmp;
18689 tmp = lr1;
18690 lr1 = lr2;
18691 lr2 = tmp;
18692 }
18693#if 0
18694 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018695 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018696 }
18697 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018698 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018699 }
18700 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018701 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018702 }
18703 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018704 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018705 }
18706#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018707#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018708 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018709 lr1->defs->def,
18710 lr1->color,
18711 lr2->defs->def,
18712 lr2->color);
18713#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018714
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018715 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018716#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018717#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018718#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018719 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018720 * and to the necessity of making multiple passes of coalesce_live_ranges.
18721 * A failure to find some coalesce opportunities in coaleace_live_ranges
18722 * does not impact the correct of the compiler just the efficiency with
18723 * which registers are allocated.
18724 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018725 head = lr1->defs;
18726 mid1 = lr1->defs->prev;
18727 mid2 = lr2->defs;
18728 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018729
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018730 head->prev = end;
18731 end->next = head;
18732
18733 mid1->next = mid2;
18734 mid2->prev = mid1;
18735
18736 /* Fixup the live range in the added live range defs */
18737 lrd = head;
18738 do {
18739 lrd->lr = lr1;
18740 lrd = lrd->next;
18741 } while(lrd != head);
18742
18743 /* Mark lr2 as free. */
18744 lr2->defs = 0;
18745 lr2->color = REG_UNNEEDED;
18746 lr2->classes = 0;
18747
18748 if (!lr1->defs) {
18749 internal_error(state, 0, "lr1->defs == 0 ?");
18750 }
18751
18752 lr1->color = color;
18753 lr1->classes = classes;
18754
Eric Biederman153ea352003-06-20 14:43:20 +000018755 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18756 transfer_live_edges(rstate, lr1, lr2);
18757
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018758 return lr1;
18759}
18760
18761static struct live_range_def *live_range_head(
18762 struct compile_state *state, struct live_range *lr,
18763 struct live_range_def *last)
18764{
18765 struct live_range_def *result;
18766 result = 0;
18767 if (last == 0) {
18768 result = lr->defs;
18769 }
18770 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18771 result = last->next;
18772 }
18773 return result;
18774}
18775
18776static struct live_range_def *live_range_end(
18777 struct compile_state *state, struct live_range *lr,
18778 struct live_range_def *last)
18779{
18780 struct live_range_def *result;
18781 result = 0;
18782 if (last == 0) {
18783 result = lr->defs->prev;
18784 }
18785 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18786 result = last->prev;
18787 }
18788 return result;
18789}
18790
18791
Eric Biedermanb138ac82003-04-22 18:44:01 +000018792static void initialize_live_ranges(
18793 struct compile_state *state, struct reg_state *rstate)
18794{
18795 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018796 size_t count, size;
18797 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018798
Eric Biederman83b991a2003-10-11 06:20:25 +000018799 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018800 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018801 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018802 count = count_triples(state);
18803 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018804 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018805 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018806 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018807 /* Potentially I need one live range for each instruction
18808 * plus an extra for the dummy live range.
18809 */
18810 rstate->ranges = count + 1;
18811 size = sizeof(rstate->lrd[0]) * rstate->defs;
18812 rstate->lrd = xcmalloc(size, "live_range_def");
18813 size = sizeof(rstate->lr[0]) * rstate->ranges;
18814 rstate->lr = xcmalloc(size, "live_range");
18815
Eric Biedermanb138ac82003-04-22 18:44:01 +000018816 /* Setup the dummy live range */
18817 rstate->lr[0].classes = 0;
18818 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018819 rstate->lr[0].defs = 0;
18820 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018821 ins = first;
18822 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018823 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018824 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018825 struct reg_info info;
18826 /* Find the architecture specific color information */
18827 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018828 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018829 rstate->lr[i].defs = &rstate->lrd[j];
18830 rstate->lr[i].color = info.reg;
18831 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018832 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018833 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018834 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018835 /* Otherwise give the triple the dummy live range. */
18836 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018837 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018838 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018839
18840 /* Initalize the live_range_def */
18841 rstate->lrd[j].next = &rstate->lrd[j];
18842 rstate->lrd[j].prev = &rstate->lrd[j];
18843 rstate->lrd[j].def = ins;
18844 rstate->lrd[j].orig_id = ins->id;
18845 ins->id = j;
18846
18847 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018848 ins = ins->next;
18849 } while(ins != first);
18850 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018851
Eric Biedermanb138ac82003-04-22 18:44:01 +000018852 /* Make a second pass to handle achitecture specific register
18853 * constraints.
18854 */
18855 ins = first;
18856 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018857 int zlhs, zrhs, i, j;
18858 if (ins->id > rstate->defs) {
18859 internal_error(state, ins, "bad id");
18860 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018861
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018862 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018863 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018864 if ((zlhs == 0) && triple_is_def(state, ins)) {
18865 zlhs = 1;
18866 }
Eric Biederman90089602004-05-28 14:11:54 +000018867 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018868
Eric Biederman5ade04a2003-10-22 04:03:46 +000018869 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018870 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018871 ins, zlhs, zrhs);
18872 }
18873
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018874 for(i = 0; i < zlhs; i++) {
18875 struct reg_info linfo;
18876 struct live_range_def *lhs;
18877 linfo = arch_reg_lhs(state, ins, i);
18878 if (linfo.reg < MAX_REGISTERS) {
18879 continue;
18880 }
18881 if (triple_is_def(state, ins)) {
18882 lhs = &rstate->lrd[ins->id];
18883 } else {
18884 lhs = &rstate->lrd[LHS(ins, i)->id];
18885 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018886
18887 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018888 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018889 i, lhs, linfo.reg);
18890 }
18891
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018892 for(j = 0; j < zrhs; j++) {
18893 struct reg_info rinfo;
18894 struct live_range_def *rhs;
18895 rinfo = arch_reg_rhs(state, ins, j);
18896 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018897 continue;
18898 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018899 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018900
18901 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018902 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018903 j, rhs, rinfo.reg);
18904 }
18905
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018906 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018907 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018908 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018909 }
18910 }
18911 }
18912 ins = ins->next;
18913 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018914}
18915
Eric Biedermanf96a8102003-06-16 16:57:34 +000018916static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018917 struct compile_state *state,
18918 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018919 struct reg_block *rb, struct triple *ins, void *arg)
18920{
18921 struct reg_state *rstate = arg;
18922 struct live_range *def;
18923 struct triple_reg_set *entry;
18924
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018925 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018926 * we do not have a definition to add to
18927 * the interference graph.
18928 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018929 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018930 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018931 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018932 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018933
Eric Biedermanb138ac82003-04-22 18:44:01 +000018934 /* Create an edge between ins and everything that is
18935 * alive, unless the live_range cannot share
18936 * a physical register with ins.
18937 */
18938 for(entry = live; entry; entry = entry->next) {
18939 struct live_range *lr;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018940 if (entry->member->id > rstate->defs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018941 internal_error(state, 0, "bad entry?");
18942 }
18943 lr = rstate->lrd[entry->member->id].lr;
18944 if (def == lr) {
18945 continue;
18946 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018947 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18948 continue;
18949 }
18950 add_live_edge(rstate, def, lr);
18951 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018952 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018953}
18954
Stefan Reinauer50542a82007-10-24 11:14:14 +000018955#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018956static struct live_range *get_verify_live_range(
18957 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18958{
18959 struct live_range *lr;
18960 struct live_range_def *lrd;
18961 int ins_found;
18962 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18963 internal_error(state, ins, "bad ins?");
18964 }
18965 lr = rstate->lrd[ins->id].lr;
18966 ins_found = 0;
18967 lrd = lr->defs;
18968 do {
18969 if (lrd->def == ins) {
18970 ins_found = 1;
18971 }
18972 lrd = lrd->next;
18973 } while(lrd != lr->defs);
18974 if (!ins_found) {
18975 internal_error(state, ins, "ins not in live range");
18976 }
18977 return lr;
18978}
18979
18980static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018981 struct compile_state *state,
18982 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018983 struct reg_block *rb, struct triple *ins, void *arg)
18984{
18985 struct reg_state *rstate = arg;
18986 struct triple_reg_set *entry1, *entry2;
18987
18988
18989 /* Compare live against edges and make certain the code is working */
18990 for(entry1 = live; entry1; entry1 = entry1->next) {
18991 struct live_range *lr1;
18992 lr1 = get_verify_live_range(state, rstate, entry1->member);
18993 for(entry2 = live; entry2; entry2 = entry2->next) {
18994 struct live_range *lr2;
18995 struct live_range_edge *edge2;
18996 int lr1_found;
18997 int lr2_degree;
18998 if (entry2 == entry1) {
18999 continue;
19000 }
19001 lr2 = get_verify_live_range(state, rstate, entry2->member);
19002 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019003 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019004 "live range with 2 values simultaneously alive");
19005 }
19006 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19007 continue;
19008 }
19009 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019010 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019011 "edges don't interfere?");
19012 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019013
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019014 lr1_found = 0;
19015 lr2_degree = 0;
19016 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19017 lr2_degree++;
19018 if (edge2->node == lr1) {
19019 lr1_found = 1;
19020 }
19021 }
19022 if (lr2_degree != lr2->degree) {
19023 internal_error(state, entry2->member,
19024 "computed degree: %d does not match reported degree: %d\n",
19025 lr2_degree, lr2->degree);
19026 }
19027 if (!lr1_found) {
19028 internal_error(state, entry2->member, "missing edge");
19029 }
19030 }
19031 }
19032 return;
19033}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019034#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019035
Eric Biedermanf96a8102003-06-16 16:57:34 +000019036static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019037 struct compile_state *state,
19038 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019039 struct reg_block *rb, struct triple *ins, void *arg)
19040{
19041 struct reg_state *rstate = arg;
19042 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019043 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019044 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019045
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019046 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019047 id = ins->id;
19048 ins->id = rstate->lrd[id].orig_id;
19049 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019050 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019051 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019052
19053 if (lr->defs) {
19054 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019055 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019056 lrd = lr->defs;
19057 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019058 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019059 lrd = lrd->next;
19060 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019061 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019062 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019063 if (live) {
19064 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019065 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019066 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019067 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019068 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019069 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019070 }
19071 if (lr->edges) {
19072 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019073 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019074 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019075 struct live_range_def *lrd;
19076 lrd = entry->node->defs;
19077 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019078 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019079 lrd = lrd->next;
19080 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019081 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019082 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019083 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019084 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019085 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019086 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019087 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019088 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019089}
19090
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019091static int coalesce_live_ranges(
19092 struct compile_state *state, struct reg_state *rstate)
19093{
19094 /* At the point where a value is moved from one
19095 * register to another that value requires two
19096 * registers, thus increasing register pressure.
19097 * Live range coaleescing reduces the register
19098 * pressure by keeping a value in one register
19099 * longer.
19100 *
19101 * In the case of a phi function all paths leading
19102 * into it must be allocated to the same register
19103 * otherwise the phi function may not be removed.
19104 *
19105 * Forcing a value to stay in a single register
19106 * for an extended period of time does have
19107 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019108 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019109 *
19110 * The two cases I have identified are:
19111 * 1) Two forced register assignments may
19112 * collide.
19113 * 2) Registers may go unused because they
19114 * are only good for storing the value
19115 * and not manipulating it.
19116 *
19117 * Because of this I need to split live ranges,
19118 * even outside of the context of coalesced live
19119 * ranges. The need to split live ranges does
19120 * impose some constraints on live range coalescing.
19121 *
19122 * - Live ranges may not be coalesced across phi
19123 * functions. This creates a 2 headed live
19124 * range that cannot be sanely split.
19125 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019126 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019127 * are handled as pre split live ranges so we will
19128 * never attempt to split them.
19129 */
19130 int coalesced;
19131 int i;
19132
19133 coalesced = 0;
19134 for(i = 0; i <= rstate->ranges; i++) {
19135 struct live_range *lr1;
19136 struct live_range_def *lrd1;
19137 lr1 = &rstate->lr[i];
19138 if (!lr1->defs) {
19139 continue;
19140 }
19141 lrd1 = live_range_end(state, lr1, 0);
19142 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19143 struct triple_set *set;
19144 if (lrd1->def->op != OP_COPY) {
19145 continue;
19146 }
19147 /* Skip copies that are the result of a live range split. */
19148 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19149 continue;
19150 }
19151 for(set = lrd1->def->use; set; set = set->next) {
19152 struct live_range_def *lrd2;
19153 struct live_range *lr2, *res;
19154
19155 lrd2 = &rstate->lrd[set->member->id];
19156
19157 /* Don't coalesce with instructions
19158 * that are the result of a live range
19159 * split.
19160 */
19161 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19162 continue;
19163 }
19164 lr2 = rstate->lrd[set->member->id].lr;
19165 if (lr1 == lr2) {
19166 continue;
19167 }
19168 if ((lr1->color != lr2->color) &&
19169 (lr1->color != REG_UNSET) &&
19170 (lr2->color != REG_UNSET)) {
19171 continue;
19172 }
19173 if ((lr1->classes & lr2->classes) == 0) {
19174 continue;
19175 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019176
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019177 if (interfere(rstate, lr1, lr2)) {
19178 continue;
19179 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019180
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019181 res = coalesce_ranges(state, rstate, lr1, lr2);
19182 coalesced += 1;
19183 if (res != lr1) {
19184 goto next;
19185 }
19186 }
19187 }
19188 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019189 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019190 }
19191 return coalesced;
19192}
19193
19194
Eric Biedermanf96a8102003-06-16 16:57:34 +000019195static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019196 struct reg_block *blocks, struct triple_reg_set *live,
19197 struct reg_block *rb, struct triple *ins, void *arg)
19198{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019199 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019200 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019201
19202 /* See if we have a mandatory coalesce operation between
19203 * a lhs and a rhs value. If so and the rhs value is also
19204 * alive then this triple needs to be pre copied. Otherwise
19205 * we would have two definitions in the same live range simultaneously
19206 * alive.
19207 */
Eric Biederman90089602004-05-28 14:11:54 +000019208 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019209 if ((zlhs == 0) && triple_is_def(state, ins)) {
19210 zlhs = 1;
19211 }
Eric Biederman90089602004-05-28 14:11:54 +000019212 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019213 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019214 struct reg_info linfo;
19215 linfo = arch_reg_lhs(state, ins, i);
19216 if (linfo.reg < MAX_REGISTERS) {
19217 continue;
19218 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019219 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019220 struct reg_info rinfo;
19221 struct triple *rhs;
19222 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019223 int found;
19224 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019225 rinfo = arch_reg_rhs(state, ins, j);
19226 if (rinfo.reg != linfo.reg) {
19227 continue;
19228 }
19229 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019230 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019231 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019232 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019233 }
19234 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019235 if (found) {
19236 struct triple *copy;
19237 copy = pre_copy(state, ins, j);
19238 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019239 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019240 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019241 }
19242 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019243 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019244}
19245
Eric Biedermand1ea5392003-06-28 06:49:45 +000019246static int correct_coalesce_conflicts(
19247 struct compile_state *state, struct reg_block *blocks)
19248{
19249 int conflicts;
19250 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019251 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019252 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019253 return conflicts;
19254}
19255
Eric Biedermanf96a8102003-06-16 16:57:34 +000019256static void replace_set_use(struct compile_state *state,
19257 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019258{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019259 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019260 for(set = head; set; set = set->next) {
19261 if (set->member == orig) {
19262 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019263 }
19264 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019265}
19266
Stefan Reinauer14e22772010-04-27 06:56:47 +000019267static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019268 struct reg_block *blocks, struct triple *orig, struct triple *new)
19269{
19270 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019271#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019272#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019273#endif
Eric Biederman90089602004-05-28 14:11:54 +000019274 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019275 struct reg_block *rb;
19276 rb = &blocks[i];
19277 replace_set_use(state, rb->in, orig, new);
19278 replace_set_use(state, rb->out, orig, new);
19279 }
19280}
19281
19282static void color_instructions(struct compile_state *state)
19283{
19284 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019285 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019286 ins = first;
19287 do {
19288 if (triple_is_def(state, ins)) {
19289 struct reg_info info;
19290 info = find_lhs_color(state, ins, 0);
19291 if (info.reg >= MAX_REGISTERS) {
19292 info.reg = REG_UNSET;
19293 }
19294 SET_INFO(ins->id, info);
19295 }
19296 ins = ins->next;
19297 } while(ins != first);
19298}
19299
19300static struct reg_info read_lhs_color(
19301 struct compile_state *state, struct triple *ins, int index)
19302{
19303 struct reg_info info;
19304 if ((index == 0) && triple_is_def(state, ins)) {
19305 info.reg = ID_REG(ins->id);
19306 info.regcm = ID_REGCM(ins->id);
19307 }
Eric Biederman90089602004-05-28 14:11:54 +000019308 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019309 info = read_lhs_color(state, LHS(ins, index), 0);
19310 }
19311 else {
19312 internal_error(state, ins, "Bad lhs %d", index);
19313 info.reg = REG_UNSET;
19314 info.regcm = 0;
19315 }
19316 return info;
19317}
19318
19319static struct triple *resolve_tangle(
19320 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019321{
19322 struct reg_info info, uinfo;
19323 struct triple_set *set, *next;
19324 struct triple *copy;
19325
Stefan Reinauer50542a82007-10-24 11:14:14 +000019326#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019327#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019328#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019329 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019330 for(set = tangle->use; set; set = next) {
19331 struct triple *user;
19332 int i, zrhs;
19333 next = set->next;
19334 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019335 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019336 for(i = 0; i < zrhs; i++) {
19337 if (RHS(user, i) != tangle) {
19338 continue;
19339 }
19340 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019341 if (uinfo.reg == info.reg) {
19342 copy = pre_copy(state, user, i);
19343 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019344 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019345 }
19346 }
19347 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019348 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019349 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019351 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019352 copy = post_copy(state, tangle);
19353 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019354 linfo = find_lhs_color(state, copy, 0);
19355 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019356 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019357 info = find_lhs_color(state, tangle, 0);
19358 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019359
Eric Biedermanf96a8102003-06-16 16:57:34 +000019360 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019361}
19362
19363
Eric Biedermanf96a8102003-06-16 16:57:34 +000019364static void fix_tangles(struct compile_state *state,
19365 struct reg_block *blocks, struct triple_reg_set *live,
19366 struct reg_block *rb, struct triple *ins, void *arg)
19367{
Eric Biederman153ea352003-06-20 14:43:20 +000019368 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019369 struct triple *tangle;
19370 do {
19371 char used[MAX_REGISTERS];
19372 struct triple_reg_set *set;
19373 tangle = 0;
19374
19375 /* Find out which registers have multiple uses at this point */
19376 memset(used, 0, sizeof(used));
19377 for(set = live; set; set = set->next) {
19378 struct reg_info info;
19379 info = read_lhs_color(state, set->member, 0);
19380 if (info.reg == REG_UNSET) {
19381 continue;
19382 }
19383 reg_inc_used(state, used, info.reg);
19384 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019385
Eric Biedermanf96a8102003-06-16 16:57:34 +000019386 /* Now find the least dominated definition of a register in
19387 * conflict I have seen so far.
19388 */
19389 for(set = live; set; set = set->next) {
19390 struct reg_info info;
19391 info = read_lhs_color(state, set->member, 0);
19392 if (used[info.reg] < 2) {
19393 continue;
19394 }
Eric Biederman153ea352003-06-20 14:43:20 +000019395 /* Changing copies that feed into phi functions
19396 * is incorrect.
19397 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019398 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019399 (set->member->use->member->op == OP_PHI)) {
19400 continue;
19401 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019402 if (!tangle || tdominates(state, set->member, tangle)) {
19403 tangle = set->member;
19404 }
19405 }
19406 /* If I have found a tangle resolve it */
19407 if (tangle) {
19408 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019409 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019410 post_copy = resolve_tangle(state, tangle);
19411 if (post_copy) {
19412 replace_block_use(state, blocks, tangle, post_copy);
19413 }
19414 if (post_copy && (tangle != ins)) {
19415 replace_set_use(state, live, tangle, post_copy);
19416 }
19417 }
19418 } while(tangle);
19419 return;
19420}
19421
Eric Biederman153ea352003-06-20 14:43:20 +000019422static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019423 struct compile_state *state, struct reg_block *blocks)
19424{
Eric Biederman153ea352003-06-20 14:43:20 +000019425 int tangles;
19426 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019427 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019428 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019429 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019430 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019431}
19432
Eric Biedermand1ea5392003-06-28 06:49:45 +000019433
19434static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19435static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19436
19437struct triple *find_constrained_def(
19438 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019439{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019440 struct live_range_def *lrd, *lrd_next;
19441 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019442 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019443 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019444 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019445
19446 lrd = lrd_next;
19447 lrd_next = lrd->next;
19448
Eric Biedermand1ea5392003-06-28 06:49:45 +000019449 regcm = arch_type_to_regcm(state, lrd->def->type);
19450 info = find_lhs_color(state, lrd->def, 0);
19451 regcm = arch_regcm_reg_normalize(state, regcm);
19452 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019453 /* If the 2 register class masks are equal then
19454 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019455 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019456 if (regcm == info.regcm) {
19457 continue;
19458 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019459
Eric Biederman5ade04a2003-10-22 04:03:46 +000019460 /* If there is just one use.
19461 * That use cannot accept a larger register class.
19462 * There are no intervening definitions except
19463 * definitions that feed into that use.
19464 * Then a triple is not constrained.
19465 * FIXME handle this case!
19466 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019467#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019468#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019469#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019470
Eric Biederman5ade04a2003-10-22 04:03:46 +000019471
Eric Biedermand1ea5392003-06-28 06:49:45 +000019472 /* Of the constrained live ranges deal with the
19473 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019474 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019475 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019476 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019477 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019478 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019479 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019480 tdominates(state, lrd->def, constrained))
19481 {
19482 constrained = lrd->def;
19483 }
19484 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019485 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019486}
19487
Eric Biedermand1ea5392003-06-28 06:49:45 +000019488static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019489 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019490 struct live_range *range)
19491{
19492 /* Walk through the edges in conflict and our current live
19493 * range, and find definitions that are more severly constrained
19494 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019495 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019496 * Then pick one of those ranges and relax the constraints.
19497 */
19498 struct live_range_edge *edge;
19499 struct triple *constrained;
19500
19501 constrained = 0;
19502 for(edge = range->edges; edge; edge = edge->next) {
19503 constrained = find_constrained_def(state, edge->node, constrained);
19504 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019505#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019506#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019507#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019508 if (!constrained) {
19509 constrained = find_constrained_def(state, range, constrained);
19510 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019511
19512 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019513 fprintf(state->errout, "constrained: ");
19514 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019515 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019516 if (constrained) {
19517 ids_from_rstate(state, rstate);
19518 cleanup_rstate(state, rstate);
19519 resolve_tangle(state, constrained);
19520 }
19521 return !!constrained;
19522}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019523
Eric Biedermand1ea5392003-06-28 06:49:45 +000019524static int split_ranges(
19525 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019526 char *used, struct live_range *range)
19527{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019528 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019529 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019530 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019531 rstate->passes, tops(range->defs->def->op), range->defs->def);
19532 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019533 if ((range->color == REG_UNNEEDED) ||
19534 (rstate->passes >= rstate->max_passes)) {
19535 return 0;
19536 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019537 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019538
Eric Biedermand1ea5392003-06-28 06:49:45 +000019539 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019540 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019541 * (a) allow me to spill a register or
19542 * (b) allow me to place a value in another register.
19543 *
19544 * So far I don't have a test case for this, the resolving
19545 * of mandatory constraints has solved all of my
19546 * know issues. So I have choosen not to write any
19547 * code until I cat get a better feel for cases where
19548 * it would be useful to have.
19549 *
19550 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019551#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019552#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019553#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019554
Eric Biederman5ade04a2003-10-22 04:03:46 +000019555 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019556 FILE *fp = state->errout;
19557 print_interference_blocks(state, rstate, fp, 0);
19558 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019559 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019560 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019561}
19562
Eric Biederman5ade04a2003-10-22 04:03:46 +000019563static FILE *cgdebug_fp(struct compile_state *state)
19564{
19565 FILE *fp;
19566 fp = 0;
19567 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019568 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019569 }
19570 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019571 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019572 }
19573 return fp;
19574}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019575
Eric Biederman5ade04a2003-10-22 04:03:46 +000019576static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19577{
19578 FILE *fp;
19579 fp = cgdebug_fp(state);
19580 if (fp) {
19581 va_list args;
19582 va_start(args, fmt);
19583 vfprintf(fp, fmt, args);
19584 va_end(args);
19585 }
19586}
19587
19588static void cgdebug_flush(struct compile_state *state)
19589{
19590 FILE *fp;
19591 fp = cgdebug_fp(state);
19592 if (fp) {
19593 fflush(fp);
19594 }
19595}
19596
19597static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19598{
19599 FILE *fp;
19600 fp = cgdebug_fp(state);
19601 if (fp) {
19602 loc(fp, state, ins);
19603 }
19604}
19605
Stefan Reinauer14e22772010-04-27 06:56:47 +000019606static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019607 struct reg_state *rstate, struct live_range *range)
19608{
19609 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019610 struct live_range_def *lrd;
19611 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019612 struct live_range_edge *edge;
19613 char used[MAX_REGISTERS];
19614 struct triple **expr;
19615
Eric Biedermanb138ac82003-04-22 18:44:01 +000019616 /* Instead of doing just the trivial color select here I try
19617 * a few extra things because a good color selection will help reduce
19618 * copies.
19619 */
19620
19621 /* Find the registers currently in use */
19622 memset(used, 0, sizeof(used));
19623 for(edge = range->edges; edge; edge = edge->next) {
19624 if (edge->node->color == REG_UNSET) {
19625 continue;
19626 }
19627 reg_fill_used(state, used, edge->node->color);
19628 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019629
19630 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019631 int i;
19632 i = 0;
19633 for(edge = range->edges; edge; edge = edge->next) {
19634 i++;
19635 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019636 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019637 tops(range->defs->def->op), i);
19638 cgdebug_loc(state, range->defs->def);
19639 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019640 for(i = 0; i < MAX_REGISTERS; i++) {
19641 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019642 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019643 arch_reg_str(i));
19644 }
19645 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019646 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019647
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019648 /* If a color is already assigned see if it will work */
19649 if (range->color != REG_UNSET) {
19650 struct live_range_def *lrd;
19651 if (!used[range->color]) {
19652 return 1;
19653 }
19654 for(edge = range->edges; edge; edge = edge->next) {
19655 if (edge->node->color != range->color) {
19656 continue;
19657 }
19658 warning(state, edge->node->defs->def, "edge: ");
19659 lrd = edge->node->defs;
19660 do {
19661 warning(state, lrd->def, " %p %s",
19662 lrd->def, tops(lrd->def->op));
19663 lrd = lrd->next;
19664 } while(lrd != edge->node->defs);
19665 }
19666 lrd = range->defs;
19667 warning(state, range->defs->def, "def: ");
19668 do {
19669 warning(state, lrd->def, " %p %s",
19670 lrd->def, tops(lrd->def->op));
19671 lrd = lrd->next;
19672 } while(lrd != range->defs);
19673 internal_error(state, range->defs->def,
19674 "live range with already used color %s",
19675 arch_reg_str(range->color));
19676 }
19677
Eric Biedermanb138ac82003-04-22 18:44:01 +000019678 /* If I feed into an expression reuse it's color.
19679 * This should help remove copies in the case of 2 register instructions
19680 * and phi functions.
19681 */
19682 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019683 lrd = live_range_end(state, range, 0);
19684 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19685 entry = lrd->def->use;
19686 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19687 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019688 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019689 insd = &rstate->lrd[entry->member->id];
19690 if (insd->lr->defs == 0) {
19691 continue;
19692 }
19693 if (!phi && (insd->def->op == OP_PHI) &&
19694 !interfere(rstate, range, insd->lr)) {
19695 phi = insd;
19696 }
Eric Biederman530b5192003-07-01 10:05:30 +000019697 if (insd->lr->color == REG_UNSET) {
19698 continue;
19699 }
19700 regcm = insd->lr->classes;
19701 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019702 (used[insd->lr->color])) {
19703 continue;
19704 }
19705 if (interfere(rstate, range, insd->lr)) {
19706 continue;
19707 }
19708 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019709 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019710 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019711 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019712 * of something else that feeds into the phi function.
19713 */
19714 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019715 if (phi->lr->color != REG_UNSET) {
19716 if (used[phi->lr->color]) {
19717 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019718 }
19719 }
19720 else {
19721 expr = triple_rhs(state, phi->def, 0);
19722 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19723 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019724 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019725 if (!*expr) {
19726 continue;
19727 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019728 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019729 if (lr->color == REG_UNSET) {
19730 continue;
19731 }
19732 regcm = lr->classes;
19733 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019734 (used[lr->color])) {
19735 continue;
19736 }
19737 if (interfere(rstate, range, lr)) {
19738 continue;
19739 }
19740 range->color = lr->color;
19741 }
19742 }
19743 }
19744 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019745 lrd = live_range_head(state, range, 0);
19746 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19747 expr = triple_rhs(state, lrd->def, 0);
19748 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019749 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019750 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019751 if (!*expr) {
19752 continue;
19753 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019754 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019755 if (lr->color == REG_UNSET) {
19756 continue;
19757 }
19758 regcm = lr->classes;
19759 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019760 (used[lr->color])) {
19761 continue;
19762 }
19763 if (interfere(rstate, range, lr)) {
19764 continue;
19765 }
19766 range->color = lr->color;
19767 break;
19768 }
19769 }
19770 /* If I have not opportunitically picked a useful color
19771 * pick the first color that is free.
19772 */
19773 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019774 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019775 arch_select_free_register(state, used, range->classes);
19776 }
19777 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019778 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019779 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019780 if (split_ranges(state, rstate, used, range)) {
19781 return 0;
19782 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019783 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019784 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019785 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019786 lrd = edge->node->defs;
19787 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019788 warning(state, lrd->def, " %s %p",
19789 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019790 lrd = lrd->next;
19791 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019792 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019793 warning(state, range->defs->def, "range: ");
19794 lrd = range->defs;
19795 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019796 warning(state, lrd->def, " %s %p",
19797 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019798 lrd = lrd->next;
19799 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019800
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019801 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019802 range->classes);
19803 for(i = 0; i < MAX_REGISTERS; i++) {
19804 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019805 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019806 arch_reg_str(i));
19807 }
19808 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019809 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019810 }
Eric Biederman530b5192003-07-01 10:05:30 +000019811 range->classes &= arch_reg_regcm(state, range->color);
19812 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019813 internal_error(state, range->defs->def, "select_free_color did not?");
19814 }
19815 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019816}
19817
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019818static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019819{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019820 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019821 struct live_range_edge *edge;
19822 struct live_range *range;
19823 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019824 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019825 range = rstate->low;
19826 if (*range->group_prev != range) {
19827 internal_error(state, 0, "lo: *prev != range?");
19828 }
19829 *range->group_prev = range->group_next;
19830 if (range->group_next) {
19831 range->group_next->group_prev = range->group_prev;
19832 }
19833 if (&range->group_next == rstate->low_tail) {
19834 rstate->low_tail = range->group_prev;
19835 }
19836 if (rstate->low == range) {
19837 internal_error(state, 0, "low: next != prev?");
19838 }
19839 }
19840 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019841 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019842 range = rstate->high;
19843 if (*range->group_prev != range) {
19844 internal_error(state, 0, "hi: *prev != range?");
19845 }
19846 *range->group_prev = range->group_next;
19847 if (range->group_next) {
19848 range->group_next->group_prev = range->group_prev;
19849 }
19850 if (&range->group_next == rstate->high_tail) {
19851 rstate->high_tail = range->group_prev;
19852 }
19853 if (rstate->high == range) {
19854 internal_error(state, 0, "high: next != prev?");
19855 }
19856 }
19857 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019858 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019859 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019860 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019861 range->group_prev = 0;
19862 for(edge = range->edges; edge; edge = edge->next) {
19863 struct live_range *node;
19864 node = edge->node;
19865 /* Move nodes from the high to the low list */
19866 if (node->group_prev && (node->color == REG_UNSET) &&
19867 (node->degree == regc_max_size(state, node->classes))) {
19868 if (*node->group_prev != node) {
19869 internal_error(state, 0, "move: *prev != node?");
19870 }
19871 *node->group_prev = node->group_next;
19872 if (node->group_next) {
19873 node->group_next->group_prev = node->group_prev;
19874 }
19875 if (&node->group_next == rstate->high_tail) {
19876 rstate->high_tail = node->group_prev;
19877 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019878 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019879 node->group_prev = rstate->low_tail;
19880 node->group_next = 0;
19881 *rstate->low_tail = node;
19882 rstate->low_tail = &node->group_next;
19883 if (*node->group_prev != node) {
19884 internal_error(state, 0, "move2: *prev != node?");
19885 }
19886 }
19887 node->degree -= 1;
19888 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019889 colored = color_graph(state, rstate);
19890 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019891 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019892 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019893 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019894 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019895 if (colored) {
19896 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19897 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019898 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019899 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019900}
19901
Eric Biedermana96d6a92003-05-13 20:45:19 +000019902static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19903{
19904 struct live_range *lr;
19905 struct live_range_edge *edge;
19906 struct triple *ins, *first;
19907 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019908 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019909 ins = first;
19910 do {
19911 if (triple_is_def(state, ins)) {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019912 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019913 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019914 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019915 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019916 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019917 if (lr->color == REG_UNSET) {
19918 internal_error(state, ins,
19919 "triple without a color");
19920 }
19921 /* Find the registers used by the edges */
19922 memset(used, 0, sizeof(used));
19923 for(edge = lr->edges; edge; edge = edge->next) {
19924 if (edge->node->color == REG_UNSET) {
19925 internal_error(state, 0,
19926 "live range without a color");
19927 }
19928 reg_fill_used(state, used, edge->node->color);
19929 }
19930 if (used[lr->color]) {
19931 internal_error(state, ins,
19932 "triple with already used color");
19933 }
19934 }
19935 ins = ins->next;
19936 } while(ins != first);
19937}
19938
Eric Biedermanb138ac82003-04-22 18:44:01 +000019939static void color_triples(struct compile_state *state, struct reg_state *rstate)
19940{
Eric Biederman90089602004-05-28 14:11:54 +000019941 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019942 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019943 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019944 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019945 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019946 do {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019947 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019948 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019949 "triple without a live range");
19950 }
Eric Biederman90089602004-05-28 14:11:54 +000019951 lrd = &rstate->lrd[ins->id];
19952 lr = lrd->lr;
19953 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019954 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019955 ins = ins->next;
19956 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019957}
19958
Eric Biedermanb138ac82003-04-22 18:44:01 +000019959static struct live_range *merge_sort_lr(
19960 struct live_range *first, struct live_range *last)
19961{
19962 struct live_range *mid, *join, **join_tail, *pick;
19963 size_t size;
19964 size = (last - first) + 1;
19965 if (size >= 2) {
19966 mid = first + size/2;
19967 first = merge_sort_lr(first, mid -1);
19968 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019969
Eric Biedermanb138ac82003-04-22 18:44:01 +000019970 join = 0;
19971 join_tail = &join;
19972 /* merge the two lists */
19973 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019974 if ((first->degree < mid->degree) ||
19975 ((first->degree == mid->degree) &&
19976 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019977 pick = first;
19978 first = first->group_next;
19979 if (first) {
19980 first->group_prev = 0;
19981 }
19982 }
19983 else {
19984 pick = mid;
19985 mid = mid->group_next;
19986 if (mid) {
19987 mid->group_prev = 0;
19988 }
19989 }
19990 pick->group_next = 0;
19991 pick->group_prev = join_tail;
19992 *join_tail = pick;
19993 join_tail = &pick->group_next;
19994 }
19995 /* Splice the remaining list */
19996 pick = (first)? first : mid;
19997 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019998 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019999 pick->group_prev = join_tail;
20000 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020001 }
20002 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020003 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020004 first = 0;
20005 }
20006 join = first;
20007 }
20008 return join;
20009}
20010
Stefan Reinauer14e22772010-04-27 06:56:47 +000020011static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020012 struct reg_state *rstate)
20013{
20014 struct triple *ins, *first;
20015 if (!rstate->defs) {
20016 return;
20017 }
20018 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020019 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020020 FILE *fp = state->dbgout;
20021 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020022 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020023 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020024 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020025 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020026 ins = first;
20027 do {
20028 if (ins->id) {
20029 struct live_range_def *lrd;
20030 lrd = &rstate->lrd[ins->id];
20031 ins->id = lrd->orig_id;
20032 }
20033 ins = ins->next;
20034 } while(ins != first);
20035}
20036
20037static void cleanup_live_edges(struct reg_state *rstate)
20038{
20039 int i;
20040 /* Free the edges on each node */
20041 for(i = 1; i <= rstate->ranges; i++) {
20042 remove_live_edges(rstate, &rstate->lr[i]);
20043 }
20044}
20045
20046static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20047{
20048 cleanup_live_edges(rstate);
20049 xfree(rstate->lrd);
20050 xfree(rstate->lr);
20051
20052 /* Free the variable lifetime information */
20053 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020054 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020055 }
20056 rstate->defs = 0;
20057 rstate->ranges = 0;
20058 rstate->lrd = 0;
20059 rstate->lr = 0;
20060 rstate->blocks = 0;
20061}
20062
Eric Biederman153ea352003-06-20 14:43:20 +000020063static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020064static void allocate_registers(struct compile_state *state)
20065{
20066 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020067 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020068
20069 /* Clear out the reg_state */
20070 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020071 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020072
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020073 do {
20074 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020075 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020076 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020077
Eric Biederman5ade04a2003-10-22 04:03:46 +000020078 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020079 FILE *fp = state->errout;
20080 fprintf(fp, "pass: %d\n", rstate.passes);
20081 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020082 }
Eric Biederman153ea352003-06-20 14:43:20 +000020083
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020084 /* Restore ids */
20085 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020086
Eric Biedermanf96a8102003-06-16 16:57:34 +000020087 /* Cleanup the temporary data structures */
20088 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020089
Eric Biedermanf96a8102003-06-16 16:57:34 +000020090 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020091 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020092
Eric Biedermanf96a8102003-06-16 16:57:34 +000020093 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020094 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020095
Eric Biederman153ea352003-06-20 14:43:20 +000020096 /* Fix two simultaneous uses of the same register.
20097 * In a few pathlogical cases a partial untangle moves
20098 * the tangle to a part of the graph we won't revisit.
20099 * So we keep looping until we have no more tangle fixes
20100 * to apply.
20101 */
20102 do {
20103 tangles = correct_tangles(state, rstate.blocks);
20104 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020105
Stefan Reinauer14e22772010-04-27 06:56:47 +000020106
Eric Biederman90089602004-05-28 14:11:54 +000020107 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020108 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020109
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020110 /* Allocate and initialize the live ranges */
20111 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020112
Stefan Reinauer14e22772010-04-27 06:56:47 +000020113 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020114 * buys me nothing. The code is left this way in case
20115 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020116 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020117 */
20118 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020119 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020120 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020121 }
20122
Eric Biederman153ea352003-06-20 14:43:20 +000020123 /* Remove any previous live edge calculations */
20124 cleanup_live_edges(&rstate);
20125
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020126 /* Compute the interference graph */
20127 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020128 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020129 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020130
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020131 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020132 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020133 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020134 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020135 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020136 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020137 print_interference_ins, &rstate);
20138 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020139
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020140 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020141
Eric Biederman5ade04a2003-10-22 04:03:46 +000020142 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020143 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020144 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020145 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020146
Eric Biederman83b991a2003-10-11 06:20:25 +000020147#if DEBUG_CONSISTENCY > 1
20148# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020149 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020150# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020151 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020152 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020153 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020154 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020155# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020156 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020157#endif
20158#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020159
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020160 /* Build the groups low and high. But with the nodes
20161 * first sorted by degree order.
20162 */
20163 rstate.low_tail = &rstate.low;
20164 rstate.high_tail = &rstate.high;
20165 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20166 if (rstate.high) {
20167 rstate.high->group_prev = &rstate.high;
20168 }
20169 for(point = &rstate.high; *point; point = &(*point)->group_next)
20170 ;
20171 rstate.high_tail = point;
20172 /* Walk through the high list and move everything that needs
20173 * to be onto low.
20174 */
20175 for(point = &rstate.high; *point; point = next) {
20176 struct live_range *range;
20177 next = &(*point)->group_next;
20178 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020179
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020180 /* If it has a low degree or it already has a color
20181 * place the node in low.
20182 */
20183 if ((range->degree < regc_max_size(state, range->classes)) ||
20184 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020185 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020186 range - rstate.lr, range->degree,
20187 (range->color != REG_UNSET) ? " (colored)": "");
20188 *range->group_prev = range->group_next;
20189 if (range->group_next) {
20190 range->group_next->group_prev = range->group_prev;
20191 }
20192 if (&range->group_next == rstate.high_tail) {
20193 rstate.high_tail = range->group_prev;
20194 }
20195 range->group_prev = rstate.low_tail;
20196 range->group_next = 0;
20197 *rstate.low_tail = range;
20198 rstate.low_tail = &range->group_next;
20199 next = point;
20200 }
20201 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020202 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020203 range - rstate.lr, range->degree,
20204 (range->color != REG_UNSET) ? " (colored)": "");
20205 }
20206 }
20207 /* Color the live_ranges */
20208 colored = color_graph(state, &rstate);
20209 rstate.passes++;
20210 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020211
Eric Biedermana96d6a92003-05-13 20:45:19 +000020212 /* Verify the graph was properly colored */
20213 verify_colors(state, &rstate);
20214
Eric Biedermanb138ac82003-04-22 18:44:01 +000020215 /* Move the colors from the graph to the triples */
20216 color_triples(state, &rstate);
20217
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020218 /* Cleanup the temporary data structures */
20219 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020220
20221 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020222 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020223}
20224
20225/* Sparce Conditional Constant Propogation
20226 * =========================================
20227 */
20228struct ssa_edge;
20229struct flow_block;
20230struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020231 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020232 struct triple *def;
20233 struct ssa_edge *out;
20234 struct flow_block *fblock;
20235 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020236 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020237 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020238 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020239 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020240};
20241struct ssa_edge {
20242 struct lattice_node *src;
20243 struct lattice_node *dst;
20244 struct ssa_edge *work_next;
20245 struct ssa_edge *work_prev;
20246 struct ssa_edge *out_next;
20247};
20248struct flow_edge {
20249 struct flow_block *src;
20250 struct flow_block *dst;
20251 struct flow_edge *work_next;
20252 struct flow_edge *work_prev;
20253 struct flow_edge *in_next;
20254 struct flow_edge *out_next;
20255 int executable;
20256};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020257#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020258struct flow_block {
20259 struct block *block;
20260 struct flow_edge *in;
20261 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020262 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020263};
20264
20265struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020266 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020267 struct lattice_node *lattice;
20268 struct ssa_edge *ssa_edges;
20269 struct flow_block *flow_blocks;
20270 struct flow_edge *flow_work_list;
20271 struct ssa_edge *ssa_work_list;
20272};
20273
20274
Eric Biederman90089602004-05-28 14:11:54 +000020275static int is_scc_const(struct compile_state *state, struct triple *ins)
20276{
20277 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20278}
20279
20280static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20281{
20282 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20283}
20284
20285static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20286{
20287 return is_scc_const(state, lnode->val);
20288}
20289
20290static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20291{
20292 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20293}
20294
Stefan Reinauer14e22772010-04-27 06:56:47 +000020295static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020296 struct flow_edge *fedge)
20297{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020298 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020299 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020300 fedge,
20301 fedge->src->block?fedge->src->block->last->id: 0,
20302 fedge->dst->block?fedge->dst->block->first->id: 0);
20303 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020304 if ((fedge == scc->flow_work_list) ||
20305 (fedge->work_next != fedge) ||
20306 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020307
20308 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020309 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020310 fedge);
20311 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020312 return;
20313 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020314 if (!scc->flow_work_list) {
20315 scc->flow_work_list = fedge;
20316 fedge->work_next = fedge->work_prev = fedge;
20317 }
20318 else {
20319 struct flow_edge *ftail;
20320 ftail = scc->flow_work_list->work_prev;
20321 fedge->work_next = ftail->work_next;
20322 fedge->work_prev = ftail;
20323 fedge->work_next->work_prev = fedge;
20324 fedge->work_prev->work_next = fedge;
20325 }
20326}
20327
20328static struct flow_edge *scc_next_fedge(
20329 struct compile_state *state, struct scc_state *scc)
20330{
20331 struct flow_edge *fedge;
20332 fedge = scc->flow_work_list;
20333 if (fedge) {
20334 fedge->work_next->work_prev = fedge->work_prev;
20335 fedge->work_prev->work_next = fedge->work_next;
20336 if (fedge->work_next != fedge) {
20337 scc->flow_work_list = fedge->work_next;
20338 } else {
20339 scc->flow_work_list = 0;
20340 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020341 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020342 }
20343 return fedge;
20344}
20345
20346static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20347 struct ssa_edge *sedge)
20348{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020349 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020350 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20351 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020352 sedge->src->def->id,
20353 sedge->dst->def->id);
20354 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020355 if ((sedge == scc->ssa_work_list) ||
20356 (sedge->work_next != sedge) ||
20357 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020358
20359 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020360 fprintf(state->errout, "dupped sedge: %5ld\n",
20361 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020362 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020363 return;
20364 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020365 if (!scc->ssa_work_list) {
20366 scc->ssa_work_list = sedge;
20367 sedge->work_next = sedge->work_prev = sedge;
20368 }
20369 else {
20370 struct ssa_edge *stail;
20371 stail = scc->ssa_work_list->work_prev;
20372 sedge->work_next = stail->work_next;
20373 sedge->work_prev = stail;
20374 sedge->work_next->work_prev = sedge;
20375 sedge->work_prev->work_next = sedge;
20376 }
20377}
20378
20379static struct ssa_edge *scc_next_sedge(
20380 struct compile_state *state, struct scc_state *scc)
20381{
20382 struct ssa_edge *sedge;
20383 sedge = scc->ssa_work_list;
20384 if (sedge) {
20385 sedge->work_next->work_prev = sedge->work_prev;
20386 sedge->work_prev->work_next = sedge->work_next;
20387 if (sedge->work_next != sedge) {
20388 scc->ssa_work_list = sedge->work_next;
20389 } else {
20390 scc->ssa_work_list = 0;
20391 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020392 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020393 }
20394 return sedge;
20395}
20396
20397static void initialize_scc_state(
20398 struct compile_state *state, struct scc_state *scc)
20399{
20400 int ins_count, ssa_edge_count;
20401 int ins_index, ssa_edge_index, fblock_index;
20402 struct triple *first, *ins;
20403 struct block *block;
20404 struct flow_block *fblock;
20405
20406 memset(scc, 0, sizeof(*scc));
20407
20408 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020409 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020410 ins = first;
20411 ins_count = ssa_edge_count = 0;
20412 do {
20413 struct triple_set *edge;
20414 ins_count += 1;
20415 for(edge = ins->use; edge; edge = edge->next) {
20416 ssa_edge_count++;
20417 }
20418 ins = ins->next;
20419 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020420 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020421 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20422 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020423 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020424 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020425 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020426 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020427 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020428 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020429 scc->flow_blocks =
20430 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020431 "flow_blocks");
20432
20433 /* Initialize pass one collect up the nodes */
20434 fblock = 0;
20435 block = 0;
20436 ins_index = ssa_edge_index = fblock_index = 0;
20437 ins = first;
20438 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020439 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20440 block = ins->u.block;
20441 if (!block) {
20442 internal_error(state, ins, "label without block");
20443 }
20444 fblock_index += 1;
20445 block->vertex = fblock_index;
20446 fblock = &scc->flow_blocks[fblock_index];
20447 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020448 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20449 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020450 }
20451 {
20452 struct lattice_node *lnode;
20453 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020454 lnode = &scc->lattice[ins_index];
20455 lnode->def = ins;
20456 lnode->out = 0;
20457 lnode->fblock = fblock;
20458 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020459 if (lnode->val->op == OP_UNKNOWNVAL) {
20460 lnode->val = 0; /* LATTICE LOW by definition */
20461 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020462 lnode->old_id = ins->id;
20463 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020464 }
20465 ins = ins->next;
20466 } while(ins != first);
20467 /* Initialize pass two collect up the edges */
20468 block = 0;
20469 fblock = 0;
20470 ins = first;
20471 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020472 {
20473 struct triple_set *edge;
20474 struct ssa_edge **stail;
20475 struct lattice_node *lnode;
20476 lnode = &scc->lattice[ins->id];
20477 lnode->out = 0;
20478 stail = &lnode->out;
20479 for(edge = ins->use; edge; edge = edge->next) {
20480 struct ssa_edge *sedge;
20481 ssa_edge_index += 1;
20482 sedge = &scc->ssa_edges[ssa_edge_index];
20483 *stail = sedge;
20484 stail = &sedge->out_next;
20485 sedge->src = lnode;
20486 sedge->dst = &scc->lattice[edge->member->id];
20487 sedge->work_next = sedge->work_prev = sedge;
20488 sedge->out_next = 0;
20489 }
20490 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020491 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20492 struct flow_edge *fedge, **ftail;
20493 struct block_set *bedge;
20494 block = ins->u.block;
20495 fblock = &scc->flow_blocks[block->vertex];
20496 fblock->in = 0;
20497 fblock->out = 0;
20498 ftail = &fblock->out;
20499
20500 fedge = fblock->edges;
20501 bedge = block->edges;
20502 for(; bedge; bedge = bedge->next, fedge++) {
20503 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20504 if (fedge->dst->block != bedge->member) {
20505 internal_error(state, 0, "block mismatch");
20506 }
20507 *ftail = fedge;
20508 ftail = &fedge->out_next;
20509 fedge->out_next = 0;
20510 }
20511 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20512 fedge->src = fblock;
20513 fedge->work_next = fedge->work_prev = fedge;
20514 fedge->executable = 0;
20515 }
20516 }
20517 ins = ins->next;
20518 } while (ins != first);
20519 block = 0;
20520 fblock = 0;
20521 ins = first;
20522 do {
20523 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20524 struct flow_edge **ftail;
20525 struct block_set *bedge;
20526 block = ins->u.block;
20527 fblock = &scc->flow_blocks[block->vertex];
20528 ftail = &fblock->in;
20529 for(bedge = block->use; bedge; bedge = bedge->next) {
20530 struct block *src_block;
20531 struct flow_block *sfblock;
20532 struct flow_edge *sfedge;
20533 src_block = bedge->member;
20534 sfblock = &scc->flow_blocks[src_block->vertex];
20535 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20536 if (sfedge->dst == fblock) {
20537 break;
20538 }
20539 }
20540 if (!sfedge) {
20541 internal_error(state, 0, "edge mismatch");
20542 }
20543 *ftail = sfedge;
20544 ftail = &sfedge->in_next;
20545 sfedge->in_next = 0;
20546 }
20547 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020548 ins = ins->next;
20549 } while(ins != first);
20550 /* Setup a dummy block 0 as a node above the start node */
20551 {
20552 struct flow_block *fblock, *dst;
20553 struct flow_edge *fedge;
20554 fblock = &scc->flow_blocks[0];
20555 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020556 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020557 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020558 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020559 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020560 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020561 fedge->src = fblock;
20562 fedge->dst = dst;
20563 fedge->work_next = fedge;
20564 fedge->work_prev = fedge;
20565 fedge->in_next = fedge->dst->in;
20566 fedge->out_next = 0;
20567 fedge->executable = 0;
20568 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020569
Eric Biedermanb138ac82003-04-22 18:44:01 +000020570 /* Initialize the work lists */
20571 scc->flow_work_list = 0;
20572 scc->ssa_work_list = 0;
20573 scc_add_fedge(state, scc, fedge);
20574 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020575 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020576 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020577 ins_index, ssa_edge_index, fblock_index);
20578 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020579}
20580
Stefan Reinauer14e22772010-04-27 06:56:47 +000020581
Eric Biedermanb138ac82003-04-22 18:44:01 +000020582static void free_scc_state(
20583 struct compile_state *state, struct scc_state *scc)
20584{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020585 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020586 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020587 struct flow_block *fblock;
20588 fblock = &scc->flow_blocks[i];
20589 if (fblock->edges) {
20590 xfree(fblock->edges);
20591 fblock->edges = 0;
20592 }
20593 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020594 xfree(scc->flow_blocks);
20595 xfree(scc->ssa_edges);
20596 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020597
Eric Biedermanb138ac82003-04-22 18:44:01 +000020598}
20599
20600static struct lattice_node *triple_to_lattice(
20601 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20602{
20603 if (ins->id <= 0) {
20604 internal_error(state, ins, "bad id");
20605 }
20606 return &scc->lattice[ins->id];
20607}
20608
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020609static struct triple *preserve_lval(
20610 struct compile_state *state, struct lattice_node *lnode)
20611{
20612 struct triple *old;
20613 /* Preserve the original value */
20614 if (lnode->val) {
20615 old = dup_triple(state, lnode->val);
20616 if (lnode->val != lnode->def) {
20617 xfree(lnode->val);
20618 }
20619 lnode->val = 0;
20620 } else {
20621 old = 0;
20622 }
20623 return old;
20624}
20625
Stefan Reinauer14e22772010-04-27 06:56:47 +000020626static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020627 struct triple *old, struct lattice_node *lnode)
20628{
20629 int changed;
20630 /* See if the lattice value has changed */
20631 changed = 1;
20632 if (!old && !lnode->val) {
20633 changed = 0;
20634 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020635 if (changed &&
20636 lnode->val && old &&
20637 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020638 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020639 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20640 changed = 0;
20641 }
20642 if (old) {
20643 xfree(old);
20644 }
20645 return changed;
20646
20647}
20648
Eric Biederman5ade04a2003-10-22 04:03:46 +000020649static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020650 struct compile_state *state, struct scc_state *scc,
20651 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020652{
Eric Biederman90089602004-05-28 14:11:54 +000020653 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20654 display_triple_changes(state->errout, lnode->val, lnode->def);
20655 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020656 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020657 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020658 struct triple *val, **expr;
20659 val = lnode->val? lnode->val : lnode->def;
20660 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020661 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020662 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20663 lnode->def->id,
20664 tops(lnode->def->op));
20665 expr = triple_rhs(state, lnode->def, 0);
20666 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20667 if (*expr) {
20668 fprintf(fp, " %d", (*expr)->id);
20669 }
20670 }
20671 if (val->op == OP_INTCONST) {
20672 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20673 }
20674 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020675 (is_lattice_hi(state, lnode)? "hi":
20676 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020677 changed? "changed" : ""
20678 );
20679 }
20680}
20681
Eric Biedermanb138ac82003-04-22 18:44:01 +000020682static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20683 struct lattice_node *lnode)
20684{
20685 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020686 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020687 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020688 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020689
Eric Biedermanb138ac82003-04-22 18:44:01 +000020690 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020691 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020692
Eric Biedermanb138ac82003-04-22 18:44:01 +000020693 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020695 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020696 scratch->next = scratch;
20697 scratch->prev = scratch;
20698 scratch->use = 0;
20699
Eric Biederman90089602004-05-28 14:11:54 +000020700 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020701 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702 dexpr = &lnode->def->param[i];
20703 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020704 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020705 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20706 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020707 *dexpr) {
20708 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020709 tmp = triple_to_lattice(state, scc, *dexpr);
20710 *vexpr = (tmp->val)? tmp->val : tmp->def;
20711 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020712 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020713 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020714 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020715 }
20716 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020717#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020718#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020719#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020720 /* So far it looks like only the strength reduction
20721 * optimization are things I need to worry about.
20722 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020723 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020724 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020725 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020726 internal_error(state, lnode->def, "scratch used?");
20727 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020728 if ((scratch->prev != scratch) ||
20729 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020730 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020731 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020732 internal_error(state, lnode->def, "scratch in list?");
20733 }
20734 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020735 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020736 for(i = 0; i < count; i++) {
20737 vexpr = &scratch->param[i];
20738 if (*vexpr) {
20739 unuse_triple(*vexpr, scratch);
20740 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020741 }
Eric Biederman90089602004-05-28 14:11:54 +000020742 if (lnode->val->op == OP_UNKNOWNVAL) {
20743 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020744 }
Eric Biederman90089602004-05-28 14:11:54 +000020745 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020746 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020747 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020748 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020749 count * sizeof(lnode->val->param[0])) == 0) &&
20750 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020751 lnode->val = lnode->def;
20752 }
Eric Biederman90089602004-05-28 14:11:54 +000020753 /* Only allow lattice high when all of my inputs
20754 * are also lattice high. Occassionally I can
20755 * have constants with a lattice low input, so
20756 * I do not need to check that case.
20757 */
20758 if (is_lattice_hi(state, lnode)) {
20759 struct lattice_node *tmp;
20760 int rhs;
20761 rhs = lnode->val->rhs;
20762 for(i = 0; i < rhs; i++) {
20763 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20764 if (!is_lattice_hi(state, tmp)) {
20765 lnode->val = 0;
20766 break;
20767 }
20768 }
20769 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020770 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020771 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020772 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020773 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020774 lnode->val = 0;
20775 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020776 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020777 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020778 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020779 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020780 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020781 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020782 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020783#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020784#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020785#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020786 if (changed) {
20787 internal_warning(state, lnode->def, "non def changes value?");
20788 }
20789 lnode->val = 0;
20790 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020791
Eric Biederman83b991a2003-10-11 06:20:25 +000020792 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020793 if (lnode->val != scratch) {
20794 xfree(scratch);
20795 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020796
Eric Biedermanb138ac82003-04-22 18:44:01 +000020797 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020798}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020799
Eric Biederman90089602004-05-28 14:11:54 +000020800
20801static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020802 struct lattice_node *lnode)
20803{
20804 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020805 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020806 int changed;
20807
20808 /* Update the branch value */
20809 changed = compute_lnode_val(state, scc, lnode);
20810 scc_debug_lnode(state, scc, lnode, changed);
20811
20812 /* This only applies to conditional branches */
20813 if (!triple_is_cbranch(state, lnode->def)) {
20814 internal_error(state, lnode->def, "not a conditional branch");
20815 }
20816
Eric Biederman5ade04a2003-10-22 04:03:46 +000020817 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020818 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020819 FILE *fp = state->errout;
20820 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020821 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020822 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020823
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020825 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020826 }
Eric Biederman90089602004-05-28 14:11:54 +000020827 fprintf(fp, " )");
20828 if (lnode->def->rhs > 0) {
20829 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020830 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020831 }
Eric Biederman90089602004-05-28 14:11:54 +000020832 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020833 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020834 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020835 for(left = cond->fblock->out; left; left = left->out_next) {
20836 if (left->dst->block->first == lnode->def->next) {
20837 break;
20838 }
20839 }
20840 if (!left) {
20841 internal_error(state, lnode->def, "Cannot find left branch edge");
20842 }
20843 for(right = cond->fblock->out; right; right = right->out_next) {
20844 if (right->dst->block->first == TARG(lnode->def, 0)) {
20845 break;
20846 }
20847 }
20848 if (!right) {
20849 internal_error(state, lnode->def, "Cannot find right branch edge");
20850 }
Eric Biederman90089602004-05-28 14:11:54 +000020851 /* I should only come here if the controlling expressions value
20852 * has changed, which means it must be either a constant or lo.
20853 */
20854 if (is_lattice_hi(state, cond)) {
20855 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020856 return;
20857 }
Eric Biederman90089602004-05-28 14:11:54 +000020858 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020859 scc_add_fedge(state, scc, left);
20860 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020861 }
20862 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020863 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020864 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020865 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020866 }
20867
20868}
20869
Eric Biederman90089602004-05-28 14:11:54 +000020870
Stefan Reinauer14e22772010-04-27 06:56:47 +000020871static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020872 struct scc_state *scc, struct ssa_edge *sedge)
20873{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020874 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020875 scc_visit_cbranch(state, scc, sedge->dst);
20876 }
20877 else if (triple_is_def(state, sedge->dst->def)) {
20878 scc_add_sedge(state, scc, sedge);
20879 }
20880}
20881
Stefan Reinauer14e22772010-04-27 06:56:47 +000020882static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020883 struct lattice_node *lnode)
20884{
20885 struct lattice_node *tmp;
20886 struct triple **slot, *old;
20887 struct flow_edge *fedge;
20888 int changed;
20889 int index;
20890 if (lnode->def->op != OP_PHI) {
20891 internal_error(state, lnode->def, "not phi");
20892 }
20893 /* Store the original value */
20894 old = preserve_lval(state, lnode);
20895
20896 /* default to lattice high */
20897 lnode->val = lnode->def;
20898 slot = &RHS(lnode->def, 0);
20899 index = 0;
20900 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20901 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020902 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020903 index,
20904 fedge->dst->block->vertex,
20905 fedge->executable
20906 );
20907 }
20908 if (!fedge->executable) {
20909 continue;
20910 }
20911 if (!slot[index]) {
20912 internal_error(state, lnode->def, "no phi value");
20913 }
20914 tmp = triple_to_lattice(state, scc, slot[index]);
20915 /* meet(X, lattice low) = lattice low */
20916 if (is_lattice_lo(state, tmp)) {
20917 lnode->val = 0;
20918 }
20919 /* meet(X, lattice high) = X */
20920 else if (is_lattice_hi(state, tmp)) {
20921 lnode->val = lnode->val;
20922 }
20923 /* meet(lattice high, X) = X */
20924 else if (is_lattice_hi(state, lnode)) {
20925 lnode->val = dup_triple(state, tmp->val);
20926 /* Only change the type if necessary */
20927 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20928 lnode->val->type = lnode->def->type;
20929 }
20930 }
20931 /* meet(const, const) = const or lattice low */
20932 else if (!constants_equal(state, lnode->val, tmp->val)) {
20933 lnode->val = 0;
20934 }
20935
20936 /* meet(lattice low, X) = lattice low */
20937 if (is_lattice_lo(state, lnode)) {
20938 lnode->val = 0;
20939 break;
20940 }
20941 }
20942 changed = lval_changed(state, old, lnode);
20943 scc_debug_lnode(state, scc, lnode, changed);
20944
20945 /* If the lattice value has changed update the work lists. */
20946 if (changed) {
20947 struct ssa_edge *sedge;
20948 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20949 scc_add_sedge_dst(state, scc, sedge);
20950 }
20951 }
20952}
20953
20954
Eric Biedermanb138ac82003-04-22 18:44:01 +000020955static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20956 struct lattice_node *lnode)
20957{
20958 int changed;
20959
Eric Biederman90089602004-05-28 14:11:54 +000020960 if (!triple_is_def(state, lnode->def)) {
20961 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020962 }
Eric Biederman90089602004-05-28 14:11:54 +000020963 changed = compute_lnode_val(state, scc, lnode);
20964 scc_debug_lnode(state, scc, lnode, changed);
20965
20966 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020967 struct ssa_edge *sedge;
20968 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020969 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020970 }
20971 }
20972}
20973
20974static void scc_writeback_values(
20975 struct compile_state *state, struct scc_state *scc)
20976{
20977 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020978 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020979 ins = first;
20980 do {
20981 struct lattice_node *lnode;
20982 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020983 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020984 if (is_lattice_hi(state, lnode) &&
20985 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020986 {
20987 struct flow_edge *fedge;
20988 int executable;
20989 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020990 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020991 !executable && fedge; fedge = fedge->in_next) {
20992 executable |= fedge->executable;
20993 }
20994 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020995 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020996 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020997 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020998 tops(lnode->def->op),
20999 tops(lnode->val->op));
21000 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021001 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021002 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021003
Eric Biederman83b991a2003-10-11 06:20:25 +000021004 /* Restore id */
21005 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021006 if (lnode->val && (lnode->val != ins)) {
21007 /* See if it something I know how to write back */
21008 switch(lnode->val->op) {
21009 case OP_INTCONST:
21010 mkconst(state, ins, lnode->val->u.cval);
21011 break;
21012 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021013 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021014 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021015 break;
21016 default:
21017 /* By default don't copy the changes,
21018 * recompute them in place instead.
21019 */
21020 simplify(state, ins);
21021 break;
21022 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021023 if (is_const(lnode->val) &&
21024 !constants_equal(state, lnode->val, ins)) {
21025 internal_error(state, 0, "constants not equal");
21026 }
21027 /* Free the lattice nodes */
21028 xfree(lnode->val);
21029 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021030 }
21031 ins = ins->next;
21032 } while(ins != first);
21033}
21034
21035static void scc_transform(struct compile_state *state)
21036{
21037 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021038 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21039 return;
21040 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021041
21042 initialize_scc_state(state, &scc);
21043
21044 while(scc.flow_work_list || scc.ssa_work_list) {
21045 struct flow_edge *fedge;
21046 struct ssa_edge *sedge;
21047 struct flow_edge *fptr;
21048 while((fedge = scc_next_fedge(state, &scc))) {
21049 struct block *block;
21050 struct triple *ptr;
21051 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021052 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021053 int done;
21054 if (fedge->executable) {
21055 continue;
21056 }
21057 if (!fedge->dst) {
21058 internal_error(state, 0, "fedge without dst");
21059 }
21060 if (!fedge->src) {
21061 internal_error(state, 0, "fedge without src");
21062 }
21063 fedge->executable = 1;
21064 fblock = fedge->dst;
21065 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021066 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021067 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21068 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021069 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021070 }
21071 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021072
Eric Biederman5ade04a2003-10-22 04:03:46 +000021073 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021074 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021075 block->vertex, reps);
21076 }
21077
Eric Biedermanb138ac82003-04-22 18:44:01 +000021078 done = 0;
21079 for(ptr = block->first; !done; ptr = ptr->next) {
21080 struct lattice_node *lnode;
21081 done = (ptr == block->last);
21082 lnode = &scc.lattice[ptr->id];
21083 if (ptr->op == OP_PHI) {
21084 scc_visit_phi(state, &scc, lnode);
21085 }
Eric Biederman90089602004-05-28 14:11:54 +000021086 else if ((reps == 1) && triple_is_def(state, ptr))
21087 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021088 scc_visit_expr(state, &scc, lnode);
21089 }
21090 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021091 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021092 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021093 struct flow_edge *out;
21094 for(out = fblock->out; out; out = out->out_next) {
21095 scc_add_fedge(state, &scc, out);
21096 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021097 }
21098 }
21099 while((sedge = scc_next_sedge(state, &scc))) {
21100 struct lattice_node *lnode;
21101 struct flow_block *fblock;
21102 lnode = sedge->dst;
21103 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021104
21105 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021106 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021107 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021108 sedge->src->def->id,
21109 sedge->dst->def->id);
21110 }
21111
Eric Biedermanb138ac82003-04-22 18:44:01 +000021112 if (lnode->def->op == OP_PHI) {
21113 scc_visit_phi(state, &scc, lnode);
21114 }
21115 else {
21116 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21117 if (fptr->executable) {
21118 break;
21119 }
21120 }
21121 if (fptr) {
21122 scc_visit_expr(state, &scc, lnode);
21123 }
21124 }
21125 }
21126 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021127
Eric Biedermanb138ac82003-04-22 18:44:01 +000021128 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021129 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021130 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021131
Eric Biederman90089602004-05-28 14:11:54 +000021132 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021133}
21134
21135
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021136static void transform_to_arch_instructions(struct compile_state *state)
21137{
21138 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021139 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021140 ins = first;
21141 do {
21142 ins = transform_to_arch_instruction(state, ins);
21143 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021144
Eric Biederman90089602004-05-28 14:11:54 +000021145 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021146}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021147
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021148#if DEBUG_CONSISTENCY
21149static void verify_uses(struct compile_state *state)
21150{
21151 struct triple *first, *ins;
21152 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021153 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021154 ins = first;
21155 do {
21156 struct triple **expr;
21157 expr = triple_rhs(state, ins, 0);
21158 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021159 struct triple *rhs;
21160 rhs = *expr;
21161 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021162 if (set->member == ins) {
21163 break;
21164 }
21165 }
21166 if (!set) {
21167 internal_error(state, ins, "rhs not used");
21168 }
21169 }
21170 expr = triple_lhs(state, ins, 0);
21171 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021172 struct triple *lhs;
21173 lhs = *expr;
21174 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021175 if (set->member == ins) {
21176 break;
21177 }
21178 }
21179 if (!set) {
21180 internal_error(state, ins, "lhs not used");
21181 }
21182 }
Eric Biederman90089602004-05-28 14:11:54 +000021183 expr = triple_misc(state, ins, 0);
21184 if (ins->op != OP_PHI) {
21185 for(; expr; expr = triple_targ(state, ins, expr)) {
21186 struct triple *misc;
21187 misc = *expr;
21188 for(set = misc?misc->use:0; set; set = set->next) {
21189 if (set->member == ins) {
21190 break;
21191 }
21192 }
21193 if (!set) {
21194 internal_error(state, ins, "misc not used");
21195 }
21196 }
21197 }
21198 if (!triple_is_ret(state, ins)) {
21199 expr = triple_targ(state, ins, 0);
21200 for(; expr; expr = triple_targ(state, ins, expr)) {
21201 struct triple *targ;
21202 targ = *expr;
21203 for(set = targ?targ->use:0; set; set = set->next) {
21204 if (set->member == ins) {
21205 break;
21206 }
21207 }
21208 if (!set) {
21209 internal_error(state, ins, "targ not used");
21210 }
21211 }
21212 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021213 ins = ins->next;
21214 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021215
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021216}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021217static void verify_blocks_present(struct compile_state *state)
21218{
21219 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021220 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021221 return;
21222 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021223 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021224 ins = first;
21225 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021226 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021227 if (triple_stores_block(state, ins)) {
21228 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021229 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021230 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021231 }
21232 }
21233 ins = ins->next;
21234 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021235
21236
Eric Biedermand1ea5392003-06-28 06:49:45 +000021237}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021238
21239static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21240{
21241 struct block_set *bedge;
21242 struct block *targ;
21243 targ = block_of_triple(state, edge);
21244 for(bedge = block->edges; bedge; bedge = bedge->next) {
21245 if (bedge->member == targ) {
21246 return 1;
21247 }
21248 }
21249 return 0;
21250}
21251
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021252static void verify_blocks(struct compile_state *state)
21253{
21254 struct triple *ins;
21255 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021256 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021257 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021258 if (!block) {
21259 return;
21260 }
Eric Biederman530b5192003-07-01 10:05:30 +000021261 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021262 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021263 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021264 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021265 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021266 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021267 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021268 internal_error(state, ins, "inconsitent block specified");
21269 }
Eric Biederman530b5192003-07-01 10:05:30 +000021270 valid_ins(state, ins);
21271 }
21272 users = 0;
21273 for(user = block->use; user; user = user->next) {
21274 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021275 if (!user->member->first) {
21276 internal_error(state, block->first, "user is empty");
21277 }
Eric Biederman90089602004-05-28 14:11:54 +000021278 if ((block == state->bb.last_block) &&
21279 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021280 continue;
21281 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021282 for(edge = user->member->edges; edge; edge = edge->next) {
21283 if (edge->member == block) {
21284 break;
21285 }
21286 }
21287 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021288 internal_error(state, user->member->first,
21289 "user does not use block");
21290 }
21291 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021292 if (triple_is_branch(state, block->last)) {
21293 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021294 expr = triple_edge_targ(state, block->last, 0);
21295 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021296 if (*expr && !edge_present(state, block, *expr)) {
21297 internal_error(state, block->last, "no edge to targ");
21298 }
21299 }
Eric Biederman530b5192003-07-01 10:05:30 +000021300 }
Eric Biederman90089602004-05-28 14:11:54 +000021301 if (!triple_is_ubranch(state, block->last) &&
21302 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021303 !edge_present(state, block, block->last->next)) {
21304 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021305 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021306 for(edge = block->edges; edge; edge = edge->next) {
21307 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021308 if (user->member == block) {
21309 break;
21310 }
21311 }
21312 if (!user || user->member != block) {
21313 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021314 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021315 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021316 if (!edge->member->first) {
21317 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021318 }
Eric Biederman530b5192003-07-01 10:05:30 +000021319 }
21320 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021321 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021322 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021323 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021324 }
21325 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021326 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021327 "cannot find next block");
21328 }
21329 block = block->last->next->u.block;
21330 if (!block) {
21331 internal_error(state, block->last->next,
21332 "bad next block");
21333 }
Eric Biederman90089602004-05-28 14:11:54 +000021334 } while(block != state->bb.first_block);
21335 if (blocks != state->bb.last_vertex) {
21336 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21337 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021338 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021339}
21340
21341static void verify_domination(struct compile_state *state)
21342{
21343 struct triple *first, *ins;
21344 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021345 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021346 return;
21347 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021348
Eric Biederman83b991a2003-10-11 06:20:25 +000021349 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021350 ins = first;
21351 do {
21352 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021353 struct triple **slot;
21354 struct triple *use_point;
21355 int i, zrhs;
21356 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021357 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021358 slot = &RHS(set->member, 0);
21359 /* See if the use is on the right hand side */
21360 for(i = 0; i < zrhs; i++) {
21361 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021362 break;
21363 }
21364 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021365 if (i < zrhs) {
21366 use_point = set->member;
21367 if (set->member->op == OP_PHI) {
21368 struct block_set *bset;
21369 int edge;
21370 bset = set->member->u.block->use;
21371 for(edge = 0; bset && (edge < i); edge++) {
21372 bset = bset->next;
21373 }
21374 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021375 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021376 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021377 }
21378 use_point = bset->member->last;
21379 }
21380 }
21381 if (use_point &&
21382 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021383 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021384 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021385 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021386 }
21387 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021388 internal_error(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 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021391 }
21392 }
21393 ins = ins->next;
21394 } while(ins != first);
21395}
21396
Eric Biederman83b991a2003-10-11 06:20:25 +000021397static void verify_rhs(struct compile_state *state)
21398{
21399 struct triple *first, *ins;
21400 first = state->first;
21401 ins = first;
21402 do {
21403 struct triple **slot;
21404 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021405 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021406 slot = &RHS(ins, 0);
21407 for(i = 0; i < zrhs; i++) {
21408 if (slot[i] == 0) {
21409 internal_error(state, ins,
21410 "missing rhs %d on %s",
21411 i, tops(ins->op));
21412 }
21413 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21414 internal_error(state, ins,
21415 "ins == rhs[%d] on %s",
21416 i, tops(ins->op));
21417 }
21418 }
21419 ins = ins->next;
21420 } while(ins != first);
21421}
21422
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021423static void verify_piece(struct compile_state *state)
21424{
21425 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021426 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021427 ins = first;
21428 do {
21429 struct triple *ptr;
21430 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021431 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021432 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21433 if (ptr != LHS(ins, i)) {
21434 internal_error(state, ins, "malformed lhs on %s",
21435 tops(ins->op));
21436 }
21437 if (ptr->op != OP_PIECE) {
21438 internal_error(state, ins, "bad lhs op %s at %d on %s",
21439 tops(ptr->op), i, tops(ins->op));
21440 }
21441 if (ptr->u.cval != i) {
21442 internal_error(state, ins, "bad u.cval of %d %d expected",
21443 ptr->u.cval, i);
21444 }
21445 }
21446 ins = ins->next;
21447 } while(ins != first);
21448}
Eric Biederman83b991a2003-10-11 06:20:25 +000021449
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021450static void verify_ins_colors(struct compile_state *state)
21451{
21452 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021453
Eric Biederman83b991a2003-10-11 06:20:25 +000021454 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021455 ins = first;
21456 do {
21457 ins = ins->next;
21458 } while(ins != first);
21459}
Eric Biederman90089602004-05-28 14:11:54 +000021460
21461static void verify_unknown(struct compile_state *state)
21462{
21463 struct triple *first, *ins;
21464 if ( (unknown_triple.next != &unknown_triple) ||
21465 (unknown_triple.prev != &unknown_triple) ||
21466#if 0
21467 (unknown_triple.use != 0) ||
21468#endif
21469 (unknown_triple.op != OP_UNKNOWNVAL) ||
21470 (unknown_triple.lhs != 0) ||
21471 (unknown_triple.rhs != 0) ||
21472 (unknown_triple.misc != 0) ||
21473 (unknown_triple.targ != 0) ||
21474 (unknown_triple.template_id != 0) ||
21475 (unknown_triple.id != -1) ||
21476 (unknown_triple.type != &unknown_type) ||
21477 (unknown_triple.occurance != &dummy_occurance) ||
21478 (unknown_triple.param[0] != 0) ||
21479 (unknown_triple.param[1] != 0)) {
21480 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21481 }
21482 if ( (dummy_occurance.count != 2) ||
21483 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21484 (strcmp(dummy_occurance.function, "") != 0) ||
21485 (dummy_occurance.col != 0) ||
21486 (dummy_occurance.parent != 0)) {
21487 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21488 }
21489 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21490 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21491 }
21492 first = state->first;
21493 ins = first;
21494 do {
21495 int params, i;
21496 if (ins == &unknown_triple) {
21497 internal_error(state, ins, "unknown triple in list");
21498 }
21499 params = TRIPLE_SIZE(ins);
21500 for(i = 0; i < params; i++) {
21501 if (ins->param[i] == &unknown_triple) {
21502 internal_error(state, ins, "unknown triple used!");
21503 }
21504 }
21505 ins = ins->next;
21506 } while(ins != first);
21507}
21508
21509static void verify_types(struct compile_state *state)
21510{
21511 struct triple *first, *ins;
21512 first = state->first;
21513 ins = first;
21514 do {
21515 struct type *invalid;
21516 invalid = invalid_type(state, ins->type);
21517 if (invalid) {
21518 FILE *fp = state->errout;
21519 fprintf(fp, "type: ");
21520 name_of(fp, ins->type);
21521 fprintf(fp, "\n");
21522 fprintf(fp, "invalid type: ");
21523 name_of(fp, invalid);
21524 fprintf(fp, "\n");
21525 internal_error(state, ins, "invalid ins type");
21526 }
21527 } while(ins != first);
21528}
21529
21530static void verify_copy(struct compile_state *state)
21531{
21532 struct triple *first, *ins, *next;
21533 first = state->first;
21534 next = ins = first;
21535 do {
21536 ins = next;
21537 next = ins->next;
21538 if (ins->op != OP_COPY) {
21539 continue;
21540 }
21541 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21542 FILE *fp = state->errout;
21543 fprintf(fp, "src type: ");
21544 name_of(fp, RHS(ins, 0)->type);
21545 fprintf(fp, "\n");
21546 fprintf(fp, "dst type: ");
21547 name_of(fp, ins->type);
21548 fprintf(fp, "\n");
21549 internal_error(state, ins, "type mismatch in copy");
21550 }
21551 } while(next != first);
21552}
21553
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021554static void verify_consistency(struct compile_state *state)
21555{
Eric Biederman90089602004-05-28 14:11:54 +000021556 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021557 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021558 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021559 verify_blocks(state);
21560 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021561 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021562 verify_piece(state);
21563 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021564 verify_types(state);
21565 verify_copy(state);
21566 if (state->compiler->debug & DEBUG_VERIFICATION) {
21567 fprintf(state->dbgout, "consistency verified\n");
21568 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021569}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021570#else
Eric Biederman153ea352003-06-20 14:43:20 +000021571static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021572#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021573
21574static void optimize(struct compile_state *state)
21575{
Eric Biederman90089602004-05-28 14:11:54 +000021576 /* Join all of the functions into one giant function */
21577 join_functions(state);
21578
Eric Biederman5ade04a2003-10-22 04:03:46 +000021579 /* Dump what the instruction graph intially looks like */
21580 print_triples(state);
21581
Eric Biederman0babc1c2003-05-09 02:39:00 +000021582 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021583 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021584 print_triples(state);
21585
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021586 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021587 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021588 state->bb.first = state->first;
21589 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021590
Eric Biederman530b5192003-07-01 10:05:30 +000021591 /* Transform the code to ssa form. */
21592 /*
21593 * The transformation to ssa form puts a phi function
21594 * on each of edge of a dominance frontier where that
21595 * phi function might be needed. At -O2 if we don't
21596 * eleminate the excess phi functions we can get an
21597 * exponential code size growth. So I kill the extra
21598 * phi functions early and I kill them often.
21599 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021600 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021601 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021602
Eric Biederman83b991a2003-10-11 06:20:25 +000021603 /* Remove dead code */
21604 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021605 verify_consistency(state);
21606
Eric Biedermanb138ac82003-04-22 18:44:01 +000021607 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021608 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021609 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021610 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021611 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021612 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021613#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021614#warning "WISHLIST implement single use constants (least possible register pressure)"
21615#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021616#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021617 /* Select architecture instructions and an initial partial
21618 * coloring based on architecture constraints.
21619 */
21620 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021621 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021622
Eric Biederman83b991a2003-10-11 06:20:25 +000021623 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021624 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021625 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021626
Eric Biedermanb138ac82003-04-22 18:44:01 +000021627 /* Color all of the variables to see if they will fit in registers */
21628 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021629 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021630
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021631 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021632 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021633
Eric Biedermanb138ac82003-04-22 18:44:01 +000021634 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021635 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021636
Eric Biedermanb138ac82003-04-22 18:44:01 +000021637 /* Remove the optimization information.
21638 * This is more to check for memory consistency than to free memory.
21639 */
Eric Biederman90089602004-05-28 14:11:54 +000021640 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021641}
21642
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021643static void print_op_asm(struct compile_state *state,
21644 struct triple *ins, FILE *fp)
21645{
21646 struct asm_info *info;
21647 const char *ptr;
21648 unsigned lhs, rhs, i;
21649 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021650 lhs = ins->lhs;
21651 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021652 /* Don't count the clobbers in lhs */
21653 for(i = 0; i < lhs; i++) {
21654 if (LHS(ins, i)->type == &void_type) {
21655 break;
21656 }
21657 }
21658 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021659 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021660 fputc('\t', fp);
21661 for(ptr = info->str; *ptr; ptr++) {
21662 char *next;
21663 unsigned long param;
21664 struct triple *piece;
21665 if (*ptr != '%') {
21666 fputc(*ptr, fp);
21667 continue;
21668 }
21669 ptr++;
21670 if (*ptr == '%') {
21671 fputc('%', fp);
21672 continue;
21673 }
21674 param = strtoul(ptr, &next, 10);
21675 if (ptr == next) {
21676 error(state, ins, "Invalid asm template");
21677 }
21678 if (param >= (lhs + rhs)) {
21679 error(state, ins, "Invalid param %%%u in asm template",
21680 param);
21681 }
21682 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021683 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021684 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021685 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021686 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021687 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021688}
21689
21690
21691/* Only use the low x86 byte registers. This allows me
21692 * allocate the entire register when a byte register is used.
21693 */
21694#define X86_4_8BIT_GPRS 1
21695
Eric Biederman83b991a2003-10-11 06:20:25 +000021696/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021697#define X86_MMX_REGS (1<<0)
21698#define X86_XMM_REGS (1<<1)
21699#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021700
Eric Biedermanb138ac82003-04-22 18:44:01 +000021701/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021702#define REGC_FLAGS 0
21703#define REGC_GPR8 1
21704#define REGC_GPR16 2
21705#define REGC_GPR32 3
21706#define REGC_DIVIDEND64 4
21707#define REGC_DIVIDEND32 5
21708#define REGC_MMX 6
21709#define REGC_XMM 7
21710#define REGC_GPR32_8 8
21711#define REGC_GPR16_8 9
21712#define REGC_GPR8_LO 10
21713#define REGC_IMM32 11
21714#define REGC_IMM16 12
21715#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021716#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021717#if LAST_REGC >= MAX_REGC
21718#error "MAX_REGC is to low"
21719#endif
21720
21721/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021722#define REGCM_FLAGS (1 << REGC_FLAGS)
21723#define REGCM_GPR8 (1 << REGC_GPR8)
21724#define REGCM_GPR16 (1 << REGC_GPR16)
21725#define REGCM_GPR32 (1 << REGC_GPR32)
21726#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21727#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21728#define REGCM_MMX (1 << REGC_MMX)
21729#define REGCM_XMM (1 << REGC_XMM)
21730#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21731#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21732#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21733#define REGCM_IMM32 (1 << REGC_IMM32)
21734#define REGCM_IMM16 (1 << REGC_IMM16)
21735#define REGCM_IMM8 (1 << REGC_IMM8)
21736#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021737#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021738
21739/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021740#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021741#define REGC_FLAGS_FIRST REG_EFLAGS
21742#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021743#define REG_AL 3
21744#define REG_BL 4
21745#define REG_CL 5
21746#define REG_DL 6
21747#define REG_AH 7
21748#define REG_BH 8
21749#define REG_CH 9
21750#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021751#define REGC_GPR8_LO_FIRST REG_AL
21752#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021753#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021754#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021755#define REG_AX 11
21756#define REG_BX 12
21757#define REG_CX 13
21758#define REG_DX 14
21759#define REG_SI 15
21760#define REG_DI 16
21761#define REG_BP 17
21762#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021763#define REGC_GPR16_FIRST REG_AX
21764#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021765#define REG_EAX 19
21766#define REG_EBX 20
21767#define REG_ECX 21
21768#define REG_EDX 22
21769#define REG_ESI 23
21770#define REG_EDI 24
21771#define REG_EBP 25
21772#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021773#define REGC_GPR32_FIRST REG_EAX
21774#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021775#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021776#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21777#define REGC_DIVIDEND64_LAST REG_EDXEAX
21778#define REG_DXAX 28
21779#define REGC_DIVIDEND32_FIRST REG_DXAX
21780#define REGC_DIVIDEND32_LAST REG_DXAX
21781#define REG_MMX0 29
21782#define REG_MMX1 30
21783#define REG_MMX2 31
21784#define REG_MMX3 32
21785#define REG_MMX4 33
21786#define REG_MMX5 34
21787#define REG_MMX6 35
21788#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021789#define REGC_MMX_FIRST REG_MMX0
21790#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021791#define REG_XMM0 37
21792#define REG_XMM1 38
21793#define REG_XMM2 39
21794#define REG_XMM3 40
21795#define REG_XMM4 41
21796#define REG_XMM5 42
21797#define REG_XMM6 43
21798#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021799#define REGC_XMM_FIRST REG_XMM0
21800#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021801
21802#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021803#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021804#endif
21805
Eric Biedermanb138ac82003-04-22 18:44:01 +000021806#define LAST_REG REG_XMM7
21807
21808#define REGC_GPR32_8_FIRST REG_EAX
21809#define REGC_GPR32_8_LAST REG_EDX
21810#define REGC_GPR16_8_FIRST REG_AX
21811#define REGC_GPR16_8_LAST REG_DX
21812
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021813#define REGC_IMM8_FIRST -1
21814#define REGC_IMM8_LAST -1
21815#define REGC_IMM16_FIRST -2
21816#define REGC_IMM16_LAST -1
21817#define REGC_IMM32_FIRST -4
21818#define REGC_IMM32_LAST -1
21819
Eric Biedermanb138ac82003-04-22 18:44:01 +000021820#if LAST_REG >= MAX_REGISTERS
21821#error "MAX_REGISTERS to low"
21822#endif
21823
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021824
21825static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021826 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21827 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21828 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21829 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21830 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21831 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21832 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21833 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21834 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21835 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21836 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21837 [REGC_IMM32] = 0,
21838 [REGC_IMM16] = 0,
21839 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021840};
21841
21842static const struct {
21843 int first, last;
21844} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021845 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21846 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21847 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21848 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21849 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21850 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21851 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21852 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21853 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21854 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21855 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21856 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21857 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21858 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021859};
21860
Eric Biederman90089602004-05-28 14:11:54 +000021861#if ARCH_INPUT_REGS != 4
21862#error ARCH_INPUT_REGS size mismatch
21863#endif
21864static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21865 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21866 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21867 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21868 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21869};
21870
21871#if ARCH_OUTPUT_REGS != 4
21872#error ARCH_INPUT_REGS size mismatch
21873#endif
21874static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21875 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21876 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21877 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21878 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21879};
21880
Eric Biederman5ade04a2003-10-22 04:03:46 +000021881static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021882{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021883 memset(arch, 0, sizeof(*arch));
21884 arch->features = 0;
21885}
21886
Eric Biederman90089602004-05-28 14:11:54 +000021887static const struct compiler_flag arch_flags[] = {
21888 { "mmx", X86_MMX_REGS },
21889 { "sse", X86_XMM_REGS },
21890 { "noop-copy", X86_NOOP_COPY },
21891 { 0, 0 },
21892};
21893static const struct compiler_flag arch_cpus[] = {
21894 { "i386", 0 },
21895 { "p2", X86_MMX_REGS },
21896 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21897 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21898 { "k7", X86_MMX_REGS },
21899 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21900 { "c3", X86_MMX_REGS },
21901 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21902 { 0, 0 }
21903};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021904static int arch_encode_flag(struct arch_state *arch, const char *flag)
21905{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021906 int result;
21907 int act;
21908
21909 act = 1;
21910 result = -1;
21911 if (strncmp(flag, "no-", 3) == 0) {
21912 flag += 3;
21913 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021914 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021915 if (act && strncmp(flag, "cpu=", 4) == 0) {
21916 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021917 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021918 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021919 else {
Eric Biederman90089602004-05-28 14:11:54 +000021920 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021921 }
21922 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021923}
21924
Eric Biederman90089602004-05-28 14:11:54 +000021925static void arch_usage(FILE *fp)
21926{
21927 flag_usage(fp, arch_flags, "-m", "-mno-");
21928 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21929}
21930
Eric Biedermanb138ac82003-04-22 18:44:01 +000021931static unsigned arch_regc_size(struct compile_state *state, int class)
21932{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021933 if ((class < 0) || (class > LAST_REGC)) {
21934 return 0;
21935 }
21936 return regc_size[class];
21937}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021938
Eric Biedermanb138ac82003-04-22 18:44:01 +000021939static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21940{
21941 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021942 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021943 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021944 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021945
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021946 /* Special case for the immediates */
21947 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21948 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21949 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021950 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021951 return 0;
21952 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021953 return (regcm1 & regcm2) ||
21954 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21955}
21956
21957static void arch_reg_equivs(
21958 struct compile_state *state, unsigned *equiv, int reg)
21959{
21960 if ((reg < 0) || (reg > LAST_REG)) {
21961 internal_error(state, 0, "invalid register");
21962 }
21963 *equiv++ = reg;
21964 switch(reg) {
21965 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021966#if X86_4_8BIT_GPRS
21967 *equiv++ = REG_AH;
21968#endif
21969 *equiv++ = REG_AX;
21970 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021971 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021972 *equiv++ = REG_EDXEAX;
21973 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021974 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021975#if X86_4_8BIT_GPRS
21976 *equiv++ = REG_AL;
21977#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021978 *equiv++ = REG_AX;
21979 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021980 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021981 *equiv++ = REG_EDXEAX;
21982 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021983 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021984#if X86_4_8BIT_GPRS
21985 *equiv++ = REG_BH;
21986#endif
21987 *equiv++ = REG_BX;
21988 *equiv++ = REG_EBX;
21989 break;
21990
Eric Biedermanb138ac82003-04-22 18:44:01 +000021991 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021992#if X86_4_8BIT_GPRS
21993 *equiv++ = REG_BL;
21994#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021995 *equiv++ = REG_BX;
21996 *equiv++ = REG_EBX;
21997 break;
21998 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021999#if X86_4_8BIT_GPRS
22000 *equiv++ = REG_CH;
22001#endif
22002 *equiv++ = REG_CX;
22003 *equiv++ = REG_ECX;
22004 break;
22005
Eric Biedermanb138ac82003-04-22 18:44:01 +000022006 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022007#if X86_4_8BIT_GPRS
22008 *equiv++ = REG_CL;
22009#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022010 *equiv++ = REG_CX;
22011 *equiv++ = REG_ECX;
22012 break;
22013 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022014#if X86_4_8BIT_GPRS
22015 *equiv++ = REG_DH;
22016#endif
22017 *equiv++ = REG_DX;
22018 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022019 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022020 *equiv++ = REG_EDXEAX;
22021 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022022 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022023#if X86_4_8BIT_GPRS
22024 *equiv++ = REG_DL;
22025#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022026 *equiv++ = REG_DX;
22027 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022028 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022029 *equiv++ = REG_EDXEAX;
22030 break;
22031 case REG_AX:
22032 *equiv++ = REG_AL;
22033 *equiv++ = REG_AH;
22034 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022035 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022036 *equiv++ = REG_EDXEAX;
22037 break;
22038 case REG_BX:
22039 *equiv++ = REG_BL;
22040 *equiv++ = REG_BH;
22041 *equiv++ = REG_EBX;
22042 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022043 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022044 *equiv++ = REG_CL;
22045 *equiv++ = REG_CH;
22046 *equiv++ = REG_ECX;
22047 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022048 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022049 *equiv++ = REG_DL;
22050 *equiv++ = REG_DH;
22051 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022052 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022053 *equiv++ = REG_EDXEAX;
22054 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022055 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022056 *equiv++ = REG_ESI;
22057 break;
22058 case REG_DI:
22059 *equiv++ = REG_EDI;
22060 break;
22061 case REG_BP:
22062 *equiv++ = REG_EBP;
22063 break;
22064 case REG_SP:
22065 *equiv++ = REG_ESP;
22066 break;
22067 case REG_EAX:
22068 *equiv++ = REG_AL;
22069 *equiv++ = REG_AH;
22070 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022071 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022072 *equiv++ = REG_EDXEAX;
22073 break;
22074 case REG_EBX:
22075 *equiv++ = REG_BL;
22076 *equiv++ = REG_BH;
22077 *equiv++ = REG_BX;
22078 break;
22079 case REG_ECX:
22080 *equiv++ = REG_CL;
22081 *equiv++ = REG_CH;
22082 *equiv++ = REG_CX;
22083 break;
22084 case REG_EDX:
22085 *equiv++ = REG_DL;
22086 *equiv++ = REG_DH;
22087 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022088 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022089 *equiv++ = REG_EDXEAX;
22090 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022091 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022092 *equiv++ = REG_SI;
22093 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022094 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022095 *equiv++ = REG_DI;
22096 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022097 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022098 *equiv++ = REG_BP;
22099 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022100 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022101 *equiv++ = REG_SP;
22102 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022103 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022104 *equiv++ = REG_AL;
22105 *equiv++ = REG_AH;
22106 *equiv++ = REG_DL;
22107 *equiv++ = REG_DH;
22108 *equiv++ = REG_AX;
22109 *equiv++ = REG_DX;
22110 *equiv++ = REG_EAX;
22111 *equiv++ = REG_EDX;
22112 *equiv++ = REG_EDXEAX;
22113 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022114 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022115 *equiv++ = REG_AL;
22116 *equiv++ = REG_AH;
22117 *equiv++ = REG_DL;
22118 *equiv++ = REG_DH;
22119 *equiv++ = REG_AX;
22120 *equiv++ = REG_DX;
22121 *equiv++ = REG_EAX;
22122 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022123 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022124 break;
22125 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022126 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022127}
22128
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022129static unsigned arch_avail_mask(struct compile_state *state)
22130{
22131 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022132 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022133 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22134 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022135 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022136 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022137 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022138 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022139 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022140 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022141 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022142 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022143 return avail_mask;
22144}
22145
22146static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22147{
22148 unsigned mask, result;
22149 int class, class2;
22150 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022151
22152 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22153 if ((result & mask) == 0) {
22154 continue;
22155 }
22156 if (class > LAST_REGC) {
22157 result &= ~mask;
22158 }
22159 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22160 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22161 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22162 result |= (1 << class2);
22163 }
22164 }
22165 }
Eric Biederman530b5192003-07-01 10:05:30 +000022166 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022167 return result;
22168}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022169
Eric Biedermand1ea5392003-06-28 06:49:45 +000022170static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22171{
22172 /* Like arch_regcm_normalize except immediate register classes are excluded */
22173 regcm = arch_regcm_normalize(state, regcm);
22174 /* Remove the immediate register classes */
22175 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22176 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022177
Eric Biedermand1ea5392003-06-28 06:49:45 +000022178}
22179
Eric Biedermanb138ac82003-04-22 18:44:01 +000022180static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22181{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022182 unsigned mask;
22183 int class;
22184 mask = 0;
22185 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022186 if ((reg >= regcm_bound[class].first) &&
22187 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022188 mask |= (1 << class);
22189 }
22190 }
22191 if (!mask) {
22192 internal_error(state, 0, "reg %d not in any class", reg);
22193 }
22194 return mask;
22195}
22196
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022197static struct reg_info arch_reg_constraint(
22198 struct compile_state *state, struct type *type, const char *constraint)
22199{
22200 static const struct {
22201 char class;
22202 unsigned int mask;
22203 unsigned int reg;
22204 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022205 { 'r', REGCM_GPR32, REG_UNSET },
22206 { 'g', REGCM_GPR32, REG_UNSET },
22207 { 'p', REGCM_GPR32, REG_UNSET },
22208 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022209 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022210 { 'x', REGCM_XMM, REG_UNSET },
22211 { 'y', REGCM_MMX, REG_UNSET },
22212 { 'a', REGCM_GPR32, REG_EAX },
22213 { 'b', REGCM_GPR32, REG_EBX },
22214 { 'c', REGCM_GPR32, REG_ECX },
22215 { 'd', REGCM_GPR32, REG_EDX },
22216 { 'D', REGCM_GPR32, REG_EDI },
22217 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022218 { '\0', 0, REG_UNSET },
22219 };
22220 unsigned int regcm;
22221 unsigned int mask, reg;
22222 struct reg_info result;
22223 const char *ptr;
22224 regcm = arch_type_to_regcm(state, type);
22225 reg = REG_UNSET;
22226 mask = 0;
22227 for(ptr = constraint; *ptr; ptr++) {
22228 int i;
22229 if (*ptr == ' ') {
22230 continue;
22231 }
22232 for(i = 0; constraints[i].class != '\0'; i++) {
22233 if (constraints[i].class == *ptr) {
22234 break;
22235 }
22236 }
22237 if (constraints[i].class == '\0') {
22238 error(state, 0, "invalid register constraint ``%c''", *ptr);
22239 break;
22240 }
22241 if ((constraints[i].mask & regcm) == 0) {
22242 error(state, 0, "invalid register class %c specified",
22243 *ptr);
22244 }
22245 mask |= constraints[i].mask;
22246 if (constraints[i].reg != REG_UNSET) {
22247 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22248 error(state, 0, "Only one register may be specified");
22249 }
22250 reg = constraints[i].reg;
22251 }
22252 }
22253 result.reg = reg;
22254 result.regcm = mask;
22255 return result;
22256}
22257
22258static struct reg_info arch_reg_clobber(
22259 struct compile_state *state, const char *clobber)
22260{
22261 struct reg_info result;
22262 if (strcmp(clobber, "memory") == 0) {
22263 result.reg = REG_UNSET;
22264 result.regcm = 0;
22265 }
Eric Biederman90089602004-05-28 14:11:54 +000022266 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022267 result.reg = REG_EAX;
22268 result.regcm = REGCM_GPR32;
22269 }
Eric Biederman90089602004-05-28 14:11:54 +000022270 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022271 result.reg = REG_EBX;
22272 result.regcm = REGCM_GPR32;
22273 }
Eric Biederman90089602004-05-28 14:11:54 +000022274 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022275 result.reg = REG_ECX;
22276 result.regcm = REGCM_GPR32;
22277 }
Eric Biederman90089602004-05-28 14:11:54 +000022278 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022279 result.reg = REG_EDX;
22280 result.regcm = REGCM_GPR32;
22281 }
Eric Biederman90089602004-05-28 14:11:54 +000022282 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022283 result.reg = REG_ESI;
22284 result.regcm = REGCM_GPR32;
22285 }
Eric Biederman90089602004-05-28 14:11:54 +000022286 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022287 result.reg = REG_EDI;
22288 result.regcm = REGCM_GPR32;
22289 }
Eric Biederman90089602004-05-28 14:11:54 +000022290 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022291 result.reg = REG_EBP;
22292 result.regcm = REGCM_GPR32;
22293 }
Eric Biederman90089602004-05-28 14:11:54 +000022294 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022295 result.reg = REG_ESP;
22296 result.regcm = REGCM_GPR32;
22297 }
22298 else if (strcmp(clobber, "cc") == 0) {
22299 result.reg = REG_EFLAGS;
22300 result.regcm = REGCM_FLAGS;
22301 }
22302 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22303 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22304 result.reg = REG_XMM0 + octdigval(clobber[3]);
22305 result.regcm = REGCM_XMM;
22306 }
Eric Biederman90089602004-05-28 14:11:54 +000022307 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022308 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22309 result.reg = REG_MMX0 + octdigval(clobber[3]);
22310 result.regcm = REGCM_MMX;
22311 }
22312 else {
Eric Biederman90089602004-05-28 14:11:54 +000022313 error(state, 0, "unknown register name `%s' in asm",
22314 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022315 result.reg = REG_UNSET;
22316 result.regcm = 0;
22317 }
22318 return result;
22319}
22320
Stefan Reinauer14e22772010-04-27 06:56:47 +000022321static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022322 char *used, int reg, unsigned classes)
22323{
22324 unsigned mask;
22325 if (used[reg]) {
22326 return REG_UNSET;
22327 }
22328 mask = arch_reg_regcm(state, reg);
22329 return (classes & mask) ? reg : REG_UNSET;
22330}
22331
22332static int arch_select_free_register(
22333 struct compile_state *state, char *used, int classes)
22334{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022335 /* Live ranges with the most neighbors are colored first.
22336 *
22337 * Generally it does not matter which colors are given
22338 * as the register allocator attempts to color live ranges
22339 * in an order where you are guaranteed not to run out of colors.
22340 *
22341 * Occasionally the register allocator cannot find an order
22342 * of register selection that will find a free color. To
22343 * increase the odds the register allocator will work when
22344 * it guesses first give out registers from register classes
22345 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022346 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022347 */
22348 int i, reg;
22349 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022350 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022351 reg = do_select_reg(state, used, i, classes);
22352 }
22353 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22354 reg = do_select_reg(state, used, i, classes);
22355 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022356 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022357 reg = do_select_reg(state, used, i, classes);
22358 }
22359 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22360 reg = do_select_reg(state, used, i, classes);
22361 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022362 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22363 reg = do_select_reg(state, used, i, classes);
22364 }
Eric Biederman530b5192003-07-01 10:05:30 +000022365 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22366 reg = do_select_reg(state, used, i, classes);
22367 }
22368 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22369 reg = do_select_reg(state, used, i, classes);
22370 }
22371 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022372 reg = do_select_reg(state, used, i, classes);
22373 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022374 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22375 reg = do_select_reg(state, used, i, classes);
22376 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022377 return reg;
22378}
22379
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022380
Stefan Reinauer14e22772010-04-27 06:56:47 +000022381static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022382{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022383
22384#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022385#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022386#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022387 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022388 mask = 0;
22389 switch(type->type & TYPE_MASK) {
22390 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022391 case TYPE_VOID:
22392 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022393 break;
22394 case TYPE_CHAR:
22395 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022396 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022397 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022398 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022399 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022400 REGCM_MMX | REGCM_XMM |
22401 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022402 break;
22403 case TYPE_SHORT:
22404 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022405 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022406 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022407 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022408 REGCM_MMX | REGCM_XMM |
22409 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022410 break;
Eric Biederman90089602004-05-28 14:11:54 +000022411 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022412 case TYPE_INT:
22413 case TYPE_UINT:
22414 case TYPE_LONG:
22415 case TYPE_ULONG:
22416 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022417 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022418 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22419 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022420 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022421 break;
Eric Biederman90089602004-05-28 14:11:54 +000022422 case TYPE_JOIN:
22423 case TYPE_UNION:
22424 mask = arch_type_to_regcm(state, type->left);
22425 break;
22426 case TYPE_OVERLAP:
22427 mask = arch_type_to_regcm(state, type->left) &
22428 arch_type_to_regcm(state, type->right);
22429 break;
22430 case TYPE_BITFIELD:
22431 mask = arch_type_to_regcm(state, type->left);
22432 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022433 default:
Eric Biederman90089602004-05-28 14:11:54 +000022434 fprintf(state->errout, "type: ");
22435 name_of(state->errout, type);
22436 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022437 internal_error(state, 0, "no register class for type");
22438 break;
22439 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022440 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022441 return mask;
22442}
22443
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022444static int is_imm32(struct triple *imm)
22445{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022446 // second condition commented out to prevent compiler warning:
22447 // imm->u.cval is always 32bit unsigned, so the comparison is
22448 // always true.
22449 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022450 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022451
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022452}
22453static int is_imm16(struct triple *imm)
22454{
22455 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22456}
22457static int is_imm8(struct triple *imm)
22458{
22459 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22460}
22461
22462static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022463{
22464 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022465 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022466 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022467 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022468 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022469 if (!is_imm32(imm)) {
22470 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022471 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022472 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022473 use_triple(imm, ins);
22474 *expr = imm;
22475 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022476}
22477
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022478static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022479{
22480 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022481 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022482 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022483 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022484 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022485 if (!is_imm8(imm)) {
22486 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022487 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022488 unuse_triple(*expr, ins);
22489 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022490 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022491 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022492}
22493
Eric Biederman530b5192003-07-01 10:05:30 +000022494#define TEMPLATE_NOP 0
22495#define TEMPLATE_INTCONST8 1
22496#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022497#define TEMPLATE_UNKNOWNVAL 3
22498#define TEMPLATE_COPY8_REG 5
22499#define TEMPLATE_COPY16_REG 6
22500#define TEMPLATE_COPY32_REG 7
22501#define TEMPLATE_COPY_IMM8 8
22502#define TEMPLATE_COPY_IMM16 9
22503#define TEMPLATE_COPY_IMM32 10
22504#define TEMPLATE_PHI8 11
22505#define TEMPLATE_PHI16 12
22506#define TEMPLATE_PHI32 13
22507#define TEMPLATE_STORE8 14
22508#define TEMPLATE_STORE16 15
22509#define TEMPLATE_STORE32 16
22510#define TEMPLATE_LOAD8 17
22511#define TEMPLATE_LOAD16 18
22512#define TEMPLATE_LOAD32 19
22513#define TEMPLATE_BINARY8_REG 20
22514#define TEMPLATE_BINARY16_REG 21
22515#define TEMPLATE_BINARY32_REG 22
22516#define TEMPLATE_BINARY8_IMM 23
22517#define TEMPLATE_BINARY16_IMM 24
22518#define TEMPLATE_BINARY32_IMM 25
22519#define TEMPLATE_SL8_CL 26
22520#define TEMPLATE_SL16_CL 27
22521#define TEMPLATE_SL32_CL 28
22522#define TEMPLATE_SL8_IMM 29
22523#define TEMPLATE_SL16_IMM 30
22524#define TEMPLATE_SL32_IMM 31
22525#define TEMPLATE_UNARY8 32
22526#define TEMPLATE_UNARY16 33
22527#define TEMPLATE_UNARY32 34
22528#define TEMPLATE_CMP8_REG 35
22529#define TEMPLATE_CMP16_REG 36
22530#define TEMPLATE_CMP32_REG 37
22531#define TEMPLATE_CMP8_IMM 38
22532#define TEMPLATE_CMP16_IMM 39
22533#define TEMPLATE_CMP32_IMM 40
22534#define TEMPLATE_TEST8 41
22535#define TEMPLATE_TEST16 42
22536#define TEMPLATE_TEST32 43
22537#define TEMPLATE_SET 44
22538#define TEMPLATE_JMP 45
22539#define TEMPLATE_RET 46
22540#define TEMPLATE_INB_DX 47
22541#define TEMPLATE_INB_IMM 48
22542#define TEMPLATE_INW_DX 49
22543#define TEMPLATE_INW_IMM 50
22544#define TEMPLATE_INL_DX 51
22545#define TEMPLATE_INL_IMM 52
22546#define TEMPLATE_OUTB_DX 53
22547#define TEMPLATE_OUTB_IMM 54
22548#define TEMPLATE_OUTW_DX 55
22549#define TEMPLATE_OUTW_IMM 56
22550#define TEMPLATE_OUTL_DX 57
22551#define TEMPLATE_OUTL_IMM 58
22552#define TEMPLATE_BSF 59
22553#define TEMPLATE_RDMSR 60
22554#define TEMPLATE_WRMSR 61
22555#define TEMPLATE_UMUL8 62
22556#define TEMPLATE_UMUL16 63
22557#define TEMPLATE_UMUL32 64
22558#define TEMPLATE_DIV8 65
22559#define TEMPLATE_DIV16 66
22560#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022561#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022562#if LAST_TEMPLATE >= MAX_TEMPLATES
22563#error "MAX_TEMPLATES to low"
22564#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022565
Eric Biederman530b5192003-07-01 10:05:30 +000022566#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 +000022567#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022568#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022569
Eric Biedermanb138ac82003-04-22 18:44:01 +000022570
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022571static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022572 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022573 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022574 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22575 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22576 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22577 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22638 },
22639 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022640 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022641 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22642 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022643 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022644 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22645 },
Eric Biederman90089602004-05-28 14:11:54 +000022646 [TEMPLATE_UNKNOWNVAL] = {
22647 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22648 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022649 [TEMPLATE_COPY8_REG] = {
22650 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22651 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022652 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022653 [TEMPLATE_COPY16_REG] = {
22654 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22655 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22656 },
22657 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022658 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022659 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022660 },
22661 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022662 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022663 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22664 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022665 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022666 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022667 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22668 },
22669 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022670 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022671 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22672 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022673 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022674 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022675 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22676 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022677 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022678 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022679 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022680 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022681 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022682 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022683 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022684 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022685 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022686 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022687 [0] = { REG_UNSET, REGCM_GPR32 },
22688 [1] = { REG_UNSET, REGCM_GPR8_LO },
22689 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022690 },
22691 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022692 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022693 [0] = { REG_UNSET, REGCM_GPR32 },
22694 [1] = { REG_UNSET, REGCM_GPR16 },
22695 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022696 },
22697 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022698 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022699 [0] = { REG_UNSET, REGCM_GPR32 },
22700 [1] = { REG_UNSET, REGCM_GPR32 },
22701 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022702 },
22703 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022704 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022705 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22706 },
22707 [TEMPLATE_LOAD16] = {
22708 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22709 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22710 },
22711 [TEMPLATE_LOAD32] = {
22712 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22713 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22714 },
Eric Biederman530b5192003-07-01 10:05:30 +000022715 [TEMPLATE_BINARY8_REG] = {
22716 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022717 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022718 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22719 [1] = { REG_UNSET, REGCM_GPR8_LO },
22720 },
22721 },
22722 [TEMPLATE_BINARY16_REG] = {
22723 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022724 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022725 [0] = { REG_VIRT0, REGCM_GPR16 },
22726 [1] = { REG_UNSET, REGCM_GPR16 },
22727 },
22728 },
22729 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022730 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022731 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022732 [0] = { REG_VIRT0, REGCM_GPR32 },
22733 [1] = { REG_UNSET, REGCM_GPR32 },
22734 },
22735 },
Eric Biederman530b5192003-07-01 10:05:30 +000022736 [TEMPLATE_BINARY8_IMM] = {
22737 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022738 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022739 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22740 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22741 },
22742 },
22743 [TEMPLATE_BINARY16_IMM] = {
22744 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022745 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022746 [0] = { REG_VIRT0, REGCM_GPR16 },
22747 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22748 },
22749 },
22750 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022751 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022752 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022753 [0] = { REG_VIRT0, REGCM_GPR32 },
22754 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22755 },
22756 },
Eric Biederman530b5192003-07-01 10:05:30 +000022757 [TEMPLATE_SL8_CL] = {
22758 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022759 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022760 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22761 [1] = { REG_CL, REGCM_GPR8_LO },
22762 },
22763 },
22764 [TEMPLATE_SL16_CL] = {
22765 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022766 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022767 [0] = { REG_VIRT0, REGCM_GPR16 },
22768 [1] = { REG_CL, REGCM_GPR8_LO },
22769 },
22770 },
22771 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022772 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022773 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022774 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022775 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022776 },
22777 },
Eric Biederman530b5192003-07-01 10:05:30 +000022778 [TEMPLATE_SL8_IMM] = {
22779 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022780 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022781 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22782 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22783 },
22784 },
22785 [TEMPLATE_SL16_IMM] = {
22786 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022787 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022788 [0] = { REG_VIRT0, REGCM_GPR16 },
22789 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22790 },
22791 },
22792 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022793 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022794 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022795 [0] = { REG_VIRT0, REGCM_GPR32 },
22796 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22797 },
22798 },
Eric Biederman530b5192003-07-01 10:05:30 +000022799 [TEMPLATE_UNARY8] = {
22800 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22801 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22802 },
22803 [TEMPLATE_UNARY16] = {
22804 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22805 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22806 },
22807 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022808 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22809 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22810 },
Eric Biederman530b5192003-07-01 10:05:30 +000022811 [TEMPLATE_CMP8_REG] = {
22812 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22813 .rhs = {
22814 [0] = { REG_UNSET, REGCM_GPR8_LO },
22815 [1] = { REG_UNSET, REGCM_GPR8_LO },
22816 },
22817 },
22818 [TEMPLATE_CMP16_REG] = {
22819 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22820 .rhs = {
22821 [0] = { REG_UNSET, REGCM_GPR16 },
22822 [1] = { REG_UNSET, REGCM_GPR16 },
22823 },
22824 },
22825 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022826 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22827 .rhs = {
22828 [0] = { REG_UNSET, REGCM_GPR32 },
22829 [1] = { REG_UNSET, REGCM_GPR32 },
22830 },
22831 },
Eric Biederman530b5192003-07-01 10:05:30 +000022832 [TEMPLATE_CMP8_IMM] = {
22833 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22834 .rhs = {
22835 [0] = { REG_UNSET, REGCM_GPR8_LO },
22836 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22837 },
22838 },
22839 [TEMPLATE_CMP16_IMM] = {
22840 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22841 .rhs = {
22842 [0] = { REG_UNSET, REGCM_GPR16 },
22843 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22844 },
22845 },
22846 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022847 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22848 .rhs = {
22849 [0] = { REG_UNSET, REGCM_GPR32 },
22850 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22851 },
22852 },
Eric Biederman530b5192003-07-01 10:05:30 +000022853 [TEMPLATE_TEST8] = {
22854 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22855 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22856 },
22857 [TEMPLATE_TEST16] = {
22858 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22859 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22860 },
22861 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022862 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22863 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22864 },
22865 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022866 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022867 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22868 },
22869 [TEMPLATE_JMP] = {
22870 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22871 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022872 [TEMPLATE_RET] = {
22873 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22874 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022875 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022876 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022877 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22878 },
22879 [TEMPLATE_INB_IMM] = {
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_UNNEEDED, REGCM_IMM8 } },
22882 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022883 [TEMPLATE_INW_DX] = {
22884 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022885 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22886 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022887 [TEMPLATE_INW_IMM] = {
22888 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022889 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22890 },
22891 [TEMPLATE_INL_DX] = {
22892 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22893 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22894 },
22895 [TEMPLATE_INL_IMM] = {
22896 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22897 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22898 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022899 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022900 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022901 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022902 [1] = { REG_DX, REGCM_GPR16 },
22903 },
22904 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022905 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022906 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022907 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022908 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22909 },
22910 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022911 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022912 .rhs = {
22913 [0] = { REG_AX, REGCM_GPR16 },
22914 [1] = { REG_DX, REGCM_GPR16 },
22915 },
22916 },
22917 [TEMPLATE_OUTW_IMM] = {
22918 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022919 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022920 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22921 },
22922 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022923 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022924 .rhs = {
22925 [0] = { REG_EAX, REGCM_GPR32 },
22926 [1] = { REG_DX, REGCM_GPR16 },
22927 },
22928 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022929 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022930 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022931 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022932 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22933 },
22934 },
22935 [TEMPLATE_BSF] = {
22936 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22937 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22938 },
22939 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022940 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022941 [0] = { REG_EAX, REGCM_GPR32 },
22942 [1] = { REG_EDX, REGCM_GPR32 },
22943 },
22944 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22945 },
22946 [TEMPLATE_WRMSR] = {
22947 .rhs = {
22948 [0] = { REG_ECX, REGCM_GPR32 },
22949 [1] = { REG_EAX, REGCM_GPR32 },
22950 [2] = { REG_EDX, REGCM_GPR32 },
22951 },
22952 },
Eric Biederman530b5192003-07-01 10:05:30 +000022953 [TEMPLATE_UMUL8] = {
22954 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022955 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022956 [0] = { REG_AL, REGCM_GPR8_LO },
22957 [1] = { REG_UNSET, REGCM_GPR8_LO },
22958 },
22959 },
22960 [TEMPLATE_UMUL16] = {
22961 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022962 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022963 [0] = { REG_AX, REGCM_GPR16 },
22964 [1] = { REG_UNSET, REGCM_GPR16 },
22965 },
22966 },
22967 [TEMPLATE_UMUL32] = {
22968 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022969 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022970 [0] = { REG_EAX, REGCM_GPR32 },
22971 [1] = { REG_UNSET, REGCM_GPR32 },
22972 },
22973 },
Eric Biederman530b5192003-07-01 10:05:30 +000022974 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022975 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022976 [0] = { REG_AL, REGCM_GPR8_LO },
22977 [1] = { REG_AH, REGCM_GPR8 },
22978 },
22979 .rhs = {
22980 [0] = { REG_AX, REGCM_GPR16 },
22981 [1] = { REG_UNSET, REGCM_GPR8_LO },
22982 },
22983 },
22984 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022985 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022986 [0] = { REG_AX, REGCM_GPR16 },
22987 [1] = { REG_DX, REGCM_GPR16 },
22988 },
22989 .rhs = {
22990 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22991 [1] = { REG_UNSET, REGCM_GPR16 },
22992 },
22993 },
22994 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022995 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022996 [0] = { REG_EAX, REGCM_GPR32 },
22997 [1] = { REG_EDX, REGCM_GPR32 },
22998 },
22999 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023000 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023001 [1] = { REG_UNSET, REGCM_GPR32 },
23002 },
23003 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023004};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023005
Eric Biederman83b991a2003-10-11 06:20:25 +000023006static void fixup_branch(struct compile_state *state,
23007 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23008 struct triple *left, struct triple *right)
23009{
23010 struct triple *test;
23011 if (!left) {
23012 internal_error(state, branch, "no branch test?");
23013 }
23014 test = pre_triple(state, branch,
23015 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023016 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023017 if (cmp_op == OP_CMP) {
23018 test->template_id = TEMPLATE_CMP32_REG;
23019 if (get_imm32(test, &RHS(test, 1))) {
23020 test->template_id = TEMPLATE_CMP32_IMM;
23021 }
23022 }
23023 use_triple(RHS(test, 0), test);
23024 use_triple(RHS(test, 1), test);
23025 unuse_triple(RHS(branch, 0), branch);
23026 RHS(branch, 0) = test;
23027 branch->op = jmp_op;
23028 branch->template_id = TEMPLATE_JMP;
23029 use_triple(RHS(branch, 0), branch);
23030}
23031
Eric Biedermanb138ac82003-04-22 18:44:01 +000023032static void fixup_branches(struct compile_state *state,
23033 struct triple *cmp, struct triple *use, int jmp_op)
23034{
23035 struct triple_set *entry, *next;
23036 for(entry = use->use; entry; entry = next) {
23037 next = entry->next;
23038 if (entry->member->op == OP_COPY) {
23039 fixup_branches(state, cmp, entry->member, jmp_op);
23040 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023041 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023042 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023043 struct triple *left, *right;
23044 left = right = 0;
23045 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023046 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023047 right = RHS(cmp, 1);
23048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023049 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023050 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023051 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023052 }
23053 }
23054}
23055
Stefan Reinauer14e22772010-04-27 06:56:47 +000023056static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023057 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23058{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023059 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023060 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023061
23062 /* Put a barrier up before the cmp which preceeds the
23063 * copy instruction. If a set actually occurs this gives
23064 * us a chance to move variables in registers out of the way.
23065 */
23066
23067 /* Modify the comparison operator */
23068 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023069 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023070 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023071 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023072 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023073 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023074 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023075 }
23076 /* Generate the instruction sequence that will transform the
23077 * result of the comparison into a logical value.
23078 */
Eric Biederman90089602004-05-28 14:11:54 +000023079 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023080 use_triple(ins, set);
23081 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023082
Eric Biederman90089602004-05-28 14:11:54 +000023083 convert = set;
23084 if (!equiv_types(ins->type, set->type)) {
23085 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23086 use_triple(set, convert);
23087 convert->template_id = TEMPLATE_COPY32_REG;
23088 }
23089
Eric Biedermanb138ac82003-04-22 18:44:01 +000023090 for(entry = ins->use; entry; entry = next) {
23091 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023092 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023093 continue;
23094 }
Eric Biederman90089602004-05-28 14:11:54 +000023095 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023096 }
Eric Biederman90089602004-05-28 14:11:54 +000023097 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023098}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023099
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023100struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23101{
23102 struct ins_template *template;
23103 struct reg_info result;
23104 int zlhs;
23105 if (ins->op == OP_PIECE) {
23106 index = ins->u.cval;
23107 ins = MISC(ins, 0);
23108 }
Eric Biederman90089602004-05-28 14:11:54 +000023109 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023110 if (triple_is_def(state, ins)) {
23111 zlhs = 1;
23112 }
23113 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023114 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023115 index, tops(ins->op));
23116 }
23117 switch(ins->op) {
23118 case OP_ASM:
23119 template = &ins->u.ainfo->tmpl;
23120 break;
23121 default:
23122 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023123 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023124 ins->template_id);
23125 }
23126 template = &templates[ins->template_id];
23127 break;
23128 }
23129 result = template->lhs[index];
23130 result.regcm = arch_regcm_normalize(state, result.regcm);
23131 if (result.reg != REG_UNNEEDED) {
23132 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23133 }
23134 if (result.regcm == 0) {
23135 internal_error(state, ins, "lhs %d regcm == 0", index);
23136 }
23137 return result;
23138}
23139
23140struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23141{
23142 struct reg_info result;
23143 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023144 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023145 (ins->op == OP_PIECE)) {
23146 internal_error(state, ins, "index %d out of range for %s\n",
23147 index, tops(ins->op));
23148 }
23149 switch(ins->op) {
23150 case OP_ASM:
23151 template = &ins->u.ainfo->tmpl;
23152 break;
Eric Biederman90089602004-05-28 14:11:54 +000023153 case OP_PHI:
23154 index = 0;
23155 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023156 default:
23157 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023158 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023159 ins->template_id);
23160 }
23161 template = &templates[ins->template_id];
23162 break;
23163 }
23164 result = template->rhs[index];
23165 result.regcm = arch_regcm_normalize(state, result.regcm);
23166 if (result.regcm == 0) {
23167 internal_error(state, ins, "rhs %d regcm == 0", index);
23168 }
23169 return result;
23170}
23171
Eric Biederman530b5192003-07-01 10:05:30 +000023172static struct triple *mod_div(struct compile_state *state,
23173 struct triple *ins, int div_op, int index)
23174{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023175 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023176
Eric Biederman530b5192003-07-01 10:05:30 +000023177 /* Generate the appropriate division instruction */
23178 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23179 RHS(div, 0) = RHS(ins, 0);
23180 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023181 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023182 div->template_id = TEMPLATE_DIV32;
23183 use_triple(RHS(div, 0), div);
23184 use_triple(RHS(div, 1), div);
23185 use_triple(LHS(div, 0), div);
23186 use_triple(LHS(div, 1), div);
23187
Eric Biederman530b5192003-07-01 10:05:30 +000023188 /* Replate uses of ins with the appropriate piece of the div */
23189 propogate_use(state, ins, LHS(div, index));
23190 release_triple(state, ins);
23191
23192 /* Return the address of the next instruction */
23193 return piece1->next;
23194}
23195
Eric Biederman90089602004-05-28 14:11:54 +000023196static int noop_adecl(struct triple *adecl)
23197{
23198 struct triple_set *use;
23199 /* It's a noop if it doesn't specify stoorage */
23200 if (adecl->lhs == 0) {
23201 return 1;
23202 }
23203 /* Is the adecl used? If not it's a noop */
23204 for(use = adecl->use; use ; use = use->next) {
23205 if ((use->member->op != OP_PIECE) ||
23206 (MISC(use->member, 0) != adecl)) {
23207 return 0;
23208 }
23209 }
23210 return 1;
23211}
23212
23213static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23214{
23215 struct triple *mask, *nmask, *shift;
23216 struct triple *val, *val_mask, *val_shift;
23217 struct triple *targ, *targ_mask;
23218 struct triple *new;
23219 ulong_t the_mask, the_nmask;
23220
23221 targ = RHS(ins, 0);
23222 val = RHS(ins, 1);
23223
23224 /* Get constant for the mask value */
23225 the_mask = 1;
23226 the_mask <<= ins->u.bitfield.size;
23227 the_mask -= 1;
23228 the_mask <<= ins->u.bitfield.offset;
23229 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23230 mask->u.cval = the_mask;
23231
23232 /* Get the inverted mask value */
23233 the_nmask = ~the_mask;
23234 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23235 nmask->u.cval = the_nmask;
23236
23237 /* Get constant for the shift value */
23238 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23239 shift->u.cval = ins->u.bitfield.offset;
23240
23241 /* Shift and mask the source value */
23242 val_shift = val;
23243 if (shift->u.cval != 0) {
23244 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23245 use_triple(val, val_shift);
23246 use_triple(shift, val_shift);
23247 }
23248 val_mask = val_shift;
23249 if (is_signed(val->type)) {
23250 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23251 use_triple(val_shift, val_mask);
23252 use_triple(mask, val_mask);
23253 }
23254
23255 /* Mask the target value */
23256 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23257 use_triple(targ, targ_mask);
23258 use_triple(nmask, targ_mask);
23259
23260 /* Now combined them together */
23261 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23262 use_triple(targ_mask, new);
23263 use_triple(val_mask, new);
23264
23265 /* Move all of the users over to the new expression */
23266 propogate_use(state, ins, new);
23267
23268 /* Delete the original triple */
23269 release_triple(state, ins);
23270
23271 /* Restart the transformation at mask */
23272 return mask;
23273}
23274
23275static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23276{
23277 struct triple *mask, *shift;
23278 struct triple *val, *val_mask, *val_shift;
23279 ulong_t the_mask;
23280
23281 val = RHS(ins, 0);
23282
23283 /* Get constant for the mask value */
23284 the_mask = 1;
23285 the_mask <<= ins->u.bitfield.size;
23286 the_mask -= 1;
23287 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23288 mask->u.cval = the_mask;
23289
23290 /* Get constant for the right shift value */
23291 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23292 shift->u.cval = ins->u.bitfield.offset;
23293
23294 /* Shift arithmetic right, to correct the sign */
23295 val_shift = val;
23296 if (shift->u.cval != 0) {
23297 int op;
23298 if (ins->op == OP_SEXTRACT) {
23299 op = OP_SSR;
23300 } else {
23301 op = OP_USR;
23302 }
23303 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23304 use_triple(val, val_shift);
23305 use_triple(shift, val_shift);
23306 }
23307
23308 /* Finally mask the value */
23309 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23310 use_triple(val_shift, val_mask);
23311 use_triple(mask, val_mask);
23312
23313 /* Move all of the users over to the new expression */
23314 propogate_use(state, ins, val_mask);
23315
23316 /* Release the original instruction */
23317 release_triple(state, ins);
23318
23319 return mask;
23320
23321}
23322
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023323static struct triple *transform_to_arch_instruction(
23324 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023325{
23326 /* Transform from generic 3 address instructions
23327 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023328 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023329 * Copies are inserted to preserve the register flexibility
23330 * of 3 address instructions.
23331 */
Eric Biederman90089602004-05-28 14:11:54 +000023332 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023333 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023334 next = ins->next;
23335 switch(ins->op) {
23336 case OP_INTCONST:
23337 ins->template_id = TEMPLATE_INTCONST32;
23338 if (ins->u.cval < 256) {
23339 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023340 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023341 break;
23342 case OP_ADDRCONST:
23343 ins->template_id = TEMPLATE_INTCONST32;
23344 break;
Eric Biederman90089602004-05-28 14:11:54 +000023345 case OP_UNKNOWNVAL:
23346 ins->template_id = TEMPLATE_UNKNOWNVAL;
23347 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023348 case OP_NOOP:
23349 case OP_SDECL:
23350 case OP_BLOBCONST:
23351 case OP_LABEL:
23352 ins->template_id = TEMPLATE_NOP;
23353 break;
23354 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023355 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023356 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023357 value = RHS(ins, 0);
23358 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023359 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023360 }
Eric Biederman90089602004-05-28 14:11:54 +000023361 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023362 ins->template_id = TEMPLATE_COPY_IMM16;
23363 }
Eric Biederman90089602004-05-28 14:11:54 +000023364 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023365 ins->template_id = TEMPLATE_COPY_IMM32;
23366 }
Eric Biederman90089602004-05-28 14:11:54 +000023367 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023368 internal_error(state, ins, "bad constant passed to copy");
23369 }
Eric Biederman90089602004-05-28 14:11:54 +000023370 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023371 ins->template_id = TEMPLATE_COPY8_REG;
23372 }
Eric Biederman90089602004-05-28 14:11:54 +000023373 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023374 ins->template_id = TEMPLATE_COPY16_REG;
23375 }
Eric Biederman90089602004-05-28 14:11:54 +000023376 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023377 ins->template_id = TEMPLATE_COPY32_REG;
23378 }
23379 else {
23380 internal_error(state, ins, "bad type passed to copy");
23381 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023382 break;
23383 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023384 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023385 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023386 ins->template_id = TEMPLATE_PHI8;
23387 }
Eric Biederman90089602004-05-28 14:11:54 +000023388 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023389 ins->template_id = TEMPLATE_PHI16;
23390 }
Eric Biederman90089602004-05-28 14:11:54 +000023391 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023392 ins->template_id = TEMPLATE_PHI32;
23393 }
23394 else {
23395 internal_error(state, ins, "bad type passed to phi");
23396 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023397 break;
Eric Biederman90089602004-05-28 14:11:54 +000023398 case OP_ADECL:
23399 /* Adecls should always be treated as dead code and
23400 * removed. If we are not optimizing they may linger.
23401 */
23402 if (!noop_adecl(ins)) {
23403 internal_error(state, ins, "adecl remains?");
23404 }
23405 ins->template_id = TEMPLATE_NOP;
23406 next = after_lhs(state, ins);
23407 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023408 case OP_STORE:
23409 switch(ins->type->type & TYPE_MASK) {
23410 case TYPE_CHAR: case TYPE_UCHAR:
23411 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023412 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023413 case TYPE_SHORT: case TYPE_USHORT:
23414 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023415 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023416 case TYPE_INT: case TYPE_UINT:
23417 case TYPE_LONG: case TYPE_ULONG:
23418 case TYPE_POINTER:
23419 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023420 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023421 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023422 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023423 break;
23424 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023425 break;
23426 case OP_LOAD:
23427 switch(ins->type->type & TYPE_MASK) {
23428 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023429 case TYPE_SHORT: case TYPE_USHORT:
23430 case TYPE_INT: case TYPE_UINT:
23431 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023432 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023433 break;
23434 default:
23435 internal_error(state, ins, "unknown type in load");
23436 break;
23437 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023438 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023439 break;
23440 case OP_ADD:
23441 case OP_SUB:
23442 case OP_AND:
23443 case OP_XOR:
23444 case OP_OR:
23445 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023446 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023447 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023448 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023449 }
23450 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023451 case OP_SDIVT:
23452 case OP_UDIVT:
23453 ins->template_id = TEMPLATE_DIV32;
23454 next = after_lhs(state, ins);
23455 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023456 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023457 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023458 break;
23459 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023460 next = mod_div(state, ins, OP_UDIVT, 0);
23461 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023462 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023463 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023464 break;
23465 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023466 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023467 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023468 case OP_SMOD:
23469 next = mod_div(state, ins, OP_SDIVT, 1);
23470 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023471 case OP_SL:
23472 case OP_SSR:
23473 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023474 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023475 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023476 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023477 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23478 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023479 }
23480 break;
23481 case OP_INVERT:
23482 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023483 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023484 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023485 case OP_EQ:
23486 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023487 break;
23488 case OP_NOTEQ:
23489 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23490 break;
23491 case OP_SLESS:
23492 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23493 break;
23494 case OP_ULESS:
23495 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23496 break;
23497 case OP_SMORE:
23498 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23499 break;
23500 case OP_UMORE:
23501 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23502 break;
23503 case OP_SLESSEQ:
23504 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23505 break;
23506 case OP_ULESSEQ:
23507 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23508 break;
23509 case OP_SMOREEQ:
23510 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23511 break;
23512 case OP_UMOREEQ:
23513 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23514 break;
23515 case OP_LTRUE:
23516 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23517 break;
23518 case OP_LFALSE:
23519 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23520 break;
23521 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023522 ins->op = OP_JMP;
23523 ins->template_id = TEMPLATE_NOP;
23524 break;
23525 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023526 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023527 RHS(ins, 0)->type, RHS(ins, 0), 0);
23528 break;
23529 case OP_CALL:
23530 ins->template_id = TEMPLATE_NOP;
23531 break;
23532 case OP_RET:
23533 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023534 break;
23535 case OP_INB:
23536 case OP_INW:
23537 case OP_INL:
23538 switch(ins->op) {
23539 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23540 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23541 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23542 }
23543 if (get_imm8(ins, &RHS(ins, 0))) {
23544 ins->template_id += 1;
23545 }
23546 break;
23547 case OP_OUTB:
23548 case OP_OUTW:
23549 case OP_OUTL:
23550 switch(ins->op) {
23551 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23552 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23553 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23554 }
23555 if (get_imm8(ins, &RHS(ins, 1))) {
23556 ins->template_id += 1;
23557 }
23558 break;
23559 case OP_BSF:
23560 case OP_BSR:
23561 ins->template_id = TEMPLATE_BSF;
23562 break;
23563 case OP_RDMSR:
23564 ins->template_id = TEMPLATE_RDMSR;
23565 next = after_lhs(state, ins);
23566 break;
23567 case OP_WRMSR:
23568 ins->template_id = TEMPLATE_WRMSR;
23569 break;
23570 case OP_HLT:
23571 ins->template_id = TEMPLATE_NOP;
23572 break;
23573 case OP_ASM:
23574 ins->template_id = TEMPLATE_NOP;
23575 next = after_lhs(state, ins);
23576 break;
23577 /* Already transformed instructions */
23578 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023579 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023580 break;
23581 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023582 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023583 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023584 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023585 }
23586 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023587 case OP_JMP:
23588 ins->template_id = TEMPLATE_NOP;
23589 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023590 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23591 case OP_JMP_SLESS: case OP_JMP_ULESS:
23592 case OP_JMP_SMORE: case OP_JMP_UMORE:
23593 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23594 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23595 ins->template_id = TEMPLATE_JMP;
23596 break;
23597 case OP_SET_EQ: case OP_SET_NOTEQ:
23598 case OP_SET_SLESS: case OP_SET_ULESS:
23599 case OP_SET_SMORE: case OP_SET_UMORE:
23600 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23601 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23602 ins->template_id = TEMPLATE_SET;
23603 break;
Eric Biederman90089602004-05-28 14:11:54 +000023604 case OP_DEPOSIT:
23605 next = x86_deposit(state, ins);
23606 break;
23607 case OP_SEXTRACT:
23608 case OP_UEXTRACT:
23609 next = x86_extract(state, ins);
23610 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023611 /* Unhandled instructions */
23612 case OP_PIECE:
23613 default:
Eric Biederman90089602004-05-28 14:11:54 +000023614 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023615 ins->op, tops(ins->op));
23616 break;
23617 }
23618 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023619}
23620
Eric Biederman530b5192003-07-01 10:05:30 +000023621static long next_label(struct compile_state *state)
23622{
Eric Biederman90089602004-05-28 14:11:54 +000023623 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023624 return ++label_counter;
23625}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023626static void generate_local_labels(struct compile_state *state)
23627{
23628 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023629 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023630 label = first;
23631 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023632 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023633 (label->op == OP_SDECL)) {
23634 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023635 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023636 } else {
23637 label->u.cval = 0;
23638 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023639
Eric Biedermanb138ac82003-04-22 18:44:01 +000023640 }
23641 label = label->next;
23642 } while(label != first);
23643}
23644
Stefan Reinauer14e22772010-04-27 06:56:47 +000023645static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023646 struct triple *triple, int classes)
23647{
23648 unsigned mask;
23649 int reg;
23650 reg = ID_REG(triple->id);
23651 if (reg == REG_UNSET) {
23652 internal_error(state, triple, "register not set");
23653 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023654 mask = arch_reg_regcm(state, reg);
23655 if (!(classes & mask)) {
23656 internal_error(state, triple, "reg %d in wrong class",
23657 reg);
23658 }
23659 return reg;
23660}
23661
Eric Biederman90089602004-05-28 14:11:54 +000023662
Eric Biederman530b5192003-07-01 10:05:30 +000023663#if REG_XMM7 != 44
23664#error "Registers have renumberd fix arch_reg_str"
23665#endif
Eric Biederman90089602004-05-28 14:11:54 +000023666static const char *arch_regs[] = {
23667 "%unset",
23668 "%unneeded",
23669 "%eflags",
23670 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23671 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23672 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23673 "%edx:%eax",
23674 "%dx:%ax",
23675 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023676 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023677 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23678};
23679static const char *arch_reg_str(int reg)
23680{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023681 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23682 reg = 0;
23683 }
Eric Biederman90089602004-05-28 14:11:54 +000023684 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023685}
23686
23687static const char *reg(struct compile_state *state, struct triple *triple,
23688 int classes)
23689{
23690 int reg;
23691 reg = check_reg(state, triple, classes);
23692 return arch_reg_str(reg);
23693}
23694
Eric Biederman90089602004-05-28 14:11:54 +000023695static int arch_reg_size(int reg)
23696{
23697 int size;
23698 size = 0;
23699 if (reg == REG_EFLAGS) {
23700 size = 32;
23701 }
23702 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23703 size = 8;
23704 }
23705 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23706 size = 16;
23707 }
23708 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23709 size = 32;
23710 }
23711 else if (reg == REG_EDXEAX) {
23712 size = 64;
23713 }
23714 else if (reg == REG_DXAX) {
23715 size = 32;
23716 }
23717 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23718 size = 64;
23719 }
23720 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23721 size = 128;
23722 }
23723 return size;
23724}
23725
23726static int reg_size(struct compile_state *state, struct triple *ins)
23727{
23728 int reg;
23729 reg = ID_REG(ins->id);
23730 if (reg == REG_UNSET) {
23731 internal_error(state, ins, "register not set");
23732 }
23733 return arch_reg_size(reg);
23734}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023735
Eric Biederman90089602004-05-28 14:11:54 +000023736
23737
Eric Biedermanb138ac82003-04-22 18:44:01 +000023738const char *type_suffix(struct compile_state *state, struct type *type)
23739{
23740 const char *suffix;
23741 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023742 case SIZEOF_I8: suffix = "b"; break;
23743 case SIZEOF_I16: suffix = "w"; break;
23744 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023745 default:
23746 internal_error(state, 0, "unknown suffix");
23747 suffix = 0;
23748 break;
23749 }
23750 return suffix;
23751}
23752
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023753static void print_const_val(
23754 struct compile_state *state, struct triple *ins, FILE *fp)
23755{
23756 switch(ins->op) {
23757 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023758 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023759 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023760 break;
23761 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023762 if ((MISC(ins, 0)->op != OP_SDECL) &&
23763 (MISC(ins, 0)->op != OP_LABEL))
23764 {
Eric Biederman830c9882003-07-04 00:27:33 +000023765 internal_error(state, ins, "bad base for addrconst");
23766 }
23767 if (MISC(ins, 0)->u.cval <= 0) {
23768 internal_error(state, ins, "unlabeled constant");
23769 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023770 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023771 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023772 (unsigned long)(MISC(ins, 0)->u.cval),
23773 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023774 break;
23775 default:
23776 internal_error(state, ins, "unknown constant type");
23777 break;
23778 }
23779}
23780
Eric Biederman530b5192003-07-01 10:05:30 +000023781static void print_const(struct compile_state *state,
23782 struct triple *ins, FILE *fp)
23783{
23784 switch(ins->op) {
23785 case OP_INTCONST:
23786 switch(ins->type->type & TYPE_MASK) {
23787 case TYPE_CHAR:
23788 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023789 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023790 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023791 break;
23792 case TYPE_SHORT:
23793 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023794 fprintf(fp, ".short 0x%04lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023795 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023796 break;
23797 case TYPE_INT:
23798 case TYPE_UINT:
23799 case TYPE_LONG:
23800 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023801 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023802 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023803 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023804 break;
23805 default:
Eric Biederman90089602004-05-28 14:11:54 +000023806 fprintf(state->errout, "type: ");
23807 name_of(state->errout, ins->type);
23808 fprintf(state->errout, "\n");
23809 internal_error(state, ins, "Unknown constant type. Val: %lu",
23810 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023811 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023812
Eric Biederman530b5192003-07-01 10:05:30 +000023813 break;
23814 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023815 if ((MISC(ins, 0)->op != OP_SDECL) &&
23816 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023817 internal_error(state, ins, "bad base for addrconst");
23818 }
23819 if (MISC(ins, 0)->u.cval <= 0) {
23820 internal_error(state, ins, "unlabeled constant");
23821 }
23822 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023823 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023824 (unsigned long)(MISC(ins, 0)->u.cval),
23825 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023826 break;
23827 case OP_BLOBCONST:
23828 {
23829 unsigned char *blob;
23830 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023831 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023832 blob = ins->u.blob;
23833 for(i = 0; i < size; i++) {
23834 fprintf(fp, ".byte 0x%02x\n",
23835 blob[i]);
23836 }
23837 break;
23838 }
23839 default:
23840 internal_error(state, ins, "Unknown constant type");
23841 break;
23842 }
23843}
23844
23845#define TEXT_SECTION ".rom.text"
23846#define DATA_SECTION ".rom.data"
23847
23848static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023849 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023850{
Eric Biederman90089602004-05-28 14:11:54 +000023851 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023852 long ref;
23853 ref = next_label(state);
23854 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023855 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023856 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023857 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023858 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23859 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023860 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023861 }
Eric Biederman530b5192003-07-01 10:05:30 +000023862 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23863 return ref;
23864}
23865
Eric Biederman90089602004-05-28 14:11:54 +000023866static long get_mask_pool_ref(
23867 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23868{
23869 long ref;
23870 if (mask == 0xff) {
23871 ref = 1;
23872 }
23873 else if (mask == 0xffff) {
23874 ref = 2;
23875 }
23876 else {
23877 ref = 0;
23878 internal_error(state, ins, "unhandled mask value");
23879 }
23880 return ref;
23881}
23882
Eric Biedermanb138ac82003-04-22 18:44:01 +000023883static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023884 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023885{
23886 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023887 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023888 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023889 internal_error(state, ins, "invalid register assignment");
23890 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023891 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023892 fprintf(fp, "\t%s ", op);
23893 print_const_val(state, RHS(ins, 1), fp);
23894 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023895 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023896 }
23897 else {
23898 unsigned lmask, rmask;
23899 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023900 lreg = check_reg(state, RHS(ins, 0), mask);
23901 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023902 lmask = arch_reg_regcm(state, lreg);
23903 rmask = arch_reg_regcm(state, rreg);
23904 mask = lmask & rmask;
23905 fprintf(fp, "\t%s %s, %s\n",
23906 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023907 reg(state, RHS(ins, 1), mask),
23908 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023909 }
23910}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023911static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023912 const char *op, struct triple *ins, FILE *fp)
23913{
23914 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023915 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023916 fprintf(fp, "\t%s %s\n",
23917 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023918 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023919}
23920
23921static void print_op_shift(struct compile_state *state,
23922 const char *op, struct triple *ins, FILE *fp)
23923{
23924 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023925 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023926 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023927 internal_error(state, ins, "invalid register assignment");
23928 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023929 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023930 fprintf(fp, "\t%s ", op);
23931 print_const_val(state, RHS(ins, 1), fp);
23932 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023933 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023934 }
23935 else {
23936 fprintf(fp, "\t%s %s, %s\n",
23937 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023938 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023939 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023940 }
23941}
23942
23943static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23944{
23945 const char *op;
23946 int mask;
23947 int dreg;
23948 mask = 0;
23949 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023950 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023951 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23952 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23953 default:
23954 internal_error(state, ins, "not an in operation");
23955 op = 0;
23956 break;
23957 }
23958 dreg = check_reg(state, ins, mask);
23959 if (!reg_is_reg(state, dreg, REG_EAX)) {
23960 internal_error(state, ins, "dst != %%eax");
23961 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023962 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023963 fprintf(fp, "\t%s ", op);
23964 print_const_val(state, RHS(ins, 0), fp);
23965 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023966 reg(state, ins, mask));
23967 }
23968 else {
23969 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023970 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023971 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23972 internal_error(state, ins, "src != %%dx");
23973 }
23974 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023975 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023976 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023977 reg(state, ins, mask));
23978 }
23979}
23980
23981static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23982{
23983 const char *op;
23984 int mask;
23985 int lreg;
23986 mask = 0;
23987 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023988 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023989 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23990 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23991 default:
23992 internal_error(state, ins, "not an out operation");
23993 op = 0;
23994 break;
23995 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023996 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023997 if (!reg_is_reg(state, lreg, REG_EAX)) {
23998 internal_error(state, ins, "src != %%eax");
23999 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024000 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024001 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024002 op, reg(state, RHS(ins, 0), mask));
24003 print_const_val(state, RHS(ins, 1), fp);
24004 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024005 }
24006 else {
24007 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024008 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024009 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24010 internal_error(state, ins, "dst != %%dx");
24011 }
24012 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024013 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024014 reg(state, RHS(ins, 0), mask),
24015 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024016 }
24017}
24018
24019static void print_op_move(struct compile_state *state,
24020 struct triple *ins, FILE *fp)
24021{
24022 /* op_move is complex because there are many types
24023 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024024 * Because OP_COPY will be introduced in arbitrary locations
24025 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024026 * OP_CONVERT can change the flags and it is the only operation
24027 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024028 */
24029 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24030 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024031 if (state->arch->features & X86_NOOP_COPY) {
24032 omit_copy = 0;
24033 }
24034 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024035 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024036 dst = ins;
24037 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024038 else {
24039 internal_error(state, ins, "unknown move operation");
24040 src = dst = 0;
24041 }
Eric Biederman90089602004-05-28 14:11:54 +000024042 if (reg_size(state, dst) < size_of(state, dst->type)) {
24043 internal_error(state, ins, "Invalid destination register");
24044 }
24045 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24046 fprintf(state->errout, "src type: ");
24047 name_of(state->errout, src->type);
24048 fprintf(state->errout, "\n");
24049 fprintf(state->errout, "dst type: ");
24050 name_of(state->errout, dst->type);
24051 fprintf(state->errout, "\n");
24052 internal_error(state, ins, "Type mismatch for OP_COPY");
24053 }
24054
Eric Biederman0babc1c2003-05-09 02:39:00 +000024055 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024056 int src_reg, dst_reg;
24057 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024058 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024059 dst_reg = ID_REG(dst->id);
24060 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024061 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024062 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024063 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024064 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024065 if ((src_reg != dst_reg) || !omit_copy) {
24066 fprintf(fp, "\tmov %s, %s\n",
24067 reg(state, src, src_regcm),
24068 reg(state, dst, dst_regcm));
24069 }
24070 }
24071 /* Move 32bit to 16bit */
24072 else if ((src_regcm & REGCM_GPR32) &&
24073 (dst_regcm & REGCM_GPR16)) {
24074 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24075 if ((src_reg != dst_reg) || !omit_copy) {
24076 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024077 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024078 arch_reg_str(dst_reg));
24079 }
24080 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024081 /* Move from 32bit gprs to 16bit gprs */
24082 else if ((src_regcm & REGCM_GPR32) &&
24083 (dst_regcm & REGCM_GPR16)) {
24084 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24085 if ((src_reg != dst_reg) || !omit_copy) {
24086 fprintf(fp, "\tmov %s, %s\n",
24087 arch_reg_str(src_reg),
24088 arch_reg_str(dst_reg));
24089 }
24090 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024091 /* Move 32bit to 8bit */
24092 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024093 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024094 {
24095 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24096 if ((src_reg != dst_reg) || !omit_copy) {
24097 fprintf(fp, "\tmovb %s, %s\n",
24098 arch_reg_str(src_reg),
24099 arch_reg_str(dst_reg));
24100 }
24101 }
24102 /* Move 16bit to 8bit */
24103 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024104 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024105 {
24106 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24107 if ((src_reg != dst_reg) || !omit_copy) {
24108 fprintf(fp, "\tmovb %s, %s\n",
24109 arch_reg_str(src_reg),
24110 arch_reg_str(dst_reg));
24111 }
24112 }
24113 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024114 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024115 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024116 const char *op;
24117 op = is_signed(src->type)? "movsx": "movzx";
24118 fprintf(fp, "\t%s %s, %s\n",
24119 op,
24120 reg(state, src, src_regcm),
24121 reg(state, dst, dst_regcm));
24122 }
24123 /* Move between sse registers */
24124 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24125 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024126 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024127 reg(state, src, src_regcm),
24128 reg(state, dst, dst_regcm));
24129 }
24130 }
Eric Biederman530b5192003-07-01 10:05:30 +000024131 /* Move between mmx registers */
24132 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024133 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024134 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024135 reg(state, src, src_regcm),
24136 reg(state, dst, dst_regcm));
24137 }
24138 }
Eric Biederman530b5192003-07-01 10:05:30 +000024139 /* Move from sse to mmx registers */
24140 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24141 fprintf(fp, "\tmovdq2q %s, %s\n",
24142 reg(state, src, src_regcm),
24143 reg(state, dst, dst_regcm));
24144 }
24145 /* Move from mmx to sse registers */
24146 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24147 fprintf(fp, "\tmovq2dq %s, %s\n",
24148 reg(state, src, src_regcm),
24149 reg(state, dst, dst_regcm));
24150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024151 /* Move between 32bit gprs & mmx/sse registers */
24152 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24153 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24154 fprintf(fp, "\tmovd %s, %s\n",
24155 reg(state, src, src_regcm),
24156 reg(state, dst, dst_regcm));
24157 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024158 /* Move from 16bit gprs & mmx/sse registers */
24159 else if ((src_regcm & REGCM_GPR16) &&
24160 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24161 const char *op;
24162 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024163 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024164 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24165 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24166 op,
24167 arch_reg_str(src_reg),
24168 arch_reg_str(mid_reg),
24169 arch_reg_str(mid_reg),
24170 arch_reg_str(dst_reg));
24171 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024172 /* Move from mmx/sse registers to 16bit gprs */
24173 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24174 (dst_regcm & REGCM_GPR16)) {
24175 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24176 fprintf(fp, "\tmovd %s, %s\n",
24177 arch_reg_str(src_reg),
24178 arch_reg_str(dst_reg));
24179 }
Eric Biederman530b5192003-07-01 10:05:30 +000024180 /* Move from gpr to 64bit dividend */
24181 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24182 (dst_regcm & REGCM_DIVIDEND64)) {
24183 const char *extend;
24184 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24185 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024186 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024187 extend);
24188 }
24189 /* Move from 64bit gpr to gpr */
24190 else if ((src_regcm & REGCM_DIVIDEND64) &&
24191 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24192 if (dst_regcm & REGCM_GPR32) {
24193 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024194 }
Eric Biederman530b5192003-07-01 10:05:30 +000024195 else if (dst_regcm & REGCM_GPR16) {
24196 src_reg = REG_AX;
24197 }
24198 else if (dst_regcm & REGCM_GPR8_LO) {
24199 src_reg = REG_AL;
24200 }
24201 fprintf(fp, "\tmov %s, %s\n",
24202 arch_reg_str(src_reg),
24203 arch_reg_str(dst_reg));
24204 }
24205 /* Move from mmx/sse registers to 64bit gpr */
24206 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24207 (dst_regcm & REGCM_DIVIDEND64)) {
24208 const char *extend;
24209 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24210 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24211 arch_reg_str(src_reg),
24212 extend);
24213 }
24214 /* Move from 64bit gpr to mmx/sse register */
24215 else if ((src_regcm & REGCM_DIVIDEND64) &&
24216 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24217 fprintf(fp, "\tmovd %%eax, %s\n",
24218 arch_reg_str(dst_reg));
24219 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024220#if X86_4_8BIT_GPRS
24221 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024222 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024223 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24224 const char *op;
24225 int mid_reg;
24226 op = is_signed(src->type)? "movsx":"movzx";
24227 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24228 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24229 op,
24230 reg(state, src, src_regcm),
24231 arch_reg_str(mid_reg),
24232 arch_reg_str(mid_reg),
24233 reg(state, dst, dst_regcm));
24234 }
24235 /* Move from mmx/sse registers and 8bit gprs */
24236 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024237 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024238 int mid_reg;
24239 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24240 fprintf(fp, "\tmovd %s, %s\n",
24241 reg(state, src, src_regcm),
24242 arch_reg_str(mid_reg));
24243 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024244 /* Move from 32bit gprs to 8bit gprs */
24245 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024246 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024247 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24248 if ((src_reg != dst_reg) || !omit_copy) {
24249 fprintf(fp, "\tmov %s, %s\n",
24250 arch_reg_str(src_reg),
24251 arch_reg_str(dst_reg));
24252 }
24253 }
24254 /* Move from 16bit gprs to 8bit gprs */
24255 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024256 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024257 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24258 if ((src_reg != dst_reg) || !omit_copy) {
24259 fprintf(fp, "\tmov %s, %s\n",
24260 arch_reg_str(src_reg),
24261 arch_reg_str(dst_reg));
24262 }
24263 }
24264#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024265 /* Move from %eax:%edx to %eax:%edx */
24266 else if ((src_regcm & REGCM_DIVIDEND64) &&
24267 (dst_regcm & REGCM_DIVIDEND64) &&
24268 (src_reg == dst_reg)) {
24269 if (!omit_copy) {
24270 fprintf(fp, "\t/*mov %s, %s*/\n",
24271 arch_reg_str(src_reg),
24272 arch_reg_str(dst_reg));
24273 }
24274 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024275 else {
Eric Biederman90089602004-05-28 14:11:54 +000024276 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24277 internal_error(state, ins, "attempt to copy from %%eflags!");
24278 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024279 internal_error(state, ins, "unknown copy type");
24280 }
24281 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024282 else {
Eric Biederman90089602004-05-28 14:11:54 +000024283 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024284 int dst_reg;
24285 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024286 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024287 dst_reg = ID_REG(dst->id);
24288 dst_regcm = arch_reg_regcm(state, dst_reg);
24289 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24290 fprintf(fp, "\tmov ");
24291 print_const_val(state, src, fp);
24292 fprintf(fp, ", %s\n",
24293 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24294 }
24295 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024296 if (dst_size > SIZEOF_I32) {
24297 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024298 }
24299 fprintf(fp, "\tmov $0, %%edx\n");
24300 fprintf(fp, "\tmov ");
24301 print_const_val(state, src, fp);
24302 fprintf(fp, ", %%eax\n");
24303 }
24304 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024305 if (dst_size > SIZEOF_I16) {
24306 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024307 }
24308 fprintf(fp, "\tmov $0, %%dx\n");
24309 fprintf(fp, "\tmov ");
24310 print_const_val(state, src, fp);
24311 fprintf(fp, ", %%ax");
24312 }
24313 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24314 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024315 if (dst_size > SIZEOF_I32) {
24316 internal_error(state, ins, "%d bit constant...", dst_size);
24317 }
24318 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024319 fprintf(fp, "\tmovd L%s%lu, %s\n",
24320 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024321 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24322 }
24323 else {
24324 internal_error(state, ins, "unknown copy immediate type");
24325 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024326 }
Eric Biederman90089602004-05-28 14:11:54 +000024327 /* Leave now if this is not a type conversion */
24328 if (ins->op != OP_CONVERT) {
24329 return;
24330 }
24331 /* Now make certain I have not logically overflowed the destination */
24332 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24333 (size_of(state, dst->type) < reg_size(state, dst)))
24334 {
24335 unsigned long mask;
24336 int dst_reg;
24337 int dst_regcm;
24338 if (size_of(state, dst->type) >= 32) {
24339 fprintf(state->errout, "dst type: ");
24340 name_of(state->errout, dst->type);
24341 fprintf(state->errout, "\n");
24342 internal_error(state, dst, "unhandled dst type size");
24343 }
24344 mask = 1;
24345 mask <<= size_of(state, dst->type);
24346 mask -= 1;
24347
24348 dst_reg = ID_REG(dst->id);
24349 dst_regcm = arch_reg_regcm(state, dst_reg);
24350
24351 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24352 fprintf(fp, "\tand $0x%lx, %s\n",
24353 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24354 }
24355 else if (dst_regcm & REGCM_MMX) {
24356 long ref;
24357 ref = get_mask_pool_ref(state, dst, mask, fp);
24358 fprintf(fp, "\tpand L%s%lu, %s\n",
24359 state->compiler->label_prefix, ref,
24360 reg(state, dst, REGCM_MMX));
24361 }
24362 else if (dst_regcm & REGCM_XMM) {
24363 long ref;
24364 ref = get_mask_pool_ref(state, dst, mask, fp);
24365 fprintf(fp, "\tpand L%s%lu, %s\n",
24366 state->compiler->label_prefix, ref,
24367 reg(state, dst, REGCM_XMM));
24368 }
24369 else {
24370 fprintf(state->errout, "dst type: ");
24371 name_of(state->errout, dst->type);
24372 fprintf(state->errout, "\n");
24373 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24374 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24375 }
24376 }
24377 /* Make certain I am properly sign extended */
24378 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24379 (is_signed(src->type)))
24380 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024381 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024382 int dst_reg;
24383 int dst_regcm;
24384
Eric Biederman90089602004-05-28 14:11:54 +000024385 reg_bits = reg_size(state, dst);
24386 if (reg_bits > 32) {
24387 reg_bits = 32;
24388 }
24389 shift_bits = reg_bits - size_of(state, src->type);
24390 dst_reg = ID_REG(dst->id);
24391 dst_regcm = arch_reg_regcm(state, dst_reg);
24392
24393 if (shift_bits < 0) {
24394 internal_error(state, dst, "negative shift?");
24395 }
24396
24397 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024398 fprintf(fp, "\tshl $%d, %s\n",
24399 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024400 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024401 fprintf(fp, "\tsar $%d, %s\n",
24402 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024403 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24404 }
24405 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24406 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024407 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024408 reg(state, dst, REGCM_MMX | REGCM_XMM));
24409 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024410 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024411 reg(state, dst, REGCM_MMX | REGCM_XMM));
24412 }
24413 else {
24414 fprintf(state->errout, "dst type: ");
24415 name_of(state->errout, dst->type);
24416 fprintf(state->errout, "\n");
24417 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24418 internal_error(state, dst, "failed to signed extend value");
24419 }
24420 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024421}
24422
24423static void print_op_load(struct compile_state *state,
24424 struct triple *ins, FILE *fp)
24425{
24426 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024427 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024428 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024429 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024430 if (is_const(src) || is_const(dst)) {
24431 internal_error(state, ins, "unknown load operation");
24432 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024433 switch(ins->type->type & TYPE_MASK) {
24434 case TYPE_CHAR: op = "movsbl"; break;
24435 case TYPE_UCHAR: op = "movzbl"; break;
24436 case TYPE_SHORT: op = "movswl"; break;
24437 case TYPE_USHORT: op = "movzwl"; break;
24438 case TYPE_INT: case TYPE_UINT:
24439 case TYPE_LONG: case TYPE_ULONG:
24440 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024441 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024442 break;
24443 default:
24444 internal_error(state, ins, "unknown type in load");
24445 op = "<invalid opcode>";
24446 break;
24447 }
24448 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024449 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024450 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024451 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024452}
24453
24454
24455static void print_op_store(struct compile_state *state,
24456 struct triple *ins, FILE *fp)
24457{
24458 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024459 dst = RHS(ins, 0);
24460 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024461 if (is_const(src) && (src->op == OP_INTCONST)) {
24462 long_t value;
24463 value = (long_t)(src->u.cval);
24464 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24465 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024466 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024467 reg(state, dst, REGCM_GPR32));
24468 }
24469 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24470 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24471 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024472 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024473 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024474 }
24475 else {
24476 if (is_const(src) || is_const(dst)) {
24477 internal_error(state, ins, "unknown store operation");
24478 }
24479 fprintf(fp, "\tmov%s %s, (%s)\n",
24480 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024481 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024482 reg(state, dst, REGCM_GPR32));
24483 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024484
24485
Eric Biedermanb138ac82003-04-22 18:44:01 +000024486}
24487
24488static void print_op_smul(struct compile_state *state,
24489 struct triple *ins, FILE *fp)
24490{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024491 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024492 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024493 reg(state, RHS(ins, 1), REGCM_GPR32),
24494 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024495 }
24496 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024497 fprintf(fp, "\timul ");
24498 print_const_val(state, RHS(ins, 1), fp);
24499 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024500 }
24501}
24502
24503static void print_op_cmp(struct compile_state *state,
24504 struct triple *ins, FILE *fp)
24505{
24506 unsigned mask;
24507 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024508 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024509 dreg = check_reg(state, ins, REGCM_FLAGS);
24510 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24511 internal_error(state, ins, "bad dest register for cmp");
24512 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024513 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024514 fprintf(fp, "\tcmp ");
24515 print_const_val(state, RHS(ins, 1), fp);
24516 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024517 }
24518 else {
24519 unsigned lmask, rmask;
24520 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024521 lreg = check_reg(state, RHS(ins, 0), mask);
24522 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024523 lmask = arch_reg_regcm(state, lreg);
24524 rmask = arch_reg_regcm(state, rreg);
24525 mask = lmask & rmask;
24526 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024527 reg(state, RHS(ins, 1), mask),
24528 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024529 }
24530}
24531
24532static void print_op_test(struct compile_state *state,
24533 struct triple *ins, FILE *fp)
24534{
24535 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024536 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024537 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024538 reg(state, RHS(ins, 0), mask),
24539 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024540}
24541
24542static void print_op_branch(struct compile_state *state,
24543 struct triple *branch, FILE *fp)
24544{
24545 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024546 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024547 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024548 internal_error(state, branch, "jmp with condition?");
24549 }
24550 bop = "jmp";
24551 }
24552 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024553 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024554 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024555 internal_error(state, branch, "jmpcc without condition?");
24556 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024557 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24558 if ((RHS(branch, 0)->op != OP_CMP) &&
24559 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024560 internal_error(state, branch, "bad branch test");
24561 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024562#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024563#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024564#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024565 ptr = RHS(branch, 0);
24566 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24567 if (ptr->op != OP_COPY) {
24568 internal_error(state, branch, "branch does not follow test");
24569 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024570 }
24571 switch(branch->op) {
24572 case OP_JMP_EQ: bop = "jz"; break;
24573 case OP_JMP_NOTEQ: bop = "jnz"; break;
24574 case OP_JMP_SLESS: bop = "jl"; break;
24575 case OP_JMP_ULESS: bop = "jb"; break;
24576 case OP_JMP_SMORE: bop = "jg"; break;
24577 case OP_JMP_UMORE: bop = "ja"; break;
24578 case OP_JMP_SLESSEQ: bop = "jle"; break;
24579 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24580 case OP_JMP_SMOREEQ: bop = "jge"; break;
24581 case OP_JMP_UMOREEQ: bop = "jae"; break;
24582 default:
24583 internal_error(state, branch, "Invalid branch op");
24584 break;
24585 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024586
Eric Biedermanb138ac82003-04-22 18:44:01 +000024587 }
Eric Biederman90089602004-05-28 14:11:54 +000024588#if 1
24589 if (branch->op == OP_CALL) {
24590 fprintf(fp, "\t/* call */\n");
24591 }
24592#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024593 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024594 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024595 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024596 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024597}
24598
Eric Biederman5ade04a2003-10-22 04:03:46 +000024599static void print_op_ret(struct compile_state *state,
24600 struct triple *branch, FILE *fp)
24601{
24602 fprintf(fp, "\tjmp *%s\n",
24603 reg(state, RHS(branch, 0), REGCM_GPR32));
24604}
24605
Eric Biedermanb138ac82003-04-22 18:44:01 +000024606static void print_op_set(struct compile_state *state,
24607 struct triple *set, FILE *fp)
24608{
24609 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024610 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024611 internal_error(state, set, "setcc without condition?");
24612 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024613 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24614 if ((RHS(set, 0)->op != OP_CMP) &&
24615 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024616 internal_error(state, set, "bad set test");
24617 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024618 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024619 internal_error(state, set, "set does not follow test");
24620 }
24621 switch(set->op) {
24622 case OP_SET_EQ: sop = "setz"; break;
24623 case OP_SET_NOTEQ: sop = "setnz"; break;
24624 case OP_SET_SLESS: sop = "setl"; break;
24625 case OP_SET_ULESS: sop = "setb"; break;
24626 case OP_SET_SMORE: sop = "setg"; break;
24627 case OP_SET_UMORE: sop = "seta"; break;
24628 case OP_SET_SLESSEQ: sop = "setle"; break;
24629 case OP_SET_ULESSEQ: sop = "setbe"; break;
24630 case OP_SET_SMOREEQ: sop = "setge"; break;
24631 case OP_SET_UMOREEQ: sop = "setae"; break;
24632 default:
24633 internal_error(state, set, "Invalid set op");
24634 break;
24635 }
24636 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024637 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024638}
24639
Stefan Reinauer14e22772010-04-27 06:56:47 +000024640static void print_op_bit_scan(struct compile_state *state,
24641 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024642{
24643 const char *op;
24644 switch(ins->op) {
24645 case OP_BSF: op = "bsf"; break;
24646 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024647 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024648 internal_error(state, ins, "unknown bit scan");
24649 op = 0;
24650 break;
24651 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024652 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024653 "\t%s %s, %s\n"
24654 "\tjnz 1f\n"
24655 "\tmovl $-1, %s\n"
24656 "1:\n",
24657 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024658 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024659 reg(state, ins, REGCM_GPR32),
24660 reg(state, ins, REGCM_GPR32));
24661}
24662
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024663
Eric Biedermanb138ac82003-04-22 18:44:01 +000024664static void print_sdecl(struct compile_state *state,
24665 struct triple *ins, FILE *fp)
24666{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024667 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024668 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024669 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024670 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024671 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024672 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024673
Eric Biedermanb138ac82003-04-22 18:44:01 +000024674}
24675
24676static void print_instruction(struct compile_state *state,
24677 struct triple *ins, FILE *fp)
24678{
24679 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024680 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024681 */
24682 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024683 case OP_ASM:
24684 print_op_asm(state, ins, fp);
24685 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024686 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24687 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24688 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24689 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24690 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24691 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24692 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24693 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24694 case OP_POS: break;
24695 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24696 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024697 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024698 case OP_INTCONST:
24699 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024700 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024701 /* Don't generate anything here for constants */
24702 case OP_PHI:
24703 /* Don't generate anything for variable declarations. */
24704 break;
Eric Biederman90089602004-05-28 14:11:54 +000024705 case OP_UNKNOWNVAL:
24706 fprintf(fp, " /* unknown %s */\n",
24707 reg(state, ins, REGCM_ALL));
24708 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024709 case OP_SDECL:
24710 print_sdecl(state, ins, fp);
24711 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024712 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024713 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024714 print_op_move(state, ins, fp);
24715 break;
24716 case OP_LOAD:
24717 print_op_load(state, ins, fp);
24718 break;
24719 case OP_STORE:
24720 print_op_store(state, ins, fp);
24721 break;
24722 case OP_SMUL:
24723 print_op_smul(state, ins, fp);
24724 break;
24725 case OP_CMP: print_op_cmp(state, ins, fp); break;
24726 case OP_TEST: print_op_test(state, ins, fp); break;
24727 case OP_JMP:
24728 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24729 case OP_JMP_SLESS: case OP_JMP_ULESS:
24730 case OP_JMP_SMORE: case OP_JMP_UMORE:
24731 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24732 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024733 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024734 print_op_branch(state, ins, fp);
24735 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024736 case OP_RET:
24737 print_op_ret(state, ins, fp);
24738 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024739 case OP_SET_EQ: case OP_SET_NOTEQ:
24740 case OP_SET_SLESS: case OP_SET_ULESS:
24741 case OP_SET_SMORE: case OP_SET_UMORE:
24742 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24743 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24744 print_op_set(state, ins, fp);
24745 break;
24746 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024747 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024748 break;
24749 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024750 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024751 break;
24752 case OP_BSF:
24753 case OP_BSR:
24754 print_op_bit_scan(state, ins, fp);
24755 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024756 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024757 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024758 fprintf(fp, "\trdmsr\n");
24759 break;
24760 case OP_WRMSR:
24761 fprintf(fp, "\twrmsr\n");
24762 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024763 case OP_HLT:
24764 fprintf(fp, "\thlt\n");
24765 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024766 case OP_SDIVT:
24767 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24768 break;
24769 case OP_UDIVT:
24770 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24771 break;
24772 case OP_UMUL:
24773 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24774 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024775 case OP_LABEL:
24776 if (!ins->use) {
24777 return;
24778 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024779 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024780 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024781 break;
Eric Biederman90089602004-05-28 14:11:54 +000024782 case OP_ADECL:
24783 /* Ignore adecls with no registers error otherwise */
24784 if (!noop_adecl(ins)) {
24785 internal_error(state, ins, "adecl remains?");
24786 }
24787 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024788 /* Ignore OP_PIECE */
24789 case OP_PIECE:
24790 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024791 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024792 case OP_SDIV: case OP_UDIV:
24793 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024794 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24795 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24796 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24797 default:
24798 internal_error(state, ins, "unknown op: %d %s",
24799 ins->op, tops(ins->op));
24800 break;
24801 }
24802}
24803
24804static void print_instructions(struct compile_state *state)
24805{
24806 struct triple *first, *ins;
24807 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024808 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024809 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024810 int max_inline_depth;
24811 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024812 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024813 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024814 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024815 /* Masks for common sizes */
24816 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24817 fprintf(fp, ".balign 16\n");
24818 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24819 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24820 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24821 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024822 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024823 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024824 ins = first;
24825 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024826 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024827 last_occurance != ins->occurance) {
24828 if (!ins->occurance->parent) {
24829 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024830 ins->occurance->function?ins->occurance->function:"(null)",
24831 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024832 ins->occurance->line,
24833 ins->occurance->col);
24834 }
24835 else {
24836 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024837 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024838 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024839 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024840 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024841 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024842 fprintf(fp, "\t * %s,%s:%d.%d\n",
24843 ptr->function,
24844 ptr->filename,
24845 ptr->line,
24846 ptr->col);
24847 }
24848 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024849 if (inline_depth > max_inline_depth) {
24850 max_inline_depth = inline_depth;
24851 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024852 }
24853 if (last_occurance) {
24854 put_occurance(last_occurance);
24855 }
24856 get_occurance(ins->occurance);
24857 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024858 }
24859
24860 print_instruction(state, ins, fp);
24861 ins = ins->next;
24862 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024863 if (print_location) {
24864 fprintf(fp, "/* max inline depth %d */\n",
24865 max_inline_depth);
24866 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024867}
Eric Biederman530b5192003-07-01 10:05:30 +000024868
Eric Biedermanb138ac82003-04-22 18:44:01 +000024869static void generate_code(struct compile_state *state)
24870{
24871 generate_local_labels(state);
24872 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024873
Eric Biedermanb138ac82003-04-22 18:44:01 +000024874}
24875
Eric Biederman90089602004-05-28 14:11:54 +000024876static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024877{
Eric Biederman90089602004-05-28 14:11:54 +000024878 int tok;
24879 FILE *fp;
24880 int line;
24881 const char *filename;
24882 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024883 filename = 0;
24884 line = 0;
24885 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024886 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024887 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024888 const char *token_str;
24889 tok = peek(state);
24890 if (tok == TOK_EOF) {
24891 break;
24892 }
Eric Biederman41203d92004-11-08 09:31:09 +000024893 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024894 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024895 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024896 tk->str_len ? tk->val.str :
24897 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024898
24899 file = state->file;
24900 while(file->macro && file->prev) {
24901 file = file->prev;
24902 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024903 if (!file->macro &&
24904 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024905 {
Eric Biederman90089602004-05-28 14:11:54 +000024906 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024907 if ((file->basename == filename) &&
24908 (line < file->line)) {
24909 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024910 fprintf(fp, "\n");
24911 line++;
24912 }
24913 }
24914 else {
24915 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024916 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024917 }
Eric Biederman132368b2004-11-09 08:59:23 +000024918 line = file->line;
24919 filename = file->basename;
24920 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024921 for(i = 0; i < col; i++) {
24922 fprintf(fp, " ");
24923 }
24924 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024925
Eric Biederman90089602004-05-28 14:11:54 +000024926 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024927
Eric Biederman90089602004-05-28 14:11:54 +000024928 if (state->compiler->debug & DEBUG_TOKENS) {
24929 loc(state->dbgout, state, 0);
24930 fprintf(state->dbgout, "%s <- `%s'\n",
24931 tokens[tok], token_str);
24932 }
24933 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024934}
24935
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024936static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024937 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024938{
24939 int i;
24940 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024941 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024942 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024943 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024944 state.compiler = compiler;
24945 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024946 state.file = 0;
24947 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24948 memset(&state.token[i], 0, sizeof(state.token[i]));
24949 state.token[i].tok = -1;
24950 }
Eric Biederman90089602004-05-28 14:11:54 +000024951 /* Remember the output descriptors */
24952 state.errout = stderr;
24953 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024954 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024955 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24956 state.output = stdout;
24957 } else {
24958 state.output = fopen(state.compiler->ofilename, "w");
24959 if (!state.output) {
24960 error(&state, 0, "Cannot open output file %s\n",
24961 state.compiler->ofilename);
24962 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024963 }
Eric Biederman90089602004-05-28 14:11:54 +000024964 /* Make certain a good cleanup happens */
24965 exit_state = &state;
24966 atexit(exit_cleanup);
24967
Eric Biedermanb138ac82003-04-22 18:44:01 +000024968 /* Prep the preprocessor */
24969 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024970 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024971 /* register the C keywords */
24972 register_keywords(&state);
24973 /* register the keywords the macro preprocessor knows */
24974 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024975 /* generate some builtin macros */
24976 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024977 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024978 state.i_switch = lookup(&state, "switch", 6);
24979 state.i_case = lookup(&state, "case", 4);
24980 state.i_continue = lookup(&state, "continue", 8);
24981 state.i_break = lookup(&state, "break", 5);
24982 state.i_default = lookup(&state, "default", 7);
24983 state.i_return = lookup(&state, "return", 6);
24984 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024985 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24986 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24987 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24988 /* Memorize where predefined identifiers are. */
24989 state.i___func__ = lookup(&state, "__func__", 8);
24990 /* Memorize where some attribute keywords are. */
24991 state.i_noinline = lookup(&state, "noinline", 8);
24992 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024993 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020024994 state.i_unused = lookup(&state, "unused", 6);
Stefan Reinauerec664bc2013-05-09 14:06:04 -070024995 state.i_packed = lookup(&state, "packed", 6);
Eric Biederman90089602004-05-28 14:11:54 +000024996
24997 /* Process the command line macros */
24998 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024999
25000 /* Allocate beginning bounding labels for the function list */
25001 state.first = label(&state);
25002 state.first->id |= TRIPLE_FLAG_VOLATILE;
25003 use_triple(state.first, state.first);
25004 ptr = label(&state);
25005 ptr->id |= TRIPLE_FLAG_VOLATILE;
25006 use_triple(ptr, ptr);
25007 flatten(&state, state.first, ptr);
25008
Eric Biederman5ade04a2003-10-22 04:03:46 +000025009 /* Allocate a label for the pool of global variables */
25010 state.global_pool = label(&state);
25011 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25012 flatten(&state, state.first, state.global_pool);
25013
Eric Biedermanb138ac82003-04-22 18:44:01 +000025014 /* Enter the globl definition scope */
25015 start_scope(&state);
25016 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025017
Eric Biedermanb138ac82003-04-22 18:44:01 +000025018 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025019
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025020 while (includes) {
25021 compile_file(&state, includes->filename, 1);
25022 includes=includes->next;
25023 }
Eric Biederman90089602004-05-28 14:11:54 +000025024
25025 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025026 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025027 print_preprocessed_tokens(&state);
25028 return;
25029 }
25030
Eric Biedermanb138ac82003-04-22 18:44:01 +000025031 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025032
Eric Biedermanb138ac82003-04-22 18:44:01 +000025033 /* Exit the global definition scope */
25034 end_scope(&state);
25035
Stefan Reinauer14e22772010-04-27 06:56:47 +000025036 /* Now that basic compilation has happened
25037 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025038 */
25039 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025040
Eric Biedermanb138ac82003-04-22 18:44:01 +000025041 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025042 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025043 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025044 }
Eric Biederman90089602004-05-28 14:11:54 +000025045 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025046}
25047
Eric Biederman90089602004-05-28 14:11:54 +000025048static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025049{
Eric Biederman90089602004-05-28 14:11:54 +000025050 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025051}
25052
25053static void usage(void)
25054{
Eric Biederman90089602004-05-28 14:11:54 +000025055 FILE *fp = stdout;
25056 version(fp);
25057 fprintf(fp,
25058 "\nUsage: romcc [options] <source>.c\n"
25059 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25060 "Options: \n"
25061 "-o <output file name>\n"
25062 "-f<option> Specify a generic compiler option\n"
25063 "-m<option> Specify a arch dependent option\n"
25064 "-- Specify this is the last option\n"
25065 "\nGeneric compiler options:\n"
25066 );
25067 compiler_usage(fp);
25068 fprintf(fp,
25069 "\nArchitecture compiler options:\n"
25070 );
25071 arch_usage(fp);
25072 fprintf(fp,
25073 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025074 );
25075}
25076
25077static void arg_error(char *fmt, ...)
25078{
25079 va_list args;
25080 va_start(args, fmt);
25081 vfprintf(stderr, fmt, args);
25082 va_end(args);
25083 usage();
25084 exit(1);
25085}
25086
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -080025087static void arg_warning(char *fmt, ...)
25088{
25089 va_list args;
25090
25091 va_start(args, fmt);
25092 vfprintf(stderr, fmt, args);
25093 va_end(args);
25094}
25095
Eric Biedermanb138ac82003-04-22 18:44:01 +000025096int main(int argc, char **argv)
25097{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025098 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025099 struct compiler_state compiler;
25100 struct arch_state arch;
25101 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025102
25103
Eric Biederman90089602004-05-28 14:11:54 +000025104 /* I don't want any surprises */
25105 setlocale(LC_ALL, "C");
25106
Eric Biederman5ade04a2003-10-22 04:03:46 +000025107 init_compiler_state(&compiler);
25108 init_arch_state(&arch);
25109 filename = 0;
25110 all_opts = 0;
25111 while(argc > 1) {
25112 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25113 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025114 argv += 2;
25115 argc -= 2;
25116 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025117 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025118 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025119 result = -1;
25120 if (strcmp(argv[1], "--") == 0) {
25121 result = 0;
25122 all_opts = 1;
25123 }
Eric Biederman90089602004-05-28 14:11:54 +000025124 else if (strncmp(argv[1], "-E", 2) == 0) {
25125 result = compiler_encode_flag(&compiler, argv[1]);
25126 }
25127 else if (strncmp(argv[1], "-O", 2) == 0) {
25128 result = compiler_encode_flag(&compiler, argv[1]);
25129 }
25130 else if (strncmp(argv[1], "-I", 2) == 0) {
25131 result = compiler_encode_flag(&compiler, argv[1]);
25132 }
25133 else if (strncmp(argv[1], "-D", 2) == 0) {
25134 result = compiler_encode_flag(&compiler, argv[1]);
25135 }
25136 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025137 result = compiler_encode_flag(&compiler, argv[1]);
25138 }
25139 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25140 result = compiler_encode_flag(&compiler, argv[1]+2);
25141 }
25142 else if (strncmp(argv[1], "-f", 2) == 0) {
25143 result = compiler_encode_flag(&compiler, argv[1]+2);
25144 }
25145 else if (strncmp(argv[1], "-m", 2) == 0) {
25146 result = arch_encode_flag(&arch, argv[1]+2);
25147 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025148 else if (strncmp(argv[1], "-c", 2) == 0) {
25149 result = 0;
25150 }
25151 else if (strncmp(argv[1], "-S", 2) == 0) {
25152 result = 0;
25153 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025154 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025155 struct filelist *old_head = include_filelist;
25156 include_filelist = malloc(sizeof(struct filelist));
25157 if (!include_filelist) {
25158 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025159 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025160 argv++;
25161 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025162 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025163 include_filelist->next = old_head;
25164 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025165 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025166 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025167 arg_error("Invalid option specified: %s\n",
25168 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025169 }
25170 argv++;
25171 argc--;
25172 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025173 else {
25174 if (filename) {
25175 arg_error("Only one filename may be specified\n");
25176 }
25177 filename = argv[1];
25178 argv++;
25179 argc--;
25180 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025181 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025182 if (!filename) {
25183 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025184 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025185 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025186
25187 return 0;
25188}