blob: 9a7d1a2ff61ec27b57d4dc97feee99881069b0e9 [file] [log] [blame]
Eric Biederman90089602004-05-28 14:11:54 +00001#undef VERSION_MAJOR
2#undef VERSION_MINOR
3#undef RELEASE_DATE
4#undef VERSION
5#define VERSION_MAJOR "0"
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -08006#define VERSION_MINOR "80"
7#define RELEASE_DATE "18 November 2015"
Eric Biederman90089602004-05-28 14:11:54 +00008#define VERSION VERSION_MAJOR "." VERSION_MINOR
9
Eric Biedermanb138ac82003-04-22 18:44:01 +000010#include <stdarg.h>
11#include <errno.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <stdio.h>
20#include <string.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000021#include <limits.h>
Eric Biederman90089602004-05-28 14:11:54 +000022#include <locale.h>
23#include <time.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000024
Eric Biederman90089602004-05-28 14:11:54 +000025#define MAX_CWD_SIZE 4096
Eric Biederman5ade04a2003-10-22 04:03:46 +000026#define MAX_ALLOCATION_PASSES 100
27
Stefan Reinauer14e22772010-04-27 06:56:47 +000028/* NOTE: Before you even start thinking to touch anything
Stefan Reinauer50542a82007-10-24 11:14:14 +000029 * in this code, set DEBUG_ROMCC_WARNINGS to 1 to get an
Stefan Reinauer14e22772010-04-27 06:56:47 +000030 * insight on the original author's thoughts. We introduced
Stefan Reinauer50542a82007-10-24 11:14:14 +000031 * this switch as romcc was about the only thing producing
32 * massive warnings in our code..
33 */
34#define DEBUG_ROMCC_WARNINGS 0
35
Eric Biedermana4aef6d2003-12-05 06:16:19 +000036#define DEBUG_CONSISTENCY 1
Eric Biederman530b5192003-07-01 10:05:30 +000037#define DEBUG_SDP_BLOCKS 0
38#define DEBUG_TRIPLE_COLOR 0
Eric Biedermanb138ac82003-04-22 18:44:01 +000039
Eric Biederman90089602004-05-28 14:11:54 +000040#define DEBUG_DISPLAY_USES 1
41#define DEBUG_DISPLAY_TYPES 1
42#define DEBUG_REPLACE_CLOSURE_TYPE_HIRES 0
43#define DEBUG_DECOMPOSE_PRINT_TUPLES 0
44#define DEBUG_DECOMPOSE_HIRES 0
45#define DEBUG_INITIALIZER 0
46#define DEBUG_UPDATE_CLOSURE_TYPE 0
47#define DEBUG_LOCAL_TRIPLE 0
48#define DEBUG_BASIC_BLOCKS_VERBOSE 0
49#define DEBUG_CPS_RENAME_VARIABLES_HIRES 0
50#define DEBUG_SIMPLIFY_HIRES 0
51#define DEBUG_SHRINKING 0
52#define DEBUG_COALESCE_HITCHES 0
53#define DEBUG_CODE_ELIMINATION 0
54
55#define DEBUG_EXPLICIT_CLOSURES 0
56
Stefan Reinauer50542a82007-10-24 11:14:14 +000057#if DEBUG_ROMCC_WARNINGS
Eric Biederman8d9c1232003-06-17 08:42:17 +000058#warning "FIXME give clear error messages about unused variables"
Eric Biederman530b5192003-07-01 10:05:30 +000059#warning "FIXME properly handle multi dimensional arrays"
Eric Biederman90089602004-05-28 14:11:54 +000060#warning "FIXME handle multiple register sizes"
Stefan Reinauer50542a82007-10-24 11:14:14 +000061#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000062
Eric Biedermanb138ac82003-04-22 18:44:01 +000063/* Control flow graph of a loop without goto.
Stefan Reinauer14e22772010-04-27 06:56:47 +000064 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000065 * AAA
66 * +---/
67 * /
68 * / +--->CCC
69 * | | / \
70 * | | DDD EEE break;
71 * | | \ \
72 * | | FFF \
73 * \| / \ \
74 * |\ GGG HHH | continue;
75 * | \ \ | |
76 * | \ III | /
Stefan Reinauer14e22772010-04-27 06:56:47 +000077 * | \ | / /
78 * | vvv /
79 * +----BBB /
Eric Biedermanb138ac82003-04-22 18:44:01 +000080 * | /
81 * vv
82 * JJJ
83 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000084 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000085 * AAA
86 * +-----+ | +----+
87 * | \ | / |
88 * | BBB +-+ |
89 * | / \ / | |
90 * | CCC JJJ / /
Stefan Reinauer14e22772010-04-27 06:56:47 +000091 * | / \ / /
92 * | DDD EEE / /
Eric Biedermanb138ac82003-04-22 18:44:01 +000093 * | | +-/ /
Stefan Reinauer14e22772010-04-27 06:56:47 +000094 * | FFF /
95 * | / \ /
96 * | GGG HHH /
Eric Biedermanb138ac82003-04-22 18:44:01 +000097 * | | +-/
98 * | III
Stefan Reinauer14e22772010-04-27 06:56:47 +000099 * +--+
Eric Biedermanb138ac82003-04-22 18:44:01 +0000100 *
Stefan Reinauer14e22772010-04-27 06:56:47 +0000101 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000102 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
103 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
104 *
105 *
106 * [] == DFlocal(X) U DF(X)
107 * () == DFup(X)
108 *
109 * Dominator graph of the same nodes.
110 *
111 * AAA AAA: [ ] ()
112 * / \
113 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
114 * |
115 * CCC CCC: [ ] ( BBB, JJJ )
116 * / \
117 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
118 * |
119 * FFF FFF: [ ] ( BBB )
Stefan Reinauer14e22772010-04-27 06:56:47 +0000120 * / \
Eric Biedermanb138ac82003-04-22 18:44:01 +0000121 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
122 * |
123 * III III: [ BBB ] ()
124 *
125 *
126 * BBB and JJJ are definitely the dominance frontier.
127 * Where do I place phi functions and how do I make that decision.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000128 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000129 */
Stefan Reinauerb4d3af82010-02-11 03:21:29 +0000130
131struct filelist {
132 const char *filename;
133 struct filelist *next;
134};
135
136struct filelist *include_filelist = NULL;
137
Stefan Reinauerbed872d2010-04-03 00:03:07 +0000138static void __attribute__((noreturn)) die(char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000139{
140 va_list args;
141
142 va_start(args, fmt);
143 vfprintf(stderr, fmt, args);
144 va_end(args);
145 fflush(stdout);
146 fflush(stderr);
147 exit(1);
148}
149
Eric Biedermanb138ac82003-04-22 18:44:01 +0000150static void *xmalloc(size_t size, const char *name)
151{
152 void *buf;
153 buf = malloc(size);
154 if (!buf) {
155 die("Cannot malloc %ld bytes to hold %s: %s\n",
156 size + 0UL, name, strerror(errno));
157 }
158 return buf;
159}
160
161static void *xcmalloc(size_t size, const char *name)
162{
163 void *buf;
164 buf = xmalloc(size, name);
165 memset(buf, 0, size);
166 return buf;
167}
168
Eric Biederman90089602004-05-28 14:11:54 +0000169static void *xrealloc(void *ptr, size_t size, const char *name)
170{
171 void *buf;
172 buf = realloc(ptr, size);
173 if (!buf) {
174 die("Cannot realloc %ld bytes to hold %s: %s\n",
175 size + 0UL, name, strerror(errno));
176 }
177 return buf;
178}
179
Eric Biedermanb138ac82003-04-22 18:44:01 +0000180static void xfree(const void *ptr)
181{
182 free((void *)ptr);
183}
184
185static char *xstrdup(const char *str)
186{
187 char *new;
188 int len;
189 len = strlen(str);
190 new = xmalloc(len + 1, "xstrdup string");
191 memcpy(new, str, len);
192 new[len] = '\0';
193 return new;
194}
195
196static void xchdir(const char *path)
197{
198 if (chdir(path) != 0) {
Eric Biederman90089602004-05-28 14:11:54 +0000199 die("chdir to `%s' failed: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +0000200 path, strerror(errno));
201 }
202}
203
204static int exists(const char *dirname, const char *filename)
205{
Eric Biederman90089602004-05-28 14:11:54 +0000206 char cwd[MAX_CWD_SIZE];
207 int does_exist;
208
209 if (getcwd(cwd, sizeof(cwd)) == 0) {
210 die("cwd buffer to small");
211 }
212
213 does_exist = 1;
214 if (chdir(dirname) != 0) {
215 does_exist = 0;
216 }
217 if (does_exist && (access(filename, O_RDONLY) < 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000218 if ((errno != EACCES) && (errno != EROFS)) {
219 does_exist = 0;
220 }
221 }
Eric Biederman90089602004-05-28 14:11:54 +0000222 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000223 return does_exist;
224}
225
Patrick Georgi32eeff42014-08-11 09:27:18 +0200226static off_t get_file_size(FILE *f)
227{
228 struct stat s;
229 int fd = fileno(f);
230 if (fd == -1) return -1;
231 if (fstat(fd, &s) == -1) return -1;
232 return s.st_size;
233}
Eric Biedermanb138ac82003-04-22 18:44:01 +0000234
235static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
236{
Eric Biederman90089602004-05-28 14:11:54 +0000237 char cwd[MAX_CWD_SIZE];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000238 char *buf;
239 off_t size, progress;
240 ssize_t result;
Patrick Georgi26774f22009-11-21 19:54:02 +0000241 FILE* file;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000242
Eric Biedermanb138ac82003-04-22 18:44:01 +0000243 if (!filename) {
244 *r_size = 0;
245 return 0;
246 }
Eric Biederman90089602004-05-28 14:11:54 +0000247 if (getcwd(cwd, sizeof(cwd)) == 0) {
248 die("cwd buffer to small");
249 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000250 xchdir(dirname);
Patrick Georgi26774f22009-11-21 19:54:02 +0000251 file = fopen(filename, "rb");
Eric Biederman90089602004-05-28 14:11:54 +0000252 xchdir(cwd);
Patrick Georgi26774f22009-11-21 19:54:02 +0000253 if (file == NULL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000254 die("Cannot open '%s' : %s\n",
255 filename, strerror(errno));
256 }
Patrick Georgi32eeff42014-08-11 09:27:18 +0200257 size = get_file_size(file);
258 if (size == -1) {
259 die("Could not fetch size of '%s': %s\n", filename, strerror(errno));
260 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000261 *r_size = size +1;
262 buf = xmalloc(size +2, filename);
263 buf[size] = '\n'; /* Make certain the file is newline terminated */
264 buf[size+1] = '\0'; /* Null terminate the file for good measure */
265 progress = 0;
266 while(progress < size) {
Patrick Georgi26774f22009-11-21 19:54:02 +0000267 result = fread(buf + progress, 1, size - progress, file);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000268 if (result < 0) {
269 if ((errno == EINTR) || (errno == EAGAIN))
270 continue;
271 die("read on %s of %ld bytes failed: %s\n",
272 filename, (size - progress)+ 0UL, strerror(errno));
273 }
274 progress += result;
275 }
Patrick Georgi26774f22009-11-21 19:54:02 +0000276 fclose(file);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000277 return buf;
278}
279
Eric Biederman83b991a2003-10-11 06:20:25 +0000280/* Types on the destination platform */
Stefan Reinauer50542a82007-10-24 11:14:14 +0000281#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +0000282#warning "FIXME this assumes 32bit x86 is the destination"
Stefan Reinauer50542a82007-10-24 11:14:14 +0000283#endif
Eric Biederman83b991a2003-10-11 06:20:25 +0000284typedef int8_t schar_t;
285typedef uint8_t uchar_t;
286typedef int8_t char_t;
287typedef int16_t short_t;
288typedef uint16_t ushort_t;
289typedef int32_t int_t;
290typedef uint32_t uint_t;
291typedef int32_t long_t;
Patrick Georgia84a99b2009-05-26 14:03:51 +0000292#define ulong_t uint32_t
Eric Biederman83b991a2003-10-11 06:20:25 +0000293
294#define SCHAR_T_MIN (-128)
295#define SCHAR_T_MAX 127
296#define UCHAR_T_MAX 255
297#define CHAR_T_MIN SCHAR_T_MIN
298#define CHAR_T_MAX SCHAR_T_MAX
299#define SHRT_T_MIN (-32768)
300#define SHRT_T_MAX 32767
301#define USHRT_T_MAX 65535
302#define INT_T_MIN (-LONG_T_MAX - 1)
303#define INT_T_MAX 2147483647
304#define UINT_T_MAX 4294967295U
305#define LONG_T_MIN (-LONG_T_MAX - 1)
306#define LONG_T_MAX 2147483647
307#define ULONG_T_MAX 4294967295U
Eric Biedermanb138ac82003-04-22 18:44:01 +0000308
Eric Biederman90089602004-05-28 14:11:54 +0000309#define SIZEOF_I8 8
310#define SIZEOF_I16 16
311#define SIZEOF_I32 32
312#define SIZEOF_I64 64
313
314#define SIZEOF_CHAR 8
315#define SIZEOF_SHORT 16
316#define SIZEOF_INT 32
317#define SIZEOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
318
319
320#define ALIGNOF_CHAR 8
321#define ALIGNOF_SHORT 16
322#define ALIGNOF_INT 32
323#define ALIGNOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
324
325#define REG_SIZEOF_REG 32
326#define REG_SIZEOF_CHAR REG_SIZEOF_REG
327#define REG_SIZEOF_SHORT REG_SIZEOF_REG
328#define REG_SIZEOF_INT REG_SIZEOF_REG
329#define REG_SIZEOF_LONG REG_SIZEOF_REG
330
331#define REG_ALIGNOF_REG REG_SIZEOF_REG
332#define REG_ALIGNOF_CHAR REG_SIZEOF_REG
333#define REG_ALIGNOF_SHORT REG_SIZEOF_REG
334#define REG_ALIGNOF_INT REG_SIZEOF_REG
335#define REG_ALIGNOF_LONG REG_SIZEOF_REG
336
337/* Additional definitions for clarity.
338 * I currently assume a long is the largest native
339 * machine word and that a pointer fits into it.
340 */
341#define SIZEOF_WORD SIZEOF_LONG
342#define SIZEOF_POINTER SIZEOF_LONG
343#define ALIGNOF_WORD ALIGNOF_LONG
344#define ALIGNOF_POINTER ALIGNOF_LONG
345#define REG_SIZEOF_POINTER REG_SIZEOF_LONG
346#define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG
347
Eric Biedermanb138ac82003-04-22 18:44:01 +0000348struct file_state {
349 struct file_state *prev;
350 const char *basename;
351 char *dirname;
Eric Biedermancb364952004-11-15 10:46:44 +0000352 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000353 off_t size;
Eric Biederman90089602004-05-28 14:11:54 +0000354 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000355 int line;
Eric Biederman90089602004-05-28 14:11:54 +0000356 const char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000357 int report_line;
358 const char *report_name;
359 const char *report_dir;
Eric Biedermancb364952004-11-15 10:46:44 +0000360 int macro : 1;
361 int trigraphs : 1;
362 int join_lines : 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000363};
364struct hash_entry;
365struct token {
366 int tok;
367 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +0000368 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000369 int str_len;
370 union {
371 ulong_t integer;
372 const char *str;
Eric Biederman90089602004-05-28 14:11:54 +0000373 int notmacro;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000374 } val;
375};
376
377/* I have two classes of types:
378 * Operational types.
379 * Logical types. (The type the C standard says the operation is of)
380 *
381 * The operational types are:
382 * chars
383 * shorts
384 * ints
385 * longs
386 *
387 * floats
388 * doubles
389 * long doubles
390 *
391 * pointer
392 */
393
394
395/* Machine model.
396 * No memory is useable by the compiler.
397 * There is no floating point support.
398 * All operations take place in general purpose registers.
399 * There is one type of general purpose register.
400 * Unsigned longs are stored in that general purpose register.
401 */
402
403/* Operations on general purpose registers.
404 */
405
Eric Biederman530b5192003-07-01 10:05:30 +0000406#define OP_SDIVT 0
407#define OP_UDIVT 1
408#define OP_SMUL 2
409#define OP_UMUL 3
410#define OP_SDIV 4
411#define OP_UDIV 5
412#define OP_SMOD 6
413#define OP_UMOD 7
414#define OP_ADD 8
415#define OP_SUB 9
416#define OP_SL 10
417#define OP_USR 11
Stefan Reinauer14e22772010-04-27 06:56:47 +0000418#define OP_SSR 12
419#define OP_AND 13
Eric Biederman530b5192003-07-01 10:05:30 +0000420#define OP_XOR 14
421#define OP_OR 15
422#define OP_POS 16 /* Dummy positive operator don't use it */
423#define OP_NEG 17
424#define OP_INVERT 18
Stefan Reinauer14e22772010-04-27 06:56:47 +0000425
Eric Biedermanb138ac82003-04-22 18:44:01 +0000426#define OP_EQ 20
427#define OP_NOTEQ 21
428#define OP_SLESS 22
429#define OP_ULESS 23
430#define OP_SMORE 24
431#define OP_UMORE 25
432#define OP_SLESSEQ 26
433#define OP_ULESSEQ 27
434#define OP_SMOREEQ 28
435#define OP_UMOREEQ 29
Stefan Reinauer14e22772010-04-27 06:56:47 +0000436
Eric Biedermanb138ac82003-04-22 18:44:01 +0000437#define OP_LFALSE 30 /* Test if the expression is logically false */
438#define OP_LTRUE 31 /* Test if the expression is logcially true */
439
440#define OP_LOAD 32
441#define OP_STORE 33
Eric Biederman530b5192003-07-01 10:05:30 +0000442/* For OP_STORE ->type holds the type
443 * RHS(0) holds the destination address
444 * RHS(1) holds the value to store.
445 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000446
Eric Biederman90089602004-05-28 14:11:54 +0000447#define OP_UEXTRACT 34
448/* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register
449 * RHS(0) holds the psuedo register to extract from
450 * ->type holds the size of the bitfield.
451 * ->u.bitfield.size holds the size of the bitfield.
452 * ->u.bitfield.offset holds the offset to extract from
453 */
454#define OP_SEXTRACT 35
455/* OP_SEXTRACT extracts a signed bitfield from a pseudo register
456 * RHS(0) holds the psuedo register to extract from
457 * ->type holds the size of the bitfield.
458 * ->u.bitfield.size holds the size of the bitfield.
459 * ->u.bitfield.offset holds the offset to extract from
460 */
461#define OP_DEPOSIT 36
462/* OP_DEPOSIT replaces a bitfield with a new value.
463 * RHS(0) holds the value to replace a bitifield in.
464 * RHS(1) holds the replacement value
465 * ->u.bitfield.size holds the size of the bitfield.
466 * ->u.bitfield.offset holds the deposit into
467 */
468
469#define OP_NOOP 37
Eric Biedermanb138ac82003-04-22 18:44:01 +0000470
471#define OP_MIN_CONST 50
Eric Biederman90089602004-05-28 14:11:54 +0000472#define OP_MAX_CONST 58
Eric Biedermanb138ac82003-04-22 18:44:01 +0000473#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
474#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000475/* For OP_INTCONST ->type holds the type.
476 * ->u.cval holds the constant value.
477 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000478#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000479/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000480 * information. u.blob holds a pointer to the raw binary
481 * data for the constant initializer.
482 */
483#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000484/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000485 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000486 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000487 */
Eric Biederman90089602004-05-28 14:11:54 +0000488#define OP_UNKNOWNVAL 59
489/* For OP_UNKNOWNAL ->type holds the type.
490 * For some reason we don't know what value this type has.
491 * This allows for variables that have don't have values
492 * assigned yet, or variables whose value we simply do not know.
493 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000494
Stefan Reinauer14e22772010-04-27 06:56:47 +0000495#define OP_WRITE 60
Eric Biedermanb138ac82003-04-22 18:44:01 +0000496/* OP_WRITE moves one pseudo register to another.
Eric Biederman90089602004-05-28 14:11:54 +0000497 * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
498 * RHS(0) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000499 */
500
501#define OP_READ 61
502/* OP_READ reads the value of a variable and makes
503 * it available for the pseudo operation.
504 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000505 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000506 */
507#define OP_COPY 62
Eric Biederman90089602004-05-28 14:11:54 +0000508/* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
Eric Biederman0babc1c2003-05-09 02:39:00 +0000509 */
Eric Biederman90089602004-05-28 14:11:54 +0000510#define OP_CONVERT 63
511/* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0).
512 * And then the type is converted appropriately.
513 */
514#define OP_PIECE 64
Eric Biederman0babc1c2003-05-09 02:39:00 +0000515/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000516 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000517 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000518 */
Eric Biederman90089602004-05-28 14:11:54 +0000519#define OP_ASM 65
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000520/* OP_ASM holds a sequence of assembly instructions, the result
521 * of a C asm directive.
522 * RHS(x) holds input value x to the assembly sequence.
523 * LHS(x) holds the output value x from the assembly sequence.
524 * u.blob holds the string of assembly instructions.
525 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000526
Eric Biederman90089602004-05-28 14:11:54 +0000527#define OP_DEREF 66
Eric Biedermanb138ac82003-04-22 18:44:01 +0000528/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000529 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000530 * OP_DEREF serves as a place holder to indicate all necessary
531 * checks have been done to indicate a value is an lvalue.
532 */
Eric Biederman90089602004-05-28 14:11:54 +0000533#define OP_DOT 67
Eric Biederman0babc1c2003-05-09 02:39:00 +0000534/* OP_DOT references a submember of a structure lvalue.
Eric Biederman90089602004-05-28 14:11:54 +0000535 * MISC(0) holds the lvalue.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000536 * ->u.field holds the name of the field we want.
537 *
Eric Biederman90089602004-05-28 14:11:54 +0000538 * Not seen after structures are flattened.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000539 */
Eric Biederman90089602004-05-28 14:11:54 +0000540#define OP_INDEX 68
541/* OP_INDEX references a submember of a tuple or array lvalue.
542 * MISC(0) holds the lvalue.
543 * ->u.cval holds the index into the lvalue.
544 *
545 * Not seen after structures are flattened.
546 */
547#define OP_VAL 69
Eric Biedermanb138ac82003-04-22 18:44:01 +0000548/* OP_VAL returns the value of a subexpression of the current expression.
549 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000550 * RHS(0) holds the expression.
551 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000552 * value of the expression.
553 *
554 * Not seen outside of expressions.
555 */
Eric Biederman90089602004-05-28 14:11:54 +0000556
557#define OP_TUPLE 70
558/* OP_TUPLE is an array of triples that are either variable
559 * or values for a structure or an array. It is used as
560 * a place holder when flattening compound types.
561 * The value represented by an OP_TUPLE is held in N registers.
562 * LHS(0..N-1) refer to those registers.
563 * ->use is a list of statements that use the value.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000564 *
Eric Biederman90089602004-05-28 14:11:54 +0000565 * Although OP_TUPLE always has register sized pieces they are not
566 * used until structures are flattened/decomposed into their register
Stefan Reinauer14e22772010-04-27 06:56:47 +0000567 * components.
Eric Biederman90089602004-05-28 14:11:54 +0000568 * ???? registers ????
Eric Biedermanb138ac82003-04-22 18:44:01 +0000569 */
570
Eric Biederman90089602004-05-28 14:11:54 +0000571#define OP_BITREF 71
572/* OP_BITREF describes a bitfield as an lvalue.
573 * RHS(0) holds the register value.
574 * ->type holds the type of the bitfield.
575 * ->u.bitfield.size holds the size of the bitfield.
576 * ->u.bitfield.offset holds the offset of the bitfield in the register
577 */
578
579
580#define OP_FCALL 72
Stefan Reinauer14e22772010-04-27 06:56:47 +0000581/* OP_FCALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000582 * MISC(0) holds a pointer to the OP_LIST of a function
583 * RHS(x) holds argument x of a function
Stefan Reinauer14e22772010-04-27 06:56:47 +0000584 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000585 * Currently not seen outside of expressions.
586 */
Eric Biederman90089602004-05-28 14:11:54 +0000587#define OP_PROG 73
588/* OP_PROG is an expression that holds a list of statements, or
589 * expressions. The final expression is the value of the expression.
590 * RHS(0) holds the start of the list.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000591 */
592
593/* statements */
594#define OP_LIST 80
Eric Biederman5ade04a2003-10-22 04:03:46 +0000595/* OP_LIST Holds a list of statements that compose a function, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000596 * RHS(0) holds the list of statements.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000597 * A list of all functions is maintained.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000598 */
599
Eric Biederman5ade04a2003-10-22 04:03:46 +0000600#define OP_BRANCH 81 /* an unconditional branch */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000601/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000602 * TARG(0) holds the branch target.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000603 * ->next holds where to branch to if the branch is not taken.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000604 * The branch target can only be a label
Eric Biedermanb138ac82003-04-22 18:44:01 +0000605 */
606
Eric Biederman5ade04a2003-10-22 04:03:46 +0000607#define OP_CBRANCH 82 /* a conditional branch */
608/* For conditional branch instructions
609 * RHS(0) holds the branch condition.
Eric Biederman41203d92004-11-08 09:31:09 +0000610 * TARG(0) holds the branch target.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000611 * ->next holds where to branch to if the branch is not taken.
612 * The branch target can only be a label
613 */
614
615#define OP_CALL 83 /* an uncontional branch that will return */
616/* For call instructions
617 * MISC(0) holds the OP_RET that returns from the branch
618 * TARG(0) holds the branch target.
619 * ->next holds where to branch to if the branch is not taken.
620 * The branch target can only be a label
621 */
622
623#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
624/* For call instructions
625 * RHS(0) holds the variable with the return address
626 * The branch target can only be a label
627 */
628
629#define OP_LABEL 86
Eric Biedermanb138ac82003-04-22 18:44:01 +0000630/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000631 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000632 */
633
Stefan Reinauer14e22772010-04-27 06:56:47 +0000634#define OP_ADECL 87
Eric Biederman5ade04a2003-10-22 04:03:46 +0000635/* OP_ADECL is a triple that establishes an lvalue for assignments.
Eric Biederman90089602004-05-28 14:11:54 +0000636 * A variable takes N registers to contain.
637 * LHS(0..N-1) refer to an OP_PIECE triple that represents
638 * the Xth register that the variable is stored in.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000639 * ->use is a list of statements that use the variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000640 *
Eric Biederman90089602004-05-28 14:11:54 +0000641 * Although OP_ADECL always has register sized pieces they are not
642 * used until structures are flattened/decomposed into their register
Stefan Reinauer14e22772010-04-27 06:56:47 +0000643 * components.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000644 */
645
Eric Biederman5ade04a2003-10-22 04:03:46 +0000646#define OP_SDECL 88
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000647/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000648 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000649 * ->use is a list of statements that use the variable.
650 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000651 */
652
653
Eric Biederman5ade04a2003-10-22 04:03:46 +0000654#define OP_PHI 89
Stefan Reinauer14e22772010-04-27 06:56:47 +0000655/* OP_PHI is a triple used in SSA form code.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000656 * It is used when multiple code paths merge and a variable needs
657 * a single assignment from any of those code paths.
658 * The operation is a cross between OP_DECL and OP_WRITE, which
Eric Biederman5ade04a2003-10-22 04:03:46 +0000659 * is what OP_PHI is generated from.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000660 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000661 * RHS(x) points to the value from code path x
662 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000663 * in which OP_PHI resides. The elements of the array point to point
664 * to the variables OP_PHI is derived from.
665 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000666 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000667 */
668
Eric Biederman90089602004-05-28 14:11:54 +0000669#if 0
670/* continuation helpers
671 */
672#define OP_CPS_BRANCH 90 /* an unconditional branch */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000673/* OP_CPS_BRANCH calls a continuation
Eric Biederman90089602004-05-28 14:11:54 +0000674 * RHS(x) holds argument x of the function
675 * TARG(0) holds OP_CPS_START target
676 */
677#define OP_CPS_CBRANCH 91 /* a conditional branch */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000678/* OP_CPS_CBRANCH conditionally calls one of two continuations
Eric Biederman90089602004-05-28 14:11:54 +0000679 * RHS(0) holds the branch condition
680 * RHS(x + 1) holds argument x of the function
681 * TARG(0) holds the OP_CPS_START to jump to when true
682 * ->next holds the OP_CPS_START to jump to when false
683 */
684#define OP_CPS_CALL 92 /* an uncontional branch that will return */
685/* For OP_CPS_CALL instructions
686 * RHS(x) holds argument x of the function
687 * MISC(0) holds the OP_CPS_RET that returns from the branch
688 * TARG(0) holds the branch target.
689 * ->next holds where the OP_CPS_RET will return to.
690 */
691#define OP_CPS_RET 93
Stefan Reinauer14e22772010-04-27 06:56:47 +0000692/* OP_CPS_RET conditionally calls one of two continuations
Eric Biederman90089602004-05-28 14:11:54 +0000693 * RHS(0) holds the variable with the return function address
694 * RHS(x + 1) holds argument x of the function
695 * The branch target may be any OP_CPS_START
696 */
697#define OP_CPS_END 94
698/* OP_CPS_END is the triple at the end of the program.
699 * For most practical purposes it is a branch.
700 */
701#define OP_CPS_START 95
702/* OP_CPS_START is a triple at the start of a continuation
703 * The arguments variables takes N registers to contain.
704 * LHS(0..N-1) refer to an OP_PIECE triple that represents
705 * the Xth register that the arguments are stored in.
706 */
707#endif
708
Eric Biedermanb138ac82003-04-22 18:44:01 +0000709/* Architecture specific instructions */
710#define OP_CMP 100
711#define OP_TEST 101
712#define OP_SET_EQ 102
713#define OP_SET_NOTEQ 103
714#define OP_SET_SLESS 104
715#define OP_SET_ULESS 105
716#define OP_SET_SMORE 106
717#define OP_SET_UMORE 107
718#define OP_SET_SLESSEQ 108
719#define OP_SET_ULESSEQ 109
720#define OP_SET_SMOREEQ 110
721#define OP_SET_UMOREEQ 111
722
723#define OP_JMP 112
724#define OP_JMP_EQ 113
725#define OP_JMP_NOTEQ 114
726#define OP_JMP_SLESS 115
727#define OP_JMP_ULESS 116
728#define OP_JMP_SMORE 117
729#define OP_JMP_UMORE 118
730#define OP_JMP_SLESSEQ 119
731#define OP_JMP_ULESSEQ 120
732#define OP_JMP_SMOREEQ 121
733#define OP_JMP_UMOREEQ 122
734
735/* Builtin operators that it is just simpler to use the compiler for */
736#define OP_INB 130
737#define OP_INW 131
738#define OP_INL 132
739#define OP_OUTB 133
740#define OP_OUTW 134
741#define OP_OUTL 135
742#define OP_BSF 136
743#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000744#define OP_RDMSR 138
745#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000746#define OP_HLT 140
747
Eric Biederman0babc1c2003-05-09 02:39:00 +0000748struct op_info {
749 const char *name;
750 unsigned flags;
Eric Biederman90089602004-05-28 14:11:54 +0000751#define PURE 0x001 /* Triple has no side effects */
752#define IMPURE 0x002 /* Triple has side effects */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000753#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
Eric Biederman90089602004-05-28 14:11:54 +0000754#define DEF 0x004 /* Triple is a variable definition */
755#define BLOCK 0x008 /* Triple stores the current block */
756#define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */
757#define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 )
758#define UBRANCH 0x020 /* Triple is an unconditional branch instruction */
759#define CBRANCH 0x040 /* Triple is a conditional branch instruction */
760#define RETBRANCH 0x060 /* Triple is a return instruction */
761#define CALLBRANCH 0x080 /* Triple is a call instruction */
762#define ENDBRANCH 0x0a0 /* Triple is an end instruction */
763#define PART 0x100 /* Triple is really part of another triple */
764#define BITFIELD 0x200 /* Triple manipulates a bitfield */
765 signed char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000766};
767
Eric Biederman0babc1c2003-05-09 02:39:00 +0000768#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
769 .name = (NAME), \
770 .flags = (FLAGS), \
771 .lhs = (LHS), \
772 .rhs = (RHS), \
773 .misc = (MISC), \
774 .targ = (TARG), \
775 }
776static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000777[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
778[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000779[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
780[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
781[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
782[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
783[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
784[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
785[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
786[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
787[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
788[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
789[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
790[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
791[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
792[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
793[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
794[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
795[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000796
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000797[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
798[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
799[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
800[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
801[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
802[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
803[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
804[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
805[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
806[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
807[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
808[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000809
Eric Biederman90089602004-05-28 14:11:54 +0000810[OP_LOAD ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "load"),
811[OP_STORE ] = OP( 0, 2, 0, 0, PURE | BLOCK , "store"),
812
813[OP_UEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "uextract"),
814[OP_SEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "sextract"),
815[OP_DEPOSIT ] = OP( 0, 2, 0, 0, PURE | DEF | BITFIELD, "deposit"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000816
Eric Biederman83b991a2003-10-11 06:20:25 +0000817[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000818
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000819[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000820[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000821[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman90089602004-05-28 14:11:54 +0000822[OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000823
Stefan Reinauer50542a82007-10-24 11:14:14 +0000824#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +0000825#warning "FIXME is it correct for OP_WRITE to be a def? I currently use it as one..."
Stefan Reinauer50542a82007-10-24 11:14:14 +0000826#endif
Eric Biederman90089602004-05-28 14:11:54 +0000827[OP_WRITE ] = OP( 0, 1, 1, 0, PURE | DEF | BLOCK, "write"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000828[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
829[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
Eric Biederman90089602004-05-28 14:11:54 +0000830[OP_CONVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "convert"),
831[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"),
832[OP_ASM ] = OP(-1, -1, 0, 0, PURE, "asm"),
Stefan Reinauer14e22772010-04-27 06:56:47 +0000833[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
Eric Biederman90089602004-05-28 14:11:54 +0000834[OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"),
835[OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000836
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000837[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
Eric Biederman90089602004-05-28 14:11:54 +0000838[OP_TUPLE ] = OP(-1, 0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"),
839[OP_BITREF ] = OP( 0, 1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000840/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman90089602004-05-28 14:11:54 +0000841[OP_FCALL ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"),
842[OP_PROG ] = OP( 0, 1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"),
843/* The sizes of OP_FCALL depends upon context */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000844
Eric Biederman83b991a2003-10-11 06:20:25 +0000845[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
Eric Biederman90089602004-05-28 14:11:54 +0000846[OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "branch"),
847[OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"),
848[OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"),
849[OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000850[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
851[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
852[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000853/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000854[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000855
Eric Biederman90089602004-05-28 14:11:54 +0000856#if 0
857[OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH, "cps_branch"),
858[OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH, "cps_cbranch"),
859[OP_CPS_CALL ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH, "cps_call"),
860[OP_CPS_RET ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH, "cps_ret"),
861[OP_CPS_END ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"),
862[OP_CPS_START ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "cps_start"),
863#endif
864
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000865[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
866[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
867[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
868[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
869[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
870[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
871[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
872[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
873[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
874[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
875[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
876[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
Eric Biederman90089602004-05-28 14:11:54 +0000877[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"),
878[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"),
879[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"),
880[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"),
881[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"),
882[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"),
883[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"),
884[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"),
885[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"),
886[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"),
887[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000888
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000889[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
890[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
891[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
892[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
893[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
894[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
895[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
896[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
897[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
898[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
899[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000900};
901#undef OP
902#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000903
Stefan Reinauer14e22772010-04-27 06:56:47 +0000904static const char *tops(int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000905{
906 static const char unknown[] = "unknown op";
907 if (index < 0) {
908 return unknown;
909 }
Patrick Georgi0dde01c2013-11-11 15:57:09 +0100910 if (index >= OP_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000911 return unknown;
912 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000913 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000914}
915
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000916struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000917struct triple;
918struct block;
919struct triple_set {
920 struct triple_set *next;
921 struct triple *member;
922};
923
Eric Biederman90089602004-05-28 14:11:54 +0000924#define MAX_LHS 63
925#define MAX_RHS 127
Eric Biederman678d8162003-07-03 03:59:38 +0000926#define MAX_MISC 3
Eric Biederman90089602004-05-28 14:11:54 +0000927#define MAX_TARG 1
Eric Biederman0babc1c2003-05-09 02:39:00 +0000928
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000929struct occurance {
930 int count;
931 const char *filename;
932 const char *function;
933 int line;
934 int col;
935 struct occurance *parent;
936};
Eric Biederman90089602004-05-28 14:11:54 +0000937struct bitfield {
938 ulong_t size : 8;
939 ulong_t offset : 24;
940};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000941struct triple {
942 struct triple *next, *prev;
943 struct triple_set *use;
944 struct type *type;
Eric Biederman90089602004-05-28 14:11:54 +0000945 unsigned int op : 8;
946 unsigned int template_id : 7;
947 unsigned int lhs : 6;
948 unsigned int rhs : 7;
949 unsigned int misc : 2;
950 unsigned int targ : 1;
951#define TRIPLE_SIZE(TRIPLE) \
952 ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ)
953#define TRIPLE_LHS_OFF(PTR) (0)
954#define TRIPLE_RHS_OFF(PTR) (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs)
955#define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs)
956#define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc)
957#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)])
958#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)])
959#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)])
960#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000961 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000962#define TRIPLE_FLAG_FLATTENED (1 << 31)
963#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
964#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biederman83b991a2003-10-11 06:20:25 +0000965#define TRIPLE_FLAG_VOLATILE (1 << 28)
Eric Biederman90089602004-05-28 14:11:54 +0000966#define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */
967#define TRIPLE_FLAG_LOCAL (1 << 26)
968
969#define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000970 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000971 union {
972 ulong_t cval;
Eric Biederman90089602004-05-28 14:11:54 +0000973 struct bitfield bitfield;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000974 struct block *block;
975 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000976 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000977 struct asm_info *ainfo;
Eric Biederman90089602004-05-28 14:11:54 +0000978 struct triple *func;
979 struct symbol *symbol;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000980 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000981 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000982};
983
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000984struct reg_info {
985 unsigned reg;
986 unsigned regcm;
987};
988struct ins_template {
989 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
990};
991
992struct asm_info {
993 struct ins_template tmpl;
994 char *str;
995};
996
Eric Biedermanb138ac82003-04-22 18:44:01 +0000997struct block_set {
998 struct block_set *next;
999 struct block *member;
1000};
1001struct block {
1002 struct block *work_next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001003 struct triple *first, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001004 int edge_count;
1005 struct block_set *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001006 int users;
1007 struct block_set *use;
1008 struct block_set *idominates;
1009 struct block_set *domfrontier;
1010 struct block *idom;
1011 struct block_set *ipdominates;
1012 struct block_set *ipdomfrontier;
1013 struct block *ipdom;
1014 int vertex;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001015
Eric Biedermanb138ac82003-04-22 18:44:01 +00001016};
1017
1018struct symbol {
1019 struct symbol *next;
1020 struct hash_entry *ident;
1021 struct triple *def;
1022 struct type *type;
1023 int scope_depth;
1024};
1025
Eric Biederman90089602004-05-28 14:11:54 +00001026struct macro_arg {
1027 struct macro_arg *next;
1028 struct hash_entry *ident;
1029};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001030struct macro {
1031 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00001032 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001033 int buf_len;
Eric Biederman90089602004-05-28 14:11:54 +00001034 struct macro_arg *args;
1035 int argc;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001036};
1037
1038struct hash_entry {
1039 struct hash_entry *next;
1040 const char *name;
1041 int name_len;
1042 int tok;
1043 struct macro *sym_define;
1044 struct symbol *sym_label;
Eric Biederman83b991a2003-10-11 06:20:25 +00001045 struct symbol *sym_tag;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001046 struct symbol *sym_ident;
1047};
1048
1049#define HASH_TABLE_SIZE 2048
1050
Eric Biederman5ade04a2003-10-22 04:03:46 +00001051struct compiler_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +00001052 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001053 const char *ofilename;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001054 unsigned long flags;
1055 unsigned long debug;
1056 unsigned long max_allocation_passes;
Eric Biederman90089602004-05-28 14:11:54 +00001057
1058 size_t include_path_count;
1059 const char **include_paths;
1060
1061 size_t define_count;
1062 const char **defines;
1063
1064 size_t undef_count;
1065 const char **undefs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001066};
1067struct arch_state {
1068 unsigned long features;
1069};
Eric Biederman90089602004-05-28 14:11:54 +00001070struct basic_blocks {
1071 struct triple *func;
1072 struct triple *first;
1073 struct block *first_block, *last_block;
1074 int last_vertex;
1075};
Eric Biedermancb364952004-11-15 10:46:44 +00001076#define MAX_PP_IF_DEPTH 63
Eric Biederman5ade04a2003-10-22 04:03:46 +00001077struct compile_state {
1078 struct compiler_state *compiler;
1079 struct arch_state *arch;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001080 FILE *output;
Eric Biederman90089602004-05-28 14:11:54 +00001081 FILE *errout;
1082 FILE *dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001083 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001084 struct occurance *last_occurance;
1085 const char *function;
Eric Biederman41203d92004-11-08 09:31:09 +00001086 int token_base;
1087 struct token token[6];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001088 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +00001089 struct hash_entry *i_switch;
1090 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001091 struct hash_entry *i_continue;
1092 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +00001093 struct hash_entry *i_default;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001094 struct hash_entry *i_return;
Stefan Reinauerc89c4d42010-02-28 18:37:38 +00001095 struct hash_entry *i_noreturn;
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -07001096 struct hash_entry *i_unused;
Stefan Reinauerec664bc2013-05-09 14:06:04 -07001097 struct hash_entry *i_packed;
Eric Biederman90089602004-05-28 14:11:54 +00001098 /* Additional hash entries for predefined macros */
1099 struct hash_entry *i_defined;
1100 struct hash_entry *i___VA_ARGS__;
1101 struct hash_entry *i___FILE__;
1102 struct hash_entry *i___LINE__;
1103 /* Additional hash entries for predefined identifiers */
1104 struct hash_entry *i___func__;
1105 /* Additional hash entries for attributes */
1106 struct hash_entry *i_noinline;
1107 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001108 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001109 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001110 int if_depth;
1111 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001112 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001113 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001114 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001115 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001116 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001117 struct basic_blocks bb;
1118 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001119};
1120
Eric Biederman0babc1c2003-05-09 02:39:00 +00001121/* visibility global/local */
1122/* static/auto duration */
1123/* typedef, register, inline */
1124#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001125#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001126/* Visibility */
1127#define STOR_GLOBAL 0x0001
1128/* Duration */
1129#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001130/* Definition locality */
1131#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001132/* Storage specifiers */
1133#define STOR_AUTO 0x0000
1134#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001135#define STOR_LOCAL 0x0003
1136#define STOR_EXTERN 0x0007
1137#define STOR_INLINE 0x0008
1138#define STOR_REGISTER 0x0010
1139#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001140
Eric Biederman5ade04a2003-10-22 04:03:46 +00001141#define QUAL_SHIFT 5
1142#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001143#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001144#define QUAL_CONST 0x0020
1145#define QUAL_VOLATILE 0x0040
1146#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001147
1148#define TYPE_SHIFT 8
1149#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001150#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1151#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001152#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1153#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001154#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1155#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001156#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1157#define TYPE_DEFAULT 0x0000
1158#define TYPE_VOID 0x0100
1159#define TYPE_CHAR 0x0200
1160#define TYPE_UCHAR 0x0300
1161#define TYPE_SHORT 0x0400
1162#define TYPE_USHORT 0x0500
1163#define TYPE_INT 0x0600
1164#define TYPE_UINT 0x0700
1165#define TYPE_LONG 0x0800
1166#define TYPE_ULONG 0x0900
1167#define TYPE_LLONG 0x0a00 /* long long */
1168#define TYPE_ULLONG 0x0b00
1169#define TYPE_FLOAT 0x0c00
1170#define TYPE_DOUBLE 0x0d00
1171#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001172
1173/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1174#define TYPE_ENUM 0x1600
1175#define TYPE_LIST 0x1700
1176/* TYPE_LIST is a basic building block when defining enumerations
1177 * type->field_ident holds the name of this enumeration entry.
1178 * type->right holds the entry in the list.
1179 */
1180
Eric Biederman0babc1c2003-05-09 02:39:00 +00001181#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001182/* For TYPE_STRUCT
1183 * type->left holds the link list of TYPE_PRODUCT entries that
1184 * make up the structure.
1185 * type->elements hold the length of the linked list
1186 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001187#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001188/* For TYPE_UNION
1189 * type->left holds the link list of TYPE_OVERLAP entries that
1190 * make up the union.
1191 * type->elements hold the length of the linked list
1192 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001193#define TYPE_POINTER 0x1200
Eric Biederman0babc1c2003-05-09 02:39:00 +00001194/* For TYPE_POINTER:
1195 * type->left holds the type pointed to.
1196 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001197#define TYPE_FUNCTION 0x1300
Eric Biederman0babc1c2003-05-09 02:39:00 +00001198/* For TYPE_FUNCTION:
1199 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001200 * type->right holds the type of the arguments
1201 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001202 */
1203#define TYPE_PRODUCT 0x1400
1204/* TYPE_PRODUCT is a basic building block when defining structures
1205 * type->left holds the type that appears first in memory.
1206 * type->right holds the type that appears next in memory.
1207 */
1208#define TYPE_OVERLAP 0x1500
1209/* TYPE_OVERLAP is a basic building block when defining unions
1210 * type->left and type->right holds to types that overlap
1211 * each other in memory.
1212 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001213#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001214/* TYPE_ARRAY is a basic building block when definitng arrays.
1215 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001216 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001217 */
Eric Biederman90089602004-05-28 14:11:54 +00001218#define TYPE_TUPLE 0x1900
Stefan Reinauer14e22772010-04-27 06:56:47 +00001219/* TYPE_TUPLE is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001220 * positionally reference type conglomerations. (i.e. closures)
1221 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1222 * except it has no field names.
1223 * type->left holds the liked list of TYPE_PRODUCT entries that
1224 * make up the closure type.
1225 * type->elements hold the number of elements in the closure.
1226 */
1227#define TYPE_JOIN 0x1a00
Stefan Reinauer14e22772010-04-27 06:56:47 +00001228/* TYPE_JOIN is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001229 * positionally reference type conglomerations. (i.e. closures)
1230 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1231 * except it has no field names.
1232 * type->left holds the liked list of TYPE_OVERLAP entries that
1233 * make up the closure type.
1234 * type->elements hold the number of elements in the closure.
1235 */
1236#define TYPE_BITFIELD 0x1b00
1237/* TYPE_BITFIED is the type of a bitfield.
1238 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1239 * type->elements holds the number of bits in the bitfield.
1240 */
1241#define TYPE_UNKNOWN 0x1c00
1242/* TYPE_UNKNOWN is the type of an unknown value.
1243 * Used on unknown consts and other places where I don't know the type.
1244 */
1245
1246#define ATTRIB_SHIFT 16
1247#define ATTRIB_MASK 0xffff0000
1248#define ATTRIB_NOINLINE 0x00010000
1249#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001250
Eric Biederman83b991a2003-10-11 06:20:25 +00001251#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001252
1253struct type {
1254 unsigned int type;
1255 struct type *left, *right;
1256 ulong_t elements;
1257 struct hash_entry *field_ident;
1258 struct hash_entry *type_ident;
1259};
1260
Eric Biederman530b5192003-07-01 10:05:30 +00001261#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001262#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001263#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001264#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001265#define MAX_REGISTERS 75
1266#define REGISTER_BITS 7
1267#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001268#define REG_ERROR 0
1269#define REG_UNSET 1
1270#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001271#define REG_VIRT0 (MAX_REGISTERS + 0)
1272#define REG_VIRT1 (MAX_REGISTERS + 1)
1273#define REG_VIRT2 (MAX_REGISTERS + 2)
1274#define REG_VIRT3 (MAX_REGISTERS + 3)
1275#define REG_VIRT4 (MAX_REGISTERS + 4)
1276#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001277#define REG_VIRT6 (MAX_REGISTERS + 6)
1278#define REG_VIRT7 (MAX_REGISTERS + 7)
1279#define REG_VIRT8 (MAX_REGISTERS + 8)
1280#define REG_VIRT9 (MAX_REGISTERS + 9)
1281
1282#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1283#error "MAX_VIRT_REGISTERS to small"
1284#endif
Eric Biederman90089602004-05-28 14:11:54 +00001285#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001286#error "Too many id bits used"
1287#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001288
1289/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001290#define REG_SHIFT 0
1291#define REGC_SHIFT REGISTER_BITS
1292#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001293#define REG_MASK (MAX_VIRT_REGISTERS -1)
1294#define ID_REG(ID) ((ID) & REG_MASK)
1295#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001296#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1297#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1298#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1299 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001300
Eric Biederman90089602004-05-28 14:11:54 +00001301#define ARCH_INPUT_REGS 4
1302#define ARCH_OUTPUT_REGS 4
1303
1304static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1305static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001306static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001307static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001308static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001309static void arch_reg_equivs(
1310 struct compile_state *state, unsigned *equiv, int reg);
1311static int arch_select_free_register(
1312 struct compile_state *state, char *used, int classes);
1313static unsigned arch_regc_size(struct compile_state *state, int class);
1314static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1315static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1316static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001317static struct reg_info arch_reg_constraint(
1318 struct compile_state *state, struct type *type, const char *constraint);
1319static struct reg_info arch_reg_clobber(
1320 struct compile_state *state, const char *clobber);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001321static struct reg_info arch_reg_lhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001322 struct triple *ins, int index);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001323static struct reg_info arch_reg_rhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001324 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001325static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001326static struct triple *transform_to_arch_instruction(
1327 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001328static struct triple *flatten(
1329 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001330static void print_dominators(struct compile_state *state,
1331 FILE *fp, struct basic_blocks *bb);
1332static void print_dominance_frontiers(struct compile_state *state,
1333 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001334
1335
Eric Biedermanb138ac82003-04-22 18:44:01 +00001336
Eric Biederman5ade04a2003-10-22 04:03:46 +00001337#define DEBUG_ABORT_ON_ERROR 0x00000001
1338#define DEBUG_BASIC_BLOCKS 0x00000002
1339#define DEBUG_FDOMINATORS 0x00000004
1340#define DEBUG_RDOMINATORS 0x00000008
1341#define DEBUG_TRIPLES 0x00000010
1342#define DEBUG_INTERFERENCE 0x00000020
1343#define DEBUG_SCC_TRANSFORM 0x00000040
1344#define DEBUG_SCC_TRANSFORM2 0x00000080
1345#define DEBUG_REBUILD_SSA_FORM 0x00000100
1346#define DEBUG_INLINE 0x00000200
1347#define DEBUG_RANGE_CONFLICTS 0x00000400
1348#define DEBUG_RANGE_CONFLICTS2 0x00000800
1349#define DEBUG_COLOR_GRAPH 0x00001000
1350#define DEBUG_COLOR_GRAPH2 0x00002000
1351#define DEBUG_COALESCING 0x00004000
1352#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001353#define DEBUG_VERIFICATION 0x00010000
1354#define DEBUG_CALLS 0x00020000
1355#define DEBUG_CALLS2 0x00040000
1356#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001357
1358#define DEBUG_DEFAULT ( \
1359 DEBUG_ABORT_ON_ERROR | \
1360 DEBUG_BASIC_BLOCKS | \
1361 DEBUG_FDOMINATORS | \
1362 DEBUG_RDOMINATORS | \
1363 DEBUG_TRIPLES | \
1364 0 )
1365
Eric Biederman90089602004-05-28 14:11:54 +00001366#define DEBUG_ALL ( \
1367 DEBUG_ABORT_ON_ERROR | \
1368 DEBUG_BASIC_BLOCKS | \
1369 DEBUG_FDOMINATORS | \
1370 DEBUG_RDOMINATORS | \
1371 DEBUG_TRIPLES | \
1372 DEBUG_INTERFERENCE | \
1373 DEBUG_SCC_TRANSFORM | \
1374 DEBUG_SCC_TRANSFORM2 | \
1375 DEBUG_REBUILD_SSA_FORM | \
1376 DEBUG_INLINE | \
1377 DEBUG_RANGE_CONFLICTS | \
1378 DEBUG_RANGE_CONFLICTS2 | \
1379 DEBUG_COLOR_GRAPH | \
1380 DEBUG_COLOR_GRAPH2 | \
1381 DEBUG_COALESCING | \
1382 DEBUG_COALESCING2 | \
1383 DEBUG_VERIFICATION | \
1384 DEBUG_CALLS | \
1385 DEBUG_CALLS2 | \
1386 DEBUG_TOKENS | \
1387 0 )
1388
1389#define COMPILER_INLINE_MASK 0x00000007
1390#define COMPILER_INLINE_ALWAYS 0x00000000
1391#define COMPILER_INLINE_NEVER 0x00000001
1392#define COMPILER_INLINE_DEFAULTON 0x00000002
1393#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1394#define COMPILER_INLINE_NOPENALTY 0x00000004
1395#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1396#define COMPILER_SIMPLIFY 0x00000010
1397#define COMPILER_SCC_TRANSFORM 0x00000020
1398#define COMPILER_SIMPLIFY_OP 0x00000040
1399#define COMPILER_SIMPLIFY_PHI 0x00000080
1400#define COMPILER_SIMPLIFY_LABEL 0x00000100
1401#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1402#define COMPILER_SIMPLIFY_COPY 0x00000400
1403#define COMPILER_SIMPLIFY_ARITH 0x00000800
1404#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1405#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1406#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1407#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1408
Eric Biedermancb364952004-11-15 10:46:44 +00001409#define COMPILER_TRIGRAPHS 0x40000000
1410#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001411
1412#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001413 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001414 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001415 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001416 COMPILER_SIMPLIFY_OP | \
1417 COMPILER_SIMPLIFY_PHI | \
1418 COMPILER_SIMPLIFY_LABEL | \
1419 COMPILER_SIMPLIFY_BRANCH | \
1420 COMPILER_SIMPLIFY_COPY | \
1421 COMPILER_SIMPLIFY_ARITH | \
1422 COMPILER_SIMPLIFY_SHIFT | \
1423 COMPILER_SIMPLIFY_BITWISE | \
1424 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001425 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001426 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001427
Eric Biederman153ea352003-06-20 14:43:20 +00001428#define GLOBAL_SCOPE_DEPTH 1
1429#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001430
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001431static void compile_file(struct compile_state *old_state, const char *filename, int local);
1432
Eric Biederman5ade04a2003-10-22 04:03:46 +00001433
1434
1435static void init_compiler_state(struct compiler_state *compiler)
1436{
1437 memset(compiler, 0, sizeof(*compiler));
1438 compiler->label_prefix = "";
1439 compiler->ofilename = "auto.inc";
1440 compiler->flags = COMPILER_DEFAULT_FLAGS;
1441 compiler->debug = 0;
1442 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001443 compiler->include_path_count = 1;
1444 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1445 compiler->define_count = 1;
1446 compiler->defines = xcmalloc(sizeof(char *), "defines");
1447 compiler->undef_count = 1;
1448 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001449}
1450
1451struct compiler_flag {
1452 const char *name;
1453 unsigned long flag;
1454};
Eric Biederman90089602004-05-28 14:11:54 +00001455
1456struct compiler_arg {
1457 const char *name;
1458 unsigned long mask;
1459 struct compiler_flag flags[16];
1460};
1461
Eric Biederman5ade04a2003-10-22 04:03:46 +00001462static int set_flag(
1463 const struct compiler_flag *ptr, unsigned long *flags,
1464 int act, const char *flag)
1465{
1466 int result = -1;
1467 for(; ptr->name; ptr++) {
1468 if (strcmp(ptr->name, flag) == 0) {
1469 break;
1470 }
1471 }
1472 if (ptr->name) {
1473 result = 0;
1474 *flags &= ~(ptr->flag);
1475 if (act) {
1476 *flags |= ptr->flag;
1477 }
1478 }
1479 return result;
1480}
1481
Eric Biederman90089602004-05-28 14:11:54 +00001482static int set_arg(
1483 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1484{
1485 const char *val;
1486 int result = -1;
1487 int len;
1488 val = strchr(arg, '=');
1489 if (val) {
1490 len = val - arg;
1491 val++;
1492 for(; ptr->name; ptr++) {
1493 if (strncmp(ptr->name, arg, len) == 0) {
1494 break;
1495 }
1496 }
1497 if (ptr->name) {
1498 *flags &= ~ptr->mask;
1499 result = set_flag(&ptr->flags[0], flags, 1, val);
1500 }
1501 }
1502 return result;
1503}
Eric Biederman90089602004-05-28 14:11:54 +00001504
Stefan Reinauer14e22772010-04-27 06:56:47 +00001505
1506static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001507 const char *prefix, const char *invert_prefix)
1508{
1509 for(;ptr->name; ptr++) {
1510 fprintf(fp, "%s%s\n", prefix, ptr->name);
1511 if (invert_prefix) {
1512 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1513 }
1514 }
1515}
1516
1517static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1518 const char *prefix)
1519{
1520 for(;ptr->name; ptr++) {
1521 const struct compiler_flag *flag;
1522 for(flag = &ptr->flags[0]; flag->name; flag++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001523 fprintf(fp, "%s%s=%s\n",
Eric Biederman90089602004-05-28 14:11:54 +00001524 prefix, ptr->name, flag->name);
1525 }
1526 }
1527}
1528
1529static int append_string(size_t *max, const char ***vec, const char *str,
1530 const char *name)
1531{
1532 size_t count;
1533 count = ++(*max);
1534 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1535 (*vec)[count -1] = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001536 (*vec)[count -2] = str;
Eric Biederman90089602004-05-28 14:11:54 +00001537 return 0;
1538}
1539
1540static void arg_error(char *fmt, ...);
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -08001541static void arg_warning(char *fmt, ...);
Eric Biederman90089602004-05-28 14:11:54 +00001542static const char *identifier(const char *str, const char *end);
1543
1544static int append_include_path(struct compiler_state *compiler, const char *str)
1545{
1546 int result;
1547 if (!exists(str, ".")) {
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -08001548 arg_warning("Warning: Nonexistent include path: `%s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001549 str);
1550 }
1551 result = append_string(&compiler->include_path_count,
1552 &compiler->include_paths, str, "include_paths");
1553 return result;
1554}
1555
1556static int append_define(struct compiler_state *compiler, const char *str)
1557{
1558 const char *end, *rest;
1559 int result;
1560
1561 end = strchr(str, '=');
1562 if (!end) {
1563 end = str + strlen(str);
1564 }
1565 rest = identifier(str, end);
1566 if (rest != end) {
1567 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001568 arg_error("Invalid name cannot define macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001569 len, len, str);
1570 }
1571 result = append_string(&compiler->define_count,
1572 &compiler->defines, str, "defines");
1573 return result;
1574}
1575
1576static int append_undef(struct compiler_state *compiler, const char *str)
1577{
1578 const char *end, *rest;
1579 int result;
1580
1581 end = str + strlen(str);
1582 rest = identifier(str, end);
1583 if (rest != end) {
1584 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001585 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001586 len, len, str);
1587 }
1588 result = append_string(&compiler->undef_count,
1589 &compiler->undefs, str, "undefs");
1590 return result;
1591}
1592
1593static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001594 { "trigraphs", COMPILER_TRIGRAPHS },
1595 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001596 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1597 { "simplify", COMPILER_SIMPLIFY },
1598 { "scc-transform", COMPILER_SCC_TRANSFORM },
1599 { "simplify-op", COMPILER_SIMPLIFY_OP },
1600 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1601 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1602 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1603 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1604 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1605 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1606 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1607 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1608 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1609 { 0, 0 },
1610};
1611static const struct compiler_arg romcc_args[] = {
1612 { "inline-policy", COMPILER_INLINE_MASK,
1613 {
1614 { "always", COMPILER_INLINE_ALWAYS, },
1615 { "never", COMPILER_INLINE_NEVER, },
1616 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1617 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1618 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1619 { 0, 0 },
1620 },
1621 },
1622 { 0, 0 },
1623};
1624static const struct compiler_flag romcc_opt_flags[] = {
1625 { "-O", COMPILER_SIMPLIFY },
1626 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001627 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001628 { 0, 0, },
1629};
1630static const struct compiler_flag romcc_debug_flags[] = {
1631 { "all", DEBUG_ALL },
1632 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1633 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1634 { "fdominators", DEBUG_FDOMINATORS },
1635 { "rdominators", DEBUG_RDOMINATORS },
1636 { "triples", DEBUG_TRIPLES },
1637 { "interference", DEBUG_INTERFERENCE },
1638 { "scc-transform", DEBUG_SCC_TRANSFORM },
1639 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1640 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1641 { "inline", DEBUG_INLINE },
1642 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1643 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1644 { "color-graph", DEBUG_COLOR_GRAPH },
1645 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1646 { "coalescing", DEBUG_COALESCING },
1647 { "coalescing2", DEBUG_COALESCING2 },
1648 { "verification", DEBUG_VERIFICATION },
1649 { "calls", DEBUG_CALLS },
1650 { "calls2", DEBUG_CALLS2 },
1651 { "tokens", DEBUG_TOKENS },
1652 { 0, 0 },
1653};
1654
Eric Biederman5ade04a2003-10-22 04:03:46 +00001655static int compiler_encode_flag(
1656 struct compiler_state *compiler, const char *flag)
1657{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001658 int act;
1659 int result;
1660
1661 act = 1;
1662 result = -1;
1663 if (strncmp(flag, "no-", 3) == 0) {
1664 flag += 3;
1665 act = 0;
1666 }
1667 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001668 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1669 }
1670 else if (strncmp(flag, "-E", 2) == 0) {
1671 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1672 }
1673 else if (strncmp(flag, "-I", 2) == 0) {
1674 result = append_include_path(compiler, flag + 2);
1675 }
1676 else if (strncmp(flag, "-D", 2) == 0) {
1677 result = append_define(compiler, flag + 2);
1678 }
1679 else if (strncmp(flag, "-U", 2) == 0) {
1680 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001681 }
1682 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1683 result = 0;
1684 compiler->label_prefix = flag + 13;
1685 }
1686 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1687 unsigned long max_passes;
1688 char *end;
1689 max_passes = strtoul(flag + 22, &end, 10);
1690 if (end[0] == '\0') {
1691 result = 0;
1692 compiler->max_allocation_passes = max_passes;
1693 }
1694 }
1695 else if (act && strcmp(flag, "debug") == 0) {
1696 result = 0;
1697 compiler->debug |= DEBUG_DEFAULT;
1698 }
1699 else if (strncmp(flag, "debug-", 6) == 0) {
1700 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001701 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001702 }
1703 else {
Eric Biederman90089602004-05-28 14:11:54 +00001704 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1705 if (result < 0) {
1706 result = set_arg(romcc_args, &compiler->flags, flag);
1707 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001708 }
1709 return result;
1710}
1711
Eric Biederman90089602004-05-28 14:11:54 +00001712static void compiler_usage(FILE *fp)
1713{
1714 flag_usage(fp, romcc_opt_flags, "", 0);
1715 flag_usage(fp, romcc_flags, "-f", "-fno-");
1716 arg_usage(fp, romcc_args, "-f");
1717 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1718 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1719 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1720 fprintf(fp, "-I<include path>\n");
1721 fprintf(fp, "-D<macro>[=defn]\n");
1722 fprintf(fp, "-U<macro>\n");
1723}
1724
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001725static void do_cleanup(struct compile_state *state)
1726{
1727 if (state->output) {
1728 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001729 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001730 state->output = 0;
1731 }
1732 if (state->dbgout) {
1733 fflush(state->dbgout);
1734 }
1735 if (state->errout) {
1736 fflush(state->errout);
1737 }
1738}
1739
1740static struct compile_state *exit_state;
1741static void exit_cleanup(void)
1742{
1743 if (exit_state) {
1744 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001745 }
1746}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001747
1748static int get_col(struct file_state *file)
1749{
1750 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001751 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001752 ptr = file->line_start;
1753 end = file->pos;
1754 for(col = 0; ptr < end; ptr++) {
1755 if (*ptr != '\t') {
1756 col++;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001757 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001758 else {
1759 col = (col & ~7) + 8;
1760 }
1761 }
1762 return col;
1763}
1764
1765static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1766{
1767 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001768 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001769 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001770 for(spot = triple->occurance; spot; spot = spot->parent) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001771 fprintf(fp, "%s:%d.%d: ",
Eric Biederman5ade04a2003-10-22 04:03:46 +00001772 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001773 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001774 return;
1775 }
1776 if (!state->file) {
1777 return;
1778 }
1779 col = get_col(state->file);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001780 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001781 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001782}
1783
Stefan Reinauer14e22772010-04-27 06:56:47 +00001784static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001785 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001786{
Eric Biederman90089602004-05-28 14:11:54 +00001787 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001788 va_list args;
1789 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001790 loc(fp, state, ptr);
1791 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001792 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001793 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001794 }
Eric Biederman90089602004-05-28 14:11:54 +00001795 fprintf(fp, "Internal compiler error: ");
1796 vfprintf(fp, fmt, args);
1797 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001798 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001799 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001800 abort();
1801}
1802
1803
Stefan Reinauer14e22772010-04-27 06:56:47 +00001804static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001805 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001806{
Eric Biederman90089602004-05-28 14:11:54 +00001807 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001808 va_list args;
1809 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001810 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001811 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001812 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001813 }
Eric Biederman90089602004-05-28 14:11:54 +00001814 fprintf(fp, "Internal compiler warning: ");
1815 vfprintf(fp, fmt, args);
1816 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001817 va_end(args);
1818}
1819
1820
1821
Stefan Reinauer14e22772010-04-27 06:56:47 +00001822static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001823 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001824{
Eric Biederman90089602004-05-28 14:11:54 +00001825 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001826 va_list args;
1827 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001828 loc(fp, state, ptr);
1829 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001830 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001831 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001832 }
Eric Biederman90089602004-05-28 14:11:54 +00001833 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001834 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001835 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001836 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001837 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001838 abort();
1839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001840 exit(1);
1841}
1842
Stefan Reinauer14e22772010-04-27 06:56:47 +00001843static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001844 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001845{
Eric Biederman90089602004-05-28 14:11:54 +00001846 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001847 va_list args;
1848 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001849 loc(fp, state, ptr);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001850 fprintf(fp, "warning: ");
Eric Biederman90089602004-05-28 14:11:54 +00001851 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1852 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1853 }
1854 vfprintf(fp, fmt, args);
1855 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001856 va_end(args);
1857}
1858
Eric Biedermanb138ac82003-04-22 18:44:01 +00001859#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1860
Eric Biederman0babc1c2003-05-09 02:39:00 +00001861static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001862{
1863 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001864 if (op >= OP_MAX) {
1865 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001866 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001867 if (op < 0) {
1868 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001869 }
1870}
1871
Eric Biederman0babc1c2003-05-09 02:39:00 +00001872static void valid_ins(struct compile_state *state, struct triple *ptr)
1873{
1874 valid_op(state, ptr->op);
1875}
1876
Stefan Reinauer50542a82007-10-24 11:14:14 +00001877#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001878static void valid_param_count(struct compile_state *state, struct triple *ins)
1879{
1880 int lhs, rhs, misc, targ;
1881 valid_ins(state, ins);
1882 lhs = table_ops[ins->op].lhs;
1883 rhs = table_ops[ins->op].rhs;
1884 misc = table_ops[ins->op].misc;
1885 targ = table_ops[ins->op].targ;
1886
1887 if ((lhs >= 0) && (ins->lhs != lhs)) {
1888 internal_error(state, ins, "Bad lhs count");
1889 }
1890 if ((rhs >= 0) && (ins->rhs != rhs)) {
1891 internal_error(state, ins, "Bad rhs count");
1892 }
1893 if ((misc >= 0) && (ins->misc != misc)) {
1894 internal_error(state, ins, "Bad misc count");
1895 }
1896 if ((targ >= 0) && (ins->targ != targ)) {
1897 internal_error(state, ins, "Bad targ count");
1898 }
1899}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001900#endif
Eric Biederman90089602004-05-28 14:11:54 +00001901
Eric Biedermanb138ac82003-04-22 18:44:01 +00001902static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001903static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001904static void use_triple(struct triple *used, struct triple *user)
1905{
1906 struct triple_set **ptr, *new;
1907 if (!used)
1908 return;
1909 if (!user)
1910 return;
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001911 ptr = &used->use;
1912 while(*ptr) {
1913 if ((*ptr)->member == user) {
1914 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001915 }
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001916 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001917 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00001918 /* Append new to the head of the list,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001919 * copy_func and rename_block_variables
1920 * depends on this.
1921 */
1922 new = xcmalloc(sizeof(*new), "triple_set");
1923 new->member = user;
1924 new->next = used->use;
1925 used->use = new;
1926}
1927
1928static void unuse_triple(struct triple *used, struct triple *unuser)
1929{
1930 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001931 if (!used) {
1932 return;
1933 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001934 ptr = &used->use;
1935 while(*ptr) {
1936 use = *ptr;
1937 if (use->member == unuser) {
1938 *ptr = use->next;
1939 xfree(use);
1940 }
1941 else {
1942 ptr = &use->next;
1943 }
1944 }
1945}
1946
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001947static void put_occurance(struct occurance *occurance)
1948{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001949 if (occurance) {
1950 occurance->count -= 1;
1951 if (occurance->count <= 0) {
1952 if (occurance->parent) {
1953 put_occurance(occurance->parent);
1954 }
1955 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001956 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001957 }
1958}
1959
1960static void get_occurance(struct occurance *occurance)
1961{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001962 if (occurance) {
1963 occurance->count += 1;
1964 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001965}
1966
1967
1968static struct occurance *new_occurance(struct compile_state *state)
1969{
1970 struct occurance *result, *last;
1971 const char *filename;
1972 const char *function;
1973 int line, col;
1974
1975 function = "";
1976 filename = 0;
1977 line = 0;
1978 col = 0;
1979 if (state->file) {
1980 filename = state->file->report_name;
1981 line = state->file->report_line;
1982 col = get_col(state->file);
1983 }
1984 if (state->function) {
1985 function = state->function;
1986 }
1987 last = state->last_occurance;
1988 if (last &&
1989 (last->col == col) &&
1990 (last->line == line) &&
1991 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001992 ((last->filename == filename) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00001993 (strcmp(last->filename, filename) == 0)))
Eric Biederman83b991a2003-10-11 06:20:25 +00001994 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001995 get_occurance(last);
1996 return last;
1997 }
1998 if (last) {
1999 state->last_occurance = 0;
2000 put_occurance(last);
2001 }
2002 result = xmalloc(sizeof(*result), "occurance");
2003 result->count = 2;
2004 result->filename = filename;
2005 result->function = function;
2006 result->line = line;
2007 result->col = col;
2008 result->parent = 0;
2009 state->last_occurance = result;
2010 return result;
2011}
2012
2013static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002014 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002015{
2016 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002017 if (top->parent) {
2018 internal_error(state, 0, "inlining an already inlined function?");
2019 }
2020 /* If I have a null base treat it that way */
2021 if ((base->parent == 0) &&
2022 (base->col == 0) &&
2023 (base->line == 0) &&
2024 (base->function[0] == '\0') &&
2025 (base->filename[0] == '\0')) {
2026 base = 0;
2027 }
2028 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002029 last = state->last_occurance;
2030 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002031 (last->parent == base) &&
2032 (last->col == top->col) &&
2033 (last->line == top->line) &&
2034 (last->function == top->function) &&
2035 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002036 get_occurance(last);
2037 return last;
2038 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002039 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002040 if (last) {
2041 state->last_occurance = 0;
2042 put_occurance(last);
2043 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002044 /* Generate a new occurance structure */
2045 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002046 result = xmalloc(sizeof(*result), "occurance");
2047 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002048 result->filename = top->filename;
2049 result->function = top->function;
2050 result->line = top->line;
2051 result->col = top->col;
2052 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002053 state->last_occurance = result;
2054 return result;
2055}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002056
2057static struct occurance dummy_occurance = {
2058 .count = 2,
2059 .filename = __FILE__,
2060 .function = "",
2061 .line = __LINE__,
2062 .col = 0,
2063 .parent = 0,
2064};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002065
Eric Biederman90089602004-05-28 14:11:54 +00002066/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002067 * from a triple. Having allows certain sanity checks to pass even
2068 * when the original triple that was pointed to is gone.
2069 */
Eric Biederman90089602004-05-28 14:11:54 +00002070static struct triple unknown_triple = {
2071 .next = &unknown_triple,
2072 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002073 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002074 .op = OP_UNKNOWNVAL,
2075 .lhs = 0,
2076 .rhs = 0,
2077 .misc = 0,
2078 .targ = 0,
2079 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002080 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002081 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002082 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002083 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002084};
2085
Eric Biederman0babc1c2003-05-09 02:39:00 +00002086
Eric Biederman90089602004-05-28 14:11:54 +00002087static size_t registers_of(struct compile_state *state, struct type *type);
2088
Stefan Reinauer14e22772010-04-27 06:56:47 +00002089static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002090 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2091 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002092{
Eric Biederman90089602004-05-28 14:11:54 +00002093 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002094 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002095 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002096 dummy.op = op;
2097 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002098 valid_op(state, op);
2099 lhs = table_ops[op].lhs;
2100 rhs = table_ops[op].rhs;
2101 misc = table_ops[op].misc;
2102 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002103
2104 switch(op) {
2105 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002106 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002107 break;
2108 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002109 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002110 break;
2111 case OP_ADECL:
2112 lhs = registers_of(state, type);
2113 break;
2114 case OP_TUPLE:
2115 lhs = registers_of(state, type);
2116 break;
2117 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002118 rhs = rhs_wanted;
2119 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002120 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002121 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002122 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002123 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002124 }
2125 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002126 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002127 }
2128 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002129 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002130 }
2131 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002132 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002133 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002134
2135 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002136 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002137 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2138
2139 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2140 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002141 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002142 ret->lhs = lhs;
2143 ret->rhs = rhs;
2144 ret->misc = misc;
2145 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002146 ret->type = type;
2147 ret->next = ret;
2148 ret->prev = ret;
2149 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002150 /* A simple sanity check */
2151 if ((ret->op != op) ||
2152 (ret->lhs != lhs) ||
2153 (ret->rhs != rhs) ||
2154 (ret->misc != misc) ||
2155 (ret->targ != targ) ||
2156 (ret->type != type) ||
2157 (ret->next != ret) ||
2158 (ret->prev != ret) ||
2159 (ret->occurance != occurance)) {
2160 internal_error(state, ret, "huh?");
2161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002162 return ret;
2163}
2164
Eric Biederman0babc1c2003-05-09 02:39:00 +00002165struct triple *dup_triple(struct compile_state *state, struct triple *src)
2166{
2167 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002168 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002169 src_lhs = src->lhs;
2170 src_rhs = src->rhs;
2171 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002172 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002173 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002174 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002175 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002176 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002177 return dup;
2178}
2179
Eric Biederman41203d92004-11-08 09:31:09 +00002180static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2181{
2182 struct triple *copy;
2183 copy = dup_triple(state, src);
2184 copy->use = 0;
2185 copy->next = copy->prev = copy;
2186 return copy;
2187}
2188
Stefan Reinauer14e22772010-04-27 06:56:47 +00002189static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002190 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002191{
2192 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002193 struct occurance *occurance;
2194 occurance = new_occurance(state);
2195 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002196 return ret;
2197}
2198
Stefan Reinauer14e22772010-04-27 06:56:47 +00002199static struct triple *build_triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002200 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002201 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002202{
2203 struct triple *ret;
2204 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002205 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002206 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002207 if (count > 0) {
2208 ret->param[0] = left;
2209 }
2210 if (count > 1) {
2211 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002212 }
2213 return ret;
2214}
2215
Stefan Reinauer14e22772010-04-27 06:56:47 +00002216static struct triple *triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002217 int op, struct type *type, struct triple *left, struct triple *right)
2218{
2219 struct triple *ret;
2220 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002221 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002222 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002223 if (count >= 1) {
2224 ret->param[0] = left;
2225 }
2226 if (count >= 2) {
2227 ret->param[1] = right;
2228 }
2229 return ret;
2230}
2231
Stefan Reinauer14e22772010-04-27 06:56:47 +00002232static struct triple *branch(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002233 struct triple *targ, struct triple *test)
2234{
2235 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002236 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002237 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002238 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002239 } else {
2240 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002241 }
2242 TARG(ret, 0) = targ;
2243 /* record the branch target was used */
2244 if (!targ || (targ->op != OP_LABEL)) {
2245 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002246 }
2247 return ret;
2248}
2249
Eric Biederman90089602004-05-28 14:11:54 +00002250static int triple_is_label(struct compile_state *state, struct triple *ins);
2251static int triple_is_call(struct compile_state *state, struct triple *ins);
2252static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002253static void insert_triple(struct compile_state *state,
2254 struct triple *first, struct triple *ptr)
2255{
2256 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002257 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002258 internal_error(state, ptr, "expression already used");
2259 }
2260 ptr->next = first;
2261 ptr->prev = first->prev;
2262 ptr->prev->next = ptr;
2263 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002264
2265 if (triple_is_cbranch(state, ptr->prev) ||
2266 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002267 unuse_triple(first, ptr->prev);
2268 use_triple(ptr, ptr->prev);
2269 }
2270 }
2271}
2272
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002273static int triple_stores_block(struct compile_state *state, struct triple *ins)
2274{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002275 /* This function is used to determine if u.block
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002276 * is utilized to store the current block number.
2277 */
2278 int stores_block;
2279 valid_ins(state, ins);
2280 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2281 return stores_block;
2282}
2283
Eric Biederman90089602004-05-28 14:11:54 +00002284static int triple_is_branch(struct compile_state *state, struct triple *ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002285static struct block *block_of_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002286 struct triple *ins)
2287{
2288 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002289 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002290 return 0;
2291 }
2292 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002293 while(ins != first && !triple_is_branch(state, ins->prev) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00002294 !triple_stores_block(state, ins))
2295 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002296 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002297 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002298 }
2299 ins = ins->prev;
2300 }
Eric Biederman90089602004-05-28 14:11:54 +00002301 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002302}
2303
Eric Biederman90089602004-05-28 14:11:54 +00002304static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002305static struct triple *pre_triple(struct compile_state *state,
2306 struct triple *base,
2307 int op, struct type *type, struct triple *left, struct triple *right)
2308{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002309 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002310 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002311 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002312 /* If I am an OP_PIECE jump to the real instruction */
2313 if (base->op == OP_PIECE) {
2314 base = MISC(base, 0);
2315 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002316 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002317 get_occurance(base->occurance);
2318 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002319 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002320 if (triple_stores_block(state, ret)) {
2321 ret->u.block = block;
2322 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002323 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002324 for(i = 0; i < ret->lhs; i++) {
2325 struct triple *piece;
2326 piece = LHS(ret, i);
2327 insert_triple(state, base, piece);
2328 use_triple(ret, piece);
2329 use_triple(piece, ret);
2330 }
2331 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002332 block->first = ret;
2333 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002334 return ret;
2335}
2336
2337static struct triple *post_triple(struct compile_state *state,
2338 struct triple *base,
2339 int op, struct type *type, struct triple *left, struct triple *right)
2340{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002341 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002342 struct triple *ret, *next;
2343 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002344 /* If I am an OP_PIECE jump to the real instruction */
2345 if (base->op == OP_PIECE) {
2346 base = MISC(base, 0);
2347 }
2348 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002349 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002350 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002351 base = LHS(base, zlhs - 1);
2352 }
2353
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002354 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002355 get_occurance(base->occurance);
2356 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002357 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002358 if (triple_stores_block(state, ret)) {
2359 ret->u.block = block;
2360 }
Eric Biederman90089602004-05-28 14:11:54 +00002361 next = base->next;
2362 insert_triple(state, next, ret);
2363 zlhs = ret->lhs;
2364 for(i = 0; i < zlhs; i++) {
2365 struct triple *piece;
2366 piece = LHS(ret, i);
2367 insert_triple(state, next, piece);
2368 use_triple(ret, piece);
2369 use_triple(piece, ret);
2370 }
2371 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002372 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002373 if (zlhs) {
2374 block->last = LHS(ret, zlhs - 1);
2375 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002376 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002377 return ret;
2378}
2379
Eric Biederman90089602004-05-28 14:11:54 +00002380static struct type *reg_type(
2381 struct compile_state *state, struct type *type, int reg);
2382
2383static void generate_lhs_piece(
2384 struct compile_state *state, struct triple *ins, int index)
2385{
2386 struct type *piece_type;
2387 struct triple *piece;
2388 get_occurance(ins->occurance);
2389 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2390
2391 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2392 piece_type = piece_type->left;
2393 }
2394#if 0
2395{
2396 static void name_of(FILE *fp, struct type *type);
2397 FILE * fp = state->errout;
2398 fprintf(fp, "piece_type(%d): ", index);
2399 name_of(fp, piece_type);
2400 fprintf(fp, "\n");
2401}
2402#endif
2403 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2404 piece->u.cval = index;
2405 LHS(ins, piece->u.cval) = piece;
2406 MISC(piece, 0) = ins;
2407}
2408
2409static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2410{
2411 int i, zlhs;
2412 zlhs = ins->lhs;
2413 for(i = 0; i < zlhs; i++) {
2414 generate_lhs_piece(state, ins, i);
2415 }
2416}
2417
Eric Biedermanb138ac82003-04-22 18:44:01 +00002418static struct triple *label(struct compile_state *state)
2419{
2420 /* Labels don't get a type */
2421 struct triple *result;
2422 result = triple(state, OP_LABEL, &void_type, 0, 0);
2423 return result;
2424}
2425
Eric Biederman90089602004-05-28 14:11:54 +00002426static struct triple *mkprog(struct compile_state *state, ...)
2427{
2428 struct triple *prog, *head, *arg;
2429 va_list args;
2430 int i;
2431
2432 head = label(state);
2433 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2434 RHS(prog, 0) = head;
2435 va_start(args, state);
2436 i = 0;
2437 while((arg = va_arg(args, struct triple *)) != 0) {
2438 if (++i >= 100) {
2439 internal_error(state, 0, "too many arguments to mkprog");
2440 }
2441 flatten(state, head, arg);
2442 }
2443 va_end(args);
2444 prog->type = head->prev->type;
2445 return prog;
2446}
2447static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002448static void display_triple(FILE *fp, struct triple *ins)
2449{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002450 struct occurance *ptr;
2451 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002452 char pre, post, vol;
2453 pre = post = vol = ' ';
2454 if (ins) {
2455 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2456 pre = '^';
2457 }
2458 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2459 post = ',';
2460 }
2461 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2462 vol = 'v';
2463 }
2464 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002465 }
Eric Biederman90089602004-05-28 14:11:54 +00002466 if (ins == 0) {
2467 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002468 }
Eric Biederman90089602004-05-28 14:11:54 +00002469 else if (ins->op == OP_INTCONST) {
2470 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002471 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002472 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002473 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002474 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002475 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002476 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002477 MISC(ins, 0), (unsigned long)(ins->u.cval));
2478 }
2479 else if (ins->op == OP_INDEX) {
2480 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002481 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002482 RHS(ins, 0), (unsigned long)(ins->u.cval));
2483 }
2484 else if (ins->op == OP_PIECE) {
2485 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002486 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002487 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002488 }
2489 else {
2490 int i, count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002491 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
Eric Biederman90089602004-05-28 14:11:54 +00002492 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2493 if (table_ops[ins->op].flags & BITFIELD) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002494 fprintf(fp, " <%2d-%2d:%2d>",
Eric Biederman90089602004-05-28 14:11:54 +00002495 ins->u.bitfield.offset,
2496 ins->u.bitfield.offset + ins->u.bitfield.size,
2497 ins->u.bitfield.size);
2498 }
2499 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002500 for(i = 0; i < count; i++) {
2501 fprintf(fp, " %-10p", ins->param[i]);
2502 }
2503 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002504 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002505 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002506 }
Eric Biederman90089602004-05-28 14:11:54 +00002507 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002508 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002509#if DEBUG_DISPLAY_TYPES
2510 fprintf(fp, " <");
2511 name_of(fp, ins->type);
2512 fprintf(fp, "> ");
2513#endif
2514#if DEBUG_DISPLAY_USES
2515 fprintf(fp, " [");
2516 for(user = ins->use; user; user = user->next) {
2517 fprintf(fp, " %-10p", user->member);
2518 }
2519 fprintf(fp, " ]");
2520#endif
2521 fprintf(fp, " @");
2522 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2523 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002524 ptr->function,
Eric Biederman90089602004-05-28 14:11:54 +00002525 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002526 ptr->line,
Eric Biederman90089602004-05-28 14:11:54 +00002527 ptr->col);
2528 }
2529 if (ins->op == OP_ASM) {
2530 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002531 }
2532 }
Eric Biederman90089602004-05-28 14:11:54 +00002533 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002534 fflush(fp);
2535}
2536
Eric Biederman90089602004-05-28 14:11:54 +00002537static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002538static void display_triple_changes(
2539 FILE *fp, const struct triple *new, const struct triple *orig)
2540{
2541
2542 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002543 new_count = TRIPLE_SIZE(new);
2544 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002545 if ((new->op != orig->op) ||
2546 (new_count != orig_count) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002547 (memcmp(orig->param, new->param,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002548 orig_count * sizeof(orig->param[0])) != 0) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002549 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
Eric Biederman5ade04a2003-10-22 04:03:46 +00002550 {
2551 struct occurance *ptr;
2552 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002553 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002554 if (orig->op == new->op) {
2555 fprintf(fp, " %-11s", tops(orig->op));
2556 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002557 fprintf(fp, " [%-10s %-10s]",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002558 tops(new->op), tops(orig->op));
2559 }
2560 min_count = new_count;
2561 if (min_count > orig_count) {
2562 min_count = orig_count;
2563 }
2564 for(indent = i = 0; i < min_count; i++) {
2565 if (orig->param[i] == new->param[i]) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002566 fprintf(fp, " %-11p",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002567 orig->param[i]);
2568 indent += 12;
2569 } else {
2570 fprintf(fp, " [%-10p %-10p]",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002571 new->param[i],
Eric Biederman5ade04a2003-10-22 04:03:46 +00002572 orig->param[i]);
2573 indent += 24;
2574 }
2575 }
2576 for(; i < orig_count; i++) {
2577 fprintf(fp, " [%-9p]", orig->param[i]);
2578 indent += 12;
2579 }
2580 for(; i < new_count; i++) {
2581 fprintf(fp, " [%-9p]", new->param[i]);
2582 indent += 12;
2583 }
2584 if ((new->op == OP_INTCONST)||
2585 (new->op == OP_ADDRCONST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002586 fprintf(fp, " <0x%08lx>",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002587 (unsigned long)(new->u.cval));
2588 indent += 13;
2589 }
2590 for(;indent < 36; indent++) {
2591 putc(' ', fp);
2592 }
Eric Biederman90089602004-05-28 14:11:54 +00002593
2594#if DEBUG_DISPLAY_TYPES
2595 fprintf(fp, " <");
2596 name_of(fp, new->type);
2597 if (!equiv_types(new->type, orig->type)) {
2598 fprintf(fp, " -- ");
2599 name_of(fp, orig->type);
2600 }
2601 fprintf(fp, "> ");
2602#endif
2603
Eric Biederman5ade04a2003-10-22 04:03:46 +00002604 fprintf(fp, " @");
2605 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2606 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002607 ptr->function,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002608 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002609 ptr->line,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002610 ptr->col);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002611
Eric Biederman5ade04a2003-10-22 04:03:46 +00002612 }
2613 fprintf(fp, "\n");
2614 fflush(fp);
2615 }
2616}
2617
Eric Biederman83b991a2003-10-11 06:20:25 +00002618static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002619{
2620 /* Does the triple have no side effects.
Stefan Reinauer14e22772010-04-27 06:56:47 +00002621 * I.e. Rexecuting the triple with the same arguments
Eric Biedermanb138ac82003-04-22 18:44:01 +00002622 * gives the same value.
2623 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002624 unsigned pure;
2625 valid_ins(state, ins);
2626 pure = PURE_BITS(table_ops[ins->op].flags);
2627 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002628 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002629 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002630 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002631 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002632}
2633
Stefan Reinauer14e22772010-04-27 06:56:47 +00002634static int triple_is_branch_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002635 struct triple *ins, unsigned type)
2636{
2637 /* Is this one of the passed branch types? */
2638 valid_ins(state, ins);
2639 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2640}
2641
Eric Biederman0babc1c2003-05-09 02:39:00 +00002642static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002643{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002644 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002645 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002646 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002647}
2648
Eric Biederman90089602004-05-28 14:11:54 +00002649static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002650{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002651 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002652 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002653}
2654
Eric Biederman90089602004-05-28 14:11:54 +00002655static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002656{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002657 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002658 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002659 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002660 type = BRANCH_BITS(table_ops[ins->op].flags);
2661 return (type != 0) && (type != CBRANCH);
2662}
2663
2664static int triple_is_call(struct compile_state *state, struct triple *ins)
2665{
2666 /* Is this triple a call instruction? */
2667 return triple_is_branch_type(state, ins, CALLBRANCH);
2668}
2669
2670static int triple_is_ret(struct compile_state *state, struct triple *ins)
2671{
2672 /* Is this triple a return instruction? */
2673 return triple_is_branch_type(state, ins, RETBRANCH);
2674}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002675
Stefan Reinauer50542a82007-10-24 11:14:14 +00002676#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002677static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2678{
2679 /* Is this triple an unconditional branch and not a call or a
2680 * return? */
2681 return triple_is_branch_type(state, ins, UBRANCH);
2682}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002683#endif
Eric Biederman90089602004-05-28 14:11:54 +00002684
2685static int triple_is_end(struct compile_state *state, struct triple *ins)
2686{
2687 return triple_is_branch_type(state, ins, ENDBRANCH);
2688}
2689
2690static int triple_is_label(struct compile_state *state, struct triple *ins)
2691{
2692 valid_ins(state, ins);
2693 return (ins->op == OP_LABEL);
2694}
2695
2696static struct triple *triple_to_block_start(
2697 struct compile_state *state, struct triple *start)
2698{
2699 while(!triple_is_branch(state, start->prev) &&
2700 (!triple_is_label(state, start) || !start->use)) {
2701 start = start->prev;
2702 }
2703 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002704}
2705
Eric Biederman0babc1c2003-05-09 02:39:00 +00002706static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002707{
2708 /* This function is used to determine which triples need
2709 * a register.
2710 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002711 int is_def;
2712 valid_ins(state, ins);
2713 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002714 if (ins->lhs >= 1) {
2715 is_def = 0;
2716 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002717 return is_def;
2718}
2719
Eric Biederman83b991a2003-10-11 06:20:25 +00002720static int triple_is_structural(struct compile_state *state, struct triple *ins)
2721{
2722 int is_structural;
2723 valid_ins(state, ins);
2724 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2725 return is_structural;
2726}
2727
Eric Biederman90089602004-05-28 14:11:54 +00002728static int triple_is_part(struct compile_state *state, struct triple *ins)
2729{
2730 int is_part;
2731 valid_ins(state, ins);
2732 is_part = (table_ops[ins->op].flags & PART) == PART;
2733 return is_part;
2734}
2735
2736static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2737{
2738 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2739}
2740
Eric Biederman0babc1c2003-05-09 02:39:00 +00002741static struct triple **triple_iter(struct compile_state *state,
2742 size_t count, struct triple **vector,
2743 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002744{
2745 struct triple **ret;
2746 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002747 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002748 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002749 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002750 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002751 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002752 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002753 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002754 }
2755 return ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002756
Eric Biedermanb138ac82003-04-22 18:44:01 +00002757}
2758
2759static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002760 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002761{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002762 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002763 ins, last);
2764}
2765
2766static struct triple **triple_rhs(struct compile_state *state,
2767 struct triple *ins, struct triple **last)
2768{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002769 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002770 ins, last);
2771}
2772
2773static struct triple **triple_misc(struct compile_state *state,
2774 struct triple *ins, struct triple **last)
2775{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002776 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002777 ins, last);
2778}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002779
Eric Biederman90089602004-05-28 14:11:54 +00002780static struct triple **do_triple_targ(struct compile_state *state,
2781 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002782{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002783 size_t count;
2784 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002785 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002786 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002787 count = ins->targ;
2788 next_is_targ = 0;
2789 if (triple_is_cbranch(state, ins)) {
2790 next_is_targ = 1;
2791 }
2792 if (!call_edges && triple_is_call(state, ins)) {
2793 count = 0;
2794 }
2795 if (next_edges && triple_is_call(state, ins)) {
2796 next_is_targ = 1;
2797 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002798 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002799 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002800 if (!last) {
2801 ret = &ins->next;
2802 } else if (last == &ins->next) {
2803 last = 0;
2804 }
2805 }
2806 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002807 if (!last) {
2808 ret = vector;
2809 }
2810 else if ((last >= vector) && (last < (vector + count - 1))) {
2811 ret = last + 1;
2812 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002813 else if (last == vector + count - 1) {
2814 last = 0;
2815 }
2816 }
Eric Biederman90089602004-05-28 14:11:54 +00002817 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002818 struct triple_set *use;
2819 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002820 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002821 continue;
2822 }
2823 if (!last) {
2824 ret = &use->member->next;
2825 break;
2826 }
2827 else if (last == &use->member->next) {
2828 last = 0;
2829 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002830 }
2831 }
2832 return ret;
2833}
2834
Eric Biederman90089602004-05-28 14:11:54 +00002835static struct triple **triple_targ(struct compile_state *state,
2836 struct triple *ins, struct triple **last)
2837{
2838 return do_triple_targ(state, ins, last, 1, 1);
2839}
2840
2841static struct triple **triple_edge_targ(struct compile_state *state,
2842 struct triple *ins, struct triple **last)
2843{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002844 return do_triple_targ(state, ins, last,
Eric Biederman90089602004-05-28 14:11:54 +00002845 state->functions_joined, !state->functions_joined);
2846}
2847
2848static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2849{
2850 struct triple *next;
2851 int lhs, i;
2852 lhs = ins->lhs;
2853 next = ins->next;
2854 for(i = 0; i < lhs; i++) {
2855 struct triple *piece;
2856 piece = LHS(ins, i);
2857 if (next != piece) {
2858 internal_error(state, ins, "malformed lhs on %s",
2859 tops(ins->op));
2860 }
2861 if (next->op != OP_PIECE) {
2862 internal_error(state, ins, "bad lhs op %s at %d on %s",
2863 tops(next->op), i, tops(ins->op));
2864 }
2865 if (next->u.cval != i) {
2866 internal_error(state, ins, "bad u.cval of %d %d expected",
2867 next->u.cval, i);
2868 }
2869 next = next->next;
2870 }
2871 return next;
2872}
2873
2874/* Function piece accessor functions */
Stefan Reinauer14e22772010-04-27 06:56:47 +00002875static struct triple *do_farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002876 struct triple *func, unsigned index)
2877{
2878 struct type *ftype;
2879 struct triple *first, *arg;
2880 unsigned i;
2881
2882 ftype = func->type;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +11002883 if(index >= (ftype->elements + 2)) {
Eric Biederman90089602004-05-28 14:11:54 +00002884 internal_error(state, func, "bad argument index: %d", index);
2885 }
2886 first = RHS(func, 0);
2887 arg = first->next;
2888 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2889 /* do nothing */
2890 }
2891 if (arg->op != OP_ADECL) {
2892 internal_error(state, 0, "arg not adecl?");
2893 }
2894 return arg;
2895}
2896static struct triple *fresult(struct compile_state *state, struct triple *func)
2897{
2898 return do_farg(state, func, 0);
2899}
2900static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2901{
2902 return do_farg(state, func, 1);
2903}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002904static struct triple *farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002905 struct triple *func, unsigned index)
2906{
2907 return do_farg(state, func, index + 2);
2908}
2909
2910
2911static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2912{
2913 struct triple *first, *ins;
2914 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2915 first = ins = RHS(func, 0);
2916 do {
2917 if (triple_is_label(state, ins) && ins->use) {
2918 fprintf(fp, "%p:\n", ins);
2919 }
2920 display_triple(fp, ins);
2921
2922 if (triple_is_branch(state, ins)) {
2923 fprintf(fp, "\n");
2924 }
2925 if (ins->next->prev != ins) {
2926 internal_error(state, ins->next, "bad prev");
2927 }
2928 ins = ins->next;
2929 } while(ins != first);
2930}
2931
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002932static void verify_use(struct compile_state *state,
2933 struct triple *user, struct triple *used)
2934{
2935 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002936 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002937 for(i = 0; i < size; i++) {
2938 if (user->param[i] == used) {
2939 break;
2940 }
2941 }
2942 if (triple_is_branch(state, user)) {
2943 if (user->next == used) {
2944 i = -1;
2945 }
2946 }
2947 if (i == size) {
2948 internal_error(state, user, "%s(%p) does not use %s(%p)",
2949 tops(user->op), user, tops(used->op), used);
2950 }
2951}
2952
Stefan Reinauer14e22772010-04-27 06:56:47 +00002953static int find_rhs_use(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002954 struct triple *user, struct triple *used)
2955{
2956 struct triple **param;
2957 int size, i;
2958 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002959
2960#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002961#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002962#endif
Eric Biederman90089602004-05-28 14:11:54 +00002963 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002964 param = &RHS(user, 0);
2965 for(i = 0; i < size; i++) {
2966 if (param[i] == used) {
2967 return i;
2968 }
2969 }
2970 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002971}
2972
2973static void free_triple(struct compile_state *state, struct triple *ptr)
2974{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002975 size_t size;
2976 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002977 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002978 ptr->prev->next = ptr->next;
2979 ptr->next->prev = ptr->prev;
2980 if (ptr->use) {
2981 internal_error(state, ptr, "ptr->use != 0");
2982 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002983 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002984 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002985 xfree(ptr);
2986}
2987
2988static void release_triple(struct compile_state *state, struct triple *ptr)
2989{
2990 struct triple_set *set, *next;
2991 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002992 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002993 if (ptr == &unknown_triple) {
2994 return;
2995 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002996 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002997 /* Make certain the we are not the first or last element of a block */
2998 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002999 if (block) {
3000 if ((block->last == ptr) && (block->first == ptr)) {
3001 block->last = block->first = 0;
3002 }
3003 else if (block->last == ptr) {
3004 block->last = ptr->prev;
3005 }
3006 else if (block->first == ptr) {
3007 block->first = ptr->next;
3008 }
Eric Biederman66fe2222003-07-04 15:14:04 +00003009 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003010 /* Remove ptr from use chains where it is the user */
3011 expr = triple_rhs(state, ptr, 0);
3012 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3013 if (*expr) {
3014 unuse_triple(*expr, ptr);
3015 }
3016 }
3017 expr = triple_lhs(state, ptr, 0);
3018 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3019 if (*expr) {
3020 unuse_triple(*expr, ptr);
3021 }
3022 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003023 expr = triple_misc(state, ptr, 0);
3024 for(; expr; expr = triple_misc(state, ptr, expr)) {
3025 if (*expr) {
3026 unuse_triple(*expr, ptr);
3027 }
3028 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003029 expr = triple_targ(state, ptr, 0);
3030 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003031 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003032 unuse_triple(*expr, ptr);
3033 }
3034 }
3035 /* Reomve ptr from use chains where it is used */
3036 for(set = ptr->use; set; set = next) {
3037 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003038 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003039 expr = triple_rhs(state, set->member, 0);
3040 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3041 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003042 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003043 }
3044 }
3045 expr = triple_lhs(state, set->member, 0);
3046 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3047 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003048 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003049 }
3050 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003051 expr = triple_misc(state, set->member, 0);
3052 for(; expr; expr = triple_misc(state, set->member, expr)) {
3053 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003054 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003055 }
3056 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003057 expr = triple_targ(state, set->member, 0);
3058 for(; expr; expr = triple_targ(state, set->member, expr)) {
3059 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003060 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003061 }
3062 }
3063 unuse_triple(ptr, set->member);
3064 }
3065 free_triple(state, ptr);
3066}
3067
Eric Biederman5ade04a2003-10-22 04:03:46 +00003068static void print_triples(struct compile_state *state);
3069static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003070
Jason Schildt27b85112005-08-10 14:31:52 +00003071#define TOK_UNKNOWN 0
3072#define TOK_SPACE 1
3073#define TOK_SEMI 2
3074#define TOK_LBRACE 3
3075#define TOK_RBRACE 4
3076#define TOK_COMMA 5
3077#define TOK_EQ 6
3078#define TOK_COLON 7
3079#define TOK_LBRACKET 8
3080#define TOK_RBRACKET 9
3081#define TOK_LPAREN 10
3082#define TOK_RPAREN 11
3083#define TOK_STAR 12
3084#define TOK_DOTS 13
3085#define TOK_MORE 14
3086#define TOK_LESS 15
3087#define TOK_TIMESEQ 16
3088#define TOK_DIVEQ 17
3089#define TOK_MODEQ 18
3090#define TOK_PLUSEQ 19
3091#define TOK_MINUSEQ 20
3092#define TOK_SLEQ 21
3093#define TOK_SREQ 22
3094#define TOK_ANDEQ 23
3095#define TOK_XOREQ 24
3096#define TOK_OREQ 25
3097#define TOK_EQEQ 26
3098#define TOK_NOTEQ 27
3099#define TOK_QUEST 28
3100#define TOK_LOGOR 29
3101#define TOK_LOGAND 30
3102#define TOK_OR 31
3103#define TOK_AND 32
3104#define TOK_XOR 33
3105#define TOK_LESSEQ 34
3106#define TOK_MOREEQ 35
3107#define TOK_SL 36
3108#define TOK_SR 37
3109#define TOK_PLUS 38
3110#define TOK_MINUS 39
3111#define TOK_DIV 40
3112#define TOK_MOD 41
3113#define TOK_PLUSPLUS 42
3114#define TOK_MINUSMINUS 43
3115#define TOK_BANG 44
3116#define TOK_ARROW 45
3117#define TOK_DOT 46
3118#define TOK_TILDE 47
3119#define TOK_LIT_STRING 48
3120#define TOK_LIT_CHAR 49
3121#define TOK_LIT_INT 50
3122#define TOK_LIT_FLOAT 51
3123#define TOK_MACRO 52
3124#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003125
Jason Schildt27b85112005-08-10 14:31:52 +00003126#define TOK_IDENT 54
3127#define TOK_STRUCT_NAME 55
3128#define TOK_ENUM_CONST 56
3129#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003130
Jason Schildt27b85112005-08-10 14:31:52 +00003131#define TOK_AUTO 58
3132#define TOK_BREAK 59
3133#define TOK_CASE 60
3134#define TOK_CHAR 61
3135#define TOK_CONST 62
3136#define TOK_CONTINUE 63
3137#define TOK_DEFAULT 64
3138#define TOK_DO 65
3139#define TOK_DOUBLE 66
3140#define TOK_ELSE 67
3141#define TOK_ENUM 68
3142#define TOK_EXTERN 69
3143#define TOK_FLOAT 70
3144#define TOK_FOR 71
3145#define TOK_GOTO 72
3146#define TOK_IF 73
3147#define TOK_INLINE 74
3148#define TOK_INT 75
3149#define TOK_LONG 76
3150#define TOK_REGISTER 77
3151#define TOK_RESTRICT 78
3152#define TOK_RETURN 79
3153#define TOK_SHORT 80
3154#define TOK_SIGNED 81
3155#define TOK_SIZEOF 82
3156#define TOK_STATIC 83
3157#define TOK_STRUCT 84
3158#define TOK_SWITCH 85
3159#define TOK_TYPEDEF 86
3160#define TOK_UNION 87
3161#define TOK_UNSIGNED 88
3162#define TOK_VOID 89
3163#define TOK_VOLATILE 90
3164#define TOK_WHILE 91
3165#define TOK_ASM 92
3166#define TOK_ATTRIBUTE 93
3167#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003168#define TOK_FIRST_KEYWORD TOK_AUTO
3169#define TOK_LAST_KEYWORD TOK_ALIGNOF
3170
Eric Biedermancb364952004-11-15 10:46:44 +00003171#define TOK_MDEFINE 100
3172#define TOK_MDEFINED 101
3173#define TOK_MUNDEF 102
3174#define TOK_MINCLUDE 103
3175#define TOK_MLINE 104
3176#define TOK_MERROR 105
3177#define TOK_MWARNING 106
3178#define TOK_MPRAGMA 107
3179#define TOK_MIFDEF 108
3180#define TOK_MIFNDEF 109
3181#define TOK_MELIF 110
3182#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003183
Eric Biedermancb364952004-11-15 10:46:44 +00003184#define TOK_FIRST_MACRO TOK_MDEFINE
3185#define TOK_LAST_MACRO TOK_MENDIF
Stefan Reinauer14e22772010-04-27 06:56:47 +00003186
Eric Biedermancb364952004-11-15 10:46:44 +00003187#define TOK_MIF 112
3188#define TOK_MELSE 113
3189#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003190
Eric Biedermancb364952004-11-15 10:46:44 +00003191#define TOK_EOL 115
3192#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003193
3194static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003195[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003196[TOK_SPACE ] = ":space:",
3197[TOK_SEMI ] = ";",
3198[TOK_LBRACE ] = "{",
3199[TOK_RBRACE ] = "}",
3200[TOK_COMMA ] = ",",
3201[TOK_EQ ] = "=",
3202[TOK_COLON ] = ":",
3203[TOK_LBRACKET ] = "[",
3204[TOK_RBRACKET ] = "]",
3205[TOK_LPAREN ] = "(",
3206[TOK_RPAREN ] = ")",
3207[TOK_STAR ] = "*",
3208[TOK_DOTS ] = "...",
3209[TOK_MORE ] = ">",
3210[TOK_LESS ] = "<",
3211[TOK_TIMESEQ ] = "*=",
3212[TOK_DIVEQ ] = "/=",
3213[TOK_MODEQ ] = "%=",
3214[TOK_PLUSEQ ] = "+=",
3215[TOK_MINUSEQ ] = "-=",
3216[TOK_SLEQ ] = "<<=",
3217[TOK_SREQ ] = ">>=",
3218[TOK_ANDEQ ] = "&=",
3219[TOK_XOREQ ] = "^=",
3220[TOK_OREQ ] = "|=",
3221[TOK_EQEQ ] = "==",
3222[TOK_NOTEQ ] = "!=",
3223[TOK_QUEST ] = "?",
3224[TOK_LOGOR ] = "||",
3225[TOK_LOGAND ] = "&&",
3226[TOK_OR ] = "|",
3227[TOK_AND ] = "&",
3228[TOK_XOR ] = "^",
3229[TOK_LESSEQ ] = "<=",
3230[TOK_MOREEQ ] = ">=",
3231[TOK_SL ] = "<<",
3232[TOK_SR ] = ">>",
3233[TOK_PLUS ] = "+",
3234[TOK_MINUS ] = "-",
3235[TOK_DIV ] = "/",
3236[TOK_MOD ] = "%",
3237[TOK_PLUSPLUS ] = "++",
3238[TOK_MINUSMINUS ] = "--",
3239[TOK_BANG ] = "!",
3240[TOK_ARROW ] = "->",
3241[TOK_DOT ] = ".",
3242[TOK_TILDE ] = "~",
3243[TOK_LIT_STRING ] = ":string:",
3244[TOK_IDENT ] = ":ident:",
3245[TOK_TYPE_NAME ] = ":typename:",
3246[TOK_LIT_CHAR ] = ":char:",
3247[TOK_LIT_INT ] = ":integer:",
3248[TOK_LIT_FLOAT ] = ":float:",
3249[TOK_MACRO ] = "#",
3250[TOK_CONCATENATE ] = "##",
3251
3252[TOK_AUTO ] = "auto",
3253[TOK_BREAK ] = "break",
3254[TOK_CASE ] = "case",
3255[TOK_CHAR ] = "char",
3256[TOK_CONST ] = "const",
3257[TOK_CONTINUE ] = "continue",
3258[TOK_DEFAULT ] = "default",
3259[TOK_DO ] = "do",
3260[TOK_DOUBLE ] = "double",
3261[TOK_ELSE ] = "else",
3262[TOK_ENUM ] = "enum",
3263[TOK_EXTERN ] = "extern",
3264[TOK_FLOAT ] = "float",
3265[TOK_FOR ] = "for",
3266[TOK_GOTO ] = "goto",
3267[TOK_IF ] = "if",
3268[TOK_INLINE ] = "inline",
3269[TOK_INT ] = "int",
3270[TOK_LONG ] = "long",
3271[TOK_REGISTER ] = "register",
3272[TOK_RESTRICT ] = "restrict",
3273[TOK_RETURN ] = "return",
3274[TOK_SHORT ] = "short",
3275[TOK_SIGNED ] = "signed",
3276[TOK_SIZEOF ] = "sizeof",
3277[TOK_STATIC ] = "static",
3278[TOK_STRUCT ] = "struct",
3279[TOK_SWITCH ] = "switch",
3280[TOK_TYPEDEF ] = "typedef",
3281[TOK_UNION ] = "union",
3282[TOK_UNSIGNED ] = "unsigned",
3283[TOK_VOID ] = "void",
3284[TOK_VOLATILE ] = "volatile",
3285[TOK_WHILE ] = "while",
3286[TOK_ASM ] = "asm",
3287[TOK_ATTRIBUTE ] = "__attribute__",
3288[TOK_ALIGNOF ] = "__alignof__",
3289
Eric Biederman41203d92004-11-08 09:31:09 +00003290[TOK_MDEFINE ] = "#define",
3291[TOK_MDEFINED ] = "#defined",
3292[TOK_MUNDEF ] = "#undef",
3293[TOK_MINCLUDE ] = "#include",
3294[TOK_MLINE ] = "#line",
3295[TOK_MERROR ] = "#error",
3296[TOK_MWARNING ] = "#warning",
3297[TOK_MPRAGMA ] = "#pragma",
3298[TOK_MIFDEF ] = "#ifdef",
3299[TOK_MIFNDEF ] = "#ifndef",
3300[TOK_MELIF ] = "#elif",
3301[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003302
Eric Biederman41203d92004-11-08 09:31:09 +00003303[TOK_MIF ] = "#if",
3304[TOK_MELSE ] = "#else",
3305[TOK_MIDENT ] = "#:ident:",
Stefan Reinauer14e22772010-04-27 06:56:47 +00003306[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003307[TOK_EOF ] = "EOF",
3308};
3309
3310static unsigned int hash(const char *str, int str_len)
3311{
3312 unsigned int hash;
3313 const char *end;
3314 end = str + str_len;
3315 hash = 0;
3316 for(; str < end; str++) {
3317 hash = (hash *263) + *str;
3318 }
3319 hash = hash & (HASH_TABLE_SIZE -1);
3320 return hash;
3321}
3322
3323static struct hash_entry *lookup(
3324 struct compile_state *state, const char *name, int name_len)
3325{
3326 struct hash_entry *entry;
3327 unsigned int index;
3328 index = hash(name, name_len);
3329 entry = state->hash_table[index];
Stefan Reinauer14e22772010-04-27 06:56:47 +00003330 while(entry &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003331 ((entry->name_len != name_len) ||
3332 (memcmp(entry->name, name, name_len) != 0))) {
3333 entry = entry->next;
3334 }
3335 if (!entry) {
3336 char *new_name;
3337 /* Get a private copy of the name */
3338 new_name = xmalloc(name_len + 1, "hash_name");
3339 memcpy(new_name, name, name_len);
3340 new_name[name_len] = '\0';
3341
3342 /* Create a new hash entry */
3343 entry = xcmalloc(sizeof(*entry), "hash_entry");
3344 entry->next = state->hash_table[index];
3345 entry->name = new_name;
3346 entry->name_len = name_len;
3347
3348 /* Place the new entry in the hash table */
3349 state->hash_table[index] = entry;
3350 }
3351 return entry;
3352}
3353
3354static void ident_to_keyword(struct compile_state *state, struct token *tk)
3355{
3356 struct hash_entry *entry;
3357 entry = tk->ident;
3358 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3359 (entry->tok == TOK_ENUM_CONST) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00003360 ((entry->tok >= TOK_FIRST_KEYWORD) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003361 (entry->tok <= TOK_LAST_KEYWORD)))) {
3362 tk->tok = entry->tok;
3363 }
3364}
3365
3366static void ident_to_macro(struct compile_state *state, struct token *tk)
3367{
3368 struct hash_entry *entry;
3369 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003370 if (!entry)
3371 return;
3372 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003373 tk->tok = entry->tok;
3374 }
Eric Biederman41203d92004-11-08 09:31:09 +00003375 else if (entry->tok == TOK_IF) {
3376 tk->tok = TOK_MIF;
3377 }
3378 else if (entry->tok == TOK_ELSE) {
3379 tk->tok = TOK_MELSE;
3380 }
3381 else {
3382 tk->tok = TOK_MIDENT;
3383 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003384}
3385
3386static void hash_keyword(
3387 struct compile_state *state, const char *keyword, int tok)
3388{
3389 struct hash_entry *entry;
3390 entry = lookup(state, keyword, strlen(keyword));
3391 if (entry && entry->tok != TOK_UNKNOWN) {
3392 die("keyword %s already hashed", keyword);
3393 }
3394 entry->tok = tok;
3395}
3396
Eric Biederman90089602004-05-28 14:11:54 +00003397static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003398 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003399 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003400{
3401 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003402 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003403 error(state, 0, "%s already defined", ident->name);
3404 }
3405 sym = xcmalloc(sizeof(*sym), "symbol");
3406 sym->ident = ident;
3407 sym->def = def;
3408 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003409 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003410 sym->next = *chain;
3411 *chain = sym;
3412}
3413
Eric Biederman90089602004-05-28 14:11:54 +00003414static void symbol(
3415 struct compile_state *state, struct hash_entry *ident,
3416 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003417{
Eric Biederman90089602004-05-28 14:11:54 +00003418 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3419}
3420
Stefan Reinauer14e22772010-04-27 06:56:47 +00003421static void var_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003422 struct hash_entry *ident, struct triple *def)
3423{
3424 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3425 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003426 }
Eric Biederman90089602004-05-28 14:11:54 +00003427 symbol(state, ident, &ident->sym_ident, def, def->type);
3428}
3429
Stefan Reinauer14e22772010-04-27 06:56:47 +00003430static void label_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003431 struct hash_entry *ident, struct triple *label, int depth)
3432{
3433 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003434}
3435
Eric Biedermanb138ac82003-04-22 18:44:01 +00003436static void start_scope(struct compile_state *state)
3437{
3438 state->scope_depth++;
3439}
3440
Eric Biederman90089602004-05-28 14:11:54 +00003441static void end_scope_syms(struct compile_state *state,
3442 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003443{
3444 struct symbol *sym, *next;
3445 sym = *chain;
3446 while(sym && (sym->scope_depth == depth)) {
3447 next = sym->next;
3448 xfree(sym);
3449 sym = next;
3450 }
3451 *chain = sym;
3452}
3453
3454static void end_scope(struct compile_state *state)
3455{
3456 int i;
3457 int depth;
3458 /* Walk through the hash table and remove all symbols
Stefan Reinauer14e22772010-04-27 06:56:47 +00003459 * in the current scope.
Eric Biedermanb138ac82003-04-22 18:44:01 +00003460 */
3461 depth = state->scope_depth;
3462 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3463 struct hash_entry *entry;
3464 entry = state->hash_table[i];
3465 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003466 end_scope_syms(state, &entry->sym_label, depth);
3467 end_scope_syms(state, &entry->sym_tag, depth);
3468 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003469 entry = entry->next;
3470 }
3471 }
3472 state->scope_depth = depth - 1;
3473}
3474
3475static void register_keywords(struct compile_state *state)
3476{
3477 hash_keyword(state, "auto", TOK_AUTO);
3478 hash_keyword(state, "break", TOK_BREAK);
3479 hash_keyword(state, "case", TOK_CASE);
3480 hash_keyword(state, "char", TOK_CHAR);
3481 hash_keyword(state, "const", TOK_CONST);
3482 hash_keyword(state, "continue", TOK_CONTINUE);
3483 hash_keyword(state, "default", TOK_DEFAULT);
3484 hash_keyword(state, "do", TOK_DO);
3485 hash_keyword(state, "double", TOK_DOUBLE);
3486 hash_keyword(state, "else", TOK_ELSE);
3487 hash_keyword(state, "enum", TOK_ENUM);
3488 hash_keyword(state, "extern", TOK_EXTERN);
3489 hash_keyword(state, "float", TOK_FLOAT);
3490 hash_keyword(state, "for", TOK_FOR);
3491 hash_keyword(state, "goto", TOK_GOTO);
3492 hash_keyword(state, "if", TOK_IF);
3493 hash_keyword(state, "inline", TOK_INLINE);
3494 hash_keyword(state, "int", TOK_INT);
3495 hash_keyword(state, "long", TOK_LONG);
3496 hash_keyword(state, "register", TOK_REGISTER);
3497 hash_keyword(state, "restrict", TOK_RESTRICT);
3498 hash_keyword(state, "return", TOK_RETURN);
3499 hash_keyword(state, "short", TOK_SHORT);
3500 hash_keyword(state, "signed", TOK_SIGNED);
3501 hash_keyword(state, "sizeof", TOK_SIZEOF);
3502 hash_keyword(state, "static", TOK_STATIC);
3503 hash_keyword(state, "struct", TOK_STRUCT);
3504 hash_keyword(state, "switch", TOK_SWITCH);
3505 hash_keyword(state, "typedef", TOK_TYPEDEF);
3506 hash_keyword(state, "union", TOK_UNION);
3507 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3508 hash_keyword(state, "void", TOK_VOID);
3509 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003510 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003511 hash_keyword(state, "while", TOK_WHILE);
3512 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003513 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003514 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3515 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3516}
3517
3518static void register_macro_keywords(struct compile_state *state)
3519{
Eric Biederman41203d92004-11-08 09:31:09 +00003520 hash_keyword(state, "define", TOK_MDEFINE);
3521 hash_keyword(state, "defined", TOK_MDEFINED);
3522 hash_keyword(state, "undef", TOK_MUNDEF);
3523 hash_keyword(state, "include", TOK_MINCLUDE);
3524 hash_keyword(state, "line", TOK_MLINE);
3525 hash_keyword(state, "error", TOK_MERROR);
3526 hash_keyword(state, "warning", TOK_MWARNING);
3527 hash_keyword(state, "pragma", TOK_MPRAGMA);
3528 hash_keyword(state, "ifdef", TOK_MIFDEF);
3529 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3530 hash_keyword(state, "elif", TOK_MELIF);
3531 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003532}
3533
Eric Biederman90089602004-05-28 14:11:54 +00003534
3535static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3536{
3537 if (ident->sym_define != 0) {
3538 struct macro *macro;
3539 struct macro_arg *arg, *anext;
3540 macro = ident->sym_define;
3541 ident->sym_define = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003542
Eric Biederman90089602004-05-28 14:11:54 +00003543 /* Free the macro arguments... */
3544 anext = macro->args;
3545 while(anext) {
3546 arg = anext;
3547 anext = arg->next;
3548 xfree(arg);
3549 }
3550
3551 /* Free the macro buffer */
3552 xfree(macro->buf);
3553
3554 /* Now free the macro itself */
3555 xfree(macro);
3556 }
3557}
3558
Stefan Reinauer14e22772010-04-27 06:56:47 +00003559static void do_define_macro(struct compile_state *state,
3560 struct hash_entry *ident, const char *body,
Eric Biedermancb364952004-11-15 10:46:44 +00003561 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003562{
3563 struct macro *macro;
3564 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003565 size_t body_len;
3566
3567 /* Find the length of the body */
3568 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003569 macro = ident->sym_define;
3570 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003571 int identical_bodies, identical_args;
3572 struct macro_arg *oarg;
3573 /* Explicitly allow identical redfinitions of the same macro */
Stefan Reinauer14e22772010-04-27 06:56:47 +00003574 identical_bodies =
Eric Biedermancb364952004-11-15 10:46:44 +00003575 (macro->buf_len == body_len) &&
3576 (memcmp(macro->buf, body, body_len) == 0);
3577 identical_args = macro->argc == argc;
3578 oarg = macro->args;
3579 arg = args;
3580 while(identical_args && arg) {
3581 identical_args = oarg->ident == arg->ident;
3582 arg = arg->next;
3583 oarg = oarg->next;
3584 }
3585 if (identical_bodies && identical_args) {
3586 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003587 return;
3588 }
3589 error(state, 0, "macro %s already defined\n", ident->name);
3590 }
3591#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003592 fprintf(state->errout, "#define %s: `%*.*s'\n",
3593 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003594#endif
3595 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003596 macro->ident = ident;
3597 macro->buf = body;
3598 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003599 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003600 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003601
3602 ident->sym_define = macro;
3603}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003604
Eric Biedermancb364952004-11-15 10:46:44 +00003605static void define_macro(
3606 struct compile_state *state,
3607 struct hash_entry *ident,
3608 const char *body, int body_len,
3609 int argc, struct macro_arg *args)
3610{
3611 char *buf;
3612 buf = xmalloc(body_len + 1, "macro buf");
3613 memcpy(buf, body, body_len);
3614 buf[body_len] = '\0';
3615 do_define_macro(state, ident, buf, argc, args);
3616}
Eric Biederman90089602004-05-28 14:11:54 +00003617
3618static void register_builtin_macro(struct compile_state *state,
3619 const char *name, const char *value)
3620{
3621 struct hash_entry *ident;
3622
3623 if (value[0] == '(') {
3624 internal_error(state, 0, "Builtin macros with arguments not supported");
3625 }
3626 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003627 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003628}
3629
3630static void register_builtin_macros(struct compile_state *state)
3631{
3632 char buf[30];
3633 char scratch[30];
3634 time_t now;
3635 struct tm *tm;
3636 now = time(NULL);
3637 tm = localtime(&now);
3638
3639 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3640 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3641 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3642 register_builtin_macro(state, "__LINE__", "54321");
3643
3644 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3645 sprintf(buf, "\"%s\"", scratch);
3646 register_builtin_macro(state, "__DATE__", buf);
3647
3648 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3649 sprintf(buf, "\"%s\"", scratch);
3650 register_builtin_macro(state, "__TIME__", buf);
3651
3652 /* I can't be a conforming implementation of C :( */
3653 register_builtin_macro(state, "__STDC__", "0");
3654 /* In particular I don't conform to C99 */
3655 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
Stefan Reinauer14e22772010-04-27 06:56:47 +00003656
Eric Biederman90089602004-05-28 14:11:54 +00003657}
3658
3659static void process_cmdline_macros(struct compile_state *state)
3660{
3661 const char **macro, *name;
3662 struct hash_entry *ident;
3663 for(macro = state->compiler->defines; (name = *macro); macro++) {
3664 const char *body;
3665 size_t name_len;
3666
3667 name_len = strlen(name);
3668 body = strchr(name, '=');
3669 if (!body) {
3670 body = "\0";
3671 } else {
3672 name_len = body - name;
3673 body++;
3674 }
3675 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003676 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003677 }
3678 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3679 ident = lookup(state, name, strlen(name));
3680 undef_macro(state, ident);
3681 }
3682}
3683
Eric Biedermanb138ac82003-04-22 18:44:01 +00003684static int spacep(int c)
3685{
3686 int ret = 0;
3687 switch(c) {
3688 case ' ':
3689 case '\t':
3690 case '\f':
3691 case '\v':
3692 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003693 ret = 1;
3694 break;
3695 }
3696 return ret;
3697}
3698
Eric Biedermanb138ac82003-04-22 18:44:01 +00003699static int digitp(int c)
3700{
3701 int ret = 0;
3702 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003703 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003704 case '5': case '6': case '7': case '8': case '9':
3705 ret = 1;
3706 break;
3707 }
3708 return ret;
3709}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003710static int digval(int c)
3711{
3712 int val = -1;
3713 if ((c >= '0') && (c <= '9')) {
3714 val = c - '0';
3715 }
3716 return val;
3717}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003718
3719static int hexdigitp(int c)
3720{
3721 int ret = 0;
3722 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003723 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003724 case '5': case '6': case '7': case '8': case '9':
3725 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3726 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3727 ret = 1;
3728 break;
3729 }
3730 return ret;
3731}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003732static int hexdigval(int c)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003733{
3734 int val = -1;
3735 if ((c >= '0') && (c <= '9')) {
3736 val = c - '0';
3737 }
3738 else if ((c >= 'A') && (c <= 'F')) {
3739 val = 10 + (c - 'A');
3740 }
3741 else if ((c >= 'a') && (c <= 'f')) {
3742 val = 10 + (c - 'a');
3743 }
3744 return val;
3745}
3746
3747static int octdigitp(int c)
3748{
3749 int ret = 0;
3750 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003751 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003752 case '4': case '5': case '6': case '7':
3753 ret = 1;
3754 break;
3755 }
3756 return ret;
3757}
3758static int octdigval(int c)
3759{
3760 int val = -1;
3761 if ((c >= '0') && (c <= '7')) {
3762 val = c - '0';
3763 }
3764 return val;
3765}
3766
3767static int letterp(int c)
3768{
3769 int ret = 0;
3770 switch(c) {
3771 case 'a': case 'b': case 'c': case 'd': case 'e':
3772 case 'f': case 'g': case 'h': case 'i': case 'j':
3773 case 'k': case 'l': case 'm': case 'n': case 'o':
3774 case 'p': case 'q': case 'r': case 's': case 't':
3775 case 'u': case 'v': case 'w': case 'x': case 'y':
3776 case 'z':
3777 case 'A': case 'B': case 'C': case 'D': case 'E':
3778 case 'F': case 'G': case 'H': case 'I': case 'J':
3779 case 'K': case 'L': case 'M': case 'N': case 'O':
3780 case 'P': case 'Q': case 'R': case 'S': case 'T':
3781 case 'U': case 'V': case 'W': case 'X': case 'Y':
3782 case 'Z':
3783 case '_':
3784 ret = 1;
3785 break;
3786 }
3787 return ret;
3788}
3789
Eric Biederman90089602004-05-28 14:11:54 +00003790static const char *identifier(const char *str, const char *end)
3791{
3792 if (letterp(*str)) {
3793 for(; str < end; str++) {
3794 int c;
3795 c = *str;
3796 if (!letterp(c) && !digitp(c)) {
3797 break;
3798 }
3799 }
3800 }
3801 return str;
3802}
3803
Eric Biedermanb138ac82003-04-22 18:44:01 +00003804static int char_value(struct compile_state *state,
3805 const signed char **strp, const signed char *end)
3806{
3807 const signed char *str;
3808 int c;
3809 str = *strp;
3810 c = *str++;
3811 if ((c == '\\') && (str < end)) {
3812 switch(*str) {
3813 case 'n': c = '\n'; str++; break;
3814 case 't': c = '\t'; str++; break;
3815 case 'v': c = '\v'; str++; break;
3816 case 'b': c = '\b'; str++; break;
3817 case 'r': c = '\r'; str++; break;
3818 case 'f': c = '\f'; str++; break;
3819 case 'a': c = '\a'; str++; break;
3820 case '\\': c = '\\'; str++; break;
3821 case '?': c = '?'; str++; break;
3822 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003823 case '"': c = '"'; str++; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003824 case 'x':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003825 c = 0;
3826 str++;
3827 while((str < end) && hexdigitp(*str)) {
3828 c <<= 4;
3829 c += hexdigval(*str);
3830 str++;
3831 }
3832 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003833 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003834 case '4': case '5': case '6': case '7':
3835 c = 0;
3836 while((str < end) && octdigitp(*str)) {
3837 c <<= 3;
3838 c += octdigval(*str);
3839 str++;
3840 }
3841 break;
3842 default:
3843 error(state, 0, "Invalid character constant");
3844 break;
3845 }
3846 }
3847 *strp = str;
3848 return c;
3849}
3850
Eric Biedermancb364952004-11-15 10:46:44 +00003851static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003852{
Eric Biedermancb364952004-11-15 10:46:44 +00003853 const char *end = file->buf + file->size;
3854 while(pos < end) {
3855 /* Lookup the character */
3856 int size = 1;
3857 int c = *pos;
3858 /* Is this a trigraph? */
3859 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003860 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003861 {
3862 switch(pos[2]) {
3863 case '=': c = '#'; break;
3864 case '/': c = '\\'; break;
3865 case '\'': c = '^'; break;
3866 case '(': c = '['; break;
3867 case ')': c = ']'; break;
3868 case '!': c = '!'; break;
3869 case '<': c = '{'; break;
3870 case '>': c = '}'; break;
3871 case '-': c = '~'; break;
3872 }
3873 if (c != '?') {
3874 size = 3;
3875 }
3876 }
3877 /* Is this an escaped newline? */
3878 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003879 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003880 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003881 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003882 /* At the start of a line just eat it */
3883 if (pos == file->pos) {
3884 file->line++;
3885 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003886 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003887 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003888 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003889 }
3890 /* Do I need to ga any farther? */
3891 else if (index == 0) {
3892 break;
3893 }
3894 /* Process a normal character */
3895 else {
3896 pos += size;
3897 index -= 1;
3898 }
3899 }
3900 return pos;
3901}
3902
3903static int get_char(struct file_state *file, const char *pos)
3904{
3905 const char *end = file->buf + file->size;
3906 int c;
3907 c = -1;
3908 pos = next_char(file, pos, 0);
3909 if (pos < end) {
3910 /* Lookup the character */
3911 c = *pos;
3912 /* If it is a trigraph get the trigraph value */
3913 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003914 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003915 {
3916 switch(pos[2]) {
3917 case '=': c = '#'; break;
3918 case '/': c = '\\'; break;
3919 case '\'': c = '^'; break;
3920 case '(': c = '['; break;
3921 case ')': c = ']'; break;
3922 case '!': c = '!'; break;
3923 case '<': c = '{'; break;
3924 case '>': c = '}'; break;
3925 case '-': c = '~'; break;
3926 }
3927 }
3928 }
3929 return c;
3930}
3931
3932static void eat_chars(struct file_state *file, const char *targ)
3933{
3934 const char *pos = file->pos;
3935 while(pos < targ) {
3936 /* Do we have a newline? */
3937 if (pos[0] == '\n') {
3938 file->line++;
3939 file->report_line++;
3940 file->line_start = pos + 1;
3941 }
3942 pos++;
3943 }
3944 file->pos = pos;
3945}
3946
3947
3948static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3949{
3950 size_t len;
3951 len = 0;
3952 while(src < end) {
3953 src = next_char(file, src, 1);
3954 len++;
3955 }
3956 return len;
3957}
3958
Stefan Reinauer14e22772010-04-27 06:56:47 +00003959static void char_strcpy(char *dest,
Eric Biedermancb364952004-11-15 10:46:44 +00003960 struct file_state *file, const char *src, const char *end)
3961{
3962 while(src < end) {
3963 int c;
3964 c = get_char(file, src);
3965 src = next_char(file, src, 1);
3966 *dest++ = c;
3967 }
3968}
3969
Stefan Reinauer14e22772010-04-27 06:56:47 +00003970static char *char_strdup(struct file_state *file,
Eric Biedermancb364952004-11-15 10:46:44 +00003971 const char *start, const char *end, const char *id)
3972{
3973 char *str;
3974 size_t str_len;
3975 str_len = char_strlen(file, start, end);
3976 str = xcmalloc(str_len + 1, id);
3977 char_strcpy(str, file, start, end);
3978 str[str_len] = '\0';
3979 return str;
3980}
3981
3982static const char *after_digits(struct file_state *file, const char *ptr)
3983{
3984 while(digitp(get_char(file, ptr))) {
3985 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003986 }
3987 return ptr;
3988}
3989
Eric Biedermancb364952004-11-15 10:46:44 +00003990static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003991{
Eric Biedermancb364952004-11-15 10:46:44 +00003992 while(octdigitp(get_char(file, ptr))) {
3993 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003994 }
3995 return ptr;
3996}
3997
Eric Biedermancb364952004-11-15 10:46:44 +00003998static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003999{
Eric Biedermancb364952004-11-15 10:46:44 +00004000 while(hexdigitp(get_char(file, ptr))) {
4001 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004002 }
4003 return ptr;
4004}
4005
Eric Biedermancb364952004-11-15 10:46:44 +00004006static const char *after_alnums(struct file_state *file, const char *ptr)
4007{
4008 int c;
4009 c = get_char(file, ptr);
4010 while(letterp(c) || digitp(c)) {
4011 ptr = next_char(file, ptr, 1);
4012 c = get_char(file, ptr);
4013 }
4014 return ptr;
4015}
4016
4017static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004018 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004019{
4020 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004021
Eric Biedermanb138ac82003-04-22 18:44:01 +00004022 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004023 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004024
4025 /* Store the copy in the token */
4026 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004027 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004028}
4029
Stefan Reinauer14e22772010-04-27 06:56:47 +00004030static void raw_next_token(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004031 struct file_state *file, struct token *tk)
4032{
4033 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004034 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004035 const char *tokp;
4036 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004037 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004038
Eric Biedermanb138ac82003-04-22 18:44:01 +00004039 tk->str_len = 0;
4040 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004041 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004042 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004043 c = get_char(file, tokp);
4044 tokp = next_char(file, tokp, 1);
4045 eat = 0;
4046 c1 = get_char(file, tokp);
4047 c2 = get_char(file, next_char(file, tokp, 1));
4048 c3 = get_char(file, next_char(file, tokp, 2));
4049
4050 /* The end of the file */
4051 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004052 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004053 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004054 /* Whitespace */
4055 else if (spacep(c)) {
4056 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004057 while (spacep(get_char(file, tokp))) {
4058 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004059 }
4060 }
4061 /* EOL Comments */
4062 else if ((c == '/') && (c1 == '/')) {
4063 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004064 tokp = next_char(file, tokp, 1);
4065 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004066 /* Advance to the next character only after we verify
Stefan Reinauer14e22772010-04-27 06:56:47 +00004067 * the current character is not a newline.
Eric Biederman57183382006-12-02 16:48:48 +00004068 * EOL is special to the preprocessor so we don't
4069 * want to loose any.
4070 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004071 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004072 break;
4073 }
Eric Biederman57183382006-12-02 16:48:48 +00004074 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004075 }
4076 }
4077 /* Comments */
4078 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004079 tokp = next_char(file, tokp, 2);
4080 c = c2;
4081 while((c1 = get_char(file, tokp)) != -1) {
4082 tokp = next_char(file, tokp, 1);
4083 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004084 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004085 break;
4086 }
Eric Biedermancb364952004-11-15 10:46:44 +00004087 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004088 }
4089 if (tok == TOK_UNKNOWN) {
4090 error(state, 0, "unterminated comment");
4091 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004092 }
4093 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004094 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004095 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004096
Eric Biedermancb364952004-11-15 10:46:44 +00004097 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004098 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004099 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004100 }
Eric Biedermancb364952004-11-15 10:46:44 +00004101 while((c = get_char(file, tokp)) != -1) {
4102 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004103 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004104 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004105 }
Eric Biedermancb364952004-11-15 10:46:44 +00004106 else if (c == '\\') {
4107 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004108 }
4109 else if (c == '"') {
4110 tok = TOK_LIT_STRING;
4111 break;
4112 }
4113 }
4114 if (tok == TOK_UNKNOWN) {
4115 error(state, 0, "unterminated string constant");
4116 }
Eric Biedermancb364952004-11-15 10:46:44 +00004117 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004118 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004119 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004120
4121 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004122 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004123 }
4124 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004125 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004126 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004127
Eric Biedermancb364952004-11-15 10:46:44 +00004128 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004129 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004130 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004131 }
Eric Biedermancb364952004-11-15 10:46:44 +00004132 while((c = get_char(file, tokp)) != -1) {
4133 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004134 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004135 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004136 }
Eric Biedermancb364952004-11-15 10:46:44 +00004137 else if (c == '\\') {
4138 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004139 }
4140 else if (c == '\'') {
4141 tok = TOK_LIT_CHAR;
4142 break;
4143 }
4144 }
4145 if (tok == TOK_UNKNOWN) {
4146 error(state, 0, "unterminated character constant");
4147 }
Eric Biedermancb364952004-11-15 10:46:44 +00004148 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004149 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004151
4152 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004153 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004154 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004155 /* integer and floating constants
Eric Biedermanb138ac82003-04-22 18:44:01 +00004156 * Integer Constants
4157 * {digits}
4158 * 0[Xx]{hexdigits}
4159 * 0{octdigit}+
Stefan Reinauer14e22772010-04-27 06:56:47 +00004160 *
Eric Biedermanb138ac82003-04-22 18:44:01 +00004161 * Floating constants
4162 * {digits}.{digits}[Ee][+-]?{digits}
4163 * {digits}.{digits}
4164 * {digits}[Ee][+-]?{digits}
4165 * .{digits}[Ee][+-]?{digits}
4166 * .{digits}
4167 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004168 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004169 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004170 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004171 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004172 is_float = 0;
4173 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004174 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004175 }
4176 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004177 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004178 }
Eric Biedermancb364952004-11-15 10:46:44 +00004179 cn = get_char(file, next);
4180 if (cn == '.') {
4181 next = next_char(file, next, 1);
4182 next = after_digits(file, next);
4183 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004184 }
Eric Biedermancb364952004-11-15 10:46:44 +00004185 cn = get_char(file, next);
4186 if ((cn == 'e') || (cn == 'E')) {
4187 const char *new;
4188 next = next_char(file, next, 1);
4189 cn = get_char(file, next);
4190 if ((cn == '+') || (cn == '-')) {
4191 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004192 }
Eric Biedermancb364952004-11-15 10:46:44 +00004193 new = after_digits(file, next);
4194 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004195 next = new;
4196 }
4197 if (is_float) {
4198 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004199 cn = get_char(file, next);
4200 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4201 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004202 }
4203 }
4204 if (!is_float && digitp(c)) {
4205 tok = TOK_LIT_INT;
4206 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004207 next = next_char(file, tokp, 1);
4208 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004209 }
4210 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004211 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004212 }
4213 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004214 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004215 }
4216 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004217 cn = get_char(file, next);
4218 if ((cn == 'u') || (cn == 'U')) {
4219 next = next_char(file, next, 1);
4220 cn = get_char(file, next);
4221 if ((cn == 'l') || (cn == 'L')) {
4222 next = next_char(file, next, 1);
4223 cn = get_char(file, next);
4224 }
4225 if ((cn == 'l') || (cn == 'L')) {
4226 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004227 }
4228 }
Eric Biedermancb364952004-11-15 10:46:44 +00004229 else if ((cn == 'l') || (cn == 'L')) {
4230 next = next_char(file, next, 1);
4231 cn = get_char(file, next);
4232 if ((cn == 'l') || (cn == 'L')) {
4233 next = next_char(file, next, 1);
4234 cn = get_char(file, next);
4235 }
4236 if ((cn == 'u') || (cn == 'U')) {
4237 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004238 }
4239 }
4240 }
Eric Biedermancb364952004-11-15 10:46:44 +00004241 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004242
4243 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004244 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004245 }
4246 /* identifiers */
4247 else if (letterp(c)) {
4248 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004249
4250 /* Find and save the identifier string */
4251 tokp = after_alnums(file, tokp);
4252 save_string(file, tk, token, tokp, "identifier");
4253
4254 /* Look up to see which identifier it is */
4255 tk->ident = lookup(state, tk->val.str, tk->str_len);
4256
4257 /* Free the identifier string */
4258 tk->str_len = 0;
4259 xfree(tk->val.str);
4260
Eric Biederman90089602004-05-28 14:11:54 +00004261 /* See if this identifier can be macro expanded */
4262 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004263 c = get_char(file, tokp);
4264 if (c == '$') {
4265 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004266 tk->val.notmacro = 1;
4267 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004268 }
4269 /* C99 alternate macro characters */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004270 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004271 eat += 3;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004272 tok = TOK_CONCATENATE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004273 }
Eric Biedermancb364952004-11-15 10:46:44 +00004274 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4275 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4276 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4277 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4278 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4279 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4280 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4281 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4282 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4283 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4284 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4285 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4286 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4287 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4288 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4289 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4290 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4291 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4292 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4293 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4294 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4295 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4296 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4297 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4298 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4299 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4300 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4301 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004302 else if (c == ';') { tok = TOK_SEMI; }
4303 else if (c == '{') { tok = TOK_LBRACE; }
4304 else if (c == '}') { tok = TOK_RBRACE; }
4305 else if (c == ',') { tok = TOK_COMMA; }
4306 else if (c == '=') { tok = TOK_EQ; }
4307 else if (c == ':') { tok = TOK_COLON; }
4308 else if (c == '[') { tok = TOK_LBRACKET; }
4309 else if (c == ']') { tok = TOK_RBRACKET; }
4310 else if (c == '(') { tok = TOK_LPAREN; }
4311 else if (c == ')') { tok = TOK_RPAREN; }
4312 else if (c == '*') { tok = TOK_STAR; }
4313 else if (c == '>') { tok = TOK_MORE; }
4314 else if (c == '<') { tok = TOK_LESS; }
4315 else if (c == '?') { tok = TOK_QUEST; }
4316 else if (c == '|') { tok = TOK_OR; }
4317 else if (c == '&') { tok = TOK_AND; }
4318 else if (c == '^') { tok = TOK_XOR; }
4319 else if (c == '+') { tok = TOK_PLUS; }
4320 else if (c == '-') { tok = TOK_MINUS; }
4321 else if (c == '/') { tok = TOK_DIV; }
4322 else if (c == '%') { tok = TOK_MOD; }
4323 else if (c == '!') { tok = TOK_BANG; }
4324 else if (c == '.') { tok = TOK_DOT; }
4325 else if (c == '~') { tok = TOK_TILDE; }
4326 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004327 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004328
Eric Biedermancb364952004-11-15 10:46:44 +00004329 tokp = next_char(file, tokp, eat);
4330 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004331 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004332 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004333}
4334
4335static void check_tok(struct compile_state *state, struct token *tk, int tok)
4336{
4337 if (tk->tok != tok) {
4338 const char *name1, *name2;
4339 name1 = tokens[tk->tok];
4340 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004341 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004342 name2 = tk->ident->name;
4343 }
4344 error(state, 0, "\tfound %s %s expected %s",
4345 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004346 }
4347}
4348
Eric Biederman90089602004-05-28 14:11:54 +00004349struct macro_arg_value {
4350 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004351 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004352 size_t len;
4353};
4354static struct macro_arg_value *read_macro_args(
Stefan Reinauer14e22772010-04-27 06:56:47 +00004355 struct compile_state *state, struct macro *macro,
Eric Biederman90089602004-05-28 14:11:54 +00004356 struct file_state *file, struct token *tk)
4357{
4358 struct macro_arg_value *argv;
4359 struct macro_arg *arg;
4360 int paren_depth;
4361 int i;
4362
4363 if (macro->argc == 0) {
4364 do {
4365 raw_next_token(state, file, tk);
4366 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004367 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004368 }
4369 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4370 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4371 argv[i].value = 0;
4372 argv[i].len = 0;
4373 argv[i].ident = arg->ident;
4374 }
4375 paren_depth = 0;
4376 i = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004377
Eric Biederman90089602004-05-28 14:11:54 +00004378 for(;;) {
4379 const char *start;
4380 size_t len;
4381 start = file->pos;
4382 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004383
Eric Biederman90089602004-05-28 14:11:54 +00004384 if (!paren_depth && (tk->tok == TOK_COMMA) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004385 (argv[i].ident != state->i___VA_ARGS__))
Eric Biederman90089602004-05-28 14:11:54 +00004386 {
4387 i++;
4388 if (i >= macro->argc) {
4389 error(state, 0, "too many args to %s\n",
4390 macro->ident->name);
4391 }
4392 continue;
4393 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004394
Eric Biederman90089602004-05-28 14:11:54 +00004395 if (tk->tok == TOK_LPAREN) {
4396 paren_depth++;
4397 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004398
Eric Biederman90089602004-05-28 14:11:54 +00004399 if (tk->tok == TOK_RPAREN) {
4400 if (paren_depth == 0) {
4401 break;
4402 }
4403 paren_depth--;
4404 }
4405 if (tk->tok == TOK_EOF) {
4406 error(state, 0, "End of file encountered while parsing macro arguments");
4407 }
Eric Biedermancb364952004-11-15 10:46:44 +00004408
4409 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004410 argv[i].value = xrealloc(
4411 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004412 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004413 argv[i].len += len;
4414 }
4415 if (i != macro->argc -1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00004416 error(state, 0, "missing %s arg %d\n",
Eric Biederman90089602004-05-28 14:11:54 +00004417 macro->ident->name, i +2);
4418 }
4419 return argv;
4420}
4421
4422
4423static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4424{
4425 int i;
4426 for(i = 0; i < macro->argc; i++) {
4427 xfree(argv[i].value);
4428 }
4429 xfree(argv);
4430}
4431
4432struct macro_buf {
4433 char *str;
4434 size_t len, pos;
4435};
4436
Eric Biedermancb364952004-11-15 10:46:44 +00004437static void grow_macro_buf(struct compile_state *state,
4438 const char *id, struct macro_buf *buf,
4439 size_t grow)
4440{
4441 if ((buf->pos + grow) >= buf->len) {
4442 buf->str = xrealloc(buf->str, buf->len + grow, id);
4443 buf->len += grow;
4444 }
4445}
4446
Eric Biederman90089602004-05-28 14:11:54 +00004447static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004448 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004449 const char *fstart, size_t flen)
4450{
Eric Biedermancb364952004-11-15 10:46:44 +00004451 grow_macro_buf(state, id, buf, flen);
4452 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004453#if 0
4454 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4455 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004456 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004457#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004458 buf->pos += flen;
4459}
4460
4461
4462static void append_macro_chars(struct compile_state *state,
4463 const char *id, struct macro_buf *buf,
4464 struct file_state *file, const char *start, const char *end)
4465{
4466 size_t flen;
4467 flen = char_strlen(file, start, end);
4468 grow_macro_buf(state, id, buf, flen);
4469 char_strcpy(buf->str + buf->pos, file, start, end);
4470#if 0
4471 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4472 buf->pos, buf->pos, buf->str,
4473 flen, flen, buf->str + buf->pos);
4474#endif
Eric Biederman90089602004-05-28 14:11:54 +00004475 buf->pos += flen;
4476}
4477
Stefan Reinauer14e22772010-04-27 06:56:47 +00004478static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004479 struct file_state **filep, struct token *tk);
4480
Stefan Reinauer14e22772010-04-27 06:56:47 +00004481static void macro_expand_args(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004482 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4483{
Eric Biedermancb364952004-11-15 10:46:44 +00004484 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004485
Eric Biederman90089602004-05-28 14:11:54 +00004486 for(i = 0; i < macro->argc; i++) {
4487 struct file_state fmacro, *file;
4488 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004489
Eric Biedermancb364952004-11-15 10:46:44 +00004490 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004491 fmacro.basename = argv[i].ident->name;
4492 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004493 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004494 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004495 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004496 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004497 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004498 fmacro.report_line = 1;
4499 fmacro.report_name = fmacro.basename;
4500 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004501 fmacro.macro = 1;
4502 fmacro.trigraphs = 0;
4503 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004504
4505 buf.len = argv[i].len;
4506 buf.str = xmalloc(buf.len, argv[i].ident->name);
4507 buf.pos = 0;
4508
4509 file = &fmacro;
4510 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004511 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004512
Eric Biedermancb364952004-11-15 10:46:44 +00004513 /* If we have recursed into another macro body
4514 * get out of it.
4515 */
Eric Biederman90089602004-05-28 14:11:54 +00004516 if (tk->tok == TOK_EOF) {
4517 struct file_state *old;
4518 old = file;
4519 file = file->prev;
4520 if (!file) {
4521 break;
4522 }
4523 /* old->basename is used keep it */
4524 xfree(old->dirname);
4525 xfree(old->buf);
4526 xfree(old);
4527 continue;
4528 }
4529 else if (tk->ident && tk->ident->sym_define) {
4530 if (compile_macro(state, &file, tk)) {
4531 continue;
4532 }
4533 }
4534
Eric Biedermancb364952004-11-15 10:46:44 +00004535 append_macro_chars(state, macro->ident->name, &buf,
4536 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004537 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004538
Eric Biederman90089602004-05-28 14:11:54 +00004539 xfree(argv[i].value);
4540 argv[i].value = buf.str;
4541 argv[i].len = buf.pos;
4542 }
4543 return;
4544}
4545
4546static void expand_macro(struct compile_state *state,
4547 struct macro *macro, struct macro_buf *buf,
4548 struct macro_arg_value *argv, struct token *tk)
4549{
4550 struct file_state fmacro;
4551 const char space[] = " ";
4552 const char *fstart;
4553 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004554 int i, j;
4555
4556 /* Place the macro body in a dummy file */
4557 fmacro.prev = 0;
4558 fmacro.basename = macro->ident->name;
4559 fmacro.dirname = "";
4560 fmacro.buf = macro->buf;
4561 fmacro.size = macro->buf_len;
4562 fmacro.pos = fmacro.buf;
4563 fmacro.line = 1;
4564 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004565 fmacro.report_line = 1;
4566 fmacro.report_name = fmacro.basename;
4567 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004568 fmacro.macro = 1;
4569 fmacro.trigraphs = 0;
4570 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004571
Eric Biedermancb364952004-11-15 10:46:44 +00004572 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004573 buf->len = macro->buf_len + 3;
4574 buf->str = xmalloc(buf->len, macro->ident->name);
4575 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004576
Eric Biederman90089602004-05-28 14:11:54 +00004577 fstart = fmacro.pos;
4578 raw_next_token(state, &fmacro, tk);
4579 while(tk->tok != TOK_EOF) {
4580 flen = fmacro.pos - fstart;
4581 switch(tk->tok) {
4582 case TOK_IDENT:
Patrick Georgid8051892017-01-02 19:15:44 +01004583 if (macro->argc < 0) {
4584 break;
4585 }
Eric Biederman90089602004-05-28 14:11:54 +00004586 for(i = 0; i < macro->argc; i++) {
4587 if (argv[i].ident == tk->ident) {
4588 break;
4589 }
4590 }
4591 if (i >= macro->argc) {
4592 break;
4593 }
4594 /* Substitute macro parameter */
4595 fstart = argv[i].value;
4596 flen = argv[i].len;
4597 break;
4598 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004599 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004600 break;
4601 }
4602 do {
4603 raw_next_token(state, &fmacro, tk);
4604 } while(tk->tok == TOK_SPACE);
4605 check_tok(state, tk, TOK_IDENT);
4606 for(i = 0; i < macro->argc; i++) {
4607 if (argv[i].ident == tk->ident) {
4608 break;
4609 }
4610 }
4611 if (i >= macro->argc) {
4612 error(state, 0, "parameter `%s' not found",
4613 tk->ident->name);
4614 }
4615 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004616 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004617 for(j = 0; j < argv[i].len; j++) {
4618 char *str = argv[i].value + j;
4619 size_t len = 1;
4620 if (*str == '\\') {
4621 str = "\\";
4622 len = 2;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004623 }
Eric Biederman90089602004-05-28 14:11:54 +00004624 else if (*str == '"') {
4625 str = "\\\"";
4626 len = 2;
4627 }
Eric Biedermancb364952004-11-15 10:46:44 +00004628 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004629 }
Eric Biedermancb364952004-11-15 10:46:44 +00004630 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004631 fstart = 0;
4632 flen = 0;
4633 break;
4634 case TOK_CONCATENATE:
4635 /* Concatenate tokens */
4636 /* Delete the previous whitespace token */
4637 if (buf->str[buf->pos - 1] == ' ') {
4638 buf->pos -= 1;
4639 }
4640 /* Skip the next sequence of whitspace tokens */
4641 do {
4642 fstart = fmacro.pos;
4643 raw_next_token(state, &fmacro, tk);
4644 } while(tk->tok == TOK_SPACE);
4645 /* Restart at the top of the loop.
4646 * I need to process the non white space token.
4647 */
4648 continue;
4649 break;
4650 case TOK_SPACE:
4651 /* Collapse multiple spaces into one */
4652 if (buf->str[buf->pos - 1] != ' ') {
4653 fstart = space;
4654 flen = 1;
4655 } else {
4656 fstart = 0;
4657 flen = 0;
4658 }
4659 break;
4660 default:
4661 break;
4662 }
4663
Eric Biedermancb364952004-11-15 10:46:44 +00004664 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004665
Eric Biederman90089602004-05-28 14:11:54 +00004666 fstart = fmacro.pos;
4667 raw_next_token(state, &fmacro, tk);
4668 }
4669}
4670
4671static void tag_macro_name(struct compile_state *state,
4672 struct macro *macro, struct macro_buf *buf,
4673 struct token *tk)
4674{
4675 /* Guard all instances of the macro name in the replacement
4676 * text from further macro expansion.
4677 */
4678 struct file_state fmacro;
4679 const char *fstart;
4680 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004681
4682 /* Put the old macro expansion buffer in a file */
4683 fmacro.prev = 0;
4684 fmacro.basename = macro->ident->name;
4685 fmacro.dirname = "";
4686 fmacro.buf = buf->str;
4687 fmacro.size = buf->pos;
4688 fmacro.pos = fmacro.buf;
4689 fmacro.line = 1;
4690 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004691 fmacro.report_line = 1;
4692 fmacro.report_name = fmacro.basename;
4693 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004694 fmacro.macro = 1;
4695 fmacro.trigraphs = 0;
4696 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004697
Eric Biedermancb364952004-11-15 10:46:44 +00004698 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004699 buf->len = macro->buf_len + 3;
4700 buf->str = xmalloc(buf->len, macro->ident->name);
4701 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004702
Eric Biederman90089602004-05-28 14:11:54 +00004703 fstart = fmacro.pos;
4704 raw_next_token(state, &fmacro, tk);
4705 while(tk->tok != TOK_EOF) {
4706 flen = fmacro.pos - fstart;
4707 if ((tk->tok == TOK_IDENT) &&
4708 (tk->ident == macro->ident) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004709 (tk->val.notmacro == 0))
Eric Biedermancb364952004-11-15 10:46:44 +00004710 {
4711 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004712 fstart = "$";
4713 flen = 1;
4714 }
4715
Eric Biedermancb364952004-11-15 10:46:44 +00004716 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004717
Eric Biederman90089602004-05-28 14:11:54 +00004718 fstart = fmacro.pos;
4719 raw_next_token(state, &fmacro, tk);
4720 }
4721 xfree(fmacro.buf);
4722}
Eric Biedermancb364952004-11-15 10:46:44 +00004723
Stefan Reinauer14e22772010-04-27 06:56:47 +00004724static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004725 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004726{
4727 struct file_state *file;
4728 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004729 struct macro *macro;
4730 struct macro_arg_value *argv;
4731 struct macro_buf buf;
4732
4733#if 0
4734 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4735#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004736 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004737 macro = ident->sym_define;
4738
4739 /* If this token comes from a macro expansion ignore it */
4740 if (tk->val.notmacro) {
4741 return 0;
4742 }
4743 /* If I am a function like macro and the identifier is not followed
4744 * by a left parenthesis, do nothing.
4745 */
Eric Biedermancb364952004-11-15 10:46:44 +00004746 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004747 return 0;
4748 }
4749
4750 /* Read in the macro arguments */
4751 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004752 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004753 raw_next_token(state, *filep, tk);
4754 check_tok(state, tk, TOK_LPAREN);
4755
4756 argv = read_macro_args(state, macro, *filep, tk);
4757
4758 check_tok(state, tk, TOK_RPAREN);
4759 }
4760 /* Macro expand the macro arguments */
4761 macro_expand_args(state, macro, argv, tk);
4762
4763 buf.str = 0;
4764 buf.len = 0;
4765 buf.pos = 0;
4766 if (ident == state->i___FILE__) {
4767 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4768 buf.str = xmalloc(buf.len, ident->name);
4769 sprintf(buf.str, "\"%s\"", state->file->basename);
4770 buf.pos = strlen(buf.str);
4771 }
4772 else if (ident == state->i___LINE__) {
4773 buf.len = 30;
4774 buf.str = xmalloc(buf.len, ident->name);
4775 sprintf(buf.str, "%d", state->file->line);
4776 buf.pos = strlen(buf.str);
4777 }
4778 else {
4779 expand_macro(state, macro, &buf, argv, tk);
4780 }
4781 /* Tag the macro name with a $ so it will no longer
4782 * be regonized as a canidate for macro expansion.
4783 */
4784 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004785
4786#if 0
4787 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4788 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4789#endif
4790
4791 free_macro_args(macro, argv);
4792
Eric Biedermanb138ac82003-04-22 18:44:01 +00004793 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004794 file->prev = *filep;
4795 file->basename = xstrdup(ident->name);
4796 file->dirname = xstrdup("");
4797 file->buf = buf.str;
4798 file->size = buf.pos;
4799 file->pos = file->buf;
4800 file->line = 1;
4801 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004802 file->report_line = 1;
4803 file->report_name = file->basename;
4804 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004805 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004806 file->trigraphs = 0;
4807 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004808 *filep = file;
4809 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004810}
4811
Eric Biederman90089602004-05-28 14:11:54 +00004812static void eat_tokens(struct compile_state *state, int targ_tok)
4813{
4814 if (state->eat_depth > 0) {
4815 internal_error(state, 0, "Already eating...");
4816 }
4817 state->eat_depth = state->if_depth;
4818 state->eat_targ = targ_tok;
4819}
4820static int if_eat(struct compile_state *state)
4821{
4822 return state->eat_depth > 0;
4823}
4824static int if_value(struct compile_state *state)
4825{
4826 int index, offset;
4827 index = state->if_depth / CHAR_BIT;
4828 offset = state->if_depth % CHAR_BIT;
4829 return !!(state->if_bytes[index] & (1 << (offset)));
4830}
Stefan Reinauer14e22772010-04-27 06:56:47 +00004831static void set_if_value(struct compile_state *state, int value)
Eric Biederman90089602004-05-28 14:11:54 +00004832{
4833 int index, offset;
4834 index = state->if_depth / CHAR_BIT;
4835 offset = state->if_depth % CHAR_BIT;
4836
4837 state->if_bytes[index] &= ~(1 << offset);
4838 if (value) {
4839 state->if_bytes[index] |= (1 << offset);
4840 }
4841}
4842static void in_if(struct compile_state *state, const char *name)
4843{
4844 if (state->if_depth <= 0) {
4845 error(state, 0, "%s without #if", name);
4846 }
4847}
4848static void enter_if(struct compile_state *state)
4849{
4850 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004851 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004852 error(state, 0, "#if depth too great");
4853 }
4854}
4855static void reenter_if(struct compile_state *state, const char *name)
4856{
4857 in_if(state, name);
4858 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004859 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004860 state->eat_depth = 0;
4861 state->eat_targ = 0;
4862 }
4863}
4864static void enter_else(struct compile_state *state, const char *name)
4865{
4866 in_if(state, name);
4867 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004868 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004869 state->eat_depth = 0;
4870 state->eat_targ = 0;
4871 }
4872}
4873static void exit_if(struct compile_state *state, const char *name)
4874{
4875 in_if(state, name);
4876 if (state->eat_depth == state->if_depth) {
4877 state->eat_depth = 0;
4878 state->eat_targ = 0;
4879 }
4880 state->if_depth -= 1;
4881}
4882
Eric Biedermancb364952004-11-15 10:46:44 +00004883static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004884{
4885 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004886 int rescan;
4887
Eric Biedermancb364952004-11-15 10:46:44 +00004888 file = state->file;
4889 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004890 do {
4891 rescan = 0;
4892 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004893 /* Exit out of an include directive or macro call */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004894 if ((tk->tok == TOK_EOF) &&
4895 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004896 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004897 state->file = file->prev;
4898 /* file->basename is used keep it */
4899 xfree(file->dirname);
4900 xfree(file->buf);
4901 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004902 file = 0;
4903 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004904 rescan = 1;
4905 }
Eric Biederman41203d92004-11-08 09:31:09 +00004906 } while(rescan);
4907}
4908
Eric Biedermancb364952004-11-15 10:46:44 +00004909static void pp_token(struct compile_state *state, struct token *tk)
4910{
Eric Biedermancb364952004-11-15 10:46:44 +00004911 int rescan;
4912
4913 raw_token(state, tk);
4914 do {
4915 rescan = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004916 if (tk->tok == TOK_SPACE) {
4917 raw_token(state, tk);
4918 rescan = 1;
4919 }
4920 else if (tk->tok == TOK_IDENT) {
4921 if (state->token_base == 0) {
4922 ident_to_keyword(state, tk);
4923 } else {
4924 ident_to_macro(state, tk);
4925 }
4926 }
4927 } while(rescan);
4928}
4929
Eric Biederman41203d92004-11-08 09:31:09 +00004930static void preprocess(struct compile_state *state, struct token *tk);
4931
4932static void token(struct compile_state *state, struct token *tk)
4933{
4934 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004935 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004936 do {
4937 rescan = 0;
4938 /* Process a macro directive */
4939 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004940 /* Only match preprocessor directives at the start of a line */
4941 const char *ptr;
4942 ptr = state->file->line_start;
4943 while((ptr < tk->pos)
4944 && spacep(get_char(state->file, ptr)))
4945 {
4946 ptr = next_char(state->file, ptr, 1);
4947 }
4948 if (ptr == tk->pos) {
4949 preprocess(state, tk);
4950 rescan = 1;
4951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004952 }
Eric Biederman41203d92004-11-08 09:31:09 +00004953 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004954 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004955 rescan = compile_macro(state, &state->file, tk);
4956 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004957 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004958 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004959 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004960 /* Eat tokens disabled by the preprocessor
4961 * (Unless we are parsing a preprocessor directive
Eric Biedermancb364952004-11-15 10:46:44 +00004962 */
Eric Biedermana649a412004-11-09 00:35:39 +00004963 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004964 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004965 rescan = 1;
4966 }
Eric Biedermana649a412004-11-09 00:35:39 +00004967 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004968 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004969 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004970 rescan = 1;
4971 }
Eric Biedermancb364952004-11-15 10:46:44 +00004972 /* Error on unknown tokens */
4973 else if (tk->tok == TOK_UNKNOWN) {
4974 error(state, 0, "unknown token");
4975 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004976 } while(rescan);
4977}
4978
Eric Biederman41203d92004-11-08 09:31:09 +00004979
4980static inline struct token *get_token(struct compile_state *state, int offset)
4981{
4982 int index;
4983 index = state->token_base + offset;
4984 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4985 internal_error(state, 0, "token array to small");
4986 }
4987 return &state->token[index];
4988}
4989
4990static struct token *do_eat_token(struct compile_state *state, int tok)
4991{
4992 struct token *tk;
4993 int i;
4994 check_tok(state, get_token(state, 1), tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004995
Eric Biederman41203d92004-11-08 09:31:09 +00004996 /* Free the old token value */
4997 tk = get_token(state, 0);
4998 if (tk->str_len) {
4999 memset((void *)tk->val.str, -1, tk->str_len);
5000 xfree(tk->val.str);
5001 }
5002 /* Overwrite the old token with newer tokens */
5003 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
5004 state->token[i] = state->token[i + 1];
5005 }
5006 /* Clear the last token */
5007 memset(&state->token[i], 0, sizeof(state->token[i]));
5008 state->token[i].tok = -1;
5009
5010 /* Return the token */
5011 return tk;
5012}
5013
Eric Biedermancb364952004-11-15 10:46:44 +00005014static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005015{
5016 struct token *tk1;
5017 tk1 = get_token(state, 1);
5018 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005019 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005020 }
5021 return tk1->tok;
5022}
5023
Eric Biedermancb364952004-11-15 10:46:44 +00005024static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005025{
Eric Biedermancb364952004-11-15 10:46:44 +00005026 raw_peek(state);
5027 return do_eat_token(state, tok);
5028}
5029
5030static int pp_peek(struct compile_state *state)
5031{
5032 struct token *tk1;
5033 tk1 = get_token(state, 1);
5034 if (tk1->tok == -1) {
5035 pp_token(state, tk1);
5036 }
5037 return tk1->tok;
5038}
5039
5040static struct token *pp_eat(struct compile_state *state, int tok)
5041{
5042 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005043 return do_eat_token(state, tok);
5044}
5045
Eric Biedermanb138ac82003-04-22 18:44:01 +00005046static int peek(struct compile_state *state)
5047{
Eric Biederman41203d92004-11-08 09:31:09 +00005048 struct token *tk1;
5049 tk1 = get_token(state, 1);
5050 if (tk1->tok == -1) {
5051 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005052 }
Eric Biederman41203d92004-11-08 09:31:09 +00005053 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005054}
5055
5056static int peek2(struct compile_state *state)
5057{
Eric Biederman41203d92004-11-08 09:31:09 +00005058 struct token *tk1, *tk2;
5059 tk1 = get_token(state, 1);
5060 tk2 = get_token(state, 2);
5061 if (tk1->tok == -1) {
5062 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005063 }
Eric Biederman41203d92004-11-08 09:31:09 +00005064 if (tk2->tok == -1) {
5065 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005066 }
Eric Biederman41203d92004-11-08 09:31:09 +00005067 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005068}
5069
Eric Biederman41203d92004-11-08 09:31:09 +00005070static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005071{
Eric Biederman90089602004-05-28 14:11:54 +00005072 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005073 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005074}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005075
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005076static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005077{
Eric Biederman90089602004-05-28 14:11:54 +00005078 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005079 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005080 int subdir_len;
5081 struct file_state *file;
5082 char *basename;
5083 file = xmalloc(sizeof(*file), "file_state");
5084
5085 base = strrchr(filename, '/');
5086 subdir = filename;
5087 if (base != 0) {
5088 subdir_len = base - filename;
5089 base++;
5090 }
5091 else {
5092 base = filename;
5093 subdir_len = 0;
5094 }
5095 basename = xmalloc(strlen(base) +1, "basename");
5096 strcpy(basename, base);
5097 file->basename = basename;
5098
5099 if (getcwd(cwd, sizeof(cwd)) == 0) {
5100 die("cwd buffer to small");
5101 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005102 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005103 file->dirname = xmalloc(subdir_len + 1, "dirname");
5104 memcpy(file->dirname, subdir, subdir_len);
5105 file->dirname[subdir_len] = '\0';
5106 }
5107 else {
Eric Biederman90089602004-05-28 14:11:54 +00005108 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005109 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005110 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005111 /* Find the appropriate directory... */
5112 dir = 0;
5113 if (!state->file && exists(cwd, filename)) {
5114 dir = cwd;
5115 }
5116 if (local && state->file && exists(state->file->dirname, filename)) {
5117 dir = state->file->dirname;
5118 }
Eric Biederman90089602004-05-28 14:11:54 +00005119 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005120 if (exists(*path, filename)) {
5121 dir = *path;
5122 }
5123 }
5124 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005125 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005126 }
5127 dirlen = strlen(dir);
5128 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5129 memcpy(file->dirname, dir, dirlen);
5130 file->dirname[dirlen] = '/';
5131 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5132 file->dirname[dirlen + 1 + subdir_len] = '\0';
5133 }
5134 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005135
5136 file->pos = file->buf;
5137 file->line_start = file->pos;
5138 file->line = 1;
5139
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005140 file->report_line = 1;
5141 file->report_name = file->basename;
5142 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005143 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005144 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5145 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005146
Eric Biedermanb138ac82003-04-22 18:44:01 +00005147 file->prev = state->file;
5148 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005149}
5150
Eric Biederman41203d92004-11-08 09:31:09 +00005151static struct triple *constant_expr(struct compile_state *state);
5152static void integral(struct compile_state *state, struct triple *def);
5153
5154static int mcexpr(struct compile_state *state)
5155{
5156 struct triple *cvalue;
5157 cvalue = constant_expr(state);
5158 integral(state, cvalue);
5159 if (cvalue->op != OP_INTCONST) {
5160 error(state, 0, "integer constant expected");
5161 }
5162 return cvalue->u.cval != 0;
5163}
5164
5165static void preprocess(struct compile_state *state, struct token *current_token)
5166{
5167 /* Doing much more with the preprocessor would require
5168 * a parser and a major restructuring.
5169 * Postpone that for later.
5170 */
Eric Biederman41203d92004-11-08 09:31:09 +00005171 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005172 int tok;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005173
Eric Biedermancb364952004-11-15 10:46:44 +00005174 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005175
5176 old_token_base = state->token_base;
5177 state->token_base = current_token - state->token;
5178
Eric Biedermancb364952004-11-15 10:46:44 +00005179 tok = pp_peek(state);
5180 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005181 case TOK_LIT_INT:
5182 {
5183 struct token *tk;
5184 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005185 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005186 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005187 /* I have a preprocessor line marker parse it */
5188 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005189 const char *token, *base;
5190 char *name, *dir;
5191 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005192 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005193 name = xmalloc(tk->str_len, "report_name");
5194 token = tk->val.str + 1;
5195 base = strrchr(token, '/');
5196 name_len = tk->str_len -2;
5197 if (base != 0) {
5198 dir_len = base - token;
5199 base++;
5200 name_len -= base - token;
5201 } else {
5202 dir_len = 0;
5203 base = token;
5204 }
5205 memcpy(name, base, name_len);
5206 name[name_len] = '\0';
5207 dir = xmalloc(dir_len + 1, "report_dir");
5208 memcpy(dir, token, dir_len);
5209 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005210 state->file->report_line = override_line - 1;
5211 state->file->report_name = name;
5212 state->file->report_dir = dir;
5213 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005214 }
5215 break;
5216 }
5217 case TOK_MLINE:
5218 {
5219 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005220 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005221 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005222 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5223 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005224 const char *token, *base;
5225 char *name, *dir;
5226 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005227 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005228 name = xmalloc(tk->str_len, "report_name");
5229 token = tk->val.str + 1;
5230 base = strrchr(token, '/');
5231 name_len = tk->str_len - 2;
5232 if (base != 0) {
5233 dir_len = base - token;
5234 base++;
5235 name_len -= base - token;
5236 } else {
5237 dir_len = 0;
5238 base = token;
5239 }
5240 memcpy(name, base, name_len);
5241 name[name_len] = '\0';
5242 dir = xmalloc(dir_len + 1, "report_dir");
5243 memcpy(dir, token, dir_len);
5244 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005245 state->file->report_name = name;
5246 state->file->report_dir = dir;
5247 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005248 }
5249 break;
5250 }
5251 case TOK_MUNDEF:
5252 {
5253 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005254 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005255 if (if_eat(state)) /* quit early when #if'd out */
5256 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005257
Eric Biedermancb364952004-11-15 10:46:44 +00005258 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005259
5260 undef_macro(state, ident);
5261 break;
5262 }
5263 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005264 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005265 if (if_eat(state)) /* quit early when #if'd out */
5266 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005267 warning(state, 0, "Ignoring pragma");
Eric Biederman41203d92004-11-08 09:31:09 +00005268 break;
5269 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005270 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005271 reenter_if(state, "#elif");
5272 if (if_eat(state)) /* quit early when #if'd out */
5273 break;
5274 /* If the #if was taken the #elif just disables the following code */
5275 if (if_value(state)) {
5276 eat_tokens(state, TOK_MENDIF);
5277 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005278 /* If the previous #if was not taken see if the #elif enables the
Eric Biederman41203d92004-11-08 09:31:09 +00005279 * trailing code.
5280 */
5281 else {
5282 set_if_value(state, mcexpr(state));
5283 if (!if_value(state)) {
5284 eat_tokens(state, TOK_MELSE);
5285 }
5286 }
5287 break;
5288 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005289 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005290 enter_if(state);
5291 if (if_eat(state)) /* quit early when #if'd out */
5292 break;
5293 set_if_value(state, mcexpr(state));
5294 if (!if_value(state)) {
5295 eat_tokens(state, TOK_MELSE);
5296 }
5297 break;
5298 case TOK_MIFNDEF:
5299 {
5300 struct hash_entry *ident;
5301
Eric Biedermancb364952004-11-15 10:46:44 +00005302 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005303 enter_if(state);
5304 if (if_eat(state)) /* quit early when #if'd out */
5305 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005306 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005307 set_if_value(state, ident->sym_define == 0);
5308 if (!if_value(state)) {
5309 eat_tokens(state, TOK_MELSE);
5310 }
5311 break;
5312 }
5313 case TOK_MIFDEF:
5314 {
5315 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005316 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005317 enter_if(state);
5318 if (if_eat(state)) /* quit early when #if'd out */
5319 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005320 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005321 set_if_value(state, ident->sym_define != 0);
5322 if (!if_value(state)) {
5323 eat_tokens(state, TOK_MELSE);
5324 }
5325 break;
5326 }
5327 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005328 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005329 enter_else(state, "#else");
5330 if (!if_eat(state) && if_value(state)) {
5331 eat_tokens(state, TOK_MENDIF);
5332 }
5333 break;
5334 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005335 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005336 exit_if(state, "#endif");
5337 break;
5338 case TOK_MDEFINE:
5339 {
5340 struct hash_entry *ident;
5341 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005342 const char *mstart, *mend;
5343 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005344
Eric Biedermancb364952004-11-15 10:46:44 +00005345 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005346 if (if_eat(state)) /* quit early when #if'd out */
5347 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005348 ident = pp_eat(state, TOK_MIDENT)->ident;
5349 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005350 args = 0;
5351 larg = &args;
5352
Eric Biederman41203d92004-11-08 09:31:09 +00005353 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005354 if (raw_peek(state) == TOK_LPAREN) {
5355 raw_eat(state, TOK_LPAREN);
5356 argc += 1;
5357
Eric Biederman41203d92004-11-08 09:31:09 +00005358 for(;;) {
5359 struct macro_arg *narg, *arg;
5360 struct hash_entry *aident;
5361 int tok;
5362
Eric Biedermancb364952004-11-15 10:46:44 +00005363 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005364 if (!args && (tok == TOK_RPAREN)) {
5365 break;
5366 }
5367 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005368 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005369 aident = state->i___VA_ARGS__;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005370 }
Eric Biederman41203d92004-11-08 09:31:09 +00005371 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005372 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005373 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005374
Eric Biederman41203d92004-11-08 09:31:09 +00005375 narg = xcmalloc(sizeof(*arg), "macro arg");
5376 narg->ident = aident;
5377
5378 /* Verify I don't have a duplicate identifier */
5379 for(arg = args; arg; arg = arg->next) {
5380 if (arg->ident == narg->ident) {
5381 error(state, 0, "Duplicate macro arg `%s'",
5382 narg->ident->name);
5383 }
5384 }
5385 /* Add the new argument to the end of the list */
5386 *larg = narg;
5387 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005388 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005389
5390 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005391 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005392 break;
5393 }
Eric Biedermancb364952004-11-15 10:46:44 +00005394 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005395 }
Eric Biedermancb364952004-11-15 10:46:44 +00005396 pp_eat(state, TOK_RPAREN);
5397 }
5398 /* Remove leading whitespace */
5399 while(raw_peek(state) == TOK_SPACE) {
5400 raw_eat(state, TOK_SPACE);
5401 }
Eric Biederman41203d92004-11-08 09:31:09 +00005402
Eric Biedermancb364952004-11-15 10:46:44 +00005403 /* Remember the start of the macro body */
Patrick Georgi37935572017-01-02 21:39:43 +01005404 raw_peek(state);
Eric Biedermancb364952004-11-15 10:46:44 +00005405 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005406
Eric Biedermancb364952004-11-15 10:46:44 +00005407 /* Find the end of the macro */
5408 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5409 raw_eat(state, tok);
5410 /* Remember the end of the last non space token */
5411 raw_peek(state);
5412 if (tok != TOK_SPACE) {
5413 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005414 }
5415 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005416
Eric Biedermancb364952004-11-15 10:46:44 +00005417 /* Now that I have found the body defined the token */
5418 do_define_macro(state, ident,
5419 char_strdup(state->file, mstart, mend, "macro buf"),
5420 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005421 break;
5422 }
5423 case TOK_MERROR:
5424 {
Eric Biedermancb364952004-11-15 10:46:44 +00005425 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005426 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005427
Eric Biedermancb364952004-11-15 10:46:44 +00005428 pp_eat(state, TOK_MERROR);
5429 /* Find the start of the line */
5430 raw_peek(state);
5431 start = get_token(state, 1)->pos;
5432
Eric Biederman41203d92004-11-08 09:31:09 +00005433 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005434 while((tok = raw_peek(state)) != TOK_EOL) {
5435 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005436 }
Eric Biedermancb364952004-11-15 10:46:44 +00005437 end = get_token(state, 1)->pos;
5438 len = end - start;
5439 if (!if_eat(state)) {
5440 error(state, 0, "%*.*s", len, len, start);
5441 }
Eric Biederman41203d92004-11-08 09:31:09 +00005442 break;
5443 }
5444 case TOK_MWARNING:
5445 {
Eric Biedermancb364952004-11-15 10:46:44 +00005446 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005447 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005448
Eric Biedermancb364952004-11-15 10:46:44 +00005449 pp_eat(state, TOK_MWARNING);
5450
5451 /* Find the start of the line */
5452 raw_peek(state);
5453 start = get_token(state, 1)->pos;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005454
Eric Biederman41203d92004-11-08 09:31:09 +00005455 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005456 while((tok = raw_peek(state)) != TOK_EOL) {
5457 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005458 }
Eric Biedermancb364952004-11-15 10:46:44 +00005459 end = get_token(state, 1)->pos;
5460 len = end - start;
5461 if (!if_eat(state)) {
5462 warning(state, 0, "%*.*s", len, len, start);
5463 }
Eric Biederman41203d92004-11-08 09:31:09 +00005464 break;
5465 }
5466 case TOK_MINCLUDE:
5467 {
5468 char *name;
5469 int local;
Eric Biederman41203d92004-11-08 09:31:09 +00005470 name = 0;
5471
Eric Biedermancb364952004-11-15 10:46:44 +00005472 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005473 if (if_eat(state)) {
5474 /* Find the end of the line */
5475 while((tok = raw_peek(state)) != TOK_EOL) {
5476 raw_eat(state, tok);
5477 }
5478 break;
5479 }
Eric Biederman41203d92004-11-08 09:31:09 +00005480 tok = peek(state);
5481 if (tok == TOK_LIT_STRING) {
5482 struct token *tk;
5483 const char *token;
5484 int name_len;
5485 tk = eat(state, TOK_LIT_STRING);
5486 name = xmalloc(tk->str_len, "include");
5487 token = tk->val.str +1;
5488 name_len = tk->str_len -2;
5489 if (*token == '"') {
5490 token++;
5491 name_len--;
5492 }
5493 memcpy(name, token, name_len);
5494 name[name_len] = '\0';
5495 local = 1;
5496 }
5497 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005498 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005499 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005500
5501 buf.len = 40;
5502 buf.str = xmalloc(buf.len, "include");
5503 buf.pos = 0;
5504
5505 tok = peek(state);
5506 while((tok != TOK_MORE) &&
5507 (tok != TOK_EOL) && (tok != TOK_EOF))
5508 {
5509 struct token *tk;
5510 tk = eat(state, tok);
5511 append_macro_chars(state, "include", &buf,
5512 state->file, tk->pos, state->file->pos);
5513 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005514 }
Eric Biedermancb364952004-11-15 10:46:44 +00005515 append_macro_text(state, "include", &buf, "\0", 1);
5516 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005517 error(state, 0, "Unterminated include directive");
5518 }
Eric Biederman41203d92004-11-08 09:31:09 +00005519 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005520 local = 0;
5521 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005522 }
5523 else {
5524 error(state, 0, "Invalid include directive");
5525 }
5526 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005527 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005528 error(state, 0, "garbage after include directive");
5529 }
5530 if (!if_eat(state)) {
5531 compile_file(state, name, local);
5532 }
5533 xfree(name);
5534 break;
5535 }
5536 case TOK_EOL:
5537 /* Ignore # without a follwing ident */
5538 break;
5539 default:
5540 {
5541 const char *name1, *name2;
5542 name1 = tokens[tok];
5543 name2 = "";
5544 if (tok == TOK_MIDENT) {
5545 name2 = get_token(state, 1)->ident->name;
5546 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005547 error(state, 0, "Invalid preprocessor directive: %s %s",
Eric Biederman41203d92004-11-08 09:31:09 +00005548 name1, name2);
5549 break;
5550 }
5551 }
5552 /* Consume the rest of the macro line */
5553 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005554 tok = pp_peek(state);
5555 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005556 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5557 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005558 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005559 return;
5560}
5561
Eric Biederman0babc1c2003-05-09 02:39:00 +00005562/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005563
5564static struct type *new_type(
5565 unsigned int type, struct type *left, struct type *right)
5566{
5567 struct type *result;
5568 result = xmalloc(sizeof(*result), "type");
5569 result->type = type;
5570 result->left = left;
5571 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005572 result->field_ident = 0;
5573 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005574 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005575 return result;
5576}
5577
5578static struct type *clone_type(unsigned int specifiers, struct type *old)
5579{
5580 struct type *result;
5581 result = xmalloc(sizeof(*result), "type");
5582 memcpy(result, old, sizeof(*result));
5583 result->type &= TYPE_MASK;
5584 result->type |= specifiers;
5585 return result;
5586}
5587
Eric Biederman90089602004-05-28 14:11:54 +00005588static struct type *dup_type(struct compile_state *state, struct type *orig)
5589{
5590 struct type *new;
5591 new = xcmalloc(sizeof(*new), "type");
5592 new->type = orig->type;
5593 new->field_ident = orig->field_ident;
5594 new->type_ident = orig->type_ident;
5595 new->elements = orig->elements;
5596 if (orig->left) {
5597 new->left = dup_type(state, orig->left);
5598 }
5599 if (orig->right) {
5600 new->right = dup_type(state, orig->right);
5601 }
5602 return new;
5603}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005604
Eric Biederman90089602004-05-28 14:11:54 +00005605
5606static struct type *invalid_type(struct compile_state *state, struct type *type)
5607{
5608 struct type *invalid, *member;
5609 invalid = 0;
5610 if (!type) {
5611 internal_error(state, 0, "type missing?");
5612 }
5613 switch(type->type & TYPE_MASK) {
5614 case TYPE_VOID:
5615 case TYPE_CHAR: case TYPE_UCHAR:
5616 case TYPE_SHORT: case TYPE_USHORT:
5617 case TYPE_INT: case TYPE_UINT:
5618 case TYPE_LONG: case TYPE_ULONG:
5619 case TYPE_LLONG: case TYPE_ULLONG:
5620 case TYPE_POINTER:
5621 case TYPE_ENUM:
5622 break;
5623 case TYPE_BITFIELD:
5624 invalid = invalid_type(state, type->left);
5625 break;
5626 case TYPE_ARRAY:
5627 invalid = invalid_type(state, type->left);
5628 break;
5629 case TYPE_STRUCT:
5630 case TYPE_TUPLE:
5631 member = type->left;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005632 while(member && (invalid == 0) &&
Eric Biederman90089602004-05-28 14:11:54 +00005633 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5634 invalid = invalid_type(state, member->left);
5635 member = member->right;
5636 }
5637 if (!invalid) {
5638 invalid = invalid_type(state, member);
5639 }
5640 break;
5641 case TYPE_UNION:
5642 case TYPE_JOIN:
5643 member = type->left;
5644 while(member && (invalid == 0) &&
5645 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5646 invalid = invalid_type(state, member->left);
5647 member = member->right;
5648 }
5649 if (!invalid) {
5650 invalid = invalid_type(state, member);
5651 }
5652 break;
5653 default:
5654 invalid = type;
5655 break;
5656 }
5657 return invalid;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005658
Eric Biederman90089602004-05-28 14:11:54 +00005659}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005660
Eric Biederman90089602004-05-28 14:11:54 +00005661static struct type void_type = { .type = TYPE_VOID };
5662static struct type char_type = { .type = TYPE_CHAR };
5663static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005664#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005665static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005666#endif
Eric Biederman90089602004-05-28 14:11:54 +00005667static struct type ushort_type = { .type = TYPE_USHORT };
5668static struct type int_type = { .type = TYPE_INT };
5669static struct type uint_type = { .type = TYPE_UINT };
5670static struct type long_type = { .type = TYPE_LONG };
5671static struct type ulong_type = { .type = TYPE_ULONG };
5672static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005673
Eric Biederman5ade04a2003-10-22 04:03:46 +00005674static struct type void_ptr_type = {
5675 .type = TYPE_POINTER,
5676 .left = &void_type,
5677};
5678
Stefan Reinauer50542a82007-10-24 11:14:14 +00005679#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005680static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005681 .type = TYPE_FUNCTION,
5682 .left = &void_type,
5683 .right = &void_type,
5684};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005685#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005686
Eric Biederman90089602004-05-28 14:11:54 +00005687static size_t bits_to_bytes(size_t size)
5688{
5689 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5690}
5691
Eric Biedermanb138ac82003-04-22 18:44:01 +00005692static struct triple *variable(struct compile_state *state, struct type *type)
5693{
5694 struct triple *result;
5695 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005696 result = triple(state, OP_ADECL, type, 0, 0);
5697 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005698 }
5699 else {
5700 result = triple(state, OP_SDECL, type, 0, 0);
5701 }
5702 return result;
5703}
5704
5705static void stor_of(FILE *fp, struct type *type)
5706{
5707 switch(type->type & STOR_MASK) {
5708 case STOR_AUTO:
5709 fprintf(fp, "auto ");
5710 break;
5711 case STOR_STATIC:
5712 fprintf(fp, "static ");
5713 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005714 case STOR_LOCAL:
5715 fprintf(fp, "local ");
5716 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005717 case STOR_EXTERN:
5718 fprintf(fp, "extern ");
5719 break;
5720 case STOR_REGISTER:
5721 fprintf(fp, "register ");
5722 break;
5723 case STOR_TYPEDEF:
5724 fprintf(fp, "typedef ");
5725 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005726 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005727 fprintf(fp, "inline ");
5728 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005729 case STOR_INLINE | STOR_STATIC:
5730 fprintf(fp, "static inline");
5731 break;
5732 case STOR_INLINE | STOR_EXTERN:
5733 fprintf(fp, "extern inline");
5734 break;
5735 default:
5736 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5737 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005738 }
5739}
5740static void qual_of(FILE *fp, struct type *type)
5741{
5742 if (type->type & QUAL_CONST) {
5743 fprintf(fp, " const");
5744 }
5745 if (type->type & QUAL_VOLATILE) {
5746 fprintf(fp, " volatile");
5747 }
5748 if (type->type & QUAL_RESTRICT) {
5749 fprintf(fp, " restrict");
5750 }
5751}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005752
Eric Biedermanb138ac82003-04-22 18:44:01 +00005753static void name_of(FILE *fp, struct type *type)
5754{
Eric Biederman90089602004-05-28 14:11:54 +00005755 unsigned int base_type;
5756 base_type = type->type & TYPE_MASK;
5757 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5758 stor_of(fp, type);
5759 }
5760 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005761 case TYPE_VOID:
5762 fprintf(fp, "void");
5763 qual_of(fp, type);
5764 break;
5765 case TYPE_CHAR:
5766 fprintf(fp, "signed char");
5767 qual_of(fp, type);
5768 break;
5769 case TYPE_UCHAR:
5770 fprintf(fp, "unsigned char");
5771 qual_of(fp, type);
5772 break;
5773 case TYPE_SHORT:
5774 fprintf(fp, "signed short");
5775 qual_of(fp, type);
5776 break;
5777 case TYPE_USHORT:
5778 fprintf(fp, "unsigned short");
5779 qual_of(fp, type);
5780 break;
5781 case TYPE_INT:
5782 fprintf(fp, "signed int");
5783 qual_of(fp, type);
5784 break;
5785 case TYPE_UINT:
5786 fprintf(fp, "unsigned int");
5787 qual_of(fp, type);
5788 break;
5789 case TYPE_LONG:
5790 fprintf(fp, "signed long");
5791 qual_of(fp, type);
5792 break;
5793 case TYPE_ULONG:
5794 fprintf(fp, "unsigned long");
5795 qual_of(fp, type);
5796 break;
5797 case TYPE_POINTER:
5798 name_of(fp, type->left);
5799 fprintf(fp, " * ");
5800 qual_of(fp, type);
5801 break;
5802 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005803 name_of(fp, type->left);
5804 fprintf(fp, ", ");
5805 name_of(fp, type->right);
5806 break;
Eric Biederman90089602004-05-28 14:11:54 +00005807 case TYPE_OVERLAP:
5808 name_of(fp, type->left);
5809 fprintf(fp, ",| ");
5810 name_of(fp, type->right);
5811 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005812 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005813 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005814 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005815 qual_of(fp, type);
5816 break;
5817 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005818 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005819 (type->type_ident)? type->type_ident->name : "");
5820 name_of(fp, type->left);
5821 fprintf(fp, " } ");
5822 qual_of(fp, type);
5823 break;
5824 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005825 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005826 (type->type_ident)? type->type_ident->name : "");
5827 name_of(fp, type->left);
5828 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005829 qual_of(fp, type);
5830 break;
5831 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005832 name_of(fp, type->left);
5833 fprintf(fp, " (*)(");
5834 name_of(fp, type->right);
5835 fprintf(fp, ")");
5836 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005837 case TYPE_ARRAY:
5838 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005839 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005840 break;
Eric Biederman90089602004-05-28 14:11:54 +00005841 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005842 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005843 name_of(fp, type->left);
5844 fprintf(fp, " } ");
5845 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005846 break;
Eric Biederman90089602004-05-28 14:11:54 +00005847 case TYPE_JOIN:
5848 fprintf(fp, "join { ");
5849 name_of(fp, type->left);
5850 fprintf(fp, " } ");
5851 qual_of(fp, type);
5852 break;
5853 case TYPE_BITFIELD:
5854 name_of(fp, type->left);
5855 fprintf(fp, " : %d ", type->elements);
5856 qual_of(fp, type);
5857 break;
5858 case TYPE_UNKNOWN:
5859 fprintf(fp, "unknown_t");
5860 break;
5861 default:
5862 fprintf(fp, "????: %x", base_type);
5863 break;
5864 }
5865 if (type->field_ident && type->field_ident->name) {
5866 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005867 }
5868}
5869
5870static size_t align_of(struct compile_state *state, struct type *type)
5871{
5872 size_t align;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005873 switch(type->type & TYPE_MASK) {
5874 case TYPE_VOID:
5875 align = 1;
5876 break;
Eric Biederman90089602004-05-28 14:11:54 +00005877 case TYPE_BITFIELD:
5878 align = 1;
5879 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005880 case TYPE_CHAR:
5881 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005882 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005883 break;
5884 case TYPE_SHORT:
5885 case TYPE_USHORT:
5886 align = ALIGNOF_SHORT;
5887 break;
5888 case TYPE_INT:
5889 case TYPE_UINT:
5890 case TYPE_ENUM:
5891 align = ALIGNOF_INT;
5892 break;
5893 case TYPE_LONG:
5894 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005895 align = ALIGNOF_LONG;
5896 break;
Eric Biederman90089602004-05-28 14:11:54 +00005897 case TYPE_POINTER:
5898 align = ALIGNOF_POINTER;
5899 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005900 case TYPE_PRODUCT:
5901 case TYPE_OVERLAP:
5902 {
5903 size_t left_align, right_align;
5904 left_align = align_of(state, type->left);
5905 right_align = align_of(state, type->right);
5906 align = (left_align >= right_align) ? left_align : right_align;
5907 break;
5908 }
5909 case TYPE_ARRAY:
5910 align = align_of(state, type->left);
5911 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005912 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005913 case TYPE_TUPLE:
5914 case TYPE_UNION:
5915 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005916 align = align_of(state, type->left);
5917 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005918 default:
5919 error(state, 0, "alignof not yet defined for type\n");
5920 break;
5921 }
5922 return align;
5923}
5924
Eric Biederman90089602004-05-28 14:11:54 +00005925static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005926{
Eric Biederman90089602004-05-28 14:11:54 +00005927 size_t align;
Eric Biederman90089602004-05-28 14:11:54 +00005928 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;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006032 switch(type->type & TYPE_MASK) {
6033 case TYPE_VOID:
6034 size = 0;
6035 break;
Eric Biederman90089602004-05-28 14:11:54 +00006036 case TYPE_BITFIELD:
6037 size = type->elements;
6038 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006039 case TYPE_CHAR:
6040 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006041 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006042 break;
6043 case TYPE_SHORT:
6044 case TYPE_USHORT:
6045 size = SIZEOF_SHORT;
6046 break;
6047 case TYPE_INT:
6048 case TYPE_UINT:
6049 case TYPE_ENUM:
6050 size = SIZEOF_INT;
6051 break;
6052 case TYPE_LONG:
6053 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006054 size = SIZEOF_LONG;
6055 break;
Eric Biederman90089602004-05-28 14:11:54 +00006056 case TYPE_POINTER:
6057 size = SIZEOF_POINTER;
6058 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006059 case TYPE_PRODUCT:
6060 {
Eric Biederman90089602004-05-28 14:11:54 +00006061 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006062 size = 0;
6063 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006064 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006065 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006066 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006067 }
Eric Biederman90089602004-05-28 14:11:54 +00006068 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006069 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006070 break;
6071 }
6072 case TYPE_OVERLAP:
6073 {
6074 size_t size_left, size_right;
6075 size_left = size_of(state, type->left);
6076 size_right = size_of(state, type->right);
6077 size = (size_left >= size_right)? size_left : size_right;
6078 break;
6079 }
6080 case TYPE_ARRAY:
6081 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6082 internal_error(state, 0, "Invalid array type");
6083 } else {
6084 size = size_of(state, type->left) * type->elements;
6085 }
6086 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006087 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006088 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006089 {
Eric Biederman90089602004-05-28 14:11:54 +00006090 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006091 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006092 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006093 pad = needed_padding(state, type, size);
6094 size = size + pad;
6095 break;
6096 }
6097 case TYPE_UNION:
6098 case TYPE_JOIN:
6099 {
6100 size_t pad;
6101 size = size_of(state, type->left);
6102 /* Pad unions so their size is a multiple of their alignment */
6103 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006104 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006105 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006106 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006107 default:
Eric Biederman90089602004-05-28 14:11:54 +00006108 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006109 break;
6110 }
6111 return size;
6112}
6113
Eric Biederman90089602004-05-28 14:11:54 +00006114static size_t reg_size_of(struct compile_state *state, struct type *type)
6115{
6116 size_t size;
Eric Biederman90089602004-05-28 14:11:54 +00006117 switch(type->type & TYPE_MASK) {
6118 case TYPE_VOID:
6119 size = 0;
6120 break;
6121 case TYPE_BITFIELD:
6122 size = type->elements;
6123 break;
6124 case TYPE_CHAR:
6125 case TYPE_UCHAR:
6126 size = REG_SIZEOF_CHAR;
6127 break;
6128 case TYPE_SHORT:
6129 case TYPE_USHORT:
6130 size = REG_SIZEOF_SHORT;
6131 break;
6132 case TYPE_INT:
6133 case TYPE_UINT:
6134 case TYPE_ENUM:
6135 size = REG_SIZEOF_INT;
6136 break;
6137 case TYPE_LONG:
6138 case TYPE_ULONG:
6139 size = REG_SIZEOF_LONG;
6140 break;
6141 case TYPE_POINTER:
6142 size = REG_SIZEOF_POINTER;
6143 break;
6144 case TYPE_PRODUCT:
6145 {
6146 size_t pad;
6147 size = 0;
6148 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6149 pad = reg_needed_padding(state, type->left, size);
6150 size = size + pad + reg_size_of(state, type->left);
6151 type = type->right;
6152 }
6153 pad = reg_needed_padding(state, type, size);
6154 size = size + pad + reg_size_of(state, type);
6155 break;
6156 }
6157 case TYPE_OVERLAP:
6158 {
6159 size_t size_left, size_right;
6160 size_left = reg_size_of(state, type->left);
6161 size_right = reg_size_of(state, type->right);
6162 size = (size_left >= size_right)? size_left : size_right;
6163 break;
6164 }
6165 case TYPE_ARRAY:
6166 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6167 internal_error(state, 0, "Invalid array type");
6168 } else {
6169 size = reg_size_of(state, type->left) * type->elements;
6170 }
6171 break;
6172 case TYPE_STRUCT:
6173 case TYPE_TUPLE:
6174 {
6175 size_t pad;
6176 size = reg_size_of(state, type->left);
6177 /* Pad structures so their size is a multiples of their alignment */
6178 pad = reg_needed_padding(state, type, size);
6179 size = size + pad;
6180 break;
6181 }
6182 case TYPE_UNION:
6183 case TYPE_JOIN:
6184 {
6185 size_t pad;
6186 size = reg_size_of(state, type->left);
6187 /* Pad unions so their size is a multiple of their alignment */
6188 pad = reg_needed_padding(state, type, size);
6189 size = size + pad;
6190 break;
6191 }
6192 default:
6193 internal_error(state, 0, "sizeof not yet defined for type");
6194 break;
6195 }
6196 return size;
6197}
6198
6199static size_t registers_of(struct compile_state *state, struct type *type)
6200{
6201 size_t registers;
6202 registers = reg_size_of(state, type);
6203 registers += REG_SIZEOF_REG - 1;
6204 registers /= REG_SIZEOF_REG;
6205 return registers;
6206}
6207
6208static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6209{
6210 return bits_to_bytes(size_of(state, type));
6211}
6212
Stefan Reinauer14e22772010-04-27 06:56:47 +00006213static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006214 struct type *type, struct hash_entry *field)
6215{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006216 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006217 size_t size;
6218
Eric Biederman0babc1c2003-05-09 02:39:00 +00006219 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006220 member = 0;
6221 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6222 member = type->left;
6223 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6224 size += needed_padding(state, member->left, size);
6225 if (member->left->field_ident == field) {
6226 member = member->left;
6227 break;
6228 }
6229 size += size_of(state, member->left);
6230 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006231 }
Eric Biederman90089602004-05-28 14:11:54 +00006232 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006233 }
Eric Biederman90089602004-05-28 14:11:54 +00006234 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6235 member = type->left;
6236 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6237 if (member->left->field_ident == field) {
6238 member = member->left;
6239 break;
6240 }
6241 member = member->right;
6242 }
6243 }
6244 else {
6245 internal_error(state, 0, "field_offset only works on structures and unions");
6246 }
6247
6248 if (!member || (member->field_ident != field)) {
6249 error(state, 0, "member %s not present", field->name);
6250 }
6251 return size;
6252}
6253
Stefan Reinauer14e22772010-04-27 06:56:47 +00006254static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006255 struct type *type, struct hash_entry *field)
6256{
6257 struct type *member;
6258 size_t size;
6259
6260 size = 0;
6261 member = 0;
6262 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6263 member = type->left;
6264 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6265 size += reg_needed_padding(state, member->left, size);
6266 if (member->left->field_ident == field) {
6267 member = member->left;
6268 break;
6269 }
6270 size += reg_size_of(state, member->left);
6271 member = member->right;
6272 }
6273 }
6274 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6275 member = type->left;
6276 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6277 if (member->left->field_ident == field) {
6278 member = member->left;
6279 break;
6280 }
6281 member = member->right;
6282 }
6283 }
6284 else {
6285 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6286 }
6287
Eric Biederman90089602004-05-28 14:11:54 +00006288 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006289 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006290 }
Patrick Georgif78e6582016-12-15 15:14:59 +01006291 size += reg_needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006292 return size;
6293}
6294
Stefan Reinauer14e22772010-04-27 06:56:47 +00006295static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006296 struct type *type, struct hash_entry *field)
6297{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006298 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006299
6300 member = 0;
6301 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6302 member = type->left;
6303 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6304 if (member->left->field_ident == field) {
6305 member = member->left;
6306 break;
6307 }
6308 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006309 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006310 }
Eric Biederman90089602004-05-28 14:11:54 +00006311 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6312 member = type->left;
6313 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6314 if (member->left->field_ident == field) {
6315 member = member->left;
6316 break;
6317 }
6318 member = member->right;
6319 }
6320 }
6321 else {
6322 internal_error(state, 0, "field_type only works on structures and unions");
6323 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006324
Eric Biederman90089602004-05-28 14:11:54 +00006325 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006326 error(state, 0, "member %s not present", field->name);
6327 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006328 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006329}
6330
Stefan Reinauer14e22772010-04-27 06:56:47 +00006331static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006332 struct type *type, ulong_t index)
6333{
6334 struct type *member;
6335 size_t size;
6336 size = 0;
6337 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6338 size = size_of(state, type->left) * index;
6339 }
6340 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6341 ulong_t i;
6342 member = type->left;
6343 i = 0;
6344 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6345 size += needed_padding(state, member->left, size);
6346 if (i == index) {
6347 member = member->left;
6348 break;
6349 }
6350 size += size_of(state, member->left);
6351 i++;
6352 member = member->right;
6353 }
6354 size += needed_padding(state, member, size);
6355 if (i != index) {
6356 internal_error(state, 0, "Missing member index: %u", index);
6357 }
6358 }
6359 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6360 ulong_t i;
6361 size = 0;
6362 member = type->left;
6363 i = 0;
6364 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6365 if (i == index) {
Eric Biederman90089602004-05-28 14:11:54 +00006366 break;
6367 }
6368 i++;
6369 member = member->right;
6370 }
6371 if (i != index) {
6372 internal_error(state, 0, "Missing member index: %u", index);
6373 }
6374 }
6375 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006376 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006377 "request for index %u in something not an array, tuple or join",
6378 index);
6379 }
6380 return size;
6381}
6382
Stefan Reinauer14e22772010-04-27 06:56:47 +00006383static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006384 struct type *type, ulong_t index)
6385{
6386 struct type *member;
6387 size_t size;
6388 size = 0;
6389 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6390 size = reg_size_of(state, type->left) * index;
6391 }
6392 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6393 ulong_t i;
6394 member = type->left;
6395 i = 0;
6396 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6397 size += reg_needed_padding(state, member->left, size);
6398 if (i == index) {
6399 member = member->left;
6400 break;
6401 }
6402 size += reg_size_of(state, member->left);
6403 i++;
6404 member = member->right;
6405 }
Eric Biederman90089602004-05-28 14:11:54 +00006406 if (i != index) {
6407 internal_error(state, 0, "Missing member index: %u", index);
6408 }
Patrick Georgiadcad7f2016-12-15 15:24:06 +01006409 size += reg_needed_padding(state, member, size);
Eric Biederman90089602004-05-28 14:11:54 +00006410 }
6411 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6412 ulong_t i;
6413 size = 0;
6414 member = type->left;
6415 i = 0;
6416 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6417 if (i == index) {
Eric Biederman90089602004-05-28 14:11:54 +00006418 break;
6419 }
6420 i++;
6421 member = member->right;
6422 }
6423 if (i != index) {
6424 internal_error(state, 0, "Missing member index: %u", index);
6425 }
6426 }
6427 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006428 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006429 "request for index %u in something not an array, tuple or join",
6430 index);
6431 }
6432 return size;
6433}
6434
6435static struct type *index_type(struct compile_state *state,
6436 struct type *type, ulong_t index)
6437{
6438 struct type *member;
6439 if (index >= type->elements) {
6440 internal_error(state, 0, "Invalid element %u requested", index);
6441 }
6442 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6443 member = type->left;
6444 }
6445 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6446 ulong_t i;
6447 member = type->left;
6448 i = 0;
6449 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6450 if (i == index) {
6451 member = member->left;
6452 break;
6453 }
6454 i++;
6455 member = member->right;
6456 }
6457 if (i != index) {
6458 internal_error(state, 0, "Missing member index: %u", index);
6459 }
6460 }
6461 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6462 ulong_t i;
6463 member = type->left;
6464 i = 0;
6465 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6466 if (i == index) {
6467 member = member->left;
6468 break;
6469 }
6470 i++;
6471 member = member->right;
6472 }
6473 if (i != index) {
6474 internal_error(state, 0, "Missing member index: %u", index);
6475 }
6476 }
6477 else {
6478 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006479 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006480 "request for index %u in something not an array, tuple or join",
6481 index);
6482 }
6483 return member;
6484}
6485
6486static struct type *unpack_type(struct compile_state *state, struct type *type)
6487{
6488 /* If I have a single register compound type not a bit-field
6489 * find the real type.
6490 */
6491 struct type *start_type;
6492 size_t size;
6493 /* Get out early if I need multiple registers for this type */
6494 size = reg_size_of(state, type);
6495 if (size > REG_SIZEOF_REG) {
6496 return type;
6497 }
6498 /* Get out early if I don't need any registers for this type */
6499 if (size == 0) {
6500 return &void_type;
6501 }
6502 /* Loop until I have no more layers I can remove */
6503 do {
6504 start_type = type;
6505 switch(type->type & TYPE_MASK) {
6506 case TYPE_ARRAY:
6507 /* If I have a single element the unpacked type
6508 * is that element.
6509 */
6510 if (type->elements == 1) {
6511 type = type->left;
6512 }
6513 break;
6514 case TYPE_STRUCT:
6515 case TYPE_TUPLE:
6516 /* If I have a single element the unpacked type
6517 * is that element.
6518 */
6519 if (type->elements == 1) {
6520 type = type->left;
6521 }
6522 /* If I have multiple elements the unpacked
6523 * type is the non-void element.
6524 */
6525 else {
6526 struct type *next, *member;
6527 struct type *sub_type;
6528 sub_type = 0;
6529 next = type->left;
6530 while(next) {
6531 member = next;
6532 next = 0;
6533 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6534 next = member->right;
6535 member = member->left;
6536 }
6537 if (reg_size_of(state, member) > 0) {
6538 if (sub_type) {
6539 internal_error(state, 0, "true compound type in a register");
6540 }
6541 sub_type = member;
6542 }
6543 }
6544 if (sub_type) {
6545 type = sub_type;
6546 }
6547 }
6548 break;
6549
6550 case TYPE_UNION:
6551 case TYPE_JOIN:
6552 /* If I have a single element the unpacked type
6553 * is that element.
6554 */
6555 if (type->elements == 1) {
6556 type = type->left;
6557 }
6558 /* I can't in general unpack union types */
6559 break;
6560 default:
6561 /* If I'm not a compound type I can't unpack it */
6562 break;
6563 }
6564 } while(start_type != type);
6565 switch(type->type & TYPE_MASK) {
6566 case TYPE_STRUCT:
6567 case TYPE_ARRAY:
6568 case TYPE_TUPLE:
6569 internal_error(state, 0, "irredicible type?");
6570 break;
6571 }
6572 return type;
6573}
6574
6575static int equiv_types(struct type *left, struct type *right);
6576static int is_compound_type(struct type *type);
6577
6578static struct type *reg_type(
6579 struct compile_state *state, struct type *type, int reg_offset)
6580{
6581 struct type *member;
6582 size_t size;
6583#if 1
6584 struct type *invalid;
6585 invalid = invalid_type(state, type);
6586 if (invalid) {
6587 fprintf(state->errout, "type: ");
6588 name_of(state->errout, type);
6589 fprintf(state->errout, "\n");
6590 fprintf(state->errout, "invalid: ");
6591 name_of(state->errout, invalid);
6592 fprintf(state->errout, "\n");
6593 internal_error(state, 0, "bad input type?");
6594 }
6595#endif
6596
6597 size = reg_size_of(state, type);
6598 if (reg_offset > size) {
6599 member = 0;
6600 fprintf(state->errout, "type: ");
6601 name_of(state->errout, type);
6602 fprintf(state->errout, "\n");
6603 internal_error(state, 0, "offset outside of type");
6604 }
6605 else {
6606 switch(type->type & TYPE_MASK) {
6607 /* Don't do anything with the basic types */
6608 case TYPE_VOID:
6609 case TYPE_CHAR: case TYPE_UCHAR:
6610 case TYPE_SHORT: case TYPE_USHORT:
6611 case TYPE_INT: case TYPE_UINT:
6612 case TYPE_LONG: case TYPE_ULONG:
6613 case TYPE_LLONG: case TYPE_ULLONG:
6614 case TYPE_FLOAT: case TYPE_DOUBLE:
6615 case TYPE_LDOUBLE:
6616 case TYPE_POINTER:
6617 case TYPE_ENUM:
6618 case TYPE_BITFIELD:
6619 member = type;
6620 break;
6621 case TYPE_ARRAY:
6622 member = type->left;
6623 size = reg_size_of(state, member);
6624 if (size > REG_SIZEOF_REG) {
6625 member = reg_type(state, member, reg_offset % size);
6626 }
6627 break;
6628 case TYPE_STRUCT:
6629 case TYPE_TUPLE:
6630 {
6631 size_t offset;
6632 offset = 0;
6633 member = type->left;
6634 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6635 size = reg_size_of(state, member->left);
6636 offset += reg_needed_padding(state, member->left, offset);
6637 if ((offset + size) > reg_offset) {
6638 member = member->left;
6639 break;
6640 }
6641 offset += size;
6642 member = member->right;
6643 }
6644 offset += reg_needed_padding(state, member, offset);
6645 member = reg_type(state, member, reg_offset - offset);
6646 break;
6647 }
6648 case TYPE_UNION:
6649 case TYPE_JOIN:
6650 {
6651 struct type *join, **jnext, *mnext;
6652 join = new_type(TYPE_JOIN, 0, 0);
6653 jnext = &join->left;
6654 mnext = type->left;
6655 while(mnext) {
6656 size_t size;
6657 member = mnext;
6658 mnext = 0;
6659 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6660 mnext = member->right;
6661 member = member->left;
6662 }
6663 size = reg_size_of(state, member);
6664 if (size > reg_offset) {
6665 struct type *part, *hunt;
6666 part = reg_type(state, member, reg_offset);
6667 /* See if this type is already in the union */
6668 hunt = join->left;
6669 while(hunt) {
6670 struct type *test = hunt;
6671 hunt = 0;
6672 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6673 hunt = test->right;
6674 test = test->left;
6675 }
6676 if (equiv_types(part, test)) {
6677 goto next;
6678 }
6679 }
6680 /* Nope add it */
6681 if (!*jnext) {
6682 *jnext = part;
6683 } else {
6684 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6685 jnext = &(*jnext)->right;
6686 }
6687 join->elements++;
6688 }
6689 next:
6690 ;
6691 }
6692 if (join->elements == 0) {
6693 internal_error(state, 0, "No elements?");
6694 }
6695 member = join;
6696 break;
6697 }
6698 default:
6699 member = 0;
6700 fprintf(state->errout, "type: ");
6701 name_of(state->errout, type);
6702 fprintf(state->errout, "\n");
6703 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006704
Eric Biederman90089602004-05-28 14:11:54 +00006705 }
6706 }
6707 /* If I have a single register compound type not a bit-field
6708 * find the real type.
6709 */
6710 member = unpack_type(state, member);
6711 ;
6712 size = reg_size_of(state, member);
6713 if (size > REG_SIZEOF_REG) {
6714 internal_error(state, 0, "Cannot find type of single register");
6715 }
6716#if 1
6717 invalid = invalid_type(state, member);
6718 if (invalid) {
6719 fprintf(state->errout, "type: ");
6720 name_of(state->errout, member);
6721 fprintf(state->errout, "\n");
6722 fprintf(state->errout, "invalid: ");
6723 name_of(state->errout, invalid);
6724 fprintf(state->errout, "\n");
6725 internal_error(state, 0, "returning bad type?");
6726 }
6727#endif
6728 return member;
6729}
6730
Eric Biederman03b59862003-06-24 14:27:37 +00006731static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006732 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006733{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006734 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006735 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6736 internal_error(state, 0, "next_field only works on structures");
6737 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006738 member = type->left;
6739 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006740 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006741 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006742 break;
6743 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006744 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006745 prev_member = 0;
6746 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006747 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006748 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006749 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006750 prev_member = 0;
6751 }
6752 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006753 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006754 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006755 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006756 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006757}
6758
Stefan Reinauer14e22772010-04-27 06:56:47 +00006759typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006760 size_t ret_offset, size_t mem_offset, void *arg);
6761
6762static void walk_type_fields(struct compile_state *state,
6763 struct type *type, size_t reg_offset, size_t mem_offset,
6764 walk_type_fields_cb_t cb, void *arg);
6765
6766static void walk_struct_fields(struct compile_state *state,
6767 struct type *type, size_t reg_offset, size_t mem_offset,
6768 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006769{
Eric Biederman90089602004-05-28 14:11:54 +00006770 struct type *tptr;
6771 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006772 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006773 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006774 }
Eric Biederman90089602004-05-28 14:11:54 +00006775 tptr = type->left;
6776 for(i = 0; i < type->elements; i++) {
6777 struct type *mtype;
6778 mtype = tptr;
6779 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6780 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006781 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006782 walk_type_fields(state, mtype,
6783 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006784 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006785 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006786 field_offset(state, type, mtype->field_ident),
6787 cb, arg);
6788 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006789 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006790
Eric Biederman90089602004-05-28 14:11:54 +00006791}
6792
6793static void walk_type_fields(struct compile_state *state,
6794 struct type *type, size_t reg_offset, size_t mem_offset,
6795 walk_type_fields_cb_t cb, void *arg)
6796{
6797 switch(type->type & TYPE_MASK) {
6798 case TYPE_STRUCT:
6799 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6800 break;
6801 case TYPE_CHAR:
6802 case TYPE_UCHAR:
6803 case TYPE_SHORT:
6804 case TYPE_USHORT:
6805 case TYPE_INT:
6806 case TYPE_UINT:
6807 case TYPE_LONG:
6808 case TYPE_ULONG:
6809 cb(state, type, reg_offset, mem_offset, arg);
6810 break;
6811 case TYPE_VOID:
6812 break;
6813 default:
6814 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006815 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006816}
6817
Eric Biedermanb138ac82003-04-22 18:44:01 +00006818static void arrays_complete(struct compile_state *state, struct type *type)
6819{
6820 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6821 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6822 error(state, 0, "array size not specified");
6823 }
6824 arrays_complete(state, type->left);
6825 }
6826}
6827
Eric Biederman90089602004-05-28 14:11:54 +00006828static unsigned int get_basic_type(struct type *type)
6829{
6830 unsigned int basic;
6831 basic = type->type & TYPE_MASK;
6832 /* Convert enums to ints */
6833 if (basic == TYPE_ENUM) {
6834 basic = TYPE_INT;
6835 }
6836 /* Convert bitfields to standard types */
6837 else if (basic == TYPE_BITFIELD) {
6838 if (type->elements <= SIZEOF_CHAR) {
6839 basic = TYPE_CHAR;
6840 }
6841 else if (type->elements <= SIZEOF_SHORT) {
6842 basic = TYPE_SHORT;
6843 }
6844 else if (type->elements <= SIZEOF_INT) {
6845 basic = TYPE_INT;
6846 }
6847 else if (type->elements <= SIZEOF_LONG) {
6848 basic = TYPE_LONG;
6849 }
6850 if (!TYPE_SIGNED(type->left->type)) {
6851 basic += 1;
6852 }
6853 }
6854 return basic;
6855}
6856
Eric Biedermanb138ac82003-04-22 18:44:01 +00006857static unsigned int do_integral_promotion(unsigned int type)
6858{
Eric Biederman83b991a2003-10-11 06:20:25 +00006859 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006860 type = TYPE_INT;
6861 }
6862 return type;
6863}
6864
6865static unsigned int do_arithmetic_conversion(
6866 unsigned int left, unsigned int right)
6867{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006868 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6869 return TYPE_LDOUBLE;
6870 }
6871 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6872 return TYPE_DOUBLE;
6873 }
6874 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6875 return TYPE_FLOAT;
6876 }
6877 left = do_integral_promotion(left);
6878 right = do_integral_promotion(right);
6879 /* If both operands have the same size done */
6880 if (left == right) {
6881 return left;
6882 }
6883 /* If both operands have the same signedness pick the larger */
6884 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6885 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6886 }
6887 /* If the signed type can hold everything use it */
6888 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6889 return left;
6890 }
6891 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6892 return right;
6893 }
6894 /* Convert to the unsigned type with the same rank as the signed type */
6895 else if (TYPE_SIGNED(left)) {
6896 return TYPE_MKUNSIGNED(left);
6897 }
6898 else {
6899 return TYPE_MKUNSIGNED(right);
6900 }
6901}
6902
6903/* see if two types are the same except for qualifiers */
6904static int equiv_types(struct type *left, struct type *right)
6905{
6906 unsigned int type;
6907 /* Error if the basic types do not match */
6908 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6909 return 0;
6910 }
6911 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006912 /* If the basic types match and it is a void type we are done */
6913 if (type == TYPE_VOID) {
6914 return 1;
6915 }
Eric Biederman90089602004-05-28 14:11:54 +00006916 /* For bitfields we need to compare the sizes */
6917 else if (type == TYPE_BITFIELD) {
6918 return (left->elements == right->elements) &&
6919 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6920 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006921 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006922 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006923 return 1;
6924 }
6925 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006926 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006927 return equiv_types(left->left, right->left);
6928 }
6929 else if (type == TYPE_ARRAY) {
6930 return (left->elements == right->elements) &&
6931 equiv_types(left->left, right->left);
6932 }
Eric Biederman90089602004-05-28 14:11:54 +00006933 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006934 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006935 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006936 }
Eric Biederman90089602004-05-28 14:11:54 +00006937 /* test for union equality */
6938 else if (type == TYPE_UNION) {
6939 return left->type_ident == right->type_ident;
6940 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006941 /* Test for equivalent functions */
6942 else if (type == TYPE_FUNCTION) {
6943 return equiv_types(left->left, right->left) &&
6944 equiv_types(left->right, right->right);
6945 }
6946 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006947 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006948 else if (type == TYPE_PRODUCT) {
6949 return equiv_types(left->left, right->left) &&
6950 equiv_types(left->right, right->right);
6951 }
Eric Biederman90089602004-05-28 14:11:54 +00006952 /* We should see TYPE_OVERLAP when comparing joins */
6953 else if (type == TYPE_OVERLAP) {
6954 return equiv_types(left->left, right->left) &&
6955 equiv_types(left->right, right->right);
6956 }
6957 /* Test for equivalence of tuples */
6958 else if (type == TYPE_TUPLE) {
6959 return (left->elements == right->elements) &&
6960 equiv_types(left->left, right->left);
6961 }
6962 /* Test for equivalence of joins */
6963 else if (type == TYPE_JOIN) {
6964 return (left->elements == right->elements) &&
6965 equiv_types(left->left, right->left);
6966 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006967 else {
6968 return 0;
6969 }
6970}
6971
6972static int equiv_ptrs(struct type *left, struct type *right)
6973{
6974 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6975 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6976 return 0;
6977 }
6978 return equiv_types(left->left, right->left);
6979}
6980
6981static struct type *compatible_types(struct type *left, struct type *right)
6982{
6983 struct type *result;
6984 unsigned int type, qual_type;
6985 /* Error if the basic types do not match */
6986 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6987 return 0;
6988 }
6989 type = left->type & TYPE_MASK;
6990 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6991 result = 0;
6992 /* if the basic types match and it is an arithmetic type we are done */
6993 if (TYPE_ARITHMETIC(type)) {
6994 result = new_type(qual_type, 0, 0);
6995 }
6996 /* If it is a pointer type recurse and keep testing */
6997 else if (type == TYPE_POINTER) {
6998 result = compatible_types(left->left, right->left);
6999 if (result) {
7000 result = new_type(qual_type, result, 0);
7001 }
7002 }
Eric Biederman90089602004-05-28 14:11:54 +00007003 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007004 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007005 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007006 result = left;
7007 }
7008 }
Eric Biederman90089602004-05-28 14:11:54 +00007009 /* test for union equality */
7010 else if (type == TYPE_UNION) {
7011 if (left->type_ident == right->type_ident) {
7012 result = left;
7013 }
7014 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007015 /* Test for equivalent functions */
7016 else if (type == TYPE_FUNCTION) {
7017 struct type *lf, *rf;
7018 lf = compatible_types(left->left, right->left);
7019 rf = compatible_types(left->right, right->right);
7020 if (lf && rf) {
7021 result = new_type(qual_type, lf, rf);
7022 }
7023 }
7024 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7025 else if (type == TYPE_PRODUCT) {
7026 struct type *lf, *rf;
7027 lf = compatible_types(left->left, right->left);
7028 rf = compatible_types(left->right, right->right);
7029 if (lf && rf) {
7030 result = new_type(qual_type, lf, rf);
7031 }
7032 }
7033 else {
7034 /* Nothing else is compatible */
7035 }
7036 return result;
7037}
7038
Eric Biederman90089602004-05-28 14:11:54 +00007039/* See if left is a equivalent to right or right is a union member of left */
7040static int is_subset_type(struct type *left, struct type *right)
7041{
7042 if (equiv_types(left, right)) {
7043 return 1;
7044 }
7045 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7046 struct type *member, *mnext;
7047 mnext = left->left;
7048 while(mnext) {
7049 member = mnext;
7050 mnext = 0;
7051 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7052 mnext = member->right;
7053 member = member->left;
7054 }
7055 if (is_subset_type( member, right)) {
7056 return 1;
7057 }
7058 }
7059 }
7060 return 0;
7061}
7062
Eric Biedermanb138ac82003-04-22 18:44:01 +00007063static struct type *compatible_ptrs(struct type *left, struct type *right)
7064{
7065 struct type *result;
7066 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7067 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7068 return 0;
7069 }
7070 result = compatible_types(left->left, right->left);
7071 if (result) {
7072 unsigned int qual_type;
7073 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7074 result = new_type(qual_type, result, 0);
7075 }
7076 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007077
Eric Biedermanb138ac82003-04-22 18:44:01 +00007078}
7079static struct triple *integral_promotion(
7080 struct compile_state *state, struct triple *def)
7081{
7082 struct type *type;
7083 type = def->type;
7084 /* As all operations are carried out in registers
7085 * the values are converted on load I just convert
7086 * logical type of the operand.
7087 */
7088 if (TYPE_INTEGER(type->type)) {
7089 unsigned int int_type;
7090 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007091 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007092 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007093 if (def->op != OP_LOAD) {
7094 def->type = new_type(int_type, 0, 0);
7095 }
7096 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007097 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007098 new_type(int_type, 0, 0), def, 0);
7099 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007100 }
7101 }
7102 return def;
7103}
7104
7105
7106static void arithmetic(struct compile_state *state, struct triple *def)
7107{
7108 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007109 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007110 }
7111}
7112
7113static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7114{
7115 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7116 error(state, def, "pointer or arithmetic type expected");
7117 }
7118}
7119
7120static int is_integral(struct triple *ins)
7121{
7122 return TYPE_INTEGER(ins->type->type);
7123}
7124
7125static void integral(struct compile_state *state, struct triple *def)
7126{
7127 if (!is_integral(def)) {
7128 error(state, 0, "integral type expected");
7129 }
7130}
7131
7132
7133static void bool(struct compile_state *state, struct triple *def)
7134{
7135 if (!TYPE_ARITHMETIC(def->type->type) &&
7136 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7137 error(state, 0, "arithmetic or pointer type expected");
7138 }
7139}
7140
7141static int is_signed(struct type *type)
7142{
Eric Biederman90089602004-05-28 14:11:54 +00007143 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7144 type = type->left;
7145 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007146 return !!TYPE_SIGNED(type->type);
7147}
Eric Biederman90089602004-05-28 14:11:54 +00007148static int is_compound_type(struct type *type)
7149{
7150 int is_compound;
7151 switch((type->type & TYPE_MASK)) {
7152 case TYPE_ARRAY:
7153 case TYPE_STRUCT:
7154 case TYPE_TUPLE:
7155 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007156 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007157 is_compound = 1;
7158 break;
7159 default:
7160 is_compound = 0;
7161 break;
7162 }
7163 return is_compound;
7164}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007165
Eric Biederman0babc1c2003-05-09 02:39:00 +00007166/* Is this value located in a register otherwise it must be in memory */
7167static int is_in_reg(struct compile_state *state, struct triple *def)
7168{
7169 int in_reg;
7170 if (def->op == OP_ADECL) {
7171 in_reg = 1;
7172 }
7173 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7174 in_reg = 0;
7175 }
Eric Biederman90089602004-05-28 14:11:54 +00007176 else if (triple_is_part(state, def)) {
7177 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007178 }
7179 else {
Eric Biederman90089602004-05-28 14:11:54 +00007180 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007181 in_reg = -1;
7182 }
7183 return in_reg;
7184}
7185
Eric Biederman90089602004-05-28 14:11:54 +00007186/* Is this an auto or static variable location? Something that can
7187 * be assigned to. Otherwise it must must be a pure value, a temporary.
7188 */
7189static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007190{
7191 int ret;
7192 ret = 0;
7193 if (!def) {
7194 return 0;
7195 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007196 if ((def->op == OP_ADECL) ||
7197 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007198 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007199 (def->op == OP_BLOBCONST) ||
7200 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007201 ret = 1;
7202 }
Eric Biederman90089602004-05-28 14:11:54 +00007203 else if (triple_is_part(state, def)) {
7204 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007205 }
7206 return ret;
7207}
7208
Eric Biederman00443072003-06-24 12:34:45 +00007209static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007210{
7211 if (!def) {
7212 internal_error(state, def, "nothing where lvalue expected?");
7213 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007214 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007215 error(state, def, "lvalue expected");
7216 }
7217}
Eric Biederman00443072003-06-24 12:34:45 +00007218static void lvalue(struct compile_state *state, struct triple *def)
7219{
7220 clvalue(state, def);
7221 if (def->type->type & QUAL_CONST) {
7222 error(state, def, "modifable lvalue expected");
7223 }
7224}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007225
7226static int is_pointer(struct triple *def)
7227{
7228 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7229}
7230
7231static void pointer(struct compile_state *state, struct triple *def)
7232{
7233 if (!is_pointer(def)) {
7234 error(state, def, "pointer expected");
7235 }
7236}
7237
7238static struct triple *int_const(
7239 struct compile_state *state, struct type *type, ulong_t value)
7240{
7241 struct triple *result;
7242 switch(type->type & TYPE_MASK) {
7243 case TYPE_CHAR:
7244 case TYPE_INT: case TYPE_UINT:
7245 case TYPE_LONG: case TYPE_ULONG:
7246 break;
7247 default:
Eric Biederman90089602004-05-28 14:11:54 +00007248 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007249 }
7250 result = triple(state, OP_INTCONST, type, 0, 0);
7251 result->u.cval = value;
7252 return result;
7253}
7254
7255
Eric Biederman83b991a2003-10-11 06:20:25 +00007256static struct triple *read_expr(struct compile_state *state, struct triple *def);
7257
Stefan Reinauer14e22772010-04-27 06:56:47 +00007258static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007259 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007260{
7261 struct triple *result;
Eric Biederman00443072003-06-24 12:34:45 +00007262 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007263
Stefan Reinauer14e22772010-04-27 06:56:47 +00007264
Eric Biedermanb138ac82003-04-22 18:44:01 +00007265 result = 0;
7266 if (expr->op == OP_ADECL) {
7267 error(state, expr, "address of auto variables not supported");
7268 }
7269 else if (expr->op == OP_SDECL) {
Patrick Georgid0571252017-01-02 19:24:48 +01007270 struct type *ptr_type;
7271 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
7272
Eric Biederman90089602004-05-28 14:11:54 +00007273 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007274 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007275 result->u.cval = offset;
7276 }
7277 else if (expr->op == OP_DEREF) {
Patrick Georgid0571252017-01-02 19:24:48 +01007278 struct type *ptr_type;
7279 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
7280
Eric Biederman90089602004-05-28 14:11:54 +00007281 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007282 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007283 int_const(state, &ulong_type, offset));
7284 }
Eric Biederman90089602004-05-28 14:11:54 +00007285 else if (expr->op == OP_BLOBCONST) {
7286 FINISHME();
7287 internal_error(state, expr, "not yet implemented");
7288 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007289 else if (expr->op == OP_LIST) {
7290 error(state, 0, "Function addresses not supported");
7291 }
Eric Biederman90089602004-05-28 14:11:54 +00007292 else if (triple_is_part(state, expr)) {
7293 struct triple *part;
7294 part = expr;
7295 expr = MISC(expr, 0);
7296 if (part->op == OP_DOT) {
7297 offset += bits_to_bytes(
7298 field_offset(state, expr->type, part->u.field));
7299 }
7300 else if (part->op == OP_INDEX) {
7301 offset += bits_to_bytes(
7302 index_offset(state, expr->type, part->u.cval));
7303 }
7304 else {
7305 internal_error(state, part, "unhandled part type");
7306 }
7307 result = do_mk_addr_expr(state, expr, type, offset);
7308 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007309 if (!result) {
7310 internal_error(state, expr, "cannot take address of expression");
7311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007312 return result;
7313}
7314
Eric Biederman0babc1c2003-05-09 02:39:00 +00007315static struct triple *mk_addr_expr(
7316 struct compile_state *state, struct triple *expr, ulong_t offset)
7317{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007318 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007319}
7320
Eric Biedermanb138ac82003-04-22 18:44:01 +00007321static struct triple *mk_deref_expr(
7322 struct compile_state *state, struct triple *expr)
7323{
7324 struct type *base_type;
7325 pointer(state, expr);
7326 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007327 return triple(state, OP_DEREF, base_type, expr, 0);
7328}
7329
Eric Biederman90089602004-05-28 14:11:54 +00007330/* lvalue conversions always apply except when certain operators
7331 * are applied. So I apply apply it when I know no more
7332 * operators will be applied.
7333 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007334static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007335{
Eric Biederman5cd81732004-03-11 15:01:31 +00007336 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007337 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7338 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007339 type = new_type(
7340 TYPE_POINTER | (def->type->type & QUAL_MASK),
7341 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007342 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007343 struct triple *addrconst;
7344 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7345 internal_error(state, def, "bad array constant");
7346 }
7347 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7348 MISC(addrconst, 0) = def;
7349 def = addrconst;
7350 }
7351 else {
Eric Biederman90089602004-05-28 14:11:54 +00007352 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007353 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007354 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007355 /* Transform a function to a pointer to it */
7356 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7357 def = mk_addr_expr(state, def, 0);
7358 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007359 return def;
7360}
7361
Eric Biederman0babc1c2003-05-09 02:39:00 +00007362static struct triple *deref_field(
7363 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7364{
7365 struct triple *result;
7366 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007367 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007368 if (!field) {
7369 internal_error(state, 0, "No field passed to deref_field");
7370 }
7371 result = 0;
7372 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007373 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7374 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007375 error(state, 0, "request for member %s in something not a struct or union",
7376 field->name);
7377 }
Eric Biederman03b59862003-06-24 14:27:37 +00007378 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007379 if ((type->type & STOR_MASK) == STOR_PERM) {
7380 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007381 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007382 result = do_mk_addr_expr(state, expr, member, offset);
7383 result = mk_deref_expr(state, result);
7384 }
7385 else {
7386 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007387 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007388 result->u.field = field;
7389 }
7390 return result;
7391}
7392
Eric Biederman90089602004-05-28 14:11:54 +00007393static struct triple *deref_index(
7394 struct compile_state *state, struct triple *expr, size_t index)
7395{
7396 struct triple *result;
7397 struct type *type, *member;
7398 ulong_t offset;
7399
7400 result = 0;
7401 type = expr->type;
7402 member = index_type(state, type, index);
7403
7404 if ((type->type & STOR_MASK) == STOR_PERM) {
7405 offset = bits_to_bytes(index_offset(state, type, index));
7406 result = do_mk_addr_expr(state, expr, member, offset);
7407 result = mk_deref_expr(state, result);
7408 }
7409 else {
7410 result = triple(state, OP_INDEX, member, expr, 0);
7411 result->u.cval = index;
7412 }
7413 return result;
7414}
7415
Eric Biedermanb138ac82003-04-22 18:44:01 +00007416static struct triple *read_expr(struct compile_state *state, struct triple *def)
7417{
7418 int op;
7419 if (!def) {
7420 return 0;
7421 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007422#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007423#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007424#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007425 /* Transform lvalues into something we can read */
7426 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007427 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007428 return def;
7429 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007430 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007431 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007432 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007433 if (def->op == OP_SDECL) {
7434 def = mk_addr_expr(state, def, 0);
7435 def = mk_deref_expr(state, def);
7436 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007437 op = OP_LOAD;
7438 }
Eric Biederman90089602004-05-28 14:11:54 +00007439 def = triple(state, op, def->type, def, 0);
7440 if (def->type->type & QUAL_VOLATILE) {
7441 def->id |= TRIPLE_FLAG_VOLATILE;
7442 }
7443 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007444}
7445
Stefan Reinauer14e22772010-04-27 06:56:47 +00007446int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007447 struct type *dest, struct type *rval)
7448{
7449 int compatible = 0;
7450 /* Both operands have arithmetic type */
7451 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7452 compatible = 1;
7453 }
7454 /* One operand is a pointer and the other is a pointer to void */
7455 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7456 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7457 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7458 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7459 compatible = 1;
7460 }
7461 /* If both types are the same without qualifiers we are good */
7462 else if (equiv_ptrs(dest, rval)) {
7463 compatible = 1;
7464 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007465 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007466 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007467 compatible = 1;
7468 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007469 return compatible;
7470}
7471
Eric Biedermane058a1e2003-07-12 01:21:31 +00007472static void write_compatible(struct compile_state *state,
7473 struct type *dest, struct type *rval)
7474{
7475 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007476 FILE *fp = state->errout;
7477 fprintf(fp, "dest: ");
7478 name_of(fp, dest);
7479 fprintf(fp,"\nrval: ");
7480 name_of(fp, rval);
7481 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007482 error(state, 0, "Incompatible types in assignment");
7483 }
7484}
7485
Eric Biedermane058a1e2003-07-12 01:21:31 +00007486static int is_init_compatible(struct compile_state *state,
7487 struct type *dest, struct type *rval)
7488{
7489 int compatible = 0;
7490 if (is_write_compatible(state, dest, rval)) {
7491 compatible = 1;
7492 }
7493 else if (equiv_types(dest, rval)) {
7494 compatible = 1;
7495 }
7496 return compatible;
7497}
7498
Eric Biedermanb138ac82003-04-22 18:44:01 +00007499static struct triple *write_expr(
7500 struct compile_state *state, struct triple *dest, struct triple *rval)
7501{
7502 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007503
7504 def = 0;
7505 if (!rval) {
7506 internal_error(state, 0, "missing rval");
7507 }
7508
7509 if (rval->op == OP_LIST) {
7510 internal_error(state, 0, "expression of type OP_LIST?");
7511 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007512 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007513 internal_error(state, 0, "writing to a non lvalue?");
7514 }
Eric Biederman00443072003-06-24 12:34:45 +00007515 if (dest->type->type & QUAL_CONST) {
7516 internal_error(state, 0, "modifable lvalue expexted");
7517 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007518
7519 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007520 if (!equiv_types(dest->type, rval->type)) {
7521 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007523
7524 /* Now figure out which assignment operator to use */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007525 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007526 def = triple(state, OP_WRITE, dest->type, rval, dest);
7527 if (MISC(def, 0) != dest) {
7528 internal_error(state, def, "huh?");
7529 }
7530 if (RHS(def, 0) != rval) {
7531 internal_error(state, def, "huh?");
7532 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007533 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007534 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007535 }
Eric Biederman90089602004-05-28 14:11:54 +00007536 if (def->type->type & QUAL_VOLATILE) {
7537 def->id |= TRIPLE_FLAG_VOLATILE;
7538 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007539 return def;
7540}
7541
7542static struct triple *init_expr(
7543 struct compile_state *state, struct triple *dest, struct triple *rval)
7544{
7545 struct triple *def;
7546
7547 def = 0;
7548 if (!rval) {
7549 internal_error(state, 0, "missing rval");
7550 }
7551 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7552 rval = read_expr(state, rval);
7553 def = write_expr(state, dest, rval);
7554 }
7555 else {
7556 /* Fill in the array size if necessary */
7557 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7558 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7559 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7560 dest->type->elements = rval->type->elements;
7561 }
7562 }
7563 if (!equiv_types(dest->type, rval->type)) {
7564 error(state, 0, "Incompatible types in inializer");
7565 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007566 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007567 insert_triple(state, dest, rval);
7568 rval->id |= TRIPLE_FLAG_FLATTENED;
7569 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007570 }
7571 return def;
7572}
7573
7574struct type *arithmetic_result(
7575 struct compile_state *state, struct triple *left, struct triple *right)
7576{
7577 struct type *type;
7578 /* Sanity checks to ensure I am working with arithmetic types */
7579 arithmetic(state, left);
7580 arithmetic(state, right);
7581 type = new_type(
7582 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007583 get_basic_type(left->type),
7584 get_basic_type(right->type)),
7585 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007586 return type;
7587}
7588
7589struct type *ptr_arithmetic_result(
7590 struct compile_state *state, struct triple *left, struct triple *right)
7591{
7592 struct type *type;
7593 /* Sanity checks to ensure I am working with the proper types */
7594 ptr_arithmetic(state, left);
7595 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007596 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007597 TYPE_ARITHMETIC(right->type->type)) {
7598 type = arithmetic_result(state, left, right);
7599 }
7600 else if (TYPE_PTR(left->type->type)) {
7601 type = left->type;
7602 }
7603 else {
7604 internal_error(state, 0, "huh?");
7605 type = 0;
7606 }
7607 return type;
7608}
7609
Eric Biedermanb138ac82003-04-22 18:44:01 +00007610/* boolean helper function */
7611
Stefan Reinauer14e22772010-04-27 06:56:47 +00007612static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007613 struct triple *expr)
7614{
7615 switch(expr->op) {
7616 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7617 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7618 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7619 /* If the expression is already boolean do nothing */
7620 break;
7621 default:
7622 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7623 break;
7624 }
7625 return expr;
7626}
7627
Stefan Reinauer14e22772010-04-27 06:56:47 +00007628static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007629 struct triple *expr)
7630{
7631 return triple(state, OP_LFALSE, &int_type, expr, 0);
7632}
7633
Eric Biederman90089602004-05-28 14:11:54 +00007634static struct triple *mkland_expr(
7635 struct compile_state *state,
7636 struct triple *left, struct triple *right)
7637{
7638 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007639 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007640
7641 /* Generate some intermediate triples */
7642 end = label(state);
7643 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007644
Eric Biederman90089602004-05-28 14:11:54 +00007645 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007646 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007647
7648 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007649 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007650 lfalse_expr(state, read_expr(state, var)));
7651 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007652
Eric Biederman90089602004-05-28 14:11:54 +00007653 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007654 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007655
7656 /* An expression for the computed value */
7657 val = read_expr(state, var);
7658
7659 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007660 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007661
Eric Biederman90089602004-05-28 14:11:54 +00007662 return def;
7663}
7664
7665static struct triple *mklor_expr(
7666 struct compile_state *state,
7667 struct triple *left, struct triple *right)
7668{
7669 struct triple *def, *val, *var, *jmp, *mid, *end;
7670
7671 /* Generate some intermediate triples */
7672 end = label(state);
7673 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007674
Eric Biederman90089602004-05-28 14:11:54 +00007675 /* Store the left hand side value */
7676 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007677
Eric Biederman90089602004-05-28 14:11:54 +00007678 /* Jump if the value is true */
7679 jmp = branch(state, end, read_expr(state, var));
7680 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007681
Eric Biederman90089602004-05-28 14:11:54 +00007682 /* Store the right hand side value */
7683 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007684
Eric Biederman90089602004-05-28 14:11:54 +00007685 /* An expression for the computed value*/
7686 val = read_expr(state, var);
7687
7688 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007689 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007690
7691 return def;
7692}
7693
7694static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007695 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007696 struct triple *test, struct triple *left, struct triple *right)
7697{
Eric Biederman90089602004-05-28 14:11:54 +00007698 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007699 struct type *result_type;
7700 unsigned int left_type, right_type;
7701 bool(state, test);
7702 left_type = left->type->type;
7703 right_type = right->type->type;
7704 result_type = 0;
7705 /* Both operands have arithmetic type */
7706 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7707 result_type = arithmetic_result(state, left, right);
7708 }
7709 /* Both operands have void type */
7710 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7711 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7712 result_type = &void_type;
7713 }
7714 /* pointers to the same type... */
7715 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7716 ;
7717 }
7718 /* Both operands are pointers and left is a pointer to void */
7719 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7720 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7721 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7722 result_type = right->type;
7723 }
7724 /* Both operands are pointers and right is a pointer to void */
7725 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7726 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7727 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7728 result_type = left->type;
7729 }
7730 if (!result_type) {
7731 error(state, 0, "Incompatible types in conditional expression");
7732 }
Eric Biederman90089602004-05-28 14:11:54 +00007733 /* Generate some intermediate triples */
7734 mid = label(state);
7735 end = label(state);
7736 var = variable(state, result_type);
7737
7738 /* Branch if the test is false */
7739 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7740 top = label(state);
7741
7742 /* Store the left hand side value */
7743 left = write_expr(state, var, left);
7744
7745 /* Branch to the end */
7746 jmp2 = branch(state, end, 0);
7747
7748 /* Store the right hand side value */
7749 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007750
Eric Biederman90089602004-05-28 14:11:54 +00007751 /* An expression for the computed value */
7752 val = read_expr(state, var);
7753
7754 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007755 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007756
Eric Biedermanb138ac82003-04-22 18:44:01 +00007757 return def;
7758}
7759
7760
Eric Biederman0babc1c2003-05-09 02:39:00 +00007761static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007762{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007763#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007764#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007765#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007766 int count;
7767 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007768 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7769 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007770 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007771 else if (ins->op == OP_DEREF) {
7772 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007773 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007774 else if (ins->op == OP_VAL) {
7775 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007776 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007777 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007778 /* Don't figure the depth of a call just guess it is huge */
7779 count = 1000;
7780 }
7781 else {
7782 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007783 expr = triple_rhs(state, ins, 0);
7784 for(;expr; expr = triple_rhs(state, ins, expr)) {
7785 if (*expr) {
7786 int depth;
7787 depth = expr_depth(state, *expr);
7788 if (depth > count) {
7789 count = depth;
7790 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007791 }
7792 }
7793 }
7794 return count + 1;
7795}
7796
Eric Biederman0babc1c2003-05-09 02:39:00 +00007797static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007798 struct compile_state *state, struct triple *first, struct triple *ptr,
7799 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007800{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007801 struct rhs_vector {
7802 int depth;
7803 struct triple **ins;
7804 } vector[MAX_RHS];
7805 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007806 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007807 rhs = ptr->rhs;
7808 lhs = ptr->lhs;
7809 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007810 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007811 ptr->op, tops(ptr->op));
7812 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007813 /* Find the depth of the rhs elements */
7814 for(i = 0; i < rhs; i++) {
7815 vector[i].ins = &RHS(ptr, i);
7816 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007817 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007818 /* Selection sort the rhs */
7819 for(i = 0; i < rhs; i++) {
7820 int j, max = i;
7821 for(j = i + 1; j < rhs; j++ ) {
7822 if (vector[j].depth > vector[max].depth) {
7823 max = j;
7824 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007825 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007826 if (max != i) {
7827 struct rhs_vector tmp;
7828 tmp = vector[i];
7829 vector[i] = vector[max];
7830 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007831 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007832 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007833 /* Now flatten the rhs elements */
7834 for(i = 0; i < rhs; i++) {
7835 *vector[i].ins = flatten(state, first, *vector[i].ins);
7836 use_triple(*vector[i].ins, ptr);
7837 }
Eric Biederman90089602004-05-28 14:11:54 +00007838 if (lhs) {
7839 insert_triple(state, first, ptr);
7840 ptr->id |= TRIPLE_FLAG_FLATTENED;
7841 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007842
Eric Biederman90089602004-05-28 14:11:54 +00007843 /* Now flatten the lhs elements */
7844 for(i = 0; i < lhs; i++) {
7845 struct triple **ins = &LHS(ptr, i);
7846 *ins = flatten(state, first, *ins);
7847 use_triple(*ins, ptr);
7848 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007849 }
7850 return ptr;
7851}
7852
Eric Biederman90089602004-05-28 14:11:54 +00007853static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007854 struct compile_state *state, struct triple *first, struct triple *ptr)
7855{
Eric Biederman90089602004-05-28 14:11:54 +00007856 struct triple *head, *body, *val;
7857 head = RHS(ptr, 0);
7858 RHS(ptr, 0) = 0;
7859 val = head->prev;
7860 body = head->next;
7861 release_triple(state, head);
7862 release_triple(state, ptr);
7863 val->next = first;
7864 body->prev = first->prev;
7865 body->prev->next = body;
7866 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007867
Eric Biederman90089602004-05-28 14:11:54 +00007868 if (triple_is_cbranch(state, body->prev) ||
7869 triple_is_call(state, body->prev)) {
7870 unuse_triple(first, body->prev);
7871 use_triple(body, body->prev);
7872 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007873
Eric Biederman90089602004-05-28 14:11:54 +00007874 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7875 internal_error(state, val, "val not flattened?");
7876 }
7877
7878 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007879}
7880
Eric Biederman90089602004-05-28 14:11:54 +00007881
7882static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007883 struct compile_state *state, struct triple *first, struct triple *ptr)
7884{
Eric Biederman90089602004-05-28 14:11:54 +00007885 if (!triple_is_part(state, ptr)) {
7886 internal_error(state, ptr, "not a part");
7887 }
7888 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7889 internal_error(state, ptr, "unexpected args for: %d %s",
7890 ptr->op, tops(ptr->op));
7891 }
7892 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7893 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007894 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007895}
7896
7897static struct triple *flatten(
7898 struct compile_state *state, struct triple *first, struct triple *ptr)
7899{
7900 struct triple *orig_ptr;
7901 if (!ptr)
7902 return 0;
7903 do {
7904 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007905 /* Only flatten triples once */
7906 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7907 return ptr;
7908 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007909 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007910 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007911 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7912 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007913 break;
Eric Biederman90089602004-05-28 14:11:54 +00007914 case OP_PROG:
7915 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007916 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007917 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007918 ptr = flatten_generic(state, first, ptr, 1);
7919 insert_triple(state, first, ptr);
7920 ptr->id |= TRIPLE_FLAG_FLATTENED;
7921 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7922 if (ptr->next != ptr) {
7923 use_triple(ptr->next, ptr);
7924 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007925 break;
7926 case OP_READ:
7927 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007928 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7929 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007930 break;
Eric Biederman90089602004-05-28 14:11:54 +00007931 case OP_WRITE:
7932 ptr = flatten_generic(state, first, ptr, 1);
7933 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7934 use_triple(MISC(ptr, 0), ptr);
7935 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007936 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007937 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007938 break;
7939 case OP_CBRANCH:
7940 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7941 use_triple(RHS(ptr, 0), ptr);
7942 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007943 insert_triple(state, first, ptr);
7944 ptr->id |= TRIPLE_FLAG_FLATTENED;
7945 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007946 if (ptr->next != ptr) {
7947 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007948 }
7949 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007950 case OP_CALL:
7951 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7952 use_triple(MISC(ptr, 0), ptr);
7953 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007954 insert_triple(state, first, ptr);
7955 ptr->id |= TRIPLE_FLAG_FLATTENED;
7956 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007957 if (ptr->next != ptr) {
7958 use_triple(ptr->next, ptr);
7959 }
7960 break;
7961 case OP_RET:
7962 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7963 use_triple(RHS(ptr, 0), ptr);
7964 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007965 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007966 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007967 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007968 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007969 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007970 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007971 break;
7972 case OP_DEREF:
7973 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007974 ptr = RHS(ptr, 0);
7975 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007976 free_triple(state, orig_ptr);
7977 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007978 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007979 if (RHS(ptr, 0)->op == OP_DEREF) {
7980 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007981 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007982 base = MISC(ptr, 0);
7983 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007984 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007985 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007986 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007987 int_const(state, &ulong_type, offset));
7988 free_triple(state, base);
7989 }
Eric Biederman90089602004-05-28 14:11:54 +00007990 else {
7991 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007992 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007993 break;
Eric Biederman90089602004-05-28 14:11:54 +00007994 case OP_INDEX:
7995 if (RHS(ptr, 0)->op == OP_DEREF) {
7996 struct triple *base, *left;
7997 ulong_t offset;
7998 base = MISC(ptr, 0);
7999 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8000 left = RHS(base, 0);
8001 ptr = triple(state, OP_ADD, left->type,
8002 read_expr(state, left),
8003 int_const(state, &long_type, offset));
8004 free_triple(state, base);
8005 }
8006 else {
8007 ptr = flatten_part(state, first, ptr);
8008 }
8009 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008010 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008011 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008012 use_triple(ptr, MISC(ptr, 0));
8013 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008014 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008015 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8016 use_triple(MISC(ptr, 0), ptr);
8017 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008018 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008019 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008020 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8021 use_triple(MISC(ptr, 0), ptr);
8022 insert_triple(state, first, ptr);
8023 ptr->id |= TRIPLE_FLAG_FLATTENED;
8024 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8025 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008026 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008027 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008028 break;
8029 default:
8030 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008031 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008032 break;
8033 }
8034 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008035 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008036 insert_triple(state, first, ptr);
8037 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008038 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008039 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008040 return ptr;
8041}
8042
8043static void release_expr(struct compile_state *state, struct triple *expr)
8044{
8045 struct triple *head;
8046 head = label(state);
8047 flatten(state, head, expr);
8048 while(head->next != head) {
8049 release_triple(state, head->next);
8050 }
8051 free_triple(state, head);
8052}
8053
8054static int replace_rhs_use(struct compile_state *state,
8055 struct triple *orig, struct triple *new, struct triple *use)
8056{
8057 struct triple **expr;
8058 int found;
8059 found = 0;
8060 expr = triple_rhs(state, use, 0);
8061 for(;expr; expr = triple_rhs(state, use, expr)) {
8062 if (*expr == orig) {
8063 *expr = new;
8064 found = 1;
8065 }
8066 }
8067 if (found) {
8068 unuse_triple(orig, use);
8069 use_triple(new, use);
8070 }
8071 return found;
8072}
8073
8074static int replace_lhs_use(struct compile_state *state,
8075 struct triple *orig, struct triple *new, struct triple *use)
8076{
8077 struct triple **expr;
8078 int found;
8079 found = 0;
8080 expr = triple_lhs(state, use, 0);
8081 for(;expr; expr = triple_lhs(state, use, expr)) {
8082 if (*expr == orig) {
8083 *expr = new;
8084 found = 1;
8085 }
8086 }
8087 if (found) {
8088 unuse_triple(orig, use);
8089 use_triple(new, use);
8090 }
8091 return found;
8092}
8093
Eric Biederman90089602004-05-28 14:11:54 +00008094static int replace_misc_use(struct compile_state *state,
8095 struct triple *orig, struct triple *new, struct triple *use)
8096{
8097 struct triple **expr;
8098 int found;
8099 found = 0;
8100 expr = triple_misc(state, use, 0);
8101 for(;expr; expr = triple_misc(state, use, expr)) {
8102 if (*expr == orig) {
8103 *expr = new;
8104 found = 1;
8105 }
8106 }
8107 if (found) {
8108 unuse_triple(orig, use);
8109 use_triple(new, use);
8110 }
8111 return found;
8112}
8113
8114static int replace_targ_use(struct compile_state *state,
8115 struct triple *orig, struct triple *new, struct triple *use)
8116{
8117 struct triple **expr;
8118 int found;
8119 found = 0;
8120 expr = triple_targ(state, use, 0);
8121 for(;expr; expr = triple_targ(state, use, expr)) {
8122 if (*expr == orig) {
8123 *expr = new;
8124 found = 1;
8125 }
8126 }
8127 if (found) {
8128 unuse_triple(orig, use);
8129 use_triple(new, use);
8130 }
8131 return found;
8132}
8133
8134static void replace_use(struct compile_state *state,
8135 struct triple *orig, struct triple *new, struct triple *use)
8136{
8137 int found;
8138 found = 0;
8139 found |= replace_rhs_use(state, orig, new, use);
8140 found |= replace_lhs_use(state, orig, new, use);
8141 found |= replace_misc_use(state, orig, new, use);
8142 found |= replace_targ_use(state, orig, new, use);
8143 if (!found) {
8144 internal_error(state, use, "use without use");
8145 }
8146}
8147
Eric Biedermanb138ac82003-04-22 18:44:01 +00008148static void propogate_use(struct compile_state *state,
8149 struct triple *orig, struct triple *new)
8150{
8151 struct triple_set *user, *next;
8152 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008153 /* Careful replace_use modifies the use chain and
8154 * removes use. So we must get a copy of the next
8155 * entry early.
8156 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008157 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008158 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008159 }
8160 if (orig->use) {
8161 internal_error(state, orig, "used after propogate_use");
8162 }
8163}
8164
8165/*
8166 * Code generators
8167 * ===========================
8168 */
8169
Eric Biederman90089602004-05-28 14:11:54 +00008170static struct triple *mk_cast_expr(
8171 struct compile_state *state, struct type *type, struct triple *expr)
8172{
8173 struct triple *def;
8174 def = read_expr(state, expr);
8175 def = triple(state, OP_CONVERT, type, def, 0);
8176 return def;
8177}
8178
Eric Biedermanb138ac82003-04-22 18:44:01 +00008179static struct triple *mk_add_expr(
8180 struct compile_state *state, struct triple *left, struct triple *right)
8181{
8182 struct type *result_type;
8183 /* Put pointer operands on the left */
8184 if (is_pointer(right)) {
8185 struct triple *tmp;
8186 tmp = left;
8187 left = right;
8188 right = tmp;
8189 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008190 left = read_expr(state, left);
8191 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008192 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008193 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008194 struct type *ptr_math;
8195 int op;
8196 if (is_signed(right->type)) {
8197 ptr_math = &long_type;
8198 op = OP_SMUL;
8199 } else {
8200 ptr_math = &ulong_type;
8201 op = OP_UMUL;
8202 }
8203 if (!equiv_types(right->type, ptr_math)) {
8204 right = mk_cast_expr(state, ptr_math, right);
8205 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008206 right = triple(state, op, ptr_math, right,
8207 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008208 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008209 }
8210 return triple(state, OP_ADD, result_type, left, right);
8211}
8212
8213static struct triple *mk_sub_expr(
8214 struct compile_state *state, struct triple *left, struct triple *right)
8215{
8216 struct type *result_type;
8217 result_type = ptr_arithmetic_result(state, left, right);
8218 left = read_expr(state, left);
8219 right = read_expr(state, right);
8220 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008221 struct type *ptr_math;
8222 int op;
8223 if (is_signed(right->type)) {
8224 ptr_math = &long_type;
8225 op = OP_SMUL;
8226 } else {
8227 ptr_math = &ulong_type;
8228 op = OP_UMUL;
8229 }
8230 if (!equiv_types(right->type, ptr_math)) {
8231 right = mk_cast_expr(state, ptr_math, right);
8232 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008233 right = triple(state, op, ptr_math, right,
8234 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008235 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008236 }
8237 return triple(state, OP_SUB, result_type, left, right);
8238}
8239
8240static struct triple *mk_pre_inc_expr(
8241 struct compile_state *state, struct triple *def)
8242{
8243 struct triple *val;
8244 lvalue(state, def);
8245 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8246 return triple(state, OP_VAL, def->type,
8247 write_expr(state, def, val),
8248 val);
8249}
8250
8251static struct triple *mk_pre_dec_expr(
8252 struct compile_state *state, struct triple *def)
8253{
8254 struct triple *val;
8255 lvalue(state, def);
8256 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8257 return triple(state, OP_VAL, def->type,
8258 write_expr(state, def, val),
8259 val);
8260}
8261
8262static struct triple *mk_post_inc_expr(
8263 struct compile_state *state, struct triple *def)
8264{
8265 struct triple *val;
8266 lvalue(state, def);
8267 val = read_expr(state, def);
8268 return triple(state, OP_VAL, def->type,
8269 write_expr(state, def,
8270 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8271 , val);
8272}
8273
8274static struct triple *mk_post_dec_expr(
8275 struct compile_state *state, struct triple *def)
8276{
8277 struct triple *val;
8278 lvalue(state, def);
8279 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008280 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008281 write_expr(state, def,
8282 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8283 , val);
8284}
8285
8286static struct triple *mk_subscript_expr(
8287 struct compile_state *state, struct triple *left, struct triple *right)
8288{
8289 left = read_expr(state, left);
8290 right = read_expr(state, right);
8291 if (!is_pointer(left) && !is_pointer(right)) {
8292 error(state, left, "subscripted value is not a pointer");
8293 }
8294 return mk_deref_expr(state, mk_add_expr(state, left, right));
8295}
8296
Eric Biedermane058a1e2003-07-12 01:21:31 +00008297
Eric Biedermanb138ac82003-04-22 18:44:01 +00008298/*
8299 * Compile time evaluation
8300 * ===========================
8301 */
8302static int is_const(struct triple *ins)
8303{
8304 return IS_CONST_OP(ins->op);
8305}
8306
Eric Biederman83b991a2003-10-11 06:20:25 +00008307static int is_simple_const(struct triple *ins)
8308{
Eric Biederman90089602004-05-28 14:11:54 +00008309 /* Is this a constant that u.cval has the value.
8310 * Or equivalently is this a constant that read_const
8311 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008312 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008313 */
8314 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008315}
8316
Stefan Reinauer14e22772010-04-27 06:56:47 +00008317static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008318 struct triple *left, struct triple *right)
8319{
8320 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008321 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8322 equal = 0;
8323 }
8324 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008325 equal = 0;
8326 }
8327 else if (left->op != right->op) {
8328 equal = 0;
8329 }
8330 else if (!equiv_types(left->type, right->type)) {
8331 equal = 0;
8332 }
8333 else {
8334 equal = 0;
8335 switch(left->op) {
8336 case OP_INTCONST:
8337 if (left->u.cval == right->u.cval) {
8338 equal = 1;
8339 }
8340 break;
8341 case OP_BLOBCONST:
8342 {
Eric Biederman90089602004-05-28 14:11:54 +00008343 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008344 lsize = size_of(state, left->type);
8345 rsize = size_of(state, right->type);
8346 if (lsize != rsize) {
8347 break;
8348 }
Eric Biederman90089602004-05-28 14:11:54 +00008349 bytes = bits_to_bytes(lsize);
8350 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008351 equal = 1;
8352 }
8353 break;
8354 }
8355 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008356 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008357 (left->u.cval == right->u.cval)) {
8358 equal = 1;
8359 }
8360 break;
8361 default:
8362 internal_error(state, left, "uknown constant type");
8363 break;
8364 }
8365 }
8366 return equal;
8367}
8368
8369static int is_zero(struct triple *ins)
8370{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008371 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008372}
8373
8374static int is_one(struct triple *ins)
8375{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008376 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008377}
8378
Stefan Reinauer50542a82007-10-24 11:14:14 +00008379#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008380static long_t bit_count(ulong_t value)
8381{
8382 int count;
8383 int i;
8384 count = 0;
8385 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8386 ulong_t mask;
8387 mask = 1;
8388 mask <<= i;
8389 if (value & mask) {
8390 count++;
8391 }
8392 }
8393 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008394
Eric Biederman530b5192003-07-01 10:05:30 +00008395}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008396#endif
8397
Eric Biedermanb138ac82003-04-22 18:44:01 +00008398static long_t bsr(ulong_t value)
8399{
8400 int i;
8401 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8402 ulong_t mask;
8403 mask = 1;
8404 mask <<= i;
8405 if (value & mask) {
8406 return i;
8407 }
8408 }
8409 return -1;
8410}
8411
8412static long_t bsf(ulong_t value)
8413{
8414 int i;
8415 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8416 ulong_t mask;
8417 mask = 1;
8418 mask <<= 1;
8419 if (value & mask) {
8420 return i;
8421 }
8422 }
8423 return -1;
8424}
8425
Eric Biedermancb364952004-11-15 10:46:44 +00008426static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008427{
8428 return bsr(value);
8429}
8430
8431static long_t tlog2(struct triple *ins)
8432{
Eric Biedermancb364952004-11-15 10:46:44 +00008433 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008434}
8435
8436static int is_pow2(struct triple *ins)
8437{
8438 ulong_t value, mask;
8439 long_t log;
8440 if (!is_const(ins)) {
8441 return 0;
8442 }
8443 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008444 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008445 if (log == -1) {
8446 return 0;
8447 }
8448 mask = 1;
8449 mask <<= log;
8450 return ((value & mask) == value);
8451}
8452
8453static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008454 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008455{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008456 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008457 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008458 case TYPE_SHORT:
8459 case TYPE_INT:
8460 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008461 case TYPE_UCHAR:
8462 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008463 case TYPE_UINT:
8464 case TYPE_ULONG:
8465 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008466 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008467 break;
8468 default:
Eric Biederman90089602004-05-28 14:11:54 +00008469 fprintf(state->errout, "type: ");
8470 name_of(state->errout, rhs->type);
8471 fprintf(state->errout, "\n");
8472 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008473 break;
8474 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008475 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008476 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008477 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008478 return rhs->u.cval;
8479}
8480
Eric Biederman5ade04a2003-10-22 04:03:46 +00008481static long_t read_sconst(struct compile_state *state,
8482 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008483{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008484 return (long_t)(rhs->u.cval);
8485}
8486
Eric Biederman5ade04a2003-10-22 04:03:46 +00008487int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8488{
8489 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008490 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008491 }
8492 return !is_zero(rhs);
8493}
8494
8495int const_eq(struct compile_state *state, struct triple *ins,
8496 struct triple *left, struct triple *right)
8497{
8498 int result;
8499 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008500 internal_warning(state, ins, "non const passed to const_eq");
8501 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008502 }
8503 else if (left == right) {
8504 result = 1;
8505 }
8506 else if (is_simple_const(left) && is_simple_const(right)) {
8507 ulong_t lval, rval;
8508 lval = read_const(state, ins, left);
8509 rval = read_const(state, ins, right);
8510 result = (lval == rval);
8511 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008512 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008513 (right->op == OP_ADDRCONST)) {
8514 result = (MISC(left, 0) == MISC(right, 0)) &&
8515 (left->u.cval == right->u.cval);
8516 }
8517 else {
Eric Biederman90089602004-05-28 14:11:54 +00008518 internal_warning(state, ins, "incomparable constants passed to const_eq");
8519 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008520 }
8521 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008522
Eric Biederman5ade04a2003-10-22 04:03:46 +00008523}
8524
8525int const_ucmp(struct compile_state *state, struct triple *ins,
8526 struct triple *left, struct triple *right)
8527{
8528 int result;
8529 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008530 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008531 result = -2;
8532 }
8533 else if (left == right) {
8534 result = 0;
8535 }
8536 else if (is_simple_const(left) && is_simple_const(right)) {
8537 ulong_t lval, rval;
8538 lval = read_const(state, ins, left);
8539 rval = read_const(state, ins, right);
8540 result = 0;
8541 if (lval > rval) {
8542 result = 1;
8543 } else if (rval > lval) {
8544 result = -1;
8545 }
8546 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008547 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008548 (right->op == OP_ADDRCONST) &&
8549 (MISC(left, 0) == MISC(right, 0))) {
8550 result = 0;
8551 if (left->u.cval > right->u.cval) {
8552 result = 1;
8553 } else if (left->u.cval < right->u.cval) {
8554 result = -1;
8555 }
8556 }
8557 else {
Eric Biederman90089602004-05-28 14:11:54 +00008558 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008559 result = -2;
8560 }
8561 return result;
8562}
8563
8564int const_scmp(struct compile_state *state, struct triple *ins,
8565 struct triple *left, struct triple *right)
8566{
8567 int result;
8568 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008569 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008570 result = -2;
8571 }
8572 else if (left == right) {
8573 result = 0;
8574 }
8575 else if (is_simple_const(left) && is_simple_const(right)) {
8576 long_t lval, rval;
8577 lval = read_sconst(state, ins, left);
8578 rval = read_sconst(state, ins, right);
8579 result = 0;
8580 if (lval > rval) {
8581 result = 1;
8582 } else if (rval > lval) {
8583 result = -1;
8584 }
8585 }
8586 else {
Eric Biederman90089602004-05-28 14:11:54 +00008587 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008588 result = -2;
8589 }
8590 return result;
8591}
8592
Eric Biedermanb138ac82003-04-22 18:44:01 +00008593static void unuse_rhs(struct compile_state *state, struct triple *ins)
8594{
8595 struct triple **expr;
8596 expr = triple_rhs(state, ins, 0);
8597 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008598 if (*expr) {
8599 unuse_triple(*expr, ins);
8600 *expr = 0;
8601 }
8602 }
8603}
8604
8605static void unuse_lhs(struct compile_state *state, struct triple *ins)
8606{
8607 struct triple **expr;
8608 expr = triple_lhs(state, ins, 0);
8609 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008610 unuse_triple(*expr, ins);
8611 *expr = 0;
8612 }
8613}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008614
Stefan Reinauer50542a82007-10-24 11:14:14 +00008615#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008616static void unuse_misc(struct compile_state *state, struct triple *ins)
8617{
8618 struct triple **expr;
8619 expr = triple_misc(state, ins, 0);
8620 for(;expr;expr = triple_misc(state, ins, expr)) {
8621 unuse_triple(*expr, ins);
8622 *expr = 0;
8623 }
8624}
8625
8626static void unuse_targ(struct compile_state *state, struct triple *ins)
8627{
8628 int i;
8629 struct triple **slot;
8630 slot = &TARG(ins, 0);
8631 for(i = 0; i < ins->targ; i++) {
8632 unuse_triple(slot[i], ins);
8633 slot[i] = 0;
8634 }
8635}
8636
Eric Biedermanb138ac82003-04-22 18:44:01 +00008637static void check_lhs(struct compile_state *state, struct triple *ins)
8638{
8639 struct triple **expr;
8640 expr = triple_lhs(state, ins, 0);
8641 for(;expr;expr = triple_lhs(state, ins, expr)) {
8642 internal_error(state, ins, "unexpected lhs");
8643 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008644
Eric Biedermanb138ac82003-04-22 18:44:01 +00008645}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008646#endif
Eric Biederman90089602004-05-28 14:11:54 +00008647
8648static void check_misc(struct compile_state *state, struct triple *ins)
8649{
8650 struct triple **expr;
8651 expr = triple_misc(state, ins, 0);
8652 for(;expr;expr = triple_misc(state, ins, expr)) {
8653 if (*expr) {
8654 internal_error(state, ins, "unexpected misc");
8655 }
8656 }
8657}
8658
Eric Biedermanb138ac82003-04-22 18:44:01 +00008659static void check_targ(struct compile_state *state, struct triple *ins)
8660{
8661 struct triple **expr;
8662 expr = triple_targ(state, ins, 0);
8663 for(;expr;expr = triple_targ(state, ins, expr)) {
8664 internal_error(state, ins, "unexpected targ");
8665 }
8666}
8667
8668static void wipe_ins(struct compile_state *state, struct triple *ins)
8669{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008670 /* Becareful which instructions you replace the wiped
8671 * instruction with, as there are not enough slots
8672 * in all instructions to hold all others.
8673 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008674 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008675 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008676 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008677 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008678 ins->lhs = 0;
8679 ins->rhs = 0;
8680 ins->misc = 0;
8681 ins->targ = 0;
8682}
8683
Stefan Reinauer50542a82007-10-24 11:14:14 +00008684#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008685static void wipe_branch(struct compile_state *state, struct triple *ins)
8686{
8687 /* Becareful which instructions you replace the wiped
8688 * instruction with, as there are not enough slots
8689 * in all instructions to hold all others.
8690 */
8691 unuse_rhs(state, ins);
8692 unuse_lhs(state, ins);
8693 unuse_misc(state, ins);
8694 unuse_targ(state, ins);
8695 ins->lhs = 0;
8696 ins->rhs = 0;
8697 ins->misc = 0;
8698 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008699}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008700#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008701
Stefan Reinauer14e22772010-04-27 06:56:47 +00008702static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008703 struct triple *ins, struct triple *rhs)
8704{
Eric Biederman83b991a2003-10-11 06:20:25 +00008705 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008706 if (!equiv_types(ins->type, rhs->type)) {
8707 FILE *fp = state->errout;
8708 fprintf(fp, "src type: ");
8709 name_of(fp, rhs->type);
8710 fprintf(fp, "\ndst type: ");
8711 name_of(fp, ins->type);
8712 fprintf(fp, "\n");
8713 internal_error(state, ins, "mkcopy type mismatch");
8714 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008715 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008716 wipe_ins(state, ins);
8717 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008718 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008719 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008720 RHS(ins, 0) = rhs;
8721 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008722}
8723
Stefan Reinauer14e22772010-04-27 06:56:47 +00008724static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008725 struct triple *ins, ulong_t value)
8726{
8727 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008728 fprintf(state->errout, "type: ");
8729 name_of(state->errout, ins->type);
8730 fprintf(state->errout, "\n");
8731 internal_error(state, ins, "unknown type to make constant value: %ld",
8732 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008733 }
8734 wipe_ins(state, ins);
8735 ins->op = OP_INTCONST;
8736 ins->u.cval = value;
8737}
8738
8739static void mkaddr_const(struct compile_state *state,
8740 struct triple *ins, struct triple *sdecl, ulong_t value)
8741{
Eric Biederman90089602004-05-28 14:11:54 +00008742 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008743 internal_error(state, ins, "bad base for addrconst");
8744 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008745 wipe_ins(state, ins);
8746 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008747 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008748 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008749 ins->u.cval = value;
8750 use_triple(sdecl, ins);
8751}
8752
Eric Biederman90089602004-05-28 14:11:54 +00008753#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008754static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008755 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008756{
Eric Biederman90089602004-05-28 14:11:54 +00008757 FILE *fp = state->dbgout;
8758 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8759 name_of(fp, tuple->type);
8760 if (tuple->lhs > 0) {
8761 fprintf(fp, " lhs: ");
8762 name_of(fp, LHS(tuple, 0)->type);
8763 }
8764 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008765
Eric Biederman90089602004-05-28 14:11:54 +00008766}
8767#endif
8768
Stefan Reinauer14e22772010-04-27 06:56:47 +00008769static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008770 struct triple *ins, struct triple *tuple)
8771{
8772 struct triple *next;
8773 next = ins->next;
8774 flatten(state, next, tuple);
8775#if DEBUG_DECOMPOSE_PRINT_TUPLES
8776 print_tuple(state, ins, tuple);
8777#endif
8778
8779 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8780 struct triple *tmp;
8781 if (tuple->lhs != 1) {
8782 internal_error(state, tuple, "plain type in multiple registers?");
8783 }
8784 tmp = LHS(tuple, 0);
8785 release_triple(state, tuple);
8786 tuple = tmp;
8787 }
8788
8789 propogate_use(state, ins, tuple);
8790 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008791
Eric Biederman90089602004-05-28 14:11:54 +00008792 return next;
8793}
8794
8795static struct triple *decompose_unknownval(struct compile_state *state,
8796 struct triple *ins)
8797{
8798 struct triple *tuple;
8799 ulong_t i;
8800
8801#if DEBUG_DECOMPOSE_HIRES
8802 FILE *fp = state->dbgout;
8803 fprintf(fp, "unknown type: ");
8804 name_of(fp, ins->type);
8805 fprintf(fp, "\n");
8806#endif
8807
8808 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008809 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008810 ins->occurance);
8811
8812 for(i = 0; i < tuple->lhs; i++) {
8813 struct type *piece_type;
8814 struct triple *unknown;
8815
8816 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8817 get_occurance(tuple->occurance);
8818 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8819 tuple->occurance);
8820 LHS(tuple, i) = unknown;
8821 }
8822 return decompose_with_tuple(state, ins, tuple);
8823}
8824
8825
Stefan Reinauer14e22772010-04-27 06:56:47 +00008826static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008827 struct triple *ins)
8828{
8829 struct triple *tuple, *lval;
8830 ulong_t i;
8831
8832 lval = RHS(ins, 0);
8833
8834 if (lval->op == OP_PIECE) {
8835 return ins->next;
8836 }
8837 get_occurance(ins->occurance);
8838 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8839 ins->occurance);
8840
8841 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008842 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008843 {
8844 internal_error(state, ins, "lhs size inconsistency?");
8845 }
8846 for(i = 0; i < tuple->lhs; i++) {
8847 struct triple *piece, *read, *bitref;
8848 if ((i != 0) || !triple_is_def(state, lval)) {
8849 piece = LHS(lval, i);
8850 } else {
8851 piece = lval;
8852 }
8853
8854 /* See if the piece is really a bitref */
8855 bitref = 0;
8856 if (piece->op == OP_BITREF) {
8857 bitref = piece;
8858 piece = RHS(bitref, 0);
8859 }
8860
8861 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008862 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008863 tuple->occurance);
8864 RHS(read, 0) = piece;
8865
8866 if (bitref) {
8867 struct triple *extract;
8868 int op;
8869 if (is_signed(bitref->type->left)) {
8870 op = OP_SEXTRACT;
8871 } else {
8872 op = OP_UEXTRACT;
8873 }
8874 get_occurance(tuple->occurance);
8875 extract = alloc_triple(state, op, bitref->type, -1, -1,
8876 tuple->occurance);
8877 RHS(extract, 0) = read;
8878 extract->u.bitfield.size = bitref->u.bitfield.size;
8879 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8880
8881 read = extract;
8882 }
8883
8884 LHS(tuple, i) = read;
8885 }
8886 return decompose_with_tuple(state, ins, tuple);
8887}
8888
Stefan Reinauer14e22772010-04-27 06:56:47 +00008889static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008890 struct triple *ins)
8891{
8892 struct triple *tuple, *lval, *val;
8893 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008894
Eric Biederman90089602004-05-28 14:11:54 +00008895 lval = MISC(ins, 0);
8896 val = RHS(ins, 0);
8897 get_occurance(ins->occurance);
8898 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8899 ins->occurance);
8900
8901 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008902 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008903 {
8904 internal_error(state, ins, "lhs size inconsistency?");
8905 }
8906 for(i = 0; i < tuple->lhs; i++) {
8907 struct triple *piece, *write, *pval, *bitref;
8908 if ((i != 0) || !triple_is_def(state, lval)) {
8909 piece = LHS(lval, i);
8910 } else {
8911 piece = lval;
8912 }
8913 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8914 pval = val;
8915 }
8916 else {
8917 if (i > val->lhs) {
8918 internal_error(state, ins, "lhs size inconsistency?");
8919 }
8920 pval = LHS(val, i);
8921 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008922
Eric Biederman90089602004-05-28 14:11:54 +00008923 /* See if the piece is really a bitref */
8924 bitref = 0;
8925 if (piece->op == OP_BITREF) {
8926 struct triple *read, *deposit;
8927 bitref = piece;
8928 piece = RHS(bitref, 0);
8929
8930 /* Read the destination register */
8931 get_occurance(tuple->occurance);
8932 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8933 tuple->occurance);
8934 RHS(read, 0) = piece;
8935
8936 /* Deposit the new bitfield value */
8937 get_occurance(tuple->occurance);
8938 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8939 tuple->occurance);
8940 RHS(deposit, 0) = read;
8941 RHS(deposit, 1) = pval;
8942 deposit->u.bitfield.size = bitref->u.bitfield.size;
8943 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8944
8945 /* Now write the newly generated value */
8946 pval = deposit;
8947 }
8948
8949 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008950 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008951 tuple->occurance);
8952 MISC(write, 0) = piece;
8953 RHS(write, 0) = pval;
8954 LHS(tuple, i) = write;
8955 }
8956 return decompose_with_tuple(state, ins, tuple);
8957}
8958
8959struct decompose_load_info {
8960 struct occurance *occurance;
8961 struct triple *lval;
8962 struct triple *tuple;
8963};
8964static void decompose_load_cb(struct compile_state *state,
8965 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8966{
8967 struct decompose_load_info *info = arg;
8968 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008969
Eric Biederman90089602004-05-28 14:11:54 +00008970 if (reg_offset > info->tuple->lhs) {
8971 internal_error(state, info->tuple, "lhs to small?");
8972 }
8973 get_occurance(info->occurance);
8974 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8975 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8976 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8977}
8978
Stefan Reinauer14e22772010-04-27 06:56:47 +00008979static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008980 struct triple *ins)
8981{
8982 struct triple *tuple;
8983 struct decompose_load_info info;
8984
8985 if (!is_compound_type(ins->type)) {
8986 return ins->next;
8987 }
8988 get_occurance(ins->occurance);
8989 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8990 ins->occurance);
8991
8992 info.occurance = ins->occurance;
8993 info.lval = RHS(ins, 0);
8994 info.tuple = tuple;
8995 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8996
8997 return decompose_with_tuple(state, ins, tuple);
8998}
8999
9000
9001struct decompose_store_info {
9002 struct occurance *occurance;
9003 struct triple *lval;
9004 struct triple *val;
9005 struct triple *tuple;
9006};
9007static void decompose_store_cb(struct compile_state *state,
9008 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9009{
9010 struct decompose_store_info *info = arg;
9011 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009012
Eric Biederman90089602004-05-28 14:11:54 +00009013 if (reg_offset > info->tuple->lhs) {
9014 internal_error(state, info->tuple, "lhs to small?");
9015 }
9016 get_occurance(info->occurance);
9017 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9018 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9019 RHS(store, 1) = LHS(info->val, reg_offset);
9020 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9021}
9022
Stefan Reinauer14e22772010-04-27 06:56:47 +00009023static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009024 struct triple *ins)
9025{
9026 struct triple *tuple;
9027 struct decompose_store_info info;
9028
9029 if (!is_compound_type(ins->type)) {
9030 return ins->next;
9031 }
9032 get_occurance(ins->occurance);
9033 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9034 ins->occurance);
9035
9036 info.occurance = ins->occurance;
9037 info.lval = RHS(ins, 0);
9038 info.val = RHS(ins, 1);
9039 info.tuple = tuple;
9040 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9041
9042 return decompose_with_tuple(state, ins, tuple);
9043}
9044
Stefan Reinauer14e22772010-04-27 06:56:47 +00009045static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009046 struct triple *ins)
9047{
9048 struct triple *tuple, *lval;
9049 struct type *type;
9050 size_t reg_offset;
9051 int i, idx;
9052
9053 lval = MISC(ins, 0);
9054 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9055 idx = reg_offset/REG_SIZEOF_REG;
9056 type = field_type(state, lval->type, ins->u.field);
9057#if DEBUG_DECOMPOSE_HIRES
9058 {
9059 FILE *fp = state->dbgout;
9060 fprintf(fp, "field type: ");
9061 name_of(fp, type);
9062 fprintf(fp, "\n");
9063 }
9064#endif
9065
9066 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009067 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009068 ins->occurance);
9069
9070 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9071 (tuple->lhs != 1))
9072 {
9073 internal_error(state, ins, "multi register bitfield?");
9074 }
9075
9076 for(i = 0; i < tuple->lhs; i++, idx++) {
9077 struct triple *piece;
9078 if (!triple_is_def(state, lval)) {
9079 if (idx > lval->lhs) {
9080 internal_error(state, ins, "inconsistent lhs count");
9081 }
9082 piece = LHS(lval, idx);
9083 } else {
9084 if (idx != 0) {
9085 internal_error(state, ins, "bad reg_offset into def");
9086 }
9087 if (i != 0) {
9088 internal_error(state, ins, "bad reg count from def");
9089 }
9090 piece = lval;
9091 }
9092
9093 /* Remember the offset of the bitfield */
9094 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9095 get_occurance(ins->occurance);
9096 piece = build_triple(state, OP_BITREF, type, piece, 0,
9097 ins->occurance);
9098 piece->u.bitfield.size = size_of(state, type);
9099 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9100 }
9101 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009102 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009103 "request for a nonbitfield sub register?");
9104 }
9105
9106 LHS(tuple, i) = piece;
9107 }
9108
9109 return decompose_with_tuple(state, ins, tuple);
9110}
9111
Stefan Reinauer14e22772010-04-27 06:56:47 +00009112static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009113 struct triple *ins)
9114{
9115 struct triple *tuple, *lval;
9116 struct type *type;
9117 int i, idx;
9118
9119 lval = MISC(ins, 0);
9120 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9121 type = index_type(state, lval->type, ins->u.cval);
9122#if DEBUG_DECOMPOSE_HIRES
9123{
9124 FILE *fp = state->dbgout;
9125 fprintf(fp, "index type: ");
9126 name_of(fp, type);
9127 fprintf(fp, "\n");
9128}
9129#endif
9130
9131 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009132 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009133 ins->occurance);
9134
9135 for(i = 0; i < tuple->lhs; i++, idx++) {
9136 struct triple *piece;
9137 if (!triple_is_def(state, lval)) {
9138 if (idx > lval->lhs) {
9139 internal_error(state, ins, "inconsistent lhs count");
9140 }
9141 piece = LHS(lval, idx);
9142 } else {
9143 if (idx != 0) {
9144 internal_error(state, ins, "bad reg_offset into def");
9145 }
9146 if (i != 0) {
9147 internal_error(state, ins, "bad reg count from def");
9148 }
9149 piece = lval;
9150 }
9151 LHS(tuple, i) = piece;
9152 }
9153
9154 return decompose_with_tuple(state, ins, tuple);
9155}
9156
9157static void decompose_compound_types(struct compile_state *state)
9158{
9159 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009160 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00009161
9162 /* Pass one expand compound values into pseudo registers.
9163 */
9164 next = first;
9165 do {
9166 ins = next;
9167 next = ins->next;
9168 switch(ins->op) {
9169 case OP_UNKNOWNVAL:
9170 next = decompose_unknownval(state, ins);
9171 break;
9172
9173 case OP_READ:
9174 next = decompose_read(state, ins);
9175 break;
9176
9177 case OP_WRITE:
9178 next = decompose_write(state, ins);
9179 break;
9180
9181
9182 /* Be very careful with the load/store logic. These
9183 * operations must convert from the in register layout
9184 * to the in memory layout, which is nontrivial.
9185 */
9186 case OP_LOAD:
9187 next = decompose_load(state, ins);
9188 break;
9189 case OP_STORE:
9190 next = decompose_store(state, ins);
9191 break;
9192
9193 case OP_DOT:
9194 next = decompose_dot(state, ins);
9195 break;
9196 case OP_INDEX:
9197 next = decompose_index(state, ins);
9198 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009199
Eric Biederman90089602004-05-28 14:11:54 +00009200 }
9201#if DEBUG_DECOMPOSE_HIRES
9202 fprintf(fp, "decompose next: %p \n", next);
9203 fflush(fp);
9204 fprintf(fp, "next->op: %d %s\n",
9205 next->op, tops(next->op));
9206 /* High resolution debugging mode */
9207 print_triples(state);
9208#endif
9209 } while (next != first);
9210
9211 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009212 */
9213 ins = first;
9214 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009215 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009216 if (ins->op == OP_TUPLE) {
9217 if (ins->use) {
9218 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009219 }
Eric Biederman90089602004-05-28 14:11:54 +00009220 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009221 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009222 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009223 }
Eric Biederman90089602004-05-28 14:11:54 +00009224 ins = next;
9225 } while(ins != first);
9226 ins = first;
9227 do {
9228 next = ins->next;
9229 if (ins->op == OP_BITREF) {
9230 if (ins->use) {
9231 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +00009232 }
Eric Biederman90089602004-05-28 14:11:54 +00009233 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009234 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009235 }
9236 }
9237 ins = next;
9238 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009239
Eric Biederman0babc1c2003-05-09 02:39:00 +00009240 /* Pass three verify the state and set ->id to 0.
9241 */
Eric Biederman90089602004-05-28 14:11:54 +00009242 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009243 do {
Eric Biederman90089602004-05-28 14:11:54 +00009244 ins = next;
9245 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009246 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009247 if (triple_stores_block(state, ins)) {
9248 ins->u.block = 0;
9249 }
9250 if (triple_is_def(state, ins)) {
9251 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9252 internal_error(state, ins, "multi register value remains?");
9253 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009254 }
9255 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009256 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009257 }
Eric Biederman90089602004-05-28 14:11:54 +00009258 if (ins->op == OP_INDEX) {
9259 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009260 }
Eric Biederman90089602004-05-28 14:11:54 +00009261 if (ins->op == OP_BITREF) {
9262 internal_error(state, ins, "OP_BITREF remains?");
9263 }
9264 if (ins->op == OP_TUPLE) {
9265 internal_error(state, ins, "OP_TUPLE remains?");
9266 }
9267 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009268}
9269
Eric Biedermanb138ac82003-04-22 18:44:01 +00009270/* For those operations that cannot be simplified */
9271static void simplify_noop(struct compile_state *state, struct triple *ins)
9272{
9273 return;
9274}
9275
9276static void simplify_smul(struct compile_state *state, struct triple *ins)
9277{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009278 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009279 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009280 tmp = RHS(ins, 0);
9281 RHS(ins, 0) = RHS(ins, 1);
9282 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009283 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009284 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009285 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009286 left = read_sconst(state, ins, RHS(ins, 0));
9287 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288 mkconst(state, ins, left * right);
9289 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009290 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009291 mkconst(state, ins, 0);
9292 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009293 else if (is_one(RHS(ins, 1))) {
9294 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009295 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009296 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009297 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009299 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009300 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009301 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009303 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 }
9305}
9306
9307static void simplify_umul(struct compile_state *state, struct triple *ins)
9308{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009309 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009310 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009311 tmp = RHS(ins, 0);
9312 RHS(ins, 0) = RHS(ins, 1);
9313 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009314 }
Eric Biederman90089602004-05-28 14:11:54 +00009315 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009316 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009317 left = read_const(state, ins, RHS(ins, 0));
9318 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 mkconst(state, ins, left * right);
9320 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009321 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009322 mkconst(state, ins, 0);
9323 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009324 else if (is_one(RHS(ins, 1))) {
9325 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009326 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009327 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009328 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009329 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009330 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009331 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009332 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009334 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009335 }
9336}
9337
9338static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9339{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009340 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009341 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009342 left = read_sconst(state, ins, RHS(ins, 0));
9343 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009344 mkconst(state, ins, left / right);
9345 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009346 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009347 mkconst(state, ins, 0);
9348 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009349 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009350 error(state, ins, "division by zero");
9351 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009352 else if (is_one(RHS(ins, 1))) {
9353 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009354 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009355 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009356 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009357 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009358 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009359 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009360 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009362 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 }
9364}
9365
9366static void simplify_udiv(struct compile_state *state, struct triple *ins)
9367{
Eric Biederman90089602004-05-28 14:11:54 +00009368 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009369 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009370 left = read_const(state, ins, RHS(ins, 0));
9371 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009372 mkconst(state, ins, left / right);
9373 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009374 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009375 mkconst(state, ins, 0);
9376 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009377 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009378 error(state, ins, "division by zero");
9379 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009380 else if (is_one(RHS(ins, 1))) {
9381 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009382 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009383 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009384 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009385 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009386 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009387 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009388 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009390 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009391 }
9392}
9393
9394static void simplify_smod(struct compile_state *state, struct triple *ins)
9395{
Eric Biederman90089602004-05-28 14:11:54 +00009396 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009397 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009398 left = read_const(state, ins, RHS(ins, 0));
9399 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009400 mkconst(state, ins, left % right);
9401 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009402 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009403 mkconst(state, ins, 0);
9404 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009405 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009406 error(state, ins, "division by zero");
9407 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009408 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009409 mkconst(state, ins, 0);
9410 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009411 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009412 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009413 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009415 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009418 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009419 }
9420}
Eric Biederman83b991a2003-10-11 06:20:25 +00009421
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422static void simplify_umod(struct compile_state *state, struct triple *ins)
9423{
Eric Biederman90089602004-05-28 14:11:54 +00009424 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009425 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009426 left = read_const(state, ins, RHS(ins, 0));
9427 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428 mkconst(state, ins, left % right);
9429 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009430 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009431 mkconst(state, ins, 0);
9432 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009433 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009434 error(state, ins, "division by zero");
9435 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009436 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437 mkconst(state, ins, 0);
9438 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009439 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009441 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009443 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009444 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 }
9448}
9449
9450static void simplify_add(struct compile_state *state, struct triple *ins)
9451{
9452 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009453 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009454 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009455 tmp = RHS(ins, 0);
9456 RHS(ins, 0) = RHS(ins, 1);
9457 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009458 }
Eric Biederman90089602004-05-28 14:11:54 +00009459 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009460 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009461 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009462 left = read_const(state, ins, RHS(ins, 0));
9463 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009464 mkconst(state, ins, left + right);
9465 }
Eric Biederman530b5192003-07-01 10:05:30 +00009466 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009467 struct triple *sdecl;
9468 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009469 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009470 left = RHS(ins, 0)->u.cval;
9471 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 mkaddr_const(state, ins, sdecl, left + right);
9473 }
Eric Biederman530b5192003-07-01 10:05:30 +00009474 else {
9475 internal_warning(state, ins, "Optimize me!");
9476 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009477 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009478 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009479 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009480 tmp = RHS(ins, 1);
9481 RHS(ins, 1) = RHS(ins, 0);
9482 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009483 }
9484}
9485
9486static void simplify_sub(struct compile_state *state, struct triple *ins)
9487{
Eric Biederman90089602004-05-28 14:11:54 +00009488 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009489 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009490 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009491 left = read_const(state, ins, RHS(ins, 0));
9492 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009493 mkconst(state, ins, left - right);
9494 }
Eric Biederman530b5192003-07-01 10:05:30 +00009495 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009496 struct triple *sdecl;
9497 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009498 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009499 left = RHS(ins, 0)->u.cval;
9500 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009501 mkaddr_const(state, ins, sdecl, left - right);
9502 }
Eric Biederman530b5192003-07-01 10:05:30 +00009503 else {
9504 internal_warning(state, ins, "Optimize me!");
9505 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009506 }
9507}
9508
9509static void simplify_sl(struct compile_state *state, struct triple *ins)
9510{
Eric Biederman90089602004-05-28 14:11:54 +00009511 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009512 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009513 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009514 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009515 warning(state, ins, "left shift count >= width of type");
9516 }
9517 }
Eric Biederman90089602004-05-28 14:11:54 +00009518 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009519 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009520 left = read_const(state, ins, RHS(ins, 0));
9521 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009522 mkconst(state, ins, left << right);
9523 }
9524}
9525
9526static void simplify_usr(struct compile_state *state, struct triple *ins)
9527{
Eric Biederman90089602004-05-28 14:11:54 +00009528 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009529 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009530 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009531 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009532 warning(state, ins, "right shift count >= width of type");
9533 }
9534 }
Eric Biederman90089602004-05-28 14:11:54 +00009535 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009536 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009537 left = read_const(state, ins, RHS(ins, 0));
9538 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009539 mkconst(state, ins, left >> right);
9540 }
9541}
9542
9543static void simplify_ssr(struct compile_state *state, struct triple *ins)
9544{
Eric Biederman90089602004-05-28 14:11:54 +00009545 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009546 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009547 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009548 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 warning(state, ins, "right shift count >= width of type");
9550 }
9551 }
Eric Biederman90089602004-05-28 14:11:54 +00009552 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009553 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009554 left = read_sconst(state, ins, RHS(ins, 0));
9555 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009556 mkconst(state, ins, left >> right);
9557 }
9558}
9559
9560static void simplify_and(struct compile_state *state, struct triple *ins)
9561{
Eric Biederman90089602004-05-28 14:11:54 +00009562 struct triple *left, *right;
9563 left = RHS(ins, 0);
9564 right = RHS(ins, 1);
9565
9566 if (is_simple_const(left) && is_simple_const(right)) {
9567 ulong_t lval, rval;
9568 lval = read_const(state, ins, left);
9569 rval = read_const(state, ins, right);
9570 mkconst(state, ins, lval & rval);
9571 }
9572 else if (is_zero(right) || is_zero(left)) {
9573 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009574 }
9575}
9576
9577static void simplify_or(struct compile_state *state, struct triple *ins)
9578{
Eric Biederman90089602004-05-28 14:11:54 +00009579 struct triple *left, *right;
9580 left = RHS(ins, 0);
9581 right = RHS(ins, 1);
9582
9583 if (is_simple_const(left) && is_simple_const(right)) {
9584 ulong_t lval, rval;
9585 lval = read_const(state, ins, left);
9586 rval = read_const(state, ins, right);
9587 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009588 }
Eric Biederman90089602004-05-28 14:11:54 +00009589#if 0 /* I need to handle type mismatches here... */
9590 else if (is_zero(right)) {
9591 mkcopy(state, ins, left);
9592 }
9593 else if (is_zero(left)) {
9594 mkcopy(state, ins, right);
9595 }
9596#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009597}
9598
9599static void simplify_xor(struct compile_state *state, struct triple *ins)
9600{
Eric Biederman90089602004-05-28 14:11:54 +00009601 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009602 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009603 left = read_const(state, ins, RHS(ins, 0));
9604 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009605 mkconst(state, ins, left ^ right);
9606 }
9607}
9608
9609static void simplify_pos(struct compile_state *state, struct triple *ins)
9610{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009611 if (is_const(RHS(ins, 0))) {
9612 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009613 }
9614 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009615 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009616 }
9617}
9618
9619static void simplify_neg(struct compile_state *state, struct triple *ins)
9620{
Eric Biederman90089602004-05-28 14:11:54 +00009621 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009622 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009623 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009624 mkconst(state, ins, -left);
9625 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009626 else if (RHS(ins, 0)->op == OP_NEG) {
9627 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009628 }
9629}
9630
9631static void simplify_invert(struct compile_state *state, struct triple *ins)
9632{
Eric Biederman90089602004-05-28 14:11:54 +00009633 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009634 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009635 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009636 mkconst(state, ins, ~left);
9637 }
9638}
9639
9640static void simplify_eq(struct compile_state *state, struct triple *ins)
9641{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009642 struct triple *left, *right;
9643 left = RHS(ins, 0);
9644 right = RHS(ins, 1);
9645
9646 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009647 int val;
9648 val = const_eq(state, ins, left, right);
9649 if (val >= 0) {
9650 mkconst(state, ins, val == 1);
9651 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009652 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009653 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009654 mkconst(state, ins, 1);
9655 }
9656}
9657
9658static void simplify_noteq(struct compile_state *state, struct triple *ins)
9659{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009660 struct triple *left, *right;
9661 left = RHS(ins, 0);
9662 right = RHS(ins, 1);
9663
9664 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009665 int val;
9666 val = const_eq(state, ins, left, right);
9667 if (val >= 0) {
9668 mkconst(state, ins, val != 1);
9669 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009670 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009671 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009672 mkconst(state, ins, 0);
9673 }
9674}
9675
9676static void simplify_sless(struct compile_state *state, struct triple *ins)
9677{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009678 struct triple *left, *right;
9679 left = RHS(ins, 0);
9680 right = RHS(ins, 1);
9681
9682 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009683 int val;
9684 val = const_scmp(state, ins, left, right);
9685 if ((val >= -1) && (val <= 1)) {
9686 mkconst(state, ins, val < 0);
9687 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009688 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009689 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009690 mkconst(state, ins, 0);
9691 }
9692}
9693
9694static void simplify_uless(struct compile_state *state, struct triple *ins)
9695{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009696 struct triple *left, *right;
9697 left = RHS(ins, 0);
9698 right = RHS(ins, 1);
9699
9700 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009701 int val;
9702 val = const_ucmp(state, ins, left, right);
9703 if ((val >= -1) && (val <= 1)) {
9704 mkconst(state, ins, val < 0);
9705 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009706 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009707 else if (is_zero(right)) {
9708 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009709 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009710 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009711 mkconst(state, ins, 0);
9712 }
9713}
9714
9715static void simplify_smore(struct compile_state *state, struct triple *ins)
9716{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009717 struct triple *left, *right;
9718 left = RHS(ins, 0);
9719 right = RHS(ins, 1);
9720
9721 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009722 int val;
9723 val = const_scmp(state, ins, left, right);
9724 if ((val >= -1) && (val <= 1)) {
9725 mkconst(state, ins, val > 0);
9726 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009727 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009728 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009729 mkconst(state, ins, 0);
9730 }
9731}
9732
9733static void simplify_umore(struct compile_state *state, struct triple *ins)
9734{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009735 struct triple *left, *right;
9736 left = RHS(ins, 0);
9737 right = RHS(ins, 1);
9738
9739 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009740 int val;
9741 val = const_ucmp(state, ins, left, right);
9742 if ((val >= -1) && (val <= 1)) {
9743 mkconst(state, ins, val > 0);
9744 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009745 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009746 else if (is_zero(left)) {
9747 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009748 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009749 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009750 mkconst(state, ins, 0);
9751 }
9752}
9753
9754
9755static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9756{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009757 struct triple *left, *right;
9758 left = RHS(ins, 0);
9759 right = RHS(ins, 1);
9760
9761 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009762 int val;
9763 val = const_scmp(state, ins, left, right);
9764 if ((val >= -1) && (val <= 1)) {
9765 mkconst(state, ins, val <= 0);
9766 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009767 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009768 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009769 mkconst(state, ins, 1);
9770 }
9771}
9772
9773static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9774{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009775 struct triple *left, *right;
9776 left = RHS(ins, 0);
9777 right = RHS(ins, 1);
9778
9779 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009780 int val;
9781 val = const_ucmp(state, ins, left, right);
9782 if ((val >= -1) && (val <= 1)) {
9783 mkconst(state, ins, val <= 0);
9784 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009785 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009786 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009787 mkconst(state, ins, 1);
9788 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009789 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009790 mkconst(state, ins, 1);
9791 }
9792}
9793
9794static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9795{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009796 struct triple *left, *right;
9797 left = RHS(ins, 0);
9798 right = RHS(ins, 1);
9799
9800 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009801 int val;
9802 val = const_scmp(state, ins, left, right);
9803 if ((val >= -1) && (val <= 1)) {
9804 mkconst(state, ins, val >= 0);
9805 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009806 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009807 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009808 mkconst(state, ins, 1);
9809 }
9810}
9811
9812static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9813{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009814 struct triple *left, *right;
9815 left = RHS(ins, 0);
9816 right = RHS(ins, 1);
9817
9818 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009819 int val;
9820 val = const_ucmp(state, ins, left, right);
9821 if ((val >= -1) && (val <= 1)) {
9822 mkconst(state, ins, val >= 0);
9823 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009824 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009825 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009826 mkconst(state, ins, 1);
9827 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009828 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009829 mkconst(state, ins, 1);
9830 }
9831}
9832
9833static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9834{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009835 struct triple *rhs;
9836 rhs = RHS(ins, 0);
9837
9838 if (is_const(rhs)) {
9839 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009840 }
9841 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009842 else if ((rhs->use) &&
9843 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009844 int need_copy = 1;
9845 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009846 switch(rhs->op) {
9847 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9848 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9849 case OP_EQ: rhs->op = OP_NOTEQ; break;
9850 case OP_NOTEQ: rhs->op = OP_EQ; break;
9851 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9852 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9853 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9854 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9855 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9856 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9857 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9858 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009859 default:
9860 need_copy = 0;
9861 break;
9862 }
9863 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009864 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009865 }
9866 }
9867}
9868
9869static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9870{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009871 struct triple *rhs;
9872 rhs = RHS(ins, 0);
9873
9874 if (is_const(rhs)) {
9875 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009876 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009877 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009878 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9879 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9880 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009881 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009882 }
9883
9884}
9885
Eric Biederman90089602004-05-28 14:11:54 +00009886static void simplify_load(struct compile_state *state, struct triple *ins)
9887{
9888 struct triple *addr, *sdecl, *blob;
9889
9890 /* If I am doing a load with a constant pointer from a constant
9891 * table get the value.
9892 */
9893 addr = RHS(ins, 0);
9894 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9895 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9896 (blob->op == OP_BLOBCONST)) {
9897 unsigned char buffer[SIZEOF_WORD];
9898 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009899 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009900 ulong_t val;
9901 reg_size = reg_size_of(state, ins->type);
9902 if (reg_size > REG_SIZEOF_REG) {
9903 internal_error(state, ins, "load size greater than register");
9904 }
9905 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009906 end = blob->u.blob;
9907 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009908 src = blob->u.blob;
9909 src += addr->u.cval;
9910
Eric Biederman7dea9552004-06-29 05:38:37 +00009911 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009912 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009913 }
9914
Eric Biederman90089602004-05-28 14:11:54 +00009915 memset(buffer, 0, sizeof(buffer));
9916 memcpy(buffer, src, bits_to_bytes(mem_size));
9917
9918 switch(mem_size) {
9919 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9920 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9921 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9922 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9923 default:
9924 internal_error(state, ins, "mem_size: %d not handled",
9925 mem_size);
9926 val = 0;
9927 break;
9928 }
9929 mkconst(state, ins, val);
9930 }
9931}
9932
9933static void simplify_uextract(struct compile_state *state, struct triple *ins)
9934{
9935 if (is_simple_const(RHS(ins, 0))) {
9936 ulong_t val;
9937 ulong_t mask;
9938 val = read_const(state, ins, RHS(ins, 0));
9939 mask = 1;
9940 mask <<= ins->u.bitfield.size;
9941 mask -= 1;
9942 val >>= ins->u.bitfield.offset;
9943 val &= mask;
9944 mkconst(state, ins, val);
9945 }
9946}
9947
9948static void simplify_sextract(struct compile_state *state, struct triple *ins)
9949{
9950 if (is_simple_const(RHS(ins, 0))) {
9951 ulong_t val;
9952 ulong_t mask;
9953 long_t sval;
9954 val = read_const(state, ins, RHS(ins, 0));
9955 mask = 1;
9956 mask <<= ins->u.bitfield.size;
9957 mask -= 1;
9958 val >>= ins->u.bitfield.offset;
9959 val &= mask;
9960 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9961 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009962 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009963 mkconst(state, ins, sval);
9964 }
9965}
9966
9967static void simplify_deposit(struct compile_state *state, struct triple *ins)
9968{
9969 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9970 ulong_t targ, val;
9971 ulong_t mask;
9972 targ = read_const(state, ins, RHS(ins, 0));
9973 val = read_const(state, ins, RHS(ins, 1));
9974 mask = 1;
9975 mask <<= ins->u.bitfield.size;
9976 mask -= 1;
9977 mask <<= ins->u.bitfield.offset;
9978 targ &= ~mask;
9979 val <<= ins->u.bitfield.offset;
9980 val &= mask;
9981 targ |= val;
9982 mkconst(state, ins, targ);
9983 }
9984}
9985
Eric Biedermanb138ac82003-04-22 18:44:01 +00009986static void simplify_copy(struct compile_state *state, struct triple *ins)
9987{
Eric Biederman90089602004-05-28 14:11:54 +00009988 struct triple *right;
9989 right = RHS(ins, 0);
9990 if (is_subset_type(ins->type, right->type)) {
9991 ins->type = right->type;
9992 }
9993 if (equiv_types(ins->type, right->type)) {
9994 ins->op = OP_COPY;/* I don't need to convert if the types match */
9995 } else {
9996 if (ins->op == OP_COPY) {
9997 internal_error(state, ins, "type mismatch on copy");
9998 }
9999 }
10000 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10001 struct triple *sdecl;
10002 ulong_t offset;
10003 sdecl = MISC(right, 0);
10004 offset = right->u.cval;
10005 mkaddr_const(state, ins, sdecl, offset);
10006 }
10007 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10008 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010009 case OP_INTCONST:
10010 {
10011 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010012 left = read_const(state, ins, right);
10013 /* Ensure I have not overflowed the destination. */
10014 if (size_of(state, right->type) > size_of(state, ins->type)) {
10015 ulong_t mask;
10016 mask = 1;
10017 mask <<= size_of(state, ins->type);
10018 mask -= 1;
10019 left &= mask;
10020 }
10021 /* Ensure I am properly sign extended */
10022 if (size_of(state, right->type) < size_of(state, ins->type) &&
10023 is_signed(right->type)) {
Patrick Georgi766c3fe2017-01-02 18:47:50 +010010024 uint64_t val;
Eric Biederman90089602004-05-28 14:11:54 +000010025 int shift;
10026 shift = SIZEOF_LONG - size_of(state, right->type);
10027 val = left;
10028 val <<= shift;
10029 val >>= shift;
Patrick Georgi766c3fe2017-01-02 18:47:50 +010010030 left = (ulong_t)val;
Eric Biederman90089602004-05-28 14:11:54 +000010031 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010032 mkconst(state, ins, left);
10033 break;
10034 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010035 default:
10036 internal_error(state, ins, "uknown constant");
10037 break;
10038 }
10039 }
10040}
10041
Eric Biederman83b991a2003-10-11 06:20:25 +000010042static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010043{
10044 struct triple *ptr;
10045 if (!block) {
10046 return 0;
10047 }
10048 ptr = block->first;
10049 do {
10050 if (ptr->op == OP_PHI) {
10051 return 1;
10052 }
10053 ptr = ptr->next;
10054 } while(ptr != block->last);
10055 return 0;
10056}
10057
Eric Biederman83b991a2003-10-11 06:20:25 +000010058static int phi_dependency(struct block *block)
10059{
10060 /* A block has a phi dependency if a phi function
10061 * depends on that block to exist, and makes a block
10062 * that is otherwise useless unsafe to remove.
10063 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010064 if (block) {
10065 struct block_set *edge;
10066 for(edge = block->edges; edge; edge = edge->next) {
10067 if (phi_present(edge->member)) {
10068 return 1;
10069 }
10070 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010071 }
10072 return 0;
10073}
10074
10075static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10076{
10077 struct triple *targ;
10078 targ = TARG(ins, 0);
10079 /* During scc_transform temporary triples are allocated that
10080 * loop back onto themselves. If I see one don't advance the
10081 * target.
10082 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010083 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010084 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010085 targ = targ->next;
10086 }
10087 return targ;
10088}
10089
10090
10091static void simplify_branch(struct compile_state *state, struct triple *ins)
10092{
Eric Biederman90089602004-05-28 14:11:54 +000010093 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010094 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010095 internal_error(state, ins, "not branch");
10096 }
10097 if (ins->use != 0) {
10098 internal_error(state, ins, "branch use");
10099 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010100 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010101 * make modifications to the control flow graph as well
10102 * as to the branch instruction itself. That is handled
10103 * by rebuilding the basic blocks after simplify all is called.
10104 */
10105
10106 /* If we have a branch to an unconditional branch update
10107 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010108 * functions.
10109 * Also only do this a limited number of times so
10110 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010111 */
Eric Biederman90089602004-05-28 14:11:54 +000010112 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010113 do {
10114 struct triple *targ;
10115 simplified = 0;
10116 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010117 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010118 !phi_dependency(targ->u.block))
10119 {
10120 unuse_triple(TARG(ins, 0), ins);
10121 TARG(ins, 0) = TARG(targ, 0);
10122 use_triple(TARG(ins, 0), ins);
10123 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010124 }
Eric Biederman90089602004-05-28 14:11:54 +000010125 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010126
10127 /* If we have a conditional branch with a constant condition
10128 * make it an unconditional branch.
10129 */
Eric Biederman90089602004-05-28 14:11:54 +000010130 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010131 struct triple *targ;
10132 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010133 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010134 unuse_triple(RHS(ins, 0), ins);
10135 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010136 ins->rhs = 0;
10137 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010138 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010139 if (value) {
10140 unuse_triple(ins->next, ins);
10141 TARG(ins, 0) = targ;
10142 }
10143 else {
10144 unuse_triple(targ, ins);
10145 TARG(ins, 0) = ins->next;
10146 }
10147 }
Eric Biederman90089602004-05-28 14:11:54 +000010148
10149 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010150 * make it a noop.
10151 */
10152 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010153 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010154 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010155 unuse_triple(RHS(ins, 0), ins);
10156 unuse_triple(ins->next, ins);
10157 }
Eric Biederman90089602004-05-28 14:11:54 +000010158 ins->lhs = 0;
10159 ins->rhs = 0;
10160 ins->misc = 0;
10161 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010162 ins->op = OP_NOOP;
10163 if (ins->use) {
10164 internal_error(state, ins, "noop use != 0");
10165 }
10166 }
10167}
10168
Eric Biederman530b5192003-07-01 10:05:30 +000010169static void simplify_label(struct compile_state *state, struct triple *ins)
10170{
Eric Biederman83b991a2003-10-11 06:20:25 +000010171 /* Ignore volatile labels */
10172 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010173 return;
10174 }
10175 if (ins->use == 0) {
10176 ins->op = OP_NOOP;
10177 }
10178 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010179 /* In general it is not safe to merge one label that
10180 * imediately follows another. The problem is that the empty
10181 * looking block may have phi functions that depend on it.
10182 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010183 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010184 struct triple_set *user, *next;
10185 ins->op = OP_NOOP;
10186 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010187 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010188 next = user->next;
10189 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010190 expr = triple_targ(state, use, 0);
10191 for(;expr; expr = triple_targ(state, use, expr)) {
10192 if (*expr == ins) {
10193 *expr = ins->prev;
10194 unuse_triple(ins, use);
10195 use_triple(ins->prev, use);
10196 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010197
Eric Biederman530b5192003-07-01 10:05:30 +000010198 }
10199 }
10200 if (ins->use) {
10201 internal_error(state, ins, "noop use != 0");
10202 }
10203 }
10204 }
10205}
10206
Eric Biedermanb138ac82003-04-22 18:44:01 +000010207static void simplify_phi(struct compile_state *state, struct triple *ins)
10208{
Eric Biederman83b991a2003-10-11 06:20:25 +000010209 struct triple **slot;
10210 struct triple *value;
10211 int zrhs, i;
10212 ulong_t cvalue;
10213 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010214 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010215 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010216 return;
10217 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010218 /* See if all of the rhs members of a phi have the same value */
10219 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010220 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010221 for(i = 1; i < zrhs; i++) {
10222 if ( !slot[i] ||
10223 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010224 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010225 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010226 break;
10227 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010228 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010229 if (i == zrhs) {
10230 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010231 return;
10232 }
10233 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010234
Eric Biederman83b991a2003-10-11 06:20:25 +000010235 /* See if all of rhs members of a phi are the same */
10236 value = slot[0];
10237 for(i = 1; i < zrhs; i++) {
10238 if (slot[i] != value) {
10239 break;
10240 }
10241 }
10242 if (i == zrhs) {
10243 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010244 if (!is_subset_type(ins->type, value->type)) {
10245 internal_error(state, ins, "bad input type to phi");
10246 }
10247 /* Make the types match */
10248 if (!equiv_types(ins->type, value->type)) {
10249 ins->type = value->type;
10250 }
10251 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010252 mkcopy(state, ins, value);
10253 return;
10254 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010255}
10256
10257
10258static void simplify_bsf(struct compile_state *state, struct triple *ins)
10259{
Eric Biederman90089602004-05-28 14:11:54 +000010260 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010261 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010262 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010263 mkconst(state, ins, bsf(left));
10264 }
10265}
10266
10267static void simplify_bsr(struct compile_state *state, struct triple *ins)
10268{
Eric Biederman90089602004-05-28 14:11:54 +000010269 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010270 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010271 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010272 mkconst(state, ins, bsr(left));
10273 }
10274}
10275
10276
10277typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010278static const struct simplify_table {
10279 simplify_t func;
10280 unsigned long flag;
10281} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010282#define simplify_sdivt simplify_noop
10283#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010284#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010285
Eric Biederman5ade04a2003-10-22 04:03:46 +000010286[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10287[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10288[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10289[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10290[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10291[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10292[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10293[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10294[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10295[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10296[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10297[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10298[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10299[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10300[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10301[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10302[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10303[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10304[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010305
Eric Biederman5ade04a2003-10-22 04:03:46 +000010306[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10307[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10308[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10309[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10310[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10311[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10312[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010318
Eric Biederman90089602004-05-28 14:11:54 +000010319[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010320[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010321
Eric Biederman90089602004-05-28 14:11:54 +000010322[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10323[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10324[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10325
Eric Biederman5ade04a2003-10-22 04:03:46 +000010326[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010327
Eric Biederman5ade04a2003-10-22 04:03:46 +000010328[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10329[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10330[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010331[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010332
Eric Biederman5ade04a2003-10-22 04:03:46 +000010333[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10334[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10335[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010336[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010337[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10338[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010339
Eric Biederman5ade04a2003-10-22 04:03:46 +000010340[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010341[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010342
Eric Biederman5ade04a2003-10-22 04:03:46 +000010343[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10344[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10345[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10346[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10347[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10348[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10349[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10350[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10351[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010352
Eric Biederman5ade04a2003-10-22 04:03:46 +000010353[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10354[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10355[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10356[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10357[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10358[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10359[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10360[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10361[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010362[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010363[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010364};
10365
Stefan Reinauer14e22772010-04-27 06:56:47 +000010366static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010367 simplify_t do_simplify, struct triple *ins)
10368{
10369#if DEBUG_SIMPLIFY_HIRES
10370 if (state->functions_joined && (do_simplify != simplify_noop)) {
10371 /* High resolution debugging mode */
10372 fprintf(state->dbgout, "simplifing: ");
10373 display_triple(state->dbgout, ins);
10374 }
10375#endif
10376 do_simplify(state, ins);
10377#if DEBUG_SIMPLIFY_HIRES
10378 if (state->functions_joined && (do_simplify != simplify_noop)) {
10379 /* High resolution debugging mode */
10380 fprintf(state->dbgout, "simplified: ");
10381 display_triple(state->dbgout, ins);
10382 }
10383#endif
10384}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010385static void simplify(struct compile_state *state, struct triple *ins)
10386{
10387 int op;
10388 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010389 if (ins == &unknown_triple) {
10390 internal_error(state, ins, "simplifying the unknown triple?");
10391 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010392 do {
10393 op = ins->op;
10394 do_simplify = 0;
Patrick Georgi0dde01c2013-11-11 15:57:09 +010010395 if ((op < 0) || (op >= sizeof(table_simplify)/sizeof(table_simplify[0]))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010396 do_simplify = 0;
10397 }
Eric Biederman90089602004-05-28 14:11:54 +000010398 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010399 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010400 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010401 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010402 !(state->compiler->flags & table_simplify[op].flag)) {
10403 do_simplify = simplify_noop;
10404 }
10405 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010406 do_simplify = simplify_noop;
10407 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010408
Eric Biedermanb138ac82003-04-22 18:44:01 +000010409 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010410 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010411 op, tops(op));
10412 return;
10413 }
Eric Biederman90089602004-05-28 14:11:54 +000010414 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010415 } while(ins->op != op);
10416}
10417
Eric Biederman5ade04a2003-10-22 04:03:46 +000010418static void rebuild_ssa_form(struct compile_state *state);
10419
Eric Biedermanb138ac82003-04-22 18:44:01 +000010420static void simplify_all(struct compile_state *state)
10421{
10422 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010423 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10424 return;
10425 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010426 first = state->first;
10427 ins = first->prev;
10428 do {
10429 simplify(state, ins);
10430 ins = ins->prev;
10431 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010432 ins = first;
10433 do {
10434 simplify(state, ins);
10435 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010436 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010437 rebuild_ssa_form(state);
10438
Eric Biederman90089602004-05-28 14:11:54 +000010439 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010440}
10441
10442/*
10443 * Builtins....
10444 * ============================
10445 */
10446
Eric Biederman0babc1c2003-05-09 02:39:00 +000010447static void register_builtin_function(struct compile_state *state,
10448 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010449{
Eric Biederman90089602004-05-28 14:11:54 +000010450 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010451 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010452 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010453 struct file_state file;
10454 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010455 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010456 va_list args;
10457 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010458
10459 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010460 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010461 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010462 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010463 file.report_line = 1;
10464 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010465 file.prev = state->file;
10466 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010467 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010468
10469 /* Find the Parameter count */
10470 valid_op(state, op);
10471 parameters = table_ops[op].rhs;
10472 if (parameters < 0 ) {
10473 internal_error(state, 0, "Invalid builtin parameter count");
10474 }
10475
10476 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010477 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010478 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010479 next = &ftype->right;
10480 va_start(args, rtype);
10481 for(i = 0; i < parameters; i++) {
10482 atype = va_arg(args, struct type *);
10483 if (!*next) {
10484 *next = atype;
10485 } else {
10486 *next = new_type(TYPE_PRODUCT, *next, atype);
10487 next = &((*next)->right);
10488 }
10489 }
10490 if (!*next) {
10491 *next = &void_type;
10492 }
10493 va_end(args);
10494
Eric Biederman90089602004-05-28 14:11:54 +000010495 /* Get the initial closure type */
10496 ctype = new_type(TYPE_JOIN, &void_type, 0);
10497 ctype->elements = 1;
10498
10499 /* Get the return type */
10500 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10501 crtype->elements = 2;
10502
Eric Biedermanb138ac82003-04-22 18:44:01 +000010503 /* Generate the needed triples */
10504 def = triple(state, OP_LIST, ftype, 0, 0);
10505 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010506 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010507 result = flatten(state, first, variable(state, crtype));
10508 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010509 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010510
10511 /* Now string them together */
10512 param = ftype->right;
10513 for(i = 0; i < parameters; i++) {
10514 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10515 atype = param->left;
10516 } else {
10517 atype = param;
10518 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010519 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010520 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010521 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010522 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010523 generate_lhs_pieces(state, work);
10524 for(i = 0; i < parameters; i++) {
10525 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010526 }
Eric Biederman90089602004-05-28 14:11:54 +000010527 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10528 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010529 }
Patrick Georgi37935572017-01-02 21:39:43 +010010530 flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010531 flatten(state, first, label(state));
Patrick Georgi37935572017-01-02 21:39:43 +010010532 flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010533 name_len = strlen(name);
10534 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010535 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010536 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010537
Eric Biedermanb138ac82003-04-22 18:44:01 +000010538 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010539 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010540 state->main_function = 0;
10541
Eric Biederman5ade04a2003-10-22 04:03:46 +000010542 if (!state->functions) {
10543 state->functions = def;
10544 } else {
10545 insert_triple(state, state->functions, def);
10546 }
10547 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010548 FILE *fp = state->dbgout;
10549 fprintf(fp, "\n");
10550 loc(fp, state, 0);
10551 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10552 display_func(state, fp, def);
10553 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010554 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010555}
10556
Eric Biederman0babc1c2003-05-09 02:39:00 +000010557static struct type *partial_struct(struct compile_state *state,
10558 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010559{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010560 struct hash_entry *field_ident;
10561 struct type *result;
10562 int field_name_len;
10563
10564 field_name_len = strlen(field_name);
10565 field_ident = lookup(state, field_name, field_name_len);
10566
10567 result = clone_type(0, type);
10568 result->field_ident = field_ident;
10569
10570 if (rest) {
10571 result = new_type(TYPE_PRODUCT, result, rest);
10572 }
10573 return result;
10574}
10575
10576static struct type *register_builtin_type(struct compile_state *state,
10577 const char *name, struct type *type)
10578{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010579 struct hash_entry *ident;
10580 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010581
Eric Biedermanb138ac82003-04-22 18:44:01 +000010582 name_len = strlen(name);
10583 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010584
Eric Biederman0babc1c2003-05-09 02:39:00 +000010585 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10586 ulong_t elements = 0;
10587 struct type *field;
10588 type = new_type(TYPE_STRUCT, type, 0);
10589 field = type->left;
10590 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10591 elements++;
10592 field = field->right;
10593 }
10594 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010595 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010596 type->type_ident = ident;
10597 type->elements = elements;
10598 }
10599 symbol(state, ident, &ident->sym_ident, 0, type);
10600 ident->tok = TOK_TYPE_NAME;
10601 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010602}
10603
Eric Biederman0babc1c2003-05-09 02:39:00 +000010604
Eric Biedermanb138ac82003-04-22 18:44:01 +000010605static void register_builtins(struct compile_state *state)
10606{
Eric Biederman530b5192003-07-01 10:05:30 +000010607 struct type *div_type, *ldiv_type;
10608 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010609 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010610
Eric Biederman530b5192003-07-01 10:05:30 +000010611 div_type = register_builtin_type(state, "__builtin_div_t",
10612 partial_struct(state, "quot", &int_type,
10613 partial_struct(state, "rem", &int_type, 0)));
10614 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10615 partial_struct(state, "quot", &long_type,
10616 partial_struct(state, "rem", &long_type, 0)));
10617 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10618 partial_struct(state, "quot", &uint_type,
10619 partial_struct(state, "rem", &uint_type, 0)));
10620 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10621 partial_struct(state, "quot", &ulong_type,
10622 partial_struct(state, "rem", &ulong_type, 0)));
10623
10624 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10625 &int_type, &int_type);
10626 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10627 &long_type, &long_type);
10628 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10629 &uint_type, &uint_type);
10630 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10631 &ulong_type, &ulong_type);
10632
Stefan Reinauer14e22772010-04-27 06:56:47 +000010633 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010634 &ushort_type);
10635 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10636 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010637 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010638 &ushort_type);
10639
Stefan Reinauer14e22772010-04-27 06:56:47 +000010640 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010641 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010642 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010643 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010644 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010645 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010646
10647 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010648 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010649 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010650 &int_type);
10651
10652 msr_type = register_builtin_type(state, "__builtin_msr_t",
10653 partial_struct(state, "lo", &ulong_type,
10654 partial_struct(state, "hi", &ulong_type, 0)));
10655
10656 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10657 &ulong_type);
10658 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10659 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010660
10661 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010662 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010663}
10664
10665static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010666 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010667 struct hash_entry **ident, int need_ident);
10668static void decl(struct compile_state *state, struct triple *first);
10669static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010670#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010671static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010672#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010673static struct type *decl_specifiers(struct compile_state *state);
10674static int istype(int tok);
10675static struct triple *expr(struct compile_state *state);
10676static struct triple *assignment_expr(struct compile_state *state);
10677static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010678static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010679
10680static struct triple *call_expr(
10681 struct compile_state *state, struct triple *func)
10682{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010683 struct triple *def;
10684 struct type *param, *type;
10685 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010686
10687 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10688 error(state, 0, "Called object is not a function");
10689 }
10690 if (func->op != OP_LIST) {
10691 internal_error(state, 0, "improper function");
10692 }
10693 eat(state, TOK_LPAREN);
10694 /* Find the return type without any specifiers */
10695 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010696 /* Count the number of rhs entries for OP_FCALL */
10697 param = func->type->right;
10698 pvals = 0;
10699 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10700 pvals++;
10701 param = param->right;
10702 }
10703 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10704 pvals++;
10705 }
10706 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010707 MISC(def, 0) = func;
10708
10709 param = func->type->right;
10710 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010711 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010712 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010713 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010714 arg_type = param;
10715 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10716 arg_type = param->left;
10717 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010718 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010719 RHS(def, index) = val;
10720 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010721 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010722 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010723 }
10724 }
10725 eat(state, TOK_RPAREN);
10726 return def;
10727}
10728
10729
10730static struct triple *character_constant(struct compile_state *state)
10731{
10732 struct triple *def;
10733 struct token *tk;
10734 const signed char *str, *end;
10735 int c;
10736 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010737 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010738 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010739 str_len = tk->str_len - 2;
10740 if (str_len <= 0) {
10741 error(state, 0, "empty character constant");
10742 }
10743 end = str + str_len;
10744 c = char_value(state, &str, end);
10745 if (str != end) {
10746 error(state, 0, "multibyte character constant not supported");
10747 }
10748 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10749 return def;
10750}
10751
10752static struct triple *string_constant(struct compile_state *state)
10753{
10754 struct triple *def;
10755 struct token *tk;
10756 struct type *type;
10757 const signed char *str, *end;
10758 signed char *buf, *ptr;
10759 int str_len;
10760
10761 buf = 0;
10762 type = new_type(TYPE_ARRAY, &char_type, 0);
10763 type->elements = 0;
10764 /* The while loop handles string concatenation */
10765 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010766 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010767 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010768 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010769 if (str_len < 0) {
10770 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010771 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010772 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010773 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010774 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010775 end = str + str_len;
10776 ptr = buf;
10777 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10778 memcpy(buf, ptr, type->elements);
Patrick Georgif23cba02016-12-15 15:12:16 +010010779 free(ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010780 ptr = buf + type->elements;
10781 do {
10782 *ptr++ = char_value(state, &str, end);
10783 } while(str < end);
10784 type->elements = ptr - buf;
10785 } while(peek(state) == TOK_LIT_STRING);
10786 *ptr = '\0';
10787 type->elements += 1;
10788 def = triple(state, OP_BLOBCONST, type, 0, 0);
10789 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010790
Eric Biedermanb138ac82003-04-22 18:44:01 +000010791 return def;
10792}
10793
10794
10795static struct triple *integer_constant(struct compile_state *state)
10796{
10797 struct triple *def;
10798 unsigned long val;
10799 struct token *tk;
10800 char *end;
10801 int u, l, decimal;
10802 struct type *type;
10803
Eric Biederman41203d92004-11-08 09:31:09 +000010804 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010805 errno = 0;
10806 decimal = (tk->val.str[0] != '0');
10807 val = strtoul(tk->val.str, &end, 0);
Patrick Georgib6239b82014-08-09 19:42:08 +020010808 if (errno == ERANGE) {
10809 error(state, 0, "Integer constant out of range");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010810 }
10811 u = l = 0;
10812 if ((*end == 'u') || (*end == 'U')) {
10813 u = 1;
10814 end++;
10815 }
10816 if ((*end == 'l') || (*end == 'L')) {
10817 l = 1;
10818 end++;
10819 }
10820 if ((*end == 'u') || (*end == 'U')) {
10821 u = 1;
10822 end++;
10823 }
10824 if (*end) {
10825 error(state, 0, "Junk at end of integer constant");
10826 }
10827 if (u && l) {
10828 type = &ulong_type;
10829 }
10830 else if (l) {
10831 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010832 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010833 type = &ulong_type;
10834 }
10835 }
10836 else if (u) {
10837 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010838 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010839 type = &ulong_type;
10840 }
10841 }
10842 else {
10843 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010844 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010845 type = &uint_type;
10846 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010847 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010848 type = &ulong_type;
10849 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010850 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010851 type = &long_type;
10852 }
10853 }
10854 def = int_const(state, type, val);
10855 return def;
10856}
10857
10858static struct triple *primary_expr(struct compile_state *state)
10859{
10860 struct triple *def;
10861 int tok;
10862 tok = peek(state);
10863 switch(tok) {
10864 case TOK_IDENT:
10865 {
10866 struct hash_entry *ident;
10867 /* Here ident is either:
10868 * a varable name
10869 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010870 */
Eric Biederman41203d92004-11-08 09:31:09 +000010871 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010872 if (!ident->sym_ident) {
10873 error(state, 0, "%s undeclared", ident->name);
10874 }
10875 def = ident->sym_ident->def;
10876 break;
10877 }
10878 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010879 {
10880 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010881 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010882 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010883 if (!ident->sym_ident) {
10884 error(state, 0, "%s undeclared", ident->name);
10885 }
10886 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010887 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010888 }
Eric Biederman41203d92004-11-08 09:31:09 +000010889 case TOK_MIDENT:
10890 {
10891 struct hash_entry *ident;
10892 ident = eat(state, TOK_MIDENT)->ident;
10893 warning(state, 0, "Replacing undefined macro: %s with 0",
10894 ident->name);
10895 def = int_const(state, &int_type, 0);
10896 break;
10897 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010898 case TOK_LPAREN:
10899 eat(state, TOK_LPAREN);
10900 def = expr(state);
10901 eat(state, TOK_RPAREN);
10902 break;
10903 case TOK_LIT_INT:
10904 def = integer_constant(state);
10905 break;
10906 case TOK_LIT_FLOAT:
10907 eat(state, TOK_LIT_FLOAT);
10908 error(state, 0, "Floating point constants not supported");
10909 def = 0;
10910 FINISHME();
10911 break;
10912 case TOK_LIT_CHAR:
10913 def = character_constant(state);
10914 break;
10915 case TOK_LIT_STRING:
10916 def = string_constant(state);
10917 break;
10918 default:
10919 def = 0;
10920 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10921 }
10922 return def;
10923}
10924
10925static struct triple *postfix_expr(struct compile_state *state)
10926{
10927 struct triple *def;
10928 int postfix;
10929 def = primary_expr(state);
10930 do {
10931 struct triple *left;
10932 int tok;
10933 postfix = 1;
10934 left = def;
10935 switch((tok = peek(state))) {
10936 case TOK_LBRACKET:
10937 eat(state, TOK_LBRACKET);
10938 def = mk_subscript_expr(state, left, expr(state));
10939 eat(state, TOK_RBRACKET);
10940 break;
10941 case TOK_LPAREN:
10942 def = call_expr(state, def);
10943 break;
10944 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010945 {
10946 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010947 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010948 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010949 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010950 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010952 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010953 {
10954 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010955 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010956 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010957 def = mk_deref_expr(state, read_expr(state, def));
10958 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010959 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010960 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010961 case TOK_PLUSPLUS:
10962 eat(state, TOK_PLUSPLUS);
10963 def = mk_post_inc_expr(state, left);
10964 break;
10965 case TOK_MINUSMINUS:
10966 eat(state, TOK_MINUSMINUS);
10967 def = mk_post_dec_expr(state, left);
10968 break;
10969 default:
10970 postfix = 0;
10971 break;
10972 }
10973 } while(postfix);
10974 return def;
10975}
10976
10977static struct triple *cast_expr(struct compile_state *state);
10978
10979static struct triple *unary_expr(struct compile_state *state)
10980{
10981 struct triple *def, *right;
10982 int tok;
10983 switch((tok = peek(state))) {
10984 case TOK_PLUSPLUS:
10985 eat(state, TOK_PLUSPLUS);
10986 def = mk_pre_inc_expr(state, unary_expr(state));
10987 break;
10988 case TOK_MINUSMINUS:
10989 eat(state, TOK_MINUSMINUS);
10990 def = mk_pre_dec_expr(state, unary_expr(state));
10991 break;
10992 case TOK_AND:
10993 eat(state, TOK_AND);
10994 def = mk_addr_expr(state, cast_expr(state), 0);
10995 break;
10996 case TOK_STAR:
10997 eat(state, TOK_STAR);
10998 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
10999 break;
11000 case TOK_PLUS:
11001 eat(state, TOK_PLUS);
11002 right = read_expr(state, cast_expr(state));
11003 arithmetic(state, right);
11004 def = integral_promotion(state, right);
11005 break;
11006 case TOK_MINUS:
11007 eat(state, TOK_MINUS);
11008 right = read_expr(state, cast_expr(state));
11009 arithmetic(state, right);
11010 def = integral_promotion(state, right);
11011 def = triple(state, OP_NEG, def->type, def, 0);
11012 break;
11013 case TOK_TILDE:
11014 eat(state, TOK_TILDE);
11015 right = read_expr(state, cast_expr(state));
11016 integral(state, right);
11017 def = integral_promotion(state, right);
11018 def = triple(state, OP_INVERT, def->type, def, 0);
11019 break;
11020 case TOK_BANG:
11021 eat(state, TOK_BANG);
11022 right = read_expr(state, cast_expr(state));
11023 bool(state, right);
11024 def = lfalse_expr(state, right);
11025 break;
11026 case TOK_SIZEOF:
11027 {
11028 struct type *type;
11029 int tok1, tok2;
11030 eat(state, TOK_SIZEOF);
11031 tok1 = peek(state);
11032 tok2 = peek2(state);
11033 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11034 eat(state, TOK_LPAREN);
11035 type = type_name(state);
11036 eat(state, TOK_RPAREN);
11037 }
11038 else {
11039 struct triple *expr;
11040 expr = unary_expr(state);
11041 type = expr->type;
11042 release_expr(state, expr);
11043 }
Eric Biederman90089602004-05-28 14:11:54 +000011044 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011045 break;
11046 }
11047 case TOK_ALIGNOF:
11048 {
11049 struct type *type;
11050 int tok1, tok2;
11051 eat(state, TOK_ALIGNOF);
11052 tok1 = peek(state);
11053 tok2 = peek2(state);
11054 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11055 eat(state, TOK_LPAREN);
11056 type = type_name(state);
11057 eat(state, TOK_RPAREN);
11058 }
11059 else {
11060 struct triple *expr;
11061 expr = unary_expr(state);
11062 type = expr->type;
11063 release_expr(state, expr);
11064 }
Eric Biederman90089602004-05-28 14:11:54 +000011065 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011066 break;
11067 }
Eric Biederman41203d92004-11-08 09:31:09 +000011068 case TOK_MDEFINED:
11069 {
11070 /* We only come here if we are called from the preprocessor */
11071 struct hash_entry *ident;
11072 int parens;
11073 eat(state, TOK_MDEFINED);
11074 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011075 if (pp_peek(state) == TOK_LPAREN) {
11076 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011077 parens = 1;
11078 }
Eric Biedermancb364952004-11-15 10:46:44 +000011079 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011080 if (parens) {
11081 eat(state, TOK_RPAREN);
11082 }
11083 def = int_const(state, &int_type, ident->sym_define != 0);
11084 break;
11085 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011086 default:
11087 def = postfix_expr(state);
11088 break;
11089 }
11090 return def;
11091}
11092
11093static struct triple *cast_expr(struct compile_state *state)
11094{
11095 struct triple *def;
11096 int tok1, tok2;
11097 tok1 = peek(state);
11098 tok2 = peek2(state);
11099 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11100 struct type *type;
11101 eat(state, TOK_LPAREN);
11102 type = type_name(state);
11103 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011104 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011105 }
11106 else {
11107 def = unary_expr(state);
11108 }
11109 return def;
11110}
11111
11112static struct triple *mult_expr(struct compile_state *state)
11113{
11114 struct triple *def;
11115 int done;
11116 def = cast_expr(state);
11117 do {
11118 struct triple *left, *right;
11119 struct type *result_type;
11120 int tok, op, sign;
11121 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011122 tok = peek(state);
11123 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011124 case TOK_STAR:
11125 case TOK_DIV:
11126 case TOK_MOD:
11127 left = read_expr(state, def);
11128 arithmetic(state, left);
11129
11130 eat(state, tok);
11131
11132 right = read_expr(state, cast_expr(state));
11133 arithmetic(state, right);
11134
11135 result_type = arithmetic_result(state, left, right);
11136 sign = is_signed(result_type);
11137 op = -1;
11138 switch(tok) {
11139 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11140 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11141 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11142 }
11143 def = triple(state, op, result_type, left, right);
11144 break;
11145 default:
11146 done = 1;
11147 break;
11148 }
11149 } while(!done);
11150 return def;
11151}
11152
11153static struct triple *add_expr(struct compile_state *state)
11154{
11155 struct triple *def;
11156 int done;
11157 def = mult_expr(state);
11158 do {
11159 done = 0;
11160 switch( peek(state)) {
11161 case TOK_PLUS:
11162 eat(state, TOK_PLUS);
11163 def = mk_add_expr(state, def, mult_expr(state));
11164 break;
11165 case TOK_MINUS:
11166 eat(state, TOK_MINUS);
11167 def = mk_sub_expr(state, def, mult_expr(state));
11168 break;
11169 default:
11170 done = 1;
11171 break;
11172 }
11173 } while(!done);
11174 return def;
11175}
11176
11177static struct triple *shift_expr(struct compile_state *state)
11178{
11179 struct triple *def;
11180 int done;
11181 def = add_expr(state);
11182 do {
11183 struct triple *left, *right;
11184 int tok, op;
11185 done = 0;
11186 switch((tok = peek(state))) {
11187 case TOK_SL:
11188 case TOK_SR:
11189 left = read_expr(state, def);
11190 integral(state, left);
11191 left = integral_promotion(state, left);
11192
11193 eat(state, tok);
11194
11195 right = read_expr(state, add_expr(state));
11196 integral(state, right);
11197 right = integral_promotion(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011198
11199 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011200 is_signed(left->type)? OP_SSR: OP_USR;
11201
11202 def = triple(state, op, left->type, left, right);
11203 break;
11204 default:
11205 done = 1;
11206 break;
11207 }
11208 } while(!done);
11209 return def;
11210}
11211
11212static struct triple *relational_expr(struct compile_state *state)
11213{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011214#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011215#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011216#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011217 struct triple *def;
11218 int done;
11219 def = shift_expr(state);
11220 do {
11221 struct triple *left, *right;
11222 struct type *arg_type;
11223 int tok, op, sign;
11224 done = 0;
11225 switch((tok = peek(state))) {
11226 case TOK_LESS:
11227 case TOK_MORE:
11228 case TOK_LESSEQ:
11229 case TOK_MOREEQ:
11230 left = read_expr(state, def);
11231 arithmetic(state, left);
11232
11233 eat(state, tok);
11234
11235 right = read_expr(state, shift_expr(state));
11236 arithmetic(state, right);
11237
11238 arg_type = arithmetic_result(state, left, right);
11239 sign = is_signed(arg_type);
11240 op = -1;
11241 switch(tok) {
11242 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11243 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11244 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11245 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11246 }
11247 def = triple(state, op, &int_type, left, right);
11248 break;
11249 default:
11250 done = 1;
11251 break;
11252 }
11253 } while(!done);
11254 return def;
11255}
11256
11257static struct triple *equality_expr(struct compile_state *state)
11258{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011259#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011260#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011261#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011262 struct triple *def;
11263 int done;
11264 def = relational_expr(state);
11265 do {
11266 struct triple *left, *right;
11267 int tok, op;
11268 done = 0;
11269 switch((tok = peek(state))) {
11270 case TOK_EQEQ:
11271 case TOK_NOTEQ:
11272 left = read_expr(state, def);
11273 arithmetic(state, left);
11274 eat(state, tok);
11275 right = read_expr(state, relational_expr(state));
11276 arithmetic(state, right);
11277 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11278 def = triple(state, op, &int_type, left, right);
11279 break;
11280 default:
11281 done = 1;
11282 break;
11283 }
11284 } while(!done);
11285 return def;
11286}
11287
11288static struct triple *and_expr(struct compile_state *state)
11289{
11290 struct triple *def;
11291 def = equality_expr(state);
11292 while(peek(state) == TOK_AND) {
11293 struct triple *left, *right;
11294 struct type *result_type;
11295 left = read_expr(state, def);
11296 integral(state, left);
11297 eat(state, TOK_AND);
11298 right = read_expr(state, equality_expr(state));
11299 integral(state, right);
11300 result_type = arithmetic_result(state, left, right);
11301 def = triple(state, OP_AND, result_type, left, right);
11302 }
11303 return def;
11304}
11305
11306static struct triple *xor_expr(struct compile_state *state)
11307{
11308 struct triple *def;
11309 def = and_expr(state);
11310 while(peek(state) == TOK_XOR) {
11311 struct triple *left, *right;
11312 struct type *result_type;
11313 left = read_expr(state, def);
11314 integral(state, left);
11315 eat(state, TOK_XOR);
11316 right = read_expr(state, and_expr(state));
11317 integral(state, right);
11318 result_type = arithmetic_result(state, left, right);
11319 def = triple(state, OP_XOR, result_type, left, right);
11320 }
11321 return def;
11322}
11323
11324static struct triple *or_expr(struct compile_state *state)
11325{
11326 struct triple *def;
11327 def = xor_expr(state);
11328 while(peek(state) == TOK_OR) {
11329 struct triple *left, *right;
11330 struct type *result_type;
11331 left = read_expr(state, def);
11332 integral(state, left);
11333 eat(state, TOK_OR);
11334 right = read_expr(state, xor_expr(state));
11335 integral(state, right);
11336 result_type = arithmetic_result(state, left, right);
11337 def = triple(state, OP_OR, result_type, left, right);
11338 }
11339 return def;
11340}
11341
11342static struct triple *land_expr(struct compile_state *state)
11343{
11344 struct triple *def;
11345 def = or_expr(state);
11346 while(peek(state) == TOK_LOGAND) {
11347 struct triple *left, *right;
11348 left = read_expr(state, def);
11349 bool(state, left);
11350 eat(state, TOK_LOGAND);
11351 right = read_expr(state, or_expr(state));
11352 bool(state, right);
11353
Eric Biederman90089602004-05-28 14:11:54 +000011354 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011355 ltrue_expr(state, left),
11356 ltrue_expr(state, right));
11357 }
11358 return def;
11359}
11360
11361static struct triple *lor_expr(struct compile_state *state)
11362{
11363 struct triple *def;
11364 def = land_expr(state);
11365 while(peek(state) == TOK_LOGOR) {
11366 struct triple *left, *right;
11367 left = read_expr(state, def);
11368 bool(state, left);
11369 eat(state, TOK_LOGOR);
11370 right = read_expr(state, land_expr(state));
11371 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011372
Stefan Reinauer14e22772010-04-27 06:56:47 +000011373 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011374 ltrue_expr(state, left),
11375 ltrue_expr(state, right));
11376 }
11377 return def;
11378}
11379
11380static struct triple *conditional_expr(struct compile_state *state)
11381{
11382 struct triple *def;
11383 def = lor_expr(state);
11384 if (peek(state) == TOK_QUEST) {
11385 struct triple *test, *left, *right;
11386 bool(state, def);
11387 test = ltrue_expr(state, read_expr(state, def));
11388 eat(state, TOK_QUEST);
11389 left = read_expr(state, expr(state));
11390 eat(state, TOK_COLON);
11391 right = read_expr(state, conditional_expr(state));
11392
Eric Biederman90089602004-05-28 14:11:54 +000011393 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011394 }
11395 return def;
11396}
11397
Eric Biederman41203d92004-11-08 09:31:09 +000011398struct cv_triple {
11399 struct triple *val;
11400 int id;
11401};
11402
11403static void set_cv(struct compile_state *state, struct cv_triple *cv,
11404 struct triple *dest, struct triple *val)
11405{
11406 if (cv[dest->id].val) {
11407 free_triple(state, cv[dest->id].val);
11408 }
11409 cv[dest->id].val = val;
11410}
11411static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11412 struct triple *src)
11413{
11414 return cv[src->id].val;
11415}
11416
Eric Biedermanb138ac82003-04-22 18:44:01 +000011417static struct triple *eval_const_expr(
11418 struct compile_state *state, struct triple *expr)
11419{
11420 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011421 if (is_const(expr)) {
11422 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011423 }
Eric Biederman00443072003-06-24 12:34:45 +000011424 else {
11425 /* If we don't start out as a constant simplify into one */
11426 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011427 struct cv_triple *cv;
11428 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011429 head = label(state); /* dummy initial triple */
11430 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011431 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011432 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011433 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011434 }
Eric Biederman41203d92004-11-08 09:31:09 +000011435 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11436 i = 1;
11437 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11438 cv[i].val = 0;
11439 cv[i].id = ptr->id;
11440 ptr->id = i;
11441 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011442 }
Eric Biederman41203d92004-11-08 09:31:09 +000011443 ptr = head->next;
11444 do {
11445 valid_ins(state, ptr);
11446 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011447 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011448 "unexpected %s in constant expression",
11449 tops(ptr->op));
11450 }
11451 else if (ptr->op == OP_LIST) {
11452 }
11453 else if (triple_is_structural(state, ptr)) {
11454 ptr = ptr->next;
11455 }
11456 else if (triple_is_ubranch(state, ptr)) {
11457 ptr = TARG(ptr, 0);
11458 }
11459 else if (triple_is_cbranch(state, ptr)) {
11460 struct triple *cond_val;
11461 cond_val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011462 if (!cond_val || !is_const(cond_val) ||
11463 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011464 {
11465 internal_error(state, ptr, "bad branch condition");
11466 }
11467 if (cond_val->u.cval == 0) {
11468 ptr = ptr->next;
11469 } else {
11470 ptr = TARG(ptr, 0);
11471 }
11472 }
11473 else if (triple_is_branch(state, ptr)) {
11474 error(state, ptr, "bad branch type in constant expression");
11475 }
11476 else if (ptr->op == OP_WRITE) {
11477 struct triple *val;
11478 val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011479
11480 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011481 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011482 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011483 copy_triple(state, val));
11484 ptr = ptr->next;
11485 }
11486 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011487 set_cv(state, cv, ptr,
11488 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011489 get_cv(state, cv, RHS(ptr, 0))));
11490 ptr = ptr->next;
11491 }
11492 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11493 struct triple *val, **rhs;
11494 val = copy_triple(state, ptr);
11495 rhs = triple_rhs(state, val, 0);
11496 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11497 if (!*rhs) {
11498 internal_error(state, ptr, "Missing rhs");
11499 }
11500 *rhs = get_cv(state, cv, *rhs);
11501 }
11502 simplify(state, val);
11503 set_cv(state, cv, ptr, val);
11504 ptr = ptr->next;
11505 }
11506 else {
11507 error(state, ptr, "impure operation in constant expression");
11508 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011509
Eric Biederman41203d92004-11-08 09:31:09 +000011510 } while(ptr != head);
11511
11512 /* Get the result value */
11513 def = get_cv(state, cv, head->prev);
11514 cv[head->prev->id].val = 0;
11515
11516 /* Free the temporary values */
11517 for(i = 0; i < count; i++) {
11518 if (cv[i].val) {
11519 free_triple(state, cv[i].val);
11520 cv[i].val = 0;
11521 }
11522 }
11523 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011524 /* Free the intermediate expressions */
11525 while(head->next != head) {
11526 release_triple(state, head->next);
11527 }
11528 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011529 }
Eric Biederman41203d92004-11-08 09:31:09 +000011530 if (!is_const(def)) {
11531 error(state, expr, "Not a constant expression");
11532 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011533 return def;
11534}
11535
11536static struct triple *constant_expr(struct compile_state *state)
11537{
11538 return eval_const_expr(state, conditional_expr(state));
11539}
11540
11541static struct triple *assignment_expr(struct compile_state *state)
11542{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011543 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011544 int tok, op, sign;
11545 /* The C grammer in K&R shows assignment expressions
11546 * only taking unary expressions as input on their
11547 * left hand side. But specifies the precedence of
11548 * assignemnt as the lowest operator except for comma.
11549 *
11550 * Allowing conditional expressions on the left hand side
11551 * of an assignement results in a grammar that accepts
11552 * a larger set of statements than standard C. As long
11553 * as the subset of the grammar that is standard C behaves
11554 * correctly this should cause no problems.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011555 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011556 * For the extra token strings accepted by the grammar
11557 * none of them should produce a valid lvalue, so they
11558 * should not produce functioning programs.
11559 *
11560 * GCC has this bug as well, so surprises should be minimal.
11561 */
11562 def = conditional_expr(state);
11563 left = def;
11564 switch((tok = peek(state))) {
11565 case TOK_EQ:
11566 lvalue(state, left);
11567 eat(state, TOK_EQ);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011568 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011569 read_expr(state, assignment_expr(state)));
11570 break;
11571 case TOK_TIMESEQ:
11572 case TOK_DIVEQ:
11573 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011574 lvalue(state, left);
11575 arithmetic(state, left);
11576 eat(state, tok);
11577 right = read_expr(state, assignment_expr(state));
11578 arithmetic(state, right);
11579
11580 sign = is_signed(left->type);
11581 op = -1;
11582 switch(tok) {
11583 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11584 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11585 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011586 }
11587 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011588 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011589 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011590 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011591 case TOK_PLUSEQ:
11592 lvalue(state, left);
11593 eat(state, TOK_PLUSEQ);
11594 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011595 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011596 break;
11597 case TOK_MINUSEQ:
11598 lvalue(state, left);
11599 eat(state, TOK_MINUSEQ);
11600 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011601 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011602 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011603 case TOK_SLEQ:
11604 case TOK_SREQ:
11605 case TOK_ANDEQ:
11606 case TOK_XOREQ:
11607 case TOK_OREQ:
11608 lvalue(state, left);
11609 integral(state, left);
11610 eat(state, tok);
11611 right = read_expr(state, assignment_expr(state));
11612 integral(state, right);
11613 right = integral_promotion(state, right);
11614 sign = is_signed(left->type);
11615 op = -1;
11616 switch(tok) {
11617 case TOK_SLEQ: op = OP_SL; break;
11618 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11619 case TOK_ANDEQ: op = OP_AND; break;
11620 case TOK_XOREQ: op = OP_XOR; break;
11621 case TOK_OREQ: op = OP_OR; break;
11622 }
11623 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011624 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011625 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011626 break;
11627 }
11628 return def;
11629}
11630
11631static struct triple *expr(struct compile_state *state)
11632{
11633 struct triple *def;
11634 def = assignment_expr(state);
11635 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011636 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011637 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011638 }
11639 return def;
11640}
11641
11642static void expr_statement(struct compile_state *state, struct triple *first)
11643{
11644 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011645 /* lvalue conversions always apply except when certian operators
11646 * are applied. I apply the lvalue conversions here
11647 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011648 */
11649 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011650 }
11651 eat(state, TOK_SEMI);
11652}
11653
11654static void if_statement(struct compile_state *state, struct triple *first)
11655{
11656 struct triple *test, *jmp1, *jmp2, *middle, *end;
11657
11658 jmp1 = jmp2 = middle = 0;
11659 eat(state, TOK_IF);
11660 eat(state, TOK_LPAREN);
11661 test = expr(state);
11662 bool(state, test);
11663 /* Cleanup and invert the test */
11664 test = lfalse_expr(state, read_expr(state, test));
11665 eat(state, TOK_RPAREN);
11666 /* Generate the needed pieces */
11667 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011668 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011669 /* Thread the pieces together */
11670 flatten(state, first, test);
11671 flatten(state, first, jmp1);
11672 flatten(state, first, label(state));
11673 statement(state, first);
11674 if (peek(state) == TOK_ELSE) {
11675 eat(state, TOK_ELSE);
11676 /* Generate the rest of the pieces */
11677 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011678 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011679 /* Thread them together */
11680 flatten(state, first, jmp2);
11681 flatten(state, first, middle);
11682 statement(state, first);
11683 flatten(state, first, end);
11684 }
11685 else {
11686 flatten(state, first, middle);
11687 }
11688}
11689
11690static void for_statement(struct compile_state *state, struct triple *first)
11691{
11692 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11693 struct triple *label1, *label2, *label3;
11694 struct hash_entry *ident;
11695
11696 eat(state, TOK_FOR);
11697 eat(state, TOK_LPAREN);
11698 head = test = tail = jmp1 = jmp2 = 0;
11699 if (peek(state) != TOK_SEMI) {
11700 head = expr(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011701 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011702 eat(state, TOK_SEMI);
11703 if (peek(state) != TOK_SEMI) {
11704 test = expr(state);
11705 bool(state, test);
11706 test = ltrue_expr(state, read_expr(state, test));
11707 }
11708 eat(state, TOK_SEMI);
11709 if (peek(state) != TOK_RPAREN) {
11710 tail = expr(state);
11711 }
11712 eat(state, TOK_RPAREN);
11713 /* Generate the needed pieces */
11714 label1 = label(state);
11715 label2 = label(state);
11716 label3 = label(state);
11717 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011718 jmp1 = branch(state, label3, 0);
11719 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011720 }
11721 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011722 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011723 }
11724 end = label(state);
11725 /* Remember where break and continue go */
11726 start_scope(state);
11727 ident = state->i_break;
11728 symbol(state, ident, &ident->sym_ident, end, end->type);
11729 ident = state->i_continue;
11730 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11731 /* Now include the body */
11732 flatten(state, first, head);
11733 flatten(state, first, jmp1);
11734 flatten(state, first, label1);
11735 statement(state, first);
11736 flatten(state, first, label2);
11737 flatten(state, first, tail);
11738 flatten(state, first, label3);
11739 flatten(state, first, test);
11740 flatten(state, first, jmp2);
11741 flatten(state, first, end);
11742 /* Cleanup the break/continue scope */
11743 end_scope(state);
11744}
11745
11746static void while_statement(struct compile_state *state, struct triple *first)
11747{
11748 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11749 struct hash_entry *ident;
11750 eat(state, TOK_WHILE);
11751 eat(state, TOK_LPAREN);
11752 test = expr(state);
11753 bool(state, test);
11754 test = ltrue_expr(state, read_expr(state, test));
11755 eat(state, TOK_RPAREN);
11756 /* Generate the needed pieces */
11757 label1 = label(state);
11758 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011759 jmp1 = branch(state, label2, 0);
11760 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011761 end = label(state);
11762 /* Remember where break and continue go */
11763 start_scope(state);
11764 ident = state->i_break;
11765 symbol(state, ident, &ident->sym_ident, end, end->type);
11766 ident = state->i_continue;
11767 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11768 /* Thread them together */
11769 flatten(state, first, jmp1);
11770 flatten(state, first, label1);
11771 statement(state, first);
11772 flatten(state, first, label2);
11773 flatten(state, first, test);
11774 flatten(state, first, jmp2);
11775 flatten(state, first, end);
11776 /* Cleanup the break/continue scope */
11777 end_scope(state);
11778}
11779
11780static void do_statement(struct compile_state *state, struct triple *first)
11781{
11782 struct triple *label1, *label2, *test, *end;
11783 struct hash_entry *ident;
11784 eat(state, TOK_DO);
11785 /* Generate the needed pieces */
11786 label1 = label(state);
11787 label2 = label(state);
11788 end = label(state);
11789 /* Remember where break and continue go */
11790 start_scope(state);
11791 ident = state->i_break;
11792 symbol(state, ident, &ident->sym_ident, end, end->type);
11793 ident = state->i_continue;
11794 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11795 /* Now include the body */
11796 flatten(state, first, label1);
11797 statement(state, first);
11798 /* Cleanup the break/continue scope */
11799 end_scope(state);
11800 /* Eat the rest of the loop */
11801 eat(state, TOK_WHILE);
11802 eat(state, TOK_LPAREN);
11803 test = read_expr(state, expr(state));
11804 bool(state, test);
11805 eat(state, TOK_RPAREN);
11806 eat(state, TOK_SEMI);
11807 /* Thread the pieces together */
11808 test = ltrue_expr(state, test);
11809 flatten(state, first, label2);
11810 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011811 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011812 flatten(state, first, end);
11813}
11814
11815
11816static void return_statement(struct compile_state *state, struct triple *first)
11817{
11818 struct triple *jmp, *mv, *dest, *var, *val;
11819 int last;
11820 eat(state, TOK_RETURN);
11821
Stefan Reinauer50542a82007-10-24 11:14:14 +000011822#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011823#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011824#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011825 val = 0;
11826 /* If we have a return value do some more work */
11827 if (peek(state) != TOK_SEMI) {
11828 val = read_expr(state, expr(state));
11829 }
11830 eat(state, TOK_SEMI);
11831
11832 /* See if this last statement in a function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011833 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011834 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11835
11836 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011837 var = fresult(state, state->main_function);
11838
Eric Biedermanb138ac82003-04-22 18:44:01 +000011839 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011840 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011841 mv = jmp = 0;
11842 /* If needed generate a jump instruction */
11843 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011844 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011845 }
11846 /* If needed generate an assignment instruction */
11847 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011848 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011849 }
11850 /* Now put the code together */
11851 if (mv) {
11852 flatten(state, first, mv);
11853 flatten(state, first, jmp);
11854 }
11855 else if (jmp) {
11856 flatten(state, first, jmp);
11857 }
11858}
11859
11860static void break_statement(struct compile_state *state, struct triple *first)
11861{
11862 struct triple *dest;
11863 eat(state, TOK_BREAK);
11864 eat(state, TOK_SEMI);
11865 if (!state->i_break->sym_ident) {
11866 error(state, 0, "break statement not within loop or switch");
11867 }
11868 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011869 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011870}
11871
11872static void continue_statement(struct compile_state *state, struct triple *first)
11873{
11874 struct triple *dest;
11875 eat(state, TOK_CONTINUE);
11876 eat(state, TOK_SEMI);
11877 if (!state->i_continue->sym_ident) {
11878 error(state, 0, "continue statement outside of a loop");
11879 }
11880 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011881 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011882}
11883
11884static void goto_statement(struct compile_state *state, struct triple *first)
11885{
Eric Biederman153ea352003-06-20 14:43:20 +000011886 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011887 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011888 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011889 if (!ident->sym_label) {
11890 /* If this is a forward branch allocate the label now,
11891 * it will be flattend in the appropriate location later.
11892 */
11893 struct triple *ins;
11894 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011895 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011896 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011897 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011898
11899 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011900}
11901
11902static void labeled_statement(struct compile_state *state, struct triple *first)
11903{
Eric Biederman153ea352003-06-20 14:43:20 +000011904 struct triple *ins;
11905 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011906
Eric Biederman41203d92004-11-08 09:31:09 +000011907 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011908 if (ident->sym_label && ident->sym_label->def) {
11909 ins = ident->sym_label->def;
11910 put_occurance(ins->occurance);
11911 ins->occurance = new_occurance(state);
11912 }
11913 else {
11914 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011915 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011916 }
11917 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11918 error(state, 0, "label %s already defined", ident->name);
11919 }
11920 flatten(state, first, ins);
11921
Eric Biedermanb138ac82003-04-22 18:44:01 +000011922 eat(state, TOK_COLON);
11923 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011924}
11925
11926static void switch_statement(struct compile_state *state, struct triple *first)
11927{
Eric Biederman83b991a2003-10-11 06:20:25 +000011928 struct triple *value, *top, *end, *dbranch;
11929 struct hash_entry *ident;
11930
11931 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011932 eat(state, TOK_SWITCH);
11933 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011934 value = expr(state);
11935 integral(state, value);
11936 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011937 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011938 /* Generate the needed pieces */
11939 top = label(state);
11940 end = label(state);
11941 dbranch = branch(state, end, 0);
11942 /* Remember where case branches and break goes */
11943 start_scope(state);
11944 ident = state->i_switch;
11945 symbol(state, ident, &ident->sym_ident, value, value->type);
11946 ident = state->i_case;
11947 symbol(state, ident, &ident->sym_ident, top, top->type);
11948 ident = state->i_break;
11949 symbol(state, ident, &ident->sym_ident, end, end->type);
11950 ident = state->i_default;
11951 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11952 /* Thread them together */
11953 flatten(state, first, value);
11954 flatten(state, first, top);
11955 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011956 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011957 flatten(state, first, end);
11958 /* Cleanup the switch scope */
11959 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011960}
11961
11962static void case_statement(struct compile_state *state, struct triple *first)
11963{
Eric Biederman83b991a2003-10-11 06:20:25 +000011964 struct triple *cvalue, *dest, *test, *jmp;
11965 struct triple *ptr, *value, *top, *dbranch;
11966
11967 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011968 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011969 cvalue = constant_expr(state);
11970 integral(state, cvalue);
11971 if (cvalue->op != OP_INTCONST) {
11972 error(state, 0, "integer constant expected");
11973 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011974 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011975 if (!state->i_case->sym_ident) {
11976 error(state, 0, "case statement not within a switch");
11977 }
11978
11979 /* Lookup the interesting pieces */
11980 top = state->i_case->sym_ident->def;
11981 value = state->i_switch->sym_ident->def;
11982 dbranch = state->i_default->sym_ident->def;
11983
11984 /* See if this case label has already been used */
11985 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11986 if (ptr->op != OP_EQ) {
11987 continue;
11988 }
11989 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11990 error(state, 0, "duplicate case %d statement",
11991 cvalue->u.cval);
11992 }
11993 }
11994 /* Generate the needed pieces */
11995 dest = label(state);
11996 test = triple(state, OP_EQ, &int_type, value, cvalue);
11997 jmp = branch(state, dest, test);
11998 /* Thread the pieces together */
11999 flatten(state, dbranch, test);
12000 flatten(state, dbranch, jmp);
12001 flatten(state, dbranch, label(state));
12002 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012003 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012004}
12005
12006static void default_statement(struct compile_state *state, struct triple *first)
12007{
Eric Biederman83b991a2003-10-11 06:20:25 +000012008 struct triple *dest;
12009 struct triple *dbranch, *end;
12010
12011 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012012 eat(state, TOK_DEFAULT);
12013 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012014
12015 if (!state->i_case->sym_ident) {
12016 error(state, 0, "default statement not within a switch");
12017 }
12018
12019 /* Lookup the interesting pieces */
12020 dbranch = state->i_default->sym_ident->def;
12021 end = state->i_break->sym_ident->def;
12022
12023 /* See if a default statement has already happened */
12024 if (TARG(dbranch, 0) != end) {
12025 error(state, 0, "duplicate default statement");
12026 }
12027
12028 /* Generate the needed pieces */
12029 dest = label(state);
12030
Eric Biederman90089602004-05-28 14:11:54 +000012031 /* Blame the branch on the default statement */
12032 put_occurance(dbranch->occurance);
12033 dbranch->occurance = new_occurance(state);
12034
Eric Biederman83b991a2003-10-11 06:20:25 +000012035 /* Thread the pieces together */
12036 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012037 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012038 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012039 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012040}
12041
12042static void asm_statement(struct compile_state *state, struct triple *first)
12043{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012044 struct asm_info *info;
12045 struct {
12046 struct triple *constraint;
12047 struct triple *expr;
12048 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12049 struct triple *def, *asm_str;
12050 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012051 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012052
Eric Biederman90089602004-05-28 14:11:54 +000012053 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012054 eat(state, TOK_ASM);
12055 /* For now ignore the qualifiers */
12056 switch(peek(state)) {
12057 case TOK_CONST:
12058 eat(state, TOK_CONST);
12059 break;
12060 case TOK_VOLATILE:
12061 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012062 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012063 break;
12064 }
12065 eat(state, TOK_LPAREN);
12066 asm_str = string_constant(state);
12067
12068 colons = 0;
12069 out = in = clobbers = 0;
12070 /* Outputs */
12071 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12072 eat(state, TOK_COLON);
12073 colons++;
12074 more = (peek(state) == TOK_LIT_STRING);
12075 while(more) {
12076 struct triple *var;
12077 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012078 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012079 more = 0;
12080 if (out > MAX_LHS) {
12081 error(state, 0, "Maximum output count exceeded.");
12082 }
12083 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012084 str = constraint->u.blob;
12085 if (str[0] != '=') {
12086 error(state, 0, "Output constraint does not start with =");
12087 }
12088 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012089 eat(state, TOK_LPAREN);
12090 var = conditional_expr(state);
12091 eat(state, TOK_RPAREN);
12092
12093 lvalue(state, var);
12094 out_param[out].constraint = constraint;
12095 out_param[out].expr = var;
12096 if (peek(state) == TOK_COMMA) {
12097 eat(state, TOK_COMMA);
12098 more = 1;
12099 }
12100 out++;
12101 }
12102 }
12103 /* Inputs */
12104 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12105 eat(state, TOK_COLON);
12106 colons++;
12107 more = (peek(state) == TOK_LIT_STRING);
12108 while(more) {
12109 struct triple *val;
12110 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012111 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012112 more = 0;
12113 if (in > MAX_RHS) {
12114 error(state, 0, "Maximum input count exceeded.");
12115 }
12116 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012117 str = constraint->u.blob;
12118 if (digitp(str[0] && str[1] == '\0')) {
12119 int val;
12120 val = digval(str[0]);
12121 if ((val < 0) || (val >= out)) {
12122 error(state, 0, "Invalid input constraint %d", val);
12123 }
12124 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012125 eat(state, TOK_LPAREN);
12126 val = conditional_expr(state);
12127 eat(state, TOK_RPAREN);
12128
12129 in_param[in].constraint = constraint;
12130 in_param[in].expr = val;
12131 if (peek(state) == TOK_COMMA) {
12132 eat(state, TOK_COMMA);
12133 more = 1;
12134 }
12135 in++;
12136 }
12137 }
12138
12139 /* Clobber */
12140 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12141 eat(state, TOK_COLON);
12142 colons++;
12143 more = (peek(state) == TOK_LIT_STRING);
12144 while(more) {
12145 struct triple *clobber;
12146 more = 0;
12147 if ((clobbers + out) > MAX_LHS) {
12148 error(state, 0, "Maximum clobber limit exceeded.");
12149 }
12150 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012151
12152 clob_param[clobbers].constraint = clobber;
12153 if (peek(state) == TOK_COMMA) {
12154 eat(state, TOK_COMMA);
12155 more = 1;
12156 }
12157 clobbers++;
12158 }
12159 }
12160 eat(state, TOK_RPAREN);
12161 eat(state, TOK_SEMI);
12162
12163
12164 info = xcmalloc(sizeof(*info), "asm_info");
12165 info->str = asm_str->u.blob;
12166 free_triple(state, asm_str);
12167
12168 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12169 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012170 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012171
12172 /* Find the register constraints */
12173 for(i = 0; i < out; i++) {
12174 struct triple *constraint;
12175 constraint = out_param[i].constraint;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012176 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012177 out_param[i].expr->type, constraint->u.blob);
12178 free_triple(state, constraint);
12179 }
12180 for(; i - out < clobbers; i++) {
12181 struct triple *constraint;
12182 constraint = clob_param[i - out].constraint;
12183 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12184 free_triple(state, constraint);
12185 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012186 for(i = 0; i < in; i++) {
12187 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012188 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012189 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012190 str = constraint->u.blob;
12191 if (digitp(str[0]) && str[1] == '\0') {
12192 struct reg_info cinfo;
12193 int val;
12194 val = digval(str[0]);
12195 cinfo.reg = info->tmpl.lhs[val].reg;
12196 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12197 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12198 if (cinfo.reg == REG_UNSET) {
12199 cinfo.reg = REG_VIRT0 + val;
12200 }
12201 if (cinfo.regcm == 0) {
12202 error(state, 0, "No registers for %d", val);
12203 }
12204 info->tmpl.lhs[val] = cinfo;
12205 info->tmpl.rhs[i] = cinfo;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012206
Eric Biederman8d9c1232003-06-17 08:42:17 +000012207 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012208 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012209 in_param[i].expr->type, str);
12210 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012211 free_triple(state, constraint);
12212 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012213
12214 /* Now build the helper expressions */
12215 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012216 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012217 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012218 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012219 for(i = 0; i < (out + clobbers); i++) {
12220 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012221 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012222 if (i < out) {
12223 type = out_param[i].expr->type;
12224 } else {
12225 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12226 if (size >= SIZEOF_LONG) {
12227 type = &ulong_type;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012228 }
Eric Biederman90089602004-05-28 14:11:54 +000012229 else if (size >= SIZEOF_INT) {
12230 type = &uint_type;
12231 }
12232 else if (size >= SIZEOF_SHORT) {
12233 type = &ushort_type;
12234 }
12235 else {
12236 type = &uchar_type;
12237 }
12238 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012239 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012240 piece->u.cval = i;
12241 LHS(def, i) = piece;
12242 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012243 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012244 /* And write the helpers to their destinations */
12245 for(i = 0; i < out; i++) {
12246 struct triple *piece;
12247 piece = LHS(def, i);
12248 flatten(state, first,
12249 write_expr(state, out_param[i].expr, piece));
12250 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012251}
12252
12253
12254static int isdecl(int tok)
12255{
12256 switch(tok) {
12257 case TOK_AUTO:
12258 case TOK_REGISTER:
12259 case TOK_STATIC:
12260 case TOK_EXTERN:
12261 case TOK_TYPEDEF:
12262 case TOK_CONST:
12263 case TOK_RESTRICT:
12264 case TOK_VOLATILE:
12265 case TOK_VOID:
12266 case TOK_CHAR:
12267 case TOK_SHORT:
12268 case TOK_INT:
12269 case TOK_LONG:
12270 case TOK_FLOAT:
12271 case TOK_DOUBLE:
12272 case TOK_SIGNED:
12273 case TOK_UNSIGNED:
12274 case TOK_STRUCT:
12275 case TOK_UNION:
12276 case TOK_ENUM:
12277 case TOK_TYPE_NAME: /* typedef name */
12278 return 1;
12279 default:
12280 return 0;
12281 }
12282}
12283
12284static void compound_statement(struct compile_state *state, struct triple *first)
12285{
12286 eat(state, TOK_LBRACE);
12287 start_scope(state);
12288
12289 /* statement-list opt */
12290 while (peek(state) != TOK_RBRACE) {
12291 statement(state, first);
12292 }
12293 end_scope(state);
12294 eat(state, TOK_RBRACE);
12295}
12296
12297static void statement(struct compile_state *state, struct triple *first)
12298{
12299 int tok;
12300 tok = peek(state);
12301 if (tok == TOK_LBRACE) {
12302 compound_statement(state, first);
12303 }
12304 else if (tok == TOK_IF) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012305 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012306 }
12307 else if (tok == TOK_FOR) {
12308 for_statement(state, first);
12309 }
12310 else if (tok == TOK_WHILE) {
12311 while_statement(state, first);
12312 }
12313 else if (tok == TOK_DO) {
12314 do_statement(state, first);
12315 }
12316 else if (tok == TOK_RETURN) {
12317 return_statement(state, first);
12318 }
12319 else if (tok == TOK_BREAK) {
12320 break_statement(state, first);
12321 }
12322 else if (tok == TOK_CONTINUE) {
12323 continue_statement(state, first);
12324 }
12325 else if (tok == TOK_GOTO) {
12326 goto_statement(state, first);
12327 }
12328 else if (tok == TOK_SWITCH) {
12329 switch_statement(state, first);
12330 }
12331 else if (tok == TOK_ASM) {
12332 asm_statement(state, first);
12333 }
12334 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012335 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012336 }
12337 else if (tok == TOK_CASE) {
12338 case_statement(state, first);
12339 }
12340 else if (tok == TOK_DEFAULT) {
12341 default_statement(state, first);
12342 }
12343 else if (isdecl(tok)) {
12344 /* This handles C99 intermixing of statements and decls */
12345 decl(state, first);
12346 }
12347 else {
12348 expr_statement(state, first);
12349 }
12350}
12351
12352static struct type *param_decl(struct compile_state *state)
12353{
12354 struct type *type;
12355 struct hash_entry *ident;
12356 /* Cheat so the declarator will know we are not global */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012357 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012358 ident = 0;
12359 type = decl_specifiers(state);
12360 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012361 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012362 end_scope(state);
12363 return type;
12364}
12365
12366static struct type *param_type_list(struct compile_state *state, struct type *type)
12367{
12368 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012369 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012370 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012371 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012372 while(peek(state) == TOK_COMMA) {
12373 eat(state, TOK_COMMA);
12374 if (peek(state) == TOK_DOTS) {
12375 eat(state, TOK_DOTS);
12376 error(state, 0, "variadic functions not supported");
12377 }
12378 else {
12379 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12380 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012381 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012382 }
12383 }
12384 return ftype;
12385}
12386
Eric Biedermanb138ac82003-04-22 18:44:01 +000012387static struct type *type_name(struct compile_state *state)
12388{
12389 struct type *type;
12390 type = specifier_qualifier_list(state);
12391 /* abstract-declarator (may consume no tokens) */
12392 type = declarator(state, type, 0, 0);
12393 return type;
12394}
12395
12396static struct type *direct_declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012397 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012398 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012399{
Eric Biederman41203d92004-11-08 09:31:09 +000012400 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012401 struct type *outer;
12402 int op;
12403 outer = 0;
12404 arrays_complete(state, type);
12405 switch(peek(state)) {
12406 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012407 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012408 if (!ident) {
12409 error(state, 0, "Unexpected identifier found");
12410 }
12411 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012412 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012413 break;
12414 case TOK_LPAREN:
12415 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012416 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012417 eat(state, TOK_RPAREN);
12418 break;
12419 default:
12420 if (need_ident) {
12421 error(state, 0, "Identifier expected");
12422 }
12423 break;
12424 }
12425 do {
12426 op = 1;
12427 arrays_complete(state, type);
12428 switch(peek(state)) {
12429 case TOK_LPAREN:
12430 eat(state, TOK_LPAREN);
12431 type = param_type_list(state, type);
12432 eat(state, TOK_RPAREN);
12433 break;
12434 case TOK_LBRACKET:
12435 {
12436 unsigned int qualifiers;
12437 struct triple *value;
12438 value = 0;
12439 eat(state, TOK_LBRACKET);
12440 if (peek(state) != TOK_RBRACKET) {
12441 value = constant_expr(state);
12442 integral(state, value);
12443 }
12444 eat(state, TOK_RBRACKET);
12445
12446 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12447 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12448 if (value) {
12449 type->elements = value->u.cval;
12450 free_triple(state, value);
12451 } else {
12452 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12453 op = 0;
12454 }
12455 }
12456 break;
12457 default:
12458 op = 0;
12459 break;
12460 }
12461 } while(op);
12462 if (outer) {
12463 struct type *inner;
12464 arrays_complete(state, type);
12465 FINISHME();
12466 for(inner = outer; inner->left; inner = inner->left)
12467 ;
12468 inner->left = type;
12469 type = outer;
12470 }
12471 return type;
12472}
12473
12474static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012475 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012476 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012477{
12478 while(peek(state) == TOK_STAR) {
12479 eat(state, TOK_STAR);
12480 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12481 }
Eric Biederman41203d92004-11-08 09:31:09 +000012482 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012483 return type;
12484}
12485
Eric Biedermanb138ac82003-04-22 18:44:01 +000012486static struct type *typedef_name(
12487 struct compile_state *state, unsigned int specifiers)
12488{
12489 struct hash_entry *ident;
12490 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012491 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012492 type = ident->sym_ident->type;
12493 specifiers |= type->type & QUAL_MASK;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012494 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012495 (type->type & (STOR_MASK | QUAL_MASK))) {
12496 type = clone_type(specifiers, type);
12497 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012498 return type;
12499}
12500
12501static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012502 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012503{
Eric Biederman83b991a2003-10-11 06:20:25 +000012504 struct hash_entry *ident;
12505 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012506 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012507 struct type *enum_type;
12508 enum_type = 0;
12509 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012510 eat(state, TOK_ENUM);
12511 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012512 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012513 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012514 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012515 base = 0;
12516 if (!ident || (peek(state) == TOK_LBRACE)) {
12517 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012518 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012519 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12520 enum_type->type_ident = ident;
12521 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012522 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012523 struct hash_entry *eident;
12524 struct triple *value;
12525 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012526 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012527 if (eident->sym_ident) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012528 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012529 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012530 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012531 eident->tok = TOK_ENUM_CONST;
12532 if (peek(state) == TOK_EQ) {
12533 struct triple *val;
12534 eat(state, TOK_EQ);
12535 val = constant_expr(state);
12536 integral(state, val);
12537 base = val->u.cval;
12538 }
12539 value = int_const(state, &int_type, base);
12540 symbol(state, eident, &eident->sym_ident, value, &int_type);
12541 entry = new_type(TYPE_LIST, 0, 0);
12542 entry->field_ident = eident;
12543 *next = entry;
12544 next = &entry->right;
12545 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012546 if (peek(state) == TOK_COMMA) {
12547 eat(state, TOK_COMMA);
12548 }
12549 } while(peek(state) != TOK_RBRACE);
12550 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012551 if (ident) {
12552 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12553 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012554 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012555 if (ident && ident->sym_tag &&
12556 ident->sym_tag->type &&
12557 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12558 enum_type = clone_type(spec, ident->sym_tag->type);
12559 }
12560 else if (ident && !enum_type) {
12561 error(state, 0, "enum %s undeclared", ident->name);
12562 }
12563 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012564}
12565
Eric Biedermanb138ac82003-04-22 18:44:01 +000012566static struct type *struct_declarator(
12567 struct compile_state *state, struct type *type, struct hash_entry **ident)
12568{
Eric Biederman90089602004-05-28 14:11:54 +000012569 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012570 type = declarator(state, type, ident, 1);
12571 }
Eric Biederman90089602004-05-28 14:11:54 +000012572 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012573 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012574 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012575 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012576 if (value->op != OP_INTCONST) {
12577 error(state, 0, "Invalid constant expression");
12578 }
12579 if (value->u.cval > size_of(state, type)) {
12580 error(state, 0, "bitfield larger than base type");
12581 }
12582 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12583 error(state, 0, "bitfield base not an integer type");
12584 }
12585 type = new_type(TYPE_BITFIELD, type, 0);
12586 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012587 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012588 return type;
12589}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012590
12591static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012592 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012593{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012594 struct type *struct_type;
12595 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012596 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012597 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012598 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012599 struct_type = 0;
12600 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012601 switch(peek(state)) {
12602 case TOK_STRUCT:
12603 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012604 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012605 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012606 break;
12607 case TOK_UNION:
12608 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012609 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012610 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012611 break;
12612 default:
12613 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012614 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012615 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012616 break;
12617 }
12618 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012619 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012620 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012621 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012622 if (!ident || (peek(state) == TOK_LBRACE)) {
12623 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012624 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012625 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012626 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012627 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012628 do {
12629 struct type *base_type;
12630 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012631 base_type = specifier_qualifier_list(state);
12632 do {
12633 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012634 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012635 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012636 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012637 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012638 if (peek(state) == TOK_COMMA) {
12639 done = 0;
12640 eat(state, TOK_COMMA);
12641 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012642 type = clone_type(0, type);
12643 type->field_ident = fident;
12644 if (*next) {
12645 *next = new_type(type_join, *next, type);
12646 next = &((*next)->right);
12647 } else {
12648 *next = type;
12649 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012650 } while(!done);
12651 eat(state, TOK_SEMI);
12652 } while(peek(state) != TOK_RBRACE);
12653 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012654 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012655 struct_type->type_ident = ident;
12656 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012657 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012658 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012659 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012660 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000012661 if (ident && ident->sym_tag &&
12662 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012663 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012664 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012665 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012666 else if (ident && !struct_type) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012667 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012668 (type_main == TYPE_STRUCT)?"struct" : "union",
12669 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012670 }
12671 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012672}
12673
12674static unsigned int storage_class_specifier_opt(struct compile_state *state)
12675{
12676 unsigned int specifiers;
12677 switch(peek(state)) {
12678 case TOK_AUTO:
12679 eat(state, TOK_AUTO);
12680 specifiers = STOR_AUTO;
12681 break;
12682 case TOK_REGISTER:
12683 eat(state, TOK_REGISTER);
12684 specifiers = STOR_REGISTER;
12685 break;
12686 case TOK_STATIC:
12687 eat(state, TOK_STATIC);
12688 specifiers = STOR_STATIC;
12689 break;
12690 case TOK_EXTERN:
12691 eat(state, TOK_EXTERN);
12692 specifiers = STOR_EXTERN;
12693 break;
12694 case TOK_TYPEDEF:
12695 eat(state, TOK_TYPEDEF);
12696 specifiers = STOR_TYPEDEF;
12697 break;
12698 default:
12699 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012700 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012701 }
12702 else {
12703 specifiers = STOR_AUTO;
12704 }
12705 }
12706 return specifiers;
12707}
12708
12709static unsigned int function_specifier_opt(struct compile_state *state)
12710{
12711 /* Ignore the inline keyword */
12712 unsigned int specifiers;
12713 specifiers = 0;
12714 switch(peek(state)) {
12715 case TOK_INLINE:
12716 eat(state, TOK_INLINE);
12717 specifiers = STOR_INLINE;
12718 }
12719 return specifiers;
12720}
12721
Eric Biederman90089602004-05-28 14:11:54 +000012722static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12723{
12724 int tok = peek(state);
12725 switch(tok) {
12726 case TOK_COMMA:
12727 case TOK_LPAREN:
12728 /* The empty attribute ignore it */
12729 break;
12730 case TOK_IDENT:
12731 case TOK_ENUM_CONST:
12732 case TOK_TYPE_NAME:
12733 {
12734 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012735 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012736
12737 if (ident == state->i_noinline) {
12738 if (attributes & ATTRIB_ALWAYS_INLINE) {
12739 error(state, 0, "both always_inline and noinline attribtes");
12740 }
12741 attributes |= ATTRIB_NOINLINE;
12742 }
12743 else if (ident == state->i_always_inline) {
12744 if (attributes & ATTRIB_NOINLINE) {
12745 error(state, 0, "both noinline and always_inline attribtes");
12746 }
12747 attributes |= ATTRIB_ALWAYS_INLINE;
12748 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012749 else if (ident == state->i_noreturn) {
12750 // attribute((noreturn)) does nothing (yet?)
12751 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012752 else if (ident == state->i_unused) {
12753 // attribute((unused)) does nothing (yet?)
12754 }
Stefan Reinauerec664bc2013-05-09 14:06:04 -070012755 else if (ident == state->i_packed) {
12756 // attribute((packed)) does nothing (yet?)
12757 }
Eric Biederman90089602004-05-28 14:11:54 +000012758 else {
12759 error(state, 0, "Unknown attribute:%s", ident->name);
12760 }
12761 break;
12762 }
12763 default:
12764 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12765 break;
12766 }
12767 return attributes;
12768}
12769
12770static unsigned int attribute_list(struct compile_state *state, unsigned type)
12771{
12772 type = attrib(state, type);
12773 while(peek(state) == TOK_COMMA) {
12774 eat(state, TOK_COMMA);
12775 type = attrib(state, type);
12776 }
12777 return type;
12778}
12779
12780static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12781{
12782 if (peek(state) == TOK_ATTRIBUTE) {
12783 eat(state, TOK_ATTRIBUTE);
12784 eat(state, TOK_LPAREN);
12785 eat(state, TOK_LPAREN);
12786 type = attribute_list(state, type);
12787 eat(state, TOK_RPAREN);
12788 eat(state, TOK_RPAREN);
12789 }
12790 return type;
12791}
12792
Eric Biedermanb138ac82003-04-22 18:44:01 +000012793static unsigned int type_qualifiers(struct compile_state *state)
12794{
12795 unsigned int specifiers;
12796 int done;
12797 done = 0;
12798 specifiers = QUAL_NONE;
12799 do {
12800 switch(peek(state)) {
12801 case TOK_CONST:
12802 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012803 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012804 break;
12805 case TOK_VOLATILE:
12806 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012807 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012808 break;
12809 case TOK_RESTRICT:
12810 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012811 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012812 break;
12813 default:
12814 done = 1;
12815 break;
12816 }
12817 } while(!done);
12818 return specifiers;
12819}
12820
12821static struct type *type_specifier(
12822 struct compile_state *state, unsigned int spec)
12823{
12824 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012825 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012826 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012827 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012828 case TOK_VOID:
12829 eat(state, TOK_VOID);
12830 type = new_type(TYPE_VOID | spec, 0, 0);
12831 break;
12832 case TOK_CHAR:
12833 eat(state, TOK_CHAR);
12834 type = new_type(TYPE_CHAR | spec, 0, 0);
12835 break;
12836 case TOK_SHORT:
12837 eat(state, TOK_SHORT);
12838 if (peek(state) == TOK_INT) {
12839 eat(state, TOK_INT);
12840 }
12841 type = new_type(TYPE_SHORT | spec, 0, 0);
12842 break;
12843 case TOK_INT:
12844 eat(state, TOK_INT);
12845 type = new_type(TYPE_INT | spec, 0, 0);
12846 break;
12847 case TOK_LONG:
12848 eat(state, TOK_LONG);
12849 switch(peek(state)) {
12850 case TOK_LONG:
12851 eat(state, TOK_LONG);
12852 error(state, 0, "long long not supported");
12853 break;
12854 case TOK_DOUBLE:
12855 eat(state, TOK_DOUBLE);
12856 error(state, 0, "long double not supported");
12857 break;
12858 case TOK_INT:
12859 eat(state, TOK_INT);
12860 type = new_type(TYPE_LONG | spec, 0, 0);
12861 break;
12862 default:
12863 type = new_type(TYPE_LONG | spec, 0, 0);
12864 break;
12865 }
12866 break;
12867 case TOK_FLOAT:
12868 eat(state, TOK_FLOAT);
12869 error(state, 0, "type float not supported");
12870 break;
12871 case TOK_DOUBLE:
12872 eat(state, TOK_DOUBLE);
12873 error(state, 0, "type double not supported");
12874 break;
12875 case TOK_SIGNED:
12876 eat(state, TOK_SIGNED);
12877 switch(peek(state)) {
12878 case TOK_LONG:
12879 eat(state, TOK_LONG);
12880 switch(peek(state)) {
12881 case TOK_LONG:
12882 eat(state, TOK_LONG);
12883 error(state, 0, "type long long not supported");
12884 break;
12885 case TOK_INT:
12886 eat(state, TOK_INT);
12887 type = new_type(TYPE_LONG | spec, 0, 0);
12888 break;
12889 default:
12890 type = new_type(TYPE_LONG | spec, 0, 0);
12891 break;
12892 }
12893 break;
12894 case TOK_INT:
12895 eat(state, TOK_INT);
12896 type = new_type(TYPE_INT | spec, 0, 0);
12897 break;
12898 case TOK_SHORT:
12899 eat(state, TOK_SHORT);
12900 type = new_type(TYPE_SHORT | spec, 0, 0);
12901 break;
12902 case TOK_CHAR:
12903 eat(state, TOK_CHAR);
12904 type = new_type(TYPE_CHAR | spec, 0, 0);
12905 break;
12906 default:
12907 type = new_type(TYPE_INT | spec, 0, 0);
12908 break;
12909 }
12910 break;
12911 case TOK_UNSIGNED:
12912 eat(state, TOK_UNSIGNED);
12913 switch(peek(state)) {
12914 case TOK_LONG:
12915 eat(state, TOK_LONG);
12916 switch(peek(state)) {
12917 case TOK_LONG:
12918 eat(state, TOK_LONG);
12919 error(state, 0, "unsigned long long not supported");
12920 break;
12921 case TOK_INT:
12922 eat(state, TOK_INT);
12923 type = new_type(TYPE_ULONG | spec, 0, 0);
12924 break;
12925 default:
12926 type = new_type(TYPE_ULONG | spec, 0, 0);
12927 break;
12928 }
12929 break;
12930 case TOK_INT:
12931 eat(state, TOK_INT);
12932 type = new_type(TYPE_UINT | spec, 0, 0);
12933 break;
12934 case TOK_SHORT:
12935 eat(state, TOK_SHORT);
12936 type = new_type(TYPE_USHORT | spec, 0, 0);
12937 break;
12938 case TOK_CHAR:
12939 eat(state, TOK_CHAR);
12940 type = new_type(TYPE_UCHAR | spec, 0, 0);
12941 break;
12942 default:
12943 type = new_type(TYPE_UINT | spec, 0, 0);
12944 break;
12945 }
12946 break;
12947 /* struct or union specifier */
12948 case TOK_STRUCT:
12949 case TOK_UNION:
12950 type = struct_or_union_specifier(state, spec);
12951 break;
12952 /* enum-spefifier */
12953 case TOK_ENUM:
12954 type = enum_specifier(state, spec);
12955 break;
12956 /* typedef name */
12957 case TOK_TYPE_NAME:
12958 type = typedef_name(state, spec);
12959 break;
12960 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012961 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012962 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012963 break;
12964 }
12965 return type;
12966}
12967
12968static int istype(int tok)
12969{
12970 switch(tok) {
12971 case TOK_CONST:
12972 case TOK_RESTRICT:
12973 case TOK_VOLATILE:
12974 case TOK_VOID:
12975 case TOK_CHAR:
12976 case TOK_SHORT:
12977 case TOK_INT:
12978 case TOK_LONG:
12979 case TOK_FLOAT:
12980 case TOK_DOUBLE:
12981 case TOK_SIGNED:
12982 case TOK_UNSIGNED:
12983 case TOK_STRUCT:
12984 case TOK_UNION:
12985 case TOK_ENUM:
12986 case TOK_TYPE_NAME:
12987 return 1;
12988 default:
12989 return 0;
12990 }
12991}
12992
12993
12994static struct type *specifier_qualifier_list(struct compile_state *state)
12995{
12996 struct type *type;
12997 unsigned int specifiers = 0;
12998
12999 /* type qualifiers */
13000 specifiers |= type_qualifiers(state);
13001
13002 /* type specifier */
13003 type = type_specifier(state, specifiers);
13004
13005 return type;
13006}
13007
Stefan Reinauer50542a82007-10-24 11:14:14 +000013008#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013009static int isdecl_specifier(int tok)
13010{
13011 switch(tok) {
13012 /* storage class specifier */
13013 case TOK_AUTO:
13014 case TOK_REGISTER:
13015 case TOK_STATIC:
13016 case TOK_EXTERN:
13017 case TOK_TYPEDEF:
13018 /* type qualifier */
13019 case TOK_CONST:
13020 case TOK_RESTRICT:
13021 case TOK_VOLATILE:
13022 /* type specifiers */
13023 case TOK_VOID:
13024 case TOK_CHAR:
13025 case TOK_SHORT:
13026 case TOK_INT:
13027 case TOK_LONG:
13028 case TOK_FLOAT:
13029 case TOK_DOUBLE:
13030 case TOK_SIGNED:
13031 case TOK_UNSIGNED:
13032 /* struct or union specifier */
13033 case TOK_STRUCT:
13034 case TOK_UNION:
13035 /* enum-spefifier */
13036 case TOK_ENUM:
13037 /* typedef name */
13038 case TOK_TYPE_NAME:
13039 /* function specifiers */
13040 case TOK_INLINE:
13041 return 1;
13042 default:
13043 return 0;
13044 }
13045}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013046#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013047
13048static struct type *decl_specifiers(struct compile_state *state)
13049{
13050 struct type *type;
13051 unsigned int specifiers;
13052 /* I am overly restrictive in the arragement of specifiers supported.
13053 * C is overly flexible in this department it makes interpreting
13054 * the parse tree difficult.
13055 */
13056 specifiers = 0;
13057
13058 /* storage class specifier */
13059 specifiers |= storage_class_specifier_opt(state);
13060
13061 /* function-specifier */
13062 specifiers |= function_specifier_opt(state);
13063
Eric Biederman90089602004-05-28 14:11:54 +000013064 /* attributes */
13065 specifiers |= attributes_opt(state, 0);
13066
Eric Biedermanb138ac82003-04-22 18:44:01 +000013067 /* type qualifier */
13068 specifiers |= type_qualifiers(state);
13069
13070 /* type specifier */
13071 type = type_specifier(state, specifiers);
13072 return type;
13073}
13074
Eric Biederman00443072003-06-24 12:34:45 +000013075struct field_info {
13076 struct type *type;
13077 size_t offset;
13078};
13079
13080static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013081{
13082 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013083 struct field_info info;
13084 info.offset = ~0U;
13085 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013086 do {
13087 switch(peek(state)) {
13088 case TOK_LBRACKET:
13089 {
13090 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013091 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13092 error(state, 0, "Array designator not in array initializer");
13093 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013094 eat(state, TOK_LBRACKET);
13095 value = constant_expr(state);
13096 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013097
13098 info.type = type->left;
13099 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013100 break;
13101 }
13102 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013103 {
13104 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013105 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13106 ((type->type & TYPE_MASK) != TYPE_UNION))
13107 {
Eric Biederman00443072003-06-24 12:34:45 +000013108 error(state, 0, "Struct designator not in struct initializer");
13109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013110 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013111 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013112 info.offset = field_offset(state, type, field);
13113 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013114 break;
Eric Biederman00443072003-06-24 12:34:45 +000013115 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013116 default:
13117 error(state, 0, "Invalid designator");
13118 }
13119 tok = peek(state);
13120 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13121 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013122 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013123}
13124
13125static struct triple *initializer(
13126 struct compile_state *state, struct type *type)
13127{
13128 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013129#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013130#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013131#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013132 if (peek(state) != TOK_LBRACE) {
13133 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013134 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13135 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13136 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13137 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13138 (equiv_types(type->left, result->type->left))) {
13139 type->elements = result->type->elements;
13140 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013141 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013142 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13143 (type->type & TYPE_MASK) != TYPE_ARRAY)
13144 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013145 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013146 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013147 if (!is_init_compatible(state, type, result->type)) {
13148 error(state, 0, "Incompatible types in initializer");
13149 }
13150 if (!equiv_types(type, result->type)) {
13151 result = mk_cast_expr(state, type, result);
13152 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013153 }
13154 else {
13155 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013156 size_t max_offset;
13157 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013158 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013159 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13160 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13161 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013162 }
Eric Biederman00443072003-06-24 12:34:45 +000013163 info.offset = 0;
13164 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013165 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13166 info.type = next_field(state, type, 0);
13167 }
Eric Biederman00443072003-06-24 12:34:45 +000013168 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13169 max_offset = 0;
13170 } else {
13171 max_offset = size_of(state, type);
13172 }
Eric Biederman90089602004-05-28 14:11:54 +000013173 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013174 eat(state, TOK_LBRACE);
13175 do {
13176 struct triple *value;
13177 struct type *value_type;
13178 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013179 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013180 int tok;
13181 comma = 0;
13182 tok = peek(state);
13183 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013184 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013185 }
Eric Biederman00443072003-06-24 12:34:45 +000013186 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13187 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013188 error(state, 0, "element beyond bounds");
13189 }
Eric Biederman00443072003-06-24 12:34:45 +000013190 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013191 value = eval_const_expr(state, initializer(state, value_type));
13192 value_size = size_of(state, value_type);
13193 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013194 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13195 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013196 void *old_buf;
13197 size_t old_size;
13198 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013199 old_size = max_offset;
13200 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013201 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13202 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013203 xfree(old_buf);
13204 }
Eric Biederman90089602004-05-28 14:11:54 +000013205 dest = ((char *)buf) + bits_to_bytes(info.offset);
13206#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013207 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013208 dest - buf,
13209 bits_to_bytes(max_offset),
13210 bits_to_bytes(value_size),
13211 value->op);
13212#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013213 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013214 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013215 }
Eric Biederman90089602004-05-28 14:11:54 +000013216 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13217#if DEBUG_INITIALIZER
13218 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13219#endif
Eric Biederman00443072003-06-24 12:34:45 +000013220 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013221 }
Eric Biederman90089602004-05-28 14:11:54 +000013222 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013223 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013224 }
Eric Biederman90089602004-05-28 14:11:54 +000013225 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013226 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013227 }
13228 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013229 internal_error(state, 0, "unhandled constant initializer");
13230 }
Eric Biederman00443072003-06-24 12:34:45 +000013231 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013232 if (peek(state) == TOK_COMMA) {
13233 eat(state, TOK_COMMA);
13234 comma = 1;
13235 }
Eric Biederman00443072003-06-24 12:34:45 +000013236 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013237 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13238 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013239 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013240 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013241 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013242 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013243 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13244 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13245 type->elements = max_offset / size_of(state, type->left);
13246 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013247 eat(state, TOK_RBRACE);
13248 result = triple(state, OP_BLOBCONST, type, 0, 0);
13249 result->u.blob = buf;
13250 }
13251 return result;
13252}
13253
Eric Biederman90089602004-05-28 14:11:54 +000013254static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013255{
13256 /* Make a second pass and finish anything outstanding
13257 * with respect to branches. The only outstanding item
13258 * is to see if there are goto to labels that have not
13259 * been defined and to error about them.
13260 */
13261 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013262 struct triple *ins;
13263 /* Also error on branches that do not use their targets */
13264 ins = first;
13265 do {
13266 if (!triple_is_ret(state, ins)) {
13267 struct triple **expr ;
13268 struct triple_set *set;
13269 expr = triple_targ(state, ins, 0);
13270 for(; expr; expr = triple_targ(state, ins, expr)) {
13271 struct triple *targ;
13272 targ = *expr;
13273 for(set = targ?targ->use:0; set; set = set->next) {
13274 if (set->member == ins) {
13275 break;
13276 }
13277 }
13278 if (!set) {
13279 internal_error(state, ins, "targ not used");
13280 }
13281 }
13282 }
13283 ins = ins->next;
13284 } while(ins != first);
13285 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013286 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13287 struct hash_entry *entry;
13288 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13289 struct triple *ins;
13290 if (!entry->sym_label) {
13291 continue;
13292 }
13293 ins = entry->sym_label->def;
13294 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13295 error(state, ins, "label `%s' used but not defined",
13296 entry->name);
13297 }
13298 }
13299 }
13300}
13301
Eric Biedermanb138ac82003-04-22 18:44:01 +000013302static struct triple *function_definition(
13303 struct compile_state *state, struct type *type)
13304{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013305 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013306 struct triple *fname;
13307 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013308 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013309 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013310 int i;
13311 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13312 error(state, 0, "Invalid function header");
13313 }
13314
13315 /* Verify the function type */
13316 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13317 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013318 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013319 error(state, 0, "Invalid function parameters");
13320 }
13321 param = type->right;
13322 i = 0;
13323 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13324 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013325 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013326 error(state, 0, "No identifier for parameter %d\n", i);
13327 }
13328 param = param->right;
13329 }
13330 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013331 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013332 error(state, 0, "No identifier for paramter %d\n", i);
13333 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013334
Eric Biedermanb138ac82003-04-22 18:44:01 +000013335 /* Get a list of statements for this function. */
13336 def = triple(state, OP_LIST, type, 0, 0);
13337
13338 /* Start a new scope for the passed parameters */
13339 start_scope(state);
13340
13341 /* Put a label at the very start of a function */
13342 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013343 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013344
13345 /* Put a label at the very end of a function */
13346 end = label(state);
13347 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013348 /* Remember where return goes */
13349 ident = state->i_return;
13350 symbol(state, ident, &ident->sym_ident, end, end->type);
13351
Eric Biederman90089602004-05-28 14:11:54 +000013352 /* Get the initial closure type */
13353 ctype = new_type(TYPE_JOIN, &void_type, 0);
13354 ctype->elements = 1;
13355
13356 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013357 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013358 /* Remove all type qualifiers from the return type */
13359 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13360 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013361 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013362
Eric Biederman5ade04a2003-10-22 04:03:46 +000013363 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013364 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013365
13366 /* Add in the return instruction */
13367 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Patrick Georgi37935572017-01-02 21:39:43 +010013368 flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013369
13370 /* Walk through the parameters and create symbol table entries
13371 * for them.
13372 */
13373 param = type->right;
13374 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013375 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013376 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013377 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013378 flatten(state, end, tmp);
13379 param = param->right;
13380 }
13381 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13382 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013383 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013384 tmp = variable(state, param);
13385 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13386 flatten(state, end, tmp);
13387 }
Eric Biederman90089602004-05-28 14:11:54 +000013388
13389 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013390 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013391 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13392 fname_type->type |= QUAL_CONST | STOR_STATIC;
13393 fname_type->elements = strlen(state->function) + 1;
13394
13395 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13396 fname->u.blob = (void *)state->function;
13397 fname = flatten(state, end, fname);
13398
13399 ident = state->i___func__;
13400 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013401
13402 /* Remember which function I am compiling.
13403 * Also assume the last defined function is the main function.
13404 */
13405 state->main_function = def;
13406
13407 /* Now get the actual function definition */
13408 compound_statement(state, end);
13409
Eric Biederman153ea352003-06-20 14:43:20 +000013410 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013411 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013412
Eric Biedermanb138ac82003-04-22 18:44:01 +000013413 /* Remove the parameter scope */
13414 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013415
Eric Biederman5ade04a2003-10-22 04:03:46 +000013416
13417 /* Remember I have defined a function */
13418 if (!state->functions) {
13419 state->functions = def;
13420 } else {
13421 insert_triple(state, state->functions, def);
13422 }
13423 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013424 FILE *fp = state->dbgout;
13425 fprintf(fp, "\n");
13426 loc(fp, state, 0);
13427 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13428 display_func(state, fp, def);
13429 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013430 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013431
13432 return def;
13433}
13434
Stefan Reinauer14e22772010-04-27 06:56:47 +000013435static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013436 struct type *type, struct hash_entry *ident)
13437{
13438 struct triple *def;
13439 def = 0;
13440 /* Clean up the storage types used */
13441 switch (type->type & STOR_MASK) {
13442 case STOR_AUTO:
13443 case STOR_STATIC:
13444 /* These are the good types I am aiming for */
13445 break;
13446 case STOR_REGISTER:
13447 type->type &= ~STOR_MASK;
13448 type->type |= STOR_AUTO;
13449 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013450 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013451 case STOR_EXTERN:
13452 type->type &= ~STOR_MASK;
13453 type->type |= STOR_STATIC;
13454 break;
13455 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013456 if (!ident) {
13457 error(state, 0, "typedef without name");
13458 }
13459 symbol(state, ident, &ident->sym_ident, 0, type);
13460 ident->tok = TOK_TYPE_NAME;
13461 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013462 break;
13463 default:
13464 internal_error(state, 0, "Undefined storage class");
13465 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013466 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
Patrick Georgi11a7db32013-02-16 20:16:34 +010013467 // ignore function prototypes
13468 return def;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013469 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013470 if (ident &&
13471 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13472 ((type->type & STOR_MASK) != STOR_STATIC))
13473 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013474 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013475 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013476 ((type->type & QUAL_CONST) == 0)) {
13477 error(state, 0, "non const static variables not supported");
13478 }
13479 if (ident) {
13480 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013481 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013482 }
13483 return def;
13484}
13485
13486static void decl(struct compile_state *state, struct triple *first)
13487{
13488 struct type *base_type, *type;
13489 struct hash_entry *ident;
13490 struct triple *def;
13491 int global;
13492 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13493 base_type = decl_specifiers(state);
13494 ident = 0;
13495 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013496 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013497 if (global && ident && (peek(state) == TOK_LBRACE)) {
13498 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013499 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013500 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013501 def = function_definition(state, type);
13502 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013503 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013504 }
13505 else {
13506 int done;
13507 flatten(state, first, do_decl(state, type, ident));
13508 /* type or variable definition */
13509 do {
13510 done = 1;
13511 if (peek(state) == TOK_EQ) {
13512 if (!ident) {
13513 error(state, 0, "cannot assign to a type");
13514 }
13515 eat(state, TOK_EQ);
13516 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013517 init_expr(state,
13518 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013519 initializer(state, type)));
13520 }
13521 arrays_complete(state, type);
13522 if (peek(state) == TOK_COMMA) {
13523 eat(state, TOK_COMMA);
13524 ident = 0;
13525 type = declarator(state, base_type, &ident, 0);
13526 flatten(state, first, do_decl(state, type, ident));
13527 done = 0;
13528 }
13529 } while(!done);
13530 eat(state, TOK_SEMI);
13531 }
13532}
13533
13534static void decls(struct compile_state *state)
13535{
13536 struct triple *list;
13537 int tok;
13538 list = label(state);
13539 while(1) {
13540 tok = peek(state);
13541 if (tok == TOK_EOF) {
13542 return;
13543 }
13544 if (tok == TOK_SPACE) {
13545 eat(state, TOK_SPACE);
13546 }
13547 decl(state, list);
13548 if (list->next != list) {
13549 error(state, 0, "global variables not supported");
13550 }
13551 }
13552}
13553
Stefan Reinauer14e22772010-04-27 06:56:47 +000013554/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013555 * Function inlining
13556 */
Eric Biederman90089602004-05-28 14:11:54 +000013557struct triple_reg_set {
13558 struct triple_reg_set *next;
13559 struct triple *member;
13560 struct triple *new;
13561};
13562struct reg_block {
13563 struct block *block;
13564 struct triple_reg_set *in;
13565 struct triple_reg_set *out;
13566 int vertex;
13567};
13568static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13569static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13570static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13571static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13572static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13573 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13574 void *arg);
13575static void print_block(
13576 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013577static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013578 struct triple *member, struct triple *new_member);
13579static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13580static struct reg_block *compute_variable_lifetimes(
13581 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013582static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013583 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013584#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013585static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013586 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013587#endif
Eric Biederman90089602004-05-28 14:11:54 +000013588
Eric Biederman5ade04a2003-10-22 04:03:46 +000013589
13590static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013591 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013592 struct triple *targ, struct triple *ret)
13593{
13594 struct triple *call;
13595
13596 if (!retvar || !is_lvalue(state, retvar)) {
13597 internal_error(state, 0, "writing to a non lvalue?");
13598 }
13599 write_compatible(state, retvar->type, &void_ptr_type);
13600
13601 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13602 TARG(call, 0) = targ;
13603 MISC(call, 0) = ret;
13604 if (!targ || (targ->op != OP_LABEL)) {
13605 internal_error(state, 0, "call not to a label");
13606 }
13607 if (!ret || (ret->op != OP_RET)) {
13608 internal_error(state, 0, "call not matched with return");
13609 }
13610 return call;
13611}
13612
Eric Biederman5ade04a2003-10-22 04:03:46 +000013613static void walk_functions(struct compile_state *state,
13614 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13615 void *arg)
13616{
13617 struct triple *func, *first;
13618 func = first = state->functions;
13619 do {
13620 cb(state, func, arg);
13621 func = func->next;
13622 } while(func != first);
13623}
13624
Eric Biederman90089602004-05-28 14:11:54 +000013625static void reverse_walk_functions(struct compile_state *state,
13626 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13627 void *arg)
13628{
13629 struct triple *func, *first;
13630 func = first = state->functions;
13631 do {
13632 func = func->prev;
13633 cb(state, func, arg);
13634 } while(func != first);
13635}
13636
13637
13638static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13639{
13640 struct triple *ptr, *first;
13641 if (func->u.cval == 0) {
13642 return;
13643 }
13644 ptr = first = RHS(func, 0);
13645 do {
13646 if (ptr->op == OP_FCALL) {
13647 struct triple *called_func;
13648 called_func = MISC(ptr, 0);
13649 /* Mark the called function as used */
13650 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13651 called_func->u.cval++;
13652 }
13653 /* Remove the called function from the list */
13654 called_func->prev->next = called_func->next;
13655 called_func->next->prev = called_func->prev;
13656
13657 /* Place the called function before me on the list */
13658 called_func->next = func;
13659 called_func->prev = func->prev;
13660 called_func->prev->next = called_func;
13661 called_func->next->prev = called_func;
13662 }
13663 ptr = ptr->next;
13664 } while(ptr != first);
13665 func->id |= TRIPLE_FLAG_FLATTENED;
13666}
13667
13668static void mark_live_functions(struct compile_state *state)
13669{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013670 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013671 * the list of functions.
13672 */
13673 if ((state->main_function->next != state->functions) ||
13674 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013675 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013676 "state->main_function is not at the end of the function list ");
13677 }
13678 state->main_function->u.cval = 1;
13679 reverse_walk_functions(state, mark_live, 0);
13680}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013681
Stefan Reinauer14e22772010-04-27 06:56:47 +000013682static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013683 struct triple *func, struct triple *ins)
13684{
13685 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13686#if 0
13687 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013688 FILE *fp = state->errout;
13689 fprintf(fp, "global: ");
13690 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013691 }
13692#endif
13693 return local;
13694}
13695
Stefan Reinauer14e22772010-04-27 06:56:47 +000013696struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013697 struct occurance *base_occurance)
13698{
13699 struct triple *nfunc;
13700 struct triple *nfirst, *ofirst;
13701 struct triple *new, *old;
13702
13703 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013704 FILE *fp = state->dbgout;
13705 fprintf(fp, "\n");
13706 loc(fp, state, 0);
13707 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13708 display_func(state, fp, ofunc);
13709 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013710 }
13711
13712 /* Make a new copy of the old function */
13713 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13714 nfirst = 0;
13715 ofirst = old = RHS(ofunc, 0);
13716 do {
13717 struct triple *new;
13718 struct occurance *occurance;
13719 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013720 old_lhs = old->lhs;
13721 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013722 occurance = inline_occurance(state, base_occurance, old->occurance);
13723 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13724 MISC(old, 0)->u.cval += 1;
13725 }
13726 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13727 occurance);
13728 if (!triple_stores_block(state, new)) {
13729 memcpy(&new->u, &old->u, sizeof(new->u));
13730 }
13731 if (!nfirst) {
13732 RHS(nfunc, 0) = nfirst = new;
13733 }
13734 else {
13735 insert_triple(state, nfirst, new);
13736 }
13737 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013738 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013739
Eric Biederman5ade04a2003-10-22 04:03:46 +000013740 /* During the copy remember new as user of old */
13741 use_triple(old, new);
13742
Eric Biederman5ade04a2003-10-22 04:03:46 +000013743 /* Remember which instructions are local */
13744 old->id |= TRIPLE_FLAG_LOCAL;
13745 old = old->next;
13746 } while(old != ofirst);
13747
13748 /* Make a second pass to fix up any unresolved references */
13749 old = ofirst;
13750 new = nfirst;
13751 do {
13752 struct triple **oexpr, **nexpr;
13753 int count, i;
13754 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013755 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013756 for(i = 0; i < count; i++) {
13757 oexpr = &old->param[i];
13758 nexpr = &new->param[i];
13759 if (*oexpr && !*nexpr) {
13760 if (!local_triple(state, ofunc, *oexpr)) {
13761 *nexpr = *oexpr;
13762 }
13763 else if ((*oexpr)->use) {
13764 *nexpr = (*oexpr)->use->member;
13765 }
13766 if (*nexpr == old) {
13767 internal_error(state, 0, "new == old?");
13768 }
13769 use_triple(*nexpr, new);
13770 }
13771 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013772 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013773 }
13774 }
13775 old = old->next;
13776 new = new->next;
13777 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013778
Eric Biederman5ade04a2003-10-22 04:03:46 +000013779 /* Make a third pass to cleanup the extra useses */
13780 old = ofirst;
13781 new = nfirst;
13782 do {
13783 unuse_triple(old, new);
13784 /* Forget which instructions are local */
13785 old->id &= ~TRIPLE_FLAG_LOCAL;
13786 old = old->next;
13787 new = new->next;
13788 } while ((old != ofirst) && (new != nfirst));
13789 return nfunc;
13790}
13791
Eric Biederman90089602004-05-28 14:11:54 +000013792static void expand_inline_call(
13793 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013794{
13795 /* Inline the function call */
13796 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013797 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013798 struct triple *end, *nend;
13799 int pvals, i;
13800
13801 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013802 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013803 if (ofunc->op != OP_LIST) {
13804 internal_error(state, 0, "improper function");
13805 }
Eric Biederman90089602004-05-28 14:11:54 +000013806 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013807 /* Prepend the parameter reading into the new function list */
13808 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013809 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013810 for(i = 0; i < pvals; i++) {
13811 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013812 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013813 atype = ptype;
13814 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13815 atype = ptype->left;
13816 }
Eric Biederman90089602004-05-28 14:11:54 +000013817 param = farg(state, nfunc, i);
13818 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13819 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013820 }
Eric Biederman90089602004-05-28 14:11:54 +000013821 arg = RHS(fcall, i);
13822 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013823 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013824 }
13825 result = 0;
13826 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013827 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013828 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013829 }
13830 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013831 FILE *fp = state->dbgout;
13832 fprintf(fp, "\n");
13833 loc(fp, state, 0);
13834 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13835 display_func(state, fp, nfunc);
13836 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013837 }
13838
Stefan Reinauer14e22772010-04-27 06:56:47 +000013839 /*
13840 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013841 */
13842 /* Remove the read of the return address */
13843 ins = RHS(nfunc, 0)->prev->prev;
13844 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13845 internal_error(state, ins, "Not return addres read?");
13846 }
13847 release_triple(state, ins);
13848 /* Remove the return instruction */
13849 ins = RHS(nfunc, 0)->prev;
13850 if (ins->op != OP_RET) {
13851 internal_error(state, ins, "Not return?");
13852 }
13853 release_triple(state, ins);
13854 /* Remove the retaddres variable */
13855 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013856 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013857 (retvar->op != OP_ADECL) ||
13858 (retvar->next->op != OP_PIECE) ||
13859 (MISC(retvar->next, 0) != retvar)) {
13860 internal_error(state, retvar, "Not the return address?");
13861 }
13862 release_triple(state, retvar->next);
13863 release_triple(state, retvar);
13864
13865 /* Remove the label at the start of the function */
13866 ins = RHS(nfunc, 0);
13867 if (ins->op != OP_LABEL) {
13868 internal_error(state, ins, "Not label?");
13869 }
13870 nfirst = ins->next;
13871 free_triple(state, ins);
13872 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013873 RHS(nfunc, 0) = 0;
13874 free_triple(state, nfunc);
13875
13876 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013877 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013878 nend = nfirst->prev;
13879 end->next = nfirst;
13880 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013881 nend->next = fcall;
13882 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013883
Eric Biederman90089602004-05-28 14:11:54 +000013884 /* Now the result reading code */
13885 if (result) {
13886 result = flatten(state, fcall, result);
13887 propogate_use(state, fcall, result);
13888 }
13889
13890 /* Release the original fcall instruction */
13891 release_triple(state, fcall);
13892
13893 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013894}
13895
Eric Biederman90089602004-05-28 14:11:54 +000013896/*
13897 *
13898 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013899 *
Eric Biederman90089602004-05-28 14:11:54 +000013900 * result
13901 * |
13902 * +----------+------------+
13903 * | |
13904 * union of closures result_type
13905 * |
13906 * +------------------+---------------+
13907 * | |
13908 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013909 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013910 * +----+--+-+--------+-----+ +----+----+---+-----+
13911 * | | | | | | | | |
13912 * var1 var2 var3 ... varN result var1 var2 ... varN result
13913 * |
13914 * +--------+---------+
13915 * | |
13916 * union of closures result_type
13917 * |
13918 * +-----+-------------------+
13919 * | |
13920 * closure1 ... closureN
13921 * | |
13922 * +-----+---+----+----+ +----+---+----+-----+
13923 * | | | | | | | |
13924 * var1 var2 ... varN result var1 var2 ... varN result
13925 */
13926
Stefan Reinauer14e22772010-04-27 06:56:47 +000013927static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013928 struct triple *func, struct type *closure_type)
13929{
13930 struct type *type, *ctype, **next;
13931 struct triple *var, *new_var;
13932 int i;
13933
13934#if 0
13935 FILE *fp = state->errout;
13936 fprintf(fp, "original_type: ");
13937 name_of(fp, fresult(state, func)->type);
13938 fprintf(fp, "\n");
13939#endif
13940 /* find the original type */
13941 var = fresult(state, func);
13942 type = var->type;
13943 if (type->elements != 2) {
13944 internal_error(state, var, "bad return type");
13945 }
13946
13947 /* Find the complete closure type and update it */
13948 ctype = type->left->left;
13949 next = &ctype->left;
13950 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13951 next = &(*next)->right;
13952 }
13953 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13954 ctype->elements += 1;
13955
13956#if 0
13957 fprintf(fp, "new_type: ");
13958 name_of(fp, type);
13959 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013960 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013961 ctype, ctype->elements, reg_size_of(state, ctype));
13962 name_of(fp, ctype);
13963 fprintf(fp, "\n");
13964#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013965
Eric Biederman90089602004-05-28 14:11:54 +000013966 /* Regenerate the variable with the new type definition */
13967 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13968 new_var->id |= TRIPLE_FLAG_FLATTENED;
13969 for(i = 0; i < new_var->lhs; i++) {
13970 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13971 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013972
Eric Biederman90089602004-05-28 14:11:54 +000013973 /* Point everyone at the new variable */
13974 propogate_use(state, var, new_var);
13975
13976 /* Release the original variable */
13977 for(i = 0; i < var->lhs; i++) {
13978 release_triple(state, LHS(var, i));
13979 }
13980 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013981
Eric Biederman90089602004-05-28 14:11:54 +000013982 /* Return the index of the added closure type */
13983 return ctype->elements - 1;
13984}
13985
13986static struct triple *closure_expr(struct compile_state *state,
13987 struct triple *func, int closure_idx, int var_idx)
13988{
13989 return deref_index(state,
13990 deref_index(state,
13991 deref_index(state, fresult(state, func), 0),
13992 closure_idx),
13993 var_idx);
13994}
13995
13996
13997static void insert_triple_set(
13998 struct triple_reg_set **head, struct triple *member)
13999{
14000 struct triple_reg_set *new;
14001 new = xcmalloc(sizeof(*new), "triple_set");
14002 new->member = member;
14003 new->new = 0;
14004 new->next = *head;
14005 *head = new;
14006}
14007
14008static int ordered_triple_set(
14009 struct triple_reg_set **head, struct triple *member)
14010{
14011 struct triple_reg_set **ptr;
14012 if (!member)
14013 return 0;
14014 ptr = head;
14015 while(*ptr) {
14016 if (member == (*ptr)->member) {
14017 return 0;
14018 }
14019 /* keep the list ordered */
14020 if (member->id < (*ptr)->member->id) {
14021 break;
14022 }
14023 ptr = &(*ptr)->next;
14024 }
14025 insert_triple_set(ptr, member);
14026 return 1;
14027}
14028
14029
14030static void free_closure_variables(struct compile_state *state,
14031 struct triple_reg_set **enclose)
14032{
14033 struct triple_reg_set *entry, *next;
14034 for(entry = *enclose; entry; entry = next) {
14035 next = entry->next;
14036 do_triple_unset(enclose, entry->member);
14037 }
14038}
14039
14040static int lookup_closure_index(struct compile_state *state,
14041 struct triple *me, struct triple *val)
14042{
14043 struct triple *first, *ins, *next;
14044 first = RHS(me, 0);
Patrick Georgi37935572017-01-02 21:39:43 +010014045 next = first;
Eric Biederman90089602004-05-28 14:11:54 +000014046 do {
14047 struct triple *result;
14048 struct triple *index0, *index1, *index2, *read, *write;
14049 ins = next;
14050 next = ins->next;
14051 if (ins->op != OP_CALL) {
14052 continue;
14053 }
14054 /* I am at a previous call point examine it closely */
14055 if (ins->next->op != OP_LABEL) {
14056 internal_error(state, ins, "call not followed by label");
14057 }
14058 /* Does this call does not enclose any variables? */
14059 if ((ins->next->next->op != OP_INDEX) ||
14060 (ins->next->next->u.cval != 0) ||
14061 (result = MISC(ins->next->next, 0)) ||
14062 (result->id & TRIPLE_FLAG_LOCAL)) {
14063 continue;
14064 }
Eric Biederman90089602004-05-28 14:11:54 +000014065 /* The pattern is:
14066 * 0 index result < 0 >
14067 * 1 index 0 < ? >
14068 * 2 index 1 < ? >
14069 * 3 read 2
14070 * 4 write 3 var
14071 */
14072 for(index0 = ins->next->next;
14073 (index0->op == OP_INDEX) &&
14074 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014075 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014076 index0 = write->next)
14077 {
14078 index1 = index0->next;
14079 index2 = index1->next;
14080 read = index2->next;
14081 write = read->next;
14082 if ((index0->op != OP_INDEX) ||
14083 (index1->op != OP_INDEX) ||
14084 (index2->op != OP_INDEX) ||
14085 (read->op != OP_READ) ||
14086 (write->op != OP_WRITE) ||
14087 (MISC(index1, 0) != index0) ||
14088 (MISC(index2, 0) != index1) ||
14089 (RHS(read, 0) != index2) ||
14090 (RHS(write, 0) != read)) {
14091 internal_error(state, index0, "bad var read");
14092 }
14093 if (MISC(write, 0) == val) {
14094 return index2->u.cval;
14095 }
14096 }
14097 } while(next != first);
14098 return -1;
14099}
14100
14101static inline int enclose_triple(struct triple *ins)
14102{
14103 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14104}
14105
14106static void compute_closure_variables(struct compile_state *state,
14107 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14108{
14109 struct triple_reg_set *set, *vars, **last_var;
14110 struct basic_blocks bb;
14111 struct reg_block *rb;
14112 struct block *block;
14113 struct triple *old_result, *first, *ins;
14114 size_t count, idx;
14115 unsigned long used_indicies;
14116 int i, max_index;
14117#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14118#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014119 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014120 unsigned id;
14121 int index;
14122 } *info;
14123
Stefan Reinauer14e22772010-04-27 06:56:47 +000014124
Eric Biederman90089602004-05-28 14:11:54 +000014125 /* Find the basic blocks of this function */
14126 bb.func = me;
14127 bb.first = RHS(me, 0);
14128 old_result = 0;
14129 if (!triple_is_ret(state, bb.first->prev)) {
14130 bb.func = 0;
14131 } else {
14132 old_result = fresult(state, me);
14133 }
14134 analyze_basic_blocks(state, &bb);
14135
14136 /* Find which variables are currently alive in a given block */
14137 rb = compute_variable_lifetimes(state, &bb);
14138
14139 /* Find the variables that are currently alive */
14140 block = block_of_triple(state, fcall);
14141 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14142 internal_error(state, fcall, "No reg block? block: %p", block);
14143 }
14144
14145#if DEBUG_EXPLICIT_CLOSURES
14146 print_live_variables(state, &bb, rb, state->dbgout);
14147 fflush(state->dbgout);
14148#endif
14149
14150 /* Count the number of triples in the function */
14151 first = RHS(me, 0);
14152 ins = first;
14153 count = 0;
14154 do {
14155 count++;
14156 ins = ins->next;
14157 } while(ins != first);
14158
14159 /* Allocate some memory to temorary hold the id info */
14160 info = xcmalloc(sizeof(*info) * (count +1), "info");
14161
14162 /* Mark the local function */
14163 first = RHS(me, 0);
14164 ins = first;
14165 idx = 1;
14166 do {
14167 info[idx].id = ins->id;
14168 ins->id = TRIPLE_FLAG_LOCAL | idx;
14169 idx++;
14170 ins = ins->next;
14171 } while(ins != first);
14172
Stefan Reinauer14e22772010-04-27 06:56:47 +000014173 /*
Eric Biederman90089602004-05-28 14:11:54 +000014174 * Build the list of variables to enclose.
14175 *
14176 * A target it to put the same variable in the
14177 * same slot for ever call of a given function.
14178 * After coloring this removes all of the variable
14179 * manipulation code.
14180 *
14181 * The list of variables to enclose is built ordered
14182 * program order because except in corner cases this
14183 * gives me the stability of assignment I need.
14184 *
14185 * To gurantee that stability I lookup the variables
14186 * to see where they have been used before and
14187 * I build my final list with the assigned indicies.
14188 */
14189 vars = 0;
14190 if (enclose_triple(old_result)) {
14191 ordered_triple_set(&vars, old_result);
14192 }
14193 for(set = rb[block->vertex].out; set; set = set->next) {
14194 if (!enclose_triple(set->member)) {
14195 continue;
14196 }
14197 if ((set->member == fcall) || (set->member == old_result)) {
14198 continue;
14199 }
14200 if (!local_triple(state, me, set->member)) {
14201 internal_error(state, set->member, "not local?");
14202 }
14203 ordered_triple_set(&vars, set->member);
14204 }
14205
14206 /* Lookup the current indicies of the live varialbe */
14207 used_indicies = 0;
14208 max_index = -1;
14209 for(set = vars; set ; set = set->next) {
14210 struct triple *ins;
14211 int index;
14212 ins = set->member;
14213 index = lookup_closure_index(state, me, ins);
14214 info[ID_BITS(ins->id)].index = index;
14215 if (index < 0) {
14216 continue;
14217 }
14218 if (index >= MAX_INDICIES) {
14219 internal_error(state, ins, "index unexpectedly large");
14220 }
14221 if (used_indicies & (1 << index)) {
14222 internal_error(state, ins, "index previously used?");
14223 }
14224 /* Remember which indicies have been used */
14225 used_indicies |= (1 << index);
14226 if (index > max_index) {
14227 max_index = index;
14228 }
14229 }
14230
14231 /* Walk through the live variables and make certain
14232 * everything is assigned an index.
14233 */
14234 for(set = vars; set; set = set->next) {
14235 struct triple *ins;
14236 int index;
14237 ins = set->member;
14238 index = info[ID_BITS(ins->id)].index;
14239 if (index >= 0) {
14240 continue;
14241 }
14242 /* Find the lowest unused index value */
14243 for(index = 0; index < MAX_INDICIES; index++) {
Patrick Georgie7724f12017-01-02 21:24:50 +010014244 if (!(used_indicies & ((uint64_t)1 << index))) {
Eric Biederman90089602004-05-28 14:11:54 +000014245 break;
14246 }
14247 }
14248 if (index == MAX_INDICIES) {
14249 internal_error(state, ins, "no free indicies?");
14250 }
14251 info[ID_BITS(ins->id)].index = index;
14252 /* Remember which indicies have been used */
14253 used_indicies |= (1 << index);
14254 if (index > max_index) {
14255 max_index = index;
14256 }
14257 }
14258
14259 /* Build the return list of variables with positions matching
14260 * their indicies.
14261 */
14262 *enclose = 0;
14263 last_var = enclose;
14264 for(i = 0; i <= max_index; i++) {
14265 struct triple *var;
14266 var = 0;
14267 if (used_indicies & (1 << i)) {
14268 for(set = vars; set; set = set->next) {
14269 int index;
14270 index = info[ID_BITS(set->member->id)].index;
14271 if (index == i) {
14272 var = set->member;
14273 break;
14274 }
14275 }
14276 if (!var) {
14277 internal_error(state, me, "missing variable");
14278 }
14279 }
14280 insert_triple_set(last_var, var);
14281 last_var = &(*last_var)->next;
14282 }
14283
14284#if DEBUG_EXPLICIT_CLOSURES
14285 /* Print out the variables to be enclosed */
14286 loc(state->dbgout, state, fcall);
14287 fprintf(state->dbgout, "Alive: \n");
14288 for(set = *enclose; set; set = set->next) {
14289 display_triple(state->dbgout, set->member);
14290 }
14291 fflush(state->dbgout);
14292#endif
14293
14294 /* Clear the marks */
14295 ins = first;
14296 do {
14297 ins->id = info[ID_BITS(ins->id)].id;
14298 ins = ins->next;
14299 } while(ins != first);
14300
14301 /* Release the ordered list of live variables */
14302 free_closure_variables(state, &vars);
14303
14304 /* Release the storage of the old ids */
14305 xfree(info);
14306
14307 /* Release the variable lifetime information */
14308 free_variable_lifetimes(state, &bb, rb);
14309
14310 /* Release the basic blocks of this function */
14311 free_basic_blocks(state, &bb);
14312}
14313
14314static void expand_function_call(
14315 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014316{
14317 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014318 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014319 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014320 struct triple *first;
14321 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014322 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014323 struct triple_reg_set *enclose, *set;
14324 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014325
Eric Biederman90089602004-05-28 14:11:54 +000014326#if DEBUG_EXPLICIT_CLOSURES
14327 FILE *fp = state->dbgout;
14328 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14329 display_func(state, fp, MISC(fcall, 0));
14330 display_func(state, fp, me);
14331 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14332#endif
14333
Eric Biederman5ade04a2003-10-22 04:03:46 +000014334 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014335 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014336 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014337 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014338 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014339 first = fcall->next;
14340
14341 /* Find what I need to enclose */
14342 compute_closure_variables(state, me, fcall, &enclose);
14343
14344 /* Compute the closure type */
14345 closure_type = new_type(TYPE_TUPLE, 0, 0);
14346 closure_type->elements = 0;
14347 closure_next = &closure_type->left;
14348 for(set = enclose; set ; set = set->next) {
14349 struct type *type;
14350 type = &void_type;
14351 if (set->member) {
14352 type = set->member->type;
14353 }
14354 if (!*closure_next) {
14355 *closure_next = type;
14356 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014357 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014358 type);
14359 closure_next = &(*closure_next)->right;
14360 }
14361 closure_type->elements += 1;
14362 }
14363 if (closure_type->elements == 0) {
14364 closure_type->type = TYPE_VOID;
14365 }
14366
14367
14368#if DEBUG_EXPLICIT_CLOSURES
14369 fprintf(state->dbgout, "closure type: ");
14370 name_of(state->dbgout, closure_type);
14371 fprintf(state->dbgout, "\n");
14372#endif
14373
14374 /* Update the called functions closure variable */
14375 closure_idx = add_closure_type(state, func, closure_type);
Patrick Georgi61dbbf62017-01-02 18:54:28 +010014376 free(closure_type);
14377 closure_type = NULL;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014378
14379 /* Generate some needed triples */
14380 ret_loc = label(state);
14381 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14382
14383 /* Pass the parameters to the new function */
14384 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014385 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014386 for(i = 0; i < pvals; i++) {
14387 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014388 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014389 atype = ptype;
14390 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14391 atype = ptype->left;
14392 }
Eric Biederman90089602004-05-28 14:11:54 +000014393 param = farg(state, func, i);
14394 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14395 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014396 }
Eric Biederman90089602004-05-28 14:11:54 +000014397 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014398 flatten(state, first, write_expr(state, param, arg));
14399 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014400 }
Eric Biederman90089602004-05-28 14:11:54 +000014401 rtype = func->type->left;
14402
Eric Biederman5ade04a2003-10-22 04:03:46 +000014403 /* Thread the triples together */
14404 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014405
14406 /* Save the active variables in the result variable */
14407 for(i = 0, set = enclose; set ; set = set->next, i++) {
14408 if (!set->member) {
14409 continue;
14410 }
14411 flatten(state, ret_loc,
14412 write_expr(state,
14413 closure_expr(state, func, closure_idx, i),
14414 read_expr(state, set->member)));
14415 }
14416
14417 /* Initialize the return value */
14418 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014419 flatten(state, ret_loc,
14420 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014421 deref_index(state, fresult(state, func), 1),
14422 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14423 }
14424
Eric Biederman5ade04a2003-10-22 04:03:46 +000014425 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014426 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14427 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014428 call(state, retvar, ret_addr, func_first, func_last));
14429
Eric Biederman7dea9552004-06-29 05:38:37 +000014430 /* Find the result */
14431 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14432 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014433 result = flatten(state, first,
14434 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014435 deref_index(state, fresult(state, func), 1)));
14436
14437 propogate_use(state, fcall, result);
14438 }
14439
14440 /* Release the original fcall instruction */
14441 release_triple(state, fcall);
14442
Eric Biederman90089602004-05-28 14:11:54 +000014443 /* Restore the active variables from the result variable */
14444 for(i = 0, set = enclose; set ; set = set->next, i++) {
14445 struct triple_set *use, *next;
14446 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014447 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014448 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014449 continue;
14450 }
Eric Biederman90089602004-05-28 14:11:54 +000014451 /* Generate an expression for the value */
14452 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014453 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014454 closure_expr(state, func, closure_idx, i)));
14455
14456
14457 /* If the original is an lvalue restore the preserved value */
14458 if (is_lvalue(state, set->member)) {
14459 flatten(state, first,
14460 write_expr(state, set->member, new));
14461 continue;
14462 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014463 /*
14464 * If the original is a value update the dominated uses.
14465 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014466
Eric Biederman7dea9552004-06-29 05:38:37 +000014467 /* Analyze the basic blocks so I can see who dominates whom */
14468 bb.func = me;
14469 bb.first = RHS(me, 0);
14470 if (!triple_is_ret(state, bb.first->prev)) {
14471 bb.func = 0;
14472 }
14473 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014474
Eric Biederman90089602004-05-28 14:11:54 +000014475
14476#if DEBUG_EXPLICIT_CLOSURES
14477 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14478 set->member, new);
14479#endif
14480 /* If fcall dominates the use update the expression */
14481 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014482 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014483 * removes use, so I must take a copy of the
14484 * next entry early.
14485 */
14486 next = use->next;
14487 if (!tdominates(state, fcall, use->member)) {
14488 continue;
14489 }
14490 replace_use(state, set->member, new, use->member);
14491 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014492
14493 /* Release the basic blocks, the instructions will be
14494 * different next time, and flatten/insert_triple does
14495 * not update the block values so I can't cache the analysis.
14496 */
14497 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014498 }
14499
Eric Biederman90089602004-05-28 14:11:54 +000014500 /* Release the closure variable list */
14501 free_closure_variables(state, &enclose);
14502
14503 if (state->compiler->debug & DEBUG_INLINE) {
14504 FILE *fp = state->dbgout;
14505 fprintf(fp, "\n");
14506 loc(fp, state, 0);
14507 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14508 display_func(state, fp, func);
14509 display_func(state, fp, me);
14510 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14511 }
14512
14513 return;
14514}
14515
14516static int do_inline(struct compile_state *state, struct triple *func)
14517{
14518 int do_inline;
14519 int policy;
14520
14521 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14522 switch(policy) {
14523 case COMPILER_INLINE_ALWAYS:
14524 do_inline = 1;
14525 if (func->type->type & ATTRIB_NOINLINE) {
14526 error(state, func, "noinline with always_inline compiler option");
14527 }
14528 break;
14529 case COMPILER_INLINE_NEVER:
14530 do_inline = 0;
14531 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14532 error(state, func, "always_inline with noinline compiler option");
14533 }
14534 break;
14535 case COMPILER_INLINE_DEFAULTON:
14536 switch(func->type->type & STOR_MASK) {
14537 case STOR_STATIC | STOR_INLINE:
14538 case STOR_LOCAL | STOR_INLINE:
14539 case STOR_EXTERN | STOR_INLINE:
14540 do_inline = 1;
14541 break;
14542 default:
14543 do_inline = 1;
14544 break;
14545 }
14546 break;
14547 case COMPILER_INLINE_DEFAULTOFF:
14548 switch(func->type->type & STOR_MASK) {
14549 case STOR_STATIC | STOR_INLINE:
14550 case STOR_LOCAL | STOR_INLINE:
14551 case STOR_EXTERN | STOR_INLINE:
14552 do_inline = 1;
14553 break;
14554 default:
14555 do_inline = 0;
14556 break;
14557 }
14558 break;
14559 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014560 switch(func->type->type & STOR_MASK) {
14561 case STOR_STATIC | STOR_INLINE:
14562 case STOR_LOCAL | STOR_INLINE:
14563 case STOR_EXTERN | STOR_INLINE:
14564 do_inline = 1;
14565 break;
14566 default:
14567 do_inline = (func->u.cval == 1);
14568 break;
14569 }
Eric Biederman90089602004-05-28 14:11:54 +000014570 break;
14571 default:
Eric Biederman90089602004-05-28 14:11:54 +000014572 internal_error(state, 0, "Unimplemented inline policy");
14573 break;
14574 }
14575 /* Force inlining */
14576 if (func->type->type & ATTRIB_NOINLINE) {
14577 do_inline = 0;
14578 }
14579 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14580 do_inline = 1;
14581 }
14582 return do_inline;
14583}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014584
Eric Biederman90089602004-05-28 14:11:54 +000014585static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14586{
14587 struct triple *first, *ptr, *next;
14588 /* If the function is not used don't bother */
14589 if (me->u.cval <= 0) {
14590 return;
14591 }
14592 if (state->compiler->debug & DEBUG_CALLS2) {
14593 FILE *fp = state->dbgout;
14594 fprintf(fp, "in: %s\n",
14595 me->type->type_ident->name);
14596 }
14597
14598 first = RHS(me, 0);
Patrick Georgi37935572017-01-02 21:39:43 +010014599 next = first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014600 do {
Eric Biederman90089602004-05-28 14:11:54 +000014601 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014602 ptr = next;
14603 prev = ptr->prev;
14604 next = ptr->next;
14605 if (ptr->op != OP_FCALL) {
14606 continue;
14607 }
14608 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014609 /* See if the function should be inlined */
14610 if (!do_inline(state, func)) {
14611 /* Put a label after the fcall */
14612 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14613 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014614 }
Eric Biederman90089602004-05-28 14:11:54 +000014615 if (state->compiler->debug & DEBUG_CALLS) {
14616 FILE *fp = state->dbgout;
14617 if (state->compiler->debug & DEBUG_CALLS2) {
14618 loc(fp, state, ptr);
14619 }
14620 fprintf(fp, "inlining %s\n",
14621 func->type->type_ident->name);
14622 fflush(fp);
14623 }
14624
14625 /* Update the function use counts */
14626 func->u.cval -= 1;
14627
14628 /* Replace the fcall with the called function */
14629 expand_inline_call(state, me, ptr);
14630
14631 next = prev->next;
14632 } while (next != first);
14633
Patrick Georgi37935572017-01-02 21:39:43 +010014634 next = first;
Eric Biederman90089602004-05-28 14:11:54 +000014635 do {
14636 struct triple *prev, *func;
14637 ptr = next;
14638 prev = ptr->prev;
14639 next = ptr->next;
14640 if (ptr->op != OP_FCALL) {
14641 continue;
14642 }
14643 func = MISC(ptr, 0);
14644 if (state->compiler->debug & DEBUG_CALLS) {
14645 FILE *fp = state->dbgout;
14646 if (state->compiler->debug & DEBUG_CALLS2) {
14647 loc(fp, state, ptr);
14648 }
14649 fprintf(fp, "calling %s\n",
14650 func->type->type_ident->name);
14651 fflush(fp);
14652 }
14653 /* Replace the fcall with the instruction sequence
14654 * needed to make the call.
14655 */
14656 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014657 next = prev->next;
14658 } while(next != first);
14659}
Eric Biederman90089602004-05-28 14:11:54 +000014660
14661static void inline_functions(struct compile_state *state, struct triple *func)
14662{
14663 inline_function(state, func, 0);
14664 reverse_walk_functions(state, inline_function, 0);
14665}
14666
Eric Biederman5ade04a2003-10-22 04:03:46 +000014667static void insert_function(struct compile_state *state,
14668 struct triple *func, void *arg)
14669{
14670 struct triple *first, *end, *ffirst, *fend;
14671
14672 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014673 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014674 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014675 func->type->type_ident->name, func->u.cval);
14676 }
14677 if (func->u.cval == 0) {
14678 return;
14679 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014680
14681 /* Find the end points of the lists */
14682 first = arg;
14683 end = first->prev;
14684 ffirst = RHS(func, 0);
14685 fend = ffirst->prev;
14686
14687 /* splice the lists together */
14688 end->next = ffirst;
14689 ffirst->prev = end;
14690 fend->next = first;
14691 first->prev = fend;
14692}
14693
Eric Biederman90089602004-05-28 14:11:54 +000014694struct triple *input_asm(struct compile_state *state)
14695{
14696 struct asm_info *info;
14697 struct triple *def;
14698 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014699
Eric Biederman90089602004-05-28 14:11:54 +000014700 info = xcmalloc(sizeof(*info), "asm_info");
14701 info->str = "";
14702
14703 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14704 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14705
14706 def = new_triple(state, OP_ASM, &void_type, out, 0);
14707 def->u.ainfo = info;
14708 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014709
Eric Biederman90089602004-05-28 14:11:54 +000014710 for(i = 0; i < out; i++) {
14711 struct triple *piece;
14712 piece = triple(state, OP_PIECE, &int_type, def, 0);
14713 piece->u.cval = i;
14714 LHS(def, i) = piece;
14715 }
14716
14717 return def;
14718}
14719
14720struct triple *output_asm(struct compile_state *state)
14721{
14722 struct asm_info *info;
14723 struct triple *def;
14724 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014725
Eric Biederman90089602004-05-28 14:11:54 +000014726 info = xcmalloc(sizeof(*info), "asm_info");
14727 info->str = "";
14728
14729 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14730 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14731
14732 def = new_triple(state, OP_ASM, &void_type, 0, in);
14733 def->u.ainfo = info;
14734 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014735
Eric Biederman90089602004-05-28 14:11:54 +000014736 return def;
14737}
14738
Eric Biederman5ade04a2003-10-22 04:03:46 +000014739static void join_functions(struct compile_state *state)
14740{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014741 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014742 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014743 struct type *pnext, *param;
14744 struct type *result_type, *args_type;
14745 int idx;
14746
14747 /* Be clear the functions have not been joined yet */
14748 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014749
14750 /* Dummy file state to get debug handing right */
14751 memset(&file, 0, sizeof(file));
14752 file.basename = "";
14753 file.line = 0;
14754 file.report_line = 0;
14755 file.report_name = file.basename;
14756 file.prev = state->file;
14757 state->file = &file;
14758 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014759
Eric Biederman41203d92004-11-08 09:31:09 +000014760 if (!state->main_function) {
14761 error(state, 0, "No functions to compile\n");
14762 }
14763
Eric Biederman90089602004-05-28 14:11:54 +000014764 /* The type of arguments */
14765 args_type = state->main_function->type->right;
14766 /* The return type without any specifiers */
14767 result_type = clone_type(0, state->main_function->type->left);
14768
14769
14770 /* Verify the external arguments */
14771 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014772 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014773 "Too many external input arguments");
14774 }
14775 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014776 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014777 "Too many external output arguments");
14778 }
14779
Eric Biederman5ade04a2003-10-22 04:03:46 +000014780 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014781 end = label(state);
14782 start = label(state);
14783 start = flatten(state, state->first, start);
14784 end = flatten(state, state->first, end);
14785 in = input_asm(state);
14786 out = output_asm(state);
14787 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014788 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014789 in = flatten(state, state->first, in);
14790 call = flatten(state, state->first, call);
14791 out = flatten(state, state->first, out);
14792
14793
14794 /* Read the external input arguments */
14795 pnext = args_type;
14796 idx = 0;
14797 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14798 struct triple *expr;
14799 param = pnext;
14800 pnext = 0;
14801 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14802 pnext = param->right;
14803 param = param->left;
14804 }
14805 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014806 error(state, state->main_function,
14807 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014808 idx + 1, param->field_ident->name);
14809 }
14810 expr = read_expr(state, LHS(in, idx));
14811 RHS(call, idx) = expr;
14812 expr = flatten(state, call, expr);
14813 use_triple(expr, call);
14814
Stefan Reinauer14e22772010-04-27 06:56:47 +000014815 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014816 }
14817
14818
14819 /* Write the external output arguments */
14820 pnext = result_type;
14821 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14822 pnext = result_type->left;
14823 }
14824 for(idx = 0; idx < out->rhs; idx++) {
14825 struct triple *expr;
14826 param = pnext;
14827 pnext = 0;
14828 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14829 pnext = param->right;
14830 param = param->left;
14831 }
14832 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14833 param = 0;
14834 }
14835 if (param) {
14836 if (registers_of(state, param) != 1) {
14837 error(state, state->main_function,
14838 "Result: %d %s requires multiple registers",
14839 idx, param->field_ident->name);
14840 }
14841 expr = read_expr(state, call);
14842 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14843 expr = deref_field(state, expr, param->field_ident);
14844 }
14845 } else {
14846 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14847 }
14848 flatten(state, out, expr);
14849 RHS(out, idx) = expr;
14850 use_triple(expr, out);
14851 }
14852
14853 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014854 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014855 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14856 func->type->type_ident = lookup(state, "", 0);
14857 RHS(func, 0) = state->first;
14858 func->u.cval = 1;
14859
Eric Biederman5ade04a2003-10-22 04:03:46 +000014860 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014861 mark_live_functions(state);
14862 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014863 walk_functions(state, insert_function, end);
14864
14865 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014866 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014867 }
14868
Eric Biederman90089602004-05-28 14:11:54 +000014869 /* OK now the functions have been joined. */
14870 state->functions_joined = 1;
14871
Eric Biederman5ade04a2003-10-22 04:03:46 +000014872 /* Done now cleanup */
14873 state->file = file.prev;
14874 state->function = 0;
14875}
14876
Eric Biedermanb138ac82003-04-22 18:44:01 +000014877/*
14878 * Data structurs for optimation.
14879 */
14880
Eric Biederman5ade04a2003-10-22 04:03:46 +000014881
Eric Biederman83b991a2003-10-11 06:20:25 +000014882static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014883 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014884 int front)
14885{
14886 struct block_set **ptr, *new;
14887 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014888 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014889 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014890 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014891 ptr = head;
14892 while(*ptr) {
14893 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014894 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014895 }
14896 ptr = &(*ptr)->next;
14897 }
14898 new = xcmalloc(sizeof(*new), "block_set");
14899 new->member = user;
14900 if (front) {
14901 new->next = *head;
14902 *head = new;
14903 }
14904 else {
14905 new->next = 0;
14906 *ptr = new;
14907 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014908 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014909}
Eric Biederman83b991a2003-10-11 06:20:25 +000014910static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014911 struct block *used, struct block_set **head, struct block *unuser)
14912{
14913 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014914 int count;
14915 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014916 ptr = head;
14917 while(*ptr) {
14918 use = *ptr;
14919 if (use->member == unuser) {
14920 *ptr = use->next;
14921 memset(use, -1, sizeof(*use));
14922 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014923 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014924 }
14925 else {
14926 ptr = &use->next;
14927 }
14928 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014929 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014930}
14931
14932static void use_block(struct block *used, struct block *user)
14933{
Eric Biederman83b991a2003-10-11 06:20:25 +000014934 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014935 /* Append new to the head of the list, print_block
14936 * depends on this.
14937 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014938 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014939 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014940}
14941static void unuse_block(struct block *used, struct block *unuser)
14942{
Eric Biederman83b991a2003-10-11 06:20:25 +000014943 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014944 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014945 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014946}
14947
Eric Biederman5ade04a2003-10-22 04:03:46 +000014948static void add_block_edge(struct block *block, struct block *edge, int front)
14949{
14950 int count;
14951 count = do_use_block(block, &block->edges, edge, front);
14952 block->edge_count += count;
14953}
14954
14955static void remove_block_edge(struct block *block, struct block *edge)
14956{
14957 int count;
14958 count = do_unuse_block(block, &block->edges, edge);
14959 block->edge_count -= count;
14960}
14961
Eric Biedermanb138ac82003-04-22 18:44:01 +000014962static void idom_block(struct block *idom, struct block *user)
14963{
14964 do_use_block(idom, &idom->idominates, user, 0);
14965}
14966
14967static void unidom_block(struct block *idom, struct block *unuser)
14968{
14969 do_unuse_block(idom, &idom->idominates, unuser);
14970}
14971
14972static void domf_block(struct block *block, struct block *domf)
14973{
14974 do_use_block(block, &block->domfrontier, domf, 0);
14975}
14976
14977static void undomf_block(struct block *block, struct block *undomf)
14978{
14979 do_unuse_block(block, &block->domfrontier, undomf);
14980}
14981
14982static void ipdom_block(struct block *ipdom, struct block *user)
14983{
14984 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14985}
14986
14987static void unipdom_block(struct block *ipdom, struct block *unuser)
14988{
14989 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14990}
14991
14992static void ipdomf_block(struct block *block, struct block *ipdomf)
14993{
14994 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14995}
14996
14997static void unipdomf_block(struct block *block, struct block *unipdomf)
14998{
14999 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15000}
15001
Eric Biederman83b991a2003-10-11 06:20:25 +000015002static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015003 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015004 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15005 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015006{
Eric Biederman83b991a2003-10-11 06:20:25 +000015007 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015008 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015009 ptr = state->first;
15010 do {
Eric Biederman90089602004-05-28 14:11:54 +000015011 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015012 if (ptr->next->prev != ptr) {
15013 internal_error(state, ptr->next, "bad prev");
15014 }
15015 ptr = ptr->next;
15016 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015017 return result;
15018}
15019
Eric Biedermanb138ac82003-04-22 18:44:01 +000015020#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015021static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015022{
Eric Biederman90089602004-05-28 14:11:54 +000015023 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015024 int op;
15025 op = ins->op;
15026 if (op == OP_LIST) {
15027#if !PRINT_LIST
15028 return 0;
15029#endif
15030 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015031 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015032 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015033 }
Eric Biederman90089602004-05-28 14:11:54 +000015034 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015035
Stefan Reinauer14e22772010-04-27 06:56:47 +000015036 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015037 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015038 internal_error(state, ins, "branch used?");
15039 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015040 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015041 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015042 }
15043 return 0;
15044}
15045
Eric Biedermanb138ac82003-04-22 18:44:01 +000015046static void print_triples(struct compile_state *state)
15047{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015048 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015049 FILE *fp = state->dbgout;
15050 fprintf(fp, "--------------- triples ---------------\n");
15051 walk_triples(state, do_print_triple, fp);
15052 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015053 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015054}
15055
15056struct cf_block {
15057 struct block *block;
15058};
15059static void find_cf_blocks(struct cf_block *cf, struct block *block)
15060{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015061 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015062 if (!block || (cf[block->vertex].block == block)) {
15063 return;
15064 }
15065 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015066 for(edge = block->edges; edge; edge = edge->next) {
15067 find_cf_blocks(cf, edge->member);
15068 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015069}
15070
Eric Biederman90089602004-05-28 14:11:54 +000015071static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015072 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015073{
15074 struct cf_block *cf;
15075 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015076 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015077 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15078 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015079
Eric Biederman90089602004-05-28 14:11:54 +000015080 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015081 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015082 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015083 block = cf[i].block;
15084 if (!block)
15085 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015086 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015087 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015088 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015089 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015090 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015091 }
15092
15093 xfree(cf);
15094}
15095
Eric Biedermanb138ac82003-04-22 18:44:01 +000015096static void free_basic_block(struct compile_state *state, struct block *block)
15097{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015098 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015099 struct block *child;
15100 if (!block) {
15101 return;
15102 }
15103 if (block->vertex == -1) {
15104 return;
15105 }
15106 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015107 for(edge = block->edges; edge; edge = edge->next) {
15108 if (edge->member) {
15109 unuse_block(edge->member, block);
15110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015111 }
15112 if (block->idom) {
15113 unidom_block(block->idom, block);
15114 }
15115 block->idom = 0;
15116 if (block->ipdom) {
15117 unipdom_block(block->ipdom, block);
15118 }
15119 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015120 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015121 child = entry->member;
15122 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015123 if (child && (child->vertex != -1)) {
15124 for(edge = child->edges; edge; edge = edge->next) {
15125 edge->member = 0;
15126 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015127 }
15128 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015129 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015130 child = entry->member;
15131 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015132 if (child && (child->vertex != -1)) {
15133 child->idom = 0;
15134 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015135 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015136 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015137 child = entry->member;
15138 undomf_block(block, child);
15139 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015140 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015141 child = entry->member;
15142 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015143 if (child && (child->vertex != -1)) {
15144 child->ipdom = 0;
15145 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015146 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015147 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015148 child = entry->member;
15149 unipdomf_block(block, child);
15150 }
15151 if (block->users != 0) {
15152 internal_error(state, 0, "block still has users");
15153 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015154 while((edge = block->edges)) {
15155 child = edge->member;
15156 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015157
Eric Biederman5ade04a2003-10-22 04:03:46 +000015158 if (child && (child->vertex != -1)) {
15159 free_basic_block(state, child);
15160 }
15161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015162 memset(block, -1, sizeof(*block));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015163}
15164
Stefan Reinauer14e22772010-04-27 06:56:47 +000015165static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015166 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015167{
15168 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015169 free_basic_block(state, bb->first_block);
15170 bb->last_vertex = 0;
15171 bb->first_block = bb->last_block = 0;
15172 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015173 ins = first;
15174 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015175 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015176 ins->u.block = 0;
15177 }
15178 ins = ins->next;
15179 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015180
Eric Biedermanb138ac82003-04-22 18:44:01 +000015181}
15182
Stefan Reinauer14e22772010-04-27 06:56:47 +000015183static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015184 struct basic_blocks *bb, struct triple *first)
15185{
15186 struct block *block;
15187 struct triple *ptr;
15188 if (!triple_is_label(state, first)) {
15189 internal_error(state, first, "block does not start with a label");
15190 }
15191 /* See if this basic block has already been setup */
15192 if (first->u.block != 0) {
15193 return first->u.block;
15194 }
15195 /* Allocate another basic block structure */
15196 bb->last_vertex += 1;
15197 block = xcmalloc(sizeof(*block), "block");
15198 block->first = block->last = first;
15199 block->vertex = bb->last_vertex;
15200 ptr = first;
15201 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015202 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015203 break;
15204 }
15205 block->last = ptr;
15206 /* If ptr->u is not used remember where the baic block is */
15207 if (triple_stores_block(state, ptr)) {
15208 ptr->u.block = block;
15209 }
15210 if (triple_is_branch(state, ptr)) {
15211 break;
15212 }
15213 ptr = ptr->next;
15214 } while (ptr != bb->first);
15215 if ((ptr == bb->first) ||
15216 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015217 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015218 triple_is_ret(state, ptr))))
15219 {
15220 /* The block has no outflowing edges */
15221 }
15222 else if (triple_is_label(state, ptr)) {
15223 struct block *next;
15224 next = basic_block(state, bb, ptr);
15225 add_block_edge(block, next, 0);
15226 use_block(next, block);
15227 }
15228 else if (triple_is_branch(state, ptr)) {
15229 struct triple **expr, *first;
15230 struct block *child;
15231 /* Find the branch targets.
15232 * I special case the first branch as that magically
15233 * avoids some difficult cases for the register allocator.
15234 */
15235 expr = triple_edge_targ(state, ptr, 0);
15236 if (!expr) {
15237 internal_error(state, ptr, "branch without targets");
15238 }
15239 first = *expr;
15240 expr = triple_edge_targ(state, ptr, expr);
15241 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15242 if (!*expr) continue;
15243 child = basic_block(state, bb, *expr);
15244 use_block(child, block);
15245 add_block_edge(block, child, 0);
15246 }
15247 if (first) {
15248 child = basic_block(state, bb, first);
15249 use_block(child, block);
15250 add_block_edge(block, child, 1);
15251
15252 /* Be certain the return block of a call is
15253 * in a basic block. When it is not find
15254 * start of the block, insert a label if
15255 * necessary and build the basic block.
15256 * Then add a fake edge from the start block
15257 * to the return block of the function.
15258 */
15259 if (state->functions_joined && triple_is_call(state, ptr)
15260 && !block_of_triple(state, MISC(ptr, 0))) {
15261 struct block *tail;
15262 struct triple *start;
15263 start = triple_to_block_start(state, MISC(ptr, 0));
15264 if (!triple_is_label(state, start)) {
15265 start = pre_triple(state,
15266 start, OP_LABEL, &void_type, 0, 0);
15267 }
15268 tail = basic_block(state, bb, start);
15269 add_block_edge(child, tail, 0);
15270 use_block(tail, child);
15271 }
15272 }
15273 }
15274 else {
15275 internal_error(state, 0, "Bad basic block split");
15276 }
15277#if 0
15278{
15279 struct block_set *edge;
15280 FILE *fp = state->errout;
15281 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015282 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015283 block->first, block->last);
15284 for(edge = block->edges; edge; edge = edge->next) {
15285 fprintf(fp, " %10p [%2d]",
15286 edge->member ? edge->member->first : 0,
15287 edge->member ? edge->member->vertex : -1);
15288 }
15289 fprintf(fp, "\n");
15290}
15291#endif
15292 return block;
15293}
15294
15295
15296static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15297 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15298 void *arg)
15299{
15300 struct triple *ptr, *first;
15301 struct block *last_block;
15302 last_block = 0;
15303 first = bb->first;
15304 ptr = first;
15305 do {
15306 if (triple_stores_block(state, ptr)) {
15307 struct block *block;
15308 block = ptr->u.block;
15309 if (block && (block != last_block)) {
15310 cb(state, block, arg);
15311 }
15312 last_block = block;
15313 }
15314 ptr = ptr->next;
15315 } while(ptr != first);
15316}
15317
15318static void print_block(
15319 struct compile_state *state, struct block *block, void *arg)
15320{
15321 struct block_set *user, *edge;
15322 struct triple *ptr;
15323 FILE *fp = arg;
15324
15325 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015326 block,
Eric Biederman90089602004-05-28 14:11:54 +000015327 block->vertex);
15328
15329 for(edge = block->edges; edge; edge = edge->next) {
15330 fprintf(fp, " %p<-%p",
15331 edge->member,
15332 (edge->member && edge->member->use)?
15333 edge->member->use->member : 0);
15334 }
15335 fprintf(fp, "\n");
15336 if (block->first->op == OP_LABEL) {
15337 fprintf(fp, "%p:\n", block->first);
15338 }
15339 for(ptr = block->first; ; ) {
15340 display_triple(fp, ptr);
15341 if (ptr == block->last)
15342 break;
15343 ptr = ptr->next;
15344 if (ptr == block->first) {
15345 internal_error(state, 0, "missing block last?");
15346 }
15347 }
15348 fprintf(fp, "users %d: ", block->users);
15349 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015350 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015351 user->member,
15352 user->member->vertex);
15353 }
15354 fprintf(fp,"\n\n");
15355}
15356
15357
15358static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15359{
15360 fprintf(fp, "--------------- blocks ---------------\n");
15361 walk_blocks(state, &state->bb, print_block, fp);
15362}
15363static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15364{
15365 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15366 fprintf(fp, "After %s\n", func);
15367 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015368 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15369 print_dominators(state, fp, &state->bb);
15370 print_dominance_frontiers(state, fp, &state->bb);
15371 }
15372 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015373 }
15374}
15375
Stefan Reinauer14e22772010-04-27 06:56:47 +000015376static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015377 struct basic_blocks *bb)
15378{
15379 struct block *block;
15380 struct triple *first, *ins, *next;
15381 /* Delete the triples not in a basic block */
15382 block = 0;
15383 first = bb->first;
15384 ins = first;
15385 do {
15386 next = ins->next;
15387 if (ins->op == OP_LABEL) {
15388 block = ins->u.block;
15389 }
15390 if (!block) {
15391 struct triple_set *use;
15392 for(use = ins->use; use; use = use->next) {
15393 struct block *block;
15394 block = block_of_triple(state, use->member);
15395 if (block != 0) {
15396 internal_error(state, ins, "pruning used ins?");
15397 }
15398 }
15399 release_triple(state, ins);
15400 }
15401 if (block && block->last == ins) {
15402 block = 0;
15403 }
15404 ins = next;
15405 } while(ins != first);
15406}
15407
Stefan Reinauer14e22772010-04-27 06:56:47 +000015408static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015409 struct basic_blocks *bb)
15410{
15411 if (!triple_stores_block(state, bb->first)) {
15412 internal_error(state, 0, "ins will not store block?");
15413 }
15414 /* Initialize the state */
15415 bb->first_block = bb->last_block = 0;
15416 bb->last_vertex = 0;
15417 free_basic_blocks(state, bb);
15418
15419 /* Find the basic blocks */
15420 bb->first_block = basic_block(state, bb, bb->first);
15421
15422 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015423 * entire program is in a basic block. When it is not find
15424 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015425 * basic block. Then add a fake edge from the start block
15426 * to the final block.
15427 */
15428 if (!block_of_triple(state, bb->first->prev)) {
15429 struct triple *start;
15430 struct block *tail;
15431 start = triple_to_block_start(state, bb->first->prev);
15432 if (!triple_is_label(state, start)) {
15433 start = pre_triple(state,
15434 start, OP_LABEL, &void_type, 0, 0);
15435 }
15436 tail = basic_block(state, bb, start);
15437 add_block_edge(bb->first_block, tail, 0);
15438 use_block(tail, bb->first_block);
15439 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015440
Eric Biederman90089602004-05-28 14:11:54 +000015441 /* Find the last basic block.
15442 */
15443 bb->last_block = block_of_triple(state, bb->first->prev);
15444
15445 /* Delete the triples not in a basic block */
15446 prune_nonblock_triples(state, bb);
15447
15448#if 0
15449 /* If we are debugging print what I have just done */
15450 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15451 print_blocks(state, state->dbgout);
15452 print_control_flow(state, bb);
15453 }
15454#endif
15455}
15456
15457
Eric Biedermanb138ac82003-04-22 18:44:01 +000015458struct sdom_block {
15459 struct block *block;
15460 struct sdom_block *sdominates;
15461 struct sdom_block *sdom_next;
15462 struct sdom_block *sdom;
15463 struct sdom_block *label;
15464 struct sdom_block *parent;
15465 struct sdom_block *ancestor;
15466 int vertex;
15467};
15468
15469
15470static void unsdom_block(struct sdom_block *block)
15471{
15472 struct sdom_block **ptr;
15473 if (!block->sdom_next) {
15474 return;
15475 }
15476 ptr = &block->sdom->sdominates;
15477 while(*ptr) {
15478 if ((*ptr) == block) {
15479 *ptr = block->sdom_next;
15480 return;
15481 }
15482 ptr = &(*ptr)->sdom_next;
15483 }
15484}
15485
15486static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15487{
15488 unsdom_block(block);
15489 block->sdom = sdom;
15490 block->sdom_next = sdom->sdominates;
15491 sdom->sdominates = block;
15492}
15493
15494
15495
15496static int initialize_sdblock(struct sdom_block *sd,
15497 struct block *parent, struct block *block, int vertex)
15498{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015499 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015500 if (!block || (sd[block->vertex].block == block)) {
15501 return vertex;
15502 }
15503 vertex += 1;
15504 /* Renumber the blocks in a convinient fashion */
15505 block->vertex = vertex;
15506 sd[vertex].block = block;
15507 sd[vertex].sdom = &sd[vertex];
15508 sd[vertex].label = &sd[vertex];
15509 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15510 sd[vertex].ancestor = 0;
15511 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015512 for(edge = block->edges; edge; edge = edge->next) {
15513 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15514 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015515 return vertex;
15516}
15517
Eric Biederman83b991a2003-10-11 06:20:25 +000015518static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015519 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015520 struct block *parent, struct block *block, int vertex)
15521{
15522 struct block_set *user;
15523 if (!block || (sd[block->vertex].block == block)) {
15524 return vertex;
15525 }
15526 vertex += 1;
15527 /* Renumber the blocks in a convinient fashion */
15528 block->vertex = vertex;
15529 sd[vertex].block = block;
15530 sd[vertex].sdom = &sd[vertex];
15531 sd[vertex].label = &sd[vertex];
15532 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15533 sd[vertex].ancestor = 0;
15534 sd[vertex].vertex = vertex;
15535 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015536 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015537 }
15538 return vertex;
15539}
15540
Stefan Reinauer14e22772010-04-27 06:56:47 +000015541static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015542 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015543{
15544 struct block *block;
15545 int vertex;
15546 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015547 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015548
Eric Biederman5ade04a2003-10-22 04:03:46 +000015549 /* Walk through the graph and find unconnected blocks. Add a
15550 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015551 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015552 */
Eric Biederman90089602004-05-28 14:11:54 +000015553 block = bb->first_block->last->next->u.block;
15554 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015555 if (sd[block->vertex].block == block) {
15556 continue;
15557 }
Eric Biederman530b5192003-07-01 10:05:30 +000015558#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015559 {
15560 FILE *fp = state->errout;
15561 fprintf(fp, "Adding %d\n", vertex +1);
15562 }
Eric Biederman530b5192003-07-01 10:05:30 +000015563#endif
Eric Biederman90089602004-05-28 14:11:54 +000015564 add_block_edge(block, bb->last_block, 0);
15565 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015566
Eric Biederman90089602004-05-28 14:11:54 +000015567 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015568 }
15569 return vertex;
15570}
15571
15572static void compress_ancestors(struct sdom_block *v)
15573{
15574 /* This procedure assumes ancestor(v) != 0 */
15575 /* if (ancestor(ancestor(v)) != 0) {
15576 * compress(ancestor(ancestor(v)));
15577 * if (semi(label(ancestor(v))) < semi(label(v))) {
15578 * label(v) = label(ancestor(v));
15579 * }
15580 * ancestor(v) = ancestor(ancestor(v));
15581 * }
15582 */
15583 if (!v->ancestor) {
15584 return;
15585 }
15586 if (v->ancestor->ancestor) {
15587 compress_ancestors(v->ancestor->ancestor);
15588 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15589 v->label = v->ancestor->label;
15590 }
15591 v->ancestor = v->ancestor->ancestor;
15592 }
15593}
15594
Stefan Reinauer14e22772010-04-27 06:56:47 +000015595static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015596 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015597{
15598 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015599 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015600 * for each v <= pred(w) {
15601 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015602 * if (semi[u] < semi[w] {
15603 * semi[w] = semi[u];
15604 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015605 * }
15606 * add w to bucket(vertex(semi[w]));
15607 * LINK(parent(w), w);
15608 *
15609 * // step 3
15610 * for each v <= bucket(parent(w)) {
15611 * delete v from bucket(parent(w));
15612 * u = EVAL(v);
15613 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15614 * }
15615 */
Eric Biederman90089602004-05-28 14:11:54 +000015616 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015617 struct sdom_block *v, *parent, *next;
15618 struct block_set *user;
15619 struct block *block;
15620 block = sd[i].block;
15621 parent = sd[i].parent;
15622 /* Step 2 */
15623 for(user = block->use; user; user = user->next) {
15624 struct sdom_block *v, *u;
15625 v = &sd[user->member->vertex];
15626 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15627 if (u->sdom->vertex < sd[i].sdom->vertex) {
15628 sd[i].sdom = u->sdom;
15629 }
15630 }
15631 sdom_block(sd[i].sdom, &sd[i]);
15632 sd[i].ancestor = parent;
15633 /* Step 3 */
15634 for(v = parent->sdominates; v; v = next) {
15635 struct sdom_block *u;
15636 next = v->sdom_next;
15637 unsdom_block(v);
15638 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015639 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015640 u->block : parent->block;
15641 }
15642 }
15643}
15644
Stefan Reinauer14e22772010-04-27 06:56:47 +000015645static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015646 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015647{
15648 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015649 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015650 * for each v <= pred(w) {
15651 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015652 * if (semi[u] < semi[w] {
15653 * semi[w] = semi[u];
15654 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015655 * }
15656 * add w to bucket(vertex(semi[w]));
15657 * LINK(parent(w), w);
15658 *
15659 * // step 3
15660 * for each v <= bucket(parent(w)) {
15661 * delete v from bucket(parent(w));
15662 * u = EVAL(v);
15663 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15664 * }
15665 */
Eric Biederman90089602004-05-28 14:11:54 +000015666 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015667 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015668 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015669 struct block *block;
15670 block = sd[i].block;
15671 parent = sd[i].parent;
15672 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015673 for(edge = block->edges; edge; edge = edge->next) {
15674 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015675 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15676 if (u->sdom->vertex < sd[i].sdom->vertex) {
15677 sd[i].sdom = u->sdom;
15678 }
15679 }
15680 sdom_block(sd[i].sdom, &sd[i]);
15681 sd[i].ancestor = parent;
15682 /* Step 3 */
15683 for(v = parent->sdominates; v; v = next) {
15684 struct sdom_block *u;
15685 next = v->sdom_next;
15686 unsdom_block(v);
15687 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015688 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015689 u->block : parent->block;
15690 }
15691 }
15692}
15693
Stefan Reinauer14e22772010-04-27 06:56:47 +000015694static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015695 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015696{
15697 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015698 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015699 struct block *block;
15700 block = sd[i].block;
15701 if (block->idom->vertex != sd[i].sdom->vertex) {
15702 block->idom = block->idom->idom;
15703 }
15704 idom_block(block->idom, block);
15705 }
15706 sd[1].block->idom = 0;
15707}
15708
Stefan Reinauer14e22772010-04-27 06:56:47 +000015709static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015710 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015711{
15712 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015713 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015714 struct block *block;
15715 block = sd[i].block;
15716 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15717 block->ipdom = block->ipdom->ipdom;
15718 }
15719 ipdom_block(block->ipdom, block);
15720 }
15721 sd[1].block->ipdom = 0;
15722}
15723
15724 /* Theorem 1:
15725 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015726 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015727 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015728 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015729 * v dominates w if and only if v is a proper ancestor of w in
15730 * the dominator tree.
15731 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015732 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015733 * If v and w are vertices of G such that v <= w,
15734 * than any path from v to w must contain a common ancestor
15735 * of v and w in T.
15736 */
15737 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15738 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15739 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15740 /* Theorem 2:
15741 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15742 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15743 */
15744 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015745 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015746 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15747 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15748 */
15749 /* Lemma 5: Let vertices v,w satisfy v -> w.
15750 * Then v -> idom(w) or idom(w) -> idom(v)
15751 */
15752
Eric Biederman90089602004-05-28 14:11:54 +000015753static void find_immediate_dominators(struct compile_state *state,
15754 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015755{
15756 struct sdom_block *sd;
15757 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15758 * vi > w for (1 <= i <= k - 1}
15759 */
15760 /* Theorem 4:
15761 * For any vertex w != r.
15762 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015763 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015764 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15765 */
15766 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015767 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015768 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15769 * Then:
15770 * { sdom(w) if sdom(w) = sdom(u),
15771 * idom(w) = {
15772 * { idom(u) otherwise
15773 */
15774 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015775 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015776 * Number the vertices from 1 to N as they are reached during
15777 * the search. Initialize the variables used in succeeding steps.
15778 * Step 2. Compute the semidominators of all vertices by applying
15779 * theorem 4. Carry out the computation vertex by vertex in
15780 * decreasing order by number.
15781 * Step 3. Implicitly define the immediate dominator of each vertex
15782 * by applying Corollary 1.
15783 * Step 4. Explicitly define the immediate dominator of each vertex,
15784 * carrying out the computation vertex by vertex in increasing order
15785 * by number.
15786 */
15787 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015788 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15789 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015790#if 0
15791 sd[1].size = 0;
15792 sd[1].label = 0;
15793 sd[1].sdom = 0;
15794#endif
15795 /* Step 2 compute the semidominators */
15796 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015797 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015798 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015799 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015800 xfree(sd);
15801}
15802
Eric Biederman90089602004-05-28 14:11:54 +000015803static void find_post_dominators(struct compile_state *state,
15804 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015805{
15806 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015807 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015808 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015809 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015810
Eric Biederman90089602004-05-28 14:11:54 +000015811 vertex = setup_spdblocks(state, bb, sd);
15812 if (vertex != bb->last_vertex) {
15813 internal_error(state, 0, "missing %d blocks",
15814 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015815 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015816
15817 /* Step 2 compute the semidominators */
15818 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015819 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015820 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015821 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015822 xfree(sd);
15823}
15824
15825
15826
15827static void find_block_domf(struct compile_state *state, struct block *block)
15828{
15829 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015830 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015831 if (block->domfrontier != 0) {
15832 internal_error(state, block->first, "domfrontier present?");
15833 }
15834 for(user = block->idominates; user; user = user->next) {
15835 child = user->member;
15836 if (child->idom != block) {
15837 internal_error(state, block->first, "bad idom");
15838 }
15839 find_block_domf(state, child);
15840 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015841 for(edge = block->edges; edge; edge = edge->next) {
15842 if (edge->member->idom != block) {
15843 domf_block(block, edge->member);
15844 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015845 }
15846 for(user = block->idominates; user; user = user->next) {
15847 struct block_set *frontier;
15848 child = user->member;
15849 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15850 if (frontier->member->idom != block) {
15851 domf_block(block, frontier->member);
15852 }
15853 }
15854 }
15855}
15856
15857static void find_block_ipdomf(struct compile_state *state, struct block *block)
15858{
15859 struct block *child;
15860 struct block_set *user;
15861 if (block->ipdomfrontier != 0) {
15862 internal_error(state, block->first, "ipdomfrontier present?");
15863 }
15864 for(user = block->ipdominates; user; user = user->next) {
15865 child = user->member;
15866 if (child->ipdom != block) {
15867 internal_error(state, block->first, "bad ipdom");
15868 }
15869 find_block_ipdomf(state, child);
15870 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015871 for(user = block->use; user; user = user->next) {
15872 if (user->member->ipdom != block) {
15873 ipdomf_block(block, user->member);
15874 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015875 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015876 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015877 struct block_set *frontier;
15878 child = user->member;
15879 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15880 if (frontier->member->ipdom != block) {
15881 ipdomf_block(block, frontier->member);
15882 }
15883 }
15884 }
15885}
15886
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015887static void print_dominated(
15888 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015889{
15890 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015891 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015892
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015893 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015894 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896 if (user->member->idom != block) {
15897 internal_error(state, user->member->first, "bad idom");
15898 }
15899 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015900 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015901}
15902
Eric Biederman5ade04a2003-10-22 04:03:46 +000015903static void print_dominated2(
15904 struct compile_state *state, FILE *fp, int depth, struct block *block)
15905{
15906 struct block_set *user;
15907 struct triple *ins;
15908 struct occurance *ptr, *ptr2;
15909 const char *filename1, *filename2;
15910 int equal_filenames;
15911 int i;
15912 for(i = 0; i < depth; i++) {
15913 fprintf(fp, " ");
15914 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015915 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015916 block->vertex, block, block->first, block->last);
15917 ins = block->first;
15918 while(ins != block->last && (ins->occurance->line == 0)) {
15919 ins = ins->next;
15920 }
15921 ptr = ins->occurance;
15922 ptr2 = block->last->occurance;
15923 filename1 = ptr->filename? ptr->filename : "";
15924 filename2 = ptr2->filename? ptr2->filename : "";
15925 equal_filenames = (strcmp(filename1, filename2) == 0);
15926 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15927 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15928 } else if (equal_filenames) {
15929 fprintf(fp, " %s:(%d - %d)",
15930 ptr->filename, ptr->line, ptr2->line);
15931 } else {
15932 fprintf(fp, " (%s:%d - %s:%d)",
15933 ptr->filename, ptr->line,
15934 ptr2->filename, ptr2->line);
15935 }
15936 fprintf(fp, "\n");
15937 for(user = block->idominates; user; user = user->next) {
15938 print_dominated2(state, fp, depth + 1, user->member);
15939 }
15940}
15941
Eric Biederman90089602004-05-28 14:11:54 +000015942static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015943{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015944 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015945 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015946 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015947 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015948}
15949
15950
15951static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015952 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015953{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015954 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015955
15956 if (!block || (block->vertex != vertex + 1)) {
15957 return vertex;
15958 }
15959 vertex += 1;
15960
Eric Biederman7dea9552004-06-29 05:38:37 +000015961 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015962 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015963 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015964 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015965 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015966
Eric Biederman5ade04a2003-10-22 04:03:46 +000015967 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015968 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015969 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015970 return vertex;
15971}
Eric Biederman90089602004-05-28 14:11:54 +000015972static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015973 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015974{
Eric Biederman7dea9552004-06-29 05:38:37 +000015975 fprintf(fp, "\ndominance frontiers\n");
15976 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015977
Eric Biedermanb138ac82003-04-22 18:44:01 +000015978}
15979
Eric Biederman90089602004-05-28 14:11:54 +000015980static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015981{
15982 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015983 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015984 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015985 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015987 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015988 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015989 print_dominance_frontiers(state, state->dbgout, bb);
15990 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015991 }
15992}
15993
15994
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015995static void print_ipdominated(
15996 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015997{
15998 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015999 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016000
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016001 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016002 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004 if (user->member->ipdom != block) {
16005 internal_error(state, user->member->first, "bad ipdom");
16006 }
16007 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016008 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016009}
16010
Eric Biederman90089602004-05-28 14:11:54 +000016011static void print_ipdominators(struct compile_state *state, FILE *fp,
16012 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016013{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016014 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016015 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016016}
16017
16018static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016019 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016020{
16021 struct block_set *user;
16022
16023 if (!block || (block->vertex != vertex + 1)) {
16024 return vertex;
16025 }
16026 vertex += 1;
16027
Eric Biederman7dea9552004-06-29 05:38:37 +000016028 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016029 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016030 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016031 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016032 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016033 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016034 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035 }
16036 return vertex;
16037}
Eric Biederman90089602004-05-28 14:11:54 +000016038static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016039 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016040{
Eric Biederman7dea9552004-06-29 05:38:37 +000016041 fprintf(fp, "\nipdominance frontiers\n");
16042 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016043
Eric Biedermanb138ac82003-04-22 18:44:01 +000016044}
16045
Eric Biederman90089602004-05-28 14:11:54 +000016046static void analyze_ipdominators(struct compile_state *state,
16047 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016048{
16049 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016050 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016051 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016052 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016053 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016054 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016055 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016056 print_ipdominance_frontiers(state, state->dbgout, bb);
16057 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016058 }
16059}
16060
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016061static int bdominates(struct compile_state *state,
16062 struct block *dom, struct block *sub)
16063{
16064 while(sub && (sub != dom)) {
16065 sub = sub->idom;
16066 }
16067 return sub == dom;
16068}
16069
16070static int tdominates(struct compile_state *state,
16071 struct triple *dom, struct triple *sub)
16072{
16073 struct block *bdom, *bsub;
16074 int result;
16075 bdom = block_of_triple(state, dom);
16076 bsub = block_of_triple(state, sub);
16077 if (bdom != bsub) {
16078 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016079 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016080 else {
16081 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016082 if (!bdom || !bsub) {
16083 internal_error(state, dom, "huh?");
16084 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016085 ins = sub;
16086 while((ins != bsub->first) && (ins != dom)) {
16087 ins = ins->prev;
16088 }
16089 result = (ins == dom);
16090 }
16091 return result;
16092}
16093
Eric Biederman90089602004-05-28 14:11:54 +000016094static void analyze_basic_blocks(
16095 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016096{
Eric Biederman90089602004-05-28 14:11:54 +000016097 setup_basic_blocks(state, bb);
16098 analyze_idominators(state, bb);
16099 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016100}
16101
Eric Biedermanb138ac82003-04-22 18:44:01 +000016102static void insert_phi_operations(struct compile_state *state)
16103{
16104 size_t size;
16105 struct triple *first;
16106 int *has_already, *work;
16107 struct block *work_list, **work_list_tail;
16108 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016109 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016110
Eric Biederman90089602004-05-28 14:11:54 +000016111 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016112 has_already = xcmalloc(size, "has_already");
16113 work = xcmalloc(size, "work");
16114 iter = 0;
16115
Eric Biederman83b991a2003-10-11 06:20:25 +000016116 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016117 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016118 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016119 struct triple_set *user, *unext;
16120 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016121
16122 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016123 continue;
16124 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016125
Eric Biedermanb138ac82003-04-22 18:44:01 +000016126 iter += 1;
16127 work_list = 0;
16128 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016129 for(user = var->use; user; user = unext) {
16130 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016131 if (MISC(var, 0) == user->member) {
16132 continue;
16133 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016134 if (user->member->op == OP_READ) {
16135 continue;
16136 }
16137 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016138 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016139 "bad variable access");
16140 }
16141 block = user->member->u.block;
16142 if (!block) {
16143 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016144 release_triple(state, user->member);
16145 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016146 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016147 if (work[block->vertex] >= iter) {
16148 continue;
16149 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016150 work[block->vertex] = iter;
16151 *work_list_tail = block;
16152 block->work_next = 0;
16153 work_list_tail = &block->work_next;
16154 }
16155 for(block = work_list; block; block = block->work_next) {
16156 struct block_set *df;
16157 for(df = block->domfrontier; df; df = df->next) {
16158 struct triple *phi;
16159 struct block *front;
16160 int in_edges;
16161 front = df->member;
16162
16163 if (has_already[front->vertex] >= iter) {
16164 continue;
16165 }
16166 /* Count how many edges flow into this block */
16167 in_edges = front->users;
16168 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016169 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016170 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016171 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016172 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016173 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016174 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016175 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016176#if 1
16177 if (phi->rhs != in_edges) {
16178 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16179 phi->rhs, in_edges);
16180 }
16181#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016182 /* Insert the phi functions immediately after the label */
16183 insert_triple(state, front->first->next, phi);
16184 if (front->first == front->last) {
16185 front->last = front->first->next;
16186 }
16187 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016188 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016189
Eric Biedermanb138ac82003-04-22 18:44:01 +000016190 /* If necessary plan to visit the basic block */
16191 if (work[front->vertex] >= iter) {
16192 continue;
16193 }
16194 work[front->vertex] = iter;
16195 *work_list_tail = front;
16196 front->work_next = 0;
16197 work_list_tail = &front->work_next;
16198 }
16199 }
16200 }
16201 xfree(has_already);
16202 xfree(work);
16203}
16204
Eric Biederman66fe2222003-07-04 15:14:04 +000016205
Eric Biederman83b991a2003-10-11 06:20:25 +000016206struct stack {
16207 struct triple_set *top;
16208 unsigned orig_id;
16209};
16210
Eric Biederman90089602004-05-28 14:11:54 +000016211static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016212{
16213 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016214 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016215 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016216 ins = first;
16217 do {
Eric Biederman90089602004-05-28 14:11:54 +000016218 if (triple_is_auto_var(state, ins)) {
16219 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016220 }
16221 ins = ins->next;
16222 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016223 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016224}
16225
Eric Biederman90089602004-05-28 14:11:54 +000016226static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016227{
16228 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016229 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016230 first = state->first;
16231 ins = first;
16232 do {
Eric Biederman90089602004-05-28 14:11:54 +000016233 if (triple_is_auto_var(state, ins)) {
16234 auto_vars += 1;
16235 stacks[auto_vars].orig_id = ins->id;
16236 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016237 }
16238 ins = ins->next;
16239 } while(ins != first);
16240}
16241
Eric Biederman90089602004-05-28 14:11:54 +000016242static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016243{
16244 struct triple *first, *ins;
16245 first = state->first;
16246 ins = first;
16247 do {
Eric Biederman90089602004-05-28 14:11:54 +000016248 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016249 ins->id = stacks[ins->id].orig_id;
16250 }
16251 ins = ins->next;
16252 } while(ins != first);
16253}
16254
16255static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016256{
16257 struct triple_set *head;
16258 struct triple *top_val;
16259 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016260 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016261 if (head) {
16262 top_val = head->member;
16263 }
16264 return top_val;
16265}
16266
Eric Biederman83b991a2003-10-11 06:20:25 +000016267static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016268{
16269 struct triple_set *new;
16270 /* Append new to the head of the list,
16271 * it's the only sensible behavoir for a stack.
16272 */
16273 new = xcmalloc(sizeof(*new), "triple_set");
16274 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016275 new->next = stacks[var->id].top;
16276 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016277}
16278
Eric Biederman83b991a2003-10-11 06:20:25 +000016279static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016280{
16281 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016282 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016283 while(*ptr) {
16284 set = *ptr;
16285 if (set->member == oldval) {
16286 *ptr = set->next;
16287 xfree(set);
16288 /* Only free one occurance from the stack */
16289 return;
16290 }
16291 else {
16292 ptr = &set->next;
16293 }
16294 }
16295}
16296
Eric Biedermanb138ac82003-04-22 18:44:01 +000016297/*
16298 * C(V)
16299 * S(V)
16300 */
16301static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016302 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016303{
16304 struct block_set *set;
16305 struct triple *ptr;
16306 int edge;
16307 if (!parent || !block)
16308 return;
16309 /* Find the edge I am coming in on */
16310 edge = 0;
16311 for(set = block->use; set; set = set->next, edge++) {
16312 if (set->member == parent) {
16313 break;
16314 }
16315 }
16316 if (!set) {
16317 internal_error(state, 0, "phi input is not on a control predecessor");
16318 }
16319 for(ptr = block->first; ; ptr = ptr->next) {
16320 if (ptr->op == OP_PHI) {
16321 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016322 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016323 if (!var) {
16324 internal_error(state, ptr, "no var???");
16325 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016326 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016327 val = peek_triple(stacks, var);
16328 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016329 internal_error(state, val, "bad value in phi");
16330 }
Eric Biederman90089602004-05-28 14:11:54 +000016331 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016332 internal_error(state, ptr, "edges > phi rhs");
16333 }
16334 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016335 if ((*slot != 0) && (*slot != val)) {
16336 internal_error(state, ptr, "phi already bound on this edge");
16337 }
16338 *slot = val;
16339 use_triple(val, ptr);
16340 }
16341 if (ptr == block->last) {
16342 break;
16343 }
16344 }
16345}
16346
16347
16348static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016349 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016350{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016351 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016352 struct triple *ptr, *next, *last;
16353 int done;
16354 if (!block)
16355 return;
16356 last = block->first;
16357 done = 0;
16358 for(ptr = block->first; !done; ptr = next) {
16359 next = ptr->next;
16360 if (ptr == block->last) {
16361 done = 1;
16362 }
16363 /* RHS(A) */
16364 if (ptr->op == OP_READ) {
16365 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016366 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016367 if (!triple_is_auto_var(state, var)) {
16368 internal_error(state, ptr, "read of non auto var!");
16369 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016370 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016371 /* Find the current value of the variable */
16372 val = peek_triple(stacks, var);
16373 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016374 /* Let the optimizer at variables that are not initially
16375 * set. But give it a bogus value so things seem to
16376 * work by accident. This is useful for bitfields because
16377 * setting them always involves a read-modify-write.
16378 */
16379 if (TYPE_ARITHMETIC(ptr->type->type)) {
16380 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16381 val->u.cval = 0xdeadbeaf;
16382 } else {
16383 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16384 }
16385 }
16386 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016387 error(state, ptr, "variable used without being set");
16388 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016389 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16390 internal_error(state, val, "bad value in read");
16391 }
16392 propogate_use(state, ptr, val);
16393 release_triple(state, ptr);
16394 continue;
16395 }
16396 /* LHS(A) */
16397 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016398 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016399 var = MISC(ptr, 0);
16400 if (!triple_is_auto_var(state, var)) {
16401 internal_error(state, ptr, "write to non auto var!");
16402 }
16403 tval = val = RHS(ptr, 0);
16404 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16405 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016406 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016407 }
Eric Biederman90089602004-05-28 14:11:54 +000016408 /* Insert a cast if the types differ */
16409 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016410 if (val->op == OP_INTCONST) {
16411 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16412 tval->u.cval = val->u.cval;
16413 }
16414 else {
Eric Biederman90089602004-05-28 14:11:54 +000016415 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016416 use_triple(val, tval);
16417 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016418 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016419 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016420 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016421 use_triple(tval, ptr);
16422 }
16423 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016424 unuse_triple(var, ptr);
16425 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016426 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016427 }
16428 if (ptr->op == OP_PHI) {
16429 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016430 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016431 if (!triple_is_auto_var(state, var)) {
16432 internal_error(state, ptr, "phi references non auto var!");
16433 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016434 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016435 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016436 }
16437 last = ptr;
16438 }
16439 block->last = last;
16440
16441 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016442 for(edge = block->edges; edge; edge = edge->next) {
16443 fixup_block_phi_variables(state, stacks, block, edge->member);
16444 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016445 /* rename variables in the dominated nodes */
16446 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016447 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016448 }
16449 /* pop the renamed variable stack */
16450 last = block->first;
16451 done = 0;
16452 for(ptr = block->first; !done ; ptr = next) {
16453 next = ptr->next;
16454 if (ptr == block->last) {
16455 done = 1;
16456 }
16457 if (ptr->op == OP_WRITE) {
16458 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016459 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016460 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016461 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016462 release_triple(state, ptr);
16463 continue;
16464 }
16465 if (ptr->op == OP_PHI) {
16466 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016467 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016468 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016469 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016470 }
16471 last = ptr;
16472 }
16473 block->last = last;
16474}
16475
Eric Biederman83b991a2003-10-11 06:20:25 +000016476static void rename_variables(struct compile_state *state)
16477{
16478 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016479 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016480
16481 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016482 auto_vars = count_auto_vars(state);
16483 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016484
Eric Biederman90089602004-05-28 14:11:54 +000016485 /* Give each auto_var a stack */
16486 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016487
16488 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016489 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016490
Eric Biederman90089602004-05-28 14:11:54 +000016491 /* Remove the stacks from the auto_vars */
16492 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016493 xfree(stacks);
16494}
16495
Eric Biedermanb138ac82003-04-22 18:44:01 +000016496static void prune_block_variables(struct compile_state *state,
16497 struct block *block)
16498{
16499 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016500 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016501 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016502
Eric Biedermanb138ac82003-04-22 18:44:01 +000016503 done = 0;
16504 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016505 /* Be extremely careful I am deleting the list
16506 * as I walk trhough it.
16507 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016508 next = ptr->next;
16509 if (ptr == block->last) {
16510 done = 1;
16511 }
Eric Biederman90089602004-05-28 14:11:54 +000016512 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016513 struct triple_set *user, *next;
16514 for(user = ptr->use; user; user = next) {
16515 struct triple *use;
16516 next = user->next;
16517 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016518 if (MISC(ptr, 0) == user->member) {
16519 continue;
16520 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016521 if (use->op != OP_PHI) {
16522 internal_error(state, use, "decl still used");
16523 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016524 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016525 internal_error(state, use, "bad phi use of decl");
16526 }
16527 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016528 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016529 }
Eric Biederman90089602004-05-28 14:11:54 +000016530 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16531 /* Delete the adecl */
16532 release_triple(state, MISC(ptr, 0));
16533 /* And the piece */
16534 release_triple(state, ptr);
16535 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016536 continue;
16537 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016538 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016539 for(user = block->idominates; user; user = user->next) {
16540 prune_block_variables(state, user->member);
16541 }
16542}
16543
Eric Biederman66fe2222003-07-04 15:14:04 +000016544struct phi_triple {
16545 struct triple *phi;
16546 unsigned orig_id;
16547 int alive;
16548};
16549
16550static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16551{
16552 struct triple **slot;
16553 int zrhs, i;
16554 if (live[phi->id].alive) {
16555 return;
16556 }
16557 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016558 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016559 slot = &RHS(phi, 0);
16560 for(i = 0; i < zrhs; i++) {
16561 struct triple *used;
16562 used = slot[i];
16563 if (used && (used->op == OP_PHI)) {
16564 keep_phi(state, live, used);
16565 }
16566 }
16567}
16568
16569static void prune_unused_phis(struct compile_state *state)
16570{
16571 struct triple *first, *phi;
16572 struct phi_triple *live;
16573 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016574
Eric Biederman66fe2222003-07-04 15:14:04 +000016575 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016576 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016577
16578 /* Count how many phi functions I need to process */
16579 phis = 0;
16580 for(phi = first->next; phi != first; phi = phi->next) {
16581 if (phi->op == OP_PHI) {
16582 phis += 1;
16583 }
16584 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016585
Eric Biederman66fe2222003-07-04 15:14:04 +000016586 /* Mark them all dead */
16587 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16588 phis = 0;
16589 for(phi = first->next; phi != first; phi = phi->next) {
16590 if (phi->op != OP_PHI) {
16591 continue;
16592 }
16593 live[phis].alive = 0;
16594 live[phis].orig_id = phi->id;
16595 live[phis].phi = phi;
16596 phi->id = phis;
16597 phis += 1;
16598 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016599
Eric Biederman66fe2222003-07-04 15:14:04 +000016600 /* Mark phis alive that are used by non phis */
16601 for(i = 0; i < phis; i++) {
16602 struct triple_set *set;
16603 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16604 if (set->member->op != OP_PHI) {
16605 keep_phi(state, live, live[i].phi);
16606 break;
16607 }
16608 }
16609 }
16610
16611 /* Delete the extraneous phis */
16612 for(i = 0; i < phis; i++) {
16613 struct triple **slot;
16614 int zrhs, j;
16615 if (!live[i].alive) {
16616 release_triple(state, live[i].phi);
16617 continue;
16618 }
16619 phi = live[i].phi;
16620 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016621 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016622 for(j = 0; j < zrhs; j++) {
16623 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016624 struct triple *unknown;
16625 get_occurance(phi->occurance);
16626 unknown = flatten(state, state->global_pool,
16627 alloc_triple(state, OP_UNKNOWNVAL,
16628 phi->type, 0, 0, phi->occurance));
16629 slot[j] = unknown;
16630 use_triple(unknown, phi);
16631 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016632#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016633 warning(state, phi, "variable not set at index %d on all paths to use", j);
16634#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016635 }
16636 }
16637 }
16638 xfree(live);
16639}
16640
Eric Biedermanb138ac82003-04-22 18:44:01 +000016641static void transform_to_ssa_form(struct compile_state *state)
16642{
16643 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016644 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016645
Eric Biederman90089602004-05-28 14:11:54 +000016646 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016647 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016648
Eric Biederman90089602004-05-28 14:11:54 +000016649 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016650}
16651
16652
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016653static void clear_vertex(
16654 struct compile_state *state, struct block *block, void *arg)
16655{
Eric Biederman83b991a2003-10-11 06:20:25 +000016656 /* Clear the current blocks vertex and the vertex of all
16657 * of the current blocks neighbors in case there are malformed
16658 * blocks with now instructions at this point.
16659 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016660 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016661 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016662 for(edge = block->edges; edge; edge = edge->next) {
16663 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016664 }
16665 for(user = block->use; user; user = user->next) {
16666 user->member->vertex = 0;
16667 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016668}
16669
16670static void mark_live_block(
16671 struct compile_state *state, struct block *block, int *next_vertex)
16672{
16673 /* See if this is a block that has not been marked */
16674 if (block->vertex != 0) {
16675 return;
16676 }
16677 block->vertex = *next_vertex;
16678 *next_vertex += 1;
16679 if (triple_is_branch(state, block->last)) {
16680 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016681 targ = triple_edge_targ(state, block->last, 0);
16682 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016683 if (!*targ) {
16684 continue;
16685 }
16686 if (!triple_stores_block(state, *targ)) {
16687 internal_error(state, 0, "bad targ");
16688 }
16689 mark_live_block(state, (*targ)->u.block, next_vertex);
16690 }
Eric Biederman90089602004-05-28 14:11:54 +000016691 /* Ensure the last block of a function remains alive */
16692 if (triple_is_call(state, block->last)) {
16693 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16694 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016695 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016696 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016697 struct triple *ins;
16698 ins = block->last->next;
16699 if (!triple_stores_block(state, ins)) {
16700 internal_error(state, 0, "bad block start");
16701 }
16702 mark_live_block(state, ins->u.block, next_vertex);
16703 }
16704}
16705
Eric Biedermanb138ac82003-04-22 18:44:01 +000016706static void transform_from_ssa_form(struct compile_state *state)
16707{
16708 /* To get out of ssa form we insert moves on the incoming
16709 * edges to blocks containting phi functions.
16710 */
16711 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016712 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016713 int next_vertex;
16714
16715 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016716 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016717 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016718 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016719
16720 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016721 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016722 for(phi = first->next; phi != first ; phi = next) {
16723 struct block_set *set;
16724 struct block *block;
16725 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016726 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016727 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016728 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016729 next = phi->next;
16730 if (phi->op != OP_PHI) {
16731 continue;
16732 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016733
Eric Biedermanb138ac82003-04-22 18:44:01 +000016734 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016735 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016736
Eric Biederman83b991a2003-10-11 06:20:25 +000016737 /* If this phi is in a dead block just forget it */
16738 if (block->vertex == 0) {
16739 release_triple(state, phi);
16740 continue;
16741 }
16742
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016743 /* Forget uses from code in dead blocks */
16744 for(use = phi->use; use; use = use_next) {
16745 struct block *ublock;
16746 struct triple **expr;
16747 use_next = use->next;
16748 ublock = block_of_triple(state, use->member);
16749 if ((use->member == phi) || (ublock->vertex != 0)) {
16750 continue;
16751 }
16752 expr = triple_rhs(state, use->member, 0);
16753 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16754 if (*expr == phi) {
16755 *expr = 0;
16756 }
16757 }
16758 unuse_triple(phi, use->member);
16759 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016760 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016761 if (registers_of(state, phi->type) != 1) {
16762 internal_error(state, phi, "phi->type does not fit in a single register!");
16763 }
16764 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16765 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016766
Eric Biederman83b991a2003-10-11 06:20:25 +000016767 /* Replaces use of phi with var */
16768 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016769
Eric Biederman90089602004-05-28 14:11:54 +000016770 /* Count the readers */
16771 readers = 0;
16772 for(use = var->use; use; use = use->next) {
16773 if (use->member != MISC(var, 0)) {
16774 readers++;
16775 }
16776 }
16777
Eric Biedermanb138ac82003-04-22 18:44:01 +000016778 /* Walk all of the incoming edges/blocks and insert moves.
16779 */
Eric Biederman90089602004-05-28 14:11:54 +000016780 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016781 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016782 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016783 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016784 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016785 eblock = set->member;
16786 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016787 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016788 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016789 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016790
Eric Biederman83b991a2003-10-11 06:20:25 +000016791 /* If we don't have a value that belongs in an OP_WRITE
16792 * continue on.
16793 */
Eric Biederman90089602004-05-28 14:11:54 +000016794 if (!val || (val == &unknown_triple) || (val == phi)
16795 || (vblock && (vblock->vertex == 0))) {
16796 continue;
16797 }
16798 /* If the value should never occur error */
16799 if (!vblock) {
16800 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016801 continue;
16802 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016803
16804 /* If the value occurs in a dead block see if a replacement
16805 * block can be found.
16806 */
16807 while(eblock && (eblock->vertex == 0)) {
16808 eblock = eblock->idom;
16809 }
16810 /* If not continue on with the next value. */
16811 if (!eblock || (eblock->vertex == 0)) {
16812 continue;
16813 }
16814
16815 /* If we have an empty incoming block ignore it. */
16816 if (!eblock->first) {
16817 internal_error(state, 0, "empty block?");
16818 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016819
Eric Biederman530b5192003-07-01 10:05:30 +000016820 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016821 /* Walk through the edge block backwards to find an
16822 * appropriate location for the OP_WRITE.
16823 */
16824 for(base = eblock->last; base != eblock->first; base = base->prev) {
16825 struct triple **expr;
16826 if (base->op == OP_PIECE) {
16827 base = MISC(base, 0);
16828 }
16829 if ((base == var) || (base == val)) {
16830 goto out;
16831 }
16832 expr = triple_lhs(state, base, 0);
16833 for(; expr; expr = triple_lhs(state, base, expr)) {
16834 if ((*expr) == val) {
16835 goto out;
16836 }
16837 }
16838 expr = triple_rhs(state, base, 0);
16839 for(; expr; expr = triple_rhs(state, base, expr)) {
16840 if ((*expr) == var) {
16841 goto out;
16842 }
16843 }
Eric Biederman530b5192003-07-01 10:05:30 +000016844 }
Eric Biederman90089602004-05-28 14:11:54 +000016845 out:
16846 if (triple_is_branch(state, base)) {
16847 internal_error(state, base,
16848 "Could not insert write to phi");
16849 }
16850 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016851 use_triple(val, move);
16852 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016853 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016854 }
Eric Biederman90089602004-05-28 14:11:54 +000016855 if (!writers && readers) {
16856 internal_error(state, var, "no value written to in use phi?");
16857 }
16858 /* If var is not used free it */
16859 if (!writers) {
16860 release_triple(state, MISC(var, 0));
16861 release_triple(state, var);
16862 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016863 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016864 release_triple(state, phi);
16865 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016866
Eric Biederman83b991a2003-10-11 06:20:25 +000016867 /* Walk all of the operations to find the adecls */
16868 for(var = first->next; var != first ; var = var->next) {
16869 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016870 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016871 continue;
16872 }
16873
16874 /* Walk through all of the rhs uses of var and
16875 * replace them with read of var.
16876 */
16877 for(use = var->use; use; use = use_next) {
16878 struct triple *read, *user;
16879 struct triple **slot;
16880 int zrhs, i, used;
16881 use_next = use->next;
16882 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016883
Eric Biederman83b991a2003-10-11 06:20:25 +000016884 /* Generate a read of var */
16885 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16886 use_triple(var, read);
16887
16888 /* Find the rhs uses and see if they need to be replaced */
16889 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016890 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016891 slot = &RHS(user, 0);
16892 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016893 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016894 slot[i] = read;
16895 used = 1;
16896 }
16897 }
16898 /* If we did use it cleanup the uses */
16899 if (used) {
16900 unuse_triple(var, user);
16901 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016902 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016903 /* If we didn't use it release the extra triple */
16904 else {
16905 release_triple(state, read);
16906 }
16907 }
16908 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016909}
16910
Eric Biederman5ade04a2003-10-22 04:03:46 +000016911#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016912 FILE *fp = state->dbgout; \
16913 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016914 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016915
Eric Biederman83b991a2003-10-11 06:20:25 +000016916static void rebuild_ssa_form(struct compile_state *state)
16917{
16918HI();
16919 transform_from_ssa_form(state);
16920HI();
Eric Biederman90089602004-05-28 14:11:54 +000016921 state->bb.first = state->first;
16922 free_basic_blocks(state, &state->bb);
16923 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016924HI();
16925 insert_phi_operations(state);
16926HI();
16927 rename_variables(state);
16928HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016929
Eric Biederman90089602004-05-28 14:11:54 +000016930 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016931HI();
16932 prune_unused_phis(state);
16933HI();
16934}
16935#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016936
Stefan Reinauer14e22772010-04-27 06:56:47 +000016937/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016938 * Register conflict resolution
16939 * =========================================================
16940 */
16941
16942static struct reg_info find_def_color(
16943 struct compile_state *state, struct triple *def)
16944{
16945 struct triple_set *set;
16946 struct reg_info info;
16947 info.reg = REG_UNSET;
16948 info.regcm = 0;
16949 if (!triple_is_def(state, def)) {
16950 return info;
16951 }
16952 info = arch_reg_lhs(state, def, 0);
16953 if (info.reg >= MAX_REGISTERS) {
16954 info.reg = REG_UNSET;
16955 }
16956 for(set = def->use; set; set = set->next) {
16957 struct reg_info tinfo;
16958 int i;
16959 i = find_rhs_use(state, set->member, def);
16960 if (i < 0) {
16961 continue;
16962 }
16963 tinfo = arch_reg_rhs(state, set->member, i);
16964 if (tinfo.reg >= MAX_REGISTERS) {
16965 tinfo.reg = REG_UNSET;
16966 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016967 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016968 (info.reg != REG_UNSET) &&
16969 (tinfo.reg != info.reg)) {
16970 internal_error(state, def, "register conflict");
16971 }
16972 if ((info.regcm & tinfo.regcm) == 0) {
16973 internal_error(state, def, "regcm conflict %x & %x == 0",
16974 info.regcm, tinfo.regcm);
16975 }
16976 if (info.reg == REG_UNSET) {
16977 info.reg = tinfo.reg;
16978 }
16979 info.regcm &= tinfo.regcm;
16980 }
16981 if (info.reg >= MAX_REGISTERS) {
16982 internal_error(state, def, "register out of range");
16983 }
16984 return info;
16985}
16986
16987static struct reg_info find_lhs_pre_color(
16988 struct compile_state *state, struct triple *ins, int index)
16989{
16990 struct reg_info info;
16991 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016992 zrhs = ins->rhs;
16993 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016994 if (!zlhs && triple_is_def(state, ins)) {
16995 zlhs = 1;
16996 }
16997 if (index >= zlhs) {
16998 internal_error(state, ins, "Bad lhs %d", index);
16999 }
17000 info = arch_reg_lhs(state, ins, index);
17001 for(i = 0; i < zrhs; i++) {
17002 struct reg_info rinfo;
17003 rinfo = arch_reg_rhs(state, ins, i);
17004 if ((info.reg == rinfo.reg) &&
17005 (rinfo.reg >= MAX_REGISTERS)) {
17006 struct reg_info tinfo;
17007 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17008 info.reg = tinfo.reg;
17009 info.regcm &= tinfo.regcm;
17010 break;
17011 }
17012 }
17013 if (info.reg >= MAX_REGISTERS) {
17014 info.reg = REG_UNSET;
17015 }
17016 return info;
17017}
17018
17019static struct reg_info find_rhs_post_color(
17020 struct compile_state *state, struct triple *ins, int index);
17021
17022static struct reg_info find_lhs_post_color(
17023 struct compile_state *state, struct triple *ins, int index)
17024{
17025 struct triple_set *set;
17026 struct reg_info info;
17027 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017028#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017029 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017030 ins, index);
17031#endif
17032 if ((index == 0) && triple_is_def(state, ins)) {
17033 lhs = ins;
17034 }
Eric Biederman90089602004-05-28 14:11:54 +000017035 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017036 lhs = LHS(ins, index);
17037 }
17038 else {
17039 internal_error(state, ins, "Bad lhs %d", index);
17040 lhs = 0;
17041 }
17042 info = arch_reg_lhs(state, ins, index);
17043 if (info.reg >= MAX_REGISTERS) {
17044 info.reg = REG_UNSET;
17045 }
17046 for(set = lhs->use; set; set = set->next) {
17047 struct reg_info rinfo;
17048 struct triple *user;
17049 int zrhs, i;
17050 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017051 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017052 for(i = 0; i < zrhs; i++) {
17053 if (RHS(user, i) != lhs) {
17054 continue;
17055 }
17056 rinfo = find_rhs_post_color(state, user, i);
17057 if ((info.reg != REG_UNSET) &&
17058 (rinfo.reg != REG_UNSET) &&
17059 (info.reg != rinfo.reg)) {
17060 internal_error(state, ins, "register conflict");
17061 }
17062 if ((info.regcm & rinfo.regcm) == 0) {
17063 internal_error(state, ins, "regcm conflict %x & %x == 0",
17064 info.regcm, rinfo.regcm);
17065 }
17066 if (info.reg == REG_UNSET) {
17067 info.reg = rinfo.reg;
17068 }
17069 info.regcm &= rinfo.regcm;
17070 }
17071 }
Eric Biederman530b5192003-07-01 10:05:30 +000017072#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017073 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017074 ins, index, info.reg, info.regcm);
17075#endif
17076 return info;
17077}
17078
17079static struct reg_info find_rhs_post_color(
17080 struct compile_state *state, struct triple *ins, int index)
17081{
17082 struct reg_info info, rinfo;
17083 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017084#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017085 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017086 ins, index);
17087#endif
17088 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017089 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017090 if (!zlhs && triple_is_def(state, ins)) {
17091 zlhs = 1;
17092 }
17093 info = rinfo;
17094 if (info.reg >= MAX_REGISTERS) {
17095 info.reg = REG_UNSET;
17096 }
17097 for(i = 0; i < zlhs; i++) {
17098 struct reg_info linfo;
17099 linfo = arch_reg_lhs(state, ins, i);
17100 if ((linfo.reg == rinfo.reg) &&
17101 (linfo.reg >= MAX_REGISTERS)) {
17102 struct reg_info tinfo;
17103 tinfo = find_lhs_post_color(state, ins, i);
17104 if (tinfo.reg >= MAX_REGISTERS) {
17105 tinfo.reg = REG_UNSET;
17106 }
Eric Biederman530b5192003-07-01 10:05:30 +000017107 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017108 info.regcm &= tinfo.regcm;
17109 if (info.reg != REG_UNSET) {
17110 internal_error(state, ins, "register conflict");
17111 }
17112 if (info.regcm == 0) {
17113 internal_error(state, ins, "regcm conflict");
17114 }
17115 info.reg = tinfo.reg;
17116 }
17117 }
Eric Biederman530b5192003-07-01 10:05:30 +000017118#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017119 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017120 ins, index, info.reg, info.regcm);
17121#endif
17122 return info;
17123}
17124
17125static struct reg_info find_lhs_color(
17126 struct compile_state *state, struct triple *ins, int index)
17127{
17128 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017129#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017130 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017131 ins, index);
17132#endif
17133 pre = find_lhs_pre_color(state, ins, index);
17134 post = find_lhs_post_color(state, ins, index);
17135 if ((pre.reg != post.reg) &&
17136 (pre.reg != REG_UNSET) &&
17137 (post.reg != REG_UNSET)) {
17138 internal_error(state, ins, "register conflict");
17139 }
17140 info.regcm = pre.regcm & post.regcm;
17141 info.reg = pre.reg;
17142 if (info.reg == REG_UNSET) {
17143 info.reg = post.reg;
17144 }
Eric Biederman530b5192003-07-01 10:05:30 +000017145#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017146 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017147 ins, index, info.reg, info.regcm,
17148 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017149#endif
17150 return info;
17151}
17152
17153static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17154{
17155 struct triple_set *entry, *next;
17156 struct triple *out;
17157 struct reg_info info, rinfo;
17158
17159 info = arch_reg_lhs(state, ins, 0);
17160 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17161 use_triple(RHS(out, 0), out);
17162 /* Get the users of ins to use out instead */
17163 for(entry = ins->use; entry; entry = next) {
17164 int i;
17165 next = entry->next;
17166 if (entry->member == out) {
17167 continue;
17168 }
17169 i = find_rhs_use(state, entry->member, ins);
17170 if (i < 0) {
17171 continue;
17172 }
17173 rinfo = arch_reg_rhs(state, entry->member, i);
17174 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17175 continue;
17176 }
17177 replace_rhs_use(state, ins, out, entry->member);
17178 }
17179 transform_to_arch_instruction(state, out);
17180 return out;
17181}
17182
Eric Biedermand1ea5392003-06-28 06:49:45 +000017183static struct triple *typed_pre_copy(
17184 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017185{
17186 /* Carefully insert enough operations so that I can
17187 * enter any operation with a GPR32.
17188 */
17189 struct triple *in;
17190 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017191 unsigned classes;
17192 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017193 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017194 if (ins->op == OP_PHI) {
17195 internal_error(state, ins, "pre_copy on a phi?");
17196 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017197 classes = arch_type_to_regcm(state, type);
17198 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017199 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017200 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017201 FILE *fp = state->errout;
17202 fprintf(fp, "src_type: ");
17203 name_of(fp, ins->type);
17204 fprintf(fp, "\ndst_type: ");
17205 name_of(fp, type);
17206 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017207 internal_error(state, ins, "pre_copy with no register classes");
17208 }
Eric Biederman90089602004-05-28 14:11:54 +000017209 op = OP_COPY;
17210 if (!equiv_types(type, (*expr)->type)) {
17211 op = OP_CONVERT;
17212 }
17213 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017214 unuse_triple(*expr, ins);
17215 *expr = in;
17216 use_triple(RHS(in, 0), in);
17217 use_triple(in, ins);
17218 transform_to_arch_instruction(state, in);
17219 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017220
Eric Biedermand1ea5392003-06-28 06:49:45 +000017221}
17222static struct triple *pre_copy(
17223 struct compile_state *state, struct triple *ins, int index)
17224{
17225 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017226}
17227
17228
Eric Biedermanb138ac82003-04-22 18:44:01 +000017229static void insert_copies_to_phi(struct compile_state *state)
17230{
17231 /* To get out of ssa form we insert moves on the incoming
17232 * edges to blocks containting phi functions.
17233 */
17234 struct triple *first;
17235 struct triple *phi;
17236
17237 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017238 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017239 for(phi = first->next; phi != first ; phi = phi->next) {
17240 struct block_set *set;
17241 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017242 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017243 int edge;
17244 if (phi->op != OP_PHI) {
17245 continue;
17246 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017247 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017248 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017249 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017250 /* Phi's that feed into mandatory live range joins
17251 * cause nasty complications. Insert a copy of
17252 * the phi value so I never have to deal with
17253 * that in the rest of the code.
17254 */
17255 copy = post_copy(state, phi);
17256 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017257 /* Walk all of the incoming edges/blocks and insert moves.
17258 */
17259 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17260 struct block *eblock;
17261 struct triple *move;
17262 struct triple *val;
17263 struct triple *ptr;
17264 eblock = set->member;
17265 val = slot[edge];
17266
17267 if (val == phi) {
17268 continue;
17269 }
17270
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017271 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017272 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017273 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017274 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017275 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017276 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017277
Eric Biedermanb138ac82003-04-22 18:44:01 +000017278 slot[edge] = move;
17279 unuse_triple(val, phi);
17280 use_triple(move, phi);
17281
Eric Biederman66fe2222003-07-04 15:14:04 +000017282 /* Walk up the dominator tree until I have found the appropriate block */
17283 while(eblock && !tdominates(state, val, eblock->last)) {
17284 eblock = eblock->idom;
17285 }
17286 if (!eblock) {
17287 internal_error(state, phi, "Cannot find block dominated by %p",
17288 val);
17289 }
17290
Eric Biedermanb138ac82003-04-22 18:44:01 +000017291 /* Walk through the block backwards to find
17292 * an appropriate location for the OP_COPY.
17293 */
17294 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17295 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017296 if (ptr->op == OP_PIECE) {
17297 ptr = MISC(ptr, 0);
17298 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017299 if ((ptr == phi) || (ptr == val)) {
17300 goto out;
17301 }
Eric Biederman90089602004-05-28 14:11:54 +000017302 expr = triple_lhs(state, ptr, 0);
17303 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17304 if ((*expr) == val) {
17305 goto out;
17306 }
17307 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017308 expr = triple_rhs(state, ptr, 0);
17309 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17310 if ((*expr) == phi) {
17311 goto out;
17312 }
17313 }
17314 }
17315 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017316 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017317 internal_error(state, ptr,
17318 "Could not insert write to phi");
17319 }
Eric Biederman90089602004-05-28 14:11:54 +000017320 insert_triple(state, after_lhs(state, ptr), move);
17321 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017322 eblock->last = move;
17323 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017324 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017325 }
17326 }
Eric Biederman90089602004-05-28 14:11:54 +000017327 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017328}
17329
Eric Biederman90089602004-05-28 14:11:54 +000017330struct triple_reg_set;
17331struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017332
Eric Biedermanb138ac82003-04-22 18:44:01 +000017333
Stefan Reinauer14e22772010-04-27 06:56:47 +000017334static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335 struct triple *member, struct triple *new_member)
17336{
17337 struct triple_reg_set **ptr, *new;
17338 if (!member)
17339 return 0;
17340 ptr = head;
17341 while(*ptr) {
17342 if ((*ptr)->member == member) {
17343 return 0;
17344 }
17345 ptr = &(*ptr)->next;
17346 }
17347 new = xcmalloc(sizeof(*new), "triple_set");
17348 new->member = member;
17349 new->new = new_member;
17350 new->next = *head;
17351 *head = new;
17352 return 1;
17353}
17354
17355static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17356{
17357 struct triple_reg_set *entry, **ptr;
17358 ptr = head;
17359 while(*ptr) {
17360 entry = *ptr;
17361 if (entry->member == member) {
17362 *ptr = entry->next;
17363 xfree(entry);
17364 return;
17365 }
17366 else {
17367 ptr = &entry->next;
17368 }
17369 }
17370}
17371
17372static int in_triple(struct reg_block *rb, struct triple *in)
17373{
17374 return do_triple_set(&rb->in, in, 0);
17375}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017376
17377#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017378static void unin_triple(struct reg_block *rb, struct triple *unin)
17379{
17380 do_triple_unset(&rb->in, unin);
17381}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017382#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017383
17384static int out_triple(struct reg_block *rb, struct triple *out)
17385{
17386 return do_triple_set(&rb->out, out, 0);
17387}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017388#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017389static void unout_triple(struct reg_block *rb, struct triple *unout)
17390{
17391 do_triple_unset(&rb->out, unout);
17392}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017393#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017394
17395static int initialize_regblock(struct reg_block *blocks,
17396 struct block *block, int vertex)
17397{
17398 struct block_set *user;
17399 if (!block || (blocks[block->vertex].block == block)) {
17400 return vertex;
17401 }
17402 vertex += 1;
17403 /* Renumber the blocks in a convinient fashion */
17404 block->vertex = vertex;
17405 blocks[vertex].block = block;
17406 blocks[vertex].vertex = vertex;
17407 for(user = block->use; user; user = user->next) {
17408 vertex = initialize_regblock(blocks, user->member, vertex);
17409 }
17410 return vertex;
17411}
17412
Eric Biederman90089602004-05-28 14:11:54 +000017413static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17414{
17415/* Part to piece is a best attempt and it cannot be correct all by
17416 * itself. If various values are read as different sizes in different
17417 * parts of the code this function cannot work. Or rather it cannot
17418 * work in conjunction with compute_variable_liftimes. As the
17419 * analysis will get confused.
17420 */
17421 struct triple *base;
17422 unsigned reg;
17423 if (!is_lvalue(state, ins)) {
17424 return ins;
17425 }
17426 base = 0;
17427 reg = 0;
17428 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17429 base = MISC(ins, 0);
17430 switch(ins->op) {
17431 case OP_INDEX:
17432 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17433 break;
17434 case OP_DOT:
17435 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17436 break;
17437 default:
17438 internal_error(state, ins, "unhandled part");
17439 break;
17440 }
17441 ins = base;
17442 }
17443 if (base) {
17444 if (reg > base->lhs) {
17445 internal_error(state, base, "part out of range?");
17446 }
17447 ins = LHS(base, reg);
17448 }
17449 return ins;
17450}
17451
Stefan Reinauer14e22772010-04-27 06:56:47 +000017452static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017453 struct triple *ins, struct triple *other)
17454{
17455 if (ins == other) {
17456 return 1;
17457 }
17458 if (ins->op == OP_WRITE) {
17459 ins = part_to_piece(state, MISC(ins, 0));
17460 }
17461 return ins == other;
17462}
17463
Eric Biedermanb138ac82003-04-22 18:44:01 +000017464static int phi_in(struct compile_state *state, struct reg_block *blocks,
17465 struct reg_block *rb, struct block *suc)
17466{
17467 /* Read the conditional input set of a successor block
17468 * (i.e. the input to the phi nodes) and place it in the
17469 * current blocks output set.
17470 */
17471 struct block_set *set;
17472 struct triple *ptr;
17473 int edge;
17474 int done, change;
17475 change = 0;
17476 /* Find the edge I am coming in on */
17477 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17478 if (set->member == rb->block) {
17479 break;
17480 }
17481 }
17482 if (!set) {
17483 internal_error(state, 0, "Not coming on a control edge?");
17484 }
17485 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17486 struct triple **slot, *expr, *ptr2;
17487 int out_change, done2;
17488 done = (ptr == suc->last);
17489 if (ptr->op != OP_PHI) {
17490 continue;
17491 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017492 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017493 expr = slot[edge];
17494 out_change = out_triple(rb, expr);
17495 if (!out_change) {
17496 continue;
17497 }
17498 /* If we don't define the variable also plast it
17499 * in the current blocks input set.
17500 */
17501 ptr2 = rb->block->first;
17502 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017503 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017504 break;
17505 }
17506 done2 = (ptr2 == rb->block->last);
17507 }
17508 if (!done2) {
17509 continue;
17510 }
17511 change |= in_triple(rb, expr);
17512 }
17513 return change;
17514}
17515
17516static int reg_in(struct compile_state *state, struct reg_block *blocks,
17517 struct reg_block *rb, struct block *suc)
17518{
17519 struct triple_reg_set *in_set;
17520 int change;
17521 change = 0;
17522 /* Read the input set of a successor block
17523 * and place it in the current blocks output set.
17524 */
17525 in_set = blocks[suc->vertex].in;
17526 for(; in_set; in_set = in_set->next) {
17527 int out_change, done;
17528 struct triple *first, *last, *ptr;
17529 out_change = out_triple(rb, in_set->member);
17530 if (!out_change) {
17531 continue;
17532 }
17533 /* If we don't define the variable also place it
17534 * in the current blocks input set.
17535 */
17536 first = rb->block->first;
17537 last = rb->block->last;
17538 done = 0;
17539 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017540 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017541 break;
17542 }
17543 done = (ptr == last);
17544 }
17545 if (!done) {
17546 continue;
17547 }
17548 change |= in_triple(rb, in_set->member);
17549 }
17550 change |= phi_in(state, blocks, rb, suc);
17551 return change;
17552}
17553
Eric Biedermanb138ac82003-04-22 18:44:01 +000017554static int use_in(struct compile_state *state, struct reg_block *rb)
17555{
17556 /* Find the variables we use but don't define and add
17557 * it to the current blocks input set.
17558 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017559#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017560#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017561#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017562 struct block *block;
17563 struct triple *ptr;
17564 int done;
17565 int change;
17566 block = rb->block;
17567 change = 0;
17568 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17569 struct triple **expr;
17570 done = (ptr == block->first);
17571 /* The variable a phi function uses depends on the
17572 * control flow, and is handled in phi_in, not
17573 * here.
17574 */
17575 if (ptr->op == OP_PHI) {
17576 continue;
17577 }
17578 expr = triple_rhs(state, ptr, 0);
17579 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17580 struct triple *rhs, *test;
17581 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017582 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017583 if (!rhs) {
17584 continue;
17585 }
Eric Biederman90089602004-05-28 14:11:54 +000017586
17587 /* See if rhs is defined in this block.
17588 * A write counts as a definition.
17589 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017590 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17591 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017592 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017593 rhs = 0;
17594 break;
17595 }
17596 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017597 /* If I still have a valid rhs add it to in */
17598 change |= in_triple(rb, rhs);
17599 }
17600 }
17601 return change;
17602}
17603
17604static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017605 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017606{
17607 struct reg_block *blocks;
17608 int change;
17609 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017610 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17611 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017612 do {
17613 int i;
17614 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017615 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017616 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017617 struct reg_block *rb;
17618 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017619 /* Add the all successor's input set to in */
17620 for(edge = rb->block->edges; edge; edge = edge->next) {
17621 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017622 }
17623 /* Add use to in... */
17624 change |= use_in(state, rb);
17625 }
17626 } while(change);
17627 return blocks;
17628}
17629
Stefan Reinauer14e22772010-04-27 06:56:47 +000017630static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017631 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017632{
17633 int i;
17634 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017635 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017636 struct triple_reg_set *entry, *next;
17637 struct reg_block *rb;
17638 rb = &blocks[i];
17639 for(entry = rb->in; entry ; entry = next) {
17640 next = entry->next;
17641 do_triple_unset(&rb->in, entry->member);
17642 }
17643 for(entry = rb->out; entry; entry = next) {
17644 next = entry->next;
17645 do_triple_unset(&rb->out, entry->member);
17646 }
17647 }
17648 xfree(blocks);
17649
17650}
17651
Eric Biedermanf96a8102003-06-16 16:57:34 +000017652typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017653 struct compile_state *state,
17654 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017655 struct reg_block *rb, struct triple *ins, void *arg);
17656
17657static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017658 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017659 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017660{
17661 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017662
Eric Biederman90089602004-05-28 14:11:54 +000017663 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017664 struct triple_reg_set *live;
17665 struct triple_reg_set *entry, *next;
17666 struct triple *ptr, *prev;
17667 struct reg_block *rb;
17668 struct block *block;
17669 int done;
17670
17671 /* Get the blocks */
17672 rb = &blocks[i];
17673 block = rb->block;
17674
17675 /* Copy out into live */
17676 live = 0;
17677 for(entry = rb->out; entry; entry = next) {
17678 next = entry->next;
17679 do_triple_set(&live, entry->member, entry->new);
17680 }
17681 /* Walk through the basic block calculating live */
17682 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017683 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017684
17685 prev = ptr->prev;
17686 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017687
17688 /* Ensure the current definition is in live */
17689 if (triple_is_def(state, ptr)) {
17690 do_triple_set(&live, ptr, 0);
17691 }
17692
17693 /* Inform the callback function of what is
17694 * going on.
17695 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017696 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017697
Eric Biedermanb138ac82003-04-22 18:44:01 +000017698 /* Remove the current definition from live */
17699 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017700
Eric Biedermanb138ac82003-04-22 18:44:01 +000017701 /* Add the current uses to live.
17702 *
17703 * It is safe to skip phi functions because they do
17704 * not have any block local uses, and the block
17705 * output sets already properly account for what
17706 * control flow depedent uses phi functions do have.
17707 */
17708 if (ptr->op == OP_PHI) {
17709 continue;
17710 }
17711 expr = triple_rhs(state, ptr, 0);
17712 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17713 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017714 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017715 continue;
17716 }
17717 do_triple_set(&live, *expr, 0);
17718 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017719 }
17720 /* Free live */
17721 for(entry = live; entry; entry = next) {
17722 next = entry->next;
17723 do_triple_unset(&live, entry->member);
17724 }
17725 }
17726}
17727
Eric Biederman90089602004-05-28 14:11:54 +000017728struct print_live_variable_info {
17729 struct reg_block *rb;
17730 FILE *fp;
17731};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017732#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017733static void print_live_variables_block(
17734 struct compile_state *state, struct block *block, void *arg)
17735
17736{
17737 struct print_live_variable_info *info = arg;
17738 struct block_set *edge;
17739 FILE *fp = info->fp;
17740 struct reg_block *rb;
17741 struct triple *ptr;
17742 int phi_present;
17743 int done;
17744 rb = &info->rb[block->vertex];
17745
17746 fprintf(fp, "\nblock: %p (%d),",
17747 block, block->vertex);
17748 for(edge = block->edges; edge; edge = edge->next) {
17749 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017750 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017751 edge->member && edge->member->use?edge->member->use->member : 0);
17752 }
17753 fprintf(fp, "\n");
17754 if (rb->in) {
17755 struct triple_reg_set *in_set;
17756 fprintf(fp, " in:");
17757 for(in_set = rb->in; in_set; in_set = in_set->next) {
17758 fprintf(fp, " %-10p", in_set->member);
17759 }
17760 fprintf(fp, "\n");
17761 }
17762 phi_present = 0;
17763 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17764 done = (ptr == block->last);
17765 if (ptr->op == OP_PHI) {
17766 phi_present = 1;
17767 break;
17768 }
17769 }
17770 if (phi_present) {
17771 int edge;
17772 for(edge = 0; edge < block->users; edge++) {
17773 fprintf(fp, " in(%d):", edge);
17774 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17775 struct triple **slot;
17776 done = (ptr == block->last);
17777 if (ptr->op != OP_PHI) {
17778 continue;
17779 }
17780 slot = &RHS(ptr, 0);
17781 fprintf(fp, " %-10p", slot[edge]);
17782 }
17783 fprintf(fp, "\n");
17784 }
17785 }
17786 if (block->first->op == OP_LABEL) {
17787 fprintf(fp, "%p:\n", block->first);
17788 }
17789 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17790 done = (ptr == block->last);
17791 display_triple(fp, ptr);
17792 }
17793 if (rb->out) {
17794 struct triple_reg_set *out_set;
17795 fprintf(fp, " out:");
17796 for(out_set = rb->out; out_set; out_set = out_set->next) {
17797 fprintf(fp, " %-10p", out_set->member);
17798 }
17799 fprintf(fp, "\n");
17800 }
17801 fprintf(fp, "\n");
17802}
17803
Stefan Reinauer14e22772010-04-27 06:56:47 +000017804static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017805 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17806{
17807 struct print_live_variable_info info;
17808 info.rb = rb;
17809 info.fp = fp;
17810 fprintf(fp, "\nlive variables by block\n");
17811 walk_blocks(state, bb, print_live_variables_block, &info);
17812
17813}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017814#endif
Eric Biederman90089602004-05-28 14:11:54 +000017815
Eric Biedermanb138ac82003-04-22 18:44:01 +000017816static int count_triples(struct compile_state *state)
17817{
17818 struct triple *first, *ins;
17819 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017820 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017821 ins = first;
17822 do {
17823 triples++;
17824 ins = ins->next;
17825 } while (ins != first);
17826 return triples;
17827}
Eric Biederman66fe2222003-07-04 15:14:04 +000017828
17829
Eric Biedermanb138ac82003-04-22 18:44:01 +000017830struct dead_triple {
17831 struct triple *triple;
17832 struct dead_triple *work_next;
17833 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017834 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017835 int flags;
17836#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017837#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017838};
17839
Stefan Reinauer14e22772010-04-27 06:56:47 +000017840static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017841 struct dead_triple *dtriple)
17842{
17843 struct triple *first, *ins;
17844 struct dead_triple *dt;
17845 FILE *fp;
17846 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17847 return;
17848 }
17849 fp = state->dbgout;
17850 fprintf(fp, "--------------- dtriples ---------------\n");
17851 first = state->first;
17852 ins = first;
17853 do {
17854 dt = &dtriple[ins->id];
17855 if ((ins->op == OP_LABEL) && (ins->use)) {
17856 fprintf(fp, "\n%p:\n", ins);
17857 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017858 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017859 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17860 display_triple(fp, ins);
17861 if (triple_is_branch(state, ins)) {
17862 fprintf(fp, "\n");
17863 }
17864 ins = ins->next;
17865 } while(ins != first);
17866 fprintf(fp, "\n");
17867}
17868
Eric Biedermanb138ac82003-04-22 18:44:01 +000017869
17870static void awaken(
17871 struct compile_state *state,
17872 struct dead_triple *dtriple, struct triple **expr,
17873 struct dead_triple ***work_list_tail)
17874{
17875 struct triple *triple;
17876 struct dead_triple *dt;
17877 if (!expr) {
17878 return;
17879 }
17880 triple = *expr;
17881 if (!triple) {
17882 return;
17883 }
17884 if (triple->id <= 0) {
17885 internal_error(state, triple, "bad triple id: %d",
17886 triple->id);
17887 }
17888 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017889 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017890 return;
17891 }
17892 dt = &dtriple[triple->id];
17893 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17894 dt->flags |= TRIPLE_FLAG_ALIVE;
17895 if (!dt->work_next) {
17896 **work_list_tail = dt;
17897 *work_list_tail = &dt->work_next;
17898 }
17899 }
17900}
17901
17902static void eliminate_inefectual_code(struct compile_state *state)
17903{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017904 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17905 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017906 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017907
Eric Biederman5ade04a2003-10-22 04:03:46 +000017908 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17909 return;
17910 }
17911
Eric Biedermanb138ac82003-04-22 18:44:01 +000017912 /* Setup the work list */
17913 work_list = 0;
17914 work_list_tail = &work_list;
17915
Eric Biederman83b991a2003-10-11 06:20:25 +000017916 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017917
17918 /* Count how many triples I have */
17919 triples = count_triples(state);
17920
17921 /* Now put then in an array and mark all of the triples dead */
17922 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017923
Eric Biedermanb138ac82003-04-22 18:44:01 +000017924 ins = first;
17925 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017926 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017927 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017928 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017929 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017930 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017931 ins->id = i;
17932 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017933 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017934 awaken(state, dtriple, &ins, &work_list_tail);
17935 }
17936 i++;
17937 ins = ins->next;
17938 } while(ins != first);
17939 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017940 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017941 struct dead_triple *dt;
17942 struct block_set *user;
17943 struct triple **expr;
17944 dt = work_list;
17945 work_list = dt->work_next;
17946 if (!work_list) {
17947 work_list_tail = &work_list;
17948 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017949 /* Make certain the block the current instruction is in lives */
17950 block = block_of_triple(state, dt->triple);
17951 awaken(state, dtriple, &block->first, &work_list_tail);
17952 if (triple_is_branch(state, block->last)) {
17953 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017954 } else {
17955 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017956 }
17957
Eric Biedermanb138ac82003-04-22 18:44:01 +000017958 /* Wake up the data depencencies of this triple */
17959 expr = 0;
17960 do {
17961 expr = triple_rhs(state, dt->triple, expr);
17962 awaken(state, dtriple, expr, &work_list_tail);
17963 } while(expr);
17964 do {
17965 expr = triple_lhs(state, dt->triple, expr);
17966 awaken(state, dtriple, expr, &work_list_tail);
17967 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017968 do {
17969 expr = triple_misc(state, dt->triple, expr);
17970 awaken(state, dtriple, expr, &work_list_tail);
17971 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017972 /* Wake up the forward control dependencies */
17973 do {
17974 expr = triple_targ(state, dt->triple, expr);
17975 awaken(state, dtriple, expr, &work_list_tail);
17976 } while(expr);
17977 /* Wake up the reverse control dependencies of this triple */
17978 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017979 struct triple *last;
17980 last = user->member->last;
17981 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017982#if DEBUG_ROMCC_WARNINGS
17983#warning "Should we bring the awakening noops back?"
17984#endif
17985 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017986 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017987 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017988 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017989 }
17990 }
Eric Biederman90089602004-05-28 14:11:54 +000017991 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017992 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000017993 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000017994 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17995 internal_error(state, dt->triple, "noop effective?");
17996 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017997 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017998 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017999 release_triple(state, dt->triple);
18000 }
18001 }
18002 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018003
18004 rebuild_ssa_form(state);
18005
Eric Biederman90089602004-05-28 14:11:54 +000018006 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018007}
18008
18009
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018010static void insert_mandatory_copies(struct compile_state *state)
18011{
18012 struct triple *ins, *first;
18013
18014 /* The object is with a minimum of inserted copies,
18015 * to resolve in fundamental register conflicts between
18016 * register value producers and consumers.
18017 * Theoretically we may be greater than minimal when we
18018 * are inserting copies before instructions but that
18019 * case should be rare.
18020 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018021 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018022 ins = first;
18023 do {
18024 struct triple_set *entry, *next;
18025 struct triple *tmp;
18026 struct reg_info info;
18027 unsigned reg, regcm;
18028 int do_post_copy, do_pre_copy;
18029 tmp = 0;
18030 if (!triple_is_def(state, ins)) {
18031 goto next;
18032 }
18033 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018034 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018035 if (info.reg >= MAX_REGISTERS) {
18036 info.reg = REG_UNSET;
18037 }
Eric Biederman90089602004-05-28 14:11:54 +000018038
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018039 reg = REG_UNSET;
18040 regcm = arch_type_to_regcm(state, ins->type);
Patrick Georgi37935572017-01-02 21:39:43 +010018041 do_pre_copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018042
18043 /* Walk through the uses of ins and check for conflicts */
18044 for(entry = ins->use; entry; entry = next) {
18045 struct reg_info rinfo;
18046 int i;
18047 next = entry->next;
18048 i = find_rhs_use(state, entry->member, ins);
18049 if (i < 0) {
18050 continue;
18051 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018052
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018053 /* Find the users color requirements */
18054 rinfo = arch_reg_rhs(state, entry->member, i);
18055 if (rinfo.reg >= MAX_REGISTERS) {
18056 rinfo.reg = REG_UNSET;
18057 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018058
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018059 /* See if I need a pre_copy */
18060 if (rinfo.reg != REG_UNSET) {
18061 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18062 do_pre_copy = 1;
18063 }
18064 reg = rinfo.reg;
18065 }
18066 regcm &= rinfo.regcm;
18067 regcm = arch_regcm_normalize(state, regcm);
18068 if (regcm == 0) {
18069 do_pre_copy = 1;
18070 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018071 /* Always use pre_copies for constants.
18072 * They do not take up any registers until a
18073 * copy places them in one.
18074 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018075 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018076 (rinfo.reg != REG_UNNEEDED)) {
18077 do_pre_copy = 1;
18078 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018079 }
18080 do_post_copy =
18081 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018082 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018083 (reg != REG_UNSET) &&
18084 (info.reg != reg)) ||
18085 ((info.regcm & regcm) == 0));
18086
18087 reg = info.reg;
18088 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018089 /* 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 +000018090 for(entry = ins->use; entry; entry = next) {
18091 struct reg_info rinfo;
18092 int i;
18093 next = entry->next;
18094 i = find_rhs_use(state, entry->member, ins);
18095 if (i < 0) {
18096 continue;
18097 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018098
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018099 /* Find the users color requirements */
18100 rinfo = arch_reg_rhs(state, entry->member, i);
18101 if (rinfo.reg >= MAX_REGISTERS) {
18102 rinfo.reg = REG_UNSET;
18103 }
18104
18105 /* Now see if it is time to do the pre_copy */
18106 if (rinfo.reg != REG_UNSET) {
18107 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18108 ((regcm & rinfo.regcm) == 0) ||
18109 /* Don't let a mandatory coalesce sneak
18110 * into a operation that is marked to prevent
18111 * coalescing.
18112 */
18113 ((reg != REG_UNNEEDED) &&
18114 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18115 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18116 ) {
18117 if (do_pre_copy) {
18118 struct triple *user;
18119 user = entry->member;
18120 if (RHS(user, i) != ins) {
18121 internal_error(state, user, "bad rhs");
18122 }
18123 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018124 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018125 continue;
18126 } else {
18127 do_post_copy = 1;
18128 }
18129 }
18130 reg = rinfo.reg;
18131 }
18132 if ((regcm & rinfo.regcm) == 0) {
18133 if (do_pre_copy) {
18134 struct triple *user;
18135 user = entry->member;
18136 if (RHS(user, i) != ins) {
18137 internal_error(state, user, "bad rhs");
18138 }
18139 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018140 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018141 continue;
18142 } else {
18143 do_post_copy = 1;
18144 }
18145 }
18146 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018147
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018148 }
18149 if (do_post_copy) {
18150 struct reg_info pre, post;
18151 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018152 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018153 pre = arch_reg_lhs(state, ins, 0);
18154 post = arch_reg_lhs(state, tmp, 0);
18155 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18156 internal_error(state, tmp, "useless copy");
18157 }
18158 }
18159 next:
18160 ins = ins->next;
18161 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018162
Eric Biederman90089602004-05-28 14:11:54 +000018163 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018164}
18165
18166
Eric Biedermanb138ac82003-04-22 18:44:01 +000018167struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018168struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018169struct live_range {
18170 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018171 struct live_range_def *defs;
18172/* Note. The list pointed to by defs is kept in order.
18173 * That is baring splits in the flow control
18174 * defs dominates defs->next wich dominates defs->next->next
18175 * etc.
18176 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018177 unsigned color;
18178 unsigned classes;
18179 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018180 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018181 struct live_range *group_next, **group_prev;
18182};
18183
18184struct live_range_edge {
18185 struct live_range_edge *next;
18186 struct live_range *node;
18187};
18188
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018189struct live_range_def {
18190 struct live_range_def *next;
18191 struct live_range_def *prev;
18192 struct live_range *lr;
18193 struct triple *def;
18194 unsigned orig_id;
18195};
18196
Eric Biedermanb138ac82003-04-22 18:44:01 +000018197#define LRE_HASH_SIZE 2048
18198struct lre_hash {
18199 struct lre_hash *next;
18200 struct live_range *left;
18201 struct live_range *right;
18202};
18203
18204
18205struct reg_state {
18206 struct lre_hash *hash[LRE_HASH_SIZE];
18207 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018208 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018209 struct live_range *lr;
18210 struct live_range *low, **low_tail;
18211 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018212 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018213 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018214 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018215};
18216
18217
Eric Biedermand1ea5392003-06-28 06:49:45 +000018218struct print_interference_block_info {
18219 struct reg_state *rstate;
18220 FILE *fp;
18221 int need_edges;
18222};
18223static void print_interference_block(
18224 struct compile_state *state, struct block *block, void *arg)
18225
18226{
18227 struct print_interference_block_info *info = arg;
18228 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018229 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018230 FILE *fp = info->fp;
18231 struct reg_block *rb;
18232 struct triple *ptr;
18233 int phi_present;
18234 int done;
18235 rb = &rstate->blocks[block->vertex];
18236
Eric Biederman5ade04a2003-10-22 04:03:46 +000018237 fprintf(fp, "\nblock: %p (%d),",
18238 block, block->vertex);
18239 for(edge = block->edges; edge; edge = edge->next) {
18240 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018241 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018242 edge->member && edge->member->use?edge->member->use->member : 0);
18243 }
18244 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018245 if (rb->in) {
18246 struct triple_reg_set *in_set;
18247 fprintf(fp, " in:");
18248 for(in_set = rb->in; in_set; in_set = in_set->next) {
18249 fprintf(fp, " %-10p", in_set->member);
18250 }
18251 fprintf(fp, "\n");
18252 }
18253 phi_present = 0;
18254 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18255 done = (ptr == block->last);
18256 if (ptr->op == OP_PHI) {
18257 phi_present = 1;
18258 break;
18259 }
18260 }
18261 if (phi_present) {
18262 int edge;
18263 for(edge = 0; edge < block->users; edge++) {
18264 fprintf(fp, " in(%d):", edge);
18265 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18266 struct triple **slot;
18267 done = (ptr == block->last);
18268 if (ptr->op != OP_PHI) {
18269 continue;
18270 }
18271 slot = &RHS(ptr, 0);
18272 fprintf(fp, " %-10p", slot[edge]);
18273 }
18274 fprintf(fp, "\n");
18275 }
18276 }
18277 if (block->first->op == OP_LABEL) {
18278 fprintf(fp, "%p:\n", block->first);
18279 }
18280 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018281 struct live_range *lr;
18282 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018283 done = (ptr == block->last);
18284 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018285
Eric Biedermand1ea5392003-06-28 06:49:45 +000018286 id = ptr->id;
18287 ptr->id = rstate->lrd[id].orig_id;
18288 SET_REG(ptr->id, lr->color);
18289 display_triple(fp, ptr);
18290 ptr->id = id;
18291
18292 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18293 internal_error(state, ptr, "lr has no defs!");
18294 }
18295 if (info->need_edges) {
18296 if (lr->defs) {
18297 struct live_range_def *lrd;
18298 fprintf(fp, " range:");
18299 lrd = lr->defs;
18300 do {
18301 fprintf(fp, " %-10p", lrd->def);
18302 lrd = lrd->next;
18303 } while(lrd != lr->defs);
18304 fprintf(fp, "\n");
18305 }
18306 if (lr->edges > 0) {
18307 struct live_range_edge *edge;
18308 fprintf(fp, " edges:");
18309 for(edge = lr->edges; edge; edge = edge->next) {
18310 struct live_range_def *lrd;
18311 lrd = edge->node->defs;
18312 do {
18313 fprintf(fp, " %-10p", lrd->def);
18314 lrd = lrd->next;
18315 } while(lrd != edge->node->defs);
18316 fprintf(fp, "|");
18317 }
18318 fprintf(fp, "\n");
18319 }
18320 }
18321 /* Do a bunch of sanity checks */
18322 valid_ins(state, ptr);
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018323 if (ptr->id > rstate->defs) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018324 internal_error(state, ptr, "Invalid triple id: %d",
18325 ptr->id);
18326 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018327 }
18328 if (rb->out) {
18329 struct triple_reg_set *out_set;
18330 fprintf(fp, " out:");
18331 for(out_set = rb->out; out_set; out_set = out_set->next) {
18332 fprintf(fp, " %-10p", out_set->member);
18333 }
18334 fprintf(fp, "\n");
18335 }
18336 fprintf(fp, "\n");
18337}
18338
18339static void print_interference_blocks(
18340 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18341{
18342 struct print_interference_block_info info;
18343 info.rstate = rstate;
18344 info.fp = fp;
18345 info.need_edges = need_edges;
18346 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018347 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018348
18349}
18350
Eric Biedermanb138ac82003-04-22 18:44:01 +000018351static unsigned regc_max_size(struct compile_state *state, int classes)
18352{
18353 unsigned max_size;
18354 int i;
18355 max_size = 0;
18356 for(i = 0; i < MAX_REGC; i++) {
18357 if (classes & (1 << i)) {
18358 unsigned size;
18359 size = arch_regc_size(state, i);
18360 if (size > max_size) {
18361 max_size = size;
18362 }
18363 }
18364 }
18365 return max_size;
18366}
18367
18368static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18369{
18370 unsigned equivs[MAX_REG_EQUIVS];
18371 int i;
18372 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18373 internal_error(state, 0, "invalid register");
18374 }
18375 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18376 internal_error(state, 0, "invalid register");
18377 }
18378 arch_reg_equivs(state, equivs, reg1);
18379 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18380 if (equivs[i] == reg2) {
18381 return 1;
18382 }
18383 }
18384 return 0;
18385}
18386
18387static void reg_fill_used(struct compile_state *state, char *used, int reg)
18388{
18389 unsigned equivs[MAX_REG_EQUIVS];
18390 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018391 if (reg == REG_UNNEEDED) {
18392 return;
18393 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018394 arch_reg_equivs(state, equivs, reg);
18395 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18396 used[equivs[i]] = 1;
18397 }
18398 return;
18399}
18400
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018401static void reg_inc_used(struct compile_state *state, char *used, int reg)
18402{
18403 unsigned equivs[MAX_REG_EQUIVS];
18404 int i;
18405 if (reg == REG_UNNEEDED) {
18406 return;
18407 }
18408 arch_reg_equivs(state, equivs, reg);
18409 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18410 used[equivs[i]] += 1;
18411 }
18412 return;
18413}
18414
Eric Biedermanb138ac82003-04-22 18:44:01 +000018415static unsigned int hash_live_edge(
18416 struct live_range *left, struct live_range *right)
18417{
18418 unsigned int hash, val;
18419 unsigned long lval, rval;
18420 lval = ((unsigned long)left)/sizeof(struct live_range);
18421 rval = ((unsigned long)right)/sizeof(struct live_range);
18422 hash = 0;
18423 while(lval) {
18424 val = lval & 0xff;
18425 lval >>= 8;
18426 hash = (hash *263) + val;
18427 }
18428 while(rval) {
18429 val = rval & 0xff;
18430 rval >>= 8;
18431 hash = (hash *263) + val;
18432 }
18433 hash = hash & (LRE_HASH_SIZE - 1);
18434 return hash;
18435}
18436
18437static struct lre_hash **lre_probe(struct reg_state *rstate,
18438 struct live_range *left, struct live_range *right)
18439{
18440 struct lre_hash **ptr;
18441 unsigned int index;
18442 /* Ensure left <= right */
18443 if (left > right) {
18444 struct live_range *tmp;
18445 tmp = left;
18446 left = right;
18447 right = tmp;
18448 }
18449 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018450
Eric Biedermanb138ac82003-04-22 18:44:01 +000018451 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018452 while(*ptr) {
18453 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18454 break;
18455 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018456 ptr = &(*ptr)->next;
18457 }
18458 return ptr;
18459}
18460
18461static int interfere(struct reg_state *rstate,
18462 struct live_range *left, struct live_range *right)
18463{
18464 struct lre_hash **ptr;
18465 ptr = lre_probe(rstate, left, right);
18466 return ptr && *ptr;
18467}
18468
Stefan Reinauer14e22772010-04-27 06:56:47 +000018469static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018470 struct live_range *left, struct live_range *right)
18471{
18472 /* FIXME the memory allocation overhead is noticeable here... */
18473 struct lre_hash **ptr, *new_hash;
18474 struct live_range_edge *edge;
18475
18476 if (left == right) {
18477 return;
18478 }
18479 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18480 return;
18481 }
18482 /* Ensure left <= right */
18483 if (left > right) {
18484 struct live_range *tmp;
18485 tmp = left;
18486 left = right;
18487 right = tmp;
18488 }
18489 ptr = lre_probe(rstate, left, right);
18490 if (*ptr) {
18491 return;
18492 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018493#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018494 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018495 left, right);
18496#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018497 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18498 new_hash->next = *ptr;
18499 new_hash->left = left;
18500 new_hash->right = right;
18501 *ptr = new_hash;
18502
18503 edge = xmalloc(sizeof(*edge), "live_range_edge");
18504 edge->next = left->edges;
18505 edge->node = right;
18506 left->edges = edge;
18507 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018508
Eric Biedermanb138ac82003-04-22 18:44:01 +000018509 edge = xmalloc(sizeof(*edge), "live_range_edge");
18510 edge->next = right->edges;
18511 edge->node = left;
18512 right->edges = edge;
18513 right->degree += 1;
18514}
18515
18516static void remove_live_edge(struct reg_state *rstate,
18517 struct live_range *left, struct live_range *right)
18518{
18519 struct live_range_edge *edge, **ptr;
18520 struct lre_hash **hptr, *entry;
18521 hptr = lre_probe(rstate, left, right);
18522 if (!hptr || !*hptr) {
18523 return;
18524 }
18525 entry = *hptr;
18526 *hptr = entry->next;
18527 xfree(entry);
18528
18529 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18530 edge = *ptr;
18531 if (edge->node == right) {
18532 *ptr = edge->next;
18533 memset(edge, 0, sizeof(*edge));
18534 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018535 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018536 break;
18537 }
18538 }
18539 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18540 edge = *ptr;
18541 if (edge->node == left) {
18542 *ptr = edge->next;
18543 memset(edge, 0, sizeof(*edge));
18544 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018545 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018546 break;
18547 }
18548 }
18549}
18550
18551static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18552{
18553 struct live_range_edge *edge, *next;
18554 for(edge = range->edges; edge; edge = next) {
18555 next = edge->next;
18556 remove_live_edge(rstate, range, edge->node);
18557 }
18558}
18559
Stefan Reinauer14e22772010-04-27 06:56:47 +000018560static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018561 struct live_range *dest, struct live_range *src)
18562{
18563 struct live_range_edge *edge, *next;
18564 for(edge = src->edges; edge; edge = next) {
18565 struct live_range *other;
18566 next = edge->next;
18567 other = edge->node;
18568 remove_live_edge(rstate, src, other);
18569 add_live_edge(rstate, dest, other);
18570 }
18571}
18572
Eric Biedermanb138ac82003-04-22 18:44:01 +000018573
18574/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018575 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018576 * new(n) --- Return a graph with n nodes but no edges.
18577 * add(g,x,y) --- Return a graph including g with an between x and y
18578 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18579 * x and y in the graph g
18580 * degree(g, x) --- Return the degree of the node x in the graph g
18581 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18582 *
18583 * Implement with a hash table && a set of adjcency vectors.
18584 * The hash table supports constant time implementations of add and interfere.
18585 * The adjacency vectors support an efficient implementation of neighbors.
18586 */
18587
Stefan Reinauer14e22772010-04-27 06:56:47 +000018588/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018589 * +---------------------------------------------------+
18590 * | +--------------+ |
18591 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018592 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018593 *
18594 * -- In simplify implment optimistic coloring... (No backtracking)
18595 * -- Implement Rematerialization it is the only form of spilling we can perform
18596 * Essentially this means dropping a constant from a register because
18597 * we can regenerate it later.
18598 *
18599 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18600 * coalesce at phi points...
18601 * --- Bias coloring if at all possible do the coalesing a compile time.
18602 *
18603 *
18604 */
18605
Stefan Reinauer50542a82007-10-24 11:14:14 +000018606#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018607static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018608 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018609 struct triple *parent, struct triple *ins)
18610{
18611 struct live_range *lr;
18612 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018613 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018614 expr = triple_rhs(state, ins, 0);
18615 for(;expr; expr = triple_rhs(state, ins, expr)) {
18616 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018617 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018618 continue;
18619 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018620 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018621 if (lr->color == lr2->color) {
18622 internal_error(state, ins, "live range too big");
18623 }
18624 }
18625}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018626#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018627
18628static struct live_range *coalesce_ranges(
18629 struct compile_state *state, struct reg_state *rstate,
18630 struct live_range *lr1, struct live_range *lr2)
18631{
18632 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18633 unsigned color;
18634 unsigned classes;
18635 if (lr1 == lr2) {
18636 return lr1;
18637 }
18638 if (!lr1->defs || !lr2->defs) {
18639 internal_error(state, 0,
18640 "cannot coalese dead live ranges");
18641 }
18642 if ((lr1->color == REG_UNNEEDED) ||
18643 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018644 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018645 "cannot coalesce live ranges without a possible color");
18646 }
18647 if ((lr1->color != lr2->color) &&
18648 (lr1->color != REG_UNSET) &&
18649 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018650 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018651 "cannot coalesce live ranges of different colors");
18652 }
18653 color = lr1->color;
18654 if (color == REG_UNSET) {
18655 color = lr2->color;
18656 }
18657 classes = lr1->classes & lr2->classes;
18658 if (!classes) {
18659 internal_error(state, lr1->defs->def,
18660 "cannot coalesce live ranges with dissimilar register classes");
18661 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018662 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018663 FILE *fp = state->errout;
18664 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018665 lrd = lr1->defs;
18666 do {
Eric Biederman90089602004-05-28 14:11:54 +000018667 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018668 lrd = lrd->next;
18669 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018670 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018671 lrd = lr2->defs;
18672 do {
Eric Biederman90089602004-05-28 14:11:54 +000018673 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018674 lrd = lrd->next;
18675 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018676 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018677 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018678 /* If there is a clear dominate live range put it in lr1,
18679 * For purposes of this test phi functions are
18680 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018681 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018682 */
18683 if ((lr1->defs->prev->def->op == OP_PHI) ||
18684 ((lr2->defs->prev->def->op != OP_PHI) &&
18685 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18686 struct live_range *tmp;
18687 tmp = lr1;
18688 lr1 = lr2;
18689 lr2 = tmp;
18690 }
18691#if 0
18692 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018693 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018694 }
18695 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018696 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018697 }
18698 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018699 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018700 }
18701 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018702 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018703 }
18704#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018705#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018706 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018707 lr1->defs->def,
18708 lr1->color,
18709 lr2->defs->def,
18710 lr2->color);
18711#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018712
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018713 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018714#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018715#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018716#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018717 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018718 * and to the necessity of making multiple passes of coalesce_live_ranges.
18719 * A failure to find some coalesce opportunities in coaleace_live_ranges
18720 * does not impact the correct of the compiler just the efficiency with
18721 * which registers are allocated.
18722 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018723 head = lr1->defs;
18724 mid1 = lr1->defs->prev;
18725 mid2 = lr2->defs;
18726 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018727
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018728 head->prev = end;
18729 end->next = head;
18730
18731 mid1->next = mid2;
18732 mid2->prev = mid1;
18733
18734 /* Fixup the live range in the added live range defs */
18735 lrd = head;
18736 do {
18737 lrd->lr = lr1;
18738 lrd = lrd->next;
18739 } while(lrd != head);
18740
18741 /* Mark lr2 as free. */
18742 lr2->defs = 0;
18743 lr2->color = REG_UNNEEDED;
18744 lr2->classes = 0;
18745
18746 if (!lr1->defs) {
18747 internal_error(state, 0, "lr1->defs == 0 ?");
18748 }
18749
18750 lr1->color = color;
18751 lr1->classes = classes;
18752
Eric Biederman153ea352003-06-20 14:43:20 +000018753 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18754 transfer_live_edges(rstate, lr1, lr2);
18755
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018756 return lr1;
18757}
18758
18759static struct live_range_def *live_range_head(
18760 struct compile_state *state, struct live_range *lr,
18761 struct live_range_def *last)
18762{
18763 struct live_range_def *result;
18764 result = 0;
18765 if (last == 0) {
18766 result = lr->defs;
18767 }
18768 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18769 result = last->next;
18770 }
18771 return result;
18772}
18773
18774static struct live_range_def *live_range_end(
18775 struct compile_state *state, struct live_range *lr,
18776 struct live_range_def *last)
18777{
18778 struct live_range_def *result;
18779 result = 0;
18780 if (last == 0) {
18781 result = lr->defs->prev;
18782 }
18783 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18784 result = last->prev;
18785 }
18786 return result;
18787}
18788
18789
Eric Biedermanb138ac82003-04-22 18:44:01 +000018790static void initialize_live_ranges(
18791 struct compile_state *state, struct reg_state *rstate)
18792{
18793 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018794 size_t count, size;
18795 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018796
Eric Biederman83b991a2003-10-11 06:20:25 +000018797 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018798 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018799 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018800 count = count_triples(state);
18801 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018802 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018803 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018804 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018805 /* Potentially I need one live range for each instruction
18806 * plus an extra for the dummy live range.
18807 */
18808 rstate->ranges = count + 1;
18809 size = sizeof(rstate->lrd[0]) * rstate->defs;
18810 rstate->lrd = xcmalloc(size, "live_range_def");
18811 size = sizeof(rstate->lr[0]) * rstate->ranges;
18812 rstate->lr = xcmalloc(size, "live_range");
18813
Eric Biedermanb138ac82003-04-22 18:44:01 +000018814 /* Setup the dummy live range */
18815 rstate->lr[0].classes = 0;
18816 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018817 rstate->lr[0].defs = 0;
18818 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018819 ins = first;
18820 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018821 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018822 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018823 struct reg_info info;
18824 /* Find the architecture specific color information */
18825 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018826 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018827 rstate->lr[i].defs = &rstate->lrd[j];
18828 rstate->lr[i].color = info.reg;
18829 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018830 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018831 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018832 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018833 /* Otherwise give the triple the dummy live range. */
18834 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018835 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018836 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018837
18838 /* Initalize the live_range_def */
18839 rstate->lrd[j].next = &rstate->lrd[j];
18840 rstate->lrd[j].prev = &rstate->lrd[j];
18841 rstate->lrd[j].def = ins;
18842 rstate->lrd[j].orig_id = ins->id;
18843 ins->id = j;
18844
18845 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018846 ins = ins->next;
18847 } while(ins != first);
18848 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018849
Eric Biedermanb138ac82003-04-22 18:44:01 +000018850 /* Make a second pass to handle achitecture specific register
18851 * constraints.
18852 */
18853 ins = first;
18854 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018855 int zlhs, zrhs, i, j;
18856 if (ins->id > rstate->defs) {
18857 internal_error(state, ins, "bad id");
18858 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018859
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018860 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018861 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018862 if ((zlhs == 0) && triple_is_def(state, ins)) {
18863 zlhs = 1;
18864 }
Eric Biederman90089602004-05-28 14:11:54 +000018865 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018866
Eric Biederman5ade04a2003-10-22 04:03:46 +000018867 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018868 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018869 ins, zlhs, zrhs);
18870 }
18871
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018872 for(i = 0; i < zlhs; i++) {
18873 struct reg_info linfo;
18874 struct live_range_def *lhs;
18875 linfo = arch_reg_lhs(state, ins, i);
18876 if (linfo.reg < MAX_REGISTERS) {
18877 continue;
18878 }
18879 if (triple_is_def(state, ins)) {
18880 lhs = &rstate->lrd[ins->id];
18881 } else {
18882 lhs = &rstate->lrd[LHS(ins, i)->id];
18883 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018884
18885 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018886 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018887 i, lhs, linfo.reg);
18888 }
18889
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018890 for(j = 0; j < zrhs; j++) {
18891 struct reg_info rinfo;
18892 struct live_range_def *rhs;
18893 rinfo = arch_reg_rhs(state, ins, j);
18894 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018895 continue;
18896 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018897 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018898
18899 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018900 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018901 j, rhs, rinfo.reg);
18902 }
18903
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018904 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018905 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018906 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018907 }
18908 }
18909 }
18910 ins = ins->next;
18911 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018912}
18913
Eric Biedermanf96a8102003-06-16 16:57:34 +000018914static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018915 struct compile_state *state,
18916 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018917 struct reg_block *rb, struct triple *ins, void *arg)
18918{
18919 struct reg_state *rstate = arg;
18920 struct live_range *def;
18921 struct triple_reg_set *entry;
18922
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018923 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018924 * we do not have a definition to add to
18925 * the interference graph.
18926 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018927 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018928 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018929 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018930 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018931
Eric Biedermanb138ac82003-04-22 18:44:01 +000018932 /* Create an edge between ins and everything that is
18933 * alive, unless the live_range cannot share
18934 * a physical register with ins.
18935 */
18936 for(entry = live; entry; entry = entry->next) {
18937 struct live_range *lr;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018938 if (entry->member->id > rstate->defs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018939 internal_error(state, 0, "bad entry?");
18940 }
18941 lr = rstate->lrd[entry->member->id].lr;
18942 if (def == lr) {
18943 continue;
18944 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018945 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18946 continue;
18947 }
18948 add_live_edge(rstate, def, lr);
18949 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018950 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018951}
18952
Stefan Reinauer50542a82007-10-24 11:14:14 +000018953#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018954static struct live_range *get_verify_live_range(
18955 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18956{
18957 struct live_range *lr;
18958 struct live_range_def *lrd;
18959 int ins_found;
18960 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18961 internal_error(state, ins, "bad ins?");
18962 }
18963 lr = rstate->lrd[ins->id].lr;
18964 ins_found = 0;
18965 lrd = lr->defs;
18966 do {
18967 if (lrd->def == ins) {
18968 ins_found = 1;
18969 }
18970 lrd = lrd->next;
18971 } while(lrd != lr->defs);
18972 if (!ins_found) {
18973 internal_error(state, ins, "ins not in live range");
18974 }
18975 return lr;
18976}
18977
18978static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018979 struct compile_state *state,
18980 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018981 struct reg_block *rb, struct triple *ins, void *arg)
18982{
18983 struct reg_state *rstate = arg;
18984 struct triple_reg_set *entry1, *entry2;
18985
18986
18987 /* Compare live against edges and make certain the code is working */
18988 for(entry1 = live; entry1; entry1 = entry1->next) {
18989 struct live_range *lr1;
18990 lr1 = get_verify_live_range(state, rstate, entry1->member);
18991 for(entry2 = live; entry2; entry2 = entry2->next) {
18992 struct live_range *lr2;
18993 struct live_range_edge *edge2;
18994 int lr1_found;
18995 int lr2_degree;
18996 if (entry2 == entry1) {
18997 continue;
18998 }
18999 lr2 = get_verify_live_range(state, rstate, entry2->member);
19000 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019001 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019002 "live range with 2 values simultaneously alive");
19003 }
19004 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19005 continue;
19006 }
19007 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019008 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019009 "edges don't interfere?");
19010 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019011
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019012 lr1_found = 0;
19013 lr2_degree = 0;
19014 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19015 lr2_degree++;
19016 if (edge2->node == lr1) {
19017 lr1_found = 1;
19018 }
19019 }
19020 if (lr2_degree != lr2->degree) {
19021 internal_error(state, entry2->member,
19022 "computed degree: %d does not match reported degree: %d\n",
19023 lr2_degree, lr2->degree);
19024 }
19025 if (!lr1_found) {
19026 internal_error(state, entry2->member, "missing edge");
19027 }
19028 }
19029 }
19030 return;
19031}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019032#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019033
Eric Biedermanf96a8102003-06-16 16:57:34 +000019034static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019035 struct compile_state *state,
19036 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019037 struct reg_block *rb, struct triple *ins, void *arg)
19038{
19039 struct reg_state *rstate = arg;
19040 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019041 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019042 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019043
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019044 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019045 id = ins->id;
19046 ins->id = rstate->lrd[id].orig_id;
19047 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019048 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019049 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019050
19051 if (lr->defs) {
19052 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019053 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019054 lrd = lr->defs;
19055 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019056 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019057 lrd = lrd->next;
19058 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019059 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019060 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019061 if (live) {
19062 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019063 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019064 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019065 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019066 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019067 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019068 }
19069 if (lr->edges) {
19070 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019071 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019072 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019073 struct live_range_def *lrd;
19074 lrd = entry->node->defs;
19075 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019076 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019077 lrd = lrd->next;
19078 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019079 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019080 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019081 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019082 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019083 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019084 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019085 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019086 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019087}
19088
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019089static int coalesce_live_ranges(
19090 struct compile_state *state, struct reg_state *rstate)
19091{
19092 /* At the point where a value is moved from one
19093 * register to another that value requires two
19094 * registers, thus increasing register pressure.
19095 * Live range coaleescing reduces the register
19096 * pressure by keeping a value in one register
19097 * longer.
19098 *
19099 * In the case of a phi function all paths leading
19100 * into it must be allocated to the same register
19101 * otherwise the phi function may not be removed.
19102 *
19103 * Forcing a value to stay in a single register
19104 * for an extended period of time does have
19105 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019106 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019107 *
19108 * The two cases I have identified are:
19109 * 1) Two forced register assignments may
19110 * collide.
19111 * 2) Registers may go unused because they
19112 * are only good for storing the value
19113 * and not manipulating it.
19114 *
19115 * Because of this I need to split live ranges,
19116 * even outside of the context of coalesced live
19117 * ranges. The need to split live ranges does
19118 * impose some constraints on live range coalescing.
19119 *
19120 * - Live ranges may not be coalesced across phi
19121 * functions. This creates a 2 headed live
19122 * range that cannot be sanely split.
19123 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019124 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019125 * are handled as pre split live ranges so we will
19126 * never attempt to split them.
19127 */
19128 int coalesced;
19129 int i;
19130
19131 coalesced = 0;
19132 for(i = 0; i <= rstate->ranges; i++) {
19133 struct live_range *lr1;
19134 struct live_range_def *lrd1;
19135 lr1 = &rstate->lr[i];
19136 if (!lr1->defs) {
19137 continue;
19138 }
19139 lrd1 = live_range_end(state, lr1, 0);
19140 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19141 struct triple_set *set;
19142 if (lrd1->def->op != OP_COPY) {
19143 continue;
19144 }
19145 /* Skip copies that are the result of a live range split. */
19146 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19147 continue;
19148 }
19149 for(set = lrd1->def->use; set; set = set->next) {
19150 struct live_range_def *lrd2;
19151 struct live_range *lr2, *res;
19152
19153 lrd2 = &rstate->lrd[set->member->id];
19154
19155 /* Don't coalesce with instructions
19156 * that are the result of a live range
19157 * split.
19158 */
19159 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19160 continue;
19161 }
19162 lr2 = rstate->lrd[set->member->id].lr;
19163 if (lr1 == lr2) {
19164 continue;
19165 }
19166 if ((lr1->color != lr2->color) &&
19167 (lr1->color != REG_UNSET) &&
19168 (lr2->color != REG_UNSET)) {
19169 continue;
19170 }
19171 if ((lr1->classes & lr2->classes) == 0) {
19172 continue;
19173 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019174
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019175 if (interfere(rstate, lr1, lr2)) {
19176 continue;
19177 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019178
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019179 res = coalesce_ranges(state, rstate, lr1, lr2);
19180 coalesced += 1;
19181 if (res != lr1) {
19182 goto next;
19183 }
19184 }
19185 }
19186 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019187 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019188 }
19189 return coalesced;
19190}
19191
19192
Eric Biedermanf96a8102003-06-16 16:57:34 +000019193static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019194 struct reg_block *blocks, struct triple_reg_set *live,
19195 struct reg_block *rb, struct triple *ins, void *arg)
19196{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019197 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019198 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019199
19200 /* See if we have a mandatory coalesce operation between
19201 * a lhs and a rhs value. If so and the rhs value is also
19202 * alive then this triple needs to be pre copied. Otherwise
19203 * we would have two definitions in the same live range simultaneously
19204 * alive.
19205 */
Eric Biederman90089602004-05-28 14:11:54 +000019206 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019207 if ((zlhs == 0) && triple_is_def(state, ins)) {
19208 zlhs = 1;
19209 }
Eric Biederman90089602004-05-28 14:11:54 +000019210 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019211 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019212 struct reg_info linfo;
19213 linfo = arch_reg_lhs(state, ins, i);
19214 if (linfo.reg < MAX_REGISTERS) {
19215 continue;
19216 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019217 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019218 struct reg_info rinfo;
19219 struct triple *rhs;
19220 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019221 int found;
19222 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019223 rinfo = arch_reg_rhs(state, ins, j);
19224 if (rinfo.reg != linfo.reg) {
19225 continue;
19226 }
19227 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019228 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019229 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019230 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019231 }
19232 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019233 if (found) {
19234 struct triple *copy;
19235 copy = pre_copy(state, ins, j);
19236 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019237 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019238 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019239 }
19240 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019241 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019242}
19243
Eric Biedermand1ea5392003-06-28 06:49:45 +000019244static int correct_coalesce_conflicts(
19245 struct compile_state *state, struct reg_block *blocks)
19246{
19247 int conflicts;
19248 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019249 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019250 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019251 return conflicts;
19252}
19253
Eric Biedermanf96a8102003-06-16 16:57:34 +000019254static void replace_set_use(struct compile_state *state,
19255 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019256{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019257 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019258 for(set = head; set; set = set->next) {
19259 if (set->member == orig) {
19260 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019261 }
19262 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019263}
19264
Stefan Reinauer14e22772010-04-27 06:56:47 +000019265static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019266 struct reg_block *blocks, struct triple *orig, struct triple *new)
19267{
19268 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019269#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019270#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019271#endif
Eric Biederman90089602004-05-28 14:11:54 +000019272 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019273 struct reg_block *rb;
19274 rb = &blocks[i];
19275 replace_set_use(state, rb->in, orig, new);
19276 replace_set_use(state, rb->out, orig, new);
19277 }
19278}
19279
19280static void color_instructions(struct compile_state *state)
19281{
19282 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019283 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019284 ins = first;
19285 do {
19286 if (triple_is_def(state, ins)) {
19287 struct reg_info info;
19288 info = find_lhs_color(state, ins, 0);
19289 if (info.reg >= MAX_REGISTERS) {
19290 info.reg = REG_UNSET;
19291 }
19292 SET_INFO(ins->id, info);
19293 }
19294 ins = ins->next;
19295 } while(ins != first);
19296}
19297
19298static struct reg_info read_lhs_color(
19299 struct compile_state *state, struct triple *ins, int index)
19300{
19301 struct reg_info info;
19302 if ((index == 0) && triple_is_def(state, ins)) {
19303 info.reg = ID_REG(ins->id);
19304 info.regcm = ID_REGCM(ins->id);
19305 }
Eric Biederman90089602004-05-28 14:11:54 +000019306 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019307 info = read_lhs_color(state, LHS(ins, index), 0);
19308 }
19309 else {
19310 internal_error(state, ins, "Bad lhs %d", index);
19311 info.reg = REG_UNSET;
19312 info.regcm = 0;
19313 }
19314 return info;
19315}
19316
19317static struct triple *resolve_tangle(
19318 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019319{
19320 struct reg_info info, uinfo;
19321 struct triple_set *set, *next;
19322 struct triple *copy;
19323
Stefan Reinauer50542a82007-10-24 11:14:14 +000019324#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019325#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019326#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019327 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019328 for(set = tangle->use; set; set = next) {
19329 struct triple *user;
19330 int i, zrhs;
19331 next = set->next;
19332 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019333 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019334 for(i = 0; i < zrhs; i++) {
19335 if (RHS(user, i) != tangle) {
19336 continue;
19337 }
19338 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019339 if (uinfo.reg == info.reg) {
19340 copy = pre_copy(state, user, i);
19341 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019342 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019343 }
19344 }
19345 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019346 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019347 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019348 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019349 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350 copy = post_copy(state, tangle);
19351 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019352 linfo = find_lhs_color(state, copy, 0);
19353 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019354 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019355 info = find_lhs_color(state, tangle, 0);
19356 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019357
Eric Biedermanf96a8102003-06-16 16:57:34 +000019358 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019359}
19360
19361
Eric Biedermanf96a8102003-06-16 16:57:34 +000019362static void fix_tangles(struct compile_state *state,
19363 struct reg_block *blocks, struct triple_reg_set *live,
19364 struct reg_block *rb, struct triple *ins, void *arg)
19365{
Eric Biederman153ea352003-06-20 14:43:20 +000019366 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019367 struct triple *tangle;
19368 do {
19369 char used[MAX_REGISTERS];
19370 struct triple_reg_set *set;
19371 tangle = 0;
19372
19373 /* Find out which registers have multiple uses at this point */
19374 memset(used, 0, sizeof(used));
19375 for(set = live; set; set = set->next) {
19376 struct reg_info info;
19377 info = read_lhs_color(state, set->member, 0);
19378 if (info.reg == REG_UNSET) {
19379 continue;
19380 }
19381 reg_inc_used(state, used, info.reg);
19382 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019383
Eric Biedermanf96a8102003-06-16 16:57:34 +000019384 /* Now find the least dominated definition of a register in
19385 * conflict I have seen so far.
19386 */
19387 for(set = live; set; set = set->next) {
19388 struct reg_info info;
19389 info = read_lhs_color(state, set->member, 0);
19390 if (used[info.reg] < 2) {
19391 continue;
19392 }
Eric Biederman153ea352003-06-20 14:43:20 +000019393 /* Changing copies that feed into phi functions
19394 * is incorrect.
19395 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019396 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019397 (set->member->use->member->op == OP_PHI)) {
19398 continue;
19399 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019400 if (!tangle || tdominates(state, set->member, tangle)) {
19401 tangle = set->member;
19402 }
19403 }
19404 /* If I have found a tangle resolve it */
19405 if (tangle) {
19406 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019407 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019408 post_copy = resolve_tangle(state, tangle);
19409 if (post_copy) {
19410 replace_block_use(state, blocks, tangle, post_copy);
19411 }
19412 if (post_copy && (tangle != ins)) {
19413 replace_set_use(state, live, tangle, post_copy);
19414 }
19415 }
19416 } while(tangle);
19417 return;
19418}
19419
Eric Biederman153ea352003-06-20 14:43:20 +000019420static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019421 struct compile_state *state, struct reg_block *blocks)
19422{
Eric Biederman153ea352003-06-20 14:43:20 +000019423 int tangles;
19424 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019425 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019426 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019427 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019428 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019429}
19430
Eric Biedermand1ea5392003-06-28 06:49:45 +000019431
19432static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19433static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19434
19435struct triple *find_constrained_def(
19436 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019437{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019438 struct live_range_def *lrd, *lrd_next;
19439 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019440 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019441 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019442 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019443
19444 lrd = lrd_next;
19445 lrd_next = lrd->next;
19446
Eric Biedermand1ea5392003-06-28 06:49:45 +000019447 regcm = arch_type_to_regcm(state, lrd->def->type);
19448 info = find_lhs_color(state, lrd->def, 0);
19449 regcm = arch_regcm_reg_normalize(state, regcm);
19450 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019451 /* If the 2 register class masks are equal then
19452 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019453 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019454 if (regcm == info.regcm) {
19455 continue;
19456 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019457
Eric Biederman5ade04a2003-10-22 04:03:46 +000019458 /* If there is just one use.
19459 * That use cannot accept a larger register class.
19460 * There are no intervening definitions except
19461 * definitions that feed into that use.
19462 * Then a triple is not constrained.
19463 * FIXME handle this case!
19464 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019465#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019466#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019467#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019468
Eric Biederman5ade04a2003-10-22 04:03:46 +000019469
Eric Biedermand1ea5392003-06-28 06:49:45 +000019470 /* Of the constrained live ranges deal with the
19471 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019472 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019473 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019474 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019475 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019476 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019477 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019478 tdominates(state, lrd->def, constrained))
19479 {
19480 constrained = lrd->def;
19481 }
19482 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019483 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019484}
19485
Eric Biedermand1ea5392003-06-28 06:49:45 +000019486static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019487 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019488 struct live_range *range)
19489{
19490 /* Walk through the edges in conflict and our current live
19491 * range, and find definitions that are more severly constrained
19492 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019493 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019494 * Then pick one of those ranges and relax the constraints.
19495 */
19496 struct live_range_edge *edge;
19497 struct triple *constrained;
19498
19499 constrained = 0;
19500 for(edge = range->edges; edge; edge = edge->next) {
19501 constrained = find_constrained_def(state, edge->node, constrained);
19502 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019503#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019504#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019505#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019506 if (!constrained) {
19507 constrained = find_constrained_def(state, range, constrained);
19508 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019509
19510 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019511 fprintf(state->errout, "constrained: ");
19512 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019513 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019514 if (constrained) {
19515 ids_from_rstate(state, rstate);
19516 cleanup_rstate(state, rstate);
19517 resolve_tangle(state, constrained);
19518 }
19519 return !!constrained;
19520}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019521
Eric Biedermand1ea5392003-06-28 06:49:45 +000019522static int split_ranges(
19523 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019524 char *used, struct live_range *range)
19525{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019526 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019527 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019528 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019529 rstate->passes, tops(range->defs->def->op), range->defs->def);
19530 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019531 if ((range->color == REG_UNNEEDED) ||
19532 (rstate->passes >= rstate->max_passes)) {
19533 return 0;
19534 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019535 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019536
Eric Biedermand1ea5392003-06-28 06:49:45 +000019537 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019538 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019539 * (a) allow me to spill a register or
19540 * (b) allow me to place a value in another register.
19541 *
19542 * So far I don't have a test case for this, the resolving
19543 * of mandatory constraints has solved all of my
19544 * know issues. So I have choosen not to write any
19545 * code until I cat get a better feel for cases where
19546 * it would be useful to have.
19547 *
19548 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019549#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019550#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019551#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019552
Eric Biederman5ade04a2003-10-22 04:03:46 +000019553 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019554 FILE *fp = state->errout;
19555 print_interference_blocks(state, rstate, fp, 0);
19556 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019557 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019558 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019559}
19560
Eric Biederman5ade04a2003-10-22 04:03:46 +000019561static FILE *cgdebug_fp(struct compile_state *state)
19562{
19563 FILE *fp;
19564 fp = 0;
19565 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019566 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019567 }
19568 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019569 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019570 }
19571 return fp;
19572}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019573
Eric Biederman5ade04a2003-10-22 04:03:46 +000019574static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19575{
19576 FILE *fp;
19577 fp = cgdebug_fp(state);
19578 if (fp) {
19579 va_list args;
19580 va_start(args, fmt);
19581 vfprintf(fp, fmt, args);
19582 va_end(args);
19583 }
19584}
19585
19586static void cgdebug_flush(struct compile_state *state)
19587{
19588 FILE *fp;
19589 fp = cgdebug_fp(state);
19590 if (fp) {
19591 fflush(fp);
19592 }
19593}
19594
19595static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19596{
19597 FILE *fp;
19598 fp = cgdebug_fp(state);
19599 if (fp) {
19600 loc(fp, state, ins);
19601 }
19602}
19603
Stefan Reinauer14e22772010-04-27 06:56:47 +000019604static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019605 struct reg_state *rstate, struct live_range *range)
19606{
19607 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019608 struct live_range_def *lrd;
19609 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019610 struct live_range_edge *edge;
19611 char used[MAX_REGISTERS];
19612 struct triple **expr;
19613
Eric Biedermanb138ac82003-04-22 18:44:01 +000019614 /* Instead of doing just the trivial color select here I try
19615 * a few extra things because a good color selection will help reduce
19616 * copies.
19617 */
19618
19619 /* Find the registers currently in use */
19620 memset(used, 0, sizeof(used));
19621 for(edge = range->edges; edge; edge = edge->next) {
19622 if (edge->node->color == REG_UNSET) {
19623 continue;
19624 }
19625 reg_fill_used(state, used, edge->node->color);
19626 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019627
19628 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019629 int i;
19630 i = 0;
19631 for(edge = range->edges; edge; edge = edge->next) {
19632 i++;
19633 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019634 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019635 tops(range->defs->def->op), i);
19636 cgdebug_loc(state, range->defs->def);
19637 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019638 for(i = 0; i < MAX_REGISTERS; i++) {
19639 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019640 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019641 arch_reg_str(i));
19642 }
19643 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019644 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019645
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019646 /* If a color is already assigned see if it will work */
19647 if (range->color != REG_UNSET) {
19648 struct live_range_def *lrd;
19649 if (!used[range->color]) {
19650 return 1;
19651 }
19652 for(edge = range->edges; edge; edge = edge->next) {
19653 if (edge->node->color != range->color) {
19654 continue;
19655 }
19656 warning(state, edge->node->defs->def, "edge: ");
19657 lrd = edge->node->defs;
19658 do {
19659 warning(state, lrd->def, " %p %s",
19660 lrd->def, tops(lrd->def->op));
19661 lrd = lrd->next;
19662 } while(lrd != edge->node->defs);
19663 }
19664 lrd = range->defs;
19665 warning(state, range->defs->def, "def: ");
19666 do {
19667 warning(state, lrd->def, " %p %s",
19668 lrd->def, tops(lrd->def->op));
19669 lrd = lrd->next;
19670 } while(lrd != range->defs);
19671 internal_error(state, range->defs->def,
19672 "live range with already used color %s",
19673 arch_reg_str(range->color));
19674 }
19675
Eric Biedermanb138ac82003-04-22 18:44:01 +000019676 /* If I feed into an expression reuse it's color.
19677 * This should help remove copies in the case of 2 register instructions
19678 * and phi functions.
19679 */
19680 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019681 lrd = live_range_end(state, range, 0);
19682 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19683 entry = lrd->def->use;
19684 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19685 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019686 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019687 insd = &rstate->lrd[entry->member->id];
19688 if (insd->lr->defs == 0) {
19689 continue;
19690 }
19691 if (!phi && (insd->def->op == OP_PHI) &&
19692 !interfere(rstate, range, insd->lr)) {
19693 phi = insd;
19694 }
Eric Biederman530b5192003-07-01 10:05:30 +000019695 if (insd->lr->color == REG_UNSET) {
19696 continue;
19697 }
19698 regcm = insd->lr->classes;
19699 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019700 (used[insd->lr->color])) {
19701 continue;
19702 }
19703 if (interfere(rstate, range, insd->lr)) {
19704 continue;
19705 }
19706 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019707 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019708 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019709 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019710 * of something else that feeds into the phi function.
19711 */
19712 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019713 if (phi->lr->color != REG_UNSET) {
19714 if (used[phi->lr->color]) {
19715 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019716 }
19717 }
19718 else {
19719 expr = triple_rhs(state, phi->def, 0);
19720 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19721 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019722 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019723 if (!*expr) {
19724 continue;
19725 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019726 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019727 if (lr->color == REG_UNSET) {
19728 continue;
19729 }
19730 regcm = lr->classes;
19731 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019732 (used[lr->color])) {
19733 continue;
19734 }
19735 if (interfere(rstate, range, lr)) {
19736 continue;
19737 }
19738 range->color = lr->color;
19739 }
19740 }
19741 }
19742 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019743 lrd = live_range_head(state, range, 0);
19744 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19745 expr = triple_rhs(state, lrd->def, 0);
19746 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019747 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019748 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019749 if (!*expr) {
19750 continue;
19751 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019752 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019753 if (lr->color == REG_UNSET) {
19754 continue;
19755 }
19756 regcm = lr->classes;
19757 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019758 (used[lr->color])) {
19759 continue;
19760 }
19761 if (interfere(rstate, range, lr)) {
19762 continue;
19763 }
19764 range->color = lr->color;
19765 break;
19766 }
19767 }
19768 /* If I have not opportunitically picked a useful color
19769 * pick the first color that is free.
19770 */
19771 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019772 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019773 arch_select_free_register(state, used, range->classes);
19774 }
19775 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019776 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019777 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019778 if (split_ranges(state, rstate, used, range)) {
19779 return 0;
19780 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019781 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019782 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019783 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019784 lrd = edge->node->defs;
19785 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019786 warning(state, lrd->def, " %s %p",
19787 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019788 lrd = lrd->next;
19789 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019790 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019791 warning(state, range->defs->def, "range: ");
19792 lrd = range->defs;
19793 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019794 warning(state, lrd->def, " %s %p",
19795 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019796 lrd = lrd->next;
19797 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019798
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019799 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019800 range->classes);
19801 for(i = 0; i < MAX_REGISTERS; i++) {
19802 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019803 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019804 arch_reg_str(i));
19805 }
19806 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019807 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019808 }
Eric Biederman530b5192003-07-01 10:05:30 +000019809 range->classes &= arch_reg_regcm(state, range->color);
19810 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019811 internal_error(state, range->defs->def, "select_free_color did not?");
19812 }
19813 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019814}
19815
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019816static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019817{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019818 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019819 struct live_range_edge *edge;
19820 struct live_range *range;
19821 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019822 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019823 range = rstate->low;
19824 if (*range->group_prev != range) {
19825 internal_error(state, 0, "lo: *prev != range?");
19826 }
19827 *range->group_prev = range->group_next;
19828 if (range->group_next) {
19829 range->group_next->group_prev = range->group_prev;
19830 }
19831 if (&range->group_next == rstate->low_tail) {
19832 rstate->low_tail = range->group_prev;
19833 }
19834 if (rstate->low == range) {
19835 internal_error(state, 0, "low: next != prev?");
19836 }
19837 }
19838 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019839 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019840 range = rstate->high;
19841 if (*range->group_prev != range) {
19842 internal_error(state, 0, "hi: *prev != range?");
19843 }
19844 *range->group_prev = range->group_next;
19845 if (range->group_next) {
19846 range->group_next->group_prev = range->group_prev;
19847 }
19848 if (&range->group_next == rstate->high_tail) {
19849 rstate->high_tail = range->group_prev;
19850 }
19851 if (rstate->high == range) {
19852 internal_error(state, 0, "high: next != prev?");
19853 }
19854 }
19855 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019856 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019857 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019858 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019859 range->group_prev = 0;
19860 for(edge = range->edges; edge; edge = edge->next) {
19861 struct live_range *node;
19862 node = edge->node;
19863 /* Move nodes from the high to the low list */
19864 if (node->group_prev && (node->color == REG_UNSET) &&
19865 (node->degree == regc_max_size(state, node->classes))) {
19866 if (*node->group_prev != node) {
19867 internal_error(state, 0, "move: *prev != node?");
19868 }
19869 *node->group_prev = node->group_next;
19870 if (node->group_next) {
19871 node->group_next->group_prev = node->group_prev;
19872 }
19873 if (&node->group_next == rstate->high_tail) {
19874 rstate->high_tail = node->group_prev;
19875 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019876 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019877 node->group_prev = rstate->low_tail;
19878 node->group_next = 0;
19879 *rstate->low_tail = node;
19880 rstate->low_tail = &node->group_next;
19881 if (*node->group_prev != node) {
19882 internal_error(state, 0, "move2: *prev != node?");
19883 }
19884 }
19885 node->degree -= 1;
19886 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019887 colored = color_graph(state, rstate);
19888 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019889 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019890 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019891 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019892 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019893 if (colored) {
19894 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19895 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019896 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019897 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019898}
19899
Eric Biedermana96d6a92003-05-13 20:45:19 +000019900static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19901{
19902 struct live_range *lr;
19903 struct live_range_edge *edge;
19904 struct triple *ins, *first;
19905 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019906 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019907 ins = first;
19908 do {
19909 if (triple_is_def(state, ins)) {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019910 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019911 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019912 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019913 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019914 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019915 if (lr->color == REG_UNSET) {
19916 internal_error(state, ins,
19917 "triple without a color");
19918 }
19919 /* Find the registers used by the edges */
19920 memset(used, 0, sizeof(used));
19921 for(edge = lr->edges; edge; edge = edge->next) {
19922 if (edge->node->color == REG_UNSET) {
19923 internal_error(state, 0,
19924 "live range without a color");
19925 }
19926 reg_fill_used(state, used, edge->node->color);
19927 }
19928 if (used[lr->color]) {
19929 internal_error(state, ins,
19930 "triple with already used color");
19931 }
19932 }
19933 ins = ins->next;
19934 } while(ins != first);
19935}
19936
Eric Biedermanb138ac82003-04-22 18:44:01 +000019937static void color_triples(struct compile_state *state, struct reg_state *rstate)
19938{
Eric Biederman90089602004-05-28 14:11:54 +000019939 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019940 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019941 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019942 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019943 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019944 do {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019945 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019946 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019947 "triple without a live range");
19948 }
Eric Biederman90089602004-05-28 14:11:54 +000019949 lrd = &rstate->lrd[ins->id];
19950 lr = lrd->lr;
19951 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019952 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019953 ins = ins->next;
19954 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019955}
19956
Eric Biedermanb138ac82003-04-22 18:44:01 +000019957static struct live_range *merge_sort_lr(
19958 struct live_range *first, struct live_range *last)
19959{
19960 struct live_range *mid, *join, **join_tail, *pick;
19961 size_t size;
19962 size = (last - first) + 1;
19963 if (size >= 2) {
19964 mid = first + size/2;
19965 first = merge_sort_lr(first, mid -1);
19966 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019967
Eric Biedermanb138ac82003-04-22 18:44:01 +000019968 join = 0;
19969 join_tail = &join;
19970 /* merge the two lists */
19971 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019972 if ((first->degree < mid->degree) ||
19973 ((first->degree == mid->degree) &&
19974 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019975 pick = first;
19976 first = first->group_next;
19977 if (first) {
19978 first->group_prev = 0;
19979 }
19980 }
19981 else {
19982 pick = mid;
19983 mid = mid->group_next;
19984 if (mid) {
19985 mid->group_prev = 0;
19986 }
19987 }
19988 pick->group_next = 0;
19989 pick->group_prev = join_tail;
19990 *join_tail = pick;
19991 join_tail = &pick->group_next;
19992 }
19993 /* Splice the remaining list */
19994 pick = (first)? first : mid;
19995 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019996 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019997 pick->group_prev = join_tail;
19998 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019999 }
20000 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020001 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020002 first = 0;
20003 }
20004 join = first;
20005 }
20006 return join;
20007}
20008
Stefan Reinauer14e22772010-04-27 06:56:47 +000020009static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020010 struct reg_state *rstate)
20011{
20012 struct triple *ins, *first;
20013 if (!rstate->defs) {
20014 return;
20015 }
20016 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020017 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020018 FILE *fp = state->dbgout;
20019 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020020 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020021 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020022 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020023 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020024 ins = first;
20025 do {
20026 if (ins->id) {
20027 struct live_range_def *lrd;
20028 lrd = &rstate->lrd[ins->id];
20029 ins->id = lrd->orig_id;
20030 }
20031 ins = ins->next;
20032 } while(ins != first);
20033}
20034
20035static void cleanup_live_edges(struct reg_state *rstate)
20036{
20037 int i;
20038 /* Free the edges on each node */
20039 for(i = 1; i <= rstate->ranges; i++) {
20040 remove_live_edges(rstate, &rstate->lr[i]);
20041 }
20042}
20043
20044static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20045{
20046 cleanup_live_edges(rstate);
20047 xfree(rstate->lrd);
20048 xfree(rstate->lr);
20049
20050 /* Free the variable lifetime information */
20051 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020052 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020053 }
20054 rstate->defs = 0;
20055 rstate->ranges = 0;
20056 rstate->lrd = 0;
20057 rstate->lr = 0;
20058 rstate->blocks = 0;
20059}
20060
Eric Biederman153ea352003-06-20 14:43:20 +000020061static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020062static void allocate_registers(struct compile_state *state)
20063{
20064 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020065 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020066
20067 /* Clear out the reg_state */
20068 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020069 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020070
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020071 do {
20072 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020073 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020074 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020075
Eric Biederman5ade04a2003-10-22 04:03:46 +000020076 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020077 FILE *fp = state->errout;
20078 fprintf(fp, "pass: %d\n", rstate.passes);
20079 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020080 }
Eric Biederman153ea352003-06-20 14:43:20 +000020081
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020082 /* Restore ids */
20083 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020084
Eric Biedermanf96a8102003-06-16 16:57:34 +000020085 /* Cleanup the temporary data structures */
20086 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020087
Eric Biedermanf96a8102003-06-16 16:57:34 +000020088 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020089 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020090
Eric Biedermanf96a8102003-06-16 16:57:34 +000020091 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020092 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020093
Eric Biederman153ea352003-06-20 14:43:20 +000020094 /* Fix two simultaneous uses of the same register.
20095 * In a few pathlogical cases a partial untangle moves
20096 * the tangle to a part of the graph we won't revisit.
20097 * So we keep looping until we have no more tangle fixes
20098 * to apply.
20099 */
20100 do {
20101 tangles = correct_tangles(state, rstate.blocks);
20102 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020103
Stefan Reinauer14e22772010-04-27 06:56:47 +000020104
Eric Biederman90089602004-05-28 14:11:54 +000020105 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020106 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020107
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020108 /* Allocate and initialize the live ranges */
20109 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020110
Stefan Reinauer14e22772010-04-27 06:56:47 +000020111 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020112 * buys me nothing. The code is left this way in case
20113 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020114 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020115 */
20116 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020117 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020118 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020119 }
20120
Eric Biederman153ea352003-06-20 14:43:20 +000020121 /* Remove any previous live edge calculations */
20122 cleanup_live_edges(&rstate);
20123
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020124 /* Compute the interference graph */
20125 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020126 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020127 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020128
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020129 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020130 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020131 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020132 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020133 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020134 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020135 print_interference_ins, &rstate);
20136 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020137
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020138 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020139
Eric Biederman5ade04a2003-10-22 04:03:46 +000020140 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020141 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020142 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020143 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020144
Eric Biederman83b991a2003-10-11 06:20:25 +000020145#if DEBUG_CONSISTENCY > 1
20146# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020147 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020148# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020149 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020150 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020151 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020152 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020153# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020154 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020155#endif
20156#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020157
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020158 /* Build the groups low and high. But with the nodes
20159 * first sorted by degree order.
20160 */
20161 rstate.low_tail = &rstate.low;
20162 rstate.high_tail = &rstate.high;
20163 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20164 if (rstate.high) {
20165 rstate.high->group_prev = &rstate.high;
20166 }
20167 for(point = &rstate.high; *point; point = &(*point)->group_next)
20168 ;
20169 rstate.high_tail = point;
20170 /* Walk through the high list and move everything that needs
20171 * to be onto low.
20172 */
20173 for(point = &rstate.high; *point; point = next) {
20174 struct live_range *range;
20175 next = &(*point)->group_next;
20176 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020177
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020178 /* If it has a low degree or it already has a color
20179 * place the node in low.
20180 */
20181 if ((range->degree < regc_max_size(state, range->classes)) ||
20182 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020183 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020184 range - rstate.lr, range->degree,
20185 (range->color != REG_UNSET) ? " (colored)": "");
20186 *range->group_prev = range->group_next;
20187 if (range->group_next) {
20188 range->group_next->group_prev = range->group_prev;
20189 }
20190 if (&range->group_next == rstate.high_tail) {
20191 rstate.high_tail = range->group_prev;
20192 }
20193 range->group_prev = rstate.low_tail;
20194 range->group_next = 0;
20195 *rstate.low_tail = range;
20196 rstate.low_tail = &range->group_next;
20197 next = point;
20198 }
20199 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020200 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020201 range - rstate.lr, range->degree,
20202 (range->color != REG_UNSET) ? " (colored)": "");
20203 }
20204 }
20205 /* Color the live_ranges */
20206 colored = color_graph(state, &rstate);
20207 rstate.passes++;
20208 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020209
Eric Biedermana96d6a92003-05-13 20:45:19 +000020210 /* Verify the graph was properly colored */
20211 verify_colors(state, &rstate);
20212
Eric Biedermanb138ac82003-04-22 18:44:01 +000020213 /* Move the colors from the graph to the triples */
20214 color_triples(state, &rstate);
20215
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020216 /* Cleanup the temporary data structures */
20217 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020218
20219 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020220 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020221}
20222
20223/* Sparce Conditional Constant Propogation
20224 * =========================================
20225 */
20226struct ssa_edge;
20227struct flow_block;
20228struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020229 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020230 struct triple *def;
20231 struct ssa_edge *out;
20232 struct flow_block *fblock;
20233 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020234 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020235 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020236 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020237 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020238};
20239struct ssa_edge {
20240 struct lattice_node *src;
20241 struct lattice_node *dst;
20242 struct ssa_edge *work_next;
20243 struct ssa_edge *work_prev;
20244 struct ssa_edge *out_next;
20245};
20246struct flow_edge {
20247 struct flow_block *src;
20248 struct flow_block *dst;
20249 struct flow_edge *work_next;
20250 struct flow_edge *work_prev;
20251 struct flow_edge *in_next;
20252 struct flow_edge *out_next;
20253 int executable;
20254};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020255#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020256struct flow_block {
20257 struct block *block;
20258 struct flow_edge *in;
20259 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020260 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020261};
20262
20263struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020264 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020265 struct lattice_node *lattice;
20266 struct ssa_edge *ssa_edges;
20267 struct flow_block *flow_blocks;
20268 struct flow_edge *flow_work_list;
20269 struct ssa_edge *ssa_work_list;
20270};
20271
20272
Eric Biederman90089602004-05-28 14:11:54 +000020273static int is_scc_const(struct compile_state *state, struct triple *ins)
20274{
20275 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20276}
20277
20278static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20279{
20280 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20281}
20282
20283static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20284{
20285 return is_scc_const(state, lnode->val);
20286}
20287
20288static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20289{
20290 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20291}
20292
Stefan Reinauer14e22772010-04-27 06:56:47 +000020293static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020294 struct flow_edge *fedge)
20295{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020296 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020297 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020298 fedge,
20299 fedge->src->block?fedge->src->block->last->id: 0,
20300 fedge->dst->block?fedge->dst->block->first->id: 0);
20301 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020302 if ((fedge == scc->flow_work_list) ||
20303 (fedge->work_next != fedge) ||
20304 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020305
20306 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020307 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020308 fedge);
20309 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020310 return;
20311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020312 if (!scc->flow_work_list) {
20313 scc->flow_work_list = fedge;
20314 fedge->work_next = fedge->work_prev = fedge;
20315 }
20316 else {
20317 struct flow_edge *ftail;
20318 ftail = scc->flow_work_list->work_prev;
20319 fedge->work_next = ftail->work_next;
20320 fedge->work_prev = ftail;
20321 fedge->work_next->work_prev = fedge;
20322 fedge->work_prev->work_next = fedge;
20323 }
20324}
20325
20326static struct flow_edge *scc_next_fedge(
20327 struct compile_state *state, struct scc_state *scc)
20328{
20329 struct flow_edge *fedge;
20330 fedge = scc->flow_work_list;
20331 if (fedge) {
20332 fedge->work_next->work_prev = fedge->work_prev;
20333 fedge->work_prev->work_next = fedge->work_next;
20334 if (fedge->work_next != fedge) {
20335 scc->flow_work_list = fedge->work_next;
20336 } else {
20337 scc->flow_work_list = 0;
20338 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020339 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020340 }
20341 return fedge;
20342}
20343
20344static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20345 struct ssa_edge *sedge)
20346{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020347 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020348 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20349 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020350 sedge->src->def->id,
20351 sedge->dst->def->id);
20352 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020353 if ((sedge == scc->ssa_work_list) ||
20354 (sedge->work_next != sedge) ||
20355 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020356
20357 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020358 fprintf(state->errout, "dupped sedge: %5ld\n",
20359 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020360 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020361 return;
20362 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020363 if (!scc->ssa_work_list) {
20364 scc->ssa_work_list = sedge;
20365 sedge->work_next = sedge->work_prev = sedge;
20366 }
20367 else {
20368 struct ssa_edge *stail;
20369 stail = scc->ssa_work_list->work_prev;
20370 sedge->work_next = stail->work_next;
20371 sedge->work_prev = stail;
20372 sedge->work_next->work_prev = sedge;
20373 sedge->work_prev->work_next = sedge;
20374 }
20375}
20376
20377static struct ssa_edge *scc_next_sedge(
20378 struct compile_state *state, struct scc_state *scc)
20379{
20380 struct ssa_edge *sedge;
20381 sedge = scc->ssa_work_list;
20382 if (sedge) {
20383 sedge->work_next->work_prev = sedge->work_prev;
20384 sedge->work_prev->work_next = sedge->work_next;
20385 if (sedge->work_next != sedge) {
20386 scc->ssa_work_list = sedge->work_next;
20387 } else {
20388 scc->ssa_work_list = 0;
20389 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020390 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020391 }
20392 return sedge;
20393}
20394
20395static void initialize_scc_state(
20396 struct compile_state *state, struct scc_state *scc)
20397{
20398 int ins_count, ssa_edge_count;
20399 int ins_index, ssa_edge_index, fblock_index;
20400 struct triple *first, *ins;
20401 struct block *block;
20402 struct flow_block *fblock;
20403
20404 memset(scc, 0, sizeof(*scc));
20405
20406 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020407 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020408 ins = first;
20409 ins_count = ssa_edge_count = 0;
20410 do {
20411 struct triple_set *edge;
20412 ins_count += 1;
20413 for(edge = ins->use; edge; edge = edge->next) {
20414 ssa_edge_count++;
20415 }
20416 ins = ins->next;
20417 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020418 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020419 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20420 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020421 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020422 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020423 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020424 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020425 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020426 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020427 scc->flow_blocks =
20428 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020429 "flow_blocks");
20430
20431 /* Initialize pass one collect up the nodes */
20432 fblock = 0;
20433 block = 0;
20434 ins_index = ssa_edge_index = fblock_index = 0;
20435 ins = first;
20436 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020437 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20438 block = ins->u.block;
20439 if (!block) {
20440 internal_error(state, ins, "label without block");
20441 }
20442 fblock_index += 1;
20443 block->vertex = fblock_index;
20444 fblock = &scc->flow_blocks[fblock_index];
20445 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020446 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20447 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020448 }
20449 {
20450 struct lattice_node *lnode;
20451 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020452 lnode = &scc->lattice[ins_index];
20453 lnode->def = ins;
20454 lnode->out = 0;
20455 lnode->fblock = fblock;
20456 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020457 if (lnode->val->op == OP_UNKNOWNVAL) {
20458 lnode->val = 0; /* LATTICE LOW by definition */
20459 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020460 lnode->old_id = ins->id;
20461 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020462 }
20463 ins = ins->next;
20464 } while(ins != first);
20465 /* Initialize pass two collect up the edges */
20466 block = 0;
20467 fblock = 0;
20468 ins = first;
20469 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020470 {
20471 struct triple_set *edge;
20472 struct ssa_edge **stail;
20473 struct lattice_node *lnode;
20474 lnode = &scc->lattice[ins->id];
20475 lnode->out = 0;
20476 stail = &lnode->out;
20477 for(edge = ins->use; edge; edge = edge->next) {
20478 struct ssa_edge *sedge;
20479 ssa_edge_index += 1;
20480 sedge = &scc->ssa_edges[ssa_edge_index];
20481 *stail = sedge;
20482 stail = &sedge->out_next;
20483 sedge->src = lnode;
20484 sedge->dst = &scc->lattice[edge->member->id];
20485 sedge->work_next = sedge->work_prev = sedge;
20486 sedge->out_next = 0;
20487 }
20488 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020489 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20490 struct flow_edge *fedge, **ftail;
20491 struct block_set *bedge;
20492 block = ins->u.block;
20493 fblock = &scc->flow_blocks[block->vertex];
20494 fblock->in = 0;
20495 fblock->out = 0;
20496 ftail = &fblock->out;
20497
20498 fedge = fblock->edges;
20499 bedge = block->edges;
20500 for(; bedge; bedge = bedge->next, fedge++) {
20501 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20502 if (fedge->dst->block != bedge->member) {
20503 internal_error(state, 0, "block mismatch");
20504 }
20505 *ftail = fedge;
20506 ftail = &fedge->out_next;
20507 fedge->out_next = 0;
20508 }
20509 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20510 fedge->src = fblock;
20511 fedge->work_next = fedge->work_prev = fedge;
20512 fedge->executable = 0;
20513 }
20514 }
20515 ins = ins->next;
20516 } while (ins != first);
20517 block = 0;
20518 fblock = 0;
20519 ins = first;
20520 do {
20521 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20522 struct flow_edge **ftail;
20523 struct block_set *bedge;
20524 block = ins->u.block;
20525 fblock = &scc->flow_blocks[block->vertex];
20526 ftail = &fblock->in;
20527 for(bedge = block->use; bedge; bedge = bedge->next) {
20528 struct block *src_block;
20529 struct flow_block *sfblock;
20530 struct flow_edge *sfedge;
20531 src_block = bedge->member;
20532 sfblock = &scc->flow_blocks[src_block->vertex];
20533 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20534 if (sfedge->dst == fblock) {
20535 break;
20536 }
20537 }
20538 if (!sfedge) {
20539 internal_error(state, 0, "edge mismatch");
20540 }
20541 *ftail = sfedge;
20542 ftail = &sfedge->in_next;
20543 sfedge->in_next = 0;
20544 }
20545 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020546 ins = ins->next;
20547 } while(ins != first);
20548 /* Setup a dummy block 0 as a node above the start node */
20549 {
20550 struct flow_block *fblock, *dst;
20551 struct flow_edge *fedge;
20552 fblock = &scc->flow_blocks[0];
20553 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020554 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020555 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020556 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020557 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020558 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020559 fedge->src = fblock;
20560 fedge->dst = dst;
20561 fedge->work_next = fedge;
20562 fedge->work_prev = fedge;
20563 fedge->in_next = fedge->dst->in;
20564 fedge->out_next = 0;
20565 fedge->executable = 0;
20566 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020567
Eric Biedermanb138ac82003-04-22 18:44:01 +000020568 /* Initialize the work lists */
20569 scc->flow_work_list = 0;
20570 scc->ssa_work_list = 0;
20571 scc_add_fedge(state, scc, fedge);
20572 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020573 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020574 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020575 ins_index, ssa_edge_index, fblock_index);
20576 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020577}
20578
Stefan Reinauer14e22772010-04-27 06:56:47 +000020579
Eric Biedermanb138ac82003-04-22 18:44:01 +000020580static void free_scc_state(
20581 struct compile_state *state, struct scc_state *scc)
20582{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020583 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020584 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020585 struct flow_block *fblock;
20586 fblock = &scc->flow_blocks[i];
20587 if (fblock->edges) {
20588 xfree(fblock->edges);
20589 fblock->edges = 0;
20590 }
20591 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020592 xfree(scc->flow_blocks);
20593 xfree(scc->ssa_edges);
20594 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020595
Eric Biedermanb138ac82003-04-22 18:44:01 +000020596}
20597
20598static struct lattice_node *triple_to_lattice(
20599 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20600{
20601 if (ins->id <= 0) {
20602 internal_error(state, ins, "bad id");
20603 }
20604 return &scc->lattice[ins->id];
20605}
20606
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020607static struct triple *preserve_lval(
20608 struct compile_state *state, struct lattice_node *lnode)
20609{
20610 struct triple *old;
20611 /* Preserve the original value */
20612 if (lnode->val) {
20613 old = dup_triple(state, lnode->val);
20614 if (lnode->val != lnode->def) {
20615 xfree(lnode->val);
20616 }
20617 lnode->val = 0;
20618 } else {
20619 old = 0;
20620 }
20621 return old;
20622}
20623
Stefan Reinauer14e22772010-04-27 06:56:47 +000020624static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020625 struct triple *old, struct lattice_node *lnode)
20626{
20627 int changed;
20628 /* See if the lattice value has changed */
20629 changed = 1;
20630 if (!old && !lnode->val) {
20631 changed = 0;
20632 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020633 if (changed &&
20634 lnode->val && old &&
20635 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020636 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020637 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20638 changed = 0;
20639 }
20640 if (old) {
20641 xfree(old);
20642 }
20643 return changed;
20644
20645}
20646
Eric Biederman5ade04a2003-10-22 04:03:46 +000020647static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020648 struct compile_state *state, struct scc_state *scc,
20649 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020650{
Eric Biederman90089602004-05-28 14:11:54 +000020651 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20652 display_triple_changes(state->errout, lnode->val, lnode->def);
20653 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020654 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020655 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020656 struct triple *val, **expr;
20657 val = lnode->val? lnode->val : lnode->def;
20658 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020659 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020660 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20661 lnode->def->id,
20662 tops(lnode->def->op));
20663 expr = triple_rhs(state, lnode->def, 0);
20664 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20665 if (*expr) {
20666 fprintf(fp, " %d", (*expr)->id);
20667 }
20668 }
20669 if (val->op == OP_INTCONST) {
20670 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20671 }
20672 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020673 (is_lattice_hi(state, lnode)? "hi":
20674 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020675 changed? "changed" : ""
20676 );
20677 }
20678}
20679
Eric Biedermanb138ac82003-04-22 18:44:01 +000020680static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20681 struct lattice_node *lnode)
20682{
20683 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020684 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020685 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020686 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020687
Eric Biedermanb138ac82003-04-22 18:44:01 +000020688 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020689 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020690
Eric Biedermanb138ac82003-04-22 18:44:01 +000020691 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020692 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020693 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694 scratch->next = scratch;
20695 scratch->prev = scratch;
20696 scratch->use = 0;
20697
Eric Biederman90089602004-05-28 14:11:54 +000020698 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020699 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020700 dexpr = &lnode->def->param[i];
20701 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020702 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020703 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20704 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020705 *dexpr) {
20706 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020707 tmp = triple_to_lattice(state, scc, *dexpr);
20708 *vexpr = (tmp->val)? tmp->val : tmp->def;
20709 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020710 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020711 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020712 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020713 }
20714 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020715#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020716#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020717#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020718 /* So far it looks like only the strength reduction
20719 * optimization are things I need to worry about.
20720 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020721 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020722 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020723 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020724 internal_error(state, lnode->def, "scratch used?");
20725 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020726 if ((scratch->prev != scratch) ||
20727 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020728 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020729 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020730 internal_error(state, lnode->def, "scratch in list?");
20731 }
20732 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020733 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020734 for(i = 0; i < count; i++) {
20735 vexpr = &scratch->param[i];
20736 if (*vexpr) {
20737 unuse_triple(*vexpr, scratch);
20738 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020739 }
Eric Biederman90089602004-05-28 14:11:54 +000020740 if (lnode->val->op == OP_UNKNOWNVAL) {
20741 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020742 }
Eric Biederman90089602004-05-28 14:11:54 +000020743 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020744 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020745 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020746 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020747 count * sizeof(lnode->val->param[0])) == 0) &&
20748 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020749 lnode->val = lnode->def;
20750 }
Eric Biederman90089602004-05-28 14:11:54 +000020751 /* Only allow lattice high when all of my inputs
20752 * are also lattice high. Occassionally I can
20753 * have constants with a lattice low input, so
20754 * I do not need to check that case.
20755 */
20756 if (is_lattice_hi(state, lnode)) {
20757 struct lattice_node *tmp;
20758 int rhs;
20759 rhs = lnode->val->rhs;
20760 for(i = 0; i < rhs; i++) {
20761 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20762 if (!is_lattice_hi(state, tmp)) {
20763 lnode->val = 0;
20764 break;
20765 }
20766 }
20767 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020768 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020769 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020770 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020771 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020772 lnode->val = 0;
20773 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020774 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020775 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020776 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020777 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020778 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020779 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020780 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020781#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020782#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020783#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020784 if (changed) {
20785 internal_warning(state, lnode->def, "non def changes value?");
20786 }
20787 lnode->val = 0;
20788 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020789
Eric Biederman83b991a2003-10-11 06:20:25 +000020790 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020791 if (lnode->val != scratch) {
20792 xfree(scratch);
20793 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020794
Eric Biedermanb138ac82003-04-22 18:44:01 +000020795 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020796}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020797
Eric Biederman90089602004-05-28 14:11:54 +000020798
20799static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020800 struct lattice_node *lnode)
20801{
20802 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020803 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020804 int changed;
20805
20806 /* Update the branch value */
20807 changed = compute_lnode_val(state, scc, lnode);
20808 scc_debug_lnode(state, scc, lnode, changed);
20809
20810 /* This only applies to conditional branches */
20811 if (!triple_is_cbranch(state, lnode->def)) {
20812 internal_error(state, lnode->def, "not a conditional branch");
20813 }
20814
Eric Biederman5ade04a2003-10-22 04:03:46 +000020815 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020816 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020817 FILE *fp = state->errout;
20818 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020819 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020820 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020821
Eric Biedermanb138ac82003-04-22 18:44:01 +000020822 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020823 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 }
Eric Biederman90089602004-05-28 14:11:54 +000020825 fprintf(fp, " )");
20826 if (lnode->def->rhs > 0) {
20827 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020828 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020829 }
Eric Biederman90089602004-05-28 14:11:54 +000020830 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020831 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020832 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020833 for(left = cond->fblock->out; left; left = left->out_next) {
20834 if (left->dst->block->first == lnode->def->next) {
20835 break;
20836 }
20837 }
20838 if (!left) {
20839 internal_error(state, lnode->def, "Cannot find left branch edge");
20840 }
20841 for(right = cond->fblock->out; right; right = right->out_next) {
20842 if (right->dst->block->first == TARG(lnode->def, 0)) {
20843 break;
20844 }
20845 }
20846 if (!right) {
20847 internal_error(state, lnode->def, "Cannot find right branch edge");
20848 }
Eric Biederman90089602004-05-28 14:11:54 +000020849 /* I should only come here if the controlling expressions value
20850 * has changed, which means it must be either a constant or lo.
20851 */
20852 if (is_lattice_hi(state, cond)) {
20853 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020854 return;
20855 }
Eric Biederman90089602004-05-28 14:11:54 +000020856 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020857 scc_add_fedge(state, scc, left);
20858 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020859 }
20860 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020861 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020862 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020863 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020864 }
20865
20866}
20867
Eric Biederman90089602004-05-28 14:11:54 +000020868
Stefan Reinauer14e22772010-04-27 06:56:47 +000020869static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020870 struct scc_state *scc, struct ssa_edge *sedge)
20871{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020872 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020873 scc_visit_cbranch(state, scc, sedge->dst);
20874 }
20875 else if (triple_is_def(state, sedge->dst->def)) {
20876 scc_add_sedge(state, scc, sedge);
20877 }
20878}
20879
Stefan Reinauer14e22772010-04-27 06:56:47 +000020880static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020881 struct lattice_node *lnode)
20882{
20883 struct lattice_node *tmp;
20884 struct triple **slot, *old;
20885 struct flow_edge *fedge;
20886 int changed;
20887 int index;
20888 if (lnode->def->op != OP_PHI) {
20889 internal_error(state, lnode->def, "not phi");
20890 }
20891 /* Store the original value */
20892 old = preserve_lval(state, lnode);
20893
20894 /* default to lattice high */
20895 lnode->val = lnode->def;
20896 slot = &RHS(lnode->def, 0);
20897 index = 0;
20898 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20899 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020900 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020901 index,
20902 fedge->dst->block->vertex,
20903 fedge->executable
20904 );
20905 }
20906 if (!fedge->executable) {
20907 continue;
20908 }
20909 if (!slot[index]) {
20910 internal_error(state, lnode->def, "no phi value");
20911 }
20912 tmp = triple_to_lattice(state, scc, slot[index]);
20913 /* meet(X, lattice low) = lattice low */
20914 if (is_lattice_lo(state, tmp)) {
20915 lnode->val = 0;
20916 }
20917 /* meet(X, lattice high) = X */
20918 else if (is_lattice_hi(state, tmp)) {
Eric Biederman90089602004-05-28 14:11:54 +000020919 }
20920 /* meet(lattice high, X) = X */
20921 else if (is_lattice_hi(state, lnode)) {
20922 lnode->val = dup_triple(state, tmp->val);
20923 /* Only change the type if necessary */
20924 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20925 lnode->val->type = lnode->def->type;
20926 }
20927 }
20928 /* meet(const, const) = const or lattice low */
20929 else if (!constants_equal(state, lnode->val, tmp->val)) {
20930 lnode->val = 0;
20931 }
20932
20933 /* meet(lattice low, X) = lattice low */
20934 if (is_lattice_lo(state, lnode)) {
20935 lnode->val = 0;
20936 break;
20937 }
20938 }
20939 changed = lval_changed(state, old, lnode);
20940 scc_debug_lnode(state, scc, lnode, changed);
20941
20942 /* If the lattice value has changed update the work lists. */
20943 if (changed) {
20944 struct ssa_edge *sedge;
20945 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20946 scc_add_sedge_dst(state, scc, sedge);
20947 }
20948 }
20949}
20950
20951
Eric Biedermanb138ac82003-04-22 18:44:01 +000020952static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20953 struct lattice_node *lnode)
20954{
20955 int changed;
20956
Eric Biederman90089602004-05-28 14:11:54 +000020957 if (!triple_is_def(state, lnode->def)) {
20958 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020959 }
Eric Biederman90089602004-05-28 14:11:54 +000020960 changed = compute_lnode_val(state, scc, lnode);
20961 scc_debug_lnode(state, scc, lnode, changed);
20962
20963 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020964 struct ssa_edge *sedge;
20965 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020966 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020967 }
20968 }
20969}
20970
20971static void scc_writeback_values(
20972 struct compile_state *state, struct scc_state *scc)
20973{
20974 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020975 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020976 ins = first;
20977 do {
20978 struct lattice_node *lnode;
20979 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020980 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020981 if (is_lattice_hi(state, lnode) &&
20982 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020983 {
20984 struct flow_edge *fedge;
20985 int executable;
20986 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020987 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020988 !executable && fedge; fedge = fedge->in_next) {
20989 executable |= fedge->executable;
20990 }
20991 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020992 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020993 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020994 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020995 tops(lnode->def->op),
20996 tops(lnode->val->op));
20997 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020998 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020999 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021000
Eric Biederman83b991a2003-10-11 06:20:25 +000021001 /* Restore id */
21002 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021003 if (lnode->val && (lnode->val != ins)) {
21004 /* See if it something I know how to write back */
21005 switch(lnode->val->op) {
21006 case OP_INTCONST:
21007 mkconst(state, ins, lnode->val->u.cval);
21008 break;
21009 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021010 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021011 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021012 break;
21013 default:
21014 /* By default don't copy the changes,
21015 * recompute them in place instead.
21016 */
21017 simplify(state, ins);
21018 break;
21019 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021020 if (is_const(lnode->val) &&
21021 !constants_equal(state, lnode->val, ins)) {
21022 internal_error(state, 0, "constants not equal");
21023 }
21024 /* Free the lattice nodes */
21025 xfree(lnode->val);
21026 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021027 }
21028 ins = ins->next;
21029 } while(ins != first);
21030}
21031
21032static void scc_transform(struct compile_state *state)
21033{
21034 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021035 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21036 return;
21037 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021038
21039 initialize_scc_state(state, &scc);
21040
21041 while(scc.flow_work_list || scc.ssa_work_list) {
21042 struct flow_edge *fedge;
21043 struct ssa_edge *sedge;
21044 struct flow_edge *fptr;
21045 while((fedge = scc_next_fedge(state, &scc))) {
21046 struct block *block;
21047 struct triple *ptr;
21048 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021049 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021050 int done;
21051 if (fedge->executable) {
21052 continue;
21053 }
21054 if (!fedge->dst) {
21055 internal_error(state, 0, "fedge without dst");
21056 }
21057 if (!fedge->src) {
21058 internal_error(state, 0, "fedge without src");
21059 }
21060 fedge->executable = 1;
21061 fblock = fedge->dst;
21062 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021063 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021064 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21065 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021066 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021067 }
21068 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021069
Eric Biederman5ade04a2003-10-22 04:03:46 +000021070 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021071 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021072 block->vertex, reps);
21073 }
21074
Eric Biedermanb138ac82003-04-22 18:44:01 +000021075 done = 0;
21076 for(ptr = block->first; !done; ptr = ptr->next) {
21077 struct lattice_node *lnode;
21078 done = (ptr == block->last);
21079 lnode = &scc.lattice[ptr->id];
21080 if (ptr->op == OP_PHI) {
21081 scc_visit_phi(state, &scc, lnode);
21082 }
Eric Biederman90089602004-05-28 14:11:54 +000021083 else if ((reps == 1) && triple_is_def(state, ptr))
21084 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021085 scc_visit_expr(state, &scc, lnode);
21086 }
21087 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021088 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021089 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021090 struct flow_edge *out;
21091 for(out = fblock->out; out; out = out->out_next) {
21092 scc_add_fedge(state, &scc, out);
21093 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021094 }
21095 }
21096 while((sedge = scc_next_sedge(state, &scc))) {
21097 struct lattice_node *lnode;
21098 struct flow_block *fblock;
21099 lnode = sedge->dst;
21100 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021101
21102 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021103 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021104 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021105 sedge->src->def->id,
21106 sedge->dst->def->id);
21107 }
21108
Eric Biedermanb138ac82003-04-22 18:44:01 +000021109 if (lnode->def->op == OP_PHI) {
21110 scc_visit_phi(state, &scc, lnode);
21111 }
21112 else {
21113 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21114 if (fptr->executable) {
21115 break;
21116 }
21117 }
21118 if (fptr) {
21119 scc_visit_expr(state, &scc, lnode);
21120 }
21121 }
21122 }
21123 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021124
Eric Biedermanb138ac82003-04-22 18:44:01 +000021125 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021126 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021127 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021128
Eric Biederman90089602004-05-28 14:11:54 +000021129 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021130}
21131
21132
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021133static void transform_to_arch_instructions(struct compile_state *state)
21134{
21135 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021136 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021137 ins = first;
21138 do {
21139 ins = transform_to_arch_instruction(state, ins);
21140 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021141
Eric Biederman90089602004-05-28 14:11:54 +000021142 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021143}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021144
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021145#if DEBUG_CONSISTENCY
21146static void verify_uses(struct compile_state *state)
21147{
21148 struct triple *first, *ins;
21149 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021150 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021151 ins = first;
21152 do {
21153 struct triple **expr;
21154 expr = triple_rhs(state, ins, 0);
21155 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021156 struct triple *rhs;
21157 rhs = *expr;
21158 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021159 if (set->member == ins) {
21160 break;
21161 }
21162 }
21163 if (!set) {
21164 internal_error(state, ins, "rhs not used");
21165 }
21166 }
21167 expr = triple_lhs(state, ins, 0);
21168 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021169 struct triple *lhs;
21170 lhs = *expr;
21171 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021172 if (set->member == ins) {
21173 break;
21174 }
21175 }
21176 if (!set) {
21177 internal_error(state, ins, "lhs not used");
21178 }
21179 }
Eric Biederman90089602004-05-28 14:11:54 +000021180 expr = triple_misc(state, ins, 0);
21181 if (ins->op != OP_PHI) {
21182 for(; expr; expr = triple_targ(state, ins, expr)) {
21183 struct triple *misc;
21184 misc = *expr;
21185 for(set = misc?misc->use:0; set; set = set->next) {
21186 if (set->member == ins) {
21187 break;
21188 }
21189 }
21190 if (!set) {
21191 internal_error(state, ins, "misc not used");
21192 }
21193 }
21194 }
21195 if (!triple_is_ret(state, ins)) {
21196 expr = triple_targ(state, ins, 0);
21197 for(; expr; expr = triple_targ(state, ins, expr)) {
21198 struct triple *targ;
21199 targ = *expr;
21200 for(set = targ?targ->use:0; set; set = set->next) {
21201 if (set->member == ins) {
21202 break;
21203 }
21204 }
21205 if (!set) {
21206 internal_error(state, ins, "targ not used");
21207 }
21208 }
21209 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021210 ins = ins->next;
21211 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021212
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021213}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021214static void verify_blocks_present(struct compile_state *state)
21215{
21216 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021217 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021218 return;
21219 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021220 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021221 ins = first;
21222 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021223 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021224 if (triple_stores_block(state, ins)) {
21225 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021226 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021227 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021228 }
21229 }
21230 ins = ins->next;
21231 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021232
21233
Eric Biedermand1ea5392003-06-28 06:49:45 +000021234}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021235
21236static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21237{
21238 struct block_set *bedge;
21239 struct block *targ;
21240 targ = block_of_triple(state, edge);
21241 for(bedge = block->edges; bedge; bedge = bedge->next) {
21242 if (bedge->member == targ) {
21243 return 1;
21244 }
21245 }
21246 return 0;
21247}
21248
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021249static void verify_blocks(struct compile_state *state)
21250{
21251 struct triple *ins;
21252 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021253 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021254 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021255 if (!block) {
21256 return;
21257 }
Eric Biederman530b5192003-07-01 10:05:30 +000021258 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021259 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021260 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021261 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021262 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021263 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021264 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021265 internal_error(state, ins, "inconsitent block specified");
21266 }
Eric Biederman530b5192003-07-01 10:05:30 +000021267 valid_ins(state, ins);
21268 }
21269 users = 0;
21270 for(user = block->use; user; user = user->next) {
21271 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021272 if (!user->member->first) {
21273 internal_error(state, block->first, "user is empty");
21274 }
Eric Biederman90089602004-05-28 14:11:54 +000021275 if ((block == state->bb.last_block) &&
21276 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021277 continue;
21278 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021279 for(edge = user->member->edges; edge; edge = edge->next) {
21280 if (edge->member == block) {
21281 break;
21282 }
21283 }
21284 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021285 internal_error(state, user->member->first,
21286 "user does not use block");
21287 }
21288 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021289 if (triple_is_branch(state, block->last)) {
21290 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021291 expr = triple_edge_targ(state, block->last, 0);
21292 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021293 if (*expr && !edge_present(state, block, *expr)) {
21294 internal_error(state, block->last, "no edge to targ");
21295 }
21296 }
Eric Biederman530b5192003-07-01 10:05:30 +000021297 }
Eric Biederman90089602004-05-28 14:11:54 +000021298 if (!triple_is_ubranch(state, block->last) &&
21299 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021300 !edge_present(state, block, block->last->next)) {
21301 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021302 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021303 for(edge = block->edges; edge; edge = edge->next) {
21304 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021305 if (user->member == block) {
21306 break;
21307 }
21308 }
21309 if (!user || user->member != block) {
21310 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021311 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021312 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021313 if (!edge->member->first) {
21314 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021315 }
Eric Biederman530b5192003-07-01 10:05:30 +000021316 }
21317 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021318 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021319 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021320 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021321 }
Patrick Georgided1e052016-12-15 15:21:56 +010021322 if (!(block->last->next) || !(block->last->next->u.block)) {
21323 internal_error(state, block->last,
21324 "bad next block");
21325 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021326 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021327 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021328 "cannot find next block");
21329 }
21330 block = block->last->next->u.block;
Eric Biederman90089602004-05-28 14:11:54 +000021331 } while(block != state->bb.first_block);
21332 if (blocks != state->bb.last_vertex) {
21333 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21334 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021335 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021336}
21337
21338static void verify_domination(struct compile_state *state)
21339{
21340 struct triple *first, *ins;
21341 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021342 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021343 return;
21344 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021345
Eric Biederman83b991a2003-10-11 06:20:25 +000021346 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021347 ins = first;
21348 do {
21349 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021350 struct triple **slot;
21351 struct triple *use_point;
21352 int i, zrhs;
21353 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021354 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021355 slot = &RHS(set->member, 0);
21356 /* See if the use is on the right hand side */
21357 for(i = 0; i < zrhs; i++) {
21358 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021359 break;
21360 }
21361 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021362 if (i < zrhs) {
21363 use_point = set->member;
21364 if (set->member->op == OP_PHI) {
21365 struct block_set *bset;
21366 int edge;
21367 bset = set->member->u.block->use;
21368 for(edge = 0; bset && (edge < i); edge++) {
21369 bset = bset->next;
21370 }
21371 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021372 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021373 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021374 }
21375 use_point = bset->member->last;
21376 }
21377 }
21378 if (use_point &&
21379 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021380 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021381 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021382 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021383 }
21384 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021385 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021386 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021387 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021388 }
21389 }
21390 ins = ins->next;
21391 } while(ins != first);
21392}
21393
Eric Biederman83b991a2003-10-11 06:20:25 +000021394static void verify_rhs(struct compile_state *state)
21395{
21396 struct triple *first, *ins;
21397 first = state->first;
21398 ins = first;
21399 do {
21400 struct triple **slot;
21401 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021402 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021403 slot = &RHS(ins, 0);
21404 for(i = 0; i < zrhs; i++) {
21405 if (slot[i] == 0) {
21406 internal_error(state, ins,
21407 "missing rhs %d on %s",
21408 i, tops(ins->op));
21409 }
21410 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21411 internal_error(state, ins,
21412 "ins == rhs[%d] on %s",
21413 i, tops(ins->op));
21414 }
21415 }
21416 ins = ins->next;
21417 } while(ins != first);
21418}
21419
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021420static void verify_piece(struct compile_state *state)
21421{
21422 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021423 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021424 ins = first;
21425 do {
21426 struct triple *ptr;
21427 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021428 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021429 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21430 if (ptr != LHS(ins, i)) {
21431 internal_error(state, ins, "malformed lhs on %s",
21432 tops(ins->op));
21433 }
21434 if (ptr->op != OP_PIECE) {
21435 internal_error(state, ins, "bad lhs op %s at %d on %s",
21436 tops(ptr->op), i, tops(ins->op));
21437 }
21438 if (ptr->u.cval != i) {
21439 internal_error(state, ins, "bad u.cval of %d %d expected",
21440 ptr->u.cval, i);
21441 }
21442 }
21443 ins = ins->next;
21444 } while(ins != first);
21445}
Eric Biederman83b991a2003-10-11 06:20:25 +000021446
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021447static void verify_ins_colors(struct compile_state *state)
21448{
21449 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021450
Eric Biederman83b991a2003-10-11 06:20:25 +000021451 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021452 ins = first;
21453 do {
21454 ins = ins->next;
21455 } while(ins != first);
21456}
Eric Biederman90089602004-05-28 14:11:54 +000021457
21458static void verify_unknown(struct compile_state *state)
21459{
21460 struct triple *first, *ins;
21461 if ( (unknown_triple.next != &unknown_triple) ||
21462 (unknown_triple.prev != &unknown_triple) ||
21463#if 0
21464 (unknown_triple.use != 0) ||
21465#endif
21466 (unknown_triple.op != OP_UNKNOWNVAL) ||
21467 (unknown_triple.lhs != 0) ||
21468 (unknown_triple.rhs != 0) ||
21469 (unknown_triple.misc != 0) ||
21470 (unknown_triple.targ != 0) ||
21471 (unknown_triple.template_id != 0) ||
21472 (unknown_triple.id != -1) ||
21473 (unknown_triple.type != &unknown_type) ||
21474 (unknown_triple.occurance != &dummy_occurance) ||
21475 (unknown_triple.param[0] != 0) ||
21476 (unknown_triple.param[1] != 0)) {
21477 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21478 }
21479 if ( (dummy_occurance.count != 2) ||
21480 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21481 (strcmp(dummy_occurance.function, "") != 0) ||
21482 (dummy_occurance.col != 0) ||
21483 (dummy_occurance.parent != 0)) {
21484 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21485 }
21486 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21487 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21488 }
21489 first = state->first;
21490 ins = first;
21491 do {
21492 int params, i;
21493 if (ins == &unknown_triple) {
21494 internal_error(state, ins, "unknown triple in list");
21495 }
21496 params = TRIPLE_SIZE(ins);
21497 for(i = 0; i < params; i++) {
21498 if (ins->param[i] == &unknown_triple) {
21499 internal_error(state, ins, "unknown triple used!");
21500 }
21501 }
21502 ins = ins->next;
21503 } while(ins != first);
21504}
21505
21506static void verify_types(struct compile_state *state)
21507{
21508 struct triple *first, *ins;
21509 first = state->first;
21510 ins = first;
21511 do {
21512 struct type *invalid;
21513 invalid = invalid_type(state, ins->type);
21514 if (invalid) {
21515 FILE *fp = state->errout;
21516 fprintf(fp, "type: ");
21517 name_of(fp, ins->type);
21518 fprintf(fp, "\n");
21519 fprintf(fp, "invalid type: ");
21520 name_of(fp, invalid);
21521 fprintf(fp, "\n");
21522 internal_error(state, ins, "invalid ins type");
21523 }
21524 } while(ins != first);
21525}
21526
21527static void verify_copy(struct compile_state *state)
21528{
21529 struct triple *first, *ins, *next;
21530 first = state->first;
21531 next = ins = first;
21532 do {
21533 ins = next;
21534 next = ins->next;
21535 if (ins->op != OP_COPY) {
21536 continue;
21537 }
21538 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21539 FILE *fp = state->errout;
21540 fprintf(fp, "src type: ");
21541 name_of(fp, RHS(ins, 0)->type);
21542 fprintf(fp, "\n");
21543 fprintf(fp, "dst type: ");
21544 name_of(fp, ins->type);
21545 fprintf(fp, "\n");
21546 internal_error(state, ins, "type mismatch in copy");
21547 }
21548 } while(next != first);
21549}
21550
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021551static void verify_consistency(struct compile_state *state)
21552{
Eric Biederman90089602004-05-28 14:11:54 +000021553 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021554 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021555 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021556 verify_blocks(state);
21557 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021558 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021559 verify_piece(state);
21560 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021561 verify_types(state);
21562 verify_copy(state);
21563 if (state->compiler->debug & DEBUG_VERIFICATION) {
21564 fprintf(state->dbgout, "consistency verified\n");
21565 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021566}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021567#else
Eric Biederman153ea352003-06-20 14:43:20 +000021568static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021569#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021570
21571static void optimize(struct compile_state *state)
21572{
Eric Biederman90089602004-05-28 14:11:54 +000021573 /* Join all of the functions into one giant function */
21574 join_functions(state);
21575
Eric Biederman5ade04a2003-10-22 04:03:46 +000021576 /* Dump what the instruction graph intially looks like */
21577 print_triples(state);
21578
Eric Biederman0babc1c2003-05-09 02:39:00 +000021579 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021580 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021581 print_triples(state);
21582
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021583 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021584 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021585 state->bb.first = state->first;
21586 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021587
Eric Biederman530b5192003-07-01 10:05:30 +000021588 /* Transform the code to ssa form. */
21589 /*
21590 * The transformation to ssa form puts a phi function
21591 * on each of edge of a dominance frontier where that
21592 * phi function might be needed. At -O2 if we don't
21593 * eleminate the excess phi functions we can get an
21594 * exponential code size growth. So I kill the extra
21595 * phi functions early and I kill them often.
21596 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021597 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021598 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021599
Eric Biederman83b991a2003-10-11 06:20:25 +000021600 /* Remove dead code */
21601 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021602 verify_consistency(state);
21603
Eric Biedermanb138ac82003-04-22 18:44:01 +000021604 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021605 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021606 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021607 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021608 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021609 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021610#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021611#warning "WISHLIST implement single use constants (least possible register pressure)"
21612#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021613#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021614 /* Select architecture instructions and an initial partial
21615 * coloring based on architecture constraints.
21616 */
21617 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021618 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021619
Eric Biederman83b991a2003-10-11 06:20:25 +000021620 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021621 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021622 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021623
Eric Biedermanb138ac82003-04-22 18:44:01 +000021624 /* Color all of the variables to see if they will fit in registers */
21625 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021626 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021627
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021628 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021629 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021630
Eric Biedermanb138ac82003-04-22 18:44:01 +000021631 allocate_registers(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 /* Remove the optimization information.
21635 * This is more to check for memory consistency than to free memory.
21636 */
Eric Biederman90089602004-05-28 14:11:54 +000021637 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021638}
21639
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021640static void print_op_asm(struct compile_state *state,
21641 struct triple *ins, FILE *fp)
21642{
21643 struct asm_info *info;
21644 const char *ptr;
21645 unsigned lhs, rhs, i;
21646 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021647 lhs = ins->lhs;
21648 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021649 /* Don't count the clobbers in lhs */
21650 for(i = 0; i < lhs; i++) {
21651 if (LHS(ins, i)->type == &void_type) {
21652 break;
21653 }
21654 }
21655 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021656 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021657 fputc('\t', fp);
21658 for(ptr = info->str; *ptr; ptr++) {
21659 char *next;
21660 unsigned long param;
21661 struct triple *piece;
21662 if (*ptr != '%') {
21663 fputc(*ptr, fp);
21664 continue;
21665 }
21666 ptr++;
21667 if (*ptr == '%') {
21668 fputc('%', fp);
21669 continue;
21670 }
21671 param = strtoul(ptr, &next, 10);
21672 if (ptr == next) {
21673 error(state, ins, "Invalid asm template");
21674 }
21675 if (param >= (lhs + rhs)) {
21676 error(state, ins, "Invalid param %%%u in asm template",
21677 param);
21678 }
21679 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021680 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021681 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021682 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021683 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021684 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021685}
21686
21687
21688/* Only use the low x86 byte registers. This allows me
21689 * allocate the entire register when a byte register is used.
21690 */
21691#define X86_4_8BIT_GPRS 1
21692
Eric Biederman83b991a2003-10-11 06:20:25 +000021693/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021694#define X86_MMX_REGS (1<<0)
21695#define X86_XMM_REGS (1<<1)
21696#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021697
Eric Biedermanb138ac82003-04-22 18:44:01 +000021698/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021699#define REGC_FLAGS 0
21700#define REGC_GPR8 1
21701#define REGC_GPR16 2
21702#define REGC_GPR32 3
21703#define REGC_DIVIDEND64 4
21704#define REGC_DIVIDEND32 5
21705#define REGC_MMX 6
21706#define REGC_XMM 7
21707#define REGC_GPR32_8 8
21708#define REGC_GPR16_8 9
21709#define REGC_GPR8_LO 10
21710#define REGC_IMM32 11
21711#define REGC_IMM16 12
21712#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021713#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021714#if LAST_REGC >= MAX_REGC
21715#error "MAX_REGC is to low"
21716#endif
21717
21718/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021719#define REGCM_FLAGS (1 << REGC_FLAGS)
21720#define REGCM_GPR8 (1 << REGC_GPR8)
21721#define REGCM_GPR16 (1 << REGC_GPR16)
21722#define REGCM_GPR32 (1 << REGC_GPR32)
21723#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21724#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21725#define REGCM_MMX (1 << REGC_MMX)
21726#define REGCM_XMM (1 << REGC_XMM)
21727#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21728#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21729#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21730#define REGCM_IMM32 (1 << REGC_IMM32)
21731#define REGCM_IMM16 (1 << REGC_IMM16)
21732#define REGCM_IMM8 (1 << REGC_IMM8)
21733#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021734#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021735
21736/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021737#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021738#define REGC_FLAGS_FIRST REG_EFLAGS
21739#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021740#define REG_AL 3
21741#define REG_BL 4
21742#define REG_CL 5
21743#define REG_DL 6
21744#define REG_AH 7
21745#define REG_BH 8
21746#define REG_CH 9
21747#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021748#define REGC_GPR8_LO_FIRST REG_AL
21749#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021750#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021751#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021752#define REG_AX 11
21753#define REG_BX 12
21754#define REG_CX 13
21755#define REG_DX 14
21756#define REG_SI 15
21757#define REG_DI 16
21758#define REG_BP 17
21759#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021760#define REGC_GPR16_FIRST REG_AX
21761#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021762#define REG_EAX 19
21763#define REG_EBX 20
21764#define REG_ECX 21
21765#define REG_EDX 22
21766#define REG_ESI 23
21767#define REG_EDI 24
21768#define REG_EBP 25
21769#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021770#define REGC_GPR32_FIRST REG_EAX
21771#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021772#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021773#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21774#define REGC_DIVIDEND64_LAST REG_EDXEAX
21775#define REG_DXAX 28
21776#define REGC_DIVIDEND32_FIRST REG_DXAX
21777#define REGC_DIVIDEND32_LAST REG_DXAX
21778#define REG_MMX0 29
21779#define REG_MMX1 30
21780#define REG_MMX2 31
21781#define REG_MMX3 32
21782#define REG_MMX4 33
21783#define REG_MMX5 34
21784#define REG_MMX6 35
21785#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021786#define REGC_MMX_FIRST REG_MMX0
21787#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021788#define REG_XMM0 37
21789#define REG_XMM1 38
21790#define REG_XMM2 39
21791#define REG_XMM3 40
21792#define REG_XMM4 41
21793#define REG_XMM5 42
21794#define REG_XMM6 43
21795#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021796#define REGC_XMM_FIRST REG_XMM0
21797#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021798
21799#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021800#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021801#endif
21802
Eric Biedermanb138ac82003-04-22 18:44:01 +000021803#define LAST_REG REG_XMM7
21804
21805#define REGC_GPR32_8_FIRST REG_EAX
21806#define REGC_GPR32_8_LAST REG_EDX
21807#define REGC_GPR16_8_FIRST REG_AX
21808#define REGC_GPR16_8_LAST REG_DX
21809
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021810#define REGC_IMM8_FIRST -1
21811#define REGC_IMM8_LAST -1
21812#define REGC_IMM16_FIRST -2
21813#define REGC_IMM16_LAST -1
21814#define REGC_IMM32_FIRST -4
21815#define REGC_IMM32_LAST -1
21816
Eric Biedermanb138ac82003-04-22 18:44:01 +000021817#if LAST_REG >= MAX_REGISTERS
21818#error "MAX_REGISTERS to low"
21819#endif
21820
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021821
21822static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021823 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21824 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21825 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21826 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21827 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21828 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21829 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21830 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21831 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21832 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21833 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21834 [REGC_IMM32] = 0,
21835 [REGC_IMM16] = 0,
21836 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021837};
21838
21839static const struct {
21840 int first, last;
21841} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021842 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21843 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21844 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21845 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21846 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21847 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21848 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21849 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21850 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21851 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21852 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21853 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21854 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21855 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021856};
21857
Eric Biederman90089602004-05-28 14:11:54 +000021858#if ARCH_INPUT_REGS != 4
21859#error ARCH_INPUT_REGS size mismatch
21860#endif
21861static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21862 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21863 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21864 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21865 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21866};
21867
21868#if ARCH_OUTPUT_REGS != 4
21869#error ARCH_INPUT_REGS size mismatch
21870#endif
21871static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21872 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21873 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21874 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21875 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21876};
21877
Eric Biederman5ade04a2003-10-22 04:03:46 +000021878static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021879{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021880 memset(arch, 0, sizeof(*arch));
21881 arch->features = 0;
21882}
21883
Eric Biederman90089602004-05-28 14:11:54 +000021884static const struct compiler_flag arch_flags[] = {
21885 { "mmx", X86_MMX_REGS },
21886 { "sse", X86_XMM_REGS },
21887 { "noop-copy", X86_NOOP_COPY },
21888 { 0, 0 },
21889};
21890static const struct compiler_flag arch_cpus[] = {
21891 { "i386", 0 },
21892 { "p2", X86_MMX_REGS },
21893 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21894 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21895 { "k7", X86_MMX_REGS },
21896 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21897 { "c3", X86_MMX_REGS },
21898 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21899 { 0, 0 }
21900};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021901static int arch_encode_flag(struct arch_state *arch, const char *flag)
21902{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021903 int result;
21904 int act;
21905
21906 act = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021907 if (strncmp(flag, "no-", 3) == 0) {
21908 flag += 3;
21909 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021910 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021911 if (act && strncmp(flag, "cpu=", 4) == 0) {
21912 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021913 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021914 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021915 else {
Eric Biederman90089602004-05-28 14:11:54 +000021916 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021917 }
21918 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021919}
21920
Eric Biederman90089602004-05-28 14:11:54 +000021921static void arch_usage(FILE *fp)
21922{
21923 flag_usage(fp, arch_flags, "-m", "-mno-");
21924 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21925}
21926
Eric Biedermanb138ac82003-04-22 18:44:01 +000021927static unsigned arch_regc_size(struct compile_state *state, int class)
21928{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021929 if ((class < 0) || (class > LAST_REGC)) {
21930 return 0;
21931 }
21932 return regc_size[class];
21933}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021934
Eric Biedermanb138ac82003-04-22 18:44:01 +000021935static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21936{
21937 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021938 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021939 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021940 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021941
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021942 /* Special case for the immediates */
21943 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21944 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21945 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021946 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021947 return 0;
21948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021949 return (regcm1 & regcm2) ||
21950 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21951}
21952
21953static void arch_reg_equivs(
21954 struct compile_state *state, unsigned *equiv, int reg)
21955{
21956 if ((reg < 0) || (reg > LAST_REG)) {
21957 internal_error(state, 0, "invalid register");
21958 }
21959 *equiv++ = reg;
21960 switch(reg) {
21961 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021962#if X86_4_8BIT_GPRS
21963 *equiv++ = REG_AH;
21964#endif
21965 *equiv++ = REG_AX;
21966 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021967 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021968 *equiv++ = REG_EDXEAX;
21969 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021970 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021971#if X86_4_8BIT_GPRS
21972 *equiv++ = REG_AL;
21973#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021974 *equiv++ = REG_AX;
21975 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021976 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021977 *equiv++ = REG_EDXEAX;
21978 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021979 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021980#if X86_4_8BIT_GPRS
21981 *equiv++ = REG_BH;
21982#endif
21983 *equiv++ = REG_BX;
21984 *equiv++ = REG_EBX;
21985 break;
21986
Eric Biedermanb138ac82003-04-22 18:44:01 +000021987 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021988#if X86_4_8BIT_GPRS
21989 *equiv++ = REG_BL;
21990#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021991 *equiv++ = REG_BX;
21992 *equiv++ = REG_EBX;
21993 break;
21994 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021995#if X86_4_8BIT_GPRS
21996 *equiv++ = REG_CH;
21997#endif
21998 *equiv++ = REG_CX;
21999 *equiv++ = REG_ECX;
22000 break;
22001
Eric Biedermanb138ac82003-04-22 18:44:01 +000022002 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022003#if X86_4_8BIT_GPRS
22004 *equiv++ = REG_CL;
22005#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022006 *equiv++ = REG_CX;
22007 *equiv++ = REG_ECX;
22008 break;
22009 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022010#if X86_4_8BIT_GPRS
22011 *equiv++ = REG_DH;
22012#endif
22013 *equiv++ = REG_DX;
22014 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022015 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022016 *equiv++ = REG_EDXEAX;
22017 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022018 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022019#if X86_4_8BIT_GPRS
22020 *equiv++ = REG_DL;
22021#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022022 *equiv++ = REG_DX;
22023 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022024 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022025 *equiv++ = REG_EDXEAX;
22026 break;
22027 case REG_AX:
22028 *equiv++ = REG_AL;
22029 *equiv++ = REG_AH;
22030 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022031 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022032 *equiv++ = REG_EDXEAX;
22033 break;
22034 case REG_BX:
22035 *equiv++ = REG_BL;
22036 *equiv++ = REG_BH;
22037 *equiv++ = REG_EBX;
22038 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022039 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022040 *equiv++ = REG_CL;
22041 *equiv++ = REG_CH;
22042 *equiv++ = REG_ECX;
22043 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022044 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022045 *equiv++ = REG_DL;
22046 *equiv++ = REG_DH;
22047 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022048 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022049 *equiv++ = REG_EDXEAX;
22050 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022051 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022052 *equiv++ = REG_ESI;
22053 break;
22054 case REG_DI:
22055 *equiv++ = REG_EDI;
22056 break;
22057 case REG_BP:
22058 *equiv++ = REG_EBP;
22059 break;
22060 case REG_SP:
22061 *equiv++ = REG_ESP;
22062 break;
22063 case REG_EAX:
22064 *equiv++ = REG_AL;
22065 *equiv++ = REG_AH;
22066 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022067 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022068 *equiv++ = REG_EDXEAX;
22069 break;
22070 case REG_EBX:
22071 *equiv++ = REG_BL;
22072 *equiv++ = REG_BH;
22073 *equiv++ = REG_BX;
22074 break;
22075 case REG_ECX:
22076 *equiv++ = REG_CL;
22077 *equiv++ = REG_CH;
22078 *equiv++ = REG_CX;
22079 break;
22080 case REG_EDX:
22081 *equiv++ = REG_DL;
22082 *equiv++ = REG_DH;
22083 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022084 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022085 *equiv++ = REG_EDXEAX;
22086 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022087 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022088 *equiv++ = REG_SI;
22089 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022090 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022091 *equiv++ = REG_DI;
22092 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022093 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022094 *equiv++ = REG_BP;
22095 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022096 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022097 *equiv++ = REG_SP;
22098 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022099 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022100 *equiv++ = REG_AL;
22101 *equiv++ = REG_AH;
22102 *equiv++ = REG_DL;
22103 *equiv++ = REG_DH;
22104 *equiv++ = REG_AX;
22105 *equiv++ = REG_DX;
22106 *equiv++ = REG_EAX;
22107 *equiv++ = REG_EDX;
22108 *equiv++ = REG_EDXEAX;
22109 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022110 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022111 *equiv++ = REG_AL;
22112 *equiv++ = REG_AH;
22113 *equiv++ = REG_DL;
22114 *equiv++ = REG_DH;
22115 *equiv++ = REG_AX;
22116 *equiv++ = REG_DX;
22117 *equiv++ = REG_EAX;
22118 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022119 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022120 break;
22121 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022122 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022123}
22124
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022125static unsigned arch_avail_mask(struct compile_state *state)
22126{
22127 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022128 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022129 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22130 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022131 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022132 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022133 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022134 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022135 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022136 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022137 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022138 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022139 return avail_mask;
22140}
22141
22142static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22143{
22144 unsigned mask, result;
22145 int class, class2;
22146 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022147
22148 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22149 if ((result & mask) == 0) {
22150 continue;
22151 }
22152 if (class > LAST_REGC) {
22153 result &= ~mask;
22154 }
22155 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22156 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22157 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22158 result |= (1 << class2);
22159 }
22160 }
22161 }
Eric Biederman530b5192003-07-01 10:05:30 +000022162 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022163 return result;
22164}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022165
Eric Biedermand1ea5392003-06-28 06:49:45 +000022166static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22167{
22168 /* Like arch_regcm_normalize except immediate register classes are excluded */
22169 regcm = arch_regcm_normalize(state, regcm);
22170 /* Remove the immediate register classes */
22171 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22172 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022173
Eric Biedermand1ea5392003-06-28 06:49:45 +000022174}
22175
Eric Biedermanb138ac82003-04-22 18:44:01 +000022176static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22177{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022178 unsigned mask;
22179 int class;
22180 mask = 0;
22181 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022182 if ((reg >= regcm_bound[class].first) &&
22183 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022184 mask |= (1 << class);
22185 }
22186 }
22187 if (!mask) {
22188 internal_error(state, 0, "reg %d not in any class", reg);
22189 }
22190 return mask;
22191}
22192
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022193static struct reg_info arch_reg_constraint(
22194 struct compile_state *state, struct type *type, const char *constraint)
22195{
22196 static const struct {
22197 char class;
22198 unsigned int mask;
22199 unsigned int reg;
22200 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022201 { 'r', REGCM_GPR32, REG_UNSET },
22202 { 'g', REGCM_GPR32, REG_UNSET },
22203 { 'p', REGCM_GPR32, REG_UNSET },
22204 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022205 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022206 { 'x', REGCM_XMM, REG_UNSET },
22207 { 'y', REGCM_MMX, REG_UNSET },
22208 { 'a', REGCM_GPR32, REG_EAX },
22209 { 'b', REGCM_GPR32, REG_EBX },
22210 { 'c', REGCM_GPR32, REG_ECX },
22211 { 'd', REGCM_GPR32, REG_EDX },
22212 { 'D', REGCM_GPR32, REG_EDI },
22213 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022214 { '\0', 0, REG_UNSET },
22215 };
22216 unsigned int regcm;
22217 unsigned int mask, reg;
22218 struct reg_info result;
22219 const char *ptr;
22220 regcm = arch_type_to_regcm(state, type);
22221 reg = REG_UNSET;
22222 mask = 0;
22223 for(ptr = constraint; *ptr; ptr++) {
22224 int i;
22225 if (*ptr == ' ') {
22226 continue;
22227 }
22228 for(i = 0; constraints[i].class != '\0'; i++) {
22229 if (constraints[i].class == *ptr) {
22230 break;
22231 }
22232 }
22233 if (constraints[i].class == '\0') {
22234 error(state, 0, "invalid register constraint ``%c''", *ptr);
22235 break;
22236 }
22237 if ((constraints[i].mask & regcm) == 0) {
22238 error(state, 0, "invalid register class %c specified",
22239 *ptr);
22240 }
22241 mask |= constraints[i].mask;
22242 if (constraints[i].reg != REG_UNSET) {
22243 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22244 error(state, 0, "Only one register may be specified");
22245 }
22246 reg = constraints[i].reg;
22247 }
22248 }
22249 result.reg = reg;
22250 result.regcm = mask;
22251 return result;
22252}
22253
22254static struct reg_info arch_reg_clobber(
22255 struct compile_state *state, const char *clobber)
22256{
22257 struct reg_info result;
22258 if (strcmp(clobber, "memory") == 0) {
22259 result.reg = REG_UNSET;
22260 result.regcm = 0;
22261 }
Eric Biederman90089602004-05-28 14:11:54 +000022262 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022263 result.reg = REG_EAX;
22264 result.regcm = REGCM_GPR32;
22265 }
Eric Biederman90089602004-05-28 14:11:54 +000022266 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022267 result.reg = REG_EBX;
22268 result.regcm = REGCM_GPR32;
22269 }
Eric Biederman90089602004-05-28 14:11:54 +000022270 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022271 result.reg = REG_ECX;
22272 result.regcm = REGCM_GPR32;
22273 }
Eric Biederman90089602004-05-28 14:11:54 +000022274 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022275 result.reg = REG_EDX;
22276 result.regcm = REGCM_GPR32;
22277 }
Eric Biederman90089602004-05-28 14:11:54 +000022278 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022279 result.reg = REG_ESI;
22280 result.regcm = REGCM_GPR32;
22281 }
Eric Biederman90089602004-05-28 14:11:54 +000022282 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022283 result.reg = REG_EDI;
22284 result.regcm = REGCM_GPR32;
22285 }
Eric Biederman90089602004-05-28 14:11:54 +000022286 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022287 result.reg = REG_EBP;
22288 result.regcm = REGCM_GPR32;
22289 }
Eric Biederman90089602004-05-28 14:11:54 +000022290 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022291 result.reg = REG_ESP;
22292 result.regcm = REGCM_GPR32;
22293 }
22294 else if (strcmp(clobber, "cc") == 0) {
22295 result.reg = REG_EFLAGS;
22296 result.regcm = REGCM_FLAGS;
22297 }
22298 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22299 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22300 result.reg = REG_XMM0 + octdigval(clobber[3]);
22301 result.regcm = REGCM_XMM;
22302 }
Eric Biederman90089602004-05-28 14:11:54 +000022303 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022304 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22305 result.reg = REG_MMX0 + octdigval(clobber[3]);
22306 result.regcm = REGCM_MMX;
22307 }
22308 else {
Eric Biederman90089602004-05-28 14:11:54 +000022309 error(state, 0, "unknown register name `%s' in asm",
22310 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022311 result.reg = REG_UNSET;
22312 result.regcm = 0;
22313 }
22314 return result;
22315}
22316
Stefan Reinauer14e22772010-04-27 06:56:47 +000022317static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022318 char *used, int reg, unsigned classes)
22319{
22320 unsigned mask;
22321 if (used[reg]) {
22322 return REG_UNSET;
22323 }
22324 mask = arch_reg_regcm(state, reg);
22325 return (classes & mask) ? reg : REG_UNSET;
22326}
22327
22328static int arch_select_free_register(
22329 struct compile_state *state, char *used, int classes)
22330{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022331 /* Live ranges with the most neighbors are colored first.
22332 *
22333 * Generally it does not matter which colors are given
22334 * as the register allocator attempts to color live ranges
22335 * in an order where you are guaranteed not to run out of colors.
22336 *
22337 * Occasionally the register allocator cannot find an order
22338 * of register selection that will find a free color. To
22339 * increase the odds the register allocator will work when
22340 * it guesses first give out registers from register classes
22341 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022342 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022343 */
22344 int i, reg;
22345 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022346 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022347 reg = do_select_reg(state, used, i, classes);
22348 }
22349 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22350 reg = do_select_reg(state, used, i, classes);
22351 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022352 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022353 reg = do_select_reg(state, used, i, classes);
22354 }
22355 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22356 reg = do_select_reg(state, used, i, classes);
22357 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022358 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22359 reg = do_select_reg(state, used, i, classes);
22360 }
Eric Biederman530b5192003-07-01 10:05:30 +000022361 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22362 reg = do_select_reg(state, used, i, classes);
22363 }
22364 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22365 reg = do_select_reg(state, used, i, classes);
22366 }
22367 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022368 reg = do_select_reg(state, used, i, classes);
22369 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022370 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22371 reg = do_select_reg(state, used, i, classes);
22372 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022373 return reg;
22374}
22375
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022376
Stefan Reinauer14e22772010-04-27 06:56:47 +000022377static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022379
22380#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022381#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022382#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022383 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022384 switch(type->type & TYPE_MASK) {
22385 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022386 case TYPE_VOID:
22387 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022388 break;
22389 case TYPE_CHAR:
22390 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022391 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022392 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022393 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022394 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022395 REGCM_MMX | REGCM_XMM |
22396 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022397 break;
22398 case TYPE_SHORT:
22399 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022400 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022401 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022402 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022403 REGCM_MMX | REGCM_XMM |
22404 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022405 break;
Eric Biederman90089602004-05-28 14:11:54 +000022406 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022407 case TYPE_INT:
22408 case TYPE_UINT:
22409 case TYPE_LONG:
22410 case TYPE_ULONG:
22411 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022412 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022413 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22414 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022415 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022416 break;
Eric Biederman90089602004-05-28 14:11:54 +000022417 case TYPE_JOIN:
22418 case TYPE_UNION:
22419 mask = arch_type_to_regcm(state, type->left);
22420 break;
22421 case TYPE_OVERLAP:
22422 mask = arch_type_to_regcm(state, type->left) &
22423 arch_type_to_regcm(state, type->right);
22424 break;
22425 case TYPE_BITFIELD:
22426 mask = arch_type_to_regcm(state, type->left);
22427 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022428 default:
Eric Biederman90089602004-05-28 14:11:54 +000022429 fprintf(state->errout, "type: ");
22430 name_of(state->errout, type);
22431 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022432 internal_error(state, 0, "no register class for type");
22433 break;
22434 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022435 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022436 return mask;
22437}
22438
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022439static int is_imm32(struct triple *imm)
22440{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022441 // second condition commented out to prevent compiler warning:
22442 // imm->u.cval is always 32bit unsigned, so the comparison is
22443 // always true.
22444 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022445 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022446
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022447}
22448static int is_imm16(struct triple *imm)
22449{
22450 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22451}
22452static int is_imm8(struct triple *imm)
22453{
22454 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22455}
22456
22457static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022458{
22459 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022460 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022461 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022462 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022463 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022464 if (!is_imm32(imm)) {
22465 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022466 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022467 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022468 use_triple(imm, ins);
22469 *expr = imm;
22470 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022471}
22472
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022473static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022474{
22475 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022476 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022477 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022478 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022479 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022480 if (!is_imm8(imm)) {
22481 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022482 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022483 unuse_triple(*expr, ins);
22484 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022485 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022486 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022487}
22488
Eric Biederman530b5192003-07-01 10:05:30 +000022489#define TEMPLATE_NOP 0
22490#define TEMPLATE_INTCONST8 1
22491#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022492#define TEMPLATE_UNKNOWNVAL 3
22493#define TEMPLATE_COPY8_REG 5
22494#define TEMPLATE_COPY16_REG 6
22495#define TEMPLATE_COPY32_REG 7
22496#define TEMPLATE_COPY_IMM8 8
22497#define TEMPLATE_COPY_IMM16 9
22498#define TEMPLATE_COPY_IMM32 10
22499#define TEMPLATE_PHI8 11
22500#define TEMPLATE_PHI16 12
22501#define TEMPLATE_PHI32 13
22502#define TEMPLATE_STORE8 14
22503#define TEMPLATE_STORE16 15
22504#define TEMPLATE_STORE32 16
22505#define TEMPLATE_LOAD8 17
22506#define TEMPLATE_LOAD16 18
22507#define TEMPLATE_LOAD32 19
22508#define TEMPLATE_BINARY8_REG 20
22509#define TEMPLATE_BINARY16_REG 21
22510#define TEMPLATE_BINARY32_REG 22
22511#define TEMPLATE_BINARY8_IMM 23
22512#define TEMPLATE_BINARY16_IMM 24
22513#define TEMPLATE_BINARY32_IMM 25
22514#define TEMPLATE_SL8_CL 26
22515#define TEMPLATE_SL16_CL 27
22516#define TEMPLATE_SL32_CL 28
22517#define TEMPLATE_SL8_IMM 29
22518#define TEMPLATE_SL16_IMM 30
22519#define TEMPLATE_SL32_IMM 31
22520#define TEMPLATE_UNARY8 32
22521#define TEMPLATE_UNARY16 33
22522#define TEMPLATE_UNARY32 34
22523#define TEMPLATE_CMP8_REG 35
22524#define TEMPLATE_CMP16_REG 36
22525#define TEMPLATE_CMP32_REG 37
22526#define TEMPLATE_CMP8_IMM 38
22527#define TEMPLATE_CMP16_IMM 39
22528#define TEMPLATE_CMP32_IMM 40
22529#define TEMPLATE_TEST8 41
22530#define TEMPLATE_TEST16 42
22531#define TEMPLATE_TEST32 43
22532#define TEMPLATE_SET 44
22533#define TEMPLATE_JMP 45
22534#define TEMPLATE_RET 46
22535#define TEMPLATE_INB_DX 47
22536#define TEMPLATE_INB_IMM 48
22537#define TEMPLATE_INW_DX 49
22538#define TEMPLATE_INW_IMM 50
22539#define TEMPLATE_INL_DX 51
22540#define TEMPLATE_INL_IMM 52
22541#define TEMPLATE_OUTB_DX 53
22542#define TEMPLATE_OUTB_IMM 54
22543#define TEMPLATE_OUTW_DX 55
22544#define TEMPLATE_OUTW_IMM 56
22545#define TEMPLATE_OUTL_DX 57
22546#define TEMPLATE_OUTL_IMM 58
22547#define TEMPLATE_BSF 59
22548#define TEMPLATE_RDMSR 60
22549#define TEMPLATE_WRMSR 61
22550#define TEMPLATE_UMUL8 62
22551#define TEMPLATE_UMUL16 63
22552#define TEMPLATE_UMUL32 64
22553#define TEMPLATE_DIV8 65
22554#define TEMPLATE_DIV16 66
22555#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022556#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022557#if LAST_TEMPLATE >= MAX_TEMPLATES
22558#error "MAX_TEMPLATES to low"
22559#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022560
Eric Biederman530b5192003-07-01 10:05:30 +000022561#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 +000022562#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022563#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022564
Eric Biedermanb138ac82003-04-22 18:44:01 +000022565
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022566static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022567 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022568 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022569 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22570 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22571 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22572 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22573 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22574 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22575 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22576 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22577 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22633 },
22634 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022635 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022636 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22637 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022638 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022639 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22640 },
Eric Biederman90089602004-05-28 14:11:54 +000022641 [TEMPLATE_UNKNOWNVAL] = {
22642 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22643 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022644 [TEMPLATE_COPY8_REG] = {
22645 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22646 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022647 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022648 [TEMPLATE_COPY16_REG] = {
22649 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22650 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22651 },
22652 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022653 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022654 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022655 },
22656 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022657 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022658 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22659 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022660 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022661 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022662 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22663 },
22664 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022665 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022666 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22667 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022668 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022669 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022670 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22671 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022672 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022673 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022674 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022675 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022676 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022677 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022678 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022679 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022680 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022681 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022682 [0] = { REG_UNSET, REGCM_GPR32 },
22683 [1] = { REG_UNSET, REGCM_GPR8_LO },
22684 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022685 },
22686 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022687 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022688 [0] = { REG_UNSET, REGCM_GPR32 },
22689 [1] = { REG_UNSET, REGCM_GPR16 },
22690 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022691 },
22692 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022693 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022694 [0] = { REG_UNSET, REGCM_GPR32 },
22695 [1] = { REG_UNSET, REGCM_GPR32 },
22696 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022697 },
22698 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022699 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022700 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22701 },
22702 [TEMPLATE_LOAD16] = {
22703 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22704 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22705 },
22706 [TEMPLATE_LOAD32] = {
22707 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22708 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22709 },
Eric Biederman530b5192003-07-01 10:05:30 +000022710 [TEMPLATE_BINARY8_REG] = {
22711 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022712 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022713 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22714 [1] = { REG_UNSET, REGCM_GPR8_LO },
22715 },
22716 },
22717 [TEMPLATE_BINARY16_REG] = {
22718 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022719 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022720 [0] = { REG_VIRT0, REGCM_GPR16 },
22721 [1] = { REG_UNSET, REGCM_GPR16 },
22722 },
22723 },
22724 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022725 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022726 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022727 [0] = { REG_VIRT0, REGCM_GPR32 },
22728 [1] = { REG_UNSET, REGCM_GPR32 },
22729 },
22730 },
Eric Biederman530b5192003-07-01 10:05:30 +000022731 [TEMPLATE_BINARY8_IMM] = {
22732 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022733 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022734 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22735 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22736 },
22737 },
22738 [TEMPLATE_BINARY16_IMM] = {
22739 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022740 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022741 [0] = { REG_VIRT0, REGCM_GPR16 },
22742 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22743 },
22744 },
22745 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022746 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022747 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022748 [0] = { REG_VIRT0, REGCM_GPR32 },
22749 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22750 },
22751 },
Eric Biederman530b5192003-07-01 10:05:30 +000022752 [TEMPLATE_SL8_CL] = {
22753 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022754 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022755 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22756 [1] = { REG_CL, REGCM_GPR8_LO },
22757 },
22758 },
22759 [TEMPLATE_SL16_CL] = {
22760 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022761 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022762 [0] = { REG_VIRT0, REGCM_GPR16 },
22763 [1] = { REG_CL, REGCM_GPR8_LO },
22764 },
22765 },
22766 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022767 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022768 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022769 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022770 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022771 },
22772 },
Eric Biederman530b5192003-07-01 10:05:30 +000022773 [TEMPLATE_SL8_IMM] = {
22774 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022775 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022776 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22777 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22778 },
22779 },
22780 [TEMPLATE_SL16_IMM] = {
22781 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022782 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022783 [0] = { REG_VIRT0, REGCM_GPR16 },
22784 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22785 },
22786 },
22787 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022788 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022789 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022790 [0] = { REG_VIRT0, REGCM_GPR32 },
22791 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22792 },
22793 },
Eric Biederman530b5192003-07-01 10:05:30 +000022794 [TEMPLATE_UNARY8] = {
22795 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22796 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22797 },
22798 [TEMPLATE_UNARY16] = {
22799 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22800 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22801 },
22802 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022803 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22804 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22805 },
Eric Biederman530b5192003-07-01 10:05:30 +000022806 [TEMPLATE_CMP8_REG] = {
22807 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22808 .rhs = {
22809 [0] = { REG_UNSET, REGCM_GPR8_LO },
22810 [1] = { REG_UNSET, REGCM_GPR8_LO },
22811 },
22812 },
22813 [TEMPLATE_CMP16_REG] = {
22814 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22815 .rhs = {
22816 [0] = { REG_UNSET, REGCM_GPR16 },
22817 [1] = { REG_UNSET, REGCM_GPR16 },
22818 },
22819 },
22820 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022821 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22822 .rhs = {
22823 [0] = { REG_UNSET, REGCM_GPR32 },
22824 [1] = { REG_UNSET, REGCM_GPR32 },
22825 },
22826 },
Eric Biederman530b5192003-07-01 10:05:30 +000022827 [TEMPLATE_CMP8_IMM] = {
22828 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22829 .rhs = {
22830 [0] = { REG_UNSET, REGCM_GPR8_LO },
22831 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22832 },
22833 },
22834 [TEMPLATE_CMP16_IMM] = {
22835 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22836 .rhs = {
22837 [0] = { REG_UNSET, REGCM_GPR16 },
22838 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22839 },
22840 },
22841 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022842 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22843 .rhs = {
22844 [0] = { REG_UNSET, REGCM_GPR32 },
22845 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22846 },
22847 },
Eric Biederman530b5192003-07-01 10:05:30 +000022848 [TEMPLATE_TEST8] = {
22849 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22850 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22851 },
22852 [TEMPLATE_TEST16] = {
22853 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22854 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22855 },
22856 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022857 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22858 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22859 },
22860 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022861 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022862 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22863 },
22864 [TEMPLATE_JMP] = {
22865 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22866 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022867 [TEMPLATE_RET] = {
22868 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22869 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022870 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022871 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022872 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22873 },
22874 [TEMPLATE_INB_IMM] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022875 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022876 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22877 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022878 [TEMPLATE_INW_DX] = {
22879 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022880 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22881 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022882 [TEMPLATE_INW_IMM] = {
22883 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022884 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22885 },
22886 [TEMPLATE_INL_DX] = {
22887 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22888 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22889 },
22890 [TEMPLATE_INL_IMM] = {
22891 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22892 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22893 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022894 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022895 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022896 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022897 [1] = { REG_DX, REGCM_GPR16 },
22898 },
22899 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022900 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022901 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022902 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022903 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22904 },
22905 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022906 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022907 .rhs = {
22908 [0] = { REG_AX, REGCM_GPR16 },
22909 [1] = { REG_DX, REGCM_GPR16 },
22910 },
22911 },
22912 [TEMPLATE_OUTW_IMM] = {
22913 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022914 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022915 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22916 },
22917 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022918 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022919 .rhs = {
22920 [0] = { REG_EAX, REGCM_GPR32 },
22921 [1] = { REG_DX, REGCM_GPR16 },
22922 },
22923 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022924 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022925 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022926 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022927 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22928 },
22929 },
22930 [TEMPLATE_BSF] = {
22931 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22932 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22933 },
22934 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022935 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022936 [0] = { REG_EAX, REGCM_GPR32 },
22937 [1] = { REG_EDX, REGCM_GPR32 },
22938 },
22939 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22940 },
22941 [TEMPLATE_WRMSR] = {
22942 .rhs = {
22943 [0] = { REG_ECX, REGCM_GPR32 },
22944 [1] = { REG_EAX, REGCM_GPR32 },
22945 [2] = { REG_EDX, REGCM_GPR32 },
22946 },
22947 },
Eric Biederman530b5192003-07-01 10:05:30 +000022948 [TEMPLATE_UMUL8] = {
22949 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022950 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022951 [0] = { REG_AL, REGCM_GPR8_LO },
22952 [1] = { REG_UNSET, REGCM_GPR8_LO },
22953 },
22954 },
22955 [TEMPLATE_UMUL16] = {
22956 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022957 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022958 [0] = { REG_AX, REGCM_GPR16 },
22959 [1] = { REG_UNSET, REGCM_GPR16 },
22960 },
22961 },
22962 [TEMPLATE_UMUL32] = {
22963 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022964 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022965 [0] = { REG_EAX, REGCM_GPR32 },
22966 [1] = { REG_UNSET, REGCM_GPR32 },
22967 },
22968 },
Eric Biederman530b5192003-07-01 10:05:30 +000022969 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022970 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022971 [0] = { REG_AL, REGCM_GPR8_LO },
22972 [1] = { REG_AH, REGCM_GPR8 },
22973 },
22974 .rhs = {
22975 [0] = { REG_AX, REGCM_GPR16 },
22976 [1] = { REG_UNSET, REGCM_GPR8_LO },
22977 },
22978 },
22979 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022980 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022981 [0] = { REG_AX, REGCM_GPR16 },
22982 [1] = { REG_DX, REGCM_GPR16 },
22983 },
22984 .rhs = {
22985 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22986 [1] = { REG_UNSET, REGCM_GPR16 },
22987 },
22988 },
22989 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022990 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022991 [0] = { REG_EAX, REGCM_GPR32 },
22992 [1] = { REG_EDX, REGCM_GPR32 },
22993 },
22994 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022995 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022996 [1] = { REG_UNSET, REGCM_GPR32 },
22997 },
22998 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022999};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023000
Eric Biederman83b991a2003-10-11 06:20:25 +000023001static void fixup_branch(struct compile_state *state,
23002 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23003 struct triple *left, struct triple *right)
23004{
23005 struct triple *test;
23006 if (!left) {
23007 internal_error(state, branch, "no branch test?");
23008 }
23009 test = pre_triple(state, branch,
23010 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023011 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023012 if (cmp_op == OP_CMP) {
23013 test->template_id = TEMPLATE_CMP32_REG;
23014 if (get_imm32(test, &RHS(test, 1))) {
23015 test->template_id = TEMPLATE_CMP32_IMM;
23016 }
23017 }
23018 use_triple(RHS(test, 0), test);
23019 use_triple(RHS(test, 1), test);
23020 unuse_triple(RHS(branch, 0), branch);
23021 RHS(branch, 0) = test;
23022 branch->op = jmp_op;
23023 branch->template_id = TEMPLATE_JMP;
23024 use_triple(RHS(branch, 0), branch);
23025}
23026
Eric Biedermanb138ac82003-04-22 18:44:01 +000023027static void fixup_branches(struct compile_state *state,
23028 struct triple *cmp, struct triple *use, int jmp_op)
23029{
23030 struct triple_set *entry, *next;
23031 for(entry = use->use; entry; entry = next) {
23032 next = entry->next;
23033 if (entry->member->op == OP_COPY) {
23034 fixup_branches(state, cmp, entry->member, jmp_op);
23035 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023036 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023037 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023038 struct triple *left, *right;
23039 left = right = 0;
23040 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023041 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023042 right = RHS(cmp, 1);
23043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023044 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023045 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023046 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023047 }
23048 }
23049}
23050
Stefan Reinauer14e22772010-04-27 06:56:47 +000023051static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023052 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23053{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023054 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023055 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023056
23057 /* Put a barrier up before the cmp which preceeds the
23058 * copy instruction. If a set actually occurs this gives
23059 * us a chance to move variables in registers out of the way.
23060 */
23061
23062 /* Modify the comparison operator */
23063 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023064 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023065 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023066 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023067 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023068 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023069 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023070 }
23071 /* Generate the instruction sequence that will transform the
23072 * result of the comparison into a logical value.
23073 */
Eric Biederman90089602004-05-28 14:11:54 +000023074 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023075 use_triple(ins, set);
23076 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023077
Eric Biederman90089602004-05-28 14:11:54 +000023078 convert = set;
23079 if (!equiv_types(ins->type, set->type)) {
23080 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23081 use_triple(set, convert);
23082 convert->template_id = TEMPLATE_COPY32_REG;
23083 }
23084
Eric Biedermanb138ac82003-04-22 18:44:01 +000023085 for(entry = ins->use; entry; entry = next) {
23086 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023087 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023088 continue;
23089 }
Eric Biederman90089602004-05-28 14:11:54 +000023090 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023091 }
Eric Biederman90089602004-05-28 14:11:54 +000023092 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023093}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023094
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023095struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23096{
23097 struct ins_template *template;
23098 struct reg_info result;
23099 int zlhs;
23100 if (ins->op == OP_PIECE) {
23101 index = ins->u.cval;
23102 ins = MISC(ins, 0);
23103 }
Eric Biederman90089602004-05-28 14:11:54 +000023104 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023105 if (triple_is_def(state, ins)) {
23106 zlhs = 1;
23107 }
23108 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023109 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023110 index, tops(ins->op));
23111 }
23112 switch(ins->op) {
23113 case OP_ASM:
23114 template = &ins->u.ainfo->tmpl;
23115 break;
23116 default:
23117 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023118 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023119 ins->template_id);
23120 }
23121 template = &templates[ins->template_id];
23122 break;
23123 }
23124 result = template->lhs[index];
23125 result.regcm = arch_regcm_normalize(state, result.regcm);
23126 if (result.reg != REG_UNNEEDED) {
23127 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23128 }
23129 if (result.regcm == 0) {
23130 internal_error(state, ins, "lhs %d regcm == 0", index);
23131 }
23132 return result;
23133}
23134
23135struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23136{
23137 struct reg_info result;
23138 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023139 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023140 (ins->op == OP_PIECE)) {
23141 internal_error(state, ins, "index %d out of range for %s\n",
23142 index, tops(ins->op));
23143 }
23144 switch(ins->op) {
23145 case OP_ASM:
23146 template = &ins->u.ainfo->tmpl;
23147 break;
Eric Biederman90089602004-05-28 14:11:54 +000023148 case OP_PHI:
23149 index = 0;
23150 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023151 default:
23152 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023153 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023154 ins->template_id);
23155 }
23156 template = &templates[ins->template_id];
23157 break;
23158 }
23159 result = template->rhs[index];
23160 result.regcm = arch_regcm_normalize(state, result.regcm);
23161 if (result.regcm == 0) {
23162 internal_error(state, ins, "rhs %d regcm == 0", index);
23163 }
23164 return result;
23165}
23166
Eric Biederman530b5192003-07-01 10:05:30 +000023167static struct triple *mod_div(struct compile_state *state,
23168 struct triple *ins, int div_op, int index)
23169{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023170 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023171
Eric Biederman530b5192003-07-01 10:05:30 +000023172 /* Generate the appropriate division instruction */
23173 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23174 RHS(div, 0) = RHS(ins, 0);
23175 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023176 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023177 div->template_id = TEMPLATE_DIV32;
23178 use_triple(RHS(div, 0), div);
23179 use_triple(RHS(div, 1), div);
23180 use_triple(LHS(div, 0), div);
23181 use_triple(LHS(div, 1), div);
23182
Eric Biederman530b5192003-07-01 10:05:30 +000023183 /* Replate uses of ins with the appropriate piece of the div */
23184 propogate_use(state, ins, LHS(div, index));
23185 release_triple(state, ins);
23186
23187 /* Return the address of the next instruction */
23188 return piece1->next;
23189}
23190
Eric Biederman90089602004-05-28 14:11:54 +000023191static int noop_adecl(struct triple *adecl)
23192{
23193 struct triple_set *use;
23194 /* It's a noop if it doesn't specify stoorage */
23195 if (adecl->lhs == 0) {
23196 return 1;
23197 }
23198 /* Is the adecl used? If not it's a noop */
23199 for(use = adecl->use; use ; use = use->next) {
23200 if ((use->member->op != OP_PIECE) ||
23201 (MISC(use->member, 0) != adecl)) {
23202 return 0;
23203 }
23204 }
23205 return 1;
23206}
23207
23208static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23209{
23210 struct triple *mask, *nmask, *shift;
23211 struct triple *val, *val_mask, *val_shift;
23212 struct triple *targ, *targ_mask;
23213 struct triple *new;
23214 ulong_t the_mask, the_nmask;
23215
23216 targ = RHS(ins, 0);
23217 val = RHS(ins, 1);
23218
23219 /* Get constant for the mask value */
23220 the_mask = 1;
23221 the_mask <<= ins->u.bitfield.size;
23222 the_mask -= 1;
23223 the_mask <<= ins->u.bitfield.offset;
23224 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23225 mask->u.cval = the_mask;
23226
23227 /* Get the inverted mask value */
23228 the_nmask = ~the_mask;
23229 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23230 nmask->u.cval = the_nmask;
23231
23232 /* Get constant for the shift value */
23233 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23234 shift->u.cval = ins->u.bitfield.offset;
23235
23236 /* Shift and mask the source value */
23237 val_shift = val;
23238 if (shift->u.cval != 0) {
23239 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23240 use_triple(val, val_shift);
23241 use_triple(shift, val_shift);
23242 }
23243 val_mask = val_shift;
23244 if (is_signed(val->type)) {
23245 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23246 use_triple(val_shift, val_mask);
23247 use_triple(mask, val_mask);
23248 }
23249
23250 /* Mask the target value */
23251 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23252 use_triple(targ, targ_mask);
23253 use_triple(nmask, targ_mask);
23254
23255 /* Now combined them together */
23256 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23257 use_triple(targ_mask, new);
23258 use_triple(val_mask, new);
23259
23260 /* Move all of the users over to the new expression */
23261 propogate_use(state, ins, new);
23262
23263 /* Delete the original triple */
23264 release_triple(state, ins);
23265
23266 /* Restart the transformation at mask */
23267 return mask;
23268}
23269
23270static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23271{
23272 struct triple *mask, *shift;
23273 struct triple *val, *val_mask, *val_shift;
23274 ulong_t the_mask;
23275
23276 val = RHS(ins, 0);
23277
23278 /* Get constant for the mask value */
23279 the_mask = 1;
23280 the_mask <<= ins->u.bitfield.size;
23281 the_mask -= 1;
23282 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23283 mask->u.cval = the_mask;
23284
23285 /* Get constant for the right shift value */
23286 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23287 shift->u.cval = ins->u.bitfield.offset;
23288
23289 /* Shift arithmetic right, to correct the sign */
23290 val_shift = val;
23291 if (shift->u.cval != 0) {
23292 int op;
23293 if (ins->op == OP_SEXTRACT) {
23294 op = OP_SSR;
23295 } else {
23296 op = OP_USR;
23297 }
23298 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23299 use_triple(val, val_shift);
23300 use_triple(shift, val_shift);
23301 }
23302
23303 /* Finally mask the value */
23304 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23305 use_triple(val_shift, val_mask);
23306 use_triple(mask, val_mask);
23307
23308 /* Move all of the users over to the new expression */
23309 propogate_use(state, ins, val_mask);
23310
23311 /* Release the original instruction */
23312 release_triple(state, ins);
23313
23314 return mask;
23315
23316}
23317
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023318static struct triple *transform_to_arch_instruction(
23319 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023320{
23321 /* Transform from generic 3 address instructions
23322 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023323 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023324 * Copies are inserted to preserve the register flexibility
23325 * of 3 address instructions.
23326 */
Eric Biederman90089602004-05-28 14:11:54 +000023327 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023328 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023329 next = ins->next;
23330 switch(ins->op) {
23331 case OP_INTCONST:
23332 ins->template_id = TEMPLATE_INTCONST32;
23333 if (ins->u.cval < 256) {
23334 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023335 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023336 break;
23337 case OP_ADDRCONST:
23338 ins->template_id = TEMPLATE_INTCONST32;
23339 break;
Eric Biederman90089602004-05-28 14:11:54 +000023340 case OP_UNKNOWNVAL:
23341 ins->template_id = TEMPLATE_UNKNOWNVAL;
23342 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023343 case OP_NOOP:
23344 case OP_SDECL:
23345 case OP_BLOBCONST:
23346 case OP_LABEL:
23347 ins->template_id = TEMPLATE_NOP;
23348 break;
23349 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023350 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023351 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023352 value = RHS(ins, 0);
23353 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023354 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023355 }
Eric Biederman90089602004-05-28 14:11:54 +000023356 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023357 ins->template_id = TEMPLATE_COPY_IMM16;
23358 }
Eric Biederman90089602004-05-28 14:11:54 +000023359 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023360 ins->template_id = TEMPLATE_COPY_IMM32;
23361 }
Eric Biederman90089602004-05-28 14:11:54 +000023362 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023363 internal_error(state, ins, "bad constant passed to copy");
23364 }
Eric Biederman90089602004-05-28 14:11:54 +000023365 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023366 ins->template_id = TEMPLATE_COPY8_REG;
23367 }
Eric Biederman90089602004-05-28 14:11:54 +000023368 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023369 ins->template_id = TEMPLATE_COPY16_REG;
23370 }
Eric Biederman90089602004-05-28 14:11:54 +000023371 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023372 ins->template_id = TEMPLATE_COPY32_REG;
23373 }
23374 else {
23375 internal_error(state, ins, "bad type passed to copy");
23376 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023377 break;
23378 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023379 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023380 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023381 ins->template_id = TEMPLATE_PHI8;
23382 }
Eric Biederman90089602004-05-28 14:11:54 +000023383 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023384 ins->template_id = TEMPLATE_PHI16;
23385 }
Eric Biederman90089602004-05-28 14:11:54 +000023386 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023387 ins->template_id = TEMPLATE_PHI32;
23388 }
23389 else {
23390 internal_error(state, ins, "bad type passed to phi");
23391 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023392 break;
Eric Biederman90089602004-05-28 14:11:54 +000023393 case OP_ADECL:
23394 /* Adecls should always be treated as dead code and
23395 * removed. If we are not optimizing they may linger.
23396 */
23397 if (!noop_adecl(ins)) {
23398 internal_error(state, ins, "adecl remains?");
23399 }
23400 ins->template_id = TEMPLATE_NOP;
23401 next = after_lhs(state, ins);
23402 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023403 case OP_STORE:
23404 switch(ins->type->type & TYPE_MASK) {
23405 case TYPE_CHAR: case TYPE_UCHAR:
23406 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023407 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023408 case TYPE_SHORT: case TYPE_USHORT:
23409 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023410 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023411 case TYPE_INT: case TYPE_UINT:
23412 case TYPE_LONG: case TYPE_ULONG:
23413 case TYPE_POINTER:
23414 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023415 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023416 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023417 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023418 break;
23419 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023420 break;
23421 case OP_LOAD:
23422 switch(ins->type->type & TYPE_MASK) {
23423 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023424 case TYPE_SHORT: case TYPE_USHORT:
23425 case TYPE_INT: case TYPE_UINT:
23426 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023427 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023428 break;
23429 default:
23430 internal_error(state, ins, "unknown type in load");
23431 break;
23432 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023433 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023434 break;
23435 case OP_ADD:
23436 case OP_SUB:
23437 case OP_AND:
23438 case OP_XOR:
23439 case OP_OR:
23440 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023441 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023442 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023443 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023444 }
23445 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023446 case OP_SDIVT:
23447 case OP_UDIVT:
23448 ins->template_id = TEMPLATE_DIV32;
23449 next = after_lhs(state, ins);
23450 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023451 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023452 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023453 break;
23454 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023455 next = mod_div(state, ins, OP_UDIVT, 0);
23456 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023457 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023458 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023459 break;
23460 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023461 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023462 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023463 case OP_SMOD:
23464 next = mod_div(state, ins, OP_SDIVT, 1);
23465 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023466 case OP_SL:
23467 case OP_SSR:
23468 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023469 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023470 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023471 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023472 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23473 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023474 }
23475 break;
23476 case OP_INVERT:
23477 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023478 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023479 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023480 case OP_EQ:
23481 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023482 break;
23483 case OP_NOTEQ:
23484 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23485 break;
23486 case OP_SLESS:
23487 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23488 break;
23489 case OP_ULESS:
23490 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23491 break;
23492 case OP_SMORE:
23493 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23494 break;
23495 case OP_UMORE:
23496 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23497 break;
23498 case OP_SLESSEQ:
23499 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23500 break;
23501 case OP_ULESSEQ:
23502 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23503 break;
23504 case OP_SMOREEQ:
23505 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23506 break;
23507 case OP_UMOREEQ:
23508 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23509 break;
23510 case OP_LTRUE:
23511 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23512 break;
23513 case OP_LFALSE:
23514 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23515 break;
23516 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023517 ins->op = OP_JMP;
23518 ins->template_id = TEMPLATE_NOP;
23519 break;
23520 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023521 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023522 RHS(ins, 0)->type, RHS(ins, 0), 0);
23523 break;
23524 case OP_CALL:
23525 ins->template_id = TEMPLATE_NOP;
23526 break;
23527 case OP_RET:
23528 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023529 break;
23530 case OP_INB:
23531 case OP_INW:
23532 case OP_INL:
23533 switch(ins->op) {
23534 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23535 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23536 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23537 }
23538 if (get_imm8(ins, &RHS(ins, 0))) {
23539 ins->template_id += 1;
23540 }
23541 break;
23542 case OP_OUTB:
23543 case OP_OUTW:
23544 case OP_OUTL:
23545 switch(ins->op) {
23546 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23547 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23548 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23549 }
23550 if (get_imm8(ins, &RHS(ins, 1))) {
23551 ins->template_id += 1;
23552 }
23553 break;
23554 case OP_BSF:
23555 case OP_BSR:
23556 ins->template_id = TEMPLATE_BSF;
23557 break;
23558 case OP_RDMSR:
23559 ins->template_id = TEMPLATE_RDMSR;
23560 next = after_lhs(state, ins);
23561 break;
23562 case OP_WRMSR:
23563 ins->template_id = TEMPLATE_WRMSR;
23564 break;
23565 case OP_HLT:
23566 ins->template_id = TEMPLATE_NOP;
23567 break;
23568 case OP_ASM:
23569 ins->template_id = TEMPLATE_NOP;
23570 next = after_lhs(state, ins);
23571 break;
23572 /* Already transformed instructions */
23573 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023574 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023575 break;
23576 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023577 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023578 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023579 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023580 }
23581 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023582 case OP_JMP:
23583 ins->template_id = TEMPLATE_NOP;
23584 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023585 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23586 case OP_JMP_SLESS: case OP_JMP_ULESS:
23587 case OP_JMP_SMORE: case OP_JMP_UMORE:
23588 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23589 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23590 ins->template_id = TEMPLATE_JMP;
23591 break;
23592 case OP_SET_EQ: case OP_SET_NOTEQ:
23593 case OP_SET_SLESS: case OP_SET_ULESS:
23594 case OP_SET_SMORE: case OP_SET_UMORE:
23595 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23596 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23597 ins->template_id = TEMPLATE_SET;
23598 break;
Eric Biederman90089602004-05-28 14:11:54 +000023599 case OP_DEPOSIT:
23600 next = x86_deposit(state, ins);
23601 break;
23602 case OP_SEXTRACT:
23603 case OP_UEXTRACT:
23604 next = x86_extract(state, ins);
23605 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023606 /* Unhandled instructions */
23607 case OP_PIECE:
23608 default:
Eric Biederman90089602004-05-28 14:11:54 +000023609 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023610 ins->op, tops(ins->op));
23611 break;
23612 }
23613 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023614}
23615
Eric Biederman530b5192003-07-01 10:05:30 +000023616static long next_label(struct compile_state *state)
23617{
Eric Biederman90089602004-05-28 14:11:54 +000023618 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023619 return ++label_counter;
23620}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023621static void generate_local_labels(struct compile_state *state)
23622{
23623 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023624 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023625 label = first;
23626 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023627 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023628 (label->op == OP_SDECL)) {
23629 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023630 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023631 } else {
23632 label->u.cval = 0;
23633 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023634
Eric Biedermanb138ac82003-04-22 18:44:01 +000023635 }
23636 label = label->next;
23637 } while(label != first);
23638}
23639
Stefan Reinauer14e22772010-04-27 06:56:47 +000023640static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023641 struct triple *triple, int classes)
23642{
23643 unsigned mask;
23644 int reg;
23645 reg = ID_REG(triple->id);
23646 if (reg == REG_UNSET) {
23647 internal_error(state, triple, "register not set");
23648 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023649 mask = arch_reg_regcm(state, reg);
23650 if (!(classes & mask)) {
23651 internal_error(state, triple, "reg %d in wrong class",
23652 reg);
23653 }
23654 return reg;
23655}
23656
Eric Biederman90089602004-05-28 14:11:54 +000023657
Eric Biederman530b5192003-07-01 10:05:30 +000023658#if REG_XMM7 != 44
23659#error "Registers have renumberd fix arch_reg_str"
23660#endif
Eric Biederman90089602004-05-28 14:11:54 +000023661static const char *arch_regs[] = {
23662 "%unset",
23663 "%unneeded",
23664 "%eflags",
23665 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23666 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23667 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23668 "%edx:%eax",
23669 "%dx:%ax",
23670 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023671 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023672 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23673};
23674static const char *arch_reg_str(int reg)
23675{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023676 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23677 reg = 0;
23678 }
Eric Biederman90089602004-05-28 14:11:54 +000023679 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023680}
23681
23682static const char *reg(struct compile_state *state, struct triple *triple,
23683 int classes)
23684{
23685 int reg;
23686 reg = check_reg(state, triple, classes);
23687 return arch_reg_str(reg);
23688}
23689
Eric Biederman90089602004-05-28 14:11:54 +000023690static int arch_reg_size(int reg)
23691{
23692 int size;
23693 size = 0;
23694 if (reg == REG_EFLAGS) {
23695 size = 32;
23696 }
23697 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23698 size = 8;
23699 }
23700 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23701 size = 16;
23702 }
23703 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23704 size = 32;
23705 }
23706 else if (reg == REG_EDXEAX) {
23707 size = 64;
23708 }
23709 else if (reg == REG_DXAX) {
23710 size = 32;
23711 }
23712 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23713 size = 64;
23714 }
23715 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23716 size = 128;
23717 }
23718 return size;
23719}
23720
23721static int reg_size(struct compile_state *state, struct triple *ins)
23722{
23723 int reg;
23724 reg = ID_REG(ins->id);
23725 if (reg == REG_UNSET) {
23726 internal_error(state, ins, "register not set");
23727 }
23728 return arch_reg_size(reg);
23729}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023730
Eric Biederman90089602004-05-28 14:11:54 +000023731
23732
Eric Biedermanb138ac82003-04-22 18:44:01 +000023733const char *type_suffix(struct compile_state *state, struct type *type)
23734{
23735 const char *suffix;
23736 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023737 case SIZEOF_I8: suffix = "b"; break;
23738 case SIZEOF_I16: suffix = "w"; break;
23739 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023740 default:
23741 internal_error(state, 0, "unknown suffix");
23742 suffix = 0;
23743 break;
23744 }
23745 return suffix;
23746}
23747
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023748static void print_const_val(
23749 struct compile_state *state, struct triple *ins, FILE *fp)
23750{
23751 switch(ins->op) {
23752 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023753 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023754 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023755 break;
23756 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023757 if ((MISC(ins, 0)->op != OP_SDECL) &&
23758 (MISC(ins, 0)->op != OP_LABEL))
23759 {
Eric Biederman830c9882003-07-04 00:27:33 +000023760 internal_error(state, ins, "bad base for addrconst");
23761 }
23762 if (MISC(ins, 0)->u.cval <= 0) {
23763 internal_error(state, ins, "unlabeled constant");
23764 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023765 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023766 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023767 (unsigned long)(MISC(ins, 0)->u.cval),
23768 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023769 break;
23770 default:
23771 internal_error(state, ins, "unknown constant type");
23772 break;
23773 }
23774}
23775
Eric Biederman530b5192003-07-01 10:05:30 +000023776static void print_const(struct compile_state *state,
23777 struct triple *ins, FILE *fp)
23778{
23779 switch(ins->op) {
23780 case OP_INTCONST:
23781 switch(ins->type->type & TYPE_MASK) {
23782 case TYPE_CHAR:
23783 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023784 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023785 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023786 break;
23787 case TYPE_SHORT:
23788 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023789 fprintf(fp, ".short 0x%04lx\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_INT:
23793 case TYPE_UINT:
23794 case TYPE_LONG:
23795 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023796 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023797 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023798 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023799 break;
23800 default:
Eric Biederman90089602004-05-28 14:11:54 +000023801 fprintf(state->errout, "type: ");
23802 name_of(state->errout, ins->type);
23803 fprintf(state->errout, "\n");
23804 internal_error(state, ins, "Unknown constant type. Val: %lu",
23805 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023806 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023807
Eric Biederman530b5192003-07-01 10:05:30 +000023808 break;
23809 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023810 if ((MISC(ins, 0)->op != OP_SDECL) &&
23811 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023812 internal_error(state, ins, "bad base for addrconst");
23813 }
23814 if (MISC(ins, 0)->u.cval <= 0) {
23815 internal_error(state, ins, "unlabeled constant");
23816 }
23817 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023818 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023819 (unsigned long)(MISC(ins, 0)->u.cval),
23820 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023821 break;
23822 case OP_BLOBCONST:
23823 {
23824 unsigned char *blob;
23825 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023826 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023827 blob = ins->u.blob;
23828 for(i = 0; i < size; i++) {
23829 fprintf(fp, ".byte 0x%02x\n",
23830 blob[i]);
23831 }
23832 break;
23833 }
23834 default:
23835 internal_error(state, ins, "Unknown constant type");
23836 break;
23837 }
23838}
23839
23840#define TEXT_SECTION ".rom.text"
23841#define DATA_SECTION ".rom.data"
23842
23843static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023844 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023845{
Eric Biederman90089602004-05-28 14:11:54 +000023846 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023847 long ref;
23848 ref = next_label(state);
23849 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023850 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023851 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023852 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023853 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23854 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023855 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023856 }
Eric Biederman530b5192003-07-01 10:05:30 +000023857 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23858 return ref;
23859}
23860
Eric Biederman90089602004-05-28 14:11:54 +000023861static long get_mask_pool_ref(
23862 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23863{
23864 long ref;
23865 if (mask == 0xff) {
23866 ref = 1;
23867 }
23868 else if (mask == 0xffff) {
23869 ref = 2;
23870 }
23871 else {
Eric Biederman90089602004-05-28 14:11:54 +000023872 internal_error(state, ins, "unhandled mask value");
23873 }
23874 return ref;
23875}
23876
Eric Biedermanb138ac82003-04-22 18:44:01 +000023877static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023878 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023879{
23880 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023881 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023882 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023883 internal_error(state, ins, "invalid register assignment");
23884 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023885 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023886 fprintf(fp, "\t%s ", op);
23887 print_const_val(state, RHS(ins, 1), fp);
23888 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023889 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023890 }
23891 else {
23892 unsigned lmask, rmask;
23893 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023894 lreg = check_reg(state, RHS(ins, 0), mask);
23895 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023896 lmask = arch_reg_regcm(state, lreg);
23897 rmask = arch_reg_regcm(state, rreg);
23898 mask = lmask & rmask;
23899 fprintf(fp, "\t%s %s, %s\n",
23900 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023901 reg(state, RHS(ins, 1), mask),
23902 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023903 }
23904}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023905static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023906 const char *op, struct triple *ins, FILE *fp)
23907{
23908 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023909 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023910 fprintf(fp, "\t%s %s\n",
23911 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023912 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023913}
23914
23915static void print_op_shift(struct compile_state *state,
23916 const char *op, struct triple *ins, FILE *fp)
23917{
23918 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023919 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023920 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023921 internal_error(state, ins, "invalid register assignment");
23922 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023923 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023924 fprintf(fp, "\t%s ", op);
23925 print_const_val(state, RHS(ins, 1), fp);
23926 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023927 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023928 }
23929 else {
23930 fprintf(fp, "\t%s %s, %s\n",
23931 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023932 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023933 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023934 }
23935}
23936
23937static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23938{
23939 const char *op;
23940 int mask;
23941 int dreg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023942 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023943 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023944 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23945 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23946 default:
23947 internal_error(state, ins, "not an in operation");
23948 op = 0;
23949 break;
23950 }
23951 dreg = check_reg(state, ins, mask);
23952 if (!reg_is_reg(state, dreg, REG_EAX)) {
23953 internal_error(state, ins, "dst != %%eax");
23954 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023955 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023956 fprintf(fp, "\t%s ", op);
23957 print_const_val(state, RHS(ins, 0), fp);
23958 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023959 reg(state, ins, mask));
23960 }
23961 else {
23962 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023963 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023964 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23965 internal_error(state, ins, "src != %%dx");
23966 }
23967 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023968 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023969 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023970 reg(state, ins, mask));
23971 }
23972}
23973
23974static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23975{
23976 const char *op;
23977 int mask;
23978 int lreg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023979 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023980 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023981 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23982 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23983 default:
23984 internal_error(state, ins, "not an out operation");
23985 op = 0;
23986 break;
23987 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023988 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023989 if (!reg_is_reg(state, lreg, REG_EAX)) {
23990 internal_error(state, ins, "src != %%eax");
23991 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023992 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023993 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023994 op, reg(state, RHS(ins, 0), mask));
23995 print_const_val(state, RHS(ins, 1), fp);
23996 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023997 }
23998 else {
23999 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024000 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024001 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24002 internal_error(state, ins, "dst != %%dx");
24003 }
24004 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024005 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024006 reg(state, RHS(ins, 0), mask),
24007 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024008 }
24009}
24010
24011static void print_op_move(struct compile_state *state,
24012 struct triple *ins, FILE *fp)
24013{
24014 /* op_move is complex because there are many types
24015 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024016 * Because OP_COPY will be introduced in arbitrary locations
24017 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024018 * OP_CONVERT can change the flags and it is the only operation
24019 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024020 */
24021 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24022 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024023 if (state->arch->features & X86_NOOP_COPY) {
24024 omit_copy = 0;
24025 }
24026 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024027 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024028 dst = ins;
24029 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024030 else {
24031 internal_error(state, ins, "unknown move operation");
24032 src = dst = 0;
24033 }
Eric Biederman90089602004-05-28 14:11:54 +000024034 if (reg_size(state, dst) < size_of(state, dst->type)) {
24035 internal_error(state, ins, "Invalid destination register");
24036 }
24037 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24038 fprintf(state->errout, "src type: ");
24039 name_of(state->errout, src->type);
24040 fprintf(state->errout, "\n");
24041 fprintf(state->errout, "dst type: ");
24042 name_of(state->errout, dst->type);
24043 fprintf(state->errout, "\n");
24044 internal_error(state, ins, "Type mismatch for OP_COPY");
24045 }
24046
Eric Biederman0babc1c2003-05-09 02:39:00 +000024047 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024048 int src_reg, dst_reg;
24049 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024050 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024051 dst_reg = ID_REG(dst->id);
24052 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024053 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024054 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024055 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024056 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024057 if ((src_reg != dst_reg) || !omit_copy) {
24058 fprintf(fp, "\tmov %s, %s\n",
24059 reg(state, src, src_regcm),
24060 reg(state, dst, dst_regcm));
24061 }
24062 }
24063 /* Move 32bit to 16bit */
24064 else if ((src_regcm & REGCM_GPR32) &&
24065 (dst_regcm & REGCM_GPR16)) {
24066 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24067 if ((src_reg != dst_reg) || !omit_copy) {
24068 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024069 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024070 arch_reg_str(dst_reg));
24071 }
24072 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024073 /* Move from 32bit gprs to 16bit gprs */
24074 else if ((src_regcm & REGCM_GPR32) &&
24075 (dst_regcm & REGCM_GPR16)) {
24076 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24077 if ((src_reg != dst_reg) || !omit_copy) {
24078 fprintf(fp, "\tmov %s, %s\n",
24079 arch_reg_str(src_reg),
24080 arch_reg_str(dst_reg));
24081 }
24082 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024083 /* Move 32bit to 8bit */
24084 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024085 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024086 {
24087 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24088 if ((src_reg != dst_reg) || !omit_copy) {
24089 fprintf(fp, "\tmovb %s, %s\n",
24090 arch_reg_str(src_reg),
24091 arch_reg_str(dst_reg));
24092 }
24093 }
24094 /* Move 16bit to 8bit */
24095 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024096 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024097 {
24098 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24099 if ((src_reg != dst_reg) || !omit_copy) {
24100 fprintf(fp, "\tmovb %s, %s\n",
24101 arch_reg_str(src_reg),
24102 arch_reg_str(dst_reg));
24103 }
24104 }
24105 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024106 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024107 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024108 const char *op;
24109 op = is_signed(src->type)? "movsx": "movzx";
24110 fprintf(fp, "\t%s %s, %s\n",
24111 op,
24112 reg(state, src, src_regcm),
24113 reg(state, dst, dst_regcm));
24114 }
24115 /* Move between sse registers */
24116 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24117 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024118 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024119 reg(state, src, src_regcm),
24120 reg(state, dst, dst_regcm));
24121 }
24122 }
Eric Biederman530b5192003-07-01 10:05:30 +000024123 /* Move between mmx registers */
24124 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024125 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024126 fprintf(fp, "\tmovq %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 from sse to mmx registers */
24132 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24133 fprintf(fp, "\tmovdq2q %s, %s\n",
24134 reg(state, src, src_regcm),
24135 reg(state, dst, dst_regcm));
24136 }
24137 /* Move from mmx to sse registers */
24138 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24139 fprintf(fp, "\tmovq2dq %s, %s\n",
24140 reg(state, src, src_regcm),
24141 reg(state, dst, dst_regcm));
24142 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024143 /* Move between 32bit gprs & mmx/sse registers */
24144 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24145 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24146 fprintf(fp, "\tmovd %s, %s\n",
24147 reg(state, src, src_regcm),
24148 reg(state, dst, dst_regcm));
24149 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024150 /* Move from 16bit gprs & mmx/sse registers */
24151 else if ((src_regcm & REGCM_GPR16) &&
24152 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24153 const char *op;
24154 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024155 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024156 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24157 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24158 op,
24159 arch_reg_str(src_reg),
24160 arch_reg_str(mid_reg),
24161 arch_reg_str(mid_reg),
24162 arch_reg_str(dst_reg));
24163 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024164 /* Move from mmx/sse registers to 16bit gprs */
24165 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24166 (dst_regcm & REGCM_GPR16)) {
24167 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24168 fprintf(fp, "\tmovd %s, %s\n",
24169 arch_reg_str(src_reg),
24170 arch_reg_str(dst_reg));
24171 }
Eric Biederman530b5192003-07-01 10:05:30 +000024172 /* Move from gpr to 64bit dividend */
24173 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24174 (dst_regcm & REGCM_DIVIDEND64)) {
24175 const char *extend;
24176 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24177 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024178 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024179 extend);
24180 }
24181 /* Move from 64bit gpr to gpr */
24182 else if ((src_regcm & REGCM_DIVIDEND64) &&
24183 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24184 if (dst_regcm & REGCM_GPR32) {
24185 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024186 }
Eric Biederman530b5192003-07-01 10:05:30 +000024187 else if (dst_regcm & REGCM_GPR16) {
24188 src_reg = REG_AX;
24189 }
24190 else if (dst_regcm & REGCM_GPR8_LO) {
24191 src_reg = REG_AL;
24192 }
24193 fprintf(fp, "\tmov %s, %s\n",
24194 arch_reg_str(src_reg),
24195 arch_reg_str(dst_reg));
24196 }
24197 /* Move from mmx/sse registers to 64bit gpr */
24198 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24199 (dst_regcm & REGCM_DIVIDEND64)) {
24200 const char *extend;
24201 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24202 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24203 arch_reg_str(src_reg),
24204 extend);
24205 }
24206 /* Move from 64bit gpr to mmx/sse register */
24207 else if ((src_regcm & REGCM_DIVIDEND64) &&
24208 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24209 fprintf(fp, "\tmovd %%eax, %s\n",
24210 arch_reg_str(dst_reg));
24211 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024212#if X86_4_8BIT_GPRS
24213 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024214 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024215 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24216 const char *op;
24217 int mid_reg;
24218 op = is_signed(src->type)? "movsx":"movzx";
24219 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24220 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24221 op,
24222 reg(state, src, src_regcm),
24223 arch_reg_str(mid_reg),
24224 arch_reg_str(mid_reg),
24225 reg(state, dst, dst_regcm));
24226 }
24227 /* Move from mmx/sse registers and 8bit gprs */
24228 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024229 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024230 int mid_reg;
24231 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24232 fprintf(fp, "\tmovd %s, %s\n",
24233 reg(state, src, src_regcm),
24234 arch_reg_str(mid_reg));
24235 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024236 /* Move from 32bit gprs to 8bit gprs */
24237 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024238 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024239 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24240 if ((src_reg != dst_reg) || !omit_copy) {
24241 fprintf(fp, "\tmov %s, %s\n",
24242 arch_reg_str(src_reg),
24243 arch_reg_str(dst_reg));
24244 }
24245 }
24246 /* Move from 16bit gprs to 8bit gprs */
24247 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024248 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024249 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24250 if ((src_reg != dst_reg) || !omit_copy) {
24251 fprintf(fp, "\tmov %s, %s\n",
24252 arch_reg_str(src_reg),
24253 arch_reg_str(dst_reg));
24254 }
24255 }
24256#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024257 /* Move from %eax:%edx to %eax:%edx */
24258 else if ((src_regcm & REGCM_DIVIDEND64) &&
24259 (dst_regcm & REGCM_DIVIDEND64) &&
24260 (src_reg == dst_reg)) {
24261 if (!omit_copy) {
24262 fprintf(fp, "\t/*mov %s, %s*/\n",
24263 arch_reg_str(src_reg),
24264 arch_reg_str(dst_reg));
24265 }
24266 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024267 else {
Eric Biederman90089602004-05-28 14:11:54 +000024268 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24269 internal_error(state, ins, "attempt to copy from %%eflags!");
24270 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024271 internal_error(state, ins, "unknown copy type");
24272 }
24273 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024274 else {
Eric Biederman90089602004-05-28 14:11:54 +000024275 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024276 int dst_reg;
24277 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024278 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024279 dst_reg = ID_REG(dst->id);
24280 dst_regcm = arch_reg_regcm(state, dst_reg);
24281 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24282 fprintf(fp, "\tmov ");
24283 print_const_val(state, src, fp);
24284 fprintf(fp, ", %s\n",
24285 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24286 }
24287 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024288 if (dst_size > SIZEOF_I32) {
24289 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024290 }
24291 fprintf(fp, "\tmov $0, %%edx\n");
24292 fprintf(fp, "\tmov ");
24293 print_const_val(state, src, fp);
24294 fprintf(fp, ", %%eax\n");
24295 }
24296 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024297 if (dst_size > SIZEOF_I16) {
24298 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024299 }
24300 fprintf(fp, "\tmov $0, %%dx\n");
24301 fprintf(fp, "\tmov ");
24302 print_const_val(state, src, fp);
24303 fprintf(fp, ", %%ax");
24304 }
24305 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24306 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024307 if (dst_size > SIZEOF_I32) {
24308 internal_error(state, ins, "%d bit constant...", dst_size);
24309 }
24310 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024311 fprintf(fp, "\tmovd L%s%lu, %s\n",
24312 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024313 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24314 }
24315 else {
24316 internal_error(state, ins, "unknown copy immediate type");
24317 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024318 }
Eric Biederman90089602004-05-28 14:11:54 +000024319 /* Leave now if this is not a type conversion */
24320 if (ins->op != OP_CONVERT) {
24321 return;
24322 }
24323 /* Now make certain I have not logically overflowed the destination */
24324 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24325 (size_of(state, dst->type) < reg_size(state, dst)))
24326 {
24327 unsigned long mask;
24328 int dst_reg;
24329 int dst_regcm;
24330 if (size_of(state, dst->type) >= 32) {
24331 fprintf(state->errout, "dst type: ");
24332 name_of(state->errout, dst->type);
24333 fprintf(state->errout, "\n");
24334 internal_error(state, dst, "unhandled dst type size");
24335 }
24336 mask = 1;
24337 mask <<= size_of(state, dst->type);
24338 mask -= 1;
24339
24340 dst_reg = ID_REG(dst->id);
24341 dst_regcm = arch_reg_regcm(state, dst_reg);
24342
24343 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24344 fprintf(fp, "\tand $0x%lx, %s\n",
24345 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24346 }
24347 else if (dst_regcm & REGCM_MMX) {
24348 long ref;
24349 ref = get_mask_pool_ref(state, dst, mask, fp);
24350 fprintf(fp, "\tpand L%s%lu, %s\n",
24351 state->compiler->label_prefix, ref,
24352 reg(state, dst, REGCM_MMX));
24353 }
24354 else if (dst_regcm & REGCM_XMM) {
24355 long ref;
24356 ref = get_mask_pool_ref(state, dst, mask, fp);
24357 fprintf(fp, "\tpand L%s%lu, %s\n",
24358 state->compiler->label_prefix, ref,
24359 reg(state, dst, REGCM_XMM));
24360 }
24361 else {
24362 fprintf(state->errout, "dst type: ");
24363 name_of(state->errout, dst->type);
24364 fprintf(state->errout, "\n");
24365 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24366 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24367 }
24368 }
24369 /* Make certain I am properly sign extended */
24370 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24371 (is_signed(src->type)))
24372 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024373 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024374 int dst_reg;
24375 int dst_regcm;
24376
Eric Biederman90089602004-05-28 14:11:54 +000024377 reg_bits = reg_size(state, dst);
24378 if (reg_bits > 32) {
24379 reg_bits = 32;
24380 }
24381 shift_bits = reg_bits - size_of(state, src->type);
24382 dst_reg = ID_REG(dst->id);
24383 dst_regcm = arch_reg_regcm(state, dst_reg);
24384
24385 if (shift_bits < 0) {
24386 internal_error(state, dst, "negative shift?");
24387 }
24388
24389 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024390 fprintf(fp, "\tshl $%d, %s\n",
24391 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024392 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024393 fprintf(fp, "\tsar $%d, %s\n",
24394 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024395 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24396 }
24397 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24398 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024399 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024400 reg(state, dst, REGCM_MMX | REGCM_XMM));
24401 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024402 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024403 reg(state, dst, REGCM_MMX | REGCM_XMM));
24404 }
24405 else {
24406 fprintf(state->errout, "dst type: ");
24407 name_of(state->errout, dst->type);
24408 fprintf(state->errout, "\n");
24409 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24410 internal_error(state, dst, "failed to signed extend value");
24411 }
24412 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024413}
24414
24415static void print_op_load(struct compile_state *state,
24416 struct triple *ins, FILE *fp)
24417{
24418 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024419 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024420 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024421 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024422 if (is_const(src) || is_const(dst)) {
24423 internal_error(state, ins, "unknown load operation");
24424 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024425 switch(ins->type->type & TYPE_MASK) {
24426 case TYPE_CHAR: op = "movsbl"; break;
24427 case TYPE_UCHAR: op = "movzbl"; break;
24428 case TYPE_SHORT: op = "movswl"; break;
24429 case TYPE_USHORT: op = "movzwl"; break;
24430 case TYPE_INT: case TYPE_UINT:
24431 case TYPE_LONG: case TYPE_ULONG:
24432 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024433 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024434 break;
24435 default:
24436 internal_error(state, ins, "unknown type in load");
24437 op = "<invalid opcode>";
24438 break;
24439 }
24440 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024441 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024442 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024443 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024444}
24445
24446
24447static void print_op_store(struct compile_state *state,
24448 struct triple *ins, FILE *fp)
24449{
24450 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024451 dst = RHS(ins, 0);
24452 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024453 if (is_const(src) && (src->op == OP_INTCONST)) {
24454 long_t value;
24455 value = (long_t)(src->u.cval);
24456 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24457 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024458 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024459 reg(state, dst, REGCM_GPR32));
24460 }
24461 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24462 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24463 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024464 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024465 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024466 }
24467 else {
24468 if (is_const(src) || is_const(dst)) {
24469 internal_error(state, ins, "unknown store operation");
24470 }
24471 fprintf(fp, "\tmov%s %s, (%s)\n",
24472 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024473 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024474 reg(state, dst, REGCM_GPR32));
24475 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024476
24477
Eric Biedermanb138ac82003-04-22 18:44:01 +000024478}
24479
24480static void print_op_smul(struct compile_state *state,
24481 struct triple *ins, FILE *fp)
24482{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024483 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024484 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024485 reg(state, RHS(ins, 1), REGCM_GPR32),
24486 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024487 }
24488 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024489 fprintf(fp, "\timul ");
24490 print_const_val(state, RHS(ins, 1), fp);
24491 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024492 }
24493}
24494
24495static void print_op_cmp(struct compile_state *state,
24496 struct triple *ins, FILE *fp)
24497{
24498 unsigned mask;
24499 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024500 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024501 dreg = check_reg(state, ins, REGCM_FLAGS);
24502 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24503 internal_error(state, ins, "bad dest register for cmp");
24504 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024505 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024506 fprintf(fp, "\tcmp ");
24507 print_const_val(state, RHS(ins, 1), fp);
24508 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024509 }
24510 else {
24511 unsigned lmask, rmask;
24512 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024513 lreg = check_reg(state, RHS(ins, 0), mask);
24514 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024515 lmask = arch_reg_regcm(state, lreg);
24516 rmask = arch_reg_regcm(state, rreg);
24517 mask = lmask & rmask;
24518 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024519 reg(state, RHS(ins, 1), mask),
24520 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024521 }
24522}
24523
24524static void print_op_test(struct compile_state *state,
24525 struct triple *ins, FILE *fp)
24526{
24527 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024528 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024529 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024530 reg(state, RHS(ins, 0), mask),
24531 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024532}
24533
24534static void print_op_branch(struct compile_state *state,
24535 struct triple *branch, FILE *fp)
24536{
24537 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024538 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024539 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024540 internal_error(state, branch, "jmp with condition?");
24541 }
24542 bop = "jmp";
24543 }
24544 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024545 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024546 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024547 internal_error(state, branch, "jmpcc without condition?");
24548 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024549 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24550 if ((RHS(branch, 0)->op != OP_CMP) &&
24551 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024552 internal_error(state, branch, "bad branch test");
24553 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024554#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024555#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024556#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024557 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24558 if (ptr->op != OP_COPY) {
24559 internal_error(state, branch, "branch does not follow test");
24560 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024561 }
24562 switch(branch->op) {
24563 case OP_JMP_EQ: bop = "jz"; break;
24564 case OP_JMP_NOTEQ: bop = "jnz"; break;
24565 case OP_JMP_SLESS: bop = "jl"; break;
24566 case OP_JMP_ULESS: bop = "jb"; break;
24567 case OP_JMP_SMORE: bop = "jg"; break;
24568 case OP_JMP_UMORE: bop = "ja"; break;
24569 case OP_JMP_SLESSEQ: bop = "jle"; break;
24570 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24571 case OP_JMP_SMOREEQ: bop = "jge"; break;
24572 case OP_JMP_UMOREEQ: bop = "jae"; break;
24573 default:
24574 internal_error(state, branch, "Invalid branch op");
24575 break;
24576 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024577
Eric Biedermanb138ac82003-04-22 18:44:01 +000024578 }
Eric Biederman90089602004-05-28 14:11:54 +000024579#if 1
24580 if (branch->op == OP_CALL) {
24581 fprintf(fp, "\t/* call */\n");
24582 }
24583#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024584 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024585 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024586 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024587 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024588}
24589
Eric Biederman5ade04a2003-10-22 04:03:46 +000024590static void print_op_ret(struct compile_state *state,
24591 struct triple *branch, FILE *fp)
24592{
24593 fprintf(fp, "\tjmp *%s\n",
24594 reg(state, RHS(branch, 0), REGCM_GPR32));
24595}
24596
Eric Biedermanb138ac82003-04-22 18:44:01 +000024597static void print_op_set(struct compile_state *state,
24598 struct triple *set, FILE *fp)
24599{
24600 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024601 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024602 internal_error(state, set, "setcc without condition?");
24603 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024604 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24605 if ((RHS(set, 0)->op != OP_CMP) &&
24606 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024607 internal_error(state, set, "bad set test");
24608 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024609 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024610 internal_error(state, set, "set does not follow test");
24611 }
24612 switch(set->op) {
24613 case OP_SET_EQ: sop = "setz"; break;
24614 case OP_SET_NOTEQ: sop = "setnz"; break;
24615 case OP_SET_SLESS: sop = "setl"; break;
24616 case OP_SET_ULESS: sop = "setb"; break;
24617 case OP_SET_SMORE: sop = "setg"; break;
24618 case OP_SET_UMORE: sop = "seta"; break;
24619 case OP_SET_SLESSEQ: sop = "setle"; break;
24620 case OP_SET_ULESSEQ: sop = "setbe"; break;
24621 case OP_SET_SMOREEQ: sop = "setge"; break;
24622 case OP_SET_UMOREEQ: sop = "setae"; break;
24623 default:
24624 internal_error(state, set, "Invalid set op");
24625 break;
24626 }
24627 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024628 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024629}
24630
Stefan Reinauer14e22772010-04-27 06:56:47 +000024631static void print_op_bit_scan(struct compile_state *state,
24632 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024633{
24634 const char *op;
24635 switch(ins->op) {
24636 case OP_BSF: op = "bsf"; break;
24637 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024638 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024639 internal_error(state, ins, "unknown bit scan");
24640 op = 0;
24641 break;
24642 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024643 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024644 "\t%s %s, %s\n"
24645 "\tjnz 1f\n"
24646 "\tmovl $-1, %s\n"
24647 "1:\n",
24648 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024649 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024650 reg(state, ins, REGCM_GPR32),
24651 reg(state, ins, REGCM_GPR32));
24652}
24653
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024654
Eric Biedermanb138ac82003-04-22 18:44:01 +000024655static void print_sdecl(struct compile_state *state,
24656 struct triple *ins, FILE *fp)
24657{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024658 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024659 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024660 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024661 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024662 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024663 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024664
Eric Biedermanb138ac82003-04-22 18:44:01 +000024665}
24666
24667static void print_instruction(struct compile_state *state,
24668 struct triple *ins, FILE *fp)
24669{
24670 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024671 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024672 */
24673 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024674 case OP_ASM:
24675 print_op_asm(state, ins, fp);
24676 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024677 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24678 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24679 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24680 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24681 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24682 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24683 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24684 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24685 case OP_POS: break;
24686 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24687 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024688 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024689 case OP_INTCONST:
24690 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024691 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024692 /* Don't generate anything here for constants */
24693 case OP_PHI:
24694 /* Don't generate anything for variable declarations. */
24695 break;
Eric Biederman90089602004-05-28 14:11:54 +000024696 case OP_UNKNOWNVAL:
24697 fprintf(fp, " /* unknown %s */\n",
24698 reg(state, ins, REGCM_ALL));
24699 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024700 case OP_SDECL:
24701 print_sdecl(state, ins, fp);
24702 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024703 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024704 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024705 print_op_move(state, ins, fp);
24706 break;
24707 case OP_LOAD:
24708 print_op_load(state, ins, fp);
24709 break;
24710 case OP_STORE:
24711 print_op_store(state, ins, fp);
24712 break;
24713 case OP_SMUL:
24714 print_op_smul(state, ins, fp);
24715 break;
24716 case OP_CMP: print_op_cmp(state, ins, fp); break;
24717 case OP_TEST: print_op_test(state, ins, fp); break;
24718 case OP_JMP:
24719 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24720 case OP_JMP_SLESS: case OP_JMP_ULESS:
24721 case OP_JMP_SMORE: case OP_JMP_UMORE:
24722 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24723 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024724 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024725 print_op_branch(state, ins, fp);
24726 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024727 case OP_RET:
24728 print_op_ret(state, ins, fp);
24729 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024730 case OP_SET_EQ: case OP_SET_NOTEQ:
24731 case OP_SET_SLESS: case OP_SET_ULESS:
24732 case OP_SET_SMORE: case OP_SET_UMORE:
24733 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24734 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24735 print_op_set(state, ins, fp);
24736 break;
24737 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024738 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024739 break;
24740 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024741 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024742 break;
24743 case OP_BSF:
24744 case OP_BSR:
24745 print_op_bit_scan(state, ins, fp);
24746 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024747 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024748 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024749 fprintf(fp, "\trdmsr\n");
24750 break;
24751 case OP_WRMSR:
24752 fprintf(fp, "\twrmsr\n");
24753 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024754 case OP_HLT:
24755 fprintf(fp, "\thlt\n");
24756 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024757 case OP_SDIVT:
24758 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24759 break;
24760 case OP_UDIVT:
24761 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24762 break;
24763 case OP_UMUL:
24764 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24765 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024766 case OP_LABEL:
24767 if (!ins->use) {
24768 return;
24769 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024770 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024771 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024772 break;
Eric Biederman90089602004-05-28 14:11:54 +000024773 case OP_ADECL:
24774 /* Ignore adecls with no registers error otherwise */
24775 if (!noop_adecl(ins)) {
24776 internal_error(state, ins, "adecl remains?");
24777 }
24778 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024779 /* Ignore OP_PIECE */
24780 case OP_PIECE:
24781 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024782 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024783 case OP_SDIV: case OP_UDIV:
24784 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024785 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24786 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24787 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24788 default:
24789 internal_error(state, ins, "unknown op: %d %s",
24790 ins->op, tops(ins->op));
24791 break;
24792 }
24793}
24794
24795static void print_instructions(struct compile_state *state)
24796{
24797 struct triple *first, *ins;
24798 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024799 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024800 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024801 int max_inline_depth;
24802 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024803 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024804 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024805 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024806 /* Masks for common sizes */
24807 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24808 fprintf(fp, ".balign 16\n");
24809 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24810 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24811 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24812 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024813 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024814 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024815 ins = first;
24816 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024817 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024818 last_occurance != ins->occurance) {
24819 if (!ins->occurance->parent) {
24820 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024821 ins->occurance->function?ins->occurance->function:"(null)",
24822 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024823 ins->occurance->line,
24824 ins->occurance->col);
24825 }
24826 else {
24827 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024828 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024829 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024830 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024831 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024832 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024833 fprintf(fp, "\t * %s,%s:%d.%d\n",
24834 ptr->function,
24835 ptr->filename,
24836 ptr->line,
24837 ptr->col);
24838 }
24839 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024840 if (inline_depth > max_inline_depth) {
24841 max_inline_depth = inline_depth;
24842 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024843 }
24844 if (last_occurance) {
24845 put_occurance(last_occurance);
24846 }
24847 get_occurance(ins->occurance);
24848 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024849 }
24850
24851 print_instruction(state, ins, fp);
24852 ins = ins->next;
24853 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024854 if (print_location) {
24855 fprintf(fp, "/* max inline depth %d */\n",
24856 max_inline_depth);
24857 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024858}
Eric Biederman530b5192003-07-01 10:05:30 +000024859
Eric Biedermanb138ac82003-04-22 18:44:01 +000024860static void generate_code(struct compile_state *state)
24861{
24862 generate_local_labels(state);
24863 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024864
Eric Biedermanb138ac82003-04-22 18:44:01 +000024865}
24866
Eric Biederman90089602004-05-28 14:11:54 +000024867static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024868{
Eric Biederman90089602004-05-28 14:11:54 +000024869 int tok;
24870 FILE *fp;
24871 int line;
24872 const char *filename;
24873 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024874 filename = 0;
24875 line = 0;
24876 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024877 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024878 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024879 const char *token_str;
24880 tok = peek(state);
24881 if (tok == TOK_EOF) {
24882 break;
24883 }
Eric Biederman41203d92004-11-08 09:31:09 +000024884 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024885 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024886 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024887 tk->str_len ? tk->val.str :
24888 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024889
24890 file = state->file;
24891 while(file->macro && file->prev) {
24892 file = file->prev;
24893 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024894 if (!file->macro &&
24895 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024896 {
Eric Biederman90089602004-05-28 14:11:54 +000024897 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024898 if ((file->basename == filename) &&
24899 (line < file->line)) {
24900 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024901 fprintf(fp, "\n");
24902 line++;
24903 }
24904 }
24905 else {
24906 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024907 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024908 }
Eric Biederman132368b2004-11-09 08:59:23 +000024909 line = file->line;
24910 filename = file->basename;
24911 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024912 for(i = 0; i < col; i++) {
24913 fprintf(fp, " ");
24914 }
24915 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024916
Eric Biederman90089602004-05-28 14:11:54 +000024917 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024918
Eric Biederman90089602004-05-28 14:11:54 +000024919 if (state->compiler->debug & DEBUG_TOKENS) {
24920 loc(state->dbgout, state, 0);
24921 fprintf(state->dbgout, "%s <- `%s'\n",
24922 tokens[tok], token_str);
24923 }
24924 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024925}
24926
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024927static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024928 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024929{
24930 int i;
24931 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024932 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024933 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024934 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024935 state.compiler = compiler;
24936 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024937 state.file = 0;
24938 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24939 memset(&state.token[i], 0, sizeof(state.token[i]));
24940 state.token[i].tok = -1;
24941 }
Eric Biederman90089602004-05-28 14:11:54 +000024942 /* Remember the output descriptors */
24943 state.errout = stderr;
24944 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024945 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024946 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24947 state.output = stdout;
24948 } else {
24949 state.output = fopen(state.compiler->ofilename, "w");
24950 if (!state.output) {
24951 error(&state, 0, "Cannot open output file %s\n",
24952 state.compiler->ofilename);
24953 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024954 }
Eric Biederman90089602004-05-28 14:11:54 +000024955 /* Make certain a good cleanup happens */
24956 exit_state = &state;
24957 atexit(exit_cleanup);
24958
Eric Biedermanb138ac82003-04-22 18:44:01 +000024959 /* Prep the preprocessor */
24960 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024961 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024962 /* register the C keywords */
24963 register_keywords(&state);
24964 /* register the keywords the macro preprocessor knows */
24965 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024966 /* generate some builtin macros */
24967 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024968 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024969 state.i_switch = lookup(&state, "switch", 6);
24970 state.i_case = lookup(&state, "case", 4);
24971 state.i_continue = lookup(&state, "continue", 8);
24972 state.i_break = lookup(&state, "break", 5);
24973 state.i_default = lookup(&state, "default", 7);
24974 state.i_return = lookup(&state, "return", 6);
24975 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024976 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24977 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24978 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24979 /* Memorize where predefined identifiers are. */
24980 state.i___func__ = lookup(&state, "__func__", 8);
24981 /* Memorize where some attribute keywords are. */
24982 state.i_noinline = lookup(&state, "noinline", 8);
24983 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024984 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020024985 state.i_unused = lookup(&state, "unused", 6);
Stefan Reinauerec664bc2013-05-09 14:06:04 -070024986 state.i_packed = lookup(&state, "packed", 6);
Eric Biederman90089602004-05-28 14:11:54 +000024987
24988 /* Process the command line macros */
24989 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024990
24991 /* Allocate beginning bounding labels for the function list */
24992 state.first = label(&state);
24993 state.first->id |= TRIPLE_FLAG_VOLATILE;
24994 use_triple(state.first, state.first);
24995 ptr = label(&state);
24996 ptr->id |= TRIPLE_FLAG_VOLATILE;
24997 use_triple(ptr, ptr);
24998 flatten(&state, state.first, ptr);
24999
Eric Biederman5ade04a2003-10-22 04:03:46 +000025000 /* Allocate a label for the pool of global variables */
25001 state.global_pool = label(&state);
25002 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25003 flatten(&state, state.first, state.global_pool);
25004
Eric Biedermanb138ac82003-04-22 18:44:01 +000025005 /* Enter the globl definition scope */
25006 start_scope(&state);
25007 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025008
Eric Biedermanb138ac82003-04-22 18:44:01 +000025009 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025010
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025011 while (includes) {
25012 compile_file(&state, includes->filename, 1);
25013 includes=includes->next;
25014 }
Eric Biederman90089602004-05-28 14:11:54 +000025015
25016 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025017 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025018 print_preprocessed_tokens(&state);
25019 return;
25020 }
25021
Eric Biedermanb138ac82003-04-22 18:44:01 +000025022 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025023
Eric Biedermanb138ac82003-04-22 18:44:01 +000025024 /* Exit the global definition scope */
25025 end_scope(&state);
25026
Stefan Reinauer14e22772010-04-27 06:56:47 +000025027 /* Now that basic compilation has happened
25028 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025029 */
25030 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025031
Eric Biedermanb138ac82003-04-22 18:44:01 +000025032 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025033 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025034 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025035 }
Eric Biederman90089602004-05-28 14:11:54 +000025036 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025037}
25038
Eric Biederman90089602004-05-28 14:11:54 +000025039static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025040{
Eric Biederman90089602004-05-28 14:11:54 +000025041 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025042}
25043
25044static void usage(void)
25045{
Eric Biederman90089602004-05-28 14:11:54 +000025046 FILE *fp = stdout;
25047 version(fp);
25048 fprintf(fp,
25049 "\nUsage: romcc [options] <source>.c\n"
25050 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25051 "Options: \n"
25052 "-o <output file name>\n"
25053 "-f<option> Specify a generic compiler option\n"
25054 "-m<option> Specify a arch dependent option\n"
25055 "-- Specify this is the last option\n"
25056 "\nGeneric compiler options:\n"
25057 );
25058 compiler_usage(fp);
25059 fprintf(fp,
25060 "\nArchitecture compiler options:\n"
25061 );
25062 arch_usage(fp);
25063 fprintf(fp,
25064 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025065 );
25066}
25067
25068static void arg_error(char *fmt, ...)
25069{
25070 va_list args;
25071 va_start(args, fmt);
25072 vfprintf(stderr, fmt, args);
25073 va_end(args);
25074 usage();
25075 exit(1);
25076}
25077
Stefan Reinauer3d0ba1c2015-11-18 17:23:39 -080025078static void arg_warning(char *fmt, ...)
25079{
25080 va_list args;
25081
25082 va_start(args, fmt);
25083 vfprintf(stderr, fmt, args);
25084 va_end(args);
25085}
25086
Eric Biedermanb138ac82003-04-22 18:44:01 +000025087int main(int argc, char **argv)
25088{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025089 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025090 struct compiler_state compiler;
25091 struct arch_state arch;
25092 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025093
25094
Eric Biederman90089602004-05-28 14:11:54 +000025095 /* I don't want any surprises */
25096 setlocale(LC_ALL, "C");
25097
Eric Biederman5ade04a2003-10-22 04:03:46 +000025098 init_compiler_state(&compiler);
25099 init_arch_state(&arch);
25100 filename = 0;
25101 all_opts = 0;
25102 while(argc > 1) {
25103 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25104 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025105 argv += 2;
25106 argc -= 2;
25107 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025108 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025109 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025110 result = -1;
25111 if (strcmp(argv[1], "--") == 0) {
25112 result = 0;
25113 all_opts = 1;
25114 }
Eric Biederman90089602004-05-28 14:11:54 +000025115 else if (strncmp(argv[1], "-E", 2) == 0) {
25116 result = compiler_encode_flag(&compiler, argv[1]);
25117 }
25118 else if (strncmp(argv[1], "-O", 2) == 0) {
25119 result = compiler_encode_flag(&compiler, argv[1]);
25120 }
25121 else if (strncmp(argv[1], "-I", 2) == 0) {
25122 result = compiler_encode_flag(&compiler, argv[1]);
25123 }
25124 else if (strncmp(argv[1], "-D", 2) == 0) {
25125 result = compiler_encode_flag(&compiler, argv[1]);
25126 }
25127 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025128 result = compiler_encode_flag(&compiler, argv[1]);
25129 }
25130 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25131 result = compiler_encode_flag(&compiler, argv[1]+2);
25132 }
25133 else if (strncmp(argv[1], "-f", 2) == 0) {
25134 result = compiler_encode_flag(&compiler, argv[1]+2);
25135 }
25136 else if (strncmp(argv[1], "-m", 2) == 0) {
25137 result = arch_encode_flag(&arch, argv[1]+2);
25138 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025139 else if (strncmp(argv[1], "-c", 2) == 0) {
25140 result = 0;
25141 }
25142 else if (strncmp(argv[1], "-S", 2) == 0) {
25143 result = 0;
25144 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025145 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025146 struct filelist *old_head = include_filelist;
25147 include_filelist = malloc(sizeof(struct filelist));
25148 if (!include_filelist) {
25149 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025150 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025151 argv++;
25152 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025153 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025154 include_filelist->next = old_head;
25155 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025156 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025157 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025158 arg_error("Invalid option specified: %s\n",
25159 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025160 }
25161 argv++;
25162 argc--;
25163 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025164 else {
25165 if (filename) {
25166 arg_error("Only one filename may be specified\n");
25167 }
25168 filename = argv[1];
25169 argv++;
25170 argc--;
25171 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025172 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025173 if (!filename) {
25174 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025175 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025176 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025177
25178 return 0;
25179}