blob: b045b468e48607284537afb497ef3f77a3d4ca2d [file] [log] [blame]
Eric Biederman90089602004-05-28 14:11:54 +00001#undef VERSION_MAJOR
2#undef VERSION_MINOR
3#undef RELEASE_DATE
4#undef VERSION
5#define VERSION_MAJOR "0"
Stefan Reinauerec664bc2013-05-09 14:06:04 -07006#define VERSION_MINOR "73"
7#define RELEASE_DATE "08 May 2013"
Eric Biederman90089602004-05-28 14:11:54 +00008#define VERSION VERSION_MAJOR "." VERSION_MINOR
9
Eric Biedermanb138ac82003-04-22 18:44:01 +000010#include <stdarg.h>
11#include <errno.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <stdio.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <stdio.h>
20#include <string.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000021#include <limits.h>
Eric Biederman90089602004-05-28 14:11:54 +000022#include <locale.h>
23#include <time.h>
Eric Biedermanb138ac82003-04-22 18:44:01 +000024
Eric Biederman90089602004-05-28 14:11:54 +000025#define MAX_CWD_SIZE 4096
Eric Biederman5ade04a2003-10-22 04:03:46 +000026#define MAX_ALLOCATION_PASSES 100
27
Stefan Reinauer14e22772010-04-27 06:56:47 +000028/* NOTE: Before you even start thinking to touch anything
Stefan Reinauer50542a82007-10-24 11:14:14 +000029 * in this code, set DEBUG_ROMCC_WARNINGS to 1 to get an
Stefan Reinauer14e22772010-04-27 06:56:47 +000030 * insight on the original author's thoughts. We introduced
Stefan Reinauer50542a82007-10-24 11:14:14 +000031 * this switch as romcc was about the only thing producing
32 * massive warnings in our code..
33 */
34#define DEBUG_ROMCC_WARNINGS 0
35
Eric Biedermana4aef6d2003-12-05 06:16:19 +000036#define DEBUG_CONSISTENCY 1
Eric Biederman530b5192003-07-01 10:05:30 +000037#define DEBUG_SDP_BLOCKS 0
38#define DEBUG_TRIPLE_COLOR 0
Eric Biedermanb138ac82003-04-22 18:44:01 +000039
Eric Biederman90089602004-05-28 14:11:54 +000040#define DEBUG_DISPLAY_USES 1
41#define DEBUG_DISPLAY_TYPES 1
42#define DEBUG_REPLACE_CLOSURE_TYPE_HIRES 0
43#define DEBUG_DECOMPOSE_PRINT_TUPLES 0
44#define DEBUG_DECOMPOSE_HIRES 0
45#define DEBUG_INITIALIZER 0
46#define DEBUG_UPDATE_CLOSURE_TYPE 0
47#define DEBUG_LOCAL_TRIPLE 0
48#define DEBUG_BASIC_BLOCKS_VERBOSE 0
49#define DEBUG_CPS_RENAME_VARIABLES_HIRES 0
50#define DEBUG_SIMPLIFY_HIRES 0
51#define DEBUG_SHRINKING 0
52#define DEBUG_COALESCE_HITCHES 0
53#define DEBUG_CODE_ELIMINATION 0
54
55#define DEBUG_EXPLICIT_CLOSURES 0
56
Stefan Reinauer50542a82007-10-24 11:14:14 +000057#if DEBUG_ROMCC_WARNINGS
Eric Biederman8d9c1232003-06-17 08:42:17 +000058#warning "FIXME give clear error messages about unused variables"
Eric Biederman530b5192003-07-01 10:05:30 +000059#warning "FIXME properly handle multi dimensional arrays"
Eric Biederman90089602004-05-28 14:11:54 +000060#warning "FIXME handle multiple register sizes"
Stefan Reinauer50542a82007-10-24 11:14:14 +000061#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000062
Eric Biedermanb138ac82003-04-22 18:44:01 +000063/* Control flow graph of a loop without goto.
Stefan Reinauer14e22772010-04-27 06:56:47 +000064 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000065 * AAA
66 * +---/
67 * /
68 * / +--->CCC
69 * | | / \
70 * | | DDD EEE break;
71 * | | \ \
72 * | | FFF \
73 * \| / \ \
74 * |\ GGG HHH | continue;
75 * | \ \ | |
76 * | \ III | /
Stefan Reinauer14e22772010-04-27 06:56:47 +000077 * | \ | / /
78 * | vvv /
79 * +----BBB /
Eric Biedermanb138ac82003-04-22 18:44:01 +000080 * | /
81 * vv
82 * JJJ
83 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000084 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000085 * AAA
86 * +-----+ | +----+
87 * | \ | / |
88 * | BBB +-+ |
89 * | / \ / | |
90 * | CCC JJJ / /
Stefan Reinauer14e22772010-04-27 06:56:47 +000091 * | / \ / /
92 * | DDD EEE / /
Eric Biedermanb138ac82003-04-22 18:44:01 +000093 * | | +-/ /
Stefan Reinauer14e22772010-04-27 06:56:47 +000094 * | FFF /
95 * | / \ /
96 * | GGG HHH /
Eric Biedermanb138ac82003-04-22 18:44:01 +000097 * | | +-/
98 * | III
Stefan Reinauer14e22772010-04-27 06:56:47 +000099 * +--+
Eric Biedermanb138ac82003-04-22 18:44:01 +0000100 *
Stefan Reinauer14e22772010-04-27 06:56:47 +0000101 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000102 * DFlocal(X) = { Y <- Succ(X) | idom(Y) != X }
103 * DFup(Z) = { Y <- DF(Z) | idom(Y) != X }
104 *
105 *
106 * [] == DFlocal(X) U DF(X)
107 * () == DFup(X)
108 *
109 * Dominator graph of the same nodes.
110 *
111 * AAA AAA: [ ] ()
112 * / \
113 * BBB JJJ BBB: [ JJJ ] ( JJJ ) JJJ: [ ] ()
114 * |
115 * CCC CCC: [ ] ( BBB, JJJ )
116 * / \
117 * DDD EEE DDD: [ ] ( BBB ) EEE: [ JJJ ] ()
118 * |
119 * FFF FFF: [ ] ( BBB )
Stefan Reinauer14e22772010-04-27 06:56:47 +0000120 * / \
Eric Biedermanb138ac82003-04-22 18:44:01 +0000121 * GGG HHH GGG: [ ] ( BBB ) HHH: [ BBB ] ()
122 * |
123 * III III: [ BBB ] ()
124 *
125 *
126 * BBB and JJJ are definitely the dominance frontier.
127 * Where do I place phi functions and how do I make that decision.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000128 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000129 */
Stefan Reinauerb4d3af82010-02-11 03:21:29 +0000130
131struct filelist {
132 const char *filename;
133 struct filelist *next;
134};
135
136struct filelist *include_filelist = NULL;
137
Stefan Reinauerbed872d2010-04-03 00:03:07 +0000138static void __attribute__((noreturn)) die(char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000139{
140 va_list args;
141
142 va_start(args, fmt);
143 vfprintf(stderr, fmt, args);
144 va_end(args);
145 fflush(stdout);
146 fflush(stderr);
147 exit(1);
148}
149
Eric Biedermanb138ac82003-04-22 18:44:01 +0000150static void *xmalloc(size_t size, const char *name)
151{
152 void *buf;
153 buf = malloc(size);
154 if (!buf) {
155 die("Cannot malloc %ld bytes to hold %s: %s\n",
156 size + 0UL, name, strerror(errno));
157 }
158 return buf;
159}
160
161static void *xcmalloc(size_t size, const char *name)
162{
163 void *buf;
164 buf = xmalloc(size, name);
165 memset(buf, 0, size);
166 return buf;
167}
168
Eric Biederman90089602004-05-28 14:11:54 +0000169static void *xrealloc(void *ptr, size_t size, const char *name)
170{
171 void *buf;
172 buf = realloc(ptr, size);
173 if (!buf) {
174 die("Cannot realloc %ld bytes to hold %s: %s\n",
175 size + 0UL, name, strerror(errno));
176 }
177 return buf;
178}
179
Eric Biedermanb138ac82003-04-22 18:44:01 +0000180static void xfree(const void *ptr)
181{
182 free((void *)ptr);
183}
184
185static char *xstrdup(const char *str)
186{
187 char *new;
188 int len;
189 len = strlen(str);
190 new = xmalloc(len + 1, "xstrdup string");
191 memcpy(new, str, len);
192 new[len] = '\0';
193 return new;
194}
195
196static void xchdir(const char *path)
197{
198 if (chdir(path) != 0) {
Eric Biederman90089602004-05-28 14:11:54 +0000199 die("chdir to `%s' failed: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +0000200 path, strerror(errno));
201 }
202}
203
204static int exists(const char *dirname, const char *filename)
205{
Eric Biederman90089602004-05-28 14:11:54 +0000206 char cwd[MAX_CWD_SIZE];
207 int does_exist;
208
209 if (getcwd(cwd, sizeof(cwd)) == 0) {
210 die("cwd buffer to small");
211 }
212
213 does_exist = 1;
214 if (chdir(dirname) != 0) {
215 does_exist = 0;
216 }
217 if (does_exist && (access(filename, O_RDONLY) < 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000218 if ((errno != EACCES) && (errno != EROFS)) {
219 does_exist = 0;
220 }
221 }
Eric Biederman90089602004-05-28 14:11:54 +0000222 xchdir(cwd);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000223 return does_exist;
224}
225
226
227static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
228{
Eric Biederman90089602004-05-28 14:11:54 +0000229 char cwd[MAX_CWD_SIZE];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000230 char *buf;
231 off_t size, progress;
232 ssize_t result;
Patrick Georgi26774f22009-11-21 19:54:02 +0000233 FILE* file;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000234
Eric Biedermanb138ac82003-04-22 18:44:01 +0000235 if (!filename) {
236 *r_size = 0;
237 return 0;
238 }
Eric Biederman90089602004-05-28 14:11:54 +0000239 if (getcwd(cwd, sizeof(cwd)) == 0) {
240 die("cwd buffer to small");
241 }
Eric Biedermanb138ac82003-04-22 18:44:01 +0000242 xchdir(dirname);
Patrick Georgi26774f22009-11-21 19:54:02 +0000243 file = fopen(filename, "rb");
Eric Biederman90089602004-05-28 14:11:54 +0000244 xchdir(cwd);
Patrick Georgi26774f22009-11-21 19:54:02 +0000245 if (file == NULL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000246 die("Cannot open '%s' : %s\n",
247 filename, strerror(errno));
248 }
Patrick Georgi26774f22009-11-21 19:54:02 +0000249 fseek(file, 0, SEEK_END);
250 size = ftell(file);
251 fseek(file, 0, SEEK_SET);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000252 *r_size = size +1;
253 buf = xmalloc(size +2, filename);
254 buf[size] = '\n'; /* Make certain the file is newline terminated */
255 buf[size+1] = '\0'; /* Null terminate the file for good measure */
256 progress = 0;
257 while(progress < size) {
Patrick Georgi26774f22009-11-21 19:54:02 +0000258 result = fread(buf + progress, 1, size - progress, file);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000259 if (result < 0) {
260 if ((errno == EINTR) || (errno == EAGAIN))
261 continue;
262 die("read on %s of %ld bytes failed: %s\n",
263 filename, (size - progress)+ 0UL, strerror(errno));
264 }
265 progress += result;
266 }
Patrick Georgi26774f22009-11-21 19:54:02 +0000267 fclose(file);
Eric Biedermanb138ac82003-04-22 18:44:01 +0000268 return buf;
269}
270
Eric Biederman83b991a2003-10-11 06:20:25 +0000271/* Types on the destination platform */
Stefan Reinauer50542a82007-10-24 11:14:14 +0000272#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +0000273#warning "FIXME this assumes 32bit x86 is the destination"
Stefan Reinauer50542a82007-10-24 11:14:14 +0000274#endif
Eric Biederman83b991a2003-10-11 06:20:25 +0000275typedef int8_t schar_t;
276typedef uint8_t uchar_t;
277typedef int8_t char_t;
278typedef int16_t short_t;
279typedef uint16_t ushort_t;
280typedef int32_t int_t;
281typedef uint32_t uint_t;
282typedef int32_t long_t;
Patrick Georgia84a99b2009-05-26 14:03:51 +0000283#define ulong_t uint32_t
Eric Biederman83b991a2003-10-11 06:20:25 +0000284
285#define SCHAR_T_MIN (-128)
286#define SCHAR_T_MAX 127
287#define UCHAR_T_MAX 255
288#define CHAR_T_MIN SCHAR_T_MIN
289#define CHAR_T_MAX SCHAR_T_MAX
290#define SHRT_T_MIN (-32768)
291#define SHRT_T_MAX 32767
292#define USHRT_T_MAX 65535
293#define INT_T_MIN (-LONG_T_MAX - 1)
294#define INT_T_MAX 2147483647
295#define UINT_T_MAX 4294967295U
296#define LONG_T_MIN (-LONG_T_MAX - 1)
297#define LONG_T_MAX 2147483647
298#define ULONG_T_MAX 4294967295U
Eric Biedermanb138ac82003-04-22 18:44:01 +0000299
Eric Biederman90089602004-05-28 14:11:54 +0000300#define SIZEOF_I8 8
301#define SIZEOF_I16 16
302#define SIZEOF_I32 32
303#define SIZEOF_I64 64
304
305#define SIZEOF_CHAR 8
306#define SIZEOF_SHORT 16
307#define SIZEOF_INT 32
308#define SIZEOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
309
310
311#define ALIGNOF_CHAR 8
312#define ALIGNOF_SHORT 16
313#define ALIGNOF_INT 32
314#define ALIGNOF_LONG (sizeof(long_t)*SIZEOF_CHAR)
315
316#define REG_SIZEOF_REG 32
317#define REG_SIZEOF_CHAR REG_SIZEOF_REG
318#define REG_SIZEOF_SHORT REG_SIZEOF_REG
319#define REG_SIZEOF_INT REG_SIZEOF_REG
320#define REG_SIZEOF_LONG REG_SIZEOF_REG
321
322#define REG_ALIGNOF_REG REG_SIZEOF_REG
323#define REG_ALIGNOF_CHAR REG_SIZEOF_REG
324#define REG_ALIGNOF_SHORT REG_SIZEOF_REG
325#define REG_ALIGNOF_INT REG_SIZEOF_REG
326#define REG_ALIGNOF_LONG REG_SIZEOF_REG
327
328/* Additional definitions for clarity.
329 * I currently assume a long is the largest native
330 * machine word and that a pointer fits into it.
331 */
332#define SIZEOF_WORD SIZEOF_LONG
333#define SIZEOF_POINTER SIZEOF_LONG
334#define ALIGNOF_WORD ALIGNOF_LONG
335#define ALIGNOF_POINTER ALIGNOF_LONG
336#define REG_SIZEOF_POINTER REG_SIZEOF_LONG
337#define REG_ALIGNOF_POINTER REG_ALIGNOF_LONG
338
Eric Biedermanb138ac82003-04-22 18:44:01 +0000339struct file_state {
340 struct file_state *prev;
341 const char *basename;
342 char *dirname;
Eric Biedermancb364952004-11-15 10:46:44 +0000343 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000344 off_t size;
Eric Biederman90089602004-05-28 14:11:54 +0000345 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000346 int line;
Eric Biederman90089602004-05-28 14:11:54 +0000347 const char *line_start;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000348 int report_line;
349 const char *report_name;
350 const char *report_dir;
Eric Biedermancb364952004-11-15 10:46:44 +0000351 int macro : 1;
352 int trigraphs : 1;
353 int join_lines : 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000354};
355struct hash_entry;
356struct token {
357 int tok;
358 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +0000359 const char *pos;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000360 int str_len;
361 union {
362 ulong_t integer;
363 const char *str;
Eric Biederman90089602004-05-28 14:11:54 +0000364 int notmacro;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000365 } val;
366};
367
368/* I have two classes of types:
369 * Operational types.
370 * Logical types. (The type the C standard says the operation is of)
371 *
372 * The operational types are:
373 * chars
374 * shorts
375 * ints
376 * longs
377 *
378 * floats
379 * doubles
380 * long doubles
381 *
382 * pointer
383 */
384
385
386/* Machine model.
387 * No memory is useable by the compiler.
388 * There is no floating point support.
389 * All operations take place in general purpose registers.
390 * There is one type of general purpose register.
391 * Unsigned longs are stored in that general purpose register.
392 */
393
394/* Operations on general purpose registers.
395 */
396
Eric Biederman530b5192003-07-01 10:05:30 +0000397#define OP_SDIVT 0
398#define OP_UDIVT 1
399#define OP_SMUL 2
400#define OP_UMUL 3
401#define OP_SDIV 4
402#define OP_UDIV 5
403#define OP_SMOD 6
404#define OP_UMOD 7
405#define OP_ADD 8
406#define OP_SUB 9
407#define OP_SL 10
408#define OP_USR 11
Stefan Reinauer14e22772010-04-27 06:56:47 +0000409#define OP_SSR 12
410#define OP_AND 13
Eric Biederman530b5192003-07-01 10:05:30 +0000411#define OP_XOR 14
412#define OP_OR 15
413#define OP_POS 16 /* Dummy positive operator don't use it */
414#define OP_NEG 17
415#define OP_INVERT 18
Stefan Reinauer14e22772010-04-27 06:56:47 +0000416
Eric Biedermanb138ac82003-04-22 18:44:01 +0000417#define OP_EQ 20
418#define OP_NOTEQ 21
419#define OP_SLESS 22
420#define OP_ULESS 23
421#define OP_SMORE 24
422#define OP_UMORE 25
423#define OP_SLESSEQ 26
424#define OP_ULESSEQ 27
425#define OP_SMOREEQ 28
426#define OP_UMOREEQ 29
Stefan Reinauer14e22772010-04-27 06:56:47 +0000427
Eric Biedermanb138ac82003-04-22 18:44:01 +0000428#define OP_LFALSE 30 /* Test if the expression is logically false */
429#define OP_LTRUE 31 /* Test if the expression is logcially true */
430
431#define OP_LOAD 32
432#define OP_STORE 33
Eric Biederman530b5192003-07-01 10:05:30 +0000433/* For OP_STORE ->type holds the type
434 * RHS(0) holds the destination address
435 * RHS(1) holds the value to store.
436 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000437
Eric Biederman90089602004-05-28 14:11:54 +0000438#define OP_UEXTRACT 34
439/* OP_UEXTRACT extracts an unsigned bitfield from a pseudo register
440 * RHS(0) holds the psuedo register to extract from
441 * ->type holds the size of the bitfield.
442 * ->u.bitfield.size holds the size of the bitfield.
443 * ->u.bitfield.offset holds the offset to extract from
444 */
445#define OP_SEXTRACT 35
446/* OP_SEXTRACT extracts a signed bitfield from a pseudo register
447 * RHS(0) holds the psuedo register to extract from
448 * ->type holds the size of the bitfield.
449 * ->u.bitfield.size holds the size of the bitfield.
450 * ->u.bitfield.offset holds the offset to extract from
451 */
452#define OP_DEPOSIT 36
453/* OP_DEPOSIT replaces a bitfield with a new value.
454 * RHS(0) holds the value to replace a bitifield in.
455 * RHS(1) holds the replacement value
456 * ->u.bitfield.size holds the size of the bitfield.
457 * ->u.bitfield.offset holds the deposit into
458 */
459
460#define OP_NOOP 37
Eric Biedermanb138ac82003-04-22 18:44:01 +0000461
462#define OP_MIN_CONST 50
Eric Biederman90089602004-05-28 14:11:54 +0000463#define OP_MAX_CONST 58
Eric Biedermanb138ac82003-04-22 18:44:01 +0000464#define IS_CONST_OP(X) (((X) >= OP_MIN_CONST) && ((X) <= OP_MAX_CONST))
465#define OP_INTCONST 50
Eric Biedermand1ea5392003-06-28 06:49:45 +0000466/* For OP_INTCONST ->type holds the type.
467 * ->u.cval holds the constant value.
468 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000469#define OP_BLOBCONST 51
Eric Biederman0babc1c2003-05-09 02:39:00 +0000470/* For OP_BLOBCONST ->type holds the layout and size
Eric Biedermanb138ac82003-04-22 18:44:01 +0000471 * information. u.blob holds a pointer to the raw binary
472 * data for the constant initializer.
473 */
474#define OP_ADDRCONST 52
Eric Biederman0babc1c2003-05-09 02:39:00 +0000475/* For OP_ADDRCONST ->type holds the type.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000476 * MISC(0) holds the reference to the static variable.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000477 * ->u.cval holds an offset from that value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000478 */
Eric Biederman90089602004-05-28 14:11:54 +0000479#define OP_UNKNOWNVAL 59
480/* For OP_UNKNOWNAL ->type holds the type.
481 * For some reason we don't know what value this type has.
482 * This allows for variables that have don't have values
483 * assigned yet, or variables whose value we simply do not know.
484 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000485
Stefan Reinauer14e22772010-04-27 06:56:47 +0000486#define OP_WRITE 60
Eric Biedermanb138ac82003-04-22 18:44:01 +0000487/* OP_WRITE moves one pseudo register to another.
Eric Biederman90089602004-05-28 14:11:54 +0000488 * MISC(0) holds the destination pseudo register, which must be an OP_DECL.
489 * RHS(0) holds the psuedo to move.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000490 */
491
492#define OP_READ 61
493/* OP_READ reads the value of a variable and makes
494 * it available for the pseudo operation.
495 * Useful for things like def-use chains.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000496 * RHS(0) holds points to the triple to read from.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000497 */
498#define OP_COPY 62
Eric Biederman90089602004-05-28 14:11:54 +0000499/* OP_COPY makes a copy of the pseudo register or constant in RHS(0).
Eric Biederman0babc1c2003-05-09 02:39:00 +0000500 */
Eric Biederman90089602004-05-28 14:11:54 +0000501#define OP_CONVERT 63
502/* OP_CONVERT makes a copy of the pseudo register or constant in RHS(0).
503 * And then the type is converted appropriately.
504 */
505#define OP_PIECE 64
Eric Biederman0babc1c2003-05-09 02:39:00 +0000506/* OP_PIECE returns one piece of a instruction that returns a structure.
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000507 * MISC(0) is the instruction
Eric Biederman0babc1c2003-05-09 02:39:00 +0000508 * u.cval is the LHS piece of the instruction to return.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000509 */
Eric Biederman90089602004-05-28 14:11:54 +0000510#define OP_ASM 65
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000511/* OP_ASM holds a sequence of assembly instructions, the result
512 * of a C asm directive.
513 * RHS(x) holds input value x to the assembly sequence.
514 * LHS(x) holds the output value x from the assembly sequence.
515 * u.blob holds the string of assembly instructions.
516 */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000517
Eric Biederman90089602004-05-28 14:11:54 +0000518#define OP_DEREF 66
Eric Biedermanb138ac82003-04-22 18:44:01 +0000519/* OP_DEREF generates an lvalue from a pointer.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000520 * RHS(0) holds the pointer value.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000521 * OP_DEREF serves as a place holder to indicate all necessary
522 * checks have been done to indicate a value is an lvalue.
523 */
Eric Biederman90089602004-05-28 14:11:54 +0000524#define OP_DOT 67
Eric Biederman0babc1c2003-05-09 02:39:00 +0000525/* OP_DOT references a submember of a structure lvalue.
Eric Biederman90089602004-05-28 14:11:54 +0000526 * MISC(0) holds the lvalue.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000527 * ->u.field holds the name of the field we want.
528 *
Eric Biederman90089602004-05-28 14:11:54 +0000529 * Not seen after structures are flattened.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000530 */
Eric Biederman90089602004-05-28 14:11:54 +0000531#define OP_INDEX 68
532/* OP_INDEX references a submember of a tuple or array lvalue.
533 * MISC(0) holds the lvalue.
534 * ->u.cval holds the index into the lvalue.
535 *
536 * Not seen after structures are flattened.
537 */
538#define OP_VAL 69
Eric Biedermanb138ac82003-04-22 18:44:01 +0000539/* OP_VAL returns the value of a subexpression of the current expression.
540 * Useful for operators that have side effects.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000541 * RHS(0) holds the expression.
542 * MISC(0) holds the subexpression of RHS(0) that is the
Eric Biedermanb138ac82003-04-22 18:44:01 +0000543 * value of the expression.
544 *
545 * Not seen outside of expressions.
546 */
Eric Biederman90089602004-05-28 14:11:54 +0000547
548#define OP_TUPLE 70
549/* OP_TUPLE is an array of triples that are either variable
550 * or values for a structure or an array. It is used as
551 * a place holder when flattening compound types.
552 * The value represented by an OP_TUPLE is held in N registers.
553 * LHS(0..N-1) refer to those registers.
554 * ->use is a list of statements that use the value.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000555 *
Eric Biederman90089602004-05-28 14:11:54 +0000556 * Although OP_TUPLE always has register sized pieces they are not
557 * used until structures are flattened/decomposed into their register
Stefan Reinauer14e22772010-04-27 06:56:47 +0000558 * components.
Eric Biederman90089602004-05-28 14:11:54 +0000559 * ???? registers ????
Eric Biedermanb138ac82003-04-22 18:44:01 +0000560 */
561
Eric Biederman90089602004-05-28 14:11:54 +0000562#define OP_BITREF 71
563/* OP_BITREF describes a bitfield as an lvalue.
564 * RHS(0) holds the register value.
565 * ->type holds the type of the bitfield.
566 * ->u.bitfield.size holds the size of the bitfield.
567 * ->u.bitfield.offset holds the offset of the bitfield in the register
568 */
569
570
571#define OP_FCALL 72
Stefan Reinauer14e22772010-04-27 06:56:47 +0000572/* OP_FCALL performs a procedure call.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000573 * MISC(0) holds a pointer to the OP_LIST of a function
574 * RHS(x) holds argument x of a function
Stefan Reinauer14e22772010-04-27 06:56:47 +0000575 *
Eric Biedermanb138ac82003-04-22 18:44:01 +0000576 * Currently not seen outside of expressions.
577 */
Eric Biederman90089602004-05-28 14:11:54 +0000578#define OP_PROG 73
579/* OP_PROG is an expression that holds a list of statements, or
580 * expressions. The final expression is the value of the expression.
581 * RHS(0) holds the start of the list.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000582 */
583
584/* statements */
585#define OP_LIST 80
Eric Biederman5ade04a2003-10-22 04:03:46 +0000586/* OP_LIST Holds a list of statements that compose a function, and a result value.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000587 * RHS(0) holds the list of statements.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000588 * A list of all functions is maintained.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000589 */
590
Eric Biederman5ade04a2003-10-22 04:03:46 +0000591#define OP_BRANCH 81 /* an unconditional branch */
Eric Biedermanb138ac82003-04-22 18:44:01 +0000592/* For branch instructions
Eric Biederman0babc1c2003-05-09 02:39:00 +0000593 * TARG(0) holds the branch target.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000594 * ->next holds where to branch to if the branch is not taken.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000595 * The branch target can only be a label
Eric Biedermanb138ac82003-04-22 18:44:01 +0000596 */
597
Eric Biederman5ade04a2003-10-22 04:03:46 +0000598#define OP_CBRANCH 82 /* a conditional branch */
599/* For conditional branch instructions
600 * RHS(0) holds the branch condition.
Eric Biederman41203d92004-11-08 09:31:09 +0000601 * TARG(0) holds the branch target.
Eric Biederman5ade04a2003-10-22 04:03:46 +0000602 * ->next holds where to branch to if the branch is not taken.
603 * The branch target can only be a label
604 */
605
606#define OP_CALL 83 /* an uncontional branch that will return */
607/* For call instructions
608 * MISC(0) holds the OP_RET that returns from the branch
609 * TARG(0) holds the branch target.
610 * ->next holds where to branch to if the branch is not taken.
611 * The branch target can only be a label
612 */
613
614#define OP_RET 84 /* an uncontinonal branch through a variable back to an OP_CALL */
615/* For call instructions
616 * RHS(0) holds the variable with the return address
617 * The branch target can only be a label
618 */
619
620#define OP_LABEL 86
Eric Biedermanb138ac82003-04-22 18:44:01 +0000621/* OP_LABEL is a triple that establishes an target for branches.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000622 * ->use is the list of all branches that use this label.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000623 */
624
Stefan Reinauer14e22772010-04-27 06:56:47 +0000625#define OP_ADECL 87
Eric Biederman5ade04a2003-10-22 04:03:46 +0000626/* OP_ADECL is a triple that establishes an lvalue for assignments.
Eric Biederman90089602004-05-28 14:11:54 +0000627 * A variable takes N registers to contain.
628 * LHS(0..N-1) refer to an OP_PIECE triple that represents
629 * the Xth register that the variable is stored in.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000630 * ->use is a list of statements that use the variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000631 *
Eric Biederman90089602004-05-28 14:11:54 +0000632 * Although OP_ADECL always has register sized pieces they are not
633 * used until structures are flattened/decomposed into their register
Stefan Reinauer14e22772010-04-27 06:56:47 +0000634 * components.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000635 */
636
Eric Biederman5ade04a2003-10-22 04:03:46 +0000637#define OP_SDECL 88
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000638/* OP_SDECL is a triple that establishes a variable of static
Eric Biedermanb138ac82003-04-22 18:44:01 +0000639 * storage duration.
Eric Biederman0babc1c2003-05-09 02:39:00 +0000640 * ->use is a list of statements that use the variable.
641 * MISC(0) holds the initializer expression.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000642 */
643
644
Eric Biederman5ade04a2003-10-22 04:03:46 +0000645#define OP_PHI 89
Stefan Reinauer14e22772010-04-27 06:56:47 +0000646/* OP_PHI is a triple used in SSA form code.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000647 * It is used when multiple code paths merge and a variable needs
648 * a single assignment from any of those code paths.
649 * The operation is a cross between OP_DECL and OP_WRITE, which
Eric Biederman5ade04a2003-10-22 04:03:46 +0000650 * is what OP_PHI is generated from.
Stefan Reinauer14e22772010-04-27 06:56:47 +0000651 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000652 * RHS(x) points to the value from code path x
653 * The number of RHS entries is the number of control paths into the block
Eric Biedermanb138ac82003-04-22 18:44:01 +0000654 * in which OP_PHI resides. The elements of the array point to point
655 * to the variables OP_PHI is derived from.
656 *
Eric Biederman0babc1c2003-05-09 02:39:00 +0000657 * MISC(0) holds a pointer to the orginal OP_DECL node.
Eric Biedermanb138ac82003-04-22 18:44:01 +0000658 */
659
Eric Biederman90089602004-05-28 14:11:54 +0000660#if 0
661/* continuation helpers
662 */
663#define OP_CPS_BRANCH 90 /* an unconditional branch */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000664/* OP_CPS_BRANCH calls a continuation
Eric Biederman90089602004-05-28 14:11:54 +0000665 * RHS(x) holds argument x of the function
666 * TARG(0) holds OP_CPS_START target
667 */
668#define OP_CPS_CBRANCH 91 /* a conditional branch */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000669/* OP_CPS_CBRANCH conditionally calls one of two continuations
Eric Biederman90089602004-05-28 14:11:54 +0000670 * RHS(0) holds the branch condition
671 * RHS(x + 1) holds argument x of the function
672 * TARG(0) holds the OP_CPS_START to jump to when true
673 * ->next holds the OP_CPS_START to jump to when false
674 */
675#define OP_CPS_CALL 92 /* an uncontional branch that will return */
676/* For OP_CPS_CALL instructions
677 * RHS(x) holds argument x of the function
678 * MISC(0) holds the OP_CPS_RET that returns from the branch
679 * TARG(0) holds the branch target.
680 * ->next holds where the OP_CPS_RET will return to.
681 */
682#define OP_CPS_RET 93
Stefan Reinauer14e22772010-04-27 06:56:47 +0000683/* OP_CPS_RET conditionally calls one of two continuations
Eric Biederman90089602004-05-28 14:11:54 +0000684 * RHS(0) holds the variable with the return function address
685 * RHS(x + 1) holds argument x of the function
686 * The branch target may be any OP_CPS_START
687 */
688#define OP_CPS_END 94
689/* OP_CPS_END is the triple at the end of the program.
690 * For most practical purposes it is a branch.
691 */
692#define OP_CPS_START 95
693/* OP_CPS_START is a triple at the start of a continuation
694 * The arguments variables takes N registers to contain.
695 * LHS(0..N-1) refer to an OP_PIECE triple that represents
696 * the Xth register that the arguments are stored in.
697 */
698#endif
699
Eric Biedermanb138ac82003-04-22 18:44:01 +0000700/* Architecture specific instructions */
701#define OP_CMP 100
702#define OP_TEST 101
703#define OP_SET_EQ 102
704#define OP_SET_NOTEQ 103
705#define OP_SET_SLESS 104
706#define OP_SET_ULESS 105
707#define OP_SET_SMORE 106
708#define OP_SET_UMORE 107
709#define OP_SET_SLESSEQ 108
710#define OP_SET_ULESSEQ 109
711#define OP_SET_SMOREEQ 110
712#define OP_SET_UMOREEQ 111
713
714#define OP_JMP 112
715#define OP_JMP_EQ 113
716#define OP_JMP_NOTEQ 114
717#define OP_JMP_SLESS 115
718#define OP_JMP_ULESS 116
719#define OP_JMP_SMORE 117
720#define OP_JMP_UMORE 118
721#define OP_JMP_SLESSEQ 119
722#define OP_JMP_ULESSEQ 120
723#define OP_JMP_SMOREEQ 121
724#define OP_JMP_UMOREEQ 122
725
726/* Builtin operators that it is just simpler to use the compiler for */
727#define OP_INB 130
728#define OP_INW 131
729#define OP_INL 132
730#define OP_OUTB 133
731#define OP_OUTW 134
732#define OP_OUTL 135
733#define OP_BSF 136
734#define OP_BSR 137
Eric Biedermanb138ac82003-04-22 18:44:01 +0000735#define OP_RDMSR 138
736#define OP_WRMSR 139
Eric Biedermanb138ac82003-04-22 18:44:01 +0000737#define OP_HLT 140
738
Eric Biederman0babc1c2003-05-09 02:39:00 +0000739struct op_info {
740 const char *name;
741 unsigned flags;
Eric Biederman90089602004-05-28 14:11:54 +0000742#define PURE 0x001 /* Triple has no side effects */
743#define IMPURE 0x002 /* Triple has side effects */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000744#define PURE_BITS(FLAGS) ((FLAGS) & 0x3)
Eric Biederman90089602004-05-28 14:11:54 +0000745#define DEF 0x004 /* Triple is a variable definition */
746#define BLOCK 0x008 /* Triple stores the current block */
747#define STRUCTURAL 0x010 /* Triple does not generate a machine instruction */
748#define BRANCH_BITS(FLAGS) ((FLAGS) & 0xe0 )
749#define UBRANCH 0x020 /* Triple is an unconditional branch instruction */
750#define CBRANCH 0x040 /* Triple is a conditional branch instruction */
751#define RETBRANCH 0x060 /* Triple is a return instruction */
752#define CALLBRANCH 0x080 /* Triple is a call instruction */
753#define ENDBRANCH 0x0a0 /* Triple is an end instruction */
754#define PART 0x100 /* Triple is really part of another triple */
755#define BITFIELD 0x200 /* Triple manipulates a bitfield */
756 signed char lhs, rhs, misc, targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000757};
758
Eric Biederman0babc1c2003-05-09 02:39:00 +0000759#define OP(LHS, RHS, MISC, TARG, FLAGS, NAME) { \
760 .name = (NAME), \
761 .flags = (FLAGS), \
762 .lhs = (LHS), \
763 .rhs = (RHS), \
764 .misc = (MISC), \
765 .targ = (TARG), \
766 }
767static const struct op_info table_ops[] = {
Eric Biederman530b5192003-07-01 10:05:30 +0000768[OP_SDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "sdivt"),
769[OP_UDIVT ] = OP( 2, 2, 0, 0, PURE | BLOCK , "udivt"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000770[OP_SMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smul"),
771[OP_UMUL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umul"),
772[OP_SDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sdiv"),
773[OP_UDIV ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "udiv"),
774[OP_SMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smod"),
775[OP_UMOD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umod"),
776[OP_ADD ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "add"),
777[OP_SUB ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sub"),
778[OP_SL ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sl"),
779[OP_USR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "usr"),
780[OP_SSR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ssr"),
781[OP_AND ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "and"),
782[OP_XOR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "xor"),
783[OP_OR ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "or"),
784[OP_POS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "pos"),
785[OP_NEG ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "neg"),
786[OP_INVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "invert"),
Eric Biedermanb138ac82003-04-22 18:44:01 +0000787
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000788[OP_EQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "eq"),
789[OP_NOTEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "noteq"),
790[OP_SLESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "sless"),
791[OP_ULESS ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "uless"),
792[OP_SMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smore"),
793[OP_UMORE ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umore"),
794[OP_SLESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "slesseq"),
795[OP_ULESSEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "ulesseq"),
796[OP_SMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "smoreeq"),
797[OP_UMOREEQ ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK , "umoreeq"),
798[OP_LFALSE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "lfalse"),
799[OP_LTRUE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK , "ltrue"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000800
Eric Biederman90089602004-05-28 14:11:54 +0000801[OP_LOAD ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "load"),
802[OP_STORE ] = OP( 0, 2, 0, 0, PURE | BLOCK , "store"),
803
804[OP_UEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "uextract"),
805[OP_SEXTRACT ] = OP( 0, 1, 0, 0, PURE | DEF | BITFIELD, "sextract"),
806[OP_DEPOSIT ] = OP( 0, 2, 0, 0, PURE | DEF | BITFIELD, "deposit"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000807
Eric Biederman83b991a2003-10-11 06:20:25 +0000808[OP_NOOP ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "noop"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000809
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000810[OP_INTCONST ] = OP( 0, 0, 0, 0, PURE | DEF, "intconst"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000811[OP_BLOBCONST ] = OP( 0, 0, 0, 0, PURE , "blobconst"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000812[OP_ADDRCONST ] = OP( 0, 0, 1, 0, PURE | DEF, "addrconst"),
Eric Biederman90089602004-05-28 14:11:54 +0000813[OP_UNKNOWNVAL ] = OP( 0, 0, 0, 0, PURE | DEF, "unknown"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000814
Stefan Reinauer50542a82007-10-24 11:14:14 +0000815#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +0000816#warning "FIXME is it correct for OP_WRITE to be a def? I currently use it as one..."
Stefan Reinauer50542a82007-10-24 11:14:14 +0000817#endif
Eric Biederman90089602004-05-28 14:11:54 +0000818[OP_WRITE ] = OP( 0, 1, 1, 0, PURE | DEF | BLOCK, "write"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000819[OP_READ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "read"),
820[OP_COPY ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "copy"),
Eric Biederman90089602004-05-28 14:11:54 +0000821[OP_CONVERT ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "convert"),
822[OP_PIECE ] = OP( 0, 0, 1, 0, PURE | DEF | STRUCTURAL | PART, "piece"),
823[OP_ASM ] = OP(-1, -1, 0, 0, PURE, "asm"),
Stefan Reinauer14e22772010-04-27 06:56:47 +0000824[OP_DEREF ] = OP( 0, 1, 0, 0, 0 | DEF | BLOCK, "deref"),
Eric Biederman90089602004-05-28 14:11:54 +0000825[OP_DOT ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "dot"),
826[OP_INDEX ] = OP( 0, 0, 1, 0, PURE | DEF | PART, "index"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000827
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000828[OP_VAL ] = OP( 0, 1, 1, 0, 0 | DEF | BLOCK, "val"),
Eric Biederman90089602004-05-28 14:11:54 +0000829[OP_TUPLE ] = OP(-1, 0, 0, 0, 0 | PURE | BLOCK | STRUCTURAL, "tuple"),
830[OP_BITREF ] = OP( 0, 1, 0, 0, 0 | DEF | PURE | STRUCTURAL | BITFIELD, "bitref"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000831/* Call is special most it can stand in for anything so it depends on context */
Eric Biederman90089602004-05-28 14:11:54 +0000832[OP_FCALL ] = OP( 0, -1, 1, 0, 0 | BLOCK | CALLBRANCH, "fcall"),
833[OP_PROG ] = OP( 0, 1, 0, 0, 0 | IMPURE | BLOCK | STRUCTURAL, "prog"),
834/* The sizes of OP_FCALL depends upon context */
Eric Biederman0babc1c2003-05-09 02:39:00 +0000835
Eric Biederman83b991a2003-10-11 06:20:25 +0000836[OP_LIST ] = OP( 0, 1, 1, 0, 0 | DEF | STRUCTURAL, "list"),
Eric Biederman90089602004-05-28 14:11:54 +0000837[OP_BRANCH ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "branch"),
838[OP_CBRANCH ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "cbranch"),
839[OP_CALL ] = OP( 0, 0, 1, 1, PURE | BLOCK | CALLBRANCH, "call"),
840[OP_RET ] = OP( 0, 1, 0, 0, PURE | BLOCK | RETBRANCH, "ret"),
Eric Biederman83b991a2003-10-11 06:20:25 +0000841[OP_LABEL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "label"),
842[OP_ADECL ] = OP( 0, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "adecl"),
843[OP_SDECL ] = OP( 0, 0, 1, 0, PURE | BLOCK | STRUCTURAL, "sdecl"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000844/* The number of RHS elements of OP_PHI depend upon context */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000845[OP_PHI ] = OP( 0, -1, 1, 0, PURE | DEF | BLOCK, "phi"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000846
Eric Biederman90089602004-05-28 14:11:54 +0000847#if 0
848[OP_CPS_BRANCH ] = OP( 0, -1, 0, 1, PURE | BLOCK | UBRANCH, "cps_branch"),
849[OP_CPS_CBRANCH] = OP( 0, -1, 0, 1, PURE | BLOCK | CBRANCH, "cps_cbranch"),
850[OP_CPS_CALL ] = OP( 0, -1, 1, 1, PURE | BLOCK | CALLBRANCH, "cps_call"),
851[OP_CPS_RET ] = OP( 0, -1, 0, 0, PURE | BLOCK | RETBRANCH, "cps_ret"),
852[OP_CPS_END ] = OP( 0, -1, 0, 0, IMPURE | BLOCK | ENDBRANCH, "cps_end"),
853[OP_CPS_START ] = OP( -1, 0, 0, 0, PURE | BLOCK | STRUCTURAL, "cps_start"),
854#endif
855
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000856[OP_CMP ] = OP( 0, 2, 0, 0, PURE | DEF | BLOCK, "cmp"),
857[OP_TEST ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "test"),
858[OP_SET_EQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_eq"),
859[OP_SET_NOTEQ ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_noteq"),
860[OP_SET_SLESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_sless"),
861[OP_SET_ULESS ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_uless"),
862[OP_SET_SMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smore"),
863[OP_SET_UMORE ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umore"),
864[OP_SET_SLESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_slesseq"),
865[OP_SET_ULESSEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_ulesseq"),
866[OP_SET_SMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_smoreq"),
867[OP_SET_UMOREEQ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "set_umoreq"),
Eric Biederman90089602004-05-28 14:11:54 +0000868[OP_JMP ] = OP( 0, 0, 0, 1, PURE | BLOCK | UBRANCH, "jmp"),
869[OP_JMP_EQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_eq"),
870[OP_JMP_NOTEQ ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_noteq"),
871[OP_JMP_SLESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_sless"),
872[OP_JMP_ULESS ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_uless"),
873[OP_JMP_SMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smore"),
874[OP_JMP_UMORE ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umore"),
875[OP_JMP_SLESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_slesseq"),
876[OP_JMP_ULESSEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_ulesseq"),
877[OP_JMP_SMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_smoreq"),
878[OP_JMP_UMOREEQ] = OP( 0, 1, 0, 1, PURE | BLOCK | CBRANCH, "jmp_umoreq"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000879
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000880[OP_INB ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inb"),
881[OP_INW ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inw"),
882[OP_INL ] = OP( 0, 1, 0, 0, IMPURE | DEF | BLOCK, "__inl"),
883[OP_OUTB ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outb"),
884[OP_OUTW ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outw"),
885[OP_OUTL ] = OP( 0, 2, 0, 0, IMPURE| BLOCK, "__outl"),
886[OP_BSF ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsf"),
887[OP_BSR ] = OP( 0, 1, 0, 0, PURE | DEF | BLOCK, "__bsr"),
888[OP_RDMSR ] = OP( 2, 1, 0, 0, IMPURE | BLOCK, "__rdmsr"),
889[OP_WRMSR ] = OP( 0, 3, 0, 0, IMPURE | BLOCK, "__wrmsr"),
890[OP_HLT ] = OP( 0, 0, 0, 0, IMPURE | BLOCK, "__hlt"),
Eric Biederman0babc1c2003-05-09 02:39:00 +0000891};
892#undef OP
893#define OP_MAX (sizeof(table_ops)/sizeof(table_ops[0]))
Eric Biedermanb138ac82003-04-22 18:44:01 +0000894
Stefan Reinauer14e22772010-04-27 06:56:47 +0000895static const char *tops(int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +0000896{
897 static const char unknown[] = "unknown op";
898 if (index < 0) {
899 return unknown;
900 }
Patrick Georgi0dde01c2013-11-11 15:57:09 +0100901 if (index >= OP_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +0000902 return unknown;
903 }
Eric Biederman0babc1c2003-05-09 02:39:00 +0000904 return table_ops[index].name;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000905}
906
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000907struct asm_info;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000908struct triple;
909struct block;
910struct triple_set {
911 struct triple_set *next;
912 struct triple *member;
913};
914
Eric Biederman90089602004-05-28 14:11:54 +0000915#define MAX_LHS 63
916#define MAX_RHS 127
Eric Biederman678d8162003-07-03 03:59:38 +0000917#define MAX_MISC 3
Eric Biederman90089602004-05-28 14:11:54 +0000918#define MAX_TARG 1
Eric Biederman0babc1c2003-05-09 02:39:00 +0000919
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000920struct occurance {
921 int count;
922 const char *filename;
923 const char *function;
924 int line;
925 int col;
926 struct occurance *parent;
927};
Eric Biederman90089602004-05-28 14:11:54 +0000928struct bitfield {
929 ulong_t size : 8;
930 ulong_t offset : 24;
931};
Eric Biedermanb138ac82003-04-22 18:44:01 +0000932struct triple {
933 struct triple *next, *prev;
934 struct triple_set *use;
935 struct type *type;
Eric Biederman90089602004-05-28 14:11:54 +0000936 unsigned int op : 8;
937 unsigned int template_id : 7;
938 unsigned int lhs : 6;
939 unsigned int rhs : 7;
940 unsigned int misc : 2;
941 unsigned int targ : 1;
942#define TRIPLE_SIZE(TRIPLE) \
943 ((TRIPLE)->lhs + (TRIPLE)->rhs + (TRIPLE)->misc + (TRIPLE)->targ)
944#define TRIPLE_LHS_OFF(PTR) (0)
945#define TRIPLE_RHS_OFF(PTR) (TRIPLE_LHS_OFF(PTR) + (PTR)->lhs)
946#define TRIPLE_MISC_OFF(PTR) (TRIPLE_RHS_OFF(PTR) + (PTR)->rhs)
947#define TRIPLE_TARG_OFF(PTR) (TRIPLE_MISC_OFF(PTR) + (PTR)->misc)
948#define LHS(PTR,INDEX) ((PTR)->param[TRIPLE_LHS_OFF(PTR) + (INDEX)])
949#define RHS(PTR,INDEX) ((PTR)->param[TRIPLE_RHS_OFF(PTR) + (INDEX)])
950#define TARG(PTR,INDEX) ((PTR)->param[TRIPLE_TARG_OFF(PTR) + (INDEX)])
951#define MISC(PTR,INDEX) ((PTR)->param[TRIPLE_MISC_OFF(PTR) + (INDEX)])
Eric Biedermanb138ac82003-04-22 18:44:01 +0000952 unsigned id; /* A scratch value and finally the register */
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000953#define TRIPLE_FLAG_FLATTENED (1 << 31)
954#define TRIPLE_FLAG_PRE_SPLIT (1 << 30)
955#define TRIPLE_FLAG_POST_SPLIT (1 << 29)
Eric Biederman83b991a2003-10-11 06:20:25 +0000956#define TRIPLE_FLAG_VOLATILE (1 << 28)
Eric Biederman90089602004-05-28 14:11:54 +0000957#define TRIPLE_FLAG_INLINE (1 << 27) /* ???? */
958#define TRIPLE_FLAG_LOCAL (1 << 26)
959
960#define TRIPLE_FLAG_COPY TRIPLE_FLAG_VOLATILE
Eric Biedermanf7a0ba82003-06-19 15:14:52 +0000961 struct occurance *occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000962 union {
963 ulong_t cval;
Eric Biederman90089602004-05-28 14:11:54 +0000964 struct bitfield bitfield;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000965 struct block *block;
966 void *blob;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000967 struct hash_entry *field;
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000968 struct asm_info *ainfo;
Eric Biederman90089602004-05-28 14:11:54 +0000969 struct triple *func;
970 struct symbol *symbol;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000971 } u;
Eric Biederman0babc1c2003-05-09 02:39:00 +0000972 struct triple *param[2];
Eric Biedermanb138ac82003-04-22 18:44:01 +0000973};
974
Eric Biederman6aa31cc2003-06-10 21:22:07 +0000975struct reg_info {
976 unsigned reg;
977 unsigned regcm;
978};
979struct ins_template {
980 struct reg_info lhs[MAX_LHS + 1], rhs[MAX_RHS + 1];
981};
982
983struct asm_info {
984 struct ins_template tmpl;
985 char *str;
986};
987
Eric Biedermanb138ac82003-04-22 18:44:01 +0000988struct block_set {
989 struct block_set *next;
990 struct block *member;
991};
992struct block {
993 struct block *work_next;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000994 struct triple *first, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +0000995 int edge_count;
996 struct block_set *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000997 int users;
998 struct block_set *use;
999 struct block_set *idominates;
1000 struct block_set *domfrontier;
1001 struct block *idom;
1002 struct block_set *ipdominates;
1003 struct block_set *ipdomfrontier;
1004 struct block *ipdom;
1005 int vertex;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001006
Eric Biedermanb138ac82003-04-22 18:44:01 +00001007};
1008
1009struct symbol {
1010 struct symbol *next;
1011 struct hash_entry *ident;
1012 struct triple *def;
1013 struct type *type;
1014 int scope_depth;
1015};
1016
Eric Biederman90089602004-05-28 14:11:54 +00001017struct macro_arg {
1018 struct macro_arg *next;
1019 struct hash_entry *ident;
1020};
Eric Biedermanb138ac82003-04-22 18:44:01 +00001021struct macro {
1022 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00001023 const char *buf;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001024 int buf_len;
Eric Biederman90089602004-05-28 14:11:54 +00001025 struct macro_arg *args;
1026 int argc;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001027};
1028
1029struct hash_entry {
1030 struct hash_entry *next;
1031 const char *name;
1032 int name_len;
1033 int tok;
1034 struct macro *sym_define;
1035 struct symbol *sym_label;
Eric Biederman83b991a2003-10-11 06:20:25 +00001036 struct symbol *sym_tag;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001037 struct symbol *sym_ident;
1038};
1039
1040#define HASH_TABLE_SIZE 2048
1041
Eric Biederman5ade04a2003-10-22 04:03:46 +00001042struct compiler_state {
Eric Biederman05f26fc2003-06-11 21:55:00 +00001043 const char *label_prefix;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001044 const char *ofilename;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001045 unsigned long flags;
1046 unsigned long debug;
1047 unsigned long max_allocation_passes;
Eric Biederman90089602004-05-28 14:11:54 +00001048
1049 size_t include_path_count;
1050 const char **include_paths;
1051
1052 size_t define_count;
1053 const char **defines;
1054
1055 size_t undef_count;
1056 const char **undefs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001057};
1058struct arch_state {
1059 unsigned long features;
1060};
Eric Biederman90089602004-05-28 14:11:54 +00001061struct basic_blocks {
1062 struct triple *func;
1063 struct triple *first;
1064 struct block *first_block, *last_block;
1065 int last_vertex;
1066};
Eric Biedermancb364952004-11-15 10:46:44 +00001067#define MAX_PP_IF_DEPTH 63
Eric Biederman5ade04a2003-10-22 04:03:46 +00001068struct compile_state {
1069 struct compiler_state *compiler;
1070 struct arch_state *arch;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001071 FILE *output;
Eric Biederman90089602004-05-28 14:11:54 +00001072 FILE *errout;
1073 FILE *dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001074 struct file_state *file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001075 struct occurance *last_occurance;
1076 const char *function;
Eric Biederman41203d92004-11-08 09:31:09 +00001077 int token_base;
1078 struct token token[6];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001079 struct hash_entry *hash_table[HASH_TABLE_SIZE];
Eric Biederman83b991a2003-10-11 06:20:25 +00001080 struct hash_entry *i_switch;
1081 struct hash_entry *i_case;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001082 struct hash_entry *i_continue;
1083 struct hash_entry *i_break;
Eric Biederman83b991a2003-10-11 06:20:25 +00001084 struct hash_entry *i_default;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001085 struct hash_entry *i_return;
Stefan Reinauerc89c4d42010-02-28 18:37:38 +00001086 struct hash_entry *i_noreturn;
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -07001087 struct hash_entry *i_unused;
Stefan Reinauerec664bc2013-05-09 14:06:04 -07001088 struct hash_entry *i_packed;
Eric Biederman90089602004-05-28 14:11:54 +00001089 /* Additional hash entries for predefined macros */
1090 struct hash_entry *i_defined;
1091 struct hash_entry *i___VA_ARGS__;
1092 struct hash_entry *i___FILE__;
1093 struct hash_entry *i___LINE__;
1094 /* Additional hash entries for predefined identifiers */
1095 struct hash_entry *i___func__;
1096 /* Additional hash entries for attributes */
1097 struct hash_entry *i_noinline;
1098 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001099 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001100 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001101 int if_depth;
1102 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001103 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001104 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001105 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001106 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001107 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001108 struct basic_blocks bb;
1109 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001110};
1111
Eric Biederman0babc1c2003-05-09 02:39:00 +00001112/* visibility global/local */
1113/* static/auto duration */
1114/* typedef, register, inline */
1115#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001116#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001117/* Visibility */
1118#define STOR_GLOBAL 0x0001
1119/* Duration */
1120#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001121/* Definition locality */
1122#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001123/* Storage specifiers */
1124#define STOR_AUTO 0x0000
1125#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001126#define STOR_LOCAL 0x0003
1127#define STOR_EXTERN 0x0007
1128#define STOR_INLINE 0x0008
1129#define STOR_REGISTER 0x0010
1130#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001131
Eric Biederman5ade04a2003-10-22 04:03:46 +00001132#define QUAL_SHIFT 5
1133#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001134#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001135#define QUAL_CONST 0x0020
1136#define QUAL_VOLATILE 0x0040
1137#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001138
1139#define TYPE_SHIFT 8
1140#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001141#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1142#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001143#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1144#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001145#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1146#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001147#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1148#define TYPE_DEFAULT 0x0000
1149#define TYPE_VOID 0x0100
1150#define TYPE_CHAR 0x0200
1151#define TYPE_UCHAR 0x0300
1152#define TYPE_SHORT 0x0400
1153#define TYPE_USHORT 0x0500
1154#define TYPE_INT 0x0600
1155#define TYPE_UINT 0x0700
1156#define TYPE_LONG 0x0800
1157#define TYPE_ULONG 0x0900
1158#define TYPE_LLONG 0x0a00 /* long long */
1159#define TYPE_ULLONG 0x0b00
1160#define TYPE_FLOAT 0x0c00
1161#define TYPE_DOUBLE 0x0d00
1162#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001163
1164/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1165#define TYPE_ENUM 0x1600
1166#define TYPE_LIST 0x1700
1167/* TYPE_LIST is a basic building block when defining enumerations
1168 * type->field_ident holds the name of this enumeration entry.
1169 * type->right holds the entry in the list.
1170 */
1171
Eric Biederman0babc1c2003-05-09 02:39:00 +00001172#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001173/* For TYPE_STRUCT
1174 * type->left holds the link list of TYPE_PRODUCT entries that
1175 * make up the structure.
1176 * type->elements hold the length of the linked list
1177 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001178#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001179/* For TYPE_UNION
1180 * type->left holds the link list of TYPE_OVERLAP entries that
1181 * make up the union.
1182 * type->elements hold the length of the linked list
1183 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001184#define TYPE_POINTER 0x1200
Eric Biederman0babc1c2003-05-09 02:39:00 +00001185/* For TYPE_POINTER:
1186 * type->left holds the type pointed to.
1187 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001188#define TYPE_FUNCTION 0x1300
Eric Biederman0babc1c2003-05-09 02:39:00 +00001189/* For TYPE_FUNCTION:
1190 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001191 * type->right holds the type of the arguments
1192 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001193 */
1194#define TYPE_PRODUCT 0x1400
1195/* TYPE_PRODUCT is a basic building block when defining structures
1196 * type->left holds the type that appears first in memory.
1197 * type->right holds the type that appears next in memory.
1198 */
1199#define TYPE_OVERLAP 0x1500
1200/* TYPE_OVERLAP is a basic building block when defining unions
1201 * type->left and type->right holds to types that overlap
1202 * each other in memory.
1203 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001204#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001205/* TYPE_ARRAY is a basic building block when definitng arrays.
1206 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001207 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001208 */
Eric Biederman90089602004-05-28 14:11:54 +00001209#define TYPE_TUPLE 0x1900
Stefan Reinauer14e22772010-04-27 06:56:47 +00001210/* TYPE_TUPLE is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001211 * positionally reference type conglomerations. (i.e. closures)
1212 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1213 * except it has no field names.
1214 * type->left holds the liked list of TYPE_PRODUCT entries that
1215 * make up the closure type.
1216 * type->elements hold the number of elements in the closure.
1217 */
1218#define TYPE_JOIN 0x1a00
Stefan Reinauer14e22772010-04-27 06:56:47 +00001219/* TYPE_JOIN 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_OVERLAP, like TYPE_UNION
1222 * except it has no field names.
1223 * type->left holds the liked list of TYPE_OVERLAP entries that
1224 * make up the closure type.
1225 * type->elements hold the number of elements in the closure.
1226 */
1227#define TYPE_BITFIELD 0x1b00
1228/* TYPE_BITFIED is the type of a bitfield.
1229 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1230 * type->elements holds the number of bits in the bitfield.
1231 */
1232#define TYPE_UNKNOWN 0x1c00
1233/* TYPE_UNKNOWN is the type of an unknown value.
1234 * Used on unknown consts and other places where I don't know the type.
1235 */
1236
1237#define ATTRIB_SHIFT 16
1238#define ATTRIB_MASK 0xffff0000
1239#define ATTRIB_NOINLINE 0x00010000
1240#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001241
Eric Biederman83b991a2003-10-11 06:20:25 +00001242#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001243
1244struct type {
1245 unsigned int type;
1246 struct type *left, *right;
1247 ulong_t elements;
1248 struct hash_entry *field_ident;
1249 struct hash_entry *type_ident;
1250};
1251
Eric Biederman530b5192003-07-01 10:05:30 +00001252#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001253#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001254#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001255#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001256#define MAX_REGISTERS 75
1257#define REGISTER_BITS 7
1258#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001259#define REG_ERROR 0
1260#define REG_UNSET 1
1261#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001262#define REG_VIRT0 (MAX_REGISTERS + 0)
1263#define REG_VIRT1 (MAX_REGISTERS + 1)
1264#define REG_VIRT2 (MAX_REGISTERS + 2)
1265#define REG_VIRT3 (MAX_REGISTERS + 3)
1266#define REG_VIRT4 (MAX_REGISTERS + 4)
1267#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001268#define REG_VIRT6 (MAX_REGISTERS + 6)
1269#define REG_VIRT7 (MAX_REGISTERS + 7)
1270#define REG_VIRT8 (MAX_REGISTERS + 8)
1271#define REG_VIRT9 (MAX_REGISTERS + 9)
1272
1273#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1274#error "MAX_VIRT_REGISTERS to small"
1275#endif
Eric Biederman90089602004-05-28 14:11:54 +00001276#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001277#error "Too many id bits used"
1278#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001279
1280/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001281#define REG_SHIFT 0
1282#define REGC_SHIFT REGISTER_BITS
1283#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001284#define REG_MASK (MAX_VIRT_REGISTERS -1)
1285#define ID_REG(ID) ((ID) & REG_MASK)
1286#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001287#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1288#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1289#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1290 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001291
Eric Biederman90089602004-05-28 14:11:54 +00001292#define ARCH_INPUT_REGS 4
1293#define ARCH_OUTPUT_REGS 4
1294
1295static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1296static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001297static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001298static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001299static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001300static void arch_reg_equivs(
1301 struct compile_state *state, unsigned *equiv, int reg);
1302static int arch_select_free_register(
1303 struct compile_state *state, char *used, int classes);
1304static unsigned arch_regc_size(struct compile_state *state, int class);
1305static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1306static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1307static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001308static struct reg_info arch_reg_constraint(
1309 struct compile_state *state, struct type *type, const char *constraint);
1310static struct reg_info arch_reg_clobber(
1311 struct compile_state *state, const char *clobber);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001312static struct reg_info arch_reg_lhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001313 struct triple *ins, int index);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001314static struct reg_info arch_reg_rhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001315 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001316static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001317static struct triple *transform_to_arch_instruction(
1318 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001319static struct triple *flatten(
1320 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001321static void print_dominators(struct compile_state *state,
1322 FILE *fp, struct basic_blocks *bb);
1323static void print_dominance_frontiers(struct compile_state *state,
1324 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001325
1326
Eric Biedermanb138ac82003-04-22 18:44:01 +00001327
Eric Biederman5ade04a2003-10-22 04:03:46 +00001328#define DEBUG_ABORT_ON_ERROR 0x00000001
1329#define DEBUG_BASIC_BLOCKS 0x00000002
1330#define DEBUG_FDOMINATORS 0x00000004
1331#define DEBUG_RDOMINATORS 0x00000008
1332#define DEBUG_TRIPLES 0x00000010
1333#define DEBUG_INTERFERENCE 0x00000020
1334#define DEBUG_SCC_TRANSFORM 0x00000040
1335#define DEBUG_SCC_TRANSFORM2 0x00000080
1336#define DEBUG_REBUILD_SSA_FORM 0x00000100
1337#define DEBUG_INLINE 0x00000200
1338#define DEBUG_RANGE_CONFLICTS 0x00000400
1339#define DEBUG_RANGE_CONFLICTS2 0x00000800
1340#define DEBUG_COLOR_GRAPH 0x00001000
1341#define DEBUG_COLOR_GRAPH2 0x00002000
1342#define DEBUG_COALESCING 0x00004000
1343#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001344#define DEBUG_VERIFICATION 0x00010000
1345#define DEBUG_CALLS 0x00020000
1346#define DEBUG_CALLS2 0x00040000
1347#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001348
1349#define DEBUG_DEFAULT ( \
1350 DEBUG_ABORT_ON_ERROR | \
1351 DEBUG_BASIC_BLOCKS | \
1352 DEBUG_FDOMINATORS | \
1353 DEBUG_RDOMINATORS | \
1354 DEBUG_TRIPLES | \
1355 0 )
1356
Eric Biederman90089602004-05-28 14:11:54 +00001357#define DEBUG_ALL ( \
1358 DEBUG_ABORT_ON_ERROR | \
1359 DEBUG_BASIC_BLOCKS | \
1360 DEBUG_FDOMINATORS | \
1361 DEBUG_RDOMINATORS | \
1362 DEBUG_TRIPLES | \
1363 DEBUG_INTERFERENCE | \
1364 DEBUG_SCC_TRANSFORM | \
1365 DEBUG_SCC_TRANSFORM2 | \
1366 DEBUG_REBUILD_SSA_FORM | \
1367 DEBUG_INLINE | \
1368 DEBUG_RANGE_CONFLICTS | \
1369 DEBUG_RANGE_CONFLICTS2 | \
1370 DEBUG_COLOR_GRAPH | \
1371 DEBUG_COLOR_GRAPH2 | \
1372 DEBUG_COALESCING | \
1373 DEBUG_COALESCING2 | \
1374 DEBUG_VERIFICATION | \
1375 DEBUG_CALLS | \
1376 DEBUG_CALLS2 | \
1377 DEBUG_TOKENS | \
1378 0 )
1379
1380#define COMPILER_INLINE_MASK 0x00000007
1381#define COMPILER_INLINE_ALWAYS 0x00000000
1382#define COMPILER_INLINE_NEVER 0x00000001
1383#define COMPILER_INLINE_DEFAULTON 0x00000002
1384#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1385#define COMPILER_INLINE_NOPENALTY 0x00000004
1386#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1387#define COMPILER_SIMPLIFY 0x00000010
1388#define COMPILER_SCC_TRANSFORM 0x00000020
1389#define COMPILER_SIMPLIFY_OP 0x00000040
1390#define COMPILER_SIMPLIFY_PHI 0x00000080
1391#define COMPILER_SIMPLIFY_LABEL 0x00000100
1392#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1393#define COMPILER_SIMPLIFY_COPY 0x00000400
1394#define COMPILER_SIMPLIFY_ARITH 0x00000800
1395#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1396#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1397#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1398#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1399
Eric Biedermancb364952004-11-15 10:46:44 +00001400#define COMPILER_TRIGRAPHS 0x40000000
1401#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001402
1403#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001404 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001405 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001406 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001407 COMPILER_SIMPLIFY_OP | \
1408 COMPILER_SIMPLIFY_PHI | \
1409 COMPILER_SIMPLIFY_LABEL | \
1410 COMPILER_SIMPLIFY_BRANCH | \
1411 COMPILER_SIMPLIFY_COPY | \
1412 COMPILER_SIMPLIFY_ARITH | \
1413 COMPILER_SIMPLIFY_SHIFT | \
1414 COMPILER_SIMPLIFY_BITWISE | \
1415 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001416 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001417 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001418
Eric Biederman153ea352003-06-20 14:43:20 +00001419#define GLOBAL_SCOPE_DEPTH 1
1420#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001421
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001422static void compile_file(struct compile_state *old_state, const char *filename, int local);
1423
Eric Biederman5ade04a2003-10-22 04:03:46 +00001424
1425
1426static void init_compiler_state(struct compiler_state *compiler)
1427{
1428 memset(compiler, 0, sizeof(*compiler));
1429 compiler->label_prefix = "";
1430 compiler->ofilename = "auto.inc";
1431 compiler->flags = COMPILER_DEFAULT_FLAGS;
1432 compiler->debug = 0;
1433 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001434 compiler->include_path_count = 1;
1435 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1436 compiler->define_count = 1;
1437 compiler->defines = xcmalloc(sizeof(char *), "defines");
1438 compiler->undef_count = 1;
1439 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001440}
1441
1442struct compiler_flag {
1443 const char *name;
1444 unsigned long flag;
1445};
Eric Biederman90089602004-05-28 14:11:54 +00001446
1447struct compiler_arg {
1448 const char *name;
1449 unsigned long mask;
1450 struct compiler_flag flags[16];
1451};
1452
Eric Biederman5ade04a2003-10-22 04:03:46 +00001453static int set_flag(
1454 const struct compiler_flag *ptr, unsigned long *flags,
1455 int act, const char *flag)
1456{
1457 int result = -1;
1458 for(; ptr->name; ptr++) {
1459 if (strcmp(ptr->name, flag) == 0) {
1460 break;
1461 }
1462 }
1463 if (ptr->name) {
1464 result = 0;
1465 *flags &= ~(ptr->flag);
1466 if (act) {
1467 *flags |= ptr->flag;
1468 }
1469 }
1470 return result;
1471}
1472
Eric Biederman90089602004-05-28 14:11:54 +00001473static int set_arg(
1474 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1475{
1476 const char *val;
1477 int result = -1;
1478 int len;
1479 val = strchr(arg, '=');
1480 if (val) {
1481 len = val - arg;
1482 val++;
1483 for(; ptr->name; ptr++) {
1484 if (strncmp(ptr->name, arg, len) == 0) {
1485 break;
1486 }
1487 }
1488 if (ptr->name) {
1489 *flags &= ~ptr->mask;
1490 result = set_flag(&ptr->flags[0], flags, 1, val);
1491 }
1492 }
1493 return result;
1494}
Eric Biederman90089602004-05-28 14:11:54 +00001495
Stefan Reinauer14e22772010-04-27 06:56:47 +00001496
1497static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001498 const char *prefix, const char *invert_prefix)
1499{
1500 for(;ptr->name; ptr++) {
1501 fprintf(fp, "%s%s\n", prefix, ptr->name);
1502 if (invert_prefix) {
1503 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1504 }
1505 }
1506}
1507
1508static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1509 const char *prefix)
1510{
1511 for(;ptr->name; ptr++) {
1512 const struct compiler_flag *flag;
1513 for(flag = &ptr->flags[0]; flag->name; flag++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001514 fprintf(fp, "%s%s=%s\n",
Eric Biederman90089602004-05-28 14:11:54 +00001515 prefix, ptr->name, flag->name);
1516 }
1517 }
1518}
1519
1520static int append_string(size_t *max, const char ***vec, const char *str,
1521 const char *name)
1522{
1523 size_t count;
1524 count = ++(*max);
1525 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1526 (*vec)[count -1] = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001527 (*vec)[count -2] = str;
Eric Biederman90089602004-05-28 14:11:54 +00001528 return 0;
1529}
1530
1531static void arg_error(char *fmt, ...);
1532static const char *identifier(const char *str, const char *end);
1533
1534static int append_include_path(struct compiler_state *compiler, const char *str)
1535{
1536 int result;
1537 if (!exists(str, ".")) {
1538 arg_error("Nonexistent include path: `%s'\n",
1539 str);
1540 }
1541 result = append_string(&compiler->include_path_count,
1542 &compiler->include_paths, str, "include_paths");
1543 return result;
1544}
1545
1546static int append_define(struct compiler_state *compiler, const char *str)
1547{
1548 const char *end, *rest;
1549 int result;
1550
1551 end = strchr(str, '=');
1552 if (!end) {
1553 end = str + strlen(str);
1554 }
1555 rest = identifier(str, end);
1556 if (rest != end) {
1557 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001558 arg_error("Invalid name cannot define macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001559 len, len, str);
1560 }
1561 result = append_string(&compiler->define_count,
1562 &compiler->defines, str, "defines");
1563 return result;
1564}
1565
1566static int append_undef(struct compiler_state *compiler, const char *str)
1567{
1568 const char *end, *rest;
1569 int result;
1570
1571 end = str + strlen(str);
1572 rest = identifier(str, end);
1573 if (rest != end) {
1574 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001575 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001576 len, len, str);
1577 }
1578 result = append_string(&compiler->undef_count,
1579 &compiler->undefs, str, "undefs");
1580 return result;
1581}
1582
1583static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001584 { "trigraphs", COMPILER_TRIGRAPHS },
1585 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001586 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1587 { "simplify", COMPILER_SIMPLIFY },
1588 { "scc-transform", COMPILER_SCC_TRANSFORM },
1589 { "simplify-op", COMPILER_SIMPLIFY_OP },
1590 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1591 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1592 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1593 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1594 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1595 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1596 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1597 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1598 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1599 { 0, 0 },
1600};
1601static const struct compiler_arg romcc_args[] = {
1602 { "inline-policy", COMPILER_INLINE_MASK,
1603 {
1604 { "always", COMPILER_INLINE_ALWAYS, },
1605 { "never", COMPILER_INLINE_NEVER, },
1606 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1607 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1608 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1609 { 0, 0 },
1610 },
1611 },
1612 { 0, 0 },
1613};
1614static const struct compiler_flag romcc_opt_flags[] = {
1615 { "-O", COMPILER_SIMPLIFY },
1616 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001617 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001618 { 0, 0, },
1619};
1620static const struct compiler_flag romcc_debug_flags[] = {
1621 { "all", DEBUG_ALL },
1622 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1623 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1624 { "fdominators", DEBUG_FDOMINATORS },
1625 { "rdominators", DEBUG_RDOMINATORS },
1626 { "triples", DEBUG_TRIPLES },
1627 { "interference", DEBUG_INTERFERENCE },
1628 { "scc-transform", DEBUG_SCC_TRANSFORM },
1629 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1630 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1631 { "inline", DEBUG_INLINE },
1632 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1633 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1634 { "color-graph", DEBUG_COLOR_GRAPH },
1635 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1636 { "coalescing", DEBUG_COALESCING },
1637 { "coalescing2", DEBUG_COALESCING2 },
1638 { "verification", DEBUG_VERIFICATION },
1639 { "calls", DEBUG_CALLS },
1640 { "calls2", DEBUG_CALLS2 },
1641 { "tokens", DEBUG_TOKENS },
1642 { 0, 0 },
1643};
1644
Eric Biederman5ade04a2003-10-22 04:03:46 +00001645static int compiler_encode_flag(
1646 struct compiler_state *compiler, const char *flag)
1647{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001648 int act;
1649 int result;
1650
1651 act = 1;
1652 result = -1;
1653 if (strncmp(flag, "no-", 3) == 0) {
1654 flag += 3;
1655 act = 0;
1656 }
1657 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001658 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1659 }
1660 else if (strncmp(flag, "-E", 2) == 0) {
1661 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1662 }
1663 else if (strncmp(flag, "-I", 2) == 0) {
1664 result = append_include_path(compiler, flag + 2);
1665 }
1666 else if (strncmp(flag, "-D", 2) == 0) {
1667 result = append_define(compiler, flag + 2);
1668 }
1669 else if (strncmp(flag, "-U", 2) == 0) {
1670 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001671 }
1672 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1673 result = 0;
1674 compiler->label_prefix = flag + 13;
1675 }
1676 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1677 unsigned long max_passes;
1678 char *end;
1679 max_passes = strtoul(flag + 22, &end, 10);
1680 if (end[0] == '\0') {
1681 result = 0;
1682 compiler->max_allocation_passes = max_passes;
1683 }
1684 }
1685 else if (act && strcmp(flag, "debug") == 0) {
1686 result = 0;
1687 compiler->debug |= DEBUG_DEFAULT;
1688 }
1689 else if (strncmp(flag, "debug-", 6) == 0) {
1690 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001691 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001692 }
1693 else {
Eric Biederman90089602004-05-28 14:11:54 +00001694 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1695 if (result < 0) {
1696 result = set_arg(romcc_args, &compiler->flags, flag);
1697 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001698 }
1699 return result;
1700}
1701
Eric Biederman90089602004-05-28 14:11:54 +00001702static void compiler_usage(FILE *fp)
1703{
1704 flag_usage(fp, romcc_opt_flags, "", 0);
1705 flag_usage(fp, romcc_flags, "-f", "-fno-");
1706 arg_usage(fp, romcc_args, "-f");
1707 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1708 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1709 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1710 fprintf(fp, "-I<include path>\n");
1711 fprintf(fp, "-D<macro>[=defn]\n");
1712 fprintf(fp, "-U<macro>\n");
1713}
1714
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001715static void do_cleanup(struct compile_state *state)
1716{
1717 if (state->output) {
1718 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001719 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001720 state->output = 0;
1721 }
1722 if (state->dbgout) {
1723 fflush(state->dbgout);
1724 }
1725 if (state->errout) {
1726 fflush(state->errout);
1727 }
1728}
1729
1730static struct compile_state *exit_state;
1731static void exit_cleanup(void)
1732{
1733 if (exit_state) {
1734 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001735 }
1736}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001737
1738static int get_col(struct file_state *file)
1739{
1740 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001741 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001742 ptr = file->line_start;
1743 end = file->pos;
1744 for(col = 0; ptr < end; ptr++) {
1745 if (*ptr != '\t') {
1746 col++;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001747 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001748 else {
1749 col = (col & ~7) + 8;
1750 }
1751 }
1752 return col;
1753}
1754
1755static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1756{
1757 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001758 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001759 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001760 for(spot = triple->occurance; spot; spot = spot->parent) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001761 fprintf(fp, "%s:%d.%d: ",
Eric Biederman5ade04a2003-10-22 04:03:46 +00001762 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001763 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001764 return;
1765 }
1766 if (!state->file) {
1767 return;
1768 }
1769 col = get_col(state->file);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001770 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001771 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001772}
1773
Stefan Reinauer14e22772010-04-27 06:56:47 +00001774static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001775 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001776{
Eric Biederman90089602004-05-28 14:11:54 +00001777 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001778 va_list args;
1779 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001780 loc(fp, state, ptr);
1781 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001782 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001783 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001784 }
Eric Biederman90089602004-05-28 14:11:54 +00001785 fprintf(fp, "Internal compiler error: ");
1786 vfprintf(fp, fmt, args);
1787 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001788 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001789 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001790 abort();
1791}
1792
1793
Stefan Reinauer14e22772010-04-27 06:56:47 +00001794static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001795 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001796{
Eric Biederman90089602004-05-28 14:11:54 +00001797 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001798 va_list args;
1799 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001800 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001801 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001802 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001803 }
Eric Biederman90089602004-05-28 14:11:54 +00001804 fprintf(fp, "Internal compiler warning: ");
1805 vfprintf(fp, fmt, args);
1806 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001807 va_end(args);
1808}
1809
1810
1811
Stefan Reinauer14e22772010-04-27 06:56:47 +00001812static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001813 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001814{
Eric Biederman90089602004-05-28 14:11:54 +00001815 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001816 va_list args;
1817 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001818 loc(fp, state, ptr);
1819 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001820 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001821 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001822 }
Eric Biederman90089602004-05-28 14:11:54 +00001823 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001824 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001825 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001826 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001827 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001828 abort();
1829 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001830 exit(1);
1831}
1832
Stefan Reinauer14e22772010-04-27 06:56:47 +00001833static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001834 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001835{
Eric Biederman90089602004-05-28 14:11:54 +00001836 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001837 va_list args;
1838 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001839 loc(fp, state, ptr);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001840 fprintf(fp, "warning: ");
Eric Biederman90089602004-05-28 14:11:54 +00001841 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1842 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1843 }
1844 vfprintf(fp, fmt, args);
1845 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001846 va_end(args);
1847}
1848
Eric Biedermanb138ac82003-04-22 18:44:01 +00001849#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1850
Eric Biederman0babc1c2003-05-09 02:39:00 +00001851static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001852{
1853 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001854 if (op >= OP_MAX) {
1855 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001856 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001857 if (op < 0) {
1858 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001859 }
1860}
1861
Eric Biederman0babc1c2003-05-09 02:39:00 +00001862static void valid_ins(struct compile_state *state, struct triple *ptr)
1863{
1864 valid_op(state, ptr->op);
1865}
1866
Stefan Reinauer50542a82007-10-24 11:14:14 +00001867#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001868static void valid_param_count(struct compile_state *state, struct triple *ins)
1869{
1870 int lhs, rhs, misc, targ;
1871 valid_ins(state, ins);
1872 lhs = table_ops[ins->op].lhs;
1873 rhs = table_ops[ins->op].rhs;
1874 misc = table_ops[ins->op].misc;
1875 targ = table_ops[ins->op].targ;
1876
1877 if ((lhs >= 0) && (ins->lhs != lhs)) {
1878 internal_error(state, ins, "Bad lhs count");
1879 }
1880 if ((rhs >= 0) && (ins->rhs != rhs)) {
1881 internal_error(state, ins, "Bad rhs count");
1882 }
1883 if ((misc >= 0) && (ins->misc != misc)) {
1884 internal_error(state, ins, "Bad misc count");
1885 }
1886 if ((targ >= 0) && (ins->targ != targ)) {
1887 internal_error(state, ins, "Bad targ count");
1888 }
1889}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001890#endif
Eric Biederman90089602004-05-28 14:11:54 +00001891
Eric Biedermanb138ac82003-04-22 18:44:01 +00001892static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001893static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001894static void use_triple(struct triple *used, struct triple *user)
1895{
1896 struct triple_set **ptr, *new;
1897 if (!used)
1898 return;
1899 if (!user)
1900 return;
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001901 ptr = &used->use;
1902 while(*ptr) {
1903 if ((*ptr)->member == user) {
1904 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001905 }
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001906 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001907 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00001908 /* Append new to the head of the list,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001909 * copy_func and rename_block_variables
1910 * depends on this.
1911 */
1912 new = xcmalloc(sizeof(*new), "triple_set");
1913 new->member = user;
1914 new->next = used->use;
1915 used->use = new;
1916}
1917
1918static void unuse_triple(struct triple *used, struct triple *unuser)
1919{
1920 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001921 if (!used) {
1922 return;
1923 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001924 ptr = &used->use;
1925 while(*ptr) {
1926 use = *ptr;
1927 if (use->member == unuser) {
1928 *ptr = use->next;
1929 xfree(use);
1930 }
1931 else {
1932 ptr = &use->next;
1933 }
1934 }
1935}
1936
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001937static void put_occurance(struct occurance *occurance)
1938{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001939 if (occurance) {
1940 occurance->count -= 1;
1941 if (occurance->count <= 0) {
1942 if (occurance->parent) {
1943 put_occurance(occurance->parent);
1944 }
1945 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001946 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001947 }
1948}
1949
1950static void get_occurance(struct occurance *occurance)
1951{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001952 if (occurance) {
1953 occurance->count += 1;
1954 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001955}
1956
1957
1958static struct occurance *new_occurance(struct compile_state *state)
1959{
1960 struct occurance *result, *last;
1961 const char *filename;
1962 const char *function;
1963 int line, col;
1964
1965 function = "";
1966 filename = 0;
1967 line = 0;
1968 col = 0;
1969 if (state->file) {
1970 filename = state->file->report_name;
1971 line = state->file->report_line;
1972 col = get_col(state->file);
1973 }
1974 if (state->function) {
1975 function = state->function;
1976 }
1977 last = state->last_occurance;
1978 if (last &&
1979 (last->col == col) &&
1980 (last->line == line) &&
1981 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001982 ((last->filename == filename) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00001983 (strcmp(last->filename, filename) == 0)))
Eric Biederman83b991a2003-10-11 06:20:25 +00001984 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001985 get_occurance(last);
1986 return last;
1987 }
1988 if (last) {
1989 state->last_occurance = 0;
1990 put_occurance(last);
1991 }
1992 result = xmalloc(sizeof(*result), "occurance");
1993 result->count = 2;
1994 result->filename = filename;
1995 result->function = function;
1996 result->line = line;
1997 result->col = col;
1998 result->parent = 0;
1999 state->last_occurance = result;
2000 return result;
2001}
2002
2003static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002004 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002005{
2006 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002007 if (top->parent) {
2008 internal_error(state, 0, "inlining an already inlined function?");
2009 }
2010 /* If I have a null base treat it that way */
2011 if ((base->parent == 0) &&
2012 (base->col == 0) &&
2013 (base->line == 0) &&
2014 (base->function[0] == '\0') &&
2015 (base->filename[0] == '\0')) {
2016 base = 0;
2017 }
2018 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002019 last = state->last_occurance;
2020 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002021 (last->parent == base) &&
2022 (last->col == top->col) &&
2023 (last->line == top->line) &&
2024 (last->function == top->function) &&
2025 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002026 get_occurance(last);
2027 return last;
2028 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002029 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002030 if (last) {
2031 state->last_occurance = 0;
2032 put_occurance(last);
2033 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002034 /* Generate a new occurance structure */
2035 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002036 result = xmalloc(sizeof(*result), "occurance");
2037 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002038 result->filename = top->filename;
2039 result->function = top->function;
2040 result->line = top->line;
2041 result->col = top->col;
2042 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002043 state->last_occurance = result;
2044 return result;
2045}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002046
2047static struct occurance dummy_occurance = {
2048 .count = 2,
2049 .filename = __FILE__,
2050 .function = "",
2051 .line = __LINE__,
2052 .col = 0,
2053 .parent = 0,
2054};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002055
Eric Biederman90089602004-05-28 14:11:54 +00002056/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002057 * from a triple. Having allows certain sanity checks to pass even
2058 * when the original triple that was pointed to is gone.
2059 */
Eric Biederman90089602004-05-28 14:11:54 +00002060static struct triple unknown_triple = {
2061 .next = &unknown_triple,
2062 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002063 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002064 .op = OP_UNKNOWNVAL,
2065 .lhs = 0,
2066 .rhs = 0,
2067 .misc = 0,
2068 .targ = 0,
2069 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002070 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002071 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002072 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002073 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002074};
2075
Eric Biederman0babc1c2003-05-09 02:39:00 +00002076
Eric Biederman90089602004-05-28 14:11:54 +00002077static size_t registers_of(struct compile_state *state, struct type *type);
2078
Stefan Reinauer14e22772010-04-27 06:56:47 +00002079static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002080 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2081 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002082{
Eric Biederman90089602004-05-28 14:11:54 +00002083 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002084 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002085 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002086 dummy.op = op;
2087 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002088 valid_op(state, op);
2089 lhs = table_ops[op].lhs;
2090 rhs = table_ops[op].rhs;
2091 misc = table_ops[op].misc;
2092 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002093
2094 switch(op) {
2095 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002096 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002097 break;
2098 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002099 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002100 break;
2101 case OP_ADECL:
2102 lhs = registers_of(state, type);
2103 break;
2104 case OP_TUPLE:
2105 lhs = registers_of(state, type);
2106 break;
2107 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002108 rhs = rhs_wanted;
2109 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002110 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002111 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002112 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002113 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002114 }
2115 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002116 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002117 }
2118 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002119 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002120 }
2121 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002122 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002123 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002124
2125 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002126 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002127 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2128
2129 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2130 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002131 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002132 ret->lhs = lhs;
2133 ret->rhs = rhs;
2134 ret->misc = misc;
2135 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002136 ret->type = type;
2137 ret->next = ret;
2138 ret->prev = ret;
2139 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002140 /* A simple sanity check */
2141 if ((ret->op != op) ||
2142 (ret->lhs != lhs) ||
2143 (ret->rhs != rhs) ||
2144 (ret->misc != misc) ||
2145 (ret->targ != targ) ||
2146 (ret->type != type) ||
2147 (ret->next != ret) ||
2148 (ret->prev != ret) ||
2149 (ret->occurance != occurance)) {
2150 internal_error(state, ret, "huh?");
2151 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002152 return ret;
2153}
2154
Eric Biederman0babc1c2003-05-09 02:39:00 +00002155struct triple *dup_triple(struct compile_state *state, struct triple *src)
2156{
2157 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002158 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002159 src_lhs = src->lhs;
2160 src_rhs = src->rhs;
2161 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002162 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002163 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002164 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002165 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002166 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002167 return dup;
2168}
2169
Eric Biederman41203d92004-11-08 09:31:09 +00002170static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2171{
2172 struct triple *copy;
2173 copy = dup_triple(state, src);
2174 copy->use = 0;
2175 copy->next = copy->prev = copy;
2176 return copy;
2177}
2178
Stefan Reinauer14e22772010-04-27 06:56:47 +00002179static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002180 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002181{
2182 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002183 struct occurance *occurance;
2184 occurance = new_occurance(state);
2185 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002186 return ret;
2187}
2188
Stefan Reinauer14e22772010-04-27 06:56:47 +00002189static struct triple *build_triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002190 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002191 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002192{
2193 struct triple *ret;
2194 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002195 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002196 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002197 if (count > 0) {
2198 ret->param[0] = left;
2199 }
2200 if (count > 1) {
2201 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002202 }
2203 return ret;
2204}
2205
Stefan Reinauer14e22772010-04-27 06:56:47 +00002206static struct triple *triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002207 int op, struct type *type, struct triple *left, struct triple *right)
2208{
2209 struct triple *ret;
2210 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002211 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002212 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002213 if (count >= 1) {
2214 ret->param[0] = left;
2215 }
2216 if (count >= 2) {
2217 ret->param[1] = right;
2218 }
2219 return ret;
2220}
2221
Stefan Reinauer14e22772010-04-27 06:56:47 +00002222static struct triple *branch(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002223 struct triple *targ, struct triple *test)
2224{
2225 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002226 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002227 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002228 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002229 } else {
2230 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002231 }
2232 TARG(ret, 0) = targ;
2233 /* record the branch target was used */
2234 if (!targ || (targ->op != OP_LABEL)) {
2235 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002236 }
2237 return ret;
2238}
2239
Eric Biederman90089602004-05-28 14:11:54 +00002240static int triple_is_label(struct compile_state *state, struct triple *ins);
2241static int triple_is_call(struct compile_state *state, struct triple *ins);
2242static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002243static void insert_triple(struct compile_state *state,
2244 struct triple *first, struct triple *ptr)
2245{
2246 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002247 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002248 internal_error(state, ptr, "expression already used");
2249 }
2250 ptr->next = first;
2251 ptr->prev = first->prev;
2252 ptr->prev->next = ptr;
2253 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002254
2255 if (triple_is_cbranch(state, ptr->prev) ||
2256 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002257 unuse_triple(first, ptr->prev);
2258 use_triple(ptr, ptr->prev);
2259 }
2260 }
2261}
2262
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002263static int triple_stores_block(struct compile_state *state, struct triple *ins)
2264{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002265 /* This function is used to determine if u.block
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002266 * is utilized to store the current block number.
2267 */
2268 int stores_block;
2269 valid_ins(state, ins);
2270 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2271 return stores_block;
2272}
2273
Eric Biederman90089602004-05-28 14:11:54 +00002274static int triple_is_branch(struct compile_state *state, struct triple *ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002275static struct block *block_of_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002276 struct triple *ins)
2277{
2278 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002279 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002280 return 0;
2281 }
2282 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002283 while(ins != first && !triple_is_branch(state, ins->prev) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00002284 !triple_stores_block(state, ins))
2285 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002286 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002287 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002288 }
2289 ins = ins->prev;
2290 }
Eric Biederman90089602004-05-28 14:11:54 +00002291 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002292}
2293
Eric Biederman90089602004-05-28 14:11:54 +00002294static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002295static struct triple *pre_triple(struct compile_state *state,
2296 struct triple *base,
2297 int op, struct type *type, struct triple *left, struct triple *right)
2298{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002299 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002300 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002301 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002302 /* If I am an OP_PIECE jump to the real instruction */
2303 if (base->op == OP_PIECE) {
2304 base = MISC(base, 0);
2305 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002306 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002307 get_occurance(base->occurance);
2308 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002309 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002310 if (triple_stores_block(state, ret)) {
2311 ret->u.block = block;
2312 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002313 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002314 for(i = 0; i < ret->lhs; i++) {
2315 struct triple *piece;
2316 piece = LHS(ret, i);
2317 insert_triple(state, base, piece);
2318 use_triple(ret, piece);
2319 use_triple(piece, ret);
2320 }
2321 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002322 block->first = ret;
2323 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002324 return ret;
2325}
2326
2327static struct triple *post_triple(struct compile_state *state,
2328 struct triple *base,
2329 int op, struct type *type, struct triple *left, struct triple *right)
2330{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002331 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002332 struct triple *ret, *next;
2333 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002334 /* If I am an OP_PIECE jump to the real instruction */
2335 if (base->op == OP_PIECE) {
2336 base = MISC(base, 0);
2337 }
2338 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002339 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002340 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002341 base = LHS(base, zlhs - 1);
2342 }
2343
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002344 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002345 get_occurance(base->occurance);
2346 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002347 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002348 if (triple_stores_block(state, ret)) {
2349 ret->u.block = block;
2350 }
Eric Biederman90089602004-05-28 14:11:54 +00002351 next = base->next;
2352 insert_triple(state, next, ret);
2353 zlhs = ret->lhs;
2354 for(i = 0; i < zlhs; i++) {
2355 struct triple *piece;
2356 piece = LHS(ret, i);
2357 insert_triple(state, next, piece);
2358 use_triple(ret, piece);
2359 use_triple(piece, ret);
2360 }
2361 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002362 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002363 if (zlhs) {
2364 block->last = LHS(ret, zlhs - 1);
2365 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002366 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002367 return ret;
2368}
2369
Eric Biederman90089602004-05-28 14:11:54 +00002370static struct type *reg_type(
2371 struct compile_state *state, struct type *type, int reg);
2372
2373static void generate_lhs_piece(
2374 struct compile_state *state, struct triple *ins, int index)
2375{
2376 struct type *piece_type;
2377 struct triple *piece;
2378 get_occurance(ins->occurance);
2379 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2380
2381 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2382 piece_type = piece_type->left;
2383 }
2384#if 0
2385{
2386 static void name_of(FILE *fp, struct type *type);
2387 FILE * fp = state->errout;
2388 fprintf(fp, "piece_type(%d): ", index);
2389 name_of(fp, piece_type);
2390 fprintf(fp, "\n");
2391}
2392#endif
2393 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2394 piece->u.cval = index;
2395 LHS(ins, piece->u.cval) = piece;
2396 MISC(piece, 0) = ins;
2397}
2398
2399static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2400{
2401 int i, zlhs;
2402 zlhs = ins->lhs;
2403 for(i = 0; i < zlhs; i++) {
2404 generate_lhs_piece(state, ins, i);
2405 }
2406}
2407
Eric Biedermanb138ac82003-04-22 18:44:01 +00002408static struct triple *label(struct compile_state *state)
2409{
2410 /* Labels don't get a type */
2411 struct triple *result;
2412 result = triple(state, OP_LABEL, &void_type, 0, 0);
2413 return result;
2414}
2415
Eric Biederman90089602004-05-28 14:11:54 +00002416static struct triple *mkprog(struct compile_state *state, ...)
2417{
2418 struct triple *prog, *head, *arg;
2419 va_list args;
2420 int i;
2421
2422 head = label(state);
2423 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2424 RHS(prog, 0) = head;
2425 va_start(args, state);
2426 i = 0;
2427 while((arg = va_arg(args, struct triple *)) != 0) {
2428 if (++i >= 100) {
2429 internal_error(state, 0, "too many arguments to mkprog");
2430 }
2431 flatten(state, head, arg);
2432 }
2433 va_end(args);
2434 prog->type = head->prev->type;
2435 return prog;
2436}
2437static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002438static void display_triple(FILE *fp, struct triple *ins)
2439{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002440 struct occurance *ptr;
2441 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002442 char pre, post, vol;
2443 pre = post = vol = ' ';
2444 if (ins) {
2445 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2446 pre = '^';
2447 }
2448 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2449 post = ',';
2450 }
2451 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2452 vol = 'v';
2453 }
2454 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002455 }
Eric Biederman90089602004-05-28 14:11:54 +00002456 if (ins == 0) {
2457 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002458 }
Eric Biederman90089602004-05-28 14:11:54 +00002459 else if (ins->op == OP_INTCONST) {
2460 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002461 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002462 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002463 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002464 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002465 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002466 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002467 MISC(ins, 0), (unsigned long)(ins->u.cval));
2468 }
2469 else if (ins->op == OP_INDEX) {
2470 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002471 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002472 RHS(ins, 0), (unsigned long)(ins->u.cval));
2473 }
2474 else if (ins->op == OP_PIECE) {
2475 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 Biederman83b991a2003-10-11 06:20:25 +00002477 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002478 }
2479 else {
2480 int i, count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002481 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
Eric Biederman90089602004-05-28 14:11:54 +00002482 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2483 if (table_ops[ins->op].flags & BITFIELD) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002484 fprintf(fp, " <%2d-%2d:%2d>",
Eric Biederman90089602004-05-28 14:11:54 +00002485 ins->u.bitfield.offset,
2486 ins->u.bitfield.offset + ins->u.bitfield.size,
2487 ins->u.bitfield.size);
2488 }
2489 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002490 for(i = 0; i < count; i++) {
2491 fprintf(fp, " %-10p", ins->param[i]);
2492 }
2493 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002494 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002495 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002496 }
Eric Biederman90089602004-05-28 14:11:54 +00002497 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002498 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002499#if DEBUG_DISPLAY_TYPES
2500 fprintf(fp, " <");
2501 name_of(fp, ins->type);
2502 fprintf(fp, "> ");
2503#endif
2504#if DEBUG_DISPLAY_USES
2505 fprintf(fp, " [");
2506 for(user = ins->use; user; user = user->next) {
2507 fprintf(fp, " %-10p", user->member);
2508 }
2509 fprintf(fp, " ]");
2510#endif
2511 fprintf(fp, " @");
2512 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2513 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002514 ptr->function,
Eric Biederman90089602004-05-28 14:11:54 +00002515 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002516 ptr->line,
Eric Biederman90089602004-05-28 14:11:54 +00002517 ptr->col);
2518 }
2519 if (ins->op == OP_ASM) {
2520 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002521 }
2522 }
Eric Biederman90089602004-05-28 14:11:54 +00002523 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002524 fflush(fp);
2525}
2526
Eric Biederman90089602004-05-28 14:11:54 +00002527static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002528static void display_triple_changes(
2529 FILE *fp, const struct triple *new, const struct triple *orig)
2530{
2531
2532 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002533 new_count = TRIPLE_SIZE(new);
2534 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002535 if ((new->op != orig->op) ||
2536 (new_count != orig_count) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002537 (memcmp(orig->param, new->param,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002538 orig_count * sizeof(orig->param[0])) != 0) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002539 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
Eric Biederman5ade04a2003-10-22 04:03:46 +00002540 {
2541 struct occurance *ptr;
2542 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002543 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002544 if (orig->op == new->op) {
2545 fprintf(fp, " %-11s", tops(orig->op));
2546 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002547 fprintf(fp, " [%-10s %-10s]",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002548 tops(new->op), tops(orig->op));
2549 }
2550 min_count = new_count;
2551 if (min_count > orig_count) {
2552 min_count = orig_count;
2553 }
2554 for(indent = i = 0; i < min_count; i++) {
2555 if (orig->param[i] == new->param[i]) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002556 fprintf(fp, " %-11p",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002557 orig->param[i]);
2558 indent += 12;
2559 } else {
2560 fprintf(fp, " [%-10p %-10p]",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002561 new->param[i],
Eric Biederman5ade04a2003-10-22 04:03:46 +00002562 orig->param[i]);
2563 indent += 24;
2564 }
2565 }
2566 for(; i < orig_count; i++) {
2567 fprintf(fp, " [%-9p]", orig->param[i]);
2568 indent += 12;
2569 }
2570 for(; i < new_count; i++) {
2571 fprintf(fp, " [%-9p]", new->param[i]);
2572 indent += 12;
2573 }
2574 if ((new->op == OP_INTCONST)||
2575 (new->op == OP_ADDRCONST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002576 fprintf(fp, " <0x%08lx>",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002577 (unsigned long)(new->u.cval));
2578 indent += 13;
2579 }
2580 for(;indent < 36; indent++) {
2581 putc(' ', fp);
2582 }
Eric Biederman90089602004-05-28 14:11:54 +00002583
2584#if DEBUG_DISPLAY_TYPES
2585 fprintf(fp, " <");
2586 name_of(fp, new->type);
2587 if (!equiv_types(new->type, orig->type)) {
2588 fprintf(fp, " -- ");
2589 name_of(fp, orig->type);
2590 }
2591 fprintf(fp, "> ");
2592#endif
2593
Eric Biederman5ade04a2003-10-22 04:03:46 +00002594 fprintf(fp, " @");
2595 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2596 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002597 ptr->function,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002598 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002599 ptr->line,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002600 ptr->col);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002601
Eric Biederman5ade04a2003-10-22 04:03:46 +00002602 }
2603 fprintf(fp, "\n");
2604 fflush(fp);
2605 }
2606}
2607
Eric Biederman83b991a2003-10-11 06:20:25 +00002608static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002609{
2610 /* Does the triple have no side effects.
Stefan Reinauer14e22772010-04-27 06:56:47 +00002611 * I.e. Rexecuting the triple with the same arguments
Eric Biedermanb138ac82003-04-22 18:44:01 +00002612 * gives the same value.
2613 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002614 unsigned pure;
2615 valid_ins(state, ins);
2616 pure = PURE_BITS(table_ops[ins->op].flags);
2617 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002618 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002619 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002620 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002621 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002622}
2623
Stefan Reinauer14e22772010-04-27 06:56:47 +00002624static int triple_is_branch_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002625 struct triple *ins, unsigned type)
2626{
2627 /* Is this one of the passed branch types? */
2628 valid_ins(state, ins);
2629 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2630}
2631
Eric Biederman0babc1c2003-05-09 02:39:00 +00002632static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002633{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002634 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002635 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002636 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002637}
2638
Eric Biederman90089602004-05-28 14:11:54 +00002639static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002640{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002641 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002642 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002643}
2644
Eric Biederman90089602004-05-28 14:11:54 +00002645static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002646{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002647 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002648 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002649 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002650 type = BRANCH_BITS(table_ops[ins->op].flags);
2651 return (type != 0) && (type != CBRANCH);
2652}
2653
2654static int triple_is_call(struct compile_state *state, struct triple *ins)
2655{
2656 /* Is this triple a call instruction? */
2657 return triple_is_branch_type(state, ins, CALLBRANCH);
2658}
2659
2660static int triple_is_ret(struct compile_state *state, struct triple *ins)
2661{
2662 /* Is this triple a return instruction? */
2663 return triple_is_branch_type(state, ins, RETBRANCH);
2664}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002665
Stefan Reinauer50542a82007-10-24 11:14:14 +00002666#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002667static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2668{
2669 /* Is this triple an unconditional branch and not a call or a
2670 * return? */
2671 return triple_is_branch_type(state, ins, UBRANCH);
2672}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002673#endif
Eric Biederman90089602004-05-28 14:11:54 +00002674
2675static int triple_is_end(struct compile_state *state, struct triple *ins)
2676{
2677 return triple_is_branch_type(state, ins, ENDBRANCH);
2678}
2679
2680static int triple_is_label(struct compile_state *state, struct triple *ins)
2681{
2682 valid_ins(state, ins);
2683 return (ins->op == OP_LABEL);
2684}
2685
2686static struct triple *triple_to_block_start(
2687 struct compile_state *state, struct triple *start)
2688{
2689 while(!triple_is_branch(state, start->prev) &&
2690 (!triple_is_label(state, start) || !start->use)) {
2691 start = start->prev;
2692 }
2693 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002694}
2695
Eric Biederman0babc1c2003-05-09 02:39:00 +00002696static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002697{
2698 /* This function is used to determine which triples need
2699 * a register.
2700 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002701 int is_def;
2702 valid_ins(state, ins);
2703 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002704 if (ins->lhs >= 1) {
2705 is_def = 0;
2706 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002707 return is_def;
2708}
2709
Eric Biederman83b991a2003-10-11 06:20:25 +00002710static int triple_is_structural(struct compile_state *state, struct triple *ins)
2711{
2712 int is_structural;
2713 valid_ins(state, ins);
2714 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2715 return is_structural;
2716}
2717
Eric Biederman90089602004-05-28 14:11:54 +00002718static int triple_is_part(struct compile_state *state, struct triple *ins)
2719{
2720 int is_part;
2721 valid_ins(state, ins);
2722 is_part = (table_ops[ins->op].flags & PART) == PART;
2723 return is_part;
2724}
2725
2726static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2727{
2728 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2729}
2730
Eric Biederman0babc1c2003-05-09 02:39:00 +00002731static struct triple **triple_iter(struct compile_state *state,
2732 size_t count, struct triple **vector,
2733 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002734{
2735 struct triple **ret;
2736 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002737 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002738 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002739 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002740 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002741 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002742 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002743 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002744 }
2745 return ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002746
Eric Biedermanb138ac82003-04-22 18:44:01 +00002747}
2748
2749static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002750 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002751{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002752 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002753 ins, last);
2754}
2755
2756static struct triple **triple_rhs(struct compile_state *state,
2757 struct triple *ins, struct triple **last)
2758{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002759 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002760 ins, last);
2761}
2762
2763static struct triple **triple_misc(struct compile_state *state,
2764 struct triple *ins, struct triple **last)
2765{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002766 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002767 ins, last);
2768}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002769
Eric Biederman90089602004-05-28 14:11:54 +00002770static struct triple **do_triple_targ(struct compile_state *state,
2771 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002772{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002773 size_t count;
2774 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002775 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002776 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002777 count = ins->targ;
2778 next_is_targ = 0;
2779 if (triple_is_cbranch(state, ins)) {
2780 next_is_targ = 1;
2781 }
2782 if (!call_edges && triple_is_call(state, ins)) {
2783 count = 0;
2784 }
2785 if (next_edges && triple_is_call(state, ins)) {
2786 next_is_targ = 1;
2787 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002788 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002789 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002790 if (!last) {
2791 ret = &ins->next;
2792 } else if (last == &ins->next) {
2793 last = 0;
2794 }
2795 }
2796 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002797 if (!last) {
2798 ret = vector;
2799 }
2800 else if ((last >= vector) && (last < (vector + count - 1))) {
2801 ret = last + 1;
2802 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002803 else if (last == vector + count - 1) {
2804 last = 0;
2805 }
2806 }
Eric Biederman90089602004-05-28 14:11:54 +00002807 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002808 struct triple_set *use;
2809 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002810 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002811 continue;
2812 }
2813 if (!last) {
2814 ret = &use->member->next;
2815 break;
2816 }
2817 else if (last == &use->member->next) {
2818 last = 0;
2819 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002820 }
2821 }
2822 return ret;
2823}
2824
Eric Biederman90089602004-05-28 14:11:54 +00002825static struct triple **triple_targ(struct compile_state *state,
2826 struct triple *ins, struct triple **last)
2827{
2828 return do_triple_targ(state, ins, last, 1, 1);
2829}
2830
2831static struct triple **triple_edge_targ(struct compile_state *state,
2832 struct triple *ins, struct triple **last)
2833{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002834 return do_triple_targ(state, ins, last,
Eric Biederman90089602004-05-28 14:11:54 +00002835 state->functions_joined, !state->functions_joined);
2836}
2837
2838static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2839{
2840 struct triple *next;
2841 int lhs, i;
2842 lhs = ins->lhs;
2843 next = ins->next;
2844 for(i = 0; i < lhs; i++) {
2845 struct triple *piece;
2846 piece = LHS(ins, i);
2847 if (next != piece) {
2848 internal_error(state, ins, "malformed lhs on %s",
2849 tops(ins->op));
2850 }
2851 if (next->op != OP_PIECE) {
2852 internal_error(state, ins, "bad lhs op %s at %d on %s",
2853 tops(next->op), i, tops(ins->op));
2854 }
2855 if (next->u.cval != i) {
2856 internal_error(state, ins, "bad u.cval of %d %d expected",
2857 next->u.cval, i);
2858 }
2859 next = next->next;
2860 }
2861 return next;
2862}
2863
2864/* Function piece accessor functions */
Stefan Reinauer14e22772010-04-27 06:56:47 +00002865static struct triple *do_farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002866 struct triple *func, unsigned index)
2867{
2868 struct type *ftype;
2869 struct triple *first, *arg;
2870 unsigned i;
2871
2872 ftype = func->type;
2873 if((index < 0) || (index >= (ftype->elements + 2))) {
2874 internal_error(state, func, "bad argument index: %d", index);
2875 }
2876 first = RHS(func, 0);
2877 arg = first->next;
2878 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2879 /* do nothing */
2880 }
2881 if (arg->op != OP_ADECL) {
2882 internal_error(state, 0, "arg not adecl?");
2883 }
2884 return arg;
2885}
2886static struct triple *fresult(struct compile_state *state, struct triple *func)
2887{
2888 return do_farg(state, func, 0);
2889}
2890static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2891{
2892 return do_farg(state, func, 1);
2893}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002894static struct triple *farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002895 struct triple *func, unsigned index)
2896{
2897 return do_farg(state, func, index + 2);
2898}
2899
2900
2901static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2902{
2903 struct triple *first, *ins;
2904 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2905 first = ins = RHS(func, 0);
2906 do {
2907 if (triple_is_label(state, ins) && ins->use) {
2908 fprintf(fp, "%p:\n", ins);
2909 }
2910 display_triple(fp, ins);
2911
2912 if (triple_is_branch(state, ins)) {
2913 fprintf(fp, "\n");
2914 }
2915 if (ins->next->prev != ins) {
2916 internal_error(state, ins->next, "bad prev");
2917 }
2918 ins = ins->next;
2919 } while(ins != first);
2920}
2921
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002922static void verify_use(struct compile_state *state,
2923 struct triple *user, struct triple *used)
2924{
2925 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002926 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002927 for(i = 0; i < size; i++) {
2928 if (user->param[i] == used) {
2929 break;
2930 }
2931 }
2932 if (triple_is_branch(state, user)) {
2933 if (user->next == used) {
2934 i = -1;
2935 }
2936 }
2937 if (i == size) {
2938 internal_error(state, user, "%s(%p) does not use %s(%p)",
2939 tops(user->op), user, tops(used->op), used);
2940 }
2941}
2942
Stefan Reinauer14e22772010-04-27 06:56:47 +00002943static int find_rhs_use(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002944 struct triple *user, struct triple *used)
2945{
2946 struct triple **param;
2947 int size, i;
2948 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002949
2950#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002951#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002952#endif
Eric Biederman90089602004-05-28 14:11:54 +00002953 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002954 param = &RHS(user, 0);
2955 for(i = 0; i < size; i++) {
2956 if (param[i] == used) {
2957 return i;
2958 }
2959 }
2960 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002961}
2962
2963static void free_triple(struct compile_state *state, struct triple *ptr)
2964{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002965 size_t size;
2966 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002967 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002968 ptr->prev->next = ptr->next;
2969 ptr->next->prev = ptr->prev;
2970 if (ptr->use) {
2971 internal_error(state, ptr, "ptr->use != 0");
2972 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002973 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002974 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002975 xfree(ptr);
2976}
2977
2978static void release_triple(struct compile_state *state, struct triple *ptr)
2979{
2980 struct triple_set *set, *next;
2981 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002982 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002983 if (ptr == &unknown_triple) {
2984 return;
2985 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002986 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002987 /* Make certain the we are not the first or last element of a block */
2988 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002989 if (block) {
2990 if ((block->last == ptr) && (block->first == ptr)) {
2991 block->last = block->first = 0;
2992 }
2993 else if (block->last == ptr) {
2994 block->last = ptr->prev;
2995 }
2996 else if (block->first == ptr) {
2997 block->first = ptr->next;
2998 }
Eric Biederman66fe2222003-07-04 15:14:04 +00002999 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003000 /* Remove ptr from use chains where it is the user */
3001 expr = triple_rhs(state, ptr, 0);
3002 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3003 if (*expr) {
3004 unuse_triple(*expr, ptr);
3005 }
3006 }
3007 expr = triple_lhs(state, ptr, 0);
3008 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3009 if (*expr) {
3010 unuse_triple(*expr, ptr);
3011 }
3012 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003013 expr = triple_misc(state, ptr, 0);
3014 for(; expr; expr = triple_misc(state, ptr, expr)) {
3015 if (*expr) {
3016 unuse_triple(*expr, ptr);
3017 }
3018 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003019 expr = triple_targ(state, ptr, 0);
3020 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003021 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003022 unuse_triple(*expr, ptr);
3023 }
3024 }
3025 /* Reomve ptr from use chains where it is used */
3026 for(set = ptr->use; set; set = next) {
3027 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003028 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003029 expr = triple_rhs(state, set->member, 0);
3030 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3031 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003032 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003033 }
3034 }
3035 expr = triple_lhs(state, set->member, 0);
3036 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3037 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003038 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003039 }
3040 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003041 expr = triple_misc(state, set->member, 0);
3042 for(; expr; expr = triple_misc(state, set->member, expr)) {
3043 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003044 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003045 }
3046 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003047 expr = triple_targ(state, set->member, 0);
3048 for(; expr; expr = triple_targ(state, set->member, expr)) {
3049 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003050 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003051 }
3052 }
3053 unuse_triple(ptr, set->member);
3054 }
3055 free_triple(state, ptr);
3056}
3057
Eric Biederman5ade04a2003-10-22 04:03:46 +00003058static void print_triples(struct compile_state *state);
3059static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003060
Jason Schildt27b85112005-08-10 14:31:52 +00003061#define TOK_UNKNOWN 0
3062#define TOK_SPACE 1
3063#define TOK_SEMI 2
3064#define TOK_LBRACE 3
3065#define TOK_RBRACE 4
3066#define TOK_COMMA 5
3067#define TOK_EQ 6
3068#define TOK_COLON 7
3069#define TOK_LBRACKET 8
3070#define TOK_RBRACKET 9
3071#define TOK_LPAREN 10
3072#define TOK_RPAREN 11
3073#define TOK_STAR 12
3074#define TOK_DOTS 13
3075#define TOK_MORE 14
3076#define TOK_LESS 15
3077#define TOK_TIMESEQ 16
3078#define TOK_DIVEQ 17
3079#define TOK_MODEQ 18
3080#define TOK_PLUSEQ 19
3081#define TOK_MINUSEQ 20
3082#define TOK_SLEQ 21
3083#define TOK_SREQ 22
3084#define TOK_ANDEQ 23
3085#define TOK_XOREQ 24
3086#define TOK_OREQ 25
3087#define TOK_EQEQ 26
3088#define TOK_NOTEQ 27
3089#define TOK_QUEST 28
3090#define TOK_LOGOR 29
3091#define TOK_LOGAND 30
3092#define TOK_OR 31
3093#define TOK_AND 32
3094#define TOK_XOR 33
3095#define TOK_LESSEQ 34
3096#define TOK_MOREEQ 35
3097#define TOK_SL 36
3098#define TOK_SR 37
3099#define TOK_PLUS 38
3100#define TOK_MINUS 39
3101#define TOK_DIV 40
3102#define TOK_MOD 41
3103#define TOK_PLUSPLUS 42
3104#define TOK_MINUSMINUS 43
3105#define TOK_BANG 44
3106#define TOK_ARROW 45
3107#define TOK_DOT 46
3108#define TOK_TILDE 47
3109#define TOK_LIT_STRING 48
3110#define TOK_LIT_CHAR 49
3111#define TOK_LIT_INT 50
3112#define TOK_LIT_FLOAT 51
3113#define TOK_MACRO 52
3114#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003115
Jason Schildt27b85112005-08-10 14:31:52 +00003116#define TOK_IDENT 54
3117#define TOK_STRUCT_NAME 55
3118#define TOK_ENUM_CONST 56
3119#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003120
Jason Schildt27b85112005-08-10 14:31:52 +00003121#define TOK_AUTO 58
3122#define TOK_BREAK 59
3123#define TOK_CASE 60
3124#define TOK_CHAR 61
3125#define TOK_CONST 62
3126#define TOK_CONTINUE 63
3127#define TOK_DEFAULT 64
3128#define TOK_DO 65
3129#define TOK_DOUBLE 66
3130#define TOK_ELSE 67
3131#define TOK_ENUM 68
3132#define TOK_EXTERN 69
3133#define TOK_FLOAT 70
3134#define TOK_FOR 71
3135#define TOK_GOTO 72
3136#define TOK_IF 73
3137#define TOK_INLINE 74
3138#define TOK_INT 75
3139#define TOK_LONG 76
3140#define TOK_REGISTER 77
3141#define TOK_RESTRICT 78
3142#define TOK_RETURN 79
3143#define TOK_SHORT 80
3144#define TOK_SIGNED 81
3145#define TOK_SIZEOF 82
3146#define TOK_STATIC 83
3147#define TOK_STRUCT 84
3148#define TOK_SWITCH 85
3149#define TOK_TYPEDEF 86
3150#define TOK_UNION 87
3151#define TOK_UNSIGNED 88
3152#define TOK_VOID 89
3153#define TOK_VOLATILE 90
3154#define TOK_WHILE 91
3155#define TOK_ASM 92
3156#define TOK_ATTRIBUTE 93
3157#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003158#define TOK_FIRST_KEYWORD TOK_AUTO
3159#define TOK_LAST_KEYWORD TOK_ALIGNOF
3160
Eric Biedermancb364952004-11-15 10:46:44 +00003161#define TOK_MDEFINE 100
3162#define TOK_MDEFINED 101
3163#define TOK_MUNDEF 102
3164#define TOK_MINCLUDE 103
3165#define TOK_MLINE 104
3166#define TOK_MERROR 105
3167#define TOK_MWARNING 106
3168#define TOK_MPRAGMA 107
3169#define TOK_MIFDEF 108
3170#define TOK_MIFNDEF 109
3171#define TOK_MELIF 110
3172#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003173
Eric Biedermancb364952004-11-15 10:46:44 +00003174#define TOK_FIRST_MACRO TOK_MDEFINE
3175#define TOK_LAST_MACRO TOK_MENDIF
Stefan Reinauer14e22772010-04-27 06:56:47 +00003176
Eric Biedermancb364952004-11-15 10:46:44 +00003177#define TOK_MIF 112
3178#define TOK_MELSE 113
3179#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003180
Eric Biedermancb364952004-11-15 10:46:44 +00003181#define TOK_EOL 115
3182#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003183
3184static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003185[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003186[TOK_SPACE ] = ":space:",
3187[TOK_SEMI ] = ";",
3188[TOK_LBRACE ] = "{",
3189[TOK_RBRACE ] = "}",
3190[TOK_COMMA ] = ",",
3191[TOK_EQ ] = "=",
3192[TOK_COLON ] = ":",
3193[TOK_LBRACKET ] = "[",
3194[TOK_RBRACKET ] = "]",
3195[TOK_LPAREN ] = "(",
3196[TOK_RPAREN ] = ")",
3197[TOK_STAR ] = "*",
3198[TOK_DOTS ] = "...",
3199[TOK_MORE ] = ">",
3200[TOK_LESS ] = "<",
3201[TOK_TIMESEQ ] = "*=",
3202[TOK_DIVEQ ] = "/=",
3203[TOK_MODEQ ] = "%=",
3204[TOK_PLUSEQ ] = "+=",
3205[TOK_MINUSEQ ] = "-=",
3206[TOK_SLEQ ] = "<<=",
3207[TOK_SREQ ] = ">>=",
3208[TOK_ANDEQ ] = "&=",
3209[TOK_XOREQ ] = "^=",
3210[TOK_OREQ ] = "|=",
3211[TOK_EQEQ ] = "==",
3212[TOK_NOTEQ ] = "!=",
3213[TOK_QUEST ] = "?",
3214[TOK_LOGOR ] = "||",
3215[TOK_LOGAND ] = "&&",
3216[TOK_OR ] = "|",
3217[TOK_AND ] = "&",
3218[TOK_XOR ] = "^",
3219[TOK_LESSEQ ] = "<=",
3220[TOK_MOREEQ ] = ">=",
3221[TOK_SL ] = "<<",
3222[TOK_SR ] = ">>",
3223[TOK_PLUS ] = "+",
3224[TOK_MINUS ] = "-",
3225[TOK_DIV ] = "/",
3226[TOK_MOD ] = "%",
3227[TOK_PLUSPLUS ] = "++",
3228[TOK_MINUSMINUS ] = "--",
3229[TOK_BANG ] = "!",
3230[TOK_ARROW ] = "->",
3231[TOK_DOT ] = ".",
3232[TOK_TILDE ] = "~",
3233[TOK_LIT_STRING ] = ":string:",
3234[TOK_IDENT ] = ":ident:",
3235[TOK_TYPE_NAME ] = ":typename:",
3236[TOK_LIT_CHAR ] = ":char:",
3237[TOK_LIT_INT ] = ":integer:",
3238[TOK_LIT_FLOAT ] = ":float:",
3239[TOK_MACRO ] = "#",
3240[TOK_CONCATENATE ] = "##",
3241
3242[TOK_AUTO ] = "auto",
3243[TOK_BREAK ] = "break",
3244[TOK_CASE ] = "case",
3245[TOK_CHAR ] = "char",
3246[TOK_CONST ] = "const",
3247[TOK_CONTINUE ] = "continue",
3248[TOK_DEFAULT ] = "default",
3249[TOK_DO ] = "do",
3250[TOK_DOUBLE ] = "double",
3251[TOK_ELSE ] = "else",
3252[TOK_ENUM ] = "enum",
3253[TOK_EXTERN ] = "extern",
3254[TOK_FLOAT ] = "float",
3255[TOK_FOR ] = "for",
3256[TOK_GOTO ] = "goto",
3257[TOK_IF ] = "if",
3258[TOK_INLINE ] = "inline",
3259[TOK_INT ] = "int",
3260[TOK_LONG ] = "long",
3261[TOK_REGISTER ] = "register",
3262[TOK_RESTRICT ] = "restrict",
3263[TOK_RETURN ] = "return",
3264[TOK_SHORT ] = "short",
3265[TOK_SIGNED ] = "signed",
3266[TOK_SIZEOF ] = "sizeof",
3267[TOK_STATIC ] = "static",
3268[TOK_STRUCT ] = "struct",
3269[TOK_SWITCH ] = "switch",
3270[TOK_TYPEDEF ] = "typedef",
3271[TOK_UNION ] = "union",
3272[TOK_UNSIGNED ] = "unsigned",
3273[TOK_VOID ] = "void",
3274[TOK_VOLATILE ] = "volatile",
3275[TOK_WHILE ] = "while",
3276[TOK_ASM ] = "asm",
3277[TOK_ATTRIBUTE ] = "__attribute__",
3278[TOK_ALIGNOF ] = "__alignof__",
3279
Eric Biederman41203d92004-11-08 09:31:09 +00003280[TOK_MDEFINE ] = "#define",
3281[TOK_MDEFINED ] = "#defined",
3282[TOK_MUNDEF ] = "#undef",
3283[TOK_MINCLUDE ] = "#include",
3284[TOK_MLINE ] = "#line",
3285[TOK_MERROR ] = "#error",
3286[TOK_MWARNING ] = "#warning",
3287[TOK_MPRAGMA ] = "#pragma",
3288[TOK_MIFDEF ] = "#ifdef",
3289[TOK_MIFNDEF ] = "#ifndef",
3290[TOK_MELIF ] = "#elif",
3291[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003292
Eric Biederman41203d92004-11-08 09:31:09 +00003293[TOK_MIF ] = "#if",
3294[TOK_MELSE ] = "#else",
3295[TOK_MIDENT ] = "#:ident:",
Stefan Reinauer14e22772010-04-27 06:56:47 +00003296[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003297[TOK_EOF ] = "EOF",
3298};
3299
3300static unsigned int hash(const char *str, int str_len)
3301{
3302 unsigned int hash;
3303 const char *end;
3304 end = str + str_len;
3305 hash = 0;
3306 for(; str < end; str++) {
3307 hash = (hash *263) + *str;
3308 }
3309 hash = hash & (HASH_TABLE_SIZE -1);
3310 return hash;
3311}
3312
3313static struct hash_entry *lookup(
3314 struct compile_state *state, const char *name, int name_len)
3315{
3316 struct hash_entry *entry;
3317 unsigned int index;
3318 index = hash(name, name_len);
3319 entry = state->hash_table[index];
Stefan Reinauer14e22772010-04-27 06:56:47 +00003320 while(entry &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003321 ((entry->name_len != name_len) ||
3322 (memcmp(entry->name, name, name_len) != 0))) {
3323 entry = entry->next;
3324 }
3325 if (!entry) {
3326 char *new_name;
3327 /* Get a private copy of the name */
3328 new_name = xmalloc(name_len + 1, "hash_name");
3329 memcpy(new_name, name, name_len);
3330 new_name[name_len] = '\0';
3331
3332 /* Create a new hash entry */
3333 entry = xcmalloc(sizeof(*entry), "hash_entry");
3334 entry->next = state->hash_table[index];
3335 entry->name = new_name;
3336 entry->name_len = name_len;
3337
3338 /* Place the new entry in the hash table */
3339 state->hash_table[index] = entry;
3340 }
3341 return entry;
3342}
3343
3344static void ident_to_keyword(struct compile_state *state, struct token *tk)
3345{
3346 struct hash_entry *entry;
3347 entry = tk->ident;
3348 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3349 (entry->tok == TOK_ENUM_CONST) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00003350 ((entry->tok >= TOK_FIRST_KEYWORD) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003351 (entry->tok <= TOK_LAST_KEYWORD)))) {
3352 tk->tok = entry->tok;
3353 }
3354}
3355
3356static void ident_to_macro(struct compile_state *state, struct token *tk)
3357{
3358 struct hash_entry *entry;
3359 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003360 if (!entry)
3361 return;
3362 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003363 tk->tok = entry->tok;
3364 }
Eric Biederman41203d92004-11-08 09:31:09 +00003365 else if (entry->tok == TOK_IF) {
3366 tk->tok = TOK_MIF;
3367 }
3368 else if (entry->tok == TOK_ELSE) {
3369 tk->tok = TOK_MELSE;
3370 }
3371 else {
3372 tk->tok = TOK_MIDENT;
3373 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003374}
3375
3376static void hash_keyword(
3377 struct compile_state *state, const char *keyword, int tok)
3378{
3379 struct hash_entry *entry;
3380 entry = lookup(state, keyword, strlen(keyword));
3381 if (entry && entry->tok != TOK_UNKNOWN) {
3382 die("keyword %s already hashed", keyword);
3383 }
3384 entry->tok = tok;
3385}
3386
Eric Biederman90089602004-05-28 14:11:54 +00003387static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003388 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003389 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003390{
3391 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003392 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003393 error(state, 0, "%s already defined", ident->name);
3394 }
3395 sym = xcmalloc(sizeof(*sym), "symbol");
3396 sym->ident = ident;
3397 sym->def = def;
3398 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003399 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003400 sym->next = *chain;
3401 *chain = sym;
3402}
3403
Eric Biederman90089602004-05-28 14:11:54 +00003404static void symbol(
3405 struct compile_state *state, struct hash_entry *ident,
3406 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003407{
Eric Biederman90089602004-05-28 14:11:54 +00003408 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3409}
3410
Stefan Reinauer14e22772010-04-27 06:56:47 +00003411static void var_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003412 struct hash_entry *ident, struct triple *def)
3413{
3414 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3415 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003416 }
Eric Biederman90089602004-05-28 14:11:54 +00003417 symbol(state, ident, &ident->sym_ident, def, def->type);
3418}
3419
Stefan Reinauer14e22772010-04-27 06:56:47 +00003420static void label_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003421 struct hash_entry *ident, struct triple *label, int depth)
3422{
3423 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003424}
3425
Eric Biedermanb138ac82003-04-22 18:44:01 +00003426static void start_scope(struct compile_state *state)
3427{
3428 state->scope_depth++;
3429}
3430
Eric Biederman90089602004-05-28 14:11:54 +00003431static void end_scope_syms(struct compile_state *state,
3432 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003433{
3434 struct symbol *sym, *next;
3435 sym = *chain;
3436 while(sym && (sym->scope_depth == depth)) {
3437 next = sym->next;
3438 xfree(sym);
3439 sym = next;
3440 }
3441 *chain = sym;
3442}
3443
3444static void end_scope(struct compile_state *state)
3445{
3446 int i;
3447 int depth;
3448 /* Walk through the hash table and remove all symbols
Stefan Reinauer14e22772010-04-27 06:56:47 +00003449 * in the current scope.
Eric Biedermanb138ac82003-04-22 18:44:01 +00003450 */
3451 depth = state->scope_depth;
3452 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3453 struct hash_entry *entry;
3454 entry = state->hash_table[i];
3455 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003456 end_scope_syms(state, &entry->sym_label, depth);
3457 end_scope_syms(state, &entry->sym_tag, depth);
3458 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003459 entry = entry->next;
3460 }
3461 }
3462 state->scope_depth = depth - 1;
3463}
3464
3465static void register_keywords(struct compile_state *state)
3466{
3467 hash_keyword(state, "auto", TOK_AUTO);
3468 hash_keyword(state, "break", TOK_BREAK);
3469 hash_keyword(state, "case", TOK_CASE);
3470 hash_keyword(state, "char", TOK_CHAR);
3471 hash_keyword(state, "const", TOK_CONST);
3472 hash_keyword(state, "continue", TOK_CONTINUE);
3473 hash_keyword(state, "default", TOK_DEFAULT);
3474 hash_keyword(state, "do", TOK_DO);
3475 hash_keyword(state, "double", TOK_DOUBLE);
3476 hash_keyword(state, "else", TOK_ELSE);
3477 hash_keyword(state, "enum", TOK_ENUM);
3478 hash_keyword(state, "extern", TOK_EXTERN);
3479 hash_keyword(state, "float", TOK_FLOAT);
3480 hash_keyword(state, "for", TOK_FOR);
3481 hash_keyword(state, "goto", TOK_GOTO);
3482 hash_keyword(state, "if", TOK_IF);
3483 hash_keyword(state, "inline", TOK_INLINE);
3484 hash_keyword(state, "int", TOK_INT);
3485 hash_keyword(state, "long", TOK_LONG);
3486 hash_keyword(state, "register", TOK_REGISTER);
3487 hash_keyword(state, "restrict", TOK_RESTRICT);
3488 hash_keyword(state, "return", TOK_RETURN);
3489 hash_keyword(state, "short", TOK_SHORT);
3490 hash_keyword(state, "signed", TOK_SIGNED);
3491 hash_keyword(state, "sizeof", TOK_SIZEOF);
3492 hash_keyword(state, "static", TOK_STATIC);
3493 hash_keyword(state, "struct", TOK_STRUCT);
3494 hash_keyword(state, "switch", TOK_SWITCH);
3495 hash_keyword(state, "typedef", TOK_TYPEDEF);
3496 hash_keyword(state, "union", TOK_UNION);
3497 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3498 hash_keyword(state, "void", TOK_VOID);
3499 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003500 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003501 hash_keyword(state, "while", TOK_WHILE);
3502 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003503 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003504 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3505 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3506}
3507
3508static void register_macro_keywords(struct compile_state *state)
3509{
Eric Biederman41203d92004-11-08 09:31:09 +00003510 hash_keyword(state, "define", TOK_MDEFINE);
3511 hash_keyword(state, "defined", TOK_MDEFINED);
3512 hash_keyword(state, "undef", TOK_MUNDEF);
3513 hash_keyword(state, "include", TOK_MINCLUDE);
3514 hash_keyword(state, "line", TOK_MLINE);
3515 hash_keyword(state, "error", TOK_MERROR);
3516 hash_keyword(state, "warning", TOK_MWARNING);
3517 hash_keyword(state, "pragma", TOK_MPRAGMA);
3518 hash_keyword(state, "ifdef", TOK_MIFDEF);
3519 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3520 hash_keyword(state, "elif", TOK_MELIF);
3521 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003522}
3523
Eric Biederman90089602004-05-28 14:11:54 +00003524
3525static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3526{
3527 if (ident->sym_define != 0) {
3528 struct macro *macro;
3529 struct macro_arg *arg, *anext;
3530 macro = ident->sym_define;
3531 ident->sym_define = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003532
Eric Biederman90089602004-05-28 14:11:54 +00003533 /* Free the macro arguments... */
3534 anext = macro->args;
3535 while(anext) {
3536 arg = anext;
3537 anext = arg->next;
3538 xfree(arg);
3539 }
3540
3541 /* Free the macro buffer */
3542 xfree(macro->buf);
3543
3544 /* Now free the macro itself */
3545 xfree(macro);
3546 }
3547}
3548
Stefan Reinauer14e22772010-04-27 06:56:47 +00003549static void do_define_macro(struct compile_state *state,
3550 struct hash_entry *ident, const char *body,
Eric Biedermancb364952004-11-15 10:46:44 +00003551 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003552{
3553 struct macro *macro;
3554 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003555 size_t body_len;
3556
3557 /* Find the length of the body */
3558 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003559 macro = ident->sym_define;
3560 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003561 int identical_bodies, identical_args;
3562 struct macro_arg *oarg;
3563 /* Explicitly allow identical redfinitions of the same macro */
Stefan Reinauer14e22772010-04-27 06:56:47 +00003564 identical_bodies =
Eric Biedermancb364952004-11-15 10:46:44 +00003565 (macro->buf_len == body_len) &&
3566 (memcmp(macro->buf, body, body_len) == 0);
3567 identical_args = macro->argc == argc;
3568 oarg = macro->args;
3569 arg = args;
3570 while(identical_args && arg) {
3571 identical_args = oarg->ident == arg->ident;
3572 arg = arg->next;
3573 oarg = oarg->next;
3574 }
3575 if (identical_bodies && identical_args) {
3576 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003577 return;
3578 }
3579 error(state, 0, "macro %s already defined\n", ident->name);
3580 }
3581#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003582 fprintf(state->errout, "#define %s: `%*.*s'\n",
3583 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003584#endif
3585 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003586 macro->ident = ident;
3587 macro->buf = body;
3588 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003589 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003590 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003591
3592 ident->sym_define = macro;
3593}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003594
Eric Biedermancb364952004-11-15 10:46:44 +00003595static void define_macro(
3596 struct compile_state *state,
3597 struct hash_entry *ident,
3598 const char *body, int body_len,
3599 int argc, struct macro_arg *args)
3600{
3601 char *buf;
3602 buf = xmalloc(body_len + 1, "macro buf");
3603 memcpy(buf, body, body_len);
3604 buf[body_len] = '\0';
3605 do_define_macro(state, ident, buf, argc, args);
3606}
Eric Biederman90089602004-05-28 14:11:54 +00003607
3608static void register_builtin_macro(struct compile_state *state,
3609 const char *name, const char *value)
3610{
3611 struct hash_entry *ident;
3612
3613 if (value[0] == '(') {
3614 internal_error(state, 0, "Builtin macros with arguments not supported");
3615 }
3616 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003617 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003618}
3619
3620static void register_builtin_macros(struct compile_state *state)
3621{
3622 char buf[30];
3623 char scratch[30];
3624 time_t now;
3625 struct tm *tm;
3626 now = time(NULL);
3627 tm = localtime(&now);
3628
3629 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3630 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3631 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3632 register_builtin_macro(state, "__LINE__", "54321");
3633
3634 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3635 sprintf(buf, "\"%s\"", scratch);
3636 register_builtin_macro(state, "__DATE__", buf);
3637
3638 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3639 sprintf(buf, "\"%s\"", scratch);
3640 register_builtin_macro(state, "__TIME__", buf);
3641
3642 /* I can't be a conforming implementation of C :( */
3643 register_builtin_macro(state, "__STDC__", "0");
3644 /* In particular I don't conform to C99 */
3645 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
Stefan Reinauer14e22772010-04-27 06:56:47 +00003646
Eric Biederman90089602004-05-28 14:11:54 +00003647}
3648
3649static void process_cmdline_macros(struct compile_state *state)
3650{
3651 const char **macro, *name;
3652 struct hash_entry *ident;
3653 for(macro = state->compiler->defines; (name = *macro); macro++) {
3654 const char *body;
3655 size_t name_len;
3656
3657 name_len = strlen(name);
3658 body = strchr(name, '=');
3659 if (!body) {
3660 body = "\0";
3661 } else {
3662 name_len = body - name;
3663 body++;
3664 }
3665 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003666 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003667 }
3668 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3669 ident = lookup(state, name, strlen(name));
3670 undef_macro(state, ident);
3671 }
3672}
3673
Eric Biedermanb138ac82003-04-22 18:44:01 +00003674static int spacep(int c)
3675{
3676 int ret = 0;
3677 switch(c) {
3678 case ' ':
3679 case '\t':
3680 case '\f':
3681 case '\v':
3682 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003683 ret = 1;
3684 break;
3685 }
3686 return ret;
3687}
3688
Eric Biedermanb138ac82003-04-22 18:44:01 +00003689static int digitp(int c)
3690{
3691 int ret = 0;
3692 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003693 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003694 case '5': case '6': case '7': case '8': case '9':
3695 ret = 1;
3696 break;
3697 }
3698 return ret;
3699}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003700static int digval(int c)
3701{
3702 int val = -1;
3703 if ((c >= '0') && (c <= '9')) {
3704 val = c - '0';
3705 }
3706 return val;
3707}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003708
3709static int hexdigitp(int c)
3710{
3711 int ret = 0;
3712 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003713 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003714 case '5': case '6': case '7': case '8': case '9':
3715 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3716 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3717 ret = 1;
3718 break;
3719 }
3720 return ret;
3721}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003722static int hexdigval(int c)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003723{
3724 int val = -1;
3725 if ((c >= '0') && (c <= '9')) {
3726 val = c - '0';
3727 }
3728 else if ((c >= 'A') && (c <= 'F')) {
3729 val = 10 + (c - 'A');
3730 }
3731 else if ((c >= 'a') && (c <= 'f')) {
3732 val = 10 + (c - 'a');
3733 }
3734 return val;
3735}
3736
3737static int octdigitp(int c)
3738{
3739 int ret = 0;
3740 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003741 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003742 case '4': case '5': case '6': case '7':
3743 ret = 1;
3744 break;
3745 }
3746 return ret;
3747}
3748static int octdigval(int c)
3749{
3750 int val = -1;
3751 if ((c >= '0') && (c <= '7')) {
3752 val = c - '0';
3753 }
3754 return val;
3755}
3756
3757static int letterp(int c)
3758{
3759 int ret = 0;
3760 switch(c) {
3761 case 'a': case 'b': case 'c': case 'd': case 'e':
3762 case 'f': case 'g': case 'h': case 'i': case 'j':
3763 case 'k': case 'l': case 'm': case 'n': case 'o':
3764 case 'p': case 'q': case 'r': case 's': case 't':
3765 case 'u': case 'v': case 'w': case 'x': case 'y':
3766 case 'z':
3767 case 'A': case 'B': case 'C': case 'D': case 'E':
3768 case 'F': case 'G': case 'H': case 'I': case 'J':
3769 case 'K': case 'L': case 'M': case 'N': case 'O':
3770 case 'P': case 'Q': case 'R': case 'S': case 'T':
3771 case 'U': case 'V': case 'W': case 'X': case 'Y':
3772 case 'Z':
3773 case '_':
3774 ret = 1;
3775 break;
3776 }
3777 return ret;
3778}
3779
Eric Biederman90089602004-05-28 14:11:54 +00003780static const char *identifier(const char *str, const char *end)
3781{
3782 if (letterp(*str)) {
3783 for(; str < end; str++) {
3784 int c;
3785 c = *str;
3786 if (!letterp(c) && !digitp(c)) {
3787 break;
3788 }
3789 }
3790 }
3791 return str;
3792}
3793
Eric Biedermanb138ac82003-04-22 18:44:01 +00003794static int char_value(struct compile_state *state,
3795 const signed char **strp, const signed char *end)
3796{
3797 const signed char *str;
3798 int c;
3799 str = *strp;
3800 c = *str++;
3801 if ((c == '\\') && (str < end)) {
3802 switch(*str) {
3803 case 'n': c = '\n'; str++; break;
3804 case 't': c = '\t'; str++; break;
3805 case 'v': c = '\v'; str++; break;
3806 case 'b': c = '\b'; str++; break;
3807 case 'r': c = '\r'; str++; break;
3808 case 'f': c = '\f'; str++; break;
3809 case 'a': c = '\a'; str++; break;
3810 case '\\': c = '\\'; str++; break;
3811 case '?': c = '?'; str++; break;
3812 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003813 case '"': c = '"'; str++; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003814 case 'x':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003815 c = 0;
3816 str++;
3817 while((str < end) && hexdigitp(*str)) {
3818 c <<= 4;
3819 c += hexdigval(*str);
3820 str++;
3821 }
3822 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003823 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003824 case '4': case '5': case '6': case '7':
3825 c = 0;
3826 while((str < end) && octdigitp(*str)) {
3827 c <<= 3;
3828 c += octdigval(*str);
3829 str++;
3830 }
3831 break;
3832 default:
3833 error(state, 0, "Invalid character constant");
3834 break;
3835 }
3836 }
3837 *strp = str;
3838 return c;
3839}
3840
Eric Biedermancb364952004-11-15 10:46:44 +00003841static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003842{
Eric Biedermancb364952004-11-15 10:46:44 +00003843 const char *end = file->buf + file->size;
3844 while(pos < end) {
3845 /* Lookup the character */
3846 int size = 1;
3847 int c = *pos;
3848 /* Is this a trigraph? */
3849 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003850 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003851 {
3852 switch(pos[2]) {
3853 case '=': c = '#'; break;
3854 case '/': c = '\\'; break;
3855 case '\'': c = '^'; break;
3856 case '(': c = '['; break;
3857 case ')': c = ']'; break;
3858 case '!': c = '!'; break;
3859 case '<': c = '{'; break;
3860 case '>': c = '}'; break;
3861 case '-': c = '~'; break;
3862 }
3863 if (c != '?') {
3864 size = 3;
3865 }
3866 }
3867 /* Is this an escaped newline? */
3868 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003869 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003870 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003871 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003872 /* At the start of a line just eat it */
3873 if (pos == file->pos) {
3874 file->line++;
3875 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003876 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003877 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003878 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003879 }
3880 /* Do I need to ga any farther? */
3881 else if (index == 0) {
3882 break;
3883 }
3884 /* Process a normal character */
3885 else {
3886 pos += size;
3887 index -= 1;
3888 }
3889 }
3890 return pos;
3891}
3892
3893static int get_char(struct file_state *file, const char *pos)
3894{
3895 const char *end = file->buf + file->size;
3896 int c;
3897 c = -1;
3898 pos = next_char(file, pos, 0);
3899 if (pos < end) {
3900 /* Lookup the character */
3901 c = *pos;
3902 /* If it is a trigraph get the trigraph value */
3903 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003904 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003905 {
3906 switch(pos[2]) {
3907 case '=': c = '#'; break;
3908 case '/': c = '\\'; break;
3909 case '\'': c = '^'; break;
3910 case '(': c = '['; break;
3911 case ')': c = ']'; break;
3912 case '!': c = '!'; break;
3913 case '<': c = '{'; break;
3914 case '>': c = '}'; break;
3915 case '-': c = '~'; break;
3916 }
3917 }
3918 }
3919 return c;
3920}
3921
3922static void eat_chars(struct file_state *file, const char *targ)
3923{
3924 const char *pos = file->pos;
3925 while(pos < targ) {
3926 /* Do we have a newline? */
3927 if (pos[0] == '\n') {
3928 file->line++;
3929 file->report_line++;
3930 file->line_start = pos + 1;
3931 }
3932 pos++;
3933 }
3934 file->pos = pos;
3935}
3936
3937
3938static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3939{
3940 size_t len;
3941 len = 0;
3942 while(src < end) {
3943 src = next_char(file, src, 1);
3944 len++;
3945 }
3946 return len;
3947}
3948
Stefan Reinauer14e22772010-04-27 06:56:47 +00003949static void char_strcpy(char *dest,
Eric Biedermancb364952004-11-15 10:46:44 +00003950 struct file_state *file, const char *src, const char *end)
3951{
3952 while(src < end) {
3953 int c;
3954 c = get_char(file, src);
3955 src = next_char(file, src, 1);
3956 *dest++ = c;
3957 }
3958}
3959
Stefan Reinauer14e22772010-04-27 06:56:47 +00003960static char *char_strdup(struct file_state *file,
Eric Biedermancb364952004-11-15 10:46:44 +00003961 const char *start, const char *end, const char *id)
3962{
3963 char *str;
3964 size_t str_len;
3965 str_len = char_strlen(file, start, end);
3966 str = xcmalloc(str_len + 1, id);
3967 char_strcpy(str, file, start, end);
3968 str[str_len] = '\0';
3969 return str;
3970}
3971
3972static const char *after_digits(struct file_state *file, const char *ptr)
3973{
3974 while(digitp(get_char(file, ptr))) {
3975 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003976 }
3977 return ptr;
3978}
3979
Eric Biedermancb364952004-11-15 10:46:44 +00003980static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003981{
Eric Biedermancb364952004-11-15 10:46:44 +00003982 while(octdigitp(get_char(file, ptr))) {
3983 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003984 }
3985 return ptr;
3986}
3987
Eric Biedermancb364952004-11-15 10:46:44 +00003988static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003989{
Eric Biedermancb364952004-11-15 10:46:44 +00003990 while(hexdigitp(get_char(file, ptr))) {
3991 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003992 }
3993 return ptr;
3994}
3995
Eric Biedermancb364952004-11-15 10:46:44 +00003996static const char *after_alnums(struct file_state *file, const char *ptr)
3997{
3998 int c;
3999 c = get_char(file, ptr);
4000 while(letterp(c) || digitp(c)) {
4001 ptr = next_char(file, ptr, 1);
4002 c = get_char(file, ptr);
4003 }
4004 return ptr;
4005}
4006
4007static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004008 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004009{
4010 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004011
Eric Biedermanb138ac82003-04-22 18:44:01 +00004012 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004013 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004014
4015 /* Store the copy in the token */
4016 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004017 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004018}
4019
Stefan Reinauer14e22772010-04-27 06:56:47 +00004020static void raw_next_token(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004021 struct file_state *file, struct token *tk)
4022{
4023 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004024 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004025 const char *tokp;
4026 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004027 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004028
Eric Biedermanb138ac82003-04-22 18:44:01 +00004029 tk->str_len = 0;
4030 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004031 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004032 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004033 c = get_char(file, tokp);
4034 tokp = next_char(file, tokp, 1);
4035 eat = 0;
4036 c1 = get_char(file, tokp);
4037 c2 = get_char(file, next_char(file, tokp, 1));
4038 c3 = get_char(file, next_char(file, tokp, 2));
4039
4040 /* The end of the file */
4041 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004042 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004044 /* Whitespace */
4045 else if (spacep(c)) {
4046 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004047 while (spacep(get_char(file, tokp))) {
4048 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004049 }
4050 }
4051 /* EOL Comments */
4052 else if ((c == '/') && (c1 == '/')) {
4053 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004054 tokp = next_char(file, tokp, 1);
4055 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004056 /* Advance to the next character only after we verify
Stefan Reinauer14e22772010-04-27 06:56:47 +00004057 * the current character is not a newline.
Eric Biederman57183382006-12-02 16:48:48 +00004058 * EOL is special to the preprocessor so we don't
4059 * want to loose any.
4060 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004061 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004062 break;
4063 }
Eric Biederman57183382006-12-02 16:48:48 +00004064 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004065 }
4066 }
4067 /* Comments */
4068 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004069 tokp = next_char(file, tokp, 2);
4070 c = c2;
4071 while((c1 = get_char(file, tokp)) != -1) {
4072 tokp = next_char(file, tokp, 1);
4073 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004074 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004075 break;
4076 }
Eric Biedermancb364952004-11-15 10:46:44 +00004077 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004078 }
4079 if (tok == TOK_UNKNOWN) {
4080 error(state, 0, "unterminated comment");
4081 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004082 }
4083 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004084 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004085 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004086
Eric Biedermancb364952004-11-15 10:46:44 +00004087 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004088 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004089 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004090 }
Eric Biedermancb364952004-11-15 10:46:44 +00004091 while((c = get_char(file, tokp)) != -1) {
4092 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004093 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004094 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004095 }
Eric Biedermancb364952004-11-15 10:46:44 +00004096 else if (c == '\\') {
4097 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004098 }
4099 else if (c == '"') {
4100 tok = TOK_LIT_STRING;
4101 break;
4102 }
4103 }
4104 if (tok == TOK_UNKNOWN) {
4105 error(state, 0, "unterminated string constant");
4106 }
Eric Biedermancb364952004-11-15 10:46:44 +00004107 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004108 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004110
4111 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004112 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004113 }
4114 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004115 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004116 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004117
Eric Biedermancb364952004-11-15 10:46:44 +00004118 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004119 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004120 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004121 }
Eric Biedermancb364952004-11-15 10:46:44 +00004122 while((c = get_char(file, tokp)) != -1) {
4123 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004124 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004125 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004126 }
Eric Biedermancb364952004-11-15 10:46:44 +00004127 else if (c == '\\') {
4128 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004129 }
4130 else if (c == '\'') {
4131 tok = TOK_LIT_CHAR;
4132 break;
4133 }
4134 }
4135 if (tok == TOK_UNKNOWN) {
4136 error(state, 0, "unterminated character constant");
4137 }
Eric Biedermancb364952004-11-15 10:46:44 +00004138 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004139 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004140 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004141
4142 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004143 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004144 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004145 /* integer and floating constants
Eric Biedermanb138ac82003-04-22 18:44:01 +00004146 * Integer Constants
4147 * {digits}
4148 * 0[Xx]{hexdigits}
4149 * 0{octdigit}+
Stefan Reinauer14e22772010-04-27 06:56:47 +00004150 *
Eric Biedermanb138ac82003-04-22 18:44:01 +00004151 * Floating constants
4152 * {digits}.{digits}[Ee][+-]?{digits}
4153 * {digits}.{digits}
4154 * {digits}[Ee][+-]?{digits}
4155 * .{digits}[Ee][+-]?{digits}
4156 * .{digits}
4157 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004158 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004159 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004160 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004161 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004162 is_float = 0;
4163 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004164 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004165 }
4166 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004167 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004168 }
Eric Biedermancb364952004-11-15 10:46:44 +00004169 cn = get_char(file, next);
4170 if (cn == '.') {
4171 next = next_char(file, next, 1);
4172 next = after_digits(file, next);
4173 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004174 }
Eric Biedermancb364952004-11-15 10:46:44 +00004175 cn = get_char(file, next);
4176 if ((cn == 'e') || (cn == 'E')) {
4177 const char *new;
4178 next = next_char(file, next, 1);
4179 cn = get_char(file, next);
4180 if ((cn == '+') || (cn == '-')) {
4181 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004182 }
Eric Biedermancb364952004-11-15 10:46:44 +00004183 new = after_digits(file, next);
4184 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004185 next = new;
4186 }
4187 if (is_float) {
4188 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004189 cn = get_char(file, next);
4190 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4191 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004192 }
4193 }
4194 if (!is_float && digitp(c)) {
4195 tok = TOK_LIT_INT;
4196 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004197 next = next_char(file, tokp, 1);
4198 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004199 }
4200 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004201 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004202 }
4203 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004204 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004205 }
4206 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004207 cn = get_char(file, next);
4208 if ((cn == 'u') || (cn == 'U')) {
4209 next = next_char(file, next, 1);
4210 cn = get_char(file, next);
4211 if ((cn == 'l') || (cn == 'L')) {
4212 next = next_char(file, next, 1);
4213 cn = get_char(file, next);
4214 }
4215 if ((cn == 'l') || (cn == 'L')) {
4216 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004217 }
4218 }
Eric Biedermancb364952004-11-15 10:46:44 +00004219 else if ((cn == 'l') || (cn == 'L')) {
4220 next = next_char(file, next, 1);
4221 cn = get_char(file, next);
4222 if ((cn == 'l') || (cn == 'L')) {
4223 next = next_char(file, next, 1);
4224 cn = get_char(file, next);
4225 }
4226 if ((cn == 'u') || (cn == 'U')) {
4227 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004228 }
4229 }
4230 }
Eric Biedermancb364952004-11-15 10:46:44 +00004231 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004232
4233 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004234 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004235 }
4236 /* identifiers */
4237 else if (letterp(c)) {
4238 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004239
4240 /* Find and save the identifier string */
4241 tokp = after_alnums(file, tokp);
4242 save_string(file, tk, token, tokp, "identifier");
4243
4244 /* Look up to see which identifier it is */
4245 tk->ident = lookup(state, tk->val.str, tk->str_len);
4246
4247 /* Free the identifier string */
4248 tk->str_len = 0;
4249 xfree(tk->val.str);
4250
Eric Biederman90089602004-05-28 14:11:54 +00004251 /* See if this identifier can be macro expanded */
4252 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004253 c = get_char(file, tokp);
4254 if (c == '$') {
4255 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004256 tk->val.notmacro = 1;
4257 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004258 }
4259 /* C99 alternate macro characters */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004260 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004261 eat += 3;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004262 tok = TOK_CONCATENATE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004263 }
Eric Biedermancb364952004-11-15 10:46:44 +00004264 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4265 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4266 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4267 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4268 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4269 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4270 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4271 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4272 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4273 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4274 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4275 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4276 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4277 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4278 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4279 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4280 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4281 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4282 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4283 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4284 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4285 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4286 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4287 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4288 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4289 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4290 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4291 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004292 else if (c == ';') { tok = TOK_SEMI; }
4293 else if (c == '{') { tok = TOK_LBRACE; }
4294 else if (c == '}') { tok = TOK_RBRACE; }
4295 else if (c == ',') { tok = TOK_COMMA; }
4296 else if (c == '=') { tok = TOK_EQ; }
4297 else if (c == ':') { tok = TOK_COLON; }
4298 else if (c == '[') { tok = TOK_LBRACKET; }
4299 else if (c == ']') { tok = TOK_RBRACKET; }
4300 else if (c == '(') { tok = TOK_LPAREN; }
4301 else if (c == ')') { tok = TOK_RPAREN; }
4302 else if (c == '*') { tok = TOK_STAR; }
4303 else if (c == '>') { tok = TOK_MORE; }
4304 else if (c == '<') { tok = TOK_LESS; }
4305 else if (c == '?') { tok = TOK_QUEST; }
4306 else if (c == '|') { tok = TOK_OR; }
4307 else if (c == '&') { tok = TOK_AND; }
4308 else if (c == '^') { tok = TOK_XOR; }
4309 else if (c == '+') { tok = TOK_PLUS; }
4310 else if (c == '-') { tok = TOK_MINUS; }
4311 else if (c == '/') { tok = TOK_DIV; }
4312 else if (c == '%') { tok = TOK_MOD; }
4313 else if (c == '!') { tok = TOK_BANG; }
4314 else if (c == '.') { tok = TOK_DOT; }
4315 else if (c == '~') { tok = TOK_TILDE; }
4316 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004317 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004318
Eric Biedermancb364952004-11-15 10:46:44 +00004319 tokp = next_char(file, tokp, eat);
4320 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004321 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004322 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004323}
4324
4325static void check_tok(struct compile_state *state, struct token *tk, int tok)
4326{
4327 if (tk->tok != tok) {
4328 const char *name1, *name2;
4329 name1 = tokens[tk->tok];
4330 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004331 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004332 name2 = tk->ident->name;
4333 }
4334 error(state, 0, "\tfound %s %s expected %s",
4335 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004336 }
4337}
4338
Eric Biederman90089602004-05-28 14:11:54 +00004339struct macro_arg_value {
4340 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004341 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004342 size_t len;
4343};
4344static struct macro_arg_value *read_macro_args(
Stefan Reinauer14e22772010-04-27 06:56:47 +00004345 struct compile_state *state, struct macro *macro,
Eric Biederman90089602004-05-28 14:11:54 +00004346 struct file_state *file, struct token *tk)
4347{
4348 struct macro_arg_value *argv;
4349 struct macro_arg *arg;
4350 int paren_depth;
4351 int i;
4352
4353 if (macro->argc == 0) {
4354 do {
4355 raw_next_token(state, file, tk);
4356 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004357 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004358 }
4359 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4360 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4361 argv[i].value = 0;
4362 argv[i].len = 0;
4363 argv[i].ident = arg->ident;
4364 }
4365 paren_depth = 0;
4366 i = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004367
Eric Biederman90089602004-05-28 14:11:54 +00004368 for(;;) {
4369 const char *start;
4370 size_t len;
4371 start = file->pos;
4372 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004373
Eric Biederman90089602004-05-28 14:11:54 +00004374 if (!paren_depth && (tk->tok == TOK_COMMA) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004375 (argv[i].ident != state->i___VA_ARGS__))
Eric Biederman90089602004-05-28 14:11:54 +00004376 {
4377 i++;
4378 if (i >= macro->argc) {
4379 error(state, 0, "too many args to %s\n",
4380 macro->ident->name);
4381 }
4382 continue;
4383 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004384
Eric Biederman90089602004-05-28 14:11:54 +00004385 if (tk->tok == TOK_LPAREN) {
4386 paren_depth++;
4387 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004388
Eric Biederman90089602004-05-28 14:11:54 +00004389 if (tk->tok == TOK_RPAREN) {
4390 if (paren_depth == 0) {
4391 break;
4392 }
4393 paren_depth--;
4394 }
4395 if (tk->tok == TOK_EOF) {
4396 error(state, 0, "End of file encountered while parsing macro arguments");
4397 }
Eric Biedermancb364952004-11-15 10:46:44 +00004398
4399 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004400 argv[i].value = xrealloc(
4401 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004402 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004403 argv[i].len += len;
4404 }
4405 if (i != macro->argc -1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00004406 error(state, 0, "missing %s arg %d\n",
Eric Biederman90089602004-05-28 14:11:54 +00004407 macro->ident->name, i +2);
4408 }
4409 return argv;
4410}
4411
4412
4413static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4414{
4415 int i;
4416 for(i = 0; i < macro->argc; i++) {
4417 xfree(argv[i].value);
4418 }
4419 xfree(argv);
4420}
4421
4422struct macro_buf {
4423 char *str;
4424 size_t len, pos;
4425};
4426
Eric Biedermancb364952004-11-15 10:46:44 +00004427static void grow_macro_buf(struct compile_state *state,
4428 const char *id, struct macro_buf *buf,
4429 size_t grow)
4430{
4431 if ((buf->pos + grow) >= buf->len) {
4432 buf->str = xrealloc(buf->str, buf->len + grow, id);
4433 buf->len += grow;
4434 }
4435}
4436
Eric Biederman90089602004-05-28 14:11:54 +00004437static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004438 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004439 const char *fstart, size_t flen)
4440{
Eric Biedermancb364952004-11-15 10:46:44 +00004441 grow_macro_buf(state, id, buf, flen);
4442 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004443#if 0
4444 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4445 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004446 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004447#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004448 buf->pos += flen;
4449}
4450
4451
4452static void append_macro_chars(struct compile_state *state,
4453 const char *id, struct macro_buf *buf,
4454 struct file_state *file, const char *start, const char *end)
4455{
4456 size_t flen;
4457 flen = char_strlen(file, start, end);
4458 grow_macro_buf(state, id, buf, flen);
4459 char_strcpy(buf->str + buf->pos, file, start, end);
4460#if 0
4461 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4462 buf->pos, buf->pos, buf->str,
4463 flen, flen, buf->str + buf->pos);
4464#endif
Eric Biederman90089602004-05-28 14:11:54 +00004465 buf->pos += flen;
4466}
4467
Stefan Reinauer14e22772010-04-27 06:56:47 +00004468static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004469 struct file_state **filep, struct token *tk);
4470
Stefan Reinauer14e22772010-04-27 06:56:47 +00004471static void macro_expand_args(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004472 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4473{
Eric Biedermancb364952004-11-15 10:46:44 +00004474 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004475
Eric Biederman90089602004-05-28 14:11:54 +00004476 for(i = 0; i < macro->argc; i++) {
4477 struct file_state fmacro, *file;
4478 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004479
Eric Biedermancb364952004-11-15 10:46:44 +00004480 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004481 fmacro.basename = argv[i].ident->name;
4482 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004483 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004484 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004485 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004486 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004487 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004488 fmacro.report_line = 1;
4489 fmacro.report_name = fmacro.basename;
4490 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004491 fmacro.macro = 1;
4492 fmacro.trigraphs = 0;
4493 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004494
4495 buf.len = argv[i].len;
4496 buf.str = xmalloc(buf.len, argv[i].ident->name);
4497 buf.pos = 0;
4498
4499 file = &fmacro;
4500 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004501 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004502
Eric Biedermancb364952004-11-15 10:46:44 +00004503 /* If we have recursed into another macro body
4504 * get out of it.
4505 */
Eric Biederman90089602004-05-28 14:11:54 +00004506 if (tk->tok == TOK_EOF) {
4507 struct file_state *old;
4508 old = file;
4509 file = file->prev;
4510 if (!file) {
4511 break;
4512 }
4513 /* old->basename is used keep it */
4514 xfree(old->dirname);
4515 xfree(old->buf);
4516 xfree(old);
4517 continue;
4518 }
4519 else if (tk->ident && tk->ident->sym_define) {
4520 if (compile_macro(state, &file, tk)) {
4521 continue;
4522 }
4523 }
4524
Eric Biedermancb364952004-11-15 10:46:44 +00004525 append_macro_chars(state, macro->ident->name, &buf,
4526 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004527 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004528
Eric Biederman90089602004-05-28 14:11:54 +00004529 xfree(argv[i].value);
4530 argv[i].value = buf.str;
4531 argv[i].len = buf.pos;
4532 }
4533 return;
4534}
4535
4536static void expand_macro(struct compile_state *state,
4537 struct macro *macro, struct macro_buf *buf,
4538 struct macro_arg_value *argv, struct token *tk)
4539{
4540 struct file_state fmacro;
4541 const char space[] = " ";
4542 const char *fstart;
4543 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004544 int i, j;
4545
4546 /* Place the macro body in a dummy file */
4547 fmacro.prev = 0;
4548 fmacro.basename = macro->ident->name;
4549 fmacro.dirname = "";
4550 fmacro.buf = macro->buf;
4551 fmacro.size = macro->buf_len;
4552 fmacro.pos = fmacro.buf;
4553 fmacro.line = 1;
4554 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004555 fmacro.report_line = 1;
4556 fmacro.report_name = fmacro.basename;
4557 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004558 fmacro.macro = 1;
4559 fmacro.trigraphs = 0;
4560 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004561
Eric Biedermancb364952004-11-15 10:46:44 +00004562 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004563 buf->len = macro->buf_len + 3;
4564 buf->str = xmalloc(buf->len, macro->ident->name);
4565 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004566
Eric Biederman90089602004-05-28 14:11:54 +00004567 fstart = fmacro.pos;
4568 raw_next_token(state, &fmacro, tk);
4569 while(tk->tok != TOK_EOF) {
4570 flen = fmacro.pos - fstart;
4571 switch(tk->tok) {
4572 case TOK_IDENT:
4573 for(i = 0; i < macro->argc; i++) {
4574 if (argv[i].ident == tk->ident) {
4575 break;
4576 }
4577 }
4578 if (i >= macro->argc) {
4579 break;
4580 }
4581 /* Substitute macro parameter */
4582 fstart = argv[i].value;
4583 flen = argv[i].len;
4584 break;
4585 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004586 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004587 break;
4588 }
4589 do {
4590 raw_next_token(state, &fmacro, tk);
4591 } while(tk->tok == TOK_SPACE);
4592 check_tok(state, tk, TOK_IDENT);
4593 for(i = 0; i < macro->argc; i++) {
4594 if (argv[i].ident == tk->ident) {
4595 break;
4596 }
4597 }
4598 if (i >= macro->argc) {
4599 error(state, 0, "parameter `%s' not found",
4600 tk->ident->name);
4601 }
4602 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004603 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004604 for(j = 0; j < argv[i].len; j++) {
4605 char *str = argv[i].value + j;
4606 size_t len = 1;
4607 if (*str == '\\') {
4608 str = "\\";
4609 len = 2;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004610 }
Eric Biederman90089602004-05-28 14:11:54 +00004611 else if (*str == '"') {
4612 str = "\\\"";
4613 len = 2;
4614 }
Eric Biedermancb364952004-11-15 10:46:44 +00004615 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004616 }
Eric Biedermancb364952004-11-15 10:46:44 +00004617 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004618 fstart = 0;
4619 flen = 0;
4620 break;
4621 case TOK_CONCATENATE:
4622 /* Concatenate tokens */
4623 /* Delete the previous whitespace token */
4624 if (buf->str[buf->pos - 1] == ' ') {
4625 buf->pos -= 1;
4626 }
4627 /* Skip the next sequence of whitspace tokens */
4628 do {
4629 fstart = fmacro.pos;
4630 raw_next_token(state, &fmacro, tk);
4631 } while(tk->tok == TOK_SPACE);
4632 /* Restart at the top of the loop.
4633 * I need to process the non white space token.
4634 */
4635 continue;
4636 break;
4637 case TOK_SPACE:
4638 /* Collapse multiple spaces into one */
4639 if (buf->str[buf->pos - 1] != ' ') {
4640 fstart = space;
4641 flen = 1;
4642 } else {
4643 fstart = 0;
4644 flen = 0;
4645 }
4646 break;
4647 default:
4648 break;
4649 }
4650
Eric Biedermancb364952004-11-15 10:46:44 +00004651 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004652
Eric Biederman90089602004-05-28 14:11:54 +00004653 fstart = fmacro.pos;
4654 raw_next_token(state, &fmacro, tk);
4655 }
4656}
4657
4658static void tag_macro_name(struct compile_state *state,
4659 struct macro *macro, struct macro_buf *buf,
4660 struct token *tk)
4661{
4662 /* Guard all instances of the macro name in the replacement
4663 * text from further macro expansion.
4664 */
4665 struct file_state fmacro;
4666 const char *fstart;
4667 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004668
4669 /* Put the old macro expansion buffer in a file */
4670 fmacro.prev = 0;
4671 fmacro.basename = macro->ident->name;
4672 fmacro.dirname = "";
4673 fmacro.buf = buf->str;
4674 fmacro.size = buf->pos;
4675 fmacro.pos = fmacro.buf;
4676 fmacro.line = 1;
4677 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004678 fmacro.report_line = 1;
4679 fmacro.report_name = fmacro.basename;
4680 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004681 fmacro.macro = 1;
4682 fmacro.trigraphs = 0;
4683 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004684
Eric Biedermancb364952004-11-15 10:46:44 +00004685 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004686 buf->len = macro->buf_len + 3;
4687 buf->str = xmalloc(buf->len, macro->ident->name);
4688 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004689
Eric Biederman90089602004-05-28 14:11:54 +00004690 fstart = fmacro.pos;
4691 raw_next_token(state, &fmacro, tk);
4692 while(tk->tok != TOK_EOF) {
4693 flen = fmacro.pos - fstart;
4694 if ((tk->tok == TOK_IDENT) &&
4695 (tk->ident == macro->ident) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004696 (tk->val.notmacro == 0))
Eric Biedermancb364952004-11-15 10:46:44 +00004697 {
4698 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004699 fstart = "$";
4700 flen = 1;
4701 }
4702
Eric Biedermancb364952004-11-15 10:46:44 +00004703 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004704
Eric Biederman90089602004-05-28 14:11:54 +00004705 fstart = fmacro.pos;
4706 raw_next_token(state, &fmacro, tk);
4707 }
4708 xfree(fmacro.buf);
4709}
Eric Biedermancb364952004-11-15 10:46:44 +00004710
Stefan Reinauer14e22772010-04-27 06:56:47 +00004711static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004712 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004713{
4714 struct file_state *file;
4715 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004716 struct macro *macro;
4717 struct macro_arg_value *argv;
4718 struct macro_buf buf;
4719
4720#if 0
4721 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4722#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004723 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004724 macro = ident->sym_define;
4725
4726 /* If this token comes from a macro expansion ignore it */
4727 if (tk->val.notmacro) {
4728 return 0;
4729 }
4730 /* If I am a function like macro and the identifier is not followed
4731 * by a left parenthesis, do nothing.
4732 */
Eric Biedermancb364952004-11-15 10:46:44 +00004733 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004734 return 0;
4735 }
4736
4737 /* Read in the macro arguments */
4738 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004739 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004740 raw_next_token(state, *filep, tk);
4741 check_tok(state, tk, TOK_LPAREN);
4742
4743 argv = read_macro_args(state, macro, *filep, tk);
4744
4745 check_tok(state, tk, TOK_RPAREN);
4746 }
4747 /* Macro expand the macro arguments */
4748 macro_expand_args(state, macro, argv, tk);
4749
4750 buf.str = 0;
4751 buf.len = 0;
4752 buf.pos = 0;
4753 if (ident == state->i___FILE__) {
4754 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4755 buf.str = xmalloc(buf.len, ident->name);
4756 sprintf(buf.str, "\"%s\"", state->file->basename);
4757 buf.pos = strlen(buf.str);
4758 }
4759 else if (ident == state->i___LINE__) {
4760 buf.len = 30;
4761 buf.str = xmalloc(buf.len, ident->name);
4762 sprintf(buf.str, "%d", state->file->line);
4763 buf.pos = strlen(buf.str);
4764 }
4765 else {
4766 expand_macro(state, macro, &buf, argv, tk);
4767 }
4768 /* Tag the macro name with a $ so it will no longer
4769 * be regonized as a canidate for macro expansion.
4770 */
4771 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004772
4773#if 0
4774 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4775 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4776#endif
4777
4778 free_macro_args(macro, argv);
4779
Eric Biedermanb138ac82003-04-22 18:44:01 +00004780 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004781 file->prev = *filep;
4782 file->basename = xstrdup(ident->name);
4783 file->dirname = xstrdup("");
4784 file->buf = buf.str;
4785 file->size = buf.pos;
4786 file->pos = file->buf;
4787 file->line = 1;
4788 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004789 file->report_line = 1;
4790 file->report_name = file->basename;
4791 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004792 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004793 file->trigraphs = 0;
4794 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004795 *filep = file;
4796 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004797}
4798
Eric Biederman90089602004-05-28 14:11:54 +00004799static void eat_tokens(struct compile_state *state, int targ_tok)
4800{
4801 if (state->eat_depth > 0) {
4802 internal_error(state, 0, "Already eating...");
4803 }
4804 state->eat_depth = state->if_depth;
4805 state->eat_targ = targ_tok;
4806}
4807static int if_eat(struct compile_state *state)
4808{
4809 return state->eat_depth > 0;
4810}
4811static int if_value(struct compile_state *state)
4812{
4813 int index, offset;
4814 index = state->if_depth / CHAR_BIT;
4815 offset = state->if_depth % CHAR_BIT;
4816 return !!(state->if_bytes[index] & (1 << (offset)));
4817}
Stefan Reinauer14e22772010-04-27 06:56:47 +00004818static void set_if_value(struct compile_state *state, int value)
Eric Biederman90089602004-05-28 14:11:54 +00004819{
4820 int index, offset;
4821 index = state->if_depth / CHAR_BIT;
4822 offset = state->if_depth % CHAR_BIT;
4823
4824 state->if_bytes[index] &= ~(1 << offset);
4825 if (value) {
4826 state->if_bytes[index] |= (1 << offset);
4827 }
4828}
4829static void in_if(struct compile_state *state, const char *name)
4830{
4831 if (state->if_depth <= 0) {
4832 error(state, 0, "%s without #if", name);
4833 }
4834}
4835static void enter_if(struct compile_state *state)
4836{
4837 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004838 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004839 error(state, 0, "#if depth too great");
4840 }
4841}
4842static void reenter_if(struct compile_state *state, const char *name)
4843{
4844 in_if(state, name);
4845 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004846 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004847 state->eat_depth = 0;
4848 state->eat_targ = 0;
4849 }
4850}
4851static void enter_else(struct compile_state *state, const char *name)
4852{
4853 in_if(state, name);
4854 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004855 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004856 state->eat_depth = 0;
4857 state->eat_targ = 0;
4858 }
4859}
4860static void exit_if(struct compile_state *state, const char *name)
4861{
4862 in_if(state, name);
4863 if (state->eat_depth == state->if_depth) {
4864 state->eat_depth = 0;
4865 state->eat_targ = 0;
4866 }
4867 state->if_depth -= 1;
4868}
4869
Eric Biedermancb364952004-11-15 10:46:44 +00004870static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004871{
4872 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004873 int rescan;
4874
Eric Biedermancb364952004-11-15 10:46:44 +00004875 file = state->file;
4876 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004877 do {
4878 rescan = 0;
4879 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004880 /* Exit out of an include directive or macro call */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004881 if ((tk->tok == TOK_EOF) &&
4882 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004883 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004884 state->file = file->prev;
4885 /* file->basename is used keep it */
4886 xfree(file->dirname);
4887 xfree(file->buf);
4888 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004889 file = 0;
4890 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004891 rescan = 1;
4892 }
Eric Biederman41203d92004-11-08 09:31:09 +00004893 } while(rescan);
4894}
4895
Eric Biedermancb364952004-11-15 10:46:44 +00004896static void pp_token(struct compile_state *state, struct token *tk)
4897{
Eric Biedermancb364952004-11-15 10:46:44 +00004898 int rescan;
4899
4900 raw_token(state, tk);
4901 do {
4902 rescan = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004903 if (tk->tok == TOK_SPACE) {
4904 raw_token(state, tk);
4905 rescan = 1;
4906 }
4907 else if (tk->tok == TOK_IDENT) {
4908 if (state->token_base == 0) {
4909 ident_to_keyword(state, tk);
4910 } else {
4911 ident_to_macro(state, tk);
4912 }
4913 }
4914 } while(rescan);
4915}
4916
Eric Biederman41203d92004-11-08 09:31:09 +00004917static void preprocess(struct compile_state *state, struct token *tk);
4918
4919static void token(struct compile_state *state, struct token *tk)
4920{
4921 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004922 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004923 do {
4924 rescan = 0;
4925 /* Process a macro directive */
4926 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004927 /* Only match preprocessor directives at the start of a line */
4928 const char *ptr;
4929 ptr = state->file->line_start;
4930 while((ptr < tk->pos)
4931 && spacep(get_char(state->file, ptr)))
4932 {
4933 ptr = next_char(state->file, ptr, 1);
4934 }
4935 if (ptr == tk->pos) {
4936 preprocess(state, tk);
4937 rescan = 1;
4938 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004939 }
Eric Biederman41203d92004-11-08 09:31:09 +00004940 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004941 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004942 rescan = compile_macro(state, &state->file, tk);
4943 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004944 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004945 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004946 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004947 /* Eat tokens disabled by the preprocessor
4948 * (Unless we are parsing a preprocessor directive
Eric Biedermancb364952004-11-15 10:46:44 +00004949 */
Eric Biedermana649a412004-11-09 00:35:39 +00004950 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004951 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004952 rescan = 1;
4953 }
Eric Biedermana649a412004-11-09 00:35:39 +00004954 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004955 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004956 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004957 rescan = 1;
4958 }
Eric Biedermancb364952004-11-15 10:46:44 +00004959 /* Error on unknown tokens */
4960 else if (tk->tok == TOK_UNKNOWN) {
4961 error(state, 0, "unknown token");
4962 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004963 } while(rescan);
4964}
4965
Eric Biederman41203d92004-11-08 09:31:09 +00004966
4967static inline struct token *get_token(struct compile_state *state, int offset)
4968{
4969 int index;
4970 index = state->token_base + offset;
4971 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4972 internal_error(state, 0, "token array to small");
4973 }
4974 return &state->token[index];
4975}
4976
4977static struct token *do_eat_token(struct compile_state *state, int tok)
4978{
4979 struct token *tk;
4980 int i;
4981 check_tok(state, get_token(state, 1), tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004982
Eric Biederman41203d92004-11-08 09:31:09 +00004983 /* Free the old token value */
4984 tk = get_token(state, 0);
4985 if (tk->str_len) {
4986 memset((void *)tk->val.str, -1, tk->str_len);
4987 xfree(tk->val.str);
4988 }
4989 /* Overwrite the old token with newer tokens */
4990 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4991 state->token[i] = state->token[i + 1];
4992 }
4993 /* Clear the last token */
4994 memset(&state->token[i], 0, sizeof(state->token[i]));
4995 state->token[i].tok = -1;
4996
4997 /* Return the token */
4998 return tk;
4999}
5000
Eric Biedermancb364952004-11-15 10:46:44 +00005001static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005002{
5003 struct token *tk1;
5004 tk1 = get_token(state, 1);
5005 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005006 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005007 }
5008 return tk1->tok;
5009}
5010
Eric Biedermancb364952004-11-15 10:46:44 +00005011static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005012{
Eric Biedermancb364952004-11-15 10:46:44 +00005013 raw_peek(state);
5014 return do_eat_token(state, tok);
5015}
5016
5017static int pp_peek(struct compile_state *state)
5018{
5019 struct token *tk1;
5020 tk1 = get_token(state, 1);
5021 if (tk1->tok == -1) {
5022 pp_token(state, tk1);
5023 }
5024 return tk1->tok;
5025}
5026
5027static struct token *pp_eat(struct compile_state *state, int tok)
5028{
5029 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005030 return do_eat_token(state, tok);
5031}
5032
Eric Biedermanb138ac82003-04-22 18:44:01 +00005033static int peek(struct compile_state *state)
5034{
Eric Biederman41203d92004-11-08 09:31:09 +00005035 struct token *tk1;
5036 tk1 = get_token(state, 1);
5037 if (tk1->tok == -1) {
5038 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005039 }
Eric Biederman41203d92004-11-08 09:31:09 +00005040 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005041}
5042
5043static int peek2(struct compile_state *state)
5044{
Eric Biederman41203d92004-11-08 09:31:09 +00005045 struct token *tk1, *tk2;
5046 tk1 = get_token(state, 1);
5047 tk2 = get_token(state, 2);
5048 if (tk1->tok == -1) {
5049 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005050 }
Eric Biederman41203d92004-11-08 09:31:09 +00005051 if (tk2->tok == -1) {
5052 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005053 }
Eric Biederman41203d92004-11-08 09:31:09 +00005054 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005055}
5056
Eric Biederman41203d92004-11-08 09:31:09 +00005057static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005058{
Eric Biederman90089602004-05-28 14:11:54 +00005059 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005060 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005061}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005062
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005063static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005064{
Eric Biederman90089602004-05-28 14:11:54 +00005065 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005066 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005067 int subdir_len;
5068 struct file_state *file;
5069 char *basename;
5070 file = xmalloc(sizeof(*file), "file_state");
5071
5072 base = strrchr(filename, '/');
5073 subdir = filename;
5074 if (base != 0) {
5075 subdir_len = base - filename;
5076 base++;
5077 }
5078 else {
5079 base = filename;
5080 subdir_len = 0;
5081 }
5082 basename = xmalloc(strlen(base) +1, "basename");
5083 strcpy(basename, base);
5084 file->basename = basename;
5085
5086 if (getcwd(cwd, sizeof(cwd)) == 0) {
5087 die("cwd buffer to small");
5088 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005089 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005090 file->dirname = xmalloc(subdir_len + 1, "dirname");
5091 memcpy(file->dirname, subdir, subdir_len);
5092 file->dirname[subdir_len] = '\0';
5093 }
5094 else {
Eric Biederman90089602004-05-28 14:11:54 +00005095 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005096 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005097 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005098 /* Find the appropriate directory... */
5099 dir = 0;
5100 if (!state->file && exists(cwd, filename)) {
5101 dir = cwd;
5102 }
5103 if (local && state->file && exists(state->file->dirname, filename)) {
5104 dir = state->file->dirname;
5105 }
Eric Biederman90089602004-05-28 14:11:54 +00005106 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005107 if (exists(*path, filename)) {
5108 dir = *path;
5109 }
5110 }
5111 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005112 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005113 }
5114 dirlen = strlen(dir);
5115 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5116 memcpy(file->dirname, dir, dirlen);
5117 file->dirname[dirlen] = '/';
5118 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5119 file->dirname[dirlen + 1 + subdir_len] = '\0';
5120 }
5121 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005122
5123 file->pos = file->buf;
5124 file->line_start = file->pos;
5125 file->line = 1;
5126
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005127 file->report_line = 1;
5128 file->report_name = file->basename;
5129 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005130 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005131 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5132 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005133
Eric Biedermanb138ac82003-04-22 18:44:01 +00005134 file->prev = state->file;
5135 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005136}
5137
Eric Biederman41203d92004-11-08 09:31:09 +00005138static struct triple *constant_expr(struct compile_state *state);
5139static void integral(struct compile_state *state, struct triple *def);
5140
5141static int mcexpr(struct compile_state *state)
5142{
5143 struct triple *cvalue;
5144 cvalue = constant_expr(state);
5145 integral(state, cvalue);
5146 if (cvalue->op != OP_INTCONST) {
5147 error(state, 0, "integer constant expected");
5148 }
5149 return cvalue->u.cval != 0;
5150}
5151
5152static void preprocess(struct compile_state *state, struct token *current_token)
5153{
5154 /* Doing much more with the preprocessor would require
5155 * a parser and a major restructuring.
5156 * Postpone that for later.
5157 */
Eric Biederman41203d92004-11-08 09:31:09 +00005158 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005159 int tok;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005160
Eric Biedermancb364952004-11-15 10:46:44 +00005161 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005162
5163 old_token_base = state->token_base;
5164 state->token_base = current_token - state->token;
5165
Eric Biedermancb364952004-11-15 10:46:44 +00005166 tok = pp_peek(state);
5167 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005168 case TOK_LIT_INT:
5169 {
5170 struct token *tk;
5171 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005172 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005173 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005174 /* I have a preprocessor line marker parse it */
5175 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005176 const char *token, *base;
5177 char *name, *dir;
5178 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005179 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005180 name = xmalloc(tk->str_len, "report_name");
5181 token = tk->val.str + 1;
5182 base = strrchr(token, '/');
5183 name_len = tk->str_len -2;
5184 if (base != 0) {
5185 dir_len = base - token;
5186 base++;
5187 name_len -= base - token;
5188 } else {
5189 dir_len = 0;
5190 base = token;
5191 }
5192 memcpy(name, base, name_len);
5193 name[name_len] = '\0';
5194 dir = xmalloc(dir_len + 1, "report_dir");
5195 memcpy(dir, token, dir_len);
5196 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005197 state->file->report_line = override_line - 1;
5198 state->file->report_name = name;
5199 state->file->report_dir = dir;
5200 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005201 }
5202 break;
5203 }
5204 case TOK_MLINE:
5205 {
5206 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005207 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005208 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005209 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5210 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005211 const char *token, *base;
5212 char *name, *dir;
5213 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005214 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005215 name = xmalloc(tk->str_len, "report_name");
5216 token = tk->val.str + 1;
5217 base = strrchr(token, '/');
5218 name_len = tk->str_len - 2;
5219 if (base != 0) {
5220 dir_len = base - token;
5221 base++;
5222 name_len -= base - token;
5223 } else {
5224 dir_len = 0;
5225 base = token;
5226 }
5227 memcpy(name, base, name_len);
5228 name[name_len] = '\0';
5229 dir = xmalloc(dir_len + 1, "report_dir");
5230 memcpy(dir, token, dir_len);
5231 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005232 state->file->report_name = name;
5233 state->file->report_dir = dir;
5234 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005235 }
5236 break;
5237 }
5238 case TOK_MUNDEF:
5239 {
5240 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005241 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005242 if (if_eat(state)) /* quit early when #if'd out */
5243 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005244
Eric Biedermancb364952004-11-15 10:46:44 +00005245 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005246
5247 undef_macro(state, ident);
5248 break;
5249 }
5250 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005251 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005252 if (if_eat(state)) /* quit early when #if'd out */
5253 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005254 warning(state, 0, "Ignoring pragma");
Eric Biederman41203d92004-11-08 09:31:09 +00005255 break;
5256 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005257 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005258 reenter_if(state, "#elif");
5259 if (if_eat(state)) /* quit early when #if'd out */
5260 break;
5261 /* If the #if was taken the #elif just disables the following code */
5262 if (if_value(state)) {
5263 eat_tokens(state, TOK_MENDIF);
5264 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005265 /* If the previous #if was not taken see if the #elif enables the
Eric Biederman41203d92004-11-08 09:31:09 +00005266 * trailing code.
5267 */
5268 else {
5269 set_if_value(state, mcexpr(state));
5270 if (!if_value(state)) {
5271 eat_tokens(state, TOK_MELSE);
5272 }
5273 }
5274 break;
5275 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005276 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005277 enter_if(state);
5278 if (if_eat(state)) /* quit early when #if'd out */
5279 break;
5280 set_if_value(state, mcexpr(state));
5281 if (!if_value(state)) {
5282 eat_tokens(state, TOK_MELSE);
5283 }
5284 break;
5285 case TOK_MIFNDEF:
5286 {
5287 struct hash_entry *ident;
5288
Eric Biedermancb364952004-11-15 10:46:44 +00005289 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005290 enter_if(state);
5291 if (if_eat(state)) /* quit early when #if'd out */
5292 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005293 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005294 set_if_value(state, ident->sym_define == 0);
5295 if (!if_value(state)) {
5296 eat_tokens(state, TOK_MELSE);
5297 }
5298 break;
5299 }
5300 case TOK_MIFDEF:
5301 {
5302 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005303 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005304 enter_if(state);
5305 if (if_eat(state)) /* quit early when #if'd out */
5306 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005307 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005308 set_if_value(state, ident->sym_define != 0);
5309 if (!if_value(state)) {
5310 eat_tokens(state, TOK_MELSE);
5311 }
5312 break;
5313 }
5314 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005315 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005316 enter_else(state, "#else");
5317 if (!if_eat(state) && if_value(state)) {
5318 eat_tokens(state, TOK_MENDIF);
5319 }
5320 break;
5321 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005322 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005323 exit_if(state, "#endif");
5324 break;
5325 case TOK_MDEFINE:
5326 {
5327 struct hash_entry *ident;
5328 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005329 const char *mstart, *mend;
5330 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005331
Eric Biedermancb364952004-11-15 10:46:44 +00005332 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005333 if (if_eat(state)) /* quit early when #if'd out */
5334 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005335 ident = pp_eat(state, TOK_MIDENT)->ident;
5336 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005337 args = 0;
5338 larg = &args;
5339
Eric Biederman41203d92004-11-08 09:31:09 +00005340 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005341 if (raw_peek(state) == TOK_LPAREN) {
5342 raw_eat(state, TOK_LPAREN);
5343 argc += 1;
5344
Eric Biederman41203d92004-11-08 09:31:09 +00005345 for(;;) {
5346 struct macro_arg *narg, *arg;
5347 struct hash_entry *aident;
5348 int tok;
5349
Eric Biedermancb364952004-11-15 10:46:44 +00005350 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005351 if (!args && (tok == TOK_RPAREN)) {
5352 break;
5353 }
5354 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005355 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005356 aident = state->i___VA_ARGS__;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005357 }
Eric Biederman41203d92004-11-08 09:31:09 +00005358 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005359 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005360 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005361
Eric Biederman41203d92004-11-08 09:31:09 +00005362 narg = xcmalloc(sizeof(*arg), "macro arg");
5363 narg->ident = aident;
5364
5365 /* Verify I don't have a duplicate identifier */
5366 for(arg = args; arg; arg = arg->next) {
5367 if (arg->ident == narg->ident) {
5368 error(state, 0, "Duplicate macro arg `%s'",
5369 narg->ident->name);
5370 }
5371 }
5372 /* Add the new argument to the end of the list */
5373 *larg = narg;
5374 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005375 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005376
5377 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005378 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005379 break;
5380 }
Eric Biedermancb364952004-11-15 10:46:44 +00005381 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005382 }
Eric Biedermancb364952004-11-15 10:46:44 +00005383 pp_eat(state, TOK_RPAREN);
5384 }
5385 /* Remove leading whitespace */
5386 while(raw_peek(state) == TOK_SPACE) {
5387 raw_eat(state, TOK_SPACE);
5388 }
Eric Biederman41203d92004-11-08 09:31:09 +00005389
Eric Biedermancb364952004-11-15 10:46:44 +00005390 /* Remember the start of the macro body */
5391 tok = raw_peek(state);
5392 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005393
Eric Biedermancb364952004-11-15 10:46:44 +00005394 /* Find the end of the macro */
5395 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5396 raw_eat(state, tok);
5397 /* Remember the end of the last non space token */
5398 raw_peek(state);
5399 if (tok != TOK_SPACE) {
5400 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005401 }
5402 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005403
Eric Biedermancb364952004-11-15 10:46:44 +00005404 /* Now that I have found the body defined the token */
5405 do_define_macro(state, ident,
5406 char_strdup(state->file, mstart, mend, "macro buf"),
5407 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005408 break;
5409 }
5410 case TOK_MERROR:
5411 {
Eric Biedermancb364952004-11-15 10:46:44 +00005412 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005413 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005414
Eric Biedermancb364952004-11-15 10:46:44 +00005415 pp_eat(state, TOK_MERROR);
5416 /* Find the start of the line */
5417 raw_peek(state);
5418 start = get_token(state, 1)->pos;
5419
Eric Biederman41203d92004-11-08 09:31:09 +00005420 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005421 while((tok = raw_peek(state)) != TOK_EOL) {
5422 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005423 }
Eric Biedermancb364952004-11-15 10:46:44 +00005424 end = get_token(state, 1)->pos;
5425 len = end - start;
5426 if (!if_eat(state)) {
5427 error(state, 0, "%*.*s", len, len, start);
5428 }
Eric Biederman41203d92004-11-08 09:31:09 +00005429 break;
5430 }
5431 case TOK_MWARNING:
5432 {
Eric Biedermancb364952004-11-15 10:46:44 +00005433 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005434 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005435
Eric Biedermancb364952004-11-15 10:46:44 +00005436 pp_eat(state, TOK_MWARNING);
5437
5438 /* Find the start of the line */
5439 raw_peek(state);
5440 start = get_token(state, 1)->pos;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005441
Eric Biederman41203d92004-11-08 09:31:09 +00005442 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005443 while((tok = raw_peek(state)) != TOK_EOL) {
5444 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005445 }
Eric Biedermancb364952004-11-15 10:46:44 +00005446 end = get_token(state, 1)->pos;
5447 len = end - start;
5448 if (!if_eat(state)) {
5449 warning(state, 0, "%*.*s", len, len, start);
5450 }
Eric Biederman41203d92004-11-08 09:31:09 +00005451 break;
5452 }
5453 case TOK_MINCLUDE:
5454 {
5455 char *name;
5456 int local;
5457 local = 0;
5458 name = 0;
5459
Eric Biedermancb364952004-11-15 10:46:44 +00005460 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005461 if (if_eat(state)) {
5462 /* Find the end of the line */
5463 while((tok = raw_peek(state)) != TOK_EOL) {
5464 raw_eat(state, tok);
5465 }
5466 break;
5467 }
Eric Biederman41203d92004-11-08 09:31:09 +00005468 tok = peek(state);
5469 if (tok == TOK_LIT_STRING) {
5470 struct token *tk;
5471 const char *token;
5472 int name_len;
5473 tk = eat(state, TOK_LIT_STRING);
5474 name = xmalloc(tk->str_len, "include");
5475 token = tk->val.str +1;
5476 name_len = tk->str_len -2;
5477 if (*token == '"') {
5478 token++;
5479 name_len--;
5480 }
5481 memcpy(name, token, name_len);
5482 name[name_len] = '\0';
5483 local = 1;
5484 }
5485 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005486 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005487 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005488
5489 buf.len = 40;
5490 buf.str = xmalloc(buf.len, "include");
5491 buf.pos = 0;
5492
5493 tok = peek(state);
5494 while((tok != TOK_MORE) &&
5495 (tok != TOK_EOL) && (tok != TOK_EOF))
5496 {
5497 struct token *tk;
5498 tk = eat(state, tok);
5499 append_macro_chars(state, "include", &buf,
5500 state->file, tk->pos, state->file->pos);
5501 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005502 }
Eric Biedermancb364952004-11-15 10:46:44 +00005503 append_macro_text(state, "include", &buf, "\0", 1);
5504 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005505 error(state, 0, "Unterminated include directive");
5506 }
Eric Biederman41203d92004-11-08 09:31:09 +00005507 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005508 local = 0;
5509 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005510 }
5511 else {
5512 error(state, 0, "Invalid include directive");
5513 }
5514 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005515 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005516 error(state, 0, "garbage after include directive");
5517 }
5518 if (!if_eat(state)) {
5519 compile_file(state, name, local);
5520 }
5521 xfree(name);
5522 break;
5523 }
5524 case TOK_EOL:
5525 /* Ignore # without a follwing ident */
5526 break;
5527 default:
5528 {
5529 const char *name1, *name2;
5530 name1 = tokens[tok];
5531 name2 = "";
5532 if (tok == TOK_MIDENT) {
5533 name2 = get_token(state, 1)->ident->name;
5534 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005535 error(state, 0, "Invalid preprocessor directive: %s %s",
Eric Biederman41203d92004-11-08 09:31:09 +00005536 name1, name2);
5537 break;
5538 }
5539 }
5540 /* Consume the rest of the macro line */
5541 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005542 tok = pp_peek(state);
5543 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005544 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5545 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005546 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005547 return;
5548}
5549
Eric Biederman0babc1c2003-05-09 02:39:00 +00005550/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005551
5552static struct type *new_type(
5553 unsigned int type, struct type *left, struct type *right)
5554{
5555 struct type *result;
5556 result = xmalloc(sizeof(*result), "type");
5557 result->type = type;
5558 result->left = left;
5559 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005560 result->field_ident = 0;
5561 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005562 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005563 return result;
5564}
5565
5566static struct type *clone_type(unsigned int specifiers, struct type *old)
5567{
5568 struct type *result;
5569 result = xmalloc(sizeof(*result), "type");
5570 memcpy(result, old, sizeof(*result));
5571 result->type &= TYPE_MASK;
5572 result->type |= specifiers;
5573 return result;
5574}
5575
Eric Biederman90089602004-05-28 14:11:54 +00005576static struct type *dup_type(struct compile_state *state, struct type *orig)
5577{
5578 struct type *new;
5579 new = xcmalloc(sizeof(*new), "type");
5580 new->type = orig->type;
5581 new->field_ident = orig->field_ident;
5582 new->type_ident = orig->type_ident;
5583 new->elements = orig->elements;
5584 if (orig->left) {
5585 new->left = dup_type(state, orig->left);
5586 }
5587 if (orig->right) {
5588 new->right = dup_type(state, orig->right);
5589 }
5590 return new;
5591}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005592
Eric Biederman90089602004-05-28 14:11:54 +00005593
5594static struct type *invalid_type(struct compile_state *state, struct type *type)
5595{
5596 struct type *invalid, *member;
5597 invalid = 0;
5598 if (!type) {
5599 internal_error(state, 0, "type missing?");
5600 }
5601 switch(type->type & TYPE_MASK) {
5602 case TYPE_VOID:
5603 case TYPE_CHAR: case TYPE_UCHAR:
5604 case TYPE_SHORT: case TYPE_USHORT:
5605 case TYPE_INT: case TYPE_UINT:
5606 case TYPE_LONG: case TYPE_ULONG:
5607 case TYPE_LLONG: case TYPE_ULLONG:
5608 case TYPE_POINTER:
5609 case TYPE_ENUM:
5610 break;
5611 case TYPE_BITFIELD:
5612 invalid = invalid_type(state, type->left);
5613 break;
5614 case TYPE_ARRAY:
5615 invalid = invalid_type(state, type->left);
5616 break;
5617 case TYPE_STRUCT:
5618 case TYPE_TUPLE:
5619 member = type->left;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005620 while(member && (invalid == 0) &&
Eric Biederman90089602004-05-28 14:11:54 +00005621 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5622 invalid = invalid_type(state, member->left);
5623 member = member->right;
5624 }
5625 if (!invalid) {
5626 invalid = invalid_type(state, member);
5627 }
5628 break;
5629 case TYPE_UNION:
5630 case TYPE_JOIN:
5631 member = type->left;
5632 while(member && (invalid == 0) &&
5633 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
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 default:
5642 invalid = type;
5643 break;
5644 }
5645 return invalid;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005646
Eric Biederman90089602004-05-28 14:11:54 +00005647}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005648
5649#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005650#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005651static inline ulong_t mask_uint(ulong_t x)
5652{
5653 if (SIZEOF_INT < SIZEOF_LONG) {
Stefan Reinauer50542a82007-10-24 11:14:14 +00005654 ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005655 x &= mask;
5656 }
5657 return x;
5658}
5659#define MASK_UINT(X) (mask_uint(X))
5660#define MASK_ULONG(X) (X)
5661
Eric Biederman90089602004-05-28 14:11:54 +00005662static struct type void_type = { .type = TYPE_VOID };
5663static struct type char_type = { .type = TYPE_CHAR };
5664static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005665#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005666static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005667#endif
Eric Biederman90089602004-05-28 14:11:54 +00005668static struct type ushort_type = { .type = TYPE_USHORT };
5669static struct type int_type = { .type = TYPE_INT };
5670static struct type uint_type = { .type = TYPE_UINT };
5671static struct type long_type = { .type = TYPE_LONG };
5672static struct type ulong_type = { .type = TYPE_ULONG };
5673static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005674
Eric Biederman5ade04a2003-10-22 04:03:46 +00005675static struct type void_ptr_type = {
5676 .type = TYPE_POINTER,
5677 .left = &void_type,
5678};
5679
Stefan Reinauer50542a82007-10-24 11:14:14 +00005680#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005681static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005682 .type = TYPE_FUNCTION,
5683 .left = &void_type,
5684 .right = &void_type,
5685};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005686#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005687
Eric Biederman90089602004-05-28 14:11:54 +00005688static size_t bits_to_bytes(size_t size)
5689{
5690 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5691}
5692
Eric Biedermanb138ac82003-04-22 18:44:01 +00005693static struct triple *variable(struct compile_state *state, struct type *type)
5694{
5695 struct triple *result;
5696 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005697 result = triple(state, OP_ADECL, type, 0, 0);
5698 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005699 }
5700 else {
5701 result = triple(state, OP_SDECL, type, 0, 0);
5702 }
5703 return result;
5704}
5705
5706static void stor_of(FILE *fp, struct type *type)
5707{
5708 switch(type->type & STOR_MASK) {
5709 case STOR_AUTO:
5710 fprintf(fp, "auto ");
5711 break;
5712 case STOR_STATIC:
5713 fprintf(fp, "static ");
5714 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005715 case STOR_LOCAL:
5716 fprintf(fp, "local ");
5717 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005718 case STOR_EXTERN:
5719 fprintf(fp, "extern ");
5720 break;
5721 case STOR_REGISTER:
5722 fprintf(fp, "register ");
5723 break;
5724 case STOR_TYPEDEF:
5725 fprintf(fp, "typedef ");
5726 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005727 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005728 fprintf(fp, "inline ");
5729 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005730 case STOR_INLINE | STOR_STATIC:
5731 fprintf(fp, "static inline");
5732 break;
5733 case STOR_INLINE | STOR_EXTERN:
5734 fprintf(fp, "extern inline");
5735 break;
5736 default:
5737 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5738 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005739 }
5740}
5741static void qual_of(FILE *fp, struct type *type)
5742{
5743 if (type->type & QUAL_CONST) {
5744 fprintf(fp, " const");
5745 }
5746 if (type->type & QUAL_VOLATILE) {
5747 fprintf(fp, " volatile");
5748 }
5749 if (type->type & QUAL_RESTRICT) {
5750 fprintf(fp, " restrict");
5751 }
5752}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005753
Eric Biedermanb138ac82003-04-22 18:44:01 +00005754static void name_of(FILE *fp, struct type *type)
5755{
Eric Biederman90089602004-05-28 14:11:54 +00005756 unsigned int base_type;
5757 base_type = type->type & TYPE_MASK;
5758 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5759 stor_of(fp, type);
5760 }
5761 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005762 case TYPE_VOID:
5763 fprintf(fp, "void");
5764 qual_of(fp, type);
5765 break;
5766 case TYPE_CHAR:
5767 fprintf(fp, "signed char");
5768 qual_of(fp, type);
5769 break;
5770 case TYPE_UCHAR:
5771 fprintf(fp, "unsigned char");
5772 qual_of(fp, type);
5773 break;
5774 case TYPE_SHORT:
5775 fprintf(fp, "signed short");
5776 qual_of(fp, type);
5777 break;
5778 case TYPE_USHORT:
5779 fprintf(fp, "unsigned short");
5780 qual_of(fp, type);
5781 break;
5782 case TYPE_INT:
5783 fprintf(fp, "signed int");
5784 qual_of(fp, type);
5785 break;
5786 case TYPE_UINT:
5787 fprintf(fp, "unsigned int");
5788 qual_of(fp, type);
5789 break;
5790 case TYPE_LONG:
5791 fprintf(fp, "signed long");
5792 qual_of(fp, type);
5793 break;
5794 case TYPE_ULONG:
5795 fprintf(fp, "unsigned long");
5796 qual_of(fp, type);
5797 break;
5798 case TYPE_POINTER:
5799 name_of(fp, type->left);
5800 fprintf(fp, " * ");
5801 qual_of(fp, type);
5802 break;
5803 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005804 name_of(fp, type->left);
5805 fprintf(fp, ", ");
5806 name_of(fp, type->right);
5807 break;
Eric Biederman90089602004-05-28 14:11:54 +00005808 case TYPE_OVERLAP:
5809 name_of(fp, type->left);
5810 fprintf(fp, ",| ");
5811 name_of(fp, type->right);
5812 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005813 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005814 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005815 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005816 qual_of(fp, type);
5817 break;
5818 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005819 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005820 (type->type_ident)? type->type_ident->name : "");
5821 name_of(fp, type->left);
5822 fprintf(fp, " } ");
5823 qual_of(fp, type);
5824 break;
5825 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005826 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005827 (type->type_ident)? type->type_ident->name : "");
5828 name_of(fp, type->left);
5829 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005830 qual_of(fp, type);
5831 break;
5832 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005833 name_of(fp, type->left);
5834 fprintf(fp, " (*)(");
5835 name_of(fp, type->right);
5836 fprintf(fp, ")");
5837 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 case TYPE_ARRAY:
5839 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005840 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005841 break;
Eric Biederman90089602004-05-28 14:11:54 +00005842 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005843 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005844 name_of(fp, type->left);
5845 fprintf(fp, " } ");
5846 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005847 break;
Eric Biederman90089602004-05-28 14:11:54 +00005848 case TYPE_JOIN:
5849 fprintf(fp, "join { ");
5850 name_of(fp, type->left);
5851 fprintf(fp, " } ");
5852 qual_of(fp, type);
5853 break;
5854 case TYPE_BITFIELD:
5855 name_of(fp, type->left);
5856 fprintf(fp, " : %d ", type->elements);
5857 qual_of(fp, type);
5858 break;
5859 case TYPE_UNKNOWN:
5860 fprintf(fp, "unknown_t");
5861 break;
5862 default:
5863 fprintf(fp, "????: %x", base_type);
5864 break;
5865 }
5866 if (type->field_ident && type->field_ident->name) {
5867 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005868 }
5869}
5870
5871static size_t align_of(struct compile_state *state, struct type *type)
5872{
5873 size_t align;
5874 align = 0;
5875 switch(type->type & TYPE_MASK) {
5876 case TYPE_VOID:
5877 align = 1;
5878 break;
Eric Biederman90089602004-05-28 14:11:54 +00005879 case TYPE_BITFIELD:
5880 align = 1;
5881 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005882 case TYPE_CHAR:
5883 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005884 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005885 break;
5886 case TYPE_SHORT:
5887 case TYPE_USHORT:
5888 align = ALIGNOF_SHORT;
5889 break;
5890 case TYPE_INT:
5891 case TYPE_UINT:
5892 case TYPE_ENUM:
5893 align = ALIGNOF_INT;
5894 break;
5895 case TYPE_LONG:
5896 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005897 align = ALIGNOF_LONG;
5898 break;
Eric Biederman90089602004-05-28 14:11:54 +00005899 case TYPE_POINTER:
5900 align = ALIGNOF_POINTER;
5901 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005902 case TYPE_PRODUCT:
5903 case TYPE_OVERLAP:
5904 {
5905 size_t left_align, right_align;
5906 left_align = align_of(state, type->left);
5907 right_align = align_of(state, type->right);
5908 align = (left_align >= right_align) ? left_align : right_align;
5909 break;
5910 }
5911 case TYPE_ARRAY:
5912 align = align_of(state, type->left);
5913 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005914 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005915 case TYPE_TUPLE:
5916 case TYPE_UNION:
5917 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005918 align = align_of(state, type->left);
5919 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005920 default:
5921 error(state, 0, "alignof not yet defined for type\n");
5922 break;
5923 }
5924 return align;
5925}
5926
Eric Biederman90089602004-05-28 14:11:54 +00005927static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005928{
Eric Biederman90089602004-05-28 14:11:54 +00005929 size_t align;
5930 align = 0;
5931 switch(type->type & TYPE_MASK) {
5932 case TYPE_VOID:
5933 align = 1;
5934 break;
5935 case TYPE_BITFIELD:
5936 align = 1;
5937 break;
5938 case TYPE_CHAR:
5939 case TYPE_UCHAR:
5940 align = REG_ALIGNOF_CHAR;
5941 break;
5942 case TYPE_SHORT:
5943 case TYPE_USHORT:
5944 align = REG_ALIGNOF_SHORT;
5945 break;
5946 case TYPE_INT:
5947 case TYPE_UINT:
5948 case TYPE_ENUM:
5949 align = REG_ALIGNOF_INT;
5950 break;
5951 case TYPE_LONG:
5952 case TYPE_ULONG:
5953 align = REG_ALIGNOF_LONG;
5954 break;
5955 case TYPE_POINTER:
5956 align = REG_ALIGNOF_POINTER;
5957 break;
5958 case TYPE_PRODUCT:
5959 case TYPE_OVERLAP:
5960 {
5961 size_t left_align, right_align;
5962 left_align = reg_align_of(state, type->left);
5963 right_align = reg_align_of(state, type->right);
5964 align = (left_align >= right_align) ? left_align : right_align;
5965 break;
5966 }
5967 case TYPE_ARRAY:
5968 align = reg_align_of(state, type->left);
5969 break;
5970 case TYPE_STRUCT:
5971 case TYPE_UNION:
5972 case TYPE_TUPLE:
5973 case TYPE_JOIN:
5974 align = reg_align_of(state, type->left);
5975 break;
5976 default:
5977 error(state, 0, "alignof not yet defined for type\n");
5978 break;
5979 }
5980 return align;
5981}
5982
5983static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5984{
5985 return bits_to_bytes(align_of(state, type));
5986}
5987static size_t size_of(struct compile_state *state, struct type *type);
5988static size_t reg_size_of(struct compile_state *state, struct type *type);
5989
Stefan Reinauer14e22772010-04-27 06:56:47 +00005990static size_t needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00005991 struct type *type, size_t offset)
5992{
5993 size_t padding, align;
5994 align = align_of(state, type);
5995 /* Align to the next machine word if the bitfield does completely
5996 * fit into the current word.
5997 */
5998 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5999 size_t size;
6000 size = size_of(state, type);
6001 if ((offset + type->elements)/size != offset/size) {
6002 align = size;
6003 }
6004 }
Eric Biederman03b59862003-06-24 14:27:37 +00006005 padding = 0;
6006 if (offset % align) {
6007 padding = align - (offset % align);
6008 }
6009 return padding;
6010}
Eric Biederman90089602004-05-28 14:11:54 +00006011
Stefan Reinauer14e22772010-04-27 06:56:47 +00006012static size_t reg_needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006013 struct type *type, size_t offset)
6014{
6015 size_t padding, align;
6016 align = reg_align_of(state, type);
6017 /* Align to the next register word if the bitfield does completely
6018 * fit into the current register.
6019 */
6020 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00006021 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
Eric Biederman90089602004-05-28 14:11:54 +00006022 {
6023 align = REG_SIZEOF_REG;
6024 }
6025 padding = 0;
6026 if (offset % align) {
6027 padding = align - (offset % align);
6028 }
6029 return padding;
6030}
6031
Eric Biedermanb138ac82003-04-22 18:44:01 +00006032static size_t size_of(struct compile_state *state, struct type *type)
6033{
6034 size_t size;
6035 size = 0;
6036 switch(type->type & TYPE_MASK) {
6037 case TYPE_VOID:
6038 size = 0;
6039 break;
Eric Biederman90089602004-05-28 14:11:54 +00006040 case TYPE_BITFIELD:
6041 size = type->elements;
6042 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006043 case TYPE_CHAR:
6044 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006045 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006046 break;
6047 case TYPE_SHORT:
6048 case TYPE_USHORT:
6049 size = SIZEOF_SHORT;
6050 break;
6051 case TYPE_INT:
6052 case TYPE_UINT:
6053 case TYPE_ENUM:
6054 size = SIZEOF_INT;
6055 break;
6056 case TYPE_LONG:
6057 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006058 size = SIZEOF_LONG;
6059 break;
Eric Biederman90089602004-05-28 14:11:54 +00006060 case TYPE_POINTER:
6061 size = SIZEOF_POINTER;
6062 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006063 case TYPE_PRODUCT:
6064 {
Eric Biederman90089602004-05-28 14:11:54 +00006065 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006066 size = 0;
6067 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006068 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006069 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006070 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006071 }
Eric Biederman90089602004-05-28 14:11:54 +00006072 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006073 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006074 break;
6075 }
6076 case TYPE_OVERLAP:
6077 {
6078 size_t size_left, size_right;
6079 size_left = size_of(state, type->left);
6080 size_right = size_of(state, type->right);
6081 size = (size_left >= size_right)? size_left : size_right;
6082 break;
6083 }
6084 case TYPE_ARRAY:
6085 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6086 internal_error(state, 0, "Invalid array type");
6087 } else {
6088 size = size_of(state, type->left) * type->elements;
6089 }
6090 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006091 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006092 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006093 {
Eric Biederman90089602004-05-28 14:11:54 +00006094 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006095 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006096 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006097 pad = needed_padding(state, type, size);
6098 size = size + pad;
6099 break;
6100 }
6101 case TYPE_UNION:
6102 case TYPE_JOIN:
6103 {
6104 size_t pad;
6105 size = size_of(state, type->left);
6106 /* Pad unions so their size is a multiple of their alignment */
6107 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006108 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006109 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006111 default:
Eric Biederman90089602004-05-28 14:11:54 +00006112 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006113 break;
6114 }
6115 return size;
6116}
6117
Eric Biederman90089602004-05-28 14:11:54 +00006118static size_t reg_size_of(struct compile_state *state, struct type *type)
6119{
6120 size_t size;
6121 size = 0;
6122 switch(type->type & TYPE_MASK) {
6123 case TYPE_VOID:
6124 size = 0;
6125 break;
6126 case TYPE_BITFIELD:
6127 size = type->elements;
6128 break;
6129 case TYPE_CHAR:
6130 case TYPE_UCHAR:
6131 size = REG_SIZEOF_CHAR;
6132 break;
6133 case TYPE_SHORT:
6134 case TYPE_USHORT:
6135 size = REG_SIZEOF_SHORT;
6136 break;
6137 case TYPE_INT:
6138 case TYPE_UINT:
6139 case TYPE_ENUM:
6140 size = REG_SIZEOF_INT;
6141 break;
6142 case TYPE_LONG:
6143 case TYPE_ULONG:
6144 size = REG_SIZEOF_LONG;
6145 break;
6146 case TYPE_POINTER:
6147 size = REG_SIZEOF_POINTER;
6148 break;
6149 case TYPE_PRODUCT:
6150 {
6151 size_t pad;
6152 size = 0;
6153 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6154 pad = reg_needed_padding(state, type->left, size);
6155 size = size + pad + reg_size_of(state, type->left);
6156 type = type->right;
6157 }
6158 pad = reg_needed_padding(state, type, size);
6159 size = size + pad + reg_size_of(state, type);
6160 break;
6161 }
6162 case TYPE_OVERLAP:
6163 {
6164 size_t size_left, size_right;
6165 size_left = reg_size_of(state, type->left);
6166 size_right = reg_size_of(state, type->right);
6167 size = (size_left >= size_right)? size_left : size_right;
6168 break;
6169 }
6170 case TYPE_ARRAY:
6171 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6172 internal_error(state, 0, "Invalid array type");
6173 } else {
6174 size = reg_size_of(state, type->left) * type->elements;
6175 }
6176 break;
6177 case TYPE_STRUCT:
6178 case TYPE_TUPLE:
6179 {
6180 size_t pad;
6181 size = reg_size_of(state, type->left);
6182 /* Pad structures so their size is a multiples of their alignment */
6183 pad = reg_needed_padding(state, type, size);
6184 size = size + pad;
6185 break;
6186 }
6187 case TYPE_UNION:
6188 case TYPE_JOIN:
6189 {
6190 size_t pad;
6191 size = reg_size_of(state, type->left);
6192 /* Pad unions so their size is a multiple of their alignment */
6193 pad = reg_needed_padding(state, type, size);
6194 size = size + pad;
6195 break;
6196 }
6197 default:
6198 internal_error(state, 0, "sizeof not yet defined for type");
6199 break;
6200 }
6201 return size;
6202}
6203
6204static size_t registers_of(struct compile_state *state, struct type *type)
6205{
6206 size_t registers;
6207 registers = reg_size_of(state, type);
6208 registers += REG_SIZEOF_REG - 1;
6209 registers /= REG_SIZEOF_REG;
6210 return registers;
6211}
6212
6213static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6214{
6215 return bits_to_bytes(size_of(state, type));
6216}
6217
Stefan Reinauer14e22772010-04-27 06:56:47 +00006218static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006219 struct type *type, struct hash_entry *field)
6220{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006221 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006222 size_t size;
6223
Eric Biederman0babc1c2003-05-09 02:39:00 +00006224 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006225 member = 0;
6226 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6227 member = type->left;
6228 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6229 size += needed_padding(state, member->left, size);
6230 if (member->left->field_ident == field) {
6231 member = member->left;
6232 break;
6233 }
6234 size += size_of(state, member->left);
6235 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006236 }
Eric Biederman90089602004-05-28 14:11:54 +00006237 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006238 }
Eric Biederman90089602004-05-28 14:11:54 +00006239 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6240 member = type->left;
6241 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6242 if (member->left->field_ident == field) {
6243 member = member->left;
6244 break;
6245 }
6246 member = member->right;
6247 }
6248 }
6249 else {
6250 internal_error(state, 0, "field_offset only works on structures and unions");
6251 }
6252
6253 if (!member || (member->field_ident != field)) {
6254 error(state, 0, "member %s not present", field->name);
6255 }
6256 return size;
6257}
6258
Stefan Reinauer14e22772010-04-27 06:56:47 +00006259static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006260 struct type *type, struct hash_entry *field)
6261{
6262 struct type *member;
6263 size_t size;
6264
6265 size = 0;
6266 member = 0;
6267 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6268 member = type->left;
6269 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6270 size += reg_needed_padding(state, member->left, size);
6271 if (member->left->field_ident == field) {
6272 member = member->left;
6273 break;
6274 }
6275 size += reg_size_of(state, member->left);
6276 member = member->right;
6277 }
6278 }
6279 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6280 member = type->left;
6281 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6282 if (member->left->field_ident == field) {
6283 member = member->left;
6284 break;
6285 }
6286 member = member->right;
6287 }
6288 }
6289 else {
6290 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6291 }
6292
6293 size += reg_needed_padding(state, member, size);
6294 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006295 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006296 }
6297 return size;
6298}
6299
Stefan Reinauer14e22772010-04-27 06:56:47 +00006300static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006301 struct type *type, struct hash_entry *field)
6302{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006303 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006304
6305 member = 0;
6306 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6307 member = type->left;
6308 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6309 if (member->left->field_ident == field) {
6310 member = member->left;
6311 break;
6312 }
6313 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006314 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006315 }
Eric Biederman90089602004-05-28 14:11:54 +00006316 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6317 member = type->left;
6318 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6319 if (member->left->field_ident == field) {
6320 member = member->left;
6321 break;
6322 }
6323 member = member->right;
6324 }
6325 }
6326 else {
6327 internal_error(state, 0, "field_type only works on structures and unions");
6328 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006329
Eric Biederman90089602004-05-28 14:11:54 +00006330 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006331 error(state, 0, "member %s not present", field->name);
6332 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006333 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006334}
6335
Stefan Reinauer14e22772010-04-27 06:56:47 +00006336static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006337 struct type *type, ulong_t index)
6338{
6339 struct type *member;
6340 size_t size;
6341 size = 0;
6342 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6343 size = size_of(state, type->left) * index;
6344 }
6345 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6346 ulong_t i;
6347 member = type->left;
6348 i = 0;
6349 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6350 size += needed_padding(state, member->left, size);
6351 if (i == index) {
6352 member = member->left;
6353 break;
6354 }
6355 size += size_of(state, member->left);
6356 i++;
6357 member = member->right;
6358 }
6359 size += needed_padding(state, member, size);
6360 if (i != index) {
6361 internal_error(state, 0, "Missing member index: %u", index);
6362 }
6363 }
6364 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6365 ulong_t i;
6366 size = 0;
6367 member = type->left;
6368 i = 0;
6369 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6370 if (i == index) {
6371 member = member->left;
6372 break;
6373 }
6374 i++;
6375 member = member->right;
6376 }
6377 if (i != index) {
6378 internal_error(state, 0, "Missing member index: %u", index);
6379 }
6380 }
6381 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006382 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006383 "request for index %u in something not an array, tuple or join",
6384 index);
6385 }
6386 return size;
6387}
6388
Stefan Reinauer14e22772010-04-27 06:56:47 +00006389static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006390 struct type *type, ulong_t index)
6391{
6392 struct type *member;
6393 size_t size;
6394 size = 0;
6395 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6396 size = reg_size_of(state, type->left) * index;
6397 }
6398 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6399 ulong_t i;
6400 member = type->left;
6401 i = 0;
6402 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6403 size += reg_needed_padding(state, member->left, size);
6404 if (i == index) {
6405 member = member->left;
6406 break;
6407 }
6408 size += reg_size_of(state, member->left);
6409 i++;
6410 member = member->right;
6411 }
6412 size += reg_needed_padding(state, member, size);
6413 if (i != index) {
6414 internal_error(state, 0, "Missing member index: %u", index);
6415 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006416
Eric Biederman90089602004-05-28 14:11:54 +00006417 }
6418 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6419 ulong_t i;
6420 size = 0;
6421 member = type->left;
6422 i = 0;
6423 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6424 if (i == index) {
6425 member = member->left;
6426 break;
6427 }
6428 i++;
6429 member = member->right;
6430 }
6431 if (i != index) {
6432 internal_error(state, 0, "Missing member index: %u", index);
6433 }
6434 }
6435 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006436 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006437 "request for index %u in something not an array, tuple or join",
6438 index);
6439 }
6440 return size;
6441}
6442
6443static struct type *index_type(struct compile_state *state,
6444 struct type *type, ulong_t index)
6445{
6446 struct type *member;
6447 if (index >= type->elements) {
6448 internal_error(state, 0, "Invalid element %u requested", index);
6449 }
6450 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6451 member = type->left;
6452 }
6453 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6454 ulong_t i;
6455 member = type->left;
6456 i = 0;
6457 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6458 if (i == index) {
6459 member = member->left;
6460 break;
6461 }
6462 i++;
6463 member = member->right;
6464 }
6465 if (i != index) {
6466 internal_error(state, 0, "Missing member index: %u", index);
6467 }
6468 }
6469 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6470 ulong_t i;
6471 member = type->left;
6472 i = 0;
6473 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6474 if (i == index) {
6475 member = member->left;
6476 break;
6477 }
6478 i++;
6479 member = member->right;
6480 }
6481 if (i != index) {
6482 internal_error(state, 0, "Missing member index: %u", index);
6483 }
6484 }
6485 else {
6486 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006487 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006488 "request for index %u in something not an array, tuple or join",
6489 index);
6490 }
6491 return member;
6492}
6493
6494static struct type *unpack_type(struct compile_state *state, struct type *type)
6495{
6496 /* If I have a single register compound type not a bit-field
6497 * find the real type.
6498 */
6499 struct type *start_type;
6500 size_t size;
6501 /* Get out early if I need multiple registers for this type */
6502 size = reg_size_of(state, type);
6503 if (size > REG_SIZEOF_REG) {
6504 return type;
6505 }
6506 /* Get out early if I don't need any registers for this type */
6507 if (size == 0) {
6508 return &void_type;
6509 }
6510 /* Loop until I have no more layers I can remove */
6511 do {
6512 start_type = type;
6513 switch(type->type & TYPE_MASK) {
6514 case TYPE_ARRAY:
6515 /* If I have a single element the unpacked type
6516 * is that element.
6517 */
6518 if (type->elements == 1) {
6519 type = type->left;
6520 }
6521 break;
6522 case TYPE_STRUCT:
6523 case TYPE_TUPLE:
6524 /* If I have a single element the unpacked type
6525 * is that element.
6526 */
6527 if (type->elements == 1) {
6528 type = type->left;
6529 }
6530 /* If I have multiple elements the unpacked
6531 * type is the non-void element.
6532 */
6533 else {
6534 struct type *next, *member;
6535 struct type *sub_type;
6536 sub_type = 0;
6537 next = type->left;
6538 while(next) {
6539 member = next;
6540 next = 0;
6541 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6542 next = member->right;
6543 member = member->left;
6544 }
6545 if (reg_size_of(state, member) > 0) {
6546 if (sub_type) {
6547 internal_error(state, 0, "true compound type in a register");
6548 }
6549 sub_type = member;
6550 }
6551 }
6552 if (sub_type) {
6553 type = sub_type;
6554 }
6555 }
6556 break;
6557
6558 case TYPE_UNION:
6559 case TYPE_JOIN:
6560 /* If I have a single element the unpacked type
6561 * is that element.
6562 */
6563 if (type->elements == 1) {
6564 type = type->left;
6565 }
6566 /* I can't in general unpack union types */
6567 break;
6568 default:
6569 /* If I'm not a compound type I can't unpack it */
6570 break;
6571 }
6572 } while(start_type != type);
6573 switch(type->type & TYPE_MASK) {
6574 case TYPE_STRUCT:
6575 case TYPE_ARRAY:
6576 case TYPE_TUPLE:
6577 internal_error(state, 0, "irredicible type?");
6578 break;
6579 }
6580 return type;
6581}
6582
6583static int equiv_types(struct type *left, struct type *right);
6584static int is_compound_type(struct type *type);
6585
6586static struct type *reg_type(
6587 struct compile_state *state, struct type *type, int reg_offset)
6588{
6589 struct type *member;
6590 size_t size;
6591#if 1
6592 struct type *invalid;
6593 invalid = invalid_type(state, type);
6594 if (invalid) {
6595 fprintf(state->errout, "type: ");
6596 name_of(state->errout, type);
6597 fprintf(state->errout, "\n");
6598 fprintf(state->errout, "invalid: ");
6599 name_of(state->errout, invalid);
6600 fprintf(state->errout, "\n");
6601 internal_error(state, 0, "bad input type?");
6602 }
6603#endif
6604
6605 size = reg_size_of(state, type);
6606 if (reg_offset > size) {
6607 member = 0;
6608 fprintf(state->errout, "type: ");
6609 name_of(state->errout, type);
6610 fprintf(state->errout, "\n");
6611 internal_error(state, 0, "offset outside of type");
6612 }
6613 else {
6614 switch(type->type & TYPE_MASK) {
6615 /* Don't do anything with the basic types */
6616 case TYPE_VOID:
6617 case TYPE_CHAR: case TYPE_UCHAR:
6618 case TYPE_SHORT: case TYPE_USHORT:
6619 case TYPE_INT: case TYPE_UINT:
6620 case TYPE_LONG: case TYPE_ULONG:
6621 case TYPE_LLONG: case TYPE_ULLONG:
6622 case TYPE_FLOAT: case TYPE_DOUBLE:
6623 case TYPE_LDOUBLE:
6624 case TYPE_POINTER:
6625 case TYPE_ENUM:
6626 case TYPE_BITFIELD:
6627 member = type;
6628 break;
6629 case TYPE_ARRAY:
6630 member = type->left;
6631 size = reg_size_of(state, member);
6632 if (size > REG_SIZEOF_REG) {
6633 member = reg_type(state, member, reg_offset % size);
6634 }
6635 break;
6636 case TYPE_STRUCT:
6637 case TYPE_TUPLE:
6638 {
6639 size_t offset;
6640 offset = 0;
6641 member = type->left;
6642 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6643 size = reg_size_of(state, member->left);
6644 offset += reg_needed_padding(state, member->left, offset);
6645 if ((offset + size) > reg_offset) {
6646 member = member->left;
6647 break;
6648 }
6649 offset += size;
6650 member = member->right;
6651 }
6652 offset += reg_needed_padding(state, member, offset);
6653 member = reg_type(state, member, reg_offset - offset);
6654 break;
6655 }
6656 case TYPE_UNION:
6657 case TYPE_JOIN:
6658 {
6659 struct type *join, **jnext, *mnext;
6660 join = new_type(TYPE_JOIN, 0, 0);
6661 jnext = &join->left;
6662 mnext = type->left;
6663 while(mnext) {
6664 size_t size;
6665 member = mnext;
6666 mnext = 0;
6667 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6668 mnext = member->right;
6669 member = member->left;
6670 }
6671 size = reg_size_of(state, member);
6672 if (size > reg_offset) {
6673 struct type *part, *hunt;
6674 part = reg_type(state, member, reg_offset);
6675 /* See if this type is already in the union */
6676 hunt = join->left;
6677 while(hunt) {
6678 struct type *test = hunt;
6679 hunt = 0;
6680 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6681 hunt = test->right;
6682 test = test->left;
6683 }
6684 if (equiv_types(part, test)) {
6685 goto next;
6686 }
6687 }
6688 /* Nope add it */
6689 if (!*jnext) {
6690 *jnext = part;
6691 } else {
6692 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6693 jnext = &(*jnext)->right;
6694 }
6695 join->elements++;
6696 }
6697 next:
6698 ;
6699 }
6700 if (join->elements == 0) {
6701 internal_error(state, 0, "No elements?");
6702 }
6703 member = join;
6704 break;
6705 }
6706 default:
6707 member = 0;
6708 fprintf(state->errout, "type: ");
6709 name_of(state->errout, type);
6710 fprintf(state->errout, "\n");
6711 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006712
Eric Biederman90089602004-05-28 14:11:54 +00006713 }
6714 }
6715 /* If I have a single register compound type not a bit-field
6716 * find the real type.
6717 */
6718 member = unpack_type(state, member);
6719 ;
6720 size = reg_size_of(state, member);
6721 if (size > REG_SIZEOF_REG) {
6722 internal_error(state, 0, "Cannot find type of single register");
6723 }
6724#if 1
6725 invalid = invalid_type(state, member);
6726 if (invalid) {
6727 fprintf(state->errout, "type: ");
6728 name_of(state->errout, member);
6729 fprintf(state->errout, "\n");
6730 fprintf(state->errout, "invalid: ");
6731 name_of(state->errout, invalid);
6732 fprintf(state->errout, "\n");
6733 internal_error(state, 0, "returning bad type?");
6734 }
6735#endif
6736 return member;
6737}
6738
Eric Biederman03b59862003-06-24 14:27:37 +00006739static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006740 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006741{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006742 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006743 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6744 internal_error(state, 0, "next_field only works on structures");
6745 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006746 member = type->left;
6747 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006748 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006749 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006750 break;
6751 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006752 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006753 prev_member = 0;
6754 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006755 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006756 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006757 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006758 prev_member = 0;
6759 }
6760 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006761 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006762 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006763 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006764 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006765}
6766
Stefan Reinauer14e22772010-04-27 06:56:47 +00006767typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006768 size_t ret_offset, size_t mem_offset, void *arg);
6769
6770static void walk_type_fields(struct compile_state *state,
6771 struct type *type, size_t reg_offset, size_t mem_offset,
6772 walk_type_fields_cb_t cb, void *arg);
6773
6774static void walk_struct_fields(struct compile_state *state,
6775 struct type *type, size_t reg_offset, size_t mem_offset,
6776 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006777{
Eric Biederman90089602004-05-28 14:11:54 +00006778 struct type *tptr;
6779 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006780 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006781 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006782 }
Eric Biederman90089602004-05-28 14:11:54 +00006783 tptr = type->left;
6784 for(i = 0; i < type->elements; i++) {
6785 struct type *mtype;
6786 mtype = tptr;
6787 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6788 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006789 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006790 walk_type_fields(state, mtype,
6791 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006792 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006793 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006794 field_offset(state, type, mtype->field_ident),
6795 cb, arg);
6796 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006797 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006798
Eric Biederman90089602004-05-28 14:11:54 +00006799}
6800
6801static void walk_type_fields(struct compile_state *state,
6802 struct type *type, size_t reg_offset, size_t mem_offset,
6803 walk_type_fields_cb_t cb, void *arg)
6804{
6805 switch(type->type & TYPE_MASK) {
6806 case TYPE_STRUCT:
6807 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6808 break;
6809 case TYPE_CHAR:
6810 case TYPE_UCHAR:
6811 case TYPE_SHORT:
6812 case TYPE_USHORT:
6813 case TYPE_INT:
6814 case TYPE_UINT:
6815 case TYPE_LONG:
6816 case TYPE_ULONG:
6817 cb(state, type, reg_offset, mem_offset, arg);
6818 break;
6819 case TYPE_VOID:
6820 break;
6821 default:
6822 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006823 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006824}
6825
Eric Biedermanb138ac82003-04-22 18:44:01 +00006826static void arrays_complete(struct compile_state *state, struct type *type)
6827{
6828 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6829 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6830 error(state, 0, "array size not specified");
6831 }
6832 arrays_complete(state, type->left);
6833 }
6834}
6835
Eric Biederman90089602004-05-28 14:11:54 +00006836static unsigned int get_basic_type(struct type *type)
6837{
6838 unsigned int basic;
6839 basic = type->type & TYPE_MASK;
6840 /* Convert enums to ints */
6841 if (basic == TYPE_ENUM) {
6842 basic = TYPE_INT;
6843 }
6844 /* Convert bitfields to standard types */
6845 else if (basic == TYPE_BITFIELD) {
6846 if (type->elements <= SIZEOF_CHAR) {
6847 basic = TYPE_CHAR;
6848 }
6849 else if (type->elements <= SIZEOF_SHORT) {
6850 basic = TYPE_SHORT;
6851 }
6852 else if (type->elements <= SIZEOF_INT) {
6853 basic = TYPE_INT;
6854 }
6855 else if (type->elements <= SIZEOF_LONG) {
6856 basic = TYPE_LONG;
6857 }
6858 if (!TYPE_SIGNED(type->left->type)) {
6859 basic += 1;
6860 }
6861 }
6862 return basic;
6863}
6864
Eric Biedermanb138ac82003-04-22 18:44:01 +00006865static unsigned int do_integral_promotion(unsigned int type)
6866{
Eric Biederman83b991a2003-10-11 06:20:25 +00006867 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006868 type = TYPE_INT;
6869 }
6870 return type;
6871}
6872
6873static unsigned int do_arithmetic_conversion(
6874 unsigned int left, unsigned int right)
6875{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006876 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6877 return TYPE_LDOUBLE;
6878 }
6879 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6880 return TYPE_DOUBLE;
6881 }
6882 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6883 return TYPE_FLOAT;
6884 }
6885 left = do_integral_promotion(left);
6886 right = do_integral_promotion(right);
6887 /* If both operands have the same size done */
6888 if (left == right) {
6889 return left;
6890 }
6891 /* If both operands have the same signedness pick the larger */
6892 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6893 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6894 }
6895 /* If the signed type can hold everything use it */
6896 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6897 return left;
6898 }
6899 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6900 return right;
6901 }
6902 /* Convert to the unsigned type with the same rank as the signed type */
6903 else if (TYPE_SIGNED(left)) {
6904 return TYPE_MKUNSIGNED(left);
6905 }
6906 else {
6907 return TYPE_MKUNSIGNED(right);
6908 }
6909}
6910
6911/* see if two types are the same except for qualifiers */
6912static int equiv_types(struct type *left, struct type *right)
6913{
6914 unsigned int type;
6915 /* Error if the basic types do not match */
6916 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6917 return 0;
6918 }
6919 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006920 /* If the basic types match and it is a void type we are done */
6921 if (type == TYPE_VOID) {
6922 return 1;
6923 }
Eric Biederman90089602004-05-28 14:11:54 +00006924 /* For bitfields we need to compare the sizes */
6925 else if (type == TYPE_BITFIELD) {
6926 return (left->elements == right->elements) &&
6927 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6928 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006929 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006930 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006931 return 1;
6932 }
6933 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006934 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006935 return equiv_types(left->left, right->left);
6936 }
6937 else if (type == TYPE_ARRAY) {
6938 return (left->elements == right->elements) &&
6939 equiv_types(left->left, right->left);
6940 }
Eric Biederman90089602004-05-28 14:11:54 +00006941 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006942 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006943 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006944 }
Eric Biederman90089602004-05-28 14:11:54 +00006945 /* test for union equality */
6946 else if (type == TYPE_UNION) {
6947 return left->type_ident == right->type_ident;
6948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006949 /* Test for equivalent functions */
6950 else if (type == TYPE_FUNCTION) {
6951 return equiv_types(left->left, right->left) &&
6952 equiv_types(left->right, right->right);
6953 }
6954 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006955 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006956 else if (type == TYPE_PRODUCT) {
6957 return equiv_types(left->left, right->left) &&
6958 equiv_types(left->right, right->right);
6959 }
Eric Biederman90089602004-05-28 14:11:54 +00006960 /* We should see TYPE_OVERLAP when comparing joins */
6961 else if (type == TYPE_OVERLAP) {
6962 return equiv_types(left->left, right->left) &&
6963 equiv_types(left->right, right->right);
6964 }
6965 /* Test for equivalence of tuples */
6966 else if (type == TYPE_TUPLE) {
6967 return (left->elements == right->elements) &&
6968 equiv_types(left->left, right->left);
6969 }
6970 /* Test for equivalence of joins */
6971 else if (type == TYPE_JOIN) {
6972 return (left->elements == right->elements) &&
6973 equiv_types(left->left, right->left);
6974 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006975 else {
6976 return 0;
6977 }
6978}
6979
6980static int equiv_ptrs(struct type *left, struct type *right)
6981{
6982 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6983 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6984 return 0;
6985 }
6986 return equiv_types(left->left, right->left);
6987}
6988
6989static struct type *compatible_types(struct type *left, struct type *right)
6990{
6991 struct type *result;
6992 unsigned int type, qual_type;
6993 /* Error if the basic types do not match */
6994 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6995 return 0;
6996 }
6997 type = left->type & TYPE_MASK;
6998 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6999 result = 0;
7000 /* if the basic types match and it is an arithmetic type we are done */
7001 if (TYPE_ARITHMETIC(type)) {
7002 result = new_type(qual_type, 0, 0);
7003 }
7004 /* If it is a pointer type recurse and keep testing */
7005 else if (type == TYPE_POINTER) {
7006 result = compatible_types(left->left, right->left);
7007 if (result) {
7008 result = new_type(qual_type, result, 0);
7009 }
7010 }
Eric Biederman90089602004-05-28 14:11:54 +00007011 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007012 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007013 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007014 result = left;
7015 }
7016 }
Eric Biederman90089602004-05-28 14:11:54 +00007017 /* test for union equality */
7018 else if (type == TYPE_UNION) {
7019 if (left->type_ident == right->type_ident) {
7020 result = left;
7021 }
7022 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007023 /* Test for equivalent functions */
7024 else if (type == TYPE_FUNCTION) {
7025 struct type *lf, *rf;
7026 lf = compatible_types(left->left, right->left);
7027 rf = compatible_types(left->right, right->right);
7028 if (lf && rf) {
7029 result = new_type(qual_type, lf, rf);
7030 }
7031 }
7032 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7033 else if (type == TYPE_PRODUCT) {
7034 struct type *lf, *rf;
7035 lf = compatible_types(left->left, right->left);
7036 rf = compatible_types(left->right, right->right);
7037 if (lf && rf) {
7038 result = new_type(qual_type, lf, rf);
7039 }
7040 }
7041 else {
7042 /* Nothing else is compatible */
7043 }
7044 return result;
7045}
7046
Eric Biederman90089602004-05-28 14:11:54 +00007047/* See if left is a equivalent to right or right is a union member of left */
7048static int is_subset_type(struct type *left, struct type *right)
7049{
7050 if (equiv_types(left, right)) {
7051 return 1;
7052 }
7053 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7054 struct type *member, *mnext;
7055 mnext = left->left;
7056 while(mnext) {
7057 member = mnext;
7058 mnext = 0;
7059 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7060 mnext = member->right;
7061 member = member->left;
7062 }
7063 if (is_subset_type( member, right)) {
7064 return 1;
7065 }
7066 }
7067 }
7068 return 0;
7069}
7070
Eric Biedermanb138ac82003-04-22 18:44:01 +00007071static struct type *compatible_ptrs(struct type *left, struct type *right)
7072{
7073 struct type *result;
7074 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7075 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7076 return 0;
7077 }
7078 result = compatible_types(left->left, right->left);
7079 if (result) {
7080 unsigned int qual_type;
7081 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7082 result = new_type(qual_type, result, 0);
7083 }
7084 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007085
Eric Biedermanb138ac82003-04-22 18:44:01 +00007086}
7087static struct triple *integral_promotion(
7088 struct compile_state *state, struct triple *def)
7089{
7090 struct type *type;
7091 type = def->type;
7092 /* As all operations are carried out in registers
7093 * the values are converted on load I just convert
7094 * logical type of the operand.
7095 */
7096 if (TYPE_INTEGER(type->type)) {
7097 unsigned int int_type;
7098 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007099 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007100 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007101 if (def->op != OP_LOAD) {
7102 def->type = new_type(int_type, 0, 0);
7103 }
7104 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007105 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007106 new_type(int_type, 0, 0), def, 0);
7107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007108 }
7109 }
7110 return def;
7111}
7112
7113
7114static void arithmetic(struct compile_state *state, struct triple *def)
7115{
7116 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007117 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007118 }
7119}
7120
7121static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7122{
7123 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7124 error(state, def, "pointer or arithmetic type expected");
7125 }
7126}
7127
7128static int is_integral(struct triple *ins)
7129{
7130 return TYPE_INTEGER(ins->type->type);
7131}
7132
7133static void integral(struct compile_state *state, struct triple *def)
7134{
7135 if (!is_integral(def)) {
7136 error(state, 0, "integral type expected");
7137 }
7138}
7139
7140
7141static void bool(struct compile_state *state, struct triple *def)
7142{
7143 if (!TYPE_ARITHMETIC(def->type->type) &&
7144 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7145 error(state, 0, "arithmetic or pointer type expected");
7146 }
7147}
7148
7149static int is_signed(struct type *type)
7150{
Eric Biederman90089602004-05-28 14:11:54 +00007151 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7152 type = type->left;
7153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007154 return !!TYPE_SIGNED(type->type);
7155}
Eric Biederman90089602004-05-28 14:11:54 +00007156static int is_compound_type(struct type *type)
7157{
7158 int is_compound;
7159 switch((type->type & TYPE_MASK)) {
7160 case TYPE_ARRAY:
7161 case TYPE_STRUCT:
7162 case TYPE_TUPLE:
7163 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007164 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007165 is_compound = 1;
7166 break;
7167 default:
7168 is_compound = 0;
7169 break;
7170 }
7171 return is_compound;
7172}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007173
Eric Biederman0babc1c2003-05-09 02:39:00 +00007174/* Is this value located in a register otherwise it must be in memory */
7175static int is_in_reg(struct compile_state *state, struct triple *def)
7176{
7177 int in_reg;
7178 if (def->op == OP_ADECL) {
7179 in_reg = 1;
7180 }
7181 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7182 in_reg = 0;
7183 }
Eric Biederman90089602004-05-28 14:11:54 +00007184 else if (triple_is_part(state, def)) {
7185 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007186 }
7187 else {
Eric Biederman90089602004-05-28 14:11:54 +00007188 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007189 in_reg = -1;
7190 }
7191 return in_reg;
7192}
7193
Eric Biederman90089602004-05-28 14:11:54 +00007194/* Is this an auto or static variable location? Something that can
7195 * be assigned to. Otherwise it must must be a pure value, a temporary.
7196 */
7197static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007198{
7199 int ret;
7200 ret = 0;
7201 if (!def) {
7202 return 0;
7203 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007204 if ((def->op == OP_ADECL) ||
7205 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007206 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007207 (def->op == OP_BLOBCONST) ||
7208 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007209 ret = 1;
7210 }
Eric Biederman90089602004-05-28 14:11:54 +00007211 else if (triple_is_part(state, def)) {
7212 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007213 }
7214 return ret;
7215}
7216
Eric Biederman00443072003-06-24 12:34:45 +00007217static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007218{
7219 if (!def) {
7220 internal_error(state, def, "nothing where lvalue expected?");
7221 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007222 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007223 error(state, def, "lvalue expected");
7224 }
7225}
Eric Biederman00443072003-06-24 12:34:45 +00007226static void lvalue(struct compile_state *state, struct triple *def)
7227{
7228 clvalue(state, def);
7229 if (def->type->type & QUAL_CONST) {
7230 error(state, def, "modifable lvalue expected");
7231 }
7232}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007233
7234static int is_pointer(struct triple *def)
7235{
7236 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7237}
7238
7239static void pointer(struct compile_state *state, struct triple *def)
7240{
7241 if (!is_pointer(def)) {
7242 error(state, def, "pointer expected");
7243 }
7244}
7245
7246static struct triple *int_const(
7247 struct compile_state *state, struct type *type, ulong_t value)
7248{
7249 struct triple *result;
7250 switch(type->type & TYPE_MASK) {
7251 case TYPE_CHAR:
7252 case TYPE_INT: case TYPE_UINT:
7253 case TYPE_LONG: case TYPE_ULONG:
7254 break;
7255 default:
Eric Biederman90089602004-05-28 14:11:54 +00007256 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007257 }
7258 result = triple(state, OP_INTCONST, type, 0, 0);
7259 result->u.cval = value;
7260 return result;
7261}
7262
7263
Eric Biederman83b991a2003-10-11 06:20:25 +00007264static struct triple *read_expr(struct compile_state *state, struct triple *def);
7265
Stefan Reinauer14e22772010-04-27 06:56:47 +00007266static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007267 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007268{
7269 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007270 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007271 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007272
Eric Biederman90089602004-05-28 14:11:54 +00007273 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007274
Stefan Reinauer14e22772010-04-27 06:56:47 +00007275
Eric Biedermanb138ac82003-04-22 18:44:01 +00007276 result = 0;
7277 if (expr->op == OP_ADECL) {
7278 error(state, expr, "address of auto variables not supported");
7279 }
7280 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007281 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007282 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007283 result->u.cval = offset;
7284 }
7285 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007286 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007287 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007288 int_const(state, &ulong_type, offset));
7289 }
Eric Biederman90089602004-05-28 14:11:54 +00007290 else if (expr->op == OP_BLOBCONST) {
7291 FINISHME();
7292 internal_error(state, expr, "not yet implemented");
7293 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007294 else if (expr->op == OP_LIST) {
7295 error(state, 0, "Function addresses not supported");
7296 }
Eric Biederman90089602004-05-28 14:11:54 +00007297 else if (triple_is_part(state, expr)) {
7298 struct triple *part;
7299 part = expr;
7300 expr = MISC(expr, 0);
7301 if (part->op == OP_DOT) {
7302 offset += bits_to_bytes(
7303 field_offset(state, expr->type, part->u.field));
7304 }
7305 else if (part->op == OP_INDEX) {
7306 offset += bits_to_bytes(
7307 index_offset(state, expr->type, part->u.cval));
7308 }
7309 else {
7310 internal_error(state, part, "unhandled part type");
7311 }
7312 result = do_mk_addr_expr(state, expr, type, offset);
7313 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007314 if (!result) {
7315 internal_error(state, expr, "cannot take address of expression");
7316 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007317 return result;
7318}
7319
Eric Biederman0babc1c2003-05-09 02:39:00 +00007320static struct triple *mk_addr_expr(
7321 struct compile_state *state, struct triple *expr, ulong_t offset)
7322{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007323 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007324}
7325
Eric Biedermanb138ac82003-04-22 18:44:01 +00007326static struct triple *mk_deref_expr(
7327 struct compile_state *state, struct triple *expr)
7328{
7329 struct type *base_type;
7330 pointer(state, expr);
7331 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007332 return triple(state, OP_DEREF, base_type, expr, 0);
7333}
7334
Eric Biederman90089602004-05-28 14:11:54 +00007335/* lvalue conversions always apply except when certain operators
7336 * are applied. So I apply apply it when I know no more
7337 * operators will be applied.
7338 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007339static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007340{
Eric Biederman5cd81732004-03-11 15:01:31 +00007341 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007342 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7343 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007344 type = new_type(
7345 TYPE_POINTER | (def->type->type & QUAL_MASK),
7346 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007347 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007348 struct triple *addrconst;
7349 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7350 internal_error(state, def, "bad array constant");
7351 }
7352 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7353 MISC(addrconst, 0) = def;
7354 def = addrconst;
7355 }
7356 else {
Eric Biederman90089602004-05-28 14:11:54 +00007357 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007358 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007359 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007360 /* Transform a function to a pointer to it */
7361 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7362 def = mk_addr_expr(state, def, 0);
7363 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007364 return def;
7365}
7366
Eric Biederman0babc1c2003-05-09 02:39:00 +00007367static struct triple *deref_field(
7368 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7369{
7370 struct triple *result;
7371 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007372 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007373 if (!field) {
7374 internal_error(state, 0, "No field passed to deref_field");
7375 }
7376 result = 0;
7377 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007378 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7379 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007380 error(state, 0, "request for member %s in something not a struct or union",
7381 field->name);
7382 }
Eric Biederman03b59862003-06-24 14:27:37 +00007383 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007384 if ((type->type & STOR_MASK) == STOR_PERM) {
7385 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007386 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007387 result = do_mk_addr_expr(state, expr, member, offset);
7388 result = mk_deref_expr(state, result);
7389 }
7390 else {
7391 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007392 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007393 result->u.field = field;
7394 }
7395 return result;
7396}
7397
Eric Biederman90089602004-05-28 14:11:54 +00007398static struct triple *deref_index(
7399 struct compile_state *state, struct triple *expr, size_t index)
7400{
7401 struct triple *result;
7402 struct type *type, *member;
7403 ulong_t offset;
7404
7405 result = 0;
7406 type = expr->type;
7407 member = index_type(state, type, index);
7408
7409 if ((type->type & STOR_MASK) == STOR_PERM) {
7410 offset = bits_to_bytes(index_offset(state, type, index));
7411 result = do_mk_addr_expr(state, expr, member, offset);
7412 result = mk_deref_expr(state, result);
7413 }
7414 else {
7415 result = triple(state, OP_INDEX, member, expr, 0);
7416 result->u.cval = index;
7417 }
7418 return result;
7419}
7420
Eric Biedermanb138ac82003-04-22 18:44:01 +00007421static struct triple *read_expr(struct compile_state *state, struct triple *def)
7422{
7423 int op;
7424 if (!def) {
7425 return 0;
7426 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007427#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007428#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007429#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007430 /* Transform lvalues into something we can read */
7431 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007432 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007433 return def;
7434 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007435 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007436 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007437 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007438 if (def->op == OP_SDECL) {
7439 def = mk_addr_expr(state, def, 0);
7440 def = mk_deref_expr(state, def);
7441 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007442 op = OP_LOAD;
7443 }
Eric Biederman90089602004-05-28 14:11:54 +00007444 def = triple(state, op, def->type, def, 0);
7445 if (def->type->type & QUAL_VOLATILE) {
7446 def->id |= TRIPLE_FLAG_VOLATILE;
7447 }
7448 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007449}
7450
Stefan Reinauer14e22772010-04-27 06:56:47 +00007451int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007452 struct type *dest, struct type *rval)
7453{
7454 int compatible = 0;
7455 /* Both operands have arithmetic type */
7456 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7457 compatible = 1;
7458 }
7459 /* One operand is a pointer and the other is a pointer to void */
7460 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7461 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7462 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7463 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7464 compatible = 1;
7465 }
7466 /* If both types are the same without qualifiers we are good */
7467 else if (equiv_ptrs(dest, rval)) {
7468 compatible = 1;
7469 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007470 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007471 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007472 compatible = 1;
7473 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007474 return compatible;
7475}
7476
Eric Biedermane058a1e2003-07-12 01:21:31 +00007477static void write_compatible(struct compile_state *state,
7478 struct type *dest, struct type *rval)
7479{
7480 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007481 FILE *fp = state->errout;
7482 fprintf(fp, "dest: ");
7483 name_of(fp, dest);
7484 fprintf(fp,"\nrval: ");
7485 name_of(fp, rval);
7486 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007487 error(state, 0, "Incompatible types in assignment");
7488 }
7489}
7490
Eric Biedermane058a1e2003-07-12 01:21:31 +00007491static int is_init_compatible(struct compile_state *state,
7492 struct type *dest, struct type *rval)
7493{
7494 int compatible = 0;
7495 if (is_write_compatible(state, dest, rval)) {
7496 compatible = 1;
7497 }
7498 else if (equiv_types(dest, rval)) {
7499 compatible = 1;
7500 }
7501 return compatible;
7502}
7503
Eric Biedermanb138ac82003-04-22 18:44:01 +00007504static struct triple *write_expr(
7505 struct compile_state *state, struct triple *dest, struct triple *rval)
7506{
7507 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007508
7509 def = 0;
7510 if (!rval) {
7511 internal_error(state, 0, "missing rval");
7512 }
7513
7514 if (rval->op == OP_LIST) {
7515 internal_error(state, 0, "expression of type OP_LIST?");
7516 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007517 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007518 internal_error(state, 0, "writing to a non lvalue?");
7519 }
Eric Biederman00443072003-06-24 12:34:45 +00007520 if (dest->type->type & QUAL_CONST) {
7521 internal_error(state, 0, "modifable lvalue expexted");
7522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007523
7524 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007525 if (!equiv_types(dest->type, rval->type)) {
7526 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7527 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007528
7529 /* Now figure out which assignment operator to use */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007530 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007531 def = triple(state, OP_WRITE, dest->type, rval, dest);
7532 if (MISC(def, 0) != dest) {
7533 internal_error(state, def, "huh?");
7534 }
7535 if (RHS(def, 0) != rval) {
7536 internal_error(state, def, "huh?");
7537 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007538 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007539 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007540 }
Eric Biederman90089602004-05-28 14:11:54 +00007541 if (def->type->type & QUAL_VOLATILE) {
7542 def->id |= TRIPLE_FLAG_VOLATILE;
7543 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007544 return def;
7545}
7546
7547static struct triple *init_expr(
7548 struct compile_state *state, struct triple *dest, struct triple *rval)
7549{
7550 struct triple *def;
7551
7552 def = 0;
7553 if (!rval) {
7554 internal_error(state, 0, "missing rval");
7555 }
7556 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7557 rval = read_expr(state, rval);
7558 def = write_expr(state, dest, rval);
7559 }
7560 else {
7561 /* Fill in the array size if necessary */
7562 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7563 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7564 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7565 dest->type->elements = rval->type->elements;
7566 }
7567 }
7568 if (!equiv_types(dest->type, rval->type)) {
7569 error(state, 0, "Incompatible types in inializer");
7570 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007571 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007572 insert_triple(state, dest, rval);
7573 rval->id |= TRIPLE_FLAG_FLATTENED;
7574 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007575 }
7576 return def;
7577}
7578
7579struct type *arithmetic_result(
7580 struct compile_state *state, struct triple *left, struct triple *right)
7581{
7582 struct type *type;
7583 /* Sanity checks to ensure I am working with arithmetic types */
7584 arithmetic(state, left);
7585 arithmetic(state, right);
7586 type = new_type(
7587 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007588 get_basic_type(left->type),
7589 get_basic_type(right->type)),
7590 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007591 return type;
7592}
7593
7594struct type *ptr_arithmetic_result(
7595 struct compile_state *state, struct triple *left, struct triple *right)
7596{
7597 struct type *type;
7598 /* Sanity checks to ensure I am working with the proper types */
7599 ptr_arithmetic(state, left);
7600 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007601 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007602 TYPE_ARITHMETIC(right->type->type)) {
7603 type = arithmetic_result(state, left, right);
7604 }
7605 else if (TYPE_PTR(left->type->type)) {
7606 type = left->type;
7607 }
7608 else {
7609 internal_error(state, 0, "huh?");
7610 type = 0;
7611 }
7612 return type;
7613}
7614
Eric Biedermanb138ac82003-04-22 18:44:01 +00007615/* boolean helper function */
7616
Stefan Reinauer14e22772010-04-27 06:56:47 +00007617static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007618 struct triple *expr)
7619{
7620 switch(expr->op) {
7621 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7622 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7623 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7624 /* If the expression is already boolean do nothing */
7625 break;
7626 default:
7627 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7628 break;
7629 }
7630 return expr;
7631}
7632
Stefan Reinauer14e22772010-04-27 06:56:47 +00007633static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007634 struct triple *expr)
7635{
7636 return triple(state, OP_LFALSE, &int_type, expr, 0);
7637}
7638
Eric Biederman90089602004-05-28 14:11:54 +00007639static struct triple *mkland_expr(
7640 struct compile_state *state,
7641 struct triple *left, struct triple *right)
7642{
7643 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007644 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007645
7646 /* Generate some intermediate triples */
7647 end = label(state);
7648 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007649
Eric Biederman90089602004-05-28 14:11:54 +00007650 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007651 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007652
7653 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007654 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007655 lfalse_expr(state, read_expr(state, var)));
7656 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007657
Eric Biederman90089602004-05-28 14:11:54 +00007658 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007659 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007660
7661 /* An expression for the computed value */
7662 val = read_expr(state, var);
7663
7664 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007665 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007666
Eric Biederman90089602004-05-28 14:11:54 +00007667 return def;
7668}
7669
7670static struct triple *mklor_expr(
7671 struct compile_state *state,
7672 struct triple *left, struct triple *right)
7673{
7674 struct triple *def, *val, *var, *jmp, *mid, *end;
7675
7676 /* Generate some intermediate triples */
7677 end = label(state);
7678 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007679
Eric Biederman90089602004-05-28 14:11:54 +00007680 /* Store the left hand side value */
7681 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007682
Eric Biederman90089602004-05-28 14:11:54 +00007683 /* Jump if the value is true */
7684 jmp = branch(state, end, read_expr(state, var));
7685 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007686
Eric Biederman90089602004-05-28 14:11:54 +00007687 /* Store the right hand side value */
7688 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007689
Eric Biederman90089602004-05-28 14:11:54 +00007690 /* An expression for the computed value*/
7691 val = read_expr(state, var);
7692
7693 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007694 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007695
7696 return def;
7697}
7698
7699static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007700 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007701 struct triple *test, struct triple *left, struct triple *right)
7702{
Eric Biederman90089602004-05-28 14:11:54 +00007703 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007704 struct type *result_type;
7705 unsigned int left_type, right_type;
7706 bool(state, test);
7707 left_type = left->type->type;
7708 right_type = right->type->type;
7709 result_type = 0;
7710 /* Both operands have arithmetic type */
7711 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7712 result_type = arithmetic_result(state, left, right);
7713 }
7714 /* Both operands have void type */
7715 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7716 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7717 result_type = &void_type;
7718 }
7719 /* pointers to the same type... */
7720 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7721 ;
7722 }
7723 /* Both operands are pointers and left is a pointer to void */
7724 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7725 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7726 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7727 result_type = right->type;
7728 }
7729 /* Both operands are pointers and right is a pointer to void */
7730 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7731 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7732 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7733 result_type = left->type;
7734 }
7735 if (!result_type) {
7736 error(state, 0, "Incompatible types in conditional expression");
7737 }
Eric Biederman90089602004-05-28 14:11:54 +00007738 /* Generate some intermediate triples */
7739 mid = label(state);
7740 end = label(state);
7741 var = variable(state, result_type);
7742
7743 /* Branch if the test is false */
7744 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7745 top = label(state);
7746
7747 /* Store the left hand side value */
7748 left = write_expr(state, var, left);
7749
7750 /* Branch to the end */
7751 jmp2 = branch(state, end, 0);
7752
7753 /* Store the right hand side value */
7754 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007755
Eric Biederman90089602004-05-28 14:11:54 +00007756 /* An expression for the computed value */
7757 val = read_expr(state, var);
7758
7759 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007760 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007761
Eric Biedermanb138ac82003-04-22 18:44:01 +00007762 return def;
7763}
7764
7765
Eric Biederman0babc1c2003-05-09 02:39:00 +00007766static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007767{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007768#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007769#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007770#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007771 int count;
7772 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007773 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7774 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007775 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007776 else if (ins->op == OP_DEREF) {
7777 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007778 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007779 else if (ins->op == OP_VAL) {
7780 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007781 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007782 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007783 /* Don't figure the depth of a call just guess it is huge */
7784 count = 1000;
7785 }
7786 else {
7787 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007788 expr = triple_rhs(state, ins, 0);
7789 for(;expr; expr = triple_rhs(state, ins, expr)) {
7790 if (*expr) {
7791 int depth;
7792 depth = expr_depth(state, *expr);
7793 if (depth > count) {
7794 count = depth;
7795 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007796 }
7797 }
7798 }
7799 return count + 1;
7800}
7801
Eric Biederman0babc1c2003-05-09 02:39:00 +00007802static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007803 struct compile_state *state, struct triple *first, struct triple *ptr,
7804 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007805{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007806 struct rhs_vector {
7807 int depth;
7808 struct triple **ins;
7809 } vector[MAX_RHS];
7810 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007811 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007812 rhs = ptr->rhs;
7813 lhs = ptr->lhs;
7814 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007815 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007816 ptr->op, tops(ptr->op));
7817 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007818 /* Find the depth of the rhs elements */
7819 for(i = 0; i < rhs; i++) {
7820 vector[i].ins = &RHS(ptr, i);
7821 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007822 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007823 /* Selection sort the rhs */
7824 for(i = 0; i < rhs; i++) {
7825 int j, max = i;
7826 for(j = i + 1; j < rhs; j++ ) {
7827 if (vector[j].depth > vector[max].depth) {
7828 max = j;
7829 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007830 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007831 if (max != i) {
7832 struct rhs_vector tmp;
7833 tmp = vector[i];
7834 vector[i] = vector[max];
7835 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007836 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007837 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007838 /* Now flatten the rhs elements */
7839 for(i = 0; i < rhs; i++) {
7840 *vector[i].ins = flatten(state, first, *vector[i].ins);
7841 use_triple(*vector[i].ins, ptr);
7842 }
Eric Biederman90089602004-05-28 14:11:54 +00007843 if (lhs) {
7844 insert_triple(state, first, ptr);
7845 ptr->id |= TRIPLE_FLAG_FLATTENED;
7846 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007847
Eric Biederman90089602004-05-28 14:11:54 +00007848 /* Now flatten the lhs elements */
7849 for(i = 0; i < lhs; i++) {
7850 struct triple **ins = &LHS(ptr, i);
7851 *ins = flatten(state, first, *ins);
7852 use_triple(*ins, ptr);
7853 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007854 }
7855 return ptr;
7856}
7857
Eric Biederman90089602004-05-28 14:11:54 +00007858static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007859 struct compile_state *state, struct triple *first, struct triple *ptr)
7860{
Eric Biederman90089602004-05-28 14:11:54 +00007861 struct triple *head, *body, *val;
7862 head = RHS(ptr, 0);
7863 RHS(ptr, 0) = 0;
7864 val = head->prev;
7865 body = head->next;
7866 release_triple(state, head);
7867 release_triple(state, ptr);
7868 val->next = first;
7869 body->prev = first->prev;
7870 body->prev->next = body;
7871 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007872
Eric Biederman90089602004-05-28 14:11:54 +00007873 if (triple_is_cbranch(state, body->prev) ||
7874 triple_is_call(state, body->prev)) {
7875 unuse_triple(first, body->prev);
7876 use_triple(body, body->prev);
7877 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007878
Eric Biederman90089602004-05-28 14:11:54 +00007879 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7880 internal_error(state, val, "val not flattened?");
7881 }
7882
7883 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007884}
7885
Eric Biederman90089602004-05-28 14:11:54 +00007886
7887static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007888 struct compile_state *state, struct triple *first, struct triple *ptr)
7889{
Eric Biederman90089602004-05-28 14:11:54 +00007890 if (!triple_is_part(state, ptr)) {
7891 internal_error(state, ptr, "not a part");
7892 }
7893 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7894 internal_error(state, ptr, "unexpected args for: %d %s",
7895 ptr->op, tops(ptr->op));
7896 }
7897 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7898 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007899 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007900}
7901
7902static struct triple *flatten(
7903 struct compile_state *state, struct triple *first, struct triple *ptr)
7904{
7905 struct triple *orig_ptr;
7906 if (!ptr)
7907 return 0;
7908 do {
7909 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007910 /* Only flatten triples once */
7911 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7912 return ptr;
7913 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007914 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007915 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007916 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7917 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007918 break;
Eric Biederman90089602004-05-28 14:11:54 +00007919 case OP_PROG:
7920 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007921 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007922 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007923 ptr = flatten_generic(state, first, ptr, 1);
7924 insert_triple(state, first, ptr);
7925 ptr->id |= TRIPLE_FLAG_FLATTENED;
7926 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7927 if (ptr->next != ptr) {
7928 use_triple(ptr->next, ptr);
7929 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007930 break;
7931 case OP_READ:
7932 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007933 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7934 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007935 break;
Eric Biederman90089602004-05-28 14:11:54 +00007936 case OP_WRITE:
7937 ptr = flatten_generic(state, first, ptr, 1);
7938 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7939 use_triple(MISC(ptr, 0), ptr);
7940 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007941 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007942 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007943 break;
7944 case OP_CBRANCH:
7945 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7946 use_triple(RHS(ptr, 0), ptr);
7947 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007948 insert_triple(state, first, ptr);
7949 ptr->id |= TRIPLE_FLAG_FLATTENED;
7950 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007951 if (ptr->next != ptr) {
7952 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007953 }
7954 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007955 case OP_CALL:
7956 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7957 use_triple(MISC(ptr, 0), ptr);
7958 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007959 insert_triple(state, first, ptr);
7960 ptr->id |= TRIPLE_FLAG_FLATTENED;
7961 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007962 if (ptr->next != ptr) {
7963 use_triple(ptr->next, ptr);
7964 }
7965 break;
7966 case OP_RET:
7967 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7968 use_triple(RHS(ptr, 0), ptr);
7969 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007970 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007971 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007972 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007973 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007974 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007975 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007976 break;
7977 case OP_DEREF:
7978 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007979 ptr = RHS(ptr, 0);
7980 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007981 free_triple(state, orig_ptr);
7982 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007983 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007984 if (RHS(ptr, 0)->op == OP_DEREF) {
7985 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007986 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007987 base = MISC(ptr, 0);
7988 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007989 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007990 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007991 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007992 int_const(state, &ulong_type, offset));
7993 free_triple(state, base);
7994 }
Eric Biederman90089602004-05-28 14:11:54 +00007995 else {
7996 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007997 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007998 break;
Eric Biederman90089602004-05-28 14:11:54 +00007999 case OP_INDEX:
8000 if (RHS(ptr, 0)->op == OP_DEREF) {
8001 struct triple *base, *left;
8002 ulong_t offset;
8003 base = MISC(ptr, 0);
8004 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8005 left = RHS(base, 0);
8006 ptr = triple(state, OP_ADD, left->type,
8007 read_expr(state, left),
8008 int_const(state, &long_type, offset));
8009 free_triple(state, base);
8010 }
8011 else {
8012 ptr = flatten_part(state, first, ptr);
8013 }
8014 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008015 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008016 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008017 use_triple(ptr, MISC(ptr, 0));
8018 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008019 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008020 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8021 use_triple(MISC(ptr, 0), ptr);
8022 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008023 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008024 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008025 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8026 use_triple(MISC(ptr, 0), ptr);
8027 insert_triple(state, first, ptr);
8028 ptr->id |= TRIPLE_FLAG_FLATTENED;
8029 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8030 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008031 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008032 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008033 break;
8034 default:
8035 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008036 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008037 break;
8038 }
8039 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008040 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008041 insert_triple(state, first, ptr);
8042 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008043 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008044 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008045 return ptr;
8046}
8047
8048static void release_expr(struct compile_state *state, struct triple *expr)
8049{
8050 struct triple *head;
8051 head = label(state);
8052 flatten(state, head, expr);
8053 while(head->next != head) {
8054 release_triple(state, head->next);
8055 }
8056 free_triple(state, head);
8057}
8058
8059static int replace_rhs_use(struct compile_state *state,
8060 struct triple *orig, struct triple *new, struct triple *use)
8061{
8062 struct triple **expr;
8063 int found;
8064 found = 0;
8065 expr = triple_rhs(state, use, 0);
8066 for(;expr; expr = triple_rhs(state, use, expr)) {
8067 if (*expr == orig) {
8068 *expr = new;
8069 found = 1;
8070 }
8071 }
8072 if (found) {
8073 unuse_triple(orig, use);
8074 use_triple(new, use);
8075 }
8076 return found;
8077}
8078
8079static int replace_lhs_use(struct compile_state *state,
8080 struct triple *orig, struct triple *new, struct triple *use)
8081{
8082 struct triple **expr;
8083 int found;
8084 found = 0;
8085 expr = triple_lhs(state, use, 0);
8086 for(;expr; expr = triple_lhs(state, use, expr)) {
8087 if (*expr == orig) {
8088 *expr = new;
8089 found = 1;
8090 }
8091 }
8092 if (found) {
8093 unuse_triple(orig, use);
8094 use_triple(new, use);
8095 }
8096 return found;
8097}
8098
Eric Biederman90089602004-05-28 14:11:54 +00008099static int replace_misc_use(struct compile_state *state,
8100 struct triple *orig, struct triple *new, struct triple *use)
8101{
8102 struct triple **expr;
8103 int found;
8104 found = 0;
8105 expr = triple_misc(state, use, 0);
8106 for(;expr; expr = triple_misc(state, use, expr)) {
8107 if (*expr == orig) {
8108 *expr = new;
8109 found = 1;
8110 }
8111 }
8112 if (found) {
8113 unuse_triple(orig, use);
8114 use_triple(new, use);
8115 }
8116 return found;
8117}
8118
8119static int replace_targ_use(struct compile_state *state,
8120 struct triple *orig, struct triple *new, struct triple *use)
8121{
8122 struct triple **expr;
8123 int found;
8124 found = 0;
8125 expr = triple_targ(state, use, 0);
8126 for(;expr; expr = triple_targ(state, use, expr)) {
8127 if (*expr == orig) {
8128 *expr = new;
8129 found = 1;
8130 }
8131 }
8132 if (found) {
8133 unuse_triple(orig, use);
8134 use_triple(new, use);
8135 }
8136 return found;
8137}
8138
8139static void replace_use(struct compile_state *state,
8140 struct triple *orig, struct triple *new, struct triple *use)
8141{
8142 int found;
8143 found = 0;
8144 found |= replace_rhs_use(state, orig, new, use);
8145 found |= replace_lhs_use(state, orig, new, use);
8146 found |= replace_misc_use(state, orig, new, use);
8147 found |= replace_targ_use(state, orig, new, use);
8148 if (!found) {
8149 internal_error(state, use, "use without use");
8150 }
8151}
8152
Eric Biedermanb138ac82003-04-22 18:44:01 +00008153static void propogate_use(struct compile_state *state,
8154 struct triple *orig, struct triple *new)
8155{
8156 struct triple_set *user, *next;
8157 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008158 /* Careful replace_use modifies the use chain and
8159 * removes use. So we must get a copy of the next
8160 * entry early.
8161 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008162 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008163 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008164 }
8165 if (orig->use) {
8166 internal_error(state, orig, "used after propogate_use");
8167 }
8168}
8169
8170/*
8171 * Code generators
8172 * ===========================
8173 */
8174
Eric Biederman90089602004-05-28 14:11:54 +00008175static struct triple *mk_cast_expr(
8176 struct compile_state *state, struct type *type, struct triple *expr)
8177{
8178 struct triple *def;
8179 def = read_expr(state, expr);
8180 def = triple(state, OP_CONVERT, type, def, 0);
8181 return def;
8182}
8183
Eric Biedermanb138ac82003-04-22 18:44:01 +00008184static struct triple *mk_add_expr(
8185 struct compile_state *state, struct triple *left, struct triple *right)
8186{
8187 struct type *result_type;
8188 /* Put pointer operands on the left */
8189 if (is_pointer(right)) {
8190 struct triple *tmp;
8191 tmp = left;
8192 left = right;
8193 right = tmp;
8194 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008195 left = read_expr(state, left);
8196 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008197 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008198 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008199 struct type *ptr_math;
8200 int op;
8201 if (is_signed(right->type)) {
8202 ptr_math = &long_type;
8203 op = OP_SMUL;
8204 } else {
8205 ptr_math = &ulong_type;
8206 op = OP_UMUL;
8207 }
8208 if (!equiv_types(right->type, ptr_math)) {
8209 right = mk_cast_expr(state, ptr_math, right);
8210 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008211 right = triple(state, op, ptr_math, right,
8212 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008213 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008214 }
8215 return triple(state, OP_ADD, result_type, left, right);
8216}
8217
8218static struct triple *mk_sub_expr(
8219 struct compile_state *state, struct triple *left, struct triple *right)
8220{
8221 struct type *result_type;
8222 result_type = ptr_arithmetic_result(state, left, right);
8223 left = read_expr(state, left);
8224 right = read_expr(state, right);
8225 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008226 struct type *ptr_math;
8227 int op;
8228 if (is_signed(right->type)) {
8229 ptr_math = &long_type;
8230 op = OP_SMUL;
8231 } else {
8232 ptr_math = &ulong_type;
8233 op = OP_UMUL;
8234 }
8235 if (!equiv_types(right->type, ptr_math)) {
8236 right = mk_cast_expr(state, ptr_math, right);
8237 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008238 right = triple(state, op, ptr_math, right,
8239 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008240 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008241 }
8242 return triple(state, OP_SUB, result_type, left, right);
8243}
8244
8245static struct triple *mk_pre_inc_expr(
8246 struct compile_state *state, struct triple *def)
8247{
8248 struct triple *val;
8249 lvalue(state, def);
8250 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8251 return triple(state, OP_VAL, def->type,
8252 write_expr(state, def, val),
8253 val);
8254}
8255
8256static struct triple *mk_pre_dec_expr(
8257 struct compile_state *state, struct triple *def)
8258{
8259 struct triple *val;
8260 lvalue(state, def);
8261 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8262 return triple(state, OP_VAL, def->type,
8263 write_expr(state, def, val),
8264 val);
8265}
8266
8267static struct triple *mk_post_inc_expr(
8268 struct compile_state *state, struct triple *def)
8269{
8270 struct triple *val;
8271 lvalue(state, def);
8272 val = read_expr(state, def);
8273 return triple(state, OP_VAL, def->type,
8274 write_expr(state, def,
8275 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8276 , val);
8277}
8278
8279static struct triple *mk_post_dec_expr(
8280 struct compile_state *state, struct triple *def)
8281{
8282 struct triple *val;
8283 lvalue(state, def);
8284 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008285 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008286 write_expr(state, def,
8287 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8288 , val);
8289}
8290
8291static struct triple *mk_subscript_expr(
8292 struct compile_state *state, struct triple *left, struct triple *right)
8293{
8294 left = read_expr(state, left);
8295 right = read_expr(state, right);
8296 if (!is_pointer(left) && !is_pointer(right)) {
8297 error(state, left, "subscripted value is not a pointer");
8298 }
8299 return mk_deref_expr(state, mk_add_expr(state, left, right));
8300}
8301
Eric Biedermane058a1e2003-07-12 01:21:31 +00008302
Eric Biedermanb138ac82003-04-22 18:44:01 +00008303/*
8304 * Compile time evaluation
8305 * ===========================
8306 */
8307static int is_const(struct triple *ins)
8308{
8309 return IS_CONST_OP(ins->op);
8310}
8311
Eric Biederman83b991a2003-10-11 06:20:25 +00008312static int is_simple_const(struct triple *ins)
8313{
Eric Biederman90089602004-05-28 14:11:54 +00008314 /* Is this a constant that u.cval has the value.
8315 * Or equivalently is this a constant that read_const
8316 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008317 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008318 */
8319 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008320}
8321
Stefan Reinauer14e22772010-04-27 06:56:47 +00008322static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008323 struct triple *left, struct triple *right)
8324{
8325 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008326 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8327 equal = 0;
8328 }
8329 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008330 equal = 0;
8331 }
8332 else if (left->op != right->op) {
8333 equal = 0;
8334 }
8335 else if (!equiv_types(left->type, right->type)) {
8336 equal = 0;
8337 }
8338 else {
8339 equal = 0;
8340 switch(left->op) {
8341 case OP_INTCONST:
8342 if (left->u.cval == right->u.cval) {
8343 equal = 1;
8344 }
8345 break;
8346 case OP_BLOBCONST:
8347 {
Eric Biederman90089602004-05-28 14:11:54 +00008348 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008349 lsize = size_of(state, left->type);
8350 rsize = size_of(state, right->type);
8351 if (lsize != rsize) {
8352 break;
8353 }
Eric Biederman90089602004-05-28 14:11:54 +00008354 bytes = bits_to_bytes(lsize);
8355 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008356 equal = 1;
8357 }
8358 break;
8359 }
8360 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008361 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008362 (left->u.cval == right->u.cval)) {
8363 equal = 1;
8364 }
8365 break;
8366 default:
8367 internal_error(state, left, "uknown constant type");
8368 break;
8369 }
8370 }
8371 return equal;
8372}
8373
8374static int is_zero(struct triple *ins)
8375{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008376 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008377}
8378
8379static int is_one(struct triple *ins)
8380{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008381 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008382}
8383
Stefan Reinauer50542a82007-10-24 11:14:14 +00008384#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008385static long_t bit_count(ulong_t value)
8386{
8387 int count;
8388 int i;
8389 count = 0;
8390 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8391 ulong_t mask;
8392 mask = 1;
8393 mask <<= i;
8394 if (value & mask) {
8395 count++;
8396 }
8397 }
8398 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008399
Eric Biederman530b5192003-07-01 10:05:30 +00008400}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008401#endif
8402
Eric Biedermanb138ac82003-04-22 18:44:01 +00008403static long_t bsr(ulong_t value)
8404{
8405 int i;
8406 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8407 ulong_t mask;
8408 mask = 1;
8409 mask <<= i;
8410 if (value & mask) {
8411 return i;
8412 }
8413 }
8414 return -1;
8415}
8416
8417static long_t bsf(ulong_t value)
8418{
8419 int i;
8420 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8421 ulong_t mask;
8422 mask = 1;
8423 mask <<= 1;
8424 if (value & mask) {
8425 return i;
8426 }
8427 }
8428 return -1;
8429}
8430
Eric Biedermancb364952004-11-15 10:46:44 +00008431static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008432{
8433 return bsr(value);
8434}
8435
8436static long_t tlog2(struct triple *ins)
8437{
Eric Biedermancb364952004-11-15 10:46:44 +00008438 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008439}
8440
8441static int is_pow2(struct triple *ins)
8442{
8443 ulong_t value, mask;
8444 long_t log;
8445 if (!is_const(ins)) {
8446 return 0;
8447 }
8448 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008449 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008450 if (log == -1) {
8451 return 0;
8452 }
8453 mask = 1;
8454 mask <<= log;
8455 return ((value & mask) == value);
8456}
8457
8458static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008459 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008460{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008461 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008462 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008463 case TYPE_SHORT:
8464 case TYPE_INT:
8465 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008466 case TYPE_UCHAR:
8467 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008468 case TYPE_UINT:
8469 case TYPE_ULONG:
8470 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008471 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008472 break;
8473 default:
Eric Biederman90089602004-05-28 14:11:54 +00008474 fprintf(state->errout, "type: ");
8475 name_of(state->errout, rhs->type);
8476 fprintf(state->errout, "\n");
8477 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008478 break;
8479 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008480 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008481 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008482 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008483 return rhs->u.cval;
8484}
8485
Eric Biederman5ade04a2003-10-22 04:03:46 +00008486static long_t read_sconst(struct compile_state *state,
8487 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008488{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008489 return (long_t)(rhs->u.cval);
8490}
8491
Eric Biederman5ade04a2003-10-22 04:03:46 +00008492int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8493{
8494 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008495 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008496 }
8497 return !is_zero(rhs);
8498}
8499
8500int const_eq(struct compile_state *state, struct triple *ins,
8501 struct triple *left, struct triple *right)
8502{
8503 int result;
8504 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008505 internal_warning(state, ins, "non const passed to const_eq");
8506 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008507 }
8508 else if (left == right) {
8509 result = 1;
8510 }
8511 else if (is_simple_const(left) && is_simple_const(right)) {
8512 ulong_t lval, rval;
8513 lval = read_const(state, ins, left);
8514 rval = read_const(state, ins, right);
8515 result = (lval == rval);
8516 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008517 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008518 (right->op == OP_ADDRCONST)) {
8519 result = (MISC(left, 0) == MISC(right, 0)) &&
8520 (left->u.cval == right->u.cval);
8521 }
8522 else {
Eric Biederman90089602004-05-28 14:11:54 +00008523 internal_warning(state, ins, "incomparable constants passed to const_eq");
8524 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008525 }
8526 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008527
Eric Biederman5ade04a2003-10-22 04:03:46 +00008528}
8529
8530int const_ucmp(struct compile_state *state, struct triple *ins,
8531 struct triple *left, struct triple *right)
8532{
8533 int result;
8534 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008535 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008536 result = -2;
8537 }
8538 else if (left == right) {
8539 result = 0;
8540 }
8541 else if (is_simple_const(left) && is_simple_const(right)) {
8542 ulong_t lval, rval;
8543 lval = read_const(state, ins, left);
8544 rval = read_const(state, ins, right);
8545 result = 0;
8546 if (lval > rval) {
8547 result = 1;
8548 } else if (rval > lval) {
8549 result = -1;
8550 }
8551 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008552 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008553 (right->op == OP_ADDRCONST) &&
8554 (MISC(left, 0) == MISC(right, 0))) {
8555 result = 0;
8556 if (left->u.cval > right->u.cval) {
8557 result = 1;
8558 } else if (left->u.cval < right->u.cval) {
8559 result = -1;
8560 }
8561 }
8562 else {
Eric Biederman90089602004-05-28 14:11:54 +00008563 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008564 result = -2;
8565 }
8566 return result;
8567}
8568
8569int const_scmp(struct compile_state *state, struct triple *ins,
8570 struct triple *left, struct triple *right)
8571{
8572 int result;
8573 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008574 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008575 result = -2;
8576 }
8577 else if (left == right) {
8578 result = 0;
8579 }
8580 else if (is_simple_const(left) && is_simple_const(right)) {
8581 long_t lval, rval;
8582 lval = read_sconst(state, ins, left);
8583 rval = read_sconst(state, ins, right);
8584 result = 0;
8585 if (lval > rval) {
8586 result = 1;
8587 } else if (rval > lval) {
8588 result = -1;
8589 }
8590 }
8591 else {
Eric Biederman90089602004-05-28 14:11:54 +00008592 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008593 result = -2;
8594 }
8595 return result;
8596}
8597
Eric Biedermanb138ac82003-04-22 18:44:01 +00008598static void unuse_rhs(struct compile_state *state, struct triple *ins)
8599{
8600 struct triple **expr;
8601 expr = triple_rhs(state, ins, 0);
8602 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008603 if (*expr) {
8604 unuse_triple(*expr, ins);
8605 *expr = 0;
8606 }
8607 }
8608}
8609
8610static void unuse_lhs(struct compile_state *state, struct triple *ins)
8611{
8612 struct triple **expr;
8613 expr = triple_lhs(state, ins, 0);
8614 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008615 unuse_triple(*expr, ins);
8616 *expr = 0;
8617 }
8618}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008619
Stefan Reinauer50542a82007-10-24 11:14:14 +00008620#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008621static void unuse_misc(struct compile_state *state, struct triple *ins)
8622{
8623 struct triple **expr;
8624 expr = triple_misc(state, ins, 0);
8625 for(;expr;expr = triple_misc(state, ins, expr)) {
8626 unuse_triple(*expr, ins);
8627 *expr = 0;
8628 }
8629}
8630
8631static void unuse_targ(struct compile_state *state, struct triple *ins)
8632{
8633 int i;
8634 struct triple **slot;
8635 slot = &TARG(ins, 0);
8636 for(i = 0; i < ins->targ; i++) {
8637 unuse_triple(slot[i], ins);
8638 slot[i] = 0;
8639 }
8640}
8641
Eric Biedermanb138ac82003-04-22 18:44:01 +00008642static void check_lhs(struct compile_state *state, struct triple *ins)
8643{
8644 struct triple **expr;
8645 expr = triple_lhs(state, ins, 0);
8646 for(;expr;expr = triple_lhs(state, ins, expr)) {
8647 internal_error(state, ins, "unexpected lhs");
8648 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008649
Eric Biedermanb138ac82003-04-22 18:44:01 +00008650}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008651#endif
Eric Biederman90089602004-05-28 14:11:54 +00008652
8653static void check_misc(struct compile_state *state, struct triple *ins)
8654{
8655 struct triple **expr;
8656 expr = triple_misc(state, ins, 0);
8657 for(;expr;expr = triple_misc(state, ins, expr)) {
8658 if (*expr) {
8659 internal_error(state, ins, "unexpected misc");
8660 }
8661 }
8662}
8663
Eric Biedermanb138ac82003-04-22 18:44:01 +00008664static void check_targ(struct compile_state *state, struct triple *ins)
8665{
8666 struct triple **expr;
8667 expr = triple_targ(state, ins, 0);
8668 for(;expr;expr = triple_targ(state, ins, expr)) {
8669 internal_error(state, ins, "unexpected targ");
8670 }
8671}
8672
8673static void wipe_ins(struct compile_state *state, struct triple *ins)
8674{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008675 /* Becareful which instructions you replace the wiped
8676 * instruction with, as there are not enough slots
8677 * in all instructions to hold all others.
8678 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008679 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008680 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008681 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008682 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008683 ins->lhs = 0;
8684 ins->rhs = 0;
8685 ins->misc = 0;
8686 ins->targ = 0;
8687}
8688
Stefan Reinauer50542a82007-10-24 11:14:14 +00008689#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008690static void wipe_branch(struct compile_state *state, struct triple *ins)
8691{
8692 /* Becareful which instructions you replace the wiped
8693 * instruction with, as there are not enough slots
8694 * in all instructions to hold all others.
8695 */
8696 unuse_rhs(state, ins);
8697 unuse_lhs(state, ins);
8698 unuse_misc(state, ins);
8699 unuse_targ(state, ins);
8700 ins->lhs = 0;
8701 ins->rhs = 0;
8702 ins->misc = 0;
8703 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008704}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008705#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008706
Stefan Reinauer14e22772010-04-27 06:56:47 +00008707static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008708 struct triple *ins, struct triple *rhs)
8709{
Eric Biederman83b991a2003-10-11 06:20:25 +00008710 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008711 if (!equiv_types(ins->type, rhs->type)) {
8712 FILE *fp = state->errout;
8713 fprintf(fp, "src type: ");
8714 name_of(fp, rhs->type);
8715 fprintf(fp, "\ndst type: ");
8716 name_of(fp, ins->type);
8717 fprintf(fp, "\n");
8718 internal_error(state, ins, "mkcopy type mismatch");
8719 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008720 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008721 wipe_ins(state, ins);
8722 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008723 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008724 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008725 RHS(ins, 0) = rhs;
8726 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008727}
8728
Stefan Reinauer14e22772010-04-27 06:56:47 +00008729static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008730 struct triple *ins, ulong_t value)
8731{
8732 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008733 fprintf(state->errout, "type: ");
8734 name_of(state->errout, ins->type);
8735 fprintf(state->errout, "\n");
8736 internal_error(state, ins, "unknown type to make constant value: %ld",
8737 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008738 }
8739 wipe_ins(state, ins);
8740 ins->op = OP_INTCONST;
8741 ins->u.cval = value;
8742}
8743
8744static void mkaddr_const(struct compile_state *state,
8745 struct triple *ins, struct triple *sdecl, ulong_t value)
8746{
Eric Biederman90089602004-05-28 14:11:54 +00008747 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008748 internal_error(state, ins, "bad base for addrconst");
8749 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008750 wipe_ins(state, ins);
8751 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008752 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008753 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008754 ins->u.cval = value;
8755 use_triple(sdecl, ins);
8756}
8757
Eric Biederman90089602004-05-28 14:11:54 +00008758#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008759static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008760 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008761{
Eric Biederman90089602004-05-28 14:11:54 +00008762 FILE *fp = state->dbgout;
8763 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8764 name_of(fp, tuple->type);
8765 if (tuple->lhs > 0) {
8766 fprintf(fp, " lhs: ");
8767 name_of(fp, LHS(tuple, 0)->type);
8768 }
8769 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008770
Eric Biederman90089602004-05-28 14:11:54 +00008771}
8772#endif
8773
Stefan Reinauer14e22772010-04-27 06:56:47 +00008774static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008775 struct triple *ins, struct triple *tuple)
8776{
8777 struct triple *next;
8778 next = ins->next;
8779 flatten(state, next, tuple);
8780#if DEBUG_DECOMPOSE_PRINT_TUPLES
8781 print_tuple(state, ins, tuple);
8782#endif
8783
8784 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8785 struct triple *tmp;
8786 if (tuple->lhs != 1) {
8787 internal_error(state, tuple, "plain type in multiple registers?");
8788 }
8789 tmp = LHS(tuple, 0);
8790 release_triple(state, tuple);
8791 tuple = tmp;
8792 }
8793
8794 propogate_use(state, ins, tuple);
8795 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008796
Eric Biederman90089602004-05-28 14:11:54 +00008797 return next;
8798}
8799
8800static struct triple *decompose_unknownval(struct compile_state *state,
8801 struct triple *ins)
8802{
8803 struct triple *tuple;
8804 ulong_t i;
8805
8806#if DEBUG_DECOMPOSE_HIRES
8807 FILE *fp = state->dbgout;
8808 fprintf(fp, "unknown type: ");
8809 name_of(fp, ins->type);
8810 fprintf(fp, "\n");
8811#endif
8812
8813 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008814 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008815 ins->occurance);
8816
8817 for(i = 0; i < tuple->lhs; i++) {
8818 struct type *piece_type;
8819 struct triple *unknown;
8820
8821 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8822 get_occurance(tuple->occurance);
8823 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8824 tuple->occurance);
8825 LHS(tuple, i) = unknown;
8826 }
8827 return decompose_with_tuple(state, ins, tuple);
8828}
8829
8830
Stefan Reinauer14e22772010-04-27 06:56:47 +00008831static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008832 struct triple *ins)
8833{
8834 struct triple *tuple, *lval;
8835 ulong_t i;
8836
8837 lval = RHS(ins, 0);
8838
8839 if (lval->op == OP_PIECE) {
8840 return ins->next;
8841 }
8842 get_occurance(ins->occurance);
8843 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8844 ins->occurance);
8845
8846 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008847 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008848 {
8849 internal_error(state, ins, "lhs size inconsistency?");
8850 }
8851 for(i = 0; i < tuple->lhs; i++) {
8852 struct triple *piece, *read, *bitref;
8853 if ((i != 0) || !triple_is_def(state, lval)) {
8854 piece = LHS(lval, i);
8855 } else {
8856 piece = lval;
8857 }
8858
8859 /* See if the piece is really a bitref */
8860 bitref = 0;
8861 if (piece->op == OP_BITREF) {
8862 bitref = piece;
8863 piece = RHS(bitref, 0);
8864 }
8865
8866 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008867 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008868 tuple->occurance);
8869 RHS(read, 0) = piece;
8870
8871 if (bitref) {
8872 struct triple *extract;
8873 int op;
8874 if (is_signed(bitref->type->left)) {
8875 op = OP_SEXTRACT;
8876 } else {
8877 op = OP_UEXTRACT;
8878 }
8879 get_occurance(tuple->occurance);
8880 extract = alloc_triple(state, op, bitref->type, -1, -1,
8881 tuple->occurance);
8882 RHS(extract, 0) = read;
8883 extract->u.bitfield.size = bitref->u.bitfield.size;
8884 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8885
8886 read = extract;
8887 }
8888
8889 LHS(tuple, i) = read;
8890 }
8891 return decompose_with_tuple(state, ins, tuple);
8892}
8893
Stefan Reinauer14e22772010-04-27 06:56:47 +00008894static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008895 struct triple *ins)
8896{
8897 struct triple *tuple, *lval, *val;
8898 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008899
Eric Biederman90089602004-05-28 14:11:54 +00008900 lval = MISC(ins, 0);
8901 val = RHS(ins, 0);
8902 get_occurance(ins->occurance);
8903 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8904 ins->occurance);
8905
8906 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008907 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008908 {
8909 internal_error(state, ins, "lhs size inconsistency?");
8910 }
8911 for(i = 0; i < tuple->lhs; i++) {
8912 struct triple *piece, *write, *pval, *bitref;
8913 if ((i != 0) || !triple_is_def(state, lval)) {
8914 piece = LHS(lval, i);
8915 } else {
8916 piece = lval;
8917 }
8918 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8919 pval = val;
8920 }
8921 else {
8922 if (i > val->lhs) {
8923 internal_error(state, ins, "lhs size inconsistency?");
8924 }
8925 pval = LHS(val, i);
8926 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008927
Eric Biederman90089602004-05-28 14:11:54 +00008928 /* See if the piece is really a bitref */
8929 bitref = 0;
8930 if (piece->op == OP_BITREF) {
8931 struct triple *read, *deposit;
8932 bitref = piece;
8933 piece = RHS(bitref, 0);
8934
8935 /* Read the destination register */
8936 get_occurance(tuple->occurance);
8937 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8938 tuple->occurance);
8939 RHS(read, 0) = piece;
8940
8941 /* Deposit the new bitfield value */
8942 get_occurance(tuple->occurance);
8943 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8944 tuple->occurance);
8945 RHS(deposit, 0) = read;
8946 RHS(deposit, 1) = pval;
8947 deposit->u.bitfield.size = bitref->u.bitfield.size;
8948 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8949
8950 /* Now write the newly generated value */
8951 pval = deposit;
8952 }
8953
8954 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008955 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008956 tuple->occurance);
8957 MISC(write, 0) = piece;
8958 RHS(write, 0) = pval;
8959 LHS(tuple, i) = write;
8960 }
8961 return decompose_with_tuple(state, ins, tuple);
8962}
8963
8964struct decompose_load_info {
8965 struct occurance *occurance;
8966 struct triple *lval;
8967 struct triple *tuple;
8968};
8969static void decompose_load_cb(struct compile_state *state,
8970 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8971{
8972 struct decompose_load_info *info = arg;
8973 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008974
Eric Biederman90089602004-05-28 14:11:54 +00008975 if (reg_offset > info->tuple->lhs) {
8976 internal_error(state, info->tuple, "lhs to small?");
8977 }
8978 get_occurance(info->occurance);
8979 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8980 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8981 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8982}
8983
Stefan Reinauer14e22772010-04-27 06:56:47 +00008984static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008985 struct triple *ins)
8986{
8987 struct triple *tuple;
8988 struct decompose_load_info info;
8989
8990 if (!is_compound_type(ins->type)) {
8991 return ins->next;
8992 }
8993 get_occurance(ins->occurance);
8994 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8995 ins->occurance);
8996
8997 info.occurance = ins->occurance;
8998 info.lval = RHS(ins, 0);
8999 info.tuple = tuple;
9000 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9001
9002 return decompose_with_tuple(state, ins, tuple);
9003}
9004
9005
9006struct decompose_store_info {
9007 struct occurance *occurance;
9008 struct triple *lval;
9009 struct triple *val;
9010 struct triple *tuple;
9011};
9012static void decompose_store_cb(struct compile_state *state,
9013 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9014{
9015 struct decompose_store_info *info = arg;
9016 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009017
Eric Biederman90089602004-05-28 14:11:54 +00009018 if (reg_offset > info->tuple->lhs) {
9019 internal_error(state, info->tuple, "lhs to small?");
9020 }
9021 get_occurance(info->occurance);
9022 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9023 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9024 RHS(store, 1) = LHS(info->val, reg_offset);
9025 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9026}
9027
Stefan Reinauer14e22772010-04-27 06:56:47 +00009028static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009029 struct triple *ins)
9030{
9031 struct triple *tuple;
9032 struct decompose_store_info info;
9033
9034 if (!is_compound_type(ins->type)) {
9035 return ins->next;
9036 }
9037 get_occurance(ins->occurance);
9038 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9039 ins->occurance);
9040
9041 info.occurance = ins->occurance;
9042 info.lval = RHS(ins, 0);
9043 info.val = RHS(ins, 1);
9044 info.tuple = tuple;
9045 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9046
9047 return decompose_with_tuple(state, ins, tuple);
9048}
9049
Stefan Reinauer14e22772010-04-27 06:56:47 +00009050static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009051 struct triple *ins)
9052{
9053 struct triple *tuple, *lval;
9054 struct type *type;
9055 size_t reg_offset;
9056 int i, idx;
9057
9058 lval = MISC(ins, 0);
9059 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9060 idx = reg_offset/REG_SIZEOF_REG;
9061 type = field_type(state, lval->type, ins->u.field);
9062#if DEBUG_DECOMPOSE_HIRES
9063 {
9064 FILE *fp = state->dbgout;
9065 fprintf(fp, "field type: ");
9066 name_of(fp, type);
9067 fprintf(fp, "\n");
9068 }
9069#endif
9070
9071 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009072 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009073 ins->occurance);
9074
9075 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9076 (tuple->lhs != 1))
9077 {
9078 internal_error(state, ins, "multi register bitfield?");
9079 }
9080
9081 for(i = 0; i < tuple->lhs; i++, idx++) {
9082 struct triple *piece;
9083 if (!triple_is_def(state, lval)) {
9084 if (idx > lval->lhs) {
9085 internal_error(state, ins, "inconsistent lhs count");
9086 }
9087 piece = LHS(lval, idx);
9088 } else {
9089 if (idx != 0) {
9090 internal_error(state, ins, "bad reg_offset into def");
9091 }
9092 if (i != 0) {
9093 internal_error(state, ins, "bad reg count from def");
9094 }
9095 piece = lval;
9096 }
9097
9098 /* Remember the offset of the bitfield */
9099 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9100 get_occurance(ins->occurance);
9101 piece = build_triple(state, OP_BITREF, type, piece, 0,
9102 ins->occurance);
9103 piece->u.bitfield.size = size_of(state, type);
9104 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9105 }
9106 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009107 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009108 "request for a nonbitfield sub register?");
9109 }
9110
9111 LHS(tuple, i) = piece;
9112 }
9113
9114 return decompose_with_tuple(state, ins, tuple);
9115}
9116
Stefan Reinauer14e22772010-04-27 06:56:47 +00009117static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009118 struct triple *ins)
9119{
9120 struct triple *tuple, *lval;
9121 struct type *type;
9122 int i, idx;
9123
9124 lval = MISC(ins, 0);
9125 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9126 type = index_type(state, lval->type, ins->u.cval);
9127#if DEBUG_DECOMPOSE_HIRES
9128{
9129 FILE *fp = state->dbgout;
9130 fprintf(fp, "index type: ");
9131 name_of(fp, type);
9132 fprintf(fp, "\n");
9133}
9134#endif
9135
9136 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009137 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009138 ins->occurance);
9139
9140 for(i = 0; i < tuple->lhs; i++, idx++) {
9141 struct triple *piece;
9142 if (!triple_is_def(state, lval)) {
9143 if (idx > lval->lhs) {
9144 internal_error(state, ins, "inconsistent lhs count");
9145 }
9146 piece = LHS(lval, idx);
9147 } else {
9148 if (idx != 0) {
9149 internal_error(state, ins, "bad reg_offset into def");
9150 }
9151 if (i != 0) {
9152 internal_error(state, ins, "bad reg count from def");
9153 }
9154 piece = lval;
9155 }
9156 LHS(tuple, i) = piece;
9157 }
9158
9159 return decompose_with_tuple(state, ins, tuple);
9160}
9161
9162static void decompose_compound_types(struct compile_state *state)
9163{
9164 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009165 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009166 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009167
9168 /* Pass one expand compound values into pseudo registers.
9169 */
9170 next = first;
9171 do {
9172 ins = next;
9173 next = ins->next;
9174 switch(ins->op) {
9175 case OP_UNKNOWNVAL:
9176 next = decompose_unknownval(state, ins);
9177 break;
9178
9179 case OP_READ:
9180 next = decompose_read(state, ins);
9181 break;
9182
9183 case OP_WRITE:
9184 next = decompose_write(state, ins);
9185 break;
9186
9187
9188 /* Be very careful with the load/store logic. These
9189 * operations must convert from the in register layout
9190 * to the in memory layout, which is nontrivial.
9191 */
9192 case OP_LOAD:
9193 next = decompose_load(state, ins);
9194 break;
9195 case OP_STORE:
9196 next = decompose_store(state, ins);
9197 break;
9198
9199 case OP_DOT:
9200 next = decompose_dot(state, ins);
9201 break;
9202 case OP_INDEX:
9203 next = decompose_index(state, ins);
9204 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009205
Eric Biederman90089602004-05-28 14:11:54 +00009206 }
9207#if DEBUG_DECOMPOSE_HIRES
9208 fprintf(fp, "decompose next: %p \n", next);
9209 fflush(fp);
9210 fprintf(fp, "next->op: %d %s\n",
9211 next->op, tops(next->op));
9212 /* High resolution debugging mode */
9213 print_triples(state);
9214#endif
9215 } while (next != first);
9216
9217 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009218 */
9219 ins = first;
9220 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009221 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009222 if (ins->op == OP_TUPLE) {
9223 if (ins->use) {
9224 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009225 }
Eric Biederman90089602004-05-28 14:11:54 +00009226 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009227 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009228 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009229 }
Eric Biederman90089602004-05-28 14:11:54 +00009230 ins = next;
9231 } while(ins != first);
9232 ins = first;
9233 do {
9234 next = ins->next;
9235 if (ins->op == OP_BITREF) {
9236 if (ins->use) {
9237 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +00009238 }
Eric Biederman90089602004-05-28 14:11:54 +00009239 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009240 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009241 }
9242 }
9243 ins = next;
9244 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009245
Eric Biederman0babc1c2003-05-09 02:39:00 +00009246 /* Pass three verify the state and set ->id to 0.
9247 */
Eric Biederman90089602004-05-28 14:11:54 +00009248 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009249 do {
Eric Biederman90089602004-05-28 14:11:54 +00009250 ins = next;
9251 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009252 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009253 if (triple_stores_block(state, ins)) {
9254 ins->u.block = 0;
9255 }
9256 if (triple_is_def(state, ins)) {
9257 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9258 internal_error(state, ins, "multi register value remains?");
9259 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009260 }
9261 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009262 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009263 }
Eric Biederman90089602004-05-28 14:11:54 +00009264 if (ins->op == OP_INDEX) {
9265 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009266 }
Eric Biederman90089602004-05-28 14:11:54 +00009267 if (ins->op == OP_BITREF) {
9268 internal_error(state, ins, "OP_BITREF remains?");
9269 }
9270 if (ins->op == OP_TUPLE) {
9271 internal_error(state, ins, "OP_TUPLE remains?");
9272 }
9273 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009274}
9275
Eric Biedermanb138ac82003-04-22 18:44:01 +00009276/* For those operations that cannot be simplified */
9277static void simplify_noop(struct compile_state *state, struct triple *ins)
9278{
9279 return;
9280}
9281
9282static void simplify_smul(struct compile_state *state, struct triple *ins)
9283{
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 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009286 tmp = RHS(ins, 0);
9287 RHS(ins, 0) = RHS(ins, 1);
9288 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009289 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009290 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009291 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009292 left = read_sconst(state, ins, RHS(ins, 0));
9293 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009294 mkconst(state, ins, left * right);
9295 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009296 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009297 mkconst(state, ins, 0);
9298 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009299 else if (is_one(RHS(ins, 1))) {
9300 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009301 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009302 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009303 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009304 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009305 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009306 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009307 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009308 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009309 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009310 }
9311}
9312
9313static void simplify_umul(struct compile_state *state, struct triple *ins)
9314{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009315 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009316 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009317 tmp = RHS(ins, 0);
9318 RHS(ins, 0) = RHS(ins, 1);
9319 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009320 }
Eric Biederman90089602004-05-28 14:11:54 +00009321 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009322 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009323 left = read_const(state, ins, RHS(ins, 0));
9324 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009325 mkconst(state, ins, left * right);
9326 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009327 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009328 mkconst(state, ins, 0);
9329 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009330 else if (is_one(RHS(ins, 1))) {
9331 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009332 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009333 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009334 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009335 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009336 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009337 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009338 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009339 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009340 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009341 }
9342}
9343
9344static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9345{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009346 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009347 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009348 left = read_sconst(state, ins, RHS(ins, 0));
9349 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009350 mkconst(state, ins, left / right);
9351 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009352 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009353 mkconst(state, ins, 0);
9354 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009355 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009356 error(state, ins, "division by zero");
9357 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009358 else if (is_one(RHS(ins, 1))) {
9359 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009360 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009361 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009362 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009363 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009364 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009365 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009366 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009367 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009368 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009369 }
9370}
9371
9372static void simplify_udiv(struct compile_state *state, struct triple *ins)
9373{
Eric Biederman90089602004-05-28 14:11:54 +00009374 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009375 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009376 left = read_const(state, ins, RHS(ins, 0));
9377 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009378 mkconst(state, ins, left / right);
9379 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009380 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009381 mkconst(state, ins, 0);
9382 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009383 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009384 error(state, ins, "division by zero");
9385 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009386 else if (is_one(RHS(ins, 1))) {
9387 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009388 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009389 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009390 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009391 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009392 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009393 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009394 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009395 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009396 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009397 }
9398}
9399
9400static void simplify_smod(struct compile_state *state, struct triple *ins)
9401{
Eric Biederman90089602004-05-28 14:11:54 +00009402 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009403 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009404 left = read_const(state, ins, RHS(ins, 0));
9405 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009406 mkconst(state, ins, left % right);
9407 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009408 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009409 mkconst(state, ins, 0);
9410 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009411 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009412 error(state, ins, "division by zero");
9413 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009414 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009415 mkconst(state, ins, 0);
9416 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009417 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009418 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009419 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009420 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009421 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009422 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009423 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009424 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009425 }
9426}
Eric Biederman83b991a2003-10-11 06:20:25 +00009427
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428static void simplify_umod(struct compile_state *state, struct triple *ins)
9429{
Eric Biederman90089602004-05-28 14:11:54 +00009430 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009431 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009432 left = read_const(state, ins, RHS(ins, 0));
9433 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009434 mkconst(state, ins, left % right);
9435 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009436 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437 mkconst(state, ins, 0);
9438 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009439 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440 error(state, ins, "division by zero");
9441 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009442 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009443 mkconst(state, ins, 0);
9444 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009445 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009446 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009447 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009448 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009449 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009450 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009451 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009452 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009453 }
9454}
9455
9456static void simplify_add(struct compile_state *state, struct triple *ins)
9457{
9458 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009459 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009460 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009461 tmp = RHS(ins, 0);
9462 RHS(ins, 0) = RHS(ins, 1);
9463 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009464 }
Eric Biederman90089602004-05-28 14:11:54 +00009465 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009466 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009467 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009468 left = read_const(state, ins, RHS(ins, 0));
9469 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009470 mkconst(state, ins, left + right);
9471 }
Eric Biederman530b5192003-07-01 10:05:30 +00009472 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009473 struct triple *sdecl;
9474 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009475 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009476 left = RHS(ins, 0)->u.cval;
9477 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009478 mkaddr_const(state, ins, sdecl, left + right);
9479 }
Eric Biederman530b5192003-07-01 10:05:30 +00009480 else {
9481 internal_warning(state, ins, "Optimize me!");
9482 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009483 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009484 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009485 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009486 tmp = RHS(ins, 1);
9487 RHS(ins, 1) = RHS(ins, 0);
9488 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009489 }
9490}
9491
9492static void simplify_sub(struct compile_state *state, struct triple *ins)
9493{
Eric Biederman90089602004-05-28 14:11:54 +00009494 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009495 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009496 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009497 left = read_const(state, ins, RHS(ins, 0));
9498 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009499 mkconst(state, ins, left - right);
9500 }
Eric Biederman530b5192003-07-01 10:05:30 +00009501 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009502 struct triple *sdecl;
9503 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009504 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009505 left = RHS(ins, 0)->u.cval;
9506 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009507 mkaddr_const(state, ins, sdecl, left - right);
9508 }
Eric Biederman530b5192003-07-01 10:05:30 +00009509 else {
9510 internal_warning(state, ins, "Optimize me!");
9511 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009512 }
9513}
9514
9515static void simplify_sl(struct compile_state *state, struct triple *ins)
9516{
Eric Biederman90089602004-05-28 14:11:54 +00009517 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009518 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009519 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009520 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009521 warning(state, ins, "left shift count >= width of type");
9522 }
9523 }
Eric Biederman90089602004-05-28 14:11:54 +00009524 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009525 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009526 left = read_const(state, ins, RHS(ins, 0));
9527 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009528 mkconst(state, ins, left << right);
9529 }
9530}
9531
9532static void simplify_usr(struct compile_state *state, struct triple *ins)
9533{
Eric Biederman90089602004-05-28 14:11:54 +00009534 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009535 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009536 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009537 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009538 warning(state, ins, "right shift count >= width of type");
9539 }
9540 }
Eric Biederman90089602004-05-28 14:11:54 +00009541 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009542 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009543 left = read_const(state, ins, RHS(ins, 0));
9544 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009545 mkconst(state, ins, left >> right);
9546 }
9547}
9548
9549static void simplify_ssr(struct compile_state *state, struct triple *ins)
9550{
Eric Biederman90089602004-05-28 14:11:54 +00009551 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009552 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009553 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009554 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009555 warning(state, ins, "right shift count >= width of type");
9556 }
9557 }
Eric Biederman90089602004-05-28 14:11:54 +00009558 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009559 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009560 left = read_sconst(state, ins, RHS(ins, 0));
9561 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009562 mkconst(state, ins, left >> right);
9563 }
9564}
9565
9566static void simplify_and(struct compile_state *state, struct triple *ins)
9567{
Eric Biederman90089602004-05-28 14:11:54 +00009568 struct triple *left, *right;
9569 left = RHS(ins, 0);
9570 right = RHS(ins, 1);
9571
9572 if (is_simple_const(left) && is_simple_const(right)) {
9573 ulong_t lval, rval;
9574 lval = read_const(state, ins, left);
9575 rval = read_const(state, ins, right);
9576 mkconst(state, ins, lval & rval);
9577 }
9578 else if (is_zero(right) || is_zero(left)) {
9579 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009580 }
9581}
9582
9583static void simplify_or(struct compile_state *state, struct triple *ins)
9584{
Eric Biederman90089602004-05-28 14:11:54 +00009585 struct triple *left, *right;
9586 left = RHS(ins, 0);
9587 right = RHS(ins, 1);
9588
9589 if (is_simple_const(left) && is_simple_const(right)) {
9590 ulong_t lval, rval;
9591 lval = read_const(state, ins, left);
9592 rval = read_const(state, ins, right);
9593 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009594 }
Eric Biederman90089602004-05-28 14:11:54 +00009595#if 0 /* I need to handle type mismatches here... */
9596 else if (is_zero(right)) {
9597 mkcopy(state, ins, left);
9598 }
9599 else if (is_zero(left)) {
9600 mkcopy(state, ins, right);
9601 }
9602#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009603}
9604
9605static void simplify_xor(struct compile_state *state, struct triple *ins)
9606{
Eric Biederman90089602004-05-28 14:11:54 +00009607 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009608 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009609 left = read_const(state, ins, RHS(ins, 0));
9610 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009611 mkconst(state, ins, left ^ right);
9612 }
9613}
9614
9615static void simplify_pos(struct compile_state *state, struct triple *ins)
9616{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009617 if (is_const(RHS(ins, 0))) {
9618 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009619 }
9620 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009621 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009622 }
9623}
9624
9625static void simplify_neg(struct compile_state *state, struct triple *ins)
9626{
Eric Biederman90089602004-05-28 14:11:54 +00009627 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009628 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009629 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009630 mkconst(state, ins, -left);
9631 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009632 else if (RHS(ins, 0)->op == OP_NEG) {
9633 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009634 }
9635}
9636
9637static void simplify_invert(struct compile_state *state, struct triple *ins)
9638{
Eric Biederman90089602004-05-28 14:11:54 +00009639 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009640 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009641 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009642 mkconst(state, ins, ~left);
9643 }
9644}
9645
9646static void simplify_eq(struct compile_state *state, struct triple *ins)
9647{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009648 struct triple *left, *right;
9649 left = RHS(ins, 0);
9650 right = RHS(ins, 1);
9651
9652 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009653 int val;
9654 val = const_eq(state, ins, left, right);
9655 if (val >= 0) {
9656 mkconst(state, ins, val == 1);
9657 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009658 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009659 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009660 mkconst(state, ins, 1);
9661 }
9662}
9663
9664static void simplify_noteq(struct compile_state *state, struct triple *ins)
9665{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009666 struct triple *left, *right;
9667 left = RHS(ins, 0);
9668 right = RHS(ins, 1);
9669
9670 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009671 int val;
9672 val = const_eq(state, ins, left, right);
9673 if (val >= 0) {
9674 mkconst(state, ins, val != 1);
9675 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009676 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009677 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009678 mkconst(state, ins, 0);
9679 }
9680}
9681
9682static void simplify_sless(struct compile_state *state, struct triple *ins)
9683{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009684 struct triple *left, *right;
9685 left = RHS(ins, 0);
9686 right = RHS(ins, 1);
9687
9688 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009689 int val;
9690 val = const_scmp(state, ins, left, right);
9691 if ((val >= -1) && (val <= 1)) {
9692 mkconst(state, ins, val < 0);
9693 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009694 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009695 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009696 mkconst(state, ins, 0);
9697 }
9698}
9699
9700static void simplify_uless(struct compile_state *state, struct triple *ins)
9701{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009702 struct triple *left, *right;
9703 left = RHS(ins, 0);
9704 right = RHS(ins, 1);
9705
9706 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009707 int val;
9708 val = const_ucmp(state, ins, left, right);
9709 if ((val >= -1) && (val <= 1)) {
9710 mkconst(state, ins, val < 0);
9711 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009712 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009713 else if (is_zero(right)) {
9714 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009715 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009716 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009717 mkconst(state, ins, 0);
9718 }
9719}
9720
9721static void simplify_smore(struct compile_state *state, struct triple *ins)
9722{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009723 struct triple *left, *right;
9724 left = RHS(ins, 0);
9725 right = RHS(ins, 1);
9726
9727 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009728 int val;
9729 val = const_scmp(state, ins, left, right);
9730 if ((val >= -1) && (val <= 1)) {
9731 mkconst(state, ins, val > 0);
9732 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009733 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009734 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009735 mkconst(state, ins, 0);
9736 }
9737}
9738
9739static void simplify_umore(struct compile_state *state, struct triple *ins)
9740{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009741 struct triple *left, *right;
9742 left = RHS(ins, 0);
9743 right = RHS(ins, 1);
9744
9745 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009746 int val;
9747 val = const_ucmp(state, ins, left, right);
9748 if ((val >= -1) && (val <= 1)) {
9749 mkconst(state, ins, val > 0);
9750 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009751 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009752 else if (is_zero(left)) {
9753 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009754 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009755 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009756 mkconst(state, ins, 0);
9757 }
9758}
9759
9760
9761static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9762{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009763 struct triple *left, *right;
9764 left = RHS(ins, 0);
9765 right = RHS(ins, 1);
9766
9767 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009768 int val;
9769 val = const_scmp(state, ins, left, right);
9770 if ((val >= -1) && (val <= 1)) {
9771 mkconst(state, ins, val <= 0);
9772 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009773 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009774 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009775 mkconst(state, ins, 1);
9776 }
9777}
9778
9779static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9780{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009781 struct triple *left, *right;
9782 left = RHS(ins, 0);
9783 right = RHS(ins, 1);
9784
9785 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009786 int val;
9787 val = const_ucmp(state, ins, left, right);
9788 if ((val >= -1) && (val <= 1)) {
9789 mkconst(state, ins, val <= 0);
9790 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009791 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009792 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009793 mkconst(state, ins, 1);
9794 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009795 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009796 mkconst(state, ins, 1);
9797 }
9798}
9799
9800static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9801{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009802 struct triple *left, *right;
9803 left = RHS(ins, 0);
9804 right = RHS(ins, 1);
9805
9806 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009807 int val;
9808 val = const_scmp(state, ins, left, right);
9809 if ((val >= -1) && (val <= 1)) {
9810 mkconst(state, ins, val >= 0);
9811 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009812 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009813 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009814 mkconst(state, ins, 1);
9815 }
9816}
9817
9818static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9819{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009820 struct triple *left, *right;
9821 left = RHS(ins, 0);
9822 right = RHS(ins, 1);
9823
9824 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009825 int val;
9826 val = const_ucmp(state, ins, left, right);
9827 if ((val >= -1) && (val <= 1)) {
9828 mkconst(state, ins, val >= 0);
9829 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009830 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009831 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009832 mkconst(state, ins, 1);
9833 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009834 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009835 mkconst(state, ins, 1);
9836 }
9837}
9838
9839static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9840{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009841 struct triple *rhs;
9842 rhs = RHS(ins, 0);
9843
9844 if (is_const(rhs)) {
9845 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009846 }
9847 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009848 else if ((rhs->use) &&
9849 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009850 int need_copy = 1;
9851 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009852 switch(rhs->op) {
9853 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9854 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9855 case OP_EQ: rhs->op = OP_NOTEQ; break;
9856 case OP_NOTEQ: rhs->op = OP_EQ; break;
9857 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9858 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9859 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9860 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9861 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9862 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9863 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9864 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009865 default:
9866 need_copy = 0;
9867 break;
9868 }
9869 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009870 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009871 }
9872 }
9873}
9874
9875static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9876{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009877 struct triple *rhs;
9878 rhs = RHS(ins, 0);
9879
9880 if (is_const(rhs)) {
9881 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009882 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009883 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009884 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9885 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9886 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009887 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009888 }
9889
9890}
9891
Eric Biederman90089602004-05-28 14:11:54 +00009892static void simplify_load(struct compile_state *state, struct triple *ins)
9893{
9894 struct triple *addr, *sdecl, *blob;
9895
9896 /* If I am doing a load with a constant pointer from a constant
9897 * table get the value.
9898 */
9899 addr = RHS(ins, 0);
9900 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9901 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9902 (blob->op == OP_BLOBCONST)) {
9903 unsigned char buffer[SIZEOF_WORD];
9904 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009905 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009906 ulong_t val;
9907 reg_size = reg_size_of(state, ins->type);
9908 if (reg_size > REG_SIZEOF_REG) {
9909 internal_error(state, ins, "load size greater than register");
9910 }
9911 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009912 end = blob->u.blob;
9913 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009914 src = blob->u.blob;
9915 src += addr->u.cval;
9916
Eric Biederman7dea9552004-06-29 05:38:37 +00009917 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009918 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009919 }
9920
Eric Biederman90089602004-05-28 14:11:54 +00009921 memset(buffer, 0, sizeof(buffer));
9922 memcpy(buffer, src, bits_to_bytes(mem_size));
9923
9924 switch(mem_size) {
9925 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9926 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9927 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9928 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9929 default:
9930 internal_error(state, ins, "mem_size: %d not handled",
9931 mem_size);
9932 val = 0;
9933 break;
9934 }
9935 mkconst(state, ins, val);
9936 }
9937}
9938
9939static void simplify_uextract(struct compile_state *state, struct triple *ins)
9940{
9941 if (is_simple_const(RHS(ins, 0))) {
9942 ulong_t val;
9943 ulong_t mask;
9944 val = read_const(state, ins, RHS(ins, 0));
9945 mask = 1;
9946 mask <<= ins->u.bitfield.size;
9947 mask -= 1;
9948 val >>= ins->u.bitfield.offset;
9949 val &= mask;
9950 mkconst(state, ins, val);
9951 }
9952}
9953
9954static void simplify_sextract(struct compile_state *state, struct triple *ins)
9955{
9956 if (is_simple_const(RHS(ins, 0))) {
9957 ulong_t val;
9958 ulong_t mask;
9959 long_t sval;
9960 val = read_const(state, ins, RHS(ins, 0));
9961 mask = 1;
9962 mask <<= ins->u.bitfield.size;
9963 mask -= 1;
9964 val >>= ins->u.bitfield.offset;
9965 val &= mask;
9966 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9967 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009968 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009969 mkconst(state, ins, sval);
9970 }
9971}
9972
9973static void simplify_deposit(struct compile_state *state, struct triple *ins)
9974{
9975 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9976 ulong_t targ, val;
9977 ulong_t mask;
9978 targ = read_const(state, ins, RHS(ins, 0));
9979 val = read_const(state, ins, RHS(ins, 1));
9980 mask = 1;
9981 mask <<= ins->u.bitfield.size;
9982 mask -= 1;
9983 mask <<= ins->u.bitfield.offset;
9984 targ &= ~mask;
9985 val <<= ins->u.bitfield.offset;
9986 val &= mask;
9987 targ |= val;
9988 mkconst(state, ins, targ);
9989 }
9990}
9991
Eric Biedermanb138ac82003-04-22 18:44:01 +00009992static void simplify_copy(struct compile_state *state, struct triple *ins)
9993{
Eric Biederman90089602004-05-28 14:11:54 +00009994 struct triple *right;
9995 right = RHS(ins, 0);
9996 if (is_subset_type(ins->type, right->type)) {
9997 ins->type = right->type;
9998 }
9999 if (equiv_types(ins->type, right->type)) {
10000 ins->op = OP_COPY;/* I don't need to convert if the types match */
10001 } else {
10002 if (ins->op == OP_COPY) {
10003 internal_error(state, ins, "type mismatch on copy");
10004 }
10005 }
10006 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10007 struct triple *sdecl;
10008 ulong_t offset;
10009 sdecl = MISC(right, 0);
10010 offset = right->u.cval;
10011 mkaddr_const(state, ins, sdecl, offset);
10012 }
10013 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10014 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010015 case OP_INTCONST:
10016 {
10017 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010018 left = read_const(state, ins, right);
10019 /* Ensure I have not overflowed the destination. */
10020 if (size_of(state, right->type) > size_of(state, ins->type)) {
10021 ulong_t mask;
10022 mask = 1;
10023 mask <<= size_of(state, ins->type);
10024 mask -= 1;
10025 left &= mask;
10026 }
10027 /* Ensure I am properly sign extended */
10028 if (size_of(state, right->type) < size_of(state, ins->type) &&
10029 is_signed(right->type)) {
10030 long_t val;
10031 int shift;
10032 shift = SIZEOF_LONG - size_of(state, right->type);
10033 val = left;
10034 val <<= shift;
10035 val >>= shift;
10036 left = val;
10037 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010038 mkconst(state, ins, left);
10039 break;
10040 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010041 default:
10042 internal_error(state, ins, "uknown constant");
10043 break;
10044 }
10045 }
10046}
10047
Eric Biederman83b991a2003-10-11 06:20:25 +000010048static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010049{
10050 struct triple *ptr;
10051 if (!block) {
10052 return 0;
10053 }
10054 ptr = block->first;
10055 do {
10056 if (ptr->op == OP_PHI) {
10057 return 1;
10058 }
10059 ptr = ptr->next;
10060 } while(ptr != block->last);
10061 return 0;
10062}
10063
Eric Biederman83b991a2003-10-11 06:20:25 +000010064static int phi_dependency(struct block *block)
10065{
10066 /* A block has a phi dependency if a phi function
10067 * depends on that block to exist, and makes a block
10068 * that is otherwise useless unsafe to remove.
10069 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010070 if (block) {
10071 struct block_set *edge;
10072 for(edge = block->edges; edge; edge = edge->next) {
10073 if (phi_present(edge->member)) {
10074 return 1;
10075 }
10076 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010077 }
10078 return 0;
10079}
10080
10081static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10082{
10083 struct triple *targ;
10084 targ = TARG(ins, 0);
10085 /* During scc_transform temporary triples are allocated that
10086 * loop back onto themselves. If I see one don't advance the
10087 * target.
10088 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010089 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010090 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010091 targ = targ->next;
10092 }
10093 return targ;
10094}
10095
10096
10097static void simplify_branch(struct compile_state *state, struct triple *ins)
10098{
Eric Biederman90089602004-05-28 14:11:54 +000010099 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010100 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010101 internal_error(state, ins, "not branch");
10102 }
10103 if (ins->use != 0) {
10104 internal_error(state, ins, "branch use");
10105 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010106 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010107 * make modifications to the control flow graph as well
10108 * as to the branch instruction itself. That is handled
10109 * by rebuilding the basic blocks after simplify all is called.
10110 */
10111
10112 /* If we have a branch to an unconditional branch update
10113 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010114 * functions.
10115 * Also only do this a limited number of times so
10116 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010117 */
Eric Biederman90089602004-05-28 14:11:54 +000010118 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010119 do {
10120 struct triple *targ;
10121 simplified = 0;
10122 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010123 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010124 !phi_dependency(targ->u.block))
10125 {
10126 unuse_triple(TARG(ins, 0), ins);
10127 TARG(ins, 0) = TARG(targ, 0);
10128 use_triple(TARG(ins, 0), ins);
10129 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010130 }
Eric Biederman90089602004-05-28 14:11:54 +000010131 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010132
10133 /* If we have a conditional branch with a constant condition
10134 * make it an unconditional branch.
10135 */
Eric Biederman90089602004-05-28 14:11:54 +000010136 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010137 struct triple *targ;
10138 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010139 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010140 unuse_triple(RHS(ins, 0), ins);
10141 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010142 ins->rhs = 0;
10143 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010144 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010145 if (value) {
10146 unuse_triple(ins->next, ins);
10147 TARG(ins, 0) = targ;
10148 }
10149 else {
10150 unuse_triple(targ, ins);
10151 TARG(ins, 0) = ins->next;
10152 }
10153 }
Eric Biederman90089602004-05-28 14:11:54 +000010154
10155 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010156 * make it a noop.
10157 */
10158 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010159 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010160 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010161 unuse_triple(RHS(ins, 0), ins);
10162 unuse_triple(ins->next, ins);
10163 }
Eric Biederman90089602004-05-28 14:11:54 +000010164 ins->lhs = 0;
10165 ins->rhs = 0;
10166 ins->misc = 0;
10167 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010168 ins->op = OP_NOOP;
10169 if (ins->use) {
10170 internal_error(state, ins, "noop use != 0");
10171 }
10172 }
10173}
10174
Eric Biederman530b5192003-07-01 10:05:30 +000010175static void simplify_label(struct compile_state *state, struct triple *ins)
10176{
Eric Biederman83b991a2003-10-11 06:20:25 +000010177 /* Ignore volatile labels */
10178 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010179 return;
10180 }
10181 if (ins->use == 0) {
10182 ins->op = OP_NOOP;
10183 }
10184 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010185 /* In general it is not safe to merge one label that
10186 * imediately follows another. The problem is that the empty
10187 * looking block may have phi functions that depend on it.
10188 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010189 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010190 struct triple_set *user, *next;
10191 ins->op = OP_NOOP;
10192 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010193 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010194 next = user->next;
10195 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010196 expr = triple_targ(state, use, 0);
10197 for(;expr; expr = triple_targ(state, use, expr)) {
10198 if (*expr == ins) {
10199 *expr = ins->prev;
10200 unuse_triple(ins, use);
10201 use_triple(ins->prev, use);
10202 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010203
Eric Biederman530b5192003-07-01 10:05:30 +000010204 }
10205 }
10206 if (ins->use) {
10207 internal_error(state, ins, "noop use != 0");
10208 }
10209 }
10210 }
10211}
10212
Eric Biedermanb138ac82003-04-22 18:44:01 +000010213static void simplify_phi(struct compile_state *state, struct triple *ins)
10214{
Eric Biederman83b991a2003-10-11 06:20:25 +000010215 struct triple **slot;
10216 struct triple *value;
10217 int zrhs, i;
10218 ulong_t cvalue;
10219 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010220 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010221 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010222 return;
10223 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010224 /* See if all of the rhs members of a phi have the same value */
10225 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010226 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010227 for(i = 1; i < zrhs; i++) {
10228 if ( !slot[i] ||
10229 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010230 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010231 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010232 break;
10233 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010234 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010235 if (i == zrhs) {
10236 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010237 return;
10238 }
10239 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010240
Eric Biederman83b991a2003-10-11 06:20:25 +000010241 /* See if all of rhs members of a phi are the same */
10242 value = slot[0];
10243 for(i = 1; i < zrhs; i++) {
10244 if (slot[i] != value) {
10245 break;
10246 }
10247 }
10248 if (i == zrhs) {
10249 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010250 if (!is_subset_type(ins->type, value->type)) {
10251 internal_error(state, ins, "bad input type to phi");
10252 }
10253 /* Make the types match */
10254 if (!equiv_types(ins->type, value->type)) {
10255 ins->type = value->type;
10256 }
10257 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010258 mkcopy(state, ins, value);
10259 return;
10260 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010261}
10262
10263
10264static void simplify_bsf(struct compile_state *state, struct triple *ins)
10265{
Eric Biederman90089602004-05-28 14:11:54 +000010266 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010267 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010268 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010269 mkconst(state, ins, bsf(left));
10270 }
10271}
10272
10273static void simplify_bsr(struct compile_state *state, struct triple *ins)
10274{
Eric Biederman90089602004-05-28 14:11:54 +000010275 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010276 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010277 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010278 mkconst(state, ins, bsr(left));
10279 }
10280}
10281
10282
10283typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010284static const struct simplify_table {
10285 simplify_t func;
10286 unsigned long flag;
10287} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010288#define simplify_sdivt simplify_noop
10289#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010290#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010291
Eric Biederman5ade04a2003-10-22 04:03:46 +000010292[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10293[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10294[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10295[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10296[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10297[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10298[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10299[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10300[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10301[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10302[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10303[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10304[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10305[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10306[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10307[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10308[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10309[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10310[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010311
Eric Biederman5ade04a2003-10-22 04:03:46 +000010312[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10318[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10319[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10320[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10321[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10322[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10323[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010324
Eric Biederman90089602004-05-28 14:11:54 +000010325[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010326[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010327
Eric Biederman90089602004-05-28 14:11:54 +000010328[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10329[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10330[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10331
Eric Biederman5ade04a2003-10-22 04:03:46 +000010332[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010333
Eric Biederman5ade04a2003-10-22 04:03:46 +000010334[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10335[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10336[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010337[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010338
Eric Biederman5ade04a2003-10-22 04:03:46 +000010339[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10340[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10341[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010342[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010343[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10344[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010345
Eric Biederman5ade04a2003-10-22 04:03:46 +000010346[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010347[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010348
Eric Biederman5ade04a2003-10-22 04:03:46 +000010349[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10350[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10351[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10352[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10353[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10354[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10355[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10356[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10357[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010358
Eric Biederman5ade04a2003-10-22 04:03:46 +000010359[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10360[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10361[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10362[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10363[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10364[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10365[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10366[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10367[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010368[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010369[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010370};
10371
Stefan Reinauer14e22772010-04-27 06:56:47 +000010372static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010373 simplify_t do_simplify, struct triple *ins)
10374{
10375#if DEBUG_SIMPLIFY_HIRES
10376 if (state->functions_joined && (do_simplify != simplify_noop)) {
10377 /* High resolution debugging mode */
10378 fprintf(state->dbgout, "simplifing: ");
10379 display_triple(state->dbgout, ins);
10380 }
10381#endif
10382 do_simplify(state, ins);
10383#if DEBUG_SIMPLIFY_HIRES
10384 if (state->functions_joined && (do_simplify != simplify_noop)) {
10385 /* High resolution debugging mode */
10386 fprintf(state->dbgout, "simplified: ");
10387 display_triple(state->dbgout, ins);
10388 }
10389#endif
10390}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010391static void simplify(struct compile_state *state, struct triple *ins)
10392{
10393 int op;
10394 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010395 if (ins == &unknown_triple) {
10396 internal_error(state, ins, "simplifying the unknown triple?");
10397 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010398 do {
10399 op = ins->op;
10400 do_simplify = 0;
Patrick Georgi0dde01c2013-11-11 15:57:09 +010010401 if ((op < 0) || (op >= sizeof(table_simplify)/sizeof(table_simplify[0]))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010402 do_simplify = 0;
10403 }
Eric Biederman90089602004-05-28 14:11:54 +000010404 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010405 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010406 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010407 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010408 !(state->compiler->flags & table_simplify[op].flag)) {
10409 do_simplify = simplify_noop;
10410 }
10411 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010412 do_simplify = simplify_noop;
10413 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010414
Eric Biedermanb138ac82003-04-22 18:44:01 +000010415 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010416 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010417 op, tops(op));
10418 return;
10419 }
Eric Biederman90089602004-05-28 14:11:54 +000010420 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010421 } while(ins->op != op);
10422}
10423
Eric Biederman5ade04a2003-10-22 04:03:46 +000010424static void rebuild_ssa_form(struct compile_state *state);
10425
Eric Biedermanb138ac82003-04-22 18:44:01 +000010426static void simplify_all(struct compile_state *state)
10427{
10428 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010429 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10430 return;
10431 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010432 first = state->first;
10433 ins = first->prev;
10434 do {
10435 simplify(state, ins);
10436 ins = ins->prev;
10437 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010438 ins = first;
10439 do {
10440 simplify(state, ins);
10441 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010442 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010443 rebuild_ssa_form(state);
10444
Eric Biederman90089602004-05-28 14:11:54 +000010445 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010446}
10447
10448/*
10449 * Builtins....
10450 * ============================
10451 */
10452
Eric Biederman0babc1c2003-05-09 02:39:00 +000010453static void register_builtin_function(struct compile_state *state,
10454 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010455{
Eric Biederman90089602004-05-28 14:11:54 +000010456 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010457 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010458 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010459 struct file_state file;
10460 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010461 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010462 va_list args;
10463 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010464
10465 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010466 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010467 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010468 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010469 file.report_line = 1;
10470 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010471 file.prev = state->file;
10472 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010473 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010474
10475 /* Find the Parameter count */
10476 valid_op(state, op);
10477 parameters = table_ops[op].rhs;
10478 if (parameters < 0 ) {
10479 internal_error(state, 0, "Invalid builtin parameter count");
10480 }
10481
10482 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010483 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010484 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010485 next = &ftype->right;
10486 va_start(args, rtype);
10487 for(i = 0; i < parameters; i++) {
10488 atype = va_arg(args, struct type *);
10489 if (!*next) {
10490 *next = atype;
10491 } else {
10492 *next = new_type(TYPE_PRODUCT, *next, atype);
10493 next = &((*next)->right);
10494 }
10495 }
10496 if (!*next) {
10497 *next = &void_type;
10498 }
10499 va_end(args);
10500
Eric Biederman90089602004-05-28 14:11:54 +000010501 /* Get the initial closure type */
10502 ctype = new_type(TYPE_JOIN, &void_type, 0);
10503 ctype->elements = 1;
10504
10505 /* Get the return type */
10506 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10507 crtype->elements = 2;
10508
Eric Biedermanb138ac82003-04-22 18:44:01 +000010509 /* Generate the needed triples */
10510 def = triple(state, OP_LIST, ftype, 0, 0);
10511 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010512 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010513 result = flatten(state, first, variable(state, crtype));
10514 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010515 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010516
10517 /* Now string them together */
10518 param = ftype->right;
10519 for(i = 0; i < parameters; i++) {
10520 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10521 atype = param->left;
10522 } else {
10523 atype = param;
10524 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010525 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010526 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010527 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010528 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010529 generate_lhs_pieces(state, work);
10530 for(i = 0; i < parameters; i++) {
10531 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010532 }
Eric Biederman90089602004-05-28 14:11:54 +000010533 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10534 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010535 }
10536 work = flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010537 flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010538 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010539 name_len = strlen(name);
10540 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010541 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010542 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010543
Eric Biedermanb138ac82003-04-22 18:44:01 +000010544 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010545 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010546 state->main_function = 0;
10547
Eric Biederman5ade04a2003-10-22 04:03:46 +000010548 if (!state->functions) {
10549 state->functions = def;
10550 } else {
10551 insert_triple(state, state->functions, def);
10552 }
10553 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010554 FILE *fp = state->dbgout;
10555 fprintf(fp, "\n");
10556 loc(fp, state, 0);
10557 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10558 display_func(state, fp, def);
10559 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010560 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010561}
10562
Eric Biederman0babc1c2003-05-09 02:39:00 +000010563static struct type *partial_struct(struct compile_state *state,
10564 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010565{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010566 struct hash_entry *field_ident;
10567 struct type *result;
10568 int field_name_len;
10569
10570 field_name_len = strlen(field_name);
10571 field_ident = lookup(state, field_name, field_name_len);
10572
10573 result = clone_type(0, type);
10574 result->field_ident = field_ident;
10575
10576 if (rest) {
10577 result = new_type(TYPE_PRODUCT, result, rest);
10578 }
10579 return result;
10580}
10581
10582static struct type *register_builtin_type(struct compile_state *state,
10583 const char *name, struct type *type)
10584{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010585 struct hash_entry *ident;
10586 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010587
Eric Biedermanb138ac82003-04-22 18:44:01 +000010588 name_len = strlen(name);
10589 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010590
Eric Biederman0babc1c2003-05-09 02:39:00 +000010591 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10592 ulong_t elements = 0;
10593 struct type *field;
10594 type = new_type(TYPE_STRUCT, type, 0);
10595 field = type->left;
10596 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10597 elements++;
10598 field = field->right;
10599 }
10600 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010601 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010602 type->type_ident = ident;
10603 type->elements = elements;
10604 }
10605 symbol(state, ident, &ident->sym_ident, 0, type);
10606 ident->tok = TOK_TYPE_NAME;
10607 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010608}
10609
Eric Biederman0babc1c2003-05-09 02:39:00 +000010610
Eric Biedermanb138ac82003-04-22 18:44:01 +000010611static void register_builtins(struct compile_state *state)
10612{
Eric Biederman530b5192003-07-01 10:05:30 +000010613 struct type *div_type, *ldiv_type;
10614 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010615 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010616
Eric Biederman530b5192003-07-01 10:05:30 +000010617 div_type = register_builtin_type(state, "__builtin_div_t",
10618 partial_struct(state, "quot", &int_type,
10619 partial_struct(state, "rem", &int_type, 0)));
10620 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10621 partial_struct(state, "quot", &long_type,
10622 partial_struct(state, "rem", &long_type, 0)));
10623 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10624 partial_struct(state, "quot", &uint_type,
10625 partial_struct(state, "rem", &uint_type, 0)));
10626 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10627 partial_struct(state, "quot", &ulong_type,
10628 partial_struct(state, "rem", &ulong_type, 0)));
10629
10630 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10631 &int_type, &int_type);
10632 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10633 &long_type, &long_type);
10634 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10635 &uint_type, &uint_type);
10636 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10637 &ulong_type, &ulong_type);
10638
Stefan Reinauer14e22772010-04-27 06:56:47 +000010639 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010640 &ushort_type);
10641 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10642 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010643 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010644 &ushort_type);
10645
Stefan Reinauer14e22772010-04-27 06:56:47 +000010646 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010647 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010648 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010649 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010650 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010651 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010652
10653 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010654 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010655 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010656 &int_type);
10657
10658 msr_type = register_builtin_type(state, "__builtin_msr_t",
10659 partial_struct(state, "lo", &ulong_type,
10660 partial_struct(state, "hi", &ulong_type, 0)));
10661
10662 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10663 &ulong_type);
10664 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10665 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010666
10667 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010668 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010669}
10670
10671static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010672 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010673 struct hash_entry **ident, int need_ident);
10674static void decl(struct compile_state *state, struct triple *first);
10675static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010676#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010677static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010678#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010679static struct type *decl_specifiers(struct compile_state *state);
10680static int istype(int tok);
10681static struct triple *expr(struct compile_state *state);
10682static struct triple *assignment_expr(struct compile_state *state);
10683static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010684static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010685
10686static struct triple *call_expr(
10687 struct compile_state *state, struct triple *func)
10688{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010689 struct triple *def;
10690 struct type *param, *type;
10691 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010692
10693 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10694 error(state, 0, "Called object is not a function");
10695 }
10696 if (func->op != OP_LIST) {
10697 internal_error(state, 0, "improper function");
10698 }
10699 eat(state, TOK_LPAREN);
10700 /* Find the return type without any specifiers */
10701 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010702 /* Count the number of rhs entries for OP_FCALL */
10703 param = func->type->right;
10704 pvals = 0;
10705 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10706 pvals++;
10707 param = param->right;
10708 }
10709 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10710 pvals++;
10711 }
10712 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010713 MISC(def, 0) = func;
10714
10715 param = func->type->right;
10716 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010717 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010718 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010719 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010720 arg_type = param;
10721 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10722 arg_type = param->left;
10723 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010724 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010725 RHS(def, index) = val;
10726 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010727 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010728 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010729 }
10730 }
10731 eat(state, TOK_RPAREN);
10732 return def;
10733}
10734
10735
10736static struct triple *character_constant(struct compile_state *state)
10737{
10738 struct triple *def;
10739 struct token *tk;
10740 const signed char *str, *end;
10741 int c;
10742 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010743 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010744 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010745 str_len = tk->str_len - 2;
10746 if (str_len <= 0) {
10747 error(state, 0, "empty character constant");
10748 }
10749 end = str + str_len;
10750 c = char_value(state, &str, end);
10751 if (str != end) {
10752 error(state, 0, "multibyte character constant not supported");
10753 }
10754 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10755 return def;
10756}
10757
10758static struct triple *string_constant(struct compile_state *state)
10759{
10760 struct triple *def;
10761 struct token *tk;
10762 struct type *type;
10763 const signed char *str, *end;
10764 signed char *buf, *ptr;
10765 int str_len;
10766
10767 buf = 0;
10768 type = new_type(TYPE_ARRAY, &char_type, 0);
10769 type->elements = 0;
10770 /* The while loop handles string concatenation */
10771 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010772 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010773 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010774 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010775 if (str_len < 0) {
10776 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010777 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010778 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010779 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010780 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010781 end = str + str_len;
10782 ptr = buf;
10783 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10784 memcpy(buf, ptr, type->elements);
10785 ptr = buf + type->elements;
10786 do {
10787 *ptr++ = char_value(state, &str, end);
10788 } while(str < end);
10789 type->elements = ptr - buf;
10790 } while(peek(state) == TOK_LIT_STRING);
10791 *ptr = '\0';
10792 type->elements += 1;
10793 def = triple(state, OP_BLOBCONST, type, 0, 0);
10794 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010795
Eric Biedermanb138ac82003-04-22 18:44:01 +000010796 return def;
10797}
10798
10799
10800static struct triple *integer_constant(struct compile_state *state)
10801{
10802 struct triple *def;
10803 unsigned long val;
10804 struct token *tk;
10805 char *end;
10806 int u, l, decimal;
10807 struct type *type;
10808
Eric Biederman41203d92004-11-08 09:31:09 +000010809 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010810 errno = 0;
10811 decimal = (tk->val.str[0] != '0');
10812 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010813 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010814 error(state, 0, "Integer constant to large");
10815 }
10816 u = l = 0;
10817 if ((*end == 'u') || (*end == 'U')) {
10818 u = 1;
10819 end++;
10820 }
10821 if ((*end == 'l') || (*end == 'L')) {
10822 l = 1;
10823 end++;
10824 }
10825 if ((*end == 'u') || (*end == 'U')) {
10826 u = 1;
10827 end++;
10828 }
10829 if (*end) {
10830 error(state, 0, "Junk at end of integer constant");
10831 }
10832 if (u && l) {
10833 type = &ulong_type;
10834 }
10835 else if (l) {
10836 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010837 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010838 type = &ulong_type;
10839 }
10840 }
10841 else if (u) {
10842 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010843 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010844 type = &ulong_type;
10845 }
10846 }
10847 else {
10848 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010849 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010850 type = &uint_type;
10851 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010852 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010853 type = &ulong_type;
10854 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010855 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010856 type = &long_type;
10857 }
10858 }
10859 def = int_const(state, type, val);
10860 return def;
10861}
10862
10863static struct triple *primary_expr(struct compile_state *state)
10864{
10865 struct triple *def;
10866 int tok;
10867 tok = peek(state);
10868 switch(tok) {
10869 case TOK_IDENT:
10870 {
10871 struct hash_entry *ident;
10872 /* Here ident is either:
10873 * a varable name
10874 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010875 */
Eric Biederman41203d92004-11-08 09:31:09 +000010876 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010877 if (!ident->sym_ident) {
10878 error(state, 0, "%s undeclared", ident->name);
10879 }
10880 def = ident->sym_ident->def;
10881 break;
10882 }
10883 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010884 {
10885 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010886 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010887 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010888 if (!ident->sym_ident) {
10889 error(state, 0, "%s undeclared", ident->name);
10890 }
10891 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010892 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010893 }
Eric Biederman41203d92004-11-08 09:31:09 +000010894 case TOK_MIDENT:
10895 {
10896 struct hash_entry *ident;
10897 ident = eat(state, TOK_MIDENT)->ident;
10898 warning(state, 0, "Replacing undefined macro: %s with 0",
10899 ident->name);
10900 def = int_const(state, &int_type, 0);
10901 break;
10902 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010903 case TOK_LPAREN:
10904 eat(state, TOK_LPAREN);
10905 def = expr(state);
10906 eat(state, TOK_RPAREN);
10907 break;
10908 case TOK_LIT_INT:
10909 def = integer_constant(state);
10910 break;
10911 case TOK_LIT_FLOAT:
10912 eat(state, TOK_LIT_FLOAT);
10913 error(state, 0, "Floating point constants not supported");
10914 def = 0;
10915 FINISHME();
10916 break;
10917 case TOK_LIT_CHAR:
10918 def = character_constant(state);
10919 break;
10920 case TOK_LIT_STRING:
10921 def = string_constant(state);
10922 break;
10923 default:
10924 def = 0;
10925 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10926 }
10927 return def;
10928}
10929
10930static struct triple *postfix_expr(struct compile_state *state)
10931{
10932 struct triple *def;
10933 int postfix;
10934 def = primary_expr(state);
10935 do {
10936 struct triple *left;
10937 int tok;
10938 postfix = 1;
10939 left = def;
10940 switch((tok = peek(state))) {
10941 case TOK_LBRACKET:
10942 eat(state, TOK_LBRACKET);
10943 def = mk_subscript_expr(state, left, expr(state));
10944 eat(state, TOK_RBRACKET);
10945 break;
10946 case TOK_LPAREN:
10947 def = call_expr(state, def);
10948 break;
10949 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010950 {
10951 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010952 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010953 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010954 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010955 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010956 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010957 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010958 {
10959 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010960 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010961 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010962 def = mk_deref_expr(state, read_expr(state, def));
10963 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010964 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010965 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010966 case TOK_PLUSPLUS:
10967 eat(state, TOK_PLUSPLUS);
10968 def = mk_post_inc_expr(state, left);
10969 break;
10970 case TOK_MINUSMINUS:
10971 eat(state, TOK_MINUSMINUS);
10972 def = mk_post_dec_expr(state, left);
10973 break;
10974 default:
10975 postfix = 0;
10976 break;
10977 }
10978 } while(postfix);
10979 return def;
10980}
10981
10982static struct triple *cast_expr(struct compile_state *state);
10983
10984static struct triple *unary_expr(struct compile_state *state)
10985{
10986 struct triple *def, *right;
10987 int tok;
10988 switch((tok = peek(state))) {
10989 case TOK_PLUSPLUS:
10990 eat(state, TOK_PLUSPLUS);
10991 def = mk_pre_inc_expr(state, unary_expr(state));
10992 break;
10993 case TOK_MINUSMINUS:
10994 eat(state, TOK_MINUSMINUS);
10995 def = mk_pre_dec_expr(state, unary_expr(state));
10996 break;
10997 case TOK_AND:
10998 eat(state, TOK_AND);
10999 def = mk_addr_expr(state, cast_expr(state), 0);
11000 break;
11001 case TOK_STAR:
11002 eat(state, TOK_STAR);
11003 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11004 break;
11005 case TOK_PLUS:
11006 eat(state, TOK_PLUS);
11007 right = read_expr(state, cast_expr(state));
11008 arithmetic(state, right);
11009 def = integral_promotion(state, right);
11010 break;
11011 case TOK_MINUS:
11012 eat(state, TOK_MINUS);
11013 right = read_expr(state, cast_expr(state));
11014 arithmetic(state, right);
11015 def = integral_promotion(state, right);
11016 def = triple(state, OP_NEG, def->type, def, 0);
11017 break;
11018 case TOK_TILDE:
11019 eat(state, TOK_TILDE);
11020 right = read_expr(state, cast_expr(state));
11021 integral(state, right);
11022 def = integral_promotion(state, right);
11023 def = triple(state, OP_INVERT, def->type, def, 0);
11024 break;
11025 case TOK_BANG:
11026 eat(state, TOK_BANG);
11027 right = read_expr(state, cast_expr(state));
11028 bool(state, right);
11029 def = lfalse_expr(state, right);
11030 break;
11031 case TOK_SIZEOF:
11032 {
11033 struct type *type;
11034 int tok1, tok2;
11035 eat(state, TOK_SIZEOF);
11036 tok1 = peek(state);
11037 tok2 = peek2(state);
11038 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11039 eat(state, TOK_LPAREN);
11040 type = type_name(state);
11041 eat(state, TOK_RPAREN);
11042 }
11043 else {
11044 struct triple *expr;
11045 expr = unary_expr(state);
11046 type = expr->type;
11047 release_expr(state, expr);
11048 }
Eric Biederman90089602004-05-28 14:11:54 +000011049 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011050 break;
11051 }
11052 case TOK_ALIGNOF:
11053 {
11054 struct type *type;
11055 int tok1, tok2;
11056 eat(state, TOK_ALIGNOF);
11057 tok1 = peek(state);
11058 tok2 = peek2(state);
11059 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11060 eat(state, TOK_LPAREN);
11061 type = type_name(state);
11062 eat(state, TOK_RPAREN);
11063 }
11064 else {
11065 struct triple *expr;
11066 expr = unary_expr(state);
11067 type = expr->type;
11068 release_expr(state, expr);
11069 }
Eric Biederman90089602004-05-28 14:11:54 +000011070 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011071 break;
11072 }
Eric Biederman41203d92004-11-08 09:31:09 +000011073 case TOK_MDEFINED:
11074 {
11075 /* We only come here if we are called from the preprocessor */
11076 struct hash_entry *ident;
11077 int parens;
11078 eat(state, TOK_MDEFINED);
11079 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011080 if (pp_peek(state) == TOK_LPAREN) {
11081 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011082 parens = 1;
11083 }
Eric Biedermancb364952004-11-15 10:46:44 +000011084 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011085 if (parens) {
11086 eat(state, TOK_RPAREN);
11087 }
11088 def = int_const(state, &int_type, ident->sym_define != 0);
11089 break;
11090 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011091 default:
11092 def = postfix_expr(state);
11093 break;
11094 }
11095 return def;
11096}
11097
11098static struct triple *cast_expr(struct compile_state *state)
11099{
11100 struct triple *def;
11101 int tok1, tok2;
11102 tok1 = peek(state);
11103 tok2 = peek2(state);
11104 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11105 struct type *type;
11106 eat(state, TOK_LPAREN);
11107 type = type_name(state);
11108 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011109 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011110 }
11111 else {
11112 def = unary_expr(state);
11113 }
11114 return def;
11115}
11116
11117static struct triple *mult_expr(struct compile_state *state)
11118{
11119 struct triple *def;
11120 int done;
11121 def = cast_expr(state);
11122 do {
11123 struct triple *left, *right;
11124 struct type *result_type;
11125 int tok, op, sign;
11126 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011127 tok = peek(state);
11128 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011129 case TOK_STAR:
11130 case TOK_DIV:
11131 case TOK_MOD:
11132 left = read_expr(state, def);
11133 arithmetic(state, left);
11134
11135 eat(state, tok);
11136
11137 right = read_expr(state, cast_expr(state));
11138 arithmetic(state, right);
11139
11140 result_type = arithmetic_result(state, left, right);
11141 sign = is_signed(result_type);
11142 op = -1;
11143 switch(tok) {
11144 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11145 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11146 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11147 }
11148 def = triple(state, op, result_type, left, right);
11149 break;
11150 default:
11151 done = 1;
11152 break;
11153 }
11154 } while(!done);
11155 return def;
11156}
11157
11158static struct triple *add_expr(struct compile_state *state)
11159{
11160 struct triple *def;
11161 int done;
11162 def = mult_expr(state);
11163 do {
11164 done = 0;
11165 switch( peek(state)) {
11166 case TOK_PLUS:
11167 eat(state, TOK_PLUS);
11168 def = mk_add_expr(state, def, mult_expr(state));
11169 break;
11170 case TOK_MINUS:
11171 eat(state, TOK_MINUS);
11172 def = mk_sub_expr(state, def, mult_expr(state));
11173 break;
11174 default:
11175 done = 1;
11176 break;
11177 }
11178 } while(!done);
11179 return def;
11180}
11181
11182static struct triple *shift_expr(struct compile_state *state)
11183{
11184 struct triple *def;
11185 int done;
11186 def = add_expr(state);
11187 do {
11188 struct triple *left, *right;
11189 int tok, op;
11190 done = 0;
11191 switch((tok = peek(state))) {
11192 case TOK_SL:
11193 case TOK_SR:
11194 left = read_expr(state, def);
11195 integral(state, left);
11196 left = integral_promotion(state, left);
11197
11198 eat(state, tok);
11199
11200 right = read_expr(state, add_expr(state));
11201 integral(state, right);
11202 right = integral_promotion(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011203
11204 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011205 is_signed(left->type)? OP_SSR: OP_USR;
11206
11207 def = triple(state, op, left->type, left, right);
11208 break;
11209 default:
11210 done = 1;
11211 break;
11212 }
11213 } while(!done);
11214 return def;
11215}
11216
11217static struct triple *relational_expr(struct compile_state *state)
11218{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011219#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011220#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011221#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011222 struct triple *def;
11223 int done;
11224 def = shift_expr(state);
11225 do {
11226 struct triple *left, *right;
11227 struct type *arg_type;
11228 int tok, op, sign;
11229 done = 0;
11230 switch((tok = peek(state))) {
11231 case TOK_LESS:
11232 case TOK_MORE:
11233 case TOK_LESSEQ:
11234 case TOK_MOREEQ:
11235 left = read_expr(state, def);
11236 arithmetic(state, left);
11237
11238 eat(state, tok);
11239
11240 right = read_expr(state, shift_expr(state));
11241 arithmetic(state, right);
11242
11243 arg_type = arithmetic_result(state, left, right);
11244 sign = is_signed(arg_type);
11245 op = -1;
11246 switch(tok) {
11247 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11248 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11249 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11250 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11251 }
11252 def = triple(state, op, &int_type, left, right);
11253 break;
11254 default:
11255 done = 1;
11256 break;
11257 }
11258 } while(!done);
11259 return def;
11260}
11261
11262static struct triple *equality_expr(struct compile_state *state)
11263{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011264#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011265#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011266#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011267 struct triple *def;
11268 int done;
11269 def = relational_expr(state);
11270 do {
11271 struct triple *left, *right;
11272 int tok, op;
11273 done = 0;
11274 switch((tok = peek(state))) {
11275 case TOK_EQEQ:
11276 case TOK_NOTEQ:
11277 left = read_expr(state, def);
11278 arithmetic(state, left);
11279 eat(state, tok);
11280 right = read_expr(state, relational_expr(state));
11281 arithmetic(state, right);
11282 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11283 def = triple(state, op, &int_type, left, right);
11284 break;
11285 default:
11286 done = 1;
11287 break;
11288 }
11289 } while(!done);
11290 return def;
11291}
11292
11293static struct triple *and_expr(struct compile_state *state)
11294{
11295 struct triple *def;
11296 def = equality_expr(state);
11297 while(peek(state) == TOK_AND) {
11298 struct triple *left, *right;
11299 struct type *result_type;
11300 left = read_expr(state, def);
11301 integral(state, left);
11302 eat(state, TOK_AND);
11303 right = read_expr(state, equality_expr(state));
11304 integral(state, right);
11305 result_type = arithmetic_result(state, left, right);
11306 def = triple(state, OP_AND, result_type, left, right);
11307 }
11308 return def;
11309}
11310
11311static struct triple *xor_expr(struct compile_state *state)
11312{
11313 struct triple *def;
11314 def = and_expr(state);
11315 while(peek(state) == TOK_XOR) {
11316 struct triple *left, *right;
11317 struct type *result_type;
11318 left = read_expr(state, def);
11319 integral(state, left);
11320 eat(state, TOK_XOR);
11321 right = read_expr(state, and_expr(state));
11322 integral(state, right);
11323 result_type = arithmetic_result(state, left, right);
11324 def = triple(state, OP_XOR, result_type, left, right);
11325 }
11326 return def;
11327}
11328
11329static struct triple *or_expr(struct compile_state *state)
11330{
11331 struct triple *def;
11332 def = xor_expr(state);
11333 while(peek(state) == TOK_OR) {
11334 struct triple *left, *right;
11335 struct type *result_type;
11336 left = read_expr(state, def);
11337 integral(state, left);
11338 eat(state, TOK_OR);
11339 right = read_expr(state, xor_expr(state));
11340 integral(state, right);
11341 result_type = arithmetic_result(state, left, right);
11342 def = triple(state, OP_OR, result_type, left, right);
11343 }
11344 return def;
11345}
11346
11347static struct triple *land_expr(struct compile_state *state)
11348{
11349 struct triple *def;
11350 def = or_expr(state);
11351 while(peek(state) == TOK_LOGAND) {
11352 struct triple *left, *right;
11353 left = read_expr(state, def);
11354 bool(state, left);
11355 eat(state, TOK_LOGAND);
11356 right = read_expr(state, or_expr(state));
11357 bool(state, right);
11358
Eric Biederman90089602004-05-28 14:11:54 +000011359 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011360 ltrue_expr(state, left),
11361 ltrue_expr(state, right));
11362 }
11363 return def;
11364}
11365
11366static struct triple *lor_expr(struct compile_state *state)
11367{
11368 struct triple *def;
11369 def = land_expr(state);
11370 while(peek(state) == TOK_LOGOR) {
11371 struct triple *left, *right;
11372 left = read_expr(state, def);
11373 bool(state, left);
11374 eat(state, TOK_LOGOR);
11375 right = read_expr(state, land_expr(state));
11376 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011377
Stefan Reinauer14e22772010-04-27 06:56:47 +000011378 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011379 ltrue_expr(state, left),
11380 ltrue_expr(state, right));
11381 }
11382 return def;
11383}
11384
11385static struct triple *conditional_expr(struct compile_state *state)
11386{
11387 struct triple *def;
11388 def = lor_expr(state);
11389 if (peek(state) == TOK_QUEST) {
11390 struct triple *test, *left, *right;
11391 bool(state, def);
11392 test = ltrue_expr(state, read_expr(state, def));
11393 eat(state, TOK_QUEST);
11394 left = read_expr(state, expr(state));
11395 eat(state, TOK_COLON);
11396 right = read_expr(state, conditional_expr(state));
11397
Eric Biederman90089602004-05-28 14:11:54 +000011398 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011399 }
11400 return def;
11401}
11402
Eric Biederman41203d92004-11-08 09:31:09 +000011403struct cv_triple {
11404 struct triple *val;
11405 int id;
11406};
11407
11408static void set_cv(struct compile_state *state, struct cv_triple *cv,
11409 struct triple *dest, struct triple *val)
11410{
11411 if (cv[dest->id].val) {
11412 free_triple(state, cv[dest->id].val);
11413 }
11414 cv[dest->id].val = val;
11415}
11416static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11417 struct triple *src)
11418{
11419 return cv[src->id].val;
11420}
11421
Eric Biedermanb138ac82003-04-22 18:44:01 +000011422static struct triple *eval_const_expr(
11423 struct compile_state *state, struct triple *expr)
11424{
11425 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011426 if (is_const(expr)) {
11427 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011428 }
Eric Biederman00443072003-06-24 12:34:45 +000011429 else {
11430 /* If we don't start out as a constant simplify into one */
11431 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011432 struct cv_triple *cv;
11433 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011434 head = label(state); /* dummy initial triple */
11435 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011436 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011437 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011438 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011439 }
Eric Biederman41203d92004-11-08 09:31:09 +000011440 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11441 i = 1;
11442 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11443 cv[i].val = 0;
11444 cv[i].id = ptr->id;
11445 ptr->id = i;
11446 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011447 }
Eric Biederman41203d92004-11-08 09:31:09 +000011448 ptr = head->next;
11449 do {
11450 valid_ins(state, ptr);
11451 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011452 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011453 "unexpected %s in constant expression",
11454 tops(ptr->op));
11455 }
11456 else if (ptr->op == OP_LIST) {
11457 }
11458 else if (triple_is_structural(state, ptr)) {
11459 ptr = ptr->next;
11460 }
11461 else if (triple_is_ubranch(state, ptr)) {
11462 ptr = TARG(ptr, 0);
11463 }
11464 else if (triple_is_cbranch(state, ptr)) {
11465 struct triple *cond_val;
11466 cond_val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011467 if (!cond_val || !is_const(cond_val) ||
11468 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011469 {
11470 internal_error(state, ptr, "bad branch condition");
11471 }
11472 if (cond_val->u.cval == 0) {
11473 ptr = ptr->next;
11474 } else {
11475 ptr = TARG(ptr, 0);
11476 }
11477 }
11478 else if (triple_is_branch(state, ptr)) {
11479 error(state, ptr, "bad branch type in constant expression");
11480 }
11481 else if (ptr->op == OP_WRITE) {
11482 struct triple *val;
11483 val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011484
11485 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011486 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011487 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011488 copy_triple(state, val));
11489 ptr = ptr->next;
11490 }
11491 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011492 set_cv(state, cv, ptr,
11493 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011494 get_cv(state, cv, RHS(ptr, 0))));
11495 ptr = ptr->next;
11496 }
11497 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11498 struct triple *val, **rhs;
11499 val = copy_triple(state, ptr);
11500 rhs = triple_rhs(state, val, 0);
11501 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11502 if (!*rhs) {
11503 internal_error(state, ptr, "Missing rhs");
11504 }
11505 *rhs = get_cv(state, cv, *rhs);
11506 }
11507 simplify(state, val);
11508 set_cv(state, cv, ptr, val);
11509 ptr = ptr->next;
11510 }
11511 else {
11512 error(state, ptr, "impure operation in constant expression");
11513 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011514
Eric Biederman41203d92004-11-08 09:31:09 +000011515 } while(ptr != head);
11516
11517 /* Get the result value */
11518 def = get_cv(state, cv, head->prev);
11519 cv[head->prev->id].val = 0;
11520
11521 /* Free the temporary values */
11522 for(i = 0; i < count; i++) {
11523 if (cv[i].val) {
11524 free_triple(state, cv[i].val);
11525 cv[i].val = 0;
11526 }
11527 }
11528 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011529 /* Free the intermediate expressions */
11530 while(head->next != head) {
11531 release_triple(state, head->next);
11532 }
11533 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011534 }
Eric Biederman41203d92004-11-08 09:31:09 +000011535 if (!is_const(def)) {
11536 error(state, expr, "Not a constant expression");
11537 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011538 return def;
11539}
11540
11541static struct triple *constant_expr(struct compile_state *state)
11542{
11543 return eval_const_expr(state, conditional_expr(state));
11544}
11545
11546static struct triple *assignment_expr(struct compile_state *state)
11547{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011548 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011549 int tok, op, sign;
11550 /* The C grammer in K&R shows assignment expressions
11551 * only taking unary expressions as input on their
11552 * left hand side. But specifies the precedence of
11553 * assignemnt as the lowest operator except for comma.
11554 *
11555 * Allowing conditional expressions on the left hand side
11556 * of an assignement results in a grammar that accepts
11557 * a larger set of statements than standard C. As long
11558 * as the subset of the grammar that is standard C behaves
11559 * correctly this should cause no problems.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011560 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011561 * For the extra token strings accepted by the grammar
11562 * none of them should produce a valid lvalue, so they
11563 * should not produce functioning programs.
11564 *
11565 * GCC has this bug as well, so surprises should be minimal.
11566 */
11567 def = conditional_expr(state);
11568 left = def;
11569 switch((tok = peek(state))) {
11570 case TOK_EQ:
11571 lvalue(state, left);
11572 eat(state, TOK_EQ);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011573 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011574 read_expr(state, assignment_expr(state)));
11575 break;
11576 case TOK_TIMESEQ:
11577 case TOK_DIVEQ:
11578 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011579 lvalue(state, left);
11580 arithmetic(state, left);
11581 eat(state, tok);
11582 right = read_expr(state, assignment_expr(state));
11583 arithmetic(state, right);
11584
11585 sign = is_signed(left->type);
11586 op = -1;
11587 switch(tok) {
11588 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11589 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11590 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011591 }
11592 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011593 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011594 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011595 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011596 case TOK_PLUSEQ:
11597 lvalue(state, left);
11598 eat(state, TOK_PLUSEQ);
11599 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011600 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011601 break;
11602 case TOK_MINUSEQ:
11603 lvalue(state, left);
11604 eat(state, TOK_MINUSEQ);
11605 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011606 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011607 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011608 case TOK_SLEQ:
11609 case TOK_SREQ:
11610 case TOK_ANDEQ:
11611 case TOK_XOREQ:
11612 case TOK_OREQ:
11613 lvalue(state, left);
11614 integral(state, left);
11615 eat(state, tok);
11616 right = read_expr(state, assignment_expr(state));
11617 integral(state, right);
11618 right = integral_promotion(state, right);
11619 sign = is_signed(left->type);
11620 op = -1;
11621 switch(tok) {
11622 case TOK_SLEQ: op = OP_SL; break;
11623 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11624 case TOK_ANDEQ: op = OP_AND; break;
11625 case TOK_XOREQ: op = OP_XOR; break;
11626 case TOK_OREQ: op = OP_OR; break;
11627 }
11628 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011629 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011630 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011631 break;
11632 }
11633 return def;
11634}
11635
11636static struct triple *expr(struct compile_state *state)
11637{
11638 struct triple *def;
11639 def = assignment_expr(state);
11640 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011641 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011642 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011643 }
11644 return def;
11645}
11646
11647static void expr_statement(struct compile_state *state, struct triple *first)
11648{
11649 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011650 /* lvalue conversions always apply except when certian operators
11651 * are applied. I apply the lvalue conversions here
11652 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011653 */
11654 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011655 }
11656 eat(state, TOK_SEMI);
11657}
11658
11659static void if_statement(struct compile_state *state, struct triple *first)
11660{
11661 struct triple *test, *jmp1, *jmp2, *middle, *end;
11662
11663 jmp1 = jmp2 = middle = 0;
11664 eat(state, TOK_IF);
11665 eat(state, TOK_LPAREN);
11666 test = expr(state);
11667 bool(state, test);
11668 /* Cleanup and invert the test */
11669 test = lfalse_expr(state, read_expr(state, test));
11670 eat(state, TOK_RPAREN);
11671 /* Generate the needed pieces */
11672 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011673 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011674 /* Thread the pieces together */
11675 flatten(state, first, test);
11676 flatten(state, first, jmp1);
11677 flatten(state, first, label(state));
11678 statement(state, first);
11679 if (peek(state) == TOK_ELSE) {
11680 eat(state, TOK_ELSE);
11681 /* Generate the rest of the pieces */
11682 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011683 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011684 /* Thread them together */
11685 flatten(state, first, jmp2);
11686 flatten(state, first, middle);
11687 statement(state, first);
11688 flatten(state, first, end);
11689 }
11690 else {
11691 flatten(state, first, middle);
11692 }
11693}
11694
11695static void for_statement(struct compile_state *state, struct triple *first)
11696{
11697 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11698 struct triple *label1, *label2, *label3;
11699 struct hash_entry *ident;
11700
11701 eat(state, TOK_FOR);
11702 eat(state, TOK_LPAREN);
11703 head = test = tail = jmp1 = jmp2 = 0;
11704 if (peek(state) != TOK_SEMI) {
11705 head = expr(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011706 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011707 eat(state, TOK_SEMI);
11708 if (peek(state) != TOK_SEMI) {
11709 test = expr(state);
11710 bool(state, test);
11711 test = ltrue_expr(state, read_expr(state, test));
11712 }
11713 eat(state, TOK_SEMI);
11714 if (peek(state) != TOK_RPAREN) {
11715 tail = expr(state);
11716 }
11717 eat(state, TOK_RPAREN);
11718 /* Generate the needed pieces */
11719 label1 = label(state);
11720 label2 = label(state);
11721 label3 = label(state);
11722 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011723 jmp1 = branch(state, label3, 0);
11724 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011725 }
11726 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011727 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011728 }
11729 end = label(state);
11730 /* Remember where break and continue go */
11731 start_scope(state);
11732 ident = state->i_break;
11733 symbol(state, ident, &ident->sym_ident, end, end->type);
11734 ident = state->i_continue;
11735 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11736 /* Now include the body */
11737 flatten(state, first, head);
11738 flatten(state, first, jmp1);
11739 flatten(state, first, label1);
11740 statement(state, first);
11741 flatten(state, first, label2);
11742 flatten(state, first, tail);
11743 flatten(state, first, label3);
11744 flatten(state, first, test);
11745 flatten(state, first, jmp2);
11746 flatten(state, first, end);
11747 /* Cleanup the break/continue scope */
11748 end_scope(state);
11749}
11750
11751static void while_statement(struct compile_state *state, struct triple *first)
11752{
11753 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11754 struct hash_entry *ident;
11755 eat(state, TOK_WHILE);
11756 eat(state, TOK_LPAREN);
11757 test = expr(state);
11758 bool(state, test);
11759 test = ltrue_expr(state, read_expr(state, test));
11760 eat(state, TOK_RPAREN);
11761 /* Generate the needed pieces */
11762 label1 = label(state);
11763 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011764 jmp1 = branch(state, label2, 0);
11765 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011766 end = label(state);
11767 /* Remember where break and continue go */
11768 start_scope(state);
11769 ident = state->i_break;
11770 symbol(state, ident, &ident->sym_ident, end, end->type);
11771 ident = state->i_continue;
11772 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11773 /* Thread them together */
11774 flatten(state, first, jmp1);
11775 flatten(state, first, label1);
11776 statement(state, first);
11777 flatten(state, first, label2);
11778 flatten(state, first, test);
11779 flatten(state, first, jmp2);
11780 flatten(state, first, end);
11781 /* Cleanup the break/continue scope */
11782 end_scope(state);
11783}
11784
11785static void do_statement(struct compile_state *state, struct triple *first)
11786{
11787 struct triple *label1, *label2, *test, *end;
11788 struct hash_entry *ident;
11789 eat(state, TOK_DO);
11790 /* Generate the needed pieces */
11791 label1 = label(state);
11792 label2 = label(state);
11793 end = label(state);
11794 /* Remember where break and continue go */
11795 start_scope(state);
11796 ident = state->i_break;
11797 symbol(state, ident, &ident->sym_ident, end, end->type);
11798 ident = state->i_continue;
11799 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11800 /* Now include the body */
11801 flatten(state, first, label1);
11802 statement(state, first);
11803 /* Cleanup the break/continue scope */
11804 end_scope(state);
11805 /* Eat the rest of the loop */
11806 eat(state, TOK_WHILE);
11807 eat(state, TOK_LPAREN);
11808 test = read_expr(state, expr(state));
11809 bool(state, test);
11810 eat(state, TOK_RPAREN);
11811 eat(state, TOK_SEMI);
11812 /* Thread the pieces together */
11813 test = ltrue_expr(state, test);
11814 flatten(state, first, label2);
11815 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011816 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011817 flatten(state, first, end);
11818}
11819
11820
11821static void return_statement(struct compile_state *state, struct triple *first)
11822{
11823 struct triple *jmp, *mv, *dest, *var, *val;
11824 int last;
11825 eat(state, TOK_RETURN);
11826
Stefan Reinauer50542a82007-10-24 11:14:14 +000011827#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011828#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011829#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011830 val = 0;
11831 /* If we have a return value do some more work */
11832 if (peek(state) != TOK_SEMI) {
11833 val = read_expr(state, expr(state));
11834 }
11835 eat(state, TOK_SEMI);
11836
11837 /* See if this last statement in a function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011838 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011839 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11840
11841 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011842 var = fresult(state, state->main_function);
11843
Eric Biedermanb138ac82003-04-22 18:44:01 +000011844 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011845 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011846 mv = jmp = 0;
11847 /* If needed generate a jump instruction */
11848 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011849 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011850 }
11851 /* If needed generate an assignment instruction */
11852 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011853 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011854 }
11855 /* Now put the code together */
11856 if (mv) {
11857 flatten(state, first, mv);
11858 flatten(state, first, jmp);
11859 }
11860 else if (jmp) {
11861 flatten(state, first, jmp);
11862 }
11863}
11864
11865static void break_statement(struct compile_state *state, struct triple *first)
11866{
11867 struct triple *dest;
11868 eat(state, TOK_BREAK);
11869 eat(state, TOK_SEMI);
11870 if (!state->i_break->sym_ident) {
11871 error(state, 0, "break statement not within loop or switch");
11872 }
11873 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011874 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011875}
11876
11877static void continue_statement(struct compile_state *state, struct triple *first)
11878{
11879 struct triple *dest;
11880 eat(state, TOK_CONTINUE);
11881 eat(state, TOK_SEMI);
11882 if (!state->i_continue->sym_ident) {
11883 error(state, 0, "continue statement outside of a loop");
11884 }
11885 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011886 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011887}
11888
11889static void goto_statement(struct compile_state *state, struct triple *first)
11890{
Eric Biederman153ea352003-06-20 14:43:20 +000011891 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011892 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011893 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011894 if (!ident->sym_label) {
11895 /* If this is a forward branch allocate the label now,
11896 * it will be flattend in the appropriate location later.
11897 */
11898 struct triple *ins;
11899 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011900 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011901 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011902 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011903
11904 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011905}
11906
11907static void labeled_statement(struct compile_state *state, struct triple *first)
11908{
Eric Biederman153ea352003-06-20 14:43:20 +000011909 struct triple *ins;
11910 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011911
Eric Biederman41203d92004-11-08 09:31:09 +000011912 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011913 if (ident->sym_label && ident->sym_label->def) {
11914 ins = ident->sym_label->def;
11915 put_occurance(ins->occurance);
11916 ins->occurance = new_occurance(state);
11917 }
11918 else {
11919 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011920 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011921 }
11922 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11923 error(state, 0, "label %s already defined", ident->name);
11924 }
11925 flatten(state, first, ins);
11926
Eric Biedermanb138ac82003-04-22 18:44:01 +000011927 eat(state, TOK_COLON);
11928 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011929}
11930
11931static void switch_statement(struct compile_state *state, struct triple *first)
11932{
Eric Biederman83b991a2003-10-11 06:20:25 +000011933 struct triple *value, *top, *end, *dbranch;
11934 struct hash_entry *ident;
11935
11936 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011937 eat(state, TOK_SWITCH);
11938 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011939 value = expr(state);
11940 integral(state, value);
11941 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011942 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011943 /* Generate the needed pieces */
11944 top = label(state);
11945 end = label(state);
11946 dbranch = branch(state, end, 0);
11947 /* Remember where case branches and break goes */
11948 start_scope(state);
11949 ident = state->i_switch;
11950 symbol(state, ident, &ident->sym_ident, value, value->type);
11951 ident = state->i_case;
11952 symbol(state, ident, &ident->sym_ident, top, top->type);
11953 ident = state->i_break;
11954 symbol(state, ident, &ident->sym_ident, end, end->type);
11955 ident = state->i_default;
11956 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11957 /* Thread them together */
11958 flatten(state, first, value);
11959 flatten(state, first, top);
11960 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011961 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011962 flatten(state, first, end);
11963 /* Cleanup the switch scope */
11964 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011965}
11966
11967static void case_statement(struct compile_state *state, struct triple *first)
11968{
Eric Biederman83b991a2003-10-11 06:20:25 +000011969 struct triple *cvalue, *dest, *test, *jmp;
11970 struct triple *ptr, *value, *top, *dbranch;
11971
11972 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011973 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011974 cvalue = constant_expr(state);
11975 integral(state, cvalue);
11976 if (cvalue->op != OP_INTCONST) {
11977 error(state, 0, "integer constant expected");
11978 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011979 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011980 if (!state->i_case->sym_ident) {
11981 error(state, 0, "case statement not within a switch");
11982 }
11983
11984 /* Lookup the interesting pieces */
11985 top = state->i_case->sym_ident->def;
11986 value = state->i_switch->sym_ident->def;
11987 dbranch = state->i_default->sym_ident->def;
11988
11989 /* See if this case label has already been used */
11990 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11991 if (ptr->op != OP_EQ) {
11992 continue;
11993 }
11994 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11995 error(state, 0, "duplicate case %d statement",
11996 cvalue->u.cval);
11997 }
11998 }
11999 /* Generate the needed pieces */
12000 dest = label(state);
12001 test = triple(state, OP_EQ, &int_type, value, cvalue);
12002 jmp = branch(state, dest, test);
12003 /* Thread the pieces together */
12004 flatten(state, dbranch, test);
12005 flatten(state, dbranch, jmp);
12006 flatten(state, dbranch, label(state));
12007 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012008 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012009}
12010
12011static void default_statement(struct compile_state *state, struct triple *first)
12012{
Eric Biederman83b991a2003-10-11 06:20:25 +000012013 struct triple *dest;
12014 struct triple *dbranch, *end;
12015
12016 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012017 eat(state, TOK_DEFAULT);
12018 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012019
12020 if (!state->i_case->sym_ident) {
12021 error(state, 0, "default statement not within a switch");
12022 }
12023
12024 /* Lookup the interesting pieces */
12025 dbranch = state->i_default->sym_ident->def;
12026 end = state->i_break->sym_ident->def;
12027
12028 /* See if a default statement has already happened */
12029 if (TARG(dbranch, 0) != end) {
12030 error(state, 0, "duplicate default statement");
12031 }
12032
12033 /* Generate the needed pieces */
12034 dest = label(state);
12035
Eric Biederman90089602004-05-28 14:11:54 +000012036 /* Blame the branch on the default statement */
12037 put_occurance(dbranch->occurance);
12038 dbranch->occurance = new_occurance(state);
12039
Eric Biederman83b991a2003-10-11 06:20:25 +000012040 /* Thread the pieces together */
12041 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012042 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012043 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012044 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012045}
12046
12047static void asm_statement(struct compile_state *state, struct triple *first)
12048{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012049 struct asm_info *info;
12050 struct {
12051 struct triple *constraint;
12052 struct triple *expr;
12053 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12054 struct triple *def, *asm_str;
12055 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012056 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012057
Eric Biederman90089602004-05-28 14:11:54 +000012058 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012059 eat(state, TOK_ASM);
12060 /* For now ignore the qualifiers */
12061 switch(peek(state)) {
12062 case TOK_CONST:
12063 eat(state, TOK_CONST);
12064 break;
12065 case TOK_VOLATILE:
12066 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012067 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012068 break;
12069 }
12070 eat(state, TOK_LPAREN);
12071 asm_str = string_constant(state);
12072
12073 colons = 0;
12074 out = in = clobbers = 0;
12075 /* Outputs */
12076 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12077 eat(state, TOK_COLON);
12078 colons++;
12079 more = (peek(state) == TOK_LIT_STRING);
12080 while(more) {
12081 struct triple *var;
12082 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012083 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012084 more = 0;
12085 if (out > MAX_LHS) {
12086 error(state, 0, "Maximum output count exceeded.");
12087 }
12088 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012089 str = constraint->u.blob;
12090 if (str[0] != '=') {
12091 error(state, 0, "Output constraint does not start with =");
12092 }
12093 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012094 eat(state, TOK_LPAREN);
12095 var = conditional_expr(state);
12096 eat(state, TOK_RPAREN);
12097
12098 lvalue(state, var);
12099 out_param[out].constraint = constraint;
12100 out_param[out].expr = var;
12101 if (peek(state) == TOK_COMMA) {
12102 eat(state, TOK_COMMA);
12103 more = 1;
12104 }
12105 out++;
12106 }
12107 }
12108 /* Inputs */
12109 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12110 eat(state, TOK_COLON);
12111 colons++;
12112 more = (peek(state) == TOK_LIT_STRING);
12113 while(more) {
12114 struct triple *val;
12115 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012116 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012117 more = 0;
12118 if (in > MAX_RHS) {
12119 error(state, 0, "Maximum input count exceeded.");
12120 }
12121 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012122 str = constraint->u.blob;
12123 if (digitp(str[0] && str[1] == '\0')) {
12124 int val;
12125 val = digval(str[0]);
12126 if ((val < 0) || (val >= out)) {
12127 error(state, 0, "Invalid input constraint %d", val);
12128 }
12129 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012130 eat(state, TOK_LPAREN);
12131 val = conditional_expr(state);
12132 eat(state, TOK_RPAREN);
12133
12134 in_param[in].constraint = constraint;
12135 in_param[in].expr = val;
12136 if (peek(state) == TOK_COMMA) {
12137 eat(state, TOK_COMMA);
12138 more = 1;
12139 }
12140 in++;
12141 }
12142 }
12143
12144 /* Clobber */
12145 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12146 eat(state, TOK_COLON);
12147 colons++;
12148 more = (peek(state) == TOK_LIT_STRING);
12149 while(more) {
12150 struct triple *clobber;
12151 more = 0;
12152 if ((clobbers + out) > MAX_LHS) {
12153 error(state, 0, "Maximum clobber limit exceeded.");
12154 }
12155 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012156
12157 clob_param[clobbers].constraint = clobber;
12158 if (peek(state) == TOK_COMMA) {
12159 eat(state, TOK_COMMA);
12160 more = 1;
12161 }
12162 clobbers++;
12163 }
12164 }
12165 eat(state, TOK_RPAREN);
12166 eat(state, TOK_SEMI);
12167
12168
12169 info = xcmalloc(sizeof(*info), "asm_info");
12170 info->str = asm_str->u.blob;
12171 free_triple(state, asm_str);
12172
12173 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12174 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012175 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012176
12177 /* Find the register constraints */
12178 for(i = 0; i < out; i++) {
12179 struct triple *constraint;
12180 constraint = out_param[i].constraint;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012181 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012182 out_param[i].expr->type, constraint->u.blob);
12183 free_triple(state, constraint);
12184 }
12185 for(; i - out < clobbers; i++) {
12186 struct triple *constraint;
12187 constraint = clob_param[i - out].constraint;
12188 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12189 free_triple(state, constraint);
12190 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012191 for(i = 0; i < in; i++) {
12192 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012193 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012194 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012195 str = constraint->u.blob;
12196 if (digitp(str[0]) && str[1] == '\0') {
12197 struct reg_info cinfo;
12198 int val;
12199 val = digval(str[0]);
12200 cinfo.reg = info->tmpl.lhs[val].reg;
12201 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12202 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12203 if (cinfo.reg == REG_UNSET) {
12204 cinfo.reg = REG_VIRT0 + val;
12205 }
12206 if (cinfo.regcm == 0) {
12207 error(state, 0, "No registers for %d", val);
12208 }
12209 info->tmpl.lhs[val] = cinfo;
12210 info->tmpl.rhs[i] = cinfo;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012211
Eric Biederman8d9c1232003-06-17 08:42:17 +000012212 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012213 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012214 in_param[i].expr->type, str);
12215 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012216 free_triple(state, constraint);
12217 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012218
12219 /* Now build the helper expressions */
12220 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012221 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012222 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012223 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012224 for(i = 0; i < (out + clobbers); i++) {
12225 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012226 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012227 if (i < out) {
12228 type = out_param[i].expr->type;
12229 } else {
12230 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12231 if (size >= SIZEOF_LONG) {
12232 type = &ulong_type;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012233 }
Eric Biederman90089602004-05-28 14:11:54 +000012234 else if (size >= SIZEOF_INT) {
12235 type = &uint_type;
12236 }
12237 else if (size >= SIZEOF_SHORT) {
12238 type = &ushort_type;
12239 }
12240 else {
12241 type = &uchar_type;
12242 }
12243 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012244 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012245 piece->u.cval = i;
12246 LHS(def, i) = piece;
12247 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012248 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012249 /* And write the helpers to their destinations */
12250 for(i = 0; i < out; i++) {
12251 struct triple *piece;
12252 piece = LHS(def, i);
12253 flatten(state, first,
12254 write_expr(state, out_param[i].expr, piece));
12255 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012256}
12257
12258
12259static int isdecl(int tok)
12260{
12261 switch(tok) {
12262 case TOK_AUTO:
12263 case TOK_REGISTER:
12264 case TOK_STATIC:
12265 case TOK_EXTERN:
12266 case TOK_TYPEDEF:
12267 case TOK_CONST:
12268 case TOK_RESTRICT:
12269 case TOK_VOLATILE:
12270 case TOK_VOID:
12271 case TOK_CHAR:
12272 case TOK_SHORT:
12273 case TOK_INT:
12274 case TOK_LONG:
12275 case TOK_FLOAT:
12276 case TOK_DOUBLE:
12277 case TOK_SIGNED:
12278 case TOK_UNSIGNED:
12279 case TOK_STRUCT:
12280 case TOK_UNION:
12281 case TOK_ENUM:
12282 case TOK_TYPE_NAME: /* typedef name */
12283 return 1;
12284 default:
12285 return 0;
12286 }
12287}
12288
12289static void compound_statement(struct compile_state *state, struct triple *first)
12290{
12291 eat(state, TOK_LBRACE);
12292 start_scope(state);
12293
12294 /* statement-list opt */
12295 while (peek(state) != TOK_RBRACE) {
12296 statement(state, first);
12297 }
12298 end_scope(state);
12299 eat(state, TOK_RBRACE);
12300}
12301
12302static void statement(struct compile_state *state, struct triple *first)
12303{
12304 int tok;
12305 tok = peek(state);
12306 if (tok == TOK_LBRACE) {
12307 compound_statement(state, first);
12308 }
12309 else if (tok == TOK_IF) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012310 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012311 }
12312 else if (tok == TOK_FOR) {
12313 for_statement(state, first);
12314 }
12315 else if (tok == TOK_WHILE) {
12316 while_statement(state, first);
12317 }
12318 else if (tok == TOK_DO) {
12319 do_statement(state, first);
12320 }
12321 else if (tok == TOK_RETURN) {
12322 return_statement(state, first);
12323 }
12324 else if (tok == TOK_BREAK) {
12325 break_statement(state, first);
12326 }
12327 else if (tok == TOK_CONTINUE) {
12328 continue_statement(state, first);
12329 }
12330 else if (tok == TOK_GOTO) {
12331 goto_statement(state, first);
12332 }
12333 else if (tok == TOK_SWITCH) {
12334 switch_statement(state, first);
12335 }
12336 else if (tok == TOK_ASM) {
12337 asm_statement(state, first);
12338 }
12339 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012340 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012341 }
12342 else if (tok == TOK_CASE) {
12343 case_statement(state, first);
12344 }
12345 else if (tok == TOK_DEFAULT) {
12346 default_statement(state, first);
12347 }
12348 else if (isdecl(tok)) {
12349 /* This handles C99 intermixing of statements and decls */
12350 decl(state, first);
12351 }
12352 else {
12353 expr_statement(state, first);
12354 }
12355}
12356
12357static struct type *param_decl(struct compile_state *state)
12358{
12359 struct type *type;
12360 struct hash_entry *ident;
12361 /* Cheat so the declarator will know we are not global */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012362 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012363 ident = 0;
12364 type = decl_specifiers(state);
12365 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012366 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012367 end_scope(state);
12368 return type;
12369}
12370
12371static struct type *param_type_list(struct compile_state *state, struct type *type)
12372{
12373 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012374 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012375 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012376 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012377 while(peek(state) == TOK_COMMA) {
12378 eat(state, TOK_COMMA);
12379 if (peek(state) == TOK_DOTS) {
12380 eat(state, TOK_DOTS);
12381 error(state, 0, "variadic functions not supported");
12382 }
12383 else {
12384 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12385 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012386 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012387 }
12388 }
12389 return ftype;
12390}
12391
Eric Biedermanb138ac82003-04-22 18:44:01 +000012392static struct type *type_name(struct compile_state *state)
12393{
12394 struct type *type;
12395 type = specifier_qualifier_list(state);
12396 /* abstract-declarator (may consume no tokens) */
12397 type = declarator(state, type, 0, 0);
12398 return type;
12399}
12400
12401static struct type *direct_declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012402 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012403 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012404{
Eric Biederman41203d92004-11-08 09:31:09 +000012405 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012406 struct type *outer;
12407 int op;
12408 outer = 0;
12409 arrays_complete(state, type);
12410 switch(peek(state)) {
12411 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012412 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012413 if (!ident) {
12414 error(state, 0, "Unexpected identifier found");
12415 }
12416 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012417 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012418 break;
12419 case TOK_LPAREN:
12420 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012421 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012422 eat(state, TOK_RPAREN);
12423 break;
12424 default:
12425 if (need_ident) {
12426 error(state, 0, "Identifier expected");
12427 }
12428 break;
12429 }
12430 do {
12431 op = 1;
12432 arrays_complete(state, type);
12433 switch(peek(state)) {
12434 case TOK_LPAREN:
12435 eat(state, TOK_LPAREN);
12436 type = param_type_list(state, type);
12437 eat(state, TOK_RPAREN);
12438 break;
12439 case TOK_LBRACKET:
12440 {
12441 unsigned int qualifiers;
12442 struct triple *value;
12443 value = 0;
12444 eat(state, TOK_LBRACKET);
12445 if (peek(state) != TOK_RBRACKET) {
12446 value = constant_expr(state);
12447 integral(state, value);
12448 }
12449 eat(state, TOK_RBRACKET);
12450
12451 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12452 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12453 if (value) {
12454 type->elements = value->u.cval;
12455 free_triple(state, value);
12456 } else {
12457 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12458 op = 0;
12459 }
12460 }
12461 break;
12462 default:
12463 op = 0;
12464 break;
12465 }
12466 } while(op);
12467 if (outer) {
12468 struct type *inner;
12469 arrays_complete(state, type);
12470 FINISHME();
12471 for(inner = outer; inner->left; inner = inner->left)
12472 ;
12473 inner->left = type;
12474 type = outer;
12475 }
12476 return type;
12477}
12478
12479static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012480 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012481 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012482{
12483 while(peek(state) == TOK_STAR) {
12484 eat(state, TOK_STAR);
12485 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12486 }
Eric Biederman41203d92004-11-08 09:31:09 +000012487 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012488 return type;
12489}
12490
Eric Biedermanb138ac82003-04-22 18:44:01 +000012491static struct type *typedef_name(
12492 struct compile_state *state, unsigned int specifiers)
12493{
12494 struct hash_entry *ident;
12495 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012496 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012497 type = ident->sym_ident->type;
12498 specifiers |= type->type & QUAL_MASK;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012499 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012500 (type->type & (STOR_MASK | QUAL_MASK))) {
12501 type = clone_type(specifiers, type);
12502 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012503 return type;
12504}
12505
12506static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012507 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012508{
Eric Biederman83b991a2003-10-11 06:20:25 +000012509 struct hash_entry *ident;
12510 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012511 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012512 struct type *enum_type;
12513 enum_type = 0;
12514 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012515 eat(state, TOK_ENUM);
12516 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012517 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012518 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012519 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012520 base = 0;
12521 if (!ident || (peek(state) == TOK_LBRACE)) {
12522 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012523 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012524 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12525 enum_type->type_ident = ident;
12526 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012527 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012528 struct hash_entry *eident;
12529 struct triple *value;
12530 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012531 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012532 if (eident->sym_ident) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012533 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012534 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012535 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012536 eident->tok = TOK_ENUM_CONST;
12537 if (peek(state) == TOK_EQ) {
12538 struct triple *val;
12539 eat(state, TOK_EQ);
12540 val = constant_expr(state);
12541 integral(state, val);
12542 base = val->u.cval;
12543 }
12544 value = int_const(state, &int_type, base);
12545 symbol(state, eident, &eident->sym_ident, value, &int_type);
12546 entry = new_type(TYPE_LIST, 0, 0);
12547 entry->field_ident = eident;
12548 *next = entry;
12549 next = &entry->right;
12550 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012551 if (peek(state) == TOK_COMMA) {
12552 eat(state, TOK_COMMA);
12553 }
12554 } while(peek(state) != TOK_RBRACE);
12555 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012556 if (ident) {
12557 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12558 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012559 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012560 if (ident && ident->sym_tag &&
12561 ident->sym_tag->type &&
12562 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12563 enum_type = clone_type(spec, ident->sym_tag->type);
12564 }
12565 else if (ident && !enum_type) {
12566 error(state, 0, "enum %s undeclared", ident->name);
12567 }
12568 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012569}
12570
Eric Biedermanb138ac82003-04-22 18:44:01 +000012571static struct type *struct_declarator(
12572 struct compile_state *state, struct type *type, struct hash_entry **ident)
12573{
Eric Biederman90089602004-05-28 14:11:54 +000012574 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012575 type = declarator(state, type, ident, 1);
12576 }
Eric Biederman90089602004-05-28 14:11:54 +000012577 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012578 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012579 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012580 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012581 if (value->op != OP_INTCONST) {
12582 error(state, 0, "Invalid constant expression");
12583 }
12584 if (value->u.cval > size_of(state, type)) {
12585 error(state, 0, "bitfield larger than base type");
12586 }
12587 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12588 error(state, 0, "bitfield base not an integer type");
12589 }
12590 type = new_type(TYPE_BITFIELD, type, 0);
12591 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012592 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012593 return type;
12594}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012595
12596static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012597 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012598{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012599 struct type *struct_type;
12600 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012601 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012602 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012603 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012604 struct_type = 0;
12605 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012606 switch(peek(state)) {
12607 case TOK_STRUCT:
12608 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012609 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012610 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012611 break;
12612 case TOK_UNION:
12613 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012614 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012615 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012616 break;
12617 default:
12618 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012619 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012620 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012621 break;
12622 }
12623 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012624 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012625 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012626 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012627 if (!ident || (peek(state) == TOK_LBRACE)) {
12628 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012629 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012630 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012631 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012632 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012633 do {
12634 struct type *base_type;
12635 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012636 base_type = specifier_qualifier_list(state);
12637 do {
12638 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012639 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012640 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012641 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012642 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012643 if (peek(state) == TOK_COMMA) {
12644 done = 0;
12645 eat(state, TOK_COMMA);
12646 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012647 type = clone_type(0, type);
12648 type->field_ident = fident;
12649 if (*next) {
12650 *next = new_type(type_join, *next, type);
12651 next = &((*next)->right);
12652 } else {
12653 *next = type;
12654 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012655 } while(!done);
12656 eat(state, TOK_SEMI);
12657 } while(peek(state) != TOK_RBRACE);
12658 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012659 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012660 struct_type->type_ident = ident;
12661 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012662 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012663 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012664 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012665 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000012666 if (ident && ident->sym_tag &&
12667 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012668 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012669 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012670 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012671 else if (ident && !struct_type) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012672 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012673 (type_main == TYPE_STRUCT)?"struct" : "union",
12674 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012675 }
12676 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012677}
12678
12679static unsigned int storage_class_specifier_opt(struct compile_state *state)
12680{
12681 unsigned int specifiers;
12682 switch(peek(state)) {
12683 case TOK_AUTO:
12684 eat(state, TOK_AUTO);
12685 specifiers = STOR_AUTO;
12686 break;
12687 case TOK_REGISTER:
12688 eat(state, TOK_REGISTER);
12689 specifiers = STOR_REGISTER;
12690 break;
12691 case TOK_STATIC:
12692 eat(state, TOK_STATIC);
12693 specifiers = STOR_STATIC;
12694 break;
12695 case TOK_EXTERN:
12696 eat(state, TOK_EXTERN);
12697 specifiers = STOR_EXTERN;
12698 break;
12699 case TOK_TYPEDEF:
12700 eat(state, TOK_TYPEDEF);
12701 specifiers = STOR_TYPEDEF;
12702 break;
12703 default:
12704 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012705 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012706 }
12707 else {
12708 specifiers = STOR_AUTO;
12709 }
12710 }
12711 return specifiers;
12712}
12713
12714static unsigned int function_specifier_opt(struct compile_state *state)
12715{
12716 /* Ignore the inline keyword */
12717 unsigned int specifiers;
12718 specifiers = 0;
12719 switch(peek(state)) {
12720 case TOK_INLINE:
12721 eat(state, TOK_INLINE);
12722 specifiers = STOR_INLINE;
12723 }
12724 return specifiers;
12725}
12726
Eric Biederman90089602004-05-28 14:11:54 +000012727static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12728{
12729 int tok = peek(state);
12730 switch(tok) {
12731 case TOK_COMMA:
12732 case TOK_LPAREN:
12733 /* The empty attribute ignore it */
12734 break;
12735 case TOK_IDENT:
12736 case TOK_ENUM_CONST:
12737 case TOK_TYPE_NAME:
12738 {
12739 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012740 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012741
12742 if (ident == state->i_noinline) {
12743 if (attributes & ATTRIB_ALWAYS_INLINE) {
12744 error(state, 0, "both always_inline and noinline attribtes");
12745 }
12746 attributes |= ATTRIB_NOINLINE;
12747 }
12748 else if (ident == state->i_always_inline) {
12749 if (attributes & ATTRIB_NOINLINE) {
12750 error(state, 0, "both noinline and always_inline attribtes");
12751 }
12752 attributes |= ATTRIB_ALWAYS_INLINE;
12753 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012754 else if (ident == state->i_noreturn) {
12755 // attribute((noreturn)) does nothing (yet?)
12756 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012757 else if (ident == state->i_unused) {
12758 // attribute((unused)) does nothing (yet?)
12759 }
Stefan Reinauerec664bc2013-05-09 14:06:04 -070012760 else if (ident == state->i_packed) {
12761 // attribute((packed)) does nothing (yet?)
12762 }
Eric Biederman90089602004-05-28 14:11:54 +000012763 else {
12764 error(state, 0, "Unknown attribute:%s", ident->name);
12765 }
12766 break;
12767 }
12768 default:
12769 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12770 break;
12771 }
12772 return attributes;
12773}
12774
12775static unsigned int attribute_list(struct compile_state *state, unsigned type)
12776{
12777 type = attrib(state, type);
12778 while(peek(state) == TOK_COMMA) {
12779 eat(state, TOK_COMMA);
12780 type = attrib(state, type);
12781 }
12782 return type;
12783}
12784
12785static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12786{
12787 if (peek(state) == TOK_ATTRIBUTE) {
12788 eat(state, TOK_ATTRIBUTE);
12789 eat(state, TOK_LPAREN);
12790 eat(state, TOK_LPAREN);
12791 type = attribute_list(state, type);
12792 eat(state, TOK_RPAREN);
12793 eat(state, TOK_RPAREN);
12794 }
12795 return type;
12796}
12797
Eric Biedermanb138ac82003-04-22 18:44:01 +000012798static unsigned int type_qualifiers(struct compile_state *state)
12799{
12800 unsigned int specifiers;
12801 int done;
12802 done = 0;
12803 specifiers = QUAL_NONE;
12804 do {
12805 switch(peek(state)) {
12806 case TOK_CONST:
12807 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012808 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012809 break;
12810 case TOK_VOLATILE:
12811 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012812 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012813 break;
12814 case TOK_RESTRICT:
12815 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012816 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012817 break;
12818 default:
12819 done = 1;
12820 break;
12821 }
12822 } while(!done);
12823 return specifiers;
12824}
12825
12826static struct type *type_specifier(
12827 struct compile_state *state, unsigned int spec)
12828{
12829 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012830 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012831 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012832 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012833 case TOK_VOID:
12834 eat(state, TOK_VOID);
12835 type = new_type(TYPE_VOID | spec, 0, 0);
12836 break;
12837 case TOK_CHAR:
12838 eat(state, TOK_CHAR);
12839 type = new_type(TYPE_CHAR | spec, 0, 0);
12840 break;
12841 case TOK_SHORT:
12842 eat(state, TOK_SHORT);
12843 if (peek(state) == TOK_INT) {
12844 eat(state, TOK_INT);
12845 }
12846 type = new_type(TYPE_SHORT | spec, 0, 0);
12847 break;
12848 case TOK_INT:
12849 eat(state, TOK_INT);
12850 type = new_type(TYPE_INT | spec, 0, 0);
12851 break;
12852 case TOK_LONG:
12853 eat(state, TOK_LONG);
12854 switch(peek(state)) {
12855 case TOK_LONG:
12856 eat(state, TOK_LONG);
12857 error(state, 0, "long long not supported");
12858 break;
12859 case TOK_DOUBLE:
12860 eat(state, TOK_DOUBLE);
12861 error(state, 0, "long double not supported");
12862 break;
12863 case TOK_INT:
12864 eat(state, TOK_INT);
12865 type = new_type(TYPE_LONG | spec, 0, 0);
12866 break;
12867 default:
12868 type = new_type(TYPE_LONG | spec, 0, 0);
12869 break;
12870 }
12871 break;
12872 case TOK_FLOAT:
12873 eat(state, TOK_FLOAT);
12874 error(state, 0, "type float not supported");
12875 break;
12876 case TOK_DOUBLE:
12877 eat(state, TOK_DOUBLE);
12878 error(state, 0, "type double not supported");
12879 break;
12880 case TOK_SIGNED:
12881 eat(state, TOK_SIGNED);
12882 switch(peek(state)) {
12883 case TOK_LONG:
12884 eat(state, TOK_LONG);
12885 switch(peek(state)) {
12886 case TOK_LONG:
12887 eat(state, TOK_LONG);
12888 error(state, 0, "type long long not supported");
12889 break;
12890 case TOK_INT:
12891 eat(state, TOK_INT);
12892 type = new_type(TYPE_LONG | spec, 0, 0);
12893 break;
12894 default:
12895 type = new_type(TYPE_LONG | spec, 0, 0);
12896 break;
12897 }
12898 break;
12899 case TOK_INT:
12900 eat(state, TOK_INT);
12901 type = new_type(TYPE_INT | spec, 0, 0);
12902 break;
12903 case TOK_SHORT:
12904 eat(state, TOK_SHORT);
12905 type = new_type(TYPE_SHORT | spec, 0, 0);
12906 break;
12907 case TOK_CHAR:
12908 eat(state, TOK_CHAR);
12909 type = new_type(TYPE_CHAR | spec, 0, 0);
12910 break;
12911 default:
12912 type = new_type(TYPE_INT | spec, 0, 0);
12913 break;
12914 }
12915 break;
12916 case TOK_UNSIGNED:
12917 eat(state, TOK_UNSIGNED);
12918 switch(peek(state)) {
12919 case TOK_LONG:
12920 eat(state, TOK_LONG);
12921 switch(peek(state)) {
12922 case TOK_LONG:
12923 eat(state, TOK_LONG);
12924 error(state, 0, "unsigned long long not supported");
12925 break;
12926 case TOK_INT:
12927 eat(state, TOK_INT);
12928 type = new_type(TYPE_ULONG | spec, 0, 0);
12929 break;
12930 default:
12931 type = new_type(TYPE_ULONG | spec, 0, 0);
12932 break;
12933 }
12934 break;
12935 case TOK_INT:
12936 eat(state, TOK_INT);
12937 type = new_type(TYPE_UINT | spec, 0, 0);
12938 break;
12939 case TOK_SHORT:
12940 eat(state, TOK_SHORT);
12941 type = new_type(TYPE_USHORT | spec, 0, 0);
12942 break;
12943 case TOK_CHAR:
12944 eat(state, TOK_CHAR);
12945 type = new_type(TYPE_UCHAR | spec, 0, 0);
12946 break;
12947 default:
12948 type = new_type(TYPE_UINT | spec, 0, 0);
12949 break;
12950 }
12951 break;
12952 /* struct or union specifier */
12953 case TOK_STRUCT:
12954 case TOK_UNION:
12955 type = struct_or_union_specifier(state, spec);
12956 break;
12957 /* enum-spefifier */
12958 case TOK_ENUM:
12959 type = enum_specifier(state, spec);
12960 break;
12961 /* typedef name */
12962 case TOK_TYPE_NAME:
12963 type = typedef_name(state, spec);
12964 break;
12965 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012966 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012967 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012968 break;
12969 }
12970 return type;
12971}
12972
12973static int istype(int tok)
12974{
12975 switch(tok) {
12976 case TOK_CONST:
12977 case TOK_RESTRICT:
12978 case TOK_VOLATILE:
12979 case TOK_VOID:
12980 case TOK_CHAR:
12981 case TOK_SHORT:
12982 case TOK_INT:
12983 case TOK_LONG:
12984 case TOK_FLOAT:
12985 case TOK_DOUBLE:
12986 case TOK_SIGNED:
12987 case TOK_UNSIGNED:
12988 case TOK_STRUCT:
12989 case TOK_UNION:
12990 case TOK_ENUM:
12991 case TOK_TYPE_NAME:
12992 return 1;
12993 default:
12994 return 0;
12995 }
12996}
12997
12998
12999static struct type *specifier_qualifier_list(struct compile_state *state)
13000{
13001 struct type *type;
13002 unsigned int specifiers = 0;
13003
13004 /* type qualifiers */
13005 specifiers |= type_qualifiers(state);
13006
13007 /* type specifier */
13008 type = type_specifier(state, specifiers);
13009
13010 return type;
13011}
13012
Stefan Reinauer50542a82007-10-24 11:14:14 +000013013#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013014static int isdecl_specifier(int tok)
13015{
13016 switch(tok) {
13017 /* storage class specifier */
13018 case TOK_AUTO:
13019 case TOK_REGISTER:
13020 case TOK_STATIC:
13021 case TOK_EXTERN:
13022 case TOK_TYPEDEF:
13023 /* type qualifier */
13024 case TOK_CONST:
13025 case TOK_RESTRICT:
13026 case TOK_VOLATILE:
13027 /* type specifiers */
13028 case TOK_VOID:
13029 case TOK_CHAR:
13030 case TOK_SHORT:
13031 case TOK_INT:
13032 case TOK_LONG:
13033 case TOK_FLOAT:
13034 case TOK_DOUBLE:
13035 case TOK_SIGNED:
13036 case TOK_UNSIGNED:
13037 /* struct or union specifier */
13038 case TOK_STRUCT:
13039 case TOK_UNION:
13040 /* enum-spefifier */
13041 case TOK_ENUM:
13042 /* typedef name */
13043 case TOK_TYPE_NAME:
13044 /* function specifiers */
13045 case TOK_INLINE:
13046 return 1;
13047 default:
13048 return 0;
13049 }
13050}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013051#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013052
13053static struct type *decl_specifiers(struct compile_state *state)
13054{
13055 struct type *type;
13056 unsigned int specifiers;
13057 /* I am overly restrictive in the arragement of specifiers supported.
13058 * C is overly flexible in this department it makes interpreting
13059 * the parse tree difficult.
13060 */
13061 specifiers = 0;
13062
13063 /* storage class specifier */
13064 specifiers |= storage_class_specifier_opt(state);
13065
13066 /* function-specifier */
13067 specifiers |= function_specifier_opt(state);
13068
Eric Biederman90089602004-05-28 14:11:54 +000013069 /* attributes */
13070 specifiers |= attributes_opt(state, 0);
13071
Eric Biedermanb138ac82003-04-22 18:44:01 +000013072 /* type qualifier */
13073 specifiers |= type_qualifiers(state);
13074
13075 /* type specifier */
13076 type = type_specifier(state, specifiers);
13077 return type;
13078}
13079
Eric Biederman00443072003-06-24 12:34:45 +000013080struct field_info {
13081 struct type *type;
13082 size_t offset;
13083};
13084
13085static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013086{
13087 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013088 struct field_info info;
13089 info.offset = ~0U;
13090 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013091 do {
13092 switch(peek(state)) {
13093 case TOK_LBRACKET:
13094 {
13095 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013096 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13097 error(state, 0, "Array designator not in array initializer");
13098 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013099 eat(state, TOK_LBRACKET);
13100 value = constant_expr(state);
13101 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013102
13103 info.type = type->left;
13104 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013105 break;
13106 }
13107 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013108 {
13109 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013110 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13111 ((type->type & TYPE_MASK) != TYPE_UNION))
13112 {
Eric Biederman00443072003-06-24 12:34:45 +000013113 error(state, 0, "Struct designator not in struct initializer");
13114 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013116 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013117 info.offset = field_offset(state, type, field);
13118 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013119 break;
Eric Biederman00443072003-06-24 12:34:45 +000013120 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013121 default:
13122 error(state, 0, "Invalid designator");
13123 }
13124 tok = peek(state);
13125 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13126 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013127 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013128}
13129
13130static struct triple *initializer(
13131 struct compile_state *state, struct type *type)
13132{
13133 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013134#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013135#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013136#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013137 if (peek(state) != TOK_LBRACE) {
13138 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013139 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13140 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13141 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13142 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13143 (equiv_types(type->left, result->type->left))) {
13144 type->elements = result->type->elements;
13145 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013146 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013147 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13148 (type->type & TYPE_MASK) != TYPE_ARRAY)
13149 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013150 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013151 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013152 if (!is_init_compatible(state, type, result->type)) {
13153 error(state, 0, "Incompatible types in initializer");
13154 }
13155 if (!equiv_types(type, result->type)) {
13156 result = mk_cast_expr(state, type, result);
13157 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013158 }
13159 else {
13160 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013161 size_t max_offset;
13162 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013163 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013164 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13165 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13166 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013167 }
Eric Biederman00443072003-06-24 12:34:45 +000013168 info.offset = 0;
13169 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013170 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13171 info.type = next_field(state, type, 0);
13172 }
Eric Biederman00443072003-06-24 12:34:45 +000013173 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13174 max_offset = 0;
13175 } else {
13176 max_offset = size_of(state, type);
13177 }
Eric Biederman90089602004-05-28 14:11:54 +000013178 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013179 eat(state, TOK_LBRACE);
13180 do {
13181 struct triple *value;
13182 struct type *value_type;
13183 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013184 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013185 int tok;
13186 comma = 0;
13187 tok = peek(state);
13188 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013189 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013190 }
Eric Biederman00443072003-06-24 12:34:45 +000013191 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13192 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013193 error(state, 0, "element beyond bounds");
13194 }
Eric Biederman00443072003-06-24 12:34:45 +000013195 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013196 value = eval_const_expr(state, initializer(state, value_type));
13197 value_size = size_of(state, value_type);
13198 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013199 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13200 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013201 void *old_buf;
13202 size_t old_size;
13203 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013204 old_size = max_offset;
13205 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013206 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13207 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013208 xfree(old_buf);
13209 }
Eric Biederman90089602004-05-28 14:11:54 +000013210 dest = ((char *)buf) + bits_to_bytes(info.offset);
13211#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013212 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013213 dest - buf,
13214 bits_to_bytes(max_offset),
13215 bits_to_bytes(value_size),
13216 value->op);
13217#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013218 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013219 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013220 }
Eric Biederman90089602004-05-28 14:11:54 +000013221 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13222#if DEBUG_INITIALIZER
13223 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13224#endif
Eric Biederman00443072003-06-24 12:34:45 +000013225 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013226 }
Eric Biederman90089602004-05-28 14:11:54 +000013227 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013228 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013229 }
Eric Biederman90089602004-05-28 14:11:54 +000013230 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013231 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013232 }
13233 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013234 internal_error(state, 0, "unhandled constant initializer");
13235 }
Eric Biederman00443072003-06-24 12:34:45 +000013236 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013237 if (peek(state) == TOK_COMMA) {
13238 eat(state, TOK_COMMA);
13239 comma = 1;
13240 }
Eric Biederman00443072003-06-24 12:34:45 +000013241 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013242 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13243 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013244 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013245 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013246 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013247 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013248 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13249 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13250 type->elements = max_offset / size_of(state, type->left);
13251 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013252 eat(state, TOK_RBRACE);
13253 result = triple(state, OP_BLOBCONST, type, 0, 0);
13254 result->u.blob = buf;
13255 }
13256 return result;
13257}
13258
Eric Biederman90089602004-05-28 14:11:54 +000013259static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013260{
13261 /* Make a second pass and finish anything outstanding
13262 * with respect to branches. The only outstanding item
13263 * is to see if there are goto to labels that have not
13264 * been defined and to error about them.
13265 */
13266 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013267 struct triple *ins;
13268 /* Also error on branches that do not use their targets */
13269 ins = first;
13270 do {
13271 if (!triple_is_ret(state, ins)) {
13272 struct triple **expr ;
13273 struct triple_set *set;
13274 expr = triple_targ(state, ins, 0);
13275 for(; expr; expr = triple_targ(state, ins, expr)) {
13276 struct triple *targ;
13277 targ = *expr;
13278 for(set = targ?targ->use:0; set; set = set->next) {
13279 if (set->member == ins) {
13280 break;
13281 }
13282 }
13283 if (!set) {
13284 internal_error(state, ins, "targ not used");
13285 }
13286 }
13287 }
13288 ins = ins->next;
13289 } while(ins != first);
13290 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013291 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13292 struct hash_entry *entry;
13293 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13294 struct triple *ins;
13295 if (!entry->sym_label) {
13296 continue;
13297 }
13298 ins = entry->sym_label->def;
13299 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13300 error(state, ins, "label `%s' used but not defined",
13301 entry->name);
13302 }
13303 }
13304 }
13305}
13306
Eric Biedermanb138ac82003-04-22 18:44:01 +000013307static struct triple *function_definition(
13308 struct compile_state *state, struct type *type)
13309{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013310 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013311 struct triple *fname;
13312 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013313 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013314 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013315 int i;
13316 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13317 error(state, 0, "Invalid function header");
13318 }
13319
13320 /* Verify the function type */
13321 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13322 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013323 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013324 error(state, 0, "Invalid function parameters");
13325 }
13326 param = type->right;
13327 i = 0;
13328 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13329 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013330 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013331 error(state, 0, "No identifier for parameter %d\n", i);
13332 }
13333 param = param->right;
13334 }
13335 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013336 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013337 error(state, 0, "No identifier for paramter %d\n", i);
13338 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013339
Eric Biedermanb138ac82003-04-22 18:44:01 +000013340 /* Get a list of statements for this function. */
13341 def = triple(state, OP_LIST, type, 0, 0);
13342
13343 /* Start a new scope for the passed parameters */
13344 start_scope(state);
13345
13346 /* Put a label at the very start of a function */
13347 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013348 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013349
13350 /* Put a label at the very end of a function */
13351 end = label(state);
13352 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013353 /* Remember where return goes */
13354 ident = state->i_return;
13355 symbol(state, ident, &ident->sym_ident, end, end->type);
13356
Eric Biederman90089602004-05-28 14:11:54 +000013357 /* Get the initial closure type */
13358 ctype = new_type(TYPE_JOIN, &void_type, 0);
13359 ctype->elements = 1;
13360
13361 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013362 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013363 /* Remove all type qualifiers from the return type */
13364 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13365 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013366 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013367
Eric Biederman5ade04a2003-10-22 04:03:46 +000013368 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013369 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013370
13371 /* Add in the return instruction */
13372 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13373 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013374
13375 /* Walk through the parameters and create symbol table entries
13376 * for them.
13377 */
13378 param = type->right;
13379 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013380 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013381 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013382 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013383 flatten(state, end, tmp);
13384 param = param->right;
13385 }
13386 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13387 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013388 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013389 tmp = variable(state, param);
13390 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13391 flatten(state, end, tmp);
13392 }
Eric Biederman90089602004-05-28 14:11:54 +000013393
13394 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013395 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013396 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13397 fname_type->type |= QUAL_CONST | STOR_STATIC;
13398 fname_type->elements = strlen(state->function) + 1;
13399
13400 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13401 fname->u.blob = (void *)state->function;
13402 fname = flatten(state, end, fname);
13403
13404 ident = state->i___func__;
13405 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013406
13407 /* Remember which function I am compiling.
13408 * Also assume the last defined function is the main function.
13409 */
13410 state->main_function = def;
13411
13412 /* Now get the actual function definition */
13413 compound_statement(state, end);
13414
Eric Biederman153ea352003-06-20 14:43:20 +000013415 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013416 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013417
Eric Biedermanb138ac82003-04-22 18:44:01 +000013418 /* Remove the parameter scope */
13419 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013420
Eric Biederman5ade04a2003-10-22 04:03:46 +000013421
13422 /* Remember I have defined a function */
13423 if (!state->functions) {
13424 state->functions = def;
13425 } else {
13426 insert_triple(state, state->functions, def);
13427 }
13428 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013429 FILE *fp = state->dbgout;
13430 fprintf(fp, "\n");
13431 loc(fp, state, 0);
13432 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13433 display_func(state, fp, def);
13434 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013435 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013436
13437 return def;
13438}
13439
Stefan Reinauer14e22772010-04-27 06:56:47 +000013440static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013441 struct type *type, struct hash_entry *ident)
13442{
13443 struct triple *def;
13444 def = 0;
13445 /* Clean up the storage types used */
13446 switch (type->type & STOR_MASK) {
13447 case STOR_AUTO:
13448 case STOR_STATIC:
13449 /* These are the good types I am aiming for */
13450 break;
13451 case STOR_REGISTER:
13452 type->type &= ~STOR_MASK;
13453 type->type |= STOR_AUTO;
13454 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013455 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013456 case STOR_EXTERN:
13457 type->type &= ~STOR_MASK;
13458 type->type |= STOR_STATIC;
13459 break;
13460 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013461 if (!ident) {
13462 error(state, 0, "typedef without name");
13463 }
13464 symbol(state, ident, &ident->sym_ident, 0, type);
13465 ident->tok = TOK_TYPE_NAME;
13466 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013467 break;
13468 default:
13469 internal_error(state, 0, "Undefined storage class");
13470 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013471 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
Patrick Georgi11a7db32013-02-16 20:16:34 +010013472 // ignore function prototypes
13473 return def;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013474 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013475 if (ident &&
13476 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13477 ((type->type & STOR_MASK) != STOR_STATIC))
13478 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013479 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013480 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013481 ((type->type & QUAL_CONST) == 0)) {
13482 error(state, 0, "non const static variables not supported");
13483 }
13484 if (ident) {
13485 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013486 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013487 }
13488 return def;
13489}
13490
13491static void decl(struct compile_state *state, struct triple *first)
13492{
13493 struct type *base_type, *type;
13494 struct hash_entry *ident;
13495 struct triple *def;
13496 int global;
13497 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13498 base_type = decl_specifiers(state);
13499 ident = 0;
13500 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013501 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013502 if (global && ident && (peek(state) == TOK_LBRACE)) {
13503 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013504 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013505 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013506 def = function_definition(state, type);
13507 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013508 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013509 }
13510 else {
13511 int done;
13512 flatten(state, first, do_decl(state, type, ident));
13513 /* type or variable definition */
13514 do {
13515 done = 1;
13516 if (peek(state) == TOK_EQ) {
13517 if (!ident) {
13518 error(state, 0, "cannot assign to a type");
13519 }
13520 eat(state, TOK_EQ);
13521 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013522 init_expr(state,
13523 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013524 initializer(state, type)));
13525 }
13526 arrays_complete(state, type);
13527 if (peek(state) == TOK_COMMA) {
13528 eat(state, TOK_COMMA);
13529 ident = 0;
13530 type = declarator(state, base_type, &ident, 0);
13531 flatten(state, first, do_decl(state, type, ident));
13532 done = 0;
13533 }
13534 } while(!done);
13535 eat(state, TOK_SEMI);
13536 }
13537}
13538
13539static void decls(struct compile_state *state)
13540{
13541 struct triple *list;
13542 int tok;
13543 list = label(state);
13544 while(1) {
13545 tok = peek(state);
13546 if (tok == TOK_EOF) {
13547 return;
13548 }
13549 if (tok == TOK_SPACE) {
13550 eat(state, TOK_SPACE);
13551 }
13552 decl(state, list);
13553 if (list->next != list) {
13554 error(state, 0, "global variables not supported");
13555 }
13556 }
13557}
13558
Stefan Reinauer14e22772010-04-27 06:56:47 +000013559/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013560 * Function inlining
13561 */
Eric Biederman90089602004-05-28 14:11:54 +000013562struct triple_reg_set {
13563 struct triple_reg_set *next;
13564 struct triple *member;
13565 struct triple *new;
13566};
13567struct reg_block {
13568 struct block *block;
13569 struct triple_reg_set *in;
13570 struct triple_reg_set *out;
13571 int vertex;
13572};
13573static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13574static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13575static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13576static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13577static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13578 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13579 void *arg);
13580static void print_block(
13581 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013582static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013583 struct triple *member, struct triple *new_member);
13584static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13585static struct reg_block *compute_variable_lifetimes(
13586 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013587static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013588 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013589#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013590static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013591 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013592#endif
Eric Biederman90089602004-05-28 14:11:54 +000013593
Eric Biederman5ade04a2003-10-22 04:03:46 +000013594
13595static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013596 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013597 struct triple *targ, struct triple *ret)
13598{
13599 struct triple *call;
13600
13601 if (!retvar || !is_lvalue(state, retvar)) {
13602 internal_error(state, 0, "writing to a non lvalue?");
13603 }
13604 write_compatible(state, retvar->type, &void_ptr_type);
13605
13606 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13607 TARG(call, 0) = targ;
13608 MISC(call, 0) = ret;
13609 if (!targ || (targ->op != OP_LABEL)) {
13610 internal_error(state, 0, "call not to a label");
13611 }
13612 if (!ret || (ret->op != OP_RET)) {
13613 internal_error(state, 0, "call not matched with return");
13614 }
13615 return call;
13616}
13617
Eric Biederman5ade04a2003-10-22 04:03:46 +000013618static void walk_functions(struct compile_state *state,
13619 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13620 void *arg)
13621{
13622 struct triple *func, *first;
13623 func = first = state->functions;
13624 do {
13625 cb(state, func, arg);
13626 func = func->next;
13627 } while(func != first);
13628}
13629
Eric Biederman90089602004-05-28 14:11:54 +000013630static void reverse_walk_functions(struct compile_state *state,
13631 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13632 void *arg)
13633{
13634 struct triple *func, *first;
13635 func = first = state->functions;
13636 do {
13637 func = func->prev;
13638 cb(state, func, arg);
13639 } while(func != first);
13640}
13641
13642
13643static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13644{
13645 struct triple *ptr, *first;
13646 if (func->u.cval == 0) {
13647 return;
13648 }
13649 ptr = first = RHS(func, 0);
13650 do {
13651 if (ptr->op == OP_FCALL) {
13652 struct triple *called_func;
13653 called_func = MISC(ptr, 0);
13654 /* Mark the called function as used */
13655 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13656 called_func->u.cval++;
13657 }
13658 /* Remove the called function from the list */
13659 called_func->prev->next = called_func->next;
13660 called_func->next->prev = called_func->prev;
13661
13662 /* Place the called function before me on the list */
13663 called_func->next = func;
13664 called_func->prev = func->prev;
13665 called_func->prev->next = called_func;
13666 called_func->next->prev = called_func;
13667 }
13668 ptr = ptr->next;
13669 } while(ptr != first);
13670 func->id |= TRIPLE_FLAG_FLATTENED;
13671}
13672
13673static void mark_live_functions(struct compile_state *state)
13674{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013675 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013676 * the list of functions.
13677 */
13678 if ((state->main_function->next != state->functions) ||
13679 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013680 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013681 "state->main_function is not at the end of the function list ");
13682 }
13683 state->main_function->u.cval = 1;
13684 reverse_walk_functions(state, mark_live, 0);
13685}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013686
Stefan Reinauer14e22772010-04-27 06:56:47 +000013687static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013688 struct triple *func, struct triple *ins)
13689{
13690 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13691#if 0
13692 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013693 FILE *fp = state->errout;
13694 fprintf(fp, "global: ");
13695 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013696 }
13697#endif
13698 return local;
13699}
13700
Stefan Reinauer14e22772010-04-27 06:56:47 +000013701struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013702 struct occurance *base_occurance)
13703{
13704 struct triple *nfunc;
13705 struct triple *nfirst, *ofirst;
13706 struct triple *new, *old;
13707
13708 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013709 FILE *fp = state->dbgout;
13710 fprintf(fp, "\n");
13711 loc(fp, state, 0);
13712 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13713 display_func(state, fp, ofunc);
13714 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013715 }
13716
13717 /* Make a new copy of the old function */
13718 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13719 nfirst = 0;
13720 ofirst = old = RHS(ofunc, 0);
13721 do {
13722 struct triple *new;
13723 struct occurance *occurance;
13724 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013725 old_lhs = old->lhs;
13726 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013727 occurance = inline_occurance(state, base_occurance, old->occurance);
13728 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13729 MISC(old, 0)->u.cval += 1;
13730 }
13731 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13732 occurance);
13733 if (!triple_stores_block(state, new)) {
13734 memcpy(&new->u, &old->u, sizeof(new->u));
13735 }
13736 if (!nfirst) {
13737 RHS(nfunc, 0) = nfirst = new;
13738 }
13739 else {
13740 insert_triple(state, nfirst, new);
13741 }
13742 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013743 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013744
Eric Biederman5ade04a2003-10-22 04:03:46 +000013745 /* During the copy remember new as user of old */
13746 use_triple(old, new);
13747
Eric Biederman5ade04a2003-10-22 04:03:46 +000013748 /* Remember which instructions are local */
13749 old->id |= TRIPLE_FLAG_LOCAL;
13750 old = old->next;
13751 } while(old != ofirst);
13752
13753 /* Make a second pass to fix up any unresolved references */
13754 old = ofirst;
13755 new = nfirst;
13756 do {
13757 struct triple **oexpr, **nexpr;
13758 int count, i;
13759 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013760 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013761 for(i = 0; i < count; i++) {
13762 oexpr = &old->param[i];
13763 nexpr = &new->param[i];
13764 if (*oexpr && !*nexpr) {
13765 if (!local_triple(state, ofunc, *oexpr)) {
13766 *nexpr = *oexpr;
13767 }
13768 else if ((*oexpr)->use) {
13769 *nexpr = (*oexpr)->use->member;
13770 }
13771 if (*nexpr == old) {
13772 internal_error(state, 0, "new == old?");
13773 }
13774 use_triple(*nexpr, new);
13775 }
13776 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013777 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013778 }
13779 }
13780 old = old->next;
13781 new = new->next;
13782 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013783
Eric Biederman5ade04a2003-10-22 04:03:46 +000013784 /* Make a third pass to cleanup the extra useses */
13785 old = ofirst;
13786 new = nfirst;
13787 do {
13788 unuse_triple(old, new);
13789 /* Forget which instructions are local */
13790 old->id &= ~TRIPLE_FLAG_LOCAL;
13791 old = old->next;
13792 new = new->next;
13793 } while ((old != ofirst) && (new != nfirst));
13794 return nfunc;
13795}
13796
Eric Biederman90089602004-05-28 14:11:54 +000013797static void expand_inline_call(
13798 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013799{
13800 /* Inline the function call */
13801 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013802 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013803 struct triple *end, *nend;
13804 int pvals, i;
13805
13806 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013807 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013808 if (ofunc->op != OP_LIST) {
13809 internal_error(state, 0, "improper function");
13810 }
Eric Biederman90089602004-05-28 14:11:54 +000013811 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013812 /* Prepend the parameter reading into the new function list */
13813 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013814 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013815 for(i = 0; i < pvals; i++) {
13816 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013817 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013818 atype = ptype;
13819 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13820 atype = ptype->left;
13821 }
Eric Biederman90089602004-05-28 14:11:54 +000013822 param = farg(state, nfunc, i);
13823 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13824 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013825 }
Eric Biederman90089602004-05-28 14:11:54 +000013826 arg = RHS(fcall, i);
13827 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013828 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013829 }
13830 result = 0;
13831 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013832 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013833 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013834 }
13835 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013836 FILE *fp = state->dbgout;
13837 fprintf(fp, "\n");
13838 loc(fp, state, 0);
13839 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13840 display_func(state, fp, nfunc);
13841 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013842 }
13843
Stefan Reinauer14e22772010-04-27 06:56:47 +000013844 /*
13845 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013846 */
13847 /* Remove the read of the return address */
13848 ins = RHS(nfunc, 0)->prev->prev;
13849 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13850 internal_error(state, ins, "Not return addres read?");
13851 }
13852 release_triple(state, ins);
13853 /* Remove the return instruction */
13854 ins = RHS(nfunc, 0)->prev;
13855 if (ins->op != OP_RET) {
13856 internal_error(state, ins, "Not return?");
13857 }
13858 release_triple(state, ins);
13859 /* Remove the retaddres variable */
13860 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013861 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013862 (retvar->op != OP_ADECL) ||
13863 (retvar->next->op != OP_PIECE) ||
13864 (MISC(retvar->next, 0) != retvar)) {
13865 internal_error(state, retvar, "Not the return address?");
13866 }
13867 release_triple(state, retvar->next);
13868 release_triple(state, retvar);
13869
13870 /* Remove the label at the start of the function */
13871 ins = RHS(nfunc, 0);
13872 if (ins->op != OP_LABEL) {
13873 internal_error(state, ins, "Not label?");
13874 }
13875 nfirst = ins->next;
13876 free_triple(state, ins);
13877 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013878 RHS(nfunc, 0) = 0;
13879 free_triple(state, nfunc);
13880
13881 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013882 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013883 nend = nfirst->prev;
13884 end->next = nfirst;
13885 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013886 nend->next = fcall;
13887 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013888
Eric Biederman90089602004-05-28 14:11:54 +000013889 /* Now the result reading code */
13890 if (result) {
13891 result = flatten(state, fcall, result);
13892 propogate_use(state, fcall, result);
13893 }
13894
13895 /* Release the original fcall instruction */
13896 release_triple(state, fcall);
13897
13898 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013899}
13900
Eric Biederman90089602004-05-28 14:11:54 +000013901/*
13902 *
13903 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013904 *
Eric Biederman90089602004-05-28 14:11:54 +000013905 * result
13906 * |
13907 * +----------+------------+
13908 * | |
13909 * union of closures result_type
13910 * |
13911 * +------------------+---------------+
13912 * | |
13913 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013914 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013915 * +----+--+-+--------+-----+ +----+----+---+-----+
13916 * | | | | | | | | |
13917 * var1 var2 var3 ... varN result var1 var2 ... varN result
13918 * |
13919 * +--------+---------+
13920 * | |
13921 * union of closures result_type
13922 * |
13923 * +-----+-------------------+
13924 * | |
13925 * closure1 ... closureN
13926 * | |
13927 * +-----+---+----+----+ +----+---+----+-----+
13928 * | | | | | | | |
13929 * var1 var2 ... varN result var1 var2 ... varN result
13930 */
13931
Stefan Reinauer14e22772010-04-27 06:56:47 +000013932static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013933 struct triple *func, struct type *closure_type)
13934{
13935 struct type *type, *ctype, **next;
13936 struct triple *var, *new_var;
13937 int i;
13938
13939#if 0
13940 FILE *fp = state->errout;
13941 fprintf(fp, "original_type: ");
13942 name_of(fp, fresult(state, func)->type);
13943 fprintf(fp, "\n");
13944#endif
13945 /* find the original type */
13946 var = fresult(state, func);
13947 type = var->type;
13948 if (type->elements != 2) {
13949 internal_error(state, var, "bad return type");
13950 }
13951
13952 /* Find the complete closure type and update it */
13953 ctype = type->left->left;
13954 next = &ctype->left;
13955 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13956 next = &(*next)->right;
13957 }
13958 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13959 ctype->elements += 1;
13960
13961#if 0
13962 fprintf(fp, "new_type: ");
13963 name_of(fp, type);
13964 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013965 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013966 ctype, ctype->elements, reg_size_of(state, ctype));
13967 name_of(fp, ctype);
13968 fprintf(fp, "\n");
13969#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013970
Eric Biederman90089602004-05-28 14:11:54 +000013971 /* Regenerate the variable with the new type definition */
13972 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13973 new_var->id |= TRIPLE_FLAG_FLATTENED;
13974 for(i = 0; i < new_var->lhs; i++) {
13975 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13976 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013977
Eric Biederman90089602004-05-28 14:11:54 +000013978 /* Point everyone at the new variable */
13979 propogate_use(state, var, new_var);
13980
13981 /* Release the original variable */
13982 for(i = 0; i < var->lhs; i++) {
13983 release_triple(state, LHS(var, i));
13984 }
13985 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013986
Eric Biederman90089602004-05-28 14:11:54 +000013987 /* Return the index of the added closure type */
13988 return ctype->elements - 1;
13989}
13990
13991static struct triple *closure_expr(struct compile_state *state,
13992 struct triple *func, int closure_idx, int var_idx)
13993{
13994 return deref_index(state,
13995 deref_index(state,
13996 deref_index(state, fresult(state, func), 0),
13997 closure_idx),
13998 var_idx);
13999}
14000
14001
14002static void insert_triple_set(
14003 struct triple_reg_set **head, struct triple *member)
14004{
14005 struct triple_reg_set *new;
14006 new = xcmalloc(sizeof(*new), "triple_set");
14007 new->member = member;
14008 new->new = 0;
14009 new->next = *head;
14010 *head = new;
14011}
14012
14013static int ordered_triple_set(
14014 struct triple_reg_set **head, struct triple *member)
14015{
14016 struct triple_reg_set **ptr;
14017 if (!member)
14018 return 0;
14019 ptr = head;
14020 while(*ptr) {
14021 if (member == (*ptr)->member) {
14022 return 0;
14023 }
14024 /* keep the list ordered */
14025 if (member->id < (*ptr)->member->id) {
14026 break;
14027 }
14028 ptr = &(*ptr)->next;
14029 }
14030 insert_triple_set(ptr, member);
14031 return 1;
14032}
14033
14034
14035static void free_closure_variables(struct compile_state *state,
14036 struct triple_reg_set **enclose)
14037{
14038 struct triple_reg_set *entry, *next;
14039 for(entry = *enclose; entry; entry = next) {
14040 next = entry->next;
14041 do_triple_unset(enclose, entry->member);
14042 }
14043}
14044
14045static int lookup_closure_index(struct compile_state *state,
14046 struct triple *me, struct triple *val)
14047{
14048 struct triple *first, *ins, *next;
14049 first = RHS(me, 0);
14050 ins = next = first;
14051 do {
14052 struct triple *result;
14053 struct triple *index0, *index1, *index2, *read, *write;
14054 ins = next;
14055 next = ins->next;
14056 if (ins->op != OP_CALL) {
14057 continue;
14058 }
14059 /* I am at a previous call point examine it closely */
14060 if (ins->next->op != OP_LABEL) {
14061 internal_error(state, ins, "call not followed by label");
14062 }
14063 /* Does this call does not enclose any variables? */
14064 if ((ins->next->next->op != OP_INDEX) ||
14065 (ins->next->next->u.cval != 0) ||
14066 (result = MISC(ins->next->next, 0)) ||
14067 (result->id & TRIPLE_FLAG_LOCAL)) {
14068 continue;
14069 }
14070 index0 = ins->next->next;
14071 /* The pattern is:
14072 * 0 index result < 0 >
14073 * 1 index 0 < ? >
14074 * 2 index 1 < ? >
14075 * 3 read 2
14076 * 4 write 3 var
14077 */
14078 for(index0 = ins->next->next;
14079 (index0->op == OP_INDEX) &&
14080 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014081 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014082 index0 = write->next)
14083 {
14084 index1 = index0->next;
14085 index2 = index1->next;
14086 read = index2->next;
14087 write = read->next;
14088 if ((index0->op != OP_INDEX) ||
14089 (index1->op != OP_INDEX) ||
14090 (index2->op != OP_INDEX) ||
14091 (read->op != OP_READ) ||
14092 (write->op != OP_WRITE) ||
14093 (MISC(index1, 0) != index0) ||
14094 (MISC(index2, 0) != index1) ||
14095 (RHS(read, 0) != index2) ||
14096 (RHS(write, 0) != read)) {
14097 internal_error(state, index0, "bad var read");
14098 }
14099 if (MISC(write, 0) == val) {
14100 return index2->u.cval;
14101 }
14102 }
14103 } while(next != first);
14104 return -1;
14105}
14106
14107static inline int enclose_triple(struct triple *ins)
14108{
14109 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14110}
14111
14112static void compute_closure_variables(struct compile_state *state,
14113 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14114{
14115 struct triple_reg_set *set, *vars, **last_var;
14116 struct basic_blocks bb;
14117 struct reg_block *rb;
14118 struct block *block;
14119 struct triple *old_result, *first, *ins;
14120 size_t count, idx;
14121 unsigned long used_indicies;
14122 int i, max_index;
14123#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14124#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014125 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014126 unsigned id;
14127 int index;
14128 } *info;
14129
Stefan Reinauer14e22772010-04-27 06:56:47 +000014130
Eric Biederman90089602004-05-28 14:11:54 +000014131 /* Find the basic blocks of this function */
14132 bb.func = me;
14133 bb.first = RHS(me, 0);
14134 old_result = 0;
14135 if (!triple_is_ret(state, bb.first->prev)) {
14136 bb.func = 0;
14137 } else {
14138 old_result = fresult(state, me);
14139 }
14140 analyze_basic_blocks(state, &bb);
14141
14142 /* Find which variables are currently alive in a given block */
14143 rb = compute_variable_lifetimes(state, &bb);
14144
14145 /* Find the variables that are currently alive */
14146 block = block_of_triple(state, fcall);
14147 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14148 internal_error(state, fcall, "No reg block? block: %p", block);
14149 }
14150
14151#if DEBUG_EXPLICIT_CLOSURES
14152 print_live_variables(state, &bb, rb, state->dbgout);
14153 fflush(state->dbgout);
14154#endif
14155
14156 /* Count the number of triples in the function */
14157 first = RHS(me, 0);
14158 ins = first;
14159 count = 0;
14160 do {
14161 count++;
14162 ins = ins->next;
14163 } while(ins != first);
14164
14165 /* Allocate some memory to temorary hold the id info */
14166 info = xcmalloc(sizeof(*info) * (count +1), "info");
14167
14168 /* Mark the local function */
14169 first = RHS(me, 0);
14170 ins = first;
14171 idx = 1;
14172 do {
14173 info[idx].id = ins->id;
14174 ins->id = TRIPLE_FLAG_LOCAL | idx;
14175 idx++;
14176 ins = ins->next;
14177 } while(ins != first);
14178
Stefan Reinauer14e22772010-04-27 06:56:47 +000014179 /*
Eric Biederman90089602004-05-28 14:11:54 +000014180 * Build the list of variables to enclose.
14181 *
14182 * A target it to put the same variable in the
14183 * same slot for ever call of a given function.
14184 * After coloring this removes all of the variable
14185 * manipulation code.
14186 *
14187 * The list of variables to enclose is built ordered
14188 * program order because except in corner cases this
14189 * gives me the stability of assignment I need.
14190 *
14191 * To gurantee that stability I lookup the variables
14192 * to see where they have been used before and
14193 * I build my final list with the assigned indicies.
14194 */
14195 vars = 0;
14196 if (enclose_triple(old_result)) {
14197 ordered_triple_set(&vars, old_result);
14198 }
14199 for(set = rb[block->vertex].out; set; set = set->next) {
14200 if (!enclose_triple(set->member)) {
14201 continue;
14202 }
14203 if ((set->member == fcall) || (set->member == old_result)) {
14204 continue;
14205 }
14206 if (!local_triple(state, me, set->member)) {
14207 internal_error(state, set->member, "not local?");
14208 }
14209 ordered_triple_set(&vars, set->member);
14210 }
14211
14212 /* Lookup the current indicies of the live varialbe */
14213 used_indicies = 0;
14214 max_index = -1;
14215 for(set = vars; set ; set = set->next) {
14216 struct triple *ins;
14217 int index;
14218 ins = set->member;
14219 index = lookup_closure_index(state, me, ins);
14220 info[ID_BITS(ins->id)].index = index;
14221 if (index < 0) {
14222 continue;
14223 }
14224 if (index >= MAX_INDICIES) {
14225 internal_error(state, ins, "index unexpectedly large");
14226 }
14227 if (used_indicies & (1 << index)) {
14228 internal_error(state, ins, "index previously used?");
14229 }
14230 /* Remember which indicies have been used */
14231 used_indicies |= (1 << index);
14232 if (index > max_index) {
14233 max_index = index;
14234 }
14235 }
14236
14237 /* Walk through the live variables and make certain
14238 * everything is assigned an index.
14239 */
14240 for(set = vars; set; set = set->next) {
14241 struct triple *ins;
14242 int index;
14243 ins = set->member;
14244 index = info[ID_BITS(ins->id)].index;
14245 if (index >= 0) {
14246 continue;
14247 }
14248 /* Find the lowest unused index value */
14249 for(index = 0; index < MAX_INDICIES; index++) {
14250 if (!(used_indicies & (1 << index))) {
14251 break;
14252 }
14253 }
14254 if (index == MAX_INDICIES) {
14255 internal_error(state, ins, "no free indicies?");
14256 }
14257 info[ID_BITS(ins->id)].index = index;
14258 /* Remember which indicies have been used */
14259 used_indicies |= (1 << index);
14260 if (index > max_index) {
14261 max_index = index;
14262 }
14263 }
14264
14265 /* Build the return list of variables with positions matching
14266 * their indicies.
14267 */
14268 *enclose = 0;
14269 last_var = enclose;
14270 for(i = 0; i <= max_index; i++) {
14271 struct triple *var;
14272 var = 0;
14273 if (used_indicies & (1 << i)) {
14274 for(set = vars; set; set = set->next) {
14275 int index;
14276 index = info[ID_BITS(set->member->id)].index;
14277 if (index == i) {
14278 var = set->member;
14279 break;
14280 }
14281 }
14282 if (!var) {
14283 internal_error(state, me, "missing variable");
14284 }
14285 }
14286 insert_triple_set(last_var, var);
14287 last_var = &(*last_var)->next;
14288 }
14289
14290#if DEBUG_EXPLICIT_CLOSURES
14291 /* Print out the variables to be enclosed */
14292 loc(state->dbgout, state, fcall);
14293 fprintf(state->dbgout, "Alive: \n");
14294 for(set = *enclose; set; set = set->next) {
14295 display_triple(state->dbgout, set->member);
14296 }
14297 fflush(state->dbgout);
14298#endif
14299
14300 /* Clear the marks */
14301 ins = first;
14302 do {
14303 ins->id = info[ID_BITS(ins->id)].id;
14304 ins = ins->next;
14305 } while(ins != first);
14306
14307 /* Release the ordered list of live variables */
14308 free_closure_variables(state, &vars);
14309
14310 /* Release the storage of the old ids */
14311 xfree(info);
14312
14313 /* Release the variable lifetime information */
14314 free_variable_lifetimes(state, &bb, rb);
14315
14316 /* Release the basic blocks of this function */
14317 free_basic_blocks(state, &bb);
14318}
14319
14320static void expand_function_call(
14321 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014322{
14323 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014324 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014325 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014326 struct triple *first;
14327 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014328 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014329 struct triple_reg_set *enclose, *set;
14330 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014331
Eric Biederman90089602004-05-28 14:11:54 +000014332#if DEBUG_EXPLICIT_CLOSURES
14333 FILE *fp = state->dbgout;
14334 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14335 display_func(state, fp, MISC(fcall, 0));
14336 display_func(state, fp, me);
14337 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14338#endif
14339
Eric Biederman5ade04a2003-10-22 04:03:46 +000014340 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014341 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014342 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014343 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014344 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014345 first = fcall->next;
14346
14347 /* Find what I need to enclose */
14348 compute_closure_variables(state, me, fcall, &enclose);
14349
14350 /* Compute the closure type */
14351 closure_type = new_type(TYPE_TUPLE, 0, 0);
14352 closure_type->elements = 0;
14353 closure_next = &closure_type->left;
14354 for(set = enclose; set ; set = set->next) {
14355 struct type *type;
14356 type = &void_type;
14357 if (set->member) {
14358 type = set->member->type;
14359 }
14360 if (!*closure_next) {
14361 *closure_next = type;
14362 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014363 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014364 type);
14365 closure_next = &(*closure_next)->right;
14366 }
14367 closure_type->elements += 1;
14368 }
14369 if (closure_type->elements == 0) {
14370 closure_type->type = TYPE_VOID;
14371 }
14372
14373
14374#if DEBUG_EXPLICIT_CLOSURES
14375 fprintf(state->dbgout, "closure type: ");
14376 name_of(state->dbgout, closure_type);
14377 fprintf(state->dbgout, "\n");
14378#endif
14379
14380 /* Update the called functions closure variable */
14381 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014382
14383 /* Generate some needed triples */
14384 ret_loc = label(state);
14385 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14386
14387 /* Pass the parameters to the new function */
14388 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014389 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014390 for(i = 0; i < pvals; i++) {
14391 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014392 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014393 atype = ptype;
14394 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14395 atype = ptype->left;
14396 }
Eric Biederman90089602004-05-28 14:11:54 +000014397 param = farg(state, func, i);
14398 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14399 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014400 }
Eric Biederman90089602004-05-28 14:11:54 +000014401 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014402 flatten(state, first, write_expr(state, param, arg));
14403 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014404 }
Eric Biederman90089602004-05-28 14:11:54 +000014405 rtype = func->type->left;
14406
Eric Biederman5ade04a2003-10-22 04:03:46 +000014407 /* Thread the triples together */
14408 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014409
14410 /* Save the active variables in the result variable */
14411 for(i = 0, set = enclose; set ; set = set->next, i++) {
14412 if (!set->member) {
14413 continue;
14414 }
14415 flatten(state, ret_loc,
14416 write_expr(state,
14417 closure_expr(state, func, closure_idx, i),
14418 read_expr(state, set->member)));
14419 }
14420
14421 /* Initialize the return value */
14422 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014423 flatten(state, ret_loc,
14424 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014425 deref_index(state, fresult(state, func), 1),
14426 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14427 }
14428
Eric Biederman5ade04a2003-10-22 04:03:46 +000014429 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014430 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14431 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014432 call(state, retvar, ret_addr, func_first, func_last));
14433
Eric Biederman7dea9552004-06-29 05:38:37 +000014434 /* Find the result */
14435 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14436 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014437 result = flatten(state, first,
14438 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014439 deref_index(state, fresult(state, func), 1)));
14440
14441 propogate_use(state, fcall, result);
14442 }
14443
14444 /* Release the original fcall instruction */
14445 release_triple(state, fcall);
14446
Eric Biederman90089602004-05-28 14:11:54 +000014447 /* Restore the active variables from the result variable */
14448 for(i = 0, set = enclose; set ; set = set->next, i++) {
14449 struct triple_set *use, *next;
14450 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014451 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014452 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014453 continue;
14454 }
Eric Biederman90089602004-05-28 14:11:54 +000014455 /* Generate an expression for the value */
14456 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014457 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014458 closure_expr(state, func, closure_idx, i)));
14459
14460
14461 /* If the original is an lvalue restore the preserved value */
14462 if (is_lvalue(state, set->member)) {
14463 flatten(state, first,
14464 write_expr(state, set->member, new));
14465 continue;
14466 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014467 /*
14468 * If the original is a value update the dominated uses.
14469 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014470
Eric Biederman7dea9552004-06-29 05:38:37 +000014471 /* Analyze the basic blocks so I can see who dominates whom */
14472 bb.func = me;
14473 bb.first = RHS(me, 0);
14474 if (!triple_is_ret(state, bb.first->prev)) {
14475 bb.func = 0;
14476 }
14477 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014478
Eric Biederman90089602004-05-28 14:11:54 +000014479
14480#if DEBUG_EXPLICIT_CLOSURES
14481 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14482 set->member, new);
14483#endif
14484 /* If fcall dominates the use update the expression */
14485 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014486 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014487 * removes use, so I must take a copy of the
14488 * next entry early.
14489 */
14490 next = use->next;
14491 if (!tdominates(state, fcall, use->member)) {
14492 continue;
14493 }
14494 replace_use(state, set->member, new, use->member);
14495 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014496
14497 /* Release the basic blocks, the instructions will be
14498 * different next time, and flatten/insert_triple does
14499 * not update the block values so I can't cache the analysis.
14500 */
14501 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014502 }
14503
Eric Biederman90089602004-05-28 14:11:54 +000014504 /* Release the closure variable list */
14505 free_closure_variables(state, &enclose);
14506
14507 if (state->compiler->debug & DEBUG_INLINE) {
14508 FILE *fp = state->dbgout;
14509 fprintf(fp, "\n");
14510 loc(fp, state, 0);
14511 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14512 display_func(state, fp, func);
14513 display_func(state, fp, me);
14514 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14515 }
14516
14517 return;
14518}
14519
14520static int do_inline(struct compile_state *state, struct triple *func)
14521{
14522 int do_inline;
14523 int policy;
14524
14525 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14526 switch(policy) {
14527 case COMPILER_INLINE_ALWAYS:
14528 do_inline = 1;
14529 if (func->type->type & ATTRIB_NOINLINE) {
14530 error(state, func, "noinline with always_inline compiler option");
14531 }
14532 break;
14533 case COMPILER_INLINE_NEVER:
14534 do_inline = 0;
14535 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14536 error(state, func, "always_inline with noinline compiler option");
14537 }
14538 break;
14539 case COMPILER_INLINE_DEFAULTON:
14540 switch(func->type->type & STOR_MASK) {
14541 case STOR_STATIC | STOR_INLINE:
14542 case STOR_LOCAL | STOR_INLINE:
14543 case STOR_EXTERN | STOR_INLINE:
14544 do_inline = 1;
14545 break;
14546 default:
14547 do_inline = 1;
14548 break;
14549 }
14550 break;
14551 case COMPILER_INLINE_DEFAULTOFF:
14552 switch(func->type->type & STOR_MASK) {
14553 case STOR_STATIC | STOR_INLINE:
14554 case STOR_LOCAL | STOR_INLINE:
14555 case STOR_EXTERN | STOR_INLINE:
14556 do_inline = 1;
14557 break;
14558 default:
14559 do_inline = 0;
14560 break;
14561 }
14562 break;
14563 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014564 switch(func->type->type & STOR_MASK) {
14565 case STOR_STATIC | STOR_INLINE:
14566 case STOR_LOCAL | STOR_INLINE:
14567 case STOR_EXTERN | STOR_INLINE:
14568 do_inline = 1;
14569 break;
14570 default:
14571 do_inline = (func->u.cval == 1);
14572 break;
14573 }
Eric Biederman90089602004-05-28 14:11:54 +000014574 break;
14575 default:
14576 do_inline = 0;
14577 internal_error(state, 0, "Unimplemented inline policy");
14578 break;
14579 }
14580 /* Force inlining */
14581 if (func->type->type & ATTRIB_NOINLINE) {
14582 do_inline = 0;
14583 }
14584 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14585 do_inline = 1;
14586 }
14587 return do_inline;
14588}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014589
Eric Biederman90089602004-05-28 14:11:54 +000014590static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14591{
14592 struct triple *first, *ptr, *next;
14593 /* If the function is not used don't bother */
14594 if (me->u.cval <= 0) {
14595 return;
14596 }
14597 if (state->compiler->debug & DEBUG_CALLS2) {
14598 FILE *fp = state->dbgout;
14599 fprintf(fp, "in: %s\n",
14600 me->type->type_ident->name);
14601 }
14602
14603 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014604 ptr = next = first;
14605 do {
Eric Biederman90089602004-05-28 14:11:54 +000014606 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014607 ptr = next;
14608 prev = ptr->prev;
14609 next = ptr->next;
14610 if (ptr->op != OP_FCALL) {
14611 continue;
14612 }
14613 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014614 /* See if the function should be inlined */
14615 if (!do_inline(state, func)) {
14616 /* Put a label after the fcall */
14617 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14618 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014619 }
Eric Biederman90089602004-05-28 14:11:54 +000014620 if (state->compiler->debug & DEBUG_CALLS) {
14621 FILE *fp = state->dbgout;
14622 if (state->compiler->debug & DEBUG_CALLS2) {
14623 loc(fp, state, ptr);
14624 }
14625 fprintf(fp, "inlining %s\n",
14626 func->type->type_ident->name);
14627 fflush(fp);
14628 }
14629
14630 /* Update the function use counts */
14631 func->u.cval -= 1;
14632
14633 /* Replace the fcall with the called function */
14634 expand_inline_call(state, me, ptr);
14635
14636 next = prev->next;
14637 } while (next != first);
14638
14639 ptr = next = first;
14640 do {
14641 struct triple *prev, *func;
14642 ptr = next;
14643 prev = ptr->prev;
14644 next = ptr->next;
14645 if (ptr->op != OP_FCALL) {
14646 continue;
14647 }
14648 func = MISC(ptr, 0);
14649 if (state->compiler->debug & DEBUG_CALLS) {
14650 FILE *fp = state->dbgout;
14651 if (state->compiler->debug & DEBUG_CALLS2) {
14652 loc(fp, state, ptr);
14653 }
14654 fprintf(fp, "calling %s\n",
14655 func->type->type_ident->name);
14656 fflush(fp);
14657 }
14658 /* Replace the fcall with the instruction sequence
14659 * needed to make the call.
14660 */
14661 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014662 next = prev->next;
14663 } while(next != first);
14664}
Eric Biederman90089602004-05-28 14:11:54 +000014665
14666static void inline_functions(struct compile_state *state, struct triple *func)
14667{
14668 inline_function(state, func, 0);
14669 reverse_walk_functions(state, inline_function, 0);
14670}
14671
Eric Biederman5ade04a2003-10-22 04:03:46 +000014672static void insert_function(struct compile_state *state,
14673 struct triple *func, void *arg)
14674{
14675 struct triple *first, *end, *ffirst, *fend;
14676
14677 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014678 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014679 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014680 func->type->type_ident->name, func->u.cval);
14681 }
14682 if (func->u.cval == 0) {
14683 return;
14684 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014685
14686 /* Find the end points of the lists */
14687 first = arg;
14688 end = first->prev;
14689 ffirst = RHS(func, 0);
14690 fend = ffirst->prev;
14691
14692 /* splice the lists together */
14693 end->next = ffirst;
14694 ffirst->prev = end;
14695 fend->next = first;
14696 first->prev = fend;
14697}
14698
Eric Biederman90089602004-05-28 14:11:54 +000014699struct triple *input_asm(struct compile_state *state)
14700{
14701 struct asm_info *info;
14702 struct triple *def;
14703 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014704
Eric Biederman90089602004-05-28 14:11:54 +000014705 info = xcmalloc(sizeof(*info), "asm_info");
14706 info->str = "";
14707
14708 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14709 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14710
14711 def = new_triple(state, OP_ASM, &void_type, out, 0);
14712 def->u.ainfo = info;
14713 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014714
Eric Biederman90089602004-05-28 14:11:54 +000014715 for(i = 0; i < out; i++) {
14716 struct triple *piece;
14717 piece = triple(state, OP_PIECE, &int_type, def, 0);
14718 piece->u.cval = i;
14719 LHS(def, i) = piece;
14720 }
14721
14722 return def;
14723}
14724
14725struct triple *output_asm(struct compile_state *state)
14726{
14727 struct asm_info *info;
14728 struct triple *def;
14729 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014730
Eric Biederman90089602004-05-28 14:11:54 +000014731 info = xcmalloc(sizeof(*info), "asm_info");
14732 info->str = "";
14733
14734 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14735 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14736
14737 def = new_triple(state, OP_ASM, &void_type, 0, in);
14738 def->u.ainfo = info;
14739 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014740
Eric Biederman90089602004-05-28 14:11:54 +000014741 return def;
14742}
14743
Eric Biederman5ade04a2003-10-22 04:03:46 +000014744static void join_functions(struct compile_state *state)
14745{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014746 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014747 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014748 struct type *pnext, *param;
14749 struct type *result_type, *args_type;
14750 int idx;
14751
14752 /* Be clear the functions have not been joined yet */
14753 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014754
14755 /* Dummy file state to get debug handing right */
14756 memset(&file, 0, sizeof(file));
14757 file.basename = "";
14758 file.line = 0;
14759 file.report_line = 0;
14760 file.report_name = file.basename;
14761 file.prev = state->file;
14762 state->file = &file;
14763 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014764
Eric Biederman41203d92004-11-08 09:31:09 +000014765 if (!state->main_function) {
14766 error(state, 0, "No functions to compile\n");
14767 }
14768
Eric Biederman90089602004-05-28 14:11:54 +000014769 /* The type of arguments */
14770 args_type = state->main_function->type->right;
14771 /* The return type without any specifiers */
14772 result_type = clone_type(0, state->main_function->type->left);
14773
14774
14775 /* Verify the external arguments */
14776 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014777 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014778 "Too many external input arguments");
14779 }
14780 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014781 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014782 "Too many external output arguments");
14783 }
14784
Eric Biederman5ade04a2003-10-22 04:03:46 +000014785 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014786 end = label(state);
14787 start = label(state);
14788 start = flatten(state, state->first, start);
14789 end = flatten(state, state->first, end);
14790 in = input_asm(state);
14791 out = output_asm(state);
14792 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014793 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014794 in = flatten(state, state->first, in);
14795 call = flatten(state, state->first, call);
14796 out = flatten(state, state->first, out);
14797
14798
14799 /* Read the external input arguments */
14800 pnext = args_type;
14801 idx = 0;
14802 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14803 struct triple *expr;
14804 param = pnext;
14805 pnext = 0;
14806 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14807 pnext = param->right;
14808 param = param->left;
14809 }
14810 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014811 error(state, state->main_function,
14812 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014813 idx + 1, param->field_ident->name);
14814 }
14815 expr = read_expr(state, LHS(in, idx));
14816 RHS(call, idx) = expr;
14817 expr = flatten(state, call, expr);
14818 use_triple(expr, call);
14819
Stefan Reinauer14e22772010-04-27 06:56:47 +000014820 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014821 }
14822
14823
14824 /* Write the external output arguments */
14825 pnext = result_type;
14826 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14827 pnext = result_type->left;
14828 }
14829 for(idx = 0; idx < out->rhs; idx++) {
14830 struct triple *expr;
14831 param = pnext;
14832 pnext = 0;
14833 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14834 pnext = param->right;
14835 param = param->left;
14836 }
14837 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14838 param = 0;
14839 }
14840 if (param) {
14841 if (registers_of(state, param) != 1) {
14842 error(state, state->main_function,
14843 "Result: %d %s requires multiple registers",
14844 idx, param->field_ident->name);
14845 }
14846 expr = read_expr(state, call);
14847 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14848 expr = deref_field(state, expr, param->field_ident);
14849 }
14850 } else {
14851 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14852 }
14853 flatten(state, out, expr);
14854 RHS(out, idx) = expr;
14855 use_triple(expr, out);
14856 }
14857
14858 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014859 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014860 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14861 func->type->type_ident = lookup(state, "", 0);
14862 RHS(func, 0) = state->first;
14863 func->u.cval = 1;
14864
Eric Biederman5ade04a2003-10-22 04:03:46 +000014865 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014866 mark_live_functions(state);
14867 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014868 walk_functions(state, insert_function, end);
14869
14870 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014871 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014872 }
14873
Eric Biederman90089602004-05-28 14:11:54 +000014874 /* OK now the functions have been joined. */
14875 state->functions_joined = 1;
14876
Eric Biederman5ade04a2003-10-22 04:03:46 +000014877 /* Done now cleanup */
14878 state->file = file.prev;
14879 state->function = 0;
14880}
14881
Eric Biedermanb138ac82003-04-22 18:44:01 +000014882/*
14883 * Data structurs for optimation.
14884 */
14885
Eric Biederman5ade04a2003-10-22 04:03:46 +000014886
Eric Biederman83b991a2003-10-11 06:20:25 +000014887static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014888 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014889 int front)
14890{
14891 struct block_set **ptr, *new;
14892 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014893 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014894 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014895 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014896 ptr = head;
14897 while(*ptr) {
14898 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014899 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014900 }
14901 ptr = &(*ptr)->next;
14902 }
14903 new = xcmalloc(sizeof(*new), "block_set");
14904 new->member = user;
14905 if (front) {
14906 new->next = *head;
14907 *head = new;
14908 }
14909 else {
14910 new->next = 0;
14911 *ptr = new;
14912 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014913 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014914}
Eric Biederman83b991a2003-10-11 06:20:25 +000014915static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014916 struct block *used, struct block_set **head, struct block *unuser)
14917{
14918 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014919 int count;
14920 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014921 ptr = head;
14922 while(*ptr) {
14923 use = *ptr;
14924 if (use->member == unuser) {
14925 *ptr = use->next;
14926 memset(use, -1, sizeof(*use));
14927 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014928 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014929 }
14930 else {
14931 ptr = &use->next;
14932 }
14933 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014934 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014935}
14936
14937static void use_block(struct block *used, struct block *user)
14938{
Eric Biederman83b991a2003-10-11 06:20:25 +000014939 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014940 /* Append new to the head of the list, print_block
14941 * depends on this.
14942 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014943 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014944 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014945}
14946static void unuse_block(struct block *used, struct block *unuser)
14947{
Eric Biederman83b991a2003-10-11 06:20:25 +000014948 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014949 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014950 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014951}
14952
Eric Biederman5ade04a2003-10-22 04:03:46 +000014953static void add_block_edge(struct block *block, struct block *edge, int front)
14954{
14955 int count;
14956 count = do_use_block(block, &block->edges, edge, front);
14957 block->edge_count += count;
14958}
14959
14960static void remove_block_edge(struct block *block, struct block *edge)
14961{
14962 int count;
14963 count = do_unuse_block(block, &block->edges, edge);
14964 block->edge_count -= count;
14965}
14966
Eric Biedermanb138ac82003-04-22 18:44:01 +000014967static void idom_block(struct block *idom, struct block *user)
14968{
14969 do_use_block(idom, &idom->idominates, user, 0);
14970}
14971
14972static void unidom_block(struct block *idom, struct block *unuser)
14973{
14974 do_unuse_block(idom, &idom->idominates, unuser);
14975}
14976
14977static void domf_block(struct block *block, struct block *domf)
14978{
14979 do_use_block(block, &block->domfrontier, domf, 0);
14980}
14981
14982static void undomf_block(struct block *block, struct block *undomf)
14983{
14984 do_unuse_block(block, &block->domfrontier, undomf);
14985}
14986
14987static void ipdom_block(struct block *ipdom, struct block *user)
14988{
14989 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14990}
14991
14992static void unipdom_block(struct block *ipdom, struct block *unuser)
14993{
14994 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14995}
14996
14997static void ipdomf_block(struct block *block, struct block *ipdomf)
14998{
14999 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
15000}
15001
15002static void unipdomf_block(struct block *block, struct block *unipdomf)
15003{
15004 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15005}
15006
Eric Biederman83b991a2003-10-11 06:20:25 +000015007static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015008 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015009 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15010 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015011{
Eric Biederman83b991a2003-10-11 06:20:25 +000015012 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015013 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015014 ptr = state->first;
15015 do {
Eric Biederman90089602004-05-28 14:11:54 +000015016 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015017 if (ptr->next->prev != ptr) {
15018 internal_error(state, ptr->next, "bad prev");
15019 }
15020 ptr = ptr->next;
15021 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015022 return result;
15023}
15024
Eric Biedermanb138ac82003-04-22 18:44:01 +000015025#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015026static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015027{
Eric Biederman90089602004-05-28 14:11:54 +000015028 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015029 int op;
15030 op = ins->op;
15031 if (op == OP_LIST) {
15032#if !PRINT_LIST
15033 return 0;
15034#endif
15035 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015036 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015037 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015038 }
Eric Biederman90089602004-05-28 14:11:54 +000015039 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015040
Stefan Reinauer14e22772010-04-27 06:56:47 +000015041 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015042 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015043 internal_error(state, ins, "branch used?");
15044 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015045 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015046 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015047 }
15048 return 0;
15049}
15050
Eric Biedermanb138ac82003-04-22 18:44:01 +000015051static void print_triples(struct compile_state *state)
15052{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015053 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015054 FILE *fp = state->dbgout;
15055 fprintf(fp, "--------------- triples ---------------\n");
15056 walk_triples(state, do_print_triple, fp);
15057 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015058 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015059}
15060
15061struct cf_block {
15062 struct block *block;
15063};
15064static void find_cf_blocks(struct cf_block *cf, struct block *block)
15065{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015066 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015067 if (!block || (cf[block->vertex].block == block)) {
15068 return;
15069 }
15070 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015071 for(edge = block->edges; edge; edge = edge->next) {
15072 find_cf_blocks(cf, edge->member);
15073 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015074}
15075
Eric Biederman90089602004-05-28 14:11:54 +000015076static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015077 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015078{
15079 struct cf_block *cf;
15080 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015081 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015082 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15083 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015084
Eric Biederman90089602004-05-28 14:11:54 +000015085 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015086 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015087 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015088 block = cf[i].block;
15089 if (!block)
15090 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015091 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015092 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015093 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015094 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015095 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015096 }
15097
15098 xfree(cf);
15099}
15100
Eric Biedermanb138ac82003-04-22 18:44:01 +000015101static void free_basic_block(struct compile_state *state, struct block *block)
15102{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015103 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015104 struct block *child;
15105 if (!block) {
15106 return;
15107 }
15108 if (block->vertex == -1) {
15109 return;
15110 }
15111 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015112 for(edge = block->edges; edge; edge = edge->next) {
15113 if (edge->member) {
15114 unuse_block(edge->member, block);
15115 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015116 }
15117 if (block->idom) {
15118 unidom_block(block->idom, block);
15119 }
15120 block->idom = 0;
15121 if (block->ipdom) {
15122 unipdom_block(block->ipdom, block);
15123 }
15124 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015125 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015126 child = entry->member;
15127 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015128 if (child && (child->vertex != -1)) {
15129 for(edge = child->edges; edge; edge = edge->next) {
15130 edge->member = 0;
15131 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015132 }
15133 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015134 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015135 child = entry->member;
15136 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015137 if (child && (child->vertex != -1)) {
15138 child->idom = 0;
15139 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015140 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015141 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015142 child = entry->member;
15143 undomf_block(block, child);
15144 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015145 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015146 child = entry->member;
15147 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015148 if (child && (child->vertex != -1)) {
15149 child->ipdom = 0;
15150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015151 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015152 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015153 child = entry->member;
15154 unipdomf_block(block, child);
15155 }
15156 if (block->users != 0) {
15157 internal_error(state, 0, "block still has users");
15158 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015159 while((edge = block->edges)) {
15160 child = edge->member;
15161 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015162
Eric Biederman5ade04a2003-10-22 04:03:46 +000015163 if (child && (child->vertex != -1)) {
15164 free_basic_block(state, child);
15165 }
15166 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015167 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015168#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015169 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015170#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015171}
15172
Stefan Reinauer14e22772010-04-27 06:56:47 +000015173static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015174 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015175{
15176 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015177 free_basic_block(state, bb->first_block);
15178 bb->last_vertex = 0;
15179 bb->first_block = bb->last_block = 0;
15180 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015181 ins = first;
15182 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015183 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015184 ins->u.block = 0;
15185 }
15186 ins = ins->next;
15187 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015188
Eric Biedermanb138ac82003-04-22 18:44:01 +000015189}
15190
Stefan Reinauer14e22772010-04-27 06:56:47 +000015191static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015192 struct basic_blocks *bb, struct triple *first)
15193{
15194 struct block *block;
15195 struct triple *ptr;
15196 if (!triple_is_label(state, first)) {
15197 internal_error(state, first, "block does not start with a label");
15198 }
15199 /* See if this basic block has already been setup */
15200 if (first->u.block != 0) {
15201 return first->u.block;
15202 }
15203 /* Allocate another basic block structure */
15204 bb->last_vertex += 1;
15205 block = xcmalloc(sizeof(*block), "block");
15206 block->first = block->last = first;
15207 block->vertex = bb->last_vertex;
15208 ptr = first;
15209 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015210 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015211 break;
15212 }
15213 block->last = ptr;
15214 /* If ptr->u is not used remember where the baic block is */
15215 if (triple_stores_block(state, ptr)) {
15216 ptr->u.block = block;
15217 }
15218 if (triple_is_branch(state, ptr)) {
15219 break;
15220 }
15221 ptr = ptr->next;
15222 } while (ptr != bb->first);
15223 if ((ptr == bb->first) ||
15224 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015225 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015226 triple_is_ret(state, ptr))))
15227 {
15228 /* The block has no outflowing edges */
15229 }
15230 else if (triple_is_label(state, ptr)) {
15231 struct block *next;
15232 next = basic_block(state, bb, ptr);
15233 add_block_edge(block, next, 0);
15234 use_block(next, block);
15235 }
15236 else if (triple_is_branch(state, ptr)) {
15237 struct triple **expr, *first;
15238 struct block *child;
15239 /* Find the branch targets.
15240 * I special case the first branch as that magically
15241 * avoids some difficult cases for the register allocator.
15242 */
15243 expr = triple_edge_targ(state, ptr, 0);
15244 if (!expr) {
15245 internal_error(state, ptr, "branch without targets");
15246 }
15247 first = *expr;
15248 expr = triple_edge_targ(state, ptr, expr);
15249 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15250 if (!*expr) continue;
15251 child = basic_block(state, bb, *expr);
15252 use_block(child, block);
15253 add_block_edge(block, child, 0);
15254 }
15255 if (first) {
15256 child = basic_block(state, bb, first);
15257 use_block(child, block);
15258 add_block_edge(block, child, 1);
15259
15260 /* Be certain the return block of a call is
15261 * in a basic block. When it is not find
15262 * start of the block, insert a label if
15263 * necessary and build the basic block.
15264 * Then add a fake edge from the start block
15265 * to the return block of the function.
15266 */
15267 if (state->functions_joined && triple_is_call(state, ptr)
15268 && !block_of_triple(state, MISC(ptr, 0))) {
15269 struct block *tail;
15270 struct triple *start;
15271 start = triple_to_block_start(state, MISC(ptr, 0));
15272 if (!triple_is_label(state, start)) {
15273 start = pre_triple(state,
15274 start, OP_LABEL, &void_type, 0, 0);
15275 }
15276 tail = basic_block(state, bb, start);
15277 add_block_edge(child, tail, 0);
15278 use_block(tail, child);
15279 }
15280 }
15281 }
15282 else {
15283 internal_error(state, 0, "Bad basic block split");
15284 }
15285#if 0
15286{
15287 struct block_set *edge;
15288 FILE *fp = state->errout;
15289 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015290 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015291 block->first, block->last);
15292 for(edge = block->edges; edge; edge = edge->next) {
15293 fprintf(fp, " %10p [%2d]",
15294 edge->member ? edge->member->first : 0,
15295 edge->member ? edge->member->vertex : -1);
15296 }
15297 fprintf(fp, "\n");
15298}
15299#endif
15300 return block;
15301}
15302
15303
15304static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15305 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15306 void *arg)
15307{
15308 struct triple *ptr, *first;
15309 struct block *last_block;
15310 last_block = 0;
15311 first = bb->first;
15312 ptr = first;
15313 do {
15314 if (triple_stores_block(state, ptr)) {
15315 struct block *block;
15316 block = ptr->u.block;
15317 if (block && (block != last_block)) {
15318 cb(state, block, arg);
15319 }
15320 last_block = block;
15321 }
15322 ptr = ptr->next;
15323 } while(ptr != first);
15324}
15325
15326static void print_block(
15327 struct compile_state *state, struct block *block, void *arg)
15328{
15329 struct block_set *user, *edge;
15330 struct triple *ptr;
15331 FILE *fp = arg;
15332
15333 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015334 block,
Eric Biederman90089602004-05-28 14:11:54 +000015335 block->vertex);
15336
15337 for(edge = block->edges; edge; edge = edge->next) {
15338 fprintf(fp, " %p<-%p",
15339 edge->member,
15340 (edge->member && edge->member->use)?
15341 edge->member->use->member : 0);
15342 }
15343 fprintf(fp, "\n");
15344 if (block->first->op == OP_LABEL) {
15345 fprintf(fp, "%p:\n", block->first);
15346 }
15347 for(ptr = block->first; ; ) {
15348 display_triple(fp, ptr);
15349 if (ptr == block->last)
15350 break;
15351 ptr = ptr->next;
15352 if (ptr == block->first) {
15353 internal_error(state, 0, "missing block last?");
15354 }
15355 }
15356 fprintf(fp, "users %d: ", block->users);
15357 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015358 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015359 user->member,
15360 user->member->vertex);
15361 }
15362 fprintf(fp,"\n\n");
15363}
15364
15365
15366static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15367{
15368 fprintf(fp, "--------------- blocks ---------------\n");
15369 walk_blocks(state, &state->bb, print_block, fp);
15370}
15371static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15372{
15373 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15374 fprintf(fp, "After %s\n", func);
15375 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015376 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15377 print_dominators(state, fp, &state->bb);
15378 print_dominance_frontiers(state, fp, &state->bb);
15379 }
15380 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015381 }
15382}
15383
Stefan Reinauer14e22772010-04-27 06:56:47 +000015384static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015385 struct basic_blocks *bb)
15386{
15387 struct block *block;
15388 struct triple *first, *ins, *next;
15389 /* Delete the triples not in a basic block */
15390 block = 0;
15391 first = bb->first;
15392 ins = first;
15393 do {
15394 next = ins->next;
15395 if (ins->op == OP_LABEL) {
15396 block = ins->u.block;
15397 }
15398 if (!block) {
15399 struct triple_set *use;
15400 for(use = ins->use; use; use = use->next) {
15401 struct block *block;
15402 block = block_of_triple(state, use->member);
15403 if (block != 0) {
15404 internal_error(state, ins, "pruning used ins?");
15405 }
15406 }
15407 release_triple(state, ins);
15408 }
15409 if (block && block->last == ins) {
15410 block = 0;
15411 }
15412 ins = next;
15413 } while(ins != first);
15414}
15415
Stefan Reinauer14e22772010-04-27 06:56:47 +000015416static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015417 struct basic_blocks *bb)
15418{
15419 if (!triple_stores_block(state, bb->first)) {
15420 internal_error(state, 0, "ins will not store block?");
15421 }
15422 /* Initialize the state */
15423 bb->first_block = bb->last_block = 0;
15424 bb->last_vertex = 0;
15425 free_basic_blocks(state, bb);
15426
15427 /* Find the basic blocks */
15428 bb->first_block = basic_block(state, bb, bb->first);
15429
15430 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015431 * entire program is in a basic block. When it is not find
15432 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015433 * basic block. Then add a fake edge from the start block
15434 * to the final block.
15435 */
15436 if (!block_of_triple(state, bb->first->prev)) {
15437 struct triple *start;
15438 struct block *tail;
15439 start = triple_to_block_start(state, bb->first->prev);
15440 if (!triple_is_label(state, start)) {
15441 start = pre_triple(state,
15442 start, OP_LABEL, &void_type, 0, 0);
15443 }
15444 tail = basic_block(state, bb, start);
15445 add_block_edge(bb->first_block, tail, 0);
15446 use_block(tail, bb->first_block);
15447 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015448
Eric Biederman90089602004-05-28 14:11:54 +000015449 /* Find the last basic block.
15450 */
15451 bb->last_block = block_of_triple(state, bb->first->prev);
15452
15453 /* Delete the triples not in a basic block */
15454 prune_nonblock_triples(state, bb);
15455
15456#if 0
15457 /* If we are debugging print what I have just done */
15458 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15459 print_blocks(state, state->dbgout);
15460 print_control_flow(state, bb);
15461 }
15462#endif
15463}
15464
15465
Eric Biedermanb138ac82003-04-22 18:44:01 +000015466struct sdom_block {
15467 struct block *block;
15468 struct sdom_block *sdominates;
15469 struct sdom_block *sdom_next;
15470 struct sdom_block *sdom;
15471 struct sdom_block *label;
15472 struct sdom_block *parent;
15473 struct sdom_block *ancestor;
15474 int vertex;
15475};
15476
15477
15478static void unsdom_block(struct sdom_block *block)
15479{
15480 struct sdom_block **ptr;
15481 if (!block->sdom_next) {
15482 return;
15483 }
15484 ptr = &block->sdom->sdominates;
15485 while(*ptr) {
15486 if ((*ptr) == block) {
15487 *ptr = block->sdom_next;
15488 return;
15489 }
15490 ptr = &(*ptr)->sdom_next;
15491 }
15492}
15493
15494static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15495{
15496 unsdom_block(block);
15497 block->sdom = sdom;
15498 block->sdom_next = sdom->sdominates;
15499 sdom->sdominates = block;
15500}
15501
15502
15503
15504static int initialize_sdblock(struct sdom_block *sd,
15505 struct block *parent, struct block *block, int vertex)
15506{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015507 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015508 if (!block || (sd[block->vertex].block == block)) {
15509 return vertex;
15510 }
15511 vertex += 1;
15512 /* Renumber the blocks in a convinient fashion */
15513 block->vertex = vertex;
15514 sd[vertex].block = block;
15515 sd[vertex].sdom = &sd[vertex];
15516 sd[vertex].label = &sd[vertex];
15517 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15518 sd[vertex].ancestor = 0;
15519 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015520 for(edge = block->edges; edge; edge = edge->next) {
15521 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015523 return vertex;
15524}
15525
Eric Biederman83b991a2003-10-11 06:20:25 +000015526static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015527 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015528 struct block *parent, struct block *block, int vertex)
15529{
15530 struct block_set *user;
15531 if (!block || (sd[block->vertex].block == block)) {
15532 return vertex;
15533 }
15534 vertex += 1;
15535 /* Renumber the blocks in a convinient fashion */
15536 block->vertex = vertex;
15537 sd[vertex].block = block;
15538 sd[vertex].sdom = &sd[vertex];
15539 sd[vertex].label = &sd[vertex];
15540 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15541 sd[vertex].ancestor = 0;
15542 sd[vertex].vertex = vertex;
15543 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015544 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015545 }
15546 return vertex;
15547}
15548
Stefan Reinauer14e22772010-04-27 06:56:47 +000015549static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015550 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015551{
15552 struct block *block;
15553 int vertex;
15554 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015555 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015556
Eric Biederman5ade04a2003-10-22 04:03:46 +000015557 /* Walk through the graph and find unconnected blocks. Add a
15558 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015559 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015560 */
Eric Biederman90089602004-05-28 14:11:54 +000015561 block = bb->first_block->last->next->u.block;
15562 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015563 if (sd[block->vertex].block == block) {
15564 continue;
15565 }
Eric Biederman530b5192003-07-01 10:05:30 +000015566#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015567 {
15568 FILE *fp = state->errout;
15569 fprintf(fp, "Adding %d\n", vertex +1);
15570 }
Eric Biederman530b5192003-07-01 10:05:30 +000015571#endif
Eric Biederman90089602004-05-28 14:11:54 +000015572 add_block_edge(block, bb->last_block, 0);
15573 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015574
Eric Biederman90089602004-05-28 14:11:54 +000015575 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015576 }
15577 return vertex;
15578}
15579
15580static void compress_ancestors(struct sdom_block *v)
15581{
15582 /* This procedure assumes ancestor(v) != 0 */
15583 /* if (ancestor(ancestor(v)) != 0) {
15584 * compress(ancestor(ancestor(v)));
15585 * if (semi(label(ancestor(v))) < semi(label(v))) {
15586 * label(v) = label(ancestor(v));
15587 * }
15588 * ancestor(v) = ancestor(ancestor(v));
15589 * }
15590 */
15591 if (!v->ancestor) {
15592 return;
15593 }
15594 if (v->ancestor->ancestor) {
15595 compress_ancestors(v->ancestor->ancestor);
15596 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15597 v->label = v->ancestor->label;
15598 }
15599 v->ancestor = v->ancestor->ancestor;
15600 }
15601}
15602
Stefan Reinauer14e22772010-04-27 06:56:47 +000015603static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015604 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015605{
15606 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015607 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015608 * for each v <= pred(w) {
15609 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015610 * if (semi[u] < semi[w] {
15611 * semi[w] = semi[u];
15612 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015613 * }
15614 * add w to bucket(vertex(semi[w]));
15615 * LINK(parent(w), w);
15616 *
15617 * // step 3
15618 * for each v <= bucket(parent(w)) {
15619 * delete v from bucket(parent(w));
15620 * u = EVAL(v);
15621 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15622 * }
15623 */
Eric Biederman90089602004-05-28 14:11:54 +000015624 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015625 struct sdom_block *v, *parent, *next;
15626 struct block_set *user;
15627 struct block *block;
15628 block = sd[i].block;
15629 parent = sd[i].parent;
15630 /* Step 2 */
15631 for(user = block->use; user; user = user->next) {
15632 struct sdom_block *v, *u;
15633 v = &sd[user->member->vertex];
15634 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15635 if (u->sdom->vertex < sd[i].sdom->vertex) {
15636 sd[i].sdom = u->sdom;
15637 }
15638 }
15639 sdom_block(sd[i].sdom, &sd[i]);
15640 sd[i].ancestor = parent;
15641 /* Step 3 */
15642 for(v = parent->sdominates; v; v = next) {
15643 struct sdom_block *u;
15644 next = v->sdom_next;
15645 unsdom_block(v);
15646 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015647 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015648 u->block : parent->block;
15649 }
15650 }
15651}
15652
Stefan Reinauer14e22772010-04-27 06:56:47 +000015653static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015654 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015655{
15656 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015657 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015658 * for each v <= pred(w) {
15659 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015660 * if (semi[u] < semi[w] {
15661 * semi[w] = semi[u];
15662 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015663 * }
15664 * add w to bucket(vertex(semi[w]));
15665 * LINK(parent(w), w);
15666 *
15667 * // step 3
15668 * for each v <= bucket(parent(w)) {
15669 * delete v from bucket(parent(w));
15670 * u = EVAL(v);
15671 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15672 * }
15673 */
Eric Biederman90089602004-05-28 14:11:54 +000015674 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015675 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015676 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015677 struct block *block;
15678 block = sd[i].block;
15679 parent = sd[i].parent;
15680 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015681 for(edge = block->edges; edge; edge = edge->next) {
15682 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015683 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15684 if (u->sdom->vertex < sd[i].sdom->vertex) {
15685 sd[i].sdom = u->sdom;
15686 }
15687 }
15688 sdom_block(sd[i].sdom, &sd[i]);
15689 sd[i].ancestor = parent;
15690 /* Step 3 */
15691 for(v = parent->sdominates; v; v = next) {
15692 struct sdom_block *u;
15693 next = v->sdom_next;
15694 unsdom_block(v);
15695 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015696 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015697 u->block : parent->block;
15698 }
15699 }
15700}
15701
Stefan Reinauer14e22772010-04-27 06:56:47 +000015702static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015703 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015704{
15705 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015706 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015707 struct block *block;
15708 block = sd[i].block;
15709 if (block->idom->vertex != sd[i].sdom->vertex) {
15710 block->idom = block->idom->idom;
15711 }
15712 idom_block(block->idom, block);
15713 }
15714 sd[1].block->idom = 0;
15715}
15716
Stefan Reinauer14e22772010-04-27 06:56:47 +000015717static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015718 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015719{
15720 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015721 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015722 struct block *block;
15723 block = sd[i].block;
15724 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15725 block->ipdom = block->ipdom->ipdom;
15726 }
15727 ipdom_block(block->ipdom, block);
15728 }
15729 sd[1].block->ipdom = 0;
15730}
15731
15732 /* Theorem 1:
15733 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015734 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015735 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015736 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015737 * v dominates w if and only if v is a proper ancestor of w in
15738 * the dominator tree.
15739 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015740 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015741 * If v and w are vertices of G such that v <= w,
15742 * than any path from v to w must contain a common ancestor
15743 * of v and w in T.
15744 */
15745 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15746 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15747 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15748 /* Theorem 2:
15749 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15750 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15751 */
15752 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015753 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015754 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15755 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15756 */
15757 /* Lemma 5: Let vertices v,w satisfy v -> w.
15758 * Then v -> idom(w) or idom(w) -> idom(v)
15759 */
15760
Eric Biederman90089602004-05-28 14:11:54 +000015761static void find_immediate_dominators(struct compile_state *state,
15762 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015763{
15764 struct sdom_block *sd;
15765 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15766 * vi > w for (1 <= i <= k - 1}
15767 */
15768 /* Theorem 4:
15769 * For any vertex w != r.
15770 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015771 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015772 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15773 */
15774 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015775 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015776 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15777 * Then:
15778 * { sdom(w) if sdom(w) = sdom(u),
15779 * idom(w) = {
15780 * { idom(u) otherwise
15781 */
15782 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015783 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015784 * Number the vertices from 1 to N as they are reached during
15785 * the search. Initialize the variables used in succeeding steps.
15786 * Step 2. Compute the semidominators of all vertices by applying
15787 * theorem 4. Carry out the computation vertex by vertex in
15788 * decreasing order by number.
15789 * Step 3. Implicitly define the immediate dominator of each vertex
15790 * by applying Corollary 1.
15791 * Step 4. Explicitly define the immediate dominator of each vertex,
15792 * carrying out the computation vertex by vertex in increasing order
15793 * by number.
15794 */
15795 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015796 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15797 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015798#if 0
15799 sd[1].size = 0;
15800 sd[1].label = 0;
15801 sd[1].sdom = 0;
15802#endif
15803 /* Step 2 compute the semidominators */
15804 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015805 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015806 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015807 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015808 xfree(sd);
15809}
15810
Eric Biederman90089602004-05-28 14:11:54 +000015811static void find_post_dominators(struct compile_state *state,
15812 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015813{
15814 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015815 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015816 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015817 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015818
Eric Biederman90089602004-05-28 14:11:54 +000015819 vertex = setup_spdblocks(state, bb, sd);
15820 if (vertex != bb->last_vertex) {
15821 internal_error(state, 0, "missing %d blocks",
15822 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015823 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015824
15825 /* Step 2 compute the semidominators */
15826 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015827 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015828 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015829 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015830 xfree(sd);
15831}
15832
15833
15834
15835static void find_block_domf(struct compile_state *state, struct block *block)
15836{
15837 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015838 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015839 if (block->domfrontier != 0) {
15840 internal_error(state, block->first, "domfrontier present?");
15841 }
15842 for(user = block->idominates; user; user = user->next) {
15843 child = user->member;
15844 if (child->idom != block) {
15845 internal_error(state, block->first, "bad idom");
15846 }
15847 find_block_domf(state, child);
15848 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015849 for(edge = block->edges; edge; edge = edge->next) {
15850 if (edge->member->idom != block) {
15851 domf_block(block, edge->member);
15852 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015853 }
15854 for(user = block->idominates; user; user = user->next) {
15855 struct block_set *frontier;
15856 child = user->member;
15857 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15858 if (frontier->member->idom != block) {
15859 domf_block(block, frontier->member);
15860 }
15861 }
15862 }
15863}
15864
15865static void find_block_ipdomf(struct compile_state *state, struct block *block)
15866{
15867 struct block *child;
15868 struct block_set *user;
15869 if (block->ipdomfrontier != 0) {
15870 internal_error(state, block->first, "ipdomfrontier present?");
15871 }
15872 for(user = block->ipdominates; user; user = user->next) {
15873 child = user->member;
15874 if (child->ipdom != block) {
15875 internal_error(state, block->first, "bad ipdom");
15876 }
15877 find_block_ipdomf(state, child);
15878 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015879 for(user = block->use; user; user = user->next) {
15880 if (user->member->ipdom != block) {
15881 ipdomf_block(block, user->member);
15882 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015883 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015884 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015885 struct block_set *frontier;
15886 child = user->member;
15887 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15888 if (frontier->member->ipdom != block) {
15889 ipdomf_block(block, frontier->member);
15890 }
15891 }
15892 }
15893}
15894
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895static void print_dominated(
15896 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015897{
15898 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015899 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015900
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015901 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015902 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015903 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015904 if (user->member->idom != block) {
15905 internal_error(state, user->member->first, "bad idom");
15906 }
15907 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015908 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015909}
15910
Eric Biederman5ade04a2003-10-22 04:03:46 +000015911static void print_dominated2(
15912 struct compile_state *state, FILE *fp, int depth, struct block *block)
15913{
15914 struct block_set *user;
15915 struct triple *ins;
15916 struct occurance *ptr, *ptr2;
15917 const char *filename1, *filename2;
15918 int equal_filenames;
15919 int i;
15920 for(i = 0; i < depth; i++) {
15921 fprintf(fp, " ");
15922 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015923 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015924 block->vertex, block, block->first, block->last);
15925 ins = block->first;
15926 while(ins != block->last && (ins->occurance->line == 0)) {
15927 ins = ins->next;
15928 }
15929 ptr = ins->occurance;
15930 ptr2 = block->last->occurance;
15931 filename1 = ptr->filename? ptr->filename : "";
15932 filename2 = ptr2->filename? ptr2->filename : "";
15933 equal_filenames = (strcmp(filename1, filename2) == 0);
15934 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15935 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15936 } else if (equal_filenames) {
15937 fprintf(fp, " %s:(%d - %d)",
15938 ptr->filename, ptr->line, ptr2->line);
15939 } else {
15940 fprintf(fp, " (%s:%d - %s:%d)",
15941 ptr->filename, ptr->line,
15942 ptr2->filename, ptr2->line);
15943 }
15944 fprintf(fp, "\n");
15945 for(user = block->idominates; user; user = user->next) {
15946 print_dominated2(state, fp, depth + 1, user->member);
15947 }
15948}
15949
Eric Biederman90089602004-05-28 14:11:54 +000015950static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015951{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015952 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015953 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015954 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015955 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015956}
15957
15958
15959static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015960 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015961{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015962 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015963
15964 if (!block || (block->vertex != vertex + 1)) {
15965 return vertex;
15966 }
15967 vertex += 1;
15968
Eric Biederman7dea9552004-06-29 05:38:37 +000015969 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015970 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015971 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015972 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015973 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015974
Eric Biederman5ade04a2003-10-22 04:03:46 +000015975 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015976 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015977 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015978 return vertex;
15979}
Eric Biederman90089602004-05-28 14:11:54 +000015980static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015981 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015982{
Eric Biederman7dea9552004-06-29 05:38:37 +000015983 fprintf(fp, "\ndominance frontiers\n");
15984 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015985
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986}
15987
Eric Biederman90089602004-05-28 14:11:54 +000015988static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015989{
15990 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015991 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015992 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015993 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015994 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015995 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015996 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015997 print_dominance_frontiers(state, state->dbgout, bb);
15998 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015999 }
16000}
16001
16002
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003static void print_ipdominated(
16004 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016005{
16006 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016007 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016008
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016009 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016010 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016011 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016012 if (user->member->ipdom != block) {
16013 internal_error(state, user->member->first, "bad ipdom");
16014 }
16015 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016016 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016017}
16018
Eric Biederman90089602004-05-28 14:11:54 +000016019static void print_ipdominators(struct compile_state *state, FILE *fp,
16020 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016021{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016022 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016023 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016024}
16025
16026static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016027 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016028{
16029 struct block_set *user;
16030
16031 if (!block || (block->vertex != vertex + 1)) {
16032 return vertex;
16033 }
16034 vertex += 1;
16035
Eric Biederman7dea9552004-06-29 05:38:37 +000016036 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016037 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016038 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016039 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016040 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016041 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016042 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016043 }
16044 return vertex;
16045}
Eric Biederman90089602004-05-28 14:11:54 +000016046static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016047 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016048{
Eric Biederman7dea9552004-06-29 05:38:37 +000016049 fprintf(fp, "\nipdominance frontiers\n");
16050 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016051
Eric Biedermanb138ac82003-04-22 18:44:01 +000016052}
16053
Eric Biederman90089602004-05-28 14:11:54 +000016054static void analyze_ipdominators(struct compile_state *state,
16055 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016056{
16057 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016058 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016059 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016060 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016061 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016062 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016063 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016064 print_ipdominance_frontiers(state, state->dbgout, bb);
16065 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016066 }
16067}
16068
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016069static int bdominates(struct compile_state *state,
16070 struct block *dom, struct block *sub)
16071{
16072 while(sub && (sub != dom)) {
16073 sub = sub->idom;
16074 }
16075 return sub == dom;
16076}
16077
16078static int tdominates(struct compile_state *state,
16079 struct triple *dom, struct triple *sub)
16080{
16081 struct block *bdom, *bsub;
16082 int result;
16083 bdom = block_of_triple(state, dom);
16084 bsub = block_of_triple(state, sub);
16085 if (bdom != bsub) {
16086 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016087 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016088 else {
16089 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016090 if (!bdom || !bsub) {
16091 internal_error(state, dom, "huh?");
16092 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016093 ins = sub;
16094 while((ins != bsub->first) && (ins != dom)) {
16095 ins = ins->prev;
16096 }
16097 result = (ins == dom);
16098 }
16099 return result;
16100}
16101
Eric Biederman90089602004-05-28 14:11:54 +000016102static void analyze_basic_blocks(
16103 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016104{
Eric Biederman90089602004-05-28 14:11:54 +000016105 setup_basic_blocks(state, bb);
16106 analyze_idominators(state, bb);
16107 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016108}
16109
Eric Biedermanb138ac82003-04-22 18:44:01 +000016110static void insert_phi_operations(struct compile_state *state)
16111{
16112 size_t size;
16113 struct triple *first;
16114 int *has_already, *work;
16115 struct block *work_list, **work_list_tail;
16116 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016117 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016118
Eric Biederman90089602004-05-28 14:11:54 +000016119 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016120 has_already = xcmalloc(size, "has_already");
16121 work = xcmalloc(size, "work");
16122 iter = 0;
16123
Eric Biederman83b991a2003-10-11 06:20:25 +000016124 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016125 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016126 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016127 struct triple_set *user, *unext;
16128 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016129
16130 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016131 continue;
16132 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016133
Eric Biedermanb138ac82003-04-22 18:44:01 +000016134 iter += 1;
16135 work_list = 0;
16136 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016137 for(user = var->use; user; user = unext) {
16138 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016139 if (MISC(var, 0) == user->member) {
16140 continue;
16141 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016142 if (user->member->op == OP_READ) {
16143 continue;
16144 }
16145 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016146 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016147 "bad variable access");
16148 }
16149 block = user->member->u.block;
16150 if (!block) {
16151 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016152 release_triple(state, user->member);
16153 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016154 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016155 if (work[block->vertex] >= iter) {
16156 continue;
16157 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016158 work[block->vertex] = iter;
16159 *work_list_tail = block;
16160 block->work_next = 0;
16161 work_list_tail = &block->work_next;
16162 }
16163 for(block = work_list; block; block = block->work_next) {
16164 struct block_set *df;
16165 for(df = block->domfrontier; df; df = df->next) {
16166 struct triple *phi;
16167 struct block *front;
16168 int in_edges;
16169 front = df->member;
16170
16171 if (has_already[front->vertex] >= iter) {
16172 continue;
16173 }
16174 /* Count how many edges flow into this block */
16175 in_edges = front->users;
16176 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016177 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016178 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016179 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016180 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016181 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016182 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016183 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016184#if 1
16185 if (phi->rhs != in_edges) {
16186 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16187 phi->rhs, in_edges);
16188 }
16189#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016190 /* Insert the phi functions immediately after the label */
16191 insert_triple(state, front->first->next, phi);
16192 if (front->first == front->last) {
16193 front->last = front->first->next;
16194 }
16195 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016196 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016197
Eric Biedermanb138ac82003-04-22 18:44:01 +000016198 /* If necessary plan to visit the basic block */
16199 if (work[front->vertex] >= iter) {
16200 continue;
16201 }
16202 work[front->vertex] = iter;
16203 *work_list_tail = front;
16204 front->work_next = 0;
16205 work_list_tail = &front->work_next;
16206 }
16207 }
16208 }
16209 xfree(has_already);
16210 xfree(work);
16211}
16212
Eric Biederman66fe2222003-07-04 15:14:04 +000016213
Eric Biederman83b991a2003-10-11 06:20:25 +000016214struct stack {
16215 struct triple_set *top;
16216 unsigned orig_id;
16217};
16218
Eric Biederman90089602004-05-28 14:11:54 +000016219static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016220{
16221 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016222 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016223 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016224 ins = first;
16225 do {
Eric Biederman90089602004-05-28 14:11:54 +000016226 if (triple_is_auto_var(state, ins)) {
16227 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016228 }
16229 ins = ins->next;
16230 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016231 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016232}
16233
Eric Biederman90089602004-05-28 14:11:54 +000016234static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016235{
16236 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016237 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016238 first = state->first;
16239 ins = first;
16240 do {
Eric Biederman90089602004-05-28 14:11:54 +000016241 if (triple_is_auto_var(state, ins)) {
16242 auto_vars += 1;
16243 stacks[auto_vars].orig_id = ins->id;
16244 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016245 }
16246 ins = ins->next;
16247 } while(ins != first);
16248}
16249
Eric Biederman90089602004-05-28 14:11:54 +000016250static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016251{
16252 struct triple *first, *ins;
16253 first = state->first;
16254 ins = first;
16255 do {
Eric Biederman90089602004-05-28 14:11:54 +000016256 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016257 ins->id = stacks[ins->id].orig_id;
16258 }
16259 ins = ins->next;
16260 } while(ins != first);
16261}
16262
16263static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016264{
16265 struct triple_set *head;
16266 struct triple *top_val;
16267 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016268 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016269 if (head) {
16270 top_val = head->member;
16271 }
16272 return top_val;
16273}
16274
Eric Biederman83b991a2003-10-11 06:20:25 +000016275static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016276{
16277 struct triple_set *new;
16278 /* Append new to the head of the list,
16279 * it's the only sensible behavoir for a stack.
16280 */
16281 new = xcmalloc(sizeof(*new), "triple_set");
16282 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016283 new->next = stacks[var->id].top;
16284 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016285}
16286
Eric Biederman83b991a2003-10-11 06:20:25 +000016287static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016288{
16289 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016290 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016291 while(*ptr) {
16292 set = *ptr;
16293 if (set->member == oldval) {
16294 *ptr = set->next;
16295 xfree(set);
16296 /* Only free one occurance from the stack */
16297 return;
16298 }
16299 else {
16300 ptr = &set->next;
16301 }
16302 }
16303}
16304
Eric Biedermanb138ac82003-04-22 18:44:01 +000016305/*
16306 * C(V)
16307 * S(V)
16308 */
16309static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016310 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016311{
16312 struct block_set *set;
16313 struct triple *ptr;
16314 int edge;
16315 if (!parent || !block)
16316 return;
16317 /* Find the edge I am coming in on */
16318 edge = 0;
16319 for(set = block->use; set; set = set->next, edge++) {
16320 if (set->member == parent) {
16321 break;
16322 }
16323 }
16324 if (!set) {
16325 internal_error(state, 0, "phi input is not on a control predecessor");
16326 }
16327 for(ptr = block->first; ; ptr = ptr->next) {
16328 if (ptr->op == OP_PHI) {
16329 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016330 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016331 if (!var) {
16332 internal_error(state, ptr, "no var???");
16333 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016334 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016335 val = peek_triple(stacks, var);
16336 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016337 internal_error(state, val, "bad value in phi");
16338 }
Eric Biederman90089602004-05-28 14:11:54 +000016339 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016340 internal_error(state, ptr, "edges > phi rhs");
16341 }
16342 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016343 if ((*slot != 0) && (*slot != val)) {
16344 internal_error(state, ptr, "phi already bound on this edge");
16345 }
16346 *slot = val;
16347 use_triple(val, ptr);
16348 }
16349 if (ptr == block->last) {
16350 break;
16351 }
16352 }
16353}
16354
16355
16356static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016357 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016358{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016359 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016360 struct triple *ptr, *next, *last;
16361 int done;
16362 if (!block)
16363 return;
16364 last = block->first;
16365 done = 0;
16366 for(ptr = block->first; !done; ptr = next) {
16367 next = ptr->next;
16368 if (ptr == block->last) {
16369 done = 1;
16370 }
16371 /* RHS(A) */
16372 if (ptr->op == OP_READ) {
16373 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016374 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016375 if (!triple_is_auto_var(state, var)) {
16376 internal_error(state, ptr, "read of non auto var!");
16377 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016378 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016379 /* Find the current value of the variable */
16380 val = peek_triple(stacks, var);
16381 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016382 /* Let the optimizer at variables that are not initially
16383 * set. But give it a bogus value so things seem to
16384 * work by accident. This is useful for bitfields because
16385 * setting them always involves a read-modify-write.
16386 */
16387 if (TYPE_ARITHMETIC(ptr->type->type)) {
16388 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16389 val->u.cval = 0xdeadbeaf;
16390 } else {
16391 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16392 }
16393 }
16394 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016395 error(state, ptr, "variable used without being set");
16396 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016397 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16398 internal_error(state, val, "bad value in read");
16399 }
16400 propogate_use(state, ptr, val);
16401 release_triple(state, ptr);
16402 continue;
16403 }
16404 /* LHS(A) */
16405 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016406 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016407 var = MISC(ptr, 0);
16408 if (!triple_is_auto_var(state, var)) {
16409 internal_error(state, ptr, "write to non auto var!");
16410 }
16411 tval = val = RHS(ptr, 0);
16412 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16413 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016414 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016415 }
Eric Biederman90089602004-05-28 14:11:54 +000016416 /* Insert a cast if the types differ */
16417 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016418 if (val->op == OP_INTCONST) {
16419 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16420 tval->u.cval = val->u.cval;
16421 }
16422 else {
Eric Biederman90089602004-05-28 14:11:54 +000016423 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016424 use_triple(val, tval);
16425 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016426 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016427 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016428 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016429 use_triple(tval, ptr);
16430 }
16431 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016432 unuse_triple(var, ptr);
16433 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016434 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016435 }
16436 if (ptr->op == OP_PHI) {
16437 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016438 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016439 if (!triple_is_auto_var(state, var)) {
16440 internal_error(state, ptr, "phi references non auto var!");
16441 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016442 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016443 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016444 }
16445 last = ptr;
16446 }
16447 block->last = last;
16448
16449 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016450 for(edge = block->edges; edge; edge = edge->next) {
16451 fixup_block_phi_variables(state, stacks, block, edge->member);
16452 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016453 /* rename variables in the dominated nodes */
16454 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016455 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016456 }
16457 /* pop the renamed variable stack */
16458 last = block->first;
16459 done = 0;
16460 for(ptr = block->first; !done ; ptr = next) {
16461 next = ptr->next;
16462 if (ptr == block->last) {
16463 done = 1;
16464 }
16465 if (ptr->op == OP_WRITE) {
16466 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +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 Biederman90089602004-05-28 14:11:54 +000016469 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016470 release_triple(state, ptr);
16471 continue;
16472 }
16473 if (ptr->op == OP_PHI) {
16474 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016475 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016476 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016477 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016478 }
16479 last = ptr;
16480 }
16481 block->last = last;
16482}
16483
Eric Biederman83b991a2003-10-11 06:20:25 +000016484static void rename_variables(struct compile_state *state)
16485{
16486 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016487 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016488
16489 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016490 auto_vars = count_auto_vars(state);
16491 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016492
Eric Biederman90089602004-05-28 14:11:54 +000016493 /* Give each auto_var a stack */
16494 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016495
16496 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016497 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016498
Eric Biederman90089602004-05-28 14:11:54 +000016499 /* Remove the stacks from the auto_vars */
16500 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016501 xfree(stacks);
16502}
16503
Eric Biedermanb138ac82003-04-22 18:44:01 +000016504static void prune_block_variables(struct compile_state *state,
16505 struct block *block)
16506{
16507 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016508 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016509 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016510
Eric Biedermanb138ac82003-04-22 18:44:01 +000016511 done = 0;
16512 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016513 /* Be extremely careful I am deleting the list
16514 * as I walk trhough it.
16515 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016516 next = ptr->next;
16517 if (ptr == block->last) {
16518 done = 1;
16519 }
Eric Biederman90089602004-05-28 14:11:54 +000016520 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016521 struct triple_set *user, *next;
16522 for(user = ptr->use; user; user = next) {
16523 struct triple *use;
16524 next = user->next;
16525 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016526 if (MISC(ptr, 0) == user->member) {
16527 continue;
16528 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016529 if (use->op != OP_PHI) {
16530 internal_error(state, use, "decl still used");
16531 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016532 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016533 internal_error(state, use, "bad phi use of decl");
16534 }
16535 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016536 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016537 }
Eric Biederman90089602004-05-28 14:11:54 +000016538 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16539 /* Delete the adecl */
16540 release_triple(state, MISC(ptr, 0));
16541 /* And the piece */
16542 release_triple(state, ptr);
16543 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016544 continue;
16545 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016546 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016547 for(user = block->idominates; user; user = user->next) {
16548 prune_block_variables(state, user->member);
16549 }
16550}
16551
Eric Biederman66fe2222003-07-04 15:14:04 +000016552struct phi_triple {
16553 struct triple *phi;
16554 unsigned orig_id;
16555 int alive;
16556};
16557
16558static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16559{
16560 struct triple **slot;
16561 int zrhs, i;
16562 if (live[phi->id].alive) {
16563 return;
16564 }
16565 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016566 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016567 slot = &RHS(phi, 0);
16568 for(i = 0; i < zrhs; i++) {
16569 struct triple *used;
16570 used = slot[i];
16571 if (used && (used->op == OP_PHI)) {
16572 keep_phi(state, live, used);
16573 }
16574 }
16575}
16576
16577static void prune_unused_phis(struct compile_state *state)
16578{
16579 struct triple *first, *phi;
16580 struct phi_triple *live;
16581 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016582
Eric Biederman66fe2222003-07-04 15:14:04 +000016583 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016584 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016585
16586 /* Count how many phi functions I need to process */
16587 phis = 0;
16588 for(phi = first->next; phi != first; phi = phi->next) {
16589 if (phi->op == OP_PHI) {
16590 phis += 1;
16591 }
16592 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016593
Eric Biederman66fe2222003-07-04 15:14:04 +000016594 /* Mark them all dead */
16595 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16596 phis = 0;
16597 for(phi = first->next; phi != first; phi = phi->next) {
16598 if (phi->op != OP_PHI) {
16599 continue;
16600 }
16601 live[phis].alive = 0;
16602 live[phis].orig_id = phi->id;
16603 live[phis].phi = phi;
16604 phi->id = phis;
16605 phis += 1;
16606 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016607
Eric Biederman66fe2222003-07-04 15:14:04 +000016608 /* Mark phis alive that are used by non phis */
16609 for(i = 0; i < phis; i++) {
16610 struct triple_set *set;
16611 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16612 if (set->member->op != OP_PHI) {
16613 keep_phi(state, live, live[i].phi);
16614 break;
16615 }
16616 }
16617 }
16618
16619 /* Delete the extraneous phis */
16620 for(i = 0; i < phis; i++) {
16621 struct triple **slot;
16622 int zrhs, j;
16623 if (!live[i].alive) {
16624 release_triple(state, live[i].phi);
16625 continue;
16626 }
16627 phi = live[i].phi;
16628 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016629 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016630 for(j = 0; j < zrhs; j++) {
16631 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016632 struct triple *unknown;
16633 get_occurance(phi->occurance);
16634 unknown = flatten(state, state->global_pool,
16635 alloc_triple(state, OP_UNKNOWNVAL,
16636 phi->type, 0, 0, phi->occurance));
16637 slot[j] = unknown;
16638 use_triple(unknown, phi);
16639 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016640#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016641 warning(state, phi, "variable not set at index %d on all paths to use", j);
16642#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016643 }
16644 }
16645 }
16646 xfree(live);
16647}
16648
Eric Biedermanb138ac82003-04-22 18:44:01 +000016649static void transform_to_ssa_form(struct compile_state *state)
16650{
16651 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016652 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016653
Eric Biederman90089602004-05-28 14:11:54 +000016654 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016655 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016656
Eric Biederman90089602004-05-28 14:11:54 +000016657 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016658}
16659
16660
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016661static void clear_vertex(
16662 struct compile_state *state, struct block *block, void *arg)
16663{
Eric Biederman83b991a2003-10-11 06:20:25 +000016664 /* Clear the current blocks vertex and the vertex of all
16665 * of the current blocks neighbors in case there are malformed
16666 * blocks with now instructions at this point.
16667 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016668 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016669 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016670 for(edge = block->edges; edge; edge = edge->next) {
16671 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016672 }
16673 for(user = block->use; user; user = user->next) {
16674 user->member->vertex = 0;
16675 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016676}
16677
16678static void mark_live_block(
16679 struct compile_state *state, struct block *block, int *next_vertex)
16680{
16681 /* See if this is a block that has not been marked */
16682 if (block->vertex != 0) {
16683 return;
16684 }
16685 block->vertex = *next_vertex;
16686 *next_vertex += 1;
16687 if (triple_is_branch(state, block->last)) {
16688 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016689 targ = triple_edge_targ(state, block->last, 0);
16690 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016691 if (!*targ) {
16692 continue;
16693 }
16694 if (!triple_stores_block(state, *targ)) {
16695 internal_error(state, 0, "bad targ");
16696 }
16697 mark_live_block(state, (*targ)->u.block, next_vertex);
16698 }
Eric Biederman90089602004-05-28 14:11:54 +000016699 /* Ensure the last block of a function remains alive */
16700 if (triple_is_call(state, block->last)) {
16701 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16702 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016703 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016704 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016705 struct triple *ins;
16706 ins = block->last->next;
16707 if (!triple_stores_block(state, ins)) {
16708 internal_error(state, 0, "bad block start");
16709 }
16710 mark_live_block(state, ins->u.block, next_vertex);
16711 }
16712}
16713
Eric Biedermanb138ac82003-04-22 18:44:01 +000016714static void transform_from_ssa_form(struct compile_state *state)
16715{
16716 /* To get out of ssa form we insert moves on the incoming
16717 * edges to blocks containting phi functions.
16718 */
16719 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016720 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016721 int next_vertex;
16722
16723 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016724 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016725 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016726 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016727
16728 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016729 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016730 for(phi = first->next; phi != first ; phi = next) {
16731 struct block_set *set;
16732 struct block *block;
16733 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016734 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016735 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016736 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016737 next = phi->next;
16738 if (phi->op != OP_PHI) {
16739 continue;
16740 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016741
Eric Biedermanb138ac82003-04-22 18:44:01 +000016742 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016743 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016744
Eric Biederman83b991a2003-10-11 06:20:25 +000016745 /* If this phi is in a dead block just forget it */
16746 if (block->vertex == 0) {
16747 release_triple(state, phi);
16748 continue;
16749 }
16750
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016751 /* Forget uses from code in dead blocks */
16752 for(use = phi->use; use; use = use_next) {
16753 struct block *ublock;
16754 struct triple **expr;
16755 use_next = use->next;
16756 ublock = block_of_triple(state, use->member);
16757 if ((use->member == phi) || (ublock->vertex != 0)) {
16758 continue;
16759 }
16760 expr = triple_rhs(state, use->member, 0);
16761 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16762 if (*expr == phi) {
16763 *expr = 0;
16764 }
16765 }
16766 unuse_triple(phi, use->member);
16767 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016768 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016769 if (registers_of(state, phi->type) != 1) {
16770 internal_error(state, phi, "phi->type does not fit in a single register!");
16771 }
16772 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16773 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016774
Eric Biederman83b991a2003-10-11 06:20:25 +000016775 /* Replaces use of phi with var */
16776 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016777
Eric Biederman90089602004-05-28 14:11:54 +000016778 /* Count the readers */
16779 readers = 0;
16780 for(use = var->use; use; use = use->next) {
16781 if (use->member != MISC(var, 0)) {
16782 readers++;
16783 }
16784 }
16785
Eric Biedermanb138ac82003-04-22 18:44:01 +000016786 /* Walk all of the incoming edges/blocks and insert moves.
16787 */
Eric Biederman90089602004-05-28 14:11:54 +000016788 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016789 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016790 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016791 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016792 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016793 eblock = set->member;
16794 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016795 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016796 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016797 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016798
Eric Biederman83b991a2003-10-11 06:20:25 +000016799 /* If we don't have a value that belongs in an OP_WRITE
16800 * continue on.
16801 */
Eric Biederman90089602004-05-28 14:11:54 +000016802 if (!val || (val == &unknown_triple) || (val == phi)
16803 || (vblock && (vblock->vertex == 0))) {
16804 continue;
16805 }
16806 /* If the value should never occur error */
16807 if (!vblock) {
16808 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016809 continue;
16810 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016811
16812 /* If the value occurs in a dead block see if a replacement
16813 * block can be found.
16814 */
16815 while(eblock && (eblock->vertex == 0)) {
16816 eblock = eblock->idom;
16817 }
16818 /* If not continue on with the next value. */
16819 if (!eblock || (eblock->vertex == 0)) {
16820 continue;
16821 }
16822
16823 /* If we have an empty incoming block ignore it. */
16824 if (!eblock->first) {
16825 internal_error(state, 0, "empty block?");
16826 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016827
Eric Biederman530b5192003-07-01 10:05:30 +000016828 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016829 /* Walk through the edge block backwards to find an
16830 * appropriate location for the OP_WRITE.
16831 */
16832 for(base = eblock->last; base != eblock->first; base = base->prev) {
16833 struct triple **expr;
16834 if (base->op == OP_PIECE) {
16835 base = MISC(base, 0);
16836 }
16837 if ((base == var) || (base == val)) {
16838 goto out;
16839 }
16840 expr = triple_lhs(state, base, 0);
16841 for(; expr; expr = triple_lhs(state, base, expr)) {
16842 if ((*expr) == val) {
16843 goto out;
16844 }
16845 }
16846 expr = triple_rhs(state, base, 0);
16847 for(; expr; expr = triple_rhs(state, base, expr)) {
16848 if ((*expr) == var) {
16849 goto out;
16850 }
16851 }
Eric Biederman530b5192003-07-01 10:05:30 +000016852 }
Eric Biederman90089602004-05-28 14:11:54 +000016853 out:
16854 if (triple_is_branch(state, base)) {
16855 internal_error(state, base,
16856 "Could not insert write to phi");
16857 }
16858 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016859 use_triple(val, move);
16860 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016861 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016862 }
Eric Biederman90089602004-05-28 14:11:54 +000016863 if (!writers && readers) {
16864 internal_error(state, var, "no value written to in use phi?");
16865 }
16866 /* If var is not used free it */
16867 if (!writers) {
16868 release_triple(state, MISC(var, 0));
16869 release_triple(state, var);
16870 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016871 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016872 release_triple(state, phi);
16873 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016874
Eric Biederman83b991a2003-10-11 06:20:25 +000016875 /* Walk all of the operations to find the adecls */
16876 for(var = first->next; var != first ; var = var->next) {
16877 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016878 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016879 continue;
16880 }
16881
16882 /* Walk through all of the rhs uses of var and
16883 * replace them with read of var.
16884 */
16885 for(use = var->use; use; use = use_next) {
16886 struct triple *read, *user;
16887 struct triple **slot;
16888 int zrhs, i, used;
16889 use_next = use->next;
16890 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016891
Eric Biederman83b991a2003-10-11 06:20:25 +000016892 /* Generate a read of var */
16893 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16894 use_triple(var, read);
16895
16896 /* Find the rhs uses and see if they need to be replaced */
16897 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016898 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016899 slot = &RHS(user, 0);
16900 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016901 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016902 slot[i] = read;
16903 used = 1;
16904 }
16905 }
16906 /* If we did use it cleanup the uses */
16907 if (used) {
16908 unuse_triple(var, user);
16909 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016910 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016911 /* If we didn't use it release the extra triple */
16912 else {
16913 release_triple(state, read);
16914 }
16915 }
16916 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016917}
16918
Eric Biederman5ade04a2003-10-22 04:03:46 +000016919#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016920 FILE *fp = state->dbgout; \
16921 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016922 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016923
Eric Biederman83b991a2003-10-11 06:20:25 +000016924static void rebuild_ssa_form(struct compile_state *state)
16925{
16926HI();
16927 transform_from_ssa_form(state);
16928HI();
Eric Biederman90089602004-05-28 14:11:54 +000016929 state->bb.first = state->first;
16930 free_basic_blocks(state, &state->bb);
16931 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016932HI();
16933 insert_phi_operations(state);
16934HI();
16935 rename_variables(state);
16936HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016937
Eric Biederman90089602004-05-28 14:11:54 +000016938 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016939HI();
16940 prune_unused_phis(state);
16941HI();
16942}
16943#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016944
Stefan Reinauer14e22772010-04-27 06:56:47 +000016945/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016946 * Register conflict resolution
16947 * =========================================================
16948 */
16949
16950static struct reg_info find_def_color(
16951 struct compile_state *state, struct triple *def)
16952{
16953 struct triple_set *set;
16954 struct reg_info info;
16955 info.reg = REG_UNSET;
16956 info.regcm = 0;
16957 if (!triple_is_def(state, def)) {
16958 return info;
16959 }
16960 info = arch_reg_lhs(state, def, 0);
16961 if (info.reg >= MAX_REGISTERS) {
16962 info.reg = REG_UNSET;
16963 }
16964 for(set = def->use; set; set = set->next) {
16965 struct reg_info tinfo;
16966 int i;
16967 i = find_rhs_use(state, set->member, def);
16968 if (i < 0) {
16969 continue;
16970 }
16971 tinfo = arch_reg_rhs(state, set->member, i);
16972 if (tinfo.reg >= MAX_REGISTERS) {
16973 tinfo.reg = REG_UNSET;
16974 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016975 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016976 (info.reg != REG_UNSET) &&
16977 (tinfo.reg != info.reg)) {
16978 internal_error(state, def, "register conflict");
16979 }
16980 if ((info.regcm & tinfo.regcm) == 0) {
16981 internal_error(state, def, "regcm conflict %x & %x == 0",
16982 info.regcm, tinfo.regcm);
16983 }
16984 if (info.reg == REG_UNSET) {
16985 info.reg = tinfo.reg;
16986 }
16987 info.regcm &= tinfo.regcm;
16988 }
16989 if (info.reg >= MAX_REGISTERS) {
16990 internal_error(state, def, "register out of range");
16991 }
16992 return info;
16993}
16994
16995static struct reg_info find_lhs_pre_color(
16996 struct compile_state *state, struct triple *ins, int index)
16997{
16998 struct reg_info info;
16999 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000017000 zrhs = ins->rhs;
17001 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017002 if (!zlhs && triple_is_def(state, ins)) {
17003 zlhs = 1;
17004 }
17005 if (index >= zlhs) {
17006 internal_error(state, ins, "Bad lhs %d", index);
17007 }
17008 info = arch_reg_lhs(state, ins, index);
17009 for(i = 0; i < zrhs; i++) {
17010 struct reg_info rinfo;
17011 rinfo = arch_reg_rhs(state, ins, i);
17012 if ((info.reg == rinfo.reg) &&
17013 (rinfo.reg >= MAX_REGISTERS)) {
17014 struct reg_info tinfo;
17015 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17016 info.reg = tinfo.reg;
17017 info.regcm &= tinfo.regcm;
17018 break;
17019 }
17020 }
17021 if (info.reg >= MAX_REGISTERS) {
17022 info.reg = REG_UNSET;
17023 }
17024 return info;
17025}
17026
17027static struct reg_info find_rhs_post_color(
17028 struct compile_state *state, struct triple *ins, int index);
17029
17030static struct reg_info find_lhs_post_color(
17031 struct compile_state *state, struct triple *ins, int index)
17032{
17033 struct triple_set *set;
17034 struct reg_info info;
17035 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017036#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017037 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017038 ins, index);
17039#endif
17040 if ((index == 0) && triple_is_def(state, ins)) {
17041 lhs = ins;
17042 }
Eric Biederman90089602004-05-28 14:11:54 +000017043 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017044 lhs = LHS(ins, index);
17045 }
17046 else {
17047 internal_error(state, ins, "Bad lhs %d", index);
17048 lhs = 0;
17049 }
17050 info = arch_reg_lhs(state, ins, index);
17051 if (info.reg >= MAX_REGISTERS) {
17052 info.reg = REG_UNSET;
17053 }
17054 for(set = lhs->use; set; set = set->next) {
17055 struct reg_info rinfo;
17056 struct triple *user;
17057 int zrhs, i;
17058 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017059 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017060 for(i = 0; i < zrhs; i++) {
17061 if (RHS(user, i) != lhs) {
17062 continue;
17063 }
17064 rinfo = find_rhs_post_color(state, user, i);
17065 if ((info.reg != REG_UNSET) &&
17066 (rinfo.reg != REG_UNSET) &&
17067 (info.reg != rinfo.reg)) {
17068 internal_error(state, ins, "register conflict");
17069 }
17070 if ((info.regcm & rinfo.regcm) == 0) {
17071 internal_error(state, ins, "regcm conflict %x & %x == 0",
17072 info.regcm, rinfo.regcm);
17073 }
17074 if (info.reg == REG_UNSET) {
17075 info.reg = rinfo.reg;
17076 }
17077 info.regcm &= rinfo.regcm;
17078 }
17079 }
Eric Biederman530b5192003-07-01 10:05:30 +000017080#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017081 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017082 ins, index, info.reg, info.regcm);
17083#endif
17084 return info;
17085}
17086
17087static struct reg_info find_rhs_post_color(
17088 struct compile_state *state, struct triple *ins, int index)
17089{
17090 struct reg_info info, rinfo;
17091 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017092#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017093 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017094 ins, index);
17095#endif
17096 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017097 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017098 if (!zlhs && triple_is_def(state, ins)) {
17099 zlhs = 1;
17100 }
17101 info = rinfo;
17102 if (info.reg >= MAX_REGISTERS) {
17103 info.reg = REG_UNSET;
17104 }
17105 for(i = 0; i < zlhs; i++) {
17106 struct reg_info linfo;
17107 linfo = arch_reg_lhs(state, ins, i);
17108 if ((linfo.reg == rinfo.reg) &&
17109 (linfo.reg >= MAX_REGISTERS)) {
17110 struct reg_info tinfo;
17111 tinfo = find_lhs_post_color(state, ins, i);
17112 if (tinfo.reg >= MAX_REGISTERS) {
17113 tinfo.reg = REG_UNSET;
17114 }
Eric Biederman530b5192003-07-01 10:05:30 +000017115 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017116 info.regcm &= tinfo.regcm;
17117 if (info.reg != REG_UNSET) {
17118 internal_error(state, ins, "register conflict");
17119 }
17120 if (info.regcm == 0) {
17121 internal_error(state, ins, "regcm conflict");
17122 }
17123 info.reg = tinfo.reg;
17124 }
17125 }
Eric Biederman530b5192003-07-01 10:05:30 +000017126#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017127 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017128 ins, index, info.reg, info.regcm);
17129#endif
17130 return info;
17131}
17132
17133static struct reg_info find_lhs_color(
17134 struct compile_state *state, struct triple *ins, int index)
17135{
17136 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017137#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017138 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017139 ins, index);
17140#endif
17141 pre = find_lhs_pre_color(state, ins, index);
17142 post = find_lhs_post_color(state, ins, index);
17143 if ((pre.reg != post.reg) &&
17144 (pre.reg != REG_UNSET) &&
17145 (post.reg != REG_UNSET)) {
17146 internal_error(state, ins, "register conflict");
17147 }
17148 info.regcm = pre.regcm & post.regcm;
17149 info.reg = pre.reg;
17150 if (info.reg == REG_UNSET) {
17151 info.reg = post.reg;
17152 }
Eric Biederman530b5192003-07-01 10:05:30 +000017153#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017154 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017155 ins, index, info.reg, info.regcm,
17156 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017157#endif
17158 return info;
17159}
17160
17161static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17162{
17163 struct triple_set *entry, *next;
17164 struct triple *out;
17165 struct reg_info info, rinfo;
17166
17167 info = arch_reg_lhs(state, ins, 0);
17168 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17169 use_triple(RHS(out, 0), out);
17170 /* Get the users of ins to use out instead */
17171 for(entry = ins->use; entry; entry = next) {
17172 int i;
17173 next = entry->next;
17174 if (entry->member == out) {
17175 continue;
17176 }
17177 i = find_rhs_use(state, entry->member, ins);
17178 if (i < 0) {
17179 continue;
17180 }
17181 rinfo = arch_reg_rhs(state, entry->member, i);
17182 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17183 continue;
17184 }
17185 replace_rhs_use(state, ins, out, entry->member);
17186 }
17187 transform_to_arch_instruction(state, out);
17188 return out;
17189}
17190
Eric Biedermand1ea5392003-06-28 06:49:45 +000017191static struct triple *typed_pre_copy(
17192 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017193{
17194 /* Carefully insert enough operations so that I can
17195 * enter any operation with a GPR32.
17196 */
17197 struct triple *in;
17198 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017199 unsigned classes;
17200 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017201 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017202 if (ins->op == OP_PHI) {
17203 internal_error(state, ins, "pre_copy on a phi?");
17204 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017205 classes = arch_type_to_regcm(state, type);
17206 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017207 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017208 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017209 FILE *fp = state->errout;
17210 fprintf(fp, "src_type: ");
17211 name_of(fp, ins->type);
17212 fprintf(fp, "\ndst_type: ");
17213 name_of(fp, type);
17214 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017215 internal_error(state, ins, "pre_copy with no register classes");
17216 }
Eric Biederman90089602004-05-28 14:11:54 +000017217 op = OP_COPY;
17218 if (!equiv_types(type, (*expr)->type)) {
17219 op = OP_CONVERT;
17220 }
17221 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017222 unuse_triple(*expr, ins);
17223 *expr = in;
17224 use_triple(RHS(in, 0), in);
17225 use_triple(in, ins);
17226 transform_to_arch_instruction(state, in);
17227 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017228
Eric Biedermand1ea5392003-06-28 06:49:45 +000017229}
17230static struct triple *pre_copy(
17231 struct compile_state *state, struct triple *ins, int index)
17232{
17233 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017234}
17235
17236
Eric Biedermanb138ac82003-04-22 18:44:01 +000017237static void insert_copies_to_phi(struct compile_state *state)
17238{
17239 /* To get out of ssa form we insert moves on the incoming
17240 * edges to blocks containting phi functions.
17241 */
17242 struct triple *first;
17243 struct triple *phi;
17244
17245 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017246 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017247 for(phi = first->next; phi != first ; phi = phi->next) {
17248 struct block_set *set;
17249 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017250 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017251 int edge;
17252 if (phi->op != OP_PHI) {
17253 continue;
17254 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017255 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017256 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017257 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017258 /* Phi's that feed into mandatory live range joins
17259 * cause nasty complications. Insert a copy of
17260 * the phi value so I never have to deal with
17261 * that in the rest of the code.
17262 */
17263 copy = post_copy(state, phi);
17264 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017265 /* Walk all of the incoming edges/blocks and insert moves.
17266 */
17267 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17268 struct block *eblock;
17269 struct triple *move;
17270 struct triple *val;
17271 struct triple *ptr;
17272 eblock = set->member;
17273 val = slot[edge];
17274
17275 if (val == phi) {
17276 continue;
17277 }
17278
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017279 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017280 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017281 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017282 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017283 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017284 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017285
Eric Biedermanb138ac82003-04-22 18:44:01 +000017286 slot[edge] = move;
17287 unuse_triple(val, phi);
17288 use_triple(move, phi);
17289
Eric Biederman66fe2222003-07-04 15:14:04 +000017290 /* Walk up the dominator tree until I have found the appropriate block */
17291 while(eblock && !tdominates(state, val, eblock->last)) {
17292 eblock = eblock->idom;
17293 }
17294 if (!eblock) {
17295 internal_error(state, phi, "Cannot find block dominated by %p",
17296 val);
17297 }
17298
Eric Biedermanb138ac82003-04-22 18:44:01 +000017299 /* Walk through the block backwards to find
17300 * an appropriate location for the OP_COPY.
17301 */
17302 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17303 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017304 if (ptr->op == OP_PIECE) {
17305 ptr = MISC(ptr, 0);
17306 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017307 if ((ptr == phi) || (ptr == val)) {
17308 goto out;
17309 }
Eric Biederman90089602004-05-28 14:11:54 +000017310 expr = triple_lhs(state, ptr, 0);
17311 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17312 if ((*expr) == val) {
17313 goto out;
17314 }
17315 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017316 expr = triple_rhs(state, ptr, 0);
17317 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17318 if ((*expr) == phi) {
17319 goto out;
17320 }
17321 }
17322 }
17323 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017324 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017325 internal_error(state, ptr,
17326 "Could not insert write to phi");
17327 }
Eric Biederman90089602004-05-28 14:11:54 +000017328 insert_triple(state, after_lhs(state, ptr), move);
17329 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017330 eblock->last = move;
17331 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017332 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017333 }
17334 }
Eric Biederman90089602004-05-28 14:11:54 +000017335 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017336}
17337
Eric Biederman90089602004-05-28 14:11:54 +000017338struct triple_reg_set;
17339struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017340
Eric Biedermanb138ac82003-04-22 18:44:01 +000017341
Stefan Reinauer14e22772010-04-27 06:56:47 +000017342static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017343 struct triple *member, struct triple *new_member)
17344{
17345 struct triple_reg_set **ptr, *new;
17346 if (!member)
17347 return 0;
17348 ptr = head;
17349 while(*ptr) {
17350 if ((*ptr)->member == member) {
17351 return 0;
17352 }
17353 ptr = &(*ptr)->next;
17354 }
17355 new = xcmalloc(sizeof(*new), "triple_set");
17356 new->member = member;
17357 new->new = new_member;
17358 new->next = *head;
17359 *head = new;
17360 return 1;
17361}
17362
17363static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17364{
17365 struct triple_reg_set *entry, **ptr;
17366 ptr = head;
17367 while(*ptr) {
17368 entry = *ptr;
17369 if (entry->member == member) {
17370 *ptr = entry->next;
17371 xfree(entry);
17372 return;
17373 }
17374 else {
17375 ptr = &entry->next;
17376 }
17377 }
17378}
17379
17380static int in_triple(struct reg_block *rb, struct triple *in)
17381{
17382 return do_triple_set(&rb->in, in, 0);
17383}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017384
17385#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017386static void unin_triple(struct reg_block *rb, struct triple *unin)
17387{
17388 do_triple_unset(&rb->in, unin);
17389}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017390#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017391
17392static int out_triple(struct reg_block *rb, struct triple *out)
17393{
17394 return do_triple_set(&rb->out, out, 0);
17395}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017396#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017397static void unout_triple(struct reg_block *rb, struct triple *unout)
17398{
17399 do_triple_unset(&rb->out, unout);
17400}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017401#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017402
17403static int initialize_regblock(struct reg_block *blocks,
17404 struct block *block, int vertex)
17405{
17406 struct block_set *user;
17407 if (!block || (blocks[block->vertex].block == block)) {
17408 return vertex;
17409 }
17410 vertex += 1;
17411 /* Renumber the blocks in a convinient fashion */
17412 block->vertex = vertex;
17413 blocks[vertex].block = block;
17414 blocks[vertex].vertex = vertex;
17415 for(user = block->use; user; user = user->next) {
17416 vertex = initialize_regblock(blocks, user->member, vertex);
17417 }
17418 return vertex;
17419}
17420
Eric Biederman90089602004-05-28 14:11:54 +000017421static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17422{
17423/* Part to piece is a best attempt and it cannot be correct all by
17424 * itself. If various values are read as different sizes in different
17425 * parts of the code this function cannot work. Or rather it cannot
17426 * work in conjunction with compute_variable_liftimes. As the
17427 * analysis will get confused.
17428 */
17429 struct triple *base;
17430 unsigned reg;
17431 if (!is_lvalue(state, ins)) {
17432 return ins;
17433 }
17434 base = 0;
17435 reg = 0;
17436 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17437 base = MISC(ins, 0);
17438 switch(ins->op) {
17439 case OP_INDEX:
17440 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17441 break;
17442 case OP_DOT:
17443 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17444 break;
17445 default:
17446 internal_error(state, ins, "unhandled part");
17447 break;
17448 }
17449 ins = base;
17450 }
17451 if (base) {
17452 if (reg > base->lhs) {
17453 internal_error(state, base, "part out of range?");
17454 }
17455 ins = LHS(base, reg);
17456 }
17457 return ins;
17458}
17459
Stefan Reinauer14e22772010-04-27 06:56:47 +000017460static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017461 struct triple *ins, struct triple *other)
17462{
17463 if (ins == other) {
17464 return 1;
17465 }
17466 if (ins->op == OP_WRITE) {
17467 ins = part_to_piece(state, MISC(ins, 0));
17468 }
17469 return ins == other;
17470}
17471
Eric Biedermanb138ac82003-04-22 18:44:01 +000017472static int phi_in(struct compile_state *state, struct reg_block *blocks,
17473 struct reg_block *rb, struct block *suc)
17474{
17475 /* Read the conditional input set of a successor block
17476 * (i.e. the input to the phi nodes) and place it in the
17477 * current blocks output set.
17478 */
17479 struct block_set *set;
17480 struct triple *ptr;
17481 int edge;
17482 int done, change;
17483 change = 0;
17484 /* Find the edge I am coming in on */
17485 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17486 if (set->member == rb->block) {
17487 break;
17488 }
17489 }
17490 if (!set) {
17491 internal_error(state, 0, "Not coming on a control edge?");
17492 }
17493 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17494 struct triple **slot, *expr, *ptr2;
17495 int out_change, done2;
17496 done = (ptr == suc->last);
17497 if (ptr->op != OP_PHI) {
17498 continue;
17499 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017500 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017501 expr = slot[edge];
17502 out_change = out_triple(rb, expr);
17503 if (!out_change) {
17504 continue;
17505 }
17506 /* If we don't define the variable also plast it
17507 * in the current blocks input set.
17508 */
17509 ptr2 = rb->block->first;
17510 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017511 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017512 break;
17513 }
17514 done2 = (ptr2 == rb->block->last);
17515 }
17516 if (!done2) {
17517 continue;
17518 }
17519 change |= in_triple(rb, expr);
17520 }
17521 return change;
17522}
17523
17524static int reg_in(struct compile_state *state, struct reg_block *blocks,
17525 struct reg_block *rb, struct block *suc)
17526{
17527 struct triple_reg_set *in_set;
17528 int change;
17529 change = 0;
17530 /* Read the input set of a successor block
17531 * and place it in the current blocks output set.
17532 */
17533 in_set = blocks[suc->vertex].in;
17534 for(; in_set; in_set = in_set->next) {
17535 int out_change, done;
17536 struct triple *first, *last, *ptr;
17537 out_change = out_triple(rb, in_set->member);
17538 if (!out_change) {
17539 continue;
17540 }
17541 /* If we don't define the variable also place it
17542 * in the current blocks input set.
17543 */
17544 first = rb->block->first;
17545 last = rb->block->last;
17546 done = 0;
17547 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017548 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017549 break;
17550 }
17551 done = (ptr == last);
17552 }
17553 if (!done) {
17554 continue;
17555 }
17556 change |= in_triple(rb, in_set->member);
17557 }
17558 change |= phi_in(state, blocks, rb, suc);
17559 return change;
17560}
17561
Eric Biedermanb138ac82003-04-22 18:44:01 +000017562static int use_in(struct compile_state *state, struct reg_block *rb)
17563{
17564 /* Find the variables we use but don't define and add
17565 * it to the current blocks input set.
17566 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017567#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017568#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017569#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017570 struct block *block;
17571 struct triple *ptr;
17572 int done;
17573 int change;
17574 block = rb->block;
17575 change = 0;
17576 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17577 struct triple **expr;
17578 done = (ptr == block->first);
17579 /* The variable a phi function uses depends on the
17580 * control flow, and is handled in phi_in, not
17581 * here.
17582 */
17583 if (ptr->op == OP_PHI) {
17584 continue;
17585 }
17586 expr = triple_rhs(state, ptr, 0);
17587 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17588 struct triple *rhs, *test;
17589 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017590 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017591 if (!rhs) {
17592 continue;
17593 }
Eric Biederman90089602004-05-28 14:11:54 +000017594
17595 /* See if rhs is defined in this block.
17596 * A write counts as a definition.
17597 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017598 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17599 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017600 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017601 rhs = 0;
17602 break;
17603 }
17604 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017605 /* If I still have a valid rhs add it to in */
17606 change |= in_triple(rb, rhs);
17607 }
17608 }
17609 return change;
17610}
17611
17612static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017613 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017614{
17615 struct reg_block *blocks;
17616 int change;
17617 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017618 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17619 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017620 do {
17621 int i;
17622 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017623 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017624 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017625 struct reg_block *rb;
17626 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017627 /* Add the all successor's input set to in */
17628 for(edge = rb->block->edges; edge; edge = edge->next) {
17629 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017630 }
17631 /* Add use to in... */
17632 change |= use_in(state, rb);
17633 }
17634 } while(change);
17635 return blocks;
17636}
17637
Stefan Reinauer14e22772010-04-27 06:56:47 +000017638static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017639 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017640{
17641 int i;
17642 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017643 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017644 struct triple_reg_set *entry, *next;
17645 struct reg_block *rb;
17646 rb = &blocks[i];
17647 for(entry = rb->in; entry ; entry = next) {
17648 next = entry->next;
17649 do_triple_unset(&rb->in, entry->member);
17650 }
17651 for(entry = rb->out; entry; entry = next) {
17652 next = entry->next;
17653 do_triple_unset(&rb->out, entry->member);
17654 }
17655 }
17656 xfree(blocks);
17657
17658}
17659
Eric Biedermanf96a8102003-06-16 16:57:34 +000017660typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017661 struct compile_state *state,
17662 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017663 struct reg_block *rb, struct triple *ins, void *arg);
17664
17665static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017666 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017667 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017668{
17669 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017670
Eric Biederman90089602004-05-28 14:11:54 +000017671 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017672 struct triple_reg_set *live;
17673 struct triple_reg_set *entry, *next;
17674 struct triple *ptr, *prev;
17675 struct reg_block *rb;
17676 struct block *block;
17677 int done;
17678
17679 /* Get the blocks */
17680 rb = &blocks[i];
17681 block = rb->block;
17682
17683 /* Copy out into live */
17684 live = 0;
17685 for(entry = rb->out; entry; entry = next) {
17686 next = entry->next;
17687 do_triple_set(&live, entry->member, entry->new);
17688 }
17689 /* Walk through the basic block calculating live */
17690 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017691 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017692
17693 prev = ptr->prev;
17694 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017695
17696 /* Ensure the current definition is in live */
17697 if (triple_is_def(state, ptr)) {
17698 do_triple_set(&live, ptr, 0);
17699 }
17700
17701 /* Inform the callback function of what is
17702 * going on.
17703 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017704 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017705
Eric Biedermanb138ac82003-04-22 18:44:01 +000017706 /* Remove the current definition from live */
17707 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017708
Eric Biedermanb138ac82003-04-22 18:44:01 +000017709 /* Add the current uses to live.
17710 *
17711 * It is safe to skip phi functions because they do
17712 * not have any block local uses, and the block
17713 * output sets already properly account for what
17714 * control flow depedent uses phi functions do have.
17715 */
17716 if (ptr->op == OP_PHI) {
17717 continue;
17718 }
17719 expr = triple_rhs(state, ptr, 0);
17720 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17721 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017722 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017723 continue;
17724 }
17725 do_triple_set(&live, *expr, 0);
17726 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017727 }
17728 /* Free live */
17729 for(entry = live; entry; entry = next) {
17730 next = entry->next;
17731 do_triple_unset(&live, entry->member);
17732 }
17733 }
17734}
17735
Eric Biederman90089602004-05-28 14:11:54 +000017736struct print_live_variable_info {
17737 struct reg_block *rb;
17738 FILE *fp;
17739};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017740#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017741static void print_live_variables_block(
17742 struct compile_state *state, struct block *block, void *arg)
17743
17744{
17745 struct print_live_variable_info *info = arg;
17746 struct block_set *edge;
17747 FILE *fp = info->fp;
17748 struct reg_block *rb;
17749 struct triple *ptr;
17750 int phi_present;
17751 int done;
17752 rb = &info->rb[block->vertex];
17753
17754 fprintf(fp, "\nblock: %p (%d),",
17755 block, block->vertex);
17756 for(edge = block->edges; edge; edge = edge->next) {
17757 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017758 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017759 edge->member && edge->member->use?edge->member->use->member : 0);
17760 }
17761 fprintf(fp, "\n");
17762 if (rb->in) {
17763 struct triple_reg_set *in_set;
17764 fprintf(fp, " in:");
17765 for(in_set = rb->in; in_set; in_set = in_set->next) {
17766 fprintf(fp, " %-10p", in_set->member);
17767 }
17768 fprintf(fp, "\n");
17769 }
17770 phi_present = 0;
17771 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17772 done = (ptr == block->last);
17773 if (ptr->op == OP_PHI) {
17774 phi_present = 1;
17775 break;
17776 }
17777 }
17778 if (phi_present) {
17779 int edge;
17780 for(edge = 0; edge < block->users; edge++) {
17781 fprintf(fp, " in(%d):", edge);
17782 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17783 struct triple **slot;
17784 done = (ptr == block->last);
17785 if (ptr->op != OP_PHI) {
17786 continue;
17787 }
17788 slot = &RHS(ptr, 0);
17789 fprintf(fp, " %-10p", slot[edge]);
17790 }
17791 fprintf(fp, "\n");
17792 }
17793 }
17794 if (block->first->op == OP_LABEL) {
17795 fprintf(fp, "%p:\n", block->first);
17796 }
17797 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17798 done = (ptr == block->last);
17799 display_triple(fp, ptr);
17800 }
17801 if (rb->out) {
17802 struct triple_reg_set *out_set;
17803 fprintf(fp, " out:");
17804 for(out_set = rb->out; out_set; out_set = out_set->next) {
17805 fprintf(fp, " %-10p", out_set->member);
17806 }
17807 fprintf(fp, "\n");
17808 }
17809 fprintf(fp, "\n");
17810}
17811
Stefan Reinauer14e22772010-04-27 06:56:47 +000017812static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017813 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17814{
17815 struct print_live_variable_info info;
17816 info.rb = rb;
17817 info.fp = fp;
17818 fprintf(fp, "\nlive variables by block\n");
17819 walk_blocks(state, bb, print_live_variables_block, &info);
17820
17821}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017822#endif
Eric Biederman90089602004-05-28 14:11:54 +000017823
Eric Biedermanb138ac82003-04-22 18:44:01 +000017824static int count_triples(struct compile_state *state)
17825{
17826 struct triple *first, *ins;
17827 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017828 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017829 ins = first;
17830 do {
17831 triples++;
17832 ins = ins->next;
17833 } while (ins != first);
17834 return triples;
17835}
Eric Biederman66fe2222003-07-04 15:14:04 +000017836
17837
Eric Biedermanb138ac82003-04-22 18:44:01 +000017838struct dead_triple {
17839 struct triple *triple;
17840 struct dead_triple *work_next;
17841 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017842 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017843 int flags;
17844#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017845#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017846};
17847
Stefan Reinauer14e22772010-04-27 06:56:47 +000017848static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017849 struct dead_triple *dtriple)
17850{
17851 struct triple *first, *ins;
17852 struct dead_triple *dt;
17853 FILE *fp;
17854 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17855 return;
17856 }
17857 fp = state->dbgout;
17858 fprintf(fp, "--------------- dtriples ---------------\n");
17859 first = state->first;
17860 ins = first;
17861 do {
17862 dt = &dtriple[ins->id];
17863 if ((ins->op == OP_LABEL) && (ins->use)) {
17864 fprintf(fp, "\n%p:\n", ins);
17865 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017866 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017867 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17868 display_triple(fp, ins);
17869 if (triple_is_branch(state, ins)) {
17870 fprintf(fp, "\n");
17871 }
17872 ins = ins->next;
17873 } while(ins != first);
17874 fprintf(fp, "\n");
17875}
17876
Eric Biedermanb138ac82003-04-22 18:44:01 +000017877
17878static void awaken(
17879 struct compile_state *state,
17880 struct dead_triple *dtriple, struct triple **expr,
17881 struct dead_triple ***work_list_tail)
17882{
17883 struct triple *triple;
17884 struct dead_triple *dt;
17885 if (!expr) {
17886 return;
17887 }
17888 triple = *expr;
17889 if (!triple) {
17890 return;
17891 }
17892 if (triple->id <= 0) {
17893 internal_error(state, triple, "bad triple id: %d",
17894 triple->id);
17895 }
17896 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017897 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017898 return;
17899 }
17900 dt = &dtriple[triple->id];
17901 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17902 dt->flags |= TRIPLE_FLAG_ALIVE;
17903 if (!dt->work_next) {
17904 **work_list_tail = dt;
17905 *work_list_tail = &dt->work_next;
17906 }
17907 }
17908}
17909
17910static void eliminate_inefectual_code(struct compile_state *state)
17911{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017912 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17913 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017914 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017915
Eric Biederman5ade04a2003-10-22 04:03:46 +000017916 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17917 return;
17918 }
17919
Eric Biedermanb138ac82003-04-22 18:44:01 +000017920 /* Setup the work list */
17921 work_list = 0;
17922 work_list_tail = &work_list;
17923
Eric Biederman83b991a2003-10-11 06:20:25 +000017924 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017925
17926 /* Count how many triples I have */
17927 triples = count_triples(state);
17928
17929 /* Now put then in an array and mark all of the triples dead */
17930 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017931
Eric Biedermanb138ac82003-04-22 18:44:01 +000017932 ins = first;
17933 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017934 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017935 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017936 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017937 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017938 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017939 ins->id = i;
17940 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017941 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017942 awaken(state, dtriple, &ins, &work_list_tail);
17943 }
17944 i++;
17945 ins = ins->next;
17946 } while(ins != first);
17947 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017948 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017949 struct dead_triple *dt;
17950 struct block_set *user;
17951 struct triple **expr;
17952 dt = work_list;
17953 work_list = dt->work_next;
17954 if (!work_list) {
17955 work_list_tail = &work_list;
17956 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017957 /* Make certain the block the current instruction is in lives */
17958 block = block_of_triple(state, dt->triple);
17959 awaken(state, dtriple, &block->first, &work_list_tail);
17960 if (triple_is_branch(state, block->last)) {
17961 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017962 } else {
17963 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017964 }
17965
Eric Biedermanb138ac82003-04-22 18:44:01 +000017966 /* Wake up the data depencencies of this triple */
17967 expr = 0;
17968 do {
17969 expr = triple_rhs(state, dt->triple, expr);
17970 awaken(state, dtriple, expr, &work_list_tail);
17971 } while(expr);
17972 do {
17973 expr = triple_lhs(state, dt->triple, expr);
17974 awaken(state, dtriple, expr, &work_list_tail);
17975 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017976 do {
17977 expr = triple_misc(state, dt->triple, expr);
17978 awaken(state, dtriple, expr, &work_list_tail);
17979 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017980 /* Wake up the forward control dependencies */
17981 do {
17982 expr = triple_targ(state, dt->triple, expr);
17983 awaken(state, dtriple, expr, &work_list_tail);
17984 } while(expr);
17985 /* Wake up the reverse control dependencies of this triple */
17986 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017987 struct triple *last;
17988 last = user->member->last;
17989 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017990#if DEBUG_ROMCC_WARNINGS
17991#warning "Should we bring the awakening noops back?"
17992#endif
17993 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017994 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017995 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017996 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017997 }
17998 }
Eric Biederman90089602004-05-28 14:11:54 +000017999 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018000 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018001 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000018002 (dt->flags & TRIPLE_FLAG_ALIVE)) {
18003 internal_error(state, dt->triple, "noop effective?");
18004 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018005 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018006 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018007 release_triple(state, dt->triple);
18008 }
18009 }
18010 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018011
18012 rebuild_ssa_form(state);
18013
Eric Biederman90089602004-05-28 14:11:54 +000018014 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018015}
18016
18017
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018018static void insert_mandatory_copies(struct compile_state *state)
18019{
18020 struct triple *ins, *first;
18021
18022 /* The object is with a minimum of inserted copies,
18023 * to resolve in fundamental register conflicts between
18024 * register value producers and consumers.
18025 * Theoretically we may be greater than minimal when we
18026 * are inserting copies before instructions but that
18027 * case should be rare.
18028 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018029 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018030 ins = first;
18031 do {
18032 struct triple_set *entry, *next;
18033 struct triple *tmp;
18034 struct reg_info info;
18035 unsigned reg, regcm;
18036 int do_post_copy, do_pre_copy;
18037 tmp = 0;
18038 if (!triple_is_def(state, ins)) {
18039 goto next;
18040 }
18041 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018042 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018043 if (info.reg >= MAX_REGISTERS) {
18044 info.reg = REG_UNSET;
18045 }
Eric Biederman90089602004-05-28 14:11:54 +000018046
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018047 reg = REG_UNSET;
18048 regcm = arch_type_to_regcm(state, ins->type);
18049 do_post_copy = do_pre_copy = 0;
18050
18051 /* Walk through the uses of ins and check for conflicts */
18052 for(entry = ins->use; entry; entry = next) {
18053 struct reg_info rinfo;
18054 int i;
18055 next = entry->next;
18056 i = find_rhs_use(state, entry->member, ins);
18057 if (i < 0) {
18058 continue;
18059 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018060
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018061 /* Find the users color requirements */
18062 rinfo = arch_reg_rhs(state, entry->member, i);
18063 if (rinfo.reg >= MAX_REGISTERS) {
18064 rinfo.reg = REG_UNSET;
18065 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018066
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018067 /* See if I need a pre_copy */
18068 if (rinfo.reg != REG_UNSET) {
18069 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18070 do_pre_copy = 1;
18071 }
18072 reg = rinfo.reg;
18073 }
18074 regcm &= rinfo.regcm;
18075 regcm = arch_regcm_normalize(state, regcm);
18076 if (regcm == 0) {
18077 do_pre_copy = 1;
18078 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018079 /* Always use pre_copies for constants.
18080 * They do not take up any registers until a
18081 * copy places them in one.
18082 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018083 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018084 (rinfo.reg != REG_UNNEEDED)) {
18085 do_pre_copy = 1;
18086 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018087 }
18088 do_post_copy =
18089 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018090 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018091 (reg != REG_UNSET) &&
18092 (info.reg != reg)) ||
18093 ((info.regcm & regcm) == 0));
18094
18095 reg = info.reg;
18096 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018097 /* 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 +000018098 for(entry = ins->use; entry; entry = next) {
18099 struct reg_info rinfo;
18100 int i;
18101 next = entry->next;
18102 i = find_rhs_use(state, entry->member, ins);
18103 if (i < 0) {
18104 continue;
18105 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018106
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018107 /* Find the users color requirements */
18108 rinfo = arch_reg_rhs(state, entry->member, i);
18109 if (rinfo.reg >= MAX_REGISTERS) {
18110 rinfo.reg = REG_UNSET;
18111 }
18112
18113 /* Now see if it is time to do the pre_copy */
18114 if (rinfo.reg != REG_UNSET) {
18115 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18116 ((regcm & rinfo.regcm) == 0) ||
18117 /* Don't let a mandatory coalesce sneak
18118 * into a operation that is marked to prevent
18119 * coalescing.
18120 */
18121 ((reg != REG_UNNEEDED) &&
18122 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18123 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18124 ) {
18125 if (do_pre_copy) {
18126 struct triple *user;
18127 user = entry->member;
18128 if (RHS(user, i) != ins) {
18129 internal_error(state, user, "bad rhs");
18130 }
18131 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018132 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018133 continue;
18134 } else {
18135 do_post_copy = 1;
18136 }
18137 }
18138 reg = rinfo.reg;
18139 }
18140 if ((regcm & rinfo.regcm) == 0) {
18141 if (do_pre_copy) {
18142 struct triple *user;
18143 user = entry->member;
18144 if (RHS(user, i) != ins) {
18145 internal_error(state, user, "bad rhs");
18146 }
18147 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018148 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018149 continue;
18150 } else {
18151 do_post_copy = 1;
18152 }
18153 }
18154 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018155
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018156 }
18157 if (do_post_copy) {
18158 struct reg_info pre, post;
18159 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018160 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018161 pre = arch_reg_lhs(state, ins, 0);
18162 post = arch_reg_lhs(state, tmp, 0);
18163 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18164 internal_error(state, tmp, "useless copy");
18165 }
18166 }
18167 next:
18168 ins = ins->next;
18169 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018170
Eric Biederman90089602004-05-28 14:11:54 +000018171 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018172}
18173
18174
Eric Biedermanb138ac82003-04-22 18:44:01 +000018175struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018176struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018177struct live_range {
18178 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018179 struct live_range_def *defs;
18180/* Note. The list pointed to by defs is kept in order.
18181 * That is baring splits in the flow control
18182 * defs dominates defs->next wich dominates defs->next->next
18183 * etc.
18184 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018185 unsigned color;
18186 unsigned classes;
18187 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018188 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018189 struct live_range *group_next, **group_prev;
18190};
18191
18192struct live_range_edge {
18193 struct live_range_edge *next;
18194 struct live_range *node;
18195};
18196
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018197struct live_range_def {
18198 struct live_range_def *next;
18199 struct live_range_def *prev;
18200 struct live_range *lr;
18201 struct triple *def;
18202 unsigned orig_id;
18203};
18204
Eric Biedermanb138ac82003-04-22 18:44:01 +000018205#define LRE_HASH_SIZE 2048
18206struct lre_hash {
18207 struct lre_hash *next;
18208 struct live_range *left;
18209 struct live_range *right;
18210};
18211
18212
18213struct reg_state {
18214 struct lre_hash *hash[LRE_HASH_SIZE];
18215 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018216 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018217 struct live_range *lr;
18218 struct live_range *low, **low_tail;
18219 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018220 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018221 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018222 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018223};
18224
18225
Eric Biedermand1ea5392003-06-28 06:49:45 +000018226struct print_interference_block_info {
18227 struct reg_state *rstate;
18228 FILE *fp;
18229 int need_edges;
18230};
18231static void print_interference_block(
18232 struct compile_state *state, struct block *block, void *arg)
18233
18234{
18235 struct print_interference_block_info *info = arg;
18236 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018237 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018238 FILE *fp = info->fp;
18239 struct reg_block *rb;
18240 struct triple *ptr;
18241 int phi_present;
18242 int done;
18243 rb = &rstate->blocks[block->vertex];
18244
Eric Biederman5ade04a2003-10-22 04:03:46 +000018245 fprintf(fp, "\nblock: %p (%d),",
18246 block, block->vertex);
18247 for(edge = block->edges; edge; edge = edge->next) {
18248 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018249 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018250 edge->member && edge->member->use?edge->member->use->member : 0);
18251 }
18252 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018253 if (rb->in) {
18254 struct triple_reg_set *in_set;
18255 fprintf(fp, " in:");
18256 for(in_set = rb->in; in_set; in_set = in_set->next) {
18257 fprintf(fp, " %-10p", in_set->member);
18258 }
18259 fprintf(fp, "\n");
18260 }
18261 phi_present = 0;
18262 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18263 done = (ptr == block->last);
18264 if (ptr->op == OP_PHI) {
18265 phi_present = 1;
18266 break;
18267 }
18268 }
18269 if (phi_present) {
18270 int edge;
18271 for(edge = 0; edge < block->users; edge++) {
18272 fprintf(fp, " in(%d):", edge);
18273 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18274 struct triple **slot;
18275 done = (ptr == block->last);
18276 if (ptr->op != OP_PHI) {
18277 continue;
18278 }
18279 slot = &RHS(ptr, 0);
18280 fprintf(fp, " %-10p", slot[edge]);
18281 }
18282 fprintf(fp, "\n");
18283 }
18284 }
18285 if (block->first->op == OP_LABEL) {
18286 fprintf(fp, "%p:\n", block->first);
18287 }
18288 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018289 struct live_range *lr;
18290 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018291 done = (ptr == block->last);
18292 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018293
Eric Biedermand1ea5392003-06-28 06:49:45 +000018294 id = ptr->id;
18295 ptr->id = rstate->lrd[id].orig_id;
18296 SET_REG(ptr->id, lr->color);
18297 display_triple(fp, ptr);
18298 ptr->id = id;
18299
18300 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18301 internal_error(state, ptr, "lr has no defs!");
18302 }
18303 if (info->need_edges) {
18304 if (lr->defs) {
18305 struct live_range_def *lrd;
18306 fprintf(fp, " range:");
18307 lrd = lr->defs;
18308 do {
18309 fprintf(fp, " %-10p", lrd->def);
18310 lrd = lrd->next;
18311 } while(lrd != lr->defs);
18312 fprintf(fp, "\n");
18313 }
18314 if (lr->edges > 0) {
18315 struct live_range_edge *edge;
18316 fprintf(fp, " edges:");
18317 for(edge = lr->edges; edge; edge = edge->next) {
18318 struct live_range_def *lrd;
18319 lrd = edge->node->defs;
18320 do {
18321 fprintf(fp, " %-10p", lrd->def);
18322 lrd = lrd->next;
18323 } while(lrd != edge->node->defs);
18324 fprintf(fp, "|");
18325 }
18326 fprintf(fp, "\n");
18327 }
18328 }
18329 /* Do a bunch of sanity checks */
18330 valid_ins(state, ptr);
18331 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18332 internal_error(state, ptr, "Invalid triple id: %d",
18333 ptr->id);
18334 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018335 }
18336 if (rb->out) {
18337 struct triple_reg_set *out_set;
18338 fprintf(fp, " out:");
18339 for(out_set = rb->out; out_set; out_set = out_set->next) {
18340 fprintf(fp, " %-10p", out_set->member);
18341 }
18342 fprintf(fp, "\n");
18343 }
18344 fprintf(fp, "\n");
18345}
18346
18347static void print_interference_blocks(
18348 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18349{
18350 struct print_interference_block_info info;
18351 info.rstate = rstate;
18352 info.fp = fp;
18353 info.need_edges = need_edges;
18354 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018355 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018356
18357}
18358
Eric Biedermanb138ac82003-04-22 18:44:01 +000018359static unsigned regc_max_size(struct compile_state *state, int classes)
18360{
18361 unsigned max_size;
18362 int i;
18363 max_size = 0;
18364 for(i = 0; i < MAX_REGC; i++) {
18365 if (classes & (1 << i)) {
18366 unsigned size;
18367 size = arch_regc_size(state, i);
18368 if (size > max_size) {
18369 max_size = size;
18370 }
18371 }
18372 }
18373 return max_size;
18374}
18375
18376static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18377{
18378 unsigned equivs[MAX_REG_EQUIVS];
18379 int i;
18380 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18381 internal_error(state, 0, "invalid register");
18382 }
18383 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18384 internal_error(state, 0, "invalid register");
18385 }
18386 arch_reg_equivs(state, equivs, reg1);
18387 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18388 if (equivs[i] == reg2) {
18389 return 1;
18390 }
18391 }
18392 return 0;
18393}
18394
18395static void reg_fill_used(struct compile_state *state, char *used, int reg)
18396{
18397 unsigned equivs[MAX_REG_EQUIVS];
18398 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018399 if (reg == REG_UNNEEDED) {
18400 return;
18401 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018402 arch_reg_equivs(state, equivs, reg);
18403 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18404 used[equivs[i]] = 1;
18405 }
18406 return;
18407}
18408
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018409static void reg_inc_used(struct compile_state *state, char *used, int reg)
18410{
18411 unsigned equivs[MAX_REG_EQUIVS];
18412 int i;
18413 if (reg == REG_UNNEEDED) {
18414 return;
18415 }
18416 arch_reg_equivs(state, equivs, reg);
18417 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18418 used[equivs[i]] += 1;
18419 }
18420 return;
18421}
18422
Eric Biedermanb138ac82003-04-22 18:44:01 +000018423static unsigned int hash_live_edge(
18424 struct live_range *left, struct live_range *right)
18425{
18426 unsigned int hash, val;
18427 unsigned long lval, rval;
18428 lval = ((unsigned long)left)/sizeof(struct live_range);
18429 rval = ((unsigned long)right)/sizeof(struct live_range);
18430 hash = 0;
18431 while(lval) {
18432 val = lval & 0xff;
18433 lval >>= 8;
18434 hash = (hash *263) + val;
18435 }
18436 while(rval) {
18437 val = rval & 0xff;
18438 rval >>= 8;
18439 hash = (hash *263) + val;
18440 }
18441 hash = hash & (LRE_HASH_SIZE - 1);
18442 return hash;
18443}
18444
18445static struct lre_hash **lre_probe(struct reg_state *rstate,
18446 struct live_range *left, struct live_range *right)
18447{
18448 struct lre_hash **ptr;
18449 unsigned int index;
18450 /* Ensure left <= right */
18451 if (left > right) {
18452 struct live_range *tmp;
18453 tmp = left;
18454 left = right;
18455 right = tmp;
18456 }
18457 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018458
Eric Biedermanb138ac82003-04-22 18:44:01 +000018459 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018460 while(*ptr) {
18461 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18462 break;
18463 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018464 ptr = &(*ptr)->next;
18465 }
18466 return ptr;
18467}
18468
18469static int interfere(struct reg_state *rstate,
18470 struct live_range *left, struct live_range *right)
18471{
18472 struct lre_hash **ptr;
18473 ptr = lre_probe(rstate, left, right);
18474 return ptr && *ptr;
18475}
18476
Stefan Reinauer14e22772010-04-27 06:56:47 +000018477static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018478 struct live_range *left, struct live_range *right)
18479{
18480 /* FIXME the memory allocation overhead is noticeable here... */
18481 struct lre_hash **ptr, *new_hash;
18482 struct live_range_edge *edge;
18483
18484 if (left == right) {
18485 return;
18486 }
18487 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18488 return;
18489 }
18490 /* Ensure left <= right */
18491 if (left > right) {
18492 struct live_range *tmp;
18493 tmp = left;
18494 left = right;
18495 right = tmp;
18496 }
18497 ptr = lre_probe(rstate, left, right);
18498 if (*ptr) {
18499 return;
18500 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018501#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018502 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018503 left, right);
18504#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018505 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18506 new_hash->next = *ptr;
18507 new_hash->left = left;
18508 new_hash->right = right;
18509 *ptr = new_hash;
18510
18511 edge = xmalloc(sizeof(*edge), "live_range_edge");
18512 edge->next = left->edges;
18513 edge->node = right;
18514 left->edges = edge;
18515 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018516
Eric Biedermanb138ac82003-04-22 18:44:01 +000018517 edge = xmalloc(sizeof(*edge), "live_range_edge");
18518 edge->next = right->edges;
18519 edge->node = left;
18520 right->edges = edge;
18521 right->degree += 1;
18522}
18523
18524static void remove_live_edge(struct reg_state *rstate,
18525 struct live_range *left, struct live_range *right)
18526{
18527 struct live_range_edge *edge, **ptr;
18528 struct lre_hash **hptr, *entry;
18529 hptr = lre_probe(rstate, left, right);
18530 if (!hptr || !*hptr) {
18531 return;
18532 }
18533 entry = *hptr;
18534 *hptr = entry->next;
18535 xfree(entry);
18536
18537 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18538 edge = *ptr;
18539 if (edge->node == right) {
18540 *ptr = edge->next;
18541 memset(edge, 0, sizeof(*edge));
18542 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018543 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018544 break;
18545 }
18546 }
18547 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18548 edge = *ptr;
18549 if (edge->node == left) {
18550 *ptr = edge->next;
18551 memset(edge, 0, sizeof(*edge));
18552 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018553 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018554 break;
18555 }
18556 }
18557}
18558
18559static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18560{
18561 struct live_range_edge *edge, *next;
18562 for(edge = range->edges; edge; edge = next) {
18563 next = edge->next;
18564 remove_live_edge(rstate, range, edge->node);
18565 }
18566}
18567
Stefan Reinauer14e22772010-04-27 06:56:47 +000018568static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018569 struct live_range *dest, struct live_range *src)
18570{
18571 struct live_range_edge *edge, *next;
18572 for(edge = src->edges; edge; edge = next) {
18573 struct live_range *other;
18574 next = edge->next;
18575 other = edge->node;
18576 remove_live_edge(rstate, src, other);
18577 add_live_edge(rstate, dest, other);
18578 }
18579}
18580
Eric Biedermanb138ac82003-04-22 18:44:01 +000018581
18582/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018583 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018584 * new(n) --- Return a graph with n nodes but no edges.
18585 * add(g,x,y) --- Return a graph including g with an between x and y
18586 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18587 * x and y in the graph g
18588 * degree(g, x) --- Return the degree of the node x in the graph g
18589 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18590 *
18591 * Implement with a hash table && a set of adjcency vectors.
18592 * The hash table supports constant time implementations of add and interfere.
18593 * The adjacency vectors support an efficient implementation of neighbors.
18594 */
18595
Stefan Reinauer14e22772010-04-27 06:56:47 +000018596/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018597 * +---------------------------------------------------+
18598 * | +--------------+ |
18599 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018600 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018601 *
18602 * -- In simplify implment optimistic coloring... (No backtracking)
18603 * -- Implement Rematerialization it is the only form of spilling we can perform
18604 * Essentially this means dropping a constant from a register because
18605 * we can regenerate it later.
18606 *
18607 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18608 * coalesce at phi points...
18609 * --- Bias coloring if at all possible do the coalesing a compile time.
18610 *
18611 *
18612 */
18613
Stefan Reinauer50542a82007-10-24 11:14:14 +000018614#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018615static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018616 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018617 struct triple *parent, struct triple *ins)
18618{
18619 struct live_range *lr;
18620 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018621 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018622 expr = triple_rhs(state, ins, 0);
18623 for(;expr; expr = triple_rhs(state, ins, expr)) {
18624 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018625 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018626 continue;
18627 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018628 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018629 if (lr->color == lr2->color) {
18630 internal_error(state, ins, "live range too big");
18631 }
18632 }
18633}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018634#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018635
18636static struct live_range *coalesce_ranges(
18637 struct compile_state *state, struct reg_state *rstate,
18638 struct live_range *lr1, struct live_range *lr2)
18639{
18640 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18641 unsigned color;
18642 unsigned classes;
18643 if (lr1 == lr2) {
18644 return lr1;
18645 }
18646 if (!lr1->defs || !lr2->defs) {
18647 internal_error(state, 0,
18648 "cannot coalese dead live ranges");
18649 }
18650 if ((lr1->color == REG_UNNEEDED) ||
18651 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018652 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018653 "cannot coalesce live ranges without a possible color");
18654 }
18655 if ((lr1->color != lr2->color) &&
18656 (lr1->color != REG_UNSET) &&
18657 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018658 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018659 "cannot coalesce live ranges of different colors");
18660 }
18661 color = lr1->color;
18662 if (color == REG_UNSET) {
18663 color = lr2->color;
18664 }
18665 classes = lr1->classes & lr2->classes;
18666 if (!classes) {
18667 internal_error(state, lr1->defs->def,
18668 "cannot coalesce live ranges with dissimilar register classes");
18669 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018670 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018671 FILE *fp = state->errout;
18672 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018673 lrd = lr1->defs;
18674 do {
Eric Biederman90089602004-05-28 14:11:54 +000018675 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018676 lrd = lrd->next;
18677 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018678 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018679 lrd = lr2->defs;
18680 do {
Eric Biederman90089602004-05-28 14:11:54 +000018681 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018682 lrd = lrd->next;
18683 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018684 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018685 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018686 /* If there is a clear dominate live range put it in lr1,
18687 * For purposes of this test phi functions are
18688 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018689 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018690 */
18691 if ((lr1->defs->prev->def->op == OP_PHI) ||
18692 ((lr2->defs->prev->def->op != OP_PHI) &&
18693 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18694 struct live_range *tmp;
18695 tmp = lr1;
18696 lr1 = lr2;
18697 lr2 = tmp;
18698 }
18699#if 0
18700 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018701 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018702 }
18703 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018704 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018705 }
18706 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018707 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018708 }
18709 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018710 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018711 }
18712#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018713#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018714 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018715 lr1->defs->def,
18716 lr1->color,
18717 lr2->defs->def,
18718 lr2->color);
18719#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018720
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018721 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018722#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018723#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018724#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018725 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018726 * and to the necessity of making multiple passes of coalesce_live_ranges.
18727 * A failure to find some coalesce opportunities in coaleace_live_ranges
18728 * does not impact the correct of the compiler just the efficiency with
18729 * which registers are allocated.
18730 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018731 head = lr1->defs;
18732 mid1 = lr1->defs->prev;
18733 mid2 = lr2->defs;
18734 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018735
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018736 head->prev = end;
18737 end->next = head;
18738
18739 mid1->next = mid2;
18740 mid2->prev = mid1;
18741
18742 /* Fixup the live range in the added live range defs */
18743 lrd = head;
18744 do {
18745 lrd->lr = lr1;
18746 lrd = lrd->next;
18747 } while(lrd != head);
18748
18749 /* Mark lr2 as free. */
18750 lr2->defs = 0;
18751 lr2->color = REG_UNNEEDED;
18752 lr2->classes = 0;
18753
18754 if (!lr1->defs) {
18755 internal_error(state, 0, "lr1->defs == 0 ?");
18756 }
18757
18758 lr1->color = color;
18759 lr1->classes = classes;
18760
Eric Biederman153ea352003-06-20 14:43:20 +000018761 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18762 transfer_live_edges(rstate, lr1, lr2);
18763
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018764 return lr1;
18765}
18766
18767static struct live_range_def *live_range_head(
18768 struct compile_state *state, struct live_range *lr,
18769 struct live_range_def *last)
18770{
18771 struct live_range_def *result;
18772 result = 0;
18773 if (last == 0) {
18774 result = lr->defs;
18775 }
18776 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18777 result = last->next;
18778 }
18779 return result;
18780}
18781
18782static struct live_range_def *live_range_end(
18783 struct compile_state *state, struct live_range *lr,
18784 struct live_range_def *last)
18785{
18786 struct live_range_def *result;
18787 result = 0;
18788 if (last == 0) {
18789 result = lr->defs->prev;
18790 }
18791 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18792 result = last->prev;
18793 }
18794 return result;
18795}
18796
18797
Eric Biedermanb138ac82003-04-22 18:44:01 +000018798static void initialize_live_ranges(
18799 struct compile_state *state, struct reg_state *rstate)
18800{
18801 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018802 size_t count, size;
18803 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018804
Eric Biederman83b991a2003-10-11 06:20:25 +000018805 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018806 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018807 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018808 count = count_triples(state);
18809 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018810 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018811 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018812 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018813 /* Potentially I need one live range for each instruction
18814 * plus an extra for the dummy live range.
18815 */
18816 rstate->ranges = count + 1;
18817 size = sizeof(rstate->lrd[0]) * rstate->defs;
18818 rstate->lrd = xcmalloc(size, "live_range_def");
18819 size = sizeof(rstate->lr[0]) * rstate->ranges;
18820 rstate->lr = xcmalloc(size, "live_range");
18821
Eric Biedermanb138ac82003-04-22 18:44:01 +000018822 /* Setup the dummy live range */
18823 rstate->lr[0].classes = 0;
18824 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018825 rstate->lr[0].defs = 0;
18826 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018827 ins = first;
18828 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018829 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018830 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018831 struct reg_info info;
18832 /* Find the architecture specific color information */
18833 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018834 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018835 rstate->lr[i].defs = &rstate->lrd[j];
18836 rstate->lr[i].color = info.reg;
18837 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018838 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018839 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018840 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018841 /* Otherwise give the triple the dummy live range. */
18842 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018843 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018844 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018845
18846 /* Initalize the live_range_def */
18847 rstate->lrd[j].next = &rstate->lrd[j];
18848 rstate->lrd[j].prev = &rstate->lrd[j];
18849 rstate->lrd[j].def = ins;
18850 rstate->lrd[j].orig_id = ins->id;
18851 ins->id = j;
18852
18853 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018854 ins = ins->next;
18855 } while(ins != first);
18856 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018857
Eric Biedermanb138ac82003-04-22 18:44:01 +000018858 /* Make a second pass to handle achitecture specific register
18859 * constraints.
18860 */
18861 ins = first;
18862 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018863 int zlhs, zrhs, i, j;
18864 if (ins->id > rstate->defs) {
18865 internal_error(state, ins, "bad id");
18866 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018867
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018868 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018869 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018870 if ((zlhs == 0) && triple_is_def(state, ins)) {
18871 zlhs = 1;
18872 }
Eric Biederman90089602004-05-28 14:11:54 +000018873 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018874
Eric Biederman5ade04a2003-10-22 04:03:46 +000018875 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018876 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018877 ins, zlhs, zrhs);
18878 }
18879
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018880 for(i = 0; i < zlhs; i++) {
18881 struct reg_info linfo;
18882 struct live_range_def *lhs;
18883 linfo = arch_reg_lhs(state, ins, i);
18884 if (linfo.reg < MAX_REGISTERS) {
18885 continue;
18886 }
18887 if (triple_is_def(state, ins)) {
18888 lhs = &rstate->lrd[ins->id];
18889 } else {
18890 lhs = &rstate->lrd[LHS(ins, i)->id];
18891 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018892
18893 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018894 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018895 i, lhs, linfo.reg);
18896 }
18897
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018898 for(j = 0; j < zrhs; j++) {
18899 struct reg_info rinfo;
18900 struct live_range_def *rhs;
18901 rinfo = arch_reg_rhs(state, ins, j);
18902 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018903 continue;
18904 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018905 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018906
18907 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018908 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018909 j, rhs, rinfo.reg);
18910 }
18911
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018912 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018913 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018914 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018915 }
18916 }
18917 }
18918 ins = ins->next;
18919 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018920}
18921
Eric Biedermanf96a8102003-06-16 16:57:34 +000018922static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018923 struct compile_state *state,
18924 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018925 struct reg_block *rb, struct triple *ins, void *arg)
18926{
18927 struct reg_state *rstate = arg;
18928 struct live_range *def;
18929 struct triple_reg_set *entry;
18930
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018931 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018932 * we do not have a definition to add to
18933 * the interference graph.
18934 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018935 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018936 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018937 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018938 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018939
Eric Biedermanb138ac82003-04-22 18:44:01 +000018940 /* Create an edge between ins and everything that is
18941 * alive, unless the live_range cannot share
18942 * a physical register with ins.
18943 */
18944 for(entry = live; entry; entry = entry->next) {
18945 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018946 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18947 internal_error(state, 0, "bad entry?");
18948 }
18949 lr = rstate->lrd[entry->member->id].lr;
18950 if (def == lr) {
18951 continue;
18952 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018953 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18954 continue;
18955 }
18956 add_live_edge(rstate, def, lr);
18957 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018958 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018959}
18960
Stefan Reinauer50542a82007-10-24 11:14:14 +000018961#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018962static struct live_range *get_verify_live_range(
18963 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18964{
18965 struct live_range *lr;
18966 struct live_range_def *lrd;
18967 int ins_found;
18968 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18969 internal_error(state, ins, "bad ins?");
18970 }
18971 lr = rstate->lrd[ins->id].lr;
18972 ins_found = 0;
18973 lrd = lr->defs;
18974 do {
18975 if (lrd->def == ins) {
18976 ins_found = 1;
18977 }
18978 lrd = lrd->next;
18979 } while(lrd != lr->defs);
18980 if (!ins_found) {
18981 internal_error(state, ins, "ins not in live range");
18982 }
18983 return lr;
18984}
18985
18986static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018987 struct compile_state *state,
18988 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018989 struct reg_block *rb, struct triple *ins, void *arg)
18990{
18991 struct reg_state *rstate = arg;
18992 struct triple_reg_set *entry1, *entry2;
18993
18994
18995 /* Compare live against edges and make certain the code is working */
18996 for(entry1 = live; entry1; entry1 = entry1->next) {
18997 struct live_range *lr1;
18998 lr1 = get_verify_live_range(state, rstate, entry1->member);
18999 for(entry2 = live; entry2; entry2 = entry2->next) {
19000 struct live_range *lr2;
19001 struct live_range_edge *edge2;
19002 int lr1_found;
19003 int lr2_degree;
19004 if (entry2 == entry1) {
19005 continue;
19006 }
19007 lr2 = get_verify_live_range(state, rstate, entry2->member);
19008 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019009 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019010 "live range with 2 values simultaneously alive");
19011 }
19012 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19013 continue;
19014 }
19015 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019016 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019017 "edges don't interfere?");
19018 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019019
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019020 lr1_found = 0;
19021 lr2_degree = 0;
19022 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19023 lr2_degree++;
19024 if (edge2->node == lr1) {
19025 lr1_found = 1;
19026 }
19027 }
19028 if (lr2_degree != lr2->degree) {
19029 internal_error(state, entry2->member,
19030 "computed degree: %d does not match reported degree: %d\n",
19031 lr2_degree, lr2->degree);
19032 }
19033 if (!lr1_found) {
19034 internal_error(state, entry2->member, "missing edge");
19035 }
19036 }
19037 }
19038 return;
19039}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019040#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019041
Eric Biedermanf96a8102003-06-16 16:57:34 +000019042static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019043 struct compile_state *state,
19044 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019045 struct reg_block *rb, struct triple *ins, void *arg)
19046{
19047 struct reg_state *rstate = arg;
19048 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019049 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019050 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019051
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019052 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019053 id = ins->id;
19054 ins->id = rstate->lrd[id].orig_id;
19055 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019056 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019057 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019058
19059 if (lr->defs) {
19060 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019061 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019062 lrd = lr->defs;
19063 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019064 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019065 lrd = lrd->next;
19066 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019067 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019068 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019069 if (live) {
19070 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019071 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019072 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019073 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019074 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019075 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019076 }
19077 if (lr->edges) {
19078 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019079 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019080 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019081 struct live_range_def *lrd;
19082 lrd = entry->node->defs;
19083 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019084 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019085 lrd = lrd->next;
19086 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019087 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019088 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019089 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019090 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019091 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019092 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019093 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019094 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019095}
19096
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019097static int coalesce_live_ranges(
19098 struct compile_state *state, struct reg_state *rstate)
19099{
19100 /* At the point where a value is moved from one
19101 * register to another that value requires two
19102 * registers, thus increasing register pressure.
19103 * Live range coaleescing reduces the register
19104 * pressure by keeping a value in one register
19105 * longer.
19106 *
19107 * In the case of a phi function all paths leading
19108 * into it must be allocated to the same register
19109 * otherwise the phi function may not be removed.
19110 *
19111 * Forcing a value to stay in a single register
19112 * for an extended period of time does have
19113 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019114 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019115 *
19116 * The two cases I have identified are:
19117 * 1) Two forced register assignments may
19118 * collide.
19119 * 2) Registers may go unused because they
19120 * are only good for storing the value
19121 * and not manipulating it.
19122 *
19123 * Because of this I need to split live ranges,
19124 * even outside of the context of coalesced live
19125 * ranges. The need to split live ranges does
19126 * impose some constraints on live range coalescing.
19127 *
19128 * - Live ranges may not be coalesced across phi
19129 * functions. This creates a 2 headed live
19130 * range that cannot be sanely split.
19131 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019132 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019133 * are handled as pre split live ranges so we will
19134 * never attempt to split them.
19135 */
19136 int coalesced;
19137 int i;
19138
19139 coalesced = 0;
19140 for(i = 0; i <= rstate->ranges; i++) {
19141 struct live_range *lr1;
19142 struct live_range_def *lrd1;
19143 lr1 = &rstate->lr[i];
19144 if (!lr1->defs) {
19145 continue;
19146 }
19147 lrd1 = live_range_end(state, lr1, 0);
19148 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19149 struct triple_set *set;
19150 if (lrd1->def->op != OP_COPY) {
19151 continue;
19152 }
19153 /* Skip copies that are the result of a live range split. */
19154 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19155 continue;
19156 }
19157 for(set = lrd1->def->use; set; set = set->next) {
19158 struct live_range_def *lrd2;
19159 struct live_range *lr2, *res;
19160
19161 lrd2 = &rstate->lrd[set->member->id];
19162
19163 /* Don't coalesce with instructions
19164 * that are the result of a live range
19165 * split.
19166 */
19167 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19168 continue;
19169 }
19170 lr2 = rstate->lrd[set->member->id].lr;
19171 if (lr1 == lr2) {
19172 continue;
19173 }
19174 if ((lr1->color != lr2->color) &&
19175 (lr1->color != REG_UNSET) &&
19176 (lr2->color != REG_UNSET)) {
19177 continue;
19178 }
19179 if ((lr1->classes & lr2->classes) == 0) {
19180 continue;
19181 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019182
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019183 if (interfere(rstate, lr1, lr2)) {
19184 continue;
19185 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019186
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019187 res = coalesce_ranges(state, rstate, lr1, lr2);
19188 coalesced += 1;
19189 if (res != lr1) {
19190 goto next;
19191 }
19192 }
19193 }
19194 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019195 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019196 }
19197 return coalesced;
19198}
19199
19200
Eric Biedermanf96a8102003-06-16 16:57:34 +000019201static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019202 struct reg_block *blocks, struct triple_reg_set *live,
19203 struct reg_block *rb, struct triple *ins, void *arg)
19204{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019205 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019206 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019207
19208 /* See if we have a mandatory coalesce operation between
19209 * a lhs and a rhs value. If so and the rhs value is also
19210 * alive then this triple needs to be pre copied. Otherwise
19211 * we would have two definitions in the same live range simultaneously
19212 * alive.
19213 */
Eric Biederman90089602004-05-28 14:11:54 +000019214 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019215 if ((zlhs == 0) && triple_is_def(state, ins)) {
19216 zlhs = 1;
19217 }
Eric Biederman90089602004-05-28 14:11:54 +000019218 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019219 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019220 struct reg_info linfo;
19221 linfo = arch_reg_lhs(state, ins, i);
19222 if (linfo.reg < MAX_REGISTERS) {
19223 continue;
19224 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019225 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019226 struct reg_info rinfo;
19227 struct triple *rhs;
19228 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019229 int found;
19230 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019231 rinfo = arch_reg_rhs(state, ins, j);
19232 if (rinfo.reg != linfo.reg) {
19233 continue;
19234 }
19235 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019236 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019237 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019238 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019239 }
19240 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019241 if (found) {
19242 struct triple *copy;
19243 copy = pre_copy(state, ins, j);
19244 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019245 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019246 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019247 }
19248 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019249 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019250}
19251
Eric Biedermand1ea5392003-06-28 06:49:45 +000019252static int correct_coalesce_conflicts(
19253 struct compile_state *state, struct reg_block *blocks)
19254{
19255 int conflicts;
19256 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019257 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019258 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019259 return conflicts;
19260}
19261
Eric Biedermanf96a8102003-06-16 16:57:34 +000019262static void replace_set_use(struct compile_state *state,
19263 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019264{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019265 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019266 for(set = head; set; set = set->next) {
19267 if (set->member == orig) {
19268 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019269 }
19270 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019271}
19272
Stefan Reinauer14e22772010-04-27 06:56:47 +000019273static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019274 struct reg_block *blocks, struct triple *orig, struct triple *new)
19275{
19276 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019277#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019278#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019279#endif
Eric Biederman90089602004-05-28 14:11:54 +000019280 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019281 struct reg_block *rb;
19282 rb = &blocks[i];
19283 replace_set_use(state, rb->in, orig, new);
19284 replace_set_use(state, rb->out, orig, new);
19285 }
19286}
19287
19288static void color_instructions(struct compile_state *state)
19289{
19290 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019291 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019292 ins = first;
19293 do {
19294 if (triple_is_def(state, ins)) {
19295 struct reg_info info;
19296 info = find_lhs_color(state, ins, 0);
19297 if (info.reg >= MAX_REGISTERS) {
19298 info.reg = REG_UNSET;
19299 }
19300 SET_INFO(ins->id, info);
19301 }
19302 ins = ins->next;
19303 } while(ins != first);
19304}
19305
19306static struct reg_info read_lhs_color(
19307 struct compile_state *state, struct triple *ins, int index)
19308{
19309 struct reg_info info;
19310 if ((index == 0) && triple_is_def(state, ins)) {
19311 info.reg = ID_REG(ins->id);
19312 info.regcm = ID_REGCM(ins->id);
19313 }
Eric Biederman90089602004-05-28 14:11:54 +000019314 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019315 info = read_lhs_color(state, LHS(ins, index), 0);
19316 }
19317 else {
19318 internal_error(state, ins, "Bad lhs %d", index);
19319 info.reg = REG_UNSET;
19320 info.regcm = 0;
19321 }
19322 return info;
19323}
19324
19325static struct triple *resolve_tangle(
19326 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019327{
19328 struct reg_info info, uinfo;
19329 struct triple_set *set, *next;
19330 struct triple *copy;
19331
Stefan Reinauer50542a82007-10-24 11:14:14 +000019332#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019333#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019334#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019335 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019336 for(set = tangle->use; set; set = next) {
19337 struct triple *user;
19338 int i, zrhs;
19339 next = set->next;
19340 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019341 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019342 for(i = 0; i < zrhs; i++) {
19343 if (RHS(user, i) != tangle) {
19344 continue;
19345 }
19346 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019347 if (uinfo.reg == info.reg) {
19348 copy = pre_copy(state, user, i);
19349 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019350 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019351 }
19352 }
19353 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019354 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019355 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019356 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019357 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019358 copy = post_copy(state, tangle);
19359 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019360 linfo = find_lhs_color(state, copy, 0);
19361 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019362 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019363 info = find_lhs_color(state, tangle, 0);
19364 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019365
Eric Biedermanf96a8102003-06-16 16:57:34 +000019366 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019367}
19368
19369
Eric Biedermanf96a8102003-06-16 16:57:34 +000019370static void fix_tangles(struct compile_state *state,
19371 struct reg_block *blocks, struct triple_reg_set *live,
19372 struct reg_block *rb, struct triple *ins, void *arg)
19373{
Eric Biederman153ea352003-06-20 14:43:20 +000019374 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019375 struct triple *tangle;
19376 do {
19377 char used[MAX_REGISTERS];
19378 struct triple_reg_set *set;
19379 tangle = 0;
19380
19381 /* Find out which registers have multiple uses at this point */
19382 memset(used, 0, sizeof(used));
19383 for(set = live; set; set = set->next) {
19384 struct reg_info info;
19385 info = read_lhs_color(state, set->member, 0);
19386 if (info.reg == REG_UNSET) {
19387 continue;
19388 }
19389 reg_inc_used(state, used, info.reg);
19390 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019391
Eric Biedermanf96a8102003-06-16 16:57:34 +000019392 /* Now find the least dominated definition of a register in
19393 * conflict I have seen so far.
19394 */
19395 for(set = live; set; set = set->next) {
19396 struct reg_info info;
19397 info = read_lhs_color(state, set->member, 0);
19398 if (used[info.reg] < 2) {
19399 continue;
19400 }
Eric Biederman153ea352003-06-20 14:43:20 +000019401 /* Changing copies that feed into phi functions
19402 * is incorrect.
19403 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019404 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019405 (set->member->use->member->op == OP_PHI)) {
19406 continue;
19407 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019408 if (!tangle || tdominates(state, set->member, tangle)) {
19409 tangle = set->member;
19410 }
19411 }
19412 /* If I have found a tangle resolve it */
19413 if (tangle) {
19414 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019415 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019416 post_copy = resolve_tangle(state, tangle);
19417 if (post_copy) {
19418 replace_block_use(state, blocks, tangle, post_copy);
19419 }
19420 if (post_copy && (tangle != ins)) {
19421 replace_set_use(state, live, tangle, post_copy);
19422 }
19423 }
19424 } while(tangle);
19425 return;
19426}
19427
Eric Biederman153ea352003-06-20 14:43:20 +000019428static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019429 struct compile_state *state, struct reg_block *blocks)
19430{
Eric Biederman153ea352003-06-20 14:43:20 +000019431 int tangles;
19432 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019433 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019434 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019435 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019436 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019437}
19438
Eric Biedermand1ea5392003-06-28 06:49:45 +000019439
19440static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19441static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19442
19443struct triple *find_constrained_def(
19444 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019445{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019446 struct live_range_def *lrd, *lrd_next;
19447 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019448 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019449 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019450 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019451
19452 lrd = lrd_next;
19453 lrd_next = lrd->next;
19454
Eric Biedermand1ea5392003-06-28 06:49:45 +000019455 regcm = arch_type_to_regcm(state, lrd->def->type);
19456 info = find_lhs_color(state, lrd->def, 0);
19457 regcm = arch_regcm_reg_normalize(state, regcm);
19458 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019459 /* If the 2 register class masks are equal then
19460 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019461 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019462 if (regcm == info.regcm) {
19463 continue;
19464 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019465
Eric Biederman5ade04a2003-10-22 04:03:46 +000019466 /* If there is just one use.
19467 * That use cannot accept a larger register class.
19468 * There are no intervening definitions except
19469 * definitions that feed into that use.
19470 * Then a triple is not constrained.
19471 * FIXME handle this case!
19472 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019473#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019474#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019475#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019476
Eric Biederman5ade04a2003-10-22 04:03:46 +000019477
Eric Biedermand1ea5392003-06-28 06:49:45 +000019478 /* Of the constrained live ranges deal with the
19479 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019480 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019481 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019482 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019483 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019484 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019485 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019486 tdominates(state, lrd->def, constrained))
19487 {
19488 constrained = lrd->def;
19489 }
19490 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019491 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019492}
19493
Eric Biedermand1ea5392003-06-28 06:49:45 +000019494static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019495 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019496 struct live_range *range)
19497{
19498 /* Walk through the edges in conflict and our current live
19499 * range, and find definitions that are more severly constrained
19500 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019501 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019502 * Then pick one of those ranges and relax the constraints.
19503 */
19504 struct live_range_edge *edge;
19505 struct triple *constrained;
19506
19507 constrained = 0;
19508 for(edge = range->edges; edge; edge = edge->next) {
19509 constrained = find_constrained_def(state, edge->node, constrained);
19510 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019511#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019512#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019513#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019514 if (!constrained) {
19515 constrained = find_constrained_def(state, range, constrained);
19516 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019517
19518 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019519 fprintf(state->errout, "constrained: ");
19520 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019521 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019522 if (constrained) {
19523 ids_from_rstate(state, rstate);
19524 cleanup_rstate(state, rstate);
19525 resolve_tangle(state, constrained);
19526 }
19527 return !!constrained;
19528}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019529
Eric Biedermand1ea5392003-06-28 06:49:45 +000019530static int split_ranges(
19531 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019532 char *used, struct live_range *range)
19533{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019534 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019535 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019536 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019537 rstate->passes, tops(range->defs->def->op), range->defs->def);
19538 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019539 if ((range->color == REG_UNNEEDED) ||
19540 (rstate->passes >= rstate->max_passes)) {
19541 return 0;
19542 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019543 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019544
Eric Biedermand1ea5392003-06-28 06:49:45 +000019545 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019546 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019547 * (a) allow me to spill a register or
19548 * (b) allow me to place a value in another register.
19549 *
19550 * So far I don't have a test case for this, the resolving
19551 * of mandatory constraints has solved all of my
19552 * know issues. So I have choosen not to write any
19553 * code until I cat get a better feel for cases where
19554 * it would be useful to have.
19555 *
19556 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019557#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019558#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019559#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019560
Eric Biederman5ade04a2003-10-22 04:03:46 +000019561 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019562 FILE *fp = state->errout;
19563 print_interference_blocks(state, rstate, fp, 0);
19564 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019565 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019566 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019567}
19568
Eric Biederman5ade04a2003-10-22 04:03:46 +000019569static FILE *cgdebug_fp(struct compile_state *state)
19570{
19571 FILE *fp;
19572 fp = 0;
19573 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019574 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019575 }
19576 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019577 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019578 }
19579 return fp;
19580}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019581
Eric Biederman5ade04a2003-10-22 04:03:46 +000019582static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19583{
19584 FILE *fp;
19585 fp = cgdebug_fp(state);
19586 if (fp) {
19587 va_list args;
19588 va_start(args, fmt);
19589 vfprintf(fp, fmt, args);
19590 va_end(args);
19591 }
19592}
19593
19594static void cgdebug_flush(struct compile_state *state)
19595{
19596 FILE *fp;
19597 fp = cgdebug_fp(state);
19598 if (fp) {
19599 fflush(fp);
19600 }
19601}
19602
19603static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19604{
19605 FILE *fp;
19606 fp = cgdebug_fp(state);
19607 if (fp) {
19608 loc(fp, state, ins);
19609 }
19610}
19611
Stefan Reinauer14e22772010-04-27 06:56:47 +000019612static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019613 struct reg_state *rstate, struct live_range *range)
19614{
19615 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019616 struct live_range_def *lrd;
19617 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019618 struct live_range_edge *edge;
19619 char used[MAX_REGISTERS];
19620 struct triple **expr;
19621
Eric Biedermanb138ac82003-04-22 18:44:01 +000019622 /* Instead of doing just the trivial color select here I try
19623 * a few extra things because a good color selection will help reduce
19624 * copies.
19625 */
19626
19627 /* Find the registers currently in use */
19628 memset(used, 0, sizeof(used));
19629 for(edge = range->edges; edge; edge = edge->next) {
19630 if (edge->node->color == REG_UNSET) {
19631 continue;
19632 }
19633 reg_fill_used(state, used, edge->node->color);
19634 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019635
19636 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019637 int i;
19638 i = 0;
19639 for(edge = range->edges; edge; edge = edge->next) {
19640 i++;
19641 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019642 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019643 tops(range->defs->def->op), i);
19644 cgdebug_loc(state, range->defs->def);
19645 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019646 for(i = 0; i < MAX_REGISTERS; i++) {
19647 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019648 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019649 arch_reg_str(i));
19650 }
19651 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019652 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019653
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019654 /* If a color is already assigned see if it will work */
19655 if (range->color != REG_UNSET) {
19656 struct live_range_def *lrd;
19657 if (!used[range->color]) {
19658 return 1;
19659 }
19660 for(edge = range->edges; edge; edge = edge->next) {
19661 if (edge->node->color != range->color) {
19662 continue;
19663 }
19664 warning(state, edge->node->defs->def, "edge: ");
19665 lrd = edge->node->defs;
19666 do {
19667 warning(state, lrd->def, " %p %s",
19668 lrd->def, tops(lrd->def->op));
19669 lrd = lrd->next;
19670 } while(lrd != edge->node->defs);
19671 }
19672 lrd = range->defs;
19673 warning(state, range->defs->def, "def: ");
19674 do {
19675 warning(state, lrd->def, " %p %s",
19676 lrd->def, tops(lrd->def->op));
19677 lrd = lrd->next;
19678 } while(lrd != range->defs);
19679 internal_error(state, range->defs->def,
19680 "live range with already used color %s",
19681 arch_reg_str(range->color));
19682 }
19683
Eric Biedermanb138ac82003-04-22 18:44:01 +000019684 /* If I feed into an expression reuse it's color.
19685 * This should help remove copies in the case of 2 register instructions
19686 * and phi functions.
19687 */
19688 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019689 lrd = live_range_end(state, range, 0);
19690 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19691 entry = lrd->def->use;
19692 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19693 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019694 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019695 insd = &rstate->lrd[entry->member->id];
19696 if (insd->lr->defs == 0) {
19697 continue;
19698 }
19699 if (!phi && (insd->def->op == OP_PHI) &&
19700 !interfere(rstate, range, insd->lr)) {
19701 phi = insd;
19702 }
Eric Biederman530b5192003-07-01 10:05:30 +000019703 if (insd->lr->color == REG_UNSET) {
19704 continue;
19705 }
19706 regcm = insd->lr->classes;
19707 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019708 (used[insd->lr->color])) {
19709 continue;
19710 }
19711 if (interfere(rstate, range, insd->lr)) {
19712 continue;
19713 }
19714 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019715 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019716 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019717 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019718 * of something else that feeds into the phi function.
19719 */
19720 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019721 if (phi->lr->color != REG_UNSET) {
19722 if (used[phi->lr->color]) {
19723 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019724 }
19725 }
19726 else {
19727 expr = triple_rhs(state, phi->def, 0);
19728 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19729 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019730 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019731 if (!*expr) {
19732 continue;
19733 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019734 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019735 if (lr->color == REG_UNSET) {
19736 continue;
19737 }
19738 regcm = lr->classes;
19739 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019740 (used[lr->color])) {
19741 continue;
19742 }
19743 if (interfere(rstate, range, lr)) {
19744 continue;
19745 }
19746 range->color = lr->color;
19747 }
19748 }
19749 }
19750 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019751 lrd = live_range_head(state, range, 0);
19752 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19753 expr = triple_rhs(state, lrd->def, 0);
19754 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019755 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019756 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019757 if (!*expr) {
19758 continue;
19759 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019760 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019761 if (lr->color == REG_UNSET) {
19762 continue;
19763 }
19764 regcm = lr->classes;
19765 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019766 (used[lr->color])) {
19767 continue;
19768 }
19769 if (interfere(rstate, range, lr)) {
19770 continue;
19771 }
19772 range->color = lr->color;
19773 break;
19774 }
19775 }
19776 /* If I have not opportunitically picked a useful color
19777 * pick the first color that is free.
19778 */
19779 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019780 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019781 arch_select_free_register(state, used, range->classes);
19782 }
19783 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019784 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019785 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019786 if (split_ranges(state, rstate, used, range)) {
19787 return 0;
19788 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019789 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019790 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019791 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019792 lrd = edge->node->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 != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019798 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019799 warning(state, range->defs->def, "range: ");
19800 lrd = range->defs;
19801 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019802 warning(state, lrd->def, " %s %p",
19803 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019804 lrd = lrd->next;
19805 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019806
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019807 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019808 range->classes);
19809 for(i = 0; i < MAX_REGISTERS; i++) {
19810 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019811 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019812 arch_reg_str(i));
19813 }
19814 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019815 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019816 }
Eric Biederman530b5192003-07-01 10:05:30 +000019817 range->classes &= arch_reg_regcm(state, range->color);
19818 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019819 internal_error(state, range->defs->def, "select_free_color did not?");
19820 }
19821 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019822}
19823
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019824static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019825{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019826 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019827 struct live_range_edge *edge;
19828 struct live_range *range;
19829 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019830 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019831 range = rstate->low;
19832 if (*range->group_prev != range) {
19833 internal_error(state, 0, "lo: *prev != range?");
19834 }
19835 *range->group_prev = range->group_next;
19836 if (range->group_next) {
19837 range->group_next->group_prev = range->group_prev;
19838 }
19839 if (&range->group_next == rstate->low_tail) {
19840 rstate->low_tail = range->group_prev;
19841 }
19842 if (rstate->low == range) {
19843 internal_error(state, 0, "low: next != prev?");
19844 }
19845 }
19846 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019847 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019848 range = rstate->high;
19849 if (*range->group_prev != range) {
19850 internal_error(state, 0, "hi: *prev != range?");
19851 }
19852 *range->group_prev = range->group_next;
19853 if (range->group_next) {
19854 range->group_next->group_prev = range->group_prev;
19855 }
19856 if (&range->group_next == rstate->high_tail) {
19857 rstate->high_tail = range->group_prev;
19858 }
19859 if (rstate->high == range) {
19860 internal_error(state, 0, "high: next != prev?");
19861 }
19862 }
19863 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019864 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019865 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019866 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019867 range->group_prev = 0;
19868 for(edge = range->edges; edge; edge = edge->next) {
19869 struct live_range *node;
19870 node = edge->node;
19871 /* Move nodes from the high to the low list */
19872 if (node->group_prev && (node->color == REG_UNSET) &&
19873 (node->degree == regc_max_size(state, node->classes))) {
19874 if (*node->group_prev != node) {
19875 internal_error(state, 0, "move: *prev != node?");
19876 }
19877 *node->group_prev = node->group_next;
19878 if (node->group_next) {
19879 node->group_next->group_prev = node->group_prev;
19880 }
19881 if (&node->group_next == rstate->high_tail) {
19882 rstate->high_tail = node->group_prev;
19883 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019884 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019885 node->group_prev = rstate->low_tail;
19886 node->group_next = 0;
19887 *rstate->low_tail = node;
19888 rstate->low_tail = &node->group_next;
19889 if (*node->group_prev != node) {
19890 internal_error(state, 0, "move2: *prev != node?");
19891 }
19892 }
19893 node->degree -= 1;
19894 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019895 colored = color_graph(state, rstate);
19896 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019897 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019898 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019899 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019900 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019901 if (colored) {
19902 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19903 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019904 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019905 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019906}
19907
Eric Biedermana96d6a92003-05-13 20:45:19 +000019908static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19909{
19910 struct live_range *lr;
19911 struct live_range_edge *edge;
19912 struct triple *ins, *first;
19913 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019914 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019915 ins = first;
19916 do {
19917 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019918 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019919 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019920 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019921 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019922 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019923 if (lr->color == REG_UNSET) {
19924 internal_error(state, ins,
19925 "triple without a color");
19926 }
19927 /* Find the registers used by the edges */
19928 memset(used, 0, sizeof(used));
19929 for(edge = lr->edges; edge; edge = edge->next) {
19930 if (edge->node->color == REG_UNSET) {
19931 internal_error(state, 0,
19932 "live range without a color");
19933 }
19934 reg_fill_used(state, used, edge->node->color);
19935 }
19936 if (used[lr->color]) {
19937 internal_error(state, ins,
19938 "triple with already used color");
19939 }
19940 }
19941 ins = ins->next;
19942 } while(ins != first);
19943}
19944
Eric Biedermanb138ac82003-04-22 18:44:01 +000019945static void color_triples(struct compile_state *state, struct reg_state *rstate)
19946{
Eric Biederman90089602004-05-28 14:11:54 +000019947 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019948 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019949 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019950 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019951 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019952 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019953 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019954 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019955 "triple without a live range");
19956 }
Eric Biederman90089602004-05-28 14:11:54 +000019957 lrd = &rstate->lrd[ins->id];
19958 lr = lrd->lr;
19959 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019960 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019961 ins = ins->next;
19962 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019963}
19964
Eric Biedermanb138ac82003-04-22 18:44:01 +000019965static struct live_range *merge_sort_lr(
19966 struct live_range *first, struct live_range *last)
19967{
19968 struct live_range *mid, *join, **join_tail, *pick;
19969 size_t size;
19970 size = (last - first) + 1;
19971 if (size >= 2) {
19972 mid = first + size/2;
19973 first = merge_sort_lr(first, mid -1);
19974 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019975
Eric Biedermanb138ac82003-04-22 18:44:01 +000019976 join = 0;
19977 join_tail = &join;
19978 /* merge the two lists */
19979 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019980 if ((first->degree < mid->degree) ||
19981 ((first->degree == mid->degree) &&
19982 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019983 pick = first;
19984 first = first->group_next;
19985 if (first) {
19986 first->group_prev = 0;
19987 }
19988 }
19989 else {
19990 pick = mid;
19991 mid = mid->group_next;
19992 if (mid) {
19993 mid->group_prev = 0;
19994 }
19995 }
19996 pick->group_next = 0;
19997 pick->group_prev = join_tail;
19998 *join_tail = pick;
19999 join_tail = &pick->group_next;
20000 }
20001 /* Splice the remaining list */
20002 pick = (first)? first : mid;
20003 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020004 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020005 pick->group_prev = join_tail;
20006 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020007 }
20008 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020009 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020010 first = 0;
20011 }
20012 join = first;
20013 }
20014 return join;
20015}
20016
Stefan Reinauer14e22772010-04-27 06:56:47 +000020017static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020018 struct reg_state *rstate)
20019{
20020 struct triple *ins, *first;
20021 if (!rstate->defs) {
20022 return;
20023 }
20024 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020025 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020026 FILE *fp = state->dbgout;
20027 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020028 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020029 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020030 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020031 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020032 ins = first;
20033 do {
20034 if (ins->id) {
20035 struct live_range_def *lrd;
20036 lrd = &rstate->lrd[ins->id];
20037 ins->id = lrd->orig_id;
20038 }
20039 ins = ins->next;
20040 } while(ins != first);
20041}
20042
20043static void cleanup_live_edges(struct reg_state *rstate)
20044{
20045 int i;
20046 /* Free the edges on each node */
20047 for(i = 1; i <= rstate->ranges; i++) {
20048 remove_live_edges(rstate, &rstate->lr[i]);
20049 }
20050}
20051
20052static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20053{
20054 cleanup_live_edges(rstate);
20055 xfree(rstate->lrd);
20056 xfree(rstate->lr);
20057
20058 /* Free the variable lifetime information */
20059 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020060 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020061 }
20062 rstate->defs = 0;
20063 rstate->ranges = 0;
20064 rstate->lrd = 0;
20065 rstate->lr = 0;
20066 rstate->blocks = 0;
20067}
20068
Eric Biederman153ea352003-06-20 14:43:20 +000020069static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020070static void allocate_registers(struct compile_state *state)
20071{
20072 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020073 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020074
20075 /* Clear out the reg_state */
20076 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020077 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020078
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020079 do {
20080 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020081 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020082 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020083
Eric Biederman5ade04a2003-10-22 04:03:46 +000020084 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020085 FILE *fp = state->errout;
20086 fprintf(fp, "pass: %d\n", rstate.passes);
20087 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020088 }
Eric Biederman153ea352003-06-20 14:43:20 +000020089
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020090 /* Restore ids */
20091 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020092
Eric Biedermanf96a8102003-06-16 16:57:34 +000020093 /* Cleanup the temporary data structures */
20094 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020095
Eric Biedermanf96a8102003-06-16 16:57:34 +000020096 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020097 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020098
Eric Biedermanf96a8102003-06-16 16:57:34 +000020099 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020100 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020101
Eric Biederman153ea352003-06-20 14:43:20 +000020102 /* Fix two simultaneous uses of the same register.
20103 * In a few pathlogical cases a partial untangle moves
20104 * the tangle to a part of the graph we won't revisit.
20105 * So we keep looping until we have no more tangle fixes
20106 * to apply.
20107 */
20108 do {
20109 tangles = correct_tangles(state, rstate.blocks);
20110 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020111
Stefan Reinauer14e22772010-04-27 06:56:47 +000020112
Eric Biederman90089602004-05-28 14:11:54 +000020113 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020114 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020115
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020116 /* Allocate and initialize the live ranges */
20117 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020118
Stefan Reinauer14e22772010-04-27 06:56:47 +000020119 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020120 * buys me nothing. The code is left this way in case
20121 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020122 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020123 */
20124 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020125 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020126 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020127 }
20128
Eric Biederman153ea352003-06-20 14:43:20 +000020129 /* Remove any previous live edge calculations */
20130 cleanup_live_edges(&rstate);
20131
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020132 /* Compute the interference graph */
20133 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020134 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020135 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020136
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020137 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020138 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020139 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020140 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020141 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020142 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020143 print_interference_ins, &rstate);
20144 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020145
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020146 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020147
Eric Biederman5ade04a2003-10-22 04:03:46 +000020148 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020149 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020150 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020151 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020152
Eric Biederman83b991a2003-10-11 06:20:25 +000020153#if DEBUG_CONSISTENCY > 1
20154# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020155 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020156# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020157 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020158 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020159 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020160 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020161# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020162 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020163#endif
20164#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020165
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020166 /* Build the groups low and high. But with the nodes
20167 * first sorted by degree order.
20168 */
20169 rstate.low_tail = &rstate.low;
20170 rstate.high_tail = &rstate.high;
20171 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20172 if (rstate.high) {
20173 rstate.high->group_prev = &rstate.high;
20174 }
20175 for(point = &rstate.high; *point; point = &(*point)->group_next)
20176 ;
20177 rstate.high_tail = point;
20178 /* Walk through the high list and move everything that needs
20179 * to be onto low.
20180 */
20181 for(point = &rstate.high; *point; point = next) {
20182 struct live_range *range;
20183 next = &(*point)->group_next;
20184 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020185
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020186 /* If it has a low degree or it already has a color
20187 * place the node in low.
20188 */
20189 if ((range->degree < regc_max_size(state, range->classes)) ||
20190 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020191 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020192 range - rstate.lr, range->degree,
20193 (range->color != REG_UNSET) ? " (colored)": "");
20194 *range->group_prev = range->group_next;
20195 if (range->group_next) {
20196 range->group_next->group_prev = range->group_prev;
20197 }
20198 if (&range->group_next == rstate.high_tail) {
20199 rstate.high_tail = range->group_prev;
20200 }
20201 range->group_prev = rstate.low_tail;
20202 range->group_next = 0;
20203 *rstate.low_tail = range;
20204 rstate.low_tail = &range->group_next;
20205 next = point;
20206 }
20207 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020208 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020209 range - rstate.lr, range->degree,
20210 (range->color != REG_UNSET) ? " (colored)": "");
20211 }
20212 }
20213 /* Color the live_ranges */
20214 colored = color_graph(state, &rstate);
20215 rstate.passes++;
20216 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020217
Eric Biedermana96d6a92003-05-13 20:45:19 +000020218 /* Verify the graph was properly colored */
20219 verify_colors(state, &rstate);
20220
Eric Biedermanb138ac82003-04-22 18:44:01 +000020221 /* Move the colors from the graph to the triples */
20222 color_triples(state, &rstate);
20223
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020224 /* Cleanup the temporary data structures */
20225 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020226
20227 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020228 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020229}
20230
20231/* Sparce Conditional Constant Propogation
20232 * =========================================
20233 */
20234struct ssa_edge;
20235struct flow_block;
20236struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020237 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020238 struct triple *def;
20239 struct ssa_edge *out;
20240 struct flow_block *fblock;
20241 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020242 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020243 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020244 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020245 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020246};
20247struct ssa_edge {
20248 struct lattice_node *src;
20249 struct lattice_node *dst;
20250 struct ssa_edge *work_next;
20251 struct ssa_edge *work_prev;
20252 struct ssa_edge *out_next;
20253};
20254struct flow_edge {
20255 struct flow_block *src;
20256 struct flow_block *dst;
20257 struct flow_edge *work_next;
20258 struct flow_edge *work_prev;
20259 struct flow_edge *in_next;
20260 struct flow_edge *out_next;
20261 int executable;
20262};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020263#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020264struct flow_block {
20265 struct block *block;
20266 struct flow_edge *in;
20267 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020268 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020269};
20270
20271struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020272 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020273 struct lattice_node *lattice;
20274 struct ssa_edge *ssa_edges;
20275 struct flow_block *flow_blocks;
20276 struct flow_edge *flow_work_list;
20277 struct ssa_edge *ssa_work_list;
20278};
20279
20280
Eric Biederman90089602004-05-28 14:11:54 +000020281static int is_scc_const(struct compile_state *state, struct triple *ins)
20282{
20283 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20284}
20285
20286static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20287{
20288 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20289}
20290
20291static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20292{
20293 return is_scc_const(state, lnode->val);
20294}
20295
20296static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20297{
20298 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20299}
20300
Stefan Reinauer14e22772010-04-27 06:56:47 +000020301static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020302 struct flow_edge *fedge)
20303{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020304 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020305 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020306 fedge,
20307 fedge->src->block?fedge->src->block->last->id: 0,
20308 fedge->dst->block?fedge->dst->block->first->id: 0);
20309 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020310 if ((fedge == scc->flow_work_list) ||
20311 (fedge->work_next != fedge) ||
20312 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020313
20314 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020315 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020316 fedge);
20317 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020318 return;
20319 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020320 if (!scc->flow_work_list) {
20321 scc->flow_work_list = fedge;
20322 fedge->work_next = fedge->work_prev = fedge;
20323 }
20324 else {
20325 struct flow_edge *ftail;
20326 ftail = scc->flow_work_list->work_prev;
20327 fedge->work_next = ftail->work_next;
20328 fedge->work_prev = ftail;
20329 fedge->work_next->work_prev = fedge;
20330 fedge->work_prev->work_next = fedge;
20331 }
20332}
20333
20334static struct flow_edge *scc_next_fedge(
20335 struct compile_state *state, struct scc_state *scc)
20336{
20337 struct flow_edge *fedge;
20338 fedge = scc->flow_work_list;
20339 if (fedge) {
20340 fedge->work_next->work_prev = fedge->work_prev;
20341 fedge->work_prev->work_next = fedge->work_next;
20342 if (fedge->work_next != fedge) {
20343 scc->flow_work_list = fedge->work_next;
20344 } else {
20345 scc->flow_work_list = 0;
20346 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020347 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020348 }
20349 return fedge;
20350}
20351
20352static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20353 struct ssa_edge *sedge)
20354{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020355 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020356 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20357 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020358 sedge->src->def->id,
20359 sedge->dst->def->id);
20360 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020361 if ((sedge == scc->ssa_work_list) ||
20362 (sedge->work_next != sedge) ||
20363 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020364
20365 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020366 fprintf(state->errout, "dupped sedge: %5ld\n",
20367 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020368 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020369 return;
20370 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020371 if (!scc->ssa_work_list) {
20372 scc->ssa_work_list = sedge;
20373 sedge->work_next = sedge->work_prev = sedge;
20374 }
20375 else {
20376 struct ssa_edge *stail;
20377 stail = scc->ssa_work_list->work_prev;
20378 sedge->work_next = stail->work_next;
20379 sedge->work_prev = stail;
20380 sedge->work_next->work_prev = sedge;
20381 sedge->work_prev->work_next = sedge;
20382 }
20383}
20384
20385static struct ssa_edge *scc_next_sedge(
20386 struct compile_state *state, struct scc_state *scc)
20387{
20388 struct ssa_edge *sedge;
20389 sedge = scc->ssa_work_list;
20390 if (sedge) {
20391 sedge->work_next->work_prev = sedge->work_prev;
20392 sedge->work_prev->work_next = sedge->work_next;
20393 if (sedge->work_next != sedge) {
20394 scc->ssa_work_list = sedge->work_next;
20395 } else {
20396 scc->ssa_work_list = 0;
20397 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020398 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020399 }
20400 return sedge;
20401}
20402
20403static void initialize_scc_state(
20404 struct compile_state *state, struct scc_state *scc)
20405{
20406 int ins_count, ssa_edge_count;
20407 int ins_index, ssa_edge_index, fblock_index;
20408 struct triple *first, *ins;
20409 struct block *block;
20410 struct flow_block *fblock;
20411
20412 memset(scc, 0, sizeof(*scc));
20413
20414 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020415 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020416 ins = first;
20417 ins_count = ssa_edge_count = 0;
20418 do {
20419 struct triple_set *edge;
20420 ins_count += 1;
20421 for(edge = ins->use; edge; edge = edge->next) {
20422 ssa_edge_count++;
20423 }
20424 ins = ins->next;
20425 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020426 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020427 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20428 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020429 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020430 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020431 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020432 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020433 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020434 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020435 scc->flow_blocks =
20436 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020437 "flow_blocks");
20438
20439 /* Initialize pass one collect up the nodes */
20440 fblock = 0;
20441 block = 0;
20442 ins_index = ssa_edge_index = fblock_index = 0;
20443 ins = first;
20444 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020445 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20446 block = ins->u.block;
20447 if (!block) {
20448 internal_error(state, ins, "label without block");
20449 }
20450 fblock_index += 1;
20451 block->vertex = fblock_index;
20452 fblock = &scc->flow_blocks[fblock_index];
20453 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020454 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20455 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020456 }
20457 {
20458 struct lattice_node *lnode;
20459 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020460 lnode = &scc->lattice[ins_index];
20461 lnode->def = ins;
20462 lnode->out = 0;
20463 lnode->fblock = fblock;
20464 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020465 if (lnode->val->op == OP_UNKNOWNVAL) {
20466 lnode->val = 0; /* LATTICE LOW by definition */
20467 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020468 lnode->old_id = ins->id;
20469 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020470 }
20471 ins = ins->next;
20472 } while(ins != first);
20473 /* Initialize pass two collect up the edges */
20474 block = 0;
20475 fblock = 0;
20476 ins = first;
20477 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020478 {
20479 struct triple_set *edge;
20480 struct ssa_edge **stail;
20481 struct lattice_node *lnode;
20482 lnode = &scc->lattice[ins->id];
20483 lnode->out = 0;
20484 stail = &lnode->out;
20485 for(edge = ins->use; edge; edge = edge->next) {
20486 struct ssa_edge *sedge;
20487 ssa_edge_index += 1;
20488 sedge = &scc->ssa_edges[ssa_edge_index];
20489 *stail = sedge;
20490 stail = &sedge->out_next;
20491 sedge->src = lnode;
20492 sedge->dst = &scc->lattice[edge->member->id];
20493 sedge->work_next = sedge->work_prev = sedge;
20494 sedge->out_next = 0;
20495 }
20496 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020497 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20498 struct flow_edge *fedge, **ftail;
20499 struct block_set *bedge;
20500 block = ins->u.block;
20501 fblock = &scc->flow_blocks[block->vertex];
20502 fblock->in = 0;
20503 fblock->out = 0;
20504 ftail = &fblock->out;
20505
20506 fedge = fblock->edges;
20507 bedge = block->edges;
20508 for(; bedge; bedge = bedge->next, fedge++) {
20509 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20510 if (fedge->dst->block != bedge->member) {
20511 internal_error(state, 0, "block mismatch");
20512 }
20513 *ftail = fedge;
20514 ftail = &fedge->out_next;
20515 fedge->out_next = 0;
20516 }
20517 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20518 fedge->src = fblock;
20519 fedge->work_next = fedge->work_prev = fedge;
20520 fedge->executable = 0;
20521 }
20522 }
20523 ins = ins->next;
20524 } while (ins != first);
20525 block = 0;
20526 fblock = 0;
20527 ins = first;
20528 do {
20529 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20530 struct flow_edge **ftail;
20531 struct block_set *bedge;
20532 block = ins->u.block;
20533 fblock = &scc->flow_blocks[block->vertex];
20534 ftail = &fblock->in;
20535 for(bedge = block->use; bedge; bedge = bedge->next) {
20536 struct block *src_block;
20537 struct flow_block *sfblock;
20538 struct flow_edge *sfedge;
20539 src_block = bedge->member;
20540 sfblock = &scc->flow_blocks[src_block->vertex];
20541 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20542 if (sfedge->dst == fblock) {
20543 break;
20544 }
20545 }
20546 if (!sfedge) {
20547 internal_error(state, 0, "edge mismatch");
20548 }
20549 *ftail = sfedge;
20550 ftail = &sfedge->in_next;
20551 sfedge->in_next = 0;
20552 }
20553 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020554 ins = ins->next;
20555 } while(ins != first);
20556 /* Setup a dummy block 0 as a node above the start node */
20557 {
20558 struct flow_block *fblock, *dst;
20559 struct flow_edge *fedge;
20560 fblock = &scc->flow_blocks[0];
20561 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020562 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020563 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020564 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020565 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020566 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020567 fedge->src = fblock;
20568 fedge->dst = dst;
20569 fedge->work_next = fedge;
20570 fedge->work_prev = fedge;
20571 fedge->in_next = fedge->dst->in;
20572 fedge->out_next = 0;
20573 fedge->executable = 0;
20574 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020575
Eric Biedermanb138ac82003-04-22 18:44:01 +000020576 /* Initialize the work lists */
20577 scc->flow_work_list = 0;
20578 scc->ssa_work_list = 0;
20579 scc_add_fedge(state, scc, fedge);
20580 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020581 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020582 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020583 ins_index, ssa_edge_index, fblock_index);
20584 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020585}
20586
Stefan Reinauer14e22772010-04-27 06:56:47 +000020587
Eric Biedermanb138ac82003-04-22 18:44:01 +000020588static void free_scc_state(
20589 struct compile_state *state, struct scc_state *scc)
20590{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020591 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020592 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020593 struct flow_block *fblock;
20594 fblock = &scc->flow_blocks[i];
20595 if (fblock->edges) {
20596 xfree(fblock->edges);
20597 fblock->edges = 0;
20598 }
20599 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020600 xfree(scc->flow_blocks);
20601 xfree(scc->ssa_edges);
20602 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020603
Eric Biedermanb138ac82003-04-22 18:44:01 +000020604}
20605
20606static struct lattice_node *triple_to_lattice(
20607 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20608{
20609 if (ins->id <= 0) {
20610 internal_error(state, ins, "bad id");
20611 }
20612 return &scc->lattice[ins->id];
20613}
20614
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020615static struct triple *preserve_lval(
20616 struct compile_state *state, struct lattice_node *lnode)
20617{
20618 struct triple *old;
20619 /* Preserve the original value */
20620 if (lnode->val) {
20621 old = dup_triple(state, lnode->val);
20622 if (lnode->val != lnode->def) {
20623 xfree(lnode->val);
20624 }
20625 lnode->val = 0;
20626 } else {
20627 old = 0;
20628 }
20629 return old;
20630}
20631
Stefan Reinauer14e22772010-04-27 06:56:47 +000020632static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020633 struct triple *old, struct lattice_node *lnode)
20634{
20635 int changed;
20636 /* See if the lattice value has changed */
20637 changed = 1;
20638 if (!old && !lnode->val) {
20639 changed = 0;
20640 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020641 if (changed &&
20642 lnode->val && old &&
20643 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020644 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020645 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20646 changed = 0;
20647 }
20648 if (old) {
20649 xfree(old);
20650 }
20651 return changed;
20652
20653}
20654
Eric Biederman5ade04a2003-10-22 04:03:46 +000020655static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020656 struct compile_state *state, struct scc_state *scc,
20657 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020658{
Eric Biederman90089602004-05-28 14:11:54 +000020659 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20660 display_triple_changes(state->errout, lnode->val, lnode->def);
20661 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020662 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020663 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020664 struct triple *val, **expr;
20665 val = lnode->val? lnode->val : lnode->def;
20666 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020667 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020668 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20669 lnode->def->id,
20670 tops(lnode->def->op));
20671 expr = triple_rhs(state, lnode->def, 0);
20672 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20673 if (*expr) {
20674 fprintf(fp, " %d", (*expr)->id);
20675 }
20676 }
20677 if (val->op == OP_INTCONST) {
20678 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20679 }
20680 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020681 (is_lattice_hi(state, lnode)? "hi":
20682 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020683 changed? "changed" : ""
20684 );
20685 }
20686}
20687
Eric Biedermanb138ac82003-04-22 18:44:01 +000020688static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20689 struct lattice_node *lnode)
20690{
20691 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020692 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020693 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020695
Eric Biedermanb138ac82003-04-22 18:44:01 +000020696 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020697 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020698
Eric Biedermanb138ac82003-04-22 18:44:01 +000020699 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020700 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020701 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702 scratch->next = scratch;
20703 scratch->prev = scratch;
20704 scratch->use = 0;
20705
Eric Biederman90089602004-05-28 14:11:54 +000020706 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020707 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020708 dexpr = &lnode->def->param[i];
20709 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020710 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020711 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20712 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020713 *dexpr) {
20714 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020715 tmp = triple_to_lattice(state, scc, *dexpr);
20716 *vexpr = (tmp->val)? tmp->val : tmp->def;
20717 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020718 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020719 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020720 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020721 }
20722 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020723#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020724#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020725#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020726 /* So far it looks like only the strength reduction
20727 * optimization are things I need to worry about.
20728 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020729 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020730 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020731 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020732 internal_error(state, lnode->def, "scratch used?");
20733 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020734 if ((scratch->prev != scratch) ||
20735 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020736 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020737 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020738 internal_error(state, lnode->def, "scratch in list?");
20739 }
20740 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020741 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020742 for(i = 0; i < count; i++) {
20743 vexpr = &scratch->param[i];
20744 if (*vexpr) {
20745 unuse_triple(*vexpr, scratch);
20746 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020747 }
Eric Biederman90089602004-05-28 14:11:54 +000020748 if (lnode->val->op == OP_UNKNOWNVAL) {
20749 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020750 }
Eric Biederman90089602004-05-28 14:11:54 +000020751 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020752 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020753 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020754 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020755 count * sizeof(lnode->val->param[0])) == 0) &&
20756 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020757 lnode->val = lnode->def;
20758 }
Eric Biederman90089602004-05-28 14:11:54 +000020759 /* Only allow lattice high when all of my inputs
20760 * are also lattice high. Occassionally I can
20761 * have constants with a lattice low input, so
20762 * I do not need to check that case.
20763 */
20764 if (is_lattice_hi(state, lnode)) {
20765 struct lattice_node *tmp;
20766 int rhs;
20767 rhs = lnode->val->rhs;
20768 for(i = 0; i < rhs; i++) {
20769 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20770 if (!is_lattice_hi(state, tmp)) {
20771 lnode->val = 0;
20772 break;
20773 }
20774 }
20775 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020776 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020777 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020778 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020779 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020780 lnode->val = 0;
20781 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020782 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020783 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020784 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020785 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020786 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020787 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020788 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020789#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020790#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020791#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020792 if (changed) {
20793 internal_warning(state, lnode->def, "non def changes value?");
20794 }
20795 lnode->val = 0;
20796 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020797
Eric Biederman83b991a2003-10-11 06:20:25 +000020798 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020799 if (lnode->val != scratch) {
20800 xfree(scratch);
20801 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020802
Eric Biedermanb138ac82003-04-22 18:44:01 +000020803 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020804}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020805
Eric Biederman90089602004-05-28 14:11:54 +000020806
20807static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020808 struct lattice_node *lnode)
20809{
20810 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020811 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020812 int changed;
20813
20814 /* Update the branch value */
20815 changed = compute_lnode_val(state, scc, lnode);
20816 scc_debug_lnode(state, scc, lnode, changed);
20817
20818 /* This only applies to conditional branches */
20819 if (!triple_is_cbranch(state, lnode->def)) {
20820 internal_error(state, lnode->def, "not a conditional branch");
20821 }
20822
Eric Biederman5ade04a2003-10-22 04:03:46 +000020823 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020825 FILE *fp = state->errout;
20826 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020827 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020828 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020829
Eric Biedermanb138ac82003-04-22 18:44:01 +000020830 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020831 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020832 }
Eric Biederman90089602004-05-28 14:11:54 +000020833 fprintf(fp, " )");
20834 if (lnode->def->rhs > 0) {
20835 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020836 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020837 }
Eric Biederman90089602004-05-28 14:11:54 +000020838 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020839 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020840 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020841 for(left = cond->fblock->out; left; left = left->out_next) {
20842 if (left->dst->block->first == lnode->def->next) {
20843 break;
20844 }
20845 }
20846 if (!left) {
20847 internal_error(state, lnode->def, "Cannot find left branch edge");
20848 }
20849 for(right = cond->fblock->out; right; right = right->out_next) {
20850 if (right->dst->block->first == TARG(lnode->def, 0)) {
20851 break;
20852 }
20853 }
20854 if (!right) {
20855 internal_error(state, lnode->def, "Cannot find right branch edge");
20856 }
Eric Biederman90089602004-05-28 14:11:54 +000020857 /* I should only come here if the controlling expressions value
20858 * has changed, which means it must be either a constant or lo.
20859 */
20860 if (is_lattice_hi(state, cond)) {
20861 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020862 return;
20863 }
Eric Biederman90089602004-05-28 14:11:54 +000020864 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020865 scc_add_fedge(state, scc, left);
20866 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020867 }
20868 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020869 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020870 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020871 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020872 }
20873
20874}
20875
Eric Biederman90089602004-05-28 14:11:54 +000020876
Stefan Reinauer14e22772010-04-27 06:56:47 +000020877static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020878 struct scc_state *scc, struct ssa_edge *sedge)
20879{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020880 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020881 scc_visit_cbranch(state, scc, sedge->dst);
20882 }
20883 else if (triple_is_def(state, sedge->dst->def)) {
20884 scc_add_sedge(state, scc, sedge);
20885 }
20886}
20887
Stefan Reinauer14e22772010-04-27 06:56:47 +000020888static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020889 struct lattice_node *lnode)
20890{
20891 struct lattice_node *tmp;
20892 struct triple **slot, *old;
20893 struct flow_edge *fedge;
20894 int changed;
20895 int index;
20896 if (lnode->def->op != OP_PHI) {
20897 internal_error(state, lnode->def, "not phi");
20898 }
20899 /* Store the original value */
20900 old = preserve_lval(state, lnode);
20901
20902 /* default to lattice high */
20903 lnode->val = lnode->def;
20904 slot = &RHS(lnode->def, 0);
20905 index = 0;
20906 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20907 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020908 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020909 index,
20910 fedge->dst->block->vertex,
20911 fedge->executable
20912 );
20913 }
20914 if (!fedge->executable) {
20915 continue;
20916 }
20917 if (!slot[index]) {
20918 internal_error(state, lnode->def, "no phi value");
20919 }
20920 tmp = triple_to_lattice(state, scc, slot[index]);
20921 /* meet(X, lattice low) = lattice low */
20922 if (is_lattice_lo(state, tmp)) {
20923 lnode->val = 0;
20924 }
20925 /* meet(X, lattice high) = X */
20926 else if (is_lattice_hi(state, tmp)) {
20927 lnode->val = lnode->val;
20928 }
20929 /* meet(lattice high, X) = X */
20930 else if (is_lattice_hi(state, lnode)) {
20931 lnode->val = dup_triple(state, tmp->val);
20932 /* Only change the type if necessary */
20933 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20934 lnode->val->type = lnode->def->type;
20935 }
20936 }
20937 /* meet(const, const) = const or lattice low */
20938 else if (!constants_equal(state, lnode->val, tmp->val)) {
20939 lnode->val = 0;
20940 }
20941
20942 /* meet(lattice low, X) = lattice low */
20943 if (is_lattice_lo(state, lnode)) {
20944 lnode->val = 0;
20945 break;
20946 }
20947 }
20948 changed = lval_changed(state, old, lnode);
20949 scc_debug_lnode(state, scc, lnode, changed);
20950
20951 /* If the lattice value has changed update the work lists. */
20952 if (changed) {
20953 struct ssa_edge *sedge;
20954 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20955 scc_add_sedge_dst(state, scc, sedge);
20956 }
20957 }
20958}
20959
20960
Eric Biedermanb138ac82003-04-22 18:44:01 +000020961static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20962 struct lattice_node *lnode)
20963{
20964 int changed;
20965
Eric Biederman90089602004-05-28 14:11:54 +000020966 if (!triple_is_def(state, lnode->def)) {
20967 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020968 }
Eric Biederman90089602004-05-28 14:11:54 +000020969 changed = compute_lnode_val(state, scc, lnode);
20970 scc_debug_lnode(state, scc, lnode, changed);
20971
20972 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020973 struct ssa_edge *sedge;
20974 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020975 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020976 }
20977 }
20978}
20979
20980static void scc_writeback_values(
20981 struct compile_state *state, struct scc_state *scc)
20982{
20983 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020984 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020985 ins = first;
20986 do {
20987 struct lattice_node *lnode;
20988 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020989 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020990 if (is_lattice_hi(state, lnode) &&
20991 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020992 {
20993 struct flow_edge *fedge;
20994 int executable;
20995 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020996 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020997 !executable && fedge; fedge = fedge->in_next) {
20998 executable |= fedge->executable;
20999 }
21000 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000021001 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021002 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000021003 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021004 tops(lnode->def->op),
21005 tops(lnode->val->op));
21006 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021007 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021008 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021009
Eric Biederman83b991a2003-10-11 06:20:25 +000021010 /* Restore id */
21011 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021012 if (lnode->val && (lnode->val != ins)) {
21013 /* See if it something I know how to write back */
21014 switch(lnode->val->op) {
21015 case OP_INTCONST:
21016 mkconst(state, ins, lnode->val->u.cval);
21017 break;
21018 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021019 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021020 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021021 break;
21022 default:
21023 /* By default don't copy the changes,
21024 * recompute them in place instead.
21025 */
21026 simplify(state, ins);
21027 break;
21028 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021029 if (is_const(lnode->val) &&
21030 !constants_equal(state, lnode->val, ins)) {
21031 internal_error(state, 0, "constants not equal");
21032 }
21033 /* Free the lattice nodes */
21034 xfree(lnode->val);
21035 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021036 }
21037 ins = ins->next;
21038 } while(ins != first);
21039}
21040
21041static void scc_transform(struct compile_state *state)
21042{
21043 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021044 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21045 return;
21046 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021047
21048 initialize_scc_state(state, &scc);
21049
21050 while(scc.flow_work_list || scc.ssa_work_list) {
21051 struct flow_edge *fedge;
21052 struct ssa_edge *sedge;
21053 struct flow_edge *fptr;
21054 while((fedge = scc_next_fedge(state, &scc))) {
21055 struct block *block;
21056 struct triple *ptr;
21057 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021058 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021059 int done;
21060 if (fedge->executable) {
21061 continue;
21062 }
21063 if (!fedge->dst) {
21064 internal_error(state, 0, "fedge without dst");
21065 }
21066 if (!fedge->src) {
21067 internal_error(state, 0, "fedge without src");
21068 }
21069 fedge->executable = 1;
21070 fblock = fedge->dst;
21071 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021072 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021073 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21074 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021075 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021076 }
21077 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021078
Eric Biederman5ade04a2003-10-22 04:03:46 +000021079 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021080 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021081 block->vertex, reps);
21082 }
21083
Eric Biedermanb138ac82003-04-22 18:44:01 +000021084 done = 0;
21085 for(ptr = block->first; !done; ptr = ptr->next) {
21086 struct lattice_node *lnode;
21087 done = (ptr == block->last);
21088 lnode = &scc.lattice[ptr->id];
21089 if (ptr->op == OP_PHI) {
21090 scc_visit_phi(state, &scc, lnode);
21091 }
Eric Biederman90089602004-05-28 14:11:54 +000021092 else if ((reps == 1) && triple_is_def(state, ptr))
21093 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021094 scc_visit_expr(state, &scc, lnode);
21095 }
21096 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021097 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021098 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021099 struct flow_edge *out;
21100 for(out = fblock->out; out; out = out->out_next) {
21101 scc_add_fedge(state, &scc, out);
21102 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021103 }
21104 }
21105 while((sedge = scc_next_sedge(state, &scc))) {
21106 struct lattice_node *lnode;
21107 struct flow_block *fblock;
21108 lnode = sedge->dst;
21109 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021110
21111 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021112 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021113 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021114 sedge->src->def->id,
21115 sedge->dst->def->id);
21116 }
21117
Eric Biedermanb138ac82003-04-22 18:44:01 +000021118 if (lnode->def->op == OP_PHI) {
21119 scc_visit_phi(state, &scc, lnode);
21120 }
21121 else {
21122 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21123 if (fptr->executable) {
21124 break;
21125 }
21126 }
21127 if (fptr) {
21128 scc_visit_expr(state, &scc, lnode);
21129 }
21130 }
21131 }
21132 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021133
Eric Biedermanb138ac82003-04-22 18:44:01 +000021134 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021135 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021136 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021137
Eric Biederman90089602004-05-28 14:11:54 +000021138 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021139}
21140
21141
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021142static void transform_to_arch_instructions(struct compile_state *state)
21143{
21144 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021145 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021146 ins = first;
21147 do {
21148 ins = transform_to_arch_instruction(state, ins);
21149 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021150
Eric Biederman90089602004-05-28 14:11:54 +000021151 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021152}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021153
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021154#if DEBUG_CONSISTENCY
21155static void verify_uses(struct compile_state *state)
21156{
21157 struct triple *first, *ins;
21158 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021159 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021160 ins = first;
21161 do {
21162 struct triple **expr;
21163 expr = triple_rhs(state, ins, 0);
21164 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021165 struct triple *rhs;
21166 rhs = *expr;
21167 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021168 if (set->member == ins) {
21169 break;
21170 }
21171 }
21172 if (!set) {
21173 internal_error(state, ins, "rhs not used");
21174 }
21175 }
21176 expr = triple_lhs(state, ins, 0);
21177 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021178 struct triple *lhs;
21179 lhs = *expr;
21180 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021181 if (set->member == ins) {
21182 break;
21183 }
21184 }
21185 if (!set) {
21186 internal_error(state, ins, "lhs not used");
21187 }
21188 }
Eric Biederman90089602004-05-28 14:11:54 +000021189 expr = triple_misc(state, ins, 0);
21190 if (ins->op != OP_PHI) {
21191 for(; expr; expr = triple_targ(state, ins, expr)) {
21192 struct triple *misc;
21193 misc = *expr;
21194 for(set = misc?misc->use:0; set; set = set->next) {
21195 if (set->member == ins) {
21196 break;
21197 }
21198 }
21199 if (!set) {
21200 internal_error(state, ins, "misc not used");
21201 }
21202 }
21203 }
21204 if (!triple_is_ret(state, ins)) {
21205 expr = triple_targ(state, ins, 0);
21206 for(; expr; expr = triple_targ(state, ins, expr)) {
21207 struct triple *targ;
21208 targ = *expr;
21209 for(set = targ?targ->use:0; set; set = set->next) {
21210 if (set->member == ins) {
21211 break;
21212 }
21213 }
21214 if (!set) {
21215 internal_error(state, ins, "targ not used");
21216 }
21217 }
21218 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021219 ins = ins->next;
21220 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021221
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021222}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021223static void verify_blocks_present(struct compile_state *state)
21224{
21225 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021226 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021227 return;
21228 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021229 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021230 ins = first;
21231 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021232 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021233 if (triple_stores_block(state, ins)) {
21234 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021235 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021236 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021237 }
21238 }
21239 ins = ins->next;
21240 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021241
21242
Eric Biedermand1ea5392003-06-28 06:49:45 +000021243}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021244
21245static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21246{
21247 struct block_set *bedge;
21248 struct block *targ;
21249 targ = block_of_triple(state, edge);
21250 for(bedge = block->edges; bedge; bedge = bedge->next) {
21251 if (bedge->member == targ) {
21252 return 1;
21253 }
21254 }
21255 return 0;
21256}
21257
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021258static void verify_blocks(struct compile_state *state)
21259{
21260 struct triple *ins;
21261 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021262 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021263 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021264 if (!block) {
21265 return;
21266 }
Eric Biederman530b5192003-07-01 10:05:30 +000021267 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021268 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021269 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021270 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021271 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021272 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021273 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021274 internal_error(state, ins, "inconsitent block specified");
21275 }
Eric Biederman530b5192003-07-01 10:05:30 +000021276 valid_ins(state, ins);
21277 }
21278 users = 0;
21279 for(user = block->use; user; user = user->next) {
21280 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021281 if (!user->member->first) {
21282 internal_error(state, block->first, "user is empty");
21283 }
Eric Biederman90089602004-05-28 14:11:54 +000021284 if ((block == state->bb.last_block) &&
21285 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021286 continue;
21287 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021288 for(edge = user->member->edges; edge; edge = edge->next) {
21289 if (edge->member == block) {
21290 break;
21291 }
21292 }
21293 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021294 internal_error(state, user->member->first,
21295 "user does not use block");
21296 }
21297 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021298 if (triple_is_branch(state, block->last)) {
21299 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021300 expr = triple_edge_targ(state, block->last, 0);
21301 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021302 if (*expr && !edge_present(state, block, *expr)) {
21303 internal_error(state, block->last, "no edge to targ");
21304 }
21305 }
Eric Biederman530b5192003-07-01 10:05:30 +000021306 }
Eric Biederman90089602004-05-28 14:11:54 +000021307 if (!triple_is_ubranch(state, block->last) &&
21308 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021309 !edge_present(state, block, block->last->next)) {
21310 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021311 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021312 for(edge = block->edges; edge; edge = edge->next) {
21313 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021314 if (user->member == block) {
21315 break;
21316 }
21317 }
21318 if (!user || user->member != block) {
21319 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021320 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021321 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021322 if (!edge->member->first) {
21323 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021324 }
Eric Biederman530b5192003-07-01 10:05:30 +000021325 }
21326 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021327 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021328 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021329 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021330 }
21331 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021332 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021333 "cannot find next block");
21334 }
21335 block = block->last->next->u.block;
21336 if (!block) {
21337 internal_error(state, block->last->next,
21338 "bad next block");
21339 }
Eric Biederman90089602004-05-28 14:11:54 +000021340 } while(block != state->bb.first_block);
21341 if (blocks != state->bb.last_vertex) {
21342 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21343 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021344 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021345}
21346
21347static void verify_domination(struct compile_state *state)
21348{
21349 struct triple *first, *ins;
21350 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021351 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021352 return;
21353 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021354
Eric Biederman83b991a2003-10-11 06:20:25 +000021355 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021356 ins = first;
21357 do {
21358 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021359 struct triple **slot;
21360 struct triple *use_point;
21361 int i, zrhs;
21362 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021363 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021364 slot = &RHS(set->member, 0);
21365 /* See if the use is on the right hand side */
21366 for(i = 0; i < zrhs; i++) {
21367 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021368 break;
21369 }
21370 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021371 if (i < zrhs) {
21372 use_point = set->member;
21373 if (set->member->op == OP_PHI) {
21374 struct block_set *bset;
21375 int edge;
21376 bset = set->member->u.block->use;
21377 for(edge = 0; bset && (edge < i); edge++) {
21378 bset = bset->next;
21379 }
21380 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021381 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021382 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021383 }
21384 use_point = bset->member->last;
21385 }
21386 }
21387 if (use_point &&
21388 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021389 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021390 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021391 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021392 }
21393 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021394 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021395 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021396 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021397 }
21398 }
21399 ins = ins->next;
21400 } while(ins != first);
21401}
21402
Eric Biederman83b991a2003-10-11 06:20:25 +000021403static void verify_rhs(struct compile_state *state)
21404{
21405 struct triple *first, *ins;
21406 first = state->first;
21407 ins = first;
21408 do {
21409 struct triple **slot;
21410 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021411 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021412 slot = &RHS(ins, 0);
21413 for(i = 0; i < zrhs; i++) {
21414 if (slot[i] == 0) {
21415 internal_error(state, ins,
21416 "missing rhs %d on %s",
21417 i, tops(ins->op));
21418 }
21419 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21420 internal_error(state, ins,
21421 "ins == rhs[%d] on %s",
21422 i, tops(ins->op));
21423 }
21424 }
21425 ins = ins->next;
21426 } while(ins != first);
21427}
21428
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021429static void verify_piece(struct compile_state *state)
21430{
21431 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021432 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021433 ins = first;
21434 do {
21435 struct triple *ptr;
21436 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021437 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021438 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21439 if (ptr != LHS(ins, i)) {
21440 internal_error(state, ins, "malformed lhs on %s",
21441 tops(ins->op));
21442 }
21443 if (ptr->op != OP_PIECE) {
21444 internal_error(state, ins, "bad lhs op %s at %d on %s",
21445 tops(ptr->op), i, tops(ins->op));
21446 }
21447 if (ptr->u.cval != i) {
21448 internal_error(state, ins, "bad u.cval of %d %d expected",
21449 ptr->u.cval, i);
21450 }
21451 }
21452 ins = ins->next;
21453 } while(ins != first);
21454}
Eric Biederman83b991a2003-10-11 06:20:25 +000021455
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021456static void verify_ins_colors(struct compile_state *state)
21457{
21458 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021459
Eric Biederman83b991a2003-10-11 06:20:25 +000021460 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021461 ins = first;
21462 do {
21463 ins = ins->next;
21464 } while(ins != first);
21465}
Eric Biederman90089602004-05-28 14:11:54 +000021466
21467static void verify_unknown(struct compile_state *state)
21468{
21469 struct triple *first, *ins;
21470 if ( (unknown_triple.next != &unknown_triple) ||
21471 (unknown_triple.prev != &unknown_triple) ||
21472#if 0
21473 (unknown_triple.use != 0) ||
21474#endif
21475 (unknown_triple.op != OP_UNKNOWNVAL) ||
21476 (unknown_triple.lhs != 0) ||
21477 (unknown_triple.rhs != 0) ||
21478 (unknown_triple.misc != 0) ||
21479 (unknown_triple.targ != 0) ||
21480 (unknown_triple.template_id != 0) ||
21481 (unknown_triple.id != -1) ||
21482 (unknown_triple.type != &unknown_type) ||
21483 (unknown_triple.occurance != &dummy_occurance) ||
21484 (unknown_triple.param[0] != 0) ||
21485 (unknown_triple.param[1] != 0)) {
21486 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21487 }
21488 if ( (dummy_occurance.count != 2) ||
21489 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21490 (strcmp(dummy_occurance.function, "") != 0) ||
21491 (dummy_occurance.col != 0) ||
21492 (dummy_occurance.parent != 0)) {
21493 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21494 }
21495 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21496 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21497 }
21498 first = state->first;
21499 ins = first;
21500 do {
21501 int params, i;
21502 if (ins == &unknown_triple) {
21503 internal_error(state, ins, "unknown triple in list");
21504 }
21505 params = TRIPLE_SIZE(ins);
21506 for(i = 0; i < params; i++) {
21507 if (ins->param[i] == &unknown_triple) {
21508 internal_error(state, ins, "unknown triple used!");
21509 }
21510 }
21511 ins = ins->next;
21512 } while(ins != first);
21513}
21514
21515static void verify_types(struct compile_state *state)
21516{
21517 struct triple *first, *ins;
21518 first = state->first;
21519 ins = first;
21520 do {
21521 struct type *invalid;
21522 invalid = invalid_type(state, ins->type);
21523 if (invalid) {
21524 FILE *fp = state->errout;
21525 fprintf(fp, "type: ");
21526 name_of(fp, ins->type);
21527 fprintf(fp, "\n");
21528 fprintf(fp, "invalid type: ");
21529 name_of(fp, invalid);
21530 fprintf(fp, "\n");
21531 internal_error(state, ins, "invalid ins type");
21532 }
21533 } while(ins != first);
21534}
21535
21536static void verify_copy(struct compile_state *state)
21537{
21538 struct triple *first, *ins, *next;
21539 first = state->first;
21540 next = ins = first;
21541 do {
21542 ins = next;
21543 next = ins->next;
21544 if (ins->op != OP_COPY) {
21545 continue;
21546 }
21547 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21548 FILE *fp = state->errout;
21549 fprintf(fp, "src type: ");
21550 name_of(fp, RHS(ins, 0)->type);
21551 fprintf(fp, "\n");
21552 fprintf(fp, "dst type: ");
21553 name_of(fp, ins->type);
21554 fprintf(fp, "\n");
21555 internal_error(state, ins, "type mismatch in copy");
21556 }
21557 } while(next != first);
21558}
21559
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021560static void verify_consistency(struct compile_state *state)
21561{
Eric Biederman90089602004-05-28 14:11:54 +000021562 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021563 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021564 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021565 verify_blocks(state);
21566 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021567 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021568 verify_piece(state);
21569 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021570 verify_types(state);
21571 verify_copy(state);
21572 if (state->compiler->debug & DEBUG_VERIFICATION) {
21573 fprintf(state->dbgout, "consistency verified\n");
21574 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021575}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021576#else
Eric Biederman153ea352003-06-20 14:43:20 +000021577static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021578#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021579
21580static void optimize(struct compile_state *state)
21581{
Eric Biederman90089602004-05-28 14:11:54 +000021582 /* Join all of the functions into one giant function */
21583 join_functions(state);
21584
Eric Biederman5ade04a2003-10-22 04:03:46 +000021585 /* Dump what the instruction graph intially looks like */
21586 print_triples(state);
21587
Eric Biederman0babc1c2003-05-09 02:39:00 +000021588 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021589 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021590 print_triples(state);
21591
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021592 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021593 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021594 state->bb.first = state->first;
21595 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021596
Eric Biederman530b5192003-07-01 10:05:30 +000021597 /* Transform the code to ssa form. */
21598 /*
21599 * The transformation to ssa form puts a phi function
21600 * on each of edge of a dominance frontier where that
21601 * phi function might be needed. At -O2 if we don't
21602 * eleminate the excess phi functions we can get an
21603 * exponential code size growth. So I kill the extra
21604 * phi functions early and I kill them often.
21605 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021606 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021607 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021608
Eric Biederman83b991a2003-10-11 06:20:25 +000021609 /* Remove dead code */
21610 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021611 verify_consistency(state);
21612
Eric Biedermanb138ac82003-04-22 18:44:01 +000021613 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021614 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021615 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021616 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021617 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021618 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021619#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021620#warning "WISHLIST implement single use constants (least possible register pressure)"
21621#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021622#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021623 /* Select architecture instructions and an initial partial
21624 * coloring based on architecture constraints.
21625 */
21626 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021627 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021628
Eric Biederman83b991a2003-10-11 06:20:25 +000021629 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021630 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021631 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021632
Eric Biedermanb138ac82003-04-22 18:44:01 +000021633 /* Color all of the variables to see if they will fit in registers */
21634 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021635 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021636
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021637 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021638 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021639
Eric Biedermanb138ac82003-04-22 18:44:01 +000021640 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021641 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021642
Eric Biedermanb138ac82003-04-22 18:44:01 +000021643 /* Remove the optimization information.
21644 * This is more to check for memory consistency than to free memory.
21645 */
Eric Biederman90089602004-05-28 14:11:54 +000021646 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021647}
21648
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021649static void print_op_asm(struct compile_state *state,
21650 struct triple *ins, FILE *fp)
21651{
21652 struct asm_info *info;
21653 const char *ptr;
21654 unsigned lhs, rhs, i;
21655 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021656 lhs = ins->lhs;
21657 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021658 /* Don't count the clobbers in lhs */
21659 for(i = 0; i < lhs; i++) {
21660 if (LHS(ins, i)->type == &void_type) {
21661 break;
21662 }
21663 }
21664 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021665 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021666 fputc('\t', fp);
21667 for(ptr = info->str; *ptr; ptr++) {
21668 char *next;
21669 unsigned long param;
21670 struct triple *piece;
21671 if (*ptr != '%') {
21672 fputc(*ptr, fp);
21673 continue;
21674 }
21675 ptr++;
21676 if (*ptr == '%') {
21677 fputc('%', fp);
21678 continue;
21679 }
21680 param = strtoul(ptr, &next, 10);
21681 if (ptr == next) {
21682 error(state, ins, "Invalid asm template");
21683 }
21684 if (param >= (lhs + rhs)) {
21685 error(state, ins, "Invalid param %%%u in asm template",
21686 param);
21687 }
21688 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021689 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021690 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021691 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021692 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021693 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021694}
21695
21696
21697/* Only use the low x86 byte registers. This allows me
21698 * allocate the entire register when a byte register is used.
21699 */
21700#define X86_4_8BIT_GPRS 1
21701
Eric Biederman83b991a2003-10-11 06:20:25 +000021702/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021703#define X86_MMX_REGS (1<<0)
21704#define X86_XMM_REGS (1<<1)
21705#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021706
Eric Biedermanb138ac82003-04-22 18:44:01 +000021707/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021708#define REGC_FLAGS 0
21709#define REGC_GPR8 1
21710#define REGC_GPR16 2
21711#define REGC_GPR32 3
21712#define REGC_DIVIDEND64 4
21713#define REGC_DIVIDEND32 5
21714#define REGC_MMX 6
21715#define REGC_XMM 7
21716#define REGC_GPR32_8 8
21717#define REGC_GPR16_8 9
21718#define REGC_GPR8_LO 10
21719#define REGC_IMM32 11
21720#define REGC_IMM16 12
21721#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021722#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021723#if LAST_REGC >= MAX_REGC
21724#error "MAX_REGC is to low"
21725#endif
21726
21727/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021728#define REGCM_FLAGS (1 << REGC_FLAGS)
21729#define REGCM_GPR8 (1 << REGC_GPR8)
21730#define REGCM_GPR16 (1 << REGC_GPR16)
21731#define REGCM_GPR32 (1 << REGC_GPR32)
21732#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21733#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21734#define REGCM_MMX (1 << REGC_MMX)
21735#define REGCM_XMM (1 << REGC_XMM)
21736#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21737#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21738#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21739#define REGCM_IMM32 (1 << REGC_IMM32)
21740#define REGCM_IMM16 (1 << REGC_IMM16)
21741#define REGCM_IMM8 (1 << REGC_IMM8)
21742#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021743#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021744
21745/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021746#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021747#define REGC_FLAGS_FIRST REG_EFLAGS
21748#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021749#define REG_AL 3
21750#define REG_BL 4
21751#define REG_CL 5
21752#define REG_DL 6
21753#define REG_AH 7
21754#define REG_BH 8
21755#define REG_CH 9
21756#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021757#define REGC_GPR8_LO_FIRST REG_AL
21758#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021759#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021760#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021761#define REG_AX 11
21762#define REG_BX 12
21763#define REG_CX 13
21764#define REG_DX 14
21765#define REG_SI 15
21766#define REG_DI 16
21767#define REG_BP 17
21768#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021769#define REGC_GPR16_FIRST REG_AX
21770#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021771#define REG_EAX 19
21772#define REG_EBX 20
21773#define REG_ECX 21
21774#define REG_EDX 22
21775#define REG_ESI 23
21776#define REG_EDI 24
21777#define REG_EBP 25
21778#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021779#define REGC_GPR32_FIRST REG_EAX
21780#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021781#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021782#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21783#define REGC_DIVIDEND64_LAST REG_EDXEAX
21784#define REG_DXAX 28
21785#define REGC_DIVIDEND32_FIRST REG_DXAX
21786#define REGC_DIVIDEND32_LAST REG_DXAX
21787#define REG_MMX0 29
21788#define REG_MMX1 30
21789#define REG_MMX2 31
21790#define REG_MMX3 32
21791#define REG_MMX4 33
21792#define REG_MMX5 34
21793#define REG_MMX6 35
21794#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021795#define REGC_MMX_FIRST REG_MMX0
21796#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021797#define REG_XMM0 37
21798#define REG_XMM1 38
21799#define REG_XMM2 39
21800#define REG_XMM3 40
21801#define REG_XMM4 41
21802#define REG_XMM5 42
21803#define REG_XMM6 43
21804#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021805#define REGC_XMM_FIRST REG_XMM0
21806#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021807
21808#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021809#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021810#endif
21811
Eric Biedermanb138ac82003-04-22 18:44:01 +000021812#define LAST_REG REG_XMM7
21813
21814#define REGC_GPR32_8_FIRST REG_EAX
21815#define REGC_GPR32_8_LAST REG_EDX
21816#define REGC_GPR16_8_FIRST REG_AX
21817#define REGC_GPR16_8_LAST REG_DX
21818
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021819#define REGC_IMM8_FIRST -1
21820#define REGC_IMM8_LAST -1
21821#define REGC_IMM16_FIRST -2
21822#define REGC_IMM16_LAST -1
21823#define REGC_IMM32_FIRST -4
21824#define REGC_IMM32_LAST -1
21825
Eric Biedermanb138ac82003-04-22 18:44:01 +000021826#if LAST_REG >= MAX_REGISTERS
21827#error "MAX_REGISTERS to low"
21828#endif
21829
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021830
21831static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021832 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21833 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21834 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21835 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21836 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21837 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21838 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21839 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21840 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21841 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21842 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21843 [REGC_IMM32] = 0,
21844 [REGC_IMM16] = 0,
21845 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021846};
21847
21848static const struct {
21849 int first, last;
21850} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021851 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21852 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21853 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21854 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21855 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21856 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21857 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21858 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21859 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21860 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21861 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21862 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21863 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21864 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021865};
21866
Eric Biederman90089602004-05-28 14:11:54 +000021867#if ARCH_INPUT_REGS != 4
21868#error ARCH_INPUT_REGS size mismatch
21869#endif
21870static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21871 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21872 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21873 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21874 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21875};
21876
21877#if ARCH_OUTPUT_REGS != 4
21878#error ARCH_INPUT_REGS size mismatch
21879#endif
21880static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21881 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21882 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21883 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21884 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21885};
21886
Eric Biederman5ade04a2003-10-22 04:03:46 +000021887static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021888{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021889 memset(arch, 0, sizeof(*arch));
21890 arch->features = 0;
21891}
21892
Eric Biederman90089602004-05-28 14:11:54 +000021893static const struct compiler_flag arch_flags[] = {
21894 { "mmx", X86_MMX_REGS },
21895 { "sse", X86_XMM_REGS },
21896 { "noop-copy", X86_NOOP_COPY },
21897 { 0, 0 },
21898};
21899static const struct compiler_flag arch_cpus[] = {
21900 { "i386", 0 },
21901 { "p2", X86_MMX_REGS },
21902 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21903 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21904 { "k7", X86_MMX_REGS },
21905 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21906 { "c3", X86_MMX_REGS },
21907 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21908 { 0, 0 }
21909};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021910static int arch_encode_flag(struct arch_state *arch, const char *flag)
21911{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021912 int result;
21913 int act;
21914
21915 act = 1;
21916 result = -1;
21917 if (strncmp(flag, "no-", 3) == 0) {
21918 flag += 3;
21919 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021920 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021921 if (act && strncmp(flag, "cpu=", 4) == 0) {
21922 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021923 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021924 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021925 else {
Eric Biederman90089602004-05-28 14:11:54 +000021926 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021927 }
21928 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021929}
21930
Eric Biederman90089602004-05-28 14:11:54 +000021931static void arch_usage(FILE *fp)
21932{
21933 flag_usage(fp, arch_flags, "-m", "-mno-");
21934 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21935}
21936
Eric Biedermanb138ac82003-04-22 18:44:01 +000021937static unsigned arch_regc_size(struct compile_state *state, int class)
21938{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021939 if ((class < 0) || (class > LAST_REGC)) {
21940 return 0;
21941 }
21942 return regc_size[class];
21943}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021944
Eric Biedermanb138ac82003-04-22 18:44:01 +000021945static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21946{
21947 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021948 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021949 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021950 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021951
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021952 /* Special case for the immediates */
21953 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21954 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21955 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021956 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021957 return 0;
21958 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021959 return (regcm1 & regcm2) ||
21960 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21961}
21962
21963static void arch_reg_equivs(
21964 struct compile_state *state, unsigned *equiv, int reg)
21965{
21966 if ((reg < 0) || (reg > LAST_REG)) {
21967 internal_error(state, 0, "invalid register");
21968 }
21969 *equiv++ = reg;
21970 switch(reg) {
21971 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021972#if X86_4_8BIT_GPRS
21973 *equiv++ = REG_AH;
21974#endif
21975 *equiv++ = REG_AX;
21976 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021977 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021978 *equiv++ = REG_EDXEAX;
21979 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021980 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021981#if X86_4_8BIT_GPRS
21982 *equiv++ = REG_AL;
21983#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021984 *equiv++ = REG_AX;
21985 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021986 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021987 *equiv++ = REG_EDXEAX;
21988 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021989 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021990#if X86_4_8BIT_GPRS
21991 *equiv++ = REG_BH;
21992#endif
21993 *equiv++ = REG_BX;
21994 *equiv++ = REG_EBX;
21995 break;
21996
Eric Biedermanb138ac82003-04-22 18:44:01 +000021997 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021998#if X86_4_8BIT_GPRS
21999 *equiv++ = REG_BL;
22000#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022001 *equiv++ = REG_BX;
22002 *equiv++ = REG_EBX;
22003 break;
22004 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022005#if X86_4_8BIT_GPRS
22006 *equiv++ = REG_CH;
22007#endif
22008 *equiv++ = REG_CX;
22009 *equiv++ = REG_ECX;
22010 break;
22011
Eric Biedermanb138ac82003-04-22 18:44:01 +000022012 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022013#if X86_4_8BIT_GPRS
22014 *equiv++ = REG_CL;
22015#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022016 *equiv++ = REG_CX;
22017 *equiv++ = REG_ECX;
22018 break;
22019 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022020#if X86_4_8BIT_GPRS
22021 *equiv++ = REG_DH;
22022#endif
22023 *equiv++ = REG_DX;
22024 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022025 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022026 *equiv++ = REG_EDXEAX;
22027 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022028 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022029#if X86_4_8BIT_GPRS
22030 *equiv++ = REG_DL;
22031#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022032 *equiv++ = REG_DX;
22033 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022034 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022035 *equiv++ = REG_EDXEAX;
22036 break;
22037 case REG_AX:
22038 *equiv++ = REG_AL;
22039 *equiv++ = REG_AH;
22040 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022041 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022042 *equiv++ = REG_EDXEAX;
22043 break;
22044 case REG_BX:
22045 *equiv++ = REG_BL;
22046 *equiv++ = REG_BH;
22047 *equiv++ = REG_EBX;
22048 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022049 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022050 *equiv++ = REG_CL;
22051 *equiv++ = REG_CH;
22052 *equiv++ = REG_ECX;
22053 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022054 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022055 *equiv++ = REG_DL;
22056 *equiv++ = REG_DH;
22057 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022058 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022059 *equiv++ = REG_EDXEAX;
22060 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022061 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022062 *equiv++ = REG_ESI;
22063 break;
22064 case REG_DI:
22065 *equiv++ = REG_EDI;
22066 break;
22067 case REG_BP:
22068 *equiv++ = REG_EBP;
22069 break;
22070 case REG_SP:
22071 *equiv++ = REG_ESP;
22072 break;
22073 case REG_EAX:
22074 *equiv++ = REG_AL;
22075 *equiv++ = REG_AH;
22076 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022077 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022078 *equiv++ = REG_EDXEAX;
22079 break;
22080 case REG_EBX:
22081 *equiv++ = REG_BL;
22082 *equiv++ = REG_BH;
22083 *equiv++ = REG_BX;
22084 break;
22085 case REG_ECX:
22086 *equiv++ = REG_CL;
22087 *equiv++ = REG_CH;
22088 *equiv++ = REG_CX;
22089 break;
22090 case REG_EDX:
22091 *equiv++ = REG_DL;
22092 *equiv++ = REG_DH;
22093 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022094 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022095 *equiv++ = REG_EDXEAX;
22096 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022097 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022098 *equiv++ = REG_SI;
22099 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022100 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022101 *equiv++ = REG_DI;
22102 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022103 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022104 *equiv++ = REG_BP;
22105 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022106 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022107 *equiv++ = REG_SP;
22108 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022109 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022110 *equiv++ = REG_AL;
22111 *equiv++ = REG_AH;
22112 *equiv++ = REG_DL;
22113 *equiv++ = REG_DH;
22114 *equiv++ = REG_AX;
22115 *equiv++ = REG_DX;
22116 *equiv++ = REG_EAX;
22117 *equiv++ = REG_EDX;
22118 *equiv++ = REG_EDXEAX;
22119 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022120 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022121 *equiv++ = REG_AL;
22122 *equiv++ = REG_AH;
22123 *equiv++ = REG_DL;
22124 *equiv++ = REG_DH;
22125 *equiv++ = REG_AX;
22126 *equiv++ = REG_DX;
22127 *equiv++ = REG_EAX;
22128 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022129 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022130 break;
22131 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022132 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022133}
22134
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022135static unsigned arch_avail_mask(struct compile_state *state)
22136{
22137 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022138 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022139 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22140 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022141 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022142 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022143 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022144 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022145 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022146 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022147 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022148 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022149 return avail_mask;
22150}
22151
22152static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22153{
22154 unsigned mask, result;
22155 int class, class2;
22156 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022157
22158 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22159 if ((result & mask) == 0) {
22160 continue;
22161 }
22162 if (class > LAST_REGC) {
22163 result &= ~mask;
22164 }
22165 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22166 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22167 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22168 result |= (1 << class2);
22169 }
22170 }
22171 }
Eric Biederman530b5192003-07-01 10:05:30 +000022172 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022173 return result;
22174}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022175
Eric Biedermand1ea5392003-06-28 06:49:45 +000022176static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22177{
22178 /* Like arch_regcm_normalize except immediate register classes are excluded */
22179 regcm = arch_regcm_normalize(state, regcm);
22180 /* Remove the immediate register classes */
22181 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22182 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022183
Eric Biedermand1ea5392003-06-28 06:49:45 +000022184}
22185
Eric Biedermanb138ac82003-04-22 18:44:01 +000022186static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22187{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022188 unsigned mask;
22189 int class;
22190 mask = 0;
22191 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022192 if ((reg >= regcm_bound[class].first) &&
22193 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022194 mask |= (1 << class);
22195 }
22196 }
22197 if (!mask) {
22198 internal_error(state, 0, "reg %d not in any class", reg);
22199 }
22200 return mask;
22201}
22202
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022203static struct reg_info arch_reg_constraint(
22204 struct compile_state *state, struct type *type, const char *constraint)
22205{
22206 static const struct {
22207 char class;
22208 unsigned int mask;
22209 unsigned int reg;
22210 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022211 { 'r', REGCM_GPR32, REG_UNSET },
22212 { 'g', REGCM_GPR32, REG_UNSET },
22213 { 'p', REGCM_GPR32, REG_UNSET },
22214 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022215 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022216 { 'x', REGCM_XMM, REG_UNSET },
22217 { 'y', REGCM_MMX, REG_UNSET },
22218 { 'a', REGCM_GPR32, REG_EAX },
22219 { 'b', REGCM_GPR32, REG_EBX },
22220 { 'c', REGCM_GPR32, REG_ECX },
22221 { 'd', REGCM_GPR32, REG_EDX },
22222 { 'D', REGCM_GPR32, REG_EDI },
22223 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022224 { '\0', 0, REG_UNSET },
22225 };
22226 unsigned int regcm;
22227 unsigned int mask, reg;
22228 struct reg_info result;
22229 const char *ptr;
22230 regcm = arch_type_to_regcm(state, type);
22231 reg = REG_UNSET;
22232 mask = 0;
22233 for(ptr = constraint; *ptr; ptr++) {
22234 int i;
22235 if (*ptr == ' ') {
22236 continue;
22237 }
22238 for(i = 0; constraints[i].class != '\0'; i++) {
22239 if (constraints[i].class == *ptr) {
22240 break;
22241 }
22242 }
22243 if (constraints[i].class == '\0') {
22244 error(state, 0, "invalid register constraint ``%c''", *ptr);
22245 break;
22246 }
22247 if ((constraints[i].mask & regcm) == 0) {
22248 error(state, 0, "invalid register class %c specified",
22249 *ptr);
22250 }
22251 mask |= constraints[i].mask;
22252 if (constraints[i].reg != REG_UNSET) {
22253 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22254 error(state, 0, "Only one register may be specified");
22255 }
22256 reg = constraints[i].reg;
22257 }
22258 }
22259 result.reg = reg;
22260 result.regcm = mask;
22261 return result;
22262}
22263
22264static struct reg_info arch_reg_clobber(
22265 struct compile_state *state, const char *clobber)
22266{
22267 struct reg_info result;
22268 if (strcmp(clobber, "memory") == 0) {
22269 result.reg = REG_UNSET;
22270 result.regcm = 0;
22271 }
Eric Biederman90089602004-05-28 14:11:54 +000022272 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022273 result.reg = REG_EAX;
22274 result.regcm = REGCM_GPR32;
22275 }
Eric Biederman90089602004-05-28 14:11:54 +000022276 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022277 result.reg = REG_EBX;
22278 result.regcm = REGCM_GPR32;
22279 }
Eric Biederman90089602004-05-28 14:11:54 +000022280 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022281 result.reg = REG_ECX;
22282 result.regcm = REGCM_GPR32;
22283 }
Eric Biederman90089602004-05-28 14:11:54 +000022284 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022285 result.reg = REG_EDX;
22286 result.regcm = REGCM_GPR32;
22287 }
Eric Biederman90089602004-05-28 14:11:54 +000022288 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022289 result.reg = REG_ESI;
22290 result.regcm = REGCM_GPR32;
22291 }
Eric Biederman90089602004-05-28 14:11:54 +000022292 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022293 result.reg = REG_EDI;
22294 result.regcm = REGCM_GPR32;
22295 }
Eric Biederman90089602004-05-28 14:11:54 +000022296 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022297 result.reg = REG_EBP;
22298 result.regcm = REGCM_GPR32;
22299 }
Eric Biederman90089602004-05-28 14:11:54 +000022300 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022301 result.reg = REG_ESP;
22302 result.regcm = REGCM_GPR32;
22303 }
22304 else if (strcmp(clobber, "cc") == 0) {
22305 result.reg = REG_EFLAGS;
22306 result.regcm = REGCM_FLAGS;
22307 }
22308 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22309 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22310 result.reg = REG_XMM0 + octdigval(clobber[3]);
22311 result.regcm = REGCM_XMM;
22312 }
Eric Biederman90089602004-05-28 14:11:54 +000022313 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022314 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22315 result.reg = REG_MMX0 + octdigval(clobber[3]);
22316 result.regcm = REGCM_MMX;
22317 }
22318 else {
Eric Biederman90089602004-05-28 14:11:54 +000022319 error(state, 0, "unknown register name `%s' in asm",
22320 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022321 result.reg = REG_UNSET;
22322 result.regcm = 0;
22323 }
22324 return result;
22325}
22326
Stefan Reinauer14e22772010-04-27 06:56:47 +000022327static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022328 char *used, int reg, unsigned classes)
22329{
22330 unsigned mask;
22331 if (used[reg]) {
22332 return REG_UNSET;
22333 }
22334 mask = arch_reg_regcm(state, reg);
22335 return (classes & mask) ? reg : REG_UNSET;
22336}
22337
22338static int arch_select_free_register(
22339 struct compile_state *state, char *used, int classes)
22340{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022341 /* Live ranges with the most neighbors are colored first.
22342 *
22343 * Generally it does not matter which colors are given
22344 * as the register allocator attempts to color live ranges
22345 * in an order where you are guaranteed not to run out of colors.
22346 *
22347 * Occasionally the register allocator cannot find an order
22348 * of register selection that will find a free color. To
22349 * increase the odds the register allocator will work when
22350 * it guesses first give out registers from register classes
22351 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022352 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022353 */
22354 int i, reg;
22355 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022356 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022357 reg = do_select_reg(state, used, i, classes);
22358 }
22359 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22360 reg = do_select_reg(state, used, i, classes);
22361 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022362 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022363 reg = do_select_reg(state, used, i, classes);
22364 }
22365 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22366 reg = do_select_reg(state, used, i, classes);
22367 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022368 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22369 reg = do_select_reg(state, used, i, classes);
22370 }
Eric Biederman530b5192003-07-01 10:05:30 +000022371 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22372 reg = do_select_reg(state, used, i, classes);
22373 }
22374 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22375 reg = do_select_reg(state, used, i, classes);
22376 }
22377 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378 reg = do_select_reg(state, used, i, classes);
22379 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022380 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22381 reg = do_select_reg(state, used, i, classes);
22382 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022383 return reg;
22384}
22385
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022386
Stefan Reinauer14e22772010-04-27 06:56:47 +000022387static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022388{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022389
22390#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022392#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022393 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022394 mask = 0;
22395 switch(type->type & TYPE_MASK) {
22396 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022397 case TYPE_VOID:
22398 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022399 break;
22400 case TYPE_CHAR:
22401 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022402 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022403 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022404 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022405 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022406 REGCM_MMX | REGCM_XMM |
22407 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022408 break;
22409 case TYPE_SHORT:
22410 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022411 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022412 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022413 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022414 REGCM_MMX | REGCM_XMM |
22415 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022416 break;
Eric Biederman90089602004-05-28 14:11:54 +000022417 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022418 case TYPE_INT:
22419 case TYPE_UINT:
22420 case TYPE_LONG:
22421 case TYPE_ULONG:
22422 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022423 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022424 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22425 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022426 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022427 break;
Eric Biederman90089602004-05-28 14:11:54 +000022428 case TYPE_JOIN:
22429 case TYPE_UNION:
22430 mask = arch_type_to_regcm(state, type->left);
22431 break;
22432 case TYPE_OVERLAP:
22433 mask = arch_type_to_regcm(state, type->left) &
22434 arch_type_to_regcm(state, type->right);
22435 break;
22436 case TYPE_BITFIELD:
22437 mask = arch_type_to_regcm(state, type->left);
22438 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022439 default:
Eric Biederman90089602004-05-28 14:11:54 +000022440 fprintf(state->errout, "type: ");
22441 name_of(state->errout, type);
22442 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022443 internal_error(state, 0, "no register class for type");
22444 break;
22445 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022446 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022447 return mask;
22448}
22449
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022450static int is_imm32(struct triple *imm)
22451{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022452 // second condition commented out to prevent compiler warning:
22453 // imm->u.cval is always 32bit unsigned, so the comparison is
22454 // always true.
22455 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022456 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022457
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022458}
22459static int is_imm16(struct triple *imm)
22460{
22461 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22462}
22463static int is_imm8(struct triple *imm)
22464{
22465 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22466}
22467
22468static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022469{
22470 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022471 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022472 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022473 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022474 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022475 if (!is_imm32(imm)) {
22476 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022477 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022478 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022479 use_triple(imm, ins);
22480 *expr = imm;
22481 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022482}
22483
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022484static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022485{
22486 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022487 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022488 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022489 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022490 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022491 if (!is_imm8(imm)) {
22492 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022493 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022494 unuse_triple(*expr, ins);
22495 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022496 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022497 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022498}
22499
Eric Biederman530b5192003-07-01 10:05:30 +000022500#define TEMPLATE_NOP 0
22501#define TEMPLATE_INTCONST8 1
22502#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022503#define TEMPLATE_UNKNOWNVAL 3
22504#define TEMPLATE_COPY8_REG 5
22505#define TEMPLATE_COPY16_REG 6
22506#define TEMPLATE_COPY32_REG 7
22507#define TEMPLATE_COPY_IMM8 8
22508#define TEMPLATE_COPY_IMM16 9
22509#define TEMPLATE_COPY_IMM32 10
22510#define TEMPLATE_PHI8 11
22511#define TEMPLATE_PHI16 12
22512#define TEMPLATE_PHI32 13
22513#define TEMPLATE_STORE8 14
22514#define TEMPLATE_STORE16 15
22515#define TEMPLATE_STORE32 16
22516#define TEMPLATE_LOAD8 17
22517#define TEMPLATE_LOAD16 18
22518#define TEMPLATE_LOAD32 19
22519#define TEMPLATE_BINARY8_REG 20
22520#define TEMPLATE_BINARY16_REG 21
22521#define TEMPLATE_BINARY32_REG 22
22522#define TEMPLATE_BINARY8_IMM 23
22523#define TEMPLATE_BINARY16_IMM 24
22524#define TEMPLATE_BINARY32_IMM 25
22525#define TEMPLATE_SL8_CL 26
22526#define TEMPLATE_SL16_CL 27
22527#define TEMPLATE_SL32_CL 28
22528#define TEMPLATE_SL8_IMM 29
22529#define TEMPLATE_SL16_IMM 30
22530#define TEMPLATE_SL32_IMM 31
22531#define TEMPLATE_UNARY8 32
22532#define TEMPLATE_UNARY16 33
22533#define TEMPLATE_UNARY32 34
22534#define TEMPLATE_CMP8_REG 35
22535#define TEMPLATE_CMP16_REG 36
22536#define TEMPLATE_CMP32_REG 37
22537#define TEMPLATE_CMP8_IMM 38
22538#define TEMPLATE_CMP16_IMM 39
22539#define TEMPLATE_CMP32_IMM 40
22540#define TEMPLATE_TEST8 41
22541#define TEMPLATE_TEST16 42
22542#define TEMPLATE_TEST32 43
22543#define TEMPLATE_SET 44
22544#define TEMPLATE_JMP 45
22545#define TEMPLATE_RET 46
22546#define TEMPLATE_INB_DX 47
22547#define TEMPLATE_INB_IMM 48
22548#define TEMPLATE_INW_DX 49
22549#define TEMPLATE_INW_IMM 50
22550#define TEMPLATE_INL_DX 51
22551#define TEMPLATE_INL_IMM 52
22552#define TEMPLATE_OUTB_DX 53
22553#define TEMPLATE_OUTB_IMM 54
22554#define TEMPLATE_OUTW_DX 55
22555#define TEMPLATE_OUTW_IMM 56
22556#define TEMPLATE_OUTL_DX 57
22557#define TEMPLATE_OUTL_IMM 58
22558#define TEMPLATE_BSF 59
22559#define TEMPLATE_RDMSR 60
22560#define TEMPLATE_WRMSR 61
22561#define TEMPLATE_UMUL8 62
22562#define TEMPLATE_UMUL16 63
22563#define TEMPLATE_UMUL32 64
22564#define TEMPLATE_DIV8 65
22565#define TEMPLATE_DIV16 66
22566#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022567#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022568#if LAST_TEMPLATE >= MAX_TEMPLATES
22569#error "MAX_TEMPLATES to low"
22570#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022571
Eric Biederman530b5192003-07-01 10:05:30 +000022572#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 +000022573#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022574#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022575
Eric Biedermanb138ac82003-04-22 18:44:01 +000022576
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022577static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022578 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022579 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022580 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22639 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22640 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22641 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22642 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22643 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22644 },
22645 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022646 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022647 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22648 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022649 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022650 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22651 },
Eric Biederman90089602004-05-28 14:11:54 +000022652 [TEMPLATE_UNKNOWNVAL] = {
22653 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22654 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022655 [TEMPLATE_COPY8_REG] = {
22656 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22657 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022658 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022659 [TEMPLATE_COPY16_REG] = {
22660 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22661 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22662 },
22663 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022664 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022665 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022666 },
22667 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022668 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022669 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22670 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022671 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022672 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022673 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22674 },
22675 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022676 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022677 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22678 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022679 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022680 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022681 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22682 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022683 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022684 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022685 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022686 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022687 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022688 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022689 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022690 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022691 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022692 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022693 [0] = { REG_UNSET, REGCM_GPR32 },
22694 [1] = { REG_UNSET, REGCM_GPR8_LO },
22695 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022696 },
22697 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022698 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022699 [0] = { REG_UNSET, REGCM_GPR32 },
22700 [1] = { REG_UNSET, REGCM_GPR16 },
22701 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022702 },
22703 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022704 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022705 [0] = { REG_UNSET, REGCM_GPR32 },
22706 [1] = { REG_UNSET, REGCM_GPR32 },
22707 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022708 },
22709 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022710 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022711 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22712 },
22713 [TEMPLATE_LOAD16] = {
22714 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22715 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22716 },
22717 [TEMPLATE_LOAD32] = {
22718 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22719 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22720 },
Eric Biederman530b5192003-07-01 10:05:30 +000022721 [TEMPLATE_BINARY8_REG] = {
22722 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022723 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022724 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22725 [1] = { REG_UNSET, REGCM_GPR8_LO },
22726 },
22727 },
22728 [TEMPLATE_BINARY16_REG] = {
22729 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022730 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022731 [0] = { REG_VIRT0, REGCM_GPR16 },
22732 [1] = { REG_UNSET, REGCM_GPR16 },
22733 },
22734 },
22735 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022736 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022737 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022738 [0] = { REG_VIRT0, REGCM_GPR32 },
22739 [1] = { REG_UNSET, REGCM_GPR32 },
22740 },
22741 },
Eric Biederman530b5192003-07-01 10:05:30 +000022742 [TEMPLATE_BINARY8_IMM] = {
22743 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022744 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022745 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22746 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22747 },
22748 },
22749 [TEMPLATE_BINARY16_IMM] = {
22750 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022751 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022752 [0] = { REG_VIRT0, REGCM_GPR16 },
22753 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22754 },
22755 },
22756 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022757 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022758 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022759 [0] = { REG_VIRT0, REGCM_GPR32 },
22760 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22761 },
22762 },
Eric Biederman530b5192003-07-01 10:05:30 +000022763 [TEMPLATE_SL8_CL] = {
22764 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022765 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022766 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22767 [1] = { REG_CL, REGCM_GPR8_LO },
22768 },
22769 },
22770 [TEMPLATE_SL16_CL] = {
22771 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022772 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022773 [0] = { REG_VIRT0, REGCM_GPR16 },
22774 [1] = { REG_CL, REGCM_GPR8_LO },
22775 },
22776 },
22777 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022778 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022779 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022780 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022781 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022782 },
22783 },
Eric Biederman530b5192003-07-01 10:05:30 +000022784 [TEMPLATE_SL8_IMM] = {
22785 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022786 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022787 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22788 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22789 },
22790 },
22791 [TEMPLATE_SL16_IMM] = {
22792 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022793 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022794 [0] = { REG_VIRT0, REGCM_GPR16 },
22795 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22796 },
22797 },
22798 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022799 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022800 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022801 [0] = { REG_VIRT0, REGCM_GPR32 },
22802 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22803 },
22804 },
Eric Biederman530b5192003-07-01 10:05:30 +000022805 [TEMPLATE_UNARY8] = {
22806 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22807 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22808 },
22809 [TEMPLATE_UNARY16] = {
22810 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22811 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22812 },
22813 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022814 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22815 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22816 },
Eric Biederman530b5192003-07-01 10:05:30 +000022817 [TEMPLATE_CMP8_REG] = {
22818 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22819 .rhs = {
22820 [0] = { REG_UNSET, REGCM_GPR8_LO },
22821 [1] = { REG_UNSET, REGCM_GPR8_LO },
22822 },
22823 },
22824 [TEMPLATE_CMP16_REG] = {
22825 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22826 .rhs = {
22827 [0] = { REG_UNSET, REGCM_GPR16 },
22828 [1] = { REG_UNSET, REGCM_GPR16 },
22829 },
22830 },
22831 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022832 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22833 .rhs = {
22834 [0] = { REG_UNSET, REGCM_GPR32 },
22835 [1] = { REG_UNSET, REGCM_GPR32 },
22836 },
22837 },
Eric Biederman530b5192003-07-01 10:05:30 +000022838 [TEMPLATE_CMP8_IMM] = {
22839 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22840 .rhs = {
22841 [0] = { REG_UNSET, REGCM_GPR8_LO },
22842 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22843 },
22844 },
22845 [TEMPLATE_CMP16_IMM] = {
22846 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22847 .rhs = {
22848 [0] = { REG_UNSET, REGCM_GPR16 },
22849 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22850 },
22851 },
22852 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022853 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22854 .rhs = {
22855 [0] = { REG_UNSET, REGCM_GPR32 },
22856 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22857 },
22858 },
Eric Biederman530b5192003-07-01 10:05:30 +000022859 [TEMPLATE_TEST8] = {
22860 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22861 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22862 },
22863 [TEMPLATE_TEST16] = {
22864 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22865 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22866 },
22867 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022868 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22869 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22870 },
22871 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022872 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022873 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22874 },
22875 [TEMPLATE_JMP] = {
22876 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22877 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022878 [TEMPLATE_RET] = {
22879 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22880 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022881 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022882 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022883 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22884 },
22885 [TEMPLATE_INB_IMM] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022886 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022887 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22888 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022889 [TEMPLATE_INW_DX] = {
22890 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022891 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22892 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022893 [TEMPLATE_INW_IMM] = {
22894 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022895 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22896 },
22897 [TEMPLATE_INL_DX] = {
22898 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22899 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22900 },
22901 [TEMPLATE_INL_IMM] = {
22902 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22903 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22904 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022905 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022906 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022907 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022908 [1] = { REG_DX, REGCM_GPR16 },
22909 },
22910 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022911 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022912 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022913 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022914 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22915 },
22916 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022917 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022918 .rhs = {
22919 [0] = { REG_AX, REGCM_GPR16 },
22920 [1] = { REG_DX, REGCM_GPR16 },
22921 },
22922 },
22923 [TEMPLATE_OUTW_IMM] = {
22924 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022925 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022926 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22927 },
22928 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022929 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022930 .rhs = {
22931 [0] = { REG_EAX, REGCM_GPR32 },
22932 [1] = { REG_DX, REGCM_GPR16 },
22933 },
22934 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022935 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022936 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022937 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022938 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22939 },
22940 },
22941 [TEMPLATE_BSF] = {
22942 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22943 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22944 },
22945 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022946 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022947 [0] = { REG_EAX, REGCM_GPR32 },
22948 [1] = { REG_EDX, REGCM_GPR32 },
22949 },
22950 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22951 },
22952 [TEMPLATE_WRMSR] = {
22953 .rhs = {
22954 [0] = { REG_ECX, REGCM_GPR32 },
22955 [1] = { REG_EAX, REGCM_GPR32 },
22956 [2] = { REG_EDX, REGCM_GPR32 },
22957 },
22958 },
Eric Biederman530b5192003-07-01 10:05:30 +000022959 [TEMPLATE_UMUL8] = {
22960 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022961 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022962 [0] = { REG_AL, REGCM_GPR8_LO },
22963 [1] = { REG_UNSET, REGCM_GPR8_LO },
22964 },
22965 },
22966 [TEMPLATE_UMUL16] = {
22967 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022968 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022969 [0] = { REG_AX, REGCM_GPR16 },
22970 [1] = { REG_UNSET, REGCM_GPR16 },
22971 },
22972 },
22973 [TEMPLATE_UMUL32] = {
22974 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022975 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022976 [0] = { REG_EAX, REGCM_GPR32 },
22977 [1] = { REG_UNSET, REGCM_GPR32 },
22978 },
22979 },
Eric Biederman530b5192003-07-01 10:05:30 +000022980 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022981 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022982 [0] = { REG_AL, REGCM_GPR8_LO },
22983 [1] = { REG_AH, REGCM_GPR8 },
22984 },
22985 .rhs = {
22986 [0] = { REG_AX, REGCM_GPR16 },
22987 [1] = { REG_UNSET, REGCM_GPR8_LO },
22988 },
22989 },
22990 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022991 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022992 [0] = { REG_AX, REGCM_GPR16 },
22993 [1] = { REG_DX, REGCM_GPR16 },
22994 },
22995 .rhs = {
22996 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22997 [1] = { REG_UNSET, REGCM_GPR16 },
22998 },
22999 },
23000 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023001 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023002 [0] = { REG_EAX, REGCM_GPR32 },
23003 [1] = { REG_EDX, REGCM_GPR32 },
23004 },
23005 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023006 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023007 [1] = { REG_UNSET, REGCM_GPR32 },
23008 },
23009 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023010};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023011
Eric Biederman83b991a2003-10-11 06:20:25 +000023012static void fixup_branch(struct compile_state *state,
23013 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23014 struct triple *left, struct triple *right)
23015{
23016 struct triple *test;
23017 if (!left) {
23018 internal_error(state, branch, "no branch test?");
23019 }
23020 test = pre_triple(state, branch,
23021 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023022 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023023 if (cmp_op == OP_CMP) {
23024 test->template_id = TEMPLATE_CMP32_REG;
23025 if (get_imm32(test, &RHS(test, 1))) {
23026 test->template_id = TEMPLATE_CMP32_IMM;
23027 }
23028 }
23029 use_triple(RHS(test, 0), test);
23030 use_triple(RHS(test, 1), test);
23031 unuse_triple(RHS(branch, 0), branch);
23032 RHS(branch, 0) = test;
23033 branch->op = jmp_op;
23034 branch->template_id = TEMPLATE_JMP;
23035 use_triple(RHS(branch, 0), branch);
23036}
23037
Eric Biedermanb138ac82003-04-22 18:44:01 +000023038static void fixup_branches(struct compile_state *state,
23039 struct triple *cmp, struct triple *use, int jmp_op)
23040{
23041 struct triple_set *entry, *next;
23042 for(entry = use->use; entry; entry = next) {
23043 next = entry->next;
23044 if (entry->member->op == OP_COPY) {
23045 fixup_branches(state, cmp, entry->member, jmp_op);
23046 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023047 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023048 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023049 struct triple *left, *right;
23050 left = right = 0;
23051 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023052 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023053 right = RHS(cmp, 1);
23054 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023055 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023056 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023057 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023058 }
23059 }
23060}
23061
Stefan Reinauer14e22772010-04-27 06:56:47 +000023062static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023063 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23064{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023065 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023066 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023067
23068 /* Put a barrier up before the cmp which preceeds the
23069 * copy instruction. If a set actually occurs this gives
23070 * us a chance to move variables in registers out of the way.
23071 */
23072
23073 /* Modify the comparison operator */
23074 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023075 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023076 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023077 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023078 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023079 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023080 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023081 }
23082 /* Generate the instruction sequence that will transform the
23083 * result of the comparison into a logical value.
23084 */
Eric Biederman90089602004-05-28 14:11:54 +000023085 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023086 use_triple(ins, set);
23087 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023088
Eric Biederman90089602004-05-28 14:11:54 +000023089 convert = set;
23090 if (!equiv_types(ins->type, set->type)) {
23091 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23092 use_triple(set, convert);
23093 convert->template_id = TEMPLATE_COPY32_REG;
23094 }
23095
Eric Biedermanb138ac82003-04-22 18:44:01 +000023096 for(entry = ins->use; entry; entry = next) {
23097 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023098 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023099 continue;
23100 }
Eric Biederman90089602004-05-28 14:11:54 +000023101 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023102 }
Eric Biederman90089602004-05-28 14:11:54 +000023103 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023104}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023105
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023106struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23107{
23108 struct ins_template *template;
23109 struct reg_info result;
23110 int zlhs;
23111 if (ins->op == OP_PIECE) {
23112 index = ins->u.cval;
23113 ins = MISC(ins, 0);
23114 }
Eric Biederman90089602004-05-28 14:11:54 +000023115 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023116 if (triple_is_def(state, ins)) {
23117 zlhs = 1;
23118 }
23119 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023120 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023121 index, tops(ins->op));
23122 }
23123 switch(ins->op) {
23124 case OP_ASM:
23125 template = &ins->u.ainfo->tmpl;
23126 break;
23127 default:
23128 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023129 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023130 ins->template_id);
23131 }
23132 template = &templates[ins->template_id];
23133 break;
23134 }
23135 result = template->lhs[index];
23136 result.regcm = arch_regcm_normalize(state, result.regcm);
23137 if (result.reg != REG_UNNEEDED) {
23138 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23139 }
23140 if (result.regcm == 0) {
23141 internal_error(state, ins, "lhs %d regcm == 0", index);
23142 }
23143 return result;
23144}
23145
23146struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23147{
23148 struct reg_info result;
23149 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023150 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023151 (ins->op == OP_PIECE)) {
23152 internal_error(state, ins, "index %d out of range for %s\n",
23153 index, tops(ins->op));
23154 }
23155 switch(ins->op) {
23156 case OP_ASM:
23157 template = &ins->u.ainfo->tmpl;
23158 break;
Eric Biederman90089602004-05-28 14:11:54 +000023159 case OP_PHI:
23160 index = 0;
23161 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023162 default:
23163 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023164 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023165 ins->template_id);
23166 }
23167 template = &templates[ins->template_id];
23168 break;
23169 }
23170 result = template->rhs[index];
23171 result.regcm = arch_regcm_normalize(state, result.regcm);
23172 if (result.regcm == 0) {
23173 internal_error(state, ins, "rhs %d regcm == 0", index);
23174 }
23175 return result;
23176}
23177
Eric Biederman530b5192003-07-01 10:05:30 +000023178static struct triple *mod_div(struct compile_state *state,
23179 struct triple *ins, int div_op, int index)
23180{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023181 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023182
Eric Biederman530b5192003-07-01 10:05:30 +000023183 /* Generate the appropriate division instruction */
23184 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23185 RHS(div, 0) = RHS(ins, 0);
23186 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023187 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023188 div->template_id = TEMPLATE_DIV32;
23189 use_triple(RHS(div, 0), div);
23190 use_triple(RHS(div, 1), div);
23191 use_triple(LHS(div, 0), div);
23192 use_triple(LHS(div, 1), div);
23193
Eric Biederman530b5192003-07-01 10:05:30 +000023194 /* Replate uses of ins with the appropriate piece of the div */
23195 propogate_use(state, ins, LHS(div, index));
23196 release_triple(state, ins);
23197
23198 /* Return the address of the next instruction */
23199 return piece1->next;
23200}
23201
Eric Biederman90089602004-05-28 14:11:54 +000023202static int noop_adecl(struct triple *adecl)
23203{
23204 struct triple_set *use;
23205 /* It's a noop if it doesn't specify stoorage */
23206 if (adecl->lhs == 0) {
23207 return 1;
23208 }
23209 /* Is the adecl used? If not it's a noop */
23210 for(use = adecl->use; use ; use = use->next) {
23211 if ((use->member->op != OP_PIECE) ||
23212 (MISC(use->member, 0) != adecl)) {
23213 return 0;
23214 }
23215 }
23216 return 1;
23217}
23218
23219static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23220{
23221 struct triple *mask, *nmask, *shift;
23222 struct triple *val, *val_mask, *val_shift;
23223 struct triple *targ, *targ_mask;
23224 struct triple *new;
23225 ulong_t the_mask, the_nmask;
23226
23227 targ = RHS(ins, 0);
23228 val = RHS(ins, 1);
23229
23230 /* Get constant for the mask value */
23231 the_mask = 1;
23232 the_mask <<= ins->u.bitfield.size;
23233 the_mask -= 1;
23234 the_mask <<= ins->u.bitfield.offset;
23235 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23236 mask->u.cval = the_mask;
23237
23238 /* Get the inverted mask value */
23239 the_nmask = ~the_mask;
23240 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23241 nmask->u.cval = the_nmask;
23242
23243 /* Get constant for the shift value */
23244 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23245 shift->u.cval = ins->u.bitfield.offset;
23246
23247 /* Shift and mask the source value */
23248 val_shift = val;
23249 if (shift->u.cval != 0) {
23250 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23251 use_triple(val, val_shift);
23252 use_triple(shift, val_shift);
23253 }
23254 val_mask = val_shift;
23255 if (is_signed(val->type)) {
23256 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23257 use_triple(val_shift, val_mask);
23258 use_triple(mask, val_mask);
23259 }
23260
23261 /* Mask the target value */
23262 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23263 use_triple(targ, targ_mask);
23264 use_triple(nmask, targ_mask);
23265
23266 /* Now combined them together */
23267 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23268 use_triple(targ_mask, new);
23269 use_triple(val_mask, new);
23270
23271 /* Move all of the users over to the new expression */
23272 propogate_use(state, ins, new);
23273
23274 /* Delete the original triple */
23275 release_triple(state, ins);
23276
23277 /* Restart the transformation at mask */
23278 return mask;
23279}
23280
23281static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23282{
23283 struct triple *mask, *shift;
23284 struct triple *val, *val_mask, *val_shift;
23285 ulong_t the_mask;
23286
23287 val = RHS(ins, 0);
23288
23289 /* Get constant for the mask value */
23290 the_mask = 1;
23291 the_mask <<= ins->u.bitfield.size;
23292 the_mask -= 1;
23293 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23294 mask->u.cval = the_mask;
23295
23296 /* Get constant for the right shift value */
23297 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23298 shift->u.cval = ins->u.bitfield.offset;
23299
23300 /* Shift arithmetic right, to correct the sign */
23301 val_shift = val;
23302 if (shift->u.cval != 0) {
23303 int op;
23304 if (ins->op == OP_SEXTRACT) {
23305 op = OP_SSR;
23306 } else {
23307 op = OP_USR;
23308 }
23309 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23310 use_triple(val, val_shift);
23311 use_triple(shift, val_shift);
23312 }
23313
23314 /* Finally mask the value */
23315 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23316 use_triple(val_shift, val_mask);
23317 use_triple(mask, val_mask);
23318
23319 /* Move all of the users over to the new expression */
23320 propogate_use(state, ins, val_mask);
23321
23322 /* Release the original instruction */
23323 release_triple(state, ins);
23324
23325 return mask;
23326
23327}
23328
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023329static struct triple *transform_to_arch_instruction(
23330 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023331{
23332 /* Transform from generic 3 address instructions
23333 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023334 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023335 * Copies are inserted to preserve the register flexibility
23336 * of 3 address instructions.
23337 */
Eric Biederman90089602004-05-28 14:11:54 +000023338 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023339 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023340 next = ins->next;
23341 switch(ins->op) {
23342 case OP_INTCONST:
23343 ins->template_id = TEMPLATE_INTCONST32;
23344 if (ins->u.cval < 256) {
23345 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023346 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023347 break;
23348 case OP_ADDRCONST:
23349 ins->template_id = TEMPLATE_INTCONST32;
23350 break;
Eric Biederman90089602004-05-28 14:11:54 +000023351 case OP_UNKNOWNVAL:
23352 ins->template_id = TEMPLATE_UNKNOWNVAL;
23353 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023354 case OP_NOOP:
23355 case OP_SDECL:
23356 case OP_BLOBCONST:
23357 case OP_LABEL:
23358 ins->template_id = TEMPLATE_NOP;
23359 break;
23360 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023361 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023362 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023363 value = RHS(ins, 0);
23364 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023365 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023366 }
Eric Biederman90089602004-05-28 14:11:54 +000023367 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023368 ins->template_id = TEMPLATE_COPY_IMM16;
23369 }
Eric Biederman90089602004-05-28 14:11:54 +000023370 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023371 ins->template_id = TEMPLATE_COPY_IMM32;
23372 }
Eric Biederman90089602004-05-28 14:11:54 +000023373 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023374 internal_error(state, ins, "bad constant passed to copy");
23375 }
Eric Biederman90089602004-05-28 14:11:54 +000023376 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023377 ins->template_id = TEMPLATE_COPY8_REG;
23378 }
Eric Biederman90089602004-05-28 14:11:54 +000023379 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023380 ins->template_id = TEMPLATE_COPY16_REG;
23381 }
Eric Biederman90089602004-05-28 14:11:54 +000023382 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023383 ins->template_id = TEMPLATE_COPY32_REG;
23384 }
23385 else {
23386 internal_error(state, ins, "bad type passed to copy");
23387 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023388 break;
23389 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023390 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023391 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023392 ins->template_id = TEMPLATE_PHI8;
23393 }
Eric Biederman90089602004-05-28 14:11:54 +000023394 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023395 ins->template_id = TEMPLATE_PHI16;
23396 }
Eric Biederman90089602004-05-28 14:11:54 +000023397 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023398 ins->template_id = TEMPLATE_PHI32;
23399 }
23400 else {
23401 internal_error(state, ins, "bad type passed to phi");
23402 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023403 break;
Eric Biederman90089602004-05-28 14:11:54 +000023404 case OP_ADECL:
23405 /* Adecls should always be treated as dead code and
23406 * removed. If we are not optimizing they may linger.
23407 */
23408 if (!noop_adecl(ins)) {
23409 internal_error(state, ins, "adecl remains?");
23410 }
23411 ins->template_id = TEMPLATE_NOP;
23412 next = after_lhs(state, ins);
23413 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023414 case OP_STORE:
23415 switch(ins->type->type & TYPE_MASK) {
23416 case TYPE_CHAR: case TYPE_UCHAR:
23417 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023418 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023419 case TYPE_SHORT: case TYPE_USHORT:
23420 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023421 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023422 case TYPE_INT: case TYPE_UINT:
23423 case TYPE_LONG: case TYPE_ULONG:
23424 case TYPE_POINTER:
23425 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023426 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023427 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023428 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023429 break;
23430 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023431 break;
23432 case OP_LOAD:
23433 switch(ins->type->type & TYPE_MASK) {
23434 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023435 case TYPE_SHORT: case TYPE_USHORT:
23436 case TYPE_INT: case TYPE_UINT:
23437 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023438 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023439 break;
23440 default:
23441 internal_error(state, ins, "unknown type in load");
23442 break;
23443 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023444 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023445 break;
23446 case OP_ADD:
23447 case OP_SUB:
23448 case OP_AND:
23449 case OP_XOR:
23450 case OP_OR:
23451 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023452 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023453 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023454 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023455 }
23456 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023457 case OP_SDIVT:
23458 case OP_UDIVT:
23459 ins->template_id = TEMPLATE_DIV32;
23460 next = after_lhs(state, ins);
23461 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023462 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023463 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023464 break;
23465 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023466 next = mod_div(state, ins, OP_UDIVT, 0);
23467 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023468 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023469 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023470 break;
23471 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023472 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023473 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023474 case OP_SMOD:
23475 next = mod_div(state, ins, OP_SDIVT, 1);
23476 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023477 case OP_SL:
23478 case OP_SSR:
23479 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023480 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023481 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023482 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023483 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23484 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023485 }
23486 break;
23487 case OP_INVERT:
23488 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023489 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023490 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023491 case OP_EQ:
23492 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023493 break;
23494 case OP_NOTEQ:
23495 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23496 break;
23497 case OP_SLESS:
23498 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23499 break;
23500 case OP_ULESS:
23501 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23502 break;
23503 case OP_SMORE:
23504 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23505 break;
23506 case OP_UMORE:
23507 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23508 break;
23509 case OP_SLESSEQ:
23510 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23511 break;
23512 case OP_ULESSEQ:
23513 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23514 break;
23515 case OP_SMOREEQ:
23516 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23517 break;
23518 case OP_UMOREEQ:
23519 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23520 break;
23521 case OP_LTRUE:
23522 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23523 break;
23524 case OP_LFALSE:
23525 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23526 break;
23527 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023528 ins->op = OP_JMP;
23529 ins->template_id = TEMPLATE_NOP;
23530 break;
23531 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023532 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023533 RHS(ins, 0)->type, RHS(ins, 0), 0);
23534 break;
23535 case OP_CALL:
23536 ins->template_id = TEMPLATE_NOP;
23537 break;
23538 case OP_RET:
23539 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023540 break;
23541 case OP_INB:
23542 case OP_INW:
23543 case OP_INL:
23544 switch(ins->op) {
23545 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23546 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23547 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23548 }
23549 if (get_imm8(ins, &RHS(ins, 0))) {
23550 ins->template_id += 1;
23551 }
23552 break;
23553 case OP_OUTB:
23554 case OP_OUTW:
23555 case OP_OUTL:
23556 switch(ins->op) {
23557 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23558 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23559 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23560 }
23561 if (get_imm8(ins, &RHS(ins, 1))) {
23562 ins->template_id += 1;
23563 }
23564 break;
23565 case OP_BSF:
23566 case OP_BSR:
23567 ins->template_id = TEMPLATE_BSF;
23568 break;
23569 case OP_RDMSR:
23570 ins->template_id = TEMPLATE_RDMSR;
23571 next = after_lhs(state, ins);
23572 break;
23573 case OP_WRMSR:
23574 ins->template_id = TEMPLATE_WRMSR;
23575 break;
23576 case OP_HLT:
23577 ins->template_id = TEMPLATE_NOP;
23578 break;
23579 case OP_ASM:
23580 ins->template_id = TEMPLATE_NOP;
23581 next = after_lhs(state, ins);
23582 break;
23583 /* Already transformed instructions */
23584 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023585 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023586 break;
23587 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023588 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023589 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023590 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023591 }
23592 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023593 case OP_JMP:
23594 ins->template_id = TEMPLATE_NOP;
23595 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023596 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23597 case OP_JMP_SLESS: case OP_JMP_ULESS:
23598 case OP_JMP_SMORE: case OP_JMP_UMORE:
23599 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23600 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23601 ins->template_id = TEMPLATE_JMP;
23602 break;
23603 case OP_SET_EQ: case OP_SET_NOTEQ:
23604 case OP_SET_SLESS: case OP_SET_ULESS:
23605 case OP_SET_SMORE: case OP_SET_UMORE:
23606 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23607 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23608 ins->template_id = TEMPLATE_SET;
23609 break;
Eric Biederman90089602004-05-28 14:11:54 +000023610 case OP_DEPOSIT:
23611 next = x86_deposit(state, ins);
23612 break;
23613 case OP_SEXTRACT:
23614 case OP_UEXTRACT:
23615 next = x86_extract(state, ins);
23616 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023617 /* Unhandled instructions */
23618 case OP_PIECE:
23619 default:
Eric Biederman90089602004-05-28 14:11:54 +000023620 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023621 ins->op, tops(ins->op));
23622 break;
23623 }
23624 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023625}
23626
Eric Biederman530b5192003-07-01 10:05:30 +000023627static long next_label(struct compile_state *state)
23628{
Eric Biederman90089602004-05-28 14:11:54 +000023629 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023630 return ++label_counter;
23631}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023632static void generate_local_labels(struct compile_state *state)
23633{
23634 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023635 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023636 label = first;
23637 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023638 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023639 (label->op == OP_SDECL)) {
23640 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023641 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023642 } else {
23643 label->u.cval = 0;
23644 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023645
Eric Biedermanb138ac82003-04-22 18:44:01 +000023646 }
23647 label = label->next;
23648 } while(label != first);
23649}
23650
Stefan Reinauer14e22772010-04-27 06:56:47 +000023651static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023652 struct triple *triple, int classes)
23653{
23654 unsigned mask;
23655 int reg;
23656 reg = ID_REG(triple->id);
23657 if (reg == REG_UNSET) {
23658 internal_error(state, triple, "register not set");
23659 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023660 mask = arch_reg_regcm(state, reg);
23661 if (!(classes & mask)) {
23662 internal_error(state, triple, "reg %d in wrong class",
23663 reg);
23664 }
23665 return reg;
23666}
23667
Eric Biederman90089602004-05-28 14:11:54 +000023668
Eric Biederman530b5192003-07-01 10:05:30 +000023669#if REG_XMM7 != 44
23670#error "Registers have renumberd fix arch_reg_str"
23671#endif
Eric Biederman90089602004-05-28 14:11:54 +000023672static const char *arch_regs[] = {
23673 "%unset",
23674 "%unneeded",
23675 "%eflags",
23676 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23677 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23678 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23679 "%edx:%eax",
23680 "%dx:%ax",
23681 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023682 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023683 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23684};
23685static const char *arch_reg_str(int reg)
23686{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023687 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23688 reg = 0;
23689 }
Eric Biederman90089602004-05-28 14:11:54 +000023690 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023691}
23692
23693static const char *reg(struct compile_state *state, struct triple *triple,
23694 int classes)
23695{
23696 int reg;
23697 reg = check_reg(state, triple, classes);
23698 return arch_reg_str(reg);
23699}
23700
Eric Biederman90089602004-05-28 14:11:54 +000023701static int arch_reg_size(int reg)
23702{
23703 int size;
23704 size = 0;
23705 if (reg == REG_EFLAGS) {
23706 size = 32;
23707 }
23708 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23709 size = 8;
23710 }
23711 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23712 size = 16;
23713 }
23714 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23715 size = 32;
23716 }
23717 else if (reg == REG_EDXEAX) {
23718 size = 64;
23719 }
23720 else if (reg == REG_DXAX) {
23721 size = 32;
23722 }
23723 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23724 size = 64;
23725 }
23726 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23727 size = 128;
23728 }
23729 return size;
23730}
23731
23732static int reg_size(struct compile_state *state, struct triple *ins)
23733{
23734 int reg;
23735 reg = ID_REG(ins->id);
23736 if (reg == REG_UNSET) {
23737 internal_error(state, ins, "register not set");
23738 }
23739 return arch_reg_size(reg);
23740}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023741
Eric Biederman90089602004-05-28 14:11:54 +000023742
23743
Eric Biedermanb138ac82003-04-22 18:44:01 +000023744const char *type_suffix(struct compile_state *state, struct type *type)
23745{
23746 const char *suffix;
23747 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023748 case SIZEOF_I8: suffix = "b"; break;
23749 case SIZEOF_I16: suffix = "w"; break;
23750 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023751 default:
23752 internal_error(state, 0, "unknown suffix");
23753 suffix = 0;
23754 break;
23755 }
23756 return suffix;
23757}
23758
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023759static void print_const_val(
23760 struct compile_state *state, struct triple *ins, FILE *fp)
23761{
23762 switch(ins->op) {
23763 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023764 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023765 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023766 break;
23767 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023768 if ((MISC(ins, 0)->op != OP_SDECL) &&
23769 (MISC(ins, 0)->op != OP_LABEL))
23770 {
Eric Biederman830c9882003-07-04 00:27:33 +000023771 internal_error(state, ins, "bad base for addrconst");
23772 }
23773 if (MISC(ins, 0)->u.cval <= 0) {
23774 internal_error(state, ins, "unlabeled constant");
23775 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023776 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023777 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023778 (unsigned long)(MISC(ins, 0)->u.cval),
23779 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023780 break;
23781 default:
23782 internal_error(state, ins, "unknown constant type");
23783 break;
23784 }
23785}
23786
Eric Biederman530b5192003-07-01 10:05:30 +000023787static void print_const(struct compile_state *state,
23788 struct triple *ins, FILE *fp)
23789{
23790 switch(ins->op) {
23791 case OP_INTCONST:
23792 switch(ins->type->type & TYPE_MASK) {
23793 case TYPE_CHAR:
23794 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023795 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023796 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023797 break;
23798 case TYPE_SHORT:
23799 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023800 fprintf(fp, ".short 0x%04lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023801 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023802 break;
23803 case TYPE_INT:
23804 case TYPE_UINT:
23805 case TYPE_LONG:
23806 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023807 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023808 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023809 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023810 break;
23811 default:
Eric Biederman90089602004-05-28 14:11:54 +000023812 fprintf(state->errout, "type: ");
23813 name_of(state->errout, ins->type);
23814 fprintf(state->errout, "\n");
23815 internal_error(state, ins, "Unknown constant type. Val: %lu",
23816 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023817 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023818
Eric Biederman530b5192003-07-01 10:05:30 +000023819 break;
23820 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023821 if ((MISC(ins, 0)->op != OP_SDECL) &&
23822 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023823 internal_error(state, ins, "bad base for addrconst");
23824 }
23825 if (MISC(ins, 0)->u.cval <= 0) {
23826 internal_error(state, ins, "unlabeled constant");
23827 }
23828 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023829 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023830 (unsigned long)(MISC(ins, 0)->u.cval),
23831 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023832 break;
23833 case OP_BLOBCONST:
23834 {
23835 unsigned char *blob;
23836 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023837 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023838 blob = ins->u.blob;
23839 for(i = 0; i < size; i++) {
23840 fprintf(fp, ".byte 0x%02x\n",
23841 blob[i]);
23842 }
23843 break;
23844 }
23845 default:
23846 internal_error(state, ins, "Unknown constant type");
23847 break;
23848 }
23849}
23850
23851#define TEXT_SECTION ".rom.text"
23852#define DATA_SECTION ".rom.data"
23853
23854static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023855 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023856{
Eric Biederman90089602004-05-28 14:11:54 +000023857 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023858 long ref;
23859 ref = next_label(state);
23860 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023861 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023862 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023863 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023864 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23865 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023866 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023867 }
Eric Biederman530b5192003-07-01 10:05:30 +000023868 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23869 return ref;
23870}
23871
Eric Biederman90089602004-05-28 14:11:54 +000023872static long get_mask_pool_ref(
23873 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23874{
23875 long ref;
23876 if (mask == 0xff) {
23877 ref = 1;
23878 }
23879 else if (mask == 0xffff) {
23880 ref = 2;
23881 }
23882 else {
23883 ref = 0;
23884 internal_error(state, ins, "unhandled mask value");
23885 }
23886 return ref;
23887}
23888
Eric Biedermanb138ac82003-04-22 18:44:01 +000023889static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023890 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023891{
23892 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023893 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023894 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023895 internal_error(state, ins, "invalid register assignment");
23896 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023897 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023898 fprintf(fp, "\t%s ", op);
23899 print_const_val(state, RHS(ins, 1), fp);
23900 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023901 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023902 }
23903 else {
23904 unsigned lmask, rmask;
23905 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023906 lreg = check_reg(state, RHS(ins, 0), mask);
23907 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023908 lmask = arch_reg_regcm(state, lreg);
23909 rmask = arch_reg_regcm(state, rreg);
23910 mask = lmask & rmask;
23911 fprintf(fp, "\t%s %s, %s\n",
23912 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023913 reg(state, RHS(ins, 1), mask),
23914 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023915 }
23916}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023917static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023918 const char *op, struct triple *ins, FILE *fp)
23919{
23920 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023921 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023922 fprintf(fp, "\t%s %s\n",
23923 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023924 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023925}
23926
23927static void print_op_shift(struct compile_state *state,
23928 const char *op, struct triple *ins, FILE *fp)
23929{
23930 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023931 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023932 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023933 internal_error(state, ins, "invalid register assignment");
23934 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023935 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023936 fprintf(fp, "\t%s ", op);
23937 print_const_val(state, RHS(ins, 1), fp);
23938 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023939 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023940 }
23941 else {
23942 fprintf(fp, "\t%s %s, %s\n",
23943 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023944 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023945 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023946 }
23947}
23948
23949static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23950{
23951 const char *op;
23952 int mask;
23953 int dreg;
23954 mask = 0;
23955 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023956 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023957 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23958 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23959 default:
23960 internal_error(state, ins, "not an in operation");
23961 op = 0;
23962 break;
23963 }
23964 dreg = check_reg(state, ins, mask);
23965 if (!reg_is_reg(state, dreg, REG_EAX)) {
23966 internal_error(state, ins, "dst != %%eax");
23967 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023968 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023969 fprintf(fp, "\t%s ", op);
23970 print_const_val(state, RHS(ins, 0), fp);
23971 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023972 reg(state, ins, mask));
23973 }
23974 else {
23975 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023976 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023977 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23978 internal_error(state, ins, "src != %%dx");
23979 }
23980 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023981 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023982 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023983 reg(state, ins, mask));
23984 }
23985}
23986
23987static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23988{
23989 const char *op;
23990 int mask;
23991 int lreg;
23992 mask = 0;
23993 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023994 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023995 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23996 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23997 default:
23998 internal_error(state, ins, "not an out operation");
23999 op = 0;
24000 break;
24001 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024002 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024003 if (!reg_is_reg(state, lreg, REG_EAX)) {
24004 internal_error(state, ins, "src != %%eax");
24005 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024006 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024007 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024008 op, reg(state, RHS(ins, 0), mask));
24009 print_const_val(state, RHS(ins, 1), fp);
24010 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024011 }
24012 else {
24013 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024014 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024015 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24016 internal_error(state, ins, "dst != %%dx");
24017 }
24018 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024019 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024020 reg(state, RHS(ins, 0), mask),
24021 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024022 }
24023}
24024
24025static void print_op_move(struct compile_state *state,
24026 struct triple *ins, FILE *fp)
24027{
24028 /* op_move is complex because there are many types
24029 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024030 * Because OP_COPY will be introduced in arbitrary locations
24031 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024032 * OP_CONVERT can change the flags and it is the only operation
24033 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024034 */
24035 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24036 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024037 if (state->arch->features & X86_NOOP_COPY) {
24038 omit_copy = 0;
24039 }
24040 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024041 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024042 dst = ins;
24043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024044 else {
24045 internal_error(state, ins, "unknown move operation");
24046 src = dst = 0;
24047 }
Eric Biederman90089602004-05-28 14:11:54 +000024048 if (reg_size(state, dst) < size_of(state, dst->type)) {
24049 internal_error(state, ins, "Invalid destination register");
24050 }
24051 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24052 fprintf(state->errout, "src type: ");
24053 name_of(state->errout, src->type);
24054 fprintf(state->errout, "\n");
24055 fprintf(state->errout, "dst type: ");
24056 name_of(state->errout, dst->type);
24057 fprintf(state->errout, "\n");
24058 internal_error(state, ins, "Type mismatch for OP_COPY");
24059 }
24060
Eric Biederman0babc1c2003-05-09 02:39:00 +000024061 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024062 int src_reg, dst_reg;
24063 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024064 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024065 dst_reg = ID_REG(dst->id);
24066 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024067 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024068 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024069 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024070 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024071 if ((src_reg != dst_reg) || !omit_copy) {
24072 fprintf(fp, "\tmov %s, %s\n",
24073 reg(state, src, src_regcm),
24074 reg(state, dst, dst_regcm));
24075 }
24076 }
24077 /* Move 32bit to 16bit */
24078 else if ((src_regcm & REGCM_GPR32) &&
24079 (dst_regcm & REGCM_GPR16)) {
24080 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24081 if ((src_reg != dst_reg) || !omit_copy) {
24082 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024083 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024084 arch_reg_str(dst_reg));
24085 }
24086 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024087 /* Move from 32bit gprs to 16bit gprs */
24088 else if ((src_regcm & REGCM_GPR32) &&
24089 (dst_regcm & REGCM_GPR16)) {
24090 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24091 if ((src_reg != dst_reg) || !omit_copy) {
24092 fprintf(fp, "\tmov %s, %s\n",
24093 arch_reg_str(src_reg),
24094 arch_reg_str(dst_reg));
24095 }
24096 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024097 /* Move 32bit to 8bit */
24098 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024099 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024100 {
24101 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24102 if ((src_reg != dst_reg) || !omit_copy) {
24103 fprintf(fp, "\tmovb %s, %s\n",
24104 arch_reg_str(src_reg),
24105 arch_reg_str(dst_reg));
24106 }
24107 }
24108 /* Move 16bit to 8bit */
24109 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024110 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024111 {
24112 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24113 if ((src_reg != dst_reg) || !omit_copy) {
24114 fprintf(fp, "\tmovb %s, %s\n",
24115 arch_reg_str(src_reg),
24116 arch_reg_str(dst_reg));
24117 }
24118 }
24119 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024120 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024121 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024122 const char *op;
24123 op = is_signed(src->type)? "movsx": "movzx";
24124 fprintf(fp, "\t%s %s, %s\n",
24125 op,
24126 reg(state, src, src_regcm),
24127 reg(state, dst, dst_regcm));
24128 }
24129 /* Move between sse registers */
24130 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24131 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024132 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024133 reg(state, src, src_regcm),
24134 reg(state, dst, dst_regcm));
24135 }
24136 }
Eric Biederman530b5192003-07-01 10:05:30 +000024137 /* Move between mmx registers */
24138 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024139 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024140 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024141 reg(state, src, src_regcm),
24142 reg(state, dst, dst_regcm));
24143 }
24144 }
Eric Biederman530b5192003-07-01 10:05:30 +000024145 /* Move from sse to mmx registers */
24146 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24147 fprintf(fp, "\tmovdq2q %s, %s\n",
24148 reg(state, src, src_regcm),
24149 reg(state, dst, dst_regcm));
24150 }
24151 /* Move from mmx to sse registers */
24152 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24153 fprintf(fp, "\tmovq2dq %s, %s\n",
24154 reg(state, src, src_regcm),
24155 reg(state, dst, dst_regcm));
24156 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024157 /* Move between 32bit gprs & mmx/sse registers */
24158 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24159 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24160 fprintf(fp, "\tmovd %s, %s\n",
24161 reg(state, src, src_regcm),
24162 reg(state, dst, dst_regcm));
24163 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024164 /* Move from 16bit gprs & mmx/sse registers */
24165 else if ((src_regcm & REGCM_GPR16) &&
24166 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24167 const char *op;
24168 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024169 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024170 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24171 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24172 op,
24173 arch_reg_str(src_reg),
24174 arch_reg_str(mid_reg),
24175 arch_reg_str(mid_reg),
24176 arch_reg_str(dst_reg));
24177 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024178 /* Move from mmx/sse registers to 16bit gprs */
24179 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24180 (dst_regcm & REGCM_GPR16)) {
24181 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24182 fprintf(fp, "\tmovd %s, %s\n",
24183 arch_reg_str(src_reg),
24184 arch_reg_str(dst_reg));
24185 }
Eric Biederman530b5192003-07-01 10:05:30 +000024186 /* Move from gpr to 64bit dividend */
24187 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24188 (dst_regcm & REGCM_DIVIDEND64)) {
24189 const char *extend;
24190 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24191 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024192 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024193 extend);
24194 }
24195 /* Move from 64bit gpr to gpr */
24196 else if ((src_regcm & REGCM_DIVIDEND64) &&
24197 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24198 if (dst_regcm & REGCM_GPR32) {
24199 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024200 }
Eric Biederman530b5192003-07-01 10:05:30 +000024201 else if (dst_regcm & REGCM_GPR16) {
24202 src_reg = REG_AX;
24203 }
24204 else if (dst_regcm & REGCM_GPR8_LO) {
24205 src_reg = REG_AL;
24206 }
24207 fprintf(fp, "\tmov %s, %s\n",
24208 arch_reg_str(src_reg),
24209 arch_reg_str(dst_reg));
24210 }
24211 /* Move from mmx/sse registers to 64bit gpr */
24212 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24213 (dst_regcm & REGCM_DIVIDEND64)) {
24214 const char *extend;
24215 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24216 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24217 arch_reg_str(src_reg),
24218 extend);
24219 }
24220 /* Move from 64bit gpr to mmx/sse register */
24221 else if ((src_regcm & REGCM_DIVIDEND64) &&
24222 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24223 fprintf(fp, "\tmovd %%eax, %s\n",
24224 arch_reg_str(dst_reg));
24225 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024226#if X86_4_8BIT_GPRS
24227 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024228 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024229 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24230 const char *op;
24231 int mid_reg;
24232 op = is_signed(src->type)? "movsx":"movzx";
24233 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24234 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24235 op,
24236 reg(state, src, src_regcm),
24237 arch_reg_str(mid_reg),
24238 arch_reg_str(mid_reg),
24239 reg(state, dst, dst_regcm));
24240 }
24241 /* Move from mmx/sse registers and 8bit gprs */
24242 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024243 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024244 int mid_reg;
24245 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24246 fprintf(fp, "\tmovd %s, %s\n",
24247 reg(state, src, src_regcm),
24248 arch_reg_str(mid_reg));
24249 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024250 /* Move from 32bit gprs to 8bit gprs */
24251 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024252 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024253 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24254 if ((src_reg != dst_reg) || !omit_copy) {
24255 fprintf(fp, "\tmov %s, %s\n",
24256 arch_reg_str(src_reg),
24257 arch_reg_str(dst_reg));
24258 }
24259 }
24260 /* Move from 16bit gprs to 8bit gprs */
24261 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024262 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024263 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24264 if ((src_reg != dst_reg) || !omit_copy) {
24265 fprintf(fp, "\tmov %s, %s\n",
24266 arch_reg_str(src_reg),
24267 arch_reg_str(dst_reg));
24268 }
24269 }
24270#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024271 /* Move from %eax:%edx to %eax:%edx */
24272 else if ((src_regcm & REGCM_DIVIDEND64) &&
24273 (dst_regcm & REGCM_DIVIDEND64) &&
24274 (src_reg == dst_reg)) {
24275 if (!omit_copy) {
24276 fprintf(fp, "\t/*mov %s, %s*/\n",
24277 arch_reg_str(src_reg),
24278 arch_reg_str(dst_reg));
24279 }
24280 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024281 else {
Eric Biederman90089602004-05-28 14:11:54 +000024282 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24283 internal_error(state, ins, "attempt to copy from %%eflags!");
24284 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024285 internal_error(state, ins, "unknown copy type");
24286 }
24287 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024288 else {
Eric Biederman90089602004-05-28 14:11:54 +000024289 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024290 int dst_reg;
24291 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024292 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024293 dst_reg = ID_REG(dst->id);
24294 dst_regcm = arch_reg_regcm(state, dst_reg);
24295 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24296 fprintf(fp, "\tmov ");
24297 print_const_val(state, src, fp);
24298 fprintf(fp, ", %s\n",
24299 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24300 }
24301 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024302 if (dst_size > SIZEOF_I32) {
24303 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024304 }
24305 fprintf(fp, "\tmov $0, %%edx\n");
24306 fprintf(fp, "\tmov ");
24307 print_const_val(state, src, fp);
24308 fprintf(fp, ", %%eax\n");
24309 }
24310 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024311 if (dst_size > SIZEOF_I16) {
24312 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024313 }
24314 fprintf(fp, "\tmov $0, %%dx\n");
24315 fprintf(fp, "\tmov ");
24316 print_const_val(state, src, fp);
24317 fprintf(fp, ", %%ax");
24318 }
24319 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24320 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024321 if (dst_size > SIZEOF_I32) {
24322 internal_error(state, ins, "%d bit constant...", dst_size);
24323 }
24324 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024325 fprintf(fp, "\tmovd L%s%lu, %s\n",
24326 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024327 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24328 }
24329 else {
24330 internal_error(state, ins, "unknown copy immediate type");
24331 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024332 }
Eric Biederman90089602004-05-28 14:11:54 +000024333 /* Leave now if this is not a type conversion */
24334 if (ins->op != OP_CONVERT) {
24335 return;
24336 }
24337 /* Now make certain I have not logically overflowed the destination */
24338 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24339 (size_of(state, dst->type) < reg_size(state, dst)))
24340 {
24341 unsigned long mask;
24342 int dst_reg;
24343 int dst_regcm;
24344 if (size_of(state, dst->type) >= 32) {
24345 fprintf(state->errout, "dst type: ");
24346 name_of(state->errout, dst->type);
24347 fprintf(state->errout, "\n");
24348 internal_error(state, dst, "unhandled dst type size");
24349 }
24350 mask = 1;
24351 mask <<= size_of(state, dst->type);
24352 mask -= 1;
24353
24354 dst_reg = ID_REG(dst->id);
24355 dst_regcm = arch_reg_regcm(state, dst_reg);
24356
24357 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24358 fprintf(fp, "\tand $0x%lx, %s\n",
24359 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24360 }
24361 else if (dst_regcm & REGCM_MMX) {
24362 long ref;
24363 ref = get_mask_pool_ref(state, dst, mask, fp);
24364 fprintf(fp, "\tpand L%s%lu, %s\n",
24365 state->compiler->label_prefix, ref,
24366 reg(state, dst, REGCM_MMX));
24367 }
24368 else if (dst_regcm & REGCM_XMM) {
24369 long ref;
24370 ref = get_mask_pool_ref(state, dst, mask, fp);
24371 fprintf(fp, "\tpand L%s%lu, %s\n",
24372 state->compiler->label_prefix, ref,
24373 reg(state, dst, REGCM_XMM));
24374 }
24375 else {
24376 fprintf(state->errout, "dst type: ");
24377 name_of(state->errout, dst->type);
24378 fprintf(state->errout, "\n");
24379 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24380 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24381 }
24382 }
24383 /* Make certain I am properly sign extended */
24384 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24385 (is_signed(src->type)))
24386 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024387 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024388 int dst_reg;
24389 int dst_regcm;
24390
Eric Biederman90089602004-05-28 14:11:54 +000024391 reg_bits = reg_size(state, dst);
24392 if (reg_bits > 32) {
24393 reg_bits = 32;
24394 }
24395 shift_bits = reg_bits - size_of(state, src->type);
24396 dst_reg = ID_REG(dst->id);
24397 dst_regcm = arch_reg_regcm(state, dst_reg);
24398
24399 if (shift_bits < 0) {
24400 internal_error(state, dst, "negative shift?");
24401 }
24402
24403 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024404 fprintf(fp, "\tshl $%d, %s\n",
24405 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024406 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024407 fprintf(fp, "\tsar $%d, %s\n",
24408 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024409 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24410 }
24411 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24412 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024413 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024414 reg(state, dst, REGCM_MMX | REGCM_XMM));
24415 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024416 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024417 reg(state, dst, REGCM_MMX | REGCM_XMM));
24418 }
24419 else {
24420 fprintf(state->errout, "dst type: ");
24421 name_of(state->errout, dst->type);
24422 fprintf(state->errout, "\n");
24423 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24424 internal_error(state, dst, "failed to signed extend value");
24425 }
24426 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024427}
24428
24429static void print_op_load(struct compile_state *state,
24430 struct triple *ins, FILE *fp)
24431{
24432 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024433 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024434 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024435 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024436 if (is_const(src) || is_const(dst)) {
24437 internal_error(state, ins, "unknown load operation");
24438 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024439 switch(ins->type->type & TYPE_MASK) {
24440 case TYPE_CHAR: op = "movsbl"; break;
24441 case TYPE_UCHAR: op = "movzbl"; break;
24442 case TYPE_SHORT: op = "movswl"; break;
24443 case TYPE_USHORT: op = "movzwl"; break;
24444 case TYPE_INT: case TYPE_UINT:
24445 case TYPE_LONG: case TYPE_ULONG:
24446 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024447 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024448 break;
24449 default:
24450 internal_error(state, ins, "unknown type in load");
24451 op = "<invalid opcode>";
24452 break;
24453 }
24454 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024455 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024456 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024457 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024458}
24459
24460
24461static void print_op_store(struct compile_state *state,
24462 struct triple *ins, FILE *fp)
24463{
24464 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024465 dst = RHS(ins, 0);
24466 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024467 if (is_const(src) && (src->op == OP_INTCONST)) {
24468 long_t value;
24469 value = (long_t)(src->u.cval);
24470 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24471 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024472 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024473 reg(state, dst, REGCM_GPR32));
24474 }
24475 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24476 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24477 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024478 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024479 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024480 }
24481 else {
24482 if (is_const(src) || is_const(dst)) {
24483 internal_error(state, ins, "unknown store operation");
24484 }
24485 fprintf(fp, "\tmov%s %s, (%s)\n",
24486 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024487 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024488 reg(state, dst, REGCM_GPR32));
24489 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024490
24491
Eric Biedermanb138ac82003-04-22 18:44:01 +000024492}
24493
24494static void print_op_smul(struct compile_state *state,
24495 struct triple *ins, FILE *fp)
24496{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024497 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024498 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024499 reg(state, RHS(ins, 1), REGCM_GPR32),
24500 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024501 }
24502 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024503 fprintf(fp, "\timul ");
24504 print_const_val(state, RHS(ins, 1), fp);
24505 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024506 }
24507}
24508
24509static void print_op_cmp(struct compile_state *state,
24510 struct triple *ins, FILE *fp)
24511{
24512 unsigned mask;
24513 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024514 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024515 dreg = check_reg(state, ins, REGCM_FLAGS);
24516 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24517 internal_error(state, ins, "bad dest register for cmp");
24518 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024519 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024520 fprintf(fp, "\tcmp ");
24521 print_const_val(state, RHS(ins, 1), fp);
24522 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024523 }
24524 else {
24525 unsigned lmask, rmask;
24526 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024527 lreg = check_reg(state, RHS(ins, 0), mask);
24528 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024529 lmask = arch_reg_regcm(state, lreg);
24530 rmask = arch_reg_regcm(state, rreg);
24531 mask = lmask & rmask;
24532 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024533 reg(state, RHS(ins, 1), mask),
24534 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024535 }
24536}
24537
24538static void print_op_test(struct compile_state *state,
24539 struct triple *ins, FILE *fp)
24540{
24541 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024542 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024543 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024544 reg(state, RHS(ins, 0), mask),
24545 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024546}
24547
24548static void print_op_branch(struct compile_state *state,
24549 struct triple *branch, FILE *fp)
24550{
24551 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024552 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024553 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024554 internal_error(state, branch, "jmp with condition?");
24555 }
24556 bop = "jmp";
24557 }
24558 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024559 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024560 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024561 internal_error(state, branch, "jmpcc without condition?");
24562 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024563 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24564 if ((RHS(branch, 0)->op != OP_CMP) &&
24565 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024566 internal_error(state, branch, "bad branch test");
24567 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024568#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024569#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024570#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024571 ptr = RHS(branch, 0);
24572 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24573 if (ptr->op != OP_COPY) {
24574 internal_error(state, branch, "branch does not follow test");
24575 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024576 }
24577 switch(branch->op) {
24578 case OP_JMP_EQ: bop = "jz"; break;
24579 case OP_JMP_NOTEQ: bop = "jnz"; break;
24580 case OP_JMP_SLESS: bop = "jl"; break;
24581 case OP_JMP_ULESS: bop = "jb"; break;
24582 case OP_JMP_SMORE: bop = "jg"; break;
24583 case OP_JMP_UMORE: bop = "ja"; break;
24584 case OP_JMP_SLESSEQ: bop = "jle"; break;
24585 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24586 case OP_JMP_SMOREEQ: bop = "jge"; break;
24587 case OP_JMP_UMOREEQ: bop = "jae"; break;
24588 default:
24589 internal_error(state, branch, "Invalid branch op");
24590 break;
24591 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024592
Eric Biedermanb138ac82003-04-22 18:44:01 +000024593 }
Eric Biederman90089602004-05-28 14:11:54 +000024594#if 1
24595 if (branch->op == OP_CALL) {
24596 fprintf(fp, "\t/* call */\n");
24597 }
24598#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024599 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024600 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024601 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024602 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024603}
24604
Eric Biederman5ade04a2003-10-22 04:03:46 +000024605static void print_op_ret(struct compile_state *state,
24606 struct triple *branch, FILE *fp)
24607{
24608 fprintf(fp, "\tjmp *%s\n",
24609 reg(state, RHS(branch, 0), REGCM_GPR32));
24610}
24611
Eric Biedermanb138ac82003-04-22 18:44:01 +000024612static void print_op_set(struct compile_state *state,
24613 struct triple *set, FILE *fp)
24614{
24615 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024616 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024617 internal_error(state, set, "setcc without condition?");
24618 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024619 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24620 if ((RHS(set, 0)->op != OP_CMP) &&
24621 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024622 internal_error(state, set, "bad set test");
24623 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024624 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024625 internal_error(state, set, "set does not follow test");
24626 }
24627 switch(set->op) {
24628 case OP_SET_EQ: sop = "setz"; break;
24629 case OP_SET_NOTEQ: sop = "setnz"; break;
24630 case OP_SET_SLESS: sop = "setl"; break;
24631 case OP_SET_ULESS: sop = "setb"; break;
24632 case OP_SET_SMORE: sop = "setg"; break;
24633 case OP_SET_UMORE: sop = "seta"; break;
24634 case OP_SET_SLESSEQ: sop = "setle"; break;
24635 case OP_SET_ULESSEQ: sop = "setbe"; break;
24636 case OP_SET_SMOREEQ: sop = "setge"; break;
24637 case OP_SET_UMOREEQ: sop = "setae"; break;
24638 default:
24639 internal_error(state, set, "Invalid set op");
24640 break;
24641 }
24642 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024643 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024644}
24645
Stefan Reinauer14e22772010-04-27 06:56:47 +000024646static void print_op_bit_scan(struct compile_state *state,
24647 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024648{
24649 const char *op;
24650 switch(ins->op) {
24651 case OP_BSF: op = "bsf"; break;
24652 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024653 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024654 internal_error(state, ins, "unknown bit scan");
24655 op = 0;
24656 break;
24657 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024658 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024659 "\t%s %s, %s\n"
24660 "\tjnz 1f\n"
24661 "\tmovl $-1, %s\n"
24662 "1:\n",
24663 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024664 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024665 reg(state, ins, REGCM_GPR32),
24666 reg(state, ins, REGCM_GPR32));
24667}
24668
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024669
Eric Biedermanb138ac82003-04-22 18:44:01 +000024670static void print_sdecl(struct compile_state *state,
24671 struct triple *ins, FILE *fp)
24672{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024673 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024674 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024675 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024676 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024677 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024678 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024679
Eric Biedermanb138ac82003-04-22 18:44:01 +000024680}
24681
24682static void print_instruction(struct compile_state *state,
24683 struct triple *ins, FILE *fp)
24684{
24685 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024686 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024687 */
24688 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024689 case OP_ASM:
24690 print_op_asm(state, ins, fp);
24691 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024692 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24693 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24694 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24695 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24696 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24697 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24698 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24699 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24700 case OP_POS: break;
24701 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24702 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024703 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024704 case OP_INTCONST:
24705 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024706 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024707 /* Don't generate anything here for constants */
24708 case OP_PHI:
24709 /* Don't generate anything for variable declarations. */
24710 break;
Eric Biederman90089602004-05-28 14:11:54 +000024711 case OP_UNKNOWNVAL:
24712 fprintf(fp, " /* unknown %s */\n",
24713 reg(state, ins, REGCM_ALL));
24714 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024715 case OP_SDECL:
24716 print_sdecl(state, ins, fp);
24717 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024718 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024719 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024720 print_op_move(state, ins, fp);
24721 break;
24722 case OP_LOAD:
24723 print_op_load(state, ins, fp);
24724 break;
24725 case OP_STORE:
24726 print_op_store(state, ins, fp);
24727 break;
24728 case OP_SMUL:
24729 print_op_smul(state, ins, fp);
24730 break;
24731 case OP_CMP: print_op_cmp(state, ins, fp); break;
24732 case OP_TEST: print_op_test(state, ins, fp); break;
24733 case OP_JMP:
24734 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24735 case OP_JMP_SLESS: case OP_JMP_ULESS:
24736 case OP_JMP_SMORE: case OP_JMP_UMORE:
24737 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24738 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024739 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024740 print_op_branch(state, ins, fp);
24741 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024742 case OP_RET:
24743 print_op_ret(state, ins, fp);
24744 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024745 case OP_SET_EQ: case OP_SET_NOTEQ:
24746 case OP_SET_SLESS: case OP_SET_ULESS:
24747 case OP_SET_SMORE: case OP_SET_UMORE:
24748 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24749 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24750 print_op_set(state, ins, fp);
24751 break;
24752 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024753 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024754 break;
24755 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024756 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024757 break;
24758 case OP_BSF:
24759 case OP_BSR:
24760 print_op_bit_scan(state, ins, fp);
24761 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024762 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024763 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024764 fprintf(fp, "\trdmsr\n");
24765 break;
24766 case OP_WRMSR:
24767 fprintf(fp, "\twrmsr\n");
24768 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024769 case OP_HLT:
24770 fprintf(fp, "\thlt\n");
24771 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024772 case OP_SDIVT:
24773 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24774 break;
24775 case OP_UDIVT:
24776 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24777 break;
24778 case OP_UMUL:
24779 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24780 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024781 case OP_LABEL:
24782 if (!ins->use) {
24783 return;
24784 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024785 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024786 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024787 break;
Eric Biederman90089602004-05-28 14:11:54 +000024788 case OP_ADECL:
24789 /* Ignore adecls with no registers error otherwise */
24790 if (!noop_adecl(ins)) {
24791 internal_error(state, ins, "adecl remains?");
24792 }
24793 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024794 /* Ignore OP_PIECE */
24795 case OP_PIECE:
24796 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024797 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024798 case OP_SDIV: case OP_UDIV:
24799 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024800 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24801 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24802 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24803 default:
24804 internal_error(state, ins, "unknown op: %d %s",
24805 ins->op, tops(ins->op));
24806 break;
24807 }
24808}
24809
24810static void print_instructions(struct compile_state *state)
24811{
24812 struct triple *first, *ins;
24813 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024814 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024815 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024816 int max_inline_depth;
24817 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024818 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024819 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024820 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024821 /* Masks for common sizes */
24822 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24823 fprintf(fp, ".balign 16\n");
24824 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24825 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24826 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24827 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024828 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024829 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024830 ins = first;
24831 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024832 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024833 last_occurance != ins->occurance) {
24834 if (!ins->occurance->parent) {
24835 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024836 ins->occurance->function?ins->occurance->function:"(null)",
24837 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024838 ins->occurance->line,
24839 ins->occurance->col);
24840 }
24841 else {
24842 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024843 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024844 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024845 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024846 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024847 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024848 fprintf(fp, "\t * %s,%s:%d.%d\n",
24849 ptr->function,
24850 ptr->filename,
24851 ptr->line,
24852 ptr->col);
24853 }
24854 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024855 if (inline_depth > max_inline_depth) {
24856 max_inline_depth = inline_depth;
24857 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024858 }
24859 if (last_occurance) {
24860 put_occurance(last_occurance);
24861 }
24862 get_occurance(ins->occurance);
24863 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024864 }
24865
24866 print_instruction(state, ins, fp);
24867 ins = ins->next;
24868 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024869 if (print_location) {
24870 fprintf(fp, "/* max inline depth %d */\n",
24871 max_inline_depth);
24872 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024873}
Eric Biederman530b5192003-07-01 10:05:30 +000024874
Eric Biedermanb138ac82003-04-22 18:44:01 +000024875static void generate_code(struct compile_state *state)
24876{
24877 generate_local_labels(state);
24878 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024879
Eric Biedermanb138ac82003-04-22 18:44:01 +000024880}
24881
Eric Biederman90089602004-05-28 14:11:54 +000024882static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024883{
Eric Biederman90089602004-05-28 14:11:54 +000024884 int tok;
24885 FILE *fp;
24886 int line;
24887 const char *filename;
24888 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024889 filename = 0;
24890 line = 0;
24891 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024892 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024893 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024894 const char *token_str;
24895 tok = peek(state);
24896 if (tok == TOK_EOF) {
24897 break;
24898 }
Eric Biederman41203d92004-11-08 09:31:09 +000024899 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024900 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024901 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024902 tk->str_len ? tk->val.str :
24903 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024904
24905 file = state->file;
24906 while(file->macro && file->prev) {
24907 file = file->prev;
24908 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024909 if (!file->macro &&
24910 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024911 {
Eric Biederman90089602004-05-28 14:11:54 +000024912 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024913 if ((file->basename == filename) &&
24914 (line < file->line)) {
24915 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024916 fprintf(fp, "\n");
24917 line++;
24918 }
24919 }
24920 else {
24921 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024922 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024923 }
Eric Biederman132368b2004-11-09 08:59:23 +000024924 line = file->line;
24925 filename = file->basename;
24926 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024927 for(i = 0; i < col; i++) {
24928 fprintf(fp, " ");
24929 }
24930 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024931
Eric Biederman90089602004-05-28 14:11:54 +000024932 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024933
Eric Biederman90089602004-05-28 14:11:54 +000024934 if (state->compiler->debug & DEBUG_TOKENS) {
24935 loc(state->dbgout, state, 0);
24936 fprintf(state->dbgout, "%s <- `%s'\n",
24937 tokens[tok], token_str);
24938 }
24939 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024940}
24941
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024942static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024943 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024944{
24945 int i;
24946 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024947 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024948 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024949 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024950 state.compiler = compiler;
24951 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024952 state.file = 0;
24953 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24954 memset(&state.token[i], 0, sizeof(state.token[i]));
24955 state.token[i].tok = -1;
24956 }
Eric Biederman90089602004-05-28 14:11:54 +000024957 /* Remember the output descriptors */
24958 state.errout = stderr;
24959 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024960 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024961 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24962 state.output = stdout;
24963 } else {
24964 state.output = fopen(state.compiler->ofilename, "w");
24965 if (!state.output) {
24966 error(&state, 0, "Cannot open output file %s\n",
24967 state.compiler->ofilename);
24968 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024969 }
Eric Biederman90089602004-05-28 14:11:54 +000024970 /* Make certain a good cleanup happens */
24971 exit_state = &state;
24972 atexit(exit_cleanup);
24973
Eric Biedermanb138ac82003-04-22 18:44:01 +000024974 /* Prep the preprocessor */
24975 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024976 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024977 /* register the C keywords */
24978 register_keywords(&state);
24979 /* register the keywords the macro preprocessor knows */
24980 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024981 /* generate some builtin macros */
24982 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024983 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024984 state.i_switch = lookup(&state, "switch", 6);
24985 state.i_case = lookup(&state, "case", 4);
24986 state.i_continue = lookup(&state, "continue", 8);
24987 state.i_break = lookup(&state, "break", 5);
24988 state.i_default = lookup(&state, "default", 7);
24989 state.i_return = lookup(&state, "return", 6);
24990 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024991 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24992 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24993 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24994 /* Memorize where predefined identifiers are. */
24995 state.i___func__ = lookup(&state, "__func__", 8);
24996 /* Memorize where some attribute keywords are. */
24997 state.i_noinline = lookup(&state, "noinline", 8);
24998 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024999 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020025000 state.i_unused = lookup(&state, "unused", 6);
Stefan Reinauerec664bc2013-05-09 14:06:04 -070025001 state.i_packed = lookup(&state, "packed", 6);
Eric Biederman90089602004-05-28 14:11:54 +000025002
25003 /* Process the command line macros */
25004 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000025005
25006 /* Allocate beginning bounding labels for the function list */
25007 state.first = label(&state);
25008 state.first->id |= TRIPLE_FLAG_VOLATILE;
25009 use_triple(state.first, state.first);
25010 ptr = label(&state);
25011 ptr->id |= TRIPLE_FLAG_VOLATILE;
25012 use_triple(ptr, ptr);
25013 flatten(&state, state.first, ptr);
25014
Eric Biederman5ade04a2003-10-22 04:03:46 +000025015 /* Allocate a label for the pool of global variables */
25016 state.global_pool = label(&state);
25017 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25018 flatten(&state, state.first, state.global_pool);
25019
Eric Biedermanb138ac82003-04-22 18:44:01 +000025020 /* Enter the globl definition scope */
25021 start_scope(&state);
25022 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025023
Eric Biedermanb138ac82003-04-22 18:44:01 +000025024 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025025
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025026 while (includes) {
25027 compile_file(&state, includes->filename, 1);
25028 includes=includes->next;
25029 }
Eric Biederman90089602004-05-28 14:11:54 +000025030
25031 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025032 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025033 print_preprocessed_tokens(&state);
25034 return;
25035 }
25036
Eric Biedermanb138ac82003-04-22 18:44:01 +000025037 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025038
Eric Biedermanb138ac82003-04-22 18:44:01 +000025039 /* Exit the global definition scope */
25040 end_scope(&state);
25041
Stefan Reinauer14e22772010-04-27 06:56:47 +000025042 /* Now that basic compilation has happened
25043 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025044 */
25045 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025046
Eric Biedermanb138ac82003-04-22 18:44:01 +000025047 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025048 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025049 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025050 }
Eric Biederman90089602004-05-28 14:11:54 +000025051 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025052}
25053
Eric Biederman90089602004-05-28 14:11:54 +000025054static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025055{
Eric Biederman90089602004-05-28 14:11:54 +000025056 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025057}
25058
25059static void usage(void)
25060{
Eric Biederman90089602004-05-28 14:11:54 +000025061 FILE *fp = stdout;
25062 version(fp);
25063 fprintf(fp,
25064 "\nUsage: romcc [options] <source>.c\n"
25065 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25066 "Options: \n"
25067 "-o <output file name>\n"
25068 "-f<option> Specify a generic compiler option\n"
25069 "-m<option> Specify a arch dependent option\n"
25070 "-- Specify this is the last option\n"
25071 "\nGeneric compiler options:\n"
25072 );
25073 compiler_usage(fp);
25074 fprintf(fp,
25075 "\nArchitecture compiler options:\n"
25076 );
25077 arch_usage(fp);
25078 fprintf(fp,
25079 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025080 );
25081}
25082
25083static void arg_error(char *fmt, ...)
25084{
25085 va_list args;
25086 va_start(args, fmt);
25087 vfprintf(stderr, fmt, args);
25088 va_end(args);
25089 usage();
25090 exit(1);
25091}
25092
25093int main(int argc, char **argv)
25094{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025095 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025096 struct compiler_state compiler;
25097 struct arch_state arch;
25098 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025099
25100
Eric Biederman90089602004-05-28 14:11:54 +000025101 /* I don't want any surprises */
25102 setlocale(LC_ALL, "C");
25103
Eric Biederman5ade04a2003-10-22 04:03:46 +000025104 init_compiler_state(&compiler);
25105 init_arch_state(&arch);
25106 filename = 0;
25107 all_opts = 0;
25108 while(argc > 1) {
25109 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25110 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025111 argv += 2;
25112 argc -= 2;
25113 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025114 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025115 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025116 result = -1;
25117 if (strcmp(argv[1], "--") == 0) {
25118 result = 0;
25119 all_opts = 1;
25120 }
Eric Biederman90089602004-05-28 14:11:54 +000025121 else if (strncmp(argv[1], "-E", 2) == 0) {
25122 result = compiler_encode_flag(&compiler, argv[1]);
25123 }
25124 else if (strncmp(argv[1], "-O", 2) == 0) {
25125 result = compiler_encode_flag(&compiler, argv[1]);
25126 }
25127 else if (strncmp(argv[1], "-I", 2) == 0) {
25128 result = compiler_encode_flag(&compiler, argv[1]);
25129 }
25130 else if (strncmp(argv[1], "-D", 2) == 0) {
25131 result = compiler_encode_flag(&compiler, argv[1]);
25132 }
25133 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025134 result = compiler_encode_flag(&compiler, argv[1]);
25135 }
25136 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25137 result = compiler_encode_flag(&compiler, argv[1]+2);
25138 }
25139 else if (strncmp(argv[1], "-f", 2) == 0) {
25140 result = compiler_encode_flag(&compiler, argv[1]+2);
25141 }
25142 else if (strncmp(argv[1], "-m", 2) == 0) {
25143 result = arch_encode_flag(&arch, argv[1]+2);
25144 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025145 else if (strncmp(argv[1], "-c", 2) == 0) {
25146 result = 0;
25147 }
25148 else if (strncmp(argv[1], "-S", 2) == 0) {
25149 result = 0;
25150 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025151 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025152 struct filelist *old_head = include_filelist;
25153 include_filelist = malloc(sizeof(struct filelist));
25154 if (!include_filelist) {
25155 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025156 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025157 argv++;
25158 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025159 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025160 include_filelist->next = old_head;
25161 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025162 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025163 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025164 arg_error("Invalid option specified: %s\n",
25165 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025166 }
25167 argv++;
25168 argc--;
25169 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025170 else {
25171 if (filename) {
25172 arg_error("Only one filename may be specified\n");
25173 }
25174 filename = argv[1];
25175 argv++;
25176 argc--;
25177 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025178 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025179 if (!filename) {
25180 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025181 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025182 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025183
25184 return 0;
25185}